| ... | @@ -37,22 +37,18 @@ phdr_table_index: ?u16 = null, | ... | @@ -37,22 +37,18 @@ phdr_table_index: ?u16 = null, |
| 37 | /// but incremental linking means we can't ensure they are consecutive. | 37 | /// but incremental linking means we can't ensure they are consecutive. |
| 38 | phdr_table_load_index: ?u16 = null, | 38 | phdr_table_load_index: ?u16 = null, |
| 39 | /// The index into the program headers of a PT_LOAD program header with Read and Execute flags | 39 | /// The index into the program headers of a PT_LOAD program header with Read and Execute flags |
| 40 | phdr_load_re_index: ?u16 = null, | 40 | phdr_load_re_zig_index: ?u16 = null, |
| 41 | /// The index into the program headers of the global offset table. | 41 | /// The index into the program headers of the global offset table. |
| 42 | /// It needs PT_LOAD and Read flags. | 42 | /// It needs PT_LOAD and Read flags. |
| 43 | phdr_got_index: ?u16 = null, | 43 | phdr_got_zig_index: ?u16 = null, |
| 44 | /// The index into the program headers of a PT_LOAD program header with Read flag | 44 | /// The index into the program headers of a PT_LOAD program header with Read flag |
| 45 | phdr_load_ro_index: ?u16 = null, | 45 | phdr_load_ro_zig_index: ?u16 = null, |
| 46 | /// The index into the program headers of a PT_LOAD program header with Write flag | 46 | /// The index into the program headers of a PT_LOAD program header with Write flag |
| 47 | phdr_load_rw_index: ?u16 = null, | 47 | phdr_load_rw_zig_index: ?u16 = null, |
| 48 | /// The index into the program headers of a PT_LOAD program header with zerofill data. | 48 | /// The index into the program headers of a PT_LOAD program header with zerofill data. |
| 49 | phdr_load_zerofill_index: ?u16 = null, | 49 | phdr_load_zerofill_zig_index: ?u16 = null, |
| 50 | /// The index into the program headers of the PT_TLS program header. | 50 | /// The index into the program headers of the PT_TLS program header. |
| 51 | phdr_tls_index: ?u16 = null, | 51 | phdr_tls_index: ?u16 = null, |
| 52 | /// The index into the program headers of a PT_LOAD program header with TLS data. | | |
| 53 | phdr_load_tls_data_index: ?u16 = null, | | |
| 54 | /// The index into the program headers of a PT_LOAD program header with TLS zerofill data. | | |
| 55 | phdr_load_tls_zerofill_index: ?u16 = null, | | |
| 56 | | 52 | |
| 57 | entry_index: ?Symbol.Index = null, | 53 | entry_index: ?Symbol.Index = null, |
| 58 | page_size: u32, | 54 | page_size: u32, |
| ... | @@ -91,13 +87,12 @@ copy_rel: CopyRelSection = .{}, | ... | @@ -91,13 +87,12 @@ copy_rel: CopyRelSection = .{}, |
| 91 | /// .rela.plt section | 87 | /// .rela.plt section |
| 92 | rela_plt: std.ArrayListUnmanaged(elf.Elf64_Rela) = .{}, | 88 | rela_plt: std.ArrayListUnmanaged(elf.Elf64_Rela) = .{}, |
| 93 | | 89 | |
| 94 | /// Tracked section headers | 90 | /// Tracked section headers with incremental updates to Zig module |
| 95 | text_section_index: ?u16 = null, | 91 | text_zig_section_index: ?u16 = null, |
| 96 | rodata_section_index: ?u16 = null, | 92 | rodata_zig_section_index: ?u16 = null, |
| 97 | data_section_index: ?u16 = null, | 93 | data_zig_section_index: ?u16 = null, |
| 98 | bss_section_index: ?u16 = null, | 94 | bss_zig_section_index: ?u16 = null, |
| 99 | tdata_section_index: ?u16 = null, | 95 | got_zig_section_index: ?u16 = null, |
| 100 | tbss_section_index: ?u16 = null, | | |
| 101 | debug_info_section_index: ?u16 = null, | 96 | debug_info_section_index: ?u16 = null, |
| 102 | debug_abbrev_section_index: ?u16 = null, | 97 | debug_abbrev_section_index: ?u16 = null, |
| 103 | debug_str_section_index: ?u16 = null, | 98 | debug_str_section_index: ?u16 = null, |
| ... | @@ -300,8 +295,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option | ... | @@ -300,8 +295,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 300 | esym.st_shndx = elf.SHN_ABS; | 295 | esym.st_shndx = elf.SHN_ABS; |
| 301 | symbol_ptr.esym_index = esym_index; | 296 | symbol_ptr.esym_index = esym_index; |
| 302 | | 297 | |
| 303 | // TODO move populateMissingMetadata here renamed to zig_module.initMetadata(); | 298 | try self.initMetadata(); |
| 304 | try self.populateMissingMetadata(); | | |
| 305 | } | 299 | } |
| 306 | | 300 | |
| 307 | return self; | 301 | return self; |
| ... | @@ -456,7 +450,7 @@ pub fn lowerAnonDecl(self: *Elf, decl_val: InternPool.Index, src_loc: Module.Src | ... | @@ -456,7 +450,7 @@ pub fn lowerAnonDecl(self: *Elf, decl_val: InternPool.Index, src_loc: Module.Src |
| 456 | const tv = TypedValue{ .ty = ty, .val = val }; | 450 | const tv = TypedValue{ .ty = ty, .val = val }; |
| 457 | const name = try std.fmt.allocPrint(gpa, "__anon_{d}", .{@intFromEnum(decl_val)}); | 451 | const name = try std.fmt.allocPrint(gpa, "__anon_{d}", .{@intFromEnum(decl_val)}); |
| 458 | defer gpa.free(name); | 452 | 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) { |
| 460 | else => { | 454 | else => { |
| 461 | // TODO improve error message | 455 | // TODO improve error message |
| 462 | const em = try Module.ErrorMsg.create(gpa, src_loc, "lowerAnonDecl failed with error: {s}", .{ | 456 | 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{ | ... | @@ -676,7 +670,8 @@ fn allocateNonAllocSection(self: *Elf, opts: AllocateNonAllocSectionOpts) error{ |
| 676 | return index; | 670 | return index; |
| 677 | } | 671 | } |
| 678 | | 672 | |
| 679 | pub fn populateMissingMetadata(self: *Elf) !void { | 673 | /// TODO move to ZigModule |
| | 674 | pub fn initMetadata(self: *Elf) !void { |
| 680 | const gpa = self.base.allocator; | 675 | const gpa = self.base.allocator; |
| 681 | const ptr_size: u8 = self.ptrWidthBytes(); | 676 | const ptr_size: u8 = self.ptrWidthBytes(); |
| 682 | const is_linux = self.base.options.target.os.tag == .linux; | 677 | const is_linux = self.base.options.target.os.tag == .linux; |
| ... | @@ -710,171 +705,99 @@ pub fn populateMissingMetadata(self: *Elf) !void { | ... | @@ -710,171 +705,99 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 710 | self.phdr_table_dirty = true; | 705 | self.phdr_table_dirty = true; |
| 711 | } | 706 | } |
| 712 | | 707 | |
| 713 | if (self.phdr_load_re_index == null) { | 708 | if (self.phdr_load_re_zig_index == null) { |
| 714 | self.phdr_load_re_index = try self.allocateSegment(.{ | 709 | self.phdr_load_re_zig_index = try self.allocateSegment(.{ |
| 715 | .size = self.base.options.program_code_size_hint, | 710 | .size = self.base.options.program_code_size_hint, |
| 716 | .alignment = self.page_size, | 711 | .alignment = self.page_size, |
| 717 | .flags = elf.PF_X | elf.PF_R | elf.PF_W, | 712 | .flags = elf.PF_X | elf.PF_R | elf.PF_W, |
| 718 | }); | 713 | }); |
| 719 | } | 714 | } |
| 720 | | 715 | |
| 721 | if (self.phdr_got_index == null) { | 716 | if (self.phdr_got_zig_index == null) { |
| 722 | // We really only need ptr alignment but since we are using PROGBITS, linux requires | 717 | // We really only need ptr alignment but since we are using PROGBITS, linux requires |
| 723 | // page align. | 718 | // page align. |
| 724 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | 719 | 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(.{ |
| 726 | .size = @as(u64, ptr_size) * self.base.options.symbol_count_hint, | 721 | .size = @as(u64, ptr_size) * self.base.options.symbol_count_hint, |
| 727 | .alignment = alignment, | 722 | .alignment = alignment, |
| 728 | .flags = elf.PF_R | elf.PF_W, | 723 | .flags = elf.PF_R | elf.PF_W, |
| 729 | }); | 724 | }); |
| 730 | } | 725 | } |
| 731 | | 726 | |
| 732 | if (self.phdr_load_ro_index == null) { | 727 | if (self.phdr_load_ro_zig_index == null) { |
| 733 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | 728 | 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(.{ |
| 735 | .size = 1024, | 730 | .size = 1024, |
| 736 | .alignment = alignment, | 731 | .alignment = alignment, |
| 737 | .flags = elf.PF_R | elf.PF_W, | 732 | .flags = elf.PF_R | elf.PF_W, |
| 738 | }); | 733 | }); |
| 739 | } | 734 | } |
| 740 | | 735 | |
| 741 | if (self.phdr_load_rw_index == null) { | 736 | if (self.phdr_load_rw_zig_index == null) { |
| 742 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | 737 | 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(.{ |
| 744 | .size = 1024, | 739 | .size = 1024, |
| 745 | .alignment = alignment, | 740 | .alignment = alignment, |
| 746 | .flags = elf.PF_R | elf.PF_W, | 741 | .flags = elf.PF_R | elf.PF_W, |
| 747 | }); | 742 | }); |
| 748 | } | 743 | } |
| 749 | | 744 | |
| 750 | if (self.phdr_load_zerofill_index == null) { | 745 | if (self.phdr_load_zerofill_zig_index == null) { |
| 751 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | 746 | 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(.{ |
| 753 | .size = 0, | 748 | .size = 0, |
| 754 | .alignment = alignment, | 749 | .alignment = alignment, |
| 755 | .flags = elf.PF_R | elf.PF_W, | 750 | .flags = elf.PF_R | elf.PF_W, |
| 756 | }); | 751 | }); |
| 757 | const phdr = &self.phdrs.items[self.phdr_load_zerofill_index.?]; | 752 | const phdr = &self.phdrs.items[self.phdr_load_zerofill_zig_index.?]; |
| 758 | phdr.p_offset = self.phdrs.items[self.phdr_load_rw_index.?].p_offset; // .bss overlaps .data | 753 | phdr.p_offset = self.phdrs.items[self.phdr_load_rw_zig_index.?].p_offset; // .bss overlaps .data |
| 759 | phdr.p_memsz = 1024; | 754 | phdr.p_memsz = 1024; |
| 760 | } | 755 | } |
| 761 | | 756 | |
| 762 | if (!self.base.options.single_threaded) { | 757 | if (self.text_zig_section_index == null) { |
| 763 | if (self.phdr_load_tls_data_index == null) { | 758 | self.text_zig_section_index = try self.allocateAllocSection(.{ |
| 764 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | 759 | .name = ".text.zig", |
| 765 | self.phdr_load_tls_data_index = try self.allocateSegment(.{ | 760 | .phdr_index = self.phdr_load_re_zig_index.?, |
| 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.?, | | |
| 813 | .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, | 761 | .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| 814 | }); | 762 | }); |
| 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.?, .{}); |
| 816 | } | 764 | } |
| 817 | | 765 | |
| 818 | if (self.got_section_index == null) { | 766 | if (self.got_zig_section_index == null) { |
| 819 | self.got_section_index = try self.allocateAllocSection(.{ | 767 | self.got_zig_section_index = try self.allocateAllocSection(.{ |
| 820 | .name = ".got", | 768 | .name = ".got.zig", |
| 821 | .phdr_index = self.phdr_got_index.?, | 769 | .phdr_index = self.phdr_got_zig_index.?, |
| 822 | .alignment = ptr_size, | 770 | .alignment = ptr_size, |
| 823 | }); | 771 | }); |
| 824 | } | 772 | } |
| 825 | | 773 | |
| 826 | if (self.rodata_section_index == null) { | 774 | if (self.rodata_zig_section_index == null) { |
| 827 | self.rodata_section_index = try self.allocateAllocSection(.{ | 775 | self.rodata_zig_section_index = try self.allocateAllocSection(.{ |
| 828 | .name = ".rodata", | 776 | .name = ".rodata.zig", |
| 829 | .phdr_index = self.phdr_load_ro_index.?, | 777 | .phdr_index = self.phdr_load_ro_zig_index.?, |
| 830 | }); | 778 | }); |
| 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.?, .{}); |
| 832 | } | 780 | } |
| 833 | | 781 | |
| 834 | if (self.data_section_index == null) { | 782 | if (self.data_zig_section_index == null) { |
| 835 | self.data_section_index = try self.allocateAllocSection(.{ | 783 | self.data_zig_section_index = try self.allocateAllocSection(.{ |
| 836 | .name = ".data", | 784 | .name = ".data.zig", |
| 837 | .phdr_index = self.phdr_load_rw_index.?, | 785 | .phdr_index = self.phdr_load_rw_zig_index.?, |
| 838 | .alignment = ptr_size, | 786 | .alignment = ptr_size, |
| 839 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, | 787 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 840 | }); | 788 | }); |
| 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.?, .{}); |
| 842 | } | 790 | } |
| 843 | | 791 | |
| 844 | if (self.bss_section_index == null) { | 792 | if (self.bss_zig_section_index == null) { |
| 845 | self.bss_section_index = try self.allocateAllocSection(.{ | 793 | self.bss_zig_section_index = try self.allocateAllocSection(.{ |
| 846 | .name = ".bss", | 794 | .name = ".bss.zig", |
| 847 | .phdr_index = self.phdr_load_zerofill_index.?, | 795 | .phdr_index = self.phdr_load_zerofill_zig_index.?, |
| 848 | .alignment = ptr_size, | 796 | .alignment = ptr_size, |
| 849 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, | 797 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 850 | .type = elf.SHT_NOBITS, | 798 | .type = elf.SHT_NOBITS, |
| 851 | }); | 799 | }); |
| 852 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.bss_section_index.?, .{}); | 800 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.bss_zig_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 | } | | |
| 878 | } | 801 | } |
| 879 | | 802 | |
| 880 | if (self.dwarf) |*dw| { | 803 | if (self.dwarf) |*dw| { |
| ... | @@ -1705,36 +1628,16 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node | ... | @@ -1705,36 +1628,16 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1705 | // Beyond this point, everything has been allocated a virtual address and we can resolve | 1628 | // Beyond this point, everything has been allocated a virtual address and we can resolve |
| 1706 | // the relocations, and commit objects to file. | 1629 | // the relocations, and commit objects to file. |
| 1707 | if (self.zig_module_index) |index| { | 1630 | 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 |
| 1709 | // get mapped by the loader | 1632 | // get mapped by the loader |
| 1710 | if (self.data_section_index) |data_shndx| blk: { | 1633 | if (self.data_zig_section_index) |data_shndx| blk: { |
| 1711 | const bss_shndx = self.bss_section_index orelse break :blk; | 1634 | const bss_shndx = self.bss_zig_section_index orelse break :blk; |
| 1712 | const data_phndx = self.phdr_to_shdr_table.get(data_shndx).?; | 1635 | const data_phndx = self.phdr_to_shdr_table.get(data_shndx).?; |
| 1713 | const bss_phndx = self.phdr_to_shdr_table.get(bss_shndx).?; | 1636 | const bss_phndx = self.phdr_to_shdr_table.get(bss_shndx).?; |
| 1714 | self.shdrs.items[bss_shndx].sh_offset = self.shdrs.items[data_shndx].sh_offset; | 1637 | self.shdrs.items[bss_shndx].sh_offset = self.shdrs.items[data_shndx].sh_offset; |
| 1715 | self.phdrs.items[bss_phndx].p_offset = self.phdrs.items[data_phndx].p_offset; | 1638 | self.phdrs.items[bss_phndx].p_offset = self.phdrs.items[data_phndx].p_offset; |
| 1716 | } | 1639 | } |
| 1717 | | 1640 | |
| 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 | | | |
| 1738 | const zig_module = self.file(index).?.zig_module; | 1641 | const zig_module = self.file(index).?.zig_module; |
| 1739 | for (zig_module.atoms.items) |atom_index| { | 1642 | for (zig_module.atoms.items) |atom_index| { |
| 1740 | const atom_ptr = self.atom(atom_index) orelse continue; | 1643 | 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 | ... | @@ -1767,7 +1670,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1767 | if (self.debug_info_header_dirty) { | 1670 | if (self.debug_info_header_dirty) { |
| 1768 | // Currently only one compilation unit is supported, so the address range is simply | 1671 | // Currently only one compilation unit is supported, so the address range is simply |
| 1769 | // identical to the main program header virtual address and memory size. | 1672 | // 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.?]; |
| 1771 | const low_pc = text_phdr.p_vaddr; | 1674 | const low_pc = text_phdr.p_vaddr; |
| 1772 | const high_pc = text_phdr.p_vaddr + text_phdr.p_memsz; | 1675 | const high_pc = text_phdr.p_vaddr + text_phdr.p_memsz; |
| 1773 | try dw.writeDbgInfoHeader(self.base.options.module.?, low_pc, high_pc); | 1676 | 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 | ... | @@ -1777,7 +1680,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1777 | if (self.debug_aranges_section_dirty) { | 1680 | if (self.debug_aranges_section_dirty) { |
| 1778 | // Currently only one compilation unit is supported, so the address range is simply | 1681 | // Currently only one compilation unit is supported, so the address range is simply |
| 1779 | // identical to the main program header virtual address and memory size. | 1682 | // 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.?]; |
| 1781 | try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz); | 1684 | try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz); |
| 1782 | if (!self.shdr_table_dirty) { | 1685 | if (!self.shdr_table_dirty) { |
| 1783 | // Then it won't get written with the others and we need to do it. | 1686 | // 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) | ... | @@ -3221,24 +3124,24 @@ fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index, code: []const u8) |
| 3221 | const decl = mod.declPtr(decl_index); | 3124 | const decl = mod.declPtr(decl_index); |
| 3222 | const shdr_index = switch (decl.ty.zigTypeTag(mod)) { | 3125 | const shdr_index = switch (decl.ty.zigTypeTag(mod)) { |
| 3223 | // TODO: what if this is a function pointer? | 3126 | // TODO: what if this is a function pointer? |
| 3224 | .Fn => self.text_section_index.?, | 3127 | .Fn => self.text_zig_section_index.?, |
| 3225 | else => blk: { | 3128 | else => blk: { |
| 3226 | if (decl.getOwnedVariable(mod)) |variable| { | 3129 | 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.?; |
| 3228 | if (variable.init.toValue().isUndefDeep(mod)) { | 3131 | if (variable.init.toValue().isUndefDeep(mod)) { |
| 3229 | const mode = self.base.options.optimize_mode; | 3132 | const mode = self.base.options.optimize_mode; |
| 3230 | if (mode == .Debug or mode == .ReleaseSafe) break :blk self.data_section_index.?; | 3133 | if (mode == .Debug or mode == .ReleaseSafe) break :blk self.data_zig_section_index.?; |
| 3231 | break :blk self.bss_section_index.?; | 3134 | break :blk self.bss_zig_section_index.?; |
| 3232 | } | 3135 | } |
| 3233 | // TODO I blatantly copied the logic from the Wasm linker, but is there a less | 3136 | // TODO I blatantly copied the logic from the Wasm linker, but is there a less |
| 3234 | // intrusive check for all zeroes than this? | 3137 | // intrusive check for all zeroes than this? |
| 3235 | const is_all_zeroes = for (code) |byte| { | 3138 | const is_all_zeroes = for (code) |byte| { |
| 3236 | if (byte != 0) break false; | 3139 | if (byte != 0) break false; |
| 3237 | } else true; | 3140 | } else true; |
| 3238 | if (is_all_zeroes) break :blk self.bss_section_index.?; | 3141 | if (is_all_zeroes) break :blk self.bss_zig_section_index.?; |
| 3239 | break :blk self.data_section_index.?; | 3142 | break :blk self.data_zig_section_index.?; |
| 3240 | } | 3143 | } |
| 3241 | break :blk self.rodata_section_index.?; | 3144 | break :blk self.rodata_zig_section_index.?; |
| 3242 | }, | 3145 | }, |
| 3243 | }; | 3146 | }; |
| 3244 | return shdr_index; | 3147 | return shdr_index; |
| ... | @@ -3518,8 +3421,8 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol. | ... | @@ -3518,8 +3421,8 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol. |
| 3518 | }; | 3421 | }; |
| 3519 | | 3422 | |
| 3520 | const output_section_index = switch (sym.kind) { | 3423 | const output_section_index = switch (sym.kind) { |
| 3521 | .code => self.text_section_index.?, | 3424 | .code => self.text_zig_section_index.?, |
| 3522 | .const_data => self.rodata_section_index.?, | 3425 | .const_data => self.rodata_zig_section_index.?, |
| 3523 | }; | 3426 | }; |
| 3524 | const local_sym = self.symbol(symbol_index); | 3427 | const local_sym = self.symbol(symbol_index); |
| 3525 | const phdr_index = self.phdr_to_shdr_table.get(output_section_index).?; | 3428 | 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 | ... | @@ -3565,7 +3468,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 3565 | const index = unnamed_consts.items.len; | 3468 | const index = unnamed_consts.items.len; |
| 3566 | const name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index }); | 3469 | const name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index }); |
| 3567 | defer gpa.free(name); | 3470 | 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))) { |
| 3569 | .ok => |sym_index| sym_index, | 3472 | .ok => |sym_index| sym_index, |
| 3570 | .fail => |em| { | 3473 | .fail => |em| { |
| 3571 | decl.analysis = .codegen_failure; | 3474 | decl.analysis = .codegen_failure; |