LLVM 20.0.0git
COFFYAML.h
Go to the documentation of this file.
1//===- COFFYAML.h - COFF YAMLIO implementation ------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares classes for handling the YAML representation of COFF.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_OBJECTYAML_COFFYAML_H
14#define LLVM_OBJECTYAML_COFFYAML_H
15
16#include "llvm/ADT/StringRef.h"
18#include "llvm/Object/COFF.h"
23#include <cstdint>
24#include <optional>
25#include <vector>
26
27namespace llvm {
28
29namespace COFF {
30
32 uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
33 return static_cast<Characteristics>(Ret);
34}
35
38 uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
39 return static_cast<SectionCharacteristics>(Ret);
40}
41
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
55LLVM_YAML_STRONG_TYPEDEF(uint32_t, WeakExternalCharacteristics)
57
58struct Relocation {
61
62 // Normally a Relocation can refer to the symbol via its name.
63 // It can also use a direct symbol table index instead (with no name
64 // specified), allowing disambiguating between multiple symbols with the
65 // same name or crafting intentionally broken files for testing.
67 std::optional<uint32_t> SymbolTableIndex;
68};
69
71 std::optional<uint32_t> UInt32;
73 std::optional<object::coff_load_configuration32> LoadConfig32;
74 std::optional<object::coff_load_configuration64> LoadConfig64;
75
76 size_t size() const;
77 void writeAsBinary(raw_ostream &OS) const;
78};
79
80struct Section {
82 unsigned Alignment = 0;
84 std::vector<CodeViewYAML::YAMLDebugSubsection> DebugS;
85 std::vector<CodeViewYAML::LeafRecord> DebugT;
86 std::vector<CodeViewYAML::LeafRecord> DebugP;
87 std::optional<CodeViewYAML::DebugHSection> DebugH;
88 std::vector<SectionDataEntry> StructuredData;
89 std::vector<Relocation> Relocations;
91
92 Section();
93};
94
95struct Symbol {
99 std::optional<COFF::AuxiliaryFunctionDefinition> FunctionDefinition;
100 std::optional<COFF::AuxiliarybfAndefSymbol> bfAndefSymbol;
101 std::optional<COFF::AuxiliaryWeakExternal> WeakExternal;
103 std::optional<COFF::AuxiliarySectionDefinition> SectionDefinition;
104 std::optional<COFF::AuxiliaryCLRToken> CLRToken;
106
107 Symbol();
108};
109
110struct PEHeader {
112 std::optional<COFF::DataDirectory>
114};
115
116struct Object {
117 std::optional<PEHeader> OptionalHeader;
119 std::vector<Section> Sections;
120 std::vector<Symbol> Symbols;
121
122 Object();
123};
124
125} // end namespace COFFYAML
126
127} // end namespace llvm
128
129LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section)
130LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)
131LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Relocation)
132LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::SectionDataEntry)
133
134namespace llvm {
135namespace yaml {
136
137template <>
138struct ScalarEnumerationTraits<COFFYAML::WeakExternalCharacteristics> {
139 static void enumeration(IO &IO, COFFYAML::WeakExternalCharacteristics &Value);
140};
141
142template <>
143struct ScalarEnumerationTraits<COFFYAML::AuxSymbolType> {
144 static void enumeration(IO &IO, COFFYAML::AuxSymbolType &Value);
145};
146
147template <>
148struct ScalarEnumerationTraits<COFFYAML::COMDATType> {
149 static void enumeration(IO &IO, COFFYAML::COMDATType &Value);
150};
151
152template <>
153struct ScalarEnumerationTraits<COFF::MachineTypes> {
154 static void enumeration(IO &IO, COFF::MachineTypes &Value);
155};
156
157template <>
158struct ScalarEnumerationTraits<COFF::SymbolBaseType> {
159 static void enumeration(IO &IO, COFF::SymbolBaseType &Value);
160};
161
162template <>
163struct ScalarEnumerationTraits<COFF::SymbolStorageClass> {
164 static void enumeration(IO &IO, COFF::SymbolStorageClass &Value);
165};
166
167template <>
168struct ScalarEnumerationTraits<COFF::SymbolComplexType> {
169 static void enumeration(IO &IO, COFF::SymbolComplexType &Value);
170};
171
172template <>
173struct ScalarEnumerationTraits<COFF::RelocationTypeI386> {
174 static void enumeration(IO &IO, COFF::RelocationTypeI386 &Value);
175};
176
177template <>
178struct ScalarEnumerationTraits<COFF::RelocationTypeAMD64> {
179 static void enumeration(IO &IO, COFF::RelocationTypeAMD64 &Value);
180};
181
182template <> struct ScalarEnumerationTraits<COFF::RelocationTypesMips> {
183 static void enumeration(IO &IO, COFF::RelocationTypesMips &Value);
184};
185
186template <>
187struct ScalarEnumerationTraits<COFF::RelocationTypesARM> {
188 static void enumeration(IO &IO, COFF::RelocationTypesARM &Value);
189};
190
191template <>
192struct ScalarEnumerationTraits<COFF::RelocationTypesARM64> {
193 static void enumeration(IO &IO, COFF::RelocationTypesARM64 &Value);
194};
195
196template <>
197struct ScalarEnumerationTraits<COFF::WindowsSubsystem> {
198 static void enumeration(IO &IO, COFF::WindowsSubsystem &Value);
199};
200
201template <>
202struct ScalarBitSetTraits<COFF::Characteristics> {
203 static void bitset(IO &IO, COFF::Characteristics &Value);
204};
205
206template <>
207struct ScalarBitSetTraits<COFF::SectionCharacteristics> {
208 static void bitset(IO &IO, COFF::SectionCharacteristics &Value);
209};
210
211template <>
212struct ScalarBitSetTraits<COFF::DLLCharacteristics> {
213 static void bitset(IO &IO, COFF::DLLCharacteristics &Value);
214};
215
216template <>
217struct MappingTraits<COFFYAML::Relocation> {
218 static void mapping(IO &IO, COFFYAML::Relocation &Rel);
219};
220
221template <>
222struct MappingTraits<COFFYAML::PEHeader> {
223 static void mapping(IO &IO, COFFYAML::PEHeader &PH);
224};
225
226template <>
227struct MappingTraits<COFF::DataDirectory> {
228 static void mapping(IO &IO, COFF::DataDirectory &DD);
229};
230
231template <>
232struct MappingTraits<COFF::header> {
233 static void mapping(IO &IO, COFF::header &H);
234};
235
236template <> struct MappingTraits<COFF::AuxiliaryFunctionDefinition> {
237 static void mapping(IO &IO, COFF::AuxiliaryFunctionDefinition &AFD);
238};
239
240template <> struct MappingTraits<COFF::AuxiliarybfAndefSymbol> {
241 static void mapping(IO &IO, COFF::AuxiliarybfAndefSymbol &AAS);
242};
243
244template <> struct MappingTraits<COFF::AuxiliaryWeakExternal> {
245 static void mapping(IO &IO, COFF::AuxiliaryWeakExternal &AWE);
246};
247
248template <> struct MappingTraits<COFF::AuxiliarySectionDefinition> {
249 static void mapping(IO &IO, COFF::AuxiliarySectionDefinition &ASD);
250};
251
252template <> struct MappingTraits<COFF::AuxiliaryCLRToken> {
253 static void mapping(IO &IO, COFF::AuxiliaryCLRToken &ACT);
254};
255
256template <> struct MappingTraits<object::coff_load_configuration32> {
257 static void mapping(IO &IO, object::coff_load_configuration32 &ACT);
258};
259
260template <> struct MappingTraits<object::coff_load_configuration64> {
261 static void mapping(IO &IO, object::coff_load_configuration64 &ACT);
262};
263
264template <> struct MappingTraits<object::coff_load_config_code_integrity> {
265 static void mapping(IO &IO, object::coff_load_config_code_integrity &ACT);
266};
267
268template <>
269struct MappingTraits<COFFYAML::Symbol> {
270 static void mapping(IO &IO, COFFYAML::Symbol &S);
271};
272
273template <> struct MappingTraits<COFFYAML::SectionDataEntry> {
274 static void mapping(IO &IO, COFFYAML::SectionDataEntry &Sec);
275};
276
277template <>
278struct MappingTraits<COFFYAML::Section> {
279 static void mapping(IO &IO, COFFYAML::Section &Sec);
280};
281
282template <>
283struct MappingTraits<COFFYAML::Object> {
284 static void mapping(IO &IO, COFFYAML::Object &Obj);
285};
286
287} // end namespace yaml
288} // end namespace llvm
289
290#endif // LLVM_OBJECTYAML_COFFYAML_H
COFFYAML::WeakExternalCharacteristics Characteristics
Definition: COFFYAML.cpp:350
#define H(x, y, z)
Definition: MD5.cpp:57
raw_pwrite_stream & OS
#define LLVM_YAML_IS_SEQUENCE_VECTOR(type)
#define LLVM_YAML_STRONG_TYPEDEF(_base, _type)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Specialized YAMLIO scalar type for representing a binary blob.
Definition: YAML.h:63
DLLCharacteristics
Definition: COFF.h:668
Characteristics operator|(Characteristics a, Characteristics b)
Definition: COFFYAML.h:31
RelocationTypesMips
Definition: COFF.h:420
MachineTypes
Definition: COFF.h:92
SectionCharacteristics
Definition: COFF.h:297
RelocationTypesARM64
Definition: COFF.h:399
RelocationTypeAMD64
Definition: COFF.h:359
@ NUM_DATA_DIRECTORIES
Definition: COFF.h:646
SymbolStorageClass
Storage class tells where and what the symbol represents.
Definition: COFF.h:217
WindowsSubsystem
Definition: COFF.h:649
RelocationTypesARM
Definition: COFF.h:379
RelocationTypeI386
Definition: COFF.h:345
SymbolBaseType
Definition: COFF.h:253
@ IMAGE_SYM_TYPE_NULL
No type information or unknown base type.
Definition: COFF.h:254
Characteristics
Definition: COFF.h:137
SymbolComplexType
Definition: COFF.h:272
@ IMAGE_SYM_DTYPE_NULL
No complex type; simple scalar variable.
Definition: COFF.h:273
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::optional< PEHeader > OptionalHeader
Definition: COFFYAML.h:117
std::vector< Section > Sections
Definition: COFFYAML.h:119
std::vector< Symbol > Symbols
Definition: COFFYAML.h:120
COFF::header Header
Definition: COFFYAML.h:118
std::optional< COFF::DataDirectory > DataDirectories[COFF::NUM_DATA_DIRECTORIES]
Definition: COFFYAML.h:113
COFF::PE32Header Header
Definition: COFFYAML.h:111
std::optional< uint32_t > SymbolTableIndex
Definition: COFFYAML.h:67
std::optional< object::coff_load_configuration64 > LoadConfig64
Definition: COFFYAML.h:74
std::optional< object::coff_load_configuration32 > LoadConfig32
Definition: COFFYAML.h:73
void writeAsBinary(raw_ostream &OS) const
std::optional< uint32_t > UInt32
Definition: COFFYAML.h:71
std::vector< CodeViewYAML::YAMLDebugSubsection > DebugS
Definition: COFFYAML.h:84
std::vector< SectionDataEntry > StructuredData
Definition: COFFYAML.h:88
std::vector< CodeViewYAML::LeafRecord > DebugT
Definition: COFFYAML.h:85
yaml::BinaryRef SectionData
Definition: COFFYAML.h:83
std::optional< CodeViewYAML::DebugHSection > DebugH
Definition: COFFYAML.h:87
std::vector< CodeViewYAML::LeafRecord > DebugP
Definition: COFFYAML.h:86
COFF::section Header
Definition: COFFYAML.h:81
std::vector< Relocation > Relocations
Definition: COFFYAML.h:89
std::optional< COFF::AuxiliaryWeakExternal > WeakExternal
Definition: COFFYAML.h:101
std::optional< COFF::AuxiliarybfAndefSymbol > bfAndefSymbol
Definition: COFFYAML.h:100
COFF::SymbolComplexType ComplexType
Definition: COFFYAML.h:98
std::optional< COFF::AuxiliarySectionDefinition > SectionDefinition
Definition: COFFYAML.h:103
std::optional< COFF::AuxiliaryFunctionDefinition > FunctionDefinition
Definition: COFFYAML.h:99
COFF::symbol Header
Definition: COFFYAML.h:96
COFF::SymbolBaseType SimpleType
Definition: COFFYAML.h:97
std::optional< COFF::AuxiliaryCLRToken > CLRToken
Definition: COFFYAML.h:104
32-bit load config (IMAGE_LOAD_CONFIG_DIRECTORY32)
Definition: COFF.h:614
64-bit load config (IMAGE_LOAD_CONFIG_DIRECTORY64)
Definition: COFF.h:672