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