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