COFF.h 23 KB

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