Home | History | Annotate | Download | only in Object
      1 //===- MachOFormat.h - Mach-O Format Structures And Constants ---*- 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 declares various structures and constants which are platform
     11 // independent and can be shared by any client which wishes to interact with
     12 // Mach object files.
     13 //
     14 // The definitions here are purposely chosen to match the LLVM style as opposed
     15 // to following the platform specific definition of the format.
     16 //
     17 // On a Mach system, see the <mach-o/...> includes for more information, in
     18 // particular <mach-o/loader.h>.
     19 //
     20 //===----------------------------------------------------------------------===//
     21 
     22 #ifndef LLVM_OBJECT_MACHOFORMAT_H
     23 #define LLVM_OBJECT_MACHOFORMAT_H
     24 
     25 #include "llvm/Support/DataTypes.h"
     26 
     27 namespace llvm {
     28 namespace object {
     29 
     30 /// General Mach platform information.
     31 namespace mach {
     32   /// @name CPU Type and Subtype Information
     33   /// {
     34 
     35   /// \brief Capability bits used in CPU type encoding.
     36   enum CPUTypeFlagsMask {
     37     CTFM_ArchMask =  0xFF000000,
     38     CTFM_ArchABI64 = 0x01000000
     39   };
     40 
     41   /// \brief Machine type IDs used in CPU type encoding.
     42   enum CPUTypeMachine {
     43     CTM_i386      = 7,
     44     CTM_x86_64    = CTM_i386 | CTFM_ArchABI64,
     45     CTM_ARM       = 12,
     46     CTM_SPARC     = 14,
     47     CTM_PowerPC   = 18,
     48     CTM_PowerPC64 = CTM_PowerPC | CTFM_ArchABI64
     49   };
     50 
     51   /// \brief Capability bits used in CPU subtype encoding.
     52   enum CPUSubtypeFlagsMask {
     53     CSFM_SubtypeMask =  0xFF000000,
     54     CSFM_SubtypeLib64 = 0x80000000
     55   };
     56 
     57   /// \brief ARM Machine Subtypes.
     58   enum CPUSubtypeARM {
     59     CSARM_ALL    = 0,
     60     CSARM_V4T    = 5,
     61     CSARM_V6     = 6,
     62     CSARM_V5TEJ  = 7,
     63     CSARM_XSCALE = 8,
     64     CSARM_V7     = 9
     65   };
     66 
     67   /// \brief PowerPC Machine Subtypes.
     68   enum CPUSubtypePowerPC {
     69     CSPPC_ALL = 0
     70   };
     71 
     72   /// \brief SPARC Machine Subtypes.
     73   enum CPUSubtypeSPARC {
     74     CSSPARC_ALL = 0
     75   };
     76 
     77   /// \brief x86 Machine Subtypes.
     78   enum CPUSubtypeX86 {
     79     CSX86_ALL = 3
     80   };
     81 
     82   /// @}
     83 
     84 } // end namespace mach
     85 
     86 /// Format information for Mach object files.
     87 namespace macho {
     88   /// \brief Constants for structure sizes.
     89   enum StructureSizes {
     90     Header32Size = 28,
     91     Header64Size = 32,
     92     SegmentLoadCommand32Size = 56,
     93     SegmentLoadCommand64Size = 72,
     94     Section32Size = 68,
     95     Section64Size = 80,
     96     SymtabLoadCommandSize = 24,
     97     DysymtabLoadCommandSize = 80,
     98     Nlist32Size = 12,
     99     Nlist64Size = 16,
    100     RelocationInfoSize = 8
    101   };
    102 
    103   /// \brief Constants for header magic field.
    104   enum HeaderMagic {
    105     HM_Object32 = 0xFEEDFACE,  ///< 32-bit mach object file
    106     HM_Object64 = 0xFEEDFACF,  ///< 64-bit mach object file
    107     HM_Universal = 0xCAFEBABE  ///< Universal object file
    108   };
    109 
    110   /// \brief Header common to all Mach object files.
    111   struct Header {
    112     uint32_t Magic;
    113     uint32_t CPUType;
    114     uint32_t CPUSubtype;
    115     uint32_t FileType;
    116     uint32_t NumLoadCommands;
    117     uint32_t SizeOfLoadCommands;
    118     uint32_t Flags;
    119   };
    120 
    121   /// \brief Extended header for 64-bit object files.
    122   struct Header64Ext {
    123     uint32_t Reserved;
    124   };
    125 
    126   // See <mach-o/loader.h>.
    127   enum HeaderFileType {
    128     HFT_Object = 0x1
    129   };
    130 
    131   enum HeaderFlags {
    132     HF_SubsectionsViaSymbols = 0x2000
    133   };
    134 
    135   enum LoadCommandType {
    136     LCT_Segment = 0x1,
    137     LCT_Symtab = 0x2,
    138     LCT_Dysymtab = 0xb,
    139     LCT_Segment64 = 0x19,
    140     LCT_UUID = 0x1b
    141   };
    142 
    143   /// \brief Load command structure.
    144   struct LoadCommand {
    145     uint32_t Type;
    146     uint32_t Size;
    147   };
    148 
    149   /// @name Load Command Structures
    150   /// @{
    151 
    152   struct SegmentLoadCommand {
    153     uint32_t Type;
    154     uint32_t Size;
    155     char Name[16];
    156     uint32_t VMAddress;
    157     uint32_t VMSize;
    158     uint32_t FileOffset;
    159     uint32_t FileSize;
    160     uint32_t MaxVMProtection;
    161     uint32_t InitialVMProtection;
    162     uint32_t NumSections;
    163     uint32_t Flags;
    164   };
    165 
    166   struct Segment64LoadCommand {
    167     uint32_t Type;
    168     uint32_t Size;
    169     char Name[16];
    170     uint64_t VMAddress;
    171     uint64_t VMSize;
    172     uint64_t FileOffset;
    173     uint64_t FileSize;
    174     uint32_t MaxVMProtection;
    175     uint32_t InitialVMProtection;
    176     uint32_t NumSections;
    177     uint32_t Flags;
    178   };
    179 
    180   struct SymtabLoadCommand {
    181     uint32_t Type;
    182     uint32_t Size;
    183     uint32_t SymbolTableOffset;
    184     uint32_t NumSymbolTableEntries;
    185     uint32_t StringTableOffset;
    186     uint32_t StringTableSize;
    187   };
    188 
    189   struct DysymtabLoadCommand {
    190     uint32_t Type;
    191     uint32_t Size;
    192 
    193     uint32_t LocalSymbolsIndex;
    194     uint32_t NumLocalSymbols;
    195 
    196     uint32_t ExternalSymbolsIndex;
    197     uint32_t NumExternalSymbols;
    198 
    199     uint32_t UndefinedSymbolsIndex;
    200     uint32_t NumUndefinedSymbols;
    201 
    202     uint32_t TOCOffset;
    203     uint32_t NumTOCEntries;
    204 
    205     uint32_t ModuleTableOffset;
    206     uint32_t NumModuleTableEntries;
    207 
    208     uint32_t ReferenceSymbolTableOffset;
    209     uint32_t NumReferencedSymbolTableEntries;
    210 
    211     uint32_t IndirectSymbolTableOffset;
    212     uint32_t NumIndirectSymbolTableEntries;
    213 
    214     uint32_t ExternalRelocationTableOffset;
    215     uint32_t NumExternalRelocationTableEntries;
    216 
    217     uint32_t LocalRelocationTableOffset;
    218     uint32_t NumLocalRelocationTableEntries;
    219   };
    220 
    221   /// @}
    222   /// @name Section Data
    223   /// @{
    224 
    225   struct Section {
    226     char Name[16];
    227     char SegmentName[16];
    228     uint32_t Address;
    229     uint32_t Size;
    230     uint32_t Offset;
    231     uint32_t Align;
    232     uint32_t RelocationTableOffset;
    233     uint32_t NumRelocationTableEntries;
    234     uint32_t Flags;
    235     uint32_t Reserved1;
    236     uint32_t Reserved2;
    237   };
    238   struct Section64 {
    239     char Name[16];
    240     char SegmentName[16];
    241     uint64_t Address;
    242     uint64_t Size;
    243     uint32_t Offset;
    244     uint32_t Align;
    245     uint32_t RelocationTableOffset;
    246     uint32_t NumRelocationTableEntries;
    247     uint32_t Flags;
    248     uint32_t Reserved1;
    249     uint32_t Reserved2;
    250     uint32_t Reserved3;
    251   };
    252 
    253   /// @}
    254   /// @name Symbol Table Entries
    255   /// @{
    256 
    257   struct SymbolTableEntry {
    258     uint32_t StringIndex;
    259     uint8_t Type;
    260     uint8_t SectionIndex;
    261     uint16_t Flags;
    262     uint32_t Value;
    263   };
    264   struct Symbol64TableEntry {
    265     uint32_t StringIndex;
    266     uint8_t Type;
    267     uint8_t SectionIndex;
    268     uint16_t Flags;
    269     uint64_t Value;
    270   };
    271 
    272   /// @}
    273   /// @name Indirect Symbol Table
    274   /// @{
    275 
    276   struct IndirectSymbolTableEntry {
    277     uint32_t Index;
    278   };
    279 
    280   /// @}
    281   /// @name Relocation Data
    282   /// @{
    283 
    284   struct RelocationEntry {
    285     uint32_t Word0;
    286     uint32_t Word1;
    287   };
    288 
    289   /// @}
    290 
    291   // See <mach-o/nlist.h>.
    292   enum SymbolTypeType {
    293     STT_Undefined = 0x00,
    294     STT_Absolute  = 0x02,
    295     STT_Section   = 0x0e
    296   };
    297 
    298   enum SymbolTypeFlags {
    299     // If any of these bits are set, then the entry is a stab entry number (see
    300     // <mach-o/stab.h>. Otherwise the other masks apply.
    301     STF_StabsEntryMask = 0xe0,
    302 
    303     STF_TypeMask       = 0x0e,
    304     STF_External       = 0x01,
    305     STF_PrivateExtern  = 0x10
    306   };
    307 
    308   /// IndirectSymbolFlags - Flags for encoding special values in the indirect
    309   /// symbol entry.
    310   enum IndirectSymbolFlags {
    311     ISF_Local    = 0x80000000,
    312     ISF_Absolute = 0x40000000
    313   };
    314 
    315   /// RelocationFlags - Special flags for addresses.
    316   enum RelocationFlags {
    317     RF_Scattered = 0x80000000
    318   };
    319 
    320   /// Common relocation info types.
    321   enum RelocationInfoType {
    322     RIT_Vanilla             = 0,
    323     RIT_Pair                = 1,
    324     RIT_Difference          = 2
    325   };
    326 
    327   /// Generic relocation info types, which are shared by some (but not all)
    328   /// platforms.
    329   enum RelocationInfoType_Generic {
    330     RIT_Generic_PreboundLazyPointer = 3,
    331     RIT_Generic_LocalDifference     = 4,
    332     RIT_Generic_TLV                 = 5
    333   };
    334 
    335   /// X86_64 uses its own relocation types.
    336   enum RelocationInfoTypeX86_64 {
    337     // Note that x86_64 doesn't even share the common relocation types.
    338     RIT_X86_64_Unsigned   = 0,
    339     RIT_X86_64_Signed     = 1,
    340     RIT_X86_64_Branch     = 2,
    341     RIT_X86_64_GOTLoad    = 3,
    342     RIT_X86_64_GOT        = 4,
    343     RIT_X86_64_Subtractor = 5,
    344     RIT_X86_64_Signed1    = 6,
    345     RIT_X86_64_Signed2    = 7,
    346     RIT_X86_64_Signed4    = 8,
    347     RIT_X86_64_TLV        = 9
    348   };
    349 
    350   /// ARM uses its own relocation types.
    351   enum RelocationInfoTypeARM {
    352     RIT_ARM_LocalDifference = 3,
    353     RIT_ARM_PreboundLazyPointer = 4,
    354     RIT_ARM_Branch24Bit = 5,
    355     RIT_ARM_ThumbBranch22Bit = 6,
    356     RIT_ARM_ThumbBranch32Bit = 7,
    357     RIT_ARM_Half = 8,
    358     RIT_ARM_HalfDifference = 9
    359 
    360   };
    361 
    362 } // end namespace macho
    363 
    364 } // end namespace object
    365 } // end namespace llvm
    366 
    367 #endif
    368