• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- llvm/Support/MachO.h - The MachO file format ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines manifest constants for the MachO object file format.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_MACHO_H
15 #define LLVM_SUPPORT_MACHO_H
16 
17 #include "llvm/Support/DataTypes.h"
18 
19 // NOTE: The enums in this file are intentially named to be different than those
20 // in the headers in /usr/include/mach (on darwin systems) to avoid conflicts
21 // with those macros.
22 namespace llvm {
23   namespace MachO {
24     // Enums from <mach-o/loader.h>
25     enum {
26       // Constants for the "magic" field in llvm::MachO::mach_header and
27       // llvm::MachO::mach_header_64
28       HeaderMagic32         = 0xFEEDFACEu, // MH_MAGIC
29       HeaderMagic32Swapped  = 0xCEFAEDFEu, // MH_CIGAM
30       HeaderMagic64         = 0xFEEDFACFu, // MH_MAGIC_64
31       HeaderMagic64Swapped  = 0xCFFAEDFEu, // MH_CIGAM_64
32       UniversalMagic        = 0xCAFEBABEu, // FAT_MAGIC
33       UniversalMagicSwapped = 0xBEBAFECAu, // FAT_CIGAM
34 
35       // Constants for the "filetype" field in llvm::MachO::mach_header and
36       // llvm::MachO::mach_header_64
37       HeaderFileTypeObject              = 0x1u, // MH_OBJECT
38       HeaderFileTypeExecutable          = 0x2u, // MH_EXECUTE
39       HeaderFileTypeFixedVMShlib        = 0x3u, // MH_FVMLIB
40       HeaderFileTypeCore                = 0x4u, // MH_CORE
41       HeaderFileTypePreloadedExecutable = 0x5u, // MH_PRELOAD
42       HeaderFileTypeDynamicShlib        = 0x6u, // MH_DYLIB
43       HeaderFileTypeDynamicLinkEditor   = 0x7u, // MH_DYLINKER
44       HeaderFileTypeBundle              = 0x8u, // MH_BUNDLE
45       HeaderFileTypeDynamicShlibStub    = 0x9u, // MH_DYLIB_STUB
46       HeaderFileTypeDSYM                = 0xAu, // MH_DSYM
47       HeaderFileTypeKextBundle          = 0xBu, // MH_KEXT_BUNDLE
48 
49       // Constant bits for the "flags" field in llvm::MachO::mach_header and
50       // llvm::MachO::mach_header_64
51       HeaderFlagBitNoUndefinedSymbols     = 0x00000001u, // MH_NOUNDEFS
52       HeaderFlagBitIsIncrementalLinkObject= 0x00000002u, // MH_INCRLINK
53       HeaderFlagBitIsDynamicLinkObject    = 0x00000004u, // MH_DYLDLINK
54       HeaderFlagBitBindAtLoad             = 0x00000008u, // MH_BINDATLOAD
55       HeaderFlagBitPrebound               = 0x00000010u, // MH_PREBOUND
56       HeaderFlagBitSplitSegments          = 0x00000020u, // MH_SPLIT_SEGS
57       HeaderFlagBitLazyInit               = 0x00000040u, // MH_LAZY_INIT
58       HeaderFlagBitTwoLevelNamespace      = 0x00000080u, // MH_TWOLEVEL
59       HeaderFlagBitForceFlatNamespace     = 0x00000100u, // MH_FORCE_FLAT
60       HeaderFlagBitNoMultipleDefintions   = 0x00000200u, // MH_NOMULTIDEFS
61       HeaderFlagBitNoFixPrebinding        = 0x00000400u, // MH_NOFIXPREBINDING
62       HeaderFlagBitPrebindable            = 0x00000800u, // MH_PREBINDABLE
63       HeaderFlagBitAllModulesBound        = 0x00001000u, // MH_ALLMODSBOUND
64       HeaderFlagBitSubsectionsViaSymbols  = 0x00002000u, // MH_SUBSECTIONS_VIA_SYMBOLS
65       HeaderFlagBitCanonical              = 0x00004000u, // MH_CANONICAL
66       HeaderFlagBitWeakDefines            = 0x00008000u, // MH_WEAK_DEFINES
67       HeaderFlagBitBindsToWeak            = 0x00010000u, // MH_BINDS_TO_WEAK
68       HeaderFlagBitAllowStackExecution    = 0x00020000u, // MH_ALLOW_STACK_EXECUTION
69       HeaderFlagBitRootSafe               = 0x00040000u, // MH_ROOT_SAFE
70       HeaderFlagBitSetUIDSafe             = 0x00080000u, // MH_SETUID_SAFE
71       HeaderFlagBitNoReexportedDylibs     = 0x00100000u, // MH_NO_REEXPORTED_DYLIBS
72       HeaderFlagBitPIE                    = 0x00200000u, // MH_PIE
73       HeaderFlagBitDeadStrippableDylib    = 0x00400000u, // MH_DEAD_STRIPPABLE_DYLIB
74 
75       // Constants for the "cmd" field in llvm::MachO::load_command
76       LoadCommandDynamicLinkerRequired    = 0x80000000u, // LC_REQ_DYLD
77       LoadCommandSegment32                = 0x00000001u, // LC_SEGMENT
78       LoadCommandSymtab                   = 0x00000002u, // LC_SYMTAB
79       LoadCommandSymSeg                   = 0x00000003u, // LC_SYMSEG
80       LoadCommandThread                   = 0x00000004u, // LC_THREAD
81       LoadCommandUnixThread               = 0x00000005u, // LC_UNIXTHREAD
82       LoadCommandFixedVMShlibLoad         = 0x00000006u, // LC_LOADFVMLIB
83       LoadCommandFixedVMShlibIdent        = 0x00000007u, // LC_IDFVMLIB
84       LoadCommandIdent                    = 0x00000008u, // LC_IDENT
85       LoadCommandFixedVMFileInclusion     = 0x00000009u, // LC_FVMFILE
86       LoadCommandPrePage                  = 0x0000000Au, // LC_PREPAGE
87       LoadCommandDynamicSymtabInfo        = 0x0000000Bu, // LC_DYSYMTAB
88       LoadCommandDylibLoad                = 0x0000000Cu, // LC_LOAD_DYLIB
89       LoadCommandDylibIdent               = 0x0000000Du, // LC_ID_DYLIB
90       LoadCommandDynamicLinkerLoad        = 0x0000000Eu, // LC_LOAD_DYLINKER
91       LoadCommandDynamicLinkerIdent       = 0x0000000Fu, // LC_ID_DYLINKER
92       LoadCommandDylibPrebound            = 0x00000010u, // LC_PREBOUND_DYLIB
93       LoadCommandRoutines32               = 0x00000011u, // LC_ROUTINES
94       LoadCommandSubFramework             = 0x00000012u, // LC_SUB_FRAMEWORK
95       LoadCommandSubUmbrella              = 0x00000013u, // LC_SUB_UMBRELLA
96       LoadCommandSubClient                = 0x00000014u, // LC_SUB_CLIENT
97       LoadCommandSubLibrary               = 0x00000015u, // LC_SUB_LIBRARY
98       LoadCommandTwoLevelHints            = 0x00000016u, // LC_TWOLEVEL_HINTS
99       LoadCommandPreBindChecksum          = 0x00000017u, // LC_PREBIND_CKSUM
100       LoadCommandDylibLoadWeak            = 0x80000018u, // LC_LOAD_WEAK_DYLIB
101       LoadCommandSegment64                = 0x00000019u, // LC_SEGMENT_64
102       LoadCommandRoutines64               = 0x0000001Au, // LC_ROUTINES_64
103       LoadCommandUUID                     = 0x0000001Bu, // LC_UUID
104       LoadCommandRunpath                  = 0x8000001Cu, // LC_RPATH
105       LoadCommandCodeSignature            = 0x0000001Du, // LC_CODE_SIGNATURE
106       LoadCommandSegmentSplitInfo         = 0x0000001Eu, // LC_SEGMENT_SPLIT_INFO
107       LoadCommandDylibReexport            = 0x8000001Fu, // LC_REEXPORT_DYLIB
108       LoadCommandDylibLazyLoad            = 0x00000020u, // LC_LAZY_LOAD_DYLIB
109       LoadCommandEncryptionInfo           = 0x00000021u, // LC_ENCRYPTION_INFO
110       LoadCommandDynamicLinkerInfo        = 0x00000022u, // LC_DYLD_INFO
111       LoadCommandDynamicLinkerInfoOnly    = 0x80000022u, // LC_DYLD_INFO_ONLY
112       LoadCommandDylibLoadUpward          = 0x80000023u, // LC_LOAD_UPWARD_DYLIB
113       LoadCommandVersionMinMacOSX         = 0x00000024u, // LC_VERSION_MIN_MACOSX
114       LoadCommandVersionMinIPhoneOS       = 0x00000025u, // LC_VERSION_MIN_IPHONEOS
115       LoadCommandFunctionStarts           = 0x00000026u, // LC_FUNCTION_STARTS
116       LoadCommandDyldEnvironment          = 0x00000027u, // LC_DYLD_ENVIRONMENT
117       LoadCommandMain                     = 0x80000028u, // LC_MAIN
118       LoadCommandDataInCode               = 0x00000029u, // LC_DATA_IN_CODE
119       LoadCommandSourceVersion            = 0x0000002Au, // LC_SOURCE_VERSION
120       LoadCommandCodeSignDRs              = 0x0000002Bu, // LC_DYLIB_CODE_SIGN_DRS
121 
122       // Constant bits for the "flags" field in llvm::MachO::segment_command
123       SegmentCommandFlagBitHighVM             = 0x1u, // SG_HIGHVM
124       SegmentCommandFlagBitFixedVMLibrary     = 0x2u, // SG_FVMLIB
125       SegmentCommandFlagBitNoRelocations      = 0x4u, // SG_NORELOC
126       SegmentCommandFlagBitProtectedVersion1  = 0x8u, // SG_PROTECTED_VERSION_1
127 
128 
129       // Constant masks for the "flags" field in llvm::MachO::section and
130       // llvm::MachO::section_64
131       SectionFlagMaskSectionType      = 0x000000ffu, // SECTION_TYPE
132       SectionFlagMaskAllAttributes    = 0xffffff00u, // SECTION_ATTRIBUTES
133       SectionFlagMaskUserAttributes   = 0xff000000u, // SECTION_ATTRIBUTES_USR
134       SectionFlagMaskSystemAttributes = 0x00ffff00u, // SECTION_ATTRIBUTES_SYS
135 
136       // Constant masks for the "flags[7:0]" field in llvm::MachO::section and
137       // llvm::MachO::section_64 (mask "flags" with SECTION_TYPE)
138       SectionTypeRegular                    = 0x00u, // S_REGULAR
139       SectionTypeZeroFill                   = 0x01u, // S_ZEROFILL
140       SectionTypeCStringLiterals            = 0x02u, // S_CSTRING_LITERALS
141       SectionType4ByteLiterals              = 0x03u, // S_4BYTE_LITERALS
142       SectionType8ByteLiterals              = 0x04u, // S_8BYTE_LITERALS
143       SectionTypeLiteralPointers            = 0x05u, // S_LITERAL_POINTERS
144       SectionTypeNonLazySymbolPointers      = 0x06u, // S_NON_LAZY_SYMBOL_POINTERS
145       SectionTypeLazySymbolPointers         = 0x07u, // S_LAZY_SYMBOL_POINTERS
146       SectionTypeSymbolStubs                = 0x08u, // S_SYMBOL_STUBS
147       SectionTypeModuleInitFunctionPointers = 0x09u, // S_MOD_INIT_FUNC_POINTERS
148       SectionTypeModuleTermFunctionPointers = 0x0au, // S_MOD_TERM_FUNC_POINTERS
149       SectionTypeCoalesced                  = 0x0bu, // S_COALESCED
150       SectionTypeZeroFillLarge              = 0x0cu, // S_GB_ZEROFILL
151       SectionTypeInterposing                = 0x0du, // S_INTERPOSING
152       SectionType16ByteLiterals             = 0x0eu, // S_16BYTE_LITERALS
153       SectionTypeDTraceObjectFormat         = 0x0fu, // S_DTRACE_DOF
154       SectionTypeLazyDylibSymbolPointers    = 0x10u, // S_LAZY_DYLIB_SYMBOL_POINTERS
155 
156       // Constant masks for the "flags[31:24]" field in llvm::MachO::section and
157       // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_USR)
158       SectionAttrUserPureInstructions       = 0x80000000u, // S_ATTR_PURE_INSTRUCTIONS
159       SectionAttrUserNoTableOfContents      = 0x40000000u, // S_ATTR_NO_TOC
160       SectionAttrUserCanStripStaticSymbols  = 0x20000000u, // S_ATTR_STRIP_STATIC_SYMS
161       SectionAttrUserNoDeadStrip            = 0x10000000u, // S_ATTR_NO_DEAD_STRIP
162       SectionAttrUserLiveSupport            = 0x08000000u, // S_ATTR_LIVE_SUPPORT
163       SectionAttrUserSelfModifyingCode      = 0x04000000u, // S_ATTR_SELF_MODIFYING_CODE
164       SectionAttrUserDebug                  = 0x02000000u, // S_ATTR_DEBUG
165 
166       // Constant masks for the "flags[23:8]" field in llvm::MachO::section and
167       // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_SYS)
168       SectionAttrSytemSomeInstructions      = 0x00000400u, // S_ATTR_SOME_INSTRUCTIONS
169       SectionAttrSytemHasExternalRelocations= 0x00000200u, // S_ATTR_EXT_RELOC
170       SectionAttrSytemHasLocalRelocations   = 0x00000100u, // S_ATTR_LOC_RELOC
171 
172       IndirectSymbolLocal                   = 0x80000000u, // INDIRECT_SYMBOL_LOCAL
173       IndirectSymbolAbsolute                = 0x40000000u, // INDIRECT_SYMBOL_ABS
174 
175       RebaseTypePointer                     = 1u, // REBASE_TYPE_POINTER
176       RebaseTypeTextAbsolute32              = 2u, // REBASE_TYPE_TEXT_ABSOLUTE32
177       RebaseTypeTextPCRelative32	    = 3u, // REBASE_TYPE_TEXT_PCREL32
178 
179       RebaseOpcodeMask                          = 0xF0u, // REBASE_OPCODE_MASK
180       RebaseImmediateMask                       = 0x0Fu, // REBASE_IMMEDIATE_MASK
181       RebaseOpcodeDone                          = 0x00u, // REBASE_OPCODE_DONE
182       RebaseOpcodeSetTypeImmediate              = 0x10u, // REBASE_OPCODE_SET_TYPE_IMM
183       RebaseOpcodeSetSegmentAndOffsetULEB	= 0x20u, // REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
184       RebaseOpcodeAddAddressULEB                = 0x30u, // REBASE_OPCODE_ADD_ADDR_ULEB
185       RebaseOpcodeAddAddressImmediateScaled	= 0x40u, // REBASE_OPCODE_ADD_ADDR_IMM_SCALED
186       RebaseOpcodeDoRebaseImmediateTimes	= 0x50u, // REBASE_OPCODE_DO_REBASE_IMM_TIMES
187       RebaseOpcodeDoRebaseULEBTimes             = 0x60u, // REBASE_OPCODE_DO_REBASE_ULEB_TIMES
188       RebaseOpcodeDoRebaseAddAddressULEB        = 0x70u, // REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB
189       RebaseOpcodeDoRebaseULEBTimesSkippingULEB = 0x80u, // REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB
190 
191 
192       BindTypePointer           = 1u, // BIND_TYPE_POINTER
193       BindTypeTextAbsolute32	= 2u, // BIND_TYPE_TEXT_ABSOLUTE32
194       BindTypeTextPCRelative32	= 3u, // BIND_TYPE_TEXT_PCREL32
195 
196       BindSpecialDylibSelf            =  0u, // BIND_SPECIAL_DYLIB_SELF
197       BindSpecialDylibMainExecutable  = -1u, // BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE
198       BindSpecialDylibFlatLookup      = -2u, // BIND_SPECIAL_DYLIB_FLAT_LOOKUP
199 
200       BindSymbolFlagsWeakImport         = 0x1u, // BIND_SYMBOL_FLAGS_WEAK_IMPORT
201       BindSymbolFlagsNonWeakDefinition	= 0x8u, // BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION
202 
203       BindOpcodeMask                            = 0xF0u, // BIND_OPCODE_MASK
204       BindImmediateMask                         = 0x0Fu, // BIND_IMMEDIATE_MASK
205       BindOpcodeDone                            = 0x00u, // BIND_OPCODE_DONE
206       BindOpcodeSetDylibOrdinalImmediate        = 0x10u, // BIND_OPCODE_SET_DYLIB_ORDINAL_IMM
207       BindOpcodeSetDylibOrdinalULEB             = 0x20u, // BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB
208       BindOpcodeSetDylibSpecialImmediate	= 0x30u, // BIND_OPCODE_SET_DYLIB_SPECIAL_IMM
209       BindOpcodeSetSymbolTrailingFlagsImmediate	= 0x40u, // BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
210       BindOpcodeSetTypeImmediate		= 0x50u, // BIND_OPCODE_SET_TYPE_IMM
211       BindOpcodeSetAppendSLEB                   = 0x60u, // BIND_OPCODE_SET_ADDEND_SLEB
212       BindOpcodeSetSegmentAndOffsetULEB         = 0x70u, // BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
213       BindOpcodeAddAddressULEB                  = 0x80u, // BIND_OPCODE_ADD_ADDR_ULEB
214       BindOpcodeDoBind                          = 0x90u, // BIND_OPCODE_DO_BIND
215       BindOpcodeDoBindAddAddressULEB		= 0xA0u, // BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB
216       BindOpcodeDoBindAddAddressImmediateScaled	= 0xB0u, // BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED
217       BindOpcodeDoBindULEBTimesSkippingULEB     = 0xC0u, // BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB
218 
219       ExportSymbolFlagsKindMask           = 0x03u, // EXPORT_SYMBOL_FLAGS_KIND_MASK
220       ExportSymbolFlagsKindRegular	  = 0x00u, // EXPORT_SYMBOL_FLAGS_KIND_REGULAR
221       ExportSymbolFlagsKindThreadLocal    = 0x01u, // EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL
222       ExportSymbolFlagsWeakDefinition     = 0x04u, // EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION
223       ExportSymbolFlagsIndirectDefinition = 0x08u, // EXPORT_SYMBOL_FLAGS_INDIRECT_DEFINITION
224       ExportSymbolFlagsHasSpecializations = 0x10u, // EXPORT_SYMBOL_FLAGS_HAS_SPECIALIZATIONS
225 
226 
227       // Constant masks for the "n_type" field in llvm::MachO::nlist and
228       // llvm::MachO::nlist_64
229       NlistMaskStab             = 0xe0, // N_STAB
230       NlistMaskPrivateExternal	= 0x10, // N_PEXT
231       NlistMaskType             = 0x0e, // N_TYPE
232       NlistMaskExternal         = 0x01, // N_EXT
233 
234       // Constants for the "n_type & N_TYPE" llvm::MachO::nlist and
235       // llvm::MachO::nlist_64
236       NListTypeUndefined          = 0x0u, // N_UNDF
237       NListTypeAbsolute           = 0x2u, // N_ABS
238       NListTypeSection            = 0xeu, // N_SECT
239       NListTypePreboundUndefined  = 0xcu, // N_PBUD
240       NListTypeIndirect           = 0xau, // N_INDR
241 
242       // Constant masks for the "n_sect" field in llvm::MachO::nlist and
243       // llvm::MachO::nlist_64
244       NListSectionNoSection     = 0u, // NO_SECT
245       NListSectionMaxSection    = 0xffu, // MAX_SECT
246 
247       NListDescWeakRef          = 0x40u,
248       NListDescWeakDef          = 0x80u,
249 
250       // Constant values for the "n_type" field in llvm::MachO::nlist and
251       // llvm::MachO::nlist_64 when "(n_type & NlistMaskStab) != 0"
252       StabGlobalSymbol          = 0x20u,  // N_GSYM
253       StabFunctionName          = 0x22u,  // N_FNAME
254       StabFunction              = 0x24u,  // N_FUN
255       StabStaticSymbol          = 0x26u,  // N_STSYM
256       StabLocalCommon           = 0x28u,  // N_LCSYM
257       StabBeginSymbol           = 0x2Eu,  // N_BNSYM
258       StabSourceFileOptions     = 0x3Cu,  // N_OPT
259       StabRegisterSymbol        = 0x40u,  // N_RSYM
260       StabSourceLine            = 0x44u,  // N_SLINE
261       StabEndSymbol             = 0x4Eu,  // N_ENSYM
262       StabStructureType         = 0x60u,  // N_SSYM
263       StabSourceFileName        = 0x64u,  // N_SO
264       StabObjectFileName        = 0x66u,  // N_OSO
265       StabLocalSymbol           = 0x80u,  // N_LSYM
266       StabBeginIncludeFileName  = 0x82u,  // N_BINCL
267       StabIncludeFileName       = 0x84u,  // N_SOL
268       StabCompilerParameters    = 0x86u,  // N_PARAMS
269       StabCompilerVersion       = 0x88u,  // N_VERSION
270       StabCompilerOptLevel      = 0x8Au,  // N_OLEVEL
271       StabParameter             = 0xA0u,  // N_PSYM
272       StabEndIncludeFile        = 0xA2u,  // N_EINCL
273       StabAlternateEntry        = 0xA4u,  // N_ENTRY
274       StabLeftBracket           = 0xC0u,  // N_LBRAC
275       StabDeletedIncludeFile    = 0xC2u,  // N_EXCL
276       StabRightBracket          = 0xE0u,  // N_RBRAC
277       StabBeginCommon           = 0xE2u,  // N_BCOMM
278       StabEndCommon             = 0xE4u,  // N_ECOMM
279       StabEndCommonLocal        = 0xE8u,  // N_ECOML
280       StabLength                = 0xFEu   // N_LENG
281 
282     };
283 
284     // Structs from <mach-o/loader.h>
285 
286     struct mach_header {
287       uint32_t magic;
288       uint32_t cputype;
289       uint32_t cpusubtype;
290       uint32_t filetype;
291       uint32_t ncmds;
292       uint32_t sizeofcmds;
293       uint32_t flags;
294     };
295 
296     struct mach_header_64 {
297       uint32_t magic;
298       uint32_t cputype;
299       uint32_t cpusubtype;
300       uint32_t filetype;
301       uint32_t ncmds;
302       uint32_t sizeofcmds;
303       uint32_t flags;
304       uint32_t reserved;
305     };
306 
307     struct load_command {
308       uint32_t cmd;
309       uint32_t cmdsize;
310     };
311 
312     struct segment_command {
313       uint32_t cmd;
314       uint32_t cmdsize;
315       char segname[16];
316       uint32_t vmaddr;
317       uint32_t vmsize;
318       uint32_t fileoff;
319       uint32_t filesize;
320       uint32_t maxprot;
321       uint32_t initprot;
322       uint32_t nsects;
323       uint32_t flags;
324     };
325 
326     struct segment_command_64 {
327       uint32_t cmd;
328       uint32_t cmdsize;
329       char segname[16];
330       uint64_t vmaddr;
331       uint64_t vmsize;
332       uint64_t fileoff;
333       uint64_t filesize;
334       uint32_t maxprot;
335       uint32_t initprot;
336       uint32_t nsects;
337       uint32_t flags;
338     };
339 
340     struct section {
341       char sectname[16];
342       char segname[16];
343       uint32_t addr;
344       uint32_t size;
345       uint32_t offset;
346       uint32_t align;
347       uint32_t reloff;
348       uint32_t nreloc;
349       uint32_t flags;
350       uint32_t reserved1;
351       uint32_t reserved2;
352     };
353 
354     struct section_64 {
355       char sectname[16];
356       char segname[16];
357       uint64_t addr;
358       uint64_t size;
359       uint32_t offset;
360       uint32_t align;
361       uint32_t reloff;
362       uint32_t nreloc;
363       uint32_t flags;
364       uint32_t reserved1;
365       uint32_t reserved2;
366       uint32_t reserved3;
367     };
368 
369     struct fvmlib {
370       uint32_t name;
371       uint32_t minor_version;
372       uint32_t header_addr;
373     };
374 
375     struct fvmlib_command {
376       uint32_t  cmd;
377       uint32_t cmdsize;
378       struct fvmlib fvmlib;
379     };
380 
381     struct dylib {
382       uint32_t name;
383       uint32_t timestamp;
384       uint32_t current_version;
385       uint32_t compatibility_version;
386     };
387 
388     struct dylib_command {
389       uint32_t cmd;
390       uint32_t cmdsize;
391       struct dylib dylib;
392     };
393 
394     struct sub_framework_command {
395       uint32_t cmd;
396       uint32_t cmdsize;
397       uint32_t umbrella;
398     };
399 
400     struct sub_client_command {
401       uint32_t cmd;
402       uint32_t cmdsize;
403       uint32_t client;
404     };
405 
406     struct sub_umbrella_command {
407       uint32_t cmd;
408       uint32_t cmdsize;
409       uint32_t sub_umbrella;
410     };
411 
412     struct sub_library_command {
413       uint32_t cmd;
414       uint32_t cmdsize;
415       uint32_t sub_library;
416     };
417 
418     struct prebound_dylib_command {
419       uint32_t cmd;
420       uint32_t cmdsize;
421       uint32_t name;
422       uint32_t nmodules;
423       uint32_t linked_modules;
424     };
425 
426     struct dylinker_command {
427       uint32_t cmd;
428       uint32_t cmdsize;
429       uint32_t name;
430     };
431 
432     struct thread_command {
433       uint32_t cmd;
434       uint32_t cmdsize;
435     };
436 
437     struct routines_command {
438       uint32_t cmd;
439       uint32_t cmdsize;
440       uint32_t init_address;
441       uint32_t init_module;
442       uint32_t reserved1;
443       uint32_t reserved2;
444       uint32_t reserved3;
445       uint32_t reserved4;
446       uint32_t reserved5;
447       uint32_t reserved6;
448     };
449 
450     struct routines_command_64 {
451       uint32_t cmd;
452       uint32_t cmdsize;
453       uint64_t init_address;
454       uint64_t init_module;
455       uint64_t reserved1;
456       uint64_t reserved2;
457       uint64_t reserved3;
458       uint64_t reserved4;
459       uint64_t reserved5;
460       uint64_t reserved6;
461     };
462 
463     struct symtab_command {
464       uint32_t cmd;
465       uint32_t cmdsize;
466       uint32_t symoff;
467       uint32_t nsyms;
468       uint32_t stroff;
469       uint32_t strsize;
470     };
471 
472     struct dysymtab_command {
473       uint32_t cmd;
474       uint32_t cmdsize;
475       uint32_t ilocalsym;
476       uint32_t nlocalsym;
477       uint32_t iextdefsym;
478       uint32_t nextdefsym;
479       uint32_t iundefsym;
480       uint32_t nundefsym;
481       uint32_t tocoff;
482       uint32_t ntoc;
483       uint32_t modtaboff;
484       uint32_t nmodtab;
485       uint32_t extrefsymoff;
486       uint32_t nextrefsyms;
487       uint32_t indirectsymoff;
488       uint32_t nindirectsyms;
489       uint32_t extreloff;
490       uint32_t nextrel;
491       uint32_t locreloff;
492       uint32_t nlocrel;
493     };
494 
495     struct dylib_table_of_contents {
496       uint32_t symbol_index;
497       uint32_t module_index;
498     };
499 
500     struct dylib_module {
501       uint32_t module_name;
502       uint32_t iextdefsym;
503       uint32_t nextdefsym;
504       uint32_t irefsym;
505       uint32_t nrefsym;
506       uint32_t ilocalsym;
507       uint32_t nlocalsym;
508       uint32_t iextrel;
509       uint32_t nextrel;
510       uint32_t iinit_iterm;
511       uint32_t ninit_nterm;
512       uint32_t objc_module_info_addr;
513       uint32_t objc_module_info_size;
514     };
515 
516     struct dylib_module_64 {
517       uint32_t module_name;
518       uint32_t iextdefsym;
519       uint32_t nextdefsym;
520       uint32_t irefsym;
521       uint32_t nrefsym;
522       uint32_t ilocalsym;
523       uint32_t nlocalsym;
524       uint32_t iextrel;
525       uint32_t nextrel;
526       uint32_t iinit_iterm;
527       uint32_t ninit_nterm;
528       uint32_t objc_module_info_size;
529       uint64_t objc_module_info_addr;
530     };
531 
532     struct dylib_reference {
533       uint32_t isym:24,
534                flags:8;
535     };
536 
537 
538     struct twolevel_hints_command {
539       uint32_t cmd;
540       uint32_t cmdsize;
541       uint32_t offset;
542       uint32_t nhints;
543     };
544 
545     struct twolevel_hint {
546       uint32_t isub_image:8,
547                itoc:24;
548     };
549 
550     struct prebind_cksum_command {
551       uint32_t cmd;
552       uint32_t cmdsize;
553       uint32_t cksum;
554     };
555 
556     struct uuid_command {
557       uint32_t cmd;
558       uint32_t cmdsize;
559       uint8_t uuid[16];
560     };
561 
562     struct rpath_command {
563       uint32_t cmd;
564       uint32_t cmdsize;
565       uint32_t path;
566     };
567 
568     struct linkedit_data_command {
569       uint32_t cmd;
570       uint32_t cmdsize;
571       uint32_t dataoff;
572       uint32_t datasize;
573     };
574 
575     struct encryption_info_command {
576       uint32_t cmd;
577       uint32_t cmdsize;
578       uint32_t cryptoff;
579       uint32_t cryptsize;
580       uint32_t cryptid;
581     };
582 
583     struct version_min_command {
584       uint32_t cmd;
585       uint32_t cmdsize;
586       uint32_t version;
587       uint32_t reserved;
588     };
589 
590     struct dyld_info_command {
591       uint32_t cmd;
592       uint32_t cmdsize;
593       uint32_t rebase_off;
594       uint32_t rebase_size;
595       uint32_t bind_off;
596       uint32_t bind_size;
597       uint32_t weak_bind_off;
598       uint32_t weak_bind_size;
599       uint32_t lazy_bind_off;
600       uint32_t lazy_bind_size;
601       uint32_t export_off;
602       uint32_t export_size;
603     };
604 
605     struct symseg_command {
606       uint32_t cmd;
607       uint32_t cmdsize;
608       uint32_t offset;
609       uint32_t size;
610     };
611 
612     struct ident_command {
613       uint32_t cmd;
614       uint32_t cmdsize;
615     };
616 
617     struct fvmfile_command {
618       uint32_t cmd;
619       uint32_t cmdsize;
620       uint32_t name;
621       uint32_t header_addr;
622     };
623 
624 
625     // Structs from <mach-o/fat.h>
626     struct fat_header {
627       uint32_t magic;
628       uint32_t nfat_arch;
629     };
630 
631     struct fat_arch {
632       uint32_t cputype;
633       uint32_t cpusubtype;
634       uint32_t offset;
635       uint32_t size;
636       uint32_t align;
637     };
638 
639     // Structs from <mach-o/fat.h>
640     struct nlist {
641       uint32_t n_strx;
642       uint8_t n_type;
643       uint8_t n_sect;
644       int16_t n_desc;
645       uint32_t n_value;
646     };
647 
648     struct nlist_64 {
649       uint32_t n_strx;
650       uint8_t n_type;
651       uint8_t n_sect;
652       uint16_t n_desc;
653       uint64_t n_value;
654     };
655 
656     // Get/Set functions from <mach-o/nlist.h>
657 
GET_LIBRARY_ORDINAL(uint16_t n_desc)658     static inline uint16_t GET_LIBRARY_ORDINAL(uint16_t n_desc)
659     {
660       return (((n_desc) >> 8u) & 0xffu);
661     }
662 
SET_LIBRARY_ORDINAL(uint16_t & n_desc,uint8_t ordinal)663     static inline void SET_LIBRARY_ORDINAL(uint16_t &n_desc, uint8_t ordinal)
664     {
665       n_desc = (((n_desc) & 0x00ff) | (((ordinal) & 0xff) << 8));
666     }
667 
GET_COMM_ALIGN(uint16_t n_desc)668     static inline uint8_t GET_COMM_ALIGN (uint16_t n_desc)
669     {
670       return (n_desc >> 8u) & 0x0fu;
671     }
672 
SET_COMM_ALIGN(uint16_t & n_desc,uint8_t align)673     static inline void SET_COMM_ALIGN (uint16_t &n_desc, uint8_t align)
674     {
675       n_desc = ((n_desc & 0xf0ffu) | ((align & 0x0fu) << 8u));
676     }
677 
678     // Enums from <mach/machine.h>
679     enum {
680       // Capability bits used in the definition of cpu_type.
681       CPUArchMask = 0xff000000,   // Mask for architecture bits
682       CPUArchABI64 = 0x01000000,  // 64 bit ABI
683 
684       // Constants for the cputype field.
685       CPUTypeI386      = 7,
686       CPUTypeX86_64    = CPUTypeI386 | CPUArchABI64,
687       CPUTypeARM       = 12,
688       CPUTypeSPARC     = 14,
689       CPUTypePowerPC   = 18,
690       CPUTypePowerPC64 = CPUTypePowerPC | CPUArchABI64,
691 
692 
693       // Constants for the cpusubtype field.
694 
695       // X86
696       CPUSubType_I386_ALL    = 3,
697       CPUSubType_X86_64_ALL  = 3,
698 
699       // ARM
700       CPUSubType_ARM_ALL     = 0,
701       CPUSubType_ARM_V4T     = 5,
702       CPUSubType_ARM_V5      = 7,
703       CPUSubType_ARM_V6      = 6,
704       CPUSubType_ARM_V7      = 9,
705 
706       // PowerPC
707       CPUSubType_POWERPC_ALL = 0,
708 
709       CPUSubType_SPARC_ALL   = 0
710     };
711   } // end namespace MachO
712 } // end namespace llvm
713 
714 #endif
715