1 //===- ELFDumper.cpp - ELF-specific dumper --------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements the ELF-specific dumper for llvm-readobj.
12 //===----------------------------------------------------------------------===//
14 #include "ARMEHABIPrinter.h"
15 #include "DwarfCFIEHPrinter.h"
16 #include "ObjDumper.h"
17 #include "StackMapPrinter.h"
18 #include "llvm-readobj.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/BitVector.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/DenseSet.h"
23 #include "llvm/ADT/MapVector.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/ADT/Twine.h"
30 #include "llvm/BinaryFormat/AMDGPUMetadataVerifier.h"
31 #include "llvm/BinaryFormat/ELF.h"
32 #include "llvm/BinaryFormat/MsgPackDocument.h"
33 #include "llvm/Demangle/Demangle.h"
34 #include "llvm/Object/Archive.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/MSP430AttributeParser.h"
54 #include "llvm/Support/MSP430Attributes.h"
55 #include "llvm/Support/MathExtras.h"
56 #include "llvm/Support/MipsABIFlags.h"
57 #include "llvm/Support/RISCVAttributeParser.h"
58 #include "llvm/Support/RISCVAttributes.h"
59 #include "llvm/Support/ScopedPrinter.h"
60 #include "llvm/Support/SystemZ/zOSSupport.h"
61 #include "llvm/Support/raw_ostream.h"
71 #include <system_error>
75 using namespace llvm::object
;
78 #define LLVM_READOBJ_ENUM_CASE(ns, enum) \
82 #define ENUM_ENT(enum, altName) \
83 { #enum, altName, ELF::enum }
85 #define ENUM_ENT_1(enum) \
86 { #enum, #enum, ELF::enum }
90 template <class ELFT
> struct RelSymbol
{
91 RelSymbol(const typename
ELFT::Sym
*S
, StringRef N
)
92 : Sym(S
), Name(N
.str()) {}
93 const typename
ELFT::Sym
*Sym
;
97 /// Represents a contiguous uniform range in the file. We cannot just create a
98 /// range directly because when creating one of these from the .dynamic table
99 /// the size, entity size and virtual address are different entries in arbitrary
100 /// order (DT_REL, DT_RELSZ, DT_RELENT for example).
101 struct DynRegionInfo
{
102 DynRegionInfo(const Binary
&Owner
, const ObjDumper
&D
)
103 : Obj(&Owner
), Dumper(&D
) {}
104 DynRegionInfo(const Binary
&Owner
, const ObjDumper
&D
, const uint8_t *A
,
105 uint64_t S
, uint64_t ES
)
106 : Addr(A
), Size(S
), EntSize(ES
), Obj(&Owner
), Dumper(&D
) {}
108 /// Address in current address space.
109 const uint8_t *Addr
= nullptr;
110 /// Size in bytes of the region.
112 /// Size of each entity in the region.
113 uint64_t EntSize
= 0;
115 /// Owner object. Used for error reporting.
117 /// Dumper used for error reporting.
118 const ObjDumper
*Dumper
;
119 /// Error prefix. Used for error reporting to provide more information.
121 /// Region size name. Used for error reporting.
122 StringRef SizePrintName
= "size";
123 /// Entry size name. Used for error reporting. If this field is empty, errors
124 /// will not mention the entry size.
125 StringRef EntSizePrintName
= "entry size";
127 template <typename Type
> ArrayRef
<Type
> getAsArrayRef() const {
128 const Type
*Start
= reinterpret_cast<const Type
*>(Addr
);
130 return {Start
, Start
};
132 const uint64_t Offset
=
133 Addr
- (const uint8_t *)Obj
->getMemoryBufferRef().getBufferStart();
134 const uint64_t ObjSize
= Obj
->getMemoryBufferRef().getBufferSize();
136 if (Size
> ObjSize
- Offset
) {
137 Dumper
->reportUniqueWarning(
138 "unable to read data at 0x" + Twine::utohexstr(Offset
) +
139 " of size 0x" + Twine::utohexstr(Size
) + " (" + SizePrintName
+
140 "): it goes past the end of the file of size 0x" +
141 Twine::utohexstr(ObjSize
));
142 return {Start
, Start
};
145 if (EntSize
== sizeof(Type
) && (Size
% EntSize
== 0))
146 return {Start
, Start
+ (Size
/ EntSize
)};
149 if (!Context
.empty())
150 Msg
+= Context
+ " has ";
152 Msg
+= ("invalid " + SizePrintName
+ " (0x" + Twine::utohexstr(Size
) + ")")
154 if (!EntSizePrintName
.empty())
156 (" or " + EntSizePrintName
+ " (0x" + Twine::utohexstr(EntSize
) + ")")
159 Dumper
->reportUniqueWarning(Msg
);
160 return {Start
, Start
};
169 struct GroupSection
{
171 std::string Signature
;
177 std::vector
<GroupMember
> Members
;
189 template <class ELFT
> class Relocation
{
191 Relocation(const typename
ELFT::Rel
&R
, bool IsMips64EL
)
192 : Type(R
.getType(IsMips64EL
)), Symbol(R
.getSymbol(IsMips64EL
)),
193 Offset(R
.r_offset
), Info(R
.r_info
) {}
195 Relocation(const typename
ELFT::Rela
&R
, bool IsMips64EL
)
196 : Relocation((const typename
ELFT::Rel
&)R
, IsMips64EL
) {
202 typename
ELFT::uint Offset
;
203 typename
ELFT::uint Info
;
204 std::optional
<int64_t> Addend
;
207 template <class ELFT
> class MipsGOTParser
;
209 template <typename ELFT
> class ELFDumper
: public ObjDumper
{
210 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT
)
213 ELFDumper(const object::ELFObjectFile
<ELFT
> &ObjF
, ScopedPrinter
&Writer
);
215 void printUnwindInfo() override
;
216 void printNeededLibraries() override
;
217 void printHashTable() override
;
218 void printGnuHashTable() override
;
219 void printLoadName() override
;
220 void printVersionInfo() override
;
221 void printArchSpecificInfo() override
;
222 void printStackMap() const override
;
223 void printMemtag() override
;
224 ArrayRef
<uint8_t> getMemtagGlobalsSectionContents(uint64_t ExpectedAddr
);
226 // Hash histogram shows statistics of how efficient the hash was for the
227 // dynamic symbol table. The table shows the number of hash buckets for
228 // different lengths of chains as an absolute number and percentage of the
229 // total buckets, and the cumulative coverage of symbols for each set of
231 void printHashHistograms() override
;
233 const object::ELFObjectFile
<ELFT
> &getElfObject() const { return ObjF
; };
235 std::string
describe(const Elf_Shdr
&Sec
) const;
237 unsigned getHashTableEntSize() const {
238 // EM_S390 and ELF::EM_ALPHA platforms use 8-bytes entries in SHT_HASH
239 // sections. This violates the ELF specification.
240 if (Obj
.getHeader().e_machine
== ELF::EM_S390
||
241 Obj
.getHeader().e_machine
== ELF::EM_ALPHA
)
246 std::vector
<EnumEntry
<unsigned>>
247 getOtherFlagsFromSymbol(const Elf_Ehdr
&Header
, const Elf_Sym
&Symbol
) const;
249 Elf_Dyn_Range
dynamic_table() const {
250 // A valid .dynamic section contains an array of entries terminated
251 // with a DT_NULL entry. However, sometimes the section content may
252 // continue past the DT_NULL entry, so to dump the section correctly,
253 // we first find the end of the entries by iterating over them.
254 Elf_Dyn_Range Table
= DynamicTable
.template getAsArrayRef
<Elf_Dyn
>();
257 while (Size
< Table
.size())
258 if (Table
[Size
++].getTag() == DT_NULL
)
261 return Table
.slice(0, Size
);
264 Elf_Sym_Range
dynamic_symbols() const {
266 return Elf_Sym_Range();
267 return DynSymRegion
->template getAsArrayRef
<Elf_Sym
>();
270 const Elf_Shdr
*findSectionByName(StringRef Name
) const;
272 StringRef
getDynamicStringTable() const { return DynamicStringTable
; }
275 virtual void printVersionSymbolSection(const Elf_Shdr
*Sec
) = 0;
276 virtual void printVersionDefinitionSection(const Elf_Shdr
*Sec
) = 0;
277 virtual void printVersionDependencySection(const Elf_Shdr
*Sec
) = 0;
280 printDependentLibsHelper(function_ref
<void(const Elf_Shdr
&)> OnSectionStart
,
281 function_ref
<void(StringRef
, uint64_t)> OnLibEntry
);
283 virtual void printRelRelaReloc(const Relocation
<ELFT
> &R
,
284 const RelSymbol
<ELFT
> &RelSym
) = 0;
285 virtual void printRelrReloc(const Elf_Relr
&R
) = 0;
286 virtual void printDynamicRelocHeader(unsigned Type
, StringRef Name
,
287 const DynRegionInfo
&Reg
) {}
288 void printReloc(const Relocation
<ELFT
> &R
, unsigned RelIndex
,
289 const Elf_Shdr
&Sec
, const Elf_Shdr
*SymTab
);
290 void printDynamicReloc(const Relocation
<ELFT
> &R
);
291 void printDynamicRelocationsHelper();
292 void printRelocationsHelper(const Elf_Shdr
&Sec
);
293 void forEachRelocationDo(
294 const Elf_Shdr
&Sec
, bool RawRelr
,
295 llvm::function_ref
<void(const Relocation
<ELFT
> &, unsigned,
296 const Elf_Shdr
&, const Elf_Shdr
*)>
298 llvm::function_ref
<void(const Elf_Relr
&)> RelrFn
);
300 virtual void printSymtabMessage(const Elf_Shdr
*Symtab
, size_t Offset
,
301 bool NonVisibilityBitsUsed
,
302 bool ExtraSymInfo
) const {};
303 virtual void printSymbol(const Elf_Sym
&Symbol
, unsigned SymIndex
,
304 DataRegion
<Elf_Word
> ShndxTable
,
305 std::optional
<StringRef
> StrTable
, bool IsDynamic
,
306 bool NonVisibilityBitsUsed
,
307 bool ExtraSymInfo
) const = 0;
309 virtual void printMipsABIFlags() = 0;
310 virtual void printMipsGOT(const MipsGOTParser
<ELFT
> &Parser
) = 0;
311 virtual void printMipsPLT(const MipsGOTParser
<ELFT
> &Parser
) = 0;
313 virtual void printMemtag(
314 const ArrayRef
<std::pair
<std::string
, std::string
>> DynamicEntries
,
315 const ArrayRef
<uint8_t> AndroidNoteDesc
,
316 const ArrayRef
<std::pair
<uint64_t, uint64_t>> Descriptors
) = 0;
318 virtual void printHashHistogram(const Elf_Hash
&HashTable
) const;
319 virtual void printGnuHashHistogram(const Elf_GnuHash
&GnuHashTable
) const;
320 virtual void printHashHistogramStats(size_t NBucket
, size_t MaxChain
,
321 size_t TotalSyms
, ArrayRef
<size_t> Count
,
322 bool IsGnu
) const = 0;
324 Expected
<ArrayRef
<Elf_Versym
>>
325 getVersionTable(const Elf_Shdr
&Sec
, ArrayRef
<Elf_Sym
> *SymTab
,
326 StringRef
*StrTab
, const Elf_Shdr
**SymTabSec
) const;
327 StringRef
getPrintableSectionName(const Elf_Shdr
&Sec
) const;
329 std::vector
<GroupSection
> getGroups();
331 // Returns the function symbol index for the given address. Matches the
332 // symbol's section with FunctionSec when specified.
333 // Returns std::nullopt if no function symbol can be found for the address or
334 // in case it is not defined in the specified section.
335 SmallVector
<uint32_t> getSymbolIndexesForFunctionAddress(
336 uint64_t SymValue
, std::optional
<const Elf_Shdr
*> FunctionSec
);
337 bool printFunctionStackSize(uint64_t SymValue
,
338 std::optional
<const Elf_Shdr
*> FunctionSec
,
339 const Elf_Shdr
&StackSizeSec
, DataExtractor Data
,
341 void printStackSize(const Relocation
<ELFT
> &R
, const Elf_Shdr
&RelocSec
,
342 unsigned Ndx
, const Elf_Shdr
*SymTab
,
343 const Elf_Shdr
*FunctionSec
, const Elf_Shdr
&StackSizeSec
,
344 const RelocationResolver
&Resolver
, DataExtractor Data
);
345 virtual void printStackSizeEntry(uint64_t Size
,
346 ArrayRef
<std::string
> FuncNames
) = 0;
348 void printRelocatableStackSizes(std::function
<void()> PrintHeader
);
349 void printNonRelocatableStackSizes(std::function
<void()> PrintHeader
);
351 const object::ELFObjectFile
<ELFT
> &ObjF
;
352 const ELFFile
<ELFT
> &Obj
;
355 Expected
<DynRegionInfo
> createDRI(uint64_t Offset
, uint64_t Size
,
357 if (Offset
+ Size
< Offset
|| Offset
+ Size
> Obj
.getBufSize())
358 return createError("offset (0x" + Twine::utohexstr(Offset
) +
359 ") + size (0x" + Twine::utohexstr(Size
) +
360 ") is greater than the file size (0x" +
361 Twine::utohexstr(Obj
.getBufSize()) + ")");
362 return DynRegionInfo(ObjF
, *this, Obj
.base() + Offset
, Size
, EntSize
);
365 void printAttributes(unsigned, std::unique_ptr
<ELFAttributeParser
>,
367 void printMipsReginfo();
368 void printMipsOptions();
370 std::pair
<const Elf_Phdr
*, const Elf_Shdr
*> findDynamic();
371 void loadDynamicTable();
372 void parseDynamicTable();
374 Expected
<StringRef
> getSymbolVersion(const Elf_Sym
&Sym
,
375 bool &IsDefault
) const;
376 Expected
<SmallVector
<std::optional
<VersionEntry
>, 0> *> getVersionMap() const;
378 DynRegionInfo DynRelRegion
;
379 DynRegionInfo DynRelaRegion
;
380 DynRegionInfo DynRelrRegion
;
381 DynRegionInfo DynPLTRelRegion
;
382 std::optional
<DynRegionInfo
> DynSymRegion
;
383 DynRegionInfo DynSymTabShndxRegion
;
384 DynRegionInfo DynamicTable
;
385 StringRef DynamicStringTable
;
386 const Elf_Hash
*HashTable
= nullptr;
387 const Elf_GnuHash
*GnuHashTable
= nullptr;
388 const Elf_Shdr
*DotSymtabSec
= nullptr;
389 const Elf_Shdr
*DotDynsymSec
= nullptr;
390 const Elf_Shdr
*DotAddrsigSec
= nullptr;
391 DenseMap
<const Elf_Shdr
*, ArrayRef
<Elf_Word
>> ShndxTables
;
392 std::optional
<uint64_t> SONameOffset
;
393 std::optional
<DenseMap
<uint64_t, std::vector
<uint32_t>>> AddressToIndexMap
;
395 const Elf_Shdr
*SymbolVersionSection
= nullptr; // .gnu.version
396 const Elf_Shdr
*SymbolVersionNeedSection
= nullptr; // .gnu.version_r
397 const Elf_Shdr
*SymbolVersionDefSection
= nullptr; // .gnu.version_d
399 std::string
getFullSymbolName(const Elf_Sym
&Symbol
, unsigned SymIndex
,
400 DataRegion
<Elf_Word
> ShndxTable
,
401 std::optional
<StringRef
> StrTable
,
402 bool IsDynamic
) const;
404 getSymbolSectionIndex(const Elf_Sym
&Symbol
, unsigned SymIndex
,
405 DataRegion
<Elf_Word
> ShndxTable
) const;
406 Expected
<StringRef
> getSymbolSectionName(const Elf_Sym
&Symbol
,
407 unsigned SectionIndex
) const;
408 std::string
getStaticSymbolName(uint32_t Index
) const;
409 StringRef
getDynamicString(uint64_t Value
) const;
411 void printSymbolsHelper(bool IsDynamic
, bool ExtraSymInfo
) const;
412 std::string
getDynamicEntry(uint64_t Type
, uint64_t Value
) const;
414 Expected
<RelSymbol
<ELFT
>> getRelocationTarget(const Relocation
<ELFT
> &R
,
415 const Elf_Shdr
*SymTab
) const;
417 ArrayRef
<Elf_Word
> getShndxTable(const Elf_Shdr
*Symtab
) const;
420 mutable SmallVector
<std::optional
<VersionEntry
>, 0> VersionMap
;
423 template <class ELFT
>
424 std::string ELFDumper
<ELFT
>::describe(const Elf_Shdr
&Sec
) const {
425 return ::describe(Obj
, Sec
);
430 template <class ELFT
> struct SymtabLink
{
431 typename
ELFT::SymRange Symbols
;
432 StringRef StringTable
;
433 const typename
ELFT::Shdr
*SymTab
;
436 // Returns the linked symbol table, symbols and associated string table for a
438 template <class ELFT
>
439 Expected
<SymtabLink
<ELFT
>> getLinkAsSymtab(const ELFFile
<ELFT
> &Obj
,
440 const typename
ELFT::Shdr
&Sec
,
441 unsigned ExpectedType
) {
442 Expected
<const typename
ELFT::Shdr
*> SymtabOrErr
=
443 Obj
.getSection(Sec
.sh_link
);
445 return createError("invalid section linked to " + describe(Obj
, Sec
) +
446 ": " + toString(SymtabOrErr
.takeError()));
448 if ((*SymtabOrErr
)->sh_type
!= ExpectedType
)
450 "invalid section linked to " + describe(Obj
, Sec
) + ": expected " +
451 object::getELFSectionTypeName(Obj
.getHeader().e_machine
, ExpectedType
) +
453 object::getELFSectionTypeName(Obj
.getHeader().e_machine
,
454 (*SymtabOrErr
)->sh_type
));
456 Expected
<StringRef
> StrTabOrErr
= Obj
.getLinkAsStrtab(**SymtabOrErr
);
459 "can't get a string table for the symbol table linked to " +
460 describe(Obj
, Sec
) + ": " + toString(StrTabOrErr
.takeError()));
462 Expected
<typename
ELFT::SymRange
> SymsOrErr
= Obj
.symbols(*SymtabOrErr
);
464 return createError("unable to read symbols from the " + describe(Obj
, Sec
) +
465 ": " + toString(SymsOrErr
.takeError()));
467 return SymtabLink
<ELFT
>{*SymsOrErr
, *StrTabOrErr
, *SymtabOrErr
};
472 template <class ELFT
>
473 Expected
<ArrayRef
<typename
ELFT::Versym
>>
474 ELFDumper
<ELFT
>::getVersionTable(const Elf_Shdr
&Sec
, ArrayRef
<Elf_Sym
> *SymTab
,
476 const Elf_Shdr
**SymTabSec
) const {
477 assert((!SymTab
&& !StrTab
&& !SymTabSec
) || (SymTab
&& StrTab
&& SymTabSec
));
478 if (reinterpret_cast<uintptr_t>(Obj
.base() + Sec
.sh_offset
) %
481 return createError("the " + describe(Sec
) + " is misaligned");
483 Expected
<ArrayRef
<Elf_Versym
>> VersionsOrErr
=
484 Obj
.template getSectionContentsAsArray
<Elf_Versym
>(Sec
);
486 return createError("cannot read content of " + describe(Sec
) + ": " +
487 toString(VersionsOrErr
.takeError()));
489 Expected
<SymtabLink
<ELFT
>> SymTabOrErr
=
490 getLinkAsSymtab(Obj
, Sec
, SHT_DYNSYM
);
492 reportUniqueWarning(SymTabOrErr
.takeError());
493 return *VersionsOrErr
;
496 if (SymTabOrErr
->Symbols
.size() != VersionsOrErr
->size())
497 reportUniqueWarning(describe(Sec
) + ": the number of entries (" +
498 Twine(VersionsOrErr
->size()) +
499 ") does not match the number of symbols (" +
500 Twine(SymTabOrErr
->Symbols
.size()) +
501 ") in the symbol table with index " +
505 *SymTab
= SymTabOrErr
->Symbols
;
506 *StrTab
= SymTabOrErr
->StringTable
;
507 *SymTabSec
= SymTabOrErr
->SymTab
;
509 return *VersionsOrErr
;
512 template <class ELFT
>
513 void ELFDumper
<ELFT
>::printSymbolsHelper(bool IsDynamic
,
514 bool ExtraSymInfo
) const {
515 std::optional
<StringRef
> StrTable
;
517 Elf_Sym_Range
Syms(nullptr, nullptr);
518 const Elf_Shdr
*SymtabSec
= IsDynamic
? DotDynsymSec
: DotSymtabSec
;
521 StrTable
= DynamicStringTable
;
522 Syms
= dynamic_symbols();
523 Entries
= Syms
.size();
524 } else if (DotSymtabSec
) {
525 if (Expected
<StringRef
> StrTableOrErr
=
526 Obj
.getStringTableForSymtab(*DotSymtabSec
))
527 StrTable
= *StrTableOrErr
;
530 "unable to get the string table for the SHT_SYMTAB section: " +
531 toString(StrTableOrErr
.takeError()));
533 if (Expected
<Elf_Sym_Range
> SymsOrErr
= Obj
.symbols(DotSymtabSec
))
537 "unable to read symbols from the SHT_SYMTAB section: " +
538 toString(SymsOrErr
.takeError()));
539 Entries
= DotSymtabSec
->getEntityCount();
544 // The st_other field has 2 logical parts. The first two bits hold the symbol
545 // visibility (STV_*) and the remainder hold other platform-specific values.
546 bool NonVisibilityBitsUsed
=
547 llvm::any_of(Syms
, [](const Elf_Sym
&S
) { return S
.st_other
& ~0x3; });
549 DataRegion
<Elf_Word
> ShndxTable
=
550 IsDynamic
? DataRegion
<Elf_Word
>(
551 (const Elf_Word
*)this->DynSymTabShndxRegion
.Addr
,
552 this->getElfObject().getELFFile().end())
553 : DataRegion
<Elf_Word
>(this->getShndxTable(SymtabSec
));
555 printSymtabMessage(SymtabSec
, Entries
, NonVisibilityBitsUsed
, ExtraSymInfo
);
556 for (const Elf_Sym
&Sym
: Syms
)
557 printSymbol(Sym
, &Sym
- Syms
.begin(), ShndxTable
, StrTable
, IsDynamic
,
558 NonVisibilityBitsUsed
, ExtraSymInfo
);
561 template <typename ELFT
> class GNUELFDumper
: public ELFDumper
<ELFT
> {
562 formatted_raw_ostream
&OS
;
565 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT
)
567 GNUELFDumper(const object::ELFObjectFile
<ELFT
> &ObjF
, ScopedPrinter
&Writer
)
568 : ELFDumper
<ELFT
>(ObjF
, Writer
),
569 OS(static_cast<formatted_raw_ostream
&>(Writer
.getOStream())) {
570 assert(&this->W
.getOStream() == &llvm::fouts());
573 void printFileSummary(StringRef FileStr
, ObjectFile
&Obj
,
574 ArrayRef
<std::string
> InputFilenames
,
575 const Archive
*A
) override
;
576 void printFileHeaders() override
;
577 void printGroupSections() override
;
578 void printRelocations() override
;
579 void printSectionHeaders() override
;
580 void printSymbols(bool PrintSymbols
, bool PrintDynamicSymbols
,
581 bool ExtraSymInfo
) override
;
582 void printHashSymbols() override
;
583 void printSectionDetails() override
;
584 void printDependentLibs() override
;
585 void printDynamicTable() override
;
586 void printDynamicRelocations() override
;
587 void printSymtabMessage(const Elf_Shdr
*Symtab
, size_t Offset
,
588 bool NonVisibilityBitsUsed
,
589 bool ExtraSymInfo
) const override
;
590 void printProgramHeaders(bool PrintProgramHeaders
,
591 cl::boolOrDefault PrintSectionMapping
) override
;
592 void printVersionSymbolSection(const Elf_Shdr
*Sec
) override
;
593 void printVersionDefinitionSection(const Elf_Shdr
*Sec
) override
;
594 void printVersionDependencySection(const Elf_Shdr
*Sec
) override
;
595 void printCGProfile() override
;
596 void printBBAddrMaps() override
;
597 void printAddrsig() override
;
598 void printNotes() override
;
599 void printELFLinkerOptions() override
;
600 void printStackSizes() override
;
602 const ArrayRef
<std::pair
<std::string
, std::string
>> DynamicEntries
,
603 const ArrayRef
<uint8_t> AndroidNoteDesc
,
604 const ArrayRef
<std::pair
<uint64_t, uint64_t>> Descriptors
) override
;
605 void printHashHistogramStats(size_t NBucket
, size_t MaxChain
,
606 size_t TotalSyms
, ArrayRef
<size_t> Count
,
607 bool IsGnu
) const override
;
610 void printHashTableSymbols(const Elf_Hash
&HashTable
);
611 void printGnuHashTableSymbols(const Elf_GnuHash
&GnuHashTable
);
617 Field(StringRef S
, unsigned Col
) : Str(std::string(S
)), Column(Col
) {}
618 Field(unsigned Col
) : Column(Col
) {}
621 template <typename T
, typename TEnum
>
622 std::string
printFlags(T Value
, ArrayRef
<EnumEntry
<TEnum
>> EnumValues
,
623 TEnum EnumMask1
= {}, TEnum EnumMask2
= {},
624 TEnum EnumMask3
= {}) const {
626 for (const EnumEntry
<TEnum
> &Flag
: EnumValues
) {
631 if (Flag
.Value
& EnumMask1
)
632 EnumMask
= EnumMask1
;
633 else if (Flag
.Value
& EnumMask2
)
634 EnumMask
= EnumMask2
;
635 else if (Flag
.Value
& EnumMask3
)
636 EnumMask
= EnumMask3
;
637 bool IsEnum
= (Flag
.Value
& EnumMask
) != 0;
638 if ((!IsEnum
&& (Value
& Flag
.Value
) == Flag
.Value
) ||
639 (IsEnum
&& (Value
& EnumMask
) == Flag
.Value
)) {
648 formatted_raw_ostream
&printField(struct Field F
) const {
650 OS
.PadToColumn(F
.Column
);
655 void printHashedSymbol(const Elf_Sym
*Sym
, unsigned SymIndex
,
656 DataRegion
<Elf_Word
> ShndxTable
, StringRef StrTable
,
658 void printRelrReloc(const Elf_Relr
&R
) override
;
659 void printRelRelaReloc(const Relocation
<ELFT
> &R
,
660 const RelSymbol
<ELFT
> &RelSym
) override
;
661 void printSymbol(const Elf_Sym
&Symbol
, unsigned SymIndex
,
662 DataRegion
<Elf_Word
> ShndxTable
,
663 std::optional
<StringRef
> StrTable
, bool IsDynamic
,
664 bool NonVisibilityBitsUsed
,
665 bool ExtraSymInfo
) const override
;
666 void printDynamicRelocHeader(unsigned Type
, StringRef Name
,
667 const DynRegionInfo
&Reg
) override
;
669 std::string
getSymbolSectionNdx(const Elf_Sym
&Symbol
, unsigned SymIndex
,
670 DataRegion
<Elf_Word
> ShndxTable
,
671 bool ExtraSymInfo
= false) const;
672 void printProgramHeaders() override
;
673 void printSectionMapping() override
;
674 void printGNUVersionSectionProlog(const typename
ELFT::Shdr
&Sec
,
675 const Twine
&Label
, unsigned EntriesNum
);
677 void printStackSizeEntry(uint64_t Size
,
678 ArrayRef
<std::string
> FuncNames
) override
;
680 void printMipsGOT(const MipsGOTParser
<ELFT
> &Parser
) override
;
681 void printMipsPLT(const MipsGOTParser
<ELFT
> &Parser
) override
;
682 void printMipsABIFlags() override
;
685 template <typename ELFT
> class LLVMELFDumper
: public ELFDumper
<ELFT
> {
687 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT
)
689 LLVMELFDumper(const object::ELFObjectFile
<ELFT
> &ObjF
, ScopedPrinter
&Writer
)
690 : ELFDumper
<ELFT
>(ObjF
, Writer
), W(Writer
) {}
692 void printFileHeaders() override
;
693 void printGroupSections() override
;
694 void printRelocations() override
;
695 void printSectionHeaders() override
;
696 void printSymbols(bool PrintSymbols
, bool PrintDynamicSymbols
,
697 bool ExtraSymInfo
) override
;
698 void printDependentLibs() override
;
699 void printDynamicTable() override
;
700 void printDynamicRelocations() override
;
701 void printProgramHeaders(bool PrintProgramHeaders
,
702 cl::boolOrDefault PrintSectionMapping
) override
;
703 void printVersionSymbolSection(const Elf_Shdr
*Sec
) override
;
704 void printVersionDefinitionSection(const Elf_Shdr
*Sec
) override
;
705 void printVersionDependencySection(const Elf_Shdr
*Sec
) override
;
706 void printCGProfile() override
;
707 void printBBAddrMaps() override
;
708 void printAddrsig() override
;
709 void printNotes() override
;
710 void printELFLinkerOptions() override
;
711 void printStackSizes() override
;
713 const ArrayRef
<std::pair
<std::string
, std::string
>> DynamicEntries
,
714 const ArrayRef
<uint8_t> AndroidNoteDesc
,
715 const ArrayRef
<std::pair
<uint64_t, uint64_t>> Descriptors
) override
;
716 void printSymbolSection(const Elf_Sym
&Symbol
, unsigned SymIndex
,
717 DataRegion
<Elf_Word
> ShndxTable
) const;
718 void printHashHistogramStats(size_t NBucket
, size_t MaxChain
,
719 size_t TotalSyms
, ArrayRef
<size_t> Count
,
720 bool IsGnu
) const override
;
723 void printRelrReloc(const Elf_Relr
&R
) override
;
724 void printRelRelaReloc(const Relocation
<ELFT
> &R
,
725 const RelSymbol
<ELFT
> &RelSym
) override
;
727 void printSymbol(const Elf_Sym
&Symbol
, unsigned SymIndex
,
728 DataRegion
<Elf_Word
> ShndxTable
,
729 std::optional
<StringRef
> StrTable
, bool IsDynamic
,
730 bool /*NonVisibilityBitsUsed*/,
731 bool /*ExtraSymInfo*/) const override
;
732 void printProgramHeaders() override
;
733 void printSectionMapping() override
{}
734 void printStackSizeEntry(uint64_t Size
,
735 ArrayRef
<std::string
> FuncNames
) override
;
737 void printMipsGOT(const MipsGOTParser
<ELFT
> &Parser
) override
;
738 void printMipsPLT(const MipsGOTParser
<ELFT
> &Parser
) override
;
739 void printMipsABIFlags() override
;
740 virtual void printZeroSymbolOtherField(const Elf_Sym
&Symbol
) const;
743 virtual std::string
getGroupSectionHeaderName() const;
744 void printSymbolOtherField(const Elf_Sym
&Symbol
) const;
745 virtual void printExpandedRelRelaReloc(const Relocation
<ELFT
> &R
,
746 StringRef SymbolName
,
747 StringRef RelocName
);
748 virtual void printDefaultRelRelaReloc(const Relocation
<ELFT
> &R
,
749 StringRef SymbolName
,
750 StringRef RelocName
);
751 virtual void printRelocationSectionInfo(const Elf_Shdr
&Sec
, StringRef Name
,
752 const unsigned SecNdx
);
753 virtual void printSectionGroupMembers(StringRef Name
, uint64_t Idx
) const;
754 virtual void printEmptyGroupMessage() const;
759 // JSONELFDumper shares most of the same implementation as LLVMELFDumper except
760 // it uses a JSONScopedPrinter.
761 template <typename ELFT
> class JSONELFDumper
: public LLVMELFDumper
<ELFT
> {
763 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT
)
765 JSONELFDumper(const object::ELFObjectFile
<ELFT
> &ObjF
, ScopedPrinter
&Writer
)
766 : LLVMELFDumper
<ELFT
>(ObjF
, Writer
) {}
768 std::string
getGroupSectionHeaderName() const override
;
770 void printFileSummary(StringRef FileStr
, ObjectFile
&Obj
,
771 ArrayRef
<std::string
> InputFilenames
,
772 const Archive
*A
) override
;
773 virtual void printZeroSymbolOtherField(const Elf_Sym
&Symbol
) const override
;
775 void printDefaultRelRelaReloc(const Relocation
<ELFT
> &R
,
776 StringRef SymbolName
,
777 StringRef RelocName
) override
;
779 void printRelocationSectionInfo(const Elf_Shdr
&Sec
, StringRef Name
,
780 const unsigned SecNdx
) override
;
782 void printSectionGroupMembers(StringRef Name
, uint64_t Idx
) const override
;
784 void printEmptyGroupMessage() const override
;
787 std::unique_ptr
<DictScope
> FileScope
;
790 } // end anonymous namespace
794 template <class ELFT
>
795 static std::unique_ptr
<ObjDumper
>
796 createELFDumper(const ELFObjectFile
<ELFT
> &Obj
, ScopedPrinter
&Writer
) {
797 if (opts::Output
== opts::GNU
)
798 return std::make_unique
<GNUELFDumper
<ELFT
>>(Obj
, Writer
);
799 else if (opts::Output
== opts::JSON
)
800 return std::make_unique
<JSONELFDumper
<ELFT
>>(Obj
, Writer
);
801 return std::make_unique
<LLVMELFDumper
<ELFT
>>(Obj
, Writer
);
804 std::unique_ptr
<ObjDumper
> createELFDumper(const object::ELFObjectFileBase
&Obj
,
805 ScopedPrinter
&Writer
) {
806 // Little-endian 32-bit
807 if (const ELF32LEObjectFile
*ELFObj
= dyn_cast
<ELF32LEObjectFile
>(&Obj
))
808 return createELFDumper(*ELFObj
, Writer
);
811 if (const ELF32BEObjectFile
*ELFObj
= dyn_cast
<ELF32BEObjectFile
>(&Obj
))
812 return createELFDumper(*ELFObj
, Writer
);
814 // Little-endian 64-bit
815 if (const ELF64LEObjectFile
*ELFObj
= dyn_cast
<ELF64LEObjectFile
>(&Obj
))
816 return createELFDumper(*ELFObj
, Writer
);
819 return createELFDumper(*cast
<ELF64BEObjectFile
>(&Obj
), Writer
);
822 } // end namespace llvm
824 template <class ELFT
>
825 Expected
<SmallVector
<std::optional
<VersionEntry
>, 0> *>
826 ELFDumper
<ELFT
>::getVersionMap() const {
827 // If the VersionMap has already been loaded or if there is no dynamic symtab
828 // or version table, there is nothing to do.
829 if (!VersionMap
.empty() || !DynSymRegion
|| !SymbolVersionSection
)
832 Expected
<SmallVector
<std::optional
<VersionEntry
>, 0>> MapOrErr
=
833 Obj
.loadVersionMap(SymbolVersionNeedSection
, SymbolVersionDefSection
);
835 VersionMap
= *MapOrErr
;
837 return MapOrErr
.takeError();
842 template <typename ELFT
>
843 Expected
<StringRef
> ELFDumper
<ELFT
>::getSymbolVersion(const Elf_Sym
&Sym
,
844 bool &IsDefault
) const {
845 // This is a dynamic symbol. Look in the GNU symbol version table.
846 if (!SymbolVersionSection
) {
852 assert(DynSymRegion
&& "DynSymRegion has not been initialised");
853 // Determine the position in the symbol table of this entry.
854 size_t EntryIndex
= (reinterpret_cast<uintptr_t>(&Sym
) -
855 reinterpret_cast<uintptr_t>(DynSymRegion
->Addr
)) /
858 // Get the corresponding version index entry.
859 Expected
<const Elf_Versym
*> EntryOrErr
=
860 Obj
.template getEntry
<Elf_Versym
>(*SymbolVersionSection
, EntryIndex
);
862 return EntryOrErr
.takeError();
864 unsigned Version
= (*EntryOrErr
)->vs_index
;
865 if (Version
== VER_NDX_LOCAL
|| Version
== VER_NDX_GLOBAL
) {
870 Expected
<SmallVector
<std::optional
<VersionEntry
>, 0> *> MapOrErr
=
873 return MapOrErr
.takeError();
875 return Obj
.getSymbolVersionByIndex(Version
, IsDefault
, **MapOrErr
,
876 Sym
.st_shndx
== ELF::SHN_UNDEF
);
879 template <typename ELFT
>
880 Expected
<RelSymbol
<ELFT
>>
881 ELFDumper
<ELFT
>::getRelocationTarget(const Relocation
<ELFT
> &R
,
882 const Elf_Shdr
*SymTab
) const {
884 return RelSymbol
<ELFT
>(nullptr, "");
886 Expected
<const Elf_Sym
*> SymOrErr
=
887 Obj
.template getEntry
<Elf_Sym
>(*SymTab
, R
.Symbol
);
889 return createError("unable to read an entry with index " + Twine(R
.Symbol
) +
890 " from " + describe(*SymTab
) + ": " +
891 toString(SymOrErr
.takeError()));
892 const Elf_Sym
*Sym
= *SymOrErr
;
894 return RelSymbol
<ELFT
>(nullptr, "");
896 Expected
<StringRef
> StrTableOrErr
= Obj
.getStringTableForSymtab(*SymTab
);
898 return StrTableOrErr
.takeError();
900 const Elf_Sym
*FirstSym
=
901 cantFail(Obj
.template getEntry
<Elf_Sym
>(*SymTab
, 0));
902 std::string SymbolName
=
903 getFullSymbolName(*Sym
, Sym
- FirstSym
, getShndxTable(SymTab
),
904 *StrTableOrErr
, SymTab
->sh_type
== SHT_DYNSYM
);
905 return RelSymbol
<ELFT
>(Sym
, SymbolName
);
908 template <typename ELFT
>
909 ArrayRef
<typename
ELFT::Word
>
910 ELFDumper
<ELFT
>::getShndxTable(const Elf_Shdr
*Symtab
) const {
912 auto It
= ShndxTables
.find(Symtab
);
913 if (It
!= ShndxTables
.end())
919 static std::string
maybeDemangle(StringRef Name
) {
920 return opts::Demangle
? demangle(Name
) : Name
.str();
923 template <typename ELFT
>
924 std::string ELFDumper
<ELFT
>::getStaticSymbolName(uint32_t Index
) const {
925 auto Warn
= [&](Error E
) -> std::string
{
926 reportUniqueWarning("unable to read the name of symbol with index " +
927 Twine(Index
) + ": " + toString(std::move(E
)));
931 Expected
<const typename
ELFT::Sym
*> SymOrErr
=
932 Obj
.getSymbol(DotSymtabSec
, Index
);
934 return Warn(SymOrErr
.takeError());
936 Expected
<StringRef
> StrTabOrErr
= Obj
.getStringTableForSymtab(*DotSymtabSec
);
938 return Warn(StrTabOrErr
.takeError());
940 Expected
<StringRef
> NameOrErr
= (*SymOrErr
)->getName(*StrTabOrErr
);
942 return Warn(NameOrErr
.takeError());
943 return maybeDemangle(*NameOrErr
);
946 template <typename ELFT
>
947 std::string ELFDumper
<ELFT
>::getFullSymbolName(
948 const Elf_Sym
&Symbol
, unsigned SymIndex
, DataRegion
<Elf_Word
> ShndxTable
,
949 std::optional
<StringRef
> StrTable
, bool IsDynamic
) const {
953 std::string SymbolName
;
954 if (Expected
<StringRef
> NameOrErr
= Symbol
.getName(*StrTable
)) {
955 SymbolName
= maybeDemangle(*NameOrErr
);
957 reportUniqueWarning(NameOrErr
.takeError());
961 if (SymbolName
.empty() && Symbol
.getType() == ELF::STT_SECTION
) {
962 Expected
<unsigned> SectionIndex
=
963 getSymbolSectionIndex(Symbol
, SymIndex
, ShndxTable
);
965 reportUniqueWarning(SectionIndex
.takeError());
968 Expected
<StringRef
> NameOrErr
= getSymbolSectionName(Symbol
, *SectionIndex
);
970 reportUniqueWarning(NameOrErr
.takeError());
971 return ("<section " + Twine(*SectionIndex
) + ">").str();
973 return std::string(*NameOrErr
);
980 Expected
<StringRef
> VersionOrErr
= getSymbolVersion(Symbol
, IsDefault
);
982 reportUniqueWarning(VersionOrErr
.takeError());
983 return SymbolName
+ "@<corrupt>";
986 if (!VersionOrErr
->empty()) {
987 SymbolName
+= (IsDefault
? "@@" : "@");
988 SymbolName
+= *VersionOrErr
;
993 template <typename ELFT
>
995 ELFDumper
<ELFT
>::getSymbolSectionIndex(const Elf_Sym
&Symbol
, unsigned SymIndex
,
996 DataRegion
<Elf_Word
> ShndxTable
) const {
997 unsigned Ndx
= Symbol
.st_shndx
;
998 if (Ndx
== SHN_XINDEX
)
999 return object::getExtendedSymbolTableIndex
<ELFT
>(Symbol
, SymIndex
,
1001 if (Ndx
!= SHN_UNDEF
&& Ndx
< SHN_LORESERVE
)
1004 auto CreateErr
= [&](const Twine
&Name
,
1005 std::optional
<unsigned> Offset
= std::nullopt
) {
1008 Desc
= (Name
+ "+0x" + Twine::utohexstr(*Offset
)).str();
1012 "unable to get section index for symbol with st_shndx = 0x" +
1013 Twine::utohexstr(Ndx
) + " (" + Desc
+ ")");
1016 if (Ndx
>= ELF::SHN_LOPROC
&& Ndx
<= ELF::SHN_HIPROC
)
1017 return CreateErr("SHN_LOPROC", Ndx
- ELF::SHN_LOPROC
);
1018 if (Ndx
>= ELF::SHN_LOOS
&& Ndx
<= ELF::SHN_HIOS
)
1019 return CreateErr("SHN_LOOS", Ndx
- ELF::SHN_LOOS
);
1020 if (Ndx
== ELF::SHN_UNDEF
)
1021 return CreateErr("SHN_UNDEF");
1022 if (Ndx
== ELF::SHN_ABS
)
1023 return CreateErr("SHN_ABS");
1024 if (Ndx
== ELF::SHN_COMMON
)
1025 return CreateErr("SHN_COMMON");
1026 return CreateErr("SHN_LORESERVE", Ndx
- SHN_LORESERVE
);
1029 template <typename ELFT
>
1031 ELFDumper
<ELFT
>::getSymbolSectionName(const Elf_Sym
&Symbol
,
1032 unsigned SectionIndex
) const {
1033 Expected
<const Elf_Shdr
*> SecOrErr
= Obj
.getSection(SectionIndex
);
1035 return SecOrErr
.takeError();
1036 return Obj
.getSectionName(**SecOrErr
);
1039 template <class ELFO
>
1040 static const typename
ELFO::Elf_Shdr
*
1041 findNotEmptySectionByAddress(const ELFO
&Obj
, StringRef FileName
,
1043 for (const typename
ELFO::Elf_Shdr
&Shdr
: cantFail(Obj
.sections()))
1044 if (Shdr
.sh_addr
== Addr
&& Shdr
.sh_size
> 0)
1049 const EnumEntry
<unsigned> ElfClass
[] = {
1050 {"None", "none", ELF::ELFCLASSNONE
},
1051 {"32-bit", "ELF32", ELF::ELFCLASS32
},
1052 {"64-bit", "ELF64", ELF::ELFCLASS64
},
1055 const EnumEntry
<unsigned> ElfDataEncoding
[] = {
1056 {"None", "none", ELF::ELFDATANONE
},
1057 {"LittleEndian", "2's complement, little endian", ELF::ELFDATA2LSB
},
1058 {"BigEndian", "2's complement, big endian", ELF::ELFDATA2MSB
},
1061 const EnumEntry
<unsigned> ElfObjectFileType
[] = {
1062 {"None", "NONE (none)", ELF::ET_NONE
},
1063 {"Relocatable", "REL (Relocatable file)", ELF::ET_REL
},
1064 {"Executable", "EXEC (Executable file)", ELF::ET_EXEC
},
1065 {"SharedObject", "DYN (Shared object file)", ELF::ET_DYN
},
1066 {"Core", "CORE (Core file)", ELF::ET_CORE
},
1069 const EnumEntry
<unsigned> ElfOSABI
[] = {
1070 {"SystemV", "UNIX - System V", ELF::ELFOSABI_NONE
},
1071 {"HPUX", "UNIX - HP-UX", ELF::ELFOSABI_HPUX
},
1072 {"NetBSD", "UNIX - NetBSD", ELF::ELFOSABI_NETBSD
},
1073 {"GNU/Linux", "UNIX - GNU", ELF::ELFOSABI_LINUX
},
1074 {"GNU/Hurd", "GNU/Hurd", ELF::ELFOSABI_HURD
},
1075 {"Solaris", "UNIX - Solaris", ELF::ELFOSABI_SOLARIS
},
1076 {"AIX", "UNIX - AIX", ELF::ELFOSABI_AIX
},
1077 {"IRIX", "UNIX - IRIX", ELF::ELFOSABI_IRIX
},
1078 {"FreeBSD", "UNIX - FreeBSD", ELF::ELFOSABI_FREEBSD
},
1079 {"TRU64", "UNIX - TRU64", ELF::ELFOSABI_TRU64
},
1080 {"Modesto", "Novell - Modesto", ELF::ELFOSABI_MODESTO
},
1081 {"OpenBSD", "UNIX - OpenBSD", ELF::ELFOSABI_OPENBSD
},
1082 {"OpenVMS", "VMS - OpenVMS", ELF::ELFOSABI_OPENVMS
},
1083 {"NSK", "HP - Non-Stop Kernel", ELF::ELFOSABI_NSK
},
1084 {"AROS", "AROS", ELF::ELFOSABI_AROS
},
1085 {"FenixOS", "FenixOS", ELF::ELFOSABI_FENIXOS
},
1086 {"CloudABI", "CloudABI", ELF::ELFOSABI_CLOUDABI
},
1087 {"CUDA", "NVIDIA - CUDA", ELF::ELFOSABI_CUDA
},
1088 {"Standalone", "Standalone App", ELF::ELFOSABI_STANDALONE
}
1091 const EnumEntry
<unsigned> AMDGPUElfOSABI
[] = {
1092 {"AMDGPU_HSA", "AMDGPU - HSA", ELF::ELFOSABI_AMDGPU_HSA
},
1093 {"AMDGPU_PAL", "AMDGPU - PAL", ELF::ELFOSABI_AMDGPU_PAL
},
1094 {"AMDGPU_MESA3D", "AMDGPU - MESA3D", ELF::ELFOSABI_AMDGPU_MESA3D
}
1097 const EnumEntry
<unsigned> ARMElfOSABI
[] = {
1098 {"ARM", "ARM", ELF::ELFOSABI_ARM
}
1101 const EnumEntry
<unsigned> C6000ElfOSABI
[] = {
1102 {"C6000_ELFABI", "Bare-metal C6000", ELF::ELFOSABI_C6000_ELFABI
},
1103 {"C6000_LINUX", "Linux C6000", ELF::ELFOSABI_C6000_LINUX
}
1106 const EnumEntry
<unsigned> ElfMachineType
[] = {
1107 ENUM_ENT(EM_NONE
, "None"),
1108 ENUM_ENT(EM_M32
, "WE32100"),
1109 ENUM_ENT(EM_SPARC
, "Sparc"),
1110 ENUM_ENT(EM_386
, "Intel 80386"),
1111 ENUM_ENT(EM_68K
, "MC68000"),
1112 ENUM_ENT(EM_88K
, "MC88000"),
1113 ENUM_ENT(EM_IAMCU
, "EM_IAMCU"),
1114 ENUM_ENT(EM_860
, "Intel 80860"),
1115 ENUM_ENT(EM_MIPS
, "MIPS R3000"),
1116 ENUM_ENT(EM_S370
, "IBM System/370"),
1117 ENUM_ENT(EM_MIPS_RS3_LE
, "MIPS R3000 little-endian"),
1118 ENUM_ENT(EM_PARISC
, "HPPA"),
1119 ENUM_ENT(EM_VPP500
, "Fujitsu VPP500"),
1120 ENUM_ENT(EM_SPARC32PLUS
, "Sparc v8+"),
1121 ENUM_ENT(EM_960
, "Intel 80960"),
1122 ENUM_ENT(EM_PPC
, "PowerPC"),
1123 ENUM_ENT(EM_PPC64
, "PowerPC64"),
1124 ENUM_ENT(EM_S390
, "IBM S/390"),
1125 ENUM_ENT(EM_SPU
, "SPU"),
1126 ENUM_ENT(EM_V800
, "NEC V800 series"),
1127 ENUM_ENT(EM_FR20
, "Fujistsu FR20"),
1128 ENUM_ENT(EM_RH32
, "TRW RH-32"),
1129 ENUM_ENT(EM_RCE
, "Motorola RCE"),
1130 ENUM_ENT(EM_ARM
, "ARM"),
1131 ENUM_ENT(EM_ALPHA
, "EM_ALPHA"),
1132 ENUM_ENT(EM_SH
, "Hitachi SH"),
1133 ENUM_ENT(EM_SPARCV9
, "Sparc v9"),
1134 ENUM_ENT(EM_TRICORE
, "Siemens Tricore"),
1135 ENUM_ENT(EM_ARC
, "ARC"),
1136 ENUM_ENT(EM_H8_300
, "Hitachi H8/300"),
1137 ENUM_ENT(EM_H8_300H
, "Hitachi H8/300H"),
1138 ENUM_ENT(EM_H8S
, "Hitachi H8S"),
1139 ENUM_ENT(EM_H8_500
, "Hitachi H8/500"),
1140 ENUM_ENT(EM_IA_64
, "Intel IA-64"),
1141 ENUM_ENT(EM_MIPS_X
, "Stanford MIPS-X"),
1142 ENUM_ENT(EM_COLDFIRE
, "Motorola Coldfire"),
1143 ENUM_ENT(EM_68HC12
, "Motorola MC68HC12 Microcontroller"),
1144 ENUM_ENT(EM_MMA
, "Fujitsu Multimedia Accelerator"),
1145 ENUM_ENT(EM_PCP
, "Siemens PCP"),
1146 ENUM_ENT(EM_NCPU
, "Sony nCPU embedded RISC processor"),
1147 ENUM_ENT(EM_NDR1
, "Denso NDR1 microprocesspr"),
1148 ENUM_ENT(EM_STARCORE
, "Motorola Star*Core processor"),
1149 ENUM_ENT(EM_ME16
, "Toyota ME16 processor"),
1150 ENUM_ENT(EM_ST100
, "STMicroelectronics ST100 processor"),
1151 ENUM_ENT(EM_TINYJ
, "Advanced Logic Corp. TinyJ embedded processor"),
1152 ENUM_ENT(EM_X86_64
, "Advanced Micro Devices X86-64"),
1153 ENUM_ENT(EM_PDSP
, "Sony DSP processor"),
1154 ENUM_ENT(EM_PDP10
, "Digital Equipment Corp. PDP-10"),
1155 ENUM_ENT(EM_PDP11
, "Digital Equipment Corp. PDP-11"),
1156 ENUM_ENT(EM_FX66
, "Siemens FX66 microcontroller"),
1157 ENUM_ENT(EM_ST9PLUS
, "STMicroelectronics ST9+ 8/16 bit microcontroller"),
1158 ENUM_ENT(EM_ST7
, "STMicroelectronics ST7 8-bit microcontroller"),
1159 ENUM_ENT(EM_68HC16
, "Motorola MC68HC16 Microcontroller"),
1160 ENUM_ENT(EM_68HC11
, "Motorola MC68HC11 Microcontroller"),
1161 ENUM_ENT(EM_68HC08
, "Motorola MC68HC08 Microcontroller"),
1162 ENUM_ENT(EM_68HC05
, "Motorola MC68HC05 Microcontroller"),
1163 ENUM_ENT(EM_SVX
, "Silicon Graphics SVx"),
1164 ENUM_ENT(EM_ST19
, "STMicroelectronics ST19 8-bit microcontroller"),
1165 ENUM_ENT(EM_VAX
, "Digital VAX"),
1166 ENUM_ENT(EM_CRIS
, "Axis Communications 32-bit embedded processor"),
1167 ENUM_ENT(EM_JAVELIN
, "Infineon Technologies 32-bit embedded cpu"),
1168 ENUM_ENT(EM_FIREPATH
, "Element 14 64-bit DSP processor"),
1169 ENUM_ENT(EM_ZSP
, "LSI Logic's 16-bit DSP processor"),
1170 ENUM_ENT(EM_MMIX
, "Donald Knuth's educational 64-bit processor"),
1171 ENUM_ENT(EM_HUANY
, "Harvard Universitys's machine-independent object format"),
1172 ENUM_ENT(EM_PRISM
, "Vitesse Prism"),
1173 ENUM_ENT(EM_AVR
, "Atmel AVR 8-bit microcontroller"),
1174 ENUM_ENT(EM_FR30
, "Fujitsu FR30"),
1175 ENUM_ENT(EM_D10V
, "Mitsubishi D10V"),
1176 ENUM_ENT(EM_D30V
, "Mitsubishi D30V"),
1177 ENUM_ENT(EM_V850
, "NEC v850"),
1178 ENUM_ENT(EM_M32R
, "Renesas M32R (formerly Mitsubishi M32r)"),
1179 ENUM_ENT(EM_MN10300
, "Matsushita MN10300"),
1180 ENUM_ENT(EM_MN10200
, "Matsushita MN10200"),
1181 ENUM_ENT(EM_PJ
, "picoJava"),
1182 ENUM_ENT(EM_OPENRISC
, "OpenRISC 32-bit embedded processor"),
1183 ENUM_ENT(EM_ARC_COMPACT
, "EM_ARC_COMPACT"),
1184 ENUM_ENT(EM_XTENSA
, "Tensilica Xtensa Processor"),
1185 ENUM_ENT(EM_VIDEOCORE
, "Alphamosaic VideoCore processor"),
1186 ENUM_ENT(EM_TMM_GPP
, "Thompson Multimedia General Purpose Processor"),
1187 ENUM_ENT(EM_NS32K
, "National Semiconductor 32000 series"),
1188 ENUM_ENT(EM_TPC
, "Tenor Network TPC processor"),
1189 ENUM_ENT(EM_SNP1K
, "EM_SNP1K"),
1190 ENUM_ENT(EM_ST200
, "STMicroelectronics ST200 microcontroller"),
1191 ENUM_ENT(EM_IP2K
, "Ubicom IP2xxx 8-bit microcontrollers"),
1192 ENUM_ENT(EM_MAX
, "MAX Processor"),
1193 ENUM_ENT(EM_CR
, "National Semiconductor CompactRISC"),
1194 ENUM_ENT(EM_F2MC16
, "Fujitsu F2MC16"),
1195 ENUM_ENT(EM_MSP430
, "Texas Instruments msp430 microcontroller"),
1196 ENUM_ENT(EM_BLACKFIN
, "Analog Devices Blackfin"),
1197 ENUM_ENT(EM_SE_C33
, "S1C33 Family of Seiko Epson processors"),
1198 ENUM_ENT(EM_SEP
, "Sharp embedded microprocessor"),
1199 ENUM_ENT(EM_ARCA
, "Arca RISC microprocessor"),
1200 ENUM_ENT(EM_UNICORE
, "Unicore"),
1201 ENUM_ENT(EM_EXCESS
, "eXcess 16/32/64-bit configurable embedded CPU"),
1202 ENUM_ENT(EM_DXP
, "Icera Semiconductor Inc. Deep Execution Processor"),
1203 ENUM_ENT(EM_ALTERA_NIOS2
, "Altera Nios"),
1204 ENUM_ENT(EM_CRX
, "National Semiconductor CRX microprocessor"),
1205 ENUM_ENT(EM_XGATE
, "Motorola XGATE embedded processor"),
1206 ENUM_ENT(EM_C166
, "Infineon Technologies xc16x"),
1207 ENUM_ENT(EM_M16C
, "Renesas M16C"),
1208 ENUM_ENT(EM_DSPIC30F
, "Microchip Technology dsPIC30F Digital Signal Controller"),
1209 ENUM_ENT(EM_CE
, "Freescale Communication Engine RISC core"),
1210 ENUM_ENT(EM_M32C
, "Renesas M32C"),
1211 ENUM_ENT(EM_TSK3000
, "Altium TSK3000 core"),
1212 ENUM_ENT(EM_RS08
, "Freescale RS08 embedded processor"),
1213 ENUM_ENT(EM_SHARC
, "EM_SHARC"),
1214 ENUM_ENT(EM_ECOG2
, "Cyan Technology eCOG2 microprocessor"),
1215 ENUM_ENT(EM_SCORE7
, "SUNPLUS S+Core"),
1216 ENUM_ENT(EM_DSP24
, "New Japan Radio (NJR) 24-bit DSP Processor"),
1217 ENUM_ENT(EM_VIDEOCORE3
, "Broadcom VideoCore III processor"),
1218 ENUM_ENT(EM_LATTICEMICO32
, "Lattice Mico32"),
1219 ENUM_ENT(EM_SE_C17
, "Seiko Epson C17 family"),
1220 ENUM_ENT(EM_TI_C6000
, "Texas Instruments TMS320C6000 DSP family"),
1221 ENUM_ENT(EM_TI_C2000
, "Texas Instruments TMS320C2000 DSP family"),
1222 ENUM_ENT(EM_TI_C5500
, "Texas Instruments TMS320C55x DSP family"),
1223 ENUM_ENT(EM_MMDSP_PLUS
, "STMicroelectronics 64bit VLIW Data Signal Processor"),
1224 ENUM_ENT(EM_CYPRESS_M8C
, "Cypress M8C microprocessor"),
1225 ENUM_ENT(EM_R32C
, "Renesas R32C series microprocessors"),
1226 ENUM_ENT(EM_TRIMEDIA
, "NXP Semiconductors TriMedia architecture family"),
1227 ENUM_ENT(EM_HEXAGON
, "Qualcomm Hexagon"),
1228 ENUM_ENT(EM_8051
, "Intel 8051 and variants"),
1229 ENUM_ENT(EM_STXP7X
, "STMicroelectronics STxP7x family"),
1230 ENUM_ENT(EM_NDS32
, "Andes Technology compact code size embedded RISC processor family"),
1231 ENUM_ENT(EM_ECOG1
, "Cyan Technology eCOG1 microprocessor"),
1232 // FIXME: Following EM_ECOG1X definitions is dead code since EM_ECOG1X has
1233 // an identical number to EM_ECOG1.
1234 ENUM_ENT(EM_ECOG1X
, "Cyan Technology eCOG1X family"),
1235 ENUM_ENT(EM_MAXQ30
, "Dallas Semiconductor MAXQ30 Core microcontrollers"),
1236 ENUM_ENT(EM_XIMO16
, "New Japan Radio (NJR) 16-bit DSP Processor"),
1237 ENUM_ENT(EM_MANIK
, "M2000 Reconfigurable RISC Microprocessor"),
1238 ENUM_ENT(EM_CRAYNV2
, "Cray Inc. NV2 vector architecture"),
1239 ENUM_ENT(EM_RX
, "Renesas RX"),
1240 ENUM_ENT(EM_METAG
, "Imagination Technologies Meta processor architecture"),
1241 ENUM_ENT(EM_MCST_ELBRUS
, "MCST Elbrus general purpose hardware architecture"),
1242 ENUM_ENT(EM_ECOG16
, "Cyan Technology eCOG16 family"),
1243 ENUM_ENT(EM_CR16
, "National Semiconductor CompactRISC 16-bit processor"),
1244 ENUM_ENT(EM_ETPU
, "Freescale Extended Time Processing Unit"),
1245 ENUM_ENT(EM_SLE9X
, "Infineon Technologies SLE9X core"),
1246 ENUM_ENT(EM_L10M
, "EM_L10M"),
1247 ENUM_ENT(EM_K10M
, "EM_K10M"),
1248 ENUM_ENT(EM_AARCH64
, "AArch64"),
1249 ENUM_ENT(EM_AVR32
, "Atmel Corporation 32-bit microprocessor family"),
1250 ENUM_ENT(EM_STM8
, "STMicroeletronics STM8 8-bit microcontroller"),
1251 ENUM_ENT(EM_TILE64
, "Tilera TILE64 multicore architecture family"),
1252 ENUM_ENT(EM_TILEPRO
, "Tilera TILEPro multicore architecture family"),
1253 ENUM_ENT(EM_MICROBLAZE
, "Xilinx MicroBlaze 32-bit RISC soft processor core"),
1254 ENUM_ENT(EM_CUDA
, "NVIDIA CUDA architecture"),
1255 ENUM_ENT(EM_TILEGX
, "Tilera TILE-Gx multicore architecture family"),
1256 ENUM_ENT(EM_CLOUDSHIELD
, "EM_CLOUDSHIELD"),
1257 ENUM_ENT(EM_COREA_1ST
, "EM_COREA_1ST"),
1258 ENUM_ENT(EM_COREA_2ND
, "EM_COREA_2ND"),
1259 ENUM_ENT(EM_ARC_COMPACT2
, "EM_ARC_COMPACT2"),
1260 ENUM_ENT(EM_OPEN8
, "EM_OPEN8"),
1261 ENUM_ENT(EM_RL78
, "Renesas RL78"),
1262 ENUM_ENT(EM_VIDEOCORE5
, "Broadcom VideoCore V processor"),
1263 ENUM_ENT(EM_78KOR
, "EM_78KOR"),
1264 ENUM_ENT(EM_56800EX
, "EM_56800EX"),
1265 ENUM_ENT(EM_AMDGPU
, "EM_AMDGPU"),
1266 ENUM_ENT(EM_RISCV
, "RISC-V"),
1267 ENUM_ENT(EM_LANAI
, "EM_LANAI"),
1268 ENUM_ENT(EM_BPF
, "EM_BPF"),
1269 ENUM_ENT(EM_VE
, "NEC SX-Aurora Vector Engine"),
1270 ENUM_ENT(EM_LOONGARCH
, "LoongArch"),
1273 const EnumEntry
<unsigned> ElfSymbolBindings
[] = {
1274 {"Local", "LOCAL", ELF::STB_LOCAL
},
1275 {"Global", "GLOBAL", ELF::STB_GLOBAL
},
1276 {"Weak", "WEAK", ELF::STB_WEAK
},
1277 {"Unique", "UNIQUE", ELF::STB_GNU_UNIQUE
}};
1279 const EnumEntry
<unsigned> ElfSymbolVisibilities
[] = {
1280 {"DEFAULT", "DEFAULT", ELF::STV_DEFAULT
},
1281 {"INTERNAL", "INTERNAL", ELF::STV_INTERNAL
},
1282 {"HIDDEN", "HIDDEN", ELF::STV_HIDDEN
},
1283 {"PROTECTED", "PROTECTED", ELF::STV_PROTECTED
}};
1285 const EnumEntry
<unsigned> AMDGPUSymbolTypes
[] = {
1286 { "AMDGPU_HSA_KERNEL", ELF::STT_AMDGPU_HSA_KERNEL
}
1289 static const char *getGroupType(uint32_t Flag
) {
1290 if (Flag
& ELF::GRP_COMDAT
)
1296 const EnumEntry
<unsigned> ElfSectionFlags
[] = {
1297 ENUM_ENT(SHF_WRITE
, "W"),
1298 ENUM_ENT(SHF_ALLOC
, "A"),
1299 ENUM_ENT(SHF_EXECINSTR
, "X"),
1300 ENUM_ENT(SHF_MERGE
, "M"),
1301 ENUM_ENT(SHF_STRINGS
, "S"),
1302 ENUM_ENT(SHF_INFO_LINK
, "I"),
1303 ENUM_ENT(SHF_LINK_ORDER
, "L"),
1304 ENUM_ENT(SHF_OS_NONCONFORMING
, "O"),
1305 ENUM_ENT(SHF_GROUP
, "G"),
1306 ENUM_ENT(SHF_TLS
, "T"),
1307 ENUM_ENT(SHF_COMPRESSED
, "C"),
1308 ENUM_ENT(SHF_EXCLUDE
, "E"),
1311 const EnumEntry
<unsigned> ElfGNUSectionFlags
[] = {
1312 ENUM_ENT(SHF_GNU_RETAIN
, "R")
1315 const EnumEntry
<unsigned> ElfSolarisSectionFlags
[] = {
1316 ENUM_ENT(SHF_SUNW_NODISCARD
, "R")
1319 const EnumEntry
<unsigned> ElfXCoreSectionFlags
[] = {
1320 ENUM_ENT(XCORE_SHF_CP_SECTION
, ""),
1321 ENUM_ENT(XCORE_SHF_DP_SECTION
, "")
1324 const EnumEntry
<unsigned> ElfARMSectionFlags
[] = {
1325 ENUM_ENT(SHF_ARM_PURECODE
, "y")
1328 const EnumEntry
<unsigned> ElfHexagonSectionFlags
[] = {
1329 ENUM_ENT(SHF_HEX_GPREL
, "")
1332 const EnumEntry
<unsigned> ElfMipsSectionFlags
[] = {
1333 ENUM_ENT(SHF_MIPS_NODUPES
, ""),
1334 ENUM_ENT(SHF_MIPS_NAMES
, ""),
1335 ENUM_ENT(SHF_MIPS_LOCAL
, ""),
1336 ENUM_ENT(SHF_MIPS_NOSTRIP
, ""),
1337 ENUM_ENT(SHF_MIPS_GPREL
, ""),
1338 ENUM_ENT(SHF_MIPS_MERGE
, ""),
1339 ENUM_ENT(SHF_MIPS_ADDR
, ""),
1340 ENUM_ENT(SHF_MIPS_STRING
, "")
1343 const EnumEntry
<unsigned> ElfX86_64SectionFlags
[] = {
1344 ENUM_ENT(SHF_X86_64_LARGE
, "l")
1347 static std::vector
<EnumEntry
<unsigned>>
1348 getSectionFlagsForTarget(unsigned EOSAbi
, unsigned EMachine
) {
1349 std::vector
<EnumEntry
<unsigned>> Ret(std::begin(ElfSectionFlags
),
1350 std::end(ElfSectionFlags
));
1352 case ELFOSABI_SOLARIS
:
1353 Ret
.insert(Ret
.end(), std::begin(ElfSolarisSectionFlags
),
1354 std::end(ElfSolarisSectionFlags
));
1357 Ret
.insert(Ret
.end(), std::begin(ElfGNUSectionFlags
),
1358 std::end(ElfGNUSectionFlags
));
1363 Ret
.insert(Ret
.end(), std::begin(ElfARMSectionFlags
),
1364 std::end(ElfARMSectionFlags
));
1367 Ret
.insert(Ret
.end(), std::begin(ElfHexagonSectionFlags
),
1368 std::end(ElfHexagonSectionFlags
));
1371 Ret
.insert(Ret
.end(), std::begin(ElfMipsSectionFlags
),
1372 std::end(ElfMipsSectionFlags
));
1375 Ret
.insert(Ret
.end(), std::begin(ElfX86_64SectionFlags
),
1376 std::end(ElfX86_64SectionFlags
));
1379 Ret
.insert(Ret
.end(), std::begin(ElfXCoreSectionFlags
),
1380 std::end(ElfXCoreSectionFlags
));
1388 static std::string
getGNUFlags(unsigned EOSAbi
, unsigned EMachine
,
1390 // Here we are trying to build the flags string in the same way as GNU does.
1391 // It is not that straightforward. Imagine we have sh_flags == 0x90000000.
1392 // SHF_EXCLUDE ("E") has a value of 0x80000000 and SHF_MASKPROC is 0xf0000000.
1393 // GNU readelf will not print "E" or "Ep" in this case, but will print just
1394 // "p". It only will print "E" when no other processor flag is set.
1396 bool HasUnknownFlag
= false;
1397 bool HasOSFlag
= false;
1398 bool HasProcFlag
= false;
1399 std::vector
<EnumEntry
<unsigned>> FlagsList
=
1400 getSectionFlagsForTarget(EOSAbi
, EMachine
);
1402 // Take the least significant bit as a flag.
1403 uint64_t Flag
= Flags
& -Flags
;
1406 // Find the flag in the known flags list.
1407 auto I
= llvm::find_if(FlagsList
, [=](const EnumEntry
<unsigned> &E
) {
1408 // Flags with empty names are not printed in GNU style output.
1409 return E
.Value
== Flag
&& !E
.AltName
.empty();
1411 if (I
!= FlagsList
.end()) {
1416 // If we did not find a matching regular flag, then we deal with an OS
1417 // specific flag, processor specific flag or an unknown flag.
1418 if (Flag
& ELF::SHF_MASKOS
) {
1420 Flags
&= ~ELF::SHF_MASKOS
;
1421 } else if (Flag
& ELF::SHF_MASKPROC
) {
1423 // Mask off all the processor-specific bits. This removes the SHF_EXCLUDE
1424 // bit if set so that it doesn't also get printed.
1425 Flags
&= ~ELF::SHF_MASKPROC
;
1427 HasUnknownFlag
= true;
1431 // "o", "p" and "x" are printed last.
1441 static StringRef
segmentTypeToString(unsigned Arch
, unsigned Type
) {
1442 // Check potentially overlapped processor-specific program header type.
1445 switch (Type
) { LLVM_READOBJ_ENUM_CASE(ELF
, PT_ARM_EXIDX
); }
1448 case ELF::EM_MIPS_RS3_LE
:
1450 LLVM_READOBJ_ENUM_CASE(ELF
, PT_MIPS_REGINFO
);
1451 LLVM_READOBJ_ENUM_CASE(ELF
, PT_MIPS_RTPROC
);
1452 LLVM_READOBJ_ENUM_CASE(ELF
, PT_MIPS_OPTIONS
);
1453 LLVM_READOBJ_ENUM_CASE(ELF
, PT_MIPS_ABIFLAGS
);
1457 switch (Type
) { LLVM_READOBJ_ENUM_CASE(ELF
, PT_RISCV_ATTRIBUTES
); }
1461 LLVM_READOBJ_ENUM_CASE(ELF
, PT_NULL
);
1462 LLVM_READOBJ_ENUM_CASE(ELF
, PT_LOAD
);
1463 LLVM_READOBJ_ENUM_CASE(ELF
, PT_DYNAMIC
);
1464 LLVM_READOBJ_ENUM_CASE(ELF
, PT_INTERP
);
1465 LLVM_READOBJ_ENUM_CASE(ELF
, PT_NOTE
);
1466 LLVM_READOBJ_ENUM_CASE(ELF
, PT_SHLIB
);
1467 LLVM_READOBJ_ENUM_CASE(ELF
, PT_PHDR
);
1468 LLVM_READOBJ_ENUM_CASE(ELF
, PT_TLS
);
1470 LLVM_READOBJ_ENUM_CASE(ELF
, PT_GNU_EH_FRAME
);
1471 LLVM_READOBJ_ENUM_CASE(ELF
, PT_SUNW_UNWIND
);
1473 LLVM_READOBJ_ENUM_CASE(ELF
, PT_GNU_STACK
);
1474 LLVM_READOBJ_ENUM_CASE(ELF
, PT_GNU_RELRO
);
1475 LLVM_READOBJ_ENUM_CASE(ELF
, PT_GNU_PROPERTY
);
1477 LLVM_READOBJ_ENUM_CASE(ELF
, PT_OPENBSD_MUTABLE
);
1478 LLVM_READOBJ_ENUM_CASE(ELF
, PT_OPENBSD_RANDOMIZE
);
1479 LLVM_READOBJ_ENUM_CASE(ELF
, PT_OPENBSD_WXNEEDED
);
1480 LLVM_READOBJ_ENUM_CASE(ELF
, PT_OPENBSD_NOBTCFI
);
1481 LLVM_READOBJ_ENUM_CASE(ELF
, PT_OPENBSD_SYSCALLS
);
1482 LLVM_READOBJ_ENUM_CASE(ELF
, PT_OPENBSD_BOOTDATA
);
1488 static std::string
getGNUPtType(unsigned Arch
, unsigned Type
) {
1489 StringRef Seg
= segmentTypeToString(Arch
, Type
);
1491 return std::string("<unknown>: ") + to_string(format_hex(Type
, 1));
1493 // E.g. "PT_ARM_EXIDX" -> "EXIDX".
1494 if (Seg
.consume_front("PT_ARM_"))
1497 // E.g. "PT_MIPS_REGINFO" -> "REGINFO".
1498 if (Seg
.consume_front("PT_MIPS_"))
1501 // E.g. "PT_RISCV_ATTRIBUTES"
1502 if (Seg
.consume_front("PT_RISCV_"))
1505 // E.g. "PT_LOAD" -> "LOAD".
1506 assert(Seg
.starts_with("PT_"));
1507 return Seg
.drop_front(3).str();
1510 const EnumEntry
<unsigned> ElfSegmentFlags
[] = {
1511 LLVM_READOBJ_ENUM_ENT(ELF
, PF_X
),
1512 LLVM_READOBJ_ENUM_ENT(ELF
, PF_W
),
1513 LLVM_READOBJ_ENUM_ENT(ELF
, PF_R
)
1516 const EnumEntry
<unsigned> ElfHeaderMipsFlags
[] = {
1517 ENUM_ENT(EF_MIPS_NOREORDER
, "noreorder"),
1518 ENUM_ENT(EF_MIPS_PIC
, "pic"),
1519 ENUM_ENT(EF_MIPS_CPIC
, "cpic"),
1520 ENUM_ENT(EF_MIPS_ABI2
, "abi2"),
1521 ENUM_ENT(EF_MIPS_32BITMODE
, "32bitmode"),
1522 ENUM_ENT(EF_MIPS_FP64
, "fp64"),
1523 ENUM_ENT(EF_MIPS_NAN2008
, "nan2008"),
1524 ENUM_ENT(EF_MIPS_ABI_O32
, "o32"),
1525 ENUM_ENT(EF_MIPS_ABI_O64
, "o64"),
1526 ENUM_ENT(EF_MIPS_ABI_EABI32
, "eabi32"),
1527 ENUM_ENT(EF_MIPS_ABI_EABI64
, "eabi64"),
1528 ENUM_ENT(EF_MIPS_MACH_3900
, "3900"),
1529 ENUM_ENT(EF_MIPS_MACH_4010
, "4010"),
1530 ENUM_ENT(EF_MIPS_MACH_4100
, "4100"),
1531 ENUM_ENT(EF_MIPS_MACH_4650
, "4650"),
1532 ENUM_ENT(EF_MIPS_MACH_4120
, "4120"),
1533 ENUM_ENT(EF_MIPS_MACH_4111
, "4111"),
1534 ENUM_ENT(EF_MIPS_MACH_SB1
, "sb1"),
1535 ENUM_ENT(EF_MIPS_MACH_OCTEON
, "octeon"),
1536 ENUM_ENT(EF_MIPS_MACH_XLR
, "xlr"),
1537 ENUM_ENT(EF_MIPS_MACH_OCTEON2
, "octeon2"),
1538 ENUM_ENT(EF_MIPS_MACH_OCTEON3
, "octeon3"),
1539 ENUM_ENT(EF_MIPS_MACH_5400
, "5400"),
1540 ENUM_ENT(EF_MIPS_MACH_5900
, "5900"),
1541 ENUM_ENT(EF_MIPS_MACH_5500
, "5500"),
1542 ENUM_ENT(EF_MIPS_MACH_9000
, "9000"),
1543 ENUM_ENT(EF_MIPS_MACH_LS2E
, "loongson-2e"),
1544 ENUM_ENT(EF_MIPS_MACH_LS2F
, "loongson-2f"),
1545 ENUM_ENT(EF_MIPS_MACH_LS3A
, "loongson-3a"),
1546 ENUM_ENT(EF_MIPS_MICROMIPS
, "micromips"),
1547 ENUM_ENT(EF_MIPS_ARCH_ASE_M16
, "mips16"),
1548 ENUM_ENT(EF_MIPS_ARCH_ASE_MDMX
, "mdmx"),
1549 ENUM_ENT(EF_MIPS_ARCH_1
, "mips1"),
1550 ENUM_ENT(EF_MIPS_ARCH_2
, "mips2"),
1551 ENUM_ENT(EF_MIPS_ARCH_3
, "mips3"),
1552 ENUM_ENT(EF_MIPS_ARCH_4
, "mips4"),
1553 ENUM_ENT(EF_MIPS_ARCH_5
, "mips5"),
1554 ENUM_ENT(EF_MIPS_ARCH_32
, "mips32"),
1555 ENUM_ENT(EF_MIPS_ARCH_64
, "mips64"),
1556 ENUM_ENT(EF_MIPS_ARCH_32R2
, "mips32r2"),
1557 ENUM_ENT(EF_MIPS_ARCH_64R2
, "mips64r2"),
1558 ENUM_ENT(EF_MIPS_ARCH_32R6
, "mips32r6"),
1559 ENUM_ENT(EF_MIPS_ARCH_64R6
, "mips64r6")
1562 const EnumEntry
<unsigned> ElfHeaderAMDGPUFlagsABIVersion3
[] = {
1563 ENUM_ENT(EF_AMDGPU_MACH_NONE
, "none"),
1564 ENUM_ENT(EF_AMDGPU_MACH_R600_R600
, "r600"),
1565 ENUM_ENT(EF_AMDGPU_MACH_R600_R630
, "r630"),
1566 ENUM_ENT(EF_AMDGPU_MACH_R600_RS880
, "rs880"),
1567 ENUM_ENT(EF_AMDGPU_MACH_R600_RV670
, "rv670"),
1568 ENUM_ENT(EF_AMDGPU_MACH_R600_RV710
, "rv710"),
1569 ENUM_ENT(EF_AMDGPU_MACH_R600_RV730
, "rv730"),
1570 ENUM_ENT(EF_AMDGPU_MACH_R600_RV770
, "rv770"),
1571 ENUM_ENT(EF_AMDGPU_MACH_R600_CEDAR
, "cedar"),
1572 ENUM_ENT(EF_AMDGPU_MACH_R600_CYPRESS
, "cypress"),
1573 ENUM_ENT(EF_AMDGPU_MACH_R600_JUNIPER
, "juniper"),
1574 ENUM_ENT(EF_AMDGPU_MACH_R600_REDWOOD
, "redwood"),
1575 ENUM_ENT(EF_AMDGPU_MACH_R600_SUMO
, "sumo"),
1576 ENUM_ENT(EF_AMDGPU_MACH_R600_BARTS
, "barts"),
1577 ENUM_ENT(EF_AMDGPU_MACH_R600_CAICOS
, "caicos"),
1578 ENUM_ENT(EF_AMDGPU_MACH_R600_CAYMAN
, "cayman"),
1579 ENUM_ENT(EF_AMDGPU_MACH_R600_TURKS
, "turks"),
1580 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX600
, "gfx600"),
1581 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX601
, "gfx601"),
1582 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX602
, "gfx602"),
1583 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX700
, "gfx700"),
1584 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX701
, "gfx701"),
1585 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX702
, "gfx702"),
1586 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX703
, "gfx703"),
1587 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX704
, "gfx704"),
1588 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX705
, "gfx705"),
1589 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX801
, "gfx801"),
1590 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX802
, "gfx802"),
1591 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX803
, "gfx803"),
1592 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX805
, "gfx805"),
1593 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX810
, "gfx810"),
1594 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX900
, "gfx900"),
1595 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX902
, "gfx902"),
1596 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX904
, "gfx904"),
1597 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX906
, "gfx906"),
1598 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX908
, "gfx908"),
1599 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX909
, "gfx909"),
1600 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX90A
, "gfx90a"),
1601 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX90C
, "gfx90c"),
1602 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX940
, "gfx940"),
1603 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX941
, "gfx941"),
1604 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX942
, "gfx942"),
1605 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1010
, "gfx1010"),
1606 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1011
, "gfx1011"),
1607 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1012
, "gfx1012"),
1608 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1013
, "gfx1013"),
1609 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1030
, "gfx1030"),
1610 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1031
, "gfx1031"),
1611 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1032
, "gfx1032"),
1612 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1033
, "gfx1033"),
1613 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1034
, "gfx1034"),
1614 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1035
, "gfx1035"),
1615 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1036
, "gfx1036"),
1616 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1100
, "gfx1100"),
1617 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1101
, "gfx1101"),
1618 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1102
, "gfx1102"),
1619 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1103
, "gfx1103"),
1620 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1150
, "gfx1150"),
1621 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1151
, "gfx1151"),
1622 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1200
, "gfx1200"),
1623 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1201
, "gfx1201"),
1624 ENUM_ENT(EF_AMDGPU_FEATURE_XNACK_V3
, "xnack"),
1625 ENUM_ENT(EF_AMDGPU_FEATURE_SRAMECC_V3
, "sramecc"),
1628 const EnumEntry
<unsigned> ElfHeaderAMDGPUFlagsABIVersion4
[] = {
1629 ENUM_ENT(EF_AMDGPU_MACH_NONE
, "none"),
1630 ENUM_ENT(EF_AMDGPU_MACH_R600_R600
, "r600"),
1631 ENUM_ENT(EF_AMDGPU_MACH_R600_R630
, "r630"),
1632 ENUM_ENT(EF_AMDGPU_MACH_R600_RS880
, "rs880"),
1633 ENUM_ENT(EF_AMDGPU_MACH_R600_RV670
, "rv670"),
1634 ENUM_ENT(EF_AMDGPU_MACH_R600_RV710
, "rv710"),
1635 ENUM_ENT(EF_AMDGPU_MACH_R600_RV730
, "rv730"),
1636 ENUM_ENT(EF_AMDGPU_MACH_R600_RV770
, "rv770"),
1637 ENUM_ENT(EF_AMDGPU_MACH_R600_CEDAR
, "cedar"),
1638 ENUM_ENT(EF_AMDGPU_MACH_R600_CYPRESS
, "cypress"),
1639 ENUM_ENT(EF_AMDGPU_MACH_R600_JUNIPER
, "juniper"),
1640 ENUM_ENT(EF_AMDGPU_MACH_R600_REDWOOD
, "redwood"),
1641 ENUM_ENT(EF_AMDGPU_MACH_R600_SUMO
, "sumo"),
1642 ENUM_ENT(EF_AMDGPU_MACH_R600_BARTS
, "barts"),
1643 ENUM_ENT(EF_AMDGPU_MACH_R600_CAICOS
, "caicos"),
1644 ENUM_ENT(EF_AMDGPU_MACH_R600_CAYMAN
, "cayman"),
1645 ENUM_ENT(EF_AMDGPU_MACH_R600_TURKS
, "turks"),
1646 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX600
, "gfx600"),
1647 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX601
, "gfx601"),
1648 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX602
, "gfx602"),
1649 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX700
, "gfx700"),
1650 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX701
, "gfx701"),
1651 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX702
, "gfx702"),
1652 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX703
, "gfx703"),
1653 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX704
, "gfx704"),
1654 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX705
, "gfx705"),
1655 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX801
, "gfx801"),
1656 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX802
, "gfx802"),
1657 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX803
, "gfx803"),
1658 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX805
, "gfx805"),
1659 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX810
, "gfx810"),
1660 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX900
, "gfx900"),
1661 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX902
, "gfx902"),
1662 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX904
, "gfx904"),
1663 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX906
, "gfx906"),
1664 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX908
, "gfx908"),
1665 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX909
, "gfx909"),
1666 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX90A
, "gfx90a"),
1667 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX90C
, "gfx90c"),
1668 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX940
, "gfx940"),
1669 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX941
, "gfx941"),
1670 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX942
, "gfx942"),
1671 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1010
, "gfx1010"),
1672 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1011
, "gfx1011"),
1673 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1012
, "gfx1012"),
1674 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1013
, "gfx1013"),
1675 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1030
, "gfx1030"),
1676 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1031
, "gfx1031"),
1677 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1032
, "gfx1032"),
1678 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1033
, "gfx1033"),
1679 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1034
, "gfx1034"),
1680 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1035
, "gfx1035"),
1681 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1036
, "gfx1036"),
1682 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1100
, "gfx1100"),
1683 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1101
, "gfx1101"),
1684 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1102
, "gfx1102"),
1685 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1103
, "gfx1103"),
1686 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1150
, "gfx1150"),
1687 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1151
, "gfx1151"),
1688 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1200
, "gfx1200"),
1689 ENUM_ENT(EF_AMDGPU_MACH_AMDGCN_GFX1201
, "gfx1201"),
1690 ENUM_ENT(EF_AMDGPU_FEATURE_XNACK_ANY_V4
, "xnack"),
1691 ENUM_ENT(EF_AMDGPU_FEATURE_XNACK_OFF_V4
, "xnack-"),
1692 ENUM_ENT(EF_AMDGPU_FEATURE_XNACK_ON_V4
, "xnack+"),
1693 ENUM_ENT(EF_AMDGPU_FEATURE_SRAMECC_ANY_V4
, "sramecc"),
1694 ENUM_ENT(EF_AMDGPU_FEATURE_SRAMECC_OFF_V4
, "sramecc-"),
1695 ENUM_ENT(EF_AMDGPU_FEATURE_SRAMECC_ON_V4
, "sramecc+"),
1698 const EnumEntry
<unsigned> ElfHeaderNVPTXFlags
[] = {
1699 ENUM_ENT(EF_CUDA_SM20
, "sm_20"), ENUM_ENT(EF_CUDA_SM21
, "sm_21"),
1700 ENUM_ENT(EF_CUDA_SM30
, "sm_30"), ENUM_ENT(EF_CUDA_SM32
, "sm_32"),
1701 ENUM_ENT(EF_CUDA_SM35
, "sm_35"), ENUM_ENT(EF_CUDA_SM37
, "sm_37"),
1702 ENUM_ENT(EF_CUDA_SM50
, "sm_50"), ENUM_ENT(EF_CUDA_SM52
, "sm_52"),
1703 ENUM_ENT(EF_CUDA_SM53
, "sm_53"), ENUM_ENT(EF_CUDA_SM60
, "sm_60"),
1704 ENUM_ENT(EF_CUDA_SM61
, "sm_61"), ENUM_ENT(EF_CUDA_SM62
, "sm_62"),
1705 ENUM_ENT(EF_CUDA_SM70
, "sm_70"), ENUM_ENT(EF_CUDA_SM72
, "sm_72"),
1706 ENUM_ENT(EF_CUDA_SM75
, "sm_75"), ENUM_ENT(EF_CUDA_SM80
, "sm_80"),
1707 ENUM_ENT(EF_CUDA_SM86
, "sm_86"), ENUM_ENT(EF_CUDA_SM87
, "sm_87"),
1708 ENUM_ENT(EF_CUDA_SM89
, "sm_89"), ENUM_ENT(EF_CUDA_SM90
, "sm_90"),
1711 const EnumEntry
<unsigned> ElfHeaderRISCVFlags
[] = {
1712 ENUM_ENT(EF_RISCV_RVC
, "RVC"),
1713 ENUM_ENT(EF_RISCV_FLOAT_ABI_SINGLE
, "single-float ABI"),
1714 ENUM_ENT(EF_RISCV_FLOAT_ABI_DOUBLE
, "double-float ABI"),
1715 ENUM_ENT(EF_RISCV_FLOAT_ABI_QUAD
, "quad-float ABI"),
1716 ENUM_ENT(EF_RISCV_RVE
, "RVE"),
1717 ENUM_ENT(EF_RISCV_TSO
, "TSO"),
1720 const EnumEntry
<unsigned> ElfHeaderAVRFlags
[] = {
1721 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_AVR1
),
1722 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_AVR2
),
1723 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_AVR25
),
1724 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_AVR3
),
1725 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_AVR31
),
1726 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_AVR35
),
1727 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_AVR4
),
1728 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_AVR5
),
1729 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_AVR51
),
1730 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_AVR6
),
1731 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_AVRTINY
),
1732 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_XMEGA1
),
1733 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_XMEGA2
),
1734 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_XMEGA3
),
1735 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_XMEGA4
),
1736 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_XMEGA5
),
1737 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_XMEGA6
),
1738 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AVR_ARCH_XMEGA7
),
1739 ENUM_ENT(EF_AVR_LINKRELAX_PREPARED
, "relaxable"),
1742 const EnumEntry
<unsigned> ElfHeaderLoongArchFlags
[] = {
1743 ENUM_ENT(EF_LOONGARCH_ABI_SOFT_FLOAT
, "SOFT-FLOAT"),
1744 ENUM_ENT(EF_LOONGARCH_ABI_SINGLE_FLOAT
, "SINGLE-FLOAT"),
1745 ENUM_ENT(EF_LOONGARCH_ABI_DOUBLE_FLOAT
, "DOUBLE-FLOAT"),
1746 ENUM_ENT(EF_LOONGARCH_OBJABI_V0
, "OBJ-v0"),
1747 ENUM_ENT(EF_LOONGARCH_OBJABI_V1
, "OBJ-v1"),
1750 static const EnumEntry
<unsigned> ElfHeaderXtensaFlags
[] = {
1751 LLVM_READOBJ_ENUM_ENT(ELF
, EF_XTENSA_MACH_NONE
),
1752 LLVM_READOBJ_ENUM_ENT(ELF
, EF_XTENSA_XT_INSN
),
1753 LLVM_READOBJ_ENUM_ENT(ELF
, EF_XTENSA_XT_LIT
)
1756 const EnumEntry
<unsigned> ElfSymOtherFlags
[] = {
1757 LLVM_READOBJ_ENUM_ENT(ELF
, STV_INTERNAL
),
1758 LLVM_READOBJ_ENUM_ENT(ELF
, STV_HIDDEN
),
1759 LLVM_READOBJ_ENUM_ENT(ELF
, STV_PROTECTED
)
1762 const EnumEntry
<unsigned> ElfMipsSymOtherFlags
[] = {
1763 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_OPTIONAL
),
1764 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_PLT
),
1765 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_PIC
),
1766 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_MICROMIPS
)
1769 const EnumEntry
<unsigned> ElfAArch64SymOtherFlags
[] = {
1770 LLVM_READOBJ_ENUM_ENT(ELF
, STO_AARCH64_VARIANT_PCS
)
1773 const EnumEntry
<unsigned> ElfMips16SymOtherFlags
[] = {
1774 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_OPTIONAL
),
1775 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_PLT
),
1776 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_MIPS16
)
1779 const EnumEntry
<unsigned> ElfRISCVSymOtherFlags
[] = {
1780 LLVM_READOBJ_ENUM_ENT(ELF
, STO_RISCV_VARIANT_CC
)};
1782 static const char *getElfMipsOptionsOdkType(unsigned Odk
) {
1784 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_NULL
);
1785 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_REGINFO
);
1786 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_EXCEPTIONS
);
1787 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_PAD
);
1788 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_HWPATCH
);
1789 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_FILL
);
1790 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_TAGS
);
1791 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_HWAND
);
1792 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_HWOR
);
1793 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_GP_GROUP
);
1794 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_IDENT
);
1795 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_PAGESIZE
);
1801 template <typename ELFT
>
1802 std::pair
<const typename
ELFT::Phdr
*, const typename
ELFT::Shdr
*>
1803 ELFDumper
<ELFT
>::findDynamic() {
1804 // Try to locate the PT_DYNAMIC header.
1805 const Elf_Phdr
*DynamicPhdr
= nullptr;
1806 if (Expected
<ArrayRef
<Elf_Phdr
>> PhdrsOrErr
= Obj
.program_headers()) {
1807 for (const Elf_Phdr
&Phdr
: *PhdrsOrErr
) {
1808 if (Phdr
.p_type
!= ELF::PT_DYNAMIC
)
1810 DynamicPhdr
= &Phdr
;
1814 reportUniqueWarning(
1815 "unable to read program headers to locate the PT_DYNAMIC segment: " +
1816 toString(PhdrsOrErr
.takeError()));
1819 // Try to locate the .dynamic section in the sections header table.
1820 const Elf_Shdr
*DynamicSec
= nullptr;
1821 for (const Elf_Shdr
&Sec
: cantFail(Obj
.sections())) {
1822 if (Sec
.sh_type
!= ELF::SHT_DYNAMIC
)
1828 if (DynamicPhdr
&& ((DynamicPhdr
->p_offset
+ DynamicPhdr
->p_filesz
>
1829 ObjF
.getMemoryBufferRef().getBufferSize()) ||
1830 (DynamicPhdr
->p_offset
+ DynamicPhdr
->p_filesz
<
1831 DynamicPhdr
->p_offset
))) {
1832 reportUniqueWarning(
1833 "PT_DYNAMIC segment offset (0x" +
1834 Twine::utohexstr(DynamicPhdr
->p_offset
) + ") + file size (0x" +
1835 Twine::utohexstr(DynamicPhdr
->p_filesz
) +
1836 ") exceeds the size of the file (0x" +
1837 Twine::utohexstr(ObjF
.getMemoryBufferRef().getBufferSize()) + ")");
1838 // Don't use the broken dynamic header.
1839 DynamicPhdr
= nullptr;
1842 if (DynamicPhdr
&& DynamicSec
) {
1843 if (DynamicSec
->sh_addr
+ DynamicSec
->sh_size
>
1844 DynamicPhdr
->p_vaddr
+ DynamicPhdr
->p_memsz
||
1845 DynamicSec
->sh_addr
< DynamicPhdr
->p_vaddr
)
1846 reportUniqueWarning(describe(*DynamicSec
) +
1847 " is not contained within the "
1848 "PT_DYNAMIC segment");
1850 if (DynamicSec
->sh_addr
!= DynamicPhdr
->p_vaddr
)
1851 reportUniqueWarning(describe(*DynamicSec
) + " is not at the start of "
1852 "PT_DYNAMIC segment");
1855 return std::make_pair(DynamicPhdr
, DynamicSec
);
1858 template <typename ELFT
>
1859 void ELFDumper
<ELFT
>::loadDynamicTable() {
1860 const Elf_Phdr
*DynamicPhdr
;
1861 const Elf_Shdr
*DynamicSec
;
1862 std::tie(DynamicPhdr
, DynamicSec
) = findDynamic();
1863 if (!DynamicPhdr
&& !DynamicSec
)
1866 DynRegionInfo
FromPhdr(ObjF
, *this);
1867 bool IsPhdrTableValid
= false;
1869 // Use cantFail(), because p_offset/p_filesz fields of a PT_DYNAMIC are
1870 // validated in findDynamic() and so createDRI() is not expected to fail.
1871 FromPhdr
= cantFail(createDRI(DynamicPhdr
->p_offset
, DynamicPhdr
->p_filesz
,
1873 FromPhdr
.SizePrintName
= "PT_DYNAMIC size";
1874 FromPhdr
.EntSizePrintName
= "";
1875 IsPhdrTableValid
= !FromPhdr
.template getAsArrayRef
<Elf_Dyn
>().empty();
1878 // Locate the dynamic table described in a section header.
1879 // Ignore sh_entsize and use the expected value for entry size explicitly.
1880 // This allows us to dump dynamic sections with a broken sh_entsize
1882 DynRegionInfo
FromSec(ObjF
, *this);
1883 bool IsSecTableValid
= false;
1885 Expected
<DynRegionInfo
> RegOrErr
=
1886 createDRI(DynamicSec
->sh_offset
, DynamicSec
->sh_size
, sizeof(Elf_Dyn
));
1888 FromSec
= *RegOrErr
;
1889 FromSec
.Context
= describe(*DynamicSec
);
1890 FromSec
.EntSizePrintName
= "";
1891 IsSecTableValid
= !FromSec
.template getAsArrayRef
<Elf_Dyn
>().empty();
1893 reportUniqueWarning("unable to read the dynamic table from " +
1894 describe(*DynamicSec
) + ": " +
1895 toString(RegOrErr
.takeError()));
1899 // When we only have information from one of the SHT_DYNAMIC section header or
1900 // PT_DYNAMIC program header, just use that.
1901 if (!DynamicPhdr
|| !DynamicSec
) {
1902 if ((DynamicPhdr
&& IsPhdrTableValid
) || (DynamicSec
&& IsSecTableValid
)) {
1903 DynamicTable
= DynamicPhdr
? FromPhdr
: FromSec
;
1904 parseDynamicTable();
1906 reportUniqueWarning("no valid dynamic table was found");
1911 // At this point we have tables found from the section header and from the
1912 // dynamic segment. Usually they match, but we have to do sanity checks to
1915 if (FromPhdr
.Addr
!= FromSec
.Addr
)
1916 reportUniqueWarning("SHT_DYNAMIC section header and PT_DYNAMIC "
1917 "program header disagree about "
1918 "the location of the dynamic table");
1920 if (!IsPhdrTableValid
&& !IsSecTableValid
) {
1921 reportUniqueWarning("no valid dynamic table was found");
1925 // Information in the PT_DYNAMIC program header has priority over the
1926 // information in a section header.
1927 if (IsPhdrTableValid
) {
1928 if (!IsSecTableValid
)
1929 reportUniqueWarning(
1930 "SHT_DYNAMIC dynamic table is invalid: PT_DYNAMIC will be used");
1931 DynamicTable
= FromPhdr
;
1933 reportUniqueWarning(
1934 "PT_DYNAMIC dynamic table is invalid: SHT_DYNAMIC will be used");
1935 DynamicTable
= FromSec
;
1938 parseDynamicTable();
1941 template <typename ELFT
>
1942 ELFDumper
<ELFT
>::ELFDumper(const object::ELFObjectFile
<ELFT
> &O
,
1943 ScopedPrinter
&Writer
)
1944 : ObjDumper(Writer
, O
.getFileName()), ObjF(O
), Obj(O
.getELFFile()),
1945 FileName(O
.getFileName()), DynRelRegion(O
, *this),
1946 DynRelaRegion(O
, *this), DynRelrRegion(O
, *this),
1947 DynPLTRelRegion(O
, *this), DynSymTabShndxRegion(O
, *this),
1948 DynamicTable(O
, *this) {
1949 if (!O
.IsContentValid())
1952 typename
ELFT::ShdrRange Sections
= cantFail(Obj
.sections());
1953 for (const Elf_Shdr
&Sec
: Sections
) {
1954 switch (Sec
.sh_type
) {
1955 case ELF::SHT_SYMTAB
:
1957 DotSymtabSec
= &Sec
;
1959 case ELF::SHT_DYNSYM
:
1961 DotDynsymSec
= &Sec
;
1963 if (!DynSymRegion
) {
1964 Expected
<DynRegionInfo
> RegOrErr
=
1965 createDRI(Sec
.sh_offset
, Sec
.sh_size
, Sec
.sh_entsize
);
1967 DynSymRegion
= *RegOrErr
;
1968 DynSymRegion
->Context
= describe(Sec
);
1970 if (Expected
<StringRef
> E
= Obj
.getStringTableForSymtab(Sec
))
1971 DynamicStringTable
= *E
;
1973 reportUniqueWarning("unable to get the string table for the " +
1974 describe(Sec
) + ": " + toString(E
.takeError()));
1976 reportUniqueWarning("unable to read dynamic symbols from " +
1977 describe(Sec
) + ": " +
1978 toString(RegOrErr
.takeError()));
1982 case ELF::SHT_SYMTAB_SHNDX
: {
1983 uint32_t SymtabNdx
= Sec
.sh_link
;
1984 if (SymtabNdx
>= Sections
.size()) {
1985 reportUniqueWarning(
1986 "unable to get the associated symbol table for " + describe(Sec
) +
1987 ": sh_link (" + Twine(SymtabNdx
) +
1988 ") is greater than or equal to the total number of sections (" +
1989 Twine(Sections
.size()) + ")");
1993 if (Expected
<ArrayRef
<Elf_Word
>> ShndxTableOrErr
=
1994 Obj
.getSHNDXTable(Sec
)) {
1995 if (!ShndxTables
.insert({&Sections
[SymtabNdx
], *ShndxTableOrErr
})
1997 reportUniqueWarning(
1998 "multiple SHT_SYMTAB_SHNDX sections are linked to " +
2001 reportUniqueWarning(ShndxTableOrErr
.takeError());
2005 case ELF::SHT_GNU_versym
:
2006 if (!SymbolVersionSection
)
2007 SymbolVersionSection
= &Sec
;
2009 case ELF::SHT_GNU_verdef
:
2010 if (!SymbolVersionDefSection
)
2011 SymbolVersionDefSection
= &Sec
;
2013 case ELF::SHT_GNU_verneed
:
2014 if (!SymbolVersionNeedSection
)
2015 SymbolVersionNeedSection
= &Sec
;
2017 case ELF::SHT_LLVM_ADDRSIG
:
2019 DotAddrsigSec
= &Sec
;
2027 template <typename ELFT
> void ELFDumper
<ELFT
>::parseDynamicTable() {
2028 auto toMappedAddr
= [&](uint64_t Tag
, uint64_t VAddr
) -> const uint8_t * {
2029 auto MappedAddrOrError
= Obj
.toMappedAddr(VAddr
, [&](const Twine
&Msg
) {
2030 this->reportUniqueWarning(Msg
);
2031 return Error::success();
2033 if (!MappedAddrOrError
) {
2034 this->reportUniqueWarning("unable to parse DT_" +
2035 Obj
.getDynamicTagAsString(Tag
) + ": " +
2036 llvm::toString(MappedAddrOrError
.takeError()));
2039 return MappedAddrOrError
.get();
2042 const char *StringTableBegin
= nullptr;
2043 uint64_t StringTableSize
= 0;
2044 std::optional
<DynRegionInfo
> DynSymFromTable
;
2045 for (const Elf_Dyn
&Dyn
: dynamic_table()) {
2046 if (Obj
.getHeader().e_machine
== EM_AARCH64
) {
2047 switch (Dyn
.d_tag
) {
2048 case ELF::DT_AARCH64_AUTH_RELRSZ
:
2049 DynRelrRegion
.Size
= Dyn
.getVal();
2050 DynRelrRegion
.SizePrintName
= "DT_AARCH64_AUTH_RELRSZ value";
2052 case ELF::DT_AARCH64_AUTH_RELRENT
:
2053 DynRelrRegion
.EntSize
= Dyn
.getVal();
2054 DynRelrRegion
.EntSizePrintName
= "DT_AARCH64_AUTH_RELRENT value";
2058 switch (Dyn
.d_tag
) {
2060 HashTable
= reinterpret_cast<const Elf_Hash
*>(
2061 toMappedAddr(Dyn
.getTag(), Dyn
.getPtr()));
2063 case ELF::DT_GNU_HASH
:
2064 GnuHashTable
= reinterpret_cast<const Elf_GnuHash
*>(
2065 toMappedAddr(Dyn
.getTag(), Dyn
.getPtr()));
2067 case ELF::DT_STRTAB
:
2068 StringTableBegin
= reinterpret_cast<const char *>(
2069 toMappedAddr(Dyn
.getTag(), Dyn
.getPtr()));
2072 StringTableSize
= Dyn
.getVal();
2074 case ELF::DT_SYMTAB
: {
2075 // If we can't map the DT_SYMTAB value to an address (e.g. when there are
2076 // no program headers), we ignore its value.
2077 if (const uint8_t *VA
= toMappedAddr(Dyn
.getTag(), Dyn
.getPtr())) {
2078 DynSymFromTable
.emplace(ObjF
, *this);
2079 DynSymFromTable
->Addr
= VA
;
2080 DynSymFromTable
->EntSize
= sizeof(Elf_Sym
);
2081 DynSymFromTable
->EntSizePrintName
= "";
2085 case ELF::DT_SYMENT
: {
2086 uint64_t Val
= Dyn
.getVal();
2087 if (Val
!= sizeof(Elf_Sym
))
2088 this->reportUniqueWarning("DT_SYMENT value of 0x" +
2089 Twine::utohexstr(Val
) +
2090 " is not the size of a symbol (0x" +
2091 Twine::utohexstr(sizeof(Elf_Sym
)) + ")");
2095 DynRelaRegion
.Addr
= toMappedAddr(Dyn
.getTag(), Dyn
.getPtr());
2097 case ELF::DT_RELASZ
:
2098 DynRelaRegion
.Size
= Dyn
.getVal();
2099 DynRelaRegion
.SizePrintName
= "DT_RELASZ value";
2101 case ELF::DT_RELAENT
:
2102 DynRelaRegion
.EntSize
= Dyn
.getVal();
2103 DynRelaRegion
.EntSizePrintName
= "DT_RELAENT value";
2105 case ELF::DT_SONAME
:
2106 SONameOffset
= Dyn
.getVal();
2109 DynRelRegion
.Addr
= toMappedAddr(Dyn
.getTag(), Dyn
.getPtr());
2112 DynRelRegion
.Size
= Dyn
.getVal();
2113 DynRelRegion
.SizePrintName
= "DT_RELSZ value";
2115 case ELF::DT_RELENT
:
2116 DynRelRegion
.EntSize
= Dyn
.getVal();
2117 DynRelRegion
.EntSizePrintName
= "DT_RELENT value";
2120 case ELF::DT_ANDROID_RELR
:
2121 case ELF::DT_AARCH64_AUTH_RELR
:
2122 DynRelrRegion
.Addr
= toMappedAddr(Dyn
.getTag(), Dyn
.getPtr());
2124 case ELF::DT_RELRSZ
:
2125 case ELF::DT_ANDROID_RELRSZ
:
2126 case ELF::DT_AARCH64_AUTH_RELRSZ
:
2127 DynRelrRegion
.Size
= Dyn
.getVal();
2128 DynRelrRegion
.SizePrintName
= Dyn
.d_tag
== ELF::DT_RELRSZ
2130 : "DT_ANDROID_RELRSZ value";
2132 case ELF::DT_RELRENT
:
2133 case ELF::DT_ANDROID_RELRENT
:
2134 case ELF::DT_AARCH64_AUTH_RELRENT
:
2135 DynRelrRegion
.EntSize
= Dyn
.getVal();
2136 DynRelrRegion
.EntSizePrintName
= Dyn
.d_tag
== ELF::DT_RELRENT
2137 ? "DT_RELRENT value"
2138 : "DT_ANDROID_RELRENT value";
2140 case ELF::DT_PLTREL
:
2141 if (Dyn
.getVal() == DT_REL
)
2142 DynPLTRelRegion
.EntSize
= sizeof(Elf_Rel
);
2143 else if (Dyn
.getVal() == DT_RELA
)
2144 DynPLTRelRegion
.EntSize
= sizeof(Elf_Rela
);
2146 reportUniqueWarning(Twine("unknown DT_PLTREL value of ") +
2147 Twine((uint64_t)Dyn
.getVal()));
2148 DynPLTRelRegion
.EntSizePrintName
= "PLTREL entry size";
2150 case ELF::DT_JMPREL
:
2151 DynPLTRelRegion
.Addr
= toMappedAddr(Dyn
.getTag(), Dyn
.getPtr());
2153 case ELF::DT_PLTRELSZ
:
2154 DynPLTRelRegion
.Size
= Dyn
.getVal();
2155 DynPLTRelRegion
.SizePrintName
= "DT_PLTRELSZ value";
2157 case ELF::DT_SYMTAB_SHNDX
:
2158 DynSymTabShndxRegion
.Addr
= toMappedAddr(Dyn
.getTag(), Dyn
.getPtr());
2159 DynSymTabShndxRegion
.EntSize
= sizeof(Elf_Word
);
2164 if (StringTableBegin
) {
2165 const uint64_t FileSize
= Obj
.getBufSize();
2166 const uint64_t Offset
= (const uint8_t *)StringTableBegin
- Obj
.base();
2167 if (StringTableSize
> FileSize
- Offset
)
2168 reportUniqueWarning(
2169 "the dynamic string table at 0x" + Twine::utohexstr(Offset
) +
2170 " goes past the end of the file (0x" + Twine::utohexstr(FileSize
) +
2171 ") with DT_STRSZ = 0x" + Twine::utohexstr(StringTableSize
));
2173 DynamicStringTable
= StringRef(StringTableBegin
, StringTableSize
);
2176 const bool IsHashTableSupported
= getHashTableEntSize() == 4;
2178 // Often we find the information about the dynamic symbol table
2179 // location in the SHT_DYNSYM section header. However, the value in
2180 // DT_SYMTAB has priority, because it is used by dynamic loaders to
2181 // locate .dynsym at runtime. The location we find in the section header
2182 // and the location we find here should match.
2183 if (DynSymFromTable
&& DynSymFromTable
->Addr
!= DynSymRegion
->Addr
)
2184 reportUniqueWarning(
2185 createError("SHT_DYNSYM section header and DT_SYMTAB disagree about "
2186 "the location of the dynamic symbol table"));
2188 // According to the ELF gABI: "The number of symbol table entries should
2189 // equal nchain". Check to see if the DT_HASH hash table nchain value
2190 // conflicts with the number of symbols in the dynamic symbol table
2191 // according to the section header.
2192 if (HashTable
&& IsHashTableSupported
) {
2193 if (DynSymRegion
->EntSize
== 0)
2194 reportUniqueWarning("SHT_DYNSYM section has sh_entsize == 0");
2195 else if (HashTable
->nchain
!= DynSymRegion
->Size
/ DynSymRegion
->EntSize
)
2196 reportUniqueWarning(
2197 "hash table nchain (" + Twine(HashTable
->nchain
) +
2198 ") differs from symbol count derived from SHT_DYNSYM section "
2200 Twine(DynSymRegion
->Size
/ DynSymRegion
->EntSize
) + ")");
2204 // Delay the creation of the actual dynamic symbol table until now, so that
2205 // checks can always be made against the section header-based properties,
2206 // without worrying about tag order.
2207 if (DynSymFromTable
) {
2208 if (!DynSymRegion
) {
2209 DynSymRegion
= DynSymFromTable
;
2211 DynSymRegion
->Addr
= DynSymFromTable
->Addr
;
2212 DynSymRegion
->EntSize
= DynSymFromTable
->EntSize
;
2213 DynSymRegion
->EntSizePrintName
= DynSymFromTable
->EntSizePrintName
;
2217 // Derive the dynamic symbol table size from the DT_HASH hash table, if
2219 if (HashTable
&& IsHashTableSupported
&& DynSymRegion
) {
2220 const uint64_t FileSize
= Obj
.getBufSize();
2221 const uint64_t DerivedSize
=
2222 (uint64_t)HashTable
->nchain
* DynSymRegion
->EntSize
;
2223 const uint64_t Offset
= (const uint8_t *)DynSymRegion
->Addr
- Obj
.base();
2224 if (DerivedSize
> FileSize
- Offset
)
2225 reportUniqueWarning(
2226 "the size (0x" + Twine::utohexstr(DerivedSize
) +
2227 ") of the dynamic symbol table at 0x" + Twine::utohexstr(Offset
) +
2228 ", derived from the hash table, goes past the end of the file (0x" +
2229 Twine::utohexstr(FileSize
) + ") and will be ignored");
2231 DynSymRegion
->Size
= HashTable
->nchain
* DynSymRegion
->EntSize
;
2235 template <typename ELFT
> void ELFDumper
<ELFT
>::printVersionInfo() {
2236 // Dump version symbol section.
2237 printVersionSymbolSection(SymbolVersionSection
);
2239 // Dump version definition section.
2240 printVersionDefinitionSection(SymbolVersionDefSection
);
2242 // Dump version dependency section.
2243 printVersionDependencySection(SymbolVersionNeedSection
);
2246 #define LLVM_READOBJ_DT_FLAG_ENT(prefix, enum) \
2247 { #enum, prefix##_##enum }
2249 const EnumEntry
<unsigned> ElfDynamicDTFlags
[] = {
2250 LLVM_READOBJ_DT_FLAG_ENT(DF
, ORIGIN
),
2251 LLVM_READOBJ_DT_FLAG_ENT(DF
, SYMBOLIC
),
2252 LLVM_READOBJ_DT_FLAG_ENT(DF
, TEXTREL
),
2253 LLVM_READOBJ_DT_FLAG_ENT(DF
, BIND_NOW
),
2254 LLVM_READOBJ_DT_FLAG_ENT(DF
, STATIC_TLS
)
2257 const EnumEntry
<unsigned> ElfDynamicDTFlags1
[] = {
2258 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NOW
),
2259 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, GLOBAL
),
2260 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, GROUP
),
2261 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NODELETE
),
2262 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, LOADFLTR
),
2263 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, INITFIRST
),
2264 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NOOPEN
),
2265 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, ORIGIN
),
2266 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, DIRECT
),
2267 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, TRANS
),
2268 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, INTERPOSE
),
2269 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NODEFLIB
),
2270 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NODUMP
),
2271 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, CONFALT
),
2272 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, ENDFILTEE
),
2273 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, DISPRELDNE
),
2274 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, DISPRELPND
),
2275 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NODIRECT
),
2276 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, IGNMULDEF
),
2277 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NOKSYMS
),
2278 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NOHDR
),
2279 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, EDITED
),
2280 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NORELOC
),
2281 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, SYMINTPOSE
),
2282 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, GLOBAUDIT
),
2283 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, SINGLETON
),
2284 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, PIE
),
2287 const EnumEntry
<unsigned> ElfDynamicDTMipsFlags
[] = {
2288 LLVM_READOBJ_DT_FLAG_ENT(RHF
, NONE
),
2289 LLVM_READOBJ_DT_FLAG_ENT(RHF
, QUICKSTART
),
2290 LLVM_READOBJ_DT_FLAG_ENT(RHF
, NOTPOT
),
2291 LLVM_READOBJ_DT_FLAG_ENT(RHS
, NO_LIBRARY_REPLACEMENT
),
2292 LLVM_READOBJ_DT_FLAG_ENT(RHF
, NO_MOVE
),
2293 LLVM_READOBJ_DT_FLAG_ENT(RHF
, SGI_ONLY
),
2294 LLVM_READOBJ_DT_FLAG_ENT(RHF
, GUARANTEE_INIT
),
2295 LLVM_READOBJ_DT_FLAG_ENT(RHF
, DELTA_C_PLUS_PLUS
),
2296 LLVM_READOBJ_DT_FLAG_ENT(RHF
, GUARANTEE_START_INIT
),
2297 LLVM_READOBJ_DT_FLAG_ENT(RHF
, PIXIE
),
2298 LLVM_READOBJ_DT_FLAG_ENT(RHF
, DEFAULT_DELAY_LOAD
),
2299 LLVM_READOBJ_DT_FLAG_ENT(RHF
, REQUICKSTART
),
2300 LLVM_READOBJ_DT_FLAG_ENT(RHF
, REQUICKSTARTED
),
2301 LLVM_READOBJ_DT_FLAG_ENT(RHF
, CORD
),
2302 LLVM_READOBJ_DT_FLAG_ENT(RHF
, NO_UNRES_UNDEF
),
2303 LLVM_READOBJ_DT_FLAG_ENT(RHF
, RLD_ORDER_SAFE
)
2306 #undef LLVM_READOBJ_DT_FLAG_ENT
2308 template <typename T
, typename TFlag
>
2309 void printFlags(T Value
, ArrayRef
<EnumEntry
<TFlag
>> Flags
, raw_ostream
&OS
) {
2310 SmallVector
<EnumEntry
<TFlag
>, 10> SetFlags
;
2311 for (const EnumEntry
<TFlag
> &Flag
: Flags
)
2312 if (Flag
.Value
!= 0 && (Value
& Flag
.Value
) == Flag
.Value
)
2313 SetFlags
.push_back(Flag
);
2315 for (const EnumEntry
<TFlag
> &Flag
: SetFlags
)
2316 OS
<< Flag
.Name
<< " ";
2319 template <class ELFT
>
2320 const typename
ELFT::Shdr
*
2321 ELFDumper
<ELFT
>::findSectionByName(StringRef Name
) const {
2322 for (const Elf_Shdr
&Shdr
: cantFail(Obj
.sections())) {
2323 if (Expected
<StringRef
> NameOrErr
= Obj
.getSectionName(Shdr
)) {
2324 if (*NameOrErr
== Name
)
2327 reportUniqueWarning("unable to read the name of " + describe(Shdr
) +
2328 ": " + toString(NameOrErr
.takeError()));
2334 template <class ELFT
>
2335 std::string ELFDumper
<ELFT
>::getDynamicEntry(uint64_t Type
,
2336 uint64_t Value
) const {
2337 auto FormatHexValue
= [](uint64_t V
) {
2339 raw_string_ostream
OS(Str
);
2340 const char *ConvChar
=
2341 (opts::Output
== opts::GNU
) ? "0x%" PRIx64
: "0x%" PRIX64
;
2342 OS
<< format(ConvChar
, V
);
2346 auto FormatFlags
= [](uint64_t V
,
2347 llvm::ArrayRef
<llvm::EnumEntry
<unsigned int>> Array
) {
2349 raw_string_ostream
OS(Str
);
2350 printFlags(V
, Array
, OS
);
2354 // Handle custom printing of architecture specific tags
2355 switch (Obj
.getHeader().e_machine
) {
2358 case DT_AARCH64_BTI_PLT
:
2359 case DT_AARCH64_PAC_PLT
:
2360 case DT_AARCH64_VARIANT_PCS
:
2361 case DT_AARCH64_MEMTAG_GLOBALSSZ
:
2362 return std::to_string(Value
);
2363 case DT_AARCH64_MEMTAG_MODE
:
2366 return "Synchronous (0)";
2368 return "Asynchronous (1)";
2370 return (Twine("Unknown (") + Twine(Value
) + ")").str();
2372 case DT_AARCH64_MEMTAG_HEAP
:
2373 case DT_AARCH64_MEMTAG_STACK
:
2376 return "Disabled (0)";
2378 return "Enabled (1)";
2380 return (Twine("Unknown (") + Twine(Value
) + ")").str();
2382 case DT_AARCH64_MEMTAG_GLOBALS
:
2383 return (Twine("0x") + utohexstr(Value
, /*LowerCase=*/true)).str();
2390 case DT_HEXAGON_VER
:
2391 return std::to_string(Value
);
2392 case DT_HEXAGON_SYMSZ
:
2393 case DT_HEXAGON_PLT
:
2394 return FormatHexValue(Value
);
2401 case DT_MIPS_RLD_VERSION
:
2402 case DT_MIPS_LOCAL_GOTNO
:
2403 case DT_MIPS_SYMTABNO
:
2404 case DT_MIPS_UNREFEXTNO
:
2405 return std::to_string(Value
);
2406 case DT_MIPS_TIME_STAMP
:
2407 case DT_MIPS_ICHECKSUM
:
2408 case DT_MIPS_IVERSION
:
2409 case DT_MIPS_BASE_ADDRESS
:
2411 case DT_MIPS_CONFLICT
:
2412 case DT_MIPS_LIBLIST
:
2413 case DT_MIPS_CONFLICTNO
:
2414 case DT_MIPS_LIBLISTNO
:
2415 case DT_MIPS_GOTSYM
:
2416 case DT_MIPS_HIPAGENO
:
2417 case DT_MIPS_RLD_MAP
:
2418 case DT_MIPS_DELTA_CLASS
:
2419 case DT_MIPS_DELTA_CLASS_NO
:
2420 case DT_MIPS_DELTA_INSTANCE
:
2421 case DT_MIPS_DELTA_RELOC
:
2422 case DT_MIPS_DELTA_RELOC_NO
:
2423 case DT_MIPS_DELTA_SYM
:
2424 case DT_MIPS_DELTA_SYM_NO
:
2425 case DT_MIPS_DELTA_CLASSSYM
:
2426 case DT_MIPS_DELTA_CLASSSYM_NO
:
2427 case DT_MIPS_CXX_FLAGS
:
2428 case DT_MIPS_PIXIE_INIT
:
2429 case DT_MIPS_SYMBOL_LIB
:
2430 case DT_MIPS_LOCALPAGE_GOTIDX
:
2431 case DT_MIPS_LOCAL_GOTIDX
:
2432 case DT_MIPS_HIDDEN_GOTIDX
:
2433 case DT_MIPS_PROTECTED_GOTIDX
:
2434 case DT_MIPS_OPTIONS
:
2435 case DT_MIPS_INTERFACE
:
2436 case DT_MIPS_DYNSTR_ALIGN
:
2437 case DT_MIPS_INTERFACE_SIZE
:
2438 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR
:
2439 case DT_MIPS_PERF_SUFFIX
:
2440 case DT_MIPS_COMPACT_SIZE
:
2441 case DT_MIPS_GP_VALUE
:
2442 case DT_MIPS_AUX_DYNAMIC
:
2443 case DT_MIPS_PLTGOT
:
2445 case DT_MIPS_RLD_MAP_REL
:
2447 return FormatHexValue(Value
);
2449 return FormatFlags(Value
, ArrayRef(ElfDynamicDTMipsFlags
));
2460 if (Value
== DT_REL
)
2462 if (Value
== DT_RELA
)
2476 case DT_PREINIT_ARRAY
:
2483 return FormatHexValue(Value
);
2488 return std::to_string(Value
);
2496 case DT_INIT_ARRAYSZ
:
2497 case DT_FINI_ARRAYSZ
:
2498 case DT_PREINIT_ARRAYSZ
:
2501 case DT_AARCH64_AUTH_RELRSZ
:
2502 case DT_AARCH64_AUTH_RELRENT
:
2503 case DT_ANDROID_RELSZ
:
2504 case DT_ANDROID_RELASZ
:
2505 return std::to_string(Value
) + " (bytes)";
2513 const std::map
<uint64_t, const char *> TagNames
= {
2514 {DT_NEEDED
, "Shared library"}, {DT_SONAME
, "Library soname"},
2515 {DT_AUXILIARY
, "Auxiliary library"}, {DT_USED
, "Not needed object"},
2516 {DT_FILTER
, "Filter library"}, {DT_RPATH
, "Library rpath"},
2517 {DT_RUNPATH
, "Library runpath"},
2520 return (Twine(TagNames
.at(Type
)) + ": [" + getDynamicString(Value
) + "]")
2524 return FormatFlags(Value
, ArrayRef(ElfDynamicDTFlags
));
2526 return FormatFlags(Value
, ArrayRef(ElfDynamicDTFlags1
));
2528 return FormatHexValue(Value
);
2532 template <class ELFT
>
2533 StringRef ELFDumper
<ELFT
>::getDynamicString(uint64_t Value
) const {
2534 if (DynamicStringTable
.empty() && !DynamicStringTable
.data()) {
2535 reportUniqueWarning("string table was not found");
2539 auto WarnAndReturn
= [this](const Twine
&Msg
, uint64_t Offset
) {
2540 reportUniqueWarning("string table at offset 0x" + Twine::utohexstr(Offset
) +
2545 const uint64_t FileSize
= Obj
.getBufSize();
2546 const uint64_t Offset
=
2547 (const uint8_t *)DynamicStringTable
.data() - Obj
.base();
2548 if (DynamicStringTable
.size() > FileSize
- Offset
)
2549 return WarnAndReturn(" with size 0x" +
2550 Twine::utohexstr(DynamicStringTable
.size()) +
2551 " goes past the end of the file (0x" +
2552 Twine::utohexstr(FileSize
) + ")",
2555 if (Value
>= DynamicStringTable
.size())
2556 return WarnAndReturn(
2557 ": unable to read the string at 0x" + Twine::utohexstr(Offset
+ Value
) +
2558 ": it goes past the end of the table (0x" +
2559 Twine::utohexstr(Offset
+ DynamicStringTable
.size()) + ")",
2562 if (DynamicStringTable
.back() != '\0')
2563 return WarnAndReturn(": unable to read the string at 0x" +
2564 Twine::utohexstr(Offset
+ Value
) +
2565 ": the string table is not null-terminated",
2568 return DynamicStringTable
.data() + Value
;
2571 template <class ELFT
> void ELFDumper
<ELFT
>::printUnwindInfo() {
2572 DwarfCFIEH::PrinterContext
<ELFT
> Ctx(W
, ObjF
);
2573 Ctx
.printUnwindInformation();
2576 // The namespace is needed to fix the compilation with GCC older than 7.0+.
2578 template <> void ELFDumper
<ELF32LE
>::printUnwindInfo() {
2579 if (Obj
.getHeader().e_machine
== EM_ARM
) {
2580 ARM::EHABI::PrinterContext
<ELF32LE
> Ctx(W
, Obj
, ObjF
.getFileName(),
2582 Ctx
.PrintUnwindInformation();
2584 DwarfCFIEH::PrinterContext
<ELF32LE
> Ctx(W
, ObjF
);
2585 Ctx
.printUnwindInformation();
2589 template <class ELFT
> void ELFDumper
<ELFT
>::printNeededLibraries() {
2590 ListScope
D(W
, "NeededLibraries");
2592 std::vector
<StringRef
> Libs
;
2593 for (const auto &Entry
: dynamic_table())
2594 if (Entry
.d_tag
== ELF::DT_NEEDED
)
2595 Libs
.push_back(getDynamicString(Entry
.d_un
.d_val
));
2599 for (StringRef L
: Libs
)
2603 template <class ELFT
>
2604 static Error
checkHashTable(const ELFDumper
<ELFT
> &Dumper
,
2605 const typename
ELFT::Hash
*H
,
2606 bool *IsHeaderValid
= nullptr) {
2607 const ELFFile
<ELFT
> &Obj
= Dumper
.getElfObject().getELFFile();
2608 const uint64_t SecOffset
= (const uint8_t *)H
- Obj
.base();
2609 if (Dumper
.getHashTableEntSize() == 8) {
2610 auto It
= llvm::find_if(ElfMachineType
, [&](const EnumEntry
<unsigned> &E
) {
2611 return E
.Value
== Obj
.getHeader().e_machine
;
2614 *IsHeaderValid
= false;
2615 return createError("the hash table at 0x" + Twine::utohexstr(SecOffset
) +
2616 " is not supported: it contains non-standard 8 "
2617 "byte entries on " +
2618 It
->AltName
+ " platform");
2621 auto MakeError
= [&](const Twine
&Msg
= "") {
2622 return createError("the hash table at offset 0x" +
2623 Twine::utohexstr(SecOffset
) +
2624 " goes past the end of the file (0x" +
2625 Twine::utohexstr(Obj
.getBufSize()) + ")" + Msg
);
2628 // Each SHT_HASH section starts from two 32-bit fields: nbucket and nchain.
2629 const unsigned HeaderSize
= 2 * sizeof(typename
ELFT::Word
);
2632 *IsHeaderValid
= Obj
.getBufSize() - SecOffset
>= HeaderSize
;
2634 if (Obj
.getBufSize() - SecOffset
< HeaderSize
)
2637 if (Obj
.getBufSize() - SecOffset
- HeaderSize
<
2638 ((uint64_t)H
->nbucket
+ H
->nchain
) * sizeof(typename
ELFT::Word
))
2639 return MakeError(", nbucket = " + Twine(H
->nbucket
) +
2640 ", nchain = " + Twine(H
->nchain
));
2641 return Error::success();
2644 template <class ELFT
>
2645 static Error
checkGNUHashTable(const ELFFile
<ELFT
> &Obj
,
2646 const typename
ELFT::GnuHash
*GnuHashTable
,
2647 bool *IsHeaderValid
= nullptr) {
2648 const uint8_t *TableData
= reinterpret_cast<const uint8_t *>(GnuHashTable
);
2649 assert(TableData
>= Obj
.base() && TableData
< Obj
.base() + Obj
.getBufSize() &&
2650 "GnuHashTable must always point to a location inside the file");
2652 uint64_t TableOffset
= TableData
- Obj
.base();
2654 *IsHeaderValid
= TableOffset
+ /*Header size:*/ 16 < Obj
.getBufSize();
2655 if (TableOffset
+ 16 + (uint64_t)GnuHashTable
->nbuckets
* 4 +
2656 (uint64_t)GnuHashTable
->maskwords
* sizeof(typename
ELFT::Off
) >=
2658 return createError("unable to dump the SHT_GNU_HASH "
2660 Twine::utohexstr(TableOffset
) +
2661 ": it goes past the end of the file");
2662 return Error::success();
2665 template <typename ELFT
> void ELFDumper
<ELFT
>::printHashTable() {
2666 DictScope
D(W
, "HashTable");
2671 Error Err
= checkHashTable(*this, HashTable
, &IsHeaderValid
);
2672 if (IsHeaderValid
) {
2673 W
.printNumber("Num Buckets", HashTable
->nbucket
);
2674 W
.printNumber("Num Chains", HashTable
->nchain
);
2678 reportUniqueWarning(std::move(Err
));
2682 W
.printList("Buckets", HashTable
->buckets());
2683 W
.printList("Chains", HashTable
->chains());
2686 template <class ELFT
>
2687 static Expected
<ArrayRef
<typename
ELFT::Word
>>
2688 getGnuHashTableChains(std::optional
<DynRegionInfo
> DynSymRegion
,
2689 const typename
ELFT::GnuHash
*GnuHashTable
) {
2691 return createError("no dynamic symbol table found");
2693 ArrayRef
<typename
ELFT::Sym
> DynSymTable
=
2694 DynSymRegion
->template getAsArrayRef
<typename
ELFT::Sym
>();
2695 size_t NumSyms
= DynSymTable
.size();
2697 return createError("the dynamic symbol table is empty");
2699 if (GnuHashTable
->symndx
< NumSyms
)
2700 return GnuHashTable
->values(NumSyms
);
2702 // A normal empty GNU hash table section produced by linker might have
2703 // symndx set to the number of dynamic symbols + 1 (for the zero symbol)
2704 // and have dummy null values in the Bloom filter and in the buckets
2705 // vector (or no values at all). It happens because the value of symndx is not
2706 // important for dynamic loaders when the GNU hash table is empty. They just
2707 // skip the whole object during symbol lookup. In such cases, the symndx value
2708 // is irrelevant and we should not report a warning.
2709 ArrayRef
<typename
ELFT::Word
> Buckets
= GnuHashTable
->buckets();
2710 if (!llvm::all_of(Buckets
, [](typename
ELFT::Word V
) { return V
== 0; }))
2712 "the first hashed symbol index (" + Twine(GnuHashTable
->symndx
) +
2713 ") is greater than or equal to the number of dynamic symbols (" +
2714 Twine(NumSyms
) + ")");
2715 // There is no way to represent an array of (dynamic symbols count - symndx)
2717 return ArrayRef
<typename
ELFT::Word
>();
2720 template <typename ELFT
>
2721 void ELFDumper
<ELFT
>::printGnuHashTable() {
2722 DictScope
D(W
, "GnuHashTable");
2727 Error Err
= checkGNUHashTable
<ELFT
>(Obj
, GnuHashTable
, &IsHeaderValid
);
2728 if (IsHeaderValid
) {
2729 W
.printNumber("Num Buckets", GnuHashTable
->nbuckets
);
2730 W
.printNumber("First Hashed Symbol Index", GnuHashTable
->symndx
);
2731 W
.printNumber("Num Mask Words", GnuHashTable
->maskwords
);
2732 W
.printNumber("Shift Count", GnuHashTable
->shift2
);
2736 reportUniqueWarning(std::move(Err
));
2740 ArrayRef
<typename
ELFT::Off
> BloomFilter
= GnuHashTable
->filter();
2741 W
.printHexList("Bloom Filter", BloomFilter
);
2743 ArrayRef
<Elf_Word
> Buckets
= GnuHashTable
->buckets();
2744 W
.printList("Buckets", Buckets
);
2746 Expected
<ArrayRef
<Elf_Word
>> Chains
=
2747 getGnuHashTableChains
<ELFT
>(DynSymRegion
, GnuHashTable
);
2749 reportUniqueWarning("unable to dump 'Values' for the SHT_GNU_HASH "
2751 toString(Chains
.takeError()));
2755 W
.printHexList("Values", *Chains
);
2758 template <typename ELFT
> void ELFDumper
<ELFT
>::printHashHistograms() {
2759 // Print histogram for the .hash section.
2760 if (this->HashTable
) {
2761 if (Error E
= checkHashTable
<ELFT
>(*this, this->HashTable
))
2762 this->reportUniqueWarning(std::move(E
));
2764 printHashHistogram(*this->HashTable
);
2767 // Print histogram for the .gnu.hash section.
2768 if (this->GnuHashTable
) {
2769 if (Error E
= checkGNUHashTable
<ELFT
>(this->Obj
, this->GnuHashTable
))
2770 this->reportUniqueWarning(std::move(E
));
2772 printGnuHashHistogram(*this->GnuHashTable
);
2776 template <typename ELFT
>
2777 void ELFDumper
<ELFT
>::printHashHistogram(const Elf_Hash
&HashTable
) const {
2778 size_t NBucket
= HashTable
.nbucket
;
2779 size_t NChain
= HashTable
.nchain
;
2780 ArrayRef
<Elf_Word
> Buckets
= HashTable
.buckets();
2781 ArrayRef
<Elf_Word
> Chains
= HashTable
.chains();
2782 size_t TotalSyms
= 0;
2783 // If hash table is correct, we have at least chains with 0 length.
2784 size_t MaxChain
= 1;
2786 if (NChain
== 0 || NBucket
== 0)
2789 std::vector
<size_t> ChainLen(NBucket
, 0);
2790 // Go over all buckets and note chain lengths of each bucket (total
2791 // unique chain lengths).
2792 for (size_t B
= 0; B
< NBucket
; ++B
) {
2793 BitVector
Visited(NChain
);
2794 for (size_t C
= Buckets
[B
]; C
< NChain
; C
= Chains
[C
]) {
2795 if (C
== ELF::STN_UNDEF
)
2798 this->reportUniqueWarning(
2799 ".hash section is invalid: bucket " + Twine(C
) +
2800 ": a cycle was detected in the linked chain");
2804 if (MaxChain
<= ++ChainLen
[B
])
2807 TotalSyms
+= ChainLen
[B
];
2813 std::vector
<size_t> Count(MaxChain
, 0);
2814 // Count how long is the chain for each bucket.
2815 for (size_t B
= 0; B
< NBucket
; B
++)
2816 ++Count
[ChainLen
[B
]];
2817 // Print Number of buckets with each chain lengths and their cumulative
2818 // coverage of the symbols.
2819 printHashHistogramStats(NBucket
, MaxChain
, TotalSyms
, Count
, /*IsGnu=*/false);
2822 template <class ELFT
>
2823 void ELFDumper
<ELFT
>::printGnuHashHistogram(
2824 const Elf_GnuHash
&GnuHashTable
) const {
2825 Expected
<ArrayRef
<Elf_Word
>> ChainsOrErr
=
2826 getGnuHashTableChains
<ELFT
>(this->DynSymRegion
, &GnuHashTable
);
2828 this->reportUniqueWarning("unable to print the GNU hash table histogram: " +
2829 toString(ChainsOrErr
.takeError()));
2833 ArrayRef
<Elf_Word
> Chains
= *ChainsOrErr
;
2834 size_t Symndx
= GnuHashTable
.symndx
;
2835 size_t TotalSyms
= 0;
2836 size_t MaxChain
= 1;
2838 size_t NBucket
= GnuHashTable
.nbuckets
;
2839 if (Chains
.empty() || NBucket
== 0)
2842 ArrayRef
<Elf_Word
> Buckets
= GnuHashTable
.buckets();
2843 std::vector
<size_t> ChainLen(NBucket
, 0);
2844 for (size_t B
= 0; B
< NBucket
; ++B
) {
2848 for (size_t C
= Buckets
[B
] - Symndx
;
2849 C
< Chains
.size() && (Chains
[C
] & 1) == 0; ++C
)
2850 if (MaxChain
< ++Len
)
2860 std::vector
<size_t> Count(MaxChain
, 0);
2861 for (size_t B
= 0; B
< NBucket
; ++B
)
2862 ++Count
[ChainLen
[B
]];
2863 // Print Number of buckets with each chain lengths and their cumulative
2864 // coverage of the symbols.
2865 printHashHistogramStats(NBucket
, MaxChain
, TotalSyms
, Count
, /*IsGnu=*/true);
2868 template <typename ELFT
> void ELFDumper
<ELFT
>::printLoadName() {
2869 StringRef SOName
= "<Not found>";
2871 SOName
= getDynamicString(*SONameOffset
);
2872 W
.printString("LoadName", SOName
);
2875 template <class ELFT
> void ELFDumper
<ELFT
>::printArchSpecificInfo() {
2876 switch (Obj
.getHeader().e_machine
) {
2879 printAttributes(ELF::SHT_ARM_ATTRIBUTES
,
2880 std::make_unique
<ARMAttributeParser
>(&W
),
2881 llvm::endianness::little
);
2883 reportUniqueWarning("attribute printing not implemented for big-endian "
2888 printAttributes(ELF::SHT_RISCV_ATTRIBUTES
,
2889 std::make_unique
<RISCVAttributeParser
>(&W
),
2890 llvm::endianness::little
);
2892 reportUniqueWarning("attribute printing not implemented for big-endian "
2896 printAttributes(ELF::SHT_MSP430_ATTRIBUTES
,
2897 std::make_unique
<MSP430AttributeParser
>(&W
),
2898 llvm::endianness::little
);
2901 printMipsABIFlags();
2904 MipsGOTParser
<ELFT
> Parser(*this);
2905 if (Error E
= Parser
.findGOT(dynamic_table(), dynamic_symbols()))
2906 reportUniqueWarning(std::move(E
));
2907 else if (!Parser
.isGotEmpty())
2908 printMipsGOT(Parser
);
2910 if (Error E
= Parser
.findPLT(dynamic_table()))
2911 reportUniqueWarning(std::move(E
));
2912 else if (!Parser
.isPltEmpty())
2913 printMipsPLT(Parser
);
2921 template <class ELFT
>
2922 void ELFDumper
<ELFT
>::printAttributes(
2923 unsigned AttrShType
, std::unique_ptr
<ELFAttributeParser
> AttrParser
,
2924 llvm::endianness Endianness
) {
2925 assert((AttrShType
!= ELF::SHT_NULL
) && AttrParser
&&
2926 "Incomplete ELF attribute implementation");
2927 DictScope
BA(W
, "BuildAttributes");
2928 for (const Elf_Shdr
&Sec
: cantFail(Obj
.sections())) {
2929 if (Sec
.sh_type
!= AttrShType
)
2932 ArrayRef
<uint8_t> Contents
;
2933 if (Expected
<ArrayRef
<uint8_t>> ContentOrErr
=
2934 Obj
.getSectionContents(Sec
)) {
2935 Contents
= *ContentOrErr
;
2936 if (Contents
.empty()) {
2937 reportUniqueWarning("the " + describe(Sec
) + " is empty");
2941 reportUniqueWarning("unable to read the content of the " + describe(Sec
) +
2942 ": " + toString(ContentOrErr
.takeError()));
2946 W
.printHex("FormatVersion", Contents
[0]);
2948 if (Error E
= AttrParser
->parse(Contents
, Endianness
))
2949 reportUniqueWarning("unable to dump attributes from the " +
2950 describe(Sec
) + ": " + toString(std::move(E
)));
2956 template <class ELFT
> class MipsGOTParser
{
2958 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT
)
2959 using Entry
= typename
ELFT::Addr
;
2960 using Entries
= ArrayRef
<Entry
>;
2962 const bool IsStatic
;
2963 const ELFFile
<ELFT
> &Obj
;
2964 const ELFDumper
<ELFT
> &Dumper
;
2966 MipsGOTParser(const ELFDumper
<ELFT
> &D
);
2967 Error
findGOT(Elf_Dyn_Range DynTable
, Elf_Sym_Range DynSyms
);
2968 Error
findPLT(Elf_Dyn_Range DynTable
);
2970 bool isGotEmpty() const { return GotEntries
.empty(); }
2971 bool isPltEmpty() const { return PltEntries
.empty(); }
2973 uint64_t getGp() const;
2975 const Entry
*getGotLazyResolver() const;
2976 const Entry
*getGotModulePointer() const;
2977 const Entry
*getPltLazyResolver() const;
2978 const Entry
*getPltModulePointer() const;
2980 Entries
getLocalEntries() const;
2981 Entries
getGlobalEntries() const;
2982 Entries
getOtherEntries() const;
2983 Entries
getPltEntries() const;
2985 uint64_t getGotAddress(const Entry
* E
) const;
2986 int64_t getGotOffset(const Entry
* E
) const;
2987 const Elf_Sym
*getGotSym(const Entry
*E
) const;
2989 uint64_t getPltAddress(const Entry
* E
) const;
2990 const Elf_Sym
*getPltSym(const Entry
*E
) const;
2992 StringRef
getPltStrTable() const { return PltStrTable
; }
2993 const Elf_Shdr
*getPltSymTable() const { return PltSymTable
; }
2996 const Elf_Shdr
*GotSec
;
3000 const Elf_Shdr
*PltSec
;
3001 const Elf_Shdr
*PltRelSec
;
3002 const Elf_Shdr
*PltSymTable
;
3005 Elf_Sym_Range GotDynSyms
;
3006 StringRef PltStrTable
;
3012 } // end anonymous namespace
3014 template <class ELFT
>
3015 MipsGOTParser
<ELFT
>::MipsGOTParser(const ELFDumper
<ELFT
> &D
)
3016 : IsStatic(D
.dynamic_table().empty()), Obj(D
.getElfObject().getELFFile()),
3017 Dumper(D
), GotSec(nullptr), LocalNum(0), GlobalNum(0), PltSec(nullptr),
3018 PltRelSec(nullptr), PltSymTable(nullptr),
3019 FileName(D
.getElfObject().getFileName()) {}
3021 template <class ELFT
>
3022 Error MipsGOTParser
<ELFT
>::findGOT(Elf_Dyn_Range DynTable
,
3023 Elf_Sym_Range DynSyms
) {
3024 // See "Global Offset Table" in Chapter 5 in the following document
3025 // for detailed GOT description.
3026 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
3028 // Find static GOT secton.
3030 GotSec
= Dumper
.findSectionByName(".got");
3032 return Error::success();
3034 ArrayRef
<uint8_t> Content
=
3035 unwrapOrError(FileName
, Obj
.getSectionContents(*GotSec
));
3036 GotEntries
= Entries(reinterpret_cast<const Entry
*>(Content
.data()),
3037 Content
.size() / sizeof(Entry
));
3038 LocalNum
= GotEntries
.size();
3039 return Error::success();
3042 // Lookup dynamic table tags which define the GOT layout.
3043 std::optional
<uint64_t> DtPltGot
;
3044 std::optional
<uint64_t> DtLocalGotNum
;
3045 std::optional
<uint64_t> DtGotSym
;
3046 for (const auto &Entry
: DynTable
) {
3047 switch (Entry
.getTag()) {
3048 case ELF::DT_PLTGOT
:
3049 DtPltGot
= Entry
.getVal();
3051 case ELF::DT_MIPS_LOCAL_GOTNO
:
3052 DtLocalGotNum
= Entry
.getVal();
3054 case ELF::DT_MIPS_GOTSYM
:
3055 DtGotSym
= Entry
.getVal();
3060 if (!DtPltGot
&& !DtLocalGotNum
&& !DtGotSym
)
3061 return Error::success();
3064 return createError("cannot find PLTGOT dynamic tag");
3066 return createError("cannot find MIPS_LOCAL_GOTNO dynamic tag");
3068 return createError("cannot find MIPS_GOTSYM dynamic tag");
3070 size_t DynSymTotal
= DynSyms
.size();
3071 if (*DtGotSym
> DynSymTotal
)
3072 return createError("DT_MIPS_GOTSYM value (" + Twine(*DtGotSym
) +
3073 ") exceeds the number of dynamic symbols (" +
3074 Twine(DynSymTotal
) + ")");
3076 GotSec
= findNotEmptySectionByAddress(Obj
, FileName
, *DtPltGot
);
3078 return createError("there is no non-empty GOT section at 0x" +
3079 Twine::utohexstr(*DtPltGot
));
3081 LocalNum
= *DtLocalGotNum
;
3082 GlobalNum
= DynSymTotal
- *DtGotSym
;
3084 ArrayRef
<uint8_t> Content
=
3085 unwrapOrError(FileName
, Obj
.getSectionContents(*GotSec
));
3086 GotEntries
= Entries(reinterpret_cast<const Entry
*>(Content
.data()),
3087 Content
.size() / sizeof(Entry
));
3088 GotDynSyms
= DynSyms
.drop_front(*DtGotSym
);
3090 return Error::success();
3093 template <class ELFT
>
3094 Error MipsGOTParser
<ELFT
>::findPLT(Elf_Dyn_Range DynTable
) {
3095 // Lookup dynamic table tags which define the PLT layout.
3096 std::optional
<uint64_t> DtMipsPltGot
;
3097 std::optional
<uint64_t> DtJmpRel
;
3098 for (const auto &Entry
: DynTable
) {
3099 switch (Entry
.getTag()) {
3100 case ELF::DT_MIPS_PLTGOT
:
3101 DtMipsPltGot
= Entry
.getVal();
3103 case ELF::DT_JMPREL
:
3104 DtJmpRel
= Entry
.getVal();
3109 if (!DtMipsPltGot
&& !DtJmpRel
)
3110 return Error::success();
3112 // Find PLT section.
3114 return createError("cannot find MIPS_PLTGOT dynamic tag");
3116 return createError("cannot find JMPREL dynamic tag");
3118 PltSec
= findNotEmptySectionByAddress(Obj
, FileName
, *DtMipsPltGot
);
3120 return createError("there is no non-empty PLTGOT section at 0x" +
3121 Twine::utohexstr(*DtMipsPltGot
));
3123 PltRelSec
= findNotEmptySectionByAddress(Obj
, FileName
, *DtJmpRel
);
3125 return createError("there is no non-empty RELPLT section at 0x" +
3126 Twine::utohexstr(*DtJmpRel
));
3128 if (Expected
<ArrayRef
<uint8_t>> PltContentOrErr
=
3129 Obj
.getSectionContents(*PltSec
))
3131 Entries(reinterpret_cast<const Entry
*>(PltContentOrErr
->data()),
3132 PltContentOrErr
->size() / sizeof(Entry
));
3134 return createError("unable to read PLTGOT section content: " +
3135 toString(PltContentOrErr
.takeError()));
3137 if (Expected
<const Elf_Shdr
*> PltSymTableOrErr
=
3138 Obj
.getSection(PltRelSec
->sh_link
))
3139 PltSymTable
= *PltSymTableOrErr
;
3141 return createError("unable to get a symbol table linked to the " +
3142 describe(Obj
, *PltRelSec
) + ": " +
3143 toString(PltSymTableOrErr
.takeError()));
3145 if (Expected
<StringRef
> StrTabOrErr
=
3146 Obj
.getStringTableForSymtab(*PltSymTable
))
3147 PltStrTable
= *StrTabOrErr
;
3149 return createError("unable to get a string table for the " +
3150 describe(Obj
, *PltSymTable
) + ": " +
3151 toString(StrTabOrErr
.takeError()));
3153 return Error::success();
3156 template <class ELFT
> uint64_t MipsGOTParser
<ELFT
>::getGp() const {
3157 return GotSec
->sh_addr
+ 0x7ff0;
3160 template <class ELFT
>
3161 const typename MipsGOTParser
<ELFT
>::Entry
*
3162 MipsGOTParser
<ELFT
>::getGotLazyResolver() const {
3163 return LocalNum
> 0 ? &GotEntries
[0] : nullptr;
3166 template <class ELFT
>
3167 const typename MipsGOTParser
<ELFT
>::Entry
*
3168 MipsGOTParser
<ELFT
>::getGotModulePointer() const {
3171 const Entry
&E
= GotEntries
[1];
3172 if ((E
>> (sizeof(Entry
) * 8 - 1)) == 0)
3177 template <class ELFT
>
3178 typename MipsGOTParser
<ELFT
>::Entries
3179 MipsGOTParser
<ELFT
>::getLocalEntries() const {
3180 size_t Skip
= getGotModulePointer() ? 2 : 1;
3181 if (LocalNum
- Skip
<= 0)
3183 return GotEntries
.slice(Skip
, LocalNum
- Skip
);
3186 template <class ELFT
>
3187 typename MipsGOTParser
<ELFT
>::Entries
3188 MipsGOTParser
<ELFT
>::getGlobalEntries() const {
3191 return GotEntries
.slice(LocalNum
, GlobalNum
);
3194 template <class ELFT
>
3195 typename MipsGOTParser
<ELFT
>::Entries
3196 MipsGOTParser
<ELFT
>::getOtherEntries() const {
3197 size_t OtherNum
= GotEntries
.size() - LocalNum
- GlobalNum
;
3200 return GotEntries
.slice(LocalNum
+ GlobalNum
, OtherNum
);
3203 template <class ELFT
>
3204 uint64_t MipsGOTParser
<ELFT
>::getGotAddress(const Entry
*E
) const {
3205 int64_t Offset
= std::distance(GotEntries
.data(), E
) * sizeof(Entry
);
3206 return GotSec
->sh_addr
+ Offset
;
3209 template <class ELFT
>
3210 int64_t MipsGOTParser
<ELFT
>::getGotOffset(const Entry
*E
) const {
3211 int64_t Offset
= std::distance(GotEntries
.data(), E
) * sizeof(Entry
);
3212 return Offset
- 0x7ff0;
3215 template <class ELFT
>
3216 const typename MipsGOTParser
<ELFT
>::Elf_Sym
*
3217 MipsGOTParser
<ELFT
>::getGotSym(const Entry
*E
) const {
3218 int64_t Offset
= std::distance(GotEntries
.data(), E
);
3219 return &GotDynSyms
[Offset
- LocalNum
];
3222 template <class ELFT
>
3223 const typename MipsGOTParser
<ELFT
>::Entry
*
3224 MipsGOTParser
<ELFT
>::getPltLazyResolver() const {
3225 return PltEntries
.empty() ? nullptr : &PltEntries
[0];
3228 template <class ELFT
>
3229 const typename MipsGOTParser
<ELFT
>::Entry
*
3230 MipsGOTParser
<ELFT
>::getPltModulePointer() const {
3231 return PltEntries
.size() < 2 ? nullptr : &PltEntries
[1];
3234 template <class ELFT
>
3235 typename MipsGOTParser
<ELFT
>::Entries
3236 MipsGOTParser
<ELFT
>::getPltEntries() const {
3237 if (PltEntries
.size() <= 2)
3239 return PltEntries
.slice(2, PltEntries
.size() - 2);
3242 template <class ELFT
>
3243 uint64_t MipsGOTParser
<ELFT
>::getPltAddress(const Entry
*E
) const {
3244 int64_t Offset
= std::distance(PltEntries
.data(), E
) * sizeof(Entry
);
3245 return PltSec
->sh_addr
+ Offset
;
3248 template <class ELFT
>
3249 const typename MipsGOTParser
<ELFT
>::Elf_Sym
*
3250 MipsGOTParser
<ELFT
>::getPltSym(const Entry
*E
) const {
3251 int64_t Offset
= std::distance(getPltEntries().data(), E
);
3252 if (PltRelSec
->sh_type
== ELF::SHT_REL
) {
3253 Elf_Rel_Range Rels
= unwrapOrError(FileName
, Obj
.rels(*PltRelSec
));
3254 return unwrapOrError(FileName
,
3255 Obj
.getRelocationSymbol(Rels
[Offset
], PltSymTable
));
3257 Elf_Rela_Range Rels
= unwrapOrError(FileName
, Obj
.relas(*PltRelSec
));
3258 return unwrapOrError(FileName
,
3259 Obj
.getRelocationSymbol(Rels
[Offset
], PltSymTable
));
3263 const EnumEntry
<unsigned> ElfMipsISAExtType
[] = {
3264 {"None", Mips::AFL_EXT_NONE
},
3265 {"Broadcom SB-1", Mips::AFL_EXT_SB1
},
3266 {"Cavium Networks Octeon", Mips::AFL_EXT_OCTEON
},
3267 {"Cavium Networks Octeon2", Mips::AFL_EXT_OCTEON2
},
3268 {"Cavium Networks OcteonP", Mips::AFL_EXT_OCTEONP
},
3269 {"Cavium Networks Octeon3", Mips::AFL_EXT_OCTEON3
},
3270 {"LSI R4010", Mips::AFL_EXT_4010
},
3271 {"Loongson 2E", Mips::AFL_EXT_LOONGSON_2E
},
3272 {"Loongson 2F", Mips::AFL_EXT_LOONGSON_2F
},
3273 {"Loongson 3A", Mips::AFL_EXT_LOONGSON_3A
},
3274 {"MIPS R4650", Mips::AFL_EXT_4650
},
3275 {"MIPS R5900", Mips::AFL_EXT_5900
},
3276 {"MIPS R10000", Mips::AFL_EXT_10000
},
3277 {"NEC VR4100", Mips::AFL_EXT_4100
},
3278 {"NEC VR4111/VR4181", Mips::AFL_EXT_4111
},
3279 {"NEC VR4120", Mips::AFL_EXT_4120
},
3280 {"NEC VR5400", Mips::AFL_EXT_5400
},
3281 {"NEC VR5500", Mips::AFL_EXT_5500
},
3282 {"RMI Xlr", Mips::AFL_EXT_XLR
},
3283 {"Toshiba R3900", Mips::AFL_EXT_3900
}
3286 const EnumEntry
<unsigned> ElfMipsASEFlags
[] = {
3287 {"DSP", Mips::AFL_ASE_DSP
},
3288 {"DSPR2", Mips::AFL_ASE_DSPR2
},
3289 {"Enhanced VA Scheme", Mips::AFL_ASE_EVA
},
3290 {"MCU", Mips::AFL_ASE_MCU
},
3291 {"MDMX", Mips::AFL_ASE_MDMX
},
3292 {"MIPS-3D", Mips::AFL_ASE_MIPS3D
},
3293 {"MT", Mips::AFL_ASE_MT
},
3294 {"SmartMIPS", Mips::AFL_ASE_SMARTMIPS
},
3295 {"VZ", Mips::AFL_ASE_VIRT
},
3296 {"MSA", Mips::AFL_ASE_MSA
},
3297 {"MIPS16", Mips::AFL_ASE_MIPS16
},
3298 {"microMIPS", Mips::AFL_ASE_MICROMIPS
},
3299 {"XPA", Mips::AFL_ASE_XPA
},
3300 {"CRC", Mips::AFL_ASE_CRC
},
3301 {"GINV", Mips::AFL_ASE_GINV
},
3304 const EnumEntry
<unsigned> ElfMipsFpABIType
[] = {
3305 {"Hard or soft float", Mips::Val_GNU_MIPS_ABI_FP_ANY
},
3306 {"Hard float (double precision)", Mips::Val_GNU_MIPS_ABI_FP_DOUBLE
},
3307 {"Hard float (single precision)", Mips::Val_GNU_MIPS_ABI_FP_SINGLE
},
3308 {"Soft float", Mips::Val_GNU_MIPS_ABI_FP_SOFT
},
3309 {"Hard float (MIPS32r2 64-bit FPU 12 callee-saved)",
3310 Mips::Val_GNU_MIPS_ABI_FP_OLD_64
},
3311 {"Hard float (32-bit CPU, Any FPU)", Mips::Val_GNU_MIPS_ABI_FP_XX
},
3312 {"Hard float (32-bit CPU, 64-bit FPU)", Mips::Val_GNU_MIPS_ABI_FP_64
},
3313 {"Hard float compat (32-bit CPU, 64-bit FPU)",
3314 Mips::Val_GNU_MIPS_ABI_FP_64A
}
3317 static const EnumEntry
<unsigned> ElfMipsFlags1
[] {
3318 {"ODDSPREG", Mips::AFL_FLAGS1_ODDSPREG
},
3321 static int getMipsRegisterSize(uint8_t Flag
) {
3323 case Mips::AFL_REG_NONE
:
3325 case Mips::AFL_REG_32
:
3327 case Mips::AFL_REG_64
:
3329 case Mips::AFL_REG_128
:
3336 template <class ELFT
>
3337 static void printMipsReginfoData(ScopedPrinter
&W
,
3338 const Elf_Mips_RegInfo
<ELFT
> &Reginfo
) {
3339 W
.printHex("GP", Reginfo
.ri_gp_value
);
3340 W
.printHex("General Mask", Reginfo
.ri_gprmask
);
3341 W
.printHex("Co-Proc Mask0", Reginfo
.ri_cprmask
[0]);
3342 W
.printHex("Co-Proc Mask1", Reginfo
.ri_cprmask
[1]);
3343 W
.printHex("Co-Proc Mask2", Reginfo
.ri_cprmask
[2]);
3344 W
.printHex("Co-Proc Mask3", Reginfo
.ri_cprmask
[3]);
3347 template <class ELFT
> void ELFDumper
<ELFT
>::printMipsReginfo() {
3348 const Elf_Shdr
*RegInfoSec
= findSectionByName(".reginfo");
3350 W
.startLine() << "There is no .reginfo section in the file.\n";
3354 Expected
<ArrayRef
<uint8_t>> ContentsOrErr
=
3355 Obj
.getSectionContents(*RegInfoSec
);
3356 if (!ContentsOrErr
) {
3357 this->reportUniqueWarning(
3358 "unable to read the content of the .reginfo section (" +
3359 describe(*RegInfoSec
) + "): " + toString(ContentsOrErr
.takeError()));
3363 if (ContentsOrErr
->size() < sizeof(Elf_Mips_RegInfo
<ELFT
>)) {
3364 this->reportUniqueWarning("the .reginfo section has an invalid size (0x" +
3365 Twine::utohexstr(ContentsOrErr
->size()) + ")");
3369 DictScope
GS(W
, "MIPS RegInfo");
3370 printMipsReginfoData(W
, *reinterpret_cast<const Elf_Mips_RegInfo
<ELFT
> *>(
3371 ContentsOrErr
->data()));
3374 template <class ELFT
>
3375 static Expected
<const Elf_Mips_Options
<ELFT
> *>
3376 readMipsOptions(const uint8_t *SecBegin
, ArrayRef
<uint8_t> &SecData
,
3377 bool &IsSupported
) {
3378 if (SecData
.size() < sizeof(Elf_Mips_Options
<ELFT
>))
3379 return createError("the .MIPS.options section has an invalid size (0x" +
3380 Twine::utohexstr(SecData
.size()) + ")");
3382 const Elf_Mips_Options
<ELFT
> *O
=
3383 reinterpret_cast<const Elf_Mips_Options
<ELFT
> *>(SecData
.data());
3384 const uint8_t Size
= O
->size
;
3385 if (Size
> SecData
.size()) {
3386 const uint64_t Offset
= SecData
.data() - SecBegin
;
3387 const uint64_t SecSize
= Offset
+ SecData
.size();
3388 return createError("a descriptor of size 0x" + Twine::utohexstr(Size
) +
3389 " at offset 0x" + Twine::utohexstr(Offset
) +
3390 " goes past the end of the .MIPS.options "
3391 "section of size 0x" +
3392 Twine::utohexstr(SecSize
));
3395 IsSupported
= O
->kind
== ODK_REGINFO
;
3396 const size_t ExpectedSize
=
3397 sizeof(Elf_Mips_Options
<ELFT
>) + sizeof(Elf_Mips_RegInfo
<ELFT
>);
3400 if (Size
< ExpectedSize
)
3402 "a .MIPS.options entry of kind " +
3403 Twine(getElfMipsOptionsOdkType(O
->kind
)) +
3404 " has an invalid size (0x" + Twine::utohexstr(Size
) +
3405 "), the expected size is 0x" + Twine::utohexstr(ExpectedSize
));
3407 SecData
= SecData
.drop_front(Size
);
3411 template <class ELFT
> void ELFDumper
<ELFT
>::printMipsOptions() {
3412 const Elf_Shdr
*MipsOpts
= findSectionByName(".MIPS.options");
3414 W
.startLine() << "There is no .MIPS.options section in the file.\n";
3418 DictScope
GS(W
, "MIPS Options");
3420 ArrayRef
<uint8_t> Data
=
3421 unwrapOrError(ObjF
.getFileName(), Obj
.getSectionContents(*MipsOpts
));
3422 const uint8_t *const SecBegin
= Data
.begin();
3423 while (!Data
.empty()) {
3425 Expected
<const Elf_Mips_Options
<ELFT
> *> OptsOrErr
=
3426 readMipsOptions
<ELFT
>(SecBegin
, Data
, IsSupported
);
3428 reportUniqueWarning(OptsOrErr
.takeError());
3432 unsigned Kind
= (*OptsOrErr
)->kind
;
3433 const char *Type
= getElfMipsOptionsOdkType(Kind
);
3435 W
.startLine() << "Unsupported MIPS options tag: " << Type
<< " (" << Kind
3440 DictScope
GS(W
, Type
);
3441 if (Kind
== ODK_REGINFO
)
3442 printMipsReginfoData(W
, (*OptsOrErr
)->getRegInfo());
3444 llvm_unreachable("unexpected .MIPS.options section descriptor kind");
3448 template <class ELFT
> void ELFDumper
<ELFT
>::printStackMap() const {
3449 const Elf_Shdr
*StackMapSection
= findSectionByName(".llvm_stackmaps");
3450 if (!StackMapSection
)
3453 auto Warn
= [&](Error
&&E
) {
3454 this->reportUniqueWarning("unable to read the stack map from " +
3455 describe(*StackMapSection
) + ": " +
3456 toString(std::move(E
)));
3459 Expected
<ArrayRef
<uint8_t>> ContentOrErr
=
3460 Obj
.getSectionContents(*StackMapSection
);
3461 if (!ContentOrErr
) {
3462 Warn(ContentOrErr
.takeError());
3466 if (Error E
= StackMapParser
<ELFT::TargetEndianness
>::validateHeader(
3472 prettyPrintStackMap(W
, StackMapParser
<ELFT::TargetEndianness
>(*ContentOrErr
));
3475 template <class ELFT
>
3476 void ELFDumper
<ELFT
>::printReloc(const Relocation
<ELFT
> &R
, unsigned RelIndex
,
3477 const Elf_Shdr
&Sec
, const Elf_Shdr
*SymTab
) {
3478 Expected
<RelSymbol
<ELFT
>> Target
= getRelocationTarget(R
, SymTab
);
3480 reportUniqueWarning("unable to print relocation " + Twine(RelIndex
) +
3481 " in " + describe(Sec
) + ": " +
3482 toString(Target
.takeError()));
3484 printRelRelaReloc(R
, *Target
);
3487 template <class ELFT
>
3488 std::vector
<EnumEntry
<unsigned>>
3489 ELFDumper
<ELFT
>::getOtherFlagsFromSymbol(const Elf_Ehdr
&Header
,
3490 const Elf_Sym
&Symbol
) const {
3491 std::vector
<EnumEntry
<unsigned>> SymOtherFlags(std::begin(ElfSymOtherFlags
),
3492 std::end(ElfSymOtherFlags
));
3493 if (Header
.e_machine
== EM_MIPS
) {
3494 // Someone in their infinite wisdom decided to make STO_MIPS_MIPS16
3495 // flag overlap with other ST_MIPS_xxx flags. So consider both
3496 // cases separately.
3497 if ((Symbol
.st_other
& STO_MIPS_MIPS16
) == STO_MIPS_MIPS16
)
3498 SymOtherFlags
.insert(SymOtherFlags
.end(),
3499 std::begin(ElfMips16SymOtherFlags
),
3500 std::end(ElfMips16SymOtherFlags
));
3502 SymOtherFlags
.insert(SymOtherFlags
.end(),
3503 std::begin(ElfMipsSymOtherFlags
),
3504 std::end(ElfMipsSymOtherFlags
));
3505 } else if (Header
.e_machine
== EM_AARCH64
) {
3506 SymOtherFlags
.insert(SymOtherFlags
.end(),
3507 std::begin(ElfAArch64SymOtherFlags
),
3508 std::end(ElfAArch64SymOtherFlags
));
3509 } else if (Header
.e_machine
== EM_RISCV
) {
3510 SymOtherFlags
.insert(SymOtherFlags
.end(), std::begin(ElfRISCVSymOtherFlags
),
3511 std::end(ElfRISCVSymOtherFlags
));
3513 return SymOtherFlags
;
3516 static inline void printFields(formatted_raw_ostream
&OS
, StringRef Str1
,
3520 OS
.PadToColumn(37u);
3525 template <class ELFT
>
3526 static std::string
getSectionHeadersNumString(const ELFFile
<ELFT
> &Obj
,
3527 StringRef FileName
) {
3528 const typename
ELFT::Ehdr
&ElfHeader
= Obj
.getHeader();
3529 if (ElfHeader
.e_shnum
!= 0)
3530 return to_string(ElfHeader
.e_shnum
);
3532 Expected
<ArrayRef
<typename
ELFT::Shdr
>> ArrOrErr
= Obj
.sections();
3534 // In this case we can ignore an error, because we have already reported a
3535 // warning about the broken section header table earlier.
3536 consumeError(ArrOrErr
.takeError());
3540 if (ArrOrErr
->empty())
3542 return "0 (" + to_string((*ArrOrErr
)[0].sh_size
) + ")";
3545 template <class ELFT
>
3546 static std::string
getSectionHeaderTableIndexString(const ELFFile
<ELFT
> &Obj
,
3547 StringRef FileName
) {
3548 const typename
ELFT::Ehdr
&ElfHeader
= Obj
.getHeader();
3549 if (ElfHeader
.e_shstrndx
!= SHN_XINDEX
)
3550 return to_string(ElfHeader
.e_shstrndx
);
3552 Expected
<ArrayRef
<typename
ELFT::Shdr
>> ArrOrErr
= Obj
.sections();
3554 // In this case we can ignore an error, because we have already reported a
3555 // warning about the broken section header table earlier.
3556 consumeError(ArrOrErr
.takeError());
3560 if (ArrOrErr
->empty())
3561 return "65535 (corrupt: out of range)";
3562 return to_string(ElfHeader
.e_shstrndx
) + " (" +
3563 to_string((*ArrOrErr
)[0].sh_link
) + ")";
3566 static const EnumEntry
<unsigned> *getObjectFileEnumEntry(unsigned Type
) {
3567 auto It
= llvm::find_if(ElfObjectFileType
, [&](const EnumEntry
<unsigned> &E
) {
3568 return E
.Value
== Type
;
3570 if (It
!= ArrayRef(ElfObjectFileType
).end())
3575 template <class ELFT
>
3576 void GNUELFDumper
<ELFT
>::printFileSummary(StringRef FileStr
, ObjectFile
&Obj
,
3577 ArrayRef
<std::string
> InputFilenames
,
3579 if (InputFilenames
.size() > 1 || A
) {
3580 this->W
.startLine() << "\n";
3581 this->W
.printString("File", FileStr
);
3585 template <class ELFT
> void GNUELFDumper
<ELFT
>::printFileHeaders() {
3586 const Elf_Ehdr
&e
= this->Obj
.getHeader();
3587 OS
<< "ELF Header:\n";
3590 for (int i
= 0; i
< ELF::EI_NIDENT
; i
++)
3591 OS
<< format(" %02x", static_cast<int>(e
.e_ident
[i
]));
3593 Str
= enumToString(e
.e_ident
[ELF::EI_CLASS
], ArrayRef(ElfClass
));
3594 printFields(OS
, "Class:", Str
);
3595 Str
= enumToString(e
.e_ident
[ELF::EI_DATA
], ArrayRef(ElfDataEncoding
));
3596 printFields(OS
, "Data:", Str
);
3599 OS
.PadToColumn(37u);
3600 OS
<< utohexstr(e
.e_ident
[ELF::EI_VERSION
]);
3601 if (e
.e_version
== ELF::EV_CURRENT
)
3604 auto OSABI
= ArrayRef(ElfOSABI
);
3605 if (e
.e_ident
[ELF::EI_OSABI
] >= ELF::ELFOSABI_FIRST_ARCH
&&
3606 e
.e_ident
[ELF::EI_OSABI
] <= ELF::ELFOSABI_LAST_ARCH
) {
3607 switch (e
.e_machine
) {
3608 case ELF::EM_AMDGPU
:
3609 OSABI
= ArrayRef(AMDGPUElfOSABI
);
3615 Str
= enumToString(e
.e_ident
[ELF::EI_OSABI
], OSABI
);
3616 printFields(OS
, "OS/ABI:", Str
);
3618 "ABI Version:", std::to_string(e
.e_ident
[ELF::EI_ABIVERSION
]));
3620 if (const EnumEntry
<unsigned> *E
= getObjectFileEnumEntry(e
.e_type
)) {
3621 Str
= E
->AltName
.str();
3623 if (e
.e_type
>= ET_LOPROC
)
3624 Str
= "Processor Specific: (" + utohexstr(e
.e_type
, /*LowerCase=*/true) + ")";
3625 else if (e
.e_type
>= ET_LOOS
)
3626 Str
= "OS Specific: (" + utohexstr(e
.e_type
, /*LowerCase=*/true) + ")";
3628 Str
= "<unknown>: " + utohexstr(e
.e_type
, /*LowerCase=*/true);
3630 printFields(OS
, "Type:", Str
);
3632 Str
= enumToString(e
.e_machine
, ArrayRef(ElfMachineType
));
3633 printFields(OS
, "Machine:", Str
);
3634 Str
= "0x" + utohexstr(e
.e_version
);
3635 printFields(OS
, "Version:", Str
);
3636 Str
= "0x" + utohexstr(e
.e_entry
);
3637 printFields(OS
, "Entry point address:", Str
);
3638 Str
= to_string(e
.e_phoff
) + " (bytes into file)";
3639 printFields(OS
, "Start of program headers:", Str
);
3640 Str
= to_string(e
.e_shoff
) + " (bytes into file)";
3641 printFields(OS
, "Start of section headers:", Str
);
3642 std::string ElfFlags
;
3643 if (e
.e_machine
== EM_MIPS
)
3644 ElfFlags
= printFlags(
3645 e
.e_flags
, ArrayRef(ElfHeaderMipsFlags
), unsigned(ELF::EF_MIPS_ARCH
),
3646 unsigned(ELF::EF_MIPS_ABI
), unsigned(ELF::EF_MIPS_MACH
));
3647 else if (e
.e_machine
== EM_RISCV
)
3648 ElfFlags
= printFlags(e
.e_flags
, ArrayRef(ElfHeaderRISCVFlags
));
3649 else if (e
.e_machine
== EM_AVR
)
3650 ElfFlags
= printFlags(e
.e_flags
, ArrayRef(ElfHeaderAVRFlags
),
3651 unsigned(ELF::EF_AVR_ARCH_MASK
));
3652 else if (e
.e_machine
== EM_LOONGARCH
)
3653 ElfFlags
= printFlags(e
.e_flags
, ArrayRef(ElfHeaderLoongArchFlags
),
3654 unsigned(ELF::EF_LOONGARCH_ABI_MODIFIER_MASK
),
3655 unsigned(ELF::EF_LOONGARCH_OBJABI_MASK
));
3656 else if (e
.e_machine
== EM_XTENSA
)
3657 ElfFlags
= printFlags(e
.e_flags
, ArrayRef(ElfHeaderXtensaFlags
),
3658 unsigned(ELF::EF_XTENSA_MACH
));
3659 else if (e
.e_machine
== EM_CUDA
)
3660 ElfFlags
= printFlags(e
.e_flags
, ArrayRef(ElfHeaderNVPTXFlags
),
3661 unsigned(ELF::EF_CUDA_SM
));
3662 else if (e
.e_machine
== EM_AMDGPU
) {
3663 switch (e
.e_ident
[ELF::EI_ABIVERSION
]) {
3667 // ELFOSABI_AMDGPU_PAL, ELFOSABI_AMDGPU_MESA3D support *_V3 flags.
3669 case ELF::ELFABIVERSION_AMDGPU_HSA_V3
:
3671 printFlags(e
.e_flags
, ArrayRef(ElfHeaderAMDGPUFlagsABIVersion3
),
3672 unsigned(ELF::EF_AMDGPU_MACH
));
3674 case ELF::ELFABIVERSION_AMDGPU_HSA_V4
:
3675 case ELF::ELFABIVERSION_AMDGPU_HSA_V5
:
3677 printFlags(e
.e_flags
, ArrayRef(ElfHeaderAMDGPUFlagsABIVersion4
),
3678 unsigned(ELF::EF_AMDGPU_MACH
),
3679 unsigned(ELF::EF_AMDGPU_FEATURE_XNACK_V4
),
3680 unsigned(ELF::EF_AMDGPU_FEATURE_SRAMECC_V4
));
3684 Str
= "0x" + utohexstr(e
.e_flags
);
3685 if (!ElfFlags
.empty())
3686 Str
= Str
+ ", " + ElfFlags
;
3687 printFields(OS
, "Flags:", Str
);
3688 Str
= to_string(e
.e_ehsize
) + " (bytes)";
3689 printFields(OS
, "Size of this header:", Str
);
3690 Str
= to_string(e
.e_phentsize
) + " (bytes)";
3691 printFields(OS
, "Size of program headers:", Str
);
3692 Str
= to_string(e
.e_phnum
);
3693 printFields(OS
, "Number of program headers:", Str
);
3694 Str
= to_string(e
.e_shentsize
) + " (bytes)";
3695 printFields(OS
, "Size of section headers:", Str
);
3696 Str
= getSectionHeadersNumString(this->Obj
, this->FileName
);
3697 printFields(OS
, "Number of section headers:", Str
);
3698 Str
= getSectionHeaderTableIndexString(this->Obj
, this->FileName
);
3699 printFields(OS
, "Section header string table index:", Str
);
3702 template <class ELFT
> std::vector
<GroupSection
> ELFDumper
<ELFT
>::getGroups() {
3703 auto GetSignature
= [&](const Elf_Sym
&Sym
, unsigned SymNdx
,
3704 const Elf_Shdr
&Symtab
) -> StringRef
{
3705 Expected
<StringRef
> StrTableOrErr
= Obj
.getStringTableForSymtab(Symtab
);
3706 if (!StrTableOrErr
) {
3707 reportUniqueWarning("unable to get the string table for " +
3708 describe(Symtab
) + ": " +
3709 toString(StrTableOrErr
.takeError()));
3713 StringRef Strings
= *StrTableOrErr
;
3714 if (Sym
.st_name
>= Strings
.size()) {
3715 reportUniqueWarning("unable to get the name of the symbol with index " +
3716 Twine(SymNdx
) + ": st_name (0x" +
3717 Twine::utohexstr(Sym
.st_name
) +
3718 ") is past the end of the string table of size 0x" +
3719 Twine::utohexstr(Strings
.size()));
3723 return StrTableOrErr
->data() + Sym
.st_name
;
3726 std::vector
<GroupSection
> Ret
;
3728 for (const Elf_Shdr
&Sec
: cantFail(Obj
.sections())) {
3730 if (Sec
.sh_type
!= ELF::SHT_GROUP
)
3733 StringRef Signature
= "<?>";
3734 if (Expected
<const Elf_Shdr
*> SymtabOrErr
= Obj
.getSection(Sec
.sh_link
)) {
3735 if (Expected
<const Elf_Sym
*> SymOrErr
=
3736 Obj
.template getEntry
<Elf_Sym
>(**SymtabOrErr
, Sec
.sh_info
))
3737 Signature
= GetSignature(**SymOrErr
, Sec
.sh_info
, **SymtabOrErr
);
3739 reportUniqueWarning("unable to get the signature symbol for " +
3740 describe(Sec
) + ": " +
3741 toString(SymOrErr
.takeError()));
3743 reportUniqueWarning("unable to get the symbol table for " +
3744 describe(Sec
) + ": " +
3745 toString(SymtabOrErr
.takeError()));
3748 ArrayRef
<Elf_Word
> Data
;
3749 if (Expected
<ArrayRef
<Elf_Word
>> ContentsOrErr
=
3750 Obj
.template getSectionContentsAsArray
<Elf_Word
>(Sec
)) {
3751 if (ContentsOrErr
->empty())
3752 reportUniqueWarning("unable to read the section group flag from the " +
3753 describe(Sec
) + ": the section is empty");
3755 Data
= *ContentsOrErr
;
3757 reportUniqueWarning("unable to get the content of the " + describe(Sec
) +
3758 ": " + toString(ContentsOrErr
.takeError()));
3761 Ret
.push_back({getPrintableSectionName(Sec
),
3762 maybeDemangle(Signature
),
3767 Data
.empty() ? Elf_Word(0) : Data
[0],
3773 std::vector
<GroupMember
> &GM
= Ret
.back().Members
;
3774 for (uint32_t Ndx
: Data
.slice(1)) {
3775 if (Expected
<const Elf_Shdr
*> SecOrErr
= Obj
.getSection(Ndx
)) {
3776 GM
.push_back({getPrintableSectionName(**SecOrErr
), Ndx
});
3778 reportUniqueWarning("unable to get the section with index " +
3779 Twine(Ndx
) + " when dumping the " + describe(Sec
) +
3780 ": " + toString(SecOrErr
.takeError()));
3781 GM
.push_back({"<?>", Ndx
});
3788 static DenseMap
<uint64_t, const GroupSection
*>
3789 mapSectionsToGroups(ArrayRef
<GroupSection
> Groups
) {
3790 DenseMap
<uint64_t, const GroupSection
*> Ret
;
3791 for (const GroupSection
&G
: Groups
)
3792 for (const GroupMember
&GM
: G
.Members
)
3793 Ret
.insert({GM
.Index
, &G
});
3797 template <class ELFT
> void GNUELFDumper
<ELFT
>::printGroupSections() {
3798 std::vector
<GroupSection
> V
= this->getGroups();
3799 DenseMap
<uint64_t, const GroupSection
*> Map
= mapSectionsToGroups(V
);
3800 for (const GroupSection
&G
: V
) {
3802 << getGroupType(G
.Type
) << " group section ["
3803 << format_decimal(G
.Index
, 5) << "] `" << G
.Name
<< "' [" << G
.Signature
3804 << "] contains " << G
.Members
.size() << " sections:\n"
3805 << " [Index] Name\n";
3806 for (const GroupMember
&GM
: G
.Members
) {
3807 const GroupSection
*MainGroup
= Map
[GM
.Index
];
3808 if (MainGroup
!= &G
)
3809 this->reportUniqueWarning(
3810 "section with index " + Twine(GM
.Index
) +
3811 ", included in the group section with index " +
3812 Twine(MainGroup
->Index
) +
3813 ", was also found in the group section with index " +
3815 OS
<< " [" << format_decimal(GM
.Index
, 5) << "] " << GM
.Name
<< "\n";
3820 OS
<< "There are no section groups in this file.\n";
3823 template <class ELFT
>
3824 void GNUELFDumper
<ELFT
>::printRelrReloc(const Elf_Relr
&R
) {
3825 OS
<< to_string(format_hex_no_prefix(R
, ELFT::Is64Bits
? 16 : 8)) << "\n";
3828 template <class ELFT
>
3829 void GNUELFDumper
<ELFT
>::printRelRelaReloc(const Relocation
<ELFT
> &R
,
3830 const RelSymbol
<ELFT
> &RelSym
) {
3831 // First two fields are bit width dependent. The rest of them are fixed width.
3832 unsigned Bias
= ELFT::Is64Bits
? 8 : 0;
3833 Field Fields
[5] = {0, 10 + Bias
, 19 + 2 * Bias
, 42 + 2 * Bias
, 53 + 2 * Bias
};
3834 unsigned Width
= ELFT::Is64Bits
? 16 : 8;
3836 Fields
[0].Str
= to_string(format_hex_no_prefix(R
.Offset
, Width
));
3837 Fields
[1].Str
= to_string(format_hex_no_prefix(R
.Info
, Width
));
3839 SmallString
<32> RelocName
;
3840 this->Obj
.getRelocationTypeName(R
.Type
, RelocName
);
3841 Fields
[2].Str
= RelocName
.c_str();
3845 to_string(format_hex_no_prefix(RelSym
.Sym
->getValue(), Width
));
3846 if (RelSym
.Sym
&& RelSym
.Name
.empty())
3847 Fields
[4].Str
= "<null>";
3849 Fields
[4].Str
= std::string(RelSym
.Name
);
3851 for (const Field
&F
: Fields
)
3855 if (std::optional
<int64_t> A
= R
.Addend
) {
3856 int64_t RelAddend
= *A
;
3857 if (!Fields
[4].Str
.empty()) {
3858 if (RelAddend
< 0) {
3860 RelAddend
= -static_cast<uint64_t>(RelAddend
);
3865 Addend
+= utohexstr(RelAddend
, /*LowerCase=*/true);
3867 OS
<< Addend
<< "\n";
3870 template <class ELFT
>
3871 static void printRelocHeaderFields(formatted_raw_ostream
&OS
, unsigned SType
,
3872 const typename
ELFT::Ehdr
&EHeader
) {
3873 bool IsRela
= SType
== ELF::SHT_RELA
|| SType
== ELF::SHT_ANDROID_RELA
;
3875 SType
== ELF::SHT_RELR
|| SType
== ELF::SHT_ANDROID_RELR
||
3876 (EHeader
.e_machine
== EM_AARCH64
&& SType
== ELF::SHT_AARCH64_AUTH_RELR
);
3881 if (IsRelr
&& opts::RawRelr
)
3887 << " Symbol's Value Symbol's Name";
3889 OS
<< " Info Type Sym. Value Symbol's Name";
3895 template <class ELFT
>
3896 void GNUELFDumper
<ELFT
>::printDynamicRelocHeader(unsigned Type
, StringRef Name
,
3897 const DynRegionInfo
&Reg
) {
3898 uint64_t Offset
= Reg
.Addr
- this->Obj
.base();
3899 OS
<< "\n'" << Name
.str().c_str() << "' relocation section at offset 0x"
3900 << utohexstr(Offset
, /*LowerCase=*/true) << " contains " << Reg
.Size
<< " bytes:\n";
3901 printRelocHeaderFields
<ELFT
>(OS
, Type
, this->Obj
.getHeader());
3904 template <class ELFT
>
3905 static bool isRelocationSec(const typename
ELFT::Shdr
&Sec
,
3906 const typename
ELFT::Ehdr
&EHeader
) {
3907 return Sec
.sh_type
== ELF::SHT_REL
|| Sec
.sh_type
== ELF::SHT_RELA
||
3908 Sec
.sh_type
== ELF::SHT_RELR
|| Sec
.sh_type
== ELF::SHT_ANDROID_REL
||
3909 Sec
.sh_type
== ELF::SHT_ANDROID_RELA
||
3910 Sec
.sh_type
== ELF::SHT_ANDROID_RELR
||
3911 (EHeader
.e_machine
== EM_AARCH64
&&
3912 Sec
.sh_type
== ELF::SHT_AARCH64_AUTH_RELR
);
3915 template <class ELFT
> void GNUELFDumper
<ELFT
>::printRelocations() {
3916 auto GetEntriesNum
= [&](const Elf_Shdr
&Sec
) -> Expected
<size_t> {
3917 // Android's packed relocation section needs to be unpacked first
3918 // to get the actual number of entries.
3919 if (Sec
.sh_type
== ELF::SHT_ANDROID_REL
||
3920 Sec
.sh_type
== ELF::SHT_ANDROID_RELA
) {
3921 Expected
<std::vector
<typename
ELFT::Rela
>> RelasOrErr
=
3922 this->Obj
.android_relas(Sec
);
3924 return RelasOrErr
.takeError();
3925 return RelasOrErr
->size();
3928 if (!opts::RawRelr
&&
3929 (Sec
.sh_type
== ELF::SHT_RELR
|| Sec
.sh_type
== ELF::SHT_ANDROID_RELR
||
3930 (this->Obj
.getHeader().e_machine
== EM_AARCH64
&&
3931 Sec
.sh_type
== ELF::SHT_AARCH64_AUTH_RELR
))) {
3932 Expected
<Elf_Relr_Range
> RelrsOrErr
= this->Obj
.relrs(Sec
);
3934 return RelrsOrErr
.takeError();
3935 return this->Obj
.decode_relrs(*RelrsOrErr
).size();
3938 return Sec
.getEntityCount();
3941 bool HasRelocSections
= false;
3942 for (const Elf_Shdr
&Sec
: cantFail(this->Obj
.sections())) {
3943 if (!isRelocationSec
<ELFT
>(Sec
, this->Obj
.getHeader()))
3945 HasRelocSections
= true;
3947 std::string EntriesNum
= "<?>";
3948 if (Expected
<size_t> NumOrErr
= GetEntriesNum(Sec
))
3949 EntriesNum
= std::to_string(*NumOrErr
);
3951 this->reportUniqueWarning("unable to get the number of relocations in " +
3952 this->describe(Sec
) + ": " +
3953 toString(NumOrErr
.takeError()));
3955 uintX_t Offset
= Sec
.sh_offset
;
3956 StringRef Name
= this->getPrintableSectionName(Sec
);
3957 OS
<< "\nRelocation section '" << Name
<< "' at offset 0x"
3958 << utohexstr(Offset
, /*LowerCase=*/true) << " contains " << EntriesNum
3960 printRelocHeaderFields
<ELFT
>(OS
, Sec
.sh_type
, this->Obj
.getHeader());
3961 this->printRelocationsHelper(Sec
);
3963 if (!HasRelocSections
)
3964 OS
<< "\nThere are no relocations in this file.\n";
3967 // Print the offset of a particular section from anyone of the ranges:
3968 // [SHT_LOOS, SHT_HIOS], [SHT_LOPROC, SHT_HIPROC], [SHT_LOUSER, SHT_HIUSER].
3969 // If 'Type' does not fall within any of those ranges, then a string is
3970 // returned as '<unknown>' followed by the type value.
3971 static std::string
getSectionTypeOffsetString(unsigned Type
) {
3972 if (Type
>= SHT_LOOS
&& Type
<= SHT_HIOS
)
3973 return "LOOS+0x" + utohexstr(Type
- SHT_LOOS
);
3974 else if (Type
>= SHT_LOPROC
&& Type
<= SHT_HIPROC
)
3975 return "LOPROC+0x" + utohexstr(Type
- SHT_LOPROC
);
3976 else if (Type
>= SHT_LOUSER
&& Type
<= SHT_HIUSER
)
3977 return "LOUSER+0x" + utohexstr(Type
- SHT_LOUSER
);
3978 return "0x" + utohexstr(Type
) + ": <unknown>";
3981 static std::string
getSectionTypeString(unsigned Machine
, unsigned Type
) {
3982 StringRef Name
= getELFSectionTypeName(Machine
, Type
);
3984 // Handle SHT_GNU_* type names.
3985 if (Name
.consume_front("SHT_GNU_")) {
3988 // E.g. SHT_GNU_verneed -> VERNEED.
3989 return Name
.upper();
3992 if (Name
== "SHT_SYMTAB_SHNDX")
3993 return "SYMTAB SECTION INDICES";
3995 if (Name
.consume_front("SHT_"))
3997 return getSectionTypeOffsetString(Type
);
4000 static void printSectionDescription(formatted_raw_ostream
&OS
,
4001 unsigned EMachine
) {
4002 OS
<< "Key to Flags:\n";
4003 OS
<< " W (write), A (alloc), X (execute), M (merge), S (strings), I "
4005 OS
<< " L (link order), O (extra OS processing required), G (group), T "
4007 OS
<< " C (compressed), x (unknown), o (OS specific), E (exclude),\n";
4008 OS
<< " R (retain)";
4010 if (EMachine
== EM_X86_64
)
4011 OS
<< ", l (large)";
4012 else if (EMachine
== EM_ARM
)
4013 OS
<< ", y (purecode)";
4015 OS
<< ", p (processor specific)\n";
4018 template <class ELFT
> void GNUELFDumper
<ELFT
>::printSectionHeaders() {
4019 ArrayRef
<Elf_Shdr
> Sections
= cantFail(this->Obj
.sections());
4020 if (Sections
.empty()) {
4021 OS
<< "\nThere are no sections in this file.\n";
4022 Expected
<StringRef
> SecStrTableOrErr
=
4023 this->Obj
.getSectionStringTable(Sections
, this->WarningHandler
);
4024 if (!SecStrTableOrErr
)
4025 this->reportUniqueWarning(SecStrTableOrErr
.takeError());
4028 unsigned Bias
= ELFT::Is64Bits
? 0 : 8;
4029 OS
<< "There are " << to_string(Sections
.size())
4030 << " section headers, starting at offset "
4031 << "0x" << utohexstr(this->Obj
.getHeader().e_shoff
, /*LowerCase=*/true) << ":\n\n";
4032 OS
<< "Section Headers:\n";
4033 Field Fields
[11] = {
4034 {"[Nr]", 2}, {"Name", 7}, {"Type", 25},
4035 {"Address", 41}, {"Off", 58 - Bias
}, {"Size", 65 - Bias
},
4036 {"ES", 72 - Bias
}, {"Flg", 75 - Bias
}, {"Lk", 79 - Bias
},
4037 {"Inf", 82 - Bias
}, {"Al", 86 - Bias
}};
4038 for (const Field
&F
: Fields
)
4042 StringRef SecStrTable
;
4043 if (Expected
<StringRef
> SecStrTableOrErr
=
4044 this->Obj
.getSectionStringTable(Sections
, this->WarningHandler
))
4045 SecStrTable
= *SecStrTableOrErr
;
4047 this->reportUniqueWarning(SecStrTableOrErr
.takeError());
4049 size_t SectionIndex
= 0;
4050 for (const Elf_Shdr
&Sec
: Sections
) {
4051 Fields
[0].Str
= to_string(SectionIndex
);
4052 if (SecStrTable
.empty())
4053 Fields
[1].Str
= "<no-strings>";
4055 Fields
[1].Str
= std::string(unwrapOrError
<StringRef
>(
4056 this->FileName
, this->Obj
.getSectionName(Sec
, SecStrTable
)));
4058 getSectionTypeString(this->Obj
.getHeader().e_machine
, Sec
.sh_type
);
4060 to_string(format_hex_no_prefix(Sec
.sh_addr
, ELFT::Is64Bits
? 16 : 8));
4061 Fields
[4].Str
= to_string(format_hex_no_prefix(Sec
.sh_offset
, 6));
4062 Fields
[5].Str
= to_string(format_hex_no_prefix(Sec
.sh_size
, 6));
4063 Fields
[6].Str
= to_string(format_hex_no_prefix(Sec
.sh_entsize
, 2));
4064 Fields
[7].Str
= getGNUFlags(this->Obj
.getHeader().e_ident
[ELF::EI_OSABI
],
4065 this->Obj
.getHeader().e_machine
, Sec
.sh_flags
);
4066 Fields
[8].Str
= to_string(Sec
.sh_link
);
4067 Fields
[9].Str
= to_string(Sec
.sh_info
);
4068 Fields
[10].Str
= to_string(Sec
.sh_addralign
);
4070 OS
.PadToColumn(Fields
[0].Column
);
4071 OS
<< "[" << right_justify(Fields
[0].Str
, 2) << "]";
4072 for (int i
= 1; i
< 7; i
++)
4073 printField(Fields
[i
]);
4074 OS
.PadToColumn(Fields
[7].Column
);
4075 OS
<< right_justify(Fields
[7].Str
, 3);
4076 OS
.PadToColumn(Fields
[8].Column
);
4077 OS
<< right_justify(Fields
[8].Str
, 2);
4078 OS
.PadToColumn(Fields
[9].Column
);
4079 OS
<< right_justify(Fields
[9].Str
, 3);
4080 OS
.PadToColumn(Fields
[10].Column
);
4081 OS
<< right_justify(Fields
[10].Str
, 2);
4085 printSectionDescription(OS
, this->Obj
.getHeader().e_machine
);
4088 template <class ELFT
>
4089 void GNUELFDumper
<ELFT
>::printSymtabMessage(const Elf_Shdr
*Symtab
,
4091 bool NonVisibilityBitsUsed
,
4092 bool ExtraSymInfo
) const {
4095 Name
= this->getPrintableSectionName(*Symtab
);
4097 OS
<< "\nSymbol table '" << Name
<< "'";
4099 OS
<< "\nSymbol table for image";
4100 OS
<< " contains " << Entries
<< " entries:\n";
4102 if (ELFT::Is64Bits
) {
4103 OS
<< " Num: Value Size Type Bind Vis";
4107 OS
<< " Num: Value Size Type Bind Vis";
4112 OS
.PadToColumn((ELFT::Is64Bits
? 56 : 48) + (NonVisibilityBitsUsed
? 13 : 0));
4114 OS
<< "Ndx(SecName) Name [+ Version Info]\n";
4119 template <class ELFT
>
4120 std::string GNUELFDumper
<ELFT
>::getSymbolSectionNdx(
4121 const Elf_Sym
&Symbol
, unsigned SymIndex
, DataRegion
<Elf_Word
> ShndxTable
,
4122 bool ExtraSymInfo
) const {
4123 unsigned SectionIndex
= Symbol
.st_shndx
;
4124 switch (SectionIndex
) {
4125 case ELF::SHN_UNDEF
:
4129 case ELF::SHN_COMMON
:
4131 case ELF::SHN_XINDEX
: {
4132 Expected
<uint32_t> IndexOrErr
=
4133 object::getExtendedSymbolTableIndex
<ELFT
>(Symbol
, SymIndex
, ShndxTable
);
4135 assert(Symbol
.st_shndx
== SHN_XINDEX
&&
4136 "getExtendedSymbolTableIndex should only fail due to an invalid "
4137 "SHT_SYMTAB_SHNDX table/reference");
4138 this->reportUniqueWarning(IndexOrErr
.takeError());
4139 return "RSV[0xffff]";
4141 SectionIndex
= *IndexOrErr
;
4146 // Processor specific
4147 if (SectionIndex
>= ELF::SHN_LOPROC
&& SectionIndex
<= ELF::SHN_HIPROC
)
4148 return std::string("PRC[0x") +
4149 to_string(format_hex_no_prefix(SectionIndex
, 4)) + "]";
4151 if (SectionIndex
>= ELF::SHN_LOOS
&& SectionIndex
<= ELF::SHN_HIOS
)
4152 return std::string("OS[0x") +
4153 to_string(format_hex_no_prefix(SectionIndex
, 4)) + "]";
4154 // Architecture reserved:
4155 if (SectionIndex
>= ELF::SHN_LORESERVE
&&
4156 SectionIndex
<= ELF::SHN_HIRESERVE
)
4157 return std::string("RSV[0x") +
4158 to_string(format_hex_no_prefix(SectionIndex
, 4)) + "]";
4164 auto Sec
= this->Obj
.getSection(SectionIndex
);
4166 this->reportUniqueWarning(Sec
.takeError());
4168 auto SecName
= this->Obj
.getSectionName(**Sec
);
4170 this->reportUniqueWarning(SecName
.takeError());
4172 Extra
= Twine(" (" + *SecName
+ ")").str();
4175 return to_string(format_decimal(SectionIndex
, 3)) + Extra
;
4178 template <class ELFT
>
4179 void GNUELFDumper
<ELFT
>::printSymbol(const Elf_Sym
&Symbol
, unsigned SymIndex
,
4180 DataRegion
<Elf_Word
> ShndxTable
,
4181 std::optional
<StringRef
> StrTable
,
4182 bool IsDynamic
, bool NonVisibilityBitsUsed
,
4183 bool ExtraSymInfo
) const {
4184 unsigned Bias
= ELFT::Is64Bits
? 8 : 0;
4185 Field Fields
[8] = {0, 8, 17 + Bias
, 23 + Bias
,
4186 31 + Bias
, 38 + Bias
, 48 + Bias
, 51 + Bias
};
4187 Fields
[0].Str
= to_string(format_decimal(SymIndex
, 6)) + ":";
4189 to_string(format_hex_no_prefix(Symbol
.st_value
, ELFT::Is64Bits
? 16 : 8));
4190 Fields
[2].Str
= to_string(format_decimal(Symbol
.st_size
, 5));
4192 unsigned char SymbolType
= Symbol
.getType();
4193 if (this->Obj
.getHeader().e_machine
== ELF::EM_AMDGPU
&&
4194 SymbolType
>= ELF::STT_LOOS
&& SymbolType
< ELF::STT_HIOS
)
4195 Fields
[3].Str
= enumToString(SymbolType
, ArrayRef(AMDGPUSymbolTypes
));
4197 Fields
[3].Str
= enumToString(SymbolType
, ArrayRef(ElfSymbolTypes
));
4200 enumToString(Symbol
.getBinding(), ArrayRef(ElfSymbolBindings
));
4202 enumToString(Symbol
.getVisibility(), ArrayRef(ElfSymbolVisibilities
));
4204 if (Symbol
.st_other
& ~0x3) {
4205 if (this->Obj
.getHeader().e_machine
== ELF::EM_AARCH64
) {
4206 uint8_t Other
= Symbol
.st_other
& ~0x3;
4207 if (Other
& STO_AARCH64_VARIANT_PCS
) {
4208 Other
&= ~STO_AARCH64_VARIANT_PCS
;
4209 Fields
[5].Str
+= " [VARIANT_PCS";
4211 Fields
[5].Str
.append(" | " + utohexstr(Other
, /*LowerCase=*/true));
4212 Fields
[5].Str
.append("]");
4214 } else if (this->Obj
.getHeader().e_machine
== ELF::EM_RISCV
) {
4215 uint8_t Other
= Symbol
.st_other
& ~0x3;
4216 if (Other
& STO_RISCV_VARIANT_CC
) {
4217 Other
&= ~STO_RISCV_VARIANT_CC
;
4218 Fields
[5].Str
+= " [VARIANT_CC";
4220 Fields
[5].Str
.append(" | " + utohexstr(Other
, /*LowerCase=*/true));
4221 Fields
[5].Str
.append("]");
4225 " [<other: " + to_string(format_hex(Symbol
.st_other
, 2)) + ">]";
4229 Fields
[6].Column
+= NonVisibilityBitsUsed
? 13 : 0;
4231 getSymbolSectionNdx(Symbol
, SymIndex
, ShndxTable
, ExtraSymInfo
);
4233 Fields
[7].Column
+= ExtraSymInfo
? 10 : 0;
4234 Fields
[7].Str
= this->getFullSymbolName(Symbol
, SymIndex
, ShndxTable
,
4235 StrTable
, IsDynamic
);
4236 for (const Field
&Entry
: Fields
)
4241 template <class ELFT
>
4242 void GNUELFDumper
<ELFT
>::printHashedSymbol(const Elf_Sym
*Symbol
,
4244 DataRegion
<Elf_Word
> ShndxTable
,
4247 unsigned Bias
= ELFT::Is64Bits
? 8 : 0;
4248 Field Fields
[9] = {0, 6, 11, 20 + Bias
, 25 + Bias
,
4249 34 + Bias
, 41 + Bias
, 49 + Bias
, 53 + Bias
};
4250 Fields
[0].Str
= to_string(format_decimal(SymIndex
, 5));
4251 Fields
[1].Str
= to_string(format_decimal(Bucket
, 3)) + ":";
4253 Fields
[2].Str
= to_string(
4254 format_hex_no_prefix(Symbol
->st_value
, ELFT::Is64Bits
? 16 : 8));
4255 Fields
[3].Str
= to_string(format_decimal(Symbol
->st_size
, 5));
4257 unsigned char SymbolType
= Symbol
->getType();
4258 if (this->Obj
.getHeader().e_machine
== ELF::EM_AMDGPU
&&
4259 SymbolType
>= ELF::STT_LOOS
&& SymbolType
< ELF::STT_HIOS
)
4260 Fields
[4].Str
= enumToString(SymbolType
, ArrayRef(AMDGPUSymbolTypes
));
4262 Fields
[4].Str
= enumToString(SymbolType
, ArrayRef(ElfSymbolTypes
));
4265 enumToString(Symbol
->getBinding(), ArrayRef(ElfSymbolBindings
));
4267 enumToString(Symbol
->getVisibility(), ArrayRef(ElfSymbolVisibilities
));
4268 Fields
[7].Str
= getSymbolSectionNdx(*Symbol
, SymIndex
, ShndxTable
);
4270 this->getFullSymbolName(*Symbol
, SymIndex
, ShndxTable
, StrTable
, true);
4272 for (const Field
&Entry
: Fields
)
4277 template <class ELFT
>
4278 void GNUELFDumper
<ELFT
>::printSymbols(bool PrintSymbols
,
4279 bool PrintDynamicSymbols
,
4280 bool ExtraSymInfo
) {
4281 if (!PrintSymbols
&& !PrintDynamicSymbols
)
4283 // GNU readelf prints both the .dynsym and .symtab with --symbols.
4284 this->printSymbolsHelper(true, ExtraSymInfo
);
4286 this->printSymbolsHelper(false, ExtraSymInfo
);
4289 template <class ELFT
>
4290 void GNUELFDumper
<ELFT
>::printHashTableSymbols(const Elf_Hash
&SysVHash
) {
4291 if (this->DynamicStringTable
.empty())
4295 OS
<< " Num Buc: Value Size Type Bind Vis Ndx Name";
4297 OS
<< " Num Buc: Value Size Type Bind Vis Ndx Name";
4300 Elf_Sym_Range DynSyms
= this->dynamic_symbols();
4301 const Elf_Sym
*FirstSym
= DynSyms
.empty() ? nullptr : &DynSyms
[0];
4303 this->reportUniqueWarning(
4304 Twine("unable to print symbols for the .hash table: the "
4305 "dynamic symbol table ") +
4306 (this->DynSymRegion
? "is empty" : "was not found"));
4310 DataRegion
<Elf_Word
> ShndxTable(
4311 (const Elf_Word
*)this->DynSymTabShndxRegion
.Addr
, this->Obj
.end());
4312 auto Buckets
= SysVHash
.buckets();
4313 auto Chains
= SysVHash
.chains();
4314 for (uint32_t Buc
= 0; Buc
< SysVHash
.nbucket
; Buc
++) {
4315 if (Buckets
[Buc
] == ELF::STN_UNDEF
)
4317 BitVector
Visited(SysVHash
.nchain
);
4318 for (uint32_t Ch
= Buckets
[Buc
]; Ch
< SysVHash
.nchain
; Ch
= Chains
[Ch
]) {
4319 if (Ch
== ELF::STN_UNDEF
)
4323 this->reportUniqueWarning(".hash section is invalid: bucket " +
4325 ": a cycle was detected in the linked chain");
4329 printHashedSymbol(FirstSym
+ Ch
, Ch
, ShndxTable
, this->DynamicStringTable
,
4336 template <class ELFT
>
4337 void GNUELFDumper
<ELFT
>::printGnuHashTableSymbols(const Elf_GnuHash
&GnuHash
) {
4338 if (this->DynamicStringTable
.empty())
4341 Elf_Sym_Range DynSyms
= this->dynamic_symbols();
4342 const Elf_Sym
*FirstSym
= DynSyms
.empty() ? nullptr : &DynSyms
[0];
4344 this->reportUniqueWarning(
4345 Twine("unable to print symbols for the .gnu.hash table: the "
4346 "dynamic symbol table ") +
4347 (this->DynSymRegion
? "is empty" : "was not found"));
4351 auto GetSymbol
= [&](uint64_t SymIndex
,
4352 uint64_t SymsTotal
) -> const Elf_Sym
* {
4353 if (SymIndex
>= SymsTotal
) {
4354 this->reportUniqueWarning(
4355 "unable to print hashed symbol with index " + Twine(SymIndex
) +
4356 ", which is greater than or equal to the number of dynamic symbols "
4358 Twine::utohexstr(SymsTotal
) + ")");
4361 return FirstSym
+ SymIndex
;
4364 Expected
<ArrayRef
<Elf_Word
>> ValuesOrErr
=
4365 getGnuHashTableChains
<ELFT
>(this->DynSymRegion
, &GnuHash
);
4366 ArrayRef
<Elf_Word
> Values
;
4368 this->reportUniqueWarning("unable to get hash values for the SHT_GNU_HASH "
4370 toString(ValuesOrErr
.takeError()));
4372 Values
= *ValuesOrErr
;
4374 DataRegion
<Elf_Word
> ShndxTable(
4375 (const Elf_Word
*)this->DynSymTabShndxRegion
.Addr
, this->Obj
.end());
4376 ArrayRef
<Elf_Word
> Buckets
= GnuHash
.buckets();
4377 for (uint32_t Buc
= 0; Buc
< GnuHash
.nbuckets
; Buc
++) {
4378 if (Buckets
[Buc
] == ELF::STN_UNDEF
)
4380 uint32_t Index
= Buckets
[Buc
];
4381 // Print whole chain.
4383 uint32_t SymIndex
= Index
++;
4384 if (const Elf_Sym
*Sym
= GetSymbol(SymIndex
, DynSyms
.size()))
4385 printHashedSymbol(Sym
, SymIndex
, ShndxTable
, this->DynamicStringTable
,
4390 if (SymIndex
< GnuHash
.symndx
) {
4391 this->reportUniqueWarning(
4392 "unable to read the hash value for symbol with index " +
4394 ", which is less than the index of the first hashed symbol (" +
4395 Twine(GnuHash
.symndx
) + ")");
4399 // Chain ends at symbol with stopper bit.
4400 if ((Values
[SymIndex
- GnuHash
.symndx
] & 1) == 1)
4406 template <class ELFT
> void GNUELFDumper
<ELFT
>::printHashSymbols() {
4407 if (this->HashTable
) {
4408 OS
<< "\n Symbol table of .hash for image:\n";
4409 if (Error E
= checkHashTable
<ELFT
>(*this, this->HashTable
))
4410 this->reportUniqueWarning(std::move(E
));
4412 printHashTableSymbols(*this->HashTable
);
4415 // Try printing the .gnu.hash table.
4416 if (this->GnuHashTable
) {
4417 OS
<< "\n Symbol table of .gnu.hash for image:\n";
4419 OS
<< " Num Buc: Value Size Type Bind Vis Ndx Name";
4421 OS
<< " Num Buc: Value Size Type Bind Vis Ndx Name";
4424 if (Error E
= checkGNUHashTable
<ELFT
>(this->Obj
, this->GnuHashTable
))
4425 this->reportUniqueWarning(std::move(E
));
4427 printGnuHashTableSymbols(*this->GnuHashTable
);
4431 template <class ELFT
> void GNUELFDumper
<ELFT
>::printSectionDetails() {
4432 ArrayRef
<Elf_Shdr
> Sections
= cantFail(this->Obj
.sections());
4433 if (Sections
.empty()) {
4434 OS
<< "\nThere are no sections in this file.\n";
4435 Expected
<StringRef
> SecStrTableOrErr
=
4436 this->Obj
.getSectionStringTable(Sections
, this->WarningHandler
);
4437 if (!SecStrTableOrErr
)
4438 this->reportUniqueWarning(SecStrTableOrErr
.takeError());
4441 OS
<< "There are " << to_string(Sections
.size())
4442 << " section headers, starting at offset "
4443 << "0x" << utohexstr(this->Obj
.getHeader().e_shoff
, /*LowerCase=*/true) << ":\n\n";
4445 OS
<< "Section Headers:\n";
4447 auto PrintFields
= [&](ArrayRef
<Field
> V
) {
4448 for (const Field
&F
: V
)
4453 PrintFields({{"[Nr]", 2}, {"Name", 7}});
4455 constexpr bool Is64
= ELFT::Is64Bits
;
4456 PrintFields({{"Type", 7},
4457 {Is64
? "Address" : "Addr", 23},
4458 {"Off", Is64
? 40 : 32},
4459 {"Size", Is64
? 47 : 39},
4460 {"ES", Is64
? 54 : 46},
4461 {"Lk", Is64
? 59 : 51},
4462 {"Inf", Is64
? 62 : 54},
4463 {"Al", Is64
? 66 : 57}});
4464 PrintFields({{"Flags", 7}});
4466 StringRef SecStrTable
;
4467 if (Expected
<StringRef
> SecStrTableOrErr
=
4468 this->Obj
.getSectionStringTable(Sections
, this->WarningHandler
))
4469 SecStrTable
= *SecStrTableOrErr
;
4471 this->reportUniqueWarning(SecStrTableOrErr
.takeError());
4473 size_t SectionIndex
= 0;
4474 const unsigned AddrSize
= Is64
? 16 : 8;
4475 for (const Elf_Shdr
&S
: Sections
) {
4476 StringRef Name
= "<?>";
4477 if (Expected
<StringRef
> NameOrErr
=
4478 this->Obj
.getSectionName(S
, SecStrTable
))
4481 this->reportUniqueWarning(NameOrErr
.takeError());
4484 OS
<< "[" << right_justify(to_string(SectionIndex
), 2) << "]";
4485 PrintFields({{Name
, 7}});
4487 {{getSectionTypeString(this->Obj
.getHeader().e_machine
, S
.sh_type
), 7},
4488 {to_string(format_hex_no_prefix(S
.sh_addr
, AddrSize
)), 23},
4489 {to_string(format_hex_no_prefix(S
.sh_offset
, 6)), Is64
? 39 : 32},
4490 {to_string(format_hex_no_prefix(S
.sh_size
, 6)), Is64
? 47 : 39},
4491 {to_string(format_hex_no_prefix(S
.sh_entsize
, 2)), Is64
? 54 : 46},
4492 {to_string(S
.sh_link
), Is64
? 59 : 51},
4493 {to_string(S
.sh_info
), Is64
? 63 : 55},
4494 {to_string(S
.sh_addralign
), Is64
? 66 : 58}});
4497 OS
<< "[" << to_string(format_hex_no_prefix(S
.sh_flags
, AddrSize
)) << "]: ";
4499 DenseMap
<unsigned, StringRef
> FlagToName
= {
4500 {SHF_WRITE
, "WRITE"}, {SHF_ALLOC
, "ALLOC"},
4501 {SHF_EXECINSTR
, "EXEC"}, {SHF_MERGE
, "MERGE"},
4502 {SHF_STRINGS
, "STRINGS"}, {SHF_INFO_LINK
, "INFO LINK"},
4503 {SHF_LINK_ORDER
, "LINK ORDER"}, {SHF_OS_NONCONFORMING
, "OS NONCONF"},
4504 {SHF_GROUP
, "GROUP"}, {SHF_TLS
, "TLS"},
4505 {SHF_COMPRESSED
, "COMPRESSED"}, {SHF_EXCLUDE
, "EXCLUDE"}};
4507 uint64_t Flags
= S
.sh_flags
;
4508 uint64_t UnknownFlags
= 0;
4511 // Take the least significant bit as a flag.
4512 uint64_t Flag
= Flags
& -Flags
;
4515 auto It
= FlagToName
.find(Flag
);
4516 if (It
!= FlagToName
.end())
4517 OS
<< LS
<< It
->second
;
4519 UnknownFlags
|= Flag
;
4522 auto PrintUnknownFlags
= [&](uint64_t Mask
, StringRef Name
) {
4523 uint64_t FlagsToPrint
= UnknownFlags
& Mask
;
4527 OS
<< LS
<< Name
<< " ("
4528 << to_string(format_hex_no_prefix(FlagsToPrint
, AddrSize
)) << ")";
4529 UnknownFlags
&= ~Mask
;
4532 PrintUnknownFlags(SHF_MASKOS
, "OS");
4533 PrintUnknownFlags(SHF_MASKPROC
, "PROC");
4534 PrintUnknownFlags(uint64_t(-1), "UNKNOWN");
4539 if (!(S
.sh_flags
& SHF_COMPRESSED
))
4541 Expected
<ArrayRef
<uint8_t>> Data
= this->Obj
.getSectionContents(S
);
4542 if (!Data
|| Data
->size() < sizeof(Elf_Chdr
)) {
4543 consumeError(Data
.takeError());
4544 reportWarning(createError("SHF_COMPRESSED section '" + Name
+
4545 "' does not have an Elf_Chdr header"),
4548 OS
<< "[<corrupt>]";
4551 auto *Chdr
= reinterpret_cast<const Elf_Chdr
*>(Data
->data());
4552 if (Chdr
->ch_type
== ELFCOMPRESS_ZLIB
)
4554 else if (Chdr
->ch_type
== ELFCOMPRESS_ZSTD
)
4557 OS
<< format("[<unknown>: 0x%x]", unsigned(Chdr
->ch_type
));
4558 OS
<< ", " << format_hex_no_prefix(Chdr
->ch_size
, ELFT::Is64Bits
? 16 : 8)
4559 << ", " << Chdr
->ch_addralign
;
4565 static inline std::string
printPhdrFlags(unsigned Flag
) {
4567 Str
= (Flag
& PF_R
) ? "R" : " ";
4568 Str
+= (Flag
& PF_W
) ? "W" : " ";
4569 Str
+= (Flag
& PF_X
) ? "E" : " ";
4573 template <class ELFT
>
4574 static bool checkTLSSections(const typename
ELFT::Phdr
&Phdr
,
4575 const typename
ELFT::Shdr
&Sec
) {
4576 if (Sec
.sh_flags
& ELF::SHF_TLS
) {
4577 // .tbss must only be shown in the PT_TLS segment.
4578 if (Sec
.sh_type
== ELF::SHT_NOBITS
)
4579 return Phdr
.p_type
== ELF::PT_TLS
;
4581 // SHF_TLS sections are only shown in PT_TLS, PT_LOAD or PT_GNU_RELRO
4583 return (Phdr
.p_type
== ELF::PT_TLS
) || (Phdr
.p_type
== ELF::PT_LOAD
) ||
4584 (Phdr
.p_type
== ELF::PT_GNU_RELRO
);
4587 // PT_TLS must only have SHF_TLS sections.
4588 return Phdr
.p_type
!= ELF::PT_TLS
;
4591 template <class ELFT
>
4592 static bool checkPTDynamic(const typename
ELFT::Phdr
&Phdr
,
4593 const typename
ELFT::Shdr
&Sec
) {
4594 if (Phdr
.p_type
!= ELF::PT_DYNAMIC
|| Phdr
.p_memsz
== 0 || Sec
.sh_size
!= 0)
4597 // We get here when we have an empty section. Only non-empty sections can be
4598 // at the start or at the end of PT_DYNAMIC.
4599 // Is section within the phdr both based on offset and VMA?
4600 bool CheckOffset
= (Sec
.sh_type
== ELF::SHT_NOBITS
) ||
4601 (Sec
.sh_offset
> Phdr
.p_offset
&&
4602 Sec
.sh_offset
< Phdr
.p_offset
+ Phdr
.p_filesz
);
4603 bool CheckVA
= !(Sec
.sh_flags
& ELF::SHF_ALLOC
) ||
4604 (Sec
.sh_addr
> Phdr
.p_vaddr
&& Sec
.sh_addr
< Phdr
.p_memsz
);
4605 return CheckOffset
&& CheckVA
;
4608 template <class ELFT
>
4609 void GNUELFDumper
<ELFT
>::printProgramHeaders(
4610 bool PrintProgramHeaders
, cl::boolOrDefault PrintSectionMapping
) {
4611 const bool ShouldPrintSectionMapping
= (PrintSectionMapping
!= cl::BOU_FALSE
);
4612 // Exit early if no program header or section mapping details were requested.
4613 if (!PrintProgramHeaders
&& !ShouldPrintSectionMapping
)
4616 if (PrintProgramHeaders
) {
4617 const Elf_Ehdr
&Header
= this->Obj
.getHeader();
4618 if (Header
.e_phnum
== 0) {
4619 OS
<< "\nThere are no program headers in this file.\n";
4621 printProgramHeaders();
4625 if (ShouldPrintSectionMapping
)
4626 printSectionMapping();
4629 template <class ELFT
> void GNUELFDumper
<ELFT
>::printProgramHeaders() {
4630 unsigned Bias
= ELFT::Is64Bits
? 8 : 0;
4631 const Elf_Ehdr
&Header
= this->Obj
.getHeader();
4632 Field Fields
[8] = {2, 17, 26, 37 + Bias
,
4633 48 + Bias
, 56 + Bias
, 64 + Bias
, 68 + Bias
};
4634 OS
<< "\nElf file type is "
4635 << enumToString(Header
.e_type
, ArrayRef(ElfObjectFileType
)) << "\n"
4636 << "Entry point " << format_hex(Header
.e_entry
, 3) << "\n"
4637 << "There are " << Header
.e_phnum
<< " program headers,"
4638 << " starting at offset " << Header
.e_phoff
<< "\n\n"
4639 << "Program Headers:\n";
4641 OS
<< " Type Offset VirtAddr PhysAddr "
4642 << " FileSiz MemSiz Flg Align\n";
4644 OS
<< " Type Offset VirtAddr PhysAddr FileSiz "
4645 << "MemSiz Flg Align\n";
4647 unsigned Width
= ELFT::Is64Bits
? 18 : 10;
4648 unsigned SizeWidth
= ELFT::Is64Bits
? 8 : 7;
4650 Expected
<ArrayRef
<Elf_Phdr
>> PhdrsOrErr
= this->Obj
.program_headers();
4652 this->reportUniqueWarning("unable to dump program headers: " +
4653 toString(PhdrsOrErr
.takeError()));
4657 for (const Elf_Phdr
&Phdr
: *PhdrsOrErr
) {
4658 Fields
[0].Str
= getGNUPtType(Header
.e_machine
, Phdr
.p_type
);
4659 Fields
[1].Str
= to_string(format_hex(Phdr
.p_offset
, 8));
4660 Fields
[2].Str
= to_string(format_hex(Phdr
.p_vaddr
, Width
));
4661 Fields
[3].Str
= to_string(format_hex(Phdr
.p_paddr
, Width
));
4662 Fields
[4].Str
= to_string(format_hex(Phdr
.p_filesz
, SizeWidth
));
4663 Fields
[5].Str
= to_string(format_hex(Phdr
.p_memsz
, SizeWidth
));
4664 Fields
[6].Str
= printPhdrFlags(Phdr
.p_flags
);
4665 Fields
[7].Str
= to_string(format_hex(Phdr
.p_align
, 1));
4666 for (const Field
&F
: Fields
)
4668 if (Phdr
.p_type
== ELF::PT_INTERP
) {
4670 auto ReportBadInterp
= [&](const Twine
&Msg
) {
4671 this->reportUniqueWarning(
4672 "unable to read program interpreter name at offset 0x" +
4673 Twine::utohexstr(Phdr
.p_offset
) + ": " + Msg
);
4676 if (Phdr
.p_offset
>= this->Obj
.getBufSize()) {
4677 ReportBadInterp("it goes past the end of the file (0x" +
4678 Twine::utohexstr(this->Obj
.getBufSize()) + ")");
4683 reinterpret_cast<const char *>(this->Obj
.base()) + Phdr
.p_offset
;
4684 size_t MaxSize
= this->Obj
.getBufSize() - Phdr
.p_offset
;
4685 size_t Len
= strnlen(Data
, MaxSize
);
4686 if (Len
== MaxSize
) {
4687 ReportBadInterp("it is not null-terminated");
4691 OS
<< " [Requesting program interpreter: ";
4692 OS
<< StringRef(Data
, Len
) << "]";
4698 template <class ELFT
> void GNUELFDumper
<ELFT
>::printSectionMapping() {
4699 OS
<< "\n Section to Segment mapping:\n Segment Sections...\n";
4700 DenseSet
<const Elf_Shdr
*> BelongsToSegment
;
4703 Expected
<ArrayRef
<Elf_Phdr
>> PhdrsOrErr
= this->Obj
.program_headers();
4705 this->reportUniqueWarning(
4706 "can't read program headers to build section to segment mapping: " +
4707 toString(PhdrsOrErr
.takeError()));
4711 for (const Elf_Phdr
&Phdr
: *PhdrsOrErr
) {
4712 std::string Sections
;
4713 OS
<< format(" %2.2d ", Phnum
++);
4714 // Check if each section is in a segment and then print mapping.
4715 for (const Elf_Shdr
&Sec
: cantFail(this->Obj
.sections())) {
4716 if (Sec
.sh_type
== ELF::SHT_NULL
)
4719 // readelf additionally makes sure it does not print zero sized sections
4720 // at end of segments and for PT_DYNAMIC both start and end of section
4721 // .tbss must only be shown in PT_TLS section.
4722 if (isSectionInSegment
<ELFT
>(Phdr
, Sec
) &&
4723 checkTLSSections
<ELFT
>(Phdr
, Sec
) &&
4724 checkPTDynamic
<ELFT
>(Phdr
, Sec
)) {
4726 unwrapOrError(this->FileName
, this->Obj
.getSectionName(Sec
)).str() +
4728 BelongsToSegment
.insert(&Sec
);
4731 OS
<< Sections
<< "\n";
4735 // Display sections that do not belong to a segment.
4736 std::string Sections
;
4737 for (const Elf_Shdr
&Sec
: cantFail(this->Obj
.sections())) {
4738 if (BelongsToSegment
.find(&Sec
) == BelongsToSegment
.end())
4740 unwrapOrError(this->FileName
, this->Obj
.getSectionName(Sec
)).str() +
4743 if (!Sections
.empty()) {
4744 OS
<< " None " << Sections
<< '\n';
4751 template <class ELFT
>
4752 RelSymbol
<ELFT
> getSymbolForReloc(const ELFDumper
<ELFT
> &Dumper
,
4753 const Relocation
<ELFT
> &Reloc
) {
4754 using Elf_Sym
= typename
ELFT::Sym
;
4755 auto WarnAndReturn
= [&](const Elf_Sym
*Sym
,
4756 const Twine
&Reason
) -> RelSymbol
<ELFT
> {
4757 Dumper
.reportUniqueWarning(
4758 "unable to get name of the dynamic symbol with index " +
4759 Twine(Reloc
.Symbol
) + ": " + Reason
);
4760 return {Sym
, "<corrupt>"};
4763 ArrayRef
<Elf_Sym
> Symbols
= Dumper
.dynamic_symbols();
4764 const Elf_Sym
*FirstSym
= Symbols
.begin();
4766 return WarnAndReturn(nullptr, "no dynamic symbol table found");
4768 // We might have an object without a section header. In this case the size of
4769 // Symbols is zero, because there is no way to know the size of the dynamic
4770 // table. We should allow this case and not print a warning.
4771 if (!Symbols
.empty() && Reloc
.Symbol
>= Symbols
.size())
4772 return WarnAndReturn(
4774 "index is greater than or equal to the number of dynamic symbols (" +
4775 Twine(Symbols
.size()) + ")");
4777 const ELFFile
<ELFT
> &Obj
= Dumper
.getElfObject().getELFFile();
4778 const uint64_t FileSize
= Obj
.getBufSize();
4779 const uint64_t SymOffset
= ((const uint8_t *)FirstSym
- Obj
.base()) +
4780 (uint64_t)Reloc
.Symbol
* sizeof(Elf_Sym
);
4781 if (SymOffset
+ sizeof(Elf_Sym
) > FileSize
)
4782 return WarnAndReturn(nullptr, "symbol at 0x" + Twine::utohexstr(SymOffset
) +
4783 " goes past the end of the file (0x" +
4784 Twine::utohexstr(FileSize
) + ")");
4786 const Elf_Sym
*Sym
= FirstSym
+ Reloc
.Symbol
;
4787 Expected
<StringRef
> ErrOrName
= Sym
->getName(Dumper
.getDynamicStringTable());
4789 return WarnAndReturn(Sym
, toString(ErrOrName
.takeError()));
4791 return {Sym
== FirstSym
? nullptr : Sym
, maybeDemangle(*ErrOrName
)};
4795 template <class ELFT
>
4796 static size_t getMaxDynamicTagSize(const ELFFile
<ELFT
> &Obj
,
4797 typename
ELFT::DynRange Tags
) {
4799 for (const typename
ELFT::Dyn
&Dyn
: Tags
)
4800 Max
= std::max(Max
, Obj
.getDynamicTagAsString(Dyn
.d_tag
).size());
4804 template <class ELFT
> void GNUELFDumper
<ELFT
>::printDynamicTable() {
4805 Elf_Dyn_Range Table
= this->dynamic_table();
4809 OS
<< "Dynamic section at offset "
4810 << format_hex(reinterpret_cast<const uint8_t *>(this->DynamicTable
.Addr
) -
4813 << " contains " << Table
.size() << " entries:\n";
4815 // The type name is surrounded with round brackets, hence add 2.
4816 size_t MaxTagSize
= getMaxDynamicTagSize(this->Obj
, Table
) + 2;
4817 // The "Name/Value" column should be indented from the "Type" column by N
4818 // spaces, where N = MaxTagSize - length of "Type" (4) + trailing
4820 OS
<< " Tag" + std::string(ELFT::Is64Bits
? 16 : 8, ' ') + "Type"
4821 << std::string(MaxTagSize
- 3, ' ') << "Name/Value\n";
4823 std::string ValueFmt
= " %-" + std::to_string(MaxTagSize
) + "s ";
4824 for (auto Entry
: Table
) {
4825 uintX_t Tag
= Entry
.getTag();
4827 std::string("(") + this->Obj
.getDynamicTagAsString(Tag
) + ")";
4828 std::string Value
= this->getDynamicEntry(Tag
, Entry
.getVal());
4829 OS
<< " " << format_hex(Tag
, ELFT::Is64Bits
? 18 : 10)
4830 << format(ValueFmt
.c_str(), Type
.c_str()) << Value
<< "\n";
4834 template <class ELFT
> void GNUELFDumper
<ELFT
>::printDynamicRelocations() {
4835 this->printDynamicRelocationsHelper();
4838 template <class ELFT
>
4839 void ELFDumper
<ELFT
>::printDynamicReloc(const Relocation
<ELFT
> &R
) {
4840 printRelRelaReloc(R
, getSymbolForReloc(*this, R
));
4843 template <class ELFT
>
4844 void ELFDumper
<ELFT
>::printRelocationsHelper(const Elf_Shdr
&Sec
) {
4845 this->forEachRelocationDo(
4847 [&](const Relocation
<ELFT
> &R
, unsigned Ndx
, const Elf_Shdr
&Sec
,
4848 const Elf_Shdr
*SymTab
) { printReloc(R
, Ndx
, Sec
, SymTab
); },
4849 [&](const Elf_Relr
&R
) { printRelrReloc(R
); });
4852 template <class ELFT
> void ELFDumper
<ELFT
>::printDynamicRelocationsHelper() {
4853 const bool IsMips64EL
= this->Obj
.isMips64EL();
4854 if (this->DynRelaRegion
.Size
> 0) {
4855 printDynamicRelocHeader(ELF::SHT_RELA
, "RELA", this->DynRelaRegion
);
4856 for (const Elf_Rela
&Rela
:
4857 this->DynRelaRegion
.template getAsArrayRef
<Elf_Rela
>())
4858 printDynamicReloc(Relocation
<ELFT
>(Rela
, IsMips64EL
));
4861 if (this->DynRelRegion
.Size
> 0) {
4862 printDynamicRelocHeader(ELF::SHT_REL
, "REL", this->DynRelRegion
);
4863 for (const Elf_Rel
&Rel
:
4864 this->DynRelRegion
.template getAsArrayRef
<Elf_Rel
>())
4865 printDynamicReloc(Relocation
<ELFT
>(Rel
, IsMips64EL
));
4868 if (this->DynRelrRegion
.Size
> 0) {
4869 printDynamicRelocHeader(ELF::SHT_REL
, "RELR", this->DynRelrRegion
);
4870 Elf_Relr_Range Relrs
=
4871 this->DynRelrRegion
.template getAsArrayRef
<Elf_Relr
>();
4872 for (const Elf_Rel
&Rel
: Obj
.decode_relrs(Relrs
))
4873 printDynamicReloc(Relocation
<ELFT
>(Rel
, IsMips64EL
));
4876 if (this->DynPLTRelRegion
.Size
) {
4877 if (this->DynPLTRelRegion
.EntSize
== sizeof(Elf_Rela
)) {
4878 printDynamicRelocHeader(ELF::SHT_RELA
, "PLT", this->DynPLTRelRegion
);
4879 for (const Elf_Rela
&Rela
:
4880 this->DynPLTRelRegion
.template getAsArrayRef
<Elf_Rela
>())
4881 printDynamicReloc(Relocation
<ELFT
>(Rela
, IsMips64EL
));
4883 printDynamicRelocHeader(ELF::SHT_REL
, "PLT", this->DynPLTRelRegion
);
4884 for (const Elf_Rel
&Rel
:
4885 this->DynPLTRelRegion
.template getAsArrayRef
<Elf_Rel
>())
4886 printDynamicReloc(Relocation
<ELFT
>(Rel
, IsMips64EL
));
4891 template <class ELFT
>
4892 void GNUELFDumper
<ELFT
>::printGNUVersionSectionProlog(
4893 const typename
ELFT::Shdr
&Sec
, const Twine
&Label
, unsigned EntriesNum
) {
4894 // Don't inline the SecName, because it might report a warning to stderr and
4895 // corrupt the output.
4896 StringRef SecName
= this->getPrintableSectionName(Sec
);
4897 OS
<< Label
<< " section '" << SecName
<< "' "
4898 << "contains " << EntriesNum
<< " entries:\n";
4900 StringRef LinkedSecName
= "<corrupt>";
4901 if (Expected
<const typename
ELFT::Shdr
*> LinkedSecOrErr
=
4902 this->Obj
.getSection(Sec
.sh_link
))
4903 LinkedSecName
= this->getPrintableSectionName(**LinkedSecOrErr
);
4905 this->reportUniqueWarning("invalid section linked to " +
4906 this->describe(Sec
) + ": " +
4907 toString(LinkedSecOrErr
.takeError()));
4909 OS
<< " Addr: " << format_hex_no_prefix(Sec
.sh_addr
, 16)
4910 << " Offset: " << format_hex(Sec
.sh_offset
, 8)
4911 << " Link: " << Sec
.sh_link
<< " (" << LinkedSecName
<< ")\n";
4914 template <class ELFT
>
4915 void GNUELFDumper
<ELFT
>::printVersionSymbolSection(const Elf_Shdr
*Sec
) {
4919 printGNUVersionSectionProlog(*Sec
, "Version symbols",
4920 Sec
->sh_size
/ sizeof(Elf_Versym
));
4921 Expected
<ArrayRef
<Elf_Versym
>> VerTableOrErr
=
4922 this->getVersionTable(*Sec
, /*SymTab=*/nullptr,
4923 /*StrTab=*/nullptr, /*SymTabSec=*/nullptr);
4924 if (!VerTableOrErr
) {
4925 this->reportUniqueWarning(VerTableOrErr
.takeError());
4929 SmallVector
<std::optional
<VersionEntry
>, 0> *VersionMap
= nullptr;
4930 if (Expected
<SmallVector
<std::optional
<VersionEntry
>, 0> *> MapOrErr
=
4931 this->getVersionMap())
4932 VersionMap
= *MapOrErr
;
4934 this->reportUniqueWarning(MapOrErr
.takeError());
4936 ArrayRef
<Elf_Versym
> VerTable
= *VerTableOrErr
;
4937 std::vector
<StringRef
> Versions
;
4938 for (size_t I
= 0, E
= VerTable
.size(); I
< E
; ++I
) {
4939 unsigned Ndx
= VerTable
[I
].vs_index
;
4940 if (Ndx
== VER_NDX_LOCAL
|| Ndx
== VER_NDX_GLOBAL
) {
4941 Versions
.emplace_back(Ndx
== VER_NDX_LOCAL
? "*local*" : "*global*");
4946 Versions
.emplace_back("<corrupt>");
4951 Expected
<StringRef
> NameOrErr
= this->Obj
.getSymbolVersionByIndex(
4952 Ndx
, IsDefault
, *VersionMap
, /*IsSymHidden=*/std::nullopt
);
4954 this->reportUniqueWarning("unable to get a version for entry " +
4955 Twine(I
) + " of " + this->describe(*Sec
) +
4956 ": " + toString(NameOrErr
.takeError()));
4957 Versions
.emplace_back("<corrupt>");
4960 Versions
.emplace_back(*NameOrErr
);
4963 // readelf prints 4 entries per line.
4964 uint64_t Entries
= VerTable
.size();
4965 for (uint64_t VersymRow
= 0; VersymRow
< Entries
; VersymRow
+= 4) {
4966 OS
<< " " << format_hex_no_prefix(VersymRow
, 3) << ":";
4967 for (uint64_t I
= 0; (I
< 4) && (I
+ VersymRow
) < Entries
; ++I
) {
4968 unsigned Ndx
= VerTable
[VersymRow
+ I
].vs_index
;
4969 OS
<< format("%4x%c", Ndx
& VERSYM_VERSION
,
4970 Ndx
& VERSYM_HIDDEN
? 'h' : ' ');
4971 OS
<< left_justify("(" + std::string(Versions
[VersymRow
+ I
]) + ")", 13);
4978 static std::string
versionFlagToString(unsigned Flags
) {
4983 auto AddFlag
= [&Ret
, &Flags
](unsigned Flag
, StringRef Name
) {
4984 if (!(Flags
& Flag
))
4992 AddFlag(VER_FLG_BASE
, "BASE");
4993 AddFlag(VER_FLG_WEAK
, "WEAK");
4994 AddFlag(VER_FLG_INFO
, "INFO");
4995 AddFlag(~0, "<unknown>");
4999 template <class ELFT
>
5000 void GNUELFDumper
<ELFT
>::printVersionDefinitionSection(const Elf_Shdr
*Sec
) {
5004 printGNUVersionSectionProlog(*Sec
, "Version definition", Sec
->sh_info
);
5006 Expected
<std::vector
<VerDef
>> V
= this->Obj
.getVersionDefinitions(*Sec
);
5008 this->reportUniqueWarning(V
.takeError());
5012 for (const VerDef
&Def
: *V
) {
5013 OS
<< format(" 0x%04x: Rev: %u Flags: %s Index: %u Cnt: %u Name: %s\n",
5014 Def
.Offset
, Def
.Version
,
5015 versionFlagToString(Def
.Flags
).c_str(), Def
.Ndx
, Def
.Cnt
,
5018 for (const VerdAux
&Aux
: Def
.AuxV
)
5019 OS
<< format(" 0x%04x: Parent %u: %s\n", Aux
.Offset
, ++I
,
5026 template <class ELFT
>
5027 void GNUELFDumper
<ELFT
>::printVersionDependencySection(const Elf_Shdr
*Sec
) {
5031 unsigned VerneedNum
= Sec
->sh_info
;
5032 printGNUVersionSectionProlog(*Sec
, "Version needs", VerneedNum
);
5034 Expected
<std::vector
<VerNeed
>> V
=
5035 this->Obj
.getVersionDependencies(*Sec
, this->WarningHandler
);
5037 this->reportUniqueWarning(V
.takeError());
5041 for (const VerNeed
&VN
: *V
) {
5042 OS
<< format(" 0x%04x: Version: %u File: %s Cnt: %u\n", VN
.Offset
,
5043 VN
.Version
, VN
.File
.data(), VN
.Cnt
);
5044 for (const VernAux
&Aux
: VN
.AuxV
)
5045 OS
<< format(" 0x%04x: Name: %s Flags: %s Version: %u\n", Aux
.Offset
,
5046 Aux
.Name
.data(), versionFlagToString(Aux
.Flags
).c_str(),
5052 template <class ELFT
>
5053 void GNUELFDumper
<ELFT
>::printHashHistogramStats(size_t NBucket
,
5056 ArrayRef
<size_t> Count
,
5058 size_t CumulativeNonZero
= 0;
5059 OS
<< "Histogram for" << (IsGnu
? " `.gnu.hash'" : "")
5060 << " bucket list length (total of " << NBucket
<< " buckets)\n"
5061 << " Length Number % of total Coverage\n";
5062 for (size_t I
= 0; I
< MaxChain
; ++I
) {
5063 CumulativeNonZero
+= Count
[I
] * I
;
5064 OS
<< format("%7lu %-10lu (%5.1f%%) %5.1f%%\n", I
, Count
[I
],
5065 (Count
[I
] * 100.0) / NBucket
,
5066 (CumulativeNonZero
* 100.0) / TotalSyms
);
5070 template <class ELFT
> void GNUELFDumper
<ELFT
>::printCGProfile() {
5071 OS
<< "GNUStyle::printCGProfile not implemented\n";
5074 template <class ELFT
> void GNUELFDumper
<ELFT
>::printBBAddrMaps() {
5075 OS
<< "GNUStyle::printBBAddrMaps not implemented\n";
5078 static Expected
<std::vector
<uint64_t>> toULEB128Array(ArrayRef
<uint8_t> Data
) {
5079 std::vector
<uint64_t> Ret
;
5080 const uint8_t *Cur
= Data
.begin();
5081 const uint8_t *End
= Data
.end();
5082 while (Cur
!= End
) {
5084 const char *Err
= nullptr;
5085 Ret
.push_back(decodeULEB128(Cur
, &Size
, End
, &Err
));
5087 return createError(Err
);
5093 template <class ELFT
>
5094 static Expected
<std::vector
<uint64_t>>
5095 decodeAddrsigSection(const ELFFile
<ELFT
> &Obj
, const typename
ELFT::Shdr
&Sec
) {
5096 Expected
<ArrayRef
<uint8_t>> ContentsOrErr
= Obj
.getSectionContents(Sec
);
5098 return ContentsOrErr
.takeError();
5100 if (Expected
<std::vector
<uint64_t>> SymsOrErr
=
5101 toULEB128Array(*ContentsOrErr
))
5104 return createError("unable to decode " + describe(Obj
, Sec
) + ": " +
5105 toString(SymsOrErr
.takeError()));
5108 template <class ELFT
> void GNUELFDumper
<ELFT
>::printAddrsig() {
5109 if (!this->DotAddrsigSec
)
5112 Expected
<std::vector
<uint64_t>> SymsOrErr
=
5113 decodeAddrsigSection(this->Obj
, *this->DotAddrsigSec
);
5115 this->reportUniqueWarning(SymsOrErr
.takeError());
5119 StringRef Name
= this->getPrintableSectionName(*this->DotAddrsigSec
);
5120 OS
<< "\nAddress-significant symbols section '" << Name
<< "'"
5121 << " contains " << SymsOrErr
->size() << " entries:\n";
5122 OS
<< " Num: Name\n";
5124 Field Fields
[2] = {0, 8};
5125 size_t SymIndex
= 0;
5126 for (uint64_t Sym
: *SymsOrErr
) {
5127 Fields
[0].Str
= to_string(format_decimal(++SymIndex
, 6)) + ":";
5128 Fields
[1].Str
= this->getStaticSymbolName(Sym
);
5129 for (const Field
&Entry
: Fields
)
5135 template <typename ELFT
>
5136 static std::string
getGNUProperty(uint32_t Type
, uint32_t DataSize
,
5137 ArrayRef
<uint8_t> Data
) {
5139 raw_string_ostream
OS(str
);
5141 auto DumpBit
= [&](uint32_t Flag
, StringRef Name
) {
5142 if (PrData
& Flag
) {
5152 OS
<< format("<application-specific type 0x%x>", Type
);
5154 case GNU_PROPERTY_STACK_SIZE
: {
5155 OS
<< "stack size: ";
5156 if (DataSize
== sizeof(typename
ELFT::uint
))
5157 OS
<< formatv("{0:x}",
5158 (uint64_t)(*(const typename
ELFT::Addr
*)Data
.data()));
5160 OS
<< format("<corrupt length: 0x%x>", DataSize
);
5163 case GNU_PROPERTY_NO_COPY_ON_PROTECTED
:
5164 OS
<< "no copy on protected";
5166 OS
<< format(" <corrupt length: 0x%x>", DataSize
);
5168 case GNU_PROPERTY_AARCH64_FEATURE_1_AND
:
5169 case GNU_PROPERTY_X86_FEATURE_1_AND
:
5170 OS
<< ((Type
== GNU_PROPERTY_AARCH64_FEATURE_1_AND
) ? "aarch64 feature: "
5172 if (DataSize
!= 4) {
5173 OS
<< format("<corrupt length: 0x%x>", DataSize
);
5176 PrData
= support::endian::read32
<ELFT::TargetEndianness
>(Data
.data());
5181 if (Type
== GNU_PROPERTY_AARCH64_FEATURE_1_AND
) {
5182 DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_BTI
, "BTI");
5183 DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_PAC
, "PAC");
5184 DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_GCS
, "GCS");
5186 DumpBit(GNU_PROPERTY_X86_FEATURE_1_IBT
, "IBT");
5187 DumpBit(GNU_PROPERTY_X86_FEATURE_1_SHSTK
, "SHSTK");
5190 OS
<< format("<unknown flags: 0x%x>", PrData
);
5192 case GNU_PROPERTY_X86_FEATURE_2_NEEDED
:
5193 case GNU_PROPERTY_X86_FEATURE_2_USED
:
5194 OS
<< "x86 feature "
5195 << (Type
== GNU_PROPERTY_X86_FEATURE_2_NEEDED
? "needed: " : "used: ");
5196 if (DataSize
!= 4) {
5197 OS
<< format("<corrupt length: 0x%x>", DataSize
);
5200 PrData
= support::endian::read32
<ELFT::TargetEndianness
>(Data
.data());
5205 DumpBit(GNU_PROPERTY_X86_FEATURE_2_X86
, "x86");
5206 DumpBit(GNU_PROPERTY_X86_FEATURE_2_X87
, "x87");
5207 DumpBit(GNU_PROPERTY_X86_FEATURE_2_MMX
, "MMX");
5208 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XMM
, "XMM");
5209 DumpBit(GNU_PROPERTY_X86_FEATURE_2_YMM
, "YMM");
5210 DumpBit(GNU_PROPERTY_X86_FEATURE_2_ZMM
, "ZMM");
5211 DumpBit(GNU_PROPERTY_X86_FEATURE_2_FXSR
, "FXSR");
5212 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVE
, "XSAVE");
5213 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
, "XSAVEOPT");
5214 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVEC
, "XSAVEC");
5216 OS
<< format("<unknown flags: 0x%x>", PrData
);
5218 case GNU_PROPERTY_X86_ISA_1_NEEDED
:
5219 case GNU_PROPERTY_X86_ISA_1_USED
:
5221 << (Type
== GNU_PROPERTY_X86_ISA_1_NEEDED
? "needed: " : "used: ");
5222 if (DataSize
!= 4) {
5223 OS
<< format("<corrupt length: 0x%x>", DataSize
);
5226 PrData
= support::endian::read32
<ELFT::TargetEndianness
>(Data
.data());
5231 DumpBit(GNU_PROPERTY_X86_ISA_1_BASELINE
, "x86-64-baseline");
5232 DumpBit(GNU_PROPERTY_X86_ISA_1_V2
, "x86-64-v2");
5233 DumpBit(GNU_PROPERTY_X86_ISA_1_V3
, "x86-64-v3");
5234 DumpBit(GNU_PROPERTY_X86_ISA_1_V4
, "x86-64-v4");
5236 OS
<< format("<unknown flags: 0x%x>", PrData
);
5241 template <typename ELFT
>
5242 static SmallVector
<std::string
, 4> getGNUPropertyList(ArrayRef
<uint8_t> Arr
) {
5243 using Elf_Word
= typename
ELFT::Word
;
5245 SmallVector
<std::string
, 4> Properties
;
5246 while (Arr
.size() >= 8) {
5247 uint32_t Type
= *reinterpret_cast<const Elf_Word
*>(Arr
.data());
5248 uint32_t DataSize
= *reinterpret_cast<const Elf_Word
*>(Arr
.data() + 4);
5249 Arr
= Arr
.drop_front(8);
5251 // Take padding size into account if present.
5252 uint64_t PaddedSize
= alignTo(DataSize
, sizeof(typename
ELFT::uint
));
5254 raw_string_ostream
OS(str
);
5255 if (Arr
.size() < PaddedSize
) {
5256 OS
<< format("<corrupt type (0x%x) datasz: 0x%x>", Type
, DataSize
);
5257 Properties
.push_back(OS
.str());
5260 Properties
.push_back(
5261 getGNUProperty
<ELFT
>(Type
, DataSize
, Arr
.take_front(PaddedSize
)));
5262 Arr
= Arr
.drop_front(PaddedSize
);
5266 Properties
.push_back("<corrupted GNU_PROPERTY_TYPE_0>");
5277 template <typename ELFT
> static GNUAbiTag
getGNUAbiTag(ArrayRef
<uint8_t> Desc
) {
5278 typedef typename
ELFT::Word Elf_Word
;
5280 ArrayRef
<Elf_Word
> Words(reinterpret_cast<const Elf_Word
*>(Desc
.begin()),
5281 reinterpret_cast<const Elf_Word
*>(Desc
.end()));
5283 if (Words
.size() < 4)
5284 return {"", "", /*IsValid=*/false};
5286 static const char *OSNames
[] = {
5287 "Linux", "Hurd", "Solaris", "FreeBSD", "NetBSD", "Syllable", "NaCl",
5289 StringRef OSName
= "Unknown";
5290 if (Words
[0] < std::size(OSNames
))
5291 OSName
= OSNames
[Words
[0]];
5292 uint32_t Major
= Words
[1], Minor
= Words
[2], Patch
= Words
[3];
5294 raw_string_ostream
ABI(str
);
5295 ABI
<< Major
<< "." << Minor
<< "." << Patch
;
5296 return {std::string(OSName
), ABI
.str(), /*IsValid=*/true};
5299 static std::string
getGNUBuildId(ArrayRef
<uint8_t> Desc
) {
5301 raw_string_ostream
OS(str
);
5302 for (uint8_t B
: Desc
)
5303 OS
<< format_hex_no_prefix(B
, 2);
5307 static StringRef
getDescAsStringRef(ArrayRef
<uint8_t> Desc
) {
5308 return StringRef(reinterpret_cast<const char *>(Desc
.data()), Desc
.size());
5311 template <typename ELFT
>
5312 static bool printGNUNote(raw_ostream
&OS
, uint32_t NoteType
,
5313 ArrayRef
<uint8_t> Desc
) {
5314 // Return true if we were able to pretty-print the note, false otherwise.
5318 case ELF::NT_GNU_ABI_TAG
: {
5319 const GNUAbiTag
&AbiTag
= getGNUAbiTag
<ELFT
>(Desc
);
5320 if (!AbiTag
.IsValid
)
5321 OS
<< " <corrupt GNU_ABI_TAG>";
5323 OS
<< " OS: " << AbiTag
.OSName
<< ", ABI: " << AbiTag
.ABI
;
5326 case ELF::NT_GNU_BUILD_ID
: {
5327 OS
<< " Build ID: " << getGNUBuildId(Desc
);
5330 case ELF::NT_GNU_GOLD_VERSION
:
5331 OS
<< " Version: " << getDescAsStringRef(Desc
);
5333 case ELF::NT_GNU_PROPERTY_TYPE_0
:
5334 OS
<< " Properties:";
5335 for (const std::string
&Property
: getGNUPropertyList
<ELFT
>(Desc
))
5336 OS
<< " " << Property
<< "\n";
5343 using AndroidNoteProperties
= std::vector
<std::pair
<StringRef
, std::string
>>;
5344 static AndroidNoteProperties
getAndroidNoteProperties(uint32_t NoteType
,
5345 ArrayRef
<uint8_t> Desc
) {
5346 AndroidNoteProperties Props
;
5348 case ELF::NT_ANDROID_TYPE_MEMTAG
:
5350 Props
.emplace_back("Invalid .note.android.memtag", "");
5354 switch (Desc
[0] & NT_MEMTAG_LEVEL_MASK
) {
5355 case NT_MEMTAG_LEVEL_NONE
:
5356 Props
.emplace_back("Tagging Mode", "NONE");
5358 case NT_MEMTAG_LEVEL_ASYNC
:
5359 Props
.emplace_back("Tagging Mode", "ASYNC");
5361 case NT_MEMTAG_LEVEL_SYNC
:
5362 Props
.emplace_back("Tagging Mode", "SYNC");
5367 ("Unknown (" + Twine::utohexstr(Desc
[0] & NT_MEMTAG_LEVEL_MASK
) + ")")
5371 Props
.emplace_back("Heap",
5372 (Desc
[0] & NT_MEMTAG_HEAP
) ? "Enabled" : "Disabled");
5373 Props
.emplace_back("Stack",
5374 (Desc
[0] & NT_MEMTAG_STACK
) ? "Enabled" : "Disabled");
5382 static bool printAndroidNote(raw_ostream
&OS
, uint32_t NoteType
,
5383 ArrayRef
<uint8_t> Desc
) {
5384 // Return true if we were able to pretty-print the note, false otherwise.
5385 AndroidNoteProperties Props
= getAndroidNoteProperties(NoteType
, Desc
);
5388 for (const auto &KV
: Props
)
5389 OS
<< " " << KV
.first
<< ": " << KV
.second
<< '\n';
5393 template <class ELFT
>
5394 static bool printAArch64Note(raw_ostream
&OS
, uint32_t NoteType
,
5395 ArrayRef
<uint8_t> Desc
) {
5396 if (NoteType
!= NT_ARM_TYPE_PAUTH_ABI_TAG
)
5399 OS
<< " AArch64 PAuth ABI tag: ";
5400 if (Desc
.size() < 16) {
5401 OS
<< format("<corrupted size: expected at least 16, got %d>", Desc
.size());
5406 support::endian::read64
<ELFT::TargetEndianness
>(Desc
.data() + 0);
5408 support::endian::read64
<ELFT::TargetEndianness
>(Desc
.data() + 8);
5409 OS
<< format("platform 0x%" PRIx64
", version 0x%" PRIx64
, Platform
, Version
);
5411 if (Desc
.size() > 16)
5412 OS
<< ", additional info 0x"
5413 << toHex(ArrayRef
<uint8_t>(Desc
.data() + 16, Desc
.size() - 16));
5418 template <class ELFT
>
5419 void GNUELFDumper
<ELFT
>::printMemtag(
5420 const ArrayRef
<std::pair
<std::string
, std::string
>> DynamicEntries
,
5421 const ArrayRef
<uint8_t> AndroidNoteDesc
,
5422 const ArrayRef
<std::pair
<uint64_t, uint64_t>> Descriptors
) {
5423 OS
<< "Memtag Dynamic Entries:\n";
5424 if (DynamicEntries
.empty())
5425 OS
<< " < none found >\n";
5426 for (const auto &DynamicEntryKV
: DynamicEntries
)
5427 OS
<< " " << DynamicEntryKV
.first
<< ": " << DynamicEntryKV
.second
5430 if (!AndroidNoteDesc
.empty()) {
5431 OS
<< "Memtag Android Note:\n";
5432 printAndroidNote(OS
, ELF::NT_ANDROID_TYPE_MEMTAG
, AndroidNoteDesc
);
5435 if (Descriptors
.empty())
5438 OS
<< "Memtag Global Descriptors:\n";
5439 for (const auto &[Addr
, BytesToTag
] : Descriptors
) {
5440 OS
<< " 0x" << utohexstr(Addr
, /*LowerCase=*/true) << ": 0x"
5441 << utohexstr(BytesToTag
, /*LowerCase=*/true) << "\n";
5445 template <typename ELFT
>
5446 static bool printLLVMOMPOFFLOADNote(raw_ostream
&OS
, uint32_t NoteType
,
5447 ArrayRef
<uint8_t> Desc
) {
5451 case ELF::NT_LLVM_OPENMP_OFFLOAD_VERSION
:
5452 OS
<< " Version: " << getDescAsStringRef(Desc
);
5454 case ELF::NT_LLVM_OPENMP_OFFLOAD_PRODUCER
:
5455 OS
<< " Producer: " << getDescAsStringRef(Desc
);
5457 case ELF::NT_LLVM_OPENMP_OFFLOAD_PRODUCER_VERSION
:
5458 OS
<< " Producer version: " << getDescAsStringRef(Desc
);
5465 const EnumEntry
<unsigned> FreeBSDFeatureCtlFlags
[] = {
5466 {"ASLR_DISABLE", NT_FREEBSD_FCTL_ASLR_DISABLE
},
5467 {"PROTMAX_DISABLE", NT_FREEBSD_FCTL_PROTMAX_DISABLE
},
5468 {"STKGAP_DISABLE", NT_FREEBSD_FCTL_STKGAP_DISABLE
},
5469 {"WXNEEDED", NT_FREEBSD_FCTL_WXNEEDED
},
5470 {"LA48", NT_FREEBSD_FCTL_LA48
},
5471 {"ASG_DISABLE", NT_FREEBSD_FCTL_ASG_DISABLE
},
5474 struct FreeBSDNote
{
5479 template <typename ELFT
>
5480 static std::optional
<FreeBSDNote
>
5481 getFreeBSDNote(uint32_t NoteType
, ArrayRef
<uint8_t> Desc
, bool IsCore
) {
5483 return std::nullopt
; // No pretty-printing yet.
5485 case ELF::NT_FREEBSD_ABI_TAG
:
5486 if (Desc
.size() != 4)
5487 return std::nullopt
;
5490 utostr(support::endian::read32
<ELFT::TargetEndianness
>(Desc
.data()))};
5491 case ELF::NT_FREEBSD_ARCH_TAG
:
5492 return FreeBSDNote
{"Arch tag", toStringRef(Desc
).str()};
5493 case ELF::NT_FREEBSD_FEATURE_CTL
: {
5494 if (Desc
.size() != 4)
5495 return std::nullopt
;
5497 support::endian::read32
<ELFT::TargetEndianness
>(Desc
.data());
5498 std::string FlagsStr
;
5499 raw_string_ostream
OS(FlagsStr
);
5500 printFlags(Value
, ArrayRef(FreeBSDFeatureCtlFlags
), OS
);
5501 if (OS
.str().empty())
5502 OS
<< "0x" << utohexstr(Value
);
5504 OS
<< "(0x" << utohexstr(Value
) << ")";
5505 return FreeBSDNote
{"Feature flags", OS
.str()};
5508 return std::nullopt
;
5517 template <typename ELFT
>
5518 static AMDNote
getAMDNote(uint32_t NoteType
, ArrayRef
<uint8_t> Desc
) {
5522 case ELF::NT_AMD_HSA_CODE_OBJECT_VERSION
: {
5523 struct CodeObjectVersion
{
5524 support::aligned_ulittle32_t MajorVersion
;
5525 support::aligned_ulittle32_t MinorVersion
;
5527 if (Desc
.size() != sizeof(CodeObjectVersion
))
5528 return {"AMD HSA Code Object Version",
5529 "Invalid AMD HSA Code Object Version"};
5530 std::string VersionString
;
5531 raw_string_ostream
StrOS(VersionString
);
5532 auto Version
= reinterpret_cast<const CodeObjectVersion
*>(Desc
.data());
5533 StrOS
<< "[Major: " << Version
->MajorVersion
5534 << ", Minor: " << Version
->MinorVersion
<< "]";
5535 return {"AMD HSA Code Object Version", VersionString
};
5537 case ELF::NT_AMD_HSA_HSAIL
: {
5538 struct HSAILProperties
{
5539 support::aligned_ulittle32_t HSAILMajorVersion
;
5540 support::aligned_ulittle32_t HSAILMinorVersion
;
5542 uint8_t MachineModel
;
5543 uint8_t DefaultFloatRound
;
5545 if (Desc
.size() != sizeof(HSAILProperties
))
5546 return {"AMD HSA HSAIL Properties", "Invalid AMD HSA HSAIL Properties"};
5547 auto Properties
= reinterpret_cast<const HSAILProperties
*>(Desc
.data());
5548 std::string HSAILPropetiesString
;
5549 raw_string_ostream
StrOS(HSAILPropetiesString
);
5550 StrOS
<< "[HSAIL Major: " << Properties
->HSAILMajorVersion
5551 << ", HSAIL Minor: " << Properties
->HSAILMinorVersion
5552 << ", Profile: " << uint32_t(Properties
->Profile
)
5553 << ", Machine Model: " << uint32_t(Properties
->MachineModel
)
5554 << ", Default Float Round: "
5555 << uint32_t(Properties
->DefaultFloatRound
) << "]";
5556 return {"AMD HSA HSAIL Properties", HSAILPropetiesString
};
5558 case ELF::NT_AMD_HSA_ISA_VERSION
: {
5560 support::aligned_ulittle16_t VendorNameSize
;
5561 support::aligned_ulittle16_t ArchitectureNameSize
;
5562 support::aligned_ulittle32_t Major
;
5563 support::aligned_ulittle32_t Minor
;
5564 support::aligned_ulittle32_t Stepping
;
5566 if (Desc
.size() < sizeof(IsaVersion
))
5567 return {"AMD HSA ISA Version", "Invalid AMD HSA ISA Version"};
5568 auto Isa
= reinterpret_cast<const IsaVersion
*>(Desc
.data());
5569 if (Desc
.size() < sizeof(IsaVersion
) +
5570 Isa
->VendorNameSize
+ Isa
->ArchitectureNameSize
||
5571 Isa
->VendorNameSize
== 0 || Isa
->ArchitectureNameSize
== 0)
5572 return {"AMD HSA ISA Version", "Invalid AMD HSA ISA Version"};
5573 std::string IsaString
;
5574 raw_string_ostream
StrOS(IsaString
);
5575 StrOS
<< "[Vendor: "
5576 << StringRef((const char*)Desc
.data() + sizeof(IsaVersion
), Isa
->VendorNameSize
- 1)
5577 << ", Architecture: "
5578 << StringRef((const char*)Desc
.data() + sizeof(IsaVersion
) + Isa
->VendorNameSize
,
5579 Isa
->ArchitectureNameSize
- 1)
5580 << ", Major: " << Isa
->Major
<< ", Minor: " << Isa
->Minor
5581 << ", Stepping: " << Isa
->Stepping
<< "]";
5582 return {"AMD HSA ISA Version", IsaString
};
5584 case ELF::NT_AMD_HSA_METADATA
: {
5585 if (Desc
.size() == 0)
5586 return {"AMD HSA Metadata", ""};
5589 std::string(reinterpret_cast<const char *>(Desc
.data()), Desc
.size() - 1)};
5591 case ELF::NT_AMD_HSA_ISA_NAME
: {
5592 if (Desc
.size() == 0)
5593 return {"AMD HSA ISA Name", ""};
5596 std::string(reinterpret_cast<const char *>(Desc
.data()), Desc
.size())};
5598 case ELF::NT_AMD_PAL_METADATA
: {
5599 struct PALMetadata
{
5600 support::aligned_ulittle32_t Key
;
5601 support::aligned_ulittle32_t Value
;
5603 if (Desc
.size() % sizeof(PALMetadata
) != 0)
5604 return {"AMD PAL Metadata", "Invalid AMD PAL Metadata"};
5605 auto Isa
= reinterpret_cast<const PALMetadata
*>(Desc
.data());
5606 std::string MetadataString
;
5607 raw_string_ostream
StrOS(MetadataString
);
5608 for (size_t I
= 0, E
= Desc
.size() / sizeof(PALMetadata
); I
< E
; ++I
) {
5609 StrOS
<< "[" << Isa
[I
].Key
<< ": " << Isa
[I
].Value
<< "]";
5611 return {"AMD PAL Metadata", MetadataString
};
5621 template <typename ELFT
>
5622 static AMDGPUNote
getAMDGPUNote(uint32_t NoteType
, ArrayRef
<uint8_t> Desc
) {
5626 case ELF::NT_AMDGPU_METADATA
: {
5627 StringRef MsgPackString
=
5628 StringRef(reinterpret_cast<const char *>(Desc
.data()), Desc
.size());
5629 msgpack::Document MsgPackDoc
;
5630 if (!MsgPackDoc
.readFromBlob(MsgPackString
, /*Multi=*/false))
5633 std::string MetadataString
;
5635 // FIXME: Metadata Verifier only works with AMDHSA.
5636 // This is an ugly workaround to avoid the verifier for other MD
5637 // formats (e.g. amdpal)
5638 if (MsgPackString
.contains("amdhsa.")) {
5639 AMDGPU::HSAMD::V3::MetadataVerifier
Verifier(true);
5640 if (!Verifier
.verify(MsgPackDoc
.getRoot()))
5641 MetadataString
= "Invalid AMDGPU Metadata\n";
5644 raw_string_ostream
StrOS(MetadataString
);
5645 if (MsgPackDoc
.getRoot().isScalar()) {
5646 // TODO: passing a scalar root to toYAML() asserts:
5647 // (PolymorphicTraits<T>::getKind(Val) != NodeKind::Scalar &&
5648 // "plain scalar documents are not supported")
5649 // To avoid this crash we print the raw data instead.
5652 MsgPackDoc
.toYAML(StrOS
);
5653 return {"AMDGPU Metadata", StrOS
.str()};
5658 struct CoreFileMapping
{
5659 uint64_t Start
, End
, Offset
;
5665 std::vector
<CoreFileMapping
> Mappings
;
5668 static Expected
<CoreNote
> readCoreNote(DataExtractor Desc
) {
5669 // Expected format of the NT_FILE note description:
5670 // 1. # of file mappings (call it N)
5672 // 3. N (start, end, offset) triples
5673 // 4. N packed filenames (null delimited)
5674 // Each field is an Elf_Addr, except for filenames which are char* strings.
5677 const int Bytes
= Desc
.getAddressSize();
5679 if (!Desc
.isValidOffsetForAddress(2))
5680 return createError("the note of size 0x" + Twine::utohexstr(Desc
.size()) +
5681 " is too short, expected at least 0x" +
5682 Twine::utohexstr(Bytes
* 2));
5683 if (Desc
.getData().back() != 0)
5684 return createError("the note is not NUL terminated");
5686 uint64_t DescOffset
= 0;
5687 uint64_t FileCount
= Desc
.getAddress(&DescOffset
);
5688 Ret
.PageSize
= Desc
.getAddress(&DescOffset
);
5690 if (!Desc
.isValidOffsetForAddress(3 * FileCount
* Bytes
))
5691 return createError("unable to read file mappings (found " +
5692 Twine(FileCount
) + "): the note of size 0x" +
5693 Twine::utohexstr(Desc
.size()) + " is too short");
5695 uint64_t FilenamesOffset
= 0;
5696 DataExtractor
Filenames(
5697 Desc
.getData().drop_front(DescOffset
+ 3 * FileCount
* Bytes
),
5698 Desc
.isLittleEndian(), Desc
.getAddressSize());
5700 Ret
.Mappings
.resize(FileCount
);
5702 for (CoreFileMapping
&Mapping
: Ret
.Mappings
) {
5704 if (!Filenames
.isValidOffsetForDataOfSize(FilenamesOffset
, 1))
5706 "unable to read the file name for the mapping with index " +
5707 Twine(I
) + ": the note of size 0x" + Twine::utohexstr(Desc
.size()) +
5709 Mapping
.Start
= Desc
.getAddress(&DescOffset
);
5710 Mapping
.End
= Desc
.getAddress(&DescOffset
);
5711 Mapping
.Offset
= Desc
.getAddress(&DescOffset
);
5712 Mapping
.Filename
= Filenames
.getCStrRef(&FilenamesOffset
);
5718 template <typename ELFT
>
5719 static void printCoreNote(raw_ostream
&OS
, const CoreNote
&Note
) {
5720 // Length of "0x<address>" string.
5721 const int FieldWidth
= ELFT::Is64Bits
? 18 : 10;
5723 OS
<< " Page size: " << format_decimal(Note
.PageSize
, 0) << '\n';
5724 OS
<< " " << right_justify("Start", FieldWidth
) << " "
5725 << right_justify("End", FieldWidth
) << " "
5726 << right_justify("Page Offset", FieldWidth
) << '\n';
5727 for (const CoreFileMapping
&Mapping
: Note
.Mappings
) {
5728 OS
<< " " << format_hex(Mapping
.Start
, FieldWidth
) << " "
5729 << format_hex(Mapping
.End
, FieldWidth
) << " "
5730 << format_hex(Mapping
.Offset
, FieldWidth
) << "\n "
5731 << Mapping
.Filename
<< '\n';
5735 const NoteType GenericNoteTypes
[] = {
5736 {ELF::NT_VERSION
, "NT_VERSION (version)"},
5737 {ELF::NT_ARCH
, "NT_ARCH (architecture)"},
5738 {ELF::NT_GNU_BUILD_ATTRIBUTE_OPEN
, "OPEN"},
5739 {ELF::NT_GNU_BUILD_ATTRIBUTE_FUNC
, "func"},
5742 const NoteType GNUNoteTypes
[] = {
5743 {ELF::NT_GNU_ABI_TAG
, "NT_GNU_ABI_TAG (ABI version tag)"},
5744 {ELF::NT_GNU_HWCAP
, "NT_GNU_HWCAP (DSO-supplied software HWCAP info)"},
5745 {ELF::NT_GNU_BUILD_ID
, "NT_GNU_BUILD_ID (unique build ID bitstring)"},
5746 {ELF::NT_GNU_GOLD_VERSION
, "NT_GNU_GOLD_VERSION (gold version)"},
5747 {ELF::NT_GNU_PROPERTY_TYPE_0
, "NT_GNU_PROPERTY_TYPE_0 (property note)"},
5750 const NoteType FreeBSDCoreNoteTypes
[] = {
5751 {ELF::NT_FREEBSD_THRMISC
, "NT_THRMISC (thrmisc structure)"},
5752 {ELF::NT_FREEBSD_PROCSTAT_PROC
, "NT_PROCSTAT_PROC (proc data)"},
5753 {ELF::NT_FREEBSD_PROCSTAT_FILES
, "NT_PROCSTAT_FILES (files data)"},
5754 {ELF::NT_FREEBSD_PROCSTAT_VMMAP
, "NT_PROCSTAT_VMMAP (vmmap data)"},
5755 {ELF::NT_FREEBSD_PROCSTAT_GROUPS
, "NT_PROCSTAT_GROUPS (groups data)"},
5756 {ELF::NT_FREEBSD_PROCSTAT_UMASK
, "NT_PROCSTAT_UMASK (umask data)"},
5757 {ELF::NT_FREEBSD_PROCSTAT_RLIMIT
, "NT_PROCSTAT_RLIMIT (rlimit data)"},
5758 {ELF::NT_FREEBSD_PROCSTAT_OSREL
, "NT_PROCSTAT_OSREL (osreldate data)"},
5759 {ELF::NT_FREEBSD_PROCSTAT_PSSTRINGS
,
5760 "NT_PROCSTAT_PSSTRINGS (ps_strings data)"},
5761 {ELF::NT_FREEBSD_PROCSTAT_AUXV
, "NT_PROCSTAT_AUXV (auxv data)"},
5764 const NoteType FreeBSDNoteTypes
[] = {
5765 {ELF::NT_FREEBSD_ABI_TAG
, "NT_FREEBSD_ABI_TAG (ABI version tag)"},
5766 {ELF::NT_FREEBSD_NOINIT_TAG
, "NT_FREEBSD_NOINIT_TAG (no .init tag)"},
5767 {ELF::NT_FREEBSD_ARCH_TAG
, "NT_FREEBSD_ARCH_TAG (architecture tag)"},
5768 {ELF::NT_FREEBSD_FEATURE_CTL
,
5769 "NT_FREEBSD_FEATURE_CTL (FreeBSD feature control)"},
5772 const NoteType NetBSDCoreNoteTypes
[] = {
5773 {ELF::NT_NETBSDCORE_PROCINFO
,
5774 "NT_NETBSDCORE_PROCINFO (procinfo structure)"},
5775 {ELF::NT_NETBSDCORE_AUXV
, "NT_NETBSDCORE_AUXV (ELF auxiliary vector data)"},
5776 {ELF::NT_NETBSDCORE_LWPSTATUS
, "PT_LWPSTATUS (ptrace_lwpstatus structure)"},
5779 const NoteType OpenBSDCoreNoteTypes
[] = {
5780 {ELF::NT_OPENBSD_PROCINFO
, "NT_OPENBSD_PROCINFO (procinfo structure)"},
5781 {ELF::NT_OPENBSD_AUXV
, "NT_OPENBSD_AUXV (ELF auxiliary vector data)"},
5782 {ELF::NT_OPENBSD_REGS
, "NT_OPENBSD_REGS (regular registers)"},
5783 {ELF::NT_OPENBSD_FPREGS
, "NT_OPENBSD_FPREGS (floating point registers)"},
5784 {ELF::NT_OPENBSD_WCOOKIE
, "NT_OPENBSD_WCOOKIE (window cookie)"},
5787 const NoteType AMDNoteTypes
[] = {
5788 {ELF::NT_AMD_HSA_CODE_OBJECT_VERSION
,
5789 "NT_AMD_HSA_CODE_OBJECT_VERSION (AMD HSA Code Object Version)"},
5790 {ELF::NT_AMD_HSA_HSAIL
, "NT_AMD_HSA_HSAIL (AMD HSA HSAIL Properties)"},
5791 {ELF::NT_AMD_HSA_ISA_VERSION
, "NT_AMD_HSA_ISA_VERSION (AMD HSA ISA Version)"},
5792 {ELF::NT_AMD_HSA_METADATA
, "NT_AMD_HSA_METADATA (AMD HSA Metadata)"},
5793 {ELF::NT_AMD_HSA_ISA_NAME
, "NT_AMD_HSA_ISA_NAME (AMD HSA ISA Name)"},
5794 {ELF::NT_AMD_PAL_METADATA
, "NT_AMD_PAL_METADATA (AMD PAL Metadata)"},
5797 const NoteType AMDGPUNoteTypes
[] = {
5798 {ELF::NT_AMDGPU_METADATA
, "NT_AMDGPU_METADATA (AMDGPU Metadata)"},
5801 const NoteType LLVMOMPOFFLOADNoteTypes
[] = {
5802 {ELF::NT_LLVM_OPENMP_OFFLOAD_VERSION
,
5803 "NT_LLVM_OPENMP_OFFLOAD_VERSION (image format version)"},
5804 {ELF::NT_LLVM_OPENMP_OFFLOAD_PRODUCER
,
5805 "NT_LLVM_OPENMP_OFFLOAD_PRODUCER (producing toolchain)"},
5806 {ELF::NT_LLVM_OPENMP_OFFLOAD_PRODUCER_VERSION
,
5807 "NT_LLVM_OPENMP_OFFLOAD_PRODUCER_VERSION (producing toolchain version)"},
5810 const NoteType AndroidNoteTypes
[] = {
5811 {ELF::NT_ANDROID_TYPE_IDENT
, "NT_ANDROID_TYPE_IDENT"},
5812 {ELF::NT_ANDROID_TYPE_KUSER
, "NT_ANDROID_TYPE_KUSER"},
5813 {ELF::NT_ANDROID_TYPE_MEMTAG
,
5814 "NT_ANDROID_TYPE_MEMTAG (Android memory tagging information)"},
5817 const NoteType ARMNoteTypes
[] = {
5818 {ELF::NT_ARM_TYPE_PAUTH_ABI_TAG
, "NT_ARM_TYPE_PAUTH_ABI_TAG"},
5821 const NoteType CoreNoteTypes
[] = {
5822 {ELF::NT_PRSTATUS
, "NT_PRSTATUS (prstatus structure)"},
5823 {ELF::NT_FPREGSET
, "NT_FPREGSET (floating point registers)"},
5824 {ELF::NT_PRPSINFO
, "NT_PRPSINFO (prpsinfo structure)"},
5825 {ELF::NT_TASKSTRUCT
, "NT_TASKSTRUCT (task structure)"},
5826 {ELF::NT_AUXV
, "NT_AUXV (auxiliary vector)"},
5827 {ELF::NT_PSTATUS
, "NT_PSTATUS (pstatus structure)"},
5828 {ELF::NT_FPREGS
, "NT_FPREGS (floating point registers)"},
5829 {ELF::NT_PSINFO
, "NT_PSINFO (psinfo structure)"},
5830 {ELF::NT_LWPSTATUS
, "NT_LWPSTATUS (lwpstatus_t structure)"},
5831 {ELF::NT_LWPSINFO
, "NT_LWPSINFO (lwpsinfo_t structure)"},
5832 {ELF::NT_WIN32PSTATUS
, "NT_WIN32PSTATUS (win32_pstatus structure)"},
5834 {ELF::NT_PPC_VMX
, "NT_PPC_VMX (ppc Altivec registers)"},
5835 {ELF::NT_PPC_VSX
, "NT_PPC_VSX (ppc VSX registers)"},
5836 {ELF::NT_PPC_TAR
, "NT_PPC_TAR (ppc TAR register)"},
5837 {ELF::NT_PPC_PPR
, "NT_PPC_PPR (ppc PPR register)"},
5838 {ELF::NT_PPC_DSCR
, "NT_PPC_DSCR (ppc DSCR register)"},
5839 {ELF::NT_PPC_EBB
, "NT_PPC_EBB (ppc EBB registers)"},
5840 {ELF::NT_PPC_PMU
, "NT_PPC_PMU (ppc PMU registers)"},
5841 {ELF::NT_PPC_TM_CGPR
, "NT_PPC_TM_CGPR (ppc checkpointed GPR registers)"},
5842 {ELF::NT_PPC_TM_CFPR
,
5843 "NT_PPC_TM_CFPR (ppc checkpointed floating point registers)"},
5844 {ELF::NT_PPC_TM_CVMX
,
5845 "NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)"},
5846 {ELF::NT_PPC_TM_CVSX
, "NT_PPC_TM_CVSX (ppc checkpointed VSX registers)"},
5847 {ELF::NT_PPC_TM_SPR
, "NT_PPC_TM_SPR (ppc TM special purpose registers)"},
5848 {ELF::NT_PPC_TM_CTAR
, "NT_PPC_TM_CTAR (ppc checkpointed TAR register)"},
5849 {ELF::NT_PPC_TM_CPPR
, "NT_PPC_TM_CPPR (ppc checkpointed PPR register)"},
5850 {ELF::NT_PPC_TM_CDSCR
, "NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)"},
5852 {ELF::NT_386_TLS
, "NT_386_TLS (x86 TLS information)"},
5853 {ELF::NT_386_IOPERM
, "NT_386_IOPERM (x86 I/O permissions)"},
5854 {ELF::NT_X86_XSTATE
, "NT_X86_XSTATE (x86 XSAVE extended state)"},
5856 {ELF::NT_S390_HIGH_GPRS
, "NT_S390_HIGH_GPRS (s390 upper register halves)"},
5857 {ELF::NT_S390_TIMER
, "NT_S390_TIMER (s390 timer register)"},
5858 {ELF::NT_S390_TODCMP
, "NT_S390_TODCMP (s390 TOD comparator register)"},
5859 {ELF::NT_S390_TODPREG
, "NT_S390_TODPREG (s390 TOD programmable register)"},
5860 {ELF::NT_S390_CTRS
, "NT_S390_CTRS (s390 control registers)"},
5861 {ELF::NT_S390_PREFIX
, "NT_S390_PREFIX (s390 prefix register)"},
5862 {ELF::NT_S390_LAST_BREAK
,
5863 "NT_S390_LAST_BREAK (s390 last breaking event address)"},
5864 {ELF::NT_S390_SYSTEM_CALL
,
5865 "NT_S390_SYSTEM_CALL (s390 system call restart data)"},
5866 {ELF::NT_S390_TDB
, "NT_S390_TDB (s390 transaction diagnostic block)"},
5867 {ELF::NT_S390_VXRS_LOW
,
5868 "NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)"},
5869 {ELF::NT_S390_VXRS_HIGH
, "NT_S390_VXRS_HIGH (s390 vector registers 16-31)"},
5870 {ELF::NT_S390_GS_CB
, "NT_S390_GS_CB (s390 guarded-storage registers)"},
5871 {ELF::NT_S390_GS_BC
,
5872 "NT_S390_GS_BC (s390 guarded-storage broadcast control)"},
5874 {ELF::NT_ARM_VFP
, "NT_ARM_VFP (arm VFP registers)"},
5875 {ELF::NT_ARM_TLS
, "NT_ARM_TLS (AArch TLS registers)"},
5876 {ELF::NT_ARM_HW_BREAK
,
5877 "NT_ARM_HW_BREAK (AArch hardware breakpoint registers)"},
5878 {ELF::NT_ARM_HW_WATCH
,
5879 "NT_ARM_HW_WATCH (AArch hardware watchpoint registers)"},
5880 {ELF::NT_ARM_SVE
, "NT_ARM_SVE (AArch64 SVE registers)"},
5881 {ELF::NT_ARM_PAC_MASK
,
5882 "NT_ARM_PAC_MASK (AArch64 Pointer Authentication code masks)"},
5883 {ELF::NT_ARM_TAGGED_ADDR_CTRL
,
5884 "NT_ARM_TAGGED_ADDR_CTRL (AArch64 Tagged Address Control)"},
5885 {ELF::NT_ARM_SSVE
, "NT_ARM_SSVE (AArch64 Streaming SVE registers)"},
5886 {ELF::NT_ARM_ZA
, "NT_ARM_ZA (AArch64 SME ZA registers)"},
5887 {ELF::NT_ARM_ZT
, "NT_ARM_ZT (AArch64 SME ZT registers)"},
5889 {ELF::NT_FILE
, "NT_FILE (mapped files)"},
5890 {ELF::NT_PRXFPREG
, "NT_PRXFPREG (user_xfpregs structure)"},
5891 {ELF::NT_SIGINFO
, "NT_SIGINFO (siginfo_t data)"},
5894 template <class ELFT
>
5895 StringRef
getNoteTypeName(const typename
ELFT::Note
&Note
, unsigned ELFType
) {
5896 uint32_t Type
= Note
.getType();
5897 auto FindNote
= [&](ArrayRef
<NoteType
> V
) -> StringRef
{
5898 for (const NoteType
&N
: V
)
5904 StringRef Name
= Note
.getName();
5906 return FindNote(GNUNoteTypes
);
5907 if (Name
== "FreeBSD") {
5908 if (ELFType
== ELF::ET_CORE
) {
5909 // FreeBSD also places the generic core notes in the FreeBSD namespace.
5910 StringRef Result
= FindNote(FreeBSDCoreNoteTypes
);
5911 if (!Result
.empty())
5913 return FindNote(CoreNoteTypes
);
5915 return FindNote(FreeBSDNoteTypes
);
5918 if (ELFType
== ELF::ET_CORE
&& Name
.starts_with("NetBSD-CORE")) {
5919 StringRef Result
= FindNote(NetBSDCoreNoteTypes
);
5920 if (!Result
.empty())
5922 return FindNote(CoreNoteTypes
);
5924 if (ELFType
== ELF::ET_CORE
&& Name
.starts_with("OpenBSD")) {
5925 // OpenBSD also places the generic core notes in the OpenBSD namespace.
5926 StringRef Result
= FindNote(OpenBSDCoreNoteTypes
);
5927 if (!Result
.empty())
5929 return FindNote(CoreNoteTypes
);
5932 return FindNote(AMDNoteTypes
);
5933 if (Name
== "AMDGPU")
5934 return FindNote(AMDGPUNoteTypes
);
5935 if (Name
== "LLVMOMPOFFLOAD")
5936 return FindNote(LLVMOMPOFFLOADNoteTypes
);
5937 if (Name
== "Android")
5938 return FindNote(AndroidNoteTypes
);
5940 return FindNote(ARMNoteTypes
);
5942 if (ELFType
== ELF::ET_CORE
)
5943 return FindNote(CoreNoteTypes
);
5944 return FindNote(GenericNoteTypes
);
5947 template <class ELFT
>
5948 static void processNotesHelper(
5949 const ELFDumper
<ELFT
> &Dumper
,
5950 llvm::function_ref
<void(std::optional
<StringRef
>, typename
ELFT::Off
,
5951 typename
ELFT::Addr
, size_t)>
5953 llvm::function_ref
<Error(const typename
ELFT::Note
&, bool)> ProcessNoteFn
,
5954 llvm::function_ref
<void()> FinishNotesFn
) {
5955 const ELFFile
<ELFT
> &Obj
= Dumper
.getElfObject().getELFFile();
5956 bool IsCoreFile
= Obj
.getHeader().e_type
== ELF::ET_CORE
;
5958 ArrayRef
<typename
ELFT::Shdr
> Sections
= cantFail(Obj
.sections());
5959 if (!IsCoreFile
&& !Sections
.empty()) {
5960 for (const typename
ELFT::Shdr
&S
: Sections
) {
5961 if (S
.sh_type
!= SHT_NOTE
)
5963 StartNotesFn(expectedToStdOptional(Obj
.getSectionName(S
)), S
.sh_offset
,
5964 S
.sh_size
, S
.sh_addralign
);
5965 Error Err
= Error::success();
5967 for (const typename
ELFT::Note Note
: Obj
.notes(S
, Err
)) {
5968 if (Error E
= ProcessNoteFn(Note
, IsCoreFile
))
5969 Dumper
.reportUniqueWarning(
5970 "unable to read note with index " + Twine(I
) + " from the " +
5971 describe(Obj
, S
) + ": " + toString(std::move(E
)));
5975 Dumper
.reportUniqueWarning("unable to read notes from the " +
5976 describe(Obj
, S
) + ": " +
5977 toString(std::move(Err
)));
5983 Expected
<ArrayRef
<typename
ELFT::Phdr
>> PhdrsOrErr
= Obj
.program_headers();
5985 Dumper
.reportUniqueWarning(
5986 "unable to read program headers to locate the PT_NOTE segment: " +
5987 toString(PhdrsOrErr
.takeError()));
5991 for (size_t I
= 0, E
= (*PhdrsOrErr
).size(); I
!= E
; ++I
) {
5992 const typename
ELFT::Phdr
&P
= (*PhdrsOrErr
)[I
];
5993 if (P
.p_type
!= PT_NOTE
)
5995 StartNotesFn(/*SecName=*/std::nullopt
, P
.p_offset
, P
.p_filesz
, P
.p_align
);
5996 Error Err
= Error::success();
5998 for (const typename
ELFT::Note Note
: Obj
.notes(P
, Err
)) {
5999 if (Error E
= ProcessNoteFn(Note
, IsCoreFile
))
6000 Dumper
.reportUniqueWarning("unable to read note with index " +
6002 " from the PT_NOTE segment with index " +
6003 Twine(I
) + ": " + toString(std::move(E
)));
6007 Dumper
.reportUniqueWarning(
6008 "unable to read notes from the PT_NOTE segment with index " +
6009 Twine(I
) + ": " + toString(std::move(Err
)));
6014 template <class ELFT
> void GNUELFDumper
<ELFT
>::printNotes() {
6016 bool IsFirstHeader
= true;
6017 auto PrintHeader
= [&](std::optional
<StringRef
> SecName
,
6018 const typename
ELFT::Off Offset
,
6019 const typename
ELFT::Addr Size
, size_t Al
) {
6020 Align
= std::max
<size_t>(Al
, 4);
6021 // Print a newline between notes sections to match GNU readelf.
6022 if (!IsFirstHeader
) {
6025 IsFirstHeader
= false;
6028 OS
<< "Displaying notes found ";
6031 OS
<< "in: " << *SecName
<< "\n";
6033 OS
<< "at file offset " << format_hex(Offset
, 10) << " with length "
6034 << format_hex(Size
, 10) << ":\n";
6036 OS
<< " Owner Data size \tDescription\n";
6039 auto ProcessNote
= [&](const Elf_Note
&Note
, bool IsCore
) -> Error
{
6040 StringRef Name
= Note
.getName();
6041 ArrayRef
<uint8_t> Descriptor
= Note
.getDesc(Align
);
6042 Elf_Word Type
= Note
.getType();
6044 // Print the note owner/type.
6045 OS
<< " " << left_justify(Name
, 20) << ' '
6046 << format_hex(Descriptor
.size(), 10) << '\t';
6048 StringRef NoteType
=
6049 getNoteTypeName
<ELFT
>(Note
, this->Obj
.getHeader().e_type
);
6050 if (!NoteType
.empty())
6051 OS
<< NoteType
<< '\n';
6053 OS
<< "Unknown note type: (" << format_hex(Type
, 10) << ")\n";
6055 // Print the description, or fallback to printing raw bytes for unknown
6056 // owners/if we fail to pretty-print the contents.
6057 if (Name
== "GNU") {
6058 if (printGNUNote
<ELFT
>(OS
, Type
, Descriptor
))
6059 return Error::success();
6060 } else if (Name
== "FreeBSD") {
6061 if (std::optional
<FreeBSDNote
> N
=
6062 getFreeBSDNote
<ELFT
>(Type
, Descriptor
, IsCore
)) {
6063 OS
<< " " << N
->Type
<< ": " << N
->Value
<< '\n';
6064 return Error::success();
6066 } else if (Name
== "AMD") {
6067 const AMDNote N
= getAMDNote
<ELFT
>(Type
, Descriptor
);
6068 if (!N
.Type
.empty()) {
6069 OS
<< " " << N
.Type
<< ":\n " << N
.Value
<< '\n';
6070 return Error::success();
6072 } else if (Name
== "AMDGPU") {
6073 const AMDGPUNote N
= getAMDGPUNote
<ELFT
>(Type
, Descriptor
);
6074 if (!N
.Type
.empty()) {
6075 OS
<< " " << N
.Type
<< ":\n " << N
.Value
<< '\n';
6076 return Error::success();
6078 } else if (Name
== "LLVMOMPOFFLOAD") {
6079 if (printLLVMOMPOFFLOADNote
<ELFT
>(OS
, Type
, Descriptor
))
6080 return Error::success();
6081 } else if (Name
== "CORE") {
6082 if (Type
== ELF::NT_FILE
) {
6083 DataExtractor
DescExtractor(
6084 Descriptor
, ELFT::TargetEndianness
== llvm::endianness::little
,
6086 if (Expected
<CoreNote
> NoteOrErr
= readCoreNote(DescExtractor
)) {
6087 printCoreNote
<ELFT
>(OS
, *NoteOrErr
);
6088 return Error::success();
6090 return NoteOrErr
.takeError();
6093 } else if (Name
== "Android") {
6094 if (printAndroidNote(OS
, Type
, Descriptor
))
6095 return Error::success();
6096 } else if (Name
== "ARM") {
6097 if (printAArch64Note
<ELFT
>(OS
, Type
, Descriptor
))
6098 return Error::success();
6100 if (!Descriptor
.empty()) {
6101 OS
<< " description data:";
6102 for (uint8_t B
: Descriptor
)
6103 OS
<< " " << format("%02x", B
);
6106 return Error::success();
6109 processNotesHelper(*this, /*StartNotesFn=*/PrintHeader
,
6110 /*ProcessNoteFn=*/ProcessNote
, /*FinishNotesFn=*/[]() {});
6113 template <class ELFT
>
6115 ELFDumper
<ELFT
>::getMemtagGlobalsSectionContents(uint64_t ExpectedAddr
) {
6116 for (const typename
ELFT::Shdr
&Sec
: cantFail(Obj
.sections())) {
6117 if (Sec
.sh_type
!= SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC
)
6119 if (Sec
.sh_addr
!= ExpectedAddr
) {
6120 reportUniqueWarning(
6121 "SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC section was unexpectedly at 0x" +
6122 Twine::utohexstr(Sec
.sh_addr
) +
6123 ", when DT_AARCH64_MEMTAG_GLOBALS says it should be at 0x" +
6124 Twine::utohexstr(ExpectedAddr
));
6125 return ArrayRef
<uint8_t>();
6127 Expected
<ArrayRef
<uint8_t>> Contents
= Obj
.getSectionContents(Sec
);
6128 if (auto E
= Contents
.takeError()) {
6129 reportUniqueWarning(
6130 "couldn't get SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC section contents: " +
6131 toString(std::move(E
)));
6132 return ArrayRef
<uint8_t>();
6134 return Contents
.get();
6136 return ArrayRef
<uint8_t>();
6139 // Reserve the lower three bits of the first byte of the step distance when
6140 // encoding the memtag descriptors. Found to be the best overall size tradeoff
6141 // when compiling Android T with full MTE globals enabled.
6142 constexpr uint64_t MemtagStepVarintReservedBits
= 3;
6143 constexpr uint64_t MemtagGranuleSize
= 16;
6145 template <typename ELFT
> void ELFDumper
<ELFT
>::printMemtag() {
6146 if (Obj
.getHeader().e_machine
!= EM_AARCH64
) return;
6147 std::vector
<std::pair
<std::string
, std::string
>> DynamicEntries
;
6148 uint64_t MemtagGlobalsSz
= 0;
6149 uint64_t MemtagGlobals
= 0;
6150 for (const typename
ELFT::Dyn
&Entry
: dynamic_table()) {
6151 uintX_t Tag
= Entry
.getTag();
6153 case DT_AARCH64_MEMTAG_GLOBALSSZ
:
6154 MemtagGlobalsSz
= Entry
.getVal();
6155 DynamicEntries
.emplace_back(Obj
.getDynamicTagAsString(Tag
),
6156 getDynamicEntry(Tag
, Entry
.getVal()));
6158 case DT_AARCH64_MEMTAG_GLOBALS
:
6159 MemtagGlobals
= Entry
.getVal();
6160 DynamicEntries
.emplace_back(Obj
.getDynamicTagAsString(Tag
),
6161 getDynamicEntry(Tag
, Entry
.getVal()));
6163 case DT_AARCH64_MEMTAG_MODE
:
6164 case DT_AARCH64_MEMTAG_HEAP
:
6165 case DT_AARCH64_MEMTAG_STACK
:
6166 DynamicEntries
.emplace_back(Obj
.getDynamicTagAsString(Tag
),
6167 getDynamicEntry(Tag
, Entry
.getVal()));
6172 ArrayRef
<uint8_t> AndroidNoteDesc
;
6173 auto FindAndroidNote
= [&](const Elf_Note
&Note
, bool IsCore
) -> Error
{
6174 if (Note
.getName() == "Android" &&
6175 Note
.getType() == ELF::NT_ANDROID_TYPE_MEMTAG
)
6176 AndroidNoteDesc
= Note
.getDesc(4);
6177 return Error::success();
6183 [](std::optional
<StringRef
>, const typename
ELFT::Off
,
6184 const typename
ELFT::Addr
, size_t) {},
6185 /*ProcessNoteFn=*/FindAndroidNote
, /*FinishNotesFn=*/[]() {});
6187 ArrayRef
<uint8_t> Contents
= getMemtagGlobalsSectionContents(MemtagGlobals
);
6188 if (Contents
.size() != MemtagGlobalsSz
) {
6189 reportUniqueWarning(
6190 "mismatch between DT_AARCH64_MEMTAG_GLOBALSSZ (0x" +
6191 Twine::utohexstr(MemtagGlobalsSz
) +
6192 ") and SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC section size (0x" +
6193 Twine::utohexstr(Contents
.size()) + ")");
6194 Contents
= ArrayRef
<uint8_t>();
6197 std::vector
<std::pair
<uint64_t, uint64_t>> GlobalDescriptors
;
6198 uint64_t Address
= 0;
6199 // See the AArch64 MemtagABI document for a description of encoding scheme:
6200 // https://github.com/ARM-software/abi-aa/blob/main/memtagabielf64/memtagabielf64.rst#83encoding-of-sht_aarch64_memtag_globals_dynamic
6201 for (size_t I
= 0; I
< Contents
.size();) {
6202 const char *Error
= nullptr;
6203 unsigned DecodedBytes
= 0;
6204 uint64_t Value
= decodeULEB128(Contents
.data() + I
, &DecodedBytes
,
6205 Contents
.end(), &Error
);
6208 reportUniqueWarning(
6209 "error decoding distance uleb, " + Twine(DecodedBytes
) +
6210 " byte(s) into SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC: " + Twine(Error
));
6211 GlobalDescriptors
.clear();
6214 uint64_t Distance
= Value
>> MemtagStepVarintReservedBits
;
6215 uint64_t GranulesToTag
= Value
& ((1 << MemtagStepVarintReservedBits
) - 1);
6216 if (GranulesToTag
== 0) {
6217 GranulesToTag
= decodeULEB128(Contents
.data() + I
, &DecodedBytes
,
6218 Contents
.end(), &Error
) +
6222 reportUniqueWarning(
6223 "error decoding size-only uleb, " + Twine(DecodedBytes
) +
6224 " byte(s) into SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC: " + Twine(Error
));
6225 GlobalDescriptors
.clear();
6229 Address
+= Distance
* MemtagGranuleSize
;
6230 GlobalDescriptors
.emplace_back(Address
, GranulesToTag
* MemtagGranuleSize
);
6231 Address
+= GranulesToTag
* MemtagGranuleSize
;
6234 printMemtag(DynamicEntries
, AndroidNoteDesc
, GlobalDescriptors
);
6237 template <class ELFT
> void GNUELFDumper
<ELFT
>::printELFLinkerOptions() {
6238 OS
<< "printELFLinkerOptions not implemented!\n";
6241 template <class ELFT
>
6242 void ELFDumper
<ELFT
>::printDependentLibsHelper(
6243 function_ref
<void(const Elf_Shdr
&)> OnSectionStart
,
6244 function_ref
<void(StringRef
, uint64_t)> OnLibEntry
) {
6245 auto Warn
= [this](unsigned SecNdx
, StringRef Msg
) {
6246 this->reportUniqueWarning("SHT_LLVM_DEPENDENT_LIBRARIES section at index " +
6247 Twine(SecNdx
) + " is broken: " + Msg
);
6251 for (const Elf_Shdr
&Shdr
: cantFail(Obj
.sections())) {
6253 if (Shdr
.sh_type
!= ELF::SHT_LLVM_DEPENDENT_LIBRARIES
)
6256 OnSectionStart(Shdr
);
6258 Expected
<ArrayRef
<uint8_t>> ContentsOrErr
= Obj
.getSectionContents(Shdr
);
6259 if (!ContentsOrErr
) {
6260 Warn(I
, toString(ContentsOrErr
.takeError()));
6264 ArrayRef
<uint8_t> Contents
= *ContentsOrErr
;
6265 if (!Contents
.empty() && Contents
.back() != 0) {
6266 Warn(I
, "the content is not null-terminated");
6270 for (const uint8_t *I
= Contents
.begin(), *E
= Contents
.end(); I
< E
;) {
6271 StringRef
Lib((const char *)I
);
6272 OnLibEntry(Lib
, I
- Contents
.begin());
6273 I
+= Lib
.size() + 1;
6278 template <class ELFT
>
6279 void ELFDumper
<ELFT
>::forEachRelocationDo(
6280 const Elf_Shdr
&Sec
, bool RawRelr
,
6281 llvm::function_ref
<void(const Relocation
<ELFT
> &, unsigned,
6282 const Elf_Shdr
&, const Elf_Shdr
*)>
6284 llvm::function_ref
<void(const Elf_Relr
&)> RelrFn
) {
6285 auto Warn
= [&](Error
&&E
,
6286 const Twine
&Prefix
= "unable to read relocations from") {
6287 this->reportUniqueWarning(Prefix
+ " " + describe(Sec
) + ": " +
6288 toString(std::move(E
)));
6291 // SHT_RELR/SHT_ANDROID_RELR/SHT_AARCH64_AUTH_RELR sections do not have an
6292 // associated symbol table. For them we should not treat the value of the
6293 // sh_link field as an index of a symbol table.
6294 const Elf_Shdr
*SymTab
;
6295 if (Sec
.sh_type
!= ELF::SHT_RELR
&& Sec
.sh_type
!= ELF::SHT_ANDROID_RELR
&&
6296 !(Obj
.getHeader().e_machine
== EM_AARCH64
&&
6297 Sec
.sh_type
== ELF::SHT_AARCH64_AUTH_RELR
)) {
6298 Expected
<const Elf_Shdr
*> SymTabOrErr
= Obj
.getSection(Sec
.sh_link
);
6300 Warn(SymTabOrErr
.takeError(), "unable to locate a symbol table for");
6303 SymTab
= *SymTabOrErr
;
6306 unsigned RelNdx
= 0;
6307 const bool IsMips64EL
= this->Obj
.isMips64EL();
6308 switch (Sec
.sh_type
) {
6310 if (Expected
<Elf_Rel_Range
> RangeOrErr
= Obj
.rels(Sec
)) {
6311 for (const Elf_Rel
&R
: *RangeOrErr
)
6312 RelRelaFn(Relocation
<ELFT
>(R
, IsMips64EL
), RelNdx
++, Sec
, SymTab
);
6314 Warn(RangeOrErr
.takeError());
6318 if (Expected
<Elf_Rela_Range
> RangeOrErr
= Obj
.relas(Sec
)) {
6319 for (const Elf_Rela
&R
: *RangeOrErr
)
6320 RelRelaFn(Relocation
<ELFT
>(R
, IsMips64EL
), RelNdx
++, Sec
, SymTab
);
6322 Warn(RangeOrErr
.takeError());
6325 case ELF::SHT_AARCH64_AUTH_RELR
:
6326 if (Obj
.getHeader().e_machine
!= EM_AARCH64
)
6330 case ELF::SHT_ANDROID_RELR
: {
6331 Expected
<Elf_Relr_Range
> RangeOrErr
= Obj
.relrs(Sec
);
6333 Warn(RangeOrErr
.takeError());
6337 for (const Elf_Relr
&R
: *RangeOrErr
)
6342 for (const Elf_Rel
&R
: Obj
.decode_relrs(*RangeOrErr
))
6343 RelRelaFn(Relocation
<ELFT
>(R
, IsMips64EL
), RelNdx
++, Sec
,
6344 /*SymTab=*/nullptr);
6347 case ELF::SHT_ANDROID_REL
:
6348 case ELF::SHT_ANDROID_RELA
:
6349 if (Expected
<std::vector
<Elf_Rela
>> RelasOrErr
= Obj
.android_relas(Sec
)) {
6350 for (const Elf_Rela
&R
: *RelasOrErr
)
6351 RelRelaFn(Relocation
<ELFT
>(R
, IsMips64EL
), RelNdx
++, Sec
, SymTab
);
6353 Warn(RelasOrErr
.takeError());
6359 template <class ELFT
>
6360 StringRef ELFDumper
<ELFT
>::getPrintableSectionName(const Elf_Shdr
&Sec
) const {
6361 StringRef Name
= "<?>";
6362 if (Expected
<StringRef
> SecNameOrErr
=
6363 Obj
.getSectionName(Sec
, this->WarningHandler
))
6364 Name
= *SecNameOrErr
;
6366 this->reportUniqueWarning("unable to get the name of " + describe(Sec
) +
6367 ": " + toString(SecNameOrErr
.takeError()));
6371 template <class ELFT
> void GNUELFDumper
<ELFT
>::printDependentLibs() {
6372 bool SectionStarted
= false;
6377 std::vector
<NameOffset
> SecEntries
;
6379 auto PrintSection
= [&]() {
6380 OS
<< "Dependent libraries section " << Current
.Name
<< " at offset "
6381 << format_hex(Current
.Offset
, 1) << " contains " << SecEntries
.size()
6383 for (NameOffset Entry
: SecEntries
)
6384 OS
<< " [" << format("%6" PRIx64
, Entry
.Offset
) << "] " << Entry
.Name
6390 auto OnSectionStart
= [&](const Elf_Shdr
&Shdr
) {
6393 SectionStarted
= true;
6394 Current
.Offset
= Shdr
.sh_offset
;
6395 Current
.Name
= this->getPrintableSectionName(Shdr
);
6397 auto OnLibEntry
= [&](StringRef Lib
, uint64_t Offset
) {
6398 SecEntries
.push_back(NameOffset
{Lib
, Offset
});
6401 this->printDependentLibsHelper(OnSectionStart
, OnLibEntry
);
6406 template <class ELFT
>
6407 SmallVector
<uint32_t> ELFDumper
<ELFT
>::getSymbolIndexesForFunctionAddress(
6408 uint64_t SymValue
, std::optional
<const Elf_Shdr
*> FunctionSec
) {
6409 SmallVector
<uint32_t> SymbolIndexes
;
6410 if (!this->AddressToIndexMap
) {
6411 // Populate the address to index map upon the first invocation of this
6413 this->AddressToIndexMap
.emplace();
6414 if (this->DotSymtabSec
) {
6415 if (Expected
<Elf_Sym_Range
> SymsOrError
=
6416 Obj
.symbols(this->DotSymtabSec
)) {
6417 uint32_t Index
= (uint32_t)-1;
6418 for (const Elf_Sym
&Sym
: *SymsOrError
) {
6421 if (Sym
.st_shndx
== ELF::SHN_UNDEF
|| Sym
.getType() != ELF::STT_FUNC
)
6424 Expected
<uint64_t> SymAddrOrErr
=
6425 ObjF
.toSymbolRef(this->DotSymtabSec
, Index
).getAddress();
6426 if (!SymAddrOrErr
) {
6427 std::string Name
= this->getStaticSymbolName(Index
);
6428 reportUniqueWarning("unable to get address of symbol '" + Name
+
6429 "': " + toString(SymAddrOrErr
.takeError()));
6430 return SymbolIndexes
;
6433 (*this->AddressToIndexMap
)[*SymAddrOrErr
].push_back(Index
);
6436 reportUniqueWarning("unable to read the symbol table: " +
6437 toString(SymsOrError
.takeError()));
6442 auto Symbols
= this->AddressToIndexMap
->find(SymValue
);
6443 if (Symbols
== this->AddressToIndexMap
->end())
6444 return SymbolIndexes
;
6446 for (uint32_t Index
: Symbols
->second
) {
6447 // Check if the symbol is in the right section. FunctionSec == None
6448 // means "any section".
6450 const Elf_Sym
&Sym
= *cantFail(Obj
.getSymbol(this->DotSymtabSec
, Index
));
6451 if (Expected
<const Elf_Shdr
*> SecOrErr
=
6452 Obj
.getSection(Sym
, this->DotSymtabSec
,
6453 this->getShndxTable(this->DotSymtabSec
))) {
6454 if (*FunctionSec
!= *SecOrErr
)
6457 std::string Name
= this->getStaticSymbolName(Index
);
6458 // Note: it is impossible to trigger this error currently, it is
6460 reportUniqueWarning("unable to get section of symbol '" + Name
+
6461 "': " + toString(SecOrErr
.takeError()));
6462 return SymbolIndexes
;
6466 SymbolIndexes
.push_back(Index
);
6469 return SymbolIndexes
;
6472 template <class ELFT
>
6473 bool ELFDumper
<ELFT
>::printFunctionStackSize(
6474 uint64_t SymValue
, std::optional
<const Elf_Shdr
*> FunctionSec
,
6475 const Elf_Shdr
&StackSizeSec
, DataExtractor Data
, uint64_t *Offset
) {
6476 SmallVector
<uint32_t> FuncSymIndexes
=
6477 this->getSymbolIndexesForFunctionAddress(SymValue
, FunctionSec
);
6478 if (FuncSymIndexes
.empty())
6479 reportUniqueWarning(
6480 "could not identify function symbol for stack size entry in " +
6481 describe(StackSizeSec
));
6483 // Extract the size. The expectation is that Offset is pointing to the right
6484 // place, i.e. past the function address.
6485 Error Err
= Error::success();
6486 uint64_t StackSize
= Data
.getULEB128(Offset
, &Err
);
6488 reportUniqueWarning("could not extract a valid stack size from " +
6489 describe(StackSizeSec
) + ": " +
6490 toString(std::move(Err
)));
6494 if (FuncSymIndexes
.empty()) {
6495 printStackSizeEntry(StackSize
, {"?"});
6497 SmallVector
<std::string
> FuncSymNames
;
6498 for (uint32_t Index
: FuncSymIndexes
)
6499 FuncSymNames
.push_back(this->getStaticSymbolName(Index
));
6500 printStackSizeEntry(StackSize
, FuncSymNames
);
6506 template <class ELFT
>
6507 void GNUELFDumper
<ELFT
>::printStackSizeEntry(uint64_t Size
,
6508 ArrayRef
<std::string
> FuncNames
) {
6510 OS
<< format_decimal(Size
, 11);
6513 OS
<< join(FuncNames
.begin(), FuncNames
.end(), ", ") << "\n";
6516 template <class ELFT
>
6517 void ELFDumper
<ELFT
>::printStackSize(const Relocation
<ELFT
> &R
,
6518 const Elf_Shdr
&RelocSec
, unsigned Ndx
,
6519 const Elf_Shdr
*SymTab
,
6520 const Elf_Shdr
*FunctionSec
,
6521 const Elf_Shdr
&StackSizeSec
,
6522 const RelocationResolver
&Resolver
,
6523 DataExtractor Data
) {
6524 // This function ignores potentially erroneous input, unless it is directly
6525 // related to stack size reporting.
6526 const Elf_Sym
*Sym
= nullptr;
6527 Expected
<RelSymbol
<ELFT
>> TargetOrErr
= this->getRelocationTarget(R
, SymTab
);
6529 reportUniqueWarning("unable to get the target of relocation with index " +
6530 Twine(Ndx
) + " in " + describe(RelocSec
) + ": " +
6531 toString(TargetOrErr
.takeError()));
6533 Sym
= TargetOrErr
->Sym
;
6535 uint64_t RelocSymValue
= 0;
6537 Expected
<const Elf_Shdr
*> SectionOrErr
=
6538 this->Obj
.getSection(*Sym
, SymTab
, this->getShndxTable(SymTab
));
6539 if (!SectionOrErr
) {
6540 reportUniqueWarning(
6541 "cannot identify the section for relocation symbol '" +
6542 (*TargetOrErr
).Name
+ "': " + toString(SectionOrErr
.takeError()));
6543 } else if (*SectionOrErr
!= FunctionSec
) {
6544 reportUniqueWarning("relocation symbol '" + (*TargetOrErr
).Name
+
6545 "' is not in the expected section");
6546 // Pretend that the symbol is in the correct section and report its
6547 // stack size anyway.
6548 FunctionSec
= *SectionOrErr
;
6551 RelocSymValue
= Sym
->st_value
;
6554 uint64_t Offset
= R
.Offset
;
6555 if (!Data
.isValidOffsetForDataOfSize(Offset
, sizeof(Elf_Addr
) + 1)) {
6556 reportUniqueWarning("found invalid relocation offset (0x" +
6557 Twine::utohexstr(Offset
) + ") into " +
6558 describe(StackSizeSec
) +
6559 " while trying to extract a stack size entry");
6563 uint64_t SymValue
= Resolver(R
.Type
, Offset
, RelocSymValue
,
6564 Data
.getAddress(&Offset
), R
.Addend
.value_or(0));
6565 this->printFunctionStackSize(SymValue
, FunctionSec
, StackSizeSec
, Data
,
6569 template <class ELFT
>
6570 void ELFDumper
<ELFT
>::printNonRelocatableStackSizes(
6571 std::function
<void()> PrintHeader
) {
6572 // This function ignores potentially erroneous input, unless it is directly
6573 // related to stack size reporting.
6574 for (const Elf_Shdr
&Sec
: cantFail(Obj
.sections())) {
6575 if (this->getPrintableSectionName(Sec
) != ".stack_sizes")
6578 ArrayRef
<uint8_t> Contents
=
6579 unwrapOrError(this->FileName
, Obj
.getSectionContents(Sec
));
6580 DataExtractor
Data(Contents
, Obj
.isLE(), sizeof(Elf_Addr
));
6581 uint64_t Offset
= 0;
6582 while (Offset
< Contents
.size()) {
6583 // The function address is followed by a ULEB representing the stack
6584 // size. Check for an extra byte before we try to process the entry.
6585 if (!Data
.isValidOffsetForDataOfSize(Offset
, sizeof(Elf_Addr
) + 1)) {
6586 reportUniqueWarning(
6588 " ended while trying to extract a stack size entry");
6591 uint64_t SymValue
= Data
.getAddress(&Offset
);
6592 if (!printFunctionStackSize(SymValue
, /*FunctionSec=*/std::nullopt
, Sec
,
6599 template <class ELFT
>
6600 void ELFDumper
<ELFT
>::printRelocatableStackSizes(
6601 std::function
<void()> PrintHeader
) {
6602 // Build a map between stack size sections and their corresponding relocation
6604 auto IsMatch
= [&](const Elf_Shdr
&Sec
) -> bool {
6605 StringRef SectionName
;
6606 if (Expected
<StringRef
> NameOrErr
= Obj
.getSectionName(Sec
))
6607 SectionName
= *NameOrErr
;
6609 consumeError(NameOrErr
.takeError());
6611 return SectionName
== ".stack_sizes";
6614 Expected
<MapVector
<const Elf_Shdr
*, const Elf_Shdr
*>>
6615 StackSizeRelocMapOrErr
= Obj
.getSectionAndRelocations(IsMatch
);
6616 if (!StackSizeRelocMapOrErr
) {
6617 reportUniqueWarning("unable to get stack size map section(s): " +
6618 toString(StackSizeRelocMapOrErr
.takeError()));
6622 for (const auto &StackSizeMapEntry
: *StackSizeRelocMapOrErr
) {
6624 const Elf_Shdr
*StackSizesELFSec
= StackSizeMapEntry
.first
;
6625 const Elf_Shdr
*RelocSec
= StackSizeMapEntry
.second
;
6627 // Warn about stack size sections without a relocation section.
6629 reportWarning(createError(".stack_sizes (" + describe(*StackSizesELFSec
) +
6630 ") does not have a corresponding "
6631 "relocation section"),
6636 // A .stack_sizes section header's sh_link field is supposed to point
6637 // to the section that contains the functions whose stack sizes are
6639 const Elf_Shdr
*FunctionSec
= unwrapOrError(
6640 this->FileName
, Obj
.getSection(StackSizesELFSec
->sh_link
));
6642 SupportsRelocation IsSupportedFn
;
6643 RelocationResolver Resolver
;
6644 std::tie(IsSupportedFn
, Resolver
) = getRelocationResolver(this->ObjF
);
6645 ArrayRef
<uint8_t> Contents
=
6646 unwrapOrError(this->FileName
, Obj
.getSectionContents(*StackSizesELFSec
));
6647 DataExtractor
Data(Contents
, Obj
.isLE(), sizeof(Elf_Addr
));
6649 forEachRelocationDo(
6650 *RelocSec
, /*RawRelr=*/false,
6651 [&](const Relocation
<ELFT
> &R
, unsigned Ndx
, const Elf_Shdr
&Sec
,
6652 const Elf_Shdr
*SymTab
) {
6653 if (!IsSupportedFn
|| !IsSupportedFn(R
.Type
)) {
6654 reportUniqueWarning(
6655 describe(*RelocSec
) +
6656 " contains an unsupported relocation with index " + Twine(Ndx
) +
6657 ": " + Obj
.getRelocationTypeName(R
.Type
));
6661 this->printStackSize(R
, *RelocSec
, Ndx
, SymTab
, FunctionSec
,
6662 *StackSizesELFSec
, Resolver
, Data
);
6664 [](const Elf_Relr
&) {
6665 llvm_unreachable("can't get here, because we only support "
6666 "SHT_REL/SHT_RELA sections");
6671 template <class ELFT
>
6672 void GNUELFDumper
<ELFT
>::printStackSizes() {
6673 bool HeaderHasBeenPrinted
= false;
6674 auto PrintHeader
= [&]() {
6675 if (HeaderHasBeenPrinted
)
6677 OS
<< "\nStack Sizes:\n";
6681 OS
<< "Functions\n";
6682 HeaderHasBeenPrinted
= true;
6685 // For non-relocatable objects, look directly for sections whose name starts
6686 // with .stack_sizes and process the contents.
6687 if (this->Obj
.getHeader().e_type
== ELF::ET_REL
)
6688 this->printRelocatableStackSizes(PrintHeader
);
6690 this->printNonRelocatableStackSizes(PrintHeader
);
6693 template <class ELFT
>
6694 void GNUELFDumper
<ELFT
>::printMipsGOT(const MipsGOTParser
<ELFT
> &Parser
) {
6695 size_t Bias
= ELFT::Is64Bits
? 8 : 0;
6696 auto PrintEntry
= [&](const Elf_Addr
*E
, StringRef Purpose
) {
6698 OS
<< format_hex_no_prefix(Parser
.getGotAddress(E
), 8 + Bias
);
6699 OS
.PadToColumn(11 + Bias
);
6700 OS
<< format_decimal(Parser
.getGotOffset(E
), 6) << "(gp)";
6701 OS
.PadToColumn(22 + Bias
);
6702 OS
<< format_hex_no_prefix(*E
, 8 + Bias
);
6703 OS
.PadToColumn(31 + 2 * Bias
);
6704 OS
<< Purpose
<< "\n";
6707 OS
<< (Parser
.IsStatic
? "Static GOT:\n" : "Primary GOT:\n");
6708 OS
<< " Canonical gp value: "
6709 << format_hex_no_prefix(Parser
.getGp(), 8 + Bias
) << "\n\n";
6711 OS
<< " Reserved entries:\n";
6713 OS
<< " Address Access Initial Purpose\n";
6715 OS
<< " Address Access Initial Purpose\n";
6716 PrintEntry(Parser
.getGotLazyResolver(), "Lazy resolver");
6717 if (Parser
.getGotModulePointer())
6718 PrintEntry(Parser
.getGotModulePointer(), "Module pointer (GNU extension)");
6720 if (!Parser
.getLocalEntries().empty()) {
6722 OS
<< " Local entries:\n";
6724 OS
<< " Address Access Initial\n";
6726 OS
<< " Address Access Initial\n";
6727 for (auto &E
: Parser
.getLocalEntries())
6731 if (Parser
.IsStatic
)
6734 if (!Parser
.getGlobalEntries().empty()) {
6736 OS
<< " Global entries:\n";
6738 OS
<< " Address Access Initial Sym.Val."
6739 << " Type Ndx Name\n";
6741 OS
<< " Address Access Initial Sym.Val. Type Ndx Name\n";
6743 DataRegion
<Elf_Word
> ShndxTable(
6744 (const Elf_Word
*)this->DynSymTabShndxRegion
.Addr
, this->Obj
.end());
6745 for (auto &E
: Parser
.getGlobalEntries()) {
6746 const Elf_Sym
&Sym
= *Parser
.getGotSym(&E
);
6747 const Elf_Sym
&FirstSym
= this->dynamic_symbols()[0];
6748 std::string SymName
= this->getFullSymbolName(
6749 Sym
, &Sym
- &FirstSym
, ShndxTable
, this->DynamicStringTable
, false);
6752 OS
<< to_string(format_hex_no_prefix(Parser
.getGotAddress(&E
), 8 + Bias
));
6753 OS
.PadToColumn(11 + Bias
);
6754 OS
<< to_string(format_decimal(Parser
.getGotOffset(&E
), 6)) + "(gp)";
6755 OS
.PadToColumn(22 + Bias
);
6756 OS
<< to_string(format_hex_no_prefix(E
, 8 + Bias
));
6757 OS
.PadToColumn(31 + 2 * Bias
);
6758 OS
<< to_string(format_hex_no_prefix(Sym
.st_value
, 8 + Bias
));
6759 OS
.PadToColumn(40 + 3 * Bias
);
6760 OS
<< enumToString(Sym
.getType(), ArrayRef(ElfSymbolTypes
));
6761 OS
.PadToColumn(48 + 3 * Bias
);
6762 OS
<< getSymbolSectionNdx(Sym
, &Sym
- this->dynamic_symbols().begin(),
6764 OS
.PadToColumn(52 + 3 * Bias
);
6765 OS
<< SymName
<< "\n";
6769 if (!Parser
.getOtherEntries().empty())
6770 OS
<< "\n Number of TLS and multi-GOT entries "
6771 << Parser
.getOtherEntries().size() << "\n";
6774 template <class ELFT
>
6775 void GNUELFDumper
<ELFT
>::printMipsPLT(const MipsGOTParser
<ELFT
> &Parser
) {
6776 size_t Bias
= ELFT::Is64Bits
? 8 : 0;
6777 auto PrintEntry
= [&](const Elf_Addr
*E
, StringRef Purpose
) {
6779 OS
<< format_hex_no_prefix(Parser
.getPltAddress(E
), 8 + Bias
);
6780 OS
.PadToColumn(11 + Bias
);
6781 OS
<< format_hex_no_prefix(*E
, 8 + Bias
);
6782 OS
.PadToColumn(20 + 2 * Bias
);
6783 OS
<< Purpose
<< "\n";
6786 OS
<< "PLT GOT:\n\n";
6788 OS
<< " Reserved entries:\n";
6789 OS
<< " Address Initial Purpose\n";
6790 PrintEntry(Parser
.getPltLazyResolver(), "PLT lazy resolver");
6791 if (Parser
.getPltModulePointer())
6792 PrintEntry(Parser
.getPltModulePointer(), "Module pointer");
6794 if (!Parser
.getPltEntries().empty()) {
6796 OS
<< " Entries:\n";
6797 OS
<< " Address Initial Sym.Val. Type Ndx Name\n";
6798 DataRegion
<Elf_Word
> ShndxTable(
6799 (const Elf_Word
*)this->DynSymTabShndxRegion
.Addr
, this->Obj
.end());
6800 for (auto &E
: Parser
.getPltEntries()) {
6801 const Elf_Sym
&Sym
= *Parser
.getPltSym(&E
);
6802 const Elf_Sym
&FirstSym
= *cantFail(
6803 this->Obj
.template getEntry
<Elf_Sym
>(*Parser
.getPltSymTable(), 0));
6804 std::string SymName
= this->getFullSymbolName(
6805 Sym
, &Sym
- &FirstSym
, ShndxTable
, this->DynamicStringTable
, false);
6808 OS
<< to_string(format_hex_no_prefix(Parser
.getPltAddress(&E
), 8 + Bias
));
6809 OS
.PadToColumn(11 + Bias
);
6810 OS
<< to_string(format_hex_no_prefix(E
, 8 + Bias
));
6811 OS
.PadToColumn(20 + 2 * Bias
);
6812 OS
<< to_string(format_hex_no_prefix(Sym
.st_value
, 8 + Bias
));
6813 OS
.PadToColumn(29 + 3 * Bias
);
6814 OS
<< enumToString(Sym
.getType(), ArrayRef(ElfSymbolTypes
));
6815 OS
.PadToColumn(37 + 3 * Bias
);
6816 OS
<< getSymbolSectionNdx(Sym
, &Sym
- this->dynamic_symbols().begin(),
6818 OS
.PadToColumn(41 + 3 * Bias
);
6819 OS
<< SymName
<< "\n";
6824 template <class ELFT
>
6825 Expected
<const Elf_Mips_ABIFlags
<ELFT
> *>
6826 getMipsAbiFlagsSection(const ELFDumper
<ELFT
> &Dumper
) {
6827 const typename
ELFT::Shdr
*Sec
= Dumper
.findSectionByName(".MIPS.abiflags");
6831 constexpr StringRef ErrPrefix
= "unable to read the .MIPS.abiflags section: ";
6832 Expected
<ArrayRef
<uint8_t>> DataOrErr
=
6833 Dumper
.getElfObject().getELFFile().getSectionContents(*Sec
);
6835 return createError(ErrPrefix
+ toString(DataOrErr
.takeError()));
6837 if (DataOrErr
->size() != sizeof(Elf_Mips_ABIFlags
<ELFT
>))
6838 return createError(ErrPrefix
+ "it has a wrong size (" +
6839 Twine(DataOrErr
->size()) + ")");
6840 return reinterpret_cast<const Elf_Mips_ABIFlags
<ELFT
> *>(DataOrErr
->data());
6843 template <class ELFT
> void GNUELFDumper
<ELFT
>::printMipsABIFlags() {
6844 const Elf_Mips_ABIFlags
<ELFT
> *Flags
= nullptr;
6845 if (Expected
<const Elf_Mips_ABIFlags
<ELFT
> *> SecOrErr
=
6846 getMipsAbiFlagsSection(*this))
6849 this->reportUniqueWarning(SecOrErr
.takeError());
6853 OS
<< "MIPS ABI Flags Version: " << Flags
->version
<< "\n\n";
6854 OS
<< "ISA: MIPS" << int(Flags
->isa_level
);
6855 if (Flags
->isa_rev
> 1)
6856 OS
<< "r" << int(Flags
->isa_rev
);
6858 OS
<< "GPR size: " << getMipsRegisterSize(Flags
->gpr_size
) << "\n";
6859 OS
<< "CPR1 size: " << getMipsRegisterSize(Flags
->cpr1_size
) << "\n";
6860 OS
<< "CPR2 size: " << getMipsRegisterSize(Flags
->cpr2_size
) << "\n";
6861 OS
<< "FP ABI: " << enumToString(Flags
->fp_abi
, ArrayRef(ElfMipsFpABIType
))
6863 OS
<< "ISA Extension: "
6864 << enumToString(Flags
->isa_ext
, ArrayRef(ElfMipsISAExtType
)) << "\n";
6865 if (Flags
->ases
== 0)
6866 OS
<< "ASEs: None\n";
6868 // FIXME: Print each flag on a separate line.
6869 OS
<< "ASEs: " << printFlags(Flags
->ases
, ArrayRef(ElfMipsASEFlags
))
6871 OS
<< "FLAGS 1: " << format_hex_no_prefix(Flags
->flags1
, 8, false) << "\n";
6872 OS
<< "FLAGS 2: " << format_hex_no_prefix(Flags
->flags2
, 8, false) << "\n";
6876 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printFileHeaders() {
6877 const Elf_Ehdr
&E
= this->Obj
.getHeader();
6879 DictScope
D(W
, "ElfHeader");
6881 DictScope
D(W
, "Ident");
6882 W
.printBinary("Magic",
6883 ArrayRef
<unsigned char>(E
.e_ident
).slice(ELF::EI_MAG0
, 4));
6884 W
.printEnum("Class", E
.e_ident
[ELF::EI_CLASS
], ArrayRef(ElfClass
));
6885 W
.printEnum("DataEncoding", E
.e_ident
[ELF::EI_DATA
],
6886 ArrayRef(ElfDataEncoding
));
6887 W
.printNumber("FileVersion", E
.e_ident
[ELF::EI_VERSION
]);
6889 auto OSABI
= ArrayRef(ElfOSABI
);
6890 if (E
.e_ident
[ELF::EI_OSABI
] >= ELF::ELFOSABI_FIRST_ARCH
&&
6891 E
.e_ident
[ELF::EI_OSABI
] <= ELF::ELFOSABI_LAST_ARCH
) {
6892 switch (E
.e_machine
) {
6893 case ELF::EM_AMDGPU
:
6894 OSABI
= ArrayRef(AMDGPUElfOSABI
);
6897 OSABI
= ArrayRef(ARMElfOSABI
);
6899 case ELF::EM_TI_C6000
:
6900 OSABI
= ArrayRef(C6000ElfOSABI
);
6904 W
.printEnum("OS/ABI", E
.e_ident
[ELF::EI_OSABI
], OSABI
);
6905 W
.printNumber("ABIVersion", E
.e_ident
[ELF::EI_ABIVERSION
]);
6906 W
.printBinary("Unused",
6907 ArrayRef
<unsigned char>(E
.e_ident
).slice(ELF::EI_PAD
));
6910 std::string TypeStr
;
6911 if (const EnumEntry
<unsigned> *Ent
= getObjectFileEnumEntry(E
.e_type
)) {
6912 TypeStr
= Ent
->Name
.str();
6914 if (E
.e_type
>= ET_LOPROC
)
6915 TypeStr
= "Processor Specific";
6916 else if (E
.e_type
>= ET_LOOS
)
6917 TypeStr
= "OS Specific";
6919 TypeStr
= "Unknown";
6921 W
.printString("Type", TypeStr
+ " (0x" + utohexstr(E
.e_type
) + ")");
6923 W
.printEnum("Machine", E
.e_machine
, ArrayRef(ElfMachineType
));
6924 W
.printNumber("Version", E
.e_version
);
6925 W
.printHex("Entry", E
.e_entry
);
6926 W
.printHex("ProgramHeaderOffset", E
.e_phoff
);
6927 W
.printHex("SectionHeaderOffset", E
.e_shoff
);
6928 if (E
.e_machine
== EM_MIPS
)
6929 W
.printFlags("Flags", E
.e_flags
, ArrayRef(ElfHeaderMipsFlags
),
6930 unsigned(ELF::EF_MIPS_ARCH
), unsigned(ELF::EF_MIPS_ABI
),
6931 unsigned(ELF::EF_MIPS_MACH
));
6932 else if (E
.e_machine
== EM_AMDGPU
) {
6933 switch (E
.e_ident
[ELF::EI_ABIVERSION
]) {
6935 W
.printHex("Flags", E
.e_flags
);
6938 // ELFOSABI_AMDGPU_PAL, ELFOSABI_AMDGPU_MESA3D support *_V3 flags.
6940 case ELF::ELFABIVERSION_AMDGPU_HSA_V3
:
6941 W
.printFlags("Flags", E
.e_flags
,
6942 ArrayRef(ElfHeaderAMDGPUFlagsABIVersion3
),
6943 unsigned(ELF::EF_AMDGPU_MACH
));
6945 case ELF::ELFABIVERSION_AMDGPU_HSA_V4
:
6946 case ELF::ELFABIVERSION_AMDGPU_HSA_V5
:
6947 W
.printFlags("Flags", E
.e_flags
,
6948 ArrayRef(ElfHeaderAMDGPUFlagsABIVersion4
),
6949 unsigned(ELF::EF_AMDGPU_MACH
),
6950 unsigned(ELF::EF_AMDGPU_FEATURE_XNACK_V4
),
6951 unsigned(ELF::EF_AMDGPU_FEATURE_SRAMECC_V4
));
6954 } else if (E
.e_machine
== EM_RISCV
)
6955 W
.printFlags("Flags", E
.e_flags
, ArrayRef(ElfHeaderRISCVFlags
));
6956 else if (E
.e_machine
== EM_AVR
)
6957 W
.printFlags("Flags", E
.e_flags
, ArrayRef(ElfHeaderAVRFlags
),
6958 unsigned(ELF::EF_AVR_ARCH_MASK
));
6959 else if (E
.e_machine
== EM_LOONGARCH
)
6960 W
.printFlags("Flags", E
.e_flags
, ArrayRef(ElfHeaderLoongArchFlags
),
6961 unsigned(ELF::EF_LOONGARCH_ABI_MODIFIER_MASK
),
6962 unsigned(ELF::EF_LOONGARCH_OBJABI_MASK
));
6963 else if (E
.e_machine
== EM_XTENSA
)
6964 W
.printFlags("Flags", E
.e_flags
, ArrayRef(ElfHeaderXtensaFlags
),
6965 unsigned(ELF::EF_XTENSA_MACH
));
6966 else if (E
.e_machine
== EM_CUDA
)
6967 W
.printFlags("Flags", E
.e_flags
, ArrayRef(ElfHeaderNVPTXFlags
),
6968 unsigned(ELF::EF_CUDA_SM
));
6970 W
.printFlags("Flags", E
.e_flags
);
6971 W
.printNumber("HeaderSize", E
.e_ehsize
);
6972 W
.printNumber("ProgramHeaderEntrySize", E
.e_phentsize
);
6973 W
.printNumber("ProgramHeaderCount", E
.e_phnum
);
6974 W
.printNumber("SectionHeaderEntrySize", E
.e_shentsize
);
6975 W
.printString("SectionHeaderCount",
6976 getSectionHeadersNumString(this->Obj
, this->FileName
));
6977 W
.printString("StringTableSectionIndex",
6978 getSectionHeaderTableIndexString(this->Obj
, this->FileName
));
6982 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printGroupSections() {
6983 DictScope
Lists(W
, "Groups");
6984 std::vector
<GroupSection
> V
= this->getGroups();
6985 DenseMap
<uint64_t, const GroupSection
*> Map
= mapSectionsToGroups(V
);
6986 for (const GroupSection
&G
: V
) {
6987 DictScope
D(W
, "Group");
6988 W
.printNumber("Name", G
.Name
, G
.ShName
);
6989 W
.printNumber("Index", G
.Index
);
6990 W
.printNumber("Link", G
.Link
);
6991 W
.printNumber("Info", G
.Info
);
6992 W
.printHex("Type", getGroupType(G
.Type
), G
.Type
);
6993 W
.printString("Signature", G
.Signature
);
6995 ListScope
L(W
, getGroupSectionHeaderName());
6996 for (const GroupMember
&GM
: G
.Members
) {
6997 const GroupSection
*MainGroup
= Map
[GM
.Index
];
6998 if (MainGroup
!= &G
)
6999 this->reportUniqueWarning(
7000 "section with index " + Twine(GM
.Index
) +
7001 ", included in the group section with index " +
7002 Twine(MainGroup
->Index
) +
7003 ", was also found in the group section with index " +
7005 printSectionGroupMembers(GM
.Name
, GM
.Index
);
7010 printEmptyGroupMessage();
7013 template <class ELFT
>
7014 std::string LLVMELFDumper
<ELFT
>::getGroupSectionHeaderName() const {
7015 return "Section(s) in group";
7018 template <class ELFT
>
7019 void LLVMELFDumper
<ELFT
>::printSectionGroupMembers(StringRef Name
,
7020 uint64_t Idx
) const {
7021 W
.startLine() << Name
<< " (" << Idx
<< ")\n";
7024 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printRelocations() {
7025 ListScope
D(W
, "Relocations");
7027 for (const Elf_Shdr
&Sec
: cantFail(this->Obj
.sections())) {
7028 if (!isRelocationSec
<ELFT
>(Sec
, this->Obj
.getHeader()))
7031 StringRef Name
= this->getPrintableSectionName(Sec
);
7032 unsigned SecNdx
= &Sec
- &cantFail(this->Obj
.sections()).front();
7033 printRelocationSectionInfo(Sec
, Name
, SecNdx
);
7037 template <class ELFT
>
7038 void LLVMELFDumper
<ELFT
>::printRelrReloc(const Elf_Relr
&R
) {
7039 W
.startLine() << W
.hex(R
) << "\n";
7042 template <class ELFT
>
7043 void LLVMELFDumper
<ELFT
>::printExpandedRelRelaReloc(const Relocation
<ELFT
> &R
,
7044 StringRef SymbolName
,
7045 StringRef RelocName
) {
7046 DictScope
Group(W
, "Relocation");
7047 W
.printHex("Offset", R
.Offset
);
7048 W
.printNumber("Type", RelocName
, R
.Type
);
7049 W
.printNumber("Symbol", !SymbolName
.empty() ? SymbolName
: "-", R
.Symbol
);
7051 W
.printHex("Addend", (uintX_t
)*R
.Addend
);
7054 template <class ELFT
>
7055 void LLVMELFDumper
<ELFT
>::printDefaultRelRelaReloc(const Relocation
<ELFT
> &R
,
7056 StringRef SymbolName
,
7057 StringRef RelocName
) {
7058 raw_ostream
&OS
= W
.startLine();
7059 OS
<< W
.hex(R
.Offset
) << " " << RelocName
<< " "
7060 << (!SymbolName
.empty() ? SymbolName
: "-");
7062 OS
<< " " << W
.hex((uintX_t
)*R
.Addend
);
7066 template <class ELFT
>
7067 void LLVMELFDumper
<ELFT
>::printRelocationSectionInfo(const Elf_Shdr
&Sec
,
7069 const unsigned SecNdx
) {
7070 DictScope
D(W
, (Twine("Section (") + Twine(SecNdx
) + ") " + Name
).str());
7071 this->printRelocationsHelper(Sec
);
7074 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printEmptyGroupMessage() const {
7075 W
.startLine() << "There are no group sections in the file.\n";
7078 template <class ELFT
>
7079 void LLVMELFDumper
<ELFT
>::printRelRelaReloc(const Relocation
<ELFT
> &R
,
7080 const RelSymbol
<ELFT
> &RelSym
) {
7081 StringRef SymbolName
= RelSym
.Name
;
7082 if (RelSym
.Sym
&& RelSym
.Name
.empty())
7083 SymbolName
= "<null>";
7084 SmallString
<32> RelocName
;
7085 this->Obj
.getRelocationTypeName(R
.Type
, RelocName
);
7087 if (opts::ExpandRelocs
) {
7088 printExpandedRelRelaReloc(R
, SymbolName
, RelocName
);
7090 printDefaultRelRelaReloc(R
, SymbolName
, RelocName
);
7094 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printSectionHeaders() {
7095 ListScope
SectionsD(W
, "Sections");
7097 int SectionIndex
= -1;
7098 std::vector
<EnumEntry
<unsigned>> FlagsList
=
7099 getSectionFlagsForTarget(this->Obj
.getHeader().e_ident
[ELF::EI_OSABI
],
7100 this->Obj
.getHeader().e_machine
);
7101 for (const Elf_Shdr
&Sec
: cantFail(this->Obj
.sections())) {
7102 DictScope
SectionD(W
, "Section");
7103 W
.printNumber("Index", ++SectionIndex
);
7104 W
.printNumber("Name", this->getPrintableSectionName(Sec
), Sec
.sh_name
);
7106 object::getELFSectionTypeName(this->Obj
.getHeader().e_machine
,
7109 W
.printFlags("Flags", Sec
.sh_flags
, ArrayRef(FlagsList
));
7110 W
.printHex("Address", Sec
.sh_addr
);
7111 W
.printHex("Offset", Sec
.sh_offset
);
7112 W
.printNumber("Size", Sec
.sh_size
);
7113 W
.printNumber("Link", Sec
.sh_link
);
7114 W
.printNumber("Info", Sec
.sh_info
);
7115 W
.printNumber("AddressAlignment", Sec
.sh_addralign
);
7116 W
.printNumber("EntrySize", Sec
.sh_entsize
);
7118 if (opts::SectionRelocations
) {
7119 ListScope
D(W
, "Relocations");
7120 this->printRelocationsHelper(Sec
);
7123 if (opts::SectionSymbols
) {
7124 ListScope
D(W
, "Symbols");
7125 if (this->DotSymtabSec
) {
7126 StringRef StrTable
= unwrapOrError(
7128 this->Obj
.getStringTableForSymtab(*this->DotSymtabSec
));
7129 ArrayRef
<Elf_Word
> ShndxTable
= this->getShndxTable(this->DotSymtabSec
);
7131 typename
ELFT::SymRange Symbols
= unwrapOrError(
7132 this->FileName
, this->Obj
.symbols(this->DotSymtabSec
));
7133 for (const Elf_Sym
&Sym
: Symbols
) {
7134 const Elf_Shdr
*SymSec
= unwrapOrError(
7136 this->Obj
.getSection(Sym
, this->DotSymtabSec
, ShndxTable
));
7138 printSymbol(Sym
, &Sym
- &Symbols
[0], ShndxTable
, StrTable
, false,
7139 /*NonVisibilityBitsUsed=*/false,
7140 /*ExtraSymInfo=*/false);
7145 if (opts::SectionData
&& Sec
.sh_type
!= ELF::SHT_NOBITS
) {
7146 ArrayRef
<uint8_t> Data
=
7147 unwrapOrError(this->FileName
, this->Obj
.getSectionContents(Sec
));
7150 StringRef(reinterpret_cast<const char *>(Data
.data()), Data
.size()));
7155 template <class ELFT
>
7156 void LLVMELFDumper
<ELFT
>::printSymbolSection(
7157 const Elf_Sym
&Symbol
, unsigned SymIndex
,
7158 DataRegion
<Elf_Word
> ShndxTable
) const {
7159 auto GetSectionSpecialType
= [&]() -> std::optional
<StringRef
> {
7160 if (Symbol
.isUndefined())
7161 return StringRef("Undefined");
7162 if (Symbol
.isProcessorSpecific())
7163 return StringRef("Processor Specific");
7164 if (Symbol
.isOSSpecific())
7165 return StringRef("Operating System Specific");
7166 if (Symbol
.isAbsolute())
7167 return StringRef("Absolute");
7168 if (Symbol
.isCommon())
7169 return StringRef("Common");
7170 if (Symbol
.isReserved() && Symbol
.st_shndx
!= SHN_XINDEX
)
7171 return StringRef("Reserved");
7172 return std::nullopt
;
7175 if (std::optional
<StringRef
> Type
= GetSectionSpecialType()) {
7176 W
.printHex("Section", *Type
, Symbol
.st_shndx
);
7180 Expected
<unsigned> SectionIndex
=
7181 this->getSymbolSectionIndex(Symbol
, SymIndex
, ShndxTable
);
7182 if (!SectionIndex
) {
7183 assert(Symbol
.st_shndx
== SHN_XINDEX
&&
7184 "getSymbolSectionIndex should only fail due to an invalid "
7185 "SHT_SYMTAB_SHNDX table/reference");
7186 this->reportUniqueWarning(SectionIndex
.takeError());
7187 W
.printHex("Section", "Reserved", SHN_XINDEX
);
7191 Expected
<StringRef
> SectionName
=
7192 this->getSymbolSectionName(Symbol
, *SectionIndex
);
7194 // Don't report an invalid section name if the section headers are missing.
7195 // In such situations, all sections will be "invalid".
7196 if (!this->ObjF
.sections().empty())
7197 this->reportUniqueWarning(SectionName
.takeError());
7199 consumeError(SectionName
.takeError());
7200 W
.printHex("Section", "<?>", *SectionIndex
);
7202 W
.printHex("Section", *SectionName
, *SectionIndex
);
7206 template <class ELFT
>
7207 void LLVMELFDumper
<ELFT
>::printSymbolOtherField(const Elf_Sym
&Symbol
) const {
7208 std::vector
<EnumEntry
<unsigned>> SymOtherFlags
=
7209 this->getOtherFlagsFromSymbol(this->Obj
.getHeader(), Symbol
);
7210 W
.printFlags("Other", Symbol
.st_other
, ArrayRef(SymOtherFlags
), 0x3u
);
7213 template <class ELFT
>
7214 void LLVMELFDumper
<ELFT
>::printZeroSymbolOtherField(
7215 const Elf_Sym
&Symbol
) const {
7216 assert(Symbol
.st_other
== 0 && "non-zero Other Field");
7217 // Usually st_other flag is zero. Do not pollute the output
7218 // by flags enumeration in that case.
7219 W
.printNumber("Other", 0);
7222 template <class ELFT
>
7223 void LLVMELFDumper
<ELFT
>::printSymbol(const Elf_Sym
&Symbol
, unsigned SymIndex
,
7224 DataRegion
<Elf_Word
> ShndxTable
,
7225 std::optional
<StringRef
> StrTable
,
7227 bool /*NonVisibilityBitsUsed*/,
7228 bool /*ExtraSymInfo*/) const {
7229 std::string FullSymbolName
= this->getFullSymbolName(
7230 Symbol
, SymIndex
, ShndxTable
, StrTable
, IsDynamic
);
7231 unsigned char SymbolType
= Symbol
.getType();
7233 DictScope
D(W
, "Symbol");
7234 W
.printNumber("Name", FullSymbolName
, Symbol
.st_name
);
7235 W
.printHex("Value", Symbol
.st_value
);
7236 W
.printNumber("Size", Symbol
.st_size
);
7237 W
.printEnum("Binding", Symbol
.getBinding(), ArrayRef(ElfSymbolBindings
));
7238 if (this->Obj
.getHeader().e_machine
== ELF::EM_AMDGPU
&&
7239 SymbolType
>= ELF::STT_LOOS
&& SymbolType
< ELF::STT_HIOS
)
7240 W
.printEnum("Type", SymbolType
, ArrayRef(AMDGPUSymbolTypes
));
7242 W
.printEnum("Type", SymbolType
, ArrayRef(ElfSymbolTypes
));
7243 if (Symbol
.st_other
== 0)
7244 printZeroSymbolOtherField(Symbol
);
7246 printSymbolOtherField(Symbol
);
7247 printSymbolSection(Symbol
, SymIndex
, ShndxTable
);
7250 template <class ELFT
>
7251 void LLVMELFDumper
<ELFT
>::printSymbols(bool PrintSymbols
,
7252 bool PrintDynamicSymbols
,
7253 bool ExtraSymInfo
) {
7255 ListScope
Group(W
, "Symbols");
7256 this->printSymbolsHelper(false, ExtraSymInfo
);
7258 if (PrintDynamicSymbols
) {
7259 ListScope
Group(W
, "DynamicSymbols");
7260 this->printSymbolsHelper(true, ExtraSymInfo
);
7264 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printDynamicTable() {
7265 Elf_Dyn_Range Table
= this->dynamic_table();
7269 W
.startLine() << "DynamicSection [ (" << Table
.size() << " entries)\n";
7271 size_t MaxTagSize
= getMaxDynamicTagSize(this->Obj
, Table
);
7272 // The "Name/Value" column should be indented from the "Type" column by N
7273 // spaces, where N = MaxTagSize - length of "Type" (4) + trailing
7275 W
.startLine() << " Tag" << std::string(ELFT::Is64Bits
? 16 : 8, ' ')
7276 << "Type" << std::string(MaxTagSize
- 3, ' ') << "Name/Value\n";
7278 std::string ValueFmt
= "%-" + std::to_string(MaxTagSize
) + "s ";
7279 for (auto Entry
: Table
) {
7280 uintX_t Tag
= Entry
.getTag();
7281 std::string Value
= this->getDynamicEntry(Tag
, Entry
.getVal());
7282 W
.startLine() << " " << format_hex(Tag
, ELFT::Is64Bits
? 18 : 10, true)
7284 << format(ValueFmt
.c_str(),
7285 this->Obj
.getDynamicTagAsString(Tag
).c_str())
7288 W
.startLine() << "]\n";
7291 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printDynamicRelocations() {
7292 W
.startLine() << "Dynamic Relocations {\n";
7294 this->printDynamicRelocationsHelper();
7296 W
.startLine() << "}\n";
7299 template <class ELFT
>
7300 void LLVMELFDumper
<ELFT
>::printProgramHeaders(
7301 bool PrintProgramHeaders
, cl::boolOrDefault PrintSectionMapping
) {
7302 if (PrintProgramHeaders
)
7303 printProgramHeaders();
7304 if (PrintSectionMapping
== cl::BOU_TRUE
)
7305 printSectionMapping();
7308 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printProgramHeaders() {
7309 ListScope
L(W
, "ProgramHeaders");
7311 Expected
<ArrayRef
<Elf_Phdr
>> PhdrsOrErr
= this->Obj
.program_headers();
7313 this->reportUniqueWarning("unable to dump program headers: " +
7314 toString(PhdrsOrErr
.takeError()));
7318 for (const Elf_Phdr
&Phdr
: *PhdrsOrErr
) {
7319 DictScope
P(W
, "ProgramHeader");
7321 segmentTypeToString(this->Obj
.getHeader().e_machine
, Phdr
.p_type
);
7323 W
.printHex("Type", Type
.empty() ? "Unknown" : Type
, Phdr
.p_type
);
7324 W
.printHex("Offset", Phdr
.p_offset
);
7325 W
.printHex("VirtualAddress", Phdr
.p_vaddr
);
7326 W
.printHex("PhysicalAddress", Phdr
.p_paddr
);
7327 W
.printNumber("FileSize", Phdr
.p_filesz
);
7328 W
.printNumber("MemSize", Phdr
.p_memsz
);
7329 W
.printFlags("Flags", Phdr
.p_flags
, ArrayRef(ElfSegmentFlags
));
7330 W
.printNumber("Alignment", Phdr
.p_align
);
7334 template <class ELFT
>
7335 void LLVMELFDumper
<ELFT
>::printVersionSymbolSection(const Elf_Shdr
*Sec
) {
7336 ListScope
SS(W
, "VersionSymbols");
7341 ArrayRef
<Elf_Sym
> Syms
;
7342 const Elf_Shdr
*SymTabSec
;
7343 Expected
<ArrayRef
<Elf_Versym
>> VerTableOrErr
=
7344 this->getVersionTable(*Sec
, &Syms
, &StrTable
, &SymTabSec
);
7345 if (!VerTableOrErr
) {
7346 this->reportUniqueWarning(VerTableOrErr
.takeError());
7350 if (StrTable
.empty() || Syms
.empty() || Syms
.size() != VerTableOrErr
->size())
7353 ArrayRef
<Elf_Word
> ShNdxTable
= this->getShndxTable(SymTabSec
);
7354 for (size_t I
= 0, E
= Syms
.size(); I
< E
; ++I
) {
7355 DictScope
S(W
, "Symbol");
7356 W
.printNumber("Version", (*VerTableOrErr
)[I
].vs_index
& VERSYM_VERSION
);
7357 W
.printString("Name",
7358 this->getFullSymbolName(Syms
[I
], I
, ShNdxTable
, StrTable
,
7359 /*IsDynamic=*/true));
7363 const EnumEntry
<unsigned> SymVersionFlags
[] = {
7364 {"Base", "BASE", VER_FLG_BASE
},
7365 {"Weak", "WEAK", VER_FLG_WEAK
},
7366 {"Info", "INFO", VER_FLG_INFO
}};
7368 template <class ELFT
>
7369 void LLVMELFDumper
<ELFT
>::printVersionDefinitionSection(const Elf_Shdr
*Sec
) {
7370 ListScope
SD(W
, "VersionDefinitions");
7374 Expected
<std::vector
<VerDef
>> V
= this->Obj
.getVersionDefinitions(*Sec
);
7376 this->reportUniqueWarning(V
.takeError());
7380 for (const VerDef
&D
: *V
) {
7381 DictScope
Def(W
, "Definition");
7382 W
.printNumber("Version", D
.Version
);
7383 W
.printFlags("Flags", D
.Flags
, ArrayRef(SymVersionFlags
));
7384 W
.printNumber("Index", D
.Ndx
);
7385 W
.printNumber("Hash", D
.Hash
);
7386 W
.printString("Name", D
.Name
.c_str());
7388 "Predecessors", D
.AuxV
,
7389 [](raw_ostream
&OS
, const VerdAux
&Aux
) { OS
<< Aux
.Name
.c_str(); });
7393 template <class ELFT
>
7394 void LLVMELFDumper
<ELFT
>::printVersionDependencySection(const Elf_Shdr
*Sec
) {
7395 ListScope
SD(W
, "VersionRequirements");
7399 Expected
<std::vector
<VerNeed
>> V
=
7400 this->Obj
.getVersionDependencies(*Sec
, this->WarningHandler
);
7402 this->reportUniqueWarning(V
.takeError());
7406 for (const VerNeed
&VN
: *V
) {
7407 DictScope
Entry(W
, "Dependency");
7408 W
.printNumber("Version", VN
.Version
);
7409 W
.printNumber("Count", VN
.Cnt
);
7410 W
.printString("FileName", VN
.File
.c_str());
7412 ListScope
L(W
, "Entries");
7413 for (const VernAux
&Aux
: VN
.AuxV
) {
7414 DictScope
Entry(W
, "Entry");
7415 W
.printNumber("Hash", Aux
.Hash
);
7416 W
.printFlags("Flags", Aux
.Flags
, ArrayRef(SymVersionFlags
));
7417 W
.printNumber("Index", Aux
.Other
);
7418 W
.printString("Name", Aux
.Name
.c_str());
7423 template <class ELFT
>
7424 void LLVMELFDumper
<ELFT
>::printHashHistogramStats(size_t NBucket
,
7427 ArrayRef
<size_t> Count
,
7429 StringRef HistName
= IsGnu
? "GnuHashHistogram" : "HashHistogram";
7430 StringRef BucketName
= IsGnu
? "Bucket" : "Chain";
7431 StringRef ListName
= IsGnu
? "Buckets" : "Chains";
7432 DictScope
Outer(W
, HistName
);
7433 W
.printNumber("TotalBuckets", NBucket
);
7434 ListScope
Buckets(W
, ListName
);
7435 size_t CumulativeNonZero
= 0;
7436 for (size_t I
= 0; I
< MaxChain
; ++I
) {
7437 CumulativeNonZero
+= Count
[I
] * I
;
7438 DictScope
Bucket(W
, BucketName
);
7439 W
.printNumber("Length", I
);
7440 W
.printNumber("Count", Count
[I
]);
7441 W
.printNumber("Percentage", (float)(Count
[I
] * 100.0) / NBucket
);
7442 W
.printNumber("Coverage", (float)(CumulativeNonZero
* 100.0) / TotalSyms
);
7446 // Returns true if rel/rela section exists, and populates SymbolIndices.
7447 // Otherwise returns false.
7448 template <class ELFT
>
7449 static bool getSymbolIndices(const typename
ELFT::Shdr
*CGRelSection
,
7450 const ELFFile
<ELFT
> &Obj
,
7451 const LLVMELFDumper
<ELFT
> *Dumper
,
7452 SmallVector
<uint32_t, 128> &SymbolIndices
) {
7453 if (!CGRelSection
) {
7454 Dumper
->reportUniqueWarning(
7455 "relocation section for a call graph section doesn't exist");
7459 if (CGRelSection
->sh_type
== SHT_REL
) {
7460 typename
ELFT::RelRange CGProfileRel
;
7461 Expected
<typename
ELFT::RelRange
> CGProfileRelOrError
=
7462 Obj
.rels(*CGRelSection
);
7463 if (!CGProfileRelOrError
) {
7464 Dumper
->reportUniqueWarning("unable to load relocations for "
7465 "SHT_LLVM_CALL_GRAPH_PROFILE section: " +
7466 toString(CGProfileRelOrError
.takeError()));
7470 CGProfileRel
= *CGProfileRelOrError
;
7471 for (const typename
ELFT::Rel
&Rel
: CGProfileRel
)
7472 SymbolIndices
.push_back(Rel
.getSymbol(Obj
.isMips64EL()));
7474 // MC unconditionally produces SHT_REL, but GNU strip/objcopy may convert
7475 // the format to SHT_RELA
7476 // (https://sourceware.org/bugzilla/show_bug.cgi?id=28035)
7477 typename
ELFT::RelaRange CGProfileRela
;
7478 Expected
<typename
ELFT::RelaRange
> CGProfileRelaOrError
=
7479 Obj
.relas(*CGRelSection
);
7480 if (!CGProfileRelaOrError
) {
7481 Dumper
->reportUniqueWarning("unable to load relocations for "
7482 "SHT_LLVM_CALL_GRAPH_PROFILE section: " +
7483 toString(CGProfileRelaOrError
.takeError()));
7487 CGProfileRela
= *CGProfileRelaOrError
;
7488 for (const typename
ELFT::Rela
&Rela
: CGProfileRela
)
7489 SymbolIndices
.push_back(Rela
.getSymbol(Obj
.isMips64EL()));
7495 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printCGProfile() {
7496 auto IsMatch
= [](const Elf_Shdr
&Sec
) -> bool {
7497 return Sec
.sh_type
== ELF::SHT_LLVM_CALL_GRAPH_PROFILE
;
7500 Expected
<MapVector
<const Elf_Shdr
*, const Elf_Shdr
*>> SecToRelocMapOrErr
=
7501 this->Obj
.getSectionAndRelocations(IsMatch
);
7502 if (!SecToRelocMapOrErr
) {
7503 this->reportUniqueWarning("unable to get CG Profile section(s): " +
7504 toString(SecToRelocMapOrErr
.takeError()));
7508 for (const auto &CGMapEntry
: *SecToRelocMapOrErr
) {
7509 const Elf_Shdr
*CGSection
= CGMapEntry
.first
;
7510 const Elf_Shdr
*CGRelSection
= CGMapEntry
.second
;
7512 Expected
<ArrayRef
<Elf_CGProfile
>> CGProfileOrErr
=
7513 this->Obj
.template getSectionContentsAsArray
<Elf_CGProfile
>(*CGSection
);
7514 if (!CGProfileOrErr
) {
7515 this->reportUniqueWarning(
7516 "unable to load the SHT_LLVM_CALL_GRAPH_PROFILE section: " +
7517 toString(CGProfileOrErr
.takeError()));
7521 SmallVector
<uint32_t, 128> SymbolIndices
;
7523 getSymbolIndices
<ELFT
>(CGRelSection
, this->Obj
, this, SymbolIndices
);
7524 if (UseReloc
&& SymbolIndices
.size() != CGProfileOrErr
->size() * 2) {
7525 this->reportUniqueWarning(
7526 "number of from/to pairs does not match number of frequencies");
7530 ListScope
L(W
, "CGProfile");
7531 for (uint32_t I
= 0, Size
= CGProfileOrErr
->size(); I
!= Size
; ++I
) {
7532 const Elf_CGProfile
&CGPE
= (*CGProfileOrErr
)[I
];
7533 DictScope
D(W
, "CGProfileEntry");
7535 uint32_t From
= SymbolIndices
[I
* 2];
7536 uint32_t To
= SymbolIndices
[I
* 2 + 1];
7537 W
.printNumber("From", this->getStaticSymbolName(From
), From
);
7538 W
.printNumber("To", this->getStaticSymbolName(To
), To
);
7540 W
.printNumber("Weight", CGPE
.cgp_weight
);
7545 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printBBAddrMaps() {
7546 bool IsRelocatable
= this->Obj
.getHeader().e_type
== ELF::ET_REL
;
7547 using Elf_Shdr
= typename
ELFT::Shdr
;
7548 auto IsMatch
= [](const Elf_Shdr
&Sec
) -> bool {
7549 return Sec
.sh_type
== ELF::SHT_LLVM_BB_ADDR_MAP
||
7550 Sec
.sh_type
== ELF::SHT_LLVM_BB_ADDR_MAP_V0
;
7552 Expected
<MapVector
<const Elf_Shdr
*, const Elf_Shdr
*>> SecRelocMapOrErr
=
7553 this->Obj
.getSectionAndRelocations(IsMatch
);
7554 if (!SecRelocMapOrErr
) {
7555 this->reportUniqueWarning(
7556 "failed to get SHT_LLVM_BB_ADDR_MAP section(s): " +
7557 toString(SecRelocMapOrErr
.takeError()));
7560 for (auto const &[Sec
, RelocSec
] : *SecRelocMapOrErr
) {
7561 std::optional
<const Elf_Shdr
*> FunctionSec
;
7564 unwrapOrError(this->FileName
, this->Obj
.getSection(Sec
->sh_link
));
7565 ListScope
L(W
, "BBAddrMap");
7566 if (IsRelocatable
&& !RelocSec
) {
7567 this->reportUniqueWarning("unable to get relocation section for " +
7568 this->describe(*Sec
));
7571 Expected
<std::vector
<BBAddrMap
>> BBAddrMapOrErr
=
7572 this->Obj
.decodeBBAddrMap(*Sec
, RelocSec
);
7573 if (!BBAddrMapOrErr
) {
7574 this->reportUniqueWarning("unable to dump " + this->describe(*Sec
) +
7575 ": " + toString(BBAddrMapOrErr
.takeError()));
7578 for (const BBAddrMap
&AM
: *BBAddrMapOrErr
) {
7579 DictScope
D(W
, "Function");
7580 W
.printHex("At", AM
.Addr
);
7581 SmallVector
<uint32_t> FuncSymIndex
=
7582 this->getSymbolIndexesForFunctionAddress(AM
.Addr
, FunctionSec
);
7583 std::string FuncName
= "<?>";
7584 if (FuncSymIndex
.empty())
7585 this->reportUniqueWarning(
7586 "could not identify function symbol for address (0x" +
7587 Twine::utohexstr(AM
.Addr
) + ") in " + this->describe(*Sec
));
7589 FuncName
= this->getStaticSymbolName(FuncSymIndex
.front());
7590 W
.printString("Name", FuncName
);
7592 ListScope
L(W
, "BB entries");
7593 for (const BBAddrMap::BBEntry
&BBE
: AM
.BBEntries
) {
7595 W
.printNumber("ID", BBE
.ID
);
7596 W
.printHex("Offset", BBE
.Offset
);
7597 W
.printHex("Size", BBE
.Size
);
7598 W
.printBoolean("HasReturn", BBE
.hasReturn());
7599 W
.printBoolean("HasTailCall", BBE
.hasTailCall());
7600 W
.printBoolean("IsEHPad", BBE
.isEHPad());
7601 W
.printBoolean("CanFallThrough", BBE
.canFallThrough());
7602 W
.printBoolean("HasIndirectBranch", BBE
.hasIndirectBranch());
7608 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printAddrsig() {
7609 ListScope
L(W
, "Addrsig");
7610 if (!this->DotAddrsigSec
)
7613 Expected
<std::vector
<uint64_t>> SymsOrErr
=
7614 decodeAddrsigSection(this->Obj
, *this->DotAddrsigSec
);
7616 this->reportUniqueWarning(SymsOrErr
.takeError());
7620 for (uint64_t Sym
: *SymsOrErr
)
7621 W
.printNumber("Sym", this->getStaticSymbolName(Sym
), Sym
);
7624 template <typename ELFT
>
7625 static bool printGNUNoteLLVMStyle(uint32_t NoteType
, ArrayRef
<uint8_t> Desc
,
7627 // Return true if we were able to pretty-print the note, false otherwise.
7631 case ELF::NT_GNU_ABI_TAG
: {
7632 const GNUAbiTag
&AbiTag
= getGNUAbiTag
<ELFT
>(Desc
);
7633 if (!AbiTag
.IsValid
) {
7634 W
.printString("ABI", "<corrupt GNU_ABI_TAG>");
7637 W
.printString("OS", AbiTag
.OSName
);
7638 W
.printString("ABI", AbiTag
.ABI
);
7642 case ELF::NT_GNU_BUILD_ID
: {
7643 W
.printString("Build ID", getGNUBuildId(Desc
));
7646 case ELF::NT_GNU_GOLD_VERSION
:
7647 W
.printString("Version", getDescAsStringRef(Desc
));
7649 case ELF::NT_GNU_PROPERTY_TYPE_0
:
7650 ListScope
D(W
, "Property");
7651 for (const std::string
&Property
: getGNUPropertyList
<ELFT
>(Desc
))
7652 W
.printString(Property
);
7658 static bool printAndroidNoteLLVMStyle(uint32_t NoteType
, ArrayRef
<uint8_t> Desc
,
7660 // Return true if we were able to pretty-print the note, false otherwise.
7661 AndroidNoteProperties Props
= getAndroidNoteProperties(NoteType
, Desc
);
7664 for (const auto &KV
: Props
)
7665 W
.printString(KV
.first
, KV
.second
);
7669 template <class ELFT
>
7670 static bool printAarch64NoteLLVMStyle(uint32_t NoteType
, ArrayRef
<uint8_t> Desc
,
7672 if (NoteType
!= NT_ARM_TYPE_PAUTH_ABI_TAG
)
7675 if (Desc
.size() < 16)
7679 support::endian::read64
<ELFT::TargetEndianness
>(Desc
.data() + 0);
7681 support::endian::read64
<ELFT::TargetEndianness
>(Desc
.data() + 8);
7682 W
.printNumber("Platform", platform
);
7683 W
.printNumber("Version", version
);
7685 if (Desc
.size() > 16)
7686 W
.printString("Additional info",
7687 toHex(ArrayRef
<uint8_t>(Desc
.data() + 16, Desc
.size() - 16)));
7692 template <class ELFT
>
7693 void LLVMELFDumper
<ELFT
>::printMemtag(
7694 const ArrayRef
<std::pair
<std::string
, std::string
>> DynamicEntries
,
7695 const ArrayRef
<uint8_t> AndroidNoteDesc
,
7696 const ArrayRef
<std::pair
<uint64_t, uint64_t>> Descriptors
) {
7698 ListScope
L(W
, "Memtag Dynamic Entries:");
7699 if (DynamicEntries
.empty())
7700 W
.printString("< none found >");
7701 for (const auto &DynamicEntryKV
: DynamicEntries
)
7702 W
.printString(DynamicEntryKV
.first
, DynamicEntryKV
.second
);
7705 if (!AndroidNoteDesc
.empty()) {
7706 ListScope
L(W
, "Memtag Android Note:");
7707 printAndroidNoteLLVMStyle(ELF::NT_ANDROID_TYPE_MEMTAG
, AndroidNoteDesc
, W
);
7710 if (Descriptors
.empty())
7714 ListScope
L(W
, "Memtag Global Descriptors:");
7715 for (const auto &[Addr
, BytesToTag
] : Descriptors
) {
7716 W
.printHex("0x" + utohexstr(Addr
), BytesToTag
);
7721 template <typename ELFT
>
7722 static bool printLLVMOMPOFFLOADNoteLLVMStyle(uint32_t NoteType
,
7723 ArrayRef
<uint8_t> Desc
,
7728 case ELF::NT_LLVM_OPENMP_OFFLOAD_VERSION
:
7729 W
.printString("Version", getDescAsStringRef(Desc
));
7731 case ELF::NT_LLVM_OPENMP_OFFLOAD_PRODUCER
:
7732 W
.printString("Producer", getDescAsStringRef(Desc
));
7734 case ELF::NT_LLVM_OPENMP_OFFLOAD_PRODUCER_VERSION
:
7735 W
.printString("Producer version", getDescAsStringRef(Desc
));
7741 static void printCoreNoteLLVMStyle(const CoreNote
&Note
, ScopedPrinter
&W
) {
7742 W
.printNumber("Page Size", Note
.PageSize
);
7743 for (const CoreFileMapping
&Mapping
: Note
.Mappings
) {
7744 ListScope
D(W
, "Mapping");
7745 W
.printHex("Start", Mapping
.Start
);
7746 W
.printHex("End", Mapping
.End
);
7747 W
.printHex("Offset", Mapping
.Offset
);
7748 W
.printString("Filename", Mapping
.Filename
);
7752 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printNotes() {
7753 ListScope
L(W
, "Notes");
7755 std::unique_ptr
<DictScope
> NoteScope
;
7757 auto StartNotes
= [&](std::optional
<StringRef
> SecName
,
7758 const typename
ELFT::Off Offset
,
7759 const typename
ELFT::Addr Size
, size_t Al
) {
7760 Align
= std::max
<size_t>(Al
, 4);
7761 NoteScope
= std::make_unique
<DictScope
>(W
, "NoteSection");
7762 W
.printString("Name", SecName
? *SecName
: "<?>");
7763 W
.printHex("Offset", Offset
);
7764 W
.printHex("Size", Size
);
7767 auto EndNotes
= [&] { NoteScope
.reset(); };
7769 auto ProcessNote
= [&](const Elf_Note
&Note
, bool IsCore
) -> Error
{
7770 DictScope
D2(W
, "Note");
7771 StringRef Name
= Note
.getName();
7772 ArrayRef
<uint8_t> Descriptor
= Note
.getDesc(Align
);
7773 Elf_Word Type
= Note
.getType();
7775 // Print the note owner/type.
7776 W
.printString("Owner", Name
);
7777 W
.printHex("Data size", Descriptor
.size());
7779 StringRef NoteType
=
7780 getNoteTypeName
<ELFT
>(Note
, this->Obj
.getHeader().e_type
);
7781 if (!NoteType
.empty())
7782 W
.printString("Type", NoteType
);
7784 W
.printString("Type",
7785 "Unknown (" + to_string(format_hex(Type
, 10)) + ")");
7787 // Print the description, or fallback to printing raw bytes for unknown
7788 // owners/if we fail to pretty-print the contents.
7789 if (Name
== "GNU") {
7790 if (printGNUNoteLLVMStyle
<ELFT
>(Type
, Descriptor
, W
))
7791 return Error::success();
7792 } else if (Name
== "FreeBSD") {
7793 if (std::optional
<FreeBSDNote
> N
=
7794 getFreeBSDNote
<ELFT
>(Type
, Descriptor
, IsCore
)) {
7795 W
.printString(N
->Type
, N
->Value
);
7796 return Error::success();
7798 } else if (Name
== "AMD") {
7799 const AMDNote N
= getAMDNote
<ELFT
>(Type
, Descriptor
);
7800 if (!N
.Type
.empty()) {
7801 W
.printString(N
.Type
, N
.Value
);
7802 return Error::success();
7804 } else if (Name
== "AMDGPU") {
7805 const AMDGPUNote N
= getAMDGPUNote
<ELFT
>(Type
, Descriptor
);
7806 if (!N
.Type
.empty()) {
7807 W
.printString(N
.Type
, N
.Value
);
7808 return Error::success();
7810 } else if (Name
== "LLVMOMPOFFLOAD") {
7811 if (printLLVMOMPOFFLOADNoteLLVMStyle
<ELFT
>(Type
, Descriptor
, W
))
7812 return Error::success();
7813 } else if (Name
== "CORE") {
7814 if (Type
== ELF::NT_FILE
) {
7815 DataExtractor
DescExtractor(
7816 Descriptor
, ELFT::TargetEndianness
== llvm::endianness::little
,
7818 if (Expected
<CoreNote
> N
= readCoreNote(DescExtractor
)) {
7819 printCoreNoteLLVMStyle(*N
, W
);
7820 return Error::success();
7822 return N
.takeError();
7825 } else if (Name
== "Android") {
7826 if (printAndroidNoteLLVMStyle(Type
, Descriptor
, W
))
7827 return Error::success();
7828 } else if (Name
== "ARM") {
7829 if (printAarch64NoteLLVMStyle
<ELFT
>(Type
, Descriptor
, W
))
7830 return Error::success();
7832 if (!Descriptor
.empty()) {
7833 W
.printBinaryBlock("Description data", Descriptor
);
7835 return Error::success();
7838 processNotesHelper(*this, /*StartNotesFn=*/StartNotes
,
7839 /*ProcessNoteFn=*/ProcessNote
, /*FinishNotesFn=*/EndNotes
);
7842 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printELFLinkerOptions() {
7843 ListScope
L(W
, "LinkerOptions");
7846 for (const Elf_Shdr
&Shdr
: cantFail(this->Obj
.sections())) {
7848 if (Shdr
.sh_type
!= ELF::SHT_LLVM_LINKER_OPTIONS
)
7851 Expected
<ArrayRef
<uint8_t>> ContentsOrErr
=
7852 this->Obj
.getSectionContents(Shdr
);
7853 if (!ContentsOrErr
) {
7854 this->reportUniqueWarning("unable to read the content of the "
7855 "SHT_LLVM_LINKER_OPTIONS section: " +
7856 toString(ContentsOrErr
.takeError()));
7859 if (ContentsOrErr
->empty())
7862 if (ContentsOrErr
->back() != 0) {
7863 this->reportUniqueWarning("SHT_LLVM_LINKER_OPTIONS section at index " +
7866 "content is not null-terminated");
7870 SmallVector
<StringRef
, 16> Strings
;
7871 toStringRef(ContentsOrErr
->drop_back()).split(Strings
, '\0');
7872 if (Strings
.size() % 2 != 0) {
7873 this->reportUniqueWarning(
7874 "SHT_LLVM_LINKER_OPTIONS section at index " + Twine(I
) +
7875 " is broken: an incomplete "
7876 "key-value pair was found. The last possible key was: \"" +
7877 Strings
.back() + "\"");
7881 for (size_t I
= 0; I
< Strings
.size(); I
+= 2)
7882 W
.printString(Strings
[I
], Strings
[I
+ 1]);
7886 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printDependentLibs() {
7887 ListScope
L(W
, "DependentLibs");
7888 this->printDependentLibsHelper(
7889 [](const Elf_Shdr
&) {},
7890 [this](StringRef Lib
, uint64_t) { W
.printString(Lib
); });
7893 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printStackSizes() {
7894 ListScope
L(W
, "StackSizes");
7895 if (this->Obj
.getHeader().e_type
== ELF::ET_REL
)
7896 this->printRelocatableStackSizes([]() {});
7898 this->printNonRelocatableStackSizes([]() {});
7901 template <class ELFT
>
7902 void LLVMELFDumper
<ELFT
>::printStackSizeEntry(uint64_t Size
,
7903 ArrayRef
<std::string
> FuncNames
) {
7904 DictScope
D(W
, "Entry");
7905 W
.printList("Functions", FuncNames
);
7906 W
.printHex("Size", Size
);
7909 template <class ELFT
>
7910 void LLVMELFDumper
<ELFT
>::printMipsGOT(const MipsGOTParser
<ELFT
> &Parser
) {
7911 auto PrintEntry
= [&](const Elf_Addr
*E
) {
7912 W
.printHex("Address", Parser
.getGotAddress(E
));
7913 W
.printNumber("Access", Parser
.getGotOffset(E
));
7914 W
.printHex("Initial", *E
);
7917 DictScope
GS(W
, Parser
.IsStatic
? "Static GOT" : "Primary GOT");
7919 W
.printHex("Canonical gp value", Parser
.getGp());
7921 ListScope
RS(W
, "Reserved entries");
7923 DictScope
D(W
, "Entry");
7924 PrintEntry(Parser
.getGotLazyResolver());
7925 W
.printString("Purpose", StringRef("Lazy resolver"));
7928 if (Parser
.getGotModulePointer()) {
7929 DictScope
D(W
, "Entry");
7930 PrintEntry(Parser
.getGotModulePointer());
7931 W
.printString("Purpose", StringRef("Module pointer (GNU extension)"));
7935 ListScope
LS(W
, "Local entries");
7936 for (auto &E
: Parser
.getLocalEntries()) {
7937 DictScope
D(W
, "Entry");
7942 if (Parser
.IsStatic
)
7946 ListScope
GS(W
, "Global entries");
7947 for (auto &E
: Parser
.getGlobalEntries()) {
7948 DictScope
D(W
, "Entry");
7952 const Elf_Sym
&Sym
= *Parser
.getGotSym(&E
);
7953 W
.printHex("Value", Sym
.st_value
);
7954 W
.printEnum("Type", Sym
.getType(), ArrayRef(ElfSymbolTypes
));
7956 const unsigned SymIndex
= &Sym
- this->dynamic_symbols().begin();
7957 DataRegion
<Elf_Word
> ShndxTable(
7958 (const Elf_Word
*)this->DynSymTabShndxRegion
.Addr
, this->Obj
.end());
7959 printSymbolSection(Sym
, SymIndex
, ShndxTable
);
7961 std::string SymName
= this->getFullSymbolName(
7962 Sym
, SymIndex
, ShndxTable
, this->DynamicStringTable
, true);
7963 W
.printNumber("Name", SymName
, Sym
.st_name
);
7967 W
.printNumber("Number of TLS and multi-GOT entries",
7968 uint64_t(Parser
.getOtherEntries().size()));
7971 template <class ELFT
>
7972 void LLVMELFDumper
<ELFT
>::printMipsPLT(const MipsGOTParser
<ELFT
> &Parser
) {
7973 auto PrintEntry
= [&](const Elf_Addr
*E
) {
7974 W
.printHex("Address", Parser
.getPltAddress(E
));
7975 W
.printHex("Initial", *E
);
7978 DictScope
GS(W
, "PLT GOT");
7981 ListScope
RS(W
, "Reserved entries");
7983 DictScope
D(W
, "Entry");
7984 PrintEntry(Parser
.getPltLazyResolver());
7985 W
.printString("Purpose", StringRef("PLT lazy resolver"));
7988 if (auto E
= Parser
.getPltModulePointer()) {
7989 DictScope
D(W
, "Entry");
7991 W
.printString("Purpose", StringRef("Module pointer"));
7995 ListScope
LS(W
, "Entries");
7996 DataRegion
<Elf_Word
> ShndxTable(
7997 (const Elf_Word
*)this->DynSymTabShndxRegion
.Addr
, this->Obj
.end());
7998 for (auto &E
: Parser
.getPltEntries()) {
7999 DictScope
D(W
, "Entry");
8002 const Elf_Sym
&Sym
= *Parser
.getPltSym(&E
);
8003 W
.printHex("Value", Sym
.st_value
);
8004 W
.printEnum("Type", Sym
.getType(), ArrayRef(ElfSymbolTypes
));
8005 printSymbolSection(Sym
, &Sym
- this->dynamic_symbols().begin(),
8008 const Elf_Sym
*FirstSym
= cantFail(
8009 this->Obj
.template getEntry
<Elf_Sym
>(*Parser
.getPltSymTable(), 0));
8010 std::string SymName
= this->getFullSymbolName(
8011 Sym
, &Sym
- FirstSym
, ShndxTable
, Parser
.getPltStrTable(), true);
8012 W
.printNumber("Name", SymName
, Sym
.st_name
);
8017 template <class ELFT
> void LLVMELFDumper
<ELFT
>::printMipsABIFlags() {
8018 const Elf_Mips_ABIFlags
<ELFT
> *Flags
;
8019 if (Expected
<const Elf_Mips_ABIFlags
<ELFT
> *> SecOrErr
=
8020 getMipsAbiFlagsSection(*this)) {
8023 W
.startLine() << "There is no .MIPS.abiflags section in the file.\n";
8027 this->reportUniqueWarning(SecOrErr
.takeError());
8031 raw_ostream
&OS
= W
.getOStream();
8032 DictScope
GS(W
, "MIPS ABI Flags");
8034 W
.printNumber("Version", Flags
->version
);
8035 W
.startLine() << "ISA: ";
8036 if (Flags
->isa_rev
<= 1)
8037 OS
<< format("MIPS%u", Flags
->isa_level
);
8039 OS
<< format("MIPS%ur%u", Flags
->isa_level
, Flags
->isa_rev
);
8041 W
.printEnum("ISA Extension", Flags
->isa_ext
, ArrayRef(ElfMipsISAExtType
));
8042 W
.printFlags("ASEs", Flags
->ases
, ArrayRef(ElfMipsASEFlags
));
8043 W
.printEnum("FP ABI", Flags
->fp_abi
, ArrayRef(ElfMipsFpABIType
));
8044 W
.printNumber("GPR size", getMipsRegisterSize(Flags
->gpr_size
));
8045 W
.printNumber("CPR1 size", getMipsRegisterSize(Flags
->cpr1_size
));
8046 W
.printNumber("CPR2 size", getMipsRegisterSize(Flags
->cpr2_size
));
8047 W
.printFlags("Flags 1", Flags
->flags1
, ArrayRef(ElfMipsFlags1
));
8048 W
.printHex("Flags 2", Flags
->flags2
);
8051 template <class ELFT
>
8052 void JSONELFDumper
<ELFT
>::printFileSummary(StringRef FileStr
, ObjectFile
&Obj
,
8053 ArrayRef
<std::string
> InputFilenames
,
8055 FileScope
= std::make_unique
<DictScope
>(this->W
);
8056 DictScope
D(this->W
, "FileSummary");
8057 this->W
.printString("File", FileStr
);
8058 this->W
.printString("Format", Obj
.getFileFormatName());
8059 this->W
.printString("Arch", Triple::getArchTypeName(Obj
.getArch()));
8060 this->W
.printString(
8062 std::string(formatv("{0}bit", 8 * Obj
.getBytesInAddress())));
8063 this->printLoadName();
8066 template <class ELFT
>
8067 void JSONELFDumper
<ELFT
>::printZeroSymbolOtherField(
8068 const Elf_Sym
&Symbol
) const {
8069 // We want the JSON format to be uniform, since it is machine readable, so
8070 // always print the `Other` field the same way.
8071 this->printSymbolOtherField(Symbol
);
8074 template <class ELFT
>
8075 void JSONELFDumper
<ELFT
>::printDefaultRelRelaReloc(const Relocation
<ELFT
> &R
,
8076 StringRef SymbolName
,
8077 StringRef RelocName
) {
8078 this->printExpandedRelRelaReloc(R
, SymbolName
, RelocName
);
8081 template <class ELFT
>
8082 void JSONELFDumper
<ELFT
>::printRelocationSectionInfo(const Elf_Shdr
&Sec
,
8084 const unsigned SecNdx
) {
8085 DictScope
Group(this->W
);
8086 this->W
.printNumber("SectionIndex", SecNdx
);
8087 ListScope
D(this->W
, "Relocs");
8088 this->printRelocationsHelper(Sec
);
8091 template <class ELFT
>
8092 std::string JSONELFDumper
<ELFT
>::getGroupSectionHeaderName() const {
8093 return "GroupSections";
8096 template <class ELFT
>
8097 void JSONELFDumper
<ELFT
>::printSectionGroupMembers(StringRef Name
,
8098 uint64_t Idx
) const {
8099 DictScope
Grp(this->W
);
8100 this->W
.printString("Name", Name
);
8101 this->W
.printNumber("Index", Idx
);
8104 template <class ELFT
> void JSONELFDumper
<ELFT
>::printEmptyGroupMessage() const {
8105 // JSON output does not need to print anything for empty groups