Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:Mach-O

From ReverseEngineering.wiki
Format
Platforms Platform:macOS, iOS
Magic CF FA ED FE (64-bit LE), CE FA ED FE (32-bit LE), CA FE BA BE (fat, big-endian)
Extensions .dylib, .bundle, .o, .kext, no extension for executables
Endianness host order of the target (little-endian on x86-64 and arm64); fat headers always big-endian
On this wiki Facts · Code · Notes · Topics

Executable, library and object format of macOS, iOS, tvOS and watchOS (and NeXTSTEP): a 32-byte (64-bit) mach_header starting with FE ED FA CF (little-endian files; FEEDFACE for 32-bit), a list of load commands (segments, symbol tables, dylib dependencies, code signature, fixups), and the segment data they map. Several architectures can be bundled in a big-endian fat (universal) header with magic CA FE BA BE. It is the native binary format of Platform:macOS and Platform:iOS.

Overview

A Mach-O file starts with a mach_header (mach_header_64, 32 bytes, for 64-bit): magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds, flags and (64-bit only) reserved. The ncmds load commands follow directly, each starting with cmd and cmdsize (multiple of 8 on 64-bit). Segments and sections referred to by the load commands follow in the file.

The magic is read in the file's own byte order: the value 0xFEEDFACF appears on disk as CF FA ED FE in little-endian files (all current Apple hardware), and as FE ED FA CF in big-endian (PowerPC) files. 32-bit is 0xFEEDFACE (CE FA ED FE on disk). A reader that sees the swapped constants (MH_CIGAM = 0xCEFAEDFE, MH_CIGAM_64 = 0xCFFAEDFE as a 32-bit read) must byte-swap every field.

Fat (universal) files

A fat file starts with fat_header {magic 0xCAFEBABE, nfat_arch}, always big-endian, followed by nfat_arch fat_arch records {cputype, cpusubtype, offset, size, align (power of two)} of 20 bytes each. Each slice is a complete Mach-O (or archive) at its offset. CAFEBABE is also the Java class file magic: tell them apart by the next dword (a small architecture count, below about 20, versus a Java version number of 45 or more). See Format:Mach-O/Load commands for CPU types and the loader structures.

File types (filetype)

MH_OBJECT 1, MH_EXECUTE 2, MH_FVMLIB 3, MH_CORE 4, MH_PRELOAD 5, MH_DYLIB 6, MH_DYLINKER 7, MH_BUNDLE 8, MH_DYLIB_STUB 9, MH_DSYM 0xA (debug companion), MH_KEXT_BUNDLE 0xB, MH_FILESET 0xC (kernel collections: holds LC_FILESET_ENTRY members).

Common header flags

MH_NOUNDEFS 0x1, MH_DYLDLINK 0x4, MH_TWOLEVEL 0x80, MH_PIE 0x200000 (ASLR-capable executable), MH_HAS_TLV_DESCRIPTORS 0x800000, MH_NO_HEAP_EXECUTION 0x1000000, MH_APP_EXTENSION_SAFE 0x2000000, MH_DYLIB_IN_CACHE 0x80000000 (dylib lives in the Format:dyld shared cache).

Where to look when reversing

  • LC_MAIN (entry offset in __TEXT) for modern executables, LC_UNIXTHREAD for old ones.
  • LC_LOAD_DYLIB family for dependencies, LC_CODE_SIGNATURE for the Format:Mach-O/Code signature, LC_ENCRYPTION_INFO(_64) with cryptid 1 for FairPlay-encrypted App Store binaries (the encrypted range is unreadable until dumped from a running process).
  • Sections __TEXT,__text, __stubs, __cstring, __objc_classlist, __DATA,__got; imports through LC_DYLD_CHAINED_FIXUPS or LC_DYLD_INFO_ONLY.
  • Packed Mach-O files (UPX) are listed on Tool:UPX.

Tools

LIEF (Apache-2.0) parses and rewrites Mach-O. Apple's own headers are in xnu EXTERNAL_HEADERS/mach-o/.

Topics

  • Load commands — Reference of the Mach-O load commands (LC_* values), the CPU type constants used by mach_header and fat_arch, platform ids of LC_BUILD_VERSION and the segment…
  • Code signature — The embedded code signature of Mach-O files (LC_CODE_SIGNATURE): a big-endian SuperBlob with magic FA DE 0C C0 containing the CodeDirectory (FA DE 0C 02)…
  • Chained fixups — LC_DYLD_CHAINED_FIXUPS (0x80000034): the header, per-segment starts, import tables and every DYLD_CHAINED_PTR_* pointer format with bit layouts (including…
  • Bind and rebase opcodes — The opcode streams of LC_DYLD_INFO(ONLY): dyld_info_command fields, REBASE_OPCODE', BIND_OPCODE_ and EXPORT_SYMBOL_FLAGS_* values from loader.h, how to…
  • Encryption info — LC_ENCRYPTION_INFO and LC_ENCRYPTION_INFO_64: field layout and how to recognize an encrypted (cryptid 1) binary. Recognition only.

Related Apple formats: Format:Kernelcache (MH_FILESET kernel collections), Format:IMG4 (firmware wrapper), Format:PBZX (OTA payload stream), Format:Dyld shared cache, Format:IPA, Format:Apple provisioning profile, Format:Assets.car (compiled asset catalog) and Format:BOM (Bill of Materials store), Format:XAR archive, Format:Apple Disk Image (UDIF). Unverified: AppleArchive (magic AA01) and Apple Encrypted Archive (AEA1) wrappers appear in recent IPSW/OTA packages; no source for their layout was read, ipsw has pkg/aea.

Code & repositories

  • https://github.com/apple-oss-distributions/xnu @ f6217f891ac0bb64f3d375211650a4c1ff8ca1ea · license: APSL-2.0 (declared by the contributor; GitHub's detector did not recognize a license file)
    Apple XNU kernel source: EXTERNAL_HEADERS/mach-o (loader.h, fat.h, fixup-chains.h) and osfmk/kern/cs_blobs.h
    Tested on: source read, not run: headers read only · checked 2026-10-18
  • https://github.com/lief-project/LIEF @ 2795838f5f33efbc10f071d194c44197a86a36d8 · license: Apache-2.0 (declared by the contributor for the files used; GitHub reports Apache-2.0 (read from LICENSE; GitHub's API was busy) for the repository)
    LIEF: parser and rewriter for ELF, PE, Mach-O, DEX, OAT, VDEX and ART; src/DEX, src/OAT, src/VDEX, src/ART Structures.hpp give the header layouts per Android release
    Tested on: source read, not run: source read only · checked 2026-10-18
  • https://github.com/apple-oss-distributions/dyld @ fd8d0c4d52320ebf64db34f3cb280310d905c5ae · license: APSL-2.0 (declared by the contributor; GitHub's detector did not recognize a license file)
    dyld source: include/mach-o/fixup-chains.h (chained fixup structs and DYLD_CHAINED_PTR_* values)
    Tested on: source read, not run: source read, not run · checked 2026-10-18; last activity 2026-06-18