Skip to content

Commit fda7c72

Browse files
committed
[Rust] Add more type library examples
Examples for turning c header files and BNDBs into type libraries in headless rust
1 parent 9397116 commit fda7c72

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Usage: cargo run --example bndb_to_type_library <bndb_path> <type_library_path>
2+
3+
use binaryninja::binary_view::BinaryViewExt;
4+
use binaryninja::type_library::TypeLibrary;
5+
use binaryninja::types::QualifiedName;
6+
7+
fn main() {
8+
let bndb_path_str = std::env::args().nth(1).expect("No header provided");
9+
let bndb_path = std::path::Path::new(&bndb_path_str);
10+
11+
let type_lib_path_str = std::env::args().nth(2).expect("No type library provided");
12+
let type_lib_path = std::path::Path::new(&type_lib_path_str);
13+
let type_lib_name = type_lib_path.file_stem().unwrap().to_str().unwrap();
14+
15+
println!("Starting session...");
16+
// This loads all the core architecture, platform, etc plugins
17+
let headless_session =
18+
binaryninja::headless::Session::new().expect("Failed to initialize session");
19+
20+
let file = headless_session
21+
.load(bndb_path)
22+
.expect("Failed to load BNDB");
23+
24+
let type_lib = TypeLibrary::new(file.default_arch().unwrap(), type_lib_name);
25+
26+
for ty in &file.types() {
27+
println!("Adding type: {}", ty.name);
28+
type_lib.add_named_type(ty.name, &ty.ty);
29+
}
30+
31+
for func in &file.functions() {
32+
println!("Adding function: {}", func.symbol());
33+
let qualified_name =
34+
QualifiedName::from(func.symbol().short_name().to_string_lossy().to_string());
35+
type_lib.add_named_object(qualified_name, &func.function_type());
36+
}
37+
38+
type_lib.write_to_file(&type_lib_path);
39+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Usage: cargo run --example create_type_library <header_file_path> <platform> <type_library_path>
2+
3+
use binaryninja::platform::Platform;
4+
use binaryninja::type_library::TypeLibrary;
5+
use binaryninja::type_parser::{CoreTypeParser, TypeParser};
6+
7+
fn main() {
8+
let header_path_str = std::env::args().nth(1).expect("No header provided");
9+
let header_path = std::path::Path::new(&header_path_str);
10+
let header_name = header_path.file_stem().unwrap().to_str().unwrap();
11+
let type_lib_plat_str = std::env::args().nth(2).expect("No type library provided");
12+
let type_lib_path_str = std::env::args().nth(3).expect("No type library provided");
13+
let type_lib_path = std::path::Path::new(&type_lib_path_str);
14+
let type_lib_name = type_lib_path.file_stem().unwrap().to_str().unwrap();
15+
16+
let header_contents = std::fs::read_to_string(header_path).expect("Failed to read header file");
17+
18+
println!("Starting session...");
19+
// This loads all the core architecture, platform, etc plugins
20+
let _headless_session =
21+
binaryninja::headless::Session::new().expect("Failed to initialize session");
22+
23+
let type_lib_plat = Platform::by_name(&type_lib_plat_str).expect("Invalid platform");
24+
25+
let type_lib = TypeLibrary::new(type_lib_plat.arch(), type_lib_name);
26+
27+
let plat_type_container = type_lib_plat.type_container();
28+
let parser = CoreTypeParser::default();
29+
let parsed_types = parser
30+
.parse_types_from_source(
31+
&header_contents,
32+
header_name,
33+
&type_lib_plat,
34+
&plat_type_container,
35+
&[],
36+
&[],
37+
"",
38+
)
39+
.expect("Parsed types");
40+
41+
for ty in parsed_types.types {
42+
println!("Adding type: {}", ty.name);
43+
type_lib.add_named_type(ty.name, &ty.ty);
44+
}
45+
46+
for func in parsed_types.functions {
47+
println!("Adding function: {}", func.name);
48+
type_lib.add_named_object(func.name, &func.ty);
49+
}
50+
51+
type_lib.write_to_file(&type_lib_path);
52+
}

rust/examples/dump_type_library.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,29 @@ fn main() {
1515
binaryninja::headless::Session::new().expect("Failed to initialize session");
1616

1717
let type_lib = TypeLibrary::load_from_file(type_lib_path).expect("Failed to load type library");
18-
let named_types = type_lib.named_types();
1918
println!("Name: `{}`", type_lib.name());
2019
println!("GUID: `{}`", type_lib.guid());
2120

2221
// Print out all the types as a c header.
2322
let type_lib_header_path = type_lib_path.with_extension("h");
23+
24+
let all_types: Vec<_> = type_lib
25+
.named_types()
26+
.iter()
27+
.chain(type_lib.named_objects().iter())
28+
.collect();
29+
2430
println!(
2531
"Dumping {} types to: `{:?}`",
26-
named_types.len(),
32+
all_types.len(),
2733
type_lib_header_path
2834
);
2935
let type_printer = CoreTypePrinter::default();
3036
let empty_bv =
3137
BinaryView::from_data(&FileMetadata::new(), &[]).expect("Failed to create empty view");
3238
let printed_types = type_printer
3339
.print_all_types(
34-
&type_lib.named_types(),
40+
all_types,
3541
&empty_bv,
3642
4,
3743
TokenEscapingType::NoTokenEscapingType,

0 commit comments

Comments
 (0)