Anatomy of a Mach-O File
The Mach-O format for incident responders: header and flags, universal binaries, load commands, segments, imports, __LINKEDIT and what matters in triage.
TL;DR. A Mach-O file is a header followed by a list of load commands. Those commands describe everything else in the file: the segments to map, the libraries to link, where execution starts and where the code signature is stored. The format is linear and self-describing, so a reader can walk a file from offset zero without running it. A few fields answer most triage questions: the file type, the architecture, the header flags, the linked libraries, the segment protections and the signature.
Who uses Mach-O
Every executable, library and plug-in on macOS and iOS is a Mach-O file. The filetype field of the header says which kind:
filetype | Value | What it is |
|---|---|---|
MH_EXECUTE | 0x2 | A program: the binary in Contents/MacOS, a command-line tool, a launch agent's target |
MH_DYLIB | 0x6 | A dynamic library: a .dylib or the binary inside a .framework |
MH_BUNDLE | 0x8 | A plug-in loaded with dlopen (.bundle, .plugin) |
MH_OBJECT | 0x1 | A compiler's .o output |
MH_KEXT_BUNDLE | 0xB | A kernel extension |
MH_DYLINKER | 0x7 | The dynamic linker, dyld |
MH_DSYM / MH_CORE | 0xA / 0x4 | Debug symbols, core dumps |
A .app is not a Mach-O file. It is a folder, and the program is Contents/MacOS/<name> inside it.
The header
At offset zero sits the mach_header. Its main fields are:
- the magic (
0xFEEDFACFfor 64-bit); - the CPU type and subtype:
x86_64,arm64, orarm64e, the variant with pointer authentication used by Apple's own binaries; - the file type;
- the number and total size of the load commands;
- the flags.
Three flags are worth a glance in triage:
MH_PIE(0x200000): the program can be loaded at a random address. Every modern toolchain sets it, so an executable without it is unusual.MH_ALLOW_STACK_EXECUTION(0x20000): the stack may hold executable code. No legitimate modern program needs this.MH_TWOLEVEL(0x80): every imported symbol is tied to a specific library, the two-level namespace. It is the norm.
Universal binaries
A file whose first four bytes are CA FE BA BE is a universal (or "fat") binary. Its fields are stored big-endian:
- the magic;
- a slice count;
- one
fat_archrecord per slice: CPU type, subtype, offset, size and alignment.
Each slice is a complete Mach-O file, aligned on a page boundary (16 KB with current tools). lipo creates and splits universal binaries.
What matters in triage is that each slice is signed on its own. The universal header is not covered by any signature, and neither is any gap between slices. Slices can also differ in more than their instructions: different imports, entitlements or strings. A careful analysis looks at every slice, not just the one the analyst's own machine would run.
Load commands: the table of contents
The load commands follow the header. Each one starts with cmd and cmdsize, so a reader can skip a command it doesn't know. The high bit LC_REQ_DYLD (0x80000000) marks the commands dyld must understand: it refuses to load a file containing an unknown command with that bit set. The ones you meet in every investigation:
| Command | Tells you |
|---|---|
LC_SEGMENT_64 | A segment, its memory protections and its sections |
LC_MAIN | The entry point (older binaries use LC_UNIXTHREAD) |
LC_LOAD_DYLIB | A linked library. Also in the family: LC_LOAD_WEAK_DYLIB (may be missing), LC_REEXPORT_DYLIB, LC_RPATH (search paths) |
LC_DYLD_INFO_ONLY or LC_DYLD_CHAINED_FIXUPS | How imports are bound and pointers fixed up |
LC_SYMTAB / LC_DYSYMTAB | The symbol table, and which symbols are imports |
LC_UUID | The build's unique identifier |
LC_BUILD_VERSION | Platform, minimum OS, SDK and the tools that built it |
LC_CODE_SIGNATURE | Where the signature is stored |
LC_ENCRYPTION_INFO_64 | Whether the code is FairPlay-encrypted (App Store apps) |
Segments and sections
Segments are what dyld maps into memory. Each one has a maximum and an initial protection:
__PAGEZERO: 4 GB of inaccessible memory at address zero in 64-bit programs, so that null-pointer accesses crash. A 64-bit executable without it is suspicious.__TEXT(r-x): the header and load commands themselves, the code (__text), the import stubs (__stubs), C strings (__cstring) and constants. A command-line tool's embedded Info.plist lives in__TEXT,__info_plist.__DATA_CONSTand__DATA(rw-): the import pointers (__got), global variables, and the Objective-C class and selector lists.__LINKEDIT(r--): the tables the linker and the loader need. More on it below.
The protections are a quick sanity check. A segment that is both writable and executable, or a __TEXT that is writable, is a red flag. The same goes for an entry point outside __TEXT,__text. A __text section with near-maximum entropy usually holds packed or encrypted code.
Imports
A Mach-O program does not call printf directly. It calls a small stub that jumps through a pointer, and dyld fills that pointer in when the program loads. Older binaries describe these bindings with byte-code in LC_DYLD_INFO_ONLY. Programs built for macOS 12 or later encode them as chained fixups in LC_DYLD_CHAINED_FIXUPS: the pointers in the data segments form linked lists that dyld walks.
Both describe the same thing: a list of symbols, each tied to one of the linked libraries. That list is what the symhash is computed from. Objective-C adds a twist: most of its calls go through a single import, objc_msgSend. The interesting information is the selector string passed to it, which is why a disassembler that names selectors is so useful.
__LINKEDIT ties it together
__LINKEDIT holds no code. Its contents are referenced by the load commands:
- the symbol and string tables;
- the indirect-symbol table that maps stubs to imports;
- the export trie;
- the function-start list;
- the fixups;
- last of all, the code signature.
The signature is a "SuperBlob" of several parts:
- a CodeDirectory, which holds a hash of every page of the file up to the signature;
- the requirements;
- the entitlements;
- for a real identity, a CMS signature made with an Apple-issued certificate.
How those pieces are checked is the subject of macOS code signatures: what you can verify offline.
A triage checklist
Read in this order, a Mach-O file answers most first questions in a minute:
- What is it? The file type, the architectures, and whether it is universal.
- Who signed it? Unsigned, ad hoc, Developer ID or Apple. Does the signature still match the file?
- What may it do? The entitlements and the hardened runtime.
- What does it load? The linked libraries, weak links,
@rpathsearch paths and dyld environment variables. - What does it look able to do? Imports, strings, and the code itself.
- Is it hiding something? Segment protections, entropy, an embedded Mach-O, FairPlay or UPX.
Mach-O Parser reads all of this in the browser, with every slice of a universal binary and nothing uploaded, and explains each finding in plain language.
Frequently asked questions
What is the difference between a Mach-O file and a .app?
A .app is a folder (a bundle). The program itself is the Mach-O file in Contents/MacOS, and the frameworks, plug-ins and helper tools inside the bundle are Mach-O files too. Each one has its own header, load commands and code signature.
How do I recognise a Mach-O file from its first bytes?
CF FA ED FE is a 64-bit Mach-O and CE FA ED FE a 32-bit one (the magic 0xFEEDFACF / 0xFEEDFACE stored little-endian). CA FE BA BE is a universal binary, but Java class files start with the same four bytes. In a universal binary the next field is a small slice count, while in a class file it is a version number.
Can I read Mach-O files on Windows or Linux?
Yes. LLVM ships llvm-otool and llvm-objdump --macho, and Ghidra, IDA, radare2 and LIEF all parse the format. Mach-O Parser runs in any browser and also re-verifies the code signature, which most cross-platform tools only display.