Revert "System: Add SwapByteOrder and update Support/MathExtras.h to use it."
[oota-llvm.git] / include / llvm / Support / MachO.h
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/System/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       
114       // Constant bits for the "flags" field in llvm::MachO::segment_command
115       SegmentCommandFlagBitHighVM             = 0x1u, // SG_HIGHVM
116       SegmentCommandFlagBitFixedVMLibrary     = 0x2u, // SG_FVMLIB
117       SegmentCommandFlagBitNoRelocations      = 0x4u, // SG_NORELOC
118       SegmentCommandFlagBitProtectedVersion1  = 0x8u, // SG_PROTECTED_VERSION_1
119
120
121       // Constant masks for the "flags" field in llvm::MachO::section and
122       // llvm::MachO::section_64
123       SectionFlagMaskSectionType      = 0x000000ffu, // SECTION_TYPE
124       SectionFlagMaskAllAttributes    = 0xffffff00u, // SECTION_ATTRIBUTES
125       SectionFlagMaskUserAttributes   = 0xff000000u, // SECTION_ATTRIBUTES_USR
126       SectionFlagMaskSystemAttributes = 0x00ffff00u, // SECTION_ATTRIBUTES_SYS
127
128       // Constant masks for the "flags[7:0]" field in llvm::MachO::section and
129       // llvm::MachO::section_64 (mask "flags" with SECTION_TYPE)
130       SectionTypeRegular                    = 0x00u, // S_REGULAR
131       SectionTypeZeroFill                   = 0x01u, // S_ZEROFILL
132       SectionTypeCStringLiterals            = 0x02u, // S_CSTRING_LITERALS
133       SectionType4ByteLiterals              = 0x03u, // S_4BYTE_LITERALS
134       SectionType8ByteLiterals              = 0x04u, // S_8BYTE_LITERALS
135       SectionTypeLiteralPointers            = 0x05u, // S_LITERAL_POINTERS
136       SectionTypeNonLazySymbolPointers      = 0x06u, // S_NON_LAZY_SYMBOL_POINTERS
137       SectionTypeLazySymbolPointers         = 0x07u, // S_LAZY_SYMBOL_POINTERS
138       SectionTypeSymbolStubs                = 0x08u, // S_SYMBOL_STUBS
139       SectionTypeModuleInitFunctionPointers = 0x09u, // S_MOD_INIT_FUNC_POINTERS
140       SectionTypeModuleTermFunctionPointers = 0x0au, // S_MOD_TERM_FUNC_POINTERS
141       SectionTypeCoalesced                  = 0x0bu, // S_COALESCED
142       SectionTypeZeroFillLarge              = 0x0cu, // S_GB_ZEROFILL
143       SectionTypeInterposing                = 0x0du, // S_INTERPOSING
144       SectionType16ByteLiterals             = 0x0eu, // S_16BYTE_LITERALS
145       SectionTypeDTraceObjectFormat         = 0x0fu, // S_DTRACE_DOF
146       SectionTypeLazyDylibSymbolPointers    = 0x10u, // S_LAZY_DYLIB_SYMBOL_POINTERS
147
148       // Constant masks for the "flags[31:24]" field in llvm::MachO::section and
149       // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_USR)
150       SectionAttrUserPureInstructions       = 0x80000000u, // S_ATTR_PURE_INSTRUCTIONS
151       SectionAttrUserNoTableOfContents      = 0x40000000u, // S_ATTR_NO_TOC
152       SectionAttrUserCanStripStaticSymbols  = 0x20000000u, // S_ATTR_STRIP_STATIC_SYMS
153       SectionAttrUserNoDeadStrip            = 0x10000000u, // S_ATTR_NO_DEAD_STRIP
154       SectionAttrUserLiveSupport            = 0x08000000u, // S_ATTR_LIVE_SUPPORT
155       SectionAttrUserSelfModifyingCode      = 0x04000000u, // S_ATTR_SELF_MODIFYING_CODE
156       SectionAttrUserDebug                  = 0x02000000u, // S_ATTR_DEBUG
157
158       // Constant masks for the "flags[23:8]" field in llvm::MachO::section and
159       // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_SYS)
160       SectionAttrSytemSomeInstructions      = 0x00000400u, // S_ATTR_SOME_INSTRUCTIONS
161       SectionAttrSytemHasExternalRelocations= 0x00000200u, // S_ATTR_EXT_RELOC
162       SectionAttrSytemHasLocalRelocations   = 0x00000100u, // S_ATTR_LOC_RELOC
163
164       IndirectSymbolLocal                   = 0x80000000u, // INDIRECT_SYMBOL_LOCAL
165       IndirectSymbolAbsolute                = 0x40000000u, // INDIRECT_SYMBOL_ABS
166
167       RebaseTypePointer                     = 1u, // REBASE_TYPE_POINTER
168       RebaseTypeTextAbsolute32              = 2u, // REBASE_TYPE_TEXT_ABSOLUTE32
169       RebaseTypeTextPCRelative32            = 3u, // REBASE_TYPE_TEXT_PCREL32
170
171       RebaseOpcodeMask                          = 0xF0u, // REBASE_OPCODE_MASK
172       RebaseImmediateMask                       = 0x0Fu, // REBASE_IMMEDIATE_MASK
173       RebaseOpcodeDone                          = 0x00u, // REBASE_OPCODE_DONE
174       RebaseOpcodeSetTypeImmediate              = 0x10u, // REBASE_OPCODE_SET_TYPE_IMM
175       RebaseOpcodeSetSegmentAndOffsetULEB       = 0x20u, // REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
176       RebaseOpcodeAddAddressULEB                = 0x30u, // REBASE_OPCODE_ADD_ADDR_ULEB
177       RebaseOpcodeAddAddressImmediateScaled     = 0x40u, // REBASE_OPCODE_ADD_ADDR_IMM_SCALED
178       RebaseOpcodeDoRebaseImmediateTimes        = 0x50u, // REBASE_OPCODE_DO_REBASE_IMM_TIMES
179       RebaseOpcodeDoRebaseULEBTimes             = 0x60u, // REBASE_OPCODE_DO_REBASE_ULEB_TIMES
180       RebaseOpcodeDoRebaseAddAddressULEB        = 0x70u, // REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB
181       RebaseOpcodeDoRebaseULEBTimesSkippingULEB = 0x80u, // REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB
182
183
184       BindTypePointer           = 1u, // BIND_TYPE_POINTER
185       BindTypeTextAbsolute32    = 2u, // BIND_TYPE_TEXT_ABSOLUTE32
186       BindTypeTextPCRelative32  = 3u, // BIND_TYPE_TEXT_PCREL32
187
188       BindSpecialDylibSelf            =  0u, // BIND_SPECIAL_DYLIB_SELF
189       BindSpecialDylibMainExecutable  = -1u, // BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE
190       BindSpecialDylibFlatLookup      = -2u, // BIND_SPECIAL_DYLIB_FLAT_LOOKUP
191
192       BindSymbolFlagsWeakImport         = 0x1u, // BIND_SYMBOL_FLAGS_WEAK_IMPORT
193       BindSymbolFlagsNonWeakDefinition  = 0x8u, // BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION
194
195       BindOpcodeMask                            = 0xF0u, // BIND_OPCODE_MASK
196       BindImmediateMask                         = 0x0Fu, // BIND_IMMEDIATE_MASK
197       BindOpcodeDone                            = 0x00u, // BIND_OPCODE_DONE
198       BindOpcodeSetDylibOrdinalImmediate        = 0x10u, // BIND_OPCODE_SET_DYLIB_ORDINAL_IMM
199       BindOpcodeSetDylibOrdinalULEB             = 0x20u, // BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB
200       BindOpcodeSetDylibSpecialImmediate        = 0x30u, // BIND_OPCODE_SET_DYLIB_SPECIAL_IMM
201       BindOpcodeSetSymbolTrailingFlagsImmediate = 0x40u, // BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
202       BindOpcodeSetTypeImmediate                = 0x50u, // BIND_OPCODE_SET_TYPE_IMM
203       BindOpcodeSetAppendSLEB                   = 0x60u, // BIND_OPCODE_SET_ADDEND_SLEB
204       BindOpcodeSetSegmentAndOffsetULEB         = 0x70u, // BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
205       BindOpcodeAddAddressULEB                  = 0x80u, // BIND_OPCODE_ADD_ADDR_ULEB
206       BindOpcodeDoBind                          = 0x90u, // BIND_OPCODE_DO_BIND
207       BindOpcodeDoBindAddAddressULEB            = 0xA0u, // BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB
208       BindOpcodeDoBindAddAddressImmediateScaled = 0xB0u, // BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED
209       BindOpcodeDoBindULEBTimesSkippingULEB     = 0xC0u, // BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB
210
211       ExportSymbolFlagsKindMask           = 0x03u, // EXPORT_SYMBOL_FLAGS_KIND_MASK
212       ExportSymbolFlagsKindRegular        = 0x00u, // EXPORT_SYMBOL_FLAGS_KIND_REGULAR
213       ExportSymbolFlagsKindThreadLocal    = 0x01u, // EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL
214       ExportSymbolFlagsWeakDefinition     = 0x04u, // EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION
215       ExportSymbolFlagsIndirectDefinition = 0x08u, // EXPORT_SYMBOL_FLAGS_INDIRECT_DEFINITION
216       ExportSymbolFlagsHasSpecializations = 0x10u, // EXPORT_SYMBOL_FLAGS_HAS_SPECIALIZATIONS
217
218
219       // Constant masks for the "n_type" field in llvm::MachO::nlist and
220       // llvm::MachO::nlist_64
221       NlistMaskStab             = 0xe0, // N_STAB
222       NlistMaskPrivateExternal  = 0x10, // N_PEXT
223       NlistMaskType             = 0x0e, // N_TYPE
224       NlistMaskExternal         = 0x01, // N_EXT
225
226       // Constants for the "n_type & N_TYPE" llvm::MachO::nlist and
227       // llvm::MachO::nlist_64
228       NListTypeUndefined          = 0x0u, // N_UNDF
229       NListTypeAbsolute           = 0x2u, // N_ABS
230       NListTypeSection            = 0xeu, // N_SECT
231       NListTypePreboundUndefined  = 0xcu, // N_PBUD
232       NListTypeIndirect           = 0xau, // N_INDR
233
234       // Constant masks for the "n_sect" field in llvm::MachO::nlist and
235       // llvm::MachO::nlist_64
236       NListSectionNoSection     = 0u, // NO_SECT
237       NListSectionMaxSection    = 0xffu, // MAX_SECT
238
239       // Constant values for the "n_type" field in llvm::MachO::nlist and
240       // llvm::MachO::nlist_64 when "(n_type & NlistMaskStab) != 0"
241       StabGlobalSymbol          = 0x20u,  // N_GSYM     
242       StabFunctionName          = 0x22u,  // N_FNAME    
243       StabFunction              = 0x24u,  // N_FUN      
244       StabStaticSymbol          = 0x26u,  // N_STSYM    
245       StabLocalCommon           = 0x28u,  // N_LCSYM    
246       StabBeginSymbol           = 0x2Eu,  // N_BNSYM   
247       StabSourceFileOptions     = 0x3Cu,  // N_OPT      
248       StabRegisterSymbol        = 0x40u,  // N_RSYM     
249       StabSourceLine            = 0x44u,  // N_SLINE    
250       StabEndSymbol             = 0x4Eu,  // N_ENSYM   
251       StabStructureType         = 0x60u,  // N_SSYM     
252       StabSourceFileName        = 0x64u,  // N_SO       
253       StabObjectFileName        = 0x66u,  // N_OSO      
254       StabLocalSymbol           = 0x80u,  // N_LSYM     
255       StabBeginIncludeFileName  = 0x82u,  // N_BINCL    
256       StabIncludeFileName       = 0x84u,  // N_SOL      
257       StabCompilerParameters    = 0x86u,  // N_PARAMS  
258       StabCompilerVersion       = 0x88u,  // N_VERSION 
259       StabCompilerOptLevel      = 0x8Au,  // N_OLEVEL  
260       StabParameter             = 0xA0u,  // N_PSYM     
261       StabEndIncludeFile        = 0xA2u,  // N_EINCL    
262       StabAlternateEntry        = 0xA4u,  // N_ENTRY    
263       StabLeftBracket           = 0xC0u,  // N_LBRAC    
264       StabDeletedIncludeFile    = 0xC2u,  // N_EXCL     
265       StabRightBracket          = 0xE0u,  // N_RBRAC    
266       StabBeginCommon           = 0xE2u,  // N_BCOMM    
267       StabEndCommon             = 0xE4u,  // N_ECOMM    
268       StabEndCommonLocal        = 0xE8u,  // N_ECOML    
269       StabLength                = 0xFEu   // N_LENG     
270
271     };
272     
273     // Structs from <mach-o/loader.h>
274     
275     struct mach_header {
276       uint32_t magic;
277       uint32_t cputype;
278       uint32_t cpusubtype;
279       uint32_t filetype;
280       uint32_t ncmds;
281       uint32_t sizeofcmds;
282       uint32_t flags;
283     };
284
285     struct mach_header_64 {
286       uint32_t magic;
287       uint32_t cputype;
288       uint32_t cpusubtype;
289       uint32_t filetype;
290       uint32_t ncmds;
291       uint32_t sizeofcmds;
292       uint32_t flags;
293       uint32_t reserved;
294     };
295
296     struct load_command {
297       uint32_t cmd;
298       uint32_t cmdsize;
299     };
300
301     struct segment_command {
302       uint32_t cmd;
303       uint32_t cmdsize;
304       char segname[16];
305       uint32_t vmaddr;
306       uint32_t vmsize;
307       uint32_t fileoff;
308       uint32_t filesize;
309       uint32_t maxprot;
310       uint32_t initprot;
311       uint32_t nsects;
312       uint32_t flags;
313     };
314
315     struct segment_command_64 {
316       uint32_t cmd;
317       uint32_t cmdsize;
318       char segname[16];
319       uint64_t vmaddr;
320       uint64_t vmsize;
321       uint64_t fileoff;
322       uint64_t filesize;
323       uint32_t maxprot;
324       uint32_t initprot;
325       uint32_t nsects;
326       uint32_t flags;
327     };
328
329     struct section {
330       char sectname[16];
331       char segname[16];
332       uint32_t addr;
333       uint32_t size;
334       uint32_t offset;
335       uint32_t align;
336       uint32_t reloff;
337       uint32_t nreloc;
338       uint32_t flags;
339       uint32_t reserved1;
340       uint32_t reserved2;
341     };
342
343     struct section_64 {
344       char sectname[16];
345       char segname[16];
346       uint64_t addr;
347       uint64_t size;
348       uint32_t offset;
349       uint32_t align;
350       uint32_t reloff;
351       uint32_t nreloc;
352       uint32_t flags;
353       uint32_t reserved1;
354       uint32_t reserved2;
355       uint32_t reserved3;
356     };
357
358     struct fvmlib {
359       uint32_t name;
360       uint32_t minor_version;
361       uint32_t header_addr;
362     };
363
364     struct fvmlib_command {
365       uint32_t  cmd;
366       uint32_t cmdsize;
367       struct fvmlib fvmlib;
368     };
369
370     struct dylib {
371       uint32_t name;
372       uint32_t timestamp;
373       uint32_t current_version;
374       uint32_t compatibility_version;
375     };
376
377     struct dylib_command {
378       uint32_t cmd;
379       uint32_t cmdsize;
380       struct dylib dylib;
381     };
382
383     struct sub_framework_command {
384       uint32_t cmd;
385       uint32_t cmdsize;
386       uint32_t umbrella;
387     };
388
389     struct sub_client_command {
390       uint32_t cmd;
391       uint32_t cmdsize;
392       uint32_t client;
393     };
394
395     struct sub_umbrella_command {
396       uint32_t cmd;
397       uint32_t cmdsize;
398       uint32_t sub_umbrella;
399     };
400
401     struct sub_library_command {
402       uint32_t cmd;
403       uint32_t cmdsize;
404       uint32_t sub_library;
405     };
406
407     struct prebound_dylib_command {
408       uint32_t cmd;
409       uint32_t cmdsize;
410       uint32_t name;
411       uint32_t nmodules;
412       uint32_t linked_modules;
413     };
414
415     struct dylinker_command {
416       uint32_t cmd;
417       uint32_t cmdsize;
418       uint32_t name;
419     };
420
421     struct thread_command {
422       uint32_t cmd;
423       uint32_t cmdsize;
424     };
425
426     struct routines_command {
427       uint32_t cmd;
428       uint32_t cmdsize;
429       uint32_t init_address;
430       uint32_t init_module;
431       uint32_t reserved1;
432       uint32_t reserved2;
433       uint32_t reserved3;
434       uint32_t reserved4;
435       uint32_t reserved5;
436       uint32_t reserved6;
437     };
438
439     struct routines_command_64 {
440       uint32_t cmd;
441       uint32_t cmdsize;
442       uint64_t init_address;
443       uint64_t init_module;
444       uint64_t reserved1;
445       uint64_t reserved2;
446       uint64_t reserved3;
447       uint64_t reserved4;
448       uint64_t reserved5;
449       uint64_t reserved6;
450     };
451
452     struct symtab_command {
453       uint32_t cmd;
454       uint32_t cmdsize;
455       uint32_t symoff;
456       uint32_t nsyms;
457       uint32_t stroff;
458       uint32_t strsize;
459     };
460
461     struct dysymtab_command {
462       uint32_t cmd;
463       uint32_t cmdsize;
464       uint32_t ilocalsym;
465       uint32_t nlocalsym;
466       uint32_t iextdefsym;
467       uint32_t nextdefsym;
468       uint32_t iundefsym;
469       uint32_t nundefsym;
470       uint32_t tocoff;
471       uint32_t ntoc;
472       uint32_t modtaboff;
473       uint32_t nmodtab;
474       uint32_t extrefsymoff;
475       uint32_t nextrefsyms;
476       uint32_t indirectsymoff;
477       uint32_t nindirectsyms;
478       uint32_t extreloff;
479       uint32_t nextrel;
480       uint32_t locreloff;
481       uint32_t nlocrel;
482     };  
483
484     struct dylib_table_of_contents {
485       uint32_t symbol_index;
486       uint32_t module_index;
487     };  
488
489     struct dylib_module {
490       uint32_t module_name;
491       uint32_t iextdefsym;
492       uint32_t nextdefsym;
493       uint32_t irefsym;
494       uint32_t nrefsym;
495       uint32_t ilocalsym;
496       uint32_t nlocalsym;
497       uint32_t iextrel;
498       uint32_t nextrel;
499       uint32_t iinit_iterm;
500       uint32_t ninit_nterm;
501       uint32_t objc_module_info_addr;
502       uint32_t objc_module_info_size;
503     };  
504
505     struct dylib_module_64 {
506       uint32_t module_name;
507       uint32_t iextdefsym;
508       uint32_t nextdefsym;
509       uint32_t irefsym;
510       uint32_t nrefsym;
511       uint32_t ilocalsym;
512       uint32_t nlocalsym;
513       uint32_t iextrel;
514       uint32_t nextrel;
515       uint32_t iinit_iterm;
516       uint32_t ninit_nterm;
517       uint32_t objc_module_info_size;
518       uint64_t objc_module_info_addr;
519     };
520
521     struct dylib_reference {
522       uint32_t isym:24,
523                flags:8;
524     };
525
526
527     struct twolevel_hints_command {
528       uint32_t cmd;
529       uint32_t cmdsize;
530       uint32_t offset;
531       uint32_t nhints;
532     };
533
534     struct twolevel_hint {
535       uint32_t isub_image:8,
536                itoc:24;
537     };
538
539     struct prebind_cksum_command {
540       uint32_t cmd;
541       uint32_t cmdsize;
542       uint32_t cksum;
543     };
544
545     struct uuid_command {
546       uint32_t cmd;
547       uint32_t cmdsize;
548       uint8_t uuid[16];
549     };
550
551     struct rpath_command {
552       uint32_t cmd;
553       uint32_t cmdsize;
554       uint32_t path;
555     };
556
557     struct linkedit_data_command {
558       uint32_t cmd;
559       uint32_t cmdsize;
560       uint32_t dataoff;
561       uint32_t datasize;
562     };
563
564     struct encryption_info_command {
565       uint32_t cmd;
566       uint32_t cmdsize;
567       uint32_t cryptoff;
568       uint32_t cryptsize;
569       uint32_t cryptid;
570     };
571
572     struct dyld_info_command {
573       uint32_t cmd;
574       uint32_t cmdsize;
575       uint32_t rebase_off;
576       uint32_t rebase_size;
577       uint32_t bind_off;
578       uint32_t bind_size;
579       uint32_t weak_bind_off;
580       uint32_t weak_bind_size;
581       uint32_t lazy_bind_off;
582       uint32_t lazy_bind_size;
583       uint32_t export_off;
584       uint32_t export_size;
585     };
586
587     struct symseg_command {
588       uint32_t cmd;
589       uint32_t cmdsize;
590       uint32_t offset;
591       uint32_t size;
592     };
593
594     struct ident_command {
595       uint32_t cmd;
596       uint32_t cmdsize;
597     };
598
599     struct fvmfile_command {
600       uint32_t cmd;
601       uint32_t cmdsize;
602       uint32_t name;
603       uint32_t header_addr;
604     };
605
606
607     // Structs from <mach-o/fat.h>
608     struct fat_header {
609       uint32_t magic;
610       uint32_t nfat_arch;
611     };
612
613     struct fat_arch {
614       uint32_t cputype;
615       uint32_t cpusubtype;
616       uint32_t offset;
617       uint32_t size;
618       uint32_t align;
619     };
620
621     // Structs from <mach-o/fat.h>
622     struct nlist {
623       uint32_t n_strx;
624       uint8_t n_type;
625       uint8_t n_sect;
626       int16_t n_desc;
627       uint32_t n_value;
628     };
629
630     struct nlist_64 {
631       uint32_t n_strx;
632       uint8_t n_type;
633       uint8_t n_sect;
634       uint16_t n_desc;
635       uint64_t n_value;
636     };
637
638     // Get/Set functions from <mach-o/nlist.h>
639     
640     static inline uint16_t GET_LIBRARY_ORDINAL(uint16_t n_desc)
641     {
642       return (((n_desc) >> 8u) & 0xffu);
643     }
644       
645     static inline void SET_LIBRARY_ORDINAL(uint16_t &n_desc, uint8_t ordinal)
646     {
647       n_desc = (((n_desc) & 0x00ff) | (((ordinal) & 0xff) << 8));
648     }
649
650     static inline uint8_t GET_COMM_ALIGN (uint16_t n_desc)
651     {
652       return (n_desc >> 8u) & 0x0fu;
653     }
654     
655     static inline void SET_COMM_ALIGN (uint16_t &n_desc, uint8_t align)
656     {
657       n_desc = ((n_desc & 0xf0ffu) | ((align & 0x0fu) << 8u));
658     }
659
660     // Enums from <mach/machine.h>
661     enum {
662       // Capability bits used in the definition of cpu_type.
663       CPUArchMask = 0xff000000,   // Mask for architecture bits
664       CPUArchABI64 = 0x01000000,  // 64 bit ABI
665       
666       // Constants for the cputype field.
667       CPUTypeI386      = 7,
668       CPUTypeX86_64    = CPUTypeI386 | CPUArchABI64,
669       CPUTypeARM       = 12,
670       CPUTypeSPARC     = 14,
671       CPUTypePowerPC   = 18,
672       CPUTypePowerPC64 = CPUTypePowerPC | CPUArchABI64,
673
674
675       // Constants for the cpusubtype field.
676       
677       // X86
678       CPUSubType_I386_ALL    = 3,
679       CPUSubType_X86_64_ALL  = 3,
680       
681       // ARM
682       CPUSubType_ARM_ALL     = 0,
683       CPUSubType_ARM_V4T     = 5,
684       CPUSubType_ARM_V6      = 6,
685
686       // PowerPC
687       CPUSubType_POWERPC_ALL = 0,
688       
689       CPUSubType_SPARC_ALL   = 0
690     };
691   } // end namespace MachO
692 } // end namespace llvm
693
694 #endif