Module macho

Source
Expand description

Support for reading Mach-O files.

Traits are used to abstract over the difference between 32-bit and 64-bit Mach-O files. The primary trait for this is MachHeader.

§High level API

MachOFile implements the Object trait for Mach-O files. MachOFile is parameterised by MachHeader to allow reading both 32-bit and 64-bit Mach-O files. There are type aliases for these parameters (MachOFile32 and MachOFile64).

§Low level API

The MachHeader trait can be directly used to parse both macho::MachHeader32 and macho::MachHeader64. Additionally, FatHeader and the FatArch trait can be used to iterate images in multi-architecture binaries, and DyldCache can be used to locate images in a dyld shared cache.

§Example for low level API

use object::macho;
use object::read::macho::{MachHeader, Nlist};
use std::error::Error;
use std::fs;

/// Reads a file and displays the name of each symbol.
fn main() -> Result<(), Box<dyn Error>> {
   let data = fs::read("path/to/binary")?;
   let header = macho::MachHeader64::<object::Endianness>::parse(&*data, 0)?;
   let endian = header.endian()?;
   let mut commands = header.load_commands(endian, &*data, 0)?;
   while let Some(command) = commands.next()? {
       if let Some(symtab_command) = command.symtab()? {
           let symbols = symtab_command.symbols::<macho::MachHeader64<_>, _>(endian, &*data)?;
           for symbol in symbols.iter() {
               let name = symbol.name(endian, symbols.strings())?;
               println!("{}", String::from_utf8_lossy(name));
           }
       }
   }
   Ok(())
}

Re-exports§

pub use macho::FatArch32;
pub use macho::FatArch64;
pub use macho::FatHeader;

Structs§

DyldCache
A parsed representation of the dyld shared cache.
DyldCacheImage
One image (dylib) from inside the dyld shared cache.
DyldCacheImageIterator
An iterator over all the images (dylibs) in the dyld shared cache.
DyldSubCache
Information about a subcache.
LoadCommandData
The data for a macho::LoadCommand.
LoadCommandIterator
An iterator for the load commands from a MachHeader.
MachOComdat
A COMDAT section group in a MachOFile.
MachOComdatIterator
An iterator for the COMDAT section groups in a MachOFile.
MachOComdatSectionIterator
An iterator for the sections in a COMDAT section group in a MachOFile.
MachOFatFile
A Mach-O universal binary.
MachOFile
A partially parsed Mach-O file.
MachORelocationIterator
An iterator for the relocations in a MachOSection.
MachOSection
A section in a MachOFile.
MachOSectionIterator
An iterator for the sections in a MachOFile.
MachOSegment
A segment in a MachOFile.
MachOSegmentIterator
An iterator for the segments in a MachOFile.
MachOSymbol
A symbol in a MachOFile.
MachOSymbolIterator
An iterator for the symbols in a MachOFile.
MachOSymbolTable
A symbol table in a MachOFile.
SymbolTable
A table of symbol entries in a Mach-O file.

Enums§

DyldSubCacheSlice
A slice of structs describing each subcache. The struct gained an additional field (the file suffix) in dyld-1042.1 (macOS 13 / iOS 16), so this is an enum of the two possible slice types.
LoadCommandVariant
A macho::LoadCommand that has been interpreted according to its cmd field.

Traits§

FatArch
A trait for generic access to macho::FatArch32 and macho::FatArch64.
MachHeader
A trait for generic access to macho::MachHeader32 and macho::MachHeader64.
Nlist
A trait for generic access to macho::Nlist32 and macho::Nlist64.
Section
A trait for generic access to macho::Section32 and macho::Section64.
Segment
A trait for generic access to macho::SegmentCommand32 and macho::SegmentCommand64.

Functions§

address_to_file_offset
Find the file offset of the image by looking up its address in the mappings.

Type Aliases§

MachOComdat32
A COMDAT section group in a MachOFile32.
MachOComdat64
A COMDAT section group in a MachOFile64.
MachOComdatIterator32
An iterator for the COMDAT section groups in a MachOFile64.
MachOComdatIterator64
An iterator for the COMDAT section groups in a MachOFile64.
MachOComdatSectionIterator32
An iterator for the sections in a COMDAT section group in a MachOFile32.
MachOComdatSectionIterator64
An iterator for the sections in a COMDAT section group in a MachOFile64.
MachOFatFile32
A 32-bit Mach-O universal binary.
MachOFatFile64
A 64-bit Mach-O universal binary.
MachOFile32
A 32-bit Mach-O object file.
MachOFile64
A 64-bit Mach-O object file.
MachORelocationIterator32
An iterator for the relocations in a MachOSection32.
MachORelocationIterator64
An iterator for the relocations in a MachOSection64.
MachOSection32
A section in a MachOFile32.
MachOSection64
A section in a MachOFile64.
MachOSectionIterator32
An iterator for the sections in a MachOFile32.
MachOSectionIterator64
An iterator for the sections in a MachOFile64.
MachOSegment32
A segment in a MachOFile32.
MachOSegment64
A segment in a MachOFile64.
MachOSegmentIterator32
An iterator for the segments in a MachOFile32.
MachOSegmentIterator64
An iterator for the segments in a MachOFile64.
MachOSymbol32
A symbol in a MachOFile32.
MachOSymbol64
A symbol in a MachOFile64.
MachOSymbolIterator32
An iterator for the symbols in a MachOFile32.
MachOSymbolIterator64
An iterator for the symbols in a MachOFile64.
MachOSymbolTable32
A symbol table in a MachOFile32.
MachOSymbolTable64
A symbol table in a MachOFile64.