1 //===- ELFDumper.cpp - ELF-specific dumper --------------------------------===//
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 /// \file
10 /// This file implements the ELF-specific dumper for llvm-readobj.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "ARMEHABIPrinter.h"
15 #include "DwarfCFIEHPrinter.h"
16 #include "ObjDumper.h"
17 #include "StackMapPrinter.h"
18 #include "llvm-readobj.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/MapVector.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/PointerIntPair.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/ADT/Twine.h"
31 #include "llvm/BinaryFormat/AMDGPUMetadataVerifier.h"
32 #include "llvm/BinaryFormat/ELF.h"
33 #include "llvm/Demangle/Demangle.h"
34 #include "llvm/Object/ELF.h"
35 #include "llvm/Object/ELFObjectFile.h"
36 #include "llvm/Object/ELFTypes.h"
37 #include "llvm/Object/Error.h"
38 #include "llvm/Object/ObjectFile.h"
39 #include "llvm/Object/RelocationResolver.h"
40 #include "llvm/Object/StackMapParser.h"
41 #include "llvm/Support/AMDGPUMetadata.h"
42 #include "llvm/Support/ARMAttributeParser.h"
43 #include "llvm/Support/ARMBuildAttributes.h"
44 #include "llvm/Support/Casting.h"
45 #include "llvm/Support/Compiler.h"
46 #include "llvm/Support/Endian.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/Format.h"
49 #include "llvm/Support/FormatVariadic.h"
50 #include "llvm/Support/FormattedStream.h"
51 #include "llvm/Support/LEB128.h"
52 #include "llvm/Support/MathExtras.h"
53 #include "llvm/Support/MipsABIFlags.h"
54 #include "llvm/Support/RISCVAttributeParser.h"
55 #include "llvm/Support/RISCVAttributes.h"
56 #include "llvm/Support/ScopedPrinter.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include <algorithm>
59 #include <cinttypes>
60 #include <cstddef>
61 #include <cstdint>
62 #include <cstdlib>
63 #include <iterator>
64 #include <memory>
65 #include <string>
66 #include <system_error>
67 #include <vector>
68 
69 using namespace llvm;
70 using namespace llvm::object;
71 using namespace ELF;
72 
73 #define LLVM_READOBJ_ENUM_CASE(ns, enum)                                       \
74   case ns::enum:                                                               \
75     return #enum;
76 
77 #define ENUM_ENT(enum, altName)                                                \
78   { #enum, altName, ELF::enum }
79 
80 #define ENUM_ENT_1(enum)                                                       \
81   { #enum, #enum, ELF::enum }
82 
83 namespace {
84 
85 template <class ELFT> struct RelSymbol {
86   RelSymbol(const typename ELFT::Sym *S, StringRef N)
87       : Sym(S), Name(N.str()) {}
88   const typename ELFT::Sym *Sym;
89   std::string Name;
90 };
91 
92 /// Represents a contiguous uniform range in the file. We cannot just create a
93 /// range directly because when creating one of these from the .dynamic table
94 /// the size, entity size and virtual address are different entries in arbitrary
95 /// order (DT_REL, DT_RELSZ, DT_RELENT for example).
96 struct DynRegionInfo {
97   DynRegionInfo(const Binary &Owner, const ObjDumper &D)
98       : Obj(&Owner), Dumper(&D) {}
99   DynRegionInfo(const Binary &Owner, const ObjDumper &D, const uint8_t *A,
100                 uint64_t S, uint64_t ES)
101       : Addr(A), Size(S), EntSize(ES), Obj(&Owner), Dumper(&D) {}
102 
103   /// Address in current address space.
104   const uint8_t *Addr = nullptr;
105   /// Size in bytes of the region.
106   uint64_t Size = 0;
107   /// Size of each entity in the region.
108   uint64_t EntSize = 0;
109 
110   /// Owner object. Used for error reporting.
111   const Binary *Obj;
112   /// Dumper used for error reporting.
113   const ObjDumper *Dumper;
114   /// Error prefix. Used for error reporting to provide more information.
115   std::string Context;
116   /// Region size name. Used for error reporting.
117   StringRef SizePrintName = "size";
118   /// Entry size name. Used for error reporting. If this field is empty, errors
119   /// will not mention the entry size.
120   StringRef EntSizePrintName = "entry size";
121 
122   template <typename Type> ArrayRef<Type> getAsArrayRef() const {
123     const Type *Start = reinterpret_cast<const Type *>(Addr);
124     if (!Start)
125       return {Start, Start};
126 
127     const uint64_t Offset =
128         Addr - (const uint8_t *)Obj->getMemoryBufferRef().getBufferStart();
129     const uint64_t ObjSize = Obj->getMemoryBufferRef().getBufferSize();
130 
131     if (Size > ObjSize - Offset) {
132       Dumper->reportUniqueWarning(
133           "unable to read data at 0x" + Twine::utohexstr(Offset) +
134           " of size 0x" + Twine::utohexstr(Size) + " (" + SizePrintName +
135           "): it goes past the end of the file of size 0x" +
136           Twine::utohexstr(ObjSize));
137       return {Start, Start};
138     }
139 
140     if (EntSize == sizeof(Type) && (Size % EntSize == 0))
141       return {Start, Start + (Size / EntSize)};
142 
143     std::string Msg;
144     if (!Context.empty())
145       Msg += Context + " has ";
146 
147     Msg += ("invalid " + SizePrintName + " (0x" + Twine::utohexstr(Size) + ")")
148                .str();
149     if (!EntSizePrintName.empty())
150       Msg +=
151           (" or " + EntSizePrintName + " (0x" + Twine::utohexstr(EntSize) + ")")
152               .str();
153 
154     Dumper->reportUniqueWarning(Msg);
155     return {Start, Start};
156   }
157 };
158 
159 struct GroupMember {
160   StringRef Name;
161   uint64_t Index;
162 };
163 
164 struct GroupSection {
165   StringRef Name;
166   std::string Signature;
167   uint64_t ShName;
168   uint64_t Index;
169   uint32_t Link;
170   uint32_t Info;
171   uint32_t Type;
172   std::vector<GroupMember> Members;
173 };
174 
175 namespace {
176 
177 struct NoteType {
178   uint32_t ID;
179   StringRef Name;
180 };
181 
182 } // namespace
183 
184 template <class ELFT> class Relocation {
185 public:
186   Relocation(const typename ELFT::Rel &R, bool IsMips64EL)
187       : Type(R.getType(IsMips64EL)), Symbol(R.getSymbol(IsMips64EL)),
188         Offset(R.r_offset), Info(R.r_info) {}
189 
190   Relocation(const typename ELFT::Rela &R, bool IsMips64EL)
191       : Relocation((const typename ELFT::Rel &)R, IsMips64EL) {
192     Addend = R.r_addend;
193   }
194 
195   uint32_t Type;
196   uint32_t Symbol;
197   typename ELFT::uint Offset;
198   typename ELFT::uint Info;
199   Optional<int64_t> Addend;
200 };
201 
202 template <class ELFT> class MipsGOTParser;
203 
204 template <typename ELFT> class ELFDumper : public ObjDumper {
205   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
206 
207 public:
208   ELFDumper(const object::ELFObjectFile<ELFT> &ObjF, ScopedPrinter &Writer);
209 
210   void printUnwindInfo() override;
211   void printNeededLibraries() override;
212   void printHashTable() override;
213   void printGnuHashTable() override;
214   void printLoadName() override;
215   void printVersionInfo() override;
216   void printArchSpecificInfo() override;
217   void printStackMap() const override;
218 
219   const object::ELFObjectFile<ELFT> &getElfObject() const { return ObjF; };
220 
221   std::string describe(const Elf_Shdr &Sec) const;
222 
223   unsigned getHashTableEntSize() const {
224     // EM_S390 and ELF::EM_ALPHA platforms use 8-bytes entries in SHT_HASH
225     // sections. This violates the ELF specification.
226     if (Obj.getHeader().e_machine == ELF::EM_S390 ||
227         Obj.getHeader().e_machine == ELF::EM_ALPHA)
228       return 8;
229     return 4;
230   }
231 
232   Elf_Dyn_Range dynamic_table() const {
233     // A valid .dynamic section contains an array of entries terminated
234     // with a DT_NULL entry. However, sometimes the section content may
235     // continue past the DT_NULL entry, so to dump the section correctly,
236     // we first find the end of the entries by iterating over them.
237     Elf_Dyn_Range Table = DynamicTable.template getAsArrayRef<Elf_Dyn>();
238 
239     size_t Size = 0;
240     while (Size < Table.size())
241       if (Table[Size++].getTag() == DT_NULL)
242         break;
243 
244     return Table.slice(0, Size);
245   }
246 
247   Elf_Sym_Range dynamic_symbols() const {
248     if (!DynSymRegion)
249       return Elf_Sym_Range();
250     return DynSymRegion->template getAsArrayRef<Elf_Sym>();
251   }
252 
253   const Elf_Shdr *findSectionByName(StringRef Name) const;
254 
255   StringRef getDynamicStringTable() const { return DynamicStringTable; }
256 
257 protected:
258   virtual void printVersionSymbolSection(const Elf_Shdr *Sec) = 0;
259   virtual void printVersionDefinitionSection(const Elf_Shdr *Sec) = 0;
260   virtual void printVersionDependencySection(const Elf_Shdr *Sec) = 0;
261 
262   void
263   printDependentLibsHelper(function_ref<void(const Elf_Shdr &)> OnSectionStart,
264                            function_ref<void(StringRef, uint64_t)> OnLibEntry);
265 
266   virtual void printRelRelaReloc(const Relocation<ELFT> &R,
267                                  const RelSymbol<ELFT> &RelSym) = 0;
268   virtual void printRelrReloc(const Elf_Relr &R) = 0;
269   virtual void printDynamicRelocHeader(unsigned Type, StringRef Name,
270                                        const DynRegionInfo &Reg) {}
271   void printReloc(const Relocation<ELFT> &R, unsigned RelIndex,
272                   const Elf_Shdr &Sec, const Elf_Shdr *SymTab);
273   void printDynamicReloc(const Relocation<ELFT> &R);
274   void printDynamicRelocationsHelper();
275   void printRelocationsHelper(const Elf_Shdr &Sec);
276   void forEachRelocationDo(
277       const Elf_Shdr &Sec, bool RawRelr,
278       llvm::function_ref<void(const Relocation<ELFT> &, unsigned,
279                               const Elf_Shdr &, const Elf_Shdr *)>
280           RelRelaFn,
281       llvm::function_ref<void(const Elf_Relr &)> RelrFn);
282 
283   virtual void printSymtabMessage(const Elf_Shdr *Symtab, size_t Offset,
284                                   bool NonVisibilityBitsUsed) const {};
285   virtual void printSymbol(const Elf_Sym &Symbol, unsigned SymIndex,
286                            DataRegion<Elf_Word> ShndxTable,
287                            Optional<StringRef> StrTable, bool IsDynamic,
288                            bool NonVisibilityBitsUsed) const = 0;
289 
290   virtual void printMipsABIFlags() = 0;
291   virtual void printMipsGOT(const MipsGOTParser<ELFT> &Parser) = 0;
292   virtual void printMipsPLT(const MipsGOTParser<ELFT> &Parser) = 0;
293 
294   Expected<ArrayRef<Elf_Versym>>
295   getVersionTable(const Elf_Shdr &Sec, ArrayRef<Elf_Sym> *SymTab,
296                   StringRef *StrTab, const Elf_Shdr **SymTabSec) const;
297   StringRef getPrintableSectionName(const Elf_Shdr &Sec) const;
298 
299   std::vector<GroupSection> getGroups();
300 
301   bool printFunctionStackSize(uint64_t SymValue,
302                               Optional<const Elf_Shdr *> FunctionSec,
303                               const Elf_Shdr &StackSizeSec, DataExtractor Data,
304                               uint64_t *Offset);
305   void printStackSize(const Relocation<ELFT> &R, const Elf_Shdr &RelocSec,
306                       unsigned Ndx, const Elf_Shdr *SymTab,
307                       const Elf_Shdr *FunctionSec, const Elf_Shdr &StackSizeSec,
308                       const RelocationResolver &Resolver, DataExtractor Data);
309   virtual void printStackSizeEntry(uint64_t Size, StringRef FuncName) = 0;
310 
311   void printRelocatableStackSizes(std::function<void()> PrintHeader);
312   void printNonRelocatableStackSizes(std::function<void()> PrintHeader);
313 
314   const object::ELFObjectFile<ELFT> &ObjF;
315   const ELFFile<ELFT> &Obj;
316   StringRef FileName;
317 
318   Expected<DynRegionInfo> createDRI(uint64_t Offset, uint64_t Size,
319                                     uint64_t EntSize) {
320     if (Offset + Size < Offset || Offset + Size > Obj.getBufSize())
321       return createError("offset (0x" + Twine::utohexstr(Offset) +
322                          ") + size (0x" + Twine::utohexstr(Size) +
323                          ") is greater than the file size (0x" +
324                          Twine::utohexstr(Obj.getBufSize()) + ")");
325     return DynRegionInfo(ObjF, *this, Obj.base() + Offset, Size, EntSize);
326   }
327 
328   void printAttributes();
329   void printMipsReginfo();
330   void printMipsOptions();
331 
332   std::pair<const Elf_Phdr *, const Elf_Shdr *> findDynamic();
333   void loadDynamicTable();
334   void parseDynamicTable();
335 
336   Expected<StringRef> getSymbolVersion(const Elf_Sym &Sym,
337                                        bool &IsDefault) const;
338   Expected<SmallVector<Optional<VersionEntry>, 0> *> getVersionMap() const;
339 
340   DynRegionInfo DynRelRegion;
341   DynRegionInfo DynRelaRegion;
342   DynRegionInfo DynRelrRegion;
343   DynRegionInfo DynPLTRelRegion;
344   Optional<DynRegionInfo> DynSymRegion;
345   DynRegionInfo DynSymTabShndxRegion;
346   DynRegionInfo DynamicTable;
347   StringRef DynamicStringTable;
348   const Elf_Hash *HashTable = nullptr;
349   const Elf_GnuHash *GnuHashTable = nullptr;
350   const Elf_Shdr *DotSymtabSec = nullptr;
351   const Elf_Shdr *DotDynsymSec = nullptr;
352   const Elf_Shdr *DotCGProfileSec = nullptr;
353   const Elf_Shdr *DotAddrsigSec = nullptr;
354   DenseMap<const Elf_Shdr *, ArrayRef<Elf_Word>> ShndxTables;
355   Optional<uint64_t> SONameOffset;
356 
357   const Elf_Shdr *SymbolVersionSection = nullptr;   // .gnu.version
358   const Elf_Shdr *SymbolVersionNeedSection = nullptr; // .gnu.version_r
359   const Elf_Shdr *SymbolVersionDefSection = nullptr; // .gnu.version_d
360 
361   std::string getFullSymbolName(const Elf_Sym &Symbol, unsigned SymIndex,
362                                 DataRegion<Elf_Word> ShndxTable,
363                                 Optional<StringRef> StrTable,
364                                 bool IsDynamic) const;
365   Expected<unsigned>
366   getSymbolSectionIndex(const Elf_Sym &Symbol, unsigned SymIndex,
367                         DataRegion<Elf_Word> ShndxTable) const;
368   Expected<StringRef> getSymbolSectionName(const Elf_Sym &Symbol,
369                                            unsigned SectionIndex) const;
370   std::string getStaticSymbolName(uint32_t Index) const;
371   StringRef getDynamicString(uint64_t Value) const;
372 
373   void printSymbolsHelper(bool IsDynamic) const;
374   std::string getDynamicEntry(uint64_t Type, uint64_t Value) const;
375 
376   Expected<RelSymbol<ELFT>> getRelocationTarget(const Relocation<ELFT> &R,
377                                                 const Elf_Shdr *SymTab) const;
378 
379   ArrayRef<Elf_Word> getShndxTable(const Elf_Shdr *Symtab) const;
380 
381 private:
382   mutable SmallVector<Optional<VersionEntry>, 0> VersionMap;
383 };
384 
385 template <class ELFT>
386 std::string ELFDumper<ELFT>::describe(const Elf_Shdr &Sec) const {
387   return ::describe(Obj, Sec);
388 }
389 
390 namespace {
391 
392 template <class ELFT> struct SymtabLink {
393   typename ELFT::SymRange Symbols;
394   StringRef StringTable;
395   const typename ELFT::Shdr *SymTab;
396 };
397 
398 // Returns the linked symbol table, symbols and associated string table for a
399 // given section.
400 template <class ELFT>
401 Expected<SymtabLink<ELFT>> getLinkAsSymtab(const ELFFile<ELFT> &Obj,
402                                            const typename ELFT::Shdr &Sec,
403                                            unsigned ExpectedType) {
404   Expected<const typename ELFT::Shdr *> SymtabOrErr =
405       Obj.getSection(Sec.sh_link);
406   if (!SymtabOrErr)
407     return createError("invalid section linked to " + describe(Obj, Sec) +
408                        ": " + toString(SymtabOrErr.takeError()));
409 
410   if ((*SymtabOrErr)->sh_type != ExpectedType)
411     return createError(
412         "invalid section linked to " + describe(Obj, Sec) + ": expected " +
413         object::getELFSectionTypeName(Obj.getHeader().e_machine, ExpectedType) +
414         ", but got " +
415         object::getELFSectionTypeName(Obj.getHeader().e_machine,
416                                       (*SymtabOrErr)->sh_type));
417 
418   Expected<StringRef> StrTabOrErr = Obj.getLinkAsStrtab(**SymtabOrErr);
419   if (!StrTabOrErr)
420     return createError(
421         "can't get a string table for the symbol table linked to " +
422         describe(Obj, Sec) + ": " + toString(StrTabOrErr.takeError()));
423 
424   Expected<typename ELFT::SymRange> SymsOrErr = Obj.symbols(*SymtabOrErr);
425   if (!SymsOrErr)
426     return createError("unable to read symbols from the " + describe(Obj, Sec) +
427                        ": " + toString(SymsOrErr.takeError()));
428 
429   return SymtabLink<ELFT>{*SymsOrErr, *StrTabOrErr, *SymtabOrErr};
430 }
431 
432 } // namespace
433 
434 template <class ELFT>
435 Expected<ArrayRef<typename ELFT::Versym>>
436 ELFDumper<ELFT>::getVersionTable(const Elf_Shdr &Sec, ArrayRef<Elf_Sym> *SymTab,
437                                  StringRef *StrTab,
438                                  const Elf_Shdr **SymTabSec) const {
439   assert((!SymTab && !StrTab && !SymTabSec) || (SymTab && StrTab && SymTabSec));
440   if (reinterpret_cast<uintptr_t>(Obj.base() + Sec.sh_offset) %
441           sizeof(uint16_t) !=
442       0)
443     return createError("the " + describe(Sec) + " is misaligned");
444 
445   Expected<ArrayRef<Elf_Versym>> VersionsOrErr =
446       Obj.template getSectionContentsAsArray<Elf_Versym>(Sec);
447   if (!VersionsOrErr)
448     return createError("cannot read content of " + describe(Sec) + ": " +
449                        toString(VersionsOrErr.takeError()));
450 
451   Expected<SymtabLink<ELFT>> SymTabOrErr =
452       getLinkAsSymtab(Obj, Sec, SHT_DYNSYM);
453   if (!SymTabOrErr) {
454     reportUniqueWarning(SymTabOrErr.takeError());
455     return *VersionsOrErr;
456   }
457 
458   if (SymTabOrErr->Symbols.size() != VersionsOrErr->size())
459     reportUniqueWarning(describe(Sec) + ": the number of entries (" +
460                         Twine(VersionsOrErr->size()) +
461                         ") does not match the number of symbols (" +
462                         Twine(SymTabOrErr->Symbols.size()) +
463                         ") in the symbol table with index " +
464                         Twine(Sec.sh_link));
465 
466   if (SymTab) {
467     *SymTab = SymTabOrErr->Symbols;
468     *StrTab = SymTabOrErr->StringTable;
469     *SymTabSec = SymTabOrErr->SymTab;
470   }
471   return *VersionsOrErr;
472 }
473 
474 template <class ELFT>
475 void ELFDumper<ELFT>::printSymbolsHelper(bool IsDynamic) const {
476   Optional<StringRef> StrTable;
477   size_t Entries = 0;
478   Elf_Sym_Range Syms(nullptr, nullptr);
479   const Elf_Shdr *SymtabSec = IsDynamic ? DotDynsymSec : DotSymtabSec;
480 
481   if (IsDynamic) {
482     StrTable = DynamicStringTable;
483     Syms = dynamic_symbols();
484     Entries = Syms.size();
485   } else if (DotSymtabSec) {
486     if (Expected<StringRef> StrTableOrErr =
487             Obj.getStringTableForSymtab(*DotSymtabSec))
488       StrTable = *StrTableOrErr;
489     else
490       reportUniqueWarning(
491           "unable to get the string table for the SHT_SYMTAB section: " +
492           toString(StrTableOrErr.takeError()));
493 
494     if (Expected<Elf_Sym_Range> SymsOrErr = Obj.symbols(DotSymtabSec))
495       Syms = *SymsOrErr;
496     else
497       reportUniqueWarning(
498           "unable to read symbols from the SHT_SYMTAB section: " +
499           toString(SymsOrErr.takeError()));
500     Entries = DotSymtabSec->getEntityCount();
501   }
502   if (Syms.empty())
503     return;
504 
505   // The st_other field has 2 logical parts. The first two bits hold the symbol
506   // visibility (STV_*) and the remainder hold other platform-specific values.
507   bool NonVisibilityBitsUsed =
508       llvm::any_of(Syms, [](const Elf_Sym &S) { return S.st_other & ~0x3; });
509 
510   DataRegion<Elf_Word> ShndxTable =
511       IsDynamic ? DataRegion<Elf_Word>(
512                       (const Elf_Word *)this->DynSymTabShndxRegion.Addr,
513                       this->getElfObject().getELFFile().end())
514                 : DataRegion<Elf_Word>(this->getShndxTable(SymtabSec));
515 
516   printSymtabMessage(SymtabSec, Entries, NonVisibilityBitsUsed);
517   for (const Elf_Sym &Sym : Syms)
518     printSymbol(Sym, &Sym - Syms.begin(), ShndxTable, StrTable, IsDynamic,
519                 NonVisibilityBitsUsed);
520 }
521 
522 template <typename ELFT> class GNUELFDumper : public ELFDumper<ELFT> {
523   formatted_raw_ostream &OS;
524 
525 public:
526   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
527 
528   GNUELFDumper(const object::ELFObjectFile<ELFT> &ObjF, ScopedPrinter &Writer)
529       : ELFDumper<ELFT>(ObjF, Writer),
530         OS(static_cast<formatted_raw_ostream &>(Writer.getOStream())) {
531     assert(&this->W.getOStream() == &llvm::fouts());
532   }
533 
534   void printFileHeaders() override;
535   void printGroupSections() override;
536   void printRelocations() override;
537   void printSectionHeaders() override;
538   void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols) override;
539   void printHashSymbols() override;
540   void printSectionDetails() override;
541   void printDependentLibs() override;
542   void printDynamicTable() override;
543   void printDynamicRelocations() override;
544   void printSymtabMessage(const Elf_Shdr *Symtab, size_t Offset,
545                           bool NonVisibilityBitsUsed) const override;
546   void printProgramHeaders(bool PrintProgramHeaders,
547                            cl::boolOrDefault PrintSectionMapping) override;
548   void printVersionSymbolSection(const Elf_Shdr *Sec) override;
549   void printVersionDefinitionSection(const Elf_Shdr *Sec) override;
550   void printVersionDependencySection(const Elf_Shdr *Sec) override;
551   void printHashHistograms() override;
552   void printCGProfile() override;
553   void printAddrsig() override;
554   void printNotes() override;
555   void printELFLinkerOptions() override;
556   void printStackSizes() override;
557 
558 private:
559   void printHashHistogram(const Elf_Hash &HashTable);
560   void printGnuHashHistogram(const Elf_GnuHash &GnuHashTable);
561   void printHashTableSymbols(const Elf_Hash &HashTable);
562   void printGnuHashTableSymbols(const Elf_GnuHash &GnuHashTable);
563 
564   struct Field {
565     std::string Str;
566     unsigned Column;
567 
568     Field(StringRef S, unsigned Col) : Str(std::string(S)), Column(Col) {}
569     Field(unsigned Col) : Column(Col) {}
570   };
571 
572   template <typename T, typename TEnum>
573   std::string printEnum(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues) const {
574     for (const EnumEntry<TEnum> &EnumItem : EnumValues)
575       if (EnumItem.Value == Value)
576         return std::string(EnumItem.AltName);
577     return to_hexString(Value, false);
578   }
579 
580   template <typename T, typename TEnum>
581   std::string printFlags(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues,
582                          TEnum EnumMask1 = {}, TEnum EnumMask2 = {},
583                          TEnum EnumMask3 = {}) const {
584     std::string Str;
585     for (const EnumEntry<TEnum> &Flag : EnumValues) {
586       if (Flag.Value == 0)
587         continue;
588 
589       TEnum EnumMask{};
590       if (Flag.Value & EnumMask1)
591         EnumMask = EnumMask1;
592       else if (Flag.Value & EnumMask2)
593         EnumMask = EnumMask2;
594       else if (Flag.Value & EnumMask3)
595         EnumMask = EnumMask3;
596       bool IsEnum = (Flag.Value & EnumMask) != 0;
597       if ((!IsEnum && (Value & Flag.Value) == Flag.Value) ||
598           (IsEnum && (Value & EnumMask) == Flag.Value)) {
599         if (!Str.empty())
600           Str += ", ";
601         Str += Flag.AltName;
602       }
603     }
604     return Str;
605   }
606 
607   formatted_raw_ostream &printField(struct Field F) const {
608     if (F.Column != 0)
609       OS.PadToColumn(F.Column);
610     OS << F.Str;
611     OS.flush();
612     return OS;
613   }
614   void printHashedSymbol(const Elf_Sym *Sym, unsigned SymIndex,
615                          DataRegion<Elf_Word> ShndxTable, StringRef StrTable,
616                          uint32_t Bucket);
617   void printRelrReloc(const Elf_Relr &R) override;
618   void printRelRelaReloc(const Relocation<ELFT> &R,
619                          const RelSymbol<ELFT> &RelSym) override;
620   void printSymbol(const Elf_Sym &Symbol, unsigned SymIndex,
621                    DataRegion<Elf_Word> ShndxTable,
622                    Optional<StringRef> StrTable, bool IsDynamic,
623                    bool NonVisibilityBitsUsed) const override;
624   void printDynamicRelocHeader(unsigned Type, StringRef Name,
625                                const DynRegionInfo &Reg) override;
626 
627   std::string getSymbolSectionNdx(const Elf_Sym &Symbol, unsigned SymIndex,
628                                   DataRegion<Elf_Word> ShndxTable) const;
629   void printProgramHeaders() override;
630   void printSectionMapping() override;
631   void printGNUVersionSectionProlog(const typename ELFT::Shdr &Sec,
632                                     const Twine &Label, unsigned EntriesNum);
633 
634   void printStackSizeEntry(uint64_t Size, StringRef FuncName) override;
635 
636   void printMipsGOT(const MipsGOTParser<ELFT> &Parser) override;
637   void printMipsPLT(const MipsGOTParser<ELFT> &Parser) override;
638   void printMipsABIFlags() override;
639 };
640 
641 template <typename ELFT> class LLVMELFDumper : public ELFDumper<ELFT> {
642 public:
643   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
644 
645   LLVMELFDumper(const object::ELFObjectFile<ELFT> &ObjF, ScopedPrinter &Writer)
646       : ELFDumper<ELFT>(ObjF, Writer), W(Writer) {}
647 
648   void printFileHeaders() override;
649   void printGroupSections() override;
650   void printRelocations() override;
651   void printSectionHeaders() override;
652   void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols) override;
653   void printDependentLibs() override;
654   void printDynamicTable() override;
655   void printDynamicRelocations() override;
656   void printProgramHeaders(bool PrintProgramHeaders,
657                            cl::boolOrDefault PrintSectionMapping) override;
658   void printVersionSymbolSection(const Elf_Shdr *Sec) override;
659   void printVersionDefinitionSection(const Elf_Shdr *Sec) override;
660   void printVersionDependencySection(const Elf_Shdr *Sec) override;
661   void printHashHistograms() override;
662   void printCGProfile() override;
663   void printAddrsig() override;
664   void printNotes() override;
665   void printELFLinkerOptions() override;
666   void printStackSizes() override;
667 
668 private:
669   void printRelrReloc(const Elf_Relr &R) override;
670   void printRelRelaReloc(const Relocation<ELFT> &R,
671                          const RelSymbol<ELFT> &RelSym) override;
672 
673   void printSymbolSection(const Elf_Sym &Symbol, unsigned SymIndex,
674                           DataRegion<Elf_Word> ShndxTable) const;
675   void printSymbol(const Elf_Sym &Symbol, unsigned SymIndex,
676                    DataRegion<Elf_Word> ShndxTable,
677                    Optional<StringRef> StrTable, bool IsDynamic,
678                    bool /*NonVisibilityBitsUsed*/) const override;
679   void printProgramHeaders() override;
680   void printSectionMapping() override {}
681   void printStackSizeEntry(uint64_t Size, StringRef FuncName) override;
682 
683   void printMipsGOT(const MipsGOTParser<ELFT> &Parser) override;
684   void printMipsPLT(const MipsGOTParser<ELFT> &Parser) override;
685   void printMipsABIFlags() override;
686 
687   ScopedPrinter &W;
688 };
689 
690 } // end anonymous namespace
691 
692 namespace llvm {
693 
694 template <class ELFT>
695 static std::unique_ptr<ObjDumper>
696 createELFDumper(const ELFObjectFile<ELFT> &Obj, ScopedPrinter &Writer) {
697   if (opts::Output == opts::GNU)
698     return std::make_unique<GNUELFDumper<ELFT>>(Obj, Writer);
699   return std::make_unique<LLVMELFDumper<ELFT>>(Obj, Writer);
700 }
701 
702 std::unique_ptr<ObjDumper> createELFDumper(const object::ELFObjectFileBase &Obj,
703                                            ScopedPrinter &Writer) {
704   // Little-endian 32-bit
705   if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(&Obj))
706     return createELFDumper(*ELFObj, Writer);
707 
708   // Big-endian 32-bit
709   if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(&Obj))
710     return createELFDumper(*ELFObj, Writer);
711 
712   // Little-endian 64-bit
713   if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(&Obj))
714     return createELFDumper(*ELFObj, Writer);
715 
716   // Big-endian 64-bit
717   return createELFDumper(*cast<ELF64BEObjectFile>(&Obj), Writer);
718 }
719 
720 } // end namespace llvm
721 
722 template <class ELFT>
723 Expected<SmallVector<Optional<VersionEntry>, 0> *>
724 ELFDumper<ELFT>::getVersionMap() const {
725   // If the VersionMap has already been loaded or if there is no dynamic symtab
726   // or version table, there is nothing to do.
727   if (!VersionMap.empty() || !DynSymRegion || !SymbolVersionSection)
728     return &VersionMap;
729 
730   Expected<SmallVector<Optional<VersionEntry>, 0>> MapOrErr =
731       Obj.loadVersionMap(SymbolVersionNeedSection, SymbolVersionDefSection);
732   if (MapOrErr)
733     VersionMap = *MapOrErr;
734   else
735     return MapOrErr.takeError();
736 
737   return &VersionMap;
738 }
739 
740 template <typename ELFT>
741 Expected<StringRef> ELFDumper<ELFT>::getSymbolVersion(const Elf_Sym &Sym,
742                                                       bool &IsDefault) const {
743   // This is a dynamic symbol. Look in the GNU symbol version table.
744   if (!SymbolVersionSection) {
745     // No version table.
746     IsDefault = false;
747     return "";
748   }
749 
750   assert(DynSymRegion && "DynSymRegion has not been initialised");
751   // Determine the position in the symbol table of this entry.
752   size_t EntryIndex = (reinterpret_cast<uintptr_t>(&Sym) -
753                        reinterpret_cast<uintptr_t>(DynSymRegion->Addr)) /
754                       sizeof(Elf_Sym);
755 
756   // Get the corresponding version index entry.
757   Expected<const Elf_Versym *> EntryOrErr =
758       Obj.template getEntry<Elf_Versym>(*SymbolVersionSection, EntryIndex);
759   if (!EntryOrErr)
760     return EntryOrErr.takeError();
761 
762   unsigned Version = (*EntryOrErr)->vs_index;
763   if (Version == VER_NDX_LOCAL || Version == VER_NDX_GLOBAL) {
764     IsDefault = false;
765     return "";
766   }
767 
768   Expected<SmallVector<Optional<VersionEntry>, 0> *> MapOrErr =
769       getVersionMap();
770   if (!MapOrErr)
771     return MapOrErr.takeError();
772 
773   return Obj.getSymbolVersionByIndex(Version, IsDefault, **MapOrErr,
774                                      Sym.st_shndx == ELF::SHN_UNDEF);
775 }
776 
777 template <typename ELFT>
778 Expected<RelSymbol<ELFT>>
779 ELFDumper<ELFT>::getRelocationTarget(const Relocation<ELFT> &R,
780                                      const Elf_Shdr *SymTab) const {
781   if (R.Symbol == 0)
782     return RelSymbol<ELFT>(nullptr, "");
783 
784   Expected<const Elf_Sym *> SymOrErr =
785       Obj.template getEntry<Elf_Sym>(*SymTab, R.Symbol);
786   if (!SymOrErr)
787     return createError("unable to read an entry with index " + Twine(R.Symbol) +
788                        " from " + describe(*SymTab) + ": " +
789                        toString(SymOrErr.takeError()));
790   const Elf_Sym *Sym = *SymOrErr;
791   if (!Sym)
792     return RelSymbol<ELFT>(nullptr, "");
793 
794   Expected<StringRef> StrTableOrErr = Obj.getStringTableForSymtab(*SymTab);
795   if (!StrTableOrErr)
796     return StrTableOrErr.takeError();
797 
798   const Elf_Sym *FirstSym =
799       cantFail(Obj.template getEntry<Elf_Sym>(*SymTab, 0));
800   std::string SymbolName =
801       getFullSymbolName(*Sym, Sym - FirstSym, getShndxTable(SymTab),
802                         *StrTableOrErr, SymTab->sh_type == SHT_DYNSYM);
803   return RelSymbol<ELFT>(Sym, SymbolName);
804 }
805 
806 template <typename ELFT>
807 ArrayRef<typename ELFT::Word>
808 ELFDumper<ELFT>::getShndxTable(const Elf_Shdr *Symtab) const {
809   if (Symtab) {
810     auto It = ShndxTables.find(Symtab);
811     if (It != ShndxTables.end())
812       return It->second;
813   }
814   return {};
815 }
816 
817 static std::string maybeDemangle(StringRef Name) {
818   return opts::Demangle ? demangle(std::string(Name)) : Name.str();
819 }
820 
821 template <typename ELFT>
822 std::string ELFDumper<ELFT>::getStaticSymbolName(uint32_t Index) const {
823   auto Warn = [&](Error E) -> std::string {
824     reportUniqueWarning("unable to read the name of symbol with index " +
825                         Twine(Index) + ": " + toString(std::move(E)));
826     return "<?>";
827   };
828 
829   Expected<const typename ELFT::Sym *> SymOrErr =
830       Obj.getSymbol(DotSymtabSec, Index);
831   if (!SymOrErr)
832     return Warn(SymOrErr.takeError());
833 
834   Expected<StringRef> StrTabOrErr = Obj.getStringTableForSymtab(*DotSymtabSec);
835   if (!StrTabOrErr)
836     return Warn(StrTabOrErr.takeError());
837 
838   Expected<StringRef> NameOrErr = (*SymOrErr)->getName(*StrTabOrErr);
839   if (!NameOrErr)
840     return Warn(NameOrErr.takeError());
841   return maybeDemangle(*NameOrErr);
842 }
843 
844 template <typename ELFT>
845 std::string ELFDumper<ELFT>::getFullSymbolName(const Elf_Sym &Symbol,
846                                                unsigned SymIndex,
847                                                DataRegion<Elf_Word> ShndxTable,
848                                                Optional<StringRef> StrTable,
849                                                bool IsDynamic) const {
850   if (!StrTable)
851     return "<?>";
852 
853   std::string SymbolName;
854   if (Expected<StringRef> NameOrErr = Symbol.getName(*StrTable)) {
855     SymbolName = maybeDemangle(*NameOrErr);
856   } else {
857     reportUniqueWarning(NameOrErr.takeError());
858     return "<?>";
859   }
860 
861   if (SymbolName.empty() && Symbol.getType() == ELF::STT_SECTION) {
862     Expected<unsigned> SectionIndex =
863         getSymbolSectionIndex(Symbol, SymIndex, ShndxTable);
864     if (!SectionIndex) {
865       reportUniqueWarning(SectionIndex.takeError());
866       return "<?>";
867     }
868     Expected<StringRef> NameOrErr = getSymbolSectionName(Symbol, *SectionIndex);
869     if (!NameOrErr) {
870       reportUniqueWarning(NameOrErr.takeError());
871       return ("<section " + Twine(*SectionIndex) + ">").str();
872     }
873     return std::string(*NameOrErr);
874   }
875 
876   if (!IsDynamic)
877     return SymbolName;
878 
879   bool IsDefault;
880   Expected<StringRef> VersionOrErr = getSymbolVersion(Symbol, IsDefault);
881   if (!VersionOrErr) {
882     reportUniqueWarning(VersionOrErr.takeError());
883     return SymbolName + "@<corrupt>";
884   }
885 
886   if (!VersionOrErr->empty()) {
887     SymbolName += (IsDefault ? "@@" : "@");
888     SymbolName += *VersionOrErr;
889   }
890   return SymbolName;
891 }
892 
893 template <typename ELFT>
894 Expected<unsigned>
895 ELFDumper<ELFT>::getSymbolSectionIndex(const Elf_Sym &Symbol, unsigned SymIndex,
896                                        DataRegion<Elf_Word> ShndxTable) const {
897   unsigned Ndx = Symbol.st_shndx;
898   if (Ndx == SHN_XINDEX)
899     return object::getExtendedSymbolTableIndex<ELFT>(Symbol, SymIndex,
900                                                      ShndxTable);
901   if (Ndx != SHN_UNDEF && Ndx < SHN_LORESERVE)
902     return Ndx;
903 
904   auto CreateErr = [&](const Twine &Name, Optional<unsigned> Offset = None) {
905     std::string Desc;
906     if (Offset)
907       Desc = (Name + "+0x" + Twine::utohexstr(*Offset)).str();
908     else
909       Desc = Name.str();
910     return createError(
911         "unable to get section index for symbol with st_shndx = 0x" +
912         Twine::utohexstr(Ndx) + " (" + Desc + ")");
913   };
914 
915   if (Ndx >= ELF::SHN_LOPROC && Ndx <= ELF::SHN_HIPROC)
916     return CreateErr("SHN_LOPROC", Ndx - ELF::SHN_LOPROC);
917   if (Ndx >= ELF::SHN_LOOS && Ndx <= ELF::SHN_HIOS)
918     return CreateErr("SHN_LOOS", Ndx - ELF::SHN_LOOS);
919   if (Ndx == ELF::SHN_UNDEF)
920     return CreateErr("SHN_UNDEF");
921   if (Ndx == ELF::SHN_ABS)
922     return CreateErr("SHN_ABS");
923   if (Ndx == ELF::SHN_COMMON)
924     return CreateErr("SHN_COMMON");
925   return CreateErr("SHN_LORESERVE", Ndx - SHN_LORESERVE);
926 }
927 
928 template <typename ELFT>
929 Expected<StringRef>
930 ELFDumper<ELFT>::getSymbolSectionName(const Elf_Sym &Symbol,
931                                       unsigned SectionIndex) const {
932   Expected<const Elf_Shdr *> SecOrErr = Obj.getSection(SectionIndex);
933   if (!SecOrErr)
934     return SecOrErr.takeError();
935   return Obj.getSectionName(**SecOrErr);
936 }
937 
938 template <class ELFO>
939 static const typename ELFO::Elf_Shdr *
940 findNotEmptySectionByAddress(const ELFO &Obj, StringRef FileName,
941                              uint64_t Addr) {
942   for (const typename ELFO::Elf_Shdr &Shdr : cantFail(Obj.sections()))
943     if (Shdr.sh_addr == Addr && Shdr.sh_size > 0)
944       return &Shdr;
945   return nullptr;
946 }
947 
948 static const EnumEntry<unsigned> ElfClass[] = {
949   {"None",   "none",   ELF::ELFCLASSNONE},
950   {"32-bit", "ELF32",  ELF::ELFCLASS32},
951   {"64-bit", "ELF64",  ELF::ELFCLASS64},
952 };
953 
954 static const EnumEntry<unsigned> ElfDataEncoding[] = {
955   {"None",         "none",                          ELF::ELFDATANONE},
956   {"LittleEndian", "2's complement, little endian", ELF::ELFDATA2LSB},
957   {"BigEndian",    "2's complement, big endian",    ELF::ELFDATA2MSB},
958 };
959 
960 static const EnumEntry<unsigned> ElfObjectFileType[] = {
961   {"None",         "NONE (none)",              ELF::ET_NONE},
962   {"Relocatable",  "REL (Relocatable file)",   ELF::ET_REL},
963   {"Executable",   "EXEC (Executable file)",   ELF::ET_EXEC},
964   {"SharedObject", "DYN (Shared object file)", ELF::ET_DYN},
965   {"Core",         "CORE (Core file)",         ELF::ET_CORE},
966 };
967 
968 static const EnumEntry<unsigned> ElfOSABI[] = {
969   {"SystemV",      "UNIX - System V",      ELF::ELFOSABI_NONE},
970   {"HPUX",         "UNIX - HP-UX",         ELF::ELFOSABI_HPUX},
971   {"NetBSD",       "UNIX - NetBSD",        ELF::ELFOSABI_NETBSD},
972   {"GNU/Linux",    "UNIX - GNU",           ELF::ELFOSABI_LINUX},
973   {"GNU/Hurd",     "GNU/Hurd",             ELF::ELFOSABI_HURD},
974   {"Solaris",      "UNIX - Solaris",       ELF::ELFOSABI_SOLARIS},
975   {"AIX",          "UNIX - AIX",           ELF::ELFOSABI_AIX},
976   {"IRIX",         "UNIX - IRIX",          ELF::ELFOSABI_IRIX},
977   {"FreeBSD",      "UNIX - FreeBSD",       ELF::ELFOSABI_FREEBSD},
978   {"TRU64",        "UNIX - TRU64",         ELF::ELFOSABI_TRU64},
979   {"Modesto",      "Novell - Modesto",     ELF::ELFOSABI_MODESTO},
980   {"OpenBSD",      "UNIX - OpenBSD",       ELF::ELFOSABI_OPENBSD},
981   {"OpenVMS",      "VMS - OpenVMS",        ELF::ELFOSABI_OPENVMS},
982   {"NSK",          "HP - Non-Stop Kernel", ELF::ELFOSABI_NSK},
983   {"AROS",         "AROS",                 ELF::ELFOSABI_AROS},
984   {"FenixOS",      "FenixOS",              ELF::ELFOSABI_FENIXOS},
985   {"CloudABI",     "CloudABI",             ELF::ELFOSABI_CLOUDABI},
986   {"Standalone",   "Standalone App",       ELF::ELFOSABI_STANDALONE}
987 };
988 
989 static const EnumEntry<unsigned> AMDGPUElfOSABI[] = {
990   {"AMDGPU_HSA",    "AMDGPU - HSA",    ELF::ELFOSABI_AMDGPU_HSA},
991   {"AMDGPU_PAL",    "AMDGPU - PAL",    ELF::ELFOSABI_AMDGPU_PAL},
992   {"AMDGPU_MESA3D", "AMDGPU - MESA3D", ELF::ELFOSABI_AMDGPU_MESA3D}
993 };
994 
995 static const EnumEntry<unsigned> ARMElfOSABI[] = {
996   {"ARM", "ARM", ELF::ELFOSABI_ARM}
997 };
998 
999 static const EnumEntry<unsigned> C6000ElfOSABI[] = {
1000   {"C6000_ELFABI", "Bare-metal C6000", ELF::ELFOSABI_C6000_ELFABI},
1001   {"C6000_LINUX",  "Linux C6000",      ELF::ELFOSABI_C6000_LINUX}
1002 };
1003 
1004 static const EnumEntry<unsigned> ElfMachineType[] = {
1005   ENUM_ENT(EM_NONE,          "None"),
1006   ENUM_ENT(EM_M32,           "WE32100"),
1007   ENUM_ENT(EM_SPARC,         "Sparc"),
1008   ENUM_ENT(EM_386,           "Intel 80386"),
1009   ENUM_ENT(EM_68K,           "MC68000"),
1010   ENUM_ENT(EM_88K,           "MC88000"),
1011   ENUM_ENT(EM_IAMCU,         "EM_IAMCU"),
1012   ENUM_ENT(EM_860,           "Intel 80860"),
1013   ENUM_ENT(EM_MIPS,          "MIPS R3000"),
1014   ENUM_ENT(EM_S370,          "IBM System/370"),
1015   ENUM_ENT(EM_MIPS_RS3_LE,   "MIPS R3000 little-endian"),
1016   ENUM_ENT(EM_PARISC,        "HPPA"),
1017   ENUM_ENT(EM_VPP500,        "Fujitsu VPP500"),
1018   ENUM_ENT(EM_SPARC32PLUS,   "Sparc v8+"),
1019   ENUM_ENT(EM_960,           "Intel 80960"),
1020   ENUM_ENT(EM_PPC,           "PowerPC"),
1021   ENUM_ENT(EM_PPC64,         "PowerPC64"),
1022   ENUM_ENT(EM_S390,          "IBM S/390"),
1023   ENUM_ENT(EM_SPU,           "SPU"),
1024   ENUM_ENT(EM_V800,          "NEC V800 series"),
1025   ENUM_ENT(EM_FR20,          "Fujistsu FR20"),
1026   ENUM_ENT(EM_RH32,          "TRW RH-32"),
1027   ENUM_ENT(EM_RCE,           "Motorola RCE"),
1028   ENUM_ENT(EM_ARM,           "ARM"),
1029   ENUM_ENT(EM_ALPHA,         "EM_ALPHA"),
1030   ENUM_ENT(EM_SH,            "Hitachi SH"),
1031   ENUM_ENT(EM_SPARCV9,       "Sparc v9"),
1032   ENUM_ENT(EM_TRICORE,       "Siemens Tricore"),
1033   ENUM_ENT(EM_ARC,           "ARC"),
1034   ENUM_ENT(EM_H8_300,        "Hitachi H8/300"),
1035   ENUM_ENT(EM_H8_300H,       "Hitachi H8/300H"),
1036   ENUM_ENT(EM_H8S,           "Hitachi H8S"),
1037   ENUM_ENT(EM_H8_500,        "Hitachi H8/500"),
1038   ENUM_ENT(EM_IA_64,         "Intel IA-64"),
1039   ENUM_ENT(EM_MIPS_X,        "Stanford MIPS-X"),
1040   ENUM_ENT(EM_COLDFIRE,      "Motorola Coldfire"),
1041   ENUM_ENT(EM_68HC12,        "Motorola MC68HC12 Microcontroller"),
1042   ENUM_ENT(EM_MMA,           "Fujitsu Multimedia Accelerator"),
1043   ENUM_ENT(EM_PCP,           "Siemens PCP"),
1044   ENUM_ENT(EM_NCPU,          "Sony nCPU embedded RISC processor"),
1045   ENUM_ENT(EM_NDR1,          "Denso NDR1 microprocesspr"),
1046   ENUM_ENT(EM_STARCORE,      "Motorola Star*Core processor"),
1047   ENUM_ENT(EM_ME16,          "Toyota ME16 processor"),
1048   ENUM_ENT(EM_ST100,         "STMicroelectronics ST100 processor"),
1049   ENUM_ENT(EM_TINYJ,         "Advanced Logic Corp. TinyJ embedded processor"),
1050   ENUM_ENT(EM_X86_64,        "Advanced Micro Devices X86-64"),
1051   ENUM_ENT(EM_PDSP,          "Sony DSP processor"),
1052   ENUM_ENT(EM_PDP10,         "Digital Equipment Corp. PDP-10"),
1053   ENUM_ENT(EM_PDP11,         "Digital Equipment Corp. PDP-11"),
1054   ENUM_ENT(EM_FX66,          "Siemens FX66 microcontroller"),
1055   ENUM_ENT(EM_ST9PLUS,       "STMicroelectronics ST9+ 8/16 bit microcontroller"),
1056   ENUM_ENT(EM_ST7,           "STMicroelectronics ST7 8-bit microcontroller"),
1057   ENUM_ENT(EM_68HC16,        "Motorola MC68HC16 Microcontroller"),
1058   ENUM_ENT(EM_68HC11,        "Motorola MC68HC11 Microcontroller"),
1059   ENUM_ENT(EM_68HC08,        "Motorola MC68HC08 Microcontroller"),
1060   ENUM_ENT(EM_68HC05,        "Motorola MC68HC05 Microcontroller"),
1061   ENUM_ENT(EM_SVX,           "Silicon Graphics SVx"),
1062   ENUM_ENT(EM_ST19,          "STMicroelectronics ST19 8-bit microcontroller"),
1063   ENUM_ENT(EM_VAX,           "Digital VAX"),
1064   ENUM_ENT(EM_CRIS,          "Axis Communications 32-bit embedded processor"),
1065   ENUM_ENT(EM_JAVELIN,       "Infineon Technologies 32-bit embedded cpu"),
1066   ENUM_ENT(EM_FIREPATH,      "Element 14 64-bit DSP processor"),
1067   ENUM_ENT(EM_ZSP,           "LSI Logic's 16-bit DSP processor"),
1068   ENUM_ENT(EM_MMIX,          "Donald Knuth's educational 64-bit processor"),
1069   ENUM_ENT(EM_HUANY,         "Harvard Universitys's machine-independent object format"),
1070   ENUM_ENT(EM_PRISM,         "Vitesse Prism"),
1071   ENUM_ENT(EM_AVR,           "Atmel AVR 8-bit microcontroller"),
1072   ENUM_ENT(EM_FR30,          "Fujitsu FR30"),
1073   ENUM_ENT(EM_D10V,          "Mitsubishi D10V"),
1074   ENUM_ENT(EM_D30V,          "Mitsubishi D30V"),
1075   ENUM_ENT(EM_V850,          "NEC v850"),
1076   ENUM_ENT(EM_M32R,          "Renesas M32R (formerly Mitsubishi M32r)"),
1077   ENUM_ENT(EM_MN10300,       "Matsushita MN10300"),
1078   ENUM_ENT(EM_MN10200,       "Matsushita MN10200"),
1079   ENUM_ENT(EM_PJ,            "picoJava"),
1080   ENUM_ENT(EM_OPENRISC,      "OpenRISC 32-bit embedded processor"),
1081   ENUM_ENT(EM_ARC_COMPACT,   "EM_ARC_COMPACT"),
1082   ENUM_ENT(EM_XTENSA,        "Tensilica Xtensa Processor"),
1083   ENUM_ENT(EM_VIDEOCORE,     "Alphamosaic VideoCore processor"),
1084   ENUM_ENT(EM_TMM_GPP,       "Thompson Multimedia General Purpose Processor"),
1085   ENUM_ENT(EM_NS32K,         "National Semiconductor 32000 series"),
1086   ENUM_ENT(EM_TPC,           "Tenor Network TPC processor"),
1087   ENUM_ENT(EM_SNP1K,         "EM_SNP1K"),
1088   ENUM_ENT(EM_ST200,         "STMicroelectronics ST200 microcontroller"),
1089   ENUM_ENT(EM_IP2K,          "Ubicom IP2xxx 8-bit microcontrollers"),
1090   ENUM_ENT(EM_MAX,           "MAX Processor"),
1091   ENUM_ENT(EM_CR,            "National Semiconductor CompactRISC"),
1092   ENUM_ENT(EM_F2MC16,        "Fujitsu F2MC16"),
1093   ENUM_ENT(EM_MSP430,        "Texas Instruments msp430 microcontroller"),
1094   ENUM_ENT(EM_BLACKFIN,      "Analog Devices Blackfin"),
1095   ENUM_ENT(EM_SE_C33,        "S1C33 Family of Seiko Epson processors"),
1096   ENUM_ENT(EM_SEP,           "Sharp embedded microprocessor"),
1097   ENUM_ENT(EM_ARCA,          "Arca RISC microprocessor"),
1098   ENUM_ENT(EM_UNICORE,       "Unicore"),
1099   ENUM_ENT(EM_EXCESS,        "eXcess 16/32/64-bit configurable embedded CPU"),
1100   ENUM_ENT(EM_DXP,           "Icera Semiconductor Inc. Deep Execution Processor"),
1101   ENUM_ENT(EM_ALTERA_NIOS2,  "Altera Nios"),
1102   ENUM_ENT(EM_CRX,           "National Semiconductor CRX microprocessor"),
1103   ENUM_ENT(EM_XGATE,         "Motorola XGATE embedded processor"),
1104   ENUM_ENT(EM_C166,          "Infineon Technologies xc16x"),
1105   ENUM_ENT(EM_M16C,          "Renesas M16C"),
1106   ENUM_ENT(EM_DSPIC30F,      "Microchip Technology dsPIC30F Digital Signal Controller"),
1107   ENUM_ENT(EM_CE,            "Freescale Communication Engine RISC core"),
1108   ENUM_ENT(EM_M32C,          "Renesas M32C"),
1109   ENUM_ENT(EM_TSK3000,       "Altium TSK3000 core"),
1110   ENUM_ENT(EM_RS08,          "Freescale RS08 embedded processor"),
1111   ENUM_ENT(EM_SHARC,         "EM_SHARC"),
1112   ENUM_ENT(EM_ECOG2,         "Cyan Technology eCOG2 microprocessor"),
1113   ENUM_ENT(EM_SCORE7,        "SUNPLUS S+Core"),
1114   ENUM_ENT(EM_DSP24,         "New Japan Radio (NJR) 24-bit DSP Processor"),
1115   ENUM_ENT(EM_VIDEOCORE3,    "Broadcom VideoCore III processor"),
1116   ENUM_ENT(EM_LATTICEMICO32, "Lattice Mico32"),
1117   ENUM_ENT(EM_SE_C17,        "Seiko Epson C17 family"),
1118   ENUM_ENT(EM_TI_C6000,      "Texas Instruments TMS320C6000 DSP family"),
1119   ENUM_ENT(EM_TI_C2000,      "Texas Instruments TMS320C2000 DSP family"),
1120   ENUM_ENT(EM_TI_C5500,      "Texas Instruments TMS320C55x DSP family"),
1121   ENUM_ENT(EM_MMDSP_PLUS,    "STMicroelectronics 64bit VLIW Data Signal Processor"),
1122   ENUM_ENT(EM_CYPRESS_M8C,   "Cypress M8C microprocessor"),
1123   ENUM_ENT(EM_R32C,          "Renesas R32C series microprocessors"),
1124   ENUM_ENT(EM_TRIMEDIA,      "NXP Semiconductors TriMedia architecture family"),
1125   ENUM_ENT(EM_HEXAGON,       "Qualcomm Hexagon"),
1126   ENUM_ENT(EM_8051,          "Intel 8051 and variants"),
1127   ENUM_ENT(EM_STXP7X,        "STMicroelectronics STxP7x family"),
1128   ENUM_ENT(EM_NDS32,         "Andes Technology compact code size embedded RISC processor family"),
1129   ENUM_ENT(EM_ECOG1,         "Cyan Technology eCOG1 microprocessor"),
1130   // FIXME: Following EM_ECOG1X definitions is dead code since EM_ECOG1X has
1131   //        an identical number to EM_ECOG1.
1132   ENUM_ENT(EM_ECOG1X,        "Cyan Technology eCOG1X family"),
1133   ENUM_ENT(EM_MAXQ30,        "Dallas Semiconductor MAXQ30 Core microcontrollers"),
1134   ENUM_ENT(EM_XIMO16,        "New Japan Radio (NJR) 16-bit DSP Processor"),
1135   ENUM_ENT(EM_MANIK,         "M2000 Reconfigurable RISC Microprocessor"),
1136   ENUM_ENT(EM_CRAYNV2,       "Cray Inc. NV2 vector architecture"),
1137   ENUM_ENT(EM_RX,            "Renesas RX"),
1138   ENUM_ENT(EM_METAG,         "Imagination Technologies Meta processor architecture"),
1139   ENUM_ENT(EM_MCST_ELBRUS,   "MCST Elbrus general purpose hardware architecture"),
1140   ENUM_ENT(EM_ECOG16,        "Cyan Technology eCOG16 family"),
1141   ENUM_ENT(EM_CR16,          "Xilinx MicroBlaze"),
1142   ENUM_ENT(EM_ETPU,          "Freescale Extended Time Processing Unit"),
1143   ENUM_ENT(EM_SLE9X,         "Infineon Technologies SLE9X core"),
1144   ENUM_ENT(EM_L10M,          "EM_L10M"),
1145   ENUM_ENT(EM_K10M,          "EM_K10M"),
1146   ENUM_ENT(EM_AARCH64,       "AArch64"),
1147   ENUM_ENT(EM_AVR32,         "Atmel Corporation 32-bit microprocessor family"),
1148   ENUM_ENT(EM_STM8,          "STMicroeletronics STM8 8-bit microcontroller"),
1149   ENUM_ENT(EM_TILE64,        "Tilera TILE64 multicore architecture family"),
1150   ENUM_ENT(EM_TILEPRO,       "Tilera TILEPro multicore architecture family"),
1151   ENUM_ENT(EM_CUDA,          "NVIDIA CUDA architecture"),
1152   ENUM_ENT(EM_TILEGX,        "Tilera TILE-Gx multicore architecture family"),
1153   ENUM_ENT(EM_CLOUDSHIELD,   "EM_CLOUDSHIELD"),
1154   ENUM_ENT(EM_COREA_1ST,     "EM_COREA_1ST"),
1155   ENUM_ENT(EM_COREA_2ND,     "EM_COREA_2ND"),
1156   ENUM_ENT(EM_ARC_COMPACT2,  "EM_ARC_COMPACT2"),
1157   ENUM_ENT(EM_OPEN8,         "EM_OPEN8"),
1158   ENUM_ENT(EM_RL78,          "Renesas RL78"),
1159   ENUM_ENT(EM_VIDEOCORE5,    "Broadcom VideoCore V processor"),
1160   ENUM_ENT(EM_78KOR,         "EM_78KOR"),
1161   ENUM_ENT(EM_56800EX,       "EM_56800EX"),
1162   ENUM_ENT(EM_AMDGPU,        "EM_AMDGPU"),
1163   ENUM_ENT(EM_RISCV,         "RISC-V"),
1164   ENUM_ENT(EM_LANAI,         "EM_LANAI"),
1165   ENUM_ENT(EM_BPF,           "EM_BPF"),
1166   ENUM_ENT(EM_VE,            "NEC SX-Aurora Vector Engine"),
1167 };
1168 
1169 static const EnumEntry<unsigned> ElfSymbolBindings[] = {
1170     {"Local",  "LOCAL",  ELF::STB_LOCAL},
1171     {"Global", "GLOBAL", ELF::STB_GLOBAL},
1172     {"Weak",   "WEAK",   ELF::STB_WEAK},
1173     {"Unique", "UNIQUE", ELF::STB_GNU_UNIQUE}};
1174 
1175 static const EnumEntry<unsigned> ElfSymbolVisibilities[] = {
1176     {"DEFAULT",   "DEFAULT",   ELF::STV_DEFAULT},
1177     {"INTERNAL",  "INTERNAL",  ELF::STV_INTERNAL},
1178     {"HIDDEN",    "HIDDEN",    ELF::STV_HIDDEN},
1179     {"PROTECTED", "PROTECTED", ELF::STV_PROTECTED}};
1180 
1181 static const EnumEntry<unsigned> AMDGPUSymbolTypes[] = {
1182   { "AMDGPU_HSA_KERNEL",            ELF::STT_AMDGPU_HSA_KERNEL }
1183 };
1184 
1185 static const char *getGroupType(uint32_t Flag) {
1186   if (Flag & ELF::GRP_COMDAT)
1187     return "COMDAT";
1188   else
1189     return "(unknown)";
1190 }
1191 
1192 static const EnumEntry<unsigned> ElfSectionFlags[] = {
1193   ENUM_ENT(SHF_WRITE,            "W"),
1194   ENUM_ENT(SHF_ALLOC,            "A"),
1195   ENUM_ENT(SHF_EXECINSTR,        "X"),
1196   ENUM_ENT(SHF_MERGE,            "M"),
1197   ENUM_ENT(SHF_STRINGS,          "S"),
1198   ENUM_ENT(SHF_INFO_LINK,        "I"),
1199   ENUM_ENT(SHF_LINK_ORDER,       "L"),
1200   ENUM_ENT(SHF_OS_NONCONFORMING, "O"),
1201   ENUM_ENT(SHF_GROUP,            "G"),
1202   ENUM_ENT(SHF_TLS,              "T"),
1203   ENUM_ENT(SHF_COMPRESSED,       "C"),
1204   ENUM_ENT(SHF_GNU_RETAIN,       "R"),
1205   ENUM_ENT(SHF_EXCLUDE,          "E"),
1206 };
1207 
1208 static const EnumEntry<unsigned> ElfXCoreSectionFlags[] = {
1209   ENUM_ENT(XCORE_SHF_CP_SECTION, ""),
1210   ENUM_ENT(XCORE_SHF_DP_SECTION, "")
1211 };
1212 
1213 static const EnumEntry<unsigned> ElfARMSectionFlags[] = {
1214   ENUM_ENT(SHF_ARM_PURECODE, "y")
1215 };
1216 
1217 static const EnumEntry<unsigned> ElfHexagonSectionFlags[] = {
1218   ENUM_ENT(SHF_HEX_GPREL, "")
1219 };
1220 
1221 static const EnumEntry<unsigned> ElfMipsSectionFlags[] = {
1222   ENUM_ENT(SHF_MIPS_NODUPES, ""),
1223   ENUM_ENT(SHF_MIPS_NAMES,   ""),
1224   ENUM_ENT(SHF_MIPS_LOCAL,   ""),
1225   ENUM_ENT(SHF_MIPS_NOSTRIP, ""),
1226   ENUM_ENT(SHF_MIPS_GPREL,   ""),
1227   ENUM_ENT(SHF_MIPS_MERGE,   ""),
1228   ENUM_ENT(SHF_MIPS_ADDR,    ""),
1229   ENUM_ENT(SHF_MIPS_STRING,  "")
1230 };
1231 
1232 static const EnumEntry<unsigned> ElfX86_64SectionFlags[] = {
1233   ENUM_ENT(SHF_X86_64_LARGE, "l")
1234 };
1235 
1236 static std::vector<EnumEntry<unsigned>>
1237 getSectionFlagsForTarget(unsigned EMachine) {
1238   std::vector<EnumEntry<unsigned>> Ret(std::begin(ElfSectionFlags),
1239                                        std::end(ElfSectionFlags));
1240   switch (EMachine) {
1241   case EM_ARM:
1242     Ret.insert(Ret.end(), std::begin(ElfARMSectionFlags),
1243                std::end(ElfARMSectionFlags));
1244     break;
1245   case EM_HEXAGON:
1246     Ret.insert(Ret.end(), std::begin(ElfHexagonSectionFlags),
1247                std::end(ElfHexagonSectionFlags));
1248     break;
1249   case EM_MIPS:
1250     Ret.insert(Ret.end(), std::begin(ElfMipsSectionFlags),
1251                std::end(ElfMipsSectionFlags));
1252     break;
1253   case EM_X86_64:
1254     Ret.insert(Ret.end(), std::begin(ElfX86_64SectionFlags),
1255                std::end(ElfX86_64SectionFlags));
1256     break;
1257   case EM_XCORE:
1258     Ret.insert(Ret.end(), std::begin(ElfXCoreSectionFlags),
1259                std::end(ElfXCoreSectionFlags));
1260     break;
1261   default:
1262     break;
1263   }
1264   return Ret;
1265 }
1266 
1267 static std::string getGNUFlags(unsigned EMachine, uint64_t Flags) {
1268   // Here we are trying to build the flags string in the same way as GNU does.
1269   // It is not that straightforward. Imagine we have sh_flags == 0x90000000.
1270   // SHF_EXCLUDE ("E") has a value of 0x80000000 and SHF_MASKPROC is 0xf0000000.
1271   // GNU readelf will not print "E" or "Ep" in this case, but will print just
1272   // "p". It only will print "E" when no other processor flag is set.
1273   std::string Str;
1274   bool HasUnknownFlag = false;
1275   bool HasOSFlag = false;
1276   bool HasProcFlag = false;
1277   std::vector<EnumEntry<unsigned>> FlagsList =
1278       getSectionFlagsForTarget(EMachine);
1279   while (Flags) {
1280     // Take the least significant bit as a flag.
1281     uint64_t Flag = Flags & -Flags;
1282     Flags -= Flag;
1283 
1284     // Find the flag in the known flags list.
1285     auto I = llvm::find_if(FlagsList, [=](const EnumEntry<unsigned> &E) {
1286       // Flags with empty names are not printed in GNU style output.
1287       return E.Value == Flag && !E.AltName.empty();
1288     });
1289     if (I != FlagsList.end()) {
1290       Str += I->AltName;
1291       continue;
1292     }
1293 
1294     // If we did not find a matching regular flag, then we deal with an OS
1295     // specific flag, processor specific flag or an unknown flag.
1296     if (Flag & ELF::SHF_MASKOS) {
1297       HasOSFlag = true;
1298       Flags &= ~ELF::SHF_MASKOS;
1299     } else if (Flag & ELF::SHF_MASKPROC) {
1300       HasProcFlag = true;
1301       // Mask off all the processor-specific bits. This removes the SHF_EXCLUDE
1302       // bit if set so that it doesn't also get printed.
1303       Flags &= ~ELF::SHF_MASKPROC;
1304     } else {
1305       HasUnknownFlag = true;
1306     }
1307   }
1308 
1309   // "o", "p" and "x" are printed last.
1310   if (HasOSFlag)
1311     Str += "o";
1312   if (HasProcFlag)
1313     Str += "p";
1314   if (HasUnknownFlag)
1315     Str += "x";
1316   return Str;
1317 }
1318 
1319 static StringRef segmentTypeToString(unsigned Arch, unsigned Type) {
1320   // Check potentially overlapped processor-specific program header type.
1321   switch (Arch) {
1322   case ELF::EM_ARM:
1323     switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, PT_ARM_EXIDX); }
1324     break;
1325   case ELF::EM_MIPS:
1326   case ELF::EM_MIPS_RS3_LE:
1327     switch (Type) {
1328       LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_REGINFO);
1329       LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_RTPROC);
1330       LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_OPTIONS);
1331       LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_ABIFLAGS);
1332     }
1333     break;
1334   }
1335 
1336   switch (Type) {
1337     LLVM_READOBJ_ENUM_CASE(ELF, PT_NULL);
1338     LLVM_READOBJ_ENUM_CASE(ELF, PT_LOAD);
1339     LLVM_READOBJ_ENUM_CASE(ELF, PT_DYNAMIC);
1340     LLVM_READOBJ_ENUM_CASE(ELF, PT_INTERP);
1341     LLVM_READOBJ_ENUM_CASE(ELF, PT_NOTE);
1342     LLVM_READOBJ_ENUM_CASE(ELF, PT_SHLIB);
1343     LLVM_READOBJ_ENUM_CASE(ELF, PT_PHDR);
1344     LLVM_READOBJ_ENUM_CASE(ELF, PT_TLS);
1345 
1346     LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_EH_FRAME);
1347     LLVM_READOBJ_ENUM_CASE(ELF, PT_SUNW_UNWIND);
1348 
1349     LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_STACK);
1350     LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_RELRO);
1351     LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_PROPERTY);
1352 
1353     LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_RANDOMIZE);
1354     LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_WXNEEDED);
1355     LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_BOOTDATA);
1356   default:
1357     return "";
1358   }
1359 }
1360 
1361 static std::string getGNUPtType(unsigned Arch, unsigned Type) {
1362   StringRef Seg = segmentTypeToString(Arch, Type);
1363   if (Seg.empty())
1364     return std::string("<unknown>: ") + to_string(format_hex(Type, 1));
1365 
1366   // E.g. "PT_ARM_EXIDX" -> "EXIDX".
1367   if (Seg.startswith("PT_ARM_"))
1368     return Seg.drop_front(7).str();
1369 
1370   // E.g. "PT_MIPS_REGINFO" -> "REGINFO".
1371   if (Seg.startswith("PT_MIPS_"))
1372     return Seg.drop_front(8).str();
1373 
1374   // E.g. "PT_LOAD" -> "LOAD".
1375   assert(Seg.startswith("PT_"));
1376   return Seg.drop_front(3).str();
1377 }
1378 
1379 static const EnumEntry<unsigned> ElfSegmentFlags[] = {
1380   LLVM_READOBJ_ENUM_ENT(ELF, PF_X),
1381   LLVM_READOBJ_ENUM_ENT(ELF, PF_W),
1382   LLVM_READOBJ_ENUM_ENT(ELF, PF_R)
1383 };
1384 
1385 static const EnumEntry<unsigned> ElfHeaderMipsFlags[] = {
1386   ENUM_ENT(EF_MIPS_NOREORDER, "noreorder"),
1387   ENUM_ENT(EF_MIPS_PIC, "pic"),
1388   ENUM_ENT(EF_MIPS_CPIC, "cpic"),
1389   ENUM_ENT(EF_MIPS_ABI2, "abi2"),
1390   ENUM_ENT(EF_MIPS_32BITMODE, "32bitmode"),
1391   ENUM_ENT(EF_MIPS_FP64, "fp64"),
1392   ENUM_ENT(EF_MIPS_NAN2008, "nan2008"),
1393   ENUM_ENT(EF_MIPS_ABI_O32, "o32"),
1394   ENUM_ENT(EF_MIPS_ABI_O64, "o64"),
1395   ENUM_ENT(EF_MIPS_ABI_EABI32, "eabi32"),
1396   ENUM_ENT(EF_MIPS_ABI_EABI64, "eabi64"),
1397   ENUM_ENT(EF_MIPS_MACH_3900, "3900"),
1398   ENUM_ENT(EF_MIPS_MACH_4010, "4010"),
1399   ENUM_ENT(EF_MIPS_MACH_4100, "4100"),
1400   ENUM_ENT(EF_MIPS_MACH_4650, "4650"),
1401   ENUM_ENT(EF_MIPS_MACH_4120, "4120"),
1402   ENUM_ENT(EF_MIPS_MACH_4111, "4111"),
1403   ENUM_ENT(EF_MIPS_MACH_SB1, "sb1"),
1404   ENUM_ENT(EF_MIPS_MACH_OCTEON, "octeon"),
1405   ENUM_ENT(EF_MIPS_MACH_XLR, "xlr"),
1406   ENUM_ENT(EF_MIPS_MACH_OCTEON2, "octeon2"),
1407   ENUM_ENT(EF_MIPS_MACH_OCTEON3, "octeon3"),
1408   ENUM_ENT(EF_MIPS_MACH_5400, "5400"),
1409   ENUM_ENT(EF_MIPS_MACH_5900, "5900"),
1410   ENUM_ENT(EF_MIPS_MACH_5500, "5500"),
1411   ENUM_ENT(EF_MIPS_MACH_9000, "9000"),
1412   ENUM_ENT(EF_MIPS_MACH_LS2E, "loongson-2e"),
1413   ENUM_ENT(EF_MIPS_MACH_LS2F, "loongson-2f"),
1414   ENUM_ENT(EF_MIPS_MACH_LS3A, "loongson-3a"),
1415   ENUM_ENT(EF_MIPS_MICROMIPS, "micromips"),
1416   ENUM_ENT(EF_MIPS_ARCH_ASE_M16, "mips16"),
1417   ENUM_ENT(EF_MIPS_ARCH_ASE_MDMX, "mdmx"),
1418   ENUM_ENT(EF_MIPS_ARCH_1, "mips1"),
1419   ENUM_ENT(EF_MIPS_ARCH_2, "mips2"),
1420   ENUM_ENT(EF_MIPS_ARCH_3, "mips3"),
1421   ENUM_ENT(EF_MIPS_ARCH_4, "mips4"),
1422   ENUM_ENT(EF_MIPS_ARCH_5, "mips5"),
1423   ENUM_ENT(EF_MIPS_ARCH_32, "mips32"),
1424   ENUM_ENT(EF_MIPS_ARCH_64, "mips64"),
1425   ENUM_ENT(EF_MIPS_ARCH_32R2, "mips32r2"),
1426   ENUM_ENT(EF_MIPS_ARCH_64R2, "mips64r2"),
1427   ENUM_ENT(EF_MIPS_ARCH_32R6, "mips32r6"),
1428   ENUM_ENT(EF_MIPS_ARCH_64R6, "mips64r6")
1429 };
1430 
1431 static const EnumEntry<unsigned> ElfHeaderAMDGPUFlags[] = {
1432   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_NONE),
1433   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R600),
1434   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R630),
1435   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RS880),
1436   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV670),
1437   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV710),
1438   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV730),
1439   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV770),
1440   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CEDAR),
1441   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CYPRESS),
1442   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_JUNIPER),
1443   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_REDWOOD),
1444   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_SUMO),
1445   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_BARTS),
1446   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAICOS),
1447   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAYMAN),
1448   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_TURKS),
1449   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX600),
1450   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX601),
1451   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX602),
1452   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX700),
1453   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX701),
1454   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX702),
1455   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX703),
1456   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX704),
1457   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX705),
1458   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX801),
1459   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX802),
1460   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX803),
1461   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX805),
1462   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX810),
1463   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX900),
1464   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX902),
1465   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX904),
1466   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX906),
1467   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX908),
1468   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX909),
1469   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX90A),
1470   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX90C),
1471   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1010),
1472   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1011),
1473   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1012),
1474   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1030),
1475   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1031),
1476   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1032),
1477   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1033),
1478   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_XNACK),
1479   LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_SRAM_ECC)
1480 };
1481 
1482 static const EnumEntry<unsigned> ElfHeaderRISCVFlags[] = {
1483   ENUM_ENT(EF_RISCV_RVC, "RVC"),
1484   ENUM_ENT(EF_RISCV_FLOAT_ABI_SINGLE, "single-float ABI"),
1485   ENUM_ENT(EF_RISCV_FLOAT_ABI_DOUBLE, "double-float ABI"),
1486   ENUM_ENT(EF_RISCV_FLOAT_ABI_QUAD, "quad-float ABI"),
1487   ENUM_ENT(EF_RISCV_RVE, "RVE")
1488 };
1489 
1490 static const EnumEntry<unsigned> ElfSymOtherFlags[] = {
1491   LLVM_READOBJ_ENUM_ENT(ELF, STV_INTERNAL),
1492   LLVM_READOBJ_ENUM_ENT(ELF, STV_HIDDEN),
1493   LLVM_READOBJ_ENUM_ENT(ELF, STV_PROTECTED)
1494 };
1495 
1496 static const EnumEntry<unsigned> ElfMipsSymOtherFlags[] = {
1497   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL),
1498   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT),
1499   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PIC),
1500   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MICROMIPS)
1501 };
1502 
1503 static const EnumEntry<unsigned> ElfAArch64SymOtherFlags[] = {
1504   LLVM_READOBJ_ENUM_ENT(ELF, STO_AARCH64_VARIANT_PCS)
1505 };
1506 
1507 static const EnumEntry<unsigned> ElfMips16SymOtherFlags[] = {
1508   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL),
1509   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT),
1510   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MIPS16)
1511 };
1512 
1513 static const char *getElfMipsOptionsOdkType(unsigned Odk) {
1514   switch (Odk) {
1515   LLVM_READOBJ_ENUM_CASE(ELF, ODK_NULL);
1516   LLVM_READOBJ_ENUM_CASE(ELF, ODK_REGINFO);
1517   LLVM_READOBJ_ENUM_CASE(ELF, ODK_EXCEPTIONS);
1518   LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAD);
1519   LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWPATCH);
1520   LLVM_READOBJ_ENUM_CASE(ELF, ODK_FILL);
1521   LLVM_READOBJ_ENUM_CASE(ELF, ODK_TAGS);
1522   LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWAND);
1523   LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWOR);
1524   LLVM_READOBJ_ENUM_CASE(ELF, ODK_GP_GROUP);
1525   LLVM_READOBJ_ENUM_CASE(ELF, ODK_IDENT);
1526   LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAGESIZE);
1527   default:
1528     return "Unknown";
1529   }
1530 }
1531 
1532 template <typename ELFT>
1533 std::pair<const typename ELFT::Phdr *, const typename ELFT::Shdr *>
1534 ELFDumper<ELFT>::findDynamic() {
1535   // Try to locate the PT_DYNAMIC header.
1536   const Elf_Phdr *DynamicPhdr = nullptr;
1537   if (Expected<ArrayRef<Elf_Phdr>> PhdrsOrErr = Obj.program_headers()) {
1538     for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
1539       if (Phdr.p_type != ELF::PT_DYNAMIC)
1540         continue;
1541       DynamicPhdr = &Phdr;
1542       break;
1543     }
1544   } else {
1545     reportUniqueWarning(
1546         "unable to read program headers to locate the PT_DYNAMIC segment: " +
1547         toString(PhdrsOrErr.takeError()));
1548   }
1549 
1550   // Try to locate the .dynamic section in the sections header table.
1551   const Elf_Shdr *DynamicSec = nullptr;
1552   for (const Elf_Shdr &Sec : cantFail(Obj.sections())) {
1553     if (Sec.sh_type != ELF::SHT_DYNAMIC)
1554       continue;
1555     DynamicSec = &Sec;
1556     break;
1557   }
1558 
1559   if (DynamicPhdr && ((DynamicPhdr->p_offset + DynamicPhdr->p_filesz >
1560                        ObjF.getMemoryBufferRef().getBufferSize()) ||
1561                       (DynamicPhdr->p_offset + DynamicPhdr->p_filesz <
1562                        DynamicPhdr->p_offset))) {
1563     reportUniqueWarning(
1564         "PT_DYNAMIC segment offset (0x" +
1565         Twine::utohexstr(DynamicPhdr->p_offset) + ") + file size (0x" +
1566         Twine::utohexstr(DynamicPhdr->p_filesz) +
1567         ") exceeds the size of the file (0x" +
1568         Twine::utohexstr(ObjF.getMemoryBufferRef().getBufferSize()) + ")");
1569     // Don't use the broken dynamic header.
1570     DynamicPhdr = nullptr;
1571   }
1572 
1573   if (DynamicPhdr && DynamicSec) {
1574     if (DynamicSec->sh_addr + DynamicSec->sh_size >
1575             DynamicPhdr->p_vaddr + DynamicPhdr->p_memsz ||
1576         DynamicSec->sh_addr < DynamicPhdr->p_vaddr)
1577       reportUniqueWarning(describe(*DynamicSec) +
1578                           " is not contained within the "
1579                           "PT_DYNAMIC segment");
1580 
1581     if (DynamicSec->sh_addr != DynamicPhdr->p_vaddr)
1582       reportUniqueWarning(describe(*DynamicSec) + " is not at the start of "
1583                                                   "PT_DYNAMIC segment");
1584   }
1585 
1586   return std::make_pair(DynamicPhdr, DynamicSec);
1587 }
1588 
1589 template <typename ELFT>
1590 void ELFDumper<ELFT>::loadDynamicTable() {
1591   const Elf_Phdr *DynamicPhdr;
1592   const Elf_Shdr *DynamicSec;
1593   std::tie(DynamicPhdr, DynamicSec) = findDynamic();
1594   if (!DynamicPhdr && !DynamicSec)
1595     return;
1596 
1597   DynRegionInfo FromPhdr(ObjF, *this);
1598   bool IsPhdrTableValid = false;
1599   if (DynamicPhdr) {
1600     // Use cantFail(), because p_offset/p_filesz fields of a PT_DYNAMIC are
1601     // validated in findDynamic() and so createDRI() is not expected to fail.
1602     FromPhdr = cantFail(createDRI(DynamicPhdr->p_offset, DynamicPhdr->p_filesz,
1603                                   sizeof(Elf_Dyn)));
1604     FromPhdr.SizePrintName = "PT_DYNAMIC size";
1605     FromPhdr.EntSizePrintName = "";
1606     IsPhdrTableValid = !FromPhdr.template getAsArrayRef<Elf_Dyn>().empty();
1607   }
1608 
1609   // Locate the dynamic table described in a section header.
1610   // Ignore sh_entsize and use the expected value for entry size explicitly.
1611   // This allows us to dump dynamic sections with a broken sh_entsize
1612   // field.
1613   DynRegionInfo FromSec(ObjF, *this);
1614   bool IsSecTableValid = false;
1615   if (DynamicSec) {
1616     Expected<DynRegionInfo> RegOrErr =
1617         createDRI(DynamicSec->sh_offset, DynamicSec->sh_size, sizeof(Elf_Dyn));
1618     if (RegOrErr) {
1619       FromSec = *RegOrErr;
1620       FromSec.Context = describe(*DynamicSec);
1621       FromSec.EntSizePrintName = "";
1622       IsSecTableValid = !FromSec.template getAsArrayRef<Elf_Dyn>().empty();
1623     } else {
1624       reportUniqueWarning("unable to read the dynamic table from " +
1625                           describe(*DynamicSec) + ": " +
1626                           toString(RegOrErr.takeError()));
1627     }
1628   }
1629 
1630   // When we only have information from one of the SHT_DYNAMIC section header or
1631   // PT_DYNAMIC program header, just use that.
1632   if (!DynamicPhdr || !DynamicSec) {
1633     if ((DynamicPhdr && IsPhdrTableValid) || (DynamicSec && IsSecTableValid)) {
1634       DynamicTable = DynamicPhdr ? FromPhdr : FromSec;
1635       parseDynamicTable();
1636     } else {
1637       reportUniqueWarning("no valid dynamic table was found");
1638     }
1639     return;
1640   }
1641 
1642   // At this point we have tables found from the section header and from the
1643   // dynamic segment. Usually they match, but we have to do sanity checks to
1644   // verify that.
1645 
1646   if (FromPhdr.Addr != FromSec.Addr)
1647     reportUniqueWarning("SHT_DYNAMIC section header and PT_DYNAMIC "
1648                         "program header disagree about "
1649                         "the location of the dynamic table");
1650 
1651   if (!IsPhdrTableValid && !IsSecTableValid) {
1652     reportUniqueWarning("no valid dynamic table was found");
1653     return;
1654   }
1655 
1656   // Information in the PT_DYNAMIC program header has priority over the
1657   // information in a section header.
1658   if (IsPhdrTableValid) {
1659     if (!IsSecTableValid)
1660       reportUniqueWarning(
1661           "SHT_DYNAMIC dynamic table is invalid: PT_DYNAMIC will be used");
1662     DynamicTable = FromPhdr;
1663   } else {
1664     reportUniqueWarning(
1665         "PT_DYNAMIC dynamic table is invalid: SHT_DYNAMIC will be used");
1666     DynamicTable = FromSec;
1667   }
1668 
1669   parseDynamicTable();
1670 }
1671 
1672 template <typename ELFT>
1673 ELFDumper<ELFT>::ELFDumper(const object::ELFObjectFile<ELFT> &O,
1674                            ScopedPrinter &Writer)
1675     : ObjDumper(Writer, O.getFileName()), ObjF(O), Obj(O.getELFFile()),
1676       FileName(O.getFileName()), DynRelRegion(O, *this),
1677       DynRelaRegion(O, *this), DynRelrRegion(O, *this),
1678       DynPLTRelRegion(O, *this), DynSymTabShndxRegion(O, *this),
1679       DynamicTable(O, *this) {
1680   if (!O.IsContentValid())
1681     return;
1682 
1683   typename ELFT::ShdrRange Sections = cantFail(Obj.sections());
1684   for (const Elf_Shdr &Sec : Sections) {
1685     switch (Sec.sh_type) {
1686     case ELF::SHT_SYMTAB:
1687       if (!DotSymtabSec)
1688         DotSymtabSec = &Sec;
1689       break;
1690     case ELF::SHT_DYNSYM:
1691       if (!DotDynsymSec)
1692         DotDynsymSec = &Sec;
1693 
1694       if (!DynSymRegion) {
1695         Expected<DynRegionInfo> RegOrErr =
1696             createDRI(Sec.sh_offset, Sec.sh_size, Sec.sh_entsize);
1697         if (RegOrErr) {
1698           DynSymRegion = *RegOrErr;
1699           DynSymRegion->Context = describe(Sec);
1700 
1701           if (Expected<StringRef> E = Obj.getStringTableForSymtab(Sec))
1702             DynamicStringTable = *E;
1703           else
1704             reportUniqueWarning("unable to get the string table for the " +
1705                                 describe(Sec) + ": " + toString(E.takeError()));
1706         } else {
1707           reportUniqueWarning("unable to read dynamic symbols from " +
1708                               describe(Sec) + ": " +
1709                               toString(RegOrErr.takeError()));
1710         }
1711       }
1712       break;
1713     case ELF::SHT_SYMTAB_SHNDX: {
1714       uint32_t SymtabNdx = Sec.sh_link;
1715       if (SymtabNdx >= Sections.size()) {
1716         reportUniqueWarning(
1717             "unable to get the associated symbol table for " + describe(Sec) +
1718             ": sh_link (" + Twine(SymtabNdx) +
1719             ") is greater than or equal to the total number of sections (" +
1720             Twine(Sections.size()) + ")");
1721         continue;
1722       }
1723 
1724       if (Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
1725               Obj.getSHNDXTable(Sec)) {
1726         if (!ShndxTables.insert({&Sections[SymtabNdx], *ShndxTableOrErr})
1727                  .second)
1728           reportUniqueWarning(
1729               "multiple SHT_SYMTAB_SHNDX sections are linked to " +
1730               describe(Sec));
1731       } else {
1732         reportUniqueWarning(ShndxTableOrErr.takeError());
1733       }
1734       break;
1735     }
1736     case ELF::SHT_GNU_versym:
1737       if (!SymbolVersionSection)
1738         SymbolVersionSection = &Sec;
1739       break;
1740     case ELF::SHT_GNU_verdef:
1741       if (!SymbolVersionDefSection)
1742         SymbolVersionDefSection = &Sec;
1743       break;
1744     case ELF::SHT_GNU_verneed:
1745       if (!SymbolVersionNeedSection)
1746         SymbolVersionNeedSection = &Sec;
1747       break;
1748     case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
1749       if (!DotCGProfileSec)
1750         DotCGProfileSec = &Sec;
1751       break;
1752     case ELF::SHT_LLVM_ADDRSIG:
1753       if (!DotAddrsigSec)
1754         DotAddrsigSec = &Sec;
1755       break;
1756     }
1757   }
1758 
1759   loadDynamicTable();
1760 }
1761 
1762 template <typename ELFT> void ELFDumper<ELFT>::parseDynamicTable() {
1763   auto toMappedAddr = [&](uint64_t Tag, uint64_t VAddr) -> const uint8_t * {
1764     auto MappedAddrOrError = Obj.toMappedAddr(VAddr, [&](const Twine &Msg) {
1765       this->reportUniqueWarning(Msg);
1766       return Error::success();
1767     });
1768     if (!MappedAddrOrError) {
1769       this->reportUniqueWarning("unable to parse DT_" +
1770                                 Obj.getDynamicTagAsString(Tag) + ": " +
1771                                 llvm::toString(MappedAddrOrError.takeError()));
1772       return nullptr;
1773     }
1774     return MappedAddrOrError.get();
1775   };
1776 
1777   const char *StringTableBegin = nullptr;
1778   uint64_t StringTableSize = 0;
1779   Optional<DynRegionInfo> DynSymFromTable;
1780   for (const Elf_Dyn &Dyn : dynamic_table()) {
1781     switch (Dyn.d_tag) {
1782     case ELF::DT_HASH:
1783       HashTable = reinterpret_cast<const Elf_Hash *>(
1784           toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
1785       break;
1786     case ELF::DT_GNU_HASH:
1787       GnuHashTable = reinterpret_cast<const Elf_GnuHash *>(
1788           toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
1789       break;
1790     case ELF::DT_STRTAB:
1791       StringTableBegin = reinterpret_cast<const char *>(
1792           toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
1793       break;
1794     case ELF::DT_STRSZ:
1795       StringTableSize = Dyn.getVal();
1796       break;
1797     case ELF::DT_SYMTAB: {
1798       // If we can't map the DT_SYMTAB value to an address (e.g. when there are
1799       // no program headers), we ignore its value.
1800       if (const uint8_t *VA = toMappedAddr(Dyn.getTag(), Dyn.getPtr())) {
1801         DynSymFromTable.emplace(ObjF, *this);
1802         DynSymFromTable->Addr = VA;
1803         DynSymFromTable->EntSize = sizeof(Elf_Sym);
1804         DynSymFromTable->EntSizePrintName = "";
1805       }
1806       break;
1807     }
1808     case ELF::DT_SYMENT: {
1809       uint64_t Val = Dyn.getVal();
1810       if (Val != sizeof(Elf_Sym))
1811         this->reportUniqueWarning("DT_SYMENT value of 0x" +
1812                                   Twine::utohexstr(Val) +
1813                                   " is not the size of a symbol (0x" +
1814                                   Twine::utohexstr(sizeof(Elf_Sym)) + ")");
1815       break;
1816     }
1817     case ELF::DT_RELA:
1818       DynRelaRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
1819       break;
1820     case ELF::DT_RELASZ:
1821       DynRelaRegion.Size = Dyn.getVal();
1822       DynRelaRegion.SizePrintName = "DT_RELASZ value";
1823       break;
1824     case ELF::DT_RELAENT:
1825       DynRelaRegion.EntSize = Dyn.getVal();
1826       DynRelaRegion.EntSizePrintName = "DT_RELAENT value";
1827       break;
1828     case ELF::DT_SONAME:
1829       SONameOffset = Dyn.getVal();
1830       break;
1831     case ELF::DT_REL:
1832       DynRelRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
1833       break;
1834     case ELF::DT_RELSZ:
1835       DynRelRegion.Size = Dyn.getVal();
1836       DynRelRegion.SizePrintName = "DT_RELSZ value";
1837       break;
1838     case ELF::DT_RELENT:
1839       DynRelRegion.EntSize = Dyn.getVal();
1840       DynRelRegion.EntSizePrintName = "DT_RELENT value";
1841       break;
1842     case ELF::DT_RELR:
1843     case ELF::DT_ANDROID_RELR:
1844       DynRelrRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
1845       break;
1846     case ELF::DT_RELRSZ:
1847     case ELF::DT_ANDROID_RELRSZ:
1848       DynRelrRegion.Size = Dyn.getVal();
1849       DynRelrRegion.SizePrintName = Dyn.d_tag == ELF::DT_RELRSZ
1850                                         ? "DT_RELRSZ value"
1851                                         : "DT_ANDROID_RELRSZ value";
1852       break;
1853     case ELF::DT_RELRENT:
1854     case ELF::DT_ANDROID_RELRENT:
1855       DynRelrRegion.EntSize = Dyn.getVal();
1856       DynRelrRegion.EntSizePrintName = Dyn.d_tag == ELF::DT_RELRENT
1857                                            ? "DT_RELRENT value"
1858                                            : "DT_ANDROID_RELRENT value";
1859       break;
1860     case ELF::DT_PLTREL:
1861       if (Dyn.getVal() == DT_REL)
1862         DynPLTRelRegion.EntSize = sizeof(Elf_Rel);
1863       else if (Dyn.getVal() == DT_RELA)
1864         DynPLTRelRegion.EntSize = sizeof(Elf_Rela);
1865       else
1866         reportUniqueWarning(Twine("unknown DT_PLTREL value of ") +
1867                             Twine((uint64_t)Dyn.getVal()));
1868       DynPLTRelRegion.EntSizePrintName = "PLTREL entry size";
1869       break;
1870     case ELF::DT_JMPREL:
1871       DynPLTRelRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
1872       break;
1873     case ELF::DT_PLTRELSZ:
1874       DynPLTRelRegion.Size = Dyn.getVal();
1875       DynPLTRelRegion.SizePrintName = "DT_PLTRELSZ value";
1876       break;
1877     case ELF::DT_SYMTAB_SHNDX:
1878       DynSymTabShndxRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
1879       DynSymTabShndxRegion.EntSize = sizeof(Elf_Word);
1880       break;
1881     }
1882   }
1883 
1884   if (StringTableBegin) {
1885     const uint64_t FileSize = Obj.getBufSize();
1886     const uint64_t Offset = (const uint8_t *)StringTableBegin - Obj.base();
1887     if (StringTableSize > FileSize - Offset)
1888       reportUniqueWarning(
1889           "the dynamic string table at 0x" + Twine::utohexstr(Offset) +
1890           " goes past the end of the file (0x" + Twine::utohexstr(FileSize) +
1891           ") with DT_STRSZ = 0x" + Twine::utohexstr(StringTableSize));
1892     else
1893       DynamicStringTable = StringRef(StringTableBegin, StringTableSize);
1894   }
1895 
1896   const bool IsHashTableSupported = getHashTableEntSize() == 4;
1897   if (DynSymRegion) {
1898     // Often we find the information about the dynamic symbol table
1899     // location in the SHT_DYNSYM section header. However, the value in
1900     // DT_SYMTAB has priority, because it is used by dynamic loaders to
1901     // locate .dynsym at runtime. The location we find in the section header
1902     // and the location we find here should match.
1903     if (DynSymFromTable && DynSymFromTable->Addr != DynSymRegion->Addr)
1904       reportUniqueWarning(
1905           createError("SHT_DYNSYM section header and DT_SYMTAB disagree about "
1906                       "the location of the dynamic symbol table"));
1907 
1908     // According to the ELF gABI: "The number of symbol table entries should
1909     // equal nchain". Check to see if the DT_HASH hash table nchain value
1910     // conflicts with the number of symbols in the dynamic symbol table
1911     // according to the section header.
1912     if (HashTable && IsHashTableSupported) {
1913       if (DynSymRegion->EntSize == 0)
1914         reportUniqueWarning("SHT_DYNSYM section has sh_entsize == 0");
1915       else if (HashTable->nchain != DynSymRegion->Size / DynSymRegion->EntSize)
1916         reportUniqueWarning(
1917             "hash table nchain (" + Twine(HashTable->nchain) +
1918             ") differs from symbol count derived from SHT_DYNSYM section "
1919             "header (" +
1920             Twine(DynSymRegion->Size / DynSymRegion->EntSize) + ")");
1921     }
1922   }
1923 
1924   // Delay the creation of the actual dynamic symbol table until now, so that
1925   // checks can always be made against the section header-based properties,
1926   // without worrying about tag order.
1927   if (DynSymFromTable) {
1928     if (!DynSymRegion) {
1929       DynSymRegion = DynSymFromTable;
1930     } else {
1931       DynSymRegion->Addr = DynSymFromTable->Addr;
1932       DynSymRegion->EntSize = DynSymFromTable->EntSize;
1933       DynSymRegion->EntSizePrintName = DynSymFromTable->EntSizePrintName;
1934     }
1935   }
1936 
1937   // Derive the dynamic symbol table size from the DT_HASH hash table, if
1938   // present.
1939   if (HashTable && IsHashTableSupported && DynSymRegion) {
1940     const uint64_t FileSize = Obj.getBufSize();
1941     const uint64_t DerivedSize =
1942         (uint64_t)HashTable->nchain * DynSymRegion->EntSize;
1943     const uint64_t Offset = (const uint8_t *)DynSymRegion->Addr - Obj.base();
1944     if (DerivedSize > FileSize - Offset)
1945       reportUniqueWarning(
1946           "the size (0x" + Twine::utohexstr(DerivedSize) +
1947           ") of the dynamic symbol table at 0x" + Twine::utohexstr(Offset) +
1948           ", derived from the hash table, goes past the end of the file (0x" +
1949           Twine::utohexstr(FileSize) + ") and will be ignored");
1950     else
1951       DynSymRegion->Size = HashTable->nchain * DynSymRegion->EntSize;
1952   }
1953 }
1954 
1955 template <typename ELFT> void ELFDumper<ELFT>::printVersionInfo() {
1956   // Dump version symbol section.
1957   printVersionSymbolSection(SymbolVersionSection);
1958 
1959   // Dump version definition section.
1960   printVersionDefinitionSection(SymbolVersionDefSection);
1961 
1962   // Dump version dependency section.
1963   printVersionDependencySection(SymbolVersionNeedSection);
1964 }
1965 
1966 #define LLVM_READOBJ_DT_FLAG_ENT(prefix, enum)                                 \
1967   { #enum, prefix##_##enum }
1968 
1969 static const EnumEntry<unsigned> ElfDynamicDTFlags[] = {
1970   LLVM_READOBJ_DT_FLAG_ENT(DF, ORIGIN),
1971   LLVM_READOBJ_DT_FLAG_ENT(DF, SYMBOLIC),
1972   LLVM_READOBJ_DT_FLAG_ENT(DF, TEXTREL),
1973   LLVM_READOBJ_DT_FLAG_ENT(DF, BIND_NOW),
1974   LLVM_READOBJ_DT_FLAG_ENT(DF, STATIC_TLS)
1975 };
1976 
1977 static const EnumEntry<unsigned> ElfDynamicDTFlags1[] = {
1978   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOW),
1979   LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAL),
1980   LLVM_READOBJ_DT_FLAG_ENT(DF_1, GROUP),
1981   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODELETE),
1982   LLVM_READOBJ_DT_FLAG_ENT(DF_1, LOADFLTR),
1983   LLVM_READOBJ_DT_FLAG_ENT(DF_1, INITFIRST),
1984   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOOPEN),
1985   LLVM_READOBJ_DT_FLAG_ENT(DF_1, ORIGIN),
1986   LLVM_READOBJ_DT_FLAG_ENT(DF_1, DIRECT),
1987   LLVM_READOBJ_DT_FLAG_ENT(DF_1, TRANS),
1988   LLVM_READOBJ_DT_FLAG_ENT(DF_1, INTERPOSE),
1989   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODEFLIB),
1990   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODUMP),
1991   LLVM_READOBJ_DT_FLAG_ENT(DF_1, CONFALT),
1992   LLVM_READOBJ_DT_FLAG_ENT(DF_1, ENDFILTEE),
1993   LLVM_READOBJ_DT_FLAG_ENT(DF_1, DISPRELDNE),
1994   LLVM_READOBJ_DT_FLAG_ENT(DF_1, DISPRELPND),
1995   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODIRECT),
1996   LLVM_READOBJ_DT_FLAG_ENT(DF_1, IGNMULDEF),
1997   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOKSYMS),
1998   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOHDR),
1999   LLVM_READOBJ_DT_FLAG_ENT(DF_1, EDITED),
2000   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NORELOC),
2001   LLVM_READOBJ_DT_FLAG_ENT(DF_1, SYMINTPOSE),
2002   LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAUDIT),
2003   LLVM_READOBJ_DT_FLAG_ENT(DF_1, SINGLETON),
2004   LLVM_READOBJ_DT_FLAG_ENT(DF_1, PIE),
2005 };
2006 
2007 static const EnumEntry<unsigned> ElfDynamicDTMipsFlags[] = {
2008   LLVM_READOBJ_DT_FLAG_ENT(RHF, NONE),
2009   LLVM_READOBJ_DT_FLAG_ENT(RHF, QUICKSTART),
2010   LLVM_READOBJ_DT_FLAG_ENT(RHF, NOTPOT),
2011   LLVM_READOBJ_DT_FLAG_ENT(RHS, NO_LIBRARY_REPLACEMENT),
2012   LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_MOVE),
2013   LLVM_READOBJ_DT_FLAG_ENT(RHF, SGI_ONLY),
2014   LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_INIT),
2015   LLVM_READOBJ_DT_FLAG_ENT(RHF, DELTA_C_PLUS_PLUS),
2016   LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_START_INIT),
2017   LLVM_READOBJ_DT_FLAG_ENT(RHF, PIXIE),
2018   LLVM_READOBJ_DT_FLAG_ENT(RHF, DEFAULT_DELAY_LOAD),
2019   LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTART),
2020   LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTARTED),
2021   LLVM_READOBJ_DT_FLAG_ENT(RHF, CORD),
2022   LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_UNRES_UNDEF),
2023   LLVM_READOBJ_DT_FLAG_ENT(RHF, RLD_ORDER_SAFE)
2024 };
2025 
2026 #undef LLVM_READOBJ_DT_FLAG_ENT
2027 
2028 template <typename T, typename TFlag>
2029 void printFlags(T Value, ArrayRef<EnumEntry<TFlag>> Flags, raw_ostream &OS) {
2030   SmallVector<EnumEntry<TFlag>, 10> SetFlags;
2031   for (const EnumEntry<TFlag> &Flag : Flags)
2032     if (Flag.Value != 0 && (Value & Flag.Value) == Flag.Value)
2033       SetFlags.push_back(Flag);
2034 
2035   for (const EnumEntry<TFlag> &Flag : SetFlags)
2036     OS << Flag.Name << " ";
2037 }
2038 
2039 template <class ELFT>
2040 const typename ELFT::Shdr *
2041 ELFDumper<ELFT>::findSectionByName(StringRef Name) const {
2042   for (const Elf_Shdr &Shdr : cantFail(Obj.sections())) {
2043     if (Expected<StringRef> NameOrErr = Obj.getSectionName(Shdr)) {
2044       if (*NameOrErr == Name)
2045         return &Shdr;
2046     } else {
2047       reportUniqueWarning("unable to read the name of " + describe(Shdr) +
2048                           ": " + toString(NameOrErr.takeError()));
2049     }
2050   }
2051   return nullptr;
2052 }
2053 
2054 template <class ELFT>
2055 std::string ELFDumper<ELFT>::getDynamicEntry(uint64_t Type,
2056                                              uint64_t Value) const {
2057   auto FormatHexValue = [](uint64_t V) {
2058     std::string Str;
2059     raw_string_ostream OS(Str);
2060     const char *ConvChar =
2061         (opts::Output == opts::GNU) ? "0x%" PRIx64 : "0x%" PRIX64;
2062     OS << format(ConvChar, V);
2063     return OS.str();
2064   };
2065 
2066   auto FormatFlags = [](uint64_t V,
2067                         llvm::ArrayRef<llvm::EnumEntry<unsigned int>> Array) {
2068     std::string Str;
2069     raw_string_ostream OS(Str);
2070     printFlags(V, Array, OS);
2071     return OS.str();
2072   };
2073 
2074   // Handle custom printing of architecture specific tags
2075   switch (Obj.getHeader().e_machine) {
2076   case EM_AARCH64:
2077     switch (Type) {
2078     case DT_AARCH64_BTI_PLT:
2079     case DT_AARCH64_PAC_PLT:
2080     case DT_AARCH64_VARIANT_PCS:
2081       return std::to_string(Value);
2082     default:
2083       break;
2084     }
2085     break;
2086   case EM_HEXAGON:
2087     switch (Type) {
2088     case DT_HEXAGON_VER:
2089       return std::to_string(Value);
2090     case DT_HEXAGON_SYMSZ:
2091     case DT_HEXAGON_PLT:
2092       return FormatHexValue(Value);
2093     default:
2094       break;
2095     }
2096     break;
2097   case EM_MIPS:
2098     switch (Type) {
2099     case DT_MIPS_RLD_VERSION:
2100     case DT_MIPS_LOCAL_GOTNO:
2101     case DT_MIPS_SYMTABNO:
2102     case DT_MIPS_UNREFEXTNO:
2103       return std::to_string(Value);
2104     case DT_MIPS_TIME_STAMP:
2105     case DT_MIPS_ICHECKSUM:
2106     case DT_MIPS_IVERSION:
2107     case DT_MIPS_BASE_ADDRESS:
2108     case DT_MIPS_MSYM:
2109     case DT_MIPS_CONFLICT:
2110     case DT_MIPS_LIBLIST:
2111     case DT_MIPS_CONFLICTNO:
2112     case DT_MIPS_LIBLISTNO:
2113     case DT_MIPS_GOTSYM:
2114     case DT_MIPS_HIPAGENO:
2115     case DT_MIPS_RLD_MAP:
2116     case DT_MIPS_DELTA_CLASS:
2117     case DT_MIPS_DELTA_CLASS_NO:
2118     case DT_MIPS_DELTA_INSTANCE:
2119     case DT_MIPS_DELTA_RELOC:
2120     case DT_MIPS_DELTA_RELOC_NO:
2121     case DT_MIPS_DELTA_SYM:
2122     case DT_MIPS_DELTA_SYM_NO:
2123     case DT_MIPS_DELTA_CLASSSYM:
2124     case DT_MIPS_DELTA_CLASSSYM_NO:
2125     case DT_MIPS_CXX_FLAGS:
2126     case DT_MIPS_PIXIE_INIT:
2127     case DT_MIPS_SYMBOL_LIB:
2128     case DT_MIPS_LOCALPAGE_GOTIDX:
2129     case DT_MIPS_LOCAL_GOTIDX:
2130     case DT_MIPS_HIDDEN_GOTIDX:
2131     case DT_MIPS_PROTECTED_GOTIDX:
2132     case DT_MIPS_OPTIONS:
2133     case DT_MIPS_INTERFACE:
2134     case DT_MIPS_DYNSTR_ALIGN:
2135     case DT_MIPS_INTERFACE_SIZE:
2136     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
2137     case DT_MIPS_PERF_SUFFIX:
2138     case DT_MIPS_COMPACT_SIZE:
2139     case DT_MIPS_GP_VALUE:
2140     case DT_MIPS_AUX_DYNAMIC:
2141     case DT_MIPS_PLTGOT:
2142     case DT_MIPS_RWPLT:
2143     case DT_MIPS_RLD_MAP_REL:
2144       return FormatHexValue(Value);
2145     case DT_MIPS_FLAGS:
2146       return FormatFlags(Value, makeArrayRef(ElfDynamicDTMipsFlags));
2147     default:
2148       break;
2149     }
2150     break;
2151   default:
2152     break;
2153   }
2154 
2155   switch (Type) {
2156   case DT_PLTREL:
2157     if (Value == DT_REL)
2158       return "REL";
2159     if (Value == DT_RELA)
2160       return "RELA";
2161     LLVM_FALLTHROUGH;
2162   case DT_PLTGOT:
2163   case DT_HASH:
2164   case DT_STRTAB:
2165   case DT_SYMTAB:
2166   case DT_RELA:
2167   case DT_INIT:
2168   case DT_FINI:
2169   case DT_REL:
2170   case DT_JMPREL:
2171   case DT_INIT_ARRAY:
2172   case DT_FINI_ARRAY:
2173   case DT_PREINIT_ARRAY:
2174   case DT_DEBUG:
2175   case DT_VERDEF:
2176   case DT_VERNEED:
2177   case DT_VERSYM:
2178   case DT_GNU_HASH:
2179   case DT_NULL:
2180     return FormatHexValue(Value);
2181   case DT_RELACOUNT:
2182   case DT_RELCOUNT:
2183   case DT_VERDEFNUM:
2184   case DT_VERNEEDNUM:
2185     return std::to_string(Value);
2186   case DT_PLTRELSZ:
2187   case DT_RELASZ:
2188   case DT_RELAENT:
2189   case DT_STRSZ:
2190   case DT_SYMENT:
2191   case DT_RELSZ:
2192   case DT_RELENT:
2193   case DT_INIT_ARRAYSZ:
2194   case DT_FINI_ARRAYSZ:
2195   case DT_PREINIT_ARRAYSZ:
2196   case DT_ANDROID_RELSZ:
2197   case DT_ANDROID_RELASZ:
2198     return std::to_string(Value) + " (bytes)";
2199   case DT_NEEDED:
2200   case DT_SONAME:
2201   case DT_AUXILIARY:
2202   case DT_USED:
2203   case DT_FILTER:
2204   case DT_RPATH:
2205   case DT_RUNPATH: {
2206     const std::map<uint64_t, const char *> TagNames = {
2207         {DT_NEEDED, "Shared library"},       {DT_SONAME, "Library soname"},
2208         {DT_AUXILIARY, "Auxiliary library"}, {DT_USED, "Not needed object"},
2209         {DT_FILTER, "Filter library"},       {DT_RPATH, "Library rpath"},
2210         {DT_RUNPATH, "Library runpath"},
2211     };
2212 
2213     return (Twine(TagNames.at(Type)) + ": [" + getDynamicString(Value) + "]")
2214         .str();
2215   }
2216   case DT_FLAGS:
2217     return FormatFlags(Value, makeArrayRef(ElfDynamicDTFlags));
2218   case DT_FLAGS_1:
2219     return FormatFlags(Value, makeArrayRef(ElfDynamicDTFlags1));
2220   default:
2221     return FormatHexValue(Value);
2222   }
2223 }
2224 
2225 template <class ELFT>
2226 StringRef ELFDumper<ELFT>::getDynamicString(uint64_t Value) const {
2227   if (DynamicStringTable.empty() && !DynamicStringTable.data()) {
2228     reportUniqueWarning("string table was not found");
2229     return "<?>";
2230   }
2231 
2232   auto WarnAndReturn = [this](const Twine &Msg, uint64_t Offset) {
2233     reportUniqueWarning("string table at offset 0x" + Twine::utohexstr(Offset) +
2234                         Msg);
2235     return "<?>";
2236   };
2237 
2238   const uint64_t FileSize = Obj.getBufSize();
2239   const uint64_t Offset =
2240       (const uint8_t *)DynamicStringTable.data() - Obj.base();
2241   if (DynamicStringTable.size() > FileSize - Offset)
2242     return WarnAndReturn(" with size 0x" +
2243                              Twine::utohexstr(DynamicStringTable.size()) +
2244                              " goes past the end of the file (0x" +
2245                              Twine::utohexstr(FileSize) + ")",
2246                          Offset);
2247 
2248   if (Value >= DynamicStringTable.size())
2249     return WarnAndReturn(
2250         ": unable to read the string at 0x" + Twine::utohexstr(Offset + Value) +
2251             ": it goes past the end of the table (0x" +
2252             Twine::utohexstr(Offset + DynamicStringTable.size()) + ")",
2253         Offset);
2254 
2255   if (DynamicStringTable.back() != '\0')
2256     return WarnAndReturn(": unable to read the string at 0x" +
2257                              Twine::utohexstr(Offset + Value) +
2258                              ": the string table is not null-terminated",
2259                          Offset);
2260 
2261   return DynamicStringTable.data() + Value;
2262 }
2263 
2264 template <class ELFT> void ELFDumper<ELFT>::printUnwindInfo() {
2265   DwarfCFIEH::PrinterContext<ELFT> Ctx(W, ObjF);
2266   Ctx.printUnwindInformation();
2267 }
2268 
2269 // The namespace is needed to fix the compilation with GCC older than 7.0+.
2270 namespace {
2271 template <> void ELFDumper<ELF32LE>::printUnwindInfo() {
2272   if (Obj.getHeader().e_machine == EM_ARM) {
2273     ARM::EHABI::PrinterContext<ELF32LE> Ctx(W, Obj, ObjF.getFileName(),
2274                                             DotSymtabSec);
2275     Ctx.PrintUnwindInformation();
2276   }
2277   DwarfCFIEH::PrinterContext<ELF32LE> Ctx(W, ObjF);
2278   Ctx.printUnwindInformation();
2279 }
2280 } // namespace
2281 
2282 template <class ELFT> void ELFDumper<ELFT>::printNeededLibraries() {
2283   ListScope D(W, "NeededLibraries");
2284 
2285   std::vector<StringRef> Libs;
2286   for (const auto &Entry : dynamic_table())
2287     if (Entry.d_tag == ELF::DT_NEEDED)
2288       Libs.push_back(getDynamicString(Entry.d_un.d_val));
2289 
2290   llvm::sort(Libs);
2291 
2292   for (StringRef L : Libs)
2293     W.startLine() << L << "\n";
2294 }
2295 
2296 template <class ELFT>
2297 static Error checkHashTable(const ELFDumper<ELFT> &Dumper,
2298                             const typename ELFT::Hash *H,
2299                             bool *IsHeaderValid = nullptr) {
2300   const ELFFile<ELFT> &Obj = Dumper.getElfObject().getELFFile();
2301   const uint64_t SecOffset = (const uint8_t *)H - Obj.base();
2302   if (Dumper.getHashTableEntSize() == 8) {
2303     auto It = llvm::find_if(ElfMachineType, [&](const EnumEntry<unsigned> &E) {
2304       return E.Value == Obj.getHeader().e_machine;
2305     });
2306     if (IsHeaderValid)
2307       *IsHeaderValid = false;
2308     return createError("the hash table at 0x" + Twine::utohexstr(SecOffset) +
2309                        " is not supported: it contains non-standard 8 "
2310                        "byte entries on " +
2311                        It->AltName + " platform");
2312   }
2313 
2314   auto MakeError = [&](const Twine &Msg = "") {
2315     return createError("the hash table at offset 0x" +
2316                        Twine::utohexstr(SecOffset) +
2317                        " goes past the end of the file (0x" +
2318                        Twine::utohexstr(Obj.getBufSize()) + ")" + Msg);
2319   };
2320 
2321   // Each SHT_HASH section starts from two 32-bit fields: nbucket and nchain.
2322   const unsigned HeaderSize = 2 * sizeof(typename ELFT::Word);
2323 
2324   if (IsHeaderValid)
2325     *IsHeaderValid = Obj.getBufSize() - SecOffset >= HeaderSize;
2326 
2327   if (Obj.getBufSize() - SecOffset < HeaderSize)
2328     return MakeError();
2329 
2330   if (Obj.getBufSize() - SecOffset - HeaderSize <
2331       ((uint64_t)H->nbucket + H->nchain) * sizeof(typename ELFT::Word))
2332     return MakeError(", nbucket = " + Twine(H->nbucket) +
2333                      ", nchain = " + Twine(H->nchain));
2334   return Error::success();
2335 }
2336 
2337 template <class ELFT>
2338 static Error checkGNUHashTable(const ELFFile<ELFT> &Obj,
2339                                const typename ELFT::GnuHash *GnuHashTable,
2340                                bool *IsHeaderValid = nullptr) {
2341   const uint8_t *TableData = reinterpret_cast<const uint8_t *>(GnuHashTable);
2342   assert(TableData >= Obj.base() && TableData < Obj.base() + Obj.getBufSize() &&
2343          "GnuHashTable must always point to a location inside the file");
2344 
2345   uint64_t TableOffset = TableData - Obj.base();
2346   if (IsHeaderValid)
2347     *IsHeaderValid = TableOffset + /*Header size:*/ 16 < Obj.getBufSize();
2348   if (TableOffset + 16 + (uint64_t)GnuHashTable->nbuckets * 4 +
2349           (uint64_t)GnuHashTable->maskwords * sizeof(typename ELFT::Off) >=
2350       Obj.getBufSize())
2351     return createError("unable to dump the SHT_GNU_HASH "
2352                        "section at 0x" +
2353                        Twine::utohexstr(TableOffset) +
2354                        ": it goes past the end of the file");
2355   return Error::success();
2356 }
2357 
2358 template <typename ELFT> void ELFDumper<ELFT>::printHashTable() {
2359   DictScope D(W, "HashTable");
2360   if (!HashTable)
2361     return;
2362 
2363   bool IsHeaderValid;
2364   Error Err = checkHashTable(*this, HashTable, &IsHeaderValid);
2365   if (IsHeaderValid) {
2366     W.printNumber("Num Buckets", HashTable->nbucket);
2367     W.printNumber("Num Chains", HashTable->nchain);
2368   }
2369 
2370   if (Err) {
2371     reportUniqueWarning(std::move(Err));
2372     return;
2373   }
2374 
2375   W.printList("Buckets", HashTable->buckets());
2376   W.printList("Chains", HashTable->chains());
2377 }
2378 
2379 template <class ELFT>
2380 static Expected<ArrayRef<typename ELFT::Word>>
2381 getGnuHashTableChains(Optional<DynRegionInfo> DynSymRegion,
2382                       const typename ELFT::GnuHash *GnuHashTable) {
2383   if (!DynSymRegion)
2384     return createError("no dynamic symbol table found");
2385 
2386   ArrayRef<typename ELFT::Sym> DynSymTable =
2387       DynSymRegion->template getAsArrayRef<typename ELFT::Sym>();
2388   size_t NumSyms = DynSymTable.size();
2389   if (!NumSyms)
2390     return createError("the dynamic symbol table is empty");
2391 
2392   if (GnuHashTable->symndx < NumSyms)
2393     return GnuHashTable->values(NumSyms);
2394 
2395   // A normal empty GNU hash table section produced by linker might have
2396   // symndx set to the number of dynamic symbols + 1 (for the zero symbol)
2397   // and have dummy null values in the Bloom filter and in the buckets
2398   // vector (or no values at all). It happens because the value of symndx is not
2399   // important for dynamic loaders when the GNU hash table is empty. They just
2400   // skip the whole object during symbol lookup. In such cases, the symndx value
2401   // is irrelevant and we should not report a warning.
2402   ArrayRef<typename ELFT::Word> Buckets = GnuHashTable->buckets();
2403   if (!llvm::all_of(Buckets, [](typename ELFT::Word V) { return V == 0; }))
2404     return createError(
2405         "the first hashed symbol index (" + Twine(GnuHashTable->symndx) +
2406         ") is greater than or equal to the number of dynamic symbols (" +
2407         Twine(NumSyms) + ")");
2408   // There is no way to represent an array of (dynamic symbols count - symndx)
2409   // length.
2410   return ArrayRef<typename ELFT::Word>();
2411 }
2412 
2413 template <typename ELFT>
2414 void ELFDumper<ELFT>::printGnuHashTable() {
2415   DictScope D(W, "GnuHashTable");
2416   if (!GnuHashTable)
2417     return;
2418 
2419   bool IsHeaderValid;
2420   Error Err = checkGNUHashTable<ELFT>(Obj, GnuHashTable, &IsHeaderValid);
2421   if (IsHeaderValid) {
2422     W.printNumber("Num Buckets", GnuHashTable->nbuckets);
2423     W.printNumber("First Hashed Symbol Index", GnuHashTable->symndx);
2424     W.printNumber("Num Mask Words", GnuHashTable->maskwords);
2425     W.printNumber("Shift Count", GnuHashTable->shift2);
2426   }
2427 
2428   if (Err) {
2429     reportUniqueWarning(std::move(Err));
2430     return;
2431   }
2432 
2433   ArrayRef<typename ELFT::Off> BloomFilter = GnuHashTable->filter();
2434   W.printHexList("Bloom Filter", BloomFilter);
2435 
2436   ArrayRef<Elf_Word> Buckets = GnuHashTable->buckets();
2437   W.printList("Buckets", Buckets);
2438 
2439   Expected<ArrayRef<Elf_Word>> Chains =
2440       getGnuHashTableChains<ELFT>(DynSymRegion, GnuHashTable);
2441   if (!Chains) {
2442     reportUniqueWarning("unable to dump 'Values' for the SHT_GNU_HASH "
2443                         "section: " +
2444                         toString(Chains.takeError()));
2445     return;
2446   }
2447 
2448   W.printHexList("Values", *Chains);
2449 }
2450 
2451 template <typename ELFT> void ELFDumper<ELFT>::printLoadName() {
2452   StringRef SOName = "<Not found>";
2453   if (SONameOffset)
2454     SOName = getDynamicString(*SONameOffset);
2455   W.printString("LoadName", SOName);
2456 }
2457 
2458 template <class ELFT> void ELFDumper<ELFT>::printArchSpecificInfo() {
2459   switch (Obj.getHeader().e_machine) {
2460   case EM_ARM:
2461   case EM_RISCV:
2462     printAttributes();
2463     break;
2464   case EM_MIPS: {
2465     printMipsABIFlags();
2466     printMipsOptions();
2467     printMipsReginfo();
2468     MipsGOTParser<ELFT> Parser(*this);
2469     if (Error E = Parser.findGOT(dynamic_table(), dynamic_symbols()))
2470       reportUniqueWarning(std::move(E));
2471     else if (!Parser.isGotEmpty())
2472       printMipsGOT(Parser);
2473 
2474     if (Error E = Parser.findPLT(dynamic_table()))
2475       reportUniqueWarning(std::move(E));
2476     else if (!Parser.isPltEmpty())
2477       printMipsPLT(Parser);
2478     break;
2479   }
2480   default:
2481     break;
2482   }
2483 }
2484 
2485 template <class ELFT> void ELFDumper<ELFT>::printAttributes() {
2486   if (!Obj.isLE()) {
2487     W.startLine() << "Attributes not implemented.\n";
2488     return;
2489   }
2490 
2491   const unsigned Machine = Obj.getHeader().e_machine;
2492   assert((Machine == EM_ARM || Machine == EM_RISCV) &&
2493          "Attributes not implemented.");
2494 
2495   DictScope BA(W, "BuildAttributes");
2496   for (const Elf_Shdr &Sec : cantFail(Obj.sections())) {
2497     if (Sec.sh_type != ELF::SHT_ARM_ATTRIBUTES &&
2498         Sec.sh_type != ELF::SHT_RISCV_ATTRIBUTES)
2499       continue;
2500 
2501     ArrayRef<uint8_t> Contents;
2502     if (Expected<ArrayRef<uint8_t>> ContentOrErr =
2503             Obj.getSectionContents(Sec)) {
2504       Contents = *ContentOrErr;
2505       if (Contents.empty()) {
2506         reportUniqueWarning("the " + describe(Sec) + " is empty");
2507         continue;
2508       }
2509     } else {
2510       reportUniqueWarning("unable to read the content of the " + describe(Sec) +
2511                           ": " + toString(ContentOrErr.takeError()));
2512       continue;
2513     }
2514 
2515     W.printHex("FormatVersion", Contents[0]);
2516 
2517     auto ParseAttrubutes = [&]() {
2518       if (Machine == EM_ARM)
2519         return ARMAttributeParser(&W).parse(Contents, support::little);
2520       return RISCVAttributeParser(&W).parse(Contents, support::little);
2521     };
2522 
2523     if (Error E = ParseAttrubutes())
2524       reportUniqueWarning("unable to dump attributes from the " +
2525                           describe(Sec) + ": " + toString(std::move(E)));
2526   }
2527 }
2528 
2529 namespace {
2530 
2531 template <class ELFT> class MipsGOTParser {
2532 public:
2533   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
2534   using Entry = typename ELFT::Addr;
2535   using Entries = ArrayRef<Entry>;
2536 
2537   const bool IsStatic;
2538   const ELFFile<ELFT> &Obj;
2539   const ELFDumper<ELFT> &Dumper;
2540 
2541   MipsGOTParser(const ELFDumper<ELFT> &D);
2542   Error findGOT(Elf_Dyn_Range DynTable, Elf_Sym_Range DynSyms);
2543   Error findPLT(Elf_Dyn_Range DynTable);
2544 
2545   bool isGotEmpty() const { return GotEntries.empty(); }
2546   bool isPltEmpty() const { return PltEntries.empty(); }
2547 
2548   uint64_t getGp() const;
2549 
2550   const Entry *getGotLazyResolver() const;
2551   const Entry *getGotModulePointer() const;
2552   const Entry *getPltLazyResolver() const;
2553   const Entry *getPltModulePointer() const;
2554 
2555   Entries getLocalEntries() const;
2556   Entries getGlobalEntries() const;
2557   Entries getOtherEntries() const;
2558   Entries getPltEntries() const;
2559 
2560   uint64_t getGotAddress(const Entry * E) const;
2561   int64_t getGotOffset(const Entry * E) const;
2562   const Elf_Sym *getGotSym(const Entry *E) const;
2563 
2564   uint64_t getPltAddress(const Entry * E) const;
2565   const Elf_Sym *getPltSym(const Entry *E) const;
2566 
2567   StringRef getPltStrTable() const { return PltStrTable; }
2568   const Elf_Shdr *getPltSymTable() const { return PltSymTable; }
2569 
2570 private:
2571   const Elf_Shdr *GotSec;
2572   size_t LocalNum;
2573   size_t GlobalNum;
2574 
2575   const Elf_Shdr *PltSec;
2576   const Elf_Shdr *PltRelSec;
2577   const Elf_Shdr *PltSymTable;
2578   StringRef FileName;
2579 
2580   Elf_Sym_Range GotDynSyms;
2581   StringRef PltStrTable;
2582 
2583   Entries GotEntries;
2584   Entries PltEntries;
2585 };
2586 
2587 } // end anonymous namespace
2588 
2589 template <class ELFT>
2590 MipsGOTParser<ELFT>::MipsGOTParser(const ELFDumper<ELFT> &D)
2591     : IsStatic(D.dynamic_table().empty()), Obj(D.getElfObject().getELFFile()),
2592       Dumper(D), GotSec(nullptr), LocalNum(0), GlobalNum(0), PltSec(nullptr),
2593       PltRelSec(nullptr), PltSymTable(nullptr),
2594       FileName(D.getElfObject().getFileName()) {}
2595 
2596 template <class ELFT>
2597 Error MipsGOTParser<ELFT>::findGOT(Elf_Dyn_Range DynTable,
2598                                    Elf_Sym_Range DynSyms) {
2599   // See "Global Offset Table" in Chapter 5 in the following document
2600   // for detailed GOT description.
2601   // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
2602 
2603   // Find static GOT secton.
2604   if (IsStatic) {
2605     GotSec = Dumper.findSectionByName(".got");
2606     if (!GotSec)
2607       return Error::success();
2608 
2609     ArrayRef<uint8_t> Content =
2610         unwrapOrError(FileName, Obj.getSectionContents(*GotSec));
2611     GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()),
2612                          Content.size() / sizeof(Entry));
2613     LocalNum = GotEntries.size();
2614     return Error::success();
2615   }
2616 
2617   // Lookup dynamic table tags which define the GOT layout.
2618   Optional<uint64_t> DtPltGot;
2619   Optional<uint64_t> DtLocalGotNum;
2620   Optional<uint64_t> DtGotSym;
2621   for (const auto &Entry : DynTable) {
2622     switch (Entry.getTag()) {
2623     case ELF::DT_PLTGOT:
2624       DtPltGot = Entry.getVal();
2625       break;
2626     case ELF::DT_MIPS_LOCAL_GOTNO:
2627       DtLocalGotNum = Entry.getVal();
2628       break;
2629     case ELF::DT_MIPS_GOTSYM:
2630       DtGotSym = Entry.getVal();
2631       break;
2632     }
2633   }
2634 
2635   if (!DtPltGot && !DtLocalGotNum && !DtGotSym)
2636     return Error::success();
2637 
2638   if (!DtPltGot)
2639     return createError("cannot find PLTGOT dynamic tag");
2640   if (!DtLocalGotNum)
2641     return createError("cannot find MIPS_LOCAL_GOTNO dynamic tag");
2642   if (!DtGotSym)
2643     return createError("cannot find MIPS_GOTSYM dynamic tag");
2644 
2645   size_t DynSymTotal = DynSyms.size();
2646   if (*DtGotSym > DynSymTotal)
2647     return createError("DT_MIPS_GOTSYM value (" + Twine(*DtGotSym) +
2648                        ") exceeds the number of dynamic symbols (" +
2649                        Twine(DynSymTotal) + ")");
2650 
2651   GotSec = findNotEmptySectionByAddress(Obj, FileName, *DtPltGot);
2652   if (!GotSec)
2653     return createError("there is no non-empty GOT section at 0x" +
2654                        Twine::utohexstr(*DtPltGot));
2655 
2656   LocalNum = *DtLocalGotNum;
2657   GlobalNum = DynSymTotal - *DtGotSym;
2658 
2659   ArrayRef<uint8_t> Content =
2660       unwrapOrError(FileName, Obj.getSectionContents(*GotSec));
2661   GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()),
2662                        Content.size() / sizeof(Entry));
2663   GotDynSyms = DynSyms.drop_front(*DtGotSym);
2664 
2665   return Error::success();
2666 }
2667 
2668 template <class ELFT>
2669 Error MipsGOTParser<ELFT>::findPLT(Elf_Dyn_Range DynTable) {
2670   // Lookup dynamic table tags which define the PLT layout.
2671   Optional<uint64_t> DtMipsPltGot;
2672   Optional<uint64_t> DtJmpRel;
2673   for (const auto &Entry : DynTable) {
2674     switch (Entry.getTag()) {
2675     case ELF::DT_MIPS_PLTGOT:
2676       DtMipsPltGot = Entry.getVal();
2677       break;
2678     case ELF::DT_JMPREL:
2679       DtJmpRel = Entry.getVal();
2680       break;
2681     }
2682   }
2683 
2684   if (!DtMipsPltGot && !DtJmpRel)
2685     return Error::success();
2686 
2687   // Find PLT section.
2688   if (!DtMipsPltGot)
2689     return createError("cannot find MIPS_PLTGOT dynamic tag");
2690   if (!DtJmpRel)
2691     return createError("cannot find JMPREL dynamic tag");
2692 
2693   PltSec = findNotEmptySectionByAddress(Obj, FileName, *DtMipsPltGot);
2694   if (!PltSec)
2695     return createError("there is no non-empty PLTGOT section at 0x" +
2696                        Twine::utohexstr(*DtMipsPltGot));
2697 
2698   PltRelSec = findNotEmptySectionByAddress(Obj, FileName, *DtJmpRel);
2699   if (!PltRelSec)
2700     return createError("there is no non-empty RELPLT section at 0x" +
2701                        Twine::utohexstr(*DtJmpRel));
2702 
2703   if (Expected<ArrayRef<uint8_t>> PltContentOrErr =
2704           Obj.getSectionContents(*PltSec))
2705     PltEntries =
2706         Entries(reinterpret_cast<const Entry *>(PltContentOrErr->data()),
2707                 PltContentOrErr->size() / sizeof(Entry));
2708   else
2709     return createError("unable to read PLTGOT section content: " +
2710                        toString(PltContentOrErr.takeError()));
2711 
2712   if (Expected<const Elf_Shdr *> PltSymTableOrErr =
2713           Obj.getSection(PltRelSec->sh_link))
2714     PltSymTable = *PltSymTableOrErr;
2715   else
2716     return createError("unable to get a symbol table linked to the " +
2717                        describe(Obj, *PltRelSec) + ": " +
2718                        toString(PltSymTableOrErr.takeError()));
2719 
2720   if (Expected<StringRef> StrTabOrErr =
2721           Obj.getStringTableForSymtab(*PltSymTable))
2722     PltStrTable = *StrTabOrErr;
2723   else
2724     return createError("unable to get a string table for the " +
2725                        describe(Obj, *PltSymTable) + ": " +
2726                        toString(StrTabOrErr.takeError()));
2727 
2728   return Error::success();
2729 }
2730 
2731 template <class ELFT> uint64_t MipsGOTParser<ELFT>::getGp() const {
2732   return GotSec->sh_addr + 0x7ff0;
2733 }
2734 
2735 template <class ELFT>
2736 const typename MipsGOTParser<ELFT>::Entry *
2737 MipsGOTParser<ELFT>::getGotLazyResolver() const {
2738   return LocalNum > 0 ? &GotEntries[0] : nullptr;
2739 }
2740 
2741 template <class ELFT>
2742 const typename MipsGOTParser<ELFT>::Entry *
2743 MipsGOTParser<ELFT>::getGotModulePointer() const {
2744   if (LocalNum < 2)
2745     return nullptr;
2746   const Entry &E = GotEntries[1];
2747   if ((E >> (sizeof(Entry) * 8 - 1)) == 0)
2748     return nullptr;
2749   return &E;
2750 }
2751 
2752 template <class ELFT>
2753 typename MipsGOTParser<ELFT>::Entries
2754 MipsGOTParser<ELFT>::getLocalEntries() const {
2755   size_t Skip = getGotModulePointer() ? 2 : 1;
2756   if (LocalNum - Skip <= 0)
2757     return Entries();
2758   return GotEntries.slice(Skip, LocalNum - Skip);
2759 }
2760 
2761 template <class ELFT>
2762 typename MipsGOTParser<ELFT>::Entries
2763 MipsGOTParser<ELFT>::getGlobalEntries() const {
2764   if (GlobalNum == 0)
2765     return Entries();
2766   return GotEntries.slice(LocalNum, GlobalNum);
2767 }
2768 
2769 template <class ELFT>
2770 typename MipsGOTParser<ELFT>::Entries
2771 MipsGOTParser<ELFT>::getOtherEntries() const {
2772   size_t OtherNum = GotEntries.size() - LocalNum - GlobalNum;
2773   if (OtherNum == 0)
2774     return Entries();
2775   return GotEntries.slice(LocalNum + GlobalNum, OtherNum);
2776 }
2777 
2778 template <class ELFT>
2779 uint64_t MipsGOTParser<ELFT>::getGotAddress(const Entry *E) const {
2780   int64_t Offset = std::distance(GotEntries.data(), E) * sizeof(Entry);
2781   return GotSec->sh_addr + Offset;
2782 }
2783 
2784 template <class ELFT>
2785 int64_t MipsGOTParser<ELFT>::getGotOffset(const Entry *E) const {
2786   int64_t Offset = std::distance(GotEntries.data(), E) * sizeof(Entry);
2787   return Offset - 0x7ff0;
2788 }
2789 
2790 template <class ELFT>
2791 const typename MipsGOTParser<ELFT>::Elf_Sym *
2792 MipsGOTParser<ELFT>::getGotSym(const Entry *E) const {
2793   int64_t Offset = std::distance(GotEntries.data(), E);
2794   return &GotDynSyms[Offset - LocalNum];
2795 }
2796 
2797 template <class ELFT>
2798 const typename MipsGOTParser<ELFT>::Entry *
2799 MipsGOTParser<ELFT>::getPltLazyResolver() const {
2800   return PltEntries.empty() ? nullptr : &PltEntries[0];
2801 }
2802 
2803 template <class ELFT>
2804 const typename MipsGOTParser<ELFT>::Entry *
2805 MipsGOTParser<ELFT>::getPltModulePointer() const {
2806   return PltEntries.size() < 2 ? nullptr : &PltEntries[1];
2807 }
2808 
2809 template <class ELFT>
2810 typename MipsGOTParser<ELFT>::Entries
2811 MipsGOTParser<ELFT>::getPltEntries() const {
2812   if (PltEntries.size() <= 2)
2813     return Entries();
2814   return PltEntries.slice(2, PltEntries.size() - 2);
2815 }
2816 
2817 template <class ELFT>
2818 uint64_t MipsGOTParser<ELFT>::getPltAddress(const Entry *E) const {
2819   int64_t Offset = std::distance(PltEntries.data(), E) * sizeof(Entry);
2820   return PltSec->sh_addr + Offset;
2821 }
2822 
2823 template <class ELFT>
2824 const typename MipsGOTParser<ELFT>::Elf_Sym *
2825 MipsGOTParser<ELFT>::getPltSym(const Entry *E) const {
2826   int64_t Offset = std::distance(getPltEntries().data(), E);
2827   if (PltRelSec->sh_type == ELF::SHT_REL) {
2828     Elf_Rel_Range Rels = unwrapOrError(FileName, Obj.rels(*PltRelSec));
2829     return unwrapOrError(FileName,
2830                          Obj.getRelocationSymbol(Rels[Offset], PltSymTable));
2831   } else {
2832     Elf_Rela_Range Rels = unwrapOrError(FileName, Obj.relas(*PltRelSec));
2833     return unwrapOrError(FileName,
2834                          Obj.getRelocationSymbol(Rels[Offset], PltSymTable));
2835   }
2836 }
2837 
2838 static const EnumEntry<unsigned> ElfMipsISAExtType[] = {
2839   {"None",                    Mips::AFL_EXT_NONE},
2840   {"Broadcom SB-1",           Mips::AFL_EXT_SB1},
2841   {"Cavium Networks Octeon",  Mips::AFL_EXT_OCTEON},
2842   {"Cavium Networks Octeon2", Mips::AFL_EXT_OCTEON2},
2843   {"Cavium Networks OcteonP", Mips::AFL_EXT_OCTEONP},
2844   {"Cavium Networks Octeon3", Mips::AFL_EXT_OCTEON3},
2845   {"LSI R4010",               Mips::AFL_EXT_4010},
2846   {"Loongson 2E",             Mips::AFL_EXT_LOONGSON_2E},
2847   {"Loongson 2F",             Mips::AFL_EXT_LOONGSON_2F},
2848   {"Loongson 3A",             Mips::AFL_EXT_LOONGSON_3A},
2849   {"MIPS R4650",              Mips::AFL_EXT_4650},
2850   {"MIPS R5900",              Mips::AFL_EXT_5900},
2851   {"MIPS R10000",             Mips::AFL_EXT_10000},
2852   {"NEC VR4100",              Mips::AFL_EXT_4100},
2853   {"NEC VR4111/VR4181",       Mips::AFL_EXT_4111},
2854   {"NEC VR4120",              Mips::AFL_EXT_4120},
2855   {"NEC VR5400",              Mips::AFL_EXT_5400},
2856   {"NEC VR5500",              Mips::AFL_EXT_5500},
2857   {"RMI Xlr",                 Mips::AFL_EXT_XLR},
2858   {"Toshiba R3900",           Mips::AFL_EXT_3900}
2859 };
2860 
2861 static const EnumEntry<unsigned> ElfMipsASEFlags[] = {
2862   {"DSP",                Mips::AFL_ASE_DSP},
2863   {"DSPR2",              Mips::AFL_ASE_DSPR2},
2864   {"Enhanced VA Scheme", Mips::AFL_ASE_EVA},
2865   {"MCU",                Mips::AFL_ASE_MCU},
2866   {"MDMX",               Mips::AFL_ASE_MDMX},
2867   {"MIPS-3D",            Mips::AFL_ASE_MIPS3D},
2868   {"MT",                 Mips::AFL_ASE_MT},
2869   {"SmartMIPS",          Mips::AFL_ASE_SMARTMIPS},
2870   {"VZ",                 Mips::AFL_ASE_VIRT},
2871   {"MSA",                Mips::AFL_ASE_MSA},
2872   {"MIPS16",             Mips::AFL_ASE_MIPS16},
2873   {"microMIPS",          Mips::AFL_ASE_MICROMIPS},
2874   {"XPA",                Mips::AFL_ASE_XPA},
2875   {"CRC",                Mips::AFL_ASE_CRC},
2876   {"GINV",               Mips::AFL_ASE_GINV},
2877 };
2878 
2879 static const EnumEntry<unsigned> ElfMipsFpABIType[] = {
2880   {"Hard or soft float",                  Mips::Val_GNU_MIPS_ABI_FP_ANY},
2881   {"Hard float (double precision)",       Mips::Val_GNU_MIPS_ABI_FP_DOUBLE},
2882   {"Hard float (single precision)",       Mips::Val_GNU_MIPS_ABI_FP_SINGLE},
2883   {"Soft float",                          Mips::Val_GNU_MIPS_ABI_FP_SOFT},
2884   {"Hard float (MIPS32r2 64-bit FPU 12 callee-saved)",
2885    Mips::Val_GNU_MIPS_ABI_FP_OLD_64},
2886   {"Hard float (32-bit CPU, Any FPU)",    Mips::Val_GNU_MIPS_ABI_FP_XX},
2887   {"Hard float (32-bit CPU, 64-bit FPU)", Mips::Val_GNU_MIPS_ABI_FP_64},
2888   {"Hard float compat (32-bit CPU, 64-bit FPU)",
2889    Mips::Val_GNU_MIPS_ABI_FP_64A}
2890 };
2891 
2892 static const EnumEntry<unsigned> ElfMipsFlags1[] {
2893   {"ODDSPREG", Mips::AFL_FLAGS1_ODDSPREG},
2894 };
2895 
2896 static int getMipsRegisterSize(uint8_t Flag) {
2897   switch (Flag) {
2898   case Mips::AFL_REG_NONE:
2899     return 0;
2900   case Mips::AFL_REG_32:
2901     return 32;
2902   case Mips::AFL_REG_64:
2903     return 64;
2904   case Mips::AFL_REG_128:
2905     return 128;
2906   default:
2907     return -1;
2908   }
2909 }
2910 
2911 template <class ELFT>
2912 static void printMipsReginfoData(ScopedPrinter &W,
2913                                  const Elf_Mips_RegInfo<ELFT> &Reginfo) {
2914   W.printHex("GP", Reginfo.ri_gp_value);
2915   W.printHex("General Mask", Reginfo.ri_gprmask);
2916   W.printHex("Co-Proc Mask0", Reginfo.ri_cprmask[0]);
2917   W.printHex("Co-Proc Mask1", Reginfo.ri_cprmask[1]);
2918   W.printHex("Co-Proc Mask2", Reginfo.ri_cprmask[2]);
2919   W.printHex("Co-Proc Mask3", Reginfo.ri_cprmask[3]);
2920 }
2921 
2922 template <class ELFT> void ELFDumper<ELFT>::printMipsReginfo() {
2923   const Elf_Shdr *RegInfoSec = findSectionByName(".reginfo");
2924   if (!RegInfoSec) {
2925     W.startLine() << "There is no .reginfo section in the file.\n";
2926     return;
2927   }
2928 
2929   Expected<ArrayRef<uint8_t>> ContentsOrErr =
2930       Obj.getSectionContents(*RegInfoSec);
2931   if (!ContentsOrErr) {
2932     this->reportUniqueWarning(
2933         "unable to read the content of the .reginfo section (" +
2934         describe(*RegInfoSec) + "): " + toString(ContentsOrErr.takeError()));
2935     return;
2936   }
2937 
2938   if (ContentsOrErr->size() < sizeof(Elf_Mips_RegInfo<ELFT>)) {
2939     this->reportUniqueWarning("the .reginfo section has an invalid size (0x" +
2940                               Twine::utohexstr(ContentsOrErr->size()) + ")");
2941     return;
2942   }
2943 
2944   DictScope GS(W, "MIPS RegInfo");
2945   printMipsReginfoData(W, *reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(
2946                               ContentsOrErr->data()));
2947 }
2948 
2949 template <class ELFT>
2950 static Expected<const Elf_Mips_Options<ELFT> *>
2951 readMipsOptions(const uint8_t *SecBegin, ArrayRef<uint8_t> &SecData,
2952                 bool &IsSupported) {
2953   if (SecData.size() < sizeof(Elf_Mips_Options<ELFT>))
2954     return createError("the .MIPS.options section has an invalid size (0x" +
2955                        Twine::utohexstr(SecData.size()) + ")");
2956 
2957   const Elf_Mips_Options<ELFT> *O =
2958       reinterpret_cast<const Elf_Mips_Options<ELFT> *>(SecData.data());
2959   const uint8_t Size = O->size;
2960   if (Size > SecData.size()) {
2961     const uint64_t Offset = SecData.data() - SecBegin;
2962     const uint64_t SecSize = Offset + SecData.size();
2963     return createError("a descriptor of size 0x" + Twine::utohexstr(Size) +
2964                        " at offset 0x" + Twine::utohexstr(Offset) +
2965                        " goes past the end of the .MIPS.options "
2966                        "section of size 0x" +
2967                        Twine::utohexstr(SecSize));
2968   }
2969 
2970   IsSupported = O->kind == ODK_REGINFO;
2971   const size_t ExpectedSize =
2972       sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>);
2973 
2974   if (IsSupported)
2975     if (Size < ExpectedSize)
2976       return createError(
2977           "a .MIPS.options entry of kind " +
2978           Twine(getElfMipsOptionsOdkType(O->kind)) +
2979           " has an invalid size (0x" + Twine::utohexstr(Size) +
2980           "), the expected size is 0x" + Twine::utohexstr(ExpectedSize));
2981 
2982   SecData = SecData.drop_front(Size);
2983   return O;
2984 }
2985 
2986 template <class ELFT> void ELFDumper<ELFT>::printMipsOptions() {
2987   const Elf_Shdr *MipsOpts = findSectionByName(".MIPS.options");
2988   if (!MipsOpts) {
2989     W.startLine() << "There is no .MIPS.options section in the file.\n";
2990     return;
2991   }
2992 
2993   DictScope GS(W, "MIPS Options");
2994 
2995   ArrayRef<uint8_t> Data =
2996       unwrapOrError(ObjF.getFileName(), Obj.getSectionContents(*MipsOpts));
2997   const uint8_t *const SecBegin = Data.begin();
2998   while (!Data.empty()) {
2999     bool IsSupported;
3000     Expected<const Elf_Mips_Options<ELFT> *> OptsOrErr =
3001         readMipsOptions<ELFT>(SecBegin, Data, IsSupported);
3002     if (!OptsOrErr) {
3003       reportUniqueWarning(OptsOrErr.takeError());
3004       break;
3005     }
3006 
3007     unsigned Kind = (*OptsOrErr)->kind;
3008     const char *Type = getElfMipsOptionsOdkType(Kind);
3009     if (!IsSupported) {
3010       W.startLine() << "Unsupported MIPS options tag: " << Type << " (" << Kind
3011                     << ")\n";
3012       continue;
3013     }
3014 
3015     DictScope GS(W, Type);
3016     if (Kind == ODK_REGINFO)
3017       printMipsReginfoData(W, (*OptsOrErr)->getRegInfo());
3018     else
3019       llvm_unreachable("unexpected .MIPS.options section descriptor kind");
3020   }
3021 }
3022 
3023 template <class ELFT> void ELFDumper<ELFT>::printStackMap() const {
3024   const Elf_Shdr *StackMapSection = findSectionByName(".llvm_stackmaps");
3025   if (!StackMapSection)
3026     return;
3027 
3028   auto Warn = [&](Error &&E) {
3029     this->reportUniqueWarning("unable to read the stack map from " +
3030                               describe(*StackMapSection) + ": " +
3031                               toString(std::move(E)));
3032   };
3033 
3034   Expected<ArrayRef<uint8_t>> ContentOrErr =
3035       Obj.getSectionContents(*StackMapSection);
3036   if (!ContentOrErr) {
3037     Warn(ContentOrErr.takeError());
3038     return;
3039   }
3040 
3041   if (Error E = StackMapParser<ELFT::TargetEndianness>::validateHeader(
3042           *ContentOrErr)) {
3043     Warn(std::move(E));
3044     return;
3045   }
3046 
3047   prettyPrintStackMap(W, StackMapParser<ELFT::TargetEndianness>(*ContentOrErr));
3048 }
3049 
3050 template <class ELFT>
3051 void ELFDumper<ELFT>::printReloc(const Relocation<ELFT> &R, unsigned RelIndex,
3052                                  const Elf_Shdr &Sec, const Elf_Shdr *SymTab) {
3053   Expected<RelSymbol<ELFT>> Target = getRelocationTarget(R, SymTab);
3054   if (!Target)
3055     reportUniqueWarning("unable to print relocation " + Twine(RelIndex) +
3056                         " in " + describe(Sec) + ": " +
3057                         toString(Target.takeError()));
3058   else
3059     printRelRelaReloc(R, *Target);
3060 }
3061 
3062 static inline void printFields(formatted_raw_ostream &OS, StringRef Str1,
3063                                StringRef Str2) {
3064   OS.PadToColumn(2u);
3065   OS << Str1;
3066   OS.PadToColumn(37u);
3067   OS << Str2 << "\n";
3068   OS.flush();
3069 }
3070 
3071 template <class ELFT>
3072 static std::string getSectionHeadersNumString(const ELFFile<ELFT> &Obj,
3073                                               StringRef FileName) {
3074   const typename ELFT::Ehdr &ElfHeader = Obj.getHeader();
3075   if (ElfHeader.e_shnum != 0)
3076     return to_string(ElfHeader.e_shnum);
3077 
3078   Expected<ArrayRef<typename ELFT::Shdr>> ArrOrErr = Obj.sections();
3079   if (!ArrOrErr) {
3080     // In this case we can ignore an error, because we have already reported a
3081     // warning about the broken section header table earlier.
3082     consumeError(ArrOrErr.takeError());
3083     return "<?>";
3084   }
3085 
3086   if (ArrOrErr->empty())
3087     return "0";
3088   return "0 (" + to_string((*ArrOrErr)[0].sh_size) + ")";
3089 }
3090 
3091 template <class ELFT>
3092 static std::string getSectionHeaderTableIndexString(const ELFFile<ELFT> &Obj,
3093                                                     StringRef FileName) {
3094   const typename ELFT::Ehdr &ElfHeader = Obj.getHeader();
3095   if (ElfHeader.e_shstrndx != SHN_XINDEX)
3096     return to_string(ElfHeader.e_shstrndx);
3097 
3098   Expected<ArrayRef<typename ELFT::Shdr>> ArrOrErr = Obj.sections();
3099   if (!ArrOrErr) {
3100     // In this case we can ignore an error, because we have already reported a
3101     // warning about the broken section header table earlier.
3102     consumeError(ArrOrErr.takeError());
3103     return "<?>";
3104   }
3105 
3106   if (ArrOrErr->empty())
3107     return "65535 (corrupt: out of range)";
3108   return to_string(ElfHeader.e_shstrndx) + " (" +
3109          to_string((*ArrOrErr)[0].sh_link) + ")";
3110 }
3111 
3112 static const EnumEntry<unsigned> *getObjectFileEnumEntry(unsigned Type) {
3113   auto It = llvm::find_if(ElfObjectFileType, [&](const EnumEntry<unsigned> &E) {
3114     return E.Value == Type;
3115   });
3116   if (It != makeArrayRef(ElfObjectFileType).end())
3117     return It;
3118   return nullptr;
3119 }
3120 
3121 template <class ELFT> void GNUELFDumper<ELFT>::printFileHeaders() {
3122   const Elf_Ehdr &e = this->Obj.getHeader();
3123   OS << "ELF Header:\n";
3124   OS << "  Magic:  ";
3125   std::string Str;
3126   for (int i = 0; i < ELF::EI_NIDENT; i++)
3127     OS << format(" %02x", static_cast<int>(e.e_ident[i]));
3128   OS << "\n";
3129   Str = printEnum(e.e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
3130   printFields(OS, "Class:", Str);
3131   Str = printEnum(e.e_ident[ELF::EI_DATA], makeArrayRef(ElfDataEncoding));
3132   printFields(OS, "Data:", Str);
3133   OS.PadToColumn(2u);
3134   OS << "Version:";
3135   OS.PadToColumn(37u);
3136   OS << to_hexString(e.e_ident[ELF::EI_VERSION]);
3137   if (e.e_version == ELF::EV_CURRENT)
3138     OS << " (current)";
3139   OS << "\n";
3140   Str = printEnum(e.e_ident[ELF::EI_OSABI], makeArrayRef(ElfOSABI));
3141   printFields(OS, "OS/ABI:", Str);
3142   printFields(OS,
3143               "ABI Version:", std::to_string(e.e_ident[ELF::EI_ABIVERSION]));
3144 
3145   if (const EnumEntry<unsigned> *E = getObjectFileEnumEntry(e.e_type)) {
3146     Str = E->AltName.str();
3147   } else {
3148     if (e.e_type >= ET_LOPROC)
3149       Str = "Processor Specific: (" + to_hexString(e.e_type, false) + ")";
3150     else if (e.e_type >= ET_LOOS)
3151       Str = "OS Specific: (" + to_hexString(e.e_type, false) + ")";
3152     else
3153       Str = "<unknown>: " + to_hexString(e.e_type, false);
3154   }
3155   printFields(OS, "Type:", Str);
3156 
3157   Str = printEnum(e.e_machine, makeArrayRef(ElfMachineType));
3158   printFields(OS, "Machine:", Str);
3159   Str = "0x" + to_hexString(e.e_version);
3160   printFields(OS, "Version:", Str);
3161   Str = "0x" + to_hexString(e.e_entry);
3162   printFields(OS, "Entry point address:", Str);
3163   Str = to_string(e.e_phoff) + " (bytes into file)";
3164   printFields(OS, "Start of program headers:", Str);
3165   Str = to_string(e.e_shoff) + " (bytes into file)";
3166   printFields(OS, "Start of section headers:", Str);
3167   std::string ElfFlags;
3168   if (e.e_machine == EM_MIPS)
3169     ElfFlags =
3170         printFlags(e.e_flags, makeArrayRef(ElfHeaderMipsFlags),
3171                    unsigned(ELF::EF_MIPS_ARCH), unsigned(ELF::EF_MIPS_ABI),
3172                    unsigned(ELF::EF_MIPS_MACH));
3173   else if (e.e_machine == EM_RISCV)
3174     ElfFlags = printFlags(e.e_flags, makeArrayRef(ElfHeaderRISCVFlags));
3175   Str = "0x" + to_hexString(e.e_flags);
3176   if (!ElfFlags.empty())
3177     Str = Str + ", " + ElfFlags;
3178   printFields(OS, "Flags:", Str);
3179   Str = to_string(e.e_ehsize) + " (bytes)";
3180   printFields(OS, "Size of this header:", Str);
3181   Str = to_string(e.e_phentsize) + " (bytes)";
3182   printFields(OS, "Size of program headers:", Str);
3183   Str = to_string(e.e_phnum);
3184   printFields(OS, "Number of program headers:", Str);
3185   Str = to_string(e.e_shentsize) + " (bytes)";
3186   printFields(OS, "Size of section headers:", Str);
3187   Str = getSectionHeadersNumString(this->Obj, this->FileName);
3188   printFields(OS, "Number of section headers:", Str);
3189   Str = getSectionHeaderTableIndexString(this->Obj, this->FileName);
3190   printFields(OS, "Section header string table index:", Str);
3191 }
3192 
3193 template <class ELFT> std::vector<GroupSection> ELFDumper<ELFT>::getGroups() {
3194   auto GetSignature = [&](const Elf_Sym &Sym, unsigned SymNdx,
3195                           const Elf_Shdr &Symtab) -> StringRef {
3196     Expected<StringRef> StrTableOrErr = Obj.getStringTableForSymtab(Symtab);
3197     if (!StrTableOrErr) {
3198       reportUniqueWarning("unable to get the string table for " +
3199                           describe(Symtab) + ": " +
3200                           toString(StrTableOrErr.takeError()));
3201       return "<?>";
3202     }
3203 
3204     StringRef Strings = *StrTableOrErr;
3205     if (Sym.st_name >= Strings.size()) {
3206       reportUniqueWarning("unable to get the name of the symbol with index " +
3207                           Twine(SymNdx) + ": st_name (0x" +
3208                           Twine::utohexstr(Sym.st_name) +
3209                           ") is past the end of the string table of size 0x" +
3210                           Twine::utohexstr(Strings.size()));
3211       return "<?>";
3212     }
3213 
3214     return StrTableOrErr->data() + Sym.st_name;
3215   };
3216 
3217   std::vector<GroupSection> Ret;
3218   uint64_t I = 0;
3219   for (const Elf_Shdr &Sec : cantFail(Obj.sections())) {
3220     ++I;
3221     if (Sec.sh_type != ELF::SHT_GROUP)
3222       continue;
3223 
3224     StringRef Signature = "<?>";
3225     if (Expected<const Elf_Shdr *> SymtabOrErr = Obj.getSection(Sec.sh_link)) {
3226       if (Expected<const Elf_Sym *> SymOrErr =
3227               Obj.template getEntry<Elf_Sym>(**SymtabOrErr, Sec.sh_info))
3228         Signature = GetSignature(**SymOrErr, Sec.sh_info, **SymtabOrErr);
3229       else
3230         reportUniqueWarning("unable to get the signature symbol for " +
3231                             describe(Sec) + ": " +
3232                             toString(SymOrErr.takeError()));
3233     } else {
3234       reportUniqueWarning("unable to get the symbol table for " +
3235                           describe(Sec) + ": " +
3236                           toString(SymtabOrErr.takeError()));
3237     }
3238 
3239     ArrayRef<Elf_Word> Data;
3240     if (Expected<ArrayRef<Elf_Word>> ContentsOrErr =
3241             Obj.template getSectionContentsAsArray<Elf_Word>(Sec)) {
3242       if (ContentsOrErr->empty())
3243         reportUniqueWarning("unable to read the section group flag from the " +
3244                             describe(Sec) + ": the section is empty");
3245       else
3246         Data = *ContentsOrErr;
3247     } else {
3248       reportUniqueWarning("unable to get the content of the " + describe(Sec) +
3249                           ": " + toString(ContentsOrErr.takeError()));
3250     }
3251 
3252     Ret.push_back({getPrintableSectionName(Sec),
3253                    maybeDemangle(Signature),
3254                    Sec.sh_name,
3255                    I - 1,
3256                    Sec.sh_link,
3257                    Sec.sh_info,
3258                    Data.empty() ? Elf_Word(0) : Data[0],
3259                    {}});
3260 
3261     if (Data.empty())
3262       continue;
3263 
3264     std::vector<GroupMember> &GM = Ret.back().Members;
3265     for (uint32_t Ndx : Data.slice(1)) {
3266       if (Expected<const Elf_Shdr *> SecOrErr = Obj.getSection(Ndx)) {
3267         GM.push_back({getPrintableSectionName(**SecOrErr), Ndx});
3268       } else {
3269         reportUniqueWarning("unable to get the section with index " +
3270                             Twine(Ndx) + " when dumping the " + describe(Sec) +
3271                             ": " + toString(SecOrErr.takeError()));
3272         GM.push_back({"<?>", Ndx});
3273       }
3274     }
3275   }
3276   return Ret;
3277 }
3278 
3279 static DenseMap<uint64_t, const GroupSection *>
3280 mapSectionsToGroups(ArrayRef<GroupSection> Groups) {
3281   DenseMap<uint64_t, const GroupSection *> Ret;
3282   for (const GroupSection &G : Groups)
3283     for (const GroupMember &GM : G.Members)
3284       Ret.insert({GM.Index, &G});
3285   return Ret;
3286 }
3287 
3288 template <class ELFT> void GNUELFDumper<ELFT>::printGroupSections() {
3289   std::vector<GroupSection> V = this->getGroups();
3290   DenseMap<uint64_t, const GroupSection *> Map = mapSectionsToGroups(V);
3291   for (const GroupSection &G : V) {
3292     OS << "\n"
3293        << getGroupType(G.Type) << " group section ["
3294        << format_decimal(G.Index, 5) << "] `" << G.Name << "' [" << G.Signature
3295        << "] contains " << G.Members.size() << " sections:\n"
3296        << "   [Index]    Name\n";
3297     for (const GroupMember &GM : G.Members) {
3298       const GroupSection *MainGroup = Map[GM.Index];
3299       if (MainGroup != &G)
3300         this->reportUniqueWarning(
3301             "section with index " + Twine(GM.Index) +
3302             ", included in the group section with index " +
3303             Twine(MainGroup->Index) +
3304             ", was also found in the group section with index " +
3305             Twine(G.Index));
3306       OS << "   [" << format_decimal(GM.Index, 5) << "]   " << GM.Name << "\n";
3307     }
3308   }
3309 
3310   if (V.empty())
3311     OS << "There are no section groups in this file.\n";
3312 }
3313 
3314 template <class ELFT>
3315 void GNUELFDumper<ELFT>::printRelrReloc(const Elf_Relr &R) {
3316   OS << to_string(format_hex_no_prefix(R, ELFT::Is64Bits ? 16 : 8)) << "\n";
3317 }
3318 
3319 template <class ELFT>
3320 void GNUELFDumper<ELFT>::printRelRelaReloc(const Relocation<ELFT> &R,
3321                                            const RelSymbol<ELFT> &RelSym) {
3322   // First two fields are bit width dependent. The rest of them are fixed width.
3323   unsigned Bias = ELFT::Is64Bits ? 8 : 0;
3324   Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias};
3325   unsigned Width = ELFT::Is64Bits ? 16 : 8;
3326 
3327   Fields[0].Str = to_string(format_hex_no_prefix(R.Offset, Width));
3328   Fields[1].Str = to_string(format_hex_no_prefix(R.Info, Width));
3329 
3330   SmallString<32> RelocName;
3331   this->Obj.getRelocationTypeName(R.Type, RelocName);
3332   Fields[2].Str = RelocName.c_str();
3333 
3334   if (RelSym.Sym)
3335     Fields[3].Str =
3336         to_string(format_hex_no_prefix(RelSym.Sym->getValue(), Width));
3337 
3338   Fields[4].Str = std::string(RelSym.Name);
3339   for (const Field &F : Fields)
3340     printField(F);
3341 
3342   std::string Addend;
3343   if (Optional<int64_t> A = R.Addend) {
3344     int64_t RelAddend = *A;
3345     if (!RelSym.Name.empty()) {
3346       if (RelAddend < 0) {
3347         Addend = " - ";
3348         RelAddend = std::abs(RelAddend);
3349       } else {
3350         Addend = " + ";
3351       }
3352     }
3353     Addend += to_hexString(RelAddend, false);
3354   }
3355   OS << Addend << "\n";
3356 }
3357 
3358 template <class ELFT>
3359 static void printRelocHeaderFields(formatted_raw_ostream &OS, unsigned SType) {
3360   bool IsRela = SType == ELF::SHT_RELA || SType == ELF::SHT_ANDROID_RELA;
3361   bool IsRelr = SType == ELF::SHT_RELR || SType == ELF::SHT_ANDROID_RELR;
3362   if (ELFT::Is64Bits)
3363     OS << "    ";
3364   else
3365     OS << " ";
3366   if (IsRelr && opts::RawRelr)
3367     OS << "Data  ";
3368   else
3369     OS << "Offset";
3370   if (ELFT::Is64Bits)
3371     OS << "             Info             Type"
3372        << "               Symbol's Value  Symbol's Name";
3373   else
3374     OS << "     Info    Type                Sym. Value  Symbol's Name";
3375   if (IsRela)
3376     OS << " + Addend";
3377   OS << "\n";
3378 }
3379 
3380 template <class ELFT>
3381 void GNUELFDumper<ELFT>::printDynamicRelocHeader(unsigned Type, StringRef Name,
3382                                                  const DynRegionInfo &Reg) {
3383   uint64_t Offset = Reg.Addr - this->Obj.base();
3384   OS << "\n'" << Name.str().c_str() << "' relocation section at offset 0x"
3385      << to_hexString(Offset, false) << " contains " << Reg.Size << " bytes:\n";
3386   printRelocHeaderFields<ELFT>(OS, Type);
3387 }
3388 
3389 template <class ELFT>
3390 static bool isRelocationSec(const typename ELFT::Shdr &Sec) {
3391   return Sec.sh_type == ELF::SHT_REL || Sec.sh_type == ELF::SHT_RELA ||
3392          Sec.sh_type == ELF::SHT_RELR || Sec.sh_type == ELF::SHT_ANDROID_REL ||
3393          Sec.sh_type == ELF::SHT_ANDROID_RELA ||
3394          Sec.sh_type == ELF::SHT_ANDROID_RELR;
3395 }
3396 
3397 template <class ELFT> void GNUELFDumper<ELFT>::printRelocations() {
3398   auto GetEntriesNum = [&](const Elf_Shdr &Sec) -> Expected<size_t> {
3399     // Android's packed relocation section needs to be unpacked first
3400     // to get the actual number of entries.
3401     if (Sec.sh_type == ELF::SHT_ANDROID_REL ||
3402         Sec.sh_type == ELF::SHT_ANDROID_RELA) {
3403       Expected<std::vector<typename ELFT::Rela>> RelasOrErr =
3404           this->Obj.android_relas(Sec);
3405       if (!RelasOrErr)
3406         return RelasOrErr.takeError();
3407       return RelasOrErr->size();
3408     }
3409 
3410     if (!opts::RawRelr && (Sec.sh_type == ELF::SHT_RELR ||
3411                            Sec.sh_type == ELF::SHT_ANDROID_RELR)) {
3412       Expected<Elf_Relr_Range> RelrsOrErr = this->Obj.relrs(Sec);
3413       if (!RelrsOrErr)
3414         return RelrsOrErr.takeError();
3415       return this->Obj.decode_relrs(*RelrsOrErr).size();
3416     }
3417 
3418     return Sec.getEntityCount();
3419   };
3420 
3421   bool HasRelocSections = false;
3422   for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) {
3423     if (!isRelocationSec<ELFT>(Sec))
3424       continue;
3425     HasRelocSections = true;
3426 
3427     std::string EntriesNum = "<?>";
3428     if (Expected<size_t> NumOrErr = GetEntriesNum(Sec))
3429       EntriesNum = std::to_string(*NumOrErr);
3430     else
3431       this->reportUniqueWarning("unable to get the number of relocations in " +
3432                                 this->describe(Sec) + ": " +
3433                                 toString(NumOrErr.takeError()));
3434 
3435     uintX_t Offset = Sec.sh_offset;
3436     StringRef Name = this->getPrintableSectionName(Sec);
3437     OS << "\nRelocation section '" << Name << "' at offset 0x"
3438        << to_hexString(Offset, false) << " contains " << EntriesNum
3439        << " entries:\n";
3440     printRelocHeaderFields<ELFT>(OS, Sec.sh_type);
3441     this->printRelocationsHelper(Sec);
3442   }
3443   if (!HasRelocSections)
3444     OS << "\nThere are no relocations in this file.\n";
3445 }
3446 
3447 // Print the offset of a particular section from anyone of the ranges:
3448 // [SHT_LOOS, SHT_HIOS], [SHT_LOPROC, SHT_HIPROC], [SHT_LOUSER, SHT_HIUSER].
3449 // If 'Type' does not fall within any of those ranges, then a string is
3450 // returned as '<unknown>' followed by the type value.
3451 static std::string getSectionTypeOffsetString(unsigned Type) {
3452   if (Type >= SHT_LOOS && Type <= SHT_HIOS)
3453     return "LOOS+0x" + to_hexString(Type - SHT_LOOS);
3454   else if (Type >= SHT_LOPROC && Type <= SHT_HIPROC)
3455     return "LOPROC+0x" + to_hexString(Type - SHT_LOPROC);
3456   else if (Type >= SHT_LOUSER && Type <= SHT_HIUSER)
3457     return "LOUSER+0x" + to_hexString(Type - SHT_LOUSER);
3458   return "0x" + to_hexString(Type) + ": <unknown>";
3459 }
3460 
3461 static std::string getSectionTypeString(unsigned Machine, unsigned Type) {
3462   StringRef Name = getELFSectionTypeName(Machine, Type);
3463 
3464   // Handle SHT_GNU_* type names.
3465   if (Name.startswith("SHT_GNU_")) {
3466     if (Name == "SHT_GNU_HASH")
3467       return "GNU_HASH";
3468     // E.g. SHT_GNU_verneed -> VERNEED.
3469     return Name.drop_front(8).upper();
3470   }
3471 
3472   if (Name == "SHT_SYMTAB_SHNDX")
3473     return "SYMTAB SECTION INDICES";
3474 
3475   if (Name.startswith("SHT_"))
3476     return Name.drop_front(4).str();
3477   return getSectionTypeOffsetString(Type);
3478 }
3479 
3480 static void printSectionDescription(formatted_raw_ostream &OS,
3481                                     unsigned EMachine) {
3482   OS << "Key to Flags:\n";
3483   OS << "  W (write), A (alloc), X (execute), M (merge), S (strings), I "
3484         "(info),\n";
3485   OS << "  L (link order), O (extra OS processing required), G (group), T "
3486         "(TLS),\n";
3487   OS << "  C (compressed), x (unknown), o (OS specific), E (exclude),\n";
3488   OS << "  R (retain)";
3489 
3490   if (EMachine == EM_X86_64)
3491     OS << ", l (large)";
3492   else if (EMachine == EM_ARM)
3493     OS << ", y (purecode)";
3494 
3495   OS << ", p (processor specific)\n";
3496 }
3497 
3498 template <class ELFT> void GNUELFDumper<ELFT>::printSectionHeaders() {
3499   unsigned Bias = ELFT::Is64Bits ? 0 : 8;
3500   ArrayRef<Elf_Shdr> Sections = cantFail(this->Obj.sections());
3501   OS << "There are " << to_string(Sections.size())
3502      << " section headers, starting at offset "
3503      << "0x" << to_hexString(this->Obj.getHeader().e_shoff, false) << ":\n\n";
3504   OS << "Section Headers:\n";
3505   Field Fields[11] = {
3506       {"[Nr]", 2},        {"Name", 7},        {"Type", 25},
3507       {"Address", 41},    {"Off", 58 - Bias}, {"Size", 65 - Bias},
3508       {"ES", 72 - Bias},  {"Flg", 75 - Bias}, {"Lk", 79 - Bias},
3509       {"Inf", 82 - Bias}, {"Al", 86 - Bias}};
3510   for (const Field &F : Fields)
3511     printField(F);
3512   OS << "\n";
3513 
3514   StringRef SecStrTable;
3515   if (Expected<StringRef> SecStrTableOrErr =
3516           this->Obj.getSectionStringTable(Sections, this->WarningHandler))
3517     SecStrTable = *SecStrTableOrErr;
3518   else
3519     this->reportUniqueWarning(SecStrTableOrErr.takeError());
3520 
3521   size_t SectionIndex = 0;
3522   for (const Elf_Shdr &Sec : Sections) {
3523     Fields[0].Str = to_string(SectionIndex);
3524     if (SecStrTable.empty())
3525       Fields[1].Str = "<no-strings>";
3526     else
3527       Fields[1].Str = std::string(unwrapOrError<StringRef>(
3528           this->FileName, this->Obj.getSectionName(Sec, SecStrTable)));
3529     Fields[2].Str =
3530         getSectionTypeString(this->Obj.getHeader().e_machine, Sec.sh_type);
3531     Fields[3].Str =
3532         to_string(format_hex_no_prefix(Sec.sh_addr, ELFT::Is64Bits ? 16 : 8));
3533     Fields[4].Str = to_string(format_hex_no_prefix(Sec.sh_offset, 6));
3534     Fields[5].Str = to_string(format_hex_no_prefix(Sec.sh_size, 6));
3535     Fields[6].Str = to_string(format_hex_no_prefix(Sec.sh_entsize, 2));
3536     Fields[7].Str = getGNUFlags(this->Obj.getHeader().e_machine, Sec.sh_flags);
3537     Fields[8].Str = to_string(Sec.sh_link);
3538     Fields[9].Str = to_string(Sec.sh_info);
3539     Fields[10].Str = to_string(Sec.sh_addralign);
3540 
3541     OS.PadToColumn(Fields[0].Column);
3542     OS << "[" << right_justify(Fields[0].Str, 2) << "]";
3543     for (int i = 1; i < 7; i++)
3544       printField(Fields[i]);
3545     OS.PadToColumn(Fields[7].Column);
3546     OS << right_justify(Fields[7].Str, 3);
3547     OS.PadToColumn(Fields[8].Column);
3548     OS << right_justify(Fields[8].Str, 2);
3549     OS.PadToColumn(Fields[9].Column);
3550     OS << right_justify(Fields[9].Str, 3);
3551     OS.PadToColumn(Fields[10].Column);
3552     OS << right_justify(Fields[10].Str, 2);
3553     OS << "\n";
3554     ++SectionIndex;
3555   }
3556   printSectionDescription(OS, this->Obj.getHeader().e_machine);
3557 }
3558 
3559 template <class ELFT>
3560 void GNUELFDumper<ELFT>::printSymtabMessage(const Elf_Shdr *Symtab,
3561                                             size_t Entries,
3562                                             bool NonVisibilityBitsUsed) const {
3563   StringRef Name;
3564   if (Symtab)
3565     Name = this->getPrintableSectionName(*Symtab);
3566   if (!Name.empty())
3567     OS << "\nSymbol table '" << Name << "'";
3568   else
3569     OS << "\nSymbol table for image";
3570   OS << " contains " << Entries << " entries:\n";
3571 
3572   if (ELFT::Is64Bits)
3573     OS << "   Num:    Value          Size Type    Bind   Vis";
3574   else
3575     OS << "   Num:    Value  Size Type    Bind   Vis";
3576 
3577   if (NonVisibilityBitsUsed)
3578     OS << "             ";
3579   OS << "       Ndx Name\n";
3580 }
3581 
3582 template <class ELFT>
3583 std::string
3584 GNUELFDumper<ELFT>::getSymbolSectionNdx(const Elf_Sym &Symbol,
3585                                         unsigned SymIndex,
3586                                         DataRegion<Elf_Word> ShndxTable) const {
3587   unsigned SectionIndex = Symbol.st_shndx;
3588   switch (SectionIndex) {
3589   case ELF::SHN_UNDEF:
3590     return "UND";
3591   case ELF::SHN_ABS:
3592     return "ABS";
3593   case ELF::SHN_COMMON:
3594     return "COM";
3595   case ELF::SHN_XINDEX: {
3596     Expected<uint32_t> IndexOrErr =
3597         object::getExtendedSymbolTableIndex<ELFT>(Symbol, SymIndex, ShndxTable);
3598     if (!IndexOrErr) {
3599       assert(Symbol.st_shndx == SHN_XINDEX &&
3600              "getExtendedSymbolTableIndex should only fail due to an invalid "
3601              "SHT_SYMTAB_SHNDX table/reference");
3602       this->reportUniqueWarning(IndexOrErr.takeError());
3603       return "RSV[0xffff]";
3604     }
3605     return to_string(format_decimal(*IndexOrErr, 3));
3606   }
3607   default:
3608     // Find if:
3609     // Processor specific
3610     if (SectionIndex >= ELF::SHN_LOPROC && SectionIndex <= ELF::SHN_HIPROC)
3611       return std::string("PRC[0x") +
3612              to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3613     // OS specific
3614     if (SectionIndex >= ELF::SHN_LOOS && SectionIndex <= ELF::SHN_HIOS)
3615       return std::string("OS[0x") +
3616              to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3617     // Architecture reserved:
3618     if (SectionIndex >= ELF::SHN_LORESERVE &&
3619         SectionIndex <= ELF::SHN_HIRESERVE)
3620       return std::string("RSV[0x") +
3621              to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3622     // A normal section with an index
3623     return to_string(format_decimal(SectionIndex, 3));
3624   }
3625 }
3626 
3627 template <class ELFT>
3628 void GNUELFDumper<ELFT>::printSymbol(const Elf_Sym &Symbol, unsigned SymIndex,
3629                                      DataRegion<Elf_Word> ShndxTable,
3630                                      Optional<StringRef> StrTable,
3631                                      bool IsDynamic,
3632                                      bool NonVisibilityBitsUsed) const {
3633   unsigned Bias = ELFT::Is64Bits ? 8 : 0;
3634   Field Fields[8] = {0,         8,         17 + Bias, 23 + Bias,
3635                      31 + Bias, 38 + Bias, 48 + Bias, 51 + Bias};
3636   Fields[0].Str = to_string(format_decimal(SymIndex, 6)) + ":";
3637   Fields[1].Str =
3638       to_string(format_hex_no_prefix(Symbol.st_value, ELFT::Is64Bits ? 16 : 8));
3639   Fields[2].Str = to_string(format_decimal(Symbol.st_size, 5));
3640 
3641   unsigned char SymbolType = Symbol.getType();
3642   if (this->Obj.getHeader().e_machine == ELF::EM_AMDGPU &&
3643       SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
3644     Fields[3].Str = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
3645   else
3646     Fields[3].Str = printEnum(SymbolType, makeArrayRef(ElfSymbolTypes));
3647 
3648   Fields[4].Str =
3649       printEnum(Symbol.getBinding(), makeArrayRef(ElfSymbolBindings));
3650   Fields[5].Str =
3651       printEnum(Symbol.getVisibility(), makeArrayRef(ElfSymbolVisibilities));
3652 
3653   if (Symbol.st_other & ~0x3) {
3654     if (this->Obj.getHeader().e_machine == ELF::EM_AARCH64) {
3655       uint8_t Other = Symbol.st_other & ~0x3;
3656       if (Other & STO_AARCH64_VARIANT_PCS) {
3657         Other &= ~STO_AARCH64_VARIANT_PCS;
3658         Fields[5].Str += " [VARIANT_PCS";
3659         if (Other != 0)
3660           Fields[5].Str.append(" | " + to_hexString(Other, false));
3661         Fields[5].Str.append("]");
3662       }
3663     } else {
3664       Fields[5].Str +=
3665           " [<other: " + to_string(format_hex(Symbol.st_other, 2)) + ">]";
3666     }
3667   }
3668 
3669   Fields[6].Column += NonVisibilityBitsUsed ? 13 : 0;
3670   Fields[6].Str = getSymbolSectionNdx(Symbol, SymIndex, ShndxTable);
3671 
3672   Fields[7].Str = this->getFullSymbolName(Symbol, SymIndex, ShndxTable,
3673                                           StrTable, IsDynamic);
3674   for (const Field &Entry : Fields)
3675     printField(Entry);
3676   OS << "\n";
3677 }
3678 
3679 template <class ELFT>
3680 void GNUELFDumper<ELFT>::printHashedSymbol(const Elf_Sym *Symbol,
3681                                            unsigned SymIndex,
3682                                            DataRegion<Elf_Word> ShndxTable,
3683                                            StringRef StrTable,
3684                                            uint32_t Bucket) {
3685   unsigned Bias = ELFT::Is64Bits ? 8 : 0;
3686   Field Fields[9] = {0,         6,         11,        20 + Bias, 25 + Bias,
3687                      34 + Bias, 41 + Bias, 49 + Bias, 53 + Bias};
3688   Fields[0].Str = to_string(format_decimal(SymIndex, 5));
3689   Fields[1].Str = to_string(format_decimal(Bucket, 3)) + ":";
3690 
3691   Fields[2].Str = to_string(
3692       format_hex_no_prefix(Symbol->st_value, ELFT::Is64Bits ? 16 : 8));
3693   Fields[3].Str = to_string(format_decimal(Symbol->st_size, 5));
3694 
3695   unsigned char SymbolType = Symbol->getType();
3696   if (this->Obj.getHeader().e_machine == ELF::EM_AMDGPU &&
3697       SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
3698     Fields[4].Str = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
3699   else
3700     Fields[4].Str = printEnum(SymbolType, makeArrayRef(ElfSymbolTypes));
3701 
3702   Fields[5].Str =
3703       printEnum(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
3704   Fields[6].Str =
3705       printEnum(Symbol->getVisibility(), makeArrayRef(ElfSymbolVisibilities));
3706   Fields[7].Str = getSymbolSectionNdx(*Symbol, SymIndex, ShndxTable);
3707   Fields[8].Str =
3708       this->getFullSymbolName(*Symbol, SymIndex, ShndxTable, StrTable, true);
3709 
3710   for (const Field &Entry : Fields)
3711     printField(Entry);
3712   OS << "\n";
3713 }
3714 
3715 template <class ELFT>
3716 void GNUELFDumper<ELFT>::printSymbols(bool PrintSymbols,
3717                                       bool PrintDynamicSymbols) {
3718   if (!PrintSymbols && !PrintDynamicSymbols)
3719     return;
3720   // GNU readelf prints both the .dynsym and .symtab with --symbols.
3721   this->printSymbolsHelper(true);
3722   if (PrintSymbols)
3723     this->printSymbolsHelper(false);
3724 }
3725 
3726 template <class ELFT>
3727 void GNUELFDumper<ELFT>::printHashTableSymbols(const Elf_Hash &SysVHash) {
3728   if (this->DynamicStringTable.empty())
3729     return;
3730 
3731   if (ELFT::Is64Bits)
3732     OS << "  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name";
3733   else
3734     OS << "  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name";
3735   OS << "\n";
3736 
3737   Elf_Sym_Range DynSyms = this->dynamic_symbols();
3738   const Elf_Sym *FirstSym = DynSyms.empty() ? nullptr : &DynSyms[0];
3739   if (!FirstSym) {
3740     this->reportUniqueWarning(
3741         Twine("unable to print symbols for the .hash table: the "
3742               "dynamic symbol table ") +
3743         (this->DynSymRegion ? "is empty" : "was not found"));
3744     return;
3745   }
3746 
3747   DataRegion<Elf_Word> ShndxTable(
3748       (const Elf_Word *)this->DynSymTabShndxRegion.Addr, this->Obj.end());
3749   auto Buckets = SysVHash.buckets();
3750   auto Chains = SysVHash.chains();
3751   for (uint32_t Buc = 0; Buc < SysVHash.nbucket; Buc++) {
3752     if (Buckets[Buc] == ELF::STN_UNDEF)
3753       continue;
3754     std::vector<bool> Visited(SysVHash.nchain);
3755     for (uint32_t Ch = Buckets[Buc]; Ch < SysVHash.nchain; Ch = Chains[Ch]) {
3756       if (Ch == ELF::STN_UNDEF)
3757         break;
3758 
3759       if (Visited[Ch]) {
3760         this->reportUniqueWarning(".hash section is invalid: bucket " +
3761                                   Twine(Ch) +
3762                                   ": a cycle was detected in the linked chain");
3763         break;
3764       }
3765 
3766       printHashedSymbol(FirstSym + Ch, Ch, ShndxTable, this->DynamicStringTable,
3767                         Buc);
3768       Visited[Ch] = true;
3769     }
3770   }
3771 }
3772 
3773 template <class ELFT>
3774 void GNUELFDumper<ELFT>::printGnuHashTableSymbols(const Elf_GnuHash &GnuHash) {
3775   if (this->DynamicStringTable.empty())
3776     return;
3777 
3778   Elf_Sym_Range DynSyms = this->dynamic_symbols();
3779   const Elf_Sym *FirstSym = DynSyms.empty() ? nullptr : &DynSyms[0];
3780   if (!FirstSym) {
3781     this->reportUniqueWarning(
3782         Twine("unable to print symbols for the .gnu.hash table: the "
3783               "dynamic symbol table ") +
3784         (this->DynSymRegion ? "is empty" : "was not found"));
3785     return;
3786   }
3787 
3788   auto GetSymbol = [&](uint64_t SymIndex,
3789                        uint64_t SymsTotal) -> const Elf_Sym * {
3790     if (SymIndex >= SymsTotal) {
3791       this->reportUniqueWarning(
3792           "unable to print hashed symbol with index " + Twine(SymIndex) +
3793           ", which is greater than or equal to the number of dynamic symbols "
3794           "(" +
3795           Twine::utohexstr(SymsTotal) + ")");
3796       return nullptr;
3797     }
3798     return FirstSym + SymIndex;
3799   };
3800 
3801   Expected<ArrayRef<Elf_Word>> ValuesOrErr =
3802       getGnuHashTableChains<ELFT>(this->DynSymRegion, &GnuHash);
3803   ArrayRef<Elf_Word> Values;
3804   if (!ValuesOrErr)
3805     this->reportUniqueWarning("unable to get hash values for the SHT_GNU_HASH "
3806                               "section: " +
3807                               toString(ValuesOrErr.takeError()));
3808   else
3809     Values = *ValuesOrErr;
3810 
3811   DataRegion<Elf_Word> ShndxTable(
3812       (const Elf_Word *)this->DynSymTabShndxRegion.Addr, this->Obj.end());
3813   ArrayRef<Elf_Word> Buckets = GnuHash.buckets();
3814   for (uint32_t Buc = 0; Buc < GnuHash.nbuckets; Buc++) {
3815     if (Buckets[Buc] == ELF::STN_UNDEF)
3816       continue;
3817     uint32_t Index = Buckets[Buc];
3818     // Print whole chain.
3819     while (true) {
3820       uint32_t SymIndex = Index++;
3821       if (const Elf_Sym *Sym = GetSymbol(SymIndex, DynSyms.size()))
3822         printHashedSymbol(Sym, SymIndex, ShndxTable, this->DynamicStringTable,
3823                           Buc);
3824       else
3825         break;
3826 
3827       if (SymIndex < GnuHash.symndx) {
3828         this->reportUniqueWarning(
3829             "unable to read the hash value for symbol with index " +
3830             Twine(SymIndex) +
3831             ", which is less than the index of the first hashed symbol (" +
3832             Twine(GnuHash.symndx) + ")");
3833         break;
3834       }
3835 
3836        // Chain ends at symbol with stopper bit.
3837       if ((Values[SymIndex - GnuHash.symndx] & 1) == 1)
3838         break;
3839     }
3840   }
3841 }
3842 
3843 template <class ELFT> void GNUELFDumper<ELFT>::printHashSymbols() {
3844   if (this->HashTable) {
3845     OS << "\n Symbol table of .hash for image:\n";
3846     if (Error E = checkHashTable<ELFT>(*this, this->HashTable))
3847       this->reportUniqueWarning(std::move(E));
3848     else
3849       printHashTableSymbols(*this->HashTable);
3850   }
3851 
3852   // Try printing the .gnu.hash table.
3853   if (this->GnuHashTable) {
3854     OS << "\n Symbol table of .gnu.hash for image:\n";
3855     if (ELFT::Is64Bits)
3856       OS << "  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name";
3857     else
3858       OS << "  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name";
3859     OS << "\n";
3860 
3861     if (Error E = checkGNUHashTable<ELFT>(this->Obj, this->GnuHashTable))
3862       this->reportUniqueWarning(std::move(E));
3863     else
3864       printGnuHashTableSymbols(*this->GnuHashTable);
3865   }
3866 }
3867 
3868 template <class ELFT> void GNUELFDumper<ELFT>::printSectionDetails() {
3869   ArrayRef<Elf_Shdr> Sections = cantFail(this->Obj.sections());
3870   OS << "There are " << to_string(Sections.size())
3871      << " section headers, starting at offset "
3872      << "0x" << to_hexString(this->Obj.getHeader().e_shoff, false) << ":\n\n";
3873 
3874   OS << "Section Headers:\n";
3875 
3876   auto PrintFields = [&](ArrayRef<Field> V) {
3877     for (const Field &F : V)
3878       printField(F);
3879     OS << "\n";
3880   };
3881 
3882   PrintFields({{"[Nr]", 2}, {"Name", 7}});
3883 
3884   constexpr bool Is64 = ELFT::Is64Bits;
3885   PrintFields({{"Type", 7},
3886                {Is64 ? "Address" : "Addr", 23},
3887                {"Off", Is64 ? 40 : 32},
3888                {"Size", Is64 ? 47 : 39},
3889                {"ES", Is64 ? 54 : 46},
3890                {"Lk", Is64 ? 59 : 51},
3891                {"Inf", Is64 ? 62 : 54},
3892                {"Al", Is64 ? 66 : 57}});
3893   PrintFields({{"Flags", 7}});
3894 
3895   StringRef SecStrTable;
3896   if (Expected<StringRef> SecStrTableOrErr =
3897           this->Obj.getSectionStringTable(Sections, this->WarningHandler))
3898     SecStrTable = *SecStrTableOrErr;
3899   else
3900     this->reportUniqueWarning(SecStrTableOrErr.takeError());
3901 
3902   size_t SectionIndex = 0;
3903   const unsigned AddrSize = Is64 ? 16 : 8;
3904   for (const Elf_Shdr &S : Sections) {
3905     StringRef Name = "<?>";
3906     if (Expected<StringRef> NameOrErr =
3907             this->Obj.getSectionName(S, SecStrTable))
3908       Name = *NameOrErr;
3909     else
3910       this->reportUniqueWarning(NameOrErr.takeError());
3911 
3912     OS.PadToColumn(2);
3913     OS << "[" << right_justify(to_string(SectionIndex), 2) << "]";
3914     PrintFields({{Name, 7}});
3915     PrintFields(
3916         {{getSectionTypeString(this->Obj.getHeader().e_machine, S.sh_type), 7},
3917          {to_string(format_hex_no_prefix(S.sh_addr, AddrSize)), 23},
3918          {to_string(format_hex_no_prefix(S.sh_offset, 6)), Is64 ? 39 : 32},
3919          {to_string(format_hex_no_prefix(S.sh_size, 6)), Is64 ? 47 : 39},
3920          {to_string(format_hex_no_prefix(S.sh_entsize, 2)), Is64 ? 54 : 46},
3921          {to_string(S.sh_link), Is64 ? 59 : 51},
3922          {to_string(S.sh_info), Is64 ? 63 : 55},
3923          {to_string(S.sh_addralign), Is64 ? 66 : 58}});
3924 
3925     OS.PadToColumn(7);
3926     OS << "[" << to_string(format_hex_no_prefix(S.sh_flags, AddrSize)) << "]: ";
3927 
3928     DenseMap<unsigned, StringRef> FlagToName = {
3929         {SHF_WRITE, "WRITE"},           {SHF_ALLOC, "ALLOC"},
3930         {SHF_EXECINSTR, "EXEC"},        {SHF_MERGE, "MERGE"},
3931         {SHF_STRINGS, "STRINGS"},       {SHF_INFO_LINK, "INFO LINK"},
3932         {SHF_LINK_ORDER, "LINK ORDER"}, {SHF_OS_NONCONFORMING, "OS NONCONF"},
3933         {SHF_GROUP, "GROUP"},           {SHF_TLS, "TLS"},
3934         {SHF_COMPRESSED, "COMPRESSED"}, {SHF_EXCLUDE, "EXCLUDE"}};
3935 
3936     uint64_t Flags = S.sh_flags;
3937     uint64_t UnknownFlags = 0;
3938     ListSeparator LS;
3939     while (Flags) {
3940       // Take the least significant bit as a flag.
3941       uint64_t Flag = Flags & -Flags;
3942       Flags -= Flag;
3943 
3944       auto It = FlagToName.find(Flag);
3945       if (It != FlagToName.end())
3946         OS << LS << It->second;
3947       else
3948         UnknownFlags |= Flag;
3949     }
3950 
3951     auto PrintUnknownFlags = [&](uint64_t Mask, StringRef Name) {
3952       uint64_t FlagsToPrint = UnknownFlags & Mask;
3953       if (!FlagsToPrint)
3954         return;
3955 
3956       OS << LS << Name << " ("
3957          << to_string(format_hex_no_prefix(FlagsToPrint, AddrSize)) << ")";
3958       UnknownFlags &= ~Mask;
3959     };
3960 
3961     PrintUnknownFlags(SHF_MASKOS, "OS");
3962     PrintUnknownFlags(SHF_MASKPROC, "PROC");
3963     PrintUnknownFlags(uint64_t(-1), "UNKNOWN");
3964 
3965     OS << "\n";
3966     ++SectionIndex;
3967   }
3968 }
3969 
3970 static inline std::string printPhdrFlags(unsigned Flag) {
3971   std::string Str;
3972   Str = (Flag & PF_R) ? "R" : " ";
3973   Str += (Flag & PF_W) ? "W" : " ";
3974   Str += (Flag & PF_X) ? "E" : " ";
3975   return Str;
3976 }
3977 
3978 template <class ELFT>
3979 static bool checkTLSSections(const typename ELFT::Phdr &Phdr,
3980                              const typename ELFT::Shdr &Sec) {
3981   if (Sec.sh_flags & ELF::SHF_TLS) {
3982     // .tbss must only be shown in the PT_TLS segment.
3983     if (Sec.sh_type == ELF::SHT_NOBITS)
3984       return Phdr.p_type == ELF::PT_TLS;
3985 
3986     // SHF_TLS sections are only shown in PT_TLS, PT_LOAD or PT_GNU_RELRO
3987     // segments.
3988     return (Phdr.p_type == ELF::PT_TLS) || (Phdr.p_type == ELF::PT_LOAD) ||
3989            (Phdr.p_type == ELF::PT_GNU_RELRO);
3990   }
3991 
3992   // PT_TLS must only have SHF_TLS sections.
3993   return Phdr.p_type != ELF::PT_TLS;
3994 }
3995 
3996 template <class ELFT>
3997 static bool checkOffsets(const typename ELFT::Phdr &Phdr,
3998                          const typename ELFT::Shdr &Sec) {
3999   // SHT_NOBITS sections don't need to have an offset inside the segment.
4000   if (Sec.sh_type == ELF::SHT_NOBITS)
4001     return true;
4002 
4003   if (Sec.sh_offset < Phdr.p_offset)
4004     return false;
4005 
4006   // Only non-empty sections can be at the end of a segment.
4007   if (Sec.sh_size == 0)
4008     return (Sec.sh_offset + 1 <= Phdr.p_offset + Phdr.p_filesz);
4009   return Sec.sh_offset + Sec.sh_size <= Phdr.p_offset + Phdr.p_filesz;
4010 }
4011 
4012 // Check that an allocatable section belongs to a virtual address
4013 // space of a segment.
4014 template <class ELFT>
4015 static bool checkVMA(const typename ELFT::Phdr &Phdr,
4016                      const typename ELFT::Shdr &Sec) {
4017   if (!(Sec.sh_flags & ELF::SHF_ALLOC))
4018     return true;
4019 
4020   if (Sec.sh_addr < Phdr.p_vaddr)
4021     return false;
4022 
4023   bool IsTbss =
4024       (Sec.sh_type == ELF::SHT_NOBITS) && ((Sec.sh_flags & ELF::SHF_TLS) != 0);
4025   // .tbss is special, it only has memory in PT_TLS and has NOBITS properties.
4026   bool IsTbssInNonTLS = IsTbss && Phdr.p_type != ELF::PT_TLS;
4027   // Only non-empty sections can be at the end of a segment.
4028   if (Sec.sh_size == 0 || IsTbssInNonTLS)
4029     return Sec.sh_addr + 1 <= Phdr.p_vaddr + Phdr.p_memsz;
4030   return Sec.sh_addr + Sec.sh_size <= Phdr.p_vaddr + Phdr.p_memsz;
4031 }
4032 
4033 template <class ELFT>
4034 static bool checkPTDynamic(const typename ELFT::Phdr &Phdr,
4035                            const typename ELFT::Shdr &Sec) {
4036   if (Phdr.p_type != ELF::PT_DYNAMIC || Phdr.p_memsz == 0 || Sec.sh_size != 0)
4037     return true;
4038 
4039   // We get here when we have an empty section. Only non-empty sections can be
4040   // at the start or at the end of PT_DYNAMIC.
4041   // Is section within the phdr both based on offset and VMA?
4042   bool CheckOffset = (Sec.sh_type == ELF::SHT_NOBITS) ||
4043                      (Sec.sh_offset > Phdr.p_offset &&
4044                       Sec.sh_offset < Phdr.p_offset + Phdr.p_filesz);
4045   bool CheckVA = !(Sec.sh_flags & ELF::SHF_ALLOC) ||
4046                  (Sec.sh_addr > Phdr.p_vaddr && Sec.sh_addr < Phdr.p_memsz);
4047   return CheckOffset && CheckVA;
4048 }
4049 
4050 template <class ELFT>
4051 void GNUELFDumper<ELFT>::printProgramHeaders(
4052     bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) {
4053   if (PrintProgramHeaders)
4054     printProgramHeaders();
4055 
4056   // Display the section mapping along with the program headers, unless
4057   // -section-mapping is explicitly set to false.
4058   if (PrintSectionMapping != cl::BOU_FALSE)
4059     printSectionMapping();
4060 }
4061 
4062 template <class ELFT> void GNUELFDumper<ELFT>::printProgramHeaders() {
4063   unsigned Bias = ELFT::Is64Bits ? 8 : 0;
4064   const Elf_Ehdr &Header = this->Obj.getHeader();
4065   Field Fields[8] = {2,         17,        26,        37 + Bias,
4066                      48 + Bias, 56 + Bias, 64 + Bias, 68 + Bias};
4067   OS << "\nElf file type is "
4068      << printEnum(Header.e_type, makeArrayRef(ElfObjectFileType)) << "\n"
4069      << "Entry point " << format_hex(Header.e_entry, 3) << "\n"
4070      << "There are " << Header.e_phnum << " program headers,"
4071      << " starting at offset " << Header.e_phoff << "\n\n"
4072      << "Program Headers:\n";
4073   if (ELFT::Is64Bits)
4074     OS << "  Type           Offset   VirtAddr           PhysAddr         "
4075        << "  FileSiz  MemSiz   Flg Align\n";
4076   else
4077     OS << "  Type           Offset   VirtAddr   PhysAddr   FileSiz "
4078        << "MemSiz  Flg Align\n";
4079 
4080   unsigned Width = ELFT::Is64Bits ? 18 : 10;
4081   unsigned SizeWidth = ELFT::Is64Bits ? 8 : 7;
4082 
4083   Expected<ArrayRef<Elf_Phdr>> PhdrsOrErr = this->Obj.program_headers();
4084   if (!PhdrsOrErr) {
4085     this->reportUniqueWarning("unable to dump program headers: " +
4086                               toString(PhdrsOrErr.takeError()));
4087     return;
4088   }
4089 
4090   for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
4091     Fields[0].Str = getGNUPtType(Header.e_machine, Phdr.p_type);
4092     Fields[1].Str = to_string(format_hex(Phdr.p_offset, 8));
4093     Fields[2].Str = to_string(format_hex(Phdr.p_vaddr, Width));
4094     Fields[3].Str = to_string(format_hex(Phdr.p_paddr, Width));
4095     Fields[4].Str = to_string(format_hex(Phdr.p_filesz, SizeWidth));
4096     Fields[5].Str = to_string(format_hex(Phdr.p_memsz, SizeWidth));
4097     Fields[6].Str = printPhdrFlags(Phdr.p_flags);
4098     Fields[7].Str = to_string(format_hex(Phdr.p_align, 1));
4099     for (const Field &F : Fields)
4100       printField(F);
4101     if (Phdr.p_type == ELF::PT_INTERP) {
4102       OS << "\n";
4103       auto ReportBadInterp = [&](const Twine &Msg) {
4104         this->reportUniqueWarning(
4105             "unable to read program interpreter name at offset 0x" +
4106             Twine::utohexstr(Phdr.p_offset) + ": " + Msg);
4107       };
4108 
4109       if (Phdr.p_offset >= this->Obj.getBufSize()) {
4110         ReportBadInterp("it goes past the end of the file (0x" +
4111                         Twine::utohexstr(this->Obj.getBufSize()) + ")");
4112         continue;
4113       }
4114 
4115       const char *Data =
4116           reinterpret_cast<const char *>(this->Obj.base()) + Phdr.p_offset;
4117       size_t MaxSize = this->Obj.getBufSize() - Phdr.p_offset;
4118       size_t Len = strnlen(Data, MaxSize);
4119       if (Len == MaxSize) {
4120         ReportBadInterp("it is not null-terminated");
4121         continue;
4122       }
4123 
4124       OS << "      [Requesting program interpreter: ";
4125       OS << StringRef(Data, Len) << "]";
4126     }
4127     OS << "\n";
4128   }
4129 }
4130 
4131 template <class ELFT> void GNUELFDumper<ELFT>::printSectionMapping() {
4132   OS << "\n Section to Segment mapping:\n  Segment Sections...\n";
4133   DenseSet<const Elf_Shdr *> BelongsToSegment;
4134   int Phnum = 0;
4135 
4136   Expected<ArrayRef<Elf_Phdr>> PhdrsOrErr = this->Obj.program_headers();
4137   if (!PhdrsOrErr) {
4138     this->reportUniqueWarning(
4139         "can't read program headers to build section to segment mapping: " +
4140         toString(PhdrsOrErr.takeError()));
4141     return;
4142   }
4143 
4144   for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
4145     std::string Sections;
4146     OS << format("   %2.2d     ", Phnum++);
4147     // Check if each section is in a segment and then print mapping.
4148     for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) {
4149       if (Sec.sh_type == ELF::SHT_NULL)
4150         continue;
4151 
4152       // readelf additionally makes sure it does not print zero sized sections
4153       // at end of segments and for PT_DYNAMIC both start and end of section
4154       // .tbss must only be shown in PT_TLS section.
4155       if (checkTLSSections<ELFT>(Phdr, Sec) && checkOffsets<ELFT>(Phdr, Sec) &&
4156           checkVMA<ELFT>(Phdr, Sec) && checkPTDynamic<ELFT>(Phdr, Sec)) {
4157         Sections +=
4158             unwrapOrError(this->FileName, this->Obj.getSectionName(Sec)).str() +
4159             " ";
4160         BelongsToSegment.insert(&Sec);
4161       }
4162     }
4163     OS << Sections << "\n";
4164     OS.flush();
4165   }
4166 
4167   // Display sections that do not belong to a segment.
4168   std::string Sections;
4169   for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) {
4170     if (BelongsToSegment.find(&Sec) == BelongsToSegment.end())
4171       Sections +=
4172           unwrapOrError(this->FileName, this->Obj.getSectionName(Sec)).str() +
4173           ' ';
4174   }
4175   if (!Sections.empty()) {
4176     OS << "   None  " << Sections << '\n';
4177     OS.flush();
4178   }
4179 }
4180 
4181 namespace {
4182 
4183 template <class ELFT>
4184 RelSymbol<ELFT> getSymbolForReloc(const ELFDumper<ELFT> &Dumper,
4185                                   const Relocation<ELFT> &Reloc) {
4186   using Elf_Sym = typename ELFT::Sym;
4187   auto WarnAndReturn = [&](const Elf_Sym *Sym,
4188                            const Twine &Reason) -> RelSymbol<ELFT> {
4189     Dumper.reportUniqueWarning(
4190         "unable to get name of the dynamic symbol with index " +
4191         Twine(Reloc.Symbol) + ": " + Reason);
4192     return {Sym, "<corrupt>"};
4193   };
4194 
4195   ArrayRef<Elf_Sym> Symbols = Dumper.dynamic_symbols();
4196   const Elf_Sym *FirstSym = Symbols.begin();
4197   if (!FirstSym)
4198     return WarnAndReturn(nullptr, "no dynamic symbol table found");
4199 
4200   // We might have an object without a section header. In this case the size of
4201   // Symbols is zero, because there is no way to know the size of the dynamic
4202   // table. We should allow this case and not print a warning.
4203   if (!Symbols.empty() && Reloc.Symbol >= Symbols.size())
4204     return WarnAndReturn(
4205         nullptr,
4206         "index is greater than or equal to the number of dynamic symbols (" +
4207             Twine(Symbols.size()) + ")");
4208 
4209   const ELFFile<ELFT> &Obj = Dumper.getElfObject().getELFFile();
4210   const uint64_t FileSize = Obj.getBufSize();
4211   const uint64_t SymOffset = ((const uint8_t *)FirstSym - Obj.base()) +
4212                              (uint64_t)Reloc.Symbol * sizeof(Elf_Sym);
4213   if (SymOffset + sizeof(Elf_Sym) > FileSize)
4214     return WarnAndReturn(nullptr, "symbol at 0x" + Twine::utohexstr(SymOffset) +
4215                                       " goes past the end of the file (0x" +
4216                                       Twine::utohexstr(FileSize) + ")");
4217 
4218   const Elf_Sym *Sym = FirstSym + Reloc.Symbol;
4219   Expected<StringRef> ErrOrName = Sym->getName(Dumper.getDynamicStringTable());
4220   if (!ErrOrName)
4221     return WarnAndReturn(Sym, toString(ErrOrName.takeError()));
4222 
4223   return {Sym == FirstSym ? nullptr : Sym, maybeDemangle(*ErrOrName)};
4224 }
4225 } // namespace
4226 
4227 template <class ELFT>
4228 static size_t getMaxDynamicTagSize(const ELFFile<ELFT> &Obj,
4229                                    typename ELFT::DynRange Tags) {
4230   size_t Max = 0;
4231   for (const typename ELFT::Dyn &Dyn : Tags)
4232     Max = std::max(Max, Obj.getDynamicTagAsString(Dyn.d_tag).size());
4233   return Max;
4234 }
4235 
4236 template <class ELFT> void GNUELFDumper<ELFT>::printDynamicTable() {
4237   Elf_Dyn_Range Table = this->dynamic_table();
4238   if (Table.empty())
4239     return;
4240 
4241   OS << "Dynamic section at offset "
4242      << format_hex(reinterpret_cast<const uint8_t *>(this->DynamicTable.Addr) -
4243                        this->Obj.base(),
4244                    1)
4245      << " contains " << Table.size() << " entries:\n";
4246 
4247   // The type name is surrounded with round brackets, hence add 2.
4248   size_t MaxTagSize = getMaxDynamicTagSize(this->Obj, Table) + 2;
4249   // The "Name/Value" column should be indented from the "Type" column by N
4250   // spaces, where N = MaxTagSize - length of "Type" (4) + trailing
4251   // space (1) = 3.
4252   OS << "  Tag" + std::string(ELFT::Is64Bits ? 16 : 8, ' ') + "Type"
4253      << std::string(MaxTagSize - 3, ' ') << "Name/Value\n";
4254 
4255   std::string ValueFmt = " %-" + std::to_string(MaxTagSize) + "s ";
4256   for (auto Entry : Table) {
4257     uintX_t Tag = Entry.getTag();
4258     std::string Type =
4259         std::string("(") + this->Obj.getDynamicTagAsString(Tag).c_str() + ")";
4260     std::string Value = this->getDynamicEntry(Tag, Entry.getVal());
4261     OS << "  " << format_hex(Tag, ELFT::Is64Bits ? 18 : 10)
4262        << format(ValueFmt.c_str(), Type.c_str()) << Value << "\n";
4263   }
4264 }
4265 
4266 template <class ELFT> void GNUELFDumper<ELFT>::printDynamicRelocations() {
4267   this->printDynamicRelocationsHelper();
4268 }
4269 
4270 template <class ELFT>
4271 void ELFDumper<ELFT>::printDynamicReloc(const Relocation<ELFT> &R) {
4272   printRelRelaReloc(R, getSymbolForReloc(*this, R));
4273 }
4274 
4275 template <class ELFT>
4276 void ELFDumper<ELFT>::printRelocationsHelper(const Elf_Shdr &Sec) {
4277   this->forEachRelocationDo(
4278       Sec, opts::RawRelr,
4279       [&](const Relocation<ELFT> &R, unsigned Ndx, const Elf_Shdr &Sec,
4280           const Elf_Shdr *SymTab) { printReloc(R, Ndx, Sec, SymTab); },
4281       [&](const Elf_Relr &R) { printRelrReloc(R); });
4282 }
4283 
4284 template <class ELFT> void ELFDumper<ELFT>::printDynamicRelocationsHelper() {
4285   const bool IsMips64EL = this->Obj.isMips64EL();
4286   if (this->DynRelaRegion.Size > 0) {
4287     printDynamicRelocHeader(ELF::SHT_RELA, "RELA", this->DynRelaRegion);
4288     for (const Elf_Rela &Rela :
4289          this->DynRelaRegion.template getAsArrayRef<Elf_Rela>())
4290       printDynamicReloc(Relocation<ELFT>(Rela, IsMips64EL));
4291   }
4292 
4293   if (this->DynRelRegion.Size > 0) {
4294     printDynamicRelocHeader(ELF::SHT_REL, "REL", this->DynRelRegion);
4295     for (const Elf_Rel &Rel :
4296          this->DynRelRegion.template getAsArrayRef<Elf_Rel>())
4297       printDynamicReloc(Relocation<ELFT>(Rel, IsMips64EL));
4298   }
4299 
4300   if (this->DynRelrRegion.Size > 0) {
4301     printDynamicRelocHeader(ELF::SHT_REL, "RELR", this->DynRelrRegion);
4302     Elf_Relr_Range Relrs =
4303         this->DynRelrRegion.template getAsArrayRef<Elf_Relr>();
4304     for (const Elf_Rel &Rel : Obj.decode_relrs(Relrs))
4305       printDynamicReloc(Relocation<ELFT>(Rel, IsMips64EL));
4306   }
4307 
4308   if (this->DynPLTRelRegion.Size) {
4309     if (this->DynPLTRelRegion.EntSize == sizeof(Elf_Rela)) {
4310       printDynamicRelocHeader(ELF::SHT_RELA, "PLT", this->DynPLTRelRegion);
4311       for (const Elf_Rela &Rela :
4312            this->DynPLTRelRegion.template getAsArrayRef<Elf_Rela>())
4313         printDynamicReloc(Relocation<ELFT>(Rela, IsMips64EL));
4314     } else {
4315       printDynamicRelocHeader(ELF::SHT_REL, "PLT", this->DynPLTRelRegion);
4316       for (const Elf_Rel &Rel :
4317            this->DynPLTRelRegion.template getAsArrayRef<Elf_Rel>())
4318         printDynamicReloc(Relocation<ELFT>(Rel, IsMips64EL));
4319     }
4320   }
4321 }
4322 
4323 template <class ELFT>
4324 void GNUELFDumper<ELFT>::printGNUVersionSectionProlog(
4325     const typename ELFT::Shdr &Sec, const Twine &Label, unsigned EntriesNum) {
4326   // Don't inline the SecName, because it might report a warning to stderr and
4327   // corrupt the output.
4328   StringRef SecName = this->getPrintableSectionName(Sec);
4329   OS << Label << " section '" << SecName << "' "
4330      << "contains " << EntriesNum << " entries:\n";
4331 
4332   StringRef LinkedSecName = "<corrupt>";
4333   if (Expected<const typename ELFT::Shdr *> LinkedSecOrErr =
4334           this->Obj.getSection(Sec.sh_link))
4335     LinkedSecName = this->getPrintableSectionName(**LinkedSecOrErr);
4336   else
4337     this->reportUniqueWarning("invalid section linked to " +
4338                               this->describe(Sec) + ": " +
4339                               toString(LinkedSecOrErr.takeError()));
4340 
4341   OS << " Addr: " << format_hex_no_prefix(Sec.sh_addr, 16)
4342      << "  Offset: " << format_hex(Sec.sh_offset, 8)
4343      << "  Link: " << Sec.sh_link << " (" << LinkedSecName << ")\n";
4344 }
4345 
4346 template <class ELFT>
4347 void GNUELFDumper<ELFT>::printVersionSymbolSection(const Elf_Shdr *Sec) {
4348   if (!Sec)
4349     return;
4350 
4351   printGNUVersionSectionProlog(*Sec, "Version symbols",
4352                                Sec->sh_size / sizeof(Elf_Versym));
4353   Expected<ArrayRef<Elf_Versym>> VerTableOrErr =
4354       this->getVersionTable(*Sec, /*SymTab=*/nullptr,
4355                             /*StrTab=*/nullptr, /*SymTabSec=*/nullptr);
4356   if (!VerTableOrErr) {
4357     this->reportUniqueWarning(VerTableOrErr.takeError());
4358     return;
4359   }
4360 
4361   SmallVector<Optional<VersionEntry>, 0> *VersionMap = nullptr;
4362   if (Expected<SmallVector<Optional<VersionEntry>, 0> *> MapOrErr =
4363           this->getVersionMap())
4364     VersionMap = *MapOrErr;
4365   else
4366     this->reportUniqueWarning(MapOrErr.takeError());
4367 
4368   ArrayRef<Elf_Versym> VerTable = *VerTableOrErr;
4369   std::vector<StringRef> Versions;
4370   for (size_t I = 0, E = VerTable.size(); I < E; ++I) {
4371     unsigned Ndx = VerTable[I].vs_index;
4372     if (Ndx == VER_NDX_LOCAL || Ndx == VER_NDX_GLOBAL) {
4373       Versions.emplace_back(Ndx == VER_NDX_LOCAL ? "*local*" : "*global*");
4374       continue;
4375     }
4376 
4377     if (!VersionMap) {
4378       Versions.emplace_back("<corrupt>");
4379       continue;
4380     }
4381 
4382     bool IsDefault;
4383     Expected<StringRef> NameOrErr = this->Obj.getSymbolVersionByIndex(
4384         Ndx, IsDefault, *VersionMap, /*IsSymHidden=*/None);
4385     if (!NameOrErr) {
4386       this->reportUniqueWarning("unable to get a version for entry " +
4387                                 Twine(I) + " of " + this->describe(*Sec) +
4388                                 ": " + toString(NameOrErr.takeError()));
4389       Versions.emplace_back("<corrupt>");
4390       continue;
4391     }
4392     Versions.emplace_back(*NameOrErr);
4393   }
4394 
4395   // readelf prints 4 entries per line.
4396   uint64_t Entries = VerTable.size();
4397   for (uint64_t VersymRow = 0; VersymRow < Entries; VersymRow += 4) {
4398     OS << "  " << format_hex_no_prefix(VersymRow, 3) << ":";
4399     for (uint64_t I = 0; (I < 4) && (I + VersymRow) < Entries; ++I) {
4400       unsigned Ndx = VerTable[VersymRow + I].vs_index;
4401       OS << format("%4x%c", Ndx & VERSYM_VERSION,
4402                    Ndx & VERSYM_HIDDEN ? 'h' : ' ');
4403       OS << left_justify("(" + std::string(Versions[VersymRow + I]) + ")", 13);
4404     }
4405     OS << '\n';
4406   }
4407   OS << '\n';
4408 }
4409 
4410 static std::string versionFlagToString(unsigned Flags) {
4411   if (Flags == 0)
4412     return "none";
4413 
4414   std::string Ret;
4415   auto AddFlag = [&Ret, &Flags](unsigned Flag, StringRef Name) {
4416     if (!(Flags & Flag))
4417       return;
4418     if (!Ret.empty())
4419       Ret += " | ";
4420     Ret += Name;
4421     Flags &= ~Flag;
4422   };
4423 
4424   AddFlag(VER_FLG_BASE, "BASE");
4425   AddFlag(VER_FLG_WEAK, "WEAK");
4426   AddFlag(VER_FLG_INFO, "INFO");
4427   AddFlag(~0, "<unknown>");
4428   return Ret;
4429 }
4430 
4431 template <class ELFT>
4432 void GNUELFDumper<ELFT>::printVersionDefinitionSection(const Elf_Shdr *Sec) {
4433   if (!Sec)
4434     return;
4435 
4436   printGNUVersionSectionProlog(*Sec, "Version definition", Sec->sh_info);
4437 
4438   Expected<std::vector<VerDef>> V = this->Obj.getVersionDefinitions(*Sec);
4439   if (!V) {
4440     this->reportUniqueWarning(V.takeError());
4441     return;
4442   }
4443 
4444   for (const VerDef &Def : *V) {
4445     OS << format("  0x%04x: Rev: %u  Flags: %s  Index: %u  Cnt: %u  Name: %s\n",
4446                  Def.Offset, Def.Version,
4447                  versionFlagToString(Def.Flags).c_str(), Def.Ndx, Def.Cnt,
4448                  Def.Name.data());
4449     unsigned I = 0;
4450     for (const VerdAux &Aux : Def.AuxV)
4451       OS << format("  0x%04x: Parent %u: %s\n", Aux.Offset, ++I,
4452                    Aux.Name.data());
4453   }
4454 
4455   OS << '\n';
4456 }
4457 
4458 template <class ELFT>
4459 void GNUELFDumper<ELFT>::printVersionDependencySection(const Elf_Shdr *Sec) {
4460   if (!Sec)
4461     return;
4462 
4463   unsigned VerneedNum = Sec->sh_info;
4464   printGNUVersionSectionProlog(*Sec, "Version needs", VerneedNum);
4465 
4466   Expected<std::vector<VerNeed>> V =
4467       this->Obj.getVersionDependencies(*Sec, this->WarningHandler);
4468   if (!V) {
4469     this->reportUniqueWarning(V.takeError());
4470     return;
4471   }
4472 
4473   for (const VerNeed &VN : *V) {
4474     OS << format("  0x%04x: Version: %u  File: %s  Cnt: %u\n", VN.Offset,
4475                  VN.Version, VN.File.data(), VN.Cnt);
4476     for (const VernAux &Aux : VN.AuxV)
4477       OS << format("  0x%04x:   Name: %s  Flags: %s  Version: %u\n", Aux.Offset,
4478                    Aux.Name.data(), versionFlagToString(Aux.Flags).c_str(),
4479                    Aux.Other);
4480   }
4481   OS << '\n';
4482 }
4483 
4484 template <class ELFT>
4485 void GNUELFDumper<ELFT>::printHashHistogram(const Elf_Hash &HashTable) {
4486   size_t NBucket = HashTable.nbucket;
4487   size_t NChain = HashTable.nchain;
4488   ArrayRef<Elf_Word> Buckets = HashTable.buckets();
4489   ArrayRef<Elf_Word> Chains = HashTable.chains();
4490   size_t TotalSyms = 0;
4491   // If hash table is correct, we have at least chains with 0 length
4492   size_t MaxChain = 1;
4493   size_t CumulativeNonZero = 0;
4494 
4495   if (NChain == 0 || NBucket == 0)
4496     return;
4497 
4498   std::vector<size_t> ChainLen(NBucket, 0);
4499   // Go over all buckets and and note chain lengths of each bucket (total
4500   // unique chain lengths).
4501   for (size_t B = 0; B < NBucket; B++) {
4502     std::vector<bool> Visited(NChain);
4503     for (size_t C = Buckets[B]; C < NChain; C = Chains[C]) {
4504       if (C == ELF::STN_UNDEF)
4505         break;
4506       if (Visited[C]) {
4507         this->reportUniqueWarning(".hash section is invalid: bucket " +
4508                                   Twine(C) +
4509                                   ": a cycle was detected in the linked chain");
4510         break;
4511       }
4512       Visited[C] = true;
4513       if (MaxChain <= ++ChainLen[B])
4514         MaxChain++;
4515     }
4516     TotalSyms += ChainLen[B];
4517   }
4518 
4519   if (!TotalSyms)
4520     return;
4521 
4522   std::vector<size_t> Count(MaxChain, 0);
4523   // Count how long is the chain for each bucket
4524   for (size_t B = 0; B < NBucket; B++)
4525     ++Count[ChainLen[B]];
4526   // Print Number of buckets with each chain lengths and their cumulative
4527   // coverage of the symbols
4528   OS << "Histogram for bucket list length (total of " << NBucket
4529      << " buckets)\n"
4530      << " Length  Number     % of total  Coverage\n";
4531   for (size_t I = 0; I < MaxChain; I++) {
4532     CumulativeNonZero += Count[I] * I;
4533     OS << format("%7lu  %-10lu (%5.1f%%)     %5.1f%%\n", I, Count[I],
4534                  (Count[I] * 100.0) / NBucket,
4535                  (CumulativeNonZero * 100.0) / TotalSyms);
4536   }
4537 }
4538 
4539 template <class ELFT>
4540 void GNUELFDumper<ELFT>::printGnuHashHistogram(
4541     const Elf_GnuHash &GnuHashTable) {
4542   Expected<ArrayRef<Elf_Word>> ChainsOrErr =
4543       getGnuHashTableChains<ELFT>(this->DynSymRegion, &GnuHashTable);
4544   if (!ChainsOrErr) {
4545     this->reportUniqueWarning("unable to print the GNU hash table histogram: " +
4546                               toString(ChainsOrErr.takeError()));
4547     return;
4548   }
4549 
4550   ArrayRef<Elf_Word> Chains = *ChainsOrErr;
4551   size_t Symndx = GnuHashTable.symndx;
4552   size_t TotalSyms = 0;
4553   size_t MaxChain = 1;
4554   size_t CumulativeNonZero = 0;
4555 
4556   size_t NBucket = GnuHashTable.nbuckets;
4557   if (Chains.empty() || NBucket == 0)
4558     return;
4559 
4560   ArrayRef<Elf_Word> Buckets = GnuHashTable.buckets();
4561   std::vector<size_t> ChainLen(NBucket, 0);
4562   for (size_t B = 0; B < NBucket; B++) {
4563     if (!Buckets[B])
4564       continue;
4565     size_t Len = 1;
4566     for (size_t C = Buckets[B] - Symndx;
4567          C < Chains.size() && (Chains[C] & 1) == 0; C++)
4568       if (MaxChain < ++Len)
4569         MaxChain++;
4570     ChainLen[B] = Len;
4571     TotalSyms += Len;
4572   }
4573   MaxChain++;
4574 
4575   if (!TotalSyms)
4576     return;
4577 
4578   std::vector<size_t> Count(MaxChain, 0);
4579   for (size_t B = 0; B < NBucket; B++)
4580     ++Count[ChainLen[B]];
4581   // Print Number of buckets with each chain lengths and their cumulative
4582   // coverage of the symbols
4583   OS << "Histogram for `.gnu.hash' bucket list length (total of " << NBucket
4584      << " buckets)\n"
4585      << " Length  Number     % of total  Coverage\n";
4586   for (size_t I = 0; I < MaxChain; I++) {
4587     CumulativeNonZero += Count[I] * I;
4588     OS << format("%7lu  %-10lu (%5.1f%%)     %5.1f%%\n", I, Count[I],
4589                  (Count[I] * 100.0) / NBucket,
4590                  (CumulativeNonZero * 100.0) / TotalSyms);
4591   }
4592 }
4593 
4594 // Hash histogram shows statistics of how efficient the hash was for the
4595 // dynamic symbol table. The table shows the number of hash buckets for
4596 // different lengths of chains as an absolute number and percentage of the total
4597 // buckets, and the cumulative coverage of symbols for each set of buckets.
4598 template <class ELFT> void GNUELFDumper<ELFT>::printHashHistograms() {
4599   // Print histogram for the .hash section.
4600   if (this->HashTable) {
4601     if (Error E = checkHashTable<ELFT>(*this, this->HashTable))
4602       this->reportUniqueWarning(std::move(E));
4603     else
4604       printHashHistogram(*this->HashTable);
4605   }
4606 
4607   // Print histogram for the .gnu.hash section.
4608   if (this->GnuHashTable) {
4609     if (Error E = checkGNUHashTable<ELFT>(this->Obj, this->GnuHashTable))
4610       this->reportUniqueWarning(std::move(E));
4611     else
4612       printGnuHashHistogram(*this->GnuHashTable);
4613   }
4614 }
4615 
4616 template <class ELFT> void GNUELFDumper<ELFT>::printCGProfile() {
4617   OS << "GNUStyle::printCGProfile not implemented\n";
4618 }
4619 
4620 static Expected<std::vector<uint64_t>> toULEB128Array(ArrayRef<uint8_t> Data) {
4621   std::vector<uint64_t> Ret;
4622   const uint8_t *Cur = Data.begin();
4623   const uint8_t *End = Data.end();
4624   while (Cur != End) {
4625     unsigned Size;
4626     const char *Err;
4627     Ret.push_back(decodeULEB128(Cur, &Size, End, &Err));
4628     if (Err)
4629       return createError(Err);
4630     Cur += Size;
4631   }
4632   return Ret;
4633 }
4634 
4635 template <class ELFT>
4636 static Expected<std::vector<uint64_t>>
4637 decodeAddrsigSection(const ELFFile<ELFT> &Obj, const typename ELFT::Shdr &Sec) {
4638   Expected<ArrayRef<uint8_t>> ContentsOrErr = Obj.getSectionContents(Sec);
4639   if (!ContentsOrErr)
4640     return ContentsOrErr.takeError();
4641 
4642   if (Expected<std::vector<uint64_t>> SymsOrErr =
4643           toULEB128Array(*ContentsOrErr))
4644     return *SymsOrErr;
4645   else
4646     return createError("unable to decode " + describe(Obj, Sec) + ": " +
4647                        toString(SymsOrErr.takeError()));
4648 }
4649 
4650 template <class ELFT> void GNUELFDumper<ELFT>::printAddrsig() {
4651   if (!this->DotAddrsigSec)
4652     return;
4653 
4654   Expected<std::vector<uint64_t>> SymsOrErr =
4655       decodeAddrsigSection(this->Obj, *this->DotAddrsigSec);
4656   if (!SymsOrErr) {
4657     this->reportUniqueWarning(SymsOrErr.takeError());
4658     return;
4659   }
4660 
4661   StringRef Name = this->getPrintableSectionName(*this->DotAddrsigSec);
4662   OS << "\nAddress-significant symbols section '" << Name << "'"
4663      << " contains " << SymsOrErr->size() << " entries:\n";
4664   OS << "   Num: Name\n";
4665 
4666   Field Fields[2] = {0, 8};
4667   size_t SymIndex = 0;
4668   for (uint64_t Sym : *SymsOrErr) {
4669     Fields[0].Str = to_string(format_decimal(++SymIndex, 6)) + ":";
4670     Fields[1].Str = this->getStaticSymbolName(Sym);
4671     for (const Field &Entry : Fields)
4672       printField(Entry);
4673     OS << "\n";
4674   }
4675 }
4676 
4677 template <typename ELFT>
4678 static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
4679                                   ArrayRef<uint8_t> Data) {
4680   std::string str;
4681   raw_string_ostream OS(str);
4682   uint32_t PrData;
4683   auto DumpBit = [&](uint32_t Flag, StringRef Name) {
4684     if (PrData & Flag) {
4685       PrData &= ~Flag;
4686       OS << Name;
4687       if (PrData)
4688         OS << ", ";
4689     }
4690   };
4691 
4692   switch (Type) {
4693   default:
4694     OS << format("<application-specific type 0x%x>", Type);
4695     return OS.str();
4696   case GNU_PROPERTY_STACK_SIZE: {
4697     OS << "stack size: ";
4698     if (DataSize == sizeof(typename ELFT::uint))
4699       OS << formatv("{0:x}",
4700                     (uint64_t)(*(const typename ELFT::Addr *)Data.data()));
4701     else
4702       OS << format("<corrupt length: 0x%x>", DataSize);
4703     return OS.str();
4704   }
4705   case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
4706     OS << "no copy on protected";
4707     if (DataSize)
4708       OS << format(" <corrupt length: 0x%x>", DataSize);
4709     return OS.str();
4710   case GNU_PROPERTY_AARCH64_FEATURE_1_AND:
4711   case GNU_PROPERTY_X86_FEATURE_1_AND:
4712     OS << ((Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) ? "aarch64 feature: "
4713                                                         : "x86 feature: ");
4714     if (DataSize != 4) {
4715       OS << format("<corrupt length: 0x%x>", DataSize);
4716       return OS.str();
4717     }
4718     PrData = support::endian::read32<ELFT::TargetEndianness>(Data.data());
4719     if (PrData == 0) {
4720       OS << "<None>";
4721       return OS.str();
4722     }
4723     if (Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
4724       DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI");
4725       DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC");
4726     } else {
4727       DumpBit(GNU_PROPERTY_X86_FEATURE_1_IBT, "IBT");
4728       DumpBit(GNU_PROPERTY_X86_FEATURE_1_SHSTK, "SHSTK");
4729     }
4730     if (PrData)
4731       OS << format("<unknown flags: 0x%x>", PrData);
4732     return OS.str();
4733   case GNU_PROPERTY_X86_ISA_1_NEEDED:
4734   case GNU_PROPERTY_X86_ISA_1_USED:
4735     OS << "x86 ISA "
4736        << (Type == GNU_PROPERTY_X86_ISA_1_NEEDED ? "needed: " : "used: ");
4737     if (DataSize != 4) {
4738       OS << format("<corrupt length: 0x%x>", DataSize);
4739       return OS.str();
4740     }
4741     PrData = support::endian::read32<ELFT::TargetEndianness>(Data.data());
4742     if (PrData == 0) {
4743       OS << "<None>";
4744       return OS.str();
4745     }
4746     DumpBit(GNU_PROPERTY_X86_ISA_1_CMOV, "CMOV");
4747     DumpBit(GNU_PROPERTY_X86_ISA_1_SSE, "SSE");
4748     DumpBit(GNU_PROPERTY_X86_ISA_1_SSE2, "SSE2");
4749     DumpBit(GNU_PROPERTY_X86_ISA_1_SSE3, "SSE3");
4750     DumpBit(GNU_PROPERTY_X86_ISA_1_SSSE3, "SSSE3");
4751     DumpBit(GNU_PROPERTY_X86_ISA_1_SSE4_1, "SSE4_1");
4752     DumpBit(GNU_PROPERTY_X86_ISA_1_SSE4_2, "SSE4_2");
4753     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX, "AVX");
4754     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX2, "AVX2");
4755     DumpBit(GNU_PROPERTY_X86_ISA_1_FMA, "FMA");
4756     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512F, "AVX512F");
4757     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512CD, "AVX512CD");
4758     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512ER, "AVX512ER");
4759     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512PF, "AVX512PF");
4760     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512VL, "AVX512VL");
4761     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512DQ, "AVX512DQ");
4762     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512BW, "AVX512BW");
4763     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_4FMAPS, "AVX512_4FMAPS");
4764     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_4VNNIW, "AVX512_4VNNIW");
4765     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_BITALG, "AVX512_BITALG");
4766     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_IFMA, "AVX512_IFMA");
4767     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_VBMI, "AVX512_VBMI");
4768     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_VBMI2, "AVX512_VBMI2");
4769     DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_VNNI, "AVX512_VNNI");
4770     if (PrData)
4771       OS << format("<unknown flags: 0x%x>", PrData);
4772     return OS.str();
4773     break;
4774   case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
4775   case GNU_PROPERTY_X86_FEATURE_2_USED:
4776     OS << "x86 feature "
4777        << (Type == GNU_PROPERTY_X86_FEATURE_2_NEEDED ? "needed: " : "used: ");
4778     if (DataSize != 4) {
4779       OS << format("<corrupt length: 0x%x>", DataSize);
4780       return OS.str();
4781     }
4782     PrData = support::endian::read32<ELFT::TargetEndianness>(Data.data());
4783     if (PrData == 0) {
4784       OS << "<None>";
4785       return OS.str();
4786     }
4787     DumpBit(GNU_PROPERTY_X86_FEATURE_2_X86, "x86");
4788     DumpBit(GNU_PROPERTY_X86_FEATURE_2_X87, "x87");
4789     DumpBit(GNU_PROPERTY_X86_FEATURE_2_MMX, "MMX");
4790     DumpBit(GNU_PROPERTY_X86_FEATURE_2_XMM, "XMM");
4791     DumpBit(GNU_PROPERTY_X86_FEATURE_2_YMM, "YMM");
4792     DumpBit(GNU_PROPERTY_X86_FEATURE_2_ZMM, "ZMM");
4793     DumpBit(GNU_PROPERTY_X86_FEATURE_2_FXSR, "FXSR");
4794     DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVE, "XSAVE");
4795     DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT, "XSAVEOPT");
4796     DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVEC, "XSAVEC");
4797     if (PrData)
4798       OS << format("<unknown flags: 0x%x>", PrData);
4799     return OS.str();
4800   }
4801 }
4802 
4803 template <typename ELFT>
4804 static SmallVector<std::string, 4> getGNUPropertyList(ArrayRef<uint8_t> Arr) {
4805   using Elf_Word = typename ELFT::Word;
4806 
4807   SmallVector<std::string, 4> Properties;
4808   while (Arr.size() >= 8) {
4809     uint32_t Type = *reinterpret_cast<const Elf_Word *>(Arr.data());
4810     uint32_t DataSize = *reinterpret_cast<const Elf_Word *>(Arr.data() + 4);
4811     Arr = Arr.drop_front(8);
4812 
4813     // Take padding size into account if present.
4814     uint64_t PaddedSize = alignTo(DataSize, sizeof(typename ELFT::uint));
4815     std::string str;
4816     raw_string_ostream OS(str);
4817     if (Arr.size() < PaddedSize) {
4818       OS << format("<corrupt type (0x%x) datasz: 0x%x>", Type, DataSize);
4819       Properties.push_back(OS.str());
4820       break;
4821     }
4822     Properties.push_back(
4823         getGNUProperty<ELFT>(Type, DataSize, Arr.take_front(PaddedSize)));
4824     Arr = Arr.drop_front(PaddedSize);
4825   }
4826 
4827   if (!Arr.empty())
4828     Properties.push_back("<corrupted GNU_PROPERTY_TYPE_0>");
4829 
4830   return Properties;
4831 }
4832 
4833 struct GNUAbiTag {
4834   std::string OSName;
4835   std::string ABI;
4836   bool IsValid;
4837 };
4838 
4839 template <typename ELFT> static GNUAbiTag getGNUAbiTag(ArrayRef<uint8_t> Desc) {
4840   typedef typename ELFT::Word Elf_Word;
4841 
4842   ArrayRef<Elf_Word> Words(reinterpret_cast<const Elf_Word *>(Desc.begin()),
4843                            reinterpret_cast<const Elf_Word *>(Desc.end()));
4844 
4845   if (Words.size() < 4)
4846     return {"", "", /*IsValid=*/false};
4847 
4848   static const char *OSNames[] = {
4849       "Linux", "Hurd", "Solaris", "FreeBSD", "NetBSD", "Syllable", "NaCl",
4850   };
4851   StringRef OSName = "Unknown";
4852   if (Words[0] < array_lengthof(OSNames))
4853     OSName = OSNames[Words[0]];
4854   uint32_t Major = Words[1], Minor = Words[2], Patch = Words[3];
4855   std::string str;
4856   raw_string_ostream ABI(str);
4857   ABI << Major << "." << Minor << "." << Patch;
4858   return {std::string(OSName), ABI.str(), /*IsValid=*/true};
4859 }
4860 
4861 static std::string getGNUBuildId(ArrayRef<uint8_t> Desc) {
4862   std::string str;
4863   raw_string_ostream OS(str);
4864   for (uint8_t B : Desc)
4865     OS << format_hex_no_prefix(B, 2);
4866   return OS.str();
4867 }
4868 
4869 static StringRef getGNUGoldVersion(ArrayRef<uint8_t> Desc) {
4870   return StringRef(reinterpret_cast<const char *>(Desc.data()), Desc.size());
4871 }
4872 
4873 template <typename ELFT>
4874 static bool printGNUNote(raw_ostream &OS, uint32_t NoteType,
4875                          ArrayRef<uint8_t> Desc) {
4876   // Return true if we were able to pretty-print the note, false otherwise.
4877   switch (NoteType) {
4878   default:
4879     return false;
4880   case ELF::NT_GNU_ABI_TAG: {
4881     const GNUAbiTag &AbiTag = getGNUAbiTag<ELFT>(Desc);
4882     if (!AbiTag.IsValid)
4883       OS << "    <corrupt GNU_ABI_TAG>";
4884     else
4885       OS << "    OS: " << AbiTag.OSName << ", ABI: " << AbiTag.ABI;
4886     break;
4887   }
4888   case ELF::NT_GNU_BUILD_ID: {
4889     OS << "    Build ID: " << getGNUBuildId(Desc);
4890     break;
4891   }
4892   case ELF::NT_GNU_GOLD_VERSION:
4893     OS << "    Version: " << getGNUGoldVersion(Desc);
4894     break;
4895   case ELF::NT_GNU_PROPERTY_TYPE_0:
4896     OS << "    Properties:";
4897     for (const std::string &Property : getGNUPropertyList<ELFT>(Desc))
4898       OS << "    " << Property << "\n";
4899     break;
4900   }
4901   OS << '\n';
4902   return true;
4903 }
4904 
4905 static const EnumEntry<unsigned> FreeBSDFeatureCtlFlags[] = {
4906     {"ASLR_DISABLE", NT_FREEBSD_FCTL_ASLR_DISABLE},
4907     {"PROTMAX_DISABLE", NT_FREEBSD_FCTL_PROTMAX_DISABLE},
4908     {"STKGAP_DISABLE", NT_FREEBSD_FCTL_STKGAP_DISABLE},
4909     {"WXNEEDED", NT_FREEBSD_FCTL_WXNEEDED},
4910     {"LA48", NT_FREEBSD_FCTL_LA48},
4911     {"ASG_DISABLE", NT_FREEBSD_FCTL_ASG_DISABLE},
4912 };
4913 
4914 struct FreeBSDNote {
4915   std::string Type;
4916   std::string Value;
4917 };
4918 
4919 template <typename ELFT>
4920 static Optional<FreeBSDNote>
4921 getFreeBSDNote(uint32_t NoteType, ArrayRef<uint8_t> Desc, bool IsCore) {
4922   if (IsCore)
4923     return None; // No pretty-printing yet.
4924   switch (NoteType) {
4925   case ELF::NT_FREEBSD_ABI_TAG:
4926     if (Desc.size() != 4)
4927       return None;
4928     return FreeBSDNote{
4929         "ABI tag",
4930         utostr(support::endian::read32<ELFT::TargetEndianness>(Desc.data()))};
4931   case ELF::NT_FREEBSD_ARCH_TAG:
4932     return FreeBSDNote{"Arch tag", toStringRef(Desc).str()};
4933   case ELF::NT_FREEBSD_FEATURE_CTL: {
4934     if (Desc.size() != 4)
4935       return None;
4936     unsigned Value =
4937         support::endian::read32<ELFT::TargetEndianness>(Desc.data());
4938     std::string FlagsStr;
4939     raw_string_ostream OS(FlagsStr);
4940     printFlags(Value, makeArrayRef(FreeBSDFeatureCtlFlags), OS);
4941     if (OS.str().empty())
4942       OS << "0x" << utohexstr(Value);
4943     else
4944       OS << "(0x" << utohexstr(Value) << ")";
4945     return FreeBSDNote{"Feature flags", OS.str()};
4946   }
4947   default:
4948     return None;
4949   }
4950 }
4951 
4952 struct AMDNote {
4953   std::string Type;
4954   std::string Value;
4955 };
4956 
4957 template <typename ELFT>
4958 static AMDNote getAMDNote(uint32_t NoteType, ArrayRef<uint8_t> Desc) {
4959   switch (NoteType) {
4960   default:
4961     return {"", ""};
4962   case ELF::NT_AMD_AMDGPU_HSA_METADATA:
4963     return {
4964         "HSA Metadata",
4965         std::string(reinterpret_cast<const char *>(Desc.data()), Desc.size())};
4966   case ELF::NT_AMD_AMDGPU_ISA:
4967     return {
4968         "ISA Version",
4969         std::string(reinterpret_cast<const char *>(Desc.data()), Desc.size())};
4970   }
4971 }
4972 
4973 struct AMDGPUNote {
4974   std::string Type;
4975   std::string Value;
4976 };
4977 
4978 template <typename ELFT>
4979 static AMDGPUNote getAMDGPUNote(uint32_t NoteType, ArrayRef<uint8_t> Desc) {
4980   switch (NoteType) {
4981   default:
4982     return {"", ""};
4983   case ELF::NT_AMDGPU_METADATA: {
4984     StringRef MsgPackString =
4985         StringRef(reinterpret_cast<const char *>(Desc.data()), Desc.size());
4986     msgpack::Document MsgPackDoc;
4987     if (!MsgPackDoc.readFromBlob(MsgPackString, /*Multi=*/false))
4988       return {"", ""};
4989 
4990     AMDGPU::HSAMD::V3::MetadataVerifier Verifier(true);
4991     std::string HSAMetadataString;
4992     if (!Verifier.verify(MsgPackDoc.getRoot()))
4993       HSAMetadataString = "Invalid AMDGPU Metadata\n";
4994 
4995     raw_string_ostream StrOS(HSAMetadataString);
4996     if (MsgPackDoc.getRoot().isScalar()) {
4997       // TODO: passing a scalar root to toYAML() asserts:
4998       // (PolymorphicTraits<T>::getKind(Val) != NodeKind::Scalar &&
4999       //    "plain scalar documents are not supported")
5000       // To avoid this crash we print the raw data instead.
5001       return {"", ""};
5002     }
5003     MsgPackDoc.toYAML(StrOS);
5004     return {"AMDGPU Metadata", StrOS.str()};
5005   }
5006   }
5007 }
5008 
5009 struct CoreFileMapping {
5010   uint64_t Start, End, Offset;
5011   StringRef Filename;
5012 };
5013 
5014 struct CoreNote {
5015   uint64_t PageSize;
5016   std::vector<CoreFileMapping> Mappings;
5017 };
5018 
5019 static Expected<CoreNote> readCoreNote(DataExtractor Desc) {
5020   // Expected format of the NT_FILE note description:
5021   // 1. # of file mappings (call it N)
5022   // 2. Page size
5023   // 3. N (start, end, offset) triples
5024   // 4. N packed filenames (null delimited)
5025   // Each field is an Elf_Addr, except for filenames which are char* strings.
5026 
5027   CoreNote Ret;
5028   const int Bytes = Desc.getAddressSize();
5029 
5030   if (!Desc.isValidOffsetForAddress(2))
5031     return createError("the note of size 0x" + Twine::utohexstr(Desc.size()) +
5032                        " is too short, expected at least 0x" +
5033                        Twine::utohexstr(Bytes * 2));
5034   if (Desc.getData().back() != 0)
5035     return createError("the note is not NUL terminated");
5036 
5037   uint64_t DescOffset = 0;
5038   uint64_t FileCount = Desc.getAddress(&DescOffset);
5039   Ret.PageSize = Desc.getAddress(&DescOffset);
5040 
5041   if (!Desc.isValidOffsetForAddress(3 * FileCount * Bytes))
5042     return createError("unable to read file mappings (found " +
5043                        Twine(FileCount) + "): the note of size 0x" +
5044                        Twine::utohexstr(Desc.size()) + " is too short");
5045 
5046   uint64_t FilenamesOffset = 0;
5047   DataExtractor Filenames(
5048       Desc.getData().drop_front(DescOffset + 3 * FileCount * Bytes),
5049       Desc.isLittleEndian(), Desc.getAddressSize());
5050 
5051   Ret.Mappings.resize(FileCount);
5052   size_t I = 0;
5053   for (CoreFileMapping &Mapping : Ret.Mappings) {
5054     ++I;
5055     if (!Filenames.isValidOffsetForDataOfSize(FilenamesOffset, 1))
5056       return createError(
5057           "unable to read the file name for the mapping with index " +
5058           Twine(I) + ": the note of size 0x" + Twine::utohexstr(Desc.size()) +
5059           " is truncated");
5060     Mapping.Start = Desc.getAddress(&DescOffset);
5061     Mapping.End = Desc.getAddress(&DescOffset);
5062     Mapping.Offset = Desc.getAddress(&DescOffset);
5063     Mapping.Filename = Filenames.getCStrRef(&FilenamesOffset);
5064   }
5065 
5066   return Ret;
5067 }
5068 
5069 template <typename ELFT>
5070 static void printCoreNote(raw_ostream &OS, const CoreNote &Note) {
5071   // Length of "0x<address>" string.
5072   const int FieldWidth = ELFT::Is64Bits ? 18 : 10;
5073 
5074   OS << "    Page size: " << format_decimal(Note.PageSize, 0) << '\n';
5075   OS << "    " << right_justify("Start", FieldWidth) << "  "
5076      << right_justify("End", FieldWidth) << "  "
5077      << right_justify("Page Offset", FieldWidth) << '\n';
5078   for (const CoreFileMapping &Mapping : Note.Mappings) {
5079     OS << "    " << format_hex(Mapping.Start, FieldWidth) << "  "
5080        << format_hex(Mapping.End, FieldWidth) << "  "
5081        << format_hex(Mapping.Offset, FieldWidth) << "\n        "
5082        << Mapping.Filename << '\n';
5083   }
5084 }
5085 
5086 static const NoteType GenericNoteTypes[] = {
5087     {ELF::NT_VERSION, "NT_VERSION (version)"},
5088     {ELF::NT_ARCH, "NT_ARCH (architecture)"},
5089     {ELF::NT_GNU_BUILD_ATTRIBUTE_OPEN, "OPEN"},
5090     {ELF::NT_GNU_BUILD_ATTRIBUTE_FUNC, "func"},
5091 };
5092 
5093 static const NoteType GNUNoteTypes[] = {
5094     {ELF::NT_GNU_ABI_TAG, "NT_GNU_ABI_TAG (ABI version tag)"},
5095     {ELF::NT_GNU_HWCAP, "NT_GNU_HWCAP (DSO-supplied software HWCAP info)"},
5096     {ELF::NT_GNU_BUILD_ID, "NT_GNU_BUILD_ID (unique build ID bitstring)"},
5097     {ELF::NT_GNU_GOLD_VERSION, "NT_GNU_GOLD_VERSION (gold version)"},
5098     {ELF::NT_GNU_PROPERTY_TYPE_0, "NT_GNU_PROPERTY_TYPE_0 (property note)"},
5099 };
5100 
5101 static const NoteType FreeBSDCoreNoteTypes[] = {
5102     {ELF::NT_FREEBSD_THRMISC, "NT_THRMISC (thrmisc structure)"},
5103     {ELF::NT_FREEBSD_PROCSTAT_PROC, "NT_PROCSTAT_PROC (proc data)"},
5104     {ELF::NT_FREEBSD_PROCSTAT_FILES, "NT_PROCSTAT_FILES (files data)"},
5105     {ELF::NT_FREEBSD_PROCSTAT_VMMAP, "NT_PROCSTAT_VMMAP (vmmap data)"},
5106     {ELF::NT_FREEBSD_PROCSTAT_GROUPS, "NT_PROCSTAT_GROUPS (groups data)"},
5107     {ELF::NT_FREEBSD_PROCSTAT_UMASK, "NT_PROCSTAT_UMASK (umask data)"},
5108     {ELF::NT_FREEBSD_PROCSTAT_RLIMIT, "NT_PROCSTAT_RLIMIT (rlimit data)"},
5109     {ELF::NT_FREEBSD_PROCSTAT_OSREL, "NT_PROCSTAT_OSREL (osreldate data)"},
5110     {ELF::NT_FREEBSD_PROCSTAT_PSSTRINGS,
5111      "NT_PROCSTAT_PSSTRINGS (ps_strings data)"},
5112     {ELF::NT_FREEBSD_PROCSTAT_AUXV, "NT_PROCSTAT_AUXV (auxv data)"},
5113 };
5114 
5115 static const NoteType FreeBSDNoteTypes[] = {
5116     {ELF::NT_FREEBSD_ABI_TAG, "NT_FREEBSD_ABI_TAG (ABI version tag)"},
5117     {ELF::NT_FREEBSD_NOINIT_TAG, "NT_FREEBSD_NOINIT_TAG (no .init tag)"},
5118     {ELF::NT_FREEBSD_ARCH_TAG, "NT_FREEBSD_ARCH_TAG (architecture tag)"},
5119     {ELF::NT_FREEBSD_FEATURE_CTL,
5120      "NT_FREEBSD_FEATURE_CTL (FreeBSD feature control)"},
5121 };
5122 
5123 static const NoteType AMDNoteTypes[] = {
5124     {ELF::NT_AMD_AMDGPU_HSA_METADATA,
5125      "NT_AMD_AMDGPU_HSA_METADATA (HSA Metadata)"},
5126     {ELF::NT_AMD_AMDGPU_ISA, "NT_AMD_AMDGPU_ISA (ISA Version)"},
5127     {ELF::NT_AMD_AMDGPU_PAL_METADATA,
5128      "NT_AMD_AMDGPU_PAL_METADATA (PAL Metadata)"},
5129 };
5130 
5131 static const NoteType AMDGPUNoteTypes[] = {
5132     {ELF::NT_AMDGPU_METADATA, "NT_AMDGPU_METADATA (AMDGPU Metadata)"},
5133 };
5134 
5135 static const NoteType CoreNoteTypes[] = {
5136     {ELF::NT_PRSTATUS, "NT_PRSTATUS (prstatus structure)"},
5137     {ELF::NT_FPREGSET, "NT_FPREGSET (floating point registers)"},
5138     {ELF::NT_PRPSINFO, "NT_PRPSINFO (prpsinfo structure)"},
5139     {ELF::NT_TASKSTRUCT, "NT_TASKSTRUCT (task structure)"},
5140     {ELF::NT_AUXV, "NT_AUXV (auxiliary vector)"},
5141     {ELF::NT_PSTATUS, "NT_PSTATUS (pstatus structure)"},
5142     {ELF::NT_FPREGS, "NT_FPREGS (floating point registers)"},
5143     {ELF::NT_PSINFO, "NT_PSINFO (psinfo structure)"},
5144     {ELF::NT_LWPSTATUS, "NT_LWPSTATUS (lwpstatus_t structure)"},
5145     {ELF::NT_LWPSINFO, "NT_LWPSINFO (lwpsinfo_t structure)"},
5146     {ELF::NT_WIN32PSTATUS, "NT_WIN32PSTATUS (win32_pstatus structure)"},
5147 
5148     {ELF::NT_PPC_VMX, "NT_PPC_VMX (ppc Altivec registers)"},
5149     {ELF::NT_PPC_VSX, "NT_PPC_VSX (ppc VSX registers)"},
5150     {ELF::NT_PPC_TAR, "NT_PPC_TAR (ppc TAR register)"},
5151     {ELF::NT_PPC_PPR, "NT_PPC_PPR (ppc PPR register)"},
5152     {ELF::NT_PPC_DSCR, "NT_PPC_DSCR (ppc DSCR register)"},
5153     {ELF::NT_PPC_EBB, "NT_PPC_EBB (ppc EBB registers)"},
5154     {ELF::NT_PPC_PMU, "NT_PPC_PMU (ppc PMU registers)"},
5155     {ELF::NT_PPC_TM_CGPR, "NT_PPC_TM_CGPR (ppc checkpointed GPR registers)"},
5156     {ELF::NT_PPC_TM_CFPR,
5157      "NT_PPC_TM_CFPR (ppc checkpointed floating point registers)"},
5158     {ELF::NT_PPC_TM_CVMX,
5159      "NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)"},
5160     {ELF::NT_PPC_TM_CVSX, "NT_PPC_TM_CVSX (ppc checkpointed VSX registers)"},
5161     {ELF::NT_PPC_TM_SPR, "NT_PPC_TM_SPR (ppc TM special purpose registers)"},
5162     {ELF::NT_PPC_TM_CTAR, "NT_PPC_TM_CTAR (ppc checkpointed TAR register)"},
5163     {ELF::NT_PPC_TM_CPPR, "NT_PPC_TM_CPPR (ppc checkpointed PPR register)"},
5164     {ELF::NT_PPC_TM_CDSCR, "NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)"},
5165 
5166     {ELF::NT_386_TLS, "NT_386_TLS (x86 TLS information)"},
5167     {ELF::NT_386_IOPERM, "NT_386_IOPERM (x86 I/O permissions)"},
5168     {ELF::NT_X86_XSTATE, "NT_X86_XSTATE (x86 XSAVE extended state)"},
5169 
5170     {ELF::NT_S390_HIGH_GPRS, "NT_S390_HIGH_GPRS (s390 upper register halves)"},
5171     {ELF::NT_S390_TIMER, "NT_S390_TIMER (s390 timer register)"},
5172     {ELF::NT_S390_TODCMP, "NT_S390_TODCMP (s390 TOD comparator register)"},
5173     {ELF::NT_S390_TODPREG, "NT_S390_TODPREG (s390 TOD programmable register)"},
5174     {ELF::NT_S390_CTRS, "NT_S390_CTRS (s390 control registers)"},
5175     {ELF::NT_S390_PREFIX, "NT_S390_PREFIX (s390 prefix register)"},
5176     {ELF::NT_S390_LAST_BREAK,
5177      "NT_S390_LAST_BREAK (s390 last breaking event address)"},
5178     {ELF::NT_S390_SYSTEM_CALL,
5179      "NT_S390_SYSTEM_CALL (s390 system call restart data)"},
5180     {ELF::NT_S390_TDB, "NT_S390_TDB (s390 transaction diagnostic block)"},
5181     {ELF::NT_S390_VXRS_LOW,
5182      "NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)"},
5183     {ELF::NT_S390_VXRS_HIGH, "NT_S390_VXRS_HIGH (s390 vector registers 16-31)"},
5184     {ELF::NT_S390_GS_CB, "NT_S390_GS_CB (s390 guarded-storage registers)"},
5185     {ELF::NT_S390_GS_BC,
5186      "NT_S390_GS_BC (s390 guarded-storage broadcast control)"},
5187 
5188     {ELF::NT_ARM_VFP, "NT_ARM_VFP (arm VFP registers)"},
5189     {ELF::NT_ARM_TLS, "NT_ARM_TLS (AArch TLS registers)"},
5190     {ELF::NT_ARM_HW_BREAK,
5191      "NT_ARM_HW_BREAK (AArch hardware breakpoint registers)"},
5192     {ELF::NT_ARM_HW_WATCH,
5193      "NT_ARM_HW_WATCH (AArch hardware watchpoint registers)"},
5194 
5195     {ELF::NT_FILE, "NT_FILE (mapped files)"},
5196     {ELF::NT_PRXFPREG, "NT_PRXFPREG (user_xfpregs structure)"},
5197     {ELF::NT_SIGINFO, "NT_SIGINFO (siginfo_t data)"},
5198 };
5199 
5200 template <class ELFT>
5201 StringRef getNoteTypeName(const typename ELFT::Note &Note, unsigned ELFType) {
5202   uint32_t Type = Note.getType();
5203   auto FindNote = [&](ArrayRef<NoteType> V) -> StringRef {
5204     for (const NoteType &N : V)
5205       if (N.ID == Type)
5206         return N.Name;
5207     return "";
5208   };
5209 
5210   StringRef Name = Note.getName();
5211   if (Name == "GNU")
5212     return FindNote(GNUNoteTypes);
5213   if (Name == "FreeBSD") {
5214     if (ELFType == ELF::ET_CORE) {
5215       // FreeBSD also places the generic core notes in the FreeBSD namespace.
5216       StringRef Result = FindNote(FreeBSDCoreNoteTypes);
5217       if (!Result.empty())
5218         return Result;
5219       return FindNote(CoreNoteTypes);
5220     } else {
5221       return FindNote(FreeBSDNoteTypes);
5222     }
5223   }
5224   if (Name == "AMD")
5225     return FindNote(AMDNoteTypes);
5226   if (Name == "AMDGPU")
5227     return FindNote(AMDGPUNoteTypes);
5228 
5229   if (ELFType == ELF::ET_CORE)
5230     return FindNote(CoreNoteTypes);
5231   return FindNote(GenericNoteTypes);
5232 }
5233 
5234 template <class ELFT>
5235 static void printNotesHelper(
5236     const ELFDumper<ELFT> &Dumper,
5237     llvm::function_ref<void(Optional<StringRef>, typename ELFT::Off,
5238                             typename ELFT::Addr)>
5239         StartNotesFn,
5240     llvm::function_ref<Error(const typename ELFT::Note &, bool)> ProcessNoteFn,
5241     llvm::function_ref<void()> FinishNotesFn) {
5242   const ELFFile<ELFT> &Obj = Dumper.getElfObject().getELFFile();
5243   bool IsCoreFile = Obj.getHeader().e_type == ELF::ET_CORE;
5244 
5245   ArrayRef<typename ELFT::Shdr> Sections = cantFail(Obj.sections());
5246   if (!IsCoreFile && !Sections.empty()) {
5247     for (const typename ELFT::Shdr &S : Sections) {
5248       if (S.sh_type != SHT_NOTE)
5249         continue;
5250       StartNotesFn(expectedToOptional(Obj.getSectionName(S)), S.sh_offset,
5251                    S.sh_size);
5252       Error Err = Error::success();
5253       size_t I = 0;
5254       for (const typename ELFT::Note Note : Obj.notes(S, Err)) {
5255         if (Error E = ProcessNoteFn(Note, IsCoreFile))
5256           Dumper.reportUniqueWarning(
5257               "unable to read note with index " + Twine(I) + " from the " +
5258               describe(Obj, S) + ": " + toString(std::move(E)));
5259         ++I;
5260       }
5261       if (Err)
5262         Dumper.reportUniqueWarning("unable to read notes from the " +
5263                                    describe(Obj, S) + ": " +
5264                                    toString(std::move(Err)));
5265       FinishNotesFn();
5266     }
5267     return;
5268   }
5269 
5270   Expected<ArrayRef<typename ELFT::Phdr>> PhdrsOrErr = Obj.program_headers();
5271   if (!PhdrsOrErr) {
5272     Dumper.reportUniqueWarning(
5273         "unable to read program headers to locate the PT_NOTE segment: " +
5274         toString(PhdrsOrErr.takeError()));
5275     return;
5276   }
5277 
5278   for (size_t I = 0, E = (*PhdrsOrErr).size(); I != E; ++I) {
5279     const typename ELFT::Phdr &P = (*PhdrsOrErr)[I];
5280     if (P.p_type != PT_NOTE)
5281       continue;
5282     StartNotesFn(/*SecName=*/None, P.p_offset, P.p_filesz);
5283     Error Err = Error::success();
5284     size_t Index = 0;
5285     for (const typename ELFT::Note Note : Obj.notes(P, Err)) {
5286       if (Error E = ProcessNoteFn(Note, IsCoreFile))
5287         Dumper.reportUniqueWarning("unable to read note with index " +
5288                                    Twine(Index) +
5289                                    " from the PT_NOTE segment with index " +
5290                                    Twine(I) + ": " + toString(std::move(E)));
5291       ++Index;
5292     }
5293     if (Err)
5294       Dumper.reportUniqueWarning(
5295           "unable to read notes from the PT_NOTE segment with index " +
5296           Twine(I) + ": " + toString(std::move(Err)));
5297     FinishNotesFn();
5298   }
5299 }
5300 
5301 template <class ELFT> void GNUELFDumper<ELFT>::printNotes() {
5302   bool IsFirstHeader = true;
5303   auto PrintHeader = [&](Optional<StringRef> SecName,
5304                          const typename ELFT::Off Offset,
5305                          const typename ELFT::Addr Size) {
5306     // Print a newline between notes sections to match GNU readelf.
5307     if (!IsFirstHeader) {
5308       OS << '\n';
5309     } else {
5310       IsFirstHeader = false;
5311     }
5312 
5313     OS << "Displaying notes found ";
5314 
5315     if (SecName)
5316       OS << "in: " << *SecName << "\n";
5317     else
5318       OS << "at file offset " << format_hex(Offset, 10) << " with length "
5319          << format_hex(Size, 10) << ":\n";
5320 
5321     OS << "  Owner                Data size \tDescription\n";
5322   };
5323 
5324   auto ProcessNote = [&](const Elf_Note &Note, bool IsCore) -> Error {
5325     StringRef Name = Note.getName();
5326     ArrayRef<uint8_t> Descriptor = Note.getDesc();
5327     Elf_Word Type = Note.getType();
5328 
5329     // Print the note owner/type.
5330     OS << "  " << left_justify(Name, 20) << ' '
5331        << format_hex(Descriptor.size(), 10) << '\t';
5332 
5333     StringRef NoteType =
5334         getNoteTypeName<ELFT>(Note, this->Obj.getHeader().e_type);
5335     if (!NoteType.empty())
5336       OS << NoteType << '\n';
5337     else
5338       OS << "Unknown note type: (" << format_hex(Type, 10) << ")\n";
5339 
5340     // Print the description, or fallback to printing raw bytes for unknown
5341     // owners/if we fail to pretty-print the contents.
5342     if (Name == "GNU") {
5343       if (printGNUNote<ELFT>(OS, Type, Descriptor))
5344         return Error::success();
5345     } else if (Name == "FreeBSD") {
5346       if (Optional<FreeBSDNote> N =
5347               getFreeBSDNote<ELFT>(Type, Descriptor, IsCore)) {
5348         OS << "    " << N->Type << ": " << N->Value << '\n';
5349         return Error::success();
5350       }
5351     } else if (Name == "AMD") {
5352       const AMDNote N = getAMDNote<ELFT>(Type, Descriptor);
5353       if (!N.Type.empty()) {
5354         OS << "    " << N.Type << ":\n        " << N.Value << '\n';
5355         return Error::success();
5356       }
5357     } else if (Name == "AMDGPU") {
5358       const AMDGPUNote N = getAMDGPUNote<ELFT>(Type, Descriptor);
5359       if (!N.Type.empty()) {
5360         OS << "    " << N.Type << ":\n        " << N.Value << '\n';
5361         return Error::success();
5362       }
5363     } else if (Name == "CORE") {
5364       if (Type == ELF::NT_FILE) {
5365         DataExtractor DescExtractor(Descriptor,
5366                                     ELFT::TargetEndianness == support::little,
5367                                     sizeof(Elf_Addr));
5368         if (Expected<CoreNote> NoteOrErr = readCoreNote(DescExtractor)) {
5369           printCoreNote<ELFT>(OS, *NoteOrErr);
5370           return Error::success();
5371         } else {
5372           return NoteOrErr.takeError();
5373         }
5374       }
5375     }
5376     if (!Descriptor.empty()) {
5377       OS << "   description data:";
5378       for (uint8_t B : Descriptor)
5379         OS << " " << format("%02x", B);
5380       OS << '\n';
5381     }
5382     return Error::success();
5383   };
5384 
5385   printNotesHelper(*this, PrintHeader, ProcessNote, []() {});
5386 }
5387 
5388 template <class ELFT> void GNUELFDumper<ELFT>::printELFLinkerOptions() {
5389   OS << "printELFLinkerOptions not implemented!\n";
5390 }
5391 
5392 template <class ELFT>
5393 void ELFDumper<ELFT>::printDependentLibsHelper(
5394     function_ref<void(const Elf_Shdr &)> OnSectionStart,
5395     function_ref<void(StringRef, uint64_t)> OnLibEntry) {
5396   auto Warn = [this](unsigned SecNdx, StringRef Msg) {
5397     this->reportUniqueWarning("SHT_LLVM_DEPENDENT_LIBRARIES section at index " +
5398                               Twine(SecNdx) + " is broken: " + Msg);
5399   };
5400 
5401   unsigned I = -1;
5402   for (const Elf_Shdr &Shdr : cantFail(Obj.sections())) {
5403     ++I;
5404     if (Shdr.sh_type != ELF::SHT_LLVM_DEPENDENT_LIBRARIES)
5405       continue;
5406 
5407     OnSectionStart(Shdr);
5408 
5409     Expected<ArrayRef<uint8_t>> ContentsOrErr = Obj.getSectionContents(Shdr);
5410     if (!ContentsOrErr) {
5411       Warn(I, toString(ContentsOrErr.takeError()));
5412       continue;
5413     }
5414 
5415     ArrayRef<uint8_t> Contents = *ContentsOrErr;
5416     if (!Contents.empty() && Contents.back() != 0) {
5417       Warn(I, "the content is not null-terminated");
5418       continue;
5419     }
5420 
5421     for (const uint8_t *I = Contents.begin(), *E = Contents.end(); I < E;) {
5422       StringRef Lib((const char *)I);
5423       OnLibEntry(Lib, I - Contents.begin());
5424       I += Lib.size() + 1;
5425     }
5426   }
5427 }
5428 
5429 template <class ELFT>
5430 void ELFDumper<ELFT>::forEachRelocationDo(
5431     const Elf_Shdr &Sec, bool RawRelr,
5432     llvm::function_ref<void(const Relocation<ELFT> &, unsigned,
5433                             const Elf_Shdr &, const Elf_Shdr *)>
5434         RelRelaFn,
5435     llvm::function_ref<void(const Elf_Relr &)> RelrFn) {
5436   auto Warn = [&](Error &&E,
5437                   const Twine &Prefix = "unable to read relocations from") {
5438     this->reportUniqueWarning(Prefix + " " + describe(Sec) + ": " +
5439                               toString(std::move(E)));
5440   };
5441 
5442   // SHT_RELR/SHT_ANDROID_RELR sections do not have an associated symbol table.
5443   // For them we should not treat the value of the sh_link field as an index of
5444   // a symbol table.
5445   const Elf_Shdr *SymTab;
5446   if (Sec.sh_type != ELF::SHT_RELR && Sec.sh_type != ELF::SHT_ANDROID_RELR) {
5447     Expected<const Elf_Shdr *> SymTabOrErr = Obj.getSection(Sec.sh_link);
5448     if (!SymTabOrErr) {
5449       Warn(SymTabOrErr.takeError(), "unable to locate a symbol table for");
5450       return;
5451     }
5452     SymTab = *SymTabOrErr;
5453   }
5454 
5455   unsigned RelNdx = 0;
5456   const bool IsMips64EL = this->Obj.isMips64EL();
5457   switch (Sec.sh_type) {
5458   case ELF::SHT_REL:
5459     if (Expected<Elf_Rel_Range> RangeOrErr = Obj.rels(Sec)) {
5460       for (const Elf_Rel &R : *RangeOrErr)
5461         RelRelaFn(Relocation<ELFT>(R, IsMips64EL), RelNdx++, Sec, SymTab);
5462     } else {
5463       Warn(RangeOrErr.takeError());
5464     }
5465     break;
5466   case ELF::SHT_RELA:
5467     if (Expected<Elf_Rela_Range> RangeOrErr = Obj.relas(Sec)) {
5468       for (const Elf_Rela &R : *RangeOrErr)
5469         RelRelaFn(Relocation<ELFT>(R, IsMips64EL), RelNdx++, Sec, SymTab);
5470     } else {
5471       Warn(RangeOrErr.takeError());
5472     }
5473     break;
5474   case ELF::SHT_RELR:
5475   case ELF::SHT_ANDROID_RELR: {
5476     Expected<Elf_Relr_Range> RangeOrErr = Obj.relrs(Sec);
5477     if (!RangeOrErr) {
5478       Warn(RangeOrErr.takeError());
5479       break;
5480     }
5481     if (RawRelr) {
5482       for (const Elf_Relr &R : *RangeOrErr)
5483         RelrFn(R);
5484       break;
5485     }
5486 
5487     for (const Elf_Rel &R : Obj.decode_relrs(*RangeOrErr))
5488       RelRelaFn(Relocation<ELFT>(R, IsMips64EL), RelNdx++, Sec,
5489                 /*SymTab=*/nullptr);
5490     break;
5491   }
5492   case ELF::SHT_ANDROID_REL:
5493   case ELF::SHT_ANDROID_RELA:
5494     if (Expected<std::vector<Elf_Rela>> RelasOrErr = Obj.android_relas(Sec)) {
5495       for (const Elf_Rela &R : *RelasOrErr)
5496         RelRelaFn(Relocation<ELFT>(R, IsMips64EL), RelNdx++, Sec, SymTab);
5497     } else {
5498       Warn(RelasOrErr.takeError());
5499     }
5500     break;
5501   }
5502 }
5503 
5504 template <class ELFT>
5505 StringRef ELFDumper<ELFT>::getPrintableSectionName(const Elf_Shdr &Sec) const {
5506   StringRef Name = "<?>";
5507   if (Expected<StringRef> SecNameOrErr =
5508           Obj.getSectionName(Sec, this->WarningHandler))
5509     Name = *SecNameOrErr;
5510   else
5511     this->reportUniqueWarning("unable to get the name of " + describe(Sec) +
5512                               ": " + toString(SecNameOrErr.takeError()));
5513   return Name;
5514 }
5515 
5516 template <class ELFT> void GNUELFDumper<ELFT>::printDependentLibs() {
5517   bool SectionStarted = false;
5518   struct NameOffset {
5519     StringRef Name;
5520     uint64_t Offset;
5521   };
5522   std::vector<NameOffset> SecEntries;
5523   NameOffset Current;
5524   auto PrintSection = [&]() {
5525     OS << "Dependent libraries section " << Current.Name << " at offset "
5526        << format_hex(Current.Offset, 1) << " contains " << SecEntries.size()
5527        << " entries:\n";
5528     for (NameOffset Entry : SecEntries)
5529       OS << "  [" << format("%6" PRIx64, Entry.Offset) << "]  " << Entry.Name
5530          << "\n";
5531     OS << "\n";
5532     SecEntries.clear();
5533   };
5534 
5535   auto OnSectionStart = [&](const Elf_Shdr &Shdr) {
5536     if (SectionStarted)
5537       PrintSection();
5538     SectionStarted = true;
5539     Current.Offset = Shdr.sh_offset;
5540     Current.Name = this->getPrintableSectionName(Shdr);
5541   };
5542   auto OnLibEntry = [&](StringRef Lib, uint64_t Offset) {
5543     SecEntries.push_back(NameOffset{Lib, Offset});
5544   };
5545 
5546   this->printDependentLibsHelper(OnSectionStart, OnLibEntry);
5547   if (SectionStarted)
5548     PrintSection();
5549 }
5550 
5551 template <class ELFT>
5552 bool ELFDumper<ELFT>::printFunctionStackSize(
5553     uint64_t SymValue, Optional<const Elf_Shdr *> FunctionSec,
5554     const Elf_Shdr &StackSizeSec, DataExtractor Data, uint64_t *Offset) {
5555   uint32_t FuncSymIndex = 0;
5556   if (this->DotSymtabSec) {
5557     if (Expected<Elf_Sym_Range> SymsOrError = Obj.symbols(this->DotSymtabSec)) {
5558       uint32_t Index = (uint32_t)-1;
5559       for (const Elf_Sym &Sym : *SymsOrError) {
5560         ++Index;
5561 
5562         if (Sym.st_shndx == ELF::SHN_UNDEF || Sym.getType() != ELF::STT_FUNC)
5563           continue;
5564 
5565         if (Expected<uint64_t> SymAddrOrErr =
5566                 ObjF.toSymbolRef(this->DotSymtabSec, Index).getAddress()) {
5567           if (SymValue != *SymAddrOrErr)
5568             continue;
5569         } else {
5570           std::string Name = this->getStaticSymbolName(Index);
5571           reportUniqueWarning("unable to get address of symbol '" + Name +
5572                               "': " + toString(SymAddrOrErr.takeError()));
5573           break;
5574         }
5575 
5576         // Check if the symbol is in the right section. FunctionSec == None
5577         // means "any section".
5578         if (FunctionSec) {
5579           if (Expected<const Elf_Shdr *> SecOrErr =
5580                   Obj.getSection(Sym, this->DotSymtabSec,
5581                                  this->getShndxTable(this->DotSymtabSec))) {
5582             if (*FunctionSec != *SecOrErr)
5583               continue;
5584           } else {
5585             std::string Name = this->getStaticSymbolName(Index);
5586             // Note: it is impossible to trigger this error currently, it is
5587             // untested.
5588             reportUniqueWarning("unable to get section of symbol '" + Name +
5589                                 "': " + toString(SecOrErr.takeError()));
5590             break;
5591           }
5592         }
5593 
5594         FuncSymIndex = Index;
5595         break;
5596       }
5597     } else {
5598       reportUniqueWarning("unable to read the symbol table: " +
5599                           toString(SymsOrError.takeError()));
5600     }
5601   }
5602 
5603   std::string FuncName = "?";
5604   if (!FuncSymIndex)
5605     reportUniqueWarning(
5606         "could not identify function symbol for stack size entry in " +
5607         describe(StackSizeSec));
5608   else
5609     FuncName = this->getStaticSymbolName(FuncSymIndex);
5610 
5611   // Extract the size. The expectation is that Offset is pointing to the right
5612   // place, i.e. past the function address.
5613   Error Err = Error::success();
5614   uint64_t StackSize = Data.getULEB128(Offset, &Err);
5615   if (Err) {
5616     reportUniqueWarning("could not extract a valid stack size from " +
5617                         describe(StackSizeSec) + ": " +
5618                         toString(std::move(Err)));
5619     return false;
5620   }
5621   printStackSizeEntry(StackSize, FuncName);
5622   return true;
5623 }
5624 
5625 template <class ELFT>
5626 void GNUELFDumper<ELFT>::printStackSizeEntry(uint64_t Size,
5627                                              StringRef FuncName) {
5628   OS.PadToColumn(2);
5629   OS << format_decimal(Size, 11);
5630   OS.PadToColumn(18);
5631   OS << FuncName << "\n";
5632 }
5633 
5634 template <class ELFT>
5635 void ELFDumper<ELFT>::printStackSize(const Relocation<ELFT> &R,
5636                                      const Elf_Shdr &RelocSec, unsigned Ndx,
5637                                      const Elf_Shdr *SymTab,
5638                                      const Elf_Shdr *FunctionSec,
5639                                      const Elf_Shdr &StackSizeSec,
5640                                      const RelocationResolver &Resolver,
5641                                      DataExtractor Data) {
5642   // This function ignores potentially erroneous input, unless it is directly
5643   // related to stack size reporting.
5644   const Elf_Sym *Sym = nullptr;
5645   Expected<RelSymbol<ELFT>> TargetOrErr = this->getRelocationTarget(R, SymTab);
5646   if (!TargetOrErr)
5647     reportUniqueWarning("unable to get the target of relocation with index " +
5648                         Twine(Ndx) + " in " + describe(RelocSec) + ": " +
5649                         toString(TargetOrErr.takeError()));
5650   else
5651     Sym = TargetOrErr->Sym;
5652 
5653   uint64_t RelocSymValue = 0;
5654   if (Sym) {
5655     Expected<const Elf_Shdr *> SectionOrErr =
5656         this->Obj.getSection(*Sym, SymTab, this->getShndxTable(SymTab));
5657     if (!SectionOrErr) {
5658       reportUniqueWarning(
5659           "cannot identify the section for relocation symbol '" +
5660           (*TargetOrErr).Name + "': " + toString(SectionOrErr.takeError()));
5661     } else if (*SectionOrErr != FunctionSec) {
5662       reportUniqueWarning("relocation symbol '" + (*TargetOrErr).Name +
5663                           "' is not in the expected section");
5664       // Pretend that the symbol is in the correct section and report its
5665       // stack size anyway.
5666       FunctionSec = *SectionOrErr;
5667     }
5668 
5669     RelocSymValue = Sym->st_value;
5670   }
5671 
5672   uint64_t Offset = R.Offset;
5673   if (!Data.isValidOffsetForDataOfSize(Offset, sizeof(Elf_Addr) + 1)) {
5674     reportUniqueWarning("found invalid relocation offset (0x" +
5675                         Twine::utohexstr(Offset) + ") into " +
5676                         describe(StackSizeSec) +
5677                         " while trying to extract a stack size entry");
5678     return;
5679   }
5680 
5681   uint64_t SymValue =
5682       Resolver(R.Type, Offset, RelocSymValue, Data.getAddress(&Offset),
5683                R.Addend.getValueOr(0));
5684   this->printFunctionStackSize(SymValue, FunctionSec, StackSizeSec, Data,
5685                                &Offset);
5686 }
5687 
5688 template <class ELFT>
5689 void ELFDumper<ELFT>::printNonRelocatableStackSizes(
5690     std::function<void()> PrintHeader) {
5691   // This function ignores potentially erroneous input, unless it is directly
5692   // related to stack size reporting.
5693   for (const Elf_Shdr &Sec : cantFail(Obj.sections())) {
5694     if (this->getPrintableSectionName(Sec) != ".stack_sizes")
5695       continue;
5696     PrintHeader();
5697     ArrayRef<uint8_t> Contents =
5698         unwrapOrError(this->FileName, Obj.getSectionContents(Sec));
5699     DataExtractor Data(Contents, Obj.isLE(), sizeof(Elf_Addr));
5700     uint64_t Offset = 0;
5701     while (Offset < Contents.size()) {
5702       // The function address is followed by a ULEB representing the stack
5703       // size. Check for an extra byte before we try to process the entry.
5704       if (!Data.isValidOffsetForDataOfSize(Offset, sizeof(Elf_Addr) + 1)) {
5705         reportUniqueWarning(
5706             describe(Sec) +
5707             " ended while trying to extract a stack size entry");
5708         break;
5709       }
5710       uint64_t SymValue = Data.getAddress(&Offset);
5711       if (!printFunctionStackSize(SymValue, /*FunctionSec=*/None, Sec, Data,
5712                                   &Offset))
5713         break;
5714     }
5715   }
5716 }
5717 
5718 template <class ELFT>
5719 void ELFDumper<ELFT>::printRelocatableStackSizes(
5720     std::function<void()> PrintHeader) {
5721   // Build a map between stack size sections and their corresponding relocation
5722   // sections.
5723   llvm::MapVector<const Elf_Shdr *, const Elf_Shdr *> StackSizeRelocMap;
5724   for (const Elf_Shdr &Sec : cantFail(Obj.sections())) {
5725     StringRef SectionName;
5726     if (Expected<StringRef> NameOrErr = Obj.getSectionName(Sec))
5727       SectionName = *NameOrErr;
5728     else
5729       consumeError(NameOrErr.takeError());
5730 
5731     // A stack size section that we haven't encountered yet is mapped to the
5732     // null section until we find its corresponding relocation section.
5733     if (SectionName == ".stack_sizes")
5734       if (StackSizeRelocMap
5735               .insert(std::make_pair(&Sec, (const Elf_Shdr *)nullptr))
5736               .second)
5737         continue;
5738 
5739     // Check relocation sections if they are relocating contents of a
5740     // stack sizes section.
5741     if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL)
5742       continue;
5743 
5744     Expected<const Elf_Shdr *> RelSecOrErr = Obj.getSection(Sec.sh_info);
5745     if (!RelSecOrErr) {
5746       reportUniqueWarning(describe(Sec) +
5747                           ": failed to get a relocated section: " +
5748                           toString(RelSecOrErr.takeError()));
5749       continue;
5750     }
5751 
5752     const Elf_Shdr *ContentsSec = *RelSecOrErr;
5753     if (this->getPrintableSectionName(**RelSecOrErr) != ".stack_sizes")
5754       continue;
5755 
5756     // Insert a mapping from the stack sizes section to its relocation section.
5757     StackSizeRelocMap[ContentsSec] = &Sec;
5758   }
5759 
5760   for (const auto &StackSizeMapEntry : StackSizeRelocMap) {
5761     PrintHeader();
5762     const Elf_Shdr *StackSizesELFSec = StackSizeMapEntry.first;
5763     const Elf_Shdr *RelocSec = StackSizeMapEntry.second;
5764 
5765     // Warn about stack size sections without a relocation section.
5766     if (!RelocSec) {
5767       reportWarning(createError(".stack_sizes (" + describe(*StackSizesELFSec) +
5768                                 ") does not have a corresponding "
5769                                 "relocation section"),
5770                     FileName);
5771       continue;
5772     }
5773 
5774     // A .stack_sizes section header's sh_link field is supposed to point
5775     // to the section that contains the functions whose stack sizes are
5776     // described in it.
5777     const Elf_Shdr *FunctionSec = unwrapOrError(
5778         this->FileName, Obj.getSection(StackSizesELFSec->sh_link));
5779 
5780     SupportsRelocation IsSupportedFn;
5781     RelocationResolver Resolver;
5782     std::tie(IsSupportedFn, Resolver) = getRelocationResolver(this->ObjF);
5783     ArrayRef<uint8_t> Contents =
5784         unwrapOrError(this->FileName, Obj.getSectionContents(*StackSizesELFSec));
5785     DataExtractor Data(Contents, Obj.isLE(), sizeof(Elf_Addr));
5786 
5787     forEachRelocationDo(
5788         *RelocSec, /*RawRelr=*/false,
5789         [&](const Relocation<ELFT> &R, unsigned Ndx, const Elf_Shdr &Sec,
5790             const Elf_Shdr *SymTab) {
5791           if (!IsSupportedFn || !IsSupportedFn(R.Type)) {
5792             reportUniqueWarning(
5793                 describe(*RelocSec) +
5794                 " contains an unsupported relocation with index " + Twine(Ndx) +
5795                 ": " + Obj.getRelocationTypeName(R.Type));
5796             return;
5797           }
5798 
5799           this->printStackSize(R, *RelocSec, Ndx, SymTab, FunctionSec,
5800                                *StackSizesELFSec, Resolver, Data);
5801         },
5802         [](const Elf_Relr &) {
5803           llvm_unreachable("can't get here, because we only support "
5804                            "SHT_REL/SHT_RELA sections");
5805         });
5806   }
5807 }
5808 
5809 template <class ELFT>
5810 void GNUELFDumper<ELFT>::printStackSizes() {
5811   bool HeaderHasBeenPrinted = false;
5812   auto PrintHeader = [&]() {
5813     if (HeaderHasBeenPrinted)
5814       return;
5815     OS << "\nStack Sizes:\n";
5816     OS.PadToColumn(9);
5817     OS << "Size";
5818     OS.PadToColumn(18);
5819     OS << "Function\n";
5820     HeaderHasBeenPrinted = true;
5821   };
5822 
5823   // For non-relocatable objects, look directly for sections whose name starts
5824   // with .stack_sizes and process the contents.
5825   if (this->Obj.getHeader().e_type == ELF::ET_REL)
5826     this->printRelocatableStackSizes(PrintHeader);
5827   else
5828     this->printNonRelocatableStackSizes(PrintHeader);
5829 }
5830 
5831 template <class ELFT>
5832 void GNUELFDumper<ELFT>::printMipsGOT(const MipsGOTParser<ELFT> &Parser) {
5833   size_t Bias = ELFT::Is64Bits ? 8 : 0;
5834   auto PrintEntry = [&](const Elf_Addr *E, StringRef Purpose) {
5835     OS.PadToColumn(2);
5836     OS << format_hex_no_prefix(Parser.getGotAddress(E), 8 + Bias);
5837     OS.PadToColumn(11 + Bias);
5838     OS << format_decimal(Parser.getGotOffset(E), 6) << "(gp)";
5839     OS.PadToColumn(22 + Bias);
5840     OS << format_hex_no_prefix(*E, 8 + Bias);
5841     OS.PadToColumn(31 + 2 * Bias);
5842     OS << Purpose << "\n";
5843   };
5844 
5845   OS << (Parser.IsStatic ? "Static GOT:\n" : "Primary GOT:\n");
5846   OS << " Canonical gp value: "
5847      << format_hex_no_prefix(Parser.getGp(), 8 + Bias) << "\n\n";
5848 
5849   OS << " Reserved entries:\n";
5850   if (ELFT::Is64Bits)
5851     OS << "           Address     Access          Initial Purpose\n";
5852   else
5853     OS << "   Address     Access  Initial Purpose\n";
5854   PrintEntry(Parser.getGotLazyResolver(), "Lazy resolver");
5855   if (Parser.getGotModulePointer())
5856     PrintEntry(Parser.getGotModulePointer(), "Module pointer (GNU extension)");
5857 
5858   if (!Parser.getLocalEntries().empty()) {
5859     OS << "\n";
5860     OS << " Local entries:\n";
5861     if (ELFT::Is64Bits)
5862       OS << "           Address     Access          Initial\n";
5863     else
5864       OS << "   Address     Access  Initial\n";
5865     for (auto &E : Parser.getLocalEntries())
5866       PrintEntry(&E, "");
5867   }
5868 
5869   if (Parser.IsStatic)
5870     return;
5871 
5872   if (!Parser.getGlobalEntries().empty()) {
5873     OS << "\n";
5874     OS << " Global entries:\n";
5875     if (ELFT::Is64Bits)
5876       OS << "           Address     Access          Initial         Sym.Val."
5877          << " Type    Ndx Name\n";
5878     else
5879       OS << "   Address     Access  Initial Sym.Val. Type    Ndx Name\n";
5880 
5881     DataRegion<Elf_Word> ShndxTable(
5882         (const Elf_Word *)this->DynSymTabShndxRegion.Addr, this->Obj.end());
5883     for (auto &E : Parser.getGlobalEntries()) {
5884       const Elf_Sym &Sym = *Parser.getGotSym(&E);
5885       const Elf_Sym &FirstSym = this->dynamic_symbols()[0];
5886       std::string SymName = this->getFullSymbolName(
5887           Sym, &Sym - &FirstSym, ShndxTable, this->DynamicStringTable, false);
5888 
5889       OS.PadToColumn(2);
5890       OS << to_string(format_hex_no_prefix(Parser.getGotAddress(&E), 8 + Bias));
5891       OS.PadToColumn(11 + Bias);
5892       OS << to_string(format_decimal(Parser.getGotOffset(&E), 6)) + "(gp)";
5893       OS.PadToColumn(22 + Bias);
5894       OS << to_string(format_hex_no_prefix(E, 8 + Bias));
5895       OS.PadToColumn(31 + 2 * Bias);
5896       OS << to_string(format_hex_no_prefix(Sym.st_value, 8 + Bias));
5897       OS.PadToColumn(40 + 3 * Bias);
5898       OS << printEnum(Sym.getType(), makeArrayRef(ElfSymbolTypes));
5899       OS.PadToColumn(48 + 3 * Bias);
5900       OS << getSymbolSectionNdx(Sym, &Sym - this->dynamic_symbols().begin(),
5901                                 ShndxTable);
5902       OS.PadToColumn(52 + 3 * Bias);
5903       OS << SymName << "\n";
5904     }
5905   }
5906 
5907   if (!Parser.getOtherEntries().empty())
5908     OS << "\n Number of TLS and multi-GOT entries "
5909        << Parser.getOtherEntries().size() << "\n";
5910 }
5911 
5912 template <class ELFT>
5913 void GNUELFDumper<ELFT>::printMipsPLT(const MipsGOTParser<ELFT> &Parser) {
5914   size_t Bias = ELFT::Is64Bits ? 8 : 0;
5915   auto PrintEntry = [&](const Elf_Addr *E, StringRef Purpose) {
5916     OS.PadToColumn(2);
5917     OS << format_hex_no_prefix(Parser.getPltAddress(E), 8 + Bias);
5918     OS.PadToColumn(11 + Bias);
5919     OS << format_hex_no_prefix(*E, 8 + Bias);
5920     OS.PadToColumn(20 + 2 * Bias);
5921     OS << Purpose << "\n";
5922   };
5923 
5924   OS << "PLT GOT:\n\n";
5925 
5926   OS << " Reserved entries:\n";
5927   OS << "   Address  Initial Purpose\n";
5928   PrintEntry(Parser.getPltLazyResolver(), "PLT lazy resolver");
5929   if (Parser.getPltModulePointer())
5930     PrintEntry(Parser.getPltModulePointer(), "Module pointer");
5931 
5932   if (!Parser.getPltEntries().empty()) {
5933     OS << "\n";
5934     OS << " Entries:\n";
5935     OS << "   Address  Initial Sym.Val. Type    Ndx Name\n";
5936     DataRegion<Elf_Word> ShndxTable(
5937         (const Elf_Word *)this->DynSymTabShndxRegion.Addr, this->Obj.end());
5938     for (auto &E : Parser.getPltEntries()) {
5939       const Elf_Sym &Sym = *Parser.getPltSym(&E);
5940       const Elf_Sym &FirstSym = *cantFail(
5941           this->Obj.template getEntry<Elf_Sym>(*Parser.getPltSymTable(), 0));
5942       std::string SymName = this->getFullSymbolName(
5943           Sym, &Sym - &FirstSym, ShndxTable, this->DynamicStringTable, false);
5944 
5945       OS.PadToColumn(2);
5946       OS << to_string(format_hex_no_prefix(Parser.getPltAddress(&E), 8 + Bias));
5947       OS.PadToColumn(11 + Bias);
5948       OS << to_string(format_hex_no_prefix(E, 8 + Bias));
5949       OS.PadToColumn(20 + 2 * Bias);
5950       OS << to_string(format_hex_no_prefix(Sym.st_value, 8 + Bias));
5951       OS.PadToColumn(29 + 3 * Bias);
5952       OS << printEnum(Sym.getType(), makeArrayRef(ElfSymbolTypes));
5953       OS.PadToColumn(37 + 3 * Bias);
5954       OS << getSymbolSectionNdx(Sym, &Sym - this->dynamic_symbols().begin(),
5955                                 ShndxTable);
5956       OS.PadToColumn(41 + 3 * Bias);
5957       OS << SymName << "\n";
5958     }
5959   }
5960 }
5961 
5962 template <class ELFT>
5963 Expected<const Elf_Mips_ABIFlags<ELFT> *>
5964 getMipsAbiFlagsSection(const ELFDumper<ELFT> &Dumper) {
5965   const typename ELFT::Shdr *Sec = Dumper.findSectionByName(".MIPS.abiflags");
5966   if (Sec == nullptr)
5967     return nullptr;
5968 
5969   constexpr StringRef ErrPrefix = "unable to read the .MIPS.abiflags section: ";
5970   Expected<ArrayRef<uint8_t>> DataOrErr =
5971       Dumper.getElfObject().getELFFile().getSectionContents(*Sec);
5972   if (!DataOrErr)
5973     return createError(ErrPrefix + toString(DataOrErr.takeError()));
5974 
5975   if (DataOrErr->size() != sizeof(Elf_Mips_ABIFlags<ELFT>))
5976     return createError(ErrPrefix + "it has a wrong size (" +
5977         Twine(DataOrErr->size()) + ")");
5978   return reinterpret_cast<const Elf_Mips_ABIFlags<ELFT> *>(DataOrErr->data());
5979 }
5980 
5981 template <class ELFT> void GNUELFDumper<ELFT>::printMipsABIFlags() {
5982   const Elf_Mips_ABIFlags<ELFT> *Flags = nullptr;
5983   if (Expected<const Elf_Mips_ABIFlags<ELFT> *> SecOrErr =
5984           getMipsAbiFlagsSection(*this))
5985     Flags = *SecOrErr;
5986   else
5987     this->reportUniqueWarning(SecOrErr.takeError());
5988   if (!Flags)
5989     return;
5990 
5991   OS << "MIPS ABI Flags Version: " << Flags->version << "\n\n";
5992   OS << "ISA: MIPS" << int(Flags->isa_level);
5993   if (Flags->isa_rev > 1)
5994     OS << "r" << int(Flags->isa_rev);
5995   OS << "\n";
5996   OS << "GPR size: " << getMipsRegisterSize(Flags->gpr_size) << "\n";
5997   OS << "CPR1 size: " << getMipsRegisterSize(Flags->cpr1_size) << "\n";
5998   OS << "CPR2 size: " << getMipsRegisterSize(Flags->cpr2_size) << "\n";
5999   OS << "FP ABI: " << printEnum(Flags->fp_abi, makeArrayRef(ElfMipsFpABIType))
6000      << "\n";
6001   OS << "ISA Extension: "
6002      << printEnum(Flags->isa_ext, makeArrayRef(ElfMipsISAExtType)) << "\n";
6003   if (Flags->ases == 0)
6004     OS << "ASEs: None\n";
6005   else
6006     // FIXME: Print each flag on a separate line.
6007     OS << "ASEs: " << printFlags(Flags->ases, makeArrayRef(ElfMipsASEFlags))
6008        << "\n";
6009   OS << "FLAGS 1: " << format_hex_no_prefix(Flags->flags1, 8, false) << "\n";
6010   OS << "FLAGS 2: " << format_hex_no_prefix(Flags->flags2, 8, false) << "\n";
6011   OS << "\n";
6012 }
6013 
6014 template <class ELFT> void LLVMELFDumper<ELFT>::printFileHeaders() {
6015   const Elf_Ehdr &E = this->Obj.getHeader();
6016   {
6017     DictScope D(W, "ElfHeader");
6018     {
6019       DictScope D(W, "Ident");
6020       W.printBinary("Magic", makeArrayRef(E.e_ident).slice(ELF::EI_MAG0, 4));
6021       W.printEnum("Class", E.e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
6022       W.printEnum("DataEncoding", E.e_ident[ELF::EI_DATA],
6023                   makeArrayRef(ElfDataEncoding));
6024       W.printNumber("FileVersion", E.e_ident[ELF::EI_VERSION]);
6025 
6026       auto OSABI = makeArrayRef(ElfOSABI);
6027       if (E.e_ident[ELF::EI_OSABI] >= ELF::ELFOSABI_FIRST_ARCH &&
6028           E.e_ident[ELF::EI_OSABI] <= ELF::ELFOSABI_LAST_ARCH) {
6029         switch (E.e_machine) {
6030         case ELF::EM_AMDGPU:
6031           OSABI = makeArrayRef(AMDGPUElfOSABI);
6032           break;
6033         case ELF::EM_ARM:
6034           OSABI = makeArrayRef(ARMElfOSABI);
6035           break;
6036         case ELF::EM_TI_C6000:
6037           OSABI = makeArrayRef(C6000ElfOSABI);
6038           break;
6039         }
6040       }
6041       W.printEnum("OS/ABI", E.e_ident[ELF::EI_OSABI], OSABI);
6042       W.printNumber("ABIVersion", E.e_ident[ELF::EI_ABIVERSION]);
6043       W.printBinary("Unused", makeArrayRef(E.e_ident).slice(ELF::EI_PAD));
6044     }
6045 
6046     std::string TypeStr;
6047     if (const EnumEntry<unsigned> *Ent = getObjectFileEnumEntry(E.e_type)) {
6048       TypeStr = Ent->Name.str();
6049     } else {
6050       if (E.e_type >= ET_LOPROC)
6051         TypeStr = "Processor Specific";
6052       else if (E.e_type >= ET_LOOS)
6053         TypeStr = "OS Specific";
6054       else
6055         TypeStr = "Unknown";
6056     }
6057     W.printString("Type", TypeStr + " (0x" + to_hexString(E.e_type) + ")");
6058 
6059     W.printEnum("Machine", E.e_machine, makeArrayRef(ElfMachineType));
6060     W.printNumber("Version", E.e_version);
6061     W.printHex("Entry", E.e_entry);
6062     W.printHex("ProgramHeaderOffset", E.e_phoff);
6063     W.printHex("SectionHeaderOffset", E.e_shoff);
6064     if (E.e_machine == EM_MIPS)
6065       W.printFlags("Flags", E.e_flags, makeArrayRef(ElfHeaderMipsFlags),
6066                    unsigned(ELF::EF_MIPS_ARCH), unsigned(ELF::EF_MIPS_ABI),
6067                    unsigned(ELF::EF_MIPS_MACH));
6068     else if (E.e_machine == EM_AMDGPU)
6069       W.printFlags("Flags", E.e_flags, makeArrayRef(ElfHeaderAMDGPUFlags),
6070                    unsigned(ELF::EF_AMDGPU_MACH));
6071     else if (E.e_machine == EM_RISCV)
6072       W.printFlags("Flags", E.e_flags, makeArrayRef(ElfHeaderRISCVFlags));
6073     else
6074       W.printFlags("Flags", E.e_flags);
6075     W.printNumber("HeaderSize", E.e_ehsize);
6076     W.printNumber("ProgramHeaderEntrySize", E.e_phentsize);
6077     W.printNumber("ProgramHeaderCount", E.e_phnum);
6078     W.printNumber("SectionHeaderEntrySize", E.e_shentsize);
6079     W.printString("SectionHeaderCount",
6080                   getSectionHeadersNumString(this->Obj, this->FileName));
6081     W.printString("StringTableSectionIndex",
6082                   getSectionHeaderTableIndexString(this->Obj, this->FileName));
6083   }
6084 }
6085 
6086 template <class ELFT> void LLVMELFDumper<ELFT>::printGroupSections() {
6087   DictScope Lists(W, "Groups");
6088   std::vector<GroupSection> V = this->getGroups();
6089   DenseMap<uint64_t, const GroupSection *> Map = mapSectionsToGroups(V);
6090   for (const GroupSection &G : V) {
6091     DictScope D(W, "Group");
6092     W.printNumber("Name", G.Name, G.ShName);
6093     W.printNumber("Index", G.Index);
6094     W.printNumber("Link", G.Link);
6095     W.printNumber("Info", G.Info);
6096     W.printHex("Type", getGroupType(G.Type), G.Type);
6097     W.startLine() << "Signature: " << G.Signature << "\n";
6098 
6099     ListScope L(W, "Section(s) in group");
6100     for (const GroupMember &GM : G.Members) {
6101       const GroupSection *MainGroup = Map[GM.Index];
6102       if (MainGroup != &G)
6103         this->reportUniqueWarning(
6104             "section with index " + Twine(GM.Index) +
6105             ", included in the group section with index " +
6106             Twine(MainGroup->Index) +
6107             ", was also found in the group section with index " +
6108             Twine(G.Index));
6109       W.startLine() << GM.Name << " (" << GM.Index << ")\n";
6110     }
6111   }
6112 
6113   if (V.empty())
6114     W.startLine() << "There are no group sections in the file.\n";
6115 }
6116 
6117 template <class ELFT> void LLVMELFDumper<ELFT>::printRelocations() {
6118   ListScope D(W, "Relocations");
6119 
6120   for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) {
6121     if (!isRelocationSec<ELFT>(Sec))
6122       continue;
6123 
6124     StringRef Name = this->getPrintableSectionName(Sec);
6125     unsigned SecNdx = &Sec - &cantFail(this->Obj.sections()).front();
6126     W.startLine() << "Section (" << SecNdx << ") " << Name << " {\n";
6127     W.indent();
6128     this->printRelocationsHelper(Sec);
6129     W.unindent();
6130     W.startLine() << "}\n";
6131   }
6132 }
6133 
6134 template <class ELFT>
6135 void LLVMELFDumper<ELFT>::printRelrReloc(const Elf_Relr &R) {
6136   W.startLine() << W.hex(R) << "\n";
6137 }
6138 
6139 template <class ELFT>
6140 void LLVMELFDumper<ELFT>::printRelRelaReloc(const Relocation<ELFT> &R,
6141                                             const RelSymbol<ELFT> &RelSym) {
6142   StringRef SymbolName = RelSym.Name;
6143   SmallString<32> RelocName;
6144   this->Obj.getRelocationTypeName(R.Type, RelocName);
6145 
6146   if (opts::ExpandRelocs) {
6147     DictScope Group(W, "Relocation");
6148     W.printHex("Offset", R.Offset);
6149     W.printNumber("Type", RelocName, R.Type);
6150     W.printNumber("Symbol", !SymbolName.empty() ? SymbolName : "-", R.Symbol);
6151     if (R.Addend)
6152       W.printHex("Addend", (uintX_t)*R.Addend);
6153   } else {
6154     raw_ostream &OS = W.startLine();
6155     OS << W.hex(R.Offset) << " " << RelocName << " "
6156        << (!SymbolName.empty() ? SymbolName : "-");
6157     if (R.Addend)
6158       OS << " " << W.hex((uintX_t)*R.Addend);
6159     OS << "\n";
6160   }
6161 }
6162 
6163 template <class ELFT> void LLVMELFDumper<ELFT>::printSectionHeaders() {
6164   ListScope SectionsD(W, "Sections");
6165 
6166   int SectionIndex = -1;
6167   std::vector<EnumEntry<unsigned>> FlagsList =
6168       getSectionFlagsForTarget(this->Obj.getHeader().e_machine);
6169   for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) {
6170     DictScope SectionD(W, "Section");
6171     W.printNumber("Index", ++SectionIndex);
6172     W.printNumber("Name", this->getPrintableSectionName(Sec), Sec.sh_name);
6173     W.printHex("Type",
6174                object::getELFSectionTypeName(this->Obj.getHeader().e_machine,
6175                                              Sec.sh_type),
6176                Sec.sh_type);
6177     W.printFlags("Flags", Sec.sh_flags, makeArrayRef(FlagsList));
6178     W.printHex("Address", Sec.sh_addr);
6179     W.printHex("Offset", Sec.sh_offset);
6180     W.printNumber("Size", Sec.sh_size);
6181     W.printNumber("Link", Sec.sh_link);
6182     W.printNumber("Info", Sec.sh_info);
6183     W.printNumber("AddressAlignment", Sec.sh_addralign);
6184     W.printNumber("EntrySize", Sec.sh_entsize);
6185 
6186     if (opts::SectionRelocations) {
6187       ListScope D(W, "Relocations");
6188       this->printRelocationsHelper(Sec);
6189     }
6190 
6191     if (opts::SectionSymbols) {
6192       ListScope D(W, "Symbols");
6193       if (this->DotSymtabSec) {
6194         StringRef StrTable = unwrapOrError(
6195             this->FileName,
6196             this->Obj.getStringTableForSymtab(*this->DotSymtabSec));
6197         ArrayRef<Elf_Word> ShndxTable = this->getShndxTable(this->DotSymtabSec);
6198 
6199         typename ELFT::SymRange Symbols = unwrapOrError(
6200             this->FileName, this->Obj.symbols(this->DotSymtabSec));
6201         for (const Elf_Sym &Sym : Symbols) {
6202           const Elf_Shdr *SymSec = unwrapOrError(
6203               this->FileName,
6204               this->Obj.getSection(Sym, this->DotSymtabSec, ShndxTable));
6205           if (SymSec == &Sec)
6206             printSymbol(Sym, &Sym - &Symbols[0], ShndxTable, StrTable, false,
6207                         false);
6208         }
6209       }
6210     }
6211 
6212     if (opts::SectionData && Sec.sh_type != ELF::SHT_NOBITS) {
6213       ArrayRef<uint8_t> Data =
6214           unwrapOrError(this->FileName, this->Obj.getSectionContents(Sec));
6215       W.printBinaryBlock(
6216           "SectionData",
6217           StringRef(reinterpret_cast<const char *>(Data.data()), Data.size()));
6218     }
6219   }
6220 }
6221 
6222 template <class ELFT>
6223 void LLVMELFDumper<ELFT>::printSymbolSection(
6224     const Elf_Sym &Symbol, unsigned SymIndex,
6225     DataRegion<Elf_Word> ShndxTable) const {
6226   auto GetSectionSpecialType = [&]() -> Optional<StringRef> {
6227     if (Symbol.isUndefined())
6228       return StringRef("Undefined");
6229     if (Symbol.isProcessorSpecific())
6230       return StringRef("Processor Specific");
6231     if (Symbol.isOSSpecific())
6232       return StringRef("Operating System Specific");
6233     if (Symbol.isAbsolute())
6234       return StringRef("Absolute");
6235     if (Symbol.isCommon())
6236       return StringRef("Common");
6237     if (Symbol.isReserved() && Symbol.st_shndx != SHN_XINDEX)
6238       return StringRef("Reserved");
6239     return None;
6240   };
6241 
6242   if (Optional<StringRef> Type = GetSectionSpecialType()) {
6243     W.printHex("Section", *Type, Symbol.st_shndx);
6244     return;
6245   }
6246 
6247   Expected<unsigned> SectionIndex =
6248       this->getSymbolSectionIndex(Symbol, SymIndex, ShndxTable);
6249   if (!SectionIndex) {
6250     assert(Symbol.st_shndx == SHN_XINDEX &&
6251            "getSymbolSectionIndex should only fail due to an invalid "
6252            "SHT_SYMTAB_SHNDX table/reference");
6253     this->reportUniqueWarning(SectionIndex.takeError());
6254     W.printHex("Section", "Reserved", SHN_XINDEX);
6255     return;
6256   }
6257 
6258   Expected<StringRef> SectionName =
6259       this->getSymbolSectionName(Symbol, *SectionIndex);
6260   if (!SectionName) {
6261     // Don't report an invalid section name if the section headers are missing.
6262     // In such situations, all sections will be "invalid".
6263     if (!this->ObjF.sections().empty())
6264       this->reportUniqueWarning(SectionName.takeError());
6265     else
6266       consumeError(SectionName.takeError());
6267     W.printHex("Section", "<?>", *SectionIndex);
6268   } else {
6269     W.printHex("Section", *SectionName, *SectionIndex);
6270   }
6271 }
6272 
6273 template <class ELFT>
6274 void LLVMELFDumper<ELFT>::printSymbol(const Elf_Sym &Symbol, unsigned SymIndex,
6275                                       DataRegion<Elf_Word> ShndxTable,
6276                                       Optional<StringRef> StrTable,
6277                                       bool IsDynamic,
6278                                       bool /*NonVisibilityBitsUsed*/) const {
6279   std::string FullSymbolName = this->getFullSymbolName(
6280       Symbol, SymIndex, ShndxTable, StrTable, IsDynamic);
6281   unsigned char SymbolType = Symbol.getType();
6282 
6283   DictScope D(W, "Symbol");
6284   W.printNumber("Name", FullSymbolName, Symbol.st_name);
6285   W.printHex("Value", Symbol.st_value);
6286   W.printNumber("Size", Symbol.st_size);
6287   W.printEnum("Binding", Symbol.getBinding(), makeArrayRef(ElfSymbolBindings));
6288   if (this->Obj.getHeader().e_machine == ELF::EM_AMDGPU &&
6289       SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
6290     W.printEnum("Type", SymbolType, makeArrayRef(AMDGPUSymbolTypes));
6291   else
6292     W.printEnum("Type", SymbolType, makeArrayRef(ElfSymbolTypes));
6293   if (Symbol.st_other == 0)
6294     // Usually st_other flag is zero. Do not pollute the output
6295     // by flags enumeration in that case.
6296     W.printNumber("Other", 0);
6297   else {
6298     std::vector<EnumEntry<unsigned>> SymOtherFlags(std::begin(ElfSymOtherFlags),
6299                                                    std::end(ElfSymOtherFlags));
6300     if (this->Obj.getHeader().e_machine == EM_MIPS) {
6301       // Someones in their infinite wisdom decided to make STO_MIPS_MIPS16
6302       // flag overlapped with other ST_MIPS_xxx flags. So consider both
6303       // cases separately.
6304       if ((Symbol.st_other & STO_MIPS_MIPS16) == STO_MIPS_MIPS16)
6305         SymOtherFlags.insert(SymOtherFlags.end(),
6306                              std::begin(ElfMips16SymOtherFlags),
6307                              std::end(ElfMips16SymOtherFlags));
6308       else
6309         SymOtherFlags.insert(SymOtherFlags.end(),
6310                              std::begin(ElfMipsSymOtherFlags),
6311                              std::end(ElfMipsSymOtherFlags));
6312     } else if (this->Obj.getHeader().e_machine == EM_AARCH64) {
6313       SymOtherFlags.insert(SymOtherFlags.end(),
6314                            std::begin(ElfAArch64SymOtherFlags),
6315                            std::end(ElfAArch64SymOtherFlags));
6316     }
6317     W.printFlags("Other", Symbol.st_other, makeArrayRef(SymOtherFlags), 0x3u);
6318   }
6319   printSymbolSection(Symbol, SymIndex, ShndxTable);
6320 }
6321 
6322 template <class ELFT>
6323 void LLVMELFDumper<ELFT>::printSymbols(bool PrintSymbols,
6324                                        bool PrintDynamicSymbols) {
6325   if (PrintSymbols) {
6326     ListScope Group(W, "Symbols");
6327     this->printSymbolsHelper(false);
6328   }
6329   if (PrintDynamicSymbols) {
6330     ListScope Group(W, "DynamicSymbols");
6331     this->printSymbolsHelper(true);
6332   }
6333 }
6334 
6335 template <class ELFT> void LLVMELFDumper<ELFT>::printDynamicTable() {
6336   Elf_Dyn_Range Table = this->dynamic_table();
6337   if (Table.empty())
6338     return;
6339 
6340   W.startLine() << "DynamicSection [ (" << Table.size() << " entries)\n";
6341 
6342   size_t MaxTagSize = getMaxDynamicTagSize(this->Obj, Table);
6343   // The "Name/Value" column should be indented from the "Type" column by N
6344   // spaces, where N = MaxTagSize - length of "Type" (4) + trailing
6345   // space (1) = -3.
6346   W.startLine() << "  Tag" << std::string(ELFT::Is64Bits ? 16 : 8, ' ')
6347                 << "Type" << std::string(MaxTagSize - 3, ' ') << "Name/Value\n";
6348 
6349   std::string ValueFmt = "%-" + std::to_string(MaxTagSize) + "s ";
6350   for (auto Entry : Table) {
6351     uintX_t Tag = Entry.getTag();
6352     std::string Value = this->getDynamicEntry(Tag, Entry.getVal());
6353     W.startLine() << "  " << format_hex(Tag, ELFT::Is64Bits ? 18 : 10, true)
6354                   << " "
6355                   << format(ValueFmt.c_str(),
6356                             this->Obj.getDynamicTagAsString(Tag).c_str())
6357                   << Value << "\n";
6358   }
6359   W.startLine() << "]\n";
6360 }
6361 
6362 template <class ELFT> void LLVMELFDumper<ELFT>::printDynamicRelocations() {
6363   W.startLine() << "Dynamic Relocations {\n";
6364   W.indent();
6365   this->printDynamicRelocationsHelper();
6366   W.unindent();
6367   W.startLine() << "}\n";
6368 }
6369 
6370 template <class ELFT>
6371 void LLVMELFDumper<ELFT>::printProgramHeaders(
6372     bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) {
6373   if (PrintProgramHeaders)
6374     printProgramHeaders();
6375   if (PrintSectionMapping == cl::BOU_TRUE)
6376     printSectionMapping();
6377 }
6378 
6379 template <class ELFT> void LLVMELFDumper<ELFT>::printProgramHeaders() {
6380   ListScope L(W, "ProgramHeaders");
6381 
6382   Expected<ArrayRef<Elf_Phdr>> PhdrsOrErr = this->Obj.program_headers();
6383   if (!PhdrsOrErr) {
6384     this->reportUniqueWarning("unable to dump program headers: " +
6385                               toString(PhdrsOrErr.takeError()));
6386     return;
6387   }
6388 
6389   for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
6390     DictScope P(W, "ProgramHeader");
6391     StringRef Type =
6392         segmentTypeToString(this->Obj.getHeader().e_machine, Phdr.p_type);
6393 
6394     W.printHex("Type", Type.empty() ? "Unknown" : Type, Phdr.p_type);
6395     W.printHex("Offset", Phdr.p_offset);
6396     W.printHex("VirtualAddress", Phdr.p_vaddr);
6397     W.printHex("PhysicalAddress", Phdr.p_paddr);
6398     W.printNumber("FileSize", Phdr.p_filesz);
6399     W.printNumber("MemSize", Phdr.p_memsz);
6400     W.printFlags("Flags", Phdr.p_flags, makeArrayRef(ElfSegmentFlags));
6401     W.printNumber("Alignment", Phdr.p_align);
6402   }
6403 }
6404 
6405 template <class ELFT>
6406 void LLVMELFDumper<ELFT>::printVersionSymbolSection(const Elf_Shdr *Sec) {
6407   ListScope SS(W, "VersionSymbols");
6408   if (!Sec)
6409     return;
6410 
6411   StringRef StrTable;
6412   ArrayRef<Elf_Sym> Syms;
6413   const Elf_Shdr *SymTabSec;
6414   Expected<ArrayRef<Elf_Versym>> VerTableOrErr =
6415       this->getVersionTable(*Sec, &Syms, &StrTable, &SymTabSec);
6416   if (!VerTableOrErr) {
6417     this->reportUniqueWarning(VerTableOrErr.takeError());
6418     return;
6419   }
6420 
6421   if (StrTable.empty() || Syms.empty() || Syms.size() != VerTableOrErr->size())
6422     return;
6423 
6424   ArrayRef<Elf_Word> ShNdxTable = this->getShndxTable(SymTabSec);
6425   for (size_t I = 0, E = Syms.size(); I < E; ++I) {
6426     DictScope S(W, "Symbol");
6427     W.printNumber("Version", (*VerTableOrErr)[I].vs_index & VERSYM_VERSION);
6428     W.printString("Name",
6429                   this->getFullSymbolName(Syms[I], I, ShNdxTable, StrTable,
6430                                           /*IsDynamic=*/true));
6431   }
6432 }
6433 
6434 static const EnumEntry<unsigned> SymVersionFlags[] = {
6435     {"Base", "BASE", VER_FLG_BASE},
6436     {"Weak", "WEAK", VER_FLG_WEAK},
6437     {"Info", "INFO", VER_FLG_INFO}};
6438 
6439 template <class ELFT>
6440 void LLVMELFDumper<ELFT>::printVersionDefinitionSection(const Elf_Shdr *Sec) {
6441   ListScope SD(W, "VersionDefinitions");
6442   if (!Sec)
6443     return;
6444 
6445   Expected<std::vector<VerDef>> V = this->Obj.getVersionDefinitions(*Sec);
6446   if (!V) {
6447     this->reportUniqueWarning(V.takeError());
6448     return;
6449   }
6450 
6451   for (const VerDef &D : *V) {
6452     DictScope Def(W, "Definition");
6453     W.printNumber("Version", D.Version);
6454     W.printFlags("Flags", D.Flags, makeArrayRef(SymVersionFlags));
6455     W.printNumber("Index", D.Ndx);
6456     W.printNumber("Hash", D.Hash);
6457     W.printString("Name", D.Name.c_str());
6458     W.printList(
6459         "Predecessors", D.AuxV,
6460         [](raw_ostream &OS, const VerdAux &Aux) { OS << Aux.Name.c_str(); });
6461   }
6462 }
6463 
6464 template <class ELFT>
6465 void LLVMELFDumper<ELFT>::printVersionDependencySection(const Elf_Shdr *Sec) {
6466   ListScope SD(W, "VersionRequirements");
6467   if (!Sec)
6468     return;
6469 
6470   Expected<std::vector<VerNeed>> V =
6471       this->Obj.getVersionDependencies(*Sec, this->WarningHandler);
6472   if (!V) {
6473     this->reportUniqueWarning(V.takeError());
6474     return;
6475   }
6476 
6477   for (const VerNeed &VN : *V) {
6478     DictScope Entry(W, "Dependency");
6479     W.printNumber("Version", VN.Version);
6480     W.printNumber("Count", VN.Cnt);
6481     W.printString("FileName", VN.File.c_str());
6482 
6483     ListScope L(W, "Entries");
6484     for (const VernAux &Aux : VN.AuxV) {
6485       DictScope Entry(W, "Entry");
6486       W.printNumber("Hash", Aux.Hash);
6487       W.printFlags("Flags", Aux.Flags, makeArrayRef(SymVersionFlags));
6488       W.printNumber("Index", Aux.Other);
6489       W.printString("Name", Aux.Name.c_str());
6490     }
6491   }
6492 }
6493 
6494 template <class ELFT> void LLVMELFDumper<ELFT>::printHashHistograms() {
6495   W.startLine() << "Hash Histogram not implemented!\n";
6496 }
6497 
6498 template <class ELFT> void LLVMELFDumper<ELFT>::printCGProfile() {
6499   ListScope L(W, "CGProfile");
6500   if (!this->DotCGProfileSec)
6501     return;
6502 
6503   Expected<ArrayRef<Elf_CGProfile>> CGProfileOrErr =
6504       this->Obj.template getSectionContentsAsArray<Elf_CGProfile>(
6505           *this->DotCGProfileSec);
6506   if (!CGProfileOrErr) {
6507     this->reportUniqueWarning(
6508         "unable to dump the SHT_LLVM_CALL_GRAPH_PROFILE section: " +
6509         toString(CGProfileOrErr.takeError()));
6510     return;
6511   }
6512 
6513   for (const Elf_CGProfile &CGPE : *CGProfileOrErr) {
6514     DictScope D(W, "CGProfileEntry");
6515     W.printNumber("From", this->getStaticSymbolName(CGPE.cgp_from),
6516                   CGPE.cgp_from);
6517     W.printNumber("To", this->getStaticSymbolName(CGPE.cgp_to),
6518                   CGPE.cgp_to);
6519     W.printNumber("Weight", CGPE.cgp_weight);
6520   }
6521 }
6522 
6523 template <class ELFT> void LLVMELFDumper<ELFT>::printAddrsig() {
6524   ListScope L(W, "Addrsig");
6525   if (!this->DotAddrsigSec)
6526     return;
6527 
6528   Expected<std::vector<uint64_t>> SymsOrErr =
6529       decodeAddrsigSection(this->Obj, *this->DotAddrsigSec);
6530   if (!SymsOrErr) {
6531     this->reportUniqueWarning(SymsOrErr.takeError());
6532     return;
6533   }
6534 
6535   for (uint64_t Sym : *SymsOrErr)
6536     W.printNumber("Sym", this->getStaticSymbolName(Sym), Sym);
6537 }
6538 
6539 template <typename ELFT>
6540 static bool printGNUNoteLLVMStyle(uint32_t NoteType, ArrayRef<uint8_t> Desc,
6541                                   ScopedPrinter &W) {
6542   // Return true if we were able to pretty-print the note, false otherwise.
6543   switch (NoteType) {
6544   default:
6545     return false;
6546   case ELF::NT_GNU_ABI_TAG: {
6547     const GNUAbiTag &AbiTag = getGNUAbiTag<ELFT>(Desc);
6548     if (!AbiTag.IsValid) {
6549       W.printString("ABI", "<corrupt GNU_ABI_TAG>");
6550       return false;
6551     } else {
6552       W.printString("OS", AbiTag.OSName);
6553       W.printString("ABI", AbiTag.ABI);
6554     }
6555     break;
6556   }
6557   case ELF::NT_GNU_BUILD_ID: {
6558     W.printString("Build ID", getGNUBuildId(Desc));
6559     break;
6560   }
6561   case ELF::NT_GNU_GOLD_VERSION:
6562     W.printString("Version", getGNUGoldVersion(Desc));
6563     break;
6564   case ELF::NT_GNU_PROPERTY_TYPE_0:
6565     ListScope D(W, "Property");
6566     for (const std::string &Property : getGNUPropertyList<ELFT>(Desc))
6567       W.printString(Property);
6568     break;
6569   }
6570   return true;
6571 }
6572 
6573 static void printCoreNoteLLVMStyle(const CoreNote &Note, ScopedPrinter &W) {
6574   W.printNumber("Page Size", Note.PageSize);
6575   for (const CoreFileMapping &Mapping : Note.Mappings) {
6576     ListScope D(W, "Mapping");
6577     W.printHex("Start", Mapping.Start);
6578     W.printHex("End", Mapping.End);
6579     W.printHex("Offset", Mapping.Offset);
6580     W.printString("Filename", Mapping.Filename);
6581   }
6582 }
6583 
6584 template <class ELFT> void LLVMELFDumper<ELFT>::printNotes() {
6585   ListScope L(W, "Notes");
6586 
6587   std::unique_ptr<DictScope> NoteScope;
6588   auto StartNotes = [&](Optional<StringRef> SecName,
6589                         const typename ELFT::Off Offset,
6590                         const typename ELFT::Addr Size) {
6591     NoteScope = std::make_unique<DictScope>(W, "NoteSection");
6592     W.printString("Name", SecName ? *SecName : "<?>");
6593     W.printHex("Offset", Offset);
6594     W.printHex("Size", Size);
6595   };
6596 
6597   auto EndNotes = [&] { NoteScope.reset(); };
6598 
6599   auto ProcessNote = [&](const Elf_Note &Note, bool IsCore) -> Error {
6600     DictScope D2(W, "Note");
6601     StringRef Name = Note.getName();
6602     ArrayRef<uint8_t> Descriptor = Note.getDesc();
6603     Elf_Word Type = Note.getType();
6604 
6605     // Print the note owner/type.
6606     W.printString("Owner", Name);
6607     W.printHex("Data size", Descriptor.size());
6608 
6609     StringRef NoteType =
6610         getNoteTypeName<ELFT>(Note, this->Obj.getHeader().e_type);
6611     if (!NoteType.empty())
6612       W.printString("Type", NoteType);
6613     else
6614       W.printString("Type",
6615                     "Unknown (" + to_string(format_hex(Type, 10)) + ")");
6616 
6617     // Print the description, or fallback to printing raw bytes for unknown
6618     // owners/if we fail to pretty-print the contents.
6619     if (Name == "GNU") {
6620       if (printGNUNoteLLVMStyle<ELFT>(Type, Descriptor, W))
6621         return Error::success();
6622     } else if (Name == "FreeBSD") {
6623       if (Optional<FreeBSDNote> N =
6624               getFreeBSDNote<ELFT>(Type, Descriptor, IsCore)) {
6625         W.printString(N->Type, N->Value);
6626         return Error::success();
6627       }
6628     } else if (Name == "AMD") {
6629       const AMDNote N = getAMDNote<ELFT>(Type, Descriptor);
6630       if (!N.Type.empty()) {
6631         W.printString(N.Type, N.Value);
6632         return Error::success();
6633       }
6634     } else if (Name == "AMDGPU") {
6635       const AMDGPUNote N = getAMDGPUNote<ELFT>(Type, Descriptor);
6636       if (!N.Type.empty()) {
6637         W.printString(N.Type, N.Value);
6638         return Error::success();
6639       }
6640     } else if (Name == "CORE") {
6641       if (Type == ELF::NT_FILE) {
6642         DataExtractor DescExtractor(Descriptor,
6643                                     ELFT::TargetEndianness == support::little,
6644                                     sizeof(Elf_Addr));
6645         if (Expected<CoreNote> N = readCoreNote(DescExtractor)) {
6646           printCoreNoteLLVMStyle(*N, W);
6647           return Error::success();
6648         } else {
6649           return N.takeError();
6650         }
6651       }
6652     }
6653     if (!Descriptor.empty()) {
6654       W.printBinaryBlock("Description data", Descriptor);
6655     }
6656     return Error::success();
6657   };
6658 
6659   printNotesHelper(*this, StartNotes, ProcessNote, EndNotes);
6660 }
6661 
6662 template <class ELFT> void LLVMELFDumper<ELFT>::printELFLinkerOptions() {
6663   ListScope L(W, "LinkerOptions");
6664 
6665   unsigned I = -1;
6666   for (const Elf_Shdr &Shdr : cantFail(this->Obj.sections())) {
6667     ++I;
6668     if (Shdr.sh_type != ELF::SHT_LLVM_LINKER_OPTIONS)
6669       continue;
6670 
6671     Expected<ArrayRef<uint8_t>> ContentsOrErr =
6672         this->Obj.getSectionContents(Shdr);
6673     if (!ContentsOrErr) {
6674       this->reportUniqueWarning("unable to read the content of the "
6675                                 "SHT_LLVM_LINKER_OPTIONS section: " +
6676                                 toString(ContentsOrErr.takeError()));
6677       continue;
6678     }
6679     if (ContentsOrErr->empty())
6680       continue;
6681 
6682     if (ContentsOrErr->back() != 0) {
6683       this->reportUniqueWarning("SHT_LLVM_LINKER_OPTIONS section at index " +
6684                                 Twine(I) +
6685                                 " is broken: the "
6686                                 "content is not null-terminated");
6687       continue;
6688     }
6689 
6690     SmallVector<StringRef, 16> Strings;
6691     toStringRef(ContentsOrErr->drop_back()).split(Strings, '\0');
6692     if (Strings.size() % 2 != 0) {
6693       this->reportUniqueWarning(
6694           "SHT_LLVM_LINKER_OPTIONS section at index " + Twine(I) +
6695           " is broken: an incomplete "
6696           "key-value pair was found. The last possible key was: \"" +
6697           Strings.back() + "\"");
6698       continue;
6699     }
6700 
6701     for (size_t I = 0; I < Strings.size(); I += 2)
6702       W.printString(Strings[I], Strings[I + 1]);
6703   }
6704 }
6705 
6706 template <class ELFT> void LLVMELFDumper<ELFT>::printDependentLibs() {
6707   ListScope L(W, "DependentLibs");
6708   this->printDependentLibsHelper(
6709       [](const Elf_Shdr &) {},
6710       [this](StringRef Lib, uint64_t) { W.printString(Lib); });
6711 }
6712 
6713 template <class ELFT> void LLVMELFDumper<ELFT>::printStackSizes() {
6714   ListScope L(W, "StackSizes");
6715   if (this->Obj.getHeader().e_type == ELF::ET_REL)
6716     this->printRelocatableStackSizes([]() {});
6717   else
6718     this->printNonRelocatableStackSizes([]() {});
6719 }
6720 
6721 template <class ELFT>
6722 void LLVMELFDumper<ELFT>::printStackSizeEntry(uint64_t Size, StringRef FuncName) {
6723   DictScope D(W, "Entry");
6724   W.printString("Function", FuncName);
6725   W.printHex("Size", Size);
6726 }
6727 
6728 template <class ELFT>
6729 void LLVMELFDumper<ELFT>::printMipsGOT(const MipsGOTParser<ELFT> &Parser) {
6730   auto PrintEntry = [&](const Elf_Addr *E) {
6731     W.printHex("Address", Parser.getGotAddress(E));
6732     W.printNumber("Access", Parser.getGotOffset(E));
6733     W.printHex("Initial", *E);
6734   };
6735 
6736   DictScope GS(W, Parser.IsStatic ? "Static GOT" : "Primary GOT");
6737 
6738   W.printHex("Canonical gp value", Parser.getGp());
6739   {
6740     ListScope RS(W, "Reserved entries");
6741     {
6742       DictScope D(W, "Entry");
6743       PrintEntry(Parser.getGotLazyResolver());
6744       W.printString("Purpose", StringRef("Lazy resolver"));
6745     }
6746 
6747     if (Parser.getGotModulePointer()) {
6748       DictScope D(W, "Entry");
6749       PrintEntry(Parser.getGotModulePointer());
6750       W.printString("Purpose", StringRef("Module pointer (GNU extension)"));
6751     }
6752   }
6753   {
6754     ListScope LS(W, "Local entries");
6755     for (auto &E : Parser.getLocalEntries()) {
6756       DictScope D(W, "Entry");
6757       PrintEntry(&E);
6758     }
6759   }
6760 
6761   if (Parser.IsStatic)
6762     return;
6763 
6764   {
6765     ListScope GS(W, "Global entries");
6766     for (auto &E : Parser.getGlobalEntries()) {
6767       DictScope D(W, "Entry");
6768 
6769       PrintEntry(&E);
6770 
6771       const Elf_Sym &Sym = *Parser.getGotSym(&E);
6772       W.printHex("Value", Sym.st_value);
6773       W.printEnum("Type", Sym.getType(), makeArrayRef(ElfSymbolTypes));
6774 
6775       const unsigned SymIndex = &Sym - this->dynamic_symbols().begin();
6776       DataRegion<Elf_Word> ShndxTable(
6777           (const Elf_Word *)this->DynSymTabShndxRegion.Addr, this->Obj.end());
6778       printSymbolSection(Sym, SymIndex, ShndxTable);
6779 
6780       std::string SymName = this->getFullSymbolName(
6781           Sym, SymIndex, ShndxTable, this->DynamicStringTable, true);
6782       W.printNumber("Name", SymName, Sym.st_name);
6783     }
6784   }
6785 
6786   W.printNumber("Number of TLS and multi-GOT entries",
6787                 uint64_t(Parser.getOtherEntries().size()));
6788 }
6789 
6790 template <class ELFT>
6791 void LLVMELFDumper<ELFT>::printMipsPLT(const MipsGOTParser<ELFT> &Parser) {
6792   auto PrintEntry = [&](const Elf_Addr *E) {
6793     W.printHex("Address", Parser.getPltAddress(E));
6794     W.printHex("Initial", *E);
6795   };
6796 
6797   DictScope GS(W, "PLT GOT");
6798 
6799   {
6800     ListScope RS(W, "Reserved entries");
6801     {
6802       DictScope D(W, "Entry");
6803       PrintEntry(Parser.getPltLazyResolver());
6804       W.printString("Purpose", StringRef("PLT lazy resolver"));
6805     }
6806 
6807     if (auto E = Parser.getPltModulePointer()) {
6808       DictScope D(W, "Entry");
6809       PrintEntry(E);
6810       W.printString("Purpose", StringRef("Module pointer"));
6811     }
6812   }
6813   {
6814     ListScope LS(W, "Entries");
6815     DataRegion<Elf_Word> ShndxTable(
6816         (const Elf_Word *)this->DynSymTabShndxRegion.Addr, this->Obj.end());
6817     for (auto &E : Parser.getPltEntries()) {
6818       DictScope D(W, "Entry");
6819       PrintEntry(&E);
6820 
6821       const Elf_Sym &Sym = *Parser.getPltSym(&E);
6822       W.printHex("Value", Sym.st_value);
6823       W.printEnum("Type", Sym.getType(), makeArrayRef(ElfSymbolTypes));
6824       printSymbolSection(Sym, &Sym - this->dynamic_symbols().begin(),
6825                          ShndxTable);
6826 
6827       const Elf_Sym *FirstSym = cantFail(
6828           this->Obj.template getEntry<Elf_Sym>(*Parser.getPltSymTable(), 0));
6829       std::string SymName = this->getFullSymbolName(
6830           Sym, &Sym - FirstSym, ShndxTable, Parser.getPltStrTable(), true);
6831       W.printNumber("Name", SymName, Sym.st_name);
6832     }
6833   }
6834 }
6835 
6836 template <class ELFT> void LLVMELFDumper<ELFT>::printMipsABIFlags() {
6837   const Elf_Mips_ABIFlags<ELFT> *Flags;
6838   if (Expected<const Elf_Mips_ABIFlags<ELFT> *> SecOrErr =
6839           getMipsAbiFlagsSection(*this)) {
6840     Flags = *SecOrErr;
6841     if (!Flags) {
6842       W.startLine() << "There is no .MIPS.abiflags section in the file.\n";
6843       return;
6844     }
6845   } else {
6846     this->reportUniqueWarning(SecOrErr.takeError());
6847     return;
6848   }
6849 
6850   raw_ostream &OS = W.getOStream();
6851   DictScope GS(W, "MIPS ABI Flags");
6852 
6853   W.printNumber("Version", Flags->version);
6854   W.startLine() << "ISA: ";
6855   if (Flags->isa_rev <= 1)
6856     OS << format("MIPS%u", Flags->isa_level);
6857   else
6858     OS << format("MIPS%ur%u", Flags->isa_level, Flags->isa_rev);
6859   OS << "\n";
6860   W.printEnum("ISA Extension", Flags->isa_ext, makeArrayRef(ElfMipsISAExtType));
6861   W.printFlags("ASEs", Flags->ases, makeArrayRef(ElfMipsASEFlags));
6862   W.printEnum("FP ABI", Flags->fp_abi, makeArrayRef(ElfMipsFpABIType));
6863   W.printNumber("GPR size", getMipsRegisterSize(Flags->gpr_size));
6864   W.printNumber("CPR1 size", getMipsRegisterSize(Flags->cpr1_size));
6865   W.printNumber("CPR2 size", getMipsRegisterSize(Flags->cpr2_size));
6866   W.printFlags("Flags 1", Flags->flags1, makeArrayRef(ElfMipsFlags1));
6867   W.printHex("Flags 2", Flags->flags2);
6868 }
6869