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