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