diff options
| author | Ryan Snyder <ryan@vector35.com> | 2021-01-21 18:27:48 +0000 |
|---|---|---|
| committer | KyleMiles <krm504@nyu.edu> | 2021-01-21 19:06:55 +0000 |
| commit | d3140edec185f47235b9e4642bdd56d6c585a341 (patch) | |
| tree | a61859c29e4e3539daea2b761bb1439d942beaf4 /rust/binaryninjacore-sys | |
| parent | c0ddbf0c76d3f1bb7a2b2024f749afc8b9482575 (diff) | |
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
Diffstat (limited to 'rust/binaryninjacore-sys')
| -rw-r--r-- | rust/binaryninjacore-sys/Cargo.toml | 8 | ||||
| -rw-r--r-- | rust/binaryninjacore-sys/build.rs | 130 | ||||
| -rw-r--r-- | rust/binaryninjacore-sys/linkhack/linkhack.c | 20 | ||||
| -rw-r--r-- | rust/binaryninjacore-sys/src/lib.rs | 6 | ||||
| -rw-r--r-- | rust/binaryninjacore-sys/wrapper.hpp | 1 |
5 files changed, 165 insertions, 0 deletions
diff --git a/rust/binaryninjacore-sys/Cargo.toml b/rust/binaryninjacore-sys/Cargo.toml new file mode 100644 index 00000000..5b4cc063 --- /dev/null +++ b/rust/binaryninjacore-sys/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "binaryninjacore-sys" +version = "0.1.0" +authors = ["Ryan Snyder <ryan.snyder.or@gmail.com>"] +build = "build.rs" + +[build-dependencies] +bindgen = "0.49" diff --git a/rust/binaryninjacore-sys/build.rs b/rust/binaryninjacore-sys/build.rs new file mode 100644 index 00000000..b151fc3b --- /dev/null +++ b/rust/binaryninjacore-sys/build.rs @@ -0,0 +1,130 @@ +extern crate bindgen; + +use std::io::prelude::*; +use std::io::BufReader; +use std::path::PathBuf; +use std::fs::File; +use std::env; + +#[cfg(target_os = "macos")] +static LASTRUN_PATH: (&str, &str) = ("HOME", "Library/Application Support/Binary Ninja/lastrun"); + +#[cfg(target_os = "linux")] +static LASTRUN_PATH: (&str, &str) = ("HOME", ".binaryninja/lastrun"); + +#[cfg(windows)] +static LASTRUN_PATH: (&str, &str) = ("APPDATA", "Binary Ninja\\lastrun"); + +fn link_path() -> PathBuf { + let home = PathBuf::from(env::var(LASTRUN_PATH.0).unwrap()); + let lastrun = PathBuf::from(&home) + .join(LASTRUN_PATH.1); + + File::open(lastrun).and_then(|f| { + let mut binja_path = String::new(); + let mut reader = BufReader::new(f); + + reader.read_line(&mut binja_path)?; + Ok(PathBuf::from(binja_path.trim())) + }).unwrap_or_else(|_| { + #[cfg(target_os = "macos")] + return PathBuf::from("/Applications/Binary Ninja.app/Contents/MacOS"); + + #[cfg(target_os = "linux")] + return home.join("binaryninja"); + + #[cfg(windows)] + return PathBuf::from(env::var("PROGRAMFILES").unwrap()).join("Vector35\\BinaryNinja\\"); + }) +} + +fn main() { + // Allow the library search path to be overridden for internal dev builds, but + // otherwise search the usual install paths + let out_dir = env::var("OUT_DIR").unwrap(); + let link_path = env::var("BINARYNINJADIR") + .map(PathBuf::from) + .unwrap_or_else(|_| link_path()); + + #[cfg(unix)] + { + use std::process::Command; + + // Spectacularly evil linking hack due to Cargo shortcomings. In a nutshell, + // due to the inability for our .rlib crate to contribute linker arguments + // (or even just an rpath!) to our consumers, any resulting binaries will + // fail to run unless loaded into a process that already has the core loaded. + // + // For plugins this restriction is fine as they'll only be loaded by the core; + // headless binaries on the other hand (consider `cargo test`, too!) will fail + // because libbinaryninjacore is not in the library path on pretty much any + // system anywhere. If only we could trick the linker into linking with an + // absolute path... + // + // LOOK UPON MY WORKS, YE MIGHTY, AND DESPAIR + // + // We build a simple do-nothing library called liblinkhack and add it as a + // native dependency. Crucially, we ensure that LC_ID_DYLIB (macos) or DT_SONAME + // (linux) is set to the absolute path of libbinaryninjacore. This has the effect + // that any binary attempting to link with liblinkhack will have its dependency + // on liblinkhack recorded as the path to the binaryninja core. Later on, the + // actual core will get linked and that dependency will be recorded in the normal + // way. + // + // tl;dr we're linking against a fake, shim library that actually results in + // the binaryninja core getting linked twice, once with an absolute path and once + // correctly. yes, this is as horrifying as you think it is. no, there is no other + // way to make `cargo test` work. + // + // I'm not happy about it either. +/* + #[cfg(target_os = "macos")] + Command::new("clang") + .args(&["-Wl,-dylib", "-o"]) + .arg(&format!("{}/liblinkhack.dylib", out_dir)) + .arg(&format!("-Wl,-install_name,{}/libbinaryninjacore.dylib", &link_path.to_str().unwrap())) + .arg(&format!("{}/linkhack/linkhack.c", env::var("CARGO_MANIFEST_DIR").unwrap())) + .status().unwrap(); + */ + + #[cfg(target_os = "linux")] + { + Command::new("gcc") + .args(&["-shared", "-o"]) + .arg(&format!("{}/liblinkhack.so", out_dir)) + .arg(&format!("-Wl,-soname,{}/libbinaryninjacore.so.1", &link_path.to_str().unwrap())) + .arg(&format!("{}/linkhack/linkhack.c", env::var("CARGO_MANIFEST_DIR").unwrap())) + .status().unwrap(); + + // Linux builds of binaryninja ship with libbinaryninjacore.so.1 in the + // application folder and no symlink. The linker will attempt to link with + // libbinaryninjacore.so. Since this is likely going to fail, we detect this + // ahead of time and create an appropriately named symlink inside of OUT_DIR + // and add it to the library search path. + let symlink_target = PathBuf::from(&out_dir).join("libbinaryninjacore.so"); + if !link_path.join("libbinaryninjacore.so").exists() && !symlink_target.exists() { + use std::os::unix::fs; + fs::symlink(link_path.join("libbinaryninjacore.so.1"), PathBuf::from(&out_dir).join("libbinaryninjacore.so")) + .expect("failed to create required symlink"); + } + } + + //println!("cargo:rustc-link-lib=linkhack"); + //println!("cargo:rustc-link-search={}", out_dir); + } + + println!("cargo:rustc-link-lib=binaryninjacore"); + println!("cargo:rustc-link-search={}", link_path.to_str().unwrap()); + + let bindings = bindgen::builder().header("wrapper.hpp") + .generate_comments(false) + .whitelist_function("BN.*") + .rustified_enum("BN.*") + .rustfmt_bindings(false) // required; things break otherwise + .generate() + .expect("Unable to generate bindings"); + + bindings + .write_to_file(PathBuf::from(out_dir).join("bindings.rs")) + .expect("Couldn't write bindings!"); +} diff --git a/rust/binaryninjacore-sys/linkhack/linkhack.c b/rust/binaryninjacore-sys/linkhack/linkhack.c new file mode 100644 index 00000000..6d4b3f37 --- /dev/null +++ b/rust/binaryninjacore-sys/linkhack/linkhack.c @@ -0,0 +1,20 @@ +// Have some overlap with likely-called functions so ld on +// linux doesn't skip linking liblinkhack +void BNLog() {} +void BNLogDebug() {} +void BNLogInfo() {} +void BNLogWarn() {} +void BNLogError() {} +void BNLogAlert() {} +void BNShutdown() {} +void BNNewViewReference() {} +void BNFreeBinaryView() {} +void BNInitCorePlugins() {} +void BNAllocString() {} +void BNFreeString() {} +void BNRegisterBinaryViewType() {} +void BNGetArchitectureList() {} +void BNNewFunctionReference() {} +void BNFreeFunction() {} +void BNGetFunctionBasicBlockList() {} +void BNRegisterArchitecture() {} diff --git a/rust/binaryninjacore-sys/src/lib.rs b/rust/binaryninjacore-sys/src/lib.rs new file mode 100644 index 00000000..04d76a43 --- /dev/null +++ b/rust/binaryninjacore-sys/src/lib.rs @@ -0,0 +1,6 @@ +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(unused)] + +include!(concat!(env!("OUT_DIR"), "/bindings.rs")); diff --git a/rust/binaryninjacore-sys/wrapper.hpp b/rust/binaryninjacore-sys/wrapper.hpp new file mode 100644 index 00000000..263de01c --- /dev/null +++ b/rust/binaryninjacore-sys/wrapper.hpp @@ -0,0 +1 @@ +#include "binaryninja-api/binaryninjacore.h" |
