blob: 0f33ecd58cd82486cababeecfa77238af55ca041 [file] [log] [blame]
Eugene Zelenko416e0592017-06-09 21:41:54 +00001//===- ELFDumper.cpp - ELF-specific dumper --------------------------------===//
George Rimar47936762016-01-16 00:49:19 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Rimar47936762016-01-16 00:49:19 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file implements the ELF-specific dumper for llvm-readobj.
George Rimar47936762016-01-16 00:49:19 +000011///
12//===----------------------------------------------------------------------===//
13
George Rimar47936762016-01-16 00:49:19 +000014#include "ARMEHABIPrinter.h"
Rafael Auler86fb7bf2018-03-08 00:46:53 +000015#include "DwarfCFIEHPrinter.h"
George Rimar47936762016-01-16 00:49:19 +000016#include "Error.h"
17#include "ObjDumper.h"
18#include "StackMapPrinter.h"
Zachary Turner88bb1632016-05-03 00:28:04 +000019#include "llvm-readobj.h"
Eugene Zelenko416e0592017-06-09 21:41:54 +000020#include "llvm/ADT/ArrayRef.h"
George Rimar762abff62017-09-16 14:29:51 +000021#include "llvm/ADT/DenseMap.h"
Matt Davis0d0e9c02019-02-05 21:01:01 +000022#include "llvm/ADT/DenseSet.h"
Wolfgang Piebc71c6292019-08-05 22:47:07 +000023#include "llvm/ADT/MapVector.h"
George Rimar47936762016-01-16 00:49:19 +000024#include "llvm/ADT/Optional.h"
Eugene Zelenko416e0592017-06-09 21:41:54 +000025#include "llvm/ADT/PointerIntPair.h"
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +000026#include "llvm/ADT/STLExtras.h"
George Rimar47936762016-01-16 00:49:19 +000027#include "llvm/ADT/SmallString.h"
Eugene Zelenko416e0592017-06-09 21:41:54 +000028#include "llvm/ADT/SmallVector.h"
George Rimar47936762016-01-16 00:49:19 +000029#include "llvm/ADT/StringExtras.h"
Eugene Zelenko416e0592017-06-09 21:41:54 +000030#include "llvm/ADT/StringRef.h"
31#include "llvm/ADT/Twine.h"
Scott Linderf5b36e52018-12-12 19:39:27 +000032#include "llvm/BinaryFormat/AMDGPUMetadataVerifier.h"
Eugene Zelenko416e0592017-06-09 21:41:54 +000033#include "llvm/BinaryFormat/ELF.h"
James Hendersone50d9cb2019-01-17 15:34:12 +000034#include "llvm/Demangle/Demangle.h"
Eugene Zelenko416e0592017-06-09 21:41:54 +000035#include "llvm/Object/ELF.h"
George Rimar47936762016-01-16 00:49:19 +000036#include "llvm/Object/ELFObjectFile.h"
Eugene Zelenko416e0592017-06-09 21:41:54 +000037#include "llvm/Object/ELFTypes.h"
38#include "llvm/Object/Error.h"
39#include "llvm/Object/ObjectFile.h"
Wolfgang Piebc71c6292019-08-05 22:47:07 +000040#include "llvm/Object/RelocationResolver.h"
Eugene Zelenko416e0592017-06-09 21:41:54 +000041#include "llvm/Object/StackMapParser.h"
Konstantin Zhuravlyovb3c605d2017-10-14 18:21:42 +000042#include "llvm/Support/AMDGPUMetadata.h"
Sam Parker34315ee2017-01-13 10:50:01 +000043#include "llvm/Support/ARMAttributeParser.h"
George Rimar47936762016-01-16 00:49:19 +000044#include "llvm/Support/ARMBuildAttributes.h"
Eugene Zelenko416e0592017-06-09 21:41:54 +000045#include "llvm/Support/Casting.h"
George Rimar47936762016-01-16 00:49:19 +000046#include "llvm/Support/Compiler.h"
Eugene Zelenko416e0592017-06-09 21:41:54 +000047#include "llvm/Support/Endian.h"
48#include "llvm/Support/ErrorHandling.h"
George Rimar47936762016-01-16 00:49:19 +000049#include "llvm/Support/Format.h"
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +000050#include "llvm/Support/FormatVariadic.h"
Zachary Turner88bb1632016-05-03 00:28:04 +000051#include "llvm/Support/FormattedStream.h"
Peter Collingbourne3e227332018-07-17 22:17:18 +000052#include "llvm/Support/LEB128.h"
George Rimar47936762016-01-16 00:49:19 +000053#include "llvm/Support/MathExtras.h"
54#include "llvm/Support/MipsABIFlags.h"
Zachary Turner88bb1632016-05-03 00:28:04 +000055#include "llvm/Support/ScopedPrinter.h"
George Rimar47936762016-01-16 00:49:19 +000056#include "llvm/Support/raw_ostream.h"
Eugene Zelenko416e0592017-06-09 21:41:54 +000057#include <algorithm>
58#include <cinttypes>
59#include <cstddef>
60#include <cstdint>
61#include <cstdlib>
62#include <iterator>
63#include <memory>
64#include <string>
65#include <system_error>
66#include <vector>
George Rimar47936762016-01-16 00:49:19 +000067
68using namespace llvm;
69using namespace llvm::object;
70using namespace ELF;
71
George Rimar9e88a262019-05-14 14:22:44 +000072#define LLVM_READOBJ_ENUM_CASE(ns, enum) \
73 case ns::enum: \
74 return #enum;
George Rimar47936762016-01-16 00:49:19 +000075
George Rimar9e88a262019-05-14 14:22:44 +000076#define ENUM_ENT(enum, altName) \
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +000077 { #enum, altName, ELF::enum }
78
George Rimar9e88a262019-05-14 14:22:44 +000079#define ENUM_ENT_1(enum) \
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +000080 { #enum, #enum, ELF::enum }
81
Hemant Kulkarni7d564ba2016-03-28 17:20:23 +000082#define LLVM_READOBJ_PHDR_ENUM(ns, enum) \
83 case ns::enum: \
84 return std::string(#enum).substr(3);
85
Hemant Kulkarni206ba842016-03-09 19:16:13 +000086#define TYPEDEF_ELF_TYPES(ELFT) \
Eugene Zelenko416e0592017-06-09 21:41:54 +000087 using ELFO = ELFFile<ELFT>; \
Rui Ueyama478d6352018-01-12 02:28:31 +000088 using Elf_Addr = typename ELFT::Addr; \
89 using Elf_Shdr = typename ELFT::Shdr; \
90 using Elf_Sym = typename ELFT::Sym; \
91 using Elf_Dyn = typename ELFT::Dyn; \
92 using Elf_Dyn_Range = typename ELFT::DynRange; \
93 using Elf_Rel = typename ELFT::Rel; \
94 using Elf_Rela = typename ELFT::Rela; \
Jake Ehrlich0f440d82018-06-28 21:07:34 +000095 using Elf_Relr = typename ELFT::Relr; \
Rui Ueyama478d6352018-01-12 02:28:31 +000096 using Elf_Rel_Range = typename ELFT::RelRange; \
97 using Elf_Rela_Range = typename ELFT::RelaRange; \
Jake Ehrlich0f440d82018-06-28 21:07:34 +000098 using Elf_Relr_Range = typename ELFT::RelrRange; \
Rui Ueyama478d6352018-01-12 02:28:31 +000099 using Elf_Phdr = typename ELFT::Phdr; \
100 using Elf_Half = typename ELFT::Half; \
101 using Elf_Ehdr = typename ELFT::Ehdr; \
102 using Elf_Word = typename ELFT::Word; \
103 using Elf_Hash = typename ELFT::Hash; \
104 using Elf_GnuHash = typename ELFT::GnuHash; \
George Rimarec895f12019-05-16 06:22:51 +0000105 using Elf_Note = typename ELFT::Note; \
Rui Ueyama478d6352018-01-12 02:28:31 +0000106 using Elf_Sym_Range = typename ELFT::SymRange; \
107 using Elf_Versym = typename ELFT::Versym; \
108 using Elf_Verneed = typename ELFT::Verneed; \
109 using Elf_Vernaux = typename ELFT::Vernaux; \
110 using Elf_Verdef = typename ELFT::Verdef; \
111 using Elf_Verdaux = typename ELFT::Verdaux; \
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000112 using Elf_CGProfile = typename ELFT::CGProfile; \
Rui Ueyama478d6352018-01-12 02:28:31 +0000113 using uintX_t = typename ELFT::uint;
Hemant Kulkarni206ba842016-03-09 19:16:13 +0000114
George Rimar47936762016-01-16 00:49:19 +0000115namespace {
116
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000117template <class ELFT> class DumpStyle;
118
Rafael Espindolace2fbdd2016-02-17 15:38:21 +0000119/// Represents a contiguous uniform range in the file. We cannot just create a
120/// range directly because when creating one of these from the .dynamic table
121/// the size, entity size and virtual address are different entries in arbitrary
122/// order (DT_REL, DT_RELSZ, DT_RELENT for example).
Rafael Espindola65a6fd82016-02-16 14:27:33 +0000123struct DynRegionInfo {
Eugene Zelenko416e0592017-06-09 21:41:54 +0000124 DynRegionInfo() = default;
Rafael Espindolace2fbdd2016-02-17 15:38:21 +0000125 DynRegionInfo(const void *A, uint64_t S, uint64_t ES)
126 : Addr(A), Size(S), EntSize(ES) {}
Eugene Zelenko416e0592017-06-09 21:41:54 +0000127
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000128 /// Address in current address space.
Eugene Zelenko416e0592017-06-09 21:41:54 +0000129 const void *Addr = nullptr;
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000130 /// Size in bytes of the region.
Eugene Zelenko416e0592017-06-09 21:41:54 +0000131 uint64_t Size = 0;
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000132 /// Size of each entity in the region.
Eugene Zelenko416e0592017-06-09 21:41:54 +0000133 uint64_t EntSize = 0;
Rafael Espindolac70aeda2016-02-16 14:50:39 +0000134
Rafael Espindolaaafcf752016-04-05 14:47:22 +0000135 template <typename Type> ArrayRef<Type> getAsArrayRef() const {
Rafael Espindolac70aeda2016-02-16 14:50:39 +0000136 const Type *Start = reinterpret_cast<const Type *>(Addr);
Rafael Espindola944f6552016-02-16 15:16:00 +0000137 if (!Start)
138 return {Start, Start};
George Rimarbadece02019-07-11 12:26:48 +0000139 if (EntSize != sizeof(Type) || Size % EntSize) {
140 // TODO: Add a section index to this warning.
141 reportWarning("invalid section size (" + Twine(Size) +
142 ") or entity size (" + Twine(EntSize) + ")");
143 return {Start, Start};
144 }
Rafael Espindolac70aeda2016-02-16 14:50:39 +0000145 return {Start, Start + (Size / EntSize)};
146 }
Rafael Espindola65a6fd82016-02-16 14:27:33 +0000147};
148
George Rimar9e88a262019-05-14 14:22:44 +0000149template <typename ELFT> class ELFDumper : public ObjDumper {
George Rimar47936762016-01-16 00:49:19 +0000150public:
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000151 ELFDumper(const object::ELFObjectFile<ELFT> *ObjF, ScopedPrinter &Writer);
George Rimar47936762016-01-16 00:49:19 +0000152
153 void printFileHeaders() override;
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +0000154 void printSectionHeaders() override;
George Rimar47936762016-01-16 00:49:19 +0000155 void printRelocations() override;
156 void printDynamicRelocations() override;
James Henderson21ed8682019-01-23 16:15:39 +0000157 void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols) override;
James Henderson5fc812f2019-01-22 09:35:35 +0000158 void printHashSymbols() override;
George Rimar47936762016-01-16 00:49:19 +0000159 void printUnwindInfo() override;
160
161 void printDynamicTable() override;
162 void printNeededLibraries() override;
Matt Davis50ca8ed2019-02-01 18:51:10 +0000163 void printProgramHeaders(bool PrintProgramHeaders,
164 cl::boolOrDefault PrintSectionMapping) override;
George Rimar47936762016-01-16 00:49:19 +0000165 void printHashTable() override;
166 void printGnuHashTable() override;
167 void printLoadName() override;
168 void printVersionInfo() override;
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000169 void printGroupSections() override;
George Rimar47936762016-01-16 00:49:19 +0000170
171 void printAttributes() override;
172 void printMipsPLTGOT() override;
173 void printMipsABIFlags() override;
174 void printMipsReginfo() override;
Simon Atanasyan8a71b532016-05-04 05:58:57 +0000175 void printMipsOptions() override;
George Rimar47936762016-01-16 00:49:19 +0000176
177 void printStackMap() const override;
178
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +0000179 void printHashHistogram() override;
180
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000181 void printCGProfile() override;
Peter Collingbourne3e227332018-07-17 22:17:18 +0000182 void printAddrsig() override;
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000183
Saleem Abdulrasool6a405442016-08-30 18:52:02 +0000184 void printNotes() override;
185
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000186 void printELFLinkerOptions() override;
Wolfgang Piebc71c6292019-08-05 22:47:07 +0000187 void printStackSizes() override;
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000188
George Rimara1370872019-07-16 11:07:30 +0000189 const object::ELFObjectFile<ELFT> *getElfObject() const { return ObjF; };
190
George Rimar47936762016-01-16 00:49:19 +0000191private:
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000192 std::unique_ptr<DumpStyle<ELFT>> ELFDumperStyle;
Eugene Zelenko416e0592017-06-09 21:41:54 +0000193
Rafael Espindola6bc29902016-10-06 14:07:26 +0000194 TYPEDEF_ELF_TYPES(ELFT)
George Rimar47936762016-01-16 00:49:19 +0000195
Rafael Espindolae17c3f32016-02-17 16:48:00 +0000196 DynRegionInfo checkDRI(DynRegionInfo DRI) {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000197 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
Rafael Espindolae17c3f32016-02-17 16:48:00 +0000198 if (DRI.Addr < Obj->base() ||
Xing GUO0df95d22019-04-08 11:48:36 +0000199 reinterpret_cast<const uint8_t *>(DRI.Addr) + DRI.Size >
200 Obj->base() + Obj->getBufSize())
Rafael Espindolae17c3f32016-02-17 16:48:00 +0000201 error(llvm::object::object_error::parse_failed);
202 return DRI;
203 }
204
205 DynRegionInfo createDRIFrom(const Elf_Phdr *P, uintX_t EntSize) {
George Rimar9e88a262019-05-14 14:22:44 +0000206 return checkDRI(
207 {ObjF->getELFFile()->base() + P->p_offset, P->p_filesz, EntSize});
Rafael Espindolae17c3f32016-02-17 16:48:00 +0000208 }
209
Rafael Espindolace2fbdd2016-02-17 15:38:21 +0000210 DynRegionInfo createDRIFrom(const Elf_Shdr *S) {
George Rimar9e88a262019-05-14 14:22:44 +0000211 return checkDRI(
212 {ObjF->getELFFile()->base() + S->sh_offset, S->sh_size, S->sh_entsize});
Rafael Espindolace2fbdd2016-02-17 15:38:21 +0000213 }
214
George Rimar72f821d2019-05-20 15:41:48 +0000215 void loadDynamicTable(const ELFFile<ELFT> *Obj);
216 void parseDynamicTable();
Michael J. Spencer60d82b22016-02-11 04:59:37 +0000217
George Rimar47936762016-01-16 00:49:19 +0000218 StringRef getSymbolVersion(StringRef StrTab, const Elf_Sym *symb,
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000219 bool &IsDefault) const;
220 void LoadVersionMap() const;
George Rimar47936762016-01-16 00:49:19 +0000221 void LoadVersionNeeds(const Elf_Shdr *ec) const;
222 void LoadVersionDefs(const Elf_Shdr *sec) const;
223
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000224 const object::ELFObjectFile<ELFT> *ObjF;
Simon Atanasyan72155c32016-01-16 22:40:09 +0000225 DynRegionInfo DynRelRegion;
George Rimar47936762016-01-16 00:49:19 +0000226 DynRegionInfo DynRelaRegion;
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000227 DynRegionInfo DynRelrRegion;
Rafael Espindola944f6552016-02-16 15:16:00 +0000228 DynRegionInfo DynPLTRelRegion;
Rafael Espindolace2fbdd2016-02-17 15:38:21 +0000229 DynRegionInfo DynSymRegion;
Rafael Espindolae17c3f32016-02-17 16:48:00 +0000230 DynRegionInfo DynamicTable;
George Rimar47936762016-01-16 00:49:19 +0000231 StringRef DynamicStringTable;
Yuanfang Chenabbc3ff2019-07-18 17:04:28 +0000232 std::string SOName = "<Not found>";
George Rimar47936762016-01-16 00:49:19 +0000233 const Elf_Hash *HashTable = nullptr;
234 const Elf_GnuHash *GnuHashTable = nullptr;
George Rimar47936762016-01-16 00:49:19 +0000235 const Elf_Shdr *DotSymtabSec = nullptr;
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000236 const Elf_Shdr *DotCGProfileSec = nullptr;
Peter Collingbourne3e227332018-07-17 22:17:18 +0000237 const Elf_Shdr *DotAddrsigSec = nullptr;
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +0000238 StringRef DynSymtabName;
George Rimar47936762016-01-16 00:49:19 +0000239 ArrayRef<Elf_Word> ShndxTable;
240
George Rimarec895f12019-05-16 06:22:51 +0000241 const Elf_Shdr *SymbolVersionSection = nullptr; // .gnu.version
Xing GUO09a77fe2019-03-29 01:26:36 +0000242 const Elf_Shdr *SymbolVersionNeedSection = nullptr; // .gnu.version_r
George Rimarec895f12019-05-16 06:22:51 +0000243 const Elf_Shdr *SymbolVersionDefSection = nullptr; // .gnu.version_d
George Rimar47936762016-01-16 00:49:19 +0000244
245 // Records for each version index the corresponding Verdef or Vernaux entry.
246 // This is filled the first time LoadVersionMap() is called.
247 class VersionMapEntry : public PointerIntPair<const void *, 1> {
248 public:
249 // If the integer is 0, this is an Elf_Verdef*.
250 // If the integer is 1, this is an Elf_Vernaux*.
251 VersionMapEntry() : PointerIntPair<const void *, 1>(nullptr, 0) {}
252 VersionMapEntry(const Elf_Verdef *verdef)
253 : PointerIntPair<const void *, 1>(verdef, 0) {}
254 VersionMapEntry(const Elf_Vernaux *vernaux)
255 : PointerIntPair<const void *, 1>(vernaux, 1) {}
Eugene Zelenko416e0592017-06-09 21:41:54 +0000256
George Rimar47936762016-01-16 00:49:19 +0000257 bool isNull() const { return getPointer() == nullptr; }
258 bool isVerdef() const { return !isNull() && getInt() == 0; }
259 bool isVernaux() const { return !isNull() && getInt() == 1; }
260 const Elf_Verdef *getVerdef() const {
261 return isVerdef() ? (const Elf_Verdef *)getPointer() : nullptr;
262 }
263 const Elf_Vernaux *getVernaux() const {
264 return isVernaux() ? (const Elf_Vernaux *)getPointer() : nullptr;
265 }
266 };
267 mutable SmallVector<VersionMapEntry, 16> VersionMap;
268
269public:
270 Elf_Dyn_Range dynamic_table() const {
Simon Atanasyan8362cbe2019-05-24 12:22:53 +0000271 // A valid .dynamic section contains an array of entries terminated
272 // with a DT_NULL entry. However, sometimes the section content may
273 // continue past the DT_NULL entry, so to dump the section correctly,
274 // we first find the end of the entries by iterating over them.
275 Elf_Dyn_Range Table = DynamicTable.getAsArrayRef<Elf_Dyn>();
276
277 size_t Size = 0;
278 while (Size < Table.size())
279 if (Table[Size++].getTag() == DT_NULL)
280 break;
281
282 return Table.slice(0, Size);
George Rimar47936762016-01-16 00:49:19 +0000283 }
284
Rafael Espindolace2fbdd2016-02-17 15:38:21 +0000285 Elf_Sym_Range dynamic_symbols() const {
Rafael Espindolaaafcf752016-04-05 14:47:22 +0000286 return DynSymRegion.getAsArrayRef<Elf_Sym>();
Rafael Espindolace2fbdd2016-02-17 15:38:21 +0000287 }
288
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000289 Elf_Rel_Range dyn_rels() const;
290 Elf_Rela_Range dyn_relas() const;
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000291 Elf_Relr_Range dyn_relrs() const;
George Rimar47936762016-01-16 00:49:19 +0000292 std::string getFullSymbolName(const Elf_Sym *Symbol, StringRef StrTable,
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000293 bool IsDynamic) const;
Simon Atanasyan62d32592017-12-21 10:26:02 +0000294 void getSectionNameIndex(const Elf_Sym *Symbol, const Elf_Sym *FirstSym,
295 StringRef &SectionName,
296 unsigned &SectionIndex) const;
James Hendersone50d9cb2019-01-17 15:34:12 +0000297 std::string getStaticSymbolName(uint32_t Index) const;
Yuanfang Chenabbc3ff2019-07-18 17:04:28 +0000298 std::string getDynamicString(uint64_t Value) const;
Liang Zou9f4a4d32019-03-31 14:49:00 +0000299 StringRef getSymbolVersionByIndex(StringRef StrTab,
300 uint32_t VersionSymbolIndex,
Xing GUO7ffd9112019-03-28 12:51:46 +0000301 bool &IsDefault) const;
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +0000302
303 void printSymbolsHelper(bool IsDynamic) const;
Simon Atanasyan8362cbe2019-05-24 12:22:53 +0000304 void printDynamicEntry(raw_ostream &OS, uint64_t Type, uint64_t Value) const;
305
George Rimar47936762016-01-16 00:49:19 +0000306 const Elf_Shdr *getDotSymtabSec() const { return DotSymtabSec; }
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000307 const Elf_Shdr *getDotCGProfileSec() const { return DotCGProfileSec; }
Peter Collingbourne3e227332018-07-17 22:17:18 +0000308 const Elf_Shdr *getDotAddrsigSec() const { return DotAddrsigSec; }
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000309 ArrayRef<Elf_Word> getShndxTable() const { return ShndxTable; }
Rafael Espindolace2fbdd2016-02-17 15:38:21 +0000310 StringRef getDynamicStringTable() const { return DynamicStringTable; }
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000311 const DynRegionInfo &getDynRelRegion() const { return DynRelRegion; }
312 const DynRegionInfo &getDynRelaRegion() const { return DynRelaRegion; }
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000313 const DynRegionInfo &getDynRelrRegion() const { return DynRelrRegion; }
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000314 const DynRegionInfo &getDynPLTRelRegion() const { return DynPLTRelRegion; }
Simon Atanasyan8362cbe2019-05-24 12:22:53 +0000315 const DynRegionInfo &getDynamicTableRegion() const { return DynamicTable; }
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +0000316 const Elf_Hash *getHashTable() const { return HashTable; }
317 const Elf_GnuHash *getGnuHashTable() const { return GnuHashTable; }
George Rimar47936762016-01-16 00:49:19 +0000318};
319
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +0000320template <class ELFT>
321void ELFDumper<ELFT>::printSymbolsHelper(bool IsDynamic) const {
322 StringRef StrTable, SymtabName;
323 size_t Entries = 0;
324 Elf_Sym_Range Syms(nullptr, nullptr);
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000325 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +0000326 if (IsDynamic) {
327 StrTable = DynamicStringTable;
328 Syms = dynamic_symbols();
329 SymtabName = DynSymtabName;
330 if (DynSymRegion.Addr)
331 Entries = DynSymRegion.Size / DynSymRegion.EntSize;
332 } else {
333 if (!DotSymtabSec)
334 return;
335 StrTable = unwrapOrError(Obj->getStringTableForSymtab(*DotSymtabSec));
Rafael Espindola9ea68342016-11-03 13:43:30 +0000336 Syms = unwrapOrError(Obj->symbols(DotSymtabSec));
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +0000337 SymtabName = unwrapOrError(Obj->getSectionName(DotSymtabSec));
338 Entries = DotSymtabSec->getEntityCount();
339 }
340 if (Syms.begin() == Syms.end())
341 return;
342 ELFDumperStyle->printSymtabMessage(Obj, SymtabName, Entries);
343 for (const auto &Sym : Syms)
344 ELFDumperStyle->printSymbol(Obj, &Sym, Syms.begin(), StrTable, IsDynamic);
345}
346
Simon Atanasyan62d32592017-12-21 10:26:02 +0000347template <class ELFT> class MipsGOTParser;
348
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000349template <typename ELFT> class DumpStyle {
350public:
Rui Ueyama478d6352018-01-12 02:28:31 +0000351 using Elf_Shdr = typename ELFT::Shdr;
352 using Elf_Sym = typename ELFT::Sym;
Wolfgang Piebc71c6292019-08-05 22:47:07 +0000353 using Elf_Addr = typename ELFT::Addr;
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +0000354
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000355 DumpStyle(ELFDumper<ELFT> *Dumper) : Dumper(Dumper) {}
Eugene Zelenko416e0592017-06-09 21:41:54 +0000356 virtual ~DumpStyle() = default;
357
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +0000358 virtual void printFileHeaders(const ELFFile<ELFT> *Obj) = 0;
Hemant Kulkarni206ba842016-03-09 19:16:13 +0000359 virtual void printGroupSections(const ELFFile<ELFT> *Obj) = 0;
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000360 virtual void printRelocations(const ELFFile<ELFT> *Obj) = 0;
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +0000361 virtual void printSectionHeaders(const ELFFile<ELFT> *Obj) = 0;
James Henderson21ed8682019-01-23 16:15:39 +0000362 virtual void printSymbols(const ELFFile<ELFT> *Obj, bool PrintSymbols,
363 bool PrintDynamicSymbols) = 0;
James Henderson5fc812f2019-01-22 09:35:35 +0000364 virtual void printHashSymbols(const ELFFile<ELFT> *Obj) {}
Simon Atanasyan8362cbe2019-05-24 12:22:53 +0000365 virtual void printDynamic(const ELFFile<ELFT> *Obj) {}
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000366 virtual void printDynamicRelocations(const ELFFile<ELFT> *Obj) = 0;
James Henderson5fc812f2019-01-22 09:35:35 +0000367 virtual void printSymtabMessage(const ELFFile<ELFT> *Obj, StringRef Name,
Eugene Zelenko416e0592017-06-09 21:41:54 +0000368 size_t Offset) {}
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +0000369 virtual void printSymbol(const ELFFile<ELFT> *Obj, const Elf_Sym *Symbol,
370 const Elf_Sym *FirstSym, StringRef StrTable,
371 bool IsDynamic) = 0;
Matt Davis50ca8ed2019-02-01 18:51:10 +0000372 virtual void printProgramHeaders(const ELFFile<ELFT> *Obj,
373 bool PrintProgramHeaders,
374 cl::boolOrDefault PrintSectionMapping) = 0;
Xing GUOea16be12019-03-25 11:02:49 +0000375 virtual void printVersionSymbolSection(const ELFFile<ELFT> *Obj,
376 const Elf_Shdr *Sec) = 0;
377 virtual void printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
378 const Elf_Shdr *Sec) = 0;
379 virtual void printVersionDependencySection(const ELFFile<ELFT> *Obj,
380 const Elf_Shdr *Sec) = 0;
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +0000381 virtual void printHashHistogram(const ELFFile<ELFT> *Obj) = 0;
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000382 virtual void printCGProfile(const ELFFile<ELFT> *Obj) = 0;
Peter Collingbourne3e227332018-07-17 22:17:18 +0000383 virtual void printAddrsig(const ELFFile<ELFT> *Obj) = 0;
Saleem Abdulrasool6a405442016-08-30 18:52:02 +0000384 virtual void printNotes(const ELFFile<ELFT> *Obj) = 0;
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000385 virtual void printELFLinkerOptions(const ELFFile<ELFT> *Obj) = 0;
Wolfgang Piebc71c6292019-08-05 22:47:07 +0000386 virtual void printStackSizes(const ELFObjectFile<ELFT> *Obj) = 0;
387 void printNonRelocatableStackSizes(const ELFObjectFile<ELFT> *Obj,
388 std::function<void()> PrintHeader);
389 void printRelocatableStackSizes(const ELFObjectFile<ELFT> *Obj,
390 std::function<void()> PrintHeader);
391 void printFunctionStackSize(const ELFObjectFile<ELFT> *Obj, uint64_t SymValue,
392 SectionRef FunctionSec,
393 const StringRef SectionName, DataExtractor Data,
394 uint64_t *Offset);
395 void printStackSize(const ELFObjectFile<ELFT> *Obj, RelocationRef Rel,
396 SectionRef FunctionSec,
397 const StringRef &StackSizeSectionName,
398 const RelocationResolver &Resolver, DataExtractor Data);
399 virtual void printStackSizeEntry(uint64_t Size, StringRef FuncName) = 0;
Simon Atanasyan62d32592017-12-21 10:26:02 +0000400 virtual void printMipsGOT(const MipsGOTParser<ELFT> &Parser) = 0;
401 virtual void printMipsPLT(const MipsGOTParser<ELFT> &Parser) = 0;
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000402 const ELFDumper<ELFT> *dumper() const { return Dumper; }
Eugene Zelenko416e0592017-06-09 21:41:54 +0000403
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000404private:
405 const ELFDumper<ELFT> *Dumper;
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000406};
407
408template <typename ELFT> class GNUStyle : public DumpStyle<ELFT> {
Jordan Rupprecht565f1e22019-06-12 20:16:22 +0000409 formatted_raw_ostream &OS;
Eugene Zelenko416e0592017-06-09 21:41:54 +0000410
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000411public:
Hemant Kulkarni206ba842016-03-09 19:16:13 +0000412 TYPEDEF_ELF_TYPES(ELFT)
Eugene Zelenko416e0592017-06-09 21:41:54 +0000413
Zachary Turner88bb1632016-05-03 00:28:04 +0000414 GNUStyle(ScopedPrinter &W, ELFDumper<ELFT> *Dumper)
Jordan Rupprecht565f1e22019-06-12 20:16:22 +0000415 : DumpStyle<ELFT>(Dumper),
416 OS(static_cast<formatted_raw_ostream&>(W.getOStream())) {
417 assert (&W.getOStream() == &llvm::fouts());
418 }
Eugene Zelenko416e0592017-06-09 21:41:54 +0000419
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000420 void printFileHeaders(const ELFO *Obj) override;
Hemant Kulkarni206ba842016-03-09 19:16:13 +0000421 void printGroupSections(const ELFFile<ELFT> *Obj) override;
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000422 void printRelocations(const ELFO *Obj) override;
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +0000423 void printSectionHeaders(const ELFO *Obj) override;
James Henderson21ed8682019-01-23 16:15:39 +0000424 void printSymbols(const ELFO *Obj, bool PrintSymbols,
425 bool PrintDynamicSymbols) override;
James Henderson5fc812f2019-01-22 09:35:35 +0000426 void printHashSymbols(const ELFO *Obj) override;
Simon Atanasyan8362cbe2019-05-24 12:22:53 +0000427 void printDynamic(const ELFFile<ELFT> *Obj) override;
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000428 void printDynamicRelocations(const ELFO *Obj) override;
Eugene Zelenko416e0592017-06-09 21:41:54 +0000429 void printSymtabMessage(const ELFO *Obj, StringRef Name,
430 size_t Offset) override;
Matt Davis50ca8ed2019-02-01 18:51:10 +0000431 void printProgramHeaders(const ELFO *Obj, bool PrintProgramHeaders,
432 cl::boolOrDefault PrintSectionMapping) override;
Xing GUOea16be12019-03-25 11:02:49 +0000433 void printVersionSymbolSection(const ELFFile<ELFT> *Obj,
434 const Elf_Shdr *Sec) override;
435 void printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
436 const Elf_Shdr *Sec) override;
437 void printVersionDependencySection(const ELFFile<ELFT> *Obj,
438 const Elf_Shdr *Sec) override;
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +0000439 void printHashHistogram(const ELFFile<ELFT> *Obj) override;
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000440 void printCGProfile(const ELFFile<ELFT> *Obj) override;
Peter Collingbourne3e227332018-07-17 22:17:18 +0000441 void printAddrsig(const ELFFile<ELFT> *Obj) override;
Saleem Abdulrasool6a405442016-08-30 18:52:02 +0000442 void printNotes(const ELFFile<ELFT> *Obj) override;
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000443 void printELFLinkerOptions(const ELFFile<ELFT> *Obj) override;
Wolfgang Piebc71c6292019-08-05 22:47:07 +0000444 void printStackSizes(const ELFObjectFile<ELFT> *Obj) override;
445 void printStackSizeEntry(uint64_t Size, StringRef FuncName) override;
Simon Atanasyan62d32592017-12-21 10:26:02 +0000446 void printMipsGOT(const MipsGOTParser<ELFT> &Parser) override;
447 void printMipsPLT(const MipsGOTParser<ELFT> &Parser) override;
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000448
449private:
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000450 struct Field {
George Rimar4b4899b2019-01-30 14:08:55 +0000451 std::string Str;
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000452 unsigned Column;
Eugene Zelenko416e0592017-06-09 21:41:54 +0000453
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000454 Field(StringRef S, unsigned Col) : Str(S), Column(Col) {}
George Rimar4b4899b2019-01-30 14:08:55 +0000455 Field(unsigned Col) : Column(Col) {}
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000456 };
457
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000458 template <typename T, typename TEnum>
459 std::string printEnum(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues) {
460 for (const auto &EnumItem : EnumValues)
461 if (EnumItem.Value == Value)
462 return EnumItem.AltName;
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000463 return to_hexString(Value, false);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000464 }
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +0000465
Simon Atanasyan19932542018-10-25 05:39:27 +0000466 template <typename T, typename TEnum>
467 std::string printFlags(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues,
468 TEnum EnumMask1 = {}, TEnum EnumMask2 = {},
469 TEnum EnumMask3 = {}) {
470 std::string Str;
471 for (const auto &Flag : EnumValues) {
472 if (Flag.Value == 0)
473 continue;
474
475 TEnum EnumMask{};
476 if (Flag.Value & EnumMask1)
477 EnumMask = EnumMask1;
478 else if (Flag.Value & EnumMask2)
479 EnumMask = EnumMask2;
480 else if (Flag.Value & EnumMask3)
481 EnumMask = EnumMask3;
482 bool IsEnum = (Flag.Value & EnumMask) != 0;
483 if ((!IsEnum && (Value & Flag.Value) == Flag.Value) ||
484 (IsEnum && (Value & EnumMask) == Flag.Value)) {
485 if (!Str.empty())
486 Str += ", ";
487 Str += Flag.AltName;
488 }
489 }
490 return Str;
491 }
492
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000493 formatted_raw_ostream &printField(struct Field F) {
494 if (F.Column != 0)
495 OS.PadToColumn(F.Column);
496 OS << F.Str;
497 OS.flush();
498 return OS;
499 }
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +0000500 void printHashedSymbol(const ELFO *Obj, const Elf_Sym *FirstSym, uint32_t Sym,
501 StringRef StrTable, uint32_t Bucket);
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000502 void printRelocHeader(unsigned SType);
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000503 void printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab,
504 const Elf_Rela &R, bool IsRela);
James Henderson814ab372019-03-29 11:47:19 +0000505 void printRelocation(const ELFO *Obj, const Elf_Sym *Sym,
506 StringRef SymbolName, const Elf_Rela &R, bool IsRela);
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +0000507 void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First,
508 StringRef StrTable, bool IsDynamic) override;
509 std::string getSymbolSectionNdx(const ELFO *Obj, const Elf_Sym *Symbol,
510 const Elf_Sym *FirstSym);
Hemant Kulkarnia79c7982016-03-29 02:41:49 +0000511 void printDynamicRelocation(const ELFO *Obj, Elf_Rela R, bool IsRela);
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +0000512 bool checkTLSSections(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
513 bool checkoffsets(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
514 bool checkVMA(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
515 bool checkPTDynamic(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
Matt Davis50ca8ed2019-02-01 18:51:10 +0000516 void printProgramHeaders(const ELFO *Obj);
517 void printSectionMapping(const ELFO *Obj);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000518};
519
520template <typename ELFT> class LLVMStyle : public DumpStyle<ELFT> {
521public:
Hemant Kulkarni206ba842016-03-09 19:16:13 +0000522 TYPEDEF_ELF_TYPES(ELFT)
Eugene Zelenko416e0592017-06-09 21:41:54 +0000523
Zachary Turner88bb1632016-05-03 00:28:04 +0000524 LLVMStyle(ScopedPrinter &W, ELFDumper<ELFT> *Dumper)
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000525 : DumpStyle<ELFT>(Dumper), W(W) {}
526
527 void printFileHeaders(const ELFO *Obj) override;
Hemant Kulkarni206ba842016-03-09 19:16:13 +0000528 void printGroupSections(const ELFFile<ELFT> *Obj) override;
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000529 void printRelocations(const ELFO *Obj) override;
530 void printRelocations(const Elf_Shdr *Sec, const ELFO *Obj);
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +0000531 void printSectionHeaders(const ELFO *Obj) override;
James Henderson21ed8682019-01-23 16:15:39 +0000532 void printSymbols(const ELFO *Obj, bool PrintSymbols,
533 bool PrintDynamicSymbols) override;
Simon Atanasyan8362cbe2019-05-24 12:22:53 +0000534 void printDynamic(const ELFFile<ELFT> *Obj) override;
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000535 void printDynamicRelocations(const ELFO *Obj) override;
Matt Davis50ca8ed2019-02-01 18:51:10 +0000536 void printProgramHeaders(const ELFO *Obj, bool PrintProgramHeaders,
537 cl::boolOrDefault PrintSectionMapping) override;
Xing GUOea16be12019-03-25 11:02:49 +0000538 void printVersionSymbolSection(const ELFFile<ELFT> *Obj,
539 const Elf_Shdr *Sec) override;
540 void printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
541 const Elf_Shdr *Sec) override;
542 void printVersionDependencySection(const ELFFile<ELFT> *Obj,
543 const Elf_Shdr *Sec) override;
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +0000544 void printHashHistogram(const ELFFile<ELFT> *Obj) override;
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000545 void printCGProfile(const ELFFile<ELFT> *Obj) override;
Peter Collingbourne3e227332018-07-17 22:17:18 +0000546 void printAddrsig(const ELFFile<ELFT> *Obj) override;
Saleem Abdulrasool6a405442016-08-30 18:52:02 +0000547 void printNotes(const ELFFile<ELFT> *Obj) override;
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000548 void printELFLinkerOptions(const ELFFile<ELFT> *Obj) override;
Wolfgang Piebc71c6292019-08-05 22:47:07 +0000549 void printStackSizes(const ELFObjectFile<ELFT> *Obj) override;
550 void printStackSizeEntry(uint64_t Size, StringRef FuncName) override;
Simon Atanasyan62d32592017-12-21 10:26:02 +0000551 void printMipsGOT(const MipsGOTParser<ELFT> &Parser) override;
552 void printMipsPLT(const MipsGOTParser<ELFT> &Parser) override;
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000553
554private:
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000555 void printRelocation(const ELFO *Obj, Elf_Rela Rel, const Elf_Shdr *SymTab);
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000556 void printDynamicRelocation(const ELFO *Obj, Elf_Rela Rel);
James Henderson21ed8682019-01-23 16:15:39 +0000557 void printSymbols(const ELFO *Obj);
558 void printDynamicSymbols(const ELFO *Obj);
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +0000559 void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First,
560 StringRef StrTable, bool IsDynamic) override;
Matt Davis50ca8ed2019-02-01 18:51:10 +0000561 void printProgramHeaders(const ELFO *Obj);
562 void printSectionMapping(const ELFO *Obj) {}
Eugene Zelenko416e0592017-06-09 21:41:54 +0000563
Zachary Turner88bb1632016-05-03 00:28:04 +0000564 ScopedPrinter &W;
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000565};
566
Eugene Zelenko416e0592017-06-09 21:41:54 +0000567} // end anonymous namespace
George Rimar47936762016-01-16 00:49:19 +0000568
569namespace llvm {
570
571template <class ELFT>
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000572static std::error_code createELFDumper(const ELFObjectFile<ELFT> *Obj,
Zachary Turner88bb1632016-05-03 00:28:04 +0000573 ScopedPrinter &Writer,
George Rimar47936762016-01-16 00:49:19 +0000574 std::unique_ptr<ObjDumper> &Result) {
575 Result.reset(new ELFDumper<ELFT>(Obj, Writer));
576 return readobj_error::success;
577}
578
579std::error_code createELFDumper(const object::ObjectFile *Obj,
Zachary Turner88bb1632016-05-03 00:28:04 +0000580 ScopedPrinter &Writer,
George Rimar47936762016-01-16 00:49:19 +0000581 std::unique_ptr<ObjDumper> &Result) {
582 // Little-endian 32-bit
583 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000584 return createELFDumper(ELFObj, Writer, Result);
George Rimar47936762016-01-16 00:49:19 +0000585
586 // Big-endian 32-bit
587 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000588 return createELFDumper(ELFObj, Writer, Result);
George Rimar47936762016-01-16 00:49:19 +0000589
590 // Little-endian 64-bit
591 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000592 return createELFDumper(ELFObj, Writer, Result);
George Rimar47936762016-01-16 00:49:19 +0000593
594 // Big-endian 64-bit
595 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000596 return createELFDumper(ELFObj, Writer, Result);
George Rimar47936762016-01-16 00:49:19 +0000597
598 return readobj_error::unsupported_obj_file_format;
599}
600
Eugene Zelenko416e0592017-06-09 21:41:54 +0000601} // end namespace llvm
George Rimar47936762016-01-16 00:49:19 +0000602
603// Iterate through the versions needed section, and place each Elf_Vernaux
604// in the VersionMap according to its index.
605template <class ELFT>
Xing GUOe855e2e2019-04-12 07:09:41 +0000606void ELFDumper<ELFT>::LoadVersionNeeds(const Elf_Shdr *Sec) const {
607 unsigned VerneedSize = Sec->sh_size; // Size of section in bytes
608 unsigned VerneedEntries = Sec->sh_info; // Number of Verneed entries
609 const uint8_t *VerneedStart = reinterpret_cast<const uint8_t *>(
610 ObjF->getELFFile()->base() + Sec->sh_offset);
611 const uint8_t *VerneedEnd = VerneedStart + VerneedSize;
George Rimar47936762016-01-16 00:49:19 +0000612 // The first Verneed entry is at the start of the section.
Xing GUOe855e2e2019-04-12 07:09:41 +0000613 const uint8_t *VerneedBuf = VerneedStart;
614 for (unsigned VerneedIndex = 0; VerneedIndex < VerneedEntries;
615 ++VerneedIndex) {
616 if (VerneedBuf + sizeof(Elf_Verneed) > VerneedEnd)
George Rimar47936762016-01-16 00:49:19 +0000617 report_fatal_error("Section ended unexpectedly while scanning "
618 "version needed records.");
Xing GUOe855e2e2019-04-12 07:09:41 +0000619 const Elf_Verneed *Verneed =
620 reinterpret_cast<const Elf_Verneed *>(VerneedBuf);
621 if (Verneed->vn_version != ELF::VER_NEED_CURRENT)
George Rimar47936762016-01-16 00:49:19 +0000622 report_fatal_error("Unexpected verneed version");
623 // Iterate through the Vernaux entries
Xing GUOe855e2e2019-04-12 07:09:41 +0000624 const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux;
625 for (unsigned VernauxIndex = 0; VernauxIndex < Verneed->vn_cnt;
626 ++VernauxIndex) {
627 if (VernauxBuf + sizeof(Elf_Vernaux) > VerneedEnd)
George Rimar47936762016-01-16 00:49:19 +0000628 report_fatal_error("Section ended unexpected while scanning auxiliary "
629 "version needed records.");
Xing GUOe855e2e2019-04-12 07:09:41 +0000630 const Elf_Vernaux *Vernaux =
631 reinterpret_cast<const Elf_Vernaux *>(VernauxBuf);
632 size_t Index = Vernaux->vna_other & ELF::VERSYM_VERSION;
633 if (Index >= VersionMap.size())
634 VersionMap.resize(Index + 1);
635 VersionMap[Index] = VersionMapEntry(Vernaux);
636 VernauxBuf += Vernaux->vna_next;
George Rimar47936762016-01-16 00:49:19 +0000637 }
Xing GUOe855e2e2019-04-12 07:09:41 +0000638 VerneedBuf += Verneed->vn_next;
George Rimar47936762016-01-16 00:49:19 +0000639 }
640}
641
642// Iterate through the version definitions, and place each Elf_Verdef
643// in the VersionMap according to its index.
644template <class ELFT>
Xing GUOe855e2e2019-04-12 07:09:41 +0000645void ELFDumper<ELFT>::LoadVersionDefs(const Elf_Shdr *Sec) const {
646 unsigned VerdefSize = Sec->sh_size; // Size of section in bytes
647 unsigned VerdefEntries = Sec->sh_info; // Number of Verdef entries
648 const uint8_t *VerdefStart = reinterpret_cast<const uint8_t *>(
649 ObjF->getELFFile()->base() + Sec->sh_offset);
650 const uint8_t *VerdefEnd = VerdefStart + VerdefSize;
George Rimar47936762016-01-16 00:49:19 +0000651 // The first Verdef entry is at the start of the section.
Xing GUOe855e2e2019-04-12 07:09:41 +0000652 const uint8_t *VerdefBuf = VerdefStart;
653 for (unsigned VerdefIndex = 0; VerdefIndex < VerdefEntries; ++VerdefIndex) {
654 if (VerdefBuf + sizeof(Elf_Verdef) > VerdefEnd)
George Rimar47936762016-01-16 00:49:19 +0000655 report_fatal_error("Section ended unexpectedly while scanning "
656 "version definitions.");
Xing GUOe855e2e2019-04-12 07:09:41 +0000657 const Elf_Verdef *Verdef = reinterpret_cast<const Elf_Verdef *>(VerdefBuf);
658 if (Verdef->vd_version != ELF::VER_DEF_CURRENT)
George Rimar47936762016-01-16 00:49:19 +0000659 report_fatal_error("Unexpected verdef version");
Xing GUOe855e2e2019-04-12 07:09:41 +0000660 size_t Index = Verdef->vd_ndx & ELF::VERSYM_VERSION;
661 if (Index >= VersionMap.size())
662 VersionMap.resize(Index + 1);
663 VersionMap[Index] = VersionMapEntry(Verdef);
664 VerdefBuf += Verdef->vd_next;
George Rimar47936762016-01-16 00:49:19 +0000665 }
666}
667
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000668template <class ELFT> void ELFDumper<ELFT>::LoadVersionMap() const {
George Rimar47936762016-01-16 00:49:19 +0000669 // If there is no dynamic symtab or version table, there is nothing to do.
Xing GUO09a77fe2019-03-29 01:26:36 +0000670 if (!DynSymRegion.Addr || !SymbolVersionSection)
George Rimar47936762016-01-16 00:49:19 +0000671 return;
672
673 // Has the VersionMap already been loaded?
Jordan Rupprecht16a0de22018-12-20 00:57:06 +0000674 if (!VersionMap.empty())
George Rimar47936762016-01-16 00:49:19 +0000675 return;
676
677 // The first two version indexes are reserved.
678 // Index 0 is LOCAL, index 1 is GLOBAL.
679 VersionMap.push_back(VersionMapEntry());
680 VersionMap.push_back(VersionMapEntry());
681
Xing GUO09a77fe2019-03-29 01:26:36 +0000682 if (SymbolVersionDefSection)
683 LoadVersionDefs(SymbolVersionDefSection);
George Rimar47936762016-01-16 00:49:19 +0000684
Xing GUO09a77fe2019-03-29 01:26:36 +0000685 if (SymbolVersionNeedSection)
686 LoadVersionNeeds(SymbolVersionNeedSection);
George Rimar47936762016-01-16 00:49:19 +0000687}
688
George Rimar47936762016-01-16 00:49:19 +0000689template <typename ELFT>
690StringRef ELFDumper<ELFT>::getSymbolVersion(StringRef StrTab,
Xing GUO137315e2019-03-28 12:51:35 +0000691 const Elf_Sym *Sym,
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000692 bool &IsDefault) const {
George Rimar47936762016-01-16 00:49:19 +0000693 // This is a dynamic symbol. Look in the GNU symbol version table.
Xing GUO09a77fe2019-03-29 01:26:36 +0000694 if (!SymbolVersionSection) {
George Rimar47936762016-01-16 00:49:19 +0000695 // No version table.
696 IsDefault = false;
Xing GUO12632c72019-03-28 12:51:56 +0000697 return "";
George Rimar47936762016-01-16 00:49:19 +0000698 }
699
700 // Determine the position in the symbol table of this entry.
Xing GUO137315e2019-03-28 12:51:35 +0000701 size_t EntryIndex = (reinterpret_cast<uintptr_t>(Sym) -
George Rimarec895f12019-05-16 06:22:51 +0000702 reinterpret_cast<uintptr_t>(DynSymRegion.Addr)) /
703 sizeof(Elf_Sym);
George Rimar47936762016-01-16 00:49:19 +0000704
Xing GUO7ffd9112019-03-28 12:51:46 +0000705 // Get the corresponding version index entry.
Xing GUO137315e2019-03-28 12:51:35 +0000706 const Elf_Versym *Versym =
707 unwrapOrError(ObjF->getELFFile()->template getEntry<Elf_Versym>(
Xing GUO09a77fe2019-03-29 01:26:36 +0000708 SymbolVersionSection, EntryIndex));
Xing GUO7ffd9112019-03-28 12:51:46 +0000709 return this->getSymbolVersionByIndex(StrTab, Versym->vs_index, IsDefault);
George Rimar47936762016-01-16 00:49:19 +0000710}
711
James Hendersone50d9cb2019-01-17 15:34:12 +0000712static std::string maybeDemangle(StringRef Name) {
713 return opts::Demangle ? demangle(Name) : Name.str();
714}
715
George Rimar47936762016-01-16 00:49:19 +0000716template <typename ELFT>
James Hendersone50d9cb2019-01-17 15:34:12 +0000717std::string ELFDumper<ELFT>::getStaticSymbolName(uint32_t Index) const {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000718 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
George Rimar9e88a262019-05-14 14:22:44 +0000719 StringRef StrTable =
720 unwrapOrError(Obj->getStringTableForSymtab(*DotSymtabSec));
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000721 Elf_Sym_Range Syms = unwrapOrError(Obj->symbols(DotSymtabSec));
722 if (Index >= Syms.size())
723 reportError("Invalid symbol index");
724 const Elf_Sym *Sym = &Syms[Index];
James Hendersone50d9cb2019-01-17 15:34:12 +0000725 return maybeDemangle(unwrapOrError(Sym->getName(StrTable)));
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000726}
727
728template <typename ELFT>
George Rimar9e88a262019-05-14 14:22:44 +0000729StringRef ELFDumper<ELFT>::getSymbolVersionByIndex(StringRef StrTab,
730 uint32_t SymbolVersionIndex,
731 bool &IsDefault) const {
Xing GUO137315e2019-03-28 12:51:35 +0000732 size_t VersionIndex = SymbolVersionIndex & VERSYM_VERSION;
733
734 // Special markers for unversioned symbols.
George Rimar9e88a262019-05-14 14:22:44 +0000735 if (VersionIndex == VER_NDX_LOCAL || VersionIndex == VER_NDX_GLOBAL) {
Xing GUO137315e2019-03-28 12:51:35 +0000736 IsDefault = false;
Xing GUO12632c72019-03-28 12:51:56 +0000737 return "";
Xing GUO137315e2019-03-28 12:51:35 +0000738 }
739
Xing GUO7ffd9112019-03-28 12:51:46 +0000740 // Lookup this symbol in the version table.
Xing GUO137315e2019-03-28 12:51:35 +0000741 LoadVersionMap();
742 if (VersionIndex >= VersionMap.size() || VersionMap[VersionIndex].isNull())
743 reportError("Invalid version entry");
744 const VersionMapEntry &Entry = VersionMap[VersionIndex];
745
Xing GUO7ffd9112019-03-28 12:51:46 +0000746 // Get the version name string.
Xing GUO137315e2019-03-28 12:51:35 +0000747 size_t NameOffset;
748 if (Entry.isVerdef()) {
749 // The first Verdaux entry holds the name.
750 NameOffset = Entry.getVerdef()->getAux()->vda_name;
751 IsDefault = !(SymbolVersionIndex & VERSYM_HIDDEN);
752 } else {
753 NameOffset = Entry.getVernaux()->vna_name;
754 IsDefault = false;
755 }
756 if (NameOffset >= StrTab.size())
757 reportError("Invalid string offset");
Xing GUO12632c72019-03-28 12:51:56 +0000758 return StrTab.data() + NameOffset;
Xing GUO137315e2019-03-28 12:51:35 +0000759}
760
761template <typename ELFT>
George Rimar47936762016-01-16 00:49:19 +0000762std::string ELFDumper<ELFT>::getFullSymbolName(const Elf_Sym *Symbol,
763 StringRef StrTable,
Hemant Kulkarnic030f232016-03-15 17:25:31 +0000764 bool IsDynamic) const {
James Hendersone50d9cb2019-01-17 15:34:12 +0000765 std::string SymbolName =
766 maybeDemangle(unwrapOrError(Symbol->getName(StrTable)));
Matt Davis8a6f11f2019-03-01 17:31:32 +0000767
768 if (SymbolName.empty() && Symbol->getType() == ELF::STT_SECTION) {
769 unsigned SectionIndex;
770 StringRef SectionName;
771 Elf_Sym_Range Syms =
772 unwrapOrError(ObjF->getELFFile()->symbols(DotSymtabSec));
773 getSectionNameIndex(Symbol, Syms.begin(), SectionName, SectionIndex);
774 return SectionName;
775 }
776
George Rimar47936762016-01-16 00:49:19 +0000777 if (!IsDynamic)
778 return SymbolName;
779
George Rimar47936762016-01-16 00:49:19 +0000780 bool IsDefault;
781 StringRef Version = getSymbolVersion(StrTable, &*Symbol, IsDefault);
James Henderson6135b0f82019-01-08 10:58:05 +0000782 if (!Version.empty()) {
James Hendersone50d9cb2019-01-17 15:34:12 +0000783 SymbolName += (IsDefault ? "@@" : "@");
784 SymbolName += Version;
James Henderson6135b0f82019-01-08 10:58:05 +0000785 }
James Hendersone50d9cb2019-01-17 15:34:12 +0000786 return SymbolName;
George Rimar47936762016-01-16 00:49:19 +0000787}
788
Rafael Espindola714c2952016-11-03 14:41:17 +0000789template <typename ELFT>
Simon Atanasyan62d32592017-12-21 10:26:02 +0000790void ELFDumper<ELFT>::getSectionNameIndex(const Elf_Sym *Symbol,
791 const Elf_Sym *FirstSym,
792 StringRef &SectionName,
793 unsigned &SectionIndex) const {
George Rimar47936762016-01-16 00:49:19 +0000794 SectionIndex = Symbol->st_shndx;
795 if (Symbol->isUndefined())
796 SectionName = "Undefined";
797 else if (Symbol->isProcessorSpecific())
798 SectionName = "Processor Specific";
799 else if (Symbol->isOSSpecific())
800 SectionName = "Operating System Specific";
801 else if (Symbol->isAbsolute())
802 SectionName = "Absolute";
803 else if (Symbol->isCommon())
804 SectionName = "Common";
805 else if (Symbol->isReserved() && SectionIndex != SHN_XINDEX)
806 SectionName = "Reserved";
807 else {
808 if (SectionIndex == SHN_XINDEX)
Rafael Espindola714c2952016-11-03 14:41:17 +0000809 SectionIndex = unwrapOrError(object::getExtendedSymbolTableIndex<ELFT>(
810 Symbol, FirstSym, ShndxTable));
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000811 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
Rafael Espindola714c2952016-11-03 14:41:17 +0000812 const typename ELFT::Shdr *Sec =
Simon Atanasyan62d32592017-12-21 10:26:02 +0000813 unwrapOrError(Obj->getSection(SectionIndex));
814 SectionName = unwrapOrError(Obj->getSectionName(Sec));
George Rimar47936762016-01-16 00:49:19 +0000815 }
816}
817
818template <class ELFO>
Simon Atanasyancb1175c2016-02-09 18:45:35 +0000819static const typename ELFO::Elf_Shdr *
820findNotEmptySectionByAddress(const ELFO *Obj, uint64_t Addr) {
Rafael Espindola25be8c82016-11-02 14:10:57 +0000821 for (const auto &Shdr : unwrapOrError(Obj->sections()))
Simon Atanasyancb1175c2016-02-09 18:45:35 +0000822 if (Shdr.sh_addr == Addr && Shdr.sh_size > 0)
George Rimar47936762016-01-16 00:49:19 +0000823 return &Shdr;
824 return nullptr;
825}
826
827template <class ELFO>
828static const typename ELFO::Elf_Shdr *findSectionByName(const ELFO &Obj,
829 StringRef Name) {
Rafael Espindola25be8c82016-11-02 14:10:57 +0000830 for (const auto &Shdr : unwrapOrError(Obj.sections())) {
Rafael Espindolaf04f1842016-02-17 16:21:49 +0000831 if (Name == unwrapOrError(Obj.getSectionName(&Shdr)))
George Rimar47936762016-01-16 00:49:19 +0000832 return &Shdr;
833 }
834 return nullptr;
835}
836
837static const EnumEntry<unsigned> ElfClass[] = {
George Rimarec895f12019-05-16 06:22:51 +0000838 {"None", "none", ELF::ELFCLASSNONE},
839 {"32-bit", "ELF32", ELF::ELFCLASS32},
840 {"64-bit", "ELF64", ELF::ELFCLASS64},
George Rimar47936762016-01-16 00:49:19 +0000841};
842
843static const EnumEntry<unsigned> ElfDataEncoding[] = {
George Rimarec895f12019-05-16 06:22:51 +0000844 {"None", "none", ELF::ELFDATANONE},
845 {"LittleEndian", "2's complement, little endian", ELF::ELFDATA2LSB},
846 {"BigEndian", "2's complement, big endian", ELF::ELFDATA2MSB},
George Rimar47936762016-01-16 00:49:19 +0000847};
848
849static const EnumEntry<unsigned> ElfObjectFileType[] = {
George Rimarec895f12019-05-16 06:22:51 +0000850 {"None", "NONE (none)", ELF::ET_NONE},
851 {"Relocatable", "REL (Relocatable file)", ELF::ET_REL},
852 {"Executable", "EXEC (Executable file)", ELF::ET_EXEC},
853 {"SharedObject", "DYN (Shared object file)", ELF::ET_DYN},
854 {"Core", "CORE (Core file)", ELF::ET_CORE},
George Rimar47936762016-01-16 00:49:19 +0000855};
856
857static const EnumEntry<unsigned> ElfOSABI[] = {
George Rimarec895f12019-05-16 06:22:51 +0000858 {"SystemV", "UNIX - System V", ELF::ELFOSABI_NONE},
859 {"HPUX", "UNIX - HP-UX", ELF::ELFOSABI_HPUX},
860 {"NetBSD", "UNIX - NetBSD", ELF::ELFOSABI_NETBSD},
861 {"GNU/Linux", "UNIX - GNU", ELF::ELFOSABI_LINUX},
862 {"GNU/Hurd", "GNU/Hurd", ELF::ELFOSABI_HURD},
863 {"Solaris", "UNIX - Solaris", ELF::ELFOSABI_SOLARIS},
864 {"AIX", "UNIX - AIX", ELF::ELFOSABI_AIX},
865 {"IRIX", "UNIX - IRIX", ELF::ELFOSABI_IRIX},
866 {"FreeBSD", "UNIX - FreeBSD", ELF::ELFOSABI_FREEBSD},
867 {"TRU64", "UNIX - TRU64", ELF::ELFOSABI_TRU64},
868 {"Modesto", "Novell - Modesto", ELF::ELFOSABI_MODESTO},
869 {"OpenBSD", "UNIX - OpenBSD", ELF::ELFOSABI_OPENBSD},
870 {"OpenVMS", "VMS - OpenVMS", ELF::ELFOSABI_OPENVMS},
871 {"NSK", "HP - Non-Stop Kernel", ELF::ELFOSABI_NSK},
872 {"AROS", "AROS", ELF::ELFOSABI_AROS},
873 {"FenixOS", "FenixOS", ELF::ELFOSABI_FENIXOS},
874 {"CloudABI", "CloudABI", ELF::ELFOSABI_CLOUDABI},
875 {"Standalone", "Standalone App", ELF::ELFOSABI_STANDALONE}
876};
George Rimar47936762016-01-16 00:49:19 +0000877
Xing GUOea16be12019-03-25 11:02:49 +0000878static const EnumEntry<unsigned> SymVersionFlags[] = {
879 {"Base", "BASE", VER_FLG_BASE},
880 {"Weak", "WEAK", VER_FLG_WEAK},
881 {"Info", "INFO", VER_FLG_INFO}};
882
Konstantin Zhuravlyov121125f2017-10-02 20:49:58 +0000883static const EnumEntry<unsigned> AMDGPUElfOSABI[] = {
George Rimarec895f12019-05-16 06:22:51 +0000884 {"AMDGPU_HSA", "AMDGPU - HSA", ELF::ELFOSABI_AMDGPU_HSA},
885 {"AMDGPU_PAL", "AMDGPU - PAL", ELF::ELFOSABI_AMDGPU_PAL},
886 {"AMDGPU_MESA3D", "AMDGPU - MESA3D", ELF::ELFOSABI_AMDGPU_MESA3D}
887};
Konstantin Zhuravlyov121125f2017-10-02 20:49:58 +0000888
889static const EnumEntry<unsigned> ARMElfOSABI[] = {
George Rimarec895f12019-05-16 06:22:51 +0000890 {"ARM", "ARM", ELF::ELFOSABI_ARM}
891};
Konstantin Zhuravlyov121125f2017-10-02 20:49:58 +0000892
893static const EnumEntry<unsigned> C6000ElfOSABI[] = {
George Rimarec895f12019-05-16 06:22:51 +0000894 {"C6000_ELFABI", "Bare-metal C6000", ELF::ELFOSABI_C6000_ELFABI},
895 {"C6000_LINUX", "Linux C6000", ELF::ELFOSABI_C6000_LINUX}
896};
Konstantin Zhuravlyov121125f2017-10-02 20:49:58 +0000897
George Rimar47936762016-01-16 00:49:19 +0000898static const EnumEntry<unsigned> ElfMachineType[] = {
George Rimarec895f12019-05-16 06:22:51 +0000899 ENUM_ENT(EM_NONE, "None"),
900 ENUM_ENT(EM_M32, "WE32100"),
901 ENUM_ENT(EM_SPARC, "Sparc"),
902 ENUM_ENT(EM_386, "Intel 80386"),
903 ENUM_ENT(EM_68K, "MC68000"),
904 ENUM_ENT(EM_88K, "MC88000"),
905 ENUM_ENT(EM_IAMCU, "EM_IAMCU"),
906 ENUM_ENT(EM_860, "Intel 80860"),
907 ENUM_ENT(EM_MIPS, "MIPS R3000"),
908 ENUM_ENT(EM_S370, "IBM System/370"),
909 ENUM_ENT(EM_MIPS_RS3_LE, "MIPS R3000 little-endian"),
910 ENUM_ENT(EM_PARISC, "HPPA"),
911 ENUM_ENT(EM_VPP500, "Fujitsu VPP500"),
912 ENUM_ENT(EM_SPARC32PLUS, "Sparc v8+"),
913 ENUM_ENT(EM_960, "Intel 80960"),
914 ENUM_ENT(EM_PPC, "PowerPC"),
915 ENUM_ENT(EM_PPC64, "PowerPC64"),
916 ENUM_ENT(EM_S390, "IBM S/390"),
917 ENUM_ENT(EM_SPU, "SPU"),
918 ENUM_ENT(EM_V800, "NEC V800 series"),
919 ENUM_ENT(EM_FR20, "Fujistsu FR20"),
920 ENUM_ENT(EM_RH32, "TRW RH-32"),
921 ENUM_ENT(EM_RCE, "Motorola RCE"),
922 ENUM_ENT(EM_ARM, "ARM"),
923 ENUM_ENT(EM_ALPHA, "EM_ALPHA"),
924 ENUM_ENT(EM_SH, "Hitachi SH"),
925 ENUM_ENT(EM_SPARCV9, "Sparc v9"),
926 ENUM_ENT(EM_TRICORE, "Siemens Tricore"),
927 ENUM_ENT(EM_ARC, "ARC"),
928 ENUM_ENT(EM_H8_300, "Hitachi H8/300"),
929 ENUM_ENT(EM_H8_300H, "Hitachi H8/300H"),
930 ENUM_ENT(EM_H8S, "Hitachi H8S"),
931 ENUM_ENT(EM_H8_500, "Hitachi H8/500"),
932 ENUM_ENT(EM_IA_64, "Intel IA-64"),
933 ENUM_ENT(EM_MIPS_X, "Stanford MIPS-X"),
934 ENUM_ENT(EM_COLDFIRE, "Motorola Coldfire"),
935 ENUM_ENT(EM_68HC12, "Motorola MC68HC12 Microcontroller"),
936 ENUM_ENT(EM_MMA, "Fujitsu Multimedia Accelerator"),
937 ENUM_ENT(EM_PCP, "Siemens PCP"),
938 ENUM_ENT(EM_NCPU, "Sony nCPU embedded RISC processor"),
939 ENUM_ENT(EM_NDR1, "Denso NDR1 microprocesspr"),
940 ENUM_ENT(EM_STARCORE, "Motorola Star*Core processor"),
941 ENUM_ENT(EM_ME16, "Toyota ME16 processor"),
942 ENUM_ENT(EM_ST100, "STMicroelectronics ST100 processor"),
943 ENUM_ENT(EM_TINYJ, "Advanced Logic Corp. TinyJ embedded processor"),
944 ENUM_ENT(EM_X86_64, "Advanced Micro Devices X86-64"),
945 ENUM_ENT(EM_PDSP, "Sony DSP processor"),
946 ENUM_ENT(EM_PDP10, "Digital Equipment Corp. PDP-10"),
947 ENUM_ENT(EM_PDP11, "Digital Equipment Corp. PDP-11"),
948 ENUM_ENT(EM_FX66, "Siemens FX66 microcontroller"),
949 ENUM_ENT(EM_ST9PLUS, "STMicroelectronics ST9+ 8/16 bit microcontroller"),
950 ENUM_ENT(EM_ST7, "STMicroelectronics ST7 8-bit microcontroller"),
951 ENUM_ENT(EM_68HC16, "Motorola MC68HC16 Microcontroller"),
952 ENUM_ENT(EM_68HC11, "Motorola MC68HC11 Microcontroller"),
953 ENUM_ENT(EM_68HC08, "Motorola MC68HC08 Microcontroller"),
954 ENUM_ENT(EM_68HC05, "Motorola MC68HC05 Microcontroller"),
955 ENUM_ENT(EM_SVX, "Silicon Graphics SVx"),
956 ENUM_ENT(EM_ST19, "STMicroelectronics ST19 8-bit microcontroller"),
957 ENUM_ENT(EM_VAX, "Digital VAX"),
958 ENUM_ENT(EM_CRIS, "Axis Communications 32-bit embedded processor"),
959 ENUM_ENT(EM_JAVELIN, "Infineon Technologies 32-bit embedded cpu"),
960 ENUM_ENT(EM_FIREPATH, "Element 14 64-bit DSP processor"),
961 ENUM_ENT(EM_ZSP, "LSI Logic's 16-bit DSP processor"),
962 ENUM_ENT(EM_MMIX, "Donald Knuth's educational 64-bit processor"),
963 ENUM_ENT(EM_HUANY, "Harvard Universitys's machine-independent object format"),
964 ENUM_ENT(EM_PRISM, "Vitesse Prism"),
965 ENUM_ENT(EM_AVR, "Atmel AVR 8-bit microcontroller"),
966 ENUM_ENT(EM_FR30, "Fujitsu FR30"),
967 ENUM_ENT(EM_D10V, "Mitsubishi D10V"),
968 ENUM_ENT(EM_D30V, "Mitsubishi D30V"),
969 ENUM_ENT(EM_V850, "NEC v850"),
970 ENUM_ENT(EM_M32R, "Renesas M32R (formerly Mitsubishi M32r)"),
971 ENUM_ENT(EM_MN10300, "Matsushita MN10300"),
972 ENUM_ENT(EM_MN10200, "Matsushita MN10200"),
973 ENUM_ENT(EM_PJ, "picoJava"),
974 ENUM_ENT(EM_OPENRISC, "OpenRISC 32-bit embedded processor"),
975 ENUM_ENT(EM_ARC_COMPACT, "EM_ARC_COMPACT"),
976 ENUM_ENT(EM_XTENSA, "Tensilica Xtensa Processor"),
977 ENUM_ENT(EM_VIDEOCORE, "Alphamosaic VideoCore processor"),
978 ENUM_ENT(EM_TMM_GPP, "Thompson Multimedia General Purpose Processor"),
979 ENUM_ENT(EM_NS32K, "National Semiconductor 32000 series"),
980 ENUM_ENT(EM_TPC, "Tenor Network TPC processor"),
981 ENUM_ENT(EM_SNP1K, "EM_SNP1K"),
982 ENUM_ENT(EM_ST200, "STMicroelectronics ST200 microcontroller"),
983 ENUM_ENT(EM_IP2K, "Ubicom IP2xxx 8-bit microcontrollers"),
984 ENUM_ENT(EM_MAX, "MAX Processor"),
985 ENUM_ENT(EM_CR, "National Semiconductor CompactRISC"),
986 ENUM_ENT(EM_F2MC16, "Fujitsu F2MC16"),
987 ENUM_ENT(EM_MSP430, "Texas Instruments msp430 microcontroller"),
988 ENUM_ENT(EM_BLACKFIN, "Analog Devices Blackfin"),
989 ENUM_ENT(EM_SE_C33, "S1C33 Family of Seiko Epson processors"),
990 ENUM_ENT(EM_SEP, "Sharp embedded microprocessor"),
991 ENUM_ENT(EM_ARCA, "Arca RISC microprocessor"),
992 ENUM_ENT(EM_UNICORE, "Unicore"),
993 ENUM_ENT(EM_EXCESS, "eXcess 16/32/64-bit configurable embedded CPU"),
994 ENUM_ENT(EM_DXP, "Icera Semiconductor Inc. Deep Execution Processor"),
995 ENUM_ENT(EM_ALTERA_NIOS2, "Altera Nios"),
996 ENUM_ENT(EM_CRX, "National Semiconductor CRX microprocessor"),
997 ENUM_ENT(EM_XGATE, "Motorola XGATE embedded processor"),
998 ENUM_ENT(EM_C166, "Infineon Technologies xc16x"),
999 ENUM_ENT(EM_M16C, "Renesas M16C"),
1000 ENUM_ENT(EM_DSPIC30F, "Microchip Technology dsPIC30F Digital Signal Controller"),
1001 ENUM_ENT(EM_CE, "Freescale Communication Engine RISC core"),
1002 ENUM_ENT(EM_M32C, "Renesas M32C"),
1003 ENUM_ENT(EM_TSK3000, "Altium TSK3000 core"),
1004 ENUM_ENT(EM_RS08, "Freescale RS08 embedded processor"),
1005 ENUM_ENT(EM_SHARC, "EM_SHARC"),
1006 ENUM_ENT(EM_ECOG2, "Cyan Technology eCOG2 microprocessor"),
1007 ENUM_ENT(EM_SCORE7, "SUNPLUS S+Core"),
1008 ENUM_ENT(EM_DSP24, "New Japan Radio (NJR) 24-bit DSP Processor"),
1009 ENUM_ENT(EM_VIDEOCORE3, "Broadcom VideoCore III processor"),
1010 ENUM_ENT(EM_LATTICEMICO32, "Lattice Mico32"),
1011 ENUM_ENT(EM_SE_C17, "Seiko Epson C17 family"),
1012 ENUM_ENT(EM_TI_C6000, "Texas Instruments TMS320C6000 DSP family"),
1013 ENUM_ENT(EM_TI_C2000, "Texas Instruments TMS320C2000 DSP family"),
1014 ENUM_ENT(EM_TI_C5500, "Texas Instruments TMS320C55x DSP family"),
1015 ENUM_ENT(EM_MMDSP_PLUS, "STMicroelectronics 64bit VLIW Data Signal Processor"),
1016 ENUM_ENT(EM_CYPRESS_M8C, "Cypress M8C microprocessor"),
1017 ENUM_ENT(EM_R32C, "Renesas R32C series microprocessors"),
1018 ENUM_ENT(EM_TRIMEDIA, "NXP Semiconductors TriMedia architecture family"),
1019 ENUM_ENT(EM_HEXAGON, "Qualcomm Hexagon"),
1020 ENUM_ENT(EM_8051, "Intel 8051 and variants"),
1021 ENUM_ENT(EM_STXP7X, "STMicroelectronics STxP7x family"),
1022 ENUM_ENT(EM_NDS32, "Andes Technology compact code size embedded RISC processor family"),
1023 ENUM_ENT(EM_ECOG1, "Cyan Technology eCOG1 microprocessor"),
1024 ENUM_ENT(EM_ECOG1X, "Cyan Technology eCOG1X family"),
1025 ENUM_ENT(EM_MAXQ30, "Dallas Semiconductor MAXQ30 Core microcontrollers"),
1026 ENUM_ENT(EM_XIMO16, "New Japan Radio (NJR) 16-bit DSP Processor"),
1027 ENUM_ENT(EM_MANIK, "M2000 Reconfigurable RISC Microprocessor"),
1028 ENUM_ENT(EM_CRAYNV2, "Cray Inc. NV2 vector architecture"),
1029 ENUM_ENT(EM_RX, "Renesas RX"),
1030 ENUM_ENT(EM_METAG, "Imagination Technologies Meta processor architecture"),
1031 ENUM_ENT(EM_MCST_ELBRUS, "MCST Elbrus general purpose hardware architecture"),
1032 ENUM_ENT(EM_ECOG16, "Cyan Technology eCOG16 family"),
1033 ENUM_ENT(EM_CR16, "Xilinx MicroBlaze"),
1034 ENUM_ENT(EM_ETPU, "Freescale Extended Time Processing Unit"),
1035 ENUM_ENT(EM_SLE9X, "Infineon Technologies SLE9X core"),
1036 ENUM_ENT(EM_L10M, "EM_L10M"),
1037 ENUM_ENT(EM_K10M, "EM_K10M"),
1038 ENUM_ENT(EM_AARCH64, "AArch64"),
1039 ENUM_ENT(EM_AVR32, "Atmel Corporation 32-bit microprocessor family"),
1040 ENUM_ENT(EM_STM8, "STMicroeletronics STM8 8-bit microcontroller"),
1041 ENUM_ENT(EM_TILE64, "Tilera TILE64 multicore architecture family"),
1042 ENUM_ENT(EM_TILEPRO, "Tilera TILEPro multicore architecture family"),
1043 ENUM_ENT(EM_CUDA, "NVIDIA CUDA architecture"),
1044 ENUM_ENT(EM_TILEGX, "Tilera TILE-Gx multicore architecture family"),
1045 ENUM_ENT(EM_CLOUDSHIELD, "EM_CLOUDSHIELD"),
1046 ENUM_ENT(EM_COREA_1ST, "EM_COREA_1ST"),
1047 ENUM_ENT(EM_COREA_2ND, "EM_COREA_2ND"),
1048 ENUM_ENT(EM_ARC_COMPACT2, "EM_ARC_COMPACT2"),
1049 ENUM_ENT(EM_OPEN8, "EM_OPEN8"),
1050 ENUM_ENT(EM_RL78, "Renesas RL78"),
1051 ENUM_ENT(EM_VIDEOCORE5, "Broadcom VideoCore V processor"),
1052 ENUM_ENT(EM_78KOR, "EM_78KOR"),
1053 ENUM_ENT(EM_56800EX, "EM_56800EX"),
1054 ENUM_ENT(EM_AMDGPU, "EM_AMDGPU"),
1055 ENUM_ENT(EM_RISCV, "RISC-V"),
1056 ENUM_ENT(EM_LANAI, "EM_LANAI"),
1057 ENUM_ENT(EM_BPF, "EM_BPF"),
George Rimar47936762016-01-16 00:49:19 +00001058};
1059
1060static const EnumEntry<unsigned> ElfSymbolBindings[] = {
George Rimarec895f12019-05-16 06:22:51 +00001061 {"Local", "LOCAL", ELF::STB_LOCAL},
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00001062 {"Global", "GLOBAL", ELF::STB_GLOBAL},
George Rimarec895f12019-05-16 06:22:51 +00001063 {"Weak", "WEAK", ELF::STB_WEAK},
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00001064 {"Unique", "UNIQUE", ELF::STB_GNU_UNIQUE}};
George Rimar47936762016-01-16 00:49:19 +00001065
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +00001066static const EnumEntry<unsigned> ElfSymbolVisibilities[] = {
George Rimarec895f12019-05-16 06:22:51 +00001067 {"DEFAULT", "DEFAULT", ELF::STV_DEFAULT},
1068 {"INTERNAL", "INTERNAL", ELF::STV_INTERNAL},
1069 {"HIDDEN", "HIDDEN", ELF::STV_HIDDEN},
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +00001070 {"PROTECTED", "PROTECTED", ELF::STV_PROTECTED}};
1071
George Rimar47936762016-01-16 00:49:19 +00001072static const EnumEntry<unsigned> AMDGPUSymbolTypes[] = {
George Rimarec895f12019-05-16 06:22:51 +00001073 { "AMDGPU_HSA_KERNEL", ELF::STT_AMDGPU_HSA_KERNEL }
1074};
George Rimar47936762016-01-16 00:49:19 +00001075
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +00001076static const char *getGroupType(uint32_t Flag) {
1077 if (Flag & ELF::GRP_COMDAT)
1078 return "COMDAT";
1079 else
1080 return "(unknown)";
1081}
1082
George Rimar47936762016-01-16 00:49:19 +00001083static const EnumEntry<unsigned> ElfSectionFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001084 ENUM_ENT(SHF_WRITE, "W"),
1085 ENUM_ENT(SHF_ALLOC, "A"),
1086 ENUM_ENT(SHF_EXCLUDE, "E"),
1087 ENUM_ENT(SHF_EXECINSTR, "X"),
1088 ENUM_ENT(SHF_MERGE, "M"),
1089 ENUM_ENT(SHF_STRINGS, "S"),
1090 ENUM_ENT(SHF_INFO_LINK, "I"),
1091 ENUM_ENT(SHF_LINK_ORDER, "L"),
1092 ENUM_ENT(SHF_OS_NONCONFORMING, "o"),
1093 ENUM_ENT(SHF_GROUP, "G"),
1094 ENUM_ENT(SHF_TLS, "T"),
1095 ENUM_ENT(SHF_MASKOS, "o"),
1096 ENUM_ENT(SHF_MASKPROC, "p"),
1097 ENUM_ENT_1(SHF_COMPRESSED),
George Rimarc13c59a2016-05-21 10:16:58 +00001098};
1099
1100static const EnumEntry<unsigned> ElfXCoreSectionFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001101 LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_CP_SECTION),
1102 LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_DP_SECTION)
1103};
Simon Atanasyan2d0d8532016-01-20 19:15:18 +00001104
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +00001105static const EnumEntry<unsigned> ElfARMSectionFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001106 LLVM_READOBJ_ENUM_ENT(ELF, SHF_ARM_PURECODE)
1107};
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +00001108
Simon Atanasyan2d0d8532016-01-20 19:15:18 +00001109static const EnumEntry<unsigned> ElfHexagonSectionFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001110 LLVM_READOBJ_ENUM_ENT(ELF, SHF_HEX_GPREL)
1111};
Simon Atanasyan2d0d8532016-01-20 19:15:18 +00001112
1113static const EnumEntry<unsigned> ElfMipsSectionFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001114 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NODUPES),
1115 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NAMES ),
1116 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_LOCAL ),
1117 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NOSTRIP),
1118 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_GPREL ),
1119 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_MERGE ),
1120 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_ADDR ),
1121 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_STRING )
1122};
Simon Atanasyan2d0d8532016-01-20 19:15:18 +00001123
1124static const EnumEntry<unsigned> ElfX86_64SectionFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001125 LLVM_READOBJ_ENUM_ENT(ELF, SHF_X86_64_LARGE)
1126};
Simon Atanasyan2d0d8532016-01-20 19:15:18 +00001127
Hemant Kulkarnic030f232016-03-15 17:25:31 +00001128static std::string getGNUFlags(uint64_t Flags) {
1129 std::string Str;
1130 for (auto Entry : ElfSectionFlags) {
1131 uint64_t Flag = Entry.Value & Flags;
1132 Flags &= ~Entry.Value;
1133 switch (Flag) {
1134 case ELF::SHF_WRITE:
1135 case ELF::SHF_ALLOC:
1136 case ELF::SHF_EXECINSTR:
1137 case ELF::SHF_MERGE:
1138 case ELF::SHF_STRINGS:
1139 case ELF::SHF_INFO_LINK:
1140 case ELF::SHF_LINK_ORDER:
1141 case ELF::SHF_OS_NONCONFORMING:
1142 case ELF::SHF_GROUP:
1143 case ELF::SHF_TLS:
1144 case ELF::SHF_EXCLUDE:
1145 Str += Entry.AltName;
1146 break;
1147 default:
Hemant Kulkarni04ac3d72016-05-12 19:58:52 +00001148 if (Flag & ELF::SHF_MASKOS)
Hemant Kulkarnic030f232016-03-15 17:25:31 +00001149 Str += "o";
Hemant Kulkarni04ac3d72016-05-12 19:58:52 +00001150 else if (Flag & ELF::SHF_MASKPROC)
Hemant Kulkarnic030f232016-03-15 17:25:31 +00001151 Str += "p";
1152 else if (Flag)
1153 Str += "x";
1154 }
1155 }
1156 return Str;
1157}
1158
George Rimar47936762016-01-16 00:49:19 +00001159static const char *getElfSegmentType(unsigned Arch, unsigned Type) {
1160 // Check potentially overlapped processor-specific
1161 // program header type.
1162 switch (Arch) {
George Rimar47936762016-01-16 00:49:19 +00001163 case ELF::EM_ARM:
George Rimar9e88a262019-05-14 14:22:44 +00001164 switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, PT_ARM_EXIDX); }
Ryan Prichard0c20b5b2018-10-26 23:01:54 +00001165 break;
George Rimar47936762016-01-16 00:49:19 +00001166 case ELF::EM_MIPS:
1167 case ELF::EM_MIPS_RS3_LE:
1168 switch (Type) {
George Rimar9e88a262019-05-14 14:22:44 +00001169 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_REGINFO);
George Rimarec895f12019-05-16 06:22:51 +00001170 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_RTPROC);
1171 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_OPTIONS);
1172 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_ABIFLAGS);
George Rimar47936762016-01-16 00:49:19 +00001173 }
Ryan Prichard0c20b5b2018-10-26 23:01:54 +00001174 break;
George Rimar47936762016-01-16 00:49:19 +00001175 }
1176
1177 switch (Type) {
George Rimarec895f12019-05-16 06:22:51 +00001178 LLVM_READOBJ_ENUM_CASE(ELF, PT_NULL );
1179 LLVM_READOBJ_ENUM_CASE(ELF, PT_LOAD );
1180 LLVM_READOBJ_ENUM_CASE(ELF, PT_DYNAMIC);
1181 LLVM_READOBJ_ENUM_CASE(ELF, PT_INTERP );
1182 LLVM_READOBJ_ENUM_CASE(ELF, PT_NOTE );
1183 LLVM_READOBJ_ENUM_CASE(ELF, PT_SHLIB );
1184 LLVM_READOBJ_ENUM_CASE(ELF, PT_PHDR );
1185 LLVM_READOBJ_ENUM_CASE(ELF, PT_TLS );
George Rimar47936762016-01-16 00:49:19 +00001186
George Rimarec895f12019-05-16 06:22:51 +00001187 LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_EH_FRAME);
1188 LLVM_READOBJ_ENUM_CASE(ELF, PT_SUNW_UNWIND);
George Rimar47936762016-01-16 00:49:19 +00001189
George Rimar9e88a262019-05-14 14:22:44 +00001190 LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_STACK);
1191 LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_RELRO);
George Rimarbcfcb9e2016-10-18 10:54:56 +00001192
George Rimar9e88a262019-05-14 14:22:44 +00001193 LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_RANDOMIZE);
1194 LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_WXNEEDED);
1195 LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_BOOTDATA);
George Rimarbcfcb9e2016-10-18 10:54:56 +00001196
George Rimar9e88a262019-05-14 14:22:44 +00001197 default:
1198 return "";
George Rimar47936762016-01-16 00:49:19 +00001199 }
1200}
1201
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00001202static std::string getElfPtType(unsigned Arch, unsigned Type) {
1203 switch (Type) {
Hemant Kulkarni7d564ba2016-03-28 17:20:23 +00001204 LLVM_READOBJ_PHDR_ENUM(ELF, PT_NULL)
1205 LLVM_READOBJ_PHDR_ENUM(ELF, PT_LOAD)
1206 LLVM_READOBJ_PHDR_ENUM(ELF, PT_DYNAMIC)
1207 LLVM_READOBJ_PHDR_ENUM(ELF, PT_INTERP)
1208 LLVM_READOBJ_PHDR_ENUM(ELF, PT_NOTE)
1209 LLVM_READOBJ_PHDR_ENUM(ELF, PT_SHLIB)
1210 LLVM_READOBJ_PHDR_ENUM(ELF, PT_PHDR)
1211 LLVM_READOBJ_PHDR_ENUM(ELF, PT_TLS)
1212 LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_EH_FRAME)
1213 LLVM_READOBJ_PHDR_ENUM(ELF, PT_SUNW_UNWIND)
1214 LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_STACK)
1215 LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_RELRO)
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00001216 default:
1217 // All machine specific PT_* types
1218 switch (Arch) {
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00001219 case ELF::EM_ARM:
1220 if (Type == ELF::PT_ARM_EXIDX)
1221 return "EXIDX";
Ryan Prichard0c20b5b2018-10-26 23:01:54 +00001222 break;
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00001223 case ELF::EM_MIPS:
1224 case ELF::EM_MIPS_RS3_LE:
1225 switch (Type) {
1226 case PT_MIPS_REGINFO:
1227 return "REGINFO";
1228 case PT_MIPS_RTPROC:
1229 return "RTPROC";
1230 case PT_MIPS_OPTIONS:
1231 return "OPTIONS";
1232 case PT_MIPS_ABIFLAGS:
1233 return "ABIFLAGS";
1234 }
Ryan Prichard0c20b5b2018-10-26 23:01:54 +00001235 break;
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00001236 }
1237 }
1238 return std::string("<unknown>: ") + to_string(format_hex(Type, 1));
1239}
1240
George Rimar47936762016-01-16 00:49:19 +00001241static const EnumEntry<unsigned> ElfSegmentFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001242 LLVM_READOBJ_ENUM_ENT(ELF, PF_X),
1243 LLVM_READOBJ_ENUM_ENT(ELF, PF_W),
1244 LLVM_READOBJ_ENUM_ENT(ELF, PF_R)
1245};
George Rimar47936762016-01-16 00:49:19 +00001246
1247static const EnumEntry<unsigned> ElfHeaderMipsFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001248 ENUM_ENT(EF_MIPS_NOREORDER, "noreorder"),
1249 ENUM_ENT(EF_MIPS_PIC, "pic"),
1250 ENUM_ENT(EF_MIPS_CPIC, "cpic"),
1251 ENUM_ENT(EF_MIPS_ABI2, "abi2"),
1252 ENUM_ENT(EF_MIPS_32BITMODE, "32bitmode"),
1253 ENUM_ENT(EF_MIPS_FP64, "fp64"),
1254 ENUM_ENT(EF_MIPS_NAN2008, "nan2008"),
1255 ENUM_ENT(EF_MIPS_ABI_O32, "o32"),
1256 ENUM_ENT(EF_MIPS_ABI_O64, "o64"),
1257 ENUM_ENT(EF_MIPS_ABI_EABI32, "eabi32"),
1258 ENUM_ENT(EF_MIPS_ABI_EABI64, "eabi64"),
1259 ENUM_ENT(EF_MIPS_MACH_3900, "3900"),
1260 ENUM_ENT(EF_MIPS_MACH_4010, "4010"),
1261 ENUM_ENT(EF_MIPS_MACH_4100, "4100"),
1262 ENUM_ENT(EF_MIPS_MACH_4650, "4650"),
1263 ENUM_ENT(EF_MIPS_MACH_4120, "4120"),
1264 ENUM_ENT(EF_MIPS_MACH_4111, "4111"),
1265 ENUM_ENT(EF_MIPS_MACH_SB1, "sb1"),
1266 ENUM_ENT(EF_MIPS_MACH_OCTEON, "octeon"),
1267 ENUM_ENT(EF_MIPS_MACH_XLR, "xlr"),
1268 ENUM_ENT(EF_MIPS_MACH_OCTEON2, "octeon2"),
1269 ENUM_ENT(EF_MIPS_MACH_OCTEON3, "octeon3"),
1270 ENUM_ENT(EF_MIPS_MACH_5400, "5400"),
1271 ENUM_ENT(EF_MIPS_MACH_5900, "5900"),
1272 ENUM_ENT(EF_MIPS_MACH_5500, "5500"),
1273 ENUM_ENT(EF_MIPS_MACH_9000, "9000"),
1274 ENUM_ENT(EF_MIPS_MACH_LS2E, "loongson-2e"),
1275 ENUM_ENT(EF_MIPS_MACH_LS2F, "loongson-2f"),
1276 ENUM_ENT(EF_MIPS_MACH_LS3A, "loongson-3a"),
1277 ENUM_ENT(EF_MIPS_MICROMIPS, "micromips"),
1278 ENUM_ENT(EF_MIPS_ARCH_ASE_M16, "mips16"),
1279 ENUM_ENT(EF_MIPS_ARCH_ASE_MDMX, "mdmx"),
1280 ENUM_ENT(EF_MIPS_ARCH_1, "mips1"),
1281 ENUM_ENT(EF_MIPS_ARCH_2, "mips2"),
1282 ENUM_ENT(EF_MIPS_ARCH_3, "mips3"),
1283 ENUM_ENT(EF_MIPS_ARCH_4, "mips4"),
1284 ENUM_ENT(EF_MIPS_ARCH_5, "mips5"),
1285 ENUM_ENT(EF_MIPS_ARCH_32, "mips32"),
1286 ENUM_ENT(EF_MIPS_ARCH_64, "mips64"),
1287 ENUM_ENT(EF_MIPS_ARCH_32R2, "mips32r2"),
1288 ENUM_ENT(EF_MIPS_ARCH_64R2, "mips64r2"),
1289 ENUM_ENT(EF_MIPS_ARCH_32R6, "mips32r6"),
1290 ENUM_ENT(EF_MIPS_ARCH_64R6, "mips64r6")
1291};
George Rimar47936762016-01-16 00:49:19 +00001292
Konstantin Zhuravlyovaa0835a2017-10-05 16:19:18 +00001293static const EnumEntry<unsigned> ElfHeaderAMDGPUFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001294 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_NONE),
1295 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R600),
1296 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R630),
1297 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RS880),
1298 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV670),
1299 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV710),
1300 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV730),
1301 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV770),
1302 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CEDAR),
1303 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CYPRESS),
1304 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_JUNIPER),
1305 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_REDWOOD),
1306 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_SUMO),
1307 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_BARTS),
1308 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAICOS),
1309 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAYMAN),
1310 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_TURKS),
1311 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX600),
1312 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX601),
1313 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX700),
1314 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX701),
1315 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX702),
1316 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX703),
1317 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX704),
1318 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX801),
1319 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX802),
1320 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX803),
1321 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX810),
1322 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX900),
1323 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX902),
1324 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX904),
1325 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX906),
Stanislav Mekhanoshin22b2c3d2019-07-09 18:10:06 +00001326 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX908),
George Rimarec895f12019-05-16 06:22:51 +00001327 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX909),
1328 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1010),
Stanislav Mekhanoshinc43e67b2019-06-14 00:33:31 +00001329 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1011),
1330 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1012),
George Rimarec895f12019-05-16 06:22:51 +00001331 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_XNACK),
1332 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_SRAM_ECC)
1333};
Konstantin Zhuravlyovaa0835a2017-10-05 16:19:18 +00001334
Alex Bradburybb89b2b2017-10-03 08:41:59 +00001335static const EnumEntry<unsigned> ElfHeaderRISCVFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001336 ENUM_ENT(EF_RISCV_RVC, "RVC"),
1337 ENUM_ENT(EF_RISCV_FLOAT_ABI_SINGLE, "single-float ABI"),
1338 ENUM_ENT(EF_RISCV_FLOAT_ABI_DOUBLE, "double-float ABI"),
1339 ENUM_ENT(EF_RISCV_FLOAT_ABI_QUAD, "quad-float ABI"),
1340 ENUM_ENT(EF_RISCV_RVE, "RVE")
1341};
Alex Bradburybb89b2b2017-10-03 08:41:59 +00001342
Simon Atanasyanb7807a02016-03-24 16:10:37 +00001343static const EnumEntry<unsigned> ElfSymOtherFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001344 LLVM_READOBJ_ENUM_ENT(ELF, STV_INTERNAL),
1345 LLVM_READOBJ_ENUM_ENT(ELF, STV_HIDDEN),
1346 LLVM_READOBJ_ENUM_ENT(ELF, STV_PROTECTED)
1347};
Simon Atanasyanb7807a02016-03-24 16:10:37 +00001348
1349static const EnumEntry<unsigned> ElfMipsSymOtherFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001350 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL),
1351 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT),
1352 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PIC),
1353 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MICROMIPS)
1354};
Simon Atanasyanb7807a02016-03-24 16:10:37 +00001355
1356static const EnumEntry<unsigned> ElfMips16SymOtherFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001357 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL),
1358 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT),
1359 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MIPS16)
1360};
Simon Atanasyanb7807a02016-03-24 16:10:37 +00001361
Simon Atanasyan8a71b532016-05-04 05:58:57 +00001362static const char *getElfMipsOptionsOdkType(unsigned Odk) {
1363 switch (Odk) {
George Rimarec895f12019-05-16 06:22:51 +00001364 LLVM_READOBJ_ENUM_CASE(ELF, ODK_NULL);
1365 LLVM_READOBJ_ENUM_CASE(ELF, ODK_REGINFO);
1366 LLVM_READOBJ_ENUM_CASE(ELF, ODK_EXCEPTIONS);
1367 LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAD);
1368 LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWPATCH);
1369 LLVM_READOBJ_ENUM_CASE(ELF, ODK_FILL);
1370 LLVM_READOBJ_ENUM_CASE(ELF, ODK_TAGS);
1371 LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWAND);
1372 LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWOR);
1373 LLVM_READOBJ_ENUM_CASE(ELF, ODK_GP_GROUP);
1374 LLVM_READOBJ_ENUM_CASE(ELF, ODK_IDENT);
1375 LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAGESIZE);
Simon Atanasyan8a71b532016-05-04 05:58:57 +00001376 default:
1377 return "Unknown";
1378 }
1379}
1380
George Rimar47936762016-01-16 00:49:19 +00001381template <typename ELFT>
George Rimar72f821d2019-05-20 15:41:48 +00001382void ELFDumper<ELFT>::loadDynamicTable(const ELFFile<ELFT> *Obj) {
George Rimar8ac7b2d2019-05-29 10:31:46 +00001383 // Try to locate the PT_DYNAMIC header.
George Rimar72f821d2019-05-20 15:41:48 +00001384 const Elf_Phdr *DynamicPhdr = nullptr;
1385 for (const Elf_Phdr &Phdr : unwrapOrError(Obj->program_headers())) {
1386 if (Phdr.p_type != ELF::PT_DYNAMIC)
1387 continue;
1388 DynamicPhdr = &Phdr;
1389 break;
1390 }
1391
George Rimar72f821d2019-05-20 15:41:48 +00001392 // Try to locate the .dynamic section in the sections header table.
1393 const Elf_Shdr *DynamicSec = nullptr;
1394 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
1395 if (Sec.sh_type != ELF::SHT_DYNAMIC)
1396 continue;
1397 DynamicSec = &Sec;
1398 break;
1399 }
1400
1401 // Information in the section header has priority over the information
1402 // in a PT_DYNAMIC header.
1403 // Ignore sh_entsize and use the expected value for entry size explicitly.
1404 // This allows us to dump the dynamic sections with a broken sh_entsize
1405 // field.
George Rimar8ac7b2d2019-05-29 10:31:46 +00001406 if (DynamicSec) {
George Rimar72f821d2019-05-20 15:41:48 +00001407 DynamicTable = checkDRI({ObjF->getELFFile()->base() + DynamicSec->sh_offset,
1408 DynamicSec->sh_size, sizeof(Elf_Dyn)});
George Rimar8ac7b2d2019-05-29 10:31:46 +00001409 parseDynamicTable();
1410 }
1411
1412 // If we have a PT_DYNAMIC header, we will either check the found dynamic
1413 // section or take the dynamic table data directly from the header.
1414 if (!DynamicPhdr)
1415 return;
George Rimar72f821d2019-05-20 15:41:48 +00001416
1417 if (DynamicPhdr->p_offset + DynamicPhdr->p_filesz >
1418 ObjF->getMemoryBufferRef().getBufferSize())
1419 reportError(
1420 "PT_DYNAMIC segment offset + size exceeds the size of the file");
1421
1422 if (!DynamicSec) {
1423 DynamicTable = createDRIFrom(DynamicPhdr, sizeof(Elf_Dyn));
1424 parseDynamicTable();
1425 return;
1426 }
1427
1428 StringRef Name = unwrapOrError(Obj->getSectionName(DynamicSec));
George Rimar72f821d2019-05-20 15:41:48 +00001429 if (DynamicSec->sh_addr + DynamicSec->sh_size >
1430 DynamicPhdr->p_vaddr + DynamicPhdr->p_memsz ||
1431 DynamicSec->sh_addr < DynamicPhdr->p_vaddr)
1432 reportWarning("The SHT_DYNAMIC section '" + Name +
1433 "' is not contained within the "
1434 "PT_DYNAMIC segment");
1435
1436 if (DynamicSec->sh_addr != DynamicPhdr->p_vaddr)
1437 reportWarning("The SHT_DYNAMIC section '" + Name +
1438 "' is not at the start of "
1439 "PT_DYNAMIC segment");
George Rimar72f821d2019-05-20 15:41:48 +00001440}
1441
1442template <typename ELFT>
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00001443ELFDumper<ELFT>::ELFDumper(const object::ELFObjectFile<ELFT> *ObjF,
George Rimarec895f12019-05-16 06:22:51 +00001444 ScopedPrinter &Writer)
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00001445 : ObjDumper(Writer), ObjF(ObjF) {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00001446 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
George Rimar47936762016-01-16 00:49:19 +00001447
Rafael Espindola25be8c82016-11-02 14:10:57 +00001448 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
Michael J. Spencer37304f12016-02-11 04:59:26 +00001449 switch (Sec.sh_type) {
Michael J. Spencer94f060c2016-02-11 04:59:32 +00001450 case ELF::SHT_SYMTAB:
Peter Collingbourne0dac4762019-05-28 20:01:25 +00001451 if (!DotSymtabSec)
1452 DotSymtabSec = &Sec;
Michael J. Spencer94f060c2016-02-11 04:59:32 +00001453 break;
1454 case ELF::SHT_DYNSYM:
Peter Collingbourne0dac4762019-05-28 20:01:25 +00001455 if (!DynSymRegion.Size) {
1456 DynSymRegion = createDRIFrom(&Sec);
1457 // This is only used (if Elf_Shdr present)for naming section in GNU
1458 // style
1459 DynSymtabName = unwrapOrError(Obj->getSectionName(&Sec));
George Rimardd4f2532019-06-10 14:23:46 +00001460
1461 if (Expected<StringRef> E = Obj->getStringTableForSymtab(Sec))
1462 DynamicStringTable = *E;
1463 else
1464 warn(E.takeError());
Peter Collingbourne0dac4762019-05-28 20:01:25 +00001465 }
Michael J. Spencer94f060c2016-02-11 04:59:32 +00001466 break;
Michael J. Spencer1c793ef2016-02-17 22:30:41 +00001467 case ELF::SHT_SYMTAB_SHNDX:
Rafael Espindolaf04f1842016-02-17 16:21:49 +00001468 ShndxTable = unwrapOrError(Obj->getSHNDXTable(Sec));
Michael J. Spencer94f060c2016-02-11 04:59:32 +00001469 break;
Michael J. Spencer37304f12016-02-11 04:59:26 +00001470 case ELF::SHT_GNU_versym:
Peter Collingbourne0dac4762019-05-28 20:01:25 +00001471 if (!SymbolVersionSection)
1472 SymbolVersionSection = &Sec;
Michael J. Spencer37304f12016-02-11 04:59:26 +00001473 break;
1474 case ELF::SHT_GNU_verdef:
Peter Collingbourne0dac4762019-05-28 20:01:25 +00001475 if (!SymbolVersionDefSection)
1476 SymbolVersionDefSection = &Sec;
Michael J. Spencer37304f12016-02-11 04:59:26 +00001477 break;
1478 case ELF::SHT_GNU_verneed:
Peter Collingbourne0dac4762019-05-28 20:01:25 +00001479 if (!SymbolVersionNeedSection)
1480 SymbolVersionNeedSection = &Sec;
Michael J. Spencer37304f12016-02-11 04:59:26 +00001481 break;
Michael J. Spencerae6eeae2018-06-02 16:33:01 +00001482 case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
Peter Collingbourne0dac4762019-05-28 20:01:25 +00001483 if (!DotCGProfileSec)
1484 DotCGProfileSec = &Sec;
Peter Collingbourne3e227332018-07-17 22:17:18 +00001485 break;
1486 case ELF::SHT_LLVM_ADDRSIG:
Peter Collingbourne0dac4762019-05-28 20:01:25 +00001487 if (!DotAddrsigSec)
1488 DotAddrsigSec = &Sec;
Peter Collingbourne3e227332018-07-17 22:17:18 +00001489 break;
Michael J. Spencer37304f12016-02-11 04:59:26 +00001490 }
1491 }
1492
George Rimar72f821d2019-05-20 15:41:48 +00001493 loadDynamicTable(Obj);
Michael J. Spencer60d82b22016-02-11 04:59:37 +00001494
1495 if (opts::Output == opts::GNU)
Hemant Kulkarnic030f232016-03-15 17:25:31 +00001496 ELFDumperStyle.reset(new GNUStyle<ELFT>(Writer, this));
Michael J. Spencer60d82b22016-02-11 04:59:37 +00001497 else
Hemant Kulkarnic030f232016-03-15 17:25:31 +00001498 ELFDumperStyle.reset(new LLVMStyle<ELFT>(Writer, this));
Michael J. Spencer60d82b22016-02-11 04:59:37 +00001499}
1500
George Rimar8ac7b2d2019-05-29 10:31:46 +00001501static const char *getTypeString(unsigned Arch, uint64_t Type) {
1502#define DYNAMIC_TAG(n, v)
1503 switch (Arch) {
Peter Smith49d72212019-06-04 11:44:33 +00001504
1505 case EM_AARCH64:
1506 switch (Type) {
1507#define AARCH64_DYNAMIC_TAG(name, value) \
1508 case DT_##name: \
1509 return #name;
1510#include "llvm/BinaryFormat/DynamicTags.def"
1511#undef AARCH64_DYNAMIC_TAG
1512 }
1513 break;
1514
George Rimar8ac7b2d2019-05-29 10:31:46 +00001515 case EM_HEXAGON:
1516 switch (Type) {
1517#define HEXAGON_DYNAMIC_TAG(name, value) \
1518 case DT_##name: \
1519 return #name;
1520#include "llvm/BinaryFormat/DynamicTags.def"
1521#undef HEXAGON_DYNAMIC_TAG
1522 }
1523 break;
1524
1525 case EM_MIPS:
1526 switch (Type) {
1527#define MIPS_DYNAMIC_TAG(name, value) \
1528 case DT_##name: \
1529 return #name;
1530#include "llvm/BinaryFormat/DynamicTags.def"
1531#undef MIPS_DYNAMIC_TAG
1532 }
1533 break;
1534
1535 case EM_PPC64:
1536 switch (Type) {
1537#define PPC64_DYNAMIC_TAG(name, value) \
1538 case DT_##name: \
1539 return #name;
1540#include "llvm/BinaryFormat/DynamicTags.def"
1541#undef PPC64_DYNAMIC_TAG
1542 }
1543 break;
1544 }
1545#undef DYNAMIC_TAG
1546 switch (Type) {
1547// Now handle all dynamic tags except the architecture specific ones
Peter Smith49d72212019-06-04 11:44:33 +00001548#define AARCH64_DYNAMIC_TAG(name, value)
George Rimar8ac7b2d2019-05-29 10:31:46 +00001549#define MIPS_DYNAMIC_TAG(name, value)
1550#define HEXAGON_DYNAMIC_TAG(name, value)
1551#define PPC64_DYNAMIC_TAG(name, value)
1552// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
1553#define DYNAMIC_TAG_MARKER(name, value)
1554#define DYNAMIC_TAG(name, value) \
1555 case DT_##name: \
1556 return #name;
1557#include "llvm/BinaryFormat/DynamicTags.def"
1558#undef DYNAMIC_TAG
Peter Smith49d72212019-06-04 11:44:33 +00001559#undef AARCH64_DYNAMIC_TAG
George Rimar8ac7b2d2019-05-29 10:31:46 +00001560#undef MIPS_DYNAMIC_TAG
1561#undef HEXAGON_DYNAMIC_TAG
1562#undef PPC64_DYNAMIC_TAG
1563#undef DYNAMIC_TAG_MARKER
1564 default:
1565 return "unknown";
1566 }
1567}
1568
George Rimar72f821d2019-05-20 15:41:48 +00001569template <typename ELFT> void ELFDumper<ELFT>::parseDynamicTable() {
George Rimar8ac7b2d2019-05-29 10:31:46 +00001570 auto toMappedAddr = [&](uint64_t Tag, uint64_t VAddr) -> const uint8_t * {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00001571 auto MappedAddrOrError = ObjF->getELFFile()->toMappedAddr(VAddr);
George Rimar8ac7b2d2019-05-29 10:31:46 +00001572 if (!MappedAddrOrError) {
1573 reportWarning("Unable to parse DT_" +
1574 Twine(getTypeString(
1575 ObjF->getELFFile()->getHeader()->e_machine, Tag)) +
1576 ": " + llvm::toString(MappedAddrOrError.takeError()));
1577 return nullptr;
1578 }
Xing GUOfe5a6c32018-12-08 05:32:28 +00001579 return MappedAddrOrError.get();
George Rimar47936762016-01-16 00:49:19 +00001580 };
1581
1582 uint64_t SONameOffset = 0;
1583 const char *StringTableBegin = nullptr;
1584 uint64_t StringTableSize = 0;
1585 for (const Elf_Dyn &Dyn : dynamic_table()) {
1586 switch (Dyn.d_tag) {
1587 case ELF::DT_HASH:
George Rimar8ac7b2d2019-05-29 10:31:46 +00001588 HashTable = reinterpret_cast<const Elf_Hash *>(
1589 toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
George Rimar47936762016-01-16 00:49:19 +00001590 break;
1591 case ELF::DT_GNU_HASH:
George Rimar8ac7b2d2019-05-29 10:31:46 +00001592 GnuHashTable = reinterpret_cast<const Elf_GnuHash *>(
1593 toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
George Rimar47936762016-01-16 00:49:19 +00001594 break;
Michael J. Spencer94f060c2016-02-11 04:59:32 +00001595 case ELF::DT_STRTAB:
George Rimar8ac7b2d2019-05-29 10:31:46 +00001596 StringTableBegin = reinterpret_cast<const char *>(
1597 toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
Simon Atanasyan72155c32016-01-16 22:40:09 +00001598 break;
Michael J. Spencer94f060c2016-02-11 04:59:32 +00001599 case ELF::DT_STRSZ:
1600 StringTableSize = Dyn.getVal();
Simon Atanasyan72155c32016-01-16 22:40:09 +00001601 break;
Michael J. Spencer94f060c2016-02-11 04:59:32 +00001602 case ELF::DT_SYMTAB:
George Rimar8ac7b2d2019-05-29 10:31:46 +00001603 DynSymRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
Rafael Espindolace2fbdd2016-02-17 15:38:21 +00001604 DynSymRegion.EntSize = sizeof(Elf_Sym);
Simon Atanasyan72155c32016-01-16 22:40:09 +00001605 break;
George Rimar47936762016-01-16 00:49:19 +00001606 case ELF::DT_RELA:
George Rimar8ac7b2d2019-05-29 10:31:46 +00001607 DynRelaRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
George Rimar47936762016-01-16 00:49:19 +00001608 break;
1609 case ELF::DT_RELASZ:
1610 DynRelaRegion.Size = Dyn.getVal();
1611 break;
1612 case ELF::DT_RELAENT:
1613 DynRelaRegion.EntSize = Dyn.getVal();
1614 break;
1615 case ELF::DT_SONAME:
1616 SONameOffset = Dyn.getVal();
1617 break;
Michael J. Spencer94f060c2016-02-11 04:59:32 +00001618 case ELF::DT_REL:
George Rimar8ac7b2d2019-05-29 10:31:46 +00001619 DynRelRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
George Rimar47936762016-01-16 00:49:19 +00001620 break;
Michael J. Spencer94f060c2016-02-11 04:59:32 +00001621 case ELF::DT_RELSZ:
1622 DynRelRegion.Size = Dyn.getVal();
George Rimar47936762016-01-16 00:49:19 +00001623 break;
Michael J. Spencer94f060c2016-02-11 04:59:32 +00001624 case ELF::DT_RELENT:
1625 DynRelRegion.EntSize = Dyn.getVal();
George Rimar47936762016-01-16 00:49:19 +00001626 break;
Jake Ehrlich0f440d82018-06-28 21:07:34 +00001627 case ELF::DT_RELR:
1628 case ELF::DT_ANDROID_RELR:
George Rimar8ac7b2d2019-05-29 10:31:46 +00001629 DynRelrRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
Jake Ehrlich0f440d82018-06-28 21:07:34 +00001630 break;
1631 case ELF::DT_RELRSZ:
1632 case ELF::DT_ANDROID_RELRSZ:
1633 DynRelrRegion.Size = Dyn.getVal();
1634 break;
1635 case ELF::DT_RELRENT:
1636 case ELF::DT_ANDROID_RELRENT:
1637 DynRelrRegion.EntSize = Dyn.getVal();
1638 break;
Rafael Espindola944f6552016-02-16 15:16:00 +00001639 case ELF::DT_PLTREL:
1640 if (Dyn.getVal() == DT_REL)
1641 DynPLTRelRegion.EntSize = sizeof(Elf_Rel);
1642 else if (Dyn.getVal() == DT_RELA)
1643 DynPLTRelRegion.EntSize = sizeof(Elf_Rela);
1644 else
1645 reportError(Twine("unknown DT_PLTREL value of ") +
1646 Twine((uint64_t)Dyn.getVal()));
1647 break;
1648 case ELF::DT_JMPREL:
George Rimar8ac7b2d2019-05-29 10:31:46 +00001649 DynPLTRelRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
Rafael Espindola944f6552016-02-16 15:16:00 +00001650 break;
1651 case ELF::DT_PLTRELSZ:
1652 DynPLTRelRegion.Size = Dyn.getVal();
1653 break;
George Rimar47936762016-01-16 00:49:19 +00001654 }
1655 }
1656 if (StringTableBegin)
1657 DynamicStringTable = StringRef(StringTableBegin, StringTableSize);
Yuanfang Chenabbc3ff2019-07-18 17:04:28 +00001658 SOName = getDynamicString(SONameOffset);
Rafael Espindola6009db62016-02-16 14:17:48 +00001659}
George Rimar47936762016-01-16 00:49:19 +00001660
Rafael Espindola6009db62016-02-16 14:17:48 +00001661template <typename ELFT>
Simon Atanasyan72155c32016-01-16 22:40:09 +00001662typename ELFDumper<ELFT>::Elf_Rel_Range ELFDumper<ELFT>::dyn_rels() const {
Rafael Espindolaaafcf752016-04-05 14:47:22 +00001663 return DynRelRegion.getAsArrayRef<Elf_Rel>();
George Rimar47936762016-01-16 00:49:19 +00001664}
1665
1666template <typename ELFT>
1667typename ELFDumper<ELFT>::Elf_Rela_Range ELFDumper<ELFT>::dyn_relas() const {
Rafael Espindolaaafcf752016-04-05 14:47:22 +00001668 return DynRelaRegion.getAsArrayRef<Elf_Rela>();
George Rimar47936762016-01-16 00:49:19 +00001669}
1670
Jake Ehrlich0f440d82018-06-28 21:07:34 +00001671template <typename ELFT>
1672typename ELFDumper<ELFT>::Elf_Relr_Range ELFDumper<ELFT>::dyn_relrs() const {
1673 return DynRelrRegion.getAsArrayRef<Elf_Relr>();
1674}
1675
George Rimar9e88a262019-05-14 14:22:44 +00001676template <class ELFT> void ELFDumper<ELFT>::printFileHeaders() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00001677 ELFDumperStyle->printFileHeaders(ObjF->getELFFile());
George Rimar47936762016-01-16 00:49:19 +00001678}
1679
George Rimar9e88a262019-05-14 14:22:44 +00001680template <class ELFT> void ELFDumper<ELFT>::printSectionHeaders() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00001681 ELFDumperStyle->printSectionHeaders(ObjF->getELFFile());
George Rimar47936762016-01-16 00:49:19 +00001682}
1683
George Rimar9e88a262019-05-14 14:22:44 +00001684template <class ELFT> void ELFDumper<ELFT>::printRelocations() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00001685 ELFDumperStyle->printRelocations(ObjF->getELFFile());
George Rimar47936762016-01-16 00:49:19 +00001686}
1687
Matt Davis50ca8ed2019-02-01 18:51:10 +00001688template <class ELFT>
1689void ELFDumper<ELFT>::printProgramHeaders(
1690 bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) {
1691 ELFDumperStyle->printProgramHeaders(ObjF->getELFFile(), PrintProgramHeaders,
1692 PrintSectionMapping);
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00001693}
1694
Xing GUOea16be12019-03-25 11:02:49 +00001695template <typename ELFT> void ELFDumper<ELFT>::printVersionInfo() {
1696 // Dump version symbol section.
1697 ELFDumperStyle->printVersionSymbolSection(ObjF->getELFFile(),
Xing GUO09a77fe2019-03-29 01:26:36 +00001698 SymbolVersionSection);
Xing GUOea16be12019-03-25 11:02:49 +00001699
1700 // Dump version definition section.
1701 ELFDumperStyle->printVersionDefinitionSection(ObjF->getELFFile(),
Xing GUO09a77fe2019-03-29 01:26:36 +00001702 SymbolVersionDefSection);
Xing GUOea16be12019-03-25 11:02:49 +00001703
1704 // Dump version dependency section.
1705 ELFDumperStyle->printVersionDependencySection(ObjF->getELFFile(),
Xing GUO09a77fe2019-03-29 01:26:36 +00001706 SymbolVersionNeedSection);
Xing GUOea16be12019-03-25 11:02:49 +00001707}
1708
Simon Atanasyan72155c32016-01-16 22:40:09 +00001709template <class ELFT> void ELFDumper<ELFT>::printDynamicRelocations() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00001710 ELFDumperStyle->printDynamicRelocations(ObjF->getELFFile());
George Rimar47936762016-01-16 00:49:19 +00001711}
1712
James Henderson21ed8682019-01-23 16:15:39 +00001713template <class ELFT>
1714void ELFDumper<ELFT>::printSymbols(bool PrintSymbols,
1715 bool PrintDynamicSymbols) {
1716 ELFDumperStyle->printSymbols(ObjF->getELFFile(), PrintSymbols,
1717 PrintDynamicSymbols);
George Rimar47936762016-01-16 00:49:19 +00001718}
1719
George Rimar9e88a262019-05-14 14:22:44 +00001720template <class ELFT> void ELFDumper<ELFT>::printHashSymbols() {
James Henderson5fc812f2019-01-22 09:35:35 +00001721 ELFDumperStyle->printHashSymbols(ObjF->getELFFile());
1722}
1723
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +00001724template <class ELFT> void ELFDumper<ELFT>::printHashHistogram() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00001725 ELFDumperStyle->printHashHistogram(ObjF->getELFFile());
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +00001726}
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00001727
Michael J. Spencerae6eeae2018-06-02 16:33:01 +00001728template <class ELFT> void ELFDumper<ELFT>::printCGProfile() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00001729 ELFDumperStyle->printCGProfile(ObjF->getELFFile());
Michael J. Spencerae6eeae2018-06-02 16:33:01 +00001730}
1731
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00001732template <class ELFT> void ELFDumper<ELFT>::printNotes() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00001733 ELFDumperStyle->printNotes(ObjF->getELFFile());
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00001734}
1735
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +00001736template <class ELFT> void ELFDumper<ELFT>::printELFLinkerOptions() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00001737 ELFDumperStyle->printELFLinkerOptions(ObjF->getELFFile());
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +00001738}
1739
Wolfgang Piebc71c6292019-08-05 22:47:07 +00001740template <class ELFT> void ELFDumper<ELFT>::printStackSizes() {
1741 ELFDumperStyle->printStackSizes(ObjF);
1742}
1743
George Rimar9e88a262019-05-14 14:22:44 +00001744#define LLVM_READOBJ_DT_FLAG_ENT(prefix, enum) \
George Rimar47936762016-01-16 00:49:19 +00001745 { #enum, prefix##_##enum }
1746
1747static const EnumEntry<unsigned> ElfDynamicDTFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001748 LLVM_READOBJ_DT_FLAG_ENT(DF, ORIGIN),
1749 LLVM_READOBJ_DT_FLAG_ENT(DF, SYMBOLIC),
1750 LLVM_READOBJ_DT_FLAG_ENT(DF, TEXTREL),
1751 LLVM_READOBJ_DT_FLAG_ENT(DF, BIND_NOW),
1752 LLVM_READOBJ_DT_FLAG_ENT(DF, STATIC_TLS)
1753};
George Rimar47936762016-01-16 00:49:19 +00001754
1755static const EnumEntry<unsigned> ElfDynamicDTFlags1[] = {
George Rimarec895f12019-05-16 06:22:51 +00001756 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOW),
1757 LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAL),
1758 LLVM_READOBJ_DT_FLAG_ENT(DF_1, GROUP),
1759 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODELETE),
1760 LLVM_READOBJ_DT_FLAG_ENT(DF_1, LOADFLTR),
1761 LLVM_READOBJ_DT_FLAG_ENT(DF_1, INITFIRST),
1762 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOOPEN),
1763 LLVM_READOBJ_DT_FLAG_ENT(DF_1, ORIGIN),
1764 LLVM_READOBJ_DT_FLAG_ENT(DF_1, DIRECT),
1765 LLVM_READOBJ_DT_FLAG_ENT(DF_1, TRANS),
1766 LLVM_READOBJ_DT_FLAG_ENT(DF_1, INTERPOSE),
1767 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODEFLIB),
1768 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODUMP),
1769 LLVM_READOBJ_DT_FLAG_ENT(DF_1, CONFALT),
1770 LLVM_READOBJ_DT_FLAG_ENT(DF_1, ENDFILTEE),
1771 LLVM_READOBJ_DT_FLAG_ENT(DF_1, DISPRELDNE),
1772 LLVM_READOBJ_DT_FLAG_ENT(DF_1, DISPRELPND),
1773 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODIRECT),
1774 LLVM_READOBJ_DT_FLAG_ENT(DF_1, IGNMULDEF),
1775 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOKSYMS),
1776 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOHDR),
1777 LLVM_READOBJ_DT_FLAG_ENT(DF_1, EDITED),
1778 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NORELOC),
1779 LLVM_READOBJ_DT_FLAG_ENT(DF_1, SYMINTPOSE),
1780 LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAUDIT),
1781 LLVM_READOBJ_DT_FLAG_ENT(DF_1, SINGLETON)
1782};
George Rimar47936762016-01-16 00:49:19 +00001783
1784static const EnumEntry<unsigned> ElfDynamicDTMipsFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00001785 LLVM_READOBJ_DT_FLAG_ENT(RHF, NONE),
1786 LLVM_READOBJ_DT_FLAG_ENT(RHF, QUICKSTART),
1787 LLVM_READOBJ_DT_FLAG_ENT(RHF, NOTPOT),
1788 LLVM_READOBJ_DT_FLAG_ENT(RHS, NO_LIBRARY_REPLACEMENT),
1789 LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_MOVE),
1790 LLVM_READOBJ_DT_FLAG_ENT(RHF, SGI_ONLY),
1791 LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_INIT),
1792 LLVM_READOBJ_DT_FLAG_ENT(RHF, DELTA_C_PLUS_PLUS),
1793 LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_START_INIT),
1794 LLVM_READOBJ_DT_FLAG_ENT(RHF, PIXIE),
1795 LLVM_READOBJ_DT_FLAG_ENT(RHF, DEFAULT_DELAY_LOAD),
1796 LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTART),
1797 LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTARTED),
1798 LLVM_READOBJ_DT_FLAG_ENT(RHF, CORD),
1799 LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_UNRES_UNDEF),
1800 LLVM_READOBJ_DT_FLAG_ENT(RHF, RLD_ORDER_SAFE)
1801};
George Rimar47936762016-01-16 00:49:19 +00001802
1803#undef LLVM_READOBJ_DT_FLAG_ENT
1804
1805template <typename T, typename TFlag>
1806void printFlags(T Value, ArrayRef<EnumEntry<TFlag>> Flags, raw_ostream &OS) {
Eugene Zelenko416e0592017-06-09 21:41:54 +00001807 using FlagEntry = EnumEntry<TFlag>;
1808 using FlagVector = SmallVector<FlagEntry, 10>;
George Rimar47936762016-01-16 00:49:19 +00001809 FlagVector SetFlags;
1810
1811 for (const auto &Flag : Flags) {
1812 if (Flag.Value == 0)
1813 continue;
1814
1815 if ((Value & Flag.Value) == Flag.Value)
1816 SetFlags.push_back(Flag);
1817 }
1818
1819 for (const auto &Flag : SetFlags) {
1820 OS << Flag.Name << " ";
1821 }
1822}
1823
1824template <class ELFT>
Simon Atanasyan8362cbe2019-05-24 12:22:53 +00001825void ELFDumper<ELFT>::printDynamicEntry(raw_ostream &OS, uint64_t Type,
1826 uint64_t Value) const {
George Rimar9e88a262019-05-14 14:22:44 +00001827 const char *ConvChar =
1828 (opts::Output == opts::GNU) ? "0x%" PRIx64 : "0x%" PRIX64;
Peter Smith49d72212019-06-04 11:44:33 +00001829
1830 // Handle custom printing of architecture specific tags
1831 switch (ObjF->getELFFile()->getHeader()->e_machine) {
1832 case EM_AARCH64:
1833 switch (Type) {
1834 case DT_AARCH64_BTI_PLT:
1835 case DT_AARCH64_PAC_PLT:
1836 OS << Value;
1837 return;
1838 default:
1839 break;
1840 }
1841 break;
1842 case EM_HEXAGON:
1843 switch (Type) {
1844 case DT_HEXAGON_VER:
1845 OS << Value;
1846 return;
1847 case DT_HEXAGON_SYMSZ:
1848 case DT_HEXAGON_PLT:
1849 OS << format(ConvChar, Value);
1850 return;
1851 default:
1852 break;
1853 }
1854 break;
1855 case EM_MIPS:
1856 switch (Type) {
1857 case DT_MIPS_RLD_VERSION:
1858 case DT_MIPS_LOCAL_GOTNO:
1859 case DT_MIPS_SYMTABNO:
1860 case DT_MIPS_UNREFEXTNO:
1861 OS << Value;
1862 return;
1863 case DT_MIPS_TIME_STAMP:
1864 case DT_MIPS_ICHECKSUM:
1865 case DT_MIPS_IVERSION:
1866 case DT_MIPS_BASE_ADDRESS:
1867 case DT_MIPS_MSYM:
1868 case DT_MIPS_CONFLICT:
1869 case DT_MIPS_LIBLIST:
1870 case DT_MIPS_CONFLICTNO:
1871 case DT_MIPS_LIBLISTNO:
1872 case DT_MIPS_GOTSYM:
1873 case DT_MIPS_HIPAGENO:
1874 case DT_MIPS_RLD_MAP:
1875 case DT_MIPS_DELTA_CLASS:
1876 case DT_MIPS_DELTA_CLASS_NO:
1877 case DT_MIPS_DELTA_INSTANCE:
1878 case DT_MIPS_DELTA_RELOC:
1879 case DT_MIPS_DELTA_RELOC_NO:
1880 case DT_MIPS_DELTA_SYM:
1881 case DT_MIPS_DELTA_SYM_NO:
1882 case DT_MIPS_DELTA_CLASSSYM:
1883 case DT_MIPS_DELTA_CLASSSYM_NO:
1884 case DT_MIPS_CXX_FLAGS:
1885 case DT_MIPS_PIXIE_INIT:
1886 case DT_MIPS_SYMBOL_LIB:
1887 case DT_MIPS_LOCALPAGE_GOTIDX:
1888 case DT_MIPS_LOCAL_GOTIDX:
1889 case DT_MIPS_HIDDEN_GOTIDX:
1890 case DT_MIPS_PROTECTED_GOTIDX:
1891 case DT_MIPS_OPTIONS:
1892 case DT_MIPS_INTERFACE:
1893 case DT_MIPS_DYNSTR_ALIGN:
1894 case DT_MIPS_INTERFACE_SIZE:
1895 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
1896 case DT_MIPS_PERF_SUFFIX:
1897 case DT_MIPS_COMPACT_SIZE:
1898 case DT_MIPS_GP_VALUE:
1899 case DT_MIPS_AUX_DYNAMIC:
1900 case DT_MIPS_PLTGOT:
1901 case DT_MIPS_RWPLT:
1902 case DT_MIPS_RLD_MAP_REL:
1903 OS << format(ConvChar, Value);
1904 return;
1905 case DT_MIPS_FLAGS:
1906 printFlags(Value, makeArrayRef(ElfDynamicDTMipsFlags), OS);
1907 return;
1908 default:
1909 break;
1910 }
1911 break;
1912 default:
1913 break;
1914 }
1915
George Rimar47936762016-01-16 00:49:19 +00001916 switch (Type) {
1917 case DT_PLTREL:
1918 if (Value == DT_REL) {
1919 OS << "REL";
1920 break;
1921 } else if (Value == DT_RELA) {
1922 OS << "RELA";
1923 break;
1924 }
Justin Bognerb03fd122016-08-17 05:10:15 +00001925 LLVM_FALLTHROUGH;
George Rimar47936762016-01-16 00:49:19 +00001926 case DT_PLTGOT:
1927 case DT_HASH:
1928 case DT_STRTAB:
1929 case DT_SYMTAB:
1930 case DT_RELA:
1931 case DT_INIT:
1932 case DT_FINI:
1933 case DT_REL:
1934 case DT_JMPREL:
1935 case DT_INIT_ARRAY:
1936 case DT_FINI_ARRAY:
1937 case DT_PREINIT_ARRAY:
1938 case DT_DEBUG:
1939 case DT_VERDEF:
1940 case DT_VERNEED:
1941 case DT_VERSYM:
1942 case DT_GNU_HASH:
1943 case DT_NULL:
Hemant Kulkarnicb21f3c2016-05-12 22:16:53 +00001944 OS << format(ConvChar, Value);
George Rimar47936762016-01-16 00:49:19 +00001945 break;
Davide Italiano8c503672016-01-16 06:06:36 +00001946 case DT_RELACOUNT:
George Rimar47936762016-01-16 00:49:19 +00001947 case DT_RELCOUNT:
1948 case DT_VERDEFNUM:
1949 case DT_VERNEEDNUM:
George Rimar47936762016-01-16 00:49:19 +00001950 OS << Value;
1951 break;
1952 case DT_PLTRELSZ:
1953 case DT_RELASZ:
1954 case DT_RELAENT:
1955 case DT_STRSZ:
1956 case DT_SYMENT:
1957 case DT_RELSZ:
1958 case DT_RELENT:
1959 case DT_INIT_ARRAYSZ:
1960 case DT_FINI_ARRAYSZ:
1961 case DT_PREINIT_ARRAYSZ:
Peter Collingbourne5c54f152017-10-27 17:49:40 +00001962 case DT_ANDROID_RELSZ:
1963 case DT_ANDROID_RELASZ:
George Rimar47936762016-01-16 00:49:19 +00001964 OS << Value << " (bytes)";
1965 break;
1966 case DT_NEEDED:
George Rimar47936762016-01-16 00:49:19 +00001967 case DT_SONAME:
George Rimard8a4eca2016-09-02 07:35:19 +00001968 case DT_AUXILIARY:
Xing GUOeee62262019-03-07 14:53:10 +00001969 case DT_USED:
Yuanfang Chen40a156b2019-06-19 19:31:07 +00001970 case DT_FILTER:
1971 case DT_RPATH:
1972 case DT_RUNPATH: {
James Hendersonf7cfabb2019-06-14 12:02:01 +00001973 const std::map<uint64_t, const char*> TagNames = {
1974 {DT_NEEDED, "Shared library"},
1975 {DT_SONAME, "Library soname"},
1976 {DT_AUXILIARY, "Auxiliary library"},
1977 {DT_USED, "Not needed object"},
1978 {DT_FILTER, "Filter library"},
Yuanfang Chen40a156b2019-06-19 19:31:07 +00001979 {DT_RPATH, "Library rpath"},
1980 {DT_RUNPATH, "Library runpath"},
James Hendersonf7cfabb2019-06-14 12:02:01 +00001981 };
Yuanfang Chenabbc3ff2019-07-18 17:04:28 +00001982 OS << TagNames.at(Type) << ": [" << getDynamicString(Value) << "]";
Xing GUOeee62262019-03-07 14:53:10 +00001983 break;
James Hendersonf7cfabb2019-06-14 12:02:01 +00001984 }
George Rimar47936762016-01-16 00:49:19 +00001985 case DT_FLAGS:
1986 printFlags(Value, makeArrayRef(ElfDynamicDTFlags), OS);
1987 break;
1988 case DT_FLAGS_1:
1989 printFlags(Value, makeArrayRef(ElfDynamicDTFlags1), OS);
1990 break;
1991 default:
Hemant Kulkarnicb21f3c2016-05-12 22:16:53 +00001992 OS << format(ConvChar, Value);
George Rimar47936762016-01-16 00:49:19 +00001993 break;
1994 }
1995}
1996
Yuanfang Chenabbc3ff2019-07-18 17:04:28 +00001997template <class ELFT>
1998std::string ELFDumper<ELFT>::getDynamicString(uint64_t Value) const {
1999 if (DynamicStringTable.empty())
2000 return "<String table is empty or was not found>";
2001 if (Value < DynamicStringTable.size())
2002 return DynamicStringTable.data() + Value;
2003 return Twine("<Invalid offset 0x" + utohexstr(Value) + ">").str();
2004}
2005
George Rimar9e88a262019-05-14 14:22:44 +00002006template <class ELFT> void ELFDumper<ELFT>::printUnwindInfo() {
Peter Collingbourneac136cd2019-02-28 22:42:55 +00002007 DwarfCFIEH::PrinterContext<ELFT> Ctx(W, ObjF);
2008 Ctx.printUnwindInformation();
George Rimar47936762016-01-16 00:49:19 +00002009}
2010
2011namespace {
Eugene Zelenko416e0592017-06-09 21:41:54 +00002012
Rui Ueyama1b31eb92018-01-12 01:40:32 +00002013template <> void ELFDumper<ELF32LE>::printUnwindInfo() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00002014 const ELFFile<ELF32LE> *Obj = ObjF->getELFFile();
George Rimar47936762016-01-16 00:49:19 +00002015 const unsigned Machine = Obj->getHeader()->e_machine;
2016 if (Machine == EM_ARM) {
Rui Ueyama1b31eb92018-01-12 01:40:32 +00002017 ARM::EHABI::PrinterContext<ELF32LE> Ctx(W, Obj, DotSymtabSec);
Peter Collingbourneac136cd2019-02-28 22:42:55 +00002018 Ctx.PrintUnwindInformation();
George Rimar47936762016-01-16 00:49:19 +00002019 }
Peter Collingbourneac136cd2019-02-28 22:42:55 +00002020 DwarfCFIEH::PrinterContext<ELF32LE> Ctx(W, ObjF);
2021 Ctx.printUnwindInformation();
George Rimar47936762016-01-16 00:49:19 +00002022}
Eugene Zelenko416e0592017-06-09 21:41:54 +00002023
2024} // end anonymous namespace
George Rimar47936762016-01-16 00:49:19 +00002025
George Rimar9e88a262019-05-14 14:22:44 +00002026template <class ELFT> void ELFDumper<ELFT>::printDynamicTable() {
Simon Atanasyan8362cbe2019-05-24 12:22:53 +00002027 ELFDumperStyle->printDynamic(ObjF->getELFFile());
George Rimar47936762016-01-16 00:49:19 +00002028}
2029
George Rimar9e88a262019-05-14 14:22:44 +00002030template <class ELFT> void ELFDumper<ELFT>::printNeededLibraries() {
George Rimar47936762016-01-16 00:49:19 +00002031 ListScope D(W, "NeededLibraries");
2032
Yuanfang Chenabbc3ff2019-07-18 17:04:28 +00002033 std::vector<std::string> Libs;
George Rimar47936762016-01-16 00:49:19 +00002034 for (const auto &Entry : dynamic_table())
Yuanfang Chenabbc3ff2019-07-18 17:04:28 +00002035 if (Entry.d_tag == ELF::DT_NEEDED)
2036 Libs.push_back(getDynamicString(Entry.d_un.d_val));
George Rimar47936762016-01-16 00:49:19 +00002037
Fangrui Songa5355a52019-04-22 15:53:43 +00002038 llvm::stable_sort(Libs);
George Rimar47936762016-01-16 00:49:19 +00002039
Sam Clegg88e9a152018-01-10 00:14:19 +00002040 for (const auto &L : Libs)
George Rimar9e88a262019-05-14 14:22:44 +00002041 W.startLine() << L << "\n";
George Rimar47936762016-01-16 00:49:19 +00002042}
2043
George Rimar9e88a262019-05-14 14:22:44 +00002044template <typename ELFT> void ELFDumper<ELFT>::printHashTable() {
George Rimar47936762016-01-16 00:49:19 +00002045 DictScope D(W, "HashTable");
2046 if (!HashTable)
2047 return;
2048 W.printNumber("Num Buckets", HashTable->nbucket);
2049 W.printNumber("Num Chains", HashTable->nchain);
2050 W.printList("Buckets", HashTable->buckets());
2051 W.printList("Chains", HashTable->chains());
2052}
2053
George Rimar9e88a262019-05-14 14:22:44 +00002054template <typename ELFT> void ELFDumper<ELFT>::printGnuHashTable() {
George Rimar47936762016-01-16 00:49:19 +00002055 DictScope D(W, "GnuHashTable");
2056 if (!GnuHashTable)
2057 return;
2058 W.printNumber("Num Buckets", GnuHashTable->nbuckets);
2059 W.printNumber("First Hashed Symbol Index", GnuHashTable->symndx);
2060 W.printNumber("Num Mask Words", GnuHashTable->maskwords);
2061 W.printNumber("Shift Count", GnuHashTable->shift2);
2062 W.printHexList("Bloom Filter", GnuHashTable->filter());
2063 W.printList("Buckets", GnuHashTable->buckets());
Rafael Espindolace2fbdd2016-02-17 15:38:21 +00002064 Elf_Sym_Range Syms = dynamic_symbols();
2065 unsigned NumSyms = std::distance(Syms.begin(), Syms.end());
2066 if (!NumSyms)
George Rimar47936762016-01-16 00:49:19 +00002067 reportError("No dynamic symbol section");
Rafael Espindolace2fbdd2016-02-17 15:38:21 +00002068 W.printHexList("Values", GnuHashTable->values(NumSyms));
George Rimar47936762016-01-16 00:49:19 +00002069}
2070
2071template <typename ELFT> void ELFDumper<ELFT>::printLoadName() {
Sam Clegg88e9a152018-01-10 00:14:19 +00002072 W.printString("LoadName", SOName);
George Rimar47936762016-01-16 00:49:19 +00002073}
2074
George Rimar9e88a262019-05-14 14:22:44 +00002075template <class ELFT> void ELFDumper<ELFT>::printAttributes() {
George Rimar47936762016-01-16 00:49:19 +00002076 W.startLine() << "Attributes not implemented.\n";
2077}
2078
2079namespace {
Eugene Zelenko416e0592017-06-09 21:41:54 +00002080
Rui Ueyama1b31eb92018-01-12 01:40:32 +00002081template <> void ELFDumper<ELF32LE>::printAttributes() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00002082 const ELFFile<ELF32LE> *Obj = ObjF->getELFFile();
George Rimar47936762016-01-16 00:49:19 +00002083 if (Obj->getHeader()->e_machine != EM_ARM) {
2084 W.startLine() << "Attributes not implemented.\n";
2085 return;
2086 }
2087
2088 DictScope BA(W, "BuildAttributes");
Rafael Espindola25be8c82016-11-02 14:10:57 +00002089 for (const ELFO::Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
George Rimar47936762016-01-16 00:49:19 +00002090 if (Sec.sh_type != ELF::SHT_ARM_ATTRIBUTES)
2091 continue;
2092
Rafael Espindolaf04f1842016-02-17 16:21:49 +00002093 ArrayRef<uint8_t> Contents = unwrapOrError(Obj->getSectionContents(&Sec));
2094 if (Contents[0] != ARMBuildAttrs::Format_Version) {
Benjamin Kramer3a13ed62017-12-28 16:58:54 +00002095 errs() << "unrecognised FormatVersion: 0x"
2096 << Twine::utohexstr(Contents[0]) << '\n';
George Rimar47936762016-01-16 00:49:19 +00002097 continue;
2098 }
2099
Rafael Espindolaf04f1842016-02-17 16:21:49 +00002100 W.printHex("FormatVersion", Contents[0]);
2101 if (Contents.size() == 1)
George Rimar47936762016-01-16 00:49:19 +00002102 continue;
2103
Sam Parkerdf7c6ef2017-01-18 13:52:12 +00002104 ARMAttributeParser(&W).Parse(Contents, true);
George Rimar47936762016-01-16 00:49:19 +00002105 }
2106}
George Rimar47936762016-01-16 00:49:19 +00002107
George Rimar47936762016-01-16 00:49:19 +00002108template <class ELFT> class MipsGOTParser {
2109public:
Rafael Espindola6bc29902016-10-06 14:07:26 +00002110 TYPEDEF_ELF_TYPES(ELFT)
Simon Atanasyan62d32592017-12-21 10:26:02 +00002111 using Entry = typename ELFO::Elf_Addr;
2112 using Entries = ArrayRef<Entry>;
Eugene Zelenko416e0592017-06-09 21:41:54 +00002113
Simon Atanasyan62d32592017-12-21 10:26:02 +00002114 const bool IsStatic;
George Rimarec895f12019-05-16 06:22:51 +00002115 const ELFO * const Obj;
George Rimar47936762016-01-16 00:49:19 +00002116
Simon Atanasyan62d32592017-12-21 10:26:02 +00002117 MipsGOTParser(const ELFO *Obj, Elf_Dyn_Range DynTable, Elf_Sym_Range DynSyms);
2118
2119 bool hasGot() const { return !GotEntries.empty(); }
2120 bool hasPlt() const { return !PltEntries.empty(); }
2121
2122 uint64_t getGp() const;
2123
2124 const Entry *getGotLazyResolver() const;
2125 const Entry *getGotModulePointer() const;
2126 const Entry *getPltLazyResolver() const;
2127 const Entry *getPltModulePointer() const;
2128
2129 Entries getLocalEntries() const;
2130 Entries getGlobalEntries() const;
2131 Entries getOtherEntries() const;
2132 Entries getPltEntries() const;
2133
George Rimarec895f12019-05-16 06:22:51 +00002134 uint64_t getGotAddress(const Entry * E) const;
2135 int64_t getGotOffset(const Entry * E) const;
Simon Atanasyan62d32592017-12-21 10:26:02 +00002136 const Elf_Sym *getGotSym(const Entry *E) const;
2137
George Rimarec895f12019-05-16 06:22:51 +00002138 uint64_t getPltAddress(const Entry * E) const;
Simon Atanasyan62d32592017-12-21 10:26:02 +00002139 const Elf_Sym *getPltSym(const Entry *E) const;
2140
2141 StringRef getPltStrTable() const { return PltStrTable; }
George Rimar47936762016-01-16 00:49:19 +00002142
2143private:
Simon Atanasyan62d32592017-12-21 10:26:02 +00002144 const Elf_Shdr *GotSec;
2145 size_t LocalNum;
2146 size_t GlobalNum;
George Rimar47936762016-01-16 00:49:19 +00002147
Simon Atanasyan62d32592017-12-21 10:26:02 +00002148 const Elf_Shdr *PltSec;
2149 const Elf_Shdr *PltRelSec;
2150 const Elf_Shdr *PltSymTable;
2151 Elf_Sym_Range GotDynSyms;
2152 StringRef PltStrTable;
George Rimar47936762016-01-16 00:49:19 +00002153
Simon Atanasyan62d32592017-12-21 10:26:02 +00002154 Entries GotEntries;
2155 Entries PltEntries;
George Rimar47936762016-01-16 00:49:19 +00002156};
Eugene Zelenko416e0592017-06-09 21:41:54 +00002157
2158} // end anonymous namespace
George Rimar47936762016-01-16 00:49:19 +00002159
2160template <class ELFT>
Simon Atanasyan62d32592017-12-21 10:26:02 +00002161MipsGOTParser<ELFT>::MipsGOTParser(const ELFO *Obj, Elf_Dyn_Range DynTable,
2162 Elf_Sym_Range DynSyms)
2163 : IsStatic(DynTable.empty()), Obj(Obj), GotSec(nullptr), LocalNum(0),
2164 GlobalNum(0), PltSec(nullptr), PltRelSec(nullptr), PltSymTable(nullptr) {
2165 // See "Global Offset Table" in Chapter 5 in the following document
2166 // for detailed GOT description.
2167 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
2168
2169 // Find static GOT secton.
2170 if (IsStatic) {
2171 GotSec = findSectionByName(*Obj, ".got");
2172 if (!GotSec)
2173 reportError("Cannot find .got section");
2174
2175 ArrayRef<uint8_t> Content = unwrapOrError(Obj->getSectionContents(GotSec));
2176 GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()),
2177 Content.size() / sizeof(Entry));
2178 LocalNum = GotEntries.size();
2179 return;
2180 }
2181
2182 // Lookup dynamic table tags which define GOT/PLT layouts.
2183 Optional<uint64_t> DtPltGot;
2184 Optional<uint64_t> DtLocalGotNum;
2185 Optional<uint64_t> DtGotSym;
2186 Optional<uint64_t> DtMipsPltGot;
2187 Optional<uint64_t> DtJmpRel;
George Rimar47936762016-01-16 00:49:19 +00002188 for (const auto &Entry : DynTable) {
2189 switch (Entry.getTag()) {
2190 case ELF::DT_PLTGOT:
2191 DtPltGot = Entry.getVal();
2192 break;
2193 case ELF::DT_MIPS_LOCAL_GOTNO:
2194 DtLocalGotNum = Entry.getVal();
2195 break;
2196 case ELF::DT_MIPS_GOTSYM:
2197 DtGotSym = Entry.getVal();
2198 break;
2199 case ELF::DT_MIPS_PLTGOT:
2200 DtMipsPltGot = Entry.getVal();
2201 break;
2202 case ELF::DT_JMPREL:
2203 DtJmpRel = Entry.getVal();
2204 break;
2205 }
2206 }
Simon Atanasyan62d32592017-12-21 10:26:02 +00002207
2208 // Find dynamic GOT section.
2209 if (DtPltGot || DtLocalGotNum || DtGotSym) {
2210 if (!DtPltGot)
2211 report_fatal_error("Cannot find PLTGOT dynamic table tag.");
2212 if (!DtLocalGotNum)
2213 report_fatal_error("Cannot find MIPS_LOCAL_GOTNO dynamic table tag.");
2214 if (!DtGotSym)
2215 report_fatal_error("Cannot find MIPS_GOTSYM dynamic table tag.");
2216
2217 size_t DynSymTotal = DynSyms.size();
2218 if (*DtGotSym > DynSymTotal)
2219 reportError("MIPS_GOTSYM exceeds a number of dynamic symbols");
2220
2221 GotSec = findNotEmptySectionByAddress(Obj, *DtPltGot);
2222 if (!GotSec)
2223 reportError("There is no not empty GOT section at 0x" +
2224 Twine::utohexstr(*DtPltGot));
2225
2226 LocalNum = *DtLocalGotNum;
2227 GlobalNum = DynSymTotal - *DtGotSym;
2228
2229 ArrayRef<uint8_t> Content = unwrapOrError(Obj->getSectionContents(GotSec));
2230 GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()),
2231 Content.size() / sizeof(Entry));
2232 GotDynSyms = DynSyms.drop_front(*DtGotSym);
2233 }
2234
2235 // Find PLT section.
2236 if (DtMipsPltGot || DtJmpRel) {
2237 if (!DtMipsPltGot)
2238 report_fatal_error("Cannot find MIPS_PLTGOT dynamic table tag.");
2239 if (!DtJmpRel)
2240 report_fatal_error("Cannot find JMPREL dynamic table tag.");
2241
2242 PltSec = findNotEmptySectionByAddress(Obj, *DtMipsPltGot);
2243 if (!PltSec)
2244 report_fatal_error("There is no not empty PLTGOT section at 0x " +
2245 Twine::utohexstr(*DtMipsPltGot));
2246
2247 PltRelSec = findNotEmptySectionByAddress(Obj, *DtJmpRel);
2248 if (!PltRelSec)
2249 report_fatal_error("There is no not empty RELPLT section at 0x" +
2250 Twine::utohexstr(*DtJmpRel));
2251
2252 ArrayRef<uint8_t> PltContent =
2253 unwrapOrError(Obj->getSectionContents(PltSec));
2254 PltEntries = Entries(reinterpret_cast<const Entry *>(PltContent.data()),
2255 PltContent.size() / sizeof(Entry));
2256
2257 PltSymTable = unwrapOrError(Obj->getSection(PltRelSec->sh_link));
2258 PltStrTable = unwrapOrError(Obj->getStringTableForSymtab(*PltSymTable));
2259 }
2260}
2261
2262template <class ELFT> uint64_t MipsGOTParser<ELFT>::getGp() const {
2263 return GotSec->sh_addr + 0x7ff0;
George Rimar47936762016-01-16 00:49:19 +00002264}
2265
Simon Atanasyand4b693b2017-12-02 13:06:35 +00002266template <class ELFT>
Simon Atanasyan62d32592017-12-21 10:26:02 +00002267const typename MipsGOTParser<ELFT>::Entry *
2268MipsGOTParser<ELFT>::getGotLazyResolver() const {
2269 return LocalNum > 0 ? &GotEntries[0] : nullptr;
George Rimar47936762016-01-16 00:49:19 +00002270}
2271
2272template <class ELFT>
Simon Atanasyan62d32592017-12-21 10:26:02 +00002273const typename MipsGOTParser<ELFT>::Entry *
2274MipsGOTParser<ELFT>::getGotModulePointer() const {
2275 if (LocalNum < 2)
2276 return nullptr;
2277 const Entry &E = GotEntries[1];
2278 if ((E >> (sizeof(Entry) * 8 - 1)) == 0)
2279 return nullptr;
2280 return &E;
George Rimar47936762016-01-16 00:49:19 +00002281}
2282
2283template <class ELFT>
Simon Atanasyan62d32592017-12-21 10:26:02 +00002284typename MipsGOTParser<ELFT>::Entries
2285MipsGOTParser<ELFT>::getLocalEntries() const {
2286 size_t Skip = getGotModulePointer() ? 2 : 1;
2287 if (LocalNum - Skip <= 0)
2288 return Entries();
2289 return GotEntries.slice(Skip, LocalNum - Skip);
George Rimar47936762016-01-16 00:49:19 +00002290}
2291
2292template <class ELFT>
Simon Atanasyan62d32592017-12-21 10:26:02 +00002293typename MipsGOTParser<ELFT>::Entries
2294MipsGOTParser<ELFT>::getGlobalEntries() const {
2295 if (GlobalNum == 0)
2296 return Entries();
2297 return GotEntries.slice(LocalNum, GlobalNum);
George Rimar47936762016-01-16 00:49:19 +00002298}
2299
2300template <class ELFT>
Simon Atanasyan62d32592017-12-21 10:26:02 +00002301typename MipsGOTParser<ELFT>::Entries
2302MipsGOTParser<ELFT>::getOtherEntries() const {
2303 size_t OtherNum = GotEntries.size() - LocalNum - GlobalNum;
2304 if (OtherNum == 0)
2305 return Entries();
2306 return GotEntries.slice(LocalNum + GlobalNum, OtherNum);
George Rimar47936762016-01-16 00:49:19 +00002307}
2308
2309template <class ELFT>
Simon Atanasyan62d32592017-12-21 10:26:02 +00002310uint64_t MipsGOTParser<ELFT>::getGotAddress(const Entry *E) const {
2311 int64_t Offset = std::distance(GotEntries.data(), E) * sizeof(Entry);
2312 return GotSec->sh_addr + Offset;
George Rimar47936762016-01-16 00:49:19 +00002313}
2314
2315template <class ELFT>
Simon Atanasyan62d32592017-12-21 10:26:02 +00002316int64_t MipsGOTParser<ELFT>::getGotOffset(const Entry *E) const {
2317 int64_t Offset = std::distance(GotEntries.data(), E) * sizeof(Entry);
2318 return Offset - 0x7ff0;
2319}
George Rimar47936762016-01-16 00:49:19 +00002320
Simon Atanasyan62d32592017-12-21 10:26:02 +00002321template <class ELFT>
2322const typename MipsGOTParser<ELFT>::Elf_Sym *
2323MipsGOTParser<ELFT>::getGotSym(const Entry *E) const {
2324 int64_t Offset = std::distance(GotEntries.data(), E);
2325 return &GotDynSyms[Offset - LocalNum];
2326}
George Rimar47936762016-01-16 00:49:19 +00002327
Simon Atanasyan62d32592017-12-21 10:26:02 +00002328template <class ELFT>
2329const typename MipsGOTParser<ELFT>::Entry *
2330MipsGOTParser<ELFT>::getPltLazyResolver() const {
2331 return PltEntries.empty() ? nullptr : &PltEntries[0];
2332}
2333
2334template <class ELFT>
2335const typename MipsGOTParser<ELFT>::Entry *
2336MipsGOTParser<ELFT>::getPltModulePointer() const {
2337 return PltEntries.size() < 2 ? nullptr : &PltEntries[1];
2338}
2339
2340template <class ELFT>
2341typename MipsGOTParser<ELFT>::Entries
2342MipsGOTParser<ELFT>::getPltEntries() const {
2343 if (PltEntries.size() <= 2)
2344 return Entries();
2345 return PltEntries.slice(2, PltEntries.size() - 2);
2346}
2347
2348template <class ELFT>
2349uint64_t MipsGOTParser<ELFT>::getPltAddress(const Entry *E) const {
2350 int64_t Offset = std::distance(PltEntries.data(), E) * sizeof(Entry);
2351 return PltSec->sh_addr + Offset;
2352}
2353
2354template <class ELFT>
2355const typename MipsGOTParser<ELFT>::Elf_Sym *
2356MipsGOTParser<ELFT>::getPltSym(const Entry *E) const {
2357 int64_t Offset = std::distance(getPltEntries().data(), E);
2358 if (PltRelSec->sh_type == ELF::SHT_REL) {
2359 Elf_Rel_Range Rels = unwrapOrError(Obj->rels(PltRelSec));
2360 return unwrapOrError(Obj->getRelocationSymbol(&Rels[Offset], PltSymTable));
2361 } else {
2362 Elf_Rela_Range Rels = unwrapOrError(Obj->relas(PltRelSec));
2363 return unwrapOrError(Obj->getRelocationSymbol(&Rels[Offset], PltSymTable));
2364 }
George Rimar47936762016-01-16 00:49:19 +00002365}
2366
2367template <class ELFT> void ELFDumper<ELFT>::printMipsPLTGOT() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00002368 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
Simon Atanasyan62d32592017-12-21 10:26:02 +00002369 if (Obj->getHeader()->e_machine != EM_MIPS)
2370 reportError("MIPS PLT GOT is available for MIPS targets only");
George Rimar47936762016-01-16 00:49:19 +00002371
Simon Atanasyan62d32592017-12-21 10:26:02 +00002372 MipsGOTParser<ELFT> Parser(Obj, dynamic_table(), dynamic_symbols());
2373 if (Parser.hasGot())
2374 ELFDumperStyle->printMipsGOT(Parser);
2375 if (Parser.hasPlt())
2376 ELFDumperStyle->printMipsPLT(Parser);
George Rimar47936762016-01-16 00:49:19 +00002377}
2378
2379static const EnumEntry<unsigned> ElfMipsISAExtType[] = {
George Rimarec895f12019-05-16 06:22:51 +00002380 {"None", Mips::AFL_EXT_NONE},
2381 {"Broadcom SB-1", Mips::AFL_EXT_SB1},
2382 {"Cavium Networks Octeon", Mips::AFL_EXT_OCTEON},
2383 {"Cavium Networks Octeon2", Mips::AFL_EXT_OCTEON2},
2384 {"Cavium Networks OcteonP", Mips::AFL_EXT_OCTEONP},
2385 {"Cavium Networks Octeon3", Mips::AFL_EXT_OCTEON3},
2386 {"LSI R4010", Mips::AFL_EXT_4010},
2387 {"Loongson 2E", Mips::AFL_EXT_LOONGSON_2E},
2388 {"Loongson 2F", Mips::AFL_EXT_LOONGSON_2F},
2389 {"Loongson 3A", Mips::AFL_EXT_LOONGSON_3A},
2390 {"MIPS R4650", Mips::AFL_EXT_4650},
2391 {"MIPS R5900", Mips::AFL_EXT_5900},
2392 {"MIPS R10000", Mips::AFL_EXT_10000},
2393 {"NEC VR4100", Mips::AFL_EXT_4100},
2394 {"NEC VR4111/VR4181", Mips::AFL_EXT_4111},
2395 {"NEC VR4120", Mips::AFL_EXT_4120},
2396 {"NEC VR5400", Mips::AFL_EXT_5400},
2397 {"NEC VR5500", Mips::AFL_EXT_5500},
2398 {"RMI Xlr", Mips::AFL_EXT_XLR},
2399 {"Toshiba R3900", Mips::AFL_EXT_3900}
2400};
George Rimar47936762016-01-16 00:49:19 +00002401
2402static const EnumEntry<unsigned> ElfMipsASEFlags[] = {
George Rimarec895f12019-05-16 06:22:51 +00002403 {"DSP", Mips::AFL_ASE_DSP},
2404 {"DSPR2", Mips::AFL_ASE_DSPR2},
2405 {"Enhanced VA Scheme", Mips::AFL_ASE_EVA},
2406 {"MCU", Mips::AFL_ASE_MCU},
2407 {"MDMX", Mips::AFL_ASE_MDMX},
2408 {"MIPS-3D", Mips::AFL_ASE_MIPS3D},
2409 {"MT", Mips::AFL_ASE_MT},
2410 {"SmartMIPS", Mips::AFL_ASE_SMARTMIPS},
2411 {"VZ", Mips::AFL_ASE_VIRT},
2412 {"MSA", Mips::AFL_ASE_MSA},
2413 {"MIPS16", Mips::AFL_ASE_MIPS16},
2414 {"microMIPS", Mips::AFL_ASE_MICROMIPS},
2415 {"XPA", Mips::AFL_ASE_XPA},
2416 {"CRC", Mips::AFL_ASE_CRC},
2417 {"GINV", Mips::AFL_ASE_GINV},
George Rimar47936762016-01-16 00:49:19 +00002418};
2419
2420static const EnumEntry<unsigned> ElfMipsFpABIType[] = {
George Rimarec895f12019-05-16 06:22:51 +00002421 {"Hard or soft float", Mips::Val_GNU_MIPS_ABI_FP_ANY},
2422 {"Hard float (double precision)", Mips::Val_GNU_MIPS_ABI_FP_DOUBLE},
2423 {"Hard float (single precision)", Mips::Val_GNU_MIPS_ABI_FP_SINGLE},
2424 {"Soft float", Mips::Val_GNU_MIPS_ABI_FP_SOFT},
2425 {"Hard float (MIPS32r2 64-bit FPU 12 callee-saved)",
2426 Mips::Val_GNU_MIPS_ABI_FP_OLD_64},
2427 {"Hard float (32-bit CPU, Any FPU)", Mips::Val_GNU_MIPS_ABI_FP_XX},
2428 {"Hard float (32-bit CPU, 64-bit FPU)", Mips::Val_GNU_MIPS_ABI_FP_64},
2429 {"Hard float compat (32-bit CPU, 64-bit FPU)",
2430 Mips::Val_GNU_MIPS_ABI_FP_64A}
2431};
George Rimar47936762016-01-16 00:49:19 +00002432
George Rimarec895f12019-05-16 06:22:51 +00002433static const EnumEntry<unsigned> ElfMipsFlags1[] {
2434 {"ODDSPREG", Mips::AFL_FLAGS1_ODDSPREG},
George Rimar47936762016-01-16 00:49:19 +00002435};
2436
2437static int getMipsRegisterSize(uint8_t Flag) {
2438 switch (Flag) {
2439 case Mips::AFL_REG_NONE:
2440 return 0;
2441 case Mips::AFL_REG_32:
2442 return 32;
2443 case Mips::AFL_REG_64:
2444 return 64;
2445 case Mips::AFL_REG_128:
2446 return 128;
2447 default:
2448 return -1;
2449 }
2450}
2451
2452template <class ELFT> void ELFDumper<ELFT>::printMipsABIFlags() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00002453 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
George Rimar47936762016-01-16 00:49:19 +00002454 const Elf_Shdr *Shdr = findSectionByName(*Obj, ".MIPS.abiflags");
2455 if (!Shdr) {
2456 W.startLine() << "There is no .MIPS.abiflags section in the file.\n";
2457 return;
2458 }
Rafael Espindolaf04f1842016-02-17 16:21:49 +00002459 ArrayRef<uint8_t> Sec = unwrapOrError(Obj->getSectionContents(Shdr));
2460 if (Sec.size() != sizeof(Elf_Mips_ABIFlags<ELFT>)) {
George Rimar47936762016-01-16 00:49:19 +00002461 W.startLine() << "The .MIPS.abiflags section has a wrong size.\n";
2462 return;
2463 }
2464
Rafael Espindolaf04f1842016-02-17 16:21:49 +00002465 auto *Flags = reinterpret_cast<const Elf_Mips_ABIFlags<ELFT> *>(Sec.data());
George Rimar47936762016-01-16 00:49:19 +00002466
2467 raw_ostream &OS = W.getOStream();
2468 DictScope GS(W, "MIPS ABI Flags");
2469
2470 W.printNumber("Version", Flags->version);
2471 W.startLine() << "ISA: ";
2472 if (Flags->isa_rev <= 1)
2473 OS << format("MIPS%u", Flags->isa_level);
2474 else
2475 OS << format("MIPS%ur%u", Flags->isa_level, Flags->isa_rev);
2476 OS << "\n";
2477 W.printEnum("ISA Extension", Flags->isa_ext, makeArrayRef(ElfMipsISAExtType));
2478 W.printFlags("ASEs", Flags->ases, makeArrayRef(ElfMipsASEFlags));
2479 W.printEnum("FP ABI", Flags->fp_abi, makeArrayRef(ElfMipsFpABIType));
2480 W.printNumber("GPR size", getMipsRegisterSize(Flags->gpr_size));
2481 W.printNumber("CPR1 size", getMipsRegisterSize(Flags->cpr1_size));
2482 W.printNumber("CPR2 size", getMipsRegisterSize(Flags->cpr2_size));
2483 W.printFlags("Flags 1", Flags->flags1, makeArrayRef(ElfMipsFlags1));
2484 W.printHex("Flags 2", Flags->flags2);
2485}
2486
Simon Atanasyan8a71b532016-05-04 05:58:57 +00002487template <class ELFT>
2488static void printMipsReginfoData(ScopedPrinter &W,
2489 const Elf_Mips_RegInfo<ELFT> &Reginfo) {
2490 W.printHex("GP", Reginfo.ri_gp_value);
2491 W.printHex("General Mask", Reginfo.ri_gprmask);
2492 W.printHex("Co-Proc Mask0", Reginfo.ri_cprmask[0]);
2493 W.printHex("Co-Proc Mask1", Reginfo.ri_cprmask[1]);
2494 W.printHex("Co-Proc Mask2", Reginfo.ri_cprmask[2]);
2495 W.printHex("Co-Proc Mask3", Reginfo.ri_cprmask[3]);
2496}
2497
George Rimar47936762016-01-16 00:49:19 +00002498template <class ELFT> void ELFDumper<ELFT>::printMipsReginfo() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00002499 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
George Rimar47936762016-01-16 00:49:19 +00002500 const Elf_Shdr *Shdr = findSectionByName(*Obj, ".reginfo");
2501 if (!Shdr) {
2502 W.startLine() << "There is no .reginfo section in the file.\n";
2503 return;
2504 }
Rafael Espindolaf04f1842016-02-17 16:21:49 +00002505 ArrayRef<uint8_t> Sec = unwrapOrError(Obj->getSectionContents(Shdr));
2506 if (Sec.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) {
George Rimar47936762016-01-16 00:49:19 +00002507 W.startLine() << "The .reginfo section has a wrong size.\n";
2508 return;
2509 }
2510
George Rimar47936762016-01-16 00:49:19 +00002511 DictScope GS(W, "MIPS RegInfo");
Simon Atanasyan8a71b532016-05-04 05:58:57 +00002512 auto *Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(Sec.data());
2513 printMipsReginfoData(W, *Reginfo);
2514}
2515
2516template <class ELFT> void ELFDumper<ELFT>::printMipsOptions() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00002517 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
Simon Atanasyan8a71b532016-05-04 05:58:57 +00002518 const Elf_Shdr *Shdr = findSectionByName(*Obj, ".MIPS.options");
2519 if (!Shdr) {
2520 W.startLine() << "There is no .MIPS.options section in the file.\n";
2521 return;
2522 }
2523
2524 DictScope GS(W, "MIPS Options");
2525
2526 ArrayRef<uint8_t> Sec = unwrapOrError(Obj->getSectionContents(Shdr));
2527 while (!Sec.empty()) {
2528 if (Sec.size() < sizeof(Elf_Mips_Options<ELFT>)) {
2529 W.startLine() << "The .MIPS.options section has a wrong size.\n";
2530 return;
2531 }
2532 auto *O = reinterpret_cast<const Elf_Mips_Options<ELFT> *>(Sec.data());
2533 DictScope GS(W, getElfMipsOptionsOdkType(O->kind));
2534 switch (O->kind) {
2535 case ODK_REGINFO:
2536 printMipsReginfoData(W, O->getRegInfo());
2537 break;
2538 default:
2539 W.startLine() << "Unsupported MIPS options tag.\n";
2540 break;
2541 }
2542 Sec = Sec.slice(O->size);
2543 }
George Rimar47936762016-01-16 00:49:19 +00002544}
2545
2546template <class ELFT> void ELFDumper<ELFT>::printStackMap() const {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00002547 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
George Rimar47936762016-01-16 00:49:19 +00002548 const Elf_Shdr *StackMapSection = nullptr;
Rafael Espindola25be8c82016-11-02 14:10:57 +00002549 for (const auto &Sec : unwrapOrError(Obj->sections())) {
Rafael Espindolaf04f1842016-02-17 16:21:49 +00002550 StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
2551 if (Name == ".llvm_stackmaps") {
George Rimar47936762016-01-16 00:49:19 +00002552 StackMapSection = &Sec;
2553 break;
2554 }
2555 }
2556
2557 if (!StackMapSection)
2558 return;
2559
Rafael Espindolaf04f1842016-02-17 16:21:49 +00002560 ArrayRef<uint8_t> StackMapContentsArray =
2561 unwrapOrError(Obj->getSectionContents(StackMapSection));
George Rimar47936762016-01-16 00:49:19 +00002562
Sam Clegg88e9a152018-01-10 00:14:19 +00002563 prettyPrintStackMap(
Philip Reames377f5072019-04-13 02:02:56 +00002564 W, StackMapParser<ELFT::TargetEndianness>(StackMapContentsArray));
George Rimar47936762016-01-16 00:49:19 +00002565}
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002566
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +00002567template <class ELFT> void ELFDumper<ELFT>::printGroupSections() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00002568 ELFDumperStyle->printGroupSections(ObjF->getELFFile());
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +00002569}
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002570
Peter Collingbourne3e227332018-07-17 22:17:18 +00002571template <class ELFT> void ELFDumper<ELFT>::printAddrsig() {
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00002572 ELFDumperStyle->printAddrsig(ObjF->getELFFile());
Peter Collingbourne3e227332018-07-17 22:17:18 +00002573}
2574
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002575static inline void printFields(formatted_raw_ostream &OS, StringRef Str1,
2576 StringRef Str2) {
2577 OS.PadToColumn(2u);
2578 OS << Str1;
2579 OS.PadToColumn(37u);
2580 OS << Str2 << "\n";
2581 OS.flush();
2582}
2583
George Rimar6fdac3b2018-07-18 08:19:58 +00002584template <class ELFT>
2585static std::string getSectionHeadersNumString(const ELFFile<ELFT> *Obj) {
2586 const typename ELFT::Ehdr *ElfHeader = Obj->getHeader();
2587 if (ElfHeader->e_shnum != 0)
2588 return to_string(ElfHeader->e_shnum);
2589
2590 ArrayRef<typename ELFT::Shdr> Arr = unwrapOrError(Obj->sections());
2591 if (Arr.empty())
2592 return "0";
2593 return "0 (" + to_string(Arr[0].sh_size) + ")";
2594}
2595
2596template <class ELFT>
2597static std::string getSectionHeaderTableIndexString(const ELFFile<ELFT> *Obj) {
2598 const typename ELFT::Ehdr *ElfHeader = Obj->getHeader();
2599 if (ElfHeader->e_shstrndx != SHN_XINDEX)
2600 return to_string(ElfHeader->e_shstrndx);
2601
2602 ArrayRef<typename ELFT::Shdr> Arr = unwrapOrError(Obj->sections());
2603 if (Arr.empty())
2604 return "65535 (corrupt: out of range)";
George Rimar9e88a262019-05-14 14:22:44 +00002605 return to_string(ElfHeader->e_shstrndx) + " (" + to_string(Arr[0].sh_link) +
2606 ")";
George Rimar6fdac3b2018-07-18 08:19:58 +00002607}
2608
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002609template <class ELFT> void GNUStyle<ELFT>::printFileHeaders(const ELFO *Obj) {
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002610 const Elf_Ehdr *e = Obj->getHeader();
2611 OS << "ELF Header:\n";
2612 OS << " Magic: ";
2613 std::string Str;
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002614 for (int i = 0; i < ELF::EI_NIDENT; i++)
2615 OS << format(" %02x", static_cast<int>(e->e_ident[i]));
2616 OS << "\n";
2617 Str = printEnum(e->e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002618 printFields(OS, "Class:", Str);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002619 Str = printEnum(e->e_ident[ELF::EI_DATA], makeArrayRef(ElfDataEncoding));
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002620 printFields(OS, "Data:", Str);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002621 OS.PadToColumn(2u);
2622 OS << "Version:";
2623 OS.PadToColumn(37u);
2624 OS << to_hexString(e->e_ident[ELF::EI_VERSION]);
2625 if (e->e_version == ELF::EV_CURRENT)
2626 OS << " (current)";
2627 OS << "\n";
2628 Str = printEnum(e->e_ident[ELF::EI_OSABI], makeArrayRef(ElfOSABI));
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002629 printFields(OS, "OS/ABI:", Str);
Chandler Carruthe8fa5ae2016-11-03 17:11:11 +00002630 Str = "0x" + to_hexString(e->e_ident[ELF::EI_ABIVERSION]);
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002631 printFields(OS, "ABI Version:", Str);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002632 Str = printEnum(e->e_type, makeArrayRef(ElfObjectFileType));
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002633 printFields(OS, "Type:", Str);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002634 Str = printEnum(e->e_machine, makeArrayRef(ElfMachineType));
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002635 printFields(OS, "Machine:", Str);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002636 Str = "0x" + to_hexString(e->e_version);
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002637 printFields(OS, "Version:", Str);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002638 Str = "0x" + to_hexString(e->e_entry);
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002639 printFields(OS, "Entry point address:", Str);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002640 Str = to_string(e->e_phoff) + " (bytes into file)";
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002641 printFields(OS, "Start of program headers:", Str);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002642 Str = to_string(e->e_shoff) + " (bytes into file)";
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002643 printFields(OS, "Start of section headers:", Str);
Simon Atanasyan19932542018-10-25 05:39:27 +00002644 std::string ElfFlags;
2645 if (e->e_machine == EM_MIPS)
2646 ElfFlags =
2647 printFlags(e->e_flags, makeArrayRef(ElfHeaderMipsFlags),
2648 unsigned(ELF::EF_MIPS_ARCH), unsigned(ELF::EF_MIPS_ABI),
2649 unsigned(ELF::EF_MIPS_MACH));
2650 else if (e->e_machine == EM_RISCV)
2651 ElfFlags = printFlags(e->e_flags, makeArrayRef(ElfHeaderRISCVFlags));
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002652 Str = "0x" + to_hexString(e->e_flags);
Simon Atanasyan19932542018-10-25 05:39:27 +00002653 if (!ElfFlags.empty())
2654 Str = Str + ", " + ElfFlags;
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002655 printFields(OS, "Flags:", Str);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002656 Str = to_string(e->e_ehsize) + " (bytes)";
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002657 printFields(OS, "Size of this header:", Str);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002658 Str = to_string(e->e_phentsize) + " (bytes)";
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002659 printFields(OS, "Size of program headers:", Str);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002660 Str = to_string(e->e_phnum);
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002661 printFields(OS, "Number of program headers:", Str);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002662 Str = to_string(e->e_shentsize) + " (bytes)";
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002663 printFields(OS, "Size of section headers:", Str);
George Rimar6fdac3b2018-07-18 08:19:58 +00002664 Str = getSectionHeadersNumString(Obj);
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002665 printFields(OS, "Number of section headers:", Str);
George Rimar6fdac3b2018-07-18 08:19:58 +00002666 Str = getSectionHeaderTableIndexString(Obj);
Hemant Kulkarnif84cda72016-02-11 03:41:34 +00002667 printFields(OS, "Section header string table index:", Str);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002668}
2669
George Rimarea39eed2017-09-14 07:32:52 +00002670namespace {
2671struct GroupMember {
2672 StringRef Name;
2673 uint64_t Index;
2674};
2675
2676struct GroupSection {
2677 StringRef Name;
James Hendersone50d9cb2019-01-17 15:34:12 +00002678 std::string Signature;
George Rimarea39eed2017-09-14 07:32:52 +00002679 uint64_t ShName;
2680 uint64_t Index;
Alexander Shaposhnikov8febe3d2018-03-12 22:40:09 +00002681 uint32_t Link;
2682 uint32_t Info;
George Rimarea39eed2017-09-14 07:32:52 +00002683 uint32_t Type;
2684 std::vector<GroupMember> Members;
2685};
2686
2687template <class ELFT>
2688std::vector<GroupSection> getGroups(const ELFFile<ELFT> *Obj) {
Rui Ueyama478d6352018-01-12 02:28:31 +00002689 using Elf_Shdr = typename ELFT::Shdr;
2690 using Elf_Sym = typename ELFT::Sym;
2691 using Elf_Word = typename ELFT::Word;
George Rimarea39eed2017-09-14 07:32:52 +00002692
2693 std::vector<GroupSection> Ret;
2694 uint64_t I = 0;
George Rimar3c0f3962017-09-14 07:26:14 +00002695 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
George Rimarea39eed2017-09-14 07:32:52 +00002696 ++I;
2697 if (Sec.sh_type != ELF::SHT_GROUP)
2698 continue;
2699
2700 const Elf_Shdr *Symtab = unwrapOrError(Obj->getSection(Sec.sh_link));
2701 StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*Symtab));
2702 const Elf_Sym *Sym =
2703 unwrapOrError(Obj->template getEntry<Elf_Sym>(Symtab, Sec.sh_info));
2704 auto Data =
2705 unwrapOrError(Obj->template getSectionContentsAsArray<Elf_Word>(&Sec));
2706
2707 StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
2708 StringRef Signature = StrTable.data() + Sym->st_name;
James Hendersone50d9cb2019-01-17 15:34:12 +00002709 Ret.push_back({Name,
2710 maybeDemangle(Signature),
2711 Sec.sh_name,
Alexander Shaposhnikov8febe3d2018-03-12 22:40:09 +00002712 I - 1,
2713 Sec.sh_link,
2714 Sec.sh_info,
James Hendersone50d9cb2019-01-17 15:34:12 +00002715 Data[0],
Alexander Shaposhnikov8febe3d2018-03-12 22:40:09 +00002716 {}});
George Rimarea39eed2017-09-14 07:32:52 +00002717
2718 std::vector<GroupMember> &GM = Ret.back().Members;
2719 for (uint32_t Ndx : Data.slice(1)) {
2720 auto Sec = unwrapOrError(Obj->getSection(Ndx));
2721 const StringRef Name = unwrapOrError(Obj->getSectionName(Sec));
2722 GM.push_back({Name, Ndx});
George Rimar3c0f3962017-09-14 07:26:14 +00002723 }
George Rimarc2657cd2017-09-14 07:17:04 +00002724 }
George Rimarea39eed2017-09-14 07:32:52 +00002725 return Ret;
2726}
George Rimar762abff62017-09-16 14:29:51 +00002727
2728DenseMap<uint64_t, const GroupSection *>
2729mapSectionsToGroups(ArrayRef<GroupSection> Groups) {
2730 DenseMap<uint64_t, const GroupSection *> Ret;
2731 for (const GroupSection &G : Groups)
2732 for (const GroupMember &GM : G.Members)
2733 Ret.insert({GM.Index, &G});
2734 return Ret;
2735}
2736
George Rimarea39eed2017-09-14 07:32:52 +00002737} // namespace
2738
2739template <class ELFT> void GNUStyle<ELFT>::printGroupSections(const ELFO *Obj) {
2740 std::vector<GroupSection> V = getGroups<ELFT>(Obj);
George Rimar762abff62017-09-16 14:29:51 +00002741 DenseMap<uint64_t, const GroupSection *> Map = mapSectionsToGroups(V);
George Rimarea39eed2017-09-14 07:32:52 +00002742 for (const GroupSection &G : V) {
2743 OS << "\n"
2744 << getGroupType(G.Type) << " group section ["
2745 << format_decimal(G.Index, 5) << "] `" << G.Name << "' [" << G.Signature
2746 << "] contains " << G.Members.size() << " sections:\n"
2747 << " [Index] Name\n";
George Rimar762abff62017-09-16 14:29:51 +00002748 for (const GroupMember &GM : G.Members) {
2749 const GroupSection *MainGroup = Map[GM.Index];
2750 if (MainGroup != &G) {
2751 OS.flush();
2752 errs() << "Error: section [" << format_decimal(GM.Index, 5)
2753 << "] in group section [" << format_decimal(G.Index, 5)
2754 << "] already in group section ["
2755 << format_decimal(MainGroup->Index, 5) << "]";
2756 errs().flush();
2757 continue;
2758 }
George Rimarea39eed2017-09-14 07:32:52 +00002759 OS << " [" << format_decimal(GM.Index, 5) << "] " << GM.Name << "\n";
George Rimar762abff62017-09-16 14:29:51 +00002760 }
George Rimarea39eed2017-09-14 07:32:52 +00002761 }
2762
2763 if (V.empty())
Hemant Kulkarni206ba842016-03-09 19:16:13 +00002764 OS << "There are no section groups in this file.\n";
2765}
2766
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00002767template <class ELFT>
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002768void GNUStyle<ELFT>::printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab,
2769 const Elf_Rela &R, bool IsRela) {
George Rimar1206f5a2019-01-30 15:39:05 +00002770 const Elf_Sym *Sym = unwrapOrError(Obj->getRelocationSymbol(&R, SymTab));
2771 std::string TargetName;
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002772 if (Sym && Sym->getType() == ELF::STT_SECTION) {
2773 const Elf_Shdr *Sec = unwrapOrError(
2774 Obj->getSection(Sym, SymTab, this->dumper()->getShndxTable()));
2775 TargetName = unwrapOrError(Obj->getSectionName(Sec));
2776 } else if (Sym) {
Jake Ehrlich0f440d82018-06-28 21:07:34 +00002777 StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*SymTab));
Xing GUOeec32062019-03-12 14:30:13 +00002778 TargetName = this->dumper()->getFullSymbolName(
2779 Sym, StrTable, SymTab->sh_type == SHT_DYNSYM /* IsDynamic */);
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002780 }
James Henderson814ab372019-03-29 11:47:19 +00002781 printRelocation(Obj, Sym, TargetName, R, IsRela);
2782}
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002783
James Henderson814ab372019-03-29 11:47:19 +00002784template <class ELFT>
2785void GNUStyle<ELFT>::printRelocation(const ELFO *Obj, const Elf_Sym *Sym,
2786 StringRef SymbolName, const Elf_Rela &R,
2787 bool IsRela) {
2788 // First two fields are bit width dependent. The rest of them are fixed width.
2789 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
2790 Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias};
George Rimar4b4899b2019-01-30 14:08:55 +00002791 unsigned Width = ELFT::Is64Bits ? 16 : 8;
James Henderson814ab372019-03-29 11:47:19 +00002792
George Rimar4b4899b2019-01-30 14:08:55 +00002793 Fields[0].Str = to_string(format_hex_no_prefix(R.r_offset, Width));
2794 Fields[1].Str = to_string(format_hex_no_prefix(R.r_info, Width));
James Henderson814ab372019-03-29 11:47:19 +00002795
2796 SmallString<32> RelocName;
2797 Obj->getRelocationTypeName(R.getType(Obj->isMips64EL()), RelocName);
2798 Fields[2].Str = RelocName.c_str();
2799
2800 if (Sym && (!SymbolName.empty() || Sym->getValue() != 0))
George Rimar4b4899b2019-01-30 14:08:55 +00002801 Fields[3].Str = to_string(format_hex_no_prefix(Sym->getValue(), Width));
James Henderson814ab372019-03-29 11:47:19 +00002802
2803 Fields[4].Str = SymbolName;
2804 for (const Field &F : Fields)
George Rimar4b4899b2019-01-30 14:08:55 +00002805 printField(F);
2806
2807 std::string Addend;
James Hendersonb41130b2019-03-08 13:22:05 +00002808 if (IsRela) {
2809 int64_t RelAddend = R.r_addend;
James Henderson814ab372019-03-29 11:47:19 +00002810 if (!SymbolName.empty()) {
James Hendersonb41130b2019-03-08 13:22:05 +00002811 if (R.r_addend < 0) {
2812 Addend = " - ";
2813 RelAddend = std::abs(RelAddend);
2814 } else
2815 Addend = " + ";
2816 }
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002817
James Hendersonb41130b2019-03-08 13:22:05 +00002818 Addend += to_hexString(RelAddend, false);
2819 }
George Rimar1206f5a2019-01-30 15:39:05 +00002820 OS << Addend << "\n";
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00002821}
2822
Jake Ehrlich0f440d82018-06-28 21:07:34 +00002823template <class ELFT> void GNUStyle<ELFT>::printRelocHeader(unsigned SType) {
2824 bool IsRela = SType == ELF::SHT_RELA || SType == ELF::SHT_ANDROID_RELA;
2825 bool IsRelr = SType == ELF::SHT_RELR || SType == ELF::SHT_ANDROID_RELR;
2826 if (ELFT::Is64Bits)
2827 OS << " ";
2828 else
2829 OS << " ";
2830 if (IsRelr && opts::RawRelr)
2831 OS << "Data ";
2832 else
2833 OS << "Offset";
2834 if (ELFT::Is64Bits)
2835 OS << " Info Type"
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00002836 << " Symbol's Value Symbol's Name";
2837 else
Jake Ehrlich0f440d82018-06-28 21:07:34 +00002838 OS << " Info Type Sym. Value Symbol's Name";
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002839 if (IsRela)
Jake Ehrlich0f440d82018-06-28 21:07:34 +00002840 OS << " + Addend";
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002841 OS << "\n";
2842}
2843
2844template <class ELFT> void GNUStyle<ELFT>::printRelocations(const ELFO *Obj) {
2845 bool HasRelocSections = false;
Rafael Espindola25be8c82016-11-02 14:10:57 +00002846 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
George Rimar9e88a262019-05-14 14:22:44 +00002847 if (Sec.sh_type != ELF::SHT_REL && Sec.sh_type != ELF::SHT_RELA &&
2848 Sec.sh_type != ELF::SHT_RELR && Sec.sh_type != ELF::SHT_ANDROID_REL &&
Jake Ehrlich0f440d82018-06-28 21:07:34 +00002849 Sec.sh_type != ELF::SHT_ANDROID_RELA &&
2850 Sec.sh_type != ELF::SHT_ANDROID_RELR)
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002851 continue;
2852 HasRelocSections = true;
2853 StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
2854 unsigned Entries = Sec.getEntityCount();
Peter Collingbournecf017ad2018-06-07 00:02:07 +00002855 std::vector<Elf_Rela> AndroidRelas;
2856 if (Sec.sh_type == ELF::SHT_ANDROID_REL ||
2857 Sec.sh_type == ELF::SHT_ANDROID_RELA) {
2858 // Android's packed relocation section needs to be unpacked first
2859 // to get the actual number of entries.
2860 AndroidRelas = unwrapOrError(Obj->android_relas(&Sec));
2861 Entries = AndroidRelas.size();
2862 }
Jake Ehrlich0f440d82018-06-28 21:07:34 +00002863 std::vector<Elf_Rela> RelrRelas;
2864 if (!opts::RawRelr && (Sec.sh_type == ELF::SHT_RELR ||
2865 Sec.sh_type == ELF::SHT_ANDROID_RELR)) {
2866 // .relr.dyn relative relocation section needs to be unpacked first
2867 // to get the actual number of entries.
2868 Elf_Relr_Range Relrs = unwrapOrError(Obj->relrs(&Sec));
2869 RelrRelas = unwrapOrError(Obj->decode_relrs(Relrs));
2870 Entries = RelrRelas.size();
2871 }
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002872 uintX_t Offset = Sec.sh_offset;
2873 OS << "\nRelocation section '" << Name << "' at offset 0x"
2874 << to_hexString(Offset, false) << " contains " << Entries
2875 << " entries:\n";
Jake Ehrlich0f440d82018-06-28 21:07:34 +00002876 printRelocHeader(Sec.sh_type);
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002877 const Elf_Shdr *SymTab = unwrapOrError(Obj->getSection(Sec.sh_link));
Peter Collingbourne689e6c052017-10-25 03:37:12 +00002878 switch (Sec.sh_type) {
2879 case ELF::SHT_REL:
Rafael Espindola354680a2016-11-03 19:07:15 +00002880 for (const auto &R : unwrapOrError(Obj->rels(&Sec))) {
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002881 Elf_Rela Rela;
2882 Rela.r_offset = R.r_offset;
2883 Rela.r_info = R.r_info;
2884 Rela.r_addend = 0;
2885 printRelocation(Obj, SymTab, Rela, false);
2886 }
Peter Collingbourne689e6c052017-10-25 03:37:12 +00002887 break;
2888 case ELF::SHT_RELA:
Rafael Espindola354680a2016-11-03 19:07:15 +00002889 for (const auto &R : unwrapOrError(Obj->relas(&Sec)))
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002890 printRelocation(Obj, SymTab, R, true);
Peter Collingbourne689e6c052017-10-25 03:37:12 +00002891 break;
Jake Ehrlich0f440d82018-06-28 21:07:34 +00002892 case ELF::SHT_RELR:
2893 case ELF::SHT_ANDROID_RELR:
2894 if (opts::RawRelr)
2895 for (const auto &R : unwrapOrError(Obj->relrs(&Sec)))
2896 OS << to_string(format_hex_no_prefix(R, ELFT::Is64Bits ? 16 : 8))
2897 << "\n";
2898 else
2899 for (const auto &R : RelrRelas)
2900 printRelocation(Obj, SymTab, R, false);
2901 break;
Peter Collingbourne689e6c052017-10-25 03:37:12 +00002902 case ELF::SHT_ANDROID_REL:
2903 case ELF::SHT_ANDROID_RELA:
Peter Collingbournecf017ad2018-06-07 00:02:07 +00002904 for (const auto &R : AndroidRelas)
Peter Collingbourne689e6c052017-10-25 03:37:12 +00002905 printRelocation(Obj, SymTab, R, Sec.sh_type == ELF::SHT_ANDROID_RELA);
2906 break;
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002907 }
2908 }
2909 if (!HasRelocSections)
2910 OS << "\nThere are no relocations in this file.\n";
2911}
2912
Matt Davis7a24dbd2019-02-27 18:39:17 +00002913// Print the offset of a particular section from anyone of the ranges:
2914// [SHT_LOOS, SHT_HIOS], [SHT_LOPROC, SHT_HIPROC], [SHT_LOUSER, SHT_HIUSER].
2915// If 'Type' does not fall within any of those ranges, then a string is
2916// returned as '<unknown>' followed by the type value.
2917static std::string getSectionTypeOffsetString(unsigned Type) {
2918 if (Type >= SHT_LOOS && Type <= SHT_HIOS)
2919 return "LOOS+0x" + to_hexString(Type - SHT_LOOS);
2920 else if (Type >= SHT_LOPROC && Type <= SHT_HIPROC)
2921 return "LOPROC+0x" + to_hexString(Type - SHT_LOPROC);
2922 else if (Type >= SHT_LOUSER && Type <= SHT_HIUSER)
2923 return "LOUSER+0x" + to_hexString(Type - SHT_LOUSER);
2924 return "0x" + to_hexString(Type) + ": <unknown>";
2925}
2926
2927static std::string getSectionTypeString(unsigned Arch, unsigned Type) {
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002928 using namespace ELF;
Eugene Zelenko416e0592017-06-09 21:41:54 +00002929
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002930 switch (Arch) {
2931 case EM_ARM:
2932 switch (Type) {
2933 case SHT_ARM_EXIDX:
2934 return "ARM_EXIDX";
2935 case SHT_ARM_PREEMPTMAP:
2936 return "ARM_PREEMPTMAP";
2937 case SHT_ARM_ATTRIBUTES:
2938 return "ARM_ATTRIBUTES";
2939 case SHT_ARM_DEBUGOVERLAY:
2940 return "ARM_DEBUGOVERLAY";
2941 case SHT_ARM_OVERLAYSECTION:
2942 return "ARM_OVERLAYSECTION";
2943 }
Ryan Prichard0c20b5b2018-10-26 23:01:54 +00002944 break;
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002945 case EM_X86_64:
2946 switch (Type) {
2947 case SHT_X86_64_UNWIND:
2948 return "X86_64_UNWIND";
2949 }
Ryan Prichard0c20b5b2018-10-26 23:01:54 +00002950 break;
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002951 case EM_MIPS:
2952 case EM_MIPS_RS3_LE:
2953 switch (Type) {
2954 case SHT_MIPS_REGINFO:
2955 return "MIPS_REGINFO";
2956 case SHT_MIPS_OPTIONS:
2957 return "MIPS_OPTIONS";
Fangrui Song8443e882019-02-21 11:35:41 +00002958 case SHT_MIPS_DWARF:
2959 return "MIPS_DWARF";
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002960 case SHT_MIPS_ABIFLAGS:
2961 return "MIPS_ABIFLAGS";
2962 }
Ryan Prichard0c20b5b2018-10-26 23:01:54 +00002963 break;
Hemant Kulkarnic030f232016-03-15 17:25:31 +00002964 }
2965 switch (Type) {
2966 case SHT_NULL:
2967 return "NULL";
2968 case SHT_PROGBITS:
2969 return "PROGBITS";
2970 case SHT_SYMTAB:
2971 return "SYMTAB";
2972 case SHT_STRTAB:
2973 return "STRTAB";
2974 case SHT_RELA:
2975 return "RELA";
2976 case SHT_HASH:
2977 return "HASH";
2978 case SHT_DYNAMIC:
2979 return "DYNAMIC";
2980 case SHT_NOTE:
2981 return "NOTE";
2982 case SHT_NOBITS:
2983 return "NOBITS";
2984 case SHT_REL:
2985 return "REL";
2986 case SHT_SHLIB:
2987 return "SHLIB";
2988 case SHT_DYNSYM:
2989 return "DYNSYM";
2990 case SHT_INIT_ARRAY:
2991 return "INIT_ARRAY";
2992 case SHT_FINI_ARRAY:
2993 return "FINI_ARRAY";
2994 case SHT_PREINIT_ARRAY:
2995 return "PREINIT_ARRAY";
2996 case SHT_GROUP:
2997 return "GROUP";
2998 case SHT_SYMTAB_SHNDX:
2999 return "SYMTAB SECTION INDICES";
Matt Davis7a24dbd2019-02-27 18:39:17 +00003000 case SHT_ANDROID_REL:
3001 return "ANDROID_REL";
3002 case SHT_ANDROID_RELA:
3003 return "ANDROID_RELA";
Jake Ehrlich0f440d82018-06-28 21:07:34 +00003004 case SHT_RELR:
3005 case SHT_ANDROID_RELR:
3006 return "RELR";
Peter Collingbournef0e26e72017-06-14 18:52:12 +00003007 case SHT_LLVM_ODRTAB:
3008 return "LLVM_ODRTAB";
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +00003009 case SHT_LLVM_LINKER_OPTIONS:
3010 return "LLVM_LINKER_OPTIONS";
Michael J. Spencerae6eeae2018-06-02 16:33:01 +00003011 case SHT_LLVM_CALL_GRAPH_PROFILE:
3012 return "LLVM_CALL_GRAPH_PROFILE";
Peter Collingbourne3e227332018-07-17 22:17:18 +00003013 case SHT_LLVM_ADDRSIG:
3014 return "LLVM_ADDRSIG";
Ben Dunbobbin1d165152019-05-17 03:44:15 +00003015 case SHT_LLVM_DEPENDENT_LIBRARIES:
3016 return "LLVM_DEPENDENT_LIBRARIES";
Hemant Kulkarnic030f232016-03-15 17:25:31 +00003017 // FIXME: Parse processor specific GNU attributes
3018 case SHT_GNU_ATTRIBUTES:
3019 return "ATTRIBUTES";
3020 case SHT_GNU_HASH:
3021 return "GNU_HASH";
3022 case SHT_GNU_verdef:
3023 return "VERDEF";
3024 case SHT_GNU_verneed:
3025 return "VERNEED";
3026 case SHT_GNU_versym:
3027 return "VERSYM";
3028 default:
Matt Davis7a24dbd2019-02-27 18:39:17 +00003029 return getSectionTypeOffsetString(Type);
Hemant Kulkarnic030f232016-03-15 17:25:31 +00003030 }
3031 return "";
3032}
3033
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +00003034template <class ELFT>
George Rimard6df7de2019-06-14 11:56:10 +00003035static StringRef getSectionName(const typename ELFT::Shdr &Sec,
George Rimara1370872019-07-16 11:07:30 +00003036 const ELFObjectFile<ELFT> &ElfObj,
George Rimard6df7de2019-06-14 11:56:10 +00003037 ArrayRef<typename ELFT::Shdr> Sections) {
George Rimara1370872019-07-16 11:07:30 +00003038 const ELFFile<ELFT> &Obj = *ElfObj.getELFFile();
George Rimard6df7de2019-06-14 11:56:10 +00003039 uint32_t Index = Obj.getHeader()->e_shstrndx;
3040 if (Index == ELF::SHN_XINDEX)
3041 Index = Sections[0].sh_link;
3042 if (!Index) // no section string table.
3043 return "";
George Rimara1370872019-07-16 11:07:30 +00003044 // TODO: Test a case when the sh_link of the section with index 0 is broken.
George Rimard6df7de2019-06-14 11:56:10 +00003045 if (Index >= Sections.size())
George Rimara1370872019-07-16 11:07:30 +00003046 reportError(ElfObj.getFileName(),
3047 createError("section header string table index " +
3048 Twine(Index) + " does not exist"));
George Rimard6df7de2019-06-14 11:56:10 +00003049 StringRef Data = toStringRef(unwrapOrError(
3050 Obj.template getSectionContentsAsArray<uint8_t>(&Sections[Index])));
3051 return unwrapOrError(Obj.getSectionName(&Sec, Data));
3052}
3053
3054template <class ELFT>
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +00003055void GNUStyle<ELFT>::printSectionHeaders(const ELFO *Obj) {
George Rimar4b4899b2019-01-30 14:08:55 +00003056 unsigned Bias = ELFT::Is64Bits ? 0 : 8;
George Rimara2b553b2018-07-19 14:52:57 +00003057 ArrayRef<Elf_Shdr> Sections = unwrapOrError(Obj->sections());
3058 OS << "There are " << to_string(Sections.size())
Hemant Kulkarnic030f232016-03-15 17:25:31 +00003059 << " section headers, starting at offset "
3060 << "0x" << to_hexString(Obj->getHeader()->e_shoff, false) << ":\n\n";
3061 OS << "Section Headers:\n";
George Rimar4b4899b2019-01-30 14:08:55 +00003062 Field Fields[11] = {
3063 {"[Nr]", 2}, {"Name", 7}, {"Type", 25},
3064 {"Address", 41}, {"Off", 58 - Bias}, {"Size", 65 - Bias},
3065 {"ES", 72 - Bias}, {"Flg", 75 - Bias}, {"Lk", 79 - Bias},
3066 {"Inf", 82 - Bias}, {"Al", 86 - Bias}};
3067 for (auto &F : Fields)
3068 printField(F);
Hemant Kulkarnic030f232016-03-15 17:25:31 +00003069 OS << "\n";
3070
George Rimara1370872019-07-16 11:07:30 +00003071 const ELFObjectFile<ELFT> *ElfObj = this->dumper()->getElfObject();
George Rimar4b4899b2019-01-30 14:08:55 +00003072 size_t SectionIndex = 0;
George Rimara2b553b2018-07-19 14:52:57 +00003073 for (const Elf_Shdr &Sec : Sections) {
George Rimar4b4899b2019-01-30 14:08:55 +00003074 Fields[0].Str = to_string(SectionIndex);
George Rimara1370872019-07-16 11:07:30 +00003075 Fields[1].Str = getSectionName(Sec, *ElfObj, Sections);
George Rimar4b4899b2019-01-30 14:08:55 +00003076 Fields[2].Str =
3077 getSectionTypeString(Obj->getHeader()->e_machine, Sec.sh_type);
3078 Fields[3].Str =
3079 to_string(format_hex_no_prefix(Sec.sh_addr, ELFT::Is64Bits ? 16 : 8));
3080 Fields[4].Str = to_string(format_hex_no_prefix(Sec.sh_offset, 6));
3081 Fields[5].Str = to_string(format_hex_no_prefix(Sec.sh_size, 6));
3082 Fields[6].Str = to_string(format_hex_no_prefix(Sec.sh_entsize, 2));
3083 Fields[7].Str = getGNUFlags(Sec.sh_flags);
3084 Fields[8].Str = to_string(Sec.sh_link);
3085 Fields[9].Str = to_string(Sec.sh_info);
3086 Fields[10].Str = to_string(Sec.sh_addralign);
3087
Hemant Kulkarnic030f232016-03-15 17:25:31 +00003088 OS.PadToColumn(Fields[0].Column);
3089 OS << "[" << right_justify(Fields[0].Str, 2) << "]";
3090 for (int i = 1; i < 7; i++)
3091 printField(Fields[i]);
3092 OS.PadToColumn(Fields[7].Column);
3093 OS << right_justify(Fields[7].Str, 3);
3094 OS.PadToColumn(Fields[8].Column);
3095 OS << right_justify(Fields[8].Str, 2);
3096 OS.PadToColumn(Fields[9].Column);
3097 OS << right_justify(Fields[9].Str, 3);
3098 OS.PadToColumn(Fields[10].Column);
3099 OS << right_justify(Fields[10].Str, 2);
3100 OS << "\n";
3101 ++SectionIndex;
3102 }
3103 OS << "Key to Flags:\n"
3104 << " W (write), A (alloc), X (execute), M (merge), S (strings), l "
3105 "(large)\n"
3106 << " I (info), L (link order), G (group), T (TLS), E (exclude),\
3107 x (unknown)\n"
3108 << " O (extra OS processing required) o (OS specific),\
3109 p (processor specific)\n";
3110}
3111
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +00003112template <class ELFT>
3113void GNUStyle<ELFT>::printSymtabMessage(const ELFO *Obj, StringRef Name,
3114 size_t Entries) {
Eugene Zelenko416e0592017-06-09 21:41:54 +00003115 if (!Name.empty())
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +00003116 OS << "\nSymbol table '" << Name << "' contains " << Entries
3117 << " entries:\n";
3118 else
3119 OS << "\n Symbol table for image:\n";
3120
3121 if (ELFT::Is64Bits)
3122 OS << " Num: Value Size Type Bind Vis Ndx Name\n";
3123 else
3124 OS << " Num: Value Size Type Bind Vis Ndx Name\n";
3125}
3126
3127template <class ELFT>
3128std::string GNUStyle<ELFT>::getSymbolSectionNdx(const ELFO *Obj,
3129 const Elf_Sym *Symbol,
3130 const Elf_Sym *FirstSym) {
3131 unsigned SectionIndex = Symbol->st_shndx;
3132 switch (SectionIndex) {
3133 case ELF::SHN_UNDEF:
3134 return "UND";
3135 case ELF::SHN_ABS:
3136 return "ABS";
3137 case ELF::SHN_COMMON:
3138 return "COM";
3139 case ELF::SHN_XINDEX:
Eugene Leviant49187382019-04-15 11:21:47 +00003140 return to_string(
3141 format_decimal(unwrapOrError(object::getExtendedSymbolTableIndex<ELFT>(
3142 Symbol, FirstSym, this->dumper()->getShndxTable())),
3143 3));
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +00003144 default:
3145 // Find if:
3146 // Processor specific
3147 if (SectionIndex >= ELF::SHN_LOPROC && SectionIndex <= ELF::SHN_HIPROC)
3148 return std::string("PRC[0x") +
3149 to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3150 // OS specific
3151 if (SectionIndex >= ELF::SHN_LOOS && SectionIndex <= ELF::SHN_HIOS)
3152 return std::string("OS[0x") +
3153 to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3154 // Architecture reserved:
3155 if (SectionIndex >= ELF::SHN_LORESERVE &&
3156 SectionIndex <= ELF::SHN_HIRESERVE)
3157 return std::string("RSV[0x") +
3158 to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3159 // A normal section with an index
3160 return to_string(format_decimal(SectionIndex, 3));
3161 }
3162}
3163
3164template <class ELFT>
3165void GNUStyle<ELFT>::printSymbol(const ELFO *Obj, const Elf_Sym *Symbol,
3166 const Elf_Sym *FirstSym, StringRef StrTable,
3167 bool IsDynamic) {
3168 static int Idx = 0;
3169 static bool Dynamic = true;
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +00003170
3171 // If this function was called with a different value from IsDynamic
3172 // from last call, happens when we move from dynamic to static symbol
3173 // table, "Num" field should be reset.
3174 if (!Dynamic != !IsDynamic) {
3175 Idx = 0;
3176 Dynamic = false;
3177 }
George Rimar4b4899b2019-01-30 14:08:55 +00003178
3179 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +00003180 Field Fields[8] = {0, 8, 17 + Bias, 23 + Bias,
3181 31 + Bias, 38 + Bias, 47 + Bias, 51 + Bias};
George Rimar4b4899b2019-01-30 14:08:55 +00003182 Fields[0].Str = to_string(format_decimal(Idx++, 6)) + ":";
3183 Fields[1].Str = to_string(
3184 format_hex_no_prefix(Symbol->st_value, ELFT::Is64Bits ? 16 : 8));
3185 Fields[2].Str = to_string(format_decimal(Symbol->st_size, 5));
3186
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +00003187 unsigned char SymbolType = Symbol->getType();
3188 if (Obj->getHeader()->e_machine == ELF::EM_AMDGPU &&
3189 SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
George Rimar4b4899b2019-01-30 14:08:55 +00003190 Fields[3].Str = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +00003191 else
George Rimar4b4899b2019-01-30 14:08:55 +00003192 Fields[3].Str = printEnum(SymbolType, makeArrayRef(ElfSymbolTypes));
3193
3194 Fields[4].Str =
3195 printEnum(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
3196 Fields[5].Str =
3197 printEnum(Symbol->getVisibility(), makeArrayRef(ElfSymbolVisibilities));
3198 Fields[6].Str = getSymbolSectionNdx(Obj, Symbol, FirstSym);
3199 Fields[7].Str =
3200 this->dumper()->getFullSymbolName(Symbol, StrTable, IsDynamic);
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +00003201 for (auto &Entry : Fields)
3202 printField(Entry);
3203 OS << "\n";
3204}
George Rimar4b4899b2019-01-30 14:08:55 +00003205
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003206template <class ELFT>
3207void GNUStyle<ELFT>::printHashedSymbol(const ELFO *Obj, const Elf_Sym *FirstSym,
3208 uint32_t Sym, StringRef StrTable,
3209 uint32_t Bucket) {
George Rimar1206f5a2019-01-30 15:39:05 +00003210 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003211 Field Fields[9] = {0, 6, 11, 20 + Bias, 25 + Bias,
3212 34 + Bias, 41 + Bias, 49 + Bias, 53 + Bias};
George Rimar4b4899b2019-01-30 14:08:55 +00003213 Fields[0].Str = to_string(format_decimal(Sym, 5));
3214 Fields[1].Str = to_string(format_decimal(Bucket, 3)) + ":";
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003215
3216 const auto Symbol = FirstSym + Sym;
George Rimar1206f5a2019-01-30 15:39:05 +00003217 Fields[2].Str = to_string(
George Rimarce2ef282019-07-19 10:15:03 +00003218 format_hex_no_prefix(Symbol->st_value, ELFT::Is64Bits ? 16 : 8));
George Rimar4b4899b2019-01-30 14:08:55 +00003219 Fields[3].Str = to_string(format_decimal(Symbol->st_size, 5));
3220
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003221 unsigned char SymbolType = Symbol->getType();
3222 if (Obj->getHeader()->e_machine == ELF::EM_AMDGPU &&
3223 SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
George Rimar4b4899b2019-01-30 14:08:55 +00003224 Fields[4].Str = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003225 else
George Rimar4b4899b2019-01-30 14:08:55 +00003226 Fields[4].Str = printEnum(SymbolType, makeArrayRef(ElfSymbolTypes));
3227
3228 Fields[5].Str =
3229 printEnum(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
3230 Fields[6].Str =
3231 printEnum(Symbol->getVisibility(), makeArrayRef(ElfSymbolVisibilities));
3232 Fields[7].Str = getSymbolSectionNdx(Obj, Symbol, FirstSym);
3233 Fields[8].Str = this->dumper()->getFullSymbolName(Symbol, StrTable, true);
3234
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003235 for (auto &Entry : Fields)
3236 printField(Entry);
3237 OS << "\n";
3238}
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +00003239
Hemant Kulkarnic030f232016-03-15 17:25:31 +00003240template <class ELFT>
James Henderson21ed8682019-01-23 16:15:39 +00003241void GNUStyle<ELFT>::printSymbols(const ELFO *Obj, bool PrintSymbols,
3242 bool PrintDynamicSymbols) {
3243 if (!PrintSymbols && !PrintDynamicSymbols)
3244 return;
3245 // GNU readelf prints both the .dynsym and .symtab with --symbols.
James Henderson5fc812f2019-01-22 09:35:35 +00003246 this->dumper()->printSymbolsHelper(true);
James Henderson21ed8682019-01-23 16:15:39 +00003247 if (PrintSymbols)
3248 this->dumper()->printSymbolsHelper(false);
James Henderson5fc812f2019-01-22 09:35:35 +00003249}
3250
3251template <class ELFT> void GNUStyle<ELFT>::printHashSymbols(const ELFO *Obj) {
Eugene Zelenko416e0592017-06-09 21:41:54 +00003252 if (this->dumper()->getDynamicStringTable().empty())
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003253 return;
3254 auto StringTable = this->dumper()->getDynamicStringTable();
3255 auto DynSyms = this->dumper()->dynamic_symbols();
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003256
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003257 // Try printing .hash
George Rimar1206f5a2019-01-30 15:39:05 +00003258 if (auto SysVHash = this->dumper()->getHashTable()) {
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003259 OS << "\n Symbol table of .hash for image:\n";
3260 if (ELFT::Is64Bits)
3261 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3262 else
3263 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3264 OS << "\n";
3265
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003266 auto Buckets = SysVHash->buckets();
3267 auto Chains = SysVHash->chains();
George Rimar1206f5a2019-01-30 15:39:05 +00003268 for (uint32_t Buc = 0; Buc < SysVHash->nbucket; Buc++) {
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003269 if (Buckets[Buc] == ELF::STN_UNDEF)
3270 continue;
George Rimar1206f5a2019-01-30 15:39:05 +00003271 for (uint32_t Ch = Buckets[Buc]; Ch < SysVHash->nchain; Ch = Chains[Ch]) {
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003272 if (Ch == ELF::STN_UNDEF)
3273 break;
3274 printHashedSymbol(Obj, &DynSyms[0], Ch, StringTable, Buc);
3275 }
3276 }
3277 }
3278
3279 // Try printing .gnu.hash
George Rimar1206f5a2019-01-30 15:39:05 +00003280 if (auto GnuHash = this->dumper()->getGnuHashTable()) {
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003281 OS << "\n Symbol table of .gnu.hash for image:\n";
3282 if (ELFT::Is64Bits)
3283 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3284 else
3285 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3286 OS << "\n";
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003287 auto Buckets = GnuHash->buckets();
George Rimar1206f5a2019-01-30 15:39:05 +00003288 for (uint32_t Buc = 0; Buc < GnuHash->nbuckets; Buc++) {
Hemant Kulkarnia6ee9fd2016-11-23 18:04:23 +00003289 if (Buckets[Buc] == ELF::STN_UNDEF)
3290 continue;
3291 uint32_t Index = Buckets[Buc];
3292 uint32_t GnuHashable = Index - GnuHash->symndx;
3293 // Print whole chain
3294 while (true) {
3295 printHashedSymbol(Obj, &DynSyms[0], Index++, StringTable, Buc);
3296 // Chain ends at symbol with stopper bit
3297 if ((GnuHash->values(DynSyms.size())[GnuHashable++] & 1) == 1)
3298 break;
3299 }
3300 }
3301 }
Hemant Kulkarnic030f232016-03-15 17:25:31 +00003302}
3303
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00003304static inline std::string printPhdrFlags(unsigned Flag) {
3305 std::string Str;
3306 Str = (Flag & PF_R) ? "R" : " ";
3307 Str += (Flag & PF_W) ? "W" : " ";
3308 Str += (Flag & PF_X) ? "E" : " ";
3309 return Str;
3310}
3311
3312// SHF_TLS sections are only in PT_TLS, PT_LOAD or PT_GNU_RELRO
3313// PT_TLS must only have SHF_TLS sections
3314template <class ELFT>
3315bool GNUStyle<ELFT>::checkTLSSections(const Elf_Phdr &Phdr,
3316 const Elf_Shdr &Sec) {
3317 return (((Sec.sh_flags & ELF::SHF_TLS) &&
3318 ((Phdr.p_type == ELF::PT_TLS) || (Phdr.p_type == ELF::PT_LOAD) ||
3319 (Phdr.p_type == ELF::PT_GNU_RELRO))) ||
3320 (!(Sec.sh_flags & ELF::SHF_TLS) && Phdr.p_type != ELF::PT_TLS));
3321}
3322
3323// Non-SHT_NOBITS must have its offset inside the segment
3324// Only non-zero section can be at end of segment
3325template <class ELFT>
3326bool GNUStyle<ELFT>::checkoffsets(const Elf_Phdr &Phdr, const Elf_Shdr &Sec) {
3327 if (Sec.sh_type == ELF::SHT_NOBITS)
3328 return true;
3329 bool IsSpecial =
3330 (Sec.sh_type == ELF::SHT_NOBITS) && ((Sec.sh_flags & ELF::SHF_TLS) != 0);
3331 // .tbss is special, it only has memory in PT_TLS and has NOBITS properties
3332 auto SectionSize =
3333 (IsSpecial && Phdr.p_type != ELF::PT_TLS) ? 0 : Sec.sh_size;
3334 if (Sec.sh_offset >= Phdr.p_offset)
3335 return ((Sec.sh_offset + SectionSize <= Phdr.p_filesz + Phdr.p_offset)
George Rimar9e88a262019-05-14 14:22:44 +00003336 /*only non-zero sized sections at end*/
3337 && (Sec.sh_offset + 1 <= Phdr.p_offset + Phdr.p_filesz));
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00003338 return false;
3339}
3340
3341// SHF_ALLOC must have VMA inside segment
3342// Only non-zero section can be at end of segment
3343template <class ELFT>
3344bool GNUStyle<ELFT>::checkVMA(const Elf_Phdr &Phdr, const Elf_Shdr &Sec) {
3345 if (!(Sec.sh_flags & ELF::SHF_ALLOC))
3346 return true;
3347 bool IsSpecial =
3348 (Sec.sh_type == ELF::SHT_NOBITS) && ((Sec.sh_flags & ELF::SHF_TLS) != 0);
3349 // .tbss is special, it only has memory in PT_TLS and has NOBITS properties
3350 auto SectionSize =
3351 (IsSpecial && Phdr.p_type != ELF::PT_TLS) ? 0 : Sec.sh_size;
3352 if (Sec.sh_addr >= Phdr.p_vaddr)
3353 return ((Sec.sh_addr + SectionSize <= Phdr.p_vaddr + Phdr.p_memsz) &&
3354 (Sec.sh_addr + 1 <= Phdr.p_vaddr + Phdr.p_memsz));
3355 return false;
3356}
3357
3358// No section with zero size must be at start or end of PT_DYNAMIC
3359template <class ELFT>
3360bool GNUStyle<ELFT>::checkPTDynamic(const Elf_Phdr &Phdr, const Elf_Shdr &Sec) {
3361 if (Phdr.p_type != ELF::PT_DYNAMIC || Sec.sh_size != 0 || Phdr.p_memsz == 0)
3362 return true;
3363 // Is section within the phdr both based on offset and VMA ?
3364 return ((Sec.sh_type == ELF::SHT_NOBITS) ||
3365 (Sec.sh_offset > Phdr.p_offset &&
3366 Sec.sh_offset < Phdr.p_offset + Phdr.p_filesz)) &&
3367 (!(Sec.sh_flags & ELF::SHF_ALLOC) ||
3368 (Sec.sh_addr > Phdr.p_vaddr && Sec.sh_addr < Phdr.p_memsz));
3369}
3370
3371template <class ELFT>
Matt Davis50ca8ed2019-02-01 18:51:10 +00003372void GNUStyle<ELFT>::printProgramHeaders(
3373 const ELFO *Obj, bool PrintProgramHeaders,
3374 cl::boolOrDefault PrintSectionMapping) {
3375 if (PrintProgramHeaders)
3376 printProgramHeaders(Obj);
3377
3378 // Display the section mapping along with the program headers, unless
3379 // -section-mapping is explicitly set to false.
3380 if (PrintSectionMapping != cl::BOU_FALSE)
3381 printSectionMapping(Obj);
3382}
3383
3384template <class ELFT>
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00003385void GNUStyle<ELFT>::printProgramHeaders(const ELFO *Obj) {
Hemant Kulkarni2e3254e2016-03-29 14:20:20 +00003386 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00003387 const Elf_Ehdr *Header = Obj->getHeader();
3388 Field Fields[8] = {2, 17, 26, 37 + Bias,
3389 48 + Bias, 56 + Bias, 64 + Bias, 68 + Bias};
3390 OS << "\nElf file type is "
3391 << printEnum(Header->e_type, makeArrayRef(ElfObjectFileType)) << "\n"
Hemant Kulkarni787c2ed2016-05-12 22:51:26 +00003392 << "Entry point " << format_hex(Header->e_entry, 3) << "\n"
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00003393 << "There are " << Header->e_phnum << " program headers,"
3394 << " starting at offset " << Header->e_phoff << "\n\n"
3395 << "Program Headers:\n";
3396 if (ELFT::Is64Bits)
3397 OS << " Type Offset VirtAddr PhysAddr "
3398 << " FileSiz MemSiz Flg Align\n";
3399 else
3400 OS << " Type Offset VirtAddr PhysAddr FileSiz "
3401 << "MemSiz Flg Align\n";
George Rimar1206f5a2019-01-30 15:39:05 +00003402
3403 unsigned Width = ELFT::Is64Bits ? 18 : 10;
3404 unsigned SizeWidth = ELFT::Is64Bits ? 8 : 7;
Rafael Espindola6a494972016-11-03 17:28:33 +00003405 for (const auto &Phdr : unwrapOrError(Obj->program_headers())) {
George Rimar4b4899b2019-01-30 14:08:55 +00003406 Fields[0].Str = getElfPtType(Header->e_machine, Phdr.p_type);
3407 Fields[1].Str = to_string(format_hex(Phdr.p_offset, 8));
3408 Fields[2].Str = to_string(format_hex(Phdr.p_vaddr, Width));
3409 Fields[3].Str = to_string(format_hex(Phdr.p_paddr, Width));
3410 Fields[4].Str = to_string(format_hex(Phdr.p_filesz, SizeWidth));
3411 Fields[5].Str = to_string(format_hex(Phdr.p_memsz, SizeWidth));
3412 Fields[6].Str = printPhdrFlags(Phdr.p_flags);
3413 Fields[7].Str = to_string(format_hex(Phdr.p_align, 1));
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00003414 for (auto Field : Fields)
3415 printField(Field);
3416 if (Phdr.p_type == ELF::PT_INTERP) {
3417 OS << "\n [Requesting program interpreter: ";
3418 OS << reinterpret_cast<const char *>(Obj->base()) + Phdr.p_offset << "]";
3419 }
3420 OS << "\n";
3421 }
Matt Davis50ca8ed2019-02-01 18:51:10 +00003422}
3423
3424template <class ELFT>
3425void GNUStyle<ELFT>::printSectionMapping(const ELFO *Obj) {
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00003426 OS << "\n Section to Segment mapping:\n Segment Sections...\n";
Matt Davis0d0e9c02019-02-05 21:01:01 +00003427 DenseSet<const Elf_Shdr *> BelongsToSegment;
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00003428 int Phnum = 0;
Rafael Espindola6a494972016-11-03 17:28:33 +00003429 for (const Elf_Phdr &Phdr : unwrapOrError(Obj->program_headers())) {
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00003430 std::string Sections;
3431 OS << format(" %2.2d ", Phnum++);
Rafael Espindola25be8c82016-11-02 14:10:57 +00003432 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00003433 // Check if each section is in a segment and then print mapping.
3434 // readelf additionally makes sure it does not print zero sized sections
3435 // at end of segments and for PT_DYNAMIC both start and end of section
3436 // .tbss must only be shown in PT_TLS section.
3437 bool TbssInNonTLS = (Sec.sh_type == ELF::SHT_NOBITS) &&
3438 ((Sec.sh_flags & ELF::SHF_TLS) != 0) &&
3439 Phdr.p_type != ELF::PT_TLS;
3440 if (!TbssInNonTLS && checkTLSSections(Phdr, Sec) &&
3441 checkoffsets(Phdr, Sec) && checkVMA(Phdr, Sec) &&
Matt Davis0d0e9c02019-02-05 21:01:01 +00003442 checkPTDynamic(Phdr, Sec) && (Sec.sh_type != ELF::SHT_NULL)) {
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00003443 Sections += unwrapOrError(Obj->getSectionName(&Sec)).str() + " ";
Matt Davis0d0e9c02019-02-05 21:01:01 +00003444 BelongsToSegment.insert(&Sec);
3445 }
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00003446 }
3447 OS << Sections << "\n";
3448 OS.flush();
3449 }
Matt Davis0d0e9c02019-02-05 21:01:01 +00003450
3451 // Display sections that do not belong to a segment.
3452 std::string Sections;
3453 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
3454 if (BelongsToSegment.find(&Sec) == BelongsToSegment.end())
3455 Sections += unwrapOrError(Obj->getSectionName(&Sec)).str() + ' ';
3456 }
3457 if (!Sections.empty()) {
3458 OS << " None " << Sections << '\n';
3459 OS.flush();
3460 }
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00003461}
3462
Hemant Kulkarnic030f232016-03-15 17:25:31 +00003463template <class ELFT>
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00003464void GNUStyle<ELFT>::printDynamicRelocation(const ELFO *Obj, Elf_Rela R,
3465 bool IsRela) {
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00003466 uint32_t SymIndex = R.getSymbol(Obj->isMips64EL());
3467 const Elf_Sym *Sym = this->dumper()->dynamic_symbols().begin() + SymIndex;
George Rimar4b4899b2019-01-30 14:08:55 +00003468 std::string SymbolName = maybeDemangle(
James Hendersone50d9cb2019-01-17 15:34:12 +00003469 unwrapOrError(Sym->getName(this->dumper()->getDynamicStringTable())));
James Henderson814ab372019-03-29 11:47:19 +00003470 printRelocation(Obj, Sym, SymbolName, R, IsRela);
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00003471}
3472
Simon Atanasyan8362cbe2019-05-24 12:22:53 +00003473template <class ELFT> void GNUStyle<ELFT>::printDynamic(const ELFO *Obj) {
3474 Elf_Dyn_Range Table = this->dumper()->dynamic_table();
3475 if (Table.empty())
3476 return;
3477
3478 const DynRegionInfo &DynamicTableRegion =
3479 this->dumper()->getDynamicTableRegion();
3480
3481 OS << "Dynamic section at offset "
3482 << format_hex(reinterpret_cast<const uint8_t *>(DynamicTableRegion.Addr) -
3483 Obj->base(),
3484 1)
3485 << " contains " << Table.size() << " entries:\n";
3486
3487 bool Is64 = ELFT::Is64Bits;
3488 if (Is64)
3489 OS << " Tag Type Name/Value\n";
3490 else
3491 OS << " Tag Type Name/Value\n";
3492 for (auto Entry : Table) {
3493 uintX_t Tag = Entry.getTag();
Hans Wennborg80343a32019-05-28 12:30:35 +00003494 std::string TypeString = std::string("(") +
3495 getTypeString(Obj->getHeader()->e_machine, Tag) +
3496 ")";
3497 OS << " " << format_hex(Tag, Is64 ? 18 : 10)
3498 << format(" %-20s ", TypeString.c_str());
Simon Atanasyan8362cbe2019-05-24 12:22:53 +00003499 this->dumper()->printDynamicEntry(OS, Tag, Entry.getVal());
3500 OS << "\n";
3501 }
3502}
3503
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00003504template <class ELFT>
Hemant Kulkarnic030f232016-03-15 17:25:31 +00003505void GNUStyle<ELFT>::printDynamicRelocations(const ELFO *Obj) {
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00003506 const DynRegionInfo &DynRelRegion = this->dumper()->getDynRelRegion();
3507 const DynRegionInfo &DynRelaRegion = this->dumper()->getDynRelaRegion();
Jake Ehrlich0f440d82018-06-28 21:07:34 +00003508 const DynRegionInfo &DynRelrRegion = this->dumper()->getDynRelrRegion();
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00003509 const DynRegionInfo &DynPLTRelRegion = this->dumper()->getDynPLTRelRegion();
3510 if (DynRelaRegion.Size > 0) {
3511 OS << "\n'RELA' relocation section at offset "
3512 << format_hex(reinterpret_cast<const uint8_t *>(DynRelaRegion.Addr) -
3513 Obj->base(),
George Rimar9e88a262019-05-14 14:22:44 +00003514 1)
3515 << " contains " << DynRelaRegion.Size << " bytes:\n";
Jake Ehrlich0f440d82018-06-28 21:07:34 +00003516 printRelocHeader(ELF::SHT_RELA);
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00003517 for (const Elf_Rela &Rela : this->dumper()->dyn_relas())
3518 printDynamicRelocation(Obj, Rela, true);
3519 }
3520 if (DynRelRegion.Size > 0) {
3521 OS << "\n'REL' relocation section at offset "
3522 << format_hex(reinterpret_cast<const uint8_t *>(DynRelRegion.Addr) -
3523 Obj->base(),
George Rimar9e88a262019-05-14 14:22:44 +00003524 1)
3525 << " contains " << DynRelRegion.Size << " bytes:\n";
Jake Ehrlich0f440d82018-06-28 21:07:34 +00003526 printRelocHeader(ELF::SHT_REL);
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00003527 for (const Elf_Rel &Rel : this->dumper()->dyn_rels()) {
3528 Elf_Rela Rela;
3529 Rela.r_offset = Rel.r_offset;
3530 Rela.r_info = Rel.r_info;
3531 Rela.r_addend = 0;
3532 printDynamicRelocation(Obj, Rela, false);
3533 }
3534 }
Jake Ehrlich0f440d82018-06-28 21:07:34 +00003535 if (DynRelrRegion.Size > 0) {
3536 OS << "\n'RELR' relocation section at offset "
3537 << format_hex(reinterpret_cast<const uint8_t *>(DynRelrRegion.Addr) -
3538 Obj->base(),
George Rimar9e88a262019-05-14 14:22:44 +00003539 1)
3540 << " contains " << DynRelrRegion.Size << " bytes:\n";
Jake Ehrlich0f440d82018-06-28 21:07:34 +00003541 printRelocHeader(ELF::SHT_REL);
3542 Elf_Relr_Range Relrs = this->dumper()->dyn_relrs();
3543 std::vector<Elf_Rela> RelrRelas = unwrapOrError(Obj->decode_relrs(Relrs));
3544 for (const Elf_Rela &Rela : RelrRelas) {
3545 printDynamicRelocation(Obj, Rela, false);
3546 }
3547 }
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00003548 if (DynPLTRelRegion.Size) {
3549 OS << "\n'PLT' relocation section at offset "
3550 << format_hex(reinterpret_cast<const uint8_t *>(DynPLTRelRegion.Addr) -
3551 Obj->base(),
George Rimar9e88a262019-05-14 14:22:44 +00003552 1)
3553 << " contains " << DynPLTRelRegion.Size << " bytes:\n";
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00003554 }
3555 if (DynPLTRelRegion.EntSize == sizeof(Elf_Rela)) {
Jake Ehrlich0f440d82018-06-28 21:07:34 +00003556 printRelocHeader(ELF::SHT_RELA);
Rafael Espindolaaafcf752016-04-05 14:47:22 +00003557 for (const Elf_Rela &Rela : DynPLTRelRegion.getAsArrayRef<Elf_Rela>())
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00003558 printDynamicRelocation(Obj, Rela, true);
3559 } else {
Jake Ehrlich0f440d82018-06-28 21:07:34 +00003560 printRelocHeader(ELF::SHT_REL);
Rafael Espindolaaafcf752016-04-05 14:47:22 +00003561 for (const Elf_Rel &Rel : DynPLTRelRegion.getAsArrayRef<Elf_Rel>()) {
Hemant Kulkarnia79c7982016-03-29 02:41:49 +00003562 Elf_Rela Rela;
3563 Rela.r_offset = Rel.r_offset;
3564 Rela.r_info = Rel.r_info;
3565 Rela.r_addend = 0;
3566 printDynamicRelocation(Obj, Rela, false);
3567 }
3568 }
Hemant Kulkarnic030f232016-03-15 17:25:31 +00003569}
3570
Xing GUOea16be12019-03-25 11:02:49 +00003571template <class ELFT>
George Rimare3406c42019-05-30 10:14:41 +00003572static void printGNUVersionSectionProlog(formatted_raw_ostream &OS,
3573 const Twine &Name, unsigned EntriesNum,
3574 const ELFFile<ELFT> *Obj,
3575 const typename ELFT::Shdr *Sec) {
3576 StringRef SecName = unwrapOrError(Obj->getSectionName(Sec));
3577 OS << Name << " section '" << SecName << "' "
3578 << "contains " << EntriesNum << " entries:\n";
3579
3580 const typename ELFT::Shdr *SymTab =
3581 unwrapOrError(Obj->getSection(Sec->sh_link));
3582 StringRef SymTabName = unwrapOrError(Obj->getSectionName(SymTab));
3583 OS << " Addr: " << format_hex_no_prefix(Sec->sh_addr, 16)
3584 << " Offset: " << format_hex(Sec->sh_offset, 8)
3585 << " Link: " << Sec->sh_link << " (" << SymTabName << ")\n";
3586}
3587
3588template <class ELFT>
Xing GUOea16be12019-03-25 11:02:49 +00003589void GNUStyle<ELFT>::printVersionSymbolSection(const ELFFile<ELFT> *Obj,
3590 const Elf_Shdr *Sec) {
3591 if (!Sec)
3592 return;
3593
George Rimare3406c42019-05-30 10:14:41 +00003594 unsigned Entries = Sec->sh_size / sizeof(Elf_Versym);
3595 printGNUVersionSectionProlog(OS, "Version symbols", Entries, Obj, Sec);
Xing GUO8f6166a2019-04-03 13:32:49 +00003596
3597 const uint8_t *VersymBuf =
3598 reinterpret_cast<const uint8_t *>(Obj->base() + Sec->sh_offset);
3599 const ELFDumper<ELFT> *Dumper = this->dumper();
3600 StringRef StrTable = Dumper->getDynamicStringTable();
3601
3602 // readelf prints 4 entries per line.
3603 for (uint64_t VersymRow = 0; VersymRow < Entries; VersymRow += 4) {
3604 OS << " " << format_hex_no_prefix(VersymRow, 3) << ":";
3605
3606 for (uint64_t VersymIndex = 0;
3607 (VersymIndex < 4) && (VersymIndex + VersymRow) < Entries;
3608 ++VersymIndex) {
3609 const Elf_Versym *Versym =
3610 reinterpret_cast<const Elf_Versym *>(VersymBuf);
3611 switch (Versym->vs_index) {
3612 case 0:
3613 OS << " 0 (*local*) ";
3614 break;
3615 case 1:
3616 OS << " 1 (*global*) ";
3617 break;
3618 default:
3619 OS << format("%4x%c", Versym->vs_index & VERSYM_VERSION,
3620 Versym->vs_index & VERSYM_HIDDEN ? 'h' : ' ');
3621
3622 bool IsDefault = true;
3623 std::string VersionName = Dumper->getSymbolVersionByIndex(
3624 StrTable, Versym->vs_index, IsDefault);
3625
3626 if (!VersionName.empty())
3627 VersionName = "(" + VersionName + ")";
3628 else
3629 VersionName = "(*invalid*)";
3630 OS << left_justify(VersionName, 13);
3631 }
3632 VersymBuf += sizeof(Elf_Versym);
3633 }
3634 OS << '\n';
3635 }
3636 OS << '\n';
Xing GUOea16be12019-03-25 11:02:49 +00003637}
3638
George Rimarc372f412019-05-30 10:36:52 +00003639static std::string versionFlagToString(unsigned Flags) {
George Rimare3406c42019-05-30 10:14:41 +00003640 if (Flags == 0)
3641 return "none";
3642
3643 std::string Ret;
3644 auto AddFlag = [&Ret, &Flags](unsigned Flag, StringRef Name) {
3645 if (!(Flags & Flag))
3646 return;
3647 if (!Ret.empty())
3648 Ret += " | ";
3649 Ret += Name;
3650 Flags &= ~Flag;
3651 };
3652
3653 AddFlag(VER_FLG_BASE, "BASE");
3654 AddFlag(VER_FLG_WEAK, "WEAK");
3655 AddFlag(VER_FLG_INFO, "INFO");
3656 AddFlag(~0, "<unknown>");
3657 return Ret;
Xing GUOea16be12019-03-25 11:02:49 +00003658}
3659
3660template <class ELFT>
George Rimarc372f412019-05-30 10:36:52 +00003661void GNUStyle<ELFT>::printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
3662 const Elf_Shdr *Sec) {
3663 if (!Sec)
3664 return;
3665
3666 unsigned VerDefsNum = Sec->sh_info;
3667 printGNUVersionSectionProlog(OS, "Version definition", VerDefsNum, Obj, Sec);
3668
3669 const Elf_Shdr *StrTabSec = unwrapOrError(Obj->getSection(Sec->sh_link));
3670 StringRef StringTable(
3671 reinterpret_cast<const char *>(Obj->base() + StrTabSec->sh_offset),
George Rimar28e1ff22019-05-30 10:42:47 +00003672 (size_t)StrTabSec->sh_size);
George Rimarc372f412019-05-30 10:36:52 +00003673
3674 const uint8_t *VerdefBuf = unwrapOrError(Obj->getSectionContents(Sec)).data();
3675 const uint8_t *Begin = VerdefBuf;
3676
3677 while (VerDefsNum--) {
3678 const Elf_Verdef *Verdef = reinterpret_cast<const Elf_Verdef *>(VerdefBuf);
3679 OS << format(" 0x%04x: Rev: %u Flags: %s Index: %u Cnt: %u",
3680 VerdefBuf - Begin, (unsigned)Verdef->vd_version,
3681 versionFlagToString(Verdef->vd_flags).c_str(),
3682 (unsigned)Verdef->vd_ndx, (unsigned)Verdef->vd_cnt);
3683
3684 const uint8_t *VerdauxBuf = VerdefBuf + Verdef->vd_aux;
3685 const Elf_Verdaux *Verdaux =
3686 reinterpret_cast<const Elf_Verdaux *>(VerdauxBuf);
3687 OS << format(" Name: %s\n",
3688 StringTable.drop_front(Verdaux->vda_name).data());
3689
3690 for (unsigned I = 1; I < Verdef->vd_cnt; ++I) {
3691 VerdauxBuf += Verdaux->vda_next;
3692 Verdaux = reinterpret_cast<const Elf_Verdaux *>(VerdauxBuf);
3693 OS << format(" 0x%04x: Parent %u: %s\n", VerdauxBuf - Begin, I,
3694 StringTable.drop_front(Verdaux->vda_name).data());
3695 }
3696
3697 VerdefBuf += Verdef->vd_next;
3698 }
3699 OS << '\n';
3700}
3701
3702template <class ELFT>
Xing GUOea16be12019-03-25 11:02:49 +00003703void GNUStyle<ELFT>::printVersionDependencySection(const ELFFile<ELFT> *Obj,
3704 const Elf_Shdr *Sec) {
3705 if (!Sec)
3706 return;
3707
George Rimare3406c42019-05-30 10:14:41 +00003708 unsigned VerneedNum = Sec->sh_info;
3709 printGNUVersionSectionProlog(OS, "Version needs", VerneedNum, Obj, Sec);
3710
3711 ArrayRef<uint8_t> SecData = unwrapOrError(Obj->getSectionContents(Sec));
3712
3713 const Elf_Shdr *StrTabSec = unwrapOrError(Obj->getSection(Sec->sh_link));
3714 StringRef StringTable = {
3715 reinterpret_cast<const char *>(Obj->base() + StrTabSec->sh_offset),
George Rimar28e1ff22019-05-30 10:42:47 +00003716 (size_t)StrTabSec->sh_size};
George Rimare3406c42019-05-30 10:14:41 +00003717
3718 const uint8_t *VerneedBuf = SecData.data();
3719 for (unsigned I = 0; I < VerneedNum; ++I) {
3720 const Elf_Verneed *Verneed =
3721 reinterpret_cast<const Elf_Verneed *>(VerneedBuf);
3722
3723 OS << format(" 0x%04x: Version: %u File: %s Cnt: %u\n",
3724 reinterpret_cast<const uint8_t *>(Verneed) - SecData.begin(),
3725 (unsigned)Verneed->vn_version,
3726 StringTable.drop_front(Verneed->vn_file).data(),
3727 (unsigned)Verneed->vn_cnt);
3728
3729 const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux;
3730 for (unsigned J = 0; J < Verneed->vn_cnt; ++J) {
3731 const Elf_Vernaux *Vernaux =
3732 reinterpret_cast<const Elf_Vernaux *>(VernauxBuf);
3733
3734 OS << format(" 0x%04x: Name: %s Flags: %s Version: %u\n",
3735 reinterpret_cast<const uint8_t *>(Vernaux) - SecData.begin(),
3736 StringTable.drop_front(Vernaux->vna_name).data(),
George Rimarc372f412019-05-30 10:36:52 +00003737 versionFlagToString(Vernaux->vna_flags).c_str(),
George Rimare3406c42019-05-30 10:14:41 +00003738 (unsigned)Vernaux->vna_other);
3739 VernauxBuf += Vernaux->vna_next;
3740 }
3741 VerneedBuf += Verneed->vn_next;
3742 }
3743 OS << '\n';
Xing GUOea16be12019-03-25 11:02:49 +00003744}
3745
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +00003746// Hash histogram shows statistics of how efficient the hash was for the
3747// dynamic symbol table. The table shows number of hash buckets for different
3748// lengths of chains as absolute number and percentage of the total buckets.
3749// Additionally cumulative coverage of symbols for each set of buckets.
3750template <class ELFT>
3751void GNUStyle<ELFT>::printHashHistogram(const ELFFile<ELFT> *Obj) {
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +00003752 // Print histogram for .hash section
George Rimar1206f5a2019-01-30 15:39:05 +00003753 if (const Elf_Hash *HashTable = this->dumper()->getHashTable()) {
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +00003754 size_t NBucket = HashTable->nbucket;
3755 size_t NChain = HashTable->nchain;
3756 ArrayRef<Elf_Word> Buckets = HashTable->buckets();
3757 ArrayRef<Elf_Word> Chains = HashTable->chains();
3758 size_t TotalSyms = 0;
3759 // If hash table is correct, we have at least chains with 0 length
3760 size_t MaxChain = 1;
3761 size_t CumulativeNonZero = 0;
3762
3763 if (NChain == 0 || NBucket == 0)
3764 return;
3765
3766 std::vector<size_t> ChainLen(NBucket, 0);
3767 // Go over all buckets and and note chain lengths of each bucket (total
3768 // unique chain lengths).
3769 for (size_t B = 0; B < NBucket; B++) {
3770 for (size_t C = Buckets[B]; C > 0 && C < NChain; C = Chains[C])
3771 if (MaxChain <= ++ChainLen[B])
3772 MaxChain++;
3773 TotalSyms += ChainLen[B];
3774 }
3775
3776 if (!TotalSyms)
3777 return;
3778
George Rimarec895f12019-05-16 06:22:51 +00003779 std::vector<size_t> Count(MaxChain, 0) ;
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +00003780 // Count how long is the chain for each bucket
3781 for (size_t B = 0; B < NBucket; B++)
3782 ++Count[ChainLen[B]];
3783 // Print Number of buckets with each chain lengths and their cumulative
3784 // coverage of the symbols
3785 OS << "Histogram for bucket list length (total of " << NBucket
3786 << " buckets)\n"
3787 << " Length Number % of total Coverage\n";
3788 for (size_t I = 0; I < MaxChain; I++) {
3789 CumulativeNonZero += Count[I] * I;
3790 OS << format("%7lu %-10lu (%5.1f%%) %5.1f%%\n", I, Count[I],
3791 (Count[I] * 100.0) / NBucket,
3792 (CumulativeNonZero * 100.0) / TotalSyms);
3793 }
3794 }
3795
3796 // Print histogram for .gnu.hash section
George Rimar1206f5a2019-01-30 15:39:05 +00003797 if (const Elf_GnuHash *GnuHashTable = this->dumper()->getGnuHashTable()) {
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +00003798 size_t NBucket = GnuHashTable->nbuckets;
3799 ArrayRef<Elf_Word> Buckets = GnuHashTable->buckets();
3800 unsigned NumSyms = this->dumper()->dynamic_symbols().size();
3801 if (!NumSyms)
3802 return;
3803 ArrayRef<Elf_Word> Chains = GnuHashTable->values(NumSyms);
3804 size_t Symndx = GnuHashTable->symndx;
3805 size_t TotalSyms = 0;
3806 size_t MaxChain = 1;
3807 size_t CumulativeNonZero = 0;
3808
Eugene Zelenko416e0592017-06-09 21:41:54 +00003809 if (Chains.empty() || NBucket == 0)
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +00003810 return;
3811
3812 std::vector<size_t> ChainLen(NBucket, 0);
3813
3814 for (size_t B = 0; B < NBucket; B++) {
3815 if (!Buckets[B])
3816 continue;
3817 size_t Len = 1;
3818 for (size_t C = Buckets[B] - Symndx;
3819 C < Chains.size() && (Chains[C] & 1) == 0; C++)
3820 if (MaxChain < ++Len)
3821 MaxChain++;
3822 ChainLen[B] = Len;
3823 TotalSyms += Len;
3824 }
3825 MaxChain++;
3826
3827 if (!TotalSyms)
3828 return;
3829
George Rimarec895f12019-05-16 06:22:51 +00003830 std::vector<size_t> Count(MaxChain, 0) ;
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +00003831 for (size_t B = 0; B < NBucket; B++)
3832 ++Count[ChainLen[B]];
3833 // Print Number of buckets with each chain lengths and their cumulative
3834 // coverage of the symbols
3835 OS << "Histogram for `.gnu.hash' bucket list length (total of " << NBucket
3836 << " buckets)\n"
3837 << " Length Number % of total Coverage\n";
George Rimarec895f12019-05-16 06:22:51 +00003838 for (size_t I = 0; I <MaxChain; I++) {
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +00003839 CumulativeNonZero += Count[I] * I;
3840 OS << format("%7lu %-10lu (%5.1f%%) %5.1f%%\n", I, Count[I],
3841 (Count[I] * 100.0) / NBucket,
3842 (CumulativeNonZero * 100.0) / TotalSyms);
3843 }
3844 }
3845}
3846
Michael J. Spencerae6eeae2018-06-02 16:33:01 +00003847template <class ELFT>
3848void GNUStyle<ELFT>::printCGProfile(const ELFFile<ELFT> *Obj) {
3849 OS << "GNUStyle::printCGProfile not implemented\n";
3850}
3851
Peter Collingbourne3e227332018-07-17 22:17:18 +00003852template <class ELFT>
3853void GNUStyle<ELFT>::printAddrsig(const ELFFile<ELFT> *Obj) {
George Rimarec895f12019-05-16 06:22:51 +00003854 OS << "GNUStyle::printAddrsig not implemented\n";
Peter Collingbourne3e227332018-07-17 22:17:18 +00003855}
3856
Jordan Rupprecht871baa22019-03-29 16:48:19 +00003857static StringRef getGenericNoteTypeName(const uint32_t NT) {
3858 static const struct {
3859 uint32_t ID;
3860 const char *Name;
3861 } Notes[] = {
3862 {ELF::NT_VERSION, "NT_VERSION (version)"},
3863 {ELF::NT_ARCH, "NT_ARCH (architecture)"},
3864 {ELF::NT_GNU_BUILD_ATTRIBUTE_OPEN, "OPEN"},
3865 {ELF::NT_GNU_BUILD_ATTRIBUTE_FUNC, "func"},
3866 };
3867
3868 for (const auto &Note : Notes)
3869 if (Note.ID == NT)
3870 return Note.Name;
3871
3872 return "";
3873}
3874
Jordan Rupprechtd884fbd2019-08-05 15:43:20 +00003875static StringRef getCoreNoteTypeName(const uint32_t NT) {
3876 static const struct {
3877 uint32_t ID;
3878 const char *Name;
3879 } Notes[] = {
3880 {ELF::NT_PRSTATUS, "NT_PRSTATUS (prstatus structure)"},
3881 {ELF::NT_FPREGSET, "NT_FPREGSET (floating point registers)"},
3882 {ELF::NT_PRPSINFO, "NT_PRPSINFO (prpsinfo structure)"},
3883 {ELF::NT_TASKSTRUCT, "NT_TASKSTRUCT (task structure)"},
3884 {ELF::NT_AUXV, "NT_AUXV (auxiliary vector)"},
3885 {ELF::NT_PSTATUS, "NT_PSTATUS (pstatus structure)"},
3886 {ELF::NT_FPREGS, "NT_FPREGS (floating point registers)"},
3887 {ELF::NT_PSINFO, "NT_PSINFO (psinfo structure)"},
3888 {ELF::NT_LWPSTATUS, "NT_LWPSTATUS (lwpstatus_t structure)"},
3889 {ELF::NT_LWPSINFO, "NT_LWPSINFO (lwpsinfo_t structure)"},
3890 {ELF::NT_WIN32PSTATUS, "NT_WIN32PSTATUS (win32_pstatus structure)"},
3891
3892 {ELF::NT_PPC_VMX, "NT_PPC_VMX (ppc Altivec registers)"},
3893 {ELF::NT_PPC_VSX, "NT_PPC_VSX (ppc VSX registers)"},
3894 {ELF::NT_PPC_TAR, "NT_PPC_TAR (ppc TAR register)"},
3895 {ELF::NT_PPC_PPR, "NT_PPC_PPR (ppc PPR register)"},
3896 {ELF::NT_PPC_DSCR, "NT_PPC_DSCR (ppc DSCR register)"},
3897 {ELF::NT_PPC_EBB, "NT_PPC_EBB (ppc EBB registers)"},
3898 {ELF::NT_PPC_PMU, "NT_PPC_PMU (ppc PMU registers)"},
3899 {ELF::NT_PPC_TM_CGPR, "NT_PPC_TM_CGPR (ppc checkpointed GPR registers)"},
3900 {ELF::NT_PPC_TM_CFPR,
3901 "NT_PPC_TM_CFPR (ppc checkpointed floating point registers)"},
3902 {ELF::NT_PPC_TM_CVMX,
3903 "NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)"},
3904 {ELF::NT_PPC_TM_CVSX, "NT_PPC_TM_CVSX (ppc checkpointed VSX registers)"},
3905 {ELF::NT_PPC_TM_SPR, "NT_PPC_TM_SPR (ppc TM special purpose registers)"},
3906 {ELF::NT_PPC_TM_CTAR, "NT_PPC_TM_CTAR (ppc checkpointed TAR register)"},
3907 {ELF::NT_PPC_TM_CPPR, "NT_PPC_TM_CPPR (ppc checkpointed PPR register)"},
3908 {ELF::NT_PPC_TM_CDSCR,
3909 "NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)"},
3910
3911 {ELF::NT_386_TLS, "NT_386_TLS (x86 TLS information)"},
3912 {ELF::NT_386_IOPERM, "NT_386_IOPERM (x86 I/O permissions)"},
3913 {ELF::NT_X86_XSTATE, "NT_X86_XSTATE (x86 XSAVE extended state)"},
3914
3915 {ELF::NT_S390_HIGH_GPRS,
3916 "NT_S390_HIGH_GPRS (s390 upper register halves)"},
3917 {ELF::NT_S390_TIMER, "NT_S390_TIMER (s390 timer register)"},
3918 {ELF::NT_S390_TODCMP, "NT_S390_TODCMP (s390 TOD comparator register)"},
3919 {ELF::NT_S390_TODPREG,
3920 "NT_S390_TODPREG (s390 TOD programmable register)"},
3921 {ELF::NT_S390_CTRS, "NT_S390_CTRS (s390 control registers)"},
3922 {ELF::NT_S390_PREFIX, "NT_S390_PREFIX (s390 prefix register)"},
3923 {ELF::NT_S390_LAST_BREAK,
3924 "NT_S390_LAST_BREAK (s390 last breaking event address)"},
3925 {ELF::NT_S390_SYSTEM_CALL,
3926 "NT_S390_SYSTEM_CALL (s390 system call restart data)"},
3927 {ELF::NT_S390_TDB, "NT_S390_TDB (s390 transaction diagnostic block)"},
3928 {ELF::NT_S390_VXRS_LOW,
3929 "NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)"},
3930 {ELF::NT_S390_VXRS_HIGH,
3931 "NT_S390_VXRS_HIGH (s390 vector registers 16-31)"},
3932 {ELF::NT_S390_GS_CB, "NT_S390_GS_CB (s390 guarded-storage registers)"},
3933 {ELF::NT_S390_GS_BC,
3934 "NT_S390_GS_BC (s390 guarded-storage broadcast control)"},
3935
3936 {ELF::NT_ARM_VFP, "NT_ARM_VFP (arm VFP registers)"},
3937 {ELF::NT_ARM_TLS, "NT_ARM_TLS (AArch TLS registers)"},
3938 {ELF::NT_ARM_HW_BREAK,
3939 "NT_ARM_HW_BREAK (AArch hardware breakpoint registers)"},
3940 {ELF::NT_ARM_HW_WATCH,
3941 "NT_ARM_HW_WATCH (AArch hardware watchpoint registers)"},
3942
3943 {ELF::NT_FILE, "NT_FILE (mapped files)"},
3944 {ELF::NT_PRXFPREG, "NT_PRXFPREG (user_xfpregs structure)"},
3945 {ELF::NT_SIGINFO, "NT_SIGINFO (siginfo_t data)"},
3946 };
3947
3948 for (const auto &Note : Notes)
3949 if (Note.ID == NT)
3950 return Note.Name;
3951
3952 return "";
3953}
3954
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00003955static std::string getGNUNoteTypeName(const uint32_t NT) {
3956 static const struct {
3957 uint32_t ID;
3958 const char *Name;
3959 } Notes[] = {
3960 {ELF::NT_GNU_ABI_TAG, "NT_GNU_ABI_TAG (ABI version tag)"},
3961 {ELF::NT_GNU_HWCAP, "NT_GNU_HWCAP (DSO-supplied software HWCAP info)"},
3962 {ELF::NT_GNU_BUILD_ID, "NT_GNU_BUILD_ID (unique build ID bitstring)"},
3963 {ELF::NT_GNU_GOLD_VERSION, "NT_GNU_GOLD_VERSION (gold version)"},
George Rimar6a14c022018-03-21 08:34:55 +00003964 {ELF::NT_GNU_PROPERTY_TYPE_0, "NT_GNU_PROPERTY_TYPE_0 (property note)"},
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00003965 };
3966
3967 for (const auto &Note : Notes)
3968 if (Note.ID == NT)
3969 return std::string(Note.Name);
3970
3971 std::string string;
3972 raw_string_ostream OS(string);
3973 OS << format("Unknown note type (0x%08x)", NT);
Konstantin Zhuravlyov716af742017-10-14 16:43:46 +00003974 return OS.str();
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00003975}
3976
Saleem Abdulrasool4b089132017-02-12 18:55:33 +00003977static std::string getFreeBSDNoteTypeName(const uint32_t NT) {
3978 static const struct {
3979 uint32_t ID;
3980 const char *Name;
3981 } Notes[] = {
3982 {ELF::NT_FREEBSD_THRMISC, "NT_THRMISC (thrmisc structure)"},
3983 {ELF::NT_FREEBSD_PROCSTAT_PROC, "NT_PROCSTAT_PROC (proc data)"},
3984 {ELF::NT_FREEBSD_PROCSTAT_FILES, "NT_PROCSTAT_FILES (files data)"},
3985 {ELF::NT_FREEBSD_PROCSTAT_VMMAP, "NT_PROCSTAT_VMMAP (vmmap data)"},
3986 {ELF::NT_FREEBSD_PROCSTAT_GROUPS, "NT_PROCSTAT_GROUPS (groups data)"},
3987 {ELF::NT_FREEBSD_PROCSTAT_UMASK, "NT_PROCSTAT_UMASK (umask data)"},
3988 {ELF::NT_FREEBSD_PROCSTAT_RLIMIT, "NT_PROCSTAT_RLIMIT (rlimit data)"},
3989 {ELF::NT_FREEBSD_PROCSTAT_OSREL, "NT_PROCSTAT_OSREL (osreldate data)"},
3990 {ELF::NT_FREEBSD_PROCSTAT_PSSTRINGS,
3991 "NT_PROCSTAT_PSSTRINGS (ps_strings data)"},
3992 {ELF::NT_FREEBSD_PROCSTAT_AUXV, "NT_PROCSTAT_AUXV (auxv data)"},
3993 };
3994
3995 for (const auto &Note : Notes)
3996 if (Note.ID == NT)
3997 return std::string(Note.Name);
3998
3999 std::string string;
4000 raw_string_ostream OS(string);
4001 OS << format("Unknown note type (0x%08x)", NT);
Konstantin Zhuravlyov716af742017-10-14 16:43:46 +00004002 return OS.str();
4003}
4004
Scott Linderf5b36e52018-12-12 19:39:27 +00004005static std::string getAMDNoteTypeName(const uint32_t NT) {
Konstantin Zhuravlyov716af742017-10-14 16:43:46 +00004006 static const struct {
4007 uint32_t ID;
4008 const char *Name;
George Rimar9e88a262019-05-14 14:22:44 +00004009 } Notes[] = {{ELF::NT_AMD_AMDGPU_HSA_METADATA,
4010 "NT_AMD_AMDGPU_HSA_METADATA (HSA Metadata)"},
4011 {ELF::NT_AMD_AMDGPU_ISA, "NT_AMD_AMDGPU_ISA (ISA Version)"},
4012 {ELF::NT_AMD_AMDGPU_PAL_METADATA,
4013 "NT_AMD_AMDGPU_PAL_METADATA (PAL Metadata)"}};
Konstantin Zhuravlyov716af742017-10-14 16:43:46 +00004014
4015 for (const auto &Note : Notes)
4016 if (Note.ID == NT)
4017 return std::string(Note.Name);
4018
4019 std::string string;
4020 raw_string_ostream OS(string);
4021 OS << format("Unknown note type (0x%08x)", NT);
4022 return OS.str();
Saleem Abdulrasool4b089132017-02-12 18:55:33 +00004023}
4024
Scott Linderf5b36e52018-12-12 19:39:27 +00004025static std::string getAMDGPUNoteTypeName(const uint32_t NT) {
4026 if (NT == ELF::NT_AMDGPU_METADATA)
4027 return std::string("NT_AMDGPU_METADATA (AMDGPU Metadata)");
4028
4029 std::string string;
4030 raw_string_ostream OS(string);
4031 OS << format("Unknown note type (0x%08x)", NT);
4032 return OS.str();
4033}
4034
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004035template <typename ELFT>
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004036static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
4037 ArrayRef<uint8_t> Data) {
4038 std::string str;
4039 raw_string_ostream OS(str);
Fangrui Song6a03b932019-02-13 15:58:23 +00004040 uint32_t PrData;
Fangrui Song12d55992019-02-13 01:51:45 +00004041 auto DumpBit = [&](uint32_t Flag, StringRef Name) {
Fangrui Song6a03b932019-02-13 15:58:23 +00004042 if (PrData & Flag) {
4043 PrData &= ~Flag;
Fangrui Song12d55992019-02-13 01:51:45 +00004044 OS << Name;
Fangrui Song6a03b932019-02-13 15:58:23 +00004045 if (PrData)
Fangrui Song12d55992019-02-13 01:51:45 +00004046 OS << ", ";
4047 }
4048 };
4049
George Rimar6a14c022018-03-21 08:34:55 +00004050 switch (Type) {
4051 default:
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004052 OS << format("<application-specific type 0x%x>", Type);
4053 return OS.str();
George Rimar6a14c022018-03-21 08:34:55 +00004054 case GNU_PROPERTY_STACK_SIZE: {
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004055 OS << "stack size: ";
George Rimar6a14c022018-03-21 08:34:55 +00004056 if (DataSize == sizeof(typename ELFT::uint))
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004057 OS << formatv("{0:x}",
4058 (uint64_t)(*(const typename ELFT::Addr *)Data.data()));
George Rimar6a14c022018-03-21 08:34:55 +00004059 else
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004060 OS << format("<corrupt length: 0x%x>", DataSize);
4061 return OS.str();
George Rimar6a14c022018-03-21 08:34:55 +00004062 }
4063 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004064 OS << "no copy on protected";
George Rimar6a14c022018-03-21 08:34:55 +00004065 if (DataSize)
4066 OS << format(" <corrupt length: 0x%x>", DataSize);
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004067 return OS.str();
Peter Smith580c6d32019-06-04 11:28:22 +00004068 case GNU_PROPERTY_AARCH64_FEATURE_1_AND:
Alexander Ivchenkoab60a282018-06-04 15:14:18 +00004069 case GNU_PROPERTY_X86_FEATURE_1_AND:
Peter Smith580c6d32019-06-04 11:28:22 +00004070 OS << ((Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) ? "aarch64 feature: "
4071 : "x86 feature: ");
Fangrui Song8e0d5ac2019-02-12 09:56:01 +00004072 if (DataSize != 4) {
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004073 OS << format("<corrupt length: 0x%x>", DataSize);
4074 return OS.str();
Alexander Ivchenkoab60a282018-06-04 15:14:18 +00004075 }
Fangrui Song6a03b932019-02-13 15:58:23 +00004076 PrData = support::endian::read32<ELFT::TargetEndianness>(Data.data());
4077 if (PrData == 0) {
Fangrui Song12d55992019-02-13 01:51:45 +00004078 OS << "<None>";
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004079 return OS.str();
Alexander Ivchenkoab60a282018-06-04 15:14:18 +00004080 }
Peter Smith580c6d32019-06-04 11:28:22 +00004081 if (Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
4082 DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI");
4083 DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC");
4084 } else {
4085 DumpBit(GNU_PROPERTY_X86_FEATURE_1_IBT, "IBT");
4086 DumpBit(GNU_PROPERTY_X86_FEATURE_1_SHSTK, "SHSTK");
4087 }
Fangrui Song6a03b932019-02-13 15:58:23 +00004088 if (PrData)
4089 OS << format("<unknown flags: 0x%x>", PrData);
Fangrui Song12d55992019-02-13 01:51:45 +00004090 return OS.str();
Fangrui Song91ab9bf2019-02-13 23:18:05 +00004091 case GNU_PROPERTY_X86_ISA_1_NEEDED:
4092 case GNU_PROPERTY_X86_ISA_1_USED:
4093 OS << "x86 ISA "
4094 << (Type == GNU_PROPERTY_X86_ISA_1_NEEDED ? "needed: " : "used: ");
4095 if (DataSize != 4) {
4096 OS << format("<corrupt length: 0x%x>", DataSize);
4097 return OS.str();
4098 }
4099 PrData = support::endian::read32<ELFT::TargetEndianness>(Data.data());
4100 if (PrData == 0) {
4101 OS << "<None>";
4102 return OS.str();
4103 }
4104 DumpBit(GNU_PROPERTY_X86_ISA_1_CMOV, "CMOV");
4105 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE, "SSE");
4106 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE2, "SSE2");
4107 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE3, "SSE3");
4108 DumpBit(GNU_PROPERTY_X86_ISA_1_SSSE3, "SSSE3");
4109 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE4_1, "SSE4_1");
4110 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE4_2, "SSE4_2");
4111 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX, "AVX");
4112 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX2, "AVX2");
4113 DumpBit(GNU_PROPERTY_X86_ISA_1_FMA, "FMA");
4114 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512F, "AVX512F");
4115 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512CD, "AVX512CD");
4116 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512ER, "AVX512ER");
4117 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512PF, "AVX512PF");
4118 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512VL, "AVX512VL");
4119 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512DQ, "AVX512DQ");
4120 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512BW, "AVX512BW");
4121 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_4FMAPS, "AVX512_4FMAPS");
4122 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_4VNNIW, "AVX512_4VNNIW");
4123 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_BITALG, "AVX512_BITALG");
4124 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_IFMA, "AVX512_IFMA");
4125 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_VBMI, "AVX512_VBMI");
4126 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_VBMI2, "AVX512_VBMI2");
4127 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_VNNI, "AVX512_VNNI");
4128 if (PrData)
4129 OS << format("<unknown flags: 0x%x>", PrData);
4130 return OS.str();
4131 break;
Fangrui Song12d55992019-02-13 01:51:45 +00004132 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
4133 case GNU_PROPERTY_X86_FEATURE_2_USED:
4134 OS << "x86 feature "
4135 << (Type == GNU_PROPERTY_X86_FEATURE_2_NEEDED ? "needed: " : "used: ");
4136 if (DataSize != 4) {
4137 OS << format("<corrupt length: 0x%x>", DataSize);
4138 return OS.str();
Alexander Ivchenkoab60a282018-06-04 15:14:18 +00004139 }
Fangrui Song6a03b932019-02-13 15:58:23 +00004140 PrData = support::endian::read32<ELFT::TargetEndianness>(Data.data());
4141 if (PrData == 0) {
Fangrui Song12d55992019-02-13 01:51:45 +00004142 OS << "<None>";
4143 return OS.str();
Alexander Ivchenkoab60a282018-06-04 15:14:18 +00004144 }
Fangrui Song12d55992019-02-13 01:51:45 +00004145 DumpBit(GNU_PROPERTY_X86_FEATURE_2_X86, "x86");
4146 DumpBit(GNU_PROPERTY_X86_FEATURE_2_X87, "x87");
4147 DumpBit(GNU_PROPERTY_X86_FEATURE_2_MMX, "MMX");
4148 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XMM, "XMM");
4149 DumpBit(GNU_PROPERTY_X86_FEATURE_2_YMM, "YMM");
4150 DumpBit(GNU_PROPERTY_X86_FEATURE_2_ZMM, "ZMM");
4151 DumpBit(GNU_PROPERTY_X86_FEATURE_2_FXSR, "FXSR");
4152 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVE, "XSAVE");
4153 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT, "XSAVEOPT");
4154 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVEC, "XSAVEC");
Fangrui Song6a03b932019-02-13 15:58:23 +00004155 if (PrData)
4156 OS << format("<unknown flags: 0x%x>", PrData);
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004157 return OS.str();
George Rimar6a14c022018-03-21 08:34:55 +00004158 }
4159}
4160
4161template <typename ELFT>
George Rimar9e88a262019-05-14 14:22:44 +00004162static SmallVector<std::string, 4> getGNUPropertyList(ArrayRef<uint8_t> Arr) {
George Rimar6a14c022018-03-21 08:34:55 +00004163 using Elf_Word = typename ELFT::Word;
4164
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004165 SmallVector<std::string, 4> Properties;
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004166 while (Arr.size() >= 8) {
4167 uint32_t Type = *reinterpret_cast<const Elf_Word *>(Arr.data());
4168 uint32_t DataSize = *reinterpret_cast<const Elf_Word *>(Arr.data() + 4);
4169 Arr = Arr.drop_front(8);
4170
4171 // Take padding size into account if present.
4172 uint64_t PaddedSize = alignTo(DataSize, sizeof(typename ELFT::uint));
4173 std::string str;
4174 raw_string_ostream OS(str);
4175 if (Arr.size() < PaddedSize) {
4176 OS << format("<corrupt type (0x%x) datasz: 0x%x>", Type, DataSize);
4177 Properties.push_back(OS.str());
4178 break;
4179 }
4180 Properties.push_back(
4181 getGNUProperty<ELFT>(Type, DataSize, Arr.take_front(PaddedSize)));
4182 Arr = Arr.drop_front(PaddedSize);
4183 }
4184
4185 if (!Arr.empty())
4186 Properties.push_back("<corrupted GNU_PROPERTY_TYPE_0>");
4187
4188 return Properties;
4189}
4190
4191struct GNUAbiTag {
4192 std::string OSName;
4193 std::string ABI;
4194 bool IsValid;
4195};
4196
George Rimar9e88a262019-05-14 14:22:44 +00004197template <typename ELFT> static GNUAbiTag getGNUAbiTag(ArrayRef<uint8_t> Desc) {
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00004198 typedef typename ELFT::Word Elf_Word;
4199
George Rimar9e88a262019-05-14 14:22:44 +00004200 ArrayRef<Elf_Word> Words(reinterpret_cast<const Elf_Word *>(Desc.begin()),
4201 reinterpret_cast<const Elf_Word *>(Desc.end()));
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00004202
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004203 if (Words.size() < 4)
4204 return {"", "", /*IsValid=*/false};
4205
4206 static const char *OSNames[] = {
4207 "Linux", "Hurd", "Solaris", "FreeBSD", "NetBSD", "Syllable", "NaCl",
4208 };
4209 StringRef OSName = "Unknown";
4210 if (Words[0] < array_lengthof(OSNames))
4211 OSName = OSNames[Words[0]];
4212 uint32_t Major = Words[1], Minor = Words[2], Patch = Words[3];
4213 std::string str;
4214 raw_string_ostream ABI(str);
4215 ABI << Major << "." << Minor << "." << Patch;
4216 return {OSName, ABI.str(), /*IsValid=*/true};
4217}
4218
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00004219static std::string getGNUBuildId(ArrayRef<uint8_t> Desc) {
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004220 std::string str;
4221 raw_string_ostream OS(str);
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00004222 for (const auto &B : Desc)
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004223 OS << format_hex_no_prefix(B, 2);
4224 return OS.str();
4225}
4226
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00004227static StringRef getGNUGoldVersion(ArrayRef<uint8_t> Desc) {
4228 return StringRef(reinterpret_cast<const char *>(Desc.data()), Desc.size());
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004229}
4230
4231template <typename ELFT>
4232static void printGNUNote(raw_ostream &OS, uint32_t NoteType,
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00004233 ArrayRef<uint8_t> Desc) {
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004234 switch (NoteType) {
4235 default:
4236 return;
4237 case ELF::NT_GNU_ABI_TAG: {
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00004238 const GNUAbiTag &AbiTag = getGNUAbiTag<ELFT>(Desc);
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004239 if (!AbiTag.IsValid)
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004240 OS << " <corrupt GNU_ABI_TAG>";
4241 else
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004242 OS << " OS: " << AbiTag.OSName << ", ABI: " << AbiTag.ABI;
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004243 break;
4244 }
4245 case ELF::NT_GNU_BUILD_ID: {
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00004246 OS << " Build ID: " << getGNUBuildId(Desc);
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004247 break;
4248 }
4249 case ELF::NT_GNU_GOLD_VERSION:
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00004250 OS << " Version: " << getGNUGoldVersion(Desc);
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004251 break;
George Rimar6a14c022018-03-21 08:34:55 +00004252 case ELF::NT_GNU_PROPERTY_TYPE_0:
4253 OS << " Properties:";
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00004254 for (const auto &Property : getGNUPropertyList<ELFT>(Desc))
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004255 OS << " " << Property << "\n";
George Rimar6a14c022018-03-21 08:34:55 +00004256 break;
4257 }
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004258 OS << '\n';
4259}
4260
Scott Linderf5b36e52018-12-12 19:39:27 +00004261struct AMDNote {
4262 std::string Type;
4263 std::string Value;
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004264};
4265
Konstantin Zhuravlyovb3c605d2017-10-14 18:21:42 +00004266template <typename ELFT>
Scott Linderf5b36e52018-12-12 19:39:27 +00004267static AMDNote getAMDNote(uint32_t NoteType, ArrayRef<uint8_t> Desc) {
Konstantin Zhuravlyovb3c605d2017-10-14 18:21:42 +00004268 switch (NoteType) {
4269 default:
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004270 return {"", ""};
4271 case ELF::NT_AMD_AMDGPU_HSA_METADATA:
George Rimar9e88a262019-05-14 14:22:44 +00004272 return {
4273 "HSA Metadata",
4274 std::string(reinterpret_cast<const char *>(Desc.data()), Desc.size())};
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004275 case ELF::NT_AMD_AMDGPU_ISA:
George Rimar9e88a262019-05-14 14:22:44 +00004276 return {
4277 "ISA Version",
4278 std::string(reinterpret_cast<const char *>(Desc.data()), Desc.size())};
Konstantin Zhuravlyovb3c605d2017-10-14 18:21:42 +00004279 }
Konstantin Zhuravlyovb3c605d2017-10-14 18:21:42 +00004280}
4281
Scott Linderf5b36e52018-12-12 19:39:27 +00004282struct AMDGPUNote {
4283 std::string Type;
4284 std::string Value;
4285};
4286
4287template <typename ELFT>
4288static AMDGPUNote getAMDGPUNote(uint32_t NoteType, ArrayRef<uint8_t> Desc) {
4289 switch (NoteType) {
4290 default:
4291 return {"", ""};
Tim Renoufed0b9af2019-03-13 18:55:50 +00004292 case ELF::NT_AMDGPU_METADATA: {
Scott Linderf5b36e52018-12-12 19:39:27 +00004293 auto MsgPackString =
4294 StringRef(reinterpret_cast<const char *>(Desc.data()), Desc.size());
Tim Renoufed0b9af2019-03-13 18:55:50 +00004295 msgpack::Document MsgPackDoc;
4296 if (!MsgPackDoc.readFromBlob(MsgPackString, /*Multi=*/false))
Scott Linderf5b36e52018-12-12 19:39:27 +00004297 return {"AMDGPU Metadata", "Invalid AMDGPU Metadata"};
Scott Linderf5b36e52018-12-12 19:39:27 +00004298
4299 AMDGPU::HSAMD::V3::MetadataVerifier Verifier(true);
Tim Renoufed0b9af2019-03-13 18:55:50 +00004300 if (!Verifier.verify(MsgPackDoc.getRoot()))
Scott Linderf5b36e52018-12-12 19:39:27 +00004301 return {"AMDGPU Metadata", "Invalid AMDGPU Metadata"};
4302
4303 std::string HSAMetadataString;
4304 raw_string_ostream StrOS(HSAMetadataString);
Tim Renoufed0b9af2019-03-13 18:55:50 +00004305 MsgPackDoc.toYAML(StrOS);
Scott Linderf5b36e52018-12-12 19:39:27 +00004306
4307 return {"AMDGPU Metadata", StrOS.str()};
4308 }
Tim Renoufed0b9af2019-03-13 18:55:50 +00004309 }
Scott Linderf5b36e52018-12-12 19:39:27 +00004310}
4311
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004312template <class ELFT>
4313void GNUStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
Scott Linder77a5f212018-03-12 19:28:50 +00004314 auto PrintHeader = [&](const typename ELFT::Off Offset,
4315 const typename ELFT::Addr Size) {
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004316 OS << "Displaying notes found at file offset " << format_hex(Offset, 10)
4317 << " with length " << format_hex(Size, 10) << ":\n"
Fangrui Songc3109922019-08-07 09:13:11 +00004318 << " Owner Data size \tDescription\n";
Scott Linder77a5f212018-03-12 19:28:50 +00004319 };
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004320
Scott Linder77a5f212018-03-12 19:28:50 +00004321 auto ProcessNote = [&](const Elf_Note &Note) {
4322 StringRef Name = Note.getName();
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00004323 ArrayRef<uint8_t> Descriptor = Note.getDesc();
Scott Linder77a5f212018-03-12 19:28:50 +00004324 Elf_Word Type = Note.getType();
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004325
Fangrui Songc3109922019-08-07 09:13:11 +00004326 OS << " " << left_justify(Name, 20) << ' '
Scott Linder77a5f212018-03-12 19:28:50 +00004327 << format_hex(Descriptor.size(), 10) << '\t';
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004328
Scott Linder77a5f212018-03-12 19:28:50 +00004329 if (Name == "GNU") {
4330 OS << getGNUNoteTypeName(Type) << '\n';
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004331 printGNUNote<ELFT>(OS, Type, Descriptor);
Scott Linder77a5f212018-03-12 19:28:50 +00004332 } else if (Name == "FreeBSD") {
4333 OS << getFreeBSDNoteTypeName(Type) << '\n';
4334 } else if (Name == "AMD") {
Scott Linderf5b36e52018-12-12 19:39:27 +00004335 OS << getAMDNoteTypeName(Type) << '\n';
4336 const AMDNote N = getAMDNote<ELFT>(Type, Descriptor);
4337 if (!N.Type.empty())
4338 OS << " " << N.Type << ":\n " << N.Value << '\n';
4339 } else if (Name == "AMDGPU") {
Scott Linder77a5f212018-03-12 19:28:50 +00004340 OS << getAMDGPUNoteTypeName(Type) << '\n';
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00004341 const AMDGPUNote N = getAMDGPUNote<ELFT>(Type, Descriptor);
Scott Linderf5b36e52018-12-12 19:39:27 +00004342 if (!N.Type.empty())
4343 OS << " " << N.Type << ":\n " << N.Value << '\n';
Scott Linder77a5f212018-03-12 19:28:50 +00004344 } else {
Jordan Rupprechtd884fbd2019-08-05 15:43:20 +00004345 StringRef NoteType = Obj->getHeader()->e_type == ELF::ET_CORE
4346 ? getCoreNoteTypeName(Type)
4347 : getGenericNoteTypeName(Type);
Jordan Rupprecht871baa22019-03-29 16:48:19 +00004348 if (!NoteType.empty())
4349 OS << NoteType;
4350 else
4351 OS << "Unknown note type: (" << format_hex(Type, 10) << ')';
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004352 }
Scott Linder77a5f212018-03-12 19:28:50 +00004353 OS << '\n';
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004354 };
4355
George Rimar1206f5a2019-01-30 15:39:05 +00004356 if (Obj->getHeader()->e_type == ELF::ET_CORE) {
Scott Linder77a5f212018-03-12 19:28:50 +00004357 for (const auto &P : unwrapOrError(Obj->program_headers())) {
4358 if (P.p_type != PT_NOTE)
4359 continue;
4360 PrintHeader(P.p_offset, P.p_filesz);
4361 Error Err = Error::success();
4362 for (const auto &Note : Obj->notes(P, Err))
4363 ProcessNote(Note);
4364 if (Err)
4365 error(std::move(Err));
4366 }
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004367 } else {
Scott Linder77a5f212018-03-12 19:28:50 +00004368 for (const auto &S : unwrapOrError(Obj->sections())) {
4369 if (S.sh_type != SHT_NOTE)
4370 continue;
4371 PrintHeader(S.sh_offset, S.sh_size);
4372 Error Err = Error::success();
4373 for (const auto &Note : Obj->notes(S, Err))
4374 ProcessNote(Note);
4375 if (Err)
4376 error(std::move(Err));
4377 }
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00004378 }
4379}
4380
Simon Atanasyan62d32592017-12-21 10:26:02 +00004381template <class ELFT>
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +00004382void GNUStyle<ELFT>::printELFLinkerOptions(const ELFFile<ELFT> *Obj) {
4383 OS << "printELFLinkerOptions not implemented!\n";
4384}
4385
Wolfgang Piebc71c6292019-08-05 22:47:07 +00004386template <class ELFT>
4387void DumpStyle<ELFT>::printFunctionStackSize(
4388 const ELFObjectFile<ELFT> *Obj, uint64_t SymValue, SectionRef FunctionSec,
4389 const StringRef SectionName, DataExtractor Data, uint64_t *Offset) {
4390 // This function ignores potentially erroneous input, unless it is directly
4391 // related to stack size reporting.
4392 SymbolRef FuncSym;
4393 for (const ELFSymbolRef &Symbol : Obj->symbols()) {
4394 Expected<uint64_t> SymAddrOrErr = Symbol.getAddress();
4395 if (!SymAddrOrErr) {
4396 consumeError(SymAddrOrErr.takeError());
4397 continue;
4398 }
4399 if (Symbol.getELFType() == ELF::STT_FUNC && *SymAddrOrErr == SymValue) {
4400 // Check if the symbol is in the right section.
4401 if (FunctionSec.containsSymbol(Symbol)) {
4402 FuncSym = Symbol;
4403 break;
4404 }
4405 }
4406 }
4407
4408 StringRef FileStr = Obj->getFileName();
4409 std::string FuncName = "?";
4410 // A valid SymbolRef has a non-null object file pointer.
4411 if (FuncSym.BasicSymbolRef::getObject()) {
4412 // Extract the symbol name.
4413 Expected<StringRef> FuncNameOrErr = FuncSym.getName();
4414 if (FuncNameOrErr)
4415 FuncName = maybeDemangle(*FuncNameOrErr);
4416 else
4417 consumeError(FuncNameOrErr.takeError());
4418 } else
4419 reportWarning(" '" + FileStr +
4420 "': could not identify function symbol for stack size entry");
4421
4422 // Extract the size. The expectation is that Offset is pointing to the right
4423 // place, i.e. past the function address.
4424 uint64_t PrevOffset = *Offset;
Igor Kudrin45ee9332019-08-07 11:44:47 +00004425 uint64_t StackSize = Data.getULEB128(Offset);
Wolfgang Piebc71c6292019-08-05 22:47:07 +00004426 // getULEB128() does not advance Offset if it is not able to extract a valid
4427 // integer.
4428 if (*Offset == PrevOffset)
4429 reportError(
4430 FileStr,
4431 createStringError(object_error::parse_failed,
4432 "could not extract a valid stack size in section %s",
4433 SectionName.data()));
4434
4435 printStackSizeEntry(StackSize, FuncName);
4436}
4437
4438template <class ELFT>
4439void GNUStyle<ELFT>::printStackSizeEntry(uint64_t Size, StringRef FuncName) {
4440 OS.PadToColumn(2);
4441 OS << format_decimal(Size, 11);
4442 OS.PadToColumn(18);
4443 OS << FuncName << "\n";
4444}
4445
4446template <class ELFT>
4447void DumpStyle<ELFT>::printStackSize(const ELFObjectFile<ELFT> *Obj,
4448 RelocationRef Reloc,
4449 SectionRef FunctionSec,
4450 const StringRef &StackSizeSectionName,
4451 const RelocationResolver &Resolver,
4452 DataExtractor Data) {
4453 // This function ignores potentially erroneous input, unless it is directly
4454 // related to stack size reporting.
4455 object::symbol_iterator RelocSym = Reloc.getSymbol();
4456 uint64_t RelocSymValue = 0;
4457 StringRef FileStr = Obj->getFileName();
4458 if (RelocSym != Obj->symbol_end()) {
4459 // Ensure that the relocation symbol is in the function section, i.e. the
4460 // section where the functions whose stack sizes we are reporting are
4461 // located.
4462 StringRef SymName = "?";
4463 Expected<StringRef> NameOrErr = RelocSym->getName();
4464 if (NameOrErr)
4465 SymName = *NameOrErr;
4466 else
4467 consumeError(NameOrErr.takeError());
4468
4469 auto SectionOrErr = RelocSym->getSection();
4470 if (!SectionOrErr) {
4471 reportWarning(" '" + FileStr +
4472 "': cannot identify the section for relocation symbol " +
4473 SymName);
4474 consumeError(SectionOrErr.takeError());
4475 } else if (*SectionOrErr != FunctionSec) {
4476 reportWarning(" '" + FileStr + "': relocation symbol " + SymName +
4477 " is not in the expected section");
4478 // Pretend that the symbol is in the correct section and report its
4479 // stack size anyway.
4480 FunctionSec = **SectionOrErr;
4481 }
4482
4483 Expected<uint64_t> RelocSymValueOrErr = RelocSym->getValue();
4484 if (RelocSymValueOrErr)
4485 RelocSymValue = *RelocSymValueOrErr;
4486 else
4487 consumeError(RelocSymValueOrErr.takeError());
4488 }
4489
4490 uint64_t Offset = Reloc.getOffset();
4491 if (!Data.isValidOffsetForDataOfSize(Offset, sizeof(Elf_Addr) + 1))
4492 reportError(FileStr, createStringError(
4493 object_error::parse_failed,
4494 "found invalid relocation offset into section %s "
4495 "while trying to extract a stack size entry",
4496 StackSizeSectionName.data()));
4497
Igor Kudrin45ee9332019-08-07 11:44:47 +00004498 uint64_t Addend = Data.getAddress(&Offset);
Wolfgang Piebc71c6292019-08-05 22:47:07 +00004499 uint64_t SymValue = Resolver(Reloc, RelocSymValue, Addend);
4500 this->printFunctionStackSize(Obj, SymValue, FunctionSec, StackSizeSectionName,
4501 Data, &Offset);
4502}
4503
4504template <class ELFT>
4505SectionRef toSectionRef(const ObjectFile *Obj, const typename ELFT::Shdr *Sec) {
4506 DataRefImpl DRI;
4507 DRI.p = reinterpret_cast<uintptr_t>(Sec);
4508 return SectionRef(DRI, Obj);
4509}
4510
4511template <class ELFT>
4512void DumpStyle<ELFT>::printNonRelocatableStackSizes(
4513 const ELFObjectFile<ELFT> *Obj, std::function<void()> PrintHeader) {
4514 // This function ignores potentially erroneous input, unless it is directly
4515 // related to stack size reporting.
4516 const ELFFile<ELFT> *EF = Obj->getELFFile();
4517 StringRef FileStr = Obj->getFileName();
4518 for (const SectionRef &Sec : Obj->sections()) {
4519 StringRef SectionName;
4520 Sec.getName(SectionName);
4521 const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl());
4522 if (!SectionName.startswith(".stack_sizes"))
4523 continue;
4524 PrintHeader();
4525 ArrayRef<uint8_t> Contents = unwrapOrError(EF->getSectionContents(ElfSec));
4526 DataExtractor Data(
4527 StringRef(reinterpret_cast<const char *>(Contents.data()),
4528 Contents.size()),
4529 Obj->isLittleEndian(), sizeof(Elf_Addr));
4530 // A .stack_sizes section header's sh_link field is supposed to point
4531 // to the section that contains the functions whose stack sizes are
4532 // described in it.
4533 const Elf_Shdr *FunctionELFSec =
4534 unwrapOrError(EF->getSection(ElfSec->sh_link));
4535 uint64_t Offset = 0;
4536 while (Offset < Contents.size()) {
4537 // The function address is followed by a ULEB representing the stack
4538 // size. Check for an extra byte before we try to process the entry.
4539 if (!Data.isValidOffsetForDataOfSize(Offset, sizeof(Elf_Addr) + 1)) {
4540 reportError(
4541 FileStr,
4542 createStringError(
4543 object_error::parse_failed,
4544 "section %s ended while trying to extract a stack size entry",
4545 SectionName.data()));
4546 }
Igor Kudrin45ee9332019-08-07 11:44:47 +00004547 uint64_t SymValue = Data.getAddress(&Offset);
Wolfgang Piebc71c6292019-08-05 22:47:07 +00004548 printFunctionStackSize(Obj, SymValue,
4549 toSectionRef<ELFT>(Obj, FunctionELFSec),
4550 SectionName, Data, &Offset);
4551 }
4552 }
4553}
4554
4555template <class ELFT>
4556void DumpStyle<ELFT>::printRelocatableStackSizes(
4557 const ELFObjectFile<ELFT> *Obj, std::function<void()> PrintHeader) {
4558 const ELFFile<ELFT> *EF = Obj->getELFFile();
4559 StringRef FileStr = Obj->getFileName();
4560 // Build a map between stack size sections and their corresponding relocation
4561 // sections.
4562 llvm::MapVector<SectionRef, SectionRef> StackSizeRelocMap;
4563 const SectionRef NullSection;
4564
4565 for (const SectionRef &Sec : Obj->sections()) {
4566 StringRef SectionName;
4567 Sec.getName(SectionName);
4568 // A stack size section that we haven't encountered yet is mapped to the
4569 // null section until we find its corresponding relocation section.
4570 if (SectionName.startswith(".stack_sizes"))
4571 if (StackSizeRelocMap.count(Sec) == 0) {
4572 StackSizeRelocMap[Sec] = NullSection;
4573 continue;
4574 }
4575
4576 // Check relocation sections if they are relocating contents of a
4577 // stack sizes section.
4578 const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl());
4579 uint32_t SectionType = ElfSec->sh_type;
4580 if (SectionType != ELF::SHT_RELA && SectionType != ELF::SHT_REL)
4581 continue;
4582
4583 SectionRef Contents = *Sec.getRelocatedSection();
4584 const Elf_Shdr *ContentsSec = Obj->getSection(Contents.getRawDataRefImpl());
4585 Expected<StringRef> ContentsSectionNameOrErr =
4586 EF->getSectionName(ContentsSec);
4587 if (!ContentsSectionNameOrErr) {
4588 consumeError(ContentsSectionNameOrErr.takeError());
4589 continue;
4590 }
4591 if (!ContentsSectionNameOrErr->startswith(".stack_sizes"))
4592 continue;
4593 // Insert a mapping from the stack sizes section to its relocation section.
4594 StackSizeRelocMap[toSectionRef<ELFT>(Obj, ContentsSec)] = Sec;
4595 }
4596
4597 for (const auto &StackSizeMapEntry : StackSizeRelocMap) {
4598 PrintHeader();
4599 const SectionRef &StackSizesSec = StackSizeMapEntry.first;
4600 const SectionRef &RelocSec = StackSizeMapEntry.second;
4601
4602 // Warn about stack size sections without a relocation section.
4603 StringRef StackSizeSectionName;
4604 StackSizesSec.getName(StackSizeSectionName);
4605 if (RelocSec == NullSection) {
4606 reportWarning(" '" + FileStr + "': section " + StackSizeSectionName +
4607 " does not have a corresponding "
4608 "relocation section");
4609 continue;
4610 }
4611
4612 // A .stack_sizes section header's sh_link field is supposed to point
4613 // to the section that contains the functions whose stack sizes are
4614 // described in it.
4615 const Elf_Shdr *StackSizesELFSec =
4616 Obj->getSection(StackSizesSec.getRawDataRefImpl());
4617 const SectionRef FunctionSec = toSectionRef<ELFT>(
4618 Obj, unwrapOrError(EF->getSection(StackSizesELFSec->sh_link)));
4619
4620 bool (*IsSupportedFn)(uint64_t);
4621 RelocationResolver Resolver;
4622 std::tie(IsSupportedFn, Resolver) = getRelocationResolver(*Obj);
4623 auto Contents = unwrapOrError(StackSizesSec.getContents());
4624 DataExtractor Data(
4625 StringRef(reinterpret_cast<const char *>(Contents.data()),
4626 Contents.size()),
4627 Obj->isLittleEndian(), sizeof(Elf_Addr));
4628 for (const RelocationRef &Reloc : RelocSec.relocations()) {
4629 if (!IsSupportedFn(Reloc.getType())) {
4630 StringRef RelocSectionName;
4631 RelocSec.getName(RelocSectionName);
4632 StringRef RelocName = EF->getRelocationTypeName(Reloc.getType());
4633 reportError(
4634 FileStr,
4635 createStringError(object_error::parse_failed,
4636 "unsupported relocation type in section %s: %s",
4637 RelocSectionName.data(), RelocName.data()));
4638 }
4639 this->printStackSize(Obj, Reloc, FunctionSec, StackSizeSectionName,
4640 Resolver, Data);
4641 }
4642 }
4643}
4644
4645template <class ELFT>
4646void GNUStyle<ELFT>::printStackSizes(const ELFObjectFile<ELFT> *Obj) {
4647 bool HeaderHasBeenPrinted = false;
4648 auto PrintHeader = [&]() {
4649 if (HeaderHasBeenPrinted)
4650 return;
4651 OS << "\nStack Sizes:\n";
4652 OS.PadToColumn(9);
4653 OS << "Size";
4654 OS.PadToColumn(18);
4655 OS << "Function\n";
4656 HeaderHasBeenPrinted = true;
4657 };
4658
4659 // For non-relocatable objects, look directly for sections whose name starts
4660 // with .stack_sizes and process the contents.
4661 if (Obj->isRelocatableObject())
4662 this->printRelocatableStackSizes(Obj, PrintHeader);
4663 else
4664 this->printNonRelocatableStackSizes(Obj, PrintHeader);
4665}
4666
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +00004667template <class ELFT>
Simon Atanasyan62d32592017-12-21 10:26:02 +00004668void GNUStyle<ELFT>::printMipsGOT(const MipsGOTParser<ELFT> &Parser) {
4669 size_t Bias = ELFT::Is64Bits ? 8 : 0;
4670 auto PrintEntry = [&](const Elf_Addr *E, StringRef Purpose) {
4671 OS.PadToColumn(2);
4672 OS << format_hex_no_prefix(Parser.getGotAddress(E), 8 + Bias);
4673 OS.PadToColumn(11 + Bias);
4674 OS << format_decimal(Parser.getGotOffset(E), 6) << "(gp)";
4675 OS.PadToColumn(22 + Bias);
4676 OS << format_hex_no_prefix(*E, 8 + Bias);
4677 OS.PadToColumn(31 + 2 * Bias);
4678 OS << Purpose << "\n";
4679 };
4680
4681 OS << (Parser.IsStatic ? "Static GOT:\n" : "Primary GOT:\n");
4682 OS << " Canonical gp value: "
4683 << format_hex_no_prefix(Parser.getGp(), 8 + Bias) << "\n\n";
4684
4685 OS << " Reserved entries:\n";
Simon Atanasyane4f01ec2019-05-24 10:26:48 +00004686 if (ELFT::Is64Bits)
4687 OS << " Address Access Initial Purpose\n";
4688 else
4689 OS << " Address Access Initial Purpose\n";
Simon Atanasyan62d32592017-12-21 10:26:02 +00004690 PrintEntry(Parser.getGotLazyResolver(), "Lazy resolver");
4691 if (Parser.getGotModulePointer())
4692 PrintEntry(Parser.getGotModulePointer(), "Module pointer (GNU extension)");
4693
4694 if (!Parser.getLocalEntries().empty()) {
4695 OS << "\n";
4696 OS << " Local entries:\n";
Simon Atanasyane4f01ec2019-05-24 10:26:48 +00004697 if (ELFT::Is64Bits)
4698 OS << " Address Access Initial\n";
4699 else
4700 OS << " Address Access Initial\n";
Simon Atanasyan62d32592017-12-21 10:26:02 +00004701 for (auto &E : Parser.getLocalEntries())
4702 PrintEntry(&E, "");
4703 }
4704
4705 if (Parser.IsStatic)
4706 return;
4707
4708 if (!Parser.getGlobalEntries().empty()) {
4709 OS << "\n";
4710 OS << " Global entries:\n";
Simon Atanasyane4f01ec2019-05-24 10:26:48 +00004711 if (ELFT::Is64Bits)
4712 OS << " Address Access Initial Sym.Val."
4713 << " Type Ndx Name\n";
4714 else
4715 OS << " Address Access Initial Sym.Val. Type Ndx Name\n";
Simon Atanasyan62d32592017-12-21 10:26:02 +00004716 for (auto &E : Parser.getGlobalEntries()) {
4717 const Elf_Sym *Sym = Parser.getGotSym(&E);
4718 std::string SymName = this->dumper()->getFullSymbolName(
4719 Sym, this->dumper()->getDynamicStringTable(), false);
4720
4721 OS.PadToColumn(2);
4722 OS << to_string(format_hex_no_prefix(Parser.getGotAddress(&E), 8 + Bias));
4723 OS.PadToColumn(11 + Bias);
4724 OS << to_string(format_decimal(Parser.getGotOffset(&E), 6)) + "(gp)";
4725 OS.PadToColumn(22 + Bias);
4726 OS << to_string(format_hex_no_prefix(E, 8 + Bias));
4727 OS.PadToColumn(31 + 2 * Bias);
4728 OS << to_string(format_hex_no_prefix(Sym->st_value, 8 + Bias));
4729 OS.PadToColumn(40 + 3 * Bias);
4730 OS << printEnum(Sym->getType(), makeArrayRef(ElfSymbolTypes));
4731 OS.PadToColumn(48 + 3 * Bias);
4732 OS << getSymbolSectionNdx(Parser.Obj, Sym,
4733 this->dumper()->dynamic_symbols().begin());
4734 OS.PadToColumn(52 + 3 * Bias);
4735 OS << SymName << "\n";
4736 }
4737 }
4738
4739 if (!Parser.getOtherEntries().empty())
4740 OS << "\n Number of TLS and multi-GOT entries "
4741 << Parser.getOtherEntries().size() << "\n";
4742}
4743
4744template <class ELFT>
4745void GNUStyle<ELFT>::printMipsPLT(const MipsGOTParser<ELFT> &Parser) {
4746 size_t Bias = ELFT::Is64Bits ? 8 : 0;
4747 auto PrintEntry = [&](const Elf_Addr *E, StringRef Purpose) {
4748 OS.PadToColumn(2);
Hans Wennborg5b861632019-05-28 11:24:20 +00004749 OS << format_hex_no_prefix(Parser.getPltAddress(E), 8 + Bias);
Simon Atanasyan62d32592017-12-21 10:26:02 +00004750 OS.PadToColumn(11 + Bias);
4751 OS << format_hex_no_prefix(*E, 8 + Bias);
4752 OS.PadToColumn(20 + 2 * Bias);
4753 OS << Purpose << "\n";
4754 };
4755
4756 OS << "PLT GOT:\n\n";
4757
4758 OS << " Reserved entries:\n";
4759 OS << " Address Initial Purpose\n";
4760 PrintEntry(Parser.getPltLazyResolver(), "PLT lazy resolver");
4761 if (Parser.getPltModulePointer())
Hans Wennborg5b861632019-05-28 11:24:20 +00004762 PrintEntry(Parser.getPltModulePointer(), "Module pointer");
Simon Atanasyan62d32592017-12-21 10:26:02 +00004763
4764 if (!Parser.getPltEntries().empty()) {
4765 OS << "\n";
4766 OS << " Entries:\n";
4767 OS << " Address Initial Sym.Val. Type Ndx Name\n";
4768 for (auto &E : Parser.getPltEntries()) {
4769 const Elf_Sym *Sym = Parser.getPltSym(&E);
4770 std::string SymName = this->dumper()->getFullSymbolName(
4771 Sym, this->dumper()->getDynamicStringTable(), false);
4772
4773 OS.PadToColumn(2);
Hans Wennborg5b861632019-05-28 11:24:20 +00004774 OS << to_string(format_hex_no_prefix(Parser.getPltAddress(&E), 8 + Bias));
Simon Atanasyan62d32592017-12-21 10:26:02 +00004775 OS.PadToColumn(11 + Bias);
4776 OS << to_string(format_hex_no_prefix(E, 8 + Bias));
4777 OS.PadToColumn(20 + 2 * Bias);
4778 OS << to_string(format_hex_no_prefix(Sym->st_value, 8 + Bias));
4779 OS.PadToColumn(29 + 3 * Bias);
4780 OS << printEnum(Sym->getType(), makeArrayRef(ElfSymbolTypes));
4781 OS.PadToColumn(37 + 3 * Bias);
4782 OS << getSymbolSectionNdx(Parser.Obj, Sym,
4783 this->dumper()->dynamic_symbols().begin());
4784 OS.PadToColumn(41 + 3 * Bias);
4785 OS << SymName << "\n";
4786 }
4787 }
4788}
4789
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004790template <class ELFT> void LLVMStyle<ELFT>::printFileHeaders(const ELFO *Obj) {
George Rimar1206f5a2019-01-30 15:39:05 +00004791 const Elf_Ehdr *E = Obj->getHeader();
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00004792 {
4793 DictScope D(W, "ElfHeader");
4794 {
4795 DictScope D(W, "Ident");
George Rimar1206f5a2019-01-30 15:39:05 +00004796 W.printBinary("Magic", makeArrayRef(E->e_ident).slice(ELF::EI_MAG0, 4));
4797 W.printEnum("Class", E->e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
4798 W.printEnum("DataEncoding", E->e_ident[ELF::EI_DATA],
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00004799 makeArrayRef(ElfDataEncoding));
George Rimar1206f5a2019-01-30 15:39:05 +00004800 W.printNumber("FileVersion", E->e_ident[ELF::EI_VERSION]);
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00004801
Konstantin Zhuravlyov121125f2017-10-02 20:49:58 +00004802 auto OSABI = makeArrayRef(ElfOSABI);
George Rimar1206f5a2019-01-30 15:39:05 +00004803 if (E->e_ident[ELF::EI_OSABI] >= ELF::ELFOSABI_FIRST_ARCH &&
4804 E->e_ident[ELF::EI_OSABI] <= ELF::ELFOSABI_LAST_ARCH) {
4805 switch (E->e_machine) {
Konstantin Zhuravlyov121125f2017-10-02 20:49:58 +00004806 case ELF::EM_AMDGPU:
4807 OSABI = makeArrayRef(AMDGPUElfOSABI);
4808 break;
4809 case ELF::EM_ARM:
4810 OSABI = makeArrayRef(ARMElfOSABI);
4811 break;
4812 case ELF::EM_TI_C6000:
4813 OSABI = makeArrayRef(C6000ElfOSABI);
4814 break;
4815 }
4816 }
George Rimar1206f5a2019-01-30 15:39:05 +00004817 W.printEnum("OS/ABI", E->e_ident[ELF::EI_OSABI], OSABI);
4818 W.printNumber("ABIVersion", E->e_ident[ELF::EI_ABIVERSION]);
4819 W.printBinary("Unused", makeArrayRef(E->e_ident).slice(ELF::EI_PAD));
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00004820 }
4821
George Rimar1206f5a2019-01-30 15:39:05 +00004822 W.printEnum("Type", E->e_type, makeArrayRef(ElfObjectFileType));
4823 W.printEnum("Machine", E->e_machine, makeArrayRef(ElfMachineType));
4824 W.printNumber("Version", E->e_version);
4825 W.printHex("Entry", E->e_entry);
4826 W.printHex("ProgramHeaderOffset", E->e_phoff);
4827 W.printHex("SectionHeaderOffset", E->e_shoff);
4828 if (E->e_machine == EM_MIPS)
4829 W.printFlags("Flags", E->e_flags, makeArrayRef(ElfHeaderMipsFlags),
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00004830 unsigned(ELF::EF_MIPS_ARCH), unsigned(ELF::EF_MIPS_ABI),
4831 unsigned(ELF::EF_MIPS_MACH));
George Rimar1206f5a2019-01-30 15:39:05 +00004832 else if (E->e_machine == EM_AMDGPU)
4833 W.printFlags("Flags", E->e_flags, makeArrayRef(ElfHeaderAMDGPUFlags),
Konstantin Zhuravlyov9122a632018-02-16 22:33:59 +00004834 unsigned(ELF::EF_AMDGPU_MACH));
George Rimar1206f5a2019-01-30 15:39:05 +00004835 else if (E->e_machine == EM_RISCV)
4836 W.printFlags("Flags", E->e_flags, makeArrayRef(ElfHeaderRISCVFlags));
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00004837 else
George Rimar1206f5a2019-01-30 15:39:05 +00004838 W.printFlags("Flags", E->e_flags);
4839 W.printNumber("HeaderSize", E->e_ehsize);
4840 W.printNumber("ProgramHeaderEntrySize", E->e_phentsize);
4841 W.printNumber("ProgramHeaderCount", E->e_phnum);
4842 W.printNumber("SectionHeaderEntrySize", E->e_shentsize);
George Rimar6fdac3b2018-07-18 08:19:58 +00004843 W.printString("SectionHeaderCount", getSectionHeadersNumString(Obj));
George Rimar9e88a262019-05-14 14:22:44 +00004844 W.printString("StringTableSectionIndex",
4845 getSectionHeaderTableIndexString(Obj));
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +00004846 }
4847}
Hemant Kulkarni206ba842016-03-09 19:16:13 +00004848
4849template <class ELFT>
4850void LLVMStyle<ELFT>::printGroupSections(const ELFO *Obj) {
4851 DictScope Lists(W, "Groups");
George Rimarea39eed2017-09-14 07:32:52 +00004852 std::vector<GroupSection> V = getGroups<ELFT>(Obj);
George Rimar762abff62017-09-16 14:29:51 +00004853 DenseMap<uint64_t, const GroupSection *> Map = mapSectionsToGroups(V);
George Rimarea39eed2017-09-14 07:32:52 +00004854 for (const GroupSection &G : V) {
4855 DictScope D(W, "Group");
4856 W.printNumber("Name", G.Name, G.ShName);
4857 W.printNumber("Index", G.Index);
Alexander Shaposhnikov8febe3d2018-03-12 22:40:09 +00004858 W.printNumber("Link", G.Link);
4859 W.printNumber("Info", G.Info);
George Rimarea39eed2017-09-14 07:32:52 +00004860 W.printHex("Type", getGroupType(G.Type), G.Type);
4861 W.startLine() << "Signature: " << G.Signature << "\n";
4862
4863 ListScope L(W, "Section(s) in group");
George Rimar762abff62017-09-16 14:29:51 +00004864 for (const GroupMember &GM : G.Members) {
4865 const GroupSection *MainGroup = Map[GM.Index];
4866 if (MainGroup != &G) {
4867 W.flush();
4868 errs() << "Error: " << GM.Name << " (" << GM.Index
4869 << ") in a group " + G.Name + " (" << G.Index
4870 << ") is already in a group " + MainGroup->Name + " ("
4871 << MainGroup->Index << ")\n";
4872 errs().flush();
4873 continue;
4874 }
George Rimarea39eed2017-09-14 07:32:52 +00004875 W.startLine() << GM.Name << " (" << GM.Index << ")\n";
George Rimar762abff62017-09-16 14:29:51 +00004876 }
Hemant Kulkarni206ba842016-03-09 19:16:13 +00004877 }
George Rimarea39eed2017-09-14 07:32:52 +00004878
4879 if (V.empty())
Hemant Kulkarni206ba842016-03-09 19:16:13 +00004880 W.startLine() << "There are no group sections in the file.\n";
4881}
George Rimar762abff62017-09-16 14:29:51 +00004882
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004883template <class ELFT> void LLVMStyle<ELFT>::printRelocations(const ELFO *Obj) {
4884 ListScope D(W, "Relocations");
4885
4886 int SectionNumber = -1;
Rafael Espindola25be8c82016-11-02 14:10:57 +00004887 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004888 ++SectionNumber;
4889
George Rimar9e88a262019-05-14 14:22:44 +00004890 if (Sec.sh_type != ELF::SHT_REL && Sec.sh_type != ELF::SHT_RELA &&
4891 Sec.sh_type != ELF::SHT_RELR && Sec.sh_type != ELF::SHT_ANDROID_REL &&
Jake Ehrlich0f440d82018-06-28 21:07:34 +00004892 Sec.sh_type != ELF::SHT_ANDROID_RELA &&
4893 Sec.sh_type != ELF::SHT_ANDROID_RELR)
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004894 continue;
4895
4896 StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
4897
4898 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
4899 W.indent();
4900
4901 printRelocations(&Sec, Obj);
4902
4903 W.unindent();
4904 W.startLine() << "}\n";
4905 }
4906}
4907
4908template <class ELFT>
4909void LLVMStyle<ELFT>::printRelocations(const Elf_Shdr *Sec, const ELFO *Obj) {
4910 const Elf_Shdr *SymTab = unwrapOrError(Obj->getSection(Sec->sh_link));
4911
4912 switch (Sec->sh_type) {
4913 case ELF::SHT_REL:
Rafael Espindola354680a2016-11-03 19:07:15 +00004914 for (const Elf_Rel &R : unwrapOrError(Obj->rels(Sec))) {
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004915 Elf_Rela Rela;
4916 Rela.r_offset = R.r_offset;
4917 Rela.r_info = R.r_info;
4918 Rela.r_addend = 0;
4919 printRelocation(Obj, Rela, SymTab);
4920 }
4921 break;
4922 case ELF::SHT_RELA:
Rafael Espindola354680a2016-11-03 19:07:15 +00004923 for (const Elf_Rela &R : unwrapOrError(Obj->relas(Sec)))
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004924 printRelocation(Obj, R, SymTab);
4925 break;
Jake Ehrlich0f440d82018-06-28 21:07:34 +00004926 case ELF::SHT_RELR:
4927 case ELF::SHT_ANDROID_RELR: {
4928 Elf_Relr_Range Relrs = unwrapOrError(Obj->relrs(Sec));
4929 if (opts::RawRelr) {
4930 for (const Elf_Relr &R : Relrs)
4931 W.startLine() << W.hex(R) << "\n";
4932 } else {
4933 std::vector<Elf_Rela> RelrRelas = unwrapOrError(Obj->decode_relrs(Relrs));
4934 for (const Elf_Rela &R : RelrRelas)
4935 printRelocation(Obj, R, SymTab);
4936 }
4937 break;
4938 }
Peter Collingbourne689e6c052017-10-25 03:37:12 +00004939 case ELF::SHT_ANDROID_REL:
4940 case ELF::SHT_ANDROID_RELA:
4941 for (const Elf_Rela &R : unwrapOrError(Obj->android_relas(Sec)))
4942 printRelocation(Obj, R, SymTab);
4943 break;
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004944 }
4945}
4946
4947template <class ELFT>
4948void LLVMStyle<ELFT>::printRelocation(const ELFO *Obj, Elf_Rela Rel,
4949 const Elf_Shdr *SymTab) {
4950 SmallString<32> RelocName;
4951 Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
James Hendersone50d9cb2019-01-17 15:34:12 +00004952 std::string TargetName;
Rafael Espindolaed1395a2016-11-03 18:05:33 +00004953 const Elf_Sym *Sym = unwrapOrError(Obj->getRelocationSymbol(&Rel, SymTab));
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004954 if (Sym && Sym->getType() == ELF::STT_SECTION) {
4955 const Elf_Shdr *Sec = unwrapOrError(
4956 Obj->getSection(Sym, SymTab, this->dumper()->getShndxTable()));
4957 TargetName = unwrapOrError(Obj->getSectionName(Sec));
4958 } else if (Sym) {
4959 StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*SymTab));
Xing GUOeec32062019-03-12 14:30:13 +00004960 TargetName = this->dumper()->getFullSymbolName(
4961 Sym, StrTable, SymTab->sh_type == SHT_DYNSYM /* IsDynamic */);
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004962 }
4963
4964 if (opts::ExpandRelocs) {
4965 DictScope Group(W, "Relocation");
4966 W.printHex("Offset", Rel.r_offset);
4967 W.printNumber("Type", RelocName, (int)Rel.getType(Obj->isMips64EL()));
Eugene Zelenko416e0592017-06-09 21:41:54 +00004968 W.printNumber("Symbol", !TargetName.empty() ? TargetName : "-",
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004969 Rel.getSymbol(Obj->isMips64EL()));
4970 W.printHex("Addend", Rel.r_addend);
4971 } else {
4972 raw_ostream &OS = W.startLine();
4973 OS << W.hex(Rel.r_offset) << " " << RelocName << " "
George Rimar9e88a262019-05-14 14:22:44 +00004974 << (!TargetName.empty() ? TargetName : "-") << " " << W.hex(Rel.r_addend)
4975 << "\n";
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004976 }
4977}
4978
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +00004979template <class ELFT>
4980void LLVMStyle<ELFT>::printSectionHeaders(const ELFO *Obj) {
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004981 ListScope SectionsD(W, "Sections");
4982
4983 int SectionIndex = -1;
George Rimard6df7de2019-06-14 11:56:10 +00004984 ArrayRef<Elf_Shdr> Sections = unwrapOrError(Obj->sections());
George Rimara1370872019-07-16 11:07:30 +00004985 const ELFObjectFile<ELFT> *ElfObj = this->dumper()->getElfObject();
George Rimard6df7de2019-06-14 11:56:10 +00004986 for (const Elf_Shdr &Sec : Sections) {
George Rimara1370872019-07-16 11:07:30 +00004987 StringRef Name = getSectionName(Sec, *ElfObj, Sections);
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004988 DictScope SectionD(W, "Section");
George Rimard6df7de2019-06-14 11:56:10 +00004989 W.printNumber("Index", ++SectionIndex);
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004990 W.printNumber("Name", Name, Sec.sh_name);
Rafael Espindola3ba25732017-05-02 14:04:52 +00004991 W.printHex(
4992 "Type",
4993 object::getELFSectionTypeName(Obj->getHeader()->e_machine, Sec.sh_type),
4994 Sec.sh_type);
Hemant Kulkarnic030f232016-03-15 17:25:31 +00004995 std::vector<EnumEntry<unsigned>> SectionFlags(std::begin(ElfSectionFlags),
4996 std::end(ElfSectionFlags));
4997 switch (Obj->getHeader()->e_machine) {
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +00004998 case EM_ARM:
4999 SectionFlags.insert(SectionFlags.end(), std::begin(ElfARMSectionFlags),
5000 std::end(ElfARMSectionFlags));
5001 break;
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005002 case EM_HEXAGON:
5003 SectionFlags.insert(SectionFlags.end(),
5004 std::begin(ElfHexagonSectionFlags),
5005 std::end(ElfHexagonSectionFlags));
5006 break;
5007 case EM_MIPS:
5008 SectionFlags.insert(SectionFlags.end(), std::begin(ElfMipsSectionFlags),
5009 std::end(ElfMipsSectionFlags));
5010 break;
5011 case EM_X86_64:
5012 SectionFlags.insert(SectionFlags.end(), std::begin(ElfX86_64SectionFlags),
5013 std::end(ElfX86_64SectionFlags));
5014 break;
George Rimarc13c59a2016-05-21 10:16:58 +00005015 case EM_XCORE:
5016 SectionFlags.insert(SectionFlags.end(), std::begin(ElfXCoreSectionFlags),
5017 std::end(ElfXCoreSectionFlags));
5018 break;
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005019 default:
5020 // Nothing to do.
5021 break;
5022 }
5023 W.printFlags("Flags", Sec.sh_flags, makeArrayRef(SectionFlags));
5024 W.printHex("Address", Sec.sh_addr);
5025 W.printHex("Offset", Sec.sh_offset);
5026 W.printNumber("Size", Sec.sh_size);
5027 W.printNumber("Link", Sec.sh_link);
5028 W.printNumber("Info", Sec.sh_info);
5029 W.printNumber("AddressAlignment", Sec.sh_addralign);
5030 W.printNumber("EntrySize", Sec.sh_entsize);
5031
5032 if (opts::SectionRelocations) {
5033 ListScope D(W, "Relocations");
5034 printRelocations(&Sec, Obj);
5035 }
5036
5037 if (opts::SectionSymbols) {
5038 ListScope D(W, "Symbols");
5039 const Elf_Shdr *Symtab = this->dumper()->getDotSymtabSec();
5040 StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*Symtab));
5041
Rafael Espindola9ea68342016-11-03 13:43:30 +00005042 for (const Elf_Sym &Sym : unwrapOrError(Obj->symbols(Symtab))) {
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005043 const Elf_Shdr *SymSec = unwrapOrError(
5044 Obj->getSection(&Sym, Symtab, this->dumper()->getShndxTable()));
5045 if (SymSec == &Sec)
Rafael Espindola9ea68342016-11-03 13:43:30 +00005046 printSymbol(Obj, &Sym, unwrapOrError(Obj->symbols(Symtab)).begin(),
5047 StrTable, false);
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005048 }
5049 }
5050
5051 if (opts::SectionData && Sec.sh_type != ELF::SHT_NOBITS) {
5052 ArrayRef<uint8_t> Data = unwrapOrError(Obj->getSectionContents(&Sec));
Xing GUO0df95d22019-04-08 11:48:36 +00005053 W.printBinaryBlock(
5054 "SectionData",
5055 StringRef(reinterpret_cast<const char *>(Data.data()), Data.size()));
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005056 }
5057 }
5058}
5059
5060template <class ELFT>
5061void LLVMStyle<ELFT>::printSymbol(const ELFO *Obj, const Elf_Sym *Symbol,
5062 const Elf_Sym *First, StringRef StrTable,
5063 bool IsDynamic) {
5064 unsigned SectionIndex = 0;
5065 StringRef SectionName;
Simon Atanasyan62d32592017-12-21 10:26:02 +00005066 this->dumper()->getSectionNameIndex(Symbol, First, SectionName, SectionIndex);
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005067 std::string FullSymbolName =
5068 this->dumper()->getFullSymbolName(Symbol, StrTable, IsDynamic);
5069 unsigned char SymbolType = Symbol->getType();
5070
5071 DictScope D(W, "Symbol");
5072 W.printNumber("Name", FullSymbolName, Symbol->st_name);
5073 W.printHex("Value", Symbol->st_value);
5074 W.printNumber("Size", Symbol->st_size);
5075 W.printEnum("Binding", Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
5076 if (Obj->getHeader()->e_machine == ELF::EM_AMDGPU &&
5077 SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
5078 W.printEnum("Type", SymbolType, makeArrayRef(AMDGPUSymbolTypes));
5079 else
5080 W.printEnum("Type", SymbolType, makeArrayRef(ElfSymbolTypes));
Simon Atanasyanb7807a02016-03-24 16:10:37 +00005081 if (Symbol->st_other == 0)
5082 // Usually st_other flag is zero. Do not pollute the output
5083 // by flags enumeration in that case.
5084 W.printNumber("Other", 0);
5085 else {
5086 std::vector<EnumEntry<unsigned>> SymOtherFlags(std::begin(ElfSymOtherFlags),
5087 std::end(ElfSymOtherFlags));
5088 if (Obj->getHeader()->e_machine == EM_MIPS) {
5089 // Someones in their infinite wisdom decided to make STO_MIPS_MIPS16
5090 // flag overlapped with other ST_MIPS_xxx flags. So consider both
5091 // cases separately.
5092 if ((Symbol->st_other & STO_MIPS_MIPS16) == STO_MIPS_MIPS16)
5093 SymOtherFlags.insert(SymOtherFlags.end(),
5094 std::begin(ElfMips16SymOtherFlags),
5095 std::end(ElfMips16SymOtherFlags));
5096 else
5097 SymOtherFlags.insert(SymOtherFlags.end(),
5098 std::begin(ElfMipsSymOtherFlags),
5099 std::end(ElfMipsSymOtherFlags));
5100 }
5101 W.printFlags("Other", Symbol->st_other, makeArrayRef(SymOtherFlags), 0x3u);
5102 }
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005103 W.printHex("Section", SectionName, SectionIndex);
5104}
5105
James Henderson21ed8682019-01-23 16:15:39 +00005106template <class ELFT>
5107void LLVMStyle<ELFT>::printSymbols(const ELFO *Obj, bool PrintSymbols,
5108 bool PrintDynamicSymbols) {
5109 if (PrintSymbols)
5110 printSymbols(Obj);
5111 if (PrintDynamicSymbols)
5112 printDynamicSymbols(Obj);
5113}
5114
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005115template <class ELFT> void LLVMStyle<ELFT>::printSymbols(const ELFO *Obj) {
5116 ListScope Group(W, "Symbols");
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +00005117 this->dumper()->printSymbolsHelper(false);
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005118}
5119
5120template <class ELFT>
5121void LLVMStyle<ELFT>::printDynamicSymbols(const ELFO *Obj) {
5122 ListScope Group(W, "DynamicSymbols");
Hemant Kulkarnia11fbe12016-03-21 17:18:23 +00005123 this->dumper()->printSymbolsHelper(true);
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005124}
5125
Simon Atanasyan8362cbe2019-05-24 12:22:53 +00005126template <class ELFT> void LLVMStyle<ELFT>::printDynamic(const ELFFile<ELFT> *Obj) {
5127 Elf_Dyn_Range Table = this->dumper()->dynamic_table();
5128 if (Table.empty())
5129 return;
5130
5131 raw_ostream &OS = W.getOStream();
5132 W.startLine() << "DynamicSection [ (" << Table.size() << " entries)\n";
5133
5134 bool Is64 = ELFT::Is64Bits;
5135 if (Is64)
5136 W.startLine() << " Tag Type Name/Value\n";
5137 else
5138 W.startLine() << " Tag Type Name/Value\n";
5139 for (auto Entry : Table) {
5140 uintX_t Tag = Entry.getTag();
5141 W.startLine() << " " << format_hex(Tag, Is64 ? 18 : 10, true) << " "
5142 << format("%-21s",
5143 getTypeString(Obj->getHeader()->e_machine, Tag));
5144 this->dumper()->printDynamicEntry(OS, Tag, Entry.getVal());
5145 OS << "\n";
5146 }
5147
5148 W.startLine() << "]\n";
5149}
5150
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005151template <class ELFT>
5152void LLVMStyle<ELFT>::printDynamicRelocations(const ELFO *Obj) {
5153 const DynRegionInfo &DynRelRegion = this->dumper()->getDynRelRegion();
5154 const DynRegionInfo &DynRelaRegion = this->dumper()->getDynRelaRegion();
Jake Ehrlich0f440d82018-06-28 21:07:34 +00005155 const DynRegionInfo &DynRelrRegion = this->dumper()->getDynRelrRegion();
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005156 const DynRegionInfo &DynPLTRelRegion = this->dumper()->getDynPLTRelRegion();
5157 if (DynRelRegion.Size && DynRelaRegion.Size)
5158 report_fatal_error("There are both REL and RELA dynamic relocations");
5159 W.startLine() << "Dynamic Relocations {\n";
5160 W.indent();
5161 if (DynRelaRegion.Size > 0)
5162 for (const Elf_Rela &Rela : this->dumper()->dyn_relas())
5163 printDynamicRelocation(Obj, Rela);
5164 else
5165 for (const Elf_Rel &Rel : this->dumper()->dyn_rels()) {
5166 Elf_Rela Rela;
5167 Rela.r_offset = Rel.r_offset;
5168 Rela.r_info = Rel.r_info;
5169 Rela.r_addend = 0;
5170 printDynamicRelocation(Obj, Rela);
5171 }
Jake Ehrlich0f440d82018-06-28 21:07:34 +00005172 if (DynRelrRegion.Size > 0) {
5173 Elf_Relr_Range Relrs = this->dumper()->dyn_relrs();
5174 std::vector<Elf_Rela> RelrRelas = unwrapOrError(Obj->decode_relrs(Relrs));
5175 for (const Elf_Rela &Rela : RelrRelas)
5176 printDynamicRelocation(Obj, Rela);
5177 }
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005178 if (DynPLTRelRegion.EntSize == sizeof(Elf_Rela))
Rafael Espindolaaafcf752016-04-05 14:47:22 +00005179 for (const Elf_Rela &Rela : DynPLTRelRegion.getAsArrayRef<Elf_Rela>())
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005180 printDynamicRelocation(Obj, Rela);
5181 else
Rafael Espindolaaafcf752016-04-05 14:47:22 +00005182 for (const Elf_Rel &Rel : DynPLTRelRegion.getAsArrayRef<Elf_Rel>()) {
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005183 Elf_Rela Rela;
5184 Rela.r_offset = Rel.r_offset;
5185 Rela.r_info = Rel.r_info;
5186 Rela.r_addend = 0;
5187 printDynamicRelocation(Obj, Rela);
5188 }
5189 W.unindent();
5190 W.startLine() << "}\n";
5191}
5192
5193template <class ELFT>
5194void LLVMStyle<ELFT>::printDynamicRelocation(const ELFO *Obj, Elf_Rela Rel) {
5195 SmallString<32> RelocName;
5196 Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
James Hendersone50d9cb2019-01-17 15:34:12 +00005197 std::string SymbolName;
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005198 uint32_t SymIndex = Rel.getSymbol(Obj->isMips64EL());
5199 const Elf_Sym *Sym = this->dumper()->dynamic_symbols().begin() + SymIndex;
James Hendersone50d9cb2019-01-17 15:34:12 +00005200 SymbolName = maybeDemangle(
5201 unwrapOrError(Sym->getName(this->dumper()->getDynamicStringTable())));
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005202 if (opts::ExpandRelocs) {
5203 DictScope Group(W, "Relocation");
5204 W.printHex("Offset", Rel.r_offset);
5205 W.printNumber("Type", RelocName, (int)Rel.getType(Obj->isMips64EL()));
Eugene Zelenko416e0592017-06-09 21:41:54 +00005206 W.printString("Symbol", !SymbolName.empty() ? SymbolName : "-");
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005207 W.printHex("Addend", Rel.r_addend);
5208 } else {
5209 raw_ostream &OS = W.startLine();
5210 OS << W.hex(Rel.r_offset) << " " << RelocName << " "
George Rimar9e88a262019-05-14 14:22:44 +00005211 << (!SymbolName.empty() ? SymbolName : "-") << " " << W.hex(Rel.r_addend)
5212 << "\n";
Hemant Kulkarnic030f232016-03-15 17:25:31 +00005213 }
5214}
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00005215
5216template <class ELFT>
Matt Davis50ca8ed2019-02-01 18:51:10 +00005217void LLVMStyle<ELFT>::printProgramHeaders(
5218 const ELFO *Obj, bool PrintProgramHeaders,
5219 cl::boolOrDefault PrintSectionMapping) {
5220 if (PrintProgramHeaders)
5221 printProgramHeaders(Obj);
5222 if (PrintSectionMapping == cl::BOU_TRUE)
5223 printSectionMapping(Obj);
5224}
5225
5226template <class ELFT>
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00005227void LLVMStyle<ELFT>::printProgramHeaders(const ELFO *Obj) {
5228 ListScope L(W, "ProgramHeaders");
5229
Rafael Espindola6a494972016-11-03 17:28:33 +00005230 for (const Elf_Phdr &Phdr : unwrapOrError(Obj->program_headers())) {
Hemant Kulkarni966b3ac2016-03-25 16:04:48 +00005231 DictScope P(W, "ProgramHeader");
5232 W.printHex("Type",
5233 getElfSegmentType(Obj->getHeader()->e_machine, Phdr.p_type),
5234 Phdr.p_type);
5235 W.printHex("Offset", Phdr.p_offset);
5236 W.printHex("VirtualAddress", Phdr.p_vaddr);
5237 W.printHex("PhysicalAddress", Phdr.p_paddr);
5238 W.printNumber("FileSize", Phdr.p_filesz);
5239 W.printNumber("MemSize", Phdr.p_memsz);
5240 W.printFlags("Flags", Phdr.p_flags, makeArrayRef(ElfSegmentFlags));
5241 W.printNumber("Alignment", Phdr.p_align);
5242 }
5243}
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00005244
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +00005245template <class ELFT>
Xing GUOea16be12019-03-25 11:02:49 +00005246void LLVMStyle<ELFT>::printVersionSymbolSection(const ELFFile<ELFT> *Obj,
5247 const Elf_Shdr *Sec) {
5248 DictScope SS(W, "Version symbols");
5249 if (!Sec)
5250 return;
5251
5252 StringRef SecName = unwrapOrError(Obj->getSectionName(Sec));
5253 W.printNumber("Section Name", SecName, Sec->sh_name);
5254 W.printHex("Address", Sec->sh_addr);
5255 W.printHex("Offset", Sec->sh_offset);
5256 W.printNumber("Link", Sec->sh_link);
5257
Xing GUO0df95d22019-04-08 11:48:36 +00005258 const uint8_t *VersymBuf =
5259 reinterpret_cast<const uint8_t *>(Obj->base() + Sec->sh_offset);
Xing GUOea16be12019-03-25 11:02:49 +00005260 const ELFDumper<ELFT> *Dumper = this->dumper();
5261 StringRef StrTable = Dumper->getDynamicStringTable();
5262
5263 // Same number of entries in the dynamic symbol table (DT_SYMTAB).
5264 ListScope Syms(W, "Symbols");
5265 for (const Elf_Sym &Sym : Dumper->dynamic_symbols()) {
5266 DictScope S(W, "Symbol");
5267 const Elf_Versym *Versym = reinterpret_cast<const Elf_Versym *>(VersymBuf);
5268 std::string FullSymbolName =
5269 Dumper->getFullSymbolName(&Sym, StrTable, true /* IsDynamic */);
5270 W.printNumber("Version", Versym->vs_index & VERSYM_VERSION);
5271 W.printString("Name", FullSymbolName);
5272 VersymBuf += sizeof(Elf_Versym);
5273 }
5274}
5275
5276template <class ELFT>
5277void LLVMStyle<ELFT>::printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
5278 const Elf_Shdr *Sec) {
5279 DictScope SD(W, "SHT_GNU_verdef");
5280 if (!Sec)
5281 return;
5282
5283 const uint8_t *SecStartAddress =
Xing GUO0df95d22019-04-08 11:48:36 +00005284 reinterpret_cast<const uint8_t *>(Obj->base() + Sec->sh_offset);
Xing GUOea16be12019-03-25 11:02:49 +00005285 const uint8_t *SecEndAddress = SecStartAddress + Sec->sh_size;
5286 const uint8_t *VerdefBuf = SecStartAddress;
5287 const Elf_Shdr *StrTab = unwrapOrError(Obj->getSection(Sec->sh_link));
5288
5289 unsigned VerDefsNum = Sec->sh_info;
5290 while (VerDefsNum--) {
5291 if (VerdefBuf + sizeof(Elf_Verdef) > SecEndAddress)
5292 // FIXME: report_fatal_error is not a good way to report error. We should
5293 // emit a parsing error here and below.
5294 report_fatal_error("invalid offset in the section");
5295
5296 const Elf_Verdef *Verdef = reinterpret_cast<const Elf_Verdef *>(VerdefBuf);
5297 DictScope Def(W, "Definition");
5298 W.printNumber("Version", Verdef->vd_version);
5299 W.printEnum("Flags", Verdef->vd_flags, makeArrayRef(SymVersionFlags));
5300 W.printNumber("Index", Verdef->vd_ndx);
5301 W.printNumber("Hash", Verdef->vd_hash);
Xing GUO0df95d22019-04-08 11:48:36 +00005302 W.printString("Name", StringRef(reinterpret_cast<const char *>(
5303 Obj->base() + StrTab->sh_offset +
5304 Verdef->getAux()->vda_name)));
Xing GUOea16be12019-03-25 11:02:49 +00005305 if (!Verdef->vd_cnt)
5306 report_fatal_error("at least one definition string must exist");
5307 if (Verdef->vd_cnt > 2)
5308 report_fatal_error("more than one predecessor is not expected");
5309
5310 if (Verdef->vd_cnt == 2) {
5311 const uint8_t *VerdauxBuf =
5312 VerdefBuf + Verdef->vd_aux + Verdef->getAux()->vda_next;
5313 const Elf_Verdaux *Verdaux =
5314 reinterpret_cast<const Elf_Verdaux *>(VerdauxBuf);
5315 W.printString("Predecessor",
Xing GUO0df95d22019-04-08 11:48:36 +00005316 StringRef(reinterpret_cast<const char *>(
5317 Obj->base() + StrTab->sh_offset + Verdaux->vda_name)));
Xing GUOea16be12019-03-25 11:02:49 +00005318 }
5319 VerdefBuf += Verdef->vd_next;
5320 }
5321}
5322
5323template <class ELFT>
5324void LLVMStyle<ELFT>::printVersionDependencySection(const ELFFile<ELFT> *Obj,
5325 const Elf_Shdr *Sec) {
5326 DictScope SD(W, "SHT_GNU_verneed");
5327 if (!Sec)
5328 return;
5329
Xing GUO0df95d22019-04-08 11:48:36 +00005330 const uint8_t *SecData =
5331 reinterpret_cast<const uint8_t *>(Obj->base() + Sec->sh_offset);
Xing GUOea16be12019-03-25 11:02:49 +00005332 const Elf_Shdr *StrTab = unwrapOrError(Obj->getSection(Sec->sh_link));
5333
5334 const uint8_t *VerneedBuf = SecData;
5335 unsigned VerneedNum = Sec->sh_info;
5336 for (unsigned I = 0; I < VerneedNum; ++I) {
5337 const Elf_Verneed *Verneed =
5338 reinterpret_cast<const Elf_Verneed *>(VerneedBuf);
5339 DictScope Entry(W, "Dependency");
5340 W.printNumber("Version", Verneed->vn_version);
5341 W.printNumber("Count", Verneed->vn_cnt);
5342 W.printString("FileName",
Xing GUO0df95d22019-04-08 11:48:36 +00005343 StringRef(reinterpret_cast<const char *>(
5344 Obj->base() + StrTab->sh_offset + Verneed->vn_file)));
Xing GUOea16be12019-03-25 11:02:49 +00005345
5346 const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux;
Xing GUO8ab74142019-04-10 12:47:21 +00005347 ListScope L(W, "Entries");
Xing GUOea16be12019-03-25 11:02:49 +00005348 for (unsigned J = 0; J < Verneed->vn_cnt; ++J) {
5349 const Elf_Vernaux *Vernaux =
5350 reinterpret_cast<const Elf_Vernaux *>(VernauxBuf);
5351 DictScope Entry(W, "Entry");
5352 W.printNumber("Hash", Vernaux->vna_hash);
5353 W.printEnum("Flags", Vernaux->vna_flags, makeArrayRef(SymVersionFlags));
5354 W.printNumber("Index", Vernaux->vna_other);
5355 W.printString("Name",
Xing GUO0df95d22019-04-08 11:48:36 +00005356 StringRef(reinterpret_cast<const char *>(
5357 Obj->base() + StrTab->sh_offset + Vernaux->vna_name)));
Xing GUOea16be12019-03-25 11:02:49 +00005358 VernauxBuf += Vernaux->vna_next;
5359 }
5360 VerneedBuf += Verneed->vn_next;
5361 }
5362}
5363
5364template <class ELFT>
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +00005365void LLVMStyle<ELFT>::printHashHistogram(const ELFFile<ELFT> *Obj) {
5366 W.startLine() << "Hash Histogram not implemented!\n";
5367}
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00005368
5369template <class ELFT>
Michael J. Spencerae6eeae2018-06-02 16:33:01 +00005370void LLVMStyle<ELFT>::printCGProfile(const ELFFile<ELFT> *Obj) {
5371 ListScope L(W, "CGProfile");
5372 if (!this->dumper()->getDotCGProfileSec())
5373 return;
5374 auto CGProfile =
5375 unwrapOrError(Obj->template getSectionContentsAsArray<Elf_CGProfile>(
5376 this->dumper()->getDotCGProfileSec()));
5377 for (const Elf_CGProfile &CGPE : CGProfile) {
5378 DictScope D(W, "CGProfileEntry");
5379 W.printNumber("From", this->dumper()->getStaticSymbolName(CGPE.cgp_from),
5380 CGPE.cgp_from);
5381 W.printNumber("To", this->dumper()->getStaticSymbolName(CGPE.cgp_to),
5382 CGPE.cgp_to);
5383 W.printNumber("Weight", CGPE.cgp_weight);
5384 }
5385}
5386
5387template <class ELFT>
Peter Collingbourne3e227332018-07-17 22:17:18 +00005388void LLVMStyle<ELFT>::printAddrsig(const ELFFile<ELFT> *Obj) {
5389 ListScope L(W, "Addrsig");
5390 if (!this->dumper()->getDotAddrsigSec())
5391 return;
5392 ArrayRef<uint8_t> Contents = unwrapOrError(
5393 Obj->getSectionContents(this->dumper()->getDotAddrsigSec()));
5394 const uint8_t *Cur = Contents.begin();
5395 const uint8_t *End = Contents.end();
5396 while (Cur != End) {
5397 unsigned Size;
5398 const char *Err;
Peter Collingbourne7d179082018-08-21 17:18:18 +00005399 uint64_t SymIndex = decodeULEB128(Cur, &Size, End, &Err);
Peter Collingbourne3e227332018-07-17 22:17:18 +00005400 if (Err)
5401 reportError(Err);
5402 W.printNumber("Sym", this->dumper()->getStaticSymbolName(SymIndex),
5403 SymIndex);
5404 Cur += Size;
5405 }
5406}
5407
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00005408template <typename ELFT>
George Rimar9e88a262019-05-14 14:22:44 +00005409static void printGNUNoteLLVMStyle(uint32_t NoteType, ArrayRef<uint8_t> Desc,
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00005410 ScopedPrinter &W) {
5411 switch (NoteType) {
5412 default:
5413 return;
5414 case ELF::NT_GNU_ABI_TAG: {
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00005415 const GNUAbiTag &AbiTag = getGNUAbiTag<ELFT>(Desc);
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00005416 if (!AbiTag.IsValid) {
5417 W.printString("ABI", "<corrupt GNU_ABI_TAG>");
5418 } else {
5419 W.printString("OS", AbiTag.OSName);
5420 W.printString("ABI", AbiTag.ABI);
5421 }
5422 break;
5423 }
5424 case ELF::NT_GNU_BUILD_ID: {
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00005425 W.printString("Build ID", getGNUBuildId(Desc));
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00005426 break;
5427 }
5428 case ELF::NT_GNU_GOLD_VERSION:
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00005429 W.printString("Version", getGNUGoldVersion(Desc));
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00005430 break;
5431 case ELF::NT_GNU_PROPERTY_TYPE_0:
5432 ListScope D(W, "Property");
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00005433 for (const auto &Property : getGNUPropertyList<ELFT>(Desc))
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00005434 W.printString(Property);
5435 break;
5436 }
5437}
5438
Peter Collingbourne3e227332018-07-17 22:17:18 +00005439template <class ELFT>
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00005440void LLVMStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00005441 ListScope L(W, "Notes");
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00005442
5443 auto PrintHeader = [&](const typename ELFT::Off Offset,
5444 const typename ELFT::Addr Size) {
5445 W.printHex("Offset", Offset);
5446 W.printHex("Size", Size);
5447 };
5448
5449 auto ProcessNote = [&](const Elf_Note &Note) {
5450 DictScope D2(W, "Note");
5451 StringRef Name = Note.getName();
Jake Ehrlichee7ec6c2018-11-13 01:10:35 +00005452 ArrayRef<uint8_t> Descriptor = Note.getDesc();
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00005453 Elf_Word Type = Note.getType();
5454
5455 W.printString("Owner", Name);
5456 W.printHex("Data size", Descriptor.size());
5457 if (Name == "GNU") {
5458 W.printString("Type", getGNUNoteTypeName(Type));
5459 printGNUNoteLLVMStyle<ELFT>(Type, Descriptor, W);
5460 } else if (Name == "FreeBSD") {
5461 W.printString("Type", getFreeBSDNoteTypeName(Type));
5462 } else if (Name == "AMD") {
Scott Linderf5b36e52018-12-12 19:39:27 +00005463 W.printString("Type", getAMDNoteTypeName(Type));
5464 const AMDNote N = getAMDNote<ELFT>(Type, Descriptor);
5465 if (!N.Type.empty())
5466 W.printString(N.Type, N.Value);
5467 } else if (Name == "AMDGPU") {
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00005468 W.printString("Type", getAMDGPUNoteTypeName(Type));
5469 const AMDGPUNote N = getAMDGPUNote<ELFT>(Type, Descriptor);
Scott Linderf5b36e52018-12-12 19:39:27 +00005470 if (!N.Type.empty())
5471 W.printString(N.Type, N.Value);
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00005472 } else {
Jordan Rupprechtd884fbd2019-08-05 15:43:20 +00005473 StringRef NoteType = Obj->getHeader()->e_type == ELF::ET_CORE
5474 ? getCoreNoteTypeName(Type)
5475 : getGenericNoteTypeName(Type);
Jordan Rupprecht871baa22019-03-29 16:48:19 +00005476 if (!NoteType.empty())
5477 W.printString("Type", NoteType);
5478 else
5479 W.printString("Type",
5480 "Unknown (" + to_string(format_hex(Type, 10)) + ")");
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00005481 }
5482 };
5483
George Rimar1206f5a2019-01-30 15:39:05 +00005484 if (Obj->getHeader()->e_type == ELF::ET_CORE) {
Jordan Rupprecht4f36c7a2018-11-07 23:53:50 +00005485 for (const auto &P : unwrapOrError(Obj->program_headers())) {
5486 if (P.p_type != PT_NOTE)
5487 continue;
5488 DictScope D(W, "NoteSection");
5489 PrintHeader(P.p_offset, P.p_filesz);
5490 Error Err = Error::success();
5491 for (const auto &Note : Obj->notes(P, Err))
5492 ProcessNote(Note);
5493 if (Err)
5494 error(std::move(Err));
5495 }
5496 } else {
5497 for (const auto &S : unwrapOrError(Obj->sections())) {
5498 if (S.sh_type != SHT_NOTE)
5499 continue;
5500 DictScope D(W, "NoteSection");
5501 PrintHeader(S.sh_offset, S.sh_size);
5502 Error Err = Error::success();
5503 for (const auto &Note : Obj->notes(S, Err))
5504 ProcessNote(Note);
5505 if (Err)
5506 error(std::move(Err));
5507 }
5508 }
Saleem Abdulrasool6a405442016-08-30 18:52:02 +00005509}
Simon Atanasyan62d32592017-12-21 10:26:02 +00005510
5511template <class ELFT>
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +00005512void LLVMStyle<ELFT>::printELFLinkerOptions(const ELFFile<ELFT> *Obj) {
5513 ListScope L(W, "LinkerOptions");
5514
5515 for (const Elf_Shdr &Shdr : unwrapOrError(Obj->sections())) {
5516 if (Shdr.sh_type != ELF::SHT_LLVM_LINKER_OPTIONS)
5517 continue;
5518
5519 ArrayRef<uint8_t> Contents = unwrapOrError(Obj->getSectionContents(&Shdr));
George Rimarec895f12019-05-16 06:22:51 +00005520 for (const uint8_t *P = Contents.begin(), *E = Contents.end(); P < E; ) {
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +00005521 StringRef Key = StringRef(reinterpret_cast<const char *>(P));
5522 StringRef Value =
5523 StringRef(reinterpret_cast<const char *>(P) + Key.size() + 1);
5524
5525 W.printString(Key, Value);
5526
5527 P = P + Key.size() + Value.size() + 2;
5528 }
5529 }
5530}
5531
5532template <class ELFT>
Wolfgang Piebc71c6292019-08-05 22:47:07 +00005533void LLVMStyle<ELFT>::printStackSizes(const ELFObjectFile<ELFT> *Obj) {
5534 W.printString(
5535 "Dumping of stack sizes in LLVM style is not implemented yet\n");
5536}
5537
5538template <class ELFT>
5539void LLVMStyle<ELFT>::printStackSizeEntry(uint64_t Size, StringRef FuncName) {
5540 // FIXME: Implement this function for LLVM-style dumping.
5541}
5542
5543template <class ELFT>
Simon Atanasyan62d32592017-12-21 10:26:02 +00005544void LLVMStyle<ELFT>::printMipsGOT(const MipsGOTParser<ELFT> &Parser) {
5545 auto PrintEntry = [&](const Elf_Addr *E) {
5546 W.printHex("Address", Parser.getGotAddress(E));
5547 W.printNumber("Access", Parser.getGotOffset(E));
5548 W.printHex("Initial", *E);
5549 };
5550
5551 DictScope GS(W, Parser.IsStatic ? "Static GOT" : "Primary GOT");
5552
5553 W.printHex("Canonical gp value", Parser.getGp());
5554 {
5555 ListScope RS(W, "Reserved entries");
5556 {
5557 DictScope D(W, "Entry");
5558 PrintEntry(Parser.getGotLazyResolver());
5559 W.printString("Purpose", StringRef("Lazy resolver"));
5560 }
5561
5562 if (Parser.getGotModulePointer()) {
5563 DictScope D(W, "Entry");
5564 PrintEntry(Parser.getGotModulePointer());
5565 W.printString("Purpose", StringRef("Module pointer (GNU extension)"));
5566 }
5567 }
5568 {
5569 ListScope LS(W, "Local entries");
5570 for (auto &E : Parser.getLocalEntries()) {
5571 DictScope D(W, "Entry");
5572 PrintEntry(&E);
5573 }
5574 }
5575
5576 if (Parser.IsStatic)
5577 return;
5578
5579 {
5580 ListScope GS(W, "Global entries");
5581 for (auto &E : Parser.getGlobalEntries()) {
5582 DictScope D(W, "Entry");
5583
5584 PrintEntry(&E);
5585
5586 const Elf_Sym *Sym = Parser.getGotSym(&E);
5587 W.printHex("Value", Sym->st_value);
5588 W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes));
5589
5590 unsigned SectionIndex = 0;
5591 StringRef SectionName;
5592 this->dumper()->getSectionNameIndex(
5593 Sym, this->dumper()->dynamic_symbols().begin(), SectionName,
5594 SectionIndex);
5595 W.printHex("Section", SectionName, SectionIndex);
5596
5597 std::string SymName = this->dumper()->getFullSymbolName(
5598 Sym, this->dumper()->getDynamicStringTable(), true);
5599 W.printNumber("Name", SymName, Sym->st_name);
5600 }
5601 }
5602
5603 W.printNumber("Number of TLS and multi-GOT entries",
Simon Atanasyan4e945b62017-12-21 10:46:20 +00005604 uint64_t(Parser.getOtherEntries().size()));
Simon Atanasyan62d32592017-12-21 10:26:02 +00005605}
5606
5607template <class ELFT>
5608void LLVMStyle<ELFT>::printMipsPLT(const MipsGOTParser<ELFT> &Parser) {
5609 auto PrintEntry = [&](const Elf_Addr *E) {
5610 W.printHex("Address", Parser.getPltAddress(E));
5611 W.printHex("Initial", *E);
5612 };
5613
5614 DictScope GS(W, "PLT GOT");
5615
5616 {
5617 ListScope RS(W, "Reserved entries");
5618 {
5619 DictScope D(W, "Entry");
5620 PrintEntry(Parser.getPltLazyResolver());
5621 W.printString("Purpose", StringRef("PLT lazy resolver"));
5622 }
5623
5624 if (auto E = Parser.getPltModulePointer()) {
5625 DictScope D(W, "Entry");
5626 PrintEntry(E);
5627 W.printString("Purpose", StringRef("Module pointer"));
5628 }
5629 }
5630 {
5631 ListScope LS(W, "Entries");
5632 for (auto &E : Parser.getPltEntries()) {
5633 DictScope D(W, "Entry");
5634 PrintEntry(&E);
5635
5636 const Elf_Sym *Sym = Parser.getPltSym(&E);
5637 W.printHex("Value", Sym->st_value);
5638 W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes));
5639
5640 unsigned SectionIndex = 0;
5641 StringRef SectionName;
5642 this->dumper()->getSectionNameIndex(
5643 Sym, this->dumper()->dynamic_symbols().begin(), SectionName,
5644 SectionIndex);
5645 W.printHex("Section", SectionName, SectionIndex);
5646
5647 std::string SymName =
5648 this->dumper()->getFullSymbolName(Sym, Parser.getPltStrTable(), true);
5649 W.printNumber("Name", SymName, Sym->st_name);
5650 }
5651 }
5652}