Skip to content

Commit f4068c2

Browse files
committed
Rust binding for LLVM MC disassembler
Using this for debugging purposes
1 parent cc92c16 commit f4068c2

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

binaryninjacore.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7532,11 +7532,11 @@ extern "C"
75327532

75337533
// LLVM Services APIs
75347534
BINARYNINJACOREAPI void BNLlvmServicesInit(void);
7535-
75367535
BINARYNINJACOREAPI int BNLlvmServicesAssemble(const char* src, int dialect, const char* triplet, int codeModel,
75377536
int relocMode, char** outBytes, int* outBytesLen, char** err, int* errLen);
7538-
75397537
BINARYNINJACOREAPI void BNLlvmServicesAssembleFree(char* outBytes, char* err);
7538+
BINARYNINJACOREAPI int BNLlvmServicesDisasmInstruction(const char *triplet, uint8_t *src, int srcLen,
7539+
uint64_t addr, char *result, size_t resultMaxSize);
75407540

75417541
// Filesystem functionality
75427542
BINARYNINJACOREAPI bool BNDeleteFile(const char* path);

rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub mod variable;
8989
pub mod websocket;
9090
pub mod worker_thread;
9191
pub mod workflow;
92+
pub mod llvm;
9293

9394
use crate::file_metadata::FileMetadata;
9495
use crate::function::Function;

rust/src/llvm.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use binaryninjacore_sys::BNLlvmServicesDisasmInstruction;
2+
use std::ffi::CStr;
3+
4+
pub fn disas_instruction(triplet: &str, data: &[u8], address64: u64) -> Option<(usize, String)> {
5+
return unsafe {
6+
let mut buf = vec![0; 256];
7+
let instr_len = BNLlvmServicesDisasmInstruction(
8+
triplet.as_ptr() as *const i8,
9+
data.as_ptr() as *mut u8,
10+
data.len() as i32,
11+
address64 as u64,
12+
buf.as_mut_ptr() as *mut i8,
13+
buf.len() as usize,
14+
);
15+
if instr_len > 0 {
16+
let cstr = CStr::from_ptr(buf.as_ptr() as *const i8);
17+
let string = cstr.to_str().unwrap().to_string();
18+
Some((instr_len as usize, string))
19+
} else {
20+
None
21+
}
22+
};
23+
}

0 commit comments

Comments
 (0)