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