[ELF] Write R_X86_64_IRELATIVE addends with -z rel

I found this missing case with the new --check-dynamic-relocation flag
while running the lld tests with --apply-dynamic-relocs enabled by default.

This also fixes a broken CHECK in lld/test/ELF/x86-64-gotpc-relax.s:
The test wasn't using CHECK-NEXT, so it was passing despite the output
actually containing relocations. I am not sure when this changed, but I
think this behaviour is correct.

Found with D101450 + enabling --apply-dynamic-relocs by default.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D101452
This commit is contained in:
Alex Richardson 2021-07-09 10:11:31 +01:00
parent f4b0c9abfb
commit 79332fb722
2 changed files with 21 additions and 4 deletions

View File

@ -32,6 +32,7 @@ public:
RelType getDynRel(RelType type) const override;
void writeGotPltHeader(uint8_t *buf) const override;
void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
void writeIgotPlt(uint8_t *buf, const Symbol &s) const override;
void writePltHeader(uint8_t *buf) const override;
void writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const override;
@ -380,6 +381,12 @@ void X86_64::writeGotPlt(uint8_t *buf, const Symbol &s) const {
write64le(buf, s.getPltVA() + 6);
}
void X86_64::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
// An x86 entry is the address of the ifunc resolver function (for -z rel).
if (config->writeAddends)
write64le(buf, s.getVA());
}
void X86_64::writePltHeader(uint8_t *buf) const {
const uint8_t pltData[] = {
0xff, 0x35, 0, 0, 0, 0, // pushq GOTPLT+8(%rip)

View File

@ -1,12 +1,22 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -relax-relocations -triple=x86_64-unknown-linux %s -o %t.o
# RUN: ld.lld %t.o -o %t1 --no-apply-dynamic-relocs
# RUN: llvm-readobj -x .got.plt -r %t1 | FileCheck --check-prefixes=RELOC,NO-APPLY-DYNAMIC-RELOCS %s
# RUN: ld.lld %t.o -o %t1 --apply-dynamic-relocs
# RUN: llvm-readobj -x .got.plt -r %t1 | FileCheck --check-prefixes=RELOC,APPLY-DYNAMIC-RELOCS %s
# RUN: ld.lld %t.o -o %t1
# RUN: llvm-readobj -r %t1 | FileCheck --check-prefix=RELOC %s
# RUN: llvm-objdump -d %t1 | FileCheck --check-prefix=DISASM %s
## There is no relocations.
# RELOC: Relocations [
# RELOC: ]
## There is one R_X86_64_IRELATIVE relocations.
# RELOC-LABEL: Relocations [
# RELOC-NEXT: Section (1) .rela.dyn {
# RELOC-NEXT: 0x202220 R_X86_64_IRELATIVE - 0x201172
# RELOC-NEXT: }
# RELOC-NEXT: ]
# RELOC-LABEL: Hex dump of section '.got.plt':
# NO-APPLY-DYNAMIC-RELOCS-NEXT: 0x00202220 00000000 00000000
# APPLY-DYNAMIC-RELOCS-NEXT: 0x00202220 72112000 00000000
# RELOC-EMPTY:
# 0x201173 + 7 - 10 = 0x201170
# 0x20117a + 7 - 17 = 0x201170