authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-01-26 14:28:44+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-01-26 14:29:14+01:00
logcc1d7a0e315ba63b0d8c0cd647b4c7e92a571bf2
tree281b181f5e1bc127ed41e3f7c9e958a57df4cf68
parente1b9800ffa74a637e2b0a6356249c2c37228ec01

coff: migrate to new non-allocateDeclIndexes API


8 files changed, 209 insertions(+), 202 deletions(-)

src/Module.zig+6-1
......@@ -5324,7 +5324,12 @@ 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 .elf, .macho, .c => {}, // this linker backend has already migrated to the new API
5327 .coff,
5328 .elf,
5329 .macho,
5330 .c,
5331 => {}, // this linker backend has already migrated to the new API
5332
53285333 else => if (decl.has_tv) {
53295334 if (decl.ty.isFnOrHasRuntimeBits()) {
53305335 mod.comp.bin_file.freeDecl(decl_index);
src/arch/aarch64/CodeGen.zig+10-9
......@@ -4000,7 +4000,7 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
40004000 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
40014001 const atom_index = switch (self.bin_file.tag) {
40024002 .macho => owner_decl.link.macho.getSymbolIndex().?,
4003 .coff => owner_decl.link.coff.sym_index,
4003 .coff => owner_decl.link.coff.getSymbolIndex().?,
40044004 else => unreachable, // unsupported target format
40054005 };
40064006 _ = try self.addInst(.{
......@@ -4318,11 +4318,12 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
43184318 .sym_index = fn_owner_decl.link.macho.getSymbolIndex().?,
43194319 },
43204320 });
4321 } else if (self.bin_file.cast(link.File.Coff)) |_| {
4321 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
4322 try fn_owner_decl.link.coff.ensureInitialized(coff_file);
43224323 try self.genSetReg(Type.initTag(.u64), .x30, .{
43234324 .linker_load = .{
43244325 .type = .got,
4325 .sym_index = fn_owner_decl.link.coff.sym_index,
4326 .sym_index = fn_owner_decl.link.coff.getSymbolIndex().?,
43264327 },
43274328 });
43284329 } else if (self.bin_file.cast(link.File.Plan9)) |p9| {
......@@ -5494,7 +5495,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
54945495 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
54955496 const atom_index = switch (self.bin_file.tag) {
54965497 .macho => owner_decl.link.macho.getSymbolIndex().?,
5497 .coff => owner_decl.link.coff.sym_index,
5498 .coff => owner_decl.link.coff.getSymbolIndex().?,
54985499 else => unreachable, // unsupported target format
54995500 };
55005501 _ = try self.addInst(.{
......@@ -5608,7 +5609,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
56085609 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
56095610 const atom_index = switch (self.bin_file.tag) {
56105611 .macho => owner_decl.link.macho.getSymbolIndex().?,
5611 .coff => owner_decl.link.coff.sym_index,
5612 .coff => owner_decl.link.coff.getSymbolIndex().?,
56125613 else => unreachable, // unsupported target format
56135614 };
56145615 _ = try self.addInst(.{
......@@ -5802,7 +5803,7 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I
58025803 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
58035804 const atom_index = switch (self.bin_file.tag) {
58045805 .macho => owner_decl.link.macho.getSymbolIndex().?,
5805 .coff => owner_decl.link.coff.sym_index,
5806 .coff => owner_decl.link.coff.getSymbolIndex().?,
58065807 else => unreachable, // unsupported target format
58075808 };
58085809 _ = try self.addInst(.{
......@@ -6129,11 +6130,11 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
61296130 .type = .got,
61306131 .sym_index = decl.link.macho.getSymbolIndex().?,
61316132 } };
6132 } else if (self.bin_file.cast(link.File.Coff)) |_| {
6133 assert(decl.link.coff.sym_index != 0);
6133 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
6134 try decl.link.coff.ensureInitialized(coff_file);
61346135 return MCValue{ .linker_load = .{
61356136 .type = .got,
6136 .sym_index = decl.link.coff.sym_index,
6137 .sym_index = decl.link.coff.getSymbolIndex().?,
61376138 } };
61386139 } else if (self.bin_file.cast(link.File.Plan9)) |p9| {
61396140 try p9.seeDecl(decl_index);
src/arch/x86_64/CodeGen.zig+8-6
......@@ -2673,7 +2673,7 @@ fn loadMemPtrIntoRegister(self: *Self, reg: Register, ptr_ty: Type, ptr: MCValue
26732673 const atom_index = if (self.bin_file.tag == link.File.MachO.base_tag)
26742674 fn_owner_decl.link.macho.getSymbolIndex().?
26752675 else
2676 fn_owner_decl.link.coff.sym_index;
2676 fn_owner_decl.link.coff.getSymbolIndex().?;
26772677 const flags: u2 = switch (load_struct.type) {
26782678 .got => 0b00,
26792679 .direct => 0b01,
......@@ -4005,11 +4005,13 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
40054005 .ops = Mir.Inst.Ops.encode(.{ .flags = 0b01 }),
40064006 .data = .{ .imm = got_addr },
40074007 });
4008 } else if (self.bin_file.cast(link.File.Coff)) |_| {
4008 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
4009 try fn_owner_decl.link.coff.ensureInitialized(coff_file);
4010 const sym_index = fn_owner_decl.link.coff.getSymbolIndex().?;
40094011 try self.genSetReg(Type.initTag(.usize), .rax, .{
40104012 .linker_load = .{
40114013 .type = .got,
4012 .sym_index = fn_owner_decl.link.coff.sym_index,
4014 .sym_index = sym_index,
40134015 },
40144016 });
40154017 _ = try self.addInst(.{
......@@ -6725,11 +6727,11 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
67256727 .type = .got,
67266728 .sym_index = decl.link.macho.getSymbolIndex().?,
67276729 } };
6728 } else if (self.bin_file.cast(link.File.Coff)) |_| {
6729 assert(decl.link.coff.sym_index != 0);
6730 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
6731 try decl.link.coff.ensureInitialized(coff_file);
67306732 return MCValue{ .linker_load = .{
67316733 .type = .got,
6732 .sym_index = decl.link.coff.sym_index,
6734 .sym_index = decl.link.coff.getSymbolIndex().?,
67336735 } };
67346736 } else if (self.bin_file.cast(link.File.Plan9)) |p9| {
67356737 try p9.seeDecl(decl_index);
src/link.zig+8-4
......@@ -615,12 +615,16 @@ pub const File = struct {
615615 return;
616616 }
617617 switch (base.tag) {
618 .coff => return @fieldParentPtr(Coff, "base", base).allocateDeclIndexes(decl_index),
619 .elf => {}, // no-op
620 .macho => {}, // no-op
621618 .wasm => return @fieldParentPtr(Wasm, "base", base).allocateDeclIndexes(decl_index),
622619 .plan9 => return @fieldParentPtr(Plan9, "base", base).allocateDeclIndexes(decl_index),
623 .c, .spirv, .nvptx => {},
620
621 .coff,
622 .elf,
623 .macho,
624 .c,
625 .spirv,
626 .nvptx,
627 => {},
624628 }
625629 }
626630
src/link/Coff.zig+79-73
......@@ -480,16 +480,6 @@ fn growSectionVM(self: *Coff, sect_id: u32, needed_size: u32) !void {
480480 header.virtual_size = increased_size;
481481}
482482
483pub fn allocateDeclIndexes(self: *Coff, decl_index: Module.Decl.Index) !void {
484 if (self.llvm_object) |_| return;
485 const decl = self.base.options.module.?.declPtr(decl_index);
486 if (decl.link.coff.sym_index != 0) return;
487 decl.link.coff.sym_index = try self.allocateSymbol();
488 const gpa = self.base.allocator;
489 try self.atom_by_index_table.putNoClobber(gpa, decl.link.coff.sym_index, &decl.link.coff);
490 try self.decls.putNoClobber(gpa, decl_index, null);
491}
492
493483fn allocateAtom(self: *Coff, atom: *Atom, new_atom_size: u32, alignment: u32) !u32 {
494484 const tracy = trace(@src());
495485 defer tracy.end();
......@@ -615,7 +605,7 @@ fn allocateAtom(self: *Coff, atom: *Atom, new_atom_size: u32, alignment: u32) !u
615605 return vaddr;
616606}
617607
618fn allocateSymbol(self: *Coff) !u32 {
608pub fn allocateSymbol(self: *Coff) !u32 {
619609 const gpa = self.base.allocator;
620610 try self.locals.ensureUnusedCapacity(gpa, 1);
621611
......@@ -716,12 +706,11 @@ fn createGotAtom(self: *Coff, target: SymbolWithLoc) !*Atom {
716706 const atom = try gpa.create(Atom);
717707 errdefer gpa.destroy(atom);
718708 atom.* = Atom.empty;
719 atom.sym_index = try self.allocateSymbol();
709 try atom.ensureInitialized(self);
720710 atom.size = @sizeOf(u64);
721711 atom.alignment = @alignOf(u64);
722712
723713 try self.managed_atoms.append(gpa, atom);
724 try self.atom_by_index_table.putNoClobber(gpa, atom.sym_index, atom);
725714
726715 const sym = atom.getSymbolPtr(self);
727716 sym.section_number = @intToEnum(coff.SectionNumber, self.got_section_index.? + 1);
......@@ -754,12 +743,11 @@ fn createImportAtom(self: *Coff) !*Atom {
754743 const atom = try gpa.create(Atom);
755744 errdefer gpa.destroy(atom);
756745 atom.* = Atom.empty;
757 atom.sym_index = try self.allocateSymbol();
746 try atom.ensureInitialized(self);
758747 atom.size = @sizeOf(u64);
759748 atom.alignment = @alignOf(u64);
760749
761750 try self.managed_atoms.append(gpa, atom);
762 try self.atom_by_index_table.putNoClobber(gpa, atom.sym_index, atom);
763751
764752 const sym = atom.getSymbolPtr(self);
765753 sym.section_number = @intToEnum(coff.SectionNumber, self.idata_section_index.? + 1);
......@@ -790,7 +778,11 @@ fn writeAtom(self: *Coff, atom: *Atom, code: []const u8) !void {
790778 const sym = atom.getSymbol(self);
791779 const section = self.sections.get(@enumToInt(sym.section_number) - 1);
792780 const file_offset = section.header.pointer_to_raw_data + sym.value - section.header.virtual_address;
793 log.debug("writing atom for symbol {s} at file offset 0x{x} to 0x{x}", .{ atom.getName(self), file_offset, file_offset + code.len });
781 log.debug("writing atom for symbol {s} at file offset 0x{x} to 0x{x}", .{
782 atom.getName(self),
783 file_offset,
784 file_offset + code.len,
785 });
794786 try self.base.file.?.pwriteAll(code, file_offset);
795787 try self.resolveRelocs(atom);
796788}
......@@ -848,6 +840,7 @@ fn freeAtom(self: *Coff, atom: *Atom) void {
848840 // Remove any relocs and base relocs associated with this Atom
849841 self.freeRelocationsForAtom(atom);
850842
843 const gpa = self.base.allocator;
851844 const sym = atom.getSymbol(self);
852845 const sect_id = @enumToInt(sym.section_number) - 1;
853846 const free_list = &self.sections.items(.free_list)[sect_id];
......@@ -885,7 +878,7 @@ fn freeAtom(self: *Coff, atom: *Atom) void {
885878 if (!already_have_free_list_node and prev.freeListEligible(self)) {
886879 // The free list is heuristics, it doesn't have to be perfect, so we can
887880 // ignore the OOM here.
888 free_list.append(self.base.allocator, prev) catch {};
881 free_list.append(gpa, prev) catch {};
889882 }
890883 } else {
891884 atom.prev = null;
......@@ -896,6 +889,28 @@ fn freeAtom(self: *Coff, atom: *Atom) void {
896889 } else {
897890 atom.next = null;
898891 }
892
893 // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.
894 const sym_index = atom.getSymbolIndex().?;
895 self.locals_free_list.append(gpa, sym_index) catch {};
896
897 // Try freeing GOT atom if this decl had one
898 const got_target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
899 if (self.got_entries_table.get(got_target)) |got_index| {
900 self.got_entries_free_list.append(gpa, @intCast(u32, got_index)) catch {};
901 self.got_entries.items[got_index] = .{
902 .target = .{ .sym_index = 0, .file = null },
903 .sym_index = 0,
904 };
905 _ = self.got_entries_table.remove(got_target);
906
907 log.debug(" adding GOT index {d} to free list (target local@{d})", .{ got_index, sym_index });
908 }
909
910 self.locals.items[sym_index].section_number = .UNDEFINED;
911 _ = self.atom_by_index_table.remove(sym_index);
912 log.debug(" adding local symbol index {d} to free list", .{sym_index});
913 atom.sym_index = 0;
899914}
900915
901916pub fn updateFunc(self: *Coff, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void {
......@@ -912,8 +927,15 @@ pub fn updateFunc(self: *Coff, module: *Module, func: *Module.Fn, air: Air, live
912927
913928 const decl_index = func.owner_decl;
914929 const decl = module.declPtr(decl_index);
915 self.freeUnnamedConsts(decl_index);
916 self.freeRelocationsForAtom(&decl.link.coff);
930 const atom = &decl.link.coff;
931 try atom.ensureInitialized(self);
932 const gop = try self.decls.getOrPut(self.base.allocator, decl_index);
933 if (gop.found_existing) {
934 self.freeUnnamedConsts(decl_index);
935 self.freeRelocationsForAtom(&decl.link.coff);
936 } else {
937 gop.value_ptr.* = null;
938 }
917939
918940 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
919941 defer code_buffer.deinit();
......@@ -960,9 +982,9 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
960982 const atom = try gpa.create(Atom);
961983 errdefer gpa.destroy(atom);
962984 atom.* = Atom.empty;
985 try atom.ensureInitialized(self);
986 try self.managed_atoms.append(gpa, atom);
963987
964 atom.sym_index = try self.allocateSymbol();
965 const sym = atom.getSymbolPtr(self);
966988 const sym_name = blk: {
967989 const decl_name = try decl.getFullyQualifiedName(mod);
968990 defer gpa.free(decl_name);
......@@ -971,14 +993,11 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
971993 break :blk try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });
972994 };
973995 defer gpa.free(sym_name);
974 try self.setSymbolName(sym, sym_name);
975 sym.section_number = @intToEnum(coff.SectionNumber, self.rdata_section_index.? + 1);
976
977 try self.managed_atoms.append(gpa, atom);
978 try self.atom_by_index_table.putNoClobber(gpa, atom.sym_index, atom);
996 try self.setSymbolName(atom.getSymbolPtr(self), sym_name);
997 atom.getSymbolPtr(self).section_number = @intToEnum(coff.SectionNumber, self.rdata_section_index.? + 1);
979998
980999 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), tv, &code_buffer, .none, .{
981 .parent_atom_index = atom.sym_index,
1000 .parent_atom_index = atom.getSymbolIndex().?,
9821001 });
9831002 const code = switch (res) {
9841003 .ok => code_buffer.items,
......@@ -993,17 +1012,17 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
9931012 const required_alignment = tv.ty.abiAlignment(self.base.options.target);
9941013 atom.alignment = required_alignment;
9951014 atom.size = @intCast(u32, code.len);
996 sym.value = try self.allocateAtom(atom, atom.size, atom.alignment);
1015 atom.getSymbolPtr(self).value = try self.allocateAtom(atom, atom.size, atom.alignment);
9971016 errdefer self.freeAtom(atom);
9981017
9991018 try unnamed_consts.append(gpa, atom);
10001019
1001 log.debug("allocated atom for {s} at 0x{x}", .{ sym_name, sym.value });
1020 log.debug("allocated atom for {s} at 0x{x}", .{ sym_name, atom.getSymbol(self).value });
10021021 log.debug(" (required alignment 0x{x})", .{required_alignment});
10031022
10041023 try self.writeAtom(atom, code);
10051024
1006 return atom.sym_index;
1025 return atom.getSymbolIndex().?;
10071026}
10081027
10091028pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !void {
......@@ -1028,7 +1047,14 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !
10281047 }
10291048 }
10301049
1031 self.freeRelocationsForAtom(&decl.link.coff);
1050 const atom = &decl.link.coff;
1051 try atom.ensureInitialized(self);
1052 const gop = try self.decls.getOrPut(self.base.allocator, decl_index);
1053 if (gop.found_existing) {
1054 self.freeRelocationsForAtom(atom);
1055 } else {
1056 gop.value_ptr.* = null;
1057 }
10321058
10331059 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
10341060 defer code_buffer.deinit();
......@@ -1038,7 +1064,7 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !
10381064 .ty = decl.ty,
10391065 .val = decl_val,
10401066 }, &code_buffer, .none, .{
1041 .parent_atom_index = decl.link.coff.sym_index,
1067 .parent_atom_index = decl.link.coff.getSymbolIndex().?,
10421068 });
10431069 const code = switch (res) {
10441070 .ok => code_buffer.items,
......@@ -1099,7 +1125,7 @@ fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8,
10991125
11001126 const code_len = @intCast(u32, code.len);
11011127 const atom = &decl.link.coff;
1102 assert(atom.sym_index != 0); // Caller forgot to allocateDeclIndexes()
1128
11031129 if (atom.size != 0) {
11041130 const sym = atom.getSymbolPtr(self);
11051131 try self.setSymbolName(sym, decl_name);
......@@ -1116,7 +1142,7 @@ fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8,
11161142 if (vaddr != sym.value) {
11171143 sym.value = vaddr;
11181144 log.debug(" (updating GOT entry)", .{});
1119 const got_target = SymbolWithLoc{ .sym_index = atom.sym_index, .file = null };
1145 const got_target = SymbolWithLoc{ .sym_index = atom.getSymbolIndex().?, .file = null };
11201146 const got_atom = self.getGotAtomForSymbol(got_target).?;
11211147 self.markRelocsDirtyByTarget(got_target);
11221148 try self.writePtrWidthAtom(got_atom);
......@@ -1137,10 +1163,10 @@ fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8,
11371163 atom.size = code_len;
11381164 sym.value = vaddr;
11391165
1140 const got_target = SymbolWithLoc{ .sym_index = atom.sym_index, .file = null };
1166 const got_target = SymbolWithLoc{ .sym_index = atom.getSymbolIndex().?, .file = null };
11411167 const got_index = try self.allocateGotEntry(got_target);
11421168 const got_atom = try self.createGotAtom(got_target);
1143 self.got_entries.items[got_index].sym_index = got_atom.sym_index;
1169 self.got_entries.items[got_index].sym_index = got_atom.getSymbolIndex().?;
11441170 try self.writePtrWidthAtom(got_atom);
11451171 }
11461172
......@@ -1160,11 +1186,6 @@ fn freeUnnamedConsts(self: *Coff, decl_index: Module.Decl.Index) void {
11601186 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;
11611187 for (unnamed_consts.items) |atom| {
11621188 self.freeAtom(atom);
1163 self.locals_free_list.append(gpa, atom.sym_index) catch {};
1164 self.locals.items[atom.sym_index].section_number = .UNDEFINED;
1165 _ = self.atom_by_index_table.remove(atom.sym_index);
1166 log.debug(" adding local symbol index {d} to free list", .{atom.sym_index});
1167 atom.sym_index = 0;
11681189 }
11691190 unnamed_consts.clearAndFree(gpa);
11701191}
......@@ -1179,35 +1200,11 @@ pub fn freeDecl(self: *Coff, decl_index: Module.Decl.Index) void {
11791200
11801201 log.debug("freeDecl {*}", .{decl});
11811202
1182 const kv = self.decls.fetchRemove(decl_index);
1183 if (kv.?.value) |_| {
1184 self.freeAtom(&decl.link.coff);
1185 self.freeUnnamedConsts(decl_index);
1186 }
1187
1188 // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.
1189 const gpa = self.base.allocator;
1190 const sym_index = decl.link.coff.sym_index;
1191 if (sym_index != 0) {
1192 self.locals_free_list.append(gpa, sym_index) catch {};
1193
1194 // Try freeing GOT atom if this decl had one
1195 const got_target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
1196 if (self.got_entries_table.get(got_target)) |got_index| {
1197 self.got_entries_free_list.append(gpa, @intCast(u32, got_index)) catch {};
1198 self.got_entries.items[got_index] = .{
1199 .target = .{ .sym_index = 0, .file = null },
1200 .sym_index = 0,
1201 };
1202 _ = self.got_entries_table.remove(got_target);
1203
1204 log.debug(" adding GOT index {d} to free list (target local@{d})", .{ got_index, sym_index });
1203 if (self.decls.fetchRemove(decl_index)) |kv| {
1204 if (kv.value) |_| {
1205 self.freeAtom(&decl.link.coff);
1206 self.freeUnnamedConsts(decl_index);
12051207 }
1206
1207 self.locals.items[sym_index].section_number = .UNDEFINED;
1208 _ = self.atom_by_index_table.remove(sym_index);
1209 log.debug(" adding local symbol index {d} to free list", .{sym_index});
1210 decl.link.coff.sym_index = 0;
12111208 }
12121209}
12131210
......@@ -1261,7 +1258,14 @@ pub fn updateDeclExports(
12611258
12621259 const decl = module.declPtr(decl_index);
12631260 const atom = &decl.link.coff;
1264 if (atom.sym_index == 0) return;
1261
1262 if (atom.getSymbolIndex() == null) return;
1263
1264 const gop = try self.decls.getOrPut(gpa, decl_index);
1265 if (!gop.found_existing) {
1266 gop.value_ptr.* = self.getDeclOutputSection(decl);
1267 }
1268
12651269 const decl_sym = atom.getSymbol(self);
12661270
12671271 for (exports) |exp| {
......@@ -1416,7 +1420,7 @@ pub fn flushModule(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
14161420
14171421 const import_index = try self.allocateImportEntry(global);
14181422 const import_atom = try self.createImportAtom();
1419 self.imports.items[import_index].sym_index = import_atom.sym_index;
1423 self.imports.items[import_index].sym_index = import_atom.getSymbolIndex().?;
14201424 try self.writePtrWidthAtom(import_atom);
14211425 }
14221426
......@@ -1460,10 +1464,12 @@ pub fn getDeclVAddr(
14601464 const decl = mod.declPtr(decl_index);
14611465
14621466 assert(self.llvm_object == null);
1463 assert(decl.link.coff.sym_index != 0);
1467
1468 try decl.link.coff.ensureInitialized(self);
1469 const sym_index = decl.link.coff.getSymbolIndex().?;
14641470
14651471 const atom = self.getAtomForSymbol(.{ .sym_index = reloc_info.parent_atom_index, .file = null }).?;
1466 const target = SymbolWithLoc{ .sym_index = decl.link.coff.sym_index, .file = null };
1472 const target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
14671473 try atom.addRelocation(self, .{
14681474 .type = .direct,
14691475 .target = target,
src/link/Coff/Atom.zig+19-4
......@@ -39,30 +39,45 @@ pub const empty = Atom{
3939 .next = null,
4040};
4141
42pub fn ensureInitialized(self: *Atom, coff_file: *Coff) !void {
43 if (self.getSymbolIndex() != null) return; // Already initialized
44 self.sym_index = try coff_file.allocateSymbol();
45 try coff_file.atom_by_index_table.putNoClobber(coff_file.base.allocator, self.sym_index, self);
46}
47
48pub fn getSymbolIndex(self: Atom) ?u32 {
49 if (self.sym_index == 0) return null;
50 return self.sym_index;
51}
52
4253/// Returns symbol referencing this atom.
4354pub fn getSymbol(self: Atom, coff_file: *const Coff) *const coff.Symbol {
55 const sym_index = self.getSymbolIndex().?;
4456 return coff_file.getSymbol(.{
45 .sym_index = self.sym_index,
57 .sym_index = sym_index,
4658 .file = self.file,
4759 });
4860}
4961
5062/// Returns pointer-to-symbol referencing this atom.
5163pub fn getSymbolPtr(self: Atom, coff_file: *Coff) *coff.Symbol {
64 const sym_index = self.getSymbolIndex().?;
5265 return coff_file.getSymbolPtr(.{
53 .sym_index = self.sym_index,
66 .sym_index = sym_index,
5467 .file = self.file,
5568 });
5669}
5770
5871pub fn getSymbolWithLoc(self: Atom) SymbolWithLoc {
59 return .{ .sym_index = self.sym_index, .file = self.file };
72 const sym_index = self.getSymbolIndex().?;
73 return .{ .sym_index = sym_index, .file = self.file };
6074}
6175
6276/// Returns the name of this atom.
6377pub fn getName(self: Atom, coff_file: *const Coff) []const u8 {
78 const sym_index = self.getSymbolIndex().?;
6479 return coff_file.getSymbolName(.{
65 .sym_index = self.sym_index,
80 .sym_index = sym_index,
6681 .file = self.file,
6782 });
6883}
src/link/Elf.zig+28-21
......@@ -893,7 +893,7 @@ fn growAllocSection(self: *Elf, shdr_index: u16, phdr_index: u16, needed_size: u
893893 // Must move the entire section.
894894 const new_offset = self.findFreeSpace(needed_size, self.page_size);
895895 const existing_size = if (self.atoms.get(phdr_index)) |last| blk: {
896 const sym = self.local_symbols.items[last.local_sym_index];
896 const sym = last.getSymbol(self);
897897 break :blk (sym.st_value + sym.st_size) - phdr.p_vaddr;
898898 } else if (shdr_index == self.got_section_index.?) blk: {
899899 break :blk shdr.sh_size;
......@@ -1031,7 +1031,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10311031 while (it.next()) |entry| {
10321032 const atom = entry.key_ptr.*;
10331033 const relocs = entry.value_ptr.*;
1034 const source_sym = self.local_symbols.items[atom.local_sym_index];
1034 const source_sym = atom.getSymbol(self);
10351035 const source_shdr = self.sections.items[source_sym.st_shndx];
10361036
10371037 log.debug("relocating '{s}'", .{self.getString(source_sym.st_name)});
......@@ -2034,11 +2034,13 @@ fn writeElfHeader(self: *Elf) !void {
20342034}
20352035
20362036fn freeTextBlock(self: *Elf, text_block: *TextBlock, phdr_index: u16) void {
2037 const local_sym = self.local_symbols.items[text_block.local_sym_index];
2037 const local_sym = text_block.getSymbol(self);
20382038 const name_str_index = local_sym.st_name;
20392039 const name = self.getString(name_str_index);
20402040 log.debug("freeTextBlock {*} ({s})", .{ text_block, name });
20412041
2042 self.freeRelocationsForTextBlock(text_block);
2043
20422044 const free_list = self.atom_free_lists.getPtr(phdr_index).?;
20432045 var already_have_free_list_node = false;
20442046 {
......@@ -2107,7 +2109,7 @@ fn shrinkTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, phdr
21072109}
21082110
21092111fn growTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, alignment: u64, phdr_index: u16) !u64 {
2110 const sym = self.local_symbols.items[text_block.local_sym_index];
2112 const sym = text_block.getSymbol(self);
21112113 const align_ok = mem.alignBackwardGeneric(u64, sym.st_value, alignment) == sym.st_value;
21122114 const need_realloc = !align_ok or new_block_size > text_block.capacity(self);
21132115 if (!need_realloc) return sym.st_value;
......@@ -2137,7 +2139,7 @@ fn allocateTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, al
21372139 const big_block = free_list.items[i];
21382140 // We now have a pointer to a live text block that has too much capacity.
21392141 // Is it enough that we could fit this new text block?
2140 const sym = self.local_symbols.items[big_block.local_sym_index];
2142 const sym = big_block.getSymbol(self);
21412143 const capacity = big_block.capacity(self);
21422144 const ideal_capacity = padToIdeal(capacity);
21432145 const ideal_capacity_end_vaddr = std.math.add(u64, sym.st_value, ideal_capacity) catch ideal_capacity;
......@@ -2168,7 +2170,7 @@ fn allocateTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, al
21682170 }
21692171 break :blk new_start_vaddr;
21702172 } else if (self.atoms.get(phdr_index)) |last| {
2171 const sym = self.local_symbols.items[last.local_sym_index];
2173 const sym = last.getSymbol(self);
21722174 const ideal_capacity = padToIdeal(sym.st_size);
21732175 const ideal_capacity_end_vaddr = sym.st_value + ideal_capacity;
21742176 const new_start_vaddr = mem.alignForwardGeneric(u64, ideal_capacity_end_vaddr, alignment);
......@@ -2270,6 +2272,11 @@ pub fn allocateGotOffset(self: *Elf) !u32 {
22702272 return index;
22712273}
22722274
2275fn freeRelocationsForTextBlock(self: *Elf, text_block: *TextBlock) void {
2276 var removed_relocs = self.relocs.fetchRemove(text_block);
2277 if (removed_relocs) |*relocs| relocs.value.deinit(self.base.allocator);
2278}
2279
22732280fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void {
22742281 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;
22752282 for (unnamed_consts.items) |atom| {
......@@ -2341,8 +2348,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
23412348 const phdr_index = decl_ptr.*.?;
23422349 const shdr_index = self.phdr_shdr_table.get(phdr_index).?;
23432350
2344 assert(decl.link.elf.local_sym_index != 0); // Caller forgot to allocateDeclIndexes()
2345 const local_sym = &self.local_symbols.items[decl.link.elf.local_sym_index];
2351 const local_sym = decl.link.elf.getSymbolPtr(self);
23462352 if (local_sym.st_size != 0) {
23472353 const capacity = decl.link.elf.capacity(self);
23482354 const need_realloc = code.len > capacity or
......@@ -2366,7 +2372,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
23662372 local_sym.st_other = 0;
23672373 local_sym.st_shndx = shdr_index;
23682374 // TODO this write could be avoided if no fields of the symbol were changed.
2369 try self.writeSymbol(decl.link.elf.local_sym_index);
2375 try self.writeSymbol(decl.link.elf.getSymbolIndex().?);
23702376 } else {
23712377 const name_str_index = try self.makeString(decl_name);
23722378 const vaddr = try self.allocateTextBlock(&decl.link.elf, code.len, required_alignment, phdr_index);
......@@ -2383,7 +2389,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
23832389 };
23842390 self.offset_table.items[decl.link.elf.offset_table_index] = vaddr;
23852391
2386 try self.writeSymbol(decl.link.elf.local_sym_index);
2392 try self.writeSymbol(decl.link.elf.getSymbolIndex().?);
23872393 try self.writeOffsetTableEntry(decl.link.elf.offset_table_index);
23882394 }
23892395
......@@ -2412,6 +2418,7 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven
24122418 const gop = try self.decls.getOrPut(self.base.allocator, decl_index);
24132419 if (gop.found_existing) {
24142420 self.freeUnnamedConsts(decl_index);
2421 self.freeRelocationsForTextBlock(atom);
24152422 } else {
24162423 gop.value_ptr.* = null;
24172424 }
......@@ -2481,7 +2488,9 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v
24812488 const atom = &decl.link.elf;
24822489 try atom.ensureInitialized(self);
24832490 const gop = try self.decls.getOrPut(self.base.allocator, decl_index);
2484 if (!gop.found_existing) {
2491 if (gop.found_existing) {
2492 self.freeRelocationsForTextBlock(atom);
2493 } else {
24852494 gop.value_ptr.* = null;
24862495 }
24872496
......@@ -2500,14 +2509,14 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v
25002509 }, &code_buffer, .{
25012510 .dwarf = ds,
25022511 }, .{
2503 .parent_atom_index = decl.link.elf.local_sym_index,
2512 .parent_atom_index = decl.link.elf.getSymbolIndex().?,
25042513 })
25052514 else
25062515 try codegen.generateSymbol(&self.base, decl.srcLoc(), .{
25072516 .ty = decl.ty,
25082517 .val = decl_val,
25092518 }, &code_buffer, .none, .{
2510 .parent_atom_index = decl.link.elf.local_sym_index,
2519 .parent_atom_index = decl.link.elf.getSymbolIndex().?,
25112520 });
25122521
25132522 const code = switch (res) {
......@@ -2551,6 +2560,8 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
25512560 const atom = try self.base.allocator.create(TextBlock);
25522561 errdefer self.base.allocator.destroy(atom);
25532562 atom.* = TextBlock.empty;
2563 // TODO for unnamed consts we don't need GOT offset/entry allocated
2564 try atom.ensureInitialized(self);
25542565 try self.managed_atoms.append(self.base.allocator, atom);
25552566
25562567 const name_str_index = blk: {
......@@ -2565,14 +2576,10 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
25652576 };
25662577 const name = self.getString(name_str_index);
25672578
2568 log.debug("allocating symbol indexes for {s}", .{name});
2569 atom.local_sym_index = try self.allocateLocalSymbol();
2570 try self.atom_by_index_table.putNoClobber(self.base.allocator, atom.local_sym_index, atom);
2571
25722579 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .{
25732580 .none = {},
25742581 }, .{
2575 .parent_atom_index = atom.local_sym_index,
2582 .parent_atom_index = atom.getSymbolIndex().?,
25762583 });
25772584 const code = switch (res) {
25782585 .ok => code_buffer.items,
......@@ -2592,7 +2599,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
25922599
25932600 log.debug("allocated text block for {s} at 0x{x}", .{ name, vaddr });
25942601
2595 const local_sym = &self.local_symbols.items[atom.local_sym_index];
2602 const local_sym = atom.getSymbolPtr(self);
25962603 local_sym.* = .{
25972604 .st_name = name_str_index,
25982605 .st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT,
......@@ -2602,14 +2609,14 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
26022609 .st_size = code.len,
26032610 };
26042611
2605 try self.writeSymbol(atom.local_sym_index);
2612 try self.writeSymbol(atom.getSymbolIndex().?);
26062613 try unnamed_consts.append(self.base.allocator, atom);
26072614
26082615 const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr;
26092616 const file_offset = self.sections.items[shdr_index].sh_offset + section_offset;
26102617 try self.base.file.?.pwriteAll(code, file_offset);
26112618
2612 return atom.local_sym_index;
2619 return atom.getSymbolIndex().?;
26132620}
26142621
26152622pub fn updateDeclExports(
src/link/MachO.zig+51-84
......@@ -1056,19 +1056,14 @@ pub fn allocateSpecialSymbols(self: *MachO) !void {
10561056pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
10571057 const gpa = self.base.allocator;
10581058
1059 const sym_index = try self.allocateSymbol();
1060 const atom = blk: {
1061 const atom = try gpa.create(Atom);
1062 atom.* = Atom.empty;
1063 atom.sym_index = sym_index;
1064 atom.size = @sizeOf(u64);
1065 atom.alignment = @alignOf(u64);
1066 break :blk atom;
1067 };
1059 const atom = try gpa.create(Atom);
1060 atom.* = Atom.empty;
1061 try atom.ensureInitialized(self);
1062 atom.size = @sizeOf(u64);
1063 atom.alignment = @alignOf(u64);
10681064 errdefer gpa.destroy(atom);
10691065
10701066 try self.managed_atoms.append(gpa, atom);
1071 try self.atom_by_index_table.putNoClobber(gpa, atom.sym_index, atom);
10721067
10731068 const sym = atom.getSymbolPtr(self);
10741069 sym.n_type = macho.N_SECT;
......@@ -1109,15 +1104,11 @@ pub fn createDyldPrivateAtom(self: *MachO) !void {
11091104
11101105 const gpa = self.base.allocator;
11111106
1112 const sym_index = try self.allocateSymbol();
1113 const atom = blk: {
1114 const atom = try gpa.create(Atom);
1115 atom.* = Atom.empty;
1116 atom.sym_index = sym_index;
1117 atom.size = @sizeOf(u64);
1118 atom.alignment = @alignOf(u64);
1119 break :blk atom;
1120 };
1107 const atom = try gpa.create(Atom);
1108 atom.* = Atom.empty;
1109 try atom.ensureInitialized(self);
1110 atom.size = @sizeOf(u64);
1111 atom.alignment = @alignOf(u64);
11211112 errdefer gpa.destroy(atom);
11221113
11231114 const sym = atom.getSymbolPtr(self);
......@@ -1126,7 +1117,6 @@ pub fn createDyldPrivateAtom(self: *MachO) !void {
11261117 self.dyld_private_atom = atom;
11271118
11281119 try self.managed_atoms.append(gpa, atom);
1129 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
11301120
11311121 sym.n_value = try self.allocateAtom(atom, atom.size, @alignOf(u64));
11321122 log.debug("allocated dyld_private atom at 0x{x}", .{sym.n_value});
......@@ -1144,18 +1134,14 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {
11441134 .aarch64 => 6 * @sizeOf(u32),
11451135 else => unreachable,
11461136 };
1147 const sym_index = try self.allocateSymbol();
1148 const atom = blk: {
1149 const atom = try gpa.create(Atom);
1150 atom.* = Atom.empty;
1151 atom.sym_index = sym_index;
1152 atom.size = size;
1153 atom.alignment = switch (arch) {
1154 .x86_64 => 1,
1155 .aarch64 => @alignOf(u32),
1156 else => unreachable,
1157 };
1158 break :blk atom;
1137 const atom = try gpa.create(Atom);
1138 atom.* = Atom.empty;
1139 try atom.ensureInitialized(self);
1140 atom.size = size;
1141 atom.alignment = switch (arch) {
1142 .x86_64 => 1,
1143 .aarch64 => @alignOf(u32),
1144 else => unreachable,
11591145 };
11601146 errdefer gpa.destroy(atom);
11611147
......@@ -1163,7 +1149,7 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {
11631149 sym.n_type = macho.N_SECT;
11641150 sym.n_sect = self.stub_helper_section_index.? + 1;
11651151
1166 const dyld_private_sym_index = self.dyld_private_atom.?.sym_index;
1152 const dyld_private_sym_index = self.dyld_private_atom.?.getSymbolIndex().?;
11671153
11681154 const code = try gpa.alloc(u8, size);
11691155 defer gpa.free(code);
......@@ -1258,7 +1244,6 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {
12581244 self.stub_helper_preamble_atom = atom;
12591245
12601246 try self.managed_atoms.append(gpa, atom);
1261 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
12621247
12631248 sym.n_value = try self.allocateAtom(atom, size, atom.alignment);
12641249 log.debug("allocated stub preamble atom at 0x{x}", .{sym.n_value});
......@@ -1273,18 +1258,14 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
12731258 .aarch64 => 3 * @sizeOf(u32),
12741259 else => unreachable,
12751260 };
1276 const sym_index = try self.allocateSymbol();
1277 const atom = blk: {
1278 const atom = try gpa.create(Atom);
1279 atom.* = Atom.empty;
1280 atom.sym_index = sym_index;
1281 atom.size = size;
1282 atom.alignment = switch (arch) {
1283 .x86_64 => 1,
1284 .aarch64 => @alignOf(u32),
1285 else => unreachable,
1286 };
1287 break :blk atom;
1261 const atom = try gpa.create(Atom);
1262 atom.* = Atom.empty;
1263 try atom.ensureInitialized(self);
1264 atom.size = size;
1265 atom.alignment = switch (arch) {
1266 .x86_64 => 1,
1267 .aarch64 => @alignOf(u32),
1268 else => unreachable,
12881269 };
12891270 errdefer gpa.destroy(atom);
12901271
......@@ -1306,7 +1287,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
13061287
13071288 try atom.addRelocation(self, .{
13081289 .type = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_BRANCH),
1309 .target = .{ .sym_index = self.stub_helper_preamble_atom.?.sym_index, .file = null },
1290 .target = .{ .sym_index = self.stub_helper_preamble_atom.?.getSymbolIndex().?, .file = null },
13101291 .offset = 6,
13111292 .addend = 0,
13121293 .pcrel = true,
......@@ -1329,7 +1310,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
13291310
13301311 try atom.addRelocation(self, .{
13311312 .type = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_BRANCH26),
1332 .target = .{ .sym_index = self.stub_helper_preamble_atom.?.sym_index, .file = null },
1313 .target = .{ .sym_index = self.stub_helper_preamble_atom.?.getSymbolIndex().?, .file = null },
13331314 .offset = 4,
13341315 .addend = 0,
13351316 .pcrel = true,
......@@ -1340,7 +1321,6 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
13401321 }
13411322
13421323 try self.managed_atoms.append(gpa, atom);
1343 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
13441324
13451325 sym.n_value = try self.allocateAtom(atom, size, atom.alignment);
13461326 log.debug("allocated stub helper atom at 0x{x}", .{sym.n_value});
......@@ -1351,15 +1331,11 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
13511331
13521332pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWithLoc) !*Atom {
13531333 const gpa = self.base.allocator;
1354 const sym_index = try self.allocateSymbol();
1355 const atom = blk: {
1356 const atom = try gpa.create(Atom);
1357 atom.* = Atom.empty;
1358 atom.sym_index = sym_index;
1359 atom.size = @sizeOf(u64);
1360 atom.alignment = @alignOf(u64);
1361 break :blk atom;
1362 };
1334 const atom = try gpa.create(Atom);
1335 atom.* = Atom.empty;
1336 try atom.ensureInitialized(self);
1337 atom.size = @sizeOf(u64);
1338 atom.alignment = @alignOf(u64);
13631339 errdefer gpa.destroy(atom);
13641340
13651341 const sym = atom.getSymbolPtr(self);
......@@ -1385,7 +1361,6 @@ pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWi
13851361 });
13861362
13871363 try self.managed_atoms.append(gpa, atom);
1388 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
13891364
13901365 sym.n_value = try self.allocateAtom(atom, atom.size, @alignOf(u64));
13911366 log.debug("allocated lazy pointer atom at 0x{x} ({s})", .{ sym.n_value, self.getSymbolName(target) });
......@@ -1402,19 +1377,15 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
14021377 .aarch64 => 3 * @sizeOf(u32),
14031378 else => unreachable, // unhandled architecture type
14041379 };
1405 const sym_index = try self.allocateSymbol();
1406 const atom = blk: {
1407 const atom = try gpa.create(Atom);
1408 atom.* = Atom.empty;
1409 atom.sym_index = sym_index;
1410 atom.size = size;
1411 atom.alignment = switch (arch) {
1412 .x86_64 => 1,
1413 .aarch64 => @alignOf(u32),
1414 else => unreachable, // unhandled architecture type
1380 const atom = try gpa.create(Atom);
1381 atom.* = Atom.empty;
1382 try atom.ensureInitialized(self);
1383 atom.size = size;
1384 atom.alignment = switch (arch) {
1385 .x86_64 => 1,
1386 .aarch64 => @alignOf(u32),
1387 else => unreachable, // unhandled architecture type
14151388
1416 };
1417 break :blk atom;
14181389 };
14191390 errdefer gpa.destroy(atom);
14201391
......@@ -1476,7 +1447,6 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
14761447 }
14771448
14781449 try self.managed_atoms.append(gpa, atom);
1479 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
14801450
14811451 sym.n_value = try self.allocateAtom(atom, size, atom.alignment);
14821452 log.debug("allocated stub atom at 0x{x}", .{sym.n_value});
......@@ -1617,9 +1587,9 @@ pub fn resolveSymbolsInDylibs(self: *MachO) !void {
16171587
16181588 const stub_index = try self.allocateStubEntry(global);
16191589 const stub_helper_atom = try self.createStubHelperAtom();
1620 const laptr_atom = try self.createLazyPointerAtom(stub_helper_atom.sym_index, global);
1621 const stub_atom = try self.createStubAtom(laptr_atom.sym_index);
1622 self.stubs.items[stub_index].sym_index = stub_atom.sym_index;
1590 const laptr_atom = try self.createLazyPointerAtom(stub_helper_atom.getSymbolIndex().?, global);
1591 const stub_atom = try self.createStubAtom(laptr_atom.getSymbolIndex().?);
1592 self.stubs.items[stub_index].sym_index = stub_atom.getSymbolIndex().?;
16231593 self.markRelocsDirtyByTarget(global);
16241594 }
16251595
......@@ -1717,7 +1687,7 @@ pub fn resolveDyldStubBinder(self: *MachO) !void {
17171687 // Add dyld_stub_binder as the final GOT entry.
17181688 const got_index = try self.allocateGotEntry(global);
17191689 const got_atom = try self.createGotAtom(global);
1720 self.got_entries.items[got_index].sym_index = got_atom.sym_index;
1690 self.got_entries.items[got_index].sym_index = got_atom.getSymbolIndex().?;
17211691
17221692 try self.writePtrWidthAtom(got_atom);
17231693}
......@@ -2098,14 +2068,11 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
20982068 const atom = try gpa.create(Atom);
20992069 errdefer gpa.destroy(atom);
21002070 atom.* = Atom.empty;
2101
2102 atom.sym_index = try self.allocateSymbol();
2103
2071 try atom.ensureInitialized(self);
21042072 try self.managed_atoms.append(gpa, atom);
2105 try self.atom_by_index_table.putNoClobber(gpa, atom.sym_index, atom);
21062073
21072074 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .none, .{
2108 .parent_atom_index = atom.sym_index,
2075 .parent_atom_index = atom.getSymbolIndex().?,
21092076 });
21102077 const code = switch (res) {
21112078 .ok => code_buffer.items,
......@@ -2137,7 +2104,7 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
21372104
21382105 try self.writeAtom(atom, code);
21392106
2140 return atom.sym_index;
2107 return atom.getSymbolIndex().?;
21412108}
21422109
21432110pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index) !void {
......@@ -2188,14 +2155,14 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
21882155 }, &code_buffer, .{
21892156 .dwarf = ds,
21902157 }, .{
2191 .parent_atom_index = decl.link.macho.sym_index,
2158 .parent_atom_index = decl.link.macho.getSymbolIndex().?,
21922159 })
21932160 else
21942161 try codegen.generateSymbol(&self.base, decl.srcLoc(), .{
21952162 .ty = decl.ty,
21962163 .val = decl_val,
21972164 }, &code_buffer, .none, .{
2198 .parent_atom_index = decl.link.macho.sym_index,
2165 .parent_atom_index = decl.link.macho.getSymbolIndex().?,
21992166 });
22002167
22012168 const code = switch (res) {