authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-07-16 10:53:47+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-07-22 16:58:21+02:00
log41b91442f4af293d4cbbabbe9e451bd31c5c5679
tree97cf00b1e93bd90847f33ce905db1d5e87fd5e9d
parent4658d857de9198e825f13c136b2342d630e19e62

macho: improve logs for dyld info


8 files changed, 88 insertions(+), 54 deletions(-)

src/arch/aarch64/CodeGen.zig+3-3
......@@ -3190,14 +3190,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
31903190 lib_name,
31913191 });
31923192 }
3193 const global_index = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
3193 const sym_index = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
31943194
31953195 _ = try self.addInst(.{
31963196 .tag = .call_extern,
31973197 .data = .{
3198 .extern_fn = .{
3198 .relocation = .{
31993199 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
3200 .global_index = global_index,
3200 .sym_index = sym_index,
32013201 },
32023202 },
32033203 });
src/arch/aarch64/Emit.zig+6-4
......@@ -649,7 +649,7 @@ fn mirDebugEpilogueBegin(self: *Emit) !void {
649649
650650fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
651651 assert(emit.mir.instructions.items(.tag)[inst] == .call_extern);
652 const extern_fn = emit.mir.instructions.items(.data)[inst].extern_fn;
652 const relocation = emit.mir.instructions.items(.data)[inst].relocation;
653653
654654 if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
655655 const offset = blk: {
......@@ -659,11 +659,13 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
659659 break :blk offset;
660660 };
661661 // Add relocation to the decl.
662 const atom = macho_file.atom_by_index_table.get(extern_fn.atom_index).?;
663 const target = macho_file.globals.values()[extern_fn.global_index];
662 const atom = macho_file.atom_by_index_table.get(relocation.atom_index).?;
664663 try atom.relocs.append(emit.bin_file.allocator, .{
665664 .offset = offset,
666 .target = target,
665 .target = .{
666 .sym_index = relocation.sym_index,
667 .file = null,
668 },
667669 .addend = 0,
668670 .subtractor = null,
669671 .pcrel = true,
src/arch/aarch64/Mir.zig+5-3
......@@ -225,14 +225,16 @@ pub const Inst = struct {
225225 ///
226226 /// Used by e.g. b
227227 inst: Index,
228 /// An extern function
228 /// Relocation for the linker where:
229 /// * `atom_index` is the index of the source
230 /// * `sym_index` is the index of the target
229231 ///
230232 /// Used by e.g. call_extern
231 extern_fn: struct {
233 relocation: struct {
232234 /// Index of the containing atom.
233235 atom_index: u32,
234236 /// Index into the linker's string table.
235 global_index: u32,
237 sym_index: u32,
236238 },
237239 /// A 16-bit immediate value.
238240 ///
src/arch/x86_64/CodeGen.zig+4-4
......@@ -2644,7 +2644,7 @@ fn loadMemPtrIntoRegister(self: *Self, reg: Register, ptr_ty: Type, ptr: MCValue
26442644 .flags = flags,
26452645 }),
26462646 .data = .{
2647 .load_reloc = .{
2647 .relocation = .{
26482648 .atom_index = fn_owner_decl.link.macho.sym_index,
26492649 .sym_index = sym_index,
26502650 },
......@@ -3997,14 +3997,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
39973997 lib_name,
39983998 });
39993999 }
4000 const global_index = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
4000 const sym_index = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
40014001 _ = try self.addInst(.{
40024002 .tag = .call_extern,
40034003 .ops = undefined,
40044004 .data = .{
4005 .extern_fn = .{
4005 .relocation = .{
40064006 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
4007 .global_index = global_index,
4007 .sym_index = sym_index,
40084008 },
40094009 },
40104010 });
src/arch/x86_64/Emit.zig+10-8
......@@ -982,7 +982,7 @@ fn mirLeaPie(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
982982 const tag = emit.mir.instructions.items(.tag)[inst];
983983 assert(tag == .lea_pie);
984984 const ops = emit.mir.instructions.items(.ops)[inst].decode();
985 const load_reloc = emit.mir.instructions.items(.data)[inst].load_reloc;
985 const relocation = emit.mir.instructions.items(.data)[inst].relocation;
986986
987987 // lea reg1, [rip + reloc]
988988 // RM
......@@ -1001,11 +1001,11 @@ fn mirLeaPie(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
10011001 0b01 => @enumToInt(std.macho.reloc_type_x86_64.X86_64_RELOC_SIGNED),
10021002 else => return emit.fail("TODO unused LEA PIE variants 0b10 and 0b11", .{}),
10031003 };
1004 const atom = macho_file.atom_by_index_table.get(load_reloc.atom_index).?;
1005 log.debug("adding reloc of type {} to local @{d}", .{ reloc_type, load_reloc.sym_index });
1004 const atom = macho_file.atom_by_index_table.get(relocation.atom_index).?;
1005 log.debug("adding reloc of type {} to local @{d}", .{ reloc_type, relocation.sym_index });
10061006 try atom.relocs.append(emit.bin_file.allocator, .{
10071007 .offset = @intCast(u32, end_offset - 4),
1008 .target = .{ .sym_index = load_reloc.sym_index, .file = null },
1008 .target = .{ .sym_index = relocation.sym_index, .file = null },
10091009 .addend = 0,
10101010 .subtractor = null,
10111011 .pcrel = true,
......@@ -1116,7 +1116,7 @@ fn mirCmpFloatAvx(emit: *Emit, tag: Tag, inst: Mir.Inst.Index) InnerError!void {
11161116fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
11171117 const tag = emit.mir.instructions.items(.tag)[inst];
11181118 assert(tag == .call_extern);
1119 const extern_fn = emit.mir.instructions.items(.data)[inst].extern_fn;
1119 const relocation = emit.mir.instructions.items(.data)[inst].relocation;
11201120
11211121 const offset = blk: {
11221122 // callq
......@@ -1126,11 +1126,13 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
11261126
11271127 if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
11281128 // Add relocation to the decl.
1129 const atom = macho_file.atom_by_index_table.get(extern_fn.atom_index).?;
1130 const target = macho_file.globals.values()[extern_fn.global_index];
1129 const atom = macho_file.atom_by_index_table.get(relocation.atom_index).?;
11311130 try atom.relocs.append(emit.bin_file.allocator, .{
11321131 .offset = offset,
1133 .target = target,
1132 .target = .{
1133 .sym_index = relocation.sym_index,
1134 .file = null,
1135 },
11341136 .addend = 0,
11351137 .subtractor = null,
11361138 .pcrel = true,
src/arch/x86_64/Mir.zig+6-11
......@@ -181,7 +181,7 @@ pub const Inst = struct {
181181 /// 0b00 reg1, [rip + reloc] // via GOT emits X86_64_RELOC_GOT relocation
182182 /// 0b01 reg1, [rip + reloc] // direct load emits X86_64_RELOC_SIGNED relocation
183183 /// Notes:
184 /// * `Data` contains `load_reloc`
184 /// * `Data` contains `relocation`
185185 lea_pie,
186186
187187 /// ops flags: form:
......@@ -368,7 +368,7 @@ pub const Inst = struct {
368368 /// Pseudo-instructions
369369 /// call extern function
370370 /// Notes:
371 /// * target of the call is stored as `extern_fn` in `Data` union.
371 /// * target of the call is stored as `relocation` in `Data` union.
372372 call_extern,
373373
374374 /// end of prologue
......@@ -439,15 +439,10 @@ pub const Inst = struct {
439439 /// A condition code for use with EFLAGS register.
440440 cc: bits.Condition,
441441 },
442 /// An extern function.
443 extern_fn: struct {
444 /// Index of the containing atom.
445 atom_index: u32,
446 /// Index into the linker's globals table.
447 global_index: u32,
448 },
449 /// PIE load relocation.
450 load_reloc: struct {
442 /// Relocation for the linker where:
443 /// * `atom_index` is the index of the source
444 /// * `sym_index` is the index of the target
445 relocation: struct {
451446 /// Index of the containing atom.
452447 atom_index: u32,
453448 /// Index into the linker's symbol table.
src/link/MachO.zig+52-17
......@@ -153,6 +153,13 @@ rustc_section_size: u64 = 0,
153153
154154locals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
155155globals: std.StringArrayHashMapUnmanaged(SymbolWithLoc) = .{},
156// FIXME Jakub
157// TODO storing index into globals might be dangerous if we delete a global
158// while not having everything resolved. Actually, perhaps `unresolved`
159// should not be stored at the global scope? Is this possible?
160// Otherwise, audit if this can be a problem.
161// An alternative, which I still need to investigate for perf reasons is to
162// store all global names in an adapted with context strtab.
156163unresolved: std.AutoArrayHashMapUnmanaged(u32, bool) = .{},
157164
158165locals_free_list: std.ArrayListUnmanaged(u32) = .{},
......@@ -2449,9 +2456,9 @@ pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
24492456
24502457 const target_sym = self.getSymbol(target);
24512458 if (target_sym.undf()) {
2452 const global_index = @intCast(u32, self.globals.getIndex(self.getSymbolName(target)).?);
2459 const global = self.globals.get(self.getSymbolName(target)).?;
24532460 try atom.bindings.append(gpa, .{
2454 .global_index = global_index,
2461 .target = global,
24552462 .offset = 0,
24562463 });
24572464 } else {
......@@ -2483,9 +2490,10 @@ pub fn createTlvPtrAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
24832490 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
24842491 const target_sym = self.getSymbol(target);
24852492 assert(target_sym.undf());
2486 const global_index = @intCast(u32, self.globals.getIndex(self.getSymbolName(target)).?);
2493
2494 const global = self.globals.get(self.getSymbolName(target)).?;
24872495 try atom.bindings.append(gpa, .{
2488 .global_index = global_index,
2496 .target = global,
24892497 .offset = 0,
24902498 });
24912499
......@@ -2739,7 +2747,6 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
27392747pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWithLoc) !*Atom {
27402748 const gpa = self.base.allocator;
27412749 const sym_index = @intCast(u32, self.locals.items.len);
2742 const global_index = @intCast(u32, self.globals.getIndex(self.getSymbolName(target)).?);
27432750 try self.locals.append(gpa, .{
27442751 .n_strx = 0,
27452752 .n_type = macho.N_SECT,
......@@ -2762,8 +2769,10 @@ pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWi
27622769 },
27632770 });
27642771 try atom.rebases.append(gpa, 0);
2772
2773 const global = self.globals.get(self.getSymbolName(target)).?;
27652774 try atom.lazy_bindings.append(gpa, .{
2766 .global_index = global_index,
2775 .target = global,
27672776 .offset = 0,
27682777 });
27692778
......@@ -4149,6 +4158,7 @@ pub fn deleteExport(self: *MachO, exp: Export) void {
41494158 const sym = self.getSymbolPtr(sym_loc);
41504159 const sym_name = self.getSymbolName(sym_loc);
41514160 log.debug("deleting export '{s}'", .{sym_name});
4161 assert(sym.sect() and sym.ext());
41524162 sym.* = .{
41534163 .n_strx = 0,
41544164 .n_type = 0,
......@@ -5307,7 +5317,9 @@ pub fn getGlobalSymbol(self: *MachO, name: []const u8) !u32 {
53075317 defer if (gop.found_existing) gpa.free(sym_name);
53085318
53095319 if (gop.found_existing) {
5310 return @intCast(u32, self.globals.getIndex(sym_name).?);
5320 // TODO audit this: can we ever reference anything from outside the Zig module?
5321 assert(gop.value_ptr.file == null);
5322 return gop.value_ptr.sym_index;
53115323 }
53125324
53135325 const sym_index = @intCast(u32, self.locals.items.len);
......@@ -5324,7 +5336,7 @@ pub fn getGlobalSymbol(self: *MachO, name: []const u8) !u32 {
53245336 };
53255337 try self.unresolved.putNoClobber(gpa, global_index, true);
53265338
5327 return global_index;
5339 return sym_index;
53285340}
53295341
53305342fn getSegmentAllocBase(self: MachO, indices: []const ?u16) struct { vmaddr: u64, fileoff: u64 } {
......@@ -5690,6 +5702,8 @@ fn updateSectionOrdinals(self: *MachO) !void {
56905702 }
56915703 }
56925704
5705 // FIXME Jakub
5706 // TODO no need for duping work here; simply walk the atom graph
56935707 for (self.locals.items) |*sym| {
56945708 if (sym.undf()) continue;
56955709 if (sym.n_sect == 0) continue;
......@@ -5735,11 +5749,12 @@ fn writeDyldInfoData(self: *MachO) !void {
57355749 log.debug("dyld info for {s},{s}", .{ sect.segName(), sect.sectName() });
57365750
57375751 while (true) {
5738 log.debug(" ATOM %{d}", .{atom.sym_index});
5752 log.debug(" ATOM(%{d}, '{s}')", .{ atom.sym_index, atom.getName(self) });
57395753 const sym = atom.getSymbol(self);
57405754 const base_offset = sym.n_value - seg.inner.vmaddr;
57415755
57425756 for (atom.rebases.items) |offset| {
5757 log.debug(" | rebase at {x}", .{base_offset + offset});
57435758 try rebase_pointers.append(.{
57445759 .offset = base_offset + offset,
57455760 .segment_id = match.seg,
......@@ -5747,33 +5762,53 @@ fn writeDyldInfoData(self: *MachO) !void {
57475762 }
57485763
57495764 for (atom.bindings.items) |binding| {
5750 const global = self.globals.values()[binding.global_index];
5751 const bind_sym = self.getSymbol(global);
5765 const bind_sym = self.getSymbol(binding.target);
5766 const bind_sym_name = self.getSymbolName(binding.target);
5767 const dylib_ordinal = @divTrunc(
5768 @bitCast(i16, bind_sym.n_desc),
5769 macho.N_SYMBOL_RESOLVER,
5770 );
57525771 var flags: u4 = 0;
5772 log.debug(" | bind at {x}, import('{s}') in dylib({d})", .{
5773 binding.offset + base_offset,
5774 bind_sym_name,
5775 dylib_ordinal,
5776 });
57535777 if (bind_sym.weakRef()) {
5778 log.debug(" | marking as weak ref ", .{});
57545779 flags |= @truncate(u4, macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT);
57555780 }
57565781 try bind_pointers.append(.{
57575782 .offset = binding.offset + base_offset,
57585783 .segment_id = match.seg,
5759 .dylib_ordinal = @divTrunc(@bitCast(i16, bind_sym.n_desc), macho.N_SYMBOL_RESOLVER),
5760 .name = self.getSymbolName(global),
5784 .dylib_ordinal = dylib_ordinal,
5785 .name = bind_sym_name,
57615786 .bind_flags = flags,
57625787 });
57635788 }
57645789
57655790 for (atom.lazy_bindings.items) |binding| {
5766 const global = self.globals.values()[binding.global_index];
5767 const bind_sym = self.getSymbol(global);
5791 const bind_sym = self.getSymbol(binding.target);
5792 const bind_sym_name = self.getSymbolName(binding.target);
5793 const dylib_ordinal = @divTrunc(
5794 @bitCast(i16, bind_sym.n_desc),
5795 macho.N_SYMBOL_RESOLVER,
5796 );
57685797 var flags: u4 = 0;
5798 log.debug(" | lazy bind at {x} import('{s}') ord({d})", .{
5799 binding.offset + base_offset,
5800 bind_sym_name,
5801 dylib_ordinal,
5802 });
57695803 if (bind_sym.weakRef()) {
5804 log.debug(" | marking as weak ref ", .{});
57705805 flags |= @truncate(u4, macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT);
57715806 }
57725807 try lazy_bind_pointers.append(.{
57735808 .offset = binding.offset + base_offset,
57745809 .segment_id = match.seg,
5775 .dylib_ordinal = @divTrunc(@bitCast(i16, bind_sym.n_desc), macho.N_SYMBOL_RESOLVER),
5776 .name = self.getSymbolName(global),
5810 .dylib_ordinal = dylib_ordinal,
5811 .name = bind_sym_name,
57775812 .bind_flags = flags,
57785813 });
57795814 }
src/link/MachO/Atom.zig+2-4
......@@ -71,7 +71,7 @@ dbg_info_atom: Dwarf.Atom,
7171dirty: bool = true,
7272
7373pub const Binding = struct {
74 global_index: u32,
74 target: SymbolWithLoc,
7575 offset: u64,
7676};
7777
......@@ -536,10 +536,8 @@ fn addPtrBindingOrRebase(
536536 const gpa = context.macho_file.base.allocator;
537537 const sym = context.macho_file.getSymbol(target);
538538 if (sym.undf()) {
539 const sym_name = context.macho_file.getSymbolName(target);
540 const global_index = @intCast(u32, context.macho_file.globals.getIndex(sym_name).?);
541539 try self.bindings.append(gpa, .{
542 .global_index = global_index,
540 .target = target,
543541 .offset = @intCast(u32, rel.r_address - context.base_offset),
544542 });
545543 } else {