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:
417  break;
418  case ELF::EM_X86_64:
419  break;
420  default:
421  llvm_unreachable("Unsupported architecture");
422  }
423 #undef BCase
424 #undef BCaseMask
425 }
426 
428  IO &IO, ELFYAML::ELF_SHT &Value) {
429  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
430  assert(Object && "The IO context is not initialized");
431 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
432  ECase(SHT_NULL);
434  ECase(SHT_SYMTAB);
435  // FIXME: Issue a diagnostic with this information.
436  ECase(SHT_STRTAB);
437  ECase(SHT_RELA);
438  ECase(SHT_HASH);
440  ECase(SHT_NOTE);
441  ECase(SHT_NOBITS);
442  ECase(SHT_REL);
443  ECase(SHT_SHLIB);
444  ECase(SHT_DYNSYM);
448  ECase(SHT_GROUP);
450  ECase(SHT_RELR);
464  switch (Object->Header.Machine) {
465  case ELF::EM_ARM:
471  break;
472  case ELF::EM_HEXAGON:
474  break;
475  case ELF::EM_X86_64:
477  break;
478  case ELF::EM_MIPS:
483  break;
484  default:
485  // Nothing to do.
486  break;
487  }
488 #undef ECase
489  IO.enumFallback<Hex32>(Value);
490 }
491 
493  ELFYAML::ELF_PF &Value) {
494 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
495  BCase(PF_X);
496  BCase(PF_W);
497  BCase(PF_R);
498 }
499 
501  ELFYAML::ELF_SHF &Value) {
502  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
503 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
504  BCase(SHF_WRITE);
505  BCase(SHF_ALLOC);
508  BCase(SHF_MERGE);
513  BCase(SHF_GROUP);
514  BCase(SHF_TLS);
516  switch (Object->Header.Machine) {
517  case ELF::EM_ARM:
519  break;
520  case ELF::EM_HEXAGON:
522  break;
523  case ELF::EM_MIPS:
532  break;
533  case ELF::EM_X86_64:
535  break;
536  default:
537  // Nothing to do.
538  break;
539  }
540 #undef BCase
541 }
542 
544  IO &IO, ELFYAML::ELF_SHN &Value) {
545 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
546  ECase(SHN_UNDEF);
548  ECase(SHN_LOPROC);
549  ECase(SHN_HIPROC);
550  ECase(SHN_LOOS);
551  ECase(SHN_HIOS);
552  ECase(SHN_ABS);
553  ECase(SHN_COMMON);
554  ECase(SHN_XINDEX);
561 #undef ECase
562  IO.enumFallback<Hex32>(Value);
563 }
564 
566  IO &IO, ELFYAML::ELF_STB &Value) {
567 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
568  ECase(STB_LOCAL);
569  ECase(STB_GLOBAL);
570  ECase(STB_WEAK);
572 #undef ECase
573  IO.enumFallback<Hex8>(Value);
574 }
575 
577  IO &IO, ELFYAML::ELF_STT &Value) {
578 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
579  ECase(STT_NOTYPE);
580  ECase(STT_OBJECT);
581  ECase(STT_FUNC);
583  ECase(STT_FILE);
584  ECase(STT_COMMON);
585  ECase(STT_TLS);
587 #undef ECase
588  IO.enumFallback<Hex8>(Value);
589 }
590 
592  IO &IO, ELFYAML::ELF_STV &Value) {
593 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
596  ECase(STV_HIDDEN);
598 #undef ECase
599 }
600 
602  ELFYAML::ELF_STO &Value) {
603  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
604  assert(Object && "The IO context is not initialized");
605 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
606  switch (Object->Header.Machine) {
607  case ELF::EM_MIPS:
612  break;
613  default:
614  break; // Nothing to do
615  }
616 #undef BCase
617 #undef BCaseMask
618 }
619 
621  IO &IO, ELFYAML::ELF_RSS &Value) {
622 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
623  ECase(RSS_UNDEF);
624  ECase(RSS_GP);
625  ECase(RSS_GP0);
626  ECase(RSS_LOC);
627 #undef ECase
628 }
629 
631  IO &IO, ELFYAML::ELF_REL &Value) {
632  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
633  assert(Object && "The IO context is not initialized");
634 #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X);
635  switch (Object->Header.Machine) {
636  case ELF::EM_X86_64:
637 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
638  break;
639  case ELF::EM_MIPS:
640 #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
641  break;
642  case ELF::EM_HEXAGON:
643 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
644  break;
645  case ELF::EM_386:
646  case ELF::EM_IAMCU:
647 #include "llvm/BinaryFormat/ELFRelocs/i386.def"
648  break;
649  case ELF::EM_AARCH64:
650 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
651  break;
652  case ELF::EM_ARM:
653 #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
654  break;
655  case ELF::EM_ARC:
656 #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
657  break;
658  case ELF::EM_RISCV:
659 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
660  break;
661  case ELF::EM_LANAI:
662 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
663  break;
664  case ELF::EM_AMDGPU:
665 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
666  break;
667  case ELF::EM_BPF:
668 #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
669  break;
670  default:
671  llvm_unreachable("Unsupported architecture");
672  }
673 #undef ELF_RELOC
674  IO.enumFallback<Hex32>(Value);
675 }
676 
678  IO &IO, ELFYAML::ELF_DYNTAG &Value) {
679  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
680  assert(Object && "The IO context is not initialized");
681 
682 // Disable architecture specific tags by default. We might enable them below.
683 #define MIPS_DYNAMIC_TAG(name, value)
684 #define HEXAGON_DYNAMIC_TAG(name, value)
685 #define PPC64_DYNAMIC_TAG(name, value)
686 // Ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
687 #define DYNAMIC_TAG_MARKER(name, value)
688 
689 #define STRINGIFY(X) (#X)
690 #define DYNAMIC_TAG(X, Y) IO.enumCase(Value, STRINGIFY(DT_##X), ELF::DT_##X);
691  switch (Object->Header.Machine) {
692  case ELF::EM_MIPS:
693 #undef MIPS_DYNAMIC_TAG
694 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
695 #include "llvm/BinaryFormat/DynamicTags.def"
696 #undef MIPS_DYNAMIC_TAG
697 #define MIPS_DYNAMIC_TAG(name, value)
698  break;
699  case ELF::EM_HEXAGON:
700 #undef HEXAGON_DYNAMIC_TAG
701 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
702 #include "llvm/BinaryFormat/DynamicTags.def"
703 #undef HEXAGON_DYNAMIC_TAG
704 #define HEXAGON_DYNAMIC_TAG(name, value)
705  break;
706  case ELF::EM_PPC64:
707 #undef PPC64_DYNAMIC_TAG
708 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
709 #include "llvm/BinaryFormat/DynamicTags.def"
710 #undef PPC64_DYNAMIC_TAG
711 #define PPC64_DYNAMIC_TAG(name, value)
712  break;
713  default:
714 #include "llvm/BinaryFormat/DynamicTags.def"
715  break;
716  }
717 
718 #undef MIPS_DYNAMIC_TAG
719 #undef HEXAGON_DYNAMIC_TAG
720 #undef PPC64_DYNAMIC_TAG
721 #undef DYNAMIC_TAG_MARKER
722 #undef STRINGIFY
723 #undef DYNAMIC_TAG
724 
725  IO.enumFallback<Hex64>(Value);
726 }
727 
729  IO &IO, ELFYAML::MIPS_AFL_REG &Value) {
730 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
731  ECase(REG_NONE);
732  ECase(REG_32);
733  ECase(REG_64);
734  ECase(REG_128);
735 #undef ECase
736 }
737 
739  IO &IO, ELFYAML::MIPS_ABI_FP &Value) {
740 #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X)
741  ECase(FP_ANY);
742  ECase(FP_DOUBLE);
743  ECase(FP_SINGLE);
744  ECase(FP_SOFT);
745  ECase(FP_OLD_64);
746  ECase(FP_XX);
747  ECase(FP_64);
748  ECase(FP_64A);
749 #undef ECase
750 }
751 
753  IO &IO, ELFYAML::MIPS_AFL_EXT &Value) {
754 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
755  ECase(EXT_NONE);
756  ECase(EXT_XLR);
757  ECase(EXT_OCTEON2);
758  ECase(EXT_OCTEONP);
759  ECase(EXT_LOONGSON_3A);
760  ECase(EXT_OCTEON);
761  ECase(EXT_5900);
762  ECase(EXT_4650);
763  ECase(EXT_4010);
764  ECase(EXT_4100);
765  ECase(EXT_3900);
766  ECase(EXT_10000);
767  ECase(EXT_SB1);
768  ECase(EXT_4111);
769  ECase(EXT_4120);
770  ECase(EXT_5400);
771  ECase(EXT_5500);
772  ECase(EXT_LOONGSON_2E);
773  ECase(EXT_LOONGSON_2F);
774  ECase(EXT_OCTEON3);
775 #undef ECase
776 }
777 
779  IO &IO, ELFYAML::MIPS_ISA &Value) {
780  IO.enumCase(Value, "MIPS1", 1);
781  IO.enumCase(Value, "MIPS2", 2);
782  IO.enumCase(Value, "MIPS3", 3);
783  IO.enumCase(Value, "MIPS4", 4);
784  IO.enumCase(Value, "MIPS5", 5);
785  IO.enumCase(Value, "MIPS32", 32);
786  IO.enumCase(Value, "MIPS64", 64);
787 }
788 
790  IO &IO, ELFYAML::MIPS_AFL_ASE &Value) {
791 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X)
792  BCase(DSP);
793  BCase(DSPR2);
794  BCase(EVA);
795  BCase(MCU);
796  BCase(MDMX);
797  BCase(MIPS3D);
798  BCase(MT);
799  BCase(SMARTMIPS);
800  BCase(VIRT);
801  BCase(MSA);
802  BCase(MIPS16);
803  BCase(MICROMIPS);
804  BCase(XPA);
805 #undef BCase
806 }
807 
809  IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) {
810 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X)
811  BCase(ODDSPREG);
812 #undef BCase
813 }
814 
816  ELFYAML::FileHeader &FileHdr) {
817  IO.mapRequired("Class", FileHdr.Class);
818  IO.mapRequired("Data", FileHdr.Data);
819  IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0));
820  IO.mapOptional("ABIVersion", FileHdr.ABIVersion, Hex8(0));
821  IO.mapRequired("Type", FileHdr.Type);
822  IO.mapRequired("Machine", FileHdr.Machine);
823  IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0));
824  IO.mapOptional("Entry", FileHdr.Entry, Hex64(0));
825 }
826 
828  IO &IO, ELFYAML::ProgramHeader &Phdr) {
829  IO.mapRequired("Type", Phdr.Type);
830  IO.mapOptional("Flags", Phdr.Flags, ELFYAML::ELF_PF(0));
831  IO.mapOptional("Sections", Phdr.Sections);
832  IO.mapOptional("VAddr", Phdr.VAddr, Hex64(0));
833  IO.mapOptional("PAddr", Phdr.PAddr, Hex64(0));
834  IO.mapOptional("Align", Phdr.Align);
835  IO.mapOptional("FileSize", Phdr.FileSize);
836  IO.mapOptional("MemSize", Phdr.MemSize);
837  IO.mapOptional("Offset", Phdr.Offset);
838 }
839 
840 namespace {
841 
842 struct NormalizedOther {
843  NormalizedOther(IO &)
844  : Visibility(ELFYAML::ELF_STV(0)), Other(ELFYAML::ELF_STO(0)) {}
845  NormalizedOther(IO &, uint8_t Original)
846  : Visibility(Original & 0x3), Other(Original & ~0x3) {}
847 
848  uint8_t denormalize(IO &) { return Visibility | Other; }
849 
850  ELFYAML::ELF_STV Visibility;
851  ELFYAML::ELF_STO Other;
852 };
853 
854 } // end anonymous namespace
855 
857  IO.mapOptional("Name", Symbol.Name, StringRef());
858  IO.mapOptional("NameIndex", Symbol.NameIndex);
859  IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
860  IO.mapOptional("Section", Symbol.Section, StringRef());
861  IO.mapOptional("Index", Symbol.Index);
862  IO.mapOptional("Binding", Symbol.Binding, ELFYAML::ELF_STB(0));
863  IO.mapOptional("Value", Symbol.Value, Hex64(0));
864  IO.mapOptional("Size", Symbol.Size, Hex64(0));
865  MappingNormalization<NormalizedOther, uint8_t> Keys(IO, Symbol.Other);
866  IO.mapOptional("Visibility", Keys->Visibility, ELFYAML::ELF_STV(0));
867  IO.mapOptional("Other", Keys->Other, ELFYAML::ELF_STO(0));
868 }
869 
872  if (Symbol.Index && Symbol.Section.data())
873  return "Index and Section cannot both be specified for Symbol";
874  if (Symbol.Index && *Symbol.Index == ELFYAML::ELF_SHN(ELF::SHN_XINDEX))
875  return "Large indexes are not supported";
876  if (Symbol.NameIndex && !Symbol.Name.empty())
877  return "Name and NameIndex cannot both be specified for Symbol";
878  return StringRef();
879 }
880 
882  IO.mapOptional("Name", Section.Name, StringRef());
883  IO.mapRequired("Type", Section.Type);
884  IO.mapOptional("Flags", Section.Flags, ELFYAML::ELF_SHF(0));
885  IO.mapOptional("Address", Section.Address, Hex64(0));
886  IO.mapOptional("Link", Section.Link, StringRef());
887  IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
888  IO.mapOptional("EntSize", Section.EntSize);
889 }
890 
892  commonSectionMapping(IO, Section);
893  IO.mapOptional("Entries", Section.Entries);
894  IO.mapOptional("Content", Section.Content);
895 }
896 
898  commonSectionMapping(IO, Section);
899  IO.mapOptional("Content", Section.Content);
900  IO.mapOptional("Size", Section.Size, Hex64(Section.Content.binary_size()));
901  IO.mapOptional("Info", Section.Info, Hex64(0));
902 }
903 
905  commonSectionMapping(IO, Section);
906  IO.mapOptional("Size", Section.Size, Hex64(0));
907 }
908 
910  commonSectionMapping(IO, Section);
911  IO.mapRequired("Info", Section.Info);
912  IO.mapRequired("Entries", Section.Entries);
913 }
914 
916  commonSectionMapping(IO, Section);
917  IO.mapRequired("Entries", Section.Entries);
918 }
919 
921  commonSectionMapping(IO, Section);
922  IO.mapRequired("Info", Section.Info);
923  IO.mapRequired("Dependencies", Section.VerneedV);
924 }
925 
927  commonSectionMapping(IO, Section);
928  IO.mapOptional("Info", Section.RelocatableSec, StringRef());
929  IO.mapOptional("Relocations", Section.Relocations);
930 }
931 
932 static void groupSectionMapping(IO &IO, ELFYAML::Group &Group) {
933  commonSectionMapping(IO, Group);
934  IO.mapOptional("Info", Group.Signature, StringRef());
935  IO.mapRequired("Members", Group.Members);
936 }
937 
939  IO &IO, ELFYAML::SectionOrType &sectionOrType) {
940  IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType);
941 }
942 
944  IO &IO, ELFYAML::SectionName &sectionName) {
945  IO.mapRequired("Section", sectionName.Section);
946 }
947 
949  commonSectionMapping(IO, Section);
950  IO.mapOptional("Version", Section.Version, Hex16(0));
951  IO.mapRequired("ISA", Section.ISALevel);
952  IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0));
953  IO.mapOptional("ISAExtension", Section.ISAExtension,
954  ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE));
955  IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0));
956  IO.mapOptional("FpABI", Section.FpABI,
957  ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY));
958  IO.mapOptional("GPRSize", Section.GPRSize,
959  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
960  IO.mapOptional("CPR1Size", Section.CPR1Size,
961  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
962  IO.mapOptional("CPR2Size", Section.CPR2Size,
963  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
964  IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0));
965  IO.mapOptional("Flags2", Section.Flags2, Hex32(0));
966 }
967 
969  IO &IO, std::unique_ptr<ELFYAML::Section> &Section) {
970  ELFYAML::ELF_SHT sectionType;
971  if (IO.outputting())
972  sectionType = Section->Type;
973  else
974  IO.mapRequired("Type", sectionType);
975 
976  switch (sectionType) {
977  case ELF::SHT_DYNAMIC:
978  if (!IO.outputting())
979  Section.reset(new ELFYAML::DynamicSection());
980  sectionMapping(IO, *cast<ELFYAML::DynamicSection>(Section.get()));
981  break;
982  case ELF::SHT_REL:
983  case ELF::SHT_RELA:
984  if (!IO.outputting())
985  Section.reset(new ELFYAML::RelocationSection());
986  sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get()));
987  break;
988  case ELF::SHT_GROUP:
989  if (!IO.outputting())
990  Section.reset(new ELFYAML::Group());
991  groupSectionMapping(IO, *cast<ELFYAML::Group>(Section.get()));
992  break;
993  case ELF::SHT_NOBITS:
994  if (!IO.outputting())
995  Section.reset(new ELFYAML::NoBitsSection());
996  sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get()));
997  break;
999  if (!IO.outputting())
1000  Section.reset(new ELFYAML::MipsABIFlags());
1001  sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get()));
1002  break;
1003  case ELF::SHT_GNU_verdef:
1004  if (!IO.outputting())
1005  Section.reset(new ELFYAML::VerdefSection());
1006  sectionMapping(IO, *cast<ELFYAML::VerdefSection>(Section.get()));
1007  break;
1008  case ELF::SHT_GNU_versym:
1009  if (!IO.outputting())
1010  Section.reset(new ELFYAML::SymverSection());
1011  sectionMapping(IO, *cast<ELFYAML::SymverSection>(Section.get()));
1012  break;
1013  case ELF::SHT_GNU_verneed:
1014  if (!IO.outputting())
1015  Section.reset(new ELFYAML::VerneedSection());
1016  sectionMapping(IO, *cast<ELFYAML::VerneedSection>(Section.get()));
1017  break;
1018  default:
1019  if (!IO.outputting())
1020  Section.reset(new ELFYAML::RawContentSection());
1021  sectionMapping(IO, *cast<ELFYAML::RawContentSection>(Section.get()));
1022  }
1023 }
1024 
1026  IO &io, std::unique_ptr<ELFYAML::Section> &Section) {
1027  const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(Section.get());
1028  if (!RawSection || RawSection->Size >= RawSection->Content.binary_size())
1029  return StringRef();
1030  return "Section size must be greater or equal to the content size";
1031 }
1032 
1033 namespace {
1034 
1035 struct NormalizedMips64RelType {
1036  NormalizedMips64RelType(IO &)
1037  : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1038  Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1039  Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1040  SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {}
1041  NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original)
1042  : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF),
1043  Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {}
1044 
1045  ELFYAML::ELF_REL denormalize(IO &) {
1046  ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24;
1047  return Res;
1048  }
1049 
1050  ELFYAML::ELF_REL Type;
1051  ELFYAML::ELF_REL Type2;
1052  ELFYAML::ELF_REL Type3;
1053  ELFYAML::ELF_RSS SpecSym;
1054 };
1055 
1056 } // end anonymous namespace
1057 
1059  ELFYAML::DynamicEntry &Rel) {
1060  assert(IO.getContext() && "The IO context is not initialized");
1061 
1062  IO.mapRequired("Tag", Rel.Tag);
1063  IO.mapRequired("Value", Rel.Val);
1064 }
1065 
1068  assert(IO.getContext() && "The IO context is not initialized");
1069 
1070  IO.mapRequired("Version", E.Version);
1071  IO.mapRequired("Flags", E.Flags);
1072  IO.mapRequired("VersionNdx", E.VersionNdx);
1073  IO.mapRequired("Hash", E.Hash);
1074  IO.mapRequired("Names", E.VerNames);
1075 }
1076 
1079  assert(IO.getContext() && "The IO context is not initialized");
1080 
1081  IO.mapRequired("Version", E.Version);
1082  IO.mapRequired("File", E.File);
1083  IO.mapRequired("Entries", E.AuxV);
1084 }
1085 
1088  assert(IO.getContext() && "The IO context is not initialized");
1089 
1090  IO.mapRequired("Name", E.Name);
1091  IO.mapRequired("Hash", E.Hash);
1092  IO.mapRequired("Flags", E.Flags);
1093  IO.mapRequired("Other", E.Other);
1094 }
1095 
1097  ELFYAML::Relocation &Rel) {
1098  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
1099  assert(Object && "The IO context is not initialized");
1100 
1101  IO.mapRequired("Offset", Rel.Offset);
1102  IO.mapOptional("Symbol", Rel.Symbol);
1103 
1104  if (Object->Header.Machine == ELFYAML::ELF_EM(ELF::EM_MIPS) &&
1105  Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
1106  MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key(
1107  IO, Rel.Type);
1108  IO.mapRequired("Type", Key->Type);
1109  IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
1110  IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
1111  IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF));
1112  } else
1113  IO.mapRequired("Type", Rel.Type);
1114 
1115  IO.mapOptional("Addend", Rel.Addend, (int64_t)0);
1116 }
1117 
1119  assert(!IO.getContext() && "The IO context is initialized already");
1120  IO.setContext(&Object);
1121  IO.mapTag("!ELF", true);
1122  IO.mapRequired("FileHeader", Object.Header);
1123  IO.mapOptional("ProgramHeaders", Object.ProgramHeaders);
1124  IO.mapOptional("Sections", Object.Sections);
1125  IO.mapOptional("Symbols", Object.Symbols);
1126  IO.mapOptional("DynamicSymbols", Object.DynamicSymbols);
1127  IO.setContext(nullptr);
1128 }
1129 
1130 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
1131 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
1132 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
1133 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
1134 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
1135 
1136 } // end namespace yaml
1137 
1138 } // 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:500
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:89
static void enumeration(IO &IO, ELFYAML::ELF_STT &Value)
Definition: ELFYAML.cpp:576
static void enumeration(IO &IO, ELFYAML::ELF_STV &Value)
Definition: ELFYAML.cpp:591
Optional< ELF_SHN > Index
Definition: ELFYAML.h:101
static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value)
Definition: ELFYAML.cpp:543
llvm::yaml::Hex64 PAddr
Definition: ELFYAML.h:88
static void bitset(IO &IO, ELFYAML::ELF_PF &Value)
Definition: ELFYAML.cpp:492
std::vector< SectionName > Sections
Definition: ELFYAML.h:93
llvm::yaml::Hex16 Version
Definition: ELFYAML.h:261
ELFYAML::ELF_REL Type3
Definition: ELFYAML.cpp:1052
llvm::yaml::Hex64 Info
Definition: ELFYAML.h:219
llvm::yaml::Hex64 Address
Definition: ELFYAML.h:133
ELFYAML::ELF_STV Visibility
Definition: ELFYAML.cpp:850
static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value)
Definition: ELFYAML.cpp:620
FileHeader Header
Definition: ELFYAML.h:281
static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value)
Definition: ELFYAML.cpp:427
llvm::yaml::Hex64 Entry
Definition: ELFYAML.h:77
static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value)
Definition: ELFYAML.cpp:808
llvm::yaml::Hex64 AddressAlign
Definition: ELFYAML.h:135
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:166
std::vector< DynamicEntry > Entries
Definition: ELFYAML.h:143
StringRef Section
Definition: ELFYAML.h:100
static void groupSectionMapping(IO &IO, ELFYAML::Group &Group)
Definition: ELFYAML.cpp:932
static void enumeration(IO &IO, ELFYAML::ELF_EM &Value)
Definition: ELFYAML.cpp:57
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:104
std::vector< Symbol > Symbols
Definition: ELFYAML.h:288
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
StringRef Name
Definition: ELFYAML.h:97
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:851
llvm::yaml::Hex8 ABIVersion
Definition: ELFYAML.h:73
llvm::yaml::Hex64 Info
Definition: ELFYAML.h:190
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:136
ELFYAML::ELF_RSS SpecSym
Definition: ELFYAML.cpp:1053
static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value)
Definition: ELFYAML.cpp:778
static void bitset(IO &IO, ELFYAML::ELF_STO &Value)
Definition: ELFYAML.cpp:601
std::vector< ProgramHeader > ProgramHeaders
Definition: ELFYAML.h:282
std::vector< VernauxEntry > AuxV
Definition: ELFYAML.h:185
static void commonSectionMapping(IO &IO, ELFYAML::Section &Section)
Definition: ELFYAML.cpp:881
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:677
llvm::yaml::Hex32 Flags2
Definition: ELFYAML.h:271
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define BCase(X)
static void enumeration(IO &IO, ELFYAML::ELF_REL &Value)
Definition: ELFYAML.cpp:630
llvm::yaml::Hex64 Info
Definition: ELFYAML.h:156
std::vector< VerneedEntry > VerneedV
Definition: ELFYAML.h:189
std::vector< StringRef > VerNames
Definition: ELFYAML.h:214
StringRef Signature
Definition: ELFYAML.h:232
static void sectionMapping(IO &IO, ELFYAML::DynamicSection &Section)
Definition: ELFYAML.cpp:891
std::vector< std::unique_ptr< Section > > Sections
Definition: ELFYAML.h:283
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:144
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
llvm::yaml::Hex8 ISARevision
Definition: ELFYAML.h:263
ArrayRef< uint8_t >::size_type binary_size() const
The number of bytes that are represented by this BinaryRef.
Definition: YAML.h:80
Optional< llvm::yaml::Hex64 > MemSize
Definition: ELFYAML.h:91
static void bitset(IO &IO, ELFYAML::ELF_EF &Value)
Definition: ELFYAML.cpp:273
ELF_ELFCLASS Class
Definition: ELFYAML.h:70
std::vector< SectionOrType > Members
Definition: ELFYAML.h:231
llvm::yaml::Hex64 Offset
Definition: ELFYAML.h:242
std::vector< Symbol > DynamicSymbols
Definition: ELFYAML.h:289
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:789
static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value)
Definition: ELFYAML.cpp:752
#define ECase(X)
ELFYAML::ELF_REL Type2
Definition: ELFYAML.cpp:1051
static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value)
Definition: ELFYAML.cpp:738
MIPS_AFL_EXT ISAExtension
Definition: ELFYAML.h:268
llvm::yaml::Hex64 Value
Definition: ELFYAML.h:103
static void enumeration(IO &IO, ELFYAML::ELF_STB &Value)
Definition: ELFYAML.cpp:565
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:332
Optional< llvm::yaml::Hex64 > FileSize
Definition: ELFYAML.h:90
std::vector< VerdefEntry > Entries
Definition: ELFYAML.h:218
Optional< uint32_t > NameIndex
Definition: ELFYAML.h:98
MIPS_AFL_FLAGS1 Flags1
Definition: ELFYAML.h:270
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:155
llvm::yaml::Hex64 Val
Definition: ELFYAML.h:114
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
std::vector< uint16_t > Entries
Definition: ELFYAML.h:200
static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value)
Definition: ELFYAML.cpp:728
ELF_ELFOSABI OSABI
Definition: ELFYAML.h:72
llvm::yaml::Hex64 VAddr
Definition: ELFYAML.h:87
#define BCaseMask(X, M)
Optional< StringRef > Symbol
Definition: ELFYAML.h:245
std::vector< Relocation > Relocations
Definition: ELFYAML.h:249
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
Optional< llvm::yaml::Hex64 > Offset
Definition: ELFYAML.h:92