17 releases

0.1.17 Jun 18, 2022
0.1.15 Mar 2, 2021
0.1.14 Nov 21, 2019
0.1.13 Sep 27, 2018
0.1.1 Nov 1, 2016

#387 in Parser implementations

Download history 1311/week @ 2024-07-21 1443/week @ 2024-07-28 1692/week @ 2024-08-04 1155/week @ 2024-08-11 1216/week @ 2024-08-18 1159/week @ 2024-08-25 805/week @ 2024-09-01 808/week @ 2024-09-08 930/week @ 2024-09-15 901/week @ 2024-09-22 883/week @ 2024-09-29 630/week @ 2024-10-06 598/week @ 2024-10-13 1020/week @ 2024-10-20 1238/week @ 2024-10-27 1096/week @ 2024-11-03

4,008 downloads per month
Used in 7 crates (5 directly)

Apache-2.0

235KB
4K SLoC

Contains (Mach-o exe, 320KB) tests/hellorust, (Mach-o exe, 10KB) tests/helloobjc, (Mach-o exe, 9KB) tests/helloworld, (static library, 3KB) tests/libfoo.a

rust-macho travis crate docs

Mach-O File Format Parser for Rust

Usage

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" }

Examples

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.

Dependencies

~0.7–1.5MB
~32K SLoC