COFF.h 26 KB

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