[ARM] sext of a load is free
[llvm-core.git] / tools / llvm-readobj / ELFDumper.cpp
blob78216ef4f62aa6e71746172d09732c10e5772f82
1 //===- ELFDumper.cpp - ELF-specific dumper --------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements the ELF-specific dumper for llvm-readobj.
11 ///
12 //===----------------------------------------------------------------------===//
14 #include "ARMEHABIPrinter.h"
15 #include "DwarfCFIEHPrinter.h"
16 #include "Error.h"
17 #include "ObjDumper.h"
18 #include "StackMapPrinter.h"
19 #include "llvm-readobj.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/DenseSet.h"
23 #include "llvm/ADT/MapVector.h"
24 #include "llvm/ADT/Optional.h"
25 #include "llvm/ADT/PointerIntPair.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/SmallString.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/ADT/StringExtras.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/ADT/Twine.h"
32 #include "llvm/BinaryFormat/AMDGPUMetadataVerifier.h"
33 #include "llvm/BinaryFormat/ELF.h"
34 #include "llvm/Demangle/Demangle.h"
35 #include "llvm/Object/ELF.h"
36 #include "llvm/Object/ELFObjectFile.h"
37 #include "llvm/Object/ELFTypes.h"
38 #include "llvm/Object/Error.h"
39 #include "llvm/Object/ObjectFile.h"
40 #include "llvm/Object/RelocationResolver.h"
41 #include "llvm/Object/StackMapParser.h"
42 #include "llvm/Support/AMDGPUMetadata.h"
43 #include "llvm/Support/ARMAttributeParser.h"
44 #include "llvm/Support/ARMBuildAttributes.h"
45 #include "llvm/Support/Casting.h"
46 #include "llvm/Support/Compiler.h"
47 #include "llvm/Support/Endian.h"
48 #include "llvm/Support/ErrorHandling.h"
49 #include "llvm/Support/Format.h"
50 #include "llvm/Support/FormatVariadic.h"
51 #include "llvm/Support/FormattedStream.h"
52 #include "llvm/Support/LEB128.h"
53 #include "llvm/Support/MathExtras.h"
54 #include "llvm/Support/MipsABIFlags.h"
55 #include "llvm/Support/ScopedPrinter.h"
56 #include "llvm/Support/raw_ostream.h"
57 #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 <unordered_set>
67 #include <vector>
69 using namespace llvm;
70 using namespace llvm::object;
71 using namespace ELF;
73 #define LLVM_READOBJ_ENUM_CASE(ns, enum) \
74 case ns::enum: \
75 return #enum;
77 #define ENUM_ENT(enum, altName) \
78 { #enum, altName, ELF::enum }
80 #define ENUM_ENT_1(enum) \
81 { #enum, #enum, ELF::enum }
83 #define LLVM_READOBJ_PHDR_ENUM(ns, enum) \
84 case ns::enum: \
85 return std::string(#enum).substr(3);
87 #define TYPEDEF_ELF_TYPES(ELFT) \
88 using ELFO = ELFFile<ELFT>; \
89 using Elf_Addr = typename ELFT::Addr; \
90 using Elf_Shdr = typename ELFT::Shdr; \
91 using Elf_Sym = typename ELFT::Sym; \
92 using Elf_Dyn = typename ELFT::Dyn; \
93 using Elf_Dyn_Range = typename ELFT::DynRange; \
94 using Elf_Rel = typename ELFT::Rel; \
95 using Elf_Rela = typename ELFT::Rela; \
96 using Elf_Relr = typename ELFT::Relr; \
97 using Elf_Rel_Range = typename ELFT::RelRange; \
98 using Elf_Rela_Range = typename ELFT::RelaRange; \
99 using Elf_Relr_Range = typename ELFT::RelrRange; \
100 using Elf_Phdr = typename ELFT::Phdr; \
101 using Elf_Half = typename ELFT::Half; \
102 using Elf_Ehdr = typename ELFT::Ehdr; \
103 using Elf_Word = typename ELFT::Word; \
104 using Elf_Hash = typename ELFT::Hash; \
105 using Elf_GnuHash = typename ELFT::GnuHash; \
106 using Elf_Note = typename ELFT::Note; \
107 using Elf_Sym_Range = typename ELFT::SymRange; \
108 using Elf_Versym = typename ELFT::Versym; \
109 using Elf_Verneed = typename ELFT::Verneed; \
110 using Elf_Vernaux = typename ELFT::Vernaux; \
111 using Elf_Verdef = typename ELFT::Verdef; \
112 using Elf_Verdaux = typename ELFT::Verdaux; \
113 using Elf_CGProfile = typename ELFT::CGProfile; \
114 using uintX_t = typename ELFT::uint;
116 namespace {
118 template <class ELFT> class DumpStyle;
120 /// Represents a contiguous uniform range in the file. We cannot just create a
121 /// range directly because when creating one of these from the .dynamic table
122 /// the size, entity size and virtual address are different entries in arbitrary
123 /// order (DT_REL, DT_RELSZ, DT_RELENT for example).
124 struct DynRegionInfo {
125 DynRegionInfo() = default;
126 DynRegionInfo(const void *A, uint64_t S, uint64_t ES)
127 : Addr(A), Size(S), EntSize(ES) {}
129 /// Address in current address space.
130 const void *Addr = nullptr;
131 /// Size in bytes of the region.
132 uint64_t Size = 0;
133 /// Size of each entity in the region.
134 uint64_t EntSize = 0;
136 template <typename Type> ArrayRef<Type> getAsArrayRef() const {
137 const Type *Start = reinterpret_cast<const Type *>(Addr);
138 if (!Start)
139 return {Start, Start};
140 if (EntSize != sizeof(Type) || Size % EntSize) {
141 // TODO: Add a section index to this warning.
142 reportWarning("invalid section size (" + Twine(Size) +
143 ") or entity size (" + Twine(EntSize) + ")");
144 return {Start, Start};
146 return {Start, Start + (Size / EntSize)};
150 template <typename ELFT> class ELFDumper : public ObjDumper {
151 public:
152 ELFDumper(const object::ELFObjectFile<ELFT> *ObjF, ScopedPrinter &Writer);
154 void printFileHeaders() override;
155 void printSectionHeaders() override;
156 void printRelocations() override;
157 void printDynamicRelocations() override;
158 void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols) override;
159 void printHashSymbols() override;
160 void printUnwindInfo() override;
162 void printDynamicTable() override;
163 void printNeededLibraries() override;
164 void printProgramHeaders(bool PrintProgramHeaders,
165 cl::boolOrDefault PrintSectionMapping) override;
166 void printHashTable() override;
167 void printGnuHashTable() override;
168 void printLoadName() override;
169 void printVersionInfo() override;
170 void printGroupSections() override;
172 void printAttributes() override;
173 void printMipsPLTGOT() override;
174 void printMipsABIFlags() override;
175 void printMipsReginfo() override;
176 void printMipsOptions() override;
178 void printStackMap() const override;
180 void printHashHistogram() override;
182 void printCGProfile() override;
183 void printAddrsig() override;
185 void printNotes() override;
187 void printELFLinkerOptions() override;
188 void printStackSizes() override;
190 const object::ELFObjectFile<ELFT> *getElfObject() const { return ObjF; };
192 private:
193 std::unique_ptr<DumpStyle<ELFT>> ELFDumperStyle;
195 TYPEDEF_ELF_TYPES(ELFT)
197 DynRegionInfo checkDRI(DynRegionInfo DRI) {
198 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
199 if (DRI.Addr < Obj->base() ||
200 reinterpret_cast<const uint8_t *>(DRI.Addr) + DRI.Size >
201 Obj->base() + Obj->getBufSize())
202 error(llvm::object::object_error::parse_failed);
203 return DRI;
206 DynRegionInfo createDRIFrom(const Elf_Phdr *P, uintX_t EntSize) {
207 return checkDRI(
208 {ObjF->getELFFile()->base() + P->p_offset, P->p_filesz, EntSize});
211 DynRegionInfo createDRIFrom(const Elf_Shdr *S) {
212 return checkDRI(
213 {ObjF->getELFFile()->base() + S->sh_offset, S->sh_size, S->sh_entsize});
216 void loadDynamicTable(const ELFFile<ELFT> *Obj);
217 void parseDynamicTable();
219 StringRef getSymbolVersion(StringRef StrTab, const Elf_Sym *symb,
220 bool &IsDefault) const;
221 void LoadVersionMap() const;
222 void LoadVersionNeeds(const Elf_Shdr *ec) const;
223 void LoadVersionDefs(const Elf_Shdr *sec) const;
225 const object::ELFObjectFile<ELFT> *ObjF;
226 DynRegionInfo DynRelRegion;
227 DynRegionInfo DynRelaRegion;
228 DynRegionInfo DynRelrRegion;
229 DynRegionInfo DynPLTRelRegion;
230 DynRegionInfo DynSymRegion;
231 DynRegionInfo DynamicTable;
232 StringRef DynamicStringTable;
233 std::string SOName = "<Not found>";
234 const Elf_Hash *HashTable = nullptr;
235 const Elf_GnuHash *GnuHashTable = nullptr;
236 const Elf_Shdr *DotSymtabSec = nullptr;
237 const Elf_Shdr *DotCGProfileSec = nullptr;
238 const Elf_Shdr *DotAddrsigSec = nullptr;
239 StringRef DynSymtabName;
240 ArrayRef<Elf_Word> ShndxTable;
242 const Elf_Shdr *SymbolVersionSection = nullptr; // .gnu.version
243 const Elf_Shdr *SymbolVersionNeedSection = nullptr; // .gnu.version_r
244 const Elf_Shdr *SymbolVersionDefSection = nullptr; // .gnu.version_d
246 // Records for each version index the corresponding Verdef or Vernaux entry.
247 // This is filled the first time LoadVersionMap() is called.
248 class VersionMapEntry : public PointerIntPair<const void *, 1> {
249 public:
250 // If the integer is 0, this is an Elf_Verdef*.
251 // If the integer is 1, this is an Elf_Vernaux*.
252 VersionMapEntry() : PointerIntPair<const void *, 1>(nullptr, 0) {}
253 VersionMapEntry(const Elf_Verdef *verdef)
254 : PointerIntPair<const void *, 1>(verdef, 0) {}
255 VersionMapEntry(const Elf_Vernaux *vernaux)
256 : PointerIntPair<const void *, 1>(vernaux, 1) {}
258 bool isNull() const { return getPointer() == nullptr; }
259 bool isVerdef() const { return !isNull() && getInt() == 0; }
260 bool isVernaux() const { return !isNull() && getInt() == 1; }
261 const Elf_Verdef *getVerdef() const {
262 return isVerdef() ? (const Elf_Verdef *)getPointer() : nullptr;
264 const Elf_Vernaux *getVernaux() const {
265 return isVernaux() ? (const Elf_Vernaux *)getPointer() : nullptr;
268 mutable SmallVector<VersionMapEntry, 16> VersionMap;
270 public:
271 Elf_Dyn_Range dynamic_table() const {
272 // A valid .dynamic section contains an array of entries terminated
273 // with a DT_NULL entry. However, sometimes the section content may
274 // continue past the DT_NULL entry, so to dump the section correctly,
275 // we first find the end of the entries by iterating over them.
276 Elf_Dyn_Range Table = DynamicTable.getAsArrayRef<Elf_Dyn>();
278 size_t Size = 0;
279 while (Size < Table.size())
280 if (Table[Size++].getTag() == DT_NULL)
281 break;
283 return Table.slice(0, Size);
286 Elf_Sym_Range dynamic_symbols() const {
287 return DynSymRegion.getAsArrayRef<Elf_Sym>();
290 Elf_Rel_Range dyn_rels() const;
291 Elf_Rela_Range dyn_relas() const;
292 Elf_Relr_Range dyn_relrs() const;
293 std::string getFullSymbolName(const Elf_Sym *Symbol, StringRef StrTable,
294 bool IsDynamic) const;
295 void getSectionNameIndex(const Elf_Sym *Symbol, const Elf_Sym *FirstSym,
296 StringRef &SectionName,
297 unsigned &SectionIndex) const;
298 std::string getStaticSymbolName(uint32_t Index) const;
299 std::string getDynamicString(uint64_t Value) const;
300 StringRef getSymbolVersionByIndex(StringRef StrTab,
301 uint32_t VersionSymbolIndex,
302 bool &IsDefault) const;
304 void printSymbolsHelper(bool IsDynamic) const;
305 void printDynamicEntry(raw_ostream &OS, uint64_t Type, uint64_t Value) const;
307 const Elf_Shdr *getDotSymtabSec() const { return DotSymtabSec; }
308 const Elf_Shdr *getDotCGProfileSec() const { return DotCGProfileSec; }
309 const Elf_Shdr *getDotAddrsigSec() const { return DotAddrsigSec; }
310 ArrayRef<Elf_Word> getShndxTable() const { return ShndxTable; }
311 StringRef getDynamicStringTable() const { return DynamicStringTable; }
312 const DynRegionInfo &getDynRelRegion() const { return DynRelRegion; }
313 const DynRegionInfo &getDynRelaRegion() const { return DynRelaRegion; }
314 const DynRegionInfo &getDynRelrRegion() const { return DynRelrRegion; }
315 const DynRegionInfo &getDynPLTRelRegion() const { return DynPLTRelRegion; }
316 const DynRegionInfo &getDynamicTableRegion() const { return DynamicTable; }
317 const Elf_Hash *getHashTable() const { return HashTable; }
318 const Elf_GnuHash *getGnuHashTable() const { return GnuHashTable; }
321 template <class ELFT>
322 void ELFDumper<ELFT>::printSymbolsHelper(bool IsDynamic) const {
323 StringRef StrTable, SymtabName;
324 size_t Entries = 0;
325 Elf_Sym_Range Syms(nullptr, nullptr);
326 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
327 if (IsDynamic) {
328 StrTable = DynamicStringTable;
329 Syms = dynamic_symbols();
330 SymtabName = DynSymtabName;
331 if (DynSymRegion.Addr)
332 Entries = DynSymRegion.Size / DynSymRegion.EntSize;
333 } else {
334 if (!DotSymtabSec)
335 return;
336 StrTable = unwrapOrError(ObjF->getFileName(),
337 Obj->getStringTableForSymtab(*DotSymtabSec));
338 Syms = unwrapOrError(ObjF->getFileName(), Obj->symbols(DotSymtabSec));
339 SymtabName =
340 unwrapOrError(ObjF->getFileName(), Obj->getSectionName(DotSymtabSec));
341 Entries = DotSymtabSec->getEntityCount();
343 if (Syms.begin() == Syms.end())
344 return;
345 ELFDumperStyle->printSymtabMessage(Obj, SymtabName, Entries);
346 for (const auto &Sym : Syms)
347 ELFDumperStyle->printSymbol(Obj, &Sym, Syms.begin(), StrTable, IsDynamic);
350 template <class ELFT> class MipsGOTParser;
352 template <typename ELFT> class DumpStyle {
353 public:
354 using Elf_Shdr = typename ELFT::Shdr;
355 using Elf_Sym = typename ELFT::Sym;
356 using Elf_Addr = typename ELFT::Addr;
358 DumpStyle(ELFDumper<ELFT> *Dumper) : Dumper(Dumper) {
359 FileName = this->Dumper->getElfObject()->getFileName();
361 // Dumper reports all non-critical errors as warnings.
362 // It does not print the same warning more than once.
363 WarningHandler = [this](const Twine &Msg) {
364 if (Warnings.insert(Msg.str()).second)
365 reportWarning(FileName, createError(Msg));
366 return Error::success();
370 virtual ~DumpStyle() = default;
372 virtual void printFileHeaders(const ELFFile<ELFT> *Obj) = 0;
373 virtual void printGroupSections(const ELFFile<ELFT> *Obj) = 0;
374 virtual void printRelocations(const ELFFile<ELFT> *Obj) = 0;
375 virtual void printSectionHeaders(const ELFFile<ELFT> *Obj) = 0;
376 virtual void printSymbols(const ELFFile<ELFT> *Obj, bool PrintSymbols,
377 bool PrintDynamicSymbols) = 0;
378 virtual void printHashSymbols(const ELFFile<ELFT> *Obj) {}
379 virtual void printDynamic(const ELFFile<ELFT> *Obj) {}
380 virtual void printDynamicRelocations(const ELFFile<ELFT> *Obj) = 0;
381 virtual void printSymtabMessage(const ELFFile<ELFT> *Obj, StringRef Name,
382 size_t Offset) {}
383 virtual void printSymbol(const ELFFile<ELFT> *Obj, const Elf_Sym *Symbol,
384 const Elf_Sym *FirstSym, StringRef StrTable,
385 bool IsDynamic) = 0;
386 virtual void printProgramHeaders(const ELFFile<ELFT> *Obj,
387 bool PrintProgramHeaders,
388 cl::boolOrDefault PrintSectionMapping) = 0;
389 virtual void printVersionSymbolSection(const ELFFile<ELFT> *Obj,
390 const Elf_Shdr *Sec) = 0;
391 virtual void printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
392 const Elf_Shdr *Sec) = 0;
393 virtual void printVersionDependencySection(const ELFFile<ELFT> *Obj,
394 const Elf_Shdr *Sec) = 0;
395 virtual void printHashHistogram(const ELFFile<ELFT> *Obj) = 0;
396 virtual void printCGProfile(const ELFFile<ELFT> *Obj) = 0;
397 virtual void printAddrsig(const ELFFile<ELFT> *Obj) = 0;
398 virtual void printNotes(const ELFFile<ELFT> *Obj) = 0;
399 virtual void printELFLinkerOptions(const ELFFile<ELFT> *Obj) = 0;
400 virtual void printStackSizes(const ELFObjectFile<ELFT> *Obj) = 0;
401 void printNonRelocatableStackSizes(const ELFObjectFile<ELFT> *Obj,
402 std::function<void()> PrintHeader);
403 void printRelocatableStackSizes(const ELFObjectFile<ELFT> *Obj,
404 std::function<void()> PrintHeader);
405 void printFunctionStackSize(const ELFObjectFile<ELFT> *Obj, uint64_t SymValue,
406 SectionRef FunctionSec,
407 const StringRef SectionName, DataExtractor Data,
408 uint64_t *Offset);
409 void printStackSize(const ELFObjectFile<ELFT> *Obj, RelocationRef Rel,
410 SectionRef FunctionSec,
411 const StringRef &StackSizeSectionName,
412 const RelocationResolver &Resolver, DataExtractor Data);
413 virtual void printStackSizeEntry(uint64_t Size, StringRef FuncName) = 0;
414 virtual void printMipsGOT(const MipsGOTParser<ELFT> &Parser) = 0;
415 virtual void printMipsPLT(const MipsGOTParser<ELFT> &Parser) = 0;
416 const ELFDumper<ELFT> *dumper() const { return Dumper; }
418 protected:
419 std::function<Error(const Twine &Msg)> WarningHandler;
420 StringRef FileName;
422 private:
423 std::unordered_set<std::string> Warnings;
424 const ELFDumper<ELFT> *Dumper;
427 template <typename ELFT> class GNUStyle : public DumpStyle<ELFT> {
428 formatted_raw_ostream &OS;
430 public:
431 TYPEDEF_ELF_TYPES(ELFT)
433 GNUStyle(ScopedPrinter &W, ELFDumper<ELFT> *Dumper)
434 : DumpStyle<ELFT>(Dumper),
435 OS(static_cast<formatted_raw_ostream&>(W.getOStream())) {
436 assert (&W.getOStream() == &llvm::fouts());
439 void printFileHeaders(const ELFO *Obj) override;
440 void printGroupSections(const ELFFile<ELFT> *Obj) override;
441 void printRelocations(const ELFO *Obj) override;
442 void printSectionHeaders(const ELFO *Obj) override;
443 void printSymbols(const ELFO *Obj, bool PrintSymbols,
444 bool PrintDynamicSymbols) override;
445 void printHashSymbols(const ELFO *Obj) override;
446 void printDynamic(const ELFFile<ELFT> *Obj) override;
447 void printDynamicRelocations(const ELFO *Obj) override;
448 void printSymtabMessage(const ELFO *Obj, StringRef Name,
449 size_t Offset) override;
450 void printProgramHeaders(const ELFO *Obj, bool PrintProgramHeaders,
451 cl::boolOrDefault PrintSectionMapping) override;
452 void printVersionSymbolSection(const ELFFile<ELFT> *Obj,
453 const Elf_Shdr *Sec) override;
454 void printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
455 const Elf_Shdr *Sec) override;
456 void printVersionDependencySection(const ELFFile<ELFT> *Obj,
457 const Elf_Shdr *Sec) override;
458 void printHashHistogram(const ELFFile<ELFT> *Obj) override;
459 void printCGProfile(const ELFFile<ELFT> *Obj) override;
460 void printAddrsig(const ELFFile<ELFT> *Obj) override;
461 void printNotes(const ELFFile<ELFT> *Obj) override;
462 void printELFLinkerOptions(const ELFFile<ELFT> *Obj) override;
463 void printStackSizes(const ELFObjectFile<ELFT> *Obj) override;
464 void printStackSizeEntry(uint64_t Size, StringRef FuncName) override;
465 void printMipsGOT(const MipsGOTParser<ELFT> &Parser) override;
466 void printMipsPLT(const MipsGOTParser<ELFT> &Parser) override;
468 private:
469 struct Field {
470 std::string Str;
471 unsigned Column;
473 Field(StringRef S, unsigned Col) : Str(S), Column(Col) {}
474 Field(unsigned Col) : Column(Col) {}
477 template <typename T, typename TEnum>
478 std::string printEnum(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues) {
479 for (const auto &EnumItem : EnumValues)
480 if (EnumItem.Value == Value)
481 return EnumItem.AltName;
482 return to_hexString(Value, false);
485 template <typename T, typename TEnum>
486 std::string printFlags(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues,
487 TEnum EnumMask1 = {}, TEnum EnumMask2 = {},
488 TEnum EnumMask3 = {}) {
489 std::string Str;
490 for (const auto &Flag : EnumValues) {
491 if (Flag.Value == 0)
492 continue;
494 TEnum EnumMask{};
495 if (Flag.Value & EnumMask1)
496 EnumMask = EnumMask1;
497 else if (Flag.Value & EnumMask2)
498 EnumMask = EnumMask2;
499 else if (Flag.Value & EnumMask3)
500 EnumMask = EnumMask3;
501 bool IsEnum = (Flag.Value & EnumMask) != 0;
502 if ((!IsEnum && (Value & Flag.Value) == Flag.Value) ||
503 (IsEnum && (Value & EnumMask) == Flag.Value)) {
504 if (!Str.empty())
505 Str += ", ";
506 Str += Flag.AltName;
509 return Str;
512 formatted_raw_ostream &printField(struct Field F) {
513 if (F.Column != 0)
514 OS.PadToColumn(F.Column);
515 OS << F.Str;
516 OS.flush();
517 return OS;
519 void printHashedSymbol(const ELFO *Obj, const Elf_Sym *FirstSym, uint32_t Sym,
520 StringRef StrTable, uint32_t Bucket);
521 void printRelocHeader(unsigned SType);
522 void printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab,
523 const Elf_Rela &R, bool IsRela);
524 void printRelocation(const ELFO *Obj, const Elf_Sym *Sym,
525 StringRef SymbolName, const Elf_Rela &R, bool IsRela);
526 void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First,
527 StringRef StrTable, bool IsDynamic) override;
528 std::string getSymbolSectionNdx(const ELFO *Obj, const Elf_Sym *Symbol,
529 const Elf_Sym *FirstSym);
530 void printDynamicRelocation(const ELFO *Obj, Elf_Rela R, bool IsRela);
531 bool checkTLSSections(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
532 bool checkoffsets(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
533 bool checkVMA(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
534 bool checkPTDynamic(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
535 void printProgramHeaders(const ELFO *Obj);
536 void printSectionMapping(const ELFO *Obj);
539 template <typename ELFT> class LLVMStyle : public DumpStyle<ELFT> {
540 public:
541 TYPEDEF_ELF_TYPES(ELFT)
543 LLVMStyle(ScopedPrinter &W, ELFDumper<ELFT> *Dumper)
544 : DumpStyle<ELFT>(Dumper), W(W) {}
546 void printFileHeaders(const ELFO *Obj) override;
547 void printGroupSections(const ELFFile<ELFT> *Obj) override;
548 void printRelocations(const ELFO *Obj) override;
549 void printRelocations(const Elf_Shdr *Sec, const ELFO *Obj);
550 void printSectionHeaders(const ELFO *Obj) override;
551 void printSymbols(const ELFO *Obj, bool PrintSymbols,
552 bool PrintDynamicSymbols) override;
553 void printDynamic(const ELFFile<ELFT> *Obj) override;
554 void printDynamicRelocations(const ELFO *Obj) override;
555 void printProgramHeaders(const ELFO *Obj, bool PrintProgramHeaders,
556 cl::boolOrDefault PrintSectionMapping) override;
557 void printVersionSymbolSection(const ELFFile<ELFT> *Obj,
558 const Elf_Shdr *Sec) override;
559 void printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
560 const Elf_Shdr *Sec) override;
561 void printVersionDependencySection(const ELFFile<ELFT> *Obj,
562 const Elf_Shdr *Sec) override;
563 void printHashHistogram(const ELFFile<ELFT> *Obj) override;
564 void printCGProfile(const ELFFile<ELFT> *Obj) override;
565 void printAddrsig(const ELFFile<ELFT> *Obj) override;
566 void printNotes(const ELFFile<ELFT> *Obj) override;
567 void printELFLinkerOptions(const ELFFile<ELFT> *Obj) override;
568 void printStackSizes(const ELFObjectFile<ELFT> *Obj) override;
569 void printStackSizeEntry(uint64_t Size, StringRef FuncName) override;
570 void printMipsGOT(const MipsGOTParser<ELFT> &Parser) override;
571 void printMipsPLT(const MipsGOTParser<ELFT> &Parser) override;
573 private:
574 void printRelocation(const ELFO *Obj, Elf_Rela Rel, const Elf_Shdr *SymTab);
575 void printDynamicRelocation(const ELFO *Obj, Elf_Rela Rel);
576 void printSymbols(const ELFO *Obj);
577 void printDynamicSymbols(const ELFO *Obj);
578 void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First,
579 StringRef StrTable, bool IsDynamic) override;
580 void printProgramHeaders(const ELFO *Obj);
581 void printSectionMapping(const ELFO *Obj) {}
583 ScopedPrinter &W;
586 } // end anonymous namespace
588 namespace llvm {
590 template <class ELFT>
591 static std::error_code createELFDumper(const ELFObjectFile<ELFT> *Obj,
592 ScopedPrinter &Writer,
593 std::unique_ptr<ObjDumper> &Result) {
594 Result.reset(new ELFDumper<ELFT>(Obj, Writer));
595 return readobj_error::success;
598 std::error_code createELFDumper(const object::ObjectFile *Obj,
599 ScopedPrinter &Writer,
600 std::unique_ptr<ObjDumper> &Result) {
601 // Little-endian 32-bit
602 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
603 return createELFDumper(ELFObj, Writer, Result);
605 // Big-endian 32-bit
606 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
607 return createELFDumper(ELFObj, Writer, Result);
609 // Little-endian 64-bit
610 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
611 return createELFDumper(ELFObj, Writer, Result);
613 // Big-endian 64-bit
614 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
615 return createELFDumper(ELFObj, Writer, Result);
617 return readobj_error::unsupported_obj_file_format;
620 } // end namespace llvm
622 // Iterate through the versions needed section, and place each Elf_Vernaux
623 // in the VersionMap according to its index.
624 template <class ELFT>
625 void ELFDumper<ELFT>::LoadVersionNeeds(const Elf_Shdr *Sec) const {
626 unsigned VerneedSize = Sec->sh_size; // Size of section in bytes
627 unsigned VerneedEntries = Sec->sh_info; // Number of Verneed entries
628 const uint8_t *VerneedStart = reinterpret_cast<const uint8_t *>(
629 ObjF->getELFFile()->base() + Sec->sh_offset);
630 const uint8_t *VerneedEnd = VerneedStart + VerneedSize;
631 // The first Verneed entry is at the start of the section.
632 const uint8_t *VerneedBuf = VerneedStart;
633 for (unsigned VerneedIndex = 0; VerneedIndex < VerneedEntries;
634 ++VerneedIndex) {
635 if (VerneedBuf + sizeof(Elf_Verneed) > VerneedEnd)
636 report_fatal_error("Section ended unexpectedly while scanning "
637 "version needed records.");
638 const Elf_Verneed *Verneed =
639 reinterpret_cast<const Elf_Verneed *>(VerneedBuf);
640 if (Verneed->vn_version != ELF::VER_NEED_CURRENT)
641 report_fatal_error("Unexpected verneed version");
642 // Iterate through the Vernaux entries
643 const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux;
644 for (unsigned VernauxIndex = 0; VernauxIndex < Verneed->vn_cnt;
645 ++VernauxIndex) {
646 if (VernauxBuf + sizeof(Elf_Vernaux) > VerneedEnd)
647 report_fatal_error("Section ended unexpected while scanning auxiliary "
648 "version needed records.");
649 const Elf_Vernaux *Vernaux =
650 reinterpret_cast<const Elf_Vernaux *>(VernauxBuf);
651 size_t Index = Vernaux->vna_other & ELF::VERSYM_VERSION;
652 if (Index >= VersionMap.size())
653 VersionMap.resize(Index + 1);
654 VersionMap[Index] = VersionMapEntry(Vernaux);
655 VernauxBuf += Vernaux->vna_next;
657 VerneedBuf += Verneed->vn_next;
661 // Iterate through the version definitions, and place each Elf_Verdef
662 // in the VersionMap according to its index.
663 template <class ELFT>
664 void ELFDumper<ELFT>::LoadVersionDefs(const Elf_Shdr *Sec) const {
665 unsigned VerdefSize = Sec->sh_size; // Size of section in bytes
666 unsigned VerdefEntries = Sec->sh_info; // Number of Verdef entries
667 const uint8_t *VerdefStart = reinterpret_cast<const uint8_t *>(
668 ObjF->getELFFile()->base() + Sec->sh_offset);
669 const uint8_t *VerdefEnd = VerdefStart + VerdefSize;
670 // The first Verdef entry is at the start of the section.
671 const uint8_t *VerdefBuf = VerdefStart;
672 for (unsigned VerdefIndex = 0; VerdefIndex < VerdefEntries; ++VerdefIndex) {
673 if (VerdefBuf + sizeof(Elf_Verdef) > VerdefEnd)
674 report_fatal_error("Section ended unexpectedly while scanning "
675 "version definitions.");
676 const Elf_Verdef *Verdef = reinterpret_cast<const Elf_Verdef *>(VerdefBuf);
677 if (Verdef->vd_version != ELF::VER_DEF_CURRENT)
678 report_fatal_error("Unexpected verdef version");
679 size_t Index = Verdef->vd_ndx & ELF::VERSYM_VERSION;
680 if (Index >= VersionMap.size())
681 VersionMap.resize(Index + 1);
682 VersionMap[Index] = VersionMapEntry(Verdef);
683 VerdefBuf += Verdef->vd_next;
687 template <class ELFT> void ELFDumper<ELFT>::LoadVersionMap() const {
688 // If there is no dynamic symtab or version table, there is nothing to do.
689 if (!DynSymRegion.Addr || !SymbolVersionSection)
690 return;
692 // Has the VersionMap already been loaded?
693 if (!VersionMap.empty())
694 return;
696 // The first two version indexes are reserved.
697 // Index 0 is LOCAL, index 1 is GLOBAL.
698 VersionMap.push_back(VersionMapEntry());
699 VersionMap.push_back(VersionMapEntry());
701 if (SymbolVersionDefSection)
702 LoadVersionDefs(SymbolVersionDefSection);
704 if (SymbolVersionNeedSection)
705 LoadVersionNeeds(SymbolVersionNeedSection);
708 template <typename ELFT>
709 StringRef ELFDumper<ELFT>::getSymbolVersion(StringRef StrTab,
710 const Elf_Sym *Sym,
711 bool &IsDefault) const {
712 // This is a dynamic symbol. Look in the GNU symbol version table.
713 if (!SymbolVersionSection) {
714 // No version table.
715 IsDefault = false;
716 return "";
719 // Determine the position in the symbol table of this entry.
720 size_t EntryIndex = (reinterpret_cast<uintptr_t>(Sym) -
721 reinterpret_cast<uintptr_t>(DynSymRegion.Addr)) /
722 sizeof(Elf_Sym);
724 // Get the corresponding version index entry.
725 const Elf_Versym *Versym = unwrapOrError(
726 ObjF->getFileName(), ObjF->getELFFile()->template getEntry<Elf_Versym>(
727 SymbolVersionSection, EntryIndex));
728 return this->getSymbolVersionByIndex(StrTab, Versym->vs_index, IsDefault);
731 static std::string maybeDemangle(StringRef Name) {
732 return opts::Demangle ? demangle(Name) : Name.str();
735 template <typename ELFT>
736 std::string ELFDumper<ELFT>::getStaticSymbolName(uint32_t Index) const {
737 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
738 StringRef StrTable = unwrapOrError(
739 ObjF->getFileName(), Obj->getStringTableForSymtab(*DotSymtabSec));
740 Elf_Sym_Range Syms =
741 unwrapOrError(ObjF->getFileName(), Obj->symbols(DotSymtabSec));
742 if (Index >= Syms.size())
743 reportError("Invalid symbol index");
744 const Elf_Sym *Sym = &Syms[Index];
745 return maybeDemangle(
746 unwrapOrError(ObjF->getFileName(), Sym->getName(StrTable)));
749 template <typename ELFT>
750 StringRef ELFDumper<ELFT>::getSymbolVersionByIndex(StringRef StrTab,
751 uint32_t SymbolVersionIndex,
752 bool &IsDefault) const {
753 size_t VersionIndex = SymbolVersionIndex & VERSYM_VERSION;
755 // Special markers for unversioned symbols.
756 if (VersionIndex == VER_NDX_LOCAL || VersionIndex == VER_NDX_GLOBAL) {
757 IsDefault = false;
758 return "";
761 // Lookup this symbol in the version table.
762 LoadVersionMap();
763 if (VersionIndex >= VersionMap.size() || VersionMap[VersionIndex].isNull())
764 reportError("Invalid version entry");
765 const VersionMapEntry &Entry = VersionMap[VersionIndex];
767 // Get the version name string.
768 size_t NameOffset;
769 if (Entry.isVerdef()) {
770 // The first Verdaux entry holds the name.
771 NameOffset = Entry.getVerdef()->getAux()->vda_name;
772 IsDefault = !(SymbolVersionIndex & VERSYM_HIDDEN);
773 } else {
774 NameOffset = Entry.getVernaux()->vna_name;
775 IsDefault = false;
777 if (NameOffset >= StrTab.size())
778 reportError("Invalid string offset");
779 return StrTab.data() + NameOffset;
782 template <typename ELFT>
783 std::string ELFDumper<ELFT>::getFullSymbolName(const Elf_Sym *Symbol,
784 StringRef StrTable,
785 bool IsDynamic) const {
786 std::string SymbolName = maybeDemangle(
787 unwrapOrError(ObjF->getFileName(), Symbol->getName(StrTable)));
789 if (SymbolName.empty() && Symbol->getType() == ELF::STT_SECTION) {
790 unsigned SectionIndex;
791 StringRef SectionName;
792 Elf_Sym_Range Syms = unwrapOrError(
793 ObjF->getFileName(), ObjF->getELFFile()->symbols(DotSymtabSec));
794 getSectionNameIndex(Symbol, Syms.begin(), SectionName, SectionIndex);
795 return SectionName;
798 if (!IsDynamic)
799 return SymbolName;
801 bool IsDefault;
802 StringRef Version = getSymbolVersion(StrTable, &*Symbol, IsDefault);
803 if (!Version.empty()) {
804 SymbolName += (IsDefault ? "@@" : "@");
805 SymbolName += Version;
807 return SymbolName;
810 template <typename ELFT>
811 void ELFDumper<ELFT>::getSectionNameIndex(const Elf_Sym *Symbol,
812 const Elf_Sym *FirstSym,
813 StringRef &SectionName,
814 unsigned &SectionIndex) const {
815 SectionIndex = Symbol->st_shndx;
816 if (Symbol->isUndefined())
817 SectionName = "Undefined";
818 else if (Symbol->isProcessorSpecific())
819 SectionName = "Processor Specific";
820 else if (Symbol->isOSSpecific())
821 SectionName = "Operating System Specific";
822 else if (Symbol->isAbsolute())
823 SectionName = "Absolute";
824 else if (Symbol->isCommon())
825 SectionName = "Common";
826 else if (Symbol->isReserved() && SectionIndex != SHN_XINDEX)
827 SectionName = "Reserved";
828 else {
829 if (SectionIndex == SHN_XINDEX)
830 SectionIndex = unwrapOrError(ObjF->getFileName(),
831 object::getExtendedSymbolTableIndex<ELFT>(
832 Symbol, FirstSym, ShndxTable));
833 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
834 const typename ELFT::Shdr *Sec =
835 unwrapOrError(ObjF->getFileName(), Obj->getSection(SectionIndex));
836 SectionName = unwrapOrError(ObjF->getFileName(), Obj->getSectionName(Sec));
840 template <class ELFO>
841 static const typename ELFO::Elf_Shdr *
842 findNotEmptySectionByAddress(const ELFO *Obj, StringRef FileName,
843 uint64_t Addr) {
844 for (const auto &Shdr : unwrapOrError(FileName, Obj->sections()))
845 if (Shdr.sh_addr == Addr && Shdr.sh_size > 0)
846 return &Shdr;
847 return nullptr;
850 template <class ELFO>
851 static const typename ELFO::Elf_Shdr *
852 findSectionByName(const ELFO &Obj, StringRef FileName, StringRef Name) {
853 for (const auto &Shdr : unwrapOrError(FileName, Obj.sections()))
854 if (Name == unwrapOrError(FileName, Obj.getSectionName(&Shdr)))
855 return &Shdr;
856 return nullptr;
859 static const EnumEntry<unsigned> ElfClass[] = {
860 {"None", "none", ELF::ELFCLASSNONE},
861 {"32-bit", "ELF32", ELF::ELFCLASS32},
862 {"64-bit", "ELF64", ELF::ELFCLASS64},
865 static const EnumEntry<unsigned> ElfDataEncoding[] = {
866 {"None", "none", ELF::ELFDATANONE},
867 {"LittleEndian", "2's complement, little endian", ELF::ELFDATA2LSB},
868 {"BigEndian", "2's complement, big endian", ELF::ELFDATA2MSB},
871 static const EnumEntry<unsigned> ElfObjectFileType[] = {
872 {"None", "NONE (none)", ELF::ET_NONE},
873 {"Relocatable", "REL (Relocatable file)", ELF::ET_REL},
874 {"Executable", "EXEC (Executable file)", ELF::ET_EXEC},
875 {"SharedObject", "DYN (Shared object file)", ELF::ET_DYN},
876 {"Core", "CORE (Core file)", ELF::ET_CORE},
879 static const EnumEntry<unsigned> ElfOSABI[] = {
880 {"SystemV", "UNIX - System V", ELF::ELFOSABI_NONE},
881 {"HPUX", "UNIX - HP-UX", ELF::ELFOSABI_HPUX},
882 {"NetBSD", "UNIX - NetBSD", ELF::ELFOSABI_NETBSD},
883 {"GNU/Linux", "UNIX - GNU", ELF::ELFOSABI_LINUX},
884 {"GNU/Hurd", "GNU/Hurd", ELF::ELFOSABI_HURD},
885 {"Solaris", "UNIX - Solaris", ELF::ELFOSABI_SOLARIS},
886 {"AIX", "UNIX - AIX", ELF::ELFOSABI_AIX},
887 {"IRIX", "UNIX - IRIX", ELF::ELFOSABI_IRIX},
888 {"FreeBSD", "UNIX - FreeBSD", ELF::ELFOSABI_FREEBSD},
889 {"TRU64", "UNIX - TRU64", ELF::ELFOSABI_TRU64},
890 {"Modesto", "Novell - Modesto", ELF::ELFOSABI_MODESTO},
891 {"OpenBSD", "UNIX - OpenBSD", ELF::ELFOSABI_OPENBSD},
892 {"OpenVMS", "VMS - OpenVMS", ELF::ELFOSABI_OPENVMS},
893 {"NSK", "HP - Non-Stop Kernel", ELF::ELFOSABI_NSK},
894 {"AROS", "AROS", ELF::ELFOSABI_AROS},
895 {"FenixOS", "FenixOS", ELF::ELFOSABI_FENIXOS},
896 {"CloudABI", "CloudABI", ELF::ELFOSABI_CLOUDABI},
897 {"Standalone", "Standalone App", ELF::ELFOSABI_STANDALONE}
900 static const EnumEntry<unsigned> SymVersionFlags[] = {
901 {"Base", "BASE", VER_FLG_BASE},
902 {"Weak", "WEAK", VER_FLG_WEAK},
903 {"Info", "INFO", VER_FLG_INFO}};
905 static const EnumEntry<unsigned> AMDGPUElfOSABI[] = {
906 {"AMDGPU_HSA", "AMDGPU - HSA", ELF::ELFOSABI_AMDGPU_HSA},
907 {"AMDGPU_PAL", "AMDGPU - PAL", ELF::ELFOSABI_AMDGPU_PAL},
908 {"AMDGPU_MESA3D", "AMDGPU - MESA3D", ELF::ELFOSABI_AMDGPU_MESA3D}
911 static const EnumEntry<unsigned> ARMElfOSABI[] = {
912 {"ARM", "ARM", ELF::ELFOSABI_ARM}
915 static const EnumEntry<unsigned> C6000ElfOSABI[] = {
916 {"C6000_ELFABI", "Bare-metal C6000", ELF::ELFOSABI_C6000_ELFABI},
917 {"C6000_LINUX", "Linux C6000", ELF::ELFOSABI_C6000_LINUX}
920 static const EnumEntry<unsigned> ElfMachineType[] = {
921 ENUM_ENT(EM_NONE, "None"),
922 ENUM_ENT(EM_M32, "WE32100"),
923 ENUM_ENT(EM_SPARC, "Sparc"),
924 ENUM_ENT(EM_386, "Intel 80386"),
925 ENUM_ENT(EM_68K, "MC68000"),
926 ENUM_ENT(EM_88K, "MC88000"),
927 ENUM_ENT(EM_IAMCU, "EM_IAMCU"),
928 ENUM_ENT(EM_860, "Intel 80860"),
929 ENUM_ENT(EM_MIPS, "MIPS R3000"),
930 ENUM_ENT(EM_S370, "IBM System/370"),
931 ENUM_ENT(EM_MIPS_RS3_LE, "MIPS R3000 little-endian"),
932 ENUM_ENT(EM_PARISC, "HPPA"),
933 ENUM_ENT(EM_VPP500, "Fujitsu VPP500"),
934 ENUM_ENT(EM_SPARC32PLUS, "Sparc v8+"),
935 ENUM_ENT(EM_960, "Intel 80960"),
936 ENUM_ENT(EM_PPC, "PowerPC"),
937 ENUM_ENT(EM_PPC64, "PowerPC64"),
938 ENUM_ENT(EM_S390, "IBM S/390"),
939 ENUM_ENT(EM_SPU, "SPU"),
940 ENUM_ENT(EM_V800, "NEC V800 series"),
941 ENUM_ENT(EM_FR20, "Fujistsu FR20"),
942 ENUM_ENT(EM_RH32, "TRW RH-32"),
943 ENUM_ENT(EM_RCE, "Motorola RCE"),
944 ENUM_ENT(EM_ARM, "ARM"),
945 ENUM_ENT(EM_ALPHA, "EM_ALPHA"),
946 ENUM_ENT(EM_SH, "Hitachi SH"),
947 ENUM_ENT(EM_SPARCV9, "Sparc v9"),
948 ENUM_ENT(EM_TRICORE, "Siemens Tricore"),
949 ENUM_ENT(EM_ARC, "ARC"),
950 ENUM_ENT(EM_H8_300, "Hitachi H8/300"),
951 ENUM_ENT(EM_H8_300H, "Hitachi H8/300H"),
952 ENUM_ENT(EM_H8S, "Hitachi H8S"),
953 ENUM_ENT(EM_H8_500, "Hitachi H8/500"),
954 ENUM_ENT(EM_IA_64, "Intel IA-64"),
955 ENUM_ENT(EM_MIPS_X, "Stanford MIPS-X"),
956 ENUM_ENT(EM_COLDFIRE, "Motorola Coldfire"),
957 ENUM_ENT(EM_68HC12, "Motorola MC68HC12 Microcontroller"),
958 ENUM_ENT(EM_MMA, "Fujitsu Multimedia Accelerator"),
959 ENUM_ENT(EM_PCP, "Siemens PCP"),
960 ENUM_ENT(EM_NCPU, "Sony nCPU embedded RISC processor"),
961 ENUM_ENT(EM_NDR1, "Denso NDR1 microprocesspr"),
962 ENUM_ENT(EM_STARCORE, "Motorola Star*Core processor"),
963 ENUM_ENT(EM_ME16, "Toyota ME16 processor"),
964 ENUM_ENT(EM_ST100, "STMicroelectronics ST100 processor"),
965 ENUM_ENT(EM_TINYJ, "Advanced Logic Corp. TinyJ embedded processor"),
966 ENUM_ENT(EM_X86_64, "Advanced Micro Devices X86-64"),
967 ENUM_ENT(EM_PDSP, "Sony DSP processor"),
968 ENUM_ENT(EM_PDP10, "Digital Equipment Corp. PDP-10"),
969 ENUM_ENT(EM_PDP11, "Digital Equipment Corp. PDP-11"),
970 ENUM_ENT(EM_FX66, "Siemens FX66 microcontroller"),
971 ENUM_ENT(EM_ST9PLUS, "STMicroelectronics ST9+ 8/16 bit microcontroller"),
972 ENUM_ENT(EM_ST7, "STMicroelectronics ST7 8-bit microcontroller"),
973 ENUM_ENT(EM_68HC16, "Motorola MC68HC16 Microcontroller"),
974 ENUM_ENT(EM_68HC11, "Motorola MC68HC11 Microcontroller"),
975 ENUM_ENT(EM_68HC08, "Motorola MC68HC08 Microcontroller"),
976 ENUM_ENT(EM_68HC05, "Motorola MC68HC05 Microcontroller"),
977 ENUM_ENT(EM_SVX, "Silicon Graphics SVx"),
978 ENUM_ENT(EM_ST19, "STMicroelectronics ST19 8-bit microcontroller"),
979 ENUM_ENT(EM_VAX, "Digital VAX"),
980 ENUM_ENT(EM_CRIS, "Axis Communications 32-bit embedded processor"),
981 ENUM_ENT(EM_JAVELIN, "Infineon Technologies 32-bit embedded cpu"),
982 ENUM_ENT(EM_FIREPATH, "Element 14 64-bit DSP processor"),
983 ENUM_ENT(EM_ZSP, "LSI Logic's 16-bit DSP processor"),
984 ENUM_ENT(EM_MMIX, "Donald Knuth's educational 64-bit processor"),
985 ENUM_ENT(EM_HUANY, "Harvard Universitys's machine-independent object format"),
986 ENUM_ENT(EM_PRISM, "Vitesse Prism"),
987 ENUM_ENT(EM_AVR, "Atmel AVR 8-bit microcontroller"),
988 ENUM_ENT(EM_FR30, "Fujitsu FR30"),
989 ENUM_ENT(EM_D10V, "Mitsubishi D10V"),
990 ENUM_ENT(EM_D30V, "Mitsubishi D30V"),
991 ENUM_ENT(EM_V850, "NEC v850"),
992 ENUM_ENT(EM_M32R, "Renesas M32R (formerly Mitsubishi M32r)"),
993 ENUM_ENT(EM_MN10300, "Matsushita MN10300"),
994 ENUM_ENT(EM_MN10200, "Matsushita MN10200"),
995 ENUM_ENT(EM_PJ, "picoJava"),
996 ENUM_ENT(EM_OPENRISC, "OpenRISC 32-bit embedded processor"),
997 ENUM_ENT(EM_ARC_COMPACT, "EM_ARC_COMPACT"),
998 ENUM_ENT(EM_XTENSA, "Tensilica Xtensa Processor"),
999 ENUM_ENT(EM_VIDEOCORE, "Alphamosaic VideoCore processor"),
1000 ENUM_ENT(EM_TMM_GPP, "Thompson Multimedia General Purpose Processor"),
1001 ENUM_ENT(EM_NS32K, "National Semiconductor 32000 series"),
1002 ENUM_ENT(EM_TPC, "Tenor Network TPC processor"),
1003 ENUM_ENT(EM_SNP1K, "EM_SNP1K"),
1004 ENUM_ENT(EM_ST200, "STMicroelectronics ST200 microcontroller"),
1005 ENUM_ENT(EM_IP2K, "Ubicom IP2xxx 8-bit microcontrollers"),
1006 ENUM_ENT(EM_MAX, "MAX Processor"),
1007 ENUM_ENT(EM_CR, "National Semiconductor CompactRISC"),
1008 ENUM_ENT(EM_F2MC16, "Fujitsu F2MC16"),
1009 ENUM_ENT(EM_MSP430, "Texas Instruments msp430 microcontroller"),
1010 ENUM_ENT(EM_BLACKFIN, "Analog Devices Blackfin"),
1011 ENUM_ENT(EM_SE_C33, "S1C33 Family of Seiko Epson processors"),
1012 ENUM_ENT(EM_SEP, "Sharp embedded microprocessor"),
1013 ENUM_ENT(EM_ARCA, "Arca RISC microprocessor"),
1014 ENUM_ENT(EM_UNICORE, "Unicore"),
1015 ENUM_ENT(EM_EXCESS, "eXcess 16/32/64-bit configurable embedded CPU"),
1016 ENUM_ENT(EM_DXP, "Icera Semiconductor Inc. Deep Execution Processor"),
1017 ENUM_ENT(EM_ALTERA_NIOS2, "Altera Nios"),
1018 ENUM_ENT(EM_CRX, "National Semiconductor CRX microprocessor"),
1019 ENUM_ENT(EM_XGATE, "Motorola XGATE embedded processor"),
1020 ENUM_ENT(EM_C166, "Infineon Technologies xc16x"),
1021 ENUM_ENT(EM_M16C, "Renesas M16C"),
1022 ENUM_ENT(EM_DSPIC30F, "Microchip Technology dsPIC30F Digital Signal Controller"),
1023 ENUM_ENT(EM_CE, "Freescale Communication Engine RISC core"),
1024 ENUM_ENT(EM_M32C, "Renesas M32C"),
1025 ENUM_ENT(EM_TSK3000, "Altium TSK3000 core"),
1026 ENUM_ENT(EM_RS08, "Freescale RS08 embedded processor"),
1027 ENUM_ENT(EM_SHARC, "EM_SHARC"),
1028 ENUM_ENT(EM_ECOG2, "Cyan Technology eCOG2 microprocessor"),
1029 ENUM_ENT(EM_SCORE7, "SUNPLUS S+Core"),
1030 ENUM_ENT(EM_DSP24, "New Japan Radio (NJR) 24-bit DSP Processor"),
1031 ENUM_ENT(EM_VIDEOCORE3, "Broadcom VideoCore III processor"),
1032 ENUM_ENT(EM_LATTICEMICO32, "Lattice Mico32"),
1033 ENUM_ENT(EM_SE_C17, "Seiko Epson C17 family"),
1034 ENUM_ENT(EM_TI_C6000, "Texas Instruments TMS320C6000 DSP family"),
1035 ENUM_ENT(EM_TI_C2000, "Texas Instruments TMS320C2000 DSP family"),
1036 ENUM_ENT(EM_TI_C5500, "Texas Instruments TMS320C55x DSP family"),
1037 ENUM_ENT(EM_MMDSP_PLUS, "STMicroelectronics 64bit VLIW Data Signal Processor"),
1038 ENUM_ENT(EM_CYPRESS_M8C, "Cypress M8C microprocessor"),
1039 ENUM_ENT(EM_R32C, "Renesas R32C series microprocessors"),
1040 ENUM_ENT(EM_TRIMEDIA, "NXP Semiconductors TriMedia architecture family"),
1041 ENUM_ENT(EM_HEXAGON, "Qualcomm Hexagon"),
1042 ENUM_ENT(EM_8051, "Intel 8051 and variants"),
1043 ENUM_ENT(EM_STXP7X, "STMicroelectronics STxP7x family"),
1044 ENUM_ENT(EM_NDS32, "Andes Technology compact code size embedded RISC processor family"),
1045 ENUM_ENT(EM_ECOG1, "Cyan Technology eCOG1 microprocessor"),
1046 ENUM_ENT(EM_ECOG1X, "Cyan Technology eCOG1X family"),
1047 ENUM_ENT(EM_MAXQ30, "Dallas Semiconductor MAXQ30 Core microcontrollers"),
1048 ENUM_ENT(EM_XIMO16, "New Japan Radio (NJR) 16-bit DSP Processor"),
1049 ENUM_ENT(EM_MANIK, "M2000 Reconfigurable RISC Microprocessor"),
1050 ENUM_ENT(EM_CRAYNV2, "Cray Inc. NV2 vector architecture"),
1051 ENUM_ENT(EM_RX, "Renesas RX"),
1052 ENUM_ENT(EM_METAG, "Imagination Technologies Meta processor architecture"),
1053 ENUM_ENT(EM_MCST_ELBRUS, "MCST Elbrus general purpose hardware architecture"),
1054 ENUM_ENT(EM_ECOG16, "Cyan Technology eCOG16 family"),
1055 ENUM_ENT(EM_CR16, "Xilinx MicroBlaze"),
1056 ENUM_ENT(EM_ETPU, "Freescale Extended Time Processing Unit"),
1057 ENUM_ENT(EM_SLE9X, "Infineon Technologies SLE9X core"),
1058 ENUM_ENT(EM_L10M, "EM_L10M"),
1059 ENUM_ENT(EM_K10M, "EM_K10M"),
1060 ENUM_ENT(EM_AARCH64, "AArch64"),
1061 ENUM_ENT(EM_AVR32, "Atmel Corporation 32-bit microprocessor family"),
1062 ENUM_ENT(EM_STM8, "STMicroeletronics STM8 8-bit microcontroller"),
1063 ENUM_ENT(EM_TILE64, "Tilera TILE64 multicore architecture family"),
1064 ENUM_ENT(EM_TILEPRO, "Tilera TILEPro multicore architecture family"),
1065 ENUM_ENT(EM_CUDA, "NVIDIA CUDA architecture"),
1066 ENUM_ENT(EM_TILEGX, "Tilera TILE-Gx multicore architecture family"),
1067 ENUM_ENT(EM_CLOUDSHIELD, "EM_CLOUDSHIELD"),
1068 ENUM_ENT(EM_COREA_1ST, "EM_COREA_1ST"),
1069 ENUM_ENT(EM_COREA_2ND, "EM_COREA_2ND"),
1070 ENUM_ENT(EM_ARC_COMPACT2, "EM_ARC_COMPACT2"),
1071 ENUM_ENT(EM_OPEN8, "EM_OPEN8"),
1072 ENUM_ENT(EM_RL78, "Renesas RL78"),
1073 ENUM_ENT(EM_VIDEOCORE5, "Broadcom VideoCore V processor"),
1074 ENUM_ENT(EM_78KOR, "EM_78KOR"),
1075 ENUM_ENT(EM_56800EX, "EM_56800EX"),
1076 ENUM_ENT(EM_AMDGPU, "EM_AMDGPU"),
1077 ENUM_ENT(EM_RISCV, "RISC-V"),
1078 ENUM_ENT(EM_LANAI, "EM_LANAI"),
1079 ENUM_ENT(EM_BPF, "EM_BPF"),
1082 static const EnumEntry<unsigned> ElfSymbolBindings[] = {
1083 {"Local", "LOCAL", ELF::STB_LOCAL},
1084 {"Global", "GLOBAL", ELF::STB_GLOBAL},
1085 {"Weak", "WEAK", ELF::STB_WEAK},
1086 {"Unique", "UNIQUE", ELF::STB_GNU_UNIQUE}};
1088 static const EnumEntry<unsigned> ElfSymbolVisibilities[] = {
1089 {"DEFAULT", "DEFAULT", ELF::STV_DEFAULT},
1090 {"INTERNAL", "INTERNAL", ELF::STV_INTERNAL},
1091 {"HIDDEN", "HIDDEN", ELF::STV_HIDDEN},
1092 {"PROTECTED", "PROTECTED", ELF::STV_PROTECTED}};
1094 static const EnumEntry<unsigned> AMDGPUSymbolTypes[] = {
1095 { "AMDGPU_HSA_KERNEL", ELF::STT_AMDGPU_HSA_KERNEL }
1098 static const char *getGroupType(uint32_t Flag) {
1099 if (Flag & ELF::GRP_COMDAT)
1100 return "COMDAT";
1101 else
1102 return "(unknown)";
1105 static const EnumEntry<unsigned> ElfSectionFlags[] = {
1106 ENUM_ENT(SHF_WRITE, "W"),
1107 ENUM_ENT(SHF_ALLOC, "A"),
1108 ENUM_ENT(SHF_EXCLUDE, "E"),
1109 ENUM_ENT(SHF_EXECINSTR, "X"),
1110 ENUM_ENT(SHF_MERGE, "M"),
1111 ENUM_ENT(SHF_STRINGS, "S"),
1112 ENUM_ENT(SHF_INFO_LINK, "I"),
1113 ENUM_ENT(SHF_LINK_ORDER, "L"),
1114 ENUM_ENT(SHF_OS_NONCONFORMING, "o"),
1115 ENUM_ENT(SHF_GROUP, "G"),
1116 ENUM_ENT(SHF_TLS, "T"),
1117 ENUM_ENT(SHF_MASKOS, "o"),
1118 ENUM_ENT(SHF_MASKPROC, "p"),
1119 ENUM_ENT_1(SHF_COMPRESSED),
1122 static const EnumEntry<unsigned> ElfXCoreSectionFlags[] = {
1123 LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_CP_SECTION),
1124 LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_DP_SECTION)
1127 static const EnumEntry<unsigned> ElfARMSectionFlags[] = {
1128 LLVM_READOBJ_ENUM_ENT(ELF, SHF_ARM_PURECODE)
1131 static const EnumEntry<unsigned> ElfHexagonSectionFlags[] = {
1132 LLVM_READOBJ_ENUM_ENT(ELF, SHF_HEX_GPREL)
1135 static const EnumEntry<unsigned> ElfMipsSectionFlags[] = {
1136 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NODUPES),
1137 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NAMES ),
1138 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_LOCAL ),
1139 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NOSTRIP),
1140 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_GPREL ),
1141 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_MERGE ),
1142 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_ADDR ),
1143 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_STRING )
1146 static const EnumEntry<unsigned> ElfX86_64SectionFlags[] = {
1147 LLVM_READOBJ_ENUM_ENT(ELF, SHF_X86_64_LARGE)
1150 static std::string getGNUFlags(uint64_t Flags) {
1151 std::string Str;
1152 for (auto Entry : ElfSectionFlags) {
1153 uint64_t Flag = Entry.Value & Flags;
1154 Flags &= ~Entry.Value;
1155 switch (Flag) {
1156 case ELF::SHF_WRITE:
1157 case ELF::SHF_ALLOC:
1158 case ELF::SHF_EXECINSTR:
1159 case ELF::SHF_MERGE:
1160 case ELF::SHF_STRINGS:
1161 case ELF::SHF_INFO_LINK:
1162 case ELF::SHF_LINK_ORDER:
1163 case ELF::SHF_OS_NONCONFORMING:
1164 case ELF::SHF_GROUP:
1165 case ELF::SHF_TLS:
1166 case ELF::SHF_EXCLUDE:
1167 Str += Entry.AltName;
1168 break;
1169 default:
1170 if (Flag & ELF::SHF_MASKOS)
1171 Str += "o";
1172 else if (Flag & ELF::SHF_MASKPROC)
1173 Str += "p";
1174 else if (Flag)
1175 Str += "x";
1178 return Str;
1181 static const char *getElfSegmentType(unsigned Arch, unsigned Type) {
1182 // Check potentially overlapped processor-specific
1183 // program header type.
1184 switch (Arch) {
1185 case ELF::EM_ARM:
1186 switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, PT_ARM_EXIDX); }
1187 break;
1188 case ELF::EM_MIPS:
1189 case ELF::EM_MIPS_RS3_LE:
1190 switch (Type) {
1191 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_REGINFO);
1192 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_RTPROC);
1193 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_OPTIONS);
1194 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_ABIFLAGS);
1196 break;
1199 switch (Type) {
1200 LLVM_READOBJ_ENUM_CASE(ELF, PT_NULL );
1201 LLVM_READOBJ_ENUM_CASE(ELF, PT_LOAD );
1202 LLVM_READOBJ_ENUM_CASE(ELF, PT_DYNAMIC);
1203 LLVM_READOBJ_ENUM_CASE(ELF, PT_INTERP );
1204 LLVM_READOBJ_ENUM_CASE(ELF, PT_NOTE );
1205 LLVM_READOBJ_ENUM_CASE(ELF, PT_SHLIB );
1206 LLVM_READOBJ_ENUM_CASE(ELF, PT_PHDR );
1207 LLVM_READOBJ_ENUM_CASE(ELF, PT_TLS );
1209 LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_EH_FRAME);
1210 LLVM_READOBJ_ENUM_CASE(ELF, PT_SUNW_UNWIND);
1212 LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_STACK);
1213 LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_RELRO);
1215 LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_RANDOMIZE);
1216 LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_WXNEEDED);
1217 LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_BOOTDATA);
1219 default:
1220 return "";
1224 static std::string getElfPtType(unsigned Arch, unsigned Type) {
1225 switch (Type) {
1226 LLVM_READOBJ_PHDR_ENUM(ELF, PT_NULL)
1227 LLVM_READOBJ_PHDR_ENUM(ELF, PT_LOAD)
1228 LLVM_READOBJ_PHDR_ENUM(ELF, PT_DYNAMIC)
1229 LLVM_READOBJ_PHDR_ENUM(ELF, PT_INTERP)
1230 LLVM_READOBJ_PHDR_ENUM(ELF, PT_NOTE)
1231 LLVM_READOBJ_PHDR_ENUM(ELF, PT_SHLIB)
1232 LLVM_READOBJ_PHDR_ENUM(ELF, PT_PHDR)
1233 LLVM_READOBJ_PHDR_ENUM(ELF, PT_TLS)
1234 LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_EH_FRAME)
1235 LLVM_READOBJ_PHDR_ENUM(ELF, PT_SUNW_UNWIND)
1236 LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_STACK)
1237 LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_RELRO)
1238 default:
1239 // All machine specific PT_* types
1240 switch (Arch) {
1241 case ELF::EM_ARM:
1242 if (Type == ELF::PT_ARM_EXIDX)
1243 return "EXIDX";
1244 break;
1245 case ELF::EM_MIPS:
1246 case ELF::EM_MIPS_RS3_LE:
1247 switch (Type) {
1248 case PT_MIPS_REGINFO:
1249 return "REGINFO";
1250 case PT_MIPS_RTPROC:
1251 return "RTPROC";
1252 case PT_MIPS_OPTIONS:
1253 return "OPTIONS";
1254 case PT_MIPS_ABIFLAGS:
1255 return "ABIFLAGS";
1257 break;
1260 return std::string("<unknown>: ") + to_string(format_hex(Type, 1));
1263 static const EnumEntry<unsigned> ElfSegmentFlags[] = {
1264 LLVM_READOBJ_ENUM_ENT(ELF, PF_X),
1265 LLVM_READOBJ_ENUM_ENT(ELF, PF_W),
1266 LLVM_READOBJ_ENUM_ENT(ELF, PF_R)
1269 static const EnumEntry<unsigned> ElfHeaderMipsFlags[] = {
1270 ENUM_ENT(EF_MIPS_NOREORDER, "noreorder"),
1271 ENUM_ENT(EF_MIPS_PIC, "pic"),
1272 ENUM_ENT(EF_MIPS_CPIC, "cpic"),
1273 ENUM_ENT(EF_MIPS_ABI2, "abi2"),
1274 ENUM_ENT(EF_MIPS_32BITMODE, "32bitmode"),
1275 ENUM_ENT(EF_MIPS_FP64, "fp64"),
1276 ENUM_ENT(EF_MIPS_NAN2008, "nan2008"),
1277 ENUM_ENT(EF_MIPS_ABI_O32, "o32"),
1278 ENUM_ENT(EF_MIPS_ABI_O64, "o64"),
1279 ENUM_ENT(EF_MIPS_ABI_EABI32, "eabi32"),
1280 ENUM_ENT(EF_MIPS_ABI_EABI64, "eabi64"),
1281 ENUM_ENT(EF_MIPS_MACH_3900, "3900"),
1282 ENUM_ENT(EF_MIPS_MACH_4010, "4010"),
1283 ENUM_ENT(EF_MIPS_MACH_4100, "4100"),
1284 ENUM_ENT(EF_MIPS_MACH_4650, "4650"),
1285 ENUM_ENT(EF_MIPS_MACH_4120, "4120"),
1286 ENUM_ENT(EF_MIPS_MACH_4111, "4111"),
1287 ENUM_ENT(EF_MIPS_MACH_SB1, "sb1"),
1288 ENUM_ENT(EF_MIPS_MACH_OCTEON, "octeon"),
1289 ENUM_ENT(EF_MIPS_MACH_XLR, "xlr"),
1290 ENUM_ENT(EF_MIPS_MACH_OCTEON2, "octeon2"),
1291 ENUM_ENT(EF_MIPS_MACH_OCTEON3, "octeon3"),
1292 ENUM_ENT(EF_MIPS_MACH_5400, "5400"),
1293 ENUM_ENT(EF_MIPS_MACH_5900, "5900"),
1294 ENUM_ENT(EF_MIPS_MACH_5500, "5500"),
1295 ENUM_ENT(EF_MIPS_MACH_9000, "9000"),
1296 ENUM_ENT(EF_MIPS_MACH_LS2E, "loongson-2e"),
1297 ENUM_ENT(EF_MIPS_MACH_LS2F, "loongson-2f"),
1298 ENUM_ENT(EF_MIPS_MACH_LS3A, "loongson-3a"),
1299 ENUM_ENT(EF_MIPS_MICROMIPS, "micromips"),
1300 ENUM_ENT(EF_MIPS_ARCH_ASE_M16, "mips16"),
1301 ENUM_ENT(EF_MIPS_ARCH_ASE_MDMX, "mdmx"),
1302 ENUM_ENT(EF_MIPS_ARCH_1, "mips1"),
1303 ENUM_ENT(EF_MIPS_ARCH_2, "mips2"),
1304 ENUM_ENT(EF_MIPS_ARCH_3, "mips3"),
1305 ENUM_ENT(EF_MIPS_ARCH_4, "mips4"),
1306 ENUM_ENT(EF_MIPS_ARCH_5, "mips5"),
1307 ENUM_ENT(EF_MIPS_ARCH_32, "mips32"),
1308 ENUM_ENT(EF_MIPS_ARCH_64, "mips64"),
1309 ENUM_ENT(EF_MIPS_ARCH_32R2, "mips32r2"),
1310 ENUM_ENT(EF_MIPS_ARCH_64R2, "mips64r2"),
1311 ENUM_ENT(EF_MIPS_ARCH_32R6, "mips32r6"),
1312 ENUM_ENT(EF_MIPS_ARCH_64R6, "mips64r6")
1315 static const EnumEntry<unsigned> ElfHeaderAMDGPUFlags[] = {
1316 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_NONE),
1317 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R600),
1318 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R630),
1319 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RS880),
1320 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV670),
1321 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV710),
1322 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV730),
1323 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV770),
1324 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CEDAR),
1325 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CYPRESS),
1326 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_JUNIPER),
1327 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_REDWOOD),
1328 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_SUMO),
1329 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_BARTS),
1330 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAICOS),
1331 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAYMAN),
1332 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_TURKS),
1333 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX600),
1334 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX601),
1335 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX700),
1336 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX701),
1337 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX702),
1338 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX703),
1339 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX704),
1340 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX801),
1341 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX802),
1342 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX803),
1343 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX810),
1344 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX900),
1345 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX902),
1346 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX904),
1347 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX906),
1348 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX908),
1349 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX909),
1350 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1010),
1351 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1011),
1352 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1012),
1353 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_XNACK),
1354 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_SRAM_ECC)
1357 static const EnumEntry<unsigned> ElfHeaderRISCVFlags[] = {
1358 ENUM_ENT(EF_RISCV_RVC, "RVC"),
1359 ENUM_ENT(EF_RISCV_FLOAT_ABI_SINGLE, "single-float ABI"),
1360 ENUM_ENT(EF_RISCV_FLOAT_ABI_DOUBLE, "double-float ABI"),
1361 ENUM_ENT(EF_RISCV_FLOAT_ABI_QUAD, "quad-float ABI"),
1362 ENUM_ENT(EF_RISCV_RVE, "RVE")
1365 static const EnumEntry<unsigned> ElfSymOtherFlags[] = {
1366 LLVM_READOBJ_ENUM_ENT(ELF, STV_INTERNAL),
1367 LLVM_READOBJ_ENUM_ENT(ELF, STV_HIDDEN),
1368 LLVM_READOBJ_ENUM_ENT(ELF, STV_PROTECTED)
1371 static const EnumEntry<unsigned> ElfMipsSymOtherFlags[] = {
1372 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL),
1373 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT),
1374 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PIC),
1375 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MICROMIPS)
1378 static const EnumEntry<unsigned> ElfMips16SymOtherFlags[] = {
1379 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL),
1380 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT),
1381 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MIPS16)
1384 static const char *getElfMipsOptionsOdkType(unsigned Odk) {
1385 switch (Odk) {
1386 LLVM_READOBJ_ENUM_CASE(ELF, ODK_NULL);
1387 LLVM_READOBJ_ENUM_CASE(ELF, ODK_REGINFO);
1388 LLVM_READOBJ_ENUM_CASE(ELF, ODK_EXCEPTIONS);
1389 LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAD);
1390 LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWPATCH);
1391 LLVM_READOBJ_ENUM_CASE(ELF, ODK_FILL);
1392 LLVM_READOBJ_ENUM_CASE(ELF, ODK_TAGS);
1393 LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWAND);
1394 LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWOR);
1395 LLVM_READOBJ_ENUM_CASE(ELF, ODK_GP_GROUP);
1396 LLVM_READOBJ_ENUM_CASE(ELF, ODK_IDENT);
1397 LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAGESIZE);
1398 default:
1399 return "Unknown";
1403 template <typename ELFT>
1404 void ELFDumper<ELFT>::loadDynamicTable(const ELFFile<ELFT> *Obj) {
1405 // Try to locate the PT_DYNAMIC header.
1406 const Elf_Phdr *DynamicPhdr = nullptr;
1407 for (const Elf_Phdr &Phdr :
1408 unwrapOrError(ObjF->getFileName(), Obj->program_headers())) {
1409 if (Phdr.p_type != ELF::PT_DYNAMIC)
1410 continue;
1411 DynamicPhdr = &Phdr;
1412 break;
1415 // Try to locate the .dynamic section in the sections header table.
1416 const Elf_Shdr *DynamicSec = nullptr;
1417 for (const Elf_Shdr &Sec :
1418 unwrapOrError(ObjF->getFileName(), Obj->sections())) {
1419 if (Sec.sh_type != ELF::SHT_DYNAMIC)
1420 continue;
1421 DynamicSec = &Sec;
1422 break;
1425 // Information in the section header has priority over the information
1426 // in a PT_DYNAMIC header.
1427 // Ignore sh_entsize and use the expected value for entry size explicitly.
1428 // This allows us to dump the dynamic sections with a broken sh_entsize
1429 // field.
1430 if (DynamicSec) {
1431 DynamicTable = checkDRI({ObjF->getELFFile()->base() + DynamicSec->sh_offset,
1432 DynamicSec->sh_size, sizeof(Elf_Dyn)});
1433 parseDynamicTable();
1436 // If we have a PT_DYNAMIC header, we will either check the found dynamic
1437 // section or take the dynamic table data directly from the header.
1438 if (!DynamicPhdr)
1439 return;
1441 if (DynamicPhdr->p_offset + DynamicPhdr->p_filesz >
1442 ObjF->getMemoryBufferRef().getBufferSize()) {
1443 reportWarning(
1444 "PT_DYNAMIC segment offset + size exceeds the size of the file");
1445 return;
1448 if (!DynamicSec) {
1449 DynamicTable = createDRIFrom(DynamicPhdr, sizeof(Elf_Dyn));
1450 parseDynamicTable();
1451 return;
1454 StringRef Name =
1455 unwrapOrError(ObjF->getFileName(), Obj->getSectionName(DynamicSec));
1456 if (DynamicSec->sh_addr + DynamicSec->sh_size >
1457 DynamicPhdr->p_vaddr + DynamicPhdr->p_memsz ||
1458 DynamicSec->sh_addr < DynamicPhdr->p_vaddr)
1459 reportWarning("The SHT_DYNAMIC section '" + Name +
1460 "' is not contained within the "
1461 "PT_DYNAMIC segment");
1463 if (DynamicSec->sh_addr != DynamicPhdr->p_vaddr)
1464 reportWarning("The SHT_DYNAMIC section '" + Name +
1465 "' is not at the start of "
1466 "PT_DYNAMIC segment");
1469 template <typename ELFT>
1470 ELFDumper<ELFT>::ELFDumper(const object::ELFObjectFile<ELFT> *ObjF,
1471 ScopedPrinter &Writer)
1472 : ObjDumper(Writer), ObjF(ObjF) {
1473 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
1475 for (const Elf_Shdr &Sec :
1476 unwrapOrError(ObjF->getFileName(), Obj->sections())) {
1477 switch (Sec.sh_type) {
1478 case ELF::SHT_SYMTAB:
1479 if (!DotSymtabSec)
1480 DotSymtabSec = &Sec;
1481 break;
1482 case ELF::SHT_DYNSYM:
1483 if (!DynSymRegion.Size) {
1484 DynSymRegion = createDRIFrom(&Sec);
1485 // This is only used (if Elf_Shdr present)for naming section in GNU
1486 // style
1487 DynSymtabName =
1488 unwrapOrError(ObjF->getFileName(), Obj->getSectionName(&Sec));
1490 if (Expected<StringRef> E = Obj->getStringTableForSymtab(Sec))
1491 DynamicStringTable = *E;
1492 else
1493 warn(E.takeError());
1495 break;
1496 case ELF::SHT_SYMTAB_SHNDX:
1497 ShndxTable = unwrapOrError(ObjF->getFileName(), Obj->getSHNDXTable(Sec));
1498 break;
1499 case ELF::SHT_GNU_versym:
1500 if (!SymbolVersionSection)
1501 SymbolVersionSection = &Sec;
1502 break;
1503 case ELF::SHT_GNU_verdef:
1504 if (!SymbolVersionDefSection)
1505 SymbolVersionDefSection = &Sec;
1506 break;
1507 case ELF::SHT_GNU_verneed:
1508 if (!SymbolVersionNeedSection)
1509 SymbolVersionNeedSection = &Sec;
1510 break;
1511 case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
1512 if (!DotCGProfileSec)
1513 DotCGProfileSec = &Sec;
1514 break;
1515 case ELF::SHT_LLVM_ADDRSIG:
1516 if (!DotAddrsigSec)
1517 DotAddrsigSec = &Sec;
1518 break;
1522 loadDynamicTable(Obj);
1524 if (opts::Output == opts::GNU)
1525 ELFDumperStyle.reset(new GNUStyle<ELFT>(Writer, this));
1526 else
1527 ELFDumperStyle.reset(new LLVMStyle<ELFT>(Writer, this));
1530 static const char *getTypeString(unsigned Arch, uint64_t Type) {
1531 #define DYNAMIC_TAG(n, v)
1532 switch (Arch) {
1534 case EM_AARCH64:
1535 switch (Type) {
1536 #define AARCH64_DYNAMIC_TAG(name, value) \
1537 case DT_##name: \
1538 return #name;
1539 #include "llvm/BinaryFormat/DynamicTags.def"
1540 #undef AARCH64_DYNAMIC_TAG
1542 break;
1544 case EM_HEXAGON:
1545 switch (Type) {
1546 #define HEXAGON_DYNAMIC_TAG(name, value) \
1547 case DT_##name: \
1548 return #name;
1549 #include "llvm/BinaryFormat/DynamicTags.def"
1550 #undef HEXAGON_DYNAMIC_TAG
1552 break;
1554 case EM_MIPS:
1555 switch (Type) {
1556 #define MIPS_DYNAMIC_TAG(name, value) \
1557 case DT_##name: \
1558 return #name;
1559 #include "llvm/BinaryFormat/DynamicTags.def"
1560 #undef MIPS_DYNAMIC_TAG
1562 break;
1564 case EM_PPC64:
1565 switch (Type) {
1566 #define PPC64_DYNAMIC_TAG(name, value) \
1567 case DT_##name: \
1568 return #name;
1569 #include "llvm/BinaryFormat/DynamicTags.def"
1570 #undef PPC64_DYNAMIC_TAG
1572 break;
1574 #undef DYNAMIC_TAG
1575 switch (Type) {
1576 // Now handle all dynamic tags except the architecture specific ones
1577 #define AARCH64_DYNAMIC_TAG(name, value)
1578 #define MIPS_DYNAMIC_TAG(name, value)
1579 #define HEXAGON_DYNAMIC_TAG(name, value)
1580 #define PPC64_DYNAMIC_TAG(name, value)
1581 // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
1582 #define DYNAMIC_TAG_MARKER(name, value)
1583 #define DYNAMIC_TAG(name, value) \
1584 case DT_##name: \
1585 return #name;
1586 #include "llvm/BinaryFormat/DynamicTags.def"
1587 #undef DYNAMIC_TAG
1588 #undef AARCH64_DYNAMIC_TAG
1589 #undef MIPS_DYNAMIC_TAG
1590 #undef HEXAGON_DYNAMIC_TAG
1591 #undef PPC64_DYNAMIC_TAG
1592 #undef DYNAMIC_TAG_MARKER
1593 default:
1594 return "unknown";
1598 template <typename ELFT> void ELFDumper<ELFT>::parseDynamicTable() {
1599 auto toMappedAddr = [&](uint64_t Tag, uint64_t VAddr) -> const uint8_t * {
1600 auto MappedAddrOrError = ObjF->getELFFile()->toMappedAddr(VAddr);
1601 if (!MappedAddrOrError) {
1602 reportWarning("Unable to parse DT_" +
1603 Twine(getTypeString(
1604 ObjF->getELFFile()->getHeader()->e_machine, Tag)) +
1605 ": " + llvm::toString(MappedAddrOrError.takeError()));
1606 return nullptr;
1608 return MappedAddrOrError.get();
1611 uint64_t SONameOffset = 0;
1612 const char *StringTableBegin = nullptr;
1613 uint64_t StringTableSize = 0;
1614 for (const Elf_Dyn &Dyn : dynamic_table()) {
1615 switch (Dyn.d_tag) {
1616 case ELF::DT_HASH:
1617 HashTable = reinterpret_cast<const Elf_Hash *>(
1618 toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
1619 break;
1620 case ELF::DT_GNU_HASH:
1621 GnuHashTable = reinterpret_cast<const Elf_GnuHash *>(
1622 toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
1623 break;
1624 case ELF::DT_STRTAB:
1625 StringTableBegin = reinterpret_cast<const char *>(
1626 toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
1627 break;
1628 case ELF::DT_STRSZ:
1629 StringTableSize = Dyn.getVal();
1630 break;
1631 case ELF::DT_SYMTAB:
1632 DynSymRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
1633 DynSymRegion.EntSize = sizeof(Elf_Sym);
1634 break;
1635 case ELF::DT_RELA:
1636 DynRelaRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
1637 break;
1638 case ELF::DT_RELASZ:
1639 DynRelaRegion.Size = Dyn.getVal();
1640 break;
1641 case ELF::DT_RELAENT:
1642 DynRelaRegion.EntSize = Dyn.getVal();
1643 break;
1644 case ELF::DT_SONAME:
1645 SONameOffset = Dyn.getVal();
1646 break;
1647 case ELF::DT_REL:
1648 DynRelRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
1649 break;
1650 case ELF::DT_RELSZ:
1651 DynRelRegion.Size = Dyn.getVal();
1652 break;
1653 case ELF::DT_RELENT:
1654 DynRelRegion.EntSize = Dyn.getVal();
1655 break;
1656 case ELF::DT_RELR:
1657 case ELF::DT_ANDROID_RELR:
1658 DynRelrRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
1659 break;
1660 case ELF::DT_RELRSZ:
1661 case ELF::DT_ANDROID_RELRSZ:
1662 DynRelrRegion.Size = Dyn.getVal();
1663 break;
1664 case ELF::DT_RELRENT:
1665 case ELF::DT_ANDROID_RELRENT:
1666 DynRelrRegion.EntSize = Dyn.getVal();
1667 break;
1668 case ELF::DT_PLTREL:
1669 if (Dyn.getVal() == DT_REL)
1670 DynPLTRelRegion.EntSize = sizeof(Elf_Rel);
1671 else if (Dyn.getVal() == DT_RELA)
1672 DynPLTRelRegion.EntSize = sizeof(Elf_Rela);
1673 else
1674 reportError(Twine("unknown DT_PLTREL value of ") +
1675 Twine((uint64_t)Dyn.getVal()));
1676 break;
1677 case ELF::DT_JMPREL:
1678 DynPLTRelRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
1679 break;
1680 case ELF::DT_PLTRELSZ:
1681 DynPLTRelRegion.Size = Dyn.getVal();
1682 break;
1685 if (StringTableBegin)
1686 DynamicStringTable = StringRef(StringTableBegin, StringTableSize);
1687 SOName = getDynamicString(SONameOffset);
1690 template <typename ELFT>
1691 typename ELFDumper<ELFT>::Elf_Rel_Range ELFDumper<ELFT>::dyn_rels() const {
1692 return DynRelRegion.getAsArrayRef<Elf_Rel>();
1695 template <typename ELFT>
1696 typename ELFDumper<ELFT>::Elf_Rela_Range ELFDumper<ELFT>::dyn_relas() const {
1697 return DynRelaRegion.getAsArrayRef<Elf_Rela>();
1700 template <typename ELFT>
1701 typename ELFDumper<ELFT>::Elf_Relr_Range ELFDumper<ELFT>::dyn_relrs() const {
1702 return DynRelrRegion.getAsArrayRef<Elf_Relr>();
1705 template <class ELFT> void ELFDumper<ELFT>::printFileHeaders() {
1706 ELFDumperStyle->printFileHeaders(ObjF->getELFFile());
1709 template <class ELFT> void ELFDumper<ELFT>::printSectionHeaders() {
1710 ELFDumperStyle->printSectionHeaders(ObjF->getELFFile());
1713 template <class ELFT> void ELFDumper<ELFT>::printRelocations() {
1714 ELFDumperStyle->printRelocations(ObjF->getELFFile());
1717 template <class ELFT>
1718 void ELFDumper<ELFT>::printProgramHeaders(
1719 bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) {
1720 ELFDumperStyle->printProgramHeaders(ObjF->getELFFile(), PrintProgramHeaders,
1721 PrintSectionMapping);
1724 template <typename ELFT> void ELFDumper<ELFT>::printVersionInfo() {
1725 // Dump version symbol section.
1726 ELFDumperStyle->printVersionSymbolSection(ObjF->getELFFile(),
1727 SymbolVersionSection);
1729 // Dump version definition section.
1730 ELFDumperStyle->printVersionDefinitionSection(ObjF->getELFFile(),
1731 SymbolVersionDefSection);
1733 // Dump version dependency section.
1734 ELFDumperStyle->printVersionDependencySection(ObjF->getELFFile(),
1735 SymbolVersionNeedSection);
1738 template <class ELFT> void ELFDumper<ELFT>::printDynamicRelocations() {
1739 ELFDumperStyle->printDynamicRelocations(ObjF->getELFFile());
1742 template <class ELFT>
1743 void ELFDumper<ELFT>::printSymbols(bool PrintSymbols,
1744 bool PrintDynamicSymbols) {
1745 ELFDumperStyle->printSymbols(ObjF->getELFFile(), PrintSymbols,
1746 PrintDynamicSymbols);
1749 template <class ELFT> void ELFDumper<ELFT>::printHashSymbols() {
1750 ELFDumperStyle->printHashSymbols(ObjF->getELFFile());
1753 template <class ELFT> void ELFDumper<ELFT>::printHashHistogram() {
1754 ELFDumperStyle->printHashHistogram(ObjF->getELFFile());
1757 template <class ELFT> void ELFDumper<ELFT>::printCGProfile() {
1758 ELFDumperStyle->printCGProfile(ObjF->getELFFile());
1761 template <class ELFT> void ELFDumper<ELFT>::printNotes() {
1762 ELFDumperStyle->printNotes(ObjF->getELFFile());
1765 template <class ELFT> void ELFDumper<ELFT>::printELFLinkerOptions() {
1766 ELFDumperStyle->printELFLinkerOptions(ObjF->getELFFile());
1769 template <class ELFT> void ELFDumper<ELFT>::printStackSizes() {
1770 ELFDumperStyle->printStackSizes(ObjF);
1773 #define LLVM_READOBJ_DT_FLAG_ENT(prefix, enum) \
1774 { #enum, prefix##_##enum }
1776 static const EnumEntry<unsigned> ElfDynamicDTFlags[] = {
1777 LLVM_READOBJ_DT_FLAG_ENT(DF, ORIGIN),
1778 LLVM_READOBJ_DT_FLAG_ENT(DF, SYMBOLIC),
1779 LLVM_READOBJ_DT_FLAG_ENT(DF, TEXTREL),
1780 LLVM_READOBJ_DT_FLAG_ENT(DF, BIND_NOW),
1781 LLVM_READOBJ_DT_FLAG_ENT(DF, STATIC_TLS)
1784 static const EnumEntry<unsigned> ElfDynamicDTFlags1[] = {
1785 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOW),
1786 LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAL),
1787 LLVM_READOBJ_DT_FLAG_ENT(DF_1, GROUP),
1788 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODELETE),
1789 LLVM_READOBJ_DT_FLAG_ENT(DF_1, LOADFLTR),
1790 LLVM_READOBJ_DT_FLAG_ENT(DF_1, INITFIRST),
1791 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOOPEN),
1792 LLVM_READOBJ_DT_FLAG_ENT(DF_1, ORIGIN),
1793 LLVM_READOBJ_DT_FLAG_ENT(DF_1, DIRECT),
1794 LLVM_READOBJ_DT_FLAG_ENT(DF_1, TRANS),
1795 LLVM_READOBJ_DT_FLAG_ENT(DF_1, INTERPOSE),
1796 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODEFLIB),
1797 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODUMP),
1798 LLVM_READOBJ_DT_FLAG_ENT(DF_1, CONFALT),
1799 LLVM_READOBJ_DT_FLAG_ENT(DF_1, ENDFILTEE),
1800 LLVM_READOBJ_DT_FLAG_ENT(DF_1, DISPRELDNE),
1801 LLVM_READOBJ_DT_FLAG_ENT(DF_1, DISPRELPND),
1802 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODIRECT),
1803 LLVM_READOBJ_DT_FLAG_ENT(DF_1, IGNMULDEF),
1804 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOKSYMS),
1805 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOHDR),
1806 LLVM_READOBJ_DT_FLAG_ENT(DF_1, EDITED),
1807 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NORELOC),
1808 LLVM_READOBJ_DT_FLAG_ENT(DF_1, SYMINTPOSE),
1809 LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAUDIT),
1810 LLVM_READOBJ_DT_FLAG_ENT(DF_1, SINGLETON)
1813 static const EnumEntry<unsigned> ElfDynamicDTMipsFlags[] = {
1814 LLVM_READOBJ_DT_FLAG_ENT(RHF, NONE),
1815 LLVM_READOBJ_DT_FLAG_ENT(RHF, QUICKSTART),
1816 LLVM_READOBJ_DT_FLAG_ENT(RHF, NOTPOT),
1817 LLVM_READOBJ_DT_FLAG_ENT(RHS, NO_LIBRARY_REPLACEMENT),
1818 LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_MOVE),
1819 LLVM_READOBJ_DT_FLAG_ENT(RHF, SGI_ONLY),
1820 LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_INIT),
1821 LLVM_READOBJ_DT_FLAG_ENT(RHF, DELTA_C_PLUS_PLUS),
1822 LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_START_INIT),
1823 LLVM_READOBJ_DT_FLAG_ENT(RHF, PIXIE),
1824 LLVM_READOBJ_DT_FLAG_ENT(RHF, DEFAULT_DELAY_LOAD),
1825 LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTART),
1826 LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTARTED),
1827 LLVM_READOBJ_DT_FLAG_ENT(RHF, CORD),
1828 LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_UNRES_UNDEF),
1829 LLVM_READOBJ_DT_FLAG_ENT(RHF, RLD_ORDER_SAFE)
1832 #undef LLVM_READOBJ_DT_FLAG_ENT
1834 template <typename T, typename TFlag>
1835 void printFlags(T Value, ArrayRef<EnumEntry<TFlag>> Flags, raw_ostream &OS) {
1836 using FlagEntry = EnumEntry<TFlag>;
1837 using FlagVector = SmallVector<FlagEntry, 10>;
1838 FlagVector SetFlags;
1840 for (const auto &Flag : Flags) {
1841 if (Flag.Value == 0)
1842 continue;
1844 if ((Value & Flag.Value) == Flag.Value)
1845 SetFlags.push_back(Flag);
1848 for (const auto &Flag : SetFlags) {
1849 OS << Flag.Name << " ";
1853 template <class ELFT>
1854 void ELFDumper<ELFT>::printDynamicEntry(raw_ostream &OS, uint64_t Type,
1855 uint64_t Value) const {
1856 const char *ConvChar =
1857 (opts::Output == opts::GNU) ? "0x%" PRIx64 : "0x%" PRIX64;
1859 // Handle custom printing of architecture specific tags
1860 switch (ObjF->getELFFile()->getHeader()->e_machine) {
1861 case EM_AARCH64:
1862 switch (Type) {
1863 case DT_AARCH64_BTI_PLT:
1864 case DT_AARCH64_PAC_PLT:
1865 OS << Value;
1866 return;
1867 default:
1868 break;
1870 break;
1871 case EM_HEXAGON:
1872 switch (Type) {
1873 case DT_HEXAGON_VER:
1874 OS << Value;
1875 return;
1876 case DT_HEXAGON_SYMSZ:
1877 case DT_HEXAGON_PLT:
1878 OS << format(ConvChar, Value);
1879 return;
1880 default:
1881 break;
1883 break;
1884 case EM_MIPS:
1885 switch (Type) {
1886 case DT_MIPS_RLD_VERSION:
1887 case DT_MIPS_LOCAL_GOTNO:
1888 case DT_MIPS_SYMTABNO:
1889 case DT_MIPS_UNREFEXTNO:
1890 OS << Value;
1891 return;
1892 case DT_MIPS_TIME_STAMP:
1893 case DT_MIPS_ICHECKSUM:
1894 case DT_MIPS_IVERSION:
1895 case DT_MIPS_BASE_ADDRESS:
1896 case DT_MIPS_MSYM:
1897 case DT_MIPS_CONFLICT:
1898 case DT_MIPS_LIBLIST:
1899 case DT_MIPS_CONFLICTNO:
1900 case DT_MIPS_LIBLISTNO:
1901 case DT_MIPS_GOTSYM:
1902 case DT_MIPS_HIPAGENO:
1903 case DT_MIPS_RLD_MAP:
1904 case DT_MIPS_DELTA_CLASS:
1905 case DT_MIPS_DELTA_CLASS_NO:
1906 case DT_MIPS_DELTA_INSTANCE:
1907 case DT_MIPS_DELTA_RELOC:
1908 case DT_MIPS_DELTA_RELOC_NO:
1909 case DT_MIPS_DELTA_SYM:
1910 case DT_MIPS_DELTA_SYM_NO:
1911 case DT_MIPS_DELTA_CLASSSYM:
1912 case DT_MIPS_DELTA_CLASSSYM_NO:
1913 case DT_MIPS_CXX_FLAGS:
1914 case DT_MIPS_PIXIE_INIT:
1915 case DT_MIPS_SYMBOL_LIB:
1916 case DT_MIPS_LOCALPAGE_GOTIDX:
1917 case DT_MIPS_LOCAL_GOTIDX:
1918 case DT_MIPS_HIDDEN_GOTIDX:
1919 case DT_MIPS_PROTECTED_GOTIDX:
1920 case DT_MIPS_OPTIONS:
1921 case DT_MIPS_INTERFACE:
1922 case DT_MIPS_DYNSTR_ALIGN:
1923 case DT_MIPS_INTERFACE_SIZE:
1924 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
1925 case DT_MIPS_PERF_SUFFIX:
1926 case DT_MIPS_COMPACT_SIZE:
1927 case DT_MIPS_GP_VALUE:
1928 case DT_MIPS_AUX_DYNAMIC:
1929 case DT_MIPS_PLTGOT:
1930 case DT_MIPS_RWPLT:
1931 case DT_MIPS_RLD_MAP_REL:
1932 OS << format(ConvChar, Value);
1933 return;
1934 case DT_MIPS_FLAGS:
1935 printFlags(Value, makeArrayRef(ElfDynamicDTMipsFlags), OS);
1936 return;
1937 default:
1938 break;
1940 break;
1941 default:
1942 break;
1945 switch (Type) {
1946 case DT_PLTREL:
1947 if (Value == DT_REL) {
1948 OS << "REL";
1949 break;
1950 } else if (Value == DT_RELA) {
1951 OS << "RELA";
1952 break;
1954 LLVM_FALLTHROUGH;
1955 case DT_PLTGOT:
1956 case DT_HASH:
1957 case DT_STRTAB:
1958 case DT_SYMTAB:
1959 case DT_RELA:
1960 case DT_INIT:
1961 case DT_FINI:
1962 case DT_REL:
1963 case DT_JMPREL:
1964 case DT_INIT_ARRAY:
1965 case DT_FINI_ARRAY:
1966 case DT_PREINIT_ARRAY:
1967 case DT_DEBUG:
1968 case DT_VERDEF:
1969 case DT_VERNEED:
1970 case DT_VERSYM:
1971 case DT_GNU_HASH:
1972 case DT_NULL:
1973 OS << format(ConvChar, Value);
1974 break;
1975 case DT_RELACOUNT:
1976 case DT_RELCOUNT:
1977 case DT_VERDEFNUM:
1978 case DT_VERNEEDNUM:
1979 OS << Value;
1980 break;
1981 case DT_PLTRELSZ:
1982 case DT_RELASZ:
1983 case DT_RELAENT:
1984 case DT_STRSZ:
1985 case DT_SYMENT:
1986 case DT_RELSZ:
1987 case DT_RELENT:
1988 case DT_INIT_ARRAYSZ:
1989 case DT_FINI_ARRAYSZ:
1990 case DT_PREINIT_ARRAYSZ:
1991 case DT_ANDROID_RELSZ:
1992 case DT_ANDROID_RELASZ:
1993 OS << Value << " (bytes)";
1994 break;
1995 case DT_NEEDED:
1996 case DT_SONAME:
1997 case DT_AUXILIARY:
1998 case DT_USED:
1999 case DT_FILTER:
2000 case DT_RPATH:
2001 case DT_RUNPATH: {
2002 const std::map<uint64_t, const char*> TagNames = {
2003 {DT_NEEDED, "Shared library"},
2004 {DT_SONAME, "Library soname"},
2005 {DT_AUXILIARY, "Auxiliary library"},
2006 {DT_USED, "Not needed object"},
2007 {DT_FILTER, "Filter library"},
2008 {DT_RPATH, "Library rpath"},
2009 {DT_RUNPATH, "Library runpath"},
2011 OS << TagNames.at(Type) << ": [" << getDynamicString(Value) << "]";
2012 break;
2014 case DT_FLAGS:
2015 printFlags(Value, makeArrayRef(ElfDynamicDTFlags), OS);
2016 break;
2017 case DT_FLAGS_1:
2018 printFlags(Value, makeArrayRef(ElfDynamicDTFlags1), OS);
2019 break;
2020 default:
2021 OS << format(ConvChar, Value);
2022 break;
2026 template <class ELFT>
2027 std::string ELFDumper<ELFT>::getDynamicString(uint64_t Value) const {
2028 if (DynamicStringTable.empty())
2029 return "<String table is empty or was not found>";
2030 if (Value < DynamicStringTable.size())
2031 return DynamicStringTable.data() + Value;
2032 return Twine("<Invalid offset 0x" + utohexstr(Value) + ">").str();
2035 template <class ELFT> void ELFDumper<ELFT>::printUnwindInfo() {
2036 DwarfCFIEH::PrinterContext<ELFT> Ctx(W, ObjF);
2037 Ctx.printUnwindInformation();
2040 namespace {
2042 template <> void ELFDumper<ELF32LE>::printUnwindInfo() {
2043 const ELFFile<ELF32LE> *Obj = ObjF->getELFFile();
2044 const unsigned Machine = Obj->getHeader()->e_machine;
2045 if (Machine == EM_ARM) {
2046 ARM::EHABI::PrinterContext<ELF32LE> Ctx(W, Obj, ObjF->getFileName(),
2047 DotSymtabSec);
2048 Ctx.PrintUnwindInformation();
2050 DwarfCFIEH::PrinterContext<ELF32LE> Ctx(W, ObjF);
2051 Ctx.printUnwindInformation();
2054 } // end anonymous namespace
2056 template <class ELFT> void ELFDumper<ELFT>::printDynamicTable() {
2057 ELFDumperStyle->printDynamic(ObjF->getELFFile());
2060 template <class ELFT> void ELFDumper<ELFT>::printNeededLibraries() {
2061 ListScope D(W, "NeededLibraries");
2063 std::vector<std::string> Libs;
2064 for (const auto &Entry : dynamic_table())
2065 if (Entry.d_tag == ELF::DT_NEEDED)
2066 Libs.push_back(getDynamicString(Entry.d_un.d_val));
2068 llvm::stable_sort(Libs);
2070 for (const auto &L : Libs)
2071 W.startLine() << L << "\n";
2074 template <typename ELFT> void ELFDumper<ELFT>::printHashTable() {
2075 DictScope D(W, "HashTable");
2076 if (!HashTable)
2077 return;
2078 W.printNumber("Num Buckets", HashTable->nbucket);
2079 W.printNumber("Num Chains", HashTable->nchain);
2080 W.printList("Buckets", HashTable->buckets());
2081 W.printList("Chains", HashTable->chains());
2084 template <typename ELFT> void ELFDumper<ELFT>::printGnuHashTable() {
2085 DictScope D(W, "GnuHashTable");
2086 if (!GnuHashTable)
2087 return;
2088 W.printNumber("Num Buckets", GnuHashTable->nbuckets);
2089 W.printNumber("First Hashed Symbol Index", GnuHashTable->symndx);
2090 W.printNumber("Num Mask Words", GnuHashTable->maskwords);
2091 W.printNumber("Shift Count", GnuHashTable->shift2);
2092 W.printHexList("Bloom Filter", GnuHashTable->filter());
2093 W.printList("Buckets", GnuHashTable->buckets());
2094 Elf_Sym_Range Syms = dynamic_symbols();
2095 unsigned NumSyms = std::distance(Syms.begin(), Syms.end());
2096 if (!NumSyms)
2097 reportError("No dynamic symbol section");
2098 W.printHexList("Values", GnuHashTable->values(NumSyms));
2101 template <typename ELFT> void ELFDumper<ELFT>::printLoadName() {
2102 W.printString("LoadName", SOName);
2105 template <class ELFT> void ELFDumper<ELFT>::printAttributes() {
2106 W.startLine() << "Attributes not implemented.\n";
2109 namespace {
2111 template <> void ELFDumper<ELF32LE>::printAttributes() {
2112 const ELFFile<ELF32LE> *Obj = ObjF->getELFFile();
2113 if (Obj->getHeader()->e_machine != EM_ARM) {
2114 W.startLine() << "Attributes not implemented.\n";
2115 return;
2118 DictScope BA(W, "BuildAttributes");
2119 for (const ELFO::Elf_Shdr &Sec :
2120 unwrapOrError(ObjF->getFileName(), Obj->sections())) {
2121 if (Sec.sh_type != ELF::SHT_ARM_ATTRIBUTES)
2122 continue;
2124 ArrayRef<uint8_t> Contents =
2125 unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(&Sec));
2126 if (Contents[0] != ARMBuildAttrs::Format_Version) {
2127 errs() << "unrecognised FormatVersion: 0x"
2128 << Twine::utohexstr(Contents[0]) << '\n';
2129 continue;
2132 W.printHex("FormatVersion", Contents[0]);
2133 if (Contents.size() == 1)
2134 continue;
2136 ARMAttributeParser(&W).Parse(Contents, true);
2140 template <class ELFT> class MipsGOTParser {
2141 public:
2142 TYPEDEF_ELF_TYPES(ELFT)
2143 using Entry = typename ELFO::Elf_Addr;
2144 using Entries = ArrayRef<Entry>;
2146 const bool IsStatic;
2147 const ELFO * const Obj;
2149 MipsGOTParser(const ELFO *Obj, StringRef FileName, Elf_Dyn_Range DynTable,
2150 Elf_Sym_Range DynSyms);
2152 bool hasGot() const { return !GotEntries.empty(); }
2153 bool hasPlt() const { return !PltEntries.empty(); }
2155 uint64_t getGp() const;
2157 const Entry *getGotLazyResolver() const;
2158 const Entry *getGotModulePointer() const;
2159 const Entry *getPltLazyResolver() const;
2160 const Entry *getPltModulePointer() const;
2162 Entries getLocalEntries() const;
2163 Entries getGlobalEntries() const;
2164 Entries getOtherEntries() const;
2165 Entries getPltEntries() const;
2167 uint64_t getGotAddress(const Entry * E) const;
2168 int64_t getGotOffset(const Entry * E) const;
2169 const Elf_Sym *getGotSym(const Entry *E) const;
2171 uint64_t getPltAddress(const Entry * E) const;
2172 const Elf_Sym *getPltSym(const Entry *E) const;
2174 StringRef getPltStrTable() const { return PltStrTable; }
2176 private:
2177 const Elf_Shdr *GotSec;
2178 size_t LocalNum;
2179 size_t GlobalNum;
2181 const Elf_Shdr *PltSec;
2182 const Elf_Shdr *PltRelSec;
2183 const Elf_Shdr *PltSymTable;
2184 StringRef FileName;
2186 Elf_Sym_Range GotDynSyms;
2187 StringRef PltStrTable;
2189 Entries GotEntries;
2190 Entries PltEntries;
2193 } // end anonymous namespace
2195 template <class ELFT>
2196 MipsGOTParser<ELFT>::MipsGOTParser(const ELFO *Obj, StringRef FileName,
2197 Elf_Dyn_Range DynTable,
2198 Elf_Sym_Range DynSyms)
2199 : IsStatic(DynTable.empty()), Obj(Obj), GotSec(nullptr), LocalNum(0),
2200 GlobalNum(0), PltSec(nullptr), PltRelSec(nullptr), PltSymTable(nullptr),
2201 FileName(FileName) {
2202 // See "Global Offset Table" in Chapter 5 in the following document
2203 // for detailed GOT description.
2204 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
2206 // Find static GOT secton.
2207 if (IsStatic) {
2208 GotSec = findSectionByName(*Obj, FileName, ".got");
2209 if (!GotSec)
2210 reportError("Cannot find .got section");
2212 ArrayRef<uint8_t> Content =
2213 unwrapOrError(FileName, Obj->getSectionContents(GotSec));
2214 GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()),
2215 Content.size() / sizeof(Entry));
2216 LocalNum = GotEntries.size();
2217 return;
2220 // Lookup dynamic table tags which define GOT/PLT layouts.
2221 Optional<uint64_t> DtPltGot;
2222 Optional<uint64_t> DtLocalGotNum;
2223 Optional<uint64_t> DtGotSym;
2224 Optional<uint64_t> DtMipsPltGot;
2225 Optional<uint64_t> DtJmpRel;
2226 for (const auto &Entry : DynTable) {
2227 switch (Entry.getTag()) {
2228 case ELF::DT_PLTGOT:
2229 DtPltGot = Entry.getVal();
2230 break;
2231 case ELF::DT_MIPS_LOCAL_GOTNO:
2232 DtLocalGotNum = Entry.getVal();
2233 break;
2234 case ELF::DT_MIPS_GOTSYM:
2235 DtGotSym = Entry.getVal();
2236 break;
2237 case ELF::DT_MIPS_PLTGOT:
2238 DtMipsPltGot = Entry.getVal();
2239 break;
2240 case ELF::DT_JMPREL:
2241 DtJmpRel = Entry.getVal();
2242 break;
2246 // Find dynamic GOT section.
2247 if (DtPltGot || DtLocalGotNum || DtGotSym) {
2248 if (!DtPltGot)
2249 report_fatal_error("Cannot find PLTGOT dynamic table tag.");
2250 if (!DtLocalGotNum)
2251 report_fatal_error("Cannot find MIPS_LOCAL_GOTNO dynamic table tag.");
2252 if (!DtGotSym)
2253 report_fatal_error("Cannot find MIPS_GOTSYM dynamic table tag.");
2255 size_t DynSymTotal = DynSyms.size();
2256 if (*DtGotSym > DynSymTotal)
2257 reportError("MIPS_GOTSYM exceeds a number of dynamic symbols");
2259 GotSec = findNotEmptySectionByAddress(Obj, FileName, *DtPltGot);
2260 if (!GotSec)
2261 reportError("There is no not empty GOT section at 0x" +
2262 Twine::utohexstr(*DtPltGot));
2264 LocalNum = *DtLocalGotNum;
2265 GlobalNum = DynSymTotal - *DtGotSym;
2267 ArrayRef<uint8_t> Content =
2268 unwrapOrError(FileName, Obj->getSectionContents(GotSec));
2269 GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()),
2270 Content.size() / sizeof(Entry));
2271 GotDynSyms = DynSyms.drop_front(*DtGotSym);
2274 // Find PLT section.
2275 if (DtMipsPltGot || DtJmpRel) {
2276 if (!DtMipsPltGot)
2277 report_fatal_error("Cannot find MIPS_PLTGOT dynamic table tag.");
2278 if (!DtJmpRel)
2279 report_fatal_error("Cannot find JMPREL dynamic table tag.");
2281 PltSec = findNotEmptySectionByAddress(Obj, FileName, * DtMipsPltGot);
2282 if (!PltSec)
2283 report_fatal_error("There is no not empty PLTGOT section at 0x " +
2284 Twine::utohexstr(*DtMipsPltGot));
2286 PltRelSec = findNotEmptySectionByAddress(Obj, FileName, * DtJmpRel);
2287 if (!PltRelSec)
2288 report_fatal_error("There is no not empty RELPLT section at 0x" +
2289 Twine::utohexstr(*DtJmpRel));
2291 ArrayRef<uint8_t> PltContent =
2292 unwrapOrError(FileName, Obj->getSectionContents(PltSec));
2293 PltEntries = Entries(reinterpret_cast<const Entry *>(PltContent.data()),
2294 PltContent.size() / sizeof(Entry));
2296 PltSymTable = unwrapOrError(FileName, Obj->getSection(PltRelSec->sh_link));
2297 PltStrTable =
2298 unwrapOrError(FileName, Obj->getStringTableForSymtab(*PltSymTable));
2302 template <class ELFT> uint64_t MipsGOTParser<ELFT>::getGp() const {
2303 return GotSec->sh_addr + 0x7ff0;
2306 template <class ELFT>
2307 const typename MipsGOTParser<ELFT>::Entry *
2308 MipsGOTParser<ELFT>::getGotLazyResolver() const {
2309 return LocalNum > 0 ? &GotEntries[0] : nullptr;
2312 template <class ELFT>
2313 const typename MipsGOTParser<ELFT>::Entry *
2314 MipsGOTParser<ELFT>::getGotModulePointer() const {
2315 if (LocalNum < 2)
2316 return nullptr;
2317 const Entry &E = GotEntries[1];
2318 if ((E >> (sizeof(Entry) * 8 - 1)) == 0)
2319 return nullptr;
2320 return &E;
2323 template <class ELFT>
2324 typename MipsGOTParser<ELFT>::Entries
2325 MipsGOTParser<ELFT>::getLocalEntries() const {
2326 size_t Skip = getGotModulePointer() ? 2 : 1;
2327 if (LocalNum - Skip <= 0)
2328 return Entries();
2329 return GotEntries.slice(Skip, LocalNum - Skip);
2332 template <class ELFT>
2333 typename MipsGOTParser<ELFT>::Entries
2334 MipsGOTParser<ELFT>::getGlobalEntries() const {
2335 if (GlobalNum == 0)
2336 return Entries();
2337 return GotEntries.slice(LocalNum, GlobalNum);
2340 template <class ELFT>
2341 typename MipsGOTParser<ELFT>::Entries
2342 MipsGOTParser<ELFT>::getOtherEntries() const {
2343 size_t OtherNum = GotEntries.size() - LocalNum - GlobalNum;
2344 if (OtherNum == 0)
2345 return Entries();
2346 return GotEntries.slice(LocalNum + GlobalNum, OtherNum);
2349 template <class ELFT>
2350 uint64_t MipsGOTParser<ELFT>::getGotAddress(const Entry *E) const {
2351 int64_t Offset = std::distance(GotEntries.data(), E) * sizeof(Entry);
2352 return GotSec->sh_addr + Offset;
2355 template <class ELFT>
2356 int64_t MipsGOTParser<ELFT>::getGotOffset(const Entry *E) const {
2357 int64_t Offset = std::distance(GotEntries.data(), E) * sizeof(Entry);
2358 return Offset - 0x7ff0;
2361 template <class ELFT>
2362 const typename MipsGOTParser<ELFT>::Elf_Sym *
2363 MipsGOTParser<ELFT>::getGotSym(const Entry *E) const {
2364 int64_t Offset = std::distance(GotEntries.data(), E);
2365 return &GotDynSyms[Offset - LocalNum];
2368 template <class ELFT>
2369 const typename MipsGOTParser<ELFT>::Entry *
2370 MipsGOTParser<ELFT>::getPltLazyResolver() const {
2371 return PltEntries.empty() ? nullptr : &PltEntries[0];
2374 template <class ELFT>
2375 const typename MipsGOTParser<ELFT>::Entry *
2376 MipsGOTParser<ELFT>::getPltModulePointer() const {
2377 return PltEntries.size() < 2 ? nullptr : &PltEntries[1];
2380 template <class ELFT>
2381 typename MipsGOTParser<ELFT>::Entries
2382 MipsGOTParser<ELFT>::getPltEntries() const {
2383 if (PltEntries.size() <= 2)
2384 return Entries();
2385 return PltEntries.slice(2, PltEntries.size() - 2);
2388 template <class ELFT>
2389 uint64_t MipsGOTParser<ELFT>::getPltAddress(const Entry *E) const {
2390 int64_t Offset = std::distance(PltEntries.data(), E) * sizeof(Entry);
2391 return PltSec->sh_addr + Offset;
2394 template <class ELFT>
2395 const typename MipsGOTParser<ELFT>::Elf_Sym *
2396 MipsGOTParser<ELFT>::getPltSym(const Entry *E) const {
2397 int64_t Offset = std::distance(getPltEntries().data(), E);
2398 if (PltRelSec->sh_type == ELF::SHT_REL) {
2399 Elf_Rel_Range Rels = unwrapOrError(FileName, Obj->rels(PltRelSec));
2400 return unwrapOrError(FileName,
2401 Obj->getRelocationSymbol(&Rels[Offset], PltSymTable));
2402 } else {
2403 Elf_Rela_Range Rels = unwrapOrError(FileName, Obj->relas(PltRelSec));
2404 return unwrapOrError(FileName,
2405 Obj->getRelocationSymbol(&Rels[Offset], PltSymTable));
2409 template <class ELFT> void ELFDumper<ELFT>::printMipsPLTGOT() {
2410 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
2411 if (Obj->getHeader()->e_machine != EM_MIPS)
2412 reportError("MIPS PLT GOT is available for MIPS targets only");
2414 MipsGOTParser<ELFT> Parser(Obj, ObjF->getFileName(), dynamic_table(),
2415 dynamic_symbols());
2416 if (Parser.hasGot())
2417 ELFDumperStyle->printMipsGOT(Parser);
2418 if (Parser.hasPlt())
2419 ELFDumperStyle->printMipsPLT(Parser);
2422 static const EnumEntry<unsigned> ElfMipsISAExtType[] = {
2423 {"None", Mips::AFL_EXT_NONE},
2424 {"Broadcom SB-1", Mips::AFL_EXT_SB1},
2425 {"Cavium Networks Octeon", Mips::AFL_EXT_OCTEON},
2426 {"Cavium Networks Octeon2", Mips::AFL_EXT_OCTEON2},
2427 {"Cavium Networks OcteonP", Mips::AFL_EXT_OCTEONP},
2428 {"Cavium Networks Octeon3", Mips::AFL_EXT_OCTEON3},
2429 {"LSI R4010", Mips::AFL_EXT_4010},
2430 {"Loongson 2E", Mips::AFL_EXT_LOONGSON_2E},
2431 {"Loongson 2F", Mips::AFL_EXT_LOONGSON_2F},
2432 {"Loongson 3A", Mips::AFL_EXT_LOONGSON_3A},
2433 {"MIPS R4650", Mips::AFL_EXT_4650},
2434 {"MIPS R5900", Mips::AFL_EXT_5900},
2435 {"MIPS R10000", Mips::AFL_EXT_10000},
2436 {"NEC VR4100", Mips::AFL_EXT_4100},
2437 {"NEC VR4111/VR4181", Mips::AFL_EXT_4111},
2438 {"NEC VR4120", Mips::AFL_EXT_4120},
2439 {"NEC VR5400", Mips::AFL_EXT_5400},
2440 {"NEC VR5500", Mips::AFL_EXT_5500},
2441 {"RMI Xlr", Mips::AFL_EXT_XLR},
2442 {"Toshiba R3900", Mips::AFL_EXT_3900}
2445 static const EnumEntry<unsigned> ElfMipsASEFlags[] = {
2446 {"DSP", Mips::AFL_ASE_DSP},
2447 {"DSPR2", Mips::AFL_ASE_DSPR2},
2448 {"Enhanced VA Scheme", Mips::AFL_ASE_EVA},
2449 {"MCU", Mips::AFL_ASE_MCU},
2450 {"MDMX", Mips::AFL_ASE_MDMX},
2451 {"MIPS-3D", Mips::AFL_ASE_MIPS3D},
2452 {"MT", Mips::AFL_ASE_MT},
2453 {"SmartMIPS", Mips::AFL_ASE_SMARTMIPS},
2454 {"VZ", Mips::AFL_ASE_VIRT},
2455 {"MSA", Mips::AFL_ASE_MSA},
2456 {"MIPS16", Mips::AFL_ASE_MIPS16},
2457 {"microMIPS", Mips::AFL_ASE_MICROMIPS},
2458 {"XPA", Mips::AFL_ASE_XPA},
2459 {"CRC", Mips::AFL_ASE_CRC},
2460 {"GINV", Mips::AFL_ASE_GINV},
2463 static const EnumEntry<unsigned> ElfMipsFpABIType[] = {
2464 {"Hard or soft float", Mips::Val_GNU_MIPS_ABI_FP_ANY},
2465 {"Hard float (double precision)", Mips::Val_GNU_MIPS_ABI_FP_DOUBLE},
2466 {"Hard float (single precision)", Mips::Val_GNU_MIPS_ABI_FP_SINGLE},
2467 {"Soft float", Mips::Val_GNU_MIPS_ABI_FP_SOFT},
2468 {"Hard float (MIPS32r2 64-bit FPU 12 callee-saved)",
2469 Mips::Val_GNU_MIPS_ABI_FP_OLD_64},
2470 {"Hard float (32-bit CPU, Any FPU)", Mips::Val_GNU_MIPS_ABI_FP_XX},
2471 {"Hard float (32-bit CPU, 64-bit FPU)", Mips::Val_GNU_MIPS_ABI_FP_64},
2472 {"Hard float compat (32-bit CPU, 64-bit FPU)",
2473 Mips::Val_GNU_MIPS_ABI_FP_64A}
2476 static const EnumEntry<unsigned> ElfMipsFlags1[] {
2477 {"ODDSPREG", Mips::AFL_FLAGS1_ODDSPREG},
2480 static int getMipsRegisterSize(uint8_t Flag) {
2481 switch (Flag) {
2482 case Mips::AFL_REG_NONE:
2483 return 0;
2484 case Mips::AFL_REG_32:
2485 return 32;
2486 case Mips::AFL_REG_64:
2487 return 64;
2488 case Mips::AFL_REG_128:
2489 return 128;
2490 default:
2491 return -1;
2495 template <class ELFT> void ELFDumper<ELFT>::printMipsABIFlags() {
2496 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
2497 const Elf_Shdr *Shdr =
2498 findSectionByName(*Obj, ObjF->getFileName(), ".MIPS.abiflags");
2499 if (!Shdr) {
2500 W.startLine() << "There is no .MIPS.abiflags section in the file.\n";
2501 return;
2503 ArrayRef<uint8_t> Sec =
2504 unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(Shdr));
2505 if (Sec.size() != sizeof(Elf_Mips_ABIFlags<ELFT>)) {
2506 W.startLine() << "The .MIPS.abiflags section has a wrong size.\n";
2507 return;
2510 auto *Flags = reinterpret_cast<const Elf_Mips_ABIFlags<ELFT> *>(Sec.data());
2512 raw_ostream &OS = W.getOStream();
2513 DictScope GS(W, "MIPS ABI Flags");
2515 W.printNumber("Version", Flags->version);
2516 W.startLine() << "ISA: ";
2517 if (Flags->isa_rev <= 1)
2518 OS << format("MIPS%u", Flags->isa_level);
2519 else
2520 OS << format("MIPS%ur%u", Flags->isa_level, Flags->isa_rev);
2521 OS << "\n";
2522 W.printEnum("ISA Extension", Flags->isa_ext, makeArrayRef(ElfMipsISAExtType));
2523 W.printFlags("ASEs", Flags->ases, makeArrayRef(ElfMipsASEFlags));
2524 W.printEnum("FP ABI", Flags->fp_abi, makeArrayRef(ElfMipsFpABIType));
2525 W.printNumber("GPR size", getMipsRegisterSize(Flags->gpr_size));
2526 W.printNumber("CPR1 size", getMipsRegisterSize(Flags->cpr1_size));
2527 W.printNumber("CPR2 size", getMipsRegisterSize(Flags->cpr2_size));
2528 W.printFlags("Flags 1", Flags->flags1, makeArrayRef(ElfMipsFlags1));
2529 W.printHex("Flags 2", Flags->flags2);
2532 template <class ELFT>
2533 static void printMipsReginfoData(ScopedPrinter &W,
2534 const Elf_Mips_RegInfo<ELFT> &Reginfo) {
2535 W.printHex("GP", Reginfo.ri_gp_value);
2536 W.printHex("General Mask", Reginfo.ri_gprmask);
2537 W.printHex("Co-Proc Mask0", Reginfo.ri_cprmask[0]);
2538 W.printHex("Co-Proc Mask1", Reginfo.ri_cprmask[1]);
2539 W.printHex("Co-Proc Mask2", Reginfo.ri_cprmask[2]);
2540 W.printHex("Co-Proc Mask3", Reginfo.ri_cprmask[3]);
2543 template <class ELFT> void ELFDumper<ELFT>::printMipsReginfo() {
2544 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
2545 const Elf_Shdr *Shdr = findSectionByName(*Obj, ObjF->getFileName(), ".reginfo");
2546 if (!Shdr) {
2547 W.startLine() << "There is no .reginfo section in the file.\n";
2548 return;
2550 ArrayRef<uint8_t> Sec =
2551 unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(Shdr));
2552 if (Sec.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) {
2553 W.startLine() << "The .reginfo section has a wrong size.\n";
2554 return;
2557 DictScope GS(W, "MIPS RegInfo");
2558 auto *Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(Sec.data());
2559 printMipsReginfoData(W, *Reginfo);
2562 template <class ELFT> void ELFDumper<ELFT>::printMipsOptions() {
2563 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
2564 const Elf_Shdr *Shdr =
2565 findSectionByName(*Obj, ObjF->getFileName(), ".MIPS.options");
2566 if (!Shdr) {
2567 W.startLine() << "There is no .MIPS.options section in the file.\n";
2568 return;
2571 DictScope GS(W, "MIPS Options");
2573 ArrayRef<uint8_t> Sec =
2574 unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(Shdr));
2575 while (!Sec.empty()) {
2576 if (Sec.size() < sizeof(Elf_Mips_Options<ELFT>)) {
2577 W.startLine() << "The .MIPS.options section has a wrong size.\n";
2578 return;
2580 auto *O = reinterpret_cast<const Elf_Mips_Options<ELFT> *>(Sec.data());
2581 DictScope GS(W, getElfMipsOptionsOdkType(O->kind));
2582 switch (O->kind) {
2583 case ODK_REGINFO:
2584 printMipsReginfoData(W, O->getRegInfo());
2585 break;
2586 default:
2587 W.startLine() << "Unsupported MIPS options tag.\n";
2588 break;
2590 Sec = Sec.slice(O->size);
2594 template <class ELFT> void ELFDumper<ELFT>::printStackMap() const {
2595 const ELFFile<ELFT> *Obj = ObjF->getELFFile();
2596 const Elf_Shdr *StackMapSection = nullptr;
2597 for (const auto &Sec : unwrapOrError(ObjF->getFileName(), Obj->sections())) {
2598 StringRef Name =
2599 unwrapOrError(ObjF->getFileName(), Obj->getSectionName(&Sec));
2600 if (Name == ".llvm_stackmaps") {
2601 StackMapSection = &Sec;
2602 break;
2606 if (!StackMapSection)
2607 return;
2609 ArrayRef<uint8_t> StackMapContentsArray = unwrapOrError(
2610 ObjF->getFileName(), Obj->getSectionContents(StackMapSection));
2612 prettyPrintStackMap(
2613 W, StackMapParser<ELFT::TargetEndianness>(StackMapContentsArray));
2616 template <class ELFT> void ELFDumper<ELFT>::printGroupSections() {
2617 ELFDumperStyle->printGroupSections(ObjF->getELFFile());
2620 template <class ELFT> void ELFDumper<ELFT>::printAddrsig() {
2621 ELFDumperStyle->printAddrsig(ObjF->getELFFile());
2624 static inline void printFields(formatted_raw_ostream &OS, StringRef Str1,
2625 StringRef Str2) {
2626 OS.PadToColumn(2u);
2627 OS << Str1;
2628 OS.PadToColumn(37u);
2629 OS << Str2 << "\n";
2630 OS.flush();
2633 template <class ELFT>
2634 static std::string getSectionHeadersNumString(const ELFFile<ELFT> *Obj,
2635 StringRef FileName) {
2636 const typename ELFT::Ehdr *ElfHeader = Obj->getHeader();
2637 if (ElfHeader->e_shnum != 0)
2638 return to_string(ElfHeader->e_shnum);
2640 ArrayRef<typename ELFT::Shdr> Arr = unwrapOrError(FileName, Obj->sections());
2641 if (Arr.empty())
2642 return "0";
2643 return "0 (" + to_string(Arr[0].sh_size) + ")";
2646 template <class ELFT>
2647 static std::string getSectionHeaderTableIndexString(const ELFFile<ELFT> *Obj,
2648 StringRef FileName) {
2649 const typename ELFT::Ehdr *ElfHeader = Obj->getHeader();
2650 if (ElfHeader->e_shstrndx != SHN_XINDEX)
2651 return to_string(ElfHeader->e_shstrndx);
2653 ArrayRef<typename ELFT::Shdr> Arr = unwrapOrError(FileName, Obj->sections());
2654 if (Arr.empty())
2655 return "65535 (corrupt: out of range)";
2656 return to_string(ElfHeader->e_shstrndx) + " (" + to_string(Arr[0].sh_link) +
2657 ")";
2660 template <class ELFT> void GNUStyle<ELFT>::printFileHeaders(const ELFO *Obj) {
2661 const Elf_Ehdr *e = Obj->getHeader();
2662 OS << "ELF Header:\n";
2663 OS << " Magic: ";
2664 std::string Str;
2665 for (int i = 0; i < ELF::EI_NIDENT; i++)
2666 OS << format(" %02x", static_cast<int>(e->e_ident[i]));
2667 OS << "\n";
2668 Str = printEnum(e->e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
2669 printFields(OS, "Class:", Str);
2670 Str = printEnum(e->e_ident[ELF::EI_DATA], makeArrayRef(ElfDataEncoding));
2671 printFields(OS, "Data:", Str);
2672 OS.PadToColumn(2u);
2673 OS << "Version:";
2674 OS.PadToColumn(37u);
2675 OS << to_hexString(e->e_ident[ELF::EI_VERSION]);
2676 if (e->e_version == ELF::EV_CURRENT)
2677 OS << " (current)";
2678 OS << "\n";
2679 Str = printEnum(e->e_ident[ELF::EI_OSABI], makeArrayRef(ElfOSABI));
2680 printFields(OS, "OS/ABI:", Str);
2681 Str = "0x" + to_hexString(e->e_ident[ELF::EI_ABIVERSION]);
2682 printFields(OS, "ABI Version:", Str);
2683 Str = printEnum(e->e_type, makeArrayRef(ElfObjectFileType));
2684 printFields(OS, "Type:", Str);
2685 Str = printEnum(e->e_machine, makeArrayRef(ElfMachineType));
2686 printFields(OS, "Machine:", Str);
2687 Str = "0x" + to_hexString(e->e_version);
2688 printFields(OS, "Version:", Str);
2689 Str = "0x" + to_hexString(e->e_entry);
2690 printFields(OS, "Entry point address:", Str);
2691 Str = to_string(e->e_phoff) + " (bytes into file)";
2692 printFields(OS, "Start of program headers:", Str);
2693 Str = to_string(e->e_shoff) + " (bytes into file)";
2694 printFields(OS, "Start of section headers:", Str);
2695 std::string ElfFlags;
2696 if (e->e_machine == EM_MIPS)
2697 ElfFlags =
2698 printFlags(e->e_flags, makeArrayRef(ElfHeaderMipsFlags),
2699 unsigned(ELF::EF_MIPS_ARCH), unsigned(ELF::EF_MIPS_ABI),
2700 unsigned(ELF::EF_MIPS_MACH));
2701 else if (e->e_machine == EM_RISCV)
2702 ElfFlags = printFlags(e->e_flags, makeArrayRef(ElfHeaderRISCVFlags));
2703 Str = "0x" + to_hexString(e->e_flags);
2704 if (!ElfFlags.empty())
2705 Str = Str + ", " + ElfFlags;
2706 printFields(OS, "Flags:", Str);
2707 Str = to_string(e->e_ehsize) + " (bytes)";
2708 printFields(OS, "Size of this header:", Str);
2709 Str = to_string(e->e_phentsize) + " (bytes)";
2710 printFields(OS, "Size of program headers:", Str);
2711 Str = to_string(e->e_phnum);
2712 printFields(OS, "Number of program headers:", Str);
2713 Str = to_string(e->e_shentsize) + " (bytes)";
2714 printFields(OS, "Size of section headers:", Str);
2715 Str = getSectionHeadersNumString(Obj, this->FileName);
2716 printFields(OS, "Number of section headers:", Str);
2717 Str = getSectionHeaderTableIndexString(Obj, this->FileName);
2718 printFields(OS, "Section header string table index:", Str);
2721 namespace {
2722 struct GroupMember {
2723 StringRef Name;
2724 uint64_t Index;
2727 struct GroupSection {
2728 StringRef Name;
2729 std::string Signature;
2730 uint64_t ShName;
2731 uint64_t Index;
2732 uint32_t Link;
2733 uint32_t Info;
2734 uint32_t Type;
2735 std::vector<GroupMember> Members;
2738 template <class ELFT>
2739 std::vector<GroupSection> getGroups(const ELFFile<ELFT> *Obj,
2740 StringRef FileName) {
2741 using Elf_Shdr = typename ELFT::Shdr;
2742 using Elf_Sym = typename ELFT::Sym;
2743 using Elf_Word = typename ELFT::Word;
2745 std::vector<GroupSection> Ret;
2746 uint64_t I = 0;
2747 for (const Elf_Shdr &Sec : unwrapOrError(FileName, Obj->sections())) {
2748 ++I;
2749 if (Sec.sh_type != ELF::SHT_GROUP)
2750 continue;
2752 const Elf_Shdr *Symtab =
2753 unwrapOrError(FileName, Obj->getSection(Sec.sh_link));
2754 StringRef StrTable =
2755 unwrapOrError(FileName, Obj->getStringTableForSymtab(*Symtab));
2756 const Elf_Sym *Sym = unwrapOrError(
2757 FileName, Obj->template getEntry<Elf_Sym>(Symtab, Sec.sh_info));
2758 auto Data = unwrapOrError(
2759 FileName, Obj->template getSectionContentsAsArray<Elf_Word>(&Sec));
2761 StringRef Name = unwrapOrError(FileName, Obj->getSectionName(&Sec));
2762 StringRef Signature = StrTable.data() + Sym->st_name;
2763 Ret.push_back({Name,
2764 maybeDemangle(Signature),
2765 Sec.sh_name,
2766 I - 1,
2767 Sec.sh_link,
2768 Sec.sh_info,
2769 Data[0],
2770 {}});
2772 std::vector<GroupMember> &GM = Ret.back().Members;
2773 for (uint32_t Ndx : Data.slice(1)) {
2774 auto Sec = unwrapOrError(FileName, Obj->getSection(Ndx));
2775 const StringRef Name = unwrapOrError(FileName, Obj->getSectionName(Sec));
2776 GM.push_back({Name, Ndx});
2779 return Ret;
2782 DenseMap<uint64_t, const GroupSection *>
2783 mapSectionsToGroups(ArrayRef<GroupSection> Groups) {
2784 DenseMap<uint64_t, const GroupSection *> Ret;
2785 for (const GroupSection &G : Groups)
2786 for (const GroupMember &GM : G.Members)
2787 Ret.insert({GM.Index, &G});
2788 return Ret;
2791 } // namespace
2793 template <class ELFT> void GNUStyle<ELFT>::printGroupSections(const ELFO *Obj) {
2794 std::vector<GroupSection> V = getGroups<ELFT>(Obj, this->FileName);
2795 DenseMap<uint64_t, const GroupSection *> Map = mapSectionsToGroups(V);
2796 for (const GroupSection &G : V) {
2797 OS << "\n"
2798 << getGroupType(G.Type) << " group section ["
2799 << format_decimal(G.Index, 5) << "] `" << G.Name << "' [" << G.Signature
2800 << "] contains " << G.Members.size() << " sections:\n"
2801 << " [Index] Name\n";
2802 for (const GroupMember &GM : G.Members) {
2803 const GroupSection *MainGroup = Map[GM.Index];
2804 if (MainGroup != &G) {
2805 OS.flush();
2806 errs() << "Error: section [" << format_decimal(GM.Index, 5)
2807 << "] in group section [" << format_decimal(G.Index, 5)
2808 << "] already in group section ["
2809 << format_decimal(MainGroup->Index, 5) << "]";
2810 errs().flush();
2811 continue;
2813 OS << " [" << format_decimal(GM.Index, 5) << "] " << GM.Name << "\n";
2817 if (V.empty())
2818 OS << "There are no section groups in this file.\n";
2821 template <class ELFT>
2822 void GNUStyle<ELFT>::printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab,
2823 const Elf_Rela &R, bool IsRela) {
2824 const Elf_Sym *Sym =
2825 unwrapOrError(this->FileName, Obj->getRelocationSymbol(&R, SymTab));
2826 std::string TargetName;
2827 if (Sym && Sym->getType() == ELF::STT_SECTION) {
2828 const Elf_Shdr *Sec = unwrapOrError(
2829 this->FileName,
2830 Obj->getSection(Sym, SymTab, this->dumper()->getShndxTable()));
2831 TargetName = unwrapOrError(this->FileName, Obj->getSectionName(Sec));
2832 } else if (Sym) {
2833 StringRef StrTable =
2834 unwrapOrError(this->FileName, Obj->getStringTableForSymtab(*SymTab));
2835 TargetName = this->dumper()->getFullSymbolName(
2836 Sym, StrTable, SymTab->sh_type == SHT_DYNSYM /* IsDynamic */);
2838 printRelocation(Obj, Sym, TargetName, R, IsRela);
2841 template <class ELFT>
2842 void GNUStyle<ELFT>::printRelocation(const ELFO *Obj, const Elf_Sym *Sym,
2843 StringRef SymbolName, const Elf_Rela &R,
2844 bool IsRela) {
2845 // First two fields are bit width dependent. The rest of them are fixed width.
2846 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
2847 Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias};
2848 unsigned Width = ELFT::Is64Bits ? 16 : 8;
2850 Fields[0].Str = to_string(format_hex_no_prefix(R.r_offset, Width));
2851 Fields[1].Str = to_string(format_hex_no_prefix(R.r_info, Width));
2853 SmallString<32> RelocName;
2854 Obj->getRelocationTypeName(R.getType(Obj->isMips64EL()), RelocName);
2855 Fields[2].Str = RelocName.c_str();
2857 if (Sym && (!SymbolName.empty() || Sym->getValue() != 0))
2858 Fields[3].Str = to_string(format_hex_no_prefix(Sym->getValue(), Width));
2860 Fields[4].Str = SymbolName;
2861 for (const Field &F : Fields)
2862 printField(F);
2864 std::string Addend;
2865 if (IsRela) {
2866 int64_t RelAddend = R.r_addend;
2867 if (!SymbolName.empty()) {
2868 if (R.r_addend < 0) {
2869 Addend = " - ";
2870 RelAddend = std::abs(RelAddend);
2871 } else
2872 Addend = " + ";
2875 Addend += to_hexString(RelAddend, false);
2877 OS << Addend << "\n";
2880 template <class ELFT> void GNUStyle<ELFT>::printRelocHeader(unsigned SType) {
2881 bool IsRela = SType == ELF::SHT_RELA || SType == ELF::SHT_ANDROID_RELA;
2882 bool IsRelr = SType == ELF::SHT_RELR || SType == ELF::SHT_ANDROID_RELR;
2883 if (ELFT::Is64Bits)
2884 OS << " ";
2885 else
2886 OS << " ";
2887 if (IsRelr && opts::RawRelr)
2888 OS << "Data ";
2889 else
2890 OS << "Offset";
2891 if (ELFT::Is64Bits)
2892 OS << " Info Type"
2893 << " Symbol's Value Symbol's Name";
2894 else
2895 OS << " Info Type Sym. Value Symbol's Name";
2896 if (IsRela)
2897 OS << " + Addend";
2898 OS << "\n";
2901 template <class ELFT> void GNUStyle<ELFT>::printRelocations(const ELFO *Obj) {
2902 bool HasRelocSections = false;
2903 for (const Elf_Shdr &Sec : unwrapOrError(this->FileName, Obj->sections())) {
2904 if (Sec.sh_type != ELF::SHT_REL && Sec.sh_type != ELF::SHT_RELA &&
2905 Sec.sh_type != ELF::SHT_RELR && Sec.sh_type != ELF::SHT_ANDROID_REL &&
2906 Sec.sh_type != ELF::SHT_ANDROID_RELA &&
2907 Sec.sh_type != ELF::SHT_ANDROID_RELR)
2908 continue;
2909 HasRelocSections = true;
2910 StringRef Name = unwrapOrError(this->FileName, Obj->getSectionName(&Sec));
2911 unsigned Entries = Sec.getEntityCount();
2912 std::vector<Elf_Rela> AndroidRelas;
2913 if (Sec.sh_type == ELF::SHT_ANDROID_REL ||
2914 Sec.sh_type == ELF::SHT_ANDROID_RELA) {
2915 // Android's packed relocation section needs to be unpacked first
2916 // to get the actual number of entries.
2917 AndroidRelas = unwrapOrError(this->FileName, Obj->android_relas(&Sec));
2918 Entries = AndroidRelas.size();
2920 std::vector<Elf_Rela> RelrRelas;
2921 if (!opts::RawRelr && (Sec.sh_type == ELF::SHT_RELR ||
2922 Sec.sh_type == ELF::SHT_ANDROID_RELR)) {
2923 // .relr.dyn relative relocation section needs to be unpacked first
2924 // to get the actual number of entries.
2925 Elf_Relr_Range Relrs = unwrapOrError(this->FileName, Obj->relrs(&Sec));
2926 RelrRelas = unwrapOrError(this->FileName, Obj->decode_relrs(Relrs));
2927 Entries = RelrRelas.size();
2929 uintX_t Offset = Sec.sh_offset;
2930 OS << "\nRelocation section '" << Name << "' at offset 0x"
2931 << to_hexString(Offset, false) << " contains " << Entries
2932 << " entries:\n";
2933 printRelocHeader(Sec.sh_type);
2934 const Elf_Shdr *SymTab =
2935 unwrapOrError(this->FileName, Obj->getSection(Sec.sh_link));
2936 switch (Sec.sh_type) {
2937 case ELF::SHT_REL:
2938 for (const auto &R : unwrapOrError(this->FileName, Obj->rels(&Sec))) {
2939 Elf_Rela Rela;
2940 Rela.r_offset = R.r_offset;
2941 Rela.r_info = R.r_info;
2942 Rela.r_addend = 0;
2943 printRelocation(Obj, SymTab, Rela, false);
2945 break;
2946 case ELF::SHT_RELA:
2947 for (const auto &R : unwrapOrError(this->FileName, Obj->relas(&Sec)))
2948 printRelocation(Obj, SymTab, R, true);
2949 break;
2950 case ELF::SHT_RELR:
2951 case ELF::SHT_ANDROID_RELR:
2952 if (opts::RawRelr)
2953 for (const auto &R : unwrapOrError(this->FileName, Obj->relrs(&Sec)))
2954 OS << to_string(format_hex_no_prefix(R, ELFT::Is64Bits ? 16 : 8))
2955 << "\n";
2956 else
2957 for (const auto &R : RelrRelas)
2958 printRelocation(Obj, SymTab, R, false);
2959 break;
2960 case ELF::SHT_ANDROID_REL:
2961 case ELF::SHT_ANDROID_RELA:
2962 for (const auto &R : AndroidRelas)
2963 printRelocation(Obj, SymTab, R, Sec.sh_type == ELF::SHT_ANDROID_RELA);
2964 break;
2967 if (!HasRelocSections)
2968 OS << "\nThere are no relocations in this file.\n";
2971 // Print the offset of a particular section from anyone of the ranges:
2972 // [SHT_LOOS, SHT_HIOS], [SHT_LOPROC, SHT_HIPROC], [SHT_LOUSER, SHT_HIUSER].
2973 // If 'Type' does not fall within any of those ranges, then a string is
2974 // returned as '<unknown>' followed by the type value.
2975 static std::string getSectionTypeOffsetString(unsigned Type) {
2976 if (Type >= SHT_LOOS && Type <= SHT_HIOS)
2977 return "LOOS+0x" + to_hexString(Type - SHT_LOOS);
2978 else if (Type >= SHT_LOPROC && Type <= SHT_HIPROC)
2979 return "LOPROC+0x" + to_hexString(Type - SHT_LOPROC);
2980 else if (Type >= SHT_LOUSER && Type <= SHT_HIUSER)
2981 return "LOUSER+0x" + to_hexString(Type - SHT_LOUSER);
2982 return "0x" + to_hexString(Type) + ": <unknown>";
2985 static std::string getSectionTypeString(unsigned Arch, unsigned Type) {
2986 using namespace ELF;
2988 switch (Arch) {
2989 case EM_ARM:
2990 switch (Type) {
2991 case SHT_ARM_EXIDX:
2992 return "ARM_EXIDX";
2993 case SHT_ARM_PREEMPTMAP:
2994 return "ARM_PREEMPTMAP";
2995 case SHT_ARM_ATTRIBUTES:
2996 return "ARM_ATTRIBUTES";
2997 case SHT_ARM_DEBUGOVERLAY:
2998 return "ARM_DEBUGOVERLAY";
2999 case SHT_ARM_OVERLAYSECTION:
3000 return "ARM_OVERLAYSECTION";
3002 break;
3003 case EM_X86_64:
3004 switch (Type) {
3005 case SHT_X86_64_UNWIND:
3006 return "X86_64_UNWIND";
3008 break;
3009 case EM_MIPS:
3010 case EM_MIPS_RS3_LE:
3011 switch (Type) {
3012 case SHT_MIPS_REGINFO:
3013 return "MIPS_REGINFO";
3014 case SHT_MIPS_OPTIONS:
3015 return "MIPS_OPTIONS";
3016 case SHT_MIPS_DWARF:
3017 return "MIPS_DWARF";
3018 case SHT_MIPS_ABIFLAGS:
3019 return "MIPS_ABIFLAGS";
3021 break;
3023 switch (Type) {
3024 case SHT_NULL:
3025 return "NULL";
3026 case SHT_PROGBITS:
3027 return "PROGBITS";
3028 case SHT_SYMTAB:
3029 return "SYMTAB";
3030 case SHT_STRTAB:
3031 return "STRTAB";
3032 case SHT_RELA:
3033 return "RELA";
3034 case SHT_HASH:
3035 return "HASH";
3036 case SHT_DYNAMIC:
3037 return "DYNAMIC";
3038 case SHT_NOTE:
3039 return "NOTE";
3040 case SHT_NOBITS:
3041 return "NOBITS";
3042 case SHT_REL:
3043 return "REL";
3044 case SHT_SHLIB:
3045 return "SHLIB";
3046 case SHT_DYNSYM:
3047 return "DYNSYM";
3048 case SHT_INIT_ARRAY:
3049 return "INIT_ARRAY";
3050 case SHT_FINI_ARRAY:
3051 return "FINI_ARRAY";
3052 case SHT_PREINIT_ARRAY:
3053 return "PREINIT_ARRAY";
3054 case SHT_GROUP:
3055 return "GROUP";
3056 case SHT_SYMTAB_SHNDX:
3057 return "SYMTAB SECTION INDICES";
3058 case SHT_ANDROID_REL:
3059 return "ANDROID_REL";
3060 case SHT_ANDROID_RELA:
3061 return "ANDROID_RELA";
3062 case SHT_RELR:
3063 case SHT_ANDROID_RELR:
3064 return "RELR";
3065 case SHT_LLVM_ODRTAB:
3066 return "LLVM_ODRTAB";
3067 case SHT_LLVM_LINKER_OPTIONS:
3068 return "LLVM_LINKER_OPTIONS";
3069 case SHT_LLVM_CALL_GRAPH_PROFILE:
3070 return "LLVM_CALL_GRAPH_PROFILE";
3071 case SHT_LLVM_ADDRSIG:
3072 return "LLVM_ADDRSIG";
3073 case SHT_LLVM_DEPENDENT_LIBRARIES:
3074 return "LLVM_DEPENDENT_LIBRARIES";
3075 // FIXME: Parse processor specific GNU attributes
3076 case SHT_GNU_ATTRIBUTES:
3077 return "ATTRIBUTES";
3078 case SHT_GNU_HASH:
3079 return "GNU_HASH";
3080 case SHT_GNU_verdef:
3081 return "VERDEF";
3082 case SHT_GNU_verneed:
3083 return "VERNEED";
3084 case SHT_GNU_versym:
3085 return "VERSYM";
3086 default:
3087 return getSectionTypeOffsetString(Type);
3089 return "";
3092 template <class ELFT>
3093 void GNUStyle<ELFT>::printSectionHeaders(const ELFO *Obj) {
3094 unsigned Bias = ELFT::Is64Bits ? 0 : 8;
3095 ArrayRef<Elf_Shdr> Sections = unwrapOrError(this->FileName, Obj->sections());
3096 OS << "There are " << to_string(Sections.size())
3097 << " section headers, starting at offset "
3098 << "0x" << to_hexString(Obj->getHeader()->e_shoff, false) << ":\n\n";
3099 OS << "Section Headers:\n";
3100 Field Fields[11] = {
3101 {"[Nr]", 2}, {"Name", 7}, {"Type", 25},
3102 {"Address", 41}, {"Off", 58 - Bias}, {"Size", 65 - Bias},
3103 {"ES", 72 - Bias}, {"Flg", 75 - Bias}, {"Lk", 79 - Bias},
3104 {"Inf", 82 - Bias}, {"Al", 86 - Bias}};
3105 for (auto &F : Fields)
3106 printField(F);
3107 OS << "\n";
3109 const ELFObjectFile<ELFT> *ElfObj = this->dumper()->getElfObject();
3110 size_t SectionIndex = 0;
3111 for (const Elf_Shdr &Sec : Sections) {
3112 Fields[0].Str = to_string(SectionIndex);
3113 Fields[1].Str = unwrapOrError<StringRef>(
3114 ElfObj->getFileName(), Obj->getSectionName(&Sec, this->WarningHandler));
3115 Fields[2].Str =
3116 getSectionTypeString(Obj->getHeader()->e_machine, Sec.sh_type);
3117 Fields[3].Str =
3118 to_string(format_hex_no_prefix(Sec.sh_addr, ELFT::Is64Bits ? 16 : 8));
3119 Fields[4].Str = to_string(format_hex_no_prefix(Sec.sh_offset, 6));
3120 Fields[5].Str = to_string(format_hex_no_prefix(Sec.sh_size, 6));
3121 Fields[6].Str = to_string(format_hex_no_prefix(Sec.sh_entsize, 2));
3122 Fields[7].Str = getGNUFlags(Sec.sh_flags);
3123 Fields[8].Str = to_string(Sec.sh_link);
3124 Fields[9].Str = to_string(Sec.sh_info);
3125 Fields[10].Str = to_string(Sec.sh_addralign);
3127 OS.PadToColumn(Fields[0].Column);
3128 OS << "[" << right_justify(Fields[0].Str, 2) << "]";
3129 for (int i = 1; i < 7; i++)
3130 printField(Fields[i]);
3131 OS.PadToColumn(Fields[7].Column);
3132 OS << right_justify(Fields[7].Str, 3);
3133 OS.PadToColumn(Fields[8].Column);
3134 OS << right_justify(Fields[8].Str, 2);
3135 OS.PadToColumn(Fields[9].Column);
3136 OS << right_justify(Fields[9].Str, 3);
3137 OS.PadToColumn(Fields[10].Column);
3138 OS << right_justify(Fields[10].Str, 2);
3139 OS << "\n";
3140 ++SectionIndex;
3142 OS << "Key to Flags:\n"
3143 << " W (write), A (alloc), X (execute), M (merge), S (strings), l "
3144 "(large)\n"
3145 << " I (info), L (link order), G (group), T (TLS), E (exclude),\
3146 x (unknown)\n"
3147 << " O (extra OS processing required) o (OS specific),\
3148 p (processor specific)\n";
3151 template <class ELFT>
3152 void GNUStyle<ELFT>::printSymtabMessage(const ELFO *Obj, StringRef Name,
3153 size_t Entries) {
3154 if (!Name.empty())
3155 OS << "\nSymbol table '" << Name << "' contains " << Entries
3156 << " entries:\n";
3157 else
3158 OS << "\n Symbol table for image:\n";
3160 if (ELFT::Is64Bits)
3161 OS << " Num: Value Size Type Bind Vis Ndx Name\n";
3162 else
3163 OS << " Num: Value Size Type Bind Vis Ndx Name\n";
3166 template <class ELFT>
3167 std::string GNUStyle<ELFT>::getSymbolSectionNdx(const ELFO *Obj,
3168 const Elf_Sym *Symbol,
3169 const Elf_Sym *FirstSym) {
3170 unsigned SectionIndex = Symbol->st_shndx;
3171 switch (SectionIndex) {
3172 case ELF::SHN_UNDEF:
3173 return "UND";
3174 case ELF::SHN_ABS:
3175 return "ABS";
3176 case ELF::SHN_COMMON:
3177 return "COM";
3178 case ELF::SHN_XINDEX:
3179 return to_string(format_decimal(
3180 unwrapOrError(this->FileName,
3181 object::getExtendedSymbolTableIndex<ELFT>(
3182 Symbol, FirstSym, this->dumper()->getShndxTable())),
3183 3));
3184 default:
3185 // Find if:
3186 // Processor specific
3187 if (SectionIndex >= ELF::SHN_LOPROC && SectionIndex <= ELF::SHN_HIPROC)
3188 return std::string("PRC[0x") +
3189 to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3190 // OS specific
3191 if (SectionIndex >= ELF::SHN_LOOS && SectionIndex <= ELF::SHN_HIOS)
3192 return std::string("OS[0x") +
3193 to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3194 // Architecture reserved:
3195 if (SectionIndex >= ELF::SHN_LORESERVE &&
3196 SectionIndex <= ELF::SHN_HIRESERVE)
3197 return std::string("RSV[0x") +
3198 to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3199 // A normal section with an index
3200 return to_string(format_decimal(SectionIndex, 3));
3204 template <class ELFT>
3205 void GNUStyle<ELFT>::printSymbol(const ELFO *Obj, const Elf_Sym *Symbol,
3206 const Elf_Sym *FirstSym, StringRef StrTable,
3207 bool IsDynamic) {
3208 static int Idx = 0;
3209 static bool Dynamic = true;
3211 // If this function was called with a different value from IsDynamic
3212 // from last call, happens when we move from dynamic to static symbol
3213 // table, "Num" field should be reset.
3214 if (!Dynamic != !IsDynamic) {
3215 Idx = 0;
3216 Dynamic = false;
3219 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
3220 Field Fields[8] = {0, 8, 17 + Bias, 23 + Bias,
3221 31 + Bias, 38 + Bias, 47 + Bias, 51 + Bias};
3222 Fields[0].Str = to_string(format_decimal(Idx++, 6)) + ":";
3223 Fields[1].Str = to_string(
3224 format_hex_no_prefix(Symbol->st_value, ELFT::Is64Bits ? 16 : 8));
3225 Fields[2].Str = to_string(format_decimal(Symbol->st_size, 5));
3227 unsigned char SymbolType = Symbol->getType();
3228 if (Obj->getHeader()->e_machine == ELF::EM_AMDGPU &&
3229 SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
3230 Fields[3].Str = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
3231 else
3232 Fields[3].Str = printEnum(SymbolType, makeArrayRef(ElfSymbolTypes));
3234 Fields[4].Str =
3235 printEnum(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
3236 Fields[5].Str =
3237 printEnum(Symbol->getVisibility(), makeArrayRef(ElfSymbolVisibilities));
3238 Fields[6].Str = getSymbolSectionNdx(Obj, Symbol, FirstSym);
3239 Fields[7].Str =
3240 this->dumper()->getFullSymbolName(Symbol, StrTable, IsDynamic);
3241 for (auto &Entry : Fields)
3242 printField(Entry);
3243 OS << "\n";
3246 template <class ELFT>
3247 void GNUStyle<ELFT>::printHashedSymbol(const ELFO *Obj, const Elf_Sym *FirstSym,
3248 uint32_t Sym, StringRef StrTable,
3249 uint32_t Bucket) {
3250 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
3251 Field Fields[9] = {0, 6, 11, 20 + Bias, 25 + Bias,
3252 34 + Bias, 41 + Bias, 49 + Bias, 53 + Bias};
3253 Fields[0].Str = to_string(format_decimal(Sym, 5));
3254 Fields[1].Str = to_string(format_decimal(Bucket, 3)) + ":";
3256 const auto Symbol = FirstSym + Sym;
3257 Fields[2].Str = to_string(
3258 format_hex_no_prefix(Symbol->st_value, ELFT::Is64Bits ? 16 : 8));
3259 Fields[3].Str = to_string(format_decimal(Symbol->st_size, 5));
3261 unsigned char SymbolType = Symbol->getType();
3262 if (Obj->getHeader()->e_machine == ELF::EM_AMDGPU &&
3263 SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
3264 Fields[4].Str = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
3265 else
3266 Fields[4].Str = printEnum(SymbolType, makeArrayRef(ElfSymbolTypes));
3268 Fields[5].Str =
3269 printEnum(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
3270 Fields[6].Str =
3271 printEnum(Symbol->getVisibility(), makeArrayRef(ElfSymbolVisibilities));
3272 Fields[7].Str = getSymbolSectionNdx(Obj, Symbol, FirstSym);
3273 Fields[8].Str = this->dumper()->getFullSymbolName(Symbol, StrTable, true);
3275 for (auto &Entry : Fields)
3276 printField(Entry);
3277 OS << "\n";
3280 template <class ELFT>
3281 void GNUStyle<ELFT>::printSymbols(const ELFO *Obj, bool PrintSymbols,
3282 bool PrintDynamicSymbols) {
3283 if (!PrintSymbols && !PrintDynamicSymbols)
3284 return;
3285 // GNU readelf prints both the .dynsym and .symtab with --symbols.
3286 this->dumper()->printSymbolsHelper(true);
3287 if (PrintSymbols)
3288 this->dumper()->printSymbolsHelper(false);
3291 template <class ELFT> void GNUStyle<ELFT>::printHashSymbols(const ELFO *Obj) {
3292 if (this->dumper()->getDynamicStringTable().empty())
3293 return;
3294 auto StringTable = this->dumper()->getDynamicStringTable();
3295 auto DynSyms = this->dumper()->dynamic_symbols();
3297 // Try printing .hash
3298 if (auto SysVHash = this->dumper()->getHashTable()) {
3299 OS << "\n Symbol table of .hash for image:\n";
3300 if (ELFT::Is64Bits)
3301 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3302 else
3303 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3304 OS << "\n";
3306 auto Buckets = SysVHash->buckets();
3307 auto Chains = SysVHash->chains();
3308 for (uint32_t Buc = 0; Buc < SysVHash->nbucket; Buc++) {
3309 if (Buckets[Buc] == ELF::STN_UNDEF)
3310 continue;
3311 for (uint32_t Ch = Buckets[Buc]; Ch < SysVHash->nchain; Ch = Chains[Ch]) {
3312 if (Ch == ELF::STN_UNDEF)
3313 break;
3314 printHashedSymbol(Obj, &DynSyms[0], Ch, StringTable, Buc);
3319 // Try printing .gnu.hash
3320 if (auto GnuHash = this->dumper()->getGnuHashTable()) {
3321 OS << "\n Symbol table of .gnu.hash for image:\n";
3322 if (ELFT::Is64Bits)
3323 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3324 else
3325 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3326 OS << "\n";
3327 auto Buckets = GnuHash->buckets();
3328 for (uint32_t Buc = 0; Buc < GnuHash->nbuckets; Buc++) {
3329 if (Buckets[Buc] == ELF::STN_UNDEF)
3330 continue;
3331 uint32_t Index = Buckets[Buc];
3332 uint32_t GnuHashable = Index - GnuHash->symndx;
3333 // Print whole chain
3334 while (true) {
3335 printHashedSymbol(Obj, &DynSyms[0], Index++, StringTable, Buc);
3336 // Chain ends at symbol with stopper bit
3337 if ((GnuHash->values(DynSyms.size())[GnuHashable++] & 1) == 1)
3338 break;
3344 static inline std::string printPhdrFlags(unsigned Flag) {
3345 std::string Str;
3346 Str = (Flag & PF_R) ? "R" : " ";
3347 Str += (Flag & PF_W) ? "W" : " ";
3348 Str += (Flag & PF_X) ? "E" : " ";
3349 return Str;
3352 // SHF_TLS sections are only in PT_TLS, PT_LOAD or PT_GNU_RELRO
3353 // PT_TLS must only have SHF_TLS sections
3354 template <class ELFT>
3355 bool GNUStyle<ELFT>::checkTLSSections(const Elf_Phdr &Phdr,
3356 const Elf_Shdr &Sec) {
3357 return (((Sec.sh_flags & ELF::SHF_TLS) &&
3358 ((Phdr.p_type == ELF::PT_TLS) || (Phdr.p_type == ELF::PT_LOAD) ||
3359 (Phdr.p_type == ELF::PT_GNU_RELRO))) ||
3360 (!(Sec.sh_flags & ELF::SHF_TLS) && Phdr.p_type != ELF::PT_TLS));
3363 // Non-SHT_NOBITS must have its offset inside the segment
3364 // Only non-zero section can be at end of segment
3365 template <class ELFT>
3366 bool GNUStyle<ELFT>::checkoffsets(const Elf_Phdr &Phdr, const Elf_Shdr &Sec) {
3367 if (Sec.sh_type == ELF::SHT_NOBITS)
3368 return true;
3369 bool IsSpecial =
3370 (Sec.sh_type == ELF::SHT_NOBITS) && ((Sec.sh_flags & ELF::SHF_TLS) != 0);
3371 // .tbss is special, it only has memory in PT_TLS and has NOBITS properties
3372 auto SectionSize =
3373 (IsSpecial && Phdr.p_type != ELF::PT_TLS) ? 0 : Sec.sh_size;
3374 if (Sec.sh_offset >= Phdr.p_offset)
3375 return ((Sec.sh_offset + SectionSize <= Phdr.p_filesz + Phdr.p_offset)
3376 /*only non-zero sized sections at end*/
3377 && (Sec.sh_offset + 1 <= Phdr.p_offset + Phdr.p_filesz));
3378 return false;
3381 // SHF_ALLOC must have VMA inside segment
3382 // Only non-zero section can be at end of segment
3383 template <class ELFT>
3384 bool GNUStyle<ELFT>::checkVMA(const Elf_Phdr &Phdr, const Elf_Shdr &Sec) {
3385 if (!(Sec.sh_flags & ELF::SHF_ALLOC))
3386 return true;
3387 bool IsSpecial =
3388 (Sec.sh_type == ELF::SHT_NOBITS) && ((Sec.sh_flags & ELF::SHF_TLS) != 0);
3389 // .tbss is special, it only has memory in PT_TLS and has NOBITS properties
3390 auto SectionSize =
3391 (IsSpecial && Phdr.p_type != ELF::PT_TLS) ? 0 : Sec.sh_size;
3392 if (Sec.sh_addr >= Phdr.p_vaddr)
3393 return ((Sec.sh_addr + SectionSize <= Phdr.p_vaddr + Phdr.p_memsz) &&
3394 (Sec.sh_addr + 1 <= Phdr.p_vaddr + Phdr.p_memsz));
3395 return false;
3398 // No section with zero size must be at start or end of PT_DYNAMIC
3399 template <class ELFT>
3400 bool GNUStyle<ELFT>::checkPTDynamic(const Elf_Phdr &Phdr, const Elf_Shdr &Sec) {
3401 if (Phdr.p_type != ELF::PT_DYNAMIC || Sec.sh_size != 0 || Phdr.p_memsz == 0)
3402 return true;
3403 // Is section within the phdr both based on offset and VMA ?
3404 return ((Sec.sh_type == ELF::SHT_NOBITS) ||
3405 (Sec.sh_offset > Phdr.p_offset &&
3406 Sec.sh_offset < Phdr.p_offset + Phdr.p_filesz)) &&
3407 (!(Sec.sh_flags & ELF::SHF_ALLOC) ||
3408 (Sec.sh_addr > Phdr.p_vaddr && Sec.sh_addr < Phdr.p_memsz));
3411 template <class ELFT>
3412 void GNUStyle<ELFT>::printProgramHeaders(
3413 const ELFO *Obj, bool PrintProgramHeaders,
3414 cl::boolOrDefault PrintSectionMapping) {
3415 if (PrintProgramHeaders)
3416 printProgramHeaders(Obj);
3418 // Display the section mapping along with the program headers, unless
3419 // -section-mapping is explicitly set to false.
3420 if (PrintSectionMapping != cl::BOU_FALSE)
3421 printSectionMapping(Obj);
3424 template <class ELFT>
3425 void GNUStyle<ELFT>::printProgramHeaders(const ELFO *Obj) {
3426 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
3427 const Elf_Ehdr *Header = Obj->getHeader();
3428 Field Fields[8] = {2, 17, 26, 37 + Bias,
3429 48 + Bias, 56 + Bias, 64 + Bias, 68 + Bias};
3430 OS << "\nElf file type is "
3431 << printEnum(Header->e_type, makeArrayRef(ElfObjectFileType)) << "\n"
3432 << "Entry point " << format_hex(Header->e_entry, 3) << "\n"
3433 << "There are " << Header->e_phnum << " program headers,"
3434 << " starting at offset " << Header->e_phoff << "\n\n"
3435 << "Program Headers:\n";
3436 if (ELFT::Is64Bits)
3437 OS << " Type Offset VirtAddr PhysAddr "
3438 << " FileSiz MemSiz Flg Align\n";
3439 else
3440 OS << " Type Offset VirtAddr PhysAddr FileSiz "
3441 << "MemSiz Flg Align\n";
3443 unsigned Width = ELFT::Is64Bits ? 18 : 10;
3444 unsigned SizeWidth = ELFT::Is64Bits ? 8 : 7;
3445 for (const auto &Phdr :
3446 unwrapOrError(this->FileName, Obj->program_headers())) {
3447 Fields[0].Str = getElfPtType(Header->e_machine, Phdr.p_type);
3448 Fields[1].Str = to_string(format_hex(Phdr.p_offset, 8));
3449 Fields[2].Str = to_string(format_hex(Phdr.p_vaddr, Width));
3450 Fields[3].Str = to_string(format_hex(Phdr.p_paddr, Width));
3451 Fields[4].Str = to_string(format_hex(Phdr.p_filesz, SizeWidth));
3452 Fields[5].Str = to_string(format_hex(Phdr.p_memsz, SizeWidth));
3453 Fields[6].Str = printPhdrFlags(Phdr.p_flags);
3454 Fields[7].Str = to_string(format_hex(Phdr.p_align, 1));
3455 for (auto Field : Fields)
3456 printField(Field);
3457 if (Phdr.p_type == ELF::PT_INTERP) {
3458 OS << "\n [Requesting program interpreter: ";
3459 OS << reinterpret_cast<const char *>(Obj->base()) + Phdr.p_offset << "]";
3461 OS << "\n";
3465 template <class ELFT>
3466 void GNUStyle<ELFT>::printSectionMapping(const ELFO *Obj) {
3467 OS << "\n Section to Segment mapping:\n Segment Sections...\n";
3468 DenseSet<const Elf_Shdr *> BelongsToSegment;
3469 int Phnum = 0;
3470 for (const Elf_Phdr &Phdr :
3471 unwrapOrError(this->FileName, Obj->program_headers())) {
3472 std::string Sections;
3473 OS << format(" %2.2d ", Phnum++);
3474 for (const Elf_Shdr &Sec : unwrapOrError(this->FileName, Obj->sections())) {
3475 // Check if each section is in a segment and then print mapping.
3476 // readelf additionally makes sure it does not print zero sized sections
3477 // at end of segments and for PT_DYNAMIC both start and end of section
3478 // .tbss must only be shown in PT_TLS section.
3479 bool TbssInNonTLS = (Sec.sh_type == ELF::SHT_NOBITS) &&
3480 ((Sec.sh_flags & ELF::SHF_TLS) != 0) &&
3481 Phdr.p_type != ELF::PT_TLS;
3482 if (!TbssInNonTLS && checkTLSSections(Phdr, Sec) &&
3483 checkoffsets(Phdr, Sec) && checkVMA(Phdr, Sec) &&
3484 checkPTDynamic(Phdr, Sec) && (Sec.sh_type != ELF::SHT_NULL)) {
3485 Sections +=
3486 unwrapOrError(this->FileName, Obj->getSectionName(&Sec)).str() +
3487 " ";
3488 BelongsToSegment.insert(&Sec);
3491 OS << Sections << "\n";
3492 OS.flush();
3495 // Display sections that do not belong to a segment.
3496 std::string Sections;
3497 for (const Elf_Shdr &Sec : unwrapOrError(this->FileName, Obj->sections())) {
3498 if (BelongsToSegment.find(&Sec) == BelongsToSegment.end())
3499 Sections +=
3500 unwrapOrError(this->FileName, Obj->getSectionName(&Sec)).str() + ' ';
3502 if (!Sections.empty()) {
3503 OS << " None " << Sections << '\n';
3504 OS.flush();
3508 template <class ELFT>
3509 void GNUStyle<ELFT>::printDynamicRelocation(const ELFO *Obj, Elf_Rela R,
3510 bool IsRela) {
3511 uint32_t SymIndex = R.getSymbol(Obj->isMips64EL());
3512 const Elf_Sym *Sym = this->dumper()->dynamic_symbols().begin() + SymIndex;
3513 std::string SymbolName = maybeDemangle(unwrapOrError(
3514 this->FileName, Sym->getName(this->dumper()->getDynamicStringTable())));
3515 printRelocation(Obj, Sym, SymbolName, R, IsRela);
3518 template <class ELFT> void GNUStyle<ELFT>::printDynamic(const ELFO *Obj) {
3519 Elf_Dyn_Range Table = this->dumper()->dynamic_table();
3520 if (Table.empty())
3521 return;
3523 const DynRegionInfo &DynamicTableRegion =
3524 this->dumper()->getDynamicTableRegion();
3526 OS << "Dynamic section at offset "
3527 << format_hex(reinterpret_cast<const uint8_t *>(DynamicTableRegion.Addr) -
3528 Obj->base(),
3530 << " contains " << Table.size() << " entries:\n";
3532 bool Is64 = ELFT::Is64Bits;
3533 if (Is64)
3534 OS << " Tag Type Name/Value\n";
3535 else
3536 OS << " Tag Type Name/Value\n";
3537 for (auto Entry : Table) {
3538 uintX_t Tag = Entry.getTag();
3539 std::string TypeString = std::string("(") +
3540 getTypeString(Obj->getHeader()->e_machine, Tag) +
3541 ")";
3542 OS << " " << format_hex(Tag, Is64 ? 18 : 10)
3543 << format(" %-20s ", TypeString.c_str());
3544 this->dumper()->printDynamicEntry(OS, Tag, Entry.getVal());
3545 OS << "\n";
3549 template <class ELFT>
3550 void GNUStyle<ELFT>::printDynamicRelocations(const ELFO *Obj) {
3551 const DynRegionInfo &DynRelRegion = this->dumper()->getDynRelRegion();
3552 const DynRegionInfo &DynRelaRegion = this->dumper()->getDynRelaRegion();
3553 const DynRegionInfo &DynRelrRegion = this->dumper()->getDynRelrRegion();
3554 const DynRegionInfo &DynPLTRelRegion = this->dumper()->getDynPLTRelRegion();
3555 if (DynRelaRegion.Size > 0) {
3556 OS << "\n'RELA' relocation section at offset "
3557 << format_hex(reinterpret_cast<const uint8_t *>(DynRelaRegion.Addr) -
3558 Obj->base(),
3560 << " contains " << DynRelaRegion.Size << " bytes:\n";
3561 printRelocHeader(ELF::SHT_RELA);
3562 for (const Elf_Rela &Rela : this->dumper()->dyn_relas())
3563 printDynamicRelocation(Obj, Rela, true);
3565 if (DynRelRegion.Size > 0) {
3566 OS << "\n'REL' relocation section at offset "
3567 << format_hex(reinterpret_cast<const uint8_t *>(DynRelRegion.Addr) -
3568 Obj->base(),
3570 << " contains " << DynRelRegion.Size << " bytes:\n";
3571 printRelocHeader(ELF::SHT_REL);
3572 for (const Elf_Rel &Rel : this->dumper()->dyn_rels()) {
3573 Elf_Rela Rela;
3574 Rela.r_offset = Rel.r_offset;
3575 Rela.r_info = Rel.r_info;
3576 Rela.r_addend = 0;
3577 printDynamicRelocation(Obj, Rela, false);
3580 if (DynRelrRegion.Size > 0) {
3581 OS << "\n'RELR' relocation section at offset "
3582 << format_hex(reinterpret_cast<const uint8_t *>(DynRelrRegion.Addr) -
3583 Obj->base(),
3585 << " contains " << DynRelrRegion.Size << " bytes:\n";
3586 printRelocHeader(ELF::SHT_REL);
3587 Elf_Relr_Range Relrs = this->dumper()->dyn_relrs();
3588 std::vector<Elf_Rela> RelrRelas =
3589 unwrapOrError(this->FileName, Obj->decode_relrs(Relrs));
3590 for (const Elf_Rela &Rela : RelrRelas) {
3591 printDynamicRelocation(Obj, Rela, false);
3594 if (DynPLTRelRegion.Size) {
3595 OS << "\n'PLT' relocation section at offset "
3596 << format_hex(reinterpret_cast<const uint8_t *>(DynPLTRelRegion.Addr) -
3597 Obj->base(),
3599 << " contains " << DynPLTRelRegion.Size << " bytes:\n";
3601 if (DynPLTRelRegion.EntSize == sizeof(Elf_Rela)) {
3602 printRelocHeader(ELF::SHT_RELA);
3603 for (const Elf_Rela &Rela : DynPLTRelRegion.getAsArrayRef<Elf_Rela>())
3604 printDynamicRelocation(Obj, Rela, true);
3605 } else {
3606 printRelocHeader(ELF::SHT_REL);
3607 for (const Elf_Rel &Rel : DynPLTRelRegion.getAsArrayRef<Elf_Rel>()) {
3608 Elf_Rela Rela;
3609 Rela.r_offset = Rel.r_offset;
3610 Rela.r_info = Rel.r_info;
3611 Rela.r_addend = 0;
3612 printDynamicRelocation(Obj, Rela, false);
3617 template <class ELFT>
3618 static void printGNUVersionSectionProlog(formatted_raw_ostream &OS,
3619 const Twine &Name, unsigned EntriesNum,
3620 const ELFFile<ELFT> *Obj,
3621 const typename ELFT::Shdr *Sec,
3622 StringRef FileName) {
3623 StringRef SecName = unwrapOrError(FileName, Obj->getSectionName(Sec));
3624 OS << Name << " section '" << SecName << "' "
3625 << "contains " << EntriesNum << " entries:\n";
3627 const typename ELFT::Shdr *SymTab =
3628 unwrapOrError(FileName, Obj->getSection(Sec->sh_link));
3629 StringRef SymTabName = unwrapOrError(FileName, Obj->getSectionName(SymTab));
3630 OS << " Addr: " << format_hex_no_prefix(Sec->sh_addr, 16)
3631 << " Offset: " << format_hex(Sec->sh_offset, 8)
3632 << " Link: " << Sec->sh_link << " (" << SymTabName << ")\n";
3635 template <class ELFT>
3636 void GNUStyle<ELFT>::printVersionSymbolSection(const ELFFile<ELFT> *Obj,
3637 const Elf_Shdr *Sec) {
3638 if (!Sec)
3639 return;
3641 unsigned Entries = Sec->sh_size / sizeof(Elf_Versym);
3642 printGNUVersionSectionProlog(OS, "Version symbols", Entries, Obj, Sec,
3643 this->FileName);
3645 const uint8_t *VersymBuf =
3646 reinterpret_cast<const uint8_t *>(Obj->base() + Sec->sh_offset);
3647 const ELFDumper<ELFT> *Dumper = this->dumper();
3648 StringRef StrTable = Dumper->getDynamicStringTable();
3650 // readelf prints 4 entries per line.
3651 for (uint64_t VersymRow = 0; VersymRow < Entries; VersymRow += 4) {
3652 OS << " " << format_hex_no_prefix(VersymRow, 3) << ":";
3654 for (uint64_t VersymIndex = 0;
3655 (VersymIndex < 4) && (VersymIndex + VersymRow) < Entries;
3656 ++VersymIndex) {
3657 const Elf_Versym *Versym =
3658 reinterpret_cast<const Elf_Versym *>(VersymBuf);
3659 switch (Versym->vs_index) {
3660 case 0:
3661 OS << " 0 (*local*) ";
3662 break;
3663 case 1:
3664 OS << " 1 (*global*) ";
3665 break;
3666 default:
3667 OS << format("%4x%c", Versym->vs_index & VERSYM_VERSION,
3668 Versym->vs_index & VERSYM_HIDDEN ? 'h' : ' ');
3670 bool IsDefault = true;
3671 std::string VersionName = Dumper->getSymbolVersionByIndex(
3672 StrTable, Versym->vs_index, IsDefault);
3674 if (!VersionName.empty())
3675 VersionName = "(" + VersionName + ")";
3676 else
3677 VersionName = "(*invalid*)";
3678 OS << left_justify(VersionName, 13);
3680 VersymBuf += sizeof(Elf_Versym);
3682 OS << '\n';
3684 OS << '\n';
3687 static std::string versionFlagToString(unsigned Flags) {
3688 if (Flags == 0)
3689 return "none";
3691 std::string Ret;
3692 auto AddFlag = [&Ret, &Flags](unsigned Flag, StringRef Name) {
3693 if (!(Flags & Flag))
3694 return;
3695 if (!Ret.empty())
3696 Ret += " | ";
3697 Ret += Name;
3698 Flags &= ~Flag;
3701 AddFlag(VER_FLG_BASE, "BASE");
3702 AddFlag(VER_FLG_WEAK, "WEAK");
3703 AddFlag(VER_FLG_INFO, "INFO");
3704 AddFlag(~0, "<unknown>");
3705 return Ret;
3708 template <class ELFT>
3709 void GNUStyle<ELFT>::printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
3710 const Elf_Shdr *Sec) {
3711 if (!Sec)
3712 return;
3714 unsigned VerDefsNum = Sec->sh_info;
3715 printGNUVersionSectionProlog(OS, "Version definition", VerDefsNum, Obj, Sec,
3716 this->FileName);
3718 const Elf_Shdr *StrTabSec =
3719 unwrapOrError(this->FileName, Obj->getSection(Sec->sh_link));
3720 StringRef StringTable(
3721 reinterpret_cast<const char *>(Obj->base() + StrTabSec->sh_offset),
3722 (size_t)StrTabSec->sh_size);
3724 const uint8_t *VerdefBuf =
3725 unwrapOrError(this->FileName, Obj->getSectionContents(Sec)).data();
3726 const uint8_t *Begin = VerdefBuf;
3728 while (VerDefsNum--) {
3729 const Elf_Verdef *Verdef = reinterpret_cast<const Elf_Verdef *>(VerdefBuf);
3730 OS << format(" 0x%04x: Rev: %u Flags: %s Index: %u Cnt: %u",
3731 VerdefBuf - Begin, (unsigned)Verdef->vd_version,
3732 versionFlagToString(Verdef->vd_flags).c_str(),
3733 (unsigned)Verdef->vd_ndx, (unsigned)Verdef->vd_cnt);
3735 const uint8_t *VerdauxBuf = VerdefBuf + Verdef->vd_aux;
3736 const Elf_Verdaux *Verdaux =
3737 reinterpret_cast<const Elf_Verdaux *>(VerdauxBuf);
3738 OS << format(" Name: %s\n",
3739 StringTable.drop_front(Verdaux->vda_name).data());
3741 for (unsigned I = 1; I < Verdef->vd_cnt; ++I) {
3742 VerdauxBuf += Verdaux->vda_next;
3743 Verdaux = reinterpret_cast<const Elf_Verdaux *>(VerdauxBuf);
3744 OS << format(" 0x%04x: Parent %u: %s\n", VerdauxBuf - Begin, I,
3745 StringTable.drop_front(Verdaux->vda_name).data());
3748 VerdefBuf += Verdef->vd_next;
3750 OS << '\n';
3753 template <class ELFT>
3754 void GNUStyle<ELFT>::printVersionDependencySection(const ELFFile<ELFT> *Obj,
3755 const Elf_Shdr *Sec) {
3756 if (!Sec)
3757 return;
3759 unsigned VerneedNum = Sec->sh_info;
3760 printGNUVersionSectionProlog(OS, "Version needs", VerneedNum, Obj, Sec,
3761 this->FileName);
3763 ArrayRef<uint8_t> SecData =
3764 unwrapOrError(this->FileName, Obj->getSectionContents(Sec));
3766 const Elf_Shdr *StrTabSec =
3767 unwrapOrError(this->FileName, Obj->getSection(Sec->sh_link));
3768 StringRef StringTable = {
3769 reinterpret_cast<const char *>(Obj->base() + StrTabSec->sh_offset),
3770 (size_t)StrTabSec->sh_size};
3772 const uint8_t *VerneedBuf = SecData.data();
3773 for (unsigned I = 0; I < VerneedNum; ++I) {
3774 const Elf_Verneed *Verneed =
3775 reinterpret_cast<const Elf_Verneed *>(VerneedBuf);
3777 OS << format(" 0x%04x: Version: %u File: %s Cnt: %u\n",
3778 reinterpret_cast<const uint8_t *>(Verneed) - SecData.begin(),
3779 (unsigned)Verneed->vn_version,
3780 StringTable.drop_front(Verneed->vn_file).data(),
3781 (unsigned)Verneed->vn_cnt);
3783 const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux;
3784 for (unsigned J = 0; J < Verneed->vn_cnt; ++J) {
3785 const Elf_Vernaux *Vernaux =
3786 reinterpret_cast<const Elf_Vernaux *>(VernauxBuf);
3788 OS << format(" 0x%04x: Name: %s Flags: %s Version: %u\n",
3789 reinterpret_cast<const uint8_t *>(Vernaux) - SecData.begin(),
3790 StringTable.drop_front(Vernaux->vna_name).data(),
3791 versionFlagToString(Vernaux->vna_flags).c_str(),
3792 (unsigned)Vernaux->vna_other);
3793 VernauxBuf += Vernaux->vna_next;
3795 VerneedBuf += Verneed->vn_next;
3797 OS << '\n';
3800 // Hash histogram shows statistics of how efficient the hash was for the
3801 // dynamic symbol table. The table shows number of hash buckets for different
3802 // lengths of chains as absolute number and percentage of the total buckets.
3803 // Additionally cumulative coverage of symbols for each set of buckets.
3804 template <class ELFT>
3805 void GNUStyle<ELFT>::printHashHistogram(const ELFFile<ELFT> *Obj) {
3806 // Print histogram for .hash section
3807 if (const Elf_Hash *HashTable = this->dumper()->getHashTable()) {
3808 size_t NBucket = HashTable->nbucket;
3809 size_t NChain = HashTable->nchain;
3810 ArrayRef<Elf_Word> Buckets = HashTable->buckets();
3811 ArrayRef<Elf_Word> Chains = HashTable->chains();
3812 size_t TotalSyms = 0;
3813 // If hash table is correct, we have at least chains with 0 length
3814 size_t MaxChain = 1;
3815 size_t CumulativeNonZero = 0;
3817 if (NChain == 0 || NBucket == 0)
3818 return;
3820 std::vector<size_t> ChainLen(NBucket, 0);
3821 // Go over all buckets and and note chain lengths of each bucket (total
3822 // unique chain lengths).
3823 for (size_t B = 0; B < NBucket; B++) {
3824 for (size_t C = Buckets[B]; C > 0 && C < NChain; C = Chains[C])
3825 if (MaxChain <= ++ChainLen[B])
3826 MaxChain++;
3827 TotalSyms += ChainLen[B];
3830 if (!TotalSyms)
3831 return;
3833 std::vector<size_t> Count(MaxChain, 0) ;
3834 // Count how long is the chain for each bucket
3835 for (size_t B = 0; B < NBucket; B++)
3836 ++Count[ChainLen[B]];
3837 // Print Number of buckets with each chain lengths and their cumulative
3838 // coverage of the symbols
3839 OS << "Histogram for bucket list length (total of " << NBucket
3840 << " buckets)\n"
3841 << " Length Number % of total Coverage\n";
3842 for (size_t I = 0; I < MaxChain; I++) {
3843 CumulativeNonZero += Count[I] * I;
3844 OS << format("%7lu %-10lu (%5.1f%%) %5.1f%%\n", I, Count[I],
3845 (Count[I] * 100.0) / NBucket,
3846 (CumulativeNonZero * 100.0) / TotalSyms);
3850 // Print histogram for .gnu.hash section
3851 if (const Elf_GnuHash *GnuHashTable = this->dumper()->getGnuHashTable()) {
3852 size_t NBucket = GnuHashTable->nbuckets;
3853 ArrayRef<Elf_Word> Buckets = GnuHashTable->buckets();
3854 unsigned NumSyms = this->dumper()->dynamic_symbols().size();
3855 if (!NumSyms)
3856 return;
3857 ArrayRef<Elf_Word> Chains = GnuHashTable->values(NumSyms);
3858 size_t Symndx = GnuHashTable->symndx;
3859 size_t TotalSyms = 0;
3860 size_t MaxChain = 1;
3861 size_t CumulativeNonZero = 0;
3863 if (Chains.empty() || NBucket == 0)
3864 return;
3866 std::vector<size_t> ChainLen(NBucket, 0);
3868 for (size_t B = 0; B < NBucket; B++) {
3869 if (!Buckets[B])
3870 continue;
3871 size_t Len = 1;
3872 for (size_t C = Buckets[B] - Symndx;
3873 C < Chains.size() && (Chains[C] & 1) == 0; C++)
3874 if (MaxChain < ++Len)
3875 MaxChain++;
3876 ChainLen[B] = Len;
3877 TotalSyms += Len;
3879 MaxChain++;
3881 if (!TotalSyms)
3882 return;
3884 std::vector<size_t> Count(MaxChain, 0) ;
3885 for (size_t B = 0; B < NBucket; B++)
3886 ++Count[ChainLen[B]];
3887 // Print Number of buckets with each chain lengths and their cumulative
3888 // coverage of the symbols
3889 OS << "Histogram for `.gnu.hash' bucket list length (total of " << NBucket
3890 << " buckets)\n"
3891 << " Length Number % of total Coverage\n";
3892 for (size_t I = 0; I <MaxChain; I++) {
3893 CumulativeNonZero += Count[I] * I;
3894 OS << format("%7lu %-10lu (%5.1f%%) %5.1f%%\n", I, Count[I],
3895 (Count[I] * 100.0) / NBucket,
3896 (CumulativeNonZero * 100.0) / TotalSyms);
3901 template <class ELFT>
3902 void GNUStyle<ELFT>::printCGProfile(const ELFFile<ELFT> *Obj) {
3903 OS << "GNUStyle::printCGProfile not implemented\n";
3906 template <class ELFT>
3907 void GNUStyle<ELFT>::printAddrsig(const ELFFile<ELFT> *Obj) {
3908 OS << "GNUStyle::printAddrsig not implemented\n";
3911 static StringRef getGenericNoteTypeName(const uint32_t NT) {
3912 static const struct {
3913 uint32_t ID;
3914 const char *Name;
3915 } Notes[] = {
3916 {ELF::NT_VERSION, "NT_VERSION (version)"},
3917 {ELF::NT_ARCH, "NT_ARCH (architecture)"},
3918 {ELF::NT_GNU_BUILD_ATTRIBUTE_OPEN, "OPEN"},
3919 {ELF::NT_GNU_BUILD_ATTRIBUTE_FUNC, "func"},
3922 for (const auto &Note : Notes)
3923 if (Note.ID == NT)
3924 return Note.Name;
3926 return "";
3929 static StringRef getCoreNoteTypeName(const uint32_t NT) {
3930 static const struct {
3931 uint32_t ID;
3932 const char *Name;
3933 } Notes[] = {
3934 {ELF::NT_PRSTATUS, "NT_PRSTATUS (prstatus structure)"},
3935 {ELF::NT_FPREGSET, "NT_FPREGSET (floating point registers)"},
3936 {ELF::NT_PRPSINFO, "NT_PRPSINFO (prpsinfo structure)"},
3937 {ELF::NT_TASKSTRUCT, "NT_TASKSTRUCT (task structure)"},
3938 {ELF::NT_AUXV, "NT_AUXV (auxiliary vector)"},
3939 {ELF::NT_PSTATUS, "NT_PSTATUS (pstatus structure)"},
3940 {ELF::NT_FPREGS, "NT_FPREGS (floating point registers)"},
3941 {ELF::NT_PSINFO, "NT_PSINFO (psinfo structure)"},
3942 {ELF::NT_LWPSTATUS, "NT_LWPSTATUS (lwpstatus_t structure)"},
3943 {ELF::NT_LWPSINFO, "NT_LWPSINFO (lwpsinfo_t structure)"},
3944 {ELF::NT_WIN32PSTATUS, "NT_WIN32PSTATUS (win32_pstatus structure)"},
3946 {ELF::NT_PPC_VMX, "NT_PPC_VMX (ppc Altivec registers)"},
3947 {ELF::NT_PPC_VSX, "NT_PPC_VSX (ppc VSX registers)"},
3948 {ELF::NT_PPC_TAR, "NT_PPC_TAR (ppc TAR register)"},
3949 {ELF::NT_PPC_PPR, "NT_PPC_PPR (ppc PPR register)"},
3950 {ELF::NT_PPC_DSCR, "NT_PPC_DSCR (ppc DSCR register)"},
3951 {ELF::NT_PPC_EBB, "NT_PPC_EBB (ppc EBB registers)"},
3952 {ELF::NT_PPC_PMU, "NT_PPC_PMU (ppc PMU registers)"},
3953 {ELF::NT_PPC_TM_CGPR, "NT_PPC_TM_CGPR (ppc checkpointed GPR registers)"},
3954 {ELF::NT_PPC_TM_CFPR,
3955 "NT_PPC_TM_CFPR (ppc checkpointed floating point registers)"},
3956 {ELF::NT_PPC_TM_CVMX,
3957 "NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)"},
3958 {ELF::NT_PPC_TM_CVSX, "NT_PPC_TM_CVSX (ppc checkpointed VSX registers)"},
3959 {ELF::NT_PPC_TM_SPR, "NT_PPC_TM_SPR (ppc TM special purpose registers)"},
3960 {ELF::NT_PPC_TM_CTAR, "NT_PPC_TM_CTAR (ppc checkpointed TAR register)"},
3961 {ELF::NT_PPC_TM_CPPR, "NT_PPC_TM_CPPR (ppc checkpointed PPR register)"},
3962 {ELF::NT_PPC_TM_CDSCR,
3963 "NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)"},
3965 {ELF::NT_386_TLS, "NT_386_TLS (x86 TLS information)"},
3966 {ELF::NT_386_IOPERM, "NT_386_IOPERM (x86 I/O permissions)"},
3967 {ELF::NT_X86_XSTATE, "NT_X86_XSTATE (x86 XSAVE extended state)"},
3969 {ELF::NT_S390_HIGH_GPRS,
3970 "NT_S390_HIGH_GPRS (s390 upper register halves)"},
3971 {ELF::NT_S390_TIMER, "NT_S390_TIMER (s390 timer register)"},
3972 {ELF::NT_S390_TODCMP, "NT_S390_TODCMP (s390 TOD comparator register)"},
3973 {ELF::NT_S390_TODPREG,
3974 "NT_S390_TODPREG (s390 TOD programmable register)"},
3975 {ELF::NT_S390_CTRS, "NT_S390_CTRS (s390 control registers)"},
3976 {ELF::NT_S390_PREFIX, "NT_S390_PREFIX (s390 prefix register)"},
3977 {ELF::NT_S390_LAST_BREAK,
3978 "NT_S390_LAST_BREAK (s390 last breaking event address)"},
3979 {ELF::NT_S390_SYSTEM_CALL,
3980 "NT_S390_SYSTEM_CALL (s390 system call restart data)"},
3981 {ELF::NT_S390_TDB, "NT_S390_TDB (s390 transaction diagnostic block)"},
3982 {ELF::NT_S390_VXRS_LOW,
3983 "NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)"},
3984 {ELF::NT_S390_VXRS_HIGH,
3985 "NT_S390_VXRS_HIGH (s390 vector registers 16-31)"},
3986 {ELF::NT_S390_GS_CB, "NT_S390_GS_CB (s390 guarded-storage registers)"},
3987 {ELF::NT_S390_GS_BC,
3988 "NT_S390_GS_BC (s390 guarded-storage broadcast control)"},
3990 {ELF::NT_ARM_VFP, "NT_ARM_VFP (arm VFP registers)"},
3991 {ELF::NT_ARM_TLS, "NT_ARM_TLS (AArch TLS registers)"},
3992 {ELF::NT_ARM_HW_BREAK,
3993 "NT_ARM_HW_BREAK (AArch hardware breakpoint registers)"},
3994 {ELF::NT_ARM_HW_WATCH,
3995 "NT_ARM_HW_WATCH (AArch hardware watchpoint registers)"},
3997 {ELF::NT_FILE, "NT_FILE (mapped files)"},
3998 {ELF::NT_PRXFPREG, "NT_PRXFPREG (user_xfpregs structure)"},
3999 {ELF::NT_SIGINFO, "NT_SIGINFO (siginfo_t data)"},
4002 for (const auto &Note : Notes)
4003 if (Note.ID == NT)
4004 return Note.Name;
4006 return "";
4009 static std::string getGNUNoteTypeName(const uint32_t NT) {
4010 static const struct {
4011 uint32_t ID;
4012 const char *Name;
4013 } Notes[] = {
4014 {ELF::NT_GNU_ABI_TAG, "NT_GNU_ABI_TAG (ABI version tag)"},
4015 {ELF::NT_GNU_HWCAP, "NT_GNU_HWCAP (DSO-supplied software HWCAP info)"},
4016 {ELF::NT_GNU_BUILD_ID, "NT_GNU_BUILD_ID (unique build ID bitstring)"},
4017 {ELF::NT_GNU_GOLD_VERSION, "NT_GNU_GOLD_VERSION (gold version)"},
4018 {ELF::NT_GNU_PROPERTY_TYPE_0, "NT_GNU_PROPERTY_TYPE_0 (property note)"},
4021 for (const auto &Note : Notes)
4022 if (Note.ID == NT)
4023 return std::string(Note.Name);
4025 std::string string;
4026 raw_string_ostream OS(string);
4027 OS << format("Unknown note type (0x%08x)", NT);
4028 return OS.str();
4031 static std::string getFreeBSDNoteTypeName(const uint32_t NT) {
4032 static const struct {
4033 uint32_t ID;
4034 const char *Name;
4035 } Notes[] = {
4036 {ELF::NT_FREEBSD_THRMISC, "NT_THRMISC (thrmisc structure)"},
4037 {ELF::NT_FREEBSD_PROCSTAT_PROC, "NT_PROCSTAT_PROC (proc data)"},
4038 {ELF::NT_FREEBSD_PROCSTAT_FILES, "NT_PROCSTAT_FILES (files data)"},
4039 {ELF::NT_FREEBSD_PROCSTAT_VMMAP, "NT_PROCSTAT_VMMAP (vmmap data)"},
4040 {ELF::NT_FREEBSD_PROCSTAT_GROUPS, "NT_PROCSTAT_GROUPS (groups data)"},
4041 {ELF::NT_FREEBSD_PROCSTAT_UMASK, "NT_PROCSTAT_UMASK (umask data)"},
4042 {ELF::NT_FREEBSD_PROCSTAT_RLIMIT, "NT_PROCSTAT_RLIMIT (rlimit data)"},
4043 {ELF::NT_FREEBSD_PROCSTAT_OSREL, "NT_PROCSTAT_OSREL (osreldate data)"},
4044 {ELF::NT_FREEBSD_PROCSTAT_PSSTRINGS,
4045 "NT_PROCSTAT_PSSTRINGS (ps_strings data)"},
4046 {ELF::NT_FREEBSD_PROCSTAT_AUXV, "NT_PROCSTAT_AUXV (auxv data)"},
4049 for (const auto &Note : Notes)
4050 if (Note.ID == NT)
4051 return std::string(Note.Name);
4053 std::string string;
4054 raw_string_ostream OS(string);
4055 OS << format("Unknown note type (0x%08x)", NT);
4056 return OS.str();
4059 static std::string getAMDNoteTypeName(const uint32_t NT) {
4060 static const struct {
4061 uint32_t ID;
4062 const char *Name;
4063 } Notes[] = {{ELF::NT_AMD_AMDGPU_HSA_METADATA,
4064 "NT_AMD_AMDGPU_HSA_METADATA (HSA Metadata)"},
4065 {ELF::NT_AMD_AMDGPU_ISA, "NT_AMD_AMDGPU_ISA (ISA Version)"},
4066 {ELF::NT_AMD_AMDGPU_PAL_METADATA,
4067 "NT_AMD_AMDGPU_PAL_METADATA (PAL Metadata)"}};
4069 for (const auto &Note : Notes)
4070 if (Note.ID == NT)
4071 return std::string(Note.Name);
4073 std::string string;
4074 raw_string_ostream OS(string);
4075 OS << format("Unknown note type (0x%08x)", NT);
4076 return OS.str();
4079 static std::string getAMDGPUNoteTypeName(const uint32_t NT) {
4080 if (NT == ELF::NT_AMDGPU_METADATA)
4081 return std::string("NT_AMDGPU_METADATA (AMDGPU Metadata)");
4083 std::string string;
4084 raw_string_ostream OS(string);
4085 OS << format("Unknown note type (0x%08x)", NT);
4086 return OS.str();
4089 template <typename ELFT>
4090 static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
4091 ArrayRef<uint8_t> Data) {
4092 std::string str;
4093 raw_string_ostream OS(str);
4094 uint32_t PrData;
4095 auto DumpBit = [&](uint32_t Flag, StringRef Name) {
4096 if (PrData & Flag) {
4097 PrData &= ~Flag;
4098 OS << Name;
4099 if (PrData)
4100 OS << ", ";
4104 switch (Type) {
4105 default:
4106 OS << format("<application-specific type 0x%x>", Type);
4107 return OS.str();
4108 case GNU_PROPERTY_STACK_SIZE: {
4109 OS << "stack size: ";
4110 if (DataSize == sizeof(typename ELFT::uint))
4111 OS << formatv("{0:x}",
4112 (uint64_t)(*(const typename ELFT::Addr *)Data.data()));
4113 else
4114 OS << format("<corrupt length: 0x%x>", DataSize);
4115 return OS.str();
4117 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
4118 OS << "no copy on protected";
4119 if (DataSize)
4120 OS << format(" <corrupt length: 0x%x>", DataSize);
4121 return OS.str();
4122 case GNU_PROPERTY_AARCH64_FEATURE_1_AND:
4123 case GNU_PROPERTY_X86_FEATURE_1_AND:
4124 OS << ((Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) ? "aarch64 feature: "
4125 : "x86 feature: ");
4126 if (DataSize != 4) {
4127 OS << format("<corrupt length: 0x%x>", DataSize);
4128 return OS.str();
4130 PrData = support::endian::read32<ELFT::TargetEndianness>(Data.data());
4131 if (PrData == 0) {
4132 OS << "<None>";
4133 return OS.str();
4135 if (Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
4136 DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI");
4137 DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC");
4138 } else {
4139 DumpBit(GNU_PROPERTY_X86_FEATURE_1_IBT, "IBT");
4140 DumpBit(GNU_PROPERTY_X86_FEATURE_1_SHSTK, "SHSTK");
4142 if (PrData)
4143 OS << format("<unknown flags: 0x%x>", PrData);
4144 return OS.str();
4145 case GNU_PROPERTY_X86_ISA_1_NEEDED:
4146 case GNU_PROPERTY_X86_ISA_1_USED:
4147 OS << "x86 ISA "
4148 << (Type == GNU_PROPERTY_X86_ISA_1_NEEDED ? "needed: " : "used: ");
4149 if (DataSize != 4) {
4150 OS << format("<corrupt length: 0x%x>", DataSize);
4151 return OS.str();
4153 PrData = support::endian::read32<ELFT::TargetEndianness>(Data.data());
4154 if (PrData == 0) {
4155 OS << "<None>";
4156 return OS.str();
4158 DumpBit(GNU_PROPERTY_X86_ISA_1_CMOV, "CMOV");
4159 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE, "SSE");
4160 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE2, "SSE2");
4161 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE3, "SSE3");
4162 DumpBit(GNU_PROPERTY_X86_ISA_1_SSSE3, "SSSE3");
4163 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE4_1, "SSE4_1");
4164 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE4_2, "SSE4_2");
4165 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX, "AVX");
4166 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX2, "AVX2");
4167 DumpBit(GNU_PROPERTY_X86_ISA_1_FMA, "FMA");
4168 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512F, "AVX512F");
4169 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512CD, "AVX512CD");
4170 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512ER, "AVX512ER");
4171 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512PF, "AVX512PF");
4172 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512VL, "AVX512VL");
4173 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512DQ, "AVX512DQ");
4174 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512BW, "AVX512BW");
4175 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_4FMAPS, "AVX512_4FMAPS");
4176 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_4VNNIW, "AVX512_4VNNIW");
4177 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_BITALG, "AVX512_BITALG");
4178 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_IFMA, "AVX512_IFMA");
4179 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_VBMI, "AVX512_VBMI");
4180 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_VBMI2, "AVX512_VBMI2");
4181 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_VNNI, "AVX512_VNNI");
4182 if (PrData)
4183 OS << format("<unknown flags: 0x%x>", PrData);
4184 return OS.str();
4185 break;
4186 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
4187 case GNU_PROPERTY_X86_FEATURE_2_USED:
4188 OS << "x86 feature "
4189 << (Type == GNU_PROPERTY_X86_FEATURE_2_NEEDED ? "needed: " : "used: ");
4190 if (DataSize != 4) {
4191 OS << format("<corrupt length: 0x%x>", DataSize);
4192 return OS.str();
4194 PrData = support::endian::read32<ELFT::TargetEndianness>(Data.data());
4195 if (PrData == 0) {
4196 OS << "<None>";
4197 return OS.str();
4199 DumpBit(GNU_PROPERTY_X86_FEATURE_2_X86, "x86");
4200 DumpBit(GNU_PROPERTY_X86_FEATURE_2_X87, "x87");
4201 DumpBit(GNU_PROPERTY_X86_FEATURE_2_MMX, "MMX");
4202 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XMM, "XMM");
4203 DumpBit(GNU_PROPERTY_X86_FEATURE_2_YMM, "YMM");
4204 DumpBit(GNU_PROPERTY_X86_FEATURE_2_ZMM, "ZMM");
4205 DumpBit(GNU_PROPERTY_X86_FEATURE_2_FXSR, "FXSR");
4206 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVE, "XSAVE");
4207 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT, "XSAVEOPT");
4208 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVEC, "XSAVEC");
4209 if (PrData)
4210 OS << format("<unknown flags: 0x%x>", PrData);
4211 return OS.str();
4215 template <typename ELFT>
4216 static SmallVector<std::string, 4> getGNUPropertyList(ArrayRef<uint8_t> Arr) {
4217 using Elf_Word = typename ELFT::Word;
4219 SmallVector<std::string, 4> Properties;
4220 while (Arr.size() >= 8) {
4221 uint32_t Type = *reinterpret_cast<const Elf_Word *>(Arr.data());
4222 uint32_t DataSize = *reinterpret_cast<const Elf_Word *>(Arr.data() + 4);
4223 Arr = Arr.drop_front(8);
4225 // Take padding size into account if present.
4226 uint64_t PaddedSize = alignTo(DataSize, sizeof(typename ELFT::uint));
4227 std::string str;
4228 raw_string_ostream OS(str);
4229 if (Arr.size() < PaddedSize) {
4230 OS << format("<corrupt type (0x%x) datasz: 0x%x>", Type, DataSize);
4231 Properties.push_back(OS.str());
4232 break;
4234 Properties.push_back(
4235 getGNUProperty<ELFT>(Type, DataSize, Arr.take_front(PaddedSize)));
4236 Arr = Arr.drop_front(PaddedSize);
4239 if (!Arr.empty())
4240 Properties.push_back("<corrupted GNU_PROPERTY_TYPE_0>");
4242 return Properties;
4245 struct GNUAbiTag {
4246 std::string OSName;
4247 std::string ABI;
4248 bool IsValid;
4251 template <typename ELFT> static GNUAbiTag getGNUAbiTag(ArrayRef<uint8_t> Desc) {
4252 typedef typename ELFT::Word Elf_Word;
4254 ArrayRef<Elf_Word> Words(reinterpret_cast<const Elf_Word *>(Desc.begin()),
4255 reinterpret_cast<const Elf_Word *>(Desc.end()));
4257 if (Words.size() < 4)
4258 return {"", "", /*IsValid=*/false};
4260 static const char *OSNames[] = {
4261 "Linux", "Hurd", "Solaris", "FreeBSD", "NetBSD", "Syllable", "NaCl",
4263 StringRef OSName = "Unknown";
4264 if (Words[0] < array_lengthof(OSNames))
4265 OSName = OSNames[Words[0]];
4266 uint32_t Major = Words[1], Minor = Words[2], Patch = Words[3];
4267 std::string str;
4268 raw_string_ostream ABI(str);
4269 ABI << Major << "." << Minor << "." << Patch;
4270 return {OSName, ABI.str(), /*IsValid=*/true};
4273 static std::string getGNUBuildId(ArrayRef<uint8_t> Desc) {
4274 std::string str;
4275 raw_string_ostream OS(str);
4276 for (const auto &B : Desc)
4277 OS << format_hex_no_prefix(B, 2);
4278 return OS.str();
4281 static StringRef getGNUGoldVersion(ArrayRef<uint8_t> Desc) {
4282 return StringRef(reinterpret_cast<const char *>(Desc.data()), Desc.size());
4285 template <typename ELFT>
4286 static void printGNUNote(raw_ostream &OS, uint32_t NoteType,
4287 ArrayRef<uint8_t> Desc) {
4288 switch (NoteType) {
4289 default:
4290 return;
4291 case ELF::NT_GNU_ABI_TAG: {
4292 const GNUAbiTag &AbiTag = getGNUAbiTag<ELFT>(Desc);
4293 if (!AbiTag.IsValid)
4294 OS << " <corrupt GNU_ABI_TAG>";
4295 else
4296 OS << " OS: " << AbiTag.OSName << ", ABI: " << AbiTag.ABI;
4297 break;
4299 case ELF::NT_GNU_BUILD_ID: {
4300 OS << " Build ID: " << getGNUBuildId(Desc);
4301 break;
4303 case ELF::NT_GNU_GOLD_VERSION:
4304 OS << " Version: " << getGNUGoldVersion(Desc);
4305 break;
4306 case ELF::NT_GNU_PROPERTY_TYPE_0:
4307 OS << " Properties:";
4308 for (const auto &Property : getGNUPropertyList<ELFT>(Desc))
4309 OS << " " << Property << "\n";
4310 break;
4312 OS << '\n';
4315 struct AMDNote {
4316 std::string Type;
4317 std::string Value;
4320 template <typename ELFT>
4321 static AMDNote getAMDNote(uint32_t NoteType, ArrayRef<uint8_t> Desc) {
4322 switch (NoteType) {
4323 default:
4324 return {"", ""};
4325 case ELF::NT_AMD_AMDGPU_HSA_METADATA:
4326 return {
4327 "HSA Metadata",
4328 std::string(reinterpret_cast<const char *>(Desc.data()), Desc.size())};
4329 case ELF::NT_AMD_AMDGPU_ISA:
4330 return {
4331 "ISA Version",
4332 std::string(reinterpret_cast<const char *>(Desc.data()), Desc.size())};
4336 struct AMDGPUNote {
4337 std::string Type;
4338 std::string Value;
4341 template <typename ELFT>
4342 static AMDGPUNote getAMDGPUNote(uint32_t NoteType, ArrayRef<uint8_t> Desc) {
4343 switch (NoteType) {
4344 default:
4345 return {"", ""};
4346 case ELF::NT_AMDGPU_METADATA: {
4347 auto MsgPackString =
4348 StringRef(reinterpret_cast<const char *>(Desc.data()), Desc.size());
4349 msgpack::Document MsgPackDoc;
4350 if (!MsgPackDoc.readFromBlob(MsgPackString, /*Multi=*/false))
4351 return {"AMDGPU Metadata", "Invalid AMDGPU Metadata"};
4353 AMDGPU::HSAMD::V3::MetadataVerifier Verifier(true);
4354 if (!Verifier.verify(MsgPackDoc.getRoot()))
4355 return {"AMDGPU Metadata", "Invalid AMDGPU Metadata"};
4357 std::string HSAMetadataString;
4358 raw_string_ostream StrOS(HSAMetadataString);
4359 MsgPackDoc.toYAML(StrOS);
4361 return {"AMDGPU Metadata", StrOS.str()};
4366 template <class ELFT>
4367 void GNUStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
4368 auto PrintHeader = [&](const typename ELFT::Off Offset,
4369 const typename ELFT::Addr Size) {
4370 OS << "Displaying notes found at file offset " << format_hex(Offset, 10)
4371 << " with length " << format_hex(Size, 10) << ":\n"
4372 << " Owner Data size \tDescription\n";
4375 auto ProcessNote = [&](const Elf_Note &Note) {
4376 StringRef Name = Note.getName();
4377 ArrayRef<uint8_t> Descriptor = Note.getDesc();
4378 Elf_Word Type = Note.getType();
4380 OS << " " << left_justify(Name, 20) << ' '
4381 << format_hex(Descriptor.size(), 10) << '\t';
4383 if (Name == "GNU") {
4384 OS << getGNUNoteTypeName(Type) << '\n';
4385 printGNUNote<ELFT>(OS, Type, Descriptor);
4386 } else if (Name == "FreeBSD") {
4387 OS << getFreeBSDNoteTypeName(Type) << '\n';
4388 } else if (Name == "AMD") {
4389 OS << getAMDNoteTypeName(Type) << '\n';
4390 const AMDNote N = getAMDNote<ELFT>(Type, Descriptor);
4391 if (!N.Type.empty())
4392 OS << " " << N.Type << ":\n " << N.Value << '\n';
4393 } else if (Name == "AMDGPU") {
4394 OS << getAMDGPUNoteTypeName(Type) << '\n';
4395 const AMDGPUNote N = getAMDGPUNote<ELFT>(Type, Descriptor);
4396 if (!N.Type.empty())
4397 OS << " " << N.Type << ":\n " << N.Value << '\n';
4398 } else {
4399 StringRef NoteType = Obj->getHeader()->e_type == ELF::ET_CORE
4400 ? getCoreNoteTypeName(Type)
4401 : getGenericNoteTypeName(Type);
4402 if (!NoteType.empty())
4403 OS << NoteType;
4404 else
4405 OS << "Unknown note type: (" << format_hex(Type, 10) << ')';
4407 OS << '\n';
4410 if (Obj->getHeader()->e_type == ELF::ET_CORE) {
4411 for (const auto &P :
4412 unwrapOrError(this->FileName, Obj->program_headers())) {
4413 if (P.p_type != PT_NOTE)
4414 continue;
4415 PrintHeader(P.p_offset, P.p_filesz);
4416 Error Err = Error::success();
4417 for (const auto &Note : Obj->notes(P, Err))
4418 ProcessNote(Note);
4419 if (Err)
4420 error(std::move(Err));
4422 } else {
4423 for (const auto &S :
4424 unwrapOrError(this->FileName, Obj->sections())) {
4425 if (S.sh_type != SHT_NOTE)
4426 continue;
4427 PrintHeader(S.sh_offset, S.sh_size);
4428 Error Err = Error::success();
4429 for (const auto &Note : Obj->notes(S, Err))
4430 ProcessNote(Note);
4431 if (Err)
4432 error(std::move(Err));
4437 template <class ELFT>
4438 void GNUStyle<ELFT>::printELFLinkerOptions(const ELFFile<ELFT> *Obj) {
4439 OS << "printELFLinkerOptions not implemented!\n";
4442 template <class ELFT>
4443 void DumpStyle<ELFT>::printFunctionStackSize(
4444 const ELFObjectFile<ELFT> *Obj, uint64_t SymValue, SectionRef FunctionSec,
4445 const StringRef SectionName, DataExtractor Data, uint64_t *Offset) {
4446 // This function ignores potentially erroneous input, unless it is directly
4447 // related to stack size reporting.
4448 SymbolRef FuncSym;
4449 for (const ELFSymbolRef &Symbol : Obj->symbols()) {
4450 Expected<uint64_t> SymAddrOrErr = Symbol.getAddress();
4451 if (!SymAddrOrErr) {
4452 consumeError(SymAddrOrErr.takeError());
4453 continue;
4455 if (Symbol.getELFType() == ELF::STT_FUNC && *SymAddrOrErr == SymValue) {
4456 // Check if the symbol is in the right section.
4457 if (FunctionSec.containsSymbol(Symbol)) {
4458 FuncSym = Symbol;
4459 break;
4464 StringRef FileStr = Obj->getFileName();
4465 std::string FuncName = "?";
4466 // A valid SymbolRef has a non-null object file pointer.
4467 if (FuncSym.BasicSymbolRef::getObject()) {
4468 // Extract the symbol name.
4469 Expected<StringRef> FuncNameOrErr = FuncSym.getName();
4470 if (FuncNameOrErr)
4471 FuncName = maybeDemangle(*FuncNameOrErr);
4472 else
4473 consumeError(FuncNameOrErr.takeError());
4474 } else
4475 reportWarning(" '" + FileStr +
4476 "': could not identify function symbol for stack size entry");
4478 // Extract the size. The expectation is that Offset is pointing to the right
4479 // place, i.e. past the function address.
4480 uint64_t PrevOffset = *Offset;
4481 uint64_t StackSize = Data.getULEB128(Offset);
4482 // getULEB128() does not advance Offset if it is not able to extract a valid
4483 // integer.
4484 if (*Offset == PrevOffset)
4485 reportError(
4486 FileStr,
4487 createStringError(object_error::parse_failed,
4488 "could not extract a valid stack size in section %s",
4489 SectionName.data()));
4491 printStackSizeEntry(StackSize, FuncName);
4494 template <class ELFT>
4495 void GNUStyle<ELFT>::printStackSizeEntry(uint64_t Size, StringRef FuncName) {
4496 OS.PadToColumn(2);
4497 OS << format_decimal(Size, 11);
4498 OS.PadToColumn(18);
4499 OS << FuncName << "\n";
4502 template <class ELFT>
4503 void DumpStyle<ELFT>::printStackSize(const ELFObjectFile<ELFT> *Obj,
4504 RelocationRef Reloc,
4505 SectionRef FunctionSec,
4506 const StringRef &StackSizeSectionName,
4507 const RelocationResolver &Resolver,
4508 DataExtractor Data) {
4509 // This function ignores potentially erroneous input, unless it is directly
4510 // related to stack size reporting.
4511 object::symbol_iterator RelocSym = Reloc.getSymbol();
4512 uint64_t RelocSymValue = 0;
4513 StringRef FileStr = Obj->getFileName();
4514 if (RelocSym != Obj->symbol_end()) {
4515 // Ensure that the relocation symbol is in the function section, i.e. the
4516 // section where the functions whose stack sizes we are reporting are
4517 // located.
4518 StringRef SymName = "?";
4519 Expected<StringRef> NameOrErr = RelocSym->getName();
4520 if (NameOrErr)
4521 SymName = *NameOrErr;
4522 else
4523 consumeError(NameOrErr.takeError());
4525 auto SectionOrErr = RelocSym->getSection();
4526 if (!SectionOrErr) {
4527 reportWarning(" '" + FileStr +
4528 "': cannot identify the section for relocation symbol " +
4529 SymName);
4530 consumeError(SectionOrErr.takeError());
4531 } else if (*SectionOrErr != FunctionSec) {
4532 reportWarning(" '" + FileStr + "': relocation symbol " + SymName +
4533 " is not in the expected section");
4534 // Pretend that the symbol is in the correct section and report its
4535 // stack size anyway.
4536 FunctionSec = **SectionOrErr;
4539 Expected<uint64_t> RelocSymValueOrErr = RelocSym->getValue();
4540 if (RelocSymValueOrErr)
4541 RelocSymValue = *RelocSymValueOrErr;
4542 else
4543 consumeError(RelocSymValueOrErr.takeError());
4546 uint64_t Offset = Reloc.getOffset();
4547 if (!Data.isValidOffsetForDataOfSize(Offset, sizeof(Elf_Addr) + 1))
4548 reportError(FileStr, createStringError(
4549 object_error::parse_failed,
4550 "found invalid relocation offset into section %s "
4551 "while trying to extract a stack size entry",
4552 StackSizeSectionName.data()));
4554 uint64_t Addend = Data.getAddress(&Offset);
4555 uint64_t SymValue = Resolver(Reloc, RelocSymValue, Addend);
4556 this->printFunctionStackSize(Obj, SymValue, FunctionSec, StackSizeSectionName,
4557 Data, &Offset);
4560 template <class ELFT>
4561 SectionRef toSectionRef(const ObjectFile *Obj, const typename ELFT::Shdr *Sec) {
4562 DataRefImpl DRI;
4563 DRI.p = reinterpret_cast<uintptr_t>(Sec);
4564 return SectionRef(DRI, Obj);
4567 template <class ELFT>
4568 void DumpStyle<ELFT>::printNonRelocatableStackSizes(
4569 const ELFObjectFile<ELFT> *Obj, std::function<void()> PrintHeader) {
4570 // This function ignores potentially erroneous input, unless it is directly
4571 // related to stack size reporting.
4572 const ELFFile<ELFT> *EF = Obj->getELFFile();
4573 StringRef FileStr = Obj->getFileName();
4574 for (const SectionRef &Sec : Obj->sections()) {
4575 StringRef SectionName;
4576 Sec.getName(SectionName);
4577 const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl());
4578 if (!SectionName.startswith(".stack_sizes"))
4579 continue;
4580 PrintHeader();
4581 ArrayRef<uint8_t> Contents =
4582 unwrapOrError(this->FileName, EF->getSectionContents(ElfSec));
4583 DataExtractor Data(
4584 StringRef(reinterpret_cast<const char *>(Contents.data()),
4585 Contents.size()),
4586 Obj->isLittleEndian(), sizeof(Elf_Addr));
4587 // A .stack_sizes section header's sh_link field is supposed to point
4588 // to the section that contains the functions whose stack sizes are
4589 // described in it.
4590 const Elf_Shdr *FunctionELFSec =
4591 unwrapOrError(this->FileName, EF->getSection(ElfSec->sh_link));
4592 uint64_t Offset = 0;
4593 while (Offset < Contents.size()) {
4594 // The function address is followed by a ULEB representing the stack
4595 // size. Check for an extra byte before we try to process the entry.
4596 if (!Data.isValidOffsetForDataOfSize(Offset, sizeof(Elf_Addr) + 1)) {
4597 reportError(
4598 FileStr,
4599 createStringError(
4600 object_error::parse_failed,
4601 "section %s ended while trying to extract a stack size entry",
4602 SectionName.data()));
4604 uint64_t SymValue = Data.getAddress(&Offset);
4605 printFunctionStackSize(Obj, SymValue,
4606 toSectionRef<ELFT>(Obj, FunctionELFSec),
4607 SectionName, Data, &Offset);
4612 template <class ELFT>
4613 void DumpStyle<ELFT>::printRelocatableStackSizes(
4614 const ELFObjectFile<ELFT> *Obj, std::function<void()> PrintHeader) {
4615 const ELFFile<ELFT> *EF = Obj->getELFFile();
4616 StringRef FileStr = Obj->getFileName();
4617 // Build a map between stack size sections and their corresponding relocation
4618 // sections.
4619 llvm::MapVector<SectionRef, SectionRef> StackSizeRelocMap;
4620 const SectionRef NullSection;
4622 for (const SectionRef &Sec : Obj->sections()) {
4623 StringRef SectionName;
4624 Sec.getName(SectionName);
4625 // A stack size section that we haven't encountered yet is mapped to the
4626 // null section until we find its corresponding relocation section.
4627 if (SectionName.startswith(".stack_sizes"))
4628 if (StackSizeRelocMap.count(Sec) == 0) {
4629 StackSizeRelocMap[Sec] = NullSection;
4630 continue;
4633 // Check relocation sections if they are relocating contents of a
4634 // stack sizes section.
4635 const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl());
4636 uint32_t SectionType = ElfSec->sh_type;
4637 if (SectionType != ELF::SHT_RELA && SectionType != ELF::SHT_REL)
4638 continue;
4640 SectionRef Contents = *Sec.getRelocatedSection();
4641 const Elf_Shdr *ContentsSec = Obj->getSection(Contents.getRawDataRefImpl());
4642 Expected<StringRef> ContentsSectionNameOrErr =
4643 EF->getSectionName(ContentsSec);
4644 if (!ContentsSectionNameOrErr) {
4645 consumeError(ContentsSectionNameOrErr.takeError());
4646 continue;
4648 if (!ContentsSectionNameOrErr->startswith(".stack_sizes"))
4649 continue;
4650 // Insert a mapping from the stack sizes section to its relocation section.
4651 StackSizeRelocMap[toSectionRef<ELFT>(Obj, ContentsSec)] = Sec;
4654 for (const auto &StackSizeMapEntry : StackSizeRelocMap) {
4655 PrintHeader();
4656 const SectionRef &StackSizesSec = StackSizeMapEntry.first;
4657 const SectionRef &RelocSec = StackSizeMapEntry.second;
4659 // Warn about stack size sections without a relocation section.
4660 StringRef StackSizeSectionName;
4661 StackSizesSec.getName(StackSizeSectionName);
4662 if (RelocSec == NullSection) {
4663 reportWarning(" '" + FileStr + "': section " + StackSizeSectionName +
4664 " does not have a corresponding "
4665 "relocation section");
4666 continue;
4669 // A .stack_sizes section header's sh_link field is supposed to point
4670 // to the section that contains the functions whose stack sizes are
4671 // described in it.
4672 const Elf_Shdr *StackSizesELFSec =
4673 Obj->getSection(StackSizesSec.getRawDataRefImpl());
4674 const SectionRef FunctionSec = toSectionRef<ELFT>(
4675 Obj, unwrapOrError(this->FileName,
4676 EF->getSection(StackSizesELFSec->sh_link)));
4678 bool (*IsSupportedFn)(uint64_t);
4679 RelocationResolver Resolver;
4680 std::tie(IsSupportedFn, Resolver) = getRelocationResolver(*Obj);
4681 auto Contents = unwrapOrError(this->FileName, StackSizesSec.getContents());
4682 DataExtractor Data(
4683 StringRef(reinterpret_cast<const char *>(Contents.data()),
4684 Contents.size()),
4685 Obj->isLittleEndian(), sizeof(Elf_Addr));
4686 for (const RelocationRef &Reloc : RelocSec.relocations()) {
4687 if (!IsSupportedFn(Reloc.getType())) {
4688 StringRef RelocSectionName;
4689 RelocSec.getName(RelocSectionName);
4690 StringRef RelocName = EF->getRelocationTypeName(Reloc.getType());
4691 reportError(
4692 FileStr,
4693 createStringError(object_error::parse_failed,
4694 "unsupported relocation type in section %s: %s",
4695 RelocSectionName.data(), RelocName.data()));
4697 this->printStackSize(Obj, Reloc, FunctionSec, StackSizeSectionName,
4698 Resolver, Data);
4703 template <class ELFT>
4704 void GNUStyle<ELFT>::printStackSizes(const ELFObjectFile<ELFT> *Obj) {
4705 bool HeaderHasBeenPrinted = false;
4706 auto PrintHeader = [&]() {
4707 if (HeaderHasBeenPrinted)
4708 return;
4709 OS << "\nStack Sizes:\n";
4710 OS.PadToColumn(9);
4711 OS << "Size";
4712 OS.PadToColumn(18);
4713 OS << "Function\n";
4714 HeaderHasBeenPrinted = true;
4717 // For non-relocatable objects, look directly for sections whose name starts
4718 // with .stack_sizes and process the contents.
4719 if (Obj->isRelocatableObject())
4720 this->printRelocatableStackSizes(Obj, PrintHeader);
4721 else
4722 this->printNonRelocatableStackSizes(Obj, PrintHeader);
4725 template <class ELFT>
4726 void GNUStyle<ELFT>::printMipsGOT(const MipsGOTParser<ELFT> &Parser) {
4727 size_t Bias = ELFT::Is64Bits ? 8 : 0;
4728 auto PrintEntry = [&](const Elf_Addr *E, StringRef Purpose) {
4729 OS.PadToColumn(2);
4730 OS << format_hex_no_prefix(Parser.getGotAddress(E), 8 + Bias);
4731 OS.PadToColumn(11 + Bias);
4732 OS << format_decimal(Parser.getGotOffset(E), 6) << "(gp)";
4733 OS.PadToColumn(22 + Bias);
4734 OS << format_hex_no_prefix(*E, 8 + Bias);
4735 OS.PadToColumn(31 + 2 * Bias);
4736 OS << Purpose << "\n";
4739 OS << (Parser.IsStatic ? "Static GOT:\n" : "Primary GOT:\n");
4740 OS << " Canonical gp value: "
4741 << format_hex_no_prefix(Parser.getGp(), 8 + Bias) << "\n\n";
4743 OS << " Reserved entries:\n";
4744 if (ELFT::Is64Bits)
4745 OS << " Address Access Initial Purpose\n";
4746 else
4747 OS << " Address Access Initial Purpose\n";
4748 PrintEntry(Parser.getGotLazyResolver(), "Lazy resolver");
4749 if (Parser.getGotModulePointer())
4750 PrintEntry(Parser.getGotModulePointer(), "Module pointer (GNU extension)");
4752 if (!Parser.getLocalEntries().empty()) {
4753 OS << "\n";
4754 OS << " Local entries:\n";
4755 if (ELFT::Is64Bits)
4756 OS << " Address Access Initial\n";
4757 else
4758 OS << " Address Access Initial\n";
4759 for (auto &E : Parser.getLocalEntries())
4760 PrintEntry(&E, "");
4763 if (Parser.IsStatic)
4764 return;
4766 if (!Parser.getGlobalEntries().empty()) {
4767 OS << "\n";
4768 OS << " Global entries:\n";
4769 if (ELFT::Is64Bits)
4770 OS << " Address Access Initial Sym.Val."
4771 << " Type Ndx Name\n";
4772 else
4773 OS << " Address Access Initial Sym.Val. Type Ndx Name\n";
4774 for (auto &E : Parser.getGlobalEntries()) {
4775 const Elf_Sym *Sym = Parser.getGotSym(&E);
4776 std::string SymName = this->dumper()->getFullSymbolName(
4777 Sym, this->dumper()->getDynamicStringTable(), false);
4779 OS.PadToColumn(2);
4780 OS << to_string(format_hex_no_prefix(Parser.getGotAddress(&E), 8 + Bias));
4781 OS.PadToColumn(11 + Bias);
4782 OS << to_string(format_decimal(Parser.getGotOffset(&E), 6)) + "(gp)";
4783 OS.PadToColumn(22 + Bias);
4784 OS << to_string(format_hex_no_prefix(E, 8 + Bias));
4785 OS.PadToColumn(31 + 2 * Bias);
4786 OS << to_string(format_hex_no_prefix(Sym->st_value, 8 + Bias));
4787 OS.PadToColumn(40 + 3 * Bias);
4788 OS << printEnum(Sym->getType(), makeArrayRef(ElfSymbolTypes));
4789 OS.PadToColumn(48 + 3 * Bias);
4790 OS << getSymbolSectionNdx(Parser.Obj, Sym,
4791 this->dumper()->dynamic_symbols().begin());
4792 OS.PadToColumn(52 + 3 * Bias);
4793 OS << SymName << "\n";
4797 if (!Parser.getOtherEntries().empty())
4798 OS << "\n Number of TLS and multi-GOT entries "
4799 << Parser.getOtherEntries().size() << "\n";
4802 template <class ELFT>
4803 void GNUStyle<ELFT>::printMipsPLT(const MipsGOTParser<ELFT> &Parser) {
4804 size_t Bias = ELFT::Is64Bits ? 8 : 0;
4805 auto PrintEntry = [&](const Elf_Addr *E, StringRef Purpose) {
4806 OS.PadToColumn(2);
4807 OS << format_hex_no_prefix(Parser.getPltAddress(E), 8 + Bias);
4808 OS.PadToColumn(11 + Bias);
4809 OS << format_hex_no_prefix(*E, 8 + Bias);
4810 OS.PadToColumn(20 + 2 * Bias);
4811 OS << Purpose << "\n";
4814 OS << "PLT GOT:\n\n";
4816 OS << " Reserved entries:\n";
4817 OS << " Address Initial Purpose\n";
4818 PrintEntry(Parser.getPltLazyResolver(), "PLT lazy resolver");
4819 if (Parser.getPltModulePointer())
4820 PrintEntry(Parser.getPltModulePointer(), "Module pointer");
4822 if (!Parser.getPltEntries().empty()) {
4823 OS << "\n";
4824 OS << " Entries:\n";
4825 OS << " Address Initial Sym.Val. Type Ndx Name\n";
4826 for (auto &E : Parser.getPltEntries()) {
4827 const Elf_Sym *Sym = Parser.getPltSym(&E);
4828 std::string SymName = this->dumper()->getFullSymbolName(
4829 Sym, this->dumper()->getDynamicStringTable(), false);
4831 OS.PadToColumn(2);
4832 OS << to_string(format_hex_no_prefix(Parser.getPltAddress(&E), 8 + Bias));
4833 OS.PadToColumn(11 + Bias);
4834 OS << to_string(format_hex_no_prefix(E, 8 + Bias));
4835 OS.PadToColumn(20 + 2 * Bias);
4836 OS << to_string(format_hex_no_prefix(Sym->st_value, 8 + Bias));
4837 OS.PadToColumn(29 + 3 * Bias);
4838 OS << printEnum(Sym->getType(), makeArrayRef(ElfSymbolTypes));
4839 OS.PadToColumn(37 + 3 * Bias);
4840 OS << getSymbolSectionNdx(Parser.Obj, Sym,
4841 this->dumper()->dynamic_symbols().begin());
4842 OS.PadToColumn(41 + 3 * Bias);
4843 OS << SymName << "\n";
4848 template <class ELFT> void LLVMStyle<ELFT>::printFileHeaders(const ELFO *Obj) {
4849 const Elf_Ehdr *E = Obj->getHeader();
4851 DictScope D(W, "ElfHeader");
4853 DictScope D(W, "Ident");
4854 W.printBinary("Magic", makeArrayRef(E->e_ident).slice(ELF::EI_MAG0, 4));
4855 W.printEnum("Class", E->e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
4856 W.printEnum("DataEncoding", E->e_ident[ELF::EI_DATA],
4857 makeArrayRef(ElfDataEncoding));
4858 W.printNumber("FileVersion", E->e_ident[ELF::EI_VERSION]);
4860 auto OSABI = makeArrayRef(ElfOSABI);
4861 if (E->e_ident[ELF::EI_OSABI] >= ELF::ELFOSABI_FIRST_ARCH &&
4862 E->e_ident[ELF::EI_OSABI] <= ELF::ELFOSABI_LAST_ARCH) {
4863 switch (E->e_machine) {
4864 case ELF::EM_AMDGPU:
4865 OSABI = makeArrayRef(AMDGPUElfOSABI);
4866 break;
4867 case ELF::EM_ARM:
4868 OSABI = makeArrayRef(ARMElfOSABI);
4869 break;
4870 case ELF::EM_TI_C6000:
4871 OSABI = makeArrayRef(C6000ElfOSABI);
4872 break;
4875 W.printEnum("OS/ABI", E->e_ident[ELF::EI_OSABI], OSABI);
4876 W.printNumber("ABIVersion", E->e_ident[ELF::EI_ABIVERSION]);
4877 W.printBinary("Unused", makeArrayRef(E->e_ident).slice(ELF::EI_PAD));
4880 W.printEnum("Type", E->e_type, makeArrayRef(ElfObjectFileType));
4881 W.printEnum("Machine", E->e_machine, makeArrayRef(ElfMachineType));
4882 W.printNumber("Version", E->e_version);
4883 W.printHex("Entry", E->e_entry);
4884 W.printHex("ProgramHeaderOffset", E->e_phoff);
4885 W.printHex("SectionHeaderOffset", E->e_shoff);
4886 if (E->e_machine == EM_MIPS)
4887 W.printFlags("Flags", E->e_flags, makeArrayRef(ElfHeaderMipsFlags),
4888 unsigned(ELF::EF_MIPS_ARCH), unsigned(ELF::EF_MIPS_ABI),
4889 unsigned(ELF::EF_MIPS_MACH));
4890 else if (E->e_machine == EM_AMDGPU)
4891 W.printFlags("Flags", E->e_flags, makeArrayRef(ElfHeaderAMDGPUFlags),
4892 unsigned(ELF::EF_AMDGPU_MACH));
4893 else if (E->e_machine == EM_RISCV)
4894 W.printFlags("Flags", E->e_flags, makeArrayRef(ElfHeaderRISCVFlags));
4895 else
4896 W.printFlags("Flags", E->e_flags);
4897 W.printNumber("HeaderSize", E->e_ehsize);
4898 W.printNumber("ProgramHeaderEntrySize", E->e_phentsize);
4899 W.printNumber("ProgramHeaderCount", E->e_phnum);
4900 W.printNumber("SectionHeaderEntrySize", E->e_shentsize);
4901 W.printString("SectionHeaderCount",
4902 getSectionHeadersNumString(Obj, this->FileName));
4903 W.printString("StringTableSectionIndex",
4904 getSectionHeaderTableIndexString(Obj, this->FileName));
4908 template <class ELFT>
4909 void LLVMStyle<ELFT>::printGroupSections(const ELFO *Obj) {
4910 DictScope Lists(W, "Groups");
4911 std::vector<GroupSection> V = getGroups<ELFT>(Obj, this->FileName);
4912 DenseMap<uint64_t, const GroupSection *> Map = mapSectionsToGroups(V);
4913 for (const GroupSection &G : V) {
4914 DictScope D(W, "Group");
4915 W.printNumber("Name", G.Name, G.ShName);
4916 W.printNumber("Index", G.Index);
4917 W.printNumber("Link", G.Link);
4918 W.printNumber("Info", G.Info);
4919 W.printHex("Type", getGroupType(G.Type), G.Type);
4920 W.startLine() << "Signature: " << G.Signature << "\n";
4922 ListScope L(W, "Section(s) in group");
4923 for (const GroupMember &GM : G.Members) {
4924 const GroupSection *MainGroup = Map[GM.Index];
4925 if (MainGroup != &G) {
4926 W.flush();
4927 errs() << "Error: " << GM.Name << " (" << GM.Index
4928 << ") in a group " + G.Name + " (" << G.Index
4929 << ") is already in a group " + MainGroup->Name + " ("
4930 << MainGroup->Index << ")\n";
4931 errs().flush();
4932 continue;
4934 W.startLine() << GM.Name << " (" << GM.Index << ")\n";
4938 if (V.empty())
4939 W.startLine() << "There are no group sections in the file.\n";
4942 template <class ELFT> void LLVMStyle<ELFT>::printRelocations(const ELFO *Obj) {
4943 ListScope D(W, "Relocations");
4945 int SectionNumber = -1;
4946 for (const Elf_Shdr &Sec : unwrapOrError(this->FileName, Obj->sections())) {
4947 ++SectionNumber;
4949 if (Sec.sh_type != ELF::SHT_REL && Sec.sh_type != ELF::SHT_RELA &&
4950 Sec.sh_type != ELF::SHT_RELR && Sec.sh_type != ELF::SHT_ANDROID_REL &&
4951 Sec.sh_type != ELF::SHT_ANDROID_RELA &&
4952 Sec.sh_type != ELF::SHT_ANDROID_RELR)
4953 continue;
4955 StringRef Name = unwrapOrError(this->FileName, Obj->getSectionName(&Sec));
4957 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
4958 W.indent();
4960 printRelocations(&Sec, Obj);
4962 W.unindent();
4963 W.startLine() << "}\n";
4967 template <class ELFT>
4968 void LLVMStyle<ELFT>::printRelocations(const Elf_Shdr *Sec, const ELFO *Obj) {
4969 const Elf_Shdr *SymTab =
4970 unwrapOrError(this->FileName, Obj->getSection(Sec->sh_link));
4972 switch (Sec->sh_type) {
4973 case ELF::SHT_REL:
4974 for (const Elf_Rel &R : unwrapOrError(this->FileName, Obj->rels(Sec))) {
4975 Elf_Rela Rela;
4976 Rela.r_offset = R.r_offset;
4977 Rela.r_info = R.r_info;
4978 Rela.r_addend = 0;
4979 printRelocation(Obj, Rela, SymTab);
4981 break;
4982 case ELF::SHT_RELA:
4983 for (const Elf_Rela &R : unwrapOrError(this->FileName, Obj->relas(Sec)))
4984 printRelocation(Obj, R, SymTab);
4985 break;
4986 case ELF::SHT_RELR:
4987 case ELF::SHT_ANDROID_RELR: {
4988 Elf_Relr_Range Relrs = unwrapOrError(this->FileName, Obj->relrs(Sec));
4989 if (opts::RawRelr) {
4990 for (const Elf_Relr &R : Relrs)
4991 W.startLine() << W.hex(R) << "\n";
4992 } else {
4993 std::vector<Elf_Rela> RelrRelas =
4994 unwrapOrError(this->FileName, Obj->decode_relrs(Relrs));
4995 for (const Elf_Rela &R : RelrRelas)
4996 printRelocation(Obj, R, SymTab);
4998 break;
5000 case ELF::SHT_ANDROID_REL:
5001 case ELF::SHT_ANDROID_RELA:
5002 for (const Elf_Rela &R :
5003 unwrapOrError(this->FileName, Obj->android_relas(Sec)))
5004 printRelocation(Obj, R, SymTab);
5005 break;
5009 template <class ELFT>
5010 void LLVMStyle<ELFT>::printRelocation(const ELFO *Obj, Elf_Rela Rel,
5011 const Elf_Shdr *SymTab) {
5012 SmallString<32> RelocName;
5013 Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
5014 std::string TargetName;
5015 const Elf_Sym *Sym =
5016 unwrapOrError(this->FileName, Obj->getRelocationSymbol(&Rel, SymTab));
5017 if (Sym && Sym->getType() == ELF::STT_SECTION) {
5018 const Elf_Shdr *Sec = unwrapOrError(
5019 this->FileName,
5020 Obj->getSection(Sym, SymTab, this->dumper()->getShndxTable()));
5021 TargetName = unwrapOrError(this->FileName, Obj->getSectionName(Sec));
5022 } else if (Sym) {
5023 StringRef StrTable =
5024 unwrapOrError(this->FileName, Obj->getStringTableForSymtab(*SymTab));
5025 TargetName = this->dumper()->getFullSymbolName(
5026 Sym, StrTable, SymTab->sh_type == SHT_DYNSYM /* IsDynamic */);
5029 if (opts::ExpandRelocs) {
5030 DictScope Group(W, "Relocation");
5031 W.printHex("Offset", Rel.r_offset);
5032 W.printNumber("Type", RelocName, (int)Rel.getType(Obj->isMips64EL()));
5033 W.printNumber("Symbol", !TargetName.empty() ? TargetName : "-",
5034 Rel.getSymbol(Obj->isMips64EL()));
5035 W.printHex("Addend", Rel.r_addend);
5036 } else {
5037 raw_ostream &OS = W.startLine();
5038 OS << W.hex(Rel.r_offset) << " " << RelocName << " "
5039 << (!TargetName.empty() ? TargetName : "-") << " " << W.hex(Rel.r_addend)
5040 << "\n";
5044 template <class ELFT>
5045 void LLVMStyle<ELFT>::printSectionHeaders(const ELFO *Obj) {
5046 ListScope SectionsD(W, "Sections");
5048 int SectionIndex = -1;
5049 ArrayRef<Elf_Shdr> Sections = unwrapOrError(this->FileName, Obj->sections());
5050 const ELFObjectFile<ELFT> *ElfObj = this->dumper()->getElfObject();
5051 for (const Elf_Shdr &Sec : Sections) {
5052 StringRef Name = unwrapOrError(
5053 ElfObj->getFileName(), Obj->getSectionName(&Sec, this->WarningHandler));
5054 DictScope SectionD(W, "Section");
5055 W.printNumber("Index", ++SectionIndex);
5056 W.printNumber("Name", Name, Sec.sh_name);
5057 W.printHex(
5058 "Type",
5059 object::getELFSectionTypeName(Obj->getHeader()->e_machine, Sec.sh_type),
5060 Sec.sh_type);
5061 std::vector<EnumEntry<unsigned>> SectionFlags(std::begin(ElfSectionFlags),
5062 std::end(ElfSectionFlags));
5063 switch (Obj->getHeader()->e_machine) {
5064 case EM_ARM:
5065 SectionFlags.insert(SectionFlags.end(), std::begin(ElfARMSectionFlags),
5066 std::end(ElfARMSectionFlags));
5067 break;
5068 case EM_HEXAGON:
5069 SectionFlags.insert(SectionFlags.end(),
5070 std::begin(ElfHexagonSectionFlags),
5071 std::end(ElfHexagonSectionFlags));
5072 break;
5073 case EM_MIPS:
5074 SectionFlags.insert(SectionFlags.end(), std::begin(ElfMipsSectionFlags),
5075 std::end(ElfMipsSectionFlags));
5076 break;
5077 case EM_X86_64:
5078 SectionFlags.insert(SectionFlags.end(), std::begin(ElfX86_64SectionFlags),
5079 std::end(ElfX86_64SectionFlags));
5080 break;
5081 case EM_XCORE:
5082 SectionFlags.insert(SectionFlags.end(), std::begin(ElfXCoreSectionFlags),
5083 std::end(ElfXCoreSectionFlags));
5084 break;
5085 default:
5086 // Nothing to do.
5087 break;
5089 W.printFlags("Flags", Sec.sh_flags, makeArrayRef(SectionFlags));
5090 W.printHex("Address", Sec.sh_addr);
5091 W.printHex("Offset", Sec.sh_offset);
5092 W.printNumber("Size", Sec.sh_size);
5093 W.printNumber("Link", Sec.sh_link);
5094 W.printNumber("Info", Sec.sh_info);
5095 W.printNumber("AddressAlignment", Sec.sh_addralign);
5096 W.printNumber("EntrySize", Sec.sh_entsize);
5098 if (opts::SectionRelocations) {
5099 ListScope D(W, "Relocations");
5100 printRelocations(&Sec, Obj);
5103 if (opts::SectionSymbols) {
5104 ListScope D(W, "Symbols");
5105 const Elf_Shdr *Symtab = this->dumper()->getDotSymtabSec();
5106 StringRef StrTable =
5107 unwrapOrError(this->FileName, Obj->getStringTableForSymtab(*Symtab));
5109 for (const Elf_Sym &Sym :
5110 unwrapOrError(this->FileName, Obj->symbols(Symtab))) {
5111 const Elf_Shdr *SymSec = unwrapOrError(
5112 this->FileName,
5113 Obj->getSection(&Sym, Symtab, this->dumper()->getShndxTable()));
5114 if (SymSec == &Sec)
5115 printSymbol(Obj, &Sym,
5116 unwrapOrError(this->FileName, Obj->symbols(Symtab)).begin(),
5117 StrTable, false);
5121 if (opts::SectionData && Sec.sh_type != ELF::SHT_NOBITS) {
5122 ArrayRef<uint8_t> Data =
5123 unwrapOrError(this->FileName, Obj->getSectionContents(&Sec));
5124 W.printBinaryBlock(
5125 "SectionData",
5126 StringRef(reinterpret_cast<const char *>(Data.data()), Data.size()));
5131 template <class ELFT>
5132 void LLVMStyle<ELFT>::printSymbol(const ELFO *Obj, const Elf_Sym *Symbol,
5133 const Elf_Sym *First, StringRef StrTable,
5134 bool IsDynamic) {
5135 unsigned SectionIndex = 0;
5136 StringRef SectionName;
5137 this->dumper()->getSectionNameIndex(Symbol, First, SectionName, SectionIndex);
5138 std::string FullSymbolName =
5139 this->dumper()->getFullSymbolName(Symbol, StrTable, IsDynamic);
5140 unsigned char SymbolType = Symbol->getType();
5142 DictScope D(W, "Symbol");
5143 W.printNumber("Name", FullSymbolName, Symbol->st_name);
5144 W.printHex("Value", Symbol->st_value);
5145 W.printNumber("Size", Symbol->st_size);
5146 W.printEnum("Binding", Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
5147 if (Obj->getHeader()->e_machine == ELF::EM_AMDGPU &&
5148 SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
5149 W.printEnum("Type", SymbolType, makeArrayRef(AMDGPUSymbolTypes));
5150 else
5151 W.printEnum("Type", SymbolType, makeArrayRef(ElfSymbolTypes));
5152 if (Symbol->st_other == 0)
5153 // Usually st_other flag is zero. Do not pollute the output
5154 // by flags enumeration in that case.
5155 W.printNumber("Other", 0);
5156 else {
5157 std::vector<EnumEntry<unsigned>> SymOtherFlags(std::begin(ElfSymOtherFlags),
5158 std::end(ElfSymOtherFlags));
5159 if (Obj->getHeader()->e_machine == EM_MIPS) {
5160 // Someones in their infinite wisdom decided to make STO_MIPS_MIPS16
5161 // flag overlapped with other ST_MIPS_xxx flags. So consider both
5162 // cases separately.
5163 if ((Symbol->st_other & STO_MIPS_MIPS16) == STO_MIPS_MIPS16)
5164 SymOtherFlags.insert(SymOtherFlags.end(),
5165 std::begin(ElfMips16SymOtherFlags),
5166 std::end(ElfMips16SymOtherFlags));
5167 else
5168 SymOtherFlags.insert(SymOtherFlags.end(),
5169 std::begin(ElfMipsSymOtherFlags),
5170 std::end(ElfMipsSymOtherFlags));
5172 W.printFlags("Other", Symbol->st_other, makeArrayRef(SymOtherFlags), 0x3u);
5174 W.printHex("Section", SectionName, SectionIndex);
5177 template <class ELFT>
5178 void LLVMStyle<ELFT>::printSymbols(const ELFO *Obj, bool PrintSymbols,
5179 bool PrintDynamicSymbols) {
5180 if (PrintSymbols)
5181 printSymbols(Obj);
5182 if (PrintDynamicSymbols)
5183 printDynamicSymbols(Obj);
5186 template <class ELFT> void LLVMStyle<ELFT>::printSymbols(const ELFO *Obj) {
5187 ListScope Group(W, "Symbols");
5188 this->dumper()->printSymbolsHelper(false);
5191 template <class ELFT>
5192 void LLVMStyle<ELFT>::printDynamicSymbols(const ELFO *Obj) {
5193 ListScope Group(W, "DynamicSymbols");
5194 this->dumper()->printSymbolsHelper(true);
5197 template <class ELFT> void LLVMStyle<ELFT>::printDynamic(const ELFFile<ELFT> *Obj) {
5198 Elf_Dyn_Range Table = this->dumper()->dynamic_table();
5199 if (Table.empty())
5200 return;
5202 raw_ostream &OS = W.getOStream();
5203 W.startLine() << "DynamicSection [ (" << Table.size() << " entries)\n";
5205 bool Is64 = ELFT::Is64Bits;
5206 if (Is64)
5207 W.startLine() << " Tag Type Name/Value\n";
5208 else
5209 W.startLine() << " Tag Type Name/Value\n";
5210 for (auto Entry : Table) {
5211 uintX_t Tag = Entry.getTag();
5212 W.startLine() << " " << format_hex(Tag, Is64 ? 18 : 10, true) << " "
5213 << format("%-21s",
5214 getTypeString(Obj->getHeader()->e_machine, Tag));
5215 this->dumper()->printDynamicEntry(OS, Tag, Entry.getVal());
5216 OS << "\n";
5219 W.startLine() << "]\n";
5222 template <class ELFT>
5223 void LLVMStyle<ELFT>::printDynamicRelocations(const ELFO *Obj) {
5224 const DynRegionInfo &DynRelRegion = this->dumper()->getDynRelRegion();
5225 const DynRegionInfo &DynRelaRegion = this->dumper()->getDynRelaRegion();
5226 const DynRegionInfo &DynRelrRegion = this->dumper()->getDynRelrRegion();
5227 const DynRegionInfo &DynPLTRelRegion = this->dumper()->getDynPLTRelRegion();
5228 if (DynRelRegion.Size && DynRelaRegion.Size)
5229 report_fatal_error("There are both REL and RELA dynamic relocations");
5230 W.startLine() << "Dynamic Relocations {\n";
5231 W.indent();
5232 if (DynRelaRegion.Size > 0)
5233 for (const Elf_Rela &Rela : this->dumper()->dyn_relas())
5234 printDynamicRelocation(Obj, Rela);
5235 else
5236 for (const Elf_Rel &Rel : this->dumper()->dyn_rels()) {
5237 Elf_Rela Rela;
5238 Rela.r_offset = Rel.r_offset;
5239 Rela.r_info = Rel.r_info;
5240 Rela.r_addend = 0;
5241 printDynamicRelocation(Obj, Rela);
5243 if (DynRelrRegion.Size > 0) {
5244 Elf_Relr_Range Relrs = this->dumper()->dyn_relrs();
5245 std::vector<Elf_Rela> RelrRelas =
5246 unwrapOrError(this->FileName, Obj->decode_relrs(Relrs));
5247 for (const Elf_Rela &Rela : RelrRelas)
5248 printDynamicRelocation(Obj, Rela);
5250 if (DynPLTRelRegion.EntSize == sizeof(Elf_Rela))
5251 for (const Elf_Rela &Rela : DynPLTRelRegion.getAsArrayRef<Elf_Rela>())
5252 printDynamicRelocation(Obj, Rela);
5253 else
5254 for (const Elf_Rel &Rel : DynPLTRelRegion.getAsArrayRef<Elf_Rel>()) {
5255 Elf_Rela Rela;
5256 Rela.r_offset = Rel.r_offset;
5257 Rela.r_info = Rel.r_info;
5258 Rela.r_addend = 0;
5259 printDynamicRelocation(Obj, Rela);
5261 W.unindent();
5262 W.startLine() << "}\n";
5265 template <class ELFT>
5266 void LLVMStyle<ELFT>::printDynamicRelocation(const ELFO *Obj, Elf_Rela Rel) {
5267 SmallString<32> RelocName;
5268 Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
5269 std::string SymbolName;
5270 uint32_t SymIndex = Rel.getSymbol(Obj->isMips64EL());
5271 const Elf_Sym *Sym = this->dumper()->dynamic_symbols().begin() + SymIndex;
5272 SymbolName = maybeDemangle(unwrapOrError(
5273 this->FileName, Sym->getName(this->dumper()->getDynamicStringTable())));
5274 if (opts::ExpandRelocs) {
5275 DictScope Group(W, "Relocation");
5276 W.printHex("Offset", Rel.r_offset);
5277 W.printNumber("Type", RelocName, (int)Rel.getType(Obj->isMips64EL()));
5278 W.printString("Symbol", !SymbolName.empty() ? SymbolName : "-");
5279 W.printHex("Addend", Rel.r_addend);
5280 } else {
5281 raw_ostream &OS = W.startLine();
5282 OS << W.hex(Rel.r_offset) << " " << RelocName << " "
5283 << (!SymbolName.empty() ? SymbolName : "-") << " " << W.hex(Rel.r_addend)
5284 << "\n";
5288 template <class ELFT>
5289 void LLVMStyle<ELFT>::printProgramHeaders(
5290 const ELFO *Obj, bool PrintProgramHeaders,
5291 cl::boolOrDefault PrintSectionMapping) {
5292 if (PrintProgramHeaders)
5293 printProgramHeaders(Obj);
5294 if (PrintSectionMapping == cl::BOU_TRUE)
5295 printSectionMapping(Obj);
5298 template <class ELFT>
5299 void LLVMStyle<ELFT>::printProgramHeaders(const ELFO *Obj) {
5300 ListScope L(W, "ProgramHeaders");
5302 for (const Elf_Phdr &Phdr :
5303 unwrapOrError(this->FileName, Obj->program_headers())) {
5304 DictScope P(W, "ProgramHeader");
5305 W.printHex("Type",
5306 getElfSegmentType(Obj->getHeader()->e_machine, Phdr.p_type),
5307 Phdr.p_type);
5308 W.printHex("Offset", Phdr.p_offset);
5309 W.printHex("VirtualAddress", Phdr.p_vaddr);
5310 W.printHex("PhysicalAddress", Phdr.p_paddr);
5311 W.printNumber("FileSize", Phdr.p_filesz);
5312 W.printNumber("MemSize", Phdr.p_memsz);
5313 W.printFlags("Flags", Phdr.p_flags, makeArrayRef(ElfSegmentFlags));
5314 W.printNumber("Alignment", Phdr.p_align);
5318 template <class ELFT>
5319 void LLVMStyle<ELFT>::printVersionSymbolSection(const ELFFile<ELFT> *Obj,
5320 const Elf_Shdr *Sec) {
5321 DictScope SS(W, "Version symbols");
5322 if (!Sec)
5323 return;
5325 StringRef SecName = unwrapOrError(this->FileName, Obj->getSectionName(Sec));
5326 W.printNumber("Section Name", SecName, Sec->sh_name);
5327 W.printHex("Address", Sec->sh_addr);
5328 W.printHex("Offset", Sec->sh_offset);
5329 W.printNumber("Link", Sec->sh_link);
5331 const uint8_t *VersymBuf =
5332 reinterpret_cast<const uint8_t *>(Obj->base() + Sec->sh_offset);
5333 const ELFDumper<ELFT> *Dumper = this->dumper();
5334 StringRef StrTable = Dumper->getDynamicStringTable();
5336 // Same number of entries in the dynamic symbol table (DT_SYMTAB).
5337 ListScope Syms(W, "Symbols");
5338 for (const Elf_Sym &Sym : Dumper->dynamic_symbols()) {
5339 DictScope S(W, "Symbol");
5340 const Elf_Versym *Versym = reinterpret_cast<const Elf_Versym *>(VersymBuf);
5341 std::string FullSymbolName =
5342 Dumper->getFullSymbolName(&Sym, StrTable, true /* IsDynamic */);
5343 W.printNumber("Version", Versym->vs_index & VERSYM_VERSION);
5344 W.printString("Name", FullSymbolName);
5345 VersymBuf += sizeof(Elf_Versym);
5349 template <class ELFT>
5350 void LLVMStyle<ELFT>::printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
5351 const Elf_Shdr *Sec) {
5352 DictScope SD(W, "SHT_GNU_verdef");
5353 if (!Sec)
5354 return;
5356 const uint8_t *SecStartAddress =
5357 reinterpret_cast<const uint8_t *>(Obj->base() + Sec->sh_offset);
5358 const uint8_t *SecEndAddress = SecStartAddress + Sec->sh_size;
5359 const uint8_t *VerdefBuf = SecStartAddress;
5360 const Elf_Shdr *StrTab =
5361 unwrapOrError(this->FileName, Obj->getSection(Sec->sh_link));
5363 unsigned VerDefsNum = Sec->sh_info;
5364 while (VerDefsNum--) {
5365 if (VerdefBuf + sizeof(Elf_Verdef) > SecEndAddress)
5366 // FIXME: report_fatal_error is not a good way to report error. We should
5367 // emit a parsing error here and below.
5368 report_fatal_error("invalid offset in the section");
5370 const Elf_Verdef *Verdef = reinterpret_cast<const Elf_Verdef *>(VerdefBuf);
5371 DictScope Def(W, "Definition");
5372 W.printNumber("Version", Verdef->vd_version);
5373 W.printEnum("Flags", Verdef->vd_flags, makeArrayRef(SymVersionFlags));
5374 W.printNumber("Index", Verdef->vd_ndx);
5375 W.printNumber("Hash", Verdef->vd_hash);
5376 W.printString("Name", StringRef(reinterpret_cast<const char *>(
5377 Obj->base() + StrTab->sh_offset +
5378 Verdef->getAux()->vda_name)));
5379 if (!Verdef->vd_cnt)
5380 report_fatal_error("at least one definition string must exist");
5381 if (Verdef->vd_cnt > 2)
5382 report_fatal_error("more than one predecessor is not expected");
5384 if (Verdef->vd_cnt == 2) {
5385 const uint8_t *VerdauxBuf =
5386 VerdefBuf + Verdef->vd_aux + Verdef->getAux()->vda_next;
5387 const Elf_Verdaux *Verdaux =
5388 reinterpret_cast<const Elf_Verdaux *>(VerdauxBuf);
5389 W.printString("Predecessor",
5390 StringRef(reinterpret_cast<const char *>(
5391 Obj->base() + StrTab->sh_offset + Verdaux->vda_name)));
5393 VerdefBuf += Verdef->vd_next;
5397 template <class ELFT>
5398 void LLVMStyle<ELFT>::printVersionDependencySection(const ELFFile<ELFT> *Obj,
5399 const Elf_Shdr *Sec) {
5400 DictScope SD(W, "SHT_GNU_verneed");
5401 if (!Sec)
5402 return;
5404 const uint8_t *SecData =
5405 reinterpret_cast<const uint8_t *>(Obj->base() + Sec->sh_offset);
5406 const Elf_Shdr *StrTab =
5407 unwrapOrError(this->FileName, Obj->getSection(Sec->sh_link));
5409 const uint8_t *VerneedBuf = SecData;
5410 unsigned VerneedNum = Sec->sh_info;
5411 for (unsigned I = 0; I < VerneedNum; ++I) {
5412 const Elf_Verneed *Verneed =
5413 reinterpret_cast<const Elf_Verneed *>(VerneedBuf);
5414 DictScope Entry(W, "Dependency");
5415 W.printNumber("Version", Verneed->vn_version);
5416 W.printNumber("Count", Verneed->vn_cnt);
5417 W.printString("FileName",
5418 StringRef(reinterpret_cast<const char *>(
5419 Obj->base() + StrTab->sh_offset + Verneed->vn_file)));
5421 const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux;
5422 ListScope L(W, "Entries");
5423 for (unsigned J = 0; J < Verneed->vn_cnt; ++J) {
5424 const Elf_Vernaux *Vernaux =
5425 reinterpret_cast<const Elf_Vernaux *>(VernauxBuf);
5426 DictScope Entry(W, "Entry");
5427 W.printNumber("Hash", Vernaux->vna_hash);
5428 W.printEnum("Flags", Vernaux->vna_flags, makeArrayRef(SymVersionFlags));
5429 W.printNumber("Index", Vernaux->vna_other);
5430 W.printString("Name",
5431 StringRef(reinterpret_cast<const char *>(
5432 Obj->base() + StrTab->sh_offset + Vernaux->vna_name)));
5433 VernauxBuf += Vernaux->vna_next;
5435 VerneedBuf += Verneed->vn_next;
5439 template <class ELFT>
5440 void LLVMStyle<ELFT>::printHashHistogram(const ELFFile<ELFT> *Obj) {
5441 W.startLine() << "Hash Histogram not implemented!\n";
5444 template <class ELFT>
5445 void LLVMStyle<ELFT>::printCGProfile(const ELFFile<ELFT> *Obj) {
5446 ListScope L(W, "CGProfile");
5447 if (!this->dumper()->getDotCGProfileSec())
5448 return;
5449 auto CGProfile = unwrapOrError(
5450 this->FileName, Obj->template getSectionContentsAsArray<Elf_CGProfile>(
5451 this->dumper()->getDotCGProfileSec()));
5452 for (const Elf_CGProfile &CGPE : CGProfile) {
5453 DictScope D(W, "CGProfileEntry");
5454 W.printNumber("From", this->dumper()->getStaticSymbolName(CGPE.cgp_from),
5455 CGPE.cgp_from);
5456 W.printNumber("To", this->dumper()->getStaticSymbolName(CGPE.cgp_to),
5457 CGPE.cgp_to);
5458 W.printNumber("Weight", CGPE.cgp_weight);
5462 template <class ELFT>
5463 void LLVMStyle<ELFT>::printAddrsig(const ELFFile<ELFT> *Obj) {
5464 ListScope L(W, "Addrsig");
5465 if (!this->dumper()->getDotAddrsigSec())
5466 return;
5467 ArrayRef<uint8_t> Contents = unwrapOrError(
5468 this->FileName,
5469 Obj->getSectionContents(this->dumper()->getDotAddrsigSec()));
5470 const uint8_t *Cur = Contents.begin();
5471 const uint8_t *End = Contents.end();
5472 while (Cur != End) {
5473 unsigned Size;
5474 const char *Err;
5475 uint64_t SymIndex = decodeULEB128(Cur, &Size, End, &Err);
5476 if (Err)
5477 reportError(Err);
5478 W.printNumber("Sym", this->dumper()->getStaticSymbolName(SymIndex),
5479 SymIndex);
5480 Cur += Size;
5484 template <typename ELFT>
5485 static void printGNUNoteLLVMStyle(uint32_t NoteType, ArrayRef<uint8_t> Desc,
5486 ScopedPrinter &W) {
5487 switch (NoteType) {
5488 default:
5489 return;
5490 case ELF::NT_GNU_ABI_TAG: {
5491 const GNUAbiTag &AbiTag = getGNUAbiTag<ELFT>(Desc);
5492 if (!AbiTag.IsValid) {
5493 W.printString("ABI", "<corrupt GNU_ABI_TAG>");
5494 } else {
5495 W.printString("OS", AbiTag.OSName);
5496 W.printString("ABI", AbiTag.ABI);
5498 break;
5500 case ELF::NT_GNU_BUILD_ID: {
5501 W.printString("Build ID", getGNUBuildId(Desc));
5502 break;
5504 case ELF::NT_GNU_GOLD_VERSION:
5505 W.printString("Version", getGNUGoldVersion(Desc));
5506 break;
5507 case ELF::NT_GNU_PROPERTY_TYPE_0:
5508 ListScope D(W, "Property");
5509 for (const auto &Property : getGNUPropertyList<ELFT>(Desc))
5510 W.printString(Property);
5511 break;
5515 template <class ELFT>
5516 void LLVMStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
5517 ListScope L(W, "Notes");
5519 auto PrintHeader = [&](const typename ELFT::Off Offset,
5520 const typename ELFT::Addr Size) {
5521 W.printHex("Offset", Offset);
5522 W.printHex("Size", Size);
5525 auto ProcessNote = [&](const Elf_Note &Note) {
5526 DictScope D2(W, "Note");
5527 StringRef Name = Note.getName();
5528 ArrayRef<uint8_t> Descriptor = Note.getDesc();
5529 Elf_Word Type = Note.getType();
5531 W.printString("Owner", Name);
5532 W.printHex("Data size", Descriptor.size());
5533 if (Name == "GNU") {
5534 W.printString("Type", getGNUNoteTypeName(Type));
5535 printGNUNoteLLVMStyle<ELFT>(Type, Descriptor, W);
5536 } else if (Name == "FreeBSD") {
5537 W.printString("Type", getFreeBSDNoteTypeName(Type));
5538 } else if (Name == "AMD") {
5539 W.printString("Type", getAMDNoteTypeName(Type));
5540 const AMDNote N = getAMDNote<ELFT>(Type, Descriptor);
5541 if (!N.Type.empty())
5542 W.printString(N.Type, N.Value);
5543 } else if (Name == "AMDGPU") {
5544 W.printString("Type", getAMDGPUNoteTypeName(Type));
5545 const AMDGPUNote N = getAMDGPUNote<ELFT>(Type, Descriptor);
5546 if (!N.Type.empty())
5547 W.printString(N.Type, N.Value);
5548 } else {
5549 StringRef NoteType = Obj->getHeader()->e_type == ELF::ET_CORE
5550 ? getCoreNoteTypeName(Type)
5551 : getGenericNoteTypeName(Type);
5552 if (!NoteType.empty())
5553 W.printString("Type", NoteType);
5554 else
5555 W.printString("Type",
5556 "Unknown (" + to_string(format_hex(Type, 10)) + ")");
5560 if (Obj->getHeader()->e_type == ELF::ET_CORE) {
5561 for (const auto &P :
5562 unwrapOrError(this->FileName, Obj->program_headers())) {
5563 if (P.p_type != PT_NOTE)
5564 continue;
5565 DictScope D(W, "NoteSection");
5566 PrintHeader(P.p_offset, P.p_filesz);
5567 Error Err = Error::success();
5568 for (const auto &Note : Obj->notes(P, Err))
5569 ProcessNote(Note);
5570 if (Err)
5571 error(std::move(Err));
5573 } else {
5574 for (const auto &S : unwrapOrError(this->FileName, Obj->sections())) {
5575 if (S.sh_type != SHT_NOTE)
5576 continue;
5577 DictScope D(W, "NoteSection");
5578 PrintHeader(S.sh_offset, S.sh_size);
5579 Error Err = Error::success();
5580 for (const auto &Note : Obj->notes(S, Err))
5581 ProcessNote(Note);
5582 if (Err)
5583 error(std::move(Err));
5588 template <class ELFT>
5589 void LLVMStyle<ELFT>::printELFLinkerOptions(const ELFFile<ELFT> *Obj) {
5590 ListScope L(W, "LinkerOptions");
5592 for (const Elf_Shdr &Shdr : unwrapOrError(this->FileName, Obj->sections())) {
5593 if (Shdr.sh_type != ELF::SHT_LLVM_LINKER_OPTIONS)
5594 continue;
5596 ArrayRef<uint8_t> Contents =
5597 unwrapOrError(this->FileName, Obj->getSectionContents(&Shdr));
5598 for (const uint8_t *P = Contents.begin(), *E = Contents.end(); P < E; ) {
5599 StringRef Key = StringRef(reinterpret_cast<const char *>(P));
5600 StringRef Value =
5601 StringRef(reinterpret_cast<const char *>(P) + Key.size() + 1);
5603 W.printString(Key, Value);
5605 P = P + Key.size() + Value.size() + 2;
5610 template <class ELFT>
5611 void LLVMStyle<ELFT>::printStackSizes(const ELFObjectFile<ELFT> *Obj) {
5612 W.printString(
5613 "Dumping of stack sizes in LLVM style is not implemented yet\n");
5616 template <class ELFT>
5617 void LLVMStyle<ELFT>::printStackSizeEntry(uint64_t Size, StringRef FuncName) {
5618 // FIXME: Implement this function for LLVM-style dumping.
5621 template <class ELFT>
5622 void LLVMStyle<ELFT>::printMipsGOT(const MipsGOTParser<ELFT> &Parser) {
5623 auto PrintEntry = [&](const Elf_Addr *E) {
5624 W.printHex("Address", Parser.getGotAddress(E));
5625 W.printNumber("Access", Parser.getGotOffset(E));
5626 W.printHex("Initial", *E);
5629 DictScope GS(W, Parser.IsStatic ? "Static GOT" : "Primary GOT");
5631 W.printHex("Canonical gp value", Parser.getGp());
5633 ListScope RS(W, "Reserved entries");
5635 DictScope D(W, "Entry");
5636 PrintEntry(Parser.getGotLazyResolver());
5637 W.printString("Purpose", StringRef("Lazy resolver"));
5640 if (Parser.getGotModulePointer()) {
5641 DictScope D(W, "Entry");
5642 PrintEntry(Parser.getGotModulePointer());
5643 W.printString("Purpose", StringRef("Module pointer (GNU extension)"));
5647 ListScope LS(W, "Local entries");
5648 for (auto &E : Parser.getLocalEntries()) {
5649 DictScope D(W, "Entry");
5650 PrintEntry(&E);
5654 if (Parser.IsStatic)
5655 return;
5658 ListScope GS(W, "Global entries");
5659 for (auto &E : Parser.getGlobalEntries()) {
5660 DictScope D(W, "Entry");
5662 PrintEntry(&E);
5664 const Elf_Sym *Sym = Parser.getGotSym(&E);
5665 W.printHex("Value", Sym->st_value);
5666 W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes));
5668 unsigned SectionIndex = 0;
5669 StringRef SectionName;
5670 this->dumper()->getSectionNameIndex(
5671 Sym, this->dumper()->dynamic_symbols().begin(), SectionName,
5672 SectionIndex);
5673 W.printHex("Section", SectionName, SectionIndex);
5675 std::string SymName = this->dumper()->getFullSymbolName(
5676 Sym, this->dumper()->getDynamicStringTable(), true);
5677 W.printNumber("Name", SymName, Sym->st_name);
5681 W.printNumber("Number of TLS and multi-GOT entries",
5682 uint64_t(Parser.getOtherEntries().size()));
5685 template <class ELFT>
5686 void LLVMStyle<ELFT>::printMipsPLT(const MipsGOTParser<ELFT> &Parser) {
5687 auto PrintEntry = [&](const Elf_Addr *E) {
5688 W.printHex("Address", Parser.getPltAddress(E));
5689 W.printHex("Initial", *E);
5692 DictScope GS(W, "PLT GOT");
5695 ListScope RS(W, "Reserved entries");
5697 DictScope D(W, "Entry");
5698 PrintEntry(Parser.getPltLazyResolver());
5699 W.printString("Purpose", StringRef("PLT lazy resolver"));
5702 if (auto E = Parser.getPltModulePointer()) {
5703 DictScope D(W, "Entry");
5704 PrintEntry(E);
5705 W.printString("Purpose", StringRef("Module pointer"));
5709 ListScope LS(W, "Entries");
5710 for (auto &E : Parser.getPltEntries()) {
5711 DictScope D(W, "Entry");
5712 PrintEntry(&E);
5714 const Elf_Sym *Sym = Parser.getPltSym(&E);
5715 W.printHex("Value", Sym->st_value);
5716 W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes));
5718 unsigned SectionIndex = 0;
5719 StringRef SectionName;
5720 this->dumper()->getSectionNameIndex(
5721 Sym, this->dumper()->dynamic_symbols().begin(), SectionName,
5722 SectionIndex);
5723 W.printHex("Section", SectionName, SectionIndex);
5725 std::string SymName =
5726 this->dumper()->getFullSymbolName(Sym, Parser.getPltStrTable(), true);
5727 W.printNumber("Name", SymName, Sym->st_name);