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/Object/ELFYAML.h"
15 #include "llvm/Support/Casting.h"
16 
17 namespace llvm {
18 
~Section()19 ELFYAML::Section::~Section() {}
20 
21 namespace yaml {
22 
23 void
enumeration(IO & IO,ELFYAML::ELF_ET & Value)24 ScalarEnumerationTraits<ELFYAML::ELF_ET>::enumeration(IO &IO,
25                                                       ELFYAML::ELF_ET &Value) {
26 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
27   ECase(ET_NONE)
28   ECase(ET_REL)
29   ECase(ET_EXEC)
30   ECase(ET_DYN)
31   ECase(ET_CORE)
32 #undef ECase
33   IO.enumFallback<Hex16>(Value);
34 }
35 
36 void
enumeration(IO & IO,ELFYAML::ELF_EM & Value)37 ScalarEnumerationTraits<ELFYAML::ELF_EM>::enumeration(IO &IO,
38                                                       ELFYAML::ELF_EM &Value) {
39 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
40   ECase(EM_NONE)
41   ECase(EM_M32)
42   ECase(EM_SPARC)
43   ECase(EM_386)
44   ECase(EM_68K)
45   ECase(EM_88K)
46   ECase(EM_486)
47   ECase(EM_860)
48   ECase(EM_MIPS)
49   ECase(EM_S370)
50   ECase(EM_MIPS_RS3_LE)
51   ECase(EM_PARISC)
52   ECase(EM_VPP500)
53   ECase(EM_SPARC32PLUS)
54   ECase(EM_960)
55   ECase(EM_PPC)
56   ECase(EM_PPC64)
57   ECase(EM_S390)
58   ECase(EM_SPU)
59   ECase(EM_V800)
60   ECase(EM_FR20)
61   ECase(EM_RH32)
62   ECase(EM_RCE)
63   ECase(EM_ARM)
64   ECase(EM_ALPHA)
65   ECase(EM_SH)
66   ECase(EM_SPARCV9)
67   ECase(EM_TRICORE)
68   ECase(EM_ARC)
69   ECase(EM_H8_300)
70   ECase(EM_H8_300H)
71   ECase(EM_H8S)
72   ECase(EM_H8_500)
73   ECase(EM_IA_64)
74   ECase(EM_MIPS_X)
75   ECase(EM_COLDFIRE)
76   ECase(EM_68HC12)
77   ECase(EM_MMA)
78   ECase(EM_PCP)
79   ECase(EM_NCPU)
80   ECase(EM_NDR1)
81   ECase(EM_STARCORE)
82   ECase(EM_ME16)
83   ECase(EM_ST100)
84   ECase(EM_TINYJ)
85   ECase(EM_X86_64)
86   ECase(EM_PDSP)
87   ECase(EM_PDP10)
88   ECase(EM_PDP11)
89   ECase(EM_FX66)
90   ECase(EM_ST9PLUS)
91   ECase(EM_ST7)
92   ECase(EM_68HC16)
93   ECase(EM_68HC11)
94   ECase(EM_68HC08)
95   ECase(EM_68HC05)
96   ECase(EM_SVX)
97   ECase(EM_ST19)
98   ECase(EM_VAX)
99   ECase(EM_CRIS)
100   ECase(EM_JAVELIN)
101   ECase(EM_FIREPATH)
102   ECase(EM_ZSP)
103   ECase(EM_MMIX)
104   ECase(EM_HUANY)
105   ECase(EM_PRISM)
106   ECase(EM_AVR)
107   ECase(EM_FR30)
108   ECase(EM_D10V)
109   ECase(EM_D30V)
110   ECase(EM_V850)
111   ECase(EM_M32R)
112   ECase(EM_MN10300)
113   ECase(EM_MN10200)
114   ECase(EM_PJ)
115   ECase(EM_OPENRISC)
116   ECase(EM_ARC_COMPACT)
117   ECase(EM_XTENSA)
118   ECase(EM_VIDEOCORE)
119   ECase(EM_TMM_GPP)
120   ECase(EM_NS32K)
121   ECase(EM_TPC)
122   ECase(EM_SNP1K)
123   ECase(EM_ST200)
124   ECase(EM_IP2K)
125   ECase(EM_MAX)
126   ECase(EM_CR)
127   ECase(EM_F2MC16)
128   ECase(EM_MSP430)
129   ECase(EM_BLACKFIN)
130   ECase(EM_SE_C33)
131   ECase(EM_SEP)
132   ECase(EM_ARCA)
133   ECase(EM_UNICORE)
134   ECase(EM_EXCESS)
135   ECase(EM_DXP)
136   ECase(EM_ALTERA_NIOS2)
137   ECase(EM_CRX)
138   ECase(EM_XGATE)
139   ECase(EM_C166)
140   ECase(EM_M16C)
141   ECase(EM_DSPIC30F)
142   ECase(EM_CE)
143   ECase(EM_M32C)
144   ECase(EM_TSK3000)
145   ECase(EM_RS08)
146   ECase(EM_SHARC)
147   ECase(EM_ECOG2)
148   ECase(EM_SCORE7)
149   ECase(EM_DSP24)
150   ECase(EM_VIDEOCORE3)
151   ECase(EM_LATTICEMICO32)
152   ECase(EM_SE_C17)
153   ECase(EM_TI_C6000)
154   ECase(EM_TI_C2000)
155   ECase(EM_TI_C5500)
156   ECase(EM_MMDSP_PLUS)
157   ECase(EM_CYPRESS_M8C)
158   ECase(EM_R32C)
159   ECase(EM_TRIMEDIA)
160   ECase(EM_HEXAGON)
161   ECase(EM_8051)
162   ECase(EM_STXP7X)
163   ECase(EM_NDS32)
164   ECase(EM_ECOG1)
165   ECase(EM_ECOG1X)
166   ECase(EM_MAXQ30)
167   ECase(EM_XIMO16)
168   ECase(EM_MANIK)
169   ECase(EM_CRAYNV2)
170   ECase(EM_RX)
171   ECase(EM_METAG)
172   ECase(EM_MCST_ELBRUS)
173   ECase(EM_ECOG16)
174   ECase(EM_CR16)
175   ECase(EM_ETPU)
176   ECase(EM_SLE9X)
177   ECase(EM_L10M)
178   ECase(EM_K10M)
179   ECase(EM_AARCH64)
180   ECase(EM_AVR32)
181   ECase(EM_STM8)
182   ECase(EM_TILE64)
183   ECase(EM_TILEPRO)
184   ECase(EM_CUDA)
185   ECase(EM_TILEGX)
186   ECase(EM_CLOUDSHIELD)
187   ECase(EM_COREA_1ST)
188   ECase(EM_COREA_2ND)
189   ECase(EM_ARC_COMPACT2)
190   ECase(EM_OPEN8)
191   ECase(EM_RL78)
192   ECase(EM_VIDEOCORE5)
193   ECase(EM_78KOR)
194   ECase(EM_56800EX)
195 #undef ECase
196 }
197 
enumeration(IO & IO,ELFYAML::ELF_ELFCLASS & Value)198 void ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS>::enumeration(
199     IO &IO, ELFYAML::ELF_ELFCLASS &Value) {
200 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
201   // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it
202   // here.
203   ECase(ELFCLASS32)
204   ECase(ELFCLASS64)
205 #undef ECase
206 }
207 
enumeration(IO & IO,ELFYAML::ELF_ELFDATA & Value)208 void ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA>::enumeration(
209     IO &IO, ELFYAML::ELF_ELFDATA &Value) {
210 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
211   // Since the semantics of ELFDATANONE is "invalid", just don't accept it
212   // here.
213   ECase(ELFDATA2LSB)
214   ECase(ELFDATA2MSB)
215 #undef ECase
216 }
217 
enumeration(IO & IO,ELFYAML::ELF_ELFOSABI & Value)218 void ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI>::enumeration(
219     IO &IO, ELFYAML::ELF_ELFOSABI &Value) {
220 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
221   ECase(ELFOSABI_NONE)
222   ECase(ELFOSABI_HPUX)
223   ECase(ELFOSABI_NETBSD)
224   ECase(ELFOSABI_GNU)
225   ECase(ELFOSABI_GNU)
226   ECase(ELFOSABI_HURD)
227   ECase(ELFOSABI_SOLARIS)
228   ECase(ELFOSABI_AIX)
229   ECase(ELFOSABI_IRIX)
230   ECase(ELFOSABI_FREEBSD)
231   ECase(ELFOSABI_TRU64)
232   ECase(ELFOSABI_MODESTO)
233   ECase(ELFOSABI_OPENBSD)
234   ECase(ELFOSABI_OPENVMS)
235   ECase(ELFOSABI_NSK)
236   ECase(ELFOSABI_AROS)
237   ECase(ELFOSABI_FENIXOS)
238   ECase(ELFOSABI_CLOUDABI)
239   ECase(ELFOSABI_C6000_ELFABI)
240   ECase(ELFOSABI_C6000_LINUX)
241   ECase(ELFOSABI_ARM)
242   ECase(ELFOSABI_STANDALONE)
243 #undef ECase
244 }
245 
bitset(IO & IO,ELFYAML::ELF_EF & Value)246 void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO,
247                                                  ELFYAML::ELF_EF &Value) {
248   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
249   assert(Object && "The IO context is not initialized");
250 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X);
251 #define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M);
252   switch (Object->Header.Machine) {
253   case ELF::EM_ARM:
254     BCase(EF_ARM_SOFT_FLOAT)
255     BCase(EF_ARM_VFP_FLOAT)
256     BCaseMask(EF_ARM_EABI_UNKNOWN, EF_ARM_EABIMASK)
257     BCaseMask(EF_ARM_EABI_VER1, EF_ARM_EABIMASK)
258     BCaseMask(EF_ARM_EABI_VER2, EF_ARM_EABIMASK)
259     BCaseMask(EF_ARM_EABI_VER3, EF_ARM_EABIMASK)
260     BCaseMask(EF_ARM_EABI_VER4, EF_ARM_EABIMASK)
261     BCaseMask(EF_ARM_EABI_VER5, EF_ARM_EABIMASK)
262     break;
263   case ELF::EM_MIPS:
264     BCase(EF_MIPS_NOREORDER)
265     BCase(EF_MIPS_PIC)
266     BCase(EF_MIPS_CPIC)
267     BCase(EF_MIPS_ABI2)
268     BCase(EF_MIPS_32BITMODE)
269     BCase(EF_MIPS_NAN2008)
270     BCase(EF_MIPS_ABI_O32)
271     BCase(EF_MIPS_MICROMIPS)
272     BCase(EF_MIPS_ARCH_ASE_M16)
273     BCaseMask(EF_MIPS_ARCH_1, EF_MIPS_ARCH)
274     BCaseMask(EF_MIPS_ARCH_2, EF_MIPS_ARCH)
275     BCaseMask(EF_MIPS_ARCH_3, EF_MIPS_ARCH)
276     BCaseMask(EF_MIPS_ARCH_4, EF_MIPS_ARCH)
277     BCaseMask(EF_MIPS_ARCH_5, EF_MIPS_ARCH)
278     BCaseMask(EF_MIPS_ARCH_32, EF_MIPS_ARCH)
279     BCaseMask(EF_MIPS_ARCH_64, EF_MIPS_ARCH)
280     BCaseMask(EF_MIPS_ARCH_32R2, EF_MIPS_ARCH)
281     BCaseMask(EF_MIPS_ARCH_64R2, EF_MIPS_ARCH)
282     BCaseMask(EF_MIPS_ARCH_32R6, EF_MIPS_ARCH)
283     BCaseMask(EF_MIPS_ARCH_64R6, EF_MIPS_ARCH)
284     break;
285   case ELF::EM_HEXAGON:
286     BCase(EF_HEXAGON_MACH_V2)
287     BCase(EF_HEXAGON_MACH_V3)
288     BCase(EF_HEXAGON_MACH_V4)
289     BCase(EF_HEXAGON_MACH_V5)
290     BCase(EF_HEXAGON_ISA_V2)
291     BCase(EF_HEXAGON_ISA_V3)
292     BCase(EF_HEXAGON_ISA_V4)
293     BCase(EF_HEXAGON_ISA_V5)
294     break;
295   default:
296     llvm_unreachable("Unsupported architecture");
297   }
298 #undef BCase
299 #undef BCaseMask
300 }
301 
enumeration(IO & IO,ELFYAML::ELF_SHT & Value)302 void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
303     IO &IO, ELFYAML::ELF_SHT &Value) {
304   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
305   assert(Object && "The IO context is not initialized");
306 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
307   ECase(SHT_NULL)
308   ECase(SHT_PROGBITS)
309   // No SHT_SYMTAB. Use the top-level `Symbols` key instead.
310   // FIXME: Issue a diagnostic with this information.
311   ECase(SHT_STRTAB)
312   ECase(SHT_RELA)
313   ECase(SHT_HASH)
314   ECase(SHT_DYNAMIC)
315   ECase(SHT_NOTE)
316   ECase(SHT_NOBITS)
317   ECase(SHT_REL)
318   ECase(SHT_SHLIB)
319   ECase(SHT_DYNSYM)
320   ECase(SHT_INIT_ARRAY)
321   ECase(SHT_FINI_ARRAY)
322   ECase(SHT_PREINIT_ARRAY)
323   ECase(SHT_GROUP)
324   ECase(SHT_SYMTAB_SHNDX)
325   ECase(SHT_LOOS)
326   ECase(SHT_GNU_ATTRIBUTES)
327   ECase(SHT_GNU_HASH)
328   ECase(SHT_GNU_verdef)
329   ECase(SHT_GNU_verneed)
330   ECase(SHT_GNU_versym)
331   ECase(SHT_HIOS)
332   ECase(SHT_LOPROC)
333   switch (Object->Header.Machine) {
334   case ELF::EM_ARM:
335     ECase(SHT_ARM_EXIDX)
336     ECase(SHT_ARM_PREEMPTMAP)
337     ECase(SHT_ARM_ATTRIBUTES)
338     ECase(SHT_ARM_DEBUGOVERLAY)
339     ECase(SHT_ARM_OVERLAYSECTION)
340     break;
341   case ELF::EM_HEXAGON:
342     ECase(SHT_HEX_ORDERED)
343     break;
344   case ELF::EM_X86_64:
345     ECase(SHT_X86_64_UNWIND)
346     break;
347   case ELF::EM_MIPS:
348     ECase(SHT_MIPS_REGINFO)
349     ECase(SHT_MIPS_OPTIONS)
350     ECase(SHT_MIPS_ABIFLAGS)
351     break;
352   default:
353     // Nothing to do.
354     break;
355   }
356 #undef ECase
357 }
358 
bitset(IO & IO,ELFYAML::ELF_SHF & Value)359 void ScalarBitSetTraits<ELFYAML::ELF_SHF>::bitset(IO &IO,
360                                                   ELFYAML::ELF_SHF &Value) {
361 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X);
362   BCase(SHF_WRITE)
363   BCase(SHF_ALLOC)
364   BCase(SHF_EXCLUDE)
365   BCase(SHF_EXECINSTR)
366   BCase(SHF_MERGE)
367   BCase(SHF_STRINGS)
368   BCase(SHF_INFO_LINK)
369   BCase(SHF_LINK_ORDER)
370   BCase(SHF_OS_NONCONFORMING)
371   BCase(SHF_GROUP)
372   BCase(SHF_TLS)
373 #undef BCase
374 }
375 
enumeration(IO & IO,ELFYAML::ELF_STT & Value)376 void ScalarEnumerationTraits<ELFYAML::ELF_STT>::enumeration(
377     IO &IO, ELFYAML::ELF_STT &Value) {
378 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
379   ECase(STT_NOTYPE)
380   ECase(STT_OBJECT)
381   ECase(STT_FUNC)
382   ECase(STT_SECTION)
383   ECase(STT_FILE)
384   ECase(STT_COMMON)
385   ECase(STT_TLS)
386   ECase(STT_GNU_IFUNC)
387 #undef ECase
388 }
389 
enumeration(IO & IO,ELFYAML::ELF_STV & Value)390 void ScalarEnumerationTraits<ELFYAML::ELF_STV>::enumeration(
391     IO &IO, ELFYAML::ELF_STV &Value) {
392 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
393   ECase(STV_DEFAULT)
394   ECase(STV_INTERNAL)
395   ECase(STV_HIDDEN)
396   ECase(STV_PROTECTED)
397 #undef ECase
398 }
399 
bitset(IO & IO,ELFYAML::ELF_STO & Value)400 void ScalarBitSetTraits<ELFYAML::ELF_STO>::bitset(IO &IO,
401                                                   ELFYAML::ELF_STO &Value) {
402   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
403   assert(Object && "The IO context is not initialized");
404 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X);
405   switch (Object->Header.Machine) {
406   case ELF::EM_MIPS:
407     BCase(STO_MIPS_OPTIONAL)
408     BCase(STO_MIPS_PLT)
409     BCase(STO_MIPS_PIC)
410     BCase(STO_MIPS_MICROMIPS)
411     break;
412   default:
413     break; // Nothing to do
414   }
415 #undef BCase
416 #undef BCaseMask
417 }
418 
enumeration(IO & IO,ELFYAML::ELF_RSS & Value)419 void ScalarEnumerationTraits<ELFYAML::ELF_RSS>::enumeration(
420     IO &IO, ELFYAML::ELF_RSS &Value) {
421 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
422   ECase(RSS_UNDEF)
423   ECase(RSS_GP)
424   ECase(RSS_GP0)
425   ECase(RSS_LOC)
426 #undef ECase
427 }
428 
enumeration(IO & IO,ELFYAML::ELF_REL & Value)429 void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration(
430     IO &IO, ELFYAML::ELF_REL &Value) {
431   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
432   assert(Object && "The IO context is not initialized");
433 #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X);
434   switch (Object->Header.Machine) {
435   case ELF::EM_X86_64:
436 #include "llvm/Support/ELFRelocs/x86_64.def"
437     break;
438   case ELF::EM_MIPS:
439 #include "llvm/Support/ELFRelocs/Mips.def"
440     break;
441   case ELF::EM_HEXAGON:
442 #include "llvm/Support/ELFRelocs/Hexagon.def"
443     break;
444   case ELF::EM_386:
445 #include "llvm/Support/ELFRelocs/i386.def"
446     break;
447   case ELF::EM_AARCH64:
448 #include "llvm/Support/ELFRelocs/AArch64.def"
449     break;
450   case ELF::EM_ARM:
451 #include "llvm/Support/ELFRelocs/ARM.def"
452     break;
453   default:
454     llvm_unreachable("Unsupported architecture");
455   }
456 #undef ELF_RELOC
457 }
458 
mapping(IO & IO,ELFYAML::FileHeader & FileHdr)459 void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO,
460                                                  ELFYAML::FileHeader &FileHdr) {
461   IO.mapRequired("Class", FileHdr.Class);
462   IO.mapRequired("Data", FileHdr.Data);
463   IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0));
464   IO.mapRequired("Type", FileHdr.Type);
465   IO.mapRequired("Machine", FileHdr.Machine);
466   IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0));
467   IO.mapOptional("Entry", FileHdr.Entry, Hex64(0));
468 }
469 
470 namespace {
471 struct NormalizedOther {
NormalizedOtherllvm::yaml::__anoncf04e6820111::NormalizedOther472   NormalizedOther(IO &)
473       : Visibility(ELFYAML::ELF_STV(0)), Other(ELFYAML::ELF_STO(0)) {}
NormalizedOtherllvm::yaml::__anoncf04e6820111::NormalizedOther474   NormalizedOther(IO &, uint8_t Original)
475       : Visibility(Original & 0x3), Other(Original & ~0x3) {}
476 
denormalizellvm::yaml::__anoncf04e6820111::NormalizedOther477   uint8_t denormalize(IO &) { return Visibility | Other; }
478 
479   ELFYAML::ELF_STV Visibility;
480   ELFYAML::ELF_STO Other;
481 };
482 }
483 
mapping(IO & IO,ELFYAML::Symbol & Symbol)484 void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) {
485   IO.mapOptional("Name", Symbol.Name, StringRef());
486   IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
487   IO.mapOptional("Section", Symbol.Section, StringRef());
488   IO.mapOptional("Value", Symbol.Value, Hex64(0));
489   IO.mapOptional("Size", Symbol.Size, Hex64(0));
490 
491   MappingNormalization<NormalizedOther, uint8_t> Keys(IO, Symbol.Other);
492   IO.mapOptional("Visibility", Keys->Visibility, ELFYAML::ELF_STV(0));
493   IO.mapOptional("Other", Keys->Other, ELFYAML::ELF_STO(0));
494 }
495 
mapping(IO & IO,ELFYAML::LocalGlobalWeakSymbols & Symbols)496 void MappingTraits<ELFYAML::LocalGlobalWeakSymbols>::mapping(
497     IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols) {
498   IO.mapOptional("Local", Symbols.Local);
499   IO.mapOptional("Global", Symbols.Global);
500   IO.mapOptional("Weak", Symbols.Weak);
501 }
502 
commonSectionMapping(IO & IO,ELFYAML::Section & Section)503 static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
504   IO.mapOptional("Name", Section.Name, StringRef());
505   IO.mapRequired("Type", Section.Type);
506   IO.mapOptional("Flags", Section.Flags, ELFYAML::ELF_SHF(0));
507   IO.mapOptional("Address", Section.Address, Hex64(0));
508   IO.mapOptional("Link", Section.Link, StringRef());
509   IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
510   IO.mapOptional("Info", Section.Info, StringRef());
511 }
512 
sectionMapping(IO & IO,ELFYAML::RawContentSection & Section)513 static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
514   commonSectionMapping(IO, Section);
515   IO.mapOptional("Content", Section.Content);
516   IO.mapOptional("Size", Section.Size, Hex64(Section.Content.binary_size()));
517 }
518 
sectionMapping(IO & IO,ELFYAML::RelocationSection & Section)519 static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
520   commonSectionMapping(IO, Section);
521   IO.mapOptional("Relocations", Section.Relocations);
522 }
523 
groupSectionMapping(IO & IO,ELFYAML::Group & group)524 static void groupSectionMapping(IO &IO, ELFYAML::Group &group) {
525   commonSectionMapping(IO, group);
526   IO.mapRequired("Members", group.Members);
527 }
528 
mapping(IO & IO,ELFYAML::SectionOrType & sectionOrType)529 void MappingTraits<ELFYAML::SectionOrType>::mapping(
530     IO &IO, ELFYAML::SectionOrType &sectionOrType) {
531   IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType);
532 }
533 
mapping(IO & IO,std::unique_ptr<ELFYAML::Section> & Section)534 void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping(
535     IO &IO, std::unique_ptr<ELFYAML::Section> &Section) {
536   ELFYAML::ELF_SHT sectionType;
537   if (IO.outputting())
538     sectionType = Section->Type;
539   else
540     IO.mapRequired("Type", sectionType);
541 
542   switch (sectionType) {
543   case ELF::SHT_REL:
544   case ELF::SHT_RELA:
545     if (!IO.outputting())
546       Section.reset(new ELFYAML::RelocationSection());
547     sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get()));
548     break;
549   case ELF::SHT_GROUP:
550     if (!IO.outputting())
551       Section.reset(new ELFYAML::Group());
552     groupSectionMapping(IO, *cast<ELFYAML::Group>(Section.get()));
553     break;
554   default:
555     if (!IO.outputting())
556       Section.reset(new ELFYAML::RawContentSection());
557     sectionMapping(IO, *cast<ELFYAML::RawContentSection>(Section.get()));
558   }
559 }
560 
validate(IO & io,std::unique_ptr<ELFYAML::Section> & Section)561 StringRef MappingTraits<std::unique_ptr<ELFYAML::Section>>::validate(
562     IO &io, std::unique_ptr<ELFYAML::Section> &Section) {
563   const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(Section.get());
564   if (!RawSection || RawSection->Size >= RawSection->Content.binary_size())
565     return StringRef();
566   return "Section size must be greater or equal to the content size";
567 }
568 
569 namespace {
570 struct NormalizedMips64RelType {
NormalizedMips64RelTypellvm::yaml::__anoncf04e6820211::NormalizedMips64RelType571   NormalizedMips64RelType(IO &)
572       : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
573         Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
574         Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
575         SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {}
NormalizedMips64RelTypellvm::yaml::__anoncf04e6820211::NormalizedMips64RelType576   NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original)
577       : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF),
578         Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {}
579 
denormalizellvm::yaml::__anoncf04e6820211::NormalizedMips64RelType580   ELFYAML::ELF_REL denormalize(IO &) {
581     ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24;
582     return Res;
583   }
584 
585   ELFYAML::ELF_REL Type;
586   ELFYAML::ELF_REL Type2;
587   ELFYAML::ELF_REL Type3;
588   ELFYAML::ELF_RSS SpecSym;
589 };
590 }
591 
mapping(IO & IO,ELFYAML::Relocation & Rel)592 void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
593                                                  ELFYAML::Relocation &Rel) {
594   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
595   assert(Object && "The IO context is not initialized");
596 
597   IO.mapRequired("Offset", Rel.Offset);
598   IO.mapRequired("Symbol", Rel.Symbol);
599 
600   if (Object->Header.Machine == ELFYAML::ELF_EM(ELF::EM_MIPS) &&
601       Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
602     MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key(
603         IO, Rel.Type);
604     IO.mapRequired("Type", Key->Type);
605     IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
606     IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
607     IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF));
608   } else
609     IO.mapRequired("Type", Rel.Type);
610 
611   IO.mapOptional("Addend", Rel.Addend, (int64_t)0);
612 }
613 
mapping(IO & IO,ELFYAML::Object & Object)614 void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) {
615   assert(!IO.getContext() && "The IO context is initialized already");
616   IO.setContext(&Object);
617   IO.mapRequired("FileHeader", Object.Header);
618   IO.mapOptional("Sections", Object.Sections);
619   IO.mapOptional("Symbols", Object.Symbols);
620   IO.setContext(nullptr);
621 }
622 
623 } // end namespace yaml
624 } // end namespace llvm
625