xref: /freebsd/contrib/llvm-project/llvm/lib/ObjectYAML/COFFYAML.cpp (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
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 
21 namespace llvm {
22 
23 namespace COFFYAML {
24 
25 Section::Section() { memset(&Header, 0, sizeof(COFF::section)); }
26 Symbol::Symbol() { memset(&Header, 0, sizeof(COFF::symbol)); }
27 Object::Object() { memset(&Header, 0, sizeof(COFF::header)); }
28 
29 } // end namespace COFFYAML
30 
31 namespace yaml {
32 
33 void ScalarEnumerationTraits<COFFYAML::COMDATType>::enumeration(
34     IO &IO, COFFYAML::COMDATType &Value) {
35   IO.enumCase(Value, "0", 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 
45 void
46 ScalarEnumerationTraits<COFFYAML::WeakExternalCharacteristics>::enumeration(
47     IO &IO, COFFYAML::WeakExternalCharacteristics &Value) {
48   IO.enumCase(Value, "0", 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 
55 void ScalarEnumerationTraits<COFFYAML::AuxSymbolType>::enumeration(
56     IO &IO, COFFYAML::AuxSymbolType &Value) {
57   ECase(IMAGE_AUX_SYMBOL_TYPE_TOKEN_DEF);
58 }
59 
60 void 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_EBC);
69   ECase(IMAGE_FILE_MACHINE_I386);
70   ECase(IMAGE_FILE_MACHINE_IA64);
71   ECase(IMAGE_FILE_MACHINE_M32R);
72   ECase(IMAGE_FILE_MACHINE_MIPS16);
73   ECase(IMAGE_FILE_MACHINE_MIPSFPU);
74   ECase(IMAGE_FILE_MACHINE_MIPSFPU16);
75   ECase(IMAGE_FILE_MACHINE_POWERPC);
76   ECase(IMAGE_FILE_MACHINE_POWERPCFP);
77   ECase(IMAGE_FILE_MACHINE_R4000);
78   ECase(IMAGE_FILE_MACHINE_SH3);
79   ECase(IMAGE_FILE_MACHINE_SH3DSP);
80   ECase(IMAGE_FILE_MACHINE_SH4);
81   ECase(IMAGE_FILE_MACHINE_SH5);
82   ECase(IMAGE_FILE_MACHINE_THUMB);
83   ECase(IMAGE_FILE_MACHINE_WCEMIPSV2);
84 }
85 
86 void ScalarEnumerationTraits<COFF::SymbolBaseType>::enumeration(
87     IO &IO, COFF::SymbolBaseType &Value) {
88   ECase(IMAGE_SYM_TYPE_NULL);
89   ECase(IMAGE_SYM_TYPE_VOID);
90   ECase(IMAGE_SYM_TYPE_CHAR);
91   ECase(IMAGE_SYM_TYPE_SHORT);
92   ECase(IMAGE_SYM_TYPE_INT);
93   ECase(IMAGE_SYM_TYPE_LONG);
94   ECase(IMAGE_SYM_TYPE_FLOAT);
95   ECase(IMAGE_SYM_TYPE_DOUBLE);
96   ECase(IMAGE_SYM_TYPE_STRUCT);
97   ECase(IMAGE_SYM_TYPE_UNION);
98   ECase(IMAGE_SYM_TYPE_ENUM);
99   ECase(IMAGE_SYM_TYPE_MOE);
100   ECase(IMAGE_SYM_TYPE_BYTE);
101   ECase(IMAGE_SYM_TYPE_WORD);
102   ECase(IMAGE_SYM_TYPE_UINT);
103   ECase(IMAGE_SYM_TYPE_DWORD);
104 }
105 
106 void ScalarEnumerationTraits<COFF::SymbolStorageClass>::enumeration(
107     IO &IO, COFF::SymbolStorageClass &Value) {
108   ECase(IMAGE_SYM_CLASS_END_OF_FUNCTION);
109   ECase(IMAGE_SYM_CLASS_NULL);
110   ECase(IMAGE_SYM_CLASS_AUTOMATIC);
111   ECase(IMAGE_SYM_CLASS_EXTERNAL);
112   ECase(IMAGE_SYM_CLASS_STATIC);
113   ECase(IMAGE_SYM_CLASS_REGISTER);
114   ECase(IMAGE_SYM_CLASS_EXTERNAL_DEF);
115   ECase(IMAGE_SYM_CLASS_LABEL);
116   ECase(IMAGE_SYM_CLASS_UNDEFINED_LABEL);
117   ECase(IMAGE_SYM_CLASS_MEMBER_OF_STRUCT);
118   ECase(IMAGE_SYM_CLASS_ARGUMENT);
119   ECase(IMAGE_SYM_CLASS_STRUCT_TAG);
120   ECase(IMAGE_SYM_CLASS_MEMBER_OF_UNION);
121   ECase(IMAGE_SYM_CLASS_UNION_TAG);
122   ECase(IMAGE_SYM_CLASS_TYPE_DEFINITION);
123   ECase(IMAGE_SYM_CLASS_UNDEFINED_STATIC);
124   ECase(IMAGE_SYM_CLASS_ENUM_TAG);
125   ECase(IMAGE_SYM_CLASS_MEMBER_OF_ENUM);
126   ECase(IMAGE_SYM_CLASS_REGISTER_PARAM);
127   ECase(IMAGE_SYM_CLASS_BIT_FIELD);
128   ECase(IMAGE_SYM_CLASS_BLOCK);
129   ECase(IMAGE_SYM_CLASS_FUNCTION);
130   ECase(IMAGE_SYM_CLASS_END_OF_STRUCT);
131   ECase(IMAGE_SYM_CLASS_FILE);
132   ECase(IMAGE_SYM_CLASS_SECTION);
133   ECase(IMAGE_SYM_CLASS_WEAK_EXTERNAL);
134   ECase(IMAGE_SYM_CLASS_CLR_TOKEN);
135 }
136 
137 void ScalarEnumerationTraits<COFF::SymbolComplexType>::enumeration(
138     IO &IO, COFF::SymbolComplexType &Value) {
139   ECase(IMAGE_SYM_DTYPE_NULL);
140   ECase(IMAGE_SYM_DTYPE_POINTER);
141   ECase(IMAGE_SYM_DTYPE_FUNCTION);
142   ECase(IMAGE_SYM_DTYPE_ARRAY);
143 }
144 
145 void ScalarEnumerationTraits<COFF::RelocationTypeI386>::enumeration(
146     IO &IO, COFF::RelocationTypeI386 &Value) {
147   ECase(IMAGE_REL_I386_ABSOLUTE);
148   ECase(IMAGE_REL_I386_DIR16);
149   ECase(IMAGE_REL_I386_REL16);
150   ECase(IMAGE_REL_I386_DIR32);
151   ECase(IMAGE_REL_I386_DIR32NB);
152   ECase(IMAGE_REL_I386_SEG12);
153   ECase(IMAGE_REL_I386_SECTION);
154   ECase(IMAGE_REL_I386_SECREL);
155   ECase(IMAGE_REL_I386_TOKEN);
156   ECase(IMAGE_REL_I386_SECREL7);
157   ECase(IMAGE_REL_I386_REL32);
158 }
159 
160 void ScalarEnumerationTraits<COFF::RelocationTypeAMD64>::enumeration(
161     IO &IO, COFF::RelocationTypeAMD64 &Value) {
162   ECase(IMAGE_REL_AMD64_ABSOLUTE);
163   ECase(IMAGE_REL_AMD64_ADDR64);
164   ECase(IMAGE_REL_AMD64_ADDR32);
165   ECase(IMAGE_REL_AMD64_ADDR32NB);
166   ECase(IMAGE_REL_AMD64_REL32);
167   ECase(IMAGE_REL_AMD64_REL32_1);
168   ECase(IMAGE_REL_AMD64_REL32_2);
169   ECase(IMAGE_REL_AMD64_REL32_3);
170   ECase(IMAGE_REL_AMD64_REL32_4);
171   ECase(IMAGE_REL_AMD64_REL32_5);
172   ECase(IMAGE_REL_AMD64_SECTION);
173   ECase(IMAGE_REL_AMD64_SECREL);
174   ECase(IMAGE_REL_AMD64_SECREL7);
175   ECase(IMAGE_REL_AMD64_TOKEN);
176   ECase(IMAGE_REL_AMD64_SREL32);
177   ECase(IMAGE_REL_AMD64_PAIR);
178   ECase(IMAGE_REL_AMD64_SSPAN32);
179 }
180 
181 void ScalarEnumerationTraits<COFF::RelocationTypesARM>::enumeration(
182     IO &IO, COFF::RelocationTypesARM &Value) {
183   ECase(IMAGE_REL_ARM_ABSOLUTE);
184   ECase(IMAGE_REL_ARM_ADDR32);
185   ECase(IMAGE_REL_ARM_ADDR32NB);
186   ECase(IMAGE_REL_ARM_BRANCH24);
187   ECase(IMAGE_REL_ARM_BRANCH11);
188   ECase(IMAGE_REL_ARM_TOKEN);
189   ECase(IMAGE_REL_ARM_BLX24);
190   ECase(IMAGE_REL_ARM_BLX11);
191   ECase(IMAGE_REL_ARM_REL32);
192   ECase(IMAGE_REL_ARM_SECTION);
193   ECase(IMAGE_REL_ARM_SECREL);
194   ECase(IMAGE_REL_ARM_MOV32A);
195   ECase(IMAGE_REL_ARM_MOV32T);
196   ECase(IMAGE_REL_ARM_BRANCH20T);
197   ECase(IMAGE_REL_ARM_BRANCH24T);
198   ECase(IMAGE_REL_ARM_BLX23T);
199   ECase(IMAGE_REL_ARM_PAIR);
200 }
201 
202 void ScalarEnumerationTraits<COFF::RelocationTypesARM64>::enumeration(
203     IO &IO, COFF::RelocationTypesARM64 &Value) {
204   ECase(IMAGE_REL_ARM64_ABSOLUTE);
205   ECase(IMAGE_REL_ARM64_ADDR32);
206   ECase(IMAGE_REL_ARM64_ADDR32NB);
207   ECase(IMAGE_REL_ARM64_BRANCH26);
208   ECase(IMAGE_REL_ARM64_PAGEBASE_REL21);
209   ECase(IMAGE_REL_ARM64_REL21);
210   ECase(IMAGE_REL_ARM64_PAGEOFFSET_12A);
211   ECase(IMAGE_REL_ARM64_PAGEOFFSET_12L);
212   ECase(IMAGE_REL_ARM64_SECREL);
213   ECase(IMAGE_REL_ARM64_SECREL_LOW12A);
214   ECase(IMAGE_REL_ARM64_SECREL_HIGH12A);
215   ECase(IMAGE_REL_ARM64_SECREL_LOW12L);
216   ECase(IMAGE_REL_ARM64_TOKEN);
217   ECase(IMAGE_REL_ARM64_SECTION);
218   ECase(IMAGE_REL_ARM64_ADDR64);
219   ECase(IMAGE_REL_ARM64_BRANCH19);
220   ECase(IMAGE_REL_ARM64_BRANCH14);
221   ECase(IMAGE_REL_ARM64_REL32);
222 }
223 
224 void ScalarEnumerationTraits<COFF::WindowsSubsystem>::enumeration(
225     IO &IO, COFF::WindowsSubsystem &Value) {
226   ECase(IMAGE_SUBSYSTEM_UNKNOWN);
227   ECase(IMAGE_SUBSYSTEM_NATIVE);
228   ECase(IMAGE_SUBSYSTEM_WINDOWS_GUI);
229   ECase(IMAGE_SUBSYSTEM_WINDOWS_CUI);
230   ECase(IMAGE_SUBSYSTEM_OS2_CUI);
231   ECase(IMAGE_SUBSYSTEM_POSIX_CUI);
232   ECase(IMAGE_SUBSYSTEM_NATIVE_WINDOWS);
233   ECase(IMAGE_SUBSYSTEM_WINDOWS_CE_GUI);
234   ECase(IMAGE_SUBSYSTEM_EFI_APPLICATION);
235   ECase(IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER);
236   ECase(IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER);
237   ECase(IMAGE_SUBSYSTEM_EFI_ROM);
238   ECase(IMAGE_SUBSYSTEM_XBOX);
239   ECase(IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION);
240 }
241 #undef ECase
242 
243 #define BCase(X) IO.bitSetCase(Value, #X, COFF::X);
244 void ScalarBitSetTraits<COFF::Characteristics>::bitset(
245     IO &IO, COFF::Characteristics &Value) {
246   BCase(IMAGE_FILE_RELOCS_STRIPPED);
247   BCase(IMAGE_FILE_EXECUTABLE_IMAGE);
248   BCase(IMAGE_FILE_LINE_NUMS_STRIPPED);
249   BCase(IMAGE_FILE_LOCAL_SYMS_STRIPPED);
250   BCase(IMAGE_FILE_AGGRESSIVE_WS_TRIM);
251   BCase(IMAGE_FILE_LARGE_ADDRESS_AWARE);
252   BCase(IMAGE_FILE_BYTES_REVERSED_LO);
253   BCase(IMAGE_FILE_32BIT_MACHINE);
254   BCase(IMAGE_FILE_DEBUG_STRIPPED);
255   BCase(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP);
256   BCase(IMAGE_FILE_NET_RUN_FROM_SWAP);
257   BCase(IMAGE_FILE_SYSTEM);
258   BCase(IMAGE_FILE_DLL);
259   BCase(IMAGE_FILE_UP_SYSTEM_ONLY);
260   BCase(IMAGE_FILE_BYTES_REVERSED_HI);
261 }
262 
263 void ScalarBitSetTraits<COFF::SectionCharacteristics>::bitset(
264     IO &IO, COFF::SectionCharacteristics &Value) {
265   BCase(IMAGE_SCN_TYPE_NOLOAD);
266   BCase(IMAGE_SCN_TYPE_NO_PAD);
267   BCase(IMAGE_SCN_CNT_CODE);
268   BCase(IMAGE_SCN_CNT_INITIALIZED_DATA);
269   BCase(IMAGE_SCN_CNT_UNINITIALIZED_DATA);
270   BCase(IMAGE_SCN_LNK_OTHER);
271   BCase(IMAGE_SCN_LNK_INFO);
272   BCase(IMAGE_SCN_LNK_REMOVE);
273   BCase(IMAGE_SCN_LNK_COMDAT);
274   BCase(IMAGE_SCN_GPREL);
275   BCase(IMAGE_SCN_MEM_PURGEABLE);
276   BCase(IMAGE_SCN_MEM_16BIT);
277   BCase(IMAGE_SCN_MEM_LOCKED);
278   BCase(IMAGE_SCN_MEM_PRELOAD);
279   BCase(IMAGE_SCN_LNK_NRELOC_OVFL);
280   BCase(IMAGE_SCN_MEM_DISCARDABLE);
281   BCase(IMAGE_SCN_MEM_NOT_CACHED);
282   BCase(IMAGE_SCN_MEM_NOT_PAGED);
283   BCase(IMAGE_SCN_MEM_SHARED);
284   BCase(IMAGE_SCN_MEM_EXECUTE);
285   BCase(IMAGE_SCN_MEM_READ);
286   BCase(IMAGE_SCN_MEM_WRITE);
287 }
288 
289 void ScalarBitSetTraits<COFF::DLLCharacteristics>::bitset(
290     IO &IO, COFF::DLLCharacteristics &Value) {
291   BCase(IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA);
292   BCase(IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE);
293   BCase(IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY);
294   BCase(IMAGE_DLL_CHARACTERISTICS_NX_COMPAT);
295   BCase(IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION);
296   BCase(IMAGE_DLL_CHARACTERISTICS_NO_SEH);
297   BCase(IMAGE_DLL_CHARACTERISTICS_NO_BIND);
298   BCase(IMAGE_DLL_CHARACTERISTICS_APPCONTAINER);
299   BCase(IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER);
300   BCase(IMAGE_DLL_CHARACTERISTICS_GUARD_CF);
301   BCase(IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE);
302 }
303 #undef BCase
304 
305 namespace {
306 
307 struct NSectionSelectionType {
308   NSectionSelectionType(IO &)
309       : SelectionType(COFFYAML::COMDATType(0)) {}
310   NSectionSelectionType(IO &, uint8_t C)
311       : SelectionType(COFFYAML::COMDATType(C)) {}
312 
313   uint8_t denormalize(IO &) { return SelectionType; }
314 
315   COFFYAML::COMDATType SelectionType;
316 };
317 
318 struct NWeakExternalCharacteristics {
319   NWeakExternalCharacteristics(IO &)
320       : Characteristics(COFFYAML::WeakExternalCharacteristics(0)) {}
321   NWeakExternalCharacteristics(IO &, uint32_t C)
322       : Characteristics(COFFYAML::WeakExternalCharacteristics(C)) {}
323 
324   uint32_t denormalize(IO &) { return Characteristics; }
325 
326   COFFYAML::WeakExternalCharacteristics Characteristics;
327 };
328 
329 struct NSectionCharacteristics {
330   NSectionCharacteristics(IO &)
331       : Characteristics(COFF::SectionCharacteristics(0)) {}
332   NSectionCharacteristics(IO &, uint32_t C)
333       : Characteristics(COFF::SectionCharacteristics(C)) {}
334 
335   uint32_t denormalize(IO &) { return Characteristics; }
336 
337   COFF::SectionCharacteristics Characteristics;
338 };
339 
340 struct NAuxTokenType {
341   NAuxTokenType(IO &)
342       : AuxType(COFFYAML::AuxSymbolType(0)) {}
343   NAuxTokenType(IO &, uint8_t C)
344       : AuxType(COFFYAML::AuxSymbolType(C)) {}
345 
346   uint32_t denormalize(IO &) { return AuxType; }
347 
348   COFFYAML::AuxSymbolType AuxType;
349 };
350 
351 struct NStorageClass {
352   NStorageClass(IO &) : StorageClass(COFF::SymbolStorageClass(0)) {}
353   NStorageClass(IO &, uint8_t S) : StorageClass(COFF::SymbolStorageClass(S)) {}
354 
355   uint8_t denormalize(IO &) { return StorageClass; }
356 
357   COFF::SymbolStorageClass StorageClass;
358 };
359 
360 struct NMachine {
361   NMachine(IO &) : Machine(COFF::MachineTypes(0)) {}
362   NMachine(IO &, uint16_t M) : Machine(COFF::MachineTypes(M)) {}
363 
364   uint16_t denormalize(IO &) { return Machine; }
365 
366   COFF::MachineTypes Machine;
367 };
368 
369 struct NHeaderCharacteristics {
370   NHeaderCharacteristics(IO &) : Characteristics(COFF::Characteristics(0)) {}
371   NHeaderCharacteristics(IO &, uint16_t C)
372       : Characteristics(COFF::Characteristics(C)) {}
373 
374   uint16_t denormalize(IO &) { return Characteristics; }
375 
376   COFF::Characteristics Characteristics;
377 };
378 
379 template <typename RelocType>
380 struct NType {
381   NType(IO &) : Type(RelocType(0)) {}
382   NType(IO &, uint16_t T) : Type(RelocType(T)) {}
383 
384   uint16_t denormalize(IO &) { return Type; }
385 
386   RelocType Type;
387 };
388 
389 struct NWindowsSubsystem {
390   NWindowsSubsystem(IO &) : Subsystem(COFF::WindowsSubsystem(0)) {}
391   NWindowsSubsystem(IO &, uint16_t C) : Subsystem(COFF::WindowsSubsystem(C)) {}
392 
393   uint16_t denormalize(IO &) { return Subsystem; }
394 
395   COFF::WindowsSubsystem Subsystem;
396 };
397 
398 struct NDLLCharacteristics {
399   NDLLCharacteristics(IO &) : Characteristics(COFF::DLLCharacteristics(0)) {}
400   NDLLCharacteristics(IO &, uint16_t C)
401       : Characteristics(COFF::DLLCharacteristics(C)) {}
402 
403   uint16_t denormalize(IO &) { return Characteristics; }
404 
405   COFF::DLLCharacteristics Characteristics;
406 };
407 
408 } // end anonymous namespace
409 
410 void MappingTraits<COFFYAML::Relocation>::mapping(IO &IO,
411                                                   COFFYAML::Relocation &Rel) {
412   IO.mapRequired("VirtualAddress", Rel.VirtualAddress);
413   IO.mapOptional("SymbolName", Rel.SymbolName, StringRef());
414   IO.mapOptional("SymbolTableIndex", Rel.SymbolTableIndex);
415 
416   COFF::header &H = *static_cast<COFF::header *>(IO.getContext());
417   if (H.Machine == COFF::IMAGE_FILE_MACHINE_I386) {
418     MappingNormalization<NType<COFF::RelocationTypeI386>, uint16_t> NT(
419         IO, Rel.Type);
420     IO.mapRequired("Type", NT->Type);
421   } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_AMD64) {
422     MappingNormalization<NType<COFF::RelocationTypeAMD64>, uint16_t> NT(
423         IO, Rel.Type);
424     IO.mapRequired("Type", NT->Type);
425   } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
426     MappingNormalization<NType<COFF::RelocationTypesARM>, uint16_t> NT(
427         IO, Rel.Type);
428     IO.mapRequired("Type", NT->Type);
429   } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_ARM64) {
430     MappingNormalization<NType<COFF::RelocationTypesARM64>, uint16_t> NT(
431         IO, Rel.Type);
432     IO.mapRequired("Type", NT->Type);
433   } else {
434     IO.mapRequired("Type", Rel.Type);
435   }
436 }
437 
438 void MappingTraits<COFF::DataDirectory>::mapping(IO &IO,
439                                                  COFF::DataDirectory &DD) {
440   IO.mapRequired("RelativeVirtualAddress", DD.RelativeVirtualAddress);
441   IO.mapRequired("Size", DD.Size);
442 }
443 
444 void MappingTraits<COFFYAML::PEHeader>::mapping(IO &IO,
445                                                 COFFYAML::PEHeader &PH) {
446   MappingNormalization<NWindowsSubsystem, uint16_t> NWS(IO,
447                                                         PH.Header.Subsystem);
448   MappingNormalization<NDLLCharacteristics, uint16_t> NDC(
449       IO, PH.Header.DLLCharacteristics);
450 
451   IO.mapOptional("AddressOfEntryPoint", PH.Header.AddressOfEntryPoint);
452   IO.mapOptional("ImageBase", PH.Header.ImageBase);
453   IO.mapOptional("SectionAlignment", PH.Header.SectionAlignment, 1);
454   IO.mapOptional("FileAlignment", PH.Header.FileAlignment, 1);
455   IO.mapOptional("MajorOperatingSystemVersion",
456                  PH.Header.MajorOperatingSystemVersion);
457   IO.mapOptional("MinorOperatingSystemVersion",
458                  PH.Header.MinorOperatingSystemVersion);
459   IO.mapOptional("MajorImageVersion", PH.Header.MajorImageVersion);
460   IO.mapOptional("MinorImageVersion", PH.Header.MinorImageVersion);
461   IO.mapOptional("MajorSubsystemVersion", PH.Header.MajorSubsystemVersion);
462   IO.mapOptional("MinorSubsystemVersion", PH.Header.MinorSubsystemVersion);
463   IO.mapOptional("Subsystem", NWS->Subsystem);
464   IO.mapOptional("DLLCharacteristics", NDC->Characteristics);
465   IO.mapOptional("SizeOfStackReserve", PH.Header.SizeOfStackReserve);
466   IO.mapOptional("SizeOfStackCommit", PH.Header.SizeOfStackCommit);
467   IO.mapOptional("SizeOfHeapReserve", PH.Header.SizeOfHeapReserve);
468   IO.mapOptional("SizeOfHeapCommit", PH.Header.SizeOfHeapCommit);
469 
470   IO.mapOptional("NumberOfRvaAndSize", PH.Header.NumberOfRvaAndSize,
471                  COFF::NUM_DATA_DIRECTORIES + 1);
472   IO.mapOptional("ExportTable", PH.DataDirectories[COFF::EXPORT_TABLE]);
473   IO.mapOptional("ImportTable", PH.DataDirectories[COFF::IMPORT_TABLE]);
474   IO.mapOptional("ResourceTable", PH.DataDirectories[COFF::RESOURCE_TABLE]);
475   IO.mapOptional("ExceptionTable", PH.DataDirectories[COFF::EXCEPTION_TABLE]);
476   IO.mapOptional("CertificateTable", PH.DataDirectories[COFF::CERTIFICATE_TABLE]);
477   IO.mapOptional("BaseRelocationTable",
478                  PH.DataDirectories[COFF::BASE_RELOCATION_TABLE]);
479   IO.mapOptional("Debug", PH.DataDirectories[COFF::DEBUG_DIRECTORY]);
480   IO.mapOptional("Architecture", PH.DataDirectories[COFF::ARCHITECTURE]);
481   IO.mapOptional("GlobalPtr", PH.DataDirectories[COFF::GLOBAL_PTR]);
482   IO.mapOptional("TlsTable", PH.DataDirectories[COFF::TLS_TABLE]);
483   IO.mapOptional("LoadConfigTable",
484                  PH.DataDirectories[COFF::LOAD_CONFIG_TABLE]);
485   IO.mapOptional("BoundImport", PH.DataDirectories[COFF::BOUND_IMPORT]);
486   IO.mapOptional("IAT", PH.DataDirectories[COFF::IAT]);
487   IO.mapOptional("DelayImportDescriptor",
488                  PH.DataDirectories[COFF::DELAY_IMPORT_DESCRIPTOR]);
489   IO.mapOptional("ClrRuntimeHeader",
490                  PH.DataDirectories[COFF::CLR_RUNTIME_HEADER]);
491 }
492 
493 void MappingTraits<COFF::header>::mapping(IO &IO, COFF::header &H) {
494   MappingNormalization<NMachine, uint16_t> NM(IO, H.Machine);
495   MappingNormalization<NHeaderCharacteristics, uint16_t> NC(IO,
496                                                             H.Characteristics);
497 
498   IO.mapRequired("Machine", NM->Machine);
499   IO.mapOptional("Characteristics", NC->Characteristics);
500   IO.setContext(static_cast<void *>(&H));
501 }
502 
503 void MappingTraits<COFF::AuxiliaryFunctionDefinition>::mapping(
504     IO &IO, COFF::AuxiliaryFunctionDefinition &AFD) {
505   IO.mapRequired("TagIndex", AFD.TagIndex);
506   IO.mapRequired("TotalSize", AFD.TotalSize);
507   IO.mapRequired("PointerToLinenumber", AFD.PointerToLinenumber);
508   IO.mapRequired("PointerToNextFunction", AFD.PointerToNextFunction);
509 }
510 
511 void MappingTraits<COFF::AuxiliarybfAndefSymbol>::mapping(
512     IO &IO, COFF::AuxiliarybfAndefSymbol &AAS) {
513   IO.mapRequired("Linenumber", AAS.Linenumber);
514   IO.mapRequired("PointerToNextFunction", AAS.PointerToNextFunction);
515 }
516 
517 void MappingTraits<COFF::AuxiliaryWeakExternal>::mapping(
518     IO &IO, COFF::AuxiliaryWeakExternal &AWE) {
519   MappingNormalization<NWeakExternalCharacteristics, uint32_t> NWEC(
520       IO, AWE.Characteristics);
521   IO.mapRequired("TagIndex", AWE.TagIndex);
522   IO.mapRequired("Characteristics", NWEC->Characteristics);
523 }
524 
525 void MappingTraits<COFF::AuxiliarySectionDefinition>::mapping(
526     IO &IO, COFF::AuxiliarySectionDefinition &ASD) {
527   MappingNormalization<NSectionSelectionType, uint8_t> NSST(
528       IO, ASD.Selection);
529 
530   IO.mapRequired("Length", ASD.Length);
531   IO.mapRequired("NumberOfRelocations", ASD.NumberOfRelocations);
532   IO.mapRequired("NumberOfLinenumbers", ASD.NumberOfLinenumbers);
533   IO.mapRequired("CheckSum", ASD.CheckSum);
534   IO.mapRequired("Number", ASD.Number);
535   IO.mapOptional("Selection", NSST->SelectionType, COFFYAML::COMDATType(0));
536 }
537 
538 void MappingTraits<COFF::AuxiliaryCLRToken>::mapping(
539     IO &IO, COFF::AuxiliaryCLRToken &ACT) {
540   MappingNormalization<NAuxTokenType, uint8_t> NATT(IO, ACT.AuxType);
541   IO.mapRequired("AuxType", NATT->AuxType);
542   IO.mapRequired("SymbolTableIndex", ACT.SymbolTableIndex);
543 }
544 
545 void MappingTraits<COFFYAML::Symbol>::mapping(IO &IO, COFFYAML::Symbol &S) {
546   MappingNormalization<NStorageClass, uint8_t> NS(IO, S.Header.StorageClass);
547 
548   IO.mapRequired("Name", S.Name);
549   IO.mapRequired("Value", S.Header.Value);
550   IO.mapRequired("SectionNumber", S.Header.SectionNumber);
551   IO.mapRequired("SimpleType", S.SimpleType);
552   IO.mapRequired("ComplexType", S.ComplexType);
553   IO.mapRequired("StorageClass", NS->StorageClass);
554   IO.mapOptional("FunctionDefinition", S.FunctionDefinition);
555   IO.mapOptional("bfAndefSymbol", S.bfAndefSymbol);
556   IO.mapOptional("WeakExternal", S.WeakExternal);
557   IO.mapOptional("File", S.File, StringRef());
558   IO.mapOptional("SectionDefinition", S.SectionDefinition);
559   IO.mapOptional("CLRToken", S.CLRToken);
560 }
561 
562 void MappingTraits<COFFYAML::Section>::mapping(IO &IO, COFFYAML::Section &Sec) {
563   MappingNormalization<NSectionCharacteristics, uint32_t> NC(
564       IO, Sec.Header.Characteristics);
565   IO.mapRequired("Name", Sec.Name);
566   IO.mapRequired("Characteristics", NC->Characteristics);
567   IO.mapOptional("VirtualAddress", Sec.Header.VirtualAddress, 0U);
568   IO.mapOptional("VirtualSize", Sec.Header.VirtualSize, 0U);
569   IO.mapOptional("Alignment", Sec.Alignment, 0U);
570 
571   // If this is a .debug$S .debug$T .debug$P, or .debug$H section parse the
572   // semantic representation of the symbols/types.  If it is any other kind
573   // of section, just deal in raw bytes.
574   IO.mapOptional("SectionData", Sec.SectionData);
575   if (Sec.Name == ".debug$S")
576     IO.mapOptional("Subsections", Sec.DebugS);
577   else if (Sec.Name == ".debug$T")
578     IO.mapOptional("Types", Sec.DebugT);
579   else if (Sec.Name == ".debug$P")
580     IO.mapOptional("PrecompTypes", Sec.DebugP);
581   else if (Sec.Name == ".debug$H")
582     IO.mapOptional("GlobalHashes", Sec.DebugH);
583 
584   // Uninitialized sections, such as .bss, typically have no data, but the size
585   // is carried in SizeOfRawData, even though PointerToRawData is zero.
586   if (Sec.SectionData.binary_size() == 0 &&
587       NC->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
588     IO.mapOptional("SizeOfRawData", Sec.Header.SizeOfRawData);
589 
590   IO.mapOptional("Relocations", Sec.Relocations);
591 }
592 
593 void MappingTraits<COFFYAML::Object>::mapping(IO &IO, COFFYAML::Object &Obj) {
594   IO.mapTag("!COFF", true);
595   IO.mapOptional("OptionalHeader", Obj.OptionalHeader);
596   IO.mapRequired("header", Obj.Header);
597   IO.mapRequired("sections", Obj.Sections);
598   IO.mapRequired("symbols", Obj.Symbols);
599 }
600 
601 } // end namespace yaml
602 
603 } // end namespace llvm
604