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 {...@@ -5324,7 +5324,12 @@ pub fn deleteUnusedDecl(mod: *Module, decl_index: Decl.Index) void {
5324 // Until then, we did call `allocateDeclIndexes` on this anonymous Decl and so we5324 // Until then, we did call `allocateDeclIndexes` on this anonymous Decl and so we
5325 // must call `freeDecl` in the linker backend now.5325 // must call `freeDecl` in the linker backend now.
5326 switch (mod.comp.bin_file.tag) {5326 switch (mod.comp.bin_file.tag) {
5327 .elf, .macho, .c => {}, // this linker backend has already migrated to the new API5327 .coff,
5328 .elf,
5329 .macho,
5330 .c,
5331 => {}, // this linker backend has already migrated to the new API
5332
5328 else => if (decl.has_tv) {5333 else => if (decl.has_tv) {
5329 if (decl.ty.isFnOrHasRuntimeBits()) {5334 if (decl.ty.isFnOrHasRuntimeBits()) {
5330 mod.comp.bin_file.freeDecl(decl_index);5335 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...@@ -4000,7 +4000,7 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
4000 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);4000 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
4001 const atom_index = switch (self.bin_file.tag) {4001 const atom_index = switch (self.bin_file.tag) {
4002 .macho => owner_decl.link.macho.getSymbolIndex().?,4002 .macho => owner_decl.link.macho.getSymbolIndex().?,
4003 .coff => owner_decl.link.coff.sym_index,4003 .coff => owner_decl.link.coff.getSymbolIndex().?,
4004 else => unreachable, // unsupported target format4004 else => unreachable, // unsupported target format
4005 };4005 };
4006 _ = try self.addInst(.{4006 _ = try self.addInst(.{
...@@ -4318,11 +4318,12 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier...@@ -4318,11 +4318,12 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
4318 .sym_index = fn_owner_decl.link.macho.getSymbolIndex().?,4318 .sym_index = fn_owner_decl.link.macho.getSymbolIndex().?,
4319 },4319 },
4320 });4320 });
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);
4322 try self.genSetReg(Type.initTag(.u64), .x30, .{4323 try self.genSetReg(Type.initTag(.u64), .x30, .{
4323 .linker_load = .{4324 .linker_load = .{
4324 .type = .got,4325 .type = .got,
4325 .sym_index = fn_owner_decl.link.coff.sym_index,4326 .sym_index = fn_owner_decl.link.coff.getSymbolIndex().?,
4326 },4327 },
4327 });4328 });
4328 } else if (self.bin_file.cast(link.File.Plan9)) |p9| {4329 } 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...@@ -5494,7 +5495,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
5494 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);5495 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
5495 const atom_index = switch (self.bin_file.tag) {5496 const atom_index = switch (self.bin_file.tag) {
5496 .macho => owner_decl.link.macho.getSymbolIndex().?,5497 .macho => owner_decl.link.macho.getSymbolIndex().?,
5497 .coff => owner_decl.link.coff.sym_index,5498 .coff => owner_decl.link.coff.getSymbolIndex().?,
5498 else => unreachable, // unsupported target format5499 else => unreachable, // unsupported target format
5499 };5500 };
5500 _ = try self.addInst(.{5501 _ = try self.addInst(.{
...@@ -5608,7 +5609,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void...@@ -5608,7 +5609,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
5608 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);5609 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
5609 const atom_index = switch (self.bin_file.tag) {5610 const atom_index = switch (self.bin_file.tag) {
5610 .macho => owner_decl.link.macho.getSymbolIndex().?,5611 .macho => owner_decl.link.macho.getSymbolIndex().?,
5611 .coff => owner_decl.link.coff.sym_index,5612 .coff => owner_decl.link.coff.getSymbolIndex().?,
5612 else => unreachable, // unsupported target format5613 else => unreachable, // unsupported target format
5613 };5614 };
5614 _ = try self.addInst(.{5615 _ = try self.addInst(.{
...@@ -5802,7 +5803,7 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I...@@ -5802,7 +5803,7 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I
5802 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);5803 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
5803 const atom_index = switch (self.bin_file.tag) {5804 const atom_index = switch (self.bin_file.tag) {
5804 .macho => owner_decl.link.macho.getSymbolIndex().?,5805 .macho => owner_decl.link.macho.getSymbolIndex().?,
5805 .coff => owner_decl.link.coff.sym_index,5806 .coff => owner_decl.link.coff.getSymbolIndex().?,
5806 else => unreachable, // unsupported target format5807 else => unreachable, // unsupported target format
5807 };5808 };
5808 _ = try self.addInst(.{5809 _ = try self.addInst(.{
...@@ -6129,11 +6130,11 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne...@@ -6129,11 +6130,11 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
6129 .type = .got,6130 .type = .got,
6130 .sym_index = decl.link.macho.getSymbolIndex().?,6131 .sym_index = decl.link.macho.getSymbolIndex().?,
6131 } };6132 } };
6132 } else if (self.bin_file.cast(link.File.Coff)) |_| {6133 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
6133 assert(decl.link.coff.sym_index != 0);6134 try decl.link.coff.ensureInitialized(coff_file);
6134 return MCValue{ .linker_load = .{6135 return MCValue{ .linker_load = .{
6135 .type = .got,6136 .type = .got,
6136 .sym_index = decl.link.coff.sym_index,6137 .sym_index = decl.link.coff.getSymbolIndex().?,
6137 } };6138 } };
6138 } else if (self.bin_file.cast(link.File.Plan9)) |p9| {6139 } else if (self.bin_file.cast(link.File.Plan9)) |p9| {
6139 try p9.seeDecl(decl_index);6140 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...@@ -2673,7 +2673,7 @@ fn loadMemPtrIntoRegister(self: *Self, reg: Register, ptr_ty: Type, ptr: MCValue
2673 const atom_index = if (self.bin_file.tag == link.File.MachO.base_tag)2673 const atom_index = if (self.bin_file.tag == link.File.MachO.base_tag)
2674 fn_owner_decl.link.macho.getSymbolIndex().?2674 fn_owner_decl.link.macho.getSymbolIndex().?
2675 else2675 else
2676 fn_owner_decl.link.coff.sym_index;2676 fn_owner_decl.link.coff.getSymbolIndex().?;
2677 const flags: u2 = switch (load_struct.type) {2677 const flags: u2 = switch (load_struct.type) {
2678 .got => 0b00,2678 .got => 0b00,
2679 .direct => 0b01,2679 .direct => 0b01,
...@@ -4005,11 +4005,13 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier...@@ -4005,11 +4005,13 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
4005 .ops = Mir.Inst.Ops.encode(.{ .flags = 0b01 }),4005 .ops = Mir.Inst.Ops.encode(.{ .flags = 0b01 }),
4006 .data = .{ .imm = got_addr },4006 .data = .{ .imm = got_addr },
4007 });4007 });
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().?;
4009 try self.genSetReg(Type.initTag(.usize), .rax, .{4011 try self.genSetReg(Type.initTag(.usize), .rax, .{
4010 .linker_load = .{4012 .linker_load = .{
4011 .type = .got,4013 .type = .got,
4012 .sym_index = fn_owner_decl.link.coff.sym_index,4014 .sym_index = sym_index,
4013 },4015 },
4014 });4016 });
4015 _ = try self.addInst(.{4017 _ = try self.addInst(.{
...@@ -6725,11 +6727,11 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne...@@ -6725,11 +6727,11 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
6725 .type = .got,6727 .type = .got,
6726 .sym_index = decl.link.macho.getSymbolIndex().?,6728 .sym_index = decl.link.macho.getSymbolIndex().?,
6727 } };6729 } };
6728 } else if (self.bin_file.cast(link.File.Coff)) |_| {6730 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
6729 assert(decl.link.coff.sym_index != 0);6731 try decl.link.coff.ensureInitialized(coff_file);
6730 return MCValue{ .linker_load = .{6732 return MCValue{ .linker_load = .{
6731 .type = .got,6733 .type = .got,
6732 .sym_index = decl.link.coff.sym_index,6734 .sym_index = decl.link.coff.getSymbolIndex().?,
6733 } };6735 } };
6734 } else if (self.bin_file.cast(link.File.Plan9)) |p9| {6736 } else if (self.bin_file.cast(link.File.Plan9)) |p9| {
6735 try p9.seeDecl(decl_index);6737 try p9.seeDecl(decl_index);
src/link.zig+8-4
...@@ -615,12 +615,16 @@ pub const File = struct {...@@ -615,12 +615,16 @@ pub const File = struct {
615 return;615 return;
616 }616 }
617 switch (base.tag) {617 switch (base.tag) {
618 .coff => return @fieldParentPtr(Coff, "base", base).allocateDeclIndexes(decl_index),
619 .elf => {}, // no-op
620 .macho => {}, // no-op
621 .wasm => return @fieldParentPtr(Wasm, "base", base).allocateDeclIndexes(decl_index),618 .wasm => return @fieldParentPtr(Wasm, "base", base).allocateDeclIndexes(decl_index),
622 .plan9 => return @fieldParentPtr(Plan9, "base", base).allocateDeclIndexes(decl_index),619 .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 => {},
624 }628 }
625 }629 }
626630
src/link/Coff.zig+79-73
...@@ -480,16 +480,6 @@ fn growSectionVM(self: *Coff, sect_id: u32, needed_size: u32) !void {...@@ -480,16 +480,6 @@ fn growSectionVM(self: *Coff, sect_id: u32, needed_size: u32) !void {
480 header.virtual_size = increased_size;480 header.virtual_size = increased_size;
481}481}
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
493fn allocateAtom(self: *Coff, atom: *Atom, new_atom_size: u32, alignment: u32) !u32 {483fn allocateAtom(self: *Coff, atom: *Atom, new_atom_size: u32, alignment: u32) !u32 {
494 const tracy = trace(@src());484 const tracy = trace(@src());
495 defer tracy.end();485 defer tracy.end();
...@@ -615,7 +605,7 @@ fn allocateAtom(self: *Coff, atom: *Atom, new_atom_size: u32, alignment: u32) !u...@@ -615,7 +605,7 @@ fn allocateAtom(self: *Coff, atom: *Atom, new_atom_size: u32, alignment: u32) !u
615 return vaddr;605 return vaddr;
616}606}
617607
618fn allocateSymbol(self: *Coff) !u32 {608pub fn allocateSymbol(self: *Coff) !u32 {
619 const gpa = self.base.allocator;609 const gpa = self.base.allocator;
620 try self.locals.ensureUnusedCapacity(gpa, 1);610 try self.locals.ensureUnusedCapacity(gpa, 1);
621611
...@@ -716,12 +706,11 @@ fn createGotAtom(self: *Coff, target: SymbolWithLoc) !*Atom {...@@ -716,12 +706,11 @@ fn createGotAtom(self: *Coff, target: SymbolWithLoc) !*Atom {
716 const atom = try gpa.create(Atom);706 const atom = try gpa.create(Atom);
717 errdefer gpa.destroy(atom);707 errdefer gpa.destroy(atom);
718 atom.* = Atom.empty;708 atom.* = Atom.empty;
719 atom.sym_index = try self.allocateSymbol();709 try atom.ensureInitialized(self);
720 atom.size = @sizeOf(u64);710 atom.size = @sizeOf(u64);
721 atom.alignment = @alignOf(u64);711 atom.alignment = @alignOf(u64);
722712
723 try self.managed_atoms.append(gpa, atom);713 try self.managed_atoms.append(gpa, atom);
724 try self.atom_by_index_table.putNoClobber(gpa, atom.sym_index, atom);
725714
726 const sym = atom.getSymbolPtr(self);715 const sym = atom.getSymbolPtr(self);
727 sym.section_number = @intToEnum(coff.SectionNumber, self.got_section_index.? + 1);716 sym.section_number = @intToEnum(coff.SectionNumber, self.got_section_index.? + 1);
...@@ -754,12 +743,11 @@ fn createImportAtom(self: *Coff) !*Atom {...@@ -754,12 +743,11 @@ fn createImportAtom(self: *Coff) !*Atom {
754 const atom = try gpa.create(Atom);743 const atom = try gpa.create(Atom);
755 errdefer gpa.destroy(atom);744 errdefer gpa.destroy(atom);
756 atom.* = Atom.empty;745 atom.* = Atom.empty;
757 atom.sym_index = try self.allocateSymbol();746 try atom.ensureInitialized(self);
758 atom.size = @sizeOf(u64);747 atom.size = @sizeOf(u64);
759 atom.alignment = @alignOf(u64);748 atom.alignment = @alignOf(u64);
760749
761 try self.managed_atoms.append(gpa, atom);750 try self.managed_atoms.append(gpa, atom);
762 try self.atom_by_index_table.putNoClobber(gpa, atom.sym_index, atom);
763751
764 const sym = atom.getSymbolPtr(self);752 const sym = atom.getSymbolPtr(self);
765 sym.section_number = @intToEnum(coff.SectionNumber, self.idata_section_index.? + 1);753 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 {...@@ -790,7 +778,11 @@ fn writeAtom(self: *Coff, atom: *Atom, code: []const u8) !void {
790 const sym = atom.getSymbol(self);778 const sym = atom.getSymbol(self);
791 const section = self.sections.get(@enumToInt(sym.section_number) - 1);779 const section = self.sections.get(@enumToInt(sym.section_number) - 1);
792 const file_offset = section.header.pointer_to_raw_data + sym.value - section.header.virtual_address;780 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 });
794 try self.base.file.?.pwriteAll(code, file_offset);786 try self.base.file.?.pwriteAll(code, file_offset);
795 try self.resolveRelocs(atom);787 try self.resolveRelocs(atom);
796}788}
...@@ -848,6 +840,7 @@ fn freeAtom(self: *Coff, atom: *Atom) void {...@@ -848,6 +840,7 @@ fn freeAtom(self: *Coff, atom: *Atom) void {
848 // Remove any relocs and base relocs associated with this Atom840 // Remove any relocs and base relocs associated with this Atom
849 self.freeRelocationsForAtom(atom);841 self.freeRelocationsForAtom(atom);
850842
843 const gpa = self.base.allocator;
851 const sym = atom.getSymbol(self);844 const sym = atom.getSymbol(self);
852 const sect_id = @enumToInt(sym.section_number) - 1;845 const sect_id = @enumToInt(sym.section_number) - 1;
853 const free_list = &self.sections.items(.free_list)[sect_id];846 const free_list = &self.sections.items(.free_list)[sect_id];
...@@ -885,7 +878,7 @@ fn freeAtom(self: *Coff, atom: *Atom) void {...@@ -885,7 +878,7 @@ fn freeAtom(self: *Coff, atom: *Atom) void {
885 if (!already_have_free_list_node and prev.freeListEligible(self)) {878 if (!already_have_free_list_node and prev.freeListEligible(self)) {
886 // The free list is heuristics, it doesn't have to be perfect, so we can879 // The free list is heuristics, it doesn't have to be perfect, so we can
887 // ignore the OOM here.880 // ignore the OOM here.
888 free_list.append(self.base.allocator, prev) catch {};881 free_list.append(gpa, prev) catch {};
889 }882 }
890 } else {883 } else {
891 atom.prev = null;884 atom.prev = null;
...@@ -896,6 +889,28 @@ fn freeAtom(self: *Coff, atom: *Atom) void {...@@ -896,6 +889,28 @@ fn freeAtom(self: *Coff, atom: *Atom) void {
896 } else {889 } else {
897 atom.next = null;890 atom.next = null;
898 }891 }
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;
899}914}
900915
901pub fn updateFunc(self: *Coff, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void {916pub 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...@@ -912,8 +927,15 @@ pub fn updateFunc(self: *Coff, module: *Module, func: *Module.Fn, air: Air, live
912927
913 const decl_index = func.owner_decl;928 const decl_index = func.owner_decl;
914 const decl = module.declPtr(decl_index);929 const decl = module.declPtr(decl_index);
915 self.freeUnnamedConsts(decl_index);930 const atom = &decl.link.coff;
916 self.freeRelocationsForAtom(&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
918 var code_buffer = std.ArrayList(u8).init(self.base.allocator);940 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
919 defer code_buffer.deinit();941 defer code_buffer.deinit();
...@@ -960,9 +982,9 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In...@@ -960,9 +982,9 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
960 const atom = try gpa.create(Atom);982 const atom = try gpa.create(Atom);
961 errdefer gpa.destroy(atom);983 errdefer gpa.destroy(atom);
962 atom.* = Atom.empty;984 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);
966 const sym_name = blk: {988 const sym_name = blk: {
967 const decl_name = try decl.getFullyQualifiedName(mod);989 const decl_name = try decl.getFullyQualifiedName(mod);
968 defer gpa.free(decl_name);990 defer gpa.free(decl_name);
...@@ -971,14 +993,11 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In...@@ -971,14 +993,11 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
971 break :blk try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });993 break :blk try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });
972 };994 };
973 defer gpa.free(sym_name);995 defer gpa.free(sym_name);
974 try self.setSymbolName(sym, sym_name);996 try self.setSymbolName(atom.getSymbolPtr(self), sym_name);
975 sym.section_number = @intToEnum(coff.SectionNumber, self.rdata_section_index.? + 1);997 atom.getSymbolPtr(self).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);
979998
980 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), tv, &code_buffer, .none, .{999 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().?,
982 });1001 });
983 const code = switch (res) {1002 const code = switch (res) {
984 .ok => code_buffer.items,1003 .ok => code_buffer.items,
...@@ -993,17 +1012,17 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In...@@ -993,17 +1012,17 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
993 const required_alignment = tv.ty.abiAlignment(self.base.options.target);1012 const required_alignment = tv.ty.abiAlignment(self.base.options.target);
994 atom.alignment = required_alignment;1013 atom.alignment = required_alignment;
995 atom.size = @intCast(u32, code.len);1014 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);
997 errdefer self.freeAtom(atom);1016 errdefer self.freeAtom(atom);
9981017
999 try unnamed_consts.append(gpa, atom);1018 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 });
1002 log.debug(" (required alignment 0x{x})", .{required_alignment});1021 log.debug(" (required alignment 0x{x})", .{required_alignment});
10031022
1004 try self.writeAtom(atom, code);1023 try self.writeAtom(atom, code);
10051024
1006 return atom.sym_index;1025 return atom.getSymbolIndex().?;
1007}1026}
10081027
1009pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !void {1028pub 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) !...@@ -1028,7 +1047,14 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !
1028 }1047 }
1029 }1048 }
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
1033 var code_buffer = std.ArrayList(u8).init(self.base.allocator);1059 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
1034 defer code_buffer.deinit();1060 defer code_buffer.deinit();
...@@ -1038,7 +1064,7 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !...@@ -1038,7 +1064,7 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !
1038 .ty = decl.ty,1064 .ty = decl.ty,
1039 .val = decl_val,1065 .val = decl_val,
1040 }, &code_buffer, .none, .{1066 }, &code_buffer, .none, .{
1041 .parent_atom_index = decl.link.coff.sym_index,1067 .parent_atom_index = decl.link.coff.getSymbolIndex().?,
1042 });1068 });
1043 const code = switch (res) {1069 const code = switch (res) {
1044 .ok => code_buffer.items,1070 .ok => code_buffer.items,
...@@ -1099,7 +1125,7 @@ fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8,...@@ -1099,7 +1125,7 @@ fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8,
10991125
1100 const code_len = @intCast(u32, code.len);1126 const code_len = @intCast(u32, code.len);
1101 const atom = &decl.link.coff;1127 const atom = &decl.link.coff;
1102 assert(atom.sym_index != 0); // Caller forgot to allocateDeclIndexes()1128
1103 if (atom.size != 0) {1129 if (atom.size != 0) {
1104 const sym = atom.getSymbolPtr(self);1130 const sym = atom.getSymbolPtr(self);
1105 try self.setSymbolName(sym, decl_name);1131 try self.setSymbolName(sym, decl_name);
...@@ -1116,7 +1142,7 @@ fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8,...@@ -1116,7 +1142,7 @@ fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8,
1116 if (vaddr != sym.value) {1142 if (vaddr != sym.value) {
1117 sym.value = vaddr;1143 sym.value = vaddr;
1118 log.debug(" (updating GOT entry)", .{});1144 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 };
1120 const got_atom = self.getGotAtomForSymbol(got_target).?;1146 const got_atom = self.getGotAtomForSymbol(got_target).?;
1121 self.markRelocsDirtyByTarget(got_target);1147 self.markRelocsDirtyByTarget(got_target);
1122 try self.writePtrWidthAtom(got_atom);1148 try self.writePtrWidthAtom(got_atom);
...@@ -1137,10 +1163,10 @@ fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8,...@@ -1137,10 +1163,10 @@ fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8,
1137 atom.size = code_len;1163 atom.size = code_len;
1138 sym.value = vaddr;1164 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 };
1141 const got_index = try self.allocateGotEntry(got_target);1167 const got_index = try self.allocateGotEntry(got_target);
1142 const got_atom = try self.createGotAtom(got_target);1168 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().?;
1144 try self.writePtrWidthAtom(got_atom);1170 try self.writePtrWidthAtom(got_atom);
1145 }1171 }
11461172
...@@ -1160,11 +1186,6 @@ fn freeUnnamedConsts(self: *Coff, decl_index: Module.Decl.Index) void {...@@ -1160,11 +1186,6 @@ fn freeUnnamedConsts(self: *Coff, decl_index: Module.Decl.Index) void {
1160 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;1186 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;
1161 for (unnamed_consts.items) |atom| {1187 for (unnamed_consts.items) |atom| {
1162 self.freeAtom(atom);1188 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;
1168 }1189 }
1169 unnamed_consts.clearAndFree(gpa);1190 unnamed_consts.clearAndFree(gpa);
1170}1191}
...@@ -1179,35 +1200,11 @@ pub fn freeDecl(self: *Coff, decl_index: Module.Decl.Index) void {...@@ -1179,35 +1200,11 @@ pub fn freeDecl(self: *Coff, decl_index: Module.Decl.Index) void {
11791200
1180 log.debug("freeDecl {*}", .{decl});1201 log.debug("freeDecl {*}", .{decl});
11811202
1182 const kv = self.decls.fetchRemove(decl_index);1203 if (self.decls.fetchRemove(decl_index)) |kv| {
1183 if (kv.?.value) |_| {1204 if (kv.value) |_| {
1184 self.freeAtom(&decl.link.coff);1205 self.freeAtom(&decl.link.coff);
1185 self.freeUnnamedConsts(decl_index);1206 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 });
1205 }1207 }
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;
1211 }1208 }
1212}1209}
12131210
...@@ -1261,7 +1258,14 @@ pub fn updateDeclExports(...@@ -1261,7 +1258,14 @@ pub fn updateDeclExports(
12611258
1262 const decl = module.declPtr(decl_index);1259 const decl = module.declPtr(decl_index);
1263 const atom = &decl.link.coff;1260 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
1265 const decl_sym = atom.getSymbol(self);1269 const decl_sym = atom.getSymbol(self);
12661270
1267 for (exports) |exp| {1271 for (exports) |exp| {
...@@ -1416,7 +1420,7 @@ pub fn flushModule(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod...@@ -1416,7 +1420,7 @@ pub fn flushModule(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
14161420
1417 const import_index = try self.allocateImportEntry(global);1421 const import_index = try self.allocateImportEntry(global);
1418 const import_atom = try self.createImportAtom();1422 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().?;
1420 try self.writePtrWidthAtom(import_atom);1424 try self.writePtrWidthAtom(import_atom);
1421 }1425 }
14221426
...@@ -1460,10 +1464,12 @@ pub fn getDeclVAddr(...@@ -1460,10 +1464,12 @@ pub fn getDeclVAddr(
1460 const decl = mod.declPtr(decl_index);1464 const decl = mod.declPtr(decl_index);
14611465
1462 assert(self.llvm_object == null);1466 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
1465 const atom = self.getAtomForSymbol(.{ .sym_index = reloc_info.parent_atom_index, .file = null }).?;1471 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 };
1467 try atom.addRelocation(self, .{1473 try atom.addRelocation(self, .{
1468 .type = .direct,1474 .type = .direct,
1469 .target = target,1475 .target = target,
src/link/Coff/Atom.zig+19-4
...@@ -39,30 +39,45 @@ pub const empty = Atom{...@@ -39,30 +39,45 @@ pub const empty = Atom{
39 .next = null,39 .next = null,
40};40};
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
42/// Returns symbol referencing this atom.53/// Returns symbol referencing this atom.
43pub fn getSymbol(self: Atom, coff_file: *const Coff) *const coff.Symbol {54pub fn getSymbol(self: Atom, coff_file: *const Coff) *const coff.Symbol {
55 const sym_index = self.getSymbolIndex().?;
44 return coff_file.getSymbol(.{56 return coff_file.getSymbol(.{
45 .sym_index = self.sym_index,57 .sym_index = sym_index,
46 .file = self.file,58 .file = self.file,
47 });59 });
48}60}
4961
50/// Returns pointer-to-symbol referencing this atom.62/// Returns pointer-to-symbol referencing this atom.
51pub fn getSymbolPtr(self: Atom, coff_file: *Coff) *coff.Symbol {63pub fn getSymbolPtr(self: Atom, coff_file: *Coff) *coff.Symbol {
64 const sym_index = self.getSymbolIndex().?;
52 return coff_file.getSymbolPtr(.{65 return coff_file.getSymbolPtr(.{
53 .sym_index = self.sym_index,66 .sym_index = sym_index,
54 .file = self.file,67 .file = self.file,
55 });68 });
56}69}
5770
58pub fn getSymbolWithLoc(self: Atom) SymbolWithLoc {71pub 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 };
60}74}
6175
62/// Returns the name of this atom.76/// Returns the name of this atom.
63pub fn getName(self: Atom, coff_file: *const Coff) []const u8 {77pub fn getName(self: Atom, coff_file: *const Coff) []const u8 {
78 const sym_index = self.getSymbolIndex().?;
64 return coff_file.getSymbolName(.{79 return coff_file.getSymbolName(.{
65 .sym_index = self.sym_index,80 .sym_index = sym_index,
66 .file = self.file,81 .file = self.file,
67 });82 });
68}83}
src/link/Elf.zig+28-21
...@@ -893,7 +893,7 @@ fn growAllocSection(self: *Elf, shdr_index: u16, phdr_index: u16, needed_size: u...@@ -893,7 +893,7 @@ fn growAllocSection(self: *Elf, shdr_index: u16, phdr_index: u16, needed_size: u
893 // Must move the entire section.893 // Must move the entire section.
894 const new_offset = self.findFreeSpace(needed_size, self.page_size);894 const new_offset = self.findFreeSpace(needed_size, self.page_size);
895 const existing_size = if (self.atoms.get(phdr_index)) |last| blk: {895 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);
897 break :blk (sym.st_value + sym.st_size) - phdr.p_vaddr;897 break :blk (sym.st_value + sym.st_size) - phdr.p_vaddr;
898 } else if (shdr_index == self.got_section_index.?) blk: {898 } else if (shdr_index == self.got_section_index.?) blk: {
899 break :blk shdr.sh_size;899 break :blk shdr.sh_size;
...@@ -1031,7 +1031,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1031,7 +1031,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1031 while (it.next()) |entry| {1031 while (it.next()) |entry| {
1032 const atom = entry.key_ptr.*;1032 const atom = entry.key_ptr.*;
1033 const relocs = entry.value_ptr.*;1033 const relocs = entry.value_ptr.*;
1034 const source_sym = self.local_symbols.items[atom.local_sym_index];1034 const source_sym = atom.getSymbol(self);
1035 const source_shdr = self.sections.items[source_sym.st_shndx];1035 const source_shdr = self.sections.items[source_sym.st_shndx];
10361036
1037 log.debug("relocating '{s}'", .{self.getString(source_sym.st_name)});1037 log.debug("relocating '{s}'", .{self.getString(source_sym.st_name)});
...@@ -2034,11 +2034,13 @@ fn writeElfHeader(self: *Elf) !void {...@@ -2034,11 +2034,13 @@ fn writeElfHeader(self: *Elf) !void {
2034}2034}
20352035
2036fn freeTextBlock(self: *Elf, text_block: *TextBlock, phdr_index: u16) void {2036fn 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);
2038 const name_str_index = local_sym.st_name;2038 const name_str_index = local_sym.st_name;
2039 const name = self.getString(name_str_index);2039 const name = self.getString(name_str_index);
2040 log.debug("freeTextBlock {*} ({s})", .{ text_block, name });2040 log.debug("freeTextBlock {*} ({s})", .{ text_block, name });
20412041
2042 self.freeRelocationsForTextBlock(text_block);
2043
2042 const free_list = self.atom_free_lists.getPtr(phdr_index).?;2044 const free_list = self.atom_free_lists.getPtr(phdr_index).?;
2043 var already_have_free_list_node = false;2045 var already_have_free_list_node = false;
2044 {2046 {
...@@ -2107,7 +2109,7 @@ fn shrinkTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, phdr...@@ -2107,7 +2109,7 @@ fn shrinkTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, phdr
2107}2109}
21082110
2109fn growTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, alignment: u64, phdr_index: u16) !u64 {2111fn 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);
2111 const align_ok = mem.alignBackwardGeneric(u64, sym.st_value, alignment) == sym.st_value;2113 const align_ok = mem.alignBackwardGeneric(u64, sym.st_value, alignment) == sym.st_value;
2112 const need_realloc = !align_ok or new_block_size > text_block.capacity(self);2114 const need_realloc = !align_ok or new_block_size > text_block.capacity(self);
2113 if (!need_realloc) return sym.st_value;2115 if (!need_realloc) return sym.st_value;
...@@ -2137,7 +2139,7 @@ fn allocateTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, al...@@ -2137,7 +2139,7 @@ fn allocateTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, al
2137 const big_block = free_list.items[i];2139 const big_block = free_list.items[i];
2138 // We now have a pointer to a live text block that has too much capacity.2140 // We now have a pointer to a live text block that has too much capacity.
2139 // Is it enough that we could fit this new text block?2141 // 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);
2141 const capacity = big_block.capacity(self);2143 const capacity = big_block.capacity(self);
2142 const ideal_capacity = padToIdeal(capacity);2144 const ideal_capacity = padToIdeal(capacity);
2143 const ideal_capacity_end_vaddr = std.math.add(u64, sym.st_value, ideal_capacity) catch ideal_capacity;2145 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...@@ -2168,7 +2170,7 @@ fn allocateTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, al
2168 }2170 }
2169 break :blk new_start_vaddr;2171 break :blk new_start_vaddr;
2170 } else if (self.atoms.get(phdr_index)) |last| {2172 } 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);
2172 const ideal_capacity = padToIdeal(sym.st_size);2174 const ideal_capacity = padToIdeal(sym.st_size);
2173 const ideal_capacity_end_vaddr = sym.st_value + ideal_capacity;2175 const ideal_capacity_end_vaddr = sym.st_value + ideal_capacity;
2174 const new_start_vaddr = mem.alignForwardGeneric(u64, ideal_capacity_end_vaddr, alignment);2176 const new_start_vaddr = mem.alignForwardGeneric(u64, ideal_capacity_end_vaddr, alignment);
...@@ -2270,6 +2272,11 @@ pub fn allocateGotOffset(self: *Elf) !u32 {...@@ -2270,6 +2272,11 @@ pub fn allocateGotOffset(self: *Elf) !u32 {
2270 return index;2272 return index;
2271}2273}
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
2273fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void {2280fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void {
2274 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;2281 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;
2275 for (unnamed_consts.items) |atom| {2282 for (unnamed_consts.items) |atom| {
...@@ -2341,8 +2348,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s...@@ -2341,8 +2348,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
2341 const phdr_index = decl_ptr.*.?;2348 const phdr_index = decl_ptr.*.?;
2342 const shdr_index = self.phdr_shdr_table.get(phdr_index).?;2349 const shdr_index = self.phdr_shdr_table.get(phdr_index).?;
23432350
2344 assert(decl.link.elf.local_sym_index != 0); // Caller forgot to allocateDeclIndexes()2351 const local_sym = decl.link.elf.getSymbolPtr(self);
2345 const local_sym = &self.local_symbols.items[decl.link.elf.local_sym_index];
2346 if (local_sym.st_size != 0) {2352 if (local_sym.st_size != 0) {
2347 const capacity = decl.link.elf.capacity(self);2353 const capacity = decl.link.elf.capacity(self);
2348 const need_realloc = code.len > capacity or2354 const need_realloc = code.len > capacity or
...@@ -2366,7 +2372,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s...@@ -2366,7 +2372,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
2366 local_sym.st_other = 0;2372 local_sym.st_other = 0;
2367 local_sym.st_shndx = shdr_index;2373 local_sym.st_shndx = shdr_index;
2368 // TODO this write could be avoided if no fields of the symbol were changed.2374 // 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().?);
2370 } else {2376 } else {
2371 const name_str_index = try self.makeString(decl_name);2377 const name_str_index = try self.makeString(decl_name);
2372 const vaddr = try self.allocateTextBlock(&decl.link.elf, code.len, required_alignment, phdr_index);2378 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...@@ -2383,7 +2389,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
2383 };2389 };
2384 self.offset_table.items[decl.link.elf.offset_table_index] = vaddr;2390 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().?);
2387 try self.writeOffsetTableEntry(decl.link.elf.offset_table_index);2393 try self.writeOffsetTableEntry(decl.link.elf.offset_table_index);
2388 }2394 }
23892395
...@@ -2412,6 +2418,7 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven...@@ -2412,6 +2418,7 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven
2412 const gop = try self.decls.getOrPut(self.base.allocator, decl_index);2418 const gop = try self.decls.getOrPut(self.base.allocator, decl_index);
2413 if (gop.found_existing) {2419 if (gop.found_existing) {
2414 self.freeUnnamedConsts(decl_index);2420 self.freeUnnamedConsts(decl_index);
2421 self.freeRelocationsForTextBlock(atom);
2415 } else {2422 } else {
2416 gop.value_ptr.* = null;2423 gop.value_ptr.* = null;
2417 }2424 }
...@@ -2481,7 +2488,9 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v...@@ -2481,7 +2488,9 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v
2481 const atom = &decl.link.elf;2488 const atom = &decl.link.elf;
2482 try atom.ensureInitialized(self);2489 try atom.ensureInitialized(self);
2483 const gop = try self.decls.getOrPut(self.base.allocator, decl_index);2490 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 {
2485 gop.value_ptr.* = null;2494 gop.value_ptr.* = null;
2486 }2495 }
24872496
...@@ -2500,14 +2509,14 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v...@@ -2500,14 +2509,14 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v
2500 }, &code_buffer, .{2509 }, &code_buffer, .{
2501 .dwarf = ds,2510 .dwarf = ds,
2502 }, .{2511 }, .{
2503 .parent_atom_index = decl.link.elf.local_sym_index,2512 .parent_atom_index = decl.link.elf.getSymbolIndex().?,
2504 })2513 })
2505 else2514 else
2506 try codegen.generateSymbol(&self.base, decl.srcLoc(), .{2515 try codegen.generateSymbol(&self.base, decl.srcLoc(), .{
2507 .ty = decl.ty,2516 .ty = decl.ty,
2508 .val = decl_val,2517 .val = decl_val,
2509 }, &code_buffer, .none, .{2518 }, &code_buffer, .none, .{
2510 .parent_atom_index = decl.link.elf.local_sym_index,2519 .parent_atom_index = decl.link.elf.getSymbolIndex().?,
2511 });2520 });
25122521
2513 const code = switch (res) {2522 const code = switch (res) {
...@@ -2551,6 +2560,8 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module...@@ -2551,6 +2560,8 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
2551 const atom = try self.base.allocator.create(TextBlock);2560 const atom = try self.base.allocator.create(TextBlock);
2552 errdefer self.base.allocator.destroy(atom);2561 errdefer self.base.allocator.destroy(atom);
2553 atom.* = TextBlock.empty;2562 atom.* = TextBlock.empty;
2563 // TODO for unnamed consts we don't need GOT offset/entry allocated
2564 try atom.ensureInitialized(self);
2554 try self.managed_atoms.append(self.base.allocator, atom);2565 try self.managed_atoms.append(self.base.allocator, atom);
25552566
2556 const name_str_index = blk: {2567 const name_str_index = blk: {
...@@ -2565,14 +2576,10 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module...@@ -2565,14 +2576,10 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
2565 };2576 };
2566 const name = self.getString(name_str_index);2577 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
2572 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .{2579 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .{
2573 .none = {},2580 .none = {},
2574 }, .{2581 }, .{
2575 .parent_atom_index = atom.local_sym_index,2582 .parent_atom_index = atom.getSymbolIndex().?,
2576 });2583 });
2577 const code = switch (res) {2584 const code = switch (res) {
2578 .ok => code_buffer.items,2585 .ok => code_buffer.items,
...@@ -2592,7 +2599,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module...@@ -2592,7 +2599,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
25922599
2593 log.debug("allocated text block for {s} at 0x{x}", .{ name, vaddr });2600 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);
2596 local_sym.* = .{2603 local_sym.* = .{
2597 .st_name = name_str_index,2604 .st_name = name_str_index,
2598 .st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT,2605 .st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT,
...@@ -2602,14 +2609,14 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module...@@ -2602,14 +2609,14 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
2602 .st_size = code.len,2609 .st_size = code.len,
2603 };2610 };
26042611
2605 try self.writeSymbol(atom.local_sym_index);2612 try self.writeSymbol(atom.getSymbolIndex().?);
2606 try unnamed_consts.append(self.base.allocator, atom);2613 try unnamed_consts.append(self.base.allocator, atom);
26072614
2608 const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr;2615 const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr;
2609 const file_offset = self.sections.items[shdr_index].sh_offset + section_offset;2616 const file_offset = self.sections.items[shdr_index].sh_offset + section_offset;
2610 try self.base.file.?.pwriteAll(code, file_offset);2617 try self.base.file.?.pwriteAll(code, file_offset);
26112618
2612 return atom.local_sym_index;2619 return atom.getSymbolIndex().?;
2613}2620}
26142621
2615pub fn updateDeclExports(2622pub fn updateDeclExports(
src/link/MachO.zig+51-84
...@@ -1056,19 +1056,14 @@ pub fn allocateSpecialSymbols(self: *MachO) !void {...@@ -1056,19 +1056,14 @@ pub fn allocateSpecialSymbols(self: *MachO) !void {
1056pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {1056pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
1057 const gpa = self.base.allocator;1057 const gpa = self.base.allocator;
10581058
1059 const sym_index = try self.allocateSymbol();1059 const atom = try gpa.create(Atom);
1060 const atom = blk: {1060 atom.* = Atom.empty;
1061 const atom = try gpa.create(Atom);1061 try atom.ensureInitialized(self);
1062 atom.* = Atom.empty;1062 atom.size = @sizeOf(u64);
1063 atom.sym_index = sym_index;1063 atom.alignment = @alignOf(u64);
1064 atom.size = @sizeOf(u64);
1065 atom.alignment = @alignOf(u64);
1066 break :blk atom;
1067 };
1068 errdefer gpa.destroy(atom);1064 errdefer gpa.destroy(atom);
10691065
1070 try self.managed_atoms.append(gpa, atom);1066 try self.managed_atoms.append(gpa, atom);
1071 try self.atom_by_index_table.putNoClobber(gpa, atom.sym_index, atom);
10721067
1073 const sym = atom.getSymbolPtr(self);1068 const sym = atom.getSymbolPtr(self);
1074 sym.n_type = macho.N_SECT;1069 sym.n_type = macho.N_SECT;
...@@ -1109,15 +1104,11 @@ pub fn createDyldPrivateAtom(self: *MachO) !void {...@@ -1109,15 +1104,11 @@ pub fn createDyldPrivateAtom(self: *MachO) !void {
11091104
1110 const gpa = self.base.allocator;1105 const gpa = self.base.allocator;
11111106
1112 const sym_index = try self.allocateSymbol();1107 const atom = try gpa.create(Atom);
1113 const atom = blk: {1108 atom.* = Atom.empty;
1114 const atom = try gpa.create(Atom);1109 try atom.ensureInitialized(self);
1115 atom.* = Atom.empty;1110 atom.size = @sizeOf(u64);
1116 atom.sym_index = sym_index;1111 atom.alignment = @alignOf(u64);
1117 atom.size = @sizeOf(u64);
1118 atom.alignment = @alignOf(u64);
1119 break :blk atom;
1120 };
1121 errdefer gpa.destroy(atom);1112 errdefer gpa.destroy(atom);
11221113
1123 const sym = atom.getSymbolPtr(self);1114 const sym = atom.getSymbolPtr(self);
...@@ -1126,7 +1117,6 @@ pub fn createDyldPrivateAtom(self: *MachO) !void {...@@ -1126,7 +1117,6 @@ pub fn createDyldPrivateAtom(self: *MachO) !void {
1126 self.dyld_private_atom = atom;1117 self.dyld_private_atom = atom;
11271118
1128 try self.managed_atoms.append(gpa, atom);1119 try self.managed_atoms.append(gpa, atom);
1129 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
11301120
1131 sym.n_value = try self.allocateAtom(atom, atom.size, @alignOf(u64));1121 sym.n_value = try self.allocateAtom(atom, atom.size, @alignOf(u64));
1132 log.debug("allocated dyld_private atom at 0x{x}", .{sym.n_value});1122 log.debug("allocated dyld_private atom at 0x{x}", .{sym.n_value});
...@@ -1144,18 +1134,14 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {...@@ -1144,18 +1134,14 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {
1144 .aarch64 => 6 * @sizeOf(u32),1134 .aarch64 => 6 * @sizeOf(u32),
1145 else => unreachable,1135 else => unreachable,
1146 };1136 };
1147 const sym_index = try self.allocateSymbol();1137 const atom = try gpa.create(Atom);
1148 const atom = blk: {1138 atom.* = Atom.empty;
1149 const atom = try gpa.create(Atom);1139 try atom.ensureInitialized(self);
1150 atom.* = Atom.empty;1140 atom.size = size;
1151 atom.sym_index = sym_index;1141 atom.alignment = switch (arch) {
1152 atom.size = size;1142 .x86_64 => 1,
1153 atom.alignment = switch (arch) {1143 .aarch64 => @alignOf(u32),
1154 .x86_64 => 1,1144 else => unreachable,
1155 .aarch64 => @alignOf(u32),
1156 else => unreachable,
1157 };
1158 break :blk atom;
1159 };1145 };
1160 errdefer gpa.destroy(atom);1146 errdefer gpa.destroy(atom);
11611147
...@@ -1163,7 +1149,7 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {...@@ -1163,7 +1149,7 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {
1163 sym.n_type = macho.N_SECT;1149 sym.n_type = macho.N_SECT;
1164 sym.n_sect = self.stub_helper_section_index.? + 1;1150 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
1168 const code = try gpa.alloc(u8, size);1154 const code = try gpa.alloc(u8, size);
1169 defer gpa.free(code);1155 defer gpa.free(code);
...@@ -1258,7 +1244,6 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {...@@ -1258,7 +1244,6 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {
1258 self.stub_helper_preamble_atom = atom;1244 self.stub_helper_preamble_atom = atom;
12591245
1260 try self.managed_atoms.append(gpa, atom);1246 try self.managed_atoms.append(gpa, atom);
1261 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
12621247
1263 sym.n_value = try self.allocateAtom(atom, size, atom.alignment);1248 sym.n_value = try self.allocateAtom(atom, size, atom.alignment);
1264 log.debug("allocated stub preamble atom at 0x{x}", .{sym.n_value});1249 log.debug("allocated stub preamble atom at 0x{x}", .{sym.n_value});
...@@ -1273,18 +1258,14 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {...@@ -1273,18 +1258,14 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
1273 .aarch64 => 3 * @sizeOf(u32),1258 .aarch64 => 3 * @sizeOf(u32),
1274 else => unreachable,1259 else => unreachable,
1275 };1260 };
1276 const sym_index = try self.allocateSymbol();1261 const atom = try gpa.create(Atom);
1277 const atom = blk: {1262 atom.* = Atom.empty;
1278 const atom = try gpa.create(Atom);1263 try atom.ensureInitialized(self);
1279 atom.* = Atom.empty;1264 atom.size = size;
1280 atom.sym_index = sym_index;1265 atom.alignment = switch (arch) {
1281 atom.size = size;1266 .x86_64 => 1,
1282 atom.alignment = switch (arch) {1267 .aarch64 => @alignOf(u32),
1283 .x86_64 => 1,1268 else => unreachable,
1284 .aarch64 => @alignOf(u32),
1285 else => unreachable,
1286 };
1287 break :blk atom;
1288 };1269 };
1289 errdefer gpa.destroy(atom);1270 errdefer gpa.destroy(atom);
12901271
...@@ -1306,7 +1287,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {...@@ -1306,7 +1287,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
13061287
1307 try atom.addRelocation(self, .{1288 try atom.addRelocation(self, .{
1308 .type = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_BRANCH),1289 .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 },
1310 .offset = 6,1291 .offset = 6,
1311 .addend = 0,1292 .addend = 0,
1312 .pcrel = true,1293 .pcrel = true,
...@@ -1329,7 +1310,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {...@@ -1329,7 +1310,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
13291310
1330 try atom.addRelocation(self, .{1311 try atom.addRelocation(self, .{
1331 .type = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_BRANCH26),1312 .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 },
1333 .offset = 4,1314 .offset = 4,
1334 .addend = 0,1315 .addend = 0,
1335 .pcrel = true,1316 .pcrel = true,
...@@ -1340,7 +1321,6 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {...@@ -1340,7 +1321,6 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
1340 }1321 }
13411322
1342 try self.managed_atoms.append(gpa, atom);1323 try self.managed_atoms.append(gpa, atom);
1343 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
13441324
1345 sym.n_value = try self.allocateAtom(atom, size, atom.alignment);1325 sym.n_value = try self.allocateAtom(atom, size, atom.alignment);
1346 log.debug("allocated stub helper atom at 0x{x}", .{sym.n_value});1326 log.debug("allocated stub helper atom at 0x{x}", .{sym.n_value});
...@@ -1351,15 +1331,11 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {...@@ -1351,15 +1331,11 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
13511331
1352pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWithLoc) !*Atom {1332pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWithLoc) !*Atom {
1353 const gpa = self.base.allocator;1333 const gpa = self.base.allocator;
1354 const sym_index = try self.allocateSymbol();1334 const atom = try gpa.create(Atom);
1355 const atom = blk: {1335 atom.* = Atom.empty;
1356 const atom = try gpa.create(Atom);1336 try atom.ensureInitialized(self);
1357 atom.* = Atom.empty;1337 atom.size = @sizeOf(u64);
1358 atom.sym_index = sym_index;1338 atom.alignment = @alignOf(u64);
1359 atom.size = @sizeOf(u64);
1360 atom.alignment = @alignOf(u64);
1361 break :blk atom;
1362 };
1363 errdefer gpa.destroy(atom);1339 errdefer gpa.destroy(atom);
13641340
1365 const sym = atom.getSymbolPtr(self);1341 const sym = atom.getSymbolPtr(self);
...@@ -1385,7 +1361,6 @@ pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWi...@@ -1385,7 +1361,6 @@ pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWi
1385 });1361 });
13861362
1387 try self.managed_atoms.append(gpa, atom);1363 try self.managed_atoms.append(gpa, atom);
1388 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
13891364
1390 sym.n_value = try self.allocateAtom(atom, atom.size, @alignOf(u64));1365 sym.n_value = try self.allocateAtom(atom, atom.size, @alignOf(u64));
1391 log.debug("allocated lazy pointer atom at 0x{x} ({s})", .{ sym.n_value, self.getSymbolName(target) });1366 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 {...@@ -1402,19 +1377,15 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
1402 .aarch64 => 3 * @sizeOf(u32),1377 .aarch64 => 3 * @sizeOf(u32),
1403 else => unreachable, // unhandled architecture type1378 else => unreachable, // unhandled architecture type
1404 };1379 };
1405 const sym_index = try self.allocateSymbol();1380 const atom = try gpa.create(Atom);
1406 const atom = blk: {1381 atom.* = Atom.empty;
1407 const atom = try gpa.create(Atom);1382 try atom.ensureInitialized(self);
1408 atom.* = Atom.empty;1383 atom.size = size;
1409 atom.sym_index = sym_index;1384 atom.alignment = switch (arch) {
1410 atom.size = size;1385 .x86_64 => 1,
1411 atom.alignment = switch (arch) {1386 .aarch64 => @alignOf(u32),
1412 .x86_64 => 1,1387 else => unreachable, // unhandled architecture type
1413 .aarch64 => @alignOf(u32),
1414 else => unreachable, // unhandled architecture type
14151388
1416 };
1417 break :blk atom;
1418 };1389 };
1419 errdefer gpa.destroy(atom);1390 errdefer gpa.destroy(atom);
14201391
...@@ -1476,7 +1447,6 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {...@@ -1476,7 +1447,6 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
1476 }1447 }
14771448
1478 try self.managed_atoms.append(gpa, atom);1449 try self.managed_atoms.append(gpa, atom);
1479 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
14801450
1481 sym.n_value = try self.allocateAtom(atom, size, atom.alignment);1451 sym.n_value = try self.allocateAtom(atom, size, atom.alignment);
1482 log.debug("allocated stub atom at 0x{x}", .{sym.n_value});1452 log.debug("allocated stub atom at 0x{x}", .{sym.n_value});
...@@ -1617,9 +1587,9 @@ pub fn resolveSymbolsInDylibs(self: *MachO) !void {...@@ -1617,9 +1587,9 @@ pub fn resolveSymbolsInDylibs(self: *MachO) !void {
16171587
1618 const stub_index = try self.allocateStubEntry(global);1588 const stub_index = try self.allocateStubEntry(global);
1619 const stub_helper_atom = try self.createStubHelperAtom();1589 const stub_helper_atom = try self.createStubHelperAtom();
1620 const laptr_atom = try self.createLazyPointerAtom(stub_helper_atom.sym_index, global);1590 const laptr_atom = try self.createLazyPointerAtom(stub_helper_atom.getSymbolIndex().?, global);
1621 const stub_atom = try self.createStubAtom(laptr_atom.sym_index);1591 const stub_atom = try self.createStubAtom(laptr_atom.getSymbolIndex().?);
1622 self.stubs.items[stub_index].sym_index = stub_atom.sym_index;1592 self.stubs.items[stub_index].sym_index = stub_atom.getSymbolIndex().?;
1623 self.markRelocsDirtyByTarget(global);1593 self.markRelocsDirtyByTarget(global);
1624 }1594 }
16251595
...@@ -1717,7 +1687,7 @@ pub fn resolveDyldStubBinder(self: *MachO) !void {...@@ -1717,7 +1687,7 @@ pub fn resolveDyldStubBinder(self: *MachO) !void {
1717 // Add dyld_stub_binder as the final GOT entry.1687 // Add dyld_stub_binder as the final GOT entry.
1718 const got_index = try self.allocateGotEntry(global);1688 const got_index = try self.allocateGotEntry(global);
1719 const got_atom = try self.createGotAtom(global);1689 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
1722 try self.writePtrWidthAtom(got_atom);1692 try self.writePtrWidthAtom(got_atom);
1723}1693}
...@@ -2098,14 +2068,11 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu...@@ -2098,14 +2068,11 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
2098 const atom = try gpa.create(Atom);2068 const atom = try gpa.create(Atom);
2099 errdefer gpa.destroy(atom);2069 errdefer gpa.destroy(atom);
2100 atom.* = Atom.empty;2070 atom.* = Atom.empty;
21012071 try atom.ensureInitialized(self);
2102 atom.sym_index = try self.allocateSymbol();
2103
2104 try self.managed_atoms.append(gpa, atom);2072 try self.managed_atoms.append(gpa, atom);
2105 try self.atom_by_index_table.putNoClobber(gpa, atom.sym_index, atom);
21062073
2107 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .none, .{2074 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().?,
2109 });2076 });
2110 const code = switch (res) {2077 const code = switch (res) {
2111 .ok => code_buffer.items,2078 .ok => code_buffer.items,
...@@ -2137,7 +2104,7 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu...@@ -2137,7 +2104,7 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
21372104
2138 try self.writeAtom(atom, code);2105 try self.writeAtom(atom, code);
21392106
2140 return atom.sym_index;2107 return atom.getSymbolIndex().?;
2141}2108}
21422109
2143pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index) !void {2110pub 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)...@@ -2188,14 +2155,14 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
2188 }, &code_buffer, .{2155 }, &code_buffer, .{
2189 .dwarf = ds,2156 .dwarf = ds,
2190 }, .{2157 }, .{
2191 .parent_atom_index = decl.link.macho.sym_index,2158 .parent_atom_index = decl.link.macho.getSymbolIndex().?,
2192 })2159 })
2193 else2160 else
2194 try codegen.generateSymbol(&self.base, decl.srcLoc(), .{2161 try codegen.generateSymbol(&self.base, decl.srcLoc(), .{
2195 .ty = decl.ty,2162 .ty = decl.ty,
2196 .val = decl_val,2163 .val = decl_val,
2197 }, &code_buffer, .none, .{2164 }, &code_buffer, .none, .{
2198 .parent_atom_index = decl.link.macho.sym_index,2165 .parent_atom_index = decl.link.macho.getSymbolIndex().?,
2199 });2166 });
22002167
2201 const code = switch (res) {2168 const code = switch (res) {