Skip to content

Commit 67e733d

Browse files
authored
[BOLT][NFC] Refactor relocation printing (llvm#88180)
Some refactoring. * Make the arrays themselves const -- not just the strings being pointed to. * Move each relocation array to the case using it. * Add a suitable assert, to make sure there's no out-of-bounds indexing.
1 parent 17cb8a5 commit 67e733d

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

bolt/lib/Core/Relocation.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,21 +1064,19 @@ MCBinaryExpr::Opcode Relocation::getComposeOpcodeFor(uint64_t Type) {
10641064
}
10651065
}
10661066

1067-
#define ELF_RELOC(name, value) #name,
1068-
10691067
void Relocation::print(raw_ostream &OS) const {
1070-
static const char *X86RelocNames[] = {
1071-
#include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
1072-
};
1073-
static const char *AArch64RelocNames[] = {
1074-
#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
1075-
};
10761068
switch (Arch) {
10771069
default:
10781070
OS << "RType:" << Twine::utohexstr(Type);
10791071
break;
10801072

10811073
case Triple::aarch64:
1074+
static const char *const AArch64RelocNames[] = {
1075+
#define ELF_RELOC(name, value) #name,
1076+
#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
1077+
#undef ELF_RELOC
1078+
};
1079+
assert(Type < ArrayRef(AArch64RelocNames).size());
10821080
OS << AArch64RelocNames[Type];
10831081
break;
10841082

@@ -1088,16 +1086,22 @@ void Relocation::print(raw_ostream &OS) const {
10881086
switch (Type) {
10891087
default:
10901088
llvm_unreachable("illegal RISC-V relocation");
1091-
#undef ELF_RELOC
10921089
#define ELF_RELOC(name, value) \
10931090
case value: \
10941091
OS << #name; \
10951092
break;
10961093
#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
1094+
#undef ELF_RELOC
10971095
}
10981096
break;
10991097

11001098
case Triple::x86_64:
1099+
static const char *const X86RelocNames[] = {
1100+
#define ELF_RELOC(name, value) #name,
1101+
#include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
1102+
#undef ELF_RELOC
1103+
};
1104+
assert(Type < ArrayRef(X86RelocNames).size());
11011105
OS << X86RelocNames[Type];
11021106
break;
11031107
}

0 commit comments

Comments
 (0)