COFFYAML.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. //===- COFFYAML.cpp - COFF YAMLIO implementation --------------------------===//
  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 defines classes for handling the YAML representation of COFF.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ObjectYAML/COFFYAML.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/Support/YAMLTraits.h"
  15. #include <cstdint>
  16. #include <cstring>
  17. #define ECase(X) IO.enumCase(Value, #X, COFF::X);
  18. namespace llvm {
  19. namespace COFFYAML {
  20. Section::Section() { memset(&Header, 0, sizeof(COFF::section)); }
  21. Symbol::Symbol() { memset(&Header, 0, sizeof(COFF::symbol)); }
  22. Object::Object() { memset(&Header, 0, sizeof(COFF::header)); }
  23. } // end namespace COFFYAML
  24. namespace yaml {
  25. void ScalarEnumerationTraits<COFFYAML::COMDATType>::enumeration(
  26. IO &IO, COFFYAML::COMDATType &Value) {
  27. IO.enumCase(Value, "0", 0);
  28. ECase(IMAGE_COMDAT_SELECT_NODUPLICATES);
  29. ECase(IMAGE_COMDAT_SELECT_ANY);
  30. ECase(IMAGE_COMDAT_SELECT_SAME_SIZE);
  31. ECase(IMAGE_COMDAT_SELECT_EXACT_MATCH);
  32. ECase(IMAGE_COMDAT_SELECT_ASSOCIATIVE);
  33. ECase(IMAGE_COMDAT_SELECT_LARGEST);
  34. ECase(IMAGE_COMDAT_SELECT_NEWEST);
  35. }
  36. void
  37. ScalarEnumerationTraits<COFFYAML::WeakExternalCharacteristics>::enumeration(
  38. IO &IO, COFFYAML::WeakExternalCharacteristics &Value) {
  39. IO.enumCase(Value, "0", 0);
  40. ECase(IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY);
  41. ECase(IMAGE_WEAK_EXTERN_SEARCH_LIBRARY);
  42. ECase(IMAGE_WEAK_EXTERN_SEARCH_ALIAS);
  43. ECase(IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY);
  44. }
  45. void ScalarEnumerationTraits<COFFYAML::AuxSymbolType>::enumeration(
  46. IO &IO, COFFYAML::AuxSymbolType &Value) {
  47. ECase(IMAGE_AUX_SYMBOL_TYPE_TOKEN_DEF);
  48. }
  49. void ScalarEnumerationTraits<COFF::MachineTypes>::enumeration(
  50. IO &IO, COFF::MachineTypes &Value) {
  51. ECase(IMAGE_FILE_MACHINE_UNKNOWN);
  52. ECase(IMAGE_FILE_MACHINE_AM33);
  53. ECase(IMAGE_FILE_MACHINE_AMD64);
  54. ECase(IMAGE_FILE_MACHINE_ARM);
  55. ECase(IMAGE_FILE_MACHINE_ARMNT);
  56. ECase(IMAGE_FILE_MACHINE_ARM64);
  57. ECase(IMAGE_FILE_MACHINE_ARM64EC);
  58. ECase(IMAGE_FILE_MACHINE_EBC);
  59. ECase(IMAGE_FILE_MACHINE_I386);
  60. ECase(IMAGE_FILE_MACHINE_IA64);
  61. ECase(IMAGE_FILE_MACHINE_M32R);
  62. ECase(IMAGE_FILE_MACHINE_MIPS16);
  63. ECase(IMAGE_FILE_MACHINE_MIPSFPU);
  64. ECase(IMAGE_FILE_MACHINE_MIPSFPU16);
  65. ECase(IMAGE_FILE_MACHINE_POWERPC);
  66. ECase(IMAGE_FILE_MACHINE_POWERPCFP);
  67. ECase(IMAGE_FILE_MACHINE_R4000);
  68. ECase(IMAGE_FILE_MACHINE_RISCV32);
  69. ECase(IMAGE_FILE_MACHINE_RISCV64);
  70. ECase(IMAGE_FILE_MACHINE_RISCV128);
  71. ECase(IMAGE_FILE_MACHINE_SH3);
  72. ECase(IMAGE_FILE_MACHINE_SH3DSP);
  73. ECase(IMAGE_FILE_MACHINE_SH4);
  74. ECase(IMAGE_FILE_MACHINE_SH5);
  75. ECase(IMAGE_FILE_MACHINE_THUMB);
  76. ECase(IMAGE_FILE_MACHINE_WCEMIPSV2);
  77. }
  78. void ScalarEnumerationTraits<COFF::SymbolBaseType>::enumeration(
  79. IO &IO, COFF::SymbolBaseType &Value) {
  80. ECase(IMAGE_SYM_TYPE_NULL);
  81. ECase(IMAGE_SYM_TYPE_VOID);
  82. ECase(IMAGE_SYM_TYPE_CHAR);
  83. ECase(IMAGE_SYM_TYPE_SHORT);
  84. ECase(IMAGE_SYM_TYPE_INT);
  85. ECase(IMAGE_SYM_TYPE_LONG);
  86. ECase(IMAGE_SYM_TYPE_FLOAT);
  87. ECase(IMAGE_SYM_TYPE_DOUBLE);
  88. ECase(IMAGE_SYM_TYPE_STRUCT);
  89. ECase(IMAGE_SYM_TYPE_UNION);
  90. ECase(IMAGE_SYM_TYPE_ENUM);
  91. ECase(IMAGE_SYM_TYPE_MOE);
  92. ECase(IMAGE_SYM_TYPE_BYTE);
  93. ECase(IMAGE_SYM_TYPE_WORD);
  94. ECase(IMAGE_SYM_TYPE_UINT);
  95. ECase(IMAGE_SYM_TYPE_DWORD);
  96. }
  97. void ScalarEnumerationTraits<COFF::SymbolStorageClass>::enumeration(
  98. IO &IO, COFF::SymbolStorageClass &Value) {
  99. ECase(IMAGE_SYM_CLASS_END_OF_FUNCTION);
  100. ECase(IMAGE_SYM_CLASS_NULL);
  101. ECase(IMAGE_SYM_CLASS_AUTOMATIC);
  102. ECase(IMAGE_SYM_CLASS_EXTERNAL);
  103. ECase(IMAGE_SYM_CLASS_STATIC);
  104. ECase(IMAGE_SYM_CLASS_REGISTER);
  105. ECase(IMAGE_SYM_CLASS_EXTERNAL_DEF);
  106. ECase(IMAGE_SYM_CLASS_LABEL);
  107. ECase(IMAGE_SYM_CLASS_UNDEFINED_LABEL);
  108. ECase(IMAGE_SYM_CLASS_MEMBER_OF_STRUCT);
  109. ECase(IMAGE_SYM_CLASS_ARGUMENT);
  110. ECase(IMAGE_SYM_CLASS_STRUCT_TAG);
  111. ECase(IMAGE_SYM_CLASS_MEMBER_OF_UNION);
  112. ECase(IMAGE_SYM_CLASS_UNION_TAG);
  113. ECase(IMAGE_SYM_CLASS_TYPE_DEFINITION);
  114. ECase(IMAGE_SYM_CLASS_UNDEFINED_STATIC);
  115. ECase(IMAGE_SYM_CLASS_ENUM_TAG);
  116. ECase(IMAGE_SYM_CLASS_MEMBER_OF_ENUM);
  117. ECase(IMAGE_SYM_CLASS_REGISTER_PARAM);
  118. ECase(IMAGE_SYM_CLASS_BIT_FIELD);
  119. ECase(IMAGE_SYM_CLASS_BLOCK);
  120. ECase(IMAGE_SYM_CLASS_FUNCTION);
  121. ECase(IMAGE_SYM_CLASS_END_OF_STRUCT);
  122. ECase(IMAGE_SYM_CLASS_FILE);
  123. ECase(IMAGE_SYM_CLASS_SECTION);
  124. ECase(IMAGE_SYM_CLASS_WEAK_EXTERNAL);
  125. ECase(IMAGE_SYM_CLASS_CLR_TOKEN);
  126. }
  127. void ScalarEnumerationTraits<COFF::SymbolComplexType>::enumeration(
  128. IO &IO, COFF::SymbolComplexType &Value) {
  129. ECase(IMAGE_SYM_DTYPE_NULL);
  130. ECase(IMAGE_SYM_DTYPE_POINTER);
  131. ECase(IMAGE_SYM_DTYPE_FUNCTION);
  132. ECase(IMAGE_SYM_DTYPE_ARRAY);
  133. }
  134. void ScalarEnumerationTraits<COFF::RelocationTypeI386>::enumeration(
  135. IO &IO, COFF::RelocationTypeI386 &Value) {
  136. ECase(IMAGE_REL_I386_ABSOLUTE);
  137. ECase(IMAGE_REL_I386_DIR16);
  138. ECase(IMAGE_REL_I386_REL16);
  139. ECase(IMAGE_REL_I386_DIR32);
  140. ECase(IMAGE_REL_I386_DIR32NB);
  141. ECase(IMAGE_REL_I386_SEG12);
  142. ECase(IMAGE_REL_I386_SECTION);
  143. ECase(IMAGE_REL_I386_SECREL);
  144. ECase(IMAGE_REL_I386_TOKEN);
  145. ECase(IMAGE_REL_I386_SECREL7);
  146. ECase(IMAGE_REL_I386_REL32);
  147. }
  148. void ScalarEnumerationTraits<COFF::RelocationTypeAMD64>::enumeration(
  149. IO &IO, COFF::RelocationTypeAMD64 &Value) {
  150. ECase(IMAGE_REL_AMD64_ABSOLUTE);
  151. ECase(IMAGE_REL_AMD64_ADDR64);
  152. ECase(IMAGE_REL_AMD64_ADDR32);
  153. ECase(IMAGE_REL_AMD64_ADDR32NB);
  154. ECase(IMAGE_REL_AMD64_REL32);
  155. ECase(IMAGE_REL_AMD64_REL32_1);
  156. ECase(IMAGE_REL_AMD64_REL32_2);
  157. ECase(IMAGE_REL_AMD64_REL32_3);
  158. ECase(IMAGE_REL_AMD64_REL32_4);
  159. ECase(IMAGE_REL_AMD64_REL32_5);
  160. ECase(IMAGE_REL_AMD64_SECTION);
  161. ECase(IMAGE_REL_AMD64_SECREL);
  162. ECase(IMAGE_REL_AMD64_SECREL7);
  163. ECase(IMAGE_REL_AMD64_TOKEN);
  164. ECase(IMAGE_REL_AMD64_SREL32);
  165. ECase(IMAGE_REL_AMD64_PAIR);
  166. ECase(IMAGE_REL_AMD64_SSPAN32);
  167. }
  168. void ScalarEnumerationTraits<COFF::RelocationTypesARM>::enumeration(
  169. IO &IO, COFF::RelocationTypesARM &Value) {
  170. ECase(IMAGE_REL_ARM_ABSOLUTE);
  171. ECase(IMAGE_REL_ARM_ADDR32);
  172. ECase(IMAGE_REL_ARM_ADDR32NB);
  173. ECase(IMAGE_REL_ARM_BRANCH24);
  174. ECase(IMAGE_REL_ARM_BRANCH11);
  175. ECase(IMAGE_REL_ARM_TOKEN);
  176. ECase(IMAGE_REL_ARM_BLX24);
  177. ECase(IMAGE_REL_ARM_BLX11);
  178. ECase(IMAGE_REL_ARM_REL32);
  179. ECase(IMAGE_REL_ARM_SECTION);
  180. ECase(IMAGE_REL_ARM_SECREL);
  181. ECase(IMAGE_REL_ARM_MOV32A);
  182. ECase(IMAGE_REL_ARM_MOV32T);
  183. ECase(IMAGE_REL_ARM_BRANCH20T);
  184. ECase(IMAGE_REL_ARM_BRANCH24T);
  185. ECase(IMAGE_REL_ARM_BLX23T);
  186. ECase(IMAGE_REL_ARM_PAIR);
  187. }
  188. void ScalarEnumerationTraits<COFF::RelocationTypesARM64>::enumeration(
  189. IO &IO, COFF::RelocationTypesARM64 &Value) {
  190. ECase(IMAGE_REL_ARM64_ABSOLUTE);
  191. ECase(IMAGE_REL_ARM64_ADDR32);
  192. ECase(IMAGE_REL_ARM64_ADDR32NB);
  193. ECase(IMAGE_REL_ARM64_BRANCH26);
  194. ECase(IMAGE_REL_ARM64_PAGEBASE_REL21);
  195. ECase(IMAGE_REL_ARM64_REL21);
  196. ECase(IMAGE_REL_ARM64_PAGEOFFSET_12A);
  197. ECase(IMAGE_REL_ARM64_PAGEOFFSET_12L);
  198. ECase(IMAGE_REL_ARM64_SECREL);
  199. ECase(IMAGE_REL_ARM64_SECREL_LOW12A);
  200. ECase(IMAGE_REL_ARM64_SECREL_HIGH12A);
  201. ECase(IMAGE_REL_ARM64_SECREL_LOW12L);
  202. ECase(IMAGE_REL_ARM64_TOKEN);
  203. ECase(IMAGE_REL_ARM64_SECTION);
  204. ECase(IMAGE_REL_ARM64_ADDR64);
  205. ECase(IMAGE_REL_ARM64_BRANCH19);
  206. ECase(IMAGE_REL_ARM64_BRANCH14);
  207. ECase(IMAGE_REL_ARM64_REL32);
  208. }
  209. void ScalarEnumerationTraits<COFF::WindowsSubsystem>::enumeration(
  210. IO &IO, COFF::WindowsSubsystem &Value) {
  211. ECase(IMAGE_SUBSYSTEM_UNKNOWN);
  212. ECase(IMAGE_SUBSYSTEM_NATIVE);
  213. ECase(IMAGE_SUBSYSTEM_WINDOWS_GUI);
  214. ECase(IMAGE_SUBSYSTEM_WINDOWS_CUI);
  215. ECase(IMAGE_SUBSYSTEM_OS2_CUI);
  216. ECase(IMAGE_SUBSYSTEM_POSIX_CUI);
  217. ECase(IMAGE_SUBSYSTEM_NATIVE_WINDOWS);
  218. ECase(IMAGE_SUBSYSTEM_WINDOWS_CE_GUI);
  219. ECase(IMAGE_SUBSYSTEM_EFI_APPLICATION);
  220. ECase(IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER);
  221. ECase(IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER);
  222. ECase(IMAGE_SUBSYSTEM_EFI_ROM);
  223. ECase(IMAGE_SUBSYSTEM_XBOX);
  224. ECase(IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION);
  225. }
  226. #undef ECase
  227. #define BCase(X) IO.bitSetCase(Value, #X, COFF::X);
  228. void ScalarBitSetTraits<COFF::Characteristics>::bitset(
  229. IO &IO, COFF::Characteristics &Value) {
  230. BCase(IMAGE_FILE_RELOCS_STRIPPED);
  231. BCase(IMAGE_FILE_EXECUTABLE_IMAGE);
  232. BCase(IMAGE_FILE_LINE_NUMS_STRIPPED);
  233. BCase(IMAGE_FILE_LOCAL_SYMS_STRIPPED);
  234. BCase(IMAGE_FILE_AGGRESSIVE_WS_TRIM);
  235. BCase(IMAGE_FILE_LARGE_ADDRESS_AWARE);
  236. BCase(IMAGE_FILE_BYTES_REVERSED_LO);
  237. BCase(IMAGE_FILE_32BIT_MACHINE);
  238. BCase(IMAGE_FILE_DEBUG_STRIPPED);
  239. BCase(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP);
  240. BCase(IMAGE_FILE_NET_RUN_FROM_SWAP);
  241. BCase(IMAGE_FILE_SYSTEM);
  242. BCase(IMAGE_FILE_DLL);
  243. BCase(IMAGE_FILE_UP_SYSTEM_ONLY);
  244. BCase(IMAGE_FILE_BYTES_REVERSED_HI);
  245. }
  246. void ScalarBitSetTraits<COFF::SectionCharacteristics>::bitset(
  247. IO &IO, COFF::SectionCharacteristics &Value) {
  248. BCase(IMAGE_SCN_TYPE_NOLOAD);
  249. BCase(IMAGE_SCN_TYPE_NO_PAD);
  250. BCase(IMAGE_SCN_CNT_CODE);
  251. BCase(IMAGE_SCN_CNT_INITIALIZED_DATA);
  252. BCase(IMAGE_SCN_CNT_UNINITIALIZED_DATA);
  253. BCase(IMAGE_SCN_LNK_OTHER);
  254. BCase(IMAGE_SCN_LNK_INFO);
  255. BCase(IMAGE_SCN_LNK_REMOVE);
  256. BCase(IMAGE_SCN_LNK_COMDAT);
  257. BCase(IMAGE_SCN_GPREL);
  258. BCase(IMAGE_SCN_MEM_PURGEABLE);
  259. BCase(IMAGE_SCN_MEM_16BIT);
  260. BCase(IMAGE_SCN_MEM_LOCKED);
  261. BCase(IMAGE_SCN_MEM_PRELOAD);
  262. BCase(IMAGE_SCN_LNK_NRELOC_OVFL);
  263. BCase(IMAGE_SCN_MEM_DISCARDABLE);
  264. BCase(IMAGE_SCN_MEM_NOT_CACHED);
  265. BCase(IMAGE_SCN_MEM_NOT_PAGED);
  266. BCase(IMAGE_SCN_MEM_SHARED);
  267. BCase(IMAGE_SCN_MEM_EXECUTE);
  268. BCase(IMAGE_SCN_MEM_READ);
  269. BCase(IMAGE_SCN_MEM_WRITE);
  270. }
  271. void ScalarBitSetTraits<COFF::DLLCharacteristics>::bitset(
  272. IO &IO, COFF::DLLCharacteristics &Value) {
  273. BCase(IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA);
  274. BCase(IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE);
  275. BCase(IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY);
  276. BCase(IMAGE_DLL_CHARACTERISTICS_NX_COMPAT);
  277. BCase(IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION);
  278. BCase(IMAGE_DLL_CHARACTERISTICS_NO_SEH);
  279. BCase(IMAGE_DLL_CHARACTERISTICS_NO_BIND);
  280. BCase(IMAGE_DLL_CHARACTERISTICS_APPCONTAINER);
  281. BCase(IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER);
  282. BCase(IMAGE_DLL_CHARACTERISTICS_GUARD_CF);
  283. BCase(IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE);
  284. }
  285. #undef BCase
  286. namespace {
  287. struct NSectionSelectionType {
  288. NSectionSelectionType(IO &)
  289. : SelectionType(COFFYAML::COMDATType(0)) {}
  290. NSectionSelectionType(IO &, uint8_t C)
  291. : SelectionType(COFFYAML::COMDATType(C)) {}
  292. uint8_t denormalize(IO &) { return SelectionType; }
  293. COFFYAML::COMDATType SelectionType;
  294. };
  295. struct NWeakExternalCharacteristics {
  296. NWeakExternalCharacteristics(IO &)
  297. : Characteristics(COFFYAML::WeakExternalCharacteristics(0)) {}
  298. NWeakExternalCharacteristics(IO &, uint32_t C)
  299. : Characteristics(COFFYAML::WeakExternalCharacteristics(C)) {}
  300. uint32_t denormalize(IO &) { return Characteristics; }
  301. COFFYAML::WeakExternalCharacteristics Characteristics;
  302. };
  303. struct NSectionCharacteristics {
  304. NSectionCharacteristics(IO &)
  305. : Characteristics(COFF::SectionCharacteristics(0)) {}
  306. NSectionCharacteristics(IO &, uint32_t C)
  307. : Characteristics(COFF::SectionCharacteristics(C)) {}
  308. uint32_t denormalize(IO &) { return Characteristics; }
  309. COFF::SectionCharacteristics Characteristics;
  310. };
  311. struct NAuxTokenType {
  312. NAuxTokenType(IO &)
  313. : AuxType(COFFYAML::AuxSymbolType(0)) {}
  314. NAuxTokenType(IO &, uint8_t C)
  315. : AuxType(COFFYAML::AuxSymbolType(C)) {}
  316. uint32_t denormalize(IO &) { return AuxType; }
  317. COFFYAML::AuxSymbolType AuxType;
  318. };
  319. struct NStorageClass {
  320. NStorageClass(IO &) : StorageClass(COFF::SymbolStorageClass(0)) {}
  321. NStorageClass(IO &, uint8_t S) : StorageClass(COFF::SymbolStorageClass(S)) {}
  322. uint8_t denormalize(IO &) { return StorageClass; }
  323. COFF::SymbolStorageClass StorageClass;
  324. };
  325. struct NMachine {
  326. NMachine(IO &) : Machine(COFF::MachineTypes(0)) {}
  327. NMachine(IO &, uint16_t M) : Machine(COFF::MachineTypes(M)) {}
  328. uint16_t denormalize(IO &) { return Machine; }
  329. COFF::MachineTypes Machine;
  330. };
  331. struct NHeaderCharacteristics {
  332. NHeaderCharacteristics(IO &) : Characteristics(COFF::Characteristics(0)) {}
  333. NHeaderCharacteristics(IO &, uint16_t C)
  334. : Characteristics(COFF::Characteristics(C)) {}
  335. uint16_t denormalize(IO &) { return Characteristics; }
  336. COFF::Characteristics Characteristics;
  337. };
  338. template <typename RelocType>
  339. struct NType {
  340. NType(IO &) : Type(RelocType(0)) {}
  341. NType(IO &, uint16_t T) : Type(RelocType(T)) {}
  342. uint16_t denormalize(IO &) { return Type; }
  343. RelocType Type;
  344. };
  345. struct NWindowsSubsystem {
  346. NWindowsSubsystem(IO &) : Subsystem(COFF::WindowsSubsystem(0)) {}
  347. NWindowsSubsystem(IO &, uint16_t C) : Subsystem(COFF::WindowsSubsystem(C)) {}
  348. uint16_t denormalize(IO &) { return Subsystem; }
  349. COFF::WindowsSubsystem Subsystem;
  350. };
  351. struct NDLLCharacteristics {
  352. NDLLCharacteristics(IO &) : Characteristics(COFF::DLLCharacteristics(0)) {}
  353. NDLLCharacteristics(IO &, uint16_t C)
  354. : Characteristics(COFF::DLLCharacteristics(C)) {}
  355. uint16_t denormalize(IO &) { return Characteristics; }
  356. COFF::DLLCharacteristics Characteristics;
  357. };
  358. } // end anonymous namespace
  359. void MappingTraits<COFFYAML::Relocation>::mapping(IO &IO,
  360. COFFYAML::Relocation &Rel) {
  361. IO.mapRequired("VirtualAddress", Rel.VirtualAddress);
  362. IO.mapOptional("SymbolName", Rel.SymbolName, StringRef());
  363. IO.mapOptional("SymbolTableIndex", Rel.SymbolTableIndex);
  364. COFF::header &H = *static_cast<COFF::header *>(IO.getContext());
  365. if (H.Machine == COFF::IMAGE_FILE_MACHINE_I386) {
  366. MappingNormalization<NType<COFF::RelocationTypeI386>, uint16_t> NT(
  367. IO, Rel.Type);
  368. IO.mapRequired("Type", NT->Type);
  369. } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_AMD64) {
  370. MappingNormalization<NType<COFF::RelocationTypeAMD64>, uint16_t> NT(
  371. IO, Rel.Type);
  372. IO.mapRequired("Type", NT->Type);
  373. } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
  374. MappingNormalization<NType<COFF::RelocationTypesARM>, uint16_t> NT(
  375. IO, Rel.Type);
  376. IO.mapRequired("Type", NT->Type);
  377. } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_ARM64 ||
  378. H.Machine == COFF::IMAGE_FILE_MACHINE_ARM64EC) {
  379. MappingNormalization<NType<COFF::RelocationTypesARM64>, uint16_t> NT(
  380. IO, Rel.Type);
  381. IO.mapRequired("Type", NT->Type);
  382. } else {
  383. IO.mapRequired("Type", Rel.Type);
  384. }
  385. }
  386. void MappingTraits<COFF::DataDirectory>::mapping(IO &IO,
  387. COFF::DataDirectory &DD) {
  388. IO.mapRequired("RelativeVirtualAddress", DD.RelativeVirtualAddress);
  389. IO.mapRequired("Size", DD.Size);
  390. }
  391. void MappingTraits<COFFYAML::PEHeader>::mapping(IO &IO,
  392. COFFYAML::PEHeader &PH) {
  393. MappingNormalization<NWindowsSubsystem, uint16_t> NWS(IO,
  394. PH.Header.Subsystem);
  395. MappingNormalization<NDLLCharacteristics, uint16_t> NDC(
  396. IO, PH.Header.DLLCharacteristics);
  397. IO.mapOptional("AddressOfEntryPoint", PH.Header.AddressOfEntryPoint);
  398. IO.mapOptional("ImageBase", PH.Header.ImageBase);
  399. IO.mapOptional("SectionAlignment", PH.Header.SectionAlignment, 1);
  400. IO.mapOptional("FileAlignment", PH.Header.FileAlignment, 1);
  401. IO.mapOptional("MajorOperatingSystemVersion",
  402. PH.Header.MajorOperatingSystemVersion);
  403. IO.mapOptional("MinorOperatingSystemVersion",
  404. PH.Header.MinorOperatingSystemVersion);
  405. IO.mapOptional("MajorImageVersion", PH.Header.MajorImageVersion);
  406. IO.mapOptional("MinorImageVersion", PH.Header.MinorImageVersion);
  407. IO.mapOptional("MajorSubsystemVersion", PH.Header.MajorSubsystemVersion);
  408. IO.mapOptional("MinorSubsystemVersion", PH.Header.MinorSubsystemVersion);
  409. IO.mapOptional("Subsystem", NWS->Subsystem);
  410. IO.mapOptional("DLLCharacteristics", NDC->Characteristics);
  411. IO.mapOptional("SizeOfStackReserve", PH.Header.SizeOfStackReserve);
  412. IO.mapOptional("SizeOfStackCommit", PH.Header.SizeOfStackCommit);
  413. IO.mapOptional("SizeOfHeapReserve", PH.Header.SizeOfHeapReserve);
  414. IO.mapOptional("SizeOfHeapCommit", PH.Header.SizeOfHeapCommit);
  415. IO.mapOptional("NumberOfRvaAndSize", PH.Header.NumberOfRvaAndSize,
  416. COFF::NUM_DATA_DIRECTORIES + 1);
  417. IO.mapOptional("ExportTable", PH.DataDirectories[COFF::EXPORT_TABLE]);
  418. IO.mapOptional("ImportTable", PH.DataDirectories[COFF::IMPORT_TABLE]);
  419. IO.mapOptional("ResourceTable", PH.DataDirectories[COFF::RESOURCE_TABLE]);
  420. IO.mapOptional("ExceptionTable", PH.DataDirectories[COFF::EXCEPTION_TABLE]);
  421. IO.mapOptional("CertificateTable", PH.DataDirectories[COFF::CERTIFICATE_TABLE]);
  422. IO.mapOptional("BaseRelocationTable",
  423. PH.DataDirectories[COFF::BASE_RELOCATION_TABLE]);
  424. IO.mapOptional("Debug", PH.DataDirectories[COFF::DEBUG_DIRECTORY]);
  425. IO.mapOptional("Architecture", PH.DataDirectories[COFF::ARCHITECTURE]);
  426. IO.mapOptional("GlobalPtr", PH.DataDirectories[COFF::GLOBAL_PTR]);
  427. IO.mapOptional("TlsTable", PH.DataDirectories[COFF::TLS_TABLE]);
  428. IO.mapOptional("LoadConfigTable",
  429. PH.DataDirectories[COFF::LOAD_CONFIG_TABLE]);
  430. IO.mapOptional("BoundImport", PH.DataDirectories[COFF::BOUND_IMPORT]);
  431. IO.mapOptional("IAT", PH.DataDirectories[COFF::IAT]);
  432. IO.mapOptional("DelayImportDescriptor",
  433. PH.DataDirectories[COFF::DELAY_IMPORT_DESCRIPTOR]);
  434. IO.mapOptional("ClrRuntimeHeader",
  435. PH.DataDirectories[COFF::CLR_RUNTIME_HEADER]);
  436. }
  437. void MappingTraits<COFF::header>::mapping(IO &IO, COFF::header &H) {
  438. MappingNormalization<NMachine, uint16_t> NM(IO, H.Machine);
  439. MappingNormalization<NHeaderCharacteristics, uint16_t> NC(IO,
  440. H.Characteristics);
  441. IO.mapRequired("Machine", NM->Machine);
  442. IO.mapOptional("Characteristics", NC->Characteristics);
  443. IO.setContext(static_cast<void *>(&H));
  444. }
  445. void MappingTraits<COFF::AuxiliaryFunctionDefinition>::mapping(
  446. IO &IO, COFF::AuxiliaryFunctionDefinition &AFD) {
  447. IO.mapRequired("TagIndex", AFD.TagIndex);
  448. IO.mapRequired("TotalSize", AFD.TotalSize);
  449. IO.mapRequired("PointerToLinenumber", AFD.PointerToLinenumber);
  450. IO.mapRequired("PointerToNextFunction", AFD.PointerToNextFunction);
  451. }
  452. void MappingTraits<COFF::AuxiliarybfAndefSymbol>::mapping(
  453. IO &IO, COFF::AuxiliarybfAndefSymbol &AAS) {
  454. IO.mapRequired("Linenumber", AAS.Linenumber);
  455. IO.mapRequired("PointerToNextFunction", AAS.PointerToNextFunction);
  456. }
  457. void MappingTraits<COFF::AuxiliaryWeakExternal>::mapping(
  458. IO &IO, COFF::AuxiliaryWeakExternal &AWE) {
  459. MappingNormalization<NWeakExternalCharacteristics, uint32_t> NWEC(
  460. IO, AWE.Characteristics);
  461. IO.mapRequired("TagIndex", AWE.TagIndex);
  462. IO.mapRequired("Characteristics", NWEC->Characteristics);
  463. }
  464. void MappingTraits<COFF::AuxiliarySectionDefinition>::mapping(
  465. IO &IO, COFF::AuxiliarySectionDefinition &ASD) {
  466. MappingNormalization<NSectionSelectionType, uint8_t> NSST(
  467. IO, ASD.Selection);
  468. IO.mapRequired("Length", ASD.Length);
  469. IO.mapRequired("NumberOfRelocations", ASD.NumberOfRelocations);
  470. IO.mapRequired("NumberOfLinenumbers", ASD.NumberOfLinenumbers);
  471. IO.mapRequired("CheckSum", ASD.CheckSum);
  472. IO.mapRequired("Number", ASD.Number);
  473. IO.mapOptional("Selection", NSST->SelectionType, COFFYAML::COMDATType(0));
  474. }
  475. void MappingTraits<COFF::AuxiliaryCLRToken>::mapping(
  476. IO &IO, COFF::AuxiliaryCLRToken &ACT) {
  477. MappingNormalization<NAuxTokenType, uint8_t> NATT(IO, ACT.AuxType);
  478. IO.mapRequired("AuxType", NATT->AuxType);
  479. IO.mapRequired("SymbolTableIndex", ACT.SymbolTableIndex);
  480. }
  481. void MappingTraits<COFFYAML::Symbol>::mapping(IO &IO, COFFYAML::Symbol &S) {
  482. MappingNormalization<NStorageClass, uint8_t> NS(IO, S.Header.StorageClass);
  483. IO.mapRequired("Name", S.Name);
  484. IO.mapRequired("Value", S.Header.Value);
  485. IO.mapRequired("SectionNumber", S.Header.SectionNumber);
  486. IO.mapRequired("SimpleType", S.SimpleType);
  487. IO.mapRequired("ComplexType", S.ComplexType);
  488. IO.mapRequired("StorageClass", NS->StorageClass);
  489. IO.mapOptional("FunctionDefinition", S.FunctionDefinition);
  490. IO.mapOptional("bfAndefSymbol", S.bfAndefSymbol);
  491. IO.mapOptional("WeakExternal", S.WeakExternal);
  492. IO.mapOptional("File", S.File, StringRef());
  493. IO.mapOptional("SectionDefinition", S.SectionDefinition);
  494. IO.mapOptional("CLRToken", S.CLRToken);
  495. }
  496. void MappingTraits<COFFYAML::Section>::mapping(IO &IO, COFFYAML::Section &Sec) {
  497. MappingNormalization<NSectionCharacteristics, uint32_t> NC(
  498. IO, Sec.Header.Characteristics);
  499. IO.mapRequired("Name", Sec.Name);
  500. IO.mapRequired("Characteristics", NC->Characteristics);
  501. IO.mapOptional("VirtualAddress", Sec.Header.VirtualAddress, 0U);
  502. IO.mapOptional("VirtualSize", Sec.Header.VirtualSize, 0U);
  503. IO.mapOptional("Alignment", Sec.Alignment, 0U);
  504. // If this is a .debug$S .debug$T .debug$P, or .debug$H section parse the
  505. // semantic representation of the symbols/types. If it is any other kind
  506. // of section, just deal in raw bytes.
  507. IO.mapOptional("SectionData", Sec.SectionData);
  508. if (Sec.Name == ".debug$S")
  509. IO.mapOptional("Subsections", Sec.DebugS);
  510. else if (Sec.Name == ".debug$T")
  511. IO.mapOptional("Types", Sec.DebugT);
  512. else if (Sec.Name == ".debug$P")
  513. IO.mapOptional("PrecompTypes", Sec.DebugP);
  514. else if (Sec.Name == ".debug$H")
  515. IO.mapOptional("GlobalHashes", Sec.DebugH);
  516. // Uninitialized sections, such as .bss, typically have no data, but the size
  517. // is carried in SizeOfRawData, even though PointerToRawData is zero.
  518. if (Sec.SectionData.binary_size() == 0 &&
  519. NC->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
  520. IO.mapOptional("SizeOfRawData", Sec.Header.SizeOfRawData);
  521. IO.mapOptional("Relocations", Sec.Relocations);
  522. }
  523. void MappingTraits<COFFYAML::Object>::mapping(IO &IO, COFFYAML::Object &Obj) {
  524. IO.mapTag("!COFF", true);
  525. IO.mapOptional("OptionalHeader", Obj.OptionalHeader);
  526. IO.mapRequired("header", Obj.Header);
  527. IO.mapRequired("sections", Obj.Sections);
  528. IO.mapRequired("symbols", Obj.Symbols);
  529. }
  530. } // end namespace yaml
  531. } // end namespace llvm