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