| 1 | use std::{fmt::Display, fs::File, io::Read}; |
| 2 | |
| 3 | use macho_unwind_info::opcodes::{OpcodeArm64, OpcodeX86, OpcodeX86_64}; |
| 4 | use macho_unwind_info::UnwindInfo; |
| 5 | use object::{Architecture, ObjectSection}; |
| 6 | |
| 7 | fn main() { |
| 8 | let mut args = std::env::args_os().skip(1); |
| 9 | if args.len() < 1 { |
| 10 | eprintln!("Usage: {} <path>", std::env::args().next().unwrap()); |
| 11 | std::process::exit(1); |
| 12 | } |
| 13 | let path = args.next().unwrap(); |
| 14 | |
| 15 | let mut data = Vec::new(); |
| 16 | let mut file = File::open(path).unwrap(); |
| 17 | file.read_to_end(&mut data).unwrap(); |
| 18 | let data = &data[..]; |
| 19 | |
| 20 | let file = object::File::parse(data).expect("Could not parse object file"); |
| 21 | use object::Object; |
| 22 | let unwind_info_data_section = file |
| 23 | .section_by_name_bytes(b"__unwind_info") |
| 24 | .expect("Could not find __unwind_info section"); |
| 25 | let data = unwind_info_data_section.data().unwrap(); |
| 26 | let arch = file.architecture(); |
| 27 | |
| 28 | let info = UnwindInfo::parse(data).unwrap(); |
| 29 | let address_range = info.address_range(); |
| 30 | println!( |
| 31 | "Unwind info for address range 0x{:08x}-0x{:08x}", |
| 32 | address_range.start, address_range.end |
| 33 | ); |
| 34 | println!(); |
| 35 | let mut function_iter = info.functions(); |
| 36 | while let Some(function) = function_iter.next().unwrap() { |
| 37 | print_entry(function.start_address, function.opcode, arch); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fn print_entry(address: u32, opcode: u32, arch: Architecture) { |
| 42 | match arch { |
| 43 | Architecture::I386 => print_entry_impl(address, OpcodeX86::parse(opcode)), |
| 44 | Architecture::X86_64 => print_entry_impl(address, OpcodeX86_64::parse(opcode)), |
| 45 | Architecture::Aarch64 => print_entry_impl(address, OpcodeArm64::parse(opcode)), |
| 46 | _ => {} |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | fn print_entry_impl(address: u32, opcode: impl Display) { |
| 51 | println!("0x{:08x}: {}", address, opcode); |
| 52 | } |