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