xref: /freebsd/contrib/llvm-project/llvm/lib/ObjectYAML/ELFYAML.cpp (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1 //===- ELFYAML.cpp - ELF 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 ELF.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ObjectYAML/ELFYAML.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/MapVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/BinaryFormat/ELF.h"
18 #include "llvm/Support/ARMEHABI.h"
19 #include "llvm/Support/Casting.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MipsABIFlags.h"
22 #include "llvm/Support/YAMLTraits.h"
23 #include "llvm/Support/WithColor.h"
24 #include <cassert>
25 #include <cstdint>
26 
27 namespace llvm {
28 
29 ELFYAML::Chunk::~Chunk() = default;
30 
31 namespace ELFYAML {
32 unsigned Object::getMachine() const {
33   if (Header.Machine)
34     return *Header.Machine;
35   return llvm::ELF::EM_NONE;
36 }
37 
38 constexpr StringRef SectionHeaderTable::TypeStr;
39 } // namespace ELFYAML
40 
41 namespace yaml {
42 
43 void ScalarEnumerationTraits<ELFYAML::ELF_ET>::enumeration(
44     IO &IO, ELFYAML::ELF_ET &Value) {
45 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
46   ECase(ET_NONE);
47   ECase(ET_REL);
48   ECase(ET_EXEC);
49   ECase(ET_DYN);
50   ECase(ET_CORE);
51 #undef ECase
52   IO.enumFallback<Hex16>(Value);
53 }
54 
55 void ScalarEnumerationTraits<ELFYAML::ELF_PT>::enumeration(
56     IO &IO, ELFYAML::ELF_PT &Value) {
57 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
58   ECase(PT_NULL);
59   ECase(PT_LOAD);
60   ECase(PT_DYNAMIC);
61   ECase(PT_INTERP);
62   ECase(PT_NOTE);
63   ECase(PT_SHLIB);
64   ECase(PT_PHDR);
65   ECase(PT_TLS);
66   ECase(PT_GNU_EH_FRAME);
67   ECase(PT_GNU_STACK);
68   ECase(PT_GNU_RELRO);
69   ECase(PT_GNU_PROPERTY);
70 #undef ECase
71   IO.enumFallback<Hex32>(Value);
72 }
73 
74 void ScalarEnumerationTraits<ELFYAML::ELF_NT>::enumeration(
75     IO &IO, ELFYAML::ELF_NT &Value) {
76 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
77   // Generic note types.
78   ECase(NT_VERSION);
79   ECase(NT_ARCH);
80   ECase(NT_GNU_BUILD_ATTRIBUTE_OPEN);
81   ECase(NT_GNU_BUILD_ATTRIBUTE_FUNC);
82   // Core note types.
83   ECase(NT_PRSTATUS);
84   ECase(NT_FPREGSET);
85   ECase(NT_PRPSINFO);
86   ECase(NT_TASKSTRUCT);
87   ECase(NT_AUXV);
88   ECase(NT_PSTATUS);
89   ECase(NT_FPREGS);
90   ECase(NT_PSINFO);
91   ECase(NT_LWPSTATUS);
92   ECase(NT_LWPSINFO);
93   ECase(NT_WIN32PSTATUS);
94   ECase(NT_PPC_VMX);
95   ECase(NT_PPC_VSX);
96   ECase(NT_PPC_TAR);
97   ECase(NT_PPC_PPR);
98   ECase(NT_PPC_DSCR);
99   ECase(NT_PPC_EBB);
100   ECase(NT_PPC_PMU);
101   ECase(NT_PPC_TM_CGPR);
102   ECase(NT_PPC_TM_CFPR);
103   ECase(NT_PPC_TM_CVMX);
104   ECase(NT_PPC_TM_CVSX);
105   ECase(NT_PPC_TM_SPR);
106   ECase(NT_PPC_TM_CTAR);
107   ECase(NT_PPC_TM_CPPR);
108   ECase(NT_PPC_TM_CDSCR);
109   ECase(NT_386_TLS);
110   ECase(NT_386_IOPERM);
111   ECase(NT_X86_XSTATE);
112   ECase(NT_S390_HIGH_GPRS);
113   ECase(NT_S390_TIMER);
114   ECase(NT_S390_TODCMP);
115   ECase(NT_S390_TODPREG);
116   ECase(NT_S390_CTRS);
117   ECase(NT_S390_PREFIX);
118   ECase(NT_S390_LAST_BREAK);
119   ECase(NT_S390_SYSTEM_CALL);
120   ECase(NT_S390_TDB);
121   ECase(NT_S390_VXRS_LOW);
122   ECase(NT_S390_VXRS_HIGH);
123   ECase(NT_S390_GS_CB);
124   ECase(NT_S390_GS_BC);
125   ECase(NT_ARM_VFP);
126   ECase(NT_ARM_TLS);
127   ECase(NT_ARM_HW_BREAK);
128   ECase(NT_ARM_HW_WATCH);
129   ECase(NT_ARM_SVE);
130   ECase(NT_ARM_PAC_MASK);
131   ECase(NT_FILE);
132   ECase(NT_PRXFPREG);
133   ECase(NT_SIGINFO);
134   // LLVM-specific notes.
135   ECase(NT_LLVM_HWASAN_GLOBALS);
136   // GNU note types
137   ECase(NT_GNU_ABI_TAG);
138   ECase(NT_GNU_HWCAP);
139   ECase(NT_GNU_BUILD_ID);
140   ECase(NT_GNU_GOLD_VERSION);
141   ECase(NT_GNU_PROPERTY_TYPE_0);
142   // FreeBSD note types.
143   ECase(NT_FREEBSD_ABI_TAG);
144   ECase(NT_FREEBSD_NOINIT_TAG);
145   ECase(NT_FREEBSD_ARCH_TAG);
146   ECase(NT_FREEBSD_FEATURE_CTL);
147   // FreeBSD core note types.
148   ECase(NT_FREEBSD_THRMISC);
149   ECase(NT_FREEBSD_PROCSTAT_PROC);
150   ECase(NT_FREEBSD_PROCSTAT_FILES);
151   ECase(NT_FREEBSD_PROCSTAT_VMMAP);
152   ECase(NT_FREEBSD_PROCSTAT_GROUPS);
153   ECase(NT_FREEBSD_PROCSTAT_UMASK);
154   ECase(NT_FREEBSD_PROCSTAT_RLIMIT);
155   ECase(NT_FREEBSD_PROCSTAT_OSREL);
156   ECase(NT_FREEBSD_PROCSTAT_PSSTRINGS);
157   ECase(NT_FREEBSD_PROCSTAT_AUXV);
158   // NetBSD core note types.
159   ECase(NT_NETBSDCORE_PROCINFO);
160   ECase(NT_NETBSDCORE_AUXV);
161   ECase(NT_NETBSDCORE_LWPSTATUS);
162   // OpenBSD core note types.
163   ECase(NT_OPENBSD_PROCINFO);
164   ECase(NT_OPENBSD_AUXV);
165   ECase(NT_OPENBSD_REGS);
166   ECase(NT_OPENBSD_FPREGS);
167   ECase(NT_OPENBSD_XFPREGS);
168   ECase(NT_OPENBSD_WCOOKIE);
169   // AMD specific notes. (Code Object V2)
170   ECase(NT_AMD_HSA_CODE_OBJECT_VERSION);
171   ECase(NT_AMD_HSA_HSAIL);
172   ECase(NT_AMD_HSA_ISA_VERSION);
173   ECase(NT_AMD_HSA_METADATA);
174   ECase(NT_AMD_HSA_ISA_NAME);
175   ECase(NT_AMD_PAL_METADATA);
176   // AMDGPU specific notes. (Code Object V3)
177   ECase(NT_AMDGPU_METADATA);
178 #undef ECase
179   IO.enumFallback<Hex32>(Value);
180 }
181 
182 void ScalarEnumerationTraits<ELFYAML::ELF_EM>::enumeration(
183     IO &IO, ELFYAML::ELF_EM &Value) {
184 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
185   ECase(EM_NONE);
186   ECase(EM_M32);
187   ECase(EM_SPARC);
188   ECase(EM_386);
189   ECase(EM_68K);
190   ECase(EM_88K);
191   ECase(EM_IAMCU);
192   ECase(EM_860);
193   ECase(EM_MIPS);
194   ECase(EM_S370);
195   ECase(EM_MIPS_RS3_LE);
196   ECase(EM_PARISC);
197   ECase(EM_VPP500);
198   ECase(EM_SPARC32PLUS);
199   ECase(EM_960);
200   ECase(EM_PPC);
201   ECase(EM_PPC64);
202   ECase(EM_S390);
203   ECase(EM_SPU);
204   ECase(EM_V800);
205   ECase(EM_FR20);
206   ECase(EM_RH32);
207   ECase(EM_RCE);
208   ECase(EM_ARM);
209   ECase(EM_ALPHA);
210   ECase(EM_SH);
211   ECase(EM_SPARCV9);
212   ECase(EM_TRICORE);
213   ECase(EM_ARC);
214   ECase(EM_H8_300);
215   ECase(EM_H8_300H);
216   ECase(EM_H8S);
217   ECase(EM_H8_500);
218   ECase(EM_IA_64);
219   ECase(EM_MIPS_X);
220   ECase(EM_COLDFIRE);
221   ECase(EM_68HC12);
222   ECase(EM_MMA);
223   ECase(EM_PCP);
224   ECase(EM_NCPU);
225   ECase(EM_NDR1);
226   ECase(EM_STARCORE);
227   ECase(EM_ME16);
228   ECase(EM_ST100);
229   ECase(EM_TINYJ);
230   ECase(EM_X86_64);
231   ECase(EM_PDSP);
232   ECase(EM_PDP10);
233   ECase(EM_PDP11);
234   ECase(EM_FX66);
235   ECase(EM_ST9PLUS);
236   ECase(EM_ST7);
237   ECase(EM_68HC16);
238   ECase(EM_68HC11);
239   ECase(EM_68HC08);
240   ECase(EM_68HC05);
241   ECase(EM_SVX);
242   ECase(EM_ST19);
243   ECase(EM_VAX);
244   ECase(EM_CRIS);
245   ECase(EM_JAVELIN);
246   ECase(EM_FIREPATH);
247   ECase(EM_ZSP);
248   ECase(EM_MMIX);
249   ECase(EM_HUANY);
250   ECase(EM_PRISM);
251   ECase(EM_AVR);
252   ECase(EM_FR30);
253   ECase(EM_D10V);
254   ECase(EM_D30V);
255   ECase(EM_V850);
256   ECase(EM_M32R);
257   ECase(EM_MN10300);
258   ECase(EM_MN10200);
259   ECase(EM_PJ);
260   ECase(EM_OPENRISC);
261   ECase(EM_ARC_COMPACT);
262   ECase(EM_XTENSA);
263   ECase(EM_VIDEOCORE);
264   ECase(EM_TMM_GPP);
265   ECase(EM_NS32K);
266   ECase(EM_TPC);
267   ECase(EM_SNP1K);
268   ECase(EM_ST200);
269   ECase(EM_IP2K);
270   ECase(EM_MAX);
271   ECase(EM_CR);
272   ECase(EM_F2MC16);
273   ECase(EM_MSP430);
274   ECase(EM_BLACKFIN);
275   ECase(EM_SE_C33);
276   ECase(EM_SEP);
277   ECase(EM_ARCA);
278   ECase(EM_UNICORE);
279   ECase(EM_EXCESS);
280   ECase(EM_DXP);
281   ECase(EM_ALTERA_NIOS2);
282   ECase(EM_CRX);
283   ECase(EM_XGATE);
284   ECase(EM_C166);
285   ECase(EM_M16C);
286   ECase(EM_DSPIC30F);
287   ECase(EM_CE);
288   ECase(EM_M32C);
289   ECase(EM_TSK3000);
290   ECase(EM_RS08);
291   ECase(EM_SHARC);
292   ECase(EM_ECOG2);
293   ECase(EM_SCORE7);
294   ECase(EM_DSP24);
295   ECase(EM_VIDEOCORE3);
296   ECase(EM_LATTICEMICO32);
297   ECase(EM_SE_C17);
298   ECase(EM_TI_C6000);
299   ECase(EM_TI_C2000);
300   ECase(EM_TI_C5500);
301   ECase(EM_MMDSP_PLUS);
302   ECase(EM_CYPRESS_M8C);
303   ECase(EM_R32C);
304   ECase(EM_TRIMEDIA);
305   ECase(EM_HEXAGON);
306   ECase(EM_8051);
307   ECase(EM_STXP7X);
308   ECase(EM_NDS32);
309   ECase(EM_ECOG1);
310   ECase(EM_ECOG1X);
311   ECase(EM_MAXQ30);
312   ECase(EM_XIMO16);
313   ECase(EM_MANIK);
314   ECase(EM_CRAYNV2);
315   ECase(EM_RX);
316   ECase(EM_METAG);
317   ECase(EM_MCST_ELBRUS);
318   ECase(EM_ECOG16);
319   ECase(EM_CR16);
320   ECase(EM_ETPU);
321   ECase(EM_SLE9X);
322   ECase(EM_L10M);
323   ECase(EM_K10M);
324   ECase(EM_AARCH64);
325   ECase(EM_AVR32);
326   ECase(EM_STM8);
327   ECase(EM_TILE64);
328   ECase(EM_TILEPRO);
329   ECase(EM_MICROBLAZE);
330   ECase(EM_CUDA);
331   ECase(EM_TILEGX);
332   ECase(EM_CLOUDSHIELD);
333   ECase(EM_COREA_1ST);
334   ECase(EM_COREA_2ND);
335   ECase(EM_ARC_COMPACT2);
336   ECase(EM_OPEN8);
337   ECase(EM_RL78);
338   ECase(EM_VIDEOCORE5);
339   ECase(EM_78KOR);
340   ECase(EM_56800EX);
341   ECase(EM_AMDGPU);
342   ECase(EM_RISCV);
343   ECase(EM_LANAI);
344   ECase(EM_BPF);
345   ECase(EM_VE);
346   ECase(EM_CSKY);
347 #undef ECase
348   IO.enumFallback<Hex16>(Value);
349 }
350 
351 void ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS>::enumeration(
352     IO &IO, ELFYAML::ELF_ELFCLASS &Value) {
353 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
354   // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it
355   // here.
356   ECase(ELFCLASS32);
357   ECase(ELFCLASS64);
358 #undef ECase
359 }
360 
361 void ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA>::enumeration(
362     IO &IO, ELFYAML::ELF_ELFDATA &Value) {
363 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
364   // ELFDATANONE is an invalid data encoding, but we accept it because
365   // we want to be able to produce invalid binaries for the tests.
366   ECase(ELFDATANONE);
367   ECase(ELFDATA2LSB);
368   ECase(ELFDATA2MSB);
369 #undef ECase
370 }
371 
372 void ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI>::enumeration(
373     IO &IO, ELFYAML::ELF_ELFOSABI &Value) {
374 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
375   ECase(ELFOSABI_NONE);
376   ECase(ELFOSABI_HPUX);
377   ECase(ELFOSABI_NETBSD);
378   ECase(ELFOSABI_GNU);
379   ECase(ELFOSABI_LINUX);
380   ECase(ELFOSABI_HURD);
381   ECase(ELFOSABI_SOLARIS);
382   ECase(ELFOSABI_AIX);
383   ECase(ELFOSABI_IRIX);
384   ECase(ELFOSABI_FREEBSD);
385   ECase(ELFOSABI_TRU64);
386   ECase(ELFOSABI_MODESTO);
387   ECase(ELFOSABI_OPENBSD);
388   ECase(ELFOSABI_OPENVMS);
389   ECase(ELFOSABI_NSK);
390   ECase(ELFOSABI_AROS);
391   ECase(ELFOSABI_FENIXOS);
392   ECase(ELFOSABI_CLOUDABI);
393   ECase(ELFOSABI_AMDGPU_HSA);
394   ECase(ELFOSABI_AMDGPU_PAL);
395   ECase(ELFOSABI_AMDGPU_MESA3D);
396   ECase(ELFOSABI_ARM);
397   ECase(ELFOSABI_C6000_ELFABI);
398   ECase(ELFOSABI_C6000_LINUX);
399   ECase(ELFOSABI_STANDALONE);
400 #undef ECase
401   IO.enumFallback<Hex8>(Value);
402 }
403 
404 void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO,
405                                                  ELFYAML::ELF_EF &Value) {
406   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
407   assert(Object && "The IO context is not initialized");
408 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
409 #define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M)
410   switch (Object->getMachine()) {
411   case ELF::EM_ARM:
412     BCase(EF_ARM_SOFT_FLOAT);
413     BCase(EF_ARM_VFP_FLOAT);
414     BCaseMask(EF_ARM_EABI_UNKNOWN, EF_ARM_EABIMASK);
415     BCaseMask(EF_ARM_EABI_VER1, EF_ARM_EABIMASK);
416     BCaseMask(EF_ARM_EABI_VER2, EF_ARM_EABIMASK);
417     BCaseMask(EF_ARM_EABI_VER3, EF_ARM_EABIMASK);
418     BCaseMask(EF_ARM_EABI_VER4, EF_ARM_EABIMASK);
419     BCaseMask(EF_ARM_EABI_VER5, EF_ARM_EABIMASK);
420     break;
421   case ELF::EM_MIPS:
422     BCase(EF_MIPS_NOREORDER);
423     BCase(EF_MIPS_PIC);
424     BCase(EF_MIPS_CPIC);
425     BCase(EF_MIPS_ABI2);
426     BCase(EF_MIPS_32BITMODE);
427     BCase(EF_MIPS_FP64);
428     BCase(EF_MIPS_NAN2008);
429     BCase(EF_MIPS_MICROMIPS);
430     BCase(EF_MIPS_ARCH_ASE_M16);
431     BCase(EF_MIPS_ARCH_ASE_MDMX);
432     BCaseMask(EF_MIPS_ABI_O32, EF_MIPS_ABI);
433     BCaseMask(EF_MIPS_ABI_O64, EF_MIPS_ABI);
434     BCaseMask(EF_MIPS_ABI_EABI32, EF_MIPS_ABI);
435     BCaseMask(EF_MIPS_ABI_EABI64, EF_MIPS_ABI);
436     BCaseMask(EF_MIPS_MACH_3900, EF_MIPS_MACH);
437     BCaseMask(EF_MIPS_MACH_4010, EF_MIPS_MACH);
438     BCaseMask(EF_MIPS_MACH_4100, EF_MIPS_MACH);
439     BCaseMask(EF_MIPS_MACH_4650, EF_MIPS_MACH);
440     BCaseMask(EF_MIPS_MACH_4120, EF_MIPS_MACH);
441     BCaseMask(EF_MIPS_MACH_4111, EF_MIPS_MACH);
442     BCaseMask(EF_MIPS_MACH_SB1, EF_MIPS_MACH);
443     BCaseMask(EF_MIPS_MACH_OCTEON, EF_MIPS_MACH);
444     BCaseMask(EF_MIPS_MACH_XLR, EF_MIPS_MACH);
445     BCaseMask(EF_MIPS_MACH_OCTEON2, EF_MIPS_MACH);
446     BCaseMask(EF_MIPS_MACH_OCTEON3, EF_MIPS_MACH);
447     BCaseMask(EF_MIPS_MACH_5400, EF_MIPS_MACH);
448     BCaseMask(EF_MIPS_MACH_5900, EF_MIPS_MACH);
449     BCaseMask(EF_MIPS_MACH_5500, EF_MIPS_MACH);
450     BCaseMask(EF_MIPS_MACH_9000, EF_MIPS_MACH);
451     BCaseMask(EF_MIPS_MACH_LS2E, EF_MIPS_MACH);
452     BCaseMask(EF_MIPS_MACH_LS2F, EF_MIPS_MACH);
453     BCaseMask(EF_MIPS_MACH_LS3A, EF_MIPS_MACH);
454     BCaseMask(EF_MIPS_ARCH_1, EF_MIPS_ARCH);
455     BCaseMask(EF_MIPS_ARCH_2, EF_MIPS_ARCH);
456     BCaseMask(EF_MIPS_ARCH_3, EF_MIPS_ARCH);
457     BCaseMask(EF_MIPS_ARCH_4, EF_MIPS_ARCH);
458     BCaseMask(EF_MIPS_ARCH_5, EF_MIPS_ARCH);
459     BCaseMask(EF_MIPS_ARCH_32, EF_MIPS_ARCH);
460     BCaseMask(EF_MIPS_ARCH_64, EF_MIPS_ARCH);
461     BCaseMask(EF_MIPS_ARCH_32R2, EF_MIPS_ARCH);
462     BCaseMask(EF_MIPS_ARCH_64R2, EF_MIPS_ARCH);
463     BCaseMask(EF_MIPS_ARCH_32R6, EF_MIPS_ARCH);
464     BCaseMask(EF_MIPS_ARCH_64R6, EF_MIPS_ARCH);
465     break;
466   case ELF::EM_HEXAGON:
467     BCaseMask(EF_HEXAGON_MACH_V2, EF_HEXAGON_MACH);
468     BCaseMask(EF_HEXAGON_MACH_V3, EF_HEXAGON_MACH);
469     BCaseMask(EF_HEXAGON_MACH_V4, EF_HEXAGON_MACH);
470     BCaseMask(EF_HEXAGON_MACH_V5, EF_HEXAGON_MACH);
471     BCaseMask(EF_HEXAGON_MACH_V55, EF_HEXAGON_MACH);
472     BCaseMask(EF_HEXAGON_MACH_V60, EF_HEXAGON_MACH);
473     BCaseMask(EF_HEXAGON_MACH_V62, EF_HEXAGON_MACH);
474     BCaseMask(EF_HEXAGON_MACH_V65, EF_HEXAGON_MACH);
475     BCaseMask(EF_HEXAGON_MACH_V66, EF_HEXAGON_MACH);
476     BCaseMask(EF_HEXAGON_MACH_V67, EF_HEXAGON_MACH);
477     BCaseMask(EF_HEXAGON_MACH_V67T, EF_HEXAGON_MACH);
478     BCaseMask(EF_HEXAGON_MACH_V68, EF_HEXAGON_MACH);
479     BCaseMask(EF_HEXAGON_MACH_V69, EF_HEXAGON_MACH);
480     BCaseMask(EF_HEXAGON_ISA_V2, EF_HEXAGON_ISA);
481     BCaseMask(EF_HEXAGON_ISA_V3, EF_HEXAGON_ISA);
482     BCaseMask(EF_HEXAGON_ISA_V4, EF_HEXAGON_ISA);
483     BCaseMask(EF_HEXAGON_ISA_V5, EF_HEXAGON_ISA);
484     BCaseMask(EF_HEXAGON_ISA_V55, EF_HEXAGON_ISA);
485     BCaseMask(EF_HEXAGON_ISA_V60, EF_HEXAGON_ISA);
486     BCaseMask(EF_HEXAGON_ISA_V62, EF_HEXAGON_ISA);
487     BCaseMask(EF_HEXAGON_ISA_V65, EF_HEXAGON_ISA);
488     BCaseMask(EF_HEXAGON_ISA_V66, EF_HEXAGON_ISA);
489     BCaseMask(EF_HEXAGON_ISA_V67, EF_HEXAGON_ISA);
490     BCaseMask(EF_HEXAGON_ISA_V68, EF_HEXAGON_ISA);
491     BCaseMask(EF_HEXAGON_ISA_V69, EF_HEXAGON_ISA);
492     break;
493   case ELF::EM_AVR:
494     BCaseMask(EF_AVR_ARCH_AVR1, EF_AVR_ARCH_MASK);
495     BCaseMask(EF_AVR_ARCH_AVR2, EF_AVR_ARCH_MASK);
496     BCaseMask(EF_AVR_ARCH_AVR25, EF_AVR_ARCH_MASK);
497     BCaseMask(EF_AVR_ARCH_AVR3, EF_AVR_ARCH_MASK);
498     BCaseMask(EF_AVR_ARCH_AVR31, EF_AVR_ARCH_MASK);
499     BCaseMask(EF_AVR_ARCH_AVR35, EF_AVR_ARCH_MASK);
500     BCaseMask(EF_AVR_ARCH_AVR4, EF_AVR_ARCH_MASK);
501     BCaseMask(EF_AVR_ARCH_AVR5, EF_AVR_ARCH_MASK);
502     BCaseMask(EF_AVR_ARCH_AVR51, EF_AVR_ARCH_MASK);
503     BCaseMask(EF_AVR_ARCH_AVR6, EF_AVR_ARCH_MASK);
504     BCaseMask(EF_AVR_ARCH_AVRTINY, EF_AVR_ARCH_MASK);
505     BCaseMask(EF_AVR_ARCH_XMEGA1, EF_AVR_ARCH_MASK);
506     BCaseMask(EF_AVR_ARCH_XMEGA2, EF_AVR_ARCH_MASK);
507     BCaseMask(EF_AVR_ARCH_XMEGA3, EF_AVR_ARCH_MASK);
508     BCaseMask(EF_AVR_ARCH_XMEGA4, EF_AVR_ARCH_MASK);
509     BCaseMask(EF_AVR_ARCH_XMEGA5, EF_AVR_ARCH_MASK);
510     BCaseMask(EF_AVR_ARCH_XMEGA6, EF_AVR_ARCH_MASK);
511     BCaseMask(EF_AVR_ARCH_XMEGA7, EF_AVR_ARCH_MASK);
512     BCase(EF_AVR_LINKRELAX_PREPARED);
513     break;
514   case ELF::EM_RISCV:
515     BCase(EF_RISCV_RVC);
516     BCaseMask(EF_RISCV_FLOAT_ABI_SOFT, EF_RISCV_FLOAT_ABI);
517     BCaseMask(EF_RISCV_FLOAT_ABI_SINGLE, EF_RISCV_FLOAT_ABI);
518     BCaseMask(EF_RISCV_FLOAT_ABI_DOUBLE, EF_RISCV_FLOAT_ABI);
519     BCaseMask(EF_RISCV_FLOAT_ABI_QUAD, EF_RISCV_FLOAT_ABI);
520     BCase(EF_RISCV_RVE);
521     BCase(EF_RISCV_TSO);
522     break;
523   case ELF::EM_AMDGPU:
524     BCaseMask(EF_AMDGPU_MACH_NONE, EF_AMDGPU_MACH);
525     BCaseMask(EF_AMDGPU_MACH_R600_R600, EF_AMDGPU_MACH);
526     BCaseMask(EF_AMDGPU_MACH_R600_R630, EF_AMDGPU_MACH);
527     BCaseMask(EF_AMDGPU_MACH_R600_RS880, EF_AMDGPU_MACH);
528     BCaseMask(EF_AMDGPU_MACH_R600_RV670, EF_AMDGPU_MACH);
529     BCaseMask(EF_AMDGPU_MACH_R600_RV710, EF_AMDGPU_MACH);
530     BCaseMask(EF_AMDGPU_MACH_R600_RV730, EF_AMDGPU_MACH);
531     BCaseMask(EF_AMDGPU_MACH_R600_RV770, EF_AMDGPU_MACH);
532     BCaseMask(EF_AMDGPU_MACH_R600_CEDAR, EF_AMDGPU_MACH);
533     BCaseMask(EF_AMDGPU_MACH_R600_CYPRESS, EF_AMDGPU_MACH);
534     BCaseMask(EF_AMDGPU_MACH_R600_JUNIPER, EF_AMDGPU_MACH);
535     BCaseMask(EF_AMDGPU_MACH_R600_REDWOOD, EF_AMDGPU_MACH);
536     BCaseMask(EF_AMDGPU_MACH_R600_SUMO, EF_AMDGPU_MACH);
537     BCaseMask(EF_AMDGPU_MACH_R600_BARTS, EF_AMDGPU_MACH);
538     BCaseMask(EF_AMDGPU_MACH_R600_CAICOS, EF_AMDGPU_MACH);
539     BCaseMask(EF_AMDGPU_MACH_R600_CAYMAN, EF_AMDGPU_MACH);
540     BCaseMask(EF_AMDGPU_MACH_R600_TURKS, EF_AMDGPU_MACH);
541     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX600, EF_AMDGPU_MACH);
542     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX601, EF_AMDGPU_MACH);
543     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX602, EF_AMDGPU_MACH);
544     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX700, EF_AMDGPU_MACH);
545     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX701, EF_AMDGPU_MACH);
546     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX702, EF_AMDGPU_MACH);
547     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX703, EF_AMDGPU_MACH);
548     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX704, EF_AMDGPU_MACH);
549     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX705, EF_AMDGPU_MACH);
550     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX801, EF_AMDGPU_MACH);
551     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX802, EF_AMDGPU_MACH);
552     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX803, EF_AMDGPU_MACH);
553     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX805, EF_AMDGPU_MACH);
554     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX810, EF_AMDGPU_MACH);
555     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX900, EF_AMDGPU_MACH);
556     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX902, EF_AMDGPU_MACH);
557     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX904, EF_AMDGPU_MACH);
558     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX906, EF_AMDGPU_MACH);
559     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX908, EF_AMDGPU_MACH);
560     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX909, EF_AMDGPU_MACH);
561     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX90A, EF_AMDGPU_MACH);
562     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX90C, EF_AMDGPU_MACH);
563     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1010, EF_AMDGPU_MACH);
564     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1011, EF_AMDGPU_MACH);
565     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1012, EF_AMDGPU_MACH);
566     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1013, EF_AMDGPU_MACH);
567     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1030, EF_AMDGPU_MACH);
568     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1031, EF_AMDGPU_MACH);
569     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1032, EF_AMDGPU_MACH);
570     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1033, EF_AMDGPU_MACH);
571     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1034, EF_AMDGPU_MACH);
572     BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1035, EF_AMDGPU_MACH);
573     switch (Object->Header.ABIVersion) {
574     default:
575       // ELFOSABI_AMDGPU_PAL, ELFOSABI_AMDGPU_MESA3D support *_V3 flags.
576       LLVM_FALLTHROUGH;
577     case ELF::ELFABIVERSION_AMDGPU_HSA_V3:
578       BCase(EF_AMDGPU_FEATURE_XNACK_V3);
579       BCase(EF_AMDGPU_FEATURE_SRAMECC_V3);
580       break;
581     case ELF::ELFABIVERSION_AMDGPU_HSA_V4:
582     case ELF::ELFABIVERSION_AMDGPU_HSA_V5:
583       BCaseMask(EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4,
584                 EF_AMDGPU_FEATURE_XNACK_V4);
585       BCaseMask(EF_AMDGPU_FEATURE_XNACK_ANY_V4,
586                 EF_AMDGPU_FEATURE_XNACK_V4);
587       BCaseMask(EF_AMDGPU_FEATURE_XNACK_OFF_V4,
588                 EF_AMDGPU_FEATURE_XNACK_V4);
589       BCaseMask(EF_AMDGPU_FEATURE_XNACK_ON_V4,
590                 EF_AMDGPU_FEATURE_XNACK_V4);
591       BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4,
592                 EF_AMDGPU_FEATURE_SRAMECC_V4);
593       BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_ANY_V4,
594                 EF_AMDGPU_FEATURE_SRAMECC_V4);
595       BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_OFF_V4,
596                 EF_AMDGPU_FEATURE_SRAMECC_V4);
597       BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_ON_V4,
598                 EF_AMDGPU_FEATURE_SRAMECC_V4);
599       break;
600     }
601     break;
602   default:
603     break;
604   }
605 #undef BCase
606 #undef BCaseMask
607 }
608 
609 void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
610     IO &IO, ELFYAML::ELF_SHT &Value) {
611   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
612   assert(Object && "The IO context is not initialized");
613 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
614   ECase(SHT_NULL);
615   ECase(SHT_PROGBITS);
616   ECase(SHT_SYMTAB);
617   // FIXME: Issue a diagnostic with this information.
618   ECase(SHT_STRTAB);
619   ECase(SHT_RELA);
620   ECase(SHT_HASH);
621   ECase(SHT_DYNAMIC);
622   ECase(SHT_NOTE);
623   ECase(SHT_NOBITS);
624   ECase(SHT_REL);
625   ECase(SHT_SHLIB);
626   ECase(SHT_DYNSYM);
627   ECase(SHT_INIT_ARRAY);
628   ECase(SHT_FINI_ARRAY);
629   ECase(SHT_PREINIT_ARRAY);
630   ECase(SHT_GROUP);
631   ECase(SHT_SYMTAB_SHNDX);
632   ECase(SHT_RELR);
633   ECase(SHT_ANDROID_REL);
634   ECase(SHT_ANDROID_RELA);
635   ECase(SHT_ANDROID_RELR);
636   ECase(SHT_LLVM_ODRTAB);
637   ECase(SHT_LLVM_LINKER_OPTIONS);
638   ECase(SHT_LLVM_CALL_GRAPH_PROFILE);
639   ECase(SHT_LLVM_ADDRSIG);
640   ECase(SHT_LLVM_DEPENDENT_LIBRARIES);
641   ECase(SHT_LLVM_SYMPART);
642   ECase(SHT_LLVM_PART_EHDR);
643   ECase(SHT_LLVM_PART_PHDR);
644   ECase(SHT_LLVM_BB_ADDR_MAP);
645   ECase(SHT_GNU_ATTRIBUTES);
646   ECase(SHT_GNU_HASH);
647   ECase(SHT_GNU_verdef);
648   ECase(SHT_GNU_verneed);
649   ECase(SHT_GNU_versym);
650   switch (Object->getMachine()) {
651   case ELF::EM_ARM:
652     ECase(SHT_ARM_EXIDX);
653     ECase(SHT_ARM_PREEMPTMAP);
654     ECase(SHT_ARM_ATTRIBUTES);
655     ECase(SHT_ARM_DEBUGOVERLAY);
656     ECase(SHT_ARM_OVERLAYSECTION);
657     break;
658   case ELF::EM_HEXAGON:
659     ECase(SHT_HEX_ORDERED);
660     break;
661   case ELF::EM_X86_64:
662     ECase(SHT_X86_64_UNWIND);
663     break;
664   case ELF::EM_MIPS:
665     ECase(SHT_MIPS_REGINFO);
666     ECase(SHT_MIPS_OPTIONS);
667     ECase(SHT_MIPS_DWARF);
668     ECase(SHT_MIPS_ABIFLAGS);
669     break;
670   case ELF::EM_RISCV:
671     ECase(SHT_RISCV_ATTRIBUTES);
672     break;
673   case ELF::EM_MSP430:
674     ECase(SHT_MSP430_ATTRIBUTES);
675     break;
676   default:
677     // Nothing to do.
678     break;
679   }
680 #undef ECase
681   IO.enumFallback<Hex32>(Value);
682 }
683 
684 void ScalarBitSetTraits<ELFYAML::ELF_PF>::bitset(IO &IO,
685                                                  ELFYAML::ELF_PF &Value) {
686 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
687   BCase(PF_X);
688   BCase(PF_W);
689   BCase(PF_R);
690 }
691 
692 void ScalarBitSetTraits<ELFYAML::ELF_SHF>::bitset(IO &IO,
693                                                   ELFYAML::ELF_SHF &Value) {
694   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
695 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
696   BCase(SHF_WRITE);
697   BCase(SHF_ALLOC);
698   BCase(SHF_EXCLUDE);
699   BCase(SHF_EXECINSTR);
700   BCase(SHF_MERGE);
701   BCase(SHF_STRINGS);
702   BCase(SHF_INFO_LINK);
703   BCase(SHF_LINK_ORDER);
704   BCase(SHF_OS_NONCONFORMING);
705   BCase(SHF_GROUP);
706   BCase(SHF_TLS);
707   BCase(SHF_COMPRESSED);
708   BCase(SHF_GNU_RETAIN);
709   switch (Object->getMachine()) {
710   case ELF::EM_ARM:
711     BCase(SHF_ARM_PURECODE);
712     break;
713   case ELF::EM_HEXAGON:
714     BCase(SHF_HEX_GPREL);
715     break;
716   case ELF::EM_MIPS:
717     BCase(SHF_MIPS_NODUPES);
718     BCase(SHF_MIPS_NAMES);
719     BCase(SHF_MIPS_LOCAL);
720     BCase(SHF_MIPS_NOSTRIP);
721     BCase(SHF_MIPS_GPREL);
722     BCase(SHF_MIPS_MERGE);
723     BCase(SHF_MIPS_ADDR);
724     BCase(SHF_MIPS_STRING);
725     break;
726   case ELF::EM_X86_64:
727     BCase(SHF_X86_64_LARGE);
728     break;
729   default:
730     // Nothing to do.
731     break;
732   }
733 #undef BCase
734 }
735 
736 void ScalarEnumerationTraits<ELFYAML::ELF_SHN>::enumeration(
737     IO &IO, ELFYAML::ELF_SHN &Value) {
738 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
739   ECase(SHN_UNDEF);
740   ECase(SHN_LORESERVE);
741   ECase(SHN_LOPROC);
742   ECase(SHN_HIPROC);
743   ECase(SHN_LOOS);
744   ECase(SHN_HIOS);
745   ECase(SHN_ABS);
746   ECase(SHN_COMMON);
747   ECase(SHN_XINDEX);
748   ECase(SHN_HIRESERVE);
749   ECase(SHN_AMDGPU_LDS);
750   ECase(SHN_HEXAGON_SCOMMON);
751   ECase(SHN_HEXAGON_SCOMMON_1);
752   ECase(SHN_HEXAGON_SCOMMON_2);
753   ECase(SHN_HEXAGON_SCOMMON_4);
754   ECase(SHN_HEXAGON_SCOMMON_8);
755 #undef ECase
756   IO.enumFallback<Hex16>(Value);
757 }
758 
759 void ScalarEnumerationTraits<ELFYAML::ELF_STB>::enumeration(
760     IO &IO, ELFYAML::ELF_STB &Value) {
761 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
762   ECase(STB_LOCAL);
763   ECase(STB_GLOBAL);
764   ECase(STB_WEAK);
765   ECase(STB_GNU_UNIQUE);
766 #undef ECase
767   IO.enumFallback<Hex8>(Value);
768 }
769 
770 void ScalarEnumerationTraits<ELFYAML::ELF_STT>::enumeration(
771     IO &IO, ELFYAML::ELF_STT &Value) {
772 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
773   ECase(STT_NOTYPE);
774   ECase(STT_OBJECT);
775   ECase(STT_FUNC);
776   ECase(STT_SECTION);
777   ECase(STT_FILE);
778   ECase(STT_COMMON);
779   ECase(STT_TLS);
780   ECase(STT_GNU_IFUNC);
781 #undef ECase
782   IO.enumFallback<Hex8>(Value);
783 }
784 
785 
786 void ScalarEnumerationTraits<ELFYAML::ELF_RSS>::enumeration(
787     IO &IO, ELFYAML::ELF_RSS &Value) {
788 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
789   ECase(RSS_UNDEF);
790   ECase(RSS_GP);
791   ECase(RSS_GP0);
792   ECase(RSS_LOC);
793 #undef ECase
794 }
795 
796 void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration(
797     IO &IO, ELFYAML::ELF_REL &Value) {
798   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
799   assert(Object && "The IO context is not initialized");
800 #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X);
801   switch (Object->getMachine()) {
802   case ELF::EM_X86_64:
803 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
804     break;
805   case ELF::EM_MIPS:
806 #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
807     break;
808   case ELF::EM_HEXAGON:
809 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
810     break;
811   case ELF::EM_386:
812   case ELF::EM_IAMCU:
813 #include "llvm/BinaryFormat/ELFRelocs/i386.def"
814     break;
815   case ELF::EM_AARCH64:
816 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
817     break;
818   case ELF::EM_ARM:
819 #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
820     break;
821   case ELF::EM_ARC:
822 #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
823     break;
824   case ELF::EM_RISCV:
825 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
826     break;
827   case ELF::EM_LANAI:
828 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
829     break;
830   case ELF::EM_AMDGPU:
831 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
832     break;
833   case ELF::EM_BPF:
834 #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
835     break;
836   case ELF::EM_VE:
837 #include "llvm/BinaryFormat/ELFRelocs/VE.def"
838     break;
839   case ELF::EM_CSKY:
840 #include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
841     break;
842   case ELF::EM_PPC:
843 #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
844     break;
845   case ELF::EM_PPC64:
846 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
847     break;
848   case ELF::EM_68K:
849 #include "llvm/BinaryFormat/ELFRelocs/M68k.def"
850     break;
851   default:
852     // Nothing to do.
853     break;
854   }
855 #undef ELF_RELOC
856   IO.enumFallback<Hex32>(Value);
857 }
858 
859 void ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG>::enumeration(
860     IO &IO, ELFYAML::ELF_DYNTAG &Value) {
861   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
862   assert(Object && "The IO context is not initialized");
863 
864 // Disable architecture specific tags by default. We might enable them below.
865 #define AARCH64_DYNAMIC_TAG(name, value)
866 #define MIPS_DYNAMIC_TAG(name, value)
867 #define HEXAGON_DYNAMIC_TAG(name, value)
868 #define PPC_DYNAMIC_TAG(name, value)
869 #define PPC64_DYNAMIC_TAG(name, value)
870 // Ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
871 #define DYNAMIC_TAG_MARKER(name, value)
872 
873 #define STRINGIFY(X) (#X)
874 #define DYNAMIC_TAG(X, Y) IO.enumCase(Value, STRINGIFY(DT_##X), ELF::DT_##X);
875   switch (Object->getMachine()) {
876   case ELF::EM_AARCH64:
877 #undef AARCH64_DYNAMIC_TAG
878 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
879 #include "llvm/BinaryFormat/DynamicTags.def"
880 #undef AARCH64_DYNAMIC_TAG
881 #define AARCH64_DYNAMIC_TAG(name, value)
882     break;
883   case ELF::EM_MIPS:
884 #undef MIPS_DYNAMIC_TAG
885 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
886 #include "llvm/BinaryFormat/DynamicTags.def"
887 #undef MIPS_DYNAMIC_TAG
888 #define MIPS_DYNAMIC_TAG(name, value)
889     break;
890   case ELF::EM_HEXAGON:
891 #undef HEXAGON_DYNAMIC_TAG
892 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
893 #include "llvm/BinaryFormat/DynamicTags.def"
894 #undef HEXAGON_DYNAMIC_TAG
895 #define HEXAGON_DYNAMIC_TAG(name, value)
896     break;
897   case ELF::EM_PPC:
898 #undef PPC_DYNAMIC_TAG
899 #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
900 #include "llvm/BinaryFormat/DynamicTags.def"
901 #undef PPC_DYNAMIC_TAG
902 #define PPC_DYNAMIC_TAG(name, value)
903     break;
904   case ELF::EM_PPC64:
905 #undef PPC64_DYNAMIC_TAG
906 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
907 #include "llvm/BinaryFormat/DynamicTags.def"
908 #undef PPC64_DYNAMIC_TAG
909 #define PPC64_DYNAMIC_TAG(name, value)
910     break;
911   case ELF::EM_RISCV:
912 #undef RISCV_DYNAMIC_TAG
913 #define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
914 #include "llvm/BinaryFormat/DynamicTags.def"
915 #undef RISCV_DYNAMIC_TAG
916 #define RISCV_DYNAMIC_TAG(name, value)
917     break;
918   default:
919 #include "llvm/BinaryFormat/DynamicTags.def"
920     break;
921   }
922 #undef AARCH64_DYNAMIC_TAG
923 #undef MIPS_DYNAMIC_TAG
924 #undef HEXAGON_DYNAMIC_TAG
925 #undef PPC_DYNAMIC_TAG
926 #undef PPC64_DYNAMIC_TAG
927 #undef DYNAMIC_TAG_MARKER
928 #undef STRINGIFY
929 #undef DYNAMIC_TAG
930 
931   IO.enumFallback<Hex64>(Value);
932 }
933 
934 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG>::enumeration(
935     IO &IO, ELFYAML::MIPS_AFL_REG &Value) {
936 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
937   ECase(REG_NONE);
938   ECase(REG_32);
939   ECase(REG_64);
940   ECase(REG_128);
941 #undef ECase
942 }
943 
944 void ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP>::enumeration(
945     IO &IO, ELFYAML::MIPS_ABI_FP &Value) {
946 #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X)
947   ECase(FP_ANY);
948   ECase(FP_DOUBLE);
949   ECase(FP_SINGLE);
950   ECase(FP_SOFT);
951   ECase(FP_OLD_64);
952   ECase(FP_XX);
953   ECase(FP_64);
954   ECase(FP_64A);
955 #undef ECase
956 }
957 
958 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT>::enumeration(
959     IO &IO, ELFYAML::MIPS_AFL_EXT &Value) {
960 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
961   ECase(EXT_NONE);
962   ECase(EXT_XLR);
963   ECase(EXT_OCTEON2);
964   ECase(EXT_OCTEONP);
965   ECase(EXT_LOONGSON_3A);
966   ECase(EXT_OCTEON);
967   ECase(EXT_5900);
968   ECase(EXT_4650);
969   ECase(EXT_4010);
970   ECase(EXT_4100);
971   ECase(EXT_3900);
972   ECase(EXT_10000);
973   ECase(EXT_SB1);
974   ECase(EXT_4111);
975   ECase(EXT_4120);
976   ECase(EXT_5400);
977   ECase(EXT_5500);
978   ECase(EXT_LOONGSON_2E);
979   ECase(EXT_LOONGSON_2F);
980   ECase(EXT_OCTEON3);
981 #undef ECase
982 }
983 
984 void ScalarEnumerationTraits<ELFYAML::MIPS_ISA>::enumeration(
985     IO &IO, ELFYAML::MIPS_ISA &Value) {
986   IO.enumCase(Value, "MIPS1", 1);
987   IO.enumCase(Value, "MIPS2", 2);
988   IO.enumCase(Value, "MIPS3", 3);
989   IO.enumCase(Value, "MIPS4", 4);
990   IO.enumCase(Value, "MIPS5", 5);
991   IO.enumCase(Value, "MIPS32", 32);
992   IO.enumCase(Value, "MIPS64", 64);
993   IO.enumFallback<Hex32>(Value);
994 }
995 
996 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE>::bitset(
997     IO &IO, ELFYAML::MIPS_AFL_ASE &Value) {
998 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X)
999   BCase(DSP);
1000   BCase(DSPR2);
1001   BCase(EVA);
1002   BCase(MCU);
1003   BCase(MDMX);
1004   BCase(MIPS3D);
1005   BCase(MT);
1006   BCase(SMARTMIPS);
1007   BCase(VIRT);
1008   BCase(MSA);
1009   BCase(MIPS16);
1010   BCase(MICROMIPS);
1011   BCase(XPA);
1012   BCase(CRC);
1013   BCase(GINV);
1014 #undef BCase
1015 }
1016 
1017 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1>::bitset(
1018     IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) {
1019 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X)
1020   BCase(ODDSPREG);
1021 #undef BCase
1022 }
1023 
1024 void MappingTraits<ELFYAML::SectionHeader>::mapping(
1025     IO &IO, ELFYAML::SectionHeader &SHdr) {
1026   IO.mapRequired("Name", SHdr.Name);
1027 }
1028 
1029 void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO,
1030                                                  ELFYAML::FileHeader &FileHdr) {
1031   IO.mapRequired("Class", FileHdr.Class);
1032   IO.mapRequired("Data", FileHdr.Data);
1033   IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0));
1034   IO.mapOptional("ABIVersion", FileHdr.ABIVersion, Hex8(0));
1035   IO.mapRequired("Type", FileHdr.Type);
1036   IO.mapOptional("Machine", FileHdr.Machine);
1037   IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0));
1038   IO.mapOptional("Entry", FileHdr.Entry, Hex64(0));
1039   IO.mapOptional("SectionHeaderStringTable", FileHdr.SectionHeaderStringTable);
1040 
1041   // obj2yaml does not dump these fields.
1042   assert(!IO.outputting() ||
1043          (!FileHdr.EPhOff && !FileHdr.EPhEntSize && !FileHdr.EPhNum));
1044   IO.mapOptional("EPhOff", FileHdr.EPhOff);
1045   IO.mapOptional("EPhEntSize", FileHdr.EPhEntSize);
1046   IO.mapOptional("EPhNum", FileHdr.EPhNum);
1047   IO.mapOptional("EShEntSize", FileHdr.EShEntSize);
1048   IO.mapOptional("EShOff", FileHdr.EShOff);
1049   IO.mapOptional("EShNum", FileHdr.EShNum);
1050   IO.mapOptional("EShStrNdx", FileHdr.EShStrNdx);
1051 }
1052 
1053 void MappingTraits<ELFYAML::ProgramHeader>::mapping(
1054     IO &IO, ELFYAML::ProgramHeader &Phdr) {
1055   IO.mapRequired("Type", Phdr.Type);
1056   IO.mapOptional("Flags", Phdr.Flags, ELFYAML::ELF_PF(0));
1057   IO.mapOptional("FirstSec", Phdr.FirstSec);
1058   IO.mapOptional("LastSec", Phdr.LastSec);
1059   IO.mapOptional("VAddr", Phdr.VAddr, Hex64(0));
1060   IO.mapOptional("PAddr", Phdr.PAddr, Phdr.VAddr);
1061   IO.mapOptional("Align", Phdr.Align);
1062   IO.mapOptional("FileSize", Phdr.FileSize);
1063   IO.mapOptional("MemSize", Phdr.MemSize);
1064   IO.mapOptional("Offset", Phdr.Offset);
1065 }
1066 
1067 std::string MappingTraits<ELFYAML::ProgramHeader>::validate(
1068     IO &IO, ELFYAML::ProgramHeader &FileHdr) {
1069   if (!FileHdr.FirstSec && FileHdr.LastSec)
1070     return "the \"LastSec\" key can't be used without the \"FirstSec\" key";
1071   if (FileHdr.FirstSec && !FileHdr.LastSec)
1072     return "the \"FirstSec\" key can't be used without the \"LastSec\" key";
1073   return "";
1074 }
1075 
1076 LLVM_YAML_STRONG_TYPEDEF(StringRef, StOtherPiece)
1077 
1078 template <> struct ScalarTraits<StOtherPiece> {
1079   static void output(const StOtherPiece &Val, void *, raw_ostream &Out) {
1080     Out << Val;
1081   }
1082   static StringRef input(StringRef Scalar, void *, StOtherPiece &Val) {
1083     Val = Scalar;
1084     return {};
1085   }
1086   static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1087 };
1088 template <> struct SequenceElementTraits<StOtherPiece> {
1089   static const bool flow = true;
1090 };
1091 
1092 template <> struct ScalarTraits<ELFYAML::YAMLFlowString> {
1093   static void output(const ELFYAML::YAMLFlowString &Val, void *,
1094                      raw_ostream &Out) {
1095     Out << Val;
1096   }
1097   static StringRef input(StringRef Scalar, void *,
1098                          ELFYAML::YAMLFlowString &Val) {
1099     Val = Scalar;
1100     return {};
1101   }
1102   static QuotingType mustQuote(StringRef S) {
1103     return ScalarTraits<StringRef>::mustQuote(S);
1104   }
1105 };
1106 template <> struct SequenceElementTraits<ELFYAML::YAMLFlowString> {
1107   static const bool flow = true;
1108 };
1109 
1110 namespace {
1111 
1112 struct NormalizedOther {
1113   NormalizedOther(IO &IO) : YamlIO(IO) {}
1114   NormalizedOther(IO &IO, Optional<uint8_t> Original) : YamlIO(IO) {
1115     assert(Original && "This constructor is only used for outputting YAML and "
1116                        "assumes a non-empty Original");
1117     std::vector<StOtherPiece> Ret;
1118     const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext());
1119     for (std::pair<StringRef, uint8_t> &P :
1120          getFlags(Object->getMachine()).takeVector()) {
1121       uint8_t FlagValue = P.second;
1122       if ((*Original & FlagValue) != FlagValue)
1123         continue;
1124       *Original &= ~FlagValue;
1125       Ret.push_back({P.first});
1126     }
1127 
1128     if (*Original != 0) {
1129       UnknownFlagsHolder = std::to_string(*Original);
1130       Ret.push_back({UnknownFlagsHolder});
1131     }
1132 
1133     if (!Ret.empty())
1134       Other = std::move(Ret);
1135   }
1136 
1137   uint8_t toValue(StringRef Name) {
1138     const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext());
1139     MapVector<StringRef, uint8_t> Flags = getFlags(Object->getMachine());
1140 
1141     auto It = Flags.find(Name);
1142     if (It != Flags.end())
1143       return It->second;
1144 
1145     uint8_t Val;
1146     if (to_integer(Name, Val))
1147       return Val;
1148 
1149     YamlIO.setError("an unknown value is used for symbol's 'Other' field: " +
1150                     Name);
1151     return 0;
1152   }
1153 
1154   Optional<uint8_t> denormalize(IO &) {
1155     if (!Other)
1156       return None;
1157     uint8_t Ret = 0;
1158     for (StOtherPiece &Val : *Other)
1159       Ret |= toValue(Val);
1160     return Ret;
1161   }
1162 
1163   // st_other field is used to encode symbol visibility and platform-dependent
1164   // flags and values. This method returns a name to value map that is used for
1165   // parsing and encoding this field.
1166   MapVector<StringRef, uint8_t> getFlags(unsigned EMachine) {
1167     MapVector<StringRef, uint8_t> Map;
1168     // STV_* values are just enumeration values. We add them in a reversed order
1169     // because when we convert the st_other to named constants when printing
1170     // YAML we want to use a maximum number of bits on each step:
1171     // when we have st_other == 3, we want to print it as STV_PROTECTED (3), but
1172     // not as STV_HIDDEN (2) + STV_INTERNAL (1).
1173     Map["STV_PROTECTED"] = ELF::STV_PROTECTED;
1174     Map["STV_HIDDEN"] = ELF::STV_HIDDEN;
1175     Map["STV_INTERNAL"] = ELF::STV_INTERNAL;
1176     // STV_DEFAULT is used to represent the default visibility and has a value
1177     // 0. We want to be able to read it from YAML documents, but there is no
1178     // reason to print it.
1179     if (!YamlIO.outputting())
1180       Map["STV_DEFAULT"] = ELF::STV_DEFAULT;
1181 
1182     // MIPS is not consistent. All of the STO_MIPS_* values are bit flags,
1183     // except STO_MIPS_MIPS16 which overlaps them. It should be checked and
1184     // consumed first when we print the output, because we do not want to print
1185     // any other flags that have the same bits instead.
1186     if (EMachine == ELF::EM_MIPS) {
1187       Map["STO_MIPS_MIPS16"] = ELF::STO_MIPS_MIPS16;
1188       Map["STO_MIPS_MICROMIPS"] = ELF::STO_MIPS_MICROMIPS;
1189       Map["STO_MIPS_PIC"] = ELF::STO_MIPS_PIC;
1190       Map["STO_MIPS_PLT"] = ELF::STO_MIPS_PLT;
1191       Map["STO_MIPS_OPTIONAL"] = ELF::STO_MIPS_OPTIONAL;
1192     }
1193 
1194     if (EMachine == ELF::EM_AARCH64)
1195       Map["STO_AARCH64_VARIANT_PCS"] = ELF::STO_AARCH64_VARIANT_PCS;
1196     if (EMachine == ELF::EM_RISCV)
1197       Map["STO_RISCV_VARIANT_CC"] = ELF::STO_RISCV_VARIANT_CC;
1198     return Map;
1199   }
1200 
1201   IO &YamlIO;
1202   Optional<std::vector<StOtherPiece>> Other;
1203   std::string UnknownFlagsHolder;
1204 };
1205 
1206 } // end anonymous namespace
1207 
1208 void ScalarTraits<ELFYAML::YAMLIntUInt>::output(const ELFYAML::YAMLIntUInt &Val,
1209                                                 void *Ctx, raw_ostream &Out) {
1210   Out << Val;
1211 }
1212 
1213 StringRef ScalarTraits<ELFYAML::YAMLIntUInt>::input(StringRef Scalar, void *Ctx,
1214                                                     ELFYAML::YAMLIntUInt &Val) {
1215   const bool Is64 = static_cast<ELFYAML::Object *>(Ctx)->Header.Class ==
1216                     ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
1217   StringRef ErrMsg = "invalid number";
1218   // We do not accept negative hex numbers because their meaning is ambiguous.
1219   // For example, would -0xfffffffff mean 1 or INT32_MIN?
1220   if (Scalar.empty() || Scalar.startswith("-0x"))
1221     return ErrMsg;
1222 
1223   if (Scalar.startswith("-")) {
1224     const int64_t MinVal = Is64 ? INT64_MIN : INT32_MIN;
1225     long long Int;
1226     if (getAsSignedInteger(Scalar, /*Radix=*/0, Int) || (Int < MinVal))
1227       return ErrMsg;
1228     Val = Int;
1229     return "";
1230   }
1231 
1232   const uint64_t MaxVal = Is64 ? UINT64_MAX : UINT32_MAX;
1233   unsigned long long UInt;
1234   if (getAsUnsignedInteger(Scalar, /*Radix=*/0, UInt) || (UInt > MaxVal))
1235     return ErrMsg;
1236   Val = UInt;
1237   return "";
1238 }
1239 
1240 void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) {
1241   IO.mapOptional("Name", Symbol.Name, StringRef());
1242   IO.mapOptional("StName", Symbol.StName);
1243   IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
1244   IO.mapOptional("Section", Symbol.Section);
1245   IO.mapOptional("Index", Symbol.Index);
1246   IO.mapOptional("Binding", Symbol.Binding, ELFYAML::ELF_STB(0));
1247   IO.mapOptional("Value", Symbol.Value);
1248   IO.mapOptional("Size", Symbol.Size);
1249 
1250   // Symbol's Other field is a bit special. It is usually a field that
1251   // represents st_other and holds the symbol visibility. However, on some
1252   // platforms, it can contain bit fields and regular values, or even sometimes a
1253   // crazy mix of them (see comments for NormalizedOther). Because of this, we
1254   // need special handling.
1255   MappingNormalization<NormalizedOther, Optional<uint8_t>> Keys(IO,
1256                                                                 Symbol.Other);
1257   IO.mapOptional("Other", Keys->Other);
1258 }
1259 
1260 std::string MappingTraits<ELFYAML::Symbol>::validate(IO &IO,
1261                                                      ELFYAML::Symbol &Symbol) {
1262   if (Symbol.Index && Symbol.Section)
1263     return "Index and Section cannot both be specified for Symbol";
1264   return "";
1265 }
1266 
1267 static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
1268   IO.mapOptional("Name", Section.Name, StringRef());
1269   IO.mapRequired("Type", Section.Type);
1270   IO.mapOptional("Flags", Section.Flags);
1271   IO.mapOptional("Address", Section.Address);
1272   IO.mapOptional("Link", Section.Link);
1273   IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
1274   IO.mapOptional("EntSize", Section.EntSize);
1275   IO.mapOptional("Offset", Section.Offset);
1276 
1277   IO.mapOptional("Content", Section.Content);
1278   IO.mapOptional("Size", Section.Size);
1279 
1280   // obj2yaml does not dump these fields. They are expected to be empty when we
1281   // are producing YAML, because yaml2obj sets appropriate values for them
1282   // automatically when they are not explicitly defined.
1283   assert(!IO.outputting() ||
1284          (!Section.ShOffset && !Section.ShSize && !Section.ShName &&
1285           !Section.ShFlags && !Section.ShType && !Section.ShAddrAlign));
1286   IO.mapOptional("ShAddrAlign", Section.ShAddrAlign);
1287   IO.mapOptional("ShName", Section.ShName);
1288   IO.mapOptional("ShOffset", Section.ShOffset);
1289   IO.mapOptional("ShSize", Section.ShSize);
1290   IO.mapOptional("ShFlags", Section.ShFlags);
1291   IO.mapOptional("ShType", Section.ShType);
1292 }
1293 
1294 static void sectionMapping(IO &IO, ELFYAML::DynamicSection &Section) {
1295   commonSectionMapping(IO, Section);
1296   IO.mapOptional("Entries", Section.Entries);
1297 }
1298 
1299 static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
1300   commonSectionMapping(IO, Section);
1301 
1302   // We also support reading a content as array of bytes using the ContentArray
1303   // key. obj2yaml never prints this field.
1304   assert(!IO.outputting() || !Section.ContentBuf.hasValue());
1305   IO.mapOptional("ContentArray", Section.ContentBuf);
1306   if (Section.ContentBuf) {
1307     if (Section.Content)
1308       IO.setError("Content and ContentArray can't be used together");
1309     Section.Content = yaml::BinaryRef(*Section.ContentBuf);
1310   }
1311 
1312   IO.mapOptional("Info", Section.Info);
1313 }
1314 
1315 static void sectionMapping(IO &IO, ELFYAML::BBAddrMapSection &Section) {
1316   commonSectionMapping(IO, Section);
1317   IO.mapOptional("Content", Section.Content);
1318   IO.mapOptional("Entries", Section.Entries);
1319 }
1320 
1321 static void sectionMapping(IO &IO, ELFYAML::StackSizesSection &Section) {
1322   commonSectionMapping(IO, Section);
1323   IO.mapOptional("Entries", Section.Entries);
1324 }
1325 
1326 static void sectionMapping(IO &IO, ELFYAML::HashSection &Section) {
1327   commonSectionMapping(IO, Section);
1328   IO.mapOptional("Bucket", Section.Bucket);
1329   IO.mapOptional("Chain", Section.Chain);
1330 
1331   // obj2yaml does not dump these fields. They can be used to override nchain
1332   // and nbucket values for creating broken sections.
1333   assert(!IO.outputting() ||
1334          (!Section.NBucket.hasValue() && !Section.NChain.hasValue()));
1335   IO.mapOptional("NChain", Section.NChain);
1336   IO.mapOptional("NBucket", Section.NBucket);
1337 }
1338 
1339 static void sectionMapping(IO &IO, ELFYAML::NoteSection &Section) {
1340   commonSectionMapping(IO, Section);
1341   IO.mapOptional("Notes", Section.Notes);
1342 }
1343 
1344 
1345 static void sectionMapping(IO &IO, ELFYAML::GnuHashSection &Section) {
1346   commonSectionMapping(IO, Section);
1347   IO.mapOptional("Header", Section.Header);
1348   IO.mapOptional("BloomFilter", Section.BloomFilter);
1349   IO.mapOptional("HashBuckets", Section.HashBuckets);
1350   IO.mapOptional("HashValues", Section.HashValues);
1351 }
1352 static void sectionMapping(IO &IO, ELFYAML::NoBitsSection &Section) {
1353   commonSectionMapping(IO, Section);
1354 }
1355 
1356 static void sectionMapping(IO &IO, ELFYAML::VerdefSection &Section) {
1357   commonSectionMapping(IO, Section);
1358   IO.mapOptional("Info", Section.Info);
1359   IO.mapOptional("Entries", Section.Entries);
1360 }
1361 
1362 static void sectionMapping(IO &IO, ELFYAML::SymverSection &Section) {
1363   commonSectionMapping(IO, Section);
1364   IO.mapOptional("Entries", Section.Entries);
1365 }
1366 
1367 static void sectionMapping(IO &IO, ELFYAML::VerneedSection &Section) {
1368   commonSectionMapping(IO, Section);
1369   IO.mapOptional("Info", Section.Info);
1370   IO.mapOptional("Dependencies", Section.VerneedV);
1371 }
1372 
1373 static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
1374   commonSectionMapping(IO, Section);
1375   IO.mapOptional("Info", Section.RelocatableSec, StringRef());
1376   IO.mapOptional("Relocations", Section.Relocations);
1377 }
1378 
1379 static void sectionMapping(IO &IO, ELFYAML::RelrSection &Section) {
1380   commonSectionMapping(IO, Section);
1381   IO.mapOptional("Entries", Section.Entries);
1382 }
1383 
1384 static void groupSectionMapping(IO &IO, ELFYAML::GroupSection &Group) {
1385   commonSectionMapping(IO, Group);
1386   IO.mapOptional("Info", Group.Signature);
1387   IO.mapOptional("Members", Group.Members);
1388 }
1389 
1390 static void sectionMapping(IO &IO, ELFYAML::SymtabShndxSection &Section) {
1391   commonSectionMapping(IO, Section);
1392   IO.mapOptional("Entries", Section.Entries);
1393 }
1394 
1395 static void sectionMapping(IO &IO, ELFYAML::AddrsigSection &Section) {
1396   commonSectionMapping(IO, Section);
1397   IO.mapOptional("Symbols", Section.Symbols);
1398 }
1399 
1400 static void fillMapping(IO &IO, ELFYAML::Fill &Fill) {
1401   IO.mapOptional("Name", Fill.Name, StringRef());
1402   IO.mapOptional("Pattern", Fill.Pattern);
1403   IO.mapOptional("Offset", Fill.Offset);
1404   IO.mapRequired("Size", Fill.Size);
1405 }
1406 
1407 static void sectionHeaderTableMapping(IO &IO,
1408                                       ELFYAML::SectionHeaderTable &SHT) {
1409   IO.mapOptional("Offset", SHT.Offset);
1410   IO.mapOptional("Sections", SHT.Sections);
1411   IO.mapOptional("Excluded", SHT.Excluded);
1412   IO.mapOptional("NoHeaders", SHT.NoHeaders);
1413 }
1414 
1415 static void sectionMapping(IO &IO, ELFYAML::LinkerOptionsSection &Section) {
1416   commonSectionMapping(IO, Section);
1417   IO.mapOptional("Options", Section.Options);
1418 }
1419 
1420 static void sectionMapping(IO &IO,
1421                            ELFYAML::DependentLibrariesSection &Section) {
1422   commonSectionMapping(IO, Section);
1423   IO.mapOptional("Libraries", Section.Libs);
1424 }
1425 
1426 static void sectionMapping(IO &IO, ELFYAML::CallGraphProfileSection &Section) {
1427   commonSectionMapping(IO, Section);
1428   IO.mapOptional("Entries", Section.Entries);
1429 }
1430 
1431 void MappingTraits<ELFYAML::SectionOrType>::mapping(
1432     IO &IO, ELFYAML::SectionOrType &sectionOrType) {
1433   IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType);
1434 }
1435 
1436 static void sectionMapping(IO &IO, ELFYAML::ARMIndexTableSection &Section) {
1437   commonSectionMapping(IO, Section);
1438   IO.mapOptional("Entries", Section.Entries);
1439 }
1440 
1441 static void sectionMapping(IO &IO, ELFYAML::MipsABIFlags &Section) {
1442   commonSectionMapping(IO, Section);
1443   IO.mapOptional("Version", Section.Version, Hex16(0));
1444   IO.mapRequired("ISA", Section.ISALevel);
1445   IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0));
1446   IO.mapOptional("ISAExtension", Section.ISAExtension,
1447                  ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE));
1448   IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0));
1449   IO.mapOptional("FpABI", Section.FpABI,
1450                  ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY));
1451   IO.mapOptional("GPRSize", Section.GPRSize,
1452                  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
1453   IO.mapOptional("CPR1Size", Section.CPR1Size,
1454                  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
1455   IO.mapOptional("CPR2Size", Section.CPR2Size,
1456                  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
1457   IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0));
1458   IO.mapOptional("Flags2", Section.Flags2, Hex32(0));
1459 }
1460 
1461 static StringRef getStringValue(IO &IO, const char *Key) {
1462   StringRef Val;
1463   IO.mapRequired(Key, Val);
1464   return Val;
1465 }
1466 
1467 static void setStringValue(IO &IO, const char *Key, StringRef Val) {
1468   IO.mapRequired(Key, Val);
1469 }
1470 
1471 static bool isInteger(StringRef Val) {
1472   APInt Tmp;
1473   return !Val.getAsInteger(0, Tmp);
1474 }
1475 
1476 void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
1477     IO &IO, std::unique_ptr<ELFYAML::Chunk> &Section) {
1478   ELFYAML::ELF_SHT Type;
1479   StringRef TypeStr;
1480   if (IO.outputting()) {
1481     if (auto *S = dyn_cast<ELFYAML::Section>(Section.get()))
1482       Type = S->Type;
1483     else if (auto *SHT = dyn_cast<ELFYAML::SectionHeaderTable>(Section.get()))
1484       TypeStr = SHT->TypeStr;
1485   } else {
1486     // When the Type string does not have a "SHT_" prefix, we know it is not a
1487     // description of a regular ELF output section.
1488     TypeStr = getStringValue(IO, "Type");
1489     if (TypeStr.startswith("SHT_") || isInteger(TypeStr))
1490       IO.mapRequired("Type", Type);
1491   }
1492 
1493   if (TypeStr == "Fill") {
1494     assert(!IO.outputting()); // We don't dump fills currently.
1495     Section.reset(new ELFYAML::Fill());
1496     fillMapping(IO, *cast<ELFYAML::Fill>(Section.get()));
1497     return;
1498   }
1499 
1500   if (TypeStr == ELFYAML::SectionHeaderTable::TypeStr) {
1501     if (IO.outputting())
1502       setStringValue(IO, "Type", TypeStr);
1503     else
1504       Section.reset(new ELFYAML::SectionHeaderTable(/*IsImplicit=*/false));
1505 
1506     sectionHeaderTableMapping(
1507         IO, *cast<ELFYAML::SectionHeaderTable>(Section.get()));
1508     return;
1509   }
1510 
1511   const auto &Obj = *static_cast<ELFYAML::Object *>(IO.getContext());
1512   if (Obj.getMachine() == ELF::EM_MIPS && Type == ELF::SHT_MIPS_ABIFLAGS) {
1513     if (!IO.outputting())
1514       Section.reset(new ELFYAML::MipsABIFlags());
1515     sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get()));
1516     return;
1517   }
1518 
1519   if (Obj.getMachine() == ELF::EM_ARM && Type == ELF::SHT_ARM_EXIDX) {
1520     if (!IO.outputting())
1521       Section.reset(new ELFYAML::ARMIndexTableSection());
1522     sectionMapping(IO, *cast<ELFYAML::ARMIndexTableSection>(Section.get()));
1523     return;
1524   }
1525 
1526   switch (Type) {
1527   case ELF::SHT_DYNAMIC:
1528     if (!IO.outputting())
1529       Section.reset(new ELFYAML::DynamicSection());
1530     sectionMapping(IO, *cast<ELFYAML::DynamicSection>(Section.get()));
1531     break;
1532   case ELF::SHT_REL:
1533   case ELF::SHT_RELA:
1534     if (!IO.outputting())
1535       Section.reset(new ELFYAML::RelocationSection());
1536     sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get()));
1537     break;
1538   case ELF::SHT_RELR:
1539     if (!IO.outputting())
1540       Section.reset(new ELFYAML::RelrSection());
1541     sectionMapping(IO, *cast<ELFYAML::RelrSection>(Section.get()));
1542     break;
1543   case ELF::SHT_GROUP:
1544     if (!IO.outputting())
1545       Section.reset(new ELFYAML::GroupSection());
1546     groupSectionMapping(IO, *cast<ELFYAML::GroupSection>(Section.get()));
1547     break;
1548   case ELF::SHT_NOBITS:
1549     if (!IO.outputting())
1550       Section.reset(new ELFYAML::NoBitsSection());
1551     sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get()));
1552     break;
1553   case ELF::SHT_HASH:
1554     if (!IO.outputting())
1555       Section.reset(new ELFYAML::HashSection());
1556     sectionMapping(IO, *cast<ELFYAML::HashSection>(Section.get()));
1557     break;
1558   case ELF::SHT_NOTE:
1559     if (!IO.outputting())
1560       Section.reset(new ELFYAML::NoteSection());
1561     sectionMapping(IO, *cast<ELFYAML::NoteSection>(Section.get()));
1562     break;
1563  case ELF::SHT_GNU_HASH:
1564     if (!IO.outputting())
1565       Section.reset(new ELFYAML::GnuHashSection());
1566     sectionMapping(IO, *cast<ELFYAML::GnuHashSection>(Section.get()));
1567     break;
1568   case ELF::SHT_GNU_verdef:
1569     if (!IO.outputting())
1570       Section.reset(new ELFYAML::VerdefSection());
1571     sectionMapping(IO, *cast<ELFYAML::VerdefSection>(Section.get()));
1572     break;
1573   case ELF::SHT_GNU_versym:
1574     if (!IO.outputting())
1575       Section.reset(new ELFYAML::SymverSection());
1576     sectionMapping(IO, *cast<ELFYAML::SymverSection>(Section.get()));
1577     break;
1578   case ELF::SHT_GNU_verneed:
1579     if (!IO.outputting())
1580       Section.reset(new ELFYAML::VerneedSection());
1581     sectionMapping(IO, *cast<ELFYAML::VerneedSection>(Section.get()));
1582     break;
1583   case ELF::SHT_SYMTAB_SHNDX:
1584     if (!IO.outputting())
1585       Section.reset(new ELFYAML::SymtabShndxSection());
1586     sectionMapping(IO, *cast<ELFYAML::SymtabShndxSection>(Section.get()));
1587     break;
1588   case ELF::SHT_LLVM_ADDRSIG:
1589     if (!IO.outputting())
1590       Section.reset(new ELFYAML::AddrsigSection());
1591     sectionMapping(IO, *cast<ELFYAML::AddrsigSection>(Section.get()));
1592     break;
1593   case ELF::SHT_LLVM_LINKER_OPTIONS:
1594     if (!IO.outputting())
1595       Section.reset(new ELFYAML::LinkerOptionsSection());
1596     sectionMapping(IO, *cast<ELFYAML::LinkerOptionsSection>(Section.get()));
1597     break;
1598   case ELF::SHT_LLVM_DEPENDENT_LIBRARIES:
1599     if (!IO.outputting())
1600       Section.reset(new ELFYAML::DependentLibrariesSection());
1601     sectionMapping(IO,
1602                    *cast<ELFYAML::DependentLibrariesSection>(Section.get()));
1603     break;
1604   case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
1605     if (!IO.outputting())
1606       Section.reset(new ELFYAML::CallGraphProfileSection());
1607     sectionMapping(IO, *cast<ELFYAML::CallGraphProfileSection>(Section.get()));
1608     break;
1609   case ELF::SHT_LLVM_BB_ADDR_MAP:
1610     if (!IO.outputting())
1611       Section.reset(new ELFYAML::BBAddrMapSection());
1612     sectionMapping(IO, *cast<ELFYAML::BBAddrMapSection>(Section.get()));
1613     break;
1614   default:
1615     if (!IO.outputting()) {
1616       StringRef Name;
1617       IO.mapOptional("Name", Name, StringRef());
1618       Name = ELFYAML::dropUniqueSuffix(Name);
1619 
1620       if (ELFYAML::StackSizesSection::nameMatches(Name))
1621         Section = std::make_unique<ELFYAML::StackSizesSection>();
1622       else
1623         Section = std::make_unique<ELFYAML::RawContentSection>();
1624     }
1625 
1626     if (auto S = dyn_cast<ELFYAML::RawContentSection>(Section.get()))
1627       sectionMapping(IO, *S);
1628     else
1629       sectionMapping(IO, *cast<ELFYAML::StackSizesSection>(Section.get()));
1630   }
1631 }
1632 
1633 std::string MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::validate(
1634     IO &io, std::unique_ptr<ELFYAML::Chunk> &C) {
1635   if (const auto *F = dyn_cast<ELFYAML::Fill>(C.get())) {
1636     if (F->Pattern && F->Pattern->binary_size() != 0 && !F->Size)
1637       return "\"Size\" can't be 0 when \"Pattern\" is not empty";
1638     return "";
1639   }
1640 
1641   if (const auto *SHT = dyn_cast<ELFYAML::SectionHeaderTable>(C.get())) {
1642     if (SHT->NoHeaders && (SHT->Sections || SHT->Excluded || SHT->Offset))
1643       return "NoHeaders can't be used together with Offset/Sections/Excluded";
1644     return "";
1645   }
1646 
1647   const ELFYAML::Section &Sec = *cast<ELFYAML::Section>(C.get());
1648   if (Sec.Size && Sec.Content &&
1649       (uint64_t)(*Sec.Size) < Sec.Content->binary_size())
1650     return "Section size must be greater than or equal to the content size";
1651 
1652   auto BuildErrPrefix = [](ArrayRef<std::pair<StringRef, bool>> EntV) {
1653     std::string Msg;
1654     for (size_t I = 0, E = EntV.size(); I != E; ++I) {
1655       StringRef Name = EntV[I].first;
1656       if (I == 0) {
1657         Msg = "\"" + Name.str() + "\"";
1658         continue;
1659       }
1660       if (I != EntV.size() - 1)
1661         Msg += ", \"" + Name.str() + "\"";
1662       else
1663         Msg += " and \"" + Name.str() + "\"";
1664     }
1665     return Msg;
1666   };
1667 
1668   std::vector<std::pair<StringRef, bool>> Entries = Sec.getEntries();
1669   const size_t NumUsedEntries = llvm::count_if(
1670       Entries, [](const std::pair<StringRef, bool> &P) { return P.second; });
1671 
1672   if ((Sec.Size || Sec.Content) && NumUsedEntries > 0)
1673     return BuildErrPrefix(Entries) +
1674            " cannot be used with \"Content\" or \"Size\"";
1675 
1676   if (NumUsedEntries > 0 && Entries.size() != NumUsedEntries)
1677     return BuildErrPrefix(Entries) + " must be used together";
1678 
1679   if (const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(C.get())) {
1680     if (RawSection->Flags && RawSection->ShFlags)
1681       return "ShFlags and Flags cannot be used together";
1682     return "";
1683   }
1684 
1685   if (const auto *NB = dyn_cast<ELFYAML::NoBitsSection>(C.get())) {
1686     if (NB->Content)
1687       return "SHT_NOBITS section cannot have \"Content\"";
1688     return "";
1689   }
1690 
1691   if (const auto *MF = dyn_cast<ELFYAML::MipsABIFlags>(C.get())) {
1692     if (MF->Content)
1693       return "\"Content\" key is not implemented for SHT_MIPS_ABIFLAGS "
1694              "sections";
1695     if (MF->Size)
1696       return "\"Size\" key is not implemented for SHT_MIPS_ABIFLAGS sections";
1697     return "";
1698   }
1699 
1700   return "";
1701 }
1702 
1703 namespace {
1704 
1705 struct NormalizedMips64RelType {
1706   NormalizedMips64RelType(IO &)
1707       : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1708         Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1709         Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1710         SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {}
1711   NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original)
1712       : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF),
1713         Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {}
1714 
1715   ELFYAML::ELF_REL denormalize(IO &) {
1716     ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24;
1717     return Res;
1718   }
1719 
1720   ELFYAML::ELF_REL Type;
1721   ELFYAML::ELF_REL Type2;
1722   ELFYAML::ELF_REL Type3;
1723   ELFYAML::ELF_RSS SpecSym;
1724 };
1725 
1726 } // end anonymous namespace
1727 
1728 void MappingTraits<ELFYAML::StackSizeEntry>::mapping(
1729     IO &IO, ELFYAML::StackSizeEntry &E) {
1730   assert(IO.getContext() && "The IO context is not initialized");
1731   IO.mapOptional("Address", E.Address, Hex64(0));
1732   IO.mapRequired("Size", E.Size);
1733 }
1734 
1735 void MappingTraits<ELFYAML::BBAddrMapEntry>::mapping(
1736     IO &IO, ELFYAML::BBAddrMapEntry &E) {
1737   assert(IO.getContext() && "The IO context is not initialized");
1738   IO.mapOptional("Address", E.Address, Hex64(0));
1739   IO.mapOptional("NumBlocks", E.NumBlocks);
1740   IO.mapOptional("BBEntries", E.BBEntries);
1741 }
1742 
1743 void MappingTraits<ELFYAML::BBAddrMapEntry::BBEntry>::mapping(
1744     IO &IO, ELFYAML::BBAddrMapEntry::BBEntry &E) {
1745   assert(IO.getContext() && "The IO context is not initialized");
1746   IO.mapRequired("AddressOffset", E.AddressOffset);
1747   IO.mapRequired("Size", E.Size);
1748   IO.mapRequired("Metadata", E.Metadata);
1749 }
1750 
1751 void MappingTraits<ELFYAML::GnuHashHeader>::mapping(IO &IO,
1752                                                     ELFYAML::GnuHashHeader &E) {
1753   assert(IO.getContext() && "The IO context is not initialized");
1754   IO.mapOptional("NBuckets", E.NBuckets);
1755   IO.mapRequired("SymNdx", E.SymNdx);
1756   IO.mapOptional("MaskWords", E.MaskWords);
1757   IO.mapRequired("Shift2", E.Shift2);
1758 }
1759 
1760 void MappingTraits<ELFYAML::DynamicEntry>::mapping(IO &IO,
1761                                                    ELFYAML::DynamicEntry &Rel) {
1762   assert(IO.getContext() && "The IO context is not initialized");
1763 
1764   IO.mapRequired("Tag", Rel.Tag);
1765   IO.mapRequired("Value", Rel.Val);
1766 }
1767 
1768 void MappingTraits<ELFYAML::NoteEntry>::mapping(IO &IO, ELFYAML::NoteEntry &N) {
1769   assert(IO.getContext() && "The IO context is not initialized");
1770 
1771   IO.mapOptional("Name", N.Name);
1772   IO.mapOptional("Desc", N.Desc);
1773   IO.mapRequired("Type", N.Type);
1774 }
1775 
1776 void MappingTraits<ELFYAML::VerdefEntry>::mapping(IO &IO,
1777                                                   ELFYAML::VerdefEntry &E) {
1778   assert(IO.getContext() && "The IO context is not initialized");
1779 
1780   IO.mapOptional("Version", E.Version);
1781   IO.mapOptional("Flags", E.Flags);
1782   IO.mapOptional("VersionNdx", E.VersionNdx);
1783   IO.mapOptional("Hash", E.Hash);
1784   IO.mapRequired("Names", E.VerNames);
1785 }
1786 
1787 void MappingTraits<ELFYAML::VerneedEntry>::mapping(IO &IO,
1788                                                    ELFYAML::VerneedEntry &E) {
1789   assert(IO.getContext() && "The IO context is not initialized");
1790 
1791   IO.mapRequired("Version", E.Version);
1792   IO.mapRequired("File", E.File);
1793   IO.mapRequired("Entries", E.AuxV);
1794 }
1795 
1796 void MappingTraits<ELFYAML::VernauxEntry>::mapping(IO &IO,
1797                                                    ELFYAML::VernauxEntry &E) {
1798   assert(IO.getContext() && "The IO context is not initialized");
1799 
1800   IO.mapRequired("Name", E.Name);
1801   IO.mapRequired("Hash", E.Hash);
1802   IO.mapRequired("Flags", E.Flags);
1803   IO.mapRequired("Other", E.Other);
1804 }
1805 
1806 void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
1807                                                  ELFYAML::Relocation &Rel) {
1808   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
1809   assert(Object && "The IO context is not initialized");
1810 
1811   IO.mapOptional("Offset", Rel.Offset, (Hex64)0);
1812   IO.mapOptional("Symbol", Rel.Symbol);
1813 
1814   if (Object->getMachine() == ELFYAML::ELF_EM(ELF::EM_MIPS) &&
1815       Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
1816     MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key(
1817         IO, Rel.Type);
1818     IO.mapRequired("Type", Key->Type);
1819     IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
1820     IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
1821     IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF));
1822   } else
1823     IO.mapRequired("Type", Rel.Type);
1824 
1825   IO.mapOptional("Addend", Rel.Addend, (ELFYAML::YAMLIntUInt)0);
1826 }
1827 
1828 void MappingTraits<ELFYAML::ARMIndexTableEntry>::mapping(
1829     IO &IO, ELFYAML::ARMIndexTableEntry &E) {
1830   assert(IO.getContext() && "The IO context is not initialized");
1831   IO.mapRequired("Offset", E.Offset);
1832 
1833   StringRef CantUnwind = "EXIDX_CANTUNWIND";
1834   if (IO.outputting() && (uint32_t)E.Value == ARM::EHABI::EXIDX_CANTUNWIND)
1835     IO.mapRequired("Value", CantUnwind);
1836   else if (!IO.outputting() && getStringValue(IO, "Value") == CantUnwind)
1837     E.Value = ARM::EHABI::EXIDX_CANTUNWIND;
1838   else
1839     IO.mapRequired("Value", E.Value);
1840 }
1841 
1842 void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) {
1843   assert(!IO.getContext() && "The IO context is initialized already");
1844   IO.setContext(&Object);
1845   IO.mapTag("!ELF", true);
1846   IO.mapRequired("FileHeader", Object.Header);
1847   IO.mapOptional("ProgramHeaders", Object.ProgramHeaders);
1848   IO.mapOptional("Sections", Object.Chunks);
1849   IO.mapOptional("Symbols", Object.Symbols);
1850   IO.mapOptional("DynamicSymbols", Object.DynamicSymbols);
1851   IO.mapOptional("DWARF", Object.DWARF);
1852   if (Object.DWARF) {
1853     Object.DWARF->IsLittleEndian =
1854         Object.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1855     Object.DWARF->Is64BitAddrSize =
1856         Object.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
1857   }
1858   IO.setContext(nullptr);
1859 }
1860 
1861 void MappingTraits<ELFYAML::LinkerOption>::mapping(IO &IO,
1862                                                    ELFYAML::LinkerOption &Opt) {
1863   assert(IO.getContext() && "The IO context is not initialized");
1864   IO.mapRequired("Name", Opt.Key);
1865   IO.mapRequired("Value", Opt.Value);
1866 }
1867 
1868 void MappingTraits<ELFYAML::CallGraphEntryWeight>::mapping(
1869     IO &IO, ELFYAML::CallGraphEntryWeight &E) {
1870   assert(IO.getContext() && "The IO context is not initialized");
1871   IO.mapRequired("Weight", E.Weight);
1872 }
1873 
1874 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
1875 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
1876 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
1877 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
1878 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
1879 
1880 } // end namespace yaml
1881 
1882 } // end namespace llvm
1883