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