|
| 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 | +} |
0 commit comments