Mach-O Reference
mach_header_64The Mach-O header (mach_header_64)
Every Mach-O file starts with a header giving its magic number, CPU type, file type and the count and size of the load commands that follow. It's the first thing a parser reads.
What it is
A Mach-O file opens with a fixed-size header. On 64-bit files it is
mach_header_64; on 32-bit files, mach_header. It tells a loader (and a
parser) what the file is before any of its contents are read.
Fields
| Field | Meaning |
|---|---|
magic | 0xFEEDFACF (64-bit) or 0xFEEDFACE (32-bit); byte-swapped variants mark the opposite endianness. |
cputype / cpusubtype | The architecture, e.g. x86_64 or arm64. |
filetype | What the file is: MH_EXECUTE, MH_DYLIB, MH_BUNDLE, MH_OBJECT, MH_DSYM… |
ncmds | Number of load commands that follow the header. |
sizeofcmds | Total bytes of all load commands. |
flags | Attributes such as MH_PIE, MH_TWOLEVEL, MH_DYLDLINK. |
reserved | 64-bit only; padding. |
Reading order
The header is immediately followed by ncmds
load commands, filling sizeofcmds bytes. A
parser reads the header, then walks exactly that many commands. The magic
value also tells the parser the file's endianness and word size, which it
needs before interpreting any further field.
Fat (universal) binaries
A universal binary is not a Mach-O header at all at offset 0 — it starts
with a fat_header (0xCAFEBABE) listing architecture slices, each of which is
a complete Mach-O with its own mach_header_64.