天天看點

mach header definations and mach_loader.c

00001 //===-- llvm/Support/MachO.h - The MachO file format ------------*- C++ -*-===//



00002 //



00003 //                     The LLVM Compiler Infrastructure



00004 //



00005 // This file is distributed under the University of Illinois Open Source



00006 // License. See LICENSE.TXT for details.



00007 //



00008 //===----------------------------------------------------------------------===//



00009 //



00010 // This file defines manifest constants for the MachO object file format.



00011 //



00012 //===----------------------------------------------------------------------===//



00013 


00014 #ifndef LLVM_SUPPORT_MACHO_H



00015 #define LLVM_SUPPORT_MACHO_H



00016 


00017 #include "llvm/Support/DataTypes.h"



00018 


00019 // NOTE: The enums in this file are intentially named to be different than those



00020 // in the headers in /usr/include/mach (on darwin systems) to avoid conflicts



00021 // with those macros.



00022 namespace 
llvm {


00023
   namespace 
MachO {


00024     // Enums from <mach-o/loader.h>



00025     enum
 {


00026       // Constants for the "magic" field in llvm::MachO::mach_header and 



00027       // llvm::MachO::mach_header_64



00028
       HeaderMagic32
         = 0xFEEDFACEu, // MH_MAGIC



00029
       HeaderMagic32Swapped
  = 0xCEFAEDFEu, // MH_CIGAM



00030
       HeaderMagic64
         = 0xFEEDFACFu, // MH_MAGIC_64



00031
       HeaderMagic64Swapped
  = 0xCFFAEDFEu, // MH_CIGAM_64



00032
       UniversalMagic
        = 0xCAFEBABEu, // FAT_MAGIC



00033
       UniversalMagicSwapped
 = 0xBEBAFECAu, // FAT_CIGAM



00034 


00035       // Constants for the "filetype" field in llvm::MachO::mach_header and



00036       // llvm::MachO::mach_header_64



00037
       HeaderFileTypeObject
              = 0x1u, // MH_OBJECT



00038
       HeaderFileTypeExecutable
          = 0x2u, // MH_EXECUTE



00039
       HeaderFileTypeFixedVMShlib
        = 0x3u, // MH_FVMLIB



00040
       HeaderFileTypeCore
                = 0x4u, // MH_CORE



00041
       HeaderFileTypePreloadedExecutable
 = 0x5u, // MH_PRELOAD



00042
       HeaderFileTypeDynamicShlib
        = 0x6u, // MH_DYLIB



00043
       HeaderFileTypeDynamicLinkEditor
   = 0x7u, // MH_DYLINKER



00044
       HeaderFileTypeBundle
              = 0x8u, // MH_BUNDLE



00045
       HeaderFileTypeDynamicShlibStub
    = 0x9u, // MH_DYLIB_STUB



00046
       HeaderFileTypeDSYM
                = 0xAu, // MH_DSYM



00047
       HeaderFileTypeKextBundle
          = 0xBu, // MH_KEXT_BUNDLE



00048 


00049       // Constant bits for the "flags" field in llvm::MachO::mach_header and



00050       // llvm::MachO::mach_header_64



00051
       HeaderFlagBitNoUndefinedSymbols
     = 0x00000001u, // MH_NOUNDEFS



00052
       HeaderFlagBitIsIncrementalLinkObject
= 0x00000002u, // MH_INCRLINK



00053
       HeaderFlagBitIsDynamicLinkObject
    = 0x00000004u, // MH_DYLDLINK



00054
       HeaderFlagBitBindAtLoad
             = 0x00000008u, // MH_BINDATLOAD



00055
       HeaderFlagBitPrebound
               = 0x00000010u, // MH_PREBOUND



00056
       HeaderFlagBitSplitSegments
          = 0x00000020u, // MH_SPLIT_SEGS



00057
       HeaderFlagBitLazyInit
               = 0x00000040u, // MH_LAZY_INIT



00058
       HeaderFlagBitTwoLevelNamespace
      = 0x00000080u, // MH_TWOLEVEL



00059
       HeaderFlagBitForceFlatNamespace
     = 0x00000100u, // MH_FORCE_FLAT



00060
       HeaderFlagBitNoMultipleDefintions
   = 0x00000200u, // MH_NOMULTIDEFS



00061
       HeaderFlagBitNoFixPrebinding
        = 0x00000400u, // MH_NOFIXPREBINDING



00062
       HeaderFlagBitPrebindable
            = 0x00000800u, // MH_PREBINDABLE



00063
       HeaderFlagBitAllModulesBound
        = 0x00001000u, // MH_ALLMODSBOUND



00064
       HeaderFlagBitSubsectionsViaSymbols
  = 0x00002000u, // MH_SUBSECTIONS_VIA_SYMBOLS



00065
       HeaderFlagBitCanonical
              = 0x00004000u, // MH_CANONICAL



00066
       HeaderFlagBitWeakDefines
            = 0x00008000u, // MH_WEAK_DEFINES



00067
       HeaderFlagBitBindsToWeak
            = 0x00010000u, // MH_BINDS_TO_WEAK



00068
       HeaderFlagBitAllowStackExecution
    = 0x00020000u, // MH_ALLOW_STACK_EXECUTION



00069
       HeaderFlagBitRootSafe
               = 0x00040000u, // MH_ROOT_SAFE



00070
       HeaderFlagBitSetUIDSafe
             = 0x00080000u, // MH_SETUID_SAFE



00071
       HeaderFlagBitNoReexportedDylibs
     = 0x00100000u, // MH_NO_REEXPORTED_DYLIBS



00072
       HeaderFlagBitPIE
                    = 0x00200000u, // MH_PIE



00073
       HeaderFlagBitDeadStrippableDylib
    = 0x00400000u, // MH_DEAD_STRIPPABLE_DYLIB



00074       


00075       // Constants for the "cmd" field in llvm::MachO::load_command



00076
       LoadCommandDynamicLinkerRequired
    = 0x80000000u, // LC_REQ_DYLD



00077
       LoadCommandSegment32
                = 0x00000001u, // LC_SEGMENT



00078
       LoadCommandSymtab
                   = 0x00000002u, // LC_SYMTAB



00079
       LoadCommandSymSeg
                   = 0x00000003u, // LC_SYMSEG



00080
       LoadCommandThread
                   = 0x00000004u, // LC_THREAD



00081
       LoadCommandUnixThread
               = 0x00000005u, // LC_UNIXTHREAD



00082
       LoadCommandFixedVMShlibLoad
         = 0x00000006u, // LC_LOADFVMLIB



00083
       LoadCommandFixedVMShlibIdent
        = 0x00000007u, // LC_IDFVMLIB



00084
       LoadCommandIdent
                    = 0x00000008u, // LC_IDENT



00085
       LoadCommandFixedVMFileInclusion
     = 0x00000009u, // LC_FVMFILE



00086
       LoadCommandPrePage
                  = 0x0000000Au, // LC_PREPAGE



00087
       LoadCommandDynamicSymtabInfo
        = 0x0000000Bu, // LC_DYSYMTAB



00088
       LoadCommandDylibLoad
                = 0x0000000Cu, // LC_LOAD_DYLIB



00089
       LoadCommandDylibIdent
               = 0x0000000Du, // LC_ID_DYLIB



00090
       LoadCommandDynamicLinkerLoad
        = 0x0000000Eu, // LC_LOAD_DYLINKER



00091
       LoadCommandDynamicLinkerIdent
       = 0x0000000Fu, // LC_ID_DYLINKER



00092
       LoadCommandDylibPrebound
            = 0x00000010u, // LC_PREBOUND_DYLIB



00093
       LoadCommandRoutines32
               = 0x00000011u, // LC_ROUTINES



00094
       LoadCommandSubFramework
             = 0x00000012u, // LC_SUB_FRAMEWORK



00095
       LoadCommandSubUmbrella
              = 0x00000013u, // LC_SUB_UMBRELLA



00096
       LoadCommandSubClient
                = 0x00000014u, // LC_SUB_CLIENT



00097
       LoadCommandSubLibrary
               = 0x00000015u, // LC_SUB_LIBRARY



00098
       LoadCommandTwoLevelHints
            = 0x00000016u, // LC_TWOLEVEL_HINTS



00099
       LoadCommandPreBindChecksum
          = 0x00000017u, // LC_PREBIND_CKSUM



00100
       LoadCommandDylibLoadWeak
            = 0x80000018u, // LC_LOAD_WEAK_DYLIB



00101
       LoadCommandSegment64
                = 0x00000019u, // LC_SEGMENT_64



00102
       LoadCommandRoutines64
               = 0x0000001Au, // LC_ROUTINES_64



00103
       LoadCommandUUID
                     = 0x0000001Bu, // LC_UUID



00104
       LoadCommandRunpath
                  = 0x8000001Cu, // LC_RPATH



00105
       LoadCommandCodeSignature
            = 0x0000001Du, // LC_CODE_SIGNATURE



00106
       LoadCommandSegmentSplitInfo
         = 0x0000001Eu, // LC_SEGMENT_SPLIT_INFO



00107
       LoadCommandDylibReexport
            = 0x8000001Fu, // LC_REEXPORT_DYLIB



00108
       LoadCommandDylibLazyLoad
            = 0x00000020u, // LC_LAZY_LOAD_DYLIB



00109
       LoadCommandEncryptionInfo
           = 0x00000021u, // LC_ENCRYPTION_INFO



00110
       LoadCommandDynamicLinkerInfo
        = 0x00000022u, // LC_DYLD_INFO



00111
       LoadCommandDynamicLinkerInfoOnly
    = 0x80000022u, // LC_DYLD_INFO_ONLY



00112
       LoadCommandDylibLoadUpward
          = 0x80000023u, // LC_LOAD_UPWARD_DYLIB



00113       


00114       // Constant bits for the "flags" field in llvm::MachO::segment_command



00115
       SegmentCommandFlagBitHighVM
             = 0x1u, // SG_HIGHVM



00116
       SegmentCommandFlagBitFixedVMLibrary
     = 0x2u, // SG_FVMLIB



00117
       SegmentCommandFlagBitNoRelocations
      = 0x4u, // SG_NORELOC



00118
       SegmentCommandFlagBitProtectedVersion1
  = 0x8u, // SG_PROTECTED_VERSION_1



00119 


00120 


00121       // Constant masks for the "flags" field in llvm::MachO::section and



00122       // llvm::MachO::section_64



00123
       SectionFlagMaskSectionType
      = 0x000000ffu, // SECTION_TYPE



00124
       SectionFlagMaskAllAttributes
    = 0xffffff00u, // SECTION_ATTRIBUTES



00125
       SectionFlagMaskUserAttributes
   = 0xff000000u, // SECTION_ATTRIBUTES_USR



00126
       SectionFlagMaskSystemAttributes
 = 0x00ffff00u, // SECTION_ATTRIBUTES_SYS



00127 


00128       // Constant masks for the "flags[7:0]" field in llvm::MachO::section and



00129       // llvm::MachO::section_64 (mask "flags" with SECTION_TYPE)



00130
       SectionTypeRegular
                    = 0x00u, // S_REGULAR



00131
       SectionTypeZeroFill
                   = 0x01u, // S_ZEROFILL



00132
       SectionTypeCStringLiterals
            = 0x02u, // S_CSTRING_LITERALS



00133
       SectionType4ByteLiterals
              = 0x03u, // S_4BYTE_LITERALS



00134
       SectionType8ByteLiterals
              = 0x04u, // S_8BYTE_LITERALS



00135
       SectionTypeLiteralPointers
            = 0x05u, // S_LITERAL_POINTERS



00136
       SectionTypeNonLazySymbolPointers
      = 0x06u, // S_NON_LAZY_SYMBOL_POINTERS



00137
       SectionTypeLazySymbolPointers
         = 0x07u, // S_LAZY_SYMBOL_POINTERS



00138
       SectionTypeSymbolStubs
                = 0x08u, // S_SYMBOL_STUBS



00139
       SectionTypeModuleInitFunctionPointers
 = 0x09u, // S_MOD_INIT_FUNC_POINTERS



00140
       SectionTypeModuleTermFunctionPointers
 = 0x0au, // S_MOD_TERM_FUNC_POINTERS



00141
       SectionTypeCoalesced
                  = 0x0bu, // S_COALESCED



00142
       SectionTypeZeroFillLarge
              = 0x0cu, // S_GB_ZEROFILL



00143
       SectionTypeInterposing
                = 0x0du, // S_INTERPOSING



00144
       SectionType16ByteLiterals
             = 0x0eu, // S_16BYTE_LITERALS



00145
       SectionTypeDTraceObjectFormat
         = 0x0fu, // S_DTRACE_DOF



00146
       SectionTypeLazyDylibSymbolPointers
    = 0x10u, // S_LAZY_DYLIB_SYMBOL_POINTERS



00147 


00148       // Constant masks for the "flags[31:24]" field in llvm::MachO::section and



00149       // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_USR)



00150
       SectionAttrUserPureInstructions
       = 0x80000000u, // S_ATTR_PURE_INSTRUCTIONS



00151
       SectionAttrUserNoTableOfContents
      = 0x40000000u, // S_ATTR_NO_TOC



00152
       SectionAttrUserCanStripStaticSymbols
  = 0x20000000u, // S_ATTR_STRIP_STATIC_SYMS



00153
       SectionAttrUserNoDeadStrip
            = 0x10000000u, // S_ATTR_NO_DEAD_STRIP



00154
       SectionAttrUserLiveSupport
            = 0x08000000u, // S_ATTR_LIVE_SUPPORT



00155
       SectionAttrUserSelfModifyingCode
      = 0x04000000u, // S_ATTR_SELF_MODIFYING_CODE



00156
       SectionAttrUserDebug
                  = 0x02000000u, // S_ATTR_DEBUG



00157 


00158       // Constant masks for the "flags[23:8]" field in llvm::MachO::section and



00159       // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_SYS)



00160
       SectionAttrSytemSomeInstructions
      = 0x00000400u, // S_ATTR_SOME_INSTRUCTIONS



00161
       SectionAttrSytemHasExternalRelocations
= 0x00000200u, // S_ATTR_EXT_RELOC



00162
       SectionAttrSytemHasLocalRelocations
   = 0x00000100u, // S_ATTR_LOC_RELOC



00163 


00164
       IndirectSymbolLocal
                   = 0x80000000u, // INDIRECT_SYMBOL_LOCAL



00165
       IndirectSymbolAbsolute
                = 0x40000000u, // INDIRECT_SYMBOL_ABS



00166 


00167
       RebaseTypePointer
                     = 1u, // REBASE_TYPE_POINTER



00168
       RebaseTypeTextAbsolute32
              = 2u, // REBASE_TYPE_TEXT_ABSOLUTE32



00169
       RebaseTypeTextPCRelative32
      = 3u, // REBASE_TYPE_TEXT_PCREL32



00170 


00171
       RebaseOpcodeMask
                          = 0xF0u, // REBASE_OPCODE_MASK



00172
       RebaseImmediateMask
                       = 0x0Fu, // REBASE_IMMEDIATE_MASK



00173
       RebaseOpcodeDone
                          = 0x00u, // REBASE_OPCODE_DONE



00174
       RebaseOpcodeSetTypeImmediate
              = 0x10u, // REBASE_OPCODE_SET_TYPE_IMM



00175
       RebaseOpcodeSetSegmentAndOffsetULEB
 = 0x20u, // REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB



00176
       RebaseOpcodeAddAddressULEB
                = 0x30u, // REBASE_OPCODE_ADD_ADDR_ULEB



00177
       RebaseOpcodeAddAddressImmediateScaled
 = 0x40u, // REBASE_OPCODE_ADD_ADDR_IMM_SCALED



00178
       RebaseOpcodeDoRebaseImmediateTimes
  = 0x50u, // REBASE_OPCODE_DO_REBASE_IMM_TIMES



00179
       RebaseOpcodeDoRebaseULEBTimes
             = 0x60u, // REBASE_OPCODE_DO_REBASE_ULEB_TIMES



00180
       RebaseOpcodeDoRebaseAddAddressULEB
        = 0x70u, // REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB



00181
       RebaseOpcodeDoRebaseULEBTimesSkippingULEB
 = 0x80u, // REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB



00182 


00183 


00184
       BindTypePointer
           = 1u, // BIND_TYPE_POINTER



00185
       BindTypeTextAbsolute32
  = 2u, // BIND_TYPE_TEXT_ABSOLUTE32



00186
       BindTypeTextPCRelative32
  = 3u, // BIND_TYPE_TEXT_PCREL32



00187 


00188
       BindSpecialDylibSelf
            =  0u, // BIND_SPECIAL_DYLIB_SELF



00189
       BindSpecialDylibMainExecutable
  = -1u, // BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE



00190
       BindSpecialDylibFlatLookup
      = -2u, // BIND_SPECIAL_DYLIB_FLAT_LOOKUP



00191 


00192
       BindSymbolFlagsWeakImport
         = 0x1u, // BIND_SYMBOL_FLAGS_WEAK_IMPORT



00193
       BindSymbolFlagsNonWeakDefinition
  = 0x8u, // BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION



00194 


00195
       BindOpcodeMask
                            = 0xF0u, // BIND_OPCODE_MASK



00196
       BindImmediateMask
                         = 0x0Fu, // BIND_IMMEDIATE_MASK



00197
       BindOpcodeDone
                            = 0x00u, // BIND_OPCODE_DONE



00198
       BindOpcodeSetDylibOrdinalImmediate
        = 0x10u, // BIND_OPCODE_SET_DYLIB_ORDINAL_IMM



00199
       BindOpcodeSetDylibOrdinalULEB
             = 0x20u, // BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB



00200
       BindOpcodeSetDylibSpecialImmediate
  = 0x30u, // BIND_OPCODE_SET_DYLIB_SPECIAL_IMM



00201
       BindOpcodeSetSymbolTrailingFlagsImmediate
 = 0x40u, // BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM



00202
       BindOpcodeSetTypeImmediate
    = 0x50u, // BIND_OPCODE_SET_TYPE_IMM



00203
       BindOpcodeSetAppendSLEB
                   = 0x60u, // BIND_OPCODE_SET_ADDEND_SLEB



00204
       BindOpcodeSetSegmentAndOffsetULEB
         = 0x70u, // BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB



00205
       BindOpcodeAddAddressULEB
                  = 0x80u, // BIND_OPCODE_ADD_ADDR_ULEB



00206
       BindOpcodeDoBind
                          = 0x90u, // BIND_OPCODE_DO_BIND



00207
       BindOpcodeDoBindAddAddressULEB
    = 0xA0u, // BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB



00208
       BindOpcodeDoBindAddAddressImmediateScaled
 = 0xB0u, // BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED



00209
       BindOpcodeDoBindULEBTimesSkippingULEB
     = 0xC0u, // BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB



00210 


00211
       ExportSymbolFlagsKindMask
           = 0x03u, // EXPORT_SYMBOL_FLAGS_KIND_MASK



00212
       ExportSymbolFlagsKindRegular
    = 0x00u, // EXPORT_SYMBOL_FLAGS_KIND_REGULAR



00213
       ExportSymbolFlagsKindThreadLocal
    = 0x01u, // EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL



00214
       ExportSymbolFlagsWeakDefinition
     = 0x04u, // EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION



00215
       ExportSymbolFlagsIndirectDefinition
 = 0x08u, // EXPORT_SYMBOL_FLAGS_INDIRECT_DEFINITION



00216
       ExportSymbolFlagsHasSpecializations
 = 0x10u, // EXPORT_SYMBOL_FLAGS_HAS_SPECIALIZATIONS



00217 


00218 


00219       // Constant masks for the "n_type" field in llvm::MachO::nlist and



00220       // llvm::MachO::nlist_64



00221
       NlistMaskStab
             = 0xe0, // N_STAB



00222
       NlistMaskPrivateExternal
  = 0x10, // N_PEXT



00223
       NlistMaskType
             = 0x0e, // N_TYPE



00224
       NlistMaskExternal
         = 0x01, // N_EXT



00225 


00226       // Constants for the "n_type & N_TYPE" llvm::MachO::nlist and



00227       // llvm::MachO::nlist_64



00228
       NListTypeUndefined
          = 0x0u, // N_UNDF



00229
       NListTypeAbsolute
           = 0x2u, // N_ABS



00230
       NListTypeSection
            = 0xeu, // N_SECT



00231
       NListTypePreboundUndefined
  = 0xcu, // N_PBUD



00232
       NListTypeIndirect
           = 0xau, // N_INDR



00233 


00234       // Constant masks for the "n_sect" field in llvm::MachO::nlist and



00235       // llvm::MachO::nlist_64



00236
       NListSectionNoSection
     = 0u, // NO_SECT



00237
       NListSectionMaxSection
    = 0xffu, // MAX_SECT



00238 


00239       // Constant values for the "n_type" field in llvm::MachO::nlist and



00240       // llvm::MachO::nlist_64 when "(n_type & NlistMaskStab) != 0"



00241
       StabGlobalSymbol
          = 0x20u,  // N_GSYM 



00242
       StabFunctionName
          = 0x22u,  // N_FNAME  



00243
       StabFunction
              = 0x24u,  // N_FUN  



00244
       StabStaticSymbol
          = 0x26u,  // N_STSYM  



00245
       StabLocalCommon
           = 0x28u,  // N_LCSYM  



00246
       StabBeginSymbol
           = 0x2Eu,  // N_BNSYM   



00247
       StabSourceFileOptions
     = 0x3Cu,  // N_OPT  



00248
       StabRegisterSymbol
        = 0x40u,  // N_RSYM 



00249
       StabSourceLine
            = 0x44u,  // N_SLINE  



00250
       StabEndSymbol
             = 0x4Eu,  // N_ENSYM   



00251
       StabStructureType
         = 0x60u,  // N_SSYM 



00252
       StabSourceFileName
        = 0x64u,  // N_SO 



00253
       StabObjectFileName
        = 0x66u,  // N_OSO  



00254
       StabLocalSymbol
           = 0x80u,  // N_LSYM 



00255
       StabBeginIncludeFileName
  = 0x82u,  // N_BINCL  



00256
       StabIncludeFileName
       = 0x84u,  // N_SOL  



00257
       StabCompilerParameters
    = 0x86u,  // N_PARAMS  



00258
       StabCompilerVersion
       = 0x88u,  // N_VERSION 



00259
       StabCompilerOptLevel
      = 0x8Au,  // N_OLEVEL  



00260
       StabParameter
             = 0xA0u,  // N_PSYM 



00261
       StabEndIncludeFile
        = 0xA2u,  // N_EINCL  



00262
       StabAlternateEntry
        = 0xA4u,  // N_ENTRY  



00263
       StabLeftBracket
           = 0xC0u,  // N_LBRAC  



00264
       StabDeletedIncludeFile
    = 0xC2u,  // N_EXCL 



00265
       StabRightBracket
          = 0xE0u,  // N_RBRAC  



00266
       StabBeginCommon
           = 0xE2u,  // N_BCOMM  



00267
       StabEndCommon
             = 0xE4u,  // N_ECOMM  



00268
       StabEndCommonLocal
        = 0xE8u,  // N_ECOML  



00269
       StabLength
                = 0xFEu   // N_LENG 



00270 


00271     };


00272     


00273     // Structs from <mach-o/loader.h>



00274     


00275
     struct 
mach_header
 {


00276
       uint32_t magic
;


00277
       uint32_t cputype
;


00278
       uint32_t cpusubtype
;


00279
       uint32_t filetype
;


00280
       uint32_t ncmds
;


00281
       uint32_t sizeofcmds
;


00282
       uint32_t flags
;


00283     };


00284 


00285
     struct 
mach_header_64
 {


00286
       uint32_t magic
;


00287
       uint32_t cputype
;


00288
       uint32_t cpusubtype
;


00289
       uint32_t filetype
;


00290
       uint32_t ncmds
;


00291
       uint32_t sizeofcmds
;


00292
       uint32_t flags
;


00293
       uint32_t reserved
;


00294     };


00295 


00296
     struct 
load_command
 {


00297
       uint32_t cmd
;


00298
       uint32_t cmdsize
;


00299     };


00300 


00301
     struct 
segment_command
 {


00302
       uint32_t cmd
;


00303
       uint32_t cmdsize
;


00304
       char
 segname
[16];


00305
       uint32_t vmaddr
;


00306
       uint32_t vmsize
;


00307
       uint32_t fileoff
;


00308
       uint32_t filesize
;


00309
       uint32_t maxprot
;


00310
       uint32_t initprot
;


00311
       uint32_t nsects
;


00312
       uint32_t flags
;


00313     };


00314 


00315
     struct 
segment_command_64
 {


00316
       uint32_t cmd
;


00317
       uint32_t cmdsize
;


00318
       char
 segname
[16];


00319
       uint64_t vmaddr
;


00320
       uint64_t vmsize
;


00321
       uint64_t fileoff
;


00322
       uint64_t filesize
;


00323
       uint32_t maxprot
;


00324
       uint32_t initprot
;


00325
       uint32_t nsects
;


00326
       uint32_t flags
;


00327     };


00328 


00329
     struct 
section
 {


00330
       char
 sectname
[16];


00331
       char
 segname
[16];


00332
       uint32_t addr
;


00333
       uint32_t size
;


00334
       uint32_t offset
;


00335
       uint32_t align
;


00336
       uint32_t reloff
;


00337
       uint32_t nreloc
;


00338
       uint32_t flags
;


00339
       uint32_t reserved1
;


00340
       uint32_t reserved2
;


00341     };


00342 


00343
     struct 
section_64
 {


00344
       char
 sectname
[16];


00345
       char
 segname
[16];


00346
       uint64_t addr
;


00347
       uint64_t size
;


00348
       uint32_t offset
;


00349
       uint32_t align
;


00350
       uint32_t reloff
;


00351
       uint32_t nreloc
;


00352
       uint32_t flags
;


00353
       uint32_t reserved1
;


00354
       uint32_t reserved2
;


00355
       uint32_t reserved3
;


00356     };


00357 


00358
     struct 
fvmlib
 {


00359
       uint32_t name
;


00360
       uint32_t minor_version
;


00361
       uint32_t header_addr
;


00362     };


00363 


00364
     struct 
fvmlib_command
 {


00365
       uint32_t  cmd
;


00366
       uint32_t cmdsize
;


00367
       struct 
fvmlib
 fvmlib
;


00368     };


00369 


00370
     struct 
dylib
 {


00371
       uint32_t name
;


00372
       uint32_t timestamp
;


00373
       uint32_t current_version
;


00374
       uint32_t compatibility_version
;


00375     };


00376 


00377
     struct 
dylib_command
 {


00378
       uint32_t cmd
;


00379
       uint32_t cmdsize
;


00380
       struct 
dylib
 dylib
;


00381     };


00382 


00383
     struct 
sub_framework_command
 {


00384
       uint32_t cmd
;


00385
       uint32_t cmdsize
;


00386
       uint32_t umbrella
;


00387     };


00388 


00389
     struct 
sub_client_command
 {


00390
       uint32_t cmd
;


00391
       uint32_t cmdsize
;


00392
       uint32_t client
;


00393     };


00394 


00395
     struct 
sub_umbrella_command
 {


00396
       uint32_t cmd
;


00397
       uint32_t cmdsize
;


00398
       uint32_t sub_umbrella
;


00399     };


00400 


00401
     struct 
sub_library_command
 {


00402
       uint32_t cmd
;


00403
       uint32_t cmdsize
;


00404
       uint32_t sub_library
;


00405     };


00406 


00407
     struct 
prebound_dylib_command
 {


00408
       uint32_t cmd
;


00409
       uint32_t cmdsize
;


00410
       uint32_t name
;


00411
       uint32_t nmodules
;


00412
       uint32_t linked_modules
;


00413     };


00414 


00415
     struct 
dylinker_command
 {


00416
       uint32_t cmd
;


00417
       uint32_t cmdsize
;


00418
       uint32_t name
;


00419     };


00420 


00421
     struct 
thread_command
 {


00422
       uint32_t cmd
;


00423
       uint32_t cmdsize
;


00424     };


00425 


00426
     struct 
routines_command
 {


00427
       uint32_t cmd
;


00428
       uint32_t cmdsize
;


00429
       uint32_t init_address
;


00430
       uint32_t init_module
;


00431
       uint32_t reserved1
;


00432
       uint32_t reserved2
;


00433
       uint32_t reserved3
;


00434
       uint32_t reserved4
;


00435
       uint32_t reserved5
;


00436
       uint32_t reserved6
;


00437     };


00438 


00439
     struct 
routines_command_64
 {


00440
       uint32_t cmd
;


00441
       uint32_t cmdsize
;


00442
       uint64_t init_address
;


00443
       uint64_t init_module
;


00444
       uint64_t reserved1
;


00445
       uint64_t reserved2
;


00446
       uint64_t reserved3
;


00447
       uint64_t reserved4
;


00448
       uint64_t reserved5
;


00449
       uint64_t reserved6
;


00450     };


00451 


00452
     struct 
symtab_command
 {


00453
       uint32_t cmd
;


00454
       uint32_t cmdsize
;


00455
       uint32_t symoff
;


00456
       uint32_t nsyms
;


00457
       uint32_t stroff
;


00458
       uint32_t strsize
;


00459     };


00460 


00461
     struct 
dysymtab_command
 {


00462
       uint32_t cmd
;


00463
       uint32_t cmdsize
;


00464
       uint32_t ilocalsym
;


00465
       uint32_t nlocalsym
;


00466
       uint32_t iextdefsym
;


00467
       uint32_t nextdefsym
;


00468
       uint32_t iundefsym
;


00469
       uint32_t nundefsym
;


00470
       uint32_t tocoff
;


00471
       uint32_t ntoc
;


00472
       uint32_t modtaboff
;


00473
       uint32_t nmodtab
;


00474
       uint32_t extrefsymoff
;


00475
       uint32_t nextrefsyms
;


00476
       uint32_t indirectsymoff
;


00477
       uint32_t nindirectsyms
;


00478
       uint32_t extreloff
;


00479
       uint32_t nextrel
;


00480
       uint32_t locreloff
;


00481
       uint32_t nlocrel
;


00482     };  


00483 


00484
     struct 
dylib_table_of_contents
 {


00485
       uint32_t symbol_index
;


00486
       uint32_t module_index
;


00487     };  


00488 


00489
     struct 
dylib_module
 {


00490
       uint32_t module_name
;


00491
       uint32_t iextdefsym
;


00492
       uint32_t nextdefsym
;


00493
       uint32_t irefsym
;


00494
       uint32_t nrefsym
;


00495
       uint32_t ilocalsym
;


00496
       uint32_t nlocalsym
;


00497
       uint32_t iextrel
;


00498
       uint32_t nextrel
;


00499
       uint32_t iinit_iterm
;


00500
       uint32_t ninit_nterm
;


00501
       uint32_t objc_module_info_addr
;


00502
       uint32_t objc_module_info_size
;


00503     };  


00504 


00505
     struct 
dylib_module_64
 {


00506
       uint32_t module_name
;


00507
       uint32_t iextdefsym
;


00508
       uint32_t nextdefsym
;


00509
       uint32_t irefsym
;


00510
       uint32_t nrefsym
;


00511
       uint32_t ilocalsym
;


00512
       uint32_t nlocalsym
;


00513
       uint32_t iextrel
;


00514
       uint32_t nextrel
;


00515
       uint32_t iinit_iterm
;


00516
       uint32_t ninit_nterm
;


00517
       uint32_t objc_module_info_size
;


00518
       uint64_t objc_module_info_addr
;


00519     };


00520 


00521
     struct 
dylib_reference
 {


00522
       uint32_t isym
:24,


00523                flags
:8;


00524     };


00525 


00526 


00527
     struct 
twolevel_hints_command
 {


00528
       uint32_t cmd
;


00529
       uint32_t cmdsize
;


00530
       uint32_t offset
;


00531
       uint32_t nhints
;


00532     };


00533 


00534
     struct 
twolevel_hint
 {


00535
       uint32_t isub_image
:8,


00536                itoc
:24;


00537     };


00538 


00539
     struct 
prebind_cksum_command
 {


00540
       uint32_t cmd
;


00541
       uint32_t cmdsize
;


00542
       uint32_t cksum
;


00543     };


00544 


00545
     struct 
uuid_command
 {


00546
       uint32_t cmd
;


00547
       uint32_t cmdsize
;


00548
       uint8_t uuid
[16];


00549     };


00550 


00551
     struct 
rpath_command
 {


00552
       uint32_t cmd
;


00553
       uint32_t cmdsize
;


00554
       uint32_t path
;


00555     };


00556 


00557
     struct 
linkedit_data_command
 {


00558
       uint32_t cmd
;


00559
       uint32_t cmdsize
;


00560
       uint32_t dataoff
;


00561
       uint32_t datasize
;


00562     };


00563 


00564
     struct 
encryption_info_command
 {


00565
       uint32_t cmd
;


00566
       uint32_t cmdsize
;


00567
       uint32_t cryptoff
;


00568
       uint32_t cryptsize
;


00569
       uint32_t cryptid
;


00570     };


00571 


00572
     struct 
dyld_info_command
 {


00573
       uint32_t cmd
;


00574
       uint32_t cmdsize
;


00575
       uint32_t rebase_off
;


00576
       uint32_t rebase_size
;


00577
       uint32_t bind_off
;


00578
       uint32_t bind_size
;


00579
       uint32_t weak_bind_off
;


00580
       uint32_t weak_bind_size
;


00581
       uint32_t lazy_bind_off
;


00582
       uint32_t lazy_bind_size
;


00583
       uint32_t export_off
;


00584
       uint32_t export_size
;


00585     };


00586 


00587
     struct 
symseg_command
 {


00588
       uint32_t cmd
;


00589
       uint32_t cmdsize
;


00590
       uint32_t offset
;


00591
       uint32_t size
;


00592     };


00593 


00594
     struct 
ident_command
 {


00595
       uint32_t cmd
;


00596
       uint32_t cmdsize
;


00597     };


00598 


00599
     struct 
fvmfile_command
 {


00600
       uint32_t cmd
;


00601
       uint32_t cmdsize
;


00602
       uint32_t name
;


00603
       uint32_t header_addr
;


00604     };


00605 


00606 


00607     // Structs from <mach-o/fat.h>



00608
     struct 
fat_header
 {


00609
       uint32_t magic
;


00610
       uint32_t nfat_arch
;


00611     };


00612 


00613
     struct 
fat_arch
 {


00614
       uint32_t cputype
;


00615
       uint32_t cpusubtype
;


00616
       uint32_t offset
;


00617
       uint32_t size
;


00618
       uint32_t align
;


00619     };


00620 


00621     // Structs from <mach-o/fat.h>



00622
     struct 
nlist
 {


00623
       uint32_t n_strx
;


00624
       uint8_t n_type
;


00625
       uint8_t n_sect
;


00626
       int16_t n_desc
;


00627
       uint32_t n_value
;


00628     };


00629 


00630
     struct 
nlist_64
 {


00631
       uint32_t n_strx
;


00632
       uint8_t n_type
;


00633
       uint8_t n_sect
;


00634
       uint16_t n_desc
;


00635
       uint64_t n_value
;


00636     };


00637 


00638     // Get/Set functions from <mach-o/nlist.h>



00639     


00640
     static
 inline
 uint16_t GET_LIBRARY_ORDINAL
(uint16_t n_desc)


00641     {


00642       return
 (((n_desc) >> 8u) & 0xffu);


00643     }


00644       


00645
     static
 inline
 void
 SET_LIBRARY_ORDINAL
(uint16_t &n_desc, uint8_t ordinal)


00646     {


00647       n_desc = (((n_desc) & 0x00ff) | (((ordinal) & 0xff) << 8));


00648     }


00649 


00650
     static
 inline
 uint8_t GET_COMM_ALIGN
 (uint16_t n_desc)


00651     {


00652       return
 (n_desc >> 8u) & 0x0fu;


00653     }


00654     


00655
     static
 inline
 void
 SET_COMM_ALIGN
 (uint16_t &n_desc, uint8_t align)


00656     {


00657       n_desc = ((n_desc & 0xf0ffu) | ((align & 0x0fu) << 8u));


00658     }


00659 


00660     // Enums from <mach/machine.h>



00661     enum
 {


00662       // Capability bits used in the definition of cpu_type.



00663
       CPUArchMask
 = 0xff000000,   // Mask for architecture bits



00664
       CPUArchABI64
 = 0x01000000,  // 64 bit ABI



00665       


00666       // Constants for the cputype field.



00667
       CPUTypeI386
      = 7,


00668
       CPUTypeX86_64
    = CPUTypeI386
 | CPUArchABI64
,


00669
       CPUTypeARM
       = 12,


00670
       CPUTypeSPARC
     = 14,


00671
       CPUTypePowerPC
   = 18,


00672
       CPUTypePowerPC64
 = CPUTypePowerPC
 | CPUArchABI64
,


00673 


00674 


00675       // Constants for the cpusubtype field.



00676       


00677       // X86



00678
       CPUSubType_I386_ALL
    = 3,


00679
       CPUSubType_X86_64_ALL
  = 3,


00680       


00681       // ARM



00682
       CPUSubType_ARM_ALL
     = 0,


00683
       CPUSubType_ARM_V4T
     = 5,


00684
       CPUSubType_ARM_V5
      = 7,


00685
       CPUSubType_ARM_V6
      = 6,


00686
       CPUSubType_ARM_V7
      = 9,


00687 


00688       // PowerPC



00689
       CPUSubType_POWERPC_ALL
 = 0,


00690       


00691
       CPUSubType_SPARC_ALL
   = 0


00692     };


00693   } // end namespace MachO



00694 } // end namespace llvm



00695 


00696 #endif




see also: http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/kern/mach_loader.c

      

繼續閱讀