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