Skip to content

Commit edda857

Browse files
committed
[BOLT][NFC] Move addRelocation{X86,AArch64} into MCPlusBuilder
The two methods don't belong in BinaryFunction methods. Move the dispatch tables into target-specific MCPlusBuilder methods. Reviewed By: rafauler Differential Revision: https://reviews.llvm.org/D131813
1 parent cb45be2 commit edda857

File tree

5 files changed

+102
-86
lines changed

5 files changed

+102
-86
lines changed

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,96 +1233,11 @@ class BinaryFunction {
12331233
return InputOffsetToAddressMap;
12341234
}
12351235

1236-
void addRelocationAArch64(uint64_t Offset, MCSymbol *Symbol, uint64_t RelType,
1237-
uint64_t Addend, uint64_t Value, bool IsCI) {
1238-
std::map<uint64_t, Relocation> &Rels =
1239-
(IsCI) ? Islands->Relocations : Relocations;
1240-
switch (RelType) {
1241-
case ELF::R_AARCH64_ABS64:
1242-
case ELF::R_AARCH64_ABS32:
1243-
case ELF::R_AARCH64_ABS16:
1244-
case ELF::R_AARCH64_ADD_ABS_LO12_NC:
1245-
case ELF::R_AARCH64_ADR_GOT_PAGE:
1246-
case ELF::R_AARCH64_ADR_PREL_LO21:
1247-
case ELF::R_AARCH64_ADR_PREL_PG_HI21:
1248-
case ELF::R_AARCH64_ADR_PREL_PG_HI21_NC:
1249-
case ELF::R_AARCH64_LD64_GOT_LO12_NC:
1250-
case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
1251-
case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
1252-
case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
1253-
case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
1254-
case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
1255-
case ELF::R_AARCH64_TLSDESC_ADD_LO12:
1256-
case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
1257-
case ELF::R_AARCH64_TLSDESC_ADR_PREL21:
1258-
case ELF::R_AARCH64_TLSDESC_LD64_LO12:
1259-
case ELF::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1260-
case ELF::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1261-
case ELF::R_AARCH64_MOVW_UABS_G0:
1262-
case ELF::R_AARCH64_MOVW_UABS_G0_NC:
1263-
case ELF::R_AARCH64_MOVW_UABS_G1:
1264-
case ELF::R_AARCH64_MOVW_UABS_G1_NC:
1265-
case ELF::R_AARCH64_MOVW_UABS_G2:
1266-
case ELF::R_AARCH64_MOVW_UABS_G2_NC:
1267-
case ELF::R_AARCH64_MOVW_UABS_G3:
1268-
case ELF::R_AARCH64_PREL16:
1269-
case ELF::R_AARCH64_PREL32:
1270-
case ELF::R_AARCH64_PREL64:
1271-
Rels[Offset] = Relocation{Offset, Symbol, RelType, Addend, Value};
1272-
return;
1273-
case ELF::R_AARCH64_CALL26:
1274-
case ELF::R_AARCH64_JUMP26:
1275-
case ELF::R_AARCH64_TSTBR14:
1276-
case ELF::R_AARCH64_CONDBR19:
1277-
case ELF::R_AARCH64_TLSDESC_CALL:
1278-
case ELF::R_AARCH64_TLSLE_ADD_TPREL_HI12:
1279-
case ELF::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1280-
return;
1281-
default:
1282-
llvm_unreachable("Unexpected AArch64 relocation type in code");
1283-
}
1284-
}
1285-
1286-
void addRelocationX86(uint64_t Offset, MCSymbol *Symbol, uint64_t RelType,
1287-
uint64_t Addend, uint64_t Value) {
1288-
switch (RelType) {
1289-
case ELF::R_X86_64_8:
1290-
case ELF::R_X86_64_16:
1291-
case ELF::R_X86_64_32:
1292-
case ELF::R_X86_64_32S:
1293-
case ELF::R_X86_64_64:
1294-
case ELF::R_X86_64_PC8:
1295-
case ELF::R_X86_64_PC32:
1296-
case ELF::R_X86_64_PC64:
1297-
case ELF::R_X86_64_GOTPCRELX:
1298-
case ELF::R_X86_64_REX_GOTPCRELX:
1299-
Relocations[Offset] = Relocation{Offset, Symbol, RelType, Addend, Value};
1300-
return;
1301-
case ELF::R_X86_64_PLT32:
1302-
case ELF::R_X86_64_GOTPCREL:
1303-
case ELF::R_X86_64_TPOFF32:
1304-
case ELF::R_X86_64_GOTTPOFF:
1305-
return;
1306-
default:
1307-
llvm_unreachable("Unexpected x86 relocation type in code");
1308-
}
1309-
}
1310-
13111236
/// Register relocation type \p RelType at a given \p Address in the function
13121237
/// against \p Symbol.
13131238
/// Assert if the \p Address is not inside this function.
13141239
void addRelocation(uint64_t Address, MCSymbol *Symbol, uint64_t RelType,
1315-
uint64_t Addend, uint64_t Value) {
1316-
assert(Address >= getAddress() && Address < getAddress() + getMaxSize() &&
1317-
"address is outside of the function");
1318-
uint64_t Offset = Address - getAddress();
1319-
if (BC.isAArch64()) {
1320-
return addRelocationAArch64(Offset, Symbol, RelType, Addend, Value,
1321-
isInConstantIsland(Address));
1322-
}
1323-
1324-
return addRelocationX86(Offset, Symbol, RelType, Addend, Value);
1325-
}
1240+
uint64_t Addend, uint64_t Value);
13261241

