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