macho: adjust global creation in ZigObject to new model

This commit is contained in:
Jakub Konka 2024-07-12 13:18:52 +02:00
parent 4aff0ec394
commit 2579c55d49

View File

@ -167,6 +167,7 @@ pub fn createAtomForDecl(self: *ZigObject, allocator: Allocator, macho_file: *Ma
relocs.* = .{};
const atom = self.getAtom(atom_index).?;
atom.addExtra(.{ .rel_index = relocs_index, .rel_count = 0 }, macho_file);
try self.globals.append(allocator, 0);
return symbol_index;
}
@ -1315,22 +1316,30 @@ pub fn updateExports(
break :blk global_nlist_index;
};
const global_nlist = &self.symtab.items(.nlist)[global_nlist_index];
const atom_index = self.symtab.items(.atom)[nlist_idx];
const global_sym = &self.symbols.items[global_nlist_index];
global_nlist.n_value = nlist.n_value;
global_nlist.n_sect = nlist.n_sect;
global_nlist.n_type = macho.N_EXT | macho.N_SECT;
self.symtab.items(.size)[global_nlist_index] = self.symtab.items(.size)[nlist_idx];
self.symtab.items(.atom)[global_nlist_index] = self.symtab.items(.atom)[nlist_idx];
self.symtab.items(.atom)[global_nlist_index] = atom_index;
global_sym.atom_ref = .{ .index = atom_index, .file = self.index };
switch (exp.opts.linkage) {
.internal => {
// Symbol should be hidden, or in MachO lingo, private extern.
global_nlist.n_type |= macho.N_PEXT;
global_sym.visibility = .hidden;
},
.strong => {
global_sym.visibility = .global;
},
.strong => {},
.weak => {
// Weak linkage is specified as part of n_desc field.
// Symbol's n_type is like for a symbol with strong linkage.
global_nlist.n_desc |= macho.N_WEAK_DEF;
global_sym.visibility = .global;
global_sym.flags.weak = true;
},
else => unreachable,
}
@ -1460,16 +1469,17 @@ pub fn getGlobalSymbol(self: *ZigObject, macho_file: *MachO, name: []const u8, l
const off = try self.strtab.insert(gpa, sym_name);
const lookup_gop = try self.globals_lookup.getOrPut(gpa, off);
if (!lookup_gop.found_existing) {
const sym_index = try self.addSymbol(gpa);
const sym = &self.symbols.items[sym_index];
const nlist_index = try self.addNlist(gpa);
const nlist = &self.symtab.items(.nlist)[nlist_index];
nlist.n_strx = off;
nlist.n_type = macho.N_EXT;
sym.name = off;
sym.nlist_idx = nlist_index;
sym.extra = try self.addSymbolExtra(gpa, .{});
lookup_gop.value_ptr.* = nlist_index;
_ = try macho_file.resolver.getOrPut(gpa, .{
.index = nlist_index,
.file = self.index,
}, macho_file);
try self.globals.append(gpa, nlist_index);
try self.globals.append(gpa, 0);
}
return lookup_gop.value_ptr.*;
}
@ -1692,7 +1702,7 @@ fn formatSymtab(
try writer.writeAll(" symbols\n");
const self = ctx.self;
const macho_file = ctx.macho_file;
for (self.symbols.items, 0) |sym, i| {
for (self.symbols.items, 0..) |sym, i| {
const ref = self.getSymbolRef(@intCast(i), macho_file);
if (ref.getFile(macho_file) == null) {
// TODO any better way of handling this?