authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-12 13:18:52+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-18 09:13:08+02:00
log2579c55d4903b016e0fb6d1f52fbbfe4a2a71c26
tree517d7557e6b56ff98c6315643737d2a1adca404b
parent4aff0ec394cb6ef42db982df86caf8976b558d43

macho: adjust global creation in ZigObject to new model


1 files changed, 18 insertions(+), 8 deletions(-)

src/link/MachO/ZigObject.zig+18-8
......@@ -167,6 +167,7 @@ pub fn createAtomForDecl(self: *ZigObject, allocator: Allocator, macho_file: *Ma
167167 relocs.* = .{};
168168 const atom = self.getAtom(atom_index).?;
169169 atom.addExtra(.{ .rel_index = relocs_index, .rel_count = 0 }, macho_file);
170 try self.globals.append(allocator, 0);
170171 return symbol_index;
171172}
172173
......@@ -1315,22 +1316,30 @@ pub fn updateExports(
13151316 break :blk global_nlist_index;
13161317 };
13171318 const global_nlist = &self.symtab.items(.nlist)[global_nlist_index];
1319 const atom_index = self.symtab.items(.atom)[nlist_idx];
1320 const global_sym = &self.symbols.items[global_nlist_index];
13181321 global_nlist.n_value = nlist.n_value;
13191322 global_nlist.n_sect = nlist.n_sect;
13201323 global_nlist.n_type = macho.N_EXT | macho.N_SECT;
13211324 self.symtab.items(.size)[global_nlist_index] = self.symtab.items(.size)[nlist_idx];
1322 self.symtab.items(.atom)[global_nlist_index] = self.symtab.items(.atom)[nlist_idx];
1325 self.symtab.items(.atom)[global_nlist_index] = atom_index;
1326 global_sym.atom_ref = .{ .index = atom_index, .file = self.index };
13231327
13241328 switch (exp.opts.linkage) {
13251329 .internal => {
13261330 // Symbol should be hidden, or in MachO lingo, private extern.
13271331 global_nlist.n_type |= macho.N_PEXT;
1332 global_sym.visibility = .hidden;
1333 },
1334 .strong => {
1335 global_sym.visibility = .global;
13281336 },
1329 .strong => {},
13301337 .weak => {
13311338 // Weak linkage is specified as part of n_desc field.
13321339 // Symbol's n_type is like for a symbol with strong linkage.
13331340 global_nlist.n_desc |= macho.N_WEAK_DEF;
1341 global_sym.visibility = .global;
1342 global_sym.flags.weak = true;
13341343 },
13351344 else => unreachable,
13361345 }
......@@ -1460,16 +1469,17 @@ pub fn getGlobalSymbol(self: *ZigObject, macho_file: *MachO, name: []const u8, l
14601469 const off = try self.strtab.insert(gpa, sym_name);
14611470 const lookup_gop = try self.globals_lookup.getOrPut(gpa, off);
14621471 if (!lookup_gop.found_existing) {
1472 const sym_index = try self.addSymbol(gpa);
1473 const sym = &self.symbols.items[sym_index];
14631474 const nlist_index = try self.addNlist(gpa);
14641475 const nlist = &self.symtab.items(.nlist)[nlist_index];
14651476 nlist.n_strx = off;
14661477 nlist.n_type = macho.N_EXT;
1478 sym.name = off;
1479 sym.nlist_idx = nlist_index;
1480 sym.extra = try self.addSymbolExtra(gpa, .{});
14671481 lookup_gop.value_ptr.* = nlist_index;
1468 _ = try macho_file.resolver.getOrPut(gpa, .{
1469 .index = nlist_index,
1470 .file = self.index,
1471 }, macho_file);
1472 try self.globals.append(gpa, nlist_index);
1482 try self.globals.append(gpa, 0);
14731483 }
14741484 return lookup_gop.value_ptr.*;
14751485}
......@@ -1692,7 +1702,7 @@ fn formatSymtab(
16921702 try writer.writeAll(" symbols\n");
16931703 const self = ctx.self;
16941704 const macho_file = ctx.macho_file;
1695 for (self.symbols.items, 0) |sym, i| {
1705 for (self.symbols.items, 0..) |sym, i| {
16961706 const ref = self.getSymbolRef(@intCast(i), macho_file);
16971707 if (ref.getFile(macho_file) == null) {
16981708 // TODO any better way of handling this?