diff options
| author | Peter LaFosse <peter@vector35.com> | 2018-05-07 16:03:13 -0400 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2018-05-07 16:03:13 -0400 |
| commit | f3040b9ef97f6dc521a14fa7a12c4a8d9b953e21 (patch) | |
| tree | de2ef3beb56bca7a185bf1efbbd8c085757096b6 | |
| parent | 0ee4eae0a56ecbf949c0661058483b40e81a3d84 (diff) | |
Refactor supporting calling convention like approach
| -rw-r--r-- | architecture.cpp | 110 | ||||
| -rw-r--r-- | binaryninjaapi.h | 69 | ||||
| -rw-r--r-- | binaryninjacore.h | 95 | ||||
| -rw-r--r-- | binaryview.cpp | 79 | ||||
| -rw-r--r-- | callingconvention.cpp | 2 | ||||
| -rw-r--r-- | relocationhandler.cpp | 114 |
6 files changed, 290 insertions, 179 deletions
diff --git a/architecture.cpp b/architecture.cpp index ea27d805..36241ac0 100644 --- a/architecture.cpp +++ b/architecture.cpp @@ -20,6 +20,7 @@ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> +#include <cstdint> #include <inttypes.h> #include "binaryninjaapi.h" @@ -152,19 +153,6 @@ bool Architecture::GetInstructionInfoCallback(void* ctxt, const uint8_t* data, u } -bool Architecture::GetRelocationInfoCallback(void* ctxt, BNBinaryView* view, uint64_t relocType, - BNRelocationInfo* result) -{ - Architecture* arch = (Architecture*)ctxt; - - BNRelocationInfo info = *result; - Ref<BinaryView> bv = new BinaryView(BNNewViewReference(view)); - bool ok = arch->GetRelocationInfo(bv, relocType, info); - *result = info; - return ok; -} - - bool Architecture::GetInstructionTextCallback(void* ctxt, const uint8_t* data, uint64_t addr, size_t* len, BNInstructionTextToken** result, size_t* count) { @@ -653,33 +641,6 @@ bool Architecture::SkipAndReturnValueCallback(void* ctxt, uint8_t* data, uint64_ } -// bool Architecture::ApplyPERelocationCallback(void* ctxt, BNBinaryView* view, BNRelocation* rel, uint8_t* data, size_t len) -// { -// Architecture* arch = (Architecture*)ctxt; -// Ref<BinaryView> bv = new BinaryView(BNNewViewReference(view)); -// Ref<Relocation> reloc = new Relocation(BNNewRelocationReference(rel)); -// return arch->ApplyPERelocation(bv, reloc, data, len); -// } - - -bool Architecture::ApplyELFRelocationCallback(void* ctxt, BNBinaryView* view, BNRelocationInfo* rel, uint8_t* data, size_t len) -{ - Architecture* arch = (Architecture*)ctxt; - Ref<BinaryView> bv = new BinaryView(BNNewViewReference(view)); - BNRelocationInfo reloc = *rel; - return arch->ApplyELFRelocation(bv, reloc, data, len); -} - - -// bool Architecture::ApplyMachoRelocationCallback(void* ctxt, BNBinaryView* view, BNRelocation* rel, uint8_t* data, size_t len) -// { -// Architecture* arch = (Architecture*)ctxt; -// Ref<BinaryView> bv = new BinaryView(BNNewViewReference(view)); -// Ref<Relocation> reloc = new Relocation(BNNewRelocationReference(rel)); -// return arch->ApplyMachoRelocation(bv, reloc, data, len); -// } - - void Architecture::Register(BNCustomArchitecture* callbacks) { AddRefForRegistration(); @@ -748,10 +709,6 @@ void Architecture::Register(Architecture* arch) callbacks.alwaysBranch = AlwaysBranchCallback; callbacks.invertBranch = InvertBranchCallback; callbacks.skipAndReturnValue = SkipAndReturnValueCallback; - // callbacks.applyPERelocation = ApplyPERelocationCallback; - callbacks.applyELFRelocation = ApplyELFRelocationCallback; - // callbacks.applyMachoRelocation = ApplyMachoRelocationCallback; - callbacks.getRelocationInfo = GetRelocationInfoCallback; arch->Register(&callbacks); } @@ -1164,6 +1121,17 @@ void Architecture::RegisterFunctionRecognizer(FunctionRecognizer* recog) } +void Architecture::RegisterRelocationHandler(const string& viewName, RelocationHandler* handler) +{ + BNArchitectureRegisterRelocationHandler(m_object, viewName.c_str(), handler->GetObject()); +} + + +Ref<RelocationHandler> Architecture::GetRelocationHandler(const std::string& viewName) +{ + return new CoreRelocationHandler(BNArchitectureGetRelocationHandler(m_object, viewName.c_str())); +} + bool Architecture::IsBinaryViewTypeConstantDefined(const string& type, const string& name) { return BNIsBinaryViewTypeArchitectureConstantDefined(m_object, type.c_str(), name.c_str()); @@ -1278,37 +1246,6 @@ Ref<Platform> Architecture::GetStandalonePlatform() } -// bool Architecture::ApplyPERelocation(BinaryView* view, Relocation* rel, uint8_t* dest, size_t len) -// { -// (void)view; -// (void)rel; -// (void)dest; -// (void)len; -// return true; -// } - - -bool Architecture::ApplyELFRelocation(BinaryView* view, BNRelocationInfo& rel, uint8_t* dest, size_t len) -{ - (void)view; - (void)rel; - (void)dest; - (void)len; - - return true; -} - - -// bool Architecture::ApplyMachoRelocation(BinaryView* view, Relocation* rel, uint8_t* dest, size_t len) -// { -// (void)view; -// (void)rel; -// (void)dest; -// (void)len; -// return true; -// } - - void Architecture::AddArchitectureRedirection(Architecture* from, Architecture* to) { BNAddArchitectureRedirection(m_object, from->GetObject(), to->GetObject()); @@ -1801,29 +1738,6 @@ bool CoreArchitecture::SkipAndReturnValue(uint8_t* data, uint64_t addr, size_t l } -// bool CoreArchitecture::ApplyPERelocation(BinaryView* view, Relocation* rel, uint8_t* dest, size_t len) -// { -// return BNArchitectureApplyPERelocation(m_object, view->GetObject(), rel->GetObject(), dest, len); -// } - - -bool CoreArchitecture::ApplyELFRelocation(BinaryView* view, BNRelocationInfo& rel, uint8_t* dest, size_t len) -{ - return BNArchitectureApplyELFRelocation(m_object, view->GetObject(), &rel, dest, len); -} - - -// bool CoreArchitecture::ApplyMachoRelocation(BinaryView* view, Relocation* rel, uint8_t* dest, size_t len) -// { -// return BNArchitectureApplyMachoRelocation(m_object, view->GetObject(), rel->GetObject(), dest, len); -// } - - -bool CoreArchitecture::GetRelocationInfo(BinaryView* view, uint64_t relocType, BNRelocationInfo& reloc) -{ - return BNGetRelocationInfo(m_object, view->GetObject(), relocType, &reloc); -} - ArchitectureExtension::ArchitectureExtension(const string& name, Architecture* base): Architecture(name), m_base(base) { } diff --git a/binaryninjaapi.h b/binaryninjaapi.h index ad28273d..b9405a07 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -19,7 +19,6 @@ // IN THE SOFTWARE. #pragma once - #ifdef WIN32 #define NOMINMAX #include <windows.h> @@ -34,6 +33,7 @@ #include <set> #include <mutex> #include <memory> +#include <cstdint> #include "binaryninjacore.h" #include "json/json.h" @@ -1384,6 +1384,22 @@ namespace BinaryNinja void SetMaxFunctionSizeForAnalysis(uint64_t size); }; + + class Relocation: public CoreRefCountObject<BNRelocation, BNNewRelocationReference, BNFreeRelocation> + { + public: + Relocation(BNRelocation* reloc); + BNRelocationInfo GetInfo() const; + Architecture* GetArchitecture() const; + Ref<Symbol> GetSymbol(); + uint64_t GetSymbolOffset() const; + uint64_t GetTargetAddress() const; + Ref<Segment> GetSegment(); + uint64_t GetSegmentOffset() const; + uint64_t GetDestAddress() const; + }; + + class BinaryData: public BinaryView { public: @@ -1624,6 +1640,7 @@ namespace BinaryNinja class MediumLevelILFunction; class FunctionRecognizer; class CallingConvention; + class RelocationHandler; typedef size_t ExprId; @@ -1707,11 +1724,6 @@ namespace BinaryNinja static bool InvertBranchCallback(void* ctxt, uint8_t* data, uint64_t addr, size_t len); static bool SkipAndReturnValueCallback(void* ctxt, uint8_t* data, uint64_t addr, size_t len, uint64_t value); - // static bool ApplyPERelocationCallback(void* ctxt, BNBinaryView* view, BNRelocation* rel, uint8_t* data, size_t len); - static bool ApplyELFRelocationCallback(void* ctxt, BNBinaryView* view, BNRelocationInfo* rel, uint8_t* data, size_t len); - // static bool ApplyMachoRelocationCallback(void* ctxt, BNBinaryView* view, BNRelocation* rel, uint8_t* data, size_t len); - static bool GetRelocationInfoCallback(void* ctxt, BNBinaryView* view, uint64_t relocType, BNRelocationInfo* result); - virtual void Register(BNCustomArchitecture* callbacks); public: @@ -1859,6 +1871,8 @@ namespace BinaryNinja virtual bool SkipAndReturnValue(uint8_t* data, uint64_t addr, size_t len, uint64_t value); void RegisterFunctionRecognizer(FunctionRecognizer* recog); + void RegisterRelocationHandler(const std::string& viewName, RelocationHandler* handler); + Ref<RelocationHandler> GetRelocationHandler(const std::string& viewName); bool IsBinaryViewTypeConstantDefined(const std::string& type, const std::string& name); uint64_t GetBinaryViewTypeConstant(const std::string& type, const std::string& name, @@ -1879,15 +1893,6 @@ namespace BinaryNinja Ref<CallingConvention> GetFastcallCallingConvention(); Ref<Platform> GetStandalonePlatform(); - //virtual bool ApplyPERelocation(BinaryView* view, BNRelocationInfo& rel, uint8_t* dest, size_t len); - virtual bool ApplyELFRelocation(BinaryView* view, BNRelocationInfo& rel, uint8_t* dest, size_t len); - // virtual bool ApplyMachoRelocation(BinaryView* view, BNRelocationInfo& rel, uint8_t* dest, size_t len); - virtual bool GetRelocationInfo(BinaryView* view, uint64_t relocType, BNRelocationInfo& result) - { - (void)view; (void)relocType; (void)result; - return false; - } - void AddArchitectureRedirection(Architecture* from, Architecture* to); }; @@ -1909,10 +1914,6 @@ namespace BinaryNinja virtual std::string GetRegisterName(uint32_t reg) override; virtual std::string GetFlagName(uint32_t flag) override; virtual std::string GetFlagWriteTypeName(uint32_t flags) override; - //virtual bool ApplyPERelocation(BinaryView* view, Relocation* rel, uint8_t* dest, size_t len) override; - virtual bool ApplyELFRelocation(BinaryView* view, BNRelocationInfo& rel, uint8_t* dest, size_t len) override; - //virtual bool ApplyMachoRelocation(BinaryView* view, Relocation* rel, uint8_t* dest, size_t len) override; - virtual bool GetRelocationInfo(BinaryView* view, uint64_t relocType, BNRelocationInfo& result) override; virtual std::string GetSemanticFlagClassName(uint32_t semClass) override; virtual std::string GetSemanticFlagGroupName(uint32_t semGroup) override; @@ -3335,6 +3336,36 @@ namespace BinaryNinja virtual bool RecognizeMediumLevelIL(BinaryView* data, Function* func, MediumLevelILFunction* il); }; + class RelocationHandler: public CoreRefCountObject<BNRelocationHandler, + BNNewRelocationHandlerReference, BNFreeRelocationHandler> + { + static bool GetRelocationInfoCallback(void* ctxt, BNBinaryView* view, BNArchitecture* arch, + BNRelocationInfo* result, size_t resultCount); + static bool ApplyRelocationCallback(void* ctxt, BNBinaryView* view, BNArchitecture* arch, BNRelocation* reloc, + uint8_t* dest, size_t len); + + 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); + }; + + 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; + }; + class UpdateException: public std::exception { const std::string m_desc; diff --git a/binaryninjacore.h b/binaryninjacore.h index 47f95700..0f147f27 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -146,6 +146,7 @@ extern "C" struct BNSegment; struct BNSection; struct BNRelocationInfo; + struct BNRelocationHandler; typedef bool (*BNLoadPluginCallback)(const char* repoPath, const char* pluginPath, void* ctx); @@ -1021,15 +1022,6 @@ extern "C" size_t (*write)(void* ctxt, uint64_t offset, const void* src, size_t len); }; - enum BNRelocationType - { - ELFGlobalRelocationType, - ELFCopyRelocationType, - ELFJumpSlotRelocationType, - StandardRelocationType, - CustomRelocationType - }; - struct BNCustomBinaryView { void* context; @@ -1060,15 +1052,6 @@ extern "C" uint64_t secOffset, BNSegment* seg, uint64_t segOffset); }; - struct BNCustomRelocation - { - void* context; - BNArchitecture* arch; - bool (*init)(void* ctxt); - void (*freeRelocation)(void* ctxt); - bool (*applyRelocation)(void* ctxt, BNBinaryView* view, uint8_t* dest, size_t len); - }; - struct BNCustomBinaryViewType { void* context; @@ -1109,24 +1092,30 @@ extern "C" BNArchitecture* branchArch[BN_MAX_INSTRUCTION_BRANCHES]; // If null, same architecture as instruction }; - enum BNRelativeRelocationType + enum BNRelocationType { - NotRelativeRelocationType, - SegmentRelativeRelocationType, - BaseRelativeRelocationType + ELFGlobalRelocationType, + ELFCopyRelocationType, + ELFJumpSlotRelocationType, + StandardRelocationType, + IgnoredRelocation }; struct BNRelocationInfo { - BNRelocationType bnType; // BinaryNinja Relocation Type + 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) - bool hasSign; // Addend should be subtracted size_t size; // Size of the data to be written size_t truncateSize; // After addition/subtraction truncate to - uint64_t type; // Base type from relocation entry + uint64_t nativeType; // Base type from relocation entry size_t addend; // Addend value from relocation entry + bool hasSign; // Addend should be subtracted bool implicitAddend; // Addend should be read from the BinaryView + bool external; // Relocation entry points to external symbol + size_t symbolIndex; // Index into symbol table + size_t sectionIndex; // Index into the section table + uint64_t address; // Absolute address or segment offset }; struct BNInstructionTextToken @@ -1234,11 +1223,6 @@ extern "C" bool (*alwaysBranch)(void* ctxt, uint8_t* data, uint64_t addr, size_t len); bool (*invertBranch)(void* ctxt, uint8_t* data, uint64_t addr, size_t len); bool (*skipAndReturnValue)(void* ctxt, uint8_t* data, uint64_t addr, size_t len, uint64_t value); - - //bool (*applyPERelocation)(void* ctxt, BNBinaryView* view, BNRelocation* rel, uint8_t* data, size_t len); - bool (*applyELFRelocation)(void* ctxt, BNBinaryView* view, BNRelocationInfo* rel, uint8_t* data, size_t len); - //bool (*applyMachoRelocation)(void* ctxt, BNBinaryView* view, BNRelocation* rel, uint8_t* data, size_t len); - bool (*getRelocationInfo)(void* ctxt, BNBinaryView* view, uint64_t relocType, BNRelocationInfo* result); }; struct BNBasicBlockEdge @@ -1385,6 +1369,17 @@ extern "C" bool (*recognizeMediumLevelIL)(void* ctxt, BNBinaryView* data, BNFunction* func, BNMediumLevelILFunction* il); }; + struct BNCustomRelocationHandler + { + void* context; + void (*freeObject)(void* ctxt); + + bool (*getRelocationInfo)(void* ctxt, BNBinaryView* view, BNArchitecture* arch, BNRelocationInfo* result, + size_t resultCount); + bool (*applyRelocation)(void* ctxt, BNBinaryView* view, BNArchitecture* arch, BNRelocation* reloc, uint8_t* dest, + size_t len); + }; + struct BNTypeParserResult { BNQualifiedNameAndType* types; @@ -2058,12 +2053,6 @@ extern "C" BINARYNINJACOREAPI BNBinaryView* BNCreateCustomBinaryView(const char* name, BNFileMetadata* file, BNBinaryView* parent, BNCustomBinaryView* view); - // Creation of new types of relocations - // BINARYNINJACOREAPI BNRelocation* BNCreateRelocation(BNArchitecture* arch, size_t size, BNSymbol* sym, uint64_t addend, - // uint64_t segmentOffset, BNCustomRelocation* reloc); - // BINARYNINJACOREAPI BNRelocation* BNNewRelocationReference(BNRelocation* reloc); - // BINARYNINJACOREAPI void BNFreeRelocation(BNRelocation* reloc); - // Binary view type management BINARYNINJACOREAPI BNBinaryViewType* BNGetBinaryViewTypeByName(const char* name); BINARYNINJACOREAPI BNBinaryViewType** BNGetBinaryViewTypes(size_t* count); BINARYNINJACOREAPI BNBinaryViewType** BNGetBinaryViewTypesForData(BNBinaryView* data, size_t* count); @@ -2170,8 +2159,6 @@ extern "C" BINARYNINJACOREAPI BNArchitecture* BNGetAssociatedArchitectureByAddress(BNArchitecture* arch, uint64_t* addr); BINARYNINJACOREAPI bool BNGetInstructionInfo(BNArchitecture* arch, const uint8_t* data, uint64_t addr, size_t maxLen, BNInstructionInfo* result); - BINARYNINJACOREAPI bool BNGetRelocationInfo(BNArchitecture* arch, BNBinaryView* view, uint64_t relocType, - BNRelocationInfo* result); BINARYNINJACOREAPI bool BNGetInstructionText(BNArchitecture* arch, const uint8_t* data, uint64_t addr, size_t* len, BNInstructionTextToken** result, size_t* count); BINARYNINJACOREAPI bool BNGetInstructionLowLevelIL(BNArchitecture* arch, const uint8_t* data, uint64_t addr, @@ -2250,11 +2237,7 @@ extern "C" BINARYNINJACOREAPI bool BNArchitectureInvertBranch(BNArchitecture* arch, uint8_t* data, uint64_t addr, size_t len); BINARYNINJACOREAPI bool BNArchitectureSkipAndReturnValue(BNArchitecture* arch, uint8_t* data, uint64_t addr, size_t len, uint64_t value); - //BINARYNINJACOREAPI bool BNArchitectureApplyPERelocation(BNArchitecture* arch, BNBinaryView* view, BNRelocation* rel, uint8_t* dest, size_t len); - BINARYNINJACOREAPI bool BNArchitectureApplyELFRelocation(BNArchitecture* arch, BNBinaryView* view, BNRelocationInfo* rel, uint8_t* dest, size_t len); - //BINARYNINJACOREAPI bool BNArchitectureApplyMachoRelocation(BNArchitecture* arch, BNBinaryView* view, BNRelocation* rel, uint8_t* dest, size_t len); BINARYNINJACOREAPI void BNRegisterArchitectureFunctionRecognizer(BNArchitecture* arch, BNFunctionRecognizer* rec); - BINARYNINJACOREAPI bool BNIsBinaryViewTypeArchitectureConstantDefined(BNArchitecture* arch, const char* type, const char* name); BINARYNINJACOREAPI uint64_t BNGetBinaryViewTypeArchitectureConstant(BNArchitecture* arch, const char* type, @@ -2262,6 +2245,18 @@ extern "C" BINARYNINJACOREAPI void BNSetBinaryViewTypeArchitectureConstant(BNArchitecture* arch, const char* type, const char* name, uint64_t value); + BINARYNINJACOREAPI void BNArchitectureRegisterRelocationHandler(BNArchitecture* arch, const char* viewName, + BNRelocationHandler* handler); + BINARYNINJACOREAPI BNRelocationHandler* BNCreateRelocationHandler(BNCustomRelocationHandler* handler); + BINARYNINJACOREAPI BNRelocationHandler* BNArchitectureGetRelocationHandler(BNArchitecture* arch, const char* viewName); + BINARYNINJACOREAPI BNRelocationHandler* BNNewRelocationHandlerReference(BNRelocationHandler* handler); + BINARYNINJACOREAPI void BNFreeRelocationHandler(BNRelocationHandler* handler); + BINARYNINJACOREAPI bool BNRelocationHandlerGetRelocationInfo(BNRelocationHandler* handler, BNBinaryView* data, + BNArchitecture* arch, BNRelocationInfo* info, size_t infoCount); + BINARYNINJACOREAPI bool BNRelocationHandlerApplyRelocation(BNRelocationHandler* handler, BNBinaryView* view, + 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); // Analysis BINARYNINJACOREAPI void BNAddAnalysisOption(BNBinaryView* view, const char* name); BINARYNINJACOREAPI void BNAddFunctionForAnalysis(BNBinaryView* view, BNPlatform* platform, uint64_t addr); @@ -3508,7 +3503,19 @@ extern "C" BINARYNINJACOREAPI char* BNGetLinuxCADirectory(); BINARYNINJACOREAPI char* BNGetLinuxCABundlePath(); - // Segment Operations + // Relocation object methods + BINARYNINJACOREAPI BNRelocation* BNNewRelocationReference(BNRelocation* reloc); + BINARYNINJACOREAPI void BNFreeRelocation(BNRelocation* reloc); + BINARYNINJACOREAPI BNRelocationInfo BNRelocationGetInfo(BNRelocation* reloc); + BINARYNINJACOREAPI BNArchitecture* BNRelocationGetArchitecture(BNRelocation* reloc); + BINARYNINJACOREAPI BNSymbol* BNRelocationGetSymbol(BNRelocation* reloc); + BINARYNINJACOREAPI uint64_t BNRelocationGetSymbolOffset(BNRelocation* reloc); + BINARYNINJACOREAPI uint64_t BNRelocationGetTargetAddress(BNRelocation* reloc); + BINARYNINJACOREAPI BNSegment* BNRelocationGetSegment(BNRelocation* reloc); + BINARYNINJACOREAPI uint64_t BNRelocationGetSegmentOffset(BNRelocation* reloc); + BINARYNINJACOREAPI uint64_t BNRelocationGetDestAddress(BNRelocation* reloc); + + // Segment object methods BINARYNINJACOREAPI BNSegment* BNCreateSegment(uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, uint32_t flags, bool autoDefined); BINARYNINJACOREAPI BNSegment* BNNewSegmentReference(BNSegment* seg); @@ -3525,9 +3532,9 @@ extern "C" BINARYNINJACOREAPI void BNSegmentSetDataOffset(BNSegment* segment, uint64_t dataOffset); BINARYNINJACOREAPI void BNSegmentSetDataLength(BNSegment* segment, uint64_t dataLength); BINARYNINJACOREAPI void BNSegmentSetFlags(BNSegment* segment, uint32_t flags); - // BINARYNINJACOREAPI bool BNSegmentAddRelocation(BNSegment* segment, BNRelocation* rel); BINARYNINJACOREAPI size_t BNSegmentRead(BNSegment* segment, BNBinaryView* view, uint8_t* dest, uint64_t offset, size_t len); + // Section object methods BINARYNINJACOREAPI BNSection* BNNewSectionReference(BNSection* section); BINARYNINJACOREAPI void BNFreeSection(BNSection* section); BINARYNINJACOREAPI char* BNSectionGetName(BNSection* section); diff --git a/binaryview.cpp b/binaryview.cpp index 22194a58..055608a4 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -423,9 +423,6 @@ bool Section::AutoDefined() const } - - - BinaryView::BinaryView(const std::string& typeName, FileMetadata* file, BinaryView* parentView) { BNCustomBinaryView view; @@ -619,10 +616,10 @@ void BinaryView::DefineRelocationCallback(void* ctxt, BNArchitecture* arch, BNRe uint64_t symOffset, BNSegment* seg, uint64_t segOffset) { BinaryView* view = (BinaryView*)ctxt; - Architecture* curArch = new CoreArchitecture(arch); BNRelocationInfo curInfo = *info; - Ref<Symbol> curSym = new Symbol(sym); - Ref<Segment> curSeg = new Segment(seg); + Ref<Symbol> curSym = new Symbol(BNNewSymbolReference(sym)); + Ref<Segment> curSeg = new Segment(BNNewSegmentReference(seg)); + Architecture* curArch = new CoreArchitecture(arch); return view->PerformDefineRelocation(curArch, curInfo, curSym, symOffset, curSeg, segOffset); } @@ -631,10 +628,10 @@ void BinaryView::DefineSectionRelocationCallback(void* ctxt, BNArchitecture* arc uint64_t secOffset, BNSegment* seg, uint64_t segOffset) { BinaryView* view = (BinaryView*)ctxt; - Architecture* curArch = new CoreArchitecture(arch); BNRelocationInfo curInfo = *info; - Ref<Section> curSec = new Section(sec); - Ref<Segment> curSeg = new Segment(seg); + Ref<Section> curSec = new Section(BNNewSectionReference(sec)); + Ref<Segment> curSeg = new Segment(BNNewSegmentReference(seg)); + Architecture* curArch = new CoreArchitecture(arch); return view->PerformDefineRelocation(curArch, curInfo, curSec, secOffset, curSeg, segOffset); } @@ -715,20 +712,14 @@ bool BinaryView::PerformSave(FileAccessor* file) void BinaryView::PerformDefineRelocation(Architecture* arch, BNRelocationInfo& info, const Ref<Symbol> sym, uint64_t symOffset, const Ref<Segment> seg, uint64_t segOffset) { - Ref<BinaryView> parent = GetParentView(); - if (parent) - parent->DefineRelocation(arch, info, sym, symOffset, seg, segOffset); - return; + DefineRelocation(arch, info, sym, symOffset, seg, segOffset); } void BinaryView::PerformDefineRelocation(Architecture* arch, BNRelocationInfo& info, const Ref<Section> sec, uint64_t secOffset, const Ref<Segment> seg, uint64_t segOffset) { - Ref<BinaryView> parent = GetParentView(); - if (parent) - parent->DefineRelocation(arch, info, sec, secOffset, seg, segOffset); - return; + DefineRelocation(arch, info, sec, secOffset, seg, segOffset); } @@ -2151,6 +2142,60 @@ void BinaryView::SetMaxFunctionSizeForAnalysis(uint64_t size) } +Relocation::Relocation(BNRelocation* reloc) +{ + m_object = reloc; +} + + +BNRelocationInfo Relocation::GetInfo() const +{ + return BNRelocationGetInfo(m_object); +} + + +Architecture* Relocation::GetArchitecture() const +{ + return new CoreArchitecture(BNRelocationGetArchitecture(m_object)); +} + + +Ref<Symbol> Relocation::GetSymbol() +{ + return new Symbol(BNNewSymbolReference(BNRelocationGetSymbol(m_object))); +} + + +uint64_t Relocation::GetSymbolOffset() const +{ + return BNRelocationGetSymbolOffset(m_object); +} + + +uint64_t Relocation::GetTargetAddress() const +{ + return BNRelocationGetTargetAddress(m_object); +} + + +Ref<Segment> Relocation::GetSegment() +{ + return new Segment(BNNewSegmentReference(BNRelocationGetSegment(m_object))); +} + + +uint64_t Relocation::GetSegmentOffset() const +{ + return BNRelocationGetSegmentOffset(m_object); +} + + +uint64_t Relocation::GetDestAddress() const +{ + return BNRelocationGetDestAddress(m_object); +} + + BinaryData::BinaryData(FileMetadata* file): BinaryView(BNCreateBinaryDataView(file->GetObject())) { } diff --git a/callingconvention.cpp b/callingconvention.cpp index a56b8f11..3a7298a2 100644 --- a/callingconvention.cpp +++ b/callingconvention.cpp @@ -432,4 +432,4 @@ Variable CoreCallingConvention::GetIncomingVariableForParameterVariable(const Va Variable CoreCallingConvention::GetParameterVariableForIncomingVariable(const Variable& var, Function* func) { return BNGetParameterVariableForIncomingVariable(m_object, &var, func ? func->GetObject() : nullptr); -} +}
\ No newline at end of file diff --git a/relocationhandler.cpp b/relocationhandler.cpp new file mode 100644 index 00000000..2f0cbbde --- /dev/null +++ b/relocationhandler.cpp @@ -0,0 +1,114 @@ +// Copyright (c) 2015-2017 Vector 35 LLC +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +#include "binaryninjaapi.h" + +using namespace std; +using namespace BinaryNinja; + + +RelocationHandler::RelocationHandler(BNRelocationHandler* handler) +{ + m_object = handler; +} + + +RelocationHandler::RelocationHandler() +{ + BNCustomRelocationHandler handler; + handler.context = this; + handler.freeObject = FreeCallback; + handler.getRelocationInfo = GetRelocationInfoCallback; + handler.applyRelocation = ApplyRelocationCallback; + + AddRefForRegistration(); + m_object = BNCreateRelocationHandler(&handler); +} + + +void RelocationHandler::FreeCallback(void* ctxt) +{ + RelocationHandler* handler = (RelocationHandler*)ctxt; + handler->ReleaseForRegistration(); +} + + +bool RelocationHandler::GetRelocationInfoCallback(void* ctxt, BNBinaryView* view, BNArchitecture* arch, + BNRelocationInfo* result, size_t resultCount) +{ + RelocationHandler* handler = (RelocationHandler*)ctxt; + Ref<BinaryView> viewObj = new BinaryView(BNNewViewReference(view)); + Ref<Architecture> archObj = new CoreArchitecture(arch); + if (!result) + return false; + vector<BNRelocationInfo> resultVector(&result[0], &result[resultCount]); + bool success = handler->GetRelocationInfo(viewObj, archObj, resultVector); + for (size_t i = 0; i < resultCount; i++) + result[i] = resultVector[i]; + return success; +} + + +bool RelocationHandler::ApplyRelocationCallback(void* ctxt, BNBinaryView* view, BNArchitecture* arch, BNRelocation* reloc, + uint8_t* dest, size_t len) +{ + RelocationHandler* handler = (RelocationHandler*)ctxt; + Ref<Architecture> archObj = new CoreArchitecture(arch); + Ref<BinaryView> viewObj = new BinaryView(BNNewViewReference(view)); + Ref<Relocation> relocObj = new Relocation(BNNewRelocationReference(reloc)); + return handler->ApplyRelocation(viewObj, archObj, relocObj, dest, len); +} + + +bool RelocationHandler::GetRelocationInfo(Ref<BinaryView> view, Ref<Architecture> arch, std::vector<BNRelocationInfo>& result) +{ + return false; +} + + +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); +} + +CoreRelocationHandler::CoreRelocationHandler(BNRelocationHandler* handler) : RelocationHandler(handler) +{ +} + + +bool CoreRelocationHandler::ApplyRelocation(Ref<BinaryView> view, Ref<Architecture> arch, Ref<Relocation> reloc, uint8_t* dest, + size_t len) +{ + return BNRelocationHandlerApplyRelocation(m_object, view->GetObject(), arch->GetObject(), BNNewRelocationReference(reloc->GetObject()), dest, len); +} + + +bool CoreRelocationHandler::GetRelocationInfo(Ref<BinaryView> view, Ref<Architecture> arch, std::vector<BNRelocationInfo>& result) +{ + BNRelocationInfo* results = new BNRelocationInfo[result.size()]; + for (size_t i = 0; i < result.size(); i++) + results[i] = result[i]; + bool status = BNRelocationHandlerGetRelocationInfo(m_object, view->GetObject(), arch->GetObject(), results, + result.size()); + for (size_t i = 0; i < result.size(); i++) + result[i] = results[i]; + return status; +}
\ No newline at end of file |
