From 0996601386125530284705160083919973992954 Mon Sep 17 00:00:00 2001 From: Glenn Smith Date: Thu, 31 Jul 2025 17:25:18 -0400 Subject: Support MLIL expr mappings in C++ --- binaryninjaapi.h | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'binaryninjaapi.h') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 344fb03e..32d6051f 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -11106,11 +11106,20 @@ namespace BinaryNinja { */ void SetLowLevelILFunction(Ref lowLevelIL); - /*! Set the new Medium Level IL for the current analysis context + /*! Set the new Medium Level IL for the current analysis context. + + If mapping parameters are left as default (empty), then they will be automatically + computed for you based on previous calls to AddExpr() and AddInstruction() \param mediumLevelIL the new Medium Level IL + \param llilSsaToMlilInstrMap New mappings from LLIL SSA -> MLIL instruction indices + \param llilSsaToMlilExprMap New mappings from LLIL SSA -> MLIL expression indices */ - void SetMediumLevelILFunction(Ref mediumLevelIL); + void SetMediumLevelILFunction( + Ref mediumLevelIL, + std::unordered_map llilSsaToMlilInstrMap = {}, + std::vector llilSsaToMlilExprMap = {} + ); /*! Set the new High Level IL for the current analysis context @@ -13071,21 +13080,16 @@ namespace BinaryNinja { uint32_t sourceOperand; bool valid; - ILSourceLocation() : valid(false) {} + bool ilBased; + bool ilDirect; + size_t ilExprIndex; - ILSourceLocation(uint64_t addr, uint32_t operand) : address(addr), sourceOperand(operand), valid(true) {} - - ILSourceLocation(const BNLowLevelILInstruction& instr) : - address(instr.address), sourceOperand(instr.sourceOperand), valid(true) - {} + ILSourceLocation() : valid(false), ilBased(false) {} - ILSourceLocation(const BNMediumLevelILInstruction& instr) : - address(instr.address), sourceOperand(instr.sourceOperand), valid(true) - {} - - ILSourceLocation(const BNHighLevelILInstruction& instr) : - address(instr.address), sourceOperand(instr.sourceOperand), valid(true) - {} + ILSourceLocation(uint64_t addr, uint32_t operand) : address(addr), sourceOperand(operand), valid(true), ilBased(false) {} + ILSourceLocation(const struct LowLevelILInstruction& instr); + ILSourceLocation(const struct MediumLevelILInstruction& instr); + ILSourceLocation(const struct HighLevelILInstruction& instr); }; struct LowLevelILInstruction; @@ -14535,6 +14539,22 @@ namespace BinaryNinja { public CoreRefCountObject { + struct TranslationData + { + MediumLevelILFunction* copyingFunction = nullptr; + std::unordered_map>> mlilToMlilExprMap; + std::unordered_map>> mlilToMlilInstrMap; + // todo maybe: llil ssa -> mlil mappings + }; + std::unique_ptr m_translationData; + + void RecordMLILToMLILExprMap(size_t newExprIndex, const ILSourceLocation& location); + void RecordMLILToMLILInstrMap(size_t newInstrIndex, const ILSourceLocation& location); + std::unordered_map GetLLILSSAToMLILInstrMap(bool fromTranslation); + std::vector GetLLILSSAToMLILExprMap(bool fromTranslation); + + friend class AnalysisContext; + public: MediumLevelILFunction(Architecture* arch, Function* func = nullptr, LowLevelILFunction* lowLevelIL = nullptr); MediumLevelILFunction(BNMediumLevelILFunction* func); @@ -14774,7 +14794,7 @@ namespace BinaryNinja { const ILSourceLocation& loc = ILSourceLocation()); void MarkLabel(BNMediumLevelILLabel& label); - ExprId AddInstruction(ExprId expr); + ExprId AddInstruction(ExprId expr, const ILSourceLocation& loc = ILSourceLocation()); std::vector GetOperandList(ExprId i, size_t listOperand); ExprId AddLabelMap(const std::map& labels); -- cgit v1.3.1