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