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