diff options
| author | Peter LaFosse <peter@vector35.com> | 2018-06-29 16:54:48 -0400 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2018-06-29 16:54:48 -0400 |
| commit | 635c06c68ec72e579573f87ab86996b5952bf853 (patch) | |
| tree | cd6a95bc6066f3bfb534611be7db614b3886e131 | |
| parent | d0c07251a780ca3b638b2cdff0afa00c92aea421 (diff) | |
Add api for querying relocation
| -rw-r--r-- | binaryninjaapi.h | 7 | ||||
| -rw-r--r-- | binaryninjacore.h | 14 | ||||
| -rw-r--r-- | binaryview.cpp | 62 | ||||
| -rw-r--r-- | python/binaryview.py | 50 |
4 files changed, 133 insertions, 0 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index e52a3a16..510b392a 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -1042,6 +1042,10 @@ namespace BinaryNinja uint32_t GetFlags() const; bool IsAutoDefined() const; + std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRanges() const; + std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRangesAtAddress(uint64_t addr) const; + uint64_t GetRelocationsCount() const; + void SetStart(uint64_t newSegmentBase); void SetLength(uint64_t length); void SetDataOffset(uint64_t dataOffset); @@ -1227,6 +1231,9 @@ namespace BinaryNinja void DefineRelocation(Architecture* arch, BNRelocationInfo& info, uint64_t target, uint64_t reloc); void DefineRelocation(Architecture* arch, BNRelocationInfo& info, Ref<Symbol> target, uint64_t reloc); + std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRanges() const; + std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRangesAtAddress(uint64_t addr) const; + void RegisterNotification(BinaryDataNotification* notify); void UnregisterNotification(BinaryDataNotification* notify); diff --git a/binaryninjacore.h b/binaryninjacore.h index 73ccbd4d..f2384f85 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -1800,6 +1800,12 @@ extern "C" AlwaysSkipFunctionAnalysis }; + struct BNRange + { + uint64_t start; + uint64_t end; + }; + BINARYNINJACOREAPI char* BNAllocString(const char* contents); BINARYNINJACOREAPI void BNFreeString(char* str); BINARYNINJACOREAPI char** BNAllocStringList(const char** contents, size_t size); @@ -1996,6 +2002,9 @@ extern "C" uint64_t target, uint64_t reloc); BINARYNINJACOREAPI void BNDefineSymbolRelocation(BNBinaryView* view, BNArchitecture* arch, BNRelocationInfo* info, BNSymbol* target, uint64_t reloc); + BINARYNINJACOREAPI BNRange* BNGetRelocationRanges(BNBinaryView* segment, size_t* count); + BINARYNINJACOREAPI BNRange* BNGetRelocationRangesAtAddress(BNBinaryView* segment, uint64_t addr, size_t* count); + BINARYNINJACOREAPI void BNRegisterDataNotification(BNBinaryView* view, BNBinaryDataNotification* notify); BINARYNINJACOREAPI void BNUnregisterDataNotification(BNBinaryView* view, BNBinaryDataNotification* notify); @@ -3522,6 +3531,11 @@ extern "C" bool autoDefined); BINARYNINJACOREAPI BNSegment* BNNewSegmentReference(BNSegment* seg); BINARYNINJACOREAPI void BNFreeSegment(BNSegment* seg); + + BINARYNINJACOREAPI BNRange* BNSegmentGetRelocationRanges(BNSegment* segment, size_t* count); + BINARYNINJACOREAPI uint64_t BNSegmentGetRelocationsCount(BNSegment* segment); + BINARYNINJACOREAPI BNRange* BNSegmentGetRelocationRangesAtAddress(BNSegment* segment, uint64_t addr, size_t* count); + BINARYNINJACOREAPI void BNFreeRelocationRanges(BNRange* ranges); BINARYNINJACOREAPI uint64_t BNSegmentGetStart(BNSegment* segment); BINARYNINJACOREAPI uint64_t BNSegmentGetLength(BNSegment* segment); BINARYNINJACOREAPI uint64_t BNSegmentGetEnd(BNSegment* segment); diff --git a/binaryview.cpp b/binaryview.cpp index f663d337..7aa2118f 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -273,6 +273,40 @@ Segment::Segment(BNSegment* seg) } +vector<pair<uint64_t, uint64_t>> Segment::GetRelocationRanges() const +{ + size_t count = 0; + BNRange* ranges = BNSegmentGetRelocationRanges(m_object, &count); + vector<pair<uint64_t, uint64_t>> result(count); + for (size_t i = 0; i < count; i++) + { + result.push_back({ranges[i].start, ranges[i].end}); + } + BNFreeRelocationRanges(ranges); + return result; +} + + +vector<pair<uint64_t, uint64_t>> Segment::GetRelocationRangesAtAddress(uint64_t addr) const +{ + size_t count = 0; + BNRange* ranges = BNSegmentGetRelocationRangesAtAddress(m_object, addr, &count); + vector<pair<uint64_t, uint64_t>> result(count); + for (size_t i = 0; i < count; i++) + { + result.push_back({ranges[i].start, ranges[i].end}); + } + BNFreeRelocationRanges(ranges); + return result; +} + + +uint64_t Segment::GetRelocationsCount() const +{ + return BNSegmentGetRelocationsCount(m_object); +} + + uint64_t Segment::GetStart() const { return BNSegmentGetStart(m_object); @@ -909,6 +943,34 @@ void BinaryView::DefineRelocation(Architecture* arch, BNRelocationInfo& info, Re } +vector<pair<uint64_t, uint64_t>> BinaryView::GetRelocationRanges() const +{ + size_t count = 0; + BNRange* ranges = BNGetRelocationRanges(m_object, &count); + vector<pair<uint64_t, uint64_t>> result(count); + for (size_t i = 0; i < count; i++) + { + result.push_back({ranges[i].start, ranges[i].end}); + } + BNFreeRelocationRanges(ranges); + return result; +} + + +vector<pair<uint64_t, uint64_t>> BinaryView::GetRelocationRangesAtAddress(uint64_t addr) const +{ + size_t count = 0; + BNRange* ranges = BNGetRelocationRangesAtAddress(m_object, addr, &count); + vector<pair<uint64_t, uint64_t>> result(count); + for (size_t i = 0; i < count; i++) + { + result.push_back({ranges[i].start, ranges[i].end}); + } + BNFreeRelocationRanges(ranges); + return result; +} + + void BinaryView::RegisterNotification(BinaryDataNotification* notify) { BNRegisterDataNotification(m_object, notify->GetCallbacks()); diff --git a/python/binaryview.py b/python/binaryview.py index 0b090a9a..ba99f9f9 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -466,6 +466,33 @@ class Segment(object): def data_end(self): return core.BNSegmentGetDataEnd(self.handle) + @property + def reloction_count(self): + return core.BNSegmentGetRelocationsCount(self.handle) + + @property + def relocation_ranges(self): + """List of relocation range tuples (read-only)""" + + count = ctypes.c_ulonglong() + ranges = core.BNSegmentGetRelocationRanges(self.handle, count) + result = [] + for i in xrange(0, count.value): + result.append((ranges[i].start, ranges[i].end)) + core.BNFreeRelocationRanges(ranges, count) + return result + + def relocation_ranges_at(self, addr): + """List of relocation range tuples (read-only)""" + + count = ctypes.c_ulonglong() + ranges = core.BNSegmentGetRelocationRangesAtAddress(self.handle, addr, count) + result = [] + for i in xrange(0, count.value): + result.append((ranges[i].start, ranges[i].end)) + core.BNFreeRelocationRanges(ranges, count) + return result + def __len__(self): return core.BNSegmentGetLength(self.handle) @@ -1074,6 +1101,29 @@ class BinaryView(object): def max_function_size_for_analysis(self, size): core.BNSetMaxFunctionSizeForAnalysis(self.handle, size) + @property + def relocation_ranges(self): + """List of relocation range tuples (read-only)""" + + count = ctypes.c_ulonglong() + ranges = core.BNGetRelocationRanges(self.handle, count) + result = [] + for i in xrange(0, count.value): + result.append((ranges[i].start, ranges[i].end)) + core.BNFreeRelocationRanges(ranges, count) + return result + + def relocation_ranges_at(self, addr): + """List of relocation range tuples for a given address""" + + count = ctypes.c_ulonglong() + ranges = core.BNGetRelocationRangesAtAddress(self.handle, addr, count) + result = [] + for i in xrange(0, count.value): + result.append((ranges[i].start, ranges[i].end)) + core.BNFreeRelocationRanges(ranges, count) + return result + def __len__(self): return int(core.BNGetViewLength(self.handle)) |
