diff options
| author | Peter LaFosse <peter@vector35.com> | 2018-08-26 15:31:12 -0400 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2018-08-26 15:31:12 -0400 |
| commit | fe925b01df7934785aa5dc67d23c577f13ad42e6 (patch) | |
| tree | 8a2e05c645c6be7dbc12897cb1a696f16066b8af | |
| parent | 4fdd54f690e538ddb4e38248144ef3d3e0f817da (diff) | |
Add support for new constant type EXTERN_PTR
| -rw-r--r-- | binaryninjaapi.h | 11 | ||||
| -rw-r--r-- | binaryninjacore.h | 14 | ||||
| -rw-r--r-- | examples/mlil_parser/src/mlil_parser.cpp | 13 | ||||
| -rw-r--r-- | function.cpp | 4 | ||||
| -rw-r--r-- | mediumlevelilinstruction.cpp | 2 | ||||
| -rw-r--r-- | relocationhandler.cpp | 30 |
6 files changed, 57 insertions, 17 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 7b1e31ff..b6d41825 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -2417,6 +2417,7 @@ namespace BinaryNinja { BNRegisterValueType state; int64_t value; + int64_t offset; RegisterValue(); static RegisterValue FromAPIObject(const BNRegisterValue& value); @@ -2427,6 +2428,7 @@ namespace BinaryNinja { BNRegisterValueType state; int64_t value; + int64_t offset; std::vector<BNValueRange> ranges; std::set<int64_t> valueSet; std::vector<LookupTableEntry> table; @@ -3421,27 +3423,30 @@ namespace BinaryNinja BNRelocationInfo* result, size_t resultCount); static bool ApplyRelocationCallback(void* ctxt, BNBinaryView* view, BNArchitecture* arch, BNRelocation* reloc, uint8_t* dest, size_t len); - + static size_t GetOperandForExternalRelocationCallback(void* ctxt, const uint8_t* data, uint64_t addr, + size_t length, BNLowLevelILFunction* il, BNRelocation* relocation); protected: RelocationHandler(); RelocationHandler(BNRelocationHandler* handler); static void FreeCallback(void* ctxt); public: - virtual bool GetRelocationInfo(Ref<BinaryView> view, Ref<Architecture> arch, std::vector<BNRelocationInfo>& result); virtual bool ApplyRelocation(Ref<BinaryView> view, Ref<Architecture> arch, Ref<Relocation> reloc, uint8_t* dest, size_t len); + virtual size_t GetOperandForExternalRelocation(const uint8_t* data, uint64_t addr, size_t length, + Ref<LowLevelILFunction> il, Ref<Relocation> relocation); }; class CoreRelocationHandler: public RelocationHandler { public: CoreRelocationHandler(BNRelocationHandler* handler); - virtual bool GetRelocationInfo(Ref<BinaryView> view, Ref<Architecture> arch, std::vector<BNRelocationInfo>& result) override; virtual bool ApplyRelocation(Ref<BinaryView> view, Ref<Architecture> arch, Ref<Relocation> reloc, uint8_t* dest, size_t len) override; + virtual size_t GetOperandForExternalRelocation(const uint8_t* data, uint64_t addr, size_t length, + Ref<LowLevelILFunction> il, Ref<Relocation> relocation) override; }; class UpdateException: public std::exception diff --git a/binaryninjacore.h b/binaryninjacore.h index 9b50c7e3..a8ef21c8 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -64,6 +64,7 @@ #define LLIL_GET_TEMP_REG_INDEX(n) ((n) & 0x7fffffff) #define BN_INVALID_REGISTER 0xffffffff +#define BN_NOCOERCE_EXTERN_PTR 0xfffffffe #define BN_INVALID_OPERAND 0xffffffff #define BN_INVALID_EXPR ((size_t)-1) @@ -733,6 +734,7 @@ extern "C" EntryValue, ConstantValue, ConstantPointerValue, + ExternalPointerValue, StackFrameOffset, ReturnAddressValue, ImportedAddressValue, @@ -777,6 +779,7 @@ extern "C" { BNRegisterValueType state; int64_t value; + int64_t offset; }; struct BNRegisterValueWithConfidence @@ -794,6 +797,7 @@ extern "C" { BNRegisterValueType state; int64_t value; + int64_t offset; BNValueRange* ranges; int64_t* valueSet; BNLookupTableEntry* table; @@ -1124,12 +1128,13 @@ extern "C" StandardRelocationType, IgnoredRelocation }; - + #define MAX_RELOCATION_SIZE 8 struct BNRelocationInfo { BNRelocationType type; // BinaryNinja Relocation Type bool pcRelative; // PC Relative or Absolute (subtract address from relocation) bool baseRelative; // Relative to start of module (Add module base to relocation) + uint64_t base; // Base address for this binary view size_t size; // Size of the data to be written size_t truncateSize; // After addition/subtraction truncate to uint64_t nativeType; // Base type from relocation entry @@ -1141,6 +1146,9 @@ extern "C" size_t sectionIndex; // Index into the section table uint64_t address; // Absolute address or segment offset bool dataRelocation; // This relocation is effecting data not code + uint8_t relocationDataCache[MAX_RELOCATION_SIZE]; + struct BNRelocationInfo* prev; // Link to relocation another related relocation + struct BNRelocationInfo* next; // Link to relocation another related relocation }; struct BNInstructionTextToken @@ -1403,6 +1411,8 @@ extern "C" size_t resultCount); bool (*applyRelocation)(void* ctxt, BNBinaryView* view, BNArchitecture* arch, BNRelocation* reloc, uint8_t* dest, size_t len); + size_t (*getOperandForExternalRelocation)(void* ctxt, const uint8_t* data, uint64_t addr, size_t length, + BNLowLevelILFunction* il, BNRelocation* relocation); }; struct BNTypeParserResult @@ -2346,6 +2356,8 @@ extern "C" BNArchitecture* arch, BNRelocation* reloc, uint8_t* dest, size_t len); BINARYNINJACOREAPI bool BNRelocationHandlerDefaultApplyRelocation(BNRelocationHandler* handler, BNBinaryView* view, BNArchitecture* arch, BNRelocation* reloc, uint8_t* dest, size_t len); + BINARYNINJACOREAPI size_t BNRelocationHandlerGetOperandForExternalRelocation(BNRelocationHandler* handler, + const uint8_t* data, uint64_t addr, size_t length, const BNLowLevelILFunction* il, BNRelocation* relocation); // Analysis BINARYNINJACOREAPI void BNAddAnalysisOption(BNBinaryView* view, const char* name); BINARYNINJACOREAPI void BNAddFunctionForAnalysis(BNBinaryView* view, BNPlatform* platform, uint64_t addr); diff --git a/examples/mlil_parser/src/mlil_parser.cpp b/examples/mlil_parser/src/mlil_parser.cpp index a4c10f61..a9411251 100644 --- a/examples/mlil_parser/src/mlil_parser.cpp +++ b/examples/mlil_parser/src/mlil_parser.cpp @@ -323,17 +323,10 @@ int main(int argc, char *argv[]) // Example of using visitors to find all constants in the instruction instr.VisitExprs([&](const MediumLevelILInstruction& expr) { - switch (expr.operation) - { - case MLIL_CONST: - case MLIL_CONST_PTR: - case MLIL_EXTERN_PTR: + bool status = MediumLevelILFunction::IsConstantType(expr.operation); + if (status) printf(" Found constant 0x%" PRIx64 "\n", expr.GetConstant()); - return false; // Done parsing this - default: - break; - } - return true; // Parse any subexpressions + return !status; }); // Example of using the templated accessors for efficiently parsing load instructions diff --git a/function.cpp b/function.cpp index 9c56c644..e441d1b5 100644 --- a/function.cpp +++ b/function.cpp @@ -91,7 +91,7 @@ Variable Variable::FromIdentifier(uint64_t id) } -RegisterValue::RegisterValue(): state(UndeterminedValue), value(0) +RegisterValue::RegisterValue(): state(UndeterminedValue), value(0), offset(0) { } @@ -101,6 +101,7 @@ BNRegisterValue RegisterValue::ToAPIObject() BNRegisterValue result; result.state = state; result.value = value; + result.offset = offset; return result; } @@ -278,6 +279,7 @@ PossibleValueSet PossibleValueSet::FromAPIObject(BNPossibleValueSet& value) PossibleValueSet result; result.state = value.state; result.value = value.value; + result.offset = value.offset; if (value.state == LookupTableValue) { for (size_t i = 0; i < value.count; i++) diff --git a/mediumlevelilinstruction.cpp b/mediumlevelilinstruction.cpp index 17ef88e7..c7627bd7 100644 --- a/mediumlevelilinstruction.cpp +++ b/mediumlevelilinstruction.cpp @@ -168,7 +168,7 @@ unordered_map<BNMediumLevelILOperation, vector<MediumLevelILOperandUsage>> {MLIL_MEM_PHI, {DestMemoryVersionMediumLevelOperandUsage, SourceMemoryVersionsMediumLevelOperandUsage}}, {MLIL_CONST, {ConstantMediumLevelOperandUsage}}, {MLIL_CONST_PTR, {ConstantMediumLevelOperandUsage}}, - {MLIL_EXTERN_PTR, {ConstantMediumLevelOperandUsage}}, + {MLIL_EXTERN_PTR, {ConstantMediumLevelOperandUsage, OffsetMediumLevelOperandUsage}}, {MLIL_FLOAT_CONST, {ConstantMediumLevelOperandUsage}}, {MLIL_IMPORT, {ConstantMediumLevelOperandUsage}}, {MLIL_ADD, {LeftExprMediumLevelOperandUsage, RightExprMediumLevelOperandUsage}}, diff --git a/relocationhandler.cpp b/relocationhandler.cpp index cda9bf0d..b077a82d 100644 --- a/relocationhandler.cpp +++ b/relocationhandler.cpp @@ -37,6 +37,7 @@ RelocationHandler::RelocationHandler() handler.freeObject = FreeCallback; handler.getRelocationInfo = GetRelocationInfoCallback; handler.applyRelocation = ApplyRelocationCallback; + handler.getOperandForExternalRelocation = GetOperandForExternalRelocationCallback; AddRefForRegistration(); m_object = BNCreateRelocationHandler(&handler); @@ -77,6 +78,16 @@ bool RelocationHandler::ApplyRelocationCallback(void* ctxt, BNBinaryView* view, } +size_t RelocationHandler::GetOperandForExternalRelocationCallback(void* ctxt, const uint8_t* data, uint64_t addr, + size_t length, BNLowLevelILFunction* il, BNRelocation* reloc) +{ + RelocationHandler* handler = (RelocationHandler*)ctxt; + Ref<LowLevelILFunction> func(new LowLevelILFunction(BNNewLowLevelILFunctionReference(il))); + Ref<Relocation> relocObj = new Relocation(BNNewRelocationReference(reloc)); + return handler->GetOperandForExternalRelocation(data, addr, length, func, relocObj); +} + + bool RelocationHandler::GetRelocationInfo(Ref<BinaryView> view, Ref<Architecture> arch, std::vector<BNRelocationInfo>& result) { (void)view; @@ -89,9 +100,18 @@ bool RelocationHandler::GetRelocationInfo(Ref<BinaryView> view, Ref<Architecture bool RelocationHandler::ApplyRelocation(Ref<BinaryView> view, Ref<Architecture> arch, Ref<Relocation> reloc, uint8_t* dest, size_t len) { - return BNRelocationHandlerDefaultApplyRelocation(m_object, view->GetObject(), arch->GetObject(), BNNewRelocationReference(reloc->GetObject()), dest, len); + return BNRelocationHandlerDefaultApplyRelocation(m_object, view->GetObject(), arch->GetObject(), + BNNewRelocationReference(reloc->GetObject()), dest, len); } + +size_t RelocationHandler::GetOperandForExternalRelocation(const uint8_t* data, uint64_t addr, size_t length, + Ref<LowLevelILFunction> il, Ref<Relocation> relocation) +{ + return BN_INVALID_OPERAND; +} + + CoreRelocationHandler::CoreRelocationHandler(BNRelocationHandler* handler) : RelocationHandler(handler) { } @@ -114,4 +134,12 @@ bool CoreRelocationHandler::GetRelocationInfo(Ref<BinaryView> view, Ref<Architec for (size_t i = 0; i < result.size(); i++) result[i] = results[i]; return status; +} + + +size_t CoreRelocationHandler::GetOperandForExternalRelocation(const uint8_t* data, uint64_t addr, size_t length, + Ref<LowLevelILFunction> il, Ref<Relocation> relocation) +{ + return BNRelocationHandlerGetOperandForExternalRelocation(m_object, data, addr, length, il->GetObject(), + relocation->GetObject()); }
\ No newline at end of file |
