COFFYAML.h 7.3 KB

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