COFFYAML.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- COFFYAML.h - COFF YAMLIO implementation ------------------*- 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 declares classes for handling the YAML representation of COFF.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_OBJECTYAML_COFFYAML_H
  18. #define LLVM_OBJECTYAML_COFFYAML_H
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/BinaryFormat/COFF.h"
  21. #include "llvm/ObjectYAML/CodeViewYAMLDebugSections.h"
  22. #include "llvm/ObjectYAML/CodeViewYAMLTypeHashing.h"
  23. #include "llvm/ObjectYAML/CodeViewYAMLTypes.h"
  24. #include "llvm/ObjectYAML/YAML.h"
  25. #include <cstdint>
  26. #include <optional>
  27. #include <vector>
  28. namespace llvm {
  29. namespace COFF {
  30. inline Characteristics operator|(Characteristics a, Characteristics b) {
  31. uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
  32. return static_cast<Characteristics>(Ret);
  33. }
  34. inline SectionCharacteristics operator|(SectionCharacteristics a,
  35. SectionCharacteristics b) {
  36. uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
  37. return static_cast<SectionCharacteristics>(Ret);
  38. }
  39. inline DLLCharacteristics operator|(DLLCharacteristics a,
  40. DLLCharacteristics b) {
  41. uint16_t Ret = static_cast<uint16_t>(a) | static_cast<uint16_t>(b);
  42. return static_cast<DLLCharacteristics>(Ret);
  43. }
  44. } // end namespace COFF
  45. // The structure of the yaml files is not an exact 1:1 match to COFF. In order
  46. // to use yaml::IO, we use these structures which are closer to the source.
  47. namespace COFFYAML {
  48. LLVM_YAML_STRONG_TYPEDEF(uint8_t, COMDATType)
  49. LLVM_YAML_STRONG_TYPEDEF(uint32_t, WeakExternalCharacteristics)
  50. LLVM_YAML_STRONG_TYPEDEF(uint8_t, AuxSymbolType)
  51. struct Relocation {
  52. uint32_t VirtualAddress;
  53. uint16_t Type;
  54. // Normally a Relocation can refer to the symbol via its name.
  55. // It can also use a direct symbol table index instead (with no name
  56. // specified), allowing disambiguating between multiple symbols with the
  57. // same name or crafting intentionally broken files for testing.
  58. StringRef SymbolName;
  59. std::optional<uint32_t> SymbolTableIndex;
  60. };
  61. struct Section {
  62. COFF::section Header;
  63. unsigned Alignment = 0;
  64. yaml::BinaryRef SectionData;
  65. std::vector<CodeViewYAML::YAMLDebugSubsection> DebugS;
  66. std::vector<CodeViewYAML::LeafRecord> DebugT;
  67. std::vector<CodeViewYAML::LeafRecord> DebugP;
  68. std::optional<CodeViewYAML::DebugHSection> DebugH;
  69. std::vector<Relocation> Relocations;
  70. StringRef Name;
  71. Section();
  72. };
  73. struct Symbol {
  74. COFF::symbol Header;
  75. COFF::SymbolBaseType SimpleType = COFF::IMAGE_SYM_TYPE_NULL;
  76. COFF::SymbolComplexType ComplexType = COFF::IMAGE_SYM_DTYPE_NULL;
  77. std::optional<COFF::AuxiliaryFunctionDefinition> FunctionDefinition;
  78. std::optional<COFF::AuxiliarybfAndefSymbol> bfAndefSymbol;
  79. std::optional<COFF::AuxiliaryWeakExternal> WeakExternal;
  80. StringRef File;
  81. std::optional<COFF::AuxiliarySectionDefinition> SectionDefinition;
  82. std::optional<COFF::AuxiliaryCLRToken> CLRToken;
  83. StringRef Name;
  84. Symbol();
  85. };
  86. struct PEHeader {
  87. COFF::PE32Header Header;
  88. std::optional<COFF::DataDirectory>
  89. DataDirectories[COFF::NUM_DATA_DIRECTORIES];
  90. };
  91. struct Object {
  92. std::optional<PEHeader> OptionalHeader;
  93. COFF::header Header;
  94. std::vector<Section> Sections;
  95. std::vector<Symbol> Symbols;
  96. Object();
  97. };
  98. } // end namespace COFFYAML
  99. } // end namespace llvm
  100. LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section)
  101. LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)
  102. LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Relocation)
  103. namespace llvm {
  104. namespace yaml {
  105. template <>
  106. struct ScalarEnumerationTraits<COFFYAML::WeakExternalCharacteristics> {
  107. static void enumeration(IO &IO, COFFYAML::WeakExternalCharacteristics &Value);
  108. };
  109. template <>
  110. struct ScalarEnumerationTraits<COFFYAML::AuxSymbolType> {
  111. static void enumeration(IO &IO, COFFYAML::AuxSymbolType &Value);
  112. };
  113. template <>
  114. struct ScalarEnumerationTraits<COFFYAML::COMDATType> {
  115. static void enumeration(IO &IO, COFFYAML::COMDATType &Value);
  116. };
  117. template <>
  118. struct ScalarEnumerationTraits<COFF::MachineTypes> {
  119. static void enumeration(IO &IO, COFF::MachineTypes &Value);
  120. };
  121. template <>
  122. struct ScalarEnumerationTraits<COFF::SymbolBaseType> {
  123. static void enumeration(IO &IO, COFF::SymbolBaseType &Value);
  124. };
  125. template <>
  126. struct ScalarEnumerationTraits<COFF::SymbolStorageClass> {
  127. static void enumeration(IO &IO, COFF::SymbolStorageClass &Value);
  128. };
  129. template <>
  130. struct ScalarEnumerationTraits<COFF::SymbolComplexType> {
  131. static void enumeration(IO &IO, COFF::SymbolComplexType &Value);
  132. };
  133. template <>
  134. struct ScalarEnumerationTraits<COFF::RelocationTypeI386> {
  135. static void enumeration(IO &IO, COFF::RelocationTypeI386 &Value);
  136. };
  137. template <>
  138. struct ScalarEnumerationTraits<COFF::RelocationTypeAMD64> {
  139. static void enumeration(IO &IO, COFF::RelocationTypeAMD64 &Value);
  140. };
  141. template <>
  142. struct ScalarEnumerationTraits<COFF::RelocationTypesARM> {
  143. static void enumeration(IO &IO, COFF::RelocationTypesARM &Value);
  144. };
  145. template <>
  146. struct ScalarEnumerationTraits<COFF::RelocationTypesARM64> {
  147. static void enumeration(IO &IO, COFF::RelocationTypesARM64 &Value);
  148. };
  149. template <>
  150. struct ScalarEnumerationTraits<COFF::WindowsSubsystem> {
  151. static void enumeration(IO &IO, COFF::WindowsSubsystem &Value);
  152. };
  153. template <>
  154. struct ScalarBitSetTraits<COFF::Characteristics> {
  155. static void bitset(IO &IO, COFF::Characteristics &Value);
  156. };
  157. template <>
  158. struct ScalarBitSetTraits<COFF::SectionCharacteristics> {
  159. static void bitset(IO &IO, COFF::SectionCharacteristics &Value);
  160. };
  161. template <>
  162. struct ScalarBitSetTraits<COFF::DLLCharacteristics> {
  163. static void bitset(IO &IO, COFF::DLLCharacteristics &Value);
  164. };
  165. template <>
  166. struct MappingTraits<COFFYAML::Relocation> {
  167. static void mapping(IO &IO, COFFYAML::Relocation &Rel);
  168. };
  169. template <>
  170. struct MappingTraits<COFFYAML::PEHeader> {
  171. static void mapping(IO &IO, COFFYAML::PEHeader &PH);
  172. };
  173. template <>
  174. struct MappingTraits<COFF::DataDirectory> {
  175. static void mapping(IO &IO, COFF::DataDirectory &DD);
  176. };
  177. template <>
  178. struct MappingTraits<COFF::header> {
  179. static void mapping(IO &IO, COFF::header &H);
  180. };
  181. template <> struct MappingTraits<COFF::AuxiliaryFunctionDefinition> {
  182. static void mapping(IO &IO, COFF::AuxiliaryFunctionDefinition &AFD);
  183. };
  184. template <> struct MappingTraits<COFF::AuxiliarybfAndefSymbol> {
  185. static void mapping(IO &IO, COFF::AuxiliarybfAndefSymbol &AAS);
  186. };
  187. template <> struct MappingTraits<COFF::AuxiliaryWeakExternal> {
  188. static void mapping(IO &IO, COFF::AuxiliaryWeakExternal &AWE);
  189. };
  190. template <> struct MappingTraits<COFF::AuxiliarySectionDefinition> {
  191. static void mapping(IO &IO, COFF::AuxiliarySectionDefinition &ASD);
  192. };
  193. template <> struct MappingTraits<COFF::AuxiliaryCLRToken> {
  194. static void mapping(IO &IO, COFF::AuxiliaryCLRToken &ACT);
  195. };
  196. template <>
  197. struct MappingTraits<COFFYAML::Symbol> {
  198. static void mapping(IO &IO, COFFYAML::Symbol &S);
  199. };
  200. template <>
  201. struct MappingTraits<COFFYAML::Section> {
  202. static void mapping(IO &IO, COFFYAML::Section &Sec);
  203. };
  204. template <>
  205. struct MappingTraits<COFFYAML::Object> {
  206. static void mapping(IO &IO, COFFYAML::Object &Obj);
  207. };
  208. } // end namespace yaml
  209. } // end namespace llvm
  210. #endif // LLVM_OBJECTYAML_COFFYAML_H
  211. #ifdef __GNUC__
  212. #pragma GCC diagnostic pop
  213. #endif