COFFYAML.cpp 22 KB

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