Skip to content

Commit

Permalink
[lld/ELF][X86] Respect outSecOff when checking if GOTPCREL can be rel…
Browse files Browse the repository at this point in the history
…axed (llvm#86334)

The existing implementation didn't handle when the input text section
was some offset from the output section.

This resulted in an assert in relaxGot() with an lld built with asserts
for some large binaries, or even worse, a silently broken binary with an
lld without asserts.
  • Loading branch information
aeubanks committed Mar 24, 2024
1 parent 9632e15 commit 4804805
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lld/ELF/Arch/X86_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,10 @@ bool X86_64::relaxOnce(int pass) const {
if (rel.expr != R_RELAX_GOT_PC)
continue;

uint64_t v = sec->getRelocTargetVA(
sec->file, rel.type, rel.addend,
sec->getOutputSection()->addr + rel.offset, *rel.sym, rel.expr);
uint64_t v = sec->getRelocTargetVA(sec->file, rel.type, rel.addend,
sec->getOutputSection()->addr +
sec->outSecOff + rel.offset,
*rel.sym, rel.expr);
if (isInt<32>(v))
continue;
if (rel.sym->auxIdx == 0) {
Expand Down
12 changes: 11 additions & 1 deletion lld/test/ELF/x86-64-gotpc-relax-too-far.s
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
# RUN: llvm-objdump --no-print-imm-hex -d %t/bin | FileCheck --check-prefix=DISASM %s
# RUN: llvm-readelf -S %t/bin | FileCheck --check-prefixes=GOT %s
# RUN: ld.lld -T %t/lds2 %t/a.o -o %t/bin2
# RUN: llvm-readelf -S %t/bin2 | FileCheck --check-prefixes=UNNECESSARY-GOT %s
# RUN: llvm-objdump --no-print-imm-hex -d %t/bin2 | FileCheck --check-prefix=DISASM %s
# RUN: llvm-readelf -S %t/bin2 | FileCheck --check-prefixes=GOT %s
# RUN: ld.lld -T %t/lds3 %t/a.o -o %t/bin3
# RUN: llvm-readelf -S %t/bin3 | FileCheck --check-prefixes=UNNECESSARY-GOT %s

# DISASM: <_foo>:
# DISASM-NEXT: movl 2097146(%rip), %eax
Expand Down Expand Up @@ -47,6 +50,13 @@ SECTIONS {
data 0x80200000 : { *(data) }
}
#--- lds2
SECTIONS {
.text.foo 0x100000 : { *(.text.foo) }
.text 0x1ff000 : { . = . + 0x1000 ; *(.text) }
.got 0x300000 : { *(.got) }
data 0x80200000 : { *(data) }
}
#--- lds3
SECTIONS {
.text.foo 0x100000 : { *(.text.foo) }
.text 0x200000 : { *(.text) }
Expand Down

0 comments on commit 4804805

Please sign in to comment.