From de70f093cc429a125dde201cfe245b616c10ebce Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Mon, 30 Oct 2017 22:03:01 -0400 Subject: Provide a slightly different implementation for @joshwatson's AnalysisCompletionEvent PR and documentation update --- python/binaryview.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'python/binaryview.py') diff --git a/python/binaryview.py b/python/binaryview.py index 9304d412..e6cb5b04 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -103,6 +103,17 @@ class StringReference(object): class AnalysisCompletionEvent(object): + """ + The ``AnalysisCompletionEvent`` object provides an asynchronous mechanism for receiving + callbacks when analysis is complete. + + :Example: + >>> def on_complete(self): + ... print "Analysis Complete", self.view + ... + >>> evt = AnalysisCompletionEvent(bv, on_complete) + >>> + """ def __init__(self, view, callback): self.view = view self.callback = callback @@ -114,7 +125,7 @@ class AnalysisCompletionEvent(object): def _notify(self, ctxt): try: - self.callback() + self.callback(self) except: log.log_error(traceback.format_exc()) -- cgit v1.3.1 From 382b1353649472bc2088598b3b8b60c096c1e201 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Fri, 10 Nov 2017 17:42:28 -0500 Subject: Add 'relocatable' property to BinaryView class --- binaryninjaapi.h | 3 +++ binaryninjacore.h | 2 ++ binaryview.cpp | 19 +++++++++++++++++++ python/binaryview.py | 26 ++++++++++++++++++++++++++ 4 files changed, 50 insertions(+) (limited to 'python/binaryview.py') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index a6276b00..5f3aace7 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -1088,6 +1088,7 @@ namespace BinaryNinja virtual uint64_t PerformGetEntryPoint() const { return 0; } virtual bool PerformIsExecutable() const { return false; } virtual BNEndianness PerformGetDefaultEndianness() const; + virtual bool PerformIsRelocatable() const; virtual size_t PerformGetAddressSize() const; virtual bool PerformSave(FileAccessor* file); @@ -1115,6 +1116,7 @@ namespace BinaryNinja static uint64_t GetEntryPointCallback(void* ctxt); static bool IsExecutableCallback(void* ctxt); static BNEndianness GetDefaultEndiannessCallback(void* ctxt); + static bool IsRelocatableCallback(void* ctxt); static size_t GetAddressSizeCallback(void* ctxt); static bool SaveCallback(void* ctxt, BNFileAccessor* file); @@ -1181,6 +1183,7 @@ namespace BinaryNinja void SetDefaultPlatform(Platform* platform); BNEndianness GetDefaultEndianness() const; + bool IsRelocatable() const; size_t GetAddressSize() const; bool IsExecutable() const; diff --git a/binaryninjacore.h b/binaryninjacore.h index 870c2de3..45e6310c 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -935,6 +935,7 @@ extern "C" uint64_t (*getEntryPoint)(void* ctxt); bool (*isExecutable)(void* ctxt); BNEndianness (*getDefaultEndianness)(void* ctxt); + bool (*isRelocatable)(void* ctxt); size_t (*getAddressSize)(void* ctxt); bool (*save)(void* ctxt, BNFileAccessor* accessor); }; @@ -1777,6 +1778,7 @@ extern "C" BINARYNINJACOREAPI BNPlatform* BNGetDefaultPlatform(BNBinaryView* view); BINARYNINJACOREAPI void BNSetDefaultPlatform(BNBinaryView* view, BNPlatform* platform); BINARYNINJACOREAPI BNEndianness BNGetDefaultEndianness(BNBinaryView* view); + BINARYNINJACOREAPI bool BNIsRelocatable(BNBinaryView* view); BINARYNINJACOREAPI size_t BNGetViewAddressSize(BNBinaryView* view); BINARYNINJACOREAPI bool BNIsViewModified(BNBinaryView* view); diff --git a/binaryview.cpp b/binaryview.cpp index 213be79a..0a082458 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -288,6 +288,7 @@ BinaryView::BinaryView(const std::string& typeName, FileMetadata* file, BinaryVi view.getEntryPoint = GetEntryPointCallback; view.isExecutable = IsExecutableCallback; view.getDefaultEndianness = GetDefaultEndiannessCallback; + view.isRelocatable = IsRelocatableCallback; view.getAddressSize = GetAddressSizeCallback; view.save = SaveCallback; @@ -431,6 +432,13 @@ BNEndianness BinaryView::GetDefaultEndiannessCallback(void* ctxt) } +bool BinaryView::IsRelocatableCallback(void* ctxt) +{ + BinaryView* view = (BinaryView*)ctxt; + return view->PerformIsRelocatable(); +} + + size_t BinaryView::GetAddressSizeCallback(void* ctxt) { BinaryView* view = (BinaryView*)ctxt; @@ -494,6 +502,11 @@ BNEndianness BinaryView::PerformGetDefaultEndianness() const } +bool BinaryView::PerformIsRelocatable() const +{ + return false; +} + size_t BinaryView::PerformGetAddressSize() const { Ref arch = GetDefaultArchitecture(); @@ -836,6 +849,12 @@ BNEndianness BinaryView::GetDefaultEndianness() const } +bool BinaryView::IsRelocatable() const +{ + return BNIsRelocatable(m_object); +} + + size_t BinaryView::GetAddressSize() const { return BNGetViewAddressSize(m_object); diff --git a/python/binaryview.py b/python/binaryview.py index e6cb5b04..9f149ba8 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -583,6 +583,7 @@ class BinaryView(object): self._cb.getEntryPoint = self._cb.getEntryPoint.__class__(self._get_entry_point) self._cb.isExecutable = self._cb.isExecutable.__class__(self._is_executable) self._cb.getDefaultEndianness = self._cb.getDefaultEndianness.__class__(self._get_default_endianness) + self._cb.isRelocatable = self._cb.isRelocatable.__class__(self._is_relocatable) self._cb.getAddressSize = self._cb.getAddressSize.__class__(self._get_address_size) self._cb.save = self._cb.save.__class__(self._save) self.file = file_metadata @@ -838,6 +839,11 @@ class BinaryView(object): """Endianness of the binary (read-only)""" return Endianness(core.BNGetDefaultEndianness(self.handle)) + @property + def relocatable(self): + """Boolean - is the binary relocatable (read-only)""" + return core.BNIsRelocatable(self.handle) + @property def address_size(self): """Address size of the binary (read-only)""" @@ -1202,6 +1208,13 @@ class BinaryView(object): log.log_error(traceback.format_exc()) return Endianness.LittleEndian + def _is_relocatable(self, ctxt): + try: + return self.perform_is_relocatable() + except: + log.log_error(traceback.format_exc()) + return False + def _get_address_size(self, ctxt): try: return self.perform_get_address_size() @@ -1494,6 +1507,19 @@ class BinaryView(object): """ return Endianness.LittleEndian + def perform_is_relocatable(self): + """ + ``perform_is_relocatable`` implements a check which returns true if the BinaryView is relocatable. Defaults to + True. + + .. note:: This method **may** be implemented for custom BinaryViews that are relocatable. + .. warning:: This method **must not** be called directly. + + :return: True if the BinaryView is relocatable, False otherwise + :rtype: boolean + """ + return True + def create_database(self, filename, progress_func=None): """ ``create_database`` writes the current database (.bndb) file out to the specified file. -- cgit v1.3.1 From b207c76729df133c7b6fc4bc9f1abb6948ae6438 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Thu, 14 Dec 2017 18:46:10 -0500 Subject: Add autoDefined attribute to Sections and Segments --- binaryninjaapi.h | 2 ++ binaryninjacore.h | 2 ++ binaryview.cpp | 2 +- python/binaryview.py | 17 ++++++++++------- 4 files changed, 15 insertions(+), 8 deletions(-) (limited to 'python/binaryview.py') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index baeb5de6..72f74c5a 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -1027,6 +1027,7 @@ namespace BinaryNinja uint64_t start, length; uint64_t dataOffset, dataLength; uint32_t flags; + bool autoDefined; }; struct Section @@ -1037,6 +1038,7 @@ namespace BinaryNinja uint64_t infoData; uint64_t align, entrySize; BNSectionSemantics semantics; + bool autoDefined; }; struct QualifiedNameAndType; diff --git a/binaryninjacore.h b/binaryninjacore.h index 1bb0c70a..245e6353 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -1531,6 +1531,7 @@ extern "C" uint64_t start, length; uint64_t dataOffset, dataLength; uint32_t flags; + bool autoDefined; }; enum BNSectionSemantics @@ -1551,6 +1552,7 @@ extern "C" uint64_t infoData; uint64_t align, entrySize; BNSectionSemantics semantics; + bool autoDefined; }; struct BNAddressRange diff --git a/binaryview.cpp b/binaryview.cpp index 0a082458..50011a0f 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -1724,6 +1724,7 @@ vector BinaryView::GetSegments() segment.dataOffset = segments[i].dataOffset; segment.dataLength = segments[i].dataLength; segment.flags = segments[i].flags; + segment.autoDefined = segments[i].autoDefined; result.push_back(segment); } @@ -1731,7 +1732,6 @@ vector BinaryView::GetSegments() return result; } - bool BinaryView::GetSegmentAt(uint64_t addr, Segment& result) { BNSegment segment; diff --git a/python/binaryview.py b/python/binaryview.py index 9f149ba8..6eb59db1 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -416,12 +416,13 @@ class BinaryViewType(object): class Segment(object): - def __init__(self, start, length, data_offset, data_length, flags): + def __init__(self, start, length, data_offset, data_length, flags, auto_defined): self.start = start self.length = length self.data_offset = data_offset self.data_length = data_length self.flags = flags + self.auto_defined = auto_defined @property def executable(self): @@ -450,7 +451,7 @@ class Segment(object): class Section(object): - def __init__(self, name, section_type, start, length, linked_section, info_section, info_data, align, entry_size, semantics): + def __init__(self, name, section_type, start, length, linked_section, info_section, info_data, align, entry_size, semantics, auto_defined): self.name = name self.type = section_type self.start = start @@ -461,6 +462,7 @@ class Section(object): self.align = align self.entry_size = entry_size self.semantics = SectionSemantics(semantics) + self.auto_defined = auto_defined @property def end(self): @@ -965,7 +967,7 @@ class BinaryView(object): result = [] for i in xrange(0, count.value): result.append(Segment(segment_list[i].start, segment_list[i].length, - segment_list[i].dataOffset, segment_list[i].dataLength, segment_list[i].flags)) + segment_list[i].dataOffset, segment_list[i].dataLength, segment_list[i].flags, segment_list[i].autoDefined)) core.BNFreeSegmentList(segment_list) return result @@ -979,7 +981,7 @@ class BinaryView(object): result[section_list[i].name] = Section(section_list[i].name, section_list[i].type, section_list[i].start, section_list[i].length, section_list[i].linkedSection, section_list[i].infoSection, section_list[i].infoData, section_list[i].align, section_list[i].entrySize, - section_list[i].semantics) + section_list[i].semantics, section_list[i].autoDefined) core.BNFreeSectionList(section_list, count.value) return result @@ -3353,7 +3355,7 @@ class BinaryView(object): if not core.BNGetSegmentAt(self.handle, addr, segment): return None result = Segment(segment.start, segment.length, segment.dataOffset, segment.dataLength, - segment.flags) + segment.flags, segment.autoDefined) return result def get_address_for_data_offset(self, offset): @@ -3386,7 +3388,7 @@ class BinaryView(object): result.append(Section(section_list[i].name, section_list[i].type, section_list[i].start, section_list[i].length, section_list[i].linkedSection, section_list[i].infoSection, section_list[i].infoData, section_list[i].align, section_list[i].entrySize, - section_list[i].semantics)) + section_list[i].semantics, section_list[i].autoDefined)) core.BNFreeSectionList(section_list, count.value) return result @@ -3395,7 +3397,8 @@ class BinaryView(object): if not core.BNGetSectionByName(self.handle, name, section): return None result = Section(section.name, section.type, section.start, section.length, section.linkedSection, - section.infoSection, section.infoData, section.align, section.entrySize, section.semantics) + section.infoSection, section.infoData, section.align, section.entrySize, section.semantics, + section_list.autoDefined) core.BNFreeSection(section) return result -- cgit v1.3.1