Expand description
Mach-O reader and writer with byte-identical round-trip.
v1 covers thin (non-fat) 64-bit little-endian Mach-O images
for both x86-64 and arm64. The parsed representation captures
the structured header and each load command’s (cmd, cmdsize)
prefix; load command bodies stay as opaque bytes so the suite
of cmd kinds (LC_SEGMENT_64, LC_SYMTAB, LC_CODE_SIGNATURE,
LC_DYLD_CHAINED_FIXUPS, …) round-trip without needing per-cmd
decoders.
Contract: for any supported input bytes,
MachoFile::parse(bytes)?.write_to_vec() == bytes.
Fat (universal) wrappers and 32-bit Mach-O are out of scope
for v1. Section contents (the bytes inside __text, __data,
etc.) are never interpreted here — that belongs to the arch
backends and analysis crates.
Structs§
- Build
Version Tool - LcBuild
Version - Body of
LC_BUILD_VERSION. Records the platform / minimum OS / SDK / per-tool versions.ntoolsis recovered fromtools.len()at encode time. - LcDylib
- Body of
LC_LOAD_DYLIB/LC_ID_DYLIB/LC_LOAD_WEAK_DYLIB/LC_REEXPORT_DYLIB. The same 4-u32 dylib record followed by the NUL-padded name string. - LcDylinker
- Body of
LC_LOAD_DYLINKER(and the analogousLC_ID_DYLINKER— same shape). Theoffsetis from the start of the command (including the cmd/cmdsize prefix), which in the canonical linker output is0xc— i.e. the name begins at body offset 4.nameholds the C string up to (but not including) the first NUL;tail_paddingcarries the NUL + any trailing alignment NULs verbatim, so the encoded length always matches the original. - LcDysymtab
- Structurally decoded body of an
LC_DYSYMTABcommand — 18u32s describing partitions of the LC_SYMTAB table and auxiliary indirect-symbol / module / reference tables. - LcLinkedit
Data - Body of every
linkedit_data_command-shaped load command (LC_CODE_SIGNATURE, LC_FUNCTION_STARTS, LC_DATA_IN_CODE, LC_DYLD_EXPORTS_TRIE, LC_DYLD_CHAINED_FIXUPS, …). - LcMain
- Body of
LC_MAIN. Entry-point offset + initial stack size. - LcSource
Version - Body of
LC_SOURCE_VERSION: one packed 64-bit version. - LcSymtab
- Structurally decoded body of an
LC_SYMTABcommand. Fouru32s sized exactly 16 bytes on disk. - LcUuid
- Body of
LC_UUID: 16 raw bytes. - Load
Command - One load command from the table that follows the file header.
- Mach
Header64 - Parsed 64-bit Mach-O header.
- Macho
File - A parsed thin 64-bit Mach-O file in a form that round-trips byte-identically.
- Section64
- A
section_64entry within anLC_SEGMENT_64. Field naming matches Apple’s struct verbatim. - Segment64
- A
LC_SEGMENT_64descriptor, structurally decoded enough to drive segment-data extraction and the decompile path’s section iteration. The raw bytes still round-trip through the matchingLoadCommand::body.
Enums§
- Error
- Errors surfaced when parsing or writing a Mach-O file.
- Macho
Cpu - Architecture flavour the parsed Mach-O targets.
Constants§
- CPU_
TYPE_ ARM64 - CPU_
TYPE_ X86_ 64 cputypevalues for the architectures v1 supports.- FAT_
MAGIC - Fat-arch wrapper magic (big-endian). Detected so callers can route appropriately; v1 parse refuses.
- FAT_
MAGIC_ 64 - LC_
BUILD_ VERSION LC_BUILD_VERSION: platform / minimum-OS / SDK / toolchain.- LC_
CODE_ SIGNATURE linkedit_data_commandkinds. All share the same 4 + 4 + 4 + 4 body shape: cmd + cmdsize prefix already eaten, then body =dataoff(u32) +datasize(u32).- LC_
DATA_ IN_ CODE - LC_
DYLD_ CHAINED_ FIXUPS - LC_
DYLD_ EXPORTS_ TRIE - LC_
DYLIB_ CODE_ SIGN_ DRS - LC_
DYSYMTAB LC_DYSYMTAB: extended dynamic-link symbol info. Body is 18 u32s describing partitions of the LC_SYMTAB table plus auxiliary indirect-symbol / module / reference tables.- LC_
FUNCTION_ STARTS - LC_
ID_ DYLIB LC_ID_DYLIB: identifies this image when it is itself a dylib.- LC_
LINKER_ OPTIMIZATION_ HINT - LC_
LOAD_ DYLIB LC_LOAD_DYLIB: dependent dynamic library. Body is adylib_commandafter the cmd/cmdsize prefix.- LC_
LOAD_ DYLINKER LC_LOAD_DYLINKER: file path of the dynamic linker. Body is alc_stroffset (u32) followed by the NUL-padded name.- LC_
LOAD_ WEAK_ DYLIB LC_LOAD_WEAK_DYLIB: dependent library, weak link.- LC_MAIN
LC_MAIN: entry-point load command. Body isentryoff(u64)- LC_
REEXPORT_ DYLIB LC_REEXPORT_DYLIB: dependent library to re-export.- LC_
REQ_ DYLD - Bit OR’d into
cmdto mark a command as required for correct dynamic linker behaviour. Not a kind by itself; ORed with the concreteLC_*value. - LC_
SEGMENT_ 64 LC_SEGMENT_64load-command kind. Carries a segment descriptor plus that segment’s sections.- LC_
SOURCE_ VERSION LC_SOURCE_VERSION: 64-bit packed source-control version.- LC_
SYMTAB LC_SYMTAB: classical symbol-table command. Body is four u32s:symoff,nsyms,stroff,strsize.- LC_UUID
LC_UUID: 16-byte randomly-generated identifier baked into the binary at link time.- MH_
MAGIC - 32-bit little-endian Mach-O magic (detected; v1 parse refuses).
- MH_
MAGIC_ 64 - 64-bit little-endian Mach-O magic.
Functions§
- is_
dylib_ cmd - True when
cmdis one of the dylib-shaped load commands. - is_fat
- True when
bytesare a fat (universal) Mach-O wrapper. Not supported by v1 parse, but exposed so callers can route around it. - is_
linkedit_ data_ cmd - True when
cmdis one of the linkedit_data_command-shaped load commands (the 4+4-byte body pattern). - is_
macho - True when
bytesstart with any Mach-O magic — thin 32- or 64-bit, either endian, plus the fat (universal) wrapper. Doesn’t say whether v1 will accept the file (useis_macho64for that gate). - is_
macho32 - True when
bytesare a thin 32-bit little-endian Mach-O. Detected so callers can route around it; v1 parse refuses. - is_
macho64 - True when
bytesare a thin 64-bit little-endian Mach-O — the flavourMachoFile::parsehandles. Callers that route by format should gate on this and fall through to a byte-copy for unsupported variants so the round-trip contract still holds.