Skip to content

Commit 04a906e

Browse files
authored
[llvm][lld] Support R_AARCH64_GOTPCREL32 (llvm#72584)
This is the follopw implementation to ARM-software/abi-aa#223 that supports this relocation in llvm and lld.
1 parent 21a784f commit 04a906e

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

lld/ELF/Arch/AArch64.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
165165
case R_AARCH64_ADR_GOT_PAGE:
166166
case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
167167
return R_AARCH64_GOT_PAGE_PC;
168+
case R_AARCH64_GOTPCREL32:
169+
return R_GOT_PC;
168170
case R_AARCH64_NONE:
169171
return R_NONE;
170172
default:
@@ -374,6 +376,7 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
374376
write32(loc, val);
375377
break;
376378
case R_AARCH64_PLT32:
379+
case R_AARCH64_GOTPCREL32:
377380
checkInt(loc, val, 32, rel);
378381
write32(loc, val);
379382
break;
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// REQUIRES: aarch64
2+
// RUN: llvm-mc -filetype=obj -triple=aarch64 %s -o %t.o
3+
// RUN: ld.lld %t.o -o %t.so -shared --noinhibit-exec 2>&1 | FileCheck %s --check-prefix=WARN
4+
// RUN: llvm-readelf -S %t.so | FileCheck --check-prefix=SEC %s
5+
// RUN: llvm-objdump --no-print-imm-hex -s -d %t.so | FileCheck %s
6+
7+
// SEC: .got PROGBITS 0000000000020390
8+
9+
.section .data
10+
.globl bar
11+
bar:
12+
13+
.globl _start
14+
_start: // PC = 0x303a0
15+
// bar@GOTPCREL = 0x20390 (got entry for `bar`) - 0x303a0 (.) = 0xf0fffeff
16+
// bar@GOTPCREL+4 = 0x20390 (got entry for `bar`) - 0x303a4 (.) + 4 = 0xf0fffeff
17+
// bar@GOTPCREL-4 = 0x20390 (got entry for `bar`) - 0x303a8 (.) - 4 = 0xe4fffeff
18+
// CHECK: Contents of section .data:
19+
// CHECK-NEXT: {{.*}} f0fffeff f0fffeff e4fffeff
20+
.word bar@GOTPCREL
21+
.word bar@GOTPCREL+4
22+
.word bar@GOTPCREL-4
23+
24+
// WARN: relocation R_AARCH64_GOTPCREL32 out of range: {{.*}} is not in [-2147483648, 2147483647]; references 'baz'
25+
// WARN: relocation R_AARCH64_GOTPCREL32 out of range: {{.*}} is not in [-2147483648, 2147483647]; references 'baz'
26+
.word baz@GOTPCREL+0xffffffff
27+
.word baz@GOTPCREL-0xffffffff

llvm/include/llvm/BinaryFormat/ELFRelocs/AArch64.def

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ ELF_RELOC(R_AARCH64_ADR_GOT_PAGE, 0x137)
5959
ELF_RELOC(R_AARCH64_LD64_GOT_LO12_NC, 0x138)
6060
ELF_RELOC(R_AARCH64_LD64_GOTPAGE_LO15, 0x139)
6161
ELF_RELOC(R_AARCH64_PLT32, 0x13a)
62+
ELF_RELOC(R_AARCH64_GOTPCREL32, 0x13b)
6263
ELF_RELOC(R_AARCH64_TLSGD_ADR_PREL21, 0x200)
6364
ELF_RELOC(R_AARCH64_TLSGD_ADR_PAGE21, 0x201)
6465
ELF_RELOC(R_AARCH64_TLSGD_ADD_LO12_NC, 0x202)

llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ unsigned AArch64ELFObjectWriter::getRelocType(MCContext &Ctx,
120120

121121
assert((!Target.getSymA() ||
122122
Target.getSymA()->getKind() == MCSymbolRefExpr::VK_None ||
123-
Target.getSymA()->getKind() == MCSymbolRefExpr::VK_PLT) &&
123+
Target.getSymA()->getKind() == MCSymbolRefExpr::VK_PLT ||
124+
Target.getSymA()->getKind() == MCSymbolRefExpr::VK_GOTPCREL) &&
124125
"Should only be expression-level modifiers here");
125126

126127
assert((!Target.getSymB() ||
@@ -206,7 +207,10 @@ unsigned AArch64ELFObjectWriter::getRelocType(MCContext &Ctx,
206207
case FK_Data_2:
207208
return R_CLS(ABS16);
208209
case FK_Data_4:
209-
return R_CLS(ABS32);
210+
return (!IsILP32 &&
211+
Target.getAccessVariant() == MCSymbolRefExpr::VK_GOTPCREL)
212+
? ELF::R_AARCH64_GOTPCREL32
213+
: R_CLS(ABS32);
210214
case FK_Data_8:
211215
if (IsILP32) {
212216
Ctx.reportError(Fixup.getLoc(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: llvm-mc -triple=aarch64 -filetype=obj %s -o - | \
2+
// RUN: llvm-readobj -r - | FileCheck %s
3+
4+
.section .data
5+
this:
6+
.word this@GOTPCREL
7+
.word extern_sym@GOTPCREL+4
8+
.word negative_offset@GOTPCREL-4
9+
10+
// CHECK: Section ({{.*}}) .rela.data
11+
// CHECK-NEXT: 0x0 R_AARCH64_GOTPCREL32 this 0x0
12+
// CHECK-NEXT: 0x4 R_AARCH64_GOTPCREL32 extern_sym 0x4
13+
// CHECK-NEXT: 0x8 R_AARCH64_GOTPCREL32 negative_offset 0xFFFFFFFFFFFFFFFC
14+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)