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