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