Skip to content

Commit d7d564b

Browse files
authored
[BOLT] Add BinaryFunction::registerBranch(). NFC (#83337)
Add an external interface to register a branch in a function that is in disassembled state. Allows to make custom modifications to the disassembler. E.g., a pre-CFG pass can add an instruction and register a branch that will later be used during the CFG construction.
1 parent 3f2a9e5 commit d7d564b

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,6 +2056,14 @@ class BinaryFunction {
20562056
/// Returns false if disassembly failed.
20572057
Error disassemble();
20582058

2059+
/// An external interface to register a branch while the function is in
2060+
/// disassembled state. Allows to make custom modifications to the
2061+
/// disassembler. E.g., a pre-CFG pass can add an instruction and register
2062+
/// a branch that will later be used during the CFG construction.
2063+
///
2064+
/// Return a label at the branch destination.
2065+
MCSymbol *registerBranch(uint64_t Src, uint64_t Dst);
2066+
20592067
Error handlePCRelOperand(MCInst &Instruction, uint64_t Address,
20602068
uint64_t Size);
20612069

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,16 @@ Error BinaryFunction::disassemble() {
14451445
return Error::success();
14461446
}
14471447

1448+
MCSymbol *BinaryFunction::registerBranch(uint64_t Src, uint64_t Dst) {
1449+
assert(CurrentState == State::Disassembled &&
1450+
"Cannot register branch unless function is in disassembled state.");
1451+
assert(containsAddress(Src) && containsAddress(Dst) &&
1452+
"Cannot register external branch.");
1453+
MCSymbol *Target = getOrCreateLocalLabel(Dst);
1454+
TakenBranches.emplace_back(Src - getAddress(), Dst - getAddress());
1455+
return Target;
1456+
}
1457+
14481458
bool BinaryFunction::scanExternalRefs() {
14491459
bool Success = true;
14501460
bool DisassemblyFailed = false;

0 commit comments

Comments
 (0)