authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-21 20:58:43+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-21 20:58:43+01:00
log60a8f9b989d64bb6dfbb9e85cd0dd4e1b41750e1
tree5e62c44c20dc747fcbe2b798de5c965150fff8e5
parent775a161794c9486b7866b27d364acea1cb78b6cd

elf: make GOT arch aware when resolving relocs


2 files changed, 11 insertions(+), 18 deletions(-)

src/link/Elf.zig+9
...@@ -5550,6 +5550,15 @@ pub fn comdatGroupOwner(self: *Elf, index: ComdatGroupOwner.Index) *ComdatGroupO...@@ -5550,6 +5550,15 @@ pub fn comdatGroupOwner(self: *Elf, index: ComdatGroupOwner.Index) *ComdatGroupO
5550 return &self.comdat_groups_owners.items[index];5550 return &self.comdat_groups_owners.items[index];
5551}5551}
55525552
5553pub fn gotAddress(self: *Elf) u64 {
5554 const shndx = blk: {
5555 if (self.getTarget().cpu.arch == .x86_64 and self.got_plt_section_index != null)
5556 break :blk self.got_plt_section_index.?;
5557 break :blk if (self.got_section_index) |shndx| shndx else null;
5558 };
5559 return if (shndx) |index| self.shdrs.items[index].sh_addr else 0;
5560}
5561
5553pub fn tpAddress(self: *Elf) u64 {5562pub fn tpAddress(self: *Elf) u64 {
5554 const index = self.phdr_tls_index orelse return 0;5563 const index = self.phdr_tls_index orelse return 0;
5555 const phdr = self.phdrs.items[index];5564 const phdr = self.phdrs.items[index];
src/link/Elf/Atom.zig+2-18
...@@ -731,15 +731,7 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) RelocError!voi...@@ -731,15 +731,7 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) RelocError!voi
731 // Address of the target symbol - can be address of the symbol within an atom or address of PLT stub.731 // Address of the target symbol - can be address of the symbol within an atom or address of PLT stub.
732 const S = @as(i64, @intCast(target.address(.{}, elf_file)));732 const S = @as(i64, @intCast(target.address(.{}, elf_file)));
733 // Address of the global offset table.733 // Address of the global offset table.
734 const GOT = blk: {734 const GOT = @as(i64, @intCast(elf_file.gotAddress()));
735 const shndx = if (elf_file.got_plt_section_index) |shndx|
736 shndx
737 else if (elf_file.got_section_index) |shndx|
738 shndx
739 else
740 null;
741 break :blk if (shndx) |index| @as(i64, @intCast(elf_file.shdrs.items[index].sh_addr)) else 0;
742 };
743 // Address of the .zig.got table entry if any.735 // Address of the .zig.got table entry if any.
744 const ZIG_GOT = @as(i64, @intCast(target.zigGotAddress(elf_file)));736 const ZIG_GOT = @as(i64, @intCast(target.zigGotAddress(elf_file)));
745 // Relative offset to the start of the global offset table.737 // Relative offset to the start of the global offset table.
...@@ -924,15 +916,7 @@ pub fn resolveRelocsNonAlloc(self: Atom, elf_file: *Elf, code: []u8, undefs: any...@@ -924,15 +916,7 @@ pub fn resolveRelocsNonAlloc(self: Atom, elf_file: *Elf, code: []u8, undefs: any
924 // Address of the target symbol - can be address of the symbol within an atom or address of PLT stub.916 // Address of the target symbol - can be address of the symbol within an atom or address of PLT stub.
925 const S = @as(i64, @intCast(target.address(.{}, elf_file)));917 const S = @as(i64, @intCast(target.address(.{}, elf_file)));
926 // Address of the global offset table.918 // Address of the global offset table.
927 const GOT = blk: {919 const GOT = @as(i64, @intCast(elf_file.gotAddress()));
928 const shndx = if (elf_file.got_plt_section_index) |shndx|
929 shndx
930 else if (elf_file.got_section_index) |shndx|
931 shndx
932 else
933 null;
934 break :blk if (shndx) |index| @as(i64, @intCast(elf_file.shdrs.items[index].sh_addr)) else 0;
935 };
936 // Address of the dynamic thread pointer.920 // Address of the dynamic thread pointer.
937 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));921 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));
938922