1//===- COFFYAML.cpp - COFF YAMLIO implementation --------------------------===//
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 defines classes for handling the YAML representation of COFF.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ObjectYAML/COFFYAML.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/Support/YAMLTraits.h"
16#include <cstdint>
17#include <cstring>
18
19#define ECase(X) IO.enumCase(Value, #X, COFF::X);
20
21namespace llvm {
22
23namespace COFFYAML {
24
25Section::Section() { memset(s: &Header, c: 0, n: sizeof(COFF::section)); }
26Symbol::Symbol() { memset(s: &Header, c: 0, n: sizeof(COFF::symbol)); }
27Object::Object() { memset(s: &Header, c: 0, n: sizeof(COFF::header)); }
28
29} // end namespace COFFYAML
30
31namespace yaml {
32
33void ScalarEnumerationTraits<COFFYAML::COMDATType>::enumeration(
34 IO &IO, COFFYAML::COMDATType &Value) {
35 IO.enumCase(Val&: Value, Str: "0", ConstVal: 0);
36 ECase(IMAGE_COMDAT_SELECT_NODUPLICATES);
37 ECase(IMAGE_COMDAT_SELECT_ANY);
38 ECase(IMAGE_COMDAT_SELECT_SAME_SIZE);
39 ECase(IMAGE_COMDAT_SELECT_EXACT_MATCH);
40 ECase(IMAGE_COMDAT_SELECT_ASSOCIATIVE);
41 ECase(IMAGE_COMDAT_SELECT_LARGEST);
42 ECase(IMAGE_COMDAT_SELECT_NEWEST);
43}
44
45void
46ScalarEnumerationTraits<COFFYAML::WeakExternalCharacteristics>::enumeration(
47 IO &IO, COFFYAML::WeakExternalCharacteristics &Value) {
48 IO.enumCase(Val&: Value, Str: "0", ConstVal: 0);
49 ECase(IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY);
50 ECase(IMAGE_WEAK_EXTERN_SEARCH_LIBRARY);
51 ECase(IMAGE_WEAK_EXTERN_SEARCH_ALIAS);
52 ECase(IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY);
53}
54
55void ScalarEnumerationTraits<COFFYAML::AuxSymbolType>::enumeration(
56 IO &IO, COFFYAML::AuxSymbolType &Value) {
57 ECase(IMAGE_AUX_SYMBOL_TYPE_TOKEN_DEF);
58}
59
60void ScalarEnumerationTraits<COFF::MachineTypes>::enumeration(
61 IO &IO, COFF::MachineTypes &Value) {
62 ECase(IMAGE_FILE_MACHINE_UNKNOWN);
63 ECase(IMAGE_FILE_MACHINE_AM33);
64 ECase(IMAGE_FILE_MACHINE_AMD64);
65 ECase(IMAGE_FILE_MACHINE_ARM);
66 ECase(IMAGE_FILE_MACHINE_ARMNT);
67 ECase(IMAGE_FILE_MACHINE_ARM64);
68 ECase(IMAGE_FILE_MACHINE_ARM64EC);
69 ECase(IMAGE_FILE_MACHINE_ARM64X);
70 ECase(IMAGE_FILE_MACHINE_EBC);
71 ECase(IMAGE_FILE_MACHINE_I386);
72 ECase(IMAGE_FILE_MACHINE_IA64);
73 ECase(IMAGE_FILE_MACHINE_M32R);
74 ECase(IMAGE_FILE_MACHINE_MIPS16);
75 ECase(IMAGE_FILE_MACHINE_MIPSFPU);
76 ECase(IMAGE_FILE_MACHINE_MIPSFPU16);
77 ECase(IMAGE_FILE_MACHINE_POWERPC);
78 ECase(IMAGE_FILE_MACHINE_POWERPCFP);
79 ECase(IMAGE_FILE_MACHINE_R4000);
80 ECase(IMAGE_FILE_MACHINE_RISCV32);
81 ECase(IMAGE_FILE_MACHINE_RISCV64);
82 ECase(IMAGE_FILE_MACHINE_RISCV128);
83 ECase(IMAGE_FILE_MACHINE_SH3);
84 ECase(IMAGE_FILE_MACHINE_SH3DSP);
85 ECase(IMAGE_FILE_MACHINE_SH4);
86 ECase(IMAGE_FILE_MACHINE_SH5);
87 ECase(IMAGE_FILE_MACHINE_THUMB);
88 ECase(IMAGE_FILE_MACHINE_WCEMIPSV2);
89}
90
91void ScalarEnumerationTraits<COFF::SymbolBaseType>::enumeration(
92 IO &IO, COFF::SymbolBaseType &Value) {
93 ECase(IMAGE_SYM_TYPE_NULL);
94 ECase(IMAGE_SYM_TYPE_VOID);
95 ECase(IMAGE_SYM_TYPE_CHAR);
96 ECase(IMAGE_SYM_TYPE_SHORT);
97 ECase(IMAGE_SYM_TYPE_INT);
98 ECase(IMAGE_SYM_TYPE_LONG);
99 ECase(IMAGE_SYM_TYPE_FLOAT);
100 ECase(IMAGE_SYM_TYPE_DOUBLE);
101 ECase(IMAGE_SYM_TYPE_STRUCT);
102 ECase(IMAGE_SYM_TYPE_UNION);
103 ECase(IMAGE_SYM_TYPE_ENUM);
104 ECase(IMAGE_SYM_TYPE_MOE);
105 ECase(IMAGE_SYM_TYPE_BYTE);
106 ECase(IMAGE_SYM_TYPE_WORD);
107 ECase(IMAGE_SYM_TYPE_UINT);
108 ECase(IMAGE_SYM_TYPE_DWORD);
109}
110
111void ScalarEnumerationTraits<COFF::SymbolStorageClass>::enumeration(
112 IO &IO, COFF::SymbolStorageClass &Value) {
113 ECase(IMAGE_SYM_CLASS_END_OF_FUNCTION);
114 ECase(IMAGE_SYM_CLASS_NULL);
115 ECase(IMAGE_SYM_CLASS_AUTOMATIC);
116 ECase(IMAGE_SYM_CLASS_EXTERNAL);
117 ECase(IMAGE_SYM_CLASS_STATIC);
118 ECase(IMAGE_SYM_CLASS_REGISTER);
119 ECase(IMAGE_SYM_CLASS_EXTERNAL_DEF);
120 ECase(IMAGE_SYM_CLASS_LABEL);
121 ECase(IMAGE_SYM_CLASS_UNDEFINED_LABEL);
122 ECase(IMAGE_SYM_CLASS_MEMBER_OF_STRUCT);
123 ECase(IMAGE_SYM_CLASS_ARGUMENT);
124 ECase(IMAGE_SYM_CLASS_STRUCT_TAG);
125 ECase(IMAGE_SYM_CLASS_MEMBER_OF_UNION);
126 ECase(IMAGE_SYM_CLASS_UNION_TAG);
127 ECase(IMAGE_SYM_CLASS_TYPE_DEFINITION);
128 ECase(IMAGE_SYM_CLASS_UNDEFINED_STATIC);
129 ECase(IMAGE_SYM_CLASS_ENUM_TAG);
130 ECase(IMAGE_SYM_CLASS_MEMBER_OF_ENUM);
131 ECase(IMAGE_SYM_CLASS_REGISTER_PARAM);
132 ECase(IMAGE_SYM_CLASS_BIT_FIELD);
133 ECase(IMAGE_SYM_CLASS_BLOCK);
134 ECase(IMAGE_SYM_CLASS_FUNCTION);
135 ECase(IMAGE_SYM_CLASS_END_OF_STRUCT);
136 ECase(IMAGE_SYM_CLASS_FILE);
137 ECase(IMAGE_SYM_CLASS_SECTION);
138 ECase(IMAGE_SYM_CLASS_WEAK_EXTERNAL);
139 ECase(IMAGE_SYM_CLASS_CLR_TOKEN);
140}
141
142void ScalarEnumerationTraits<COFF::SymbolComplexType>::enumeration(
143 IO &IO, COFF::SymbolComplexType &Value) {
144 ECase(IMAGE_SYM_DTYPE_NULL);
145 ECase(IMAGE_SYM_DTYPE_POINTER);
146 ECase(IMAGE_SYM_DTYPE_FUNCTION);
147 ECase(IMAGE_SYM_DTYPE_ARRAY);
148}
149
150void ScalarEnumerationTraits<COFF::RelocationTypeI386>::enumeration(
151 IO &IO, COFF::RelocationTypeI386 &Value) {
152 ECase(IMAGE_REL_I386_ABSOLUTE);
153 ECase(IMAGE_REL_I386_DIR16);
154 ECase(IMAGE_REL_I386_REL16);
155 ECase(IMAGE_REL_I386_DIR32);
156 ECase(IMAGE_REL_I386_DIR32NB);
157 ECase(IMAGE_REL_I386_SEG12);
158 ECase(IMAGE_REL_I386_SECTION);
159 ECase(IMAGE_REL_I386_SECREL);
160 ECase(IMAGE_REL_I386_TOKEN);
161 ECase(IMAGE_REL_I386_SECREL7);
162 ECase(IMAGE_REL_I386_REL32);
163}
164
165void ScalarEnumerationTraits<COFF::RelocationTypeAMD64>::enumeration(
166 IO &IO, COFF::RelocationTypeAMD64 &Value) {
167 ECase(IMAGE_REL_AMD64_ABSOLUTE);
168 ECase(IMAGE_REL_AMD64_ADDR64);
169 ECase(IMAGE_REL_AMD64_ADDR32);
170 ECase(IMAGE_REL_AMD64_ADDR32NB);
171 ECase(IMAGE_REL_AMD64_REL32);
172 ECase(IMAGE_REL_AMD64_REL32_1);
173 ECase(IMAGE_REL_AMD64_REL32_2);
174 ECase(IMAGE_REL_AMD64_REL32_3);
175 ECase(IMAGE_REL_AMD64_REL32_4);
176 ECase(IMAGE_REL_AMD64_REL32_5);
177 ECase(IMAGE_REL_AMD64_SECTION);
178 ECase(IMAGE_REL_AMD64_SECREL);
179 ECase(IMAGE_REL_AMD64_SECREL7);
180 ECase(IMAGE_REL_AMD64_TOKEN);
181 ECase(IMAGE_REL_AMD64_SREL32);
182 ECase(IMAGE_REL_AMD64_PAIR);
183 ECase(IMAGE_REL_AMD64_SSPAN32);
184}
185
186void ScalarEnumerationTraits<COFF::RelocationTypesARM>::enumeration(
187 IO &IO, COFF::RelocationTypesARM &Value) {
188 ECase(IMAGE_REL_ARM_ABSOLUTE);
189 ECase(IMAGE_REL_ARM_ADDR32);
190 ECase(IMAGE_REL_ARM_ADDR32NB);
191 ECase(IMAGE_REL_ARM_BRANCH24);
192 ECase(IMAGE_REL_ARM_BRANCH11);
193 ECase(IMAGE_REL_ARM_TOKEN);
194 ECase(IMAGE_REL_ARM_BLX24);
195 ECase(IMAGE_REL_ARM_BLX11);
196 ECase(IMAGE_REL_ARM_REL32);
197 ECase(IMAGE_REL_ARM_SECTION);
198 ECase(IMAGE_REL_ARM_SECREL);
199 ECase(IMAGE_REL_ARM_MOV32A);
200 ECase(IMAGE_REL_ARM_MOV32T);
201 ECase(IMAGE_REL_ARM_BRANCH20T);
202 ECase(IMAGE_REL_ARM_BRANCH24T);
203 ECase(IMAGE_REL_ARM_BLX23T);
204 ECase(IMAGE_REL_ARM_PAIR);
205}
206
207void ScalarEnumerationTraits<COFF::RelocationTypesARM64>::enumeration(
208 IO &IO, COFF::RelocationTypesARM64 &Value) {
209 ECase(IMAGE_REL_ARM64_ABSOLUTE);
210 ECase(IMAGE_REL_ARM64_ADDR32);
211 ECase(IMAGE_REL_ARM64_ADDR32NB);
212 ECase(IMAGE_REL_ARM64_BRANCH26);
213 ECase(IMAGE_REL_ARM64_PAGEBASE_REL21);
214 ECase(IMAGE_REL_ARM64_REL21);
215 ECase(IMAGE_REL_ARM64_PAGEOFFSET_12A);
216 ECase(IMAGE_REL_ARM64_PAGEOFFSET_12L);
217 ECase(IMAGE_REL_ARM64_SECREL);
218 ECase(IMAGE_REL_ARM64_SECREL_LOW12A);
219 ECase(IMAGE_REL_ARM64_SECREL_HIGH12A);
220 ECase(IMAGE_REL_ARM64_SECREL_LOW12L);
221 ECase(IMAGE_REL_ARM64_TOKEN);
222 ECase(IMAGE_REL_ARM64_SECTION);
223 ECase(IMAGE_REL_ARM64_ADDR64);
224 ECase(IMAGE_REL_ARM64_BRANCH19);
225 ECase(IMAGE_REL_ARM64_BRANCH14);
226 ECase(IMAGE_REL_ARM64_REL32);
227}
228
229void ScalarEnumerationTraits<COFF::WindowsSubsystem>::enumeration(
230 IO &IO, COFF::WindowsSubsystem &Value) {
231 ECase(IMAGE_SUBSYSTEM_UNKNOWN);
232 ECase(IMAGE_SUBSYSTEM_NATIVE);
233 ECase(IMAGE_SUBSYSTEM_WINDOWS_GUI);
234 ECase(IMAGE_SUBSYSTEM_WINDOWS_CUI);
235 ECase(IMAGE_SUBSYSTEM_OS2_CUI);
236 ECase(IMAGE_SUBSYSTEM_POSIX_CUI);
237 ECase(IMAGE_SUBSYSTEM_NATIVE_WINDOWS);
238 ECase(IMAGE_SUBSYSTEM_WINDOWS_CE_GUI);
239 ECase(IMAGE_SUBSYSTEM_EFI_APPLICATION);
240 ECase(IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER);
241 ECase(IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER);
242 ECase(IMAGE_SUBSYSTEM_EFI_ROM);
243 ECase(IMAGE_SUBSYSTEM_XBOX);
244 ECase(IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION);
245}
246#undef ECase
247
248#define BCase(X) IO.bitSetCase(Value, #X, COFF::X);
249void ScalarBitSetTraits<COFF::Characteristics>::bitset(
250 IO &IO, COFF::Characteristics &Value) {
251 BCase(IMAGE_FILE_RELOCS_STRIPPED);
252 BCase(IMAGE_FILE_EXECUTABLE_IMAGE);
253 BCase(IMAGE_FILE_LINE_NUMS_STRIPPED);
254 BCase(IMAGE_FILE_LOCAL_SYMS_STRIPPED);
255 BCase(IMAGE_FILE_AGGRESSIVE_WS_TRIM);
256 BCase(IMAGE_FILE_LARGE_ADDRESS_AWARE);
257 BCase(IMAGE_FILE_BYTES_REVERSED_LO);
258 BCase(IMAGE_FILE_32BIT_MACHINE);
259 BCase(IMAGE_FILE_DEBUG_STRIPPED);
260 BCase(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP);
261 BCase(IMAGE_FILE_NET_RUN_FROM_SWAP);
262 BCase(IMAGE_FILE_SYSTEM);
263 BCase(IMAGE_FILE_DLL);
264 BCase(IMAGE_FILE_UP_SYSTEM_ONLY);
265 BCase(IMAGE_FILE_BYTES_REVERSED_HI);
266}
267
268void ScalarBitSetTraits<COFF::SectionCharacteristics>::bitset(
269 IO &IO, COFF::SectionCharacteristics &Value) {
270 BCase(IMAGE_SCN_TYPE_NOLOAD);
271 BCase(IMAGE_SCN_TYPE_NO_PAD);
272 BCase(IMAGE_SCN_CNT_CODE);
273 BCase(IMAGE_SCN_CNT_INITIALIZED_DATA);
274 BCase(IMAGE_SCN_CNT_UNINITIALIZED_DATA);
275 BCase(IMAGE_SCN_LNK_OTHER);
276 BCase(IMAGE_SCN_LNK_INFO);
277 BCase(IMAGE_SCN_LNK_REMOVE);
278 BCase(IMAGE_SCN_LNK_COMDAT);
279 BCase(IMAGE_SCN_GPREL);
280 BCase(IMAGE_SCN_MEM_PURGEABLE);
281 BCase(IMAGE_SCN_MEM_16BIT);
282 BCase(IMAGE_SCN_MEM_LOCKED);
283 BCase(IMAGE_SCN_MEM_PRELOAD);
284 BCase(IMAGE_SCN_LNK_NRELOC_OVFL);
285 BCase(IMAGE_SCN_MEM_DISCARDABLE);
286 BCase(IMAGE_SCN_MEM_NOT_CACHED);
287 BCase(IMAGE_SCN_MEM_NOT_PAGED);
288 BCase(IMAGE_SCN_MEM_SHARED);
289 BCase(IMAGE_SCN_MEM_EXECUTE);
290 BCase(IMAGE_SCN_MEM_READ);
291 BCase(IMAGE_SCN_MEM_WRITE);
292}
293
294void ScalarBitSetTraits<COFF::DLLCharacteristics>::bitset(
295 IO &IO, COFF::DLLCharacteristics &Value) {
296 BCase(IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA);
297 BCase(IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE);
298 BCase(IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY);
299 BCase(IMAGE_DLL_CHARACTERISTICS_NX_COMPAT);
300 BCase(IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION);
301 BCase(IMAGE_DLL_CHARACTERISTICS_NO_SEH);
302 BCase(IMAGE_DLL_CHARACTERISTICS_NO_BIND);
303 BCase(IMAGE_DLL_CHARACTERISTICS_APPCONTAINER);
304 BCase(IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER);
305 BCase(IMAGE_DLL_CHARACTERISTICS_GUARD_CF);
306 BCase(IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE);
307}
308#undef BCase
309
310namespace {
311
312struct NSectionSelectionType {
313 NSectionSelectionType(IO &)
314 : SelectionType(COFFYAML::COMDATType(0)) {}
315 NSectionSelectionType(IO &, uint8_t C)
316 : SelectionType(COFFYAML::COMDATType(C)) {}
317
318 uint8_t denormalize(IO &) { return SelectionType; }
319
320 COFFYAML::COMDATType SelectionType;
321};
322
323struct NWeakExternalCharacteristics {
324 NWeakExternalCharacteristics(IO &)
325 : Characteristics(COFFYAML::WeakExternalCharacteristics(0)) {}
326 NWeakExternalCharacteristics(IO &, uint32_t C)
327 : Characteristics(COFFYAML::WeakExternalCharacteristics(C)) {}
328
329 uint32_t denormalize(IO &) { return Characteristics; }
330
331 COFFYAML::WeakExternalCharacteristics Characteristics;
332};
333
334struct NSectionCharacteristics {
335 NSectionCharacteristics(IO &)
336 : Characteristics(COFF::SectionCharacteristics(0)) {}
337 NSectionCharacteristics(IO &, uint32_t C)
338 : Characteristics(COFF::SectionCharacteristics(C)) {}
339
340 uint32_t denormalize(IO &) { return Characteristics; }
341
342 COFF::SectionCharacteristics Characteristics;
343};
344
345struct NAuxTokenType {
346 NAuxTokenType(IO &)
347 : AuxType(COFFYAML::AuxSymbolType(0)) {}
348 NAuxTokenType(IO &, uint8_t C)
349 : AuxType(COFFYAML::AuxSymbolType(C)) {}
350
351 uint32_t denormalize(IO &) { return AuxType; }
352
353 COFFYAML::AuxSymbolType AuxType;
354};
355
356struct NStorageClass {
357 NStorageClass(IO &) : StorageClass(COFF::SymbolStorageClass(0)) {}
358 NStorageClass(IO &, uint8_t S) : StorageClass(COFF::SymbolStorageClass(S)) {}
359
360 uint8_t denormalize(IO &) { return StorageClass; }
361
362 COFF::SymbolStorageClass StorageClass;
363};
364
365struct NMachine {
366 NMachine(IO &) : Machine(COFF::MachineTypes(0)) {}
367 NMachine(IO &, uint16_t M) : Machine(COFF::MachineTypes(M)) {}
368
369 uint16_t denormalize(IO &) { return Machine; }
370
371 COFF::MachineTypes Machine;
372};
373
374struct NHeaderCharacteristics {
375 NHeaderCharacteristics(IO &) : Characteristics(COFF::Characteristics(0)) {}
376 NHeaderCharacteristics(IO &, uint16_t C)
377 : Characteristics(COFF::Characteristics(C)) {}
378
379 uint16_t denormalize(IO &) { return Characteristics; }
380
381 COFF::Characteristics Characteristics;
382};
383
384template <typename RelocType>
385struct NType {
386 NType(IO &) : Type(RelocType(0)) {}
387 NType(IO &, uint16_t T) : Type(RelocType(T)) {}
388
389 uint16_t denormalize(IO &) { return Type; }
390
391 RelocType Type;
392};
393
394struct NWindowsSubsystem {
395 NWindowsSubsystem(IO &) : Subsystem(COFF::WindowsSubsystem(0)) {}
396 NWindowsSubsystem(IO &, uint16_t C) : Subsystem(COFF::WindowsSubsystem(C)) {}
397
398 uint16_t denormalize(IO &) { return Subsystem; }
399
400 COFF::WindowsSubsystem Subsystem;
401};
402
403struct NDLLCharacteristics {
404 NDLLCharacteristics(IO &) : Characteristics(COFF::DLLCharacteristics(0)) {}
405 NDLLCharacteristics(IO &, uint16_t C)
406 : Characteristics(COFF::DLLCharacteristics(C)) {}
407
408 uint16_t denormalize(IO &) { return Characteristics; }
409
410 COFF::DLLCharacteristics Characteristics;
411};
412
413} // end anonymous namespace
414
415void MappingTraits<COFFYAML::Relocation>::mapping(IO &IO,
416 COFFYAML::Relocation &Rel) {
417 IO.mapRequired(Key: "VirtualAddress", Val&: Rel.VirtualAddress);
418 IO.mapOptional(Key: "SymbolName", Val&: Rel.SymbolName, Default: StringRef());
419 IO.mapOptional(Key: "SymbolTableIndex", Val&: Rel.SymbolTableIndex);
420
421 COFF::header &H = *static_cast<COFF::header *>(IO.getContext());
422 if (H.Machine == COFF::IMAGE_FILE_MACHINE_I386) {
423 MappingNormalization<NType<COFF::RelocationTypeI386>, uint16_t> NT(
424 IO, Rel.Type);
425 IO.mapRequired(Key: "Type", Val&: NT->Type);
426 } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_AMD64) {
427 MappingNormalization<NType<COFF::RelocationTypeAMD64>, uint16_t> NT(
428 IO, Rel.Type);
429 IO.mapRequired(Key: "Type", Val&: NT->Type);
430 } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
431 MappingNormalization<NType<COFF::RelocationTypesARM>, uint16_t> NT(
432 IO, Rel.Type);
433 IO.mapRequired(Key: "Type", Val&: NT->Type);
434 } else if (COFF::isAnyArm64(Machine: H.Machine)) {
435 MappingNormalization<NType<COFF::RelocationTypesARM64>, uint16_t> NT(
436 IO, Rel.Type);
437 IO.mapRequired(Key: "Type", Val&: NT->Type);
438 } else {
439 IO.mapRequired(Key: "Type", Val&: Rel.Type);
440 }
441}
442
443void MappingTraits<COFF::DataDirectory>::mapping(IO &IO,
444 COFF::DataDirectory &DD) {
445 IO.mapRequired(Key: "RelativeVirtualAddress", Val&: DD.RelativeVirtualAddress);
446 IO.mapRequired(Key: "Size", Val&: DD.Size);
447}
448
449void MappingTraits<COFFYAML::PEHeader>::mapping(IO &IO,
450 COFFYAML::PEHeader &PH) {
451 MappingNormalization<NWindowsSubsystem, uint16_t> NWS(IO,
452 PH.Header.Subsystem);
453 MappingNormalization<NDLLCharacteristics, uint16_t> NDC(
454 IO, PH.Header.DLLCharacteristics);
455
456 IO.mapOptional(Key: "AddressOfEntryPoint", Val&: PH.Header.AddressOfEntryPoint);
457 IO.mapOptional(Key: "ImageBase", Val&: PH.Header.ImageBase);
458 IO.mapOptional(Key: "SectionAlignment", Val&: PH.Header.SectionAlignment, Default: 1);
459 IO.mapOptional(Key: "FileAlignment", Val&: PH.Header.FileAlignment, Default: 1);
460 IO.mapOptional(Key: "MajorOperatingSystemVersion",
461 Val&: PH.Header.MajorOperatingSystemVersion);
462 IO.mapOptional(Key: "MinorOperatingSystemVersion",
463 Val&: PH.Header.MinorOperatingSystemVersion);
464 IO.mapOptional(Key: "MajorImageVersion", Val&: PH.Header.MajorImageVersion);
465 IO.mapOptional(Key: "MinorImageVersion", Val&: PH.Header.MinorImageVersion);
466 IO.mapOptional(Key: "MajorSubsystemVersion", Val&: PH.Header.MajorSubsystemVersion);
467 IO.mapOptional(Key: "MinorSubsystemVersion", Val&: PH.Header.MinorSubsystemVersion);
468 IO.mapOptional(Key: "Subsystem", Val&: NWS->Subsystem);
469 IO.mapOptional(Key: "DLLCharacteristics", Val&: NDC->Characteristics);
470 IO.mapOptional(Key: "SizeOfStackReserve", Val&: PH.Header.SizeOfStackReserve);
471 IO.mapOptional(Key: "SizeOfStackCommit", Val&: PH.Header.SizeOfStackCommit);
472 IO.mapOptional(Key: "SizeOfHeapReserve", Val&: PH.Header.SizeOfHeapReserve);
473 IO.mapOptional(Key: "SizeOfHeapCommit", Val&: PH.Header.SizeOfHeapCommit);
474
475 IO.mapOptional(Key: "NumberOfRvaAndSize", Val&: PH.Header.NumberOfRvaAndSize,
476 Default: COFF::NUM_DATA_DIRECTORIES + 1);
477 IO.mapOptional(Key: "ExportTable", Val&: PH.DataDirectories[COFF::EXPORT_TABLE]);
478 IO.mapOptional(Key: "ImportTable", Val&: PH.DataDirectories[COFF::IMPORT_TABLE]);
479 IO.mapOptional(Key: "ResourceTable", Val&: PH.DataDirectories[COFF::RESOURCE_TABLE]);
480 IO.mapOptional(Key: "ExceptionTable", Val&: PH.DataDirectories[COFF::EXCEPTION_TABLE]);
481 IO.mapOptional(Key: "CertificateTable", Val&: PH.DataDirectories[COFF::CERTIFICATE_TABLE]);
482 IO.mapOptional(Key: "BaseRelocationTable",
483 Val&: PH.DataDirectories[COFF::BASE_RELOCATION_TABLE]);
484 IO.mapOptional(Key: "Debug", Val&: PH.DataDirectories[COFF::DEBUG_DIRECTORY]);
485 IO.mapOptional(Key: "Architecture", Val&: PH.DataDirectories[COFF::ARCHITECTURE]);
486 IO.mapOptional(Key: "GlobalPtr", Val&: PH.DataDirectories[COFF::GLOBAL_PTR]);
487 IO.mapOptional(Key: "TlsTable", Val&: PH.DataDirectories[COFF::TLS_TABLE]);
488 IO.mapOptional(Key: "LoadConfigTable",
489 Val&: PH.DataDirectories[COFF::LOAD_CONFIG_TABLE]);
490 IO.mapOptional(Key: "BoundImport", Val&: PH.DataDirectories[COFF::BOUND_IMPORT]);
491 IO.mapOptional(Key: "IAT", Val&: PH.DataDirectories[COFF::IAT]);
492 IO.mapOptional(Key: "DelayImportDescriptor",
493 Val&: PH.DataDirectories[COFF::DELAY_IMPORT_DESCRIPTOR]);
494 IO.mapOptional(Key: "ClrRuntimeHeader",
495 Val&: PH.DataDirectories[COFF::CLR_RUNTIME_HEADER]);
496}
497
498void MappingTraits<COFF::header>::mapping(IO &IO, COFF::header &H) {
499 MappingNormalization<NMachine, uint16_t> NM(IO, H.Machine);
500 MappingNormalization<NHeaderCharacteristics, uint16_t> NC(IO,
501 H.Characteristics);
502
503 IO.mapRequired(Key: "Machine", Val&: NM->Machine);
504 IO.mapOptional(Key: "Characteristics", Val&: NC->Characteristics);
505 IO.setContext(static_cast<void *>(&H));
506}
507
508void MappingTraits<COFF::AuxiliaryFunctionDefinition>::mapping(
509 IO &IO, COFF::AuxiliaryFunctionDefinition &AFD) {
510 IO.mapRequired(Key: "TagIndex", Val&: AFD.TagIndex);
511 IO.mapRequired(Key: "TotalSize", Val&: AFD.TotalSize);
512 IO.mapRequired(Key: "PointerToLinenumber", Val&: AFD.PointerToLinenumber);
513 IO.mapRequired(Key: "PointerToNextFunction", Val&: AFD.PointerToNextFunction);
514}
515
516void MappingTraits<COFF::AuxiliarybfAndefSymbol>::mapping(
517 IO &IO, COFF::AuxiliarybfAndefSymbol &AAS) {
518 IO.mapRequired(Key: "Linenumber", Val&: AAS.Linenumber);
519 IO.mapRequired(Key: "PointerToNextFunction", Val&: AAS.PointerToNextFunction);
520}
521
522void MappingTraits<COFF::AuxiliaryWeakExternal>::mapping(
523 IO &IO, COFF::AuxiliaryWeakExternal &AWE) {
524 MappingNormalization<NWeakExternalCharacteristics, uint32_t> NWEC(
525 IO, AWE.Characteristics);
526 IO.mapRequired(Key: "TagIndex", Val&: AWE.TagIndex);
527 IO.mapRequired(Key: "Characteristics", Val&: NWEC->Characteristics);
528}
529
530void MappingTraits<COFF::AuxiliarySectionDefinition>::mapping(
531 IO &IO, COFF::AuxiliarySectionDefinition &ASD) {
532 MappingNormalization<NSectionSelectionType, uint8_t> NSST(
533 IO, ASD.Selection);
534
535 IO.mapRequired(Key: "Length", Val&: ASD.Length);
536 IO.mapRequired(Key: "NumberOfRelocations", Val&: ASD.NumberOfRelocations);
537 IO.mapRequired(Key: "NumberOfLinenumbers", Val&: ASD.NumberOfLinenumbers);
538 IO.mapRequired(Key: "CheckSum", Val&: ASD.CheckSum);
539 IO.mapRequired(Key: "Number", Val&: ASD.Number);
540 IO.mapOptional(Key: "Selection", Val&: NSST->SelectionType, Default: COFFYAML::COMDATType(0));
541}
542
543void MappingTraits<COFF::AuxiliaryCLRToken>::mapping(
544 IO &IO, COFF::AuxiliaryCLRToken &ACT) {
545 MappingNormalization<NAuxTokenType, uint8_t> NATT(IO, ACT.AuxType);
546 IO.mapRequired(Key: "AuxType", Val&: NATT->AuxType);
547 IO.mapRequired(Key: "SymbolTableIndex", Val&: ACT.SymbolTableIndex);
548}
549
550void MappingTraits<object::coff_load_config_code_integrity>::mapping(
551 IO &IO, object::coff_load_config_code_integrity &S) {
552 IO.mapOptional(Key: "Flags", Val&: S.Flags);
553 IO.mapOptional(Key: "Catalog", Val&: S.Catalog);
554 IO.mapOptional(Key: "CatalogOffset", Val&: S.CatalogOffset);
555}
556
557template <typename T, typename M>
558void mapLoadConfigMember(IO &IO, T &LoadConfig, const char *Name, M &Member) {
559 // Map only members that match a specified size.
560 ptrdiff_t dist =
561 reinterpret_cast<char *>(&Member) - reinterpret_cast<char *>(&LoadConfig);
562 if (dist < (ptrdiff_t)LoadConfig.Size)
563 IO.mapOptional(Name, Member);
564}
565
566template <typename T> void mapLoadConfig(IO &IO, T &LoadConfig) {
567 IO.mapOptional("Size", LoadConfig.Size,
568 support::ulittle32_t(sizeof(LoadConfig)));
569 // The size must be large enough to fit at least the size member itself.
570 if (LoadConfig.Size < sizeof(LoadConfig.Size)) {
571 IO.setError("Size must be at least " + Twine(sizeof(LoadConfig.Size)));
572 return;
573 }
574
575#define MCase(X) mapLoadConfigMember(IO, LoadConfig, #X, LoadConfig.X)
576 MCase(TimeDateStamp);
577 MCase(MajorVersion);
578 MCase(MinorVersion);
579 MCase(GlobalFlagsClear);
580 MCase(GlobalFlagsSet);
581 MCase(CriticalSectionDefaultTimeout);
582 MCase(DeCommitFreeBlockThreshold);
583 MCase(DeCommitTotalFreeThreshold);
584 MCase(LockPrefixTable);
585 MCase(MaximumAllocationSize);
586 MCase(VirtualMemoryThreshold);
587 MCase(ProcessAffinityMask);
588 MCase(ProcessHeapFlags);
589 MCase(CSDVersion);
590 MCase(DependentLoadFlags);
591 MCase(EditList);
592 MCase(SecurityCookie);
593 MCase(SEHandlerTable);
594 MCase(SEHandlerCount);
595 MCase(GuardCFCheckFunction);
596 MCase(GuardCFCheckDispatch);
597 MCase(GuardCFFunctionTable);
598 MCase(GuardCFFunctionCount);
599 MCase(GuardFlags);
600 MCase(CodeIntegrity);
601 MCase(GuardAddressTakenIatEntryTable);
602 MCase(GuardAddressTakenIatEntryCount);
603 MCase(GuardLongJumpTargetTable);
604 MCase(GuardLongJumpTargetCount);
605 MCase(DynamicValueRelocTable);
606 MCase(CHPEMetadataPointer);
607 MCase(GuardRFFailureRoutine);
608 MCase(GuardRFFailureRoutineFunctionPointer);
609 MCase(DynamicValueRelocTableOffset);
610 MCase(DynamicValueRelocTableSection);
611 MCase(GuardRFVerifyStackPointerFunctionPointer);
612 MCase(HotPatchTableOffset);
613 MCase(EnclaveConfigurationPointer);
614 MCase(VolatileMetadataPointer);
615 MCase(GuardEHContinuationTable);
616 MCase(GuardEHContinuationCount);
617 MCase(GuardXFGCheckFunctionPointer);
618 MCase(GuardXFGDispatchFunctionPointer);
619 MCase(GuardXFGTableDispatchFunctionPointer);
620 MCase(CastGuardOsDeterminedFailureMode);
621#undef MCase
622}
623
624void MappingTraits<object::coff_load_configuration32>::mapping(
625 IO &IO, object::coff_load_configuration32 &S) {
626 mapLoadConfig(IO, LoadConfig&: S);
627}
628
629void MappingTraits<object::coff_load_configuration64>::mapping(
630 IO &IO, object::coff_load_configuration64 &S) {
631 mapLoadConfig(IO, LoadConfig&: S);
632}
633
634void MappingTraits<COFFYAML::SectionDataEntry>::mapping(
635 IO &IO, COFFYAML::SectionDataEntry &E) {
636 IO.mapOptional(Key: "UInt32", Val&: E.UInt32);
637 IO.mapOptional(Key: "Binary", Val&: E.Binary);
638
639 COFF::header &H = *static_cast<COFF::header *>(IO.getContext());
640 if (COFF::is64Bit(Machine: H.Machine))
641 IO.mapOptional(Key: "LoadConfig", Val&: E.LoadConfig64);
642 else
643 IO.mapOptional(Key: "LoadConfig", Val&: E.LoadConfig32);
644}
645
646void MappingTraits<COFFYAML::Symbol>::mapping(IO &IO, COFFYAML::Symbol &S) {
647 MappingNormalization<NStorageClass, uint8_t> NS(IO, S.Header.StorageClass);
648
649 IO.mapRequired(Key: "Name", Val&: S.Name);
650 IO.mapRequired(Key: "Value", Val&: S.Header.Value);
651 IO.mapRequired(Key: "SectionNumber", Val&: S.Header.SectionNumber);
652 IO.mapRequired(Key: "SimpleType", Val&: S.SimpleType);
653 IO.mapRequired(Key: "ComplexType", Val&: S.ComplexType);
654 IO.mapRequired(Key: "StorageClass", Val&: NS->StorageClass);
655 IO.mapOptional(Key: "FunctionDefinition", Val&: S.FunctionDefinition);
656 IO.mapOptional(Key: "bfAndefSymbol", Val&: S.bfAndefSymbol);
657 IO.mapOptional(Key: "WeakExternal", Val&: S.WeakExternal);
658 IO.mapOptional(Key: "File", Val&: S.File, Default: StringRef());
659 IO.mapOptional(Key: "SectionDefinition", Val&: S.SectionDefinition);
660 IO.mapOptional(Key: "CLRToken", Val&: S.CLRToken);
661}
662
663void MappingTraits<COFFYAML::Section>::mapping(IO &IO, COFFYAML::Section &Sec) {
664 MappingNormalization<NSectionCharacteristics, uint32_t> NC(
665 IO, Sec.Header.Characteristics);
666 IO.mapRequired(Key: "Name", Val&: Sec.Name);
667 IO.mapRequired(Key: "Characteristics", Val&: NC->Characteristics);
668 IO.mapOptional(Key: "VirtualAddress", Val&: Sec.Header.VirtualAddress, Default: 0U);
669 IO.mapOptional(Key: "VirtualSize", Val&: Sec.Header.VirtualSize, Default: 0U);
670 IO.mapOptional(Key: "Alignment", Val&: Sec.Alignment, Default: 0U);
671
672 // If this is a .debug$S .debug$T .debug$P, or .debug$H section parse the
673 // semantic representation of the symbols/types. If it is any other kind
674 // of section, just deal in raw bytes.
675 IO.mapOptional(Key: "SectionData", Val&: Sec.SectionData);
676 if (Sec.Name == ".debug$S")
677 IO.mapOptional(Key: "Subsections", Val&: Sec.DebugS);
678 else if (Sec.Name == ".debug$T")
679 IO.mapOptional(Key: "Types", Val&: Sec.DebugT);
680 else if (Sec.Name == ".debug$P")
681 IO.mapOptional(Key: "PrecompTypes", Val&: Sec.DebugP);
682 else if (Sec.Name == ".debug$H")
683 IO.mapOptional(Key: "GlobalHashes", Val&: Sec.DebugH);
684
685 IO.mapOptional(Key: "StructuredData", Val&: Sec.StructuredData);
686
687 if (!Sec.StructuredData.empty() && Sec.SectionData.binary_size()) {
688 IO.setError("StructuredData and SectionData can't be used together");
689 return;
690 }
691
692 IO.mapOptional(Key: "SizeOfRawData", Val&: Sec.Header.SizeOfRawData, Default: 0U);
693
694 if (!Sec.StructuredData.empty() && Sec.Header.SizeOfRawData) {
695 IO.setError("StructuredData and SizeOfRawData can't be used together");
696 return;
697 }
698
699 IO.mapOptional(Key: "Relocations", Val&: Sec.Relocations);
700}
701
702void MappingTraits<COFFYAML::Object>::mapping(IO &IO, COFFYAML::Object &Obj) {
703 IO.mapTag(Tag: "!COFF", Default: true);
704 IO.mapOptional(Key: "OptionalHeader", Val&: Obj.OptionalHeader);
705 IO.mapRequired(Key: "header", Val&: Obj.Header);
706 IO.mapRequired(Key: "sections", Val&: Obj.Sections);
707 IO.mapRequired(Key: "symbols", Val&: Obj.Symbols);
708}
709
710} // end namespace yaml
711
712} // end namespace llvm
713