summaryrefslogtreecommitdiff
path: root/rust/examples/mlil_lifter/src
diff options
context:
space:
mode:
authorRubens Brandao <git@rubens.io>2023-11-18 15:58:33 -0300
committerKyle Martin <krm504@nyu.edu>2023-11-21 15:18:50 -0500
commit8c9cdd38c3302280087c9e6d94f7f57083885edd (patch)
tree8bccf380b4470e0de8ac11c23b164e0acf6ffbb0 /rust/examples/mlil_lifter/src
parentb040fcfce48db861600eeb122cd0e2ff802fac96 (diff)
add mlil to rust
Diffstat (limited to 'rust/examples/mlil_lifter/src')
-rw-r--r--rust/examples/mlil_lifter/src/main.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/rust/examples/mlil_lifter/src/main.rs b/rust/examples/mlil_lifter/src/main.rs
new file mode 100644
index 00000000..3ba25630
--- /dev/null
+++ b/rust/examples/mlil_lifter/src/main.rs
@@ -0,0 +1,52 @@
+use std::env;
+
+use binaryninja::binaryview::BinaryViewExt;
+
+// Standalone executables need to provide a main function for rustc
+// Plugins should refer to `binaryninja::command::*` for the various registration callbacks.
+fn main() {
+ let mut args = env::args();
+ let _ = args.next().unwrap();
+ let Some(filename) = args.next() else {
+ panic!("Expected input filename\n");
+ };
+
+ // 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::load(filename).expect("Couldn't open binary file");
+
+ // Go through all functions in the binary
+ for func in bv.functions().iter() {
+ let sym = func.symbol();
+ println!("Function {}:", sym.full_name());
+
+ let Ok(il) = func.medium_level_il() else {
+ println!(" Does not have MLIL\n");
+ continue;
+ };
+ // Get the SSA form for this function
+ let il = il.ssa_form();
+
+ // Loop through all blocks in the function
+ for block in il.basic_blocks().iter() {
+ // Loop though each instruction in the block
+ for instr in block.iter() {
+ // Uplift the instruction into a native rust format
+ let lifted = instr.lift();
+ let address = instr.address();
+
+ // print the lifted instruction
+ println!("{address:08x}: {lifted:x?}");
+ }
+ }
+ println!();
+ }
+
+ // Important! Standalone executables need to call shutdown or they will hang forever
+ binaryninja::headless::shutdown();
+}