From dde32cdf497fe54312eca476bc32e147671d3c0d Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Fri, 20 Mar 2015 01:21:01 -0400 Subject: Code cross references, undo for creation of functions --- binaryninjaapi.h | 12 ++++++++++++ binaryview.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/binaryninjaapi.h b/binaryninjaapi.h index d7c2c091..26bf6759 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -338,6 +338,13 @@ namespace BinaryNinja uint64_t GetAddress() const; }; + struct ReferenceSource + { + Ref func; + Ref arch; + uint64_t addr; + }; + class BinaryView: public RefCountObject { protected: @@ -352,6 +359,7 @@ namespace BinaryNinja static size_t InsertCallback(void* ctxt, uint64_t offset, const void* src, size_t len); static size_t RemoveCallback(void* ctxt, uint64_t offset, uint64_t len); static BNModificationStatus GetModificationCallback(void* ctxt, uint64_t offset); + static bool IsValidOffsetCallback(void* ctxt, uint64_t offset); static uint64_t GetStartCallback(void* ctxt); static uint64_t GetLengthCallback(void* ctxt); static uint64_t GetEntryPointCallback(void* ctxt); @@ -364,6 +372,7 @@ namespace BinaryNinja virtual size_t PerformRemove(uint64_t offset, uint64_t len) { (void)offset; (void)len; return 0; } virtual BNModificationStatus PerformGetModification(uint64_t offset) { (void)offset; return Original; } + virtual bool PerformIsValidOffset(uint64_t offset); virtual uint64_t PerformGetStart() const { return 0; } virtual uint64_t PerformGetLength() const { return 0; } virtual uint64_t PerformGetEntryPoint() const { return 0; } @@ -423,6 +432,7 @@ namespace BinaryNinja void AddFunctionForAnalysis(Architecture* arch, uint64_t addr); void AddEntryPointForAnalysis(Architecture* arch, uint64_t start); + void RemoveAnalysisFunction(Function* func); void UpdateAnalysis(); void AbortAnalysis(); @@ -435,6 +445,8 @@ namespace BinaryNinja Ref GetRecentBasicBlockForAddress(uint64_t addr); std::vector> GetBasicBlocksForAddress(uint64_t addr); + std::vector GetCodeReferences(uint64_t addr); + Ref GetSymbolByAddress(uint64_t addr); Ref GetSymbolByRawName(const std::string& name); std::vector> GetSymbolsByName(const std::string& name); diff --git a/binaryview.cpp b/binaryview.cpp index 2f6e42ef..a965d67c 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -188,6 +188,13 @@ BNModificationStatus BinaryView::GetModificationCallback(void* ctxt, uint64_t of } +bool BinaryView::IsValidOffsetCallback(void* ctxt, uint64_t offset) +{ + BinaryView* view = (BinaryView*)ctxt; + return view->PerformIsValidOffset(offset); +} + + uint64_t BinaryView::GetStartCallback(void* ctxt) { BinaryView* view = (BinaryView*)ctxt; @@ -224,6 +231,13 @@ bool BinaryView::SaveCallback(void* ctxt, BNFileAccessor* file) } +bool BinaryView::PerformIsValidOffset(uint64_t offset) +{ + uint8_t val; + return PerformRead(&val, offset, 1) == 1; +} + + bool BinaryView::IsModified() const { return BNIsViewModified(m_view); @@ -425,6 +439,12 @@ void BinaryView::AddEntryPointForAnalysis(Architecture* arch, uint64_t addr) } +void BinaryView::RemoveAnalysisFunction(Function* func) +{ + BNRemoveAnalysisFunction(m_view, func->GetFunctionObject()); +} + + void BinaryView::UpdateAnalysis() { BNUpdateAnalysis(m_view); @@ -515,6 +535,25 @@ vector> BinaryView::GetBasicBlocksForAddress(uint64_t addr) } +vector BinaryView::GetCodeReferences(uint64_t addr) +{ + size_t count; + BNReferenceSource* refs = BNGetCodeReferences(m_view, addr, &count); + + vector result; + for (size_t i = 0; i < count; i++) + { + ReferenceSource src; + src.func = new Function(BNNewFunctionReference(refs[i].func)); + src.arch = new CoreArchitecture(refs[i].arch); + src.addr = refs[i].addr; + } + + BNFreeCodeReferences(refs, count); + return result; +} + + Ref BinaryView::GetSymbolByAddress(uint64_t addr) { BNSymbol* sym = BNGetSymbolByAddress(m_view, addr); -- cgit v1.3.1