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