13271242
/// Return the name of the section this function originated from.
13281243
std::optional<StringRef> getOriginSectionName() const {

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,22 @@ class MCPlusBuilder {
415415
return Info->get(Inst.getOpcode()).isPseudo();
416416
}
417417

418+
/// Return true if the relocation type needs to be registered in the function.
419+
/// These code relocations are used in disassembly to better understand code.
420+
///
421+
/// For ARM, they help us decode instruction operands unambiguously, but
422+
/// sometimes we might discard them because we already have the necessary
423+
/// information in the instruction itself (e.g. we don't need to record CALL
424+
/// relocs in ARM because we can fully decode the target from the call
425+
/// operand).
426+
///
427+
/// For X86, they might be used in scanExternalRefs when we want to skip
428+
/// a function but still patch references inside it.
429+
virtual bool shouldRecordCodeRelocation(uint64_t RelType) const {
430+
llvm_unreachable("not implemented");
431+
return false;
432+
}
433+
418434
/// Creates x86 pause instruction.
419435
virtual void createPause(MCInst &Inst) const {
420436
llvm_unreachable("not implemented");

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4517,5 +4517,21 @@ bool BinaryFunction::isAArch64Veneer() const {
45174517
return true;
45184518
}
45194519

4520+
void BinaryFunction::addRelocation(uint64_t Address, MCSymbol *Symbol,
4521+
uint64_t RelType, uint64_t Addend,
4522+
uint64_t Value) {
4523+
assert(Address >= getAddress() && Address < getAddress() + getMaxSize() &&
4524+
"address is outside of the function");
4525+
uint64_t Offset = Address - getAddress();
4526+
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: addRelocation in "
4527+
<< formatv("{0}@{1:x} against {2}\n", this, Offset,
4528+
Symbol->getName()));
4529+
bool IsCI = BC.isAArch64() && isInConstantIsland(Address);
4530+
std::map<uint64_t, Relocation> &Rels =
4531+
IsCI ? Islands->Relocations : Relocations;
4532+
if (BC.MIB->shouldRecordCodeRelocation(RelType))
4533+
Rels[Offset] = Relocation{Offset, Symbol, RelType, Addend, Value};
4534+
}
4535+
45204536
} // namespace bolt
45214537
} // namespace llvm

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,52 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
10891089
return true;
10901090
}
10911091

