LLVM  9.0.0svn
ELFYAML.cpp
Go to the documentation of this file.
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 
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/BinaryFormat/ELF.h"
16 #include "llvm/Support/Casting.h"
20 #include <cassert>
21 #include <cstdint>
22 
23 namespace llvm {
24 
25 ELFYAML::Section::~Section() = default;
26 
27 namespace yaml {
28 
30  IO &IO, ELFYAML::ELF_ET &Value) {
31 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
32  ECase(ET_NONE);
33  ECase(ET_REL);
34  ECase(ET_EXEC);
35  ECase(ET_DYN);
36  ECase(ET_CORE);
37 #undef ECase
38  IO.enumFallback<Hex16>(Value);
39 }
40 
42  IO &IO, ELFYAML::ELF_PT &Value) {
43 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
44  ECase(PT_NULL);
45  ECase(PT_LOAD);
48  ECase(PT_NOTE);
49  ECase(PT_SHLIB);
50  ECase(PT_PHDR);
51  ECase(PT_TLS);
53 #undef ECase
54  IO.enumFallback<Hex32>(Value);
55 }
56 
58  IO &IO, ELFYAML::ELF_EM &Value) {
59 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
60  ECase(EM_NONE);
61  ECase(EM_M32);
62  ECase(EM_SPARC);
63  ECase(EM_386);
64  ECase(EM_68K);
65  ECase(EM_88K);
66  ECase(EM_IAMCU);
67  ECase(EM_860);
68  ECase(EM_MIPS);
69  ECase(EM_S370);
74  ECase(EM_960);
75  ECase(EM_PPC);
76  ECase(EM_PPC64);
77  ECase(EM_S390);
78  ECase(EM_SPU);
79  ECase(EM_V800);
80  ECase(EM_FR20);
81  ECase(EM_RH32);
82  ECase(EM_RCE);
83  ECase(EM_ARM);
84  ECase(EM_ALPHA);
85  ECase(EM_SH);
88  ECase(EM_ARC);
91  ECase(EM_H8S);
93  ECase(EM_IA_64);
97  ECase(EM_MMA);
98  ECase(EM_PCP);
99  ECase(EM_NCPU);
100  ECase(EM_NDR1);
102  ECase(EM_ME16);
103  ECase(EM_ST100);
104  ECase(EM_TINYJ);
105  ECase(EM_X86_64);
106  ECase(EM_PDSP);
107  ECase(EM_PDP10);
108  ECase(EM_PDP11);
109  ECase(EM_FX66);
110  ECase(EM_ST9PLUS);
111  ECase(EM_ST7);
112  ECase(EM_68HC16);
113  ECase(EM_68HC11);
114  ECase(EM_68HC08);
115  ECase(EM_68HC05);
116  ECase(EM_SVX);
117  ECase(EM_ST19);
118  ECase(EM_VAX);
119  ECase(EM_CRIS);
120  ECase(EM_JAVELIN);
122  ECase(EM_ZSP);
123  ECase(EM_MMIX);
124  ECase(EM_HUANY);
125  ECase(EM_PRISM);
126  ECase(EM_AVR);
127  ECase(EM_FR30);
128  ECase(EM_D10V);
129  ECase(EM_D30V);
130  ECase(EM_V850);
131  ECase(EM_M32R);
132  ECase(EM_MN10300);
133  ECase(EM_MN10200);
134  ECase(EM_PJ);
137  ECase(EM_XTENSA);
139  ECase(EM_TMM_GPP);
140  ECase(EM_NS32K);
141  ECase(EM_TPC);
142  ECase(EM_SNP1K);
143  ECase(EM_ST200);
144  ECase(EM_IP2K);
145  ECase(EM_MAX);
146  ECase(EM_CR);
147  ECase(EM_F2MC16);
148  ECase(EM_MSP430);
150  ECase(EM_SE_C33);
151  ECase(EM_SEP);
152  ECase(EM_ARCA);
153  ECase(EM_UNICORE);
154  ECase(EM_EXCESS);
155  ECase(EM_DXP);
157  ECase(EM_CRX);
158  ECase(EM_XGATE);
159  ECase(EM_C166);
160  ECase(EM_M16C);
162  ECase(EM_CE);
163  ECase(EM_M32C);
164  ECase(EM_TSK3000);
165  ECase(EM_RS08);
166  ECase(EM_SHARC);
167  ECase(EM_ECOG2);
168  ECase(EM_SCORE7);
169  ECase(EM_DSP24);
172  ECase(EM_SE_C17);
178  ECase(EM_R32C);
180  ECase(EM_HEXAGON);
181  ECase(EM_8051);
182  ECase(EM_STXP7X);
183  ECase(EM_NDS32);
184  ECase(EM_ECOG1);
185  ECase(EM_ECOG1X);
186  ECase(EM_MAXQ30);
187  ECase(EM_XIMO16);
188  ECase(EM_MANIK);
189  ECase(EM_CRAYNV2);
190  ECase(EM_RX);
191  ECase(EM_METAG);
193  ECase(EM_ECOG16);
194  ECase(EM_CR16);
195  ECase(EM_ETPU);
196  ECase(EM_SLE9X);
197  ECase(EM_L10M);
198  ECase(EM_K10M);
199  ECase(EM_AARCH64);
200  ECase(EM_AVR32);
201  ECase(EM_STM8);
202  ECase(EM_TILE64);
203  ECase(EM_TILEPRO);
204  ECase(EM_CUDA);
205  ECase(EM_TILEGX);
210  ECase(EM_OPEN8);
211  ECase(EM_RL78);
213  ECase(EM_78KOR);
214  ECase(EM_56800EX);
215  ECase(EM_AMDGPU);
216  ECase(EM_RISCV);
217  ECase(EM_LANAI);
218  ECase(EM_BPF);
219 #undef ECase
220 }
221 
223  IO &IO, ELFYAML::ELF_ELFCLASS &Value) {
224 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
225  // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it
226  // here.
227  ECase(ELFCLASS32);
228  ECase(ELFCLASS64);
229 #undef ECase
230 }
231 
233  IO &IO, ELFYAML::ELF_ELFDATA &Value) {
234 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
235  // ELFDATANONE is an invalid data encoding, but we accept it because
236  // we want to be able to produce invalid binaries for the tests.
240 #undef ECase
241 }
242 
244  IO &IO, ELFYAML::ELF_ELFOSABI &Value) {
245 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
270 #undef ECase
271 }
272 
274  ELFYAML::ELF_EF &Value) {
275  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
276  assert(Object && "The IO context is not initialized");
277 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
278 #define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M)
279  switch (Object->Header.Machine) {
280  case ELF::EM_ARM:
289  break;
290  case ELF::EM_MIPS:
334  break;
335  case ELF::EM_HEXAGON:
352  break;
353  case ELF::EM_AVR:
371  break;
372  case ELF::EM_RISCV:
379  break;
380  case ELF::EM_AMDGPU:
416  break;
417  case ELF::EM_X86_64:
418  break;
419  default:
420  llvm_unreachable("Unsupported architecture");
421  }
422 #undef BCase
423 #undef BCaseMask
424 }
425 
427  IO &IO, ELFYAML::ELF_SHT &Value) {
428  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
429  assert(Object && "The IO context is not initialized");
430 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
431  ECase(SHT_NULL);
433  ECase(SHT_SYMTAB);
434  // FIXME: Issue a diagnostic with this information.
435  ECase(SHT_STRTAB);
436  ECase(SHT_RELA);
437  ECase(SHT_HASH);
439  ECase(SHT_NOTE);
440  ECase(SHT_NOBITS);
441  ECase(SHT_REL);
442  ECase(SHT_SHLIB);
443  ECase(SHT_DYNSYM);
447  ECase(SHT_GROUP);
449  ECase(SHT_RELR);
462  switch (Object->Header.Machine) {
463  case ELF::EM_ARM:
469  break;
470  case ELF::EM_HEXAGON:
472  break;
473  case ELF::EM_X86_64:
475  break;
476  case ELF::EM_MIPS:
481  break;
482  default:
483  // Nothing to do.
484  break;
485  }
486 #undef ECase
487  IO.enumFallback<Hex32>(Value);
488 }
489 
491  ELFYAML::ELF_PF &Value) {
492 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
493  BCase(PF_X);
494  BCase(PF_W);
495  BCase(PF_R);
496 }
497 
499  ELFYAML::ELF_SHF &Value) {
500  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
501 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
502  BCase(SHF_WRITE);
503  BCase(SHF_ALLOC);
506  BCase(SHF_MERGE);
511  BCase(SHF_GROUP);
512  BCase(SHF_TLS);
514  switch (Object->Header.Machine) {
515  case ELF::EM_ARM:
517  break;
518  case ELF::EM_HEXAGON:
520  break;
521  case ELF::EM_MIPS:
530  break;
531  case ELF::EM_X86_64:
533  break;
534  default:
535  // Nothing to do.
536  break;
537  }
538 #undef BCase
539 }
540 
542  IO &IO, ELFYAML::ELF_SHN &Value) {
543 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
544  ECase(SHN_UNDEF);
546  ECase(SHN_LOPROC);
547  ECase(SHN_HIPROC);
548  ECase(SHN_LOOS);
549  ECase(SHN_HIOS);
550  ECase(SHN_ABS);
551  ECase(SHN_COMMON);
552  ECase(SHN_XINDEX);
559 #undef ECase
560  IO.enumFallback<Hex32>(Value);
561 }
562 
564  IO &IO, ELFYAML::ELF_STT &Value) {
565 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
566  ECase(STT_NOTYPE);
567  ECase(STT_OBJECT);
568  ECase(STT_FUNC);
570  ECase(STT_FILE);
571  ECase(STT_COMMON);
572  ECase(STT_TLS);
574 #undef ECase
575  IO.enumFallback<Hex8>(Value);
576 }
577 
579  IO &IO, ELFYAML::ELF_STV &Value) {
580 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
583  ECase(STV_HIDDEN);
585 #undef ECase
586 }
587 
589  ELFYAML::ELF_STO &Value) {
590  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
591  assert(Object && "The IO context is not initialized");
592 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
593  switch (Object->Header.Machine) {
594  case ELF::EM_MIPS:
599  break;
600  default:
601  break; // Nothing to do
602  }
603 #undef BCase
604 #undef BCaseMask
605 }
606 
608  IO &IO, ELFYAML::ELF_RSS &Value) {
609 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
610  ECase(RSS_UNDEF);
611  ECase(RSS_GP);
612  ECase(RSS_GP0);
613  ECase(RSS_LOC);
614 #undef ECase
615 }
616 
618  IO &IO, ELFYAML::ELF_REL &Value) {
619  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
620  assert(Object && "The IO context is not initialized");
621 #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X);
622  switch (Object->Header.Machine) {
623  case ELF::EM_X86_64:
624 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
625  break;
626  case ELF::EM_MIPS:
627 #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
628  break;
629  case ELF::EM_HEXAGON:
630 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
631  break;
632  case ELF::EM_386:
633  case ELF::EM_IAMCU:
634 #include "llvm/BinaryFormat/ELFRelocs/i386.def"
635  break;
636  case ELF::EM_AARCH64:
637 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
638  break;
639  case ELF::EM_ARM:
640 #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
641  break;
642  case ELF::EM_ARC:
643 #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
644  break;
645  case ELF::EM_RISCV:
646 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
647  break;
648  case ELF::EM_LANAI:
649 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
650  break;
651  case ELF::EM_AMDGPU:
652 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
653  break;
654  case ELF::EM_BPF:
655 #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
656  break;
657  default:
658  llvm_unreachable("Unsupported architecture");
659  }
660 #undef ELF_RELOC
661  IO.enumFallback<Hex32>(Value);
662 }
663 
665  IO &IO, ELFYAML::ELF_DYNTAG &Value) {
666  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
667  assert(Object && "The IO context is not initialized");
668 
669 // Disable architecture specific tags by default. We might enable them below.
670 #define MIPS_DYNAMIC_TAG(name, value)
671 #define HEXAGON_DYNAMIC_TAG(name, value)
672 #define PPC64_DYNAMIC_TAG(name, value)
673 // Ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
674 #define DYNAMIC_TAG_MARKER(name, value)
675 
676 #define STRINGIFY(X) (#X)
677 #define DYNAMIC_TAG(X, Y) IO.enumCase(Value, STRINGIFY(DT_##X), ELF::DT_##X);
678  switch (Object->Header.Machine) {
679  case ELF::EM_MIPS:
680 #undef MIPS_DYNAMIC_TAG
681 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
682 #include "llvm/BinaryFormat/DynamicTags.def"
683 #undef MIPS_DYNAMIC_TAG
684 #define MIPS_DYNAMIC_TAG(name, value)
685  break;
686  case ELF::EM_HEXAGON:
687 #undef HEXAGON_DYNAMIC_TAG
688 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
689 #include "llvm/BinaryFormat/DynamicTags.def"
690 #undef HEXAGON_DYNAMIC_TAG
691 #define HEXAGON_DYNAMIC_TAG(name, value)
692  break;
693  case ELF::EM_PPC64:
694 #undef PPC64_DYNAMIC_TAG
695 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
696 #include "llvm/BinaryFormat/DynamicTags.def"
697 #undef PPC64_DYNAMIC_TAG
698 #define PPC64_DYNAMIC_TAG(name, value)
699  break;
700  default:
701 #include "llvm/BinaryFormat/DynamicTags.def"
702  break;
703  }
704 
705 #undef MIPS_DYNAMIC_TAG
706 #undef HEXAGON_DYNAMIC_TAG
707 #undef PPC64_DYNAMIC_TAG
708 #undef DYNAMIC_TAG_MARKER
709 #undef STRINGIFY
710 #undef DYNAMIC_TAG
711 
712  IO.enumFallback<Hex64>(Value);
713 }
714 
716  IO &IO, ELFYAML::MIPS_AFL_REG &Value) {
717 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
718  ECase(REG_NONE);
719  ECase(REG_32);
720  ECase(REG_64);
721  ECase(REG_128);
722 #undef ECase
723 }
724 
726  IO &IO, ELFYAML::MIPS_ABI_FP &Value) {
727 #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X)
728  ECase(FP_ANY);
729  ECase(FP_DOUBLE);
730  ECase(FP_SINGLE);
731  ECase(FP_SOFT);
732  ECase(FP_OLD_64);
733  ECase(FP_XX);
734  ECase(FP_64);
735  ECase(FP_64A);
736 #undef ECase
737 }
738 
740  IO &IO, ELFYAML::MIPS_AFL_EXT &Value) {
741 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
742  ECase(EXT_NONE);
743  ECase(EXT_XLR);
744  ECase(EXT_OCTEON2);
745  ECase(EXT_OCTEONP);
746  ECase(EXT_LOONGSON_3A);
747  ECase(EXT_OCTEON);
748  ECase(EXT_5900);
749  ECase(EXT_4650);
750  ECase(EXT_4010);
751  ECase(EXT_4100);
752  ECase(EXT_3900);
753  ECase(EXT_10000);
754  ECase(EXT_SB1);
755  ECase(EXT_4111);
756  ECase(EXT_4120);
757  ECase(EXT_5400);
758  ECase(EXT_5500);
759  ECase(EXT_LOONGSON_2E);
760  ECase(EXT_LOONGSON_2F);
761  ECase(EXT_OCTEON3);
762 #undef ECase
763 }
764 
766  IO &IO, ELFYAML::MIPS_ISA &Value) {
767  IO.enumCase(Value, "MIPS1", 1);
768  IO.enumCase(Value, "MIPS2", 2);
769  IO.enumCase(Value, "MIPS3", 3);
770  IO.enumCase(Value, "MIPS4", 4);
771  IO.enumCase(Value, "MIPS5", 5);
772  IO.enumCase(Value, "MIPS32", 32);
773  IO.enumCase(Value, "MIPS64", 64);
774 }
775 
777  IO &IO, ELFYAML::MIPS_AFL_ASE &Value) {
778 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X)
779  BCase(DSP);
780  BCase(DSPR2);
781  BCase(EVA);
782  BCase(MCU);
783  BCase(MDMX);
784  BCase(MIPS3D);
785  BCase(MT);
786  BCase(SMARTMIPS);
787  BCase(VIRT);
788  BCase(MSA);
789  BCase(MIPS16);
790  BCase(MICROMIPS);
791  BCase(XPA);
792 #undef BCase
793 }
794 
796  IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) {
797 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X)
798  BCase(ODDSPREG);
799 #undef BCase
800 }
801 
803  ELFYAML::FileHeader &FileHdr) {
804  IO.mapRequired("Class", FileHdr.Class);
805  IO.mapRequired("Data", FileHdr.Data);
806  IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0));
807  IO.mapOptional("ABIVersion", FileHdr.ABIVersion, Hex8(0));
808  IO.mapRequired("Type", FileHdr.Type);
809  IO.mapRequired("Machine", FileHdr.Machine);
810  IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0));
811  IO.mapOptional("Entry", FileHdr.Entry, Hex64(0));
812 }
813 
815  IO &IO, ELFYAML::ProgramHeader &Phdr) {
816  IO.mapRequired("Type", Phdr.Type);
817  IO.mapOptional("Flags", Phdr.Flags, ELFYAML::ELF_PF(0));
818  IO.mapOptional("Sections", Phdr.Sections);
819  IO.mapOptional("VAddr", Phdr.VAddr, Hex64(0));
820  IO.mapOptional("PAddr", Phdr.PAddr, Hex64(0));
821  IO.mapOptional("Align", Phdr.Align);
822  IO.mapOptional("FileSize", Phdr.FileSize);
823  IO.mapOptional("MemSize", Phdr.MemSize);
824  IO.mapOptional("Offset", Phdr.Offset);
825 }
826 
827 namespace {
828 
829 struct NormalizedOther {
830  NormalizedOther(IO &)
831  : Visibility(ELFYAML::ELF_STV(0)), Other(ELFYAML::ELF_STO(0)) {}
832  NormalizedOther(IO &, uint8_t Original)
833  : Visibility(Original & 0x3), Other(Original & ~0x3) {}
834 
835  uint8_t denormalize(IO &) { return Visibility | Other; }
836 
837  ELFYAML::ELF_STV Visibility;
838  ELFYAML::ELF_STO Other;
839 };
840 
841 } // end anonymous namespace
842 
844  IO.mapOptional("Name", Symbol.Name, StringRef());
845  IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
846  IO.mapOptional("Section", Symbol.Section, StringRef());
847  IO.mapOptional("Index", Symbol.Index);
848  IO.mapOptional("Value", Symbol.Value, Hex64(0));
849  IO.mapOptional("Size", Symbol.Size, Hex64(0));
850 
851  MappingNormalization<NormalizedOther, uint8_t> Keys(IO, Symbol.Other);
852  IO.mapOptional("Visibility", Keys->Visibility, ELFYAML::ELF_STV(0));
853  IO.mapOptional("Other", Keys->Other, ELFYAML::ELF_STO(0));
854 }
855 
858  if (Symbol.Index && Symbol.Section.data()) {
859  return "Index and Section cannot both be specified for Symbol";
860  }
861  if (Symbol.Index && *Symbol.Index == ELFYAML::ELF_SHN(ELF::SHN_XINDEX)) {
862  return "Large indexes are not supported";
863  }
864  return StringRef();
865 }
866 
868  IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols) {
869  IO.mapOptional("Local", Symbols.Local);
870  IO.mapOptional("Global", Symbols.Global);
871  IO.mapOptional("Weak", Symbols.Weak);
872 }
873 
875  IO.mapOptional("Name", Section.Name, StringRef());
876  IO.mapRequired("Type", Section.Type);
877  IO.mapOptional("Flags", Section.Flags, ELFYAML::ELF_SHF(0));
878  IO.mapOptional("Address", Section.Address, Hex64(0));
879  IO.mapOptional("Link", Section.Link, StringRef());
880  IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
881  IO.mapOptional("EntSize", Section.EntSize);
882 }
883 
885  commonSectionMapping(IO, Section);
886  IO.mapOptional("Entries", Section.Entries);
887  IO.mapOptional("Content", Section.Content);
888 }
889 
891  commonSectionMapping(IO, Section);
892  IO.mapOptional("Content", Section.Content);
893  IO.mapOptional("Size", Section.Size, Hex64(Section.Content.binary_size()));
894  IO.mapOptional("Info", Section.Info, Hex64(0));
895 }
896 
898  commonSectionMapping(IO, Section);
899  IO.mapOptional("Size", Section.Size, Hex64(0));
900 }
901 
903  commonSectionMapping(IO, Section);
904  IO.mapRequired("Info", Section.Info);
905  IO.mapRequired("Entries", Section.Entries);
906 }
907 
909  commonSectionMapping(IO, Section);
910  IO.mapRequired("Entries", Section.Entries);
911 }
912 
914  commonSectionMapping(IO, Section);
915  IO.mapRequired("Info", Section.Info);
916  IO.mapRequired("Dependencies", Section.VerneedV);
917 }
918 
920  commonSectionMapping(IO, Section);
921  IO.mapOptional("Info", Section.RelocatableSec, StringRef());
922  IO.mapOptional("Relocations", Section.Relocations);
923 }
924 
925 static void groupSectionMapping(IO &IO, ELFYAML::Group &Group) {
926  commonSectionMapping(IO, Group);
927  IO.mapOptional("Info", Group.Signature, StringRef());
928  IO.mapRequired("Members", Group.Members);
929 }
930 
932  IO &IO, ELFYAML::SectionOrType &sectionOrType) {
933  IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType);
934 }
935 
937  IO &IO, ELFYAML::SectionName &sectionName) {
938  IO.mapRequired("Section", sectionName.Section);
939 }
940 
942  commonSectionMapping(IO, Section);
943  IO.mapOptional("Version", Section.Version, Hex16(0));
944  IO.mapRequired("ISA", Section.ISALevel);
945  IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0));
946  IO.mapOptional("ISAExtension", Section.ISAExtension,
947  ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE));
948  IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0));
949  IO.mapOptional("FpABI", Section.FpABI,
950  ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY));
951  IO.mapOptional("GPRSize", Section.GPRSize,
952  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
953  IO.mapOptional("CPR1Size", Section.CPR1Size,
954  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
955  IO.mapOptional("CPR2Size", Section.CPR2Size,
956  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
957  IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0));
958  IO.mapOptional("Flags2", Section.Flags2, Hex32(0));
959 }
960 
962  IO &IO, std::unique_ptr<ELFYAML::Section> &Section) {
963  ELFYAML::ELF_SHT sectionType;
964  if (IO.outputting())
965  sectionType = Section->Type;
966  else
967  IO.mapRequired("Type", sectionType);
968 
969  switch (sectionType) {
970  case ELF::SHT_DYNAMIC:
971  if (!IO.outputting())
972  Section.reset(new ELFYAML::DynamicSection());
973  sectionMapping(IO, *cast<ELFYAML::DynamicSection>(Section.get()));
974  break;
975  case ELF::SHT_REL:
976  case ELF::SHT_RELA:
977  if (!IO.outputting())
978  Section.reset(new ELFYAML::RelocationSection());
979  sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get()));
980  break;
981  case ELF::SHT_GROUP:
982  if (!IO.outputting())
983  Section.reset(new ELFYAML::Group());
984  groupSectionMapping(IO, *cast<ELFYAML::Group>(Section.get()));
985  break;
986  case ELF::SHT_NOBITS:
987  if (!IO.outputting())
988  Section.reset(new ELFYAML::NoBitsSection());
989  sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get()));
990  break;
992  if (!IO.outputting())
993  Section.reset(new ELFYAML::MipsABIFlags());
994  sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get()));
995  break;
996  case ELF::SHT_GNU_verdef:
997  if (!IO.outputting())
998  Section.reset(new ELFYAML::VerdefSection());
999  sectionMapping(IO, *cast<ELFYAML::VerdefSection>(Section.get()));
1000  break;
1001  case ELF::SHT_GNU_versym:
1002  if (!IO.outputting())
1003  Section.reset(new ELFYAML::SymverSection());
1004  sectionMapping(IO, *cast<ELFYAML::SymverSection>(Section.get()));
1005  break;
1006  case ELF::SHT_GNU_verneed:
1007  if (!IO.outputting())
1008  Section.reset(new ELFYAML::VerneedSection());
1009  sectionMapping(IO, *cast<ELFYAML::VerneedSection>(Section.get()));
1010  break;
1011  default:
1012  if (!IO.outputting())
1013  Section.reset(new ELFYAML::RawContentSection());
1014  sectionMapping(IO, *cast<ELFYAML::RawContentSection>(Section.get()));
1015  }
1016 }
1017 
1019  IO &io, std::unique_ptr<ELFYAML::Section> &Section) {
1020  const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(Section.get());
1021  if (!RawSection || RawSection->Size >= RawSection->Content.binary_size())
1022  return StringRef();
1023  return "Section size must be greater or equal to the content size";
1024 }
1025 
1026 namespace {
1027 
1028 struct NormalizedMips64RelType {
1029  NormalizedMips64RelType(IO &)
1030  : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1031  Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1032  Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1033  SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {}
1034  NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original)
1035  : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF),
1036  Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {}
1037 
1038  ELFYAML::ELF_REL denormalize(IO &) {
1039  ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24;
1040  return Res;
1041  }
1042 
1043  ELFYAML::ELF_REL Type;
1044  ELFYAML::ELF_REL Type2;
1045  ELFYAML::ELF_REL Type3;
1046  ELFYAML::ELF_RSS SpecSym;
1047 };
1048 
1049 } // end anonymous namespace
1050 
1052  ELFYAML::DynamicEntry &Rel) {
1053  assert(IO.getContext() && "The IO context is not initialized");
1054 
1055  IO.mapRequired("Tag", Rel.Tag);
1056  IO.mapRequired("Value", Rel.Val);
1057 }
1058 
1061  assert(IO.getContext() && "The IO context is not initialized");
1062 
1063  IO.mapRequired("Version", E.Version);
1064  IO.mapRequired("Flags", E.Flags);
1065  IO.mapRequired("VersionNdx", E.VersionNdx);
1066  IO.mapRequired("Hash", E.Hash);
1067  IO.mapRequired("Names", E.VerNames);
1068 }
1069 
1072  assert(IO.getContext() && "The IO context is not initialized");
1073 
1074  IO.mapRequired("Version", E.Version);
1075  IO.mapRequired("File", E.File);
1076  IO.mapRequired("Entries", E.AuxV);
1077 }
1078 
1081  assert(IO.getContext() && "The IO context is not initialized");
1082 
1083  IO.mapRequired("Name", E.Name);
1084  IO.mapRequired("Hash", E.Hash);
1085  IO.mapRequired("Flags", E.Flags);
1086  IO.mapRequired("Other", E.Other);
1087 }
1088 
1090  ELFYAML::Relocation &Rel) {
1091  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
1092  assert(Object && "The IO context is not initialized");
1093 
1094  IO.mapRequired("Offset", Rel.Offset);
1095  IO.mapOptional("Symbol", Rel.Symbol);
1096 
1097  if (Object->Header.Machine == ELFYAML::ELF_EM(ELF::EM_MIPS) &&
1098  Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
1099  MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key(
1100  IO, Rel.Type);
1101  IO.mapRequired("Type", Key->Type);
1102  IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
1103  IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
1104  IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF));
1105  } else
1106  IO.mapRequired("Type", Rel.Type);
1107 
1108  IO.mapOptional("Addend", Rel.Addend, (int64_t)0);
1109 }
1110 
1112  assert(!IO.getContext() && "The IO context is initialized already");
1113  IO.setContext(&Object);
1114  IO.mapTag("!ELF", true);
1115  IO.mapRequired("FileHeader", Object.Header);
1116  IO.mapOptional("ProgramHeaders", Object.ProgramHeaders);
1117  IO.mapOptional("Sections", Object.Sections);
1118  IO.mapOptional("Symbols", Object.Symbols);
1119  IO.mapOptional("DynamicSymbols", Object.DynamicSymbols);
1120  IO.setContext(nullptr);
1121 }
1122 
1123 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
1124 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
1125 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
1126 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
1127 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
1128 
1129 } // end namespace yaml
1130 
1131 } // end namespace llvm
This file declares classes for handling the YAML representation of ELF.
static void bitset(IO &IO, ELFYAML::ELF_SHF &Value)
Definition: ELFYAML.cpp:498
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static void enumeration(IO &IO, ELFYAML::ELF_ET &Value)
Definition: ELFYAML.cpp:29
Optional< llvm::yaml::Hex64 > Align
Definition: ELFYAML.h:88
static void enumeration(IO &IO, ELFYAML::ELF_STT &Value)
Definition: ELFYAML.cpp:563
static void enumeration(IO &IO, ELFYAML::ELF_STV &Value)
Definition: ELFYAML.cpp:578
Optional< ELF_SHN > Index
Definition: ELFYAML.h:99
static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value)
Definition: ELFYAML.cpp:541
llvm::yaml::Hex64 PAddr
Definition: ELFYAML.h:87
static void bitset(IO &IO, ELFYAML::ELF_PF &Value)
Definition: ELFYAML.cpp:490
std::vector< SectionName > Sections
Definition: ELFYAML.h:92
llvm::yaml::Hex16 Version
Definition: ELFYAML.h:264
ELFYAML::ELF_REL Type3
Definition: ELFYAML.cpp:1045
llvm::yaml::Hex64 Info
Definition: ELFYAML.h:222
llvm::yaml::Hex64 Address
Definition: ELFYAML.h:136
ELFYAML::ELF_STV Visibility
Definition: ELFYAML.cpp:837
static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value)
Definition: ELFYAML.cpp:607
FileHeader Header
Definition: ELFYAML.h:284
static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value)
Definition: ELFYAML.cpp:426
llvm::yaml::Hex64 Entry
Definition: ELFYAML.h:76
static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value)
Definition: ELFYAML.cpp:795
LocalGlobalWeakSymbols Symbols
Definition: ELFYAML.h:291
llvm::yaml::Hex64 AddressAlign
Definition: ELFYAML.h:138
std::vector< Symbol > Weak
Definition: ELFYAML.h:108
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:169
std::vector< DynamicEntry > Entries
Definition: ELFYAML.h:146
StringRef Section
Definition: ELFYAML.h:98
static void groupSectionMapping(IO &IO, ELFYAML::Group &Group)
Definition: ELFYAML.cpp:925
static void enumeration(IO &IO, ELFYAML::ELF_EM &Value)
Definition: ELFYAML.cpp:57
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:101
StringRef Name
Definition: ELFYAML.h:96
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:838
llvm::yaml::Hex8 ABIVersion
Definition: ELFYAML.h:72
llvm::yaml::Hex64 Info
Definition: ELFYAML.h:193
Key
PAL metadata keys.
static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value)
Definition: ELFYAML.cpp:222
Optional< llvm::yaml::Hex64 > EntSize
Definition: ELFYAML.h:139
ELFYAML::ELF_RSS SpecSym
Definition: ELFYAML.cpp:1046
std::vector< Symbol > Global
Definition: ELFYAML.h:107
LocalGlobalWeakSymbols DynamicSymbols
Definition: ELFYAML.h:292
static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value)
Definition: ELFYAML.cpp:765
static void bitset(IO &IO, ELFYAML::ELF_STO &Value)
Definition: ELFYAML.cpp:588
std::vector< ProgramHeader > ProgramHeaders
Definition: ELFYAML.h:285
std::vector< VernauxEntry > AuxV
Definition: ELFYAML.h:188
static void commonSectionMapping(IO &IO, ELFYAML::Section &Section)
Definition: ELFYAML.cpp:874
static void enumeration(IO &IO, ELFYAML::ELF_PT &Value)
Definition: ELFYAML.cpp:41
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
static void enumeration(IO &IO, ELFYAML::ELF_DYNTAG &Value)
Definition: ELFYAML.cpp:664
llvm::yaml::Hex32 Flags2
Definition: ELFYAML.h:274
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define BCase(X)
static void enumeration(IO &IO, ELFYAML::ELF_REL &Value)
Definition: ELFYAML.cpp:617
llvm::yaml::Hex64 Info
Definition: ELFYAML.h:159
std::vector< VerneedEntry > VerneedV
Definition: ELFYAML.h:192
std::vector< StringRef > VerNames
Definition: ELFYAML.h:217
StringRef Signature
Definition: ELFYAML.h:235
static void sectionMapping(IO &IO, ELFYAML::DynamicSection &Section)
Definition: ELFYAML.cpp:884
std::vector< std::unique_ptr< Section > > Sections
Definition: ELFYAML.h:286
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:147
std::vector< Symbol > Local
Definition: ELFYAML.h:106
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
llvm::yaml::Hex8 ISARevision
Definition: ELFYAML.h:266
ArrayRef< uint8_t >::size_type binary_size() const
The number of bytes that are represented by this BinaryRef.
Definition: YAML.h:81
Optional< llvm::yaml::Hex64 > MemSize
Definition: ELFYAML.h:90
static void bitset(IO &IO, ELFYAML::ELF_EF &Value)
Definition: ELFYAML.cpp:273
ELF_ELFCLASS Class
Definition: ELFYAML.h:69
std::vector< SectionOrType > Members
Definition: ELFYAML.h:234
llvm::yaml::Hex64 Offset
Definition: ELFYAML.h:245
static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value)
Definition: ELFYAML.cpp:243
static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value)
Definition: ELFYAML.cpp:776
static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value)
Definition: ELFYAML.cpp:739
#define ECase(X)
ELFYAML::ELF_REL Type2
Definition: ELFYAML.cpp:1044
static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value)
Definition: ELFYAML.cpp:725
MIPS_AFL_EXT ISAExtension
Definition: ELFYAML.h:271
llvm::yaml::Hex64 Value
Definition: ELFYAML.h:100
static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value)
Definition: ELFYAML.cpp:232
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:322
Optional< llvm::yaml::Hex64 > FileSize
Definition: ELFYAML.h:89
std::vector< VerdefEntry > Entries
Definition: ELFYAML.h:221
MIPS_AFL_FLAGS1 Flags1
Definition: ELFYAML.h:273
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:122
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:72
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:158
llvm::yaml::Hex64 Val
Definition: ELFYAML.h:117
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
std::vector< uint16_t > Entries
Definition: ELFYAML.h:203
static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value)
Definition: ELFYAML.cpp:715
ELF_ELFOSABI OSABI
Definition: ELFYAML.h:71
llvm::yaml::Hex64 VAddr
Definition: ELFYAML.h:86
#define BCaseMask(X, M)
Optional< StringRef > Symbol
Definition: ELFYAML.h:248
std::vector< Relocation > Relocations
Definition: ELFYAML.h:252
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
Optional< llvm::yaml::Hex64 > Offset
Definition: ELFYAML.h:91