Skip to content

Commit 62020a3

Browse files
Kepontryllongint
authored andcommitted
[BOLT] Implement createRelocation for AArch64
The implementation is based on the X86 version, with the same code of symbol and addend extraction. The differences include the support for RelType `R_AARCH64_CALL26` and the deletion of 8-bit relocation. Reviewed By: rafauler Differential Revision: https://reviews.llvm.org/D156018
1 parent ba114ab commit 62020a3

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

bolt/lib/Core/Relocation.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,13 @@ static uint64_t encodeValueAArch64(uint64_t Type, uint64_t Value, uint64_t PC) {
345345
case ELF::R_AARCH64_PREL64:
346346
Value -= PC;
347347
break;
348+
case ELF::R_AARCH64_CALL26:
349+
Value -= PC;
350+
assert(isInt<28>(Value) && "only PC +/- 128MB is allowed for direct call");
351+
// Immediate goes in bits 25:0 of BL.
352+
// OP 1001_01 goes in bits 31:26 of BL.
353+
Value = (Value >> 2) | 0x94000000ULL;
354+
break;
348355
}
349356
return Value;
350357
}

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "MCTargetDesc/AArch64AddressingModes.h"
14+
#include "MCTargetDesc/AArch64FixupKinds.h"
1415
#include "MCTargetDesc/AArch64MCExpr.h"
1516
#include "MCTargetDesc/AArch64MCTargetDesc.h"
1617
#include "Utils/AArch64BaseInfo.h"
1718
#include "bolt/Core/MCPlusBuilder.h"
1819
#include "llvm/BinaryFormat/ELF.h"
20+
#include "llvm/MC/MCFixupKindInfo.h"
1921
#include "llvm/MC/MCInstrInfo.h"
2022
#include "llvm/MC/MCRegisterInfo.h"
2123
#include "llvm/Support/Debug.h"
@@ -1160,6 +1162,52 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
11601162
ELF::R_AARCH64_ADD_ABS_LO12_NC);
11611163
return Insts;
11621164
}
1165+
1166+
std::optional<Relocation>
1167+
createRelocation(const MCFixup &Fixup,
1168+
const MCAsmBackend &MAB) const override {
1169+
const MCFixupKindInfo &FKI = MAB.getFixupKindInfo(Fixup.getKind());
1170+
1171+
assert(FKI.TargetOffset == 0 && "0-bit relocation offset expected");
1172+
const uint64_t RelOffset = Fixup.getOffset();
1173+
1174+
uint64_t RelType;
1175+
if (Fixup.getKind() == MCFixupKind(AArch64::fixup_aarch64_pcrel_call26))
1176+
RelType = ELF::R_AARCH64_CALL26;
1177+
else if (FKI.Flags & MCFixupKindInfo::FKF_IsPCRel) {
1178+
switch (FKI.TargetSize) {
1179+
default:
1180+
return std::nullopt;
1181+
case 16:
1182+
RelType = ELF::R_AARCH64_PREL16;
1183+
break;
1184+
case 32:
1185+
RelType = ELF::R_AARCH64_PREL32;
1186+
break;
1187+
case 64:
1188+
RelType = ELF::R_AARCH64_PREL64;
1189+
break;
1190+
}
1191+
} else {
1192+
switch (FKI.TargetSize) {
1193+
default:
1194+
return std::nullopt;
1195+
case 16:
1196+
RelType = ELF::R_AARCH64_ABS16;
1197+
break;
1198+
case 32:
1199+
RelType = ELF::R_AARCH64_ABS32;
1200+
break;
1201+
case 64:
1202+
RelType = ELF::R_AARCH64_ABS64;
1203+
break;
1204+
}
1205+
}
1206+
1207+
auto [RelSymbol, RelAddend] = extractFixupExpr(Fixup);
1208+
1209+
return Relocation({RelOffset, RelSymbol, RelType, RelAddend, 0});
1210+
}
11631211
};
11641212

11651213
} // end anonymous namespace

bolt/test/AArch64/reloc-call26.s

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## This test checks processing of R_AARCH64_CALL26 relocation
2+
## when option `--funcs` is enabled
3+
4+
# REQUIRES: system-linux
5+
6+
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown \
7+
# RUN: %s -o %t.o
8+
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
9+
# RUN: llvm-bolt %t.exe -o %t.bolt --funcs=func1
10+
# RUN: llvm-objdump -d --disassemble-symbols='_start' %t.bolt | \
11+
# RUN: FileCheck %s
12+
13+
# CHECK: {{.*}} bl {{.*}} <func1>
14+
15+
.text
16+
.align 4
17+
.global _start
18+
.type _start, %function
19+
_start:
20+
bl func1
21+
mov w8, #93
22+
svc #0
23+
.size _start, .-_start
24+
25+
.global func1
26+
.type func1, %function
27+
func1:
28+
ret
29+
.size func1, .-func1

0 commit comments

Comments
 (0)