Introduction
Mach-O, short for Mach object, is the format of programs, libraries and object files on Apple’s macOS, iOS, iPadOS, watchOS and tvOS. It came from the Mach kernel through NeXTSTEP, which became Mac OS X.
Mach-O programs have no extension: the program inside an app bundle, such as
Safari.app/Contents/MacOS/Safari, is a Mach-O file. The extensions that do
appear are .dylib for dynamic libraries, .bundle for plug-ins
and .o for object files. A universal binary wraps several Mach-O files, one
per processor, such as one for Intel and one for Apple silicon, in a single file.
Structure
A Mach-O file starts with a header: a magic number that gives the word size and byte
order (0xFEEDFACE for 32-bit, 0xFEEDFACF for 64-bit), the
processor type, the file type (1 object, 2 executable, 6 dynamic library, 8 bundle and
others), and the number and total size of the load commands that follow. The header is
28 bytes for 32-bit files and 32 bytes for 64-bit ones.
Load commands describe everything else: the segments to map into memory and the sections inside them, the symbol table, the libraries to link, the entry point, the code signature and more. Each command starts with its type and size, so a reader can skip the ones it does not know.
A universal binary starts instead with the big-endian magic CA FE BA BE,
a count of architectures, and one 20-byte entry per architecture giving the processor
type and the offset and size of that architecture’s Mach-O file inside it.
0header: magic, CPU type, file type, load command count and size, flags28 or 32 bytes28 or 32load commands, each a type and size, then its fieldssizeofcmds bytes…segment data:__TEXTcode,__DATA,__LINKEDITsymbols and signaturesvaries
Each page in the menu takes one part of the format byte by byte. Start with the Mach header and the load commands, which say where everything else is. A universal binary has its own page.
The code
Each code section is shown as a disassembly, from every section with the instructions attribute. x86 and x86-64 code is decoded at every byte, as for .EXE files, so that instructions overlapping the plain sweep can still be read. ARM64 and arm64e code is decoded four bytes at a time, because every instruction is 4 bytes long and starts on a multiple of 4.
A call or a branch shows the name of its target: from the symbol table, the exports, function starts, and the stubs and GOT for imported functions. Each architecture of a universal binary opens as a separate file.
ARM64_32 (the 32-bit-pointer form of 64-bit ARM, used on Apple Watch) uses the same instructions as arm64 and is decoded the same way. 32-bit ARM and PowerPC code is left as bytes.
In an object file (.o) the code is not final: a call to another function
holds a placeholder, and the section’s relocation entries say which symbol the linker
will put there. The explorer shows that symbol (bl _foo, with the
relocation type in the note) instead of the address the placeholder looks like. The
box above the code takes a symbol name as well as a hex address.
Not described
Known, not yet covered
- Viewer: 32-bit ARM (armv6, armv7, armv7s, armv7k) and PowerPC code is shown as bytes. A 32-bit ARM decoder is being written for the site; Mach-O can use it once it is finished, telling Thumb from ARM by the Thumb flag in each symbol’s description (
N_ARM_THUMB_DEF). - Viewer: the dyld shared cache (magic
dyld_v1) is not a Mach-O file and is not opened; its layout is in Apple’s dyld source. - Viewer: the legacy Objective-C 1 runtime in the
__OBJCsegment of 32-bit Intel programs. - Viewer: DWARF in
__DWARFsections (object files and dSYM files) and the call frame information in__TEXT,__eh_frame. - Reference, Viewer: the Objective-C instance variable lists, property lists and protocol method lists (the class and method lists are decoded; Objective-C and Swift), and the rest of Swift’s metadata (generic signatures, protocol requirements, opaque types, associated types, accessors). The Objective-C runtime source and the Swift ABI documents describe them.
- Reference, Viewer: the data of
LC_NOTEcommands by owner (core file notes such as the address-bits note are described in LLDB’s source); the explorer shows the owner and the bytes. - Viewer: the early
BIND_OPCODE_THREADEDarm64e binding of the dyld information, which dyld’s source describes.
Unknown
- Reference, Viewer:
LC_DYLIB_CODE_SIGN_DRSandLC_ATOM_INFO: no published source describes their layouts (Apple’s headers and dyld’s source only name them) and none of the files checked on this Mac has them, so they are shown as bytes (the other tables). - Reference, Viewer:
LC_LAZY_LOAD_DYLIB_INFOis read from dyld’s header alone: none of the files checked has it, so that reading is not tested against a real one. - Reference, Viewer: the long-obsolete commands (
LC_SYMSEG,LC_IDENT,LC_FVMFILE,LC_PREPAGE,LC_LOADFVMLIB,LC_IDFVMLIB,LC_PREBOUND_DYLIBand the like) followloader.h, but none of the files checked on this Mac has them.
Also used for
- Executable and Linkable Format – Programs, libraries and object files on Linux and Unix systems.
Links
Software
- macOS – Apple’s systems run Mach-O programs directly (macOS, iOS)
- Xcode – Apple’s development environment, with
otool,nmandlipo(macOS) - LLVM – compiler toolkit with
llvm-objdumpandllvm-otool(macOS, Linux, Windows) - Hopper – disassembler and decompiler for Mac programs (macOS, Linux)
- Ghidra – the NSA’s free reverse engineering suite (Windows, macOS, Linux)
- IDA Pro – commercial disassembler and decompiler (Windows, macOS, Linux)
- Binary Ninja – commercial reverse engineering platform (Windows, macOS, Linux)
Developers
Reference
- mach-o/loader.h – Apple’s header for the Mach header and load commands
- dyld – the dynamic linker’s source, including chained fixups and the export trie
- cctools – Apple’s otool, nm, lipo and related tools