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