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_OBJECT_COFFYAML_H
15 #define LLVM_OBJECT_COFFYAML_H
16 
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/MC/YAML.h"
19 #include "llvm/Support/COFF.h"
20 
21 namespace llvm {
22 
23 namespace COFF {
24 inline Characteristics operator|(Characteristics a, Characteristics b) {
25   uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
26   return static_cast<Characteristics>(Ret);
27 }
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 
35 inline DLLCharacteristics operator|(DLLCharacteristics a,
36                                     DLLCharacteristics b) {
37   uint16_t Ret = static_cast<uint16_t>(a) | static_cast<uint16_t>(b);
38   return static_cast<DLLCharacteristics>(Ret);
39 }
40 }
41 
42 // The structure of the yaml files is not an exact 1:1 match to COFF. In order
43 // to use yaml::IO, we use these structures which are closer to the source.
44 namespace COFFYAML {
45   LLVM_YAML_STRONG_TYPEDEF(uint8_t, COMDATType)
46   LLVM_YAML_STRONG_TYPEDEF(uint32_t, WeakExternalCharacteristics)
47   LLVM_YAML_STRONG_TYPEDEF(uint8_t, AuxSymbolType)
48 
49   struct Relocation {
50     uint32_t VirtualAddress;
51     uint16_t Type;
52     StringRef SymbolName;
53   };
54 
55   struct Section {
56     COFF::section Header;
57     unsigned Alignment;
58     yaml::BinaryRef SectionData;
59     std::vector<Relocation> Relocations;
60     StringRef Name;
61     Section();
62   };
63 
64   struct Symbol {
65     COFF::symbol Header;
66     COFF::SymbolBaseType SimpleType;
67     COFF::SymbolComplexType ComplexType;
68     Optional<COFF::AuxiliaryFunctionDefinition> FunctionDefinition;
69     Optional<COFF::AuxiliarybfAndefSymbol> bfAndefSymbol;
70     Optional<COFF::AuxiliaryWeakExternal> WeakExternal;
71     StringRef File;
72     Optional<COFF::AuxiliarySectionDefinition> SectionDefinition;
73     Optional<COFF::AuxiliaryCLRToken> CLRToken;
74     StringRef Name;
75     Symbol();
76   };
77 
78   struct PEHeader {
79     COFF::PE32Header Header;
80     Optional<COFF::DataDirectory> DataDirectories[COFF::NUM_DATA_DIRECTORIES];
81   };
82 
83   struct Object {
84     Optional<PEHeader> OptionalHeader;
85     COFF::header Header;
86     std::vector<Section> Sections;
87     std::vector<Symbol> Symbols;
88     Object();
89   };
90 }
91 }
92 
93 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section)
LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)94 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)
95 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Relocation)
96 
97 namespace llvm {
98 namespace yaml {
99 
100 template <>
101 struct ScalarEnumerationTraits<COFFYAML::WeakExternalCharacteristics> {
102   static void enumeration(IO &IO, COFFYAML::WeakExternalCharacteristics &Value);
103 };
104 
105 template <>
106 struct ScalarEnumerationTraits<COFFYAML::AuxSymbolType> {
107   static void enumeration(IO &IO, COFFYAML::AuxSymbolType &Value);
108 };
109 
110 template <>
111 struct ScalarEnumerationTraits<COFFYAML::COMDATType> {
112   static void enumeration(IO &IO, COFFYAML::COMDATType &Value);
113 };
114 
115 template <>
116 struct ScalarEnumerationTraits<COFF::MachineTypes> {
117   static void enumeration(IO &IO, COFF::MachineTypes &Value);
118 };
119 
120 template <>
121 struct ScalarEnumerationTraits<COFF::SymbolBaseType> {
122   static void enumeration(IO &IO, COFF::SymbolBaseType &Value);
123 };
124 
125 template <>
126 struct ScalarEnumerationTraits<COFF::SymbolStorageClass> {
127   static void enumeration(IO &IO, COFF::SymbolStorageClass &Value);
128 };
129 
130 template <>
131 struct ScalarEnumerationTraits<COFF::SymbolComplexType> {
132   static void enumeration(IO &IO, COFF::SymbolComplexType &Value);
133 };
134 
135 template <>
136 struct ScalarEnumerationTraits<COFF::RelocationTypeI386> {
137   static void enumeration(IO &IO, COFF::RelocationTypeI386 &Value);
138 };
139 
140 template <>
141 struct ScalarEnumerationTraits<COFF::RelocationTypeAMD64> {
142   static void enumeration(IO &IO, COFF::RelocationTypeAMD64 &Value);
143 };
144 
145 template <>
146 struct ScalarEnumerationTraits<COFF::WindowsSubsystem> {
147   static void enumeration(IO &IO, COFF::WindowsSubsystem &Value);
148 };
149 
150 template <>
151 struct ScalarBitSetTraits<COFF::Characteristics> {
152   static void bitset(IO &IO, COFF::Characteristics &Value);
153 };
154 
155 template <>
156 struct ScalarBitSetTraits<COFF::SectionCharacteristics> {
157   static void bitset(IO &IO, COFF::SectionCharacteristics &Value);
158 };
159 
160 template <>
161 struct ScalarBitSetTraits<COFF::DLLCharacteristics> {
162   static void bitset(IO &IO, COFF::DLLCharacteristics &Value);
163 };
164 
165 template <>
166 struct MappingTraits<COFFYAML::Relocation> {
167   static void mapping(IO &IO, COFFYAML::Relocation &Rel);
168 };
169 
170 template <>
171 struct MappingTraits<COFFYAML::PEHeader> {
172   static void mapping(IO &IO, COFFYAML::PEHeader &PH);
173 };
174 
175 template <>
176 struct MappingTraits<COFF::DataDirectory> {
177   static void mapping(IO &IO, COFF::DataDirectory &DD);
178 };
179 
180 template <>
181 struct MappingTraits<COFF::header> {
182   static void mapping(IO &IO, COFF::header &H);
183 };
184 
185 template <> struct MappingTraits<COFF::AuxiliaryFunctionDefinition> {
186   static void mapping(IO &IO, COFF::AuxiliaryFunctionDefinition &AFD);
187 };
188 
189 template <> struct MappingTraits<COFF::AuxiliarybfAndefSymbol> {
190   static void mapping(IO &IO, COFF::AuxiliarybfAndefSymbol &AAS);
191 };
192 
193 template <> struct MappingTraits<COFF::AuxiliaryWeakExternal> {
194   static void mapping(IO &IO, COFF::AuxiliaryWeakExternal &AWE);
195 };
196 
197 template <> struct MappingTraits<COFF::AuxiliarySectionDefinition> {
198   static void mapping(IO &IO, COFF::AuxiliarySectionDefinition &ASD);
199 };
200 
201 template <> struct MappingTraits<COFF::AuxiliaryCLRToken> {
202   static void mapping(IO &IO, COFF::AuxiliaryCLRToken &ACT);
203 };
204 
205 template <>
206 struct MappingTraits<COFFYAML::Symbol> {
207   static void mapping(IO &IO, COFFYAML::Symbol &S);
208 };
209 
210 template <>
211 struct MappingTraits<COFFYAML::Section> {
212   static void mapping(IO &IO, COFFYAML::Section &Sec);
213 };
214 
215 template <>
216 struct MappingTraits<COFFYAML::Object> {
217   static void mapping(IO &IO, COFFYAML::Object &Obj);
218 };
219 
220 } // end namespace yaml
221 } // end namespace llvm
222 
223 #endif
224