authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-11 18:50:53+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-24 12:34:39+01:00
log6b617afe2a3904ad3d5c9e3542a30bd7f35ed54e
tree823e1748d5e8e763e5ce78fc96c396b3a77bcd76
parent8a1311733b313a59afa2ea354ca9b342a935d869

macho: resolve synthetic symbols


2 files changed, 54 insertions(+), 6 deletions(-)

src/link/MachO.zig+48
......@@ -498,6 +498,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, prog_node: *std.Progress.Node
498498
499499 try self.addUndefinedGlobals();
500500 try self.resolveSymbols();
501 try self.resolveSyntheticSymbols();
501502
502503 state_log.debug("{}", .{self.dumpState()});
503504
......@@ -1215,6 +1216,53 @@ fn markLive(self: *MachO) void {
12151216 }
12161217}
12171218
1219fn resolveSyntheticSymbols(self: *MachO) !void {
1220 const internal = self.getInternalObject() orelse return;
1221
1222 if (!self.base.isDynLib()) {
1223 self.mh_execute_header_index = try internal.addSymbol("__mh_execute_header", self);
1224 const sym = self.getSymbol(self.mh_execute_header_index.?);
1225 sym.flags.@"export" = true;
1226 sym.flags.dyn_ref = true;
1227 sym.visibility = .global;
1228 } else {
1229 self.mh_dylib_header_index = try internal.addSymbol("__mh_dylib_header", self);
1230 }
1231
1232 self.dso_handle_index = try internal.addSymbol("___dso_handle", self);
1233 self.dyld_private_index = try internal.addSymbol("dyld_private", self);
1234
1235 {
1236 const gpa = self.base.comp.gpa;
1237 var boundary_symbols = std.AutoHashMap(Symbol.Index, void).init(gpa);
1238 defer boundary_symbols.deinit();
1239
1240 for (self.objects.items) |index| {
1241 const object = self.getFile(index).?.object;
1242 for (object.symbols.items, 0..) |sym_index, i| {
1243 const nlist = object.symtab.items(.nlist)[i];
1244 const name = self.getSymbol(sym_index).getName(self);
1245 if (!nlist.undf() or !nlist.ext()) continue;
1246 if (mem.startsWith(u8, name, "segment$start$") or
1247 mem.startsWith(u8, name, "segment$stop$") or
1248 mem.startsWith(u8, name, "section$start$") or
1249 mem.startsWith(u8, name, "section$stop$"))
1250 {
1251 _ = try boundary_symbols.put(sym_index, {});
1252 }
1253 }
1254 }
1255
1256 try self.boundary_symbols.ensureTotalCapacityPrecise(gpa, boundary_symbols.count());
1257
1258 var it = boundary_symbols.iterator();
1259 while (it.next()) |entry| {
1260 _ = try internal.addSymbol(self.getSymbol(entry.key_ptr.*).getName(self), self);
1261 self.boundary_symbols.appendAssumeCapacity(entry.key_ptr.*);
1262 }
1263 }
1264}
1265
12181266fn shrinkAtom(self: *MachO, atom_index: Atom.Index, new_block_size: u64) void {
12191267 _ = self;
12201268 _ = atom_index;
src/link/MachO/InternalObject.zig+6-6
......@@ -20,9 +20,9 @@ pub fn deinit(self: *InternalObject, allocator: Allocator) void {
2020}
2121
2222pub fn addSymbol(self: *InternalObject, name: [:0]const u8, macho_file: *MachO) !Symbol.Index {
23 const gpa = macho_file.base.allocator;
23 const gpa = macho_file.base.comp.gpa;
2424 try self.symbols.ensureUnusedCapacity(gpa, 1);
25 const off = try macho_file.string_intern.insert(gpa, name);
25 const off = try macho_file.strings.insert(gpa, name);
2626 const gop = try macho_file.getOrCreateGlobal(off);
2727 self.symbols.addOneAssumeCapacity().* = gop.index;
2828 const sym = macho_file.getSymbol(gop.index);
......@@ -37,7 +37,7 @@ pub fn addObjcMsgsendSections(self: *InternalObject, sym_name: []const u8, macho
3737}
3838
3939fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_file: *MachO) !Atom.Index {
40 const gpa = macho_file.base.allocator;
40 const gpa = macho_file.base.comp.gpa;
4141 const atom_index = try macho_file.addAtom();
4242 try self.atoms.append(gpa, atom_index);
4343
......@@ -45,7 +45,7 @@ fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_fil
4545 defer gpa.free(name);
4646 const atom = macho_file.getAtom(atom_index).?;
4747 atom.atom_index = atom_index;
48 atom.name = try macho_file.string_intern.insert(gpa, name);
48 atom.name = try macho_file.strings.insert(gpa, name);
4949 atom.file = self.index;
5050 atom.size = methname.len + 1;
5151 atom.alignment = 0;
......@@ -71,7 +71,7 @@ fn addObjcSelrefsSection(
7171 methname_atom_index: Atom.Index,
7272 macho_file: *MachO,
7373) !Atom.Index {
74 const gpa = macho_file.base.allocator;
74 const gpa = macho_file.base.comp.gpa;
7575 const atom_index = try macho_file.addAtom();
7676 try self.atoms.append(gpa, atom_index);
7777
......@@ -79,7 +79,7 @@ fn addObjcSelrefsSection(
7979 defer gpa.free(name);
8080 const atom = macho_file.getAtom(atom_index).?;
8181 atom.atom_index = atom_index;
82 atom.name = try macho_file.string_intern.insert(gpa, name);
82 atom.name = try macho_file.strings.insert(gpa, name);
8383 atom.file = self.index;
8484 atom.size = @sizeOf(u64);
8585 atom.alignment = 3;