Crates.io | mach_object |
lib.rs | mach_object |
version | 0.1.17 |
source | src |
created_at | 2016-11-01 04:39:38.518861 |
updated_at | 2022-06-18 10:47:41.310104 |
description | Mach-O File Format Parser for Rust |
homepage | http://flier.github.io/rust-macho/ |
repository | https://github.com/flier/rust-macho |
max_upload_size | |
id | 7060 |
size | 666,010 |
Mach-O File Format Parser for Rust
To use, add the following line to Cargo.toml under [dependencies]:
mach_object = "0.1"
or alternatively,
mach_object = { git = "https://github.com/flier/rust-macho.git" }
Use OFile::parse to read the mach-o file from a &[u8] slice.
use std::io::{Read, Cursor};
use std::fs::File;
use mach_object::{OFile, CPU_TYPE_X86_64, MachCommand, LoadCommand};
let mut f = File::open("test/helloworld").unwrap();
let mut buf = Vec::new();
let size = f.read_to_end(&mut buf).unwrap();
let mut cur = Cursor::new(&buf[..size]);
if let OFile::MachFile { ref header, ref commands } = OFile::parse(&mut cur).unwrap() {
assert_eq!(header.cputype, CPU_TYPE_X86_64);
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.