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