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