This commit is contained in:
mlugg 2025-09-02 12:31:40 +01:00
parent fb88dab4c9
commit 89d862180f
No known key found for this signature in database
GPG key ID: 3F5B7DCCBF4AF02E
3 changed files with 28 additions and 65 deletions

View file

@ -1097,7 +1097,7 @@ const MachODumper = struct {
for (ctx.symtab.items) |sym| { for (ctx.symtab.items) |sym| {
const sym_name = ctx.getString(sym.n_strx); const sym_name = ctx.getString(sym.n_strx);
if (sym.stab()) { if (sym.n_type.bits.is_stab != 0) {
const tt = switch (sym.n_type) { const tt = switch (sym.n_type) {
macho.N_SO => "SO", macho.N_SO => "SO",
macho.N_OSO => "OSO", macho.N_OSO => "OSO",
@ -1114,7 +1114,7 @@ const MachODumper = struct {
try writer.print(" ({s},{s})", .{ sect.segName(), sect.sectName() }); try writer.print(" ({s},{s})", .{ sect.segName(), sect.sectName() });
} }
try writer.print(" {s} (stab) {s}\n", .{ tt, sym_name }); try writer.print(" {s} (stab) {s}\n", .{ tt, sym_name });
} else if (sym.sect()) { } else if (sym.n_type.type == .sect) {
const sect = ctx.sections.items[sym.n_sect - 1]; const sect = ctx.sections.items[sym.n_sect - 1];
try writer.print("{x} ({s},{s})", .{ try writer.print("{x} ({s},{s})", .{
sym.n_value, sym.n_value,
@ -1122,8 +1122,8 @@ const MachODumper = struct {
sect.sectName(), sect.sectName(),
}); });
if (sym.n_desc & macho.REFERENCED_DYNAMICALLY != 0) try writer.writeAll(" [referenced dynamically]"); if (sym.n_desc & macho.REFERENCED_DYNAMICALLY != 0) try writer.writeAll(" [referenced dynamically]");
if (sym.weakDef()) try writer.writeAll(" weak"); if (sym.n_desc.weak_def_or_ref_to_weak) try writer.writeAll(" weak");
if (sym.weakRef()) try writer.writeAll(" weakref"); if (sym.n_desc.weak_ref) try writer.writeAll(" weakref");
if (sym.ext()) { if (sym.ext()) {
if (sym.pext()) try writer.writeAll(" private"); if (sym.pext()) try writer.writeAll(" private");
try writer.writeAll(" external"); try writer.writeAll(" external");
@ -1134,7 +1134,7 @@ const MachODumper = struct {
try writer.print(" 0x{x:0>16} (common) (alignment 2^{d})", .{ sym.n_value, alignment }); try writer.print(" 0x{x:0>16} (common) (alignment 2^{d})", .{ sym.n_value, alignment });
if (sym.ext()) try writer.writeAll(" external"); if (sym.ext()) try writer.writeAll(" external");
try writer.print(" {s}\n", .{sym_name}); try writer.print(" {s}\n", .{sym_name});
} else if (sym.undf()) { } else if (sym.n_type.type == .undf) {
const ordinal = @divFloor(@as(i16, @bitCast(sym.n_desc)), macho.N_SYMBOL_RESOLVER); const ordinal = @divFloor(@as(i16, @bitCast(sym.n_desc)), macho.N_SYMBOL_RESOLVER);
const import_name = blk: { const import_name = blk: {
if (ordinal <= 0) { if (ordinal <= 0) {
@ -1153,7 +1153,7 @@ const MachODumper = struct {
break :blk basename[0..ext]; break :blk basename[0..ext];
}; };
try writer.writeAll("(undefined)"); try writer.writeAll("(undefined)");
if (sym.weakRef()) try writer.writeAll(" weakref"); if (sym.n_desc.weak_ref) try writer.writeAll(" weakref");
if (sym.ext()) try writer.writeAll(" external"); if (sym.ext()) try writer.writeAll(" external");
try writer.print(" {s} (from {s})\n", .{ try writer.print(" {s} (from {s})\n", .{
sym_name, sym_name,

View file

@ -906,43 +906,6 @@ pub const nlist_64 = extern struct {
}, },
n_value: u64, n_value: u64,
// MLUGG TODO DELETE
pub fn stab(sym: nlist_64) bool {
return sym.n_type.bits.is_stab != 0;
}
// MLUGG TODO DELETE
pub fn sect(sym: nlist_64) bool {
return sym.n_type.type == .sect;
}
// MLUGG TODO DELETE
pub fn undf(sym: nlist_64) bool {
return sym.n_type.type == .undf;
}
// MLUGG TODO DELETE
pub fn indr(sym: nlist_64) bool {
return sym.n_type.type == .indr;
}
// MLUGG TODO DELETE
pub fn abs(sym: nlist_64) bool {
return sym.n_type.type == .abs;
}
// MLUGG TODO DELETE
pub fn weakDef(sym: nlist_64) bool {
return sym.n_desc.weak_def_or_ref_to_weak;
}
// MLUGG TODO DELETE
pub fn weakRef(sym: nlist_64) bool {
return sym.n_desc.weak_ref;
}
// MLUGG TODO DELETE
pub fn discarded(sym: nlist_64) bool {
return sym.n_desc.discarded_or_no_dead_strip;
}
// MLUGG TODO DELETE
pub fn noDeadStrip(sym: nlist_64) bool {
return sym.n_desc.discarded_or_no_dead_strip;
}
pub fn tentative(sym: nlist_64) bool { pub fn tentative(sym: nlist_64) bool {
return sym.n_type.type == .undf and sym.n_value != 0; return sym.n_type.type == .undf and sym.n_value != 0;
} }

View file

@ -185,7 +185,7 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {
if (name[0] == 'l' or name[0] == 'L') return 4; if (name[0] == 'l' or name[0] == 'L') return 4;
return 3; return 3;
} }
return if (nl.weakDef()) 2 else 1; return if (nl.n_desc.weak_def_or_ref_to_weak) 2 else 1;
} }
fn lessThan(ctx: *const Object, lhs: @This(), rhs: @This()) bool { fn lessThan(ctx: *const Object, lhs: @This(), rhs: @This()) bool {
@ -202,7 +202,7 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {
var nlists = try std.array_list.Managed(NlistIdx).initCapacity(gpa, self.symtab.items(.nlist).len); var nlists = try std.array_list.Managed(NlistIdx).initCapacity(gpa, self.symtab.items(.nlist).len);
defer nlists.deinit(); defer nlists.deinit();
for (self.symtab.items(.nlist), 0..) |nlist, i| { for (self.symtab.items(.nlist), 0..) |nlist, i| {
if (nlist.stab() or !nlist.sect()) continue; if (nlist.n_type.bits.is_stab != 0 or nlist.n_type.type != .sect) continue;
nlists.appendAssumeCapacity(.{ .nlist = nlist, .idx = i }); nlists.appendAssumeCapacity(.{ .nlist = nlist, .idx = i });
} }
mem.sort(NlistIdx, nlists.items, self, NlistIdx.lessThan); mem.sort(NlistIdx, nlists.items, self, NlistIdx.lessThan);
@ -805,7 +805,7 @@ fn linkNlistToAtom(self: *Object, macho_file: *MachO) !void {
const tracy = trace(@src()); const tracy = trace(@src());
defer tracy.end(); defer tracy.end();
for (self.symtab.items(.nlist), self.symtab.items(.atom)) |nlist, *atom| { for (self.symtab.items(.nlist), self.symtab.items(.atom)) |nlist, *atom| {
if (!nlist.stab() and nlist.sect()) { if (!nlist.n_type.bits.is_stab != 0 and nlist.n_type.type == .sect) {
const sect = self.sections.items(.header)[nlist.n_sect - 1]; const sect = self.sections.items(.header)[nlist.n_sect - 1];
const subs = self.sections.items(.subsections)[nlist.n_sect - 1].items; const subs = self.sections.items(.subsections)[nlist.n_sect - 1].items;
if (nlist.n_value == sect.addr) { if (nlist.n_value == sect.addr) {
@ -852,30 +852,30 @@ fn initSymbols(self: *Object, allocator: Allocator, macho_file: *MachO) !void {
symbol.extra = self.addSymbolExtraAssumeCapacity(.{}); symbol.extra = self.addSymbolExtraAssumeCapacity(.{});
if (self.getAtom(atom_index)) |atom| { if (self.getAtom(atom_index)) |atom| {
assert(!nlist.abs()); assert(nlist.n_type.type != .abs);
symbol.value -= atom.getInputAddress(macho_file); symbol.value -= atom.getInputAddress(macho_file);
symbol.atom_ref = .{ .index = atom_index, .file = self.index }; symbol.atom_ref = .{ .index = atom_index, .file = self.index };
} }
symbol.flags.weak = nlist.weakDef(); symbol.flags.weak = nlist.n_desc.weak_def_or_ref_to_weak;
symbol.flags.abs = nlist.abs(); symbol.flags.abs = nlist.n_type.type == .abs;
symbol.flags.tentative = nlist.tentative(); symbol.flags.tentative = nlist.tentative();
symbol.flags.no_dead_strip = symbol.flags.no_dead_strip or nlist.noDeadStrip(); symbol.flags.no_dead_strip = symbol.flags.no_dead_strip or nlist.n_desc.discarded_or_no_dead_strip;
symbol.flags.dyn_ref = nlist.n_desc & macho.REFERENCED_DYNAMICALLY != 0; symbol.flags.dyn_ref = nlist.n_desc & macho.REFERENCED_DYNAMICALLY != 0;
symbol.flags.interposable = false; symbol.flags.interposable = false;
// TODO // TODO
// symbol.flags.interposable = nlist.ext() and (nlist.sect() or nlist.abs()) and macho_file.base.isDynLib() and macho_file.options.namespace == .flat and !nlist.pext(); // symbol.flags.interposable = nlist.ext() and (nlist.n_type.type == .sect or nlist.n_type.type == .abs) and macho_file.base.isDynLib() and macho_file.options.namespace == .flat and !nlist.pext();
if (nlist.sect() and if (nlist.n_type.type == .sect and
self.sections.items(.header)[nlist.n_sect - 1].type() == macho.S_THREAD_LOCAL_VARIABLES) self.sections.items(.header)[nlist.n_sect - 1].type() == macho.S_THREAD_LOCAL_VARIABLES)
{ {
symbol.flags.tlv = true; symbol.flags.tlv = true;
} }
if (nlist.ext()) { if (nlist.ext()) {
if (nlist.undf()) { if (nlist.n_type.type == .undf) {
symbol.flags.weak_ref = nlist.weakRef(); symbol.flags.weak_ref = nlist.n_desc.weak_ref;
} else if (nlist.pext() or (nlist.weakDef() and nlist.weakRef()) or self.hidden) { } else if (nlist.pext() or (nlist.n_desc.weak_def_or_ref_to_weak and nlist.n_desc.weak_ref) or self.hidden) {
symbol.visibility = .hidden; symbol.visibility = .hidden;
} else { } else {
symbol.visibility = .global; symbol.visibility = .global;
@ -902,10 +902,10 @@ fn initSymbolStabs(self: *Object, allocator: Allocator, nlists: anytype, macho_f
}; };
const start: u32 = for (self.symtab.items(.nlist), 0..) |nlist, i| { const start: u32 = for (self.symtab.items(.nlist), 0..) |nlist, i| {
if (nlist.stab()) break @intCast(i); if (nlist.n_type.bits.is_stab != 0) break @intCast(i);
} else @intCast(self.symtab.items(.nlist).len); } else @intCast(self.symtab.items(.nlist).len);
const end: u32 = for (self.symtab.items(.nlist)[start..], start..) |nlist, i| { const end: u32 = for (self.symtab.items(.nlist)[start..], start..) |nlist, i| {
if (!nlist.stab()) break @intCast(i); if (nlist.n_type.bits.is_stab == 0) break @intCast(i);
} else @intCast(self.symtab.items(.nlist).len); } else @intCast(self.symtab.items(.nlist).len);
if (start == end) return; if (start == end) return;
@ -919,7 +919,7 @@ fn initSymbolStabs(self: *Object, allocator: Allocator, nlists: anytype, macho_f
var addr_lookup = std.StringHashMap(u64).init(allocator); var addr_lookup = std.StringHashMap(u64).init(allocator);
defer addr_lookup.deinit(); defer addr_lookup.deinit();
for (syms) |sym| { for (syms) |sym| {
if (sym.sect() and (sym.ext() or sym.pext())) { if (sym.n_type.type == .sect and (sym.ext() or sym.pext())) {
try addr_lookup.putNoClobber(self.getNStrx(sym.n_strx), sym.n_value); try addr_lookup.putNoClobber(self.getNStrx(sym.n_strx), sym.n_value);
} }
} }
@ -1241,8 +1241,8 @@ fn parseUnwindRecords(self: *Object, allocator: Allocator, cpu_arch: std.Target.
const slice = self.symtab.slice(); const slice = self.symtab.slice();
for (slice.items(.nlist), slice.items(.atom), slice.items(.size)) |nlist, atom, size| { for (slice.items(.nlist), slice.items(.atom), slice.items(.size)) |nlist, atom, size| {
if (nlist.stab()) continue; if (nlist.n_type.bits.is_stab != 0) continue;
if (!nlist.sect()) continue; if (nlist.n_type.type != .sect) continue;
const sect = self.sections.items(.header)[nlist.n_sect - 1]; const sect = self.sections.items(.header)[nlist.n_sect - 1];
if (sect.isCode() and sect.size > 0) { if (sect.isCode() and sect.size > 0) {
try superposition.ensureUnusedCapacity(1); try superposition.ensureUnusedCapacity(1);
@ -1459,7 +1459,7 @@ pub fn resolveSymbols(self: *Object, macho_file: *MachO) !void {
for (self.symtab.items(.nlist), self.symtab.items(.atom), self.globals.items, 0..) |nlist, atom_index, *global, i| { for (self.symtab.items(.nlist), self.symtab.items(.atom), self.globals.items, 0..) |nlist, atom_index, *global, i| {
if (!nlist.ext()) continue; if (!nlist.ext()) continue;
if (nlist.sect()) { if (nlist.n_type.type == .sect) {
const atom = self.getAtom(atom_index).?; const atom = self.getAtom(atom_index).?;
if (!atom.isAlive()) continue; if (!atom.isAlive()) continue;
} }
@ -1473,7 +1473,7 @@ pub fn resolveSymbols(self: *Object, macho_file: *MachO) !void {
} }
global.* = gop.index; global.* = gop.index;
if (nlist.undf() and !nlist.tentative()) continue; if (nlist.n_type.type == .undf and !nlist.tentative()) continue;
if (gop.ref.getFile(macho_file) == null) { if (gop.ref.getFile(macho_file) == null) {
gop.ref.* = .{ .index = @intCast(i), .file = self.index }; gop.ref.* = .{ .index = @intCast(i), .file = self.index };
continue; continue;
@ -1481,7 +1481,7 @@ pub fn resolveSymbols(self: *Object, macho_file: *MachO) !void {
if (self.asFile().getSymbolRank(.{ if (self.asFile().getSymbolRank(.{
.archive = !self.alive, .archive = !self.alive,
.weak = nlist.weakDef(), .weak = nlist.n_desc.weak_def_or_ref_to_weak,
.tentative = nlist.tentative(), .tentative = nlist.tentative(),
}) < gop.ref.getSymbol(macho_file).?.getSymbolRank(macho_file)) { }) < gop.ref.getSymbol(macho_file).?.getSymbolRank(macho_file)) {
gop.ref.* = .{ .index = @intCast(i), .file = self.index }; gop.ref.* = .{ .index = @intCast(i), .file = self.index };
@ -1500,7 +1500,7 @@ pub fn markLive(self: *Object, macho_file: *MachO) void {
const ref = self.getSymbolRef(@intCast(i), macho_file); const ref = self.getSymbolRef(@intCast(i), macho_file);
const file = ref.getFile(macho_file) orelse continue; const file = ref.getFile(macho_file) orelse continue;
const sym = ref.getSymbol(macho_file).?; const sym = ref.getSymbol(macho_file).?;
const should_keep = nlist.undf() or (nlist.tentative() and !sym.flags.tentative); const should_keep = nlist.n_type.type == .undf or (nlist.tentative() and !sym.flags.tentative);
if (should_keep and file == .object and !file.object.alive) { if (should_keep and file == .object and !file.object.alive) {
file.object.alive = true; file.object.alive = true;
file.object.markLive(macho_file); file.object.markLive(macho_file);
@ -1685,7 +1685,7 @@ pub fn parseAr(self: *Object, macho_file: *MachO) !void {
pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, macho_file: *MachO) error{OutOfMemory}!void { pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, macho_file: *MachO) error{OutOfMemory}!void {
const gpa = macho_file.base.comp.gpa; const gpa = macho_file.base.comp.gpa;
for (self.symtab.items(.nlist)) |nlist| { for (self.symtab.items(.nlist)) |nlist| {
if (!nlist.ext() or (nlist.undf() and !nlist.tentative())) continue; if (!nlist.ext() or (nlist.n_type.type == .undf and !nlist.tentative())) continue;
const off = try ar_symtab.strtab.insert(gpa, self.getNStrx(nlist.n_strx)); const off = try ar_symtab.strtab.insert(gpa, self.getNStrx(nlist.n_strx));
try ar_symtab.entries.append(gpa, .{ .off = off, .file = self.index }); try ar_symtab.entries.append(gpa, .{ .off = off, .file = self.index });
} }