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
54LLVM_YAML_STRONG_TYPEDEF(uint8_t, COMDATType)
55LLVM_YAML_STRONG_TYPEDEF(uint32_t, WeakExternalCharacteristics)
56LLVM_YAML_STRONG_TYPEDEF(uint8_t, AuxSymbolType)
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 <>
183struct ScalarEnumerationTraits<COFF::RelocationTypesARM> {
184 static void enumeration(IO &IO, COFF::RelocationTypesARM &Value);
185};
186
187template <>
188struct ScalarEnumerationTraits<COFF::RelocationTypesARM64> {
189 static void enumeration(IO &IO, COFF::RelocationTypesARM64 &Value);
190};
191
192template <>
193struct ScalarEnumerationTraits<COFF::WindowsSubsystem> {
194 static void enumeration(IO &IO, COFF::WindowsSubsystem &Value);
195};
196
197template <>
198struct ScalarBitSetTraits<COFF::Characteristics> {
199 static void bitset(IO &IO, COFF::Characteristics &Value);
200};
201
202template <>
203struct ScalarBitSetTraits<COFF::SectionCharacteristics> {
204 static void bitset(IO &IO, COFF::SectionCharacteristics &Value);
205};
206
207template <>
208struct ScalarBitSetTraits<COFF::DLLCharacteristics> {
209 static void bitset(IO &IO, COFF::DLLCharacteristics &Value);
210};
211
212template <>
213struct MappingTraits<COFFYAML::Relocation> {
214 static void mapping(IO &IO, COFFYAML::Relocation &Rel);
215};
216
217template <>
218struct MappingTraits<COFFYAML::PEHeader> {
219 static void mapping(IO &IO, COFFYAML::PEHeader &PH);
220};
221
222template <>
223struct MappingTraits<COFF::DataDirectory> {
224 static void mapping(IO &IO, COFF::DataDirectory &DD);
225};
226
227template <>
228struct MappingTraits<COFF::header> {
229 static void mapping(IO &IO, COFF::header &H);
230};
231
232template <> struct MappingTraits<COFF::AuxiliaryFunctionDefinition> {
233 static void mapping(IO &IO, COFF::AuxiliaryFunctionDefinition &AFD);
234};
235
236template <> struct MappingTraits<COFF::AuxiliarybfAndefSymbol> {
237 static void mapping(IO &IO, COFF::AuxiliarybfAndefSymbol &AAS);
238};
239
240template <> struct MappingTraits<COFF::AuxiliaryWeakExternal> {
241 static void mapping(IO &IO, COFF::AuxiliaryWeakExternal &AWE);
242};
243
244template <> struct MappingTraits<COFF::AuxiliarySectionDefinition> {
245 static void mapping(IO &IO, COFF::AuxiliarySectionDefinition &ASD);
246};
247
248template <> struct MappingTraits<COFF::AuxiliaryCLRToken> {
249 static void mapping(IO &IO, COFF::AuxiliaryCLRToken &ACT);
250};
251
252template <> struct MappingTraits<object::coff_load_configuration32> {
253 static void mapping(IO &IO, object::coff_load_configuration32 &ACT);
254};
255
256template <> struct MappingTraits<object::coff_load_configuration64> {
257 static void mapping(IO &IO, object::coff_load_configuration64 &ACT);
258};
259
260template <> struct MappingTraits<object::coff_load_config_code_integrity> {
261 static void mapping(IO &IO, object::coff_load_config_code_integrity &ACT);
262};
263
264template <>
265struct MappingTraits<COFFYAML::Symbol> {
266 static void mapping(IO &IO, COFFYAML::Symbol &S);
267};
268
269template <> struct MappingTraits<COFFYAML::SectionDataEntry> {
270 static void mapping(IO &IO, COFFYAML::SectionDataEntry &Sec);
271};
272
273template <>
274struct MappingTraits<COFFYAML::Section> {
275 static void mapping(IO &IO, COFFYAML::Section &Sec);
276};
277
278template <>
279struct MappingTraits<COFFYAML::Object> {
280 static void mapping(IO &IO, COFFYAML::Object &Obj);
281};
282
283} // end namespace yaml
284} // end namespace llvm
285
286#endif // LLVM_OBJECTYAML_COFFYAML_H
COFFYAML::WeakExternalCharacteristics Characteristics
Definition: COFFYAML.cpp:331
#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:50
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:650
Characteristics operator|(Characteristics a, Characteristics b)
Definition: COFFYAML.h:31
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:628
SymbolStorageClass
Storage class tells where and what the symbol represents.
Definition: COFF.h:217
WindowsSubsystem
Definition: COFF.h:631
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