Skip to content

Commit 48e894a

Browse files
Alexey Moksyakovyota9
authored andcommitted
[BOLT] Add R_AARCH64_PREL16/32/64 relocations support
Reviewed By: yota9, rafauler Differential Revision: https://reviews.llvm.org/D122294
1 parent 63686af commit 48e894a

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,9 @@ class BinaryFunction {
12791279
case ELF::R_AARCH64_MOVW_UABS_G2:
12801280
case ELF::R_AARCH64_MOVW_UABS_G2_NC:
12811281
case ELF::R_AARCH64_MOVW_UABS_G3:
1282+
case ELF::R_AARCH64_PREL16:
1283+
case ELF::R_AARCH64_PREL32:
1284+
case ELF::R_AARCH64_PREL64:
12821285
Rels[Offset] = Relocation{Offset, Symbol, RelType, Addend, Value};
12831286
return;
12841287
case ELF::R_AARCH64_CALL26:

bolt/lib/Core/Relocation.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ bool isSupportedAArch64(uint64_t Type) {
7474
case ELF::R_AARCH64_TLSDESC_ADD_LO12:
7575
case ELF::R_AARCH64_TLSDESC_CALL:
7676
case ELF::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
77+
case ELF::R_AARCH64_PREL16:
7778
case ELF::R_AARCH64_PREL32:
79+
case ELF::R_AARCH64_PREL64:
7880
case ELF::R_AARCH64_ABS16:
7981
case ELF::R_AARCH64_ABS32:
8082
case ELF::R_AARCH64_ABS64:
@@ -121,6 +123,7 @@ size_t getSizeForTypeAArch64(uint64_t Type) {
121123
errs() << object::getELFRelocationTypeName(ELF::EM_AARCH64, Type) << '\n';
122124
llvm_unreachable("unsupported relocation type");
123125
case ELF::R_AARCH64_ABS16:
126+
case ELF::R_AARCH64_PREL16:
124127
return 2;
125128
case ELF::R_AARCH64_CALL26:
126129
case ELF::R_AARCH64_JUMP26:
@@ -157,6 +160,7 @@ size_t getSizeForTypeAArch64(uint64_t Type) {
157160
case ELF::R_AARCH64_ABS32:
158161
return 4;
159162
case ELF::R_AARCH64_ABS64:
163+
case ELF::R_AARCH64_PREL64:
160164
return 8;
161165
}
162166
}
@@ -258,7 +262,9 @@ uint64_t adjustValueAArch64(uint64_t Type, uint64_t Value, uint64_t PC) {
258262
llvm_unreachable("not supported relocation");
259263
case ELF::R_AARCH64_ABS32:
260264
break;
265+
case ELF::R_AARCH64_PREL16:
261266
case ELF::R_AARCH64_PREL32:
267+
case ELF::R_AARCH64_PREL64:
262268
Value -= PC;
263269
break;
264270
}
@@ -282,8 +288,12 @@ uint64_t extractValueAArch64(uint64_t Type, uint64_t Contents, uint64_t PC) {
282288
case ELF::R_AARCH64_ABS32:
283289
case ELF::R_AARCH64_ABS64:
284290
return Contents;
291+
case ELF::R_AARCH64_PREL16:
292+
return static_cast<int64_t>(PC) + SignExtend64<16>(Contents & 0xffff);
285293
case ELF::R_AARCH64_PREL32:
286294
return static_cast<int64_t>(PC) + SignExtend64<32>(Contents & 0xffffffff);
295+
case ELF::R_AARCH64_PREL64:
296+
return static_cast<int64_t>(PC) + Contents;
287297
case ELF::R_AARCH64_TLSDESC_CALL:
288298
case ELF::R_AARCH64_JUMP26:
289299
case ELF::R_AARCH64_CALL26:
@@ -505,7 +515,9 @@ bool isPCRelativeAArch64(uint64_t Type) {
505515
case ELF::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
506516
case ELF::R_AARCH64_TLSDESC_ADR_PREL21:
507517
case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
518+
case ELF::R_AARCH64_PREL16:
508519
case ELF::R_AARCH64_PREL32:
520+
case ELF::R_AARCH64_PREL64:
509521
return true;
510522
}
511523
}

bolt/test/lit.cfg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
ToolSubst('llvm-readelf', unresolved='fatal'),
9191
ToolSubst('link_fdata', command=link_fdata_cmd, unresolved='fatal'),
9292
ToolSubst('merge-fdata', unresolved='fatal'),
93+
ToolSubst('llvm-readobj', unresolved='fatal'),
9394
]
9495
llvm_config.add_tool_substitutions(tools, tool_dirs)
9596

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This test checks processing of R_AARCH64_PREL64/32/16 relocations
2+
3+
// RUN: %clang %cflags -nostartfiles -nostdlib %s -o %t.exe -Wl,-q \
4+
// RUN: -Wl,-z,max-page-size=4
5+
// RUN: llvm-readelf -Wa %t.exe | FileCheck %s -check-prefix=CHECKPREL
6+
7+
// CHECKPREL: R_AARCH64_PREL16 {{.*}} .dummy + 0
8+
// CHECKPREL-NEXT: R_AARCH64_PREL32 {{.*}} _start + 4
9+
// CHECKPREL-NEXT: R_AARCH64_PREL64 {{.*}} _start + 8
10+
11+
// RUN: llvm-bolt %t.exe -o %t.bolt
12+
// RUN: llvm-readobj -S --section-data %t.bolt | FileCheck %s
13+
14+
// CHECK: Name: .data
15+
// CHECK: SectionData (
16+
// CHECK: 0000: FCFF0000 44FF3F00 44FF3F00 00000000
17+
// CHECK: )
18+
19+
.text
20+
.align 4
21+
.globl _start
22+
.type _start, %function
23+
_start:
24+
adr x0, datatable
25+
mov x0, #0
26+
ret
27+
28+
.section .dummy, "da"
29+
dummy:
30+
.word 0
31+
32+
.section .data
33+
datatable:
34+
.hword dummy - datatable
35+
.align 2
36+
.word _start - datatable
37+
.xword _start - datatable

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,13 @@ void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
446446
write(isBE, TargetPtr, static_cast<uint32_t>(Result));
447447
break;
448448
}
449+
case ELF::R_AARCH64_PREL16: {
450+
uint64_t Result = Value + Addend - FinalAddress;
451+
assert(static_cast<int64_t>(Result) >= INT16_MIN &&
452+
static_cast<int64_t>(Result) <= UINT16_MAX);
453+
write(isBE, TargetPtr, static_cast<uint16_t>(Result & 0xffffU));
454+
break;
455+
}
449456
case ELF::R_AARCH64_PREL32: {
450457
uint64_t Result = Value + Addend - FinalAddress;
451458
assert(static_cast<int64_t>(Result) >= INT32_MIN &&

llvm/lib/Object/RelocationResolver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static bool supportsAArch64(uint64_t Type) {
7878
switch (Type) {
7979
case ELF::R_AARCH64_ABS32:
8080
case ELF::R_AARCH64_ABS64:
81+
case ELF::R_AARCH64_PREL16:
8182
case ELF::R_AARCH64_PREL32:
8283
case ELF::R_AARCH64_PREL64:
8384
return true;
@@ -93,6 +94,8 @@ static uint64_t resolveAArch64(uint64_t Type, uint64_t Offset, uint64_t S,
9394
return (S + Addend) & 0xFFFFFFFF;
9495
case ELF::R_AARCH64_ABS64:
9596
return S + Addend;
97+
case ELF::R_AARCH64_PREL16:
98+
return (S + Addend - Offset) & 0xFFFF;
9699
case ELF::R_AARCH64_PREL32:
97100
return (S + Addend - Offset) & 0xFFFFFFFF;
98101
case ELF::R_AARCH64_PREL64:

0 commit comments

Comments
 (0)