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