Skip to content

Commit 5f1b902

Browse files
authored
[BOLT][AArch64] Fix printing of relocation types (#166621)
Enumeration of relocation types is not always sequential, e.g. on AArch64 the first real relocation type is 0x101. As such, the existing code in `Relocation::print()` was crashing while printing AArch64 relocations. Fix it by using `llvm::object::getELFRelocationTypeName()`.
1 parent 163933e commit 5f1b902

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

bolt/lib/Core/Relocation.cpp

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,41 +1018,15 @@ void Relocation::print(raw_ostream &OS) const {
10181018
default:
10191019
OS << "RType:" << Twine::utohexstr(Type);
10201020
break;
1021-
1022-
case Triple::aarch64: {
1023-
static const char *const AArch64RelocNames[] = {
1024-
#define ELF_RELOC(name, value) #name,
1025-
#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
1026-
#undef ELF_RELOC
1027-
};
1028-
assert(Type < ArrayRef(AArch64RelocNames).size());
1029-
OS << AArch64RelocNames[Type];
1030-
} break;
1031-
1021+
case Triple::aarch64:
1022+
OS << object::getELFRelocationTypeName(ELF::EM_AARCH64, Type);
1023+
break;
10321024
case Triple::riscv64:
1033-
// RISC-V relocations are not sequentially numbered so we cannot use an
1034-
// array
1035-
switch (Type) {
1036-
default:
1037-
llvm_unreachable("illegal RISC-V relocation");
1038-
#define ELF_RELOC(name, value) \
1039-
case value: \
1040-
OS << #name; \
1025+
OS << object::getELFRelocationTypeName(ELF::EM_RISCV, Type);
10411026
break;
1042-
#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
1043-
#undef ELF_RELOC
1044-
}
1027+
case Triple::x86_64:
1028+
OS << object::getELFRelocationTypeName(ELF::EM_X86_64, Type);
10451029
break;
1046-
1047-
case Triple::x86_64: {
1048-
static const char *const X86RelocNames[] = {
1049-
#define ELF_RELOC(name, value) #name,
1050-
#include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
1051-
#undef ELF_RELOC
1052-
};
1053-
assert(Type < ArrayRef(X86RelocNames).size());
1054-
OS << X86RelocNames[Type];
1055-
} break;
10561030
}
10571031
OS << ", 0x" << Twine::utohexstr(Offset);
10581032
if (Symbol) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Verify that llvm-bolt correctly prints relocation types.
2+
3+
# REQUIRES: system-linux
4+
5+
# RUN: %clang %cflags -nostartfiles %s -o %t.exe -Wl,-q,--no-relax
6+
# RUN: llvm-bolt %t.exe --print-cfg --print-relocations -o %t.bolt \
7+
# RUN: | FileCheck %s
8+
9+
.section .text
10+
.align 4
11+
.globl _start
12+
.type _start, %function
13+
_start:
14+
15+
adrp x0, _start
16+
# CHECK: adrp
17+
# CHECK-SAME: R_AARCH64_ADR_PREL_PG_HI21
18+
19+
add x0, x0, :lo12:_start
20+
# CHECK-NEXT: add
21+
# CHECK-SAME: R_AARCH64_ADD_ABS_LO12_NC
22+
23+
ret
24+
.size _start, .-_start

0 commit comments

Comments
 (0)