Bug Summary

File:build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/tools/llvm-readobj/ELFDumper.cpp
Warning:line 5886, column 9
4th function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name ELFDumper.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm -resource-dir /usr/lib/llvm-16/lib/clang/16.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/llvm-readobj -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/tools/llvm-readobj -I include -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-09-04-125545-48738-1 -x c++ /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/tools/llvm-readobj/ELFDumper.cpp
1//===- ELFDumper.cpp - ELF-specific dumper --------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements the ELF-specific dumper for llvm-readobj.
11///
12//===----------------------------------------------------------------------===//
13
14#include "ARMEHABIPrinter.h"
15#include "DwarfCFIEHPrinter.h"
16#include "ObjDumper.h"
17#include "StackMapPrinter.h"
18#include "llvm-readobj.h"
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/BitVector.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/DenseSet.h"
23#include "llvm/ADT/MapVector.h"
24#include "llvm/ADT/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/BinaryFormat/MsgPackDocument.h"
35#include "llvm/Demangle/Demangle.h"
36#include "llvm/Object/Archive.h"
37#include "llvm/Object/ELF.h"
38#include "llvm/Object/ELFObjectFile.h"
39#include "llvm/Object/ELFTypes.h"
40#include "llvm/Object/Error.h"
41#include "llvm/Object/ObjectFile.h"
42#include "llvm/Object/RelocationResolver.h"
43#include "llvm/Object/StackMapParser.h"
44#include "llvm/Support/AMDGPUMetadata.h"
45#include "llvm/Support/ARMAttributeParser.h"
46#include "llvm/Support/ARMBuildAttributes.h"
47#include "llvm/Support/Casting.h"
48#include "llvm/Support/Compiler.h"
49#include "llvm/Support/Endian.h"
50#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/Format.h"
52#include "llvm/Support/FormatVariadic.h"
53#include "llvm/Support/FormattedStream.h"
54#include "llvm/Support/LEB128.h"
55#include "llvm/Support/MSP430AttributeParser.h"
56#include "llvm/Support/MSP430Attributes.h"
57#include "llvm/Support/MathExtras.h"
58#include "llvm/Support/MipsABIFlags.h"
59#include "llvm/Support/RISCVAttributeParser.h"
60#include "llvm/Support/RISCVAttributes.h"
61#include "llvm/Support/ScopedPrinter.h"
62#include "llvm/Support/raw_ostream.h"
63#include <algorithm>
64#include <cinttypes>
65#include <cstddef>
66#include <cstdint>
67#include <cstdlib>
68#include <iterator>
69#include <memory>
70#include <string>
71#include <system_error>
72#include <vector>
73
74using namespace llvm;
75using namespace llvm::object;
76using namespace ELF;
77
78#define LLVM_READOBJ_ENUM_CASE(ns, enum)case ns::enum: return "enum"; \
79 case ns::enum: \
80 return #enum;
81
82#define ENUM_ENT(enum, altName){ "enum", altName, ELF::enum } \
83 { #enum, altName, ELF::enum }
84
85#define ENUM_ENT_1(enum){ "enum", "enum", ELF::enum } \
86 { #enum, #enum, ELF::enum }
87
88namespace {
89
90template <class ELFT> struct RelSymbol {
91 RelSymbol(const typename ELFT::Sym *S, StringRef N)
92 : Sym(S), Name(N.str()) {}
93 const typename ELFT::Sym *Sym;
94 std::string Name;
95};
96
97/// Represents a contiguous uniform range in the file. We cannot just create a
98/// range directly because when creating one of these from the .dynamic table
99/// the size, entity size and virtual address are different entries in arbitrary
100/// order (DT_REL, DT_RELSZ, DT_RELENT for example).
101struct DynRegionInfo {
102 DynRegionInfo(const Binary &Owner, const ObjDumper &D)
103 : Obj(&Owner), Dumper(&D) {}
104 DynRegionInfo(const Binary &Owner, const ObjDumper &D, const uint8_t *A,
105 uint64_t S, uint64_t ES)
106 : Addr(A), Size(S), EntSize(ES), Obj(&Owner), Dumper(&D) {}
107
108 /// Address in current address space.
109 const uint8_t *Addr = nullptr;
110 /// Size in bytes of the region.
111 uint64_t Size = 0;
112 /// Size of each entity in the region.
113 uint64_t EntSize = 0;
114
115 /// Owner object. Used for error reporting.
116 const Binary *Obj;
117 /// Dumper used for error reporting.
118 const ObjDumper *Dumper;
119 /// Error prefix. Used for error reporting to provide more information.
120 std::string Context;
121 /// Region size name. Used for error reporting.
122 StringRef SizePrintName = "size";
123 /// Entry size name. Used for error reporting. If this field is empty, errors
124 /// will not mention the entry size.
125 StringRef EntSizePrintName = "entry size";
126
127 template <typename Type> ArrayRef<Type> getAsArrayRef() const {
128 const Type *Start = reinterpret_cast<const Type *>(Addr);
129 if (!Start)
130 return {Start, Start};
131
132 const uint64_t Offset =
133 Addr - (const uint8_t *)Obj->getMemoryBufferRef().getBufferStart();
134 const uint64_t ObjSize = Obj->getMemoryBufferRef().getBufferSize();
135
136 if (Size > ObjSize - Offset) {
137 Dumper->reportUniqueWarning(
138 "unable to read data at 0x" + Twine::utohexstr(Offset) +
139 " of size 0x" + Twine::utohexstr(Size) + " (" + SizePrintName +
140 "): it goes past the end of the file of size 0x" +
141 Twine::utohexstr(ObjSize));
142 return {Start, Start};
143 }
144
145 if (EntSize == sizeof(Type) && (Size % EntSize == 0))
146 return {Start, Start + (Size / EntSize)};
147
148 std::string Msg;
149 if (!Context.empty())
150 Msg += Context + " has ";
151
152 Msg += ("invalid " + SizePrintName + " (0x" + Twine::utohexstr(Size) + ")")
153 .str();
154 if (!EntSizePrintName.empty())
155 Msg +=
156 (" or " + EntSizePrintName + " (0x" + Twine::utohexstr(EntSize) + ")")
157 .str();
158
159 Dumper->reportUniqueWarning(Msg);
160 return {Start, Start};
161 }
162};
163
164struct GroupMember {
165 StringRef Name;
166 uint64_t Index;
167};
168
169struct GroupSection {
170 StringRef Name;
171 std::string Signature;
172 uint64_t ShName;
173 uint64_t Index;
174 uint32_t Link;
175 uint32_t Info;
176 uint32_t Type;
177 std::vector<GroupMember> Members;
178};
179
180namespace {
181
182struct NoteType {
183 uint32_t ID;
184 StringRef Name;
185};
186
187} // namespace
188
189template <class ELFT> class Relocation {
190public:
191 Relocation(const typename ELFT::Rel &R, bool IsMips64EL)
192 : Type(R.getType(IsMips64EL)), Symbol(R.getSymbol(IsMips64EL)),
193 Offset(R.r_offset), Info(R.r_info) {}
194
195 Relocation(const typename ELFT::Rela &R, bool IsMips64EL)
196 : Relocation((const typename ELFT::Rel &)R, IsMips64EL) {
197 Addend = R.r_addend;
198 }
199
200 uint32_t Type;
201 uint32_t Symbol;
202 typename ELFT::uint Offset;
203 typename ELFT::uint Info;
204 Optional<int64_t> Addend;
205};
206
207template <class ELFT> class MipsGOTParser;
208
209template <typename ELFT> class ELFDumper : public ObjDumper {
210 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)using Elf_Addr = typename ELFT::Addr; using Elf_Off = typename
ELFT::Off; using Elf_Half = typename ELFT::Half; using Elf_Word
= typename ELFT::Word; using Elf_Sword = typename ELFT::Sword
; using Elf_Xword = typename ELFT::Xword; using Elf_Sxword = typename
ELFT::Sxword; using uintX_t = typename ELFT::uint; using Elf_Ehdr
= typename ELFT::Ehdr; using Elf_Shdr = typename ELFT::Shdr;
using Elf_Sym = typename ELFT::Sym; using Elf_Dyn = typename
ELFT::Dyn; using Elf_Phdr = typename ELFT::Phdr; using Elf_Rel
= typename ELFT::Rel; using Elf_Rela = typename ELFT::Rela; using
Elf_Relr = typename ELFT::Relr; using Elf_Verdef = typename ELFT
::Verdef; using Elf_Verdaux = typename ELFT::Verdaux; using Elf_Verneed
= typename ELFT::Verneed; using Elf_Vernaux = typename ELFT::
Vernaux; using Elf_Versym = typename ELFT::Versym; using Elf_Hash
= typename ELFT::Hash; using Elf_GnuHash = typename ELFT::GnuHash
; using Elf_Nhdr = typename ELFT::Nhdr; using Elf_Note = typename
ELFT::Note; using Elf_Note_Iterator = typename ELFT::NoteIterator
; using Elf_CGProfile = typename ELFT::CGProfile; using Elf_Dyn_Range
= typename ELFT::DynRange; using Elf_Shdr_Range = typename ELFT
::ShdrRange; using Elf_Sym_Range = typename ELFT::SymRange; using
Elf_Rel_Range = typename ELFT::RelRange; using Elf_Rela_Range
= typename ELFT::RelaRange; using Elf_Relr_Range = typename ELFT
::RelrRange; using Elf_Phdr_Range = typename ELFT::PhdrRange;
211
212public:
213 ELFDumper(const object::ELFObjectFile<ELFT> &ObjF, ScopedPrinter &Writer);
214
215 void printUnwindInfo() override;
216 void printNeededLibraries() override;
217 void printHashTable() override;
218 void printGnuHashTable() override;
219 void printLoadName() override;
220 void printVersionInfo() override;
221 void printArchSpecificInfo() override;
222 void printStackMap() const override;
223
224 const object::ELFObjectFile<ELFT> &getElfObject() const { return ObjF; };
225
226 std::string describe(const Elf_Shdr &Sec) const;
227
228 unsigned getHashTableEntSize() const {
229 // EM_S390 and ELF::EM_ALPHA platforms use 8-bytes entries in SHT_HASH
230 // sections. This violates the ELF specification.
231 if (Obj.getHeader().e_machine == ELF::EM_S390 ||
232 Obj.getHeader().e_machine == ELF::EM_ALPHA)
233 return 8;
234 return 4;
235 }
236
237 Elf_Dyn_Range dynamic_table() const {
238 // A valid .dynamic section contains an array of entries terminated
239 // with a DT_NULL entry. However, sometimes the section content may
240 // continue past the DT_NULL entry, so to dump the section correctly,
241 // we first find the end of the entries by iterating over them.
242 Elf_Dyn_Range Table = DynamicTable.template getAsArrayRef<Elf_Dyn>();
243
244 size_t Size = 0;
245 while (Size < Table.size())
246 if (Table[Size++].getTag() == DT_NULL)
247 break;
248
249 return Table.slice(0, Size);
250 }
251
252 Elf_Sym_Range dynamic_symbols() const {
253 if (!DynSymRegion)
254 return Elf_Sym_Range();
255 return DynSymRegion->template getAsArrayRef<Elf_Sym>();
256 }
257
258 const Elf_Shdr *findSectionByName(StringRef Name) const;
259
260 StringRef getDynamicStringTable() const { return DynamicStringTable; }
261
262protected:
263 virtual void printVersionSymbolSection(const Elf_Shdr *Sec) = 0;
264 virtual void printVersionDefinitionSection(const Elf_Shdr *Sec) = 0;
265 virtual void printVersionDependencySection(const Elf_Shdr *Sec) = 0;
266
267 void
268 printDependentLibsHelper(function_ref<void(const Elf_Shdr &)> OnSectionStart,
269 function_ref<void(StringRef, uint64_t)> OnLibEntry);
270
271 virtual void printRelRelaReloc(const Relocation<ELFT> &R,
272 const RelSymbol<ELFT> &RelSym) = 0;
273 virtual void printRelrReloc(const Elf_Relr &R) = 0;
274 virtual void printDynamicRelocHeader(unsigned Type, StringRef Name,
275 const DynRegionInfo &Reg) {}
276 void printReloc(const Relocation<ELFT> &R, unsigned RelIndex,
277 const Elf_Shdr &Sec, const Elf_Shdr *SymTab);
278 void printDynamicReloc(const Relocation<ELFT> &R);
279 void printDynamicRelocationsHelper();
280 void printRelocationsHelper(const Elf_Shdr &Sec);
281 void forEachRelocationDo(
282 const Elf_Shdr &Sec, bool RawRelr,
283 llvm::function_ref<void(const Relocation<ELFT> &, unsigned,
284 const Elf_Shdr &, const Elf_Shdr *)>
285 RelRelaFn,
286 llvm::function_ref<void(const Elf_Relr &)> RelrFn);
287
288 virtual void printSymtabMessage(const Elf_Shdr *Symtab, size_t Offset,
289 bool NonVisibilityBitsUsed) const {};
290 virtual void printSymbol(const Elf_Sym &Symbol, unsigned SymIndex,
291 DataRegion<Elf_Word> ShndxTable,
292 Optional<StringRef> StrTable, bool IsDynamic,
293 bool NonVisibilityBitsUsed) const = 0;
294
295 virtual void printMipsABIFlags() = 0;
296 virtual void printMipsGOT(const MipsGOTParser<ELFT> &Parser) = 0;
297 virtual void printMipsPLT(const MipsGOTParser<ELFT> &Parser) = 0;
298
299 Expected<ArrayRef<Elf_Versym>>
300 getVersionTable(const Elf_Shdr &Sec, ArrayRef<Elf_Sym> *SymTab,
301 StringRef *StrTab, const Elf_Shdr **SymTabSec) const;
302 StringRef getPrintableSectionName(const Elf_Shdr &Sec) const;
303
304 std::vector<GroupSection> getGroups();
305
306 // Returns the function symbol index for the given address. Matches the
307 // symbol's section with FunctionSec when specified.
308 // Returns None if no function symbol can be found for the address or in case
309 // it is not defined in the specified section.
310 SmallVector<uint32_t>
311 getSymbolIndexesForFunctionAddress(uint64_t SymValue,
312 Optional<const Elf_Shdr *> FunctionSec);
313 bool printFunctionStackSize(uint64_t SymValue,
314 Optional<const Elf_Shdr *> FunctionSec,
315 const Elf_Shdr &StackSizeSec, DataExtractor Data,
316 uint64_t *Offset);
317 void printStackSize(const Relocation<ELFT> &R, const Elf_Shdr &RelocSec,
318 unsigned Ndx, const Elf_Shdr *SymTab,
319 const Elf_Shdr *FunctionSec, const Elf_Shdr &StackSizeSec,
320 const RelocationResolver &Resolver, DataExtractor Data);
321 virtual void printStackSizeEntry(uint64_t Size,
322 ArrayRef<std::string> FuncNames) = 0;
323
324 void printRelocatableStackSizes(std::function<void()> PrintHeader);
325 void printNonRelocatableStackSizes(std::function<void()> PrintHeader);
326
327 /// Retrieves sections with corresponding relocation sections based on
328 /// IsMatch.
329 void getSectionAndRelocations(
330 std::function<bool(const Elf_Shdr &)> IsMatch,
331 llvm::MapVector<const Elf_Shdr *, const Elf_Shdr *> &SecToRelocMap);
332
333 const object::ELFObjectFile<ELFT> &ObjF;
334 const ELFFile<ELFT> &Obj;
335 StringRef FileName;
336
337 Expected<DynRegionInfo> createDRI(uint64_t Offset, uint64_t Size,
338 uint64_t EntSize) {
339 if (Offset + Size < Offset || Offset + Size > Obj.getBufSize())
340 return createError("offset (0x" + Twine::utohexstr(Offset) +
341 ") + size (0x" + Twine::utohexstr(Size) +
342 ") is greater than the file size (0x" +
343 Twine::utohexstr(Obj.getBufSize()) + ")");
344 return DynRegionInfo(ObjF, *this, Obj.base() + Offset, Size, EntSize);
345 }
346
347 void printAttributes(unsigned, std::unique_ptr<ELFAttributeParser>,
348 support::endianness);
349 void printMipsReginfo();
350 void printMipsOptions();
351
352 std::pair<const Elf_Phdr *, const Elf_Shdr *> findDynamic();
353 void loadDynamicTable();
354 void parseDynamicTable();
355
356 Expected<StringRef> getSymbolVersion(const Elf_Sym &Sym,
357 bool &IsDefault) const;
358 Expected<SmallVector<Optional<VersionEntry>, 0> *> getVersionMap() const;
359
360 DynRegionInfo DynRelRegion;
361 DynRegionInfo DynRelaRegion;
362 DynRegionInfo DynRelrRegion;
363 DynRegionInfo DynPLTRelRegion;
364 Optional<DynRegionInfo> DynSymRegion;
365 DynRegionInfo DynSymTabShndxRegion;
366 DynRegionInfo DynamicTable;
367 StringRef DynamicStringTable;
368 const Elf_Hash *HashTable = nullptr;
369 const Elf_GnuHash *GnuHashTable = nullptr;
370 const Elf_Shdr *DotSymtabSec = nullptr;
371 const Elf_Shdr *DotDynsymSec = nullptr;
372 const Elf_Shdr *DotAddrsigSec = nullptr;
373 DenseMap<const Elf_Shdr *, ArrayRef<Elf_Word>> ShndxTables;
374 Optional<uint64_t> SONameOffset;
375 Optional<DenseMap<uint64_t, std::vector<uint32_t>>> AddressToIndexMap;
376
377 const Elf_Shdr *SymbolVersionSection = nullptr; // .gnu.version
378 const Elf_Shdr *SymbolVersionNeedSection = nullptr; // .gnu.version_r
379 const Elf_Shdr *SymbolVersionDefSection = nullptr; // .gnu.version_d
380
381 std::string getFullSymbolName(const Elf_Sym &Symbol, unsigned SymIndex,
382 DataRegion<Elf_Word> ShndxTable,
383 Optional<StringRef> StrTable,
384 bool IsDynamic) const;
385 Expected<unsigned>
386 getSymbolSectionIndex(const Elf_Sym &Symbol, unsigned SymIndex,
387 DataRegion<Elf_Word> ShndxTable) const;
388 Expected<StringRef> getSymbolSectionName(const Elf_Sym &Symbol,
389 unsigned SectionIndex) const;
390 std::string getStaticSymbolName(uint32_t Index) const;
391 StringRef getDynamicString(uint64_t Value) const;
392
393 void printSymbolsHelper(bool IsDynamic) const;
394 std::string getDynamicEntry(uint64_t Type, uint64_t Value) const;
395
396 Expected<RelSymbol<ELFT>> getRelocationTarget(const Relocation<ELFT> &R,
397 const Elf_Shdr *SymTab) const;
398
399 ArrayRef<Elf_Word> getShndxTable(const Elf_Shdr *Symtab) const;
400
401private:
402 mutable SmallVector<Optional<VersionEntry>, 0> VersionMap;
403};
404
405template <class ELFT>
406std::string ELFDumper<ELFT>::describe(const Elf_Shdr &Sec) const {
407 return ::describe(Obj, Sec);
408}
409
410namespace {
411
412template <class ELFT> struct SymtabLink {
413 typename ELFT::SymRange Symbols;
414 StringRef StringTable;
415 const typename ELFT::Shdr *SymTab;
416};
417
418// Returns the linked symbol table, symbols and associated string table for a
419// given section.
420template <class ELFT>
421Expected<SymtabLink<ELFT>> getLinkAsSymtab(const ELFFile<ELFT> &Obj,
422 const typename ELFT::Shdr &Sec,
423 unsigned ExpectedType) {
424 Expected<const typename ELFT::Shdr *> SymtabOrErr =
425 Obj.getSection(Sec.sh_link);
426 if (!SymtabOrErr)
427 return createError("invalid section linked to " + describe(Obj, Sec) +
428 ": " + toString(SymtabOrErr.takeError()));
429
430 if ((*SymtabOrErr)->sh_type != ExpectedType)
431 return createError(
432 "invalid section linked to " + describe(Obj, Sec) + ": expected " +
433 object::getELFSectionTypeName(Obj.getHeader().e_machine, ExpectedType) +
434 ", but got " +
435 object::getELFSectionTypeName(Obj.getHeader().e_machine,
436 (*SymtabOrErr)->sh_type));
437
438 Expected<StringRef> StrTabOrErr = Obj.getLinkAsStrtab(**SymtabOrErr);
439 if (!StrTabOrErr)
440 return createError(
441 "can't get a string table for the symbol table linked to " +
442 describe(Obj, Sec) + ": " + toString(StrTabOrErr.takeError()));
443
444 Expected<typename ELFT::SymRange> SymsOrErr = Obj.symbols(*SymtabOrErr);
445 if (!SymsOrErr)
446 return createError("unable to read symbols from the " + describe(Obj, Sec) +
447 ": " + toString(SymsOrErr.takeError()));
448
449 return SymtabLink<ELFT>{*SymsOrErr, *StrTabOrErr, *SymtabOrErr};
450}
451
452} // namespace
453
454template <class ELFT>
455Expected<ArrayRef<typename ELFT::Versym>>
456ELFDumper<ELFT>::getVersionTable(const Elf_Shdr &Sec, ArrayRef<Elf_Sym> *SymTab,
457 StringRef *StrTab,
458 const Elf_Shdr **SymTabSec) const {
459 assert((!SymTab && !StrTab && !SymTabSec) || (SymTab && StrTab && SymTabSec))(static_cast <bool> ((!SymTab && !StrTab &&
!SymTabSec) || (SymTab && StrTab && SymTabSec
)) ? void (0) : __assert_fail ("(!SymTab && !StrTab && !SymTabSec) || (SymTab && StrTab && SymTabSec)"
, "llvm/tools/llvm-readobj/ELFDumper.cpp", 459, __extension__
__PRETTY_FUNCTION__))
;
460 if (reinterpret_cast<uintptr_t>(Obj.base() + Sec.sh_offset) %
461 sizeof(uint16_t) !=
462 0)
463 return createError("the " + describe(Sec) + " is misaligned");
464
465 Expected<ArrayRef<Elf_Versym>> VersionsOrErr =
466 Obj.template getSectionContentsAsArray<Elf_Versym>(Sec);
467 if (!VersionsOrErr)
468 return createError("cannot read content of " + describe(Sec) + ": " +
469 toString(VersionsOrErr.takeError()));
470
471 Expected<SymtabLink<ELFT>> SymTabOrErr =
472 getLinkAsSymtab(Obj, Sec, SHT_DYNSYM);
473 if (!SymTabOrErr) {
474 reportUniqueWarning(SymTabOrErr.takeError());
475 return *VersionsOrErr;
476 }
477
478 if (SymTabOrErr->Symbols.size() != VersionsOrErr->size())
479 reportUniqueWarning(describe(Sec) + ": the number of entries (" +
480 Twine(VersionsOrErr->size()) +
481 ") does not match the number of symbols (" +
482 Twine(SymTabOrErr->Symbols.size()) +
483 ") in the symbol table with index " +
484 Twine(Sec.sh_link));
485
486 if (SymTab) {
487 *SymTab = SymTabOrErr->Symbols;
488 *StrTab = SymTabOrErr->StringTable;
489 *SymTabSec = SymTabOrErr->SymTab;
490 }
491 return *VersionsOrErr;
492}
493
494template <class ELFT>
495void ELFDumper<ELFT>::printSymbolsHelper(bool IsDynamic) const {
496 Optional<StringRef> StrTable;
497 size_t Entries = 0;
498 Elf_Sym_Range Syms(nullptr, nullptr);
499 const Elf_Shdr *SymtabSec = IsDynamic ? DotDynsymSec : DotSymtabSec;
500
501 if (IsDynamic) {
502 StrTable = DynamicStringTable;
503 Syms = dynamic_symbols();
504 Entries = Syms.size();
505 } else if (DotSymtabSec) {
506 if (Expected<StringRef> StrTableOrErr =
507 Obj.getStringTableForSymtab(*DotSymtabSec))
508 StrTable = *StrTableOrErr;
509 else
510 reportUniqueWarning(
511 "unable to get the string table for the SHT_SYMTAB section: " +
512 toString(StrTableOrErr.takeError()));
513
514 if (Expected<Elf_Sym_Range> SymsOrErr = Obj.symbols(DotSymtabSec))
515 Syms = *SymsOrErr;
516 else
517 reportUniqueWarning(
518 "unable to read symbols from the SHT_SYMTAB section: " +
519 toString(SymsOrErr.takeError()));
520 Entries = DotSymtabSec->getEntityCount();
521 }
522 if (Syms.empty())
523 return;
524
525 // The st_other field has 2 logical parts. The first two bits hold the symbol
526 // visibility (STV_*) and the remainder hold other platform-specific values.
527 bool NonVisibilityBitsUsed =
528 llvm::any_of(Syms, [](const Elf_Sym &S) { return S.st_other & ~0x3; });
529
530 DataRegion<Elf_Word> ShndxTable =
531 IsDynamic ? DataRegion<Elf_Word>(
532 (const Elf_Word *)this->DynSymTabShndxRegion.Addr,
533 this->getElfObject().getELFFile().end())
534 : DataRegion<Elf_Word>(this->getShndxTable(SymtabSec));
535
536 printSymtabMessage(SymtabSec, Entries, NonVisibilityBitsUsed);
537 for (const Elf_Sym &Sym : Syms)
538 printSymbol(Sym, &Sym - Syms.begin(), ShndxTable, StrTable, IsDynamic,
539 NonVisibilityBitsUsed);
540}
541
542template <typename ELFT> class GNUELFDumper : public ELFDumper<ELFT> {
543 formatted_raw_ostream &OS;
544
545public:
546 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)using Elf_Addr = typename ELFT::Addr; using Elf_Off = typename
ELFT::Off; using Elf_Half = typename ELFT::Half; using Elf_Word
= typename ELFT::Word; using Elf_Sword = typename ELFT::Sword
; using Elf_Xword = typename ELFT::Xword; using Elf_Sxword = typename
ELFT::Sxword; using uintX_t = typename ELFT::uint; using Elf_Ehdr
= typename ELFT::Ehdr; using Elf_Shdr = typename ELFT::Shdr;
using Elf_Sym = typename ELFT::Sym; using Elf_Dyn = typename
ELFT::Dyn; using Elf_Phdr = typename ELFT::Phdr; using Elf_Rel
= typename ELFT::Rel; using Elf_Rela = typename ELFT::Rela; using
Elf_Relr = typename ELFT::Relr; using Elf_Verdef = typename ELFT
::Verdef; using Elf_Verdaux = typename ELFT::Verdaux; using Elf_Verneed
= typename ELFT::Verneed; using Elf_Vernaux = typename ELFT::
Vernaux; using Elf_Versym = typename ELFT::Versym; using Elf_Hash
= typename ELFT::Hash; using Elf_GnuHash = typename ELFT::GnuHash
; using Elf_Nhdr = typename ELFT::Nhdr; using Elf_Note = typename
ELFT::Note; using Elf_Note_Iterator = typename ELFT::NoteIterator
; using Elf_CGProfile = typename ELFT::CGProfile; using Elf_Dyn_Range
= typename ELFT::DynRange; using Elf_Shdr_Range = typename ELFT
::ShdrRange; using Elf_Sym_Range = typename ELFT::SymRange; using
Elf_Rel_Range = typename ELFT::RelRange; using Elf_Rela_Range
= typename ELFT::RelaRange; using Elf_Relr_Range = typename ELFT
::RelrRange; using Elf_Phdr_Range = typename ELFT::PhdrRange;
547
548 GNUELFDumper(const object::ELFObjectFile<ELFT> &ObjF, ScopedPrinter &Writer)
549 : ELFDumper<ELFT>(ObjF, Writer),
550 OS(static_cast<formatted_raw_ostream &>(Writer.getOStream())) {
551 assert(&this->W.getOStream() == &llvm::fouts())(static_cast <bool> (&this->W.getOStream() == &
llvm::fouts()) ? void (0) : __assert_fail ("&this->W.getOStream() == &llvm::fouts()"
, "llvm/tools/llvm-readobj/ELFDumper.cpp", 551, __extension__
__PRETTY_FUNCTION__))
;
552 }
553
554 void printFileSummary(StringRef FileStr, ObjectFile &Obj,
555 ArrayRef<std::string> InputFilenames,
556 const Archive *A) override;
557 void printFileHeaders() override;
558 void printGroupSections() override;
559 void printRelocations() override;
560 void printSectionHeaders() override;
561 void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols) override;
562 void printHashSymbols() override;
563 void printSectionDetails() override;
564 void printDependentLibs() override;
565 void printDynamicTable() override;
566 void printDynamicRelocations() override;
567 void printSymtabMessage(const Elf_Shdr *Symtab, size_t Offset,
568 bool NonVisibilityBitsUsed) const override;
569 void printProgramHeaders(bool PrintProgramHeaders,
570 cl::boolOrDefault PrintSectionMapping) override;
571 void printVersionSymbolSection(const Elf_Shdr *Sec) override;
572 void printVersionDefinitionSection(const Elf_Shdr *Sec) override;
573 void printVersionDependencySection(const Elf_Shdr *Sec) override;
574 void printHashHistograms() override;
575 void printCGProfile() override;
576 void printBBAddrMaps() override;
577 void printAddrsig() override;
578 void printNotes() override;
579 void printELFLinkerOptions() override;
580 void printStackSizes() override;
581
582private:
583 void printHashHistogram(const Elf_Hash &HashTable);
584 void printGnuHashHistogram(const Elf_GnuHash &GnuHashTable);
585 void printHashTableSymbols(const Elf_Hash &HashTable);
586 void printGnuHashTableSymbols(const Elf_GnuHash &GnuHashTable);
587
588 struct Field {
589 std::string Str;
590 unsigned Column;
591
592 Field(StringRef S, unsigned Col) : Str(std::string(S)), Column(Col) {}
593 Field(unsigned Col) : Column(Col) {}
594 };
595
596 template <typename T, typename TEnum>
597 std::string printFlags(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues,
598 TEnum EnumMask1 = {}, TEnum EnumMask2 = {},
599 TEnum EnumMask3 = {}) const {
600 std::string Str;
601 for (const EnumEntry<TEnum> &Flag : EnumValues) {
602 if (Flag.Value == 0)
603 continue;
604
605 TEnum EnumMask{};
606 if (Flag.Value & EnumMask1)
607 EnumMask = EnumMask1;
608 else if (Flag.Value & EnumMask2)
609 EnumMask = EnumMask2;
610 else if (Flag.Value & EnumMask3)
611 EnumMask = EnumMask3;
612 bool IsEnum = (Flag.Value & EnumMask) != 0;
613 if ((!IsEnum && (Value & Flag.Value) == Flag.Value) ||
614 (IsEnum && (Value & EnumMask) == Flag.Value)) {
615 if (!Str.empty())
616 Str += ", ";
617 Str += Flag.AltName;
618 }
619 }
620 return Str;
621 }
622
623 formatted_raw_ostream &printField(struct Field F) const {
624 if (F.Column != 0)
625 OS.PadToColumn(F.Column);
626 OS << F.Str;
627 OS.flush();
628 return OS;
629 }
630 void printHashedSymbol(const Elf_Sym *Sym, unsigned SymIndex,
631 DataRegion<Elf_Word> ShndxTable, StringRef StrTable,
632 uint32_t Bucket);
633 void printRelrReloc(const Elf_Relr &R) override;
634 void printRelRelaReloc(const Relocation<ELFT> &R,
635 const RelSymbol<ELFT> &RelSym) override;
636 void printSymbol(const Elf_Sym &Symbol, unsigned SymIndex,
637 DataRegion<Elf_Word> ShndxTable,
638 Optional<StringRef> StrTable, bool IsDynamic,
639 bool NonVisibilityBitsUsed) const override;
640 void printDynamicRelocHeader(unsigned Type, StringRef Name,
641 const DynRegionInfo &Reg) override;
642
643 std::string getSymbolSectionNdx(const Elf_Sym &Symbol, unsigned SymIndex,
644 DataRegion<Elf_Word> ShndxTable) const;
645 void printProgramHeaders() override;
646 void printSectionMapping() override;
647 void printGNUVersionSectionProlog(const typename ELFT::Shdr &Sec,
648 const Twine &Label, unsigned EntriesNum);
649
650 void printStackSizeEntry(uint64_t Size,
651 ArrayRef<std::string> FuncNames) override;
652
653 void printMipsGOT(const MipsGOTParser<ELFT> &Parser) override;
654 void printMipsPLT(const MipsGOTParser<ELFT> &Parser) override;
655 void printMipsABIFlags() override;
656};
657
658template <typename ELFT> class LLVMELFDumper : public ELFDumper<ELFT> {
659public:
660 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)using Elf_Addr = typename ELFT::Addr; using Elf_Off = typename
ELFT::Off; using Elf_Half = typename ELFT::Half; using Elf_Word
= typename ELFT::Word; using Elf_Sword = typename ELFT::Sword
; using Elf_Xword = typename ELFT::Xword; using Elf_Sxword = typename
ELFT::Sxword; using uintX_t = typename ELFT::uint; using Elf_Ehdr
= typename ELFT::Ehdr; using Elf_Shdr = typename ELFT::Shdr;
using Elf_Sym = typename ELFT::Sym; using Elf_Dyn = typename
ELFT::Dyn; using Elf_Phdr = typename ELFT::Phdr; using Elf_Rel
= typename ELFT::Rel; using Elf_Rela = typename ELFT::Rela; using
Elf_Relr = typename ELFT::Relr; using Elf_Verdef = typename ELFT
::Verdef; using Elf_Verdaux = typename ELFT::Verdaux; using Elf_Verneed
= typename ELFT::Verneed; using Elf_Vernaux = typename ELFT::
Vernaux; using Elf_Versym = typename ELFT::Versym; using Elf_Hash
= typename ELFT::Hash; using Elf_GnuHash = typename ELFT::GnuHash
; using Elf_Nhdr = typename ELFT::Nhdr; using Elf_Note = typename
ELFT::Note; using Elf_Note_Iterator = typename ELFT::NoteIterator
; using Elf_CGProfile = typename ELFT::CGProfile; using Elf_Dyn_Range
= typename ELFT::DynRange; using Elf_Shdr_Range = typename ELFT
::ShdrRange; using Elf_Sym_Range = typename ELFT::SymRange; using
Elf_Rel_Range = typename ELFT::RelRange; using Elf_Rela_Range
= typename ELFT::RelaRange; using Elf_Relr_Range = typename ELFT
::RelrRange; using Elf_Phdr_Range = typename ELFT::PhdrRange;
661
662 LLVMELFDumper(const object::ELFObjectFile<ELFT> &ObjF, ScopedPrinter &Writer)
663 : ELFDumper<ELFT>(ObjF, Writer), W(Writer) {}
664
665 void printFileHeaders() override;
666 void printGroupSections() override;
667 void printRelocations() override;
668 void printSectionHeaders() override;
669 void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols) override;
670 void printDependentLibs() override;
671 void printDynamicTable() override;
672 void printDynamicRelocations() override;
673 void printProgramHeaders(bool PrintProgramHeaders,
674 cl::boolOrDefault PrintSectionMapping) override;
675 void printVersionSymbolSection(const Elf_Shdr *Sec) override;
676 void printVersionDefinitionSection(const Elf_Shdr *Sec) override;
677 void printVersionDependencySection(const Elf_Shdr *Sec) override;
678 void printHashHistograms() override;
679 void printCGProfile() override;
680 void printBBAddrMaps() override;
681 void printAddrsig() override;
682 void printNotes() override;
683 void printELFLinkerOptions() override;
684 void printStackSizes() override;
685
686private:
687 void printRelrReloc(const Elf_Relr &R) override;
688 void printRelRelaReloc(const Relocation<ELFT> &R,
689 const RelSymbol<ELFT> &RelSym) override;
690
691 void printSymbolSection(const Elf_Sym &Symbol, unsigned SymIndex,
692 DataRegion<Elf_Word> ShndxTable) const;
693 void printSymbol(const Elf_Sym &Symbol, unsigned SymIndex,
694 DataRegion<Elf_Word> ShndxTable,
695 Optional<StringRef> StrTable, bool IsDynamic,
696 bool /*NonVisibilityBitsUsed*/) const override;
697 void printProgramHeaders() override;
698 void printSectionMapping() override {}
699 void printStackSizeEntry(uint64_t Size,
700 ArrayRef<std::string> FuncNames) override;
701
702 void printMipsGOT(const MipsGOTParser<ELFT> &Parser) override;
703 void printMipsPLT(const MipsGOTParser<ELFT> &Parser) override;
704 void printMipsABIFlags() override;
705
706protected:
707 ScopedPrinter &W;
708};
709
710// JSONELFDumper shares most of the same implementation as LLVMELFDumper except
711// it uses a JSONScopedPrinter.
712template <typename ELFT> class JSONELFDumper : public LLVMELFDumper<ELFT> {
713public:
714 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)using Elf_Addr = typename ELFT::Addr; using Elf_Off = typename
ELFT::Off; using Elf_Half = typename ELFT::Half; using Elf_Word
= typename ELFT::Word; using Elf_Sword = typename ELFT::Sword
; using Elf_Xword = typename ELFT::Xword; using Elf_Sxword = typename
ELFT::Sxword; using uintX_t = typename ELFT::uint; using Elf_Ehdr
= typename ELFT::Ehdr; using Elf_Shdr = typename ELFT::Shdr;
using Elf_Sym = typename ELFT::Sym; using Elf_Dyn = typename
ELFT::Dyn; using Elf_Phdr = typename ELFT::Phdr; using Elf_Rel
= typename ELFT::Rel; using Elf_Rela = typename ELFT::Rela; using
Elf_Relr = typename ELFT::Relr; using Elf_Verdef = typename ELFT
::Verdef; using Elf_Verdaux = typename ELFT::Verdaux; using Elf_Verneed
= typename ELFT::Verneed; using Elf_Vernaux = typename ELFT::
Vernaux; using Elf_Versym = typename ELFT::Versym; using Elf_Hash
= typename ELFT::Hash; using Elf_GnuHash = typename ELFT::GnuHash
; using Elf_Nhdr = typename ELFT::Nhdr; using Elf_Note = typename
ELFT::Note; using Elf_Note_Iterator = typename ELFT::NoteIterator
; using Elf_CGProfile = typename ELFT::CGProfile; using Elf_Dyn_Range
= typename ELFT::DynRange; using Elf_Shdr_Range = typename ELFT
::ShdrRange; using Elf_Sym_Range = typename ELFT::SymRange; using
Elf_Rel_Range = typename ELFT::RelRange; using Elf_Rela_Range
= typename ELFT::RelaRange; using Elf_Relr_Range = typename ELFT
::RelrRange; using Elf_Phdr_Range = typename ELFT::PhdrRange;
715
716 JSONELFDumper(const object::ELFObjectFile<ELFT> &ObjF, ScopedPrinter &Writer)
717 : LLVMELFDumper<ELFT>(ObjF, Writer) {}
718
719 void printFileSummary(StringRef FileStr, ObjectFile &Obj,
720 ArrayRef<std::string> InputFilenames,
721 const Archive *A) override;
722
723private:
724 std::unique_ptr<DictScope> FileScope;
725};
726
727} // end anonymous namespace
728
729namespace llvm {
730
731template <class ELFT>
732static std::unique_ptr<ObjDumper>
733createELFDumper(const ELFObjectFile<ELFT> &Obj, ScopedPrinter &Writer) {
734 if (opts::Output == opts::GNU)
735 return std::make_unique<GNUELFDumper<ELFT>>(Obj, Writer);
736 else if (opts::Output == opts::JSON)
737 return std::make_unique<JSONELFDumper<ELFT>>(Obj, Writer);
738 return std::make_unique<LLVMELFDumper<ELFT>>(Obj, Writer);
739}
740
741std::unique_ptr<ObjDumper> createELFDumper(const object::ELFObjectFileBase &Obj,
742 ScopedPrinter &Writer) {
743 // Little-endian 32-bit
744 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(&Obj))
745 return createELFDumper(*ELFObj, Writer);
746
747 // Big-endian 32-bit
748 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(&Obj))
749 return createELFDumper(*ELFObj, Writer);
750
751 // Little-endian 64-bit
752 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(&Obj))
753 return createELFDumper(*ELFObj, Writer);
754
755 // Big-endian 64-bit
756 return createELFDumper(*cast<ELF64BEObjectFile>(&Obj), Writer);
757}
758
759} // end namespace llvm
760
761template <class ELFT>
762Expected<SmallVector<Optional<VersionEntry>, 0> *>
763ELFDumper<ELFT>::getVersionMap() const {
764 // If the VersionMap has already been loaded or if there is no dynamic symtab
765 // or version table, there is nothing to do.
766 if (!VersionMap.empty() || !DynSymRegion || !SymbolVersionSection)
767 return &VersionMap;
768
769 Expected<SmallVector<Optional<VersionEntry>, 0>> MapOrErr =
770 Obj.loadVersionMap(SymbolVersionNeedSection, SymbolVersionDefSection);
771 if (MapOrErr)
772 VersionMap = *MapOrErr;
773 else
774 return MapOrErr.takeError();
775
776 return &VersionMap;
777}
778
779template <typename ELFT>
780Expected<StringRef> ELFDumper<ELFT>::getSymbolVersion(const Elf_Sym &Sym,
781 bool &IsDefault) const {
782 // This is a dynamic symbol. Look in the GNU symbol version table.
783 if (!SymbolVersionSection) {
784 // No version table.
785 IsDefault = false;
786 return "";
787 }
788
789 assert(DynSymRegion && "DynSymRegion has not been initialised")(static_cast <bool> (DynSymRegion && "DynSymRegion has not been initialised"
) ? void (0) : __assert_fail ("DynSymRegion && \"DynSymRegion has not been initialised\""
, "llvm/tools/llvm-readobj/ELFDumper.cpp", 789, __extension__
__PRETTY_FUNCTION__))
;
790 // Determine the position in the symbol table of this entry.
791 size_t EntryIndex = (reinterpret_cast<uintptr_t>(&Sym) -
792 reinterpret_cast<uintptr_t>(DynSymRegion->Addr)) /
793 sizeof(Elf_Sym);
794
795 // Get the corresponding version index entry.
796 Expected<const Elf_Versym *> EntryOrErr =
797 Obj.template getEntry<Elf_Versym>(*SymbolVersionSection, EntryIndex);
798 if (!EntryOrErr)
799 return EntryOrErr.takeError();
800
801 unsigned Version = (*EntryOrErr)->vs_index;
802 if (Version == VER_NDX_LOCAL || Version == VER_NDX_GLOBAL) {
803 IsDefault = false;
804 return "";
805 }
806
807 Expected<SmallVector<Optional<VersionEntry>, 0> *> MapOrErr =
808 getVersionMap();
809 if (!MapOrErr)
810 return MapOrErr.takeError();
811
812 return Obj.getSymbolVersionByIndex(Version, IsDefault, **MapOrErr,
813 Sym.st_shndx == ELF::SHN_UNDEF);
814}
815
816template <typename ELFT>
817Expected<RelSymbol<ELFT>>
818ELFDumper<ELFT>::getRelocationTarget(const Relocation<ELFT> &R,
819 const Elf_Shdr *SymTab) const {
820 if (R.Symbol == 0)
821 return RelSymbol<ELFT>(nullptr, "");
822
823 Expected<const Elf_Sym *> SymOrErr =
824 Obj.template getEntry<Elf_Sym>(*SymTab, R.Symbol);
825 if (!SymOrErr)
826 return createError("unable to read an entry with index " + Twine(R.Symbol) +
827 " from " + describe(*SymTab) + ": " +
828 toString(SymOrErr.takeError()));
829 const Elf_Sym *Sym = *SymOrErr;
830 if (!Sym)
831 return RelSymbol<ELFT>(nullptr, "");
832
833 Expected<StringRef> StrTableOrErr = Obj.getStringTableForSymtab(*SymTab);
834 if (!StrTableOrErr)
835 return StrTableOrErr.takeError();
836
837 const Elf_Sym *FirstSym =
838 cantFail(Obj.template getEntry<Elf_Sym>(*SymTab, 0));
839 std::string SymbolName =
840 getFullSymbolName(*Sym, Sym - FirstSym, getShndxTable(SymTab),
841 *StrTableOrErr, SymTab->sh_type == SHT_DYNSYM);
842 return RelSymbol<ELFT>(Sym, SymbolName);
843}
844
845template <typename ELFT>
846ArrayRef<typename ELFT::Word>
847ELFDumper<ELFT>::getShndxTable(const Elf_Shdr *Symtab) const {
848 if (Symtab) {
849 auto It = ShndxTables.find(Symtab);
850 if (It != ShndxTables.end())
851 return It->second;
852 }
853 return {};
854}
855
856static std::string maybeDemangle(StringRef Name) {
857 return opts::Demangle ? demangle(std::string(Name)) : Name.str();
858}
859
860template <typename ELFT>
861std::string ELFDumper<ELFT>::getStaticSymbolName(uint32_t Index) const {
862 auto Warn = [&](Error E) -> std::string {
863 reportUniqueWarning("unable to read the name of symbol with index " +
864 Twine(Index) + ": " + toString(std::move(E)));
865 return "<?>";
866 };
867
868 Expected<const typename ELFT::Sym *> SymOrErr =
869 Obj.getSymbol(DotSymtabSec, Index);
870 if (!SymOrErr)
871 return Warn(SymOrErr.takeError());
872
873 Expected<StringRef> StrTabOrErr = Obj.getStringTableForSymtab(*DotSymtabSec);
874 if (!StrTabOrErr)
875 return Warn(StrTabOrErr.takeError());
876
877 Expected<StringRef> NameOrErr = (*SymOrErr)->getName(*StrTabOrErr);
878 if (!NameOrErr)
879 return Warn(NameOrErr.takeError());
880 return maybeDemangle(*NameOrErr);
881}
882
883template <typename ELFT>
884std::string ELFDumper<ELFT>::getFullSymbolName(const Elf_Sym &Symbol,
885 unsigned SymIndex,
886 DataRegion<Elf_Word> ShndxTable,
887 Optional<StringRef> StrTable,
888 bool IsDynamic) const {
889 if (!StrTable)
890 return "<?>";
891
892 std::string SymbolName;
893 if (Expected<StringRef> NameOrErr = Symbol.getName(*StrTable)) {
894 SymbolName = maybeDemangle(*NameOrErr);
895 } else {
896 reportUniqueWarning(NameOrErr.takeError());
897 return "<?>";
898 }
899
900 if (SymbolName.empty() && Symbol.getType() == ELF::STT_SECTION) {
901 Expected<unsigned> SectionIndex =
902 getSymbolSectionIndex(Symbol, SymIndex, ShndxTable);
903 if (!SectionIndex) {
904 reportUniqueWarning(SectionIndex.takeError());
905 return "<?>";
906 }
907 Expected<StringRef> NameOrErr = getSymbolSectionName(Symbol, *SectionIndex);
908 if (!NameOrErr) {
909 reportUniqueWarning(NameOrErr.takeError());
910 return ("<section " + Twine(*SectionIndex) + ">").str();
911 }
912 return std::string(*NameOrErr);
913 }
914
915 if (!IsDynamic)
916 return SymbolName;
917
918 bool IsDefault;
919 Expected<StringRef> VersionOrErr = getSymbolVersion(Symbol, IsDefault);
920 if (!VersionOrErr) {
921 reportUniqueWarning(VersionOrErr.takeError());
922 return SymbolName + "@<corrupt>";
923 }
924
925 if (!VersionOrErr->empty()) {
926 SymbolName += (IsDefault ? "@@" : "@");
927 SymbolName += *VersionOrErr;
928 }
929 return SymbolName;
930}
931
932template <typename ELFT>
933Expected<unsigned>
934ELFDumper<ELFT>::getSymbolSectionIndex(const Elf_Sym &Symbol, unsigned SymIndex,
935 DataRegion<Elf_Word> ShndxTable) const {
936 unsigned Ndx = Symbol.st_shndx;
937 if (Ndx == SHN_XINDEX)
938 return object::getExtendedSymbolTableIndex<ELFT>(Symbol, SymIndex,
939 ShndxTable);
940 if (Ndx != SHN_UNDEF && Ndx < SHN_LORESERVE)
941 return Ndx;
942
943 auto CreateErr = [&](const Twine &Name, Optional<unsigned> Offset = None) {
944 std::string Desc;
945 if (Offset)
946 Desc = (Name + "+0x" + Twine::utohexstr(*Offset)).str();
947 else
948 Desc = Name.str();
949 return createError(
950 "unable to get section index for symbol with st_shndx = 0x" +
951 Twine::utohexstr(Ndx) + " (" + Desc + ")");
952 };
953
954 if (Ndx >= ELF::SHN_LOPROC && Ndx <= ELF::SHN_HIPROC)
955 return CreateErr("SHN_LOPROC", Ndx - ELF::SHN_LOPROC);
956 if (Ndx >= ELF::SHN_LOOS && Ndx <= ELF::SHN_HIOS)
957 return CreateErr("SHN_LOOS", Ndx - ELF::SHN_LOOS);
958 if (Ndx == ELF::SHN_UNDEF)
959 return CreateErr("SHN_UNDEF");
960 if (Ndx == ELF::SHN_ABS)
961 return CreateErr("SHN_ABS");
962 if (Ndx == ELF::SHN_COMMON)
963 return CreateErr("SHN_COMMON");
964 return CreateErr("SHN_LORESERVE", Ndx - SHN_LORESERVE);
965}
966
967template <typename ELFT>
968Expected<StringRef>
969ELFDumper<ELFT>::getSymbolSectionName(const Elf_Sym &Symbol,
970 unsigned SectionIndex) const {
971 Expected<const Elf_Shdr *> SecOrErr = Obj.getSection(SectionIndex);
972 if (!SecOrErr)
973 return SecOrErr.takeError();
974 return Obj.getSectionName(**SecOrErr);
975}
976
977template <class ELFO>
978static const typename ELFO::Elf_Shdr *
979findNotEmptySectionByAddress(const ELFO &Obj, StringRef FileName,
980 uint64_t Addr) {
981 for (const typename ELFO::Elf_Shdr &Shdr : cantFail(Obj.sections()))
982 if (Shdr.sh_addr == Addr && Shdr.sh_size > 0)
983 return &Shdr;
984 return nullptr;
985}
986
987const EnumEntry<unsigned> ElfClass[] = {
988 {"None", "none", ELF::ELFCLASSNONE},
989 {"32-bit", "ELF32", ELF::ELFCLASS32},
990 {"64-bit", "ELF64", ELF::ELFCLASS64},
991};
992
993const EnumEntry<unsigned> ElfDataEncoding[] = {
994 {"None", "none", ELF::ELFDATANONE},
995 {"LittleEndian", "2's complement, little endian", ELF::ELFDATA2LSB},
996 {"BigEndian", "2's complement, big endian", ELF::ELFDATA2MSB},
997};
998
999const EnumEntry<unsigned> ElfObjectFileType[] = {
1000 {"None", "NONE (none)", ELF::ET_NONE},
1001 {"Relocatable", "REL (Relocatable file)", ELF::ET_REL},
1002 {"Executable", "EXEC (Executable file)", ELF::ET_EXEC},
1003 {"SharedObject", "DYN (Shared object file)", ELF::ET_DYN},
1004 {"Core", "CORE (Core file)", ELF::ET_CORE},
1005};
1006
1007const EnumEntry<unsigned> ElfOSABI[] = {
1008 {"SystemV", "UNIX - System V", ELF::ELFOSABI_NONE},
1009 {"HPUX", "UNIX - HP-UX", ELF::ELFOSABI_HPUX},
1010 {"NetBSD", "UNIX - NetBSD", ELF::ELFOSABI_NETBSD},
1011 {"GNU/Linux", "UNIX - GNU", ELF::ELFOSABI_LINUX},
1012 {"GNU/Hurd", "GNU/Hurd", ELF::ELFOSABI_HURD},
1013 {"Solaris", "UNIX - Solaris", ELF::ELFOSABI_SOLARIS},
1014 {"AIX", "UNIX - AIX", ELF::ELFOSABI_AIX},
1015 {"IRIX", "UNIX - IRIX", ELF::ELFOSABI_IRIX},
1016 {"FreeBSD", "UNIX - FreeBSD", ELF::ELFOSABI_FREEBSD},
1017 {"TRU64", "UNIX - TRU64", ELF::ELFOSABI_TRU64},
1018 {"Modesto", "Novell - Modesto", ELF::ELFOSABI_MODESTO},
1019 {"OpenBSD", "UNIX - OpenBSD", ELF::ELFOSABI_OPENBSD},
1020 {"OpenVMS", "VMS - OpenVMS", ELF::ELFOSABI_OPENVMS},
1021 {"NSK", "HP - Non-Stop Kernel", ELF::ELFOSABI_NSK},
1022 {"AROS", "AROS", ELF::ELFOSABI_AROS},
1023 {"FenixOS", "FenixOS", ELF::ELFOSABI_FENIXOS},
1024 {"CloudABI", "CloudABI", ELF::ELFOSABI_CLOUDABI},
1025 {"Standalone", "Standalone App", ELF::ELFOSABI_STANDALONE}
1026};
1027
1028const EnumEntry<unsigned> AMDGPUElfOSABI[] = {
1029 {"AMDGPU_HSA", "AMDGPU - HSA", ELF::ELFOSABI_AMDGPU_HSA},
1030 {"AMDGPU_PAL", "AMDGPU - PAL", ELF::ELFOSABI_AMDGPU_PAL},
1031 {"AMDGPU_MESA3D", "AMDGPU - MESA3D", ELF::ELFOSABI_AMDGPU_MESA3D}
1032};
1033
1034const EnumEntry<unsigned> ARMElfOSABI[] = {
1035 {"ARM", "ARM", ELF::ELFOSABI_ARM}
1036};
1037
1038const EnumEntry<unsigned> C6000ElfOSABI[] = {
1039 {"C6000_ELFABI", "Bare-metal C6000", ELF::ELFOSABI_C6000_ELFABI},
1040 {"C6000_LINUX", "Linux C6000", ELF::ELFOSABI_C6000_LINUX}
1041};
1042
1043const EnumEntry<unsigned> ElfMachineType[] = {
1044 ENUM_ENT(EM_NONE, "None"){ "EM_NONE", "None", ELF::EM_NONE },
1045 ENUM_ENT(EM_M32, "WE32100"){ "EM_M32", "WE32100", ELF::EM_M32 },
1046 ENUM_ENT(EM_SPARC, "Sparc"){ "EM_SPARC", "Sparc", ELF::EM_SPARC },
1047 ENUM_ENT(EM_386, "Intel 80386"){ "EM_386", "Intel 80386", ELF::EM_386 },
1048 ENUM_ENT(EM_68K, "MC68000"){ "EM_68K", "MC68000", ELF::EM_68K },
1049 ENUM_ENT(EM_88K, "MC88000"){ "EM_88K", "MC88000", ELF::EM_88K },
1050 ENUM_ENT(EM_IAMCU, "EM_IAMCU"){ "EM_IAMCU", "EM_IAMCU", ELF::EM_IAMCU },
1051 ENUM_ENT(EM_860, "Intel 80860"){ "EM_860", "Intel 80860", ELF::EM_860 },
1052 ENUM_ENT(EM_MIPS, "MIPS R3000"){ "EM_MIPS", "MIPS R3000", ELF::EM_MIPS },
1053 ENUM_ENT(EM_S370, "IBM System/370"){ "EM_S370", "IBM System/370", ELF::EM_S370 },
1054 ENUM_ENT(EM_MIPS_RS3_LE, "MIPS R3000 little-endian"){ "EM_MIPS_RS3_LE", "MIPS R3000 little-endian", ELF::EM_MIPS_RS3_LE
}
,
1055 ENUM_ENT(EM_PARISC, "HPPA"){ "EM_PARISC", "HPPA", ELF::EM_PARISC },
1056 ENUM_ENT(EM_VPP500, "Fujitsu VPP500"){ "EM_VPP500", "Fujitsu VPP500", ELF::EM_VPP500 },
1057 ENUM_ENT(EM_SPARC32PLUS, "Sparc v8+"){ "EM_SPARC32PLUS", "Sparc v8+", ELF::EM_SPARC32PLUS },
1058 ENUM_ENT(EM_960, "Intel 80960"){ "EM_960", "Intel 80960", ELF::EM_960 },
1059 ENUM_ENT(EM_PPC, "PowerPC"){ "EM_PPC", "PowerPC", ELF::EM_PPC },
1060 ENUM_ENT(EM_PPC64, "PowerPC64"){ "EM_PPC64", "PowerPC64", ELF::EM_PPC64 },
1061 ENUM_ENT(EM_S390, "IBM S/390"){ "EM_S390", "IBM S/390", ELF::EM_S390 },
1062 ENUM_ENT(EM_SPU, "SPU"){ "EM_SPU", "SPU", ELF::EM_SPU },
1063 ENUM_ENT(EM_V800, "NEC V800 series"){ "EM_V800", "NEC V800 series", ELF::EM_V800 },
1064 ENUM_ENT(EM_FR20, "Fujistsu FR20"){ "EM_FR20", "Fujistsu FR20", ELF::EM_FR20 },
1065 ENUM_ENT(EM_RH32, "TRW RH-32"){ "EM_RH32", "TRW RH-32", ELF::EM_RH32 },
1066 ENUM_ENT(EM_RCE, "Motorola RCE"){ "EM_RCE", "Motorola RCE", ELF::EM_RCE },
1067 ENUM_ENT(EM_ARM, "ARM"){ "EM_ARM", "ARM", ELF::EM_ARM },
1068 ENUM_ENT(EM_ALPHA, "EM_ALPHA"){ "EM_ALPHA", "EM_ALPHA", ELF::EM_ALPHA },
1069 ENUM_ENT(EM_SH, "Hitachi SH"){ "EM_SH", "Hitachi SH", ELF::EM_SH },
1070 ENUM_ENT(EM_SPARCV9, "Sparc v9"){ "EM_SPARCV9", "Sparc v9", ELF::EM_SPARCV9 },
1071 ENUM_ENT(EM_TRICORE, "Siemens Tricore"){ "EM_TRICORE", "Siemens Tricore", ELF::EM_TRICORE },
1072 ENUM_ENT(EM_ARC, "ARC"){ "EM_ARC", "ARC", ELF::EM_ARC },
1073 ENUM_ENT(EM_H8_300, "Hitachi H8/300"){ "EM_H8_300", "Hitachi H8/300", ELF::EM_H8_300 },
1074 ENUM_ENT(EM_H8_300H, "Hitachi H8/300H"){ "EM_H8_300H", "Hitachi H8/300H", ELF::EM_H8_300H },
1075 ENUM_ENT(EM_H8S, "Hitachi H8S"){ "EM_H8S", "Hitachi H8S", ELF::EM_H8S },
1076 ENUM_ENT(EM_H8_500, "Hitachi H8/500"){ "EM_H8_500", "Hitachi H8/500", ELF::EM_H8_500 },
1077 ENUM_ENT(EM_IA_64, "Intel IA-64"){ "EM_IA_64", "Intel IA-64", ELF::EM_IA_64 },
1078 ENUM_ENT(EM_MIPS_X, "Stanford MIPS-X"){ "EM_MIPS_X", "Stanford MIPS-X", ELF::EM_MIPS_X },
1079 ENUM_ENT(EM_COLDFIRE, "Motorola Coldfire"){ "EM_COLDFIRE", "Motorola Coldfire", ELF::EM_COLDFIRE },
1080 ENUM_ENT(EM_68HC12, "Motorola MC68HC12 Microcontroller"){ "EM_68HC12", "Motorola MC68HC12 Microcontroller", ELF::EM_68HC12
}
,
1081 ENUM_ENT(EM_MMA, "Fujitsu Multimedia Accelerator"){ "EM_MMA", "Fujitsu Multimedia Accelerator", ELF::EM_MMA },
1082 ENUM_ENT(EM_PCP, "Siemens PCP"){ "EM_PCP", "Siemens PCP", ELF::EM_PCP },
1083 ENUM_ENT(EM_NCPU, "Sony nCPU embedded RISC processor"){ "EM_NCPU", "Sony nCPU embedded RISC processor", ELF::EM_NCPU
}
,
1084 ENUM_ENT(EM_NDR1, "Denso NDR1 microprocesspr"){ "EM_NDR1", "Denso NDR1 microprocesspr", ELF::EM_NDR1 },
1085 ENUM_ENT(EM_STARCORE, "Motorola Star*Core processor"){ "EM_STARCORE", "Motorola Star*Core processor", ELF::EM_STARCORE
}
,
1086 ENUM_ENT(EM_ME16, "Toyota ME16 processor"){ "EM_ME16", "Toyota ME16 processor", ELF::EM_ME16 },
1087 ENUM_ENT(EM_ST100, "STMicroelectronics ST100 processor"){ "EM_ST100", "STMicroelectronics ST100 processor", ELF::EM_ST100
}
,
1088 ENUM_ENT(EM_TINYJ, "Advanced Logic Corp. TinyJ embedded processor"){ "EM_TINYJ", "Advanced Logic Corp. TinyJ embedded processor"
, ELF::EM_TINYJ }
,
1089 ENUM_ENT(EM_X86_64, "Advanced Micro Devices X86-64"){ "EM_X86_64", "Advanced Micro Devices X86-64", ELF::EM_X86_64
}
,
1090 ENUM_ENT(EM_PDSP, "Sony DSP processor"){ "EM_PDSP", "Sony DSP processor", ELF::EM_PDSP },
1091 ENUM_ENT(EM_PDP10, "Digital Equipment Corp. PDP-10"){ "EM_PDP10", "Digital Equipment Corp. PDP-10", ELF::EM_PDP10
}
,
1092 ENUM_ENT(EM_PDP11, "Digital Equipment Corp. PDP-11"){ "EM_PDP11", "Digital Equipment Corp. PDP-11", ELF::EM_PDP11
}
,
1093 ENUM_ENT(EM_FX66, "Siemens FX66 microcontroller"){ "EM_FX66", "Siemens FX66 microcontroller", ELF::EM_FX66 },
1094 ENUM_ENT(EM_ST9PLUS, "STMicroelectronics ST9+ 8/16 bit microcontroller"){ "EM_ST9PLUS", "STMicroelectronics ST9+ 8/16 bit microcontroller"
, ELF::EM_ST9PLUS }
,
1095 ENUM_ENT(EM_ST7, "STMicroelectronics ST7 8-bit microcontroller"){ "EM_ST7", "STMicroelectronics ST7 8-bit microcontroller", ELF
::EM_ST7 }
,
1096 ENUM_ENT(EM_68HC16, "Motorola MC68HC16 Microcontroller"){ "EM_68HC16", "Motorola MC68HC16 Microcontroller", ELF::EM_68HC16
}
,
1097 ENUM_ENT(EM_68HC11, "Motorola MC68HC11 Microcontroller"){ "EM_68HC11", "Motorola MC68HC11 Microcontroller", ELF::EM_68HC11
}
,
1098 ENUM_ENT(EM_68HC08, "Motorola MC68HC08 Microcontroller"){ "EM_68HC08", "Motorola MC68HC08 Microcontroller", ELF::EM_68HC08
}
,
1099 ENUM_ENT(EM_68HC05, "Motorola MC68HC05 Microcontroller"){ "EM_68HC05", "Motorola MC68HC05 Microcontroller", ELF::EM_68HC05
}
,
1100 ENUM_ENT(EM_SVX, "Silicon Graphics SVx"){ "EM_SVX", "Silicon Graphics SVx", ELF::EM_SVX },
1101 ENUM_ENT(EM_ST19, "STMicroelectronics ST19 8-bit microcontroller"){ "EM_ST19", "STMicroelectronics ST19 8-bit microcontroller",
ELF::EM_ST19 }
,
1102 ENUM_ENT(EM_VAX, "Digital VAX"){ "EM_VAX", "Digital VAX", ELF::EM_VAX },
1103 ENUM_ENT(EM_CRIS, "Axis Communications 32-bit embedded processor"){ "EM_CRIS", "Axis Communications 32-bit embedded processor",
ELF::EM_CRIS }
,
1104 ENUM_ENT(EM_JAVELIN, "Infineon Technologies 32-bit embedded cpu"){ "EM_JAVELIN", "Infineon Technologies 32-bit embedded cpu", ELF
::EM_JAVELIN }
,
1105 ENUM_ENT(EM_FIREPATH, "Element 14 64-bit DSP processor"){ "EM_FIREPATH", "Element 14 64-bit DSP processor", ELF::EM_FIREPATH
}
,
1106 ENUM_ENT(EM_ZSP, "LSI Logic's 16-bit DSP processor"){ "EM_ZSP", "LSI Logic's 16-bit DSP processor", ELF::EM_ZSP },
1107 ENUM_ENT(EM_MMIX, "Donald Knuth's educational 64-bit processor"){ "EM_MMIX", "Donald Knuth's educational 64-bit processor", ELF
::EM_MMIX }
,
1108 ENUM_ENT(EM_HUANY, "Harvard Universitys's machine-independent object format"){ "EM_HUANY", "Harvard Universitys's machine-independent object format"
, ELF::EM_HUANY }
,
1109 ENUM_ENT(EM_PRISM, "Vitesse Prism"){ "EM_PRISM", "Vitesse Prism", ELF::EM_PRISM },
1110 ENUM_ENT(EM_AVR, "Atmel AVR 8-bit microcontroller"){ "EM_AVR", "Atmel AVR 8-bit microcontroller", ELF::EM_AVR },
1111 ENUM_ENT(EM_FR30, "Fujitsu FR30"){ "EM_FR30", "Fujitsu FR30", ELF::EM_FR30 },
1112 ENUM_ENT(EM_D10V, "Mitsubishi D10V"){ "EM_D10V", "Mitsubishi D10V", ELF::EM_D10V },
1113 ENUM_ENT(EM_D30V, "Mitsubishi D30V"){ "EM_D30V", "Mitsubishi D30V", ELF::EM_D30V },
1114 ENUM_ENT(EM_V850, "NEC v850"){ "EM_V850", "NEC v850", ELF::EM_V850 },
1115 ENUM_ENT(EM_M32R, "Renesas M32R (formerly Mitsubishi M32r)"){ "EM_M32R", "Renesas M32R (formerly Mitsubishi M32r)", ELF::
EM_M32R }
,
1116 ENUM_ENT(EM_MN10300, "Matsushita MN10300"){ "EM_MN10300", "Matsushita MN10300", ELF::EM_MN10300 },
1117 ENUM_ENT(EM_MN10200, "Matsushita MN10200"){ "EM_MN10200", "Matsushita MN10200", ELF::EM_MN10200 },
1118 ENUM_ENT(EM_PJ, "picoJava"){ "EM_PJ", "picoJava", ELF::EM_PJ },
1119 ENUM_ENT(EM_OPENRISC, "OpenRISC 32-bit embedded processor"){ "EM_OPENRISC", "OpenRISC 32-bit embedded processor", ELF::EM_OPENRISC
}
,
1120 ENUM_ENT(EM_ARC_COMPACT, "EM_ARC_COMPACT"){ "EM_ARC_COMPACT", "EM_ARC_COMPACT", ELF::EM_ARC_COMPACT },
1121 ENUM_ENT(EM_XTENSA, "Tensilica Xtensa Processor"){ "EM_XTENSA", "Tensilica Xtensa Processor", ELF::EM_XTENSA },
1122 ENUM_ENT(EM_VIDEOCORE, "Alphamosaic VideoCore processor"){ "EM_VIDEOCORE", "Alphamosaic VideoCore processor", ELF::EM_VIDEOCORE
}
,
1123 ENUM_ENT(EM_TMM_GPP, "Thompson Multimedia General Purpose Processor"){ "EM_TMM_GPP", "Thompson Multimedia General Purpose Processor"
, ELF::EM_TMM_GPP }
,
1124 ENUM_ENT(EM_NS32K, "National Semiconductor 32000 series"){ "EM_NS32K", "National Semiconductor 32000 series", ELF::EM_NS32K
}
,
1125 ENUM_ENT(EM_TPC, "Tenor Network TPC processor"){ "EM_TPC", "Tenor Network TPC processor", ELF::EM_TPC },
1126 ENUM_ENT(EM_SNP1K, "EM_SNP1K"){ "EM_SNP1K", "EM_SNP1K", ELF::EM_SNP1K },
1127 ENUM_ENT(EM_ST200, "STMicroelectronics ST200 microcontroller"){ "EM_ST200", "STMicroelectronics ST200 microcontroller", ELF
::EM_ST200 }
,
1128 ENUM_ENT(EM_IP2K, "Ubicom IP2xxx 8-bit microcontrollers"){ "EM_IP2K", "Ubicom IP2xxx 8-bit microcontrollers", ELF::EM_IP2K
}
,
1129 ENUM_ENT(EM_MAX, "MAX Processor"){ "EM_MAX", "MAX Processor", ELF::EM_MAX },
1130 ENUM_ENT(EM_CR, "National Semiconductor CompactRISC"){ "EM_CR", "National Semiconductor CompactRISC", ELF::EM_CR },
1131 ENUM_ENT(EM_F2MC16, "Fujitsu F2MC16"){ "EM_F2MC16", "Fujitsu F2MC16", ELF::EM_F2MC16 },
1132 ENUM_ENT(EM_MSP430, "Texas Instruments msp430 microcontroller"){ "EM_MSP430", "Texas Instruments msp430 microcontroller", ELF
::EM_MSP430 }
,
1133 ENUM_ENT(EM_BLACKFIN, "Analog Devices Blackfin"){ "EM_BLACKFIN", "Analog Devices Blackfin", ELF::EM_BLACKFIN },
1134 ENUM_ENT(EM_SE_C33, "S1C33 Family of Seiko Epson processors"){ "EM_SE_C33", "S1C33 Family of Seiko Epson processors", ELF::
EM_SE_C33 }
,
1135 ENUM_ENT(EM_SEP, "Sharp embedded microprocessor"){ "EM_SEP", "Sharp embedded microprocessor", ELF::EM_SEP },
1136 ENUM_ENT(EM_ARCA, "Arca RISC microprocessor"){ "EM_ARCA", "Arca RISC microprocessor", ELF::EM_ARCA },
1137 ENUM_ENT(EM_UNICORE, "Unicore"){ "EM_UNICORE", "Unicore", ELF::EM_UNICORE },
1138 ENUM_ENT(EM_EXCESS, "eXcess 16/32/64-bit configurable embedded CPU"){ "EM_EXCESS", "eXcess 16/32/64-bit configurable embedded CPU"
, ELF::EM_EXCESS }
,
1139 ENUM_ENT(EM_DXP, "Icera Semiconductor Inc. Deep Execution Processor"){ "EM_DXP", "Icera Semiconductor Inc. Deep Execution Processor"
, ELF::EM_DXP }
,
1140 ENUM_ENT(EM_ALTERA_NIOS2, "Altera Nios"){ "EM_ALTERA_NIOS2", "Altera Nios", ELF::EM_ALTERA_NIOS2 },
1141 ENUM_ENT(EM_CRX, "National Semiconductor CRX microprocessor"){ "EM_CRX", "National Semiconductor CRX microprocessor", ELF::
EM_CRX }
,
1142 ENUM_ENT(EM_XGATE, "Motorola XGATE embedded processor"){ "EM_XGATE", "Motorola XGATE embedded processor", ELF::EM_XGATE
}
,
1143 ENUM_ENT(EM_C166, "Infineon Technologies xc16x"){ "EM_C166", "Infineon Technologies xc16x", ELF::EM_C166 },
1144 ENUM_ENT(EM_M16C, "Renesas M16C"){ "EM_M16C", "Renesas M16C", ELF::EM_M16C },
1145 ENUM_ENT(EM_DSPIC30F, "Microchip Technology dsPIC30F Digital Signal Controller"){ "EM_DSPIC30F", "Microchip Technology dsPIC30F Digital Signal Controller"
, ELF::EM_DSPIC30F }
,
1146 ENUM_ENT(EM_CE, "Freescale Communication Engine RISC core"){ "EM_CE", "Freescale Communication Engine RISC core", ELF::EM_CE
}
,
1147 ENUM_ENT(EM_M32C, "Renesas M32C"){ "EM_M32C", "Renesas M32C", ELF::EM_M32C },
1148 ENUM_ENT(EM_TSK3000, "Altium TSK3000 core"){ "EM_TSK3000", "Altium TSK3000 core", ELF::EM_TSK3000 },
1149 ENUM_ENT(EM_RS08, "Freescale RS08 embedded processor"){ "EM_RS08", "Freescale RS08 embedded processor", ELF::EM_RS08
}
,
1150 ENUM_ENT(EM_SHARC, "EM_SHARC"){ "EM_SHARC", "EM_SHARC", ELF::EM_SHARC },
1151 ENUM_ENT(EM_ECOG2, "Cyan Technology eCOG2 microprocessor"){ "EM_ECOG2", "Cyan Technology eCOG2 microprocessor", ELF::EM_ECOG2
}
,
1152 ENUM_ENT(EM_SCORE7, "SUNPLUS S+Core"){ "EM_SCORE7", "SUNPLUS S+Core", ELF::EM_SCORE7 },
1153 ENUM_ENT(EM_DSP24, "New Japan Radio (NJR) 24-bit DSP Processor"){ "EM_DSP24", "New Japan Radio (NJR) 24-bit DSP Processor", ELF
::EM_DSP24 }
,
1154 ENUM_ENT(EM_VIDEOCORE3, "Broadcom VideoCore III processor"){ "EM_VIDEOCORE3", "Broadcom VideoCore III processor", ELF::EM_VIDEOCORE3
}
,
1155 ENUM_ENT(EM_LATTICEMICO32, "Lattice Mico32"){ "EM_LATTICEMICO32", "Lattice Mico32", ELF::EM_LATTICEMICO32
}
,
1156 ENUM_ENT(EM_SE_C17, "Seiko Epson C17 family"){ "EM_SE_C17", "Seiko Epson C17 family", ELF::EM_SE_C17 },
1157 ENUM_ENT(EM_TI_C6000, "Texas Instruments TMS320C6000 DSP family"){ "EM_TI_C6000", "Texas Instruments TMS320C6000 DSP family", ELF
::EM_TI_C6000 }
,
1158 ENUM_ENT(EM_TI_C2000, "Texas Instruments TMS320C2000 DSP family"){ "EM_TI_C2000", "Texas Instruments TMS320C2000 DSP family", ELF
::EM_TI_C2000 }
,
1159 ENUM_ENT(EM_TI_C5500, "Texas Instruments TMS320C55x DSP family"){ "EM_TI_C5500", "Texas Instruments TMS320C55x DSP family", ELF
::EM_TI_C5500 }
,
1160 ENUM_ENT(EM_MMDSP_PLUS, "STMicroelectronics 64bit VLIW Data Signal Processor"){ "EM_MMDSP_PLUS", "STMicroelectronics 64bit VLIW Data Signal Processor"
, ELF::EM_MMDSP_PLUS }
,
1161 ENUM_ENT(EM_CYPRESS_M8C, "Cypress M8C microprocessor"){ "EM_CYPRESS_M8C", "Cypress M8C microprocessor", ELF::EM_CYPRESS_M8C
}
,
1162 ENUM_ENT(EM_R32C, "Renesas R32C series microprocessors"){ "EM_R32C", "Renesas R32C series microprocessors", ELF::EM_R32C
}
,
1163 ENUM_ENT(EM_TRIMEDIA, "NXP Semiconductors TriMedia architecture family"){ "EM_TRIMEDIA", "NXP Semiconductors TriMedia architecture family"
, ELF::EM_TRIMEDIA }
,
1164 ENUM_ENT(EM_HEXAGON, "Qualcomm Hexagon"){ "EM_HEXAGON", "Qualcomm Hexagon", ELF::EM_HEXAGON },
1165 ENUM_ENT(EM_8051, "Intel 8051 and variants"){ "EM_8051", "Intel 8051 and variants", ELF::EM_8051 },
1166 ENUM_ENT(EM_STXP7X, "STMicroelectronics STxP7x family"){ "EM_STXP7X", "STMicroelectronics STxP7x family", ELF::EM_STXP7X
}
,
1167 ENUM_ENT(EM_NDS32, "Andes Technology compact code size embedded RISC processor family"){ "EM_NDS32", "Andes Technology compact code size embedded RISC processor family"
, ELF::EM_NDS32 }
,
1168 ENUM_ENT(EM_ECOG1, "Cyan Technology eCOG1 microprocessor"){ "EM_ECOG1", "Cyan Technology eCOG1 microprocessor", ELF::EM_ECOG1
}
,
1169 // FIXME: Following EM_ECOG1X definitions is dead code since EM_ECOG1X has
1170 // an identical number to EM_ECOG1.
1171 ENUM_ENT(EM_ECOG1X, "Cyan Technology eCOG1X family"){ "EM_ECOG1X", "Cyan Technology eCOG1X family", ELF::EM_ECOG1X
}
,
1172 ENUM_ENT(EM_MAXQ30, "Dallas Semiconductor MAXQ30 Core microcontrollers"){ "EM_MAXQ30", "Dallas Semiconductor MAXQ30 Core microcontrollers"
, ELF::EM_MAXQ30 }
,
1173 ENUM_ENT(EM_XIMO16, "New Japan Radio (NJR) 16-bit DSP Processor"){ "EM_XIMO16", "New Japan Radio (NJR) 16-bit DSP Processor", ELF
::EM_XIMO16 }
,
1174 ENUM_ENT(EM_MANIK, "M2000 Reconfigurable RISC Microprocessor"){ "EM_MANIK", "M2000 Reconfigurable RISC Microprocessor", ELF
::EM_MANIK }
,
1175 ENUM_ENT(EM_CRAYNV2, "Cray Inc. NV2 vector architecture"){ "EM_CRAYNV2", "Cray Inc. NV2 vector architecture", ELF::EM_CRAYNV2
}
,
1176 ENUM_ENT(EM_RX, "Renesas RX"){ "EM_RX", "Renesas RX", ELF::EM_RX },
1177 ENUM_ENT(EM_METAG, "Imagination Technologies Meta processor architecture"){ "EM_METAG", "Imagination Technologies Meta processor architecture"
, ELF::EM_METAG }
,
1178 ENUM_ENT(EM_MCST_ELBRUS, "MCST Elbrus general purpose hardware architecture"){ "EM_MCST_ELBRUS", "MCST Elbrus general purpose hardware architecture"
, ELF::EM_MCST_ELBRUS }
,
1179 ENUM_ENT(EM_ECOG16, "Cyan Technology eCOG16 family"){ "EM_ECOG16", "Cyan Technology eCOG16 family", ELF::EM_ECOG16
}
,
1180 ENUM_ENT(EM_CR16, "National Semiconductor CompactRISC 16-bit processor"){ "EM_CR16", "National Semiconductor CompactRISC 16-bit processor"
, ELF::EM_CR16 }
,
1181 ENUM_ENT(EM_ETPU, "Freescale Extended Time Processing Unit"){ "EM_ETPU", "Freescale Extended Time Processing Unit", ELF::
EM_ETPU }
,
1182 ENUM_ENT(EM_SLE9X, "Infineon Technologies SLE9X core"){ "EM_SLE9X", "Infineon Technologies SLE9X core", ELF::EM_SLE9X
}
,
1183 ENUM_ENT(EM_L10M, "EM_L10M"){ "EM_L10M", "EM_L10M", ELF::EM_L10M },
1184 ENUM_ENT(EM_K10M, "EM_K10M"){ "EM_K10M", "EM_K10M", ELF::EM_K10M },
1185 ENUM_ENT(EM_AARCH64, "AArch64"){ "EM_AARCH64", "AArch64", ELF::EM_AARCH64 },
1186 ENUM_ENT(EM_AVR32, "Atmel Corporation 32-bit microprocessor family"){ "EM_AVR32", "Atmel Corporation 32-bit microprocessor family"
, ELF::EM_AVR32 }
,
1187 ENUM_ENT(EM_STM8, "STMicroeletronics STM8 8-bit microcontroller"){ "EM_STM8", "STMicroeletronics STM8 8-bit microcontroller", ELF
::EM_STM8 }
,
1188 ENUM_ENT(EM_TILE64, "Tilera TILE64 multicore architecture family"){ "EM_TILE64", "Tilera TILE64 multicore architecture family",
ELF::EM_TILE64 }
,
1189 ENUM_ENT(EM_TILEPRO, "Tilera TILEPro multicore architecture family"){ "EM_TILEPRO", "Tilera TILEPro multicore architecture family"
, ELF::EM_TILEPRO }
,
1190 ENUM_ENT(EM_MICROBLAZE, "Xilinx MicroBlaze 32-bit RISC soft processor core"){ "EM_MICROBLAZE", "Xilinx MicroBlaze 32-bit RISC soft processor core"
, ELF::EM_MICROBLAZE }
,
1191 ENUM_ENT(EM_CUDA, "NVIDIA CUDA architecture"){ "EM_CUDA", "NVIDIA CUDA architecture", ELF::EM_CUDA },
1192 ENUM_ENT(EM_TILEGX, "Tilera TILE-Gx multicore architecture family"){ "EM_TILEGX", "Tilera TILE-Gx multicore architecture family"
, ELF::EM_TILEGX }
,
1193 ENUM_ENT(EM_CLOUDSHIELD, "EM_CLOUDSHIELD"){ "EM_CLOUDSHIELD", "EM_CLOUDSHIELD", ELF::EM_CLOUDSHIELD },
1194 ENUM_ENT(EM_COREA_1ST, "EM_COREA_1ST"){ "EM_COREA_1ST", "EM_COREA_1ST", ELF::EM_COREA_1ST },
1195 ENUM_ENT(EM_COREA_2ND, "EM_COREA_2ND"){ "EM_COREA_2ND", "EM_COREA_2ND", ELF::EM_COREA_2ND },
1196 ENUM_ENT(EM_ARC_COMPACT2, "EM_ARC_COMPACT2"){ "EM_ARC_COMPACT2", "EM_ARC_COMPACT2", ELF::EM_ARC_COMPACT2 },
1197 ENUM_ENT(EM_OPEN8, "EM_OPEN8"){ "EM_OPEN8", "EM_OPEN8", ELF::EM_OPEN8 },
1198 ENUM_ENT(EM_RL78, "Renesas RL78"){ "EM_RL78", "Renesas RL78", ELF::EM_RL78 },
1199 ENUM_ENT(EM_VIDEOCORE5, "Broadcom VideoCore V processor"){ "EM_VIDEOCORE5", "Broadcom VideoCore V processor", ELF::EM_VIDEOCORE5
}
,
1200 ENUM_ENT(EM_78KOR, "EM_78KOR"){ "EM_78KOR", "EM_78KOR", ELF::EM_78KOR },
1201 ENUM_ENT(EM_56800EX, "EM_56800EX"){ "EM_56800EX", "EM_56800EX", ELF::EM_56800EX },
1202 ENUM_ENT(EM_AMDGPU, "EM_AMDGPU"){ "EM_AMDGPU", "EM_AMDGPU", ELF::EM_AMDGPU },
1203 ENUM_ENT(EM_RISCV, "RISC-V"){ "EM_RISCV", "RISC-V", ELF::EM_RISCV },
1204 ENUM_ENT(EM_LANAI, "EM_LANAI"){ "EM_LANAI", "EM_LANAI", ELF::EM_LANAI },
1205 ENUM_ENT(EM_BPF, "EM_BPF"){ "EM_BPF", "EM_BPF", ELF::EM_BPF },
1206 ENUM_ENT(EM_VE, "NEC SX-Aurora Vector Engine"){ "EM_VE", "NEC SX-Aurora Vector Engine", ELF::EM_VE },
1207 ENUM_ENT(EM_LOONGARCH, "LoongArch"){ "EM_LOONGARCH", "LoongArch", ELF::EM_LOONGARCH },
1208};
1209
1210const EnumEntry<unsigned> ElfSymbolBindings[] = {
1211 {"Local", "LOCAL", ELF::STB_LOCAL},
1212 {"Global", "GLOBAL", ELF::STB_GLOBAL},
1213 {"Weak", "WEAK", ELF::STB_WEAK},
1214 {"Unique", "UNIQUE", ELF::STB_GNU_UNIQUE}};
1215
1216const EnumEntry<unsigned> ElfSymbolVisibilities[] = {
1217 {"DEFAULT", "DEFAULT", ELF::STV_DEFAULT},
1218 {"INTERNAL", "INTERNAL", ELF::STV_INTERNAL},
1219 {"HIDDEN", "HIDDEN", ELF::STV_HIDDEN},
1220 {"PROTECTED", "PROTECTED", ELF::STV_PROTECTED}};
1221
1222const EnumEntry<unsigned> AMDGPUSymbolTypes[] = {
1223 { "AMDGPU_HSA_KERNEL", ELF::STT_AMDGPU_HSA_KERNEL }
1224};
1225
1226static const char *getGroupType(uint32_t Flag) {
1227 if (Flag & ELF::GRP_COMDAT)
1228 return "COMDAT";
1229 else
1230 return "(unknown)";
1231}
1232
1233const EnumEntry<unsigned> ElfSectionFlags[] = {
1234 ENUM_ENT(SHF_WRITE, "W"){ "SHF_WRITE", "W", ELF::SHF_WRITE },
1235 ENUM_ENT(SHF_ALLOC, "A"){ "SHF_ALLOC", "A", ELF::SHF_ALLOC },
1236 ENUM_ENT(SHF_EXECINSTR, "X"){ "SHF_EXECINSTR", "X", ELF::SHF_EXECINSTR },
1237 ENUM_ENT(SHF_MERGE, "M"){ "SHF_MERGE", "M", ELF::SHF_MERGE },
1238 ENUM_ENT(SHF_STRINGS, "S"){ "SHF_STRINGS", "S", ELF::SHF_STRINGS },
1239 ENUM_ENT(SHF_INFO_LINK, "I"){ "SHF_INFO_LINK", "I", ELF::SHF_INFO_LINK },
1240 ENUM_ENT(SHF_LINK_ORDER, "L"){ "SHF_LINK_ORDER", "L", ELF::SHF_LINK_ORDER },
1241 ENUM_ENT(SHF_OS_NONCONFORMING, "O"){ "SHF_OS_NONCONFORMING", "O", ELF::SHF_OS_NONCONFORMING },
1242 ENUM_ENT(SHF_GROUP, "G"){ "SHF_GROUP", "G", ELF::SHF_GROUP },
1243 ENUM_ENT(SHF_TLS, "T"){ "SHF_TLS", "T", ELF::SHF_TLS },
1244 ENUM_ENT(SHF_COMPRESSED, "C"){ "SHF_COMPRESSED", "C", ELF::SHF_COMPRESSED },
1245 ENUM_ENT(SHF_EXCLUDE, "E"){ "SHF_EXCLUDE", "E", ELF::SHF_EXCLUDE },
1246};
1247
1248const EnumEntry<unsigned> ElfGNUSectionFlags[] = {
1249 ENUM_ENT(SHF_GNU_RETAIN, "R"){ "SHF_GNU_RETAIN", "R", ELF::SHF_GNU_RETAIN }
1250};
1251
1252const EnumEntry<unsigned> ElfSolarisSectionFlags[] = {
1253 ENUM_ENT(SHF_SUNW_NODISCARD, "R"){ "SHF_SUNW_NODISCARD", "R", ELF::SHF_SUNW_NODISCARD }
1254};
1255
1256const EnumEntry<unsigned> ElfXCoreSectionFlags[] = {
1257 ENUM_ENT(XCORE_SHF_CP_SECTION, ""){ "XCORE_SHF_CP_SECTION", "", ELF::XCORE_SHF_CP_SECTION },
1258 ENUM_ENT(XCORE_SHF_DP_SECTION, ""){ "XCORE_SHF_DP_SECTION", "", ELF::XCORE_SHF_DP_SECTION }
1259};
1260
1261const EnumEntry<unsigned> ElfARMSectionFlags[] = {
1262 ENUM_ENT(SHF_ARM_PURECODE, "y"){ "SHF_ARM_PURECODE", "y", ELF::SHF_ARM_PURECODE }
1263};
1264
1265const EnumEntry<unsigned> ElfHexagonSectionFlags[] = {
1266 ENUM_ENT(SHF_HEX_GPREL, ""){ "SHF_HEX_GPREL", "", ELF::SHF_HEX_GPREL }
1267};
1268
1269const EnumEntry<unsigned> ElfMipsSectionFlags[] = {
1270 ENUM_ENT(SHF_MIPS_NODUPES, ""){ "SHF_MIPS_NODUPES", "", ELF::SHF_MIPS_NODUPES },
1271 ENUM_ENT(SHF_MIPS_NAMES, ""){ "SHF_MIPS_NAMES", "", ELF::SHF_MIPS_NAMES },
1272 ENUM_ENT(SHF_MIPS_LOCAL, ""){ "SHF_MIPS_LOCAL", "", ELF::SHF_MIPS_LOCAL },
1273 ENUM_ENT(SHF_MIPS_NOSTRIP, ""){ "SHF_MIPS_NOSTRIP", "", ELF::SHF_MIPS_NOSTRIP },
1274 ENUM_ENT(SHF_MIPS_GPREL, ""){ "SHF_MIPS_GPREL", "", ELF::SHF_MIPS_GPREL },
1275 ENUM_ENT(SHF_MIPS_MERGE, ""){ "SHF_MIPS_MERGE", "", ELF::SHF_MIPS_MERGE },
1276 ENUM_ENT(SHF_MIPS_ADDR, ""){ "SHF_MIPS_ADDR", "", ELF::SHF_MIPS_ADDR },
1277 ENUM_ENT(SHF_MIPS_STRING, ""){ "SHF_MIPS_STRING", "", ELF::SHF_MIPS_STRING }
1278};
1279
1280const EnumEntry<unsigned> ElfX86_64SectionFlags[] = {
1281 ENUM_ENT(SHF_X86_64_LARGE, "l"){ "SHF_X86_64_LARGE", "l", ELF::SHF_X86_64_LARGE }
1282};
1283
1284static std::vector<EnumEntry<unsigned>>
1285getSectionFlagsForTarget(unsigned EOSAbi, unsigned EMachine) {
1286 std::vector<EnumEntry<unsigned>> Ret(std::begin(ElfSectionFlags),
1287 std::end(ElfSectionFlags));
1288 switch (EOSAbi) {
1289 case ELFOSABI_SOLARIS:
1290 Ret.insert(Ret.end(), std::begin(ElfSolarisSectionFlags),
1291 std::end(ElfSolarisSectionFlags));
1292 break;
1293 default:
1294 Ret.insert(Ret.end(), std::begin(ElfGNUSectionFlags),
1295 std::end(ElfGNUSectionFlags));
1296 break;
1297 }
1298 switch (EMachine) {
1299 case EM_ARM:
1300 Ret.insert(Ret.end(), std::begin(ElfARMSectionFlags),
1301 std::end(ElfARMSectionFlags));
1302 break;
1303 case EM_HEXAGON:
1304 Ret.insert(Ret.end(), std::begin(ElfHexagonSectionFlags),
1305 std::end(ElfHexagonSectionFlags));
1306 break;
1307 case EM_MIPS:
1308 Ret.insert(Ret.end(), std::begin(ElfMipsSectionFlags),
1309 std::end(ElfMipsSectionFlags));
1310 break;
1311 case EM_X86_64:
1312 Ret.insert(Ret.end(), std::begin(ElfX86_64SectionFlags),
1313 std::end(ElfX86_64SectionFlags));
1314 break;
1315 case EM_XCORE:
1316 Ret.insert(Ret.end(), std::begin(ElfXCoreSectionFlags),
1317 std::end(ElfXCoreSectionFlags));
1318 break;
1319 default:
1320 break;
1321 }
1322 return Ret;
1323}
1324
1325static std::string getGNUFlags(unsigned EOSAbi, unsigned EMachine,
1326 uint64_t Flags) {
1327 // Here we are trying to build the flags string in the same way as GNU does.
1328 // It is not that straightforward. Imagine we have sh_flags == 0x90000000.
1329 // SHF_EXCLUDE ("E") has a value of 0x80000000 and SHF_MASKPROC is 0xf0000000.
1330 // GNU readelf will not print "E" or "Ep" in this case, but will print just
1331 // "p". It only will print "E" when no other processor flag is set.
1332 std::string Str;
1333 bool HasUnknownFlag = false;
1334 bool HasOSFlag = false;
1335 bool HasProcFlag = false;
1336 std::vector<EnumEntry<unsigned>> FlagsList =
1337 getSectionFlagsForTarget(EOSAbi, EMachine);
1338 while (Flags) {
1339 // Take the least significant bit as a flag.
1340 uint64_t Flag = Flags & -Flags;
1341 Flags -= Flag;
1342
1343 // Find the flag in the known flags list.
1344 auto I = llvm::find_if(FlagsList, [=](const EnumEntry<unsigned> &E) {
1345 // Flags with empty names are not printed in GNU style output.
1346 return E.Value == Flag && !E.AltName.empty();
1347 });
1348 if (I != FlagsList.end()) {
1349 Str += I->AltName;
1350 continue;
1351 }
1352
1353 // If we did not find a matching regular flag, then we deal with an OS
1354 // specific flag, processor specific flag or an unknown flag.
1355 if (Flag & ELF::SHF_MASKOS) {
1356 HasOSFlag = true;
1357 Flags &= ~ELF::SHF_MASKOS;
1358 } else if (Flag & ELF::SHF_MASKPROC) {
1359 HasProcFlag = true;
1360 // Mask off all the processor-specific bits. This removes the SHF_EXCLUDE
1361 // bit if set so that it doesn't also get printed.
1362 Flags &= ~ELF::SHF_MASKPROC;
1363 } else {
1364 HasUnknownFlag = true;
1365 }
1366 }
1367
1368 // "o", "p" and "x" are printed last.
1369 if (HasOSFlag)
1370 Str += "o";
1371 if (HasProcFlag)
1372 Str += "p";
1373 if (HasUnknownFlag)
1374 Str += "x";
1375 return Str;
1376}
1377
1378static StringRef segmentTypeToString(unsigned Arch, unsigned Type) {
1379 // Check potentially overlapped processor-specific program header type.
1380 switch (Arch) {
1381 case ELF::EM_ARM:
1382 switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, PT_ARM_EXIDX)case ELF::PT_ARM_EXIDX: return "PT_ARM_EXIDX";; }
1383 break;
1384 case ELF::EM_MIPS:
1385 case ELF::EM_MIPS_RS3_LE:
1386 switch (Type) {
1387 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_REGINFO)case ELF::PT_MIPS_REGINFO: return "PT_MIPS_REGINFO";;
1388 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_RTPROC)case ELF::PT_MIPS_RTPROC: return "PT_MIPS_RTPROC";;
1389 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_OPTIONS)case ELF::PT_MIPS_OPTIONS: return "PT_MIPS_OPTIONS";;
1390 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_ABIFLAGS)case ELF::PT_MIPS_ABIFLAGS: return "PT_MIPS_ABIFLAGS";;
1391 }
1392 break;
1393 case ELF::EM_RISCV:
1394 switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, PT_RISCV_ATTRIBUTES)case ELF::PT_RISCV_ATTRIBUTES: return "PT_RISCV_ATTRIBUTES";; }
1395 }
1396
1397 switch (Type) {
1398 LLVM_READOBJ_ENUM_CASE(ELF, PT_NULL)case ELF::PT_NULL: return "PT_NULL";;
1399 LLVM_READOBJ_ENUM_CASE(ELF, PT_LOAD)case ELF::PT_LOAD: return "PT_LOAD";;
1400 LLVM_READOBJ_ENUM_CASE(ELF, PT_DYNAMIC)case ELF::PT_DYNAMIC: return "PT_DYNAMIC";;
1401 LLVM_READOBJ_ENUM_CASE(ELF, PT_INTERP)case ELF::PT_INTERP: return "PT_INTERP";;
1402 LLVM_READOBJ_ENUM_CASE(ELF, PT_NOTE)case ELF::PT_NOTE: return "PT_NOTE";;
1403 LLVM_READOBJ_ENUM_CASE(ELF, PT_SHLIB)case ELF::PT_SHLIB: return "PT_SHLIB";;
1404 LLVM_READOBJ_ENUM_CASE(ELF, PT_PHDR)case ELF::PT_PHDR: return "PT_PHDR";;
1405 LLVM_READOBJ_ENUM_CASE(ELF, PT_TLS)case ELF::PT_TLS: return "PT_TLS";;
1406
1407 LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_EH_FRAME)case ELF::PT_GNU_EH_FRAME: return "PT_GNU_EH_FRAME";;
1408 LLVM_READOBJ_ENUM_CASE(ELF, PT_SUNW_UNWIND)case ELF::PT_SUNW_UNWIND: return "PT_SUNW_UNWIND";;
1409
1410 LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_STACK)case ELF::PT_GNU_STACK: return "PT_GNU_STACK";;
1411 LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_RELRO)case ELF::PT_GNU_RELRO: return "PT_GNU_RELRO";;
1412 LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_PROPERTY)case ELF::PT_GNU_PROPERTY: return "PT_GNU_PROPERTY";;
1413
1414 LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_RANDOMIZE)case ELF::PT_OPENBSD_RANDOMIZE: return "PT_OPENBSD_RANDOMIZE"
;
;
1415 LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_WXNEEDED)case ELF::PT_OPENBSD_WXNEEDED: return "PT_OPENBSD_WXNEEDED";;
1416 LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_BOOTDATA)case ELF::PT_OPENBSD_BOOTDATA: return "PT_OPENBSD_BOOTDATA";;
1417 default:
1418 return "";
1419 }
1420}
1421
1422static std::string getGNUPtType(unsigned Arch, unsigned Type) {
1423 StringRef Seg = segmentTypeToString(Arch, Type);
1424 if (Seg.empty())
1425 return std::string("<unknown>: ") + to_string(format_hex(Type, 1));
1426
1427 // E.g. "PT_ARM_EXIDX" -> "EXIDX".
1428 if (Seg.consume_front("PT_ARM_"))
1429 return Seg.str();
1430
1431 // E.g. "PT_MIPS_REGINFO" -> "REGINFO".
1432 if (Seg.consume_front("PT_MIPS_"))
1433 return Seg.str();
1434
1435 // E.g. "PT_RISCV_ATTRIBUTES"
1436 if (Seg.consume_front("PT_RISCV_"))
1437 return Seg.str();
1438
1439 // E.g. "PT_LOAD" -> "LOAD".
1440 assert(Seg.startswith("PT_"))(static_cast <bool> (Seg.startswith("PT_")) ? void (0) :
__assert_fail ("Seg.startswith(\"PT_\")", "llvm/tools/llvm-readobj/ELFDumper.cpp"
, 1440, __extension__ __PRETTY_FUNCTION__))
;
1441 return Seg.drop_front(3).str();
1442}
1443
1444const EnumEntry<unsigned> ElfSegmentFlags[] = {
1445 LLVM_READOBJ_ENUM_ENT(ELF, PF_X){ "PF_X", ELF::PF_X },
1446 LLVM_READOBJ_ENUM_ENT(ELF, PF_W){ "PF_W", ELF::PF_W },
1447 LLVM_READOBJ_ENUM_ENT(ELF, PF_R){ "PF_R", ELF::PF_R }
1448};
1449
1450const EnumEntry<unsigned> ElfHeaderMipsFlags[] = {
1451 ENUM_ENT(EF_MIPS_NOREORDER, "noreorder"){ "EF_MIPS_NOREORDER", "noreorder", ELF::EF_MIPS_NOREORDER },
1452 ENUM_ENT(EF_MIPS_PIC, "pic"){ "EF_MIPS_PIC", "pic", ELF::EF_MIPS_PIC },
1453 ENUM_ENT(EF_MIPS_CPIC, "cpic"){ "EF_MIPS_CPIC", "cpic", ELF::EF_MIPS_CPIC },
1454 ENUM_ENT(EF_MIPS_ABI2, "abi2"){ "EF_MIPS_ABI2", "abi2", ELF::EF_MIPS_ABI2 },
1455 ENUM_ENT(EF_MIPS_32BITMODE, "32bitmode"){ "EF_MIPS_32BITMODE", "32bitmode", ELF::EF_MIPS_32BITMODE },
1456 ENUM_ENT(EF_MIPS_FP64, "fp64"){ "EF_MIPS_FP64", "fp64", ELF::EF_MIPS_FP64 },
1457 ENUM_ENT(EF_MIPS_NAN2008, "nan2008"){ "EF_MIPS_NAN2008", "nan2008", ELF::EF_MIPS_NAN2008 },
1458 ENUM_ENT(EF_MIPS_ABI_O32, "o32"){ "EF_MIPS_ABI_O32", "o32", ELF::EF_MIPS_ABI_O32 },
1459 ENUM_ENT(EF_MIPS_ABI_O64, "o64"){ "EF_MIPS_ABI_O64", "o64", ELF::EF_MIPS_ABI_O64 },
1460 ENUM_ENT(EF_MIPS_ABI_EABI32, "eabi32"){ "EF_MIPS_ABI_EABI32", "eabi32", ELF::EF_MIPS_ABI_EABI32 },
1461 ENUM_ENT(EF_MIPS_ABI_EABI64, "eabi64"){ "EF_MIPS_ABI_EABI64", "eabi64", ELF::EF_MIPS_ABI_EABI64 },
1462 ENUM_ENT(EF_MIPS_MACH_3900, "3900"){ "EF_MIPS_MACH_3900", "3900", ELF::EF_MIPS_MACH_3900 },
1463 ENUM_ENT(EF_MIPS_MACH_4010, "4010"){ "EF_MIPS_MACH_4010", "4010", ELF::EF_MIPS_MACH_4010 },
1464 ENUM_ENT(EF_MIPS_MACH_4100, "4100"){ "EF_MIPS_MACH_4100", "4100", ELF::EF_MIPS_MACH_4100 },
1465 ENUM_ENT(EF_MIPS_MACH_4650, "4650"){ "EF_MIPS_MACH_4650", "4650", ELF::EF_MIPS_MACH_4650 },
1466 ENUM_ENT(EF_MIPS_MACH_4120, "4120"){ "EF_MIPS_MACH_4120", "4120", ELF::EF_MIPS_MACH_4120 },
1467 ENUM_ENT(EF_MIPS_MACH_4111, "4111"){ "EF_MIPS_MACH_4111", "4111", ELF::EF_MIPS_MACH_4111 },
1468 ENUM_ENT(EF_MIPS_MACH_SB1, "sb1"){ "EF_MIPS_MACH_SB1", "sb1", ELF::EF_MIPS_MACH_SB1 },
1469 ENUM_ENT(EF_MIPS_MACH_OCTEON, "octeon"){ "EF_MIPS_MACH_OCTEON", "octeon", ELF::EF_MIPS_MACH_OCTEON },
1470 ENUM_ENT(EF_MIPS_MACH_XLR, "xlr"){ "EF_MIPS_MACH_XLR", "xlr", ELF::EF_MIPS_MACH_XLR },
1471 ENUM_ENT(EF_MIPS_MACH_OCTEON2, "octeon2"){ "EF_MIPS_MACH_OCTEON2", "octeon2", ELF::EF_MIPS_MACH_OCTEON2
}
,
1472 ENUM_ENT(EF_MIPS_MACH_OCTEON3, "octeon3"){ "EF_MIPS_MACH_OCTEON3", "octeon3", ELF::EF_MIPS_MACH_OCTEON3
}
,
1473 ENUM_ENT(EF_MIPS_MACH_5400, "5400"){ "EF_MIPS_MACH_5400", "5400", ELF::EF_MIPS_MACH_5400 },
1474 ENUM_ENT(EF_MIPS_MACH_5900, "5900"){ "EF_MIPS_MACH_5900", "5900", ELF::EF_MIPS_MACH_5900 },
1475 ENUM_ENT(EF_MIPS_MACH_5500, "5500"){ "EF_MIPS_MACH_5500", "5500", ELF::EF_MIPS_MACH_5500 },
1476 ENUM_ENT(EF_MIPS_MACH_9000, "9000"){ "EF_MIPS_MACH_9000", "9000", ELF::EF_MIPS_MACH_9000 },
1477 ENUM_ENT(EF_MIPS_MACH_LS2E, "loongson-2e"){ "EF_MIPS_MACH_LS2E", "loongson-2e", ELF::EF_MIPS_MACH_LS2E },
1478 ENUM_ENT(EF_MIPS_MACH_LS2F, "loongson-2f"){ "EF_MIPS_MACH_LS2F", "loongson-2f", ELF::EF_MIPS_MACH_LS2F },
1479 ENUM_ENT(EF_MIPS_MACH_LS3A, "loongson-3a"){ "EF_MIPS_MACH_LS3A", "loongson-3a", ELF::EF_MIPS_MACH_LS3A },
1480 ENUM_ENT(EF_MIPS_MICROMIPS, "micromips"){ "EF_MIPS_MICROMIPS", "micromips", ELF::EF_MIPS_MICROMIPS },
1481 ENUM_ENT(EF_MIPS_ARCH_ASE_M16, "mips16"){ "EF_MIPS_ARCH_ASE_M16", "mips16", ELF::EF_MIPS_ARCH_ASE_M16
}
,
1482 ENUM_ENT(EF_MIPS_ARCH_ASE_MDMX, "mdmx"){ "EF_MIPS_ARCH_ASE_MDMX", "mdmx", ELF::EF_MIPS_ARCH_ASE_MDMX
}
,
1483 ENUM_ENT(EF_MIPS_ARCH_1, "mips1"){ "EF_MIPS_ARCH_1", "mips1", ELF::EF_MIPS_ARCH_1 },
1484 ENUM_ENT(EF_MIPS_ARCH_2, "mips2"){ "EF_MIPS_ARCH_2", "mips2", ELF::EF_MIPS_ARCH_2 },
1485 ENUM_ENT(EF_MIPS_ARCH_3, "mips3"){ "EF_MIPS_ARCH_3", "mips3", ELF::EF_MIPS_ARCH_3 },
1486 ENUM_ENT(EF_MIPS_ARCH_4, "mips4"){ "EF_MIPS_ARCH_4", "mips4", ELF::EF_MIPS_ARCH_4 },
1487 ENUM_ENT(EF_MIPS_ARCH_5, "mips5"){ "EF_MIPS_ARCH_5", "mips5", ELF::EF_MIPS_ARCH_5 },
1488 ENUM_ENT(EF_MIPS_ARCH_32, "mips32"){ "EF_MIPS_ARCH_32", "mips32", ELF::EF_MIPS_ARCH_32 },
1489 ENUM_ENT(EF_MIPS_ARCH_64, "mips64"){ "EF_MIPS_ARCH_64", "mips64", ELF::EF_MIPS_ARCH_64 },
1490 ENUM_ENT(EF_MIPS_ARCH_32R2, "mips32r2"){ "EF_MIPS_ARCH_32R2", "mips32r2", ELF::EF_MIPS_ARCH_32R2 },
1491 ENUM_ENT(EF_MIPS_ARCH_64R2, "mips64r2"){ "EF_MIPS_ARCH_64R2", "mips64r2", ELF::EF_MIPS_ARCH_64R2 },
1492 ENUM_ENT(EF_MIPS_ARCH_32R6, "mips32r6"){ "EF_MIPS_ARCH_32R6", "mips32r6", ELF::EF_MIPS_ARCH_32R6 },
1493 ENUM_ENT(EF_MIPS_ARCH_64R6, "mips64r6"){ "EF_MIPS_ARCH_64R6", "mips64r6", ELF::EF_MIPS_ARCH_64R6 }
1494};
1495
1496const EnumEntry<unsigned> ElfHeaderAMDGPUFlagsABIVersion3[] = {
1497 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_NONE){ "EF_AMDGPU_MACH_NONE", ELF::EF_AMDGPU_MACH_NONE },
1498 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R600){ "EF_AMDGPU_MACH_R600_R600", ELF::EF_AMDGPU_MACH_R600_R600 },
1499 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R630){ "EF_AMDGPU_MACH_R600_R630", ELF::EF_AMDGPU_MACH_R600_R630 },
1500 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RS880){ "EF_AMDGPU_MACH_R600_RS880", ELF::EF_AMDGPU_MACH_R600_RS880
}
,
1501 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV670){ "EF_AMDGPU_MACH_R600_RV670", ELF::EF_AMDGPU_MACH_R600_RV670
}
,
1502 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV710){ "EF_AMDGPU_MACH_R600_RV710", ELF::EF_AMDGPU_MACH_R600_RV710
}
,
1503 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV730){ "EF_AMDGPU_MACH_R600_RV730", ELF::EF_AMDGPU_MACH_R600_RV730
}
,
1504 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV770){ "EF_AMDGPU_MACH_R600_RV770", ELF::EF_AMDGPU_MACH_R600_RV770
}
,
1505 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CEDAR){ "EF_AMDGPU_MACH_R600_CEDAR", ELF::EF_AMDGPU_MACH_R600_CEDAR
}
,
1506 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CYPRESS){ "EF_AMDGPU_MACH_R600_CYPRESS", ELF::EF_AMDGPU_MACH_R600_CYPRESS
}
,
1507 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_JUNIPER){ "EF_AMDGPU_MACH_R600_JUNIPER", ELF::EF_AMDGPU_MACH_R600_JUNIPER
}
,
1508 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_REDWOOD){ "EF_AMDGPU_MACH_R600_REDWOOD", ELF::EF_AMDGPU_MACH_R600_REDWOOD
}
,
1509 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_SUMO){ "EF_AMDGPU_MACH_R600_SUMO", ELF::EF_AMDGPU_MACH_R600_SUMO },
1510 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_BARTS){ "EF_AMDGPU_MACH_R600_BARTS", ELF::EF_AMDGPU_MACH_R600_BARTS
}
,
1511 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAICOS){ "EF_AMDGPU_MACH_R600_CAICOS", ELF::EF_AMDGPU_MACH_R600_CAICOS
}
,
1512 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAYMAN){ "EF_AMDGPU_MACH_R600_CAYMAN", ELF::EF_AMDGPU_MACH_R600_CAYMAN
}
,
1513 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_TURKS){ "EF_AMDGPU_MACH_R600_TURKS", ELF::EF_AMDGPU_MACH_R600_TURKS
}
,
1514 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX600){ "EF_AMDGPU_MACH_AMDGCN_GFX600", ELF::EF_AMDGPU_MACH_AMDGCN_GFX600
}
,
1515 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX601){ "EF_AMDGPU_MACH_AMDGCN_GFX601", ELF::EF_AMDGPU_MACH_AMDGCN_GFX601
}
,
1516 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX602){ "EF_AMDGPU_MACH_AMDGCN_GFX602", ELF::EF_AMDGPU_MACH_AMDGCN_GFX602
}
,
1517 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX700){ "EF_AMDGPU_MACH_AMDGCN_GFX700", ELF::EF_AMDGPU_MACH_AMDGCN_GFX700
}
,
1518 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX701){ "EF_AMDGPU_MACH_AMDGCN_GFX701", ELF::EF_AMDGPU_MACH_AMDGCN_GFX701
}
,
1519 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX702){ "EF_AMDGPU_MACH_AMDGCN_GFX702", ELF::EF_AMDGPU_MACH_AMDGCN_GFX702
}
,
1520 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX703){ "EF_AMDGPU_MACH_AMDGCN_GFX703", ELF::EF_AMDGPU_MACH_AMDGCN_GFX703
}
,
1521 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX704){ "EF_AMDGPU_MACH_AMDGCN_GFX704", ELF::EF_AMDGPU_MACH_AMDGCN_GFX704
}
,
1522 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX705){ "EF_AMDGPU_MACH_AMDGCN_GFX705", ELF::EF_AMDGPU_MACH_AMDGCN_GFX705
}
,
1523 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX801){ "EF_AMDGPU_MACH_AMDGCN_GFX801", ELF::EF_AMDGPU_MACH_AMDGCN_GFX801
}
,
1524 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX802){ "EF_AMDGPU_MACH_AMDGCN_GFX802", ELF::EF_AMDGPU_MACH_AMDGCN_GFX802
}
,
1525 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX803){ "EF_AMDGPU_MACH_AMDGCN_GFX803", ELF::EF_AMDGPU_MACH_AMDGCN_GFX803
}
,
1526 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX805){ "EF_AMDGPU_MACH_AMDGCN_GFX805", ELF::EF_AMDGPU_MACH_AMDGCN_GFX805
}
,
1527 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX810){ "EF_AMDGPU_MACH_AMDGCN_GFX810", ELF::EF_AMDGPU_MACH_AMDGCN_GFX810
}
,
1528 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX900){ "EF_AMDGPU_MACH_AMDGCN_GFX900", ELF::EF_AMDGPU_MACH_AMDGCN_GFX900
}
,
1529 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX902){ "EF_AMDGPU_MACH_AMDGCN_GFX902", ELF::EF_AMDGPU_MACH_AMDGCN_GFX902
}
,
1530 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX904){ "EF_AMDGPU_MACH_AMDGCN_GFX904", ELF::EF_AMDGPU_MACH_AMDGCN_GFX904
}
,
1531 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX906){ "EF_AMDGPU_MACH_AMDGCN_GFX906", ELF::EF_AMDGPU_MACH_AMDGCN_GFX906
}
,
1532 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX908){ "EF_AMDGPU_MACH_AMDGCN_GFX908", ELF::EF_AMDGPU_MACH_AMDGCN_GFX908
}
,
1533 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX909){ "EF_AMDGPU_MACH_AMDGCN_GFX909", ELF::EF_AMDGPU_MACH_AMDGCN_GFX909
}
,
1534 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX90A){ "EF_AMDGPU_MACH_AMDGCN_GFX90A", ELF::EF_AMDGPU_MACH_AMDGCN_GFX90A
}
,
1535 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX90C){ "EF_AMDGPU_MACH_AMDGCN_GFX90C", ELF::EF_AMDGPU_MACH_AMDGCN_GFX90C
}
,
1536 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX940){ "EF_AMDGPU_MACH_AMDGCN_GFX940", ELF::EF_AMDGPU_MACH_AMDGCN_GFX940
}
,
1537 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1010){ "EF_AMDGPU_MACH_AMDGCN_GFX1010", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1010
}
,
1538 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1011){ "EF_AMDGPU_MACH_AMDGCN_GFX1011", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1011
}
,
1539 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1012){ "EF_AMDGPU_MACH_AMDGCN_GFX1012", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1012
}
,
1540 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1013){ "EF_AMDGPU_MACH_AMDGCN_GFX1013", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1013
}
,
1541 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1030){ "EF_AMDGPU_MACH_AMDGCN_GFX1030", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1030
}
,
1542 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1031){ "EF_AMDGPU_MACH_AMDGCN_GFX1031", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1031
}
,
1543 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1032){ "EF_AMDGPU_MACH_AMDGCN_GFX1032", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1032
}
,
1544 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1033){ "EF_AMDGPU_MACH_AMDGCN_GFX1033", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1033
}
,
1545 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1034){ "EF_AMDGPU_MACH_AMDGCN_GFX1034", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1034
}
,
1546 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1035){ "EF_AMDGPU_MACH_AMDGCN_GFX1035", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1035
}
,
1547 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1036){ "EF_AMDGPU_MACH_AMDGCN_GFX1036", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1036
}
,
1548 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1100){ "EF_AMDGPU_MACH_AMDGCN_GFX1100", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1100
}
,
1549 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1101){ "EF_AMDGPU_MACH_AMDGCN_GFX1101", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1101
}
,
1550 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1102){ "EF_AMDGPU_MACH_AMDGCN_GFX1102", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1102
}
,
1551 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1103){ "EF_AMDGPU_MACH_AMDGCN_GFX1103", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1103
}
,
1552 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_FEATURE_XNACK_V3){ "EF_AMDGPU_FEATURE_XNACK_V3", ELF::EF_AMDGPU_FEATURE_XNACK_V3
}
,
1553 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_FEATURE_SRAMECC_V3){ "EF_AMDGPU_FEATURE_SRAMECC_V3", ELF::EF_AMDGPU_FEATURE_SRAMECC_V3
}
1554};
1555
1556const EnumEntry<unsigned> ElfHeaderAMDGPUFlagsABIVersion4[] = {
1557 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_NONE){ "EF_AMDGPU_MACH_NONE", ELF::EF_AMDGPU_MACH_NONE },
1558 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R600){ "EF_AMDGPU_MACH_R600_R600", ELF::EF_AMDGPU_MACH_R600_R600 },
1559 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R630){ "EF_AMDGPU_MACH_R600_R630", ELF::EF_AMDGPU_MACH_R600_R630 },
1560 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RS880){ "EF_AMDGPU_MACH_R600_RS880", ELF::EF_AMDGPU_MACH_R600_RS880
}
,
1561 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV670){ "EF_AMDGPU_MACH_R600_RV670", ELF::EF_AMDGPU_MACH_R600_RV670
}
,
1562 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV710){ "EF_AMDGPU_MACH_R600_RV710", ELF::EF_AMDGPU_MACH_R600_RV710
}
,
1563 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV730){ "EF_AMDGPU_MACH_R600_RV730", ELF::EF_AMDGPU_MACH_R600_RV730
}
,
1564 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV770){ "EF_AMDGPU_MACH_R600_RV770", ELF::EF_AMDGPU_MACH_R600_RV770
}
,
1565 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CEDAR){ "EF_AMDGPU_MACH_R600_CEDAR", ELF::EF_AMDGPU_MACH_R600_CEDAR
}
,
1566 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CYPRESS){ "EF_AMDGPU_MACH_R600_CYPRESS", ELF::EF_AMDGPU_MACH_R600_CYPRESS
}
,
1567 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_JUNIPER){ "EF_AMDGPU_MACH_R600_JUNIPER", ELF::EF_AMDGPU_MACH_R600_JUNIPER
}
,
1568 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_REDWOOD){ "EF_AMDGPU_MACH_R600_REDWOOD", ELF::EF_AMDGPU_MACH_R600_REDWOOD
}
,
1569 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_SUMO){ "EF_AMDGPU_MACH_R600_SUMO", ELF::EF_AMDGPU_MACH_R600_SUMO },
1570 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_BARTS){ "EF_AMDGPU_MACH_R600_BARTS", ELF::EF_AMDGPU_MACH_R600_BARTS
}
,
1571 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAICOS){ "EF_AMDGPU_MACH_R600_CAICOS", ELF::EF_AMDGPU_MACH_R600_CAICOS
}
,
1572 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAYMAN){ "EF_AMDGPU_MACH_R600_CAYMAN", ELF::EF_AMDGPU_MACH_R600_CAYMAN
}
,
1573 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_TURKS){ "EF_AMDGPU_MACH_R600_TURKS", ELF::EF_AMDGPU_MACH_R600_TURKS
}
,
1574 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX600){ "EF_AMDGPU_MACH_AMDGCN_GFX600", ELF::EF_AMDGPU_MACH_AMDGCN_GFX600
}
,
1575 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX601){ "EF_AMDGPU_MACH_AMDGCN_GFX601", ELF::EF_AMDGPU_MACH_AMDGCN_GFX601
}
,
1576 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX602){ "EF_AMDGPU_MACH_AMDGCN_GFX602", ELF::EF_AMDGPU_MACH_AMDGCN_GFX602
}
,
1577 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX700){ "EF_AMDGPU_MACH_AMDGCN_GFX700", ELF::EF_AMDGPU_MACH_AMDGCN_GFX700
}
,
1578 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX701){ "EF_AMDGPU_MACH_AMDGCN_GFX701", ELF::EF_AMDGPU_MACH_AMDGCN_GFX701
}
,
1579 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX702){ "EF_AMDGPU_MACH_AMDGCN_GFX702", ELF::EF_AMDGPU_MACH_AMDGCN_GFX702
}
,
1580 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX703){ "EF_AMDGPU_MACH_AMDGCN_GFX703", ELF::EF_AMDGPU_MACH_AMDGCN_GFX703
}
,
1581 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX704){ "EF_AMDGPU_MACH_AMDGCN_GFX704", ELF::EF_AMDGPU_MACH_AMDGCN_GFX704
}
,
1582 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX705){ "EF_AMDGPU_MACH_AMDGCN_GFX705", ELF::EF_AMDGPU_MACH_AMDGCN_GFX705
}
,
1583 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX801){ "EF_AMDGPU_MACH_AMDGCN_GFX801", ELF::EF_AMDGPU_MACH_AMDGCN_GFX801
}
,
1584 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX802){ "EF_AMDGPU_MACH_AMDGCN_GFX802", ELF::EF_AMDGPU_MACH_AMDGCN_GFX802
}
,
1585 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX803){ "EF_AMDGPU_MACH_AMDGCN_GFX803", ELF::EF_AMDGPU_MACH_AMDGCN_GFX803
}
,
1586 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX805){ "EF_AMDGPU_MACH_AMDGCN_GFX805", ELF::EF_AMDGPU_MACH_AMDGCN_GFX805
}
,
1587 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX810){ "EF_AMDGPU_MACH_AMDGCN_GFX810", ELF::EF_AMDGPU_MACH_AMDGCN_GFX810
}
,
1588 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX900){ "EF_AMDGPU_MACH_AMDGCN_GFX900", ELF::EF_AMDGPU_MACH_AMDGCN_GFX900
}
,
1589 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX902){ "EF_AMDGPU_MACH_AMDGCN_GFX902", ELF::EF_AMDGPU_MACH_AMDGCN_GFX902
}
,
1590 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX904){ "EF_AMDGPU_MACH_AMDGCN_GFX904", ELF::EF_AMDGPU_MACH_AMDGCN_GFX904
}
,
1591 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX906){ "EF_AMDGPU_MACH_AMDGCN_GFX906", ELF::EF_AMDGPU_MACH_AMDGCN_GFX906
}
,
1592 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX908){ "EF_AMDGPU_MACH_AMDGCN_GFX908", ELF::EF_AMDGPU_MACH_AMDGCN_GFX908
}
,
1593 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX909){ "EF_AMDGPU_MACH_AMDGCN_GFX909", ELF::EF_AMDGPU_MACH_AMDGCN_GFX909
}
,
1594 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX90A){ "EF_AMDGPU_MACH_AMDGCN_GFX90A", ELF::EF_AMDGPU_MACH_AMDGCN_GFX90A
}
,
1595 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX90C){ "EF_AMDGPU_MACH_AMDGCN_GFX90C", ELF::EF_AMDGPU_MACH_AMDGCN_GFX90C
}
,
1596 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX940){ "EF_AMDGPU_MACH_AMDGCN_GFX940", ELF::EF_AMDGPU_MACH_AMDGCN_GFX940
}
,
1597 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1010){ "EF_AMDGPU_MACH_AMDGCN_GFX1010", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1010
}
,
1598 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1011){ "EF_AMDGPU_MACH_AMDGCN_GFX1011", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1011
}
,
1599 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1012){ "EF_AMDGPU_MACH_AMDGCN_GFX1012", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1012
}
,
1600 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1013){ "EF_AMDGPU_MACH_AMDGCN_GFX1013", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1013
}
,
1601 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1030){ "EF_AMDGPU_MACH_AMDGCN_GFX1030", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1030
}
,
1602 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1031){ "EF_AMDGPU_MACH_AMDGCN_GFX1031", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1031
}
,
1603 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1032){ "EF_AMDGPU_MACH_AMDGCN_GFX1032", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1032
}
,
1604 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1033){ "EF_AMDGPU_MACH_AMDGCN_GFX1033", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1033
}
,
1605 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1034){ "EF_AMDGPU_MACH_AMDGCN_GFX1034", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1034
}
,
1606 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1035){ "EF_AMDGPU_MACH_AMDGCN_GFX1035", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1035
}
,
1607 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1036){ "EF_AMDGPU_MACH_AMDGCN_GFX1036", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1036
}
,
1608 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1100){ "EF_AMDGPU_MACH_AMDGCN_GFX1100", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1100
}
,
1609 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1101){ "EF_AMDGPU_MACH_AMDGCN_GFX1101", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1101
}
,
1610 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1102){ "EF_AMDGPU_MACH_AMDGCN_GFX1102", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1102
}
,
1611 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1103){ "EF_AMDGPU_MACH_AMDGCN_GFX1103", ELF::EF_AMDGPU_MACH_AMDGCN_GFX1103
}
,
1612 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_FEATURE_XNACK_ANY_V4){ "EF_AMDGPU_FEATURE_XNACK_ANY_V4", ELF::EF_AMDGPU_FEATURE_XNACK_ANY_V4
}
,
1613 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_FEATURE_XNACK_OFF_V4){ "EF_AMDGPU_FEATURE_XNACK_OFF_V4", ELF::EF_AMDGPU_FEATURE_XNACK_OFF_V4
}
,
1614 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_FEATURE_XNACK_ON_V4){ "EF_AMDGPU_FEATURE_XNACK_ON_V4", ELF::EF_AMDGPU_FEATURE_XNACK_ON_V4
}
,
1615 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_FEATURE_SRAMECC_ANY_V4){ "EF_AMDGPU_FEATURE_SRAMECC_ANY_V4", ELF::EF_AMDGPU_FEATURE_SRAMECC_ANY_V4
}
,
1616 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_FEATURE_SRAMECC_OFF_V4){ "EF_AMDGPU_FEATURE_SRAMECC_OFF_V4", ELF::EF_AMDGPU_FEATURE_SRAMECC_OFF_V4
}
,
1617 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_FEATURE_SRAMECC_ON_V4){ "EF_AMDGPU_FEATURE_SRAMECC_ON_V4", ELF::EF_AMDGPU_FEATURE_SRAMECC_ON_V4
}
1618};
1619
1620const EnumEntry<unsigned> ElfHeaderRISCVFlags[] = {
1621 ENUM_ENT(EF_RISCV_RVC, "RVC"){ "EF_RISCV_RVC", "RVC", ELF::EF_RISCV_RVC },
1622 ENUM_ENT(EF_RISCV_FLOAT_ABI_SINGLE, "single-float ABI"){ "EF_RISCV_FLOAT_ABI_SINGLE", "single-float ABI", ELF::EF_RISCV_FLOAT_ABI_SINGLE
}
,
1623 ENUM_ENT(EF_RISCV_FLOAT_ABI_DOUBLE, "double-float ABI"){ "EF_RISCV_FLOAT_ABI_DOUBLE", "double-float ABI", ELF::EF_RISCV_FLOAT_ABI_DOUBLE
}
,
1624 ENUM_ENT(EF_RISCV_FLOAT_ABI_QUAD, "quad-float ABI"){ "EF_RISCV_FLOAT_ABI_QUAD", "quad-float ABI", ELF::EF_RISCV_FLOAT_ABI_QUAD
}
,
1625 ENUM_ENT(EF_RISCV_RVE, "RVE"){ "EF_RISCV_RVE", "RVE", ELF::EF_RISCV_RVE },
1626 ENUM_ENT(EF_RISCV_TSO, "TSO"){ "EF_RISCV_TSO", "TSO", ELF::EF_RISCV_TSO },
1627};
1628
1629const EnumEntry<unsigned> ElfHeaderAVRFlags[] = {
1630 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_AVR1){ "EF_AVR_ARCH_AVR1", ELF::EF_AVR_ARCH_AVR1 },
1631 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_AVR2){ "EF_AVR_ARCH_AVR2", ELF::EF_AVR_ARCH_AVR2 },
1632 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_AVR25){ "EF_AVR_ARCH_AVR25", ELF::EF_AVR_ARCH_AVR25 },
1633 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_AVR3){ "EF_AVR_ARCH_AVR3", ELF::EF_AVR_ARCH_AVR3 },
1634 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_AVR31){ "EF_AVR_ARCH_AVR31", ELF::EF_AVR_ARCH_AVR31 },
1635 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_AVR35){ "EF_AVR_ARCH_AVR35", ELF::EF_AVR_ARCH_AVR35 },
1636 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_AVR4){ "EF_AVR_ARCH_AVR4", ELF::EF_AVR_ARCH_AVR4 },
1637 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_AVR5){ "EF_AVR_ARCH_AVR5", ELF::EF_AVR_ARCH_AVR5 },
1638 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_AVR51){ "EF_AVR_ARCH_AVR51", ELF::EF_AVR_ARCH_AVR51 },
1639 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_AVR6){ "EF_AVR_ARCH_AVR6", ELF::EF_AVR_ARCH_AVR6 },
1640 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_AVRTINY){ "EF_AVR_ARCH_AVRTINY", ELF::EF_AVR_ARCH_AVRTINY },
1641 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_XMEGA1){ "EF_AVR_ARCH_XMEGA1", ELF::EF_AVR_ARCH_XMEGA1 },
1642 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_XMEGA2){ "EF_AVR_ARCH_XMEGA2", ELF::EF_AVR_ARCH_XMEGA2 },
1643 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_XMEGA3){ "EF_AVR_ARCH_XMEGA3", ELF::EF_AVR_ARCH_XMEGA3 },
1644 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_XMEGA4){ "EF_AVR_ARCH_XMEGA4", ELF::EF_AVR_ARCH_XMEGA4 },
1645 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_XMEGA5){ "EF_AVR_ARCH_XMEGA5", ELF::EF_AVR_ARCH_XMEGA5 },
1646 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_XMEGA6){ "EF_AVR_ARCH_XMEGA6", ELF::EF_AVR_ARCH_XMEGA6 },
1647 LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_XMEGA7){ "EF_AVR_ARCH_XMEGA7", ELF::EF_AVR_ARCH_XMEGA7 },
1648 ENUM_ENT(EF_AVR_LINKRELAX_PREPARED, "relaxable"){ "EF_AVR_LINKRELAX_PREPARED", "relaxable", ELF::EF_AVR_LINKRELAX_PREPARED
}
,
1649};
1650
1651const EnumEntry<unsigned> ElfHeaderLoongArchFlags[] = {
1652 ENUM_ENT(EF_LOONGARCH_BASE_ABI_ILP32S, "ILP32, SOFT-FLOAT"){ "EF_LOONGARCH_BASE_ABI_ILP32S", "ILP32, SOFT-FLOAT", ELF::EF_LOONGARCH_BASE_ABI_ILP32S
}
,
1653 ENUM_ENT(EF_LOONGARCH_BASE_ABI_ILP32F, "ILP32, SINGLE-FLOAT"){ "EF_LOONGARCH_BASE_ABI_ILP32F", "ILP32, SINGLE-FLOAT", ELF::
EF_LOONGARCH_BASE_ABI_ILP32F }
,
1654 ENUM_ENT(EF_LOONGARCH_BASE_ABI_ILP32D, "ILP32, DOUBLE-FLOAT"){ "EF_LOONGARCH_BASE_ABI_ILP32D", "ILP32, DOUBLE-FLOAT", ELF::
EF_LOONGARCH_BASE_ABI_ILP32D }
,
1655 ENUM_ENT(EF_LOONGARCH_BASE_ABI_LP64S, "LP64, SOFT-FLOAT"){ "EF_LOONGARCH_BASE_ABI_LP64S", "LP64, SOFT-FLOAT", ELF::EF_LOONGARCH_BASE_ABI_LP64S
}
,
1656 ENUM_ENT(EF_LOONGARCH_BASE_ABI_LP64F, "LP64, SINGLE-FLOAT"){ "EF_LOONGARCH_BASE_ABI_LP64F", "LP64, SINGLE-FLOAT", ELF::EF_LOONGARCH_BASE_ABI_LP64F
}
,
1657 ENUM_ENT(EF_LOONGARCH_BASE_ABI_LP64D, "LP64, DOUBLE-FLOAT"){ "EF_LOONGARCH_BASE_ABI_LP64D", "LP64, DOUBLE-FLOAT", ELF::EF_LOONGARCH_BASE_ABI_LP64D
}
,
1658};
1659
1660
1661const EnumEntry<unsigned> ElfSymOtherFlags[] = {
1662 LLVM_READOBJ_ENUM_ENT(ELF, STV_INTERNAL){ "STV_INTERNAL", ELF::STV_INTERNAL },
1663 LLVM_READOBJ_ENUM_ENT(ELF, STV_HIDDEN){ "STV_HIDDEN", ELF::STV_HIDDEN },
1664 LLVM_READOBJ_ENUM_ENT(ELF, STV_PROTECTED){ "STV_PROTECTED", ELF::STV_PROTECTED }
1665};
1666
1667const EnumEntry<unsigned> ElfMipsSymOtherFlags[] = {
1668 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL){ "STO_MIPS_OPTIONAL", ELF::STO_MIPS_OPTIONAL },
1669 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT){ "STO_MIPS_PLT", ELF::STO_MIPS_PLT },
1670 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PIC){ "STO_MIPS_PIC", ELF::STO_MIPS_PIC },
1671 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MICROMIPS){ "STO_MIPS_MICROMIPS", ELF::STO_MIPS_MICROMIPS }
1672};
1673
1674const EnumEntry<unsigned> ElfAArch64SymOtherFlags[] = {
1675 LLVM_READOBJ_ENUM_ENT(ELF, STO_AARCH64_VARIANT_PCS){ "STO_AARCH64_VARIANT_PCS", ELF::STO_AARCH64_VARIANT_PCS }
1676};
1677
1678const EnumEntry<unsigned> ElfMips16SymOtherFlags[] = {
1679 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL){ "STO_MIPS_OPTIONAL", ELF::STO_MIPS_OPTIONAL },
1680 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT){ "STO_MIPS_PLT", ELF::STO_MIPS_PLT },
1681 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MIPS16){ "STO_MIPS_MIPS16", ELF::STO_MIPS_MIPS16 }
1682};
1683
1684const EnumEntry<unsigned> ElfRISCVSymOtherFlags[] = {
1685 LLVM_READOBJ_ENUM_ENT(ELF, STO_RISCV_VARIANT_CC){ "STO_RISCV_VARIANT_CC", ELF::STO_RISCV_VARIANT_CC }};
1686
1687static const char *getElfMipsOptionsOdkType(unsigned Odk) {
1688 switch (Odk) {
1689 LLVM_READOBJ_ENUM_CASE(ELF, ODK_NULL)case ELF::ODK_NULL: return "ODK_NULL";;
1690 LLVM_READOBJ_ENUM_CASE(ELF, ODK_REGINFO)case ELF::ODK_REGINFO: return "ODK_REGINFO";;
1691 LLVM_READOBJ_ENUM_CASE(ELF, ODK_EXCEPTIONS)case ELF::ODK_EXCEPTIONS: return "ODK_EXCEPTIONS";;
1692 LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAD)case ELF::ODK_PAD: return "ODK_PAD";;
1693 LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWPATCH)case ELF::ODK_HWPATCH: return "ODK_HWPATCH";;
1694 LLVM_READOBJ_ENUM_CASE(ELF, ODK_FILL)case ELF::ODK_FILL: return "ODK_FILL";;
1695 LLVM_READOBJ_ENUM_CASE(ELF, ODK_TAGS)case ELF::ODK_TAGS: return "ODK_TAGS";;
1696 LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWAND)case ELF::ODK_HWAND: return "ODK_HWAND";;
1697 LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWOR)case ELF::ODK_HWOR: return "ODK_HWOR";;
1698 LLVM_READOBJ_ENUM_CASE(ELF, ODK_GP_GROUP)case ELF::ODK_GP_GROUP: return "ODK_GP_GROUP";;
1699 LLVM_READOBJ_ENUM_CASE(ELF, ODK_IDENT)case ELF::ODK_IDENT: return "ODK_IDENT";;
1700 LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAGESIZE)case ELF::ODK_PAGESIZE: return "ODK_PAGESIZE";;
1701 default:
1702 return "Unknown";
1703 }
1704}
1705
1706template <typename ELFT>
1707std::pair<const typename ELFT::Phdr *, const typename ELFT::Shdr *>
1708ELFDumper<ELFT>::findDynamic() {
1709 // Try to locate the PT_DYNAMIC header.
1710 const Elf_Phdr *DynamicPhdr = nullptr;
1711 if (Expected<ArrayRef<Elf_Phdr>> PhdrsOrErr = Obj.program_headers()) {
1712 for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
1713 if (Phdr.p_type != ELF::PT_DYNAMIC)
1714 continue;
1715 DynamicPhdr = &Phdr;
1716 break;
1717 }
1718 } else {
1719 reportUniqueWarning(
1720 "unable to read program headers to locate the PT_DYNAMIC segment: " +
1721 toString(PhdrsOrErr.takeError()));
1722 }
1723
1724 // Try to locate the .dynamic section in the sections header table.
1725 const Elf_Shdr *DynamicSec = nullptr;
1726 for (const Elf_Shdr &Sec : cantFail(Obj.sections())) {
1727 if (Sec.sh_type != ELF::SHT_DYNAMIC)
1728 continue;
1729 DynamicSec = &Sec;
1730 break;
1731 }
1732
1733 if (DynamicPhdr && ((DynamicPhdr->p_offset + DynamicPhdr->p_filesz >
1734 ObjF.getMemoryBufferRef().getBufferSize()) ||
1735 (DynamicPhdr->p_offset + DynamicPhdr->p_filesz <
1736 DynamicPhdr->p_offset))) {
1737 reportUniqueWarning(
1738 "PT_DYNAMIC segment offset (0x" +
1739 Twine::utohexstr(DynamicPhdr->p_offset) + ") + file size (0x" +
1740 Twine::utohexstr(DynamicPhdr->p_filesz) +
1741 ") exceeds the size of the file (0x" +
1742 Twine::utohexstr(ObjF.getMemoryBufferRef().getBufferSize()) + ")");
1743 // Don't use the broken dynamic header.
1744 DynamicPhdr = nullptr;
1745 }
1746
1747 if (DynamicPhdr && DynamicSec) {
1748 if (DynamicSec->sh_addr + DynamicSec->sh_size >
1749 DynamicPhdr->p_vaddr + DynamicPhdr->p_memsz ||
1750 DynamicSec->sh_addr < DynamicPhdr->p_vaddr)
1751 reportUniqueWarning(describe(*DynamicSec) +
1752 " is not contained within the "
1753 "PT_DYNAMIC segment");
1754
1755 if (DynamicSec->sh_addr != DynamicPhdr->p_vaddr)
1756 reportUniqueWarning(describe(*DynamicSec) + " is not at the start of "
1757 "PT_DYNAMIC segment");
1758 }
1759
1760 return std::make_pair(DynamicPhdr, DynamicSec);
1761}
1762
1763template <typename ELFT>
1764void ELFDumper<ELFT>::loadDynamicTable() {
1765 const Elf_Phdr *DynamicPhdr;
1766 const Elf_Shdr *DynamicSec;
1767 std::tie(DynamicPhdr, DynamicSec) = findDynamic();
1768 if (!DynamicPhdr && !DynamicSec)
1769 return;
1770
1771 DynRegionInfo FromPhdr(ObjF, *this);
1772 bool IsPhdrTableValid = false;
1773 if (DynamicPhdr) {
1774 // Use cantFail(), because p_offset/p_filesz fields of a PT_DYNAMIC are
1775 // validated in findDynamic() and so createDRI() is not expected to fail.
1776 FromPhdr = cantFail(createDRI(DynamicPhdr->p_offset, DynamicPhdr->p_filesz,
1777 sizeof(Elf_Dyn)));
1778 FromPhdr.SizePrintName = "PT_DYNAMIC size";
1779 FromPhdr.EntSizePrintName = "";
1780 IsPhdrTableValid = !FromPhdr.template getAsArrayRef<Elf_Dyn>().empty();
1781 }
1782
1783 // Locate the dynamic table described in a section header.
1784 // Ignore sh_entsize and use the expected value for entry size explicitly.
1785 // This allows us to dump dynamic sections with a broken sh_entsize
1786 // field.
1787 DynRegionInfo FromSec(ObjF, *this);
1788 bool IsSecTableValid = false;
1789 if (DynamicSec) {
1790 Expected<DynRegionInfo> RegOrErr =
1791 createDRI(DynamicSec->sh_offset, DynamicSec->sh_size, sizeof(Elf_Dyn));
1792 if (RegOrErr) {
1793 FromSec = *RegOrErr;
1794 FromSec.Context = describe(*DynamicSec);
1795 FromSec.EntSizePrintName = "";
1796 IsSecTableValid = !FromSec.template getAsArrayRef<Elf_Dyn>().empty();
1797 } else {
1798 reportUniqueWarning("unable to read the dynamic table from " +
1799 describe(*DynamicSec) + ": " +
1800 toString(RegOrErr.takeError()));
1801 }
1802 }
1803
1804 // When we only have information from one of the SHT_DYNAMIC section header or
1805 // PT_DYNAMIC program header, just use that.
1806 if (!DynamicPhdr || !DynamicSec) {
1807 if ((DynamicPhdr && IsPhdrTableValid) || (DynamicSec && IsSecTableValid)) {
1808 DynamicTable = DynamicPhdr ? FromPhdr : FromSec;
1809 parseDynamicTable();
1810 } else {
1811 reportUniqueWarning("no valid dynamic table was found");
1812 }
1813 return;
1814 }
1815
1816 // At this point we have tables found from the section header and from the
1817 // dynamic segment. Usually they match, but we have to do sanity checks to
1818 // verify that.
1819
1820 if (FromPhdr.Addr != FromSec.Addr)
1821 reportUniqueWarning("SHT_DYNAMIC section header and PT_DYNAMIC "
1822 "program header disagree about "
1823 "the location of the dynamic table");
1824
1825 if (!IsPhdrTableValid && !IsSecTableValid) {
1826 reportUniqueWarning("no valid dynamic table was found");
1827 return;
1828 }
1829
1830 // Information in the PT_DYNAMIC program header has priority over the
1831 // information in a section header.
1832 if (IsPhdrTableValid) {
1833 if (!IsSecTableValid)
1834 reportUniqueWarning(
1835 "SHT_DYNAMIC dynamic table is invalid: PT_DYNAMIC will be used");
1836 DynamicTable = FromPhdr;
1837 } else {
1838 reportUniqueWarning(
1839 "PT_DYNAMIC dynamic table is invalid: SHT_DYNAMIC will be used");
1840 DynamicTable = FromSec;
1841 }
1842
1843 parseDynamicTable();
1844}
1845
1846template <typename ELFT>
1847ELFDumper<ELFT>::ELFDumper(const object::ELFObjectFile<ELFT> &O,
1848 ScopedPrinter &Writer)
1849 : ObjDumper(Writer, O.getFileName()), ObjF(O), Obj(O.getELFFile()),
1850 FileName(O.getFileName()), DynRelRegion(O, *this),
1851 DynRelaRegion(O, *this), DynRelrRegion(O, *this),
1852 DynPLTRelRegion(O, *this), DynSymTabShndxRegion(O, *this),
1853 DynamicTable(O, *this) {
1854 if (!O.IsContentValid())
1855 return;
1856
1857 typename ELFT::ShdrRange Sections = cantFail(Obj.sections());
1858 for (const Elf_Shdr &Sec : Sections) {
1859 switch (Sec.sh_type) {
1860 case ELF::SHT_SYMTAB:
1861 if (!DotSymtabSec)
1862 DotSymtabSec = &Sec;
1863 break;
1864 case ELF::SHT_DYNSYM:
1865 if (!DotDynsymSec)
1866 DotDynsymSec = &Sec;
1867
1868 if (!DynSymRegion) {
1869 Expected<DynRegionInfo> RegOrErr =
1870 createDRI(Sec.sh_offset, Sec.sh_size, Sec.sh_entsize);
1871 if (RegOrErr) {
1872 DynSymRegion = *RegOrErr;
1873 DynSymRegion->Context = describe(Sec);
1874
1875 if (Expected<StringRef> E = Obj.getStringTableForSymtab(Sec))
1876 DynamicStringTable = *E;
1877 else
1878 reportUniqueWarning("unable to get the string table for the " +
1879 describe(Sec) + ": " + toString(E.takeError()));
1880 } else {
1881 reportUniqueWarning("unable to read dynamic symbols from " +
1882 describe(Sec) + ": " +
1883 toString(RegOrErr.takeError()));
1884 }
1885 }
1886 break;
1887 case ELF::SHT_SYMTAB_SHNDX: {
1888 uint32_t SymtabNdx = Sec.sh_link;
1889 if (SymtabNdx >= Sections.size()) {
1890 reportUniqueWarning(
1891 "unable to get the associated symbol table for " + describe(Sec) +
1892 ": sh_link (" + Twine(SymtabNdx) +
1893 ") is greater than or equal to the total number of sections (" +
1894 Twine(Sections.size()) + ")");
1895 continue;
1896 }
1897
1898 if (Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
1899 Obj.getSHNDXTable(Sec)) {
1900 if (!ShndxTables.insert({&Sections[SymtabNdx], *ShndxTableOrErr})
1901 .second)
1902 reportUniqueWarning(
1903 "multiple SHT_SYMTAB_SHNDX sections are linked to " +
1904 describe(Sec));
1905 } else {
1906 reportUniqueWarning(ShndxTableOrErr.takeError());
1907 }
1908 break;
1909 }
1910 case ELF::SHT_GNU_versym:
1911 if (!SymbolVersionSection)
1912 SymbolVersionSection = &Sec;
1913 break;
1914 case ELF::SHT_GNU_verdef:
1915 if (!SymbolVersionDefSection)
1916 SymbolVersionDefSection = &Sec;
1917 break;
1918 case ELF::SHT_GNU_verneed:
1919 if (!SymbolVersionNeedSection)
1920 SymbolVersionNeedSection = &Sec;
1921 break;
1922 case ELF::SHT_LLVM_ADDRSIG:
1923 if (!DotAddrsigSec)
1924 DotAddrsigSec = &Sec;
1925 break;
1926 }
1927 }
1928
1929 loadDynamicTable();
1930}
1931
1932template <typename ELFT> void ELFDumper<ELFT>::parseDynamicTable() {
1933 auto toMappedAddr = [&](uint64_t Tag, uint64_t VAddr) -> const uint8_t * {
1934 auto MappedAddrOrError = Obj.toMappedAddr(VAddr, [&](const Twine &Msg) {
1935 this->reportUniqueWarning(Msg);
1936 return Error::success();
1937 });
1938 if (!MappedAddrOrError) {
1939 this->reportUniqueWarning("unable to parse DT_" +
1940 Obj.getDynamicTagAsString(Tag) + ": " +
1941 llvm::toString(MappedAddrOrError.takeError()));
1942 return nullptr;
1943 }
1944 return MappedAddrOrError.get();
1945 };
1946
1947 const char *StringTableBegin = nullptr;
1948 uint64_t StringTableSize = 0;
1949 Optional<DynRegionInfo> DynSymFromTable;
1950 for (const Elf_Dyn &Dyn : dynamic_table()) {
1951 switch (Dyn.d_tag) {
1952 case ELF::DT_HASH:
1953 HashTable = reinterpret_cast<const Elf_Hash *>(
1954 toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
1955 break;
1956 case ELF::DT_GNU_HASH:
1957 GnuHashTable = reinterpret_cast<const Elf_GnuHash *>(
1958 toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
1959 break;
1960 case ELF::DT_STRTAB:
1961 StringTableBegin = reinterpret_cast<const char *>(
1962 toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
1963 break;
1964 case ELF::DT_STRSZ:
1965 StringTableSize = Dyn.getVal();
1966 break;
1967 case ELF::DT_SYMTAB: {
1968 // If we can't map the DT_SYMTAB value to an address (e.g. when there are
1969 // no program headers), we ignore its value.
1970 if (const uint8_t *VA = toMappedAddr(Dyn.getTag(), Dyn.getPtr())) {
1971 DynSymFromTable.emplace(ObjF, *this);
1972 DynSymFromTable->Addr = VA;
1973 DynSymFromTable->EntSize = sizeof(Elf_Sym);
1974 DynSymFromTable->EntSizePrintName = "";
1975 }
1976 break;
1977 }
1978 case ELF::DT_SYMENT: {
1979 uint64_t Val = Dyn.getVal();
1980 if (Val != sizeof(Elf_Sym))
1981 this->reportUniqueWarning("DT_SYMENT value of 0x" +
1982 Twine::utohexstr(Val) +
1983 " is not the size of a symbol (0x" +
1984 Twine::utohexstr(sizeof(Elf_Sym)) + ")");
1985 break;
1986 }
1987 case ELF::DT_RELA:
1988 DynRelaRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
1989 break;
1990 case ELF::DT_RELASZ:
1991 DynRelaRegion.Size = Dyn.getVal();
1992 DynRelaRegion.SizePrintName = "DT_RELASZ value";
1993 break;
1994 case ELF::DT_RELAENT:
1995 DynRelaRegion.EntSize = Dyn.getVal();
1996 DynRelaRegion.EntSizePrintName = "DT_RELAENT value";
1997 break;
1998 case ELF::DT_SONAME:
1999 SONameOffset = Dyn.getVal();
2000 break;
2001 case ELF::DT_REL:
2002 DynRelRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
2003 break;
2004 case ELF::DT_RELSZ:
2005 DynRelRegion.Size = Dyn.getVal();
2006 DynRelRegion.SizePrintName = "DT_RELSZ value";
2007 break;
2008 case ELF::DT_RELENT:
2009 DynRelRegion.EntSize = Dyn.getVal();
2010 DynRelRegion.EntSizePrintName = "DT_RELENT value";
2011 break;
2012 case ELF::DT_RELR:
2013 case ELF::DT_ANDROID_RELR:
2014 DynRelrRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
2015 break;
2016 case ELF::DT_RELRSZ:
2017 case ELF::DT_ANDROID_RELRSZ:
2018 DynRelrRegion.Size = Dyn.getVal();
2019 DynRelrRegion.SizePrintName = Dyn.d_tag == ELF::DT_RELRSZ
2020 ? "DT_RELRSZ value"
2021 : "DT_ANDROID_RELRSZ value";
2022 break;
2023 case ELF::DT_RELRENT:
2024 case ELF::DT_ANDROID_RELRENT:
2025 DynRelrRegion.EntSize = Dyn.getVal();
2026 DynRelrRegion.EntSizePrintName = Dyn.d_tag == ELF::DT_RELRENT
2027 ? "DT_RELRENT value"
2028 : "DT_ANDROID_RELRENT value";
2029 break;
2030 case ELF::DT_PLTREL:
2031 if (Dyn.getVal() == DT_REL)
2032 DynPLTRelRegion.EntSize = sizeof(Elf_Rel);
2033 else if (Dyn.getVal() == DT_RELA)
2034 DynPLTRelRegion.EntSize = sizeof(Elf_Rela);
2035 else
2036 reportUniqueWarning(Twine("unknown DT_PLTREL value of ") +
2037 Twine((uint64_t)Dyn.getVal()));
2038 DynPLTRelRegion.EntSizePrintName = "PLTREL entry size";
2039 break;
2040 case ELF::DT_JMPREL:
2041 DynPLTRelRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
2042 break;
2043 case ELF::DT_PLTRELSZ:
2044 DynPLTRelRegion.Size = Dyn.getVal();
2045 DynPLTRelRegion.SizePrintName = "DT_PLTRELSZ value";
2046 break;
2047 case ELF::DT_SYMTAB_SHNDX:
2048 DynSymTabShndxRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
2049 DynSymTabShndxRegion.EntSize = sizeof(Elf_Word);
2050 break;
2051 }
2052 }
2053
2054 if (StringTableBegin) {
2055 const uint64_t FileSize = Obj.getBufSize();
2056 const uint64_t Offset = (const uint8_t *)StringTableBegin - Obj.base();
2057 if (StringTableSize > FileSize - Offset)
2058 reportUniqueWarning(
2059 "the dynamic string table at 0x" + Twine::utohexstr(Offset) +
2060 " goes past the end of the file (0x" + Twine::utohexstr(FileSize) +
2061 ") with DT_STRSZ = 0x" + Twine::utohexstr(StringTableSize));
2062 else
2063 DynamicStringTable = StringRef(StringTableBegin, StringTableSize);
2064 }
2065
2066 const bool IsHashTableSupported = getHashTableEntSize() == 4;
2067 if (DynSymRegion) {
2068 // Often we find the information about the dynamic symbol table
2069 // location in the SHT_DYNSYM section header. However, the value in
2070 // DT_SYMTAB has priority, because it is used by dynamic loaders to
2071 // locate .dynsym at runtime. The location we find in the section header
2072 // and the location we find here should match.
2073 if (DynSymFromTable && DynSymFromTable->Addr != DynSymRegion->Addr)
2074 reportUniqueWarning(
2075 createError("SHT_DYNSYM section header and DT_SYMTAB disagree about "
2076 "the location of the dynamic symbol table"));
2077
2078 // According to the ELF gABI: "The number of symbol table entries should
2079 // equal nchain". Check to see if the DT_HASH hash table nchain value
2080 // conflicts with the number of symbols in the dynamic symbol table
2081 // according to the section header.
2082 if (HashTable && IsHashTableSupported) {
2083 if (DynSymRegion->EntSize == 0)
2084 reportUniqueWarning("SHT_DYNSYM section has sh_entsize == 0");
2085 else if (HashTable->nchain != DynSymRegion->Size / DynSymRegion->EntSize)
2086 reportUniqueWarning(
2087 "hash table nchain (" + Twine(HashTable->nchain) +
2088 ") differs from symbol count derived from SHT_DYNSYM section "
2089 "header (" +
2090 Twine(DynSymRegion->Size / DynSymRegion->EntSize) + ")");
2091 }
2092 }
2093
2094 // Delay the creation of the actual dynamic symbol table until now, so that
2095 // checks can always be made against the section header-based properties,
2096 // without worrying about tag order.
2097 if (DynSymFromTable) {
2098 if (!DynSymRegion) {
2099 DynSymRegion = DynSymFromTable;
2100 } else {
2101 DynSymRegion->Addr = DynSymFromTable->Addr;
2102 DynSymRegion->EntSize = DynSymFromTable->EntSize;
2103 DynSymRegion->EntSizePrintName = DynSymFromTable->EntSizePrintName;
2104 }
2105 }
2106
2107 // Derive the dynamic symbol table size from the DT_HASH hash table, if
2108 // present.
2109 if (HashTable && IsHashTableSupported && DynSymRegion) {
2110 const uint64_t FileSize = Obj.getBufSize();
2111 const uint64_t DerivedSize =
2112 (uint64_t)HashTable->nchain * DynSymRegion->EntSize;
2113 const uint64_t Offset = (const uint8_t *)DynSymRegion->Addr - Obj.base();
2114 if (DerivedSize > FileSize - Offset)
2115 reportUniqueWarning(
2116 "the size (0x" + Twine::utohexstr(DerivedSize) +
2117 ") of the dynamic symbol table at 0x" + Twine::utohexstr(Offset) +
2118 ", derived from the hash table, goes past the end of the file (0x" +
2119 Twine::utohexstr(FileSize) + ") and will be ignored");
2120 else
2121 DynSymRegion->Size = HashTable->nchain * DynSymRegion->EntSize;
2122 }
2123}
2124
2125template <typename ELFT> void ELFDumper<ELFT>::printVersionInfo() {
2126 // Dump version symbol section.
2127 printVersionSymbolSection(SymbolVersionSection);
2128
2129 // Dump version definition section.
2130 printVersionDefinitionSection(SymbolVersionDefSection);
2131
2132 // Dump version dependency section.
2133 printVersionDependencySection(SymbolVersionNeedSection);
2134}
2135
2136#define LLVM_READOBJ_DT_FLAG_ENT(prefix, enum) \
2137 { #enum, prefix##_##enum }
2138
2139const EnumEntry<unsigned> ElfDynamicDTFlags[] = {
2140 LLVM_READOBJ_DT_FLAG_ENT(DF, ORIGIN),
2141 LLVM_READOBJ_DT_FLAG_ENT(DF, SYMBOLIC),
2142 LLVM_READOBJ_DT_FLAG_ENT(DF, TEXTREL),
2143 LLVM_READOBJ_DT_FLAG_ENT(DF, BIND_NOW),
2144 LLVM_READOBJ_DT_FLAG_ENT(DF, STATIC_TLS)
2145};
2146
2147const EnumEntry<unsigned> ElfDynamicDTFlags1[] = {
2148 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOW),
2149 LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAL),
2150 LLVM_READOBJ_DT_FLAG_ENT(DF_1, GROUP),
2151 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODELETE),
2152 LLVM_READOBJ_DT_FLAG_ENT(DF_1, LOADFLTR),
2153 LLVM_READOBJ_DT_FLAG_ENT(DF_1, INITFIRST),
2154 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOOPEN),
2155 LLVM_READOBJ_DT_FLAG_ENT(DF_1, ORIGIN),
2156 LLVM_READOBJ_DT_FLAG_ENT(DF_1, DIRECT),
2157 LLVM_READOBJ_DT_FLAG_ENT(DF_1, TRANS),
2158 LLVM_READOBJ_DT_FLAG_ENT(DF_1, INTERPOSE),
2159 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODEFLIB),
2160 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODUMP),
2161 LLVM_READOBJ_DT_FLAG_ENT(DF_1, CONFALT),
2162 LLVM_READOBJ_DT_FLAG_ENT(DF_1, ENDFILTEE),
2163 LLVM_READOBJ_DT_FLAG_ENT(DF_1, DISPRELDNE),
2164 LLVM_READOBJ_DT_FLAG_ENT(DF_1, DISPRELPND),
2165 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODIRECT),
2166 LLVM_READOBJ_DT_FLAG_ENT(DF_1, IGNMULDEF),
2167 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOKSYMS),
2168 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOHDR),
2169 LLVM_READOBJ_DT_FLAG_ENT(DF_1, EDITED),
2170 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NORELOC),
2171 LLVM_READOBJ_DT_FLAG_ENT(DF_1, SYMINTPOSE),
2172 LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAUDIT),
2173 LLVM_READOBJ_DT_FLAG_ENT(DF_1, SINGLETON),
2174 LLVM_READOBJ_DT_FLAG_ENT(DF_1, PIE),
2175};
2176
2177const EnumEntry<unsigned> ElfDynamicDTMipsFlags[] = {
2178 LLVM_READOBJ_DT_FLAG_ENT(RHF, NONE),
2179 LLVM_READOBJ_DT_FLAG_ENT(RHF, QUICKSTART),
2180 LLVM_READOBJ_DT_FLAG_ENT(RHF, NOTPOT),
2181 LLVM_READOBJ_DT_FLAG_ENT(RHS, NO_LIBRARY_REPLACEMENT),
2182 LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_MOVE),
2183 LLVM_READOBJ_DT_FLAG_ENT(RHF, SGI_ONLY),
2184 LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_INIT),
2185 LLVM_READOBJ_DT_FLAG_ENT(RHF, DELTA_C_PLUS_PLUS),
2186 LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_START_INIT),
2187 LLVM_READOBJ_DT_FLAG_ENT(RHF, PIXIE),
2188 LLVM_READOBJ_DT_FLAG_ENT(RHF, DEFAULT_DELAY_LOAD),
2189 LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTART),
2190 LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTARTED),
2191 LLVM_READOBJ_DT_FLAG_ENT(RHF, CORD),
2192 LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_UNRES_UNDEF),
2193 LLVM_READOBJ_DT_FLAG_ENT(RHF, RLD_ORDER_SAFE)
2194};
2195
2196#undef LLVM_READOBJ_DT_FLAG_ENT
2197
2198template <typename T, typename TFlag>
2199void printFlags(T Value, ArrayRef<EnumEntry<TFlag>> Flags, raw_ostream &OS) {
2200 SmallVector<EnumEntry<TFlag>, 10> SetFlags;
2201 for (const EnumEntry<TFlag> &Flag : Flags)
2202 if (Flag.Value != 0 && (Value & Flag.Value) == Flag.Value)
2203 SetFlags.push_back(Flag);
2204
2205 for (const EnumEntry<TFlag> &Flag : SetFlags)
2206 OS << Flag.Name << " ";
2207}
2208
2209template <class ELFT>
2210const typename ELFT::Shdr *
2211ELFDumper<ELFT>::findSectionByName(StringRef Name) const {
2212 for (const Elf_Shdr &Shdr : cantFail(Obj.sections())) {
2213 if (Expected<StringRef> NameOrErr = Obj.getSectionName(Shdr)) {
2214 if (*NameOrErr == Name)
2215 return &Shdr;
2216 } else {
2217 reportUniqueWarning("unable to read the name of " + describe(Shdr) +
2218 ": " + toString(NameOrErr.takeError()));
2219 }
2220 }
2221 return nullptr;
2222}
2223
2224template <class ELFT>
2225std::string ELFDumper<ELFT>::getDynamicEntry(uint64_t Type,
2226 uint64_t Value) const {
2227 auto FormatHexValue = [](uint64_t V) {
2228 std::string Str;
2229 raw_string_ostream OS(Str);
2230 const char *ConvChar =
2231 (opts::Output == opts::GNU) ? "0x%" PRIx64"l" "x" : "0x%" PRIX64"l" "X";
2232 OS << format(ConvChar, V);
2233 return OS.str();
2234 };
2235
2236 auto FormatFlags = [](uint64_t V,
2237 llvm::ArrayRef<llvm::EnumEntry<unsigned int>> Array) {
2238 std::string Str;
2239 raw_string_ostream OS(Str);
2240 printFlags(V, Array, OS);
2241 return OS.str();
2242 };
2243
2244 // Handle custom printing of architecture specific tags
2245 switch (Obj.getHeader().e_machine) {
2246 case EM_AARCH64:
2247 switch (Type) {
2248 case DT_AARCH64_BTI_PLT:
2249 case DT_AARCH64_PAC_PLT:
2250 case DT_AARCH64_VARIANT_PCS:
2251 return std::to_string(Value);
2252 default:
2253 break;
2254 }
2255 break;
2256 case EM_HEXAGON:
2257 switch (Type) {
2258 case DT_HEXAGON_VER:
2259 return std::to_string(Value);
2260 case DT_HEXAGON_SYMSZ:
2261 case DT_HEXAGON_PLT:
2262 return FormatHexValue(Value);
2263 default:
2264 break;
2265 }
2266 break;
2267 case EM_MIPS:
2268 switch (Type) {
2269 case DT_MIPS_RLD_VERSION:
2270 case DT_MIPS_LOCAL_GOTNO:
2271 case DT_MIPS_SYMTABNO:
2272 case DT_MIPS_UNREFEXTNO:
2273 return std::to_string(Value);
2274 case DT_MIPS_TIME_STAMP:
2275 case DT_MIPS_ICHECKSUM:
2276 case DT_MIPS_IVERSION:
2277 case DT_MIPS_BASE_ADDRESS:
2278 case DT_MIPS_MSYM:
2279 case DT_MIPS_CONFLICT:
2280 case DT_MIPS_LIBLIST:
2281 case DT_MIPS_CONFLICTNO:
2282 case DT_MIPS_LIBLISTNO:
2283 case DT_MIPS_GOTSYM:
2284 case DT_MIPS_HIPAGENO:
2285 case DT_MIPS_RLD_MAP:
2286 case DT_MIPS_DELTA_CLASS:
2287 case DT_MIPS_DELTA_CLASS_NO:
2288 case DT_MIPS_DELTA_INSTANCE:
2289 case DT_MIPS_DELTA_RELOC:
2290 case DT_MIPS_DELTA_RELOC_NO:
2291 case DT_MIPS_DELTA_SYM:
2292 case DT_MIPS_DELTA_SYM_NO:
2293 case DT_MIPS_DELTA_CLASSSYM:
2294 case DT_MIPS_DELTA_CLASSSYM_NO:
2295 case DT_MIPS_CXX_FLAGS:
2296 case DT_MIPS_PIXIE_INIT:
2297 case DT_MIPS_SYMBOL_LIB:
2298 case DT_MIPS_LOCALPAGE_GOTIDX:
2299 case DT_MIPS_LOCAL_GOTIDX:
2300 case DT_MIPS_HIDDEN_GOTIDX:
2301 case DT_MIPS_PROTECTED_GOTIDX:
2302 case DT_MIPS_OPTIONS:
2303 case DT_MIPS_INTERFACE:
2304 case DT_MIPS_DYNSTR_ALIGN:
2305 case DT_MIPS_INTERFACE_SIZE:
2306 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
2307 case DT_MIPS_PERF_SUFFIX:
2308 case DT_MIPS_COMPACT_SIZE:
2309 case DT_MIPS_GP_VALUE:
2310 case DT_MIPS_AUX_DYNAMIC:
2311 case DT_MIPS_PLTGOT:
2312 case DT_MIPS_RWPLT:
2313 case DT_MIPS_RLD_MAP_REL:
2314 case DT_MIPS_XHASH:
2315 return FormatHexValue(Value);
2316 case DT_MIPS_FLAGS:
2317 return FormatFlags(Value, makeArrayRef(ElfDynamicDTMipsFlags));
2318 default:
2319 break;
2320 }
2321 break;
2322 default:
2323 break;
2324 }
2325
2326 switch (Type) {
2327 case DT_PLTREL:
2328 if (Value == DT_REL)
2329 return "REL";
2330 if (Value == DT_RELA)
2331 return "RELA";
2332 [[fallthrough]];
2333 case DT_PLTGOT:
2334 case DT_HASH:
2335 case DT_STRTAB:
2336 case DT_SYMTAB:
2337 case DT_RELA:
2338 case DT_INIT:
2339 case DT_FINI:
2340 case DT_REL:
2341 case DT_JMPREL:
2342 case DT_INIT_ARRAY:
2343 case DT_FINI_ARRAY:
2344 case DT_PREINIT_ARRAY:
2345 case DT_DEBUG:
2346 case DT_VERDEF:
2347 case DT_VERNEED:
2348 case DT_VERSYM:
2349 case DT_GNU_HASH:
2350 case DT_NULL:
2351 return FormatHexValue(Value);
2352 case DT_RELACOUNT:
2353 case DT_RELCOUNT:
2354 case DT_VERDEFNUM:
2355 case DT_VERNEEDNUM:
2356 return std::to_string(Value);
2357 case DT_PLTRELSZ:
2358 case DT_RELASZ:
2359 case DT_RELAENT:
2360 case DT_STRSZ:
2361 case DT_SYMENT:
2362 case DT_RELSZ:
2363 case DT_RELENT:
2364 case DT_INIT_ARRAYSZ:
2365 case DT_FINI_ARRAYSZ:
2366 case DT_PREINIT_ARRAYSZ:
2367 case DT_RELRSZ:
2368 case DT_RELRENT:
2369 case DT_ANDROID_RELSZ:
2370 case DT_ANDROID_RELASZ:
2371 return std::to_string(Value) + " (bytes)";
2372 case DT_NEEDED:
2373 case DT_SONAME:
2374 case DT_AUXILIARY:
2375 case DT_USED:
2376 case DT_FILTER:
2377 case DT_RPATH:
2378 case DT_RUNPATH: {
2379 const std::map<uint64_t, const char *> TagNames = {
2380 {DT_NEEDED, "Shared library"}, {DT_SONAME, "Library soname"},
2381 {DT_AUXILIARY, "Auxiliary library"}, {DT_USED, "Not needed object"},
2382 {DT_FILTER, "Filter library"}, {DT_RPATH, "Library rpath"},
2383 {DT_RUNPATH, "Library runpath"},
2384 };
2385
2386 return (Twine(TagNames.at(Type)) + ": [" + getDynamicString(Value) + "]")
2387 .str();
2388 }
2389 case DT_FLAGS:
2390 return FormatFlags(Value, makeArrayRef(ElfDynamicDTFlags));
2391 case DT_FLAGS_1:
2392 return FormatFlags(Value, makeArrayRef(ElfDynamicDTFlags1));
2393 default:
2394 return FormatHexValue(Value);
2395 }
2396}
2397
2398template <class ELFT>
2399StringRef ELFDumper<ELFT>::getDynamicString(uint64_t Value) const {
2400 if (DynamicStringTable.empty() && !DynamicStringTable.data()) {
2401 reportUniqueWarning("string table was not found");
2402 return "<?>";
2403 }
2404
2405 auto WarnAndReturn = [this](const Twine &Msg, uint64_t Offset) {
2406 reportUniqueWarning("string table at offset 0x" + Twine::utohexstr(Offset) +
2407 Msg);
2408 return "<?>";
2409 };
2410
2411 const uint64_t FileSize = Obj.getBufSize();
2412 const uint64_t Offset =
2413 (const uint8_t *)DynamicStringTable.data() - Obj.base();
2414 if (DynamicStringTable.size() > FileSize - Offset)
2415 return WarnAndReturn(" with size 0x" +
2416 Twine::utohexstr(DynamicStringTable.size()) +
2417 " goes past the end of the file (0x" +
2418 Twine::utohexstr(FileSize) + ")",
2419 Offset);
2420
2421 if (Value >= DynamicStringTable.size())
2422 return WarnAndReturn(
2423 ": unable to read the string at 0x" + Twine::utohexstr(Offset + Value) +
2424 ": it goes past the end of the table (0x" +
2425 Twine::utohexstr(Offset + DynamicStringTable.size()) + ")",
2426 Offset);
2427
2428 if (DynamicStringTable.back() != '\0')
2429 return WarnAndReturn(": unable to read the string at 0x" +
2430 Twine::utohexstr(Offset + Value) +
2431 ": the string table is not null-terminated",
2432 Offset);
2433
2434 return DynamicStringTable.data() + Value;
2435}
2436
2437template <class ELFT> void ELFDumper<ELFT>::printUnwindInfo() {
2438 DwarfCFIEH::PrinterContext<ELFT> Ctx(W, ObjF);
2439 Ctx.printUnwindInformation();
2440}
2441
2442// The namespace is needed to fix the compilation with GCC older than 7.0+.
2443namespace {
2444template <> void ELFDumper<ELF32LE>::printUnwindInfo() {
2445 if (Obj.getHeader().e_machine == EM_ARM) {
2446 ARM::EHABI::PrinterContext<ELF32LE> Ctx(W, Obj, ObjF.getFileName(),
2447 DotSymtabSec);
2448 Ctx.PrintUnwindInformation();
2449 }
2450 DwarfCFIEH::PrinterContext<ELF32LE> Ctx(W, ObjF);
2451 Ctx.printUnwindInformation();
2452}
2453} // namespace
2454
2455template <class ELFT> void ELFDumper<ELFT>::printNeededLibraries() {
2456 ListScope D(W, "NeededLibraries");
2457
2458 std::vector<StringRef> Libs;
2459 for (const auto &Entry : dynamic_table())
2460 if (Entry.d_tag == ELF::DT_NEEDED)
2461 Libs.push_back(getDynamicString(Entry.d_un.d_val));
2462
2463 llvm::sort(Libs);
2464
2465 for (StringRef L : Libs)
2466 W.startLine() << L << "\n";
2467}
2468
2469template <class ELFT>
2470static Error checkHashTable(const ELFDumper<ELFT> &Dumper,
2471 const typename ELFT::Hash *H,
2472 bool *IsHeaderValid = nullptr) {
2473 const ELFFile<ELFT> &Obj = Dumper.getElfObject().getELFFile();
2474 const uint64_t SecOffset = (const uint8_t *)H - Obj.base();
2475 if (Dumper.getHashTableEntSize() == 8) {
2476 auto It = llvm::find_if(ElfMachineType, [&](const EnumEntry<unsigned> &E) {
2477 return E.Value == Obj.getHeader().e_machine;
2478 });
2479 if (IsHeaderValid)
2480 *IsHeaderValid = false;
2481 return createError("the hash table at 0x" + Twine::utohexstr(SecOffset) +
2482 " is not supported: it contains non-standard 8 "
2483 "byte entries on " +
2484 It->AltName + " platform");
2485 }
2486
2487 auto MakeError = [&](const Twine &Msg = "") {
2488 return createError("the hash table at offset 0x" +
2489 Twine::utohexstr(SecOffset) +
2490 " goes past the end of the file (0x" +
2491 Twine::utohexstr(Obj.getBufSize()) + ")" + Msg);
2492 };
2493
2494 // Each SHT_HASH section starts from two 32-bit fields: nbucket and nchain.
2495 const unsigned HeaderSize = 2 * sizeof(typename ELFT::Word);
2496
2497 if (IsHeaderValid)
2498 *IsHeaderValid = Obj.getBufSize() - SecOffset >= HeaderSize;
2499
2500 if (Obj.getBufSize() - SecOffset < HeaderSize)
2501 return MakeError();
2502
2503 if (Obj.getBufSize() - SecOffset - HeaderSize <
2504 ((uint64_t)H->nbucket + H->nchain) * sizeof(typename ELFT::Word))
2505 return MakeError(", nbucket = " + Twine(H->nbucket) +
2506 ", nchain = " + Twine(H->nchain));
2507 return Error::success();
2508}
2509
2510template <class ELFT>
2511static Error checkGNUHashTable(const ELFFile<ELFT> &Obj,
2512 const typename ELFT::GnuHash *GnuHashTable,
2513 bool *IsHeaderValid = nullptr) {
2514 const uint8_t *TableData = reinterpret_cast<const uint8_t *>(GnuHashTable);
2515 assert(TableData >= Obj.base() && TableData < Obj.base() + Obj.getBufSize() &&(static_cast <bool> (TableData >= Obj.base() &&
TableData < Obj.base() + Obj.getBufSize() && "GnuHashTable must always point to a location inside the file"
) ? void (0) : __assert_fail ("TableData >= Obj.base() && TableData < Obj.base() + Obj.getBufSize() && \"GnuHashTable must always point to a location inside the file\""
, "llvm/tools/llvm-readobj/ELFDumper.cpp", 2516, __extension__
__PRETTY_FUNCTION__))
2516 "GnuHashTable must always point to a location inside the file")(static_cast <bool> (TableData >= Obj.base() &&
TableData < Obj.base() + Obj.getBufSize() && "GnuHashTable must always point to a location inside the file"
) ? void (0) : __assert_fail ("TableData >= Obj.base() && TableData < Obj.base() + Obj.getBufSize() && \"GnuHashTable must always point to a location inside the file\""
, "llvm/tools/llvm-readobj/ELFDumper.cpp", 2516, __extension__
__PRETTY_FUNCTION__))
;
2517
2518 uint64_t TableOffset = TableData - Obj.base();
2519 if (IsHeaderValid)
2520 *IsHeaderValid = TableOffset + /*Header size:*/ 16 < Obj.getBufSize();
2521 if (TableOffset + 16 + (uint64_t)GnuHashTable->nbuckets * 4 +
2522 (uint64_t)GnuHashTable->maskwords * sizeof(typename ELFT::Off) >=
2523 Obj.getBufSize())
2524 return createError("unable to dump the SHT_GNU_HASH "
2525 "section at 0x" +
2526 Twine::utohexstr(TableOffset) +
2527 ": it goes past the end of the file");
2528 return Error::success();
2529}
2530
2531template <typename ELFT> void ELFDumper<ELFT>::printHashTable() {
2532 DictScope D(W, "HashTable");
2533 if (!HashTable)
2534 return;
2535
2536 bool IsHeaderValid;
2537 Error Err = checkHashTable(*this, HashTable, &IsHeaderValid);
2538 if (IsHeaderValid) {
2539 W.printNumber("Num Buckets", HashTable->nbucket);
2540 W.printNumber("Num Chains", HashTable->nchain);
2541 }
2542
2543 if (Err) {
2544 reportUniqueWarning(std::move(Err));
2545 return;
2546 }
2547
2548 W.printList("Buckets", HashTable->buckets());
2549 W.printList("Chains", HashTable->chains());
2550}
2551
2552template <class ELFT>
2553static Expected<ArrayRef<typename ELFT::Word>>
2554getGnuHashTableChains(Optional<DynRegionInfo> DynSymRegion,
2555 const typename ELFT::GnuHash *GnuHashTable) {
2556 if (!DynSymRegion)
2557 return createError("no dynamic symbol table found");
2558
2559 ArrayRef<typename ELFT::Sym> DynSymTable =
2560 DynSymRegion->template getAsArrayRef<typename ELFT::Sym>();
2561 size_t NumSyms = DynSymTable.size();
2562 if (!NumSyms)
2563 return createError("the dynamic symbol table is empty");
2564
2565 if (GnuHashTable->symndx < NumSyms)
2566 return GnuHashTable->values(NumSyms);
2567
2568 // A normal empty GNU hash table section produced by linker might have
2569 // symndx set to the number of dynamic symbols + 1 (for the zero symbol)
2570 // and have dummy null values in the Bloom filter and in the buckets
2571 // vector (or no values at all). It happens because the value of symndx is not
2572 // important for dynamic loaders when the GNU hash table is empty. They just
2573 // skip the whole object during symbol lookup. In such cases, the symndx value
2574 // is irrelevant and we should not report a warning.
2575 ArrayRef<typename ELFT::Word> Buckets = GnuHashTable->buckets();
2576 if (!llvm::all_of(Buckets, [](typename ELFT::Word V) { return V == 0; }))
2577 return createError(
2578 "the first hashed symbol index (" + Twine(GnuHashTable->symndx) +
2579 ") is greater than or equal to the number of dynamic symbols (" +
2580 Twine(NumSyms) + ")");
2581 // There is no way to represent an array of (dynamic symbols count - symndx)
2582 // length.
2583 return ArrayRef<typename ELFT::Word>();
2584}
2585
2586template <typename ELFT>
2587void ELFDumper<ELFT>::printGnuHashTable() {
2588 DictScope D(W, "GnuHashTable");
2589 if (!GnuHashTable)
2590 return;
2591
2592 bool IsHeaderValid;
2593 Error Err = checkGNUHashTable<ELFT>(Obj, GnuHashTable, &IsHeaderValid);
2594 if (IsHeaderValid) {
2595 W.printNumber("Num Buckets", GnuHashTable->nbuckets);
2596 W.printNumber("First Hashed Symbol Index", GnuHashTable->symndx);
2597 W.printNumber("Num Mask Words", GnuHashTable->maskwords);
2598 W.printNumber("Shift Count", GnuHashTable->shift2);
2599 }
2600
2601 if (Err) {
2602 reportUniqueWarning(std::move(Err));
2603 return;
2604 }
2605
2606 ArrayRef<typename ELFT::Off> BloomFilter = GnuHashTable->filter();
2607 W.printHexList("Bloom Filter", BloomFilter);
2608
2609 ArrayRef<Elf_Word> Buckets = GnuHashTable->buckets();
2610 W.printList("Buckets", Buckets);
2611
2612 Expected<ArrayRef<Elf_Word>> Chains =
2613 getGnuHashTableChains<ELFT>(DynSymRegion, GnuHashTable);
2614 if (!Chains) {
2615 reportUniqueWarning("unable to dump 'Values' for the SHT_GNU_HASH "
2616 "section: " +
2617 toString(Chains.takeError()));
2618 return;
2619 }
2620
2621 W.printHexList("Values", *Chains);
2622}
2623
2624template <typename ELFT> void ELFDumper<ELFT>::printLoadName() {
2625 StringRef SOName = "<Not found>";
2626 if (SONameOffset)
2627 SOName = getDynamicString(*SONameOffset);
2628 W.printString("LoadName", SOName);
2629}
2630
2631template <class ELFT> void ELFDumper<ELFT>::printArchSpecificInfo() {
2632 switch (Obj.getHeader().e_machine) {
2633 case EM_ARM:
2634 if (Obj.isLE())
2635 printAttributes(ELF::SHT_ARM_ATTRIBUTES,
2636 std::make_unique<ARMAttributeParser>(&W),
2637 support::little);
2638 else
2639 reportUniqueWarning("attribute printing not implemented for big-endian "
2640 "ARM objects");
2641 break;
2642 case EM_RISCV:
2643 if (Obj.isLE())
2644 printAttributes(ELF::SHT_RISCV_ATTRIBUTES,
2645 std::make_unique<RISCVAttributeParser>(&W),
2646 support::little);
2647 else
2648 reportUniqueWarning("attribute printing not implemented for big-endian "
2649 "RISC-V objects");
2650 break;
2651 case EM_MSP430:
2652 printAttributes(ELF::SHT_MSP430_ATTRIBUTES,
2653 std::make_unique<MSP430AttributeParser>(&W),
2654 support::little);
2655 break;
2656 case EM_MIPS: {
2657 printMipsABIFlags();
2658 printMipsOptions();
2659 printMipsReginfo();
2660 MipsGOTParser<ELFT> Parser(*this);
2661 if (Error E = Parser.findGOT(dynamic_table(), dynamic_symbols()))
2662 reportUniqueWarning(std::move(E));
2663 else if (!Parser.isGotEmpty())
2664 printMipsGOT(Parser);
2665
2666 if (Error E = Parser.findPLT(dynamic_table()))
2667 reportUniqueWarning(std::move(E));
2668 else if (!Parser.isPltEmpty())
2669 printMipsPLT(Parser);
2670 break;
2671 }
2672 default:
2673 break;
2674 }
2675}
2676
2677template <class ELFT>
2678void ELFDumper<ELFT>::printAttributes(
2679 unsigned AttrShType, std::unique_ptr<ELFAttributeParser> AttrParser,
2680 support::endianness Endianness) {
2681 assert((AttrShType != ELF::SHT_NULL) && AttrParser &&(static_cast <bool> ((AttrShType != ELF::SHT_NULL) &&
AttrParser && "Incomplete ELF attribute implementation"
) ? void (0) : __assert_fail ("(AttrShType != ELF::SHT_NULL) && AttrParser && \"Incomplete ELF attribute implementation\""
, "llvm/tools/llvm-readobj/ELFDumper.cpp", 2682, __extension__
__PRETTY_FUNCTION__))
2682 "Incomplete ELF attribute implementation")(static_cast <bool> ((AttrShType != ELF::SHT_NULL) &&
AttrParser && "Incomplete ELF attribute implementation"
) ? void (0) : __assert_fail ("(AttrShType != ELF::SHT_NULL) && AttrParser && \"Incomplete ELF attribute implementation\""
, "llvm/tools/llvm-readobj/ELFDumper.cpp", 2682, __extension__
__PRETTY_FUNCTION__))
;
2683 DictScope BA(W, "BuildAttributes");
2684 for (const Elf_Shdr &Sec : cantFail(Obj.sections())) {
2685 if (Sec.sh_type != AttrShType)
2686 continue;
2687
2688 ArrayRef<uint8_t> Contents;
2689 if (Expected<ArrayRef<uint8_t>> ContentOrErr =
2690 Obj.getSectionContents(Sec)) {
2691 Contents = *ContentOrErr;
2692 if (Contents.empty()) {
2693 reportUniqueWarning("the " + describe(Sec) + " is empty");
2694 continue;
2695 }
2696 } else {
2697 reportUniqueWarning("unable to read the content of the " + describe(Sec) +
2698 ": " + toString(ContentOrErr.takeError()));
2699 continue;
2700 }
2701
2702 W.printHex("FormatVersion", Contents[0]);
2703
2704 if (Error E = AttrParser->parse(Contents, Endianness))
2705 reportUniqueWarning("unable to dump attributes from the " +
2706 describe(Sec) + ": " + toString(std::move(E)));
2707 }
2708}
2709
2710namespace {
2711
2712template <class ELFT> class MipsGOTParser {
2713public:
2714 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)using Elf_Addr = typename ELFT::Addr; using Elf_Off = typename
ELFT::Off; using Elf_Half = typename ELFT::Half; using Elf_Word
= typename ELFT::Word; using Elf_Sword = typename ELFT::Sword
; using Elf_Xword = typename ELFT::Xword; using Elf_Sxword = typename
ELFT::Sxword; using uintX_t = typename ELFT::uint; using Elf_Ehdr
= typename ELFT::Ehdr; using Elf_Shdr = typename ELFT::Shdr;
using Elf_Sym = typename ELFT::Sym; using Elf_Dyn = typename
ELFT::Dyn; using Elf_Phdr = typename ELFT::Phdr; using Elf_Rel
= typename ELFT::Rel; using Elf_Rela = typename ELFT::Rela; using
Elf_Relr = typename ELFT::Relr; using Elf_Verdef = typename ELFT
::Verdef; using Elf_Verdaux = typename ELFT::Verdaux; using Elf_Verneed
= typename ELFT::Verneed; using Elf_Vernaux = typename ELFT::
Vernaux; using Elf_Versym = typename ELFT::Versym; using Elf_Hash
= typename ELFT::Hash; using Elf_GnuHash = typename ELFT::GnuHash
; using Elf_Nhdr = typename ELFT::Nhdr; using Elf_Note = typename
ELFT::Note; using Elf_Note_Iterator = typename ELFT::NoteIterator
; using Elf_CGProfile = typename ELFT::CGProfile; using Elf_Dyn_Range
= typename ELFT::DynRange; using Elf_Shdr_Range = typename ELFT
::ShdrRange; using Elf_Sym_Range = typename ELFT::SymRange; using
Elf_Rel_Range = typename ELFT::RelRange; using Elf_Rela_Range
= typename ELFT::RelaRange; using Elf_Relr_Range = typename ELFT
::RelrRange; using Elf_Phdr_Range = typename ELFT::PhdrRange;
2715 using Entry = typename ELFT::Addr;
2716 using Entries = ArrayRef<Entry>;
2717
2718 const bool IsStatic;
2719 const ELFFile<ELFT> &Obj;
2720 const ELFDumper<ELFT> &Dumper;
2721
2722 MipsGOTParser(const ELFDumper<ELFT> &D);
2723 Error findGOT(Elf_Dyn_Range DynTable, Elf_Sym_Range DynSyms);
2724 Error findPLT(Elf_Dyn_Range DynTable);
2725
2726 bool isGotEmpty() const { return GotEntries.empty(); }
2727 bool isPltEmpty() const { return PltEntries.empty(); }
2728
2729 uint64_t getGp() const;
2730
2731 const Entry *getGotLazyResolver() const;
2732 const Entry *getGotModulePointer() const;
2733 const Entry *getPltLazyResolver() const;
2734 const Entry *getPltModulePointer() const;
2735
2736 Entries getLocalEntries() const;
2737 Entries getGlobalEntries() const;
2738 Entries getOtherEntries() const;
2739 Entries getPltEntries() const;
2740
2741 uint64_t getGotAddress(const Entry * E) const;
2742 int64_t getGotOffset(const Entry * E) const;
2743 const Elf_Sym *getGotSym(const Entry *E) const;
2744
2745 uint64_t getPltAddress(const Entry * E) const;
2746 const Elf_Sym *getPltSym(const Entry *E) const;
2747
2748 StringRef getPltStrTable() const { return PltStrTable; }
2749 const Elf_Shdr *getPltSymTable() const { return PltSymTable; }
2750
2751private:
2752 const Elf_Shdr *GotSec;
2753 size_t LocalNum;
2754 size_t GlobalNum;
2755
2756 const Elf_Shdr *PltSec;
2757 const Elf_Shdr *PltRelSec;
2758 const Elf_Shdr *PltSymTable;
2759 StringRef FileName;
2760
2761 Elf_Sym_Range GotDynSyms;
2762 StringRef PltStrTable;
2763
2764 Entries GotEntries;
2765 Entries PltEntries;
2766};
2767
2768} // end anonymous namespace
2769
2770template <class ELFT>
2771MipsGOTParser<ELFT>::MipsGOTParser(const ELFDumper<ELFT> &D)
2772 : IsStatic(D.dynamic_table().empty()), Obj(D.getElfObject().getELFFile()),
2773 Dumper(D), GotSec(nullptr), LocalNum(0), GlobalNum(0), PltSec(nullptr),
2774 PltRelSec(nullptr), PltSymTable(nullptr),
2775 FileName(D.getElfObject().getFileName()) {}
2776
2777template <class ELFT>
2778Error MipsGOTParser<ELFT>::findGOT(Elf_Dyn_Range DynTable,
2779 Elf_Sym_Range DynSyms) {
2780 // See "Global Offset Table" in Chapter 5 in the following document
2781 // for detailed GOT description.
2782 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
2783
2784 // Find static GOT secton.
2785 if (IsStatic) {
2786 GotSec = Dumper.findSectionByName(".got");
2787 if (!GotSec)
2788 return Error::success();
2789
2790 ArrayRef<uint8_t> Content =
2791 unwrapOrError(FileName, Obj.getSectionContents(*GotSec));
2792 GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()),
2793 Content.size() / sizeof(Entry));
2794 LocalNum = GotEntries.size();
2795 return Error::success();
2796 }
2797
2798 // Lookup dynamic table tags which define the GOT layout.
2799 Optional<uint64_t> DtPltGot;
2800 Optional<uint64_t> DtLocalGotNum;
2801 Optional<uint64_t> DtGotSym;
2802 for (const auto &Entry : DynTable) {
2803 switch (Entry.getTag()) {
2804 case ELF::DT_PLTGOT:
2805 DtPltGot = Entry.getVal();
2806 break;
2807 case ELF::DT_MIPS_LOCAL_GOTNO:
2808 DtLocalGotNum = Entry.getVal();
2809 break;
2810 case ELF::DT_MIPS_GOTSYM:
2811 DtGotSym = Entry.getVal();
2812 break;
2813 }
2814 }
2815
2816 if (!DtPltGot && !DtLocalGotNum && !DtGotSym)
2817 return Error::success();
2818
2819 if (!DtPltGot)
2820 return createError("cannot find PLTGOT dynamic tag");
2821 if (!DtLocalGotNum)
2822 return createError("cannot find MIPS_LOCAL_GOTNO dynamic tag");
2823 if (!DtGotSym)
2824 return createError("cannot find MIPS_GOTSYM dynamic tag");
2825
2826 size_t DynSymTotal = DynSyms.size();
2827 if (*DtGotSym > DynSymTotal)
2828 return createError("DT_MIPS_GOTSYM value (" + Twine(*DtGotSym) +
2829 ") exceeds the number of dynamic symbols (" +
2830 Twine(DynSymTotal) + ")");
2831
2832 GotSec = findNotEmptySectionByAddress(Obj, FileName, *DtPltGot);
2833 if (!GotSec)
2834 return createError("there is no non-empty GOT section at 0x" +
2835 Twine::utohexstr(*DtPltGot));
2836
2837 LocalNum = *DtLocalGotNum;
2838 GlobalNum = DynSymTotal - *DtGotSym;
2839
2840 ArrayRef<uint8_t> Content =
2841 unwrapOrError(FileName, Obj.getSectionContents(*GotSec));
2842 GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()),
2843 Content.size() / sizeof(Entry));
2844 GotDynSyms = DynSyms.drop_front(*DtGotSym);
2845
2846 return Error::success();
2847}
2848
2849template <class ELFT>
2850Error MipsGOTParser<ELFT>::findPLT(Elf_Dyn_Range DynTable) {
2851 // Lookup dynamic table tags which define the PLT layout.
2852 Optional<uint64_t> DtMipsPltGot;
2853 Optional<uint64_t> DtJmpRel;
2854 for (const auto &Entry : DynTable) {
2855 switch (Entry.getTag()) {
2856 case ELF::DT_MIPS_PLTGOT:
2857 DtMipsPltGot = Entry.getVal();
2858 break;
2859 case ELF::DT_JMPREL:
2860 DtJmpRel = Entry.getVal();
2861 break;
2862 }
2863 }
2864
2865 if (!DtMipsPltGot && !DtJmpRel)
2866 return Error::success();
2867
2868 // Find PLT section.
2869 if (!DtMipsPltGot)
2870 return createError("cannot find MIPS_PLTGOT dynamic tag");
2871 if (!DtJmpRel)
2872 return createError("cannot find JMPREL dynamic tag");
2873
2874 PltSec = findNotEmptySectionByAddress(Obj, FileName, *DtMipsPltGot);
2875 if (!PltSec)
2876 return createError("there is no non-empty PLTGOT section at 0x" +
2877 Twine::utohexstr(*DtMipsPltGot));
2878
2879 PltRelSec = findNotEmptySectionByAddress(Obj, FileName, *DtJmpRel);
2880 if (!PltRelSec)
2881 return createError("there is no non-empty RELPLT section at 0x" +
2882 Twine::utohexstr(*DtJmpRel));
2883
2884 if (Expected<ArrayRef<uint8_t>> PltContentOrErr =
2885 Obj.getSectionContents(*PltSec))
2886 PltEntries =
2887 Entries(reinterpret_cast<const Entry *>(PltContentOrErr->data()),
2888 PltContentOrErr->size() / sizeof(Entry));
2889 else
2890 return createError("unable to read PLTGOT section content: " +
2891 toString(PltContentOrErr.takeError()));
2892
2893 if (Expected<const Elf_Shdr *> PltSymTableOrErr =
2894 Obj.getSection(PltRelSec->sh_link))
2895 PltSymTable = *PltSymTableOrErr;
2896 else
2897 return createError("unable to get a symbol table linked to the " +
2898 describe(Obj, *PltRelSec) + ": " +
2899 toString(PltSymTableOrErr.takeError()));
2900
2901 if (Expected<StringRef> StrTabOrErr =
2902 Obj.getStringTableForSymtab(*PltSymTable))
2903 PltStrTable = *StrTabOrErr;
2904 else
2905 return createError("unable to get a string table for the " +
2906 describe(Obj, *PltSymTable) + ": " +
2907 toString(StrTabOrErr.takeError()));
2908
2909 return Error::success();
2910}
2911
2912template <class ELFT> uint64_t MipsGOTParser<ELFT>::getGp() const {
2913 return GotSec->sh_addr + 0x7ff0;
2914}
2915
2916template <class ELFT>
2917const typename MipsGOTParser<ELFT>::Entry *
2918MipsGOTParser<ELFT>::getGotLazyResolver() const {
2919 return LocalNum > 0 ? &GotEntries[0] : nullptr;
2920}
2921
2922template <class ELFT>
2923const typename MipsGOTParser<ELFT>::Entry *
2924MipsGOTParser<ELFT>::getGotModulePointer() const {
2925 if (LocalNum < 2)
2926 return nullptr;
2927 const Entry &E = GotEntries[1];
2928 if ((E >> (sizeof(Entry) * 8 - 1)) == 0)
2929 return nullptr;
2930 return &E;
2931}
2932
2933template <class ELFT>
2934typename MipsGOTParser<ELFT>::Entries
2935MipsGOTParser<ELFT>::getLocalEntries() const {
2936 size_t Skip = getGotModulePointer() ? 2 : 1;
2937 if (LocalNum - Skip <= 0)
2938 return Entries();
2939 return GotEntries.slice(Skip, LocalNum - Skip);
2940}
2941
2942template <class ELFT>
2943typename MipsGOTParser<ELFT>::Entries
2944MipsGOTParser<ELFT>::getGlobalEntries() const {
2945 if (GlobalNum == 0)
2946 return Entries();
2947 return GotEntries.slice(LocalNum, GlobalNum);
2948}
2949
2950template <class ELFT>
2951typename MipsGOTParser<ELFT>::Entries
2952MipsGOTParser<ELFT>::getOtherEntries() const {
2953 size_t OtherNum = GotEntries.size() - LocalNum - GlobalNum;
2954 if (OtherNum == 0)
2955 return Entries();
2956 return GotEntries.slice(LocalNum + GlobalNum, OtherNum);
2957}
2958
2959template <class ELFT>
2960uint64_t MipsGOTParser<ELFT>::getGotAddress(const Entry *E) const {
2961 int64_t Offset = std::distance(GotEntries.data(), E) * sizeof(Entry);
2962 return GotSec->sh_addr + Offset;
2963}
2964
2965template <class ELFT>
2966int64_t MipsGOTParser<ELFT>::getGotOffset(const Entry *E) const {
2967 int64_t Offset = std::distance(GotEntries.data(), E) * sizeof(Entry);
2968 return Offset - 0x7ff0;
2969}
2970
2971template <class ELFT>
2972const typename MipsGOTParser<ELFT>::Elf_Sym *
2973MipsGOTParser<ELFT>::getGotSym(const Entry *E) const {
2974 int64_t Offset = std::distance(GotEntries.data(), E);
2975 return &GotDynSyms[Offset - LocalNum];
2976}
2977
2978template <class ELFT>
2979const typename MipsGOTParser<ELFT>::Entry *
2980MipsGOTParser<ELFT>::getPltLazyResolver() const {
2981 return PltEntries.empty() ? nullptr : &PltEntries[0];
2982}
2983
2984template <class ELFT>
2985const typename MipsGOTParser<ELFT>::Entry *
2986MipsGOTParser<ELFT>::getPltModulePointer() const {
2987 return PltEntries.size() < 2 ? nullptr : &PltEntries[1];
2988}
2989
2990template <class ELFT>
2991typename MipsGOTParser<ELFT>::Entries
2992MipsGOTParser<ELFT>::getPltEntries() const {
2993 if (PltEntries.size() <= 2)
2994 return Entries();
2995 return PltEntries.slice(2, PltEntries.size() - 2);
2996}
2997
2998template <class ELFT>
2999uint64_t MipsGOTParser<ELFT>::getPltAddress(const Entry *E) const {
3000 int64_t Offset = std::distance(PltEntries.data(), E) * sizeof(Entry);
3001 return PltSec->sh_addr + Offset;
3002}
3003
3004template <class ELFT>
3005const typename MipsGOTParser<ELFT>::Elf_Sym *
3006MipsGOTParser<ELFT>::getPltSym(const Entry *E) const {
3007 int64_t Offset = std::distance(getPltEntries().data(), E);
3008 if (PltRelSec->sh_type == ELF::SHT_REL) {
3009 Elf_Rel_Range Rels = unwrapOrError(FileName, Obj.rels(*PltRelSec));
3010 return unwrapOrError(FileName,
3011 Obj.getRelocationSymbol(Rels[Offset], PltSymTable));
3012 } else {
3013 Elf_Rela_Range Rels = unwrapOrError(FileName, Obj.relas(*PltRelSec));
3014 return unwrapOrError(FileName,
3015 Obj.getRelocationSymbol(Rels[Offset], PltSymTable));
3016 }
3017}
3018
3019const EnumEntry<unsigned> ElfMipsISAExtType[] = {
3020 {"None", Mips::AFL_EXT_NONE},
3021 {"Broadcom SB-1", Mips::AFL_EXT_SB1},
3022 {"Cavium Networks Octeon", Mips::AFL_EXT_OCTEON},
3023 {"Cavium Networks Octeon2", Mips::AFL_EXT_OCTEON2},
3024 {"Cavium Networks OcteonP", Mips::AFL_EXT_OCTEONP},
3025 {"Cavium Networks Octeon3", Mips::AFL_EXT_OCTEON3},
3026 {"LSI R4010", Mips::AFL_EXT_4010},
3027 {"Loongson 2E", Mips::AFL_EXT_LOONGSON_2E},
3028 {"Loongson 2F", Mips::AFL_EXT_LOONGSON_2F},
3029 {"Loongson 3A", Mips::AFL_EXT_LOONGSON_3A},
3030 {"MIPS R4650", Mips::AFL_EXT_4650},
3031 {"MIPS R5900", Mips::AFL_EXT_5900},
3032 {"MIPS R10000", Mips::AFL_EXT_10000},
3033 {"NEC VR4100", Mips::AFL_EXT_4100},
3034 {"NEC VR4111/VR4181", Mips::AFL_EXT_4111},
3035 {"NEC VR4120", Mips::AFL_EXT_4120},
3036 {"NEC VR5400", Mips::AFL_EXT_5400},
3037 {"NEC VR5500", Mips::AFL_EXT_5500},
3038 {"RMI Xlr", Mips::AFL_EXT_XLR},
3039 {"Toshiba R3900", Mips::AFL_EXT_3900}
3040};
3041
3042const EnumEntry<unsigned> ElfMipsASEFlags[] = {
3043 {"DSP", Mips::AFL_ASE_DSP},
3044 {"DSPR2", Mips::AFL_ASE_DSPR2},
3045 {"Enhanced VA Scheme", Mips::AFL_ASE_EVA},
3046 {"MCU", Mips::AFL_ASE_MCU},
3047 {"MDMX", Mips::AFL_ASE_MDMX},
3048 {"MIPS-3D", Mips::AFL_ASE_MIPS3D},
3049 {"MT", Mips::AFL_ASE_MT},
3050 {"SmartMIPS", Mips::AFL_ASE_SMARTMIPS},
3051 {"VZ", Mips::AFL_ASE_VIRT},
3052 {"MSA", Mips::AFL_ASE_MSA},
3053 {"MIPS16", Mips::AFL_ASE_MIPS16},
3054 {"microMIPS", Mips::AFL_ASE_MICROMIPS},
3055 {"XPA", Mips::AFL_ASE_XPA},
3056 {"CRC", Mips::AFL_ASE_CRC},
3057 {"GINV", Mips::AFL_ASE_GINV},
3058};
3059
3060const EnumEntry<unsigned> ElfMipsFpABIType[] = {
3061 {"Hard or soft float", Mips::Val_GNU_MIPS_ABI_FP_ANY},
3062 {"Hard float (double precision)", Mips::Val_GNU_MIPS_ABI_FP_DOUBLE},
3063 {"Hard float (single precision)", Mips::Val_GNU_MIPS_ABI_FP_SINGLE},
3064 {"Soft float", Mips::Val_GNU_MIPS_ABI_FP_SOFT},
3065 {"Hard float (MIPS32r2 64-bit FPU 12 callee-saved)",
3066 Mips::Val_GNU_MIPS_ABI_FP_OLD_64},
3067 {"Hard float (32-bit CPU, Any FPU)", Mips::Val_GNU_MIPS_ABI_FP_XX},
3068 {"Hard float (32-bit CPU, 64-bit FPU)", Mips::Val_GNU_MIPS_ABI_FP_64},
3069 {"Hard float compat (32-bit CPU, 64-bit FPU)",
3070 Mips::Val_GNU_MIPS_ABI_FP_64A}
3071};
3072
3073static const EnumEntry<unsigned> ElfMipsFlags1[] {
3074 {"ODDSPREG", Mips::AFL_FLAGS1_ODDSPREG},
3075};
3076
3077static int getMipsRegisterSize(uint8_t Flag) {
3078 switch (Flag) {
3079 case Mips::AFL_REG_NONE:
3080 return 0;
3081 case Mips::AFL_REG_32:
3082 return 32;
3083 case Mips::AFL_REG_64:
3084 return 64;
3085 case Mips::AFL_REG_128:
3086 return 128;
3087 default:
3088 return -1;
3089 }
3090}
3091
3092template <class ELFT>
3093static void printMipsReginfoData(ScopedPrinter &W,
3094 const Elf_Mips_RegInfo<ELFT> &Reginfo) {
3095 W.printHex("GP", Reginfo.ri_gp_value);
3096 W.printHex("General Mask", Reginfo.ri_gprmask);
3097 W.printHex("Co-Proc Mask0", Reginfo.ri_cprmask[0]);
3098 W.printHex("Co-Proc Mask1", Reginfo.ri_cprmask[1]);
3099 W.printHex("Co-Proc Mask2", Reginfo.ri_cprmask[2]);
3100 W.printHex("Co-Proc Mask3", Reginfo.ri_cprmask[3]);
3101}
3102
3103template <class ELFT> void ELFDumper<ELFT>::printMipsReginfo() {
3104 const Elf_Shdr *RegInfoSec = findSectionByName(".reginfo");
3105 if (!RegInfoSec) {
3106 W.startLine() << "There is no .reginfo section in the file.\n";
3107 return;
3108 }
3109
3110 Expected<ArrayRef<uint8_t>> ContentsOrErr =
3111 Obj.getSectionContents(*RegInfoSec);
3112 if (!ContentsOrErr) {
3113 this->reportUniqueWarning(
3114 "unable to read the content of the .reginfo section (" +
3115 describe(*RegInfoSec) + "): " + toString(ContentsOrErr.takeError()));
3116 return;
3117 }
3118
3119 if (ContentsOrErr->size() < sizeof(Elf_Mips_RegInfo<ELFT>)) {
3120 this->reportUniqueWarning("the .reginfo section has an invalid size (0x" +
3121 Twine::utohexstr(ContentsOrErr->size()) + ")");
3122 return;
3123 }
3124
3125 DictScope GS(W, "MIPS RegInfo");
3126 printMipsReginfoData(W, *reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(
3127 ContentsOrErr->data()));
3128}
3129
3130template <class ELFT>
3131static Expected<const Elf_Mips_Options<ELFT> *>
3132readMipsOptions(const uint8_t *SecBegin, ArrayRef<uint8_t> &SecData,
3133 bool &IsSupported) {
3134 if (SecData.size() < sizeof(Elf_Mips_Options<ELFT>))
3135 return createError("the .MIPS.options section has an invalid size (0x" +
3136 Twine::utohexstr(SecData.size()) + ")");
3137
3138 const Elf_Mips_Options<ELFT> *O =
3139 reinterpret_cast<const Elf_Mips_Options<ELFT> *>(SecData.data());
3140 const uint8_t Size = O->size;
3141 if (Size > SecData.size()) {
3142 const uint64_t Offset = SecData.data() - SecBegin;
3143 const uint64_t SecSize = Offset + SecData.size();
3144 return createError("a descriptor of size 0x" + Twine::utohexstr(Size) +
3145 " at offset 0x" + Twine::utohexstr(Offset) +
3146 " goes past the end of the .MIPS.options "
3147 "section of size 0x" +
3148 Twine::utohexstr(SecSize));
3149 }
3150
3151 IsSupported = O->kind == ODK_REGINFO;
3152 const size_t ExpectedSize =
3153 sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>);
3154
3155 if (IsSupported)
3156 if (Size < ExpectedSize)
3157 return createError(
3158 "a .MIPS.options entry of kind " +
3159 Twine(getElfMipsOptionsOdkType(O->kind)) +
3160 " has an invalid size (0x" + Twine::utohexstr(Size) +
3161 "), the expected size is 0x" + Twine::utohexstr(ExpectedSize));
3162
3163 SecData = SecData.drop_front(Size);
3164 return O;
3165}
3166
3167template <class ELFT> void ELFDumper<ELFT>::printMipsOptions() {
3168 const Elf_Shdr *MipsOpts = findSectionByName(".MIPS.options");
3169 if (!MipsOpts) {
3170 W.startLine() << "There is no .MIPS.options section in the file.\n";
3171 return;
3172 }
3173
3174 DictScope GS(W, "MIPS Options");
3175
3176 ArrayRef<uint8_t> Data =
3177 unwrapOrError(ObjF.getFileName(), Obj.getSectionContents(*MipsOpts));
3178 const uint8_t *const SecBegin = Data.begin();
3179 while (!Data.empty()) {
3180 bool IsSupported;
3181 Expected<const Elf_Mips_Options<ELFT> *> OptsOrErr =
3182 readMipsOptions<ELFT>(SecBegin, Data, IsSupported);
3183 if (!OptsOrErr) {
3184 reportUniqueWarning(OptsOrErr.takeError());
3185 break;
3186 }
3187
3188 unsigned Kind = (*OptsOrErr)->kind;
3189 const char *Type = getElfMipsOptionsOdkType(Kind);
3190 if (!IsSupported) {
3191 W.startLine() << "Unsupported MIPS options tag: " << Type << " (" << Kind
3192 << ")\n";
3193 continue;
3194 }
3195
3196 DictScope GS(W, Type);
3197 if (Kind == ODK_REGINFO)
3198 printMipsReginfoData(W, (*OptsOrErr)->getRegInfo());
3199 else
3200 llvm_unreachable("unexpected .MIPS.options section descriptor kind")::llvm::llvm_unreachable_internal("unexpected .MIPS.options section descriptor kind"
, "llvm/tools/llvm-readobj/ELFDumper.cpp", 3200)
;
3201 }
3202}
3203
3204template <class ELFT> void ELFDumper<ELFT>::printStackMap() const {
3205 const Elf_Shdr *StackMapSection = findSectionByName(".llvm_stackmaps");
3206 if (!StackMapSection)
3207 return;
3208
3209 auto Warn = [&](Error &&E) {
3210 this->reportUniqueWarning("unable to read the stack map from " +
3211 describe(*StackMapSection) + ": " +
3212 toString(std::move(E)));
3213 };
3214
3215 Expected<ArrayRef<uint8_t>> ContentOrErr =
3216 Obj.getSectionContents(*StackMapSection);
3217 if (!ContentOrErr) {
3218 Warn(ContentOrErr.takeError());
3219 return;
3220 }
3221
3222 if (Error E = StackMapParser<ELFT::TargetEndianness>::validateHeader(
3223 *ContentOrErr)) {
3224 Warn(std::move(E));
3225 return;
3226 }
3227
3228 prettyPrintStackMap(W, StackMapParser<ELFT::TargetEndianness>(*ContentOrErr));
3229}
3230
3231template <class ELFT>
3232void ELFDumper<ELFT>::printReloc(const Relocation<ELFT> &R, unsigned RelIndex,
3233 const Elf_Shdr &Sec, const Elf_Shdr *SymTab) {
3234 Expected<RelSymbol<ELFT>> Target = getRelocationTarget(R, SymTab);
3235 if (!Target)
3236 reportUniqueWarning("unable to print relocation " + Twine(RelIndex) +
3237 " in " + describe(Sec) + ": " +
3238 toString(Target.takeError()));
3239 else
3240 printRelRelaReloc(R, *Target);
3241}
3242
3243static inline void printFields(formatted_raw_ostream &OS, StringRef Str1,
3244 StringRef Str2) {
3245 OS.PadToColumn(2u);
3246 OS << Str1;
3247 OS.PadToColumn(37u);
3248 OS << Str2 << "\n";
3249 OS.flush();
3250}
3251
3252template <class ELFT>
3253static std::string getSectionHeadersNumString(const ELFFile<ELFT> &Obj,
3254 StringRef FileName) {
3255 const typename ELFT::Ehdr &ElfHeader = Obj.getHeader();
3256 if (ElfHeader.e_shnum != 0)
3257 return to_string(ElfHeader.e_shnum);
3258
3259 Expected<ArrayRef<typename ELFT::Shdr>> ArrOrErr = Obj.sections();
3260 if (!ArrOrErr) {
3261 // In this case we can ignore an error, because we have already reported a
3262 // warning about the broken section header table earlier.
3263 consumeError(ArrOrErr.takeError());
3264 return "<?>";
3265 }
3266
3267 if (ArrOrErr->empty())
3268 return "0";
3269 return "0 (" + to_string((*ArrOrErr)[0].sh_size) + ")";
3270}
3271
3272template <class ELFT>
3273static std::string getSectionHeaderTableIndexString(const ELFFile<ELFT> &Obj,
3274 StringRef FileName) {
3275 const typename ELFT::Ehdr &ElfHeader = Obj.getHeader();
3276 if (ElfHeader.e_shstrndx != SHN_XINDEX)
3277 return to_string(ElfHeader.e_shstrndx);
3278
3279 Expected<ArrayRef<typename ELFT::Shdr>> ArrOrErr = Obj.sections();
3280 if (!ArrOrErr) {
3281 // In this case we can ignore an error, because we have already reported a
3282 // warning about the broken section header table earlier.
3283 consumeError(ArrOrErr.takeError());
3284 return "<?>";
3285 }
3286
3287 if (ArrOrErr->empty())
3288 return "65535 (corrupt: out of range)";
3289 return to_string(ElfHeader.e_shstrndx) + " (" +
3290 to_string((*ArrOrErr)[0].sh_link) + ")";
3291}
3292
3293static const EnumEntry<unsigned> *getObjectFileEnumEntry(unsigned Type) {
3294 auto It = llvm::find_if(ElfObjectFileType, [&](const EnumEntry<unsigned> &E) {
3295 return E.Value == Type;
3296 });
3297 if (It != makeArrayRef(ElfObjectFileType).end())
3298 return It;
3299 return nullptr;
3300}
3301
3302template <class ELFT>
3303void GNUELFDumper<ELFT>::printFileSummary(StringRef FileStr, ObjectFile &Obj,
3304 ArrayRef<std::string> InputFilenames,
3305 const Archive *A) {
3306 if (InputFilenames.size() > 1 || A) {
3307 this->W.startLine() << "\n";
3308 this->W.printString("File", FileStr);
3309 }
3310}
3311
3312template <class ELFT> void GNUELFDumper<ELFT>::printFileHeaders() {
3313 const Elf_Ehdr &e = this->Obj.getHeader();
3314 OS << "ELF Header:\n";
3315 OS << " Magic: ";
3316 std::string Str;
3317 for (int i = 0; i < ELF::EI_NIDENT; i++)
3318 OS << format(" %02x", static_cast<int>(e.e_ident[i]));
3319 OS << "\n";
3320 Str = enumToString(e.e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
3321 printFields(OS, "Class:", Str);
3322 Str = enumToString(e.e_ident[ELF::EI_DATA], makeArrayRef(ElfDataEncoding));
3323 printFields(OS, "Data:", Str);
3324 OS.PadToColumn(2u);
3325 OS << "Version:";
3326 OS.PadToColumn(37u);
3327 OS << utohexstr(e.e_ident[ELF::EI_VERSION]);
3328 if (e.e_version == ELF::EV_CURRENT)
3329 OS << " (current)";
3330 OS << "\n";
3331 Str = enumToString(e.e_ident[ELF::EI_OSABI], makeArrayRef(ElfOSABI));
3332 printFields(OS, "OS/ABI:", Str);
3333 printFields(OS,
3334 "ABI Version:", std::to_string(e.e_ident[ELF::EI_ABIVERSION]));
3335
3336 if (const EnumEntry<unsigned> *E = getObjectFileEnumEntry(e.e_type)) {
3337 Str = E->AltName.str();
3338 } else {
3339 if (e.e_type >= ET_LOPROC)
3340 Str = "Processor Specific: (" + utohexstr(e.e_type, /*LowerCase=*/true) + ")";
3341 else if (e.e_type >= ET_LOOS)
3342 Str = "OS Specific: (" + utohexstr(e.e_type, /*LowerCase=*/true) + ")";
3343 else
3344 Str = "<unknown>: " + utohexstr(e.e_type, /*LowerCase=*/true);
3345 }
3346 printFields(OS, "Type:", Str);
3347
3348 Str = enumToString(e.e_machine, makeArrayRef(ElfMachineType));
3349 printFields(OS, "Machine:", Str);
3350 Str = "0x" + utohexstr(e.e_version);
3351 printFields(OS, "Version:", Str);
3352 Str = "0x" + utohexstr(e.e_entry);
3353 printFields(OS, "Entry point address:", Str);
3354 Str = to_string(e.e_phoff) + " (bytes into file)";
3355 printFields(OS, "Start of program headers:", Str);
3356 Str = to_string(e.e_shoff) + " (bytes into file)";
3357 printFields(OS, "Start of section headers:", Str);
3358 std::string ElfFlags;
3359 if (e.e_machine == EM_MIPS)
3360 ElfFlags =
3361 printFlags(e.e_flags, makeArrayRef(ElfHeaderMipsFlags),
3362 unsigned(ELF::EF_MIPS_ARCH), unsigned(ELF::EF_MIPS_ABI),
3363 unsigned(ELF::EF_MIPS_MACH));
3364 else if (e.e_machine == EM_RISCV)
3365 ElfFlags = printFlags(e.e_flags, makeArrayRef(ElfHeaderRISCVFlags));
3366 else if (e.e_machine == EM_AVR)
3367 ElfFlags = printFlags(e.e_flags, makeArrayRef(ElfHeaderAVRFlags),
3368 unsigned(ELF::EF_AVR_ARCH_MASK));
3369 else if (e.e_machine == EM_LOONGARCH)
3370 ElfFlags = printFlags(e.e_flags, makeArrayRef(ElfHeaderLoongArchFlags),
3371 unsigned(ELF::EF_LOONGARCH_BASE_ABI_MASK));
3372 Str = "0x" + utohexstr(e.e_flags);
3373 if (!ElfFlags.empty())
3374 Str = Str + ", " + ElfFlags;
3375 printFields(OS, "Flags:", Str);
3376 Str = to_string(e.e_ehsize) + " (bytes)";
3377 printFields(OS, "Size of this header:", Str);
3378 Str = to_string(e.e_phentsize) + " (bytes)";
3379 printFields(OS, "Size of program headers:", Str);
3380 Str = to_string(e.e_phnum);
3381 printFields(OS, "Number of program headers:", Str);
3382 Str = to_string(e.e_shentsize) + " (bytes)";
3383 printFields(OS, "Size of section headers:", Str);
3384 Str = getSectionHeadersNumString(this->Obj, this->FileName);
3385 printFields(OS, "Number of section headers:", Str);
3386 Str = getSectionHeaderTableIndexString(this->Obj, this->FileName);
3387 printFields(OS, "Section header string table index:", Str);
3388}
3389
3390template <class ELFT> std::vector<GroupSection> ELFDumper<ELFT>::getGroups() {
3391 auto GetSignature = [&](const Elf_Sym &Sym, unsigned SymNdx,
3392 const Elf_Shdr &Symtab) -> StringRef {
3393 Expected<StringRef> StrTableOrErr = Obj.getStringTableForSymtab(Symtab);
3394 if (!StrTableOrErr) {
3395 reportUniqueWarning("unable to get the string table for " +
3396 describe(Symtab) + ": " +
3397 toString(StrTableOrErr.takeError()));
3398 return "<?>";
3399 }
3400
3401 StringRef Strings = *StrTableOrErr;
3402 if (Sym.st_name >= Strings.size()) {
3403 reportUniqueWarning("unable to get the name of the symbol with index " +
3404 Twine(SymNdx) + ": st_name (0x" +
3405 Twine::utohexstr(Sym.st_name) +
3406 ") is past the end of the string table of size 0x" +
3407 Twine::utohexstr(Strings.size()));
3408 return "<?>";
3409 }
3410
3411 return StrTableOrErr->data() + Sym.st_name;
3412 };
3413
3414 std::vector<GroupSection> Ret;
3415 uint64_t I = 0;
3416 for (const Elf_Shdr &Sec : cantFail(Obj.sections())) {
3417 ++I;
3418 if (Sec.sh_type != ELF::SHT_GROUP)
3419 continue;
3420
3421 StringRef Signature = "<?>";
3422 if (Expected<const Elf_Shdr *> SymtabOrErr = Obj.getSection(Sec.sh_link)) {
3423 if (Expected<const Elf_Sym *> SymOrErr =
3424 Obj.template getEntry<Elf_Sym>(**SymtabOrErr, Sec.sh_info))
3425 Signature = GetSignature(**SymOrErr, Sec.sh_info, **SymtabOrErr);
3426 else
3427 reportUniqueWarning("unable to get the signature symbol for " +
3428 describe(Sec) + ": " +
3429 toString(SymOrErr.takeError()));
3430 } else {
3431 reportUniqueWarning("unable to get the symbol table for " +
3432 describe(Sec) + ": " +
3433 toString(SymtabOrErr.takeError()));
3434 }
3435
3436 ArrayRef<Elf_Word> Data;
3437 if (Expected<ArrayRef<Elf_Word>> ContentsOrErr =
3438 Obj.template getSectionContentsAsArray<Elf_Word>(Sec)) {
3439 if (ContentsOrErr->empty())
3440 reportUniqueWarning("unable to read the section group flag from the " +
3441 describe(Sec) + ": the section is empty");
3442 else
3443 Data = *ContentsOrErr;
3444 } else {
3445 reportUniqueWarning("unable to get the content of the " + describe(Sec) +
3446 ": " + toString(ContentsOrErr.takeError()));
3447 }
3448
3449 Ret.push_back({getPrintableSectionName(Sec),
3450 maybeDemangle(Signature),
3451 Sec.sh_name,
3452 I - 1,
3453 Sec.sh_link,
3454 Sec.sh_info,
3455 Data.empty() ? Elf_Word(0) : Data[0],
3456 {}});
3457
3458 if (Data.empty())
3459 continue;
3460
3461 std::vector<GroupMember> &GM = Ret.back().Members;
3462 for (uint32_t Ndx : Data.slice(1)) {
3463 if (Expected<const Elf_Shdr *> SecOrErr = Obj.getSection(Ndx)) {
3464 GM.push_back({getPrintableSectionName(**SecOrErr), Ndx});
3465 } else {
3466 reportUniqueWarning("unable to get the section with index " +
3467 Twine(Ndx) + " when dumping the " + describe(Sec) +
3468 ": " + toString(SecOrErr.takeError()));
3469 GM.push_back({"<?>", Ndx});
3470 }
3471 }
3472 }
3473 return Ret;
3474}
3475
3476static DenseMap<uint64_t, const GroupSection *>
3477mapSectionsToGroups(ArrayRef<GroupSection> Groups) {
3478 DenseMap<uint64_t, const GroupSection *> Ret;
3479 for (const GroupSection &G : Groups)
3480 for (const GroupMember &GM : G.Members)
3481 Ret.insert({GM.Index, &G});
3482 return Ret;
3483}
3484
3485template <class ELFT> void GNUELFDumper<ELFT>::printGroupSections() {
3486 std::vector<GroupSection> V = this->getGroups();
3487 DenseMap<uint64_t, const GroupSection *> Map = mapSectionsToGroups(V);
3488 for (const GroupSection &G : V) {
3489 OS << "\n"
3490 << getGroupType(G.Type) << " group section ["
3491 << format_decimal(G.Index, 5) << "] `" << G.Name << "' [" << G.Signature
3492 << "] contains " << G.Members.size() << " sections:\n"
3493 << " [Index] Name\n";
3494 for (const GroupMember &GM : G.Members) {
3495 const GroupSection *MainGroup = Map[GM.Index];
3496 if (MainGroup != &G)
3497 this->reportUniqueWarning(
3498 "section with index " + Twine(GM.Index) +
3499 ", included in the group section with index " +
3500 Twine(MainGroup->Index) +
3501 ", was also found in the group section with index " +
3502 Twine(G.Index));
3503 OS << " [" << format_decimal(GM.Index, 5) << "] " << GM.Name << "\n";
3504 }
3505 }
3506
3507 if (V.empty())
3508 OS << "There are no section groups in this file.\n";
3509}
3510
3511template <class ELFT>
3512void GNUELFDumper<ELFT>::printRelrReloc(const Elf_Relr &R) {
3513 OS << to_string(format_hex_no_prefix(R, ELFT::Is64Bits ? 16 : 8)) << "\n";
3514}
3515
3516template <class ELFT>
3517void GNUELFDumper<ELFT>::printRelRelaReloc(const Relocation<ELFT> &R,
3518 const RelSymbol<ELFT> &RelSym) {
3519 // First two fields are bit width dependent. The rest of them are fixed width.
3520 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
3521 Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias};
3522 unsigned Width = ELFT::Is64Bits ? 16 : 8;
3523
3524 Fields[0].Str = to_string(format_hex_no_prefix(R.Offset, Width));
3525 Fields[1].Str = to_string(format_hex_no_prefix(R.Info, Width));
3526
3527 SmallString<32> RelocName;
3528 this->Obj.getRelocationTypeName(R.Type, RelocName);
3529 Fields[2].Str = RelocName.c_str();
3530
3531 if (RelSym.Sym)
3532 Fields[3].Str =
3533 to_string(format_hex_no_prefix(RelSym.Sym->getValue(), Width));
3534
3535 Fields[4].Str = std::string(RelSym.Name);
3536 for (const Field &F : Fields)
3537 printField(F);
3538
3539 std::string Addend;
3540 if (Optional<int64_t> A = R.Addend) {
3541 int64_t RelAddend = *A;
3542 if (!RelSym.Name.empty()) {
3543 if (RelAddend < 0) {
3544 Addend = " - ";
3545 RelAddend = std::abs(RelAddend);
3546 } else {
3547 Addend = " + ";
3548 }
3549 }
3550 Addend += utohexstr(RelAddend, /*LowerCase=*/true);
3551 }
3552 OS << Addend << "\n";
3553}
3554
3555template <class ELFT>
3556static void printRelocHeaderFields(formatted_raw_ostream &OS, unsigned SType) {
3557 bool IsRela = SType == ELF::SHT_RELA || SType == ELF::SHT_ANDROID_RELA;
3558 bool IsRelr = SType == ELF::SHT_RELR || SType == ELF::SHT_ANDROID_RELR;
3559 if (ELFT::Is64Bits)
3560 OS << " ";
3561 else
3562 OS << " ";
3563 if (IsRelr && opts::RawRelr)
3564 OS << "Data ";
3565 else
3566 OS << "Offset";
3567 if (ELFT::Is64Bits)
3568 OS << " Info Type"
3569 << " Symbol's Value Symbol's Name";
3570 else
3571 OS << " Info Type Sym. Value Symbol's Name";
3572 if (IsRela)
3573 OS << " + Addend";
3574 OS << "\n";
3575}
3576
3577template <class ELFT>
3578void GNUELFDumper<ELFT>::printDynamicRelocHeader(unsigned Type, StringRef Name,
3579 const DynRegionInfo &Reg) {
3580 uint64_t Offset = Reg.Addr - this->Obj.base();
3581 OS << "\n'" << Name.str().c_str() << "' relocation section at offset 0x"
3582 << utohexstr(Offset, /*LowerCase=*/true) << " contains " << Reg.Size << " bytes:\n";
3583 printRelocHeaderFields<ELFT>(OS, Type);
3584}
3585
3586template <class ELFT>
3587static bool isRelocationSec(const typename ELFT::Shdr &Sec) {
3588 return Sec.sh_type == ELF::SHT_REL || Sec.sh_type == ELF::SHT_RELA ||
3589 Sec.sh_type == ELF::SHT_RELR || Sec.sh_type == ELF::SHT_ANDROID_REL ||
3590 Sec.sh_type == ELF::SHT_ANDROID_RELA ||
3591 Sec.sh_type == ELF::SHT_ANDROID_RELR;
3592}
3593
3594template <class ELFT> void GNUELFDumper<ELFT>::printRelocations() {
3595 auto GetEntriesNum = [&](const Elf_Shdr &Sec) -> Expected<size_t> {
3596 // Android's packed relocation section needs to be unpacked first
3597 // to get the actual number of entries.
3598 if (Sec.sh_type == ELF::SHT_ANDROID_REL ||
3599 Sec.sh_type == ELF::SHT_ANDROID_RELA) {
3600 Expected<std::vector<typename ELFT::Rela>> RelasOrErr =
3601 this->Obj.android_relas(Sec);
3602 if (!RelasOrErr)
3603 return RelasOrErr.takeError();
3604 return RelasOrErr->size();
3605 }
3606
3607 if (!opts::RawRelr && (Sec.sh_type == ELF::SHT_RELR ||
3608 Sec.sh_type == ELF::SHT_ANDROID_RELR)) {
3609 Expected<Elf_Relr_Range> RelrsOrErr = this->Obj.relrs(Sec);
3610 if (!RelrsOrErr)
3611 return RelrsOrErr.takeError();
3612 return this->Obj.decode_relrs(*RelrsOrErr).size();
3613 }
3614
3615 return Sec.getEntityCount();
3616 };
3617
3618 bool HasRelocSections = false;
3619 for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) {
3620 if (!isRelocationSec<ELFT>(Sec))
3621 continue;
3622 HasRelocSections = true;
3623
3624 std::string EntriesNum = "<?>";
3625 if (Expected<size_t> NumOrErr = GetEntriesNum(Sec))
3626 EntriesNum = std::to_string(*NumOrErr);
3627 else
3628 this->reportUniqueWarning("unable to get the number of relocations in " +
3629 this->describe(Sec) + ": " +
3630 toString(NumOrErr.takeError()));
3631
3632 uintX_t Offset = Sec.sh_offset;
3633 StringRef Name = this->getPrintableSectionName(Sec);
3634 OS << "\nRelocation section '" << Name << "' at offset 0x"
3635 << utohexstr(Offset, /*LowerCase=*/true) << " contains " << EntriesNum
3636 << " entries:\n";
3637 printRelocHeaderFields<ELFT>(OS, Sec.sh_type);
3638 this->printRelocationsHelper(Sec);
3639 }
3640 if (!HasRelocSections)
3641 OS << "\nThere are no relocations in this file.\n";
3642}
3643
3644// Print the offset of a particular section from anyone of the ranges:
3645// [SHT_LOOS, SHT_HIOS], [SHT_LOPROC, SHT_HIPROC], [SHT_LOUSER, SHT_HIUSER].
3646// If 'Type' does not fall within any of those ranges, then a string is
3647// returned as '<unknown>' followed by the type value.
3648static std::string getSectionTypeOffsetString(unsigned Type) {
3649 if (Type >= SHT_LOOS && Type <= SHT_HIOS)
3650 return "LOOS+0x" + utohexstr(Type - SHT_LOOS);
3651 else if (Type >= SHT_LOPROC && Type <= SHT_HIPROC)
3652 return "LOPROC+0x" + utohexstr(Type - SHT_LOPROC);
3653 else if (Type >= SHT_LOUSER && Type <= SHT_HIUSER)
3654 return "LOUSER+0x" + utohexstr(Type - SHT_LOUSER);
3655 return "0x" + utohexstr(Type) + ": <unknown>";
3656}
3657
3658static std::string getSectionTypeString(unsigned Machine, unsigned Type) {
3659 StringRef Name = getELFSectionTypeName(Machine, Type);
3660
3661 // Handle SHT_GNU_* type names.
3662 if (Name.consume_front("SHT_GNU_")) {
3663 if (Name == "HASH")
3664 return "GNU_HASH";
3665 // E.g. SHT_GNU_verneed -> VERNEED.
3666 return Name.upper();
3667 }
3668
3669 if (Name == "SHT_SYMTAB_SHNDX")
3670 return "SYMTAB SECTION INDICES";
3671
3672 if (Name.consume_front("SHT_"))
3673 return Name.str();
3674 return getSectionTypeOffsetString(Type);
3675}
3676
3677static void printSectionDescription(formatted_raw_ostream &OS,
3678 unsigned EMachine) {
3679 OS << "Key to Flags:\n";
3680 OS << " W (write), A (alloc), X (execute), M (merge), S (strings), I "
3681 "(info),\n";
3682 OS << " L (link order), O (extra OS processing required), G (group), T "
3683 "(TLS),\n";
3684 OS << " C (compressed), x (unknown), o (OS specific), E (exclude),\n";
3685 OS << " R (retain)";
3686
3687 if (EMachine == EM_X86_64)
3688 OS << ", l (large)";
3689 else if (EMachine == EM_ARM)
3690 OS << ", y (purecode)";
3691
3692 OS << ", p (processor specific)\n";
3693}
3694
3695template <class ELFT> void GNUELFDumper<ELFT>::printSectionHeaders() {
3696 ArrayRef<Elf_Shdr> Sections = cantFail(this->Obj.sections());
3697 if (Sections.empty()) {
3698 OS << "\nThere are no sections in this file.\n";
3699 Expected<StringRef> SecStrTableOrErr =
3700 this->Obj.getSectionStringTable(Sections, this->WarningHandler);
3701 if (!SecStrTableOrErr)
3702 this->reportUniqueWarning(SecStrTableOrErr.takeError());
3703 return;
3704 }
3705 unsigned Bias = ELFT::Is64Bits ? 0 : 8;
3706 OS << "There are " << to_string(Sections.size())
3707 << " section headers, starting at offset "
3708 << "0x" << utohexstr(this->Obj.getHeader().e_shoff, /*LowerCase=*/true) << ":\n\n";
3709 OS << "Section Headers:\n";
3710 Field Fields[11] = {
3711 {"[Nr]", 2}, {"Name", 7}, {"Type", 25},
3712 {"Address", 41}, {"Off", 58 - Bias}, {"Size", 65 - Bias},
3713 {"ES", 72 - Bias}, {"Flg", 75 - Bias}, {"Lk", 79 - Bias},
3714 {"Inf", 82 - Bias}, {"Al", 86 - Bias}};
3715 for (const Field &F : Fields)
3716 printField(F);
3717 OS << "\n";
3718
3719 StringRef SecStrTable;
3720 if (Expected<StringRef> SecStrTableOrErr =
3721 this->Obj.getSectionStringTable(Sections, this->WarningHandler))
3722 SecStrTable = *SecStrTableOrErr;
3723 else
3724 this->reportUniqueWarning(SecStrTableOrErr.takeError());
3725
3726 size_t SectionIndex = 0;
3727 for (const Elf_Shdr &Sec : Sections) {
3728 Fields[0].Str = to_string(SectionIndex);
3729 if (SecStrTable.empty())
3730 Fields[1].Str = "<no-strings>";
3731 else
3732 Fields[1].Str = std::string(unwrapOrError<StringRef>(
3733 this->FileName, this->Obj.getSectionName(Sec, SecStrTable)));
3734 Fields[2].Str =
3735 getSectionTypeString(this->Obj.getHeader().e_machine, Sec.sh_type);
3736 Fields[3].Str =
3737 to_string(format_hex_no_prefix(Sec.sh_addr, ELFT::Is64Bits ? 16 : 8));
3738 Fields[4].Str = to_string(format_hex_no_prefix(Sec.sh_offset, 6));
3739 Fields[5].Str = to_string(format_hex_no_prefix(Sec.sh_size, 6));
3740 Fields[6].Str = to_string(format_hex_no_prefix(Sec.sh_entsize, 2));
3741 Fields[7].Str = getGNUFlags(this->Obj.getHeader().e_ident[ELF::EI_OSABI],
3742 this->Obj.getHeader().e_machine, Sec.sh_flags);
3743 Fields[8].Str = to_string(Sec.sh_link);
3744 Fields[9].Str = to_string(Sec.sh_info);
3745 Fields[10].Str = to_string(Sec.sh_addralign);
3746
3747 OS.PadToColumn(Fields[0].Column);
3748 OS << "[" << right_justify(Fields[0].Str, 2) << "]";
3749 for (int i = 1; i < 7; i++)
3750 printField(Fields[i]);
3751 OS.PadToColumn(Fields[7].Column);
3752 OS << right_justify(Fields[7].Str, 3);
3753 OS.PadToColumn(Fields[8].Column);
3754 OS << right_justify(Fields[8].Str, 2);
3755 OS.PadToColumn(Fields[9].Column);
3756 OS << right_justify(Fields[9].Str, 3);
3757 OS.PadToColumn(Fields[10].Column);
3758 OS << right_justify(Fields[10].Str, 2);
3759 OS << "\n";
3760 ++SectionIndex;
3761 }
3762 printSectionDescription(OS, this->Obj.getHeader().e_machine);
3763}
3764
3765template <class ELFT>
3766void GNUELFDumper<ELFT>::printSymtabMessage(const Elf_Shdr *Symtab,
3767 size_t Entries,
3768 bool NonVisibilityBitsUsed) const {
3769 StringRef Name;
3770 if (Symtab)
3771 Name = this->getPrintableSectionName(*Symtab);
3772 if (!Name.empty())
3773 OS << "\nSymbol table '" << Name << "'";
3774 else
3775 OS << "\nSymbol table for image";
3776 OS << " contains " << Entries << " entries:\n";
3777
3778 if (ELFT::Is64Bits)
3779 OS << " Num: Value Size Type Bind Vis";
3780 else
3781 OS << " Num: Value Size Type Bind Vis";
3782
3783 if (NonVisibilityBitsUsed)
3784 OS << " ";
3785 OS << " Ndx Name\n";
3786}
3787
3788template <class ELFT>
3789std::string
3790GNUELFDumper<ELFT>::getSymbolSectionNdx(const Elf_Sym &Symbol,
3791 unsigned SymIndex,
3792 DataRegion<Elf_Word> ShndxTable) const {
3793 unsigned SectionIndex = Symbol.st_shndx;
3794 switch (SectionIndex) {
3795 case ELF::SHN_UNDEF:
3796 return "UND";
3797 case ELF::SHN_ABS:
3798 return "ABS";
3799 case ELF::SHN_COMMON:
3800 return "COM";
3801 case ELF::SHN_XINDEX: {
3802 Expected<uint32_t> IndexOrErr =
3803 object::getExtendedSymbolTableIndex<ELFT>(Symbol, SymIndex, ShndxTable);
3804 if (!IndexOrErr) {
3805 assert(Symbol.st_shndx == SHN_XINDEX &&(static_cast <bool> (Symbol.st_shndx == SHN_XINDEX &&
"getExtendedSymbolTableIndex should only fail due to an invalid "
"SHT_SYMTAB_SHNDX table/reference") ? void (0) : __assert_fail
("Symbol.st_shndx == SHN_XINDEX && \"getExtendedSymbolTableIndex should only fail due to an invalid \" \"SHT_SYMTAB_SHNDX table/reference\""
, "llvm/tools/llvm-readobj/ELFDumper.cpp", 3807, __extension__
__PRETTY_FUNCTION__))
3806 "getExtendedSymbolTableIndex should only fail due to an invalid "(static_cast <bool> (Symbol.st_shndx == SHN_XINDEX &&
"getExtendedSymbolTableIndex should only fail due to an invalid "
"SHT_SYMTAB_SHNDX table/reference") ? void (0) : __assert_fail
("Symbol.st_shndx == SHN_XINDEX && \"getExtendedSymbolTableIndex should only fail due to an invalid \" \"SHT_SYMTAB_SHNDX table/reference\""
, "llvm/tools/llvm-readobj/ELFDumper.cpp", 3807, __extension__
__PRETTY_FUNCTION__))
3807 "SHT_SYMTAB_SHNDX table/reference")(static_cast <bool> (Symbol.st_shndx == SHN_XINDEX &&
"getExtendedSymbolTableIndex should only fail due to an invalid "
"SHT_SYMTAB_SHNDX table/reference") ? void (0) : __assert_fail
("Symbol.st_shndx == SHN_XINDEX && \"getExtendedSymbolTableIndex should only fail due to an invalid \" \"SHT_SYMTAB_SHNDX table/reference\""
, "llvm/tools/llvm-readobj/ELFDumper.cpp", 3807, __extension__
__PRETTY_FUNCTION__))
;
3808 this->reportUniqueWarning(IndexOrErr.takeError());
3809 return "RSV[0xffff]";
3810 }
3811 return to_string(format_decimal(*IndexOrErr, 3));
3812 }
3813 default:
3814 // Find if:
3815 // Processor specific
3816 if (SectionIndex >= ELF::SHN_LOPROC && SectionIndex <= ELF::SHN_HIPROC)
3817 return std::string("PRC[0x") +
3818 to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3819 // OS specific
3820 if (SectionIndex >= ELF::SHN_LOOS && SectionIndex <= ELF::SHN_HIOS)
3821 return std::string("OS[0x") +
3822 to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3823 // Architecture reserved:
3824 if (SectionIndex >= ELF::SHN_LORESERVE &&
3825 SectionIndex <= ELF::SHN_HIRESERVE)
3826 return std::string("RSV[0x") +
3827 to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3828 // A normal section with an index
3829 return to_string(format_decimal(SectionIndex, 3));
3830 }
3831}
3832
3833template <class ELFT>
3834void GNUELFDumper<ELFT>::printSymbol(const Elf_Sym &Symbol, unsigned SymIndex,
3835 DataRegion<Elf_Word> ShndxTable,
3836 Optional<StringRef> StrTable,
3837 bool IsDynamic,
3838 bool NonVisibilityBitsUsed) const {
3839 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
3840 Field Fields[8] = {0, 8, 17 + Bias, 23 + Bias,
3841 31 + Bias, 38 + Bias, 48 + Bias, 51 + Bias};
3842 Fields[0].Str = to_string(format_decimal(SymIndex, 6)) + ":";
3843 Fields[1].Str =
3844 to_string(format_hex_no_prefix(Symbol.st_value, ELFT::Is64Bits ? 16 : 8));
3845 Fields[2].Str = to_string(format_decimal(Symbol.st_size, 5));
3846
3847 unsigned char SymbolType = Symbol.getType();
3848 if (this->Obj.getHeader().e_machine == ELF::EM_AMDGPU &&
3849 SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
3850 Fields[3].Str = enumToString(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
3851 else
3852 Fields[3].Str = enumToString(SymbolType, makeArrayRef(ElfSymbolTypes));
3853
3854 Fields[4].Str =
3855 enumToString(Symbol.getBinding(), makeArrayRef(ElfSymbolBindings));
3856 Fields[5].Str =
3857 enumToString(Symbol.getVisibility(), makeArrayRef(ElfSymbolVisibilities));
3858
3859 if (Symbol.st_other & ~0x3) {
3860 if (this->Obj.getHeader().e_machine == ELF::EM_AARCH64) {
3861 uint8_t Other = Symbol.st_other & ~0x3;
3862 if (Other & STO_AARCH64_VARIANT_PCS) {
3863 Other &= ~STO_AARCH64_VARIANT_PCS;
3864 Fields[5].Str += " [VARIANT_PCS";
3865 if (Other != 0)
3866 Fields[5].Str.append(" | " + utohexstr(Other, /*LowerCase=*/true));
3867 Fields[5].Str.append("]");
3868 }
3869 } else if (this->Obj.getHeader().e_machine == ELF::EM_RISCV) {
3870 uint8_t Other = Symbol.st_other & ~0x3;
3871 if (Other & STO_RISCV_VARIANT_CC) {
3872 Other &= ~STO_RISCV_VARIANT_CC;
3873 Fields[5].Str += " [VARIANT_CC";
3874 if (Other != 0)
3875 Fields[5].Str.append(" | " + utohexstr(Other, /*LowerCase=*/true));
3876 Fields[5].Str.append("]");
3877 }
3878 } else {
3879 Fields[5].Str +=
3880 " [<other: " + to_string(format_hex(Symbol.st_other, 2)) + ">]";
3881 }
3882 }
3883
3884 Fields[6].Column += NonVisibilityBitsUsed ? 13 : 0;
3885 Fields[6].Str = getSymbolSectionNdx(Symbol, SymIndex, ShndxTable);
3886
3887 Fields[7].Str = this->getFullSymbolName(Symbol, SymIndex, ShndxTable,
3888 StrTable, IsDynamic);
3889 for (const Field &Entry : Fields)
3890 printField(Entry);
3891 OS << "\n";
3892}
3893
3894template <class ELFT>
3895void GNUELFDumper<ELFT>::printHashedSymbol(const Elf_Sym *Symbol,
3896 unsigned SymIndex,
3897 DataRegion<Elf_Word> ShndxTable,
3898 StringRef StrTable,
3899 uint32_t Bucket) {
3900 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
3901 Field Fields[9] = {0, 6, 11, 20 + Bias, 25 + Bias,
3902 34 + Bias, 41 + Bias, 49 + Bias, 53 + Bias};
3903 Fields[0].Str = to_string(format_decimal(SymIndex, 5));
3904 Fields[1].Str = to_string(format_decimal(Bucket, 3)) + ":";
3905
3906 Fields[2].Str = to_string(
3907 format_hex_no_prefix(Symbol->st_value, ELFT::Is64Bits ? 16 : 8));
3908 Fields[3].Str = to_string(format_decimal(Symbol->st_size, 5));
3909
3910 unsigned char SymbolType = Symbol->getType();
3911 if (this->Obj.getHeader().e_machine == ELF::EM_AMDGPU &&
3912 SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
3913 Fields[4].Str = enumToString(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
3914 else
3915 Fields[4].Str = enumToString(SymbolType, makeArrayRef(ElfSymbolTypes));
3916
3917 Fields[5].Str =
3918 enumToString(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
3919 Fields[6].Str = enumToString(Symbol->getVisibility(),
3920 makeArrayRef(ElfSymbolVisibilities));
3921 Fields[7].Str = getSymbolSectionNdx(*Symbol, SymIndex, ShndxTable);
3922 Fields[8].Str =
3923 this->getFullSymbolName(*Symbol, SymIndex, ShndxTable, StrTable, true);
3924
3925 for (const Field &Entry : Fields)
3926 printField(Entry);
3927 OS << "\n";
3928}
3929
3930template <class ELFT>
3931void GNUELFDumper<ELFT>::printSymbols(bool PrintSymbols,
3932 bool PrintDynamicSymbols) {
3933 if (!PrintSymbols && !PrintDynamicSymbols)
3934 return;
3935 // GNU readelf prints both the .dynsym and .symtab with --symbols.
3936 this->printSymbolsHelper(true);
3937 if (PrintSymbols)
3938 this->printSymbolsHelper(false);
3939}
3940
3941template <class ELFT>
3942void GNUELFDumper<ELFT>::printHashTableSymbols(const Elf_Hash &SysVHash) {
3943 if (this->DynamicStringTable.empty())
3944 return;
3945
3946 if (ELFT::Is64Bits)
3947 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3948 else
3949 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3950 OS << "\n";
3951
3952 Elf_Sym_Range DynSyms = this->dynamic_symbols();
3953 const Elf_Sym *FirstSym = DynSyms.empty() ? nullptr : &DynSyms[0];
3954 if (!FirstSym) {
3955 this->reportUniqueWarning(
3956 Twine("unable to print symbols for the .hash table: the "
3957 "dynamic symbol table ") +
3958 (this->DynSymRegion ? "is empty" : "was not found"));
3959 return;
3960 }
3961
3962 DataRegion<Elf_Word> ShndxTable(
3963 (const Elf_Word *)this->DynSymTabShndxRegion.Addr, this->Obj.end());
3964 auto Buckets = SysVHash.buckets();
3965 auto Chains = SysVHash.chains();
3966 for (uint32_t Buc = 0; Buc < SysVHash.nbucket; Buc++) {
3967 if (Buckets[Buc] == ELF::STN_UNDEF)
3968 continue;
3969 BitVector Visited(SysVHash.nchain);
3970 for (uint32_t Ch = Buckets[Buc]; Ch < SysVHash.nchain; Ch = Chains[Ch]) {
3971 if (Ch == ELF::STN_UNDEF)
3972 break;
3973
3974 if (Visited[Ch]) {
3975 this->reportUniqueWarning(".hash section is invalid: bucket " +
3976 Twine(Ch) +
3977 ": a cycle was detected in the linked chain");
3978 break;
3979 }
3980
3981 printHashedSymbol(FirstSym + Ch, Ch, ShndxTable, this->DynamicStringTable,
3982 Buc);
3983 Visited[Ch] = true;
3984 }
3985 }
3986}
3987
3988template <class ELFT>
3989void GNUELFDumper<ELFT>::printGnuHashTableSymbols(const Elf_GnuHash &GnuHash) {
3990 if (this->DynamicStringTable.empty())
3991 return;
3992
3993 Elf_Sym_Range DynSyms = this->dynamic_symbols();
3994 const Elf_Sym *FirstSym = DynSyms.empty() ? nullptr : &DynSyms[0];
3995 if (!FirstSym) {
3996 this->reportUniqueWarning(
3997 Twine("unable to print symbols for the .gnu.hash table: the "
3998 "dynamic symbol table ") +
3999 (this->DynSymRegion ? "is empty" : "was not found"));
4000 return;
4001 }
4002
4003 auto GetSymbol = [&](uint64_t SymIndex,
4004 uint64_t SymsTotal) -> const Elf_Sym * {
4005 if (SymIndex >= SymsTotal) {
4006 this->reportUniqueWarning(
4007 "unable to print hashed symbol with index " + Twine(SymIndex) +
4008 ", which is greater than or equal to the number of dynamic symbols "
4009 "(" +
4010 Twine::utohexstr(SymsTotal) + ")");
4011 return nullptr;
4012 }
4013 return FirstSym + SymIndex;
4014 };
4015
4016 Expected<ArrayRef<Elf_Word>> ValuesOrErr =
4017 getGnuHashTableChains<ELFT>(this->DynSymRegion, &GnuHash);
4018 ArrayRef<Elf_Word> Values;
4019 if (!ValuesOrErr)
4020 this->reportUniqueWarning("unable to get hash values for the SHT_GNU_HASH "
4021 "section: " +
4022 toString(ValuesOrErr.takeError()));
4023 else
4024 Values = *ValuesOrErr;
4025
4026 DataRegion<Elf_Word> ShndxTable(
4027 (const Elf_Word *)this->DynSymTabShndxRegion.Addr, this->Obj.end());
4028 ArrayRef<Elf_Word> Buckets = GnuHash.buckets();
4029 for (uint32_t Buc = 0; Buc < GnuHash.nbuckets; Buc++) {
4030 if (Buckets[Buc] == ELF::STN_UNDEF)
4031 continue;
4032 uint32_t Index = Buckets[Buc];
4033 // Print whole chain.
4034 while (true) {
4035 uint32_t SymIndex = Index++;
4036 if (const Elf_Sym *Sym = GetSymbol(SymIndex, DynSyms.size()))
4037 printHashedSymbol(Sym, SymIndex, ShndxTable, this->DynamicStringTable,
4038 Buc);
4039 else
4040 break;
4041
4042 if (SymIndex < GnuHash.symndx) {
4043 this->reportUniqueWarning(
4044 "unable to read the hash value for symbol with index " +
4045 Twine(SymIndex) +
4046 ", which is less than the index of the first hashed symbol (" +
4047 Twine(GnuHash.symndx) + ")");
4048 break;
4049 }
4050
4051 // Chain ends at symbol with stopper bit.
4052 if ((Values[SymIndex - GnuHash.symndx] & 1) == 1)
4053 break;
4054 }
4055 }
4056}
4057
4058template <class ELFT> void GNUELFDumper<ELFT>::printHashSymbols() {
4059 if (this->HashTable) {
4060 OS << "\n Symbol table of .hash for image:\n";
4061 if (Error E = checkHashTable<ELFT>(*this, this->HashTable))
4062 this->reportUniqueWarning(std::move(E));
4063 else
4064 printHashTableSymbols(*this->HashTable);
4065 }
4066
4067 // Try printing the .gnu.hash table.
4068 if (this->GnuHashTable) {
4069 OS << "\n Symbol table of .gnu.hash for image:\n";
4070 if (ELFT::Is64Bits)
4071 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
4072 else
4073 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
4074 OS << "\n";
4075
4076 if (Error E = checkGNUHashTable<ELFT>(this->Obj, this->GnuHashTable))
4077 this->reportUniqueWarning(std::move(E));
4078 else
4079 printGnuHashTableSymbols(*this->GnuHashTable);
4080 }
4081}
4082
4083template <class ELFT> void GNUELFDumper<ELFT>::printSectionDetails() {
4084 ArrayRef<Elf_Shdr> Sections = cantFail(this->Obj.sections());
4085 if (Sections.empty()) {
4086 OS << "\nThere are no sections in this file.\n";
4087 Expected<StringRef> SecStrTableOrErr =
4088 this->Obj.getSectionStringTable(Sections, this->WarningHandler);
4089 if (!SecStrTableOrErr)
4090 this->reportUniqueWarning(SecStrTableOrErr.takeError());
4091 return;
4092 }
4093 OS << "There are " << to_string(Sections.size())
4094 << " section headers, starting at offset "
4095 << "0x" << utohexstr(this->Obj.getHeader().e_shoff, /*LowerCase=*/true) << ":\n\n";
4096
4097 OS << "Section Headers:\n";
4098
4099 auto PrintFields = [&](ArrayRef<Field> V) {
4100 for (const Field &F : V)
4101 printField(F);
4102 OS << "\n";
4103 };
4104
4105 PrintFields({{"[Nr]", 2}, {"Name", 7}});
4106
4107 constexpr bool Is64 = ELFT::Is64Bits;
4108 PrintFields({{"Type", 7},
4109 {Is64 ? "Address" : "Addr", 23},
4110 {"Off", Is64 ? 40 : 32},
4111 {"Size", Is64 ? 47 : 39},
4112 {"ES", Is64 ? 54 : 46},
4113 {"Lk", Is64 ? 59 : 51},
4114 {"Inf", Is64 ? 62 : 54},
4115 {"Al", Is64 ? 66 : 57}});
4116 PrintFields({{"Flags", 7}});
4117
4118 StringRef SecStrTable;
4119 if (Expected<StringRef> SecStrTableOrErr =
4120 this->Obj.getSectionStringTable(Sections, this->WarningHandler))
4121 SecStrTable = *SecStrTableOrErr;
4122 else
4123 this->reportUniqueWarning(SecStrTableOrErr.takeError());
4124
4125 size_t SectionIndex = 0;
4126 const unsigned AddrSize = Is64 ? 16 : 8;
4127 for (const Elf_Shdr &S : Sections) {
4128 StringRef Name = "<?>";
4129 if (Expected<StringRef> NameOrErr =
4130 this->Obj.getSectionName(S, SecStrTable))
4131 Name = *NameOrErr;
4132 else
4133 this->reportUniqueWarning(NameOrErr.takeError());
4134
4135 OS.PadToColumn(2);
4136 OS << "[" << right_justify(to_string(SectionIndex), 2) << "]";
4137 PrintFields({{Name, 7}});
4138 PrintFields(
4139 {{getSectionTypeString(this->Obj.getHeader().e_machine, S.sh_type), 7},
4140 {to_string(format_hex_no_prefix(S.sh_addr, AddrSize)), 23},
4141 {to_string(format_hex_no_prefix(S.sh_offset, 6)), Is64 ? 39 : 32},
4142 {to_string(format_hex_no_prefix(S.sh_size, 6)), Is64 ? 47 : 39},
4143 {to_string(format_hex_no_prefix(S.sh_entsize, 2)), Is64 ? 54 : 46},
4144 {to_string(S.sh_link), Is64 ? 59 : 51},
4145 {to_string(S.sh_info), Is64 ? 63 : 55},
4146 {to_string(S.sh_addralign), Is64 ? 66 : 58}});
4147
4148 OS.PadToColumn(7);
4149 OS << "[" << to_string(format_hex_no_prefix(S.sh_flags, AddrSize)) << "]: ";
4150
4151 DenseMap<unsigned, StringRef> FlagToName = {
4152 {SHF_WRITE, "WRITE"}, {SHF_ALLOC, "ALLOC"},
4153 {SHF_EXECINSTR, "EXEC"}, {SHF_MERGE, "MERGE"},
4154 {SHF_STRINGS, "STRINGS"}, {SHF_INFO_LINK, "INFO LINK"},
4155 {SHF_LINK_ORDER, "LINK ORDER"}, {SHF_OS_NONCONFORMING, "OS NONCONF"},
4156 {SHF_GROUP, "GROUP"}, {SHF_TLS, "TLS"},
4157 {SHF_COMPRESSED, "COMPRESSED"}, {SHF_EXCLUDE, "EXCLUDE"}};
4158
4159 uint64_t Flags = S.sh_flags;
4160 uint64_t UnknownFlags = 0;
4161 ListSeparator LS;
4162 while (Flags) {
4163 // Take the least significant bit as a flag.
4164 uint64_t Flag = Flags & -Flags;
4165 Flags -= Flag;
4166
4167 auto It = FlagToName.find(Flag);
4168 if (It != FlagToName.end())
4169 OS << LS << It->second;
4170 else
4171 UnknownFlags |= Flag;
4172 }
4173
4174 auto PrintUnknownFlags = [&](uint64_t Mask, StringRef Name) {
4175 uint64_t FlagsToPrint = UnknownFlags & Mask;
4176 if (!FlagsToPrint)
4177 return;
4178
4179 OS << LS << Name << " ("
4180 << to_string(format_hex_no_prefix(FlagsToPrint, AddrSize)) << ")";
4181 UnknownFlags &= ~Mask;
4182 };
4183
4184 PrintUnknownFlags(SHF_MASKOS, "OS");
4185 PrintUnknownFlags(SHF_MASKPROC, "PROC");
4186 PrintUnknownFlags(uint64_t(-1), "UNKNOWN");
4187
4188 OS << "\n";
4189 ++SectionIndex;
4190 }
4191}
4192
4193static inline std::string printPhdrFlags(unsigned Flag) {
4194 std::string Str;
4195 Str = (Flag & PF_R) ? "R" : " ";
4196 Str += (Flag & PF_W) ? "W" : " ";
4197 Str += (Flag & PF_X) ? "E" : " ";
4198 return Str;
4199}
4200
4201template <class ELFT>
4202static bool checkTLSSections(const typename ELFT::Phdr &Phdr,
4203 const typename ELFT::Shdr &Sec) {
4204 if (Sec.sh_flags & ELF::SHF_TLS) {
4205 // .tbss must only be shown in the PT_TLS segment.
4206 if (Sec.sh_type == ELF::SHT_NOBITS)
4207 return Phdr.p_type == ELF::PT_TLS;
4208
4209 // SHF_TLS sections are only shown in PT_TLS, PT_LOAD or PT_GNU_RELRO
4210 // segments.
4211 return (Phdr.p_type == ELF::PT_TLS) || (Phdr.p_type == ELF::PT_LOAD) ||
4212 (Phdr.p_type == ELF::PT_GNU_RELRO);
4213 }
4214
4215 // PT_TLS must only have SHF_TLS sections.
4216 return Phdr.p_type != ELF::PT_TLS;
4217}
4218
4219template <class ELFT>
4220static bool checkOffsets(const typename ELFT::Phdr &Phdr,
4221 const typename ELFT::Shdr &Sec) {
4222 // SHT_NOBITS sections don't need to have an offset inside the segment.
4223 if (Sec.sh_type == ELF::SHT_NOBITS)
4224 return true;
4225
4226 if (Sec.sh_offset < Phdr.p_offset)
4227 return false;
4228
4229 // Only non-empty sections can be at the end of a segment.
4230 if (Sec.sh_size == 0)
4231 return (Sec.sh_offset + 1 <= Phdr.p_offset + Phdr.p_filesz);
4232 return Sec.sh_offset + Sec.sh_size <= Phdr.p_offset + Phdr.p_filesz;
4233}
4234
4235// Check that an allocatable section belongs to a virtual address
4236// space of a segment.
4237template <class ELFT>
4238static bool checkVMA(const typename ELFT::Phdr &Phdr,
4239 const typename ELFT::Shdr &Sec) {
4240 if (!(Sec.sh_flags & ELF::SHF_ALLOC))
4241 return true;
4242
4243 if (Sec.sh_addr < Phdr.p_vaddr)
4244 return false;
4245
4246 bool IsTbss =
4247 (Sec.sh_type == ELF::SHT_NOBITS) && ((Sec.sh_flags & ELF::SHF_TLS) != 0);
4248 // .tbss is special, it only has memory in PT_TLS and has NOBITS properties.
4249 bool IsTbssInNonTLS = IsTbss && Phdr.p_type != ELF::PT_TLS;
4250 // Only non-empty sections can be at the end of a segment.
4251 if (Sec.sh_size == 0 || IsTbssInNonTLS)
4252 return Sec.sh_addr + 1 <= Phdr.p_vaddr + Phdr.p_memsz;
4253 return Sec.sh_addr + Sec.sh_size <= Phdr.p_vaddr + Phdr.p_memsz;
4254}
4255
4256template <class ELFT>
4257static bool checkPTDynamic(const typename ELFT::Phdr &Phdr,
4258 const typename ELFT::Shdr &Sec) {
4259 if (Phdr.p_type != ELF::PT_DYNAMIC || Phdr.p_memsz == 0 || Sec.sh_size != 0)
4260 return true;
4261
4262 // We get here when we have an empty section. Only non-empty sections can be
4263 // at the start or at the end of PT_DYNAMIC.
4264 // Is section within the phdr both based on offset and VMA?
4265 bool CheckOffset = (Sec.sh_type == ELF::SHT_NOBITS) ||
4266 (Sec.sh_offset > Phdr.p_offset &&
4267 Sec.sh_offset < Phdr.p_offset + Phdr.p_filesz);
4268 bool CheckVA = !(Sec.sh_flags & ELF::SHF_ALLOC) ||
4269 (Sec.sh_addr > Phdr.p_vaddr && Sec.sh_addr < Phdr.p_memsz);
4270 return CheckOffset && CheckVA;
4271}
4272
4273template <class ELFT>
4274void GNUELFDumper<ELFT>::printProgramHeaders(
4275 bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) {
4276 const bool ShouldPrintSectionMapping = (PrintSectionMapping != cl::BOU_FALSE);
4277 // Exit early if no program header or section mapping details were requested.
4278 if (!PrintProgramHeaders && !ShouldPrintSectionMapping)
4279 return;
4280
4281 if (PrintProgramHeaders) {
4282 const Elf_Ehdr &Header = this->Obj.getHeader();
4283 if (Header.e_phnum == 0) {
4284 OS << "\nThere are no program headers in this file.\n";
4285 } else {
4286 printProgramHeaders();
4287 }
4288 }
4289
4290 if (ShouldPrintSectionMapping)
4291 printSectionMapping();
4292}
4293
4294template <class ELFT> void GNUELFDumper<ELFT>::printProgramHeaders() {
4295 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
4296 const Elf_Ehdr &Header = this->Obj.getHeader();
4297 Field Fields[8] = {2, 17, 26, 37 + Bias,
4298 48 + Bias, 56 + Bias, 64 + Bias, 68 + Bias};
4299 OS << "\nElf file type is "
4300 << enumToString(Header.e_type, makeArrayRef(ElfObjectFileType)) << "\n"
4301 << "Entry point " << format_hex(Header.e_entry, 3) << "\n"
4302 << "There are " << Header.e_phnum << " program headers,"
4303 << " starting at offset " << Header.e_phoff << "\n\n"
4304 << "Program Headers:\n";
4305 if (ELFT::Is64Bits)
4306 OS << " Type Offset VirtAddr PhysAddr "
4307 << " FileSiz MemSiz Flg Align\n";
4308 else
4309 OS << " Type Offset VirtAddr PhysAddr FileSiz "
4310 << "MemSiz Flg Align\n";
4311
4312 unsigned Width = ELFT::Is64Bits ? 18 : 10;
4313 unsigned SizeWidth = ELFT::Is64Bits ? 8 : 7;
4314
4315 Expected<ArrayRef<Elf_Phdr>> PhdrsOrErr = this->Obj.program_headers();
4316 if (!PhdrsOrErr) {
4317 this->reportUniqueWarning("unable to dump program headers: " +
4318 toString(PhdrsOrErr.takeError()));
4319 return;
4320 }
4321
4322 for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
4323 Fields[0].Str = getGNUPtType(Header.e_machine, Phdr.p_type);
4324 Fields[1].Str = to_string(format_hex(Phdr.p_offset, 8));
4325 Fields[2].Str = to_string(format_hex(Phdr.p_vaddr, Width));
4326 Fields[3].Str = to_string(format_hex(Phdr.p_paddr, Width));
4327 Fields[4].Str = to_string(format_hex(Phdr.p_filesz, SizeWidth));
4328 Fields[5].Str = to_string(format_hex(Phdr.p_memsz, SizeWidth));
4329 Fields[6].Str = printPhdrFlags(Phdr.p_flags);
4330 Fields[7].Str = to_string(format_hex(Phdr.p_align, 1));
4331 for (const Field &F : Fields)
4332 printField(F);
4333 if (Phdr.p_type == ELF::PT_INTERP) {
4334 OS << "\n";
4335 auto ReportBadInterp = [&](const Twine &Msg) {
4336 this->reportUniqueWarning(
4337 "unable to read program interpreter name at offset 0x" +
4338 Twine::utohexstr(Phdr.p_offset) + ": " + Msg);
4339 };
4340
4341 if (Phdr.p_offset >= this->Obj.getBufSize()) {
4342 ReportBadInterp("it goes past the end of the file (0x" +
4343 Twine::utohexstr(this->Obj.getBufSize()) + ")");
4344 continue;
4345 }
4346
4347 const char *Data =
4348 reinterpret_cast<const char *>(this->Obj.base()) + Phdr.p_offset;
4349 size_t MaxSize = this->Obj.getBufSize() - Phdr.p_offset;
4350 size_t Len = strnlen(Data, MaxSize);
4351 if (Len == MaxSize) {
4352 ReportBadInterp("it is not null-terminated");
4353 continue;
4354 }
4355
4356 OS << " [Requesting program interpreter: ";
4357 OS << StringRef(Data, Len) << "]";
4358 }
4359 OS << "\n";
4360 }
4361}
4362
4363template <class ELFT> void GNUELFDumper<ELFT>::printSectionMapping() {
4364 OS << "\n Section to Segment mapping:\n Segment Sections...\n";
4365 DenseSet<const Elf_Shdr *> BelongsToSegment;
4366 int Phnum = 0;
4367
4368 Expected<ArrayRef<Elf_Phdr>> PhdrsOrErr = this->Obj.program_headers();
4369 if (!PhdrsOrErr) {
4370 this->reportUniqueWarning(
4371 "can't read program headers to build section to segment mapping: " +
4372 toString(PhdrsOrErr.takeError()));
4373 return;
4374 }
4375
4376 for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
4377 std::string Sections;
4378 OS << format(" %2.2d ", Phnum++);
4379 // Check if each section is in a segment and then print mapping.
4380 for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) {
4381 if (Sec.sh_type == ELF::SHT_NULL)
4382 continue;
4383
4384 // readelf additionally makes sure it does not print zero sized sections
4385 // at end of segments and for PT_DYNAMIC both start and end of section
4386 // .tbss must only be shown in PT_TLS section.
4387 if (checkTLSSections<ELFT>(Phdr, Sec) && checkOffsets<ELFT>(Phdr, Sec) &&
4388 checkVMA<ELFT>(Phdr, Sec) && checkPTDynamic<ELFT>(Phdr, Sec)) {
4389 Sections +=
4390 unwrapOrError(this->FileName, this->Obj.getSectionName(Sec)).str() +
4391 " ";
4392 BelongsToSegment.insert(&Sec);
4393 }
4394 }
4395 OS << Sections << "\n";
4396 OS.flush();
4397 }
4398
4399 // Display sections that do not belong to a segment.
4400 std::string Sections;
4401 for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) {
4402 if (BelongsToSegment.find(&Sec) == BelongsToSegment.end())
4403 Sections +=
4404 unwrapOrError(this->FileName, this->Obj.getSectionName(Sec)).str() +
4405 ' ';
4406 }
4407 if (!Sections.empty()) {
4408 OS << " None " << Sections << '\n';
4409 OS.flush();
4410 }
4411}
4412
4413namespace {
4414
4415template <class ELFT>
4416RelSymbol<ELFT> getSymbolForReloc(const ELFDumper<ELFT> &Dumper,
4417 const Relocation<ELFT> &Reloc) {
4418 using Elf_Sym = typename ELFT::Sym;
4419 auto WarnAndReturn = [&](const Elf_Sym *Sym,
4420 const Twine &Reason) -> RelSymbol<ELFT> {
4421 Dumper.reportUniqueWarning(
4422 "unable to get name of the dynamic symbol with index " +
4423 Twine(Reloc.Symbol) + ": " + Reason);
4424 return {Sym, "<corrupt>"};
4425 };
4426
4427 ArrayRef<Elf_Sym> Symbols = Dumper.dynamic_symbols();
4428 const Elf_Sym *FirstSym = Symbols.begin();
4429 if (!FirstSym)
4430 return WarnAndReturn(nullptr, "no dynamic symbol table found");
4431
4432 // We might have an object without a section header. In this case the size of
4433 // Symbols is zero, because there is no way to know the size of the dynamic
4434 // table. We should allow this case and not print a warning.
4435 if (!Symbols.empty() && Reloc.Symbol >= Symbols.size())
4436 return WarnAndReturn(
4437 nullptr,
4438 "index is greater than or equal to the number of dynamic symbols (" +
4439 Twine(Symbols.size()) + ")");
4440
4441 const ELFFile<ELFT> &Obj = Dumper.getElfObject().getELFFile();
4442 const uint64_t FileSize = Obj.getBufSize();
4443 const uint64_t SymOffset = ((const uint8_t *)FirstSym - Obj.base()) +
4444 (uint64_t)Reloc.Symbol * sizeof(Elf_Sym);
4445 if (SymOffset + sizeof(Elf_Sym) > FileSize)
4446 return WarnAndReturn(nullptr, "symbol at 0x" + Twine::utohexstr(SymOffset) +
4447 " goes past the end of the file (0x" +
4448 Twine::utohexstr(FileSize) + ")");
4449
4450 const Elf_Sym *Sym = FirstSym + Reloc.Symbol;
4451 Expected<StringRef> ErrOrName = Sym->getName(Dumper.getDynamicStringTable());
4452 if (!ErrOrName)
4453 return WarnAndReturn(Sym, toString(ErrOrName.takeError()));
4454
4455 return {Sym == FirstSym ? nullptr : Sym, maybeDemangle(*ErrOrName)};
4456}
4457} // namespace
4458
4459template <class ELFT>
4460static size_t getMaxDynamicTagSize(const ELFFile<ELFT> &Obj,
4461 typename ELFT::DynRange Tags) {
4462 size_t Max = 0;
4463 for (const typename ELFT::Dyn &Dyn : Tags)
4464 Max = std::max(Max, Obj.getDynamicTagAsString(Dyn.d_tag).size());
4465 return Max;
4466}
4467
4468template <class ELFT> void GNUELFDumper<ELFT>::printDynamicTable() {
4469 Elf_Dyn_Range Table = this->dynamic_table();
4470 if (Table.empty())
4471 return;
4472
4473 OS << "Dynamic section at offset "
4474 << format_hex(reinterpret_cast<const uint8_t *>(this->DynamicTable.Addr) -
4475 this->Obj.base(),
4476 1)
4477 << " contains " << Table.size() << " entries:\n";
4478
4479 // The type name is surrounded with round brackets, hence add 2.
4480 size_t MaxTagSize = getMaxDynamicTagSize(this->Obj, Table) + 2;
4481 // The "Name/Value" column should be indented from the "Type" column by N
4482 // spaces, where N = MaxTagSize - length of "Type" (4) + trailing
4483 // space (1) = 3.
4484 OS << " Tag" + std::string(ELFT::Is64Bits ? 16 : 8, ' ') + "Type"
4485 << std::string(MaxTagSize - 3, ' ') << "Name/Value\n";
4486
4487 std::string ValueFmt = " %-" + std::to_string(MaxTagSize) + "s ";
4488 for (auto Entry : Table) {
4489 uintX_t Tag = Entry.getTag();
4490 std::string Type =
4491 std::string("(") + this->Obj.getDynamicTagAsString(Tag) + ")";
4492 std::string Value = this->getDynamicEntry(Tag, Entry.getVal());
4493 OS << " " << format_hex(Tag, ELFT::Is64Bits ? 18 : 10)
4494 << format(ValueFmt.c_str(), Type.c_str()) << Value << "\n";
4495 }
4496}
4497
4498template <class ELFT> void GNUELFDumper<ELFT>::printDynamicRelocations() {
4499 this->printDynamicRelocationsHelper();
4500}
4501
4502template <class ELFT>
4503void ELFDumper<ELFT>::printDynamicReloc(const Relocation<ELFT> &R) {
4504 printRelRelaReloc(R, getSymbolForReloc(*this, R));
4505}
4506
4507template <class ELFT>
4508void ELFDumper<ELFT>::printRelocationsHelper(const Elf_Shdr &Sec) {
4509 this->forEachRelocationDo(
4
Calling 'ELFDumper::forEachRelocationDo'
4510 Sec, opts::RawRelr,
4511 [&](const Relocation<ELFT> &R, unsigned Ndx, const Elf_Shdr &Sec,
4512 const Elf_Shdr *SymTab) { printReloc(R, Ndx, Sec, SymTab); },
4513 [&](const Elf_Relr &R) { printRelrReloc(R); });
4514}
4515
4516template <class ELFT> void ELFDumper<ELFT>::printDynamicRelocationsHelper() {
4517 const bool IsMips64EL = this->Obj.isMips64EL();
4518 if (this->DynRelaRegion.Size > 0) {
4519 printDynamicRelocHeader(ELF::SHT_RELA, "RELA", this->DynRelaRegion);
4520 for (const Elf_Rela &Rela :
4521 this->DynRelaRegion.template getAsArrayRef<Elf_Rela>())
4522 printDynamicReloc(Relocation<ELFT>(Rela, IsMips64EL));
4523 }
4524
4525 if (this->DynRelRegion.Size > 0) {
4526 printDynamicRelocHeader(ELF::SHT_REL, "REL", this->DynRelRegion);
4527 for (const Elf_Rel &Rel :
4528 this->DynRelRegion.template getAsArrayRef<Elf_Rel>())
4529 printDynamicReloc(Relocation<ELFT>(Rel, IsMips64EL));
4530 }
4531
4532 if (this->DynRelrRegion.Size > 0) {
4533 printDynamicRelocHeader(ELF::SHT_REL, "RELR", this->DynRelrRegion);
4534 Elf_Relr_Range Relrs =
4535 this->DynRelrRegion.template getAsArrayRef<Elf_Relr>();
4536 for (const Elf_Rel &Rel : Obj.decode_relrs(Relrs))
4537 printDynamicReloc(Relocation<ELFT>(Rel, IsMips64EL));
4538 }
4539
4540 if (this->DynPLTRelRegion.Size) {
4541 if (this->DynPLTRelRegion.EntSize == sizeof(Elf_Rela)) {
4542 printDynamicRelocHeader(ELF::SHT_RELA, "PLT", this->DynPLTRelRegion);
4543 for (const Elf_Rela &Rela :
4544 this->DynPLTRelRegion.template getAsArrayRef<Elf_Rela>())
4545 printDynamicReloc(Relocation<ELFT>(Rela, IsMips64EL));
4546 } else {
4547 printDynamicRelocHeader(ELF::SHT_REL, "PLT", this->DynPLTRelRegion);
4548 for (const Elf_Rel &Rel :
4549 this->DynPLTRelRegion.template getAsArrayRef<Elf_Rel>())
4550 printDynamicReloc(Relocation<ELFT>(Rel, IsMips64EL));
4551 }
4552 }
4553}
4554
4555template <class ELFT>
4556void GNUELFDumper<ELFT>::printGNUVersionSectionProlog(
4557 const typename ELFT::Shdr &Sec, const Twine &Label, unsigned EntriesNum) {
4558 // Don't inline the SecName, because it might report a warning to stderr and
4559 // corrupt the output.
4560 StringRef SecName = this->getPrintableSectionName(Sec);
4561 OS << Label << " section '" << SecName << "' "
4562 << "contains " << EntriesNum << " entries:\n";
4563
4564 StringRef LinkedSecName = "<corrupt>";
4565 if (Expected<const typename ELFT::Shdr *> LinkedSecOrErr =
4566 this->Obj.getSection(Sec.sh_link))
4567 LinkedSecName = this->getPrintableSectionName(**LinkedSecOrErr);
4568 else
4569 this->reportUniqueWarning("invalid section linked to " +
4570 this->describe(Sec) + ": " +
4571 toString(LinkedSecOrErr.takeError()));
4572
4573 OS << " Addr: " << format_hex_no_prefix(Sec.sh_addr, 16)
4574 << " Offset: " << format_hex(Sec.sh_offset, 8)
4575 << " Link: " << Sec.sh_link << " (" << LinkedSecName << ")\n";
4576}
4577
4578template <class ELFT>
4579void GNUELFDumper<ELFT>::printVersionSymbolSection(const Elf_Shdr *Sec) {
4580 if (!Sec)
4581 return;
4582
4583 printGNUVersionSectionProlog(*Sec, "Version symbols",
4584 Sec->sh_size / sizeof(Elf_Versym));
4585 Expected<ArrayRef<Elf_Versym>> VerTableOrErr =
4586 this->getVersionTable(*Sec, /*SymTab=*/nullptr,
4587 /*StrTab=*/nullptr, /*SymTabSec=*/nullptr);
4588 if (!VerTableOrErr) {
4589 this->reportUniqueWarning(VerTableOrErr.takeError());
4590 return;
4591 }
4592
4593 SmallVector<Optional<VersionEntry>, 0> *VersionMap = nullptr;
4594 if (Expected<SmallVector<Optional<VersionEntry>, 0> *> MapOrErr =
4595 this->getVersionMap())
4596 VersionMap = *MapOrErr;
4597 else
4598 this->reportUniqueWarning(MapOrErr.takeError());
4599
4600 ArrayRef<Elf_Versym> VerTable = *VerTableOrErr;
4601 std::vector<StringRef> Versions;
4602 for (size_t I = 0, E = VerTable.size(); I < E; ++I) {
4603 unsigned Ndx = VerTable[I].vs_index;
4604 if (Ndx == VER_NDX_LOCAL || Ndx == VER_NDX_GLOBAL) {
4605 Versions.emplace_back(Ndx == VER_NDX_LOCAL ? "*local*" : "*global*");
4606 continue;
4607 }
4608
4609 if (!VersionMap) {
4610 Versions.emplace_back("<corrupt>");
4611 continue;
4612 }
4613
4614 bool IsDefault;
4615 Expected<StringRef> NameOrErr = this->Obj.getSymbolVersionByIndex(
4616 Ndx, IsDefault, *VersionMap, /*IsSymHidden=*/None);
4617 if (!NameOrErr) {
4618 this->reportUniqueWarning("unable to get a version for entry " +
4619 Twine(I) + " of " + this->describe(*Sec) +
4620 ": " + toString(NameOrErr.takeError()));
4621 Versions.emplace_back("<corrupt>");
4622 continue;
4623 }
4624 Versions.emplace_back(*NameOrErr);
4625 }
4626
4627 // readelf prints 4 entries per line.
4628 uint64_t Entries = VerTable.size();
4629 for (uint64_t VersymRow = 0; VersymRow < Entries; VersymRow += 4) {
4630 OS << " " << format_hex_no_prefix(VersymRow, 3) << ":";
4631 for (uint64_t I = 0; (I < 4) && (I + VersymRow) < Entries; ++I) {
4632 unsigned Ndx = VerTable[VersymRow + I].vs_index;
4633 OS << format("%4x%c", Ndx & VERSYM_VERSION,
4634 Ndx & VERSYM_HIDDEN ? 'h' : ' ');
4635 OS << left_justify("(" + std::string(Versions[VersymRow + I]) + ")", 13);
4636 }
4637 OS << '\n';
4638 }
4639 OS << '\n';
4640}
4641
4642static std::string versionFlagToString(unsigned Flags) {
4643 if (Flags == 0)
4644 return "none";
4645
4646 std::string Ret;
4647 auto AddFlag = [&Ret, &Flags](unsigned Flag, StringRef Name) {
4648 if (!(Flags & Flag))
4649 return;
4650 if (!Ret.empty())
4651 Ret += " | ";
4652 Ret += Name;
4653 Flags &= ~Flag;
4654 };
4655
4656 AddFlag(VER_FLG_BASE, "BASE");
4657 AddFlag(VER_FLG_WEAK, "WEAK");
4658 AddFlag(VER_FLG_INFO, "INFO");
4659 AddFlag(~0, "<unknown>");
4660 return Ret;
4661}
4662
4663template <class ELFT>
4664void GNUELFDumper<ELFT>::printVersionDefinitionSection(const Elf_Shdr *Sec) {
4665 if (!Sec)
4666 return;
4667
4668 printGNUVersionSectionProlog(*Sec, "Version definition", Sec->sh_info);
4669
4670 Expected<std::vector<VerDef>> V = this->Obj.getVersionDefinitions(*Sec);
4671 if (!V) {
4672 this->reportUniqueWarning(V.takeError());
4673 return;
4674 }
4675
4676 for (const VerDef &Def : *V) {
4677 OS << format(" 0x%04x: Rev: %u Flags: %s Index: %u Cnt: %u Name: %s\n",
4678 Def.Offset, Def.Version,
4679 versionFlagToString(Def.Flags).c_str(), Def.Ndx, Def.Cnt,
4680 Def.Name.data());
4681 unsigned I = 0;
4682 for (const VerdAux &Aux : Def.AuxV)
4683 OS << format(" 0x%04x: Parent %u: %s\n", Aux.Offset, ++I,
4684 Aux.Name.data());
4685 }
4686
4687 OS << '\n';
4688}
4689
4690template <class ELFT>
4691void GNUELFDumper<ELFT>::printVersionDependencySection(const Elf_Shdr *Sec) {
4692 if (!Sec)
4693 return;
4694
4695 unsigned VerneedNum = Sec->sh_info;
4696 printGNUVersionSectionProlog(*Sec, "Version needs", VerneedNum);
4697
4698 Expected<std::vector<VerNeed>> V =
4699 this->Obj.getVersionDependencies(*Sec, this->WarningHandler);
4700 if (!V) {
4701 this->reportUniqueWarning(V.takeError());
4702 return;
4703 }
4704
4705 for (const VerNeed &VN : *V) {
4706 OS << format(" 0x%04x: Version: %u File: %s Cnt: %u\n", VN.Offset,
4707 VN.Version, VN.File.data(), VN.Cnt);
4708 for (const VernAux &Aux : VN.AuxV)
4709 OS << format(" 0x%04x: Name: %s Flags: %s Version: %u\n", Aux.Offset,
4710 Aux.Name.data(), versionFlagToString(Aux.Flags).c_str(),
4711 Aux.Other);
4712 }
4713 OS << '\n';
4714}
4715
4716template <class ELFT>
4717void GNUELFDumper<ELFT>::printHashHistogram(const Elf_Hash &HashTable) {
4718 size_t NBucket = HashTable.nbucket;
4719 size_t NChain = HashTable.nchain;
4720 ArrayRef<Elf_Word> Buckets = HashTable.buckets();
4721 ArrayRef<Elf_Word> Chains = HashTable.chains();
4722 size_t TotalSyms = 0;
4723 // If hash table is correct, we have at least chains with 0 length
4724 size_t MaxChain = 1;
4725 size_t CumulativeNonZero = 0;
4726
4727 if (NChain == 0 || NBucket == 0)
4728 return;
4729
4730 std::vector<size_t> ChainLen(NBucket, 0);
4731 // Go over all buckets and and note chain lengths of each bucket (total
4732 // unique chain lengths).
4733 for (size_t B = 0; B < NBucket; B++) {
4734 BitVector Visited(NChain);
4735 for (size_t C = Buckets[B]; C < NChain; C = Chains[C]) {
4736 if (C == ELF::STN_UNDEF)
4737 break;
4738 if (Visited[C]) {
4739 this->reportUniqueWarning(".hash section is invalid: bucket " +
4740 Twine(C) +
4741 ": a cycle was detected in the linked chain");
4742 break;
4743 }
4744 Visited[C] = true;
4745 if (MaxChain <= ++ChainLen[B])
4746 MaxChain++;
4747 }
4748 TotalSyms += ChainLen[B];
4749 }
4750
4751 if (!TotalSyms)
4752 return;
4753
4754 std::vector<size_t> Count(MaxChain, 0);
4755 // Count how long is the chain for each bucket
4756 for (size_t B = 0; B < NBucket; B++)
4757 ++Count[ChainLen[B]];
4758 // Print Number of buckets with each chain lengths and their cumulative
4759 // coverage of the symbols
4760 OS << "Histogram for bucket list length (total of " << NBucket
4761 << " buckets)\n"
4762 << " Length Number % of total Coverage\n";
4763 for (size_t I = 0; I < MaxChain; I++) {
4764 CumulativeNonZero += Count[I] * I;
4765 OS << format("%7lu %-10lu (%5.1f%%) %5.1f%%\n", I, Count[I],
4766 (Count[I] * 100.0) / NBucket,
4767 (CumulativeNonZero * 100.0) / TotalSyms);
4768 }
4769}
4770
4771template <class ELFT>
4772void GNUELFDumper<ELFT>::printGnuHashHistogram(
4773 const Elf_GnuHash &GnuHashTable) {
4774 Expected<ArrayRef<Elf_Word>> ChainsOrErr =
4775 getGnuHashTableChains<ELFT>(this->DynSymRegion, &GnuHashTable);
4776 if (!ChainsOrErr) {
4777 this->reportUniqueWarning("unable to print the GNU hash table histogram: " +
4778 toString(ChainsOrErr.takeError()));
4779 return;
4780 }
4781
4782 ArrayRef<Elf_Word> Chains = *ChainsOrErr;
4783 size_t Symndx = GnuHashTable.symndx;
4784 size_t TotalSyms = 0;
4785 size_t MaxChain = 1;
4786 size_t CumulativeNonZero = 0;
4787
4788 size_t NBucket = GnuHashTable.nbuckets;
4789 if (Chains.empty() || NBucket == 0)
4790 return;
4791
4792 ArrayRef<Elf_Word> Buckets = GnuHashTable.buckets();
4793 std::vector<size_t> ChainLen(NBucket, 0);
4794 for (size_t B = 0; B < NBucket; B++) {
4795 if (!Buckets[B])
4796 continue;
4797 size_t Len = 1;
4798 for (size_t C = Buckets[B] - Symndx;
4799 C < Chains.size() && (Chains[C] & 1) == 0; C++)
4800 if (MaxChain < ++Len)
4801 MaxChain++;
4802 ChainLen[B] = Len;
4803 TotalSyms += Len;
4804 }
4805 MaxChain++;
4806
4807 if (!TotalSyms)
4808 return;
4809
4810 std::vector<size_t> Count(MaxChain, 0);
4811 for (size_t B = 0; B < NBucket; B++)
4812 ++Count[ChainLen[B]];
4813 // Print Number of buckets with each chain lengths and their cumulative
4814 // coverage of the symbols
4815 OS << "Histogram for `.gnu.hash' bucket list length (total of " << NBucket
4816 << " buckets)\n"
4817 << " Length Number % of total Coverage\n";
4818 for (size_t I = 0; I < MaxChain; I++) {
4819 CumulativeNonZero += Count[I] * I;
4820 OS << format("%7lu %-10lu (%5.1f%%) %5.1f%%\n", I, Count[I],
4821 (Count[I] * 100.0) / NBucket,
4822 (CumulativeNonZero * 100.0) / TotalSyms);
4823 }
4824}
4825
4826// Hash histogram shows statistics of how efficient the hash was for the
4827// dynamic symbol table. The table shows the number of hash buckets for
4828// different lengths of chains as an absolute number and percentage of the total
4829// buckets, and the cumulative coverage of symbols for each set of buckets.
4830template <class ELFT> void GNUELFDumper<ELFT>::printHashHistograms() {
4831 // Print histogram for the .hash section.
4832 if (this->HashTable) {
4833 if (Error E = checkHashTable<ELFT>(*this, this->HashTable))
4834 this->reportUniqueWarning(std::move(E));
4835 else
4836 printHashHistogram(*this->HashTable);
4837 }
4838
4839 // Print histogram for the .gnu.hash section.
4840 if (this->GnuHashTable) {
4841 if (Error E = checkGNUHashTable<ELFT>(this->Obj, this->GnuHashTable))
4842 this->reportUniqueWarning(std::move(E));
4843 else
4844 printGnuHashHistogram(*this->GnuHashTable);
4845 }
4846}
4847
4848template <class ELFT> void GNUELFDumper<ELFT>::printCGProfile() {
4849 OS << "GNUStyle::printCGProfile not implemented\n";
4850}
4851
4852template <class ELFT> void GNUELFDumper<ELFT>::printBBAddrMaps() {
4853 OS << "GNUStyle::printBBAddrMaps not implemented\n";
4854}
4855
4856static Expected<std::vector<uint64_t>> toULEB128Array(ArrayRef<uint8_t> Data) {
4857 std::vector<uint64_t> Ret;
4858 const uint8_t *Cur = Data.begin();
4859 const uint8_t *End = Data.end();
4860 while (Cur != End) {
4861 unsigned Size;
4862 const char *Err;
4863 Ret.push_back(decodeULEB128(Cur, &Size, End, &Err));
4864 if (Err)
4865 return createError(Err);
4866 Cur += Size;
4867 }
4868 return Ret;
4869}
4870
4871template <class ELFT>
4872static Expected<std::vector<uint64_t>>
4873decodeAddrsigSection(const ELFFile<ELFT> &Obj, const typename ELFT::Shdr &Sec) {
4874 Expected<ArrayRef<uint8_t>> ContentsOrErr = Obj.getSectionContents(Sec);
4875 if (!ContentsOrErr)
4876 return ContentsOrErr.takeError();
4877
4878 if (Expected<std::vector<uint64_t>> SymsOrErr =
4879 toULEB128Array(*ContentsOrErr))
4880 return *SymsOrErr;
4881 else
4882 return createError("unable to decode " + describe(Obj, Sec) + ": " +
4883 toString(SymsOrErr.takeError()));
4884}
4885
4886template <class ELFT> void GNUELFDumper<ELFT>::printAddrsig() {
4887 if (!this->DotAddrsigSec)
4888 return;
4889
4890 Expected<std::vector<uint64_t>> SymsOrErr =
4891 decodeAddrsigSection(this->Obj, *this->DotAddrsigSec);
4892 if (!SymsOrErr) {
4893 this->reportUniqueWarning(SymsOrErr.takeError());
4894 return;
4895 }
4896
4897 StringRef Name = this->getPrintableSectionName(*this->DotAddrsigSec);
4898 OS << "\nAddress-significant symbols section '" << Name << "'"
4899 << " contains " << SymsOrErr->size() << " entries:\n";
4900 OS << " Num: Name\n";
4901
4902 Field Fields[2] = {0, 8};
4903 size_t SymIndex = 0;
4904 for (uint64_t Sym : *SymsOrErr) {
4905 Fields[0].Str = to_string(format_decimal(++SymIndex, 6)) + ":";
4906 Fields[1].Str = this->getStaticSymbolName(Sym);
4907 for (const Field &Entry : Fields)
4908 printField(Entry);
4909 OS << "\n";
4910 }
4911}
4912
4913template <typename ELFT>
4914static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
4915 ArrayRef<uint8_t> Data) {
4916 std::string str;
4917 raw_string_ostream OS(str);
4918 uint32_t PrData;
4919 auto DumpBit = [&](uint32_t Flag, StringRef Name) {
4920 if (PrData & Flag) {
4921 PrData &= ~Flag;
4922 OS << Name;
4923 if (PrData)
4924 OS << ", ";
4925 }
4926 };
4927
4928 switch (Type) {
4929 default:
4930 OS << format("<application-specific type 0x%x>", Type);
4931 return OS.str();
4932 case GNU_PROPERTY_STACK_SIZE: {
4933 OS << "stack size: ";
4934 if (DataSize == sizeof(typename ELFT::uint))
4935 OS << formatv("{0:x}",
4936 (uint64_t)(*(const typename ELFT::Addr *)Data.data()));
4937 else
4938 OS << format("<corrupt length: 0x%x>", DataSize);
4939 return OS.str();
4940 }
4941 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
4942 OS << "no copy on protected";
4943 if (DataSize)
4944 OS << format(" <corrupt length: 0x%x>", DataSize);
4945 return OS.str();
4946 case GNU_PROPERTY_AARCH64_FEATURE_1_AND:
4947 case GNU_PROPERTY_X86_FEATURE_1_AND:
4948 OS << ((Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) ? "aarch64 feature: "
4949 : "x86 feature: ");
4950 if (DataSize != 4) {
4951 OS << format("<corrupt length: 0x%x>", DataSize);
4952 return OS.str();
4953 }
4954 PrData = support::endian::read32<ELFT::TargetEndianness>(Data.data());
4955 if (PrData == 0) {
4956 OS << "<None>";
4957 return OS.str();
4958 }
4959 if (Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
4960 DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI");
4961 DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC");
4962 } else {
4963 DumpBit(GNU_PROPERTY_X86_FEATURE_1_IBT, "IBT");
4964 DumpBit(GNU_PROPERTY_X86_FEATURE_1_SHSTK, "SHSTK");
4965 }
4966 if (PrData)
4967 OS << format("<unknown flags: 0x%x>", PrData);
4968 return OS.str();
4969 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
4970 case GNU_PROPERTY_X86_FEATURE_2_USED:
4971 OS << "x86 feature "
4972 << (Type == GNU_PROPERTY_X86_FEATURE_2_NEEDED ? "needed: " : "used: ");
4973 if (DataSize != 4) {
4974 OS << format("<corrupt length: 0x%x>", DataSize);
4975 return OS.str();
4976 }
4977 PrData = support::endian::read32<ELFT::TargetEndianness>(Data.data());
4978 if (PrData == 0) {
4979 OS << "<None>";
4980 return OS.str();
4981 }
4982 DumpBit(GNU_PROPERTY_X86_FEATURE_2_X86, "x86");
4983 DumpBit(GNU_PROPERTY_X86_FEATURE_2_X87, "x87");
4984 DumpBit(GNU_PROPERTY_X86_FEATURE_2_MMX, "MMX");
4985 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XMM, "XMM");
4986 DumpBit(GNU_PROPERTY_X86_FEATURE_2_YMM, "YMM");
4987 DumpBit(GNU_PROPERTY_X86_FEATURE_2_ZMM, "ZMM");
4988 DumpBit(GNU_PROPERTY_X86_FEATURE_2_FXSR, "FXSR");
4989 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVE, "XSAVE");
4990 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT, "XSAVEOPT");
4991 DumpBit(GNU_PROPERTY_X86_FEATURE_2_XSAVEC, "XSAVEC");
4992 if (PrData)
4993 OS << format("<unknown flags: 0x%x>", PrData);
4994 return OS.str();
4995 case GNU_PROPERTY_X86_ISA_1_NEEDED:
4996 case GNU_PROPERTY_X86_ISA_1_USED:
4997 OS << "x86 ISA "
4998 << (Type == GNU_PROPERTY_X86_ISA_1_NEEDED ? "needed: " : "used: ");
4999 if (DataSize != 4) {
5000 OS << format("<corrupt length: 0x%x>", DataSize);
5001 return OS.str();
5002 }
5003 PrData = support::endian::read32<ELFT::TargetEndianness>(Data.data());
5004 if (PrData == 0) {
5005 OS << "<None>";
5006 return OS.str();
5007 }
5008 DumpBit(GNU_PROPERTY_X86_ISA_1_BASELINE, "x86-64-baseline");
5009 DumpBit(GNU_PROPERTY_X86_ISA_1_V2, "x86-64-v2");
5010 DumpBit(GNU_PROPERTY_X86_ISA_1_V3, "x86-64-v3");
5011 DumpBit(GNU_PROPERTY_X86_ISA_1_V4, "x86-64-v4");
5012 if (PrData)
5013 OS << format("<unknown flags: 0x%x>", PrData);
5014 return OS.str();
5015 }
5016}
5017
5018template <typename ELFT>
5019static SmallVector<std::string, 4> getGNUPropertyList(ArrayRef<uint8_t> Arr) {
5020 using Elf_Word = typename ELFT::Word;
5021
5022 SmallVector<std::string, 4> Properties;
5023 while (Arr.size() >= 8) {
5024 uint32_t Type = *reinterpret_cast<const Elf_Word *>(Arr.data());
5025 uint32_t DataSize = *reinterpret_cast<const Elf_Word *>(Arr.data() + 4);
5026 Arr = Arr.drop_front(8);
5027
5028 // Take padding size into account if present.
5029 uint64_t PaddedSize = alignTo(DataSize, sizeof(typename ELFT::uint));
5030 std::string str;
5031 raw_string_ostream OS(str);
5032 if (Arr.size() < PaddedSize) {
5033 OS << format("<corrupt type (0x%x) datasz: 0x%x>", Type, DataSize);
5034 Properties.push_back(OS.str());
5035 break;
5036 }
5037 Properties.push_back(
5038 getGNUProperty<ELFT>(Type, DataSize, Arr.take_front(PaddedSize)));
5039 Arr = Arr.drop_front(PaddedSize);
5040 }
5041
5042 if (!Arr.empty())
5043 Properties.push_back("<corrupted GNU_PROPERTY_TYPE_0>");
5044
5045 return Properties;
5046}
5047
5048struct GNUAbiTag {
5049 std::string OSName;
5050 std::string ABI;
5051 bool IsValid;
5052};
5053
5054template <typename ELFT> static GNUAbiTag getGNUAbiTag(ArrayRef<uint8_t> Desc) {
5055 typedef typename ELFT::Word Elf_Word;
5056
5057 ArrayRef<Elf_Word> Words(reinterpret_cast<const Elf_Word *>(Desc.begin()),
5058 reinterpret_cast<const Elf_Word *>(Desc.end()));
5059
5060 if (Words.size() < 4)
5061 return {"", "", /*IsValid=*/false};
5062
5063 static const char *OSNames[] = {
5064 "Linux", "Hurd", "Solaris", "FreeBSD", "NetBSD", "Syllable", "NaCl",
5065 };
5066 StringRef OSName = "Unknown";
5067 if (Words[0] < array_lengthof(OSNames))
5068 OSName = OSNames[Words[0]];
5069 uint32_t Major = Words[1], Minor = Words[2], Patch = Words[3];
5070 std::string str;
5071 raw_string_ostream ABI(str);
5072 ABI << Major << "." << Minor << "." << Patch;
5073 return {std::string(OSName), ABI.str(), /*IsValid=*/true};
5074}
5075
5076static std::string getGNUBuildId(ArrayRef<uint8_t> Desc) {
5077 std::string str;
5078 raw_string_ostream OS(str);
5079 for (uint8_t B : Desc)
5080 OS << format_hex_no_prefix(B, 2);
5081 return OS.str();
5082}
5083
5084static StringRef getDescAsStringRef(ArrayRef<uint8_t> Desc) {
5085 return StringRef(reinterpret_cast<const char *>(Desc.data()), Desc.size());
5086}
5087
5088template <typename ELFT>
5089static bool printGNUNote(raw_ostream &OS, uint32_t NoteType,
5090 ArrayRef<uint8_t> Desc) {
5091 // Return true if we were able to pretty-print the note, false otherwise.
5092 switch (NoteType) {
5093 default:
5094 return false;
5095 case ELF::NT_GNU_ABI_TAG: {
5096 const GNUAbiTag &AbiTag = getGNUAbiTag<ELFT>(Desc);
5097 if (!AbiTag.IsValid)
5098 OS << " <corrupt GNU_ABI_TAG>";
5099 else
5100 OS << " OS: " << AbiTag.OSName << ", ABI: " << AbiTag.ABI;
5101 break;
5102 }
5103 case ELF::NT_GNU_BUILD_ID: {
5104 OS << " Build ID: " << getGNUBuildId(Desc);
5105 break;
5106 }
5107 case ELF::NT_GNU_GOLD_VERSION:
5108 OS << " Version: " << getDescAsStringRef(Desc);
5109 break;
5110 case ELF::NT_GNU_PROPERTY_TYPE_0:
5111 OS << " Properties:";
5112 for (const std::string &Property : getGNUPropertyList<ELFT>(Desc))
5113 OS << " " << Property << "\n";
5114 break;
5115 }
5116 OS << '\n';
5117 return true;
5118}
5119
5120using AndroidNoteProperties = std::vector<std::pair<StringRef, std::string>>;
5121static AndroidNoteProperties getAndroidNoteProperties(uint32_t NoteType,
5122 ArrayRef<uint8_t> Desc) {
5123 AndroidNoteProperties Props;
5124 switch (NoteType) {
5125 case ELF::NT_ANDROID_TYPE_MEMTAG:
5126 if (Desc.empty()) {
5127 Props.emplace_back("Invalid .note.android.memtag", "");
5128 return Props;
5129 }
5130
5131 switch (Desc[0] & NT_MEMTAG_LEVEL_MASK) {
5132 case NT_MEMTAG_LEVEL_NONE:
5133 Props.emplace_back("Tagging Mode", "NONE");
5134 break;
5135 case NT_MEMTAG_LEVEL_ASYNC:
5136 Props.emplace_back("Tagging Mode", "ASYNC");
5137 break;
5138 case NT_MEMTAG_LEVEL_SYNC:
5139 Props.emplace_back("Tagging Mode", "SYNC");
5140 break;
5141 default:
5142 Props.emplace_back(
5143 "Tagging Mode",
5144 ("Unknown (" + Twine::utohexstr(Desc[0] & NT_MEMTAG_LEVEL_MASK) + ")")
5145 .str());
5146 break;
5147 }
5148 Props.emplace_back("Heap",
5149 (Desc[0] & NT_MEMTAG_HEAP) ? "Enabled" : "Disabled");
5150 Props.emplace_back("Stack",
5151 (Desc[0] & NT_MEMTAG_STACK) ? "Enabled" : "Disabled");
5152 break;
5153 default:
5154 return Props;
5155 }
5156 return Props;
5157}
5158
5159static bool printAndroidNote(raw_ostream &OS, uint32_t NoteType,
5160 ArrayRef<uint8_t> Desc) {
5161 // Return true if we were able to pretty-print the note, false otherwise.
5162 AndroidNoteProperties Props = getAndroidNoteProperties(NoteType, Desc);
5163 if (Props.empty())
5164 return false;
5165 for (const auto &KV : Props)
5166 OS << " " << KV.first << ": " << KV.second << '\n';
5167 OS << '\n';
5168 return true;
5169}
5170
5171template <typename ELFT>
5172static bool printLLVMOMPOFFLOADNote(raw_ostream &OS, uint32_t NoteType,
5173 ArrayRef<uint8_t> Desc) {
5174 switch (NoteType) {
5175 default:
5176 return false;
5177 case ELF::NT_LLVM_OPENMP_OFFLOAD_VERSION:
5178 OS << " Version: " << getDescAsStringRef(Desc);
5179 break;
5180 case ELF::NT_LLVM_OPENMP_OFFLOAD_PRODUCER:
5181 OS << " Producer: " << getDescAsStringRef(Desc);
5182 break;
5183 case ELF::NT_LLVM_OPENMP_OFFLOAD_PRODUCER_VERSION:
5184 OS << " Producer version: " << getDescAsStringRef(Desc);
5185 break;
5186 }
5187 OS << '\n';
5188 return true;
5189}
5190
5191const EnumEntry<unsigned> FreeBSDFeatureCtlFlags[] = {
5192 {"ASLR_DISABLE", NT_FREEBSD_FCTL_ASLR_DISABLE},
5193 {"PROTMAX_DISABLE", NT_FREEBSD_FCTL_PROTMAX_DISABLE},
5194 {"STKGAP_DISABLE", NT_FREEBSD_FCTL_STKGAP_DISABLE},
5195 {"WXNEEDED", NT_FREEBSD_FCTL_WXNEEDED},
5196 {"LA48", NT_FREEBSD_FCTL_LA48},
5197 {"ASG_DISABLE", NT_FREEBSD_FCTL_ASG_DISABLE},
5198};
5199
5200struct FreeBSDNote {
5201 std::string Type;
5202 std::string Value;
5203};
5204
5205template <typename ELFT>
5206static Optional<FreeBSDNote>
5207getFreeBSDNote(uint32_t NoteType, ArrayRef<uint8_t> Desc, bool IsCore) {
5208 if (IsCore)
5209 return None; // No pretty-printing yet.
5210 switch (NoteType) {
5211 case ELF::NT_FREEBSD_ABI_TAG:
5212 if (Desc.size() != 4)
5213 return None;
5214 return FreeBSDNote{
5215 "ABI tag",
5216 utostr(support::endian::read32<ELFT::TargetEndianness>(Desc.data()))};
5217 case ELF::NT_FREEBSD_ARCH_TAG:
5218 return FreeBSDNote{"Arch tag", toStringRef(Desc).str()};
5219 case ELF::NT_FREEBSD_FEATURE_CTL: {
5220 if (Desc.size() != 4)
5221 return None;
5222 unsigned Value =
5223 support::endian::read32<ELFT::TargetEndianness>(Desc.data());
5224 std::string FlagsStr;
5225 raw_string_ostream OS(FlagsStr);
5226 printFlags(Value, makeArrayRef(FreeBSDFeatureCtlFlags), OS);
5227 if (OS.str().empty())
5228 OS << "0x" << utohexstr(Value);
5229 else
5230 OS << "(0x" << utohexstr(Value) << ")";
5231 return FreeBSDNote{"Feature flags", OS.str()};
5232 }
5233 default:
5234 return None;
5235 }
5236}
5237
5238struct AMDNote {
5239 std::string Type;
5240 std::string Value;
5241};
5242
5243template <typename ELFT>
5244static AMDNote getAMDNote(uint32_t NoteType, ArrayRef<uint8_t> Desc) {
5245 switch (NoteType) {
5246 default:
5247 return {"", ""};
5248 case ELF::NT_AMD_HSA_CODE_OBJECT_VERSION: {
5249 struct CodeObjectVersion {
5250 uint32_t MajorVersion;
5251 uint32_t MinorVersion;
5252 };
5253 if (Desc.size() != sizeof(CodeObjectVersion))
5254 return {"AMD HSA Code Object Version",
5255 "Invalid AMD HSA Code Object Version"};
5256 std::string VersionString;
5257 raw_string_ostream StrOS(VersionString);
5258 auto Version = reinterpret_cast<const CodeObjectVersion *>(Desc.data());
5259 StrOS << "[Major: " << Version->MajorVersion
5260 << ", Minor: " << Version->MinorVersion << "]";
5261 return {"AMD HSA Code Object Version", VersionString};
5262 }
5263 case ELF::NT_AMD_HSA_HSAIL: {
5264 struct HSAILProperties {
5265 uint32_t HSAILMajorVersion;
5266 uint32_t HSAILMinorVersion;
5267 uint8_t Profile;
5268 uint8_t MachineModel;
5269 uint8_t DefaultFloatRound;
5270 };
5271 if (Desc.size() != sizeof(HSAILProperties))
5272 return {"AMD HSA HSAIL Properties", "Invalid AMD HSA HSAIL Properties"};
5273 auto Properties = reinterpret_cast<const HSAILProperties *>(Desc.data());
5274 std::string HSAILPropetiesString;
5275 raw_string_ostream StrOS(HSAILPropetiesString);
5276 StrOS << "[HSAIL Major: " << Properties->HSAILMajorVersion
5277 << ", HSAIL Minor: " << Properties->HSAILMinorVersion
5278 << ", Profile: " << uint32_t(Properties->Profile)
5279 << ", Machine Model: " << uint32_t(Properties->MachineModel)
5280 << ", Default Float Round: "
5281 << uint32_t(Properties->DefaultFloatRound) << "]";
5282 return {"AMD HSA HSAIL Properties", HSAILPropetiesString};
5283 }
5284 case ELF::NT_AMD_HSA_ISA_VERSION: {
5285 struct IsaVersion {
5286 uint16_t VendorNameSize;
5287 uint16_t ArchitectureNameSize;
5288 uint32_t Major;
5289 uint32_t Minor;
5290 uint32_t Stepping;
5291 };
5292 if (Desc.size() < sizeof(IsaVersion))
5293 return {"AMD HSA ISA Version", "Invalid AMD HSA ISA Version"};
5294 auto Isa = reinterpret_cast<const IsaVersion *>(Desc.data());
5295 if (Desc.size() < sizeof(IsaVersion) +
5296 Isa->VendorNameSize + Isa->ArchitectureNameSize ||
5297 Isa->VendorNameSize == 0 || Isa->ArchitectureNameSize == 0)
5298 return {"AMD HSA ISA Version", "Invalid AMD HSA ISA Version"};
5299 std::string IsaString;
5300 raw_string_ostream StrOS(IsaString);
5301 StrOS << "[Vendor: "
5302 << StringRef((const char*)Desc.data() + sizeof(IsaVersion), Isa->VendorNameSize - 1)
5303 << ", Architecture: "
5304 << StringRef((const char*)Desc.data() + sizeof(IsaVersion) + Isa->VendorNameSize,
5305 Isa->ArchitectureNameSize - 1)
5306 << ", Major: " << Isa->Major << ", Minor: " << Isa->Minor
5307 << ", Stepping: " << Isa->Stepping << "]";
5308 return {"AMD HSA ISA Version", IsaString};
5309 }
5310 case ELF::NT_AMD_HSA_METADATA: {
5311 if (Desc.size() == 0)
5312 return {"AMD HSA Metadata", ""};
5313 return {
5314 "AMD HSA Metadata",
5315 std::string(reinterpret_cast<const char *>(Desc.data()), Desc.size() - 1)};
5316 }
5317 case ELF::NT_AMD_HSA_ISA_NAME: {
5318 if (Desc.size() == 0)
5319 return {"AMD HSA ISA Name", ""};
5320 return {
5321 "AMD HSA ISA Name",
5322 std::string(reinterpret_cast<const char *>(Desc.data()), Desc.size())};
5323 }
5324 case ELF::NT_AMD_PAL_METADATA: {
5325 struct PALMetadata {
5326 uint32_t Key;
5327 uint32_t Value;
5328 };
5329 if (Desc.size() % sizeof(PALMetadata) != 0)
5330 return {"AMD PAL Metadata", "Invalid AMD PAL Metadata"};
5331 auto Isa = reinterpret_cast<const PALMetadata *>(Desc.data());
5332 std::string MetadataString;
5333 raw_string_ostream StrOS(MetadataString);
5334 for (size_t I = 0, E = Desc.size() / sizeof(PALMetadata); I < E; ++I) {
5335 StrOS << "[" << Isa[I].Key << ": " << Isa[I].Value << "]";
5336 }
5337 return {"AMD PAL Metadata", MetadataString};
5338 }
5339 }
5340}
5341
5342struct AMDGPUNote {
5343 std::string Type;
5344 std::string Value;
5345};
5346
5347template <typename ELFT>
5348static AMDGPUNote getAMDGPUNote(uint32_t NoteType, ArrayRef<uint8_t> Desc) {
5349 switch (NoteType) {
5350 default:
5351 return {"", ""};
5352 case ELF::NT_AMDGPU_METADATA: {
5353 StringRef MsgPackString =
5354 StringRef(reinterpret_cast<const char *>(Desc.data()), Desc.size());
5355 msgpack::Document MsgPackDoc;
5356 if (!MsgPackDoc.readFromBlob(MsgPackString, /*Multi=*/false))
5357 return {"", ""};
5358
5359 AMDGPU::HSAMD::V3::MetadataVerifier Verifier(true);
5360 std::string MetadataString;
5361 if (!Verifier.verify(MsgPackDoc.getRoot()))
5362 MetadataString = "Invalid AMDGPU Metadata\n";
5363
5364 raw_string_ostream StrOS(MetadataString);
5365 if (MsgPackDoc.getRoot().isScalar()) {
5366 // TODO: passing a scalar root to toYAML() asserts:
5367 // (PolymorphicTraits<T>::getKind(Val) != NodeKind::Scalar &&
5368 // "plain scalar documents are not supported")
5369 // To avoid this crash we print the raw data instead.
5370 return {"", ""};
5371 }
5372 MsgPackDoc.toYAML(StrOS);
5373 return {"AMDGPU Metadata", StrOS.str()};
5374 }
5375 }
5376}
5377
5378struct CoreFileMapping {
5379 uint64_t Start, End, Offset;
5380 StringRef Filename;
5381};
5382
5383struct CoreNote {
5384 uint64_t PageSize;
5385 std::vector<CoreFileMapping> Mappings;
5386};
5387
5388static Expected<CoreNote> readCoreNote(DataExtractor Desc) {
5389 // Expected format of the NT_FILE note description:
5390 // 1. # of file mappings (call it N)
5391 // 2. Page size
5392 // 3. N (start, end, offset) triples
5393 // 4. N packed filenames (null delimited)
5394 // Each field is an Elf_Addr, except for filenames which are char* strings.
5395
5396 CoreNote Ret;
5397 const int Bytes = Desc.getAddressSize();
5398
5399 if (!Desc.isValidOffsetForAddress(2))
5400 return createError("the note of size 0x" + Twine::utohexstr(Desc.size()) +
5401 " is too short, expected at least 0x" +
5402 Twine::utohexstr(Bytes * 2));
5403 if (Desc.getData().back() != 0)
5404 return createError("the note is not NUL terminated");
5405
5406 uint64_t DescOffset = 0;
5407 uint64_t FileCount = Desc.getAddress(&DescOffset);
5408 Ret.PageSize = Desc.getAddress(&DescOffset);
5409
5410 if (!Desc.isValidOffsetForAddress(3 * FileCount * Bytes))
5411 return createError("unable to read file mappings (found " +
5412 Twine(FileCount) + "): the note of size 0x" +
5413 Twine::utohexstr(Desc.size()) + " is too short");
5414
5415 uint64_t FilenamesOffset = 0;
5416 DataExtractor Filenames(
5417 Desc.getData().drop_front(DescOffset + 3 * FileCount * Bytes),
5418 Desc.isLittleEndian(), Desc.getAddressSize());
5419
5420 Ret.Mappings.resize(FileCount);
5421 size_t I = 0;
5422 for (CoreFileMapping &Mapping : Ret.Mappings) {
5423 ++I;
5424 if (!Filenames.isValidOffsetForDataOfSize(FilenamesOffset, 1))
5425 return createError(
5426 "unable to read the file name for the mapping with index " +
5427 Twine(I) + ": the note of size 0x" + Twine::utohexstr(Desc.size()) +
5428 " is truncated");
5429 Mapping.Start = Desc.getAddress(&DescOffset);
5430 Mapping.End = Desc.getAddress(&DescOffset);
5431 Mapping.Offset = Desc.getAddress(&DescOffset);
5432 Mapping.Filename = Filenames.getCStrRef(&FilenamesOffset);
5433 }
5434
5435 return Ret;
5436}
5437
5438template <typename ELFT>
5439static void printCoreNote(raw_ostream &OS, const CoreNote &Note) {
5440 // Length of "0x<address>" string.
5441 const int FieldWidth = ELFT::Is64Bits ? 18 : 10;
5442
5443 OS << " Page size: " << format_decimal(Note.PageSize, 0) << '\n';
5444 OS << " " << right_justify("Start", FieldWidth) << " "
5445 << right_justify("End", FieldWidth) << " "
5446 << right_justify("Page Offset", FieldWidth) << '\n';
5447 for (const CoreFileMapping &Mapping : Note.Mappings) {
5448 OS << " " << format_hex(Mapping.Start, FieldWidth) << " "
5449 << format_hex(Mapping.End, FieldWidth) << " "
5450 << format_hex(Mapping.Offset, FieldWidth) << "\n "
5451 << Mapping.Filename << '\n';
5452 }
5453}
5454
5455const NoteType GenericNoteTypes[] = {
5456 {ELF::NT_VERSION, "NT_VERSION (version)"},
5457 {ELF::NT_ARCH, "NT_ARCH (architecture)"},
5458 {ELF::NT_GNU_BUILD_ATTRIBUTE_OPEN, "OPEN"},
5459 {ELF::NT_GNU_BUILD_ATTRIBUTE_FUNC, "func"},
5460};
5461
5462const NoteType GNUNoteTypes[] = {
5463 {ELF::NT_GNU_ABI_TAG, "NT_GNU_ABI_TAG (ABI version tag)"},
5464 {ELF::NT_GNU_HWCAP, "NT_GNU_HWCAP (DSO-supplied software HWCAP info)"},
5465 {ELF::NT_GNU_BUILD_ID, "NT_GNU_BUILD_ID (unique build ID bitstring)"},
5466 {ELF::NT_GNU_GOLD_VERSION, "NT_GNU_GOLD_VERSION (gold version)"},
5467 {ELF::NT_GNU_PROPERTY_TYPE_0, "NT_GNU_PROPERTY_TYPE_0 (property note)"},
5468};
5469
5470const NoteType FreeBSDCoreNoteTypes[] = {
5471 {ELF::NT_FREEBSD_THRMISC, "NT_THRMISC (thrmisc structure)"},
5472 {ELF::NT_FREEBSD_PROCSTAT_PROC, "NT_PROCSTAT_PROC (proc data)"},
5473 {ELF::NT_FREEBSD_PROCSTAT_FILES, "NT_PROCSTAT_FILES (files data)"},
5474 {ELF::NT_FREEBSD_PROCSTAT_VMMAP, "NT_PROCSTAT_VMMAP (vmmap data)"},
5475 {ELF::NT_FREEBSD_PROCSTAT_GROUPS, "NT_PROCSTAT_GROUPS (groups data)"},
5476 {ELF::NT_FREEBSD_PROCSTAT_UMASK, "NT_PROCSTAT_UMASK (umask data)"},
5477 {ELF::NT_FREEBSD_PROCSTAT_RLIMIT, "NT_PROCSTAT_RLIMIT (rlimit data)"},
5478 {ELF::NT_FREEBSD_PROCSTAT_OSREL, "NT_PROCSTAT_OSREL (osreldate data)"},
5479 {ELF::NT_FREEBSD_PROCSTAT_PSSTRINGS,
5480 "NT_PROCSTAT_PSSTRINGS (ps_strings data)"},
5481 {ELF::NT_FREEBSD_PROCSTAT_AUXV, "NT_PROCSTAT_AUXV (auxv data)"},
5482};
5483
5484const NoteType FreeBSDNoteTypes[] = {
5485 {ELF::NT_FREEBSD_ABI_TAG, "NT_FREEBSD_ABI_TAG (ABI version tag)"},
5486 {ELF::NT_FREEBSD_NOINIT_TAG, "NT_FREEBSD_NOINIT_TAG (no .init tag)"},
5487 {ELF::NT_FREEBSD_ARCH_TAG, "NT_FREEBSD_ARCH_TAG (architecture tag)"},
5488 {ELF::NT_FREEBSD_FEATURE_CTL,
5489 "NT_FREEBSD_FEATURE_CTL (FreeBSD feature control)"},
5490};
5491
5492const NoteType NetBSDCoreNoteTypes[] = {
5493 {ELF::NT_NETBSDCORE_PROCINFO,
5494 "NT_NETBSDCORE_PROCINFO (procinfo structure)"},
5495 {ELF::NT_NETBSDCORE_AUXV, "NT_NETBSDCORE_AUXV (ELF auxiliary vector data)"},
5496 {ELF::NT_NETBSDCORE_LWPSTATUS, "PT_LWPSTATUS (ptrace_lwpstatus structure)"},
5497};
5498
5499const NoteType OpenBSDCoreNoteTypes[] = {
5500 {ELF::NT_OPENBSD_PROCINFO, "NT_OPENBSD_PROCINFO (procinfo structure)"},
5501 {ELF::NT_OPENBSD_AUXV, "NT_OPENBSD_AUXV (ELF auxiliary vector data)"},
5502 {ELF::NT_OPENBSD_REGS, "NT_OPENBSD_REGS (regular registers)"},
5503 {ELF::NT_OPENBSD_FPREGS, "NT_OPENBSD_FPREGS (floating point registers)"},
5504 {ELF::NT_OPENBSD_WCOOKIE, "NT_OPENBSD_WCOOKIE (window cookie)"},
5505};
5506
5507const NoteType AMDNoteTypes[] = {
5508 {ELF::NT_AMD_HSA_CODE_OBJECT_VERSION,
5509 "NT_AMD_HSA_CODE_OBJECT_VERSION (AMD HSA Code Object Version)"},
5510 {ELF::NT_AMD_HSA_HSAIL, "NT_AMD_HSA_HSAIL (AMD HSA HSAIL Properties)"},
5511 {ELF::NT_AMD_HSA_ISA_VERSION, "NT_AMD_HSA_ISA_VERSION (AMD HSA ISA Version)"},
5512 {ELF::NT_AMD_HSA_METADATA, "NT_AMD_HSA_METADATA (AMD HSA Metadata)"},
5513 {ELF::NT_AMD_HSA_ISA_NAME, "NT_AMD_HSA_ISA_NAME (AMD HSA ISA Name)"},
5514 {ELF::NT_AMD_PAL_METADATA, "NT_AMD_PAL_METADATA (AMD PAL Metadata)"},
5515};
5516
5517const NoteType AMDGPUNoteTypes[] = {
5518 {ELF::NT_AMDGPU_METADATA, "NT_AMDGPU_METADATA (AMDGPU Metadata)"},
5519};
5520
5521const NoteType LLVMOMPOFFLOADNoteTypes[] = {
5522 {ELF::NT_LLVM_OPENMP_OFFLOAD_VERSION,
5523 "NT_LLVM_OPENMP_OFFLOAD_VERSION (image format version)"},
5524 {ELF::NT_LLVM_OPENMP_OFFLOAD_PRODUCER,
5525 "NT_LLVM_OPENMP_OFFLOAD_PRODUCER (producing toolchain)"},
5526 {ELF::NT_LLVM_OPENMP_OFFLOAD_PRODUCER_VERSION,
5527 "NT_LLVM_OPENMP_OFFLOAD_PRODUCER_VERSION (producing toolchain version)"},
5528};
5529
5530const NoteType AndroidNoteTypes[] = {
5531 {ELF::NT_ANDROID_TYPE_IDENT, "NT_ANDROID_TYPE_IDENT"},
5532 {ELF::NT_ANDROID_TYPE_KUSER, "NT_ANDROID_TYPE_KUSER"},
5533 {ELF::NT_ANDROID_TYPE_MEMTAG,
5534 "NT_ANDROID_TYPE_MEMTAG (Android memory tagging information)"},
5535};
5536
5537const NoteType CoreNoteTypes[] = {
5538 {ELF::NT_PRSTATUS, "NT_PRSTATUS (prstatus structure)"},
5539 {ELF::NT_FPREGSET, "NT_FPREGSET (floating point registers)"},
5540 {ELF::NT_PRPSINFO, "NT_PRPSINFO (prpsinfo structure)"},
5541 {ELF::NT_TASKSTRUCT, "NT_TASKSTRUCT (task structure)"},
5542 {ELF::NT_AUXV, "NT_AUXV (auxiliary vector)"},
5543 {ELF::NT_PSTATUS, "NT_PSTATUS (pstatus structure)"},
5544 {ELF::NT_FPREGS, "NT_FPREGS (floating point registers)"},
5545 {ELF::NT_PSINFO, "NT_PSINFO (psinfo structure)"},
5546 {ELF::NT_LWPSTATUS, "NT_LWPSTATUS (lwpstatus_t structure)"},
5547 {ELF::NT_LWPSINFO, "NT_LWPSINFO (lwpsinfo_t structure)"},
5548 {ELF::NT_WIN32PSTATUS, "NT_WIN32PSTATUS (win32_pstatus structure)"},
5549
5550 {ELF::NT_PPC_VMX, "NT_PPC_VMX (ppc Altivec registers)"},
5551 {ELF::NT_PPC_VSX, "NT_PPC_VSX (ppc VSX registers)"},
5552 {ELF::NT_PPC_TAR, "NT_PPC_TAR (ppc TAR register)"},
5553 {ELF::NT_PPC_PPR, "NT_PPC_PPR (ppc PPR register)"},
5554 {ELF::NT_PPC_DSCR, "NT_PPC_DSCR (ppc DSCR register)"},
5555 {ELF::NT_PPC_EBB, "NT_PPC_EBB (ppc EBB registers)"},
5556 {ELF::NT_PPC_PMU, "NT_PPC_PMU (ppc PMU registers)"},
5557 {ELF::NT_PPC_TM_CGPR, "NT_PPC_TM_CGPR (ppc checkpointed GPR registers)"},
5558 {ELF::NT_PPC_TM_CFPR,
5559 "NT_PPC_TM_CFPR (ppc checkpointed floating point registers)"},
5560 {ELF::NT_PPC_TM_CVMX,
5561 "NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)"},
5562 {ELF::NT_PPC_TM_CVSX, "NT_PPC_TM_CVSX (ppc checkpointed VSX registers)"},
5563 {ELF::NT_PPC_TM_SPR, "NT_PPC_TM_SPR (ppc TM special purpose registers)"},
5564 {ELF::NT_PPC_TM_CTAR, "NT_PPC_TM_CTAR (ppc checkpointed TAR register)"},
5565 {ELF::NT_PPC_TM_CPPR, "NT_PPC_TM_CPPR (ppc checkpointed PPR register)"},
5566 {ELF::NT_PPC_TM_CDSCR, "NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)"},
5567
5568 {ELF::NT_386_TLS, "NT_386_TLS (x86 TLS information)"},
5569 {ELF::NT_386_IOPERM, "NT_386_IOPERM (x86 I/O permissions)"},
5570 {ELF::NT_X86_XSTATE, "NT_X86_XSTATE (x86 XSAVE extended state)"},
5571
5572 {ELF::NT_S390_HIGH_GPRS, "NT_S390_HIGH_GPRS (s390 upper register halves)"},
5573 {ELF::NT_S390_TIMER,