Home | History | Annotate | Download | only in Support
      1 //===-- llvm/Support/COFF.h -------------------------------------*- 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 contains an definitions used in Windows COFF Files.
     11 //
     12 // Structures and enums defined within this file where created using
     13 // information from Microsoft's publicly available PE/COFF format document:
     14 //
     15 // Microsoft Portable Executable and Common Object File Format Specification
     16 // Revision 8.1 - February 15, 2008
     17 //
     18 // As of 5/2/2010, hosted by Microsoft at:
     19 // http://www.microsoft.com/whdc/system/platform/firmware/pecoff.mspx
     20 //
     21 //===----------------------------------------------------------------------===//
     22 
     23 #ifndef LLVM_SUPPORT_COFF_H
     24 #define LLVM_SUPPORT_COFF_H
     25 
     26 #include "llvm/Support/DataTypes.h"
     27 #include <cassert>
     28 #include <cstring>
     29 
     30 namespace llvm {
     31 namespace COFF {
     32 
     33   // The maximum number of sections that a COFF object can have (inclusive).
     34   const int MaxNumberOfSections = 65299;
     35 
     36   // The PE signature bytes that follows the DOS stub header.
     37   static const char PEMagic[] = { 'P', 'E', '\0', '\0' };
     38 
     39   // Sizes in bytes of various things in the COFF format.
     40   enum {
     41     HeaderSize     = 20,
     42     NameSize       = 8,
     43     SymbolSize     = 18,
     44     SectionSize    = 40,
     45     RelocationSize = 10
     46   };
     47 
     48   struct header {
     49     uint16_t Machine;
     50     uint16_t NumberOfSections;
     51     uint32_t TimeDateStamp;
     52     uint32_t PointerToSymbolTable;
     53     uint32_t NumberOfSymbols;
     54     uint16_t SizeOfOptionalHeader;
     55     uint16_t Characteristics;
     56   };
     57 
     58   enum MachineTypes {
     59     MT_Invalid = 0xffff,
     60 
     61     IMAGE_FILE_MACHINE_UNKNOWN   = 0x0,
     62     IMAGE_FILE_MACHINE_AM33      = 0x13,
     63     IMAGE_FILE_MACHINE_AMD64     = 0x8664,
     64     IMAGE_FILE_MACHINE_ARM       = 0x1C0,
     65     IMAGE_FILE_MACHINE_ARMNT     = 0x1C4,
     66     IMAGE_FILE_MACHINE_EBC       = 0xEBC,
     67     IMAGE_FILE_MACHINE_I386      = 0x14C,
     68     IMAGE_FILE_MACHINE_IA64      = 0x200,
     69     IMAGE_FILE_MACHINE_M32R      = 0x9041,
     70     IMAGE_FILE_MACHINE_MIPS16    = 0x266,
     71     IMAGE_FILE_MACHINE_MIPSFPU   = 0x366,
     72     IMAGE_FILE_MACHINE_MIPSFPU16 = 0x466,
     73     IMAGE_FILE_MACHINE_POWERPC   = 0x1F0,
     74     IMAGE_FILE_MACHINE_POWERPCFP = 0x1F1,
     75     IMAGE_FILE_MACHINE_R4000     = 0x166,
     76     IMAGE_FILE_MACHINE_SH3       = 0x1A2,
     77     IMAGE_FILE_MACHINE_SH3DSP    = 0x1A3,
     78     IMAGE_FILE_MACHINE_SH4       = 0x1A6,
     79     IMAGE_FILE_MACHINE_SH5       = 0x1A8,
     80     IMAGE_FILE_MACHINE_THUMB     = 0x1C2,
     81     IMAGE_FILE_MACHINE_WCEMIPSV2 = 0x169
     82   };
     83 
     84   enum Characteristics {
     85     C_Invalid = 0,
     86 
     87     /// The file does not contain base relocations and must be loaded at its
     88     /// preferred base. If this cannot be done, the loader will error.
     89     IMAGE_FILE_RELOCS_STRIPPED         = 0x0001,
     90     /// The file is valid and can be run.
     91     IMAGE_FILE_EXECUTABLE_IMAGE        = 0x0002,
     92     /// COFF line numbers have been stripped. This is deprecated and should be
     93     /// 0.
     94     IMAGE_FILE_LINE_NUMS_STRIPPED      = 0x0004,
     95     /// COFF symbol table entries for local symbols have been removed. This is
     96     /// deprecated and should be 0.
     97     IMAGE_FILE_LOCAL_SYMS_STRIPPED     = 0x0008,
     98     /// Aggressively trim working set. This is deprecated and must be 0.
     99     IMAGE_FILE_AGGRESSIVE_WS_TRIM      = 0x0010,
    100     /// Image can handle > 2GiB addresses.
    101     IMAGE_FILE_LARGE_ADDRESS_AWARE     = 0x0020,
    102     /// Little endian: the LSB precedes the MSB in memory. This is deprecated
    103     /// and should be 0.
    104     IMAGE_FILE_BYTES_REVERSED_LO       = 0x0080,
    105     /// Machine is based on a 32bit word architecture.
    106     IMAGE_FILE_32BIT_MACHINE           = 0x0100,
    107     /// Debugging info has been removed.
    108     IMAGE_FILE_DEBUG_STRIPPED          = 0x0200,
    109     /// If the image is on removable media, fully load it and copy it to swap.
    110     IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP = 0x0400,
    111     /// If the image is on network media, fully load it and copy it to swap.
    112     IMAGE_FILE_NET_RUN_FROM_SWAP       = 0x0800,
    113     /// The image file is a system file, not a user program.
    114     IMAGE_FILE_SYSTEM                  = 0x1000,
    115     /// The image file is a DLL.
    116     IMAGE_FILE_DLL                     = 0x2000,
    117     /// This file should only be run on a uniprocessor machine.
    118     IMAGE_FILE_UP_SYSTEM_ONLY          = 0x4000,
    119     /// Big endian: the MSB precedes the LSB in memory. This is deprecated
    120     /// and should be 0.
    121     IMAGE_FILE_BYTES_REVERSED_HI       = 0x8000
    122   };
    123 
    124   struct symbol {
    125     char     Name[NameSize];
    126     uint32_t Value;
    127     uint16_t SectionNumber;
    128     uint16_t Type;
    129     uint8_t  StorageClass;
    130     uint8_t  NumberOfAuxSymbols;
    131   };
    132 
    133   enum SymbolFlags {
    134     SF_TypeMask = 0x0000FFFF,
    135     SF_TypeShift = 0,
    136 
    137     SF_ClassMask = 0x00FF0000,
    138     SF_ClassShift = 16,
    139 
    140     SF_WeakExternal = 0x01000000
    141   };
    142 
    143   enum SymbolSectionNumber {
    144     IMAGE_SYM_DEBUG     = 0xFFFE,
    145     IMAGE_SYM_ABSOLUTE  = 0xFFFF,
    146     IMAGE_SYM_UNDEFINED = 0
    147   };
    148 
    149   /// Storage class tells where and what the symbol represents
    150   enum SymbolStorageClass {
    151     SSC_Invalid = 0xff,
    152 
    153     IMAGE_SYM_CLASS_END_OF_FUNCTION  = -1,  ///< Physical end of function
    154     IMAGE_SYM_CLASS_NULL             = 0,   ///< No symbol
    155     IMAGE_SYM_CLASS_AUTOMATIC        = 1,   ///< Stack variable
    156     IMAGE_SYM_CLASS_EXTERNAL         = 2,   ///< External symbol
    157     IMAGE_SYM_CLASS_STATIC           = 3,   ///< Static
    158     IMAGE_SYM_CLASS_REGISTER         = 4,   ///< Register variable
    159     IMAGE_SYM_CLASS_EXTERNAL_DEF     = 5,   ///< External definition
    160     IMAGE_SYM_CLASS_LABEL            = 6,   ///< Label
    161     IMAGE_SYM_CLASS_UNDEFINED_LABEL  = 7,   ///< Undefined label
    162     IMAGE_SYM_CLASS_MEMBER_OF_STRUCT = 8,   ///< Member of structure
    163     IMAGE_SYM_CLASS_ARGUMENT         = 9,   ///< Function argument
    164     IMAGE_SYM_CLASS_STRUCT_TAG       = 10,  ///< Structure tag
    165     IMAGE_SYM_CLASS_MEMBER_OF_UNION  = 11,  ///< Member of union
    166     IMAGE_SYM_CLASS_UNION_TAG        = 12,  ///< Union tag
    167     IMAGE_SYM_CLASS_TYPE_DEFINITION  = 13,  ///< Type definition
    168     IMAGE_SYM_CLASS_UNDEFINED_STATIC = 14,  ///< Undefined static
    169     IMAGE_SYM_CLASS_ENUM_TAG         = 15,  ///< Enumeration tag
    170     IMAGE_SYM_CLASS_MEMBER_OF_ENUM   = 16,  ///< Member of enumeration
    171     IMAGE_SYM_CLASS_REGISTER_PARAM   = 17,  ///< Register parameter
    172     IMAGE_SYM_CLASS_BIT_FIELD        = 18,  ///< Bit field
    173     /// ".bb" or ".eb" - beginning or end of block
    174     IMAGE_SYM_CLASS_BLOCK            = 100,
    175     /// ".bf" or ".ef" - beginning or end of function
    176     IMAGE_SYM_CLASS_FUNCTION         = 101,
    177     IMAGE_SYM_CLASS_END_OF_STRUCT    = 102, ///< End of structure
    178     IMAGE_SYM_CLASS_FILE             = 103, ///< File name
    179     /// Line number, reformatted as symbol
    180     IMAGE_SYM_CLASS_SECTION          = 104,
    181     IMAGE_SYM_CLASS_WEAK_EXTERNAL    = 105, ///< Duplicate tag
    182     /// External symbol in dmert public lib
    183     IMAGE_SYM_CLASS_CLR_TOKEN        = 107
    184   };
    185 
    186   enum SymbolBaseType {
    187     IMAGE_SYM_TYPE_NULL   = 0,  ///< No type information or unknown base type.
    188     IMAGE_SYM_TYPE_VOID   = 1,  ///< Used with void pointers and functions.
    189     IMAGE_SYM_TYPE_CHAR   = 2,  ///< A character (signed byte).
    190     IMAGE_SYM_TYPE_SHORT  = 3,  ///< A 2-byte signed integer.
    191     IMAGE_SYM_TYPE_INT    = 4,  ///< A natural integer type on the target.
    192     IMAGE_SYM_TYPE_LONG   = 5,  ///< A 4-byte signed integer.
    193     IMAGE_SYM_TYPE_FLOAT  = 6,  ///< A 4-byte floating-point number.
    194     IMAGE_SYM_TYPE_DOUBLE = 7,  ///< An 8-byte floating-point number.
    195     IMAGE_SYM_TYPE_STRUCT = 8,  ///< A structure.
    196     IMAGE_SYM_TYPE_UNION  = 9,  ///< An union.
    197     IMAGE_SYM_TYPE_ENUM   = 10, ///< An enumerated type.
    198     IMAGE_SYM_TYPE_MOE    = 11, ///< A member of enumeration (a specific value).
    199     IMAGE_SYM_TYPE_BYTE   = 12, ///< A byte; unsigned 1-byte integer.
    200     IMAGE_SYM_TYPE_WORD   = 13, ///< A word; unsigned 2-byte integer.
    201     IMAGE_SYM_TYPE_UINT   = 14, ///< An unsigned integer of natural size.
    202     IMAGE_SYM_TYPE_DWORD  = 15  ///< An unsigned 4-byte integer.
    203   };
    204 
    205   enum SymbolComplexType {
    206     IMAGE_SYM_DTYPE_NULL     = 0, ///< No complex type; simple scalar variable.
    207     IMAGE_SYM_DTYPE_POINTER  = 1, ///< A pointer to base type.
    208     IMAGE_SYM_DTYPE_FUNCTION = 2, ///< A function that returns a base type.
    209     IMAGE_SYM_DTYPE_ARRAY    = 3, ///< An array of base type.
    210 
    211     /// Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT))
    212     SCT_COMPLEX_TYPE_SHIFT   = 4
    213   };
    214 
    215   enum AuxSymbolType {
    216     IMAGE_AUX_SYMBOL_TYPE_TOKEN_DEF = 1
    217   };
    218 
    219   struct section {
    220     char     Name[NameSize];
    221     uint32_t VirtualSize;
    222     uint32_t VirtualAddress;
    223     uint32_t SizeOfRawData;
    224     uint32_t PointerToRawData;
    225     uint32_t PointerToRelocations;
    226     uint32_t PointerToLineNumbers;
    227     uint16_t NumberOfRelocations;
    228     uint16_t NumberOfLineNumbers;
    229     uint32_t Characteristics;
    230   };
    231 
    232   enum SectionCharacteristics : uint32_t {
    233     SC_Invalid = 0xffffffff,
    234 
    235     IMAGE_SCN_TYPE_NO_PAD            = 0x00000008,
    236     IMAGE_SCN_CNT_CODE               = 0x00000020,
    237     IMAGE_SCN_CNT_INITIALIZED_DATA   = 0x00000040,
    238     IMAGE_SCN_CNT_UNINITIALIZED_DATA = 0x00000080,
    239     IMAGE_SCN_LNK_OTHER              = 0x00000100,
    240     IMAGE_SCN_LNK_INFO               = 0x00000200,
    241     IMAGE_SCN_LNK_REMOVE             = 0x00000800,
    242     IMAGE_SCN_LNK_COMDAT             = 0x00001000,
    243     IMAGE_SCN_GPREL                  = 0x00008000,
    244     IMAGE_SCN_MEM_PURGEABLE          = 0x00020000,
    245     IMAGE_SCN_MEM_16BIT              = 0x00020000,
    246     IMAGE_SCN_MEM_LOCKED             = 0x00040000,
    247     IMAGE_SCN_MEM_PRELOAD            = 0x00080000,
    248     IMAGE_SCN_ALIGN_1BYTES           = 0x00100000,
    249     IMAGE_SCN_ALIGN_2BYTES           = 0x00200000,
    250     IMAGE_SCN_ALIGN_4BYTES           = 0x00300000,
    251     IMAGE_SCN_ALIGN_8BYTES           = 0x00400000,
    252     IMAGE_SCN_ALIGN_16BYTES          = 0x00500000,
    253     IMAGE_SCN_ALIGN_32BYTES          = 0x00600000,
    254     IMAGE_SCN_ALIGN_64BYTES          = 0x00700000,
    255     IMAGE_SCN_ALIGN_128BYTES         = 0x00800000,
    256     IMAGE_SCN_ALIGN_256BYTES         = 0x00900000,
    257     IMAGE_SCN_ALIGN_512BYTES         = 0x00A00000,
    258     IMAGE_SCN_ALIGN_1024BYTES        = 0x00B00000,
    259     IMAGE_SCN_ALIGN_2048BYTES        = 0x00C00000,
    260     IMAGE_SCN_ALIGN_4096BYTES        = 0x00D00000,
    261     IMAGE_SCN_ALIGN_8192BYTES        = 0x00E00000,
    262     IMAGE_SCN_LNK_NRELOC_OVFL        = 0x01000000,
    263     IMAGE_SCN_MEM_DISCARDABLE        = 0x02000000,
    264     IMAGE_SCN_MEM_NOT_CACHED         = 0x04000000,
    265     IMAGE_SCN_MEM_NOT_PAGED          = 0x08000000,
    266     IMAGE_SCN_MEM_SHARED             = 0x10000000,
    267     IMAGE_SCN_MEM_EXECUTE            = 0x20000000,
    268     IMAGE_SCN_MEM_READ               = 0x40000000,
    269     IMAGE_SCN_MEM_WRITE              = 0x80000000
    270   };
    271 
    272   struct relocation {
    273     uint32_t VirtualAddress;
    274     uint32_t SymbolTableIndex;
    275     uint16_t Type;
    276   };
    277 
    278   enum RelocationTypeI386 {
    279     IMAGE_REL_I386_ABSOLUTE = 0x0000,
    280     IMAGE_REL_I386_DIR16    = 0x0001,
    281     IMAGE_REL_I386_REL16    = 0x0002,
    282     IMAGE_REL_I386_DIR32    = 0x0006,
    283     IMAGE_REL_I386_DIR32NB  = 0x0007,
    284     IMAGE_REL_I386_SEG12    = 0x0009,
    285     IMAGE_REL_I386_SECTION  = 0x000A,
    286     IMAGE_REL_I386_SECREL   = 0x000B,
    287     IMAGE_REL_I386_TOKEN    = 0x000C,
    288     IMAGE_REL_I386_SECREL7  = 0x000D,
    289     IMAGE_REL_I386_REL32    = 0x0014
    290   };
    291 
    292   enum RelocationTypeAMD64 {
    293     IMAGE_REL_AMD64_ABSOLUTE  = 0x0000,
    294     IMAGE_REL_AMD64_ADDR64    = 0x0001,
    295     IMAGE_REL_AMD64_ADDR32    = 0x0002,
    296     IMAGE_REL_AMD64_ADDR32NB  = 0x0003,
    297     IMAGE_REL_AMD64_REL32     = 0x0004,
    298     IMAGE_REL_AMD64_REL32_1   = 0x0005,
    299     IMAGE_REL_AMD64_REL32_2   = 0x0006,
    300     IMAGE_REL_AMD64_REL32_3   = 0x0007,
    301     IMAGE_REL_AMD64_REL32_4   = 0x0008,
    302     IMAGE_REL_AMD64_REL32_5   = 0x0009,
    303     IMAGE_REL_AMD64_SECTION   = 0x000A,
    304     IMAGE_REL_AMD64_SECREL    = 0x000B,
    305     IMAGE_REL_AMD64_SECREL7   = 0x000C,
    306     IMAGE_REL_AMD64_TOKEN     = 0x000D,
    307     IMAGE_REL_AMD64_SREL32    = 0x000E,
    308     IMAGE_REL_AMD64_PAIR      = 0x000F,
    309     IMAGE_REL_AMD64_SSPAN32   = 0x0010
    310   };
    311 
    312   enum RelocationTypesARM {
    313     IMAGE_REL_ARM_ABSOLUTE  = 0x0000,
    314     IMAGE_REL_ARM_ADDR32    = 0x0001,
    315     IMAGE_REL_ARM_ADDR32NB  = 0x0002,
    316     IMAGE_REL_ARM_BRANCH24  = 0x0003,
    317     IMAGE_REL_ARM_BRANCH11  = 0x0004,
    318     IMAGE_REL_ARM_TOKEN     = 0x0005,
    319     IMAGE_REL_ARM_BLX24     = 0x0008,
    320     IMAGE_REL_ARM_BLX11     = 0x0009,
    321     IMAGE_REL_ARM_SECTION   = 0x000E,
    322     IMAGE_REL_ARM_SECREL    = 0x000F,
    323     IMAGE_REL_ARM_MOV32A    = 0x0010,
    324     IMAGE_REL_ARM_MOV32T    = 0x0011,
    325     IMAGE_REL_ARM_BRANCH20T = 0x0012,
    326     IMAGE_REL_ARM_BRANCH24T = 0x0014,
    327     IMAGE_REL_ARM_BLX23T    = 0x0015
    328   };
    329 
    330   enum COMDATType {
    331     IMAGE_COMDAT_SELECT_NODUPLICATES = 1,
    332     IMAGE_COMDAT_SELECT_ANY,
    333     IMAGE_COMDAT_SELECT_SAME_SIZE,
    334     IMAGE_COMDAT_SELECT_EXACT_MATCH,
    335     IMAGE_COMDAT_SELECT_ASSOCIATIVE,
    336     IMAGE_COMDAT_SELECT_LARGEST,
    337     IMAGE_COMDAT_SELECT_NEWEST
    338   };
    339 
    340   // Auxiliary Symbol Formats
    341   struct AuxiliaryFunctionDefinition {
    342     uint32_t TagIndex;
    343     uint32_t TotalSize;
    344     uint32_t PointerToLinenumber;
    345     uint32_t PointerToNextFunction;
    346     char     unused[2];
    347   };
    348 
    349   struct AuxiliarybfAndefSymbol {
    350     uint8_t  unused1[4];
    351     uint16_t Linenumber;
    352     uint8_t  unused2[6];
    353     uint32_t PointerToNextFunction;
    354     uint8_t  unused3[2];
    355   };
    356 
    357   struct AuxiliaryWeakExternal {
    358     uint32_t TagIndex;
    359     uint32_t Characteristics;
    360     uint8_t  unused[10];
    361   };
    362 
    363   /// These are not documented in the spec, but are located in WinNT.h.
    364   enum WeakExternalCharacteristics {
    365     IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY = 1,
    366     IMAGE_WEAK_EXTERN_SEARCH_LIBRARY   = 2,
    367     IMAGE_WEAK_EXTERN_SEARCH_ALIAS     = 3
    368   };
    369 
    370   struct AuxiliaryFile {
    371     uint8_t FileName[18];
    372   };
    373 
    374   struct AuxiliarySectionDefinition {
    375     uint32_t Length;
    376     uint16_t NumberOfRelocations;
    377     uint16_t NumberOfLinenumbers;
    378     uint32_t CheckSum;
    379     uint16_t Number;
    380     uint8_t  Selection;
    381     char     unused[3];
    382   };
    383 
    384   struct AuxiliaryCLRToken {
    385     uint8_t  AuxType;
    386     uint8_t  unused1;
    387     uint32_t SymbolTableIndex;
    388     char     unused2[12];
    389   };
    390 
    391   union Auxiliary {
    392     AuxiliaryFunctionDefinition FunctionDefinition;
    393     AuxiliarybfAndefSymbol      bfAndefSymbol;
    394     AuxiliaryWeakExternal       WeakExternal;
    395     AuxiliaryFile               File;
    396     AuxiliarySectionDefinition  SectionDefinition;
    397   };
    398 
    399   /// @brief The Import Directory Table.
    400   ///
    401   /// There is a single array of these and one entry per imported DLL.
    402   struct ImportDirectoryTableEntry {
    403     uint32_t ImportLookupTableRVA;
    404     uint32_t TimeDateStamp;
    405     uint32_t ForwarderChain;
    406     uint32_t NameRVA;
    407     uint32_t ImportAddressTableRVA;
    408   };
    409 
    410   /// @brief The PE32 Import Lookup Table.
    411   ///
    412   /// There is an array of these for each imported DLL. It represents either
    413   /// the ordinal to import from the target DLL, or a name to lookup and import
    414   /// from the target DLL.
    415   ///
    416   /// This also happens to be the same format used by the Import Address Table
    417   /// when it is initially written out to the image.
    418   struct ImportLookupTableEntry32 {
    419     uint32_t data;
    420 
    421     /// @brief Is this entry specified by ordinal, or name?
    422     bool isOrdinal() const { return data & 0x80000000; }
    423 
    424     /// @brief Get the ordinal value of this entry. isOrdinal must be true.
    425     uint16_t getOrdinal() const {
    426       assert(isOrdinal() && "ILT entry is not an ordinal!");
    427       return data & 0xFFFF;
    428     }
    429 
    430     /// @brief Set the ordinal value and set isOrdinal to true.
    431     void setOrdinal(uint16_t o) {
    432       data = o;
    433       data |= 0x80000000;
    434     }
    435 
    436     /// @brief Get the Hint/Name entry RVA. isOrdinal must be false.
    437     uint32_t getHintNameRVA() const {
    438       assert(!isOrdinal() && "ILT entry is not a Hint/Name RVA!");
    439       return data;
    440     }
    441 
    442     /// @brief Set the Hint/Name entry RVA and set isOrdinal to false.
    443     void setHintNameRVA(uint32_t rva) { data = rva; }
    444   };
    445 
    446   /// @brief The DOS compatible header at the front of all PEs.
    447   struct DOSHeader {
    448     uint16_t Magic;
    449     uint16_t UsedBytesInTheLastPage;
    450     uint16_t FileSizeInPages;
    451     uint16_t NumberOfRelocationItems;
    452     uint16_t HeaderSizeInParagraphs;
    453     uint16_t MinimumExtraParagraphs;
    454     uint16_t MaximumExtraParagraphs;
    455     uint16_t InitialRelativeSS;
    456     uint16_t InitialSP;
    457     uint16_t Checksum;
    458     uint16_t InitialIP;
    459     uint16_t InitialRelativeCS;
    460     uint16_t AddressOfRelocationTable;
    461     uint16_t OverlayNumber;
    462     uint16_t Reserved[4];
    463     uint16_t OEMid;
    464     uint16_t OEMinfo;
    465     uint16_t Reserved2[10];
    466     uint32_t AddressOfNewExeHeader;
    467   };
    468 
    469   struct PE32Header {
    470     enum {
    471       PE32 = 0x10b,
    472       PE32_PLUS = 0x20b
    473     };
    474 
    475     uint16_t Magic;
    476     uint8_t  MajorLinkerVersion;
    477     uint8_t  MinorLinkerVersion;
    478     uint32_t SizeOfCode;
    479     uint32_t SizeOfInitializedData;
    480     uint32_t SizeOfUninitializedData;
    481     uint32_t AddressOfEntryPoint; // RVA
    482     uint32_t BaseOfCode; // RVA
    483     uint32_t BaseOfData; // RVA
    484     uint32_t ImageBase;
    485     uint32_t SectionAlignment;
    486     uint32_t FileAlignment;
    487     uint16_t MajorOperatingSystemVersion;
    488     uint16_t MinorOperatingSystemVersion;
    489     uint16_t MajorImageVersion;
    490     uint16_t MinorImageVersion;
    491     uint16_t MajorSubsystemVersion;
    492     uint16_t MinorSubsystemVersion;
    493     uint32_t Win32VersionValue;
    494     uint32_t SizeOfImage;
    495     uint32_t SizeOfHeaders;
    496     uint32_t CheckSum;
    497     uint16_t Subsystem;
    498     uint16_t DLLCharacteristics;
    499     uint32_t SizeOfStackReserve;
    500     uint32_t SizeOfStackCommit;
    501     uint32_t SizeOfHeapReserve;
    502     uint32_t SizeOfHeapCommit;
    503     uint32_t LoaderFlags;
    504     uint32_t NumberOfRvaAndSize;
    505   };
    506 
    507   struct DataDirectory {
    508     uint32_t RelativeVirtualAddress;
    509     uint32_t Size;
    510   };
    511 
    512   enum DataDirectoryIndex {
    513     EXPORT_TABLE = 0,
    514     IMPORT_TABLE,
    515     RESOURCE_TABLE,
    516     EXCEPTION_TABLE,
    517     CERTIFICATE_TABLE,
    518     BASE_RELOCATION_TABLE,
    519     DEBUG,
    520     ARCHITECTURE,
    521     GLOBAL_PTR,
    522     TLS_TABLE,
    523     LOAD_CONFIG_TABLE,
    524     BOUND_IMPORT,
    525     IAT,
    526     DELAY_IMPORT_DESCRIPTOR,
    527     CLR_RUNTIME_HEADER
    528   };
    529 
    530   enum WindowsSubsystem {
    531     IMAGE_SUBSYSTEM_UNKNOWN = 0, ///< An unknown subsystem.
    532     IMAGE_SUBSYSTEM_NATIVE = 1, ///< Device drivers and native Windows processes
    533     IMAGE_SUBSYSTEM_WINDOWS_GUI = 2, ///< The Windows GUI subsystem.
    534     IMAGE_SUBSYSTEM_WINDOWS_CUI = 3, ///< The Windows character subsystem.
    535     IMAGE_SUBSYSTEM_OS2_CUI = 5, ///< The OS/2 character subsytem.
    536     IMAGE_SUBSYSTEM_POSIX_CUI = 7, ///< The POSIX character subsystem.
    537     IMAGE_SUBSYSTEM_NATIVE_WINDOWS = 8, ///< Native Windows 9x driver.
    538     IMAGE_SUBSYSTEM_WINDOWS_CE_GUI = 9, ///< Windows CE.
    539     IMAGE_SUBSYSTEM_EFI_APPLICATION = 10, ///< An EFI application.
    540     IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER = 11, ///< An EFI driver with boot
    541                                                   ///  services.
    542     IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER = 12, ///< An EFI driver with run-time
    543                                              ///  services.
    544     IMAGE_SUBSYSTEM_EFI_ROM = 13, ///< An EFI ROM image.
    545     IMAGE_SUBSYSTEM_XBOX = 14, ///< XBOX.
    546     IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION = 16 ///< A BCD application.
    547   };
    548 
    549   enum DLLCharacteristics {
    550     /// ASLR with 64 bit address space.
    551     IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA = 0x0020,
    552     /// DLL can be relocated at load time.
    553     IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE = 0x0040,
    554     /// Code integrity checks are enforced.
    555     IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY = 0x0080,
    556     ///< Image is NX compatible.
    557     IMAGE_DLL_CHARACTERISTICS_NX_COMPAT = 0x0100,
    558     /// Isolation aware, but do not isolate the image.
    559     IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION = 0x0200,
    560     /// Does not use structured exception handling (SEH). No SEH handler may be
    561     /// called in this image.
    562     IMAGE_DLL_CHARACTERISTICS_NO_SEH = 0x0400,
    563     /// Do not bind the image.
    564     IMAGE_DLL_CHARACTERISTICS_NO_BIND = 0x0800,
    565     ///< Image should execute in an AppContainer.
    566     IMAGE_DLL_CHARACTERISTICS_APPCONTAINER = 0x1000,
    567     ///< A WDM driver.
    568     IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER = 0x2000,
    569     ///< Image supports Control Flow Guard.
    570     IMAGE_DLL_CHARACTERISTICS_GUARD_CF = 0x4000,
    571     /// Terminal Server aware.
    572     IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE = 0x8000
    573   };
    574 
    575   enum DebugType {
    576     IMAGE_DEBUG_TYPE_UNKNOWN       = 0,
    577     IMAGE_DEBUG_TYPE_COFF          = 1,
    578     IMAGE_DEBUG_TYPE_CODEVIEW      = 2,
    579     IMAGE_DEBUG_TYPE_FPO           = 3,
    580     IMAGE_DEBUG_TYPE_MISC          = 4,
    581     IMAGE_DEBUG_TYPE_EXCEPTION     = 5,
    582     IMAGE_DEBUG_TYPE_FIXUP         = 6,
    583     IMAGE_DEBUG_TYPE_OMAP_TO_SRC   = 7,
    584     IMAGE_DEBUG_TYPE_OMAP_FROM_SRC = 8,
    585     IMAGE_DEBUG_TYPE_BORLAND       = 9,
    586     IMAGE_DEBUG_TYPE_CLSID         = 11
    587   };
    588 
    589   enum BaseRelocationType {
    590     IMAGE_REL_BASED_ABSOLUTE       = 0,
    591     IMAGE_REL_BASED_HIGH           = 1,
    592     IMAGE_REL_BASED_LOW            = 2,
    593     IMAGE_REL_BASED_HIGHLOW        = 3,
    594     IMAGE_REL_BASED_HIGHADJ        = 4,
    595     IMAGE_REL_BASED_MIPS_JMPADDR   = 5,
    596     IMAGE_REL_BASED_ARM_MOV32A     = 5,
    597     IMAGE_REL_BASED_ARM_MOV32T     = 7,
    598     IMAGE_REL_BASED_MIPS_JMPADDR16 = 9,
    599     IMAGE_REL_BASED_DIR64          = 10
    600   };
    601 
    602   enum ImportType {
    603     IMPORT_CODE  = 0,
    604     IMPORT_DATA  = 1,
    605     IMPORT_CONST = 2
    606   };
    607 
    608   enum ImportNameType {
    609     /// Import is by ordinal. This indicates that the value in the Ordinal/Hint
    610     /// field of the import header is the import's ordinal. If this constant is
    611     /// not specified, then the Ordinal/Hint field should always be interpreted
    612     /// as the import's hint.
    613     IMPORT_ORDINAL         = 0,
    614     /// The import name is identical to the public symbol name
    615     IMPORT_NAME            = 1,
    616     /// The import name is the public symbol name, but skipping the leading ?,
    617     /// @, or optionally _.
    618     IMPORT_NAME_NOPREFIX   = 2,
    619     /// The import name is the public symbol name, but skipping the leading ?,
    620     /// @, or optionally _, and truncating at the first @.
    621     IMPORT_NAME_UNDECORATE = 3
    622   };
    623 
    624   struct ImportHeader {
    625     uint16_t Sig1; ///< Must be IMAGE_FILE_MACHINE_UNKNOWN (0).
    626     uint16_t Sig2; ///< Must be 0xFFFF.
    627     uint16_t Version;
    628     uint16_t Machine;
    629     uint32_t TimeDateStamp;
    630     uint32_t SizeOfData;
    631     uint16_t OrdinalHint;
    632     uint16_t TypeInfo;
    633 
    634     ImportType getType() const {
    635       return static_cast<ImportType>(TypeInfo & 0x3);
    636     }
    637 
    638     ImportNameType getNameType() const {
    639       return static_cast<ImportNameType>((TypeInfo & 0x1C) >> 3);
    640     }
    641   };
    642 
    643   enum CodeViewLineTableIdentifiers {
    644     DEBUG_SECTION_MAGIC           = 0x4,
    645     DEBUG_LINE_TABLE_SUBSECTION   = 0xF2,
    646     DEBUG_STRING_TABLE_SUBSECTION = 0xF3,
    647     DEBUG_INDEX_SUBSECTION        = 0xF4
    648   };
    649 
    650   inline bool isReservedSectionNumber(int N) {
    651     return N == IMAGE_SYM_UNDEFINED || N > MaxNumberOfSections;
    652   }
    653 
    654 } // End namespace COFF.
    655 } // End namespace llvm.
    656 
    657 #endif
    658