• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- ELFDumper.cpp - ELF-specific dumper ---------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file implements the ELF-specific dumper for llvm-readobj.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "ARMAttributeParser.h"
16 #include "ARMEHABIPrinter.h"
17 #include "Error.h"
18 #include "ObjDumper.h"
19 #include "StackMapPrinter.h"
20 #include "llvm-readobj.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Object/ELFObjectFile.h"
25 #include "llvm/Support/ARMBuildAttributes.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/Support/FormattedStream.h"
29 #include "llvm/Support/MathExtras.h"
30 #include "llvm/Support/MipsABIFlags.h"
31 #include "llvm/Support/ScopedPrinter.h"
32 #include "llvm/Support/raw_ostream.h"
33 
34 using namespace llvm;
35 using namespace llvm::object;
36 using namespace ELF;
37 
38 #define LLVM_READOBJ_ENUM_CASE(ns, enum) \
39   case ns::enum: return #enum;
40 
41 #define ENUM_ENT(enum, altName) \
42   { #enum, altName, ELF::enum }
43 
44 #define ENUM_ENT_1(enum) \
45   { #enum, #enum, ELF::enum }
46 
47 #define LLVM_READOBJ_PHDR_ENUM(ns, enum)                                       \
48   case ns::enum:                                                               \
49     return std::string(#enum).substr(3);
50 
51 #define TYPEDEF_ELF_TYPES(ELFT)                                                \
52   typedef ELFFile<ELFT> ELFO;                                                  \
53   typedef typename ELFO::Elf_Shdr Elf_Shdr;                                    \
54   typedef typename ELFO::Elf_Sym Elf_Sym;                                      \
55   typedef typename ELFO::Elf_Dyn Elf_Dyn;                                      \
56   typedef typename ELFO::Elf_Dyn_Range Elf_Dyn_Range;                          \
57   typedef typename ELFO::Elf_Rel Elf_Rel;                                      \
58   typedef typename ELFO::Elf_Rela Elf_Rela;                                    \
59   typedef typename ELFO::Elf_Rela_Range Elf_Rela_Range;                        \
60   typedef typename ELFO::Elf_Phdr Elf_Phdr;                                    \
61   typedef typename ELFO::Elf_Half Elf_Half;                                    \
62   typedef typename ELFO::Elf_Ehdr Elf_Ehdr;                                    \
63   typedef typename ELFO::Elf_Word Elf_Word;                                    \
64   typedef typename ELFO::Elf_Hash Elf_Hash;                                    \
65   typedef typename ELFO::Elf_GnuHash Elf_GnuHash;                              \
66   typedef typename ELFO::uintX_t uintX_t;
67 
68 namespace {
69 
70 template <class ELFT> class DumpStyle;
71 
72 /// Represents a contiguous uniform range in the file. We cannot just create a
73 /// range directly because when creating one of these from the .dynamic table
74 /// the size, entity size and virtual address are different entries in arbitrary
75 /// order (DT_REL, DT_RELSZ, DT_RELENT for example).
76 struct DynRegionInfo {
DynRegionInfo__anon79be9f670111::DynRegionInfo77   DynRegionInfo() : Addr(nullptr), Size(0), EntSize(0) {}
DynRegionInfo__anon79be9f670111::DynRegionInfo78   DynRegionInfo(const void *A, uint64_t S, uint64_t ES)
79       : Addr(A), Size(S), EntSize(ES) {}
80   /// \brief Address in current address space.
81   const void *Addr;
82   /// \brief Size in bytes of the region.
83   uint64_t Size;
84   /// \brief Size of each entity in the region.
85   uint64_t EntSize;
86 
getAsArrayRef__anon79be9f670111::DynRegionInfo87   template <typename Type> ArrayRef<Type> getAsArrayRef() const {
88     const Type *Start = reinterpret_cast<const Type *>(Addr);
89     if (!Start)
90       return {Start, Start};
91     if (EntSize != sizeof(Type) || Size % EntSize)
92       reportError("Invalid entity size");
93     return {Start, Start + (Size / EntSize)};
94   }
95 };
96 
97 template<typename ELFT>
98 class ELFDumper : public ObjDumper {
99 public:
100   ELFDumper(const ELFFile<ELFT> *Obj, ScopedPrinter &Writer);
101 
102   void printFileHeaders() override;
103   void printSections() override;
104   void printRelocations() override;
105   void printDynamicRelocations() override;
106   void printSymbols() override;
107   void printDynamicSymbols() override;
108   void printUnwindInfo() override;
109 
110   void printDynamicTable() override;
111   void printNeededLibraries() override;
112   void printProgramHeaders() override;
113   void printHashTable() override;
114   void printGnuHashTable() override;
115   void printLoadName() override;
116   void printVersionInfo() override;
117   void printGroupSections() override;
118 
119   void printAttributes() override;
120   void printMipsPLTGOT() override;
121   void printMipsABIFlags() override;
122   void printMipsReginfo() override;
123   void printMipsOptions() override;
124 
125   void printStackMap() const override;
126 
127   void printHashHistogram() override;
128 
129 private:
130   std::unique_ptr<DumpStyle<ELFT>> ELFDumperStyle;
131   typedef ELFFile<ELFT> ELFO;
132   typedef typename ELFO::Elf_Shdr Elf_Shdr;
133   typedef typename ELFO::Elf_Sym Elf_Sym;
134   typedef typename ELFO::Elf_Sym_Range Elf_Sym_Range;
135   typedef typename ELFO::Elf_Dyn Elf_Dyn;
136   typedef typename ELFO::Elf_Dyn_Range Elf_Dyn_Range;
137   typedef typename ELFO::Elf_Rel Elf_Rel;
138   typedef typename ELFO::Elf_Rela Elf_Rela;
139   typedef typename ELFO::Elf_Rel_Range Elf_Rel_Range;
140   typedef typename ELFO::Elf_Rela_Range Elf_Rela_Range;
141   typedef typename ELFO::Elf_Phdr Elf_Phdr;
142   typedef typename ELFO::Elf_Half Elf_Half;
143   typedef typename ELFO::Elf_Hash Elf_Hash;
144   typedef typename ELFO::Elf_GnuHash Elf_GnuHash;
145   typedef typename ELFO::Elf_Ehdr Elf_Ehdr;
146   typedef typename ELFO::Elf_Word Elf_Word;
147   typedef typename ELFO::uintX_t uintX_t;
148   typedef typename ELFO::Elf_Versym Elf_Versym;
149   typedef typename ELFO::Elf_Verneed Elf_Verneed;
150   typedef typename ELFO::Elf_Vernaux Elf_Vernaux;
151   typedef typename ELFO::Elf_Verdef Elf_Verdef;
152   typedef typename ELFO::Elf_Verdaux Elf_Verdaux;
153 
checkDRI(DynRegionInfo DRI)154   DynRegionInfo checkDRI(DynRegionInfo DRI) {
155     if (DRI.Addr < Obj->base() ||
156         (const uint8_t *)DRI.Addr + DRI.Size > Obj->base() + Obj->getBufSize())
157       error(llvm::object::object_error::parse_failed);
158     return DRI;
159   }
160 
createDRIFrom(const Elf_Phdr * P,uintX_t EntSize)161   DynRegionInfo createDRIFrom(const Elf_Phdr *P, uintX_t EntSize) {
162     return checkDRI({Obj->base() + P->p_offset, P->p_filesz, EntSize});
163   }
164 
createDRIFrom(const Elf_Shdr * S)165   DynRegionInfo createDRIFrom(const Elf_Shdr *S) {
166     return checkDRI({Obj->base() + S->sh_offset, S->sh_size, S->sh_entsize});
167   }
168 
169   void parseDynamicTable(ArrayRef<const Elf_Phdr *> LoadSegments);
170 
171   void printValue(uint64_t Type, uint64_t Value);
172 
173   StringRef getDynamicString(uint64_t Offset) const;
174   StringRef getSymbolVersion(StringRef StrTab, const Elf_Sym *symb,
175                              bool &IsDefault) const;
176   void LoadVersionMap() const;
177   void LoadVersionNeeds(const Elf_Shdr *ec) const;
178   void LoadVersionDefs(const Elf_Shdr *sec) const;
179 
180   const ELFO *Obj;
181   DynRegionInfo DynRelRegion;
182   DynRegionInfo DynRelaRegion;
183   DynRegionInfo DynPLTRelRegion;
184   DynRegionInfo DynSymRegion;
185   DynRegionInfo DynamicTable;
186   StringRef DynamicStringTable;
187   StringRef SOName;
188   const Elf_Hash *HashTable = nullptr;
189   const Elf_GnuHash *GnuHashTable = nullptr;
190   const Elf_Shdr *DotSymtabSec = nullptr;
191   StringRef DynSymtabName;
192   ArrayRef<Elf_Word> ShndxTable;
193 
194   const Elf_Shdr *dot_gnu_version_sec = nullptr;   // .gnu.version
195   const Elf_Shdr *dot_gnu_version_r_sec = nullptr; // .gnu.version_r
196   const Elf_Shdr *dot_gnu_version_d_sec = nullptr; // .gnu.version_d
197 
198   // Records for each version index the corresponding Verdef or Vernaux entry.
199   // This is filled the first time LoadVersionMap() is called.
200   class VersionMapEntry : public PointerIntPair<const void *, 1> {
201   public:
202     // If the integer is 0, this is an Elf_Verdef*.
203     // If the integer is 1, this is an Elf_Vernaux*.
VersionMapEntry()204     VersionMapEntry() : PointerIntPair<const void *, 1>(nullptr, 0) {}
VersionMapEntry(const Elf_Verdef * verdef)205     VersionMapEntry(const Elf_Verdef *verdef)
206         : PointerIntPair<const void *, 1>(verdef, 0) {}
VersionMapEntry(const Elf_Vernaux * vernaux)207     VersionMapEntry(const Elf_Vernaux *vernaux)
208         : PointerIntPair<const void *, 1>(vernaux, 1) {}
isNull() const209     bool isNull() const { return getPointer() == nullptr; }
isVerdef() const210     bool isVerdef() const { return !isNull() && getInt() == 0; }
isVernaux() const211     bool isVernaux() const { return !isNull() && getInt() == 1; }
getVerdef() const212     const Elf_Verdef *getVerdef() const {
213       return isVerdef() ? (const Elf_Verdef *)getPointer() : nullptr;
214     }
getVernaux() const215     const Elf_Vernaux *getVernaux() const {
216       return isVernaux() ? (const Elf_Vernaux *)getPointer() : nullptr;
217     }
218   };
219   mutable SmallVector<VersionMapEntry, 16> VersionMap;
220 
221 public:
dynamic_table() const222   Elf_Dyn_Range dynamic_table() const {
223     return DynamicTable.getAsArrayRef<Elf_Dyn>();
224   }
225 
dynamic_symbols() const226   Elf_Sym_Range dynamic_symbols() const {
227     return DynSymRegion.getAsArrayRef<Elf_Sym>();
228   }
229 
230   Elf_Rel_Range dyn_rels() const;
231   Elf_Rela_Range dyn_relas() const;
232   std::string getFullSymbolName(const Elf_Sym *Symbol, StringRef StrTable,
233                                 bool IsDynamic) const;
234 
235   void printSymbolsHelper(bool IsDynamic) const;
getDotSymtabSec() const236   const Elf_Shdr *getDotSymtabSec() const { return DotSymtabSec; }
getShndxTable() const237   ArrayRef<Elf_Word> getShndxTable() const { return ShndxTable; }
getDynamicStringTable() const238   StringRef getDynamicStringTable() const { return DynamicStringTable; }
getDynRelRegion() const239   const DynRegionInfo &getDynRelRegion() const { return DynRelRegion; }
getDynRelaRegion() const240   const DynRegionInfo &getDynRelaRegion() const { return DynRelaRegion; }
getDynPLTRelRegion() const241   const DynRegionInfo &getDynPLTRelRegion() const { return DynPLTRelRegion; }
getHashTable() const242   const Elf_Hash *getHashTable() const { return HashTable; }
getGnuHashTable() const243   const Elf_GnuHash *getGnuHashTable() const { return GnuHashTable; }
244 };
245 
246 template <class ELFT>
printSymbolsHelper(bool IsDynamic) const247 void ELFDumper<ELFT>::printSymbolsHelper(bool IsDynamic) const {
248   StringRef StrTable, SymtabName;
249   size_t Entries = 0;
250   Elf_Sym_Range Syms(nullptr, nullptr);
251   if (IsDynamic) {
252     StrTable = DynamicStringTable;
253     Syms = dynamic_symbols();
254     SymtabName = DynSymtabName;
255     if (DynSymRegion.Addr)
256       Entries = DynSymRegion.Size / DynSymRegion.EntSize;
257   } else {
258     if (!DotSymtabSec)
259       return;
260     StrTable = unwrapOrError(Obj->getStringTableForSymtab(*DotSymtabSec));
261     Syms = Obj->symbols(DotSymtabSec);
262     SymtabName = unwrapOrError(Obj->getSectionName(DotSymtabSec));
263     Entries = DotSymtabSec->getEntityCount();
264   }
265   if (Syms.begin() == Syms.end())
266     return;
267   ELFDumperStyle->printSymtabMessage(Obj, SymtabName, Entries);
268   for (const auto &Sym : Syms)
269     ELFDumperStyle->printSymbol(Obj, &Sym, Syms.begin(), StrTable, IsDynamic);
270 }
271 
272 template <typename ELFT> class DumpStyle {
273 public:
274   using Elf_Shdr = typename ELFFile<ELFT>::Elf_Shdr;
275   using Elf_Sym =  typename ELFFile<ELFT>::Elf_Sym;
276 
DumpStyle(ELFDumper<ELFT> * Dumper)277   DumpStyle(ELFDumper<ELFT> *Dumper) : Dumper(Dumper) {}
~DumpStyle()278   virtual ~DumpStyle() {}
279   virtual void printFileHeaders(const ELFFile<ELFT> *Obj) = 0;
280   virtual void printGroupSections(const ELFFile<ELFT> *Obj) = 0;
281   virtual void printRelocations(const ELFFile<ELFT> *Obj) = 0;
282   virtual void printSections(const ELFFile<ELFT> *Obj) = 0;
283   virtual void printSymbols(const ELFFile<ELFT> *Obj) = 0;
284   virtual void printDynamicSymbols(const ELFFile<ELFT> *Obj) = 0;
285   virtual void printDynamicRelocations(const ELFFile<ELFT> *Obj) = 0;
printSymtabMessage(const ELFFile<ELFT> * obj,StringRef Name,size_t Offset)286   virtual void printSymtabMessage(const ELFFile<ELFT> *obj, StringRef Name,
287                                   size_t Offset) {
288     return;
289   }
290   virtual void printSymbol(const ELFFile<ELFT> *Obj, const Elf_Sym *Symbol,
291                            const Elf_Sym *FirstSym, StringRef StrTable,
292                            bool IsDynamic) = 0;
293   virtual void printProgramHeaders(const ELFFile<ELFT> *Obj) = 0;
294   virtual void printHashHistogram(const ELFFile<ELFT> *Obj) = 0;
dumper() const295   const ELFDumper<ELFT> *dumper() const { return Dumper; }
296 private:
297   const ELFDumper<ELFT> *Dumper;
298 };
299 
300 template <typename ELFT> class GNUStyle : public DumpStyle<ELFT> {
301   formatted_raw_ostream OS;
302 public:
303   TYPEDEF_ELF_TYPES(ELFT)
GNUStyle(ScopedPrinter & W,ELFDumper<ELFT> * Dumper)304   GNUStyle(ScopedPrinter &W, ELFDumper<ELFT> *Dumper)
305       : DumpStyle<ELFT>(Dumper), OS(W.getOStream()) {}
306   void printFileHeaders(const ELFO *Obj) override;
307   void printGroupSections(const ELFFile<ELFT> *Obj) override;
308   void printRelocations(const ELFO *Obj) override;
309   void printSections(const ELFO *Obj) override;
310   void printSymbols(const ELFO *Obj) override;
311   void printDynamicSymbols(const ELFO *Obj) override;
312   void printDynamicRelocations(const ELFO *Obj) override;
313   virtual void printSymtabMessage(const ELFO *Obj, StringRef Name,
314                                   size_t Offset) override;
315   void printProgramHeaders(const ELFO *Obj) override;
316   void printHashHistogram(const ELFFile<ELFT> *Obj) override;
317 
318 private:
319   struct Field {
320     StringRef Str;
321     unsigned Column;
Field__anon79be9f670111::GNUStyle::Field322     Field(StringRef S, unsigned Col) : Str(S), Column(Col) {}
Field__anon79be9f670111::GNUStyle::Field323     Field(unsigned Col) : Str(""), Column(Col) {}
324   };
325 
326   template <typename T, typename TEnum>
printEnum(T Value,ArrayRef<EnumEntry<TEnum>> EnumValues)327   std::string printEnum(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues) {
328     for (const auto &EnumItem : EnumValues)
329       if (EnumItem.Value == Value)
330         return EnumItem.AltName;
331     return to_hexString(Value, false);
332   }
333 
printField(struct Field F)334   formatted_raw_ostream &printField(struct Field F) {
335     if (F.Column != 0)
336       OS.PadToColumn(F.Column);
337     OS << F.Str;
338     OS.flush();
339     return OS;
340   }
341   void printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab,
342                        const Elf_Rela &R, bool IsRela);
343   void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First,
344                    StringRef StrTable, bool IsDynamic) override;
345   std::string getSymbolSectionNdx(const ELFO *Obj, const Elf_Sym *Symbol,
346                                   const Elf_Sym *FirstSym);
347   void printDynamicRelocation(const ELFO *Obj, Elf_Rela R, bool IsRela);
348   bool checkTLSSections(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
349   bool checkoffsets(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
350   bool checkVMA(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
351   bool checkPTDynamic(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
352 };
353 
354 template <typename ELFT> class LLVMStyle : public DumpStyle<ELFT> {
355 public:
356   TYPEDEF_ELF_TYPES(ELFT)
LLVMStyle(ScopedPrinter & W,ELFDumper<ELFT> * Dumper)357   LLVMStyle(ScopedPrinter &W, ELFDumper<ELFT> *Dumper)
358       : DumpStyle<ELFT>(Dumper), W(W) {}
359 
360   void printFileHeaders(const ELFO *Obj) override;
361   void printGroupSections(const ELFFile<ELFT> *Obj) override;
362   void printRelocations(const ELFO *Obj) override;
363   void printRelocations(const Elf_Shdr *Sec, const ELFO *Obj);
364   void printSections(const ELFO *Obj) override;
365   void printSymbols(const ELFO *Obj) override;
366   void printDynamicSymbols(const ELFO *Obj) override;
367   void printDynamicRelocations(const ELFO *Obj) override;
368   void printProgramHeaders(const ELFO *Obj) override;
369   void printHashHistogram(const ELFFile<ELFT> *Obj) override;
370 
371 private:
372   void printRelocation(const ELFO *Obj, Elf_Rela Rel, const Elf_Shdr *SymTab);
373   void printDynamicRelocation(const ELFO *Obj, Elf_Rela Rel);
374   void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First,
375                    StringRef StrTable, bool IsDynamic) override;
376   ScopedPrinter &W;
377 };
378 
379 } // namespace
380 
381 namespace llvm {
382 
383 template <class ELFT>
createELFDumper(const ELFFile<ELFT> * Obj,ScopedPrinter & Writer,std::unique_ptr<ObjDumper> & Result)384 static std::error_code createELFDumper(const ELFFile<ELFT> *Obj,
385                                        ScopedPrinter &Writer,
386                                        std::unique_ptr<ObjDumper> &Result) {
387   Result.reset(new ELFDumper<ELFT>(Obj, Writer));
388   return readobj_error::success;
389 }
390 
createELFDumper(const object::ObjectFile * Obj,ScopedPrinter & Writer,std::unique_ptr<ObjDumper> & Result)391 std::error_code createELFDumper(const object::ObjectFile *Obj,
392                                 ScopedPrinter &Writer,
393                                 std::unique_ptr<ObjDumper> &Result) {
394   // Little-endian 32-bit
395   if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
396     return createELFDumper(ELFObj->getELFFile(), Writer, Result);
397 
398   // Big-endian 32-bit
399   if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
400     return createELFDumper(ELFObj->getELFFile(), Writer, Result);
401 
402   // Little-endian 64-bit
403   if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
404     return createELFDumper(ELFObj->getELFFile(), Writer, Result);
405 
406   // Big-endian 64-bit
407   if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
408     return createELFDumper(ELFObj->getELFFile(), Writer, Result);
409 
410   return readobj_error::unsupported_obj_file_format;
411 }
412 
413 } // namespace llvm
414 
415 // Iterate through the versions needed section, and place each Elf_Vernaux
416 // in the VersionMap according to its index.
417 template <class ELFT>
LoadVersionNeeds(const Elf_Shdr * sec) const418 void ELFDumper<ELFT>::LoadVersionNeeds(const Elf_Shdr *sec) const {
419   unsigned vn_size = sec->sh_size;  // Size of section in bytes
420   unsigned vn_count = sec->sh_info; // Number of Verneed entries
421   const char *sec_start = (const char *)Obj->base() + sec->sh_offset;
422   const char *sec_end = sec_start + vn_size;
423   // The first Verneed entry is at the start of the section.
424   const char *p = sec_start;
425   for (unsigned i = 0; i < vn_count; i++) {
426     if (p + sizeof(Elf_Verneed) > sec_end)
427       report_fatal_error("Section ended unexpectedly while scanning "
428                          "version needed records.");
429     const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
430     if (vn->vn_version != ELF::VER_NEED_CURRENT)
431       report_fatal_error("Unexpected verneed version");
432     // Iterate through the Vernaux entries
433     const char *paux = p + vn->vn_aux;
434     for (unsigned j = 0; j < vn->vn_cnt; j++) {
435       if (paux + sizeof(Elf_Vernaux) > sec_end)
436         report_fatal_error("Section ended unexpected while scanning auxiliary "
437                            "version needed records.");
438       const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
439       size_t index = vna->vna_other & ELF::VERSYM_VERSION;
440       if (index >= VersionMap.size())
441         VersionMap.resize(index + 1);
442       VersionMap[index] = VersionMapEntry(vna);
443       paux += vna->vna_next;
444     }
445     p += vn->vn_next;
446   }
447 }
448 
449 // Iterate through the version definitions, and place each Elf_Verdef
450 // in the VersionMap according to its index.
451 template <class ELFT>
LoadVersionDefs(const Elf_Shdr * sec) const452 void ELFDumper<ELFT>::LoadVersionDefs(const Elf_Shdr *sec) const {
453   unsigned vd_size = sec->sh_size;  // Size of section in bytes
454   unsigned vd_count = sec->sh_info; // Number of Verdef entries
455   const char *sec_start = (const char *)Obj->base() + sec->sh_offset;
456   const char *sec_end = sec_start + vd_size;
457   // The first Verdef entry is at the start of the section.
458   const char *p = sec_start;
459   for (unsigned i = 0; i < vd_count; i++) {
460     if (p + sizeof(Elf_Verdef) > sec_end)
461       report_fatal_error("Section ended unexpectedly while scanning "
462                          "version definitions.");
463     const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p);
464     if (vd->vd_version != ELF::VER_DEF_CURRENT)
465       report_fatal_error("Unexpected verdef version");
466     size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
467     if (index >= VersionMap.size())
468       VersionMap.resize(index + 1);
469     VersionMap[index] = VersionMapEntry(vd);
470     p += vd->vd_next;
471   }
472 }
473 
LoadVersionMap() const474 template <class ELFT> void ELFDumper<ELFT>::LoadVersionMap() const {
475   // If there is no dynamic symtab or version table, there is nothing to do.
476   if (!DynSymRegion.Addr || !dot_gnu_version_sec)
477     return;
478 
479   // Has the VersionMap already been loaded?
480   if (VersionMap.size() > 0)
481     return;
482 
483   // The first two version indexes are reserved.
484   // Index 0 is LOCAL, index 1 is GLOBAL.
485   VersionMap.push_back(VersionMapEntry());
486   VersionMap.push_back(VersionMapEntry());
487 
488   if (dot_gnu_version_d_sec)
489     LoadVersionDefs(dot_gnu_version_d_sec);
490 
491   if (dot_gnu_version_r_sec)
492     LoadVersionNeeds(dot_gnu_version_r_sec);
493 }
494 
495 template <typename ELFO, class ELFT>
printVersionSymbolSection(ELFDumper<ELFT> * Dumper,const ELFO * Obj,const typename ELFO::Elf_Shdr * Sec,ScopedPrinter & W)496 static void printVersionSymbolSection(ELFDumper<ELFT> *Dumper, const ELFO *Obj,
497                                       const typename ELFO::Elf_Shdr *Sec,
498                                       ScopedPrinter &W) {
499   DictScope SS(W, "Version symbols");
500   if (!Sec)
501     return;
502   StringRef Name = unwrapOrError(Obj->getSectionName(Sec));
503   W.printNumber("Section Name", Name, Sec->sh_name);
504   W.printHex("Address", Sec->sh_addr);
505   W.printHex("Offset", Sec->sh_offset);
506   W.printNumber("Link", Sec->sh_link);
507 
508   const uint8_t *P = (const uint8_t *)Obj->base() + Sec->sh_offset;
509   StringRef StrTable = Dumper->getDynamicStringTable();
510 
511   // Same number of entries in the dynamic symbol table (DT_SYMTAB).
512   ListScope Syms(W, "Symbols");
513   for (const typename ELFO::Elf_Sym &Sym : Dumper->dynamic_symbols()) {
514     DictScope S(W, "Symbol");
515     std::string FullSymbolName =
516         Dumper->getFullSymbolName(&Sym, StrTable, true /* IsDynamic */);
517     W.printNumber("Version", *P);
518     W.printString("Name", FullSymbolName);
519     P += sizeof(typename ELFO::Elf_Half);
520   }
521 }
522 
523 static const EnumEntry<unsigned> SymVersionFlags[] = {
524     {"Base", "BASE", VER_FLG_BASE},
525     {"Weak", "WEAK", VER_FLG_WEAK},
526     {"Info", "INFO", VER_FLG_INFO}};
527 
528 template <typename ELFO, class ELFT>
printVersionDefinitionSection(ELFDumper<ELFT> * Dumper,const ELFO * Obj,const typename ELFO::Elf_Shdr * Sec,ScopedPrinter & W)529 static void printVersionDefinitionSection(ELFDumper<ELFT> *Dumper,
530                                           const ELFO *Obj,
531                                           const typename ELFO::Elf_Shdr *Sec,
532                                           ScopedPrinter &W) {
533   typedef typename ELFO::Elf_Verdef VerDef;
534   typedef typename ELFO::Elf_Verdaux VerdAux;
535 
536   DictScope SD(W, "SHT_GNU_verdef");
537   if (!Sec)
538     return;
539 
540   // The number of entries in the section SHT_GNU_verdef
541   // is determined by DT_VERDEFNUM tag.
542   unsigned VerDefsNum = 0;
543   for (const typename ELFO::Elf_Dyn &Dyn : Dumper->dynamic_table()) {
544     if (Dyn.d_tag == DT_VERDEFNUM)
545       VerDefsNum = Dyn.d_un.d_val;
546   }
547   const uint8_t *SecStartAddress =
548       (const uint8_t *)Obj->base() + Sec->sh_offset;
549   const uint8_t *SecEndAddress = SecStartAddress + Sec->sh_size;
550   const uint8_t *P = SecStartAddress;
551   const typename ELFO::Elf_Shdr *StrTab =
552       unwrapOrError(Obj->getSection(Sec->sh_link));
553 
554   while (VerDefsNum--) {
555     if (P + sizeof(VerDef) > SecEndAddress)
556       report_fatal_error("invalid offset in the section");
557 
558     auto *VD = reinterpret_cast<const VerDef *>(P);
559     DictScope Def(W, "Definition");
560     W.printNumber("Version", VD->vd_version);
561     W.printEnum("Flags", VD->vd_flags, makeArrayRef(SymVersionFlags));
562     W.printNumber("Index", VD->vd_ndx);
563     W.printNumber("Hash", VD->vd_hash);
564     W.printString("Name",
565                   StringRef((const char *)(Obj->base() + StrTab->sh_offset +
566                                            VD->getAux()->vda_name)));
567     if (!VD->vd_cnt)
568       report_fatal_error("at least one definition string must exist");
569     if (VD->vd_cnt > 2)
570       report_fatal_error("more than one predecessor is not expected");
571 
572     if (VD->vd_cnt == 2) {
573       const uint8_t *PAux = P + VD->vd_aux + VD->getAux()->vda_next;
574       const VerdAux *Aux = reinterpret_cast<const VerdAux *>(PAux);
575       W.printString("Predecessor",
576                     StringRef((const char *)(Obj->base() + StrTab->sh_offset +
577                                              Aux->vda_name)));
578     }
579 
580     P += VD->vd_next;
581   }
582 }
583 
584 template <typename ELFO, class ELFT>
printVersionDependencySection(ELFDumper<ELFT> * Dumper,const ELFO * Obj,const typename ELFO::Elf_Shdr * Sec,ScopedPrinter & W)585 static void printVersionDependencySection(ELFDumper<ELFT> *Dumper,
586                                           const ELFO *Obj,
587                                           const typename ELFO::Elf_Shdr *Sec,
588                                           ScopedPrinter &W) {
589   typedef typename ELFO::Elf_Verneed VerNeed;
590   typedef typename ELFO::Elf_Vernaux VernAux;
591 
592   DictScope SD(W, "SHT_GNU_verneed");
593   if (!Sec)
594     return;
595 
596   unsigned VerNeedNum = 0;
597   for (const typename ELFO::Elf_Dyn &Dyn : Dumper->dynamic_table())
598     if (Dyn.d_tag == DT_VERNEEDNUM)
599       VerNeedNum = Dyn.d_un.d_val;
600 
601   const uint8_t *SecData = (const uint8_t *)Obj->base() + Sec->sh_offset;
602   const typename ELFO::Elf_Shdr *StrTab =
603       unwrapOrError(Obj->getSection(Sec->sh_link));
604 
605   const uint8_t *P = SecData;
606   for (unsigned I = 0; I < VerNeedNum; ++I) {
607     const VerNeed *Need = reinterpret_cast<const VerNeed *>(P);
608     DictScope Entry(W, "Dependency");
609     W.printNumber("Version", Need->vn_version);
610     W.printNumber("Count", Need->vn_cnt);
611     W.printString("FileName",
612                   StringRef((const char *)(Obj->base() + StrTab->sh_offset +
613                                            Need->vn_file)));
614 
615     const uint8_t *PAux = P + Need->vn_aux;
616     for (unsigned J = 0; J < Need->vn_cnt; ++J) {
617       const VernAux *Aux = reinterpret_cast<const VernAux *>(PAux);
618       DictScope Entry(W, "Entry");
619       W.printNumber("Hash", Aux->vna_hash);
620       W.printEnum("Flags", Aux->vna_flags, makeArrayRef(SymVersionFlags));
621       W.printNumber("Index", Aux->vna_other);
622       W.printString("Name",
623                     StringRef((const char *)(Obj->base() + StrTab->sh_offset +
624                                              Aux->vna_name)));
625       PAux += Aux->vna_next;
626     }
627     P += Need->vn_next;
628   }
629 }
630 
printVersionInfo()631 template <typename ELFT> void ELFDumper<ELFT>::printVersionInfo() {
632   // Dump version symbol section.
633   printVersionSymbolSection(this, Obj, dot_gnu_version_sec, W);
634 
635   // Dump version definition section.
636   printVersionDefinitionSection(this, Obj, dot_gnu_version_d_sec, W);
637 
638   // Dump version dependency section.
639   printVersionDependencySection(this, Obj, dot_gnu_version_r_sec, W);
640 }
641 
642 template <typename ELFT>
getSymbolVersion(StringRef StrTab,const Elf_Sym * symb,bool & IsDefault) const643 StringRef ELFDumper<ELFT>::getSymbolVersion(StringRef StrTab,
644                                             const Elf_Sym *symb,
645                                             bool &IsDefault) const {
646   // This is a dynamic symbol. Look in the GNU symbol version table.
647   if (!dot_gnu_version_sec) {
648     // No version table.
649     IsDefault = false;
650     return StringRef("");
651   }
652 
653   // Determine the position in the symbol table of this entry.
654   size_t entry_index = (reinterpret_cast<uintptr_t>(symb) -
655                         reinterpret_cast<uintptr_t>(DynSymRegion.Addr)) /
656                        sizeof(Elf_Sym);
657 
658   // Get the corresponding version index entry
659   const Elf_Versym *vs =
660       Obj->template getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
661   size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
662 
663   // Special markers for unversioned symbols.
664   if (version_index == ELF::VER_NDX_LOCAL ||
665       version_index == ELF::VER_NDX_GLOBAL) {
666     IsDefault = false;
667     return StringRef("");
668   }
669 
670   // Lookup this symbol in the version table
671   LoadVersionMap();
672   if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
673     reportError("Invalid version entry");
674   const VersionMapEntry &entry = VersionMap[version_index];
675 
676   // Get the version name string
677   size_t name_offset;
678   if (entry.isVerdef()) {
679     // The first Verdaux entry holds the name.
680     name_offset = entry.getVerdef()->getAux()->vda_name;
681     IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
682   } else {
683     name_offset = entry.getVernaux()->vna_name;
684     IsDefault = false;
685   }
686   if (name_offset >= StrTab.size())
687     reportError("Invalid string offset");
688   return StringRef(StrTab.data() + name_offset);
689 }
690 
691 template <typename ELFT>
getFullSymbolName(const Elf_Sym * Symbol,StringRef StrTable,bool IsDynamic) const692 std::string ELFDumper<ELFT>::getFullSymbolName(const Elf_Sym *Symbol,
693                                                StringRef StrTable,
694                                                bool IsDynamic) const {
695   StringRef SymbolName = unwrapOrError(Symbol->getName(StrTable));
696   if (!IsDynamic)
697     return SymbolName;
698 
699   std::string FullSymbolName(SymbolName);
700 
701   bool IsDefault;
702   StringRef Version = getSymbolVersion(StrTable, &*Symbol, IsDefault);
703   FullSymbolName += (IsDefault ? "@@" : "@");
704   FullSymbolName += Version;
705   return FullSymbolName;
706 }
707 
708 template <typename ELFO>
709 static void
getSectionNameIndex(const ELFO & Obj,const typename ELFO::Elf_Sym * Symbol,const typename ELFO::Elf_Sym * FirstSym,ArrayRef<typename ELFO::Elf_Word> ShndxTable,StringRef & SectionName,unsigned & SectionIndex)710 getSectionNameIndex(const ELFO &Obj, const typename ELFO::Elf_Sym *Symbol,
711                     const typename ELFO::Elf_Sym *FirstSym,
712                     ArrayRef<typename ELFO::Elf_Word> ShndxTable,
713                     StringRef &SectionName, unsigned &SectionIndex) {
714   SectionIndex = Symbol->st_shndx;
715   if (Symbol->isUndefined())
716     SectionName = "Undefined";
717   else if (Symbol->isProcessorSpecific())
718     SectionName = "Processor Specific";
719   else if (Symbol->isOSSpecific())
720     SectionName = "Operating System Specific";
721   else if (Symbol->isAbsolute())
722     SectionName = "Absolute";
723   else if (Symbol->isCommon())
724     SectionName = "Common";
725   else if (Symbol->isReserved() && SectionIndex != SHN_XINDEX)
726     SectionName = "Reserved";
727   else {
728     if (SectionIndex == SHN_XINDEX)
729       SectionIndex =
730           Obj.getExtendedSymbolTableIndex(Symbol, FirstSym, ShndxTable);
731     const typename ELFO::Elf_Shdr *Sec =
732         unwrapOrError(Obj.getSection(SectionIndex));
733     SectionName = unwrapOrError(Obj.getSectionName(Sec));
734   }
735 }
736 
737 template <class ELFO>
738 static const typename ELFO::Elf_Shdr *
findNotEmptySectionByAddress(const ELFO * Obj,uint64_t Addr)739 findNotEmptySectionByAddress(const ELFO *Obj, uint64_t Addr) {
740   for (const auto &Shdr : Obj->sections())
741     if (Shdr.sh_addr == Addr && Shdr.sh_size > 0)
742       return &Shdr;
743   return nullptr;
744 }
745 
746 template <class ELFO>
findSectionByName(const ELFO & Obj,StringRef Name)747 static const typename ELFO::Elf_Shdr *findSectionByName(const ELFO &Obj,
748                                                         StringRef Name) {
749   for (const auto &Shdr : Obj.sections()) {
750     if (Name == unwrapOrError(Obj.getSectionName(&Shdr)))
751       return &Shdr;
752   }
753   return nullptr;
754 }
755 
756 static const EnumEntry<unsigned> ElfClass[] = {
757   {"None",   "none",   ELF::ELFCLASSNONE},
758   {"32-bit", "ELF32",  ELF::ELFCLASS32},
759   {"64-bit", "ELF64",  ELF::ELFCLASS64},
760 };
761 
762 static const EnumEntry<unsigned> ElfDataEncoding[] = {
763   {"None",         "none",                          ELF::ELFDATANONE},
764   {"LittleEndian", "2's complement, little endian", ELF::ELFDATA2LSB},
765   {"BigEndian",    "2's complement, big endian",    ELF::ELFDATA2MSB},
766 };
767 
768 static const EnumEntry<unsigned> ElfObjectFileType[] = {
769   {"None",         "NONE (none)",              ELF::ET_NONE},
770   {"Relocatable",  "REL (Relocatable file)",   ELF::ET_REL},
771   {"Executable",   "EXEC (Executable file)",   ELF::ET_EXEC},
772   {"SharedObject", "DYN (Shared object file)", ELF::ET_DYN},
773   {"Core",         "CORE (Core file)",         ELF::ET_CORE},
774 };
775 
776 static const EnumEntry<unsigned> ElfOSABI[] = {
777   {"SystemV",      "UNIX - System V",      ELF::ELFOSABI_NONE},
778   {"HPUX",         "UNIX - HP-UX",         ELF::ELFOSABI_HPUX},
779   {"NetBSD",       "UNIX - NetBSD",        ELF::ELFOSABI_NETBSD},
780   {"GNU/Linux",    "UNIX - GNU",           ELF::ELFOSABI_LINUX},
781   {"GNU/Hurd",     "GNU/Hurd",             ELF::ELFOSABI_HURD},
782   {"Solaris",      "UNIX - Solaris",       ELF::ELFOSABI_SOLARIS},
783   {"AIX",          "UNIX - AIX",           ELF::ELFOSABI_AIX},
784   {"IRIX",         "UNIX - IRIX",          ELF::ELFOSABI_IRIX},
785   {"FreeBSD",      "UNIX - FreeBSD",       ELF::ELFOSABI_FREEBSD},
786   {"TRU64",        "UNIX - TRU64",         ELF::ELFOSABI_TRU64},
787   {"Modesto",      "Novell - Modesto",     ELF::ELFOSABI_MODESTO},
788   {"OpenBSD",      "UNIX - OpenBSD",       ELF::ELFOSABI_OPENBSD},
789   {"OpenVMS",      "VMS - OpenVMS",        ELF::ELFOSABI_OPENVMS},
790   {"NSK",          "HP - Non-Stop Kernel", ELF::ELFOSABI_NSK},
791   {"AROS",         "AROS",                 ELF::ELFOSABI_AROS},
792   {"FenixOS",      "FenixOS",              ELF::ELFOSABI_FENIXOS},
793   {"CloudABI",     "CloudABI",             ELF::ELFOSABI_CLOUDABI},
794   {"C6000_ELFABI", "Bare-metal C6000",     ELF::ELFOSABI_C6000_ELFABI},
795   {"C6000_LINUX",  "Linux C6000",          ELF::ELFOSABI_C6000_LINUX},
796   {"ARM",          "ARM",                  ELF::ELFOSABI_ARM},
797   {"Standalone",   "Standalone App",       ELF::ELFOSABI_STANDALONE}
798 };
799 
800 static const EnumEntry<unsigned> ElfMachineType[] = {
801   ENUM_ENT(EM_NONE,          "None"),
802   ENUM_ENT(EM_M32,           "WE32100"),
803   ENUM_ENT(EM_SPARC,         "Sparc"),
804   ENUM_ENT(EM_386,           "Intel 80386"),
805   ENUM_ENT(EM_68K,           "MC68000"),
806   ENUM_ENT(EM_88K,           "MC88000"),
807   ENUM_ENT(EM_IAMCU,         "EM_IAMCU"),
808   ENUM_ENT(EM_860,           "Intel 80860"),
809   ENUM_ENT(EM_MIPS,          "MIPS R3000"),
810   ENUM_ENT(EM_S370,          "IBM System/370"),
811   ENUM_ENT(EM_MIPS_RS3_LE,   "MIPS R3000 little-endian"),
812   ENUM_ENT(EM_PARISC,        "HPPA"),
813   ENUM_ENT(EM_VPP500,        "Fujitsu VPP500"),
814   ENUM_ENT(EM_SPARC32PLUS,   "Sparc v8+"),
815   ENUM_ENT(EM_960,           "Intel 80960"),
816   ENUM_ENT(EM_PPC,           "PowerPC"),
817   ENUM_ENT(EM_PPC64,         "PowerPC64"),
818   ENUM_ENT(EM_S390,          "IBM S/390"),
819   ENUM_ENT(EM_SPU,           "SPU"),
820   ENUM_ENT(EM_V800,          "NEC V800 series"),
821   ENUM_ENT(EM_FR20,          "Fujistsu FR20"),
822   ENUM_ENT(EM_RH32,          "TRW RH-32"),
823   ENUM_ENT(EM_RCE,           "Motorola RCE"),
824   ENUM_ENT(EM_ARM,           "ARM"),
825   ENUM_ENT(EM_ALPHA,         "EM_ALPHA"),
826   ENUM_ENT(EM_SH,            "Hitachi SH"),
827   ENUM_ENT(EM_SPARCV9,       "Sparc v9"),
828   ENUM_ENT(EM_TRICORE,       "Siemens Tricore"),
829   ENUM_ENT(EM_ARC,           "ARC"),
830   ENUM_ENT(EM_H8_300,        "Hitachi H8/300"),
831   ENUM_ENT(EM_H8_300H,       "Hitachi H8/300H"),
832   ENUM_ENT(EM_H8S,           "Hitachi H8S"),
833   ENUM_ENT(EM_H8_500,        "Hitachi H8/500"),
834   ENUM_ENT(EM_IA_64,         "Intel IA-64"),
835   ENUM_ENT(EM_MIPS_X,        "Stanford MIPS-X"),
836   ENUM_ENT(EM_COLDFIRE,      "Motorola Coldfire"),
837   ENUM_ENT(EM_68HC12,        "Motorola MC68HC12 Microcontroller"),
838   ENUM_ENT(EM_MMA,           "Fujitsu Multimedia Accelerator"),
839   ENUM_ENT(EM_PCP,           "Siemens PCP"),
840   ENUM_ENT(EM_NCPU,          "Sony nCPU embedded RISC processor"),
841   ENUM_ENT(EM_NDR1,          "Denso NDR1 microprocesspr"),
842   ENUM_ENT(EM_STARCORE,      "Motorola Star*Core processor"),
843   ENUM_ENT(EM_ME16,          "Toyota ME16 processor"),
844   ENUM_ENT(EM_ST100,         "STMicroelectronics ST100 processor"),
845   ENUM_ENT(EM_TINYJ,         "Advanced Logic Corp. TinyJ embedded processor"),
846   ENUM_ENT(EM_X86_64,        "Advanced Micro Devices X86-64"),
847   ENUM_ENT(EM_PDSP,          "Sony DSP processor"),
848   ENUM_ENT(EM_PDP10,         "Digital Equipment Corp. PDP-10"),
849   ENUM_ENT(EM_PDP11,         "Digital Equipment Corp. PDP-11"),
850   ENUM_ENT(EM_FX66,          "Siemens FX66 microcontroller"),
851   ENUM_ENT(EM_ST9PLUS,       "STMicroelectronics ST9+ 8/16 bit microcontroller"),
852   ENUM_ENT(EM_ST7,           "STMicroelectronics ST7 8-bit microcontroller"),
853   ENUM_ENT(EM_68HC16,        "Motorola MC68HC16 Microcontroller"),
854   ENUM_ENT(EM_68HC11,        "Motorola MC68HC11 Microcontroller"),
855   ENUM_ENT(EM_68HC08,        "Motorola MC68HC08 Microcontroller"),
856   ENUM_ENT(EM_68HC05,        "Motorola MC68HC05 Microcontroller"),
857   ENUM_ENT(EM_SVX,           "Silicon Graphics SVx"),
858   ENUM_ENT(EM_ST19,          "STMicroelectronics ST19 8-bit microcontroller"),
859   ENUM_ENT(EM_VAX,           "Digital VAX"),
860   ENUM_ENT(EM_CRIS,          "Axis Communications 32-bit embedded processor"),
861   ENUM_ENT(EM_JAVELIN,       "Infineon Technologies 32-bit embedded cpu"),
862   ENUM_ENT(EM_FIREPATH,      "Element 14 64-bit DSP processor"),
863   ENUM_ENT(EM_ZSP,           "LSI Logic's 16-bit DSP processor"),
864   ENUM_ENT(EM_MMIX,          "Donald Knuth's educational 64-bit processor"),
865   ENUM_ENT(EM_HUANY,         "Harvard Universitys's machine-independent object format"),
866   ENUM_ENT(EM_PRISM,         "Vitesse Prism"),
867   ENUM_ENT(EM_AVR,           "Atmel AVR 8-bit microcontroller"),
868   ENUM_ENT(EM_FR30,          "Fujitsu FR30"),
869   ENUM_ENT(EM_D10V,          "Mitsubishi D10V"),
870   ENUM_ENT(EM_D30V,          "Mitsubishi D30V"),
871   ENUM_ENT(EM_V850,          "NEC v850"),
872   ENUM_ENT(EM_M32R,          "Renesas M32R (formerly Mitsubishi M32r)"),
873   ENUM_ENT(EM_MN10300,       "Matsushita MN10300"),
874   ENUM_ENT(EM_MN10200,       "Matsushita MN10200"),
875   ENUM_ENT(EM_PJ,            "picoJava"),
876   ENUM_ENT(EM_OPENRISC,      "OpenRISC 32-bit embedded processor"),
877   ENUM_ENT(EM_ARC_COMPACT,   "EM_ARC_COMPACT"),
878   ENUM_ENT(EM_XTENSA,        "Tensilica Xtensa Processor"),
879   ENUM_ENT(EM_VIDEOCORE,     "Alphamosaic VideoCore processor"),
880   ENUM_ENT(EM_TMM_GPP,       "Thompson Multimedia General Purpose Processor"),
881   ENUM_ENT(EM_NS32K,         "National Semiconductor 32000 series"),
882   ENUM_ENT(EM_TPC,           "Tenor Network TPC processor"),
883   ENUM_ENT(EM_SNP1K,         "EM_SNP1K"),
884   ENUM_ENT(EM_ST200,         "STMicroelectronics ST200 microcontroller"),
885   ENUM_ENT(EM_IP2K,          "Ubicom IP2xxx 8-bit microcontrollers"),
886   ENUM_ENT(EM_MAX,           "MAX Processor"),
887   ENUM_ENT(EM_CR,            "National Semiconductor CompactRISC"),
888   ENUM_ENT(EM_F2MC16,        "Fujitsu F2MC16"),
889   ENUM_ENT(EM_MSP430,        "Texas Instruments msp430 microcontroller"),
890   ENUM_ENT(EM_BLACKFIN,      "Analog Devices Blackfin"),
891   ENUM_ENT(EM_SE_C33,        "S1C33 Family of Seiko Epson processors"),
892   ENUM_ENT(EM_SEP,           "Sharp embedded microprocessor"),
893   ENUM_ENT(EM_ARCA,          "Arca RISC microprocessor"),
894   ENUM_ENT(EM_UNICORE,       "Unicore"),
895   ENUM_ENT(EM_EXCESS,        "eXcess 16/32/64-bit configurable embedded CPU"),
896   ENUM_ENT(EM_DXP,           "Icera Semiconductor Inc. Deep Execution Processor"),
897   ENUM_ENT(EM_ALTERA_NIOS2,  "Altera Nios"),
898   ENUM_ENT(EM_CRX,           "National Semiconductor CRX microprocessor"),
899   ENUM_ENT(EM_XGATE,         "Motorola XGATE embedded processor"),
900   ENUM_ENT(EM_C166,          "Infineon Technologies xc16x"),
901   ENUM_ENT(EM_M16C,          "Renesas M16C"),
902   ENUM_ENT(EM_DSPIC30F,      "Microchip Technology dsPIC30F Digital Signal Controller"),
903   ENUM_ENT(EM_CE,            "Freescale Communication Engine RISC core"),
904   ENUM_ENT(EM_M32C,          "Renesas M32C"),
905   ENUM_ENT(EM_TSK3000,       "Altium TSK3000 core"),
906   ENUM_ENT(EM_RS08,          "Freescale RS08 embedded processor"),
907   ENUM_ENT(EM_SHARC,         "EM_SHARC"),
908   ENUM_ENT(EM_ECOG2,         "Cyan Technology eCOG2 microprocessor"),
909   ENUM_ENT(EM_SCORE7,        "SUNPLUS S+Core"),
910   ENUM_ENT(EM_DSP24,         "New Japan Radio (NJR) 24-bit DSP Processor"),
911   ENUM_ENT(EM_VIDEOCORE3,    "Broadcom VideoCore III processor"),
912   ENUM_ENT(EM_LATTICEMICO32, "Lattice Mico32"),
913   ENUM_ENT(EM_SE_C17,        "Seiko Epson C17 family"),
914   ENUM_ENT(EM_TI_C6000,      "Texas Instruments TMS320C6000 DSP family"),
915   ENUM_ENT(EM_TI_C2000,      "Texas Instruments TMS320C2000 DSP family"),
916   ENUM_ENT(EM_TI_C5500,      "Texas Instruments TMS320C55x DSP family"),
917   ENUM_ENT(EM_MMDSP_PLUS,    "STMicroelectronics 64bit VLIW Data Signal Processor"),
918   ENUM_ENT(EM_CYPRESS_M8C,   "Cypress M8C microprocessor"),
919   ENUM_ENT(EM_R32C,          "Renesas R32C series microprocessors"),
920   ENUM_ENT(EM_TRIMEDIA,      "NXP Semiconductors TriMedia architecture family"),
921   ENUM_ENT(EM_HEXAGON,       "Qualcomm Hexagon"),
922   ENUM_ENT(EM_8051,          "Intel 8051 and variants"),
923   ENUM_ENT(EM_STXP7X,        "STMicroelectronics STxP7x family"),
924   ENUM_ENT(EM_NDS32,         "Andes Technology compact code size embedded RISC processor family"),
925   ENUM_ENT(EM_ECOG1,         "Cyan Technology eCOG1 microprocessor"),
926   ENUM_ENT(EM_ECOG1X,        "Cyan Technology eCOG1X family"),
927   ENUM_ENT(EM_MAXQ30,        "Dallas Semiconductor MAXQ30 Core microcontrollers"),
928   ENUM_ENT(EM_XIMO16,        "New Japan Radio (NJR) 16-bit DSP Processor"),
929   ENUM_ENT(EM_MANIK,         "M2000 Reconfigurable RISC Microprocessor"),
930   ENUM_ENT(EM_CRAYNV2,       "Cray Inc. NV2 vector architecture"),
931   ENUM_ENT(EM_RX,            "Renesas RX"),
932   ENUM_ENT(EM_METAG,         "Imagination Technologies Meta processor architecture"),
933   ENUM_ENT(EM_MCST_ELBRUS,   "MCST Elbrus general purpose hardware architecture"),
934   ENUM_ENT(EM_ECOG16,        "Cyan Technology eCOG16 family"),
935   ENUM_ENT(EM_CR16,          "Xilinx MicroBlaze"),
936   ENUM_ENT(EM_ETPU,          "Freescale Extended Time Processing Unit"),
937   ENUM_ENT(EM_SLE9X,         "Infineon Technologies SLE9X core"),
938   ENUM_ENT(EM_L10M,          "EM_L10M"),
939   ENUM_ENT(EM_K10M,          "EM_K10M"),
940   ENUM_ENT(EM_AARCH64,       "AArch64"),
941   ENUM_ENT(EM_AVR32,         "Atmel AVR 8-bit microcontroller"),
942   ENUM_ENT(EM_STM8,          "STMicroeletronics STM8 8-bit microcontroller"),
943   ENUM_ENT(EM_TILE64,        "Tilera TILE64 multicore architecture family"),
944   ENUM_ENT(EM_TILEPRO,       "Tilera TILEPro multicore architecture family"),
945   ENUM_ENT(EM_CUDA,          "NVIDIA CUDA architecture"),
946   ENUM_ENT(EM_TILEGX,        "Tilera TILE-Gx multicore architecture family"),
947   ENUM_ENT(EM_CLOUDSHIELD,   "EM_CLOUDSHIELD"),
948   ENUM_ENT(EM_COREA_1ST,     "EM_COREA_1ST"),
949   ENUM_ENT(EM_COREA_2ND,     "EM_COREA_2ND"),
950   ENUM_ENT(EM_ARC_COMPACT2,  "EM_ARC_COMPACT2"),
951   ENUM_ENT(EM_OPEN8,         "EM_OPEN8"),
952   ENUM_ENT(EM_RL78,          "Renesas RL78"),
953   ENUM_ENT(EM_VIDEOCORE5,    "Broadcom VideoCore V processor"),
954   ENUM_ENT(EM_78KOR,         "EM_78KOR"),
955   ENUM_ENT(EM_56800EX,       "EM_56800EX"),
956   ENUM_ENT(EM_AMDGPU,        "EM_AMDGPU"),
957   ENUM_ENT(EM_WEBASSEMBLY,   "EM_WEBASSEMBLY"),
958   ENUM_ENT(EM_LANAI,         "EM_LANAI"),
959 };
960 
961 static const EnumEntry<unsigned> ElfSymbolBindings[] = {
962     {"Local",  "LOCAL",  ELF::STB_LOCAL},
963     {"Global", "GLOBAL", ELF::STB_GLOBAL},
964     {"Weak",   "WEAK",   ELF::STB_WEAK},
965     {"Unique", "UNIQUE", ELF::STB_GNU_UNIQUE}};
966 
967 static const EnumEntry<unsigned> ElfSymbolVisibilities[] = {
968     {"DEFAULT",   "DEFAULT",   ELF::STV_DEFAULT},
969     {"INTERNAL",  "INTERNAL",  ELF::STV_INTERNAL},
970     {"HIDDEN",    "HIDDEN",    ELF::STV_HIDDEN},
971     {"PROTECTED", "PROTECTED", ELF::STV_PROTECTED}};
972 
973 static const EnumEntry<unsigned> ElfSymbolTypes[] = {
974     {"None",      "NOTYPE",  ELF::STT_NOTYPE},
975     {"Object",    "OBJECT",  ELF::STT_OBJECT},
976     {"Function",  "FUNC",    ELF::STT_FUNC},
977     {"Section",   "SECTION", ELF::STT_SECTION},
978     {"File",      "FILE",    ELF::STT_FILE},
979     {"Common",    "COMMON",  ELF::STT_COMMON},
980     {"TLS",       "TLS",     ELF::STT_TLS},
981     {"GNU_IFunc", "IFUNC",   ELF::STT_GNU_IFUNC}};
982 
983 static const EnumEntry<unsigned> AMDGPUSymbolTypes[] = {
984   { "AMDGPU_HSA_KERNEL",            ELF::STT_AMDGPU_HSA_KERNEL },
985   { "AMDGPU_HSA_INDIRECT_FUNCTION", ELF::STT_AMDGPU_HSA_INDIRECT_FUNCTION },
986   { "AMDGPU_HSA_METADATA",          ELF::STT_AMDGPU_HSA_METADATA }
987 };
988 
getElfSectionType(unsigned Arch,unsigned Type)989 static const char *getElfSectionType(unsigned Arch, unsigned Type) {
990   switch (Arch) {
991   case ELF::EM_ARM:
992     switch (Type) {
993     LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_EXIDX);
994     LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
995     LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
996     LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
997     LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
998     }
999   case ELF::EM_HEXAGON:
1000     switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
1001   case ELF::EM_X86_64:
1002     switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
1003   case ELF::EM_MIPS:
1004   case ELF::EM_MIPS_RS3_LE:
1005     switch (Type) {
1006     LLVM_READOBJ_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
1007     LLVM_READOBJ_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
1008     LLVM_READOBJ_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
1009     }
1010   }
1011 
1012   switch (Type) {
1013   LLVM_READOBJ_ENUM_CASE(ELF, SHT_NULL              );
1014   LLVM_READOBJ_ENUM_CASE(ELF, SHT_PROGBITS          );
1015   LLVM_READOBJ_ENUM_CASE(ELF, SHT_SYMTAB            );
1016   LLVM_READOBJ_ENUM_CASE(ELF, SHT_STRTAB            );
1017   LLVM_READOBJ_ENUM_CASE(ELF, SHT_RELA              );
1018   LLVM_READOBJ_ENUM_CASE(ELF, SHT_HASH              );
1019   LLVM_READOBJ_ENUM_CASE(ELF, SHT_DYNAMIC           );
1020   LLVM_READOBJ_ENUM_CASE(ELF, SHT_NOTE              );
1021   LLVM_READOBJ_ENUM_CASE(ELF, SHT_NOBITS            );
1022   LLVM_READOBJ_ENUM_CASE(ELF, SHT_REL               );
1023   LLVM_READOBJ_ENUM_CASE(ELF, SHT_SHLIB             );
1024   LLVM_READOBJ_ENUM_CASE(ELF, SHT_DYNSYM            );
1025   LLVM_READOBJ_ENUM_CASE(ELF, SHT_INIT_ARRAY        );
1026   LLVM_READOBJ_ENUM_CASE(ELF, SHT_FINI_ARRAY        );
1027   LLVM_READOBJ_ENUM_CASE(ELF, SHT_PREINIT_ARRAY     );
1028   LLVM_READOBJ_ENUM_CASE(ELF, SHT_GROUP             );
1029   LLVM_READOBJ_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX      );
1030   LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES    );
1031   LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_HASH          );
1032   LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_verdef        );
1033   LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_verneed       );
1034   LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_versym        );
1035   default: return "";
1036   }
1037 }
1038 
getGroupType(uint32_t Flag)1039 static const char *getGroupType(uint32_t Flag) {
1040   if (Flag & ELF::GRP_COMDAT)
1041     return "COMDAT";
1042   else
1043     return "(unknown)";
1044 }
1045 
1046 static const EnumEntry<unsigned> ElfSectionFlags[] = {
1047   ENUM_ENT(SHF_WRITE,            "W"),
1048   ENUM_ENT(SHF_ALLOC,            "A"),
1049   ENUM_ENT(SHF_EXCLUDE,          "E"),
1050   ENUM_ENT(SHF_EXECINSTR,        "X"),
1051   ENUM_ENT(SHF_MERGE,            "M"),
1052   ENUM_ENT(SHF_STRINGS,          "S"),
1053   ENUM_ENT(SHF_INFO_LINK,        "I"),
1054   ENUM_ENT(SHF_LINK_ORDER,       "L"),
1055   ENUM_ENT(SHF_OS_NONCONFORMING, "o"),
1056   ENUM_ENT(SHF_GROUP,            "G"),
1057   ENUM_ENT(SHF_TLS,              "T"),
1058   ENUM_ENT(SHF_MASKOS,           "o"),
1059   ENUM_ENT(SHF_MASKPROC,         "p"),
1060   ENUM_ENT_1(SHF_COMPRESSED),
1061 };
1062 
1063 static const EnumEntry<unsigned> ElfXCoreSectionFlags[] = {
1064   LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_CP_SECTION),
1065   LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_DP_SECTION)
1066 };
1067 
1068 static const EnumEntry<unsigned> ElfAMDGPUSectionFlags[] = {
1069   LLVM_READOBJ_ENUM_ENT(ELF, SHF_AMDGPU_HSA_GLOBAL),
1070   LLVM_READOBJ_ENUM_ENT(ELF, SHF_AMDGPU_HSA_READONLY),
1071   LLVM_READOBJ_ENUM_ENT(ELF, SHF_AMDGPU_HSA_CODE),
1072   LLVM_READOBJ_ENUM_ENT(ELF, SHF_AMDGPU_HSA_AGENT)
1073 };
1074 
1075 static const EnumEntry<unsigned> ElfHexagonSectionFlags[] = {
1076   LLVM_READOBJ_ENUM_ENT(ELF, SHF_HEX_GPREL)
1077 };
1078 
1079 static const EnumEntry<unsigned> ElfMipsSectionFlags[] = {
1080   LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NODUPES),
1081   LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NAMES  ),
1082   LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_LOCAL  ),
1083   LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NOSTRIP),
1084   LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_GPREL  ),
1085   LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_MERGE  ),
1086   LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_ADDR   ),
1087   LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_STRING )
1088 };
1089 
1090 static const EnumEntry<unsigned> ElfX86_64SectionFlags[] = {
1091   LLVM_READOBJ_ENUM_ENT(ELF, SHF_X86_64_LARGE)
1092 };
1093 
getGNUFlags(uint64_t Flags)1094 static std::string getGNUFlags(uint64_t Flags) {
1095   std::string Str;
1096   for (auto Entry : ElfSectionFlags) {
1097     uint64_t Flag = Entry.Value & Flags;
1098     Flags &= ~Entry.Value;
1099     switch (Flag) {
1100     case ELF::SHF_WRITE:
1101     case ELF::SHF_ALLOC:
1102     case ELF::SHF_EXECINSTR:
1103     case ELF::SHF_MERGE:
1104     case ELF::SHF_STRINGS:
1105     case ELF::SHF_INFO_LINK:
1106     case ELF::SHF_LINK_ORDER:
1107     case ELF::SHF_OS_NONCONFORMING:
1108     case ELF::SHF_GROUP:
1109     case ELF::SHF_TLS:
1110     case ELF::SHF_EXCLUDE:
1111       Str += Entry.AltName;
1112       break;
1113     default:
1114       if (Flag & ELF::SHF_MASKOS)
1115         Str += "o";
1116       else if (Flag & ELF::SHF_MASKPROC)
1117         Str += "p";
1118       else if (Flag)
1119         Str += "x";
1120     }
1121   }
1122   return Str;
1123 }
1124 
getElfSegmentType(unsigned Arch,unsigned Type)1125 static const char *getElfSegmentType(unsigned Arch, unsigned Type) {
1126   // Check potentially overlapped processor-specific
1127   // program header type.
1128   switch (Arch) {
1129   case ELF::EM_AMDGPU:
1130     switch (Type) {
1131     LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_GLOBAL_PROGRAM);
1132     LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_GLOBAL_AGENT);
1133     LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_READONLY_AGENT);
1134     LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_CODE_AGENT);
1135     }
1136   case ELF::EM_ARM:
1137     switch (Type) {
1138     LLVM_READOBJ_ENUM_CASE(ELF, PT_ARM_EXIDX);
1139     }
1140   case ELF::EM_MIPS:
1141   case ELF::EM_MIPS_RS3_LE:
1142     switch (Type) {
1143     LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_REGINFO);
1144     LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_RTPROC);
1145     LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_OPTIONS);
1146     LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_ABIFLAGS);
1147     }
1148   }
1149 
1150   switch (Type) {
1151   LLVM_READOBJ_ENUM_CASE(ELF, PT_NULL   );
1152   LLVM_READOBJ_ENUM_CASE(ELF, PT_LOAD   );
1153   LLVM_READOBJ_ENUM_CASE(ELF, PT_DYNAMIC);
1154   LLVM_READOBJ_ENUM_CASE(ELF, PT_INTERP );
1155   LLVM_READOBJ_ENUM_CASE(ELF, PT_NOTE   );
1156   LLVM_READOBJ_ENUM_CASE(ELF, PT_SHLIB  );
1157   LLVM_READOBJ_ENUM_CASE(ELF, PT_PHDR   );
1158   LLVM_READOBJ_ENUM_CASE(ELF, PT_TLS    );
1159 
1160   LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_EH_FRAME);
1161   LLVM_READOBJ_ENUM_CASE(ELF, PT_SUNW_UNWIND);
1162 
1163   LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_STACK);
1164   LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_RELRO);
1165   default: return "";
1166   }
1167 }
1168 
getElfPtType(unsigned Arch,unsigned Type)1169 static std::string getElfPtType(unsigned Arch, unsigned Type) {
1170   switch (Type) {
1171     LLVM_READOBJ_PHDR_ENUM(ELF, PT_NULL)
1172     LLVM_READOBJ_PHDR_ENUM(ELF, PT_LOAD)
1173     LLVM_READOBJ_PHDR_ENUM(ELF, PT_DYNAMIC)
1174     LLVM_READOBJ_PHDR_ENUM(ELF, PT_INTERP)
1175     LLVM_READOBJ_PHDR_ENUM(ELF, PT_NOTE)
1176     LLVM_READOBJ_PHDR_ENUM(ELF, PT_SHLIB)
1177     LLVM_READOBJ_PHDR_ENUM(ELF, PT_PHDR)
1178     LLVM_READOBJ_PHDR_ENUM(ELF, PT_TLS)
1179     LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_EH_FRAME)
1180     LLVM_READOBJ_PHDR_ENUM(ELF, PT_SUNW_UNWIND)
1181     LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_STACK)
1182     LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_RELRO)
1183   default:
1184     // All machine specific PT_* types
1185     switch (Arch) {
1186     case ELF::EM_AMDGPU:
1187       switch (Type) {
1188         LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_GLOBAL_PROGRAM);
1189         LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_GLOBAL_AGENT);
1190         LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_READONLY_AGENT);
1191         LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_CODE_AGENT);
1192       }
1193       return "";
1194     case ELF::EM_ARM:
1195       if (Type == ELF::PT_ARM_EXIDX)
1196         return "EXIDX";
1197       return "";
1198     case ELF::EM_MIPS:
1199     case ELF::EM_MIPS_RS3_LE:
1200       switch (Type) {
1201       case PT_MIPS_REGINFO:
1202         return "REGINFO";
1203       case PT_MIPS_RTPROC:
1204         return "RTPROC";
1205       case PT_MIPS_OPTIONS:
1206         return "OPTIONS";
1207       case PT_MIPS_ABIFLAGS:
1208         return "ABIFLAGS";
1209       }
1210       return "";
1211     }
1212   }
1213   return std::string("<unknown>: ") + to_string(format_hex(Type, 1));
1214 }
1215 
1216 static const EnumEntry<unsigned> ElfSegmentFlags[] = {
1217   LLVM_READOBJ_ENUM_ENT(ELF, PF_X),
1218   LLVM_READOBJ_ENUM_ENT(ELF, PF_W),
1219   LLVM_READOBJ_ENUM_ENT(ELF, PF_R)
1220 };
1221 
1222 static const EnumEntry<unsigned> ElfHeaderMipsFlags[] = {
1223   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_NOREORDER),
1224   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_PIC),
1225   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_CPIC),
1226   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI2),
1227   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_32BITMODE),
1228   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_FP64),
1229   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_NAN2008),
1230   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI_O32),
1231   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI_O64),
1232   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI_EABI32),
1233   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI_EABI64),
1234   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_3900),
1235   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4010),
1236   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4100),
1237   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4650),
1238   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4120),
1239   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4111),
1240   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_SB1),
1241   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_OCTEON),
1242   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_XLR),
1243   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_OCTEON2),
1244   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_OCTEON3),
1245   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_5400),
1246   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_5900),
1247   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_5500),
1248   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_9000),
1249   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_LS2E),
1250   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_LS2F),
1251   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_LS3A),
1252   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MICROMIPS),
1253   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_ASE_M16),
1254   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_ASE_MDMX),
1255   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_1),
1256   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_2),
1257   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_3),
1258   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_4),
1259   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_5),
1260   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_32),
1261   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_64),
1262   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_32R2),
1263   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_64R2),
1264   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_32R6),
1265   LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_64R6)
1266 };
1267 
1268 static const EnumEntry<unsigned> ElfSymOtherFlags[] = {
1269   LLVM_READOBJ_ENUM_ENT(ELF, STV_INTERNAL),
1270   LLVM_READOBJ_ENUM_ENT(ELF, STV_HIDDEN),
1271   LLVM_READOBJ_ENUM_ENT(ELF, STV_PROTECTED)
1272 };
1273 
1274 static const EnumEntry<unsigned> ElfMipsSymOtherFlags[] = {
1275   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL),
1276   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT),
1277   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PIC),
1278   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MICROMIPS)
1279 };
1280 
1281 static const EnumEntry<unsigned> ElfMips16SymOtherFlags[] = {
1282   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL),
1283   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT),
1284   LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MIPS16)
1285 };
1286 
getElfMipsOptionsOdkType(unsigned Odk)1287 static const char *getElfMipsOptionsOdkType(unsigned Odk) {
1288   switch (Odk) {
1289   LLVM_READOBJ_ENUM_CASE(ELF, ODK_NULL);
1290   LLVM_READOBJ_ENUM_CASE(ELF, ODK_REGINFO);
1291   LLVM_READOBJ_ENUM_CASE(ELF, ODK_EXCEPTIONS);
1292   LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAD);
1293   LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWPATCH);
1294   LLVM_READOBJ_ENUM_CASE(ELF, ODK_FILL);
1295   LLVM_READOBJ_ENUM_CASE(ELF, ODK_TAGS);
1296   LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWAND);
1297   LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWOR);
1298   LLVM_READOBJ_ENUM_CASE(ELF, ODK_GP_GROUP);
1299   LLVM_READOBJ_ENUM_CASE(ELF, ODK_IDENT);
1300   LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAGESIZE);
1301   default:
1302     return "Unknown";
1303   }
1304 }
1305 
1306 template <typename ELFT>
ELFDumper(const ELFFile<ELFT> * Obj,ScopedPrinter & Writer)1307 ELFDumper<ELFT>::ELFDumper(const ELFFile<ELFT> *Obj, ScopedPrinter &Writer)
1308     : ObjDumper(Writer), Obj(Obj) {
1309 
1310   SmallVector<const Elf_Phdr *, 4> LoadSegments;
1311   for (const Elf_Phdr &Phdr : Obj->program_headers()) {
1312     if (Phdr.p_type == ELF::PT_DYNAMIC) {
1313       DynamicTable = createDRIFrom(&Phdr, sizeof(Elf_Dyn));
1314       continue;
1315     }
1316     if (Phdr.p_type != ELF::PT_LOAD || Phdr.p_filesz == 0)
1317       continue;
1318     LoadSegments.push_back(&Phdr);
1319   }
1320 
1321   for (const Elf_Shdr &Sec : Obj->sections()) {
1322     switch (Sec.sh_type) {
1323     case ELF::SHT_SYMTAB:
1324       if (DotSymtabSec != nullptr)
1325         reportError("Multilpe SHT_SYMTAB");
1326       DotSymtabSec = &Sec;
1327       break;
1328     case ELF::SHT_DYNSYM:
1329       if (DynSymRegion.Size)
1330         reportError("Multilpe SHT_DYNSYM");
1331       DynSymRegion = createDRIFrom(&Sec);
1332       // This is only used (if Elf_Shdr present)for naming section in GNU style
1333       DynSymtabName = unwrapOrError(Obj->getSectionName(&Sec));
1334       break;
1335     case ELF::SHT_SYMTAB_SHNDX:
1336       ShndxTable = unwrapOrError(Obj->getSHNDXTable(Sec));
1337       break;
1338     case ELF::SHT_GNU_versym:
1339       if (dot_gnu_version_sec != nullptr)
1340         reportError("Multiple SHT_GNU_versym");
1341       dot_gnu_version_sec = &Sec;
1342       break;
1343     case ELF::SHT_GNU_verdef:
1344       if (dot_gnu_version_d_sec != nullptr)
1345         reportError("Multiple SHT_GNU_verdef");
1346       dot_gnu_version_d_sec = &Sec;
1347       break;
1348     case ELF::SHT_GNU_verneed:
1349       if (dot_gnu_version_r_sec != nullptr)
1350         reportError("Multilpe SHT_GNU_verneed");
1351       dot_gnu_version_r_sec = &Sec;
1352       break;
1353     }
1354   }
1355 
1356   parseDynamicTable(LoadSegments);
1357 
1358   if (opts::Output == opts::GNU)
1359     ELFDumperStyle.reset(new GNUStyle<ELFT>(Writer, this));
1360   else
1361     ELFDumperStyle.reset(new LLVMStyle<ELFT>(Writer, this));
1362 }
1363 
1364 template <typename ELFT>
parseDynamicTable(ArrayRef<const Elf_Phdr * > LoadSegments)1365 void ELFDumper<ELFT>::parseDynamicTable(
1366     ArrayRef<const Elf_Phdr *> LoadSegments) {
1367   auto toMappedAddr = [&](uint64_t VAddr) -> const uint8_t * {
1368     const Elf_Phdr *const *I = std::upper_bound(
1369         LoadSegments.begin(), LoadSegments.end(), VAddr, compareAddr<ELFT>);
1370     if (I == LoadSegments.begin())
1371       report_fatal_error("Virtual address is not in any segment");
1372     --I;
1373     const Elf_Phdr &Phdr = **I;
1374     uint64_t Delta = VAddr - Phdr.p_vaddr;
1375     if (Delta >= Phdr.p_filesz)
1376       report_fatal_error("Virtual address is not in any segment");
1377     return Obj->base() + Phdr.p_offset + Delta;
1378   };
1379 
1380   uint64_t SONameOffset = 0;
1381   const char *StringTableBegin = nullptr;
1382   uint64_t StringTableSize = 0;
1383   for (const Elf_Dyn &Dyn : dynamic_table()) {
1384     switch (Dyn.d_tag) {
1385     case ELF::DT_HASH:
1386       HashTable =
1387           reinterpret_cast<const Elf_Hash *>(toMappedAddr(Dyn.getPtr()));
1388       break;
1389     case ELF::DT_GNU_HASH:
1390       GnuHashTable =
1391           reinterpret_cast<const Elf_GnuHash *>(toMappedAddr(Dyn.getPtr()));
1392       break;
1393     case ELF::DT_STRTAB:
1394       StringTableBegin = (const char *)toMappedAddr(Dyn.getPtr());
1395       break;
1396     case ELF::DT_STRSZ:
1397       StringTableSize = Dyn.getVal();
1398       break;
1399     case ELF::DT_SYMTAB:
1400       DynSymRegion.Addr = toMappedAddr(Dyn.getPtr());
1401       DynSymRegion.EntSize = sizeof(Elf_Sym);
1402       break;
1403     case ELF::DT_RELA:
1404       DynRelaRegion.Addr = toMappedAddr(Dyn.getPtr());
1405       break;
1406     case ELF::DT_RELASZ:
1407       DynRelaRegion.Size = Dyn.getVal();
1408       break;
1409     case ELF::DT_RELAENT:
1410       DynRelaRegion.EntSize = Dyn.getVal();
1411       break;
1412     case ELF::DT_SONAME:
1413       SONameOffset = Dyn.getVal();
1414       break;
1415     case ELF::DT_REL:
1416       DynRelRegion.Addr = toMappedAddr(Dyn.getPtr());
1417       break;
1418     case ELF::DT_RELSZ:
1419       DynRelRegion.Size = Dyn.getVal();
1420       break;
1421     case ELF::DT_RELENT:
1422       DynRelRegion.EntSize = Dyn.getVal();
1423       break;
1424     case ELF::DT_PLTREL:
1425       if (Dyn.getVal() == DT_REL)
1426         DynPLTRelRegion.EntSize = sizeof(Elf_Rel);
1427       else if (Dyn.getVal() == DT_RELA)
1428         DynPLTRelRegion.EntSize = sizeof(Elf_Rela);
1429       else
1430         reportError(Twine("unknown DT_PLTREL value of ") +
1431                     Twine((uint64_t)Dyn.getVal()));
1432       break;
1433     case ELF::DT_JMPREL:
1434       DynPLTRelRegion.Addr = toMappedAddr(Dyn.getPtr());
1435       break;
1436     case ELF::DT_PLTRELSZ:
1437       DynPLTRelRegion.Size = Dyn.getVal();
1438       break;
1439     }
1440   }
1441   if (StringTableBegin)
1442     DynamicStringTable = StringRef(StringTableBegin, StringTableSize);
1443   if (SONameOffset)
1444     SOName = getDynamicString(SONameOffset);
1445 }
1446 
1447 template <typename ELFT>
dyn_rels() const1448 typename ELFDumper<ELFT>::Elf_Rel_Range ELFDumper<ELFT>::dyn_rels() const {
1449   return DynRelRegion.getAsArrayRef<Elf_Rel>();
1450 }
1451 
1452 template <typename ELFT>
dyn_relas() const1453 typename ELFDumper<ELFT>::Elf_Rela_Range ELFDumper<ELFT>::dyn_relas() const {
1454   return DynRelaRegion.getAsArrayRef<Elf_Rela>();
1455 }
1456 
1457 template<class ELFT>
printFileHeaders()1458 void ELFDumper<ELFT>::printFileHeaders() {
1459   ELFDumperStyle->printFileHeaders(Obj);
1460 }
1461 
1462 template<class ELFT>
printSections()1463 void ELFDumper<ELFT>::printSections() {
1464   ELFDumperStyle->printSections(Obj);
1465 }
1466 
1467 template<class ELFT>
printRelocations()1468 void ELFDumper<ELFT>::printRelocations() {
1469   ELFDumperStyle->printRelocations(Obj);
1470 }
1471 
printProgramHeaders()1472 template <class ELFT> void ELFDumper<ELFT>::printProgramHeaders() {
1473   ELFDumperStyle->printProgramHeaders(Obj);
1474 }
1475 
printDynamicRelocations()1476 template <class ELFT> void ELFDumper<ELFT>::printDynamicRelocations() {
1477   ELFDumperStyle->printDynamicRelocations(Obj);
1478 }
1479 
1480 template<class ELFT>
printSymbols()1481 void ELFDumper<ELFT>::printSymbols() {
1482   ELFDumperStyle->printSymbols(Obj);
1483 }
1484 
1485 template<class ELFT>
printDynamicSymbols()1486 void ELFDumper<ELFT>::printDynamicSymbols() {
1487   ELFDumperStyle->printDynamicSymbols(Obj);
1488 }
1489 
printHashHistogram()1490 template <class ELFT> void ELFDumper<ELFT>::printHashHistogram() {
1491   ELFDumperStyle->printHashHistogram(Obj);
1492 }
1493 #define LLVM_READOBJ_TYPE_CASE(name) \
1494   case DT_##name: return #name
1495 
getTypeString(uint64_t Type)1496 static const char *getTypeString(uint64_t Type) {
1497   switch (Type) {
1498   LLVM_READOBJ_TYPE_CASE(BIND_NOW);
1499   LLVM_READOBJ_TYPE_CASE(DEBUG);
1500   LLVM_READOBJ_TYPE_CASE(FINI);
1501   LLVM_READOBJ_TYPE_CASE(FINI_ARRAY);
1502   LLVM_READOBJ_TYPE_CASE(FINI_ARRAYSZ);
1503   LLVM_READOBJ_TYPE_CASE(FLAGS);
1504   LLVM_READOBJ_TYPE_CASE(FLAGS_1);
1505   LLVM_READOBJ_TYPE_CASE(HASH);
1506   LLVM_READOBJ_TYPE_CASE(INIT);
1507   LLVM_READOBJ_TYPE_CASE(INIT_ARRAY);
1508   LLVM_READOBJ_TYPE_CASE(INIT_ARRAYSZ);
1509   LLVM_READOBJ_TYPE_CASE(PREINIT_ARRAY);
1510   LLVM_READOBJ_TYPE_CASE(PREINIT_ARRAYSZ);
1511   LLVM_READOBJ_TYPE_CASE(JMPREL);
1512   LLVM_READOBJ_TYPE_CASE(NEEDED);
1513   LLVM_READOBJ_TYPE_CASE(NULL);
1514   LLVM_READOBJ_TYPE_CASE(PLTGOT);
1515   LLVM_READOBJ_TYPE_CASE(PLTREL);
1516   LLVM_READOBJ_TYPE_CASE(PLTRELSZ);
1517   LLVM_READOBJ_TYPE_CASE(REL);
1518   LLVM_READOBJ_TYPE_CASE(RELA);
1519   LLVM_READOBJ_TYPE_CASE(RELENT);
1520   LLVM_READOBJ_TYPE_CASE(RELSZ);
1521   LLVM_READOBJ_TYPE_CASE(RELAENT);
1522   LLVM_READOBJ_TYPE_CASE(RELASZ);
1523   LLVM_READOBJ_TYPE_CASE(RPATH);
1524   LLVM_READOBJ_TYPE_CASE(RUNPATH);
1525   LLVM_READOBJ_TYPE_CASE(SONAME);
1526   LLVM_READOBJ_TYPE_CASE(STRSZ);
1527   LLVM_READOBJ_TYPE_CASE(STRTAB);
1528   LLVM_READOBJ_TYPE_CASE(SYMBOLIC);
1529   LLVM_READOBJ_TYPE_CASE(SYMENT);
1530   LLVM_READOBJ_TYPE_CASE(SYMTAB);
1531   LLVM_READOBJ_TYPE_CASE(TEXTREL);
1532   LLVM_READOBJ_TYPE_CASE(VERDEF);
1533   LLVM_READOBJ_TYPE_CASE(VERDEFNUM);
1534   LLVM_READOBJ_TYPE_CASE(VERNEED);
1535   LLVM_READOBJ_TYPE_CASE(VERNEEDNUM);
1536   LLVM_READOBJ_TYPE_CASE(VERSYM);
1537   LLVM_READOBJ_TYPE_CASE(RELACOUNT);
1538   LLVM_READOBJ_TYPE_CASE(RELCOUNT);
1539   LLVM_READOBJ_TYPE_CASE(GNU_HASH);
1540   LLVM_READOBJ_TYPE_CASE(TLSDESC_PLT);
1541   LLVM_READOBJ_TYPE_CASE(TLSDESC_GOT);
1542   LLVM_READOBJ_TYPE_CASE(MIPS_RLD_VERSION);
1543   LLVM_READOBJ_TYPE_CASE(MIPS_RLD_MAP_REL);
1544   LLVM_READOBJ_TYPE_CASE(MIPS_FLAGS);
1545   LLVM_READOBJ_TYPE_CASE(MIPS_BASE_ADDRESS);
1546   LLVM_READOBJ_TYPE_CASE(MIPS_LOCAL_GOTNO);
1547   LLVM_READOBJ_TYPE_CASE(MIPS_SYMTABNO);
1548   LLVM_READOBJ_TYPE_CASE(MIPS_UNREFEXTNO);
1549   LLVM_READOBJ_TYPE_CASE(MIPS_GOTSYM);
1550   LLVM_READOBJ_TYPE_CASE(MIPS_RLD_MAP);
1551   LLVM_READOBJ_TYPE_CASE(MIPS_PLTGOT);
1552   LLVM_READOBJ_TYPE_CASE(MIPS_OPTIONS);
1553   default: return "unknown";
1554   }
1555 }
1556 
1557 #undef LLVM_READOBJ_TYPE_CASE
1558 
1559 #define LLVM_READOBJ_DT_FLAG_ENT(prefix, enum) \
1560   { #enum, prefix##_##enum }
1561 
1562 static const EnumEntry<unsigned> ElfDynamicDTFlags[] = {
1563   LLVM_READOBJ_DT_FLAG_ENT(DF, ORIGIN),
1564   LLVM_READOBJ_DT_FLAG_ENT(DF, SYMBOLIC),
1565   LLVM_READOBJ_DT_FLAG_ENT(DF, TEXTREL),
1566   LLVM_READOBJ_DT_FLAG_ENT(DF, BIND_NOW),
1567   LLVM_READOBJ_DT_FLAG_ENT(DF, STATIC_TLS)
1568 };
1569 
1570 static const EnumEntry<unsigned> ElfDynamicDTFlags1[] = {
1571   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOW),
1572   LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAL),
1573   LLVM_READOBJ_DT_FLAG_ENT(DF_1, GROUP),
1574   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODELETE),
1575   LLVM_READOBJ_DT_FLAG_ENT(DF_1, LOADFLTR),
1576   LLVM_READOBJ_DT_FLAG_ENT(DF_1, INITFIRST),
1577   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOOPEN),
1578   LLVM_READOBJ_DT_FLAG_ENT(DF_1, ORIGIN),
1579   LLVM_READOBJ_DT_FLAG_ENT(DF_1, DIRECT),
1580   LLVM_READOBJ_DT_FLAG_ENT(DF_1, TRANS),
1581   LLVM_READOBJ_DT_FLAG_ENT(DF_1, INTERPOSE),
1582   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODEFLIB),
1583   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODUMP),
1584   LLVM_READOBJ_DT_FLAG_ENT(DF_1, CONFALT),
1585   LLVM_READOBJ_DT_FLAG_ENT(DF_1, ENDFILTEE),
1586   LLVM_READOBJ_DT_FLAG_ENT(DF_1, DISPRELDNE),
1587   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODIRECT),
1588   LLVM_READOBJ_DT_FLAG_ENT(DF_1, IGNMULDEF),
1589   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOKSYMS),
1590   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOHDR),
1591   LLVM_READOBJ_DT_FLAG_ENT(DF_1, EDITED),
1592   LLVM_READOBJ_DT_FLAG_ENT(DF_1, NORELOC),
1593   LLVM_READOBJ_DT_FLAG_ENT(DF_1, SYMINTPOSE),
1594   LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAUDIT),
1595   LLVM_READOBJ_DT_FLAG_ENT(DF_1, SINGLETON)
1596 };
1597 
1598 static const EnumEntry<unsigned> ElfDynamicDTMipsFlags[] = {
1599   LLVM_READOBJ_DT_FLAG_ENT(RHF, NONE),
1600   LLVM_READOBJ_DT_FLAG_ENT(RHF, QUICKSTART),
1601   LLVM_READOBJ_DT_FLAG_ENT(RHF, NOTPOT),
1602   LLVM_READOBJ_DT_FLAG_ENT(RHS, NO_LIBRARY_REPLACEMENT),
1603   LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_MOVE),
1604   LLVM_READOBJ_DT_FLAG_ENT(RHF, SGI_ONLY),
1605   LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_INIT),
1606   LLVM_READOBJ_DT_FLAG_ENT(RHF, DELTA_C_PLUS_PLUS),
1607   LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_START_INIT),
1608   LLVM_READOBJ_DT_FLAG_ENT(RHF, PIXIE),
1609   LLVM_READOBJ_DT_FLAG_ENT(RHF, DEFAULT_DELAY_LOAD),
1610   LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTART),
1611   LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTARTED),
1612   LLVM_READOBJ_DT_FLAG_ENT(RHF, CORD),
1613   LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_UNRES_UNDEF),
1614   LLVM_READOBJ_DT_FLAG_ENT(RHF, RLD_ORDER_SAFE)
1615 };
1616 
1617 #undef LLVM_READOBJ_DT_FLAG_ENT
1618 
1619 template <typename T, typename TFlag>
printFlags(T Value,ArrayRef<EnumEntry<TFlag>> Flags,raw_ostream & OS)1620 void printFlags(T Value, ArrayRef<EnumEntry<TFlag>> Flags, raw_ostream &OS) {
1621   typedef EnumEntry<TFlag> FlagEntry;
1622   typedef SmallVector<FlagEntry, 10> FlagVector;
1623   FlagVector SetFlags;
1624 
1625   for (const auto &Flag : Flags) {
1626     if (Flag.Value == 0)
1627       continue;
1628 
1629     if ((Value & Flag.Value) == Flag.Value)
1630       SetFlags.push_back(Flag);
1631   }
1632 
1633   for (const auto &Flag : SetFlags) {
1634     OS << Flag.Name << " ";
1635   }
1636 }
1637 
1638 template <class ELFT>
getDynamicString(uint64_t Value) const1639 StringRef ELFDumper<ELFT>::getDynamicString(uint64_t Value) const {
1640   if (Value >= DynamicStringTable.size())
1641     reportError("Invalid dynamic string table reference");
1642   return StringRef(DynamicStringTable.data() + Value);
1643 }
1644 
1645 template <class ELFT>
printValue(uint64_t Type,uint64_t Value)1646 void ELFDumper<ELFT>::printValue(uint64_t Type, uint64_t Value) {
1647   raw_ostream &OS = W.getOStream();
1648   const char* ConvChar = (opts::Output == opts::GNU) ? "0x%" PRIx64 : "0x%" PRIX64;
1649   switch (Type) {
1650   case DT_PLTREL:
1651     if (Value == DT_REL) {
1652       OS << "REL";
1653       break;
1654     } else if (Value == DT_RELA) {
1655       OS << "RELA";
1656       break;
1657     }
1658   // Fallthrough.
1659   case DT_PLTGOT:
1660   case DT_HASH:
1661   case DT_STRTAB:
1662   case DT_SYMTAB:
1663   case DT_RELA:
1664   case DT_INIT:
1665   case DT_FINI:
1666   case DT_REL:
1667   case DT_JMPREL:
1668   case DT_INIT_ARRAY:
1669   case DT_FINI_ARRAY:
1670   case DT_PREINIT_ARRAY:
1671   case DT_DEBUG:
1672   case DT_VERDEF:
1673   case DT_VERNEED:
1674   case DT_VERSYM:
1675   case DT_GNU_HASH:
1676   case DT_NULL:
1677   case DT_MIPS_BASE_ADDRESS:
1678   case DT_MIPS_GOTSYM:
1679   case DT_MIPS_RLD_MAP:
1680   case DT_MIPS_RLD_MAP_REL:
1681   case DT_MIPS_PLTGOT:
1682   case DT_MIPS_OPTIONS:
1683     OS << format(ConvChar, Value);
1684     break;
1685   case DT_RELACOUNT:
1686   case DT_RELCOUNT:
1687   case DT_VERDEFNUM:
1688   case DT_VERNEEDNUM:
1689   case DT_MIPS_RLD_VERSION:
1690   case DT_MIPS_LOCAL_GOTNO:
1691   case DT_MIPS_SYMTABNO:
1692   case DT_MIPS_UNREFEXTNO:
1693     OS << Value;
1694     break;
1695   case DT_PLTRELSZ:
1696   case DT_RELASZ:
1697   case DT_RELAENT:
1698   case DT_STRSZ:
1699   case DT_SYMENT:
1700   case DT_RELSZ:
1701   case DT_RELENT:
1702   case DT_INIT_ARRAYSZ:
1703   case DT_FINI_ARRAYSZ:
1704   case DT_PREINIT_ARRAYSZ:
1705     OS << Value << " (bytes)";
1706     break;
1707   case DT_NEEDED:
1708     OS << "SharedLibrary (" << getDynamicString(Value) << ")";
1709     break;
1710   case DT_SONAME:
1711     OS << "LibrarySoname (" << getDynamicString(Value) << ")";
1712     break;
1713   case DT_RPATH:
1714   case DT_RUNPATH:
1715     OS << getDynamicString(Value);
1716     break;
1717   case DT_MIPS_FLAGS:
1718     printFlags(Value, makeArrayRef(ElfDynamicDTMipsFlags), OS);
1719     break;
1720   case DT_FLAGS:
1721     printFlags(Value, makeArrayRef(ElfDynamicDTFlags), OS);
1722     break;
1723   case DT_FLAGS_1:
1724     printFlags(Value, makeArrayRef(ElfDynamicDTFlags1), OS);
1725     break;
1726   default:
1727     OS << format(ConvChar, Value);
1728     break;
1729   }
1730 }
1731 
1732 template<class ELFT>
printUnwindInfo()1733 void ELFDumper<ELFT>::printUnwindInfo() {
1734   W.startLine() << "UnwindInfo not implemented.\n";
1735 }
1736 
1737 namespace {
printUnwindInfo()1738 template <> void ELFDumper<ELFType<support::little, false>>::printUnwindInfo() {
1739   const unsigned Machine = Obj->getHeader()->e_machine;
1740   if (Machine == EM_ARM) {
1741     ARM::EHABI::PrinterContext<ELFType<support::little, false>> Ctx(
1742         W, Obj, DotSymtabSec);
1743     return Ctx.PrintUnwindInformation();
1744   }
1745   W.startLine() << "UnwindInfo not implemented.\n";
1746 }
1747 }
1748 
1749 template<class ELFT>
printDynamicTable()1750 void ELFDumper<ELFT>::printDynamicTable() {
1751   auto I = dynamic_table().begin();
1752   auto E = dynamic_table().end();
1753 
1754   if (I == E)
1755     return;
1756 
1757   --E;
1758   while (I != E && E->getTag() == ELF::DT_NULL)
1759     --E;
1760   if (E->getTag() != ELF::DT_NULL)
1761     ++E;
1762   ++E;
1763 
1764   ptrdiff_t Total = std::distance(I, E);
1765   if (Total == 0)
1766     return;
1767 
1768   raw_ostream &OS = W.getOStream();
1769   W.startLine() << "DynamicSection [ (" << Total << " entries)\n";
1770 
1771   bool Is64 = ELFT::Is64Bits;
1772 
1773   W.startLine()
1774      << "  Tag" << (Is64 ? "                " : "        ") << "Type"
1775      << "                 " << "Name/Value\n";
1776   while (I != E) {
1777     const Elf_Dyn &Entry = *I;
1778     uintX_t Tag = Entry.getTag();
1779     ++I;
1780     W.startLine() << "  " << format_hex(Tag, Is64 ? 18 : 10, opts::Output != opts::GNU) << " "
1781                   << format("%-21s", getTypeString(Tag));
1782     printValue(Tag, Entry.getVal());
1783     OS << "\n";
1784   }
1785 
1786   W.startLine() << "]\n";
1787 }
1788 
1789 template<class ELFT>
printNeededLibraries()1790 void ELFDumper<ELFT>::printNeededLibraries() {
1791   ListScope D(W, "NeededLibraries");
1792 
1793   typedef std::vector<StringRef> LibsTy;
1794   LibsTy Libs;
1795 
1796   for (const auto &Entry : dynamic_table())
1797     if (Entry.d_tag == ELF::DT_NEEDED)
1798       Libs.push_back(getDynamicString(Entry.d_un.d_val));
1799 
1800   std::stable_sort(Libs.begin(), Libs.end());
1801 
1802   for (const auto &L : Libs) {
1803     outs() << "  " << L << "\n";
1804   }
1805 }
1806 
1807 
1808 template <typename ELFT>
printHashTable()1809 void ELFDumper<ELFT>::printHashTable() {
1810   DictScope D(W, "HashTable");
1811   if (!HashTable)
1812     return;
1813   W.printNumber("Num Buckets", HashTable->nbucket);
1814   W.printNumber("Num Chains", HashTable->nchain);
1815   W.printList("Buckets", HashTable->buckets());
1816   W.printList("Chains", HashTable->chains());
1817 }
1818 
1819 template <typename ELFT>
printGnuHashTable()1820 void ELFDumper<ELFT>::printGnuHashTable() {
1821   DictScope D(W, "GnuHashTable");
1822   if (!GnuHashTable)
1823     return;
1824   W.printNumber("Num Buckets", GnuHashTable->nbuckets);
1825   W.printNumber("First Hashed Symbol Index", GnuHashTable->symndx);
1826   W.printNumber("Num Mask Words", GnuHashTable->maskwords);
1827   W.printNumber("Shift Count", GnuHashTable->shift2);
1828   W.printHexList("Bloom Filter", GnuHashTable->filter());
1829   W.printList("Buckets", GnuHashTable->buckets());
1830   Elf_Sym_Range Syms = dynamic_symbols();
1831   unsigned NumSyms = std::distance(Syms.begin(), Syms.end());
1832   if (!NumSyms)
1833     reportError("No dynamic symbol section");
1834   W.printHexList("Values", GnuHashTable->values(NumSyms));
1835 }
1836 
printLoadName()1837 template <typename ELFT> void ELFDumper<ELFT>::printLoadName() {
1838   outs() << "LoadName: " << SOName << '\n';
1839 }
1840 
1841 template <class ELFT>
printAttributes()1842 void ELFDumper<ELFT>::printAttributes() {
1843   W.startLine() << "Attributes not implemented.\n";
1844 }
1845 
1846 namespace {
printAttributes()1847 template <> void ELFDumper<ELFType<support::little, false>>::printAttributes() {
1848   if (Obj->getHeader()->e_machine != EM_ARM) {
1849     W.startLine() << "Attributes not implemented.\n";
1850     return;
1851   }
1852 
1853   DictScope BA(W, "BuildAttributes");
1854   for (const ELFO::Elf_Shdr &Sec : Obj->sections()) {
1855     if (Sec.sh_type != ELF::SHT_ARM_ATTRIBUTES)
1856       continue;
1857 
1858     ArrayRef<uint8_t> Contents = unwrapOrError(Obj->getSectionContents(&Sec));
1859     if (Contents[0] != ARMBuildAttrs::Format_Version) {
1860       errs() << "unrecognised FormatVersion: 0x" << utohexstr(Contents[0])
1861              << '\n';
1862       continue;
1863     }
1864 
1865     W.printHex("FormatVersion", Contents[0]);
1866     if (Contents.size() == 1)
1867       continue;
1868 
1869     ARMAttributeParser(W).Parse(Contents);
1870   }
1871 }
1872 }
1873 
1874 namespace {
1875 template <class ELFT> class MipsGOTParser {
1876 public:
1877   typedef object::ELFFile<ELFT> ELFO;
1878   typedef typename ELFO::Elf_Shdr Elf_Shdr;
1879   typedef typename ELFO::Elf_Sym Elf_Sym;
1880   typedef typename ELFO::Elf_Dyn_Range Elf_Dyn_Range;
1881   typedef typename ELFO::Elf_Addr GOTEntry;
1882   typedef typename ELFO::Elf_Rel Elf_Rel;
1883   typedef typename ELFO::Elf_Rela Elf_Rela;
1884 
1885   MipsGOTParser(ELFDumper<ELFT> *Dumper, const ELFO *Obj,
1886                 Elf_Dyn_Range DynTable, ScopedPrinter &W);
1887 
1888   void parseGOT();
1889   void parsePLT();
1890 
1891 private:
1892   ELFDumper<ELFT> *Dumper;
1893   const ELFO *Obj;
1894   ScopedPrinter &W;
1895   llvm::Optional<uint64_t> DtPltGot;
1896   llvm::Optional<uint64_t> DtLocalGotNum;
1897   llvm::Optional<uint64_t> DtGotSym;
1898   llvm::Optional<uint64_t> DtMipsPltGot;
1899   llvm::Optional<uint64_t> DtJmpRel;
1900 
1901   std::size_t getGOTTotal(ArrayRef<uint8_t> GOT) const;
1902   const GOTEntry *makeGOTIter(ArrayRef<uint8_t> GOT, std::size_t EntryNum);
1903 
1904   void printGotEntry(uint64_t GotAddr, const GOTEntry *BeginIt,
1905                      const GOTEntry *It);
1906   void printGlobalGotEntry(uint64_t GotAddr, const GOTEntry *BeginIt,
1907                            const GOTEntry *It, const Elf_Sym *Sym,
1908                            StringRef StrTable, bool IsDynamic);
1909   void printPLTEntry(uint64_t PLTAddr, const GOTEntry *BeginIt,
1910                      const GOTEntry *It, StringRef Purpose);
1911   void printPLTEntry(uint64_t PLTAddr, const GOTEntry *BeginIt,
1912                      const GOTEntry *It, StringRef StrTable,
1913                      const Elf_Sym *Sym);
1914 };
1915 }
1916 
1917 template <class ELFT>
MipsGOTParser(ELFDumper<ELFT> * Dumper,const ELFO * Obj,Elf_Dyn_Range DynTable,ScopedPrinter & W)1918 MipsGOTParser<ELFT>::MipsGOTParser(ELFDumper<ELFT> *Dumper, const ELFO *Obj,
1919                                    Elf_Dyn_Range DynTable, ScopedPrinter &W)
1920     : Dumper(Dumper), Obj(Obj), W(W) {
1921   for (const auto &Entry : DynTable) {
1922     switch (Entry.getTag()) {
1923     case ELF::DT_PLTGOT:
1924       DtPltGot = Entry.getVal();
1925       break;
1926     case ELF::DT_MIPS_LOCAL_GOTNO:
1927       DtLocalGotNum = Entry.getVal();
1928       break;
1929     case ELF::DT_MIPS_GOTSYM:
1930       DtGotSym = Entry.getVal();
1931       break;
1932     case ELF::DT_MIPS_PLTGOT:
1933       DtMipsPltGot = Entry.getVal();
1934       break;
1935     case ELF::DT_JMPREL:
1936       DtJmpRel = Entry.getVal();
1937       break;
1938     }
1939   }
1940 }
1941 
parseGOT()1942 template <class ELFT> void MipsGOTParser<ELFT>::parseGOT() {
1943   // See "Global Offset Table" in Chapter 5 in the following document
1944   // for detailed GOT description.
1945   // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1946   if (!DtPltGot) {
1947     W.startLine() << "Cannot find PLTGOT dynamic table tag.\n";
1948     return;
1949   }
1950   if (!DtLocalGotNum) {
1951     W.startLine() << "Cannot find MIPS_LOCAL_GOTNO dynamic table tag.\n";
1952     return;
1953   }
1954   if (!DtGotSym) {
1955     W.startLine() << "Cannot find MIPS_GOTSYM dynamic table tag.\n";
1956     return;
1957   }
1958 
1959   StringRef StrTable = Dumper->getDynamicStringTable();
1960   const Elf_Sym *DynSymBegin = Dumper->dynamic_symbols().begin();
1961   const Elf_Sym *DynSymEnd = Dumper->dynamic_symbols().end();
1962   std::size_t DynSymTotal = std::size_t(std::distance(DynSymBegin, DynSymEnd));
1963 
1964   if (*DtGotSym > DynSymTotal)
1965     report_fatal_error("MIPS_GOTSYM exceeds a number of dynamic symbols");
1966 
1967   std::size_t GlobalGotNum = DynSymTotal - *DtGotSym;
1968 
1969   if (*DtLocalGotNum + GlobalGotNum == 0) {
1970     W.startLine() << "GOT is empty.\n";
1971     return;
1972   }
1973 
1974   const Elf_Shdr *GOTShdr = findNotEmptySectionByAddress(Obj, *DtPltGot);
1975   if (!GOTShdr)
1976     report_fatal_error("There is no not empty GOT section at 0x" +
1977                        Twine::utohexstr(*DtPltGot));
1978 
1979   ArrayRef<uint8_t> GOT = unwrapOrError(Obj->getSectionContents(GOTShdr));
1980 
1981   if (*DtLocalGotNum + GlobalGotNum > getGOTTotal(GOT))
1982     report_fatal_error("Number of GOT entries exceeds the size of GOT section");
1983 
1984   const GOTEntry *GotBegin = makeGOTIter(GOT, 0);
1985   const GOTEntry *GotLocalEnd = makeGOTIter(GOT, *DtLocalGotNum);
1986   const GOTEntry *It = GotBegin;
1987 
1988   DictScope GS(W, "Primary GOT");
1989 
1990   W.printHex("Canonical gp value", GOTShdr->sh_addr + 0x7ff0);
1991   {
1992     ListScope RS(W, "Reserved entries");
1993 
1994     {
1995       DictScope D(W, "Entry");
1996       printGotEntry(GOTShdr->sh_addr, GotBegin, It++);
1997       W.printString("Purpose", StringRef("Lazy resolver"));
1998     }
1999 
2000     if (It != GotLocalEnd && (*It >> (sizeof(GOTEntry) * 8 - 1)) != 0) {
2001       DictScope D(W, "Entry");
2002       printGotEntry(GOTShdr->sh_addr, GotBegin, It++);
2003       W.printString("Purpose", StringRef("Module pointer (GNU extension)"));
2004     }
2005   }
2006   {
2007     ListScope LS(W, "Local entries");
2008     for (; It != GotLocalEnd; ++It) {
2009       DictScope D(W, "Entry");
2010       printGotEntry(GOTShdr->sh_addr, GotBegin, It);
2011     }
2012   }
2013   {
2014     ListScope GS(W, "Global entries");
2015 
2016     const GOTEntry *GotGlobalEnd =
2017         makeGOTIter(GOT, *DtLocalGotNum + GlobalGotNum);
2018     const Elf_Sym *GotDynSym = DynSymBegin + *DtGotSym;
2019     for (; It != GotGlobalEnd; ++It) {
2020       DictScope D(W, "Entry");
2021       printGlobalGotEntry(GOTShdr->sh_addr, GotBegin, It, GotDynSym++, StrTable,
2022                           true);
2023     }
2024   }
2025 
2026   std::size_t SpecGotNum = getGOTTotal(GOT) - *DtLocalGotNum - GlobalGotNum;
2027   W.printNumber("Number of TLS and multi-GOT entries", uint64_t(SpecGotNum));
2028 }
2029 
parsePLT()2030 template <class ELFT> void MipsGOTParser<ELFT>::parsePLT() {
2031   if (!DtMipsPltGot) {
2032     W.startLine() << "Cannot find MIPS_PLTGOT dynamic table tag.\n";
2033     return;
2034   }
2035   if (!DtJmpRel) {
2036     W.startLine() << "Cannot find JMPREL dynamic table tag.\n";
2037     return;
2038   }
2039 
2040   const Elf_Shdr *PLTShdr = findNotEmptySectionByAddress(Obj, *DtMipsPltGot);
2041   if (!PLTShdr)
2042     report_fatal_error("There is no not empty PLTGOT section at 0x " +
2043                        Twine::utohexstr(*DtMipsPltGot));
2044   ArrayRef<uint8_t> PLT = unwrapOrError(Obj->getSectionContents(PLTShdr));
2045 
2046   const Elf_Shdr *PLTRelShdr = findNotEmptySectionByAddress(Obj, *DtJmpRel);
2047   if (!PLTRelShdr)
2048     report_fatal_error("There is no not empty RELPLT section at 0x" +
2049                        Twine::utohexstr(*DtJmpRel));
2050   const Elf_Shdr *SymTable =
2051       unwrapOrError(Obj->getSection(PLTRelShdr->sh_link));
2052   StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*SymTable));
2053 
2054   const GOTEntry *PLTBegin = makeGOTIter(PLT, 0);
2055   const GOTEntry *PLTEnd = makeGOTIter(PLT, getGOTTotal(PLT));
2056   const GOTEntry *It = PLTBegin;
2057 
2058   DictScope GS(W, "PLT GOT");
2059   {
2060     ListScope RS(W, "Reserved entries");
2061     printPLTEntry(PLTShdr->sh_addr, PLTBegin, It++, "PLT lazy resolver");
2062     if (It != PLTEnd)
2063       printPLTEntry(PLTShdr->sh_addr, PLTBegin, It++, "Module pointer");
2064   }
2065   {
2066     ListScope GS(W, "Entries");
2067 
2068     switch (PLTRelShdr->sh_type) {
2069     case ELF::SHT_REL:
2070       for (const Elf_Rel *RI = Obj->rel_begin(PLTRelShdr),
2071                          *RE = Obj->rel_end(PLTRelShdr);
2072            RI != RE && It != PLTEnd; ++RI, ++It) {
2073         const Elf_Sym *Sym = Obj->getRelocationSymbol(&*RI, SymTable);
2074         printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, StrTable, Sym);
2075       }
2076       break;
2077     case ELF::SHT_RELA:
2078       for (const Elf_Rela *RI = Obj->rela_begin(PLTRelShdr),
2079                           *RE = Obj->rela_end(PLTRelShdr);
2080            RI != RE && It != PLTEnd; ++RI, ++It) {
2081         const Elf_Sym *Sym = Obj->getRelocationSymbol(&*RI, SymTable);
2082         printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, StrTable, Sym);
2083       }
2084       break;
2085     }
2086   }
2087 }
2088 
2089 template <class ELFT>
getGOTTotal(ArrayRef<uint8_t> GOT) const2090 std::size_t MipsGOTParser<ELFT>::getGOTTotal(ArrayRef<uint8_t> GOT) const {
2091   return GOT.size() / sizeof(GOTEntry);
2092 }
2093 
2094 template <class ELFT>
2095 const typename MipsGOTParser<ELFT>::GOTEntry *
makeGOTIter(ArrayRef<uint8_t> GOT,std::size_t EntryNum)2096 MipsGOTParser<ELFT>::makeGOTIter(ArrayRef<uint8_t> GOT, std::size_t EntryNum) {
2097   const char *Data = reinterpret_cast<const char *>(GOT.data());
2098   return reinterpret_cast<const GOTEntry *>(Data + EntryNum * sizeof(GOTEntry));
2099 }
2100 
2101 template <class ELFT>
printGotEntry(uint64_t GotAddr,const GOTEntry * BeginIt,const GOTEntry * It)2102 void MipsGOTParser<ELFT>::printGotEntry(uint64_t GotAddr,
2103                                         const GOTEntry *BeginIt,
2104                                         const GOTEntry *It) {
2105   int64_t Offset = std::distance(BeginIt, It) * sizeof(GOTEntry);
2106   W.printHex("Address", GotAddr + Offset);
2107   W.printNumber("Access", Offset - 0x7ff0);
2108   W.printHex("Initial", *It);
2109 }
2110 
2111 template <class ELFT>
printGlobalGotEntry(uint64_t GotAddr,const GOTEntry * BeginIt,const GOTEntry * It,const Elf_Sym * Sym,StringRef StrTable,bool IsDynamic)2112 void MipsGOTParser<ELFT>::printGlobalGotEntry(
2113     uint64_t GotAddr, const GOTEntry *BeginIt, const GOTEntry *It,
2114     const Elf_Sym *Sym, StringRef StrTable, bool IsDynamic) {
2115   printGotEntry(GotAddr, BeginIt, It);
2116 
2117   W.printHex("Value", Sym->st_value);
2118   W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes));
2119 
2120   unsigned SectionIndex = 0;
2121   StringRef SectionName;
2122   getSectionNameIndex(*Obj, Sym, Dumper->dynamic_symbols().begin(),
2123                       Dumper->getShndxTable(), SectionName, SectionIndex);
2124   W.printHex("Section", SectionName, SectionIndex);
2125 
2126   std::string FullSymbolName =
2127       Dumper->getFullSymbolName(Sym, StrTable, IsDynamic);
2128   W.printNumber("Name", FullSymbolName, Sym->st_name);
2129 }
2130 
2131 template <class ELFT>
printPLTEntry(uint64_t PLTAddr,const GOTEntry * BeginIt,const GOTEntry * It,StringRef Purpose)2132 void MipsGOTParser<ELFT>::printPLTEntry(uint64_t PLTAddr,
2133                                         const GOTEntry *BeginIt,
2134                                         const GOTEntry *It, StringRef Purpose) {
2135   DictScope D(W, "Entry");
2136   int64_t Offset = std::distance(BeginIt, It) * sizeof(GOTEntry);
2137   W.printHex("Address", PLTAddr + Offset);
2138   W.printHex("Initial", *It);
2139   W.printString("Purpose", Purpose);
2140 }
2141 
2142 template <class ELFT>
printPLTEntry(uint64_t PLTAddr,const GOTEntry * BeginIt,const GOTEntry * It,StringRef StrTable,const Elf_Sym * Sym)2143 void MipsGOTParser<ELFT>::printPLTEntry(uint64_t PLTAddr,
2144                                         const GOTEntry *BeginIt,
2145                                         const GOTEntry *It, StringRef StrTable,
2146                                         const Elf_Sym *Sym) {
2147   DictScope D(W, "Entry");
2148   int64_t Offset = std::distance(BeginIt, It) * sizeof(GOTEntry);
2149   W.printHex("Address", PLTAddr + Offset);
2150   W.printHex("Initial", *It);
2151   W.printHex("Value", Sym->st_value);
2152   W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes));
2153 
2154   unsigned SectionIndex = 0;
2155   StringRef SectionName;
2156   getSectionNameIndex(*Obj, Sym, Dumper->dynamic_symbols().begin(),
2157                       Dumper->getShndxTable(), SectionName, SectionIndex);
2158   W.printHex("Section", SectionName, SectionIndex);
2159 
2160   std::string FullSymbolName = Dumper->getFullSymbolName(Sym, StrTable, true);
2161   W.printNumber("Name", FullSymbolName, Sym->st_name);
2162 }
2163 
printMipsPLTGOT()2164 template <class ELFT> void ELFDumper<ELFT>::printMipsPLTGOT() {
2165   if (Obj->getHeader()->e_machine != EM_MIPS) {
2166     W.startLine() << "MIPS PLT GOT is available for MIPS targets only.\n";
2167     return;
2168   }
2169 
2170   MipsGOTParser<ELFT> GOTParser(this, Obj, dynamic_table(), W);
2171   GOTParser.parseGOT();
2172   GOTParser.parsePLT();
2173 }
2174 
2175 static const EnumEntry<unsigned> ElfMipsISAExtType[] = {
2176   {"None",                    Mips::AFL_EXT_NONE},
2177   {"Broadcom SB-1",           Mips::AFL_EXT_SB1},
2178   {"Cavium Networks Octeon",  Mips::AFL_EXT_OCTEON},
2179   {"Cavium Networks Octeon2", Mips::AFL_EXT_OCTEON2},
2180   {"Cavium Networks OcteonP", Mips::AFL_EXT_OCTEONP},
2181   {"Cavium Networks Octeon3", Mips::AFL_EXT_OCTEON3},
2182   {"LSI R4010",               Mips::AFL_EXT_4010},
2183   {"Loongson 2E",             Mips::AFL_EXT_LOONGSON_2E},
2184   {"Loongson 2F",             Mips::AFL_EXT_LOONGSON_2F},
2185   {"Loongson 3A",             Mips::AFL_EXT_LOONGSON_3A},
2186   {"MIPS R4650",              Mips::AFL_EXT_4650},
2187   {"MIPS R5900",              Mips::AFL_EXT_5900},
2188   {"MIPS R10000",             Mips::AFL_EXT_10000},
2189   {"NEC VR4100",              Mips::AFL_EXT_4100},
2190   {"NEC VR4111/VR4181",       Mips::AFL_EXT_4111},
2191   {"NEC VR4120",              Mips::AFL_EXT_4120},
2192   {"NEC VR5400",              Mips::AFL_EXT_5400},
2193   {"NEC VR5500",              Mips::AFL_EXT_5500},
2194   {"RMI Xlr",                 Mips::AFL_EXT_XLR},
2195   {"Toshiba R3900",           Mips::AFL_EXT_3900}
2196 };
2197 
2198 static const EnumEntry<unsigned> ElfMipsASEFlags[] = {
2199   {"DSP",                Mips::AFL_ASE_DSP},
2200   {"DSPR2",              Mips::AFL_ASE_DSPR2},
2201   {"Enhanced VA Scheme", Mips::AFL_ASE_EVA},
2202   {"MCU",                Mips::AFL_ASE_MCU},
2203   {"MDMX",               Mips::AFL_ASE_MDMX},
2204   {"MIPS-3D",            Mips::AFL_ASE_MIPS3D},
2205   {"MT",                 Mips::AFL_ASE_MT},
2206   {"SmartMIPS",          Mips::AFL_ASE_SMARTMIPS},
2207   {"VZ",                 Mips::AFL_ASE_VIRT},
2208   {"MSA",                Mips::AFL_ASE_MSA},
2209   {"MIPS16",             Mips::AFL_ASE_MIPS16},
2210   {"microMIPS",          Mips::AFL_ASE_MICROMIPS},
2211   {"XPA",                Mips::AFL_ASE_XPA}
2212 };
2213 
2214 static const EnumEntry<unsigned> ElfMipsFpABIType[] = {
2215   {"Hard or soft float",                  Mips::Val_GNU_MIPS_ABI_FP_ANY},
2216   {"Hard float (double precision)",       Mips::Val_GNU_MIPS_ABI_FP_DOUBLE},
2217   {"Hard float (single precision)",       Mips::Val_GNU_MIPS_ABI_FP_SINGLE},
2218   {"Soft float",                          Mips::Val_GNU_MIPS_ABI_FP_SOFT},
2219   {"Hard float (MIPS32r2 64-bit FPU 12 callee-saved)",
2220    Mips::Val_GNU_MIPS_ABI_FP_OLD_64},
2221   {"Hard float (32-bit CPU, Any FPU)",    Mips::Val_GNU_MIPS_ABI_FP_XX},
2222   {"Hard float (32-bit CPU, 64-bit FPU)", Mips::Val_GNU_MIPS_ABI_FP_64},
2223   {"Hard float compat (32-bit CPU, 64-bit FPU)",
2224    Mips::Val_GNU_MIPS_ABI_FP_64A}
2225 };
2226 
2227 static const EnumEntry<unsigned> ElfMipsFlags1[] {
2228   {"ODDSPREG", Mips::AFL_FLAGS1_ODDSPREG},
2229 };
2230 
getMipsRegisterSize(uint8_t Flag)2231 static int getMipsRegisterSize(uint8_t Flag) {
2232   switch (Flag) {
2233   case Mips::AFL_REG_NONE:
2234     return 0;
2235   case Mips::AFL_REG_32:
2236     return 32;
2237   case Mips::AFL_REG_64:
2238     return 64;
2239   case Mips::AFL_REG_128:
2240     return 128;
2241   default:
2242     return -1;
2243   }
2244 }
2245 
printMipsABIFlags()2246 template <class ELFT> void ELFDumper<ELFT>::printMipsABIFlags() {
2247   const Elf_Shdr *Shdr = findSectionByName(*Obj, ".MIPS.abiflags");
2248   if (!Shdr) {
2249     W.startLine() << "There is no .MIPS.abiflags section in the file.\n";
2250     return;
2251   }
2252   ArrayRef<uint8_t> Sec = unwrapOrError(Obj->getSectionContents(Shdr));
2253   if (Sec.size() != sizeof(Elf_Mips_ABIFlags<ELFT>)) {
2254     W.startLine() << "The .MIPS.abiflags section has a wrong size.\n";
2255     return;
2256   }
2257 
2258   auto *Flags = reinterpret_cast<const Elf_Mips_ABIFlags<ELFT> *>(Sec.data());
2259 
2260   raw_ostream &OS = W.getOStream();
2261   DictScope GS(W, "MIPS ABI Flags");
2262 
2263   W.printNumber("Version", Flags->version);
2264   W.startLine() << "ISA: ";
2265   if (Flags->isa_rev <= 1)
2266     OS << format("MIPS%u", Flags->isa_level);
2267   else
2268     OS << format("MIPS%ur%u", Flags->isa_level, Flags->isa_rev);
2269   OS << "\n";
2270   W.printEnum("ISA Extension", Flags->isa_ext, makeArrayRef(ElfMipsISAExtType));
2271   W.printFlags("ASEs", Flags->ases, makeArrayRef(ElfMipsASEFlags));
2272   W.printEnum("FP ABI", Flags->fp_abi, makeArrayRef(ElfMipsFpABIType));
2273   W.printNumber("GPR size", getMipsRegisterSize(Flags->gpr_size));
2274   W.printNumber("CPR1 size", getMipsRegisterSize(Flags->cpr1_size));
2275   W.printNumber("CPR2 size", getMipsRegisterSize(Flags->cpr2_size));
2276   W.printFlags("Flags 1", Flags->flags1, makeArrayRef(ElfMipsFlags1));
2277   W.printHex("Flags 2", Flags->flags2);
2278 }
2279 
2280 template <class ELFT>
printMipsReginfoData(ScopedPrinter & W,const Elf_Mips_RegInfo<ELFT> & Reginfo)2281 static void printMipsReginfoData(ScopedPrinter &W,
2282                                  const Elf_Mips_RegInfo<ELFT> &Reginfo) {
2283   W.printHex("GP", Reginfo.ri_gp_value);
2284   W.printHex("General Mask", Reginfo.ri_gprmask);
2285   W.printHex("Co-Proc Mask0", Reginfo.ri_cprmask[0]);
2286   W.printHex("Co-Proc Mask1", Reginfo.ri_cprmask[1]);
2287   W.printHex("Co-Proc Mask2", Reginfo.ri_cprmask[2]);
2288   W.printHex("Co-Proc Mask3", Reginfo.ri_cprmask[3]);
2289 }
2290 
printMipsReginfo()2291 template <class ELFT> void ELFDumper<ELFT>::printMipsReginfo() {
2292   const Elf_Shdr *Shdr = findSectionByName(*Obj, ".reginfo");
2293   if (!Shdr) {
2294     W.startLine() << "There is no .reginfo section in the file.\n";
2295     return;
2296   }
2297   ArrayRef<uint8_t> Sec = unwrapOrError(Obj->getSectionContents(Shdr));
2298   if (Sec.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) {
2299     W.startLine() << "The .reginfo section has a wrong size.\n";
2300     return;
2301   }
2302 
2303   DictScope GS(W, "MIPS RegInfo");
2304   auto *Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(Sec.data());
2305   printMipsReginfoData(W, *Reginfo);
2306 }
2307 
printMipsOptions()2308 template <class ELFT> void ELFDumper<ELFT>::printMipsOptions() {
2309   const Elf_Shdr *Shdr = findSectionByName(*Obj, ".MIPS.options");
2310   if (!Shdr) {
2311     W.startLine() << "There is no .MIPS.options section in the file.\n";
2312     return;
2313   }
2314 
2315   DictScope GS(W, "MIPS Options");
2316 
2317   ArrayRef<uint8_t> Sec = unwrapOrError(Obj->getSectionContents(Shdr));
2318   while (!Sec.empty()) {
2319     if (Sec.size() < sizeof(Elf_Mips_Options<ELFT>)) {
2320       W.startLine() << "The .MIPS.options section has a wrong size.\n";
2321       return;
2322     }
2323     auto *O = reinterpret_cast<const Elf_Mips_Options<ELFT> *>(Sec.data());
2324     DictScope GS(W, getElfMipsOptionsOdkType(O->kind));
2325     switch (O->kind) {
2326     case ODK_REGINFO:
2327       printMipsReginfoData(W, O->getRegInfo());
2328       break;
2329     default:
2330       W.startLine() << "Unsupported MIPS options tag.\n";
2331       break;
2332     }
2333     Sec = Sec.slice(O->size);
2334   }
2335 }
2336 
printStackMap() const2337 template <class ELFT> void ELFDumper<ELFT>::printStackMap() const {
2338   const Elf_Shdr *StackMapSection = nullptr;
2339   for (const auto &Sec : Obj->sections()) {
2340     StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
2341     if (Name == ".llvm_stackmaps") {
2342       StackMapSection = &Sec;
2343       break;
2344     }
2345   }
2346 
2347   if (!StackMapSection)
2348     return;
2349 
2350   StringRef StackMapContents;
2351   ArrayRef<uint8_t> StackMapContentsArray =
2352       unwrapOrError(Obj->getSectionContents(StackMapSection));
2353 
2354   prettyPrintStackMap(llvm::outs(), StackMapV1Parser<ELFT::TargetEndianness>(
2355                                         StackMapContentsArray));
2356 }
2357 
printGroupSections()2358 template <class ELFT> void ELFDumper<ELFT>::printGroupSections() {
2359   ELFDumperStyle->printGroupSections(Obj);
2360 }
2361 
printFields(formatted_raw_ostream & OS,StringRef Str1,StringRef Str2)2362 static inline void printFields(formatted_raw_ostream &OS, StringRef Str1,
2363                                StringRef Str2) {
2364   OS.PadToColumn(2u);
2365   OS << Str1;
2366   OS.PadToColumn(37u);
2367   OS << Str2 << "\n";
2368   OS.flush();
2369 }
2370 
printFileHeaders(const ELFO * Obj)2371 template <class ELFT> void GNUStyle<ELFT>::printFileHeaders(const ELFO *Obj) {
2372   const Elf_Ehdr *e = Obj->getHeader();
2373   OS << "ELF Header:\n";
2374   OS << "  Magic:  ";
2375   std::string Str;
2376   for (int i = 0; i < ELF::EI_NIDENT; i++)
2377     OS << format(" %02x", static_cast<int>(e->e_ident[i]));
2378   OS << "\n";
2379   Str = printEnum(e->e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
2380   printFields(OS, "Class:", Str);
2381   Str = printEnum(e->e_ident[ELF::EI_DATA], makeArrayRef(ElfDataEncoding));
2382   printFields(OS, "Data:", Str);
2383   OS.PadToColumn(2u);
2384   OS << "Version:";
2385   OS.PadToColumn(37u);
2386   OS << to_hexString(e->e_ident[ELF::EI_VERSION]);
2387   if (e->e_version == ELF::EV_CURRENT)
2388     OS << " (current)";
2389   OS << "\n";
2390   Str = printEnum(e->e_ident[ELF::EI_OSABI], makeArrayRef(ElfOSABI));
2391   printFields(OS, "OS/ABI:", Str);
2392   Str = "0x" + to_hexString(e->e_version);
2393   Str = to_hexString(e->e_ident[ELF::EI_ABIVERSION]);
2394   printFields(OS, "ABI Version:", Str);
2395   Str = printEnum(e->e_type, makeArrayRef(ElfObjectFileType));
2396   printFields(OS, "Type:", Str);
2397   Str = printEnum(e->e_machine, makeArrayRef(ElfMachineType));
2398   printFields(OS, "Machine:", Str);
2399   Str = "0x" + to_hexString(e->e_version);
2400   printFields(OS, "Version:", Str);
2401   Str = "0x" + to_hexString(e->e_entry);
2402   printFields(OS, "Entry point address:", Str);
2403   Str = to_string(e->e_phoff) + " (bytes into file)";
2404   printFields(OS, "Start of program headers:", Str);
2405   Str = to_string(e->e_shoff) + " (bytes into file)";
2406   printFields(OS, "Start of section headers:", Str);
2407   Str = "0x" + to_hexString(e->e_flags);
2408   printFields(OS, "Flags:", Str);
2409   Str = to_string(e->e_ehsize) + " (bytes)";
2410   printFields(OS, "Size of this header:", Str);
2411   Str = to_string(e->e_phentsize) + " (bytes)";
2412   printFields(OS, "Size of program headers:", Str);
2413   Str = to_string(e->e_phnum);
2414   printFields(OS, "Number of program headers:", Str);
2415   Str = to_string(e->e_shentsize) + " (bytes)";
2416   printFields(OS, "Size of section headers:", Str);
2417   Str = to_string(e->e_shnum);
2418   printFields(OS, "Number of section headers:", Str);
2419   Str = to_string(e->e_shstrndx);
2420   printFields(OS, "Section header string table index:", Str);
2421 }
2422 
printGroupSections(const ELFO * Obj)2423 template <class ELFT> void GNUStyle<ELFT>::printGroupSections(const ELFO *Obj) {
2424   uint32_t SectionIndex = 0;
2425   bool HasGroups = false;
2426   for (const Elf_Shdr &Sec : Obj->sections()) {
2427     if (Sec.sh_type == ELF::SHT_GROUP) {
2428       HasGroups = true;
2429       const Elf_Shdr *Symtab = unwrapOrError(Obj->getSection(Sec.sh_link));
2430       StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*Symtab));
2431       const Elf_Sym *Signature =
2432           Obj->template getEntry<Elf_Sym>(Symtab, Sec.sh_info);
2433       ArrayRef<Elf_Word> Data = unwrapOrError(
2434           Obj->template getSectionContentsAsArray<Elf_Word>(&Sec));
2435       StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
2436       OS << "\n" << getGroupType(Data[0]) << " group section ["
2437          << format_decimal(SectionIndex, 5) << "] `" << Name << "' ["
2438          << StrTable.data() + Signature->st_name << "] contains "
2439          << (Data.size() - 1) << " sections:\n"
2440          << "   [Index]    Name\n";
2441       for (auto &Ndx : Data.slice(1)) {
2442         auto Sec = unwrapOrError(Obj->getSection(Ndx));
2443         const StringRef Name = unwrapOrError(Obj->getSectionName(Sec));
2444         OS << "   [" << format_decimal(Ndx, 5) << "]   " << Name
2445            << "\n";
2446       }
2447     }
2448     ++SectionIndex;
2449   }
2450   if (!HasGroups)
2451     OS << "There are no section groups in this file.\n";
2452 }
2453 
2454 template <class ELFT>
printRelocation(const ELFO * Obj,const Elf_Shdr * SymTab,const Elf_Rela & R,bool IsRela)2455 void GNUStyle<ELFT>::printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab,
2456                                      const Elf_Rela &R, bool IsRela) {
2457   std::string Offset, Info, Addend = "", Value;
2458   SmallString<32> RelocName;
2459   StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*SymTab));
2460   StringRef TargetName;
2461   const Elf_Sym *Sym = nullptr;
2462   unsigned Width = ELFT::Is64Bits ? 16 : 8;
2463   unsigned Bias = ELFT::Is64Bits ? 8 : 0;
2464 
2465   // First two fields are bit width dependent. The rest of them are after are
2466   // fixed width.
2467   Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias};
2468   Obj->getRelocationTypeName(R.getType(Obj->isMips64EL()), RelocName);
2469   Sym = Obj->getRelocationSymbol(&R, SymTab);
2470   if (Sym && Sym->getType() == ELF::STT_SECTION) {
2471     const Elf_Shdr *Sec = unwrapOrError(
2472         Obj->getSection(Sym, SymTab, this->dumper()->getShndxTable()));
2473     TargetName = unwrapOrError(Obj->getSectionName(Sec));
2474   } else if (Sym) {
2475     TargetName = unwrapOrError(Sym->getName(StrTable));
2476   }
2477 
2478   if (Sym && IsRela) {
2479     if (R.r_addend < 0)
2480       Addend = " - ";
2481     else
2482       Addend = " + ";
2483   }
2484 
2485   Offset = to_string(format_hex_no_prefix(R.r_offset, Width));
2486   Info = to_string(format_hex_no_prefix(R.r_info, Width));
2487 
2488   int64_t RelAddend = R.r_addend;
2489   if (IsRela)
2490     Addend += to_hexString(std::abs(RelAddend), false);
2491 
2492   if (Sym)
2493     Value = to_string(format_hex_no_prefix(Sym->getValue(), Width));
2494 
2495   Fields[0].Str = Offset;
2496   Fields[1].Str = Info;
2497   Fields[2].Str = RelocName;
2498   Fields[3].Str = Value;
2499   Fields[4].Str = TargetName;
2500   for (auto &field : Fields)
2501     printField(field);
2502   OS << Addend;
2503   OS << "\n";
2504 }
2505 
printRelocHeader(raw_ostream & OS,bool Is64,bool IsRela)2506 static inline void printRelocHeader(raw_ostream &OS, bool Is64, bool IsRela) {
2507   if (Is64)
2508     OS << "    Offset             Info             Type"
2509        << "               Symbol's Value  Symbol's Name";
2510   else
2511     OS << " Offset     Info    Type                Sym. Value  "
2512        << "Symbol's Name";
2513   if (IsRela)
2514     OS << (IsRela ? " + Addend" : "");
2515   OS << "\n";
2516 }
2517 
printRelocations(const ELFO * Obj)2518 template <class ELFT> void GNUStyle<ELFT>::printRelocations(const ELFO *Obj) {
2519   bool HasRelocSections = false;
2520   for (const Elf_Shdr &Sec : Obj->sections()) {
2521     if (Sec.sh_type != ELF::SHT_REL && Sec.sh_type != ELF::SHT_RELA)
2522       continue;
2523     HasRelocSections = true;
2524     StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
2525     unsigned Entries = Sec.getEntityCount();
2526     uintX_t Offset = Sec.sh_offset;
2527     OS << "\nRelocation section '" << Name << "' at offset 0x"
2528        << to_hexString(Offset, false) << " contains " << Entries
2529        << " entries:\n";
2530     printRelocHeader(OS,  ELFT::Is64Bits, (Sec.sh_type == ELF::SHT_RELA));
2531     const Elf_Shdr *SymTab = unwrapOrError(Obj->getSection(Sec.sh_link));
2532     if (Sec.sh_type == ELF::SHT_REL) {
2533       for (const auto &R : Obj->rels(&Sec)) {
2534         Elf_Rela Rela;
2535         Rela.r_offset = R.r_offset;
2536         Rela.r_info = R.r_info;
2537         Rela.r_addend = 0;
2538         printRelocation(Obj, SymTab, Rela, false);
2539       }
2540     } else {
2541       for (const auto &R : Obj->relas(&Sec))
2542         printRelocation(Obj, SymTab, R, true);
2543     }
2544   }
2545   if (!HasRelocSections)
2546     OS << "\nThere are no relocations in this file.\n";
2547 }
2548 
getSectionTypeString(unsigned Arch,unsigned Type)2549 std::string getSectionTypeString(unsigned Arch, unsigned Type) {
2550   using namespace ELF;
2551   switch (Arch) {
2552   case EM_ARM:
2553     switch (Type) {
2554     case SHT_ARM_EXIDX:
2555       return "ARM_EXIDX";
2556     case SHT_ARM_PREEMPTMAP:
2557       return "ARM_PREEMPTMAP";
2558     case SHT_ARM_ATTRIBUTES:
2559       return "ARM_ATTRIBUTES";
2560     case SHT_ARM_DEBUGOVERLAY:
2561       return "ARM_DEBUGOVERLAY";
2562     case SHT_ARM_OVERLAYSECTION:
2563       return "ARM_OVERLAYSECTION";
2564     }
2565   case EM_X86_64:
2566     switch (Type) {
2567     case SHT_X86_64_UNWIND:
2568       return "X86_64_UNWIND";
2569     }
2570   case EM_MIPS:
2571   case EM_MIPS_RS3_LE:
2572     switch (Type) {
2573     case SHT_MIPS_REGINFO:
2574       return "MIPS_REGINFO";
2575     case SHT_MIPS_OPTIONS:
2576       return "MIPS_OPTIONS";
2577     case SHT_MIPS_ABIFLAGS:
2578       return "MIPS_ABIFLAGS";
2579     }
2580   }
2581   switch (Type) {
2582   case SHT_NULL:
2583     return "NULL";
2584   case SHT_PROGBITS:
2585     return "PROGBITS";
2586   case SHT_SYMTAB:
2587     return "SYMTAB";
2588   case SHT_STRTAB:
2589     return "STRTAB";
2590   case SHT_RELA:
2591     return "RELA";
2592   case SHT_HASH:
2593     return "HASH";
2594   case SHT_DYNAMIC:
2595     return "DYNAMIC";
2596   case SHT_NOTE:
2597     return "NOTE";
2598   case SHT_NOBITS:
2599     return "NOBITS";
2600   case SHT_REL:
2601     return "REL";
2602   case SHT_SHLIB:
2603     return "SHLIB";
2604   case SHT_DYNSYM:
2605     return "DYNSYM";
2606   case SHT_INIT_ARRAY:
2607     return "INIT_ARRAY";
2608   case SHT_FINI_ARRAY:
2609     return "FINI_ARRAY";
2610   case SHT_PREINIT_ARRAY:
2611     return "PREINIT_ARRAY";
2612   case SHT_GROUP:
2613     return "GROUP";
2614   case SHT_SYMTAB_SHNDX:
2615     return "SYMTAB SECTION INDICES";
2616   // FIXME: Parse processor specific GNU attributes
2617   case SHT_GNU_ATTRIBUTES:
2618     return "ATTRIBUTES";
2619   case SHT_GNU_HASH:
2620     return "GNU_HASH";
2621   case SHT_GNU_verdef:
2622     return "VERDEF";
2623   case SHT_GNU_verneed:
2624     return "VERNEED";
2625   case SHT_GNU_versym:
2626     return "VERSYM";
2627   default:
2628     return "";
2629   }
2630   return "";
2631 }
2632 
printSections(const ELFO * Obj)2633 template <class ELFT> void GNUStyle<ELFT>::printSections(const ELFO *Obj) {
2634   size_t SectionIndex = 0;
2635   std::string Number, Type, Size, Address, Offset, Flags, Link, Info, EntrySize,
2636       Alignment;
2637   unsigned Bias;
2638   unsigned Width;
2639 
2640   if (ELFT::Is64Bits) {
2641     Bias = 0;
2642     Width = 16;
2643   } else {
2644     Bias = 8;
2645     Width = 8;
2646   }
2647   OS << "There are " << to_string(Obj->getHeader()->e_shnum)
2648      << " section headers, starting at offset "
2649      << "0x" << to_hexString(Obj->getHeader()->e_shoff, false) << ":\n\n";
2650   OS << "Section Headers:\n";
2651   Field Fields[11] = {{"[Nr]", 2},
2652                       {"Name", 7},
2653                       {"Type", 25},
2654                       {"Address", 41},
2655                       {"Off", 58 - Bias},
2656                       {"Size", 65 - Bias},
2657                       {"ES", 72 - Bias},
2658                       {"Flg", 75 - Bias},
2659                       {"Lk", 79 - Bias},
2660                       {"Inf", 82 - Bias},
2661                       {"Al", 86 - Bias}};
2662   for (auto &f : Fields)
2663     printField(f);
2664   OS << "\n";
2665 
2666   for (const Elf_Shdr &Sec : Obj->sections()) {
2667     Number = to_string(SectionIndex);
2668     Fields[0].Str = Number;
2669     Fields[1].Str = unwrapOrError(Obj->getSectionName(&Sec));
2670     Type = getSectionTypeString(Obj->getHeader()->e_machine, Sec.sh_type);
2671     Fields[2].Str = Type;
2672     Address = to_string(format_hex_no_prefix(Sec.sh_addr, Width));
2673     Fields[3].Str = Address;
2674     Offset = to_string(format_hex_no_prefix(Sec.sh_offset, 6));
2675     Fields[4].Str = Offset;
2676     Size = to_string(format_hex_no_prefix(Sec.sh_size, 6));
2677     Fields[5].Str = Size;
2678     EntrySize = to_string(format_hex_no_prefix(Sec.sh_entsize, 2));
2679     Fields[6].Str = EntrySize;
2680     Flags = getGNUFlags(Sec.sh_flags);
2681     Fields[7].Str = Flags;
2682     Link = to_string(Sec.sh_link);
2683     Fields[8].Str = Link;
2684     Info = to_string(Sec.sh_info);
2685     Fields[9].Str = Info;
2686     Alignment = to_string(Sec.sh_addralign);
2687     Fields[10].Str = Alignment;
2688     OS.PadToColumn(Fields[0].Column);
2689     OS << "[" << right_justify(Fields[0].Str, 2) << "]";
2690     for (int i = 1; i < 7; i++)
2691       printField(Fields[i]);
2692     OS.PadToColumn(Fields[7].Column);
2693     OS << right_justify(Fields[7].Str, 3);
2694     OS.PadToColumn(Fields[8].Column);
2695     OS << right_justify(Fields[8].Str, 2);
2696     OS.PadToColumn(Fields[9].Column);
2697     OS << right_justify(Fields[9].Str, 3);
2698     OS.PadToColumn(Fields[10].Column);
2699     OS << right_justify(Fields[10].Str, 2);
2700     OS << "\n";
2701     ++SectionIndex;
2702   }
2703   OS << "Key to Flags:\n"
2704      << "  W (write), A (alloc), X (execute), M (merge), S (strings), l "
2705         "(large)\n"
2706      << "  I (info), L (link order), G (group), T (TLS), E (exclude),\
2707  x (unknown)\n"
2708      << "  O (extra OS processing required) o (OS specific),\
2709  p (processor specific)\n";
2710 }
2711 
2712 template <class ELFT>
printSymtabMessage(const ELFO * Obj,StringRef Name,size_t Entries)2713 void GNUStyle<ELFT>::printSymtabMessage(const ELFO *Obj, StringRef Name,
2714                                         size_t Entries) {
2715   if (Name.size())
2716     OS << "\nSymbol table '" << Name << "' contains " << Entries
2717        << " entries:\n";
2718   else
2719     OS << "\n Symbol table for image:\n";
2720 
2721   if (ELFT::Is64Bits)
2722     OS << "   Num:    Value          Size Type    Bind   Vis      Ndx Name\n";
2723   else
2724     OS << "   Num:    Value  Size Type    Bind   Vis      Ndx Name\n";
2725 }
2726 
2727 template <class ELFT>
getSymbolSectionNdx(const ELFO * Obj,const Elf_Sym * Symbol,const Elf_Sym * FirstSym)2728 std::string GNUStyle<ELFT>::getSymbolSectionNdx(const ELFO *Obj,
2729                                                 const Elf_Sym *Symbol,
2730                                                 const Elf_Sym *FirstSym) {
2731   unsigned SectionIndex = Symbol->st_shndx;
2732   switch (SectionIndex) {
2733   case ELF::SHN_UNDEF:
2734     return "UND";
2735   case ELF::SHN_ABS:
2736     return "ABS";
2737   case ELF::SHN_COMMON:
2738     return "COM";
2739   case ELF::SHN_XINDEX:
2740     SectionIndex = Obj->getExtendedSymbolTableIndex(
2741         Symbol, FirstSym, this->dumper()->getShndxTable());
2742   default:
2743     // Find if:
2744     // Processor specific
2745     if (SectionIndex >= ELF::SHN_LOPROC && SectionIndex <= ELF::SHN_HIPROC)
2746       return std::string("PRC[0x") +
2747              to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
2748     // OS specific
2749     if (SectionIndex >= ELF::SHN_LOOS && SectionIndex <= ELF::SHN_HIOS)
2750       return std::string("OS[0x") +
2751              to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
2752     // Architecture reserved:
2753     if (SectionIndex >= ELF::SHN_LORESERVE &&
2754         SectionIndex <= ELF::SHN_HIRESERVE)
2755       return std::string("RSV[0x") +
2756              to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
2757     // A normal section with an index
2758     return to_string(format_decimal(SectionIndex, 3));
2759   }
2760 }
2761 
2762 template <class ELFT>
printSymbol(const ELFO * Obj,const Elf_Sym * Symbol,const Elf_Sym * FirstSym,StringRef StrTable,bool IsDynamic)2763 void GNUStyle<ELFT>::printSymbol(const ELFO *Obj, const Elf_Sym *Symbol,
2764                                  const Elf_Sym *FirstSym, StringRef StrTable,
2765                                  bool IsDynamic) {
2766   static int Idx = 0;
2767   static bool Dynamic = true;
2768   size_t Width;
2769 
2770   // If this function was called with a different value from IsDynamic
2771   // from last call, happens when we move from dynamic to static symbol
2772   // table, "Num" field should be reset.
2773   if (!Dynamic != !IsDynamic) {
2774     Idx = 0;
2775     Dynamic = false;
2776   }
2777   std::string Num, Name, Value, Size, Binding, Type, Visibility, Section;
2778   unsigned Bias = 0;
2779   if (ELFT::Is64Bits) {
2780     Bias = 8;
2781     Width = 16;
2782   } else {
2783     Bias = 0;
2784     Width = 8;
2785   }
2786   Field Fields[8] = {0,         8,         17 + Bias, 23 + Bias,
2787                      31 + Bias, 38 + Bias, 47 + Bias, 51 + Bias};
2788   Num = to_string(format_decimal(Idx++, 6)) + ":";
2789   Value = to_string(format_hex_no_prefix(Symbol->st_value, Width));
2790   Size = to_string(format_decimal(Symbol->st_size, 5));
2791   unsigned char SymbolType = Symbol->getType();
2792   if (Obj->getHeader()->e_machine == ELF::EM_AMDGPU &&
2793       SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
2794     Type = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
2795   else
2796     Type = printEnum(SymbolType, makeArrayRef(ElfSymbolTypes));
2797   unsigned Vis = Symbol->getVisibility();
2798   Binding = printEnum(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
2799   Visibility = printEnum(Vis, makeArrayRef(ElfSymbolVisibilities));
2800   Section = getSymbolSectionNdx(Obj, Symbol, FirstSym);
2801   Name = this->dumper()->getFullSymbolName(Symbol, StrTable, IsDynamic);
2802   Fields[0].Str = Num;
2803   Fields[1].Str = Value;
2804   Fields[2].Str = Size;
2805   Fields[3].Str = Type;
2806   Fields[4].Str = Binding;
2807   Fields[5].Str = Visibility;
2808   Fields[6].Str = Section;
2809   Fields[7].Str = Name;
2810   for (auto &Entry : Fields)
2811     printField(Entry);
2812   OS << "\n";
2813 }
2814 
printSymbols(const ELFO * Obj)2815 template <class ELFT> void GNUStyle<ELFT>::printSymbols(const ELFO *Obj) {
2816   this->dumper()->printSymbolsHelper(true);
2817   this->dumper()->printSymbolsHelper(false);
2818 }
2819 
2820 template <class ELFT>
printDynamicSymbols(const ELFO * Obj)2821 void GNUStyle<ELFT>::printDynamicSymbols(const ELFO *Obj) {
2822   this->dumper()->printSymbolsHelper(true);
2823 }
2824 
printPhdrFlags(unsigned Flag)2825 static inline std::string printPhdrFlags(unsigned Flag) {
2826   std::string Str;
2827   Str = (Flag & PF_R) ? "R" : " ";
2828   Str += (Flag & PF_W) ? "W" : " ";
2829   Str += (Flag & PF_X) ? "E" : " ";
2830   return Str;
2831 }
2832 
2833 // SHF_TLS sections are only in PT_TLS, PT_LOAD or PT_GNU_RELRO
2834 // PT_TLS must only have SHF_TLS sections
2835 template <class ELFT>
checkTLSSections(const Elf_Phdr & Phdr,const Elf_Shdr & Sec)2836 bool GNUStyle<ELFT>::checkTLSSections(const Elf_Phdr &Phdr,
2837                                       const Elf_Shdr &Sec) {
2838   return (((Sec.sh_flags & ELF::SHF_TLS) &&
2839            ((Phdr.p_type == ELF::PT_TLS) || (Phdr.p_type == ELF::PT_LOAD) ||
2840             (Phdr.p_type == ELF::PT_GNU_RELRO))) ||
2841           (!(Sec.sh_flags & ELF::SHF_TLS) && Phdr.p_type != ELF::PT_TLS));
2842 }
2843 
2844 // Non-SHT_NOBITS must have its offset inside the segment
2845 // Only non-zero section can be at end of segment
2846 template <class ELFT>
checkoffsets(const Elf_Phdr & Phdr,const Elf_Shdr & Sec)2847 bool GNUStyle<ELFT>::checkoffsets(const Elf_Phdr &Phdr, const Elf_Shdr &Sec) {
2848   if (Sec.sh_type == ELF::SHT_NOBITS)
2849     return true;
2850   bool IsSpecial =
2851       (Sec.sh_type == ELF::SHT_NOBITS) && ((Sec.sh_flags & ELF::SHF_TLS) != 0);
2852   // .tbss is special, it only has memory in PT_TLS and has NOBITS properties
2853   auto SectionSize =
2854       (IsSpecial && Phdr.p_type != ELF::PT_TLS) ? 0 : Sec.sh_size;
2855   if (Sec.sh_offset >= Phdr.p_offset)
2856     return ((Sec.sh_offset + SectionSize <= Phdr.p_filesz + Phdr.p_offset)
2857             /*only non-zero sized sections at end*/ &&
2858             (Sec.sh_offset + 1 <= Phdr.p_offset + Phdr.p_filesz));
2859   return false;
2860 }
2861 
2862 // SHF_ALLOC must have VMA inside segment
2863 // Only non-zero section can be at end of segment
2864 template <class ELFT>
checkVMA(const Elf_Phdr & Phdr,const Elf_Shdr & Sec)2865 bool GNUStyle<ELFT>::checkVMA(const Elf_Phdr &Phdr, const Elf_Shdr &Sec) {
2866   if (!(Sec.sh_flags & ELF::SHF_ALLOC))
2867     return true;
2868   bool IsSpecial =
2869       (Sec.sh_type == ELF::SHT_NOBITS) && ((Sec.sh_flags & ELF::SHF_TLS) != 0);
2870   // .tbss is special, it only has memory in PT_TLS and has NOBITS properties
2871   auto SectionSize =
2872       (IsSpecial && Phdr.p_type != ELF::PT_TLS) ? 0 : Sec.sh_size;
2873   if (Sec.sh_addr >= Phdr.p_vaddr)
2874     return ((Sec.sh_addr + SectionSize <= Phdr.p_vaddr + Phdr.p_memsz) &&
2875             (Sec.sh_addr + 1 <= Phdr.p_vaddr + Phdr.p_memsz));
2876   return false;
2877 }
2878 
2879 // No section with zero size must be at start or end of PT_DYNAMIC
2880 template <class ELFT>
checkPTDynamic(const Elf_Phdr & Phdr,const Elf_Shdr & Sec)2881 bool GNUStyle<ELFT>::checkPTDynamic(const Elf_Phdr &Phdr, const Elf_Shdr &Sec) {
2882   if (Phdr.p_type != ELF::PT_DYNAMIC || Sec.sh_size != 0 || Phdr.p_memsz == 0)
2883     return true;
2884   // Is section within the phdr both based on offset and VMA ?
2885   return ((Sec.sh_type == ELF::SHT_NOBITS) ||
2886           (Sec.sh_offset > Phdr.p_offset &&
2887            Sec.sh_offset < Phdr.p_offset + Phdr.p_filesz)) &&
2888          (!(Sec.sh_flags & ELF::SHF_ALLOC) ||
2889           (Sec.sh_addr > Phdr.p_vaddr && Sec.sh_addr < Phdr.p_memsz));
2890 }
2891 
2892 template <class ELFT>
printProgramHeaders(const ELFO * Obj)2893 void GNUStyle<ELFT>::printProgramHeaders(const ELFO *Obj) {
2894   unsigned Bias = ELFT::Is64Bits ? 8 : 0;
2895   unsigned Width = ELFT::Is64Bits ? 18 : 10;
2896   unsigned SizeWidth = ELFT::Is64Bits ? 8 : 7;
2897   std::string Type, Offset, VMA, LMA, FileSz, MemSz, Flag, Align;
2898 
2899   const Elf_Ehdr *Header = Obj->getHeader();
2900   Field Fields[8] = {2,         17,        26,        37 + Bias,
2901                      48 + Bias, 56 + Bias, 64 + Bias, 68 + Bias};
2902   OS << "\nElf file type is "
2903      << printEnum(Header->e_type, makeArrayRef(ElfObjectFileType)) << "\n"
2904      << "Entry point " << format_hex(Header->e_entry, 3) << "\n"
2905      << "There are " << Header->e_phnum << " program headers,"
2906      << " starting at offset " << Header->e_phoff << "\n\n"
2907      << "Program Headers:\n";
2908   if (ELFT::Is64Bits)
2909     OS << "  Type           Offset   VirtAddr           PhysAddr         "
2910        << "  FileSiz  MemSiz   Flg Align\n";
2911   else
2912     OS << "  Type           Offset   VirtAddr   PhysAddr   FileSiz "
2913        << "MemSiz  Flg Align\n";
2914   for (const auto &Phdr : Obj->program_headers()) {
2915     Type = getElfPtType(Header->e_machine, Phdr.p_type);
2916     Offset = to_string(format_hex(Phdr.p_offset, 8));
2917     VMA = to_string(format_hex(Phdr.p_vaddr, Width));
2918     LMA = to_string(format_hex(Phdr.p_paddr, Width));
2919     FileSz = to_string(format_hex(Phdr.p_filesz, SizeWidth));
2920     MemSz = to_string(format_hex(Phdr.p_memsz, SizeWidth));
2921     Flag = printPhdrFlags(Phdr.p_flags);
2922     Align = to_string(format_hex(Phdr.p_align, 1));
2923     Fields[0].Str = Type;
2924     Fields[1].Str = Offset;
2925     Fields[2].Str = VMA;
2926     Fields[3].Str = LMA;
2927     Fields[4].Str = FileSz;
2928     Fields[5].Str = MemSz;
2929     Fields[6].Str = Flag;
2930     Fields[7].Str = Align;
2931     for (auto Field : Fields)
2932       printField(Field);
2933     if (Phdr.p_type == ELF::PT_INTERP) {
2934       OS << "\n      [Requesting program interpreter: ";
2935       OS << reinterpret_cast<const char *>(Obj->base()) + Phdr.p_offset << "]";
2936     }
2937     OS << "\n";
2938   }
2939   OS << "\n Section to Segment mapping:\n  Segment Sections...\n";
2940   int Phnum = 0;
2941   for (const Elf_Phdr &Phdr : Obj->program_headers()) {
2942     std::string Sections;
2943     OS << format("   %2.2d     ", Phnum++);
2944     for (const Elf_Shdr &Sec : Obj->sections()) {
2945       // Check if each section is in a segment and then print mapping.
2946       // readelf additionally makes sure it does not print zero sized sections
2947       // at end of segments and for PT_DYNAMIC both start and end of section
2948       // .tbss must only be shown in PT_TLS section.
2949       bool TbssInNonTLS = (Sec.sh_type == ELF::SHT_NOBITS) &&
2950                           ((Sec.sh_flags & ELF::SHF_TLS) != 0) &&
2951                           Phdr.p_type != ELF::PT_TLS;
2952       if (!TbssInNonTLS && checkTLSSections(Phdr, Sec) &&
2953           checkoffsets(Phdr, Sec) && checkVMA(Phdr, Sec) &&
2954           checkPTDynamic(Phdr, Sec) && (Sec.sh_type != ELF::SHT_NULL))
2955         Sections += unwrapOrError(Obj->getSectionName(&Sec)).str() + " ";
2956     }
2957     OS << Sections << "\n";
2958     OS.flush();
2959   }
2960 }
2961 
2962 template <class ELFT>
printDynamicRelocation(const ELFO * Obj,Elf_Rela R,bool IsRela)2963 void GNUStyle<ELFT>::printDynamicRelocation(const ELFO *Obj, Elf_Rela R,
2964                                             bool IsRela) {
2965   SmallString<32> RelocName;
2966   StringRef SymbolName;
2967   unsigned Width = ELFT::Is64Bits ? 16 : 8;
2968   unsigned Bias = ELFT::Is64Bits ? 8 : 0;
2969   // First two fields are bit width dependent. The rest of them are after are
2970   // fixed width.
2971   Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias};
2972 
2973   uint32_t SymIndex = R.getSymbol(Obj->isMips64EL());
2974   const Elf_Sym *Sym = this->dumper()->dynamic_symbols().begin() + SymIndex;
2975   Obj->getRelocationTypeName(R.getType(Obj->isMips64EL()), RelocName);
2976   SymbolName =
2977       unwrapOrError(Sym->getName(this->dumper()->getDynamicStringTable()));
2978   std::string Addend = "", Info, Offset, Value;
2979   Offset = to_string(format_hex_no_prefix(R.r_offset, Width));
2980   Info = to_string(format_hex_no_prefix(R.r_info, Width));
2981   Value = to_string(format_hex_no_prefix(Sym->getValue(), Width));
2982   int64_t RelAddend = R.r_addend;
2983   if (SymbolName.size() && IsRela) {
2984     if (R.r_addend < 0)
2985       Addend = " - ";
2986     else
2987       Addend = " + ";
2988   }
2989 
2990   if (!SymbolName.size() && Sym->getValue() == 0)
2991     Value = "";
2992 
2993   if (IsRela)
2994     Addend += to_string(format_hex_no_prefix(std::abs(RelAddend), 1));
2995 
2996 
2997   Fields[0].Str = Offset;
2998   Fields[1].Str = Info;
2999   Fields[2].Str = RelocName.c_str();
3000   Fields[3].Str = Value;
3001   Fields[4].Str = SymbolName;
3002   for (auto &Field : Fields)
3003     printField(Field);
3004   OS << Addend;
3005   OS << "\n";
3006 }
3007 
3008 template <class ELFT>
printDynamicRelocations(const ELFO * Obj)3009 void GNUStyle<ELFT>::printDynamicRelocations(const ELFO *Obj) {
3010   const DynRegionInfo &DynRelRegion = this->dumper()->getDynRelRegion();
3011   const DynRegionInfo &DynRelaRegion = this->dumper()->getDynRelaRegion();
3012   const DynRegionInfo &DynPLTRelRegion = this->dumper()->getDynPLTRelRegion();
3013   if (DynRelaRegion.Size > 0) {
3014     OS << "\n'RELA' relocation section at offset "
3015        << format_hex(reinterpret_cast<const uint8_t *>(DynRelaRegion.Addr) -
3016                          Obj->base(),
3017                      1) << " contains " << DynRelaRegion.Size << " bytes:\n";
3018     printRelocHeader(OS, ELFT::Is64Bits, true);
3019     for (const Elf_Rela &Rela : this->dumper()->dyn_relas())
3020       printDynamicRelocation(Obj, Rela, true);
3021   }
3022   if (DynRelRegion.Size > 0) {
3023     OS << "\n'REL' relocation section at offset "
3024        << format_hex(reinterpret_cast<const uint8_t *>(DynRelRegion.Addr) -
3025                          Obj->base(),
3026                      1) << " contains " << DynRelRegion.Size << " bytes:\n";
3027     printRelocHeader(OS, ELFT::Is64Bits, false);
3028     for (const Elf_Rel &Rel : this->dumper()->dyn_rels()) {
3029       Elf_Rela Rela;
3030       Rela.r_offset = Rel.r_offset;
3031       Rela.r_info = Rel.r_info;
3032       Rela.r_addend = 0;
3033       printDynamicRelocation(Obj, Rela, false);
3034     }
3035   }
3036   if (DynPLTRelRegion.Size) {
3037     OS << "\n'PLT' relocation section at offset "
3038        << format_hex(reinterpret_cast<const uint8_t *>(DynPLTRelRegion.Addr) -
3039                          Obj->base(),
3040                      1) << " contains " << DynPLTRelRegion.Size << " bytes:\n";
3041   }
3042   if (DynPLTRelRegion.EntSize == sizeof(Elf_Rela)) {
3043     printRelocHeader(OS, ELFT::Is64Bits, true);
3044     for (const Elf_Rela &Rela : DynPLTRelRegion.getAsArrayRef<Elf_Rela>())
3045       printDynamicRelocation(Obj, Rela, true);
3046   } else {
3047     printRelocHeader(OS, ELFT::Is64Bits, false);
3048     for (const Elf_Rel &Rel : DynPLTRelRegion.getAsArrayRef<Elf_Rel>()) {
3049       Elf_Rela Rela;
3050       Rela.r_offset = Rel.r_offset;
3051       Rela.r_info = Rel.r_info;
3052       Rela.r_addend = 0;
3053       printDynamicRelocation(Obj, Rela, false);
3054     }
3055   }
3056 }
3057 
3058 // Hash histogram shows  statistics of how efficient the hash was for the
3059 // dynamic symbol table. The table shows number of hash buckets for different
3060 // lengths of chains as absolute number and percentage of the total buckets.
3061 // Additionally cumulative coverage of symbols for each set of buckets.
3062 template <class ELFT>
printHashHistogram(const ELFFile<ELFT> * Obj)3063 void GNUStyle<ELFT>::printHashHistogram(const ELFFile<ELFT> *Obj) {
3064 
3065   const Elf_Hash *HashTable = this->dumper()->getHashTable();
3066   const Elf_GnuHash *GnuHashTable = this->dumper()->getGnuHashTable();
3067 
3068   // Print histogram for .hash section
3069   if (HashTable) {
3070     size_t NBucket = HashTable->nbucket;
3071     size_t NChain = HashTable->nchain;
3072     ArrayRef<Elf_Word> Buckets = HashTable->buckets();
3073     ArrayRef<Elf_Word> Chains = HashTable->chains();
3074     size_t TotalSyms = 0;
3075     // If hash table is correct, we have at least chains with 0 length
3076     size_t MaxChain = 1;
3077     size_t CumulativeNonZero = 0;
3078 
3079     if (NChain == 0 || NBucket == 0)
3080       return;
3081 
3082     std::vector<size_t> ChainLen(NBucket, 0);
3083     // Go over all buckets and and note chain lengths of each bucket (total
3084     // unique chain lengths).
3085     for (size_t B = 0; B < NBucket; B++) {
3086       for (size_t C = Buckets[B]; C > 0 && C < NChain; C = Chains[C])
3087         if (MaxChain <= ++ChainLen[B])
3088           MaxChain++;
3089       TotalSyms += ChainLen[B];
3090     }
3091 
3092     if (!TotalSyms)
3093       return;
3094 
3095     std::vector<size_t> Count(MaxChain, 0) ;
3096     // Count how long is the chain for each bucket
3097     for (size_t B = 0; B < NBucket; B++)
3098       ++Count[ChainLen[B]];
3099     // Print Number of buckets with each chain lengths and their cumulative
3100     // coverage of the symbols
3101     OS << "Histogram for bucket list length (total of " << NBucket
3102        << " buckets)\n"
3103        << " Length  Number     % of total  Coverage\n";
3104     for (size_t I = 0; I < MaxChain; I++) {
3105       CumulativeNonZero += Count[I] * I;
3106       OS << format("%7lu  %-10lu (%5.1f%%)     %5.1f%%\n", I, Count[I],
3107                    (Count[I] * 100.0) / NBucket,
3108                    (CumulativeNonZero * 100.0) / TotalSyms);
3109     }
3110   }
3111 
3112   // Print histogram for .gnu.hash section
3113   if (GnuHashTable) {
3114     size_t NBucket = GnuHashTable->nbuckets;
3115     ArrayRef<Elf_Word> Buckets = GnuHashTable->buckets();
3116     unsigned NumSyms = this->dumper()->dynamic_symbols().size();
3117     if (!NumSyms)
3118       return;
3119     ArrayRef<Elf_Word> Chains = GnuHashTable->values(NumSyms);
3120     size_t Symndx = GnuHashTable->symndx;
3121     size_t TotalSyms = 0;
3122     size_t MaxChain = 1;
3123     size_t CumulativeNonZero = 0;
3124 
3125     if (Chains.size() == 0 || NBucket == 0)
3126       return;
3127 
3128     std::vector<size_t> ChainLen(NBucket, 0);
3129 
3130     for (size_t B = 0; B < NBucket; B++) {
3131       if (!Buckets[B])
3132         continue;
3133       size_t Len = 1;
3134       for (size_t C = Buckets[B] - Symndx;
3135            C < Chains.size() && (Chains[C] & 1) == 0; C++)
3136         if (MaxChain < ++Len)
3137           MaxChain++;
3138       ChainLen[B] = Len;
3139       TotalSyms += Len;
3140     }
3141     MaxChain++;
3142 
3143     if (!TotalSyms)
3144       return;
3145 
3146     std::vector<size_t> Count(MaxChain, 0) ;
3147     for (size_t B = 0; B < NBucket; B++)
3148       ++Count[ChainLen[B]];
3149     // Print Number of buckets with each chain lengths and their cumulative
3150     // coverage of the symbols
3151     OS << "Histogram for `.gnu.hash' bucket list length (total of " << NBucket
3152        << " buckets)\n"
3153        << " Length  Number     % of total  Coverage\n";
3154     for (size_t I = 0; I <MaxChain; I++) {
3155       CumulativeNonZero += Count[I] * I;
3156       OS << format("%7lu  %-10lu (%5.1f%%)     %5.1f%%\n", I, Count[I],
3157                    (Count[I] * 100.0) / NBucket,
3158                    (CumulativeNonZero * 100.0) / TotalSyms);
3159     }
3160   }
3161 }
3162 
printFileHeaders(const ELFO * Obj)3163 template <class ELFT> void LLVMStyle<ELFT>::printFileHeaders(const ELFO *Obj) {
3164   const Elf_Ehdr *e = Obj->getHeader();
3165   {
3166     DictScope D(W, "ElfHeader");
3167     {
3168       DictScope D(W, "Ident");
3169       W.printBinary("Magic", makeArrayRef(e->e_ident).slice(ELF::EI_MAG0, 4));
3170       W.printEnum("Class", e->e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
3171       W.printEnum("DataEncoding", e->e_ident[ELF::EI_DATA],
3172                   makeArrayRef(ElfDataEncoding));
3173       W.printNumber("FileVersion", e->e_ident[ELF::EI_VERSION]);
3174 
3175       // Handle architecture specific OS/ABI values.
3176       if (e->e_machine == ELF::EM_AMDGPU &&
3177           e->e_ident[ELF::EI_OSABI] == ELF::ELFOSABI_AMDGPU_HSA)
3178         W.printHex("OS/ABI", "AMDGPU_HSA", ELF::ELFOSABI_AMDGPU_HSA);
3179       else
3180         W.printEnum("OS/ABI", e->e_ident[ELF::EI_OSABI],
3181                     makeArrayRef(ElfOSABI));
3182       W.printNumber("ABIVersion", e->e_ident[ELF::EI_ABIVERSION]);
3183       W.printBinary("Unused", makeArrayRef(e->e_ident).slice(ELF::EI_PAD));
3184     }
3185 
3186     W.printEnum("Type", e->e_type, makeArrayRef(ElfObjectFileType));
3187     W.printEnum("Machine", e->e_machine, makeArrayRef(ElfMachineType));
3188     W.printNumber("Version", e->e_version);
3189     W.printHex("Entry", e->e_entry);
3190     W.printHex("ProgramHeaderOffset", e->e_phoff);
3191     W.printHex("SectionHeaderOffset", e->e_shoff);
3192     if (e->e_machine == EM_MIPS)
3193       W.printFlags("Flags", e->e_flags, makeArrayRef(ElfHeaderMipsFlags),
3194                    unsigned(ELF::EF_MIPS_ARCH), unsigned(ELF::EF_MIPS_ABI),
3195                    unsigned(ELF::EF_MIPS_MACH));
3196     else
3197       W.printFlags("Flags", e->e_flags);
3198     W.printNumber("HeaderSize", e->e_ehsize);
3199     W.printNumber("ProgramHeaderEntrySize", e->e_phentsize);
3200     W.printNumber("ProgramHeaderCount", e->e_phnum);
3201     W.printNumber("SectionHeaderEntrySize", e->e_shentsize);
3202     W.printNumber("SectionHeaderCount", e->e_shnum);
3203     W.printNumber("StringTableSectionIndex", e->e_shstrndx);
3204   }
3205 }
3206 
3207 template <class ELFT>
printGroupSections(const ELFO * Obj)3208 void LLVMStyle<ELFT>::printGroupSections(const ELFO *Obj) {
3209   DictScope Lists(W, "Groups");
3210   uint32_t SectionIndex = 0;
3211   bool HasGroups = false;
3212   for (const Elf_Shdr &Sec : Obj->sections()) {
3213     if (Sec.sh_type == ELF::SHT_GROUP) {
3214       HasGroups = true;
3215       const Elf_Shdr *Symtab = unwrapOrError(Obj->getSection(Sec.sh_link));
3216       StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*Symtab));
3217       const Elf_Sym *Sym = Obj->template getEntry<Elf_Sym>(Symtab, Sec.sh_info);
3218       auto Data = unwrapOrError(
3219           Obj->template getSectionContentsAsArray<Elf_Word>(&Sec));
3220       DictScope D(W, "Group");
3221       StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
3222       W.printNumber("Name", Name, Sec.sh_name);
3223       W.printNumber("Index", SectionIndex);
3224       W.printHex("Type", getGroupType(Data[0]), Data[0]);
3225       W.startLine() << "Signature: " << StrTable.data() + Sym->st_name << "\n";
3226       {
3227         ListScope L(W, "Section(s) in group");
3228         size_t Member = 1;
3229         while (Member < Data.size()) {
3230           auto Sec = unwrapOrError(Obj->getSection(Data[Member]));
3231           const StringRef Name = unwrapOrError(Obj->getSectionName(Sec));
3232           W.startLine() << Name << " (" << Data[Member++] << ")\n";
3233         }
3234       }
3235     }
3236     ++SectionIndex;
3237   }
3238   if (!HasGroups)
3239     W.startLine() << "There are no group sections in the file.\n";
3240 }
3241 
printRelocations(const ELFO * Obj)3242 template <class ELFT> void LLVMStyle<ELFT>::printRelocations(const ELFO *Obj) {
3243   ListScope D(W, "Relocations");
3244 
3245   int SectionNumber = -1;
3246   for (const Elf_Shdr &Sec : Obj->sections()) {
3247     ++SectionNumber;
3248 
3249     if (Sec.sh_type != ELF::SHT_REL && Sec.sh_type != ELF::SHT_RELA)
3250       continue;
3251 
3252     StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
3253 
3254     W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
3255     W.indent();
3256 
3257     printRelocations(&Sec, Obj);
3258 
3259     W.unindent();
3260     W.startLine() << "}\n";
3261   }
3262 }
3263 
3264 template <class ELFT>
printRelocations(const Elf_Shdr * Sec,const ELFO * Obj)3265 void LLVMStyle<ELFT>::printRelocations(const Elf_Shdr *Sec, const ELFO *Obj) {
3266   const Elf_Shdr *SymTab = unwrapOrError(Obj->getSection(Sec->sh_link));
3267 
3268   switch (Sec->sh_type) {
3269   case ELF::SHT_REL:
3270     for (const Elf_Rel &R : Obj->rels(Sec)) {
3271       Elf_Rela Rela;
3272       Rela.r_offset = R.r_offset;
3273       Rela.r_info = R.r_info;
3274       Rela.r_addend = 0;
3275       printRelocation(Obj, Rela, SymTab);
3276     }
3277     break;
3278   case ELF::SHT_RELA:
3279     for (const Elf_Rela &R : Obj->relas(Sec))
3280       printRelocation(Obj, R, SymTab);
3281     break;
3282   }
3283 }
3284 
3285 template <class ELFT>
printRelocation(const ELFO * Obj,Elf_Rela Rel,const Elf_Shdr * SymTab)3286 void LLVMStyle<ELFT>::printRelocation(const ELFO *Obj, Elf_Rela Rel,
3287                                       const Elf_Shdr *SymTab) {
3288   SmallString<32> RelocName;
3289   Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
3290   StringRef TargetName;
3291   const Elf_Sym *Sym = Obj->getRelocationSymbol(&Rel, SymTab);
3292   if (Sym && Sym->getType() == ELF::STT_SECTION) {
3293     const Elf_Shdr *Sec = unwrapOrError(
3294         Obj->getSection(Sym, SymTab, this->dumper()->getShndxTable()));
3295     TargetName = unwrapOrError(Obj->getSectionName(Sec));
3296   } else if (Sym) {
3297     StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*SymTab));
3298     TargetName = unwrapOrError(Sym->getName(StrTable));
3299   }
3300 
3301   if (opts::ExpandRelocs) {
3302     DictScope Group(W, "Relocation");
3303     W.printHex("Offset", Rel.r_offset);
3304     W.printNumber("Type", RelocName, (int)Rel.getType(Obj->isMips64EL()));
3305     W.printNumber("Symbol", TargetName.size() > 0 ? TargetName : "-",
3306                   Rel.getSymbol(Obj->isMips64EL()));
3307     W.printHex("Addend", Rel.r_addend);
3308   } else {
3309     raw_ostream &OS = W.startLine();
3310     OS << W.hex(Rel.r_offset) << " " << RelocName << " "
3311        << (TargetName.size() > 0 ? TargetName : "-") << " "
3312        << W.hex(Rel.r_addend) << "\n";
3313   }
3314 }
3315 
printSections(const ELFO * Obj)3316 template <class ELFT> void LLVMStyle<ELFT>::printSections(const ELFO *Obj) {
3317   ListScope SectionsD(W, "Sections");
3318 
3319   int SectionIndex = -1;
3320   for (const Elf_Shdr &Sec : Obj->sections()) {
3321     ++SectionIndex;
3322 
3323     StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
3324 
3325     DictScope SectionD(W, "Section");
3326     W.printNumber("Index", SectionIndex);
3327     W.printNumber("Name", Name, Sec.sh_name);
3328     W.printHex("Type",
3329                getElfSectionType(Obj->getHeader()->e_machine, Sec.sh_type),
3330                Sec.sh_type);
3331     std::vector<EnumEntry<unsigned>> SectionFlags(std::begin(ElfSectionFlags),
3332                                                   std::end(ElfSectionFlags));
3333     switch (Obj->getHeader()->e_machine) {
3334     case EM_AMDGPU:
3335       SectionFlags.insert(SectionFlags.end(), std::begin(ElfAMDGPUSectionFlags),
3336                           std::end(ElfAMDGPUSectionFlags));
3337       break;
3338     case EM_HEXAGON:
3339       SectionFlags.insert(SectionFlags.end(),
3340                           std::begin(ElfHexagonSectionFlags),
3341                           std::end(ElfHexagonSectionFlags));
3342       break;
3343     case EM_MIPS:
3344       SectionFlags.insert(SectionFlags.end(), std::begin(ElfMipsSectionFlags),
3345                           std::end(ElfMipsSectionFlags));
3346       break;
3347     case EM_X86_64:
3348       SectionFlags.insert(SectionFlags.end(), std::begin(ElfX86_64SectionFlags),
3349                           std::end(ElfX86_64SectionFlags));
3350       break;
3351     case EM_XCORE:
3352       SectionFlags.insert(SectionFlags.end(), std::begin(ElfXCoreSectionFlags),
3353                           std::end(ElfXCoreSectionFlags));
3354       break;
3355     default:
3356       // Nothing to do.
3357       break;
3358     }
3359     W.printFlags("Flags", Sec.sh_flags, makeArrayRef(SectionFlags));
3360     W.printHex("Address", Sec.sh_addr);
3361     W.printHex("Offset", Sec.sh_offset);
3362     W.printNumber("Size", Sec.sh_size);
3363     W.printNumber("Link", Sec.sh_link);
3364     W.printNumber("Info", Sec.sh_info);
3365     W.printNumber("AddressAlignment", Sec.sh_addralign);
3366     W.printNumber("EntrySize", Sec.sh_entsize);
3367 
3368     if (opts::SectionRelocations) {
3369       ListScope D(W, "Relocations");
3370       printRelocations(&Sec, Obj);
3371     }
3372 
3373     if (opts::SectionSymbols) {
3374       ListScope D(W, "Symbols");
3375       const Elf_Shdr *Symtab = this->dumper()->getDotSymtabSec();
3376       StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*Symtab));
3377 
3378       for (const Elf_Sym &Sym : Obj->symbols(Symtab)) {
3379         const Elf_Shdr *SymSec = unwrapOrError(
3380             Obj->getSection(&Sym, Symtab, this->dumper()->getShndxTable()));
3381         if (SymSec == &Sec)
3382           printSymbol(Obj, &Sym, Obj->symbol_begin(Symtab), StrTable, false);
3383       }
3384     }
3385 
3386     if (opts::SectionData && Sec.sh_type != ELF::SHT_NOBITS) {
3387       ArrayRef<uint8_t> Data = unwrapOrError(Obj->getSectionContents(&Sec));
3388       W.printBinaryBlock("SectionData",
3389                          StringRef((const char *)Data.data(), Data.size()));
3390     }
3391   }
3392 }
3393 
3394 template <class ELFT>
printSymbol(const ELFO * Obj,const Elf_Sym * Symbol,const Elf_Sym * First,StringRef StrTable,bool IsDynamic)3395 void LLVMStyle<ELFT>::printSymbol(const ELFO *Obj, const Elf_Sym *Symbol,
3396                                   const Elf_Sym *First, StringRef StrTable,
3397                                   bool IsDynamic) {
3398   unsigned SectionIndex = 0;
3399   StringRef SectionName;
3400   getSectionNameIndex(*Obj, Symbol, First, this->dumper()->getShndxTable(),
3401                       SectionName, SectionIndex);
3402   std::string FullSymbolName =
3403       this->dumper()->getFullSymbolName(Symbol, StrTable, IsDynamic);
3404   unsigned char SymbolType = Symbol->getType();
3405 
3406   DictScope D(W, "Symbol");
3407   W.printNumber("Name", FullSymbolName, Symbol->st_name);
3408   W.printHex("Value", Symbol->st_value);
3409   W.printNumber("Size", Symbol->st_size);
3410   W.printEnum("Binding", Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
3411   if (Obj->getHeader()->e_machine == ELF::EM_AMDGPU &&
3412       SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
3413     W.printEnum("Type", SymbolType, makeArrayRef(AMDGPUSymbolTypes));
3414   else
3415     W.printEnum("Type", SymbolType, makeArrayRef(ElfSymbolTypes));
3416   if (Symbol->st_other == 0)
3417     // Usually st_other flag is zero. Do not pollute the output
3418     // by flags enumeration in that case.
3419     W.printNumber("Other", 0);
3420   else {
3421     std::vector<EnumEntry<unsigned>> SymOtherFlags(std::begin(ElfSymOtherFlags),
3422                                                    std::end(ElfSymOtherFlags));
3423     if (Obj->getHeader()->e_machine == EM_MIPS) {
3424       // Someones in their infinite wisdom decided to make STO_MIPS_MIPS16
3425       // flag overlapped with other ST_MIPS_xxx flags. So consider both
3426       // cases separately.
3427       if ((Symbol->st_other & STO_MIPS_MIPS16) == STO_MIPS_MIPS16)
3428         SymOtherFlags.insert(SymOtherFlags.end(),
3429                              std::begin(ElfMips16SymOtherFlags),
3430                              std::end(ElfMips16SymOtherFlags));
3431       else
3432         SymOtherFlags.insert(SymOtherFlags.end(),
3433                              std::begin(ElfMipsSymOtherFlags),
3434                              std::end(ElfMipsSymOtherFlags));
3435     }
3436     W.printFlags("Other", Symbol->st_other, makeArrayRef(SymOtherFlags), 0x3u);
3437   }
3438   W.printHex("Section", SectionName, SectionIndex);
3439 }
3440 
printSymbols(const ELFO * Obj)3441 template <class ELFT> void LLVMStyle<ELFT>::printSymbols(const ELFO *Obj) {
3442   ListScope Group(W, "Symbols");
3443   this->dumper()->printSymbolsHelper(false);
3444 }
3445 
3446 template <class ELFT>
printDynamicSymbols(const ELFO * Obj)3447 void LLVMStyle<ELFT>::printDynamicSymbols(const ELFO *Obj) {
3448   ListScope Group(W, "DynamicSymbols");
3449   this->dumper()->printSymbolsHelper(true);
3450 }
3451 
3452 template <class ELFT>
printDynamicRelocations(const ELFO * Obj)3453 void LLVMStyle<ELFT>::printDynamicRelocations(const ELFO *Obj) {
3454   const DynRegionInfo &DynRelRegion = this->dumper()->getDynRelRegion();
3455   const DynRegionInfo &DynRelaRegion = this->dumper()->getDynRelaRegion();
3456   const DynRegionInfo &DynPLTRelRegion = this->dumper()->getDynPLTRelRegion();
3457   if (DynRelRegion.Size && DynRelaRegion.Size)
3458     report_fatal_error("There are both REL and RELA dynamic relocations");
3459   W.startLine() << "Dynamic Relocations {\n";
3460   W.indent();
3461   if (DynRelaRegion.Size > 0)
3462     for (const Elf_Rela &Rela : this->dumper()->dyn_relas())
3463       printDynamicRelocation(Obj, Rela);
3464   else
3465     for (const Elf_Rel &Rel : this->dumper()->dyn_rels()) {
3466       Elf_Rela Rela;
3467       Rela.r_offset = Rel.r_offset;
3468       Rela.r_info = Rel.r_info;
3469       Rela.r_addend = 0;
3470       printDynamicRelocation(Obj, Rela);
3471     }
3472   if (DynPLTRelRegion.EntSize == sizeof(Elf_Rela))
3473     for (const Elf_Rela &Rela : DynPLTRelRegion.getAsArrayRef<Elf_Rela>())
3474       printDynamicRelocation(Obj, Rela);
3475   else
3476     for (const Elf_Rel &Rel : DynPLTRelRegion.getAsArrayRef<Elf_Rel>()) {
3477       Elf_Rela Rela;
3478       Rela.r_offset = Rel.r_offset;
3479       Rela.r_info = Rel.r_info;
3480       Rela.r_addend = 0;
3481       printDynamicRelocation(Obj, Rela);
3482     }
3483   W.unindent();
3484   W.startLine() << "}\n";
3485 }
3486 
3487 template <class ELFT>
printDynamicRelocation(const ELFO * Obj,Elf_Rela Rel)3488 void LLVMStyle<ELFT>::printDynamicRelocation(const ELFO *Obj, Elf_Rela Rel) {
3489   SmallString<32> RelocName;
3490   Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
3491   StringRef SymbolName;
3492   uint32_t SymIndex = Rel.getSymbol(Obj->isMips64EL());
3493   const Elf_Sym *Sym = this->dumper()->dynamic_symbols().begin() + SymIndex;
3494   SymbolName =
3495       unwrapOrError(Sym->getName(this->dumper()->getDynamicStringTable()));
3496   if (opts::ExpandRelocs) {
3497     DictScope Group(W, "Relocation");
3498     W.printHex("Offset", Rel.r_offset);
3499     W.printNumber("Type", RelocName, (int)Rel.getType(Obj->isMips64EL()));
3500     W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
3501     W.printHex("Addend", Rel.r_addend);
3502   } else {
3503     raw_ostream &OS = W.startLine();
3504     OS << W.hex(Rel.r_offset) << " " << RelocName << " "
3505        << (SymbolName.size() > 0 ? SymbolName : "-") << " "
3506        << W.hex(Rel.r_addend) << "\n";
3507   }
3508 }
3509 
3510 template <class ELFT>
printProgramHeaders(const ELFO * Obj)3511 void LLVMStyle<ELFT>::printProgramHeaders(const ELFO *Obj) {
3512   ListScope L(W, "ProgramHeaders");
3513 
3514   for (const Elf_Phdr &Phdr : Obj->program_headers()) {
3515     DictScope P(W, "ProgramHeader");
3516     W.printHex("Type",
3517                getElfSegmentType(Obj->getHeader()->e_machine, Phdr.p_type),
3518                Phdr.p_type);
3519     W.printHex("Offset", Phdr.p_offset);
3520     W.printHex("VirtualAddress", Phdr.p_vaddr);
3521     W.printHex("PhysicalAddress", Phdr.p_paddr);
3522     W.printNumber("FileSize", Phdr.p_filesz);
3523     W.printNumber("MemSize", Phdr.p_memsz);
3524     W.printFlags("Flags", Phdr.p_flags, makeArrayRef(ElfSegmentFlags));
3525     W.printNumber("Alignment", Phdr.p_align);
3526   }
3527 }
3528 template <class ELFT>
printHashHistogram(const ELFFile<ELFT> * Obj)3529 void LLVMStyle<ELFT>::printHashHistogram(const ELFFile<ELFT> *Obj) {
3530   W.startLine() << "Hash Histogram not implemented!\n";
3531 }
3532