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