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