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