LLVM  10.0.0
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/MapVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/BinaryFormat/ELF.h"
17 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/WithColor.h"
22 #include <cassert>
23 #include <cstdint>
24 
25 namespace llvm {
26 
27 ELFYAML::Chunk::~Chunk() = default;
28 
29 namespace yaml {
30 
32  IO &IO, ELFYAML::ELF_ET &Value) {
33 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
34  ECase(ET_NONE);
35  ECase(ET_REL);
36  ECase(ET_EXEC);
37  ECase(ET_DYN);
38  ECase(ET_CORE);
39 #undef ECase
40  IO.enumFallback<Hex16>(Value);
41 }
42 
44  IO &IO, ELFYAML::ELF_PT &Value) {
45 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
46  ECase(PT_NULL);
47  ECase(PT_LOAD);
50  ECase(PT_NOTE);
51  ECase(PT_SHLIB);
52  ECase(PT_PHDR);
53  ECase(PT_TLS);
58 #undef ECase
59  IO.enumFallback<Hex32>(Value);
60 }
61 
63  IO &IO, ELFYAML::ELF_EM &Value) {
64 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
65  ECase(EM_NONE);
66  ECase(EM_M32);
67  ECase(EM_SPARC);
68  ECase(EM_386);
69  ECase(EM_68K);
70  ECase(EM_88K);
71  ECase(EM_IAMCU);
72  ECase(EM_860);
73  ECase(EM_MIPS);
74  ECase(EM_S370);
79  ECase(EM_960);
80  ECase(EM_PPC);
81  ECase(EM_PPC64);
82  ECase(EM_S390);
83  ECase(EM_SPU);
84  ECase(EM_V800);
85  ECase(EM_FR20);
86  ECase(EM_RH32);
87  ECase(EM_RCE);
88  ECase(EM_ARM);
89  ECase(EM_ALPHA);
90  ECase(EM_SH);
93  ECase(EM_ARC);
96  ECase(EM_H8S);
98  ECase(EM_IA_64);
101  ECase(EM_68HC12);
102  ECase(EM_MMA);
103  ECase(EM_PCP);
104  ECase(EM_NCPU);
105  ECase(EM_NDR1);
107  ECase(EM_ME16);
108  ECase(EM_ST100);
109  ECase(EM_TINYJ);
110  ECase(EM_X86_64);
111  ECase(EM_PDSP);
112  ECase(EM_PDP10);
113  ECase(EM_PDP11);
114  ECase(EM_FX66);
115  ECase(EM_ST9PLUS);
116  ECase(EM_ST7);
117  ECase(EM_68HC16);
118  ECase(EM_68HC11);
119  ECase(EM_68HC08);
120  ECase(EM_68HC05);
121  ECase(EM_SVX);
122  ECase(EM_ST19);
123  ECase(EM_VAX);
124  ECase(EM_CRIS);
125  ECase(EM_JAVELIN);
127  ECase(EM_ZSP);
128  ECase(EM_MMIX);
129  ECase(EM_HUANY);
130  ECase(EM_PRISM);
131  ECase(EM_AVR);
132  ECase(EM_FR30);
133  ECase(EM_D10V);
134  ECase(EM_D30V);
135  ECase(EM_V850);
136  ECase(EM_M32R);
137  ECase(EM_MN10300);
138  ECase(EM_MN10200);
139  ECase(EM_PJ);
142  ECase(EM_XTENSA);
144  ECase(EM_TMM_GPP);
145  ECase(EM_NS32K);
146  ECase(EM_TPC);
147  ECase(EM_SNP1K);
148  ECase(EM_ST200);
149  ECase(EM_IP2K);
150  ECase(EM_MAX);
151  ECase(EM_CR);
152  ECase(EM_F2MC16);
153  ECase(EM_MSP430);
155  ECase(EM_SE_C33);
156  ECase(EM_SEP);
157  ECase(EM_ARCA);
158  ECase(EM_UNICORE);
159  ECase(EM_EXCESS);
160  ECase(EM_DXP);
162  ECase(EM_CRX);
163  ECase(EM_XGATE);
164  ECase(EM_C166);
165  ECase(EM_M16C);
167  ECase(EM_CE);
168  ECase(EM_M32C);
169  ECase(EM_TSK3000);
170  ECase(EM_RS08);
171  ECase(EM_SHARC);
172  ECase(EM_ECOG2);
173  ECase(EM_SCORE7);
174  ECase(EM_DSP24);
177  ECase(EM_SE_C17);
183  ECase(EM_R32C);
185  ECase(EM_HEXAGON);
186  ECase(EM_8051);
187  ECase(EM_STXP7X);
188  ECase(EM_NDS32);
189  ECase(EM_ECOG1);
190  ECase(EM_ECOG1X);
191  ECase(EM_MAXQ30);
192  ECase(EM_XIMO16);
193  ECase(EM_MANIK);
194  ECase(EM_CRAYNV2);
195  ECase(EM_RX);
196  ECase(EM_METAG);
198  ECase(EM_ECOG16);
199  ECase(EM_CR16);
200  ECase(EM_ETPU);
201  ECase(EM_SLE9X);
202  ECase(EM_L10M);
203  ECase(EM_K10M);
204  ECase(EM_AARCH64);
205  ECase(EM_AVR32);
206  ECase(EM_STM8);
207  ECase(EM_TILE64);
208  ECase(EM_TILEPRO);
209  ECase(EM_CUDA);
210  ECase(EM_TILEGX);
215  ECase(EM_OPEN8);
216  ECase(EM_RL78);
218  ECase(EM_78KOR);
219  ECase(EM_56800EX);
220  ECase(EM_AMDGPU);
221  ECase(EM_RISCV);
222  ECase(EM_LANAI);
223  ECase(EM_BPF);
224 #undef ECase
225  IO.enumFallback<Hex16>(Value);
226 }
227 
229  IO &IO, ELFYAML::ELF_ELFCLASS &Value) {
230 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
231  // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it
232  // here.
233  ECase(ELFCLASS32);
234  ECase(ELFCLASS64);
235 #undef ECase
236 }
237 
239  IO &IO, ELFYAML::ELF_ELFDATA &Value) {
240 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
241  // ELFDATANONE is an invalid data encoding, but we accept it because
242  // we want to be able to produce invalid binaries for the tests.
246 #undef ECase
247 }
248 
250  IO &IO, ELFYAML::ELF_ELFOSABI &Value) {
251 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
277 #undef ECase
278  IO.enumFallback<Hex8>(Value);
279 }
280 
282  ELFYAML::ELF_EF &Value) {
283  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
284  assert(Object && "The IO context is not initialized");
285 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
286 #define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M)
287  switch (Object->Header.Machine) {
288  case ELF::EM_ARM:
297  break;
298  case ELF::EM_MIPS:
342  break;
343  case ELF::EM_HEXAGON:
360  break;
361  case ELF::EM_AVR:
379  break;
380  case ELF::EM_RISCV:
387  break;
388  case ELF::EM_AMDGPU:
428  break;
429  case ELF::EM_X86_64:
430  break;
431  default:
432  llvm_unreachable("Unsupported architecture");
433  }
434 #undef BCase
435 #undef BCaseMask
436 }
437 
439  IO &IO, ELFYAML::ELF_SHT &Value) {
440  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
441  assert(Object && "The IO context is not initialized");
442 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
443  ECase(SHT_NULL);
445  ECase(SHT_SYMTAB);
446  // FIXME: Issue a diagnostic with this information.
447  ECase(SHT_STRTAB);
448  ECase(SHT_RELA);
449  ECase(SHT_HASH);
451  ECase(SHT_NOTE);
452  ECase(SHT_NOBITS);
453  ECase(SHT_REL);
454  ECase(SHT_SHLIB);
455  ECase(SHT_DYNSYM);
459  ECase(SHT_GROUP);
461  ECase(SHT_RELR);
478  switch (Object->Header.Machine) {
479  case ELF::EM_ARM:
485  break;
486  case ELF::EM_HEXAGON:
488  break;
489  case ELF::EM_X86_64:
491  break;
492  case ELF::EM_MIPS:
497  break;
498  default:
499  // Nothing to do.
500  break;
501  }
502 #undef ECase
503  IO.enumFallback<Hex32>(Value);
504 }
505 
507  ELFYAML::ELF_PF &Value) {
508 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
509  BCase(PF_X);
510  BCase(PF_W);
511  BCase(PF_R);
512 }
513 
515  ELFYAML::ELF_SHF &Value) {
516  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
517 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
518  BCase(SHF_WRITE);
519  BCase(SHF_ALLOC);
522  BCase(SHF_MERGE);
527  BCase(SHF_GROUP);
528  BCase(SHF_TLS);
530  switch (Object->Header.Machine) {
531  case ELF::EM_ARM:
533  break;
534  case ELF::EM_HEXAGON:
536  break;
537  case ELF::EM_MIPS:
546  break;
547  case ELF::EM_X86_64:
549  break;
550  default:
551  // Nothing to do.
552  break;
553  }
554 #undef BCase
555 }
556 
558  IO &IO, ELFYAML::ELF_SHN &Value) {
559 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
560  ECase(SHN_UNDEF);
562  ECase(SHN_LOPROC);
563  ECase(SHN_HIPROC);
564  ECase(SHN_LOOS);
565  ECase(SHN_HIOS);
566  ECase(SHN_ABS);
567  ECase(SHN_COMMON);
568  ECase(SHN_XINDEX);
576 #undef ECase
577  IO.enumFallback<Hex16>(Value);
578 }
579 
581  IO &IO, ELFYAML::ELF_STB &Value) {
582 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
583  ECase(STB_LOCAL);
584  ECase(STB_GLOBAL);
585  ECase(STB_WEAK);
587 #undef ECase
588  IO.enumFallback<Hex8>(Value);
589 }
590 
592  IO &IO, ELFYAML::ELF_STT &Value) {
593 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
594  ECase(STT_NOTYPE);
595  ECase(STT_OBJECT);
596  ECase(STT_FUNC);
598  ECase(STT_FILE);
599  ECase(STT_COMMON);
600  ECase(STT_TLS);
602 #undef ECase
603  IO.enumFallback<Hex8>(Value);
604 }
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  case ELF::EM_PPC64:
658 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
659  break;
660  default:
661  // Nothing to do.
662  break;
663  }
664 #undef ELF_RELOC
665  IO.enumFallback<Hex32>(Value);
666 }
667 
669  IO &IO, ELFYAML::ELF_DYNTAG &Value) {
670  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
671  assert(Object && "The IO context is not initialized");
672 
673 // Disable architecture specific tags by default. We might enable them below.
674 #define AARCH64_DYNAMIC_TAG(name, value)
675 #define MIPS_DYNAMIC_TAG(name, value)
676 #define HEXAGON_DYNAMIC_TAG(name, value)
677 #define PPC_DYNAMIC_TAG(name, value)
678 #define PPC64_DYNAMIC_TAG(name, value)
679 // Ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
680 #define DYNAMIC_TAG_MARKER(name, value)
681 
682 #define STRINGIFY(X) (#X)
683 #define DYNAMIC_TAG(X, Y) IO.enumCase(Value, STRINGIFY(DT_##X), ELF::DT_##X);
684  switch (Object->Header.Machine) {
685  case ELF::EM_AARCH64:
686 #undef AARCH64_DYNAMIC_TAG
687 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
688 #include "llvm/BinaryFormat/DynamicTags.def"
689 #undef AARCH64_DYNAMIC_TAG
690 #define AARCH64_DYNAMIC_TAG(name, value)
691  break;
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_PPC:
707 #undef PPC_DYNAMIC_TAG
708 #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
709 #include "llvm/BinaryFormat/DynamicTags.def"
710 #undef PPC_DYNAMIC_TAG
711 #define PPC_DYNAMIC_TAG(name, value)
712  break;
713  case ELF::EM_PPC64:
714 #undef PPC64_DYNAMIC_TAG
715 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
716 #include "llvm/BinaryFormat/DynamicTags.def"
717 #undef PPC64_DYNAMIC_TAG
718 #define PPC64_DYNAMIC_TAG(name, value)
719  break;
720  default:
721 #include "llvm/BinaryFormat/DynamicTags.def"
722  break;
723  }
724 #undef AARCH64_DYNAMIC_TAG
725 #undef MIPS_DYNAMIC_TAG
726 #undef HEXAGON_DYNAMIC_TAG
727 #undef PPC_DYNAMIC_TAG
728 #undef PPC64_DYNAMIC_TAG
729 #undef DYNAMIC_TAG_MARKER
730 #undef STRINGIFY
731 #undef DYNAMIC_TAG
732 
733  IO.enumFallback<Hex64>(Value);
734 }
735 
737  IO &IO, ELFYAML::MIPS_AFL_REG &Value) {
738 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
739  ECase(REG_NONE);
740  ECase(REG_32);
741  ECase(REG_64);
742  ECase(REG_128);
743 #undef ECase
744 }
745 
747  IO &IO, ELFYAML::MIPS_ABI_FP &Value) {
748 #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X)
749  ECase(FP_ANY);
750  ECase(FP_DOUBLE);
751  ECase(FP_SINGLE);
752  ECase(FP_SOFT);
753  ECase(FP_OLD_64);
754  ECase(FP_XX);
755  ECase(FP_64);
756  ECase(FP_64A);
757 #undef ECase
758 }
759 
761  IO &IO, ELFYAML::MIPS_AFL_EXT &Value) {
762 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
763  ECase(EXT_NONE);
764  ECase(EXT_XLR);
765  ECase(EXT_OCTEON2);
766  ECase(EXT_OCTEONP);
767  ECase(EXT_LOONGSON_3A);
768  ECase(EXT_OCTEON);
769  ECase(EXT_5900);
770  ECase(EXT_4650);
771  ECase(EXT_4010);
772  ECase(EXT_4100);
773  ECase(EXT_3900);
774  ECase(EXT_10000);
775  ECase(EXT_SB1);
776  ECase(EXT_4111);
777  ECase(EXT_4120);
778  ECase(EXT_5400);
779  ECase(EXT_5500);
780  ECase(EXT_LOONGSON_2E);
781  ECase(EXT_LOONGSON_2F);
782  ECase(EXT_OCTEON3);
783 #undef ECase
784 }
785 
787  IO &IO, ELFYAML::MIPS_ISA &Value) {
788  IO.enumCase(Value, "MIPS1", 1);
789  IO.enumCase(Value, "MIPS2", 2);
790  IO.enumCase(Value, "MIPS3", 3);
791  IO.enumCase(Value, "MIPS4", 4);
792  IO.enumCase(Value, "MIPS5", 5);
793  IO.enumCase(Value, "MIPS32", 32);
794  IO.enumCase(Value, "MIPS64", 64);
795 }
796 
798  IO &IO, ELFYAML::MIPS_AFL_ASE &Value) {
799 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X)
800  BCase(DSP);
801  BCase(DSPR2);
802  BCase(EVA);
803  BCase(MCU);
804  BCase(MDMX);
805  BCase(MIPS3D);
806  BCase(MT);
807  BCase(SMARTMIPS);
808  BCase(VIRT);
809  BCase(MSA);
810  BCase(MIPS16);
811  BCase(MICROMIPS);
812  BCase(XPA);
813 #undef BCase
814 }
815 
817  IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) {
818 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X)
819  BCase(ODDSPREG);
820 #undef BCase
821 }
822 
824  ELFYAML::FileHeader &FileHdr) {
825  IO.mapRequired("Class", FileHdr.Class);
826  IO.mapRequired("Data", FileHdr.Data);
827  IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0));
828  IO.mapOptional("ABIVersion", FileHdr.ABIVersion, Hex8(0));
829  IO.mapRequired("Type", FileHdr.Type);
830  IO.mapRequired("Machine", FileHdr.Machine);
831  IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0));
832  IO.mapOptional("Entry", FileHdr.Entry, Hex64(0));
833 
834  IO.mapOptional("SHEntSize", FileHdr.SHEntSize);
835  IO.mapOptional("SHOff", FileHdr.SHOff);
836  IO.mapOptional("SHNum", FileHdr.SHNum);
837  IO.mapOptional("SHStrNdx", FileHdr.SHStrNdx);
838 }
839 
841  IO &IO, ELFYAML::ProgramHeader &Phdr) {
842  IO.mapRequired("Type", Phdr.Type);
843  IO.mapOptional("Flags", Phdr.Flags, ELFYAML::ELF_PF(0));
844  IO.mapOptional("Sections", Phdr.Sections);
845  IO.mapOptional("VAddr", Phdr.VAddr, Hex64(0));
846  IO.mapOptional("PAddr", Phdr.PAddr, Hex64(0));
847  IO.mapOptional("Align", Phdr.Align);
848  IO.mapOptional("FileSize", Phdr.FileSize);
849  IO.mapOptional("MemSize", Phdr.MemSize);
850  IO.mapOptional("Offset", Phdr.Offset);
851 }
852 
853 LLVM_YAML_STRONG_TYPEDEF(StringRef, StOtherPiece)
854 
855 template <> struct ScalarTraits<StOtherPiece> {
856  static void output(const StOtherPiece &Val, void *, raw_ostream &Out) {
857  Out << Val;
858  }
859  static StringRef input(StringRef Scalar, void *, StOtherPiece &Val) {
860  Val = Scalar;
861  return {};
862  }
863  static QuotingType mustQuote(StringRef) { return QuotingType::None; }
864 };
865 template <> struct SequenceElementTraits<StOtherPiece> {
866  static const bool flow = true;
867 };
868 
869 template <> struct ScalarTraits<ELFYAML::YAMLFlowString> {
870  static void output(const ELFYAML::YAMLFlowString &Val, void *,
871  raw_ostream &Out) {
872  Out << Val;
873  }
874  static StringRef input(StringRef Scalar, void *,
875  ELFYAML::YAMLFlowString &Val) {
876  Val = Scalar;
877  return {};
878  }
879  static QuotingType mustQuote(StringRef S) {
880  return ScalarTraits<StringRef>::mustQuote(S);
881  }
882 };
883 template <> struct SequenceElementTraits<ELFYAML::YAMLFlowString> {
884  static const bool flow = true;
885 };
886 
887 namespace {
888 
889 struct NormalizedOther {
890  NormalizedOther(IO &IO) : YamlIO(IO) {}
891  NormalizedOther(IO &IO, Optional<uint8_t> Original) : YamlIO(IO) {
892  assert(Original && "This constructor is only used for outputting YAML and "
893  "assumes a non-empty Original");
894  std::vector<StOtherPiece> Ret;
895  const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext());
896  for (std::pair<StringRef, uint8_t> &P :
897  getFlags(Object->Header.Machine).takeVector()) {
898  uint8_t FlagValue = P.second;
899  if ((*Original & FlagValue) != FlagValue)
900  continue;
901  *Original &= ~FlagValue;
902  Ret.push_back({P.first});
903  }
904 
905  if (*Original != 0) {
906  UnknownFlagsHolder = std::to_string(*Original);
907  Ret.push_back({UnknownFlagsHolder});
908  }
909 
910  if (!Ret.empty())
911  Other = std::move(Ret);
912  }
913 
914  uint8_t toValue(StringRef Name) {
915  const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext());
916  MapVector<StringRef, uint8_t> Flags = getFlags(Object->Header.Machine);
917 
918  auto It = Flags.find(Name);
919  if (It != Flags.end())
920  return It->second;
921 
922  uint8_t Val;
923  if (to_integer(Name, Val))
924  return Val;
925 
926  YamlIO.setError("an unknown value is used for symbol's 'Other' field: " +
927  Name);
928  return 0;
929  }
930 
931  Optional<uint8_t> denormalize(IO &) {
932  if (!Other)
933  return None;
934  uint8_t Ret = 0;
935  for (StOtherPiece &Val : *Other)
936  Ret |= toValue(Val);
937  return Ret;
938  }
939 
940  // st_other field is used to encode symbol visibility and platform-dependent
941  // flags and values. This method returns a name to value map that is used for
942  // parsing and encoding this field.
943  MapVector<StringRef, uint8_t> getFlags(unsigned EMachine) {
945  // STV_* values are just enumeration values. We add them in a reversed order
946  // because when we convert the st_other to named constants when printing
947  // YAML we want to use a maximum number of bits on each step:
948  // when we have st_other == 3, we want to print it as STV_PROTECTED (3), but
949  // not as STV_HIDDEN (2) + STV_INTERNAL (1).
950  Map["STV_PROTECTED"] = ELF::STV_PROTECTED;
951  Map["STV_HIDDEN"] = ELF::STV_HIDDEN;
952  Map["STV_INTERNAL"] = ELF::STV_INTERNAL;
953  // STV_DEFAULT is used to represent the default visibility and has a value
954  // 0. We want to be able to read it from YAML documents, but there is no
955  // reason to print it.
956  if (!YamlIO.outputting())
957  Map["STV_DEFAULT"] = ELF::STV_DEFAULT;
958 
959  // MIPS is not consistent. All of the STO_MIPS_* values are bit flags,
960  // except STO_MIPS_MIPS16 which overlaps them. It should be checked and
961  // consumed first when we print the output, because we do not want to print
962  // any other flags that have the same bits instead.
963  if (EMachine == ELF::EM_MIPS) {
964  Map["STO_MIPS_MIPS16"] = ELF::STO_MIPS_MIPS16;
965  Map["STO_MIPS_MICROMIPS"] = ELF::STO_MIPS_MICROMIPS;
966  Map["STO_MIPS_PIC"] = ELF::STO_MIPS_PIC;
967  Map["STO_MIPS_PLT"] = ELF::STO_MIPS_PLT;
968  Map["STO_MIPS_OPTIONAL"] = ELF::STO_MIPS_OPTIONAL;
969  }
970  return Map;
971  }
972 
973  IO &YamlIO;
974  Optional<std::vector<StOtherPiece>> Other;
975  std::string UnknownFlagsHolder;
976 };
977 
978 } // end anonymous namespace
979 
981  IO.mapOptional("Name", Symbol.Name, StringRef());
982  IO.mapOptional("NameIndex", Symbol.NameIndex);
983  IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
984  IO.mapOptional("Section", Symbol.Section, StringRef());
985  IO.mapOptional("Index", Symbol.Index);
986  IO.mapOptional("Binding", Symbol.Binding, ELFYAML::ELF_STB(0));
987  IO.mapOptional("Value", Symbol.Value, Hex64(0));
988  IO.mapOptional("Size", Symbol.Size, Hex64(0));
989 
990  // Symbol's Other field is a bit special. It is usually a field that
991  // represents st_other and holds the symbol visibility. However, on some
992  // platforms, it can contain bit fields and regular values, or even sometimes a
993  // crazy mix of them (see comments for NormalizedOther). Because of this, we
994  // need special handling.
995  MappingNormalization<NormalizedOther, Optional<uint8_t>> Keys(IO,
996  Symbol.Other);
997  IO.mapOptional("Other", Keys->Other);
998 }
999 
1002  if (Symbol.Index && Symbol.Section.data())
1003  return "Index and Section cannot both be specified for Symbol";
1004  if (Symbol.NameIndex && !Symbol.Name.empty())
1005  return "Name and NameIndex cannot both be specified for Symbol";
1006  return StringRef();
1007 }
1008 
1010  IO.mapOptional("Name", Section.Name, StringRef());
1011  IO.mapRequired("Type", Section.Type);
1012  IO.mapOptional("Flags", Section.Flags);
1013  IO.mapOptional("Address", Section.Address, Hex64(0));
1014  IO.mapOptional("Link", Section.Link, StringRef());
1015  IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
1016  IO.mapOptional("EntSize", Section.EntSize);
1017 
1018  // obj2yaml does not dump these fields. They are expected to be empty when we
1019  // are producing YAML, because yaml2obj sets appropriate values for them
1020  // automatically when they are not explicitly defined.
1021  assert(!IO.outputting() ||
1022  (!Section.ShOffset.hasValue() && !Section.ShSize.hasValue() &&
1023  !Section.ShName.hasValue() && !Section.ShFlags.hasValue()));
1024  IO.mapOptional("ShName", Section.ShName);
1025  IO.mapOptional("ShOffset", Section.ShOffset);
1026  IO.mapOptional("ShSize", Section.ShSize);
1027  IO.mapOptional("ShFlags", Section.ShFlags);
1028 }
1029 
1031  commonSectionMapping(IO, Section);
1032  IO.mapOptional("Entries", Section.Entries);
1033  IO.mapOptional("Content", Section.Content);
1034 }
1035 
1037  commonSectionMapping(IO, Section);
1038  IO.mapOptional("Content", Section.Content);
1039  IO.mapOptional("Size", Section.Size);
1040  IO.mapOptional("Info", Section.Info);
1041 }
1042 
1044  commonSectionMapping(IO, Section);
1045  IO.mapOptional("Content", Section.Content);
1046  IO.mapOptional("Size", Section.Size);
1047  IO.mapOptional("Entries", Section.Entries);
1048 }
1049 
1051  commonSectionMapping(IO, Section);
1052  IO.mapOptional("Content", Section.Content);
1053  IO.mapOptional("Bucket", Section.Bucket);
1054  IO.mapOptional("Chain", Section.Chain);
1055  IO.mapOptional("Size", Section.Size);
1056 }
1057 
1059  commonSectionMapping(IO, Section);
1060  IO.mapOptional("Content", Section.Content);
1061  IO.mapOptional("Size", Section.Size);
1062  IO.mapOptional("Notes", Section.Notes);
1063 }
1064 
1065 
1067  commonSectionMapping(IO, Section);
1068  IO.mapOptional("Content", Section.Content);
1069  IO.mapOptional("Header", Section.Header);
1070  IO.mapOptional("BloomFilter", Section.BloomFilter);
1071  IO.mapOptional("HashBuckets", Section.HashBuckets);
1072  IO.mapOptional("HashValues", Section.HashValues);
1073 }
1075  commonSectionMapping(IO, Section);
1076  IO.mapOptional("Size", Section.Size, Hex64(0));
1077 }
1078 
1080  commonSectionMapping(IO, Section);
1081  IO.mapRequired("Info", Section.Info);
1082  IO.mapOptional("Entries", Section.Entries);
1083  IO.mapOptional("Content", Section.Content);
1084 }
1085 
1087  commonSectionMapping(IO, Section);
1088  IO.mapRequired("Entries", Section.Entries);
1089 }
1090 
1092  commonSectionMapping(IO, Section);
1093  IO.mapRequired("Info", Section.Info);
1094  IO.mapOptional("Dependencies", Section.VerneedV);
1095  IO.mapOptional("Content", Section.Content);
1096 }
1097 
1099  commonSectionMapping(IO, Section);
1100  IO.mapOptional("Info", Section.RelocatableSec, StringRef());
1101  IO.mapOptional("Relocations", Section.Relocations);
1102 }
1103 
1105  commonSectionMapping(IO, Section);
1106  IO.mapOptional("Entries", Section.Entries);
1107  IO.mapOptional("Content", Section.Content);
1108 }
1109 
1110 static void groupSectionMapping(IO &IO, ELFYAML::Group &Group) {
1111  commonSectionMapping(IO, Group);
1112  IO.mapOptional("Info", Group.Signature);
1113  IO.mapRequired("Members", Group.Members);
1114 }
1115 
1117  commonSectionMapping(IO, Section);
1118  IO.mapRequired("Entries", Section.Entries);
1119 }
1120 
1122  commonSectionMapping(IO, Section);
1123  IO.mapOptional("Content", Section.Content);
1124  IO.mapOptional("Size", Section.Size);
1125  IO.mapOptional("Symbols", Section.Symbols);
1126 }
1127 
1128 static void fillMapping(IO &IO, ELFYAML::Fill &Fill) {
1129  IO.mapOptional("Name", Fill.Name, StringRef());
1130  IO.mapOptional("Pattern", Fill.Pattern);
1131  IO.mapRequired("Size", Fill.Size);
1132 }
1133 
1135  commonSectionMapping(IO, Section);
1136  IO.mapOptional("Options", Section.Options);
1137  IO.mapOptional("Content", Section.Content);
1138 }
1139 
1140 static void sectionMapping(IO &IO,
1142  commonSectionMapping(IO, Section);
1143  IO.mapOptional("Libraries", Section.Libs);
1144  IO.mapOptional("Content", Section.Content);
1145 }
1146 
1148  IO &IO, ELFYAML::SectionOrType &sectionOrType) {
1149  IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType);
1150 }
1151 
1153  IO &IO, ELFYAML::SectionName &sectionName) {
1154  IO.mapRequired("Section", sectionName.Section);
1155 }
1156 
1158  commonSectionMapping(IO, Section);
1159  IO.mapOptional("Version", Section.Version, Hex16(0));
1160  IO.mapRequired("ISA", Section.ISALevel);
1161  IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0));
1162  IO.mapOptional("ISAExtension", Section.ISAExtension,
1163  ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE));
1164  IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0));
1165  IO.mapOptional("FpABI", Section.FpABI,
1166  ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY));
1167  IO.mapOptional("GPRSize", Section.GPRSize,
1168  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
1169  IO.mapOptional("CPR1Size", Section.CPR1Size,
1170  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
1171  IO.mapOptional("CPR2Size", Section.CPR2Size,
1172  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
1173  IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0));
1174  IO.mapOptional("Flags2", Section.Flags2, Hex32(0));
1175 }
1176 
1178  IO &IO, std::unique_ptr<ELFYAML::Chunk> &Section) {
1179  ELFYAML::ELF_SHT Type;
1180  if (IO.outputting()) {
1181  Type = cast<ELFYAML::Section>(Section.get())->Type;
1182  } else {
1183  // When the Type string does not have a "SHT_" prefix, we know it is not a
1184  // description of a regular ELF output section. Currently, we have one
1185  // special type named "Fill". See comments for Fill.
1186  StringRef StrType;
1187  IO.mapRequired("Type", StrType);
1188  if (StrType == "Fill") {
1189  Section.reset(new ELFYAML::Fill());
1190  fillMapping(IO, *cast<ELFYAML::Fill>(Section.get()));
1191  return;
1192  }
1193 
1194  IO.mapRequired("Type", Type);
1195  }
1196 
1197  switch (Type) {
1198  case ELF::SHT_DYNAMIC:
1199  if (!IO.outputting())
1200  Section.reset(new ELFYAML::DynamicSection());
1201  sectionMapping(IO, *cast<ELFYAML::DynamicSection>(Section.get()));
1202  break;
1203  case ELF::SHT_REL:
1204  case ELF::SHT_RELA:
1205  if (!IO.outputting())
1206  Section.reset(new ELFYAML::RelocationSection());
1207  sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get()));
1208  break;
1209  case ELF::SHT_RELR:
1210  if (!IO.outputting())
1211  Section.reset(new ELFYAML::RelrSection());
1212  sectionMapping(IO, *cast<ELFYAML::RelrSection>(Section.get()));
1213  break;
1214  case ELF::SHT_GROUP:
1215  if (!IO.outputting())
1216  Section.reset(new ELFYAML::Group());
1217  groupSectionMapping(IO, *cast<ELFYAML::Group>(Section.get()));
1218  break;
1219  case ELF::SHT_NOBITS:
1220  if (!IO.outputting())
1221  Section.reset(new ELFYAML::NoBitsSection());
1222  sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get()));
1223  break;
1224  case ELF::SHT_HASH:
1225  if (!IO.outputting())
1226  Section.reset(new ELFYAML::HashSection());
1227  sectionMapping(IO, *cast<ELFYAML::HashSection>(Section.get()));
1228  break;
1229  case ELF::SHT_NOTE:
1230  if (!IO.outputting())
1231  Section.reset(new ELFYAML::NoteSection());
1232  sectionMapping(IO, *cast<ELFYAML::NoteSection>(Section.get()));
1233  break;
1234  case ELF::SHT_GNU_HASH:
1235  if (!IO.outputting())
1236  Section.reset(new ELFYAML::GnuHashSection());
1237  sectionMapping(IO, *cast<ELFYAML::GnuHashSection>(Section.get()));
1238  break;
1240  if (!IO.outputting())
1241  Section.reset(new ELFYAML::MipsABIFlags());
1242  sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get()));
1243  break;
1244  case ELF::SHT_GNU_verdef:
1245  if (!IO.outputting())
1246  Section.reset(new ELFYAML::VerdefSection());
1247  sectionMapping(IO, *cast<ELFYAML::VerdefSection>(Section.get()));
1248  break;
1249  case ELF::SHT_GNU_versym:
1250  if (!IO.outputting())
1251  Section.reset(new ELFYAML::SymverSection());
1252  sectionMapping(IO, *cast<ELFYAML::SymverSection>(Section.get()));
1253  break;
1254  case ELF::SHT_GNU_verneed:
1255  if (!IO.outputting())
1256  Section.reset(new ELFYAML::VerneedSection());
1257  sectionMapping(IO, *cast<ELFYAML::VerneedSection>(Section.get()));
1258  break;
1259  case ELF::SHT_SYMTAB_SHNDX:
1260  if (!IO.outputting())
1261  Section.reset(new ELFYAML::SymtabShndxSection());
1262  sectionMapping(IO, *cast<ELFYAML::SymtabShndxSection>(Section.get()));
1263  break;
1264  case ELF::SHT_LLVM_ADDRSIG:
1265  if (!IO.outputting())
1266  Section.reset(new ELFYAML::AddrsigSection());
1267  sectionMapping(IO, *cast<ELFYAML::AddrsigSection>(Section.get()));
1268  break;
1270  if (!IO.outputting())
1271  Section.reset(new ELFYAML::LinkerOptionsSection());
1272  sectionMapping(IO, *cast<ELFYAML::LinkerOptionsSection>(Section.get()));
1273  break;
1275  if (!IO.outputting())
1276  Section.reset(new ELFYAML::DependentLibrariesSection());
1277  sectionMapping(IO,
1278  *cast<ELFYAML::DependentLibrariesSection>(Section.get()));
1279  break;
1280  default:
1281  if (!IO.outputting()) {
1282  StringRef Name;
1283  IO.mapOptional("Name", Name, StringRef());
1284  Name = ELFYAML::dropUniqueSuffix(Name);
1285 
1287  Section = std::make_unique<ELFYAML::StackSizesSection>();
1288  else
1289  Section = std::make_unique<ELFYAML::RawContentSection>();
1290  }
1291 
1292  if (auto S = dyn_cast<ELFYAML::RawContentSection>(Section.get()))
1293  sectionMapping(IO, *S);
1294  else
1295  sectionMapping(IO, *cast<ELFYAML::StackSizesSection>(Section.get()));
1296  }
1297 }
1298 
1300  IO &io, std::unique_ptr<ELFYAML::Chunk> &C) {
1301  if (const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(C.get())) {
1302  if (RawSection->Size && RawSection->Content &&
1303  (uint64_t)(*RawSection->Size) < RawSection->Content->binary_size())
1304  return "Section size must be greater than or equal to the content size";
1305  if (RawSection->Flags && RawSection->ShFlags)
1306  return "ShFlags and Flags cannot be used together";
1307  return {};
1308  }
1309 
1310  if (const auto *SS = dyn_cast<ELFYAML::StackSizesSection>(C.get())) {
1311  if (!SS->Entries && !SS->Content && !SS->Size)
1312  return ".stack_sizes: one of Content, Entries and Size must be specified";
1313 
1314  if (SS->Size && SS->Content &&
1315  (uint64_t)(*SS->Size) < SS->Content->binary_size())
1316  return ".stack_sizes: Size must be greater than or equal to the content "
1317  "size";
1318 
1319  // We accept Content, Size or both together when there are no Entries.
1320  if (!SS->Entries)
1321  return {};
1322 
1323  if (SS->Size)
1324  return ".stack_sizes: Size and Entries cannot be used together";
1325  if (SS->Content)
1326  return ".stack_sizes: Content and Entries cannot be used together";
1327  return {};
1328  }
1329 
1330  if (const auto *HS = dyn_cast<ELFYAML::HashSection>(C.get())) {
1331  if (!HS->Content && !HS->Bucket && !HS->Chain && !HS->Size)
1332  return "one of \"Content\", \"Size\", \"Bucket\" or \"Chain\" must be "
1333  "specified";
1334 
1335  if (HS->Content || HS->Size) {
1336  if (HS->Size && HS->Content &&
1337  (uint64_t)*HS->Size < HS->Content->binary_size())
1338  return "\"Size\" must be greater than or equal to the content "
1339  "size";
1340 
1341  if (HS->Bucket)
1342  return "\"Bucket\" cannot be used with \"Content\" or \"Size\"";
1343  if (HS->Chain)
1344  return "\"Chain\" cannot be used with \"Content\" or \"Size\"";
1345  return {};
1346  }
1347 
1348  if ((HS->Bucket && !HS->Chain) || (!HS->Bucket && HS->Chain))
1349  return "\"Bucket\" and \"Chain\" must be used together";
1350  return {};
1351  }
1352 
1353  if (const auto *Sec = dyn_cast<ELFYAML::AddrsigSection>(C.get())) {
1354  if (!Sec->Symbols && !Sec->Content && !Sec->Size)
1355  return "one of \"Content\", \"Size\" or \"Symbols\" must be specified";
1356 
1357  if (Sec->Content || Sec->Size) {
1358  if (Sec->Size && Sec->Content &&
1359  (uint64_t)*Sec->Size < Sec->Content->binary_size())
1360  return "\"Size\" must be greater than or equal to the content "
1361  "size";
1362 
1363  if (Sec->Symbols)
1364  return "\"Symbols\" cannot be used with \"Content\" or \"Size\"";
1365  return {};
1366  }
1367 
1368  if (!Sec->Symbols)
1369  return {};
1370 
1371  for (const ELFYAML::AddrsigSymbol &AS : *Sec->Symbols)
1372  if (AS.Index && AS.Name)
1373  return "\"Index\" and \"Name\" cannot be used together when defining a "
1374  "symbol";
1375  return {};
1376  }
1377 
1378  if (const auto *NS = dyn_cast<ELFYAML::NoteSection>(C.get())) {
1379  if (!NS->Content && !NS->Size && !NS->Notes)
1380  return "one of \"Content\", \"Size\" or \"Notes\" must be "
1381  "specified";
1382 
1383  if (!NS->Content && !NS->Size)
1384  return {};
1385 
1386  if (NS->Size && NS->Content &&
1387  (uint64_t)*NS->Size < NS->Content->binary_size())
1388  return "\"Size\" must be greater than or equal to the content "
1389  "size";
1390 
1391  if (NS->Notes)
1392  return "\"Notes\" cannot be used with \"Content\" or \"Size\"";
1393  return {};
1394  }
1395 
1396  if (const auto *Sec = dyn_cast<ELFYAML::GnuHashSection>(C.get())) {
1397  if (!Sec->Content && !Sec->Header && !Sec->BloomFilter &&
1398  !Sec->HashBuckets && !Sec->HashValues)
1399  return "either \"Content\" or \"Header\", \"BloomFilter\", "
1400  "\"HashBuckets\" and \"HashBuckets\" must be specified";
1401 
1402  if (Sec->Header || Sec->BloomFilter || Sec->HashBuckets ||
1403  Sec->HashValues) {
1404  if (!Sec->Header || !Sec->BloomFilter || !Sec->HashBuckets ||
1405  !Sec->HashValues)
1406  return "\"Header\", \"BloomFilter\", "
1407  "\"HashBuckets\" and \"HashValues\" must be used together";
1408  if (Sec->Content)
1409  return "\"Header\", \"BloomFilter\", "
1410  "\"HashBuckets\" and \"HashValues\" can't be used together with "
1411  "\"Content\"";
1412  return {};
1413  }
1414 
1415  // Only Content is specified.
1416  return {};
1417  }
1418 
1419  if (const auto *Sec = dyn_cast<ELFYAML::LinkerOptionsSection>(C.get())) {
1420  if (Sec->Options && Sec->Content)
1421  return "\"Options\" and \"Content\" can't be used together";
1422  return {};
1423  }
1424 
1425  if (const auto *Sec = dyn_cast<ELFYAML::DependentLibrariesSection>(C.get())) {
1426  if (Sec->Libs && Sec->Content)
1427  return "SHT_LLVM_DEPENDENT_LIBRARIES: \"Libraries\" and \"Content\" "
1428  "can't "
1429  "be used together";
1430  return {};
1431  }
1432 
1433  if (const auto *F = dyn_cast<ELFYAML::Fill>(C.get())) {
1434  if (!F->Pattern)
1435  return {};
1436  if (F->Pattern->binary_size() != 0 && !F->Size)
1437  return "\"Size\" can't be 0 when \"Pattern\" is not empty";
1438  return {};
1439  }
1440 
1441  if (const auto *VD = dyn_cast<ELFYAML::VerdefSection>(C.get())) {
1442  if (VD->Entries && VD->Content)
1443  return "SHT_GNU_verdef: \"Entries\" and \"Content\" can't be used "
1444  "together";
1445  return {};
1446  }
1447 
1448  if (const auto *VD = dyn_cast<ELFYAML::VerneedSection>(C.get())) {
1449  if (VD->VerneedV && VD->Content)
1450  return "SHT_GNU_verneed: \"Dependencies\" and \"Content\" can't be used "
1451  "together";
1452  return {};
1453  }
1454 
1455  if (const auto *RS = dyn_cast<ELFYAML::RelrSection>(C.get())) {
1456  if (RS->Entries && RS->Content)
1457  return "\"Entries\" and \"Content\" can't be used together";
1458  return {};
1459  }
1460 
1461  return {};
1462 }
1463 
1464 namespace {
1465 
1466 struct NormalizedMips64RelType {
1467  NormalizedMips64RelType(IO &)
1468  : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1469  Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1470  Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1471  SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {}
1472  NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original)
1473  : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF),
1474  Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {}
1475 
1476  ELFYAML::ELF_REL denormalize(IO &) {
1477  ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24;
1478  return Res;
1479  }
1480 
1481  ELFYAML::ELF_REL Type;
1482  ELFYAML::ELF_REL Type2;
1483  ELFYAML::ELF_REL Type3;
1484  ELFYAML::ELF_RSS SpecSym;
1485 };
1486 
1487 } // end anonymous namespace
1488 
1490  IO &IO, ELFYAML::StackSizeEntry &E) {
1491  assert(IO.getContext() && "The IO context is not initialized");
1492  IO.mapOptional("Address", E.Address, Hex64(0));
1493  IO.mapRequired("Size", E.Size);
1494 }
1495 
1498  assert(IO.getContext() && "The IO context is not initialized");
1499  IO.mapOptional("NBuckets", E.NBuckets);
1500  IO.mapRequired("SymNdx", E.SymNdx);
1501  IO.mapOptional("MaskWords", E.MaskWords);
1502  IO.mapRequired("Shift2", E.Shift2);
1503 }
1504 
1506  ELFYAML::DynamicEntry &Rel) {
1507  assert(IO.getContext() && "The IO context is not initialized");
1508 
1509  IO.mapRequired("Tag", Rel.Tag);
1510  IO.mapRequired("Value", Rel.Val);
1511 }
1512 
1514  assert(IO.getContext() && "The IO context is not initialized");
1515 
1516  IO.mapOptional("Name", N.Name);
1517  IO.mapOptional("Desc", N.Desc);
1518  IO.mapRequired("Type", N.Type);
1519 }
1520 
1523  assert(IO.getContext() && "The IO context is not initialized");
1524 
1525  IO.mapRequired("Version", E.Version);
1526  IO.mapRequired("Flags", E.Flags);
1527  IO.mapRequired("VersionNdx", E.VersionNdx);
1528  IO.mapRequired("Hash", E.Hash);
1529  IO.mapRequired("Names", E.VerNames);
1530 }
1531 
1534  assert(IO.getContext() && "The IO context is not initialized");
1535 
1536  IO.mapRequired("Version", E.Version);
1537  IO.mapRequired("File", E.File);
1538  IO.mapRequired("Entries", E.AuxV);
1539 }
1540 
1543  assert(IO.getContext() && "The IO context is not initialized");
1544 
1545  IO.mapRequired("Name", E.Name);
1546  IO.mapRequired("Hash", E.Hash);
1547  IO.mapRequired("Flags", E.Flags);
1548  IO.mapRequired("Other", E.Other);
1549 }
1550 
1552  ELFYAML::Relocation &Rel) {
1553  const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
1554  assert(Object && "The IO context is not initialized");
1555 
1556  IO.mapRequired("Offset", Rel.Offset);
1557  IO.mapOptional("Symbol", Rel.Symbol);
1558 
1559  if (Object->Header.Machine == ELFYAML::ELF_EM(ELF::EM_MIPS) &&
1560  Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
1561  MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key(
1562  IO, Rel.Type);
1563  IO.mapRequired("Type", Key->Type);
1564  IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
1565  IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
1566  IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF));
1567  } else
1568  IO.mapRequired("Type", Rel.Type);
1569 
1570  IO.mapOptional("Addend", Rel.Addend, (int64_t)0);
1571 }
1572 
1574  assert(!IO.getContext() && "The IO context is initialized already");
1575  IO.setContext(&Object);
1576  IO.mapTag("!ELF", true);
1577  IO.mapRequired("FileHeader", Object.Header);
1578  IO.mapOptional("ProgramHeaders", Object.ProgramHeaders);
1579  IO.mapOptional("Sections", Object.Chunks);
1580  IO.mapOptional("Symbols", Object.Symbols);
1581  IO.mapOptional("DynamicSymbols", Object.DynamicSymbols);
1582  IO.setContext(nullptr);
1583 }
1584 
1586  assert(IO.getContext() && "The IO context is not initialized");
1587  IO.mapOptional("Name", Sym.Name);
1588  IO.mapOptional("Index", Sym.Index);
1589 }
1590 
1592  ELFYAML::LinkerOption &Opt) {
1593  assert(IO.getContext() && "The IO context is not initialized");
1594  IO.mapRequired("Name", Opt.Key);
1595  IO.mapRequired("Value", Opt.Value);
1596 }
1597 
1598 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
1599 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
1600 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
1601 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
1602 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
1603 
1604 } // end namespace yaml
1605 
1606 } // end namespace llvm
Optional< llvm::yaml::Hex32 > NBuckets
Definition: ELFYAML.h:288
const NoneType None
Definition: None.h:23
uint64_t CallInst * C
This file declares classes for handling the YAML representation of ELF.
static StringRef input(StringRef Scalar, void *, ELFYAML::YAMLFlowString &Val)
Definition: ELFYAML.cpp:874
static void bitset(IO &IO, ELFYAML::ELF_SHF &Value)
Definition: ELFYAML.cpp:514
Optional< ELF_SHF > Flags
Definition: ELFYAML.h:167
Optional< llvm::yaml::Hex64 > Size
Definition: ELFYAML.h:353
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool to_integer(StringRef S, N &Num, unsigned Base=0)
Convert the string S to an integer of the specified type using the radix Base.
Definition: StringExtras.h:193
Optional< std::vector< llvm::yaml::Hex64 > > BloomFilter
Definition: ELFYAML.h:308
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:219
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:446
Optional< std::vector< Symbol > > DynamicSymbols
Definition: ELFYAML.h:499
static void enumeration(IO &IO, ELFYAML::ELF_ET &Value)
Definition: ELFYAML.cpp:31
Optional< llvm::yaml::Hex64 > Align
Definition: ELFYAML.h:96
static void enumeration(IO &IO, ELFYAML::ELF_STT &Value)
Definition: ELFYAML.cpp:591
llvm::yaml::Hex32 SymNdx
Definition: ELFYAML.h:292
Optional< ELF_SHN > Index
Definition: ELFYAML.h:108
static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value)
Definition: ELFYAML.cpp:557
Optional< llvm::yaml::Hex64 > Size
Definition: ELFYAML.h:220
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:974
llvm::yaml::Hex64 PAddr
Definition: ELFYAML.h:95
static QuotingType mustQuote(StringRef S)
Definition: ELFYAML.cpp:879
static void bitset(IO &IO, ELFYAML::ELF_PF &Value)
Definition: ELFYAML.cpp:506
std::vector< SectionName > Sections
Definition: ELFYAML.h:100
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:37
llvm::yaml::Hex16 Version
Definition: ELFYAML.h:467
Optional< llvm::yaml::Hex64 > Size
Definition: ELFYAML.h:245
ELFYAML::ELF_REL Type3
Definition: ELFYAML.cpp:1483
llvm::yaml::Hex64 Info
Definition: ELFYAML.h:408
F(f)
llvm::yaml::Hex64 Address
Definition: ELFYAML.h:168
static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value)
Definition: ELFYAML.cpp:607
FileHeader Header
Definition: ELFYAML.h:487
static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value)
Definition: ELFYAML.cpp:438
llvm::yaml::Hex64 Entry
Definition: ELFYAML.h:79
Optional< llvm::yaml::Hex64 > Size
Definition: ELFYAML.h:275
static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value)
Definition: ELFYAML.cpp:816
llvm::yaml::Hex64 AddressAlign
Definition: ELFYAML.h:170
Optional< llvm::yaml::Hex32 > Index
Definition: ELFYAML.h:348
Optional< std::vector< llvm::yaml::Hex32 > > HashValues
Definition: ELFYAML.h:310
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:256
std::vector< DynamicEntry > Entries
Definition: ELFYAML.h:235
Optional< StringRef > Signature
Definition: ELFYAML.h:419
Optional< std::vector< StackSizeEntry > > Entries
Definition: ELFYAML.h:221
StringRef Section
Definition: ELFYAML.h:107
static void groupSectionMapping(IO &IO, ELFYAML::Group &Group)
Definition: ELFYAML.cpp:1110
static void enumeration(IO &IO, ELFYAML::ELF_EM &Value)
Definition: ELFYAML.cpp:62
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:111
Optional< std::vector< uint32_t > > Chain
Definition: ELFYAML.h:277
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
llvm::yaml::Hex8 ABIVersion
Definition: ELFYAML.h:75
llvm::yaml::Hex64 Info
Definition: ELFYAML.h:333
Key
PAL metadata keys.
static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value)
Definition: ELFYAML.cpp:228
Optional< llvm::yaml::Hex64 > EntSize
Definition: ELFYAML.h:171
ELFYAML::ELF_RSS SpecSym
Definition: ELFYAML.cpp:1484
static StringRef input(StringRef Scalar, void *, StOtherPiece &Val)
Definition: ELFYAML.cpp:859
std::vector< std::unique_ptr< Chunk > > Chunks
Definition: ELFYAML.h:492
static void output(const StOtherPiece &Val, void *, raw_ostream &Out)
Definition: ELFYAML.cpp:856
Optional< GnuHashHeader > Header
Definition: ELFYAML.h:307
iterator find(const KeyT &Key)
Definition: MapVector.h:147
static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value)
Definition: ELFYAML.cpp:786
Optional< uint8_t > Other
Definition: ELFYAML.h:112
Optional< llvm::yaml::Hex64 > ShSize
Definition: ELFYAML.h:195
std::vector< ProgramHeader > ProgramHeaders
Definition: ELFYAML.h:488
StringRef Name
Definition: ELFYAML.h:159
Optional< std::vector< Symbol > > Symbols
Definition: ELFYAML.h:498
std::vector< VernauxEntry > AuxV
Definition: ELFYAML.h:327
static void commonSectionMapping(IO &IO, ELFYAML::Section &Section)
Definition: ELFYAML.cpp:1009
#define P(N)
static void enumeration(IO &IO, ELFYAML::ELF_PT &Value)
Definition: ELFYAML.cpp:43
Optional< llvm::yaml::Hex16 > SHStrNdx
Definition: ELFYAML.h:84
std::string UnknownFlagsHolder
Definition: ELFYAML.cpp:975
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
llvm::yaml::Hex32 Shift2
Definition: ELFYAML.h:301
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
static void enumeration(IO &IO, ELFYAML::ELF_DYNTAG &Value)
Definition: ELFYAML.cpp:668
llvm::yaml::Hex32 Flags2
Definition: ELFYAML.h:477
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
std::vector< uint32_t > Entries
Definition: ELFYAML.h:456
Optional< llvm::yaml::Hex64 > Size
Definition: ELFYAML.h:265
Optional< llvm::yaml::Hex64 > SHOff
Definition: ELFYAML.h:82
std::vector< StringRef > VerNames
Definition: ELFYAML.h:401
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:368
static QuotingType mustQuote(StringRef)
Definition: ELFYAML.cpp:863
static void sectionMapping(IO &IO, ELFYAML::DynamicSection &Section)
Definition: ELFYAML.cpp:1030
Optional< std::vector< ELFYAML::NoteEntry > > Notes
Definition: ELFYAML.h:266
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:244
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:236
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
llvm::yaml::Hex8 ISARevision
Definition: ELFYAML.h:469
Optional< std::vector< LinkerOption > > Options
Definition: ELFYAML.h:367
yaml::BinaryRef Desc
Definition: ELFYAML.h:131
Optional< llvm::yaml::Hex64 > MemSize
Definition: ELFYAML.h:98
Optional< std::vector< llvm::yaml::Hex64 > > Entries
Definition: ELFYAML.h:445
Optional< llvm::yaml::Hex32 > MaskWords
Definition: ELFYAML.h:298
llvm::yaml::Hex32 Type
Definition: ELFYAML.h:132
StringRef dropUniqueSuffix(StringRef S)
Definition: ELFEmitter.cpp:396
static void bitset(IO &IO, ELFYAML::ELF_EF &Value)
Definition: ELFYAML.cpp:281
ELF_ELFCLASS Class
Definition: ELFYAML.h:72
std::vector< SectionOrType > Members
Definition: ELFYAML.h:418
Optional< llvm::yaml::Hex64 > ShOffset
Definition: ELFYAML.h:191
Optional< StringRef > Name
Definition: ELFYAML.h:347
static bool nameMatches(StringRef Name)
Definition: ELFYAML.h:229
llvm::yaml::Hex64 Offset
Definition: ELFYAML.h:427
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:305
static uint32_t getFlags(const Symbol *Sym)
Definition: TapiFile.cpp:28
Optional< std::vector< VerneedEntry > > VerneedV
Definition: ELFYAML.h:332
static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value)
Definition: ELFYAML.cpp:249
Optional< llvm::yaml::Hex16 > SHNum
Definition: ELFYAML.h:83
static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value)
Definition: ELFYAML.cpp:797
Optional< llvm::yaml::Hex64 > Info
Definition: ELFYAML.h:246
Optional< llvm::yaml::Hex64 > ShName
Definition: ELFYAML.h:187
static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value)
Definition: ELFYAML.cpp:760
#define ECase(X)
bool hasValue() const
Definition: Optional.h:259
ELFYAML::ELF_REL Type2
Definition: ELFYAML.cpp:1482
Optional< llvm::yaml::Hex64 > ShFlags
Definition: ELFYAML.h:198
llvm::yaml::Hex64 Address
Definition: ELFYAML.h:125
Optional< yaml::BinaryRef > Pattern
Definition: ELFYAML.h:205
static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value)
Definition: ELFYAML.cpp:746
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:406
MIPS_AFL_EXT ISAExtension
Definition: ELFYAML.h:474
Optional< std::vector< uint32_t > > Bucket
Definition: ELFYAML.h:276
#define N
Optional< std::vector< llvm::yaml::Hex32 > > HashBuckets
Definition: ELFYAML.h:309
llvm::yaml::Hex64 Value
Definition: ELFYAML.h:110
static void enumeration(IO &IO, ELFYAML::ELF_STB &Value)
Definition: ELFYAML.cpp:580
static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value)
Definition: ELFYAML.cpp:238
Optional< std::vector< AddrsigSymbol > > Symbols
Definition: ELFYAML.h:354
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:352
Optional< llvm::yaml::Hex64 > FileSize
Definition: ELFYAML.h:97
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:331
const std::string to_string(const T &Value)
Definition: ScopedPrinter.h:61
Optional< llvm::yaml::Hex16 > SHEntSize
Definition: ELFYAML.h:81
Optional< uint32_t > NameIndex
Definition: ELFYAML.h:105
MIPS_AFL_FLAGS1 Flags1
Definition: ELFYAML.h:476
IO & YamlIO
Definition: ELFYAML.cpp:973
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:143
iterator end()
Definition: MapVector.h:71
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void fillMapping(IO &IO, ELFYAML::Fill &Fill)
Definition: ELFYAML.cpp:1128
LLVM Value Representation.
Definition: Value.h:74
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:264
llvm::yaml::Hex64 Val
Definition: ELFYAML.h:121
Optional< std::vector< VerdefEntry > > Entries
Definition: ELFYAML.h:405
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
Optional< std::vector< YAMLFlowString > > Libs
Definition: ELFYAML.h:378
static void output(const ELFYAML::YAMLFlowString &Val, void *, raw_ostream &Out)
Definition: ELFYAML.cpp:870
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:274
std::vector< uint16_t > Entries
Definition: ELFYAML.h:389
static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value)
Definition: ELFYAML.cpp:736
ELF_ELFOSABI OSABI
Definition: ELFYAML.h:74
llvm::yaml::Hex64 VAddr
Definition: ELFYAML.h:94
#define BCaseMask(X, M)
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:379
Optional< StringRef > Symbol
Definition: ELFYAML.h:430
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:126
std::vector< Relocation > Relocations
Definition: ELFYAML.h:434
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:206
Optional< llvm::yaml::Hex64 > Offset
Definition: ELFYAML.h:99