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