LLVM API Documentation

Support/COFF.h
Go to the documentation of this file.
00001 //===-- llvm/Support/COFF.h -------------------------------------*- 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 contains an definitions used in Windows COFF Files.
00011 //
00012 // Structures and enums defined within this file where created using
00013 // information from Microsoft's publicly available PE/COFF format document:
00014 //
00015 // Microsoft Portable Executable and Common Object File Format Specification
00016 // Revision 8.1 - February 15, 2008
00017 //
00018 // As of 5/2/2010, hosted by Microsoft at:
00019 // http://www.microsoft.com/whdc/system/platform/firmware/pecoff.mspx
00020 //
00021 //===----------------------------------------------------------------------===//
00022 
00023 #ifndef LLVM_SUPPORT_COFF_H
00024 #define LLVM_SUPPORT_COFF_H
00025 
00026 #include "llvm/Support/DataTypes.h"
00027 #include <cassert>
00028 #include <cstring>
00029 
00030 namespace llvm {
00031 namespace COFF {
00032 
00033   // The maximum number of sections that a COFF object can have (inclusive).
00034   const int32_t MaxNumberOfSections16 = 65279;
00035 
00036   // The PE signature bytes that follows the DOS stub header.
00037   static const char PEMagic[] = { 'P', 'E', '\0', '\0' };
00038 
00039   static const char BigObjMagic[] = {
00040       '\xc7', '\xa1', '\xba', '\xd1', '\xee', '\xba', '\xa9', '\x4b',
00041       '\xaf', '\x20', '\xfa', '\xf6', '\x6a', '\xa4', '\xdc', '\xb8',
00042   };
00043 
00044   // Sizes in bytes of various things in the COFF format.
00045   enum {
00046     Header16Size   = 20,
00047     Header32Size   = 56,
00048     NameSize       = 8,
00049     Symbol16Size   = 18,
00050     Symbol32Size   = 20,
00051     SectionSize    = 40,
00052     RelocationSize = 10
00053   };
00054 
00055   struct header {
00056     uint16_t Machine;
00057     int32_t  NumberOfSections;
00058     uint32_t TimeDateStamp;
00059     uint32_t PointerToSymbolTable;
00060     uint32_t NumberOfSymbols;
00061     uint16_t SizeOfOptionalHeader;
00062     uint16_t Characteristics;
00063   };
00064 
00065   struct BigObjHeader {
00066     enum : uint16_t { MinBigObjectVersion = 2 };
00067 
00068     uint16_t Sig1; ///< Must be IMAGE_FILE_MACHINE_UNKNOWN (0).
00069     uint16_t Sig2; ///< Must be 0xFFFF.
00070     uint16_t Version;
00071     uint16_t Machine;
00072     uint32_t TimeDateStamp;
00073     uint8_t  UUID[16];
00074     uint32_t unused1;
00075     uint32_t unused2;
00076     uint32_t unused3;
00077     uint32_t unused4;
00078     uint32_t NumberOfSections;
00079     uint32_t PointerToSymbolTable;
00080     uint32_t NumberOfSymbols;
00081   };
00082 
00083   enum MachineTypes {
00084     MT_Invalid = 0xffff,
00085 
00086     IMAGE_FILE_MACHINE_UNKNOWN   = 0x0,
00087     IMAGE_FILE_MACHINE_AM33      = 0x13,
00088     IMAGE_FILE_MACHINE_AMD64     = 0x8664,
00089     IMAGE_FILE_MACHINE_ARM       = 0x1C0,
00090     IMAGE_FILE_MACHINE_ARMNT     = 0x1C4,
00091     IMAGE_FILE_MACHINE_EBC       = 0xEBC,
00092     IMAGE_FILE_MACHINE_I386      = 0x14C,
00093     IMAGE_FILE_MACHINE_IA64      = 0x200,
00094     IMAGE_FILE_MACHINE_M32R      = 0x9041,
00095     IMAGE_FILE_MACHINE_MIPS16    = 0x266,
00096     IMAGE_FILE_MACHINE_MIPSFPU   = 0x366,
00097     IMAGE_FILE_MACHINE_MIPSFPU16 = 0x466,
00098     IMAGE_FILE_MACHINE_POWERPC   = 0x1F0,
00099     IMAGE_FILE_MACHINE_POWERPCFP = 0x1F1,
00100     IMAGE_FILE_MACHINE_R4000     = 0x166,
00101     IMAGE_FILE_MACHINE_SH3       = 0x1A2,
00102     IMAGE_FILE_MACHINE_SH3DSP    = 0x1A3,
00103     IMAGE_FILE_MACHINE_SH4       = 0x1A6,
00104     IMAGE_FILE_MACHINE_SH5       = 0x1A8,
00105     IMAGE_FILE_MACHINE_THUMB     = 0x1C2,
00106     IMAGE_FILE_MACHINE_WCEMIPSV2 = 0x169
00107   };
00108 
00109   enum Characteristics {
00110     C_Invalid = 0,
00111 
00112     /// The file does not contain base relocations and must be loaded at its
00113     /// preferred base. If this cannot be done, the loader will error.
00114     IMAGE_FILE_RELOCS_STRIPPED         = 0x0001,
00115     /// The file is valid and can be run.
00116     IMAGE_FILE_EXECUTABLE_IMAGE        = 0x0002,
00117     /// COFF line numbers have been stripped. This is deprecated and should be
00118     /// 0.
00119     IMAGE_FILE_LINE_NUMS_STRIPPED      = 0x0004,
00120     /// COFF symbol table entries for local symbols have been removed. This is
00121     /// deprecated and should be 0.
00122     IMAGE_FILE_LOCAL_SYMS_STRIPPED     = 0x0008,
00123     /// Aggressively trim working set. This is deprecated and must be 0.
00124     IMAGE_FILE_AGGRESSIVE_WS_TRIM      = 0x0010,
00125     /// Image can handle > 2GiB addresses.
00126     IMAGE_FILE_LARGE_ADDRESS_AWARE     = 0x0020,
00127     /// Little endian: the LSB precedes the MSB in memory. This is deprecated
00128     /// and should be 0.
00129     IMAGE_FILE_BYTES_REVERSED_LO       = 0x0080,
00130     /// Machine is based on a 32bit word architecture.
00131     IMAGE_FILE_32BIT_MACHINE           = 0x0100,
00132     /// Debugging info has been removed.
00133     IMAGE_FILE_DEBUG_STRIPPED          = 0x0200,
00134     /// If the image is on removable media, fully load it and copy it to swap.
00135     IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP = 0x0400,
00136     /// If the image is on network media, fully load it and copy it to swap.
00137     IMAGE_FILE_NET_RUN_FROM_SWAP       = 0x0800,
00138     /// The image file is a system file, not a user program.
00139     IMAGE_FILE_SYSTEM                  = 0x1000,
00140     /// The image file is a DLL.
00141     IMAGE_FILE_DLL                     = 0x2000,
00142     /// This file should only be run on a uniprocessor machine.
00143     IMAGE_FILE_UP_SYSTEM_ONLY          = 0x4000,
00144     /// Big endian: the MSB precedes the LSB in memory. This is deprecated
00145     /// and should be 0.
00146     IMAGE_FILE_BYTES_REVERSED_HI       = 0x8000
00147   };
00148 
00149   struct symbol {
00150     char     Name[NameSize];
00151     uint32_t Value;
00152     int32_t  SectionNumber;
00153     uint16_t Type;
00154     uint8_t  StorageClass;
00155     uint8_t  NumberOfAuxSymbols;
00156   };
00157 
00158   enum SymbolFlags {
00159     SF_TypeMask = 0x0000FFFF,
00160     SF_TypeShift = 0,
00161 
00162     SF_ClassMask = 0x00FF0000,
00163     SF_ClassShift = 16,
00164 
00165     SF_WeakExternal = 0x01000000
00166   };
00167 
00168   enum SymbolSectionNumber : int32_t {
00169     IMAGE_SYM_DEBUG     = -2,
00170     IMAGE_SYM_ABSOLUTE  = -1,
00171     IMAGE_SYM_UNDEFINED = 0
00172   };
00173 
00174   /// Storage class tells where and what the symbol represents
00175   enum SymbolStorageClass {
00176     SSC_Invalid = 0xff,
00177 
00178     IMAGE_SYM_CLASS_END_OF_FUNCTION  = -1,  ///< Physical end of function
00179     IMAGE_SYM_CLASS_NULL             = 0,   ///< No symbol
00180     IMAGE_SYM_CLASS_AUTOMATIC        = 1,   ///< Stack variable
00181     IMAGE_SYM_CLASS_EXTERNAL         = 2,   ///< External symbol
00182     IMAGE_SYM_CLASS_STATIC           = 3,   ///< Static
00183     IMAGE_SYM_CLASS_REGISTER         = 4,   ///< Register variable
00184     IMAGE_SYM_CLASS_EXTERNAL_DEF     = 5,   ///< External definition
00185     IMAGE_SYM_CLASS_LABEL            = 6,   ///< Label
00186     IMAGE_SYM_CLASS_UNDEFINED_LABEL  = 7,   ///< Undefined label
00187     IMAGE_SYM_CLASS_MEMBER_OF_STRUCT = 8,   ///< Member of structure
00188     IMAGE_SYM_CLASS_ARGUMENT         = 9,   ///< Function argument
00189     IMAGE_SYM_CLASS_STRUCT_TAG       = 10,  ///< Structure tag
00190     IMAGE_SYM_CLASS_MEMBER_OF_UNION  = 11,  ///< Member of union
00191     IMAGE_SYM_CLASS_UNION_TAG        = 12,  ///< Union tag
00192     IMAGE_SYM_CLASS_TYPE_DEFINITION  = 13,  ///< Type definition
00193     IMAGE_SYM_CLASS_UNDEFINED_STATIC = 14,  ///< Undefined static
00194     IMAGE_SYM_CLASS_ENUM_TAG         = 15,  ///< Enumeration tag
00195     IMAGE_SYM_CLASS_MEMBER_OF_ENUM   = 16,  ///< Member of enumeration
00196     IMAGE_SYM_CLASS_REGISTER_PARAM   = 17,  ///< Register parameter
00197     IMAGE_SYM_CLASS_BIT_FIELD        = 18,  ///< Bit field
00198     /// ".bb" or ".eb" - beginning or end of block
00199     IMAGE_SYM_CLASS_BLOCK            = 100,
00200     /// ".bf" or ".ef" - beginning or end of function
00201     IMAGE_SYM_CLASS_FUNCTION         = 101,
00202     IMAGE_SYM_CLASS_END_OF_STRUCT    = 102, ///< End of structure
00203     IMAGE_SYM_CLASS_FILE             = 103, ///< File name
00204     /// Line number, reformatted as symbol
00205     IMAGE_SYM_CLASS_SECTION          = 104,
00206     IMAGE_SYM_CLASS_WEAK_EXTERNAL    = 105, ///< Duplicate tag
00207     /// External symbol in dmert public lib
00208     IMAGE_SYM_CLASS_CLR_TOKEN        = 107
00209   };
00210 
00211   enum SymbolBaseType {
00212     IMAGE_SYM_TYPE_NULL   = 0,  ///< No type information or unknown base type.
00213     IMAGE_SYM_TYPE_VOID   = 1,  ///< Used with void pointers and functions.
00214     IMAGE_SYM_TYPE_CHAR   = 2,  ///< A character (signed byte).
00215     IMAGE_SYM_TYPE_SHORT  = 3,  ///< A 2-byte signed integer.
00216     IMAGE_SYM_TYPE_INT    = 4,  ///< A natural integer type on the target.
00217     IMAGE_SYM_TYPE_LONG   = 5,  ///< A 4-byte signed integer.
00218     IMAGE_SYM_TYPE_FLOAT  = 6,  ///< A 4-byte floating-point number.
00219     IMAGE_SYM_TYPE_DOUBLE = 7,  ///< An 8-byte floating-point number.
00220     IMAGE_SYM_TYPE_STRUCT = 8,  ///< A structure.
00221     IMAGE_SYM_TYPE_UNION  = 9,  ///< An union.
00222     IMAGE_SYM_TYPE_ENUM   = 10, ///< An enumerated type.
00223     IMAGE_SYM_TYPE_MOE    = 11, ///< A member of enumeration (a specific value).
00224     IMAGE_SYM_TYPE_BYTE   = 12, ///< A byte; unsigned 1-byte integer.
00225     IMAGE_SYM_TYPE_WORD   = 13, ///< A word; unsigned 2-byte integer.
00226     IMAGE_SYM_TYPE_UINT   = 14, ///< An unsigned integer of natural size.
00227     IMAGE_SYM_TYPE_DWORD  = 15  ///< An unsigned 4-byte integer.
00228   };
00229 
00230   enum SymbolComplexType {
00231     IMAGE_SYM_DTYPE_NULL     = 0, ///< No complex type; simple scalar variable.
00232     IMAGE_SYM_DTYPE_POINTER  = 1, ///< A pointer to base type.
00233     IMAGE_SYM_DTYPE_FUNCTION = 2, ///< A function that returns a base type.
00234     IMAGE_SYM_DTYPE_ARRAY    = 3, ///< An array of base type.
00235 
00236     /// Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT))
00237     SCT_COMPLEX_TYPE_SHIFT   = 4
00238   };
00239 
00240   enum AuxSymbolType {
00241     IMAGE_AUX_SYMBOL_TYPE_TOKEN_DEF = 1
00242   };
00243 
00244   struct section {
00245     char     Name[NameSize];
00246     uint32_t VirtualSize;
00247     uint32_t VirtualAddress;
00248     uint32_t SizeOfRawData;
00249     uint32_t PointerToRawData;
00250     uint32_t PointerToRelocations;
00251     uint32_t PointerToLineNumbers;
00252     uint16_t NumberOfRelocations;
00253     uint16_t NumberOfLineNumbers;
00254     uint32_t Characteristics;
00255   };
00256 
00257   enum SectionCharacteristics : uint32_t {
00258     SC_Invalid = 0xffffffff,
00259 
00260     IMAGE_SCN_TYPE_NO_PAD            = 0x00000008,
00261     IMAGE_SCN_CNT_CODE               = 0x00000020,
00262     IMAGE_SCN_CNT_INITIALIZED_DATA   = 0x00000040,
00263     IMAGE_SCN_CNT_UNINITIALIZED_DATA = 0x00000080,
00264     IMAGE_SCN_LNK_OTHER              = 0x00000100,
00265     IMAGE_SCN_LNK_INFO               = 0x00000200,
00266     IMAGE_SCN_LNK_REMOVE             = 0x00000800,
00267     IMAGE_SCN_LNK_COMDAT             = 0x00001000,
00268     IMAGE_SCN_GPREL                  = 0x00008000,
00269     IMAGE_SCN_MEM_PURGEABLE          = 0x00020000,
00270     IMAGE_SCN_MEM_16BIT              = 0x00020000,
00271     IMAGE_SCN_MEM_LOCKED             = 0x00040000,
00272     IMAGE_SCN_MEM_PRELOAD            = 0x00080000,
00273     IMAGE_SCN_ALIGN_1BYTES           = 0x00100000,
00274     IMAGE_SCN_ALIGN_2BYTES           = 0x00200000,
00275     IMAGE_SCN_ALIGN_4BYTES           = 0x00300000,
00276     IMAGE_SCN_ALIGN_8BYTES           = 0x00400000,
00277     IMAGE_SCN_ALIGN_16BYTES          = 0x00500000,
00278     IMAGE_SCN_ALIGN_32BYTES          = 0x00600000,
00279     IMAGE_SCN_ALIGN_64BYTES          = 0x00700000,
00280     IMAGE_SCN_ALIGN_128BYTES         = 0x00800000,
00281     IMAGE_SCN_ALIGN_256BYTES         = 0x00900000,
00282     IMAGE_SCN_ALIGN_512BYTES         = 0x00A00000,
00283     IMAGE_SCN_ALIGN_1024BYTES        = 0x00B00000,
00284     IMAGE_SCN_ALIGN_2048BYTES        = 0x00C00000,
00285     IMAGE_SCN_ALIGN_4096BYTES        = 0x00D00000,
00286     IMAGE_SCN_ALIGN_8192BYTES        = 0x00E00000,
00287     IMAGE_SCN_LNK_NRELOC_OVFL        = 0x01000000,
00288     IMAGE_SCN_MEM_DISCARDABLE        = 0x02000000,
00289     IMAGE_SCN_MEM_NOT_CACHED         = 0x04000000,
00290     IMAGE_SCN_MEM_NOT_PAGED          = 0x08000000,
00291     IMAGE_SCN_MEM_SHARED             = 0x10000000,
00292     IMAGE_SCN_MEM_EXECUTE            = 0x20000000,
00293     IMAGE_SCN_MEM_READ               = 0x40000000,
00294     IMAGE_SCN_MEM_WRITE              = 0x80000000
00295   };
00296 
00297   struct relocation {
00298     uint32_t VirtualAddress;
00299     uint32_t SymbolTableIndex;
00300     uint16_t Type;
00301   };
00302 
00303   enum RelocationTypeI386 {
00304     IMAGE_REL_I386_ABSOLUTE = 0x0000,
00305     IMAGE_REL_I386_DIR16    = 0x0001,
00306     IMAGE_REL_I386_REL16    = 0x0002,
00307     IMAGE_REL_I386_DIR32    = 0x0006,
00308     IMAGE_REL_I386_DIR32NB  = 0x0007,
00309     IMAGE_REL_I386_SEG12    = 0x0009,
00310     IMAGE_REL_I386_SECTION  = 0x000A,
00311     IMAGE_REL_I386_SECREL   = 0x000B,
00312     IMAGE_REL_I386_TOKEN    = 0x000C,
00313     IMAGE_REL_I386_SECREL7  = 0x000D,
00314     IMAGE_REL_I386_REL32    = 0x0014
00315   };
00316 
00317   enum RelocationTypeAMD64 {
00318     IMAGE_REL_AMD64_ABSOLUTE  = 0x0000,
00319     IMAGE_REL_AMD64_ADDR64    = 0x0001,
00320     IMAGE_REL_AMD64_ADDR32    = 0x0002,
00321     IMAGE_REL_AMD64_ADDR32NB  = 0x0003,
00322     IMAGE_REL_AMD64_REL32     = 0x0004,
00323     IMAGE_REL_AMD64_REL32_1   = 0x0005,
00324     IMAGE_REL_AMD64_REL32_2   = 0x0006,
00325     IMAGE_REL_AMD64_REL32_3   = 0x0007,
00326     IMAGE_REL_AMD64_REL32_4   = 0x0008,
00327     IMAGE_REL_AMD64_REL32_5   = 0x0009,
00328     IMAGE_REL_AMD64_SECTION   = 0x000A,
00329     IMAGE_REL_AMD64_SECREL    = 0x000B,
00330     IMAGE_REL_AMD64_SECREL7   = 0x000C,
00331     IMAGE_REL_AMD64_TOKEN     = 0x000D,
00332     IMAGE_REL_AMD64_SREL32    = 0x000E,
00333     IMAGE_REL_AMD64_PAIR      = 0x000F,
00334     IMAGE_REL_AMD64_SSPAN32   = 0x0010
00335   };
00336 
00337   enum RelocationTypesARM {
00338     IMAGE_REL_ARM_ABSOLUTE  = 0x0000,
00339     IMAGE_REL_ARM_ADDR32    = 0x0001,
00340     IMAGE_REL_ARM_ADDR32NB  = 0x0002,
00341     IMAGE_REL_ARM_BRANCH24  = 0x0003,
00342     IMAGE_REL_ARM_BRANCH11  = 0x0004,
00343     IMAGE_REL_ARM_TOKEN     = 0x0005,
00344     IMAGE_REL_ARM_BLX24     = 0x0008,
00345     IMAGE_REL_ARM_BLX11     = 0x0009,
00346     IMAGE_REL_ARM_SECTION   = 0x000E,
00347     IMAGE_REL_ARM_SECREL    = 0x000F,
00348     IMAGE_REL_ARM_MOV32A    = 0x0010,
00349     IMAGE_REL_ARM_MOV32T    = 0x0011,
00350     IMAGE_REL_ARM_BRANCH20T = 0x0012,
00351     IMAGE_REL_ARM_BRANCH24T = 0x0014,
00352     IMAGE_REL_ARM_BLX23T    = 0x0015
00353   };
00354 
00355   enum COMDATType {
00356     IMAGE_COMDAT_SELECT_NODUPLICATES = 1,
00357     IMAGE_COMDAT_SELECT_ANY,
00358     IMAGE_COMDAT_SELECT_SAME_SIZE,
00359     IMAGE_COMDAT_SELECT_EXACT_MATCH,
00360     IMAGE_COMDAT_SELECT_ASSOCIATIVE,
00361     IMAGE_COMDAT_SELECT_LARGEST,
00362     IMAGE_COMDAT_SELECT_NEWEST
00363   };
00364 
00365   // Auxiliary Symbol Formats
00366   struct AuxiliaryFunctionDefinition {
00367     uint32_t TagIndex;
00368     uint32_t TotalSize;
00369     uint32_t PointerToLinenumber;
00370     uint32_t PointerToNextFunction;
00371     char     unused[2];
00372   };
00373 
00374   struct AuxiliarybfAndefSymbol {
00375     uint8_t  unused1[4];
00376     uint16_t Linenumber;
00377     uint8_t  unused2[6];
00378     uint32_t PointerToNextFunction;
00379     uint8_t  unused3[2];
00380   };
00381 
00382   struct AuxiliaryWeakExternal {
00383     uint32_t TagIndex;
00384     uint32_t Characteristics;
00385     uint8_t  unused[10];
00386   };
00387 
00388   /// These are not documented in the spec, but are located in WinNT.h.
00389   enum WeakExternalCharacteristics {
00390     IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY = 1,
00391     IMAGE_WEAK_EXTERN_SEARCH_LIBRARY   = 2,
00392     IMAGE_WEAK_EXTERN_SEARCH_ALIAS     = 3
00393   };
00394 
00395   struct AuxiliarySectionDefinition {
00396     uint32_t Length;
00397     uint16_t NumberOfRelocations;
00398     uint16_t NumberOfLinenumbers;
00399     uint32_t CheckSum;
00400     uint32_t Number;
00401     uint8_t  Selection;
00402     char     unused;
00403   };
00404 
00405   struct AuxiliaryCLRToken {
00406     uint8_t  AuxType;
00407     uint8_t  unused1;
00408     uint32_t SymbolTableIndex;
00409     char     unused2[12];
00410   };
00411 
00412   union Auxiliary {
00413     AuxiliaryFunctionDefinition FunctionDefinition;
00414     AuxiliarybfAndefSymbol      bfAndefSymbol;
00415     AuxiliaryWeakExternal       WeakExternal;
00416     AuxiliarySectionDefinition  SectionDefinition;
00417   };
00418 
00419   /// @brief The Import Directory Table.
00420   ///
00421   /// There is a single array of these and one entry per imported DLL.
00422   struct ImportDirectoryTableEntry {
00423     uint32_t ImportLookupTableRVA;
00424     uint32_t TimeDateStamp;
00425     uint32_t ForwarderChain;
00426     uint32_t NameRVA;
00427     uint32_t ImportAddressTableRVA;
00428   };
00429 
00430   /// @brief The PE32 Import Lookup Table.
00431   ///
00432   /// There is an array of these for each imported DLL. It represents either
00433   /// the ordinal to import from the target DLL, or a name to lookup and import
00434   /// from the target DLL.
00435   ///
00436   /// This also happens to be the same format used by the Import Address Table
00437   /// when it is initially written out to the image.
00438   struct ImportLookupTableEntry32 {
00439     uint32_t data;
00440 
00441     /// @brief Is this entry specified by ordinal, or name?
00442     bool isOrdinal() const { return data & 0x80000000; }
00443 
00444     /// @brief Get the ordinal value of this entry. isOrdinal must be true.
00445     uint16_t getOrdinal() const {
00446       assert(isOrdinal() && "ILT entry is not an ordinal!");
00447       return data & 0xFFFF;
00448     }
00449 
00450     /// @brief Set the ordinal value and set isOrdinal to true.
00451     void setOrdinal(uint16_t o) {
00452       data = o;
00453       data |= 0x80000000;
00454     }
00455 
00456     /// @brief Get the Hint/Name entry RVA. isOrdinal must be false.
00457     uint32_t getHintNameRVA() const {
00458       assert(!isOrdinal() && "ILT entry is not a Hint/Name RVA!");
00459       return data;
00460     }
00461 
00462     /// @brief Set the Hint/Name entry RVA and set isOrdinal to false.
00463     void setHintNameRVA(uint32_t rva) { data = rva; }
00464   };
00465 
00466   /// @brief The DOS compatible header at the front of all PEs.
00467   struct DOSHeader {
00468     uint16_t Magic;
00469     uint16_t UsedBytesInTheLastPage;
00470     uint16_t FileSizeInPages;
00471     uint16_t NumberOfRelocationItems;
00472     uint16_t HeaderSizeInParagraphs;
00473     uint16_t MinimumExtraParagraphs;
00474     uint16_t MaximumExtraParagraphs;
00475     uint16_t InitialRelativeSS;
00476     uint16_t InitialSP;
00477     uint16_t Checksum;
00478     uint16_t InitialIP;
00479     uint16_t InitialRelativeCS;
00480     uint16_t AddressOfRelocationTable;
00481     uint16_t OverlayNumber;
00482     uint16_t Reserved[4];
00483     uint16_t OEMid;
00484     uint16_t OEMinfo;
00485     uint16_t Reserved2[10];
00486     uint32_t AddressOfNewExeHeader;
00487   };
00488 
00489   struct PE32Header {
00490     enum {
00491       PE32 = 0x10b,
00492       PE32_PLUS = 0x20b
00493     };
00494 
00495     uint16_t Magic;
00496     uint8_t  MajorLinkerVersion;
00497     uint8_t  MinorLinkerVersion;
00498     uint32_t SizeOfCode;
00499     uint32_t SizeOfInitializedData;
00500     uint32_t SizeOfUninitializedData;
00501     uint32_t AddressOfEntryPoint; // RVA
00502     uint32_t BaseOfCode; // RVA
00503     uint32_t BaseOfData; // RVA
00504     uint32_t ImageBase;
00505     uint32_t SectionAlignment;
00506     uint32_t FileAlignment;
00507     uint16_t MajorOperatingSystemVersion;
00508     uint16_t MinorOperatingSystemVersion;
00509     uint16_t MajorImageVersion;
00510     uint16_t MinorImageVersion;
00511     uint16_t MajorSubsystemVersion;
00512     uint16_t MinorSubsystemVersion;
00513     uint32_t Win32VersionValue;
00514     uint32_t SizeOfImage;
00515     uint32_t SizeOfHeaders;
00516     uint32_t CheckSum;
00517     uint16_t Subsystem;
00518     uint16_t DLLCharacteristics;
00519     uint32_t SizeOfStackReserve;
00520     uint32_t SizeOfStackCommit;
00521     uint32_t SizeOfHeapReserve;
00522     uint32_t SizeOfHeapCommit;
00523     uint32_t LoaderFlags;
00524     uint32_t NumberOfRvaAndSize;
00525   };
00526 
00527   struct DataDirectory {
00528     uint32_t RelativeVirtualAddress;
00529     uint32_t Size;
00530   };
00531 
00532   enum DataDirectoryIndex {
00533     EXPORT_TABLE = 0,
00534     IMPORT_TABLE,
00535     RESOURCE_TABLE,
00536     EXCEPTION_TABLE,
00537     CERTIFICATE_TABLE,
00538     BASE_RELOCATION_TABLE,
00539     DEBUG,
00540     ARCHITECTURE,
00541     GLOBAL_PTR,
00542     TLS_TABLE,
00543     LOAD_CONFIG_TABLE,
00544     BOUND_IMPORT,
00545     IAT,
00546     DELAY_IMPORT_DESCRIPTOR,
00547     CLR_RUNTIME_HEADER
00548   };
00549 
00550   enum WindowsSubsystem {
00551     IMAGE_SUBSYSTEM_UNKNOWN = 0, ///< An unknown subsystem.
00552     IMAGE_SUBSYSTEM_NATIVE = 1, ///< Device drivers and native Windows processes
00553     IMAGE_SUBSYSTEM_WINDOWS_GUI = 2, ///< The Windows GUI subsystem.
00554     IMAGE_SUBSYSTEM_WINDOWS_CUI = 3, ///< The Windows character subsystem.
00555     IMAGE_SUBSYSTEM_OS2_CUI = 5, ///< The OS/2 character subsytem.
00556     IMAGE_SUBSYSTEM_POSIX_CUI = 7, ///< The POSIX character subsystem.
00557     IMAGE_SUBSYSTEM_NATIVE_WINDOWS = 8, ///< Native Windows 9x driver.
00558     IMAGE_SUBSYSTEM_WINDOWS_CE_GUI = 9, ///< Windows CE.
00559     IMAGE_SUBSYSTEM_EFI_APPLICATION = 10, ///< An EFI application.
00560     IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER = 11, ///< An EFI driver with boot
00561                                                   ///  services.
00562     IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER = 12, ///< An EFI driver with run-time
00563                                              ///  services.
00564     IMAGE_SUBSYSTEM_EFI_ROM = 13, ///< An EFI ROM image.
00565     IMAGE_SUBSYSTEM_XBOX = 14, ///< XBOX.
00566     IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION = 16 ///< A BCD application.
00567   };
00568 
00569   enum DLLCharacteristics {
00570     /// ASLR with 64 bit address space.
00571     IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA = 0x0020,
00572     /// DLL can be relocated at load time.
00573     IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE = 0x0040,
00574     /// Code integrity checks are enforced.
00575     IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY = 0x0080,
00576     ///< Image is NX compatible.
00577     IMAGE_DLL_CHARACTERISTICS_NX_COMPAT = 0x0100,
00578     /// Isolation aware, but do not isolate the image.
00579     IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION = 0x0200,
00580     /// Does not use structured exception handling (SEH). No SEH handler may be
00581     /// called in this image.
00582     IMAGE_DLL_CHARACTERISTICS_NO_SEH = 0x0400,
00583     /// Do not bind the image.
00584     IMAGE_DLL_CHARACTERISTICS_NO_BIND = 0x0800,
00585     ///< Image should execute in an AppContainer.
00586     IMAGE_DLL_CHARACTERISTICS_APPCONTAINER = 0x1000,
00587     ///< A WDM driver.
00588     IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER = 0x2000,
00589     ///< Image supports Control Flow Guard.
00590     IMAGE_DLL_CHARACTERISTICS_GUARD_CF = 0x4000,
00591     /// Terminal Server aware.
00592     IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE = 0x8000
00593   };
00594 
00595   enum DebugType {
00596     IMAGE_DEBUG_TYPE_UNKNOWN       = 0,
00597     IMAGE_DEBUG_TYPE_COFF          = 1,
00598     IMAGE_DEBUG_TYPE_CODEVIEW      = 2,
00599     IMAGE_DEBUG_TYPE_FPO           = 3,
00600     IMAGE_DEBUG_TYPE_MISC          = 4,
00601     IMAGE_DEBUG_TYPE_EXCEPTION     = 5,
00602     IMAGE_DEBUG_TYPE_FIXUP         = 6,
00603     IMAGE_DEBUG_TYPE_OMAP_TO_SRC   = 7,
00604     IMAGE_DEBUG_TYPE_OMAP_FROM_SRC = 8,
00605     IMAGE_DEBUG_TYPE_BORLAND       = 9,
00606     IMAGE_DEBUG_TYPE_CLSID         = 11
00607   };
00608 
00609   enum BaseRelocationType {
00610     IMAGE_REL_BASED_ABSOLUTE       = 0,
00611     IMAGE_REL_BASED_HIGH           = 1,
00612     IMAGE_REL_BASED_LOW            = 2,
00613     IMAGE_REL_BASED_HIGHLOW        = 3,
00614     IMAGE_REL_BASED_HIGHADJ        = 4,
00615     IMAGE_REL_BASED_MIPS_JMPADDR   = 5,
00616     IMAGE_REL_BASED_ARM_MOV32A     = 5,
00617     IMAGE_REL_BASED_ARM_MOV32T     = 7,
00618     IMAGE_REL_BASED_MIPS_JMPADDR16 = 9,
00619     IMAGE_REL_BASED_DIR64          = 10
00620   };
00621 
00622   enum ImportType {
00623     IMPORT_CODE  = 0,
00624     IMPORT_DATA  = 1,
00625     IMPORT_CONST = 2
00626   };
00627 
00628   enum ImportNameType {
00629     /// Import is by ordinal. This indicates that the value in the Ordinal/Hint
00630     /// field of the import header is the import's ordinal. If this constant is
00631     /// not specified, then the Ordinal/Hint field should always be interpreted
00632     /// as the import's hint.
00633     IMPORT_ORDINAL         = 0,
00634     /// The import name is identical to the public symbol name
00635     IMPORT_NAME            = 1,
00636     /// The import name is the public symbol name, but skipping the leading ?,
00637     /// @, or optionally _.
00638     IMPORT_NAME_NOPREFIX   = 2,
00639     /// The import name is the public symbol name, but skipping the leading ?,
00640     /// @, or optionally _, and truncating at the first @.
00641     IMPORT_NAME_UNDECORATE = 3
00642   };
00643 
00644   struct ImportHeader {
00645     uint16_t Sig1; ///< Must be IMAGE_FILE_MACHINE_UNKNOWN (0).
00646     uint16_t Sig2; ///< Must be 0xFFFF.
00647     uint16_t Version;
00648     uint16_t Machine;
00649     uint32_t TimeDateStamp;
00650     uint32_t SizeOfData;
00651     uint16_t OrdinalHint;
00652     uint16_t TypeInfo;
00653 
00654     ImportType getType() const {
00655       return static_cast<ImportType>(TypeInfo & 0x3);
00656     }
00657 
00658     ImportNameType getNameType() const {
00659       return static_cast<ImportNameType>((TypeInfo & 0x1C) >> 3);
00660     }
00661   };
00662 
00663   enum CodeViewLineTableIdentifiers {
00664     DEBUG_SECTION_MAGIC           = 0x4,
00665     DEBUG_LINE_TABLE_SUBSECTION   = 0xF2,
00666     DEBUG_STRING_TABLE_SUBSECTION = 0xF3,
00667     DEBUG_INDEX_SUBSECTION        = 0xF4
00668   };
00669 
00670   inline bool isReservedSectionNumber(int32_t SectionNumber) {
00671     return SectionNumber <= 0;
00672   }
00673 
00674 } // End namespace COFF.
00675 } // End namespace llvm.
00676 
00677 #endif