Skip to content

Commit 767b61c

Browse files
aeubankststellar
authored andcommitted
[lld/ELF][X86] Respect outSecOff when checking if GOTPCREL can be relaxed (#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. (cherry picked from commit 4804805)
1 parent 7c7c3d4 commit 767b61c

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lld/ELF/Arch/X86_64.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,10 @@ bool X86_64::relaxOnce(int pass) const {
328328
if (rel.expr != R_RELAX_GOT_PC)
329329
continue;
330330

331-
uint64_t v = sec->getRelocTargetVA(
332-
sec->file, rel.type, rel.addend,
333-
sec->getOutputSection()->addr + rel.offset, *rel.sym, rel.expr);
331+
uint64_t v = sec->getRelocTargetVA(sec->file, rel.type, rel.addend,
332+
sec->getOutputSection()->addr +
333+
sec->outSecOff + rel.offset,
334+
*rel.sym, rel.expr);
334335
if (isInt<32>(v))
335336
continue;
336337
if (rel.sym->auxIdx == 0) {

lld/test/ELF/x86-64-gotpc-relax-too-far.s

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
# RUN: llvm-objdump --no-print-imm-hex -d %t/bin | FileCheck --check-prefix=DISASM %s
66
# RUN: llvm-readelf -S %t/bin | FileCheck --check-prefixes=GOT %s
77
# RUN: ld.lld -T %t/lds2 %t/a.o -o %t/bin2
8-
# RUN: llvm-readelf -S %t/bin2 | FileCheck --check-prefixes=UNNECESSARY-GOT %s
8+
# RUN: llvm-objdump --no-print-imm-hex -d %t/bin2 | FileCheck --check-prefix=DISASM %s
9+
# RUN: llvm-readelf -S %t/bin2 | FileCheck --check-prefixes=GOT %s
10+
# RUN: ld.lld -T %t/lds3 %t/a.o -o %t/bin3
11+
# RUN: llvm-readelf -S %t/bin3 | FileCheck --check-prefixes=UNNECESSARY-GOT %s
912

1013
# DISASM: <_foo>:
1114
# DISASM-NEXT: movl 2097146(%rip), %eax
@@ -47,6 +50,13 @@ SECTIONS {
4750
data 0x80200000 : { *(data) }
4851
}
4952
#--- lds2
53+
SECTIONS {
54+
.text.foo 0x100000 : { *(.text.foo) }
55+
.text 0x1ff000 : { . = . + 0x1000 ; *(.text) }
56+
.got 0x300000 : { *(.got) }
57+
data 0x80200000 : { *(data) }
58+
}
59+
#--- lds3
5060
SECTIONS {
5161
.text.foo 0x100000 : { *(.text.foo) }
5262
.text 0x200000 : { *(.text) }

0 commit comments

Comments
 (0)