authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-29 23:47:48+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-03-01 10:18:10+01:00
log9e402704e2fc01369dfbf2c9a20ac1ee7d66ca7c
tree3d7d9e687088cec8ca5495709d8daa57b859a81a
parent147beec7da5f3eb3a858037b806ad8b1b66bfffc

macho: correctly find N_GSYM symbols when parsing symbol stabs

In `ld -r` mode, the linker will emit `N_GSYM` for any defined external symbols as well as private externals. In the former case, the thing is easy since `N_EXT` bit will be set in the nlist's type. In the latter however we will encounter a local symbol with `N_PEXT` bit set (non-extern, but was private external) which we also need to include when resolving symbol stabs. The major change in the logic for parsing symbol stabs per input object file is that we no longer try to force-resolve a `N_GSYM` as a global symbol. This was a mistake since every symbol stab always describes a symbol defined within the parsed input object file. We then work out if we should forward `N_GSYM` in the output symtab after we have resolved all symbols, but never before - intel we lack when initially parsing symbol stabs. Therefore, we simply record which symbol has a debug symbol stab, and work out its precise type when emitting output symtab after symbol resolution has been done.

2 files changed, 38 insertions(+), 37 deletions(-)

src/link/MachO.zig-2
......@@ -4320,8 +4320,6 @@ const is_hot_update_compatible = switch (builtin.target.os.tag) {
43204320const default_entry_symbol_name = "_main";
43214321
43224322pub const base_tag: link.File.Tag = link.File.Tag.macho;
4323pub const N_DEAD: u16 = @as(u16, @bitCast(@as(i16, -1)));
4324pub const N_BOUNDARY: u16 = @as(u16, @bitCast(@as(i16, -2)));
43254323
43264324const Section = struct {
43274325 header: macho.section_64,
src/link/MachO/Object.zig+38-35
......@@ -590,6 +590,17 @@ fn initSymbolStabs(self: *Object, nlists: anytype, macho_file: *MachO) !void {
590590 const syms = self.symtab.items(.nlist);
591591 const sym_lookup = SymbolLookup{ .ctx = self, .entries = nlists };
592592
593 // We need to cache nlists by name so that we can properly resolve local N_GSYM stabs.
594 // What happens is `ld -r` will emit an N_GSYM stab for a symbol that may be either an
595 // external or private external.
596 var addr_lookup = std.StringHashMap(u64).init(gpa);
597 defer addr_lookup.deinit();
598 for (syms) |sym| {
599 if (sym.sect() and (sym.ext() or sym.pext())) {
600 try addr_lookup.putNoClobber(self.getString(sym.n_strx), sym.n_value);
601 }
602 }
603
593604 var i: u32 = start;
594605 while (i < end) : (i += 1) {
595606 const open = syms[i];
......@@ -611,17 +622,17 @@ fn initSymbolStabs(self: *Object, nlists: anytype, macho_file: *MachO) !void {
611622 var stab: StabFile.Stab = .{};
612623 switch (nlist.n_type) {
613624 macho.N_BNSYM => {
614 stab.tag = .func;
625 stab.is_func = true;
615626 stab.symbol = sym_lookup.find(nlist.n_value);
616627 // TODO validate
617628 i += 3;
618629 },
619630 macho.N_GSYM => {
620 stab.tag = .global;
621 stab.symbol = macho_file.getGlobalByName(self.getString(nlist.n_strx));
631 stab.is_func = false;
632 stab.symbol = sym_lookup.find(addr_lookup.get(self.getString(nlist.n_strx)).?);
622633 },
623634 macho.N_STSYM => {
624 stab.tag = .static;
635 stab.is_func = false;
625636 stab.symbol = sym_lookup.find(nlist.n_value);
626637 },
627638 else => {
......@@ -1421,11 +1432,7 @@ pub fn calcStabsSize(self: *Object, macho_file: *MachO) error{Overflow}!void {
14211432 const file = sym.getFile(macho_file).?;
14221433 if (file.getIndex() != self.index) continue;
14231434 if (!sym.flags.output_symtab) continue;
1424 const nstabs: u32 = switch (stab.tag) {
1425 .func => 4, // N_BNSYM, N_FUN, N_FUN, N_ENSYM
1426 .global => 1, // N_GSYM
1427 .static => 1, // N_STSYM
1428 };
1435 const nstabs: u32 = if (stab.is_func) 4 else 1;
14291436 self.output_symtab_ctx.nstabs += nstabs;
14301437 }
14311438 }
......@@ -1654,31 +1661,27 @@ pub fn writeStabs(self: *const Object, macho_file: *MachO, ctx: anytype) error{O
16541661 const sym_n_sect: u8 = if (!sym.flags.abs) @intCast(sym.out_n_sect + 1) else 0;
16551662 const sym_n_value = sym.getAddress(.{}, macho_file);
16561663 const sym_size = sym.getSize(macho_file);
1657 switch (stab.tag) {
1658 .func => {
1659 writeFuncStab(sym_n_strx, sym_n_sect, sym_n_value, sym_size, index, ctx);
1660 index += 4;
1661 },
1662 .global => {
1663 ctx.symtab.items[index] = .{
1664 .n_strx = sym_n_strx,
1665 .n_type = macho.N_GSYM,
1666 .n_sect = sym_n_sect,
1667 .n_desc = 0,
1668 .n_value = 0,
1669 };
1670 index += 1;
1671 },
1672 .static => {
1673 ctx.symtab.items[index] = .{
1674 .n_strx = sym_n_strx,
1675 .n_type = macho.N_STSYM,
1676 .n_sect = sym_n_sect,
1677 .n_desc = 0,
1678 .n_value = sym_n_value,
1679 };
1680 index += 1;
1681 },
1664 if (stab.is_func) {
1665 writeFuncStab(sym_n_strx, sym_n_sect, sym_n_value, sym_size, index, ctx);
1666 index += 4;
1667 } else if (sym.visibility == .global) {
1668 ctx.symtab.items[index] = .{
1669 .n_strx = sym_n_strx,
1670 .n_type = macho.N_GSYM,
1671 .n_sect = sym_n_sect,
1672 .n_desc = 0,
1673 .n_value = 0,
1674 };
1675 index += 1;
1676 } else {
1677 ctx.symtab.items[index] = .{
1678 .n_strx = sym_n_strx,
1679 .n_type = macho.N_STSYM,
1680 .n_sect = sym_n_sect,
1681 .n_desc = 0,
1682 .n_value = sym_n_value,
1683 };
1684 index += 1;
16821685 }
16831686 }
16841687
......@@ -1976,7 +1979,7 @@ const StabFile = struct {
19761979 }
19771980
19781981 const Stab = struct {
1979 tag: enum { func, global, static } = .func,
1982 is_func: bool = true,
19801983 symbol: ?Symbol.Index = null,
19811984
19821985 fn getSymbol(stab: Stab, macho_file: *MachO) ?*Symbol {