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