COFFYAML.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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/Optional.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/BinaryFormat/COFF.h"
  22. #include "llvm/ObjectYAML/CodeViewYAMLDebugSections.h"
  23. #include "llvm/ObjectYAML/CodeViewYAMLTypeHashing.h"
  24. #include "llvm/ObjectYAML/CodeViewYAMLTypes.h"
  25. #include "llvm/ObjectYAML/YAML.h"
  26. #include <cstdint>
  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. 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. 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. Optional<COFF::AuxiliaryFunctionDefinition> FunctionDefinition;
  78. Optional<COFF::AuxiliarybfAndefSymbol> bfAndefSymbol;
  79. Optional<COFF::AuxiliaryWeakExternal> WeakExternal;
  80. StringRef File;
  81. Optional<COFF::AuxiliarySectionDefinition> SectionDefinition;
  82. Optional<COFF::AuxiliaryCLRToken> CLRToken;
  83. StringRef Name;
  84. Symbol();
  85. };
  86. struct PEHeader {
  87. COFF::PE32Header Header;
  88. Optional<COFF::DataDirectory> DataDirectories[COFF::NUM_DATA_DIRECTORIES];
  89. };
  90. struct Object {
  91. Optional<PEHeader> OptionalHeader;
  92. COFF::header Header;
  93. std::vector<Section> Sections;
  94. std::vector<Symbol> Symbols;
  95. Object();
  96. };
  97. } // end namespace COFFYAML
  98. } // end namespace llvm
  99. LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section)
  100. LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)
  101. LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Relocation)
  102. namespace llvm {
  103. namespace yaml {
  104. template <>
  105. struct ScalarEnumerationTraits<COFFYAML::WeakExternalCharacteristics> {
  106. static void enumeration(IO &IO, COFFYAML::WeakExternalCharacteristics &Value);
  107. };
  108. template <>
  109. struct ScalarEnumerationTraits<COFFYAML::AuxSymbolType> {
  110. static void enumeration(IO &IO, COFFYAML::AuxSymbolType &Value);
  111. };
  112. template <>
  113. struct ScalarEnumerationTraits<COFFYAML::COMDATType> {
  114. static void enumeration(IO &IO, COFFYAML::COMDATType &Value);
  115. };
  116. template <>
  117. struct ScalarEnumerationTraits<COFF::MachineTypes> {
  118. static void enumeration(IO &IO, COFF::MachineTypes &Value);
  119. };
  120. template <>
  121. struct ScalarEnumerationTraits<COFF::SymbolBaseType> {
  122. static void enumeration(IO &IO, COFF::SymbolBaseType &Value);
  123. };
  124. template <>
  125. struct ScalarEnumerationTraits<COFF::SymbolStorageClass> {
  126. static void enumeration(IO &IO, COFF::SymbolStorageClass &Value);
  127. };
  128. template <>
  129. struct ScalarEnumerationTraits<COFF::SymbolComplexType> {
  130. static void enumeration(IO &IO, COFF::SymbolComplexType &Value);
  131. };
  132. template <>
  133. struct ScalarEnumerationTraits<COFF::RelocationTypeI386> {
  134. static void enumeration(IO &IO, COFF::RelocationTypeI386 &Value);
  135. };
  136. template <>
  137. struct ScalarEnumerationTraits<COFF::RelocationTypeAMD64> {
  138. static void enumeration(IO &IO, COFF::RelocationTypeAMD64 &Value);
  139. };
  140. template <>
  141. struct ScalarEnumerationTraits<COFF::RelocationTypesARM> {
  142. static void enumeration(IO &IO, COFF::RelocationTypesARM &Value);
  143. };
  144. template <>
  145. struct ScalarEnumerationTraits<COFF::RelocationTypesARM64> {
  146. static void enumeration(IO &IO, COFF::RelocationTypesARM64 &Value);
  147. };
  148. template <>
  149. struct ScalarEnumerationTraits<COFF::WindowsSubsystem> {
  150. static void enumeration(IO &IO, COFF::WindowsSubsystem &Value);
  151. };
  152. template <>
  153. struct ScalarBitSetTraits<COFF::Characteristics> {
  154. static void bitset(IO &IO, COFF::Characteristics &Value);
  155. };
  156. template <>
  157. struct ScalarBitSetTraits<COFF::SectionCharacteristics> {
  158. static void bitset(IO &IO, COFF::SectionCharacteristics &Value);
  159. };
  160. template <>
  161. struct ScalarBitSetTraits<COFF::DLLCharacteristics> {
  162. static void bitset(IO &IO, COFF::DLLCharacteristics &Value);
  163. };
  164. template <>
  165. struct MappingTraits<COFFYAML::Relocation> {
  166. static void mapping(IO &IO, COFFYAML::Relocation &Rel);
  167. };
  168. template <>
  169. struct MappingTraits<COFFYAML::PEHeader> {
  170. static void mapping(IO &IO, COFFYAML::PEHeader &PH);
  171. };
  172. template <>
  173. struct MappingTraits<COFF::DataDirectory> {
  174. static void mapping(IO &IO, COFF::DataDirectory &DD);
  175. };
  176. template <>
  177. struct MappingTraits<COFF::header> {
  178. static void mapping(IO &IO, COFF::header &H);
  179. };
  180. template <> struct MappingTraits<COFF::AuxiliaryFunctionDefinition> {
  181. static void mapping(IO &IO, COFF::AuxiliaryFunctionDefinition &AFD);
  182. };
  183. template <> struct MappingTraits<COFF::AuxiliarybfAndefSymbol> {
  184. static void mapping(IO &IO, COFF::AuxiliarybfAndefSymbol &AAS);
  185. };
  186. template <> struct MappingTraits<COFF::AuxiliaryWeakExternal> {
  187. static void mapping(IO &IO, COFF::AuxiliaryWeakExternal &AWE);
  188. };
  189. template <> struct MappingTraits<COFF::AuxiliarySectionDefinition> {
  190. static void mapping(IO &IO, COFF::AuxiliarySectionDefinition &ASD);
  191. };
  192. template <> struct MappingTraits<COFF::AuxiliaryCLRToken> {
  193. static void mapping(IO &IO, COFF::AuxiliaryCLRToken &ACT);
  194. };
  195. template <>
  196. struct MappingTraits<COFFYAML::Symbol> {
  197. static void mapping(IO &IO, COFFYAML::Symbol &S);
  198. };
  199. template <>
  200. struct MappingTraits<COFFYAML::Section> {
  201. static void mapping(IO &IO, COFFYAML::Section &Sec);
  202. };
  203. template <>
  204. struct MappingTraits<COFFYAML::Object> {
  205. static void mapping(IO &IO, COFFYAML::Object &Obj);
  206. };
  207. } // end namespace yaml
  208. } // end namespace llvm
  209. #endif // LLVM_OBJECTYAML_COFFYAML_H
  210. #ifdef __GNUC__
  211. #pragma GCC diagnostic pop
  212. #endif