blob: 78f021fc0386987e15140ff241bf8b3ad7992e04 [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;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010070 std::vector<CodeViewYAML::LeafRecord> DebugP;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010071 Optional<CodeViewYAML::DebugHSection> DebugH;
72 std::vector<Relocation> Relocations;
73 StringRef Name;
74
75 Section();
76};
77
78struct Symbol {
79 COFF::symbol Header;
80 COFF::SymbolBaseType SimpleType = COFF::IMAGE_SYM_TYPE_NULL;
81 COFF::SymbolComplexType ComplexType = COFF::IMAGE_SYM_DTYPE_NULL;
82 Optional<COFF::AuxiliaryFunctionDefinition> FunctionDefinition;
83 Optional<COFF::AuxiliarybfAndefSymbol> bfAndefSymbol;
84 Optional<COFF::AuxiliaryWeakExternal> WeakExternal;
85 StringRef File;
86 Optional<COFF::AuxiliarySectionDefinition> SectionDefinition;
87 Optional<COFF::AuxiliaryCLRToken> CLRToken;
88 StringRef Name;
89
90 Symbol();
91};
92
93struct PEHeader {
94 COFF::PE32Header Header;
95 Optional<COFF::DataDirectory> DataDirectories[COFF::NUM_DATA_DIRECTORIES];
96};
97
98struct Object {
99 Optional<PEHeader> OptionalHeader;
100 COFF::header Header;
101 std::vector<Section> Sections;
102 std::vector<Symbol> Symbols;
103
104 Object();
105};
106
107} // end namespace COFFYAML
108
109} // end namespace llvm
110
111LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section)
112LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)
113LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Relocation)
114
115namespace llvm {
116namespace yaml {
117
118template <>
119struct ScalarEnumerationTraits<COFFYAML::WeakExternalCharacteristics> {
120 static void enumeration(IO &IO, COFFYAML::WeakExternalCharacteristics &Value);
121};
122
123template <>
124struct ScalarEnumerationTraits<COFFYAML::AuxSymbolType> {
125 static void enumeration(IO &IO, COFFYAML::AuxSymbolType &Value);
126};
127
128template <>
129struct ScalarEnumerationTraits<COFFYAML::COMDATType> {
130 static void enumeration(IO &IO, COFFYAML::COMDATType &Value);
131};
132
133template <>
134struct ScalarEnumerationTraits<COFF::MachineTypes> {
135 static void enumeration(IO &IO, COFF::MachineTypes &Value);
136};
137
138template <>
139struct ScalarEnumerationTraits<COFF::SymbolBaseType> {
140 static void enumeration(IO &IO, COFF::SymbolBaseType &Value);
141};
142
143template <>
144struct ScalarEnumerationTraits<COFF::SymbolStorageClass> {
145 static void enumeration(IO &IO, COFF::SymbolStorageClass &Value);
146};
147
148template <>
149struct ScalarEnumerationTraits<COFF::SymbolComplexType> {
150 static void enumeration(IO &IO, COFF::SymbolComplexType &Value);
151};
152
153template <>
154struct ScalarEnumerationTraits<COFF::RelocationTypeI386> {
155 static void enumeration(IO &IO, COFF::RelocationTypeI386 &Value);
156};
157
158template <>
159struct ScalarEnumerationTraits<COFF::RelocationTypeAMD64> {
160 static void enumeration(IO &IO, COFF::RelocationTypeAMD64 &Value);
161};
162
163template <>
164struct ScalarEnumerationTraits<COFF::RelocationTypesARM> {
165 static void enumeration(IO &IO, COFF::RelocationTypesARM &Value);
166};
167
168template <>
169struct ScalarEnumerationTraits<COFF::RelocationTypesARM64> {
170 static void enumeration(IO &IO, COFF::RelocationTypesARM64 &Value);
171};
172
173template <>
174struct ScalarEnumerationTraits<COFF::WindowsSubsystem> {
175 static void enumeration(IO &IO, COFF::WindowsSubsystem &Value);
176};
177
178template <>
179struct ScalarBitSetTraits<COFF::Characteristics> {
180 static void bitset(IO &IO, COFF::Characteristics &Value);
181};
182
183template <>
184struct ScalarBitSetTraits<COFF::SectionCharacteristics> {
185 static void bitset(IO &IO, COFF::SectionCharacteristics &Value);
186};
187
188template <>
189struct ScalarBitSetTraits<COFF::DLLCharacteristics> {
190 static void bitset(IO &IO, COFF::DLLCharacteristics &Value);
191};
192
193template <>
194struct MappingTraits<COFFYAML::Relocation> {
195 static void mapping(IO &IO, COFFYAML::Relocation &Rel);
196};
197
198template <>
199struct MappingTraits<COFFYAML::PEHeader> {
200 static void mapping(IO &IO, COFFYAML::PEHeader &PH);
201};
202
203template <>
204struct MappingTraits<COFF::DataDirectory> {
205 static void mapping(IO &IO, COFF::DataDirectory &DD);
206};
207
208template <>
209struct MappingTraits<COFF::header> {
210 static void mapping(IO &IO, COFF::header &H);
211};
212
213template <> struct MappingTraits<COFF::AuxiliaryFunctionDefinition> {
214 static void mapping(IO &IO, COFF::AuxiliaryFunctionDefinition &AFD);
215};
216
217template <> struct MappingTraits<COFF::AuxiliarybfAndefSymbol> {
218 static void mapping(IO &IO, COFF::AuxiliarybfAndefSymbol &AAS);
219};
220
221template <> struct MappingTraits<COFF::AuxiliaryWeakExternal> {
222 static void mapping(IO &IO, COFF::AuxiliaryWeakExternal &AWE);
223};
224
225template <> struct MappingTraits<COFF::AuxiliarySectionDefinition> {
226 static void mapping(IO &IO, COFF::AuxiliarySectionDefinition &ASD);
227};
228
229template <> struct MappingTraits<COFF::AuxiliaryCLRToken> {
230 static void mapping(IO &IO, COFF::AuxiliaryCLRToken &ACT);
231};
232
233template <>
234struct MappingTraits<COFFYAML::Symbol> {
235 static void mapping(IO &IO, COFFYAML::Symbol &S);
236};
237
238template <>
239struct MappingTraits<COFFYAML::Section> {
240 static void mapping(IO &IO, COFFYAML::Section &Sec);
241};
242
243template <>
244struct MappingTraits<COFFYAML::Object> {
245 static void mapping(IO &IO, COFFYAML::Object &Obj);
246};
247
248} // end namespace yaml
249} // end namespace llvm
250
251#endif // LLVM_OBJECTYAML_COFFYAML_H