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