authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-31 22:36:10+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-01 14:22:44+02:00
logf2587de4e915423d0224b8b6ef0cb3d9cb7e60e2
treef03f33b4b1c0a80eea3e8cc10f1ae21186dceb40
parent381dc2d9509ffeaf60a1775ee0983c7dd1b9e346

macho: look for entry in archives/dylibs too


3 files changed, 48 insertions(+), 25 deletions(-)

src/link/MachO.zig+1-1
......@@ -3981,7 +3981,7 @@ pub fn getStubsAtomIndexForSymbol(self: *MachO, sym_with_loc: SymbolWithLoc) ?At
39813981/// Returns symbol location corresponding to the set entrypoint.
39823982/// Asserts output mode is executable.
39833983pub fn getEntryPoint(self: MachO) error{MissingMainEntrypoint}!SymbolWithLoc {
3984 const entry_name = self.base.options.entry orelse "_main";
3984 const entry_name = self.base.options.entry orelse load_commands.default_entry_point;
39853985 const global = self.getGlobal(entry_name) orelse {
39863986 log.err("entrypoint '{s}' not found", .{entry_name});
39873987 return error.MissingMainEntrypoint;
src/link/MachO/load_commands.zig+4
......@@ -8,6 +8,10 @@ const mem = std.mem;
88const Allocator = mem.Allocator;
99const Dylib = @import("Dylib.zig");
1010
11/// Default implicit entrypoint symbol name.
12pub const default_entry_point: []const u8 = "_main";
13
14/// Default path to dyld.
1115pub const default_dyld_path: [*:0]const u8 = "/usr/lib/dyld";
1216
1317fn calcInstallNameLen(cmd_size: u64, name: []const u8, assume_max_path_len: bool) u64 {
src/link/MachO/zld.zig+43-24
......@@ -932,6 +932,34 @@ pub const Zld = struct {
932932 }
933933 }
934934
935 fn resolveSymbols(self: *Zld, resolver: *SymbolResolver) !void {
936 // We add the specified entrypoint as the first unresolved symbols so that
937 // we search for it in libraries should there be no object files specified
938 // on the linker line.
939 if (self.options.output_mode == .Exe) {
940 const entry_name = self.options.entry orelse load_commands.default_entry_point;
941 const sym_index = try self.allocateSymbol();
942 const sym_loc = SymbolWithLoc{ .sym_index = sym_index };
943 const sym = self.getSymbolPtr(sym_loc);
944 sym.n_strx = try self.strtab.insert(self.gpa, entry_name);
945 sym.n_type = macho.N_UNDF | macho.N_EXT;
946 const global_index = try self.addGlobal(sym_loc);
947 try resolver.table.putNoClobber(entry_name, global_index);
948 try resolver.unresolved.putNoClobber(global_index, {});
949 }
950
951 for (self.objects.items, 0..) |_, object_id| {
952 try self.resolveSymbolsInObject(@intCast(u32, object_id), resolver);
953 }
954
955 try self.resolveSymbolsInArchives(resolver);
956 try self.resolveDyldStubBinder(resolver);
957 try self.resolveSymbolsInDylibs(resolver);
958 try self.createMhExecuteHeaderSymbol(resolver);
959 try self.createDsoHandleSymbol(resolver);
960 try self.resolveSymbolsAtLoading(resolver);
961 }
962
935963 fn resolveSymbolsInObject(self: *Zld, object_id: u32, resolver: *SymbolResolver) !void {
936964 const object = &self.objects.items[object_id];
937965 const in_symtab = object.in_symtab orelse return;
......@@ -975,9 +1003,7 @@ pub const Zld = struct {
9751003 const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = object_id + 1 };
9761004
9771005 const global_index = resolver.table.get(sym_name) orelse {
978 const gpa = self.gpa;
979 const global_index = @intCast(u32, self.globals.items.len);
980 try self.globals.append(gpa, sym_loc);
1006 const global_index = try self.addGlobal(sym_loc);
9811007 try resolver.table.putNoClobber(sym_name, global_index);
9821008 if (sym.undf() and !sym.tentative()) {
9831009 try resolver.unresolved.putNoClobber(global_index, {});
......@@ -1034,8 +1060,10 @@ pub const Zld = struct {
10341060 };
10351061
10361062 if (update_global) {
1037 const global_object = &self.objects.items[global.getFile().?];
1038 global_object.globals_lookup[global.sym_index] = global_index;
1063 if (global.getFile()) |file| {
1064 const global_object = &self.objects.items[file];
1065 global_object.globals_lookup[global.sym_index] = global_index;
1066 }
10391067 _ = resolver.unresolved.swapRemove(resolver.table.get(sym_name).?);
10401068 global.* = sym_loc;
10411069 } else {
......@@ -1180,9 +1208,7 @@ pub const Zld = struct {
11801208 global.* = sym_loc;
11811209 self.mh_execute_header_index = global_index;
11821210 } else {
1183 const global_index = @intCast(u32, self.globals.items.len);
1184 try self.globals.append(gpa, sym_loc);
1185 self.mh_execute_header_index = global_index;
1211 self.mh_execute_header_index = try self.addGlobal(sym_loc);
11861212 }
11871213 }
11881214
......@@ -1358,6 +1384,12 @@ pub const Zld = struct {
13581384 return index;
13591385 }
13601386
1387 fn addGlobal(self: *Zld, sym_loc: SymbolWithLoc) !u32 {
1388 const global_index = @intCast(u32, self.globals.items.len);
1389 try self.globals.append(self.gpa, sym_loc);
1390 return global_index;
1391 }
1392
13611393 fn allocateSpecialSymbols(self: *Zld) !void {
13621394 for (&[_]?u32{
13631395 self.dso_handle_index,
......@@ -3980,17 +4012,7 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
39804012 .table = std.StringHashMap(u32).init(arena),
39814013 .unresolved = std.AutoArrayHashMap(u32, void).init(arena),
39824014 };
3983
3984 for (zld.objects.items, 0..) |_, object_id| {
3985 try zld.resolveSymbolsInObject(@intCast(u32, object_id), &resolver);
3986 }
3987
3988 try zld.resolveSymbolsInArchives(&resolver);
3989 try zld.resolveDyldStubBinder(&resolver);
3990 try zld.resolveSymbolsInDylibs(&resolver);
3991 try zld.createMhExecuteHeaderSymbol(&resolver);
3992 try zld.createDsoHandleSymbol(&resolver);
3993 try zld.resolveSymbolsAtLoading(&resolver);
4015 try zld.resolveSymbols(&resolver);
39944016
39954017 if (resolver.unresolved.count() > 0) {
39964018 return error.UndefinedSymbolReference;
......@@ -4003,11 +4025,8 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
40034025 }
40044026
40054027 if (options.output_mode == .Exe) {
4006 const entry_name = options.entry orelse "_main";
4007 const global_index = resolver.table.get(entry_name) orelse {
4008 log.err("entrypoint '{s}' not found", .{entry_name});
4009 return error.MissingMainEntrypoint;
4010 };
4028 const entry_name = options.entry orelse load_commands.default_entry_point;
4029 const global_index = resolver.table.get(entry_name).?; // Error was flagged earlier
40114030 zld.entry_index = global_index;
40124031 }
40134032