1//===- COFFYAML.h - COFF YAMLIO implementation ------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares classes for handling the YAML representation of COFF.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_OBJECTYAML_COFFYAML_H
15#define LLVM_OBJECTYAML_COFFYAML_H
16
17#include "llvm/ADT/Optional.h"
18#include "llvm/BinaryFormat/COFF.h"
19#include "llvm/ObjectYAML/CodeViewYAMLDebugSections.h"
20#include "llvm/ObjectYAML/CodeViewYAMLTypes.h"
21#include "llvm/ObjectYAML/YAML.h"
22
23namespace llvm {
24
25namespace COFF {
26inline Characteristics operator|(Characteristics a, Characteristics b) {
27  uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
28  return static_cast<Characteristics>(Ret);
29}
30
31inline SectionCharacteristics operator|(SectionCharacteristics a,
32                                        SectionCharacteristics b) {
33  uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
34  return static_cast<SectionCharacteristics>(Ret);
35}
36
37inline DLLCharacteristics operator|(DLLCharacteristics a,
38                                    DLLCharacteristics b) {
39  uint16_t Ret = static_cast<uint16_t>(a) | static_cast<uint16_t>(b);
40  return static_cast<DLLCharacteristics>(Ret);
41}
42}
43
44// The structure of the yaml files is not an exact 1:1 match to COFF. In order
45// to use yaml::IO, we use these structures which are closer to the source.
46namespace COFFYAML {
47  LLVM_YAML_STRONG_TYPEDEF(uint8_t, COMDATType)
48  LLVM_YAML_STRONG_TYPEDEF(uint32_t, WeakExternalCharacteristics)
49  LLVM_YAML_STRONG_TYPEDEF(uint8_t, AuxSymbolType)
50
51  struct Relocation {
52    uint32_t VirtualAddress;
53    uint16_t Type;
54    StringRef SymbolName;
55  };
56
57  struct Section {
58    COFF::section Header;
59    unsigned Alignment = 0;
60    yaml::BinaryRef SectionData;
61    std::vector<CodeViewYAML::YAMLDebugSubsection> DebugS;
62    std::vector<CodeViewYAML::LeafRecord> DebugT;
63    std::vector<Relocation> Relocations;
64    StringRef Name;
65    Section();
66  };
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
82  struct PEHeader {
83    COFF::PE32Header Header;
84    Optional<COFF::DataDirectory> DataDirectories[COFF::NUM_DATA_DIRECTORIES];
85  };
86
87  struct Object {
88    Optional<PEHeader> OptionalHeader;
89    COFF::header Header;
90    std::vector<Section> Sections;
91    std::vector<Symbol> Symbols;
92    Object();
93  };
94}
95}
96
97LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section)
98LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)
99LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Relocation)
100
101namespace llvm {
102namespace yaml {
103
104template <>
105struct ScalarEnumerationTraits<COFFYAML::WeakExternalCharacteristics> {
106  static void enumeration(IO &IO, COFFYAML::WeakExternalCharacteristics &Value);
107};
108
109template <>
110struct ScalarEnumerationTraits<COFFYAML::AuxSymbolType> {
111  static void enumeration(IO &IO, COFFYAML::AuxSymbolType &Value);
112};
113
114template <>
115struct ScalarEnumerationTraits<COFFYAML::COMDATType> {
116  static void enumeration(IO &IO, COFFYAML::COMDATType &Value);
117};
118
119template <>
120struct ScalarEnumerationTraits<COFF::MachineTypes> {
121  static void enumeration(IO &IO, COFF::MachineTypes &Value);
122};
123
124template <>
125struct ScalarEnumerationTraits<COFF::SymbolBaseType> {
126  static void enumeration(IO &IO, COFF::SymbolBaseType &Value);
127};
128
129template <>
130struct ScalarEnumerationTraits<COFF::SymbolStorageClass> {
131  static void enumeration(IO &IO, COFF::SymbolStorageClass &Value);
132};
133
134template <>
135struct ScalarEnumerationTraits<COFF::SymbolComplexType> {
136  static void enumeration(IO &IO, COFF::SymbolComplexType &Value);
137};
138
139template <>
140struct ScalarEnumerationTraits<COFF::RelocationTypeI386> {
141  static void enumeration(IO &IO, COFF::RelocationTypeI386 &Value);
142};
143
144template <>
145struct ScalarEnumerationTraits<COFF::RelocationTypeAMD64> {
146  static void enumeration(IO &IO, COFF::RelocationTypeAMD64 &Value);
147};
148
149template <>
150struct ScalarEnumerationTraits<COFF::WindowsSubsystem> {
151  static void enumeration(IO &IO, COFF::WindowsSubsystem &Value);
152};
153
154template <>
155struct ScalarBitSetTraits<COFF::Characteristics> {
156  static void bitset(IO &IO, COFF::Characteristics &Value);
157};
158
159template <>
160struct ScalarBitSetTraits<COFF::SectionCharacteristics> {
161  static void bitset(IO &IO, COFF::SectionCharacteristics &Value);
162};
163
164template <>
165struct ScalarBitSetTraits<COFF::DLLCharacteristics> {
166  static void bitset(IO &IO, COFF::DLLCharacteristics &Value);
167};
168
169template <>
170struct MappingTraits<COFFYAML::Relocation> {
171  static void mapping(IO &IO, COFFYAML::Relocation &Rel);
172};
173
174template <>
175struct MappingTraits<COFFYAML::PEHeader> {
176  static void mapping(IO &IO, COFFYAML::PEHeader &PH);
177};
178
179template <>
180struct MappingTraits<COFF::DataDirectory> {
181  static void mapping(IO &IO, COFF::DataDirectory &DD);
182};
183
184template <>
185struct MappingTraits<COFF::header> {
186  static void mapping(IO &IO, COFF::header &H);
187};
188
189template <> struct MappingTraits<COFF::AuxiliaryFunctionDefinition> {
190  static void mapping(IO &IO, COFF::AuxiliaryFunctionDefinition &AFD);
191};
192
193template <> struct MappingTraits<COFF::AuxiliarybfAndefSymbol> {
194  static void mapping(IO &IO, COFF::AuxiliarybfAndefSymbol &AAS);
195};
196
197template <> struct MappingTraits<COFF::AuxiliaryWeakExternal> {
198  static void mapping(IO &IO, COFF::AuxiliaryWeakExternal &AWE);
199};
200
201template <> struct MappingTraits<COFF::AuxiliarySectionDefinition> {
202  static void mapping(IO &IO, COFF::AuxiliarySectionDefinition &ASD);
203};
204
205template <> struct MappingTraits<COFF::AuxiliaryCLRToken> {
206  static void mapping(IO &IO, COFF::AuxiliaryCLRToken &ACT);
207};
208
209template <>
210struct MappingTraits<COFFYAML::Symbol> {
211  static void mapping(IO &IO, COFFYAML::Symbol &S);
212};
213
214template <>
215struct MappingTraits<COFFYAML::Section> {
216  static void mapping(IO &IO, COFFYAML::Section &Sec);
217};
218
219template <>
220struct MappingTraits<COFFYAML::Object> {
221  static void mapping(IO &IO, COFFYAML::Object &Obj);
222};
223
224} // end namespace yaml
225} // end namespace llvm
226
227#endif
228