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