Mach-O File Format Parser for Rust
Use OFile::parse to read the mach-o file from a &[u8] slice.
```rust use std::io::{Read, Cursor}; use std::fs::File; use machobject::{OFile, CPUTYPEX8664, MachCommand, LoadCommand};
let mut f = File::open("test/helloworld").unwrap(); let mut buf = Vec::new(); let size = f.readtoend(&mut buf).unwrap(); let mut cur = Cursor::new(&buf[..size]); if let OFile::MachFile { ref header, ref commands } = OFile::parse(&mut cur).unwrap() { asserteq!(header.cputype, CPUTYPEX8664); assert_eq!(header.ncmds as usize, commands.len()); for &MachCommand(ref cmd, cmdsize) in commands { if let &LoadCommand::Segment64 { ref segname, ref sections, .. } = cmd { println!("segment: {}", segname);
for ref sect in sections {
println!(" section: {}", sect.sectname);
}
}
}
} ```
For more detail, please check the unit tests and the otool example.