1092+
bool shouldRecordCodeRelocation(uint64_t RelType) const override {
1093+
switch (RelType) {
1094+
case ELF::R_AARCH64_ABS64:
1095+
case ELF::R_AARCH64_ABS32:
1096+
case ELF::R_AARCH64_ABS16:
1097+
case ELF::R_AARCH64_ADD_ABS_LO12_NC:
1098+
case ELF::R_AARCH64_ADR_GOT_PAGE:
1099+
case ELF::R_AARCH64_ADR_PREL_LO21:
1100+
case ELF::R_AARCH64_ADR_PREL_PG_HI21:
1101+
case ELF::R_AARCH64_ADR_PREL_PG_HI21_NC:
1102+
case ELF::R_AARCH64_LD64_GOT_LO12_NC:
1103+
case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
1104+
case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
1105+
case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
1106+
case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
1107+
case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
1108+
case ELF::R_AARCH64_TLSDESC_ADD_LO12:
1109+
case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
1110+
case ELF::R_AARCH64_TLSDESC_ADR_PREL21:
1111+
case ELF::R_AARCH64_TLSDESC_LD64_LO12:
1112+
case ELF::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1113+
case ELF::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1114+
case ELF::R_AARCH64_MOVW_UABS_G0:
1115+
case ELF::R_AARCH64_MOVW_UABS_G0_NC:
1116+
case ELF::R_AARCH64_MOVW_UABS_G1:
1117+
case ELF::R_AARCH64_MOVW_UABS_G1_NC:
1118+
case ELF::R_AARCH64_MOVW_UABS_G2:
1119+
case ELF::R_AARCH64_MOVW_UABS_G2_NC:
1120+
case ELF::R_AARCH64_MOVW_UABS_G3:
1121+
case ELF::R_AARCH64_PREL16:
1122+
case ELF::R_AARCH64_PREL32:
1123+
case ELF::R_AARCH64_PREL64:
1124+
return true;
1125+
case ELF::R_AARCH64_CALL26:
1126+
case ELF::R_AARCH64_JUMP26:
1127+
case ELF::R_AARCH64_TSTBR14:
1128+
case ELF::R_AARCH64_CONDBR19:
1129+
case ELF::R_AARCH64_TLSDESC_CALL:
1130+
case ELF::R_AARCH64_TLSLE_ADD_TPREL_HI12:
1131+
case ELF::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1132+
return false;
1133+
default:
1134+
llvm_unreachable("Unexpected AArch64 relocation type in code");
1135+
}
1136+
}
1137+
10921138
bool createReturn(MCInst &Inst) const override {
10931139
Inst.setOpcode(AArch64::RET);
10941140
Inst.clear();

bolt/lib/Target/X86/X86MCPlusBuilder.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,29 @@ class X86MCPlusBuilder : public MCPlusBuilder {
388388
return (Desc.TSFlags & X86II::OpPrefixMask) == X86II::PD;
389389
}
390390

391+
bool shouldRecordCodeRelocation(uint64_t RelType) const override {
392+
switch (RelType) {
393+
case ELF::R_X86_64_8:
394+
case ELF::R_X86_64_16:
395+
case ELF::R_X86_64_32:
396+
case ELF::R_X86_64_32S:
397+
case ELF::R_X86_64_64:
398+
case ELF::R_X86_64_PC8:
399+
case ELF::R_X86_64_PC32:
400+
case ELF::R_X86_64_PC64:
401+
case ELF::R_X86_64_GOTPCRELX:
402+
case ELF::R_X86_64_REX_GOTPCRELX:
403+
return true;
404+
case ELF::R_X86_64_PLT32:
405+
case ELF::R_X86_64_GOTPCREL:
406+
case ELF::R_X86_64_TPOFF32:
407+
case ELF::R_X86_64_GOTTPOFF:
408+
return false;
409+
default:
410+
llvm_unreachable("Unexpected x86 relocation type in code");
411+
}
412+
}
413+
391414
unsigned getTrapFillValue() const override { return 0xCC; }
392415

393416
struct IndJmpMatcherFrag1 : MCInstMatcher {

0 commit comments

Comments
 (0)