1 //===- ELFYAML.cpp - ELF YAMLIO implementation ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines classes for handling the YAML representation of ELF.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ObjectYAML/ELFYAML.h"
15 #include "llvm/Support/Casting.h"
16 #include "llvm/Support/MipsABIFlags.h"
17 
18 namespace llvm {
19 
~Section()20 ELFYAML::Section::~Section() {}
21 
22 namespace yaml {
23 
24 void
enumeration(IO & IO,ELFYAML::ELF_ET & Value)25 ScalarEnumerationTraits<ELFYAML::ELF_ET>::enumeration(IO &IO,
26                                                       ELFYAML::ELF_ET &Value) {
27 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
28   ECase(ET_NONE)
29   ECase(ET_REL)
30   ECase(ET_EXEC)
31   ECase(ET_DYN)
32   ECase(ET_CORE)
33 #undef ECase
34   IO.enumFallback<Hex16>(Value);
35 }
36 
37 void
enumeration(IO & IO,ELFYAML::ELF_EM & Value)38 ScalarEnumerationTraits<ELFYAML::ELF_EM>::enumeration(IO &IO,
39                                                       ELFYAML::ELF_EM &Value) {
40 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
41   ECase(EM_NONE)
42   ECase(EM_M32)
43   ECase(EM_SPARC)
44   ECase(EM_386)
45   ECase(EM_68K)
46   ECase(EM_88K)
47   ECase(EM_IAMCU)
48   ECase(EM_860)
49   ECase(EM_MIPS)
50   ECase(EM_S370)
51   ECase(EM_MIPS_RS3_LE)
52   ECase(EM_PARISC)
53   ECase(EM_VPP500)
54   ECase(EM_SPARC32PLUS)
55   ECase(EM_960)
56   ECase(EM_PPC)
57   ECase(EM_PPC64)
58   ECase(EM_S390)
59   ECase(EM_SPU)
60   ECase(EM_V800)
61   ECase(EM_FR20)
62   ECase(EM_RH32)
63   ECase(EM_RCE)
64   ECase(EM_ARM)
65   ECase(EM_ALPHA)
66   ECase(EM_SH)
67   ECase(EM_SPARCV9)
68   ECase(EM_TRICORE)
69   ECase(EM_ARC)
70   ECase(EM_H8_300)
71   ECase(EM_H8_300H)
72   ECase(EM_H8S)
73   ECase(EM_H8_500)
74   ECase(EM_IA_64)
75   ECase(EM_MIPS_X)
76   ECase(EM_COLDFIRE)
77   ECase(EM_68HC12)
78   ECase(EM_MMA)
79   ECase(EM_PCP)
80   ECase(EM_NCPU)
81   ECase(EM_NDR1)
82   ECase(EM_STARCORE)
83   ECase(EM_ME16)
84   ECase(EM_ST100)
85   ECase(EM_TINYJ)
86   ECase(EM_X86_64)
87   ECase(EM_PDSP)
88   ECase(EM_PDP10)
89   ECase(EM_PDP11)
90   ECase(EM_FX66)
91   ECase(EM_ST9PLUS)
92   ECase(EM_ST7)
93   ECase(EM_68HC16)
94   ECase(EM_68HC11)
95   ECase(EM_68HC08)
96   ECase(EM_68HC05)
97   ECase(EM_SVX)
98   ECase(EM_ST19)
99   ECase(EM_VAX)
100   ECase(EM_CRIS)
101   ECase(EM_JAVELIN)
102   ECase(EM_FIREPATH)
103   ECase(EM_ZSP)
104   ECase(EM_MMIX)
105   ECase(EM_HUANY)
106   ECase(EM_PRISM)
107   ECase(EM_AVR)
108   ECase(EM_FR30)
109   ECase(EM_D10V)
110   ECase(EM_D30V)
111   ECase(EM_V850)
112   ECase(EM_M32R)
113   ECase(EM_MN10300)
114   ECase(EM_MN10200)
115   ECase(EM_PJ)
116   ECase(EM_OPENRISC)
117   ECase(EM_ARC_COMPACT)
118   ECase(EM_XTENSA)
119   ECase(EM_VIDEOCORE)
120   ECase(EM_TMM_GPP)
121   ECase(EM_NS32K)
122   ECase(EM_TPC)
123   ECase(EM_SNP1K)
124   ECase(EM_ST200)
125   ECase(EM_IP2K)
126   ECase(EM_MAX)
127   ECase(EM_CR)
128   ECase(EM_F2MC16)
129   ECase(EM_MSP430)
130   ECase(EM_BLACKFIN)
131   ECase(EM_SE_C33)
132   ECase(EM_SEP)
133   ECase(EM_ARCA)
134   ECase(EM_UNICORE)
135   ECase(EM_EXCESS)
136   ECase(EM_DXP)
137   ECase(EM_ALTERA_NIOS2)
138   ECase(EM_CRX)
139   ECase(EM_XGATE)
140   ECase(EM_C166)
141   ECase(EM_M16C)
142   ECase(EM_DSPIC30F)
143   ECase(EM_CE)
144   ECase(EM_M32C)
145   ECase(EM_TSK3000)
146   ECase(EM_RS08)
147   ECase(EM_SHARC)
148   ECase(EM_ECOG2)
149   ECase(EM_SCORE7)
150   ECase(EM_DSP24)
151   ECase(EM_VIDEOCORE3)
152   ECase(EM_LATTICEMICO32)
153   ECase(EM_SE_C17)
154   ECase(EM_TI_C6000)
155   ECase(EM_TI_C2000)
156   ECase(EM_TI_C5500)
157   ECase(EM_MMDSP_PLUS)
158   ECase(EM_CYPRESS_M8C)
159   ECase(EM_R32C)
160   ECase(EM_TRIMEDIA)
161   ECase(EM_HEXAGON)
162   ECase(EM_8051)
163   ECase(EM_STXP7X)
164   ECase(EM_NDS32)
165   ECase(EM_ECOG1)
166   ECase(EM_ECOG1X)
167   ECase(EM_MAXQ30)
168   ECase(EM_XIMO16)
169   ECase(EM_MANIK)
170   ECase(EM_CRAYNV2)
171   ECase(EM_RX)
172   ECase(EM_METAG)
173   ECase(EM_MCST_ELBRUS)
174   ECase(EM_ECOG16)
175   ECase(EM_CR16)
176   ECase(EM_ETPU)
177   ECase(EM_SLE9X)
178   ECase(EM_L10M)
179   ECase(EM_K10M)
180   ECase(EM_AARCH64)
181   ECase(EM_AVR32)
182   ECase(EM_STM8)
183   ECase(EM_TILE64)
184   ECase(EM_TILEPRO)
185   ECase(EM_CUDA)
186   ECase(EM_TILEGX)
187   ECase(EM_CLOUDSHIELD)
188   ECase(EM_COREA_1ST)
189   ECase(EM_COREA_2ND)
190   ECase(EM_ARC_COMPACT2)
191   ECase(EM_OPEN8)
192   ECase(EM_RL78)
193   ECase(EM_VIDEOCORE5)
194   ECase(EM_78KOR)
195   ECase(EM_56800EX)
196   ECase(EM_AMDGPU)
197   ECase(EM_LANAI)
198 #undef ECase
199 }
200 
enumeration(IO & IO,ELFYAML::ELF_ELFCLASS & Value)201 void ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS>::enumeration(
202     IO &IO, ELFYAML::ELF_ELFCLASS &Value) {
203 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
204   // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it
205   // here.
206   ECase(ELFCLASS32)
207   ECase(ELFCLASS64)
208 #undef ECase
209 }
210 
enumeration(IO & IO,ELFYAML::ELF_ELFDATA & Value)211 void ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA>::enumeration(
212     IO &IO, ELFYAML::ELF_ELFDATA &Value) {
213 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
214   // Since the semantics of ELFDATANONE is "invalid", just don't accept it
215   // here.
216   ECase(ELFDATA2LSB)
217   ECase(ELFDATA2MSB)
218 #undef ECase
219 }
220 
enumeration(IO & IO,ELFYAML::ELF_ELFOSABI & Value)221 void ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI>::enumeration(
222     IO &IO, ELFYAML::ELF_ELFOSABI &Value) {
223 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
224   ECase(ELFOSABI_NONE)
225   ECase(ELFOSABI_HPUX)
226   ECase(ELFOSABI_NETBSD)
227   ECase(ELFOSABI_GNU)
228   ECase(ELFOSABI_GNU)
229   ECase(ELFOSABI_HURD)
230   ECase(ELFOSABI_SOLARIS)
231   ECase(ELFOSABI_AIX)
232   ECase(ELFOSABI_IRIX)
233   ECase(ELFOSABI_FREEBSD)
234   ECase(ELFOSABI_TRU64)
235   ECase(ELFOSABI_MODESTO)
236   ECase(ELFOSABI_OPENBSD)
237   ECase(ELFOSABI_OPENVMS)
238   ECase(ELFOSABI_NSK)
239   ECase(ELFOSABI_AROS)
240   ECase(ELFOSABI_FENIXOS)
241   ECase(ELFOSABI_CLOUDABI)
242   ECase(ELFOSABI_C6000_ELFABI)
243   ECase(ELFOSABI_AMDGPU_HSA)
244   ECase(ELFOSABI_C6000_LINUX)
245   ECase(ELFOSABI_ARM)
246   ECase(ELFOSABI_STANDALONE)
247 #undef ECase
248 }
249 
bitset(IO & IO,ELFYAML::ELF_EF & Value)250 void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO,
251                                                  ELFYAML::ELF_EF &Value) {
252   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
253   assert(Object && "The IO context is not initialized");
254 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X);
255 #define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M);
256   switch (Object->Header.Machine) {
257   case ELF::EM_ARM:
258     BCase(EF_ARM_SOFT_FLOAT)
259     BCase(EF_ARM_VFP_FLOAT)
260     BCaseMask(EF_ARM_EABI_UNKNOWN, EF_ARM_EABIMASK)
261     BCaseMask(EF_ARM_EABI_VER1, EF_ARM_EABIMASK)
262     BCaseMask(EF_ARM_EABI_VER2, EF_ARM_EABIMASK)
263     BCaseMask(EF_ARM_EABI_VER3, EF_ARM_EABIMASK)
264     BCaseMask(EF_ARM_EABI_VER4, EF_ARM_EABIMASK)
265     BCaseMask(EF_ARM_EABI_VER5, EF_ARM_EABIMASK)
266     break;
267   case ELF::EM_MIPS:
268     BCase(EF_MIPS_NOREORDER)
269     BCase(EF_MIPS_PIC)
270     BCase(EF_MIPS_CPIC)
271     BCase(EF_MIPS_ABI2)
272     BCase(EF_MIPS_32BITMODE)
273     BCase(EF_MIPS_FP64)
274     BCase(EF_MIPS_NAN2008)
275     BCase(EF_MIPS_MICROMIPS)
276     BCase(EF_MIPS_ARCH_ASE_M16)
277     BCase(EF_MIPS_ARCH_ASE_MDMX)
278     BCaseMask(EF_MIPS_ABI_O32, EF_MIPS_ABI)
279     BCaseMask(EF_MIPS_ABI_O64, EF_MIPS_ABI)
280     BCaseMask(EF_MIPS_ABI_EABI32, EF_MIPS_ABI)
281     BCaseMask(EF_MIPS_ABI_EABI64, EF_MIPS_ABI)
282     BCaseMask(EF_MIPS_MACH_3900, EF_MIPS_MACH)
283     BCaseMask(EF_MIPS_MACH_4010, EF_MIPS_MACH)
284     BCaseMask(EF_MIPS_MACH_4100, EF_MIPS_MACH)
285     BCaseMask(EF_MIPS_MACH_4650, EF_MIPS_MACH)
286     BCaseMask(EF_MIPS_MACH_4120, EF_MIPS_MACH)
287     BCaseMask(EF_MIPS_MACH_4111, EF_MIPS_MACH)
288     BCaseMask(EF_MIPS_MACH_SB1, EF_MIPS_MACH)
289     BCaseMask(EF_MIPS_MACH_OCTEON, EF_MIPS_MACH)
290     BCaseMask(EF_MIPS_MACH_XLR, EF_MIPS_MACH)
291     BCaseMask(EF_MIPS_MACH_OCTEON2, EF_MIPS_MACH)
292     BCaseMask(EF_MIPS_MACH_OCTEON3, EF_MIPS_MACH)
293     BCaseMask(EF_MIPS_MACH_5400, EF_MIPS_MACH)
294     BCaseMask(EF_MIPS_MACH_5900, EF_MIPS_MACH)
295     BCaseMask(EF_MIPS_MACH_5500, EF_MIPS_MACH)
296     BCaseMask(EF_MIPS_MACH_9000, EF_MIPS_MACH)
297     BCaseMask(EF_MIPS_MACH_LS2E, EF_MIPS_MACH)
298     BCaseMask(EF_MIPS_MACH_LS2F, EF_MIPS_MACH)
299     BCaseMask(EF_MIPS_MACH_LS3A, EF_MIPS_MACH)
300     BCaseMask(EF_MIPS_ARCH_1, EF_MIPS_ARCH)
301     BCaseMask(EF_MIPS_ARCH_2, EF_MIPS_ARCH)
302     BCaseMask(EF_MIPS_ARCH_3, EF_MIPS_ARCH)
303     BCaseMask(EF_MIPS_ARCH_4, EF_MIPS_ARCH)
304     BCaseMask(EF_MIPS_ARCH_5, EF_MIPS_ARCH)
305     BCaseMask(EF_MIPS_ARCH_32, EF_MIPS_ARCH)
306     BCaseMask(EF_MIPS_ARCH_64, EF_MIPS_ARCH)
307     BCaseMask(EF_MIPS_ARCH_32R2, EF_MIPS_ARCH)
308     BCaseMask(EF_MIPS_ARCH_64R2, EF_MIPS_ARCH)
309     BCaseMask(EF_MIPS_ARCH_32R6, EF_MIPS_ARCH)
310     BCaseMask(EF_MIPS_ARCH_64R6, EF_MIPS_ARCH)
311     break;
312   case ELF::EM_HEXAGON:
313     BCase(EF_HEXAGON_MACH_V2)
314     BCase(EF_HEXAGON_MACH_V3)
315     BCase(EF_HEXAGON_MACH_V4)
316     BCase(EF_HEXAGON_MACH_V5)
317     BCase(EF_HEXAGON_ISA_V2)
318     BCase(EF_HEXAGON_ISA_V3)
319     BCase(EF_HEXAGON_ISA_V4)
320     BCase(EF_HEXAGON_ISA_V5)
321     break;
322   case ELF::EM_AVR:
323     BCase(EF_AVR_ARCH_AVR1)
324     BCase(EF_AVR_ARCH_AVR2)
325     BCase(EF_AVR_ARCH_AVR25)
326     BCase(EF_AVR_ARCH_AVR3)
327     BCase(EF_AVR_ARCH_AVR31)
328     BCase(EF_AVR_ARCH_AVR35)
329     BCase(EF_AVR_ARCH_AVR4)
330     BCase(EF_AVR_ARCH_AVR51)
331     BCase(EF_AVR_ARCH_AVR6)
332     BCase(EF_AVR_ARCH_AVRTINY)
333     BCase(EF_AVR_ARCH_XMEGA1)
334     BCase(EF_AVR_ARCH_XMEGA2)
335     BCase(EF_AVR_ARCH_XMEGA3)
336     BCase(EF_AVR_ARCH_XMEGA4)
337     BCase(EF_AVR_ARCH_XMEGA5)
338     BCase(EF_AVR_ARCH_XMEGA6)
339     BCase(EF_AVR_ARCH_XMEGA7)
340     break;
341   case ELF::EM_AMDGPU:
342   case ELF::EM_X86_64:
343     break;
344   default:
345     llvm_unreachable("Unsupported architecture");
346   }
347 #undef BCase
348 #undef BCaseMask
349 }
350 
enumeration(IO & IO,ELFYAML::ELF_SHT & Value)351 void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
352     IO &IO, ELFYAML::ELF_SHT &Value) {
353   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
354   assert(Object && "The IO context is not initialized");
355 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
356   ECase(SHT_NULL)
357   ECase(SHT_PROGBITS)
358   // No SHT_SYMTAB. Use the top-level `Symbols` key instead.
359   // FIXME: Issue a diagnostic with this information.
360   ECase(SHT_STRTAB)
361   ECase(SHT_RELA)
362   ECase(SHT_HASH)
363   ECase(SHT_DYNAMIC)
364   ECase(SHT_NOTE)
365   ECase(SHT_NOBITS)
366   ECase(SHT_REL)
367   ECase(SHT_SHLIB)
368   ECase(SHT_DYNSYM)
369   ECase(SHT_INIT_ARRAY)
370   ECase(SHT_FINI_ARRAY)
371   ECase(SHT_PREINIT_ARRAY)
372   ECase(SHT_GROUP)
373   ECase(SHT_SYMTAB_SHNDX)
374   ECase(SHT_LOOS)
375   ECase(SHT_GNU_ATTRIBUTES)
376   ECase(SHT_GNU_HASH)
377   ECase(SHT_GNU_verdef)
378   ECase(SHT_GNU_verneed)
379   ECase(SHT_GNU_versym)
380   ECase(SHT_HIOS)
381   ECase(SHT_LOPROC)
382   switch (Object->Header.Machine) {
383   case ELF::EM_ARM:
384     ECase(SHT_ARM_EXIDX)
385     ECase(SHT_ARM_PREEMPTMAP)
386     ECase(SHT_ARM_ATTRIBUTES)
387     ECase(SHT_ARM_DEBUGOVERLAY)
388     ECase(SHT_ARM_OVERLAYSECTION)
389     break;
390   case ELF::EM_HEXAGON:
391     ECase(SHT_HEX_ORDERED)
392     break;
393   case ELF::EM_X86_64:
394     ECase(SHT_X86_64_UNWIND)
395     break;
396   case ELF::EM_MIPS:
397     ECase(SHT_MIPS_REGINFO)
398     ECase(SHT_MIPS_OPTIONS)
399     ECase(SHT_MIPS_ABIFLAGS)
400     break;
401   default:
402     // Nothing to do.
403     break;
404   }
405 #undef ECase
406 }
407 
bitset(IO & IO,ELFYAML::ELF_SHF & Value)408 void ScalarBitSetTraits<ELFYAML::ELF_SHF>::bitset(IO &IO,
409                                                   ELFYAML::ELF_SHF &Value) {
410   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
411 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X);
412   BCase(SHF_WRITE)
413   BCase(SHF_ALLOC)
414   BCase(SHF_EXCLUDE)
415   BCase(SHF_EXECINSTR)
416   BCase(SHF_MERGE)
417   BCase(SHF_STRINGS)
418   BCase(SHF_INFO_LINK)
419   BCase(SHF_LINK_ORDER)
420   BCase(SHF_OS_NONCONFORMING)
421   BCase(SHF_GROUP)
422   BCase(SHF_TLS)
423   switch(Object->Header.Machine) {
424   case ELF::EM_AMDGPU:
425     BCase(SHF_AMDGPU_HSA_GLOBAL)
426     BCase(SHF_AMDGPU_HSA_READONLY)
427     BCase(SHF_AMDGPU_HSA_CODE)
428     BCase(SHF_AMDGPU_HSA_AGENT)
429     break;
430   case ELF::EM_HEXAGON:
431     BCase(SHF_HEX_GPREL)
432     break;
433   case ELF::EM_MIPS:
434     BCase(SHF_MIPS_NODUPES)
435     BCase(SHF_MIPS_NAMES)
436     BCase(SHF_MIPS_LOCAL)
437     BCase(SHF_MIPS_NOSTRIP)
438     BCase(SHF_MIPS_GPREL)
439     BCase(SHF_MIPS_MERGE)
440     BCase(SHF_MIPS_ADDR)
441     BCase(SHF_MIPS_STRING)
442     break;
443   case ELF::EM_X86_64:
444     BCase(SHF_X86_64_LARGE)
445     break;
446   default:
447     // Nothing to do.
448     break;
449   }
450 #undef BCase
451 }
452 
enumeration(IO & IO,ELFYAML::ELF_STT & Value)453 void ScalarEnumerationTraits<ELFYAML::ELF_STT>::enumeration(
454     IO &IO, ELFYAML::ELF_STT &Value) {
455 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
456   ECase(STT_NOTYPE)
457   ECase(STT_OBJECT)
458   ECase(STT_FUNC)
459   ECase(STT_SECTION)
460   ECase(STT_FILE)
461   ECase(STT_COMMON)
462   ECase(STT_TLS)
463   ECase(STT_GNU_IFUNC)
464 #undef ECase
465 }
466 
enumeration(IO & IO,ELFYAML::ELF_STV & Value)467 void ScalarEnumerationTraits<ELFYAML::ELF_STV>::enumeration(
468     IO &IO, ELFYAML::ELF_STV &Value) {
469 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
470   ECase(STV_DEFAULT)
471   ECase(STV_INTERNAL)
472   ECase(STV_HIDDEN)
473   ECase(STV_PROTECTED)
474 #undef ECase
475 }
476 
bitset(IO & IO,ELFYAML::ELF_STO & Value)477 void ScalarBitSetTraits<ELFYAML::ELF_STO>::bitset(IO &IO,
478                                                   ELFYAML::ELF_STO &Value) {
479   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
480   assert(Object && "The IO context is not initialized");
481 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X);
482   switch (Object->Header.Machine) {
483   case ELF::EM_MIPS:
484     BCase(STO_MIPS_OPTIONAL)
485     BCase(STO_MIPS_PLT)
486     BCase(STO_MIPS_PIC)
487     BCase(STO_MIPS_MICROMIPS)
488     break;
489   default:
490     break; // Nothing to do
491   }
492 #undef BCase
493 #undef BCaseMask
494 }
495 
enumeration(IO & IO,ELFYAML::ELF_RSS & Value)496 void ScalarEnumerationTraits<ELFYAML::ELF_RSS>::enumeration(
497     IO &IO, ELFYAML::ELF_RSS &Value) {
498 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
499   ECase(RSS_UNDEF)
500   ECase(RSS_GP)
501   ECase(RSS_GP0)
502   ECase(RSS_LOC)
503 #undef ECase
504 }
505 
enumeration(IO & IO,ELFYAML::ELF_REL & Value)506 void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration(
507     IO &IO, ELFYAML::ELF_REL &Value) {
508   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
509   assert(Object && "The IO context is not initialized");
510 #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X);
511   switch (Object->Header.Machine) {
512   case ELF::EM_X86_64:
513 #include "llvm/Support/ELFRelocs/x86_64.def"
514     break;
515   case ELF::EM_MIPS:
516 #include "llvm/Support/ELFRelocs/Mips.def"
517     break;
518   case ELF::EM_HEXAGON:
519 #include "llvm/Support/ELFRelocs/Hexagon.def"
520     break;
521   case ELF::EM_386:
522   case ELF::EM_IAMCU:
523 #include "llvm/Support/ELFRelocs/i386.def"
524     break;
525   case ELF::EM_AARCH64:
526 #include "llvm/Support/ELFRelocs/AArch64.def"
527     break;
528   case ELF::EM_ARM:
529 #include "llvm/Support/ELFRelocs/ARM.def"
530     break;
531   case ELF::EM_LANAI:
532 #include "llvm/Support/ELFRelocs/Lanai.def"
533     break;
534   case ELF::EM_AMDGPU:
535 #include "llvm/Support/ELFRelocs/AMDGPU.def"
536     break;
537   default:
538     llvm_unreachable("Unsupported architecture");
539   }
540 #undef ELF_RELOC
541 }
542 
enumeration(IO & IO,ELFYAML::MIPS_AFL_REG & Value)543 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG>::enumeration(
544     IO &IO, ELFYAML::MIPS_AFL_REG &Value) {
545 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X);
546   ECase(REG_NONE)
547   ECase(REG_32)
548   ECase(REG_64)
549   ECase(REG_128)
550 #undef ECase
551 }
552 
enumeration(IO & IO,ELFYAML::MIPS_ABI_FP & Value)553 void ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP>::enumeration(
554     IO &IO, ELFYAML::MIPS_ABI_FP &Value) {
555 #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X);
556   ECase(FP_ANY)
557   ECase(FP_DOUBLE)
558   ECase(FP_SINGLE)
559   ECase(FP_SOFT)
560   ECase(FP_OLD_64)
561   ECase(FP_XX)
562   ECase(FP_64)
563   ECase(FP_64A)
564 #undef ECase
565 }
566 
enumeration(IO & IO,ELFYAML::MIPS_AFL_EXT & Value)567 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT>::enumeration(
568     IO &IO, ELFYAML::MIPS_AFL_EXT &Value) {
569 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X);
570   ECase(EXT_NONE)
571   ECase(EXT_XLR)
572   ECase(EXT_OCTEON2)
573   ECase(EXT_OCTEONP)
574   ECase(EXT_LOONGSON_3A)
575   ECase(EXT_OCTEON)
576   ECase(EXT_5900)
577   ECase(EXT_4650)
578   ECase(EXT_4010)
579   ECase(EXT_4100)
580   ECase(EXT_3900)
581   ECase(EXT_10000)
582   ECase(EXT_SB1)
583   ECase(EXT_4111)
584   ECase(EXT_4120)
585   ECase(EXT_5400)
586   ECase(EXT_5500)
587   ECase(EXT_LOONGSON_2E)
588   ECase(EXT_LOONGSON_2F)
589   ECase(EXT_OCTEON3)
590 #undef ECase
591 }
592 
enumeration(IO & IO,ELFYAML::MIPS_ISA & Value)593 void ScalarEnumerationTraits<ELFYAML::MIPS_ISA>::enumeration(
594     IO &IO, ELFYAML::MIPS_ISA &Value) {
595   IO.enumCase(Value, "MIPS1", 1);
596   IO.enumCase(Value, "MIPS2", 2);
597   IO.enumCase(Value, "MIPS3", 3);
598   IO.enumCase(Value, "MIPS4", 4);
599   IO.enumCase(Value, "MIPS5", 5);
600   IO.enumCase(Value, "MIPS32", 32);
601   IO.enumCase(Value, "MIPS64", 64);
602 }
603 
bitset(IO & IO,ELFYAML::MIPS_AFL_ASE & Value)604 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE>::bitset(
605     IO &IO, ELFYAML::MIPS_AFL_ASE &Value) {
606 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X);
607   BCase(DSP)
608   BCase(DSPR2)
609   BCase(EVA)
610   BCase(MCU)
611   BCase(MDMX)
612   BCase(MIPS3D)
613   BCase(MT)
614   BCase(SMARTMIPS)
615   BCase(VIRT)
616   BCase(MSA)
617   BCase(MIPS16)
618   BCase(MICROMIPS)
619   BCase(XPA)
620 #undef BCase
621 }
622 
bitset(IO & IO,ELFYAML::MIPS_AFL_FLAGS1 & Value)623 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1>::bitset(
624     IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) {
625 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X);
626   BCase(ODDSPREG)
627 #undef BCase
628 }
629 
mapping(IO & IO,ELFYAML::FileHeader & FileHdr)630 void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO,
631                                                  ELFYAML::FileHeader &FileHdr) {
632   IO.mapRequired("Class", FileHdr.Class);
633   IO.mapRequired("Data", FileHdr.Data);
634   IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0));
635   IO.mapRequired("Type", FileHdr.Type);
636   IO.mapRequired("Machine", FileHdr.Machine);
637   IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0));
638   IO.mapOptional("Entry", FileHdr.Entry, Hex64(0));
639 }
640 
641 namespace {
642 struct NormalizedOther {
NormalizedOtherllvm::yaml::__anonf79b33750111::NormalizedOther643   NormalizedOther(IO &)
644       : Visibility(ELFYAML::ELF_STV(0)), Other(ELFYAML::ELF_STO(0)) {}
NormalizedOtherllvm::yaml::__anonf79b33750111::NormalizedOther645   NormalizedOther(IO &, uint8_t Original)
646       : Visibility(Original & 0x3), Other(Original & ~0x3) {}
647 
denormalizellvm::yaml::__anonf79b33750111::NormalizedOther648   uint8_t denormalize(IO &) { return Visibility | Other; }
649 
650   ELFYAML::ELF_STV Visibility;
651   ELFYAML::ELF_STO Other;
652 };
653 }
654 
mapping(IO & IO,ELFYAML::Symbol & Symbol)655 void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) {
656   IO.mapOptional("Name", Symbol.Name, StringRef());
657   IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
658   IO.mapOptional("Section", Symbol.Section, StringRef());
659   IO.mapOptional("Value", Symbol.Value, Hex64(0));
660   IO.mapOptional("Size", Symbol.Size, Hex64(0));
661 
662   MappingNormalization<NormalizedOther, uint8_t> Keys(IO, Symbol.Other);
663   IO.mapOptional("Visibility", Keys->Visibility, ELFYAML::ELF_STV(0));
664   IO.mapOptional("Other", Keys->Other, ELFYAML::ELF_STO(0));
665 }
666 
mapping(IO & IO,ELFYAML::LocalGlobalWeakSymbols & Symbols)667 void MappingTraits<ELFYAML::LocalGlobalWeakSymbols>::mapping(
668     IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols) {
669   IO.mapOptional("Local", Symbols.Local);
670   IO.mapOptional("Global", Symbols.Global);
671   IO.mapOptional("Weak", Symbols.Weak);
672 }
673 
commonSectionMapping(IO & IO,ELFYAML::Section & Section)674 static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
675   IO.mapOptional("Name", Section.Name, StringRef());
676   IO.mapRequired("Type", Section.Type);
677   IO.mapOptional("Flags", Section.Flags, ELFYAML::ELF_SHF(0));
678   IO.mapOptional("Address", Section.Address, Hex64(0));
679   IO.mapOptional("Link", Section.Link, StringRef());
680   IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
681   IO.mapOptional("Info", Section.Info, StringRef());
682 }
683 
sectionMapping(IO & IO,ELFYAML::RawContentSection & Section)684 static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
685   commonSectionMapping(IO, Section);
686   IO.mapOptional("Content", Section.Content);
687   IO.mapOptional("Size", Section.Size, Hex64(Section.Content.binary_size()));
688 }
689 
sectionMapping(IO & IO,ELFYAML::NoBitsSection & Section)690 static void sectionMapping(IO &IO, ELFYAML::NoBitsSection &Section) {
691   commonSectionMapping(IO, Section);
692   IO.mapOptional("Size", Section.Size, Hex64(0));
693 }
694 
sectionMapping(IO & IO,ELFYAML::RelocationSection & Section)695 static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
696   commonSectionMapping(IO, Section);
697   IO.mapOptional("Relocations", Section.Relocations);
698 }
699 
groupSectionMapping(IO & IO,ELFYAML::Group & group)700 static void groupSectionMapping(IO &IO, ELFYAML::Group &group) {
701   commonSectionMapping(IO, group);
702   IO.mapRequired("Members", group.Members);
703 }
704 
mapping(IO & IO,ELFYAML::SectionOrType & sectionOrType)705 void MappingTraits<ELFYAML::SectionOrType>::mapping(
706     IO &IO, ELFYAML::SectionOrType &sectionOrType) {
707   IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType);
708 }
709 
sectionMapping(IO & IO,ELFYAML::MipsABIFlags & Section)710 static void sectionMapping(IO &IO, ELFYAML::MipsABIFlags &Section) {
711   commonSectionMapping(IO, Section);
712   IO.mapOptional("Version", Section.Version, Hex16(0));
713   IO.mapRequired("ISA", Section.ISALevel);
714   IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0));
715   IO.mapOptional("ISAExtension", Section.ISAExtension,
716                  ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE));
717   IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0));
718   IO.mapOptional("FpABI", Section.FpABI,
719                  ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY));
720   IO.mapOptional("GPRSize", Section.GPRSize,
721                  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
722   IO.mapOptional("CPR1Size", Section.CPR1Size,
723                  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
724   IO.mapOptional("CPR2Size", Section.CPR2Size,
725                  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
726   IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0));
727   IO.mapOptional("Flags2", Section.Flags2, Hex32(0));
728 }
729 
mapping(IO & IO,std::unique_ptr<ELFYAML::Section> & Section)730 void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping(
731     IO &IO, std::unique_ptr<ELFYAML::Section> &Section) {
732   ELFYAML::ELF_SHT sectionType;
733   if (IO.outputting())
734     sectionType = Section->Type;
735   else
736     IO.mapRequired("Type", sectionType);
737 
738   switch (sectionType) {
739   case ELF::SHT_REL:
740   case ELF::SHT_RELA:
741     if (!IO.outputting())
742       Section.reset(new ELFYAML::RelocationSection());
743     sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get()));
744     break;
745   case ELF::SHT_GROUP:
746     if (!IO.outputting())
747       Section.reset(new ELFYAML::Group());
748     groupSectionMapping(IO, *cast<ELFYAML::Group>(Section.get()));
749     break;
750   case ELF::SHT_NOBITS:
751     if (!IO.outputting())
752       Section.reset(new ELFYAML::NoBitsSection());
753     sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get()));
754     break;
755   case ELF::SHT_MIPS_ABIFLAGS:
756     if (!IO.outputting())
757       Section.reset(new ELFYAML::MipsABIFlags());
758     sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get()));
759     break;
760   default:
761     if (!IO.outputting())
762       Section.reset(new ELFYAML::RawContentSection());
763     sectionMapping(IO, *cast<ELFYAML::RawContentSection>(Section.get()));
764   }
765 }
766 
validate(IO & io,std::unique_ptr<ELFYAML::Section> & Section)767 StringRef MappingTraits<std::unique_ptr<ELFYAML::Section>>::validate(
768     IO &io, std::unique_ptr<ELFYAML::Section> &Section) {
769   const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(Section.get());
770   if (!RawSection || RawSection->Size >= RawSection->Content.binary_size())
771     return StringRef();
772   return "Section size must be greater or equal to the content size";
773 }
774 
775 namespace {
776 struct NormalizedMips64RelType {
NormalizedMips64RelTypellvm::yaml::__anonf79b33750211::NormalizedMips64RelType777   NormalizedMips64RelType(IO &)
778       : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
779         Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
780         Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
781         SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {}
NormalizedMips64RelTypellvm::yaml::__anonf79b33750211::NormalizedMips64RelType782   NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original)
783       : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF),
784         Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {}
785 
denormalizellvm::yaml::__anonf79b33750211::NormalizedMips64RelType786   ELFYAML::ELF_REL denormalize(IO &) {
787     ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24;
788     return Res;
789   }
790 
791   ELFYAML::ELF_REL Type;
792   ELFYAML::ELF_REL Type2;
793   ELFYAML::ELF_REL Type3;
794   ELFYAML::ELF_RSS SpecSym;
795 };
796 }
797 
mapping(IO & IO,ELFYAML::Relocation & Rel)798 void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
799                                                  ELFYAML::Relocation &Rel) {
800   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
801   assert(Object && "The IO context is not initialized");
802 
803   IO.mapRequired("Offset", Rel.Offset);
804   IO.mapRequired("Symbol", Rel.Symbol);
805 
806   if (Object->Header.Machine == ELFYAML::ELF_EM(ELF::EM_MIPS) &&
807       Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
808     MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key(
809         IO, Rel.Type);
810     IO.mapRequired("Type", Key->Type);
811     IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
812     IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
813     IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF));
814   } else
815     IO.mapRequired("Type", Rel.Type);
816 
817   IO.mapOptional("Addend", Rel.Addend, (int64_t)0);
818 }
819 
mapping(IO & IO,ELFYAML::Object & Object)820 void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) {
821   assert(!IO.getContext() && "The IO context is initialized already");
822   IO.setContext(&Object);
823   IO.mapTag("!ELF", true);
824   IO.mapRequired("FileHeader", Object.Header);
825   IO.mapOptional("Sections", Object.Sections);
826   IO.mapOptional("Symbols", Object.Symbols);
827   IO.setContext(nullptr);
828 }
829 
830 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
831 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
832 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
833 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
834 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
835 
836 } // end namespace yaml
837 } // end namespace llvm
838