fix compiler ftbfs from std.macho and std.dwarf changes

This commit is contained in:
mlugg 2025-09-08 14:03:11 +01:00
parent 202aeacc05
commit 0c7b2a7bd5
No known key found for this signature in database
GPG Key ID: 3F5B7DCCBF4AF02E
12 changed files with 165 additions and 147 deletions

View File

@ -603,13 +603,13 @@ fn readEhPointerAbs(r: *Reader, enc_ty: EH.PE.Type, addr_size_bytes: u8, endian:
/// Returns `error.InvalidDebugInfo` if the encoding is `EH.PE.omit`. /// Returns `error.InvalidDebugInfo` if the encoding is `EH.PE.omit`.
fn readEhPointer(r: *Reader, enc: EH.PE, addr_size_bytes: u8, ctx: EhPointerContext, endian: Endian) !u64 { fn readEhPointer(r: *Reader, enc: EH.PE, addr_size_bytes: u8, ctx: EhPointerContext, endian: Endian) !u64 {
const offset = try readEhPointerAbs(r, enc.type, addr_size_bytes, endian); const offset = try readEhPointerAbs(r, enc.type, addr_size_bytes, endian);
if (enc.indirect) return bad(); // GCC extension; not supported
const base = switch (enc.rel) { const base = switch (enc.rel) {
.abs, .aligned => 0, .abs, .aligned => 0,
.pcrel => ctx.pc_rel_base, .pcrel => ctx.pc_rel_base,
.textrel => ctx.text_rel_base orelse return bad(), .textrel => ctx.text_rel_base orelse return bad(),
.datarel => ctx.data_rel_base orelse return bad(), .datarel => ctx.data_rel_base orelse return bad(),
.funcrel => ctx.function_rel_base orelse return bad(), .funcrel => ctx.function_rel_base orelse return bad(),
.indirect => return bad(), // GCC extension; not supported
_ => return bad(), _ => return bad(),
}; };
return switch (offset) { return switch (offset) {

View File

@ -1,6 +1,8 @@
pub const PE = packed struct(u8) { pub const PE = packed struct(u8) {
type: Type, type: Type,
rel: Rel, rel: Rel,
/// Undocumented GCC extension
indirect: bool = false,
/// This is a special encoding which does not correspond to named `type`/`rel` values. /// This is a special encoding which does not correspond to named `type`/`rel` values.
pub const omit: PE = @bitCast(@as(u8, 0xFF)); pub const omit: PE = @bitCast(@as(u8, 0xFF));
@ -18,15 +20,15 @@ pub const PE = packed struct(u8) {
_, _,
}; };
pub const Rel = enum(u4) { /// The specification considers this a `u4`, but the GCC `indirect` field extension conflicts
/// with that, so we consider it a `u3` instead.
pub const Rel = enum(u3) {
abs = 0x0, abs = 0x0,
pcrel = 0x1, pcrel = 0x1,
textrel = 0x2, textrel = 0x2,
datarel = 0x3, datarel = 0x3,
funcrel = 0x4, funcrel = 0x4,
aligned = 0x5, aligned = 0x5,
/// Undocumented GCC extension
indirect = 0x8,
_, _,
}; };
}; };

View File

@ -4852,7 +4852,7 @@ fn flushWriterError(dwarf: *Dwarf, pt: Zcu.PerThread) (FlushError || Writer.Erro
hw.writeSleb128(dwarf.debug_frame.header.data_alignment_factor) catch unreachable; hw.writeSleb128(dwarf.debug_frame.header.data_alignment_factor) catch unreachable;
hw.writeUleb128(dwarf.debug_frame.header.return_address_register) catch unreachable; hw.writeUleb128(dwarf.debug_frame.header.return_address_register) catch unreachable;
hw.writeUleb128(1) catch unreachable; hw.writeUleb128(1) catch unreachable;
hw.writeByte(DW.EH.PE.pcrel | DW.EH.PE.sdata4) catch unreachable; hw.writeByte(@bitCast(@as(DW.EH.PE, .{ .type = .sdata4, .rel = .pcrel }))) catch unreachable;
hw.writeByte(DW.CFA.def_cfa_sf) catch unreachable; hw.writeByte(DW.CFA.def_cfa_sf) catch unreachable;
hw.writeUleb128(Register.rsp.dwarfNum()) catch unreachable; hw.writeUleb128(Register.rsp.dwarfNum()) catch unreachable;
hw.writeSleb128(-1) catch unreachable; hw.writeSleb128(-1) catch unreachable;

View File

@ -456,10 +456,10 @@ pub fn writeEhFrameRelocs(elf_file: *Elf, relocs: *std.array_list.Managed(elf.El
pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void { pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
try writer.writeByte(1); // version try writer.writeByte(1); // version
try writer.writeByte(DW_EH_PE.pcrel | DW_EH_PE.sdata4); // eh_frame_ptr_enc try writer.writeByte(@bitCast(@as(DW_EH_PE, .{ .type = .sdata4, .rel = .pcrel }))); // eh_frame_ptr_enc
// Building the lookup table would be expensive work on every `flush` -- omit it. // Building the lookup table would be expensive work on every `flush` -- omit it.
try writer.writeByte(DW_EH_PE.omit); // fde_count_enc try writer.writeByte(@bitCast(DW_EH_PE.omit)); // fde_count_enc
try writer.writeByte(DW_EH_PE.omit); // table_enc try writer.writeByte(@bitCast(DW_EH_PE.omit)); // table_enc
const shdrs = elf_file.sections.items(.shdr); const shdrs = elf_file.sections.items(.shdr);
const eh_frame_shdr = shdrs[elf_file.section_indexes.eh_frame.?]; const eh_frame_shdr = shdrs[elf_file.section_indexes.eh_frame.?];

View File

@ -4149,9 +4149,9 @@ pub const SymtabCtx = struct {
pub const null_sym = macho.nlist_64{ pub const null_sym = macho.nlist_64{
.n_strx = 0, .n_strx = 0,
.n_type = 0, .n_type = @bitCast(@as(u8, 0)),
.n_sect = 0, .n_sect = 0,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = 0, .n_value = 0,
}; };

View File

@ -69,9 +69,9 @@ pub fn initSymbols(self: *InternalObject, macho_file: *MachO) !void {
const nlist = obj.symtab.addOneAssumeCapacity(); const nlist = obj.symtab.addOneAssumeCapacity();
nlist.* = .{ nlist.* = .{
.n_strx = name.pos, .n_strx = name.pos,
.n_type = args.type, .n_type = @bitCast(args.type),
.n_sect = 0, .n_sect = 0,
.n_desc = args.desc, .n_desc = @bitCast(args.desc),
.n_value = 0, .n_value = 0,
}; };
symbol.nlist_idx = nlist_idx; symbol.nlist_idx = nlist_idx;
@ -143,7 +143,7 @@ pub fn resolveSymbols(self: *InternalObject, macho_file: *MachO) !void {
} }
global.* = gop.index; global.* = gop.index;
if (nlist.undf()) continue; if (nlist.n_type.bits.type == .undf) 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;
@ -171,7 +171,7 @@ pub fn resolveBoundarySymbols(self: *InternalObject, macho_file: *MachO) !void {
const object = macho_file.getFile(index).?.object; const object = macho_file.getFile(index).?.object;
for (object.symbols.items, 0..) |sym, i| { for (object.symbols.items, 0..) |sym, i| {
const nlist = object.symtab.items(.nlist)[i]; const nlist = object.symtab.items(.nlist)[i];
if (!nlist.undf() or !nlist.ext()) continue; if (nlist.n_type.bits.type != .undf or !nlist.n_type.bits.ext) continue;
const ref = object.getSymbolRef(@intCast(i), macho_file); const ref = object.getSymbolRef(@intCast(i), macho_file);
if (ref.getFile(macho_file) != null) continue; if (ref.getFile(macho_file) != null) continue;
const name = sym.getName(macho_file); const name = sym.getName(macho_file);
@ -206,9 +206,9 @@ pub fn resolveBoundarySymbols(self: *InternalObject, macho_file: *MachO) !void {
const nlist = self.symtab.addOneAssumeCapacity(); const nlist = self.symtab.addOneAssumeCapacity();
nlist.* = .{ nlist.* = .{
.n_strx = name_str.pos, .n_strx = name_str.pos,
.n_type = macho.N_SECT, .n_type = .{ .bits = .{ .ext = false, .type = .sect, .pext = false, .is_stab = 0 } },
.n_sect = 0, .n_sect = 0,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = 0, .n_value = 0,
}; };
sym.nlist_idx = nlist_idx; sym.nlist_idx = nlist_idx;
@ -226,7 +226,7 @@ pub fn markLive(self: *InternalObject, macho_file: *MachO) void {
for (0..self.symbols.items.len) |i| { for (0..self.symbols.items.len) |i| {
const nlist = self.symtab.items[i]; const nlist = self.symtab.items[i];
if (!nlist.ext()) continue; if (!nlist.n_type.bits.ext) continue;
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;
@ -273,9 +273,9 @@ fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_fil
const nlist = try self.symtab.addOne(gpa); const nlist = try self.symtab.addOne(gpa);
nlist.* = .{ nlist.* = .{
.n_strx = name_str.pos, .n_strx = name_str.pos,
.n_type = macho.N_SECT, .n_type = .{ .bits = .{ .ext = false, .type = .sect, .pext = false, .is_stab = 0 } },
.n_sect = @intCast(n_sect + 1), .n_sect = @intCast(n_sect + 1),
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = 0, .n_value = 0,
}; };
sym.nlist_idx = nlist_idx; sym.nlist_idx = nlist_idx;
@ -326,9 +326,9 @@ fn addObjcSelrefsSection(self: *InternalObject, methname_sym_index: Symbol.Index
const nlist = try self.symtab.addOne(gpa); const nlist = try self.symtab.addOne(gpa);
nlist.* = .{ nlist.* = .{
.n_strx = 0, .n_strx = 0,
.n_type = macho.N_SECT, .n_type = .{ .bits = .{ .ext = false, .type = .sect, .pext = false, .is_stab = 0 } },
.n_sect = @intCast(n_sect + 1), .n_sect = @intCast(n_sect + 1),
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = 0, .n_value = 0,
}; };
sym.nlist_idx = nlist_idx; sym.nlist_idx = nlist_idx;
@ -352,8 +352,8 @@ pub fn resolveObjcMsgSendSymbols(self: *InternalObject, macho_file: *MachO) !voi
for (object.symbols.items, 0..) |sym, i| { for (object.symbols.items, 0..) |sym, i| {
const nlist = object.symtab.items(.nlist)[i]; const nlist = object.symtab.items(.nlist)[i];
if (!nlist.ext()) continue; if (!nlist.n_type.bits.ext) continue;
if (!nlist.undf()) continue; if (nlist.n_type.bits.type != .undf) continue;
const ref = object.getSymbolRef(@intCast(i), macho_file); const ref = object.getSymbolRef(@intCast(i), macho_file);
if (ref.getFile(macho_file) != null) continue; if (ref.getFile(macho_file) != null) continue;
@ -381,9 +381,9 @@ pub fn resolveObjcMsgSendSymbols(self: *InternalObject, macho_file: *MachO) !voi
const nlist = try self.symtab.addOne(gpa); const nlist = try self.symtab.addOne(gpa);
nlist.* = .{ nlist.* = .{
.n_strx = name_str.pos, .n_strx = name_str.pos,
.n_type = macho.N_SECT | macho.N_EXT | macho.N_PEXT, .n_type = .{ .bits = .{ .ext = true, .type = .sect, .pext = true, .is_stab = 0 } },
.n_sect = 0, .n_sect = 0,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = 0, .n_value = 0,
}; };
sym.nlist_idx = nlist_idx; sym.nlist_idx = nlist_idx;

View File

@ -179,7 +179,7 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {
idx: usize, idx: usize,
fn rank(ctx: *const Object, nl: macho.nlist_64) u8 { fn rank(ctx: *const Object, nl: macho.nlist_64) u8 {
if (!nl.ext()) { if (!nl.n_type.bits.ext) {
const name = ctx.getNStrx(nl.n_strx); const name = ctx.getNStrx(nl.n_strx);
if (name.len == 0) return 5; if (name.len == 0) return 5;
if (name[0] == 'l' or name[0] == 'L') return 4; if (name[0] == 'l' or name[0] == 'L') return 4;
@ -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.n_type.bits.is_stab != 0 or nlist.n_type.type != .sect) continue; if (nlist.n_type.bits.is_stab != 0 or nlist.n_type.bits.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);
@ -488,9 +488,9 @@ fn initCstringLiterals(self: *Object, allocator: Allocator, file: File.Handle, m
self.symtab.set(nlist_index, .{ self.symtab.set(nlist_index, .{
.nlist = .{ .nlist = .{
.n_strx = name_str.pos, .n_strx = name_str.pos,
.n_type = macho.N_SECT, .n_type = .{ .bits = .{ .ext = false, .type = .sect, .pext = false, .is_stab = 0 } },
.n_sect = @intCast(atom.n_sect + 1), .n_sect = @intCast(atom.n_sect + 1),
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = atom.getInputAddress(macho_file), .n_value = atom.getInputAddress(macho_file),
}, },
.size = atom.size, .size = atom.size,
@ -555,9 +555,9 @@ fn initFixedSizeLiterals(self: *Object, allocator: Allocator, macho_file: *MachO
self.symtab.set(nlist_index, .{ self.symtab.set(nlist_index, .{
.nlist = .{ .nlist = .{
.n_strx = name_str.pos, .n_strx = name_str.pos,
.n_type = macho.N_SECT, .n_type = .{ .bits = .{ .ext = false, .type = .sect, .pext = false, .is_stab = 0 } },
.n_sect = @intCast(atom.n_sect + 1), .n_sect = @intCast(atom.n_sect + 1),
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = atom.getInputAddress(macho_file), .n_value = atom.getInputAddress(macho_file),
}, },
.size = atom.size, .size = atom.size,
@ -613,9 +613,9 @@ fn initPointerLiterals(self: *Object, allocator: Allocator, macho_file: *MachO)
self.symtab.set(nlist_index, .{ self.symtab.set(nlist_index, .{
.nlist = .{ .nlist = .{
.n_strx = name_str.pos, .n_strx = name_str.pos,
.n_type = macho.N_SECT, .n_type = .{ .bits = .{ .ext = false, .type = .sect, .pext = false, .is_stab = 0 } },
.n_sect = @intCast(atom.n_sect + 1), .n_sect = @intCast(atom.n_sect + 1),
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = atom.getInputAddress(macho_file), .n_value = atom.getInputAddress(macho_file),
}, },
.size = atom.size, .size = atom.size,
@ -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.n_type.bits.is_stab != 0 and nlist.n_type.type == .sect) { if (nlist.n_type.bits.is_stab == 0 and nlist.n_type.bits.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.n_type.type != .abs); assert(nlist.n_type.bits.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.n_desc.weak_def_or_ref_to_weak; symbol.flags.weak = nlist.n_desc.weak_def_or_ref_to_weak;
symbol.flags.abs = nlist.n_type.type == .abs; symbol.flags.abs = nlist.n_type.bits.type == .abs;
symbol.flags.tentative = nlist.tentative(); symbol.flags.tentative = nlist.tentative();
symbol.flags.no_dead_strip = symbol.flags.no_dead_strip or nlist.n_desc.discarded_or_no_dead_strip; 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.referenced_dynamically;
symbol.flags.interposable = false; symbol.flags.interposable = false;
// TODO // TODO
// 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(); // symbol.flags.interposable = nlist.ext() and (nlist.n_type.bits.type == .sect or nlist.n_type.bits.type == .abs) and macho_file.base.isDynLib() and macho_file.options.namespace == .flat and !nlist.pext();
if (nlist.n_type.type == .sect and if (nlist.n_type.bits.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.n_type.bits.ext) {
if (nlist.n_type.type == .undf) { if (nlist.n_type.bits.type == .undf) {
symbol.flags.weak_ref = nlist.n_desc.weak_ref; symbol.flags.weak_ref = nlist.n_desc.weak_ref;
} else if (nlist.pext() or (nlist.n_desc.weak_def_or_ref_to_weak and nlist.n_desc.weak_ref) or self.hidden) { } else if (nlist.n_type.bits.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;
@ -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.n_type.type == .sect and (sym.ext() or sym.pext())) { if (sym.n_type.bits.type == .sect and (sym.n_type.bits.ext or sym.n_type.bits.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);
} }
} }
@ -927,41 +927,43 @@ fn initSymbolStabs(self: *Object, allocator: Allocator, nlists: anytype, macho_f
var i: u32 = start; var i: u32 = start;
while (i < end) : (i += 1) { while (i < end) : (i += 1) {
const open = syms[i]; const open = syms[i];
if (open.n_type != macho.N_SO) { if (open.n_type.stab != .so) {
try macho_file.reportParseError2(self.index, "unexpected symbol stab type 0x{x} as the first entry", .{ try macho_file.reportParseError2(self.index, "unexpected symbol stab type 0x{x} as the first entry", .{
open.n_type, @intFromEnum(open.n_type.stab),
}); });
return error.MalformedObject; return error.MalformedObject;
} }
while (i < end and syms[i].n_type == macho.N_SO and syms[i].n_sect != 0) : (i += 1) {} while (i < end and syms[i].n_type.stab == .so and syms[i].n_sect != 0) : (i += 1) {}
var sf: StabFile = .{ .comp_dir = i }; var sf: StabFile = .{ .comp_dir = i };
// TODO validate // TODO validate
i += 3; i += 3;
while (i < end and syms[i].n_type != macho.N_SO) : (i += 1) { while (i < end and syms[i].n_type.stab != .so) : (i += 1) {
const nlist = syms[i]; const nlist = syms[i];
var stab: StabFile.Stab = .{}; var stab: StabFile.Stab = .{};
switch (nlist.n_type) { switch (nlist.n_type.stab) {
macho.N_BNSYM => { .bnsym => {
stab.is_func = true; stab.is_func = true;
stab.index = sym_lookup.find(nlist.n_value); stab.index = sym_lookup.find(nlist.n_value);
// TODO validate // TODO validate
i += 3; i += 3;
}, },
macho.N_GSYM => { .gsym => {
stab.is_func = false; stab.is_func = false;
stab.index = sym_lookup.find(addr_lookup.get(self.getNStrx(nlist.n_strx)).?); stab.index = sym_lookup.find(addr_lookup.get(self.getNStrx(nlist.n_strx)).?);
}, },
macho.N_STSYM => { .stsym => {
stab.is_func = false; stab.is_func = false;
stab.index = sym_lookup.find(nlist.n_value); stab.index = sym_lookup.find(nlist.n_value);
}, },
_ => {
try macho_file.reportParseError2(self.index, "unhandled symbol stab type 0x{x}", .{@intFromEnum(nlist.n_type.stab)});
return error.MalformedObject;
},
else => { else => {
try macho_file.reportParseError2(self.index, "unhandled symbol stab type 0x{x}", .{ try macho_file.reportParseError2(self.index, "unhandled symbol stab type '{t}'", .{nlist.n_type.stab});
nlist.n_type,
});
return error.MalformedObject; return error.MalformedObject;
}, },
} }
@ -1132,7 +1134,7 @@ fn initUnwindRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fil
fn find(fs: @This(), addr: u64) ?Symbol.Index { fn find(fs: @This(), addr: u64) ?Symbol.Index {
for (0..fs.ctx.symbols.items.len) |i| { for (0..fs.ctx.symbols.items.len) |i| {
const nlist = fs.ctx.symtab.items(.nlist)[i]; const nlist = fs.ctx.symtab.items(.nlist)[i];
if (nlist.ext() and nlist.n_value == addr) return @intCast(i); if (nlist.n_type.bits.ext and nlist.n_value == addr) return @intCast(i);
} }
return null; return null;
} }
@ -1242,7 +1244,7 @@ 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.n_type.bits.is_stab != 0) continue; if (nlist.n_type.bits.is_stab != 0) continue;
if (nlist.n_type.type != .sect) continue; if (nlist.n_type.bits.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);
@ -1458,8 +1460,8 @@ pub fn resolveSymbols(self: *Object, macho_file: *MachO) !void {
const gpa = macho_file.base.comp.gpa; const gpa = macho_file.base.comp.gpa;
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.n_type.bits.ext) continue;
if (nlist.n_type.type == .sect) { if (nlist.n_type.bits.type == .sect) {
const atom = self.getAtom(atom_index).?; const atom = self.getAtom(atom_index).?;
if (!atom.isAlive()) continue; if (!atom.isAlive()) continue;
} }
@ -1473,7 +1475,7 @@ pub fn resolveSymbols(self: *Object, macho_file: *MachO) !void {
} }
global.* = gop.index; global.* = gop.index;
if (nlist.n_type.type == .undf and !nlist.tentative()) continue; if (nlist.n_type.bits.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;
@ -1495,12 +1497,12 @@ pub fn markLive(self: *Object, macho_file: *MachO) void {
for (0..self.symbols.items.len) |i| { for (0..self.symbols.items.len) |i| {
const nlist = self.symtab.items(.nlist)[i]; const nlist = self.symtab.items(.nlist)[i];
if (!nlist.ext()) continue; if (!nlist.n_type.bits.ext) continue;
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.n_type.type == .undf or (nlist.tentative() and !sym.flags.tentative); const should_keep = nlist.n_type.bits.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);
@ -1565,7 +1567,7 @@ pub fn convertTentativeDefinitions(self: *Object, macho_file: *MachO) !void {
const name = try std.fmt.allocPrintSentinel(gpa, "__DATA$__common${s}", .{sym.getName(macho_file)}, 0); const name = try std.fmt.allocPrintSentinel(gpa, "__DATA$__common${s}", .{sym.getName(macho_file)}, 0);
defer gpa.free(name); defer gpa.free(name);
const alignment = (nlist.n_desc >> 8) & 0x0f; const alignment = (@as(u16, @bitCast(nlist.n_desc)) >> 8) & 0x0f;
const n_sect = try self.addSection(gpa, "__DATA", "__common"); const n_sect = try self.addSection(gpa, "__DATA", "__common");
const atom_index = try self.addAtom(gpa, .{ const atom_index = try self.addAtom(gpa, .{
.name = try self.addString(gpa, name), .name = try self.addString(gpa, name),
@ -1589,9 +1591,9 @@ pub fn convertTentativeDefinitions(self: *Object, macho_file: *MachO) !void {
sym.visibility = .global; sym.visibility = .global;
nlist.n_value = 0; nlist.n_value = 0;
nlist.n_type = macho.N_EXT | macho.N_SECT; nlist.n_type = .{ .bits = .{ .ext = true, .type = .sect, .pext = false, .is_stab = 0 } };
nlist.n_sect = 0; nlist.n_sect = 0;
nlist.n_desc = 0; nlist.n_desc = @bitCast(@as(u16, 0));
nlist_atom.* = atom_index; nlist_atom.* = atom_index;
} }
} }
@ -1685,7 +1687,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.n_type.type == .undf and !nlist.tentative())) continue; if (!nlist.n_type.bits.ext or (nlist.n_type.bits.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 });
} }
@ -2028,30 +2030,30 @@ pub fn writeStabs(self: Object, stroff: u32, macho_file: *MachO, ctx: anytype) v
) void { ) void {
context.symtab.items[index] = .{ context.symtab.items[index] = .{
.n_strx = 0, .n_strx = 0,
.n_type = macho.N_BNSYM, .n_type = .{ .stab = .bnsym },
.n_sect = n_sect, .n_sect = n_sect,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = n_value, .n_value = n_value,
}; };
context.symtab.items[index + 1] = .{ context.symtab.items[index + 1] = .{
.n_strx = n_strx, .n_strx = n_strx,
.n_type = macho.N_FUN, .n_type = .{ .stab = .fun },
.n_sect = n_sect, .n_sect = n_sect,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = n_value, .n_value = n_value,
}; };
context.symtab.items[index + 2] = .{ context.symtab.items[index + 2] = .{
.n_strx = 0, .n_strx = 0,
.n_type = macho.N_FUN, .n_type = .{ .stab = .fun },
.n_sect = 0, .n_sect = 0,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = size, .n_value = size,
}; };
context.symtab.items[index + 3] = .{ context.symtab.items[index + 3] = .{
.n_strx = 0, .n_strx = 0,
.n_type = macho.N_ENSYM, .n_type = .{ .stab = .ensym },
.n_sect = n_sect, .n_sect = n_sect,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = size, .n_value = size,
}; };
} }
@ -2068,9 +2070,9 @@ pub fn writeStabs(self: Object, stroff: u32, macho_file: *MachO, ctx: anytype) v
// N_SO comp_dir // N_SO comp_dir
ctx.symtab.items[index] = .{ ctx.symtab.items[index] = .{
.n_strx = n_strx, .n_strx = n_strx,
.n_type = macho.N_SO, .n_type = .{ .stab = .so },
.n_sect = 0, .n_sect = 0,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = 0, .n_value = 0,
}; };
index += 1; index += 1;
@ -2081,9 +2083,9 @@ pub fn writeStabs(self: Object, stroff: u32, macho_file: *MachO, ctx: anytype) v
// N_SO tu_name // N_SO tu_name
macho_file.symtab.items[index] = .{ macho_file.symtab.items[index] = .{
.n_strx = n_strx, .n_strx = n_strx,
.n_type = macho.N_SO, .n_type = .{ .stab = .so },
.n_sect = 0, .n_sect = 0,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = 0, .n_value = 0,
}; };
index += 1; index += 1;
@ -2094,9 +2096,9 @@ pub fn writeStabs(self: Object, stroff: u32, macho_file: *MachO, ctx: anytype) v
// N_OSO path // N_OSO path
ctx.symtab.items[index] = .{ ctx.symtab.items[index] = .{
.n_strx = n_strx, .n_strx = n_strx,
.n_type = macho.N_OSO, .n_type = .{ .stab = .oso },
.n_sect = 0, .n_sect = 0,
.n_desc = 1, .n_desc = @bitCast(@as(u16, 1)),
.n_value = self.mtime, .n_value = self.mtime,
}; };
index += 1; index += 1;
@ -2159,18 +2161,18 @@ pub fn writeStabs(self: Object, stroff: u32, macho_file: *MachO, ctx: anytype) v
} else if (sym.visibility == .global) { } else if (sym.visibility == .global) {
ctx.symtab.items[index] = .{ ctx.symtab.items[index] = .{
.n_strx = sym_n_strx, .n_strx = sym_n_strx,
.n_type = macho.N_GSYM, .n_type = .{ .stab = .gsym },
.n_sect = sym_n_sect, .n_sect = sym_n_sect,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = 0, .n_value = 0,
}; };
index += 1; index += 1;
} else { } else {
ctx.symtab.items[index] = .{ ctx.symtab.items[index] = .{
.n_strx = sym_n_strx, .n_strx = sym_n_strx,
.n_type = macho.N_STSYM, .n_type = .{ .stab = .stsym },
.n_sect = sym_n_sect, .n_sect = sym_n_sect,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = sym_n_value, .n_value = sym_n_value,
}; };
index += 1; index += 1;
@ -2181,9 +2183,9 @@ pub fn writeStabs(self: Object, stroff: u32, macho_file: *MachO, ctx: anytype) v
// N_SO // N_SO
ctx.symtab.items[index] = .{ ctx.symtab.items[index] = .{
.n_strx = 0, .n_strx = 0,
.n_type = macho.N_SO, .n_type = .{ .stab = .so },
.n_sect = 0, .n_sect = 0,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = 0, .n_value = 0,
}; };
} else { } else {
@ -2198,9 +2200,9 @@ pub fn writeStabs(self: Object, stroff: u32, macho_file: *MachO, ctx: anytype) v
// N_SO comp_dir // N_SO comp_dir
ctx.symtab.items[index] = .{ ctx.symtab.items[index] = .{
.n_strx = n_strx, .n_strx = n_strx,
.n_type = macho.N_SO, .n_type = .{ .stab = .so },
.n_sect = 0, .n_sect = 0,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = 0, .n_value = 0,
}; };
index += 1; index += 1;
@ -2211,9 +2213,9 @@ pub fn writeStabs(self: Object, stroff: u32, macho_file: *MachO, ctx: anytype) v
// N_SO tu_name // N_SO tu_name
ctx.symtab.items[index] = .{ ctx.symtab.items[index] = .{
.n_strx = n_strx, .n_strx = n_strx,
.n_type = macho.N_SO, .n_type = .{ .stab = .so },
.n_sect = 0, .n_sect = 0,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = 0, .n_value = 0,
}; };
index += 1; index += 1;
@ -2224,9 +2226,9 @@ pub fn writeStabs(self: Object, stroff: u32, macho_file: *MachO, ctx: anytype) v
// N_OSO path // N_OSO path
ctx.symtab.items[index] = .{ ctx.symtab.items[index] = .{
.n_strx = n_strx, .n_strx = n_strx,
.n_type = macho.N_OSO, .n_type = .{ .stab = .so },
.n_sect = 0, .n_sect = 0,
.n_desc = 1, .n_desc = @bitCast(@as(u16, 1)),
.n_value = sf.getOsoModTime(self), .n_value = sf.getOsoModTime(self),
}; };
index += 1; index += 1;
@ -2254,18 +2256,18 @@ pub fn writeStabs(self: Object, stroff: u32, macho_file: *MachO, ctx: anytype) v
} else if (sym.visibility == .global) { } else if (sym.visibility == .global) {
ctx.symtab.items[index] = .{ ctx.symtab.items[index] = .{
.n_strx = sym_n_strx, .n_strx = sym_n_strx,
.n_type = macho.N_GSYM, .n_type = .{ .stab = .gsym },
.n_sect = sym_n_sect, .n_sect = sym_n_sect,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = 0, .n_value = 0,
}; };
index += 1; index += 1;
} else { } else {
ctx.symtab.items[index] = .{ ctx.symtab.items[index] = .{
.n_strx = sym_n_strx, .n_strx = sym_n_strx,
.n_type = macho.N_STSYM, .n_type = .{ .stab = .stsym },
.n_sect = sym_n_sect, .n_sect = sym_n_sect,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = sym_n_value, .n_value = sym_n_value,
}; };
index += 1; index += 1;
@ -2276,9 +2278,9 @@ pub fn writeStabs(self: Object, stroff: u32, macho_file: *MachO, ctx: anytype) v
// N_SO // N_SO
ctx.symtab.items[index] = .{ ctx.symtab.items[index] = .{
.n_strx = 0, .n_strx = 0,
.n_type = macho.N_SO, .n_type = .{ .stab = .so },
.n_sect = 0, .n_sect = 0,
.n_desc = 0, .n_desc = @bitCast(@as(u16, 0)),
.n_value = 0, .n_value = 0,
}; };
index += 1; index += 1;

View File

@ -36,7 +36,7 @@ pub fn isLocal(symbol: Symbol) bool {
pub fn isSymbolStab(symbol: Symbol, macho_file: *MachO) bool { pub fn isSymbolStab(symbol: Symbol, macho_file: *MachO) bool {
const file = symbol.getFile(macho_file) orelse return false; const file = symbol.getFile(macho_file) orelse return false;
return switch (file) { return switch (file) {
.object => symbol.getNlist(macho_file).stab(), .object => symbol.getNlist(macho_file).n_type.bits.is_stab != 0,
else => false, else => false,
}; };
} }
@ -233,35 +233,49 @@ pub inline fn setExtra(symbol: Symbol, extra: Extra, macho_file: *MachO) void {
pub fn setOutputSym(symbol: Symbol, macho_file: *MachO, out: *macho.nlist_64) void { pub fn setOutputSym(symbol: Symbol, macho_file: *MachO, out: *macho.nlist_64) void {
if (symbol.isLocal()) { if (symbol.isLocal()) {
out.n_type = if (symbol.flags.abs) macho.N_ABS else macho.N_SECT; out.n_type = .{ .bits = .{
.ext = false,
.type = if (symbol.flags.abs) .abs else .sect,
.pext = false,
.is_stab = 0,
} };
out.n_sect = if (symbol.flags.abs) 0 else @intCast(symbol.getOutputSectionIndex(macho_file) + 1); out.n_sect = if (symbol.flags.abs) 0 else @intCast(symbol.getOutputSectionIndex(macho_file) + 1);
out.n_desc = 0; out.n_desc = @bitCast(@as(u16, 0));
out.n_value = symbol.getAddress(.{ .stubs = false }, macho_file); out.n_value = symbol.getAddress(.{ .stubs = false }, macho_file);
switch (symbol.visibility) { switch (symbol.visibility) {
.hidden => out.n_type |= macho.N_PEXT, .hidden => out.n_type.bits.pext = true,
else => {}, else => {},
} }
} else if (symbol.flags.@"export") { } else if (symbol.flags.@"export") {
assert(symbol.visibility == .global); assert(symbol.visibility == .global);
out.n_type = macho.N_EXT; out.n_type = .{ .bits = .{
out.n_type |= if (symbol.flags.abs) macho.N_ABS else macho.N_SECT; .ext = true,
.type = if (symbol.flags.abs) .abs else .sect,
.pext = false,
.is_stab = 0,
} };
out.n_sect = if (symbol.flags.abs) 0 else @intCast(symbol.getOutputSectionIndex(macho_file) + 1); out.n_sect = if (symbol.flags.abs) 0 else @intCast(symbol.getOutputSectionIndex(macho_file) + 1);
out.n_value = symbol.getAddress(.{ .stubs = false }, macho_file); out.n_value = symbol.getAddress(.{ .stubs = false }, macho_file);
out.n_desc = 0; out.n_desc = @bitCast(@as(u16, 0));
if (symbol.flags.weak) { if (symbol.flags.weak) {
out.n_desc |= macho.N_WEAK_DEF; out.n_desc.weak_def_or_ref_to_weak = true;
} }
if (symbol.flags.dyn_ref) { if (symbol.flags.dyn_ref) {
out.n_desc |= macho.REFERENCED_DYNAMICALLY; out.n_desc.referenced_dynamically = true;
} }
} else { } else {
assert(symbol.visibility == .global); assert(symbol.visibility == .global);
out.n_type = macho.N_EXT; out.n_type = .{ .bits = .{
.ext = true,
.type = .undf,
.pext = false,
.is_stab = 0,
} };
out.n_sect = 0; out.n_sect = 0;
out.n_value = 0; out.n_value = 0;
out.n_desc = 0; out.n_desc = @bitCast(@as(u16, 0));
// TODO: // TODO:
// const ord: u16 = if (macho_file.options.namespace == .flat) // const ord: u16 = if (macho_file.options.namespace == .flat)
@ -274,14 +288,14 @@ pub fn setOutputSym(symbol: Symbol, macho_file: *MachO, out: *macho.nlist_64) vo
ord ord
else else
macho.BIND_SPECIAL_DYLIB_SELF; macho.BIND_SPECIAL_DYLIB_SELF;
out.n_desc = macho.N_SYMBOL_RESOLVER * ord; out.n_desc = @bitCast(macho.N_SYMBOL_RESOLVER * ord);
if (symbol.flags.weak) { if (symbol.flags.weak) {
out.n_desc |= macho.N_WEAK_DEF; out.n_desc.weak_def_or_ref_to_weak = true;
} }
if (symbol.weakRef(macho_file)) { if (symbol.weakRef(macho_file)) {
out.n_desc |= macho.N_WEAK_REF; out.n_desc.weak_ref = true;
} }
} }
} }

View File

@ -56,10 +56,10 @@ pub fn writeSymtab(thunk: Thunk, macho_file: *MachO, ctx: anytype) void {
n_strx += @intCast("__thunk".len); n_strx += @intCast("__thunk".len);
ctx.strtab.items[n_strx] = 0; ctx.strtab.items[n_strx] = 0;
n_strx += 1; n_strx += 1;
out_sym.n_type = macho.N_SECT; out_sym.n_type = .{ .bits = .{ .ext = false, .type = .sect, .pext = false, .is_stab = 0 } };
out_sym.n_sect = @intCast(thunk.out_n_sect + 1); out_sym.n_sect = @intCast(thunk.out_n_sect + 1);
out_sym.n_value = @intCast(thunk.getTargetAddress(ref, macho_file)); out_sym.n_value = @intCast(thunk.getTargetAddress(ref, macho_file));
out_sym.n_desc = 0; out_sym.n_desc = @bitCast(@as(u16, 0));
} }
} }

View File

@ -123,9 +123,9 @@ fn newSymbol(self: *ZigObject, allocator: Allocator, name: MachO.String, args: s
self.symtab.set(nlist_idx, .{ self.symtab.set(nlist_idx, .{
.nlist = .{ .nlist = .{
.n_strx = name.pos, .n_strx = name.pos,
.n_type = args.type, .n_type = @bitCast(args.type),
.n_sect = 0, .n_sect = 0,
.n_desc = args.desc, .n_desc = @bitCast(args.desc),
.n_value = 0, .n_value = 0,
}, },
.size = 0, .size = 0,
@ -206,8 +206,8 @@ pub fn resolveSymbols(self: *ZigObject, macho_file: *MachO) !void {
const gpa = macho_file.base.comp.gpa; const gpa = macho_file.base.comp.gpa;
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.n_type.bits.ext) continue;
if (nlist.sect()) { if (nlist.n_type.bits.type == .sect) {
const atom = self.getAtom(atom_index).?; const atom = self.getAtom(atom_index).?;
if (!atom.isAlive()) continue; if (!atom.isAlive()) continue;
} }
@ -221,7 +221,7 @@ pub fn resolveSymbols(self: *ZigObject, macho_file: *MachO) !void {
} }
global.* = gop.index; global.* = gop.index;
if (nlist.undf() and !nlist.tentative()) continue; if (nlist.n_type.bits.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;
@ -229,7 +229,7 @@ pub fn resolveSymbols(self: *ZigObject, macho_file: *MachO) !void {
if (self.asFile().getSymbolRank(.{ if (self.asFile().getSymbolRank(.{
.archive = false, .archive = false,
.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 };
@ -243,12 +243,12 @@ pub fn markLive(self: *ZigObject, macho_file: *MachO) void {
for (0..self.symbols.items.len) |i| { for (0..self.symbols.items.len) |i| {
const nlist = self.symtab.items(.nlist)[i]; const nlist = self.symtab.items(.nlist)[i];
if (!nlist.ext()) continue; if (!nlist.n_type.bits.ext) continue;
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.bits.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);
@ -331,8 +331,8 @@ pub fn claimUnresolved(self: *ZigObject, macho_file: *MachO) void {
for (self.symbols.items, 0..) |*sym, i| { for (self.symbols.items, 0..) |*sym, i| {
const nlist = self.symtab.items(.nlist)[i]; const nlist = self.symtab.items(.nlist)[i];
if (!nlist.ext()) continue; if (!nlist.n_type.bits.ext) continue;
if (!nlist.undf()) continue; if (nlist.n_type.bits.type != .undf) continue;
if (self.getSymbolRef(@intCast(i), macho_file).getFile(macho_file) != null) continue; if (self.getSymbolRef(@intCast(i), macho_file).getFile(macho_file) != null) continue;
@ -974,7 +974,7 @@ fn updateNavCode(
atom.setAlive(true); atom.setAlive(true);
atom.name = sym.name; atom.name = sym.name;
nlist.n_strx = sym.name.pos; nlist.n_strx = sym.name.pos;
nlist.n_type = macho.N_SECT; nlist.n_type = .{ .bits = .{ .ext = false, .type = .sect, .pext = false, .is_stab = 0 } };
nlist.n_sect = sect_index + 1; nlist.n_sect = sect_index + 1;
self.symtab.items(.size)[sym.nlist_idx] = code.len; self.symtab.items(.size)[sym.nlist_idx] = code.len;
@ -1115,7 +1115,7 @@ fn createTlvDescriptor(
atom.name = sym.name; atom.name = sym.name;
nlist.n_strx = sym.name.pos; nlist.n_strx = sym.name.pos;
nlist.n_sect = sect_index + 1; nlist.n_sect = sect_index + 1;
nlist.n_type = macho.N_SECT; nlist.n_type = .{ .bits = .{ .ext = false, .type = .sect, .pext = false, .is_stab = 0 } };
nlist.n_value = 0; nlist.n_value = 0;
self.symtab.items(.size)[sym.nlist_idx] = size; self.symtab.items(.size)[sym.nlist_idx] = size;
@ -1322,7 +1322,7 @@ pub fn updateExports(
const global_sym = &self.symbols.items[global_nlist_index]; const global_sym = &self.symbols.items[global_nlist_index];
global_nlist.n_value = nlist.n_value; global_nlist.n_value = nlist.n_value;
global_nlist.n_sect = nlist.n_sect; global_nlist.n_sect = nlist.n_sect;
global_nlist.n_type = macho.N_EXT | macho.N_SECT; global_nlist.n_type = .{ .bits = .{ .ext = true, .type = .sect, .pext = false, .is_stab = 0 } };
self.symtab.items(.size)[global_nlist_index] = self.symtab.items(.size)[nlist_idx]; self.symtab.items(.size)[global_nlist_index] = self.symtab.items(.size)[nlist_idx];
self.symtab.items(.atom)[global_nlist_index] = atom_index; self.symtab.items(.atom)[global_nlist_index] = atom_index;
global_sym.atom_ref = .{ .index = atom_index, .file = self.index }; global_sym.atom_ref = .{ .index = atom_index, .file = self.index };
@ -1330,7 +1330,7 @@ pub fn updateExports(
switch (exp.opts.linkage) { switch (exp.opts.linkage) {
.internal => { .internal => {
// Symbol should be hidden, or in MachO lingo, private extern. // Symbol should be hidden, or in MachO lingo, private extern.
global_nlist.n_type |= macho.N_PEXT; global_nlist.n_type.bits.pext = true;
global_sym.visibility = .hidden; global_sym.visibility = .hidden;
}, },
.strong => { .strong => {
@ -1339,7 +1339,7 @@ pub fn updateExports(
.weak => { .weak => {
// Weak linkage is specified as part of n_desc field. // Weak linkage is specified as part of n_desc field.
// Symbol's n_type is like for a symbol with strong linkage. // Symbol's n_type is like for a symbol with strong linkage.
global_nlist.n_desc |= macho.N_WEAK_DEF; global_nlist.n_desc.weak_def_or_ref_to_weak = true;
global_sym.visibility = .global; global_sym.visibility = .global;
global_sym.flags.weak = true; global_sym.flags.weak = true;
}, },
@ -1394,7 +1394,7 @@ fn updateLazySymbol(
const nlist = &self.symtab.items(.nlist)[sym.nlist_idx]; const nlist = &self.symtab.items(.nlist)[sym.nlist_idx];
nlist.n_strx = name_str.pos; nlist.n_strx = name_str.pos;
nlist.n_type = macho.N_SECT; nlist.n_type = .{ .bits = .{ .ext = false, .type = .sect, .pext = false, .is_stab = 0 } };
nlist.n_sect = output_section_index + 1; nlist.n_sect = output_section_index + 1;
self.symtab.items(.size)[sym.nlist_idx] = code.len; self.symtab.items(.size)[sym.nlist_idx] = code.len;

View File

@ -26,24 +26,24 @@ pub const Cie = struct {
for (aug[1..]) |ch| switch (ch) { for (aug[1..]) |ch| switch (ch) {
'R' => { 'R' => {
const enc = try reader.takeByte(); const enc: DW.EH.PE = @bitCast(try reader.takeByte());
if (enc != DW_EH_PE.pcrel | DW_EH_PE.absptr) { if (enc != @as(DW.EH.PE, .{ .type = .absptr, .rel = .pcrel })) {
@panic("unexpected pointer encoding"); // TODO error @panic("unexpected pointer encoding"); // TODO error
} }
}, },
'P' => { 'P' => {
const enc = try reader.takeByte(); const enc: DW.EH.PE = @bitCast(try reader.takeByte());
if (enc != DW_EH_PE.pcrel | DW_EH_PE.indirect | DW_EH_PE.sdata4) { if (enc != @as(DW.EH.PE, .{ .type = .sdata4, .rel = .pcrel, .indirect = true })) {
@panic("unexpected personality pointer encoding"); // TODO error @panic("unexpected personality pointer encoding"); // TODO error
} }
_ = try reader.takeInt(u32, .little); // personality pointer _ = try reader.takeInt(u32, .little); // personality pointer
}, },
'L' => { 'L' => {
const enc = try reader.takeByte(); const enc: DW.EH.PE = @bitCast(try reader.takeByte());
switch (enc & DW_EH_PE.type_mask) { switch (enc.type) {
DW_EH_PE.sdata4 => cie.lsda_size = .p32, .sdata4 => cie.lsda_size = .p32,
DW_EH_PE.absptr => cie.lsda_size = .p64, .absptr => cie.lsda_size = .p64,
else => unreachable, // TODO error else => @panic("unexpected lsda encoding"), // TODO error
} }
}, },
else => @panic("unexpected augmentation string"), // TODO error else => @panic("unexpected augmentation string"), // TODO error
@ -505,7 +505,7 @@ const Writer = std.Io.Writer;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const Atom = @import("Atom.zig"); const Atom = @import("Atom.zig");
const DW_EH_PE = std.dwarf.EH.PE; const DW = std.dwarf;
const File = @import("file.zig").File; const File = @import("file.zig").File;
const MachO = @import("../MachO.zig"); const MachO = @import("../MachO.zig");
const Object = @import("Object.zig"); const Object = @import("Object.zig");

View File

@ -192,21 +192,21 @@ pub const File = union(enum) {
assert(file == .object or file == .zig_object); assert(file == .object or file == .zig_object);
for (file.getSymbols(), file.getNlists(), 0..) |*sym, nlist, i| { for (file.getSymbols(), file.getNlists(), 0..) |*sym, nlist, i| {
if (!nlist.ext()) continue; if (!nlist.n_type.bits.ext) continue;
if (!nlist.undf()) continue; if (nlist.n_type.bits.type != .undf) continue;
if (file.getSymbolRef(@intCast(i), macho_file).getFile(macho_file) != null) continue; if (file.getSymbolRef(@intCast(i), macho_file).getFile(macho_file) != null) continue;
const is_import = switch (macho_file.undefined_treatment) { const is_import = switch (macho_file.undefined_treatment) {
.@"error" => false, .@"error" => false,
.warn, .suppress => nlist.weakRef(), .warn, .suppress => nlist.n_desc.weak_ref,
.dynamic_lookup => true, .dynamic_lookup => true,
}; };
if (is_import) { if (is_import) {
sym.value = 0; sym.value = 0;
sym.atom_ref = .{ .index = 0, .file = 0 }; sym.atom_ref = .{ .index = 0, .file = 0 };
sym.flags.weak = false; sym.flags.weak = false;
sym.flags.weak_ref = nlist.weakRef(); sym.flags.weak_ref = nlist.n_desc.weak_ref;
sym.flags.import = is_import; sym.flags.import = is_import;
sym.visibility = .global; sym.visibility = .global;
@ -223,13 +223,13 @@ pub const File = union(enum) {
assert(file == .object or file == .zig_object); assert(file == .object or file == .zig_object);
for (file.getSymbols(), file.getNlists(), 0..) |*sym, nlist, i| { for (file.getSymbols(), file.getNlists(), 0..) |*sym, nlist, i| {
if (!nlist.ext()) continue; if (!nlist.n_type.bits.ext) continue;
if (!nlist.undf()) continue; if (nlist.n_type.bits.type != .undf) continue;
if (file.getSymbolRef(@intCast(i), macho_file).getFile(macho_file) != null) continue; if (file.getSymbolRef(@intCast(i), macho_file).getFile(macho_file) != null) continue;
sym.value = 0; sym.value = 0;
sym.atom_ref = .{ .index = 0, .file = 0 }; sym.atom_ref = .{ .index = 0, .file = 0 };
sym.flags.weak_ref = nlist.weakRef(); sym.flags.weak_ref = nlist.n_desc.weak_ref;
sym.flags.import = true; sym.flags.import = true;
sym.visibility = .global; sym.visibility = .global;
@ -247,7 +247,7 @@ pub const File = union(enum) {
for (file.getSymbols(), file.getNlists(), 0..) |sym, nlist, i| { for (file.getSymbols(), file.getNlists(), 0..) |sym, nlist, i| {
if (sym.visibility != .global) continue; if (sym.visibility != .global) continue;
if (sym.flags.weak) continue; if (sym.flags.weak) continue;
if (nlist.undf()) continue; if (nlist.n_type.bits.type == .undf) continue;
const ref = file.getSymbolRef(@intCast(i), macho_file); const ref = file.getSymbolRef(@intCast(i), macho_file);
const ref_file = ref.getFile(macho_file) orelse continue; const ref_file = ref.getFile(macho_file) orelse continue;
if (ref_file.getIndex() == file.getIndex()) continue; if (ref_file.getIndex() == file.getIndex()) continue;