From a40f0e1e494af967a35ca4a8977596f4693b5c0e Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Thu, 29 Sep 2016 18:41:13 -0400 Subject: Adding APIs for segments and sections --- binaryview.cpp | 220 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 216 insertions(+), 4 deletions(-) (limited to 'binaryview.cpp') diff --git a/binaryview.cpp b/binaryview.cpp index 863d57b2..98f6f334 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -18,6 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // IN THE SOFTWARE. +#include #include "binaryninjaapi.h" using namespace BinaryNinja; @@ -243,7 +244,7 @@ void AnalysisCompletionEvent::Cancel() } -BinaryView::BinaryView(const std::string& typeName, FileMetadata* file) +BinaryView::BinaryView(const std::string& typeName, FileMetadata* file, BinaryView* parentView) { BNCustomBinaryView view; view.context = this; @@ -270,7 +271,8 @@ BinaryView::BinaryView(const std::string& typeName, FileMetadata* file) m_file = file; AddRefForRegistration(); - m_object = BNCreateCustomBinaryView(typeName.c_str(), m_file->GetObject(), &view); + m_object = BNCreateCustomBinaryView(typeName.c_str(), m_file->GetObject(), + parentView ? parentView->GetObject() : nullptr, &view); } @@ -481,6 +483,15 @@ size_t BinaryView::PerformGetAddressSize() const } +bool BinaryView::PerformSave(FileAccessor* file) +{ + Ref parent = GetParentView(); + if (parent) + return parent->Save(file); + return false; +} + + void BinaryView::NotifyDataWritten(uint64_t offset, size_t len) { BNNotifyDataWritten(m_object, offset, len); @@ -499,6 +510,15 @@ void BinaryView::NotifyDataRemoved(uint64_t offset, uint64_t len) } +Ref BinaryView::GetParentView() const +{ + BNBinaryView* view = BNGetParentView(m_object); + if (!view) + return nullptr; + return new BinaryView(view); +} + + string BinaryView::GetTypeName() const { char* str = BNGetViewType(m_object); @@ -1225,7 +1245,7 @@ vector BinaryView::GetStrings() BNStringReference* strings = BNGetStrings(m_object, &count); vector result; result.insert(result.end(), strings, strings + count); - BNFreeStringList(strings); + BNFreeStringReferenceList(strings); return result; } @@ -1236,7 +1256,7 @@ vector BinaryView::GetStrings(uint64_t start, uint64_t len) BNStringReference* strings = BNGetStringsInRange(m_object, start, len, &count); vector result; result.insert(result.end(), strings, strings + count); - BNFreeStringList(strings); + BNFreeStringReferenceList(strings); return result; } @@ -1513,6 +1533,198 @@ bool BinaryView::GetAddressInput(uint64_t& result, const string& prompt, const s } +void BinaryView::AddAutoSegment(uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, + uint32_t flags) +{ + BNAddAutoSegment(m_object, start, length, dataOffset, dataLength, flags); +} + + +void BinaryView::RemoveAutoSegment(uint64_t start, uint64_t length) +{ + BNRemoveAutoSegment(m_object, start, length); +} + + +void BinaryView::AddUserSegment(uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, + uint32_t flags) +{ + BNAddUserSegment(m_object, start, length, dataOffset, dataLength, flags); +} + + +void BinaryView::RemoveUserSegment(uint64_t start, uint64_t length) +{ + BNRemoveUserSegment(m_object, start, length); +} + + +vector BinaryView::GetSegments() +{ + size_t count; + BNSegment* segments = BNGetSegments(m_object, &count); + + vector result; + for (size_t i = 0; i < count; i++) + { + Segment segment; + segment.start = segments[i].start; + segment.length = segments[i].length; + segment.dataOffset = segments[i].dataOffset; + segment.dataLength = segments[i].dataLength; + segment.flags = segments[i].flags; + result.push_back(segment); + } + + BNFreeSegmentList(segments); + return result; +} + + +bool BinaryView::GetSegmentAt(uint64_t addr, Segment& result) +{ + BNSegment segment; + if (!BNGetSegmentAt(m_object, addr, &segment)) + return false; + + result.start = segment.start; + result.length = segment.length; + result.dataOffset = segment.dataOffset; + result.dataLength = segment.dataLength; + result.flags = segment.flags; + return true; +} + + +void BinaryView::AddAutoSection(const string& name, uint64_t start, uint64_t length, const string& type, + uint64_t align, uint64_t entrySize, const string& linkedSection, const string& infoSection, uint64_t infoData) +{ + BNAddAutoSection(m_object, name.c_str(), start, length, type.c_str(), align, entrySize, linkedSection.c_str(), + infoSection.c_str(), infoData); +} + + +void BinaryView::RemoveAutoSection(const string& name) +{ + BNRemoveAutoSection(m_object, name.c_str()); +} + + +void BinaryView::AddUserSection(const string& name, uint64_t start, uint64_t length, const string& type, + uint64_t align, uint64_t entrySize, const string& linkedSection, const string& infoSection, uint64_t infoData) +{ + BNAddUserSection(m_object, name.c_str(), start, length, type.c_str(), align, entrySize, linkedSection.c_str(), + infoSection.c_str(), infoData); +} + + +void BinaryView::RemoveUserSection(const string& name) +{ + BNRemoveUserSection(m_object, name.c_str()); +} + + +vector
BinaryView::GetSections() +{ + size_t count; + BNSection* sections = BNGetSections(m_object, &count); + + vector
result; + for (size_t i = 0; i < count; i++) + { + Section section; + section.name = sections[i].name; + section.type = sections[i].type; + section.start = sections[i].start; + section.length = sections[i].length; + section.linkedSection = sections[i].linkedSection; + section.infoSection = sections[i].infoSection; + section.infoData = sections[i].infoData; + section.align = sections[i].align; + section.entrySize = sections[i].entrySize; + result.push_back(section); + } + + BNFreeSectionList(sections, count); + return result; +} + + +vector
BinaryView::GetSectionsAt(uint64_t addr) +{ + size_t count; + BNSection* sections = BNGetSectionsAt(m_object, addr, &count); + + vector
result; + for (size_t i = 0; i < count; i++) + { + Section section; + section.name = sections[i].name; + section.type = sections[i].type; + section.start = sections[i].start; + section.length = sections[i].length; + section.linkedSection = sections[i].linkedSection; + section.infoSection = sections[i].infoSection; + section.infoData = sections[i].infoData; + section.align = sections[i].align; + section.entrySize = sections[i].entrySize; + result.push_back(section); + } + + BNFreeSectionList(sections, count); + return result; +} + + +bool BinaryView::GetSectionByName(const string& name, Section& result) +{ + BNSection section; + if (!BNGetSectionByName(m_object, name.c_str(), §ion)) + return false; + + result.name = section.name; + result.type = section.type; + result.start = section.start; + result.length = section.length; + result.linkedSection = section.linkedSection; + result.infoSection = section.infoSection; + result.infoData = section.infoData; + result.align = section.align; + result.entrySize = section.entrySize; + + BNFreeSection(§ion); + return true; +} + + +vector BinaryView::GetUniqueSectionNames(const vector& names) +{ + const char** incomingNames = new const char*[names.size()]; + for (size_t i = 0; i < names.size(); i++) + incomingNames[i] = names[i].c_str(); + + char** outgoingNames = BNGetUniqueSectionNames(m_object, incomingNames, names.size()); + vector result; + for (size_t i = 0; i < names.size(); i++) + result.push_back(outgoingNames[i]); + + BNFreeStringList(outgoingNames, names.size()); + return result; +} + + +vector BinaryView::GetAllocatedRanges() +{ + size_t count; + BNAddressRange* ranges = BNGetAllocatedRanges(m_object, &count); + + vector result; + copy(&ranges[0], &ranges[count], back_inserter(result)); + BNFreeAddressRanges(ranges); + return result; +} + + BinaryData::BinaryData(FileMetadata* file): BinaryView(BNCreateBinaryDataView(file->GetObject())) { } -- cgit v1.3.1 From c99fd8fddc8734c121383d8d3e078674cb1a5e88 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Mon, 10 Oct 2016 22:01:28 -0400 Subject: Fix missing include --- binaryview.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'binaryview.cpp') diff --git a/binaryview.cpp b/binaryview.cpp index 98f6f334..ff19de18 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -19,6 +19,7 @@ // IN THE SOFTWARE. #include +#include #include "binaryninjaapi.h" using namespace BinaryNinja; -- cgit v1.3.1 From eef9d1eff05975c31862db4157ac2f1b2bb30502 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Mon, 31 Oct 2016 22:51:44 -0400 Subject: Added APIs for dealing with Thumb more easily --- binaryninjaapi.h | 2 ++ binaryninjacore.h | 3 +++ binaryview.cpp | 7 +++++++ platform.cpp | 9 +++++++++ python/__init__.py | 21 +++++++++++++++++++++ 5 files changed, 42 insertions(+) (limited to 'binaryview.cpp') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 6311271c..a918fc42 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -926,6 +926,7 @@ namespace BinaryNinja std::vector> GetSymbolsOfType(BNSymbolType type, uint64_t start, uint64_t len); void DefineAutoSymbol(Ref sym); + void DefineAutoSymbolAndVariableOrFunction(Ref platform, Ref sym, Ref type); void UndefineAutoSymbol(Ref sym); void DefineUserSymbol(Ref sym); @@ -2243,6 +2244,7 @@ namespace BinaryNinja Ref GetRelatedPlatform(Architecture* arch); void AddRelatedPlatform(Architecture* arch, Platform* platform); + Ref GetAssociatedPlatformByAddress(uint64_t& addr); }; class ScriptingOutputListener diff --git a/binaryninjacore.h b/binaryninjacore.h index ea81c388..b2ed7cd3 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -1877,6 +1877,8 @@ extern "C" BINARYNINJACOREAPI void BNDefineUserSymbol(BNBinaryView* view, BNSymbol* sym); BINARYNINJACOREAPI void BNUndefineUserSymbol(BNBinaryView* view, BNSymbol* sym); BINARYNINJACOREAPI void BNDefineImportedFunction(BNBinaryView* view, BNSymbol* importAddressSym, BNFunction* func); + BINARYNINJACOREAPI void BNDefineAutoSymbolAndVariableOrFunction(BNBinaryView* view, BNPlatform* platform, + BNSymbol* sym, BNType* type); BINARYNINJACOREAPI BNSymbol* BNImportedFunctionFromImportAddressSymbol(BNSymbol* sym, uint64_t addr); @@ -2135,6 +2137,7 @@ extern "C" BINARYNINJACOREAPI BNPlatform* BNGetRelatedPlatform(BNPlatform* platform, BNArchitecture* arch); BINARYNINJACOREAPI void BNAddRelatedPlatform(BNPlatform* platform, BNArchitecture* arch, BNPlatform* related); + BINARYNINJACOREAPI BNPlatform* BNGetAssociatedPlatformByAddress(BNPlatform* platform, uint64_t* addr); //Demangler BINARYNINJACOREAPI bool BNDemangleMS(BNArchitecture* arch, diff --git a/binaryview.cpp b/binaryview.cpp index ff19de18..cc6667c5 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -1156,6 +1156,13 @@ void BinaryView::DefineAutoSymbol(Ref sym) } +void BinaryView::DefineAutoSymbolAndVariableOrFunction(Ref platform, Ref sym, Ref type) +{ + BNDefineAutoSymbolAndVariableOrFunction(m_object, platform ? platform->GetObject() : nullptr, sym->GetObject(), + type ? type->GetObject() : nullptr); +} + + void BinaryView::UndefineAutoSymbol(Ref sym) { BNUndefineAutoSymbol(m_object, sym->GetObject()); diff --git a/platform.cpp b/platform.cpp index 9b4053db..7a6571cc 100644 --- a/platform.cpp +++ b/platform.cpp @@ -244,3 +244,12 @@ void Platform::AddRelatedPlatform(Architecture* arch, Platform* platform) { BNAddRelatedPlatform(m_object, arch->GetObject(), platform->GetObject()); } + + +Ref Platform::GetAssociatedPlatformByAddress(uint64_t& addr) +{ + BNPlatform* platform = BNGetAssociatedPlatformByAddress(m_object, &addr); + if (!platform) + return nullptr; + return new Platform(platform); +} diff --git a/python/__init__.py b/python/__init__.py index 6c7e298c..1751fefa 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -2630,6 +2630,21 @@ class BinaryView(object): """ core.BNDefineAutoSymbol(self.handle, sym.handle) + def define_auto_symbol_and_var_or_function(self, sym, sym_type, platform = None): + """ + ``define_auto_symbol`` adds a symbol to the internal list of automatically discovered Symbol objects. + + :param Symbol sym: the symbol to define + :rtype: None + """ + if platform is None: + platform = self.platform + if platform is not None: + platform = platform.handle + if sym_type is not None: + sym_type = sym_type.handle + core.BNDefineAutoSymbolAndVariableOrFunction(self.handle, platform, sym.handle, sym_type) + def undefine_auto_symbol(self, sym): """ ``undefine_auto_symbol`` removes a symbol from the internal list of automatically discovered Symbol objects. @@ -9730,6 +9745,12 @@ class Platform(object): def add_related_platform(self, arch, platform): core.BNAddRelatedPlatform(self.handle, arch.handle, platform.handle) + def get_associated_platform_by_address(self, addr): + new_addr = ctypes.c_ulonglong() + new_addr.value = addr + result = core.BNGetAssociatedPlatformByAddress(self.handle, new_addr) + return Platform(None, handle = result), new_addr.value + class ScriptingOutputListener(object): def _register(self, handle): self._cb = core.BNScriptingOutputListener() -- cgit v1.3.1