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...@@ -167,6 +167,7 @@ pub fn createAtomForDecl(self: *ZigObject, allocator: Allocator, macho_file: *Ma
167 relocs.* = .{};167 relocs.* = .{};
168 const atom = self.getAtom(atom_index).?;168 const atom = self.getAtom(atom_index).?;
169 atom.addExtra(.{ .rel_index = relocs_index, .rel_count = 0 }, macho_file);169 atom.addExtra(.{ .rel_index = relocs_index, .rel_count = 0 }, macho_file);
170 try self.globals.append(allocator, 0);
170 return symbol_index;171 return symbol_index;
171}172}
172173
...@@ -1315,22 +1316,30 @@ pub fn updateExports(...@@ -1315,22 +1316,30 @@ pub fn updateExports(
1315 break :blk global_nlist_index;1316 break :blk global_nlist_index;
1316 };1317 };
1317 const global_nlist = &self.symtab.items(.nlist)[global_nlist_index];1318 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];
1318 global_nlist.n_value = nlist.n_value;1321 global_nlist.n_value = nlist.n_value;
1319 global_nlist.n_sect = nlist.n_sect;1322 global_nlist.n_sect = nlist.n_sect;
1320 global_nlist.n_type = macho.N_EXT | macho.N_SECT;1323 global_nlist.n_type = macho.N_EXT | macho.N_SECT;
1321 self.symtab.items(.size)[global_nlist_index] = self.symtab.items(.size)[nlist_idx];1324 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
1324 switch (exp.opts.linkage) {1328 switch (exp.opts.linkage) {
1325 .internal => {1329 .internal => {
1326 // Symbol should be hidden, or in MachO lingo, private extern.1330 // Symbol should be hidden, or in MachO lingo, private extern.
1327 global_nlist.n_type |= macho.N_PEXT;1331 global_nlist.n_type |= macho.N_PEXT;
1332 global_sym.visibility = .hidden;
1333 },
1334 .strong => {
1335 global_sym.visibility = .global;
1328 },1336 },
1329 .strong => {},
1330 .weak => {1337 .weak => {
1331 // Weak linkage is specified as part of n_desc field.1338 // Weak linkage is specified as part of n_desc field.
1332 // Symbol's n_type is like for a symbol with strong linkage.1339 // Symbol's n_type is like for a symbol with strong linkage.
1333 global_nlist.n_desc |= macho.N_WEAK_DEF;1340 global_nlist.n_desc |= macho.N_WEAK_DEF;
1341 global_sym.visibility = .global;
1342 global_sym.flags.weak = true;
1334 },1343 },
1335 else => unreachable,1344 else => unreachable,
1336 }1345 }
...@@ -1460,16 +1469,17 @@ pub fn getGlobalSymbol(self: *ZigObject, macho_file: *MachO, name: []const u8, l...@@ -1460,16 +1469,17 @@ pub fn getGlobalSymbol(self: *ZigObject, macho_file: *MachO, name: []const u8, l
1460 const off = try self.strtab.insert(gpa, sym_name);1469 const off = try self.strtab.insert(gpa, sym_name);
1461 const lookup_gop = try self.globals_lookup.getOrPut(gpa, off);1470 const lookup_gop = try self.globals_lookup.getOrPut(gpa, off);
1462 if (!lookup_gop.found_existing) {1471 if (!lookup_gop.found_existing) {
1472 const sym_index = try self.addSymbol(gpa);
1473 const sym = &self.symbols.items[sym_index];
1463 const nlist_index = try self.addNlist(gpa);1474 const nlist_index = try self.addNlist(gpa);
1464 const nlist = &self.symtab.items(.nlist)[nlist_index];1475 const nlist = &self.symtab.items(.nlist)[nlist_index];
1465 nlist.n_strx = off;1476 nlist.n_strx = off;
1466 nlist.n_type = macho.N_EXT;1477 nlist.n_type = macho.N_EXT;
1478 sym.name = off;
1479 sym.nlist_idx = nlist_index;
1480 sym.extra = try self.addSymbolExtra(gpa, .{});
1467 lookup_gop.value_ptr.* = nlist_index;1481 lookup_gop.value_ptr.* = nlist_index;
1468 _ = try macho_file.resolver.getOrPut(gpa, .{1482 try self.globals.append(gpa, 0);
1469 .index = nlist_index,
1470 .file = self.index,
1471 }, macho_file);
1472 try self.globals.append(gpa, nlist_index);
1473 }1483 }
1474 return lookup_gop.value_ptr.*;1484 return lookup_gop.value_ptr.*;
1475}1485}
...@@ -1692,7 +1702,7 @@ fn formatSymtab(...@@ -1692,7 +1702,7 @@ fn formatSymtab(
1692 try writer.writeAll(" symbols\n");1702 try writer.writeAll(" symbols\n");
1693 const self = ctx.self;1703 const self = ctx.self;
1694 const macho_file = ctx.macho_file;1704 const macho_file = ctx.macho_file;
1695 for (self.symbols.items, 0) |sym, i| {1705 for (self.symbols.items, 0..) |sym, i| {
1696 const ref = self.getSymbolRef(@intCast(i), macho_file);1706 const ref = self.getSymbolRef(@intCast(i), macho_file);
1697 if (ref.getFile(macho_file) == null) {1707 if (ref.getFile(macho_file) == null) {
1698 // TODO any better way of handling this?1708 // TODO any better way of handling this?