diff options
| author | Rusty Wagner <rusty.wagner@gmail.com> | 2024-01-03 15:56:01 -0700 |
|---|---|---|
| committer | Rusty Wagner <rusty.wagner@gmail.com> | 2024-01-04 12:42:14 -0700 |
| commit | 7e8de24c479672261b8a5c9911cac8800bad180b (patch) | |
| tree | 6992a8a6f59a46c4be93f7a4693b52fc871fbfce | |
| parent | a9cfac7ff14d93ff22092366f99eb2dd3213ea7e (diff) | |
Add GetRelocationsAt API
| -rw-r--r-- | binaryninjaapi.h | 1 | ||||
| -rw-r--r-- | binaryninjacore.h | 2 | ||||
| -rw-r--r-- | binaryview.cpp | 14 | ||||
| -rw-r--r-- | rust/src/binaryview.rs | 9 | ||||
| -rw-r--r-- | rust/src/relocation.rs | 20 |
5 files changed, 45 insertions, 1 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 9013b5ad..10a9b556 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -4363,6 +4363,7 @@ namespace BinaryNinja { std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRanges() const; std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRangesAtAddress(uint64_t addr) const; bool RangeContainsRelocation(uint64_t addr, size_t size) const; + std::vector<Ref<Relocation>> GetRelocationsAt(uint64_t addr) const; /*! Provides a mechanism for receiving callbacks for various analysis events. diff --git a/binaryninjacore.h b/binaryninjacore.h index 0c66d070..4b5ce20f 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -3433,6 +3433,8 @@ extern "C" BINARYNINJACOREAPI BNRange* BNGetRelocationRanges(BNBinaryView* view, size_t* count); BINARYNINJACOREAPI BNRange* BNGetRelocationRangesAtAddress(BNBinaryView* view, uint64_t addr, size_t* count); BINARYNINJACOREAPI bool BNRangeContainsRelocation(BNBinaryView* view, uint64_t addr, size_t size); + BINARYNINJACOREAPI BNRelocation** BNGetRelocationsAt(BNBinaryView* view, uint64_t addr, size_t* count); + BINARYNINJACOREAPI void BNFreeRelocationList(BNRelocation** relocations, size_t count); BINARYNINJACOREAPI void BNRegisterDataNotification(BNBinaryView* view, BNBinaryDataNotification* notify); BINARYNINJACOREAPI void BNUnregisterDataNotification(BNBinaryView* view, BNBinaryDataNotification* notify); diff --git a/binaryview.cpp b/binaryview.cpp index 1588d23a..e0708148 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -1607,6 +1607,20 @@ bool BinaryView::RangeContainsRelocation(uint64_t addr, size_t size) const } +std::vector<Ref<Relocation>> BinaryView::GetRelocationsAt(uint64_t addr) const +{ + size_t count = 0; + BNRelocation** relocations = BNGetRelocationsAt(m_object, addr, &count); + std::vector<Ref<Relocation>> result(count); + for (size_t i = 0; i < count; i++) + { + result.push_back(new Relocation(relocations[i])); + } + BNFreeRelocationList(relocations, count); + return result; +} + + void BinaryView::RegisterNotification(BinaryDataNotification* notify) { BNRegisterDataNotification(m_object, notify->GetCallbacks()); diff --git a/rust/src/binaryview.rs b/rust/src/binaryview.rs index d6916e2c..9bcbbec2 100644 --- a/rust/src/binaryview.rs +++ b/rust/src/binaryview.rs @@ -42,6 +42,7 @@ use crate::linearview::LinearDisassemblyLine; use crate::linearview::LinearViewCursor; use crate::metadata::Metadata; use crate::platform::Platform; +use crate::relocation::Relocation; use crate::section::{Section, SectionBuilder}; use crate::segment::{Segment, SegmentBuilder}; use crate::settings::Settings; @@ -1272,6 +1273,14 @@ pub trait BinaryViewExt: BinaryViewBase { Array::new(handle, count, ()) } } + + fn get_relocations_at(&self, addr: u64) -> Array<Relocation> { + unsafe { + let mut count = 0; + let handle = BNGetRelocationsAt(self.as_ref().handle, addr, &mut count); + Array::new(handle, count, ()) + } + } } impl<T: BinaryViewBase> BinaryViewExt for T {} diff --git a/rust/src/relocation.rs b/rust/src/relocation.rs index cb7e4e3d..7be7861d 100644 --- a/rust/src/relocation.rs +++ b/rust/src/relocation.rs @@ -3,7 +3,7 @@ use crate::{ architecture::{Architecture, CoreArchitecture}, binaryview::BinaryView, llil, - rc::{Ref, RefCountable}, + rc::{CoreArrayProvider, CoreArrayWrapper, CoreOwnedArrayProvider, Ref, RefCountable}, symbol::Symbol, }; use binaryninjacore_sys::*; @@ -210,6 +210,24 @@ impl Relocation { } } +impl CoreArrayProvider for Relocation { + type Raw = *mut BNRelocation; + type Context = (); +} + +unsafe impl CoreOwnedArrayProvider for Relocation { + unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) { + BNFreeRelocationList(raw, count); + } +} + +unsafe impl<'a> CoreArrayWrapper<'a> for Relocation { + type Wrapped = Relocation; + unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped { + Relocation(*raw) + } +} + pub trait RelocationHandler: 'static + Sized + AsRef<CoreRelocationHandler> { type Handle: Borrow<Self>; |
