authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-01-24 17:55:10+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-01-26 14:29:14+01:00
log4d804c1b239e45a0a28f4caf1f9748dac44ddce2
treeb22466da22ef55cbd69f1792939ceb378dc1e654
parent48f9e491cb97de54c7e8d395170d8b4c8ea9a62b

macho: completely remove allocateDeclIndexes in favor of linker tracking


6 files changed, 115 insertions(+), 88 deletions(-)

src/Module.zig+1-1
......@@ -5324,7 +5324,7 @@ pub fn deleteUnusedDecl(mod: *Module, decl_index: Decl.Index) void {
53245324 // Until then, we did call `allocateDeclIndexes` on this anonymous Decl and so we
53255325 // must call `freeDecl` in the linker backend now.
53265326 switch (mod.comp.bin_file.tag) {
5327 .c => {}, // this linker backend has already migrated to the new API
5327 .macho, .c => {}, // this linker backend has already migrated to the new API
53285328 else => if (decl.has_tv) {
53295329 if (decl.ty.isFnOrHasRuntimeBits()) {
53305330 mod.comp.bin_file.freeDecl(decl_index);
src/arch/aarch64/CodeGen.zig+10-9
......@@ -3999,7 +3999,7 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
39993999 const mod = self.bin_file.options.module.?;
40004000 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
40014001 const atom_index = switch (self.bin_file.tag) {
4002 .macho => owner_decl.link.macho.sym_index,
4002 .macho => owner_decl.link.macho.getSymbolIndex().?,
40034003 .coff => owner_decl.link.coff.sym_index,
40044004 else => unreachable, // unsupported target format
40054005 };
......@@ -4328,10 +4328,11 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
43284328 if (func_value.castTag(.function)) |func_payload| {
43294329 const func = func_payload.data;
43304330 const fn_owner_decl = mod.declPtr(func.owner_decl);
4331 try fn_owner_decl.link.macho.ensureInitialized(macho_file);
43314332 try self.genSetReg(Type.initTag(.u64), .x30, .{
43324333 .linker_load = .{
43334334 .type = .got,
4334 .sym_index = fn_owner_decl.link.macho.sym_index,
4335 .sym_index = fn_owner_decl.link.macho.getSymbolIndex().?,
43354336 },
43364337 });
43374338 // blr x30
......@@ -4354,7 +4355,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
43544355 .tag = .call_extern,
43554356 .data = .{
43564357 .relocation = .{
4357 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
4358 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.getSymbolIndex().?,
43584359 .sym_index = sym_index,
43594360 },
43604361 },
......@@ -5537,7 +5538,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
55375538 const mod = self.bin_file.options.module.?;
55385539 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
55395540 const atom_index = switch (self.bin_file.tag) {
5540 .macho => owner_decl.link.macho.sym_index,
5541 .macho => owner_decl.link.macho.getSymbolIndex().?,
55415542 .coff => owner_decl.link.coff.sym_index,
55425543 else => unreachable, // unsupported target format
55435544 };
......@@ -5651,7 +5652,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
56515652 const mod = self.bin_file.options.module.?;
56525653 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
56535654 const atom_index = switch (self.bin_file.tag) {
5654 .macho => owner_decl.link.macho.sym_index,
5655 .macho => owner_decl.link.macho.getSymbolIndex().?,
56555656 .coff => owner_decl.link.coff.sym_index,
56565657 else => unreachable, // unsupported target format
56575658 };
......@@ -5845,7 +5846,7 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I
58455846 const mod = self.bin_file.options.module.?;
58465847 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
58475848 const atom_index = switch (self.bin_file.tag) {
5848 .macho => owner_decl.link.macho.sym_index,
5849 .macho => owner_decl.link.macho.getSymbolIndex().?,
58495850 .coff => owner_decl.link.coff.sym_index,
58505851 else => unreachable, // unsupported target format
58515852 };
......@@ -6168,13 +6169,13 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
61686169 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
61696170 const got_addr = got.p_vaddr + decl.link.elf.offset_table_index * ptr_bytes;
61706171 return MCValue{ .memory = got_addr };
6171 } else if (self.bin_file.cast(link.File.MachO)) |_| {
6172 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
61726173 // Because MachO is PIE-always-on, we defer memory address resolution until
61736174 // the linker has enough info to perform relocations.
6174 assert(decl.link.macho.sym_index != 0);
6175 try decl.link.macho.ensureInitialized(macho_file);
61756176 return MCValue{ .linker_load = .{
61766177 .type = .got,
6177 .sym_index = decl.link.macho.sym_index,
6178 .sym_index = decl.link.macho.getSymbolIndex().?,
61786179 } };
61796180 } else if (self.bin_file.cast(link.File.Coff)) |_| {
61806181 // Because COFF is PIE-always-on, we defer memory address resolution until
src/arch/x86_64/CodeGen.zig+7-6
......@@ -2671,7 +2671,7 @@ fn loadMemPtrIntoRegister(self: *Self, reg: Register, ptr_ty: Type, ptr: MCValue
26712671 const mod = self.bin_file.options.module.?;
26722672 const fn_owner_decl = mod.declPtr(self.mod_fn.owner_decl);
26732673 const atom_index = if (self.bin_file.tag == link.File.MachO.base_tag)
2674 fn_owner_decl.link.macho.sym_index
2674 fn_owner_decl.link.macho.getSymbolIndex().?
26752675 else
26762676 fn_owner_decl.link.coff.sym_index;
26772677 const flags: u2 = switch (load_struct.type) {
......@@ -4090,7 +4090,8 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
40904090 if (func_value.castTag(.function)) |func_payload| {
40914091 const func = func_payload.data;
40924092 const fn_owner_decl = mod.declPtr(func.owner_decl);
4093 const sym_index = fn_owner_decl.link.macho.sym_index;
4093 try fn_owner_decl.link.macho.ensureInitialized(macho_file);
4094 const sym_index = fn_owner_decl.link.macho.getSymbolIndex().?;
40944095 try self.genSetReg(Type.initTag(.usize), .rax, .{
40954096 .linker_load = .{
40964097 .type = .got,
......@@ -4121,7 +4122,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
41214122 .ops = undefined,
41224123 .data = .{
41234124 .relocation = .{
4124 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
4125 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.getSymbolIndex().?,
41254126 .sym_index = sym_index,
41264127 },
41274128 },
......@@ -6784,11 +6785,11 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
67846785 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
67856786 const got_addr = got.p_vaddr + decl.link.elf.offset_table_index * ptr_bytes;
67866787 return MCValue{ .memory = got_addr };
6787 } else if (self.bin_file.cast(link.File.MachO)) |_| {
6788 assert(decl.link.macho.sym_index != 0);
6788 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
6789 try decl.link.macho.ensureInitialized(macho_file);
67896790 return MCValue{ .linker_load = .{
67906791 .type = .got,
6791 .sym_index = decl.link.macho.sym_index,
6792 .sym_index = decl.link.macho.getSymbolIndex().?,
67926793 } };
67936794 } else if (self.bin_file.cast(link.File.Coff)) |_| {
67946795 assert(decl.link.coff.sym_index != 0);
src/link.zig+3-1
......@@ -617,7 +617,7 @@ pub const File = struct {
617617 switch (base.tag) {
618618 .coff => return @fieldParentPtr(Coff, "base", base).allocateDeclIndexes(decl_index),
619619 .elf => return @fieldParentPtr(Elf, "base", base).allocateDeclIndexes(decl_index),
620 .macho => return @fieldParentPtr(MachO, "base", base).allocateDeclIndexes(decl_index),
620 .macho => {}, // no-op
621621 .wasm => return @fieldParentPtr(Wasm, "base", base).allocateDeclIndexes(decl_index),
622622 .plan9 => return @fieldParentPtr(Plan9, "base", base).allocateDeclIndexes(decl_index),
623623 .c, .spirv, .nvptx => {},
......@@ -911,6 +911,8 @@ pub const File = struct {
911911 /// The linker is passed information about the containing atom, `parent_atom_index`, and offset within it's
912912 /// memory buffer, `offset`, so that it can make a note of potential relocation sites, should the
913913 /// `Decl`'s address was not yet resolved, or the containing atom gets moved in virtual memory.
914 /// May be called before or after updateFunc/updateDecl therefore it is up to the linker to allocate
915 /// the block/atom.
914916 pub fn getDeclVAddr(base: *File, decl_index: Module.Decl.Index, reloc_info: RelocInfo) !u64 {
915917 if (build_options.only_c) unreachable;
916918 switch (base.tag) {
src/link/MachO.zig+72-63
......@@ -1811,6 +1811,8 @@ pub fn deinit(self: *MachO) void {
18111811fn freeAtom(self: *MachO, atom: *Atom) void {
18121812 log.debug("freeAtom {*}", .{atom});
18131813
1814 const gpa = self.base.allocator;
1815
18141816 // Remove any relocs and base relocs associated with this Atom
18151817 self.freeRelocationsForAtom(atom);
18161818
......@@ -1850,7 +1852,7 @@ fn freeAtom(self: *MachO, atom: *Atom) void {
18501852 if (!already_have_free_list_node and prev.freeListEligible(self)) {
18511853 // The free list is heuristics, it doesn't have to be perfect, so we can ignore
18521854 // the OOM here.
1853 free_list.append(self.base.allocator, prev) catch {};
1855 free_list.append(gpa, prev) catch {};
18541856 }
18551857 } else {
18561858 atom.prev = null;
......@@ -1862,6 +1864,33 @@ fn freeAtom(self: *MachO, atom: *Atom) void {
18621864 atom.next = null;
18631865 }
18641866
1867 // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.
1868 const sym_index = atom.getSymbolIndex().?;
1869
1870 self.locals_free_list.append(gpa, sym_index) catch {};
1871
1872 // Try freeing GOT atom if this decl had one
1873 const got_target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
1874 if (self.got_entries_table.get(got_target)) |got_index| {
1875 self.got_entries_free_list.append(gpa, @intCast(u32, got_index)) catch {};
1876 self.got_entries.items[got_index] = .{
1877 .target = .{ .sym_index = 0, .file = null },
1878 .sym_index = 0,
1879 };
1880 _ = self.got_entries_table.remove(got_target);
1881
1882 if (self.d_sym) |*d_sym| {
1883 d_sym.swapRemoveRelocs(sym_index);
1884 }
1885
1886 log.debug(" adding GOT index {d} to free list (target local@{d})", .{ got_index, sym_index });
1887 }
1888
1889 self.locals.items[sym_index].n_type = 0;
1890 _ = self.atom_by_index_table.remove(sym_index);
1891 log.debug(" adding local symbol index {d} to free list", .{sym_index});
1892 atom.sym_index = 0;
1893
18651894 if (self.d_sym) |*d_sym| {
18661895 d_sym.dwarf.freeAtom(&atom.dbg_info_atom);
18671896 }
......@@ -1883,7 +1912,7 @@ fn growAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64) !u64
18831912 return self.allocateAtom(atom, new_atom_size, alignment);
18841913}
18851914
1886fn allocateSymbol(self: *MachO) !u32 {
1915pub fn allocateSymbol(self: *MachO) !u32 {
18871916 try self.locals.ensureUnusedCapacity(self.base.allocator, 1);
18881917
18891918 const index = blk: {
......@@ -1975,16 +2004,6 @@ pub fn allocateStubEntry(self: *MachO, target: SymbolWithLoc) !u32 {
19752004 return index;
19762005}
19772006
1978pub fn allocateDeclIndexes(self: *MachO, decl_index: Module.Decl.Index) !void {
1979 if (self.llvm_object) |_| return;
1980 const decl = self.base.options.module.?.declPtr(decl_index);
1981 if (decl.link.macho.sym_index != 0) return;
1982
1983 decl.link.macho.sym_index = try self.allocateSymbol();
1984 try self.atom_by_index_table.putNoClobber(self.base.allocator, decl.link.macho.sym_index, &decl.link.macho);
1985 try self.decls.putNoClobber(self.base.allocator, decl_index, null);
1986}
1987
19882007pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void {
19892008 if (build_options.skip_non_native and builtin.object_format != .macho) {
19902009 @panic("Attempted to compile for object format that was disabled by build configuration");
......@@ -1997,8 +2016,15 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv
19972016
19982017 const decl_index = func.owner_decl;
19992018 const decl = module.declPtr(decl_index);
2000 self.freeUnnamedConsts(decl_index);
2001 self.freeRelocationsForAtom(&decl.link.macho);
2019 const atom = &decl.link.macho;
2020 try atom.ensureInitialized(self);
2021 const gop = try self.decls.getOrPut(self.base.allocator, decl_index);
2022 if (gop.found_existing) {
2023 self.freeUnnamedConsts(decl_index);
2024 self.freeRelocationsForAtom(atom);
2025 } else {
2026 gop.value_ptr.* = null;
2027 }
20022028
20032029 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
20042030 defer code_buffer.deinit();
......@@ -2136,7 +2162,14 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
21362162 }
21372163 }
21382164
2139 self.freeRelocationsForAtom(&decl.link.macho);
2165 const atom = &decl.link.macho;
2166 try atom.ensureInitialized(self);
2167 const gop = try self.decls.getOrPut(self.base.allocator, decl_index);
2168 if (gop.found_existing) {
2169 self.freeRelocationsForAtom(atom);
2170 } else {
2171 gop.value_ptr.* = null;
2172 }
21402173
21412174 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
21422175 defer code_buffer.deinit();
......@@ -2337,12 +2370,12 @@ fn updateDeclCode(self: *MachO, decl_index: Module.Decl.Index, code: []const u8)
23372370 const decl = mod.declPtr(decl_index);
23382371
23392372 const required_alignment = decl.getAlignment(self.base.options.target);
2340 assert(decl.link.macho.sym_index != 0); // Caller forgot to call allocateDeclIndexes()
23412373
23422374 const sym_name = try decl.getFullyQualifiedName(mod);
23432375 defer self.base.allocator.free(sym_name);
23442376
23452377 const atom = &decl.link.macho;
2378 const sym_index = atom.getSymbolIndex().?; // Atom was not initialized
23462379 const decl_ptr = self.decls.getPtr(decl_index).?;
23472380 if (decl_ptr.* == null) {
23482381 decl_ptr.* = self.getDeclOutputSection(decl);
......@@ -2368,7 +2401,7 @@ fn updateDeclCode(self: *MachO, decl_index: Module.Decl.Index, code: []const u8)
23682401 if (vaddr != sym.n_value) {
23692402 sym.n_value = vaddr;
23702403 log.debug(" (updating GOT entry)", .{});
2371 const got_target = SymbolWithLoc{ .sym_index = atom.sym_index, .file = null };
2404 const got_target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
23722405 const got_atom = self.getGotAtomForSymbol(got_target).?;
23732406 self.markRelocsDirtyByTarget(got_target);
23742407 try self.writePtrWidthAtom(got_atom);
......@@ -2399,10 +2432,10 @@ fn updateDeclCode(self: *MachO, decl_index: Module.Decl.Index, code: []const u8)
23992432 atom.size = code_len;
24002433 sym.n_value = vaddr;
24012434
2402 const got_target = SymbolWithLoc{ .sym_index = atom.sym_index, .file = null };
2435 const got_target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
24032436 const got_index = try self.allocateGotEntry(got_target);
24042437 const got_atom = try self.createGotAtom(got_target);
2405 self.got_entries.items[got_index].sym_index = got_atom.sym_index;
2438 self.got_entries.items[got_index].sym_index = got_atom.getSymbolIndex().?;
24062439 try self.writePtrWidthAtom(got_atom);
24072440 }
24082441
......@@ -2438,7 +2471,14 @@ pub fn updateDeclExports(
24382471 const gpa = self.base.allocator;
24392472
24402473 const decl = module.declPtr(decl_index);
2441 if (decl.link.macho.sym_index == 0) return;
2474 const atom = &decl.link.macho;
2475 try atom.ensureInitialized(self);
2476
2477 const gop = try self.decls.getOrPut(gpa, decl_index);
2478 if (!gop.found_existing) {
2479 gop.value_ptr.* = null;
2480 }
2481
24422482 const decl_sym = decl.link.macho.getSymbol(self);
24432483
24442484 for (exports) |exp| {
......@@ -2573,11 +2613,6 @@ fn freeUnnamedConsts(self: *MachO, decl_index: Module.Decl.Index) void {
25732613 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;
25742614 for (unnamed_consts.items) |atom| {
25752615 self.freeAtom(atom);
2576 self.locals_free_list.append(gpa, atom.sym_index) catch {};
2577 self.locals.items[atom.sym_index].n_type = 0;
2578 _ = self.atom_by_index_table.remove(atom.sym_index);
2579 log.debug(" adding local symbol index {d} to free list", .{atom.sym_index});
2580 atom.sym_index = 0;
25812616 }
25822617 unnamed_consts.clearAndFree(gpa);
25832618}
......@@ -2591,39 +2626,11 @@ pub fn freeDecl(self: *MachO, decl_index: Module.Decl.Index) void {
25912626
25922627 log.debug("freeDecl {*}", .{decl});
25932628
2594 const kv = self.decls.fetchSwapRemove(decl_index);
2595 if (kv.?.value) |_| {
2596 self.freeAtom(&decl.link.macho);
2597 self.freeUnnamedConsts(decl_index);
2598 }
2599
2600 // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.
2601 const gpa = self.base.allocator;
2602 const sym_index = decl.link.macho.sym_index;
2603 if (sym_index != 0) {
2604 self.locals_free_list.append(gpa, sym_index) catch {};
2605
2606 // Try freeing GOT atom if this decl had one
2607 const got_target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
2608 if (self.got_entries_table.get(got_target)) |got_index| {
2609 self.got_entries_free_list.append(gpa, @intCast(u32, got_index)) catch {};
2610 self.got_entries.items[got_index] = .{
2611 .target = .{ .sym_index = 0, .file = null },
2612 .sym_index = 0,
2613 };
2614 _ = self.got_entries_table.remove(got_target);
2615
2616 if (self.d_sym) |*d_sym| {
2617 d_sym.swapRemoveRelocs(sym_index);
2618 }
2619
2620 log.debug(" adding GOT index {d} to free list (target local@{d})", .{ got_index, sym_index });
2629 if (self.decls.fetchSwapRemove(decl_index)) |kv| {
2630 if (kv.value) |_| {
2631 self.freeAtom(&decl.link.macho);
2632 self.freeUnnamedConsts(decl_index);
26212633 }
2622
2623 self.locals.items[sym_index].n_type = 0;
2624 _ = self.atom_by_index_table.remove(sym_index);
2625 log.debug(" adding local symbol index {d} to free list", .{sym_index});
2626 decl.link.macho.sym_index = 0;
26272634 }
26282635
26292636 if (self.d_sym) |*d_sym| {
......@@ -2636,7 +2643,9 @@ pub fn getDeclVAddr(self: *MachO, decl_index: Module.Decl.Index, reloc_info: Fil
26362643 const decl = mod.declPtr(decl_index);
26372644
26382645 assert(self.llvm_object == null);
2639 assert(decl.link.macho.sym_index != 0);
2646
2647 try decl.link.macho.ensureInitialized(self);
2648 const sym_index = decl.link.macho.getSymbolIndex().?;
26402649
26412650 const atom = self.getAtomForSymbol(.{ .sym_index = reloc_info.parent_atom_index, .file = null }).?;
26422651 try atom.addRelocation(self, .{
......@@ -2645,7 +2654,7 @@ pub fn getDeclVAddr(self: *MachO, decl_index: Module.Decl.Index, reloc_info: Fil
26452654 .x86_64 => @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_UNSIGNED),
26462655 else => unreachable,
26472656 },
2648 .target = .{ .sym_index = decl.link.macho.sym_index, .file = null },
2657 .target = .{ .sym_index = sym_index, .file = null },
26492658 .offset = @intCast(u32, reloc_info.offset),
26502659 .addend = reloc_info.addend,
26512660 .pcrel = false,
......@@ -3179,7 +3188,7 @@ fn collectRebaseData(self: *MachO, rebase: *Rebase) !void {
31793188 const slice = self.sections.slice();
31803189
31813190 for (self.rebases.keys()) |atom, i| {
3182 log.debug(" ATOM(%{d}, '{s}')", .{ atom.sym_index, atom.getName(self) });
3191 log.debug(" ATOM(%{?d}, '{s}')", .{ atom.getSymbolIndex(), atom.getName(self) });
31833192
31843193 const sym = atom.getSymbol(self);
31853194 const segment_index = slice.items(.segment_index)[sym.n_sect - 1];
......@@ -3208,7 +3217,7 @@ fn collectBindData(self: *MachO, bind: anytype, raw_bindings: anytype) !void {
32083217 const slice = self.sections.slice();
32093218
32103219 for (raw_bindings.keys()) |atom, i| {
3211 log.debug(" ATOM(%{d}, '{s}')", .{ atom.sym_index, atom.getName(self) });
3220 log.debug(" ATOM(%{?d}, '{s}')", .{ atom.getSymbolIndex(), atom.getName(self) });
32123221
32133222 const sym = atom.getSymbol(self);
32143223 const segment_index = slice.items(.segment_index)[sym.n_sect - 1];
......@@ -4277,8 +4286,8 @@ pub fn logAtoms(self: *MachO) void {
42774286pub fn logAtom(self: *MachO, atom: *const Atom) void {
42784287 const sym = atom.getSymbol(self);
42794288 const sym_name = atom.getName(self);
4280 log.debug(" ATOM(%{d}, '{s}') @ {x} (sizeof({x}), alignof({x})) in object({?d}) in sect({d})", .{
4281 atom.sym_index,
4289 log.debug(" ATOM(%{?d}, '{s}') @ {x} (sizeof({x}), alignof({x})) in object({?d}) in sect({d})", .{
4290 atom.getSymbolIndex(),
42824291 sym_name,
42834292 sym.n_value,
42844293 atom.size,
src/link/MachO/Atom.zig+22-8
......@@ -64,6 +64,17 @@ pub const empty = Atom{
6464 .dbg_info_atom = undefined,
6565};
6666
67pub fn ensureInitialized(self: *Atom, macho_file: *MachO) !void {
68 if (self.getSymbolIndex() != null) return; // Already initialized
69 self.sym_index = try macho_file.allocateSymbol();
70 try macho_file.atom_by_index_table.putNoClobber(macho_file.base.allocator, self.sym_index, self);
71}
72
73pub fn getSymbolIndex(self: Atom) ?u32 {
74 if (self.sym_index == 0) return null;
75 return self.sym_index;
76}
77
6778/// Returns symbol referencing this atom.
6879pub fn getSymbol(self: Atom, macho_file: *MachO) macho.nlist_64 {
6980 return self.getSymbolPtr(macho_file).*;
......@@ -71,20 +82,23 @@ pub fn getSymbol(self: Atom, macho_file: *MachO) macho.nlist_64 {
7182
7283/// Returns pointer-to-symbol referencing this atom.
7384pub fn getSymbolPtr(self: Atom, macho_file: *MachO) *macho.nlist_64 {
85 const sym_index = self.getSymbolIndex().?;
7486 return macho_file.getSymbolPtr(.{
75 .sym_index = self.sym_index,
87 .sym_index = sym_index,
7688 .file = self.file,
7789 });
7890}
7991
8092pub fn getSymbolWithLoc(self: Atom) SymbolWithLoc {
81 return .{ .sym_index = self.sym_index, .file = self.file };
93 const sym_index = self.getSymbolIndex().?;
94 return .{ .sym_index = sym_index, .file = self.file };
8295}
8396
8497/// Returns the name of this atom.
8598pub fn getName(self: Atom, macho_file: *MachO) []const u8 {
99 const sym_index = self.getSymbolIndex().?;
86100 return macho_file.getSymbolName(.{
87 .sym_index = self.sym_index,
101 .sym_index = sym_index,
88102 .file = self.file,
89103 });
90104}
......@@ -144,7 +158,7 @@ pub fn addRelocations(
144158
145159pub fn addRebase(self: *Atom, macho_file: *MachO, offset: u32) !void {
146160 const gpa = macho_file.base.allocator;
147 log.debug(" (adding rebase at offset 0x{x} in %{d})", .{ offset, self.sym_index });
161 log.debug(" (adding rebase at offset 0x{x} in %{?d})", .{ offset, self.getSymbolIndex() });
148162 const gop = try macho_file.rebases.getOrPut(gpa, self);
149163 if (!gop.found_existing) {
150164 gop.value_ptr.* = .{};
......@@ -154,10 +168,10 @@ pub fn addRebase(self: *Atom, macho_file: *MachO, offset: u32) !void {
154168
155169pub fn addBinding(self: *Atom, macho_file: *MachO, binding: Binding) !void {
156170 const gpa = macho_file.base.allocator;
157 log.debug(" (adding binding to symbol {s} at offset 0x{x} in %{d})", .{
171 log.debug(" (adding binding to symbol {s} at offset 0x{x} in %{?d})", .{
158172 macho_file.getSymbolName(binding.target),
159173 binding.offset,
160 self.sym_index,
174 self.getSymbolIndex(),
161175 });
162176 const gop = try macho_file.bindings.getOrPut(gpa, self);
163177 if (!gop.found_existing) {
......@@ -168,10 +182,10 @@ pub fn addBinding(self: *Atom, macho_file: *MachO, binding: Binding) !void {
168182
169183pub fn addLazyBinding(self: *Atom, macho_file: *MachO, binding: Binding) !void {
170184 const gpa = macho_file.base.allocator;
171 log.debug(" (adding lazy binding to symbol {s} at offset 0x{x} in %{d})", .{
185 log.debug(" (adding lazy binding to symbol {s} at offset 0x{x} in %{?d})", .{
172186 macho_file.getSymbolName(binding.target),
173187 binding.offset,
174 self.sym_index,
188 self.getSymbolIndex(),
175189 });
176190 const gop = try macho_file.lazy_bindings.getOrPut(gpa, self);
177191 if (!gop.found_existing) {