#defineMH_MAGIC 0xfeedface /* the mach magic number */
#defineMH_CIGAM 0xcefaedfe /* NXSwapInt(MH_MAGIC) */
#defineMH_MAGIC_64 0xfeedfacf /* the 64-bit mach magic number */
#defineMH_CIGAM_64 0xcffaedfe /* NXSwapInt(MH_MAGIC_64) */
#defineFAT_MAGIC 0xcafebabe /* the fat magic number */
#defineFAT_CIGAM 0xbebafeca /* NXSwapLong(FAT_MAGIC) */
#defineFAT_MAGIC_64 0xcafebabf /* the 64-bit fat magic number */
#defineFAT_CIGAM_64 0xbfbafeca /* NXSwapLong(FAT_MAGIC_64) */
mach_header and load_command
structmach_header: /* struct mach_header_64 for 64-bit */uint32_tmagic; /* mach magic number identifier */cpu_type_tcputype; /* cpu specifier */cpu_subtype_tcpusubtype; /* machine specifier */uint32_tfiletype; /* type of file */uint32_tncmds; /* number of load commands */uint32_tsizeofcmds; /* the size of all the load commands */uint32_tflags; /* flags */_64uint32_treserved; /* reserved */structload_command: /* load_command header */uint32_tcmd; /* type of load command */uint32_tcmdsize; /* total size of command in bytes */
fat_header and fat_arch
structfat_header:
uint32_tmagic; /* FAT_MAGIC or FAT_MAGIC_64 */uint32_tnfat_arch; /* number of structs that follow */structfat_arch: /* struct fat_arch_64 for 64-bit*/cpu_type_tcputype; /* cpu specifier (int) */cpu_subtype_tcpusubtype; /* machine specifier (int) */_32uint32_toffset; /* file offset to this object file */_32uint32_tsize; /* size of this object file */_64uint64_toffset; /* file offset to this object file */_64uint64_tsize; /* size of this object file */uint32_talign; /* alignment as a power of 2 */_64uint32_treserved; /* reserved */
Segments and Sections
segment_command load_command
structsegment_command: /* struct segment_command_64 for 64-bit */uint32_tcmd; /* LC_SEGMENT or LC_SEGMENT_64 */uint32_tcmdsize; /* includes sizeof section structs */charsegname[16]; /* segment name */_32uint32_tvmaddr; /* memory address of this segment */_32uint32_tvmsize; /* memory size of this segment */_32uint32_tfileoff; /* file offset of this segment */_32uint32_tfilesize; /* amount to map from the file */_64uint64_tvmaddr; /* memory address of this segment */_64uint64_tvmsize; /* memory size of this segment */_64uint64_tfileoff; /* file offset of this segment */_64uint64_tfilesize; /* amount to map from the file */vm_prot_tmaxprot; /* maximum VM protection */vm_prot_tinitprot; /* initial VM protection */uint32_tnsects; /* number of sections in segment */uint32_tflags; /* flags */
section contained in a segment
structsection: /* struct section_64 for 64-bit */charsectname[16]; /* name of this section */charsegname[16]; /* segment this section goes in */_64uint64_taddr; /* for 64-bit memory address of this section */_64uint64_tsize; /* for 64-bit size in bytes of this section */_32uint32_taddr; /* for 32-bit memory address of this section */_32uint32_tsize; /* for 32-bit size in bytes of this section */uint32_toffset; /* file offset of this section */uint32_talign; /* section alignment (power of 2) */uint32_treloff; /* file offset of relocation entries */uint32_tnreloc; /* number of relocation entries */uint32_tflags; /* flags (section type and attributes)*/uint32_treserved1; /* reserved (for offset or index) */uint32_treserved2; /* reserved (for count or sizeof) */_64uint32_treserved3; /* reserved */
Symbols Segments
symtab_command load_command
structsymtab_command:
uint32_tcmd; /* LC_SYMTAB */uint32_tcmdsize; /* sizeof(struct symtab_command) */uint32_tsymoff; /* symbol table offset (nlist array) */uint32_tnsyms; /* number of symbol table entries */uint32_tstroff; /* string table offset */uint32_tstrsize; /* string table size in bytes */
Symbol nlist
structnlist: /* struct nlist_64 for 64-bit */unionn_un: /* union with char *n_name for 32-bit */uint32_tn_strx; /* index into the string table (union n_un) */uint8_tn_type; /* type flag, see below */uint8_tn_sect; /* section number or NO_SECT */int16_tn_desc; /* see <mach-o/stab.h> */_32uint32_tn_value; /* value of this symbol (or stab offset) */_64uint64_tn_value; /* value of this symbol (or stab offset) */
Symbol Types
Printed char
U
A
T
D
B
C
-
S
I
W
Symbol Type
undefined
absolute
text section symbol
data section symbol
bss section symbol
common symbol
debugger symbols
symbol in another section
indirect symbol
Weak reference
Symbol Section obtained by checking the nlist.n_sect section's section.sectname (see section picture above)
Additional Symbol Type info are in nlist.n_type
If the symbol is local (non-external == N_EXT bit off), the symbol's type is instead represented by the corresponding lowercase letter
A lower case u in a dynamic shared library indicates a undefined reference to a private external in another module in the same library
Weak references have nlist.n_desc & N_WEAK_REF
/*** The n_type field really contains four fields:** unsigned char [N_STAB:3 | N_PEXT:1 | N_TYPE:3 | N_EXT:1]** which are used via the following masks*/#defineN_STAB 0xe0 /* if any of these bits set, a symbolic debugging entry */
#defineN_PEXT 0x10 /* private external symbol bit */
#defineN_TYPE 0x0e /* mask for the type bits */
#defineN_EXT 0x01 /* external symbol bit, set for external symbols */
/* Values for N_TYPE bits of the n_type field */#defineN_UNDF 0x0 /* undefined, n_sect == NO_SECT */
#defineN_ABS 0x2 /* absolute, n_sect == NO_SECT */
#defineN_SECT 0xe /* defined in section number n_sect */
#defineN_PBUD 0xc /* prebound undefined (defined in a dylib) */
#defineN_INDR 0xa /* indirect */