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