diff options
| author | Fabian Freyer <fabian.freyer@physik.tu-berlin.de> | 2022-01-28 01:36:26 +0100 |
|---|---|---|
| committer | KyleMiles <krm504@nyu.edu> | 2022-02-14 18:45:11 -0500 |
| commit | 6cd49edb317112acc73f7caeb285f4134a472d72 (patch) | |
| tree | 3fc1af20b46dcb33c1ba5490193a0226e112e839 /rust/examples/decompile/src | |
| parent | 181ad567199e5685578ecf7ad400257098c2e175 (diff) | |
rust: add linearview and related APIs
This should include everything required to retrieve linear disassembly
for a function.
* add `highest_address` method to `Function`
* add `DisassemblySettings`
* add text getter for `InstructionTextToken`
* add `LinearViewObject`, `LinearViewCursor`
* add decompilation example
Diffstat (limited to 'rust/examples/decompile/src')
| -rw-r--r-- | rust/examples/decompile/src/main.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/rust/examples/decompile/src/main.rs b/rust/examples/decompile/src/main.rs new file mode 100644 index 00000000..accb7697 --- /dev/null +++ b/rust/examples/decompile/src/main.rs @@ -0,0 +1,54 @@ +use binaryninja::binaryview::{BinaryView, BinaryViewBase, BinaryViewExt}; +use binaryninja::disassembly::{DisassemblyOption, DisassemblySettings}; +use binaryninja::function::Function; +use binaryninja::linearview::{LinearViewCursor, LinearViewObject}; + +use clap::Parser; + +/// Use binaryninja to decompile to C. +#[derive(Parser, Debug)] +#[clap(version, long_about = None)] +struct Args { + /// Path to the file to decompile + filename: String, +} + +fn decompile_to_c(view: &BinaryView, func: &Function) { + let mut settings = DisassemblySettings::new(); + settings.set_option(DisassemblyOption::ShowAddress, false); + settings.set_option(DisassemblyOption::WaitForIL, true); + + let linearview = LinearViewObject::language_representation(view, &settings); + + let mut cursor = LinearViewCursor::new(&linearview); + cursor.seek_to_address(func.highest_address()); + + let last = view.get_next_linear_disassembly_lines(&mut cursor.duplicate()); + let first = view.get_previous_linear_disassembly_lines(&mut cursor); + + let lines = first.into_iter().chain(last.into_iter()); + + for line in lines { + println!("{}", line.as_ref()); + } +} + +fn main() { + let args = Args::parse(); + + eprintln!("Loading plugins..."); + binaryninja::headless::init(); + + eprintln!("Loading binary..."); + let bv = binaryninja::open_view(args.filename).expect("Couldn't open file"); + + eprintln!("Filename: `{}`", bv.metadata().filename()); + eprintln!("File size: `{:#x}`", bv.len()); + eprintln!("Function count: {}", bv.functions().len()); + + for func in &bv.functions() { + decompile_to_c(bv.as_ref(), func.as_ref()); + } + + binaryninja::headless::shutdown(); +} |
