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