| ... | @@ -33,16 +33,16 @@ phdrs: std.ArrayListUnmanaged(elf.Elf64_Phdr) = .{}, | ... | @@ -33,16 +33,16 @@ phdrs: std.ArrayListUnmanaged(elf.Elf64_Phdr) = .{}, |
| 33 | | 33 | |
| 34 | /// Tracked loadable segments during incremental linking. | 34 | /// Tracked loadable segments during incremental linking. |
| 35 | /// The index into the program headers of a PT_LOAD program header with Read and Execute flags | 35 | /// The index into the program headers of a PT_LOAD program header with Read and Execute flags |
| 36 | phdr_load_re_zig_index: ?u16 = null, | 36 | phdr_zig_load_re_index: ?u16 = null, |
| 37 | /// The index into the program headers of the global offset table. | 37 | /// The index into the program headers of the global offset table. |
| 38 | /// It needs PT_LOAD and Read flags. | 38 | /// It needs PT_LOAD and Read flags. |
| 39 | phdr_got_zig_index: ?u16 = null, | 39 | phdr_zig_got_index: ?u16 = null, |
| 40 | /// The index into the program headers of a PT_LOAD program header with Read flag | 40 | /// The index into the program headers of a PT_LOAD program header with Read flag |
| 41 | phdr_load_ro_zig_index: ?u16 = null, | 41 | phdr_zig_load_ro_index: ?u16 = null, |
| 42 | /// The index into the program headers of a PT_LOAD program header with Write flag | 42 | /// The index into the program headers of a PT_LOAD program header with Write flag |
| 43 | phdr_load_rw_zig_index: ?u16 = null, | 43 | phdr_zig_load_rw_index: ?u16 = null, |
| 44 | /// The index into the program headers of a PT_LOAD program header with zerofill data. | 44 | /// The index into the program headers of a PT_LOAD program header with zerofill data. |
| 45 | phdr_load_zerofill_zig_index: ?u16 = null, | 45 | phdr_zig_load_zerofill_index: ?u16 = null, |
| 46 | | 46 | |
| 47 | /// Special program headers | 47 | /// Special program headers |
| 48 | /// PT_PHDR | 48 | /// PT_PHDR |
| ... | @@ -99,13 +99,15 @@ plt_got: PltGotSection = .{}, | ... | @@ -99,13 +99,15 @@ plt_got: PltGotSection = .{}, |
| 99 | copy_rel: CopyRelSection = .{}, | 99 | copy_rel: CopyRelSection = .{}, |
| 100 | /// .rela.plt section | 100 | /// .rela.plt section |
| 101 | rela_plt: std.ArrayListUnmanaged(elf.Elf64_Rela) = .{}, | 101 | rela_plt: std.ArrayListUnmanaged(elf.Elf64_Rela) = .{}, |
| | 102 | /// .zig.got section |
| | 103 | zig_got: ZigGotSection = .{}, |
| 102 | | 104 | |
| 103 | /// Tracked section headers with incremental updates to Zig module | 105 | /// Tracked section headers with incremental updates to Zig module |
| 104 | text_zig_section_index: ?u16 = null, | 106 | zig_text_section_index: ?u16 = null, |
| 105 | rodata_zig_section_index: ?u16 = null, | 107 | zig_rodata_section_index: ?u16 = null, |
| 106 | data_zig_section_index: ?u16 = null, | 108 | zig_data_section_index: ?u16 = null, |
| 107 | bss_zig_section_index: ?u16 = null, | 109 | zig_bss_section_index: ?u16 = null, |
| 108 | got_zig_section_index: ?u16 = null, | 110 | zig_got_section_index: ?u16 = null, |
| 109 | debug_info_section_index: ?u16 = null, | 111 | debug_info_section_index: ?u16 = null, |
| 110 | debug_abbrev_section_index: ?u16 = null, | 112 | debug_abbrev_section_index: ?u16 = null, |
| 111 | debug_str_section_index: ?u16 = null, | 113 | debug_str_section_index: ?u16 = null, |
| ... | @@ -479,7 +481,7 @@ pub fn lowerAnonDecl(self: *Elf, decl_val: InternPool.Index, src_loc: Module.Src | ... | @@ -479,7 +481,7 @@ pub fn lowerAnonDecl(self: *Elf, decl_val: InternPool.Index, src_loc: Module.Src |
| 479 | const tv = TypedValue{ .ty = ty, .val = val }; | 481 | const tv = TypedValue{ .ty = ty, .val = val }; |
| 480 | const name = try std.fmt.allocPrint(gpa, "__anon_{d}", .{@intFromEnum(decl_val)}); | 482 | const name = try std.fmt.allocPrint(gpa, "__anon_{d}", .{@intFromEnum(decl_val)}); |
| 481 | defer gpa.free(name); | 483 | defer gpa.free(name); |
| 482 | const res = self.lowerConst(name, tv, self.rodata_zig_section_index.?, src_loc) catch |err| switch (err) { | 484 | const res = self.lowerConst(name, tv, self.zig_rodata_section_index.?, src_loc) catch |err| switch (err) { |
| 483 | else => { | 485 | else => { |
| 484 | // TODO improve error message | 486 | // TODO improve error message |
| 485 | const em = try Module.ErrorMsg.create(gpa, src_loc, "lowerAnonDecl failed with error: {s}", .{ | 487 | const em = try Module.ErrorMsg.create(gpa, src_loc, "lowerAnonDecl failed with error: {s}", .{ |
| ... | @@ -687,8 +689,8 @@ pub fn initMetadata(self: *Elf) !void { | ... | @@ -687,8 +689,8 @@ pub fn initMetadata(self: *Elf) !void { |
| 687 | const ptr_bit_width = self.base.options.target.ptrBitWidth(); | 689 | const ptr_bit_width = self.base.options.target.ptrBitWidth(); |
| 688 | const is_linux = self.base.options.target.os.tag == .linux; | 690 | const is_linux = self.base.options.target.os.tag == .linux; |
| 689 | | 691 | |
| 690 | if (self.phdr_load_re_zig_index == null) { | 692 | if (self.phdr_zig_load_re_index == null) { |
| 691 | self.phdr_load_re_zig_index = try self.allocateSegment(.{ | 693 | self.phdr_zig_load_re_index = try self.allocateSegment(.{ |
| 692 | .addr = if (ptr_bit_width >= 32) 0x8000000 else 0x8000, | 694 | .addr = if (ptr_bit_width >= 32) 0x8000000 else 0x8000, |
| 693 | .memsz = self.base.options.program_code_size_hint, | 695 | .memsz = self.base.options.program_code_size_hint, |
| 694 | .filesz = self.base.options.program_code_size_hint, | 696 | .filesz = self.base.options.program_code_size_hint, |
| ... | @@ -697,11 +699,11 @@ pub fn initMetadata(self: *Elf) !void { | ... | @@ -697,11 +699,11 @@ pub fn initMetadata(self: *Elf) !void { |
| 697 | }); | 699 | }); |
| 698 | } | 700 | } |
| 699 | | 701 | |
| 700 | if (self.phdr_got_zig_index == null) { | 702 | if (self.phdr_zig_got_index == null) { |
| 701 | // We really only need ptr alignment but since we are using PROGBITS, linux requires | 703 | // We really only need ptr alignment but since we are using PROGBITS, linux requires |
| 702 | // page align. | 704 | // page align. |
| 703 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | 705 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); |
| 704 | self.phdr_got_zig_index = try self.allocateSegment(.{ | 706 | self.phdr_zig_got_index = try self.allocateSegment(.{ |
| 705 | .addr = if (ptr_bit_width >= 32) 0x4000000 else 0x4000, | 707 | .addr = if (ptr_bit_width >= 32) 0x4000000 else 0x4000, |
| 706 | .memsz = @as(u64, ptr_size) * self.base.options.symbol_count_hint, | 708 | .memsz = @as(u64, ptr_size) * self.base.options.symbol_count_hint, |
| 707 | .filesz = @as(u64, ptr_size) * self.base.options.symbol_count_hint, | 709 | .filesz = @as(u64, ptr_size) * self.base.options.symbol_count_hint, |
| ... | @@ -710,9 +712,9 @@ pub fn initMetadata(self: *Elf) !void { | ... | @@ -710,9 +712,9 @@ pub fn initMetadata(self: *Elf) !void { |
| 710 | }); | 712 | }); |
| 711 | } | 713 | } |
| 712 | | 714 | |
| 713 | if (self.phdr_load_ro_zig_index == null) { | 715 | if (self.phdr_zig_load_ro_index == null) { |
| 714 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | 716 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); |
| 715 | self.phdr_load_ro_zig_index = try self.allocateSegment(.{ | 717 | self.phdr_zig_load_ro_index = try self.allocateSegment(.{ |
| 716 | .addr = if (ptr_bit_width >= 32) 0xc000000 else 0xa000, | 718 | .addr = if (ptr_bit_width >= 32) 0xc000000 else 0xa000, |
| 717 | .memsz = 1024, | 719 | .memsz = 1024, |
| 718 | .filesz = 1024, | 720 | .filesz = 1024, |
| ... | @@ -721,9 +723,9 @@ pub fn initMetadata(self: *Elf) !void { | ... | @@ -721,9 +723,9 @@ pub fn initMetadata(self: *Elf) !void { |
| 721 | }); | 723 | }); |
| 722 | } | 724 | } |
| 723 | | 725 | |
| 724 | if (self.phdr_load_rw_zig_index == null) { | 726 | if (self.phdr_zig_load_rw_index == null) { |
| 725 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | 727 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); |
| 726 | self.phdr_load_rw_zig_index = try self.allocateSegment(.{ | 728 | self.phdr_zig_load_rw_index = try self.allocateSegment(.{ |
| 727 | .addr = if (ptr_bit_width >= 32) 0x10000000 else 0xc000, | 729 | .addr = if (ptr_bit_width >= 32) 0x10000000 else 0xc000, |
| 728 | .memsz = 1024, | 730 | .memsz = 1024, |
| 729 | .filesz = 1024, | 731 | .filesz = 1024, |
| ... | @@ -732,64 +734,64 @@ pub fn initMetadata(self: *Elf) !void { | ... | @@ -732,64 +734,64 @@ pub fn initMetadata(self: *Elf) !void { |
| 732 | }); | 734 | }); |
| 733 | } | 735 | } |
| 734 | | 736 | |
| 735 | if (self.phdr_load_zerofill_zig_index == null) { | 737 | if (self.phdr_zig_load_zerofill_index == null) { |
| 736 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | 738 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); |
| 737 | self.phdr_load_zerofill_zig_index = try self.allocateSegment(.{ | 739 | self.phdr_zig_load_zerofill_index = try self.allocateSegment(.{ |
| 738 | .addr = if (ptr_bit_width >= 32) 0x14000000 else 0xf000, | 740 | .addr = if (ptr_bit_width >= 32) 0x14000000 else 0xf000, |
| 739 | .memsz = 0, | 741 | .memsz = 0, |
| 740 | .filesz = 0, | 742 | .filesz = 0, |
| 741 | .alignment = alignment, | 743 | .alignment = alignment, |
| 742 | .flags = elf.PF_R | elf.PF_W, | 744 | .flags = elf.PF_R | elf.PF_W, |
| 743 | }); | 745 | }); |
| 744 | const phdr = &self.phdrs.items[self.phdr_load_zerofill_zig_index.?]; | 746 | const phdr = &self.phdrs.items[self.phdr_zig_load_zerofill_index.?]; |
| 745 | phdr.p_offset = self.phdrs.items[self.phdr_load_rw_zig_index.?].p_offset; // .bss overlaps .data | 747 | phdr.p_offset = self.phdrs.items[self.phdr_zig_load_rw_index.?].p_offset; // .bss overlaps .data |
| 746 | phdr.p_memsz = 1024; | 748 | phdr.p_memsz = 1024; |
| 747 | } | 749 | } |
| 748 | | 750 | |
| 749 | if (self.text_zig_section_index == null) { | 751 | if (self.zig_text_section_index == null) { |
| 750 | self.text_zig_section_index = try self.allocateAllocSection(.{ | 752 | self.zig_text_section_index = try self.allocateAllocSection(.{ |
| 751 | .name = ".text.zig", | 753 | .name = ".zig.text", |
| 752 | .phdr_index = self.phdr_load_re_zig_index.?, | 754 | .phdr_index = self.phdr_zig_load_re_index.?, |
| 753 | .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, | 755 | .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| 754 | }); | 756 | }); |
| 755 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.text_zig_section_index.?, .{}); | 757 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_text_section_index.?, .{}); |
| 756 | } | 758 | } |
| 757 | | 759 | |
| 758 | if (self.got_zig_section_index == null) { | 760 | if (self.zig_got_section_index == null) { |
| 759 | self.got_zig_section_index = try self.allocateAllocSection(.{ | 761 | self.zig_got_section_index = try self.allocateAllocSection(.{ |
| 760 | .name = ".got.zig", | 762 | .name = ".zig.got", |
| 761 | .phdr_index = self.phdr_got_zig_index.?, | 763 | .phdr_index = self.phdr_zig_got_index.?, |
| 762 | .alignment = ptr_size, | 764 | .alignment = ptr_size, |
| 763 | }); | 765 | }); |
| 764 | } | 766 | } |
| 765 | | 767 | |
| 766 | if (self.rodata_zig_section_index == null) { | 768 | if (self.zig_rodata_section_index == null) { |
| 767 | self.rodata_zig_section_index = try self.allocateAllocSection(.{ | 769 | self.zig_rodata_section_index = try self.allocateAllocSection(.{ |
| 768 | .name = ".rodata.zig", | 770 | .name = ".zig.rodata", |
| 769 | .phdr_index = self.phdr_load_ro_zig_index.?, | 771 | .phdr_index = self.phdr_zig_load_ro_index.?, |
| 770 | }); | 772 | }); |
| 771 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.rodata_zig_section_index.?, .{}); | 773 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_rodata_section_index.?, .{}); |
| 772 | } | 774 | } |
| 773 | | 775 | |
| 774 | if (self.data_zig_section_index == null) { | 776 | if (self.zig_data_section_index == null) { |
| 775 | self.data_zig_section_index = try self.allocateAllocSection(.{ | 777 | self.zig_data_section_index = try self.allocateAllocSection(.{ |
| 776 | .name = ".data.zig", | 778 | .name = ".zig.data", |
| 777 | .phdr_index = self.phdr_load_rw_zig_index.?, | 779 | .phdr_index = self.phdr_zig_load_rw_index.?, |
| 778 | .alignment = ptr_size, | 780 | .alignment = ptr_size, |
| 779 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, | 781 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 780 | }); | 782 | }); |
| 781 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.data_zig_section_index.?, .{}); | 783 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_data_section_index.?, .{}); |
| 782 | } | 784 | } |
| 783 | | 785 | |
| 784 | if (self.bss_zig_section_index == null) { | 786 | if (self.zig_bss_section_index == null) { |
| 785 | self.bss_zig_section_index = try self.allocateAllocSection(.{ | 787 | self.zig_bss_section_index = try self.allocateAllocSection(.{ |
| 786 | .name = ".bss.zig", | 788 | .name = ".bss.zig", |
| 787 | .phdr_index = self.phdr_load_zerofill_zig_index.?, | 789 | .phdr_index = self.phdr_zig_load_zerofill_index.?, |
| 788 | .alignment = ptr_size, | 790 | .alignment = ptr_size, |
| 789 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, | 791 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 790 | .type = elf.SHT_NOBITS, | 792 | .type = elf.SHT_NOBITS, |
| 791 | }); | 793 | }); |
| 792 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.bss_zig_section_index.?, .{}); | 794 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_bss_section_index.?, .{}); |
| 793 | } | 795 | } |
| 794 | | 796 | |
| 795 | if (self.dwarf) |*dw| { | 797 | if (self.dwarf) |*dw| { |
| ... | @@ -875,7 +877,7 @@ pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void { | ... | @@ -875,7 +877,7 @@ pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void { |
| 875 | } | 877 | } |
| 876 | | 878 | |
| 877 | const mem_capacity = self.allocatedVirtualSize(phdr.p_vaddr); | 879 | const mem_capacity = self.allocatedVirtualSize(phdr.p_vaddr); |
| 878 | if (needed_size <= mem_capacity) { | 880 | if (needed_size > mem_capacity) { |
| 879 | var err = try self.addErrorWithNotes(2); | 881 | var err = try self.addErrorWithNotes(2); |
| 880 | try err.addMsg(self, "fatal linker error: cannot expand load segment phdr({d}) in virtual memory", .{ | 882 | try err.addMsg(self, "fatal linker error: cannot expand load segment phdr({d}) in virtual memory", .{ |
| 881 | phdr_index, | 883 | phdr_index, |
| ... | @@ -1544,8 +1546,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node | ... | @@ -1544,8 +1546,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1544 | if (self.zig_module_index) |index| { | 1546 | if (self.zig_module_index) |index| { |
| 1545 | // .bss.zig always overlaps .data.zig in file offset, but is zero-sized in file so it doesn't | 1547 | // .bss.zig always overlaps .data.zig in file offset, but is zero-sized in file so it doesn't |
| 1546 | // get mapped by the loader | 1548 | // get mapped by the loader |
| 1547 | if (self.data_zig_section_index) |data_shndx| blk: { | 1549 | if (self.zig_data_section_index) |data_shndx| blk: { |
| 1548 | const bss_shndx = self.bss_zig_section_index orelse break :blk; | 1550 | const bss_shndx = self.zig_bss_section_index orelse break :blk; |
| 1549 | const data_phndx = self.phdr_to_shdr_table.get(data_shndx).?; | 1551 | const data_phndx = self.phdr_to_shdr_table.get(data_shndx).?; |
| 1550 | const bss_phndx = self.phdr_to_shdr_table.get(bss_shndx).?; | 1552 | const bss_phndx = self.phdr_to_shdr_table.get(bss_shndx).?; |
| 1551 | self.shdrs.items[bss_shndx].sh_offset = self.shdrs.items[data_shndx].sh_offset; | 1553 | self.shdrs.items[bss_shndx].sh_offset = self.shdrs.items[data_shndx].sh_offset; |
| ... | @@ -1580,7 +1582,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node | ... | @@ -1580,7 +1582,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1580 | if (self.debug_info_header_dirty) { | 1582 | if (self.debug_info_header_dirty) { |
| 1581 | // Currently only one compilation unit is supported, so the address range is simply | 1583 | // Currently only one compilation unit is supported, so the address range is simply |
| 1582 | // identical to the main program header virtual address and memory size. | 1584 | // identical to the main program header virtual address and memory size. |
| 1583 | const text_phdr = &self.phdrs.items[self.phdr_load_re_zig_index.?]; | 1585 | const text_phdr = &self.phdrs.items[self.phdr_zig_load_re_index.?]; |
| 1584 | const low_pc = text_phdr.p_vaddr; | 1586 | const low_pc = text_phdr.p_vaddr; |
| 1585 | const high_pc = text_phdr.p_vaddr + text_phdr.p_memsz; | 1587 | const high_pc = text_phdr.p_vaddr + text_phdr.p_memsz; |
| 1586 | try dw.writeDbgInfoHeader(self.base.options.module.?, low_pc, high_pc); | 1588 | try dw.writeDbgInfoHeader(self.base.options.module.?, low_pc, high_pc); |
| ... | @@ -1590,7 +1592,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node | ... | @@ -1590,7 +1592,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1590 | if (self.debug_aranges_section_dirty) { | 1592 | if (self.debug_aranges_section_dirty) { |
| 1591 | // Currently only one compilation unit is supported, so the address range is simply | 1593 | // Currently only one compilation unit is supported, so the address range is simply |
| 1592 | // identical to the main program header virtual address and memory size. | 1594 | // identical to the main program header virtual address and memory size. |
| 1593 | const text_phdr = &self.phdrs.items[self.phdr_load_re_zig_index.?]; | 1595 | const text_phdr = &self.phdrs.items[self.phdr_zig_load_re_index.?]; |
| 1594 | try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz); | 1596 | try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz); |
| 1595 | self.debug_aranges_section_dirty = false; | 1597 | self.debug_aranges_section_dirty = false; |
| 1596 | } | 1598 | } |
| ... | @@ -2999,24 +3001,24 @@ fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index, code: []const u8) | ... | @@ -2999,24 +3001,24 @@ fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index, code: []const u8) |
| 2999 | const decl = mod.declPtr(decl_index); | 3001 | const decl = mod.declPtr(decl_index); |
| 3000 | const shdr_index = switch (decl.ty.zigTypeTag(mod)) { | 3002 | const shdr_index = switch (decl.ty.zigTypeTag(mod)) { |
| 3001 | // TODO: what if this is a function pointer? | 3003 | // TODO: what if this is a function pointer? |
| 3002 | .Fn => self.text_zig_section_index.?, | 3004 | .Fn => self.zig_text_section_index.?, |
| 3003 | else => blk: { | 3005 | else => blk: { |
| 3004 | if (decl.getOwnedVariable(mod)) |variable| { | 3006 | if (decl.getOwnedVariable(mod)) |variable| { |
| 3005 | if (variable.is_const) break :blk self.rodata_zig_section_index.?; | 3007 | if (variable.is_const) break :blk self.zig_rodata_section_index.?; |
| 3006 | if (variable.init.toValue().isUndefDeep(mod)) { | 3008 | if (variable.init.toValue().isUndefDeep(mod)) { |
| 3007 | const mode = self.base.options.optimize_mode; | 3009 | const mode = self.base.options.optimize_mode; |
| 3008 | if (mode == .Debug or mode == .ReleaseSafe) break :blk self.data_zig_section_index.?; | 3010 | if (mode == .Debug or mode == .ReleaseSafe) break :blk self.zig_data_section_index.?; |
| 3009 | break :blk self.bss_zig_section_index.?; | 3011 | break :blk self.zig_bss_section_index.?; |
| 3010 | } | 3012 | } |
| 3011 | // TODO I blatantly copied the logic from the Wasm linker, but is there a less | 3013 | // TODO I blatantly copied the logic from the Wasm linker, but is there a less |
| 3012 | // intrusive check for all zeroes than this? | 3014 | // intrusive check for all zeroes than this? |
| 3013 | const is_all_zeroes = for (code) |byte| { | 3015 | const is_all_zeroes = for (code) |byte| { |
| 3014 | if (byte != 0) break false; | 3016 | if (byte != 0) break false; |
| 3015 | } else true; | 3017 | } else true; |
| 3016 | if (is_all_zeroes) break :blk self.bss_zig_section_index.?; | 3018 | if (is_all_zeroes) break :blk self.zig_bss_section_index.?; |
| 3017 | break :blk self.data_zig_section_index.?; | 3019 | break :blk self.zig_data_section_index.?; |
| 3018 | } | 3020 | } |
| 3019 | break :blk self.rodata_zig_section_index.?; | 3021 | break :blk self.zig_rodata_section_index.?; |
| 3020 | }, | 3022 | }, |
| 3021 | }; | 3023 | }; |
| 3022 | return shdr_index; | 3024 | return shdr_index; |
| ... | @@ -3070,8 +3072,9 @@ fn updateDeclCode( | ... | @@ -3070,8 +3072,9 @@ fn updateDeclCode( |
| 3070 | esym.st_value = atom_ptr.value; | 3072 | esym.st_value = atom_ptr.value; |
| 3071 | | 3073 | |
| 3072 | log.debug(" (writing new offset table entry)", .{}); | 3074 | log.debug(" (writing new offset table entry)", .{}); |
| 3073 | // const extra = sym.extra(self).?; | 3075 | assert(sym.flags.has_zig_got); |
| 3074 | // try self.got.writeEntry(self, extra.got); | 3076 | const extra = sym.extra(self).?; |
| | 3077 | try self.zig_got.writeOne(self, extra.zig_got); |
| 3075 | } | 3078 | } |
| 3076 | } else if (code.len < old_size) { | 3079 | } else if (code.len < old_size) { |
| 3077 | atom_ptr.shrink(self); | 3080 | atom_ptr.shrink(self); |
| ... | @@ -3083,10 +3086,8 @@ fn updateDeclCode( | ... | @@ -3083,10 +3086,8 @@ fn updateDeclCode( |
| 3083 | sym.value = atom_ptr.value; | 3086 | sym.value = atom_ptr.value; |
| 3084 | esym.st_value = atom_ptr.value; | 3087 | esym.st_value = atom_ptr.value; |
| 3085 | | 3088 | |
| 3086 | sym.flags.needs_got = true; | 3089 | const gop = try sym.getOrCreateZigGotEntry(sym_index, self); |
| 3087 | const gop = try sym.getOrCreateGotEntry(sym_index, self); | 3090 | try self.zig_got.writeOne(self, gop.index); |
| 3088 | _ = gop; | | |
| 3089 | // try self.got.writeEntry(self, gop.index); | | |
| 3090 | } | 3091 | } |
| 3091 | | 3092 | |
| 3092 | if (self.base.child_pid) |pid| { | 3093 | if (self.base.child_pid) |pid| { |
| ... | @@ -3296,8 +3297,8 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol. | ... | @@ -3296,8 +3297,8 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol. |
| 3296 | }; | 3297 | }; |
| 3297 | | 3298 | |
| 3298 | const output_section_index = switch (sym.kind) { | 3299 | const output_section_index = switch (sym.kind) { |
| 3299 | .code => self.text_zig_section_index.?, | 3300 | .code => self.zig_text_section_index.?, |
| 3300 | .const_data => self.rodata_zig_section_index.?, | 3301 | .const_data => self.zig_rodata_section_index.?, |
| 3301 | }; | 3302 | }; |
| 3302 | const local_sym = self.symbol(symbol_index); | 3303 | const local_sym = self.symbol(symbol_index); |
| 3303 | const phdr_index = self.phdr_to_shdr_table.get(output_section_index).?; | 3304 | const phdr_index = self.phdr_to_shdr_table.get(output_section_index).?; |
| ... | @@ -3320,10 +3321,8 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol. | ... | @@ -3320,10 +3321,8 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol. |
| 3320 | local_sym.value = atom_ptr.value; | 3321 | local_sym.value = atom_ptr.value; |
| 3321 | local_esym.st_value = atom_ptr.value; | 3322 | local_esym.st_value = atom_ptr.value; |
| 3322 | | 3323 | |
| 3323 | local_sym.flags.needs_got = true; | 3324 | const gop = try local_sym.getOrCreateZigGotEntry(symbol_index, self); |
| 3324 | const gop = try local_sym.getOrCreateGotEntry(symbol_index, self); | 3325 | try self.zig_got.writeOne(self, gop.index); |
| 3325 | _ = gop; | | |
| 3326 | // try self.got.writeEntry(self, gop.index); | | |
| 3327 | | 3326 | |
| 3328 | const section_offset = atom_ptr.value - self.phdrs.items[phdr_index].p_vaddr; | 3327 | const section_offset = atom_ptr.value - self.phdrs.items[phdr_index].p_vaddr; |
| 3329 | const file_offset = self.shdrs.items[output_section_index].sh_offset + section_offset; | 3328 | const file_offset = self.shdrs.items[output_section_index].sh_offset + section_offset; |
| ... | @@ -3343,7 +3342,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module | ... | @@ -3343,7 +3342,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 3343 | const index = unnamed_consts.items.len; | 3342 | const index = unnamed_consts.items.len; |
| 3344 | const name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index }); | 3343 | const name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index }); |
| 3345 | defer gpa.free(name); | 3344 | defer gpa.free(name); |
| 3346 | const sym_index = switch (try self.lowerConst(name, typed_value, self.rodata_zig_section_index.?, decl.srcLoc(mod))) { | 3345 | const sym_index = switch (try self.lowerConst(name, typed_value, self.zig_rodata_section_index.?, decl.srcLoc(mod))) { |
| 3347 | .ok => |sym_index| sym_index, | 3346 | .ok => |sym_index| sym_index, |
| 3348 | .fail => |em| { | 3347 | .fail => |em| { |
| 3349 | decl.analysis = .codegen_failure; | 3348 | decl.analysis = .codegen_failure; |
| ... | @@ -4183,11 +4182,11 @@ fn sortSections(self: *Elf) !void { | ... | @@ -4183,11 +4182,11 @@ fn sortSections(self: *Elf) !void { |
| 4183 | &self.copy_rel_section_index, | 4182 | &self.copy_rel_section_index, |
| 4184 | &self.versym_section_index, | 4183 | &self.versym_section_index, |
| 4185 | &self.verneed_section_index, | 4184 | &self.verneed_section_index, |
| 4186 | &self.text_zig_section_index, | 4185 | &self.zig_text_section_index, |
| 4187 | &self.got_zig_section_index, | 4186 | &self.zig_got_section_index, |
| 4188 | &self.rodata_zig_section_index, | 4187 | &self.zig_rodata_section_index, |
| 4189 | &self.data_zig_section_index, | 4188 | &self.zig_data_section_index, |
| 4190 | &self.bss_zig_section_index, | 4189 | &self.zig_bss_section_index, |
| 4191 | &self.debug_str_section_index, | 4190 | &self.debug_str_section_index, |
| 4192 | &self.debug_info_section_index, | 4191 | &self.debug_info_section_index, |
| 4193 | &self.debug_abbrev_section_index, | 4192 | &self.debug_abbrev_section_index, |
| ... | @@ -4340,6 +4339,9 @@ fn updateSectionSizes(self: *Elf) !void { | ... | @@ -4340,6 +4339,9 @@ fn updateSectionSizes(self: *Elf) !void { |
| 4340 | if (self.strtab_section_index) |index| { | 4339 | if (self.strtab_section_index) |index| { |
| 4341 | // TODO I don't really this here but we need it to add symbol names from GOT and other synthetic | 4340 | // TODO I don't really this here but we need it to add symbol names from GOT and other synthetic |
| 4342 | // sections into .strtab for easier debugging. | 4341 | // sections into .strtab for easier debugging. |
| | 4342 | if (self.zig_got_section_index) |_| { |
| | 4343 | try self.zig_got.updateStrtab(self); |
| | 4344 | } |
| 4343 | if (self.got_section_index) |_| { | 4345 | if (self.got_section_index) |_| { |
| 4344 | try self.got.updateStrtab(self); | 4346 | try self.got.updateStrtab(self); |
| 4345 | } | 4347 | } |
| ... | @@ -4719,6 +4721,11 @@ fn updateSymtabSize(self: *Elf) !void { | ... | @@ -4719,6 +4721,11 @@ fn updateSymtabSize(self: *Elf) !void { |
| 4719 | sizes.nglobals += shared_object.output_symtab_size.nglobals; | 4721 | sizes.nglobals += shared_object.output_symtab_size.nglobals; |
| 4720 | } | 4722 | } |
| 4721 | | 4723 | |
| | 4724 | if (self.zig_got_section_index) |_| { |
| | 4725 | self.zig_got.updateSymtabSize(self); |
| | 4726 | sizes.nlocals += self.zig_got.output_symtab_size.nlocals; |
| | 4727 | } |
| | 4728 | |
| 4722 | if (self.got_section_index) |_| { | 4729 | if (self.got_section_index) |_| { |
| 4723 | self.got.updateSymtabSize(self); | 4730 | self.got.updateSymtabSize(self); |
| 4724 | sizes.nlocals += self.got.output_symtab_size.nlocals; | 4731 | sizes.nlocals += self.got.output_symtab_size.nlocals; |
| ... | @@ -4930,6 +4937,11 @@ fn writeSymtab(self: *Elf) !void { | ... | @@ -4930,6 +4937,11 @@ fn writeSymtab(self: *Elf) !void { |
| 4930 | ctx.iglobal += shared_object.output_symtab_size.nglobals; | 4937 | ctx.iglobal += shared_object.output_symtab_size.nglobals; |
| 4931 | } | 4938 | } |
| 4932 | | 4939 | |
| | 4940 | if (self.zig_got_section_index) |_| { |
| | 4941 | try self.zig_got.writeSymtab(self, ctx); |
| | 4942 | ctx.ilocal += self.zig_got.output_symtab_size.nlocals; |
| | 4943 | } |
| | 4944 | |
| 4933 | if (self.got_section_index) |_| { | 4945 | if (self.got_section_index) |_| { |
| 4934 | try self.got.writeSymtab(self, ctx); | 4946 | try self.got.writeSymtab(self, ctx); |
| 4935 | ctx.ilocal += self.got.output_symtab_size.nlocals; | 4947 | ctx.ilocal += self.got.output_symtab_size.nlocals; |
| ... | @@ -5289,11 +5301,11 @@ pub fn isDynLib(self: Elf) bool { | ... | @@ -5289,11 +5301,11 @@ pub fn isDynLib(self: Elf) bool { |
| 5289 | | 5301 | |
| 5290 | pub fn isZigSection(self: Elf, shndx: u16) bool { | 5302 | pub fn isZigSection(self: Elf, shndx: u16) bool { |
| 5291 | inline for (&[_]?u16{ | 5303 | inline for (&[_]?u16{ |
| 5292 | self.text_zig_section_index, | 5304 | self.zig_text_section_index, |
| 5293 | self.rodata_zig_section_index, | 5305 | self.zig_rodata_section_index, |
| 5294 | self.data_zig_section_index, | 5306 | self.zig_data_section_index, |
| 5295 | self.bss_zig_section_index, | 5307 | self.zig_bss_section_index, |
| 5296 | self.got_zig_section_index, | 5308 | self.zig_got_section_index, |
| 5297 | }) |maybe_index| { | 5309 | }) |maybe_index| { |
| 5298 | if (maybe_index) |index| { | 5310 | if (maybe_index) |index| { |
| 5299 | if (index == shndx) return true; | 5311 | if (index == shndx) return true; |
| ... | @@ -5851,6 +5863,7 @@ fn fmtDumpState( | ... | @@ -5851,6 +5863,7 @@ fn fmtDumpState( |
| 5851 | try writer.print("{}\n", .{linker_defined.fmtSymtab(self)}); | 5863 | try writer.print("{}\n", .{linker_defined.fmtSymtab(self)}); |
| 5852 | } | 5864 | } |
| 5853 | try writer.print("{}\n", .{self.got.fmt(self)}); | 5865 | try writer.print("{}\n", .{self.got.fmt(self)}); |
| | 5866 | try writer.print("{}\n", .{self.zig_got.fmt(self)}); |
| 5854 | try writer.writeAll("Output shdrs\n"); | 5867 | try writer.writeAll("Output shdrs\n"); |
| 5855 | for (self.shdrs.items, 0..) |shdr, shndx| { | 5868 | for (self.shdrs.items, 0..) |shdr, shndx| { |
| 5856 | try writer.print("shdr({d}) : phdr({?d}) : {}\n", .{ | 5869 | try writer.print("shdr({d}) : phdr({?d}) : {}\n", .{ |
| ... | @@ -6030,4 +6043,5 @@ const Type = @import("../type.zig").Type; | ... | @@ -6030,4 +6043,5 @@ const Type = @import("../type.zig").Type; |
| 6030 | const TypedValue = @import("../TypedValue.zig"); | 6043 | const TypedValue = @import("../TypedValue.zig"); |
| 6031 | const Value = @import("../value.zig").Value; | 6044 | const Value = @import("../value.zig").Value; |
| 6032 | const VerneedSection = synthetic_sections.VerneedSection; | 6045 | const VerneedSection = synthetic_sections.VerneedSection; |
| | 6046 | const ZigGotSection = synthetic_sections.ZigGotSection; |
| 6033 | const ZigModule = @import("Elf/ZigModule.zig"); | 6047 | const ZigModule = @import("Elf/ZigModule.zig"); |