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 "llvm-readobj.h"
16#include "ARMAttributeParser.h"
17#include "ARMEHABIPrinter.h"
18#include "Error.h"
19#include "ObjDumper.h"
20#include "StreamWriter.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/MathExtras.h"
29#include "llvm/Support/raw_ostream.h"
30
31using namespace llvm;
32using namespace llvm::object;
33using namespace ELF;
34
35#define LLVM_READOBJ_ENUM_CASE(ns, enum) \
36  case ns::enum: return #enum;
37
38namespace {
39
40template<typename ELFT>
41class ELFDumper : public ObjDumper {
42public:
43  ELFDumper(const ELFFile<ELFT> *Obj, StreamWriter &Writer)
44      : ObjDumper(Writer), Obj(Obj) {}
45
46  void printFileHeaders() override;
47  void printSections() override;
48  void printRelocations() override;
49  void printSymbols() override;
50  void printDynamicSymbols() override;
51  void printUnwindInfo() override;
52
53  void printDynamicTable() override;
54  void printNeededLibraries() override;
55  void printProgramHeaders() override;
56
57  void printAttributes() override;
58  void printMipsPLTGOT() override;
59
60private:
61  typedef ELFFile<ELFT> ELFO;
62  typedef typename ELFO::Elf_Shdr Elf_Shdr;
63  typedef typename ELFO::Elf_Sym Elf_Sym;
64
65  void printSymbol(typename ELFO::Elf_Sym_Iter Symbol);
66
67  void printRelocations(const Elf_Shdr *Sec);
68  void printRelocation(const Elf_Shdr *Sec, typename ELFO::Elf_Rela Rel);
69
70  const ELFO *Obj;
71};
72
73template <class T> T errorOrDefault(ErrorOr<T> Val, T Default = T()) {
74  if (!Val) {
75    error(Val.getError());
76    return Default;
77  }
78
79  return *Val;
80}
81} // namespace
82
83namespace llvm {
84
85template <class ELFT>
86static std::error_code createELFDumper(const ELFFile<ELFT> *Obj,
87                                       StreamWriter &Writer,
88                                       std::unique_ptr<ObjDumper> &Result) {
89  Result.reset(new ELFDumper<ELFT>(Obj, Writer));
90  return readobj_error::success;
91}
92
93std::error_code createELFDumper(const object::ObjectFile *Obj,
94                                StreamWriter &Writer,
95                                std::unique_ptr<ObjDumper> &Result) {
96  // Little-endian 32-bit
97  if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
98    return createELFDumper(ELFObj->getELFFile(), Writer, Result);
99
100  // Big-endian 32-bit
101  if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
102    return createELFDumper(ELFObj->getELFFile(), Writer, Result);
103
104  // Little-endian 64-bit
105  if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
106    return createELFDumper(ELFObj->getELFFile(), Writer, Result);
107
108  // Big-endian 64-bit
109  if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
110    return createELFDumper(ELFObj->getELFFile(), Writer, Result);
111
112  return readobj_error::unsupported_obj_file_format;
113}
114
115} // namespace llvm
116
117template <typename ELFO>
118static std::string getFullSymbolName(const ELFO &Obj,
119                                     typename ELFO::Elf_Sym_Iter Symbol) {
120  StringRef SymbolName = errorOrDefault(Obj.getSymbolName(Symbol));
121  if (!Symbol.isDynamic())
122    return SymbolName;
123
124  std::string FullSymbolName(SymbolName);
125
126  bool IsDefault;
127  ErrorOr<StringRef> Version =
128      Obj.getSymbolVersion(nullptr, &*Symbol, IsDefault);
129  if (Version) {
130    FullSymbolName += (IsDefault ? "@@" : "@");
131    FullSymbolName += *Version;
132  } else
133    error(Version.getError());
134  return FullSymbolName;
135}
136
137template <typename ELFO>
138static void
139getSectionNameIndex(const ELFO &Obj, typename ELFO::Elf_Sym_Iter Symbol,
140                    StringRef &SectionName, unsigned &SectionIndex) {
141  SectionIndex = Symbol->st_shndx;
142  if (SectionIndex == SHN_UNDEF) {
143    SectionName = "Undefined";
144  } else if (SectionIndex >= SHN_LOPROC && SectionIndex <= SHN_HIPROC) {
145    SectionName = "Processor Specific";
146  } else if (SectionIndex >= SHN_LOOS && SectionIndex <= SHN_HIOS) {
147    SectionName = "Operating System Specific";
148  } else if (SectionIndex > SHN_HIOS && SectionIndex < SHN_ABS) {
149    SectionName = "Reserved";
150  } else if (SectionIndex == SHN_ABS) {
151    SectionName = "Absolute";
152  } else if (SectionIndex == SHN_COMMON) {
153    SectionName = "Common";
154  } else {
155    if (SectionIndex == SHN_XINDEX)
156      SectionIndex = Obj.getSymbolTableIndex(&*Symbol);
157    assert(SectionIndex != SHN_XINDEX &&
158           "getSymbolTableIndex should handle this");
159    const typename ELFO::Elf_Shdr *Sec = Obj.getSection(SectionIndex);
160    SectionName = errorOrDefault(Obj.getSectionName(Sec));
161  }
162}
163
164template <class ELFT>
165static const typename ELFFile<ELFT>::Elf_Shdr *
166findSectionByAddress(const ELFFile<ELFT> *Obj, uint64_t Addr) {
167  for (const auto &Shdr : Obj->sections())
168    if (Shdr.sh_addr == Addr)
169      return &Shdr;
170  return nullptr;
171}
172
173static const EnumEntry<unsigned> ElfClass[] = {
174  { "None",   ELF::ELFCLASSNONE },
175  { "32-bit", ELF::ELFCLASS32   },
176  { "64-bit", ELF::ELFCLASS64   },
177};
178
179static const EnumEntry<unsigned> ElfDataEncoding[] = {
180  { "None",         ELF::ELFDATANONE },
181  { "LittleEndian", ELF::ELFDATA2LSB },
182  { "BigEndian",    ELF::ELFDATA2MSB },
183};
184
185static const EnumEntry<unsigned> ElfObjectFileType[] = {
186  { "None",         ELF::ET_NONE },
187  { "Relocatable",  ELF::ET_REL  },
188  { "Executable",   ELF::ET_EXEC },
189  { "SharedObject", ELF::ET_DYN  },
190  { "Core",         ELF::ET_CORE },
191};
192
193static const EnumEntry<unsigned> ElfOSABI[] = {
194  { "SystemV",      ELF::ELFOSABI_NONE         },
195  { "HPUX",         ELF::ELFOSABI_HPUX         },
196  { "NetBSD",       ELF::ELFOSABI_NETBSD       },
197  { "GNU/Linux",    ELF::ELFOSABI_LINUX        },
198  { "GNU/Hurd",     ELF::ELFOSABI_HURD         },
199  { "Solaris",      ELF::ELFOSABI_SOLARIS      },
200  { "AIX",          ELF::ELFOSABI_AIX          },
201  { "IRIX",         ELF::ELFOSABI_IRIX         },
202  { "FreeBSD",      ELF::ELFOSABI_FREEBSD      },
203  { "TRU64",        ELF::ELFOSABI_TRU64        },
204  { "Modesto",      ELF::ELFOSABI_MODESTO      },
205  { "OpenBSD",      ELF::ELFOSABI_OPENBSD      },
206  { "OpenVMS",      ELF::ELFOSABI_OPENVMS      },
207  { "NSK",          ELF::ELFOSABI_NSK          },
208  { "AROS",         ELF::ELFOSABI_AROS         },
209  { "FenixOS",      ELF::ELFOSABI_FENIXOS      },
210  { "C6000_ELFABI", ELF::ELFOSABI_C6000_ELFABI },
211  { "C6000_LINUX" , ELF::ELFOSABI_C6000_LINUX  },
212  { "ARM",          ELF::ELFOSABI_ARM          },
213  { "Standalone"  , ELF::ELFOSABI_STANDALONE   }
214};
215
216static const EnumEntry<unsigned> ElfMachineType[] = {
217  LLVM_READOBJ_ENUM_ENT(ELF, EM_NONE         ),
218  LLVM_READOBJ_ENUM_ENT(ELF, EM_M32          ),
219  LLVM_READOBJ_ENUM_ENT(ELF, EM_SPARC        ),
220  LLVM_READOBJ_ENUM_ENT(ELF, EM_386          ),
221  LLVM_READOBJ_ENUM_ENT(ELF, EM_68K          ),
222  LLVM_READOBJ_ENUM_ENT(ELF, EM_88K          ),
223  LLVM_READOBJ_ENUM_ENT(ELF, EM_486          ),
224  LLVM_READOBJ_ENUM_ENT(ELF, EM_860          ),
225  LLVM_READOBJ_ENUM_ENT(ELF, EM_MIPS         ),
226  LLVM_READOBJ_ENUM_ENT(ELF, EM_S370         ),
227  LLVM_READOBJ_ENUM_ENT(ELF, EM_MIPS_RS3_LE  ),
228  LLVM_READOBJ_ENUM_ENT(ELF, EM_PARISC       ),
229  LLVM_READOBJ_ENUM_ENT(ELF, EM_VPP500       ),
230  LLVM_READOBJ_ENUM_ENT(ELF, EM_SPARC32PLUS  ),
231  LLVM_READOBJ_ENUM_ENT(ELF, EM_960          ),
232  LLVM_READOBJ_ENUM_ENT(ELF, EM_PPC          ),
233  LLVM_READOBJ_ENUM_ENT(ELF, EM_PPC64        ),
234  LLVM_READOBJ_ENUM_ENT(ELF, EM_S390         ),
235  LLVM_READOBJ_ENUM_ENT(ELF, EM_SPU          ),
236  LLVM_READOBJ_ENUM_ENT(ELF, EM_V800         ),
237  LLVM_READOBJ_ENUM_ENT(ELF, EM_FR20         ),
238  LLVM_READOBJ_ENUM_ENT(ELF, EM_RH32         ),
239  LLVM_READOBJ_ENUM_ENT(ELF, EM_RCE          ),
240  LLVM_READOBJ_ENUM_ENT(ELF, EM_ARM          ),
241  LLVM_READOBJ_ENUM_ENT(ELF, EM_ALPHA        ),
242  LLVM_READOBJ_ENUM_ENT(ELF, EM_SH           ),
243  LLVM_READOBJ_ENUM_ENT(ELF, EM_SPARCV9      ),
244  LLVM_READOBJ_ENUM_ENT(ELF, EM_TRICORE      ),
245  LLVM_READOBJ_ENUM_ENT(ELF, EM_ARC          ),
246  LLVM_READOBJ_ENUM_ENT(ELF, EM_H8_300       ),
247  LLVM_READOBJ_ENUM_ENT(ELF, EM_H8_300H      ),
248  LLVM_READOBJ_ENUM_ENT(ELF, EM_H8S          ),
249  LLVM_READOBJ_ENUM_ENT(ELF, EM_H8_500       ),
250  LLVM_READOBJ_ENUM_ENT(ELF, EM_IA_64        ),
251  LLVM_READOBJ_ENUM_ENT(ELF, EM_MIPS_X       ),
252  LLVM_READOBJ_ENUM_ENT(ELF, EM_COLDFIRE     ),
253  LLVM_READOBJ_ENUM_ENT(ELF, EM_68HC12       ),
254  LLVM_READOBJ_ENUM_ENT(ELF, EM_MMA          ),
255  LLVM_READOBJ_ENUM_ENT(ELF, EM_PCP          ),
256  LLVM_READOBJ_ENUM_ENT(ELF, EM_NCPU         ),
257  LLVM_READOBJ_ENUM_ENT(ELF, EM_NDR1         ),
258  LLVM_READOBJ_ENUM_ENT(ELF, EM_STARCORE     ),
259  LLVM_READOBJ_ENUM_ENT(ELF, EM_ME16         ),
260  LLVM_READOBJ_ENUM_ENT(ELF, EM_ST100        ),
261  LLVM_READOBJ_ENUM_ENT(ELF, EM_TINYJ        ),
262  LLVM_READOBJ_ENUM_ENT(ELF, EM_X86_64       ),
263  LLVM_READOBJ_ENUM_ENT(ELF, EM_PDSP         ),
264  LLVM_READOBJ_ENUM_ENT(ELF, EM_PDP10        ),
265  LLVM_READOBJ_ENUM_ENT(ELF, EM_PDP11        ),
266  LLVM_READOBJ_ENUM_ENT(ELF, EM_FX66         ),
267  LLVM_READOBJ_ENUM_ENT(ELF, EM_ST9PLUS      ),
268  LLVM_READOBJ_ENUM_ENT(ELF, EM_ST7          ),
269  LLVM_READOBJ_ENUM_ENT(ELF, EM_68HC16       ),
270  LLVM_READOBJ_ENUM_ENT(ELF, EM_68HC11       ),
271  LLVM_READOBJ_ENUM_ENT(ELF, EM_68HC08       ),
272  LLVM_READOBJ_ENUM_ENT(ELF, EM_68HC05       ),
273  LLVM_READOBJ_ENUM_ENT(ELF, EM_SVX          ),
274  LLVM_READOBJ_ENUM_ENT(ELF, EM_ST19         ),
275  LLVM_READOBJ_ENUM_ENT(ELF, EM_VAX          ),
276  LLVM_READOBJ_ENUM_ENT(ELF, EM_CRIS         ),
277  LLVM_READOBJ_ENUM_ENT(ELF, EM_JAVELIN      ),
278  LLVM_READOBJ_ENUM_ENT(ELF, EM_FIREPATH     ),
279  LLVM_READOBJ_ENUM_ENT(ELF, EM_ZSP          ),
280  LLVM_READOBJ_ENUM_ENT(ELF, EM_MMIX         ),
281  LLVM_READOBJ_ENUM_ENT(ELF, EM_HUANY        ),
282  LLVM_READOBJ_ENUM_ENT(ELF, EM_PRISM        ),
283  LLVM_READOBJ_ENUM_ENT(ELF, EM_AVR          ),
284  LLVM_READOBJ_ENUM_ENT(ELF, EM_FR30         ),
285  LLVM_READOBJ_ENUM_ENT(ELF, EM_D10V         ),
286  LLVM_READOBJ_ENUM_ENT(ELF, EM_D30V         ),
287  LLVM_READOBJ_ENUM_ENT(ELF, EM_V850         ),
288  LLVM_READOBJ_ENUM_ENT(ELF, EM_M32R         ),
289  LLVM_READOBJ_ENUM_ENT(ELF, EM_MN10300      ),
290  LLVM_READOBJ_ENUM_ENT(ELF, EM_MN10200      ),
291  LLVM_READOBJ_ENUM_ENT(ELF, EM_PJ           ),
292  LLVM_READOBJ_ENUM_ENT(ELF, EM_OPENRISC     ),
293  LLVM_READOBJ_ENUM_ENT(ELF, EM_ARC_COMPACT  ),
294  LLVM_READOBJ_ENUM_ENT(ELF, EM_XTENSA       ),
295  LLVM_READOBJ_ENUM_ENT(ELF, EM_VIDEOCORE    ),
296  LLVM_READOBJ_ENUM_ENT(ELF, EM_TMM_GPP      ),
297  LLVM_READOBJ_ENUM_ENT(ELF, EM_NS32K        ),
298  LLVM_READOBJ_ENUM_ENT(ELF, EM_TPC          ),
299  LLVM_READOBJ_ENUM_ENT(ELF, EM_SNP1K        ),
300  LLVM_READOBJ_ENUM_ENT(ELF, EM_ST200        ),
301  LLVM_READOBJ_ENUM_ENT(ELF, EM_IP2K         ),
302  LLVM_READOBJ_ENUM_ENT(ELF, EM_MAX          ),
303  LLVM_READOBJ_ENUM_ENT(ELF, EM_CR           ),
304  LLVM_READOBJ_ENUM_ENT(ELF, EM_F2MC16       ),
305  LLVM_READOBJ_ENUM_ENT(ELF, EM_MSP430       ),
306  LLVM_READOBJ_ENUM_ENT(ELF, EM_BLACKFIN     ),
307  LLVM_READOBJ_ENUM_ENT(ELF, EM_SE_C33       ),
308  LLVM_READOBJ_ENUM_ENT(ELF, EM_SEP          ),
309  LLVM_READOBJ_ENUM_ENT(ELF, EM_ARCA         ),
310  LLVM_READOBJ_ENUM_ENT(ELF, EM_UNICORE      ),
311  LLVM_READOBJ_ENUM_ENT(ELF, EM_EXCESS       ),
312  LLVM_READOBJ_ENUM_ENT(ELF, EM_DXP          ),
313  LLVM_READOBJ_ENUM_ENT(ELF, EM_ALTERA_NIOS2 ),
314  LLVM_READOBJ_ENUM_ENT(ELF, EM_CRX          ),
315  LLVM_READOBJ_ENUM_ENT(ELF, EM_XGATE        ),
316  LLVM_READOBJ_ENUM_ENT(ELF, EM_C166         ),
317  LLVM_READOBJ_ENUM_ENT(ELF, EM_M16C         ),
318  LLVM_READOBJ_ENUM_ENT(ELF, EM_DSPIC30F     ),
319  LLVM_READOBJ_ENUM_ENT(ELF, EM_CE           ),
320  LLVM_READOBJ_ENUM_ENT(ELF, EM_M32C         ),
321  LLVM_READOBJ_ENUM_ENT(ELF, EM_TSK3000      ),
322  LLVM_READOBJ_ENUM_ENT(ELF, EM_RS08         ),
323  LLVM_READOBJ_ENUM_ENT(ELF, EM_SHARC        ),
324  LLVM_READOBJ_ENUM_ENT(ELF, EM_ECOG2        ),
325  LLVM_READOBJ_ENUM_ENT(ELF, EM_SCORE7       ),
326  LLVM_READOBJ_ENUM_ENT(ELF, EM_DSP24        ),
327  LLVM_READOBJ_ENUM_ENT(ELF, EM_VIDEOCORE3   ),
328  LLVM_READOBJ_ENUM_ENT(ELF, EM_LATTICEMICO32),
329  LLVM_READOBJ_ENUM_ENT(ELF, EM_SE_C17       ),
330  LLVM_READOBJ_ENUM_ENT(ELF, EM_TI_C6000     ),
331  LLVM_READOBJ_ENUM_ENT(ELF, EM_TI_C2000     ),
332  LLVM_READOBJ_ENUM_ENT(ELF, EM_TI_C5500     ),
333  LLVM_READOBJ_ENUM_ENT(ELF, EM_MMDSP_PLUS   ),
334  LLVM_READOBJ_ENUM_ENT(ELF, EM_CYPRESS_M8C  ),
335  LLVM_READOBJ_ENUM_ENT(ELF, EM_R32C         ),
336  LLVM_READOBJ_ENUM_ENT(ELF, EM_TRIMEDIA     ),
337  LLVM_READOBJ_ENUM_ENT(ELF, EM_HEXAGON      ),
338  LLVM_READOBJ_ENUM_ENT(ELF, EM_8051         ),
339  LLVM_READOBJ_ENUM_ENT(ELF, EM_STXP7X       ),
340  LLVM_READOBJ_ENUM_ENT(ELF, EM_NDS32        ),
341  LLVM_READOBJ_ENUM_ENT(ELF, EM_ECOG1        ),
342  LLVM_READOBJ_ENUM_ENT(ELF, EM_ECOG1X       ),
343  LLVM_READOBJ_ENUM_ENT(ELF, EM_MAXQ30       ),
344  LLVM_READOBJ_ENUM_ENT(ELF, EM_XIMO16       ),
345  LLVM_READOBJ_ENUM_ENT(ELF, EM_MANIK        ),
346  LLVM_READOBJ_ENUM_ENT(ELF, EM_CRAYNV2      ),
347  LLVM_READOBJ_ENUM_ENT(ELF, EM_RX           ),
348  LLVM_READOBJ_ENUM_ENT(ELF, EM_METAG        ),
349  LLVM_READOBJ_ENUM_ENT(ELF, EM_MCST_ELBRUS  ),
350  LLVM_READOBJ_ENUM_ENT(ELF, EM_ECOG16       ),
351  LLVM_READOBJ_ENUM_ENT(ELF, EM_CR16         ),
352  LLVM_READOBJ_ENUM_ENT(ELF, EM_ETPU         ),
353  LLVM_READOBJ_ENUM_ENT(ELF, EM_SLE9X        ),
354  LLVM_READOBJ_ENUM_ENT(ELF, EM_L10M         ),
355  LLVM_READOBJ_ENUM_ENT(ELF, EM_K10M         ),
356  LLVM_READOBJ_ENUM_ENT(ELF, EM_AARCH64      ),
357  LLVM_READOBJ_ENUM_ENT(ELF, EM_AVR32        ),
358  LLVM_READOBJ_ENUM_ENT(ELF, EM_STM8         ),
359  LLVM_READOBJ_ENUM_ENT(ELF, EM_TILE64       ),
360  LLVM_READOBJ_ENUM_ENT(ELF, EM_TILEPRO      ),
361  LLVM_READOBJ_ENUM_ENT(ELF, EM_CUDA         ),
362  LLVM_READOBJ_ENUM_ENT(ELF, EM_TILEGX       ),
363  LLVM_READOBJ_ENUM_ENT(ELF, EM_CLOUDSHIELD  ),
364  LLVM_READOBJ_ENUM_ENT(ELF, EM_COREA_1ST    ),
365  LLVM_READOBJ_ENUM_ENT(ELF, EM_COREA_2ND    ),
366  LLVM_READOBJ_ENUM_ENT(ELF, EM_ARC_COMPACT2 ),
367  LLVM_READOBJ_ENUM_ENT(ELF, EM_OPEN8        ),
368  LLVM_READOBJ_ENUM_ENT(ELF, EM_RL78         ),
369  LLVM_READOBJ_ENUM_ENT(ELF, EM_VIDEOCORE5   ),
370  LLVM_READOBJ_ENUM_ENT(ELF, EM_78KOR        ),
371  LLVM_READOBJ_ENUM_ENT(ELF, EM_56800EX      )
372};
373
374static const EnumEntry<unsigned> ElfSymbolBindings[] = {
375  { "Local",  ELF::STB_LOCAL  },
376  { "Global", ELF::STB_GLOBAL },
377  { "Weak",   ELF::STB_WEAK   }
378};
379
380static const EnumEntry<unsigned> ElfSymbolTypes[] = {
381  { "None",      ELF::STT_NOTYPE    },
382  { "Object",    ELF::STT_OBJECT    },
383  { "Function",  ELF::STT_FUNC      },
384  { "Section",   ELF::STT_SECTION   },
385  { "File",      ELF::STT_FILE      },
386  { "Common",    ELF::STT_COMMON    },
387  { "TLS",       ELF::STT_TLS       },
388  { "GNU_IFunc", ELF::STT_GNU_IFUNC }
389};
390
391static const char *getElfSectionType(unsigned Arch, unsigned Type) {
392  switch (Arch) {
393  case ELF::EM_ARM:
394    switch (Type) {
395    LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_EXIDX);
396    LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
397    LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
398    LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
399    LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
400    }
401  case ELF::EM_HEXAGON:
402    switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
403  case ELF::EM_X86_64:
404    switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
405  case ELF::EM_MIPS:
406  case ELF::EM_MIPS_RS3_LE:
407    switch (Type) {
408    LLVM_READOBJ_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
409    LLVM_READOBJ_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
410    }
411  }
412
413  switch (Type) {
414  LLVM_READOBJ_ENUM_CASE(ELF, SHT_NULL              );
415  LLVM_READOBJ_ENUM_CASE(ELF, SHT_PROGBITS          );
416  LLVM_READOBJ_ENUM_CASE(ELF, SHT_SYMTAB            );
417  LLVM_READOBJ_ENUM_CASE(ELF, SHT_STRTAB            );
418  LLVM_READOBJ_ENUM_CASE(ELF, SHT_RELA              );
419  LLVM_READOBJ_ENUM_CASE(ELF, SHT_HASH              );
420  LLVM_READOBJ_ENUM_CASE(ELF, SHT_DYNAMIC           );
421  LLVM_READOBJ_ENUM_CASE(ELF, SHT_NOTE              );
422  LLVM_READOBJ_ENUM_CASE(ELF, SHT_NOBITS            );
423  LLVM_READOBJ_ENUM_CASE(ELF, SHT_REL               );
424  LLVM_READOBJ_ENUM_CASE(ELF, SHT_SHLIB             );
425  LLVM_READOBJ_ENUM_CASE(ELF, SHT_DYNSYM            );
426  LLVM_READOBJ_ENUM_CASE(ELF, SHT_INIT_ARRAY        );
427  LLVM_READOBJ_ENUM_CASE(ELF, SHT_FINI_ARRAY        );
428  LLVM_READOBJ_ENUM_CASE(ELF, SHT_PREINIT_ARRAY     );
429  LLVM_READOBJ_ENUM_CASE(ELF, SHT_GROUP             );
430  LLVM_READOBJ_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX      );
431  LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES    );
432  LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_HASH          );
433  LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_verdef        );
434  LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_verneed       );
435  LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_versym        );
436  default: return "";
437  }
438}
439
440static const EnumEntry<unsigned> ElfSectionFlags[] = {
441  LLVM_READOBJ_ENUM_ENT(ELF, SHF_WRITE           ),
442  LLVM_READOBJ_ENUM_ENT(ELF, SHF_ALLOC           ),
443  LLVM_READOBJ_ENUM_ENT(ELF, SHF_EXCLUDE         ),
444  LLVM_READOBJ_ENUM_ENT(ELF, SHF_EXECINSTR       ),
445  LLVM_READOBJ_ENUM_ENT(ELF, SHF_MERGE           ),
446  LLVM_READOBJ_ENUM_ENT(ELF, SHF_STRINGS         ),
447  LLVM_READOBJ_ENUM_ENT(ELF, SHF_INFO_LINK       ),
448  LLVM_READOBJ_ENUM_ENT(ELF, SHF_LINK_ORDER      ),
449  LLVM_READOBJ_ENUM_ENT(ELF, SHF_OS_NONCONFORMING),
450  LLVM_READOBJ_ENUM_ENT(ELF, SHF_GROUP           ),
451  LLVM_READOBJ_ENUM_ENT(ELF, SHF_TLS             ),
452  LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_CP_SECTION),
453  LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_DP_SECTION),
454  LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NOSTRIP    )
455};
456
457static const char *getElfSegmentType(unsigned Arch, unsigned Type) {
458  // Check potentially overlapped processor-specific
459  // program header type.
460  switch (Arch) {
461  case ELF::EM_ARM:
462    switch (Type) {
463    LLVM_READOBJ_ENUM_CASE(ELF, PT_ARM_EXIDX);
464    }
465  case ELF::EM_MIPS:
466  case ELF::EM_MIPS_RS3_LE:
467    switch (Type) {
468    LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_REGINFO);
469    LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_RTPROC);
470    LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_OPTIONS);
471    }
472  }
473
474  switch (Type) {
475  LLVM_READOBJ_ENUM_CASE(ELF, PT_NULL   );
476  LLVM_READOBJ_ENUM_CASE(ELF, PT_LOAD   );
477  LLVM_READOBJ_ENUM_CASE(ELF, PT_DYNAMIC);
478  LLVM_READOBJ_ENUM_CASE(ELF, PT_INTERP );
479  LLVM_READOBJ_ENUM_CASE(ELF, PT_NOTE   );
480  LLVM_READOBJ_ENUM_CASE(ELF, PT_SHLIB  );
481  LLVM_READOBJ_ENUM_CASE(ELF, PT_PHDR   );
482  LLVM_READOBJ_ENUM_CASE(ELF, PT_TLS    );
483
484  LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_EH_FRAME);
485  LLVM_READOBJ_ENUM_CASE(ELF, PT_SUNW_UNWIND);
486
487  LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_STACK);
488  LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_RELRO);
489  default: return "";
490  }
491}
492
493static const EnumEntry<unsigned> ElfSegmentFlags[] = {
494  LLVM_READOBJ_ENUM_ENT(ELF, PF_X),
495  LLVM_READOBJ_ENUM_ENT(ELF, PF_W),
496  LLVM_READOBJ_ENUM_ENT(ELF, PF_R)
497};
498
499static const EnumEntry<unsigned> ElfHeaderMipsFlags[] = {
500  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_NOREORDER),
501  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_PIC),
502  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_CPIC),
503  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI2),
504  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_32BITMODE),
505  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_NAN2008),
506  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI_O32),
507  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MICROMIPS),
508  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_ASE_M16),
509  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_1),
510  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_2),
511  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_3),
512  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_4),
513  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_5),
514  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_32),
515  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_64),
516  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_32R2),
517  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_64R2),
518  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_32R6),
519  LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_64R6)
520};
521
522template<class ELFT>
523void ELFDumper<ELFT>::printFileHeaders() {
524  const typename ELFO::Elf_Ehdr *Header = Obj->getHeader();
525
526  {
527    DictScope D(W, "ElfHeader");
528    {
529      DictScope D(W, "Ident");
530      W.printBinary("Magic", makeArrayRef(Header->e_ident).slice(ELF::EI_MAG0,
531                                                                 4));
532      W.printEnum  ("Class", Header->e_ident[ELF::EI_CLASS],
533                      makeArrayRef(ElfClass));
534      W.printEnum  ("DataEncoding", Header->e_ident[ELF::EI_DATA],
535                      makeArrayRef(ElfDataEncoding));
536      W.printNumber("FileVersion", Header->e_ident[ELF::EI_VERSION]);
537      W.printEnum  ("OS/ABI", Header->e_ident[ELF::EI_OSABI],
538                      makeArrayRef(ElfOSABI));
539      W.printNumber("ABIVersion", Header->e_ident[ELF::EI_ABIVERSION]);
540      W.printBinary("Unused", makeArrayRef(Header->e_ident).slice(ELF::EI_PAD));
541    }
542
543    W.printEnum  ("Type", Header->e_type, makeArrayRef(ElfObjectFileType));
544    W.printEnum  ("Machine", Header->e_machine, makeArrayRef(ElfMachineType));
545    W.printNumber("Version", Header->e_version);
546    W.printHex   ("Entry", Header->e_entry);
547    W.printHex   ("ProgramHeaderOffset", Header->e_phoff);
548    W.printHex   ("SectionHeaderOffset", Header->e_shoff);
549    if (Header->e_machine == EM_MIPS)
550      W.printFlags("Flags", Header->e_flags, makeArrayRef(ElfHeaderMipsFlags),
551                   unsigned(ELF::EF_MIPS_ARCH));
552    else
553      W.printFlags("Flags", Header->e_flags);
554    W.printNumber("HeaderSize", Header->e_ehsize);
555    W.printNumber("ProgramHeaderEntrySize", Header->e_phentsize);
556    W.printNumber("ProgramHeaderCount", Header->e_phnum);
557    W.printNumber("SectionHeaderEntrySize", Header->e_shentsize);
558    W.printNumber("SectionHeaderCount", Header->e_shnum);
559    W.printNumber("StringTableSectionIndex", Header->e_shstrndx);
560  }
561}
562
563template<class ELFT>
564void ELFDumper<ELFT>::printSections() {
565  ListScope SectionsD(W, "Sections");
566
567  int SectionIndex = -1;
568  for (typename ELFO::Elf_Shdr_Iter SecI = Obj->begin_sections(),
569                                    SecE = Obj->end_sections();
570       SecI != SecE; ++SecI) {
571    ++SectionIndex;
572
573    const Elf_Shdr *Section = &*SecI;
574    StringRef Name = errorOrDefault(Obj->getSectionName(Section));
575
576    DictScope SectionD(W, "Section");
577    W.printNumber("Index", SectionIndex);
578    W.printNumber("Name", Name, Section->sh_name);
579    W.printHex("Type",
580               getElfSectionType(Obj->getHeader()->e_machine, Section->sh_type),
581               Section->sh_type);
582    W.printFlags ("Flags", Section->sh_flags, makeArrayRef(ElfSectionFlags));
583    W.printHex   ("Address", Section->sh_addr);
584    W.printHex   ("Offset", Section->sh_offset);
585    W.printNumber("Size", Section->sh_size);
586    W.printNumber("Link", Section->sh_link);
587    W.printNumber("Info", Section->sh_info);
588    W.printNumber("AddressAlignment", Section->sh_addralign);
589    W.printNumber("EntrySize", Section->sh_entsize);
590
591    if (opts::SectionRelocations) {
592      ListScope D(W, "Relocations");
593      printRelocations(Section);
594    }
595
596    if (opts::SectionSymbols) {
597      ListScope D(W, "Symbols");
598      for (typename ELFO::Elf_Sym_Iter SymI = Obj->begin_symbols(),
599                                       SymE = Obj->end_symbols();
600           SymI != SymE; ++SymI) {
601        if (Obj->getSection(&*SymI) == Section)
602          printSymbol(SymI);
603      }
604    }
605
606    if (opts::SectionData) {
607      ArrayRef<uint8_t> Data = errorOrDefault(Obj->getSectionContents(Section));
608      W.printBinaryBlock("SectionData",
609                         StringRef((const char *)Data.data(), Data.size()));
610    }
611  }
612}
613
614template<class ELFT>
615void ELFDumper<ELFT>::printRelocations() {
616  ListScope D(W, "Relocations");
617
618  int SectionNumber = -1;
619  for (typename ELFO::Elf_Shdr_Iter SecI = Obj->begin_sections(),
620                                    SecE = Obj->end_sections();
621       SecI != SecE; ++SecI) {
622    ++SectionNumber;
623
624    if (SecI->sh_type != ELF::SHT_REL && SecI->sh_type != ELF::SHT_RELA)
625      continue;
626
627    StringRef Name = errorOrDefault(Obj->getSectionName(&*SecI));
628
629    W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
630    W.indent();
631
632    printRelocations(&*SecI);
633
634    W.unindent();
635    W.startLine() << "}\n";
636  }
637}
638
639template <class ELFT>
640void ELFDumper<ELFT>::printRelocations(const Elf_Shdr *Sec) {
641  switch (Sec->sh_type) {
642  case ELF::SHT_REL:
643    for (typename ELFO::Elf_Rel_Iter RI = Obj->begin_rel(Sec),
644                                     RE = Obj->end_rel(Sec);
645         RI != RE; ++RI) {
646      typename ELFO::Elf_Rela Rela;
647      Rela.r_offset = RI->r_offset;
648      Rela.r_info = RI->r_info;
649      Rela.r_addend = 0;
650      printRelocation(Sec, Rela);
651    }
652    break;
653  case ELF::SHT_RELA:
654    for (typename ELFO::Elf_Rela_Iter RI = Obj->begin_rela(Sec),
655                                      RE = Obj->end_rela(Sec);
656         RI != RE; ++RI) {
657      printRelocation(Sec, *RI);
658    }
659    break;
660  }
661}
662
663template <class ELFT>
664void ELFDumper<ELFT>::printRelocation(const Elf_Shdr *Sec,
665                                      typename ELFO::Elf_Rela Rel) {
666  SmallString<32> RelocName;
667  Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
668  StringRef SymbolName;
669  std::pair<const Elf_Shdr *, const Elf_Sym *> Sym =
670      Obj->getRelocationSymbol(Sec, &Rel);
671  if (Sym.first)
672    SymbolName = errorOrDefault(Obj->getSymbolName(Sym.first, Sym.second));
673
674  if (opts::ExpandRelocs) {
675    DictScope Group(W, "Relocation");
676    W.printHex("Offset", Rel.r_offset);
677    W.printNumber("Type", RelocName, (int)Rel.getType(Obj->isMips64EL()));
678    W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
679    W.printHex("Addend", Rel.r_addend);
680  } else {
681    raw_ostream& OS = W.startLine();
682    OS << W.hex(Rel.r_offset)
683       << " " << RelocName
684       << " " << (SymbolName.size() > 0 ? SymbolName : "-")
685       << " " << W.hex(Rel.r_addend)
686       << "\n";
687  }
688}
689
690template<class ELFT>
691void ELFDumper<ELFT>::printSymbols() {
692  ListScope Group(W, "Symbols");
693  for (typename ELFO::Elf_Sym_Iter SymI = Obj->begin_symbols(),
694                                   SymE = Obj->end_symbols();
695       SymI != SymE; ++SymI) {
696    printSymbol(SymI);
697  }
698}
699
700template<class ELFT>
701void ELFDumper<ELFT>::printDynamicSymbols() {
702  ListScope Group(W, "DynamicSymbols");
703
704  for (typename ELFO::Elf_Sym_Iter SymI = Obj->begin_dynamic_symbols(),
705                                   SymE = Obj->end_dynamic_symbols();
706       SymI != SymE; ++SymI) {
707    printSymbol(SymI);
708  }
709}
710
711template <class ELFT>
712void ELFDumper<ELFT>::printSymbol(typename ELFO::Elf_Sym_Iter Symbol) {
713  unsigned SectionIndex = 0;
714  StringRef SectionName;
715  getSectionNameIndex(*Obj, Symbol, SectionName, SectionIndex);
716  std::string FullSymbolName = getFullSymbolName(*Obj, Symbol);
717
718  DictScope D(W, "Symbol");
719  W.printNumber("Name", FullSymbolName, Symbol->st_name);
720  W.printHex   ("Value", Symbol->st_value);
721  W.printNumber("Size", Symbol->st_size);
722  W.printEnum  ("Binding", Symbol->getBinding(),
723                  makeArrayRef(ElfSymbolBindings));
724  W.printEnum  ("Type", Symbol->getType(), makeArrayRef(ElfSymbolTypes));
725  W.printNumber("Other", Symbol->st_other);
726  W.printHex("Section", SectionName, SectionIndex);
727}
728
729#define LLVM_READOBJ_TYPE_CASE(name) \
730  case DT_##name: return #name
731
732static const char *getTypeString(uint64_t Type) {
733  switch (Type) {
734  LLVM_READOBJ_TYPE_CASE(BIND_NOW);
735  LLVM_READOBJ_TYPE_CASE(DEBUG);
736  LLVM_READOBJ_TYPE_CASE(FINI);
737  LLVM_READOBJ_TYPE_CASE(FINI_ARRAY);
738  LLVM_READOBJ_TYPE_CASE(FINI_ARRAYSZ);
739  LLVM_READOBJ_TYPE_CASE(FLAGS);
740  LLVM_READOBJ_TYPE_CASE(HASH);
741  LLVM_READOBJ_TYPE_CASE(INIT);
742  LLVM_READOBJ_TYPE_CASE(INIT_ARRAY);
743  LLVM_READOBJ_TYPE_CASE(INIT_ARRAYSZ);
744  LLVM_READOBJ_TYPE_CASE(PREINIT_ARRAY);
745  LLVM_READOBJ_TYPE_CASE(PREINIT_ARRAYSZ);
746  LLVM_READOBJ_TYPE_CASE(JMPREL);
747  LLVM_READOBJ_TYPE_CASE(NEEDED);
748  LLVM_READOBJ_TYPE_CASE(NULL);
749  LLVM_READOBJ_TYPE_CASE(PLTGOT);
750  LLVM_READOBJ_TYPE_CASE(PLTREL);
751  LLVM_READOBJ_TYPE_CASE(PLTRELSZ);
752  LLVM_READOBJ_TYPE_CASE(REL);
753  LLVM_READOBJ_TYPE_CASE(RELA);
754  LLVM_READOBJ_TYPE_CASE(RELENT);
755  LLVM_READOBJ_TYPE_CASE(RELSZ);
756  LLVM_READOBJ_TYPE_CASE(RELAENT);
757  LLVM_READOBJ_TYPE_CASE(RELASZ);
758  LLVM_READOBJ_TYPE_CASE(RPATH);
759  LLVM_READOBJ_TYPE_CASE(RUNPATH);
760  LLVM_READOBJ_TYPE_CASE(SONAME);
761  LLVM_READOBJ_TYPE_CASE(STRSZ);
762  LLVM_READOBJ_TYPE_CASE(STRTAB);
763  LLVM_READOBJ_TYPE_CASE(SYMBOLIC);
764  LLVM_READOBJ_TYPE_CASE(SYMENT);
765  LLVM_READOBJ_TYPE_CASE(SYMTAB);
766  LLVM_READOBJ_TYPE_CASE(TEXTREL);
767  LLVM_READOBJ_TYPE_CASE(VERNEED);
768  LLVM_READOBJ_TYPE_CASE(VERNEEDNUM);
769  LLVM_READOBJ_TYPE_CASE(VERSYM);
770  LLVM_READOBJ_TYPE_CASE(RELCOUNT);
771  LLVM_READOBJ_TYPE_CASE(GNU_HASH);
772  LLVM_READOBJ_TYPE_CASE(MIPS_RLD_VERSION);
773  LLVM_READOBJ_TYPE_CASE(MIPS_FLAGS);
774  LLVM_READOBJ_TYPE_CASE(MIPS_BASE_ADDRESS);
775  LLVM_READOBJ_TYPE_CASE(MIPS_LOCAL_GOTNO);
776  LLVM_READOBJ_TYPE_CASE(MIPS_SYMTABNO);
777  LLVM_READOBJ_TYPE_CASE(MIPS_UNREFEXTNO);
778  LLVM_READOBJ_TYPE_CASE(MIPS_GOTSYM);
779  LLVM_READOBJ_TYPE_CASE(MIPS_RLD_MAP);
780  LLVM_READOBJ_TYPE_CASE(MIPS_PLTGOT);
781  default: return "unknown";
782  }
783}
784
785#undef LLVM_READOBJ_TYPE_CASE
786
787#define LLVM_READOBJ_DT_FLAG_ENT(prefix, enum) \
788  { #enum, prefix##_##enum }
789
790static const EnumEntry<unsigned> ElfDynamicDTFlags[] = {
791  LLVM_READOBJ_DT_FLAG_ENT(DF, ORIGIN),
792  LLVM_READOBJ_DT_FLAG_ENT(DF, SYMBOLIC),
793  LLVM_READOBJ_DT_FLAG_ENT(DF, TEXTREL),
794  LLVM_READOBJ_DT_FLAG_ENT(DF, BIND_NOW),
795  LLVM_READOBJ_DT_FLAG_ENT(DF, STATIC_TLS)
796};
797
798static const EnumEntry<unsigned> ElfDynamicDTMipsFlags[] = {
799  LLVM_READOBJ_DT_FLAG_ENT(RHF, NONE),
800  LLVM_READOBJ_DT_FLAG_ENT(RHF, QUICKSTART),
801  LLVM_READOBJ_DT_FLAG_ENT(RHF, NOTPOT),
802  LLVM_READOBJ_DT_FLAG_ENT(RHS, NO_LIBRARY_REPLACEMENT),
803  LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_MOVE),
804  LLVM_READOBJ_DT_FLAG_ENT(RHF, SGI_ONLY),
805  LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_INIT),
806  LLVM_READOBJ_DT_FLAG_ENT(RHF, DELTA_C_PLUS_PLUS),
807  LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_START_INIT),
808  LLVM_READOBJ_DT_FLAG_ENT(RHF, PIXIE),
809  LLVM_READOBJ_DT_FLAG_ENT(RHF, DEFAULT_DELAY_LOAD),
810  LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTART),
811  LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTARTED),
812  LLVM_READOBJ_DT_FLAG_ENT(RHF, CORD),
813  LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_UNRES_UNDEF),
814  LLVM_READOBJ_DT_FLAG_ENT(RHF, RLD_ORDER_SAFE)
815};
816
817#undef LLVM_READOBJ_DT_FLAG_ENT
818
819template <typename T, typename TFlag>
820void printFlags(T Value, ArrayRef<EnumEntry<TFlag>> Flags, raw_ostream &OS) {
821  typedef EnumEntry<TFlag> FlagEntry;
822  typedef SmallVector<FlagEntry, 10> FlagVector;
823  FlagVector SetFlags;
824
825  for (const auto &Flag : Flags) {
826    if (Flag.Value == 0)
827      continue;
828
829    if ((Value & Flag.Value) == Flag.Value)
830      SetFlags.push_back(Flag);
831  }
832
833  for (const auto &Flag : SetFlags) {
834    OS << Flag.Name << " ";
835  }
836}
837
838template <class ELFT>
839static void printValue(const ELFFile<ELFT> *O, uint64_t Type, uint64_t Value,
840                       bool Is64, raw_ostream &OS) {
841  switch (Type) {
842  case DT_PLTREL:
843    if (Value == DT_REL) {
844      OS << "REL";
845      break;
846    } else if (Value == DT_RELA) {
847      OS << "RELA";
848      break;
849    }
850  // Fallthrough.
851  case DT_PLTGOT:
852  case DT_HASH:
853  case DT_STRTAB:
854  case DT_SYMTAB:
855  case DT_RELA:
856  case DT_INIT:
857  case DT_FINI:
858  case DT_REL:
859  case DT_JMPREL:
860  case DT_INIT_ARRAY:
861  case DT_FINI_ARRAY:
862  case DT_PREINIT_ARRAY:
863  case DT_DEBUG:
864  case DT_VERNEED:
865  case DT_VERSYM:
866  case DT_GNU_HASH:
867  case DT_NULL:
868  case DT_MIPS_BASE_ADDRESS:
869  case DT_MIPS_GOTSYM:
870  case DT_MIPS_RLD_MAP:
871  case DT_MIPS_PLTGOT:
872    OS << format("0x%" PRIX64, Value);
873    break;
874  case DT_RELCOUNT:
875  case DT_VERNEEDNUM:
876  case DT_MIPS_RLD_VERSION:
877  case DT_MIPS_LOCAL_GOTNO:
878  case DT_MIPS_SYMTABNO:
879  case DT_MIPS_UNREFEXTNO:
880    OS << Value;
881    break;
882  case DT_PLTRELSZ:
883  case DT_RELASZ:
884  case DT_RELAENT:
885  case DT_STRSZ:
886  case DT_SYMENT:
887  case DT_RELSZ:
888  case DT_RELENT:
889  case DT_INIT_ARRAYSZ:
890  case DT_FINI_ARRAYSZ:
891  case DT_PREINIT_ARRAYSZ:
892    OS << Value << " (bytes)";
893    break;
894  case DT_NEEDED:
895    OS << "SharedLibrary (" << O->getDynamicString(Value) << ")";
896    break;
897  case DT_SONAME:
898    OS << "LibrarySoname (" << O->getDynamicString(Value) << ")";
899    break;
900  case DT_RPATH:
901  case DT_RUNPATH:
902    OS << O->getDynamicString(Value);
903    break;
904  case DT_MIPS_FLAGS:
905    printFlags(Value, makeArrayRef(ElfDynamicDTMipsFlags), OS);
906    break;
907  case DT_FLAGS:
908    printFlags(Value, makeArrayRef(ElfDynamicDTFlags), OS);
909    break;
910  }
911}
912
913template<class ELFT>
914void ELFDumper<ELFT>::printUnwindInfo() {
915  W.startLine() << "UnwindInfo not implemented.\n";
916}
917
918namespace {
919template <>
920void ELFDumper<ELFType<support::little, 2, false> >::printUnwindInfo() {
921  const unsigned Machine = Obj->getHeader()->e_machine;
922  if (Machine == EM_ARM) {
923    ARM::EHABI::PrinterContext<ELFType<support::little, 2, false> > Ctx(W, Obj);
924    return Ctx.PrintUnwindInformation();
925  }
926  W.startLine() << "UnwindInfo not implemented.\n";
927}
928}
929
930template<class ELFT>
931void ELFDumper<ELFT>::printDynamicTable() {
932  auto DynTable = Obj->dynamic_table(true);
933
934  ptrdiff_t Total = std::distance(DynTable.begin(), DynTable.end());
935  if (Total == 0)
936    return;
937
938  raw_ostream &OS = W.getOStream();
939  W.startLine() << "DynamicSection [ (" << Total << " entries)\n";
940
941  bool Is64 = ELFT::Is64Bits;
942
943  W.startLine()
944     << "  Tag" << (Is64 ? "                " : "        ") << "Type"
945     << "                 " << "Name/Value\n";
946  for (const auto &Entry : DynTable) {
947    W.startLine()
948       << "  "
949       << format(Is64 ? "0x%016" PRIX64 : "0x%08" PRIX64, Entry.getTag())
950       << " " << format("%-21s", getTypeString(Entry.getTag()));
951    printValue(Obj, Entry.getTag(), Entry.getVal(), Is64, OS);
952    OS << "\n";
953  }
954
955  W.startLine() << "]\n";
956}
957
958template<class ELFT>
959void ELFDumper<ELFT>::printNeededLibraries() {
960  ListScope D(W, "NeededLibraries");
961
962  typedef std::vector<StringRef> LibsTy;
963  LibsTy Libs;
964
965  for (const auto &Entry : Obj->dynamic_table())
966    if (Entry.d_tag == ELF::DT_NEEDED)
967      Libs.push_back(Obj->getDynamicString(Entry.d_un.d_val));
968
969  std::stable_sort(Libs.begin(), Libs.end());
970
971  for (LibsTy::const_iterator I = Libs.begin(), E = Libs.end(); I != E; ++I) {
972    outs() << "  " << *I << "\n";
973  }
974}
975
976template<class ELFT>
977void ELFDumper<ELFT>::printProgramHeaders() {
978  ListScope L(W, "ProgramHeaders");
979
980  for (typename ELFO::Elf_Phdr_Iter PI = Obj->begin_program_headers(),
981                                    PE = Obj->end_program_headers();
982                                    PI != PE; ++PI) {
983    DictScope P(W, "ProgramHeader");
984    W.printHex   ("Type",
985                  getElfSegmentType(Obj->getHeader()->e_machine, PI->p_type),
986                  PI->p_type);
987    W.printHex   ("Offset", PI->p_offset);
988    W.printHex   ("VirtualAddress", PI->p_vaddr);
989    W.printHex   ("PhysicalAddress", PI->p_paddr);
990    W.printNumber("FileSize", PI->p_filesz);
991    W.printNumber("MemSize", PI->p_memsz);
992    W.printFlags ("Flags", PI->p_flags, makeArrayRef(ElfSegmentFlags));
993    W.printNumber("Alignment", PI->p_align);
994  }
995}
996
997template <class ELFT>
998void ELFDumper<ELFT>::printAttributes() {
999  W.startLine() << "Attributes not implemented.\n";
1000}
1001
1002namespace {
1003template <>
1004void ELFDumper<ELFType<support::little, 2, false> >::printAttributes() {
1005  if (Obj->getHeader()->e_machine != EM_ARM) {
1006    W.startLine() << "Attributes not implemented.\n";
1007    return;
1008  }
1009
1010  DictScope BA(W, "BuildAttributes");
1011  for (ELFO::Elf_Shdr_Iter SI = Obj->begin_sections(), SE = Obj->end_sections();
1012       SI != SE; ++SI) {
1013    if (SI->sh_type != ELF::SHT_ARM_ATTRIBUTES)
1014      continue;
1015
1016    ErrorOr<ArrayRef<uint8_t> > Contents = Obj->getSectionContents(&(*SI));
1017    if (!Contents)
1018      continue;
1019
1020    if ((*Contents)[0] != ARMBuildAttrs::Format_Version) {
1021      errs() << "unrecognised FormatVersion: 0x" << utohexstr((*Contents)[0])
1022             << '\n';
1023      continue;
1024    }
1025
1026    W.printHex("FormatVersion", (*Contents)[0]);
1027    if (Contents->size() == 1)
1028      continue;
1029
1030    ARMAttributeParser(W).Parse(*Contents);
1031  }
1032}
1033}
1034
1035namespace {
1036template <class ELFT> class MipsGOTParser {
1037public:
1038  typedef object::ELFFile<ELFT> ObjectFile;
1039  typedef typename ObjectFile::Elf_Shdr Elf_Shdr;
1040
1041  MipsGOTParser(const ObjectFile *Obj, StreamWriter &W) : Obj(Obj), W(W) {}
1042
1043  void parseGOT(const Elf_Shdr &GOTShdr);
1044
1045private:
1046  typedef typename ObjectFile::Elf_Sym_Iter Elf_Sym_Iter;
1047  typedef typename ObjectFile::Elf_Addr GOTEntry;
1048  typedef typename ObjectFile::template ELFEntityIterator<const GOTEntry>
1049  GOTIter;
1050
1051  const ObjectFile *Obj;
1052  StreamWriter &W;
1053
1054  std::size_t getGOTTotal(ArrayRef<uint8_t> GOT) const;
1055  GOTIter makeGOTIter(ArrayRef<uint8_t> GOT, std::size_t EntryNum);
1056
1057  bool getGOTTags(uint64_t &LocalGotNum, uint64_t &GotSym);
1058  void printGotEntry(uint64_t GotAddr, GOTIter BeginIt, GOTIter It);
1059  void printGlobalGotEntry(uint64_t GotAddr, GOTIter BeginIt, GOTIter It,
1060                           Elf_Sym_Iter Sym);
1061};
1062}
1063
1064template <class ELFT>
1065void MipsGOTParser<ELFT>::parseGOT(const Elf_Shdr &GOTShdr) {
1066  // See "Global Offset Table" in Chapter 5 in the following document
1067  // for detailed GOT description.
1068  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1069
1070  ErrorOr<ArrayRef<uint8_t>> GOT = Obj->getSectionContents(&GOTShdr);
1071  if (!GOT) {
1072    W.startLine() << "The .got section is empty.\n";
1073    return;
1074  }
1075
1076  uint64_t DtLocalGotNum;
1077  uint64_t DtGotSym;
1078  if (!getGOTTags(DtLocalGotNum, DtGotSym))
1079    return;
1080
1081  if (DtLocalGotNum > getGOTTotal(*GOT)) {
1082    W.startLine() << "MIPS_LOCAL_GOTNO exceeds a number of GOT entries.\n";
1083    return;
1084  }
1085
1086  Elf_Sym_Iter DynSymBegin = Obj->begin_dynamic_symbols();
1087  Elf_Sym_Iter DynSymEnd = Obj->end_dynamic_symbols();
1088  std::size_t DynSymTotal = std::size_t(std::distance(DynSymBegin, DynSymEnd));
1089
1090  if (DtGotSym > DynSymTotal) {
1091    W.startLine() << "MIPS_GOTSYM exceeds a number of dynamic symbols.\n";
1092    return;
1093  }
1094
1095  std::size_t GlobalGotNum = DynSymTotal - DtGotSym;
1096
1097  if (DtLocalGotNum + GlobalGotNum > getGOTTotal(*GOT)) {
1098    W.startLine() << "Number of global GOT entries exceeds the size of GOT.\n";
1099    return;
1100  }
1101
1102  GOTIter GotBegin = makeGOTIter(*GOT, 0);
1103  GOTIter GotLocalEnd = makeGOTIter(*GOT, DtLocalGotNum);
1104  GOTIter It = GotBegin;
1105
1106  DictScope GS(W, "Primary GOT");
1107
1108  W.printHex("Canonical gp value", GOTShdr.sh_addr + 0x7ff0);
1109  {
1110    ListScope RS(W, "Reserved entries");
1111
1112    {
1113      DictScope D(W, "Entry");
1114      printGotEntry(GOTShdr.sh_addr, GotBegin, It++);
1115      W.printString("Purpose", StringRef("Lazy resolver"));
1116    }
1117
1118    if (It != GotLocalEnd && (*It >> (sizeof(GOTEntry) * 8 - 1)) != 0) {
1119      DictScope D(W, "Entry");
1120      printGotEntry(GOTShdr.sh_addr, GotBegin, It++);
1121      W.printString("Purpose", StringRef("Module pointer (GNU extension)"));
1122    }
1123  }
1124  {
1125    ListScope LS(W, "Local entries");
1126    for (; It != GotLocalEnd; ++It) {
1127      DictScope D(W, "Entry");
1128      printGotEntry(GOTShdr.sh_addr, GotBegin, It);
1129    }
1130  }
1131  {
1132    ListScope GS(W, "Global entries");
1133
1134    GOTIter GotGlobalEnd = makeGOTIter(*GOT, DtLocalGotNum + GlobalGotNum);
1135    Elf_Sym_Iter GotDynSym = DynSymBegin + DtGotSym;
1136    for (; It != GotGlobalEnd; ++It) {
1137      DictScope D(W, "Entry");
1138      printGlobalGotEntry(GOTShdr.sh_addr, GotBegin, It, GotDynSym++);
1139    }
1140  }
1141
1142  std::size_t SpecGotNum = getGOTTotal(*GOT) - DtLocalGotNum - GlobalGotNum;
1143  W.printNumber("Number of TLS and multi-GOT entries", uint64_t(SpecGotNum));
1144}
1145
1146template <class ELFT>
1147std::size_t MipsGOTParser<ELFT>::getGOTTotal(ArrayRef<uint8_t> GOT) const {
1148  return GOT.size() / sizeof(GOTEntry);
1149}
1150
1151template <class ELFT>
1152typename MipsGOTParser<ELFT>::GOTIter
1153MipsGOTParser<ELFT>::makeGOTIter(ArrayRef<uint8_t> GOT, std::size_t EntryNum) {
1154  const char *Data = reinterpret_cast<const char *>(GOT.data());
1155  return GOTIter(sizeof(GOTEntry), Data + EntryNum * sizeof(GOTEntry));
1156}
1157
1158template <class ELFT>
1159bool MipsGOTParser<ELFT>::getGOTTags(uint64_t &LocalGotNum, uint64_t &GotSym) {
1160  bool FoundLocalGotNum = false;
1161  bool FoundGotSym = false;
1162  for (const auto &Entry : Obj->dynamic_table()) {
1163    switch (Entry.getTag()) {
1164    case ELF::DT_MIPS_LOCAL_GOTNO:
1165      LocalGotNum = Entry.getVal();
1166      FoundLocalGotNum = true;
1167      break;
1168    case ELF::DT_MIPS_GOTSYM:
1169      GotSym = Entry.getVal();
1170      FoundGotSym = true;
1171      break;
1172    }
1173  }
1174
1175  if (!FoundLocalGotNum) {
1176    W.startLine() << "Cannot find MIPS_LOCAL_GOTNO dynamic table tag.\n";
1177    return false;
1178  }
1179
1180  if (!FoundGotSym) {
1181    W.startLine() << "Cannot find MIPS_GOTSYM dynamic table tag.\n";
1182    return false;
1183  }
1184
1185  return true;
1186}
1187
1188template <class ELFT>
1189void MipsGOTParser<ELFT>::printGotEntry(uint64_t GotAddr, GOTIter BeginIt,
1190                                        GOTIter It) {
1191  int64_t Offset = std::distance(BeginIt, It) * sizeof(GOTEntry);
1192  W.printHex("Address", GotAddr + Offset);
1193  W.printNumber("Access", Offset - 0x7ff0);
1194  W.printHex("Initial", *It);
1195}
1196
1197template <class ELFT>
1198void MipsGOTParser<ELFT>::printGlobalGotEntry(uint64_t GotAddr, GOTIter BeginIt,
1199                                              GOTIter It, Elf_Sym_Iter Sym) {
1200  printGotEntry(GotAddr, BeginIt, It);
1201
1202  W.printHex("Value", Sym->st_value);
1203  W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes));
1204
1205  unsigned SectionIndex = 0;
1206  StringRef SectionName;
1207  getSectionNameIndex(*Obj, Sym, SectionName, SectionIndex);
1208  W.printHex("Section", SectionName, SectionIndex);
1209
1210  std::string FullSymbolName = getFullSymbolName(*Obj, Sym);
1211  W.printNumber("Name", FullSymbolName, Sym->st_name);
1212}
1213
1214template <class ELFT> void ELFDumper<ELFT>::printMipsPLTGOT() {
1215  if (Obj->getHeader()->e_machine != EM_MIPS) {
1216    W.startLine() << "MIPS PLT GOT is available for MIPS targets only.\n";
1217    return;
1218  }
1219
1220  llvm::Optional<uint64_t> DtPltGot;
1221  for (const auto &Entry : Obj->dynamic_table()) {
1222    if (Entry.getTag() == ELF::DT_PLTGOT) {
1223      DtPltGot = Entry.getVal();
1224      break;
1225    }
1226  }
1227
1228  if (!DtPltGot) {
1229    W.startLine() << "Cannot find PLTGOT dynamic table tag.\n";
1230    return;
1231  }
1232
1233  const Elf_Shdr *GotShdr = findSectionByAddress(Obj, *DtPltGot);
1234  if (!GotShdr) {
1235    W.startLine() << "There is no .got section in the file.\n";
1236    return;
1237  }
1238
1239  MipsGOTParser<ELFT>(Obj, W).parseGOT(*GotShdr);
1240}
1241