authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-10 10:30:24+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:05+02:00
loge571208433ad4ec99e62d488aa9f244472e7ea6f
treea5074645b787c9b36fd167414f00f33806a63bd8
parent871591ca1c4983448be8ab96f64dbd56b3c5b0e5

elf: rename Zig-specific section handles to _zig_ infixed


1 files changed, 66 insertions(+), 163 deletions(-)

src/link/Elf.zig+66-163
......@@ -37,22 +37,18 @@ phdr_table_index: ?u16 = null,
3737/// but incremental linking means we can't ensure they are consecutive.
3838phdr_table_load_index: ?u16 = null,
3939/// The index into the program headers of a PT_LOAD program header with Read and Execute flags
40phdr_load_re_index: ?u16 = null,
40phdr_load_re_zig_index: ?u16 = null,
4141/// The index into the program headers of the global offset table.
4242/// It needs PT_LOAD and Read flags.
43phdr_got_index: ?u16 = null,
43phdr_got_zig_index: ?u16 = null,
4444/// The index into the program headers of a PT_LOAD program header with Read flag
45phdr_load_ro_index: ?u16 = null,
45phdr_load_ro_zig_index: ?u16 = null,
4646/// The index into the program headers of a PT_LOAD program header with Write flag
47phdr_load_rw_index: ?u16 = null,
47phdr_load_rw_zig_index: ?u16 = null,
4848/// The index into the program headers of a PT_LOAD program header with zerofill data.
49phdr_load_zerofill_index: ?u16 = null,
49phdr_load_zerofill_zig_index: ?u16 = null,
5050/// The index into the program headers of the PT_TLS program header.
5151phdr_tls_index: ?u16 = null,
52/// The index into the program headers of a PT_LOAD program header with TLS data.
53phdr_load_tls_data_index: ?u16 = null,
54/// The index into the program headers of a PT_LOAD program header with TLS zerofill data.
55phdr_load_tls_zerofill_index: ?u16 = null,
5652
5753entry_index: ?Symbol.Index = null,
5854page_size: u32,
......@@ -91,13 +87,12 @@ copy_rel: CopyRelSection = .{},
9187/// .rela.plt section
9288rela_plt: std.ArrayListUnmanaged(elf.Elf64_Rela) = .{},
9389
94/// Tracked section headers
95text_section_index: ?u16 = null,
96rodata_section_index: ?u16 = null,
97data_section_index: ?u16 = null,
98bss_section_index: ?u16 = null,
99tdata_section_index: ?u16 = null,
100tbss_section_index: ?u16 = null,
90/// Tracked section headers with incremental updates to Zig module
91text_zig_section_index: ?u16 = null,
92rodata_zig_section_index: ?u16 = null,
93data_zig_section_index: ?u16 = null,
94bss_zig_section_index: ?u16 = null,
95got_zig_section_index: ?u16 = null,
10196debug_info_section_index: ?u16 = null,
10297debug_abbrev_section_index: ?u16 = null,
10398debug_str_section_index: ?u16 = null,
......@@ -300,8 +295,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
300295 esym.st_shndx = elf.SHN_ABS;
301296 symbol_ptr.esym_index = esym_index;
302297
303 // TODO move populateMissingMetadata here renamed to zig_module.initMetadata();
304 try self.populateMissingMetadata();
298 try self.initMetadata();
305299 }
306300
307301 return self;
......@@ -456,7 +450,7 @@ pub fn lowerAnonDecl(self: *Elf, decl_val: InternPool.Index, src_loc: Module.Src
456450 const tv = TypedValue{ .ty = ty, .val = val };
457451 const name = try std.fmt.allocPrint(gpa, "__anon_{d}", .{@intFromEnum(decl_val)});
458452 defer gpa.free(name);
459 const res = self.lowerConst(name, tv, self.rodata_section_index.?, src_loc) catch |err| switch (err) {
453 const res = self.lowerConst(name, tv, self.rodata_zig_section_index.?, src_loc) catch |err| switch (err) {
460454 else => {
461455 // TODO improve error message
462456 const em = try Module.ErrorMsg.create(gpa, src_loc, "lowerAnonDecl failed with error: {s}", .{
......@@ -676,7 +670,8 @@ fn allocateNonAllocSection(self: *Elf, opts: AllocateNonAllocSectionOpts) error{
676670 return index;
677671}
678672
679pub fn populateMissingMetadata(self: *Elf) !void {
673/// TODO move to ZigModule
674pub fn initMetadata(self: *Elf) !void {
680675 const gpa = self.base.allocator;
681676 const ptr_size: u8 = self.ptrWidthBytes();
682677 const is_linux = self.base.options.target.os.tag == .linux;
......@@ -710,171 +705,99 @@ pub fn populateMissingMetadata(self: *Elf) !void {
710705 self.phdr_table_dirty = true;
711706 }
712707
713 if (self.phdr_load_re_index == null) {
714 self.phdr_load_re_index = try self.allocateSegment(.{
708 if (self.phdr_load_re_zig_index == null) {
709 self.phdr_load_re_zig_index = try self.allocateSegment(.{
715710 .size = self.base.options.program_code_size_hint,
716711 .alignment = self.page_size,
717712 .flags = elf.PF_X | elf.PF_R | elf.PF_W,
718713 });
719714 }
720715
721 if (self.phdr_got_index == null) {
716 if (self.phdr_got_zig_index == null) {
722717 // We really only need ptr alignment but since we are using PROGBITS, linux requires
723718 // page align.
724719 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
725 self.phdr_got_index = try self.allocateSegment(.{
720 self.phdr_got_zig_index = try self.allocateSegment(.{
726721 .size = @as(u64, ptr_size) * self.base.options.symbol_count_hint,
727722 .alignment = alignment,
728723 .flags = elf.PF_R | elf.PF_W,
729724 });
730725 }
731726
732 if (self.phdr_load_ro_index == null) {
727 if (self.phdr_load_ro_zig_index == null) {
733728 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
734 self.phdr_load_ro_index = try self.allocateSegment(.{
729 self.phdr_load_ro_zig_index = try self.allocateSegment(.{
735730 .size = 1024,
736731 .alignment = alignment,
737732 .flags = elf.PF_R | elf.PF_W,
738733 });
739734 }
740735
741 if (self.phdr_load_rw_index == null) {
736 if (self.phdr_load_rw_zig_index == null) {
742737 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
743 self.phdr_load_rw_index = try self.allocateSegment(.{
738 self.phdr_load_rw_zig_index = try self.allocateSegment(.{
744739 .size = 1024,
745740 .alignment = alignment,
746741 .flags = elf.PF_R | elf.PF_W,
747742 });
748743 }
749744
750 if (self.phdr_load_zerofill_index == null) {
745 if (self.phdr_load_zerofill_zig_index == null) {
751746 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
752 self.phdr_load_zerofill_index = try self.allocateSegment(.{
747 self.phdr_load_zerofill_zig_index = try self.allocateSegment(.{
753748 .size = 0,
754749 .alignment = alignment,
755750 .flags = elf.PF_R | elf.PF_W,
756751 });
757 const phdr = &self.phdrs.items[self.phdr_load_zerofill_index.?];
758 phdr.p_offset = self.phdrs.items[self.phdr_load_rw_index.?].p_offset; // .bss overlaps .data
752 const phdr = &self.phdrs.items[self.phdr_load_zerofill_zig_index.?];
753 phdr.p_offset = self.phdrs.items[self.phdr_load_rw_zig_index.?].p_offset; // .bss overlaps .data
759754 phdr.p_memsz = 1024;
760755 }
761756
762 if (!self.base.options.single_threaded) {
763 if (self.phdr_load_tls_data_index == null) {
764 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
765 self.phdr_load_tls_data_index = try self.allocateSegment(.{
766 .size = 1024,
767 .alignment = alignment,
768 .flags = elf.PF_R | elf.PF_W,
769 });
770 }
771
772 if (self.phdr_load_tls_zerofill_index == null) {
773 // TODO .tbss doesn't need any physical or memory representation (aka a loadable segment)
774 // since the loader only cares about the PT_TLS to work out TLS size. However, when
775 // relocating we need to have .tdata and .tbss contiguously laid out so that we can
776 // work out correct offsets to the start/end of the TLS segment. I am thinking that
777 // perhaps it's possible to completely spoof it by having an abstracted mechanism
778 // for this that wouldn't require us to explicitly track .tbss. Anyhow, for now,
779 // we go the savage route of treating .tbss like .bss.
780 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
781 self.phdr_load_tls_zerofill_index = try self.allocateSegment(.{
782 .size = 0,
783 .alignment = alignment,
784 .flags = elf.PF_R | elf.PF_W,
785 });
786 const phdr = &self.phdrs.items[self.phdr_load_tls_zerofill_index.?];
787 phdr.p_offset = self.phdrs.items[self.phdr_load_tls_data_index.?].p_offset; // .tbss overlaps .tdata
788 phdr.p_memsz = 1024;
789 }
790
791 if (self.phdr_tls_index == null) {
792 self.phdr_tls_index = @intCast(self.phdrs.items.len);
793 const phdr_tdata = &self.phdrs.items[self.phdr_load_tls_data_index.?];
794 const phdr_tbss = &self.phdrs.items[self.phdr_load_tls_zerofill_index.?];
795 try self.phdrs.append(gpa, .{
796 .p_type = elf.PT_TLS,
797 .p_offset = phdr_tdata.p_offset,
798 .p_vaddr = phdr_tdata.p_vaddr,
799 .p_paddr = phdr_tdata.p_paddr,
800 .p_filesz = phdr_tdata.p_filesz,
801 .p_memsz = phdr_tbss.p_vaddr + phdr_tbss.p_memsz - phdr_tdata.p_vaddr,
802 .p_align = ptr_size,
803 .p_flags = elf.PF_R,
804 });
805 self.phdr_table_dirty = true;
806 }
807 }
808
809 if (self.text_section_index == null) {
810 self.text_section_index = try self.allocateAllocSection(.{
811 .name = ".text",
812 .phdr_index = self.phdr_load_re_index.?,
757 if (self.text_zig_section_index == null) {
758 self.text_zig_section_index = try self.allocateAllocSection(.{
759 .name = ".text.zig",
760 .phdr_index = self.phdr_load_re_zig_index.?,
813761 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
814762 });
815 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.text_section_index.?, .{});
763 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.text_zig_section_index.?, .{});
816764 }
817765
818 if (self.got_section_index == null) {
819 self.got_section_index = try self.allocateAllocSection(.{
820 .name = ".got",
821 .phdr_index = self.phdr_got_index.?,
766 if (self.got_zig_section_index == null) {
767 self.got_zig_section_index = try self.allocateAllocSection(.{
768 .name = ".got.zig",
769 .phdr_index = self.phdr_got_zig_index.?,
822770 .alignment = ptr_size,
823771 });
824772 }
825773
826 if (self.rodata_section_index == null) {
827 self.rodata_section_index = try self.allocateAllocSection(.{
828 .name = ".rodata",
829 .phdr_index = self.phdr_load_ro_index.?,
774 if (self.rodata_zig_section_index == null) {
775 self.rodata_zig_section_index = try self.allocateAllocSection(.{
776 .name = ".rodata.zig",
777 .phdr_index = self.phdr_load_ro_zig_index.?,
830778 });
831 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.rodata_section_index.?, .{});
779 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.rodata_zig_section_index.?, .{});
832780 }
833781
834 if (self.data_section_index == null) {
835 self.data_section_index = try self.allocateAllocSection(.{
836 .name = ".data",
837 .phdr_index = self.phdr_load_rw_index.?,
782 if (self.data_zig_section_index == null) {
783 self.data_zig_section_index = try self.allocateAllocSection(.{
784 .name = ".data.zig",
785 .phdr_index = self.phdr_load_rw_zig_index.?,
838786 .alignment = ptr_size,
839787 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
840788 });
841 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.data_section_index.?, .{});
789 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.data_zig_section_index.?, .{});
842790 }
843791
844 if (self.bss_section_index == null) {
845 self.bss_section_index = try self.allocateAllocSection(.{
846 .name = ".bss",
847 .phdr_index = self.phdr_load_zerofill_index.?,
792 if (self.bss_zig_section_index == null) {
793 self.bss_zig_section_index = try self.allocateAllocSection(.{
794 .name = ".bss.zig",
795 .phdr_index = self.phdr_load_zerofill_zig_index.?,
848796 .alignment = ptr_size,
849797 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
850798 .type = elf.SHT_NOBITS,
851799 });
852 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.bss_section_index.?, .{});
853 }
854
855 if (self.phdr_load_tls_data_index) |phdr_index| {
856 if (self.tdata_section_index == null) {
857 self.tdata_section_index = try self.allocateAllocSection(.{
858 .name = ".tdata",
859 .phdr_index = phdr_index,
860 .alignment = ptr_size,
861 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,
862 });
863 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.tdata_section_index.?, .{});
864 }
865 }
866
867 if (self.phdr_load_tls_zerofill_index) |phdr_index| {
868 if (self.tbss_section_index == null) {
869 self.tbss_section_index = try self.allocateAllocSection(.{
870 .name = ".tbss",
871 .phdr_index = phdr_index,
872 .alignment = ptr_size,
873 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,
874 .type = elf.SHT_NOBITS,
875 });
876 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.tbss_section_index.?, .{});
877 }
800 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.bss_zig_section_index.?, .{});
878801 }
879802
880803 if (self.dwarf) |*dw| {
......@@ -1705,36 +1628,16 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
17051628 // Beyond this point, everything has been allocated a virtual address and we can resolve
17061629 // the relocations, and commit objects to file.
17071630 if (self.zig_module_index) |index| {
1708 // .bss always overlaps .data in file offset, but is zero-sized in file so it doesn't
1631 // .bss.zig always overlaps .data.zig in file offset, but is zero-sized in file so it doesn't
17091632 // get mapped by the loader
1710 if (self.data_section_index) |data_shndx| blk: {
1711 const bss_shndx = self.bss_section_index orelse break :blk;
1633 if (self.data_zig_section_index) |data_shndx| blk: {
1634 const bss_shndx = self.bss_zig_section_index orelse break :blk;
17121635 const data_phndx = self.phdr_to_shdr_table.get(data_shndx).?;
17131636 const bss_phndx = self.phdr_to_shdr_table.get(bss_shndx).?;
17141637 self.shdrs.items[bss_shndx].sh_offset = self.shdrs.items[data_shndx].sh_offset;
17151638 self.phdrs.items[bss_phndx].p_offset = self.phdrs.items[data_phndx].p_offset;
17161639 }
17171640
1718 // Same treatment for .tbss section.
1719 if (self.tdata_section_index) |tdata_shndx| blk: {
1720 const tbss_shndx = self.tbss_section_index orelse break :blk;
1721 const tdata_phndx = self.phdr_to_shdr_table.get(tdata_shndx).?;
1722 const tbss_phndx = self.phdr_to_shdr_table.get(tbss_shndx).?;
1723 self.shdrs.items[tbss_shndx].sh_offset = self.shdrs.items[tdata_shndx].sh_offset;
1724 self.phdrs.items[tbss_phndx].p_offset = self.phdrs.items[tdata_phndx].p_offset;
1725 }
1726
1727 if (self.phdr_tls_index) |tls_index| {
1728 const tdata_phdr = &self.phdrs.items[self.phdr_load_tls_data_index.?];
1729 const tbss_phdr = &self.phdrs.items[self.phdr_load_tls_zerofill_index.?];
1730 const phdr = &self.phdrs.items[tls_index];
1731 phdr.p_offset = tdata_phdr.p_offset;
1732 phdr.p_filesz = tdata_phdr.p_filesz;
1733 phdr.p_vaddr = tdata_phdr.p_vaddr;
1734 phdr.p_paddr = tdata_phdr.p_vaddr;
1735 phdr.p_memsz = tbss_phdr.p_vaddr + tbss_phdr.p_memsz - tdata_phdr.p_vaddr;
1736 }
1737
17381641 const zig_module = self.file(index).?.zig_module;
17391642 for (zig_module.atoms.items) |atom_index| {
17401643 const atom_ptr = self.atom(atom_index) orelse continue;
......@@ -1767,7 +1670,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
17671670 if (self.debug_info_header_dirty) {
17681671 // Currently only one compilation unit is supported, so the address range is simply
17691672 // identical to the main program header virtual address and memory size.
1770 const text_phdr = &self.phdrs.items[self.phdr_load_re_index.?];
1673 const text_phdr = &self.phdrs.items[self.phdr_load_re_zig_index.?];
17711674 const low_pc = text_phdr.p_vaddr;
17721675 const high_pc = text_phdr.p_vaddr + text_phdr.p_memsz;
17731676 try dw.writeDbgInfoHeader(self.base.options.module.?, low_pc, high_pc);
......@@ -1777,7 +1680,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
17771680 if (self.debug_aranges_section_dirty) {
17781681 // Currently only one compilation unit is supported, so the address range is simply
17791682 // identical to the main program header virtual address and memory size.
1780 const text_phdr = &self.phdrs.items[self.phdr_load_re_index.?];
1683 const text_phdr = &self.phdrs.items[self.phdr_load_re_zig_index.?];
17811684 try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz);
17821685 if (!self.shdr_table_dirty) {
17831686 // Then it won't get written with the others and we need to do it.
......@@ -3221,24 +3124,24 @@ fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index, code: []const u8)
32213124 const decl = mod.declPtr(decl_index);
32223125 const shdr_index = switch (decl.ty.zigTypeTag(mod)) {
32233126 // TODO: what if this is a function pointer?
3224 .Fn => self.text_section_index.?,
3127 .Fn => self.text_zig_section_index.?,
32253128 else => blk: {
32263129 if (decl.getOwnedVariable(mod)) |variable| {
3227 if (variable.is_const) break :blk self.rodata_section_index.?;
3130 if (variable.is_const) break :blk self.rodata_zig_section_index.?;
32283131 if (variable.init.toValue().isUndefDeep(mod)) {
32293132 const mode = self.base.options.optimize_mode;
3230 if (mode == .Debug or mode == .ReleaseSafe) break :blk self.data_section_index.?;
3231 break :blk self.bss_section_index.?;
3133 if (mode == .Debug or mode == .ReleaseSafe) break :blk self.data_zig_section_index.?;
3134 break :blk self.bss_zig_section_index.?;
32323135 }
32333136 // TODO I blatantly copied the logic from the Wasm linker, but is there a less
32343137 // intrusive check for all zeroes than this?
32353138 const is_all_zeroes = for (code) |byte| {
32363139 if (byte != 0) break false;
32373140 } else true;
3238 if (is_all_zeroes) break :blk self.bss_section_index.?;
3239 break :blk self.data_section_index.?;
3141 if (is_all_zeroes) break :blk self.bss_zig_section_index.?;
3142 break :blk self.data_zig_section_index.?;
32403143 }
3241 break :blk self.rodata_section_index.?;
3144 break :blk self.rodata_zig_section_index.?;
32423145 },
32433146 };
32443147 return shdr_index;
......@@ -3518,8 +3421,8 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol.
35183421 };
35193422
35203423 const output_section_index = switch (sym.kind) {
3521 .code => self.text_section_index.?,
3522 .const_data => self.rodata_section_index.?,
3424 .code => self.text_zig_section_index.?,
3425 .const_data => self.rodata_zig_section_index.?,
35233426 };
35243427 const local_sym = self.symbol(symbol_index);
35253428 const phdr_index = self.phdr_to_shdr_table.get(output_section_index).?;
......@@ -3565,7 +3468,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
35653468 const index = unnamed_consts.items.len;
35663469 const name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });
35673470 defer gpa.free(name);
3568 const sym_index = switch (try self.lowerConst(name, typed_value, self.rodata_section_index.?, decl.srcLoc(mod))) {
3471 const sym_index = switch (try self.lowerConst(name, typed_value, self.rodata_zig_section_index.?, decl.srcLoc(mod))) {
35693472 .ok => |sym_index| sym_index,
35703473 .fail => |em| {
35713474 decl.analysis = .codegen_failure;