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"
17 #include "ObjDumper.h"
18 #include "StackMapPrinter.h"
19 #include "llvm-readobj.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/DenseSet.h"
23 #include "llvm/ADT/MapVector.h"
24 #include "llvm/ADT/Optional.h"
25 #include "llvm/ADT/PointerIntPair.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/SmallString.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/ADT/StringExtras.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/ADT/Twine.h"
32 #include "llvm/BinaryFormat/AMDGPUMetadataVerifier.h"
33 #include "llvm/BinaryFormat/ELF.h"
34 #include "llvm/Demangle/Demangle.h"
35 #include "llvm/Object/ELF.h"
36 #include "llvm/Object/ELFObjectFile.h"
37 #include "llvm/Object/ELFTypes.h"
38 #include "llvm/Object/Error.h"
39 #include "llvm/Object/ObjectFile.h"
40 #include "llvm/Object/RelocationResolver.h"
41 #include "llvm/Object/StackMapParser.h"
42 #include "llvm/Support/AMDGPUMetadata.h"
43 #include "llvm/Support/ARMAttributeParser.h"
44 #include "llvm/Support/ARMBuildAttributes.h"
45 #include "llvm/Support/Casting.h"
46 #include "llvm/Support/Compiler.h"
47 #include "llvm/Support/Endian.h"
48 #include "llvm/Support/ErrorHandling.h"
49 #include "llvm/Support/Format.h"
50 #include "llvm/Support/FormatVariadic.h"
51 #include "llvm/Support/FormattedStream.h"
52 #include "llvm/Support/LEB128.h"
53 #include "llvm/Support/MathExtras.h"
54 #include "llvm/Support/MipsABIFlags.h"
55 #include "llvm/Support/ScopedPrinter.h"
56 #include "llvm/Support/raw_ostream.h"
65 #include <system_error>
66 #include <unordered_set>
70 using namespace llvm::object
;
73 #define LLVM_READOBJ_ENUM_CASE(ns, enum) \
77 #define ENUM_ENT(enum, altName) \
78 { #enum, altName, ELF::enum }
80 #define ENUM_ENT_1(enum) \
81 { #enum, #enum, ELF::enum }
83 #define LLVM_READOBJ_PHDR_ENUM(ns, enum) \
85 return std::string(#enum).substr(3);
87 #define TYPEDEF_ELF_TYPES(ELFT) \
88 using ELFO = ELFFile<ELFT>; \
89 using Elf_Addr = typename ELFT::Addr; \
90 using Elf_Shdr = typename ELFT::Shdr; \
91 using Elf_Sym = typename ELFT::Sym; \
92 using Elf_Dyn = typename ELFT::Dyn; \
93 using Elf_Dyn_Range = typename ELFT::DynRange; \
94 using Elf_Rel = typename ELFT::Rel; \
95 using Elf_Rela = typename ELFT::Rela; \
96 using Elf_Relr = typename ELFT::Relr; \
97 using Elf_Rel_Range = typename ELFT::RelRange; \
98 using Elf_Rela_Range = typename ELFT::RelaRange; \
99 using Elf_Relr_Range = typename ELFT::RelrRange; \
100 using Elf_Phdr = typename ELFT::Phdr; \
101 using Elf_Half = typename ELFT::Half; \
102 using Elf_Ehdr = typename ELFT::Ehdr; \
103 using Elf_Word = typename ELFT::Word; \
104 using Elf_Hash = typename ELFT::Hash; \
105 using Elf_GnuHash = typename ELFT::GnuHash; \
106 using Elf_Note = typename ELFT::Note; \
107 using Elf_Sym_Range = typename ELFT::SymRange; \
108 using Elf_Versym = typename ELFT::Versym; \
109 using Elf_Verneed = typename ELFT::Verneed; \
110 using Elf_Vernaux = typename ELFT::Vernaux; \
111 using Elf_Verdef = typename ELFT::Verdef; \
112 using Elf_Verdaux = typename ELFT::Verdaux; \
113 using Elf_CGProfile = typename ELFT::CGProfile; \
114 using uintX_t = typename ELFT::uint;
118 template <class ELFT
> class DumpStyle
;
120 /// Represents a contiguous uniform range in the file. We cannot just create a
121 /// range directly because when creating one of these from the .dynamic table
122 /// the size, entity size and virtual address are different entries in arbitrary
123 /// order (DT_REL, DT_RELSZ, DT_RELENT for example).
124 struct DynRegionInfo
{
125 DynRegionInfo(StringRef ObjName
) : FileName(ObjName
) {}
126 DynRegionInfo(const void *A
, uint64_t S
, uint64_t ES
, StringRef ObjName
)
127 : Addr(A
), Size(S
), EntSize(ES
), FileName(ObjName
) {}
129 /// Address in current address space.
130 const void *Addr
= nullptr;
131 /// Size in bytes of the region.
133 /// Size of each entity in the region.
134 uint64_t EntSize
= 0;
136 /// Name of the file. Used for error reporting.
139 template <typename Type
> ArrayRef
<Type
> getAsArrayRef() const {
140 const Type
*Start
= reinterpret_cast<const Type
*>(Addr
);
142 return {Start
, Start
};
143 if (EntSize
!= sizeof(Type
) || Size
% EntSize
) {
144 // TODO: Add a section index to this warning.
145 reportWarning(createError("invalid section size (" + Twine(Size
) +
146 ") or entity size (" + Twine(EntSize
) + ")"),
148 return {Start
, Start
};
150 return {Start
, Start
+ (Size
/ EntSize
)};
154 template <typename ELFT
> class ELFDumper
: public ObjDumper
{
156 ELFDumper(const object::ELFObjectFile
<ELFT
> *ObjF
, ScopedPrinter
&Writer
);
158 void printFileHeaders() override
;
159 void printSectionHeaders() override
;
160 void printRelocations() override
;
161 void printDynamicRelocations() override
;
162 void printSymbols(bool PrintSymbols
, bool PrintDynamicSymbols
) override
;
163 void printHashSymbols() override
;
164 void printUnwindInfo() override
;
166 void printDynamicTable() override
;
167 void printNeededLibraries() override
;
168 void printProgramHeaders(bool PrintProgramHeaders
,
169 cl::boolOrDefault PrintSectionMapping
) override
;
170 void printHashTable() override
;
171 void printGnuHashTable() override
;
172 void printLoadName() override
;
173 void printVersionInfo() override
;
174 void printGroupSections() override
;
176 void printArchSpecificInfo() override
;
178 void printStackMap() const override
;
180 void printHashHistogram() override
;
182 void printCGProfile() override
;
183 void printAddrsig() override
;
185 void printNotes() override
;
187 void printELFLinkerOptions() override
;
188 void printStackSizes() override
;
190 const object::ELFObjectFile
<ELFT
> *getElfObject() const { return ObjF
; };
193 std::unique_ptr
<DumpStyle
<ELFT
>> ELFDumperStyle
;
195 TYPEDEF_ELF_TYPES(ELFT
)
197 DynRegionInfo
checkDRI(DynRegionInfo DRI
) {
198 const ELFFile
<ELFT
> *Obj
= ObjF
->getELFFile();
199 if (DRI
.Addr
< Obj
->base() ||
200 reinterpret_cast<const uint8_t *>(DRI
.Addr
) + DRI
.Size
>
201 Obj
->base() + Obj
->getBufSize())
202 reportError(errorCodeToError(llvm::object::object_error::parse_failed
),
203 ObjF
->getFileName());
207 DynRegionInfo
createDRIFrom(const Elf_Phdr
*P
, uintX_t EntSize
) {
208 return checkDRI({ObjF
->getELFFile()->base() + P
->p_offset
, P
->p_filesz
,
209 EntSize
, ObjF
->getFileName()});
212 DynRegionInfo
createDRIFrom(const Elf_Shdr
*S
) {
213 return checkDRI({ObjF
->getELFFile()->base() + S
->sh_offset
, S
->sh_size
,
214 S
->sh_entsize
, ObjF
->getFileName()});
217 void printAttributes();
218 void printMipsReginfo();
219 void printMipsOptions();
221 std::pair
<const Elf_Phdr
*, const Elf_Shdr
*>
222 findDynamic(const ELFFile
<ELFT
> *Obj
);
223 void loadDynamicTable(const ELFFile
<ELFT
> *Obj
);
224 void parseDynamicTable();
226 StringRef
getSymbolVersion(StringRef StrTab
, const Elf_Sym
*symb
,
227 bool &IsDefault
) const;
228 void LoadVersionMap() const;
229 void LoadVersionNeeds(const Elf_Shdr
*ec
) const;
230 void LoadVersionDefs(const Elf_Shdr
*sec
) const;
232 const object::ELFObjectFile
<ELFT
> *ObjF
;
233 DynRegionInfo DynRelRegion
;
234 DynRegionInfo DynRelaRegion
;
235 DynRegionInfo DynRelrRegion
;
236 DynRegionInfo DynPLTRelRegion
;
237 DynRegionInfo DynSymRegion
;
238 DynRegionInfo DynamicTable
;
239 StringRef DynamicStringTable
;
240 std::string SOName
= "<Not found>";
241 const Elf_Hash
*HashTable
= nullptr;
242 const Elf_GnuHash
*GnuHashTable
= nullptr;
243 const Elf_Shdr
*DotSymtabSec
= nullptr;
244 const Elf_Shdr
*DotCGProfileSec
= nullptr;
245 const Elf_Shdr
*DotAddrsigSec
= nullptr;
246 StringRef DynSymtabName
;
247 ArrayRef
<Elf_Word
> ShndxTable
;
249 const Elf_Shdr
*SymbolVersionSection
= nullptr; // .gnu.version
250 const Elf_Shdr
*SymbolVersionNeedSection
= nullptr; // .gnu.version_r
251 const Elf_Shdr
*SymbolVersionDefSection
= nullptr; // .gnu.version_d
253 // Records for each version index the corresponding Verdef or Vernaux entry.
254 // This is filled the first time LoadVersionMap() is called.
255 class VersionMapEntry
: public PointerIntPair
<const void *, 1> {
257 // If the integer is 0, this is an Elf_Verdef*.
258 // If the integer is 1, this is an Elf_Vernaux*.
259 VersionMapEntry() : PointerIntPair
<const void *, 1>(nullptr, 0) {}
260 VersionMapEntry(const Elf_Verdef
*verdef
)
261 : PointerIntPair
<const void *, 1>(verdef
, 0) {}
262 VersionMapEntry(const Elf_Vernaux
*vernaux
)
263 : PointerIntPair
<const void *, 1>(vernaux
, 1) {}
265 bool isNull() const { return getPointer() == nullptr; }
266 bool isVerdef() const { return !isNull() && getInt() == 0; }
267 bool isVernaux() const { return !isNull() && getInt() == 1; }
268 const Elf_Verdef
*getVerdef() const {
269 return isVerdef() ? (const Elf_Verdef
*)getPointer() : nullptr;
271 const Elf_Vernaux
*getVernaux() const {
272 return isVernaux() ? (const Elf_Vernaux
*)getPointer() : nullptr;
275 mutable SmallVector
<VersionMapEntry
, 16> VersionMap
;
278 Elf_Dyn_Range
dynamic_table() const {
279 // A valid .dynamic section contains an array of entries terminated
280 // with a DT_NULL entry. However, sometimes the section content may
281 // continue past the DT_NULL entry, so to dump the section correctly,
282 // we first find the end of the entries by iterating over them.
283 Elf_Dyn_Range Table
= DynamicTable
.getAsArrayRef
<Elf_Dyn
>();
286 while (Size
< Table
.size())
287 if (Table
[Size
++].getTag() == DT_NULL
)
290 return Table
.slice(0, Size
);
293 Elf_Sym_Range
dynamic_symbols() const {
294 return DynSymRegion
.getAsArrayRef
<Elf_Sym
>();
297 Elf_Rel_Range
dyn_rels() const;
298 Elf_Rela_Range
dyn_relas() const;
299 Elf_Relr_Range
dyn_relrs() const;
300 std::string
getFullSymbolName(const Elf_Sym
*Symbol
, StringRef StrTable
,
301 bool IsDynamic
) const;
302 void getSectionNameIndex(const Elf_Sym
*Symbol
, const Elf_Sym
*FirstSym
,
303 StringRef
&SectionName
,
304 unsigned &SectionIndex
) const;
305 Expected
<std::string
> getStaticSymbolName(uint32_t Index
) const;
306 std::string
getDynamicString(uint64_t Value
) const;
307 StringRef
getSymbolVersionByIndex(StringRef StrTab
,
308 uint32_t VersionSymbolIndex
,
309 bool &IsDefault
) const;
311 void printSymbolsHelper(bool IsDynamic
) const;
312 void printDynamicEntry(raw_ostream
&OS
, uint64_t Type
, uint64_t Value
) const;
314 const Elf_Shdr
*getDotSymtabSec() const { return DotSymtabSec
; }
315 const Elf_Shdr
*getDotCGProfileSec() const { return DotCGProfileSec
; }
316 const Elf_Shdr
*getDotAddrsigSec() const { return DotAddrsigSec
; }
317 ArrayRef
<Elf_Word
> getShndxTable() const { return ShndxTable
; }
318 StringRef
getDynamicStringTable() const { return DynamicStringTable
; }
319 const DynRegionInfo
&getDynRelRegion() const { return DynRelRegion
; }
320 const DynRegionInfo
&getDynRelaRegion() const { return DynRelaRegion
; }
321 const DynRegionInfo
&getDynRelrRegion() const { return DynRelrRegion
; }
322 const DynRegionInfo
&getDynPLTRelRegion() const { return DynPLTRelRegion
; }
323 const DynRegionInfo
&getDynamicTableRegion() const { return DynamicTable
; }
324 const Elf_Hash
*getHashTable() const { return HashTable
; }
325 const Elf_GnuHash
*getGnuHashTable() const { return GnuHashTable
; }
328 template <class ELFT
>
329 void ELFDumper
<ELFT
>::printSymbolsHelper(bool IsDynamic
) const {
330 StringRef StrTable
, SymtabName
;
332 Elf_Sym_Range
Syms(nullptr, nullptr);
333 const ELFFile
<ELFT
> *Obj
= ObjF
->getELFFile();
335 StrTable
= DynamicStringTable
;
336 Syms
= dynamic_symbols();
337 SymtabName
= DynSymtabName
;
338 if (DynSymRegion
.Addr
)
339 Entries
= DynSymRegion
.Size
/ DynSymRegion
.EntSize
;
343 StrTable
= unwrapOrError(ObjF
->getFileName(),
344 Obj
->getStringTableForSymtab(*DotSymtabSec
));
345 Syms
= unwrapOrError(ObjF
->getFileName(), Obj
->symbols(DotSymtabSec
));
347 unwrapOrError(ObjF
->getFileName(), Obj
->getSectionName(DotSymtabSec
));
348 Entries
= DotSymtabSec
->getEntityCount();
350 if (Syms
.begin() == Syms
.end())
353 // The st_other field has 2 logical parts. The first two bits hold the symbol
354 // visibility (STV_*) and the remainder hold other platform-specific values.
355 bool NonVisibilityBitsUsed
= llvm::find_if(Syms
, [](const Elf_Sym
&S
) {
356 return S
.st_other
& ~0x3;
359 ELFDumperStyle
->printSymtabMessage(Obj
, SymtabName
, Entries
,
360 NonVisibilityBitsUsed
);
361 for (const auto &Sym
: Syms
)
362 ELFDumperStyle
->printSymbol(Obj
, &Sym
, Syms
.begin(), StrTable
, IsDynamic
,
363 NonVisibilityBitsUsed
);
366 template <class ELFT
> class MipsGOTParser
;
368 template <typename ELFT
> class DumpStyle
{
370 using Elf_Shdr
= typename
ELFT::Shdr
;
371 using Elf_Sym
= typename
ELFT::Sym
;
372 using Elf_Addr
= typename
ELFT::Addr
;
374 DumpStyle(ELFDumper
<ELFT
> *Dumper
) : Dumper(Dumper
) {
375 FileName
= this->Dumper
->getElfObject()->getFileName();
377 // Dumper reports all non-critical errors as warnings.
378 // It does not print the same warning more than once.
379 WarningHandler
= [this](const Twine
&Msg
) {
380 if (Warnings
.insert(Msg
.str()).second
)
381 reportWarning(createError(Msg
), FileName
);
382 return Error::success();
386 virtual ~DumpStyle() = default;
388 virtual void printFileHeaders(const ELFFile
<ELFT
> *Obj
) = 0;
389 virtual void printGroupSections(const ELFFile
<ELFT
> *Obj
) = 0;
390 virtual void printRelocations(const ELFFile
<ELFT
> *Obj
) = 0;
391 virtual void printSectionHeaders(const ELFFile
<ELFT
> *Obj
) = 0;
392 virtual void printSymbols(const ELFFile
<ELFT
> *Obj
, bool PrintSymbols
,
393 bool PrintDynamicSymbols
) = 0;
394 virtual void printHashSymbols(const ELFFile
<ELFT
> *Obj
) {}
395 virtual void printDynamic(const ELFFile
<ELFT
> *Obj
) {}
396 virtual void printDynamicRelocations(const ELFFile
<ELFT
> *Obj
) = 0;
397 virtual void printSymtabMessage(const ELFFile
<ELFT
> *Obj
, StringRef Name
,
398 size_t Offset
, bool NonVisibilityBitsUsed
) {}
399 virtual void printSymbol(const ELFFile
<ELFT
> *Obj
, const Elf_Sym
*Symbol
,
400 const Elf_Sym
*FirstSym
, StringRef StrTable
,
401 bool IsDynamic
, bool NonVisibilityBitsUsed
) = 0;
402 virtual void printProgramHeaders(const ELFFile
<ELFT
> *Obj
,
403 bool PrintProgramHeaders
,
404 cl::boolOrDefault PrintSectionMapping
) = 0;
405 virtual void printVersionSymbolSection(const ELFFile
<ELFT
> *Obj
,
406 const Elf_Shdr
*Sec
) = 0;
407 virtual void printVersionDefinitionSection(const ELFFile
<ELFT
> *Obj
,
408 const Elf_Shdr
*Sec
) = 0;
409 virtual void printVersionDependencySection(const ELFFile
<ELFT
> *Obj
,
410 const Elf_Shdr
*Sec
) = 0;
411 virtual void printHashHistogram(const ELFFile
<ELFT
> *Obj
) = 0;
412 virtual void printCGProfile(const ELFFile
<ELFT
> *Obj
) = 0;
413 virtual void printAddrsig(const ELFFile
<ELFT
> *Obj
) = 0;
414 virtual void printNotes(const ELFFile
<ELFT
> *Obj
) = 0;
415 virtual void printELFLinkerOptions(const ELFFile
<ELFT
> *Obj
) = 0;
416 virtual void printStackSizes(const ELFObjectFile
<ELFT
> *Obj
) = 0;
417 void printNonRelocatableStackSizes(const ELFObjectFile
<ELFT
> *Obj
,
418 std::function
<void()> PrintHeader
);
419 void printRelocatableStackSizes(const ELFObjectFile
<ELFT
> *Obj
,
420 std::function
<void()> PrintHeader
);
421 void printFunctionStackSize(const ELFObjectFile
<ELFT
> *Obj
, uint64_t SymValue
,
422 SectionRef FunctionSec
,
423 const StringRef SectionName
, DataExtractor Data
,
425 void printStackSize(const ELFObjectFile
<ELFT
> *Obj
, RelocationRef Rel
,
426 SectionRef FunctionSec
,
427 const StringRef
&StackSizeSectionName
,
428 const RelocationResolver
&Resolver
, DataExtractor Data
);
429 virtual void printStackSizeEntry(uint64_t Size
, StringRef FuncName
) = 0;
430 virtual void printMipsGOT(const MipsGOTParser
<ELFT
> &Parser
) = 0;
431 virtual void printMipsPLT(const MipsGOTParser
<ELFT
> &Parser
) = 0;
432 virtual void printMipsABIFlags(const ELFObjectFile
<ELFT
> *Obj
) = 0;
433 const ELFDumper
<ELFT
> *dumper() const { return Dumper
; }
436 std::function
<Error(const Twine
&Msg
)> WarningHandler
;
440 std::unordered_set
<std::string
> Warnings
;
441 const ELFDumper
<ELFT
> *Dumper
;
444 template <typename ELFT
> class GNUStyle
: public DumpStyle
<ELFT
> {
445 formatted_raw_ostream
&OS
;
448 TYPEDEF_ELF_TYPES(ELFT
)
450 GNUStyle(ScopedPrinter
&W
, ELFDumper
<ELFT
> *Dumper
)
451 : DumpStyle
<ELFT
>(Dumper
),
452 OS(static_cast<formatted_raw_ostream
&>(W
.getOStream())) {
453 assert (&W
.getOStream() == &llvm::fouts());
456 void printFileHeaders(const ELFO
*Obj
) override
;
457 void printGroupSections(const ELFFile
<ELFT
> *Obj
) override
;
458 void printRelocations(const ELFO
*Obj
) override
;
459 void printSectionHeaders(const ELFO
*Obj
) override
;
460 void printSymbols(const ELFO
*Obj
, bool PrintSymbols
,
461 bool PrintDynamicSymbols
) override
;
462 void printHashSymbols(const ELFO
*Obj
) override
;
463 void printDynamic(const ELFFile
<ELFT
> *Obj
) override
;
464 void printDynamicRelocations(const ELFO
*Obj
) override
;
465 void printSymtabMessage(const ELFO
*Obj
, StringRef Name
, size_t Offset
,
466 bool NonVisibilityBitsUsed
) override
;
467 void printProgramHeaders(const ELFO
*Obj
, bool PrintProgramHeaders
,
468 cl::boolOrDefault PrintSectionMapping
) override
;
469 void printVersionSymbolSection(const ELFFile
<ELFT
> *Obj
,
470 const Elf_Shdr
*Sec
) override
;
471 void printVersionDefinitionSection(const ELFFile
<ELFT
> *Obj
,
472 const Elf_Shdr
*Sec
) override
;
473 void printVersionDependencySection(const ELFFile
<ELFT
> *Obj
,
474 const Elf_Shdr
*Sec
) override
;
475 void printHashHistogram(const ELFFile
<ELFT
> *Obj
) override
;
476 void printCGProfile(const ELFFile
<ELFT
> *Obj
) override
;
477 void printAddrsig(const ELFFile
<ELFT
> *Obj
) override
;
478 void printNotes(const ELFFile
<ELFT
> *Obj
) override
;
479 void printELFLinkerOptions(const ELFFile
<ELFT
> *Obj
) override
;
480 void printStackSizes(const ELFObjectFile
<ELFT
> *Obj
) override
;
481 void printStackSizeEntry(uint64_t Size
, StringRef FuncName
) override
;
482 void printMipsGOT(const MipsGOTParser
<ELFT
> &Parser
) override
;
483 void printMipsPLT(const MipsGOTParser
<ELFT
> &Parser
) override
;
484 void printMipsABIFlags(const ELFObjectFile
<ELFT
> *Obj
) override
;
491 Field(StringRef S
, unsigned Col
) : Str(S
), Column(Col
) {}
492 Field(unsigned Col
) : Column(Col
) {}
495 template <typename T
, typename TEnum
>
496 std::string
printEnum(T Value
, ArrayRef
<EnumEntry
<TEnum
>> EnumValues
) {
497 for (const auto &EnumItem
: EnumValues
)
498 if (EnumItem
.Value
== Value
)
499 return EnumItem
.AltName
;
500 return to_hexString(Value
, false);
503 template <typename T
, typename TEnum
>
504 std::string
printFlags(T Value
, ArrayRef
<EnumEntry
<TEnum
>> EnumValues
,
505 TEnum EnumMask1
= {}, TEnum EnumMask2
= {},
506 TEnum EnumMask3
= {}) {
508 for (const auto &Flag
: EnumValues
) {
513 if (Flag
.Value
& EnumMask1
)
514 EnumMask
= EnumMask1
;
515 else if (Flag
.Value
& EnumMask2
)
516 EnumMask
= EnumMask2
;
517 else if (Flag
.Value
& EnumMask3
)
518 EnumMask
= EnumMask3
;
519 bool IsEnum
= (Flag
.Value
& EnumMask
) != 0;
520 if ((!IsEnum
&& (Value
& Flag
.Value
) == Flag
.Value
) ||
521 (IsEnum
&& (Value
& EnumMask
) == Flag
.Value
)) {
530 formatted_raw_ostream
&printField(struct Field F
) {
532 OS
.PadToColumn(F
.Column
);
537 void printHashedSymbol(const ELFO
*Obj
, const Elf_Sym
*FirstSym
, uint32_t Sym
,
538 StringRef StrTable
, uint32_t Bucket
);
539 void printRelocHeader(unsigned SType
);
540 void printRelocation(const ELFO
*Obj
, const Elf_Shdr
*SymTab
,
541 const Elf_Rela
&R
, bool IsRela
);
542 void printRelocation(const ELFO
*Obj
, const Elf_Sym
*Sym
,
543 StringRef SymbolName
, const Elf_Rela
&R
, bool IsRela
);
544 void printSymbol(const ELFO
*Obj
, const Elf_Sym
*Symbol
, const Elf_Sym
*First
,
545 StringRef StrTable
, bool IsDynamic
,
546 bool NonVisibilityBitsUsed
) override
;
547 std::string
getSymbolSectionNdx(const ELFO
*Obj
, const Elf_Sym
*Symbol
,
548 const Elf_Sym
*FirstSym
);
549 void printDynamicRelocation(const ELFO
*Obj
, Elf_Rela R
, bool IsRela
);
550 bool checkTLSSections(const Elf_Phdr
&Phdr
, const Elf_Shdr
&Sec
);
551 bool checkoffsets(const Elf_Phdr
&Phdr
, const Elf_Shdr
&Sec
);
552 bool checkVMA(const Elf_Phdr
&Phdr
, const Elf_Shdr
&Sec
);
553 bool checkPTDynamic(const Elf_Phdr
&Phdr
, const Elf_Shdr
&Sec
);
554 void printProgramHeaders(const ELFO
*Obj
);
555 void printSectionMapping(const ELFO
*Obj
);
558 template <typename ELFT
> class LLVMStyle
: public DumpStyle
<ELFT
> {
560 TYPEDEF_ELF_TYPES(ELFT
)
562 LLVMStyle(ScopedPrinter
&W
, ELFDumper
<ELFT
> *Dumper
)
563 : DumpStyle
<ELFT
>(Dumper
), W(W
) {}
565 void printFileHeaders(const ELFO
*Obj
) override
;
566 void printGroupSections(const ELFFile
<ELFT
> *Obj
) override
;
567 void printRelocations(const ELFO
*Obj
) override
;
568 void printRelocations(const Elf_Shdr
*Sec
, const ELFO
*Obj
);
569 void printSectionHeaders(const ELFO
*Obj
) override
;
570 void printSymbols(const ELFO
*Obj
, bool PrintSymbols
,
571 bool PrintDynamicSymbols
) override
;
572 void printDynamic(const ELFFile
<ELFT
> *Obj
) override
;
573 void printDynamicRelocations(const ELFO
*Obj
) override
;
574 void printProgramHeaders(const ELFO
*Obj
, bool PrintProgramHeaders
,
575 cl::boolOrDefault PrintSectionMapping
) override
;
576 void printVersionSymbolSection(const ELFFile
<ELFT
> *Obj
,
577 const Elf_Shdr
*Sec
) override
;
578 void printVersionDefinitionSection(const ELFFile
<ELFT
> *Obj
,
579 const Elf_Shdr
*Sec
) override
;
580 void printVersionDependencySection(const ELFFile
<ELFT
> *Obj
,
581 const Elf_Shdr
*Sec
) override
;
582 void printHashHistogram(const ELFFile
<ELFT
> *Obj
) override
;
583 void printCGProfile(const ELFFile
<ELFT
> *Obj
) override
;
584 void printAddrsig(const ELFFile
<ELFT
> *Obj
) override
;
585 void printNotes(const ELFFile
<ELFT
> *Obj
) override
;
586 void printELFLinkerOptions(const ELFFile
<ELFT
> *Obj
) override
;
587 void printStackSizes(const ELFObjectFile
<ELFT
> *Obj
) override
;
588 void printStackSizeEntry(uint64_t Size
, StringRef FuncName
) override
;
589 void printMipsGOT(const MipsGOTParser
<ELFT
> &Parser
) override
;
590 void printMipsPLT(const MipsGOTParser
<ELFT
> &Parser
) override
;
591 void printMipsABIFlags(const ELFObjectFile
<ELFT
> *Obj
) override
;
594 void printRelocation(const ELFO
*Obj
, Elf_Rela Rel
, const Elf_Shdr
*SymTab
);
595 void printDynamicRelocation(const ELFO
*Obj
, Elf_Rela Rel
);
596 void printSymbols(const ELFO
*Obj
);
597 void printDynamicSymbols(const ELFO
*Obj
);
598 void printSymbol(const ELFO
*Obj
, const Elf_Sym
*Symbol
, const Elf_Sym
*First
,
599 StringRef StrTable
, bool IsDynamic
,
600 bool /*NonVisibilityBitsUsed*/) override
;
601 void printProgramHeaders(const ELFO
*Obj
);
602 void printSectionMapping(const ELFO
*Obj
) {}
607 } // end anonymous namespace
611 template <class ELFT
>
612 static std::error_code
createELFDumper(const ELFObjectFile
<ELFT
> *Obj
,
613 ScopedPrinter
&Writer
,
614 std::unique_ptr
<ObjDumper
> &Result
) {
615 Result
.reset(new ELFDumper
<ELFT
>(Obj
, Writer
));
616 return readobj_error::success
;
619 std::error_code
createELFDumper(const object::ObjectFile
*Obj
,
620 ScopedPrinter
&Writer
,
621 std::unique_ptr
<ObjDumper
> &Result
) {
622 // Little-endian 32-bit
623 if (const ELF32LEObjectFile
*ELFObj
= dyn_cast
<ELF32LEObjectFile
>(Obj
))
624 return createELFDumper(ELFObj
, Writer
, Result
);
627 if (const ELF32BEObjectFile
*ELFObj
= dyn_cast
<ELF32BEObjectFile
>(Obj
))
628 return createELFDumper(ELFObj
, Writer
, Result
);
630 // Little-endian 64-bit
631 if (const ELF64LEObjectFile
*ELFObj
= dyn_cast
<ELF64LEObjectFile
>(Obj
))
632 return createELFDumper(ELFObj
, Writer
, Result
);
635 if (const ELF64BEObjectFile
*ELFObj
= dyn_cast
<ELF64BEObjectFile
>(Obj
))
636 return createELFDumper(ELFObj
, Writer
, Result
);
638 return readobj_error::unsupported_obj_file_format
;
641 } // end namespace llvm
643 // Iterate through the versions needed section, and place each Elf_Vernaux
644 // in the VersionMap according to its index.
645 template <class ELFT
>
646 void ELFDumper
<ELFT
>::LoadVersionNeeds(const Elf_Shdr
*Sec
) const {
647 unsigned VerneedSize
= Sec
->sh_size
; // Size of section in bytes
648 unsigned VerneedEntries
= Sec
->sh_info
; // Number of Verneed entries
649 const uint8_t *VerneedStart
= reinterpret_cast<const uint8_t *>(
650 ObjF
->getELFFile()->base() + Sec
->sh_offset
);
651 const uint8_t *VerneedEnd
= VerneedStart
+ VerneedSize
;
652 // The first Verneed entry is at the start of the section.
653 const uint8_t *VerneedBuf
= VerneedStart
;
654 for (unsigned VerneedIndex
= 0; VerneedIndex
< VerneedEntries
;
656 if (VerneedBuf
+ sizeof(Elf_Verneed
) > VerneedEnd
)
657 report_fatal_error("Section ended unexpectedly while scanning "
658 "version needed records.");
659 const Elf_Verneed
*Verneed
=
660 reinterpret_cast<const Elf_Verneed
*>(VerneedBuf
);
661 if (Verneed
->vn_version
!= ELF::VER_NEED_CURRENT
)
662 report_fatal_error("Unexpected verneed version");
663 // Iterate through the Vernaux entries
664 const uint8_t *VernauxBuf
= VerneedBuf
+ Verneed
->vn_aux
;
665 for (unsigned VernauxIndex
= 0; VernauxIndex
< Verneed
->vn_cnt
;
667 if (VernauxBuf
+ sizeof(Elf_Vernaux
) > VerneedEnd
)
668 report_fatal_error("Section ended unexpected while scanning auxiliary "
669 "version needed records.");
670 const Elf_Vernaux
*Vernaux
=
671 reinterpret_cast<const Elf_Vernaux
*>(VernauxBuf
);
672 size_t Index
= Vernaux
->vna_other
& ELF::VERSYM_VERSION
;
673 if (Index
>= VersionMap
.size())
674 VersionMap
.resize(Index
+ 1);
675 VersionMap
[Index
] = VersionMapEntry(Vernaux
);
676 VernauxBuf
+= Vernaux
->vna_next
;
678 VerneedBuf
+= Verneed
->vn_next
;
682 // Iterate through the version definitions, and place each Elf_Verdef
683 // in the VersionMap according to its index.
684 template <class ELFT
>
685 void ELFDumper
<ELFT
>::LoadVersionDefs(const Elf_Shdr
*Sec
) const {
686 unsigned VerdefSize
= Sec
->sh_size
; // Size of section in bytes
687 unsigned VerdefEntries
= Sec
->sh_info
; // Number of Verdef entries
688 const uint8_t *VerdefStart
= reinterpret_cast<const uint8_t *>(
689 ObjF
->getELFFile()->base() + Sec
->sh_offset
);
690 const uint8_t *VerdefEnd
= VerdefStart
+ VerdefSize
;
691 // The first Verdef entry is at the start of the section.
692 const uint8_t *VerdefBuf
= VerdefStart
;
693 for (unsigned VerdefIndex
= 0; VerdefIndex
< VerdefEntries
; ++VerdefIndex
) {
694 if (VerdefBuf
+ sizeof(Elf_Verdef
) > VerdefEnd
)
695 report_fatal_error("Section ended unexpectedly while scanning "
696 "version definitions.");
697 const Elf_Verdef
*Verdef
= reinterpret_cast<const Elf_Verdef
*>(VerdefBuf
);
698 if (Verdef
->vd_version
!= ELF::VER_DEF_CURRENT
)
699 report_fatal_error("Unexpected verdef version");
700 size_t Index
= Verdef
->vd_ndx
& ELF::VERSYM_VERSION
;
701 if (Index
>= VersionMap
.size())
702 VersionMap
.resize(Index
+ 1);
703 VersionMap
[Index
] = VersionMapEntry(Verdef
);
704 VerdefBuf
+= Verdef
->vd_next
;
708 template <class ELFT
> void ELFDumper
<ELFT
>::LoadVersionMap() const {
709 // If there is no dynamic symtab or version table, there is nothing to do.
710 if (!DynSymRegion
.Addr
|| !SymbolVersionSection
)
713 // Has the VersionMap already been loaded?
714 if (!VersionMap
.empty())
717 // The first two version indexes are reserved.
718 // Index 0 is LOCAL, index 1 is GLOBAL.
719 VersionMap
.push_back(VersionMapEntry());
720 VersionMap
.push_back(VersionMapEntry());
722 if (SymbolVersionDefSection
)
723 LoadVersionDefs(SymbolVersionDefSection
);
725 if (SymbolVersionNeedSection
)
726 LoadVersionNeeds(SymbolVersionNeedSection
);
729 template <typename ELFT
>
730 StringRef ELFDumper
<ELFT
>::getSymbolVersion(StringRef StrTab
,
732 bool &IsDefault
) const {
733 // This is a dynamic symbol. Look in the GNU symbol version table.
734 if (!SymbolVersionSection
) {
740 // Determine the position in the symbol table of this entry.
741 size_t EntryIndex
= (reinterpret_cast<uintptr_t>(Sym
) -
742 reinterpret_cast<uintptr_t>(DynSymRegion
.Addr
)) /
745 // Get the corresponding version index entry.
746 const Elf_Versym
*Versym
= unwrapOrError(
747 ObjF
->getFileName(), ObjF
->getELFFile()->template getEntry
<Elf_Versym
>(
748 SymbolVersionSection
, EntryIndex
));
749 return this->getSymbolVersionByIndex(StrTab
, Versym
->vs_index
, IsDefault
);
752 static std::string
maybeDemangle(StringRef Name
) {
753 return opts::Demangle
? demangle(Name
) : Name
.str();
756 template <typename ELFT
>
757 Expected
<std::string
>
758 ELFDumper
<ELFT
>::getStaticSymbolName(uint32_t Index
) const {
759 const ELFFile
<ELFT
> *Obj
= ObjF
->getELFFile();
760 Expected
<const typename
ELFT::Sym
*> SymOrErr
=
761 Obj
->getSymbol(DotSymtabSec
, Index
);
763 return SymOrErr
.takeError();
765 Expected
<StringRef
> StrTabOrErr
= Obj
->getStringTableForSymtab(*DotSymtabSec
);
767 return StrTabOrErr
.takeError();
769 Expected
<StringRef
> NameOrErr
= (*SymOrErr
)->getName(*StrTabOrErr
);
771 return NameOrErr
.takeError();
772 return maybeDemangle(*NameOrErr
);
775 template <typename ELFT
>
776 StringRef ELFDumper
<ELFT
>::getSymbolVersionByIndex(StringRef StrTab
,
777 uint32_t SymbolVersionIndex
,
778 bool &IsDefault
) const {
779 size_t VersionIndex
= SymbolVersionIndex
& VERSYM_VERSION
;
781 // Special markers for unversioned symbols.
782 if (VersionIndex
== VER_NDX_LOCAL
|| VersionIndex
== VER_NDX_GLOBAL
) {
787 // Lookup this symbol in the version table.
789 if (VersionIndex
>= VersionMap
.size() || VersionMap
[VersionIndex
].isNull())
790 reportError(createError("Invalid version entry"), ObjF
->getFileName());
791 const VersionMapEntry
&Entry
= VersionMap
[VersionIndex
];
793 // Get the version name string.
795 if (Entry
.isVerdef()) {
796 // The first Verdaux entry holds the name.
797 NameOffset
= Entry
.getVerdef()->getAux()->vda_name
;
798 IsDefault
= !(SymbolVersionIndex
& VERSYM_HIDDEN
);
800 NameOffset
= Entry
.getVernaux()->vna_name
;
803 if (NameOffset
>= StrTab
.size())
804 reportError(createError("Invalid string offset"), ObjF
->getFileName());
805 return StrTab
.data() + NameOffset
;
808 template <typename ELFT
>
809 std::string ELFDumper
<ELFT
>::getFullSymbolName(const Elf_Sym
*Symbol
,
811 bool IsDynamic
) const {
812 std::string SymbolName
= maybeDemangle(
813 unwrapOrError(ObjF
->getFileName(), Symbol
->getName(StrTable
)));
815 if (SymbolName
.empty() && Symbol
->getType() == ELF::STT_SECTION
) {
816 unsigned SectionIndex
;
817 StringRef SectionName
;
818 Elf_Sym_Range Syms
= unwrapOrError(
819 ObjF
->getFileName(), ObjF
->getELFFile()->symbols(DotSymtabSec
));
820 getSectionNameIndex(Symbol
, Syms
.begin(), SectionName
, SectionIndex
);
828 StringRef Version
= getSymbolVersion(StrTable
, &*Symbol
, IsDefault
);
829 if (!Version
.empty()) {
830 SymbolName
+= (IsDefault
? "@@" : "@");
831 SymbolName
+= Version
;
836 template <typename ELFT
>
837 void ELFDumper
<ELFT
>::getSectionNameIndex(const Elf_Sym
*Symbol
,
838 const Elf_Sym
*FirstSym
,
839 StringRef
&SectionName
,
840 unsigned &SectionIndex
) const {
841 SectionIndex
= Symbol
->st_shndx
;
842 if (Symbol
->isUndefined())
843 SectionName
= "Undefined";
844 else if (Symbol
->isProcessorSpecific())
845 SectionName
= "Processor Specific";
846 else if (Symbol
->isOSSpecific())
847 SectionName
= "Operating System Specific";
848 else if (Symbol
->isAbsolute())
849 SectionName
= "Absolute";
850 else if (Symbol
->isCommon())
851 SectionName
= "Common";
852 else if (Symbol
->isReserved() && SectionIndex
!= SHN_XINDEX
)
853 SectionName
= "Reserved";
855 if (SectionIndex
== SHN_XINDEX
)
856 SectionIndex
= unwrapOrError(ObjF
->getFileName(),
857 object::getExtendedSymbolTableIndex
<ELFT
>(
858 Symbol
, FirstSym
, ShndxTable
));
859 const ELFFile
<ELFT
> *Obj
= ObjF
->getELFFile();
860 const typename
ELFT::Shdr
*Sec
=
861 unwrapOrError(ObjF
->getFileName(), Obj
->getSection(SectionIndex
));
862 SectionName
= unwrapOrError(ObjF
->getFileName(), Obj
->getSectionName(Sec
));
866 template <class ELFO
>
867 static const typename
ELFO::Elf_Shdr
*
868 findNotEmptySectionByAddress(const ELFO
*Obj
, StringRef FileName
,
870 for (const auto &Shdr
: unwrapOrError(FileName
, Obj
->sections()))
871 if (Shdr
.sh_addr
== Addr
&& Shdr
.sh_size
> 0)
876 template <class ELFO
>
877 static const typename
ELFO::Elf_Shdr
*
878 findSectionByName(const ELFO
&Obj
, StringRef FileName
, StringRef Name
) {
879 for (const auto &Shdr
: unwrapOrError(FileName
, Obj
.sections()))
880 if (Name
== unwrapOrError(FileName
, Obj
.getSectionName(&Shdr
)))
885 static const EnumEntry
<unsigned> ElfClass
[] = {
886 {"None", "none", ELF::ELFCLASSNONE
},
887 {"32-bit", "ELF32", ELF::ELFCLASS32
},
888 {"64-bit", "ELF64", ELF::ELFCLASS64
},
891 static const EnumEntry
<unsigned> ElfDataEncoding
[] = {
892 {"None", "none", ELF::ELFDATANONE
},
893 {"LittleEndian", "2's complement, little endian", ELF::ELFDATA2LSB
},
894 {"BigEndian", "2's complement, big endian", ELF::ELFDATA2MSB
},
897 static const EnumEntry
<unsigned> ElfObjectFileType
[] = {
898 {"None", "NONE (none)", ELF::ET_NONE
},
899 {"Relocatable", "REL (Relocatable file)", ELF::ET_REL
},
900 {"Executable", "EXEC (Executable file)", ELF::ET_EXEC
},
901 {"SharedObject", "DYN (Shared object file)", ELF::ET_DYN
},
902 {"Core", "CORE (Core file)", ELF::ET_CORE
},
905 static const EnumEntry
<unsigned> ElfOSABI
[] = {
906 {"SystemV", "UNIX - System V", ELF::ELFOSABI_NONE
},
907 {"HPUX", "UNIX - HP-UX", ELF::ELFOSABI_HPUX
},
908 {"NetBSD", "UNIX - NetBSD", ELF::ELFOSABI_NETBSD
},
909 {"GNU/Linux", "UNIX - GNU", ELF::ELFOSABI_LINUX
},
910 {"GNU/Hurd", "GNU/Hurd", ELF::ELFOSABI_HURD
},
911 {"Solaris", "UNIX - Solaris", ELF::ELFOSABI_SOLARIS
},
912 {"AIX", "UNIX - AIX", ELF::ELFOSABI_AIX
},
913 {"IRIX", "UNIX - IRIX", ELF::ELFOSABI_IRIX
},
914 {"FreeBSD", "UNIX - FreeBSD", ELF::ELFOSABI_FREEBSD
},
915 {"TRU64", "UNIX - TRU64", ELF::ELFOSABI_TRU64
},
916 {"Modesto", "Novell - Modesto", ELF::ELFOSABI_MODESTO
},
917 {"OpenBSD", "UNIX - OpenBSD", ELF::ELFOSABI_OPENBSD
},
918 {"OpenVMS", "VMS - OpenVMS", ELF::ELFOSABI_OPENVMS
},
919 {"NSK", "HP - Non-Stop Kernel", ELF::ELFOSABI_NSK
},
920 {"AROS", "AROS", ELF::ELFOSABI_AROS
},
921 {"FenixOS", "FenixOS", ELF::ELFOSABI_FENIXOS
},
922 {"CloudABI", "CloudABI", ELF::ELFOSABI_CLOUDABI
},
923 {"Standalone", "Standalone App", ELF::ELFOSABI_STANDALONE
}
926 static const EnumEntry
<unsigned> SymVersionFlags
[] = {
927 {"Base", "BASE", VER_FLG_BASE
},
928 {"Weak", "WEAK", VER_FLG_WEAK
},
929 {"Info", "INFO", VER_FLG_INFO
}};
931 static const EnumEntry
<unsigned> AMDGPUElfOSABI
[] = {
932 {"AMDGPU_HSA", "AMDGPU - HSA", ELF::ELFOSABI_AMDGPU_HSA
},
933 {"AMDGPU_PAL", "AMDGPU - PAL", ELF::ELFOSABI_AMDGPU_PAL
},
934 {"AMDGPU_MESA3D", "AMDGPU - MESA3D", ELF::ELFOSABI_AMDGPU_MESA3D
}
937 static const EnumEntry
<unsigned> ARMElfOSABI
[] = {
938 {"ARM", "ARM", ELF::ELFOSABI_ARM
}
941 static const EnumEntry
<unsigned> C6000ElfOSABI
[] = {
942 {"C6000_ELFABI", "Bare-metal C6000", ELF::ELFOSABI_C6000_ELFABI
},
943 {"C6000_LINUX", "Linux C6000", ELF::ELFOSABI_C6000_LINUX
}
946 static const EnumEntry
<unsigned> ElfMachineType
[] = {
947 ENUM_ENT(EM_NONE
, "None"),
948 ENUM_ENT(EM_M32
, "WE32100"),
949 ENUM_ENT(EM_SPARC
, "Sparc"),
950 ENUM_ENT(EM_386
, "Intel 80386"),
951 ENUM_ENT(EM_68K
, "MC68000"),
952 ENUM_ENT(EM_88K
, "MC88000"),
953 ENUM_ENT(EM_IAMCU
, "EM_IAMCU"),
954 ENUM_ENT(EM_860
, "Intel 80860"),
955 ENUM_ENT(EM_MIPS
, "MIPS R3000"),
956 ENUM_ENT(EM_S370
, "IBM System/370"),
957 ENUM_ENT(EM_MIPS_RS3_LE
, "MIPS R3000 little-endian"),
958 ENUM_ENT(EM_PARISC
, "HPPA"),
959 ENUM_ENT(EM_VPP500
, "Fujitsu VPP500"),
960 ENUM_ENT(EM_SPARC32PLUS
, "Sparc v8+"),
961 ENUM_ENT(EM_960
, "Intel 80960"),
962 ENUM_ENT(EM_PPC
, "PowerPC"),
963 ENUM_ENT(EM_PPC64
, "PowerPC64"),
964 ENUM_ENT(EM_S390
, "IBM S/390"),
965 ENUM_ENT(EM_SPU
, "SPU"),
966 ENUM_ENT(EM_V800
, "NEC V800 series"),
967 ENUM_ENT(EM_FR20
, "Fujistsu FR20"),
968 ENUM_ENT(EM_RH32
, "TRW RH-32"),
969 ENUM_ENT(EM_RCE
, "Motorola RCE"),
970 ENUM_ENT(EM_ARM
, "ARM"),
971 ENUM_ENT(EM_ALPHA
, "EM_ALPHA"),
972 ENUM_ENT(EM_SH
, "Hitachi SH"),
973 ENUM_ENT(EM_SPARCV9
, "Sparc v9"),
974 ENUM_ENT(EM_TRICORE
, "Siemens Tricore"),
975 ENUM_ENT(EM_ARC
, "ARC"),
976 ENUM_ENT(EM_H8_300
, "Hitachi H8/300"),
977 ENUM_ENT(EM_H8_300H
, "Hitachi H8/300H"),
978 ENUM_ENT(EM_H8S
, "Hitachi H8S"),
979 ENUM_ENT(EM_H8_500
, "Hitachi H8/500"),
980 ENUM_ENT(EM_IA_64
, "Intel IA-64"),
981 ENUM_ENT(EM_MIPS_X
, "Stanford MIPS-X"),
982 ENUM_ENT(EM_COLDFIRE
, "Motorola Coldfire"),
983 ENUM_ENT(EM_68HC12
, "Motorola MC68HC12 Microcontroller"),
984 ENUM_ENT(EM_MMA
, "Fujitsu Multimedia Accelerator"),
985 ENUM_ENT(EM_PCP
, "Siemens PCP"),
986 ENUM_ENT(EM_NCPU
, "Sony nCPU embedded RISC processor"),
987 ENUM_ENT(EM_NDR1
, "Denso NDR1 microprocesspr"),
988 ENUM_ENT(EM_STARCORE
, "Motorola Star*Core processor"),
989 ENUM_ENT(EM_ME16
, "Toyota ME16 processor"),
990 ENUM_ENT(EM_ST100
, "STMicroelectronics ST100 processor"),
991 ENUM_ENT(EM_TINYJ
, "Advanced Logic Corp. TinyJ embedded processor"),
992 ENUM_ENT(EM_X86_64
, "Advanced Micro Devices X86-64"),
993 ENUM_ENT(EM_PDSP
, "Sony DSP processor"),
994 ENUM_ENT(EM_PDP10
, "Digital Equipment Corp. PDP-10"),
995 ENUM_ENT(EM_PDP11
, "Digital Equipment Corp. PDP-11"),
996 ENUM_ENT(EM_FX66
, "Siemens FX66 microcontroller"),
997 ENUM_ENT(EM_ST9PLUS
, "STMicroelectronics ST9+ 8/16 bit microcontroller"),
998 ENUM_ENT(EM_ST7
, "STMicroelectronics ST7 8-bit microcontroller"),
999 ENUM_ENT(EM_68HC16
, "Motorola MC68HC16 Microcontroller"),
1000 ENUM_ENT(EM_68HC11
, "Motorola MC68HC11 Microcontroller"),
1001 ENUM_ENT(EM_68HC08
, "Motorola MC68HC08 Microcontroller"),
1002 ENUM_ENT(EM_68HC05
, "Motorola MC68HC05 Microcontroller"),
1003 ENUM_ENT(EM_SVX
, "Silicon Graphics SVx"),
1004 ENUM_ENT(EM_ST19
, "STMicroelectronics ST19 8-bit microcontroller"),
1005 ENUM_ENT(EM_VAX
, "Digital VAX"),
1006 ENUM_ENT(EM_CRIS
, "Axis Communications 32-bit embedded processor"),
1007 ENUM_ENT(EM_JAVELIN
, "Infineon Technologies 32-bit embedded cpu"),
1008 ENUM_ENT(EM_FIREPATH
, "Element 14 64-bit DSP processor"),
1009 ENUM_ENT(EM_ZSP
, "LSI Logic's 16-bit DSP processor"),
1010 ENUM_ENT(EM_MMIX
, "Donald Knuth's educational 64-bit processor"),
1011 ENUM_ENT(EM_HUANY
, "Harvard Universitys's machine-independent object format"),
1012 ENUM_ENT(EM_PRISM
, "Vitesse Prism"),
1013 ENUM_ENT(EM_AVR
, "Atmel AVR 8-bit microcontroller"),
1014 ENUM_ENT(EM_FR30
, "Fujitsu FR30"),
1015 ENUM_ENT(EM_D10V
, "Mitsubishi D10V"),
1016 ENUM_ENT(EM_D30V
, "Mitsubishi D30V"),
1017 ENUM_ENT(EM_V850
, "NEC v850"),
1018 ENUM_ENT(EM_M32R
, "Renesas M32R (formerly Mitsubishi M32r)"),
1019 ENUM_ENT(EM_MN10300
, "Matsushita MN10300"),
1020 ENUM_ENT(EM_MN10200
, "Matsushita MN10200"),
1021 ENUM_ENT(EM_PJ
, "picoJava"),
1022 ENUM_ENT(EM_OPENRISC
, "OpenRISC 32-bit embedded processor"),
1023 ENUM_ENT(EM_ARC_COMPACT
, "EM_ARC_COMPACT"),
1024 ENUM_ENT(EM_XTENSA
, "Tensilica Xtensa Processor"),
1025 ENUM_ENT(EM_VIDEOCORE
, "Alphamosaic VideoCore processor"),
1026 ENUM_ENT(EM_TMM_GPP
, "Thompson Multimedia General Purpose Processor"),
1027 ENUM_ENT(EM_NS32K
, "National Semiconductor 32000 series"),
1028 ENUM_ENT(EM_TPC
, "Tenor Network TPC processor"),
1029 ENUM_ENT(EM_SNP1K
, "EM_SNP1K"),
1030 ENUM_ENT(EM_ST200
, "STMicroelectronics ST200 microcontroller"),
1031 ENUM_ENT(EM_IP2K
, "Ubicom IP2xxx 8-bit microcontrollers"),
1032 ENUM_ENT(EM_MAX
, "MAX Processor"),
1033 ENUM_ENT(EM_CR
, "National Semiconductor CompactRISC"),
1034 ENUM_ENT(EM_F2MC16
, "Fujitsu F2MC16"),
1035 ENUM_ENT(EM_MSP430
, "Texas Instruments msp430 microcontroller"),
1036 ENUM_ENT(EM_BLACKFIN
, "Analog Devices Blackfin"),
1037 ENUM_ENT(EM_SE_C33
, "S1C33 Family of Seiko Epson processors"),
1038 ENUM_ENT(EM_SEP
, "Sharp embedded microprocessor"),
1039 ENUM_ENT(EM_ARCA
, "Arca RISC microprocessor"),
1040 ENUM_ENT(EM_UNICORE
, "Unicore"),
1041 ENUM_ENT(EM_EXCESS
, "eXcess 16/32/64-bit configurable embedded CPU"),
1042 ENUM_ENT(EM_DXP
, "Icera Semiconductor Inc. Deep Execution Processor"),
1043 ENUM_ENT(EM_ALTERA_NIOS2
, "Altera Nios"),
1044 ENUM_ENT(EM_CRX
, "National Semiconductor CRX microprocessor"),
1045 ENUM_ENT(EM_XGATE
, "Motorola XGATE embedded processor"),
1046 ENUM_ENT(EM_C166
, "Infineon Technologies xc16x"),
1047 ENUM_ENT(EM_M16C
, "Renesas M16C"),
1048 ENUM_ENT(EM_DSPIC30F
, "Microchip Technology dsPIC30F Digital Signal Controller"),
1049 ENUM_ENT(EM_CE
, "Freescale Communication Engine RISC core"),
1050 ENUM_ENT(EM_M32C
, "Renesas M32C"),
1051 ENUM_ENT(EM_TSK3000
, "Altium TSK3000 core"),
1052 ENUM_ENT(EM_RS08
, "Freescale RS08 embedded processor"),
1053 ENUM_ENT(EM_SHARC
, "EM_SHARC"),
1054 ENUM_ENT(EM_ECOG2
, "Cyan Technology eCOG2 microprocessor"),
1055 ENUM_ENT(EM_SCORE7
, "SUNPLUS S+Core"),
1056 ENUM_ENT(EM_DSP24
, "New Japan Radio (NJR) 24-bit DSP Processor"),
1057 ENUM_ENT(EM_VIDEOCORE3
, "Broadcom VideoCore III processor"),
1058 ENUM_ENT(EM_LATTICEMICO32
, "Lattice Mico32"),
1059 ENUM_ENT(EM_SE_C17
, "Seiko Epson C17 family"),
1060 ENUM_ENT(EM_TI_C6000
, "Texas Instruments TMS320C6000 DSP family"),
1061 ENUM_ENT(EM_TI_C2000
, "Texas Instruments TMS320C2000 DSP family"),
1062 ENUM_ENT(EM_TI_C5500
, "Texas Instruments TMS320C55x DSP family"),
1063 ENUM_ENT(EM_MMDSP_PLUS
, "STMicroelectronics 64bit VLIW Data Signal Processor"),
1064 ENUM_ENT(EM_CYPRESS_M8C
, "Cypress M8C microprocessor"),
1065 ENUM_ENT(EM_R32C
, "Renesas R32C series microprocessors"),
1066 ENUM_ENT(EM_TRIMEDIA
, "NXP Semiconductors TriMedia architecture family"),
1067 ENUM_ENT(EM_HEXAGON
, "Qualcomm Hexagon"),
1068 ENUM_ENT(EM_8051
, "Intel 8051 and variants"),
1069 ENUM_ENT(EM_STXP7X
, "STMicroelectronics STxP7x family"),
1070 ENUM_ENT(EM_NDS32
, "Andes Technology compact code size embedded RISC processor family"),
1071 ENUM_ENT(EM_ECOG1
, "Cyan Technology eCOG1 microprocessor"),
1072 ENUM_ENT(EM_ECOG1X
, "Cyan Technology eCOG1X family"),
1073 ENUM_ENT(EM_MAXQ30
, "Dallas Semiconductor MAXQ30 Core microcontrollers"),
1074 ENUM_ENT(EM_XIMO16
, "New Japan Radio (NJR) 16-bit DSP Processor"),
1075 ENUM_ENT(EM_MANIK
, "M2000 Reconfigurable RISC Microprocessor"),
1076 ENUM_ENT(EM_CRAYNV2
, "Cray Inc. NV2 vector architecture"),
1077 ENUM_ENT(EM_RX
, "Renesas RX"),
1078 ENUM_ENT(EM_METAG
, "Imagination Technologies Meta processor architecture"),
1079 ENUM_ENT(EM_MCST_ELBRUS
, "MCST Elbrus general purpose hardware architecture"),
1080 ENUM_ENT(EM_ECOG16
, "Cyan Technology eCOG16 family"),
1081 ENUM_ENT(EM_CR16
, "Xilinx MicroBlaze"),
1082 ENUM_ENT(EM_ETPU
, "Freescale Extended Time Processing Unit"),
1083 ENUM_ENT(EM_SLE9X
, "Infineon Technologies SLE9X core"),
1084 ENUM_ENT(EM_L10M
, "EM_L10M"),
1085 ENUM_ENT(EM_K10M
, "EM_K10M"),
1086 ENUM_ENT(EM_AARCH64
, "AArch64"),
1087 ENUM_ENT(EM_AVR32
, "Atmel Corporation 32-bit microprocessor family"),
1088 ENUM_ENT(EM_STM8
, "STMicroeletronics STM8 8-bit microcontroller"),
1089 ENUM_ENT(EM_TILE64
, "Tilera TILE64 multicore architecture family"),
1090 ENUM_ENT(EM_TILEPRO
, "Tilera TILEPro multicore architecture family"),
1091 ENUM_ENT(EM_CUDA
, "NVIDIA CUDA architecture"),
1092 ENUM_ENT(EM_TILEGX
, "Tilera TILE-Gx multicore architecture family"),
1093 ENUM_ENT(EM_CLOUDSHIELD
, "EM_CLOUDSHIELD"),
1094 ENUM_ENT(EM_COREA_1ST
, "EM_COREA_1ST"),
1095 ENUM_ENT(EM_COREA_2ND
, "EM_COREA_2ND"),
1096 ENUM_ENT(EM_ARC_COMPACT2
, "EM_ARC_COMPACT2"),
1097 ENUM_ENT(EM_OPEN8
, "EM_OPEN8"),
1098 ENUM_ENT(EM_RL78
, "Renesas RL78"),
1099 ENUM_ENT(EM_VIDEOCORE5
, "Broadcom VideoCore V processor"),
1100 ENUM_ENT(EM_78KOR
, "EM_78KOR"),
1101 ENUM_ENT(EM_56800EX
, "EM_56800EX"),
1102 ENUM_ENT(EM_AMDGPU
, "EM_AMDGPU"),
1103 ENUM_ENT(EM_RISCV
, "RISC-V"),
1104 ENUM_ENT(EM_LANAI
, "EM_LANAI"),
1105 ENUM_ENT(EM_BPF
, "EM_BPF"),
1108 static const EnumEntry
<unsigned> ElfSymbolBindings
[] = {
1109 {"Local", "LOCAL", ELF::STB_LOCAL
},
1110 {"Global", "GLOBAL", ELF::STB_GLOBAL
},
1111 {"Weak", "WEAK", ELF::STB_WEAK
},
1112 {"Unique", "UNIQUE", ELF::STB_GNU_UNIQUE
}};
1114 static const EnumEntry
<unsigned> ElfSymbolVisibilities
[] = {
1115 {"DEFAULT", "DEFAULT", ELF::STV_DEFAULT
},
1116 {"INTERNAL", "INTERNAL", ELF::STV_INTERNAL
},
1117 {"HIDDEN", "HIDDEN", ELF::STV_HIDDEN
},
1118 {"PROTECTED", "PROTECTED", ELF::STV_PROTECTED
}};
1120 static const EnumEntry
<unsigned> AMDGPUSymbolTypes
[] = {
1121 { "AMDGPU_HSA_KERNEL", ELF::STT_AMDGPU_HSA_KERNEL
}
1124 static const char *getGroupType(uint32_t Flag
) {
1125 if (Flag
& ELF::GRP_COMDAT
)
1131 static const EnumEntry
<unsigned> ElfSectionFlags
[] = {
1132 ENUM_ENT(SHF_WRITE
, "W"),
1133 ENUM_ENT(SHF_ALLOC
, "A"),
1134 ENUM_ENT(SHF_EXCLUDE
, "E"),
1135 ENUM_ENT(SHF_EXECINSTR
, "X"),
1136 ENUM_ENT(SHF_MERGE
, "M"),
1137 ENUM_ENT(SHF_STRINGS
, "S"),
1138 ENUM_ENT(SHF_INFO_LINK
, "I"),
1139 ENUM_ENT(SHF_LINK_ORDER
, "L"),
1140 ENUM_ENT(SHF_OS_NONCONFORMING
, "o"),
1141 ENUM_ENT(SHF_GROUP
, "G"),
1142 ENUM_ENT(SHF_TLS
, "T"),
1143 ENUM_ENT(SHF_MASKOS
, "o"),
1144 ENUM_ENT(SHF_MASKPROC
, "p"),
1145 ENUM_ENT_1(SHF_COMPRESSED
),
1148 static const EnumEntry
<unsigned> ElfXCoreSectionFlags
[] = {
1149 LLVM_READOBJ_ENUM_ENT(ELF
, XCORE_SHF_CP_SECTION
),
1150 LLVM_READOBJ_ENUM_ENT(ELF
, XCORE_SHF_DP_SECTION
)
1153 static const EnumEntry
<unsigned> ElfARMSectionFlags
[] = {
1154 LLVM_READOBJ_ENUM_ENT(ELF
, SHF_ARM_PURECODE
)
1157 static const EnumEntry
<unsigned> ElfHexagonSectionFlags
[] = {
1158 LLVM_READOBJ_ENUM_ENT(ELF
, SHF_HEX_GPREL
)
1161 static const EnumEntry
<unsigned> ElfMipsSectionFlags
[] = {
1162 LLVM_READOBJ_ENUM_ENT(ELF
, SHF_MIPS_NODUPES
),
1163 LLVM_READOBJ_ENUM_ENT(ELF
, SHF_MIPS_NAMES
),
1164 LLVM_READOBJ_ENUM_ENT(ELF
, SHF_MIPS_LOCAL
),
1165 LLVM_READOBJ_ENUM_ENT(ELF
, SHF_MIPS_NOSTRIP
),
1166 LLVM_READOBJ_ENUM_ENT(ELF
, SHF_MIPS_GPREL
),
1167 LLVM_READOBJ_ENUM_ENT(ELF
, SHF_MIPS_MERGE
),
1168 LLVM_READOBJ_ENUM_ENT(ELF
, SHF_MIPS_ADDR
),
1169 LLVM_READOBJ_ENUM_ENT(ELF
, SHF_MIPS_STRING
)
1172 static const EnumEntry
<unsigned> ElfX86_64SectionFlags
[] = {
1173 LLVM_READOBJ_ENUM_ENT(ELF
, SHF_X86_64_LARGE
)
1176 static std::string
getGNUFlags(uint64_t Flags
) {
1178 for (auto Entry
: ElfSectionFlags
) {
1179 uint64_t Flag
= Entry
.Value
& Flags
;
1180 Flags
&= ~Entry
.Value
;
1182 case ELF::SHF_WRITE
:
1183 case ELF::SHF_ALLOC
:
1184 case ELF::SHF_EXECINSTR
:
1185 case ELF::SHF_MERGE
:
1186 case ELF::SHF_STRINGS
:
1187 case ELF::SHF_INFO_LINK
:
1188 case ELF::SHF_LINK_ORDER
:
1189 case ELF::SHF_OS_NONCONFORMING
:
1190 case ELF::SHF_GROUP
:
1192 case ELF::SHF_EXCLUDE
:
1193 Str
+= Entry
.AltName
;
1196 if (Flag
& ELF::SHF_MASKOS
)
1198 else if (Flag
& ELF::SHF_MASKPROC
)
1207 static const char *getElfSegmentType(unsigned Arch
, unsigned Type
) {
1208 // Check potentially overlapped processor-specific
1209 // program header type.
1212 switch (Type
) { LLVM_READOBJ_ENUM_CASE(ELF
, PT_ARM_EXIDX
); }
1215 case ELF::EM_MIPS_RS3_LE
:
1217 LLVM_READOBJ_ENUM_CASE(ELF
, PT_MIPS_REGINFO
);
1218 LLVM_READOBJ_ENUM_CASE(ELF
, PT_MIPS_RTPROC
);
1219 LLVM_READOBJ_ENUM_CASE(ELF
, PT_MIPS_OPTIONS
);
1220 LLVM_READOBJ_ENUM_CASE(ELF
, PT_MIPS_ABIFLAGS
);
1226 LLVM_READOBJ_ENUM_CASE(ELF
, PT_NULL
);
1227 LLVM_READOBJ_ENUM_CASE(ELF
, PT_LOAD
);
1228 LLVM_READOBJ_ENUM_CASE(ELF
, PT_DYNAMIC
);
1229 LLVM_READOBJ_ENUM_CASE(ELF
, PT_INTERP
);
1230 LLVM_READOBJ_ENUM_CASE(ELF
, PT_NOTE
);
1231 LLVM_READOBJ_ENUM_CASE(ELF
, PT_SHLIB
);
1232 LLVM_READOBJ_ENUM_CASE(ELF
, PT_PHDR
);
1233 LLVM_READOBJ_ENUM_CASE(ELF
, PT_TLS
);
1235 LLVM_READOBJ_ENUM_CASE(ELF
, PT_GNU_EH_FRAME
);
1236 LLVM_READOBJ_ENUM_CASE(ELF
, PT_SUNW_UNWIND
);
1238 LLVM_READOBJ_ENUM_CASE(ELF
, PT_GNU_STACK
);
1239 LLVM_READOBJ_ENUM_CASE(ELF
, PT_GNU_RELRO
);
1241 LLVM_READOBJ_ENUM_CASE(ELF
, PT_OPENBSD_RANDOMIZE
);
1242 LLVM_READOBJ_ENUM_CASE(ELF
, PT_OPENBSD_WXNEEDED
);
1243 LLVM_READOBJ_ENUM_CASE(ELF
, PT_OPENBSD_BOOTDATA
);
1250 static std::string
getElfPtType(unsigned Arch
, unsigned Type
) {
1252 LLVM_READOBJ_PHDR_ENUM(ELF
, PT_NULL
)
1253 LLVM_READOBJ_PHDR_ENUM(ELF
, PT_LOAD
)
1254 LLVM_READOBJ_PHDR_ENUM(ELF
, PT_DYNAMIC
)
1255 LLVM_READOBJ_PHDR_ENUM(ELF
, PT_INTERP
)
1256 LLVM_READOBJ_PHDR_ENUM(ELF
, PT_NOTE
)
1257 LLVM_READOBJ_PHDR_ENUM(ELF
, PT_SHLIB
)
1258 LLVM_READOBJ_PHDR_ENUM(ELF
, PT_PHDR
)
1259 LLVM_READOBJ_PHDR_ENUM(ELF
, PT_TLS
)
1260 LLVM_READOBJ_PHDR_ENUM(ELF
, PT_GNU_EH_FRAME
)
1261 LLVM_READOBJ_PHDR_ENUM(ELF
, PT_SUNW_UNWIND
)
1262 LLVM_READOBJ_PHDR_ENUM(ELF
, PT_GNU_STACK
)
1263 LLVM_READOBJ_PHDR_ENUM(ELF
, PT_GNU_RELRO
)
1265 // All machine specific PT_* types
1268 if (Type
== ELF::PT_ARM_EXIDX
)
1272 case ELF::EM_MIPS_RS3_LE
:
1274 case PT_MIPS_REGINFO
:
1276 case PT_MIPS_RTPROC
:
1278 case PT_MIPS_OPTIONS
:
1280 case PT_MIPS_ABIFLAGS
:
1286 return std::string("<unknown>: ") + to_string(format_hex(Type
, 1));
1289 static const EnumEntry
<unsigned> ElfSegmentFlags
[] = {
1290 LLVM_READOBJ_ENUM_ENT(ELF
, PF_X
),
1291 LLVM_READOBJ_ENUM_ENT(ELF
, PF_W
),
1292 LLVM_READOBJ_ENUM_ENT(ELF
, PF_R
)
1295 static const EnumEntry
<unsigned> ElfHeaderMipsFlags
[] = {
1296 ENUM_ENT(EF_MIPS_NOREORDER
, "noreorder"),
1297 ENUM_ENT(EF_MIPS_PIC
, "pic"),
1298 ENUM_ENT(EF_MIPS_CPIC
, "cpic"),
1299 ENUM_ENT(EF_MIPS_ABI2
, "abi2"),
1300 ENUM_ENT(EF_MIPS_32BITMODE
, "32bitmode"),
1301 ENUM_ENT(EF_MIPS_FP64
, "fp64"),
1302 ENUM_ENT(EF_MIPS_NAN2008
, "nan2008"),
1303 ENUM_ENT(EF_MIPS_ABI_O32
, "o32"),
1304 ENUM_ENT(EF_MIPS_ABI_O64
, "o64"),
1305 ENUM_ENT(EF_MIPS_ABI_EABI32
, "eabi32"),
1306 ENUM_ENT(EF_MIPS_ABI_EABI64
, "eabi64"),
1307 ENUM_ENT(EF_MIPS_MACH_3900
, "3900"),
1308 ENUM_ENT(EF_MIPS_MACH_4010
, "4010"),
1309 ENUM_ENT(EF_MIPS_MACH_4100
, "4100"),
1310 ENUM_ENT(EF_MIPS_MACH_4650
, "4650"),
1311 ENUM_ENT(EF_MIPS_MACH_4120
, "4120"),
1312 ENUM_ENT(EF_MIPS_MACH_4111
, "4111"),
1313 ENUM_ENT(EF_MIPS_MACH_SB1
, "sb1"),
1314 ENUM_ENT(EF_MIPS_MACH_OCTEON
, "octeon"),
1315 ENUM_ENT(EF_MIPS_MACH_XLR
, "xlr"),
1316 ENUM_ENT(EF_MIPS_MACH_OCTEON2
, "octeon2"),
1317 ENUM_ENT(EF_MIPS_MACH_OCTEON3
, "octeon3"),
1318 ENUM_ENT(EF_MIPS_MACH_5400
, "5400"),
1319 ENUM_ENT(EF_MIPS_MACH_5900
, "5900"),
1320 ENUM_ENT(EF_MIPS_MACH_5500
, "5500"),
1321 ENUM_ENT(EF_MIPS_MACH_9000
, "9000"),
1322 ENUM_ENT(EF_MIPS_MACH_LS2E
, "loongson-2e"),
1323 ENUM_ENT(EF_MIPS_MACH_LS2F
, "loongson-2f"),
1324 ENUM_ENT(EF_MIPS_MACH_LS3A
, "loongson-3a"),
1325 ENUM_ENT(EF_MIPS_MICROMIPS
, "micromips"),
1326 ENUM_ENT(EF_MIPS_ARCH_ASE_M16
, "mips16"),
1327 ENUM_ENT(EF_MIPS_ARCH_ASE_MDMX
, "mdmx"),
1328 ENUM_ENT(EF_MIPS_ARCH_1
, "mips1"),
1329 ENUM_ENT(EF_MIPS_ARCH_2
, "mips2"),
1330 ENUM_ENT(EF_MIPS_ARCH_3
, "mips3"),
1331 ENUM_ENT(EF_MIPS_ARCH_4
, "mips4"),
1332 ENUM_ENT(EF_MIPS_ARCH_5
, "mips5"),
1333 ENUM_ENT(EF_MIPS_ARCH_32
, "mips32"),
1334 ENUM_ENT(EF_MIPS_ARCH_64
, "mips64"),
1335 ENUM_ENT(EF_MIPS_ARCH_32R2
, "mips32r2"),
1336 ENUM_ENT(EF_MIPS_ARCH_64R2
, "mips64r2"),
1337 ENUM_ENT(EF_MIPS_ARCH_32R6
, "mips32r6"),
1338 ENUM_ENT(EF_MIPS_ARCH_64R6
, "mips64r6")
1341 static const EnumEntry
<unsigned> ElfHeaderAMDGPUFlags
[] = {
1342 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_NONE
),
1343 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_R600
),
1344 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_R630
),
1345 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_RS880
),
1346 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_RV670
),
1347 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_RV710
),
1348 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_RV730
),
1349 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_RV770
),
1350 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_CEDAR
),
1351 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_CYPRESS
),
1352 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_JUNIPER
),
1353 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_REDWOOD
),
1354 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_SUMO
),
1355 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_BARTS
),
1356 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_CAICOS
),
1357 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_CAYMAN
),
1358 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_R600_TURKS
),
1359 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX600
),
1360 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX601
),
1361 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX700
),
1362 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX701
),
1363 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX702
),
1364 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX703
),
1365 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX704
),
1366 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX801
),
1367 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX802
),
1368 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX803
),
1369 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX810
),
1370 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX900
),
1371 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX902
),
1372 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX904
),
1373 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX906
),
1374 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX908
),
1375 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX909
),
1376 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX1010
),
1377 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX1011
),
1378 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_MACH_AMDGCN_GFX1012
),
1379 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_XNACK
),
1380 LLVM_READOBJ_ENUM_ENT(ELF
, EF_AMDGPU_SRAM_ECC
)
1383 static const EnumEntry
<unsigned> ElfHeaderRISCVFlags
[] = {
1384 ENUM_ENT(EF_RISCV_RVC
, "RVC"),
1385 ENUM_ENT(EF_RISCV_FLOAT_ABI_SINGLE
, "single-float ABI"),
1386 ENUM_ENT(EF_RISCV_FLOAT_ABI_DOUBLE
, "double-float ABI"),
1387 ENUM_ENT(EF_RISCV_FLOAT_ABI_QUAD
, "quad-float ABI"),
1388 ENUM_ENT(EF_RISCV_RVE
, "RVE")
1391 static const EnumEntry
<unsigned> ElfSymOtherFlags
[] = {
1392 LLVM_READOBJ_ENUM_ENT(ELF
, STV_INTERNAL
),
1393 LLVM_READOBJ_ENUM_ENT(ELF
, STV_HIDDEN
),
1394 LLVM_READOBJ_ENUM_ENT(ELF
, STV_PROTECTED
)
1397 static const EnumEntry
<unsigned> ElfMipsSymOtherFlags
[] = {
1398 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_OPTIONAL
),
1399 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_PLT
),
1400 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_PIC
),
1401 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_MICROMIPS
)
1404 static const EnumEntry
<unsigned> ElfMips16SymOtherFlags
[] = {
1405 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_OPTIONAL
),
1406 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_PLT
),
1407 LLVM_READOBJ_ENUM_ENT(ELF
, STO_MIPS_MIPS16
)
1410 static const char *getElfMipsOptionsOdkType(unsigned Odk
) {
1412 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_NULL
);
1413 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_REGINFO
);
1414 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_EXCEPTIONS
);
1415 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_PAD
);
1416 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_HWPATCH
);
1417 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_FILL
);
1418 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_TAGS
);
1419 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_HWAND
);
1420 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_HWOR
);
1421 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_GP_GROUP
);
1422 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_IDENT
);
1423 LLVM_READOBJ_ENUM_CASE(ELF
, ODK_PAGESIZE
);
1429 template <typename ELFT
>
1430 std::pair
<const typename
ELFT::Phdr
*, const typename
ELFT::Shdr
*>
1431 ELFDumper
<ELFT
>::findDynamic(const ELFFile
<ELFT
> *Obj
) {
1432 // Try to locate the PT_DYNAMIC header.
1433 const Elf_Phdr
*DynamicPhdr
= nullptr;
1434 for (const Elf_Phdr
&Phdr
:
1435 unwrapOrError(ObjF
->getFileName(), Obj
->program_headers())) {
1436 if (Phdr
.p_type
!= ELF::PT_DYNAMIC
)
1438 DynamicPhdr
= &Phdr
;
1442 // Try to locate the .dynamic section in the sections header table.
1443 const Elf_Shdr
*DynamicSec
= nullptr;
1444 for (const Elf_Shdr
&Sec
:
1445 unwrapOrError(ObjF
->getFileName(), Obj
->sections())) {
1446 if (Sec
.sh_type
!= ELF::SHT_DYNAMIC
)
1452 if (DynamicPhdr
&& DynamicPhdr
->p_offset
+ DynamicPhdr
->p_filesz
>
1453 ObjF
->getMemoryBufferRef().getBufferSize()) {
1456 "PT_DYNAMIC segment offset + size exceeds the size of the file"),
1457 ObjF
->getFileName());
1458 // Don't use the broken dynamic header.
1459 DynamicPhdr
= nullptr;
1462 if (DynamicPhdr
&& DynamicSec
) {
1464 unwrapOrError(ObjF
->getFileName(), Obj
->getSectionName(DynamicSec
));
1465 if (DynamicSec
->sh_addr
+ DynamicSec
->sh_size
>
1466 DynamicPhdr
->p_vaddr
+ DynamicPhdr
->p_memsz
||
1467 DynamicSec
->sh_addr
< DynamicPhdr
->p_vaddr
)
1468 reportWarning(createError("The SHT_DYNAMIC section '" + Name
+
1469 "' is not contained within the "
1470 "PT_DYNAMIC segment"),
1471 ObjF
->getFileName());
1473 if (DynamicSec
->sh_addr
!= DynamicPhdr
->p_vaddr
)
1474 reportWarning(createError("The SHT_DYNAMIC section '" + Name
+
1475 "' is not at the start of "
1476 "PT_DYNAMIC segment"),
1477 ObjF
->getFileName());
1480 return std::make_pair(DynamicPhdr
, DynamicSec
);
1483 template <typename ELFT
>
1484 void ELFDumper
<ELFT
>::loadDynamicTable(const ELFFile
<ELFT
> *Obj
) {
1485 const Elf_Phdr
*DynamicPhdr
;
1486 const Elf_Shdr
*DynamicSec
;
1487 std::tie(DynamicPhdr
, DynamicSec
) = findDynamic(Obj
);
1488 if (!DynamicPhdr
&& !DynamicSec
)
1491 DynRegionInfo
FromPhdr(ObjF
->getFileName());
1492 bool IsPhdrTableValid
= false;
1494 FromPhdr
= createDRIFrom(DynamicPhdr
, sizeof(Elf_Dyn
));
1495 IsPhdrTableValid
= !FromPhdr
.getAsArrayRef
<Elf_Dyn
>().empty();
1498 // Locate the dynamic table described in a section header.
1499 // Ignore sh_entsize and use the expected value for entry size explicitly.
1500 // This allows us to dump dynamic sections with a broken sh_entsize
1502 DynRegionInfo
FromSec(ObjF
->getFileName());
1503 bool IsSecTableValid
= false;
1506 checkDRI({ObjF
->getELFFile()->base() + DynamicSec
->sh_offset
,
1507 DynamicSec
->sh_size
, sizeof(Elf_Dyn
), ObjF
->getFileName()});
1508 IsSecTableValid
= !FromSec
.getAsArrayRef
<Elf_Dyn
>().empty();
1511 // When we only have information from one of the SHT_DYNAMIC section header or
1512 // PT_DYNAMIC program header, just use that.
1513 if (!DynamicPhdr
|| !DynamicSec
) {
1514 if ((DynamicPhdr
&& IsPhdrTableValid
) || (DynamicSec
&& IsSecTableValid
)) {
1515 DynamicTable
= DynamicPhdr
? FromPhdr
: FromSec
;
1516 parseDynamicTable();
1518 reportWarning(createError("no valid dynamic table was found"),
1519 ObjF
->getFileName());
1524 // At this point we have tables found from the section header and from the
1525 // dynamic segment. Usually they match, but we have to do sanity checks to
1528 if (FromPhdr
.Addr
!= FromSec
.Addr
)
1529 reportWarning(createError("SHT_DYNAMIC section header and PT_DYNAMIC "
1530 "program header disagree about "
1531 "the location of the dynamic table"),
1532 ObjF
->getFileName());
1534 if (!IsPhdrTableValid
&& !IsSecTableValid
) {
1535 reportWarning(createError("no valid dynamic table was found"),
1536 ObjF
->getFileName());
1540 // Information in the PT_DYNAMIC program header has priority over the information
1541 // in a section header.
1542 if (IsPhdrTableValid
) {
1543 if (!IsSecTableValid
)
1546 "SHT_DYNAMIC dynamic table is invalid: PT_DYNAMIC will be used"),
1547 ObjF
->getFileName());
1548 DynamicTable
= FromPhdr
;
1552 "PT_DYNAMIC dynamic table is invalid: SHT_DYNAMIC will be used"),
1553 ObjF
->getFileName());
1554 DynamicTable
= FromSec
;
1557 parseDynamicTable();
1560 template <typename ELFT
>
1561 ELFDumper
<ELFT
>::ELFDumper(const object::ELFObjectFile
<ELFT
> *ObjF
,
1562 ScopedPrinter
&Writer
)
1563 : ObjDumper(Writer
), ObjF(ObjF
), DynRelRegion(ObjF
->getFileName()),
1564 DynRelaRegion(ObjF
->getFileName()), DynRelrRegion(ObjF
->getFileName()),
1565 DynPLTRelRegion(ObjF
->getFileName()), DynSymRegion(ObjF
->getFileName()),
1566 DynamicTable(ObjF
->getFileName()) {
1567 const ELFFile
<ELFT
> *Obj
= ObjF
->getELFFile();
1568 for (const Elf_Shdr
&Sec
:
1569 unwrapOrError(ObjF
->getFileName(), Obj
->sections())) {
1570 switch (Sec
.sh_type
) {
1571 case ELF::SHT_SYMTAB
:
1573 DotSymtabSec
= &Sec
;
1575 case ELF::SHT_DYNSYM
:
1576 if (!DynSymRegion
.Size
) {
1577 DynSymRegion
= createDRIFrom(&Sec
);
1578 // This is only used (if Elf_Shdr present)for naming section in GNU
1581 unwrapOrError(ObjF
->getFileName(), Obj
->getSectionName(&Sec
));
1583 if (Expected
<StringRef
> E
= Obj
->getStringTableForSymtab(Sec
))
1584 DynamicStringTable
= *E
;
1586 reportWarning(E
.takeError(), ObjF
->getFileName());
1589 case ELF::SHT_SYMTAB_SHNDX
:
1590 ShndxTable
= unwrapOrError(ObjF
->getFileName(), Obj
->getSHNDXTable(Sec
));
1592 case ELF::SHT_GNU_versym
:
1593 if (!SymbolVersionSection
)
1594 SymbolVersionSection
= &Sec
;
1596 case ELF::SHT_GNU_verdef
:
1597 if (!SymbolVersionDefSection
)
1598 SymbolVersionDefSection
= &Sec
;
1600 case ELF::SHT_GNU_verneed
:
1601 if (!SymbolVersionNeedSection
)
1602 SymbolVersionNeedSection
= &Sec
;
1604 case ELF::SHT_LLVM_CALL_GRAPH_PROFILE
:
1605 if (!DotCGProfileSec
)
1606 DotCGProfileSec
= &Sec
;
1608 case ELF::SHT_LLVM_ADDRSIG
:
1610 DotAddrsigSec
= &Sec
;
1615 loadDynamicTable(Obj
);
1617 if (opts::Output
== opts::GNU
)
1618 ELFDumperStyle
.reset(new GNUStyle
<ELFT
>(Writer
, this));
1620 ELFDumperStyle
.reset(new LLVMStyle
<ELFT
>(Writer
, this));
1623 static const char *getTypeString(unsigned Arch
, uint64_t Type
) {
1624 #define DYNAMIC_TAG(n, v)
1629 #define AARCH64_DYNAMIC_TAG(name, value) \
1632 #include "llvm/BinaryFormat/DynamicTags.def"
1633 #undef AARCH64_DYNAMIC_TAG
1639 #define HEXAGON_DYNAMIC_TAG(name, value) \
1642 #include "llvm/BinaryFormat/DynamicTags.def"
1643 #undef HEXAGON_DYNAMIC_TAG
1649 #define MIPS_DYNAMIC_TAG(name, value) \
1652 #include "llvm/BinaryFormat/DynamicTags.def"
1653 #undef MIPS_DYNAMIC_TAG
1659 #define PPC64_DYNAMIC_TAG(name, value) \
1662 #include "llvm/BinaryFormat/DynamicTags.def"
1663 #undef PPC64_DYNAMIC_TAG
1669 // Now handle all dynamic tags except the architecture specific ones
1670 #define AARCH64_DYNAMIC_TAG(name, value)
1671 #define MIPS_DYNAMIC_TAG(name, value)
1672 #define HEXAGON_DYNAMIC_TAG(name, value)
1673 #define PPC64_DYNAMIC_TAG(name, value)
1674 // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
1675 #define DYNAMIC_TAG_MARKER(name, value)
1676 #define DYNAMIC_TAG(name, value) \
1679 #include "llvm/BinaryFormat/DynamicTags.def"
1681 #undef AARCH64_DYNAMIC_TAG
1682 #undef MIPS_DYNAMIC_TAG
1683 #undef HEXAGON_DYNAMIC_TAG
1684 #undef PPC64_DYNAMIC_TAG
1685 #undef DYNAMIC_TAG_MARKER
1691 template <typename ELFT
> void ELFDumper
<ELFT
>::parseDynamicTable() {
1692 auto toMappedAddr
= [&](uint64_t Tag
, uint64_t VAddr
) -> const uint8_t * {
1693 auto MappedAddrOrError
= ObjF
->getELFFile()->toMappedAddr(VAddr
);
1694 if (!MappedAddrOrError
) {
1696 createError("Unable to parse DT_" +
1697 Twine(getTypeString(
1698 ObjF
->getELFFile()->getHeader()->e_machine
, Tag
)) +
1699 ": " + llvm::toString(MappedAddrOrError
.takeError()));
1701 reportWarning(std::move(Err
), ObjF
->getFileName());
1704 return MappedAddrOrError
.get();
1707 uint64_t SONameOffset
= 0;
1708 const char *StringTableBegin
= nullptr;
1709 uint64_t StringTableSize
= 0;
1710 for (const Elf_Dyn
&Dyn
: dynamic_table()) {
1711 switch (Dyn
.d_tag
) {
1713 HashTable
= reinterpret_cast<const Elf_Hash
*>(
1714 toMappedAddr(Dyn
.getTag(), Dyn
.getPtr()));
1716 case ELF::DT_GNU_HASH
:
1717 GnuHashTable
= reinterpret_cast<const Elf_GnuHash
*>(
1718 toMappedAddr(Dyn
.getTag(), Dyn
.getPtr()));
1720 case ELF::DT_STRTAB
:
1721 StringTableBegin
= reinterpret_cast<const char *>(
1722 toMappedAddr(Dyn
.getTag(), Dyn
.getPtr()));
1725 StringTableSize
= Dyn
.getVal();
1727 case ELF::DT_SYMTAB
: {
1728 // Often we find the information about the dynamic symbol table
1729 // location in the SHT_DYNSYM section header. However, the value in
1730 // DT_SYMTAB has priority, because it is used by dynamic loaders to
1731 // locate .dynsym at runtime. The location we find in the section header
1732 // and the location we find here should match. If we can't map the
1733 // DT_SYMTAB value to an address (e.g. when there are no program headers), we
1734 // ignore its value.
1735 if (const uint8_t *VA
= toMappedAddr(Dyn
.getTag(), Dyn
.getPtr())) {
1736 // EntSize is non-zero if the dynamic symbol table has been found via a
1738 if (DynSymRegion
.EntSize
&& VA
!= DynSymRegion
.Addr
)
1741 "SHT_DYNSYM section header and DT_SYMTAB disagree about "
1742 "the location of the dynamic symbol table"),
1743 ObjF
->getFileName());
1745 DynSymRegion
.Addr
= VA
;
1746 DynSymRegion
.EntSize
= sizeof(Elf_Sym
);
1751 DynRelaRegion
.Addr
= toMappedAddr(Dyn
.getTag(), Dyn
.getPtr());
1753 case ELF::DT_RELASZ
:
1754 DynRelaRegion
.Size
= Dyn
.getVal();
1756 case ELF::DT_RELAENT
:
1757 DynRelaRegion
.EntSize
= Dyn
.getVal();
1759 case ELF::DT_SONAME
:
1760 SONameOffset
= Dyn
.getVal();
1763 DynRelRegion
.Addr
= toMappedAddr(Dyn
.getTag(), Dyn
.getPtr());
1766 DynRelRegion
.Size
= Dyn
.getVal();
1768 case ELF::DT_RELENT
:
1769 DynRelRegion
.EntSize
= Dyn
.getVal();
1772 case ELF::DT_ANDROID_RELR
:
1773 DynRelrRegion
.Addr
= toMappedAddr(Dyn
.getTag(), Dyn
.getPtr());
1775 case ELF::DT_RELRSZ
:
1776 case ELF::DT_ANDROID_RELRSZ
:
1777 DynRelrRegion
.Size
= Dyn
.getVal();
1779 case ELF::DT_RELRENT
:
1780 case ELF::DT_ANDROID_RELRENT
:
1781 DynRelrRegion
.EntSize
= Dyn
.getVal();
1783 case ELF::DT_PLTREL
:
1784 if (Dyn
.getVal() == DT_REL
)
1785 DynPLTRelRegion
.EntSize
= sizeof(Elf_Rel
);
1786 else if (Dyn
.getVal() == DT_RELA
)
1787 DynPLTRelRegion
.EntSize
= sizeof(Elf_Rela
);
1789 reportError(createError(Twine("unknown DT_PLTREL value of ") +
1790 Twine((uint64_t)Dyn
.getVal())),
1791 ObjF
->getFileName());
1793 case ELF::DT_JMPREL
:
1794 DynPLTRelRegion
.Addr
= toMappedAddr(Dyn
.getTag(), Dyn
.getPtr());
1796 case ELF::DT_PLTRELSZ
:
1797 DynPLTRelRegion
.Size
= Dyn
.getVal();
1801 if (StringTableBegin
)
1802 DynamicStringTable
= StringRef(StringTableBegin
, StringTableSize
);
1803 SOName
= getDynamicString(SONameOffset
);
1806 template <typename ELFT
>
1807 typename ELFDumper
<ELFT
>::Elf_Rel_Range ELFDumper
<ELFT
>::dyn_rels() const {
1808 return DynRelRegion
.getAsArrayRef
<Elf_Rel
>();
1811 template <typename ELFT
>
1812 typename ELFDumper
<ELFT
>::Elf_Rela_Range ELFDumper
<ELFT
>::dyn_relas() const {
1813 return DynRelaRegion
.getAsArrayRef
<Elf_Rela
>();
1816 template <typename ELFT
>
1817 typename ELFDumper
<ELFT
>::Elf_Relr_Range ELFDumper
<ELFT
>::dyn_relrs() const {
1818 return DynRelrRegion
.getAsArrayRef
<Elf_Relr
>();
1821 template <class ELFT
> void ELFDumper
<ELFT
>::printFileHeaders() {
1822 ELFDumperStyle
->printFileHeaders(ObjF
->getELFFile());
1825 template <class ELFT
> void ELFDumper
<ELFT
>::printSectionHeaders() {
1826 ELFDumperStyle
->printSectionHeaders(ObjF
->getELFFile());
1829 template <class ELFT
> void ELFDumper
<ELFT
>::printRelocations() {
1830 ELFDumperStyle
->printRelocations(ObjF
->getELFFile());
1833 template <class ELFT
>
1834 void ELFDumper
<ELFT
>::printProgramHeaders(
1835 bool PrintProgramHeaders
, cl::boolOrDefault PrintSectionMapping
) {
1836 ELFDumperStyle
->printProgramHeaders(ObjF
->getELFFile(), PrintProgramHeaders
,
1837 PrintSectionMapping
);
1840 template <typename ELFT
> void ELFDumper
<ELFT
>::printVersionInfo() {
1841 // Dump version symbol section.
1842 ELFDumperStyle
->printVersionSymbolSection(ObjF
->getELFFile(),
1843 SymbolVersionSection
);
1845 // Dump version definition section.
1846 ELFDumperStyle
->printVersionDefinitionSection(ObjF
->getELFFile(),
1847 SymbolVersionDefSection
);
1849 // Dump version dependency section.
1850 ELFDumperStyle
->printVersionDependencySection(ObjF
->getELFFile(),
1851 SymbolVersionNeedSection
);
1854 template <class ELFT
> void ELFDumper
<ELFT
>::printDynamicRelocations() {
1855 ELFDumperStyle
->printDynamicRelocations(ObjF
->getELFFile());
1858 template <class ELFT
>
1859 void ELFDumper
<ELFT
>::printSymbols(bool PrintSymbols
,
1860 bool PrintDynamicSymbols
) {
1861 ELFDumperStyle
->printSymbols(ObjF
->getELFFile(), PrintSymbols
,
1862 PrintDynamicSymbols
);
1865 template <class ELFT
> void ELFDumper
<ELFT
>::printHashSymbols() {
1866 ELFDumperStyle
->printHashSymbols(ObjF
->getELFFile());
1869 template <class ELFT
> void ELFDumper
<ELFT
>::printHashHistogram() {
1870 ELFDumperStyle
->printHashHistogram(ObjF
->getELFFile());
1873 template <class ELFT
> void ELFDumper
<ELFT
>::printCGProfile() {
1874 ELFDumperStyle
->printCGProfile(ObjF
->getELFFile());
1877 template <class ELFT
> void ELFDumper
<ELFT
>::printNotes() {
1878 ELFDumperStyle
->printNotes(ObjF
->getELFFile());
1881 template <class ELFT
> void ELFDumper
<ELFT
>::printELFLinkerOptions() {
1882 ELFDumperStyle
->printELFLinkerOptions(ObjF
->getELFFile());
1885 template <class ELFT
> void ELFDumper
<ELFT
>::printStackSizes() {
1886 ELFDumperStyle
->printStackSizes(ObjF
);
1889 #define LLVM_READOBJ_DT_FLAG_ENT(prefix, enum) \
1890 { #enum, prefix##_##enum }
1892 static const EnumEntry
<unsigned> ElfDynamicDTFlags
[] = {
1893 LLVM_READOBJ_DT_FLAG_ENT(DF
, ORIGIN
),
1894 LLVM_READOBJ_DT_FLAG_ENT(DF
, SYMBOLIC
),
1895 LLVM_READOBJ_DT_FLAG_ENT(DF
, TEXTREL
),
1896 LLVM_READOBJ_DT_FLAG_ENT(DF
, BIND_NOW
),
1897 LLVM_READOBJ_DT_FLAG_ENT(DF
, STATIC_TLS
)
1900 static const EnumEntry
<unsigned> ElfDynamicDTFlags1
[] = {
1901 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NOW
),
1902 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, GLOBAL
),
1903 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, GROUP
),
1904 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NODELETE
),
1905 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, LOADFLTR
),
1906 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, INITFIRST
),
1907 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NOOPEN
),
1908 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, ORIGIN
),
1909 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, DIRECT
),
1910 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, TRANS
),
1911 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, INTERPOSE
),
1912 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NODEFLIB
),
1913 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NODUMP
),
1914 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, CONFALT
),
1915 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, ENDFILTEE
),
1916 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, DISPRELDNE
),
1917 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, DISPRELPND
),
1918 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NODIRECT
),
1919 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, IGNMULDEF
),
1920 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NOKSYMS
),
1921 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NOHDR
),
1922 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, EDITED
),
1923 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, NORELOC
),
1924 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, SYMINTPOSE
),
1925 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, GLOBAUDIT
),
1926 LLVM_READOBJ_DT_FLAG_ENT(DF_1
, SINGLETON
)
1929 static const EnumEntry
<unsigned> ElfDynamicDTMipsFlags
[] = {
1930 LLVM_READOBJ_DT_FLAG_ENT(RHF
, NONE
),
1931 LLVM_READOBJ_DT_FLAG_ENT(RHF
, QUICKSTART
),
1932 LLVM_READOBJ_DT_FLAG_ENT(RHF
, NOTPOT
),
1933 LLVM_READOBJ_DT_FLAG_ENT(RHS
, NO_LIBRARY_REPLACEMENT
),
1934 LLVM_READOBJ_DT_FLAG_ENT(RHF
, NO_MOVE
),
1935 LLVM_READOBJ_DT_FLAG_ENT(RHF
, SGI_ONLY
),
1936 LLVM_READOBJ_DT_FLAG_ENT(RHF
, GUARANTEE_INIT
),
1937 LLVM_READOBJ_DT_FLAG_ENT(RHF
, DELTA_C_PLUS_PLUS
),
1938 LLVM_READOBJ_DT_FLAG_ENT(RHF
, GUARANTEE_START_INIT
),
1939 LLVM_READOBJ_DT_FLAG_ENT(RHF
, PIXIE
),
1940 LLVM_READOBJ_DT_FLAG_ENT(RHF
, DEFAULT_DELAY_LOAD
),
1941 LLVM_READOBJ_DT_FLAG_ENT(RHF
, REQUICKSTART
),
1942 LLVM_READOBJ_DT_FLAG_ENT(RHF
, REQUICKSTARTED
),
1943 LLVM_READOBJ_DT_FLAG_ENT(RHF
, CORD
),
1944 LLVM_READOBJ_DT_FLAG_ENT(RHF
, NO_UNRES_UNDEF
),
1945 LLVM_READOBJ_DT_FLAG_ENT(RHF
, RLD_ORDER_SAFE
)
1948 #undef LLVM_READOBJ_DT_FLAG_ENT
1950 template <typename T
, typename TFlag
>
1951 void printFlags(T Value
, ArrayRef
<EnumEntry
<TFlag
>> Flags
, raw_ostream
&OS
) {
1952 using FlagEntry
= EnumEntry
<TFlag
>;
1953 using FlagVector
= SmallVector
<FlagEntry
, 10>;
1954 FlagVector SetFlags
;
1956 for (const auto &Flag
: Flags
) {
1957 if (Flag
.Value
== 0)
1960 if ((Value
& Flag
.Value
) == Flag
.Value
)
1961 SetFlags
.push_back(Flag
);
1964 for (const auto &Flag
: SetFlags
) {
1965 OS
<< Flag
.Name
<< " ";
1969 template <class ELFT
>
1970 void ELFDumper
<ELFT
>::printDynamicEntry(raw_ostream
&OS
, uint64_t Type
,
1971 uint64_t Value
) const {
1972 const char *ConvChar
=
1973 (opts::Output
== opts::GNU
) ? "0x%" PRIx64
: "0x%" PRIX64
;
1975 // Handle custom printing of architecture specific tags
1976 switch (ObjF
->getELFFile()->getHeader()->e_machine
) {
1979 case DT_AARCH64_BTI_PLT
:
1980 case DT_AARCH64_PAC_PLT
:
1989 case DT_HEXAGON_VER
:
1992 case DT_HEXAGON_SYMSZ
:
1993 case DT_HEXAGON_PLT
:
1994 OS
<< format(ConvChar
, Value
);
2002 case DT_MIPS_RLD_VERSION
:
2003 case DT_MIPS_LOCAL_GOTNO
:
2004 case DT_MIPS_SYMTABNO
:
2005 case DT_MIPS_UNREFEXTNO
:
2008 case DT_MIPS_TIME_STAMP
:
2009 case DT_MIPS_ICHECKSUM
:
2010 case DT_MIPS_IVERSION
:
2011 case DT_MIPS_BASE_ADDRESS
:
2013 case DT_MIPS_CONFLICT
:
2014 case DT_MIPS_LIBLIST
:
2015 case DT_MIPS_CONFLICTNO
:
2016 case DT_MIPS_LIBLISTNO
:
2017 case DT_MIPS_GOTSYM
:
2018 case DT_MIPS_HIPAGENO
:
2019 case DT_MIPS_RLD_MAP
:
2020 case DT_MIPS_DELTA_CLASS
:
2021 case DT_MIPS_DELTA_CLASS_NO
:
2022 case DT_MIPS_DELTA_INSTANCE
:
2023 case DT_MIPS_DELTA_RELOC
:
2024 case DT_MIPS_DELTA_RELOC_NO
:
2025 case DT_MIPS_DELTA_SYM
:
2026 case DT_MIPS_DELTA_SYM_NO
:
2027 case DT_MIPS_DELTA_CLASSSYM
:
2028 case DT_MIPS_DELTA_CLASSSYM_NO
:
2029 case DT_MIPS_CXX_FLAGS
:
2030 case DT_MIPS_PIXIE_INIT
:
2031 case DT_MIPS_SYMBOL_LIB
:
2032 case DT_MIPS_LOCALPAGE_GOTIDX
:
2033 case DT_MIPS_LOCAL_GOTIDX
:
2034 case DT_MIPS_HIDDEN_GOTIDX
:
2035 case DT_MIPS_PROTECTED_GOTIDX
:
2036 case DT_MIPS_OPTIONS
:
2037 case DT_MIPS_INTERFACE
:
2038 case DT_MIPS_DYNSTR_ALIGN
:
2039 case DT_MIPS_INTERFACE_SIZE
:
2040 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR
:
2041 case DT_MIPS_PERF_SUFFIX
:
2042 case DT_MIPS_COMPACT_SIZE
:
2043 case DT_MIPS_GP_VALUE
:
2044 case DT_MIPS_AUX_DYNAMIC
:
2045 case DT_MIPS_PLTGOT
:
2047 case DT_MIPS_RLD_MAP_REL
:
2048 OS
<< format(ConvChar
, Value
);
2051 printFlags(Value
, makeArrayRef(ElfDynamicDTMipsFlags
), OS
);
2063 if (Value
== DT_REL
) {
2066 } else if (Value
== DT_RELA
) {
2082 case DT_PREINIT_ARRAY
:
2089 OS
<< format(ConvChar
, Value
);
2104 case DT_INIT_ARRAYSZ
:
2105 case DT_FINI_ARRAYSZ
:
2106 case DT_PREINIT_ARRAYSZ
:
2107 case DT_ANDROID_RELSZ
:
2108 case DT_ANDROID_RELASZ
:
2109 OS
<< Value
<< " (bytes)";
2118 const std::map
<uint64_t, const char*> TagNames
= {
2119 {DT_NEEDED
, "Shared library"},
2120 {DT_SONAME
, "Library soname"},
2121 {DT_AUXILIARY
, "Auxiliary library"},
2122 {DT_USED
, "Not needed object"},
2123 {DT_FILTER
, "Filter library"},
2124 {DT_RPATH
, "Library rpath"},
2125 {DT_RUNPATH
, "Library runpath"},
2127 OS
<< TagNames
.at(Type
) << ": [" << getDynamicString(Value
) << "]";
2131 printFlags(Value
, makeArrayRef(ElfDynamicDTFlags
), OS
);
2134 printFlags(Value
, makeArrayRef(ElfDynamicDTFlags1
), OS
);
2137 OS
<< format(ConvChar
, Value
);
2142 template <class ELFT
>
2143 std::string ELFDumper
<ELFT
>::getDynamicString(uint64_t Value
) const {
2144 if (DynamicStringTable
.empty())
2145 return "<String table is empty or was not found>";
2146 if (Value
< DynamicStringTable
.size())
2147 return DynamicStringTable
.data() + Value
;
2148 return Twine("<Invalid offset 0x" + utohexstr(Value
) + ">").str();
2151 template <class ELFT
> void ELFDumper
<ELFT
>::printUnwindInfo() {
2152 DwarfCFIEH::PrinterContext
<ELFT
> Ctx(W
, ObjF
);
2153 Ctx
.printUnwindInformation();
2158 template <> void ELFDumper
<ELF32LE
>::printUnwindInfo() {
2159 const ELFFile
<ELF32LE
> *Obj
= ObjF
->getELFFile();
2160 const unsigned Machine
= Obj
->getHeader()->e_machine
;
2161 if (Machine
== EM_ARM
) {
2162 ARM::EHABI::PrinterContext
<ELF32LE
> Ctx(W
, Obj
, ObjF
->getFileName(),
2164 Ctx
.PrintUnwindInformation();
2166 DwarfCFIEH::PrinterContext
<ELF32LE
> Ctx(W
, ObjF
);
2167 Ctx
.printUnwindInformation();
2170 } // end anonymous namespace
2172 template <class ELFT
> void ELFDumper
<ELFT
>::printDynamicTable() {
2173 ELFDumperStyle
->printDynamic(ObjF
->getELFFile());
2176 template <class ELFT
> void ELFDumper
<ELFT
>::printNeededLibraries() {
2177 ListScope
D(W
, "NeededLibraries");
2179 std::vector
<std::string
> Libs
;
2180 for (const auto &Entry
: dynamic_table())
2181 if (Entry
.d_tag
== ELF::DT_NEEDED
)
2182 Libs
.push_back(getDynamicString(Entry
.d_un
.d_val
));
2184 llvm::stable_sort(Libs
);
2186 for (const auto &L
: Libs
)
2187 W
.startLine() << L
<< "\n";
2190 template <typename ELFT
> void ELFDumper
<ELFT
>::printHashTable() {
2191 DictScope
D(W
, "HashTable");
2194 W
.printNumber("Num Buckets", HashTable
->nbucket
);
2195 W
.printNumber("Num Chains", HashTable
->nchain
);
2196 W
.printList("Buckets", HashTable
->buckets());
2197 W
.printList("Chains", HashTable
->chains());
2200 template <typename ELFT
> void ELFDumper
<ELFT
>::printGnuHashTable() {
2201 DictScope
D(W
, "GnuHashTable");
2204 W
.printNumber("Num Buckets", GnuHashTable
->nbuckets
);
2205 W
.printNumber("First Hashed Symbol Index", GnuHashTable
->symndx
);
2206 W
.printNumber("Num Mask Words", GnuHashTable
->maskwords
);
2207 W
.printNumber("Shift Count", GnuHashTable
->shift2
);
2208 W
.printHexList("Bloom Filter", GnuHashTable
->filter());
2209 W
.printList("Buckets", GnuHashTable
->buckets());
2210 Elf_Sym_Range Syms
= dynamic_symbols();
2211 unsigned NumSyms
= std::distance(Syms
.begin(), Syms
.end());
2213 reportError(createError("No dynamic symbol section"), ObjF
->getFileName());
2214 W
.printHexList("Values", GnuHashTable
->values(NumSyms
));
2217 template <typename ELFT
> void ELFDumper
<ELFT
>::printLoadName() {
2218 W
.printString("LoadName", SOName
);
2221 template <class ELFT
> void ELFDumper
<ELFT
>::printArchSpecificInfo() {
2222 const ELFFile
<ELFT
> *Obj
= ObjF
->getELFFile();
2223 switch (Obj
->getHeader()->e_machine
) {
2228 ELFDumperStyle
->printMipsABIFlags(ObjF
);
2232 MipsGOTParser
<ELFT
> Parser(Obj
, ObjF
->getFileName(), dynamic_table(),
2234 if (Parser
.hasGot())
2235 ELFDumperStyle
->printMipsGOT(Parser
);
2236 if (Parser
.hasPlt())
2237 ELFDumperStyle
->printMipsPLT(Parser
);
2245 template <class ELFT
> void ELFDumper
<ELFT
>::printAttributes() {
2246 W
.startLine() << "Attributes not implemented.\n";
2251 template <> void ELFDumper
<ELF32LE
>::printAttributes() {
2252 const ELFFile
<ELF32LE
> *Obj
= ObjF
->getELFFile();
2253 if (Obj
->getHeader()->e_machine
!= EM_ARM
) {
2254 W
.startLine() << "Attributes not implemented.\n";
2258 DictScope
BA(W
, "BuildAttributes");
2259 for (const ELFO::Elf_Shdr
&Sec
:
2260 unwrapOrError(ObjF
->getFileName(), Obj
->sections())) {
2261 if (Sec
.sh_type
!= ELF::SHT_ARM_ATTRIBUTES
)
2264 ArrayRef
<uint8_t> Contents
=
2265 unwrapOrError(ObjF
->getFileName(), Obj
->getSectionContents(&Sec
));
2266 if (Contents
[0] != ARMBuildAttrs::Format_Version
) {
2267 errs() << "unrecognised FormatVersion: 0x"
2268 << Twine::utohexstr(Contents
[0]) << '\n';
2272 W
.printHex("FormatVersion", Contents
[0]);
2273 if (Contents
.size() == 1)
2276 ARMAttributeParser(&W
).Parse(Contents
, true);
2280 template <class ELFT
> class MipsGOTParser
{
2282 TYPEDEF_ELF_TYPES(ELFT
)
2283 using Entry
= typename
ELFO::Elf_Addr
;
2284 using Entries
= ArrayRef
<Entry
>;
2286 const bool IsStatic
;
2287 const ELFO
* const Obj
;
2289 MipsGOTParser(const ELFO
*Obj
, StringRef FileName
, Elf_Dyn_Range DynTable
,
2290 Elf_Sym_Range DynSyms
);
2292 bool hasGot() const { return !GotEntries
.empty(); }
2293 bool hasPlt() const { return !PltEntries
.empty(); }
2295 uint64_t getGp() const;
2297 const Entry
*getGotLazyResolver() const;
2298 const Entry
*getGotModulePointer() const;
2299 const Entry
*getPltLazyResolver() const;
2300 const Entry
*getPltModulePointer() const;
2302 Entries
getLocalEntries() const;
2303 Entries
getGlobalEntries() const;
2304 Entries
getOtherEntries() const;
2305 Entries
getPltEntries() const;
2307 uint64_t getGotAddress(const Entry
* E
) const;
2308 int64_t getGotOffset(const Entry
* E
) const;
2309 const Elf_Sym
*getGotSym(const Entry
*E
) const;
2311 uint64_t getPltAddress(const Entry
* E
) const;
2312 const Elf_Sym
*getPltSym(const Entry
*E
) const;
2314 StringRef
getPltStrTable() const { return PltStrTable
; }
2317 const Elf_Shdr
*GotSec
;
2321 const Elf_Shdr
*PltSec
;
2322 const Elf_Shdr
*PltRelSec
;
2323 const Elf_Shdr
*PltSymTable
;
2326 Elf_Sym_Range GotDynSyms
;
2327 StringRef PltStrTable
;
2333 } // end anonymous namespace
2335 template <class ELFT
>
2336 MipsGOTParser
<ELFT
>::MipsGOTParser(const ELFO
*Obj
, StringRef FileName
,
2337 Elf_Dyn_Range DynTable
,
2338 Elf_Sym_Range DynSyms
)
2339 : IsStatic(DynTable
.empty()), Obj(Obj
), GotSec(nullptr), LocalNum(0),
2340 GlobalNum(0), PltSec(nullptr), PltRelSec(nullptr), PltSymTable(nullptr),
2341 FileName(FileName
) {
2342 // See "Global Offset Table" in Chapter 5 in the following document
2343 // for detailed GOT description.
2344 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
2346 // Find static GOT secton.
2348 GotSec
= findSectionByName(*Obj
, FileName
, ".got");
2352 ArrayRef
<uint8_t> Content
=
2353 unwrapOrError(FileName
, Obj
->getSectionContents(GotSec
));
2354 GotEntries
= Entries(reinterpret_cast<const Entry
*>(Content
.data()),
2355 Content
.size() / sizeof(Entry
));
2356 LocalNum
= GotEntries
.size();
2360 // Lookup dynamic table tags which define GOT/PLT layouts.
2361 Optional
<uint64_t> DtPltGot
;
2362 Optional
<uint64_t> DtLocalGotNum
;
2363 Optional
<uint64_t> DtGotSym
;
2364 Optional
<uint64_t> DtMipsPltGot
;
2365 Optional
<uint64_t> DtJmpRel
;
2366 for (const auto &Entry
: DynTable
) {
2367 switch (Entry
.getTag()) {
2368 case ELF::DT_PLTGOT
:
2369 DtPltGot
= Entry
.getVal();
2371 case ELF::DT_MIPS_LOCAL_GOTNO
:
2372 DtLocalGotNum
= Entry
.getVal();
2374 case ELF::DT_MIPS_GOTSYM
:
2375 DtGotSym
= Entry
.getVal();
2377 case ELF::DT_MIPS_PLTGOT
:
2378 DtMipsPltGot
= Entry
.getVal();
2380 case ELF::DT_JMPREL
:
2381 DtJmpRel
= Entry
.getVal();
2386 // Find dynamic GOT section.
2387 if (DtPltGot
|| DtLocalGotNum
|| DtGotSym
) {
2389 report_fatal_error("Cannot find PLTGOT dynamic table tag.");
2391 report_fatal_error("Cannot find MIPS_LOCAL_GOTNO dynamic table tag.");
2393 report_fatal_error("Cannot find MIPS_GOTSYM dynamic table tag.");
2395 size_t DynSymTotal
= DynSyms
.size();
2396 if (*DtGotSym
> DynSymTotal
)
2398 createError("MIPS_GOTSYM exceeds a number of dynamic symbols"),
2401 GotSec
= findNotEmptySectionByAddress(Obj
, FileName
, *DtPltGot
);
2403 reportError(createError("There is no not empty GOT section at 0x" +
2404 Twine::utohexstr(*DtPltGot
)),
2407 LocalNum
= *DtLocalGotNum
;
2408 GlobalNum
= DynSymTotal
- *DtGotSym
;
2410 ArrayRef
<uint8_t> Content
=
2411 unwrapOrError(FileName
, Obj
->getSectionContents(GotSec
));
2412 GotEntries
= Entries(reinterpret_cast<const Entry
*>(Content
.data()),
2413 Content
.size() / sizeof(Entry
));
2414 GotDynSyms
= DynSyms
.drop_front(*DtGotSym
);
2417 // Find PLT section.
2418 if (DtMipsPltGot
|| DtJmpRel
) {
2420 report_fatal_error("Cannot find MIPS_PLTGOT dynamic table tag.");
2422 report_fatal_error("Cannot find JMPREL dynamic table tag.");
2424 PltSec
= findNotEmptySectionByAddress(Obj
, FileName
, * DtMipsPltGot
);
2426 report_fatal_error("There is no not empty PLTGOT section at 0x " +
2427 Twine::utohexstr(*DtMipsPltGot
));
2429 PltRelSec
= findNotEmptySectionByAddress(Obj
, FileName
, * DtJmpRel
);
2431 report_fatal_error("There is no not empty RELPLT section at 0x" +
2432 Twine::utohexstr(*DtJmpRel
));
2434 ArrayRef
<uint8_t> PltContent
=
2435 unwrapOrError(FileName
, Obj
->getSectionContents(PltSec
));
2436 PltEntries
= Entries(reinterpret_cast<const Entry
*>(PltContent
.data()),
2437 PltContent
.size() / sizeof(Entry
));
2439 PltSymTable
= unwrapOrError(FileName
, Obj
->getSection(PltRelSec
->sh_link
));
2441 unwrapOrError(FileName
, Obj
->getStringTableForSymtab(*PltSymTable
));
2445 template <class ELFT
> uint64_t MipsGOTParser
<ELFT
>::getGp() const {
2446 return GotSec
->sh_addr
+ 0x7ff0;
2449 template <class ELFT
>
2450 const typename MipsGOTParser
<ELFT
>::Entry
*
2451 MipsGOTParser
<ELFT
>::getGotLazyResolver() const {
2452 return LocalNum
> 0 ? &GotEntries
[0] : nullptr;
2455 template <class ELFT
>
2456 const typename MipsGOTParser
<ELFT
>::Entry
*
2457 MipsGOTParser
<ELFT
>::getGotModulePointer() const {
2460 const Entry
&E
= GotEntries
[1];
2461 if ((E
>> (sizeof(Entry
) * 8 - 1)) == 0)
2466 template <class ELFT
>
2467 typename MipsGOTParser
<ELFT
>::Entries
2468 MipsGOTParser
<ELFT
>::getLocalEntries() const {
2469 size_t Skip
= getGotModulePointer() ? 2 : 1;
2470 if (LocalNum
- Skip
<= 0)
2472 return GotEntries
.slice(Skip
, LocalNum
- Skip
);
2475 template <class ELFT
>
2476 typename MipsGOTParser
<ELFT
>::Entries
2477 MipsGOTParser
<ELFT
>::getGlobalEntries() const {
2480 return GotEntries
.slice(LocalNum
, GlobalNum
);
2483 template <class ELFT
>
2484 typename MipsGOTParser
<ELFT
>::Entries
2485 MipsGOTParser
<ELFT
>::getOtherEntries() const {
2486 size_t OtherNum
= GotEntries
.size() - LocalNum
- GlobalNum
;
2489 return GotEntries
.slice(LocalNum
+ GlobalNum
, OtherNum
);
2492 template <class ELFT
>
2493 uint64_t MipsGOTParser
<ELFT
>::getGotAddress(const Entry
*E
) const {
2494 int64_t Offset
= std::distance(GotEntries
.data(), E
) * sizeof(Entry
);
2495 return GotSec
->sh_addr
+ Offset
;
2498 template <class ELFT
>
2499 int64_t MipsGOTParser
<ELFT
>::getGotOffset(const Entry
*E
) const {
2500 int64_t Offset
= std::distance(GotEntries
.data(), E
) * sizeof(Entry
);
2501 return Offset
- 0x7ff0;
2504 template <class ELFT
>
2505 const typename MipsGOTParser
<ELFT
>::Elf_Sym
*
2506 MipsGOTParser
<ELFT
>::getGotSym(const Entry
*E
) const {
2507 int64_t Offset
= std::distance(GotEntries
.data(), E
);
2508 return &GotDynSyms
[Offset
- LocalNum
];
2511 template <class ELFT
>
2512 const typename MipsGOTParser
<ELFT
>::Entry
*
2513 MipsGOTParser
<ELFT
>::getPltLazyResolver() const {
2514 return PltEntries
.empty() ? nullptr : &PltEntries
[0];
2517 template <class ELFT
>
2518 const typename MipsGOTParser
<ELFT
>::Entry
*
2519 MipsGOTParser
<ELFT
>::getPltModulePointer() const {
2520 return PltEntries
.size() < 2 ? nullptr : &PltEntries
[1];
2523 template <class ELFT
>
2524 typename MipsGOTParser
<ELFT
>::Entries
2525 MipsGOTParser
<ELFT
>::getPltEntries() const {
2526 if (PltEntries
.size() <= 2)
2528 return PltEntries
.slice(2, PltEntries
.size() - 2);
2531 template <class ELFT
>
2532 uint64_t MipsGOTParser
<ELFT
>::getPltAddress(const Entry
*E
) const {
2533 int64_t Offset
= std::distance(PltEntries
.data(), E
) * sizeof(Entry
);
2534 return PltSec
->sh_addr
+ Offset
;
2537 template <class ELFT
>
2538 const typename MipsGOTParser
<ELFT
>::Elf_Sym
*
2539 MipsGOTParser
<ELFT
>::getPltSym(const Entry
*E
) const {
2540 int64_t Offset
= std::distance(getPltEntries().data(), E
);
2541 if (PltRelSec
->sh_type
== ELF::SHT_REL
) {
2542 Elf_Rel_Range Rels
= unwrapOrError(FileName
, Obj
->rels(PltRelSec
));
2543 return unwrapOrError(FileName
,
2544 Obj
->getRelocationSymbol(&Rels
[Offset
], PltSymTable
));
2546 Elf_Rela_Range Rels
= unwrapOrError(FileName
, Obj
->relas(PltRelSec
));
2547 return unwrapOrError(FileName
,
2548 Obj
->getRelocationSymbol(&Rels
[Offset
], PltSymTable
));
2552 static const EnumEntry
<unsigned> ElfMipsISAExtType
[] = {
2553 {"None", Mips::AFL_EXT_NONE
},
2554 {"Broadcom SB-1", Mips::AFL_EXT_SB1
},
2555 {"Cavium Networks Octeon", Mips::AFL_EXT_OCTEON
},
2556 {"Cavium Networks Octeon2", Mips::AFL_EXT_OCTEON2
},
2557 {"Cavium Networks OcteonP", Mips::AFL_EXT_OCTEONP
},
2558 {"Cavium Networks Octeon3", Mips::AFL_EXT_OCTEON3
},
2559 {"LSI R4010", Mips::AFL_EXT_4010
},
2560 {"Loongson 2E", Mips::AFL_EXT_LOONGSON_2E
},
2561 {"Loongson 2F", Mips::AFL_EXT_LOONGSON_2F
},
2562 {"Loongson 3A", Mips::AFL_EXT_LOONGSON_3A
},
2563 {"MIPS R4650", Mips::AFL_EXT_4650
},
2564 {"MIPS R5900", Mips::AFL_EXT_5900
},
2565 {"MIPS R10000", Mips::AFL_EXT_10000
},
2566 {"NEC VR4100", Mips::AFL_EXT_4100
},
2567 {"NEC VR4111/VR4181", Mips::AFL_EXT_4111
},
2568 {"NEC VR4120", Mips::AFL_EXT_4120
},
2569 {"NEC VR5400", Mips::AFL_EXT_5400
},
2570 {"NEC VR5500", Mips::AFL_EXT_5500
},
2571 {"RMI Xlr", Mips::AFL_EXT_XLR
},
2572 {"Toshiba R3900", Mips::AFL_EXT_3900
}
2575 static const EnumEntry
<unsigned> ElfMipsASEFlags
[] = {
2576 {"DSP", Mips::AFL_ASE_DSP
},
2577 {"DSPR2", Mips::AFL_ASE_DSPR2
},
2578 {"Enhanced VA Scheme", Mips::AFL_ASE_EVA
},
2579 {"MCU", Mips::AFL_ASE_MCU
},
2580 {"MDMX", Mips::AFL_ASE_MDMX
},
2581 {"MIPS-3D", Mips::AFL_ASE_MIPS3D
},
2582 {"MT", Mips::AFL_ASE_MT
},
2583 {"SmartMIPS", Mips::AFL_ASE_SMARTMIPS
},
2584 {"VZ", Mips::AFL_ASE_VIRT
},
2585 {"MSA", Mips::AFL_ASE_MSA
},
2586 {"MIPS16", Mips::AFL_ASE_MIPS16
},
2587 {"microMIPS", Mips::AFL_ASE_MICROMIPS
},
2588 {"XPA", Mips::AFL_ASE_XPA
},
2589 {"CRC", Mips::AFL_ASE_CRC
},
2590 {"GINV", Mips::AFL_ASE_GINV
},
2593 static const EnumEntry
<unsigned> ElfMipsFpABIType
[] = {
2594 {"Hard or soft float", Mips::Val_GNU_MIPS_ABI_FP_ANY
},
2595 {"Hard float (double precision)", Mips::Val_GNU_MIPS_ABI_FP_DOUBLE
},
2596 {"Hard float (single precision)", Mips::Val_GNU_MIPS_ABI_FP_SINGLE
},
2597 {"Soft float", Mips::Val_GNU_MIPS_ABI_FP_SOFT
},
2598 {"Hard float (MIPS32r2 64-bit FPU 12 callee-saved)",
2599 Mips::Val_GNU_MIPS_ABI_FP_OLD_64
},
2600 {"Hard float (32-bit CPU, Any FPU)", Mips::Val_GNU_MIPS_ABI_FP_XX
},
2601 {"Hard float (32-bit CPU, 64-bit FPU)", Mips::Val_GNU_MIPS_ABI_FP_64
},
2602 {"Hard float compat (32-bit CPU, 64-bit FPU)",
2603 Mips::Val_GNU_MIPS_ABI_FP_64A
}
2606 static const EnumEntry
<unsigned> ElfMipsFlags1
[] {
2607 {"ODDSPREG", Mips::AFL_FLAGS1_ODDSPREG
},
2610 static int getMipsRegisterSize(uint8_t Flag
) {
2612 case Mips::AFL_REG_NONE
:
2614 case Mips::AFL_REG_32
:
2616 case Mips::AFL_REG_64
:
2618 case Mips::AFL_REG_128
:
2625 template <class ELFT
>
2626 static void printMipsReginfoData(ScopedPrinter
&W
,
2627 const Elf_Mips_RegInfo
<ELFT
> &Reginfo
) {
2628 W
.printHex("GP", Reginfo
.ri_gp_value
);
2629 W
.printHex("General Mask", Reginfo
.ri_gprmask
);
2630 W
.printHex("Co-Proc Mask0", Reginfo
.ri_cprmask
[0]);
2631 W
.printHex("Co-Proc Mask1", Reginfo
.ri_cprmask
[1]);
2632 W
.printHex("Co-Proc Mask2", Reginfo
.ri_cprmask
[2]);
2633 W
.printHex("Co-Proc Mask3", Reginfo
.ri_cprmask
[3]);
2636 template <class ELFT
> void ELFDumper
<ELFT
>::printMipsReginfo() {
2637 const ELFFile
<ELFT
> *Obj
= ObjF
->getELFFile();
2638 const Elf_Shdr
*Shdr
= findSectionByName(*Obj
, ObjF
->getFileName(), ".reginfo");
2640 W
.startLine() << "There is no .reginfo section in the file.\n";
2643 ArrayRef
<uint8_t> Sec
=
2644 unwrapOrError(ObjF
->getFileName(), Obj
->getSectionContents(Shdr
));
2645 if (Sec
.size() != sizeof(Elf_Mips_RegInfo
<ELFT
>)) {
2646 W
.startLine() << "The .reginfo section has a wrong size.\n";
2650 DictScope
GS(W
, "MIPS RegInfo");
2651 auto *Reginfo
= reinterpret_cast<const Elf_Mips_RegInfo
<ELFT
> *>(Sec
.data());
2652 printMipsReginfoData(W
, *Reginfo
);
2655 template <class ELFT
> void ELFDumper
<ELFT
>::printMipsOptions() {
2656 const ELFFile
<ELFT
> *Obj
= ObjF
->getELFFile();
2657 const Elf_Shdr
*Shdr
=
2658 findSectionByName(*Obj
, ObjF
->getFileName(), ".MIPS.options");
2660 W
.startLine() << "There is no .MIPS.options section in the file.\n";
2664 DictScope
GS(W
, "MIPS Options");
2666 ArrayRef
<uint8_t> Sec
=
2667 unwrapOrError(ObjF
->getFileName(), Obj
->getSectionContents(Shdr
));
2668 while (!Sec
.empty()) {
2669 if (Sec
.size() < sizeof(Elf_Mips_Options
<ELFT
>)) {
2670 W
.startLine() << "The .MIPS.options section has a wrong size.\n";
2673 auto *O
= reinterpret_cast<const Elf_Mips_Options
<ELFT
> *>(Sec
.data());
2674 DictScope
GS(W
, getElfMipsOptionsOdkType(O
->kind
));
2677 printMipsReginfoData(W
, O
->getRegInfo());
2680 W
.startLine() << "Unsupported MIPS options tag.\n";
2683 Sec
= Sec
.slice(O
->size
);
2687 template <class ELFT
> void ELFDumper
<ELFT
>::printStackMap() const {
2688 const ELFFile
<ELFT
> *Obj
= ObjF
->getELFFile();
2689 const Elf_Shdr
*StackMapSection
= nullptr;
2690 for (const auto &Sec
: unwrapOrError(ObjF
->getFileName(), Obj
->sections())) {
2692 unwrapOrError(ObjF
->getFileName(), Obj
->getSectionName(&Sec
));
2693 if (Name
== ".llvm_stackmaps") {
2694 StackMapSection
= &Sec
;
2699 if (!StackMapSection
)
2702 ArrayRef
<uint8_t> StackMapContentsArray
= unwrapOrError(
2703 ObjF
->getFileName(), Obj
->getSectionContents(StackMapSection
));
2705 prettyPrintStackMap(
2706 W
, StackMapParser
<ELFT::TargetEndianness
>(StackMapContentsArray
));
2709 template <class ELFT
> void ELFDumper
<ELFT
>::printGroupSections() {
2710 ELFDumperStyle
->printGroupSections(ObjF
->getELFFile());
2713 template <class ELFT
> void ELFDumper
<ELFT
>::printAddrsig() {
2714 ELFDumperStyle
->printAddrsig(ObjF
->getELFFile());
2717 static inline void printFields(formatted_raw_ostream
&OS
, StringRef Str1
,
2721 OS
.PadToColumn(37u);
2726 template <class ELFT
>
2727 static std::string
getSectionHeadersNumString(const ELFFile
<ELFT
> *Obj
,
2728 StringRef FileName
) {
2729 const typename
ELFT::Ehdr
*ElfHeader
= Obj
->getHeader();
2730 if (ElfHeader
->e_shnum
!= 0)
2731 return to_string(ElfHeader
->e_shnum
);
2733 ArrayRef
<typename
ELFT::Shdr
> Arr
= unwrapOrError(FileName
, Obj
->sections());
2736 return "0 (" + to_string(Arr
[0].sh_size
) + ")";
2739 template <class ELFT
>
2740 static std::string
getSectionHeaderTableIndexString(const ELFFile
<ELFT
> *Obj
,
2741 StringRef FileName
) {
2742 const typename
ELFT::Ehdr
*ElfHeader
= Obj
->getHeader();
2743 if (ElfHeader
->e_shstrndx
!= SHN_XINDEX
)
2744 return to_string(ElfHeader
->e_shstrndx
);
2746 ArrayRef
<typename
ELFT::Shdr
> Arr
= unwrapOrError(FileName
, Obj
->sections());
2748 return "65535 (corrupt: out of range)";
2749 return to_string(ElfHeader
->e_shstrndx
) + " (" + to_string(Arr
[0].sh_link
) +
2753 template <class ELFT
> void GNUStyle
<ELFT
>::printFileHeaders(const ELFO
*Obj
) {
2754 const Elf_Ehdr
*e
= Obj
->getHeader();
2755 OS
<< "ELF Header:\n";
2758 for (int i
= 0; i
< ELF::EI_NIDENT
; i
++)
2759 OS
<< format(" %02x", static_cast<int>(e
->e_ident
[i
]));
2761 Str
= printEnum(e
->e_ident
[ELF::EI_CLASS
], makeArrayRef(ElfClass
));
2762 printFields(OS
, "Class:", Str
);
2763 Str
= printEnum(e
->e_ident
[ELF::EI_DATA
], makeArrayRef(ElfDataEncoding
));
2764 printFields(OS
, "Data:", Str
);
2767 OS
.PadToColumn(37u);
2768 OS
<< to_hexString(e
->e_ident
[ELF::EI_VERSION
]);
2769 if (e
->e_version
== ELF::EV_CURRENT
)
2772 Str
= printEnum(e
->e_ident
[ELF::EI_OSABI
], makeArrayRef(ElfOSABI
));
2773 printFields(OS
, "OS/ABI:", Str
);
2774 Str
= "0x" + to_hexString(e
->e_ident
[ELF::EI_ABIVERSION
]);
2775 printFields(OS
, "ABI Version:", Str
);
2776 Str
= printEnum(e
->e_type
, makeArrayRef(ElfObjectFileType
));
2777 printFields(OS
, "Type:", Str
);
2778 Str
= printEnum(e
->e_machine
, makeArrayRef(ElfMachineType
));
2779 printFields(OS
, "Machine:", Str
);
2780 Str
= "0x" + to_hexString(e
->e_version
);
2781 printFields(OS
, "Version:", Str
);
2782 Str
= "0x" + to_hexString(e
->e_entry
);
2783 printFields(OS
, "Entry point address:", Str
);
2784 Str
= to_string(e
->e_phoff
) + " (bytes into file)";
2785 printFields(OS
, "Start of program headers:", Str
);
2786 Str
= to_string(e
->e_shoff
) + " (bytes into file)";
2787 printFields(OS
, "Start of section headers:", Str
);
2788 std::string ElfFlags
;
2789 if (e
->e_machine
== EM_MIPS
)
2791 printFlags(e
->e_flags
, makeArrayRef(ElfHeaderMipsFlags
),
2792 unsigned(ELF::EF_MIPS_ARCH
), unsigned(ELF::EF_MIPS_ABI
),
2793 unsigned(ELF::EF_MIPS_MACH
));
2794 else if (e
->e_machine
== EM_RISCV
)
2795 ElfFlags
= printFlags(e
->e_flags
, makeArrayRef(ElfHeaderRISCVFlags
));
2796 Str
= "0x" + to_hexString(e
->e_flags
);
2797 if (!ElfFlags
.empty())
2798 Str
= Str
+ ", " + ElfFlags
;
2799 printFields(OS
, "Flags:", Str
);
2800 Str
= to_string(e
->e_ehsize
) + " (bytes)";
2801 printFields(OS
, "Size of this header:", Str
);
2802 Str
= to_string(e
->e_phentsize
) + " (bytes)";
2803 printFields(OS
, "Size of program headers:", Str
);
2804 Str
= to_string(e
->e_phnum
);
2805 printFields(OS
, "Number of program headers:", Str
);
2806 Str
= to_string(e
->e_shentsize
) + " (bytes)";
2807 printFields(OS
, "Size of section headers:", Str
);
2808 Str
= getSectionHeadersNumString(Obj
, this->FileName
);
2809 printFields(OS
, "Number of section headers:", Str
);
2810 Str
= getSectionHeaderTableIndexString(Obj
, this->FileName
);
2811 printFields(OS
, "Section header string table index:", Str
);
2815 struct GroupMember
{
2820 struct GroupSection
{
2822 std::string Signature
;
2828 std::vector
<GroupMember
> Members
;
2831 template <class ELFT
>
2832 std::vector
<GroupSection
> getGroups(const ELFFile
<ELFT
> *Obj
,
2833 StringRef FileName
) {
2834 using Elf_Shdr
= typename
ELFT::Shdr
;
2835 using Elf_Sym
= typename
ELFT::Sym
;
2836 using Elf_Word
= typename
ELFT::Word
;
2838 std::vector
<GroupSection
> Ret
;
2840 for (const Elf_Shdr
&Sec
: unwrapOrError(FileName
, Obj
->sections())) {
2842 if (Sec
.sh_type
!= ELF::SHT_GROUP
)
2845 const Elf_Shdr
*Symtab
=
2846 unwrapOrError(FileName
, Obj
->getSection(Sec
.sh_link
));
2847 StringRef StrTable
=
2848 unwrapOrError(FileName
, Obj
->getStringTableForSymtab(*Symtab
));
2849 const Elf_Sym
*Sym
= unwrapOrError(
2850 FileName
, Obj
->template getEntry
<Elf_Sym
>(Symtab
, Sec
.sh_info
));
2851 auto Data
= unwrapOrError(
2852 FileName
, Obj
->template getSectionContentsAsArray
<Elf_Word
>(&Sec
));
2854 StringRef Name
= unwrapOrError(FileName
, Obj
->getSectionName(&Sec
));
2855 StringRef Signature
= StrTable
.data() + Sym
->st_name
;
2856 Ret
.push_back({Name
,
2857 maybeDemangle(Signature
),
2865 std::vector
<GroupMember
> &GM
= Ret
.back().Members
;
2866 for (uint32_t Ndx
: Data
.slice(1)) {
2867 auto Sec
= unwrapOrError(FileName
, Obj
->getSection(Ndx
));
2868 const StringRef Name
= unwrapOrError(FileName
, Obj
->getSectionName(Sec
));
2869 GM
.push_back({Name
, Ndx
});
2875 DenseMap
<uint64_t, const GroupSection
*>
2876 mapSectionsToGroups(ArrayRef
<GroupSection
> Groups
) {
2877 DenseMap
<uint64_t, const GroupSection
*> Ret
;
2878 for (const GroupSection
&G
: Groups
)
2879 for (const GroupMember
&GM
: G
.Members
)
2880 Ret
.insert({GM
.Index
, &G
});
2886 template <class ELFT
> void GNUStyle
<ELFT
>::printGroupSections(const ELFO
*Obj
) {
2887 std::vector
<GroupSection
> V
= getGroups
<ELFT
>(Obj
, this->FileName
);
2888 DenseMap
<uint64_t, const GroupSection
*> Map
= mapSectionsToGroups(V
);
2889 for (const GroupSection
&G
: V
) {
2891 << getGroupType(G
.Type
) << " group section ["
2892 << format_decimal(G
.Index
, 5) << "] `" << G
.Name
<< "' [" << G
.Signature
2893 << "] contains " << G
.Members
.size() << " sections:\n"
2894 << " [Index] Name\n";
2895 for (const GroupMember
&GM
: G
.Members
) {
2896 const GroupSection
*MainGroup
= Map
[GM
.Index
];
2897 if (MainGroup
!= &G
) {
2899 errs() << "Error: section [" << format_decimal(GM
.Index
, 5)
2900 << "] in group section [" << format_decimal(G
.Index
, 5)
2901 << "] already in group section ["
2902 << format_decimal(MainGroup
->Index
, 5) << "]";
2906 OS
<< " [" << format_decimal(GM
.Index
, 5) << "] " << GM
.Name
<< "\n";
2911 OS
<< "There are no section groups in this file.\n";
2914 template <class ELFT
>
2915 void GNUStyle
<ELFT
>::printRelocation(const ELFO
*Obj
, const Elf_Shdr
*SymTab
,
2916 const Elf_Rela
&R
, bool IsRela
) {
2917 const Elf_Sym
*Sym
=
2918 unwrapOrError(this->FileName
, Obj
->getRelocationSymbol(&R
, SymTab
));
2919 std::string TargetName
;
2920 if (Sym
&& Sym
->getType() == ELF::STT_SECTION
) {
2921 const Elf_Shdr
*Sec
= unwrapOrError(
2923 Obj
->getSection(Sym
, SymTab
, this->dumper()->getShndxTable()));
2924 TargetName
= unwrapOrError(this->FileName
, Obj
->getSectionName(Sec
));
2926 StringRef StrTable
=
2927 unwrapOrError(this->FileName
, Obj
->getStringTableForSymtab(*SymTab
));
2928 TargetName
= this->dumper()->getFullSymbolName(
2929 Sym
, StrTable
, SymTab
->sh_type
== SHT_DYNSYM
/* IsDynamic */);
2931 printRelocation(Obj
, Sym
, TargetName
, R
, IsRela
);
2934 template <class ELFT
>
2935 void GNUStyle
<ELFT
>::printRelocation(const ELFO
*Obj
, const Elf_Sym
*Sym
,
2936 StringRef SymbolName
, const Elf_Rela
&R
,
2938 // First two fields are bit width dependent. The rest of them are fixed width.
2939 unsigned Bias
= ELFT::Is64Bits
? 8 : 0;
2940 Field Fields
[5] = {0, 10 + Bias
, 19 + 2 * Bias
, 42 + 2 * Bias
, 53 + 2 * Bias
};
2941 unsigned Width
= ELFT::Is64Bits
? 16 : 8;
2943 Fields
[0].Str
= to_string(format_hex_no_prefix(R
.r_offset
, Width
));
2944 Fields
[1].Str
= to_string(format_hex_no_prefix(R
.r_info
, Width
));
2946 SmallString
<32> RelocName
;
2947 Obj
->getRelocationTypeName(R
.getType(Obj
->isMips64EL()), RelocName
);
2948 Fields
[2].Str
= RelocName
.c_str();
2950 if (Sym
&& (!SymbolName
.empty() || Sym
->getValue() != 0))
2951 Fields
[3].Str
= to_string(format_hex_no_prefix(Sym
->getValue(), Width
));
2953 Fields
[4].Str
= SymbolName
;
2954 for (const Field
&F
: Fields
)
2959 int64_t RelAddend
= R
.r_addend
;
2960 if (!SymbolName
.empty()) {
2961 if (R
.r_addend
< 0) {
2963 RelAddend
= std::abs(RelAddend
);
2968 Addend
+= to_hexString(RelAddend
, false);
2970 OS
<< Addend
<< "\n";
2973 template <class ELFT
> void GNUStyle
<ELFT
>::printRelocHeader(unsigned SType
) {
2974 bool IsRela
= SType
== ELF::SHT_RELA
|| SType
== ELF::SHT_ANDROID_RELA
;
2975 bool IsRelr
= SType
== ELF::SHT_RELR
|| SType
== ELF::SHT_ANDROID_RELR
;
2980 if (IsRelr
&& opts::RawRelr
)
2986 << " Symbol's Value Symbol's Name";
2988 OS
<< " Info Type Sym. Value Symbol's Name";
2994 template <class ELFT
> void GNUStyle
<ELFT
>::printRelocations(const ELFO
*Obj
) {
2995 bool HasRelocSections
= false;
2996 for (const Elf_Shdr
&Sec
: unwrapOrError(this->FileName
, Obj
->sections())) {
2997 if (Sec
.sh_type
!= ELF::SHT_REL
&& Sec
.sh_type
!= ELF::SHT_RELA
&&
2998 Sec
.sh_type
!= ELF::SHT_RELR
&& Sec
.sh_type
!= ELF::SHT_ANDROID_REL
&&
2999 Sec
.sh_type
!= ELF::SHT_ANDROID_RELA
&&
3000 Sec
.sh_type
!= ELF::SHT_ANDROID_RELR
)
3002 HasRelocSections
= true;
3003 StringRef Name
= unwrapOrError(this->FileName
, Obj
->getSectionName(&Sec
));
3004 unsigned Entries
= Sec
.getEntityCount();
3005 std::vector
<Elf_Rela
> AndroidRelas
;
3006 if (Sec
.sh_type
== ELF::SHT_ANDROID_REL
||
3007 Sec
.sh_type
== ELF::SHT_ANDROID_RELA
) {
3008 // Android's packed relocation section needs to be unpacked first
3009 // to get the actual number of entries.
3010 AndroidRelas
= unwrapOrError(this->FileName
, Obj
->android_relas(&Sec
));
3011 Entries
= AndroidRelas
.size();
3013 std::vector
<Elf_Rela
> RelrRelas
;
3014 if (!opts::RawRelr
&& (Sec
.sh_type
== ELF::SHT_RELR
||
3015 Sec
.sh_type
== ELF::SHT_ANDROID_RELR
)) {
3016 // .relr.dyn relative relocation section needs to be unpacked first
3017 // to get the actual number of entries.
3018 Elf_Relr_Range Relrs
= unwrapOrError(this->FileName
, Obj
->relrs(&Sec
));
3019 RelrRelas
= unwrapOrError(this->FileName
, Obj
->decode_relrs(Relrs
));
3020 Entries
= RelrRelas
.size();
3022 uintX_t Offset
= Sec
.sh_offset
;
3023 OS
<< "\nRelocation section '" << Name
<< "' at offset 0x"
3024 << to_hexString(Offset
, false) << " contains " << Entries
3026 printRelocHeader(Sec
.sh_type
);
3027 const Elf_Shdr
*SymTab
=
3028 unwrapOrError(this->FileName
, Obj
->getSection(Sec
.sh_link
));
3029 switch (Sec
.sh_type
) {
3031 for (const auto &R
: unwrapOrError(this->FileName
, Obj
->rels(&Sec
))) {
3033 Rela
.r_offset
= R
.r_offset
;
3034 Rela
.r_info
= R
.r_info
;
3036 printRelocation(Obj
, SymTab
, Rela
, false);
3040 for (const auto &R
: unwrapOrError(this->FileName
, Obj
->relas(&Sec
)))
3041 printRelocation(Obj
, SymTab
, R
, true);
3044 case ELF::SHT_ANDROID_RELR
:
3046 for (const auto &R
: unwrapOrError(this->FileName
, Obj
->relrs(&Sec
)))
3047 OS
<< to_string(format_hex_no_prefix(R
, ELFT::Is64Bits
? 16 : 8))
3050 for (const auto &R
: RelrRelas
)
3051 printRelocation(Obj
, SymTab
, R
, false);
3053 case ELF::SHT_ANDROID_REL
:
3054 case ELF::SHT_ANDROID_RELA
:
3055 for (const auto &R
: AndroidRelas
)
3056 printRelocation(Obj
, SymTab
, R
, Sec
.sh_type
== ELF::SHT_ANDROID_RELA
);
3060 if (!HasRelocSections
)
3061 OS
<< "\nThere are no relocations in this file.\n";
3064 // Print the offset of a particular section from anyone of the ranges:
3065 // [SHT_LOOS, SHT_HIOS], [SHT_LOPROC, SHT_HIPROC], [SHT_LOUSER, SHT_HIUSER].
3066 // If 'Type' does not fall within any of those ranges, then a string is
3067 // returned as '<unknown>' followed by the type value.
3068 static std::string
getSectionTypeOffsetString(unsigned Type
) {
3069 if (Type
>= SHT_LOOS
&& Type
<= SHT_HIOS
)
3070 return "LOOS+0x" + to_hexString(Type
- SHT_LOOS
);
3071 else if (Type
>= SHT_LOPROC
&& Type
<= SHT_HIPROC
)
3072 return "LOPROC+0x" + to_hexString(Type
- SHT_LOPROC
);
3073 else if (Type
>= SHT_LOUSER
&& Type
<= SHT_HIUSER
)
3074 return "LOUSER+0x" + to_hexString(Type
- SHT_LOUSER
);
3075 return "0x" + to_hexString(Type
) + ": <unknown>";
3078 static std::string
getSectionTypeString(unsigned Arch
, unsigned Type
) {
3079 using namespace ELF
;
3086 case SHT_ARM_PREEMPTMAP
:
3087 return "ARM_PREEMPTMAP";
3088 case SHT_ARM_ATTRIBUTES
:
3089 return "ARM_ATTRIBUTES";
3090 case SHT_ARM_DEBUGOVERLAY
:
3091 return "ARM_DEBUGOVERLAY";
3092 case SHT_ARM_OVERLAYSECTION
:
3093 return "ARM_OVERLAYSECTION";
3098 case SHT_X86_64_UNWIND
:
3099 return "X86_64_UNWIND";
3103 case EM_MIPS_RS3_LE
:
3105 case SHT_MIPS_REGINFO
:
3106 return "MIPS_REGINFO";
3107 case SHT_MIPS_OPTIONS
:
3108 return "MIPS_OPTIONS";
3109 case SHT_MIPS_DWARF
:
3110 return "MIPS_DWARF";
3111 case SHT_MIPS_ABIFLAGS
:
3112 return "MIPS_ABIFLAGS";
3141 case SHT_INIT_ARRAY
:
3142 return "INIT_ARRAY";
3143 case SHT_FINI_ARRAY
:
3144 return "FINI_ARRAY";
3145 case SHT_PREINIT_ARRAY
:
3146 return "PREINIT_ARRAY";
3149 case SHT_SYMTAB_SHNDX
:
3150 return "SYMTAB SECTION INDICES";
3151 case SHT_ANDROID_REL
:
3152 return "ANDROID_REL";
3153 case SHT_ANDROID_RELA
:
3154 return "ANDROID_RELA";
3156 case SHT_ANDROID_RELR
:
3158 case SHT_LLVM_ODRTAB
:
3159 return "LLVM_ODRTAB";
3160 case SHT_LLVM_LINKER_OPTIONS
:
3161 return "LLVM_LINKER_OPTIONS";
3162 case SHT_LLVM_CALL_GRAPH_PROFILE
:
3163 return "LLVM_CALL_GRAPH_PROFILE";
3164 case SHT_LLVM_ADDRSIG
:
3165 return "LLVM_ADDRSIG";
3166 case SHT_LLVM_DEPENDENT_LIBRARIES
:
3167 return "LLVM_DEPENDENT_LIBRARIES";
3168 case SHT_LLVM_SYMPART
:
3169 return "LLVM_SYMPART";
3170 case SHT_LLVM_PART_EHDR
:
3171 return "LLVM_PART_EHDR";
3172 case SHT_LLVM_PART_PHDR
:
3173 return "LLVM_PART_PHDR";
3174 // FIXME: Parse processor specific GNU attributes
3175 case SHT_GNU_ATTRIBUTES
:
3176 return "ATTRIBUTES";
3179 case SHT_GNU_verdef
:
3181 case SHT_GNU_verneed
:
3183 case SHT_GNU_versym
:
3186 return getSectionTypeOffsetString(Type
);
3191 template <class ELFT
>
3192 void GNUStyle
<ELFT
>::printSectionHeaders(const ELFO
*Obj
) {
3193 unsigned Bias
= ELFT::Is64Bits
? 0 : 8;
3194 ArrayRef
<Elf_Shdr
> Sections
= unwrapOrError(this->FileName
, Obj
->sections());
3195 OS
<< "There are " << to_string(Sections
.size())
3196 << " section headers, starting at offset "
3197 << "0x" << to_hexString(Obj
->getHeader()->e_shoff
, false) << ":\n\n";
3198 OS
<< "Section Headers:\n";
3199 Field Fields
[11] = {
3200 {"[Nr]", 2}, {"Name", 7}, {"Type", 25},
3201 {"Address", 41}, {"Off", 58 - Bias
}, {"Size", 65 - Bias
},
3202 {"ES", 72 - Bias
}, {"Flg", 75 - Bias
}, {"Lk", 79 - Bias
},
3203 {"Inf", 82 - Bias
}, {"Al", 86 - Bias
}};
3204 for (auto &F
: Fields
)
3208 const ELFObjectFile
<ELFT
> *ElfObj
= this->dumper()->getElfObject();
3209 size_t SectionIndex
= 0;
3210 for (const Elf_Shdr
&Sec
: Sections
) {
3211 Fields
[0].Str
= to_string(SectionIndex
);
3212 Fields
[1].Str
= unwrapOrError
<StringRef
>(
3213 ElfObj
->getFileName(), Obj
->getSectionName(&Sec
, this->WarningHandler
));
3215 getSectionTypeString(Obj
->getHeader()->e_machine
, Sec
.sh_type
);
3217 to_string(format_hex_no_prefix(Sec
.sh_addr
, ELFT::Is64Bits
? 16 : 8));
3218 Fields
[4].Str
= to_string(format_hex_no_prefix(Sec
.sh_offset
, 6));
3219 Fields
[5].Str
= to_string(format_hex_no_prefix(Sec
.sh_size
, 6));
3220 Fields
[6].Str
= to_string(format_hex_no_prefix(Sec
.sh_entsize
, 2));
3221 Fields
[7].Str
= getGNUFlags(Sec
.sh_flags
);
3222 Fields
[8].Str
= to_string(Sec
.sh_link
);
3223 Fields
[9].Str
= to_string(Sec
.sh_info
);
3224 Fields
[10].Str
= to_string(Sec
.sh_addralign
);
3226 OS
.PadToColumn(Fields
[0].Column
);
3227 OS
<< "[" << right_justify(Fields
[0].Str
, 2) << "]";
3228 for (int i
= 1; i
< 7; i
++)
3229 printField(Fields
[i
]);
3230 OS
.PadToColumn(Fields
[7].Column
);
3231 OS
<< right_justify(Fields
[7].Str
, 3);
3232 OS
.PadToColumn(Fields
[8].Column
);
3233 OS
<< right_justify(Fields
[8].Str
, 2);
3234 OS
.PadToColumn(Fields
[9].Column
);
3235 OS
<< right_justify(Fields
[9].Str
, 3);
3236 OS
.PadToColumn(Fields
[10].Column
);
3237 OS
<< right_justify(Fields
[10].Str
, 2);
3241 OS
<< "Key to Flags:\n"
3242 << " W (write), A (alloc), X (execute), M (merge), S (strings), l "
3244 << " I (info), L (link order), G (group), T (TLS), E (exclude),\
3246 << " O (extra OS processing required) o (OS specific),\
3247 p (processor specific)\n";
3250 template <class ELFT
>
3251 void GNUStyle
<ELFT
>::printSymtabMessage(const ELFO
*Obj
, StringRef Name
,
3253 bool NonVisibilityBitsUsed
) {
3255 OS
<< "\nSymbol table '" << Name
<< "' contains " << Entries
3258 OS
<< "\n Symbol table for image:\n";
3261 OS
<< " Num: Value Size Type Bind Vis";
3263 OS
<< " Num: Value Size Type Bind Vis";
3265 if (NonVisibilityBitsUsed
)
3267 OS
<< " Ndx Name\n";
3270 template <class ELFT
>
3271 std::string GNUStyle
<ELFT
>::getSymbolSectionNdx(const ELFO
*Obj
,
3272 const Elf_Sym
*Symbol
,
3273 const Elf_Sym
*FirstSym
) {
3274 unsigned SectionIndex
= Symbol
->st_shndx
;
3275 switch (SectionIndex
) {
3276 case ELF::SHN_UNDEF
:
3280 case ELF::SHN_COMMON
:
3282 case ELF::SHN_XINDEX
:
3283 return to_string(format_decimal(
3284 unwrapOrError(this->FileName
,
3285 object::getExtendedSymbolTableIndex
<ELFT
>(
3286 Symbol
, FirstSym
, this->dumper()->getShndxTable())),
3290 // Processor specific
3291 if (SectionIndex
>= ELF::SHN_LOPROC
&& SectionIndex
<= ELF::SHN_HIPROC
)
3292 return std::string("PRC[0x") +
3293 to_string(format_hex_no_prefix(SectionIndex
, 4)) + "]";
3295 if (SectionIndex
>= ELF::SHN_LOOS
&& SectionIndex
<= ELF::SHN_HIOS
)
3296 return std::string("OS[0x") +
3297 to_string(format_hex_no_prefix(SectionIndex
, 4)) + "]";
3298 // Architecture reserved:
3299 if (SectionIndex
>= ELF::SHN_LORESERVE
&&
3300 SectionIndex
<= ELF::SHN_HIRESERVE
)
3301 return std::string("RSV[0x") +
3302 to_string(format_hex_no_prefix(SectionIndex
, 4)) + "]";
3303 // A normal section with an index
3304 return to_string(format_decimal(SectionIndex
, 3));
3308 template <class ELFT
>
3309 void GNUStyle
<ELFT
>::printSymbol(const ELFO
*Obj
, const Elf_Sym
*Symbol
,
3310 const Elf_Sym
*FirstSym
, StringRef StrTable
,
3311 bool IsDynamic
, bool NonVisibilityBitsUsed
) {
3313 static bool Dynamic
= true;
3315 // If this function was called with a different value from IsDynamic
3316 // from last call, happens when we move from dynamic to static symbol
3317 // table, "Num" field should be reset.
3318 if (!Dynamic
!= !IsDynamic
) {
3323 unsigned Bias
= ELFT::Is64Bits
? 8 : 0;
3324 Field Fields
[8] = {0, 8, 17 + Bias
, 23 + Bias
,
3325 31 + Bias
, 38 + Bias
, 48 + Bias
, 51 + Bias
};
3326 Fields
[0].Str
= to_string(format_decimal(Idx
++, 6)) + ":";
3327 Fields
[1].Str
= to_string(
3328 format_hex_no_prefix(Symbol
->st_value
, ELFT::Is64Bits
? 16 : 8));
3329 Fields
[2].Str
= to_string(format_decimal(Symbol
->st_size
, 5));
3331 unsigned char SymbolType
= Symbol
->getType();
3332 if (Obj
->getHeader()->e_machine
== ELF::EM_AMDGPU
&&
3333 SymbolType
>= ELF::STT_LOOS
&& SymbolType
< ELF::STT_HIOS
)
3334 Fields
[3].Str
= printEnum(SymbolType
, makeArrayRef(AMDGPUSymbolTypes
));
3336 Fields
[3].Str
= printEnum(SymbolType
, makeArrayRef(ElfSymbolTypes
));
3339 printEnum(Symbol
->getBinding(), makeArrayRef(ElfSymbolBindings
));
3341 printEnum(Symbol
->getVisibility(), makeArrayRef(ElfSymbolVisibilities
));
3342 if (Symbol
->st_other
& ~0x3)
3344 " [<other: " + to_string(format_hex(Symbol
->st_other
, 2)) + ">]";
3346 Fields
[6].Column
+= NonVisibilityBitsUsed
? 13 : 0;
3347 Fields
[6].Str
= getSymbolSectionNdx(Obj
, Symbol
, FirstSym
);
3350 this->dumper()->getFullSymbolName(Symbol
, StrTable
, IsDynamic
);
3351 for (auto &Entry
: Fields
)
3356 template <class ELFT
>
3357 void GNUStyle
<ELFT
>::printHashedSymbol(const ELFO
*Obj
, const Elf_Sym
*FirstSym
,
3358 uint32_t Sym
, StringRef StrTable
,
3360 unsigned Bias
= ELFT::Is64Bits
? 8 : 0;
3361 Field Fields
[9] = {0, 6, 11, 20 + Bias
, 25 + Bias
,
3362 34 + Bias
, 41 + Bias
, 49 + Bias
, 53 + Bias
};
3363 Fields
[0].Str
= to_string(format_decimal(Sym
, 5));
3364 Fields
[1].Str
= to_string(format_decimal(Bucket
, 3)) + ":";
3366 const auto Symbol
= FirstSym
+ Sym
;
3367 Fields
[2].Str
= to_string(
3368 format_hex_no_prefix(Symbol
->st_value
, ELFT::Is64Bits
? 16 : 8));
3369 Fields
[3].Str
= to_string(format_decimal(Symbol
->st_size
, 5));
3371 unsigned char SymbolType
= Symbol
->getType();
3372 if (Obj
->getHeader()->e_machine
== ELF::EM_AMDGPU
&&
3373 SymbolType
>= ELF::STT_LOOS
&& SymbolType
< ELF::STT_HIOS
)
3374 Fields
[4].Str
= printEnum(SymbolType
, makeArrayRef(AMDGPUSymbolTypes
));
3376 Fields
[4].Str
= printEnum(SymbolType
, makeArrayRef(ElfSymbolTypes
));
3379 printEnum(Symbol
->getBinding(), makeArrayRef(ElfSymbolBindings
));
3381 printEnum(Symbol
->getVisibility(), makeArrayRef(ElfSymbolVisibilities
));
3382 Fields
[7].Str
= getSymbolSectionNdx(Obj
, Symbol
, FirstSym
);
3383 Fields
[8].Str
= this->dumper()->getFullSymbolName(Symbol
, StrTable
, true);
3385 for (auto &Entry
: Fields
)
3390 template <class ELFT
>
3391 void GNUStyle
<ELFT
>::printSymbols(const ELFO
*Obj
, bool PrintSymbols
,
3392 bool PrintDynamicSymbols
) {
3393 if (!PrintSymbols
&& !PrintDynamicSymbols
)
3395 // GNU readelf prints both the .dynsym and .symtab with --symbols.
3396 this->dumper()->printSymbolsHelper(true);
3398 this->dumper()->printSymbolsHelper(false);
3401 template <class ELFT
> void GNUStyle
<ELFT
>::printHashSymbols(const ELFO
*Obj
) {
3402 if (this->dumper()->getDynamicStringTable().empty())
3404 auto StringTable
= this->dumper()->getDynamicStringTable();
3405 auto DynSyms
= this->dumper()->dynamic_symbols();
3407 // Try printing .hash
3408 if (auto SysVHash
= this->dumper()->getHashTable()) {
3409 OS
<< "\n Symbol table of .hash for image:\n";
3411 OS
<< " Num Buc: Value Size Type Bind Vis Ndx Name";
3413 OS
<< " Num Buc: Value Size Type Bind Vis Ndx Name";
3416 auto Buckets
= SysVHash
->buckets();
3417 auto Chains
= SysVHash
->chains();
3418 for (uint32_t Buc
= 0; Buc
< SysVHash
->nbucket
; Buc
++) {
3419 if (Buckets
[Buc
] == ELF::STN_UNDEF
)
3421 std::vector
<bool> Visited(SysVHash
->nchain
);
3422 for (uint32_t Ch
= Buckets
[Buc
]; Ch
< SysVHash
->nchain
; Ch
= Chains
[Ch
]) {
3423 if (Ch
== ELF::STN_UNDEF
)
3428 createError(".hash section is invalid: bucket " + Twine(Ch
) +
3429 ": a cycle was detected in the linked chain"),
3434 printHashedSymbol(Obj
, &DynSyms
[0], Ch
, StringTable
, Buc
);
3440 // Try printing .gnu.hash
3441 if (auto GnuHash
= this->dumper()->getGnuHashTable()) {
3442 OS
<< "\n Symbol table of .gnu.hash for image:\n";
3444 OS
<< " Num Buc: Value Size Type Bind Vis Ndx Name";
3446 OS
<< " Num Buc: Value Size Type Bind Vis Ndx Name";
3448 auto Buckets
= GnuHash
->buckets();
3449 for (uint32_t Buc
= 0; Buc
< GnuHash
->nbuckets
; Buc
++) {
3450 if (Buckets
[Buc
] == ELF::STN_UNDEF
)
3452 uint32_t Index
= Buckets
[Buc
];
3453 uint32_t GnuHashable
= Index
- GnuHash
->symndx
;
3454 // Print whole chain
3456 printHashedSymbol(Obj
, &DynSyms
[0], Index
++, StringTable
, Buc
);
3457 // Chain ends at symbol with stopper bit
3458 if ((GnuHash
->values(DynSyms
.size())[GnuHashable
++] & 1) == 1)
3465 static inline std::string
printPhdrFlags(unsigned Flag
) {
3467 Str
= (Flag
& PF_R
) ? "R" : " ";
3468 Str
+= (Flag
& PF_W
) ? "W" : " ";
3469 Str
+= (Flag
& PF_X
) ? "E" : " ";
3473 // SHF_TLS sections are only in PT_TLS, PT_LOAD or PT_GNU_RELRO
3474 // PT_TLS must only have SHF_TLS sections
3475 template <class ELFT
>
3476 bool GNUStyle
<ELFT
>::checkTLSSections(const Elf_Phdr
&Phdr
,
3477 const Elf_Shdr
&Sec
) {
3478 return (((Sec
.sh_flags
& ELF::SHF_TLS
) &&
3479 ((Phdr
.p_type
== ELF::PT_TLS
) || (Phdr
.p_type
== ELF::PT_LOAD
) ||
3480 (Phdr
.p_type
== ELF::PT_GNU_RELRO
))) ||
3481 (!(Sec
.sh_flags
& ELF::SHF_TLS
) && Phdr
.p_type
!= ELF::PT_TLS
));
3484 // Non-SHT_NOBITS must have its offset inside the segment
3485 // Only non-zero section can be at end of segment
3486 template <class ELFT
>
3487 bool GNUStyle
<ELFT
>::checkoffsets(const Elf_Phdr
&Phdr
, const Elf_Shdr
&Sec
) {
3488 if (Sec
.sh_type
== ELF::SHT_NOBITS
)
3491 (Sec
.sh_type
== ELF::SHT_NOBITS
) && ((Sec
.sh_flags
& ELF::SHF_TLS
) != 0);
3492 // .tbss is special, it only has memory in PT_TLS and has NOBITS properties
3494 (IsSpecial
&& Phdr
.p_type
!= ELF::PT_TLS
) ? 0 : Sec
.sh_size
;
3495 if (Sec
.sh_offset
>= Phdr
.p_offset
)
3496 return ((Sec
.sh_offset
+ SectionSize
<= Phdr
.p_filesz
+ Phdr
.p_offset
)
3497 /*only non-zero sized sections at end*/
3498 && (Sec
.sh_offset
+ 1 <= Phdr
.p_offset
+ Phdr
.p_filesz
));
3502 // SHF_ALLOC must have VMA inside segment
3503 // Only non-zero section can be at end of segment
3504 template <class ELFT
>
3505 bool GNUStyle
<ELFT
>::checkVMA(const Elf_Phdr
&Phdr
, const Elf_Shdr
&Sec
) {
3506 if (!(Sec
.sh_flags
& ELF::SHF_ALLOC
))
3509 (Sec
.sh_type
== ELF::SHT_NOBITS
) && ((Sec
.sh_flags
& ELF::SHF_TLS
) != 0);
3510 // .tbss is special, it only has memory in PT_TLS and has NOBITS properties
3512 (IsSpecial
&& Phdr
.p_type
!= ELF::PT_TLS
) ? 0 : Sec
.sh_size
;
3513 if (Sec
.sh_addr
>= Phdr
.p_vaddr
)
3514 return ((Sec
.sh_addr
+ SectionSize
<= Phdr
.p_vaddr
+ Phdr
.p_memsz
) &&
3515 (Sec
.sh_addr
+ 1 <= Phdr
.p_vaddr
+ Phdr
.p_memsz
));
3519 // No section with zero size must be at start or end of PT_DYNAMIC
3520 template <class ELFT
>
3521 bool GNUStyle
<ELFT
>::checkPTDynamic(const Elf_Phdr
&Phdr
, const Elf_Shdr
&Sec
) {
3522 if (Phdr
.p_type
!= ELF::PT_DYNAMIC
|| Sec
.sh_size
!= 0 || Phdr
.p_memsz
== 0)
3524 // Is section within the phdr both based on offset and VMA ?
3525 return ((Sec
.sh_type
== ELF::SHT_NOBITS
) ||
3526 (Sec
.sh_offset
> Phdr
.p_offset
&&
3527 Sec
.sh_offset
< Phdr
.p_offset
+ Phdr
.p_filesz
)) &&
3528 (!(Sec
.sh_flags
& ELF::SHF_ALLOC
) ||
3529 (Sec
.sh_addr
> Phdr
.p_vaddr
&& Sec
.sh_addr
< Phdr
.p_memsz
));
3532 template <class ELFT
>
3533 void GNUStyle
<ELFT
>::printProgramHeaders(
3534 const ELFO
*Obj
, bool PrintProgramHeaders
,
3535 cl::boolOrDefault PrintSectionMapping
) {
3536 if (PrintProgramHeaders
)
3537 printProgramHeaders(Obj
);
3539 // Display the section mapping along with the program headers, unless
3540 // -section-mapping is explicitly set to false.
3541 if (PrintSectionMapping
!= cl::BOU_FALSE
)
3542 printSectionMapping(Obj
);
3545 template <class ELFT
>
3546 void GNUStyle
<ELFT
>::printProgramHeaders(const ELFO
*Obj
) {
3547 unsigned Bias
= ELFT::Is64Bits
? 8 : 0;
3548 const Elf_Ehdr
*Header
= Obj
->getHeader();
3549 Field Fields
[8] = {2, 17, 26, 37 + Bias
,
3550 48 + Bias
, 56 + Bias
, 64 + Bias
, 68 + Bias
};
3551 OS
<< "\nElf file type is "
3552 << printEnum(Header
->e_type
, makeArrayRef(ElfObjectFileType
)) << "\n"
3553 << "Entry point " << format_hex(Header
->e_entry
, 3) << "\n"
3554 << "There are " << Header
->e_phnum
<< " program headers,"
3555 << " starting at offset " << Header
->e_phoff
<< "\n\n"
3556 << "Program Headers:\n";
3558 OS
<< " Type Offset VirtAddr PhysAddr "
3559 << " FileSiz MemSiz Flg Align\n";
3561 OS
<< " Type Offset VirtAddr PhysAddr FileSiz "
3562 << "MemSiz Flg Align\n";
3564 unsigned Width
= ELFT::Is64Bits
? 18 : 10;
3565 unsigned SizeWidth
= ELFT::Is64Bits
? 8 : 7;
3566 for (const auto &Phdr
:
3567 unwrapOrError(this->FileName
, Obj
->program_headers())) {
3568 Fields
[0].Str
= getElfPtType(Header
->e_machine
, Phdr
.p_type
);
3569 Fields
[1].Str
= to_string(format_hex(Phdr
.p_offset
, 8));
3570 Fields
[2].Str
= to_string(format_hex(Phdr
.p_vaddr
, Width
));
3571 Fields
[3].Str
= to_string(format_hex(Phdr
.p_paddr
, Width
));
3572 Fields
[4].Str
= to_string(format_hex(Phdr
.p_filesz
, SizeWidth
));
3573 Fields
[5].Str
= to_string(format_hex(Phdr
.p_memsz
, SizeWidth
));
3574 Fields
[6].Str
= printPhdrFlags(Phdr
.p_flags
);
3575 Fields
[7].Str
= to_string(format_hex(Phdr
.p_align
, 1));
3576 for (auto Field
: Fields
)
3578 if (Phdr
.p_type
== ELF::PT_INTERP
) {
3579 OS
<< "\n [Requesting program interpreter: ";
3580 OS
<< reinterpret_cast<const char *>(Obj
->base()) + Phdr
.p_offset
<< "]";
3586 template <class ELFT
>
3587 void GNUStyle
<ELFT
>::printSectionMapping(const ELFO
*Obj
) {
3588 OS
<< "\n Section to Segment mapping:\n Segment Sections...\n";
3589 DenseSet
<const Elf_Shdr
*> BelongsToSegment
;
3591 for (const Elf_Phdr
&Phdr
:
3592 unwrapOrError(this->FileName
, Obj
->program_headers())) {
3593 std::string Sections
;
3594 OS
<< format(" %2.2d ", Phnum
++);
3595 for (const Elf_Shdr
&Sec
: unwrapOrError(this->FileName
, Obj
->sections())) {
3596 // Check if each section is in a segment and then print mapping.
3597 // readelf additionally makes sure it does not print zero sized sections
3598 // at end of segments and for PT_DYNAMIC both start and end of section
3599 // .tbss must only be shown in PT_TLS section.
3600 bool TbssInNonTLS
= (Sec
.sh_type
== ELF::SHT_NOBITS
) &&
3601 ((Sec
.sh_flags
& ELF::SHF_TLS
) != 0) &&
3602 Phdr
.p_type
!= ELF::PT_TLS
;
3603 if (!TbssInNonTLS
&& checkTLSSections(Phdr
, Sec
) &&
3604 checkoffsets(Phdr
, Sec
) && checkVMA(Phdr
, Sec
) &&
3605 checkPTDynamic(Phdr
, Sec
) && (Sec
.sh_type
!= ELF::SHT_NULL
)) {
3607 unwrapOrError(this->FileName
, Obj
->getSectionName(&Sec
)).str() +
3609 BelongsToSegment
.insert(&Sec
);
3612 OS
<< Sections
<< "\n";
3616 // Display sections that do not belong to a segment.
3617 std::string Sections
;
3618 for (const Elf_Shdr
&Sec
: unwrapOrError(this->FileName
, Obj
->sections())) {
3619 if (BelongsToSegment
.find(&Sec
) == BelongsToSegment
.end())
3621 unwrapOrError(this->FileName
, Obj
->getSectionName(&Sec
)).str() + ' ';
3623 if (!Sections
.empty()) {
3624 OS
<< " None " << Sections
<< '\n';
3630 template <class ELFT
> struct RelSymbol
{
3631 const typename
ELFT::Sym
*Sym
;
3635 template <class ELFT
>
3636 RelSymbol
<ELFT
> getSymbolForReloc(const ELFFile
<ELFT
> *Obj
, StringRef FileName
,
3637 const ELFDumper
<ELFT
> *Dumper
,
3638 const typename
ELFT::Rela
&Reloc
) {
3639 uint32_t SymIndex
= Reloc
.getSymbol(Obj
->isMips64EL());
3640 const typename
ELFT::Sym
*Sym
= Dumper
->dynamic_symbols().begin() + SymIndex
;
3641 Expected
<StringRef
> ErrOrName
= Sym
->getName(Dumper
->getDynamicStringTable());
3645 Name
= maybeDemangle(*ErrOrName
);
3648 createError("unable to get name of the dynamic symbol with index " +
3649 Twine(SymIndex
) + ": " + toString(ErrOrName
.takeError())),
3654 return {Sym
, std::move(Name
)};
3658 template <class ELFT
>
3659 void GNUStyle
<ELFT
>::printDynamicRelocation(const ELFO
*Obj
, Elf_Rela R
,
3661 RelSymbol
<ELFT
> S
= getSymbolForReloc(Obj
, this->FileName
, this->dumper(), R
);
3662 printRelocation(Obj
, S
.Sym
, S
.Name
, R
, IsRela
);
3665 template <class ELFT
> void GNUStyle
<ELFT
>::printDynamic(const ELFO
*Obj
) {
3666 Elf_Dyn_Range Table
= this->dumper()->dynamic_table();
3670 const DynRegionInfo
&DynamicTableRegion
=
3671 this->dumper()->getDynamicTableRegion();
3673 OS
<< "Dynamic section at offset "
3674 << format_hex(reinterpret_cast<const uint8_t *>(DynamicTableRegion
.Addr
) -
3677 << " contains " << Table
.size() << " entries:\n";
3679 bool Is64
= ELFT::Is64Bits
;
3681 OS
<< " Tag Type Name/Value\n";
3683 OS
<< " Tag Type Name/Value\n";
3684 for (auto Entry
: Table
) {
3685 uintX_t Tag
= Entry
.getTag();
3686 std::string TypeString
= std::string("(") +
3687 getTypeString(Obj
->getHeader()->e_machine
, Tag
) +
3689 OS
<< " " << format_hex(Tag
, Is64
? 18 : 10)
3690 << format(" %-20s ", TypeString
.c_str());
3691 this->dumper()->printDynamicEntry(OS
, Tag
, Entry
.getVal());
3696 template <class ELFT
>
3697 void GNUStyle
<ELFT
>::printDynamicRelocations(const ELFO
*Obj
) {
3698 const DynRegionInfo
&DynRelRegion
= this->dumper()->getDynRelRegion();
3699 const DynRegionInfo
&DynRelaRegion
= this->dumper()->getDynRelaRegion();
3700 const DynRegionInfo
&DynRelrRegion
= this->dumper()->getDynRelrRegion();
3701 const DynRegionInfo
&DynPLTRelRegion
= this->dumper()->getDynPLTRelRegion();
3702 if (DynRelaRegion
.Size
> 0) {
3703 OS
<< "\n'RELA' relocation section at offset "
3704 << format_hex(reinterpret_cast<const uint8_t *>(DynRelaRegion
.Addr
) -
3707 << " contains " << DynRelaRegion
.Size
<< " bytes:\n";
3708 printRelocHeader(ELF::SHT_RELA
);
3709 for (const Elf_Rela
&Rela
: this->dumper()->dyn_relas())
3710 printDynamicRelocation(Obj
, Rela
, true);
3712 if (DynRelRegion
.Size
> 0) {
3713 OS
<< "\n'REL' relocation section at offset "
3714 << format_hex(reinterpret_cast<const uint8_t *>(DynRelRegion
.Addr
) -
3717 << " contains " << DynRelRegion
.Size
<< " bytes:\n";
3718 printRelocHeader(ELF::SHT_REL
);
3719 for (const Elf_Rel
&Rel
: this->dumper()->dyn_rels()) {
3721 Rela
.r_offset
= Rel
.r_offset
;
3722 Rela
.r_info
= Rel
.r_info
;
3724 printDynamicRelocation(Obj
, Rela
, false);
3727 if (DynRelrRegion
.Size
> 0) {
3728 OS
<< "\n'RELR' relocation section at offset "
3729 << format_hex(reinterpret_cast<const uint8_t *>(DynRelrRegion
.Addr
) -
3732 << " contains " << DynRelrRegion
.Size
<< " bytes:\n";
3733 printRelocHeader(ELF::SHT_REL
);
3734 Elf_Relr_Range Relrs
= this->dumper()->dyn_relrs();
3735 std::vector
<Elf_Rela
> RelrRelas
=
3736 unwrapOrError(this->FileName
, Obj
->decode_relrs(Relrs
));
3737 for (const Elf_Rela
&Rela
: RelrRelas
) {
3738 printDynamicRelocation(Obj
, Rela
, false);
3741 if (DynPLTRelRegion
.Size
) {
3742 OS
<< "\n'PLT' relocation section at offset "
3743 << format_hex(reinterpret_cast<const uint8_t *>(DynPLTRelRegion
.Addr
) -
3746 << " contains " << DynPLTRelRegion
.Size
<< " bytes:\n";
3748 if (DynPLTRelRegion
.EntSize
== sizeof(Elf_Rela
)) {
3749 printRelocHeader(ELF::SHT_RELA
);
3750 for (const Elf_Rela
&Rela
: DynPLTRelRegion
.getAsArrayRef
<Elf_Rela
>())
3751 printDynamicRelocation(Obj
, Rela
, true);
3753 printRelocHeader(ELF::SHT_REL
);
3754 for (const Elf_Rel
&Rel
: DynPLTRelRegion
.getAsArrayRef
<Elf_Rel
>()) {
3756 Rela
.r_offset
= Rel
.r_offset
;
3757 Rela
.r_info
= Rel
.r_info
;
3759 printDynamicRelocation(Obj
, Rela
, false);
3764 template <class ELFT
>
3765 static void printGNUVersionSectionProlog(formatted_raw_ostream
&OS
,
3766 const Twine
&Name
, unsigned EntriesNum
,
3767 const ELFFile
<ELFT
> *Obj
,
3768 const typename
ELFT::Shdr
*Sec
,
3769 StringRef FileName
) {
3770 StringRef SecName
= unwrapOrError(FileName
, Obj
->getSectionName(Sec
));
3771 OS
<< Name
<< " section '" << SecName
<< "' "
3772 << "contains " << EntriesNum
<< " entries:\n";
3774 const typename
ELFT::Shdr
*SymTab
=
3775 unwrapOrError(FileName
, Obj
->getSection(Sec
->sh_link
));
3776 StringRef SymTabName
= unwrapOrError(FileName
, Obj
->getSectionName(SymTab
));
3777 OS
<< " Addr: " << format_hex_no_prefix(Sec
->sh_addr
, 16)
3778 << " Offset: " << format_hex(Sec
->sh_offset
, 8)
3779 << " Link: " << Sec
->sh_link
<< " (" << SymTabName
<< ")\n";
3782 template <class ELFT
>
3783 void GNUStyle
<ELFT
>::printVersionSymbolSection(const ELFFile
<ELFT
> *Obj
,
3784 const Elf_Shdr
*Sec
) {
3788 unsigned Entries
= Sec
->sh_size
/ sizeof(Elf_Versym
);
3789 printGNUVersionSectionProlog(OS
, "Version symbols", Entries
, Obj
, Sec
,
3792 const uint8_t *VersymBuf
=
3793 reinterpret_cast<const uint8_t *>(Obj
->base() + Sec
->sh_offset
);
3794 const ELFDumper
<ELFT
> *Dumper
= this->dumper();
3795 StringRef StrTable
= Dumper
->getDynamicStringTable();
3797 // readelf prints 4 entries per line.
3798 for (uint64_t VersymRow
= 0; VersymRow
< Entries
; VersymRow
+= 4) {
3799 OS
<< " " << format_hex_no_prefix(VersymRow
, 3) << ":";
3801 for (uint64_t VersymIndex
= 0;
3802 (VersymIndex
< 4) && (VersymIndex
+ VersymRow
) < Entries
;
3804 const Elf_Versym
*Versym
=
3805 reinterpret_cast<const Elf_Versym
*>(VersymBuf
);
3806 switch (Versym
->vs_index
) {
3808 OS
<< " 0 (*local*) ";
3811 OS
<< " 1 (*global*) ";
3814 OS
<< format("%4x%c", Versym
->vs_index
& VERSYM_VERSION
,
3815 Versym
->vs_index
& VERSYM_HIDDEN
? 'h' : ' ');
3817 bool IsDefault
= true;
3818 std::string VersionName
= Dumper
->getSymbolVersionByIndex(
3819 StrTable
, Versym
->vs_index
, IsDefault
);
3821 if (!VersionName
.empty())
3822 VersionName
= "(" + VersionName
+ ")";
3824 VersionName
= "(*invalid*)";
3825 OS
<< left_justify(VersionName
, 13);
3827 VersymBuf
+= sizeof(Elf_Versym
);
3834 static std::string
versionFlagToString(unsigned Flags
) {
3839 auto AddFlag
= [&Ret
, &Flags
](unsigned Flag
, StringRef Name
) {
3840 if (!(Flags
& Flag
))
3848 AddFlag(VER_FLG_BASE
, "BASE");
3849 AddFlag(VER_FLG_WEAK
, "WEAK");
3850 AddFlag(VER_FLG_INFO
, "INFO");
3851 AddFlag(~0, "<unknown>");
3855 template <class ELFT
>
3856 void GNUStyle
<ELFT
>::printVersionDefinitionSection(const ELFFile
<ELFT
> *Obj
,
3857 const Elf_Shdr
*Sec
) {
3861 unsigned VerDefsNum
= Sec
->sh_info
;
3862 printGNUVersionSectionProlog(OS
, "Version definition", VerDefsNum
, Obj
, Sec
,
3865 const Elf_Shdr
*StrTabSec
=
3866 unwrapOrError(this->FileName
, Obj
->getSection(Sec
->sh_link
));
3867 StringRef
StringTable(
3868 reinterpret_cast<const char *>(Obj
->base() + StrTabSec
->sh_offset
),
3869 (size_t)StrTabSec
->sh_size
);
3871 const uint8_t *VerdefBuf
=
3872 unwrapOrError(this->FileName
, Obj
->getSectionContents(Sec
)).data();
3873 const uint8_t *Begin
= VerdefBuf
;
3875 while (VerDefsNum
--) {
3876 const Elf_Verdef
*Verdef
= reinterpret_cast<const Elf_Verdef
*>(VerdefBuf
);
3877 OS
<< format(" 0x%04x: Rev: %u Flags: %s Index: %u Cnt: %u",
3878 VerdefBuf
- Begin
, (unsigned)Verdef
->vd_version
,
3879 versionFlagToString(Verdef
->vd_flags
).c_str(),
3880 (unsigned)Verdef
->vd_ndx
, (unsigned)Verdef
->vd_cnt
);
3882 const uint8_t *VerdauxBuf
= VerdefBuf
+ Verdef
->vd_aux
;
3883 const Elf_Verdaux
*Verdaux
=
3884 reinterpret_cast<const Elf_Verdaux
*>(VerdauxBuf
);
3885 OS
<< format(" Name: %s\n",
3886 StringTable
.drop_front(Verdaux
->vda_name
).data());
3888 for (unsigned I
= 1; I
< Verdef
->vd_cnt
; ++I
) {
3889 VerdauxBuf
+= Verdaux
->vda_next
;
3890 Verdaux
= reinterpret_cast<const Elf_Verdaux
*>(VerdauxBuf
);
3891 OS
<< format(" 0x%04x: Parent %u: %s\n", VerdauxBuf
- Begin
, I
,
3892 StringTable
.drop_front(Verdaux
->vda_name
).data());
3895 VerdefBuf
+= Verdef
->vd_next
;
3900 template <class ELFT
>
3901 void GNUStyle
<ELFT
>::printVersionDependencySection(const ELFFile
<ELFT
> *Obj
,
3902 const Elf_Shdr
*Sec
) {
3906 unsigned VerneedNum
= Sec
->sh_info
;
3907 printGNUVersionSectionProlog(OS
, "Version needs", VerneedNum
, Obj
, Sec
,
3910 ArrayRef
<uint8_t> SecData
=
3911 unwrapOrError(this->FileName
, Obj
->getSectionContents(Sec
));
3913 const Elf_Shdr
*StrTabSec
=
3914 unwrapOrError(this->FileName
, Obj
->getSection(Sec
->sh_link
));
3915 StringRef StringTable
= {
3916 reinterpret_cast<const char *>(Obj
->base() + StrTabSec
->sh_offset
),
3917 (size_t)StrTabSec
->sh_size
};
3919 const uint8_t *VerneedBuf
= SecData
.data();
3920 for (unsigned I
= 0; I
< VerneedNum
; ++I
) {
3921 const Elf_Verneed
*Verneed
=
3922 reinterpret_cast<const Elf_Verneed
*>(VerneedBuf
);
3924 OS
<< format(" 0x%04x: Version: %u File: %s Cnt: %u\n",
3925 reinterpret_cast<const uint8_t *>(Verneed
) - SecData
.begin(),
3926 (unsigned)Verneed
->vn_version
,
3927 StringTable
.drop_front(Verneed
->vn_file
).data(),
3928 (unsigned)Verneed
->vn_cnt
);
3930 const uint8_t *VernauxBuf
= VerneedBuf
+ Verneed
->vn_aux
;
3931 for (unsigned J
= 0; J
< Verneed
->vn_cnt
; ++J
) {
3932 const Elf_Vernaux
*Vernaux
=
3933 reinterpret_cast<const Elf_Vernaux
*>(VernauxBuf
);
3935 OS
<< format(" 0x%04x: Name: %s Flags: %s Version: %u\n",
3936 reinterpret_cast<const uint8_t *>(Vernaux
) - SecData
.begin(),
3937 StringTable
.drop_front(Vernaux
->vna_name
).data(),
3938 versionFlagToString(Vernaux
->vna_flags
).c_str(),
3939 (unsigned)Vernaux
->vna_other
);
3940 VernauxBuf
+= Vernaux
->vna_next
;
3942 VerneedBuf
+= Verneed
->vn_next
;
3947 // Hash histogram shows statistics of how efficient the hash was for the
3948 // dynamic symbol table. The table shows number of hash buckets for different
3949 // lengths of chains as absolute number and percentage of the total buckets.
3950 // Additionally cumulative coverage of symbols for each set of buckets.
3951 template <class ELFT
>
3952 void GNUStyle
<ELFT
>::printHashHistogram(const ELFFile
<ELFT
> *Obj
) {
3953 // Print histogram for .hash section
3954 if (const Elf_Hash
*HashTable
= this->dumper()->getHashTable()) {
3955 size_t NBucket
= HashTable
->nbucket
;
3956 size_t NChain
= HashTable
->nchain
;
3957 ArrayRef
<Elf_Word
> Buckets
= HashTable
->buckets();
3958 ArrayRef
<Elf_Word
> Chains
= HashTable
->chains();
3959 size_t TotalSyms
= 0;
3960 // If hash table is correct, we have at least chains with 0 length
3961 size_t MaxChain
= 1;
3962 size_t CumulativeNonZero
= 0;
3964 if (NChain
== 0 || NBucket
== 0)
3967 std::vector
<size_t> ChainLen(NBucket
, 0);
3968 // Go over all buckets and and note chain lengths of each bucket (total
3969 // unique chain lengths).
3970 for (size_t B
= 0; B
< NBucket
; B
++) {
3971 std::vector
<bool> Visited(NChain
);
3972 for (size_t C
= Buckets
[B
]; C
< NChain
; C
= Chains
[C
]) {
3973 if (C
== ELF::STN_UNDEF
)
3977 createError(".hash section is invalid: bucket " + Twine(C
) +
3978 ": a cycle was detected in the linked chain"),
3983 if (MaxChain
<= ++ChainLen
[B
])
3986 TotalSyms
+= ChainLen
[B
];
3992 std::vector
<size_t> Count(MaxChain
, 0) ;
3993 // Count how long is the chain for each bucket
3994 for (size_t B
= 0; B
< NBucket
; B
++)
3995 ++Count
[ChainLen
[B
]];
3996 // Print Number of buckets with each chain lengths and their cumulative
3997 // coverage of the symbols
3998 OS
<< "Histogram for bucket list length (total of " << NBucket
4000 << " Length Number % of total Coverage\n";
4001 for (size_t I
= 0; I
< MaxChain
; I
++) {
4002 CumulativeNonZero
+= Count
[I
] * I
;
4003 OS
<< format("%7lu %-10lu (%5.1f%%) %5.1f%%\n", I
, Count
[I
],
4004 (Count
[I
] * 100.0) / NBucket
,
4005 (CumulativeNonZero
* 100.0) / TotalSyms
);
4009 // Print histogram for .gnu.hash section
4010 if (const Elf_GnuHash
*GnuHashTable
= this->dumper()->getGnuHashTable()) {
4011 size_t NBucket
= GnuHashTable
->nbuckets
;
4012 ArrayRef
<Elf_Word
> Buckets
= GnuHashTable
->buckets();
4013 unsigned NumSyms
= this->dumper()->dynamic_symbols().size();
4016 ArrayRef
<Elf_Word
> Chains
= GnuHashTable
->values(NumSyms
);
4017 size_t Symndx
= GnuHashTable
->symndx
;
4018 size_t TotalSyms
= 0;
4019 size_t MaxChain
= 1;
4020 size_t CumulativeNonZero
= 0;
4022 if (Chains
.empty() || NBucket
== 0)
4025 std::vector
<size_t> ChainLen(NBucket
, 0);
4027 for (size_t B
= 0; B
< NBucket
; B
++) {
4031 for (size_t C
= Buckets
[B
] - Symndx
;
4032 C
< Chains
.size() && (Chains
[C
] & 1) == 0; C
++)
4033 if (MaxChain
< ++Len
)
4043 std::vector
<size_t> Count(MaxChain
, 0) ;
4044 for (size_t B
= 0; B
< NBucket
; B
++)
4045 ++Count
[ChainLen
[B
]];
4046 // Print Number of buckets with each chain lengths and their cumulative
4047 // coverage of the symbols
4048 OS
<< "Histogram for `.gnu.hash' bucket list length (total of " << NBucket
4050 << " Length Number % of total Coverage\n";
4051 for (size_t I
= 0; I
<MaxChain
; I
++) {
4052 CumulativeNonZero
+= Count
[I
] * I
;
4053 OS
<< format("%7lu %-10lu (%5.1f%%) %5.1f%%\n", I
, Count
[I
],
4054 (Count
[I
] * 100.0) / NBucket
,
4055 (CumulativeNonZero
* 100.0) / TotalSyms
);
4060 template <class ELFT
>
4061 void GNUStyle
<ELFT
>::printCGProfile(const ELFFile
<ELFT
> *Obj
) {
4062 OS
<< "GNUStyle::printCGProfile not implemented\n";
4065 template <class ELFT
>
4066 void GNUStyle
<ELFT
>::printAddrsig(const ELFFile
<ELFT
> *Obj
) {
4067 reportError(createError("--addrsig: not implemented"), this->FileName
);
4070 static StringRef
getGenericNoteTypeName(const uint32_t NT
) {
4071 static const struct {
4075 {ELF::NT_VERSION
, "NT_VERSION (version)"},
4076 {ELF::NT_ARCH
, "NT_ARCH (architecture)"},
4077 {ELF::NT_GNU_BUILD_ATTRIBUTE_OPEN
, "OPEN"},
4078 {ELF::NT_GNU_BUILD_ATTRIBUTE_FUNC
, "func"},
4081 for (const auto &Note
: Notes
)
4088 static StringRef
getCoreNoteTypeName(const uint32_t NT
) {
4089 static const struct {
4093 {ELF::NT_PRSTATUS
, "NT_PRSTATUS (prstatus structure)"},
4094 {ELF::NT_FPREGSET
, "NT_FPREGSET (floating point registers)"},
4095 {ELF::NT_PRPSINFO
, "NT_PRPSINFO (prpsinfo structure)"},
4096 {ELF::NT_TASKSTRUCT
, "NT_TASKSTRUCT (task structure)"},
4097 {ELF::NT_AUXV
, "NT_AUXV (auxiliary vector)"},
4098 {ELF::NT_PSTATUS
, "NT_PSTATUS (pstatus structure)"},
4099 {ELF::NT_FPREGS
, "NT_FPREGS (floating point registers)"},
4100 {ELF::NT_PSINFO
, "NT_PSINFO (psinfo structure)"},
4101 {ELF::NT_LWPSTATUS
, "NT_LWPSTATUS (lwpstatus_t structure)"},
4102 {ELF::NT_LWPSINFO
, "NT_LWPSINFO (lwpsinfo_t structure)"},
4103 {ELF::NT_WIN32PSTATUS
, "NT_WIN32PSTATUS (win32_pstatus structure)"},
4105 {ELF::NT_PPC_VMX
, "NT_PPC_VMX (ppc Altivec registers)"},
4106 {ELF::NT_PPC_VSX
, "NT_PPC_VSX (ppc VSX registers)"},
4107 {ELF::NT_PPC_TAR
, "NT_PPC_TAR (ppc TAR register)"},
4108 {ELF::NT_PPC_PPR
, "NT_PPC_PPR (ppc PPR register)"},
4109 {ELF::NT_PPC_DSCR
, "NT_PPC_DSCR (ppc DSCR register)"},
4110 {ELF::NT_PPC_EBB
, "NT_PPC_EBB (ppc EBB registers)"},
4111 {ELF::NT_PPC_PMU
, "NT_PPC_PMU (ppc PMU registers)"},
4112 {ELF::NT_PPC_TM_CGPR
, "NT_PPC_TM_CGPR (ppc checkpointed GPR registers)"},
4113 {ELF::NT_PPC_TM_CFPR
,
4114 "NT_PPC_TM_CFPR (ppc checkpointed floating point registers)"},
4115 {ELF::NT_PPC_TM_CVMX
,
4116 "NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)"},
4117 {ELF::NT_PPC_TM_CVSX
, "NT_PPC_TM_CVSX (ppc checkpointed VSX registers)"},
4118 {ELF::NT_PPC_TM_SPR
, "NT_PPC_TM_SPR (ppc TM special purpose registers)"},
4119 {ELF::NT_PPC_TM_CTAR
, "NT_PPC_TM_CTAR (ppc checkpointed TAR register)"},
4120 {ELF::NT_PPC_TM_CPPR
, "NT_PPC_TM_CPPR (ppc checkpointed PPR register)"},
4121 {ELF::NT_PPC_TM_CDSCR
,
4122 "NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)"},
4124 {ELF::NT_386_TLS
, "NT_386_TLS (x86 TLS information)"},
4125 {ELF::NT_386_IOPERM
, "NT_386_IOPERM (x86 I/O permissions)"},
4126 {ELF::NT_X86_XSTATE
, "NT_X86_XSTATE (x86 XSAVE extended state)"},
4128 {ELF::NT_S390_HIGH_GPRS
,
4129 "NT_S390_HIGH_GPRS (s390 upper register halves)"},
4130 {ELF::NT_S390_TIMER
, "NT_S390_TIMER (s390 timer register)"},
4131 {ELF::NT_S390_TODCMP
, "NT_S390_TODCMP (s390 TOD comparator register)"},
4132 {ELF::NT_S390_TODPREG
,
4133 "NT_S390_TODPREG (s390 TOD programmable register)"},
4134 {ELF::NT_S390_CTRS
, "NT_S390_CTRS (s390 control registers)"},
4135 {ELF::NT_S390_PREFIX
, "NT_S390_PREFIX (s390 prefix register)"},
4136 {ELF::NT_S390_LAST_BREAK
,
4137 "NT_S390_LAST_BREAK (s390 last breaking event address)"},
4138 {ELF::NT_S390_SYSTEM_CALL
,
4139 "NT_S390_SYSTEM_CALL (s390 system call restart data)"},
4140 {ELF::NT_S390_TDB
, "NT_S390_TDB (s390 transaction diagnostic block)"},
4141 {ELF::NT_S390_VXRS_LOW
,
4142 "NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)"},
4143 {ELF::NT_S390_VXRS_HIGH
,
4144 "NT_S390_VXRS_HIGH (s390 vector registers 16-31)"},
4145 {ELF::NT_S390_GS_CB
, "NT_S390_GS_CB (s390 guarded-storage registers)"},
4146 {ELF::NT_S390_GS_BC
,
4147 "NT_S390_GS_BC (s390 guarded-storage broadcast control)"},
4149 {ELF::NT_ARM_VFP
, "NT_ARM_VFP (arm VFP registers)"},
4150 {ELF::NT_ARM_TLS
, "NT_ARM_TLS (AArch TLS registers)"},
4151 {ELF::NT_ARM_HW_BREAK
,
4152 "NT_ARM_HW_BREAK (AArch hardware breakpoint registers)"},
4153 {ELF::NT_ARM_HW_WATCH
,
4154 "NT_ARM_HW_WATCH (AArch hardware watchpoint registers)"},
4156 {ELF::NT_FILE
, "NT_FILE (mapped files)"},
4157 {ELF::NT_PRXFPREG
, "NT_PRXFPREG (user_xfpregs structure)"},
4158 {ELF::NT_SIGINFO
, "NT_SIGINFO (siginfo_t data)"},
4161 for (const auto &Note
: Notes
)
4168 static std::string
getGNUNoteTypeName(const uint32_t NT
) {
4169 static const struct {
4173 {ELF::NT_GNU_ABI_TAG
, "NT_GNU_ABI_TAG (ABI version tag)"},
4174 {ELF::NT_GNU_HWCAP
, "NT_GNU_HWCAP (DSO-supplied software HWCAP info)"},
4175 {ELF::NT_GNU_BUILD_ID
, "NT_GNU_BUILD_ID (unique build ID bitstring)"},
4176 {ELF::NT_GNU_GOLD_VERSION
, "NT_GNU_GOLD_VERSION (gold version)"},
4177 {ELF::NT_GNU_PROPERTY_TYPE_0
, "NT_GNU_PROPERTY_TYPE_0 (property note)"},
4180 for (const auto &Note
: Notes
)
4182 return std::string(Note
.Name
);
4185 raw_string_ostream
OS(string
);
4186 OS
<< format("Unknown note type (0x%08x)", NT
);
4190 static std::string
getFreeBSDNoteTypeName(const uint32_t NT
) {
4191 static const struct {
4195 {ELF::NT_FREEBSD_THRMISC
, "NT_THRMISC (thrmisc structure)"},
4196 {ELF::NT_FREEBSD_PROCSTAT_PROC
, "NT_PROCSTAT_PROC (proc data)"},
4197 {ELF::NT_FREEBSD_PROCSTAT_FILES
, "NT_PROCSTAT_FILES (files data)"},
4198 {ELF::NT_FREEBSD_PROCSTAT_VMMAP
, "NT_PROCSTAT_VMMAP (vmmap data)"},
4199 {ELF::NT_FREEBSD_PROCSTAT_GROUPS
, "NT_PROCSTAT_GROUPS (groups data)"},
4200 {ELF::NT_FREEBSD_PROCSTAT_UMASK
, "NT_PROCSTAT_UMASK (umask data)"},
4201 {ELF::NT_FREEBSD_PROCSTAT_RLIMIT
, "NT_PROCSTAT_RLIMIT (rlimit data)"},
4202 {ELF::NT_FREEBSD_PROCSTAT_OSREL
, "NT_PROCSTAT_OSREL (osreldate data)"},
4203 {ELF::NT_FREEBSD_PROCSTAT_PSSTRINGS
,
4204 "NT_PROCSTAT_PSSTRINGS (ps_strings data)"},
4205 {ELF::NT_FREEBSD_PROCSTAT_AUXV
, "NT_PROCSTAT_AUXV (auxv data)"},
4208 for (const auto &Note
: Notes
)
4210 return std::string(Note
.Name
);
4213 raw_string_ostream
OS(string
);
4214 OS
<< format("Unknown note type (0x%08x)", NT
);
4218 static std::string
getAMDNoteTypeName(const uint32_t NT
) {
4219 static const struct {
4222 } Notes
[] = {{ELF::NT_AMD_AMDGPU_HSA_METADATA
,
4223 "NT_AMD_AMDGPU_HSA_METADATA (HSA Metadata)"},
4224 {ELF::NT_AMD_AMDGPU_ISA
, "NT_AMD_AMDGPU_ISA (ISA Version)"},
4225 {ELF::NT_AMD_AMDGPU_PAL_METADATA
,
4226 "NT_AMD_AMDGPU_PAL_METADATA (PAL Metadata)"}};
4228 for (const auto &Note
: Notes
)
4230 return std::string(Note
.Name
);
4233 raw_string_ostream
OS(string
);
4234 OS
<< format("Unknown note type (0x%08x)", NT
);
4238 static std::string
getAMDGPUNoteTypeName(const uint32_t NT
) {
4239 if (NT
== ELF::NT_AMDGPU_METADATA
)
4240 return std::string("NT_AMDGPU_METADATA (AMDGPU Metadata)");
4243 raw_string_ostream
OS(string
);
4244 OS
<< format("Unknown note type (0x%08x)", NT
);
4248 template <typename ELFT
>
4249 static std::string
getGNUProperty(uint32_t Type
, uint32_t DataSize
,
4250 ArrayRef
<uint8_t> Data
) {
4252 raw_string_ostream
OS(str
);
4254 auto DumpBit
= [&](uint32_t Flag
, StringRef Name
) {
4255 if (PrData
& Flag
) {
4265 OS
<< format("<application-specific type 0x%x>", Type
);
4267 case GNU_PROPERTY_STACK_SIZE
: {
4268 OS
<< "stack size: ";
4269 if (DataSize
== sizeof(typename
ELFT::uint
))
4270 OS
<< formatv("{0:x}",
4271 (uint64_t)(*(const typename
ELFT::Addr
*)Data
.data()));
4273 OS
<< format("<corrupt length: 0x%x>", DataSize
);
4276 case GNU_PROPERTY_NO_COPY_ON_PROTECTED
:
4277 OS
<< "no copy on protected";
4279 OS
<< format(" <corrupt length: 0x%x>", DataSize
);
4281 case GNU_PROPERTY_AARCH64_FEATURE_1_AND
:
4282 case GNU_PROPERTY_X86_FEATURE_1_AND
:
4283 OS
<< ((Type
== GNU_PROPERTY_AARCH64_FEATURE_1_AND
) ? "aarch64 feature: "
4285 if (DataSize
!= 4) {
4286 OS
<< format("<corrupt length: 0x%x>", DataSize
);
4289 PrData
= support::endian::read32
<ELFT::TargetEndianness
>(Data
.data());
4294 if (Type
== GNU_PROPERTY_AARCH64_FEATURE_1_AND
) {
4295 DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_BTI
, "BTI");
4296 DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_PAC
, "PAC");
4298 DumpBit(GNU_PROPERTY_X86_FEATURE_1_IBT
, "IBT");
4299 DumpBit(GNU_PROPERTY_X86_FEATURE_1_SHSTK
, "SHSTK");
4302 OS
<< format("<unknown flags: 0x%x>", PrData
);
4304 case GNU_PROPERTY_X86_ISA_1_NEEDED
:
4305 case GNU_PROPERTY_X86_ISA_1_USED
:
4307 << (Type
== GNU_PROPERTY_X86_ISA_1_NEEDED
? "needed: " : "used: ");
4308 if (DataSize
!= 4) {
4309 OS
<< format("<corrupt length: 0x%x>", DataSize
);
4312 PrData
= support::endian::read32
<ELFT::TargetEndianness
>(Data
.data());
4317 DumpBit(GNU_PROPERTY_X86_ISA_1_CMOV
, "CMOV");
4318 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE
, "SSE");
4319 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE2
, "SSE2");
4320 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE3
, "SSE3");
4321 DumpBit(GNU_PROPERTY_X86_ISA_1_SSSE3
, "SSSE3");
4322 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE4_1
, "SSE4_1");
4323 DumpBit(GNU_PROPERTY_X86_ISA_1_SSE4_2
, "SSE4_2");
4324 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX
, "AVX");
4325 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX2
, "AVX2");
4326 DumpBit(GNU_PROPERTY_X86_ISA_1_FMA
, "FMA");
4327 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512F
, "AVX512F");
4328 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512CD
, "AVX512CD");
4329 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512ER
, "AVX512ER");
4330 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512PF
, "AVX512PF");
4331 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512VL
, "AVX512VL");
4332 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512DQ
, "AVX512DQ");
4333 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512BW
, "AVX512BW");
4334 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_4FMAPS
, "AVX512_4FMAPS");
4335 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_4VNNIW
, "AVX512_4VNNIW");
4336 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_BITALG
, "AVX512_BITALG");
4337 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_IFMA
, "AVX512_IFMA");
4338 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_VBMI
, "AVX512_VBMI");
4339 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_VBMI2
, "AVX512_VBMI2");
4340 DumpBit(GNU_PROPERTY_X86_ISA_1_AVX512_VNNI
, "AVX512_VNNI");
4342 OS
<< format("<unknown flags: 0x%x>", PrData
);
4345 case GNU_PROPERTY_X86_FEATURE_2_NEEDED
:
4346 case GNU_PROPERTY_X86_FEATURE_2_USED
:
4347 OS
<< "x86 feature "
4348 << (Type
== GNU_PROPERTY_X86_FEATURE_2_NEEDED
? "needed: " : "used: ");
4349 if (DataSize
!= 4) {
4350 OS
<< format("<corrupt length: 0x%x>", DataSize
);
4353 PrData
= support::endian::read32
<ELFT::TargetEndianness
>(Data
.data());
4358 DumpBit(GNU_PROPERTY_X86_FEATURE_2_X86
, "x86");
4359 DumpBit(GNU_PROPERTY_X86_FEATURE_2_X87
, "x87");
4360 DumpBit(GNU_PROPERTY_X86_FEATURE_2_MMX
, "MMX");
4361 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XMM
, "XMM");
4362 DumpBit(GNU_PROPERTY_X86_FEATURE_2_YMM
, "YMM");
4363 DumpBit(GNU_PROPERTY_X86_FEATURE_2_ZMM
, "ZMM");
4364 DumpBit(GNU_PROPERTY_X86_FEATURE_2_FXSR
, "FXSR");
4365 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVE
, "XSAVE");
4366 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
, "XSAVEOPT");
4367 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVEC
, "XSAVEC");
4369 OS
<< format("<unknown flags: 0x%x>", PrData
);
4374 template <typename ELFT
>
4375 static SmallVector
<std::string
, 4> getGNUPropertyList(ArrayRef
<uint8_t> Arr
) {
4376 using Elf_Word
= typename
ELFT::Word
;
4378 SmallVector
<std::string
, 4> Properties
;
4379 while (Arr
.size() >= 8) {
4380 uint32_t Type
= *reinterpret_cast<const Elf_Word
*>(Arr
.data());
4381 uint32_t DataSize
= *reinterpret_cast<const Elf_Word
*>(Arr
.data() + 4);
4382 Arr
= Arr
.drop_front(8);
4384 // Take padding size into account if present.
4385 uint64_t PaddedSize
= alignTo(DataSize
, sizeof(typename
ELFT::uint
));
4387 raw_string_ostream
OS(str
);
4388 if (Arr
.size() < PaddedSize
) {
4389 OS
<< format("<corrupt type (0x%x) datasz: 0x%x>", Type
, DataSize
);
4390 Properties
.push_back(OS
.str());
4393 Properties
.push_back(
4394 getGNUProperty
<ELFT
>(Type
, DataSize
, Arr
.take_front(PaddedSize
)));
4395 Arr
= Arr
.drop_front(PaddedSize
);
4399 Properties
.push_back("<corrupted GNU_PROPERTY_TYPE_0>");
4410 template <typename ELFT
> static GNUAbiTag
getGNUAbiTag(ArrayRef
<uint8_t> Desc
) {
4411 typedef typename
ELFT::Word Elf_Word
;
4413 ArrayRef
<Elf_Word
> Words(reinterpret_cast<const Elf_Word
*>(Desc
.begin()),
4414 reinterpret_cast<const Elf_Word
*>(Desc
.end()));
4416 if (Words
.size() < 4)
4417 return {"", "", /*IsValid=*/false};
4419 static const char *OSNames
[] = {
4420 "Linux", "Hurd", "Solaris", "FreeBSD", "NetBSD", "Syllable", "NaCl",
4422 StringRef OSName
= "Unknown";
4423 if (Words
[0] < array_lengthof(OSNames
))
4424 OSName
= OSNames
[Words
[0]];
4425 uint32_t Major
= Words
[1], Minor
= Words
[2], Patch
= Words
[3];
4427 raw_string_ostream
ABI(str
);
4428 ABI
<< Major
<< "." << Minor
<< "." << Patch
;
4429 return {OSName
, ABI
.str(), /*IsValid=*/true};
4432 static std::string
getGNUBuildId(ArrayRef
<uint8_t> Desc
) {
4434 raw_string_ostream
OS(str
);
4435 for (const auto &B
: Desc
)
4436 OS
<< format_hex_no_prefix(B
, 2);
4440 static StringRef
getGNUGoldVersion(ArrayRef
<uint8_t> Desc
) {
4441 return StringRef(reinterpret_cast<const char *>(Desc
.data()), Desc
.size());
4444 template <typename ELFT
>
4445 static void printGNUNote(raw_ostream
&OS
, uint32_t NoteType
,
4446 ArrayRef
<uint8_t> Desc
) {
4450 case ELF::NT_GNU_ABI_TAG
: {
4451 const GNUAbiTag
&AbiTag
= getGNUAbiTag
<ELFT
>(Desc
);
4452 if (!AbiTag
.IsValid
)
4453 OS
<< " <corrupt GNU_ABI_TAG>";
4455 OS
<< " OS: " << AbiTag
.OSName
<< ", ABI: " << AbiTag
.ABI
;
4458 case ELF::NT_GNU_BUILD_ID
: {
4459 OS
<< " Build ID: " << getGNUBuildId(Desc
);
4462 case ELF::NT_GNU_GOLD_VERSION
:
4463 OS
<< " Version: " << getGNUGoldVersion(Desc
);
4465 case ELF::NT_GNU_PROPERTY_TYPE_0
:
4466 OS
<< " Properties:";
4467 for (const auto &Property
: getGNUPropertyList
<ELFT
>(Desc
))
4468 OS
<< " " << Property
<< "\n";
4479 template <typename ELFT
>
4480 static AMDNote
getAMDNote(uint32_t NoteType
, ArrayRef
<uint8_t> Desc
) {
4484 case ELF::NT_AMD_AMDGPU_HSA_METADATA
:
4487 std::string(reinterpret_cast<const char *>(Desc
.data()), Desc
.size())};
4488 case ELF::NT_AMD_AMDGPU_ISA
:
4491 std::string(reinterpret_cast<const char *>(Desc
.data()), Desc
.size())};
4500 template <typename ELFT
>
4501 static AMDGPUNote
getAMDGPUNote(uint32_t NoteType
, ArrayRef
<uint8_t> Desc
) {
4505 case ELF::NT_AMDGPU_METADATA
: {
4506 auto MsgPackString
=
4507 StringRef(reinterpret_cast<const char *>(Desc
.data()), Desc
.size());
4508 msgpack::Document MsgPackDoc
;
4509 if (!MsgPackDoc
.readFromBlob(MsgPackString
, /*Multi=*/false))
4510 return {"AMDGPU Metadata", "Invalid AMDGPU Metadata"};
4512 AMDGPU::HSAMD::V3::MetadataVerifier
Verifier(true);
4513 if (!Verifier
.verify(MsgPackDoc
.getRoot()))
4514 return {"AMDGPU Metadata", "Invalid AMDGPU Metadata"};
4516 std::string HSAMetadataString
;
4517 raw_string_ostream
StrOS(HSAMetadataString
);
4518 MsgPackDoc
.toYAML(StrOS
);
4520 return {"AMDGPU Metadata", StrOS
.str()};
4525 struct CoreFileMapping
{
4526 uint64_t Start
, End
, Offset
;
4532 std::vector
<CoreFileMapping
> Mappings
;
4535 static Expected
<CoreNote
> readCoreNote(DataExtractor Desc
) {
4536 // Expected format of the NT_FILE note description:
4537 // 1. # of file mappings (call it N)
4539 // 3. N (start, end, offset) triples
4540 // 4. N packed filenames (null delimited)
4541 // Each field is an Elf_Addr, except for filenames which are char* strings.
4544 const int Bytes
= Desc
.getAddressSize();
4546 if (!Desc
.isValidOffsetForAddress(2))
4547 return createStringError(object_error::parse_failed
,
4548 "malformed note: header too short");
4549 if (Desc
.getData().back() != 0)
4550 return createStringError(object_error::parse_failed
,
4551 "malformed note: not NUL terminated");
4553 uint64_t DescOffset
= 0;
4554 uint64_t FileCount
= Desc
.getAddress(&DescOffset
);
4555 Ret
.PageSize
= Desc
.getAddress(&DescOffset
);
4557 if (!Desc
.isValidOffsetForAddress(3 * FileCount
* Bytes
))
4558 return createStringError(object_error::parse_failed
,
4559 "malformed note: too short for number of files");
4561 uint64_t FilenamesOffset
= 0;
4562 DataExtractor
Filenames(
4563 Desc
.getData().drop_front(DescOffset
+ 3 * FileCount
* Bytes
),
4564 Desc
.isLittleEndian(), Desc
.getAddressSize());
4566 Ret
.Mappings
.resize(FileCount
);
4567 for (CoreFileMapping
&Mapping
: Ret
.Mappings
) {
4568 if (!Filenames
.isValidOffsetForDataOfSize(FilenamesOffset
, 1))
4569 return createStringError(object_error::parse_failed
,
4570 "malformed note: too few filenames");
4571 Mapping
.Start
= Desc
.getAddress(&DescOffset
);
4572 Mapping
.End
= Desc
.getAddress(&DescOffset
);
4573 Mapping
.Offset
= Desc
.getAddress(&DescOffset
);
4574 Mapping
.Filename
= Filenames
.getCStrRef(&FilenamesOffset
);
4580 template <typename ELFT
>
4581 static void printCoreNote(raw_ostream
&OS
, const CoreNote
&Note
) {
4582 // Length of "0x<address>" string.
4583 const int FieldWidth
= ELFT::Is64Bits
? 18 : 10;
4585 OS
<< " Page size: " << format_decimal(Note
.PageSize
, 0) << '\n';
4586 OS
<< " " << right_justify("Start", FieldWidth
) << " "
4587 << right_justify("End", FieldWidth
) << " "
4588 << right_justify("Page Offset", FieldWidth
) << '\n';
4589 for (const CoreFileMapping
&Mapping
: Note
.Mappings
) {
4590 OS
<< " " << format_hex(Mapping
.Start
, FieldWidth
) << " "
4591 << format_hex(Mapping
.End
, FieldWidth
) << " "
4592 << format_hex(Mapping
.Offset
, FieldWidth
) << "\n "
4593 << Mapping
.Filename
<< '\n';
4597 template <class ELFT
>
4598 void GNUStyle
<ELFT
>::printNotes(const ELFFile
<ELFT
> *Obj
) {
4599 auto PrintHeader
= [&](const typename
ELFT::Off Offset
,
4600 const typename
ELFT::Addr Size
) {
4601 OS
<< "Displaying notes found at file offset " << format_hex(Offset
, 10)
4602 << " with length " << format_hex(Size
, 10) << ":\n"
4603 << " Owner Data size \tDescription\n";
4606 auto ProcessNote
= [&](const Elf_Note
&Note
) {
4607 StringRef Name
= Note
.getName();
4608 ArrayRef
<uint8_t> Descriptor
= Note
.getDesc();
4609 Elf_Word Type
= Note
.getType();
4611 // Print the note owner/type.
4612 OS
<< " " << left_justify(Name
, 20) << ' '
4613 << format_hex(Descriptor
.size(), 10) << '\t';
4614 if (Name
== "GNU") {
4615 OS
<< getGNUNoteTypeName(Type
) << '\n';
4616 } else if (Name
== "FreeBSD") {
4617 OS
<< getFreeBSDNoteTypeName(Type
) << '\n';
4618 } else if (Name
== "AMD") {
4619 OS
<< getAMDNoteTypeName(Type
) << '\n';
4620 } else if (Name
== "AMDGPU") {
4621 OS
<< getAMDGPUNoteTypeName(Type
) << '\n';
4623 StringRef NoteType
= Obj
->getHeader()->e_type
== ELF::ET_CORE
4624 ? getCoreNoteTypeName(Type
)
4625 : getGenericNoteTypeName(Type
);
4626 if (!NoteType
.empty())
4627 OS
<< NoteType
<< '\n';
4629 OS
<< "Unknown note type: (" << format_hex(Type
, 10) << ")\n";
4632 // Print the description, or fallback to printing raw bytes for unknown
4634 if (Name
== "GNU") {
4635 printGNUNote
<ELFT
>(OS
, Type
, Descriptor
);
4636 } else if (Name
== "AMD") {
4637 const AMDNote N
= getAMDNote
<ELFT
>(Type
, Descriptor
);
4638 if (!N
.Type
.empty())
4639 OS
<< " " << N
.Type
<< ":\n " << N
.Value
<< '\n';
4640 } else if (Name
== "AMDGPU") {
4641 const AMDGPUNote N
= getAMDGPUNote
<ELFT
>(Type
, Descriptor
);
4642 if (!N
.Type
.empty())
4643 OS
<< " " << N
.Type
<< ":\n " << N
.Value
<< '\n';
4644 } else if (Name
== "CORE") {
4645 if (Type
== ELF::NT_FILE
) {
4646 DataExtractor
DescExtractor(Descriptor
,
4647 ELFT::TargetEndianness
== support::little
,
4649 Expected
<CoreNote
> Note
= readCoreNote(DescExtractor
);
4651 printCoreNote
<ELFT
>(OS
, *Note
);
4653 reportWarning(Note
.takeError(), this->FileName
);
4655 } else if (!Descriptor
.empty()) {
4656 OS
<< " description data:";
4657 for (uint8_t B
: Descriptor
)
4658 OS
<< " " << format("%02x", B
);
4663 ArrayRef
<Elf_Shdr
> Sections
= unwrapOrError(this->FileName
, Obj
->sections());
4664 if (Obj
->getHeader()->e_type
!= ELF::ET_CORE
&& !Sections
.empty()) {
4665 for (const auto &S
: Sections
) {
4666 if (S
.sh_type
!= SHT_NOTE
)
4668 PrintHeader(S
.sh_offset
, S
.sh_size
);
4669 Error Err
= Error::success();
4670 for (const auto &Note
: Obj
->notes(S
, Err
))
4673 reportError(std::move(Err
), this->FileName
);
4676 for (const auto &P
:
4677 unwrapOrError(this->FileName
, Obj
->program_headers())) {
4678 if (P
.p_type
!= PT_NOTE
)
4680 PrintHeader(P
.p_offset
, P
.p_filesz
);
4681 Error Err
= Error::success();
4682 for (const auto &Note
: Obj
->notes(P
, Err
))
4685 reportError(std::move(Err
), this->FileName
);
4690 template <class ELFT
>
4691 void GNUStyle
<ELFT
>::printELFLinkerOptions(const ELFFile
<ELFT
> *Obj
) {
4692 OS
<< "printELFLinkerOptions not implemented!\n";
4695 // Used for printing section names in places where possible errors can be
4697 static StringRef
getSectionName(const SectionRef
&Sec
) {
4698 Expected
<StringRef
> NameOrErr
= Sec
.getName();
4701 consumeError(NameOrErr
.takeError());
4705 // Used for printing symbol names in places where possible errors can be
4707 static std::string
getSymbolName(const ELFSymbolRef
&Sym
) {
4708 Expected
<StringRef
> NameOrErr
= Sym
.getName();
4710 return maybeDemangle(*NameOrErr
);
4711 consumeError(NameOrErr
.takeError());
4715 template <class ELFT
>
4716 void DumpStyle
<ELFT
>::printFunctionStackSize(
4717 const ELFObjectFile
<ELFT
> *Obj
, uint64_t SymValue
, SectionRef FunctionSec
,
4718 const StringRef SectionName
, DataExtractor Data
, uint64_t *Offset
) {
4719 // This function ignores potentially erroneous input, unless it is directly
4720 // related to stack size reporting.
4722 for (const ELFSymbolRef
&Symbol
: Obj
->symbols()) {
4723 Expected
<uint64_t> SymAddrOrErr
= Symbol
.getAddress();
4724 if (!SymAddrOrErr
) {
4725 consumeError(SymAddrOrErr
.takeError());
4728 if (Symbol
.getELFType() == ELF::STT_FUNC
&& *SymAddrOrErr
== SymValue
) {
4729 // Check if the symbol is in the right section.
4730 if (FunctionSec
.containsSymbol(Symbol
)) {
4737 std::string FuncName
= "?";
4738 // A valid SymbolRef has a non-null object file pointer.
4739 if (FuncSym
.BasicSymbolRef::getObject())
4740 FuncName
= getSymbolName(FuncSym
);
4743 createError("could not identify function symbol for stack size entry"),
4744 Obj
->getFileName());
4746 // Extract the size. The expectation is that Offset is pointing to the right
4747 // place, i.e. past the function address.
4748 uint64_t PrevOffset
= *Offset
;
4749 uint64_t StackSize
= Data
.getULEB128(Offset
);
4750 // getULEB128() does not advance Offset if it is not able to extract a valid
4752 if (*Offset
== PrevOffset
)
4754 createStringError(object_error::parse_failed
,
4755 "could not extract a valid stack size in section %s",
4756 SectionName
.data()),
4757 Obj
->getFileName());
4759 printStackSizeEntry(StackSize
, FuncName
);
4762 template <class ELFT
>
4763 void GNUStyle
<ELFT
>::printStackSizeEntry(uint64_t Size
, StringRef FuncName
) {
4765 OS
<< format_decimal(Size
, 11);
4767 OS
<< FuncName
<< "\n";
4770 template <class ELFT
>
4771 void DumpStyle
<ELFT
>::printStackSize(const ELFObjectFile
<ELFT
> *Obj
,
4772 RelocationRef Reloc
,
4773 SectionRef FunctionSec
,
4774 const StringRef
&StackSizeSectionName
,
4775 const RelocationResolver
&Resolver
,
4776 DataExtractor Data
) {
4777 // This function ignores potentially erroneous input, unless it is directly
4778 // related to stack size reporting.
4779 object::symbol_iterator RelocSym
= Reloc
.getSymbol();
4780 uint64_t RelocSymValue
= 0;
4781 StringRef FileStr
= Obj
->getFileName();
4782 if (RelocSym
!= Obj
->symbol_end()) {
4783 // Ensure that the relocation symbol is in the function section, i.e. the
4784 // section where the functions whose stack sizes we are reporting are
4786 auto SectionOrErr
= RelocSym
->getSection();
4787 if (!SectionOrErr
) {
4789 createError("cannot identify the section for relocation symbol '" +
4790 getSymbolName(*RelocSym
) + "'"),
4792 consumeError(SectionOrErr
.takeError());
4793 } else if (*SectionOrErr
!= FunctionSec
) {
4794 reportWarning(createError("relocation symbol '" +
4795 getSymbolName(*RelocSym
) +
4796 "' is not in the expected section"),
4798 // Pretend that the symbol is in the correct section and report its
4799 // stack size anyway.
4800 FunctionSec
= **SectionOrErr
;
4803 Expected
<uint64_t> RelocSymValueOrErr
= RelocSym
->getValue();
4804 if (RelocSymValueOrErr
)
4805 RelocSymValue
= *RelocSymValueOrErr
;
4807 consumeError(RelocSymValueOrErr
.takeError());
4810 uint64_t Offset
= Reloc
.getOffset();
4811 if (!Data
.isValidOffsetForDataOfSize(Offset
, sizeof(Elf_Addr
) + 1))
4813 createStringError(object_error::parse_failed
,
4814 "found invalid relocation offset into section %s "
4815 "while trying to extract a stack size entry",
4816 StackSizeSectionName
.data()),
4819 uint64_t Addend
= Data
.getAddress(&Offset
);
4820 uint64_t SymValue
= Resolver(Reloc
, RelocSymValue
, Addend
);
4821 this->printFunctionStackSize(Obj
, SymValue
, FunctionSec
, StackSizeSectionName
,
4825 template <class ELFT
>
4826 void DumpStyle
<ELFT
>::printNonRelocatableStackSizes(
4827 const ELFObjectFile
<ELFT
> *Obj
, std::function
<void()> PrintHeader
) {
4828 // This function ignores potentially erroneous input, unless it is directly
4829 // related to stack size reporting.
4830 const ELFFile
<ELFT
> *EF
= Obj
->getELFFile();
4831 StringRef FileStr
= Obj
->getFileName();
4832 for (const SectionRef
&Sec
: Obj
->sections()) {
4833 StringRef SectionName
= getSectionName(Sec
);
4834 if (SectionName
!= ".stack_sizes")
4837 const Elf_Shdr
*ElfSec
= Obj
->getSection(Sec
.getRawDataRefImpl());
4838 ArrayRef
<uint8_t> Contents
=
4839 unwrapOrError(this->FileName
, EF
->getSectionContents(ElfSec
));
4840 DataExtractor
Data(Contents
, Obj
->isLittleEndian(), sizeof(Elf_Addr
));
4841 // A .stack_sizes section header's sh_link field is supposed to point
4842 // to the section that contains the functions whose stack sizes are
4844 const Elf_Shdr
*FunctionELFSec
=
4845 unwrapOrError(this->FileName
, EF
->getSection(ElfSec
->sh_link
));
4846 uint64_t Offset
= 0;
4847 while (Offset
< Contents
.size()) {
4848 // The function address is followed by a ULEB representing the stack
4849 // size. Check for an extra byte before we try to process the entry.
4850 if (!Data
.isValidOffsetForDataOfSize(Offset
, sizeof(Elf_Addr
) + 1)) {
4853 object_error::parse_failed
,
4854 "section %s ended while trying to extract a stack size entry",
4855 SectionName
.data()),
4858 uint64_t SymValue
= Data
.getAddress(&Offset
);
4859 printFunctionStackSize(Obj
, SymValue
, Obj
->toSectionRef(FunctionELFSec
),
4860 SectionName
, Data
, &Offset
);
4865 template <class ELFT
>
4866 void DumpStyle
<ELFT
>::printRelocatableStackSizes(
4867 const ELFObjectFile
<ELFT
> *Obj
, std::function
<void()> PrintHeader
) {
4868 const ELFFile
<ELFT
> *EF
= Obj
->getELFFile();
4870 // Build a map between stack size sections and their corresponding relocation
4872 llvm::MapVector
<SectionRef
, SectionRef
> StackSizeRelocMap
;
4873 const SectionRef NullSection
{};
4875 for (const SectionRef
&Sec
: Obj
->sections()) {
4876 StringRef SectionName
;
4877 if (Expected
<StringRef
> NameOrErr
= Sec
.getName())
4878 SectionName
= *NameOrErr
;
4880 consumeError(NameOrErr
.takeError());
4882 // A stack size section that we haven't encountered yet is mapped to the
4883 // null section until we find its corresponding relocation section.
4884 if (SectionName
== ".stack_sizes")
4885 if (StackSizeRelocMap
.count(Sec
) == 0) {
4886 StackSizeRelocMap
[Sec
] = NullSection
;
4890 // Check relocation sections if they are relocating contents of a
4891 // stack sizes section.
4892 const Elf_Shdr
*ElfSec
= Obj
->getSection(Sec
.getRawDataRefImpl());
4893 uint32_t SectionType
= ElfSec
->sh_type
;
4894 if (SectionType
!= ELF::SHT_RELA
&& SectionType
!= ELF::SHT_REL
)
4897 SectionRef Contents
= *Sec
.getRelocatedSection();
4898 const Elf_Shdr
*ContentsSec
= Obj
->getSection(Contents
.getRawDataRefImpl());
4899 Expected
<StringRef
> ContentsSectionNameOrErr
=
4900 EF
->getSectionName(ContentsSec
);
4901 if (!ContentsSectionNameOrErr
) {
4902 consumeError(ContentsSectionNameOrErr
.takeError());
4905 if (*ContentsSectionNameOrErr
!= ".stack_sizes")
4907 // Insert a mapping from the stack sizes section to its relocation section.
4908 StackSizeRelocMap
[Obj
->toSectionRef(ContentsSec
)] = Sec
;
4911 for (const auto &StackSizeMapEntry
: StackSizeRelocMap
) {
4913 const SectionRef
&StackSizesSec
= StackSizeMapEntry
.first
;
4914 const SectionRef
&RelocSec
= StackSizeMapEntry
.second
;
4916 // Warn about stack size sections without a relocation section.
4917 StringRef StackSizeSectionName
= getSectionName(StackSizesSec
);
4918 if (RelocSec
== NullSection
) {
4919 reportWarning(createError("section " + StackSizeSectionName
+
4920 " does not have a corresponding "
4921 "relocation section"),
4922 Obj
->getFileName());
4926 // A .stack_sizes section header's sh_link field is supposed to point
4927 // to the section that contains the functions whose stack sizes are
4929 const Elf_Shdr
*StackSizesELFSec
=
4930 Obj
->getSection(StackSizesSec
.getRawDataRefImpl());
4931 const SectionRef FunctionSec
= Obj
->toSectionRef(unwrapOrError(
4932 this->FileName
, EF
->getSection(StackSizesELFSec
->sh_link
)));
4934 bool (*IsSupportedFn
)(uint64_t);
4935 RelocationResolver Resolver
;
4936 std::tie(IsSupportedFn
, Resolver
) = getRelocationResolver(*Obj
);
4937 auto Contents
= unwrapOrError(this->FileName
, StackSizesSec
.getContents());
4938 DataExtractor
Data(Contents
, Obj
->isLittleEndian(), sizeof(Elf_Addr
));
4939 for (const RelocationRef
&Reloc
: RelocSec
.relocations()) {
4940 if (!IsSupportedFn
|| !IsSupportedFn(Reloc
.getType()))
4941 reportError(createStringError(
4942 object_error::parse_failed
,
4943 "unsupported relocation type in section %s: %s",
4944 getSectionName(RelocSec
).data(),
4945 EF
->getRelocationTypeName(Reloc
.getType()).data()),
4946 Obj
->getFileName());
4947 this->printStackSize(Obj
, Reloc
, FunctionSec
, StackSizeSectionName
,
4953 template <class ELFT
>
4954 void GNUStyle
<ELFT
>::printStackSizes(const ELFObjectFile
<ELFT
> *Obj
) {
4955 bool HeaderHasBeenPrinted
= false;
4956 auto PrintHeader
= [&]() {
4957 if (HeaderHasBeenPrinted
)
4959 OS
<< "\nStack Sizes:\n";
4964 HeaderHasBeenPrinted
= true;
4967 // For non-relocatable objects, look directly for sections whose name starts
4968 // with .stack_sizes and process the contents.
4969 if (Obj
->isRelocatableObject())
4970 this->printRelocatableStackSizes(Obj
, PrintHeader
);
4972 this->printNonRelocatableStackSizes(Obj
, PrintHeader
);
4975 template <class ELFT
>
4976 void GNUStyle
<ELFT
>::printMipsGOT(const MipsGOTParser
<ELFT
> &Parser
) {
4977 size_t Bias
= ELFT::Is64Bits
? 8 : 0;
4978 auto PrintEntry
= [&](const Elf_Addr
*E
, StringRef Purpose
) {
4980 OS
<< format_hex_no_prefix(Parser
.getGotAddress(E
), 8 + Bias
);
4981 OS
.PadToColumn(11 + Bias
);
4982 OS
<< format_decimal(Parser
.getGotOffset(E
), 6) << "(gp)";
4983 OS
.PadToColumn(22 + Bias
);
4984 OS
<< format_hex_no_prefix(*E
, 8 + Bias
);
4985 OS
.PadToColumn(31 + 2 * Bias
);
4986 OS
<< Purpose
<< "\n";
4989 OS
<< (Parser
.IsStatic
? "Static GOT:\n" : "Primary GOT:\n");
4990 OS
<< " Canonical gp value: "
4991 << format_hex_no_prefix(Parser
.getGp(), 8 + Bias
) << "\n\n";
4993 OS
<< " Reserved entries:\n";
4995 OS
<< " Address Access Initial Purpose\n";
4997 OS
<< " Address Access Initial Purpose\n";
4998 PrintEntry(Parser
.getGotLazyResolver(), "Lazy resolver");
4999 if (Parser
.getGotModulePointer())
5000 PrintEntry(Parser
.getGotModulePointer(), "Module pointer (GNU extension)");
5002 if (!Parser
.getLocalEntries().empty()) {
5004 OS
<< " Local entries:\n";
5006 OS
<< " Address Access Initial\n";
5008 OS
<< " Address Access Initial\n";
5009 for (auto &E
: Parser
.getLocalEntries())
5013 if (Parser
.IsStatic
)
5016 if (!Parser
.getGlobalEntries().empty()) {
5018 OS
<< " Global entries:\n";
5020 OS
<< " Address Access Initial Sym.Val."
5021 << " Type Ndx Name\n";
5023 OS
<< " Address Access Initial Sym.Val. Type Ndx Name\n";
5024 for (auto &E
: Parser
.getGlobalEntries()) {
5025 const Elf_Sym
*Sym
= Parser
.getGotSym(&E
);
5026 std::string SymName
= this->dumper()->getFullSymbolName(
5027 Sym
, this->dumper()->getDynamicStringTable(), false);
5030 OS
<< to_string(format_hex_no_prefix(Parser
.getGotAddress(&E
), 8 + Bias
));
5031 OS
.PadToColumn(11 + Bias
);
5032 OS
<< to_string(format_decimal(Parser
.getGotOffset(&E
), 6)) + "(gp)";
5033 OS
.PadToColumn(22 + Bias
);
5034 OS
<< to_string(format_hex_no_prefix(E
, 8 + Bias
));
5035 OS
.PadToColumn(31 + 2 * Bias
);
5036 OS
<< to_string(format_hex_no_prefix(Sym
->st_value
, 8 + Bias
));
5037 OS
.PadToColumn(40 + 3 * Bias
);
5038 OS
<< printEnum(Sym
->getType(), makeArrayRef(ElfSymbolTypes
));
5039 OS
.PadToColumn(48 + 3 * Bias
);
5040 OS
<< getSymbolSectionNdx(Parser
.Obj
, Sym
,
5041 this->dumper()->dynamic_symbols().begin());
5042 OS
.PadToColumn(52 + 3 * Bias
);
5043 OS
<< SymName
<< "\n";
5047 if (!Parser
.getOtherEntries().empty())
5048 OS
<< "\n Number of TLS and multi-GOT entries "
5049 << Parser
.getOtherEntries().size() << "\n";
5052 template <class ELFT
>
5053 void GNUStyle
<ELFT
>::printMipsPLT(const MipsGOTParser
<ELFT
> &Parser
) {
5054 size_t Bias
= ELFT::Is64Bits
? 8 : 0;
5055 auto PrintEntry
= [&](const Elf_Addr
*E
, StringRef Purpose
) {
5057 OS
<< format_hex_no_prefix(Parser
.getPltAddress(E
), 8 + Bias
);
5058 OS
.PadToColumn(11 + Bias
);
5059 OS
<< format_hex_no_prefix(*E
, 8 + Bias
);
5060 OS
.PadToColumn(20 + 2 * Bias
);
5061 OS
<< Purpose
<< "\n";
5064 OS
<< "PLT GOT:\n\n";
5066 OS
<< " Reserved entries:\n";
5067 OS
<< " Address Initial Purpose\n";
5068 PrintEntry(Parser
.getPltLazyResolver(), "PLT lazy resolver");
5069 if (Parser
.getPltModulePointer())
5070 PrintEntry(Parser
.getPltModulePointer(), "Module pointer");
5072 if (!Parser
.getPltEntries().empty()) {
5074 OS
<< " Entries:\n";
5075 OS
<< " Address Initial Sym.Val. Type Ndx Name\n";
5076 for (auto &E
: Parser
.getPltEntries()) {
5077 const Elf_Sym
*Sym
= Parser
.getPltSym(&E
);
5078 std::string SymName
= this->dumper()->getFullSymbolName(
5079 Sym
, this->dumper()->getDynamicStringTable(), false);
5082 OS
<< to_string(format_hex_no_prefix(Parser
.getPltAddress(&E
), 8 + Bias
));
5083 OS
.PadToColumn(11 + Bias
);
5084 OS
<< to_string(format_hex_no_prefix(E
, 8 + Bias
));
5085 OS
.PadToColumn(20 + 2 * Bias
);
5086 OS
<< to_string(format_hex_no_prefix(Sym
->st_value
, 8 + Bias
));
5087 OS
.PadToColumn(29 + 3 * Bias
);
5088 OS
<< printEnum(Sym
->getType(), makeArrayRef(ElfSymbolTypes
));
5089 OS
.PadToColumn(37 + 3 * Bias
);
5090 OS
<< getSymbolSectionNdx(Parser
.Obj
, Sym
,
5091 this->dumper()->dynamic_symbols().begin());
5092 OS
.PadToColumn(41 + 3 * Bias
);
5093 OS
<< SymName
<< "\n";
5098 template <class ELFT
>
5099 void GNUStyle
<ELFT
>::printMipsABIFlags(const ELFObjectFile
<ELFT
> *ObjF
) {
5100 const ELFFile
<ELFT
> *Obj
= ObjF
->getELFFile();
5101 const Elf_Shdr
*Shdr
=
5102 findSectionByName(*Obj
, ObjF
->getFileName(), ".MIPS.abiflags");
5106 ArrayRef
<uint8_t> Sec
=
5107 unwrapOrError(ObjF
->getFileName(), Obj
->getSectionContents(Shdr
));
5108 if (Sec
.size() != sizeof(Elf_Mips_ABIFlags
<ELFT
>))
5109 reportError(createError(".MIPS.abiflags section has a wrong size"),
5110 ObjF
->getFileName());
5112 auto *Flags
= reinterpret_cast<const Elf_Mips_ABIFlags
<ELFT
> *>(Sec
.data());
5114 OS
<< "MIPS ABI Flags Version: " << Flags
->version
<< "\n\n";
5115 OS
<< "ISA: MIPS" << int(Flags
->isa_level
);
5116 if (Flags
->isa_rev
> 1)
5117 OS
<< "r" << int(Flags
->isa_rev
);
5119 OS
<< "GPR size: " << getMipsRegisterSize(Flags
->gpr_size
) << "\n";
5120 OS
<< "CPR1 size: " << getMipsRegisterSize(Flags
->cpr1_size
) << "\n";
5121 OS
<< "CPR2 size: " << getMipsRegisterSize(Flags
->cpr2_size
) << "\n";
5122 OS
<< "FP ABI: " << printEnum(Flags
->fp_abi
, makeArrayRef(ElfMipsFpABIType
))
5124 OS
<< "ISA Extension: "
5125 << printEnum(Flags
->isa_ext
, makeArrayRef(ElfMipsISAExtType
)) << "\n";
5126 if (Flags
->ases
== 0)
5127 OS
<< "ASEs: None\n";
5129 // FIXME: Print each flag on a separate line.
5130 OS
<< "ASEs: " << printFlags(Flags
->ases
, makeArrayRef(ElfMipsASEFlags
))
5132 OS
<< "FLAGS 1: " << format_hex_no_prefix(Flags
->flags1
, 8, false) << "\n";
5133 OS
<< "FLAGS 2: " << format_hex_no_prefix(Flags
->flags2
, 8, false) << "\n";
5137 template <class ELFT
> void LLVMStyle
<ELFT
>::printFileHeaders(const ELFO
*Obj
) {
5138 const Elf_Ehdr
*E
= Obj
->getHeader();
5140 DictScope
D(W
, "ElfHeader");
5142 DictScope
D(W
, "Ident");
5143 W
.printBinary("Magic", makeArrayRef(E
->e_ident
).slice(ELF::EI_MAG0
, 4));
5144 W
.printEnum("Class", E
->e_ident
[ELF::EI_CLASS
], makeArrayRef(ElfClass
));
5145 W
.printEnum("DataEncoding", E
->e_ident
[ELF::EI_DATA
],
5146 makeArrayRef(ElfDataEncoding
));
5147 W
.printNumber("FileVersion", E
->e_ident
[ELF::EI_VERSION
]);
5149 auto OSABI
= makeArrayRef(ElfOSABI
);
5150 if (E
->e_ident
[ELF::EI_OSABI
] >= ELF::ELFOSABI_FIRST_ARCH
&&
5151 E
->e_ident
[ELF::EI_OSABI
] <= ELF::ELFOSABI_LAST_ARCH
) {
5152 switch (E
->e_machine
) {
5153 case ELF::EM_AMDGPU
:
5154 OSABI
= makeArrayRef(AMDGPUElfOSABI
);
5157 OSABI
= makeArrayRef(ARMElfOSABI
);
5159 case ELF::EM_TI_C6000
:
5160 OSABI
= makeArrayRef(C6000ElfOSABI
);
5164 W
.printEnum("OS/ABI", E
->e_ident
[ELF::EI_OSABI
], OSABI
);
5165 W
.printNumber("ABIVersion", E
->e_ident
[ELF::EI_ABIVERSION
]);
5166 W
.printBinary("Unused", makeArrayRef(E
->e_ident
).slice(ELF::EI_PAD
));
5169 W
.printEnum("Type", E
->e_type
, makeArrayRef(ElfObjectFileType
));
5170 W
.printEnum("Machine", E
->e_machine
, makeArrayRef(ElfMachineType
));
5171 W
.printNumber("Version", E
->e_version
);
5172 W
.printHex("Entry", E
->e_entry
);
5173 W
.printHex("ProgramHeaderOffset", E
->e_phoff
);
5174 W
.printHex("SectionHeaderOffset", E
->e_shoff
);
5175 if (E
->e_machine
== EM_MIPS
)
5176 W
.printFlags("Flags", E
->e_flags
, makeArrayRef(ElfHeaderMipsFlags
),
5177 unsigned(ELF::EF_MIPS_ARCH
), unsigned(ELF::EF_MIPS_ABI
),
5178 unsigned(ELF::EF_MIPS_MACH
));
5179 else if (E
->e_machine
== EM_AMDGPU
)
5180 W
.printFlags("Flags", E
->e_flags
, makeArrayRef(ElfHeaderAMDGPUFlags
),
5181 unsigned(ELF::EF_AMDGPU_MACH
));
5182 else if (E
->e_machine
== EM_RISCV
)
5183 W
.printFlags("Flags", E
->e_flags
, makeArrayRef(ElfHeaderRISCVFlags
));
5185 W
.printFlags("Flags", E
->e_flags
);
5186 W
.printNumber("HeaderSize", E
->e_ehsize
);
5187 W
.printNumber("ProgramHeaderEntrySize", E
->e_phentsize
);
5188 W
.printNumber("ProgramHeaderCount", E
->e_phnum
);
5189 W
.printNumber("SectionHeaderEntrySize", E
->e_shentsize
);
5190 W
.printString("SectionHeaderCount",
5191 getSectionHeadersNumString(Obj
, this->FileName
));
5192 W
.printString("StringTableSectionIndex",
5193 getSectionHeaderTableIndexString(Obj
, this->FileName
));
5197 template <class ELFT
>
5198 void LLVMStyle
<ELFT
>::printGroupSections(const ELFO
*Obj
) {
5199 DictScope
Lists(W
, "Groups");
5200 std::vector
<GroupSection
> V
= getGroups
<ELFT
>(Obj
, this->FileName
);
5201 DenseMap
<uint64_t, const GroupSection
*> Map
= mapSectionsToGroups(V
);
5202 for (const GroupSection
&G
: V
) {
5203 DictScope
D(W
, "Group");
5204 W
.printNumber("Name", G
.Name
, G
.ShName
);
5205 W
.printNumber("Index", G
.Index
);
5206 W
.printNumber("Link", G
.Link
);
5207 W
.printNumber("Info", G
.Info
);
5208 W
.printHex("Type", getGroupType(G
.Type
), G
.Type
);
5209 W
.startLine() << "Signature: " << G
.Signature
<< "\n";
5211 ListScope
L(W
, "Section(s) in group");
5212 for (const GroupMember
&GM
: G
.Members
) {
5213 const GroupSection
*MainGroup
= Map
[GM
.Index
];
5214 if (MainGroup
!= &G
) {
5216 errs() << "Error: " << GM
.Name
<< " (" << GM
.Index
5217 << ") in a group " + G
.Name
+ " (" << G
.Index
5218 << ") is already in a group " + MainGroup
->Name
+ " ("
5219 << MainGroup
->Index
<< ")\n";
5223 W
.startLine() << GM
.Name
<< " (" << GM
.Index
<< ")\n";
5228 W
.startLine() << "There are no group sections in the file.\n";
5231 template <class ELFT
> void LLVMStyle
<ELFT
>::printRelocations(const ELFO
*Obj
) {
5232 ListScope
D(W
, "Relocations");
5234 int SectionNumber
= -1;
5235 for (const Elf_Shdr
&Sec
: unwrapOrError(this->FileName
, Obj
->sections())) {
5238 if (Sec
.sh_type
!= ELF::SHT_REL
&& Sec
.sh_type
!= ELF::SHT_RELA
&&
5239 Sec
.sh_type
!= ELF::SHT_RELR
&& Sec
.sh_type
!= ELF::SHT_ANDROID_REL
&&
5240 Sec
.sh_type
!= ELF::SHT_ANDROID_RELA
&&
5241 Sec
.sh_type
!= ELF::SHT_ANDROID_RELR
)
5244 StringRef Name
= unwrapOrError(this->FileName
, Obj
->getSectionName(&Sec
));
5246 W
.startLine() << "Section (" << SectionNumber
<< ") " << Name
<< " {\n";
5249 printRelocations(&Sec
, Obj
);
5252 W
.startLine() << "}\n";
5256 template <class ELFT
>
5257 void LLVMStyle
<ELFT
>::printRelocations(const Elf_Shdr
*Sec
, const ELFO
*Obj
) {
5258 const Elf_Shdr
*SymTab
=
5259 unwrapOrError(this->FileName
, Obj
->getSection(Sec
->sh_link
));
5261 switch (Sec
->sh_type
) {
5263 for (const Elf_Rel
&R
: unwrapOrError(this->FileName
, Obj
->rels(Sec
))) {
5265 Rela
.r_offset
= R
.r_offset
;
5266 Rela
.r_info
= R
.r_info
;
5268 printRelocation(Obj
, Rela
, SymTab
);
5272 for (const Elf_Rela
&R
: unwrapOrError(this->FileName
, Obj
->relas(Sec
)))
5273 printRelocation(Obj
, R
, SymTab
);
5276 case ELF::SHT_ANDROID_RELR
: {
5277 Elf_Relr_Range Relrs
= unwrapOrError(this->FileName
, Obj
->relrs(Sec
));
5278 if (opts::RawRelr
) {
5279 for (const Elf_Relr
&R
: Relrs
)
5280 W
.startLine() << W
.hex(R
) << "\n";
5282 std::vector
<Elf_Rela
> RelrRelas
=
5283 unwrapOrError(this->FileName
, Obj
->decode_relrs(Relrs
));
5284 for (const Elf_Rela
&R
: RelrRelas
)
5285 printRelocation(Obj
, R
, SymTab
);
5289 case ELF::SHT_ANDROID_REL
:
5290 case ELF::SHT_ANDROID_RELA
:
5291 for (const Elf_Rela
&R
:
5292 unwrapOrError(this->FileName
, Obj
->android_relas(Sec
)))
5293 printRelocation(Obj
, R
, SymTab
);
5298 template <class ELFT
>
5299 void LLVMStyle
<ELFT
>::printRelocation(const ELFO
*Obj
, Elf_Rela Rel
,
5300 const Elf_Shdr
*SymTab
) {
5301 SmallString
<32> RelocName
;
5302 Obj
->getRelocationTypeName(Rel
.getType(Obj
->isMips64EL()), RelocName
);
5303 std::string TargetName
;
5304 const Elf_Sym
*Sym
=
5305 unwrapOrError(this->FileName
, Obj
->getRelocationSymbol(&Rel
, SymTab
));
5306 if (Sym
&& Sym
->getType() == ELF::STT_SECTION
) {
5307 const Elf_Shdr
*Sec
= unwrapOrError(
5309 Obj
->getSection(Sym
, SymTab
, this->dumper()->getShndxTable()));
5310 TargetName
= unwrapOrError(this->FileName
, Obj
->getSectionName(Sec
));
5312 StringRef StrTable
=
5313 unwrapOrError(this->FileName
, Obj
->getStringTableForSymtab(*SymTab
));
5314 TargetName
= this->dumper()->getFullSymbolName(
5315 Sym
, StrTable
, SymTab
->sh_type
== SHT_DYNSYM
/* IsDynamic */);
5318 if (opts::ExpandRelocs
) {
5319 DictScope
Group(W
, "Relocation");
5320 W
.printHex("Offset", Rel
.r_offset
);
5321 W
.printNumber("Type", RelocName
, (int)Rel
.getType(Obj
->isMips64EL()));
5322 W
.printNumber("Symbol", !TargetName
.empty() ? TargetName
: "-",
5323 Rel
.getSymbol(Obj
->isMips64EL()));
5324 W
.printHex("Addend", Rel
.r_addend
);
5326 raw_ostream
&OS
= W
.startLine();
5327 OS
<< W
.hex(Rel
.r_offset
) << " " << RelocName
<< " "
5328 << (!TargetName
.empty() ? TargetName
: "-") << " " << W
.hex(Rel
.r_addend
)
5333 template <class ELFT
>
5334 void LLVMStyle
<ELFT
>::printSectionHeaders(const ELFO
*Obj
) {
5335 ListScope
SectionsD(W
, "Sections");
5337 int SectionIndex
= -1;
5338 ArrayRef
<Elf_Shdr
> Sections
= unwrapOrError(this->FileName
, Obj
->sections());
5339 const ELFObjectFile
<ELFT
> *ElfObj
= this->dumper()->getElfObject();
5340 for (const Elf_Shdr
&Sec
: Sections
) {
5341 StringRef Name
= unwrapOrError(
5342 ElfObj
->getFileName(), Obj
->getSectionName(&Sec
, this->WarningHandler
));
5343 DictScope
SectionD(W
, "Section");
5344 W
.printNumber("Index", ++SectionIndex
);
5345 W
.printNumber("Name", Name
, Sec
.sh_name
);
5348 object::getELFSectionTypeName(Obj
->getHeader()->e_machine
, Sec
.sh_type
),
5350 std::vector
<EnumEntry
<unsigned>> SectionFlags(std::begin(ElfSectionFlags
),
5351 std::end(ElfSectionFlags
));
5352 switch (Obj
->getHeader()->e_machine
) {
5354 SectionFlags
.insert(SectionFlags
.end(), std::begin(ElfARMSectionFlags
),
5355 std::end(ElfARMSectionFlags
));
5358 SectionFlags
.insert(SectionFlags
.end(),
5359 std::begin(ElfHexagonSectionFlags
),
5360 std::end(ElfHexagonSectionFlags
));
5363 SectionFlags
.insert(SectionFlags
.end(), std::begin(ElfMipsSectionFlags
),
5364 std::end(ElfMipsSectionFlags
));
5367 SectionFlags
.insert(SectionFlags
.end(), std::begin(ElfX86_64SectionFlags
),
5368 std::end(ElfX86_64SectionFlags
));
5371 SectionFlags
.insert(SectionFlags
.end(), std::begin(ElfXCoreSectionFlags
),
5372 std::end(ElfXCoreSectionFlags
));
5378 W
.printFlags("Flags", Sec
.sh_flags
, makeArrayRef(SectionFlags
));
5379 W
.printHex("Address", Sec
.sh_addr
);
5380 W
.printHex("Offset", Sec
.sh_offset
);
5381 W
.printNumber("Size", Sec
.sh_size
);
5382 W
.printNumber("Link", Sec
.sh_link
);
5383 W
.printNumber("Info", Sec
.sh_info
);
5384 W
.printNumber("AddressAlignment", Sec
.sh_addralign
);
5385 W
.printNumber("EntrySize", Sec
.sh_entsize
);
5387 if (opts::SectionRelocations
) {
5388 ListScope
D(W
, "Relocations");
5389 printRelocations(&Sec
, Obj
);
5392 if (opts::SectionSymbols
) {
5393 ListScope
D(W
, "Symbols");
5394 const Elf_Shdr
*Symtab
= this->dumper()->getDotSymtabSec();
5395 StringRef StrTable
=
5396 unwrapOrError(this->FileName
, Obj
->getStringTableForSymtab(*Symtab
));
5398 for (const Elf_Sym
&Sym
:
5399 unwrapOrError(this->FileName
, Obj
->symbols(Symtab
))) {
5400 const Elf_Shdr
*SymSec
= unwrapOrError(
5402 Obj
->getSection(&Sym
, Symtab
, this->dumper()->getShndxTable()));
5406 unwrapOrError(this->FileName
, Obj
->symbols(Symtab
)).begin(),
5407 StrTable
, false, false);
5411 if (opts::SectionData
&& Sec
.sh_type
!= ELF::SHT_NOBITS
) {
5412 ArrayRef
<uint8_t> Data
=
5413 unwrapOrError(this->FileName
, Obj
->getSectionContents(&Sec
));
5416 StringRef(reinterpret_cast<const char *>(Data
.data()), Data
.size()));
5421 template <class ELFT
>
5422 void LLVMStyle
<ELFT
>::printSymbol(const ELFO
*Obj
, const Elf_Sym
*Symbol
,
5423 const Elf_Sym
*First
, StringRef StrTable
,
5425 bool /*NonVisibilityBitsUsed*/) {
5426 unsigned SectionIndex
= 0;
5427 StringRef SectionName
;
5428 this->dumper()->getSectionNameIndex(Symbol
, First
, SectionName
, SectionIndex
);
5429 std::string FullSymbolName
=
5430 this->dumper()->getFullSymbolName(Symbol
, StrTable
, IsDynamic
);
5431 unsigned char SymbolType
= Symbol
->getType();
5433 DictScope
D(W
, "Symbol");
5434 W
.printNumber("Name", FullSymbolName
, Symbol
->st_name
);
5435 W
.printHex("Value", Symbol
->st_value
);
5436 W
.printNumber("Size", Symbol
->st_size
);
5437 W
.printEnum("Binding", Symbol
->getBinding(), makeArrayRef(ElfSymbolBindings
));
5438 if (Obj
->getHeader()->e_machine
== ELF::EM_AMDGPU
&&
5439 SymbolType
>= ELF::STT_LOOS
&& SymbolType
< ELF::STT_HIOS
)
5440 W
.printEnum("Type", SymbolType
, makeArrayRef(AMDGPUSymbolTypes
));
5442 W
.printEnum("Type", SymbolType
, makeArrayRef(ElfSymbolTypes
));
5443 if (Symbol
->st_other
== 0)
5444 // Usually st_other flag is zero. Do not pollute the output
5445 // by flags enumeration in that case.
5446 W
.printNumber("Other", 0);
5448 std::vector
<EnumEntry
<unsigned>> SymOtherFlags(std::begin(ElfSymOtherFlags
),
5449 std::end(ElfSymOtherFlags
));
5450 if (Obj
->getHeader()->e_machine
== EM_MIPS
) {
5451 // Someones in their infinite wisdom decided to make STO_MIPS_MIPS16
5452 // flag overlapped with other ST_MIPS_xxx flags. So consider both
5453 // cases separately.
5454 if ((Symbol
->st_other
& STO_MIPS_MIPS16
) == STO_MIPS_MIPS16
)
5455 SymOtherFlags
.insert(SymOtherFlags
.end(),
5456 std::begin(ElfMips16SymOtherFlags
),
5457 std::end(ElfMips16SymOtherFlags
));
5459 SymOtherFlags
.insert(SymOtherFlags
.end(),
5460 std::begin(ElfMipsSymOtherFlags
),
5461 std::end(ElfMipsSymOtherFlags
));
5463 W
.printFlags("Other", Symbol
->st_other
, makeArrayRef(SymOtherFlags
), 0x3u
);
5465 W
.printHex("Section", SectionName
, SectionIndex
);
5468 template <class ELFT
>
5469 void LLVMStyle
<ELFT
>::printSymbols(const ELFO
*Obj
, bool PrintSymbols
,
5470 bool PrintDynamicSymbols
) {
5473 if (PrintDynamicSymbols
)
5474 printDynamicSymbols(Obj
);
5477 template <class ELFT
> void LLVMStyle
<ELFT
>::printSymbols(const ELFO
*Obj
) {
5478 ListScope
Group(W
, "Symbols");
5479 this->dumper()->printSymbolsHelper(false);
5482 template <class ELFT
>
5483 void LLVMStyle
<ELFT
>::printDynamicSymbols(const ELFO
*Obj
) {
5484 ListScope
Group(W
, "DynamicSymbols");
5485 this->dumper()->printSymbolsHelper(true);
5488 template <class ELFT
> void LLVMStyle
<ELFT
>::printDynamic(const ELFFile
<ELFT
> *Obj
) {
5489 Elf_Dyn_Range Table
= this->dumper()->dynamic_table();
5493 raw_ostream
&OS
= W
.getOStream();
5494 W
.startLine() << "DynamicSection [ (" << Table
.size() << " entries)\n";
5496 bool Is64
= ELFT::Is64Bits
;
5498 W
.startLine() << " Tag Type Name/Value\n";
5500 W
.startLine() << " Tag Type Name/Value\n";
5501 for (auto Entry
: Table
) {
5502 uintX_t Tag
= Entry
.getTag();
5503 W
.startLine() << " " << format_hex(Tag
, Is64
? 18 : 10, true) << " "
5505 getTypeString(Obj
->getHeader()->e_machine
, Tag
));
5506 this->dumper()->printDynamicEntry(OS
, Tag
, Entry
.getVal());
5510 W
.startLine() << "]\n";
5513 template <class ELFT
>
5514 void LLVMStyle
<ELFT
>::printDynamicRelocations(const ELFO
*Obj
) {
5515 const DynRegionInfo
&DynRelRegion
= this->dumper()->getDynRelRegion();
5516 const DynRegionInfo
&DynRelaRegion
= this->dumper()->getDynRelaRegion();
5517 const DynRegionInfo
&DynRelrRegion
= this->dumper()->getDynRelrRegion();
5518 const DynRegionInfo
&DynPLTRelRegion
= this->dumper()->getDynPLTRelRegion();
5519 if (DynRelRegion
.Size
&& DynRelaRegion
.Size
)
5520 report_fatal_error("There are both REL and RELA dynamic relocations");
5521 W
.startLine() << "Dynamic Relocations {\n";
5523 if (DynRelaRegion
.Size
> 0)
5524 for (const Elf_Rela
&Rela
: this->dumper()->dyn_relas())
5525 printDynamicRelocation(Obj
, Rela
);
5527 for (const Elf_Rel
&Rel
: this->dumper()->dyn_rels()) {
5529 Rela
.r_offset
= Rel
.r_offset
;
5530 Rela
.r_info
= Rel
.r_info
;
5532 printDynamicRelocation(Obj
, Rela
);
5534 if (DynRelrRegion
.Size
> 0) {
5535 Elf_Relr_Range Relrs
= this->dumper()->dyn_relrs();
5536 std::vector
<Elf_Rela
> RelrRelas
=
5537 unwrapOrError(this->FileName
, Obj
->decode_relrs(Relrs
));
5538 for (const Elf_Rela
&Rela
: RelrRelas
)
5539 printDynamicRelocation(Obj
, Rela
);
5541 if (DynPLTRelRegion
.EntSize
== sizeof(Elf_Rela
))
5542 for (const Elf_Rela
&Rela
: DynPLTRelRegion
.getAsArrayRef
<Elf_Rela
>())
5543 printDynamicRelocation(Obj
, Rela
);
5545 for (const Elf_Rel
&Rel
: DynPLTRelRegion
.getAsArrayRef
<Elf_Rel
>()) {
5547 Rela
.r_offset
= Rel
.r_offset
;
5548 Rela
.r_info
= Rel
.r_info
;
5550 printDynamicRelocation(Obj
, Rela
);
5553 W
.startLine() << "}\n";
5556 template <class ELFT
>
5557 void LLVMStyle
<ELFT
>::printDynamicRelocation(const ELFO
*Obj
, Elf_Rela Rel
) {
5558 SmallString
<32> RelocName
;
5559 Obj
->getRelocationTypeName(Rel
.getType(Obj
->isMips64EL()), RelocName
);
5560 std::string SymbolName
=
5561 getSymbolForReloc(Obj
, this->FileName
, this->dumper(), Rel
).Name
;
5563 if (opts::ExpandRelocs
) {
5564 DictScope
Group(W
, "Relocation");
5565 W
.printHex("Offset", Rel
.r_offset
);
5566 W
.printNumber("Type", RelocName
, (int)Rel
.getType(Obj
->isMips64EL()));
5567 W
.printString("Symbol", !SymbolName
.empty() ? SymbolName
: "-");
5568 W
.printHex("Addend", Rel
.r_addend
);
5570 raw_ostream
&OS
= W
.startLine();
5571 OS
<< W
.hex(Rel
.r_offset
) << " " << RelocName
<< " "
5572 << (!SymbolName
.empty() ? SymbolName
: "-") << " " << W
.hex(Rel
.r_addend
)
5577 template <class ELFT
>
5578 void LLVMStyle
<ELFT
>::printProgramHeaders(
5579 const ELFO
*Obj
, bool PrintProgramHeaders
,
5580 cl::boolOrDefault PrintSectionMapping
) {
5581 if (PrintProgramHeaders
)
5582 printProgramHeaders(Obj
);
5583 if (PrintSectionMapping
== cl::BOU_TRUE
)
5584 printSectionMapping(Obj
);
5587 template <class ELFT
>
5588 void LLVMStyle
<ELFT
>::printProgramHeaders(const ELFO
*Obj
) {
5589 ListScope
L(W
, "ProgramHeaders");
5591 for (const Elf_Phdr
&Phdr
:
5592 unwrapOrError(this->FileName
, Obj
->program_headers())) {
5593 DictScope
P(W
, "ProgramHeader");
5595 getElfSegmentType(Obj
->getHeader()->e_machine
, Phdr
.p_type
),
5597 W
.printHex("Offset", Phdr
.p_offset
);
5598 W
.printHex("VirtualAddress", Phdr
.p_vaddr
);
5599 W
.printHex("PhysicalAddress", Phdr
.p_paddr
);
5600 W
.printNumber("FileSize", Phdr
.p_filesz
);
5601 W
.printNumber("MemSize", Phdr
.p_memsz
);
5602 W
.printFlags("Flags", Phdr
.p_flags
, makeArrayRef(ElfSegmentFlags
));
5603 W
.printNumber("Alignment", Phdr
.p_align
);
5607 template <class ELFT
>
5608 void LLVMStyle
<ELFT
>::printVersionSymbolSection(const ELFFile
<ELFT
> *Obj
,
5609 const Elf_Shdr
*Sec
) {
5610 DictScope
SS(W
, "Version symbols");
5614 StringRef SecName
= unwrapOrError(this->FileName
, Obj
->getSectionName(Sec
));
5615 W
.printNumber("Section Name", SecName
, Sec
->sh_name
);
5616 W
.printHex("Address", Sec
->sh_addr
);
5617 W
.printHex("Offset", Sec
->sh_offset
);
5618 W
.printNumber("Link", Sec
->sh_link
);
5620 const uint8_t *VersymBuf
=
5621 reinterpret_cast<const uint8_t *>(Obj
->base() + Sec
->sh_offset
);
5622 const ELFDumper
<ELFT
> *Dumper
= this->dumper();
5623 StringRef StrTable
= Dumper
->getDynamicStringTable();
5625 // Same number of entries in the dynamic symbol table (DT_SYMTAB).
5626 ListScope
Syms(W
, "Symbols");
5627 for (const Elf_Sym
&Sym
: Dumper
->dynamic_symbols()) {
5628 DictScope
S(W
, "Symbol");
5629 const Elf_Versym
*Versym
= reinterpret_cast<const Elf_Versym
*>(VersymBuf
);
5630 std::string FullSymbolName
=
5631 Dumper
->getFullSymbolName(&Sym
, StrTable
, true /* IsDynamic */);
5632 W
.printNumber("Version", Versym
->vs_index
& VERSYM_VERSION
);
5633 W
.printString("Name", FullSymbolName
);
5634 VersymBuf
+= sizeof(Elf_Versym
);
5638 template <class ELFT
>
5639 void LLVMStyle
<ELFT
>::printVersionDefinitionSection(const ELFFile
<ELFT
> *Obj
,
5640 const Elf_Shdr
*Sec
) {
5641 DictScope
SD(W
, "SHT_GNU_verdef");
5645 const uint8_t *SecStartAddress
=
5646 reinterpret_cast<const uint8_t *>(Obj
->base() + Sec
->sh_offset
);
5647 const uint8_t *SecEndAddress
= SecStartAddress
+ Sec
->sh_size
;
5648 const uint8_t *VerdefBuf
= SecStartAddress
;
5649 const Elf_Shdr
*StrTab
=
5650 unwrapOrError(this->FileName
, Obj
->getSection(Sec
->sh_link
));
5652 unsigned VerDefsNum
= Sec
->sh_info
;
5653 while (VerDefsNum
--) {
5654 if (VerdefBuf
+ sizeof(Elf_Verdef
) > SecEndAddress
)
5655 // FIXME: report_fatal_error is not a good way to report error. We should
5656 // emit a parsing error here and below.
5657 report_fatal_error("invalid offset in the section");
5659 const Elf_Verdef
*Verdef
= reinterpret_cast<const Elf_Verdef
*>(VerdefBuf
);
5660 DictScope
Def(W
, "Definition");
5661 W
.printNumber("Version", Verdef
->vd_version
);
5662 W
.printEnum("Flags", Verdef
->vd_flags
, makeArrayRef(SymVersionFlags
));
5663 W
.printNumber("Index", Verdef
->vd_ndx
);
5664 W
.printNumber("Hash", Verdef
->vd_hash
);
5665 W
.printString("Name", StringRef(reinterpret_cast<const char *>(
5666 Obj
->base() + StrTab
->sh_offset
+
5667 Verdef
->getAux()->vda_name
)));
5668 if (!Verdef
->vd_cnt
)
5669 report_fatal_error("at least one definition string must exist");
5670 if (Verdef
->vd_cnt
> 2)
5671 report_fatal_error("more than one predecessor is not expected");
5673 if (Verdef
->vd_cnt
== 2) {
5674 const uint8_t *VerdauxBuf
=
5675 VerdefBuf
+ Verdef
->vd_aux
+ Verdef
->getAux()->vda_next
;
5676 const Elf_Verdaux
*Verdaux
=
5677 reinterpret_cast<const Elf_Verdaux
*>(VerdauxBuf
);
5678 W
.printString("Predecessor",
5679 StringRef(reinterpret_cast<const char *>(
5680 Obj
->base() + StrTab
->sh_offset
+ Verdaux
->vda_name
)));
5682 VerdefBuf
+= Verdef
->vd_next
;
5686 template <class ELFT
>
5687 void LLVMStyle
<ELFT
>::printVersionDependencySection(const ELFFile
<ELFT
> *Obj
,
5688 const Elf_Shdr
*Sec
) {
5689 DictScope
SD(W
, "SHT_GNU_verneed");
5693 const uint8_t *SecData
=
5694 reinterpret_cast<const uint8_t *>(Obj
->base() + Sec
->sh_offset
);
5695 const Elf_Shdr
*StrTab
=
5696 unwrapOrError(this->FileName
, Obj
->getSection(Sec
->sh_link
));
5698 const uint8_t *VerneedBuf
= SecData
;
5699 unsigned VerneedNum
= Sec
->sh_info
;
5700 for (unsigned I
= 0; I
< VerneedNum
; ++I
) {
5701 const Elf_Verneed
*Verneed
=
5702 reinterpret_cast<const Elf_Verneed
*>(VerneedBuf
);
5703 DictScope
Entry(W
, "Dependency");
5704 W
.printNumber("Version", Verneed
->vn_version
);
5705 W
.printNumber("Count", Verneed
->vn_cnt
);
5706 W
.printString("FileName",
5707 StringRef(reinterpret_cast<const char *>(
5708 Obj
->base() + StrTab
->sh_offset
+ Verneed
->vn_file
)));
5710 const uint8_t *VernauxBuf
= VerneedBuf
+ Verneed
->vn_aux
;
5711 ListScope
L(W
, "Entries");
5712 for (unsigned J
= 0; J
< Verneed
->vn_cnt
; ++J
) {
5713 const Elf_Vernaux
*Vernaux
=
5714 reinterpret_cast<const Elf_Vernaux
*>(VernauxBuf
);
5715 DictScope
Entry(W
, "Entry");
5716 W
.printNumber("Hash", Vernaux
->vna_hash
);
5717 W
.printEnum("Flags", Vernaux
->vna_flags
, makeArrayRef(SymVersionFlags
));
5718 W
.printNumber("Index", Vernaux
->vna_other
);
5719 W
.printString("Name",
5720 StringRef(reinterpret_cast<const char *>(
5721 Obj
->base() + StrTab
->sh_offset
+ Vernaux
->vna_name
)));
5722 VernauxBuf
+= Vernaux
->vna_next
;
5724 VerneedBuf
+= Verneed
->vn_next
;
5728 template <class ELFT
>
5729 void LLVMStyle
<ELFT
>::printHashHistogram(const ELFFile
<ELFT
> *Obj
) {
5730 W
.startLine() << "Hash Histogram not implemented!\n";
5733 template <class ELFT
>
5734 void LLVMStyle
<ELFT
>::printCGProfile(const ELFFile
<ELFT
> *Obj
) {
5735 ListScope
L(W
, "CGProfile");
5736 if (!this->dumper()->getDotCGProfileSec())
5738 auto CGProfile
= unwrapOrError(
5739 this->FileName
, Obj
->template getSectionContentsAsArray
<Elf_CGProfile
>(
5740 this->dumper()->getDotCGProfileSec()));
5741 for (const Elf_CGProfile
&CGPE
: CGProfile
) {
5742 DictScope
D(W
, "CGProfileEntry");
5745 unwrapOrError(this->FileName
,
5746 this->dumper()->getStaticSymbolName(CGPE
.cgp_from
)),
5750 unwrapOrError(this->FileName
,
5751 this->dumper()->getStaticSymbolName(CGPE
.cgp_to
)),
5753 W
.printNumber("Weight", CGPE
.cgp_weight
);
5757 static Expected
<std::vector
<uint64_t>> toULEB128Array(ArrayRef
<uint8_t> Data
) {
5758 std::vector
<uint64_t> Ret
;
5759 const uint8_t *Cur
= Data
.begin();
5760 const uint8_t *End
= Data
.end();
5761 while (Cur
!= End
) {
5764 Ret
.push_back(decodeULEB128(Cur
, &Size
, End
, &Err
));
5766 return createError(Err
);
5772 template <class ELFT
>
5773 void LLVMStyle
<ELFT
>::printAddrsig(const ELFFile
<ELFT
> *Obj
) {
5774 ListScope
L(W
, "Addrsig");
5775 if (!this->dumper()->getDotAddrsigSec())
5777 ArrayRef
<uint8_t> Contents
= unwrapOrError(
5779 Obj
->getSectionContents(this->dumper()->getDotAddrsigSec()));
5780 Expected
<std::vector
<uint64_t>> V
= toULEB128Array(Contents
);
5782 reportWarning(V
.takeError(), this->FileName
);
5786 for (uint64_t Sym
: *V
) {
5787 Expected
<std::string
> NameOrErr
= this->dumper()->getStaticSymbolName(Sym
);
5789 W
.printNumber("Sym", *NameOrErr
, Sym
);
5792 reportWarning(NameOrErr
.takeError(), this->FileName
);
5793 W
.printNumber("Sym", "<?>", Sym
);
5797 template <typename ELFT
>
5798 static void printGNUNoteLLVMStyle(uint32_t NoteType
, ArrayRef
<uint8_t> Desc
,
5803 case ELF::NT_GNU_ABI_TAG
: {
5804 const GNUAbiTag
&AbiTag
= getGNUAbiTag
<ELFT
>(Desc
);
5805 if (!AbiTag
.IsValid
) {
5806 W
.printString("ABI", "<corrupt GNU_ABI_TAG>");
5808 W
.printString("OS", AbiTag
.OSName
);
5809 W
.printString("ABI", AbiTag
.ABI
);
5813 case ELF::NT_GNU_BUILD_ID
: {
5814 W
.printString("Build ID", getGNUBuildId(Desc
));
5817 case ELF::NT_GNU_GOLD_VERSION
:
5818 W
.printString("Version", getGNUGoldVersion(Desc
));
5820 case ELF::NT_GNU_PROPERTY_TYPE_0
:
5821 ListScope
D(W
, "Property");
5822 for (const auto &Property
: getGNUPropertyList
<ELFT
>(Desc
))
5823 W
.printString(Property
);
5828 static void printCoreNoteLLVMStyle(const CoreNote
&Note
, ScopedPrinter
&W
) {
5829 W
.printNumber("Page Size", Note
.PageSize
);
5830 for (const CoreFileMapping
&Mapping
: Note
.Mappings
) {
5831 ListScope
D(W
, "Mapping");
5832 W
.printHex("Start", Mapping
.Start
);
5833 W
.printHex("End", Mapping
.End
);
5834 W
.printHex("Offset", Mapping
.Offset
);
5835 W
.printString("Filename", Mapping
.Filename
);
5839 template <class ELFT
>
5840 void LLVMStyle
<ELFT
>::printNotes(const ELFFile
<ELFT
> *Obj
) {
5841 ListScope
L(W
, "Notes");
5843 auto PrintHeader
= [&](const typename
ELFT::Off Offset
,
5844 const typename
ELFT::Addr Size
) {
5845 W
.printHex("Offset", Offset
);
5846 W
.printHex("Size", Size
);
5849 auto ProcessNote
= [&](const Elf_Note
&Note
) {
5850 DictScope
D2(W
, "Note");
5851 StringRef Name
= Note
.getName();
5852 ArrayRef
<uint8_t> Descriptor
= Note
.getDesc();
5853 Elf_Word Type
= Note
.getType();
5855 // Print the note owner/type.
5856 W
.printString("Owner", Name
);
5857 W
.printHex("Data size", Descriptor
.size());
5858 if (Name
== "GNU") {
5859 W
.printString("Type", getGNUNoteTypeName(Type
));
5860 } else if (Name
== "FreeBSD") {
5861 W
.printString("Type", getFreeBSDNoteTypeName(Type
));
5862 } else if (Name
== "AMD") {
5863 W
.printString("Type", getAMDNoteTypeName(Type
));
5864 } else if (Name
== "AMDGPU") {
5865 W
.printString("Type", getAMDGPUNoteTypeName(Type
));
5867 StringRef NoteType
= Obj
->getHeader()->e_type
== ELF::ET_CORE
5868 ? getCoreNoteTypeName(Type
)
5869 : getGenericNoteTypeName(Type
);
5870 if (!NoteType
.empty())
5871 W
.printString("Type", NoteType
);
5873 W
.printString("Type",
5874 "Unknown (" + to_string(format_hex(Type
, 10)) + ")");
5877 // Print the description, or fallback to printing raw bytes for unknown
5879 if (Name
== "GNU") {
5880 printGNUNoteLLVMStyle
<ELFT
>(Type
, Descriptor
, W
);
5881 } else if (Name
== "AMD") {
5882 const AMDNote N
= getAMDNote
<ELFT
>(Type
, Descriptor
);
5883 if (!N
.Type
.empty())
5884 W
.printString(N
.Type
, N
.Value
);
5885 } else if (Name
== "AMDGPU") {
5886 const AMDGPUNote N
= getAMDGPUNote
<ELFT
>(Type
, Descriptor
);
5887 if (!N
.Type
.empty())
5888 W
.printString(N
.Type
, N
.Value
);
5889 } else if (Name
== "CORE") {
5890 if (Type
== ELF::NT_FILE
) {
5891 DataExtractor
DescExtractor(Descriptor
,
5892 ELFT::TargetEndianness
== support::little
,
5894 Expected
<CoreNote
> Note
= readCoreNote(DescExtractor
);
5896 printCoreNoteLLVMStyle(*Note
, W
);
5898 reportWarning(Note
.takeError(), this->FileName
);
5900 } else if (!Descriptor
.empty()) {
5901 W
.printBinaryBlock("Description data", Descriptor
);
5905 ArrayRef
<Elf_Shdr
> Sections
= unwrapOrError(this->FileName
, Obj
->sections());
5906 if (Obj
->getHeader()->e_type
!= ELF::ET_CORE
&& !Sections
.empty()) {
5907 for (const auto &S
: Sections
) {
5908 if (S
.sh_type
!= SHT_NOTE
)
5910 DictScope
D(W
, "NoteSection");
5911 PrintHeader(S
.sh_offset
, S
.sh_size
);
5912 Error Err
= Error::success();
5913 for (const auto &Note
: Obj
->notes(S
, Err
))
5916 reportError(std::move(Err
), this->FileName
);
5919 for (const auto &P
:
5920 unwrapOrError(this->FileName
, Obj
->program_headers())) {
5921 if (P
.p_type
!= PT_NOTE
)
5923 DictScope
D(W
, "NoteSection");
5924 PrintHeader(P
.p_offset
, P
.p_filesz
);
5925 Error Err
= Error::success();
5926 for (const auto &Note
: Obj
->notes(P
, Err
))
5929 reportError(std::move(Err
), this->FileName
);
5934 template <class ELFT
>
5935 void LLVMStyle
<ELFT
>::printELFLinkerOptions(const ELFFile
<ELFT
> *Obj
) {
5936 ListScope
L(W
, "LinkerOptions");
5938 for (const Elf_Shdr
&Shdr
: unwrapOrError(this->FileName
, Obj
->sections())) {
5939 if (Shdr
.sh_type
!= ELF::SHT_LLVM_LINKER_OPTIONS
)
5942 ArrayRef
<uint8_t> Contents
=
5943 unwrapOrError(this->FileName
, Obj
->getSectionContents(&Shdr
));
5944 for (const uint8_t *P
= Contents
.begin(), *E
= Contents
.end(); P
< E
; ) {
5945 StringRef Key
= StringRef(reinterpret_cast<const char *>(P
));
5947 StringRef(reinterpret_cast<const char *>(P
) + Key
.size() + 1);
5949 W
.printString(Key
, Value
);
5951 P
= P
+ Key
.size() + Value
.size() + 2;
5956 template <class ELFT
>
5957 void LLVMStyle
<ELFT
>::printStackSizes(const ELFObjectFile
<ELFT
> *Obj
) {
5958 ListScope
L(W
, "StackSizes");
5959 if (Obj
->isRelocatableObject())
5960 this->printRelocatableStackSizes(Obj
, []() {});
5962 this->printNonRelocatableStackSizes(Obj
, []() {});
5965 template <class ELFT
>
5966 void LLVMStyle
<ELFT
>::printStackSizeEntry(uint64_t Size
, StringRef FuncName
) {
5967 DictScope
D(W
, "Entry");
5968 W
.printString("Function", FuncName
);
5969 W
.printHex("Size", Size
);
5972 template <class ELFT
>
5973 void LLVMStyle
<ELFT
>::printMipsGOT(const MipsGOTParser
<ELFT
> &Parser
) {
5974 auto PrintEntry
= [&](const Elf_Addr
*E
) {
5975 W
.printHex("Address", Parser
.getGotAddress(E
));
5976 W
.printNumber("Access", Parser
.getGotOffset(E
));
5977 W
.printHex("Initial", *E
);
5980 DictScope
GS(W
, Parser
.IsStatic
? "Static GOT" : "Primary GOT");
5982 W
.printHex("Canonical gp value", Parser
.getGp());
5984 ListScope
RS(W
, "Reserved entries");
5986 DictScope
D(W
, "Entry");
5987 PrintEntry(Parser
.getGotLazyResolver());
5988 W
.printString("Purpose", StringRef("Lazy resolver"));
5991 if (Parser
.getGotModulePointer()) {
5992 DictScope
D(W
, "Entry");
5993 PrintEntry(Parser
.getGotModulePointer());
5994 W
.printString("Purpose", StringRef("Module pointer (GNU extension)"));
5998 ListScope
LS(W
, "Local entries");
5999 for (auto &E
: Parser
.getLocalEntries()) {
6000 DictScope
D(W
, "Entry");
6005 if (Parser
.IsStatic
)
6009 ListScope
GS(W
, "Global entries");
6010 for (auto &E
: Parser
.getGlobalEntries()) {
6011 DictScope
D(W
, "Entry");
6015 const Elf_Sym
*Sym
= Parser
.getGotSym(&E
);
6016 W
.printHex("Value", Sym
->st_value
);
6017 W
.printEnum("Type", Sym
->getType(), makeArrayRef(ElfSymbolTypes
));
6019 unsigned SectionIndex
= 0;
6020 StringRef SectionName
;
6021 this->dumper()->getSectionNameIndex(
6022 Sym
, this->dumper()->dynamic_symbols().begin(), SectionName
,
6024 W
.printHex("Section", SectionName
, SectionIndex
);
6026 std::string SymName
= this->dumper()->getFullSymbolName(
6027 Sym
, this->dumper()->getDynamicStringTable(), true);
6028 W
.printNumber("Name", SymName
, Sym
->st_name
);
6032 W
.printNumber("Number of TLS and multi-GOT entries",
6033 uint64_t(Parser
.getOtherEntries().size()));
6036 template <class ELFT
>
6037 void LLVMStyle
<ELFT
>::printMipsPLT(const MipsGOTParser
<ELFT
> &Parser
) {
6038 auto PrintEntry
= [&](const Elf_Addr
*E
) {
6039 W
.printHex("Address", Parser
.getPltAddress(E
));
6040 W
.printHex("Initial", *E
);
6043 DictScope
GS(W
, "PLT GOT");
6046 ListScope
RS(W
, "Reserved entries");
6048 DictScope
D(W
, "Entry");
6049 PrintEntry(Parser
.getPltLazyResolver());
6050 W
.printString("Purpose", StringRef("PLT lazy resolver"));
6053 if (auto E
= Parser
.getPltModulePointer()) {
6054 DictScope
D(W
, "Entry");
6056 W
.printString("Purpose", StringRef("Module pointer"));
6060 ListScope
LS(W
, "Entries");
6061 for (auto &E
: Parser
.getPltEntries()) {
6062 DictScope
D(W
, "Entry");
6065 const Elf_Sym
*Sym
= Parser
.getPltSym(&E
);
6066 W
.printHex("Value", Sym
->st_value
);
6067 W
.printEnum("Type", Sym
->getType(), makeArrayRef(ElfSymbolTypes
));
6069 unsigned SectionIndex
= 0;
6070 StringRef SectionName
;
6071 this->dumper()->getSectionNameIndex(
6072 Sym
, this->dumper()->dynamic_symbols().begin(), SectionName
,
6074 W
.printHex("Section", SectionName
, SectionIndex
);
6076 std::string SymName
=
6077 this->dumper()->getFullSymbolName(Sym
, Parser
.getPltStrTable(), true);
6078 W
.printNumber("Name", SymName
, Sym
->st_name
);
6083 template <class ELFT
>
6084 void LLVMStyle
<ELFT
>::printMipsABIFlags(const ELFObjectFile
<ELFT
> *ObjF
) {
6085 const ELFFile
<ELFT
> *Obj
= ObjF
->getELFFile();
6086 const Elf_Shdr
*Shdr
=
6087 findSectionByName(*Obj
, ObjF
->getFileName(), ".MIPS.abiflags");
6089 W
.startLine() << "There is no .MIPS.abiflags section in the file.\n";
6092 ArrayRef
<uint8_t> Sec
=
6093 unwrapOrError(ObjF
->getFileName(), Obj
->getSectionContents(Shdr
));
6094 if (Sec
.size() != sizeof(Elf_Mips_ABIFlags
<ELFT
>)) {
6095 W
.startLine() << "The .MIPS.abiflags section has a wrong size.\n";
6099 auto *Flags
= reinterpret_cast<const Elf_Mips_ABIFlags
<ELFT
> *>(Sec
.data());
6101 raw_ostream
&OS
= W
.getOStream();
6102 DictScope
GS(W
, "MIPS ABI Flags");
6104 W
.printNumber("Version", Flags
->version
);
6105 W
.startLine() << "ISA: ";
6106 if (Flags
->isa_rev
<= 1)
6107 OS
<< format("MIPS%u", Flags
->isa_level
);
6109 OS
<< format("MIPS%ur%u", Flags
->isa_level
, Flags
->isa_rev
);
6111 W
.printEnum("ISA Extension", Flags
->isa_ext
, makeArrayRef(ElfMipsISAExtType
));
6112 W
.printFlags("ASEs", Flags
->ases
, makeArrayRef(ElfMipsASEFlags
));
6113 W
.printEnum("FP ABI", Flags
->fp_abi
, makeArrayRef(ElfMipsFpABIType
));
6114 W
.printNumber("GPR size", getMipsRegisterSize(Flags
->gpr_size
));
6115 W
.printNumber("CPR1 size", getMipsRegisterSize(Flags
->cpr1_size
));
6116 W
.printNumber("CPR2 size", getMipsRegisterSize(Flags
->cpr2_size
));
6117 W
.printFlags("Flags 1", Flags
->flags1
, makeArrayRef(ElfMipsFlags1
));
6118 W
.printHex("Flags 2", Flags
->flags2
);