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