[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.
This commit is contained in:
Maksim Panchenko 2024-02-28 20:04:28 -08:00 committed by GitHub
parent 3f2a9e5910
commit d7d564b2fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 0 deletions

View File

@ -2056,6 +2056,14 @@ public:
/// Returns false if disassembly failed. /// Returns false if disassembly failed.
Error disassemble(); Error disassemble();
/// An external interface to register a branch while the function 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.
///
/// Return a label at the branch destination.
MCSymbol *registerBranch(uint64_t Src, uint64_t Dst);
Error handlePCRelOperand(MCInst &Instruction, uint64_t Address, Error handlePCRelOperand(MCInst &Instruction, uint64_t Address,
uint64_t Size); uint64_t Size);

View File

@ -1445,6 +1445,16 @@ add_instruction:
return Error::success(); return Error::success();
} }
MCSymbol *BinaryFunction::registerBranch(uint64_t Src, uint64_t Dst) {
assert(CurrentState == State::Disassembled &&
"Cannot register branch unless function is in disassembled state.");
assert(containsAddress(Src) && containsAddress(Dst) &&
"Cannot register external branch.");
MCSymbol *Target = getOrCreateLocalLabel(Dst);
TakenBranches.emplace_back(Src - getAddress(), Dst - getAddress());
return Target;
}
bool BinaryFunction::scanExternalRefs() { bool BinaryFunction::scanExternalRefs() {
bool Success = true; bool Success = true;
bool DisassemblyFailed = false; bool DisassemblyFailed = false;