From d3140edec185f47235b9e4642bdd56d6c585a341 Mon Sep 17 00:00:00 2001 From: Ryan Snyder Date: Thu, 21 Jan 2021 18:27:48 +0000 Subject: This is a combination of 23 commits, the work of Ryan Snyder: Initial fresh repo Add support for recent calling convention API updates and folds the binaryninjacore-sys crate directly into this one. Add support for auto function analysis suppression Finish moving binaryninjacore-sys back into this crate Update for Symbol/Segment core API changes Update for Symbol API cleanup api: advance submodule reference, support Token changes arch/lifting: support for flags in custom architectures arch/lifting: support default flag write behaviors, handle more ops build: enable headless binary support on MacOS via evil hack bv: add BinaryView wrapper support, remove wrong comment api: update to latest binja dev branch support deps: bump dep versions rust: bump to 2018 edition api: bump to avoid cargo submodule brokenness build: improve binaryninja path detection; enable linux linkhack bv: stub for bv load settings arch: fix flag related crash, minor llil update api: update for recent changes macos: disable linkhack briefly --- rust/src/headless.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 rust/src/headless.rs (limited to 'rust/src/headless.rs') diff --git a/rust/src/headless.rs b/rust/src/headless.rs new file mode 100644 index 00000000..e6b7c51c --- /dev/null +++ b/rust/src/headless.rs @@ -0,0 +1,68 @@ +use std::env; +use std::mem; +use std::os::raw; +use std::ffi::{OsStr, CString, CStr}; +use std::path::PathBuf; + +#[repr(C)] +struct DlInfo { + dli_fname: *const raw::c_char, + dli_fbase: *mut raw::c_void, + dli_sname: *const raw::c_char, + dli_saddr: *mut raw::c_void +} + +#[cfg(not(target_os = "windows"))] +fn binja_path() -> PathBuf { + use std::os::unix::ffi::OsStrExt; + + if let Ok(p) = env::var("BINJA_DIR") { + return PathBuf::from(p); + } + + extern { + fn dladdr(addr: *mut raw::c_void, info: *mut DlInfo) -> raw::c_int; + } + + unsafe { + let mut info: DlInfo = mem::uninitialized(); + + if dladdr(BNSetBundledPluginDirectory as *mut _, &mut info) == 0 { + panic!("Failed to find libbinaryninjacore path!"); + } + + if info.dli_fname.is_null() { + panic!("Failed to find libbinaryninjacore path!"); + } + + let path = CStr::from_ptr(info.dli_fname); + let path = OsStr::from_bytes(path.to_bytes()); + let mut path = PathBuf::from(path); + + path.pop(); + path + } +} + + +#[cfg(target_os = "windows")] +fn binja_path() -> PathBuf { + PathBuf::from(env::var("PROGRAMFILES").unwrap()).join("Vector35\\BinaryNinja\\") +} + +use binaryninjacore_sys::{BNSetBundledPluginDirectory, + BNInitCorePlugins, + BNInitUserPlugins, + BNInitRepoPlugins}; + +pub fn init() { + unsafe { + let path = binja_path().join("plugins").into_os_string(); + let path = CString::new(path.into_string().unwrap()).unwrap(); + + BNSetBundledPluginDirectory(path.as_ptr()); + BNInitCorePlugins(); + BNInitUserPlugins(); + BNInitRepoPlugins(); + } +} -- cgit v1.3.1