summaryrefslogtreecommitdiff
path: root/rust/examples
diff options
context:
space:
mode:
authorKyleMiles <krm504@nyu.edu>2021-06-28 22:13:43 -0400
committerKyleMiles <krm504@nyu.edu>2021-06-29 00:26:33 -0400
commit2c344cf7a77031021e346fd8649de2b7715b5c45 (patch)
treefc9b48db5748231aed4e05128d38ab649717a102 /rust/examples
parent58e515b3bfa3a96e4934b8a625bb6e51399057ec (diff)
Resolves #2521; Rust API Linking Errors
Diffstat (limited to 'rust/examples')
-rw-r--r--rust/examples/basic_script/CMakeLists.txt94
-rw-r--r--rust/examples/basic_script/build.rs67
-rw-r--r--rust/examples/dwarfdump/build.rs67
-rw-r--r--rust/examples/flowgraph/build.rs67
-rw-r--r--rust/examples/template/CMakeLists.txt89
-rw-r--r--rust/examples/template/Cargo.toml16
-rw-r--r--rust/examples/template/README.md17
-rw-r--r--rust/examples/template/build.rs67
-rw-r--r--rust/examples/template/src/main.rs46
9 files changed, 530 insertions, 0 deletions
diff --git a/rust/examples/basic_script/CMakeLists.txt b/rust/examples/basic_script/CMakeLists.txt
new file mode 100644
index 00000000..51adb923
--- /dev/null
+++ b/rust/examples/basic_script/CMakeLists.txt
@@ -0,0 +1,94 @@
+cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
+
+project(test_headless)
+
+file(GLOB PLUGIN_SOURCES
+ ${PROJECT_SOURCE_DIR}/Cargo.toml
+ ${PROJECT_SOURCE_DIR}/src/*.rs)
+
+file(GLOB API_SOURCES
+ ${PROJECT_SOURCE_DIR}/../../../binaryninjacore.h
+ ${PROJECT_SOURCE_DIR}/../../binaryninjacore-sys/build.rs
+ ${PROJECT_SOURCE_DIR}/../../binaryninjacore-sys/Cargo.toml
+ ${PROJECT_SOURCE_DIR}/../../binaryninjacore-sys/src/*
+ ${PROJECT_SOURCE_DIR}/../../Cargo.toml
+ ${PROJECT_SOURCE_DIR}/../../src/*.rs)
+
+if(CMAKE_BUILD_TYPE MATCHES Debug)
+ set(TARGET_DIR ${PROJECT_BINARY_DIR}/target/debug)
+ set(CARGO_OPTS --target-dir=${PROJECT_BINARY_DIR}/target)
+else()
+ set(TARGET_DIR ${PROJECT_BINARY_DIR}/target/release)
+ set(CARGO_OPTS --target-dir=${PROJECT_BINARY_DIR}/target --release)
+endif()
+
+set(OUTPUT_FILE basic_script${CMAKE_EXECUTABLE_SUFFIX})
+set(OUTPUT_PATH ${CMAKE_BINARY_DIR}/out/bin/${OUTPUT_FILE})
+
+add_custom_target(test_headless ALL DEPENDS ${OUTPUT_PATH})
+add_dependencies(test_headless binaryninjaapi)
+
+find_program(RUSTUP_PATH rustup REQUIRED HINTS ~/.cargo/bin)
+set(INSTALL_UPDATE_NIGHTLY ${RUSTUP_PATH} install nightly)
+
+if(APPLE)
+ if(UNIVERSAL)
+ if(CMAKE_BUILD_TYPE MATCHES Debug)
+ set(AARCH64_LIB_PATH ${PROJECT_BINARY_DIR}/target/aarch64-apple-darwin/debug/${OUTPUT_FILE})
+ set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/debug/${OUTPUT_FILE})
+ else()
+ set(AARCH64_LIB_PATH ${PROJECT_BINARY_DIR}/target/aarch64-apple-darwin/release/${OUTPUT_FILE})
+ set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/release/${OUTPUT_FILE})
+ endif()
+
+ add_custom_command(
+ OUTPUT ${OUTPUT_PATH}
+ COMMAND ${INSTALL_UPDATE_NIGHTLY}
+ COMMAND ${CMAKE_COMMAND} -E env
+ MACOSX_DEPLOYMENT_TARGET=10.14 LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR}
+ ${RUSTUP_PATH} run nightly cargo build --target=aarch64-apple-darwin ${CARGO_OPTS}
+ COMMAND ${CMAKE_COMMAND} -E env
+ MACOSX_DEPLOYMENT_TARGET=10.14 LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR}
+ ${RUSTUP_PATH} run nightly cargo build --target=x86_64-apple-darwin ${CARGO_OPTS}
+ COMMAND cp ${AARCH64_LIB_PATH} ${OUTPUT_PATH}-aarch
+ COMMAND cp ${X86_64_LIB_PATH} ${OUTPUT_PATH}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
+ else()
+ if(CMAKE_BUILD_TYPE MATCHES Debug)
+ set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/debug/${OUTPUT_FILE})
+ else()
+ set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/release/${OUTPUT_FILE})
+ endif()
+
+ add_custom_command(
+ OUTPUT ${OUTPUT_PATH}
+ COMMAND ${INSTALL_UPDATE_NIGHTLY}
+ COMMAND ${CMAKE_COMMAND} -E env
+ MACOSX_DEPLOYMENT_TARGET=10.14 LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR}
+ ${RUSTUP_PATH} run nightly cargo build --target=x86_64-apple-darwin ${CARGO_OPTS}
+ COMMAND cp ${X86_64_LIB_PATH} ${OUTPUT_PATH}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
+ endif()
+elseif(WIN32)
+ add_custom_command(
+ OUTPUT ${OUTPUT_PATH}
+ COMMAND ${INSTALL_UPDATE_NIGHTLY}
+ COMMAND ${CMAKE_COMMAND} -E env
+ LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR}
+ ${RUSTUP_PATH} run nightly cargo build ${CARGO_OPTS}
+ COMMAND cp ${TARGET_DIR}/${OUTPUT_FILE} ${OUTPUT_PATH}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
+else()
+ add_custom_command(
+ OUTPUT ${OUTPUT_PATH}
+ COMMAND ${INSTALL_UPDATE_NIGHTLY}
+ COMMAND ${CMAKE_COMMAND} -E env
+ LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR}
+ ${RUSTUP_PATH} run nightly cargo build ${CARGO_OPTS}
+ COMMAND cp ${TARGET_DIR}/${OUTPUT_FILE} ${OUTPUT_PATH}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
+endif()
diff --git a/rust/examples/basic_script/build.rs b/rust/examples/basic_script/build.rs
new file mode 100644
index 00000000..7ad93884
--- /dev/null
+++ b/rust/examples/basic_script/build.rs
@@ -0,0 +1,67 @@
+use std::env;
+use std::fs::File;
+use std::io::BufReader;
+use std::path::PathBuf;
+
+#[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");
+
+// Check last run location for path to BinaryNinja; Otherwise check the default install locations
+fn link_path() -> PathBuf {
+ use std::io::prelude::*;
+
+ 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() {
+ // Use BINARYNINJADIR first for custom BN builds/configurations (BN devs/build server), fallback on defaults
+ let install_path = env::var("BINARYNINJADIR")
+ .map(PathBuf::from)
+ .unwrap_or_else(|_| link_path());
+
+ #[cfg(target_os = "linux")]
+ println!(
+ "cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
+ install_path.to_str().unwrap(),
+ install_path.to_str().unwrap(),
+ );
+
+ #[cfg(target_os = "macos")]
+ println!(
+ "cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
+ install_path.to_str().unwrap(),
+ install_path.to_str().unwrap(),
+ );
+
+ #[cfg(target_os = "windows")]
+ {
+ println!("cargo:rustc-link-lib=binaryninjacore");
+ println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
+ }
+}
diff --git a/rust/examples/dwarfdump/build.rs b/rust/examples/dwarfdump/build.rs
new file mode 100644
index 00000000..7ad93884
--- /dev/null
+++ b/rust/examples/dwarfdump/build.rs
@@ -0,0 +1,67 @@
+use std::env;
+use std::fs::File;
+use std::io::BufReader;
+use std::path::PathBuf;
+
+#[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");
+
+// Check last run location for path to BinaryNinja; Otherwise check the default install locations
+fn link_path() -> PathBuf {
+ use std::io::prelude::*;
+
+ 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() {
+ // Use BINARYNINJADIR first for custom BN builds/configurations (BN devs/build server), fallback on defaults
+ let install_path = env::var("BINARYNINJADIR")
+ .map(PathBuf::from)
+ .unwrap_or_else(|_| link_path());
+
+ #[cfg(target_os = "linux")]
+ println!(
+ "cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
+ install_path.to_str().unwrap(),
+ install_path.to_str().unwrap(),
+ );
+
+ #[cfg(target_os = "macos")]
+ println!(
+ "cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
+ install_path.to_str().unwrap(),
+ install_path.to_str().unwrap(),
+ );
+
+ #[cfg(target_os = "windows")]
+ {
+ println!("cargo:rustc-link-lib=binaryninjacore");
+ println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
+ }
+}
diff --git a/rust/examples/flowgraph/build.rs b/rust/examples/flowgraph/build.rs
new file mode 100644
index 00000000..7ad93884
--- /dev/null
+++ b/rust/examples/flowgraph/build.rs
@@ -0,0 +1,67 @@
+use std::env;
+use std::fs::File;
+use std::io::BufReader;
+use std::path::PathBuf;
+
+#[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");
+
+// Check last run location for path to BinaryNinja; Otherwise check the default install locations
+fn link_path() -> PathBuf {
+ use std::io::prelude::*;
+
+ 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() {
+ // Use BINARYNINJADIR first for custom BN builds/configurations (BN devs/build server), fallback on defaults
+ let install_path = env::var("BINARYNINJADIR")
+ .map(PathBuf::from)
+ .unwrap_or_else(|_| link_path());
+
+ #[cfg(target_os = "linux")]
+ println!(
+ "cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
+ install_path.to_str().unwrap(),
+ install_path.to_str().unwrap(),
+ );
+
+ #[cfg(target_os = "macos")]
+ println!(
+ "cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
+ install_path.to_str().unwrap(),
+ install_path.to_str().unwrap(),
+ );
+
+ #[cfg(target_os = "windows")]
+ {
+ println!("cargo:rustc-link-lib=binaryninjacore");
+ println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
+ }
+}
diff --git a/rust/examples/template/CMakeLists.txt b/rust/examples/template/CMakeLists.txt
new file mode 100644
index 00000000..a06a5af6
--- /dev/null
+++ b/rust/examples/template/CMakeLists.txt
@@ -0,0 +1,89 @@
+cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
+
+project(test_headless)
+
+file(GLOB PLUGIN_SOURCES
+ ${PROJECT_SOURCE_DIR}/Cargo.toml
+ ${PROJECT_SOURCE_DIR}/src/*.rs)
+
+file(GLOB API_SOURCES
+ ${PROJECT_SOURCE_DIR}/../../../binaryninjacore.h
+ ${PROJECT_SOURCE_DIR}/../../binaryninjacore-sys/build.rs
+ ${PROJECT_SOURCE_DIR}/../../binaryninjacore-sys/Cargo.toml
+ ${PROJECT_SOURCE_DIR}/../../binaryninjacore-sys/src/*
+ ${PROJECT_SOURCE_DIR}/../../Cargo.toml
+ ${PROJECT_SOURCE_DIR}/../../src/*.rs)
+
+if(CMAKE_BUILD_TYPE MATCHES Debug)
+ set(TARGET_DIR ${PROJECT_BINARY_DIR}/target/debug)
+ set(CARGO_OPTS --target-dir=${PROJECT_BINARY_DIR}/target)
+else()
+ set(TARGET_DIR ${PROJECT_BINARY_DIR}/target/release)
+ set(CARGO_OPTS --target-dir=${PROJECT_BINARY_DIR}/target --release)
+endif()
+set(PLUGIN_PATH ${TARGET_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}test_headless${CMAKE_SHARED_LIBRARY_SUFFIX})
+
+add_custom_target(test_headless ALL DEPENDS ${PLUGIN_PATH})
+add_dependencies(test_headless binaryninjaapi)
+
+find_program(RUSTUP_PATH rustup REQUIRED HINTS ~/.cargo/bin)
+set(INSTALL_UPDATE_NIGHTLY ${RUSTUP_PATH} install nightly)
+
+if(APPLE)
+ if(UNIVERSAL)
+ if(CMAKE_BUILD_TYPE MATCHES Debug)
+ set(AARCH64_LIB_PATH ${PROJECT_BINARY_DIR}/target/aarch64-apple-darwin/debug/${CMAKE_STATIC_LIBRARY_PREFIX}test_headless${CMAKE_SHARED_LIBRARY_SUFFIX})
+ set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/debug/${CMAKE_STATIC_LIBRARY_PREFIX}test_headless${CMAKE_SHARED_LIBRARY_SUFFIX})
+ else()
+ set(AARCH64_LIB_PATH ${PROJECT_BINARY_DIR}/target/aarch64-apple-darwin/release/${CMAKE_STATIC_LIBRARY_PREFIX}test_headless${CMAKE_SHARED_LIBRARY_SUFFIX})
+ set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/release/${CMAKE_STATIC_LIBRARY_PREFIX}test_headless${CMAKE_SHARED_LIBRARY_SUFFIX})
+ endif()
+
+ add_custom_command(
+ OUTPUT ${PLUGIN_PATH}
+ COMMAND ${INSTALL_UPDATE_NIGHTLY}
+ COMMAND ${CMAKE_COMMAND} -E env
+ MACOSX_DEPLOYMENT_TARGET=10.14 LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR}
+ ${RUSTUP_PATH} run nightly cargo build --target=aarch64-apple-darwin ${CARGO_OPTS}
+ COMMAND ${CMAKE_COMMAND} -E env
+ MACOSX_DEPLOYMENT_TARGET=10.14 LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR}
+ ${RUSTUP_PATH} run nightly cargo build --target=x86_64-apple-darwin ${CARGO_OPTS}
+ COMMAND mkdir -p ${TARGET_DIR}
+ COMMAND lipo -create ${AARCH64_LIB_PATH} ${X86_64_LIB_PATH} -output ${PLUGIN_PATH}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
+ else()
+ if(CMAKE_BUILD_TYPE MATCHES Debug)
+ set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/debug/${CMAKE_STATIC_LIBRARY_PREFIX}test_headless${CMAKE_SHARED_LIBRARY_SUFFIX})
+ else()
+ set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/release/${CMAKE_STATIC_LIBRARY_PREFIX}test_headless${CMAKE_SHARED_LIBRARY_SUFFIX})
+ endif()
+
+ add_custom_command(
+ OUTPUT ${PLUGIN_PATH}
+ COMMAND ${INSTALL_UPDATE_NIGHTLY}
+ COMMAND ${CMAKE_COMMAND} -E env
+ MACOSX_DEPLOYMENT_TARGET=10.14 LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR}
+ ${RUSTUP_PATH} run nightly cargo build --target=x86_64-apple-darwin ${CARGO_OPTS}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
+ endif()
+elseif(WIN32)
+ add_custom_command(
+ OUTPUT ${PLUGIN_PATH}
+ COMMAND ${INSTALL_UPDATE_NIGHTLY}
+ COMMAND ${CMAKE_COMMAND} -E env
+ LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR}
+ ${RUSTUP_PATH} run nightly cargo build ${CARGO_OPTS}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
+else()
+ add_custom_command(
+ OUTPUT ${PLUGIN_PATH}
+ COMMAND ${INSTALL_UPDATE_NIGHTLY}
+ COMMAND ${CMAKE_COMMAND} -E env
+ LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR}
+ ${RUSTUP_PATH} run nightly cargo build ${CARGO_OPTS}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
+endif()
diff --git a/rust/examples/template/Cargo.toml b/rust/examples/template/Cargo.toml
new file mode 100644
index 00000000..6a691e3d
--- /dev/null
+++ b/rust/examples/template/Cargo.toml
@@ -0,0 +1,16 @@
+[package]
+name = "template"
+version = "0.1.0"
+edition = "2018"
+
+# Uncomment this if you're writing a plugin (plugins are shared objects loaded by the core):
+# [lib]
+# crate-type = ["cdylib"]
+
+# You can point at the BinaryNinja dependency in one of two ways, via path:
+# [dependencies]
+# binaryninja = {path="../../"}
+
+# Or directly at the git repo:
+[dependencies]
+binaryninja = {git = "https://github.com/Vector35/binaryninja-api.git", branch = "dev"}
diff --git a/rust/examples/template/README.md b/rust/examples/template/README.md
new file mode 100644
index 00000000..f10f89c8
--- /dev/null
+++ b/rust/examples/template/README.md
@@ -0,0 +1,17 @@
+[The only official method of providing linker arguments to a crate is through that crate's `build.rs`](https://github.com/rust-lang/cargo/issues/9554), thus this template.
+
+Please see `Cargo.toml` for further configuration options.
+
+### Plugins
+
+Enable
+```
+[lib]
+crate-type = ["cdylib"]
+```
+in Cargo.toml`
+
+### Standalone executables
+
+All standalone executables should call both `binaryninja::headless::init()` and `binaryninja::headless::shutdown()`.
+
diff --git a/rust/examples/template/build.rs b/rust/examples/template/build.rs
new file mode 100644
index 00000000..7ad93884
--- /dev/null
+++ b/rust/examples/template/build.rs
@@ -0,0 +1,67 @@
+use std::env;
+use std::fs::File;
+use std::io::BufReader;
+use std::path::PathBuf;
+
+#[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");
+
+// Check last run location for path to BinaryNinja; Otherwise check the default install locations
+fn link_path() -> PathBuf {
+ use std::io::prelude::*;
+
+ 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() {
+ // Use BINARYNINJADIR first for custom BN builds/configurations (BN devs/build server), fallback on defaults
+ let install_path = env::var("BINARYNINJADIR")
+ .map(PathBuf::from)
+ .unwrap_or_else(|_| link_path());
+
+ #[cfg(target_os = "linux")]
+ println!(
+ "cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
+ install_path.to_str().unwrap(),
+ install_path.to_str().unwrap(),
+ );
+
+ #[cfg(target_os = "macos")]
+ println!(
+ "cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
+ install_path.to_str().unwrap(),
+ install_path.to_str().unwrap(),
+ );
+
+ #[cfg(target_os = "windows")]
+ {
+ println!("cargo:rustc-link-lib=binaryninjacore");
+ println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
+ }
+}
diff --git a/rust/examples/template/src/main.rs b/rust/examples/template/src/main.rs
new file mode 100644
index 00000000..7947c0f7
--- /dev/null
+++ b/rust/examples/template/src/main.rs
@@ -0,0 +1,46 @@
+use binaryninja::architecture::Architecture;
+use binaryninja::binaryview::{BinaryViewBase, BinaryViewExt};
+
+// Standalone executables need to provide a main function for rustc
+// Plugins should refer to `binaryninja::command::*` for the various registration callbacks.
+fn main() {
+ // This loads all the core architecture, platform, etc plugins
+ // Standalone executables probably need to call this, but plugins do not
+ println!("Loading plugins...");
+ binaryninja::headless::init();
+
+ // Your code here...
+ println!("Loading binary...");
+ let bv = binaryninja::open_view("/bin/cat").expect("Couldn't open `/bin/cat`");
+
+ println!("Filename: `{}`", bv.metadata().filename());
+ println!("File size: `{:#x}`", bv.len());
+ println!("Function count: {}", bv.functions().len());
+
+ for func in &bv.functions() {
+ println!(" `{}`:", func.symbol().full_name());
+ for basic_block in &func.basic_blocks() {
+ // TODO : This is intended to be refactored to be more nice to work with soon(TM)
+ for addr in basic_block.as_ref() {
+ print!(" {} ", addr);
+ match func.arch().instruction_text(
+ bv.read_buffer(addr, func.arch().max_instr_len())
+ .unwrap()
+ .get_data(),
+ addr,
+ ) {
+ Some((_, tokens)) => {
+ tokens
+ .iter()
+ .for_each(|token| print!("{}", token.text().to_str().unwrap()));
+ println!("")
+ }
+ _ => (),
+ }
+ }
+ }
+ }
+
+ // Important! Standalone executables need to call shutdown or they will hang forever
+ binaryninja::headless::shutdown();
+}