COFF.h 23 KB

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