Skip to content

Commit ad51cee

Browse files
committedNov 14, 2018
[AArch64] Fix resolution of R_PLT_PAGE RelExpr
The R_AARCH64_ADR_PREL_PG_HI21 relocation type is given the R_PAGE_PC RelExpr. This can be transformed to R_PLT_PAGE_PC via toPlt(). Unfortunately the resolution is identical to R_PAGE_PC so instead of getting the address of the PLT entry we get the address of the symbol which may not be correct in the case of static ifuncs. The fix is to handle the cases separately and use getPltVA() + A with R_PLT_PAGE_PC. Differential Revision: https://reviews.llvm.org/D54474 llvm-svn: 346863
1 parent 82318c6 commit ad51cee

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed
 

‎lld/ELF/InputSection.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -668,14 +668,13 @@ static uint64_t getRelocTargetVA(const InputFile *File, RelType Type, int64_t A,
668668
case R_MIPS_TLSLD:
669669
return In.MipsGot->getVA() + In.MipsGot->getTlsIndexOffset(File) -
670670
In.MipsGot->getGp(File);
671-
case R_PAGE_PC:
671+
case R_PAGE_PC: {
672+
uint64_t Val = Sym.isUndefWeak() ? A : Sym.getVA(A);
673+
return getAArch64Page(Val) - getAArch64Page(P);
674+
}
672675
case R_PLT_PAGE_PC: {
673-
uint64_t Dest;
674-
if (Sym.isUndefWeak())
675-
Dest = getAArch64Page(A);
676-
else
677-
Dest = getAArch64Page(Sym.getVA(A));
678-
return Dest - getAArch64Page(P);
676+
uint64_t Val = Sym.isUndefWeak() ? A : Sym.getPltVA() + A;
677+
return getAArch64Page(Val) - getAArch64Page(P);
679678
}
680679
case R_RISCV_PC_INDIRECT: {
681680
const Relocation *HiRel = getRISCVPCRelHi20(&Sym, A);

‎lld/test/ELF/aarch64-gnu-ifunc3.s

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# REQUIRES: aarch64
2+
# RUN: llvm-mc -filetype=obj -triple=aarch64-none-linux-gnu %s -o %t.o
3+
# RUN: ld.lld -static %t.o -o %tout
4+
# RUN: llvm-objdump -D %tout | FileCheck %s
5+
# RUN: llvm-readobj -r %tout | FileCheck %s --check-prefix=RELOC
6+
7+
# The address of myfunc is the address of the PLT entry for myfunc.
8+
# The adrp to myfunc should generate a PLT entry and a got entry with an
9+
# irelative relocation.
10+
.text
11+
.globl myfunc
12+
.type myfunc,@gnu_indirect_function
13+
myfunc:
14+
ret
15+
16+
.text
17+
.globl _start
18+
.type _start,@function
19+
_start:
20+
adrp x8, myfunc
21+
add x8, x8, :lo12:myfunc
22+
ret
23+
24+
# CHECK: Disassembly of section .text:
25+
# CHECK-NEXT: myfunc:
26+
# CHECK-NEXT: 210000: c0 03 5f d6 ret
27+
# CHECK: _start:
28+
# adrp x8, 0x210000 + 0x10 from add == .plt entry
29+
# CHECK-NEXT: 210004: 08 00 00 90 adrp x8, #0
30+
# CHECK-NEXT: 210008: 08 41 00 91 add x8, x8, #16
31+
# CHECK-NEXT: 21000c: c0 03 5f d6 ret
32+
# CHECK-NEXT: Disassembly of section .plt:
33+
# CHECK-NEXT: .plt:
34+
# adrp x16, 0x220000, 0x220000 == address in .got.plt
35+
# CHECK-NEXT: 210010: 90 00 00 90 adrp x16, #65536
36+
# CHECK-NEXT: 210014: 11 02 40 f9 ldr x17, [x16]
37+
# CHECK-NEXT: 210018: 10 02 00 91 add x16, x16, #0
38+
# CHECK-NEXT: 21001c: 20 02 1f d6 br x17
39+
# CHECK-NEXT: Disassembly of section .got.plt:
40+
# CHECK-NEXT: .got.plt:
41+
# 0x210010 == address in .plt
42+
# CHECK-NEXT: 220000: 10 00 21 00
43+
# CHECK-NEXT: 220004: 00 00 00 00
44+
45+
# RELOC: Relocations [
46+
# RELOC-NEXT: Section (1) .rela.plt {
47+
# RELOC-NEXT: 0x220000 R_AARCH64_IRELATIVE - 0x210000
48+
# RELOC-NEXT: }
49+
# RELOC-NEXT: ]

0 commit comments

Comments
 (0)
Please sign in to comment.