Bug Summary

File:tools/llvm-readobj/ELFDumper.cpp
Warning:line 3152, column 25
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ELFDumper.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -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 -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn337490/build-llvm/tools/llvm-readobj -I /build/llvm-toolchain-snapshot-7~svn337490/tools/llvm-readobj -I /build/llvm-toolchain-snapshot-7~svn337490/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn337490/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/lib/gcc/x86_64-linux-gnu/8/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn337490/build-llvm/tools/llvm-readobj -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-07-20-043646-20380-1 -x c++ /build/llvm-toolchain-snapshot-7~svn337490/tools/llvm-readobj/ELFDumper.cpp -faddrsig
1//===- ELFDumper.cpp - ELF-specific dumper --------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// This file implements the ELF-specific dumper for llvm-readobj.
12///
13//===----------------------------------------------------------------------===//
14
15#include "ARMEHABIPrinter.h"
16#include "DwarfCFIEHPrinter.h"
17#include "Error.h"
18#include "ObjDumper.h"
19#include "StackMapPrinter.h"
20#include "llvm-readobj.h"
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/ADT/Optional.h"
24#include "llvm/ADT/PointerIntPair.h"
25#include "llvm/ADT/SmallString.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/STLExtras.h"
28#include "llvm/ADT/StringExtras.h"
29#include "llvm/ADT/StringRef.h"
30#include "llvm/ADT/Twine.h"
31#include "llvm/BinaryFormat/ELF.h"
32#include "llvm/Object/ELF.h"
33#include "llvm/Object/ELFObjectFile.h"
34#include "llvm/Object/ELFTypes.h"
35#include "llvm/Object/Error.h"
36#include "llvm/Object/ObjectFile.h"
37#include "llvm/Object/StackMapParser.h"
38#include "llvm/Support/AMDGPUMetadata.h"
39#include "llvm/Support/ARMAttributeParser.h"
40#include "llvm/Support/ARMBuildAttributes.h"
41#include "llvm/Support/Casting.h"
42#include "llvm/Support/Compiler.h"
43#include "llvm/Support/Endian.h"
44#include "llvm/Support/ErrorHandling.h"
45#include "llvm/Support/Format.h"
46#include "llvm/Support/FormattedStream.h"
47#include "llvm/Support/LEB128.h"
48#include "llvm/Support/MathExtras.h"
49#include "llvm/Support/MipsABIFlags.h"
50#include "llvm/Support/ScopedPrinter.h"
51#include "llvm/Support/raw_ostream.h"
52#include <algorithm>
53#include <cinttypes>
54#include <cstddef>
55#include <cstdint>
56#include <cstdlib>
57#include <iterator>
58#include <memory>
59#include <string>
60#include <system_error>
61#include <vector>
62
63using namespace llvm;
64using namespace llvm::object;
65using namespace ELF;
66
67#define LLVM_READOBJ_ENUM_CASE(ns, enum)case ns::enum: return "enum"; \
68 case ns::enum: return #enum;
69
70#define ENUM_ENT(enum, altName){ "enum", altName, ELF::enum } \
71 { #enum, altName, ELF::enum }
72
73#define ENUM_ENT_1(enum){ "enum", "enum", ELF::enum } \
74 { #enum, #enum, ELF::enum }
75
76#define LLVM_READOBJ_PHDR_ENUM(ns, enum)case ns::enum: return std::string("enum").substr(3); \
77 case ns::enum: \
78 return std::string(#enum).substr(3);
79
80#define TYPEDEF_ELF_TYPES(ELFT)using ELFO = ELFFile<ELFT>; using Elf_Addr = typename ELFT
::Addr; using Elf_Shdr = typename ELFT::Shdr; using Elf_Sym =
typename ELFT::Sym; using Elf_Dyn = typename ELFT::Dyn; using
Elf_Dyn_Range = typename ELFT::DynRange; using Elf_Rel = typename
ELFT::Rel; using Elf_Rela = typename ELFT::Rela; using Elf_Relr
= typename ELFT::Relr; using Elf_Rel_Range = typename ELFT::
RelRange; using Elf_Rela_Range = typename ELFT::RelaRange; using
Elf_Relr_Range = typename ELFT::RelrRange; using Elf_Phdr = typename
ELFT::Phdr; using Elf_Half = typename ELFT::Half; using Elf_Ehdr
= typename ELFT::Ehdr; using Elf_Word = typename ELFT::Word;
using Elf_Hash = typename ELFT::Hash; using Elf_GnuHash = typename
ELFT::GnuHash; using Elf_Note = typename ELFT::Note; using Elf_Sym_Range
= typename ELFT::SymRange; using Elf_Versym = typename ELFT::
Versym; using Elf_Verneed = typename ELFT::Verneed; using Elf_Vernaux
= typename ELFT::Vernaux; using Elf_Verdef = typename ELFT::
Verdef; using Elf_Verdaux = typename ELFT::Verdaux; using Elf_CGProfile
= typename ELFT::CGProfile; using uintX_t = typename ELFT::uint
;
\
81 using ELFO = ELFFile<ELFT>; \
82 using Elf_Addr = typename ELFT::Addr; \
83 using Elf_Shdr = typename ELFT::Shdr; \
84 using Elf_Sym = typename ELFT::Sym; \
85 using Elf_Dyn = typename ELFT::Dyn; \
86 using Elf_Dyn_Range = typename ELFT::DynRange; \
87 using Elf_Rel = typename ELFT::Rel; \
88 using Elf_Rela = typename ELFT::Rela; \
89 using Elf_Relr = typename ELFT::Relr; \
90 using Elf_Rel_Range = typename ELFT::RelRange; \
91 using Elf_Rela_Range = typename ELFT::RelaRange; \
92 using Elf_Relr_Range = typename ELFT::RelrRange; \
93 using Elf_Phdr = typename ELFT::Phdr; \
94 using Elf_Half = typename ELFT::Half; \
95 using Elf_Ehdr = typename ELFT::Ehdr; \
96 using Elf_Word = typename ELFT::Word; \
97 using Elf_Hash = typename ELFT::Hash; \
98 using Elf_GnuHash = typename ELFT::GnuHash; \
99 using Elf_Note = typename ELFT::Note; \
100 using Elf_Sym_Range = typename ELFT::SymRange; \
101 using Elf_Versym = typename ELFT::Versym; \
102 using Elf_Verneed = typename ELFT::Verneed; \
103 using Elf_Vernaux = typename ELFT::Vernaux; \
104 using Elf_Verdef = typename ELFT::Verdef; \
105 using Elf_Verdaux = typename ELFT::Verdaux; \
106 using Elf_CGProfile = typename ELFT::CGProfile; \
107 using uintX_t = typename ELFT::uint;
108
109namespace {
110
111template <class ELFT> class DumpStyle;
112
113/// Represents a contiguous uniform range in the file. We cannot just create a
114/// range directly because when creating one of these from the .dynamic table
115/// the size, entity size and virtual address are different entries in arbitrary
116/// order (DT_REL, DT_RELSZ, DT_RELENT for example).
117struct DynRegionInfo {
118 DynRegionInfo() = default;
119 DynRegionInfo(const void *A, uint64_t S, uint64_t ES)
120 : Addr(A), Size(S), EntSize(ES) {}
121
122 /// Address in current address space.
123 const void *Addr = nullptr;
124 /// Size in bytes of the region.
125 uint64_t Size = 0;
126 /// Size of each entity in the region.
127 uint64_t EntSize = 0;
128
129 template <typename Type> ArrayRef<Type> getAsArrayRef() const {
130 const Type *Start = reinterpret_cast<const Type *>(Addr);
131 if (!Start)
132 return {Start, Start};
133 if (EntSize != sizeof(Type) || Size % EntSize)
134 reportError("Invalid entity size");
135 return {Start, Start + (Size / EntSize)};
136 }
137};
138
139template<typename ELFT>
140class ELFDumper : public ObjDumper {
141public:
142 ELFDumper(const ELFFile<ELFT> *Obj, ScopedPrinter &Writer);
143
144 void printFileHeaders() override;
145 void printSections() override;
146 void printRelocations() override;
147 void printDynamicRelocations() override;
148 void printSymbols() override;
149 void printDynamicSymbols() override;
150 void printUnwindInfo() override;
151
152 void printDynamicTable() override;
153 void printNeededLibraries() override;
154 void printProgramHeaders() override;
155 void printSectionAsHex(StringRef StringName) override;
156 void printHashTable() override;
157 void printGnuHashTable() override;
158 void printLoadName() override;
159 void printVersionInfo() override;
160 void printGroupSections() override;
161
162 void printAttributes() override;
163 void printMipsPLTGOT() override;
164 void printMipsABIFlags() override;
165 void printMipsReginfo() override;
166 void printMipsOptions() override;
167
168 void printStackMap() const override;
169
170 void printHashHistogram() override;
171
172 void printCGProfile() override;
173 void printAddrsig() override;
174
175 void printNotes() override;
176
177 void printELFLinkerOptions() override;
178
179private:
180 std::unique_ptr<DumpStyle<ELFT>> ELFDumperStyle;
181
182 TYPEDEF_ELF_TYPES(ELFT)using ELFO = ELFFile<ELFT>; using Elf_Addr = typename ELFT
::Addr; using Elf_Shdr = typename ELFT::Shdr; using Elf_Sym =
typename ELFT::Sym; using Elf_Dyn = typename ELFT::Dyn; using
Elf_Dyn_Range = typename ELFT::DynRange; using Elf_Rel = typename
ELFT::Rel; using Elf_Rela = typename ELFT::Rela; using Elf_Relr
= typename ELFT::Relr; using Elf_Rel_Range = typename ELFT::
RelRange; using Elf_Rela_Range = typename ELFT::RelaRange; using
Elf_Relr_Range = typename ELFT::RelrRange; using Elf_Phdr = typename
ELFT::Phdr; using Elf_Half = typename ELFT::Half; using Elf_Ehdr
= typename ELFT::Ehdr; using Elf_Word = typename ELFT::Word;
using Elf_Hash = typename ELFT::Hash; using Elf_GnuHash = typename
ELFT::GnuHash; using Elf_Note = typename ELFT::Note; using Elf_Sym_Range
= typename ELFT::SymRange; using Elf_Versym = typename ELFT::
Versym; using Elf_Verneed = typename ELFT::Verneed; using Elf_Vernaux
= typename ELFT::Vernaux; using Elf_Verdef = typename ELFT::
Verdef; using Elf_Verdaux = typename ELFT::Verdaux; using Elf_CGProfile
= typename ELFT::CGProfile; using uintX_t = typename ELFT::uint
;
183
184 DynRegionInfo checkDRI(DynRegionInfo DRI) {
185 if (DRI.Addr < Obj->base() ||
186 (const uint8_t *)DRI.Addr + DRI.Size > Obj->base() + Obj->getBufSize())
187 error(llvm::object::object_error::parse_failed);
188 return DRI;
189 }
190
191 DynRegionInfo createDRIFrom(const Elf_Phdr *P, uintX_t EntSize) {
192 return checkDRI({Obj->base() + P->p_offset, P->p_filesz, EntSize});
193 }
194
195 DynRegionInfo createDRIFrom(const Elf_Shdr *S) {
196 return checkDRI({Obj->base() + S->sh_offset, S->sh_size, S->sh_entsize});
197 }
198
199 void parseDynamicTable(ArrayRef<const Elf_Phdr *> LoadSegments);
200
201 void printValue(uint64_t Type, uint64_t Value);
202
203 StringRef getDynamicString(uint64_t Offset) const;
204 StringRef getSymbolVersion(StringRef StrTab, const Elf_Sym *symb,
205 bool &IsDefault) const;
206 void LoadVersionMap() const;
207 void LoadVersionNeeds(const Elf_Shdr *ec) const;
208 void LoadVersionDefs(const Elf_Shdr *sec) const;
209
210 const ELFO *Obj;
211 DynRegionInfo DynRelRegion;
212 DynRegionInfo DynRelaRegion;
213 DynRegionInfo DynRelrRegion;
214 DynRegionInfo DynPLTRelRegion;
215 DynRegionInfo DynSymRegion;
216 DynRegionInfo DynamicTable;
217 StringRef DynamicStringTable;
218 StringRef SOName;
219 const Elf_Hash *HashTable = nullptr;
220 const Elf_GnuHash *GnuHashTable = nullptr;
221 const Elf_Shdr *DotSymtabSec = nullptr;
222 const Elf_Shdr *DotCGProfileSec = nullptr;
223 const Elf_Shdr *DotAddrsigSec = nullptr;
224 StringRef DynSymtabName;
225 ArrayRef<Elf_Word> ShndxTable;
226
227 const Elf_Shdr *dot_gnu_version_sec = nullptr; // .gnu.version
228 const Elf_Shdr *dot_gnu_version_r_sec = nullptr; // .gnu.version_r
229 const Elf_Shdr *dot_gnu_version_d_sec = nullptr; // .gnu.version_d
230
231 // Records for each version index the corresponding Verdef or Vernaux entry.
232 // This is filled the first time LoadVersionMap() is called.
233 class VersionMapEntry : public PointerIntPair<const void *, 1> {
234 public:
235 // If the integer is 0, this is an Elf_Verdef*.
236 // If the integer is 1, this is an Elf_Vernaux*.
237 VersionMapEntry() : PointerIntPair<const void *, 1>(nullptr, 0) {}
238 VersionMapEntry(const Elf_Verdef *verdef)
239 : PointerIntPair<const void *, 1>(verdef, 0) {}
240 VersionMapEntry(const Elf_Vernaux *vernaux)
241 : PointerIntPair<const void *, 1>(vernaux, 1) {}
242
243 bool isNull() const { return getPointer() == nullptr; }
244 bool isVerdef() const { return !isNull() && getInt() == 0; }
245 bool isVernaux() const { return !isNull() && getInt() == 1; }
246 const Elf_Verdef *getVerdef() const {
247 return isVerdef() ? (const Elf_Verdef *)getPointer() : nullptr;
248 }
249 const Elf_Vernaux *getVernaux() const {
250 return isVernaux() ? (const Elf_Vernaux *)getPointer() : nullptr;
251 }
252 };
253 mutable SmallVector<VersionMapEntry, 16> VersionMap;
254
255public:
256 Elf_Dyn_Range dynamic_table() const {
257 return DynamicTable.getAsArrayRef<Elf_Dyn>();
258 }
259
260 Elf_Sym_Range dynamic_symbols() const {
261 return DynSymRegion.getAsArrayRef<Elf_Sym>();
262 }
263
264 Elf_Rel_Range dyn_rels() const;
265 Elf_Rela_Range dyn_relas() const;
266 Elf_Relr_Range dyn_relrs() const;
267 std::string getFullSymbolName(const Elf_Sym *Symbol, StringRef StrTable,
268 bool IsDynamic) const;
269 void getSectionNameIndex(const Elf_Sym *Symbol, const Elf_Sym *FirstSym,
270 StringRef &SectionName,
271 unsigned &SectionIndex) const;
272 StringRef getStaticSymbolName(uint32_t Index) const;
273
274 void printSymbolsHelper(bool IsDynamic) const;
275 const Elf_Shdr *getDotSymtabSec() const { return DotSymtabSec; }
276 const Elf_Shdr *getDotCGProfileSec() const { return DotCGProfileSec; }
277 const Elf_Shdr *getDotAddrsigSec() const { return DotAddrsigSec; }
278 ArrayRef<Elf_Word> getShndxTable() const { return ShndxTable; }
279 StringRef getDynamicStringTable() const { return DynamicStringTable; }
280 const DynRegionInfo &getDynRelRegion() const { return DynRelRegion; }
281 const DynRegionInfo &getDynRelaRegion() const { return DynRelaRegion; }
282 const DynRegionInfo &getDynRelrRegion() const { return DynRelrRegion; }
283 const DynRegionInfo &getDynPLTRelRegion() const { return DynPLTRelRegion; }
284 const Elf_Hash *getHashTable() const { return HashTable; }
285 const Elf_GnuHash *getGnuHashTable() const { return GnuHashTable; }
286};
287
288template <class ELFT>
289void ELFDumper<ELFT>::printSectionAsHex(StringRef SectionName) {
290 char *StrPtr;
291 long SectionIndex = strtol(SectionName.data(), &StrPtr, 10);
292 const Elf_Shdr *Sec;
293 if (*StrPtr)
294 Sec = unwrapOrError(Obj->getSection(SectionName));
295 else
296 Sec = unwrapOrError(Obj->getSection((unsigned int)SectionIndex));
297
298 StringRef SecName = unwrapOrError(Obj->getSectionName(Sec));
299 const uint8_t *SecContent =
300 reinterpret_cast<const uint8_t *>(Obj->base() + Sec->sh_offset);
301
302 SectionHexDump(SecName, SecContent, Sec->sh_size);
303}
304
305template <class ELFT>
306void ELFDumper<ELFT>::printSymbolsHelper(bool IsDynamic) const {
307 StringRef StrTable, SymtabName;
308 size_t Entries = 0;
309 Elf_Sym_Range Syms(nullptr, nullptr);
310 if (IsDynamic) {
311 StrTable = DynamicStringTable;
312 Syms = dynamic_symbols();
313 SymtabName = DynSymtabName;
314 if (DynSymRegion.Addr)
315 Entries = DynSymRegion.Size / DynSymRegion.EntSize;
316 } else {
317 if (!DotSymtabSec)
318 return;
319 StrTable = unwrapOrError(Obj->getStringTableForSymtab(*DotSymtabSec));
320 Syms = unwrapOrError(Obj->symbols(DotSymtabSec));
321 SymtabName = unwrapOrError(Obj->getSectionName(DotSymtabSec));
322 Entries = DotSymtabSec->getEntityCount();
323 }
324 if (Syms.begin() == Syms.end())
325 return;
326 ELFDumperStyle->printSymtabMessage(Obj, SymtabName, Entries);
327 for (const auto &Sym : Syms)
328 ELFDumperStyle->printSymbol(Obj, &Sym, Syms.begin(), StrTable, IsDynamic);
329}
330
331template <class ELFT> class MipsGOTParser;
332
333template <typename ELFT> class DumpStyle {
334public:
335 using Elf_Shdr = typename ELFT::Shdr;
336 using Elf_Sym = typename ELFT::Sym;
337
338 DumpStyle(ELFDumper<ELFT> *Dumper) : Dumper(Dumper) {}
339 virtual ~DumpStyle() = default;
340
341 virtual void printFileHeaders(const ELFFile<ELFT> *Obj) = 0;
342 virtual void printGroupSections(const ELFFile<ELFT> *Obj) = 0;
343 virtual void printRelocations(const ELFFile<ELFT> *Obj) = 0;
344 virtual void printSections(const ELFFile<ELFT> *Obj) = 0;
345 virtual void printSymbols(const ELFFile<ELFT> *Obj) = 0;
346 virtual void printDynamicSymbols(const ELFFile<ELFT> *Obj) = 0;
347 virtual void printDynamicRelocations(const ELFFile<ELFT> *Obj) = 0;
348 virtual void printSymtabMessage(const ELFFile<ELFT> *obj, StringRef Name,
349 size_t Offset) {}
350 virtual void printSymbol(const ELFFile<ELFT> *Obj, const Elf_Sym *Symbol,
351 const Elf_Sym *FirstSym, StringRef StrTable,
352 bool IsDynamic) = 0;
353 virtual void printProgramHeaders(const ELFFile<ELFT> *Obj) = 0;
354 virtual void printHashHistogram(const ELFFile<ELFT> *Obj) = 0;
355 virtual void printCGProfile(const ELFFile<ELFT> *Obj) = 0;
356 virtual void printAddrsig(const ELFFile<ELFT> *Obj) = 0;
357 virtual void printNotes(const ELFFile<ELFT> *Obj) = 0;
358 virtual void printELFLinkerOptions(const ELFFile<ELFT> *Obj) = 0;
359 virtual void printMipsGOT(const MipsGOTParser<ELFT> &Parser) = 0;
360 virtual void printMipsPLT(const MipsGOTParser<ELFT> &Parser) = 0;
361 const ELFDumper<ELFT> *dumper() const { return Dumper; }
362
363private:
364 const ELFDumper<ELFT> *Dumper;
365};
366
367template <typename ELFT> class GNUStyle : public DumpStyle<ELFT> {
368 formatted_raw_ostream OS;
369
370public:
371 TYPEDEF_ELF_TYPES(ELFT)using ELFO = ELFFile<ELFT>; using Elf_Addr = typename ELFT
::Addr; using Elf_Shdr = typename ELFT::Shdr; using Elf_Sym =
typename ELFT::Sym; using Elf_Dyn = typename ELFT::Dyn; using
Elf_Dyn_Range = typename ELFT::DynRange; using Elf_Rel = typename
ELFT::Rel; using Elf_Rela = typename ELFT::Rela; using Elf_Relr
= typename ELFT::Relr; using Elf_Rel_Range = typename ELFT::
RelRange; using Elf_Rela_Range = typename ELFT::RelaRange; using
Elf_Relr_Range = typename ELFT::RelrRange; using Elf_Phdr = typename
ELFT::Phdr; using Elf_Half = typename ELFT::Half; using Elf_Ehdr
= typename ELFT::Ehdr; using Elf_Word = typename ELFT::Word;
using Elf_Hash = typename ELFT::Hash; using Elf_GnuHash = typename
ELFT::GnuHash; using Elf_Note = typename ELFT::Note; using Elf_Sym_Range
= typename ELFT::SymRange; using Elf_Versym = typename ELFT::
Versym; using Elf_Verneed = typename ELFT::Verneed; using Elf_Vernaux
= typename ELFT::Vernaux; using Elf_Verdef = typename ELFT::
Verdef; using Elf_Verdaux = typename ELFT::Verdaux; using Elf_CGProfile
= typename ELFT::CGProfile; using uintX_t = typename ELFT::uint
;
372
373 GNUStyle(ScopedPrinter &W, ELFDumper<ELFT> *Dumper)
374 : DumpStyle<ELFT>(Dumper), OS(W.getOStream()) {}
375
376 void printFileHeaders(const ELFO *Obj) override;
377 void printGroupSections(const ELFFile<ELFT> *Obj) override;
378 void printRelocations(const ELFO *Obj) override;
379 void printSections(const ELFO *Obj) override;
380 void printSymbols(const ELFO *Obj) override;
381 void printDynamicSymbols(const ELFO *Obj) override;
382 void printDynamicRelocations(const ELFO *Obj) override;
383 void printSymtabMessage(const ELFO *Obj, StringRef Name,
384 size_t Offset) override;
385 void printProgramHeaders(const ELFO *Obj) override;
386 void printHashHistogram(const ELFFile<ELFT> *Obj) override;
387 void printCGProfile(const ELFFile<ELFT> *Obj) override;
388 void printAddrsig(const ELFFile<ELFT> *Obj) override;
389 void printNotes(const ELFFile<ELFT> *Obj) override;
390 void printELFLinkerOptions(const ELFFile<ELFT> *Obj) override;
391 void printMipsGOT(const MipsGOTParser<ELFT> &Parser) override;
392 void printMipsPLT(const MipsGOTParser<ELFT> &Parser) override;
393
394private:
395 struct Field {
396 StringRef Str;
397 unsigned Column;
398
399 Field(StringRef S, unsigned Col) : Str(S), Column(Col) {}
400 Field(unsigned Col) : Str(""), Column(Col) {}
401 };
402
403 template <typename T, typename TEnum>
404 std::string printEnum(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues) {
405 for (const auto &EnumItem : EnumValues)
406 if (EnumItem.Value == Value)
407 return EnumItem.AltName;
408 return to_hexString(Value, false);
409 }
410
411 formatted_raw_ostream &printField(struct Field F) {
412 if (F.Column != 0)
413 OS.PadToColumn(F.Column);
414 OS << F.Str;
415 OS.flush();
416 return OS;
417 }
418 void printHashedSymbol(const ELFO *Obj, const Elf_Sym *FirstSym, uint32_t Sym,
419 StringRef StrTable, uint32_t Bucket);
420 void printRelocHeader(unsigned SType);
421 void printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab,
422 const Elf_Rela &R, bool IsRela);
423 void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First,
424 StringRef StrTable, bool IsDynamic) override;
425 std::string getSymbolSectionNdx(const ELFO *Obj, const Elf_Sym *Symbol,
426 const Elf_Sym *FirstSym);
427 void printDynamicRelocation(const ELFO *Obj, Elf_Rela R, bool IsRela);
428 bool checkTLSSections(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
429 bool checkoffsets(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
430 bool checkVMA(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
431 bool checkPTDynamic(const Elf_Phdr &Phdr, const Elf_Shdr &Sec);
432};
433
434template <typename ELFT> class LLVMStyle : public DumpStyle<ELFT> {
435public:
436 TYPEDEF_ELF_TYPES(ELFT)using ELFO = ELFFile<ELFT>; using Elf_Addr = typename ELFT
::Addr; using Elf_Shdr = typename ELFT::Shdr; using Elf_Sym =
typename ELFT::Sym; using Elf_Dyn = typename ELFT::Dyn; using
Elf_Dyn_Range = typename ELFT::DynRange; using Elf_Rel = typename
ELFT::Rel; using Elf_Rela = typename ELFT::Rela; using Elf_Relr
= typename ELFT::Relr; using Elf_Rel_Range = typename ELFT::
RelRange; using Elf_Rela_Range = typename ELFT::RelaRange; using
Elf_Relr_Range = typename ELFT::RelrRange; using Elf_Phdr = typename
ELFT::Phdr; using Elf_Half = typename ELFT::Half; using Elf_Ehdr
= typename ELFT::Ehdr; using Elf_Word = typename ELFT::Word;
using Elf_Hash = typename ELFT::Hash; using Elf_GnuHash = typename
ELFT::GnuHash; using Elf_Note = typename ELFT::Note; using Elf_Sym_Range
= typename ELFT::SymRange; using Elf_Versym = typename ELFT::
Versym; using Elf_Verneed = typename ELFT::Verneed; using Elf_Vernaux
= typename ELFT::Vernaux; using Elf_Verdef = typename ELFT::
Verdef; using Elf_Verdaux = typename ELFT::Verdaux; using Elf_CGProfile
= typename ELFT::CGProfile; using uintX_t = typename ELFT::uint
;
437
438 LLVMStyle(ScopedPrinter &W, ELFDumper<ELFT> *Dumper)
439 : DumpStyle<ELFT>(Dumper), W(W) {}
440
441 void printFileHeaders(const ELFO *Obj) override;
442 void printGroupSections(const ELFFile<ELFT> *Obj) override;
443 void printRelocations(const ELFO *Obj) override;
444 void printRelocations(const Elf_Shdr *Sec, const ELFO *Obj);
445 void printSections(const ELFO *Obj) override;
446 void printSymbols(const ELFO *Obj) override;
447 void printDynamicSymbols(const ELFO *Obj) override;
448 void printDynamicRelocations(const ELFO *Obj) override;
449 void printProgramHeaders(const ELFO *Obj) override;
450 void printHashHistogram(const ELFFile<ELFT> *Obj) override;
451 void printCGProfile(const ELFFile<ELFT> *Obj) override;
452 void printAddrsig(const ELFFile<ELFT> *Obj) override;
453 void printNotes(const ELFFile<ELFT> *Obj) override;
454 void printELFLinkerOptions(const ELFFile<ELFT> *Obj) override;
455 void printMipsGOT(const MipsGOTParser<ELFT> &Parser) override;
456 void printMipsPLT(const MipsGOTParser<ELFT> &Parser) override;
457
458private:
459 void printRelocation(const ELFO *Obj, Elf_Rela Rel, const Elf_Shdr *SymTab);
460 void printDynamicRelocation(const ELFO *Obj, Elf_Rela Rel);
461 void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First,
462 StringRef StrTable, bool IsDynamic) override;
463
464 ScopedPrinter &W;
465};
466
467} // end anonymous namespace
468
469namespace llvm {
470
471template <class ELFT>
472static std::error_code createELFDumper(const ELFFile<ELFT> *Obj,
473 ScopedPrinter &Writer,
474 std::unique_ptr<ObjDumper> &Result) {
475 Result.reset(new ELFDumper<ELFT>(Obj, Writer));
476 return readobj_error::success;
477}
478
479std::error_code createELFDumper(const object::ObjectFile *Obj,
480 ScopedPrinter &Writer,
481 std::unique_ptr<ObjDumper> &Result) {
482 // Little-endian 32-bit
483 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
484 return createELFDumper(ELFObj->getELFFile(), Writer, Result);
485
486 // Big-endian 32-bit
487 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
488 return createELFDumper(ELFObj->getELFFile(), Writer, Result);
489
490 // Little-endian 64-bit
491 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
492 return createELFDumper(ELFObj->getELFFile(), Writer, Result);
493
494 // Big-endian 64-bit
495 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
496 return createELFDumper(ELFObj->getELFFile(), Writer, Result);
497
498 return readobj_error::unsupported_obj_file_format;
499}
500
501} // end namespace llvm
502
503// Iterate through the versions needed section, and place each Elf_Vernaux
504// in the VersionMap according to its index.
505template <class ELFT>
506void ELFDumper<ELFT>::LoadVersionNeeds(const Elf_Shdr *sec) const {
507 unsigned vn_size = sec->sh_size; // Size of section in bytes
508 unsigned vn_count = sec->sh_info; // Number of Verneed entries
509 const char *sec_start = (const char *)Obj->base() + sec->sh_offset;
510 const char *sec_end = sec_start + vn_size;
511 // The first Verneed entry is at the start of the section.
512 const char *p = sec_start;
513 for (unsigned i = 0; i < vn_count; i++) {
514 if (p + sizeof(Elf_Verneed) > sec_end)
515 report_fatal_error("Section ended unexpectedly while scanning "
516 "version needed records.");
517 const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
518 if (vn->vn_version != ELF::VER_NEED_CURRENT)
519 report_fatal_error("Unexpected verneed version");
520 // Iterate through the Vernaux entries
521 const char *paux = p + vn->vn_aux;
522 for (unsigned j = 0; j < vn->vn_cnt; j++) {
523 if (paux + sizeof(Elf_Vernaux) > sec_end)
524 report_fatal_error("Section ended unexpected while scanning auxiliary "
525 "version needed records.");
526 const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
527 size_t index = vna->vna_other & ELF::VERSYM_VERSION;
528 if (index >= VersionMap.size())
529 VersionMap.resize(index + 1);
530 VersionMap[index] = VersionMapEntry(vna);
531 paux += vna->vna_next;
532 }
533 p += vn->vn_next;
534 }
535}
536
537// Iterate through the version definitions, and place each Elf_Verdef
538// in the VersionMap according to its index.
539template <class ELFT>
540void ELFDumper<ELFT>::LoadVersionDefs(const Elf_Shdr *sec) const {
541 unsigned vd_size = sec->sh_size; // Size of section in bytes
542 unsigned vd_count = sec->sh_info; // Number of Verdef entries
543 const char *sec_start = (const char *)Obj->base() + sec->sh_offset;
544 const char *sec_end = sec_start + vd_size;
545 // The first Verdef entry is at the start of the section.
546 const char *p = sec_start;
547 for (unsigned i = 0; i < vd_count; i++) {
548 if (p + sizeof(Elf_Verdef) > sec_end)
549 report_fatal_error("Section ended unexpectedly while scanning "
550 "version definitions.");
551 const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p);
552 if (vd->vd_version != ELF::VER_DEF_CURRENT)
553 report_fatal_error("Unexpected verdef version");
554 size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
555 if (index >= VersionMap.size())
556 VersionMap.resize(index + 1);
557 VersionMap[index] = VersionMapEntry(vd);
558 p += vd->vd_next;
559 }
560}
561
562template <class ELFT> void ELFDumper<ELFT>::LoadVersionMap() const {
563 // If there is no dynamic symtab or version table, there is nothing to do.
564 if (!DynSymRegion.Addr || !dot_gnu_version_sec)
565 return;
566
567 // Has the VersionMap already been loaded?
568 if (VersionMap.size() > 0)
569 return;
570
571 // The first two version indexes are reserved.
572 // Index 0 is LOCAL, index 1 is GLOBAL.
573 VersionMap.push_back(VersionMapEntry());
574 VersionMap.push_back(VersionMapEntry());
575
576 if (dot_gnu_version_d_sec)
577 LoadVersionDefs(dot_gnu_version_d_sec);
578
579 if (dot_gnu_version_r_sec)
580 LoadVersionNeeds(dot_gnu_version_r_sec);
581}
582
583template <typename ELFO, class ELFT>
584static void printVersionSymbolSection(ELFDumper<ELFT> *Dumper, const ELFO *Obj,
585 const typename ELFO::Elf_Shdr *Sec,
586 ScopedPrinter &W) {
587 DictScope SS(W, "Version symbols");
588 if (!Sec)
589 return;
590 StringRef Name = unwrapOrError(Obj->getSectionName(Sec));
591 W.printNumber("Section Name", Name, Sec->sh_name);
592 W.printHex("Address", Sec->sh_addr);
593 W.printHex("Offset", Sec->sh_offset);
594 W.printNumber("Link", Sec->sh_link);
595
596 const uint8_t *P = (const uint8_t *)Obj->base() + Sec->sh_offset;
597 StringRef StrTable = Dumper->getDynamicStringTable();
598
599 // Same number of entries in the dynamic symbol table (DT_SYMTAB).
600 ListScope Syms(W, "Symbols");
601 for (const typename ELFO::Elf_Sym &Sym : Dumper->dynamic_symbols()) {
602 DictScope S(W, "Symbol");
603 std::string FullSymbolName =
604 Dumper->getFullSymbolName(&Sym, StrTable, true /* IsDynamic */);
605 W.printNumber("Version", *P);
606 W.printString("Name", FullSymbolName);
607 P += sizeof(typename ELFO::Elf_Half);
608 }
609}
610
611static const EnumEntry<unsigned> SymVersionFlags[] = {
612 {"Base", "BASE", VER_FLG_BASE},
613 {"Weak", "WEAK", VER_FLG_WEAK},
614 {"Info", "INFO", VER_FLG_INFO}};
615
616template <typename ELFO, class ELFT>
617static void printVersionDefinitionSection(ELFDumper<ELFT> *Dumper,
618 const ELFO *Obj,
619 const typename ELFO::Elf_Shdr *Sec,
620 ScopedPrinter &W) {
621 using VerDef = typename ELFO::Elf_Verdef;
622 using VerdAux = typename ELFO::Elf_Verdaux;
623
624 DictScope SD(W, "SHT_GNU_verdef");
625 if (!Sec)
626 return;
627
628 // The number of entries in the section SHT_GNU_verdef
629 // is determined by DT_VERDEFNUM tag.
630 unsigned VerDefsNum = 0;
631 for (const typename ELFO::Elf_Dyn &Dyn : Dumper->dynamic_table()) {
632 if (Dyn.d_tag == DT_VERDEFNUM)
633 VerDefsNum = Dyn.d_un.d_val;
634 }
635 const uint8_t *SecStartAddress =
636 (const uint8_t *)Obj->base() + Sec->sh_offset;
637 const uint8_t *SecEndAddress = SecStartAddress + Sec->sh_size;
638 const uint8_t *P = SecStartAddress;
639 const typename ELFO::Elf_Shdr *StrTab =
640 unwrapOrError(Obj->getSection(Sec->sh_link));
641
642 while (VerDefsNum--) {
643 if (P + sizeof(VerDef) > SecEndAddress)
644 report_fatal_error("invalid offset in the section");
645
646 auto *VD = reinterpret_cast<const VerDef *>(P);
647 DictScope Def(W, "Definition");
648 W.printNumber("Version", VD->vd_version);
649 W.printEnum("Flags", VD->vd_flags, makeArrayRef(SymVersionFlags));
650 W.printNumber("Index", VD->vd_ndx);
651 W.printNumber("Hash", VD->vd_hash);
652 W.printString("Name",
653 StringRef((const char *)(Obj->base() + StrTab->sh_offset +
654 VD->getAux()->vda_name)));
655 if (!VD->vd_cnt)
656 report_fatal_error("at least one definition string must exist");
657 if (VD->vd_cnt > 2)
658 report_fatal_error("more than one predecessor is not expected");
659
660 if (VD->vd_cnt == 2) {
661 const uint8_t *PAux = P + VD->vd_aux + VD->getAux()->vda_next;
662 const VerdAux *Aux = reinterpret_cast<const VerdAux *>(PAux);
663 W.printString("Predecessor",
664 StringRef((const char *)(Obj->base() + StrTab->sh_offset +
665 Aux->vda_name)));
666 }
667
668 P += VD->vd_next;
669 }
670}
671
672template <typename ELFO, class ELFT>
673static void printVersionDependencySection(ELFDumper<ELFT> *Dumper,
674 const ELFO *Obj,
675 const typename ELFO::Elf_Shdr *Sec,
676 ScopedPrinter &W) {
677 using VerNeed = typename ELFO::Elf_Verneed;
678 using VernAux = typename ELFO::Elf_Vernaux;
679
680 DictScope SD(W, "SHT_GNU_verneed");
681 if (!Sec)
682 return;
683
684 unsigned VerNeedNum = 0;
685 for (const typename ELFO::Elf_Dyn &Dyn : Dumper->dynamic_table())
686 if (Dyn.d_tag == DT_VERNEEDNUM)
687 VerNeedNum = Dyn.d_un.d_val;
688
689 const uint8_t *SecData = (const uint8_t *)Obj->base() + Sec->sh_offset;
690 const typename ELFO::Elf_Shdr *StrTab =
691 unwrapOrError(Obj->getSection(Sec->sh_link));
692
693 const uint8_t *P = SecData;
694 for (unsigned I = 0; I < VerNeedNum; ++I) {
695 const VerNeed *Need = reinterpret_cast<const VerNeed *>(P);
696 DictScope Entry(W, "Dependency");
697 W.printNumber("Version", Need->vn_version);
698 W.printNumber("Count", Need->vn_cnt);
699 W.printString("FileName",
700 StringRef((const char *)(Obj->base() + StrTab->sh_offset +
701 Need->vn_file)));
702
703 const uint8_t *PAux = P + Need->vn_aux;
704 for (unsigned J = 0; J < Need->vn_cnt; ++J) {
705 const VernAux *Aux = reinterpret_cast<const VernAux *>(PAux);
706 DictScope Entry(W, "Entry");
707 W.printNumber("Hash", Aux->vna_hash);
708 W.printEnum("Flags", Aux->vna_flags, makeArrayRef(SymVersionFlags));
709 W.printNumber("Index", Aux->vna_other);
710 W.printString("Name",
711 StringRef((const char *)(Obj->base() + StrTab->sh_offset +
712 Aux->vna_name)));
713 PAux += Aux->vna_next;
714 }
715 P += Need->vn_next;
716 }
717}
718
719template <typename ELFT> void ELFDumper<ELFT>::printVersionInfo() {
720 // Dump version symbol section.
721 printVersionSymbolSection(this, Obj, dot_gnu_version_sec, W);
722
723 // Dump version definition section.
724 printVersionDefinitionSection(this, Obj, dot_gnu_version_d_sec, W);
725
726 // Dump version dependency section.
727 printVersionDependencySection(this, Obj, dot_gnu_version_r_sec, W);
728}
729
730template <typename ELFT>
731StringRef ELFDumper<ELFT>::getSymbolVersion(StringRef StrTab,
732 const Elf_Sym *symb,
733 bool &IsDefault) const {
734 // This is a dynamic symbol. Look in the GNU symbol version table.
735 if (!dot_gnu_version_sec) {
736 // No version table.
737 IsDefault = false;
738 return StringRef("");
739 }
740
741 // Determine the position in the symbol table of this entry.
742 size_t entry_index = (reinterpret_cast<uintptr_t>(symb) -
743 reinterpret_cast<uintptr_t>(DynSymRegion.Addr)) /
744 sizeof(Elf_Sym);
745
746 // Get the corresponding version index entry
747 const Elf_Versym *vs = unwrapOrError(
748 Obj->template getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index));
749 size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
750
751 // Special markers for unversioned symbols.
752 if (version_index == ELF::VER_NDX_LOCAL ||
753 version_index == ELF::VER_NDX_GLOBAL) {
754 IsDefault = false;
755 return StringRef("");
756 }
757
758 // Lookup this symbol in the version table
759 LoadVersionMap();
760 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
761 reportError("Invalid version entry");
762 const VersionMapEntry &entry = VersionMap[version_index];
763
764 // Get the version name string
765 size_t name_offset;
766 if (entry.isVerdef()) {
767 // The first Verdaux entry holds the name.
768 name_offset = entry.getVerdef()->getAux()->vda_name;
769 IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
770 } else {
771 name_offset = entry.getVernaux()->vna_name;
772 IsDefault = false;
773 }
774 if (name_offset >= StrTab.size())
775 reportError("Invalid string offset");
776 return StringRef(StrTab.data() + name_offset);
777}
778
779template <typename ELFT>
780StringRef ELFDumper<ELFT>::getStaticSymbolName(uint32_t Index) const {
781 StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*DotSymtabSec));
782 Elf_Sym_Range Syms = unwrapOrError(Obj->symbols(DotSymtabSec));
783 if (Index >= Syms.size())
784 reportError("Invalid symbol index");
785 const Elf_Sym *Sym = &Syms[Index];
786 return unwrapOrError(Sym->getName(StrTable));
787}
788
789template <typename ELFT>
790std::string ELFDumper<ELFT>::getFullSymbolName(const Elf_Sym *Symbol,
791 StringRef StrTable,
792 bool IsDynamic) const {
793 StringRef SymbolName = unwrapOrError(Symbol->getName(StrTable));
794 if (!IsDynamic)
795 return SymbolName;
796
797 std::string FullSymbolName(SymbolName);
798
799 bool IsDefault;
800 StringRef Version = getSymbolVersion(StrTable, &*Symbol, IsDefault);
801 FullSymbolName += (IsDefault ? "@@" : "@");
802 FullSymbolName += Version;
803 return FullSymbolName;
804}
805
806template <typename ELFT>
807void ELFDumper<ELFT>::getSectionNameIndex(const Elf_Sym *Symbol,
808 const Elf_Sym *FirstSym,
809 StringRef &SectionName,
810 unsigned &SectionIndex) const {
811 SectionIndex = Symbol->st_shndx;
812 if (Symbol->isUndefined())
813 SectionName = "Undefined";
814 else if (Symbol->isProcessorSpecific())
815 SectionName = "Processor Specific";
816 else if (Symbol->isOSSpecific())
817 SectionName = "Operating System Specific";
818 else if (Symbol->isAbsolute())
819 SectionName = "Absolute";
820 else if (Symbol->isCommon())
821 SectionName = "Common";
822 else if (Symbol->isReserved() && SectionIndex != SHN_XINDEX)
823 SectionName = "Reserved";
824 else {
825 if (SectionIndex == SHN_XINDEX)
826 SectionIndex = unwrapOrError(object::getExtendedSymbolTableIndex<ELFT>(
827 Symbol, FirstSym, ShndxTable));
828 const typename ELFT::Shdr *Sec =
829 unwrapOrError(Obj->getSection(SectionIndex));
830 SectionName = unwrapOrError(Obj->getSectionName(Sec));
831 }
832}
833
834template <class ELFO>
835static const typename ELFO::Elf_Shdr *
836findNotEmptySectionByAddress(const ELFO *Obj, uint64_t Addr) {
837 for (const auto &Shdr : unwrapOrError(Obj->sections()))
838 if (Shdr.sh_addr == Addr && Shdr.sh_size > 0)
839 return &Shdr;
840 return nullptr;
841}
842
843template <class ELFO>
844static const typename ELFO::Elf_Shdr *findSectionByName(const ELFO &Obj,
845 StringRef Name) {
846 for (const auto &Shdr : unwrapOrError(Obj.sections())) {
847 if (Name == unwrapOrError(Obj.getSectionName(&Shdr)))
848 return &Shdr;
849 }
850 return nullptr;
851}
852
853static const EnumEntry<unsigned> ElfClass[] = {
854 {"None", "none", ELF::ELFCLASSNONE},
855 {"32-bit", "ELF32", ELF::ELFCLASS32},
856 {"64-bit", "ELF64", ELF::ELFCLASS64},
857};
858
859static const EnumEntry<unsigned> ElfDataEncoding[] = {
860 {"None", "none", ELF::ELFDATANONE},
861 {"LittleEndian", "2's complement, little endian", ELF::ELFDATA2LSB},
862 {"BigEndian", "2's complement, big endian", ELF::ELFDATA2MSB},
863};
864
865static const EnumEntry<unsigned> ElfObjectFileType[] = {
866 {"None", "NONE (none)", ELF::ET_NONE},
867 {"Relocatable", "REL (Relocatable file)", ELF::ET_REL},
868 {"Executable", "EXEC (Executable file)", ELF::ET_EXEC},
869 {"SharedObject", "DYN (Shared object file)", ELF::ET_DYN},
870 {"Core", "CORE (Core file)", ELF::ET_CORE},
871};
872
873static const EnumEntry<unsigned> ElfOSABI[] = {
874 {"SystemV", "UNIX - System V", ELF::ELFOSABI_NONE},
875 {"HPUX", "UNIX - HP-UX", ELF::ELFOSABI_HPUX},
876 {"NetBSD", "UNIX - NetBSD", ELF::ELFOSABI_NETBSD},
877 {"GNU/Linux", "UNIX - GNU", ELF::ELFOSABI_LINUX},
878 {"GNU/Hurd", "GNU/Hurd", ELF::ELFOSABI_HURD},
879 {"Solaris", "UNIX - Solaris", ELF::ELFOSABI_SOLARIS},
880 {"AIX", "UNIX - AIX", ELF::ELFOSABI_AIX},
881 {"IRIX", "UNIX - IRIX", ELF::ELFOSABI_IRIX},
882 {"FreeBSD", "UNIX - FreeBSD", ELF::ELFOSABI_FREEBSD},
883 {"TRU64", "UNIX - TRU64", ELF::ELFOSABI_TRU64},
884 {"Modesto", "Novell - Modesto", ELF::ELFOSABI_MODESTO},
885 {"OpenBSD", "UNIX - OpenBSD", ELF::ELFOSABI_OPENBSD},
886 {"OpenVMS", "VMS - OpenVMS", ELF::ELFOSABI_OPENVMS},
887 {"NSK", "HP - Non-Stop Kernel", ELF::ELFOSABI_NSK},
888 {"AROS", "AROS", ELF::ELFOSABI_AROS},
889 {"FenixOS", "FenixOS", ELF::ELFOSABI_FENIXOS},
890 {"CloudABI", "CloudABI", ELF::ELFOSABI_CLOUDABI},
891 {"Standalone", "Standalone App", ELF::ELFOSABI_STANDALONE}
892};
893
894static const EnumEntry<unsigned> AMDGPUElfOSABI[] = {
895 {"AMDGPU_HSA", "AMDGPU - HSA", ELF::ELFOSABI_AMDGPU_HSA},
896 {"AMDGPU_PAL", "AMDGPU - PAL", ELF::ELFOSABI_AMDGPU_PAL},
897 {"AMDGPU_MESA3D", "AMDGPU - MESA3D", ELF::ELFOSABI_AMDGPU_MESA3D}
898};
899
900static const EnumEntry<unsigned> ARMElfOSABI[] = {
901 {"ARM", "ARM", ELF::ELFOSABI_ARM}
902};
903
904static const EnumEntry<unsigned> C6000ElfOSABI[] = {
905 {"C6000_ELFABI", "Bare-metal C6000", ELF::ELFOSABI_C6000_ELFABI},
906 {"C6000_LINUX", "Linux C6000", ELF::ELFOSABI_C6000_LINUX}
907};
908
909static const EnumEntry<unsigned> ElfMachineType[] = {
910 ENUM_ENT(EM_NONE, "None"){ "EM_NONE", "None", ELF::EM_NONE },
911 ENUM_ENT(EM_M32, "WE32100"){ "EM_M32", "WE32100", ELF::EM_M32 },
912 ENUM_ENT(EM_SPARC, "Sparc"){ "EM_SPARC", "Sparc", ELF::EM_SPARC },
913 ENUM_ENT(EM_386, "Intel 80386"){ "EM_386", "Intel 80386", ELF::EM_386 },
914 ENUM_ENT(EM_68K, "MC68000"){ "EM_68K", "MC68000", ELF::EM_68K },
915 ENUM_ENT(EM_88K, "MC88000"){ "EM_88K", "MC88000", ELF::EM_88K },
916 ENUM_ENT(EM_IAMCU, "EM_IAMCU"){ "EM_IAMCU", "EM_IAMCU", ELF::EM_IAMCU },
917 ENUM_ENT(EM_860, "Intel 80860"){ "EM_860", "Intel 80860", ELF::EM_860 },
918 ENUM_ENT(EM_MIPS, "MIPS R3000"){ "EM_MIPS", "MIPS R3000", ELF::EM_MIPS },
919 ENUM_ENT(EM_S370, "IBM System/370"){ "EM_S370", "IBM System/370", ELF::EM_S370 },
920 ENUM_ENT(EM_MIPS_RS3_LE, "MIPS R3000 little-endian"){ "EM_MIPS_RS3_LE", "MIPS R3000 little-endian", ELF::EM_MIPS_RS3_LE
}
,
921 ENUM_ENT(EM_PARISC, "HPPA"){ "EM_PARISC", "HPPA", ELF::EM_PARISC },
922 ENUM_ENT(EM_VPP500, "Fujitsu VPP500"){ "EM_VPP500", "Fujitsu VPP500", ELF::EM_VPP500 },
923 ENUM_ENT(EM_SPARC32PLUS, "Sparc v8+"){ "EM_SPARC32PLUS", "Sparc v8+", ELF::EM_SPARC32PLUS },
924 ENUM_ENT(EM_960, "Intel 80960"){ "EM_960", "Intel 80960", ELF::EM_960 },
925 ENUM_ENT(EM_PPC, "PowerPC"){ "EM_PPC", "PowerPC", ELF::EM_PPC },
926 ENUM_ENT(EM_PPC64, "PowerPC64"){ "EM_PPC64", "PowerPC64", ELF::EM_PPC64 },
927 ENUM_ENT(EM_S390, "IBM S/390"){ "EM_S390", "IBM S/390", ELF::EM_S390 },
928 ENUM_ENT(EM_SPU, "SPU"){ "EM_SPU", "SPU", ELF::EM_SPU },
929 ENUM_ENT(EM_V800, "NEC V800 series"){ "EM_V800", "NEC V800 series", ELF::EM_V800 },
930 ENUM_ENT(EM_FR20, "Fujistsu FR20"){ "EM_FR20", "Fujistsu FR20", ELF::EM_FR20 },
931 ENUM_ENT(EM_RH32, "TRW RH-32"){ "EM_RH32", "TRW RH-32", ELF::EM_RH32 },
932 ENUM_ENT(EM_RCE, "Motorola RCE"){ "EM_RCE", "Motorola RCE", ELF::EM_RCE },
933 ENUM_ENT(EM_ARM, "ARM"){ "EM_ARM", "ARM", ELF::EM_ARM },
934 ENUM_ENT(EM_ALPHA, "EM_ALPHA"){ "EM_ALPHA", "EM_ALPHA", ELF::EM_ALPHA },
935 ENUM_ENT(EM_SH, "Hitachi SH"){ "EM_SH", "Hitachi SH", ELF::EM_SH },
936 ENUM_ENT(EM_SPARCV9, "Sparc v9"){ "EM_SPARCV9", "Sparc v9", ELF::EM_SPARCV9 },
937 ENUM_ENT(EM_TRICORE, "Siemens Tricore"){ "EM_TRICORE", "Siemens Tricore", ELF::EM_TRICORE },
938 ENUM_ENT(EM_ARC, "ARC"){ "EM_ARC", "ARC", ELF::EM_ARC },
939 ENUM_ENT(EM_H8_300, "Hitachi H8/300"){ "EM_H8_300", "Hitachi H8/300", ELF::EM_H8_300 },
940 ENUM_ENT(EM_H8_300H, "Hitachi H8/300H"){ "EM_H8_300H", "Hitachi H8/300H", ELF::EM_H8_300H },
941 ENUM_ENT(EM_H8S, "Hitachi H8S"){ "EM_H8S", "Hitachi H8S", ELF::EM_H8S },
942 ENUM_ENT(EM_H8_500, "Hitachi H8/500"){ "EM_H8_500", "Hitachi H8/500", ELF::EM_H8_500 },
943 ENUM_ENT(EM_IA_64, "Intel IA-64"){ "EM_IA_64", "Intel IA-64", ELF::EM_IA_64 },
944 ENUM_ENT(EM_MIPS_X, "Stanford MIPS-X"){ "EM_MIPS_X", "Stanford MIPS-X", ELF::EM_MIPS_X },
945 ENUM_ENT(EM_COLDFIRE, "Motorola Coldfire"){ "EM_COLDFIRE", "Motorola Coldfire", ELF::EM_COLDFIRE },
946 ENUM_ENT(EM_68HC12, "Motorola MC68HC12 Microcontroller"){ "EM_68HC12", "Motorola MC68HC12 Microcontroller", ELF::EM_68HC12
}
,
947 ENUM_ENT(EM_MMA, "Fujitsu Multimedia Accelerator"){ "EM_MMA", "Fujitsu Multimedia Accelerator", ELF::EM_MMA },
948 ENUM_ENT(EM_PCP, "Siemens PCP"){ "EM_PCP", "Siemens PCP", ELF::EM_PCP },
949 ENUM_ENT(EM_NCPU, "Sony nCPU embedded RISC processor"){ "EM_NCPU", "Sony nCPU embedded RISC processor", ELF::EM_NCPU
}
,
950 ENUM_ENT(EM_NDR1, "Denso NDR1 microprocesspr"){ "EM_NDR1", "Denso NDR1 microprocesspr", ELF::EM_NDR1 },
951 ENUM_ENT(EM_STARCORE, "Motorola Star*Core processor"){ "EM_STARCORE", "Motorola Star*Core processor", ELF::EM_STARCORE
}
,
952 ENUM_ENT(EM_ME16, "Toyota ME16 processor"){ "EM_ME16", "Toyota ME16 processor", ELF::EM_ME16 },
953 ENUM_ENT(EM_ST100, "STMicroelectronics ST100 processor"){ "EM_ST100", "STMicroelectronics ST100 processor", ELF::EM_ST100
}
,
954 ENUM_ENT(EM_TINYJ, "Advanced Logic Corp. TinyJ embedded processor"){ "EM_TINYJ", "Advanced Logic Corp. TinyJ embedded processor"
, ELF::EM_TINYJ }
,
955 ENUM_ENT(EM_X86_64, "Advanced Micro Devices X86-64"){ "EM_X86_64", "Advanced Micro Devices X86-64", ELF::EM_X86_64
}
,
956 ENUM_ENT(EM_PDSP, "Sony DSP processor"){ "EM_PDSP", "Sony DSP processor", ELF::EM_PDSP },
957 ENUM_ENT(EM_PDP10, "Digital Equipment Corp. PDP-10"){ "EM_PDP10", "Digital Equipment Corp. PDP-10", ELF::EM_PDP10
}
,
958 ENUM_ENT(EM_PDP11, "Digital Equipment Corp. PDP-11"){ "EM_PDP11", "Digital Equipment Corp. PDP-11", ELF::EM_PDP11
}
,
959 ENUM_ENT(EM_FX66, "Siemens FX66 microcontroller"){ "EM_FX66", "Siemens FX66 microcontroller", ELF::EM_FX66 },
960 ENUM_ENT(EM_ST9PLUS, "STMicroelectronics ST9+ 8/16 bit microcontroller"){ "EM_ST9PLUS", "STMicroelectronics ST9+ 8/16 bit microcontroller"
, ELF::EM_ST9PLUS }
,
961 ENUM_ENT(EM_ST7, "STMicroelectronics ST7 8-bit microcontroller"){ "EM_ST7", "STMicroelectronics ST7 8-bit microcontroller", ELF
::EM_ST7 }
,
962 ENUM_ENT(EM_68HC16, "Motorola MC68HC16 Microcontroller"){ "EM_68HC16", "Motorola MC68HC16 Microcontroller", ELF::EM_68HC16
}
,
963 ENUM_ENT(EM_68HC11, "Motorola MC68HC11 Microcontroller"){ "EM_68HC11", "Motorola MC68HC11 Microcontroller", ELF::EM_68HC11
}
,
964 ENUM_ENT(EM_68HC08, "Motorola MC68HC08 Microcontroller"){ "EM_68HC08", "Motorola MC68HC08 Microcontroller", ELF::EM_68HC08
}
,
965 ENUM_ENT(EM_68HC05, "Motorola MC68HC05 Microcontroller"){ "EM_68HC05", "Motorola MC68HC05 Microcontroller", ELF::EM_68HC05
}
,
966 ENUM_ENT(EM_SVX, "Silicon Graphics SVx"){ "EM_SVX", "Silicon Graphics SVx", ELF::EM_SVX },
967 ENUM_ENT(EM_ST19, "STMicroelectronics ST19 8-bit microcontroller"){ "EM_ST19", "STMicroelectronics ST19 8-bit microcontroller",
ELF::EM_ST19 }
,
968 ENUM_ENT(EM_VAX, "Digital VAX"){ "EM_VAX", "Digital VAX", ELF::EM_VAX },
969 ENUM_ENT(EM_CRIS, "Axis Communications 32-bit embedded processor"){ "EM_CRIS", "Axis Communications 32-bit embedded processor",
ELF::EM_CRIS }
,
970 ENUM_ENT(EM_JAVELIN, "Infineon Technologies 32-bit embedded cpu"){ "EM_JAVELIN", "Infineon Technologies 32-bit embedded cpu", ELF
::EM_JAVELIN }
,
971 ENUM_ENT(EM_FIREPATH, "Element 14 64-bit DSP processor"){ "EM_FIREPATH", "Element 14 64-bit DSP processor", ELF::EM_FIREPATH
}
,
972 ENUM_ENT(EM_ZSP, "LSI Logic's 16-bit DSP processor"){ "EM_ZSP", "LSI Logic's 16-bit DSP processor", ELF::EM_ZSP },
973 ENUM_ENT(EM_MMIX, "Donald Knuth's educational 64-bit processor"){ "EM_MMIX", "Donald Knuth's educational 64-bit processor", ELF
::EM_MMIX }
,
974 ENUM_ENT(EM_HUANY, "Harvard Universitys's machine-independent object format"){ "EM_HUANY", "Harvard Universitys's machine-independent object format"
, ELF::EM_HUANY }
,
975 ENUM_ENT(EM_PRISM, "Vitesse Prism"){ "EM_PRISM", "Vitesse Prism", ELF::EM_PRISM },
976 ENUM_ENT(EM_AVR, "Atmel AVR 8-bit microcontroller"){ "EM_AVR", "Atmel AVR 8-bit microcontroller", ELF::EM_AVR },
977 ENUM_ENT(EM_FR30, "Fujitsu FR30"){ "EM_FR30", "Fujitsu FR30", ELF::EM_FR30 },
978 ENUM_ENT(EM_D10V, "Mitsubishi D10V"){ "EM_D10V", "Mitsubishi D10V", ELF::EM_D10V },
979 ENUM_ENT(EM_D30V, "Mitsubishi D30V"){ "EM_D30V", "Mitsubishi D30V", ELF::EM_D30V },
980 ENUM_ENT(EM_V850, "NEC v850"){ "EM_V850", "NEC v850", ELF::EM_V850 },
981 ENUM_ENT(EM_M32R, "Renesas M32R (formerly Mitsubishi M32r)"){ "EM_M32R", "Renesas M32R (formerly Mitsubishi M32r)", ELF::
EM_M32R }
,
982 ENUM_ENT(EM_MN10300, "Matsushita MN10300"){ "EM_MN10300", "Matsushita MN10300", ELF::EM_MN10300 },
983 ENUM_ENT(EM_MN10200, "Matsushita MN10200"){ "EM_MN10200", "Matsushita MN10200", ELF::EM_MN10200 },
984 ENUM_ENT(EM_PJ, "picoJava"){ "EM_PJ", "picoJava", ELF::EM_PJ },
985 ENUM_ENT(EM_OPENRISC, "OpenRISC 32-bit embedded processor"){ "EM_OPENRISC", "OpenRISC 32-bit embedded processor", ELF::EM_OPENRISC
}
,
986 ENUM_ENT(EM_ARC_COMPACT, "EM_ARC_COMPACT"){ "EM_ARC_COMPACT", "EM_ARC_COMPACT", ELF::EM_ARC_COMPACT },
987 ENUM_ENT(EM_XTENSA, "Tensilica Xtensa Processor"){ "EM_XTENSA", "Tensilica Xtensa Processor", ELF::EM_XTENSA },
988 ENUM_ENT(EM_VIDEOCORE, "Alphamosaic VideoCore processor"){ "EM_VIDEOCORE", "Alphamosaic VideoCore processor", ELF::EM_VIDEOCORE
}
,
989 ENUM_ENT(EM_TMM_GPP, "Thompson Multimedia General Purpose Processor"){ "EM_TMM_GPP", "Thompson Multimedia General Purpose Processor"
, ELF::EM_TMM_GPP }
,
990 ENUM_ENT(EM_NS32K, "National Semiconductor 32000 series"){ "EM_NS32K", "National Semiconductor 32000 series", ELF::EM_NS32K
}
,
991 ENUM_ENT(EM_TPC, "Tenor Network TPC processor"){ "EM_TPC", "Tenor Network TPC processor", ELF::EM_TPC },
992 ENUM_ENT(EM_SNP1K, "EM_SNP1K"){ "EM_SNP1K", "EM_SNP1K", ELF::EM_SNP1K },
993 ENUM_ENT(EM_ST200, "STMicroelectronics ST200 microcontroller"){ "EM_ST200", "STMicroelectronics ST200 microcontroller", ELF
::EM_ST200 }
,
994 ENUM_ENT(EM_IP2K, "Ubicom IP2xxx 8-bit microcontrollers"){ "EM_IP2K", "Ubicom IP2xxx 8-bit microcontrollers", ELF::EM_IP2K
}
,
995 ENUM_ENT(EM_MAX, "MAX Processor"){ "EM_MAX", "MAX Processor", ELF::EM_MAX },
996 ENUM_ENT(EM_CR, "National Semiconductor CompactRISC"){ "EM_CR", "National Semiconductor CompactRISC", ELF::EM_CR },
997 ENUM_ENT(EM_F2MC16, "Fujitsu F2MC16"){ "EM_F2MC16", "Fujitsu F2MC16", ELF::EM_F2MC16 },
998 ENUM_ENT(EM_MSP430, "Texas Instruments msp430 microcontroller"){ "EM_MSP430", "Texas Instruments msp430 microcontroller", ELF
::EM_MSP430 }
,
999 ENUM_ENT(EM_BLACKFIN, "Analog Devices Blackfin"){ "EM_BLACKFIN", "Analog Devices Blackfin", ELF::EM_BLACKFIN },
1000 ENUM_ENT(EM_SE_C33, "S1C33 Family of Seiko Epson processors"){ "EM_SE_C33", "S1C33 Family of Seiko Epson processors", ELF::
EM_SE_C33 }
,
1001 ENUM_ENT(EM_SEP, "Sharp embedded microprocessor"){ "EM_SEP", "Sharp embedded microprocessor", ELF::EM_SEP },
1002 ENUM_ENT(EM_ARCA, "Arca RISC microprocessor"){ "EM_ARCA", "Arca RISC microprocessor", ELF::EM_ARCA },
1003 ENUM_ENT(EM_UNICORE, "Unicore"){ "EM_UNICORE", "Unicore", ELF::EM_UNICORE },
1004 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 }
,
1005 ENUM_ENT(EM_DXP, "Icera Semiconductor Inc. Deep Execution Processor"){ "EM_DXP", "Icera Semiconductor Inc. Deep Execution Processor"
, ELF::EM_DXP }
,
1006 ENUM_ENT(EM_ALTERA_NIOS2, "Altera Nios"){ "EM_ALTERA_NIOS2", "Altera Nios", ELF::EM_ALTERA_NIOS2 },
1007 ENUM_ENT(EM_CRX, "National Semiconductor CRX microprocessor"){ "EM_CRX", "National Semiconductor CRX microprocessor", ELF::
EM_CRX }
,
1008 ENUM_ENT(EM_XGATE, "Motorola XGATE embedded processor"){ "EM_XGATE", "Motorola XGATE embedded processor", ELF::EM_XGATE
}
,
1009 ENUM_ENT(EM_C166, "Infineon Technologies xc16x"){ "EM_C166", "Infineon Technologies xc16x", ELF::EM_C166 },
1010 ENUM_ENT(EM_M16C, "Renesas M16C"){ "EM_M16C", "Renesas M16C", ELF::EM_M16C },
1011 ENUM_ENT(EM_DSPIC30F, "Microchip Technology dsPIC30F Digital Signal Controller"){ "EM_DSPIC30F", "Microchip Technology dsPIC30F Digital Signal Controller"
, ELF::EM_DSPIC30F }
,
1012 ENUM_ENT(EM_CE, "Freescale Communication Engine RISC core"){ "EM_CE", "Freescale Communication Engine RISC core", ELF::EM_CE
}
,
1013 ENUM_ENT(EM_M32C, "Renesas M32C"){ "EM_M32C", "Renesas M32C", ELF::EM_M32C },
1014 ENUM_ENT(EM_TSK3000, "Altium TSK3000 core"){ "EM_TSK3000", "Altium TSK3000 core", ELF::EM_TSK3000 },
1015 ENUM_ENT(EM_RS08, "Freescale RS08 embedded processor"){ "EM_RS08", "Freescale RS08 embedded processor", ELF::EM_RS08
}
,
1016 ENUM_ENT(EM_SHARC, "EM_SHARC"){ "EM_SHARC", "EM_SHARC", ELF::EM_SHARC },
1017 ENUM_ENT(EM_ECOG2, "Cyan Technology eCOG2 microprocessor"){ "EM_ECOG2", "Cyan Technology eCOG2 microprocessor", ELF::EM_ECOG2
}
,
1018 ENUM_ENT(EM_SCORE7, "SUNPLUS S+Core"){ "EM_SCORE7", "SUNPLUS S+Core", ELF::EM_SCORE7 },
1019 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 }
,
1020 ENUM_ENT(EM_VIDEOCORE3, "Broadcom VideoCore III processor"){ "EM_VIDEOCORE3", "Broadcom VideoCore III processor", ELF::EM_VIDEOCORE3
}
,
1021 ENUM_ENT(EM_LATTICEMICO32, "Lattice Mico32"){ "EM_LATTICEMICO32", "Lattice Mico32", ELF::EM_LATTICEMICO32
}
,
1022 ENUM_ENT(EM_SE_C17, "Seiko Epson C17 family"){ "EM_SE_C17", "Seiko Epson C17 family", ELF::EM_SE_C17 },
1023 ENUM_ENT(EM_TI_C6000, "Texas Instruments TMS320C6000 DSP family"){ "EM_TI_C6000", "Texas Instruments TMS320C6000 DSP family", ELF
::EM_TI_C6000 }
,
1024 ENUM_ENT(EM_TI_C2000, "Texas Instruments TMS320C2000 DSP family"){ "EM_TI_C2000", "Texas Instruments TMS320C2000 DSP family", ELF
::EM_TI_C2000 }
,
1025 ENUM_ENT(EM_TI_C5500, "Texas Instruments TMS320C55x DSP family"){ "EM_TI_C5500", "Texas Instruments TMS320C55x DSP family", ELF
::EM_TI_C5500 }
,
1026 ENUM_ENT(EM_MMDSP_PLUS, "STMicroelectronics 64bit VLIW Data Signal Processor"){ "EM_MMDSP_PLUS", "STMicroelectronics 64bit VLIW Data Signal Processor"
, ELF::EM_MMDSP_PLUS }
,
1027 ENUM_ENT(EM_CYPRESS_M8C, "Cypress M8C microprocessor"){ "EM_CYPRESS_M8C", "Cypress M8C microprocessor", ELF::EM_CYPRESS_M8C
}
,
1028 ENUM_ENT(EM_R32C, "Renesas R32C series microprocessors"){ "EM_R32C", "Renesas R32C series microprocessors", ELF::EM_R32C
}
,
1029 ENUM_ENT(EM_TRIMEDIA, "NXP Semiconductors TriMedia architecture family"){ "EM_TRIMEDIA", "NXP Semiconductors TriMedia architecture family"
, ELF::EM_TRIMEDIA }
,
1030 ENUM_ENT(EM_HEXAGON, "Qualcomm Hexagon"){ "EM_HEXAGON", "Qualcomm Hexagon", ELF::EM_HEXAGON },
1031 ENUM_ENT(EM_8051, "Intel 8051 and variants"){ "EM_8051", "Intel 8051 and variants", ELF::EM_8051 },
1032 ENUM_ENT(EM_STXP7X, "STMicroelectronics STxP7x family"){ "EM_STXP7X", "STMicroelectronics STxP7x family", ELF::EM_STXP7X
}
,
1033 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 }
,
1034 ENUM_ENT(EM_ECOG1, "Cyan Technology eCOG1 microprocessor"){ "EM_ECOG1", "Cyan Technology eCOG1 microprocessor", ELF::EM_ECOG1
}
,
1035 ENUM_ENT(EM_ECOG1X, "Cyan Technology eCOG1X family"){ "EM_ECOG1X", "Cyan Technology eCOG1X family", ELF::EM_ECOG1X
}
,
1036 ENUM_ENT(EM_MAXQ30, "Dallas Semiconductor MAXQ30 Core microcontrollers"){ "EM_MAXQ30", "Dallas Semiconductor MAXQ30 Core microcontrollers"
, ELF::EM_MAXQ30 }
,
1037 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 }
,
1038 ENUM_ENT(EM_MANIK, "M2000 Reconfigurable RISC Microprocessor"){ "EM_MANIK", "M2000 Reconfigurable RISC Microprocessor", ELF
::EM_MANIK }
,
1039 ENUM_ENT(EM_CRAYNV2, "Cray Inc. NV2 vector architecture"){ "EM_CRAYNV2", "Cray Inc. NV2 vector architecture", ELF::EM_CRAYNV2
}
,
1040 ENUM_ENT(EM_RX, "Renesas RX"){ "EM_RX", "Renesas RX", ELF::EM_RX },
1041 ENUM_ENT(EM_METAG, "Imagination Technologies Meta processor architecture"){ "EM_METAG", "Imagination Technologies Meta processor architecture"
, ELF::EM_METAG }
,
1042 ENUM_ENT(EM_MCST_ELBRUS, "MCST Elbrus general purpose hardware architecture"){ "EM_MCST_ELBRUS", "MCST Elbrus general purpose hardware architecture"
, ELF::EM_MCST_ELBRUS }
,
1043 ENUM_ENT(EM_ECOG16, "Cyan Technology eCOG16 family"){ "EM_ECOG16", "Cyan Technology eCOG16 family", ELF::EM_ECOG16
}
,
1044 ENUM_ENT(EM_CR16, "Xilinx MicroBlaze"){ "EM_CR16", "Xilinx MicroBlaze", ELF::EM_CR16 },
1045 ENUM_ENT(EM_ETPU, "Freescale Extended Time Processing Unit"){ "EM_ETPU", "Freescale Extended Time Processing Unit", ELF::
EM_ETPU }
,
1046 ENUM_ENT(EM_SLE9X, "Infineon Technologies SLE9X core"){ "EM_SLE9X", "Infineon Technologies SLE9X core", ELF::EM_SLE9X
}
,
1047 ENUM_ENT(EM_L10M, "EM_L10M"){ "EM_L10M", "EM_L10M", ELF::EM_L10M },
1048 ENUM_ENT(EM_K10M, "EM_K10M"){ "EM_K10M", "EM_K10M", ELF::EM_K10M },
1049 ENUM_ENT(EM_AARCH64, "AArch64"){ "EM_AARCH64", "AArch64", ELF::EM_AARCH64 },
1050 ENUM_ENT(EM_AVR32, "Atmel Corporation 32-bit microprocessor family"){ "EM_AVR32", "Atmel Corporation 32-bit microprocessor family"
, ELF::EM_AVR32 }
,
1051 ENUM_ENT(EM_STM8, "STMicroeletronics STM8 8-bit microcontroller"){ "EM_STM8", "STMicroeletronics STM8 8-bit microcontroller", ELF
::EM_STM8 }
,
1052 ENUM_ENT(EM_TILE64, "Tilera TILE64 multicore architecture family"){ "EM_TILE64", "Tilera TILE64 multicore architecture family",
ELF::EM_TILE64 }
,
1053 ENUM_ENT(EM_TILEPRO, "Tilera TILEPro multicore architecture family"){ "EM_TILEPRO", "Tilera TILEPro multicore architecture family"
, ELF::EM_TILEPRO }
,
1054 ENUM_ENT(EM_CUDA, "NVIDIA CUDA architecture"){ "EM_CUDA", "NVIDIA CUDA architecture", ELF::EM_CUDA },
1055 ENUM_ENT(EM_TILEGX, "Tilera TILE-Gx multicore architecture family"){ "EM_TILEGX", "Tilera TILE-Gx multicore architecture family"
, ELF::EM_TILEGX }
,
1056 ENUM_ENT(EM_CLOUDSHIELD, "EM_CLOUDSHIELD"){ "EM_CLOUDSHIELD", "EM_CLOUDSHIELD", ELF::EM_CLOUDSHIELD },
1057 ENUM_ENT(EM_COREA_1ST, "EM_COREA_1ST"){ "EM_COREA_1ST", "EM_COREA_1ST", ELF::EM_COREA_1ST },
1058 ENUM_ENT(EM_COREA_2ND, "EM_COREA_2ND"){ "EM_COREA_2ND", "EM_COREA_2ND", ELF::EM_COREA_2ND },
1059 ENUM_ENT(EM_ARC_COMPACT2, "EM_ARC_COMPACT2"){ "EM_ARC_COMPACT2", "EM_ARC_COMPACT2", ELF::EM_ARC_COMPACT2 },
1060 ENUM_ENT(EM_OPEN8, "EM_OPEN8"){ "EM_OPEN8", "EM_OPEN8", ELF::EM_OPEN8 },
1061 ENUM_ENT(EM_RL78, "Renesas RL78"){ "EM_RL78", "Renesas RL78", ELF::EM_RL78 },
1062 ENUM_ENT(EM_VIDEOCORE5, "Broadcom VideoCore V processor"){ "EM_VIDEOCORE5", "Broadcom VideoCore V processor", ELF::EM_VIDEOCORE5
}
,
1063 ENUM_ENT(EM_78KOR, "EM_78KOR"){ "EM_78KOR", "EM_78KOR", ELF::EM_78KOR },
1064 ENUM_ENT(EM_56800EX, "EM_56800EX"){ "EM_56800EX", "EM_56800EX", ELF::EM_56800EX },
1065 ENUM_ENT(EM_AMDGPU, "EM_AMDGPU"){ "EM_AMDGPU", "EM_AMDGPU", ELF::EM_AMDGPU },
1066 ENUM_ENT(EM_RISCV, "RISC-V"){ "EM_RISCV", "RISC-V", ELF::EM_RISCV },
1067 ENUM_ENT(EM_LANAI, "EM_LANAI"){ "EM_LANAI", "EM_LANAI", ELF::EM_LANAI },
1068 ENUM_ENT(EM_BPF, "EM_BPF"){ "EM_BPF", "EM_BPF", ELF::EM_BPF },
1069};
1070
1071static const EnumEntry<unsigned> ElfSymbolBindings[] = {
1072 {"Local", "LOCAL", ELF::STB_LOCAL},
1073 {"Global", "GLOBAL", ELF::STB_GLOBAL},
1074 {"Weak", "WEAK", ELF::STB_WEAK},
1075 {"Unique", "UNIQUE", ELF::STB_GNU_UNIQUE}};
1076
1077static const EnumEntry<unsigned> ElfSymbolVisibilities[] = {
1078 {"DEFAULT", "DEFAULT", ELF::STV_DEFAULT},
1079 {"INTERNAL", "INTERNAL", ELF::STV_INTERNAL},
1080 {"HIDDEN", "HIDDEN", ELF::STV_HIDDEN},
1081 {"PROTECTED", "PROTECTED", ELF::STV_PROTECTED}};
1082
1083static const EnumEntry<unsigned> ElfSymbolTypes[] = {
1084 {"None", "NOTYPE", ELF::STT_NOTYPE},
1085 {"Object", "OBJECT", ELF::STT_OBJECT},
1086 {"Function", "FUNC", ELF::STT_FUNC},
1087 {"Section", "SECTION", ELF::STT_SECTION},
1088 {"File", "FILE", ELF::STT_FILE},
1089 {"Common", "COMMON", ELF::STT_COMMON},
1090 {"TLS", "TLS", ELF::STT_TLS},
1091 {"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC}};
1092
1093static const EnumEntry<unsigned> AMDGPUSymbolTypes[] = {
1094 { "AMDGPU_HSA_KERNEL", ELF::STT_AMDGPU_HSA_KERNEL }
1095};
1096
1097static const char *getGroupType(uint32_t Flag) {
1098 if (Flag & ELF::GRP_COMDAT)
1099 return "COMDAT";
1100 else
1101 return "(unknown)";
1102}
1103
1104static const EnumEntry<unsigned> ElfSectionFlags[] = {
1105 ENUM_ENT(SHF_WRITE, "W"){ "SHF_WRITE", "W", ELF::SHF_WRITE },
1106 ENUM_ENT(SHF_ALLOC, "A"){ "SHF_ALLOC", "A", ELF::SHF_ALLOC },
1107 ENUM_ENT(SHF_EXCLUDE, "E"){ "SHF_EXCLUDE", "E", ELF::SHF_EXCLUDE },
1108 ENUM_ENT(SHF_EXECINSTR, "X"){ "SHF_EXECINSTR", "X", ELF::SHF_EXECINSTR },
1109 ENUM_ENT(SHF_MERGE, "M"){ "SHF_MERGE", "M", ELF::SHF_MERGE },
1110 ENUM_ENT(SHF_STRINGS, "S"){ "SHF_STRINGS", "S", ELF::SHF_STRINGS },
1111 ENUM_ENT(SHF_INFO_LINK, "I"){ "SHF_INFO_LINK", "I", ELF::SHF_INFO_LINK },
1112 ENUM_ENT(SHF_LINK_ORDER, "L"){ "SHF_LINK_ORDER", "L", ELF::SHF_LINK_ORDER },
1113 ENUM_ENT(SHF_OS_NONCONFORMING, "o"){ "SHF_OS_NONCONFORMING", "o", ELF::SHF_OS_NONCONFORMING },
1114 ENUM_ENT(SHF_GROUP, "G"){ "SHF_GROUP", "G", ELF::SHF_GROUP },
1115 ENUM_ENT(SHF_TLS, "T"){ "SHF_TLS", "T", ELF::SHF_TLS },
1116 ENUM_ENT(SHF_MASKOS, "o"){ "SHF_MASKOS", "o", ELF::SHF_MASKOS },
1117 ENUM_ENT(SHF_MASKPROC, "p"){ "SHF_MASKPROC", "p", ELF::SHF_MASKPROC },
1118 ENUM_ENT_1(SHF_COMPRESSED){ "SHF_COMPRESSED", "SHF_COMPRESSED", ELF::SHF_COMPRESSED },
1119};
1120
1121static const EnumEntry<unsigned> ElfXCoreSectionFlags[] = {
1122 LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_CP_SECTION){ "XCORE_SHF_CP_SECTION", ELF::XCORE_SHF_CP_SECTION },
1123 LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_DP_SECTION){ "XCORE_SHF_DP_SECTION", ELF::XCORE_SHF_DP_SECTION }
1124};
1125
1126static const EnumEntry<unsigned> ElfARMSectionFlags[] = {
1127 LLVM_READOBJ_ENUM_ENT(ELF, SHF_ARM_PURECODE){ "SHF_ARM_PURECODE", ELF::SHF_ARM_PURECODE }
1128};
1129
1130static const EnumEntry<unsigned> ElfHexagonSectionFlags[] = {
1131 LLVM_READOBJ_ENUM_ENT(ELF, SHF_HEX_GPREL){ "SHF_HEX_GPREL", ELF::SHF_HEX_GPREL }
1132};
1133
1134static const EnumEntry<unsigned> ElfMipsSectionFlags[] = {
1135 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NODUPES){ "SHF_MIPS_NODUPES", ELF::SHF_MIPS_NODUPES },
1136 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NAMES ){ "SHF_MIPS_NAMES", ELF::SHF_MIPS_NAMES },
1137 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_LOCAL ){ "SHF_MIPS_LOCAL", ELF::SHF_MIPS_LOCAL },
1138 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NOSTRIP){ "SHF_MIPS_NOSTRIP", ELF::SHF_MIPS_NOSTRIP },
1139 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_GPREL ){ "SHF_MIPS_GPREL", ELF::SHF_MIPS_GPREL },
1140 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_MERGE ){ "SHF_MIPS_MERGE", ELF::SHF_MIPS_MERGE },
1141 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_ADDR ){ "SHF_MIPS_ADDR", ELF::SHF_MIPS_ADDR },
1142 LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_STRING ){ "SHF_MIPS_STRING", ELF::SHF_MIPS_STRING }
1143};
1144
1145static const EnumEntry<unsigned> ElfX86_64SectionFlags[] = {
1146 LLVM_READOBJ_ENUM_ENT(ELF, SHF_X86_64_LARGE){ "SHF_X86_64_LARGE", ELF::SHF_X86_64_LARGE }
1147};
1148
1149static std::string getGNUFlags(uint64_t Flags) {
1150 std::string Str;
1151 for (auto Entry : ElfSectionFlags) {
1152 uint64_t Flag = Entry.Value & Flags;
1153 Flags &= ~Entry.Value;
1154 switch (Flag) {
1155 case ELF::SHF_WRITE:
1156 case ELF::SHF_ALLOC:
1157 case ELF::SHF_EXECINSTR:
1158 case ELF::SHF_MERGE:
1159 case ELF::SHF_STRINGS:
1160 case ELF::SHF_INFO_LINK:
1161 case ELF::SHF_LINK_ORDER:
1162 case ELF::SHF_OS_NONCONFORMING:
1163 case ELF::SHF_GROUP:
1164 case ELF::SHF_TLS:
1165 case ELF::SHF_EXCLUDE:
1166 Str += Entry.AltName;
1167 break;
1168 default:
1169 if (Flag & ELF::SHF_MASKOS)
1170 Str += "o";
1171 else if (Flag & ELF::SHF_MASKPROC)
1172 Str += "p";
1173 else if (Flag)
1174 Str += "x";
1175 }
1176 }
1177 return Str;
1178}
1179
1180static const char *getElfSegmentType(unsigned Arch, unsigned Type) {
1181 // Check potentially overlapped processor-specific
1182 // program header type.
1183 switch (Arch) {
1184 case ELF::EM_ARM:
1185 switch (Type) {
1186 LLVM_READOBJ_ENUM_CASE(ELF, PT_ARM_EXIDX)case ELF::PT_ARM_EXIDX: return "PT_ARM_EXIDX";;
1187 }
1188 case ELF::EM_MIPS:
1189 case ELF::EM_MIPS_RS3_LE:
1190 switch (Type) {
1191 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_REGINFO)case ELF::PT_MIPS_REGINFO: return "PT_MIPS_REGINFO";;
1192 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_RTPROC)case ELF::PT_MIPS_RTPROC: return "PT_MIPS_RTPROC";;
1193 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_OPTIONS)case ELF::PT_MIPS_OPTIONS: return "PT_MIPS_OPTIONS";;
1194 LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_ABIFLAGS)case ELF::PT_MIPS_ABIFLAGS: return "PT_MIPS_ABIFLAGS";;
1195 }
1196 }
1197
1198 switch (Type) {
1199 LLVM_READOBJ_ENUM_CASE(ELF, PT_NULL )case ELF::PT_NULL: return "PT_NULL";;
1200 LLVM_READOBJ_ENUM_CASE(ELF, PT_LOAD )case ELF::PT_LOAD: return "PT_LOAD";;
1201 LLVM_READOBJ_ENUM_CASE(ELF, PT_DYNAMIC)case ELF::PT_DYNAMIC: return "PT_DYNAMIC";;
1202 LLVM_READOBJ_ENUM_CASE(ELF, PT_INTERP )case ELF::PT_INTERP: return "PT_INTERP";;
1203 LLVM_READOBJ_ENUM_CASE(ELF, PT_NOTE )case ELF::PT_NOTE: return "PT_NOTE";;
1204 LLVM_READOBJ_ENUM_CASE(ELF, PT_SHLIB )case ELF::PT_SHLIB: return "PT_SHLIB";;
1205 LLVM_READOBJ_ENUM_CASE(ELF, PT_PHDR )case ELF::PT_PHDR: return "PT_PHDR";;
1206 LLVM_READOBJ_ENUM_CASE(ELF, PT_TLS )case ELF::PT_TLS: return "PT_TLS";;
1207
1208 LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_EH_FRAME)case ELF::PT_GNU_EH_FRAME: return "PT_GNU_EH_FRAME";;
1209 LLVM_READOBJ_ENUM_CASE(ELF, PT_SUNW_UNWIND)case ELF::PT_SUNW_UNWIND: return "PT_SUNW_UNWIND";;
1210
1211 LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_STACK)case ELF::PT_GNU_STACK: return "PT_GNU_STACK";;
1212 LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_RELRO)case ELF::PT_GNU_RELRO: return "PT_GNU_RELRO";;
1213
1214 LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_RANDOMIZE)case ELF::PT_OPENBSD_RANDOMIZE: return "PT_OPENBSD_RANDOMIZE"
;
;
1215 LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_WXNEEDED)case ELF::PT_OPENBSD_WXNEEDED: return "PT_OPENBSD_WXNEEDED";;
1216 LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_BOOTDATA)case ELF::PT_OPENBSD_BOOTDATA: return "PT_OPENBSD_BOOTDATA";;
1217
1218 default: return "";
1219 }
1220}
1221
1222static std::string getElfPtType(unsigned Arch, unsigned Type) {
1223 switch (Type) {
1224 LLVM_READOBJ_PHDR_ENUM(ELF, PT_NULL)case ELF::PT_NULL: return std::string("PT_NULL").substr(3);
1225 LLVM_READOBJ_PHDR_ENUM(ELF, PT_LOAD)case ELF::PT_LOAD: return std::string("PT_LOAD").substr(3);
1226 LLVM_READOBJ_PHDR_ENUM(ELF, PT_DYNAMIC)case ELF::PT_DYNAMIC: return std::string("PT_DYNAMIC").substr
(3);
1227 LLVM_READOBJ_PHDR_ENUM(ELF, PT_INTERP)case ELF::PT_INTERP: return std::string("PT_INTERP").substr(3
);
1228 LLVM_READOBJ_PHDR_ENUM(ELF, PT_NOTE)case ELF::PT_NOTE: return std::string("PT_NOTE").substr(3);
1229 LLVM_READOBJ_PHDR_ENUM(ELF, PT_SHLIB)case ELF::PT_SHLIB: return std::string("PT_SHLIB").substr(3);
1230 LLVM_READOBJ_PHDR_ENUM(ELF, PT_PHDR)case ELF::PT_PHDR: return std::string("PT_PHDR").substr(3);
1231 LLVM_READOBJ_PHDR_ENUM(ELF, PT_TLS)case ELF::PT_TLS: return std::string("PT_TLS").substr(3);
1232 LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_EH_FRAME)case ELF::PT_GNU_EH_FRAME: return std::string("PT_GNU_EH_FRAME"
).substr(3);
1233 LLVM_READOBJ_PHDR_ENUM(ELF, PT_SUNW_UNWIND)case ELF::PT_SUNW_UNWIND: return std::string("PT_SUNW_UNWIND"
).substr(3);
1234 LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_STACK)case ELF::PT_GNU_STACK: return std::string("PT_GNU_STACK").substr
(3);
1235 LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_RELRO)case ELF::PT_GNU_RELRO: return std::string("PT_GNU_RELRO").substr
(3);
1236 default:
1237 // All machine specific PT_* types
1238 switch (Arch) {
1239 case ELF::EM_ARM:
1240 if (Type == ELF::PT_ARM_EXIDX)
1241 return "EXIDX";
1242 return "";
1243 case ELF::EM_MIPS:
1244 case ELF::EM_MIPS_RS3_LE:
1245 switch (Type) {
1246 case PT_MIPS_REGINFO:
1247 return "REGINFO";
1248 case PT_MIPS_RTPROC:
1249 return "RTPROC";
1250 case PT_MIPS_OPTIONS:
1251 return "OPTIONS";
1252 case PT_MIPS_ABIFLAGS:
1253 return "ABIFLAGS";
1254 }
1255 return "";
1256 }
1257 }
1258 return std::string("<unknown>: ") + to_string(format_hex(Type, 1));
1259}
1260
1261static const EnumEntry<unsigned> ElfSegmentFlags[] = {
1262 LLVM_READOBJ_ENUM_ENT(ELF, PF_X){ "PF_X", ELF::PF_X },
1263 LLVM_READOBJ_ENUM_ENT(ELF, PF_W){ "PF_W", ELF::PF_W },
1264 LLVM_READOBJ_ENUM_ENT(ELF, PF_R){ "PF_R", ELF::PF_R }
1265};
1266
1267static const EnumEntry<unsigned> ElfHeaderMipsFlags[] = {
1268 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_NOREORDER){ "EF_MIPS_NOREORDER", ELF::EF_MIPS_NOREORDER },
1269 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_PIC){ "EF_MIPS_PIC", ELF::EF_MIPS_PIC },
1270 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_CPIC){ "EF_MIPS_CPIC", ELF::EF_MIPS_CPIC },
1271 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI2){ "EF_MIPS_ABI2", ELF::EF_MIPS_ABI2 },
1272 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_32BITMODE){ "EF_MIPS_32BITMODE", ELF::EF_MIPS_32BITMODE },
1273 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_FP64){ "EF_MIPS_FP64", ELF::EF_MIPS_FP64 },
1274 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_NAN2008){ "EF_MIPS_NAN2008", ELF::EF_MIPS_NAN2008 },
1275 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI_O32){ "EF_MIPS_ABI_O32", ELF::EF_MIPS_ABI_O32 },
1276 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI_O64){ "EF_MIPS_ABI_O64", ELF::EF_MIPS_ABI_O64 },
1277 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI_EABI32){ "EF_MIPS_ABI_EABI32", ELF::EF_MIPS_ABI_EABI32 },
1278 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI_EABI64){ "EF_MIPS_ABI_EABI64", ELF::EF_MIPS_ABI_EABI64 },
1279 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_3900){ "EF_MIPS_MACH_3900", ELF::EF_MIPS_MACH_3900 },
1280 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4010){ "EF_MIPS_MACH_4010", ELF::EF_MIPS_MACH_4010 },
1281 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4100){ "EF_MIPS_MACH_4100", ELF::EF_MIPS_MACH_4100 },
1282 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4650){ "EF_MIPS_MACH_4650", ELF::EF_MIPS_MACH_4650 },
1283 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4120){ "EF_MIPS_MACH_4120", ELF::EF_MIPS_MACH_4120 },
1284 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4111){ "EF_MIPS_MACH_4111", ELF::EF_MIPS_MACH_4111 },
1285 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_SB1){ "EF_MIPS_MACH_SB1", ELF::EF_MIPS_MACH_SB1 },
1286 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_OCTEON){ "EF_MIPS_MACH_OCTEON", ELF::EF_MIPS_MACH_OCTEON },
1287 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_XLR){ "EF_MIPS_MACH_XLR", ELF::EF_MIPS_MACH_XLR },
1288 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_OCTEON2){ "EF_MIPS_MACH_OCTEON2", ELF::EF_MIPS_MACH_OCTEON2 },
1289 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_OCTEON3){ "EF_MIPS_MACH_OCTEON3", ELF::EF_MIPS_MACH_OCTEON3 },
1290 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_5400){ "EF_MIPS_MACH_5400", ELF::EF_MIPS_MACH_5400 },
1291 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_5900){ "EF_MIPS_MACH_5900", ELF::EF_MIPS_MACH_5900 },
1292 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_5500){ "EF_MIPS_MACH_5500", ELF::EF_MIPS_MACH_5500 },
1293 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_9000){ "EF_MIPS_MACH_9000", ELF::EF_MIPS_MACH_9000 },
1294 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_LS2E){ "EF_MIPS_MACH_LS2E", ELF::EF_MIPS_MACH_LS2E },
1295 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_LS2F){ "EF_MIPS_MACH_LS2F", ELF::EF_MIPS_MACH_LS2F },
1296 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_LS3A){ "EF_MIPS_MACH_LS3A", ELF::EF_MIPS_MACH_LS3A },
1297 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MICROMIPS){ "EF_MIPS_MICROMIPS", ELF::EF_MIPS_MICROMIPS },
1298 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_ASE_M16){ "EF_MIPS_ARCH_ASE_M16", ELF::EF_MIPS_ARCH_ASE_M16 },
1299 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_ASE_MDMX){ "EF_MIPS_ARCH_ASE_MDMX", ELF::EF_MIPS_ARCH_ASE_MDMX },
1300 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_1){ "EF_MIPS_ARCH_1", ELF::EF_MIPS_ARCH_1 },
1301 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_2){ "EF_MIPS_ARCH_2", ELF::EF_MIPS_ARCH_2 },
1302 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_3){ "EF_MIPS_ARCH_3", ELF::EF_MIPS_ARCH_3 },
1303 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_4){ "EF_MIPS_ARCH_4", ELF::EF_MIPS_ARCH_4 },
1304 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_5){ "EF_MIPS_ARCH_5", ELF::EF_MIPS_ARCH_5 },
1305 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_32){ "EF_MIPS_ARCH_32", ELF::EF_MIPS_ARCH_32 },
1306 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_64){ "EF_MIPS_ARCH_64", ELF::EF_MIPS_ARCH_64 },
1307 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_32R2){ "EF_MIPS_ARCH_32R2", ELF::EF_MIPS_ARCH_32R2 },
1308 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_64R2){ "EF_MIPS_ARCH_64R2", ELF::EF_MIPS_ARCH_64R2 },
1309 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_32R6){ "EF_MIPS_ARCH_32R6", ELF::EF_MIPS_ARCH_32R6 },
1310 LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_64R6){ "EF_MIPS_ARCH_64R6", ELF::EF_MIPS_ARCH_64R6 }
1311};
1312
1313static const EnumEntry<unsigned> ElfHeaderAMDGPUFlags[] = {
1314 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_NONE){ "EF_AMDGPU_MACH_NONE", ELF::EF_AMDGPU_MACH_NONE },
1315 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R600){ "EF_AMDGPU_MACH_R600_R600", ELF::EF_AMDGPU_MACH_R600_R600 },
1316 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R630){ "EF_AMDGPU_MACH_R600_R630", ELF::EF_AMDGPU_MACH_R600_R630 },
1317 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RS880){ "EF_AMDGPU_MACH_R600_RS880", ELF::EF_AMDGPU_MACH_R600_RS880
}
,
1318 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV670){ "EF_AMDGPU_MACH_R600_RV670", ELF::EF_AMDGPU_MACH_R600_RV670
}
,
1319 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV710){ "EF_AMDGPU_MACH_R600_RV710", ELF::EF_AMDGPU_MACH_R600_RV710
}
,
1320 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV730){ "EF_AMDGPU_MACH_R600_RV730", ELF::EF_AMDGPU_MACH_R600_RV730
}
,
1321 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV770){ "EF_AMDGPU_MACH_R600_RV770", ELF::EF_AMDGPU_MACH_R600_RV770
}
,
1322 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CEDAR){ "EF_AMDGPU_MACH_R600_CEDAR", ELF::EF_AMDGPU_MACH_R600_CEDAR
}
,
1323 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CYPRESS){ "EF_AMDGPU_MACH_R600_CYPRESS", ELF::EF_AMDGPU_MACH_R600_CYPRESS
}
,
1324 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_JUNIPER){ "EF_AMDGPU_MACH_R600_JUNIPER", ELF::EF_AMDGPU_MACH_R600_JUNIPER
}
,
1325 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_REDWOOD){ "EF_AMDGPU_MACH_R600_REDWOOD", ELF::EF_AMDGPU_MACH_R600_REDWOOD
}
,
1326 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_SUMO){ "EF_AMDGPU_MACH_R600_SUMO", ELF::EF_AMDGPU_MACH_R600_SUMO },
1327 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_BARTS){ "EF_AMDGPU_MACH_R600_BARTS", ELF::EF_AMDGPU_MACH_R600_BARTS
}
,
1328 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAICOS){ "EF_AMDGPU_MACH_R600_CAICOS", ELF::EF_AMDGPU_MACH_R600_CAICOS
}
,
1329 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAYMAN){ "EF_AMDGPU_MACH_R600_CAYMAN", ELF::EF_AMDGPU_MACH_R600_CAYMAN
}
,
1330 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_TURKS){ "EF_AMDGPU_MACH_R600_TURKS", ELF::EF_AMDGPU_MACH_R600_TURKS
}
,
1331 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX600){ "EF_AMDGPU_MACH_AMDGCN_GFX600", ELF::EF_AMDGPU_MACH_AMDGCN_GFX600
}
,
1332 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX601){ "EF_AMDGPU_MACH_AMDGCN_GFX601", ELF::EF_AMDGPU_MACH_AMDGCN_GFX601
}
,
1333 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX700){ "EF_AMDGPU_MACH_AMDGCN_GFX700", ELF::EF_AMDGPU_MACH_AMDGCN_GFX700
}
,
1334 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX701){ "EF_AMDGPU_MACH_AMDGCN_GFX701", ELF::EF_AMDGPU_MACH_AMDGCN_GFX701
}
,
1335 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX702){ "EF_AMDGPU_MACH_AMDGCN_GFX702", ELF::EF_AMDGPU_MACH_AMDGCN_GFX702
}
,
1336 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX703){ "EF_AMDGPU_MACH_AMDGCN_GFX703", ELF::EF_AMDGPU_MACH_AMDGCN_GFX703
}
,
1337 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX704){ "EF_AMDGPU_MACH_AMDGCN_GFX704", ELF::EF_AMDGPU_MACH_AMDGCN_GFX704
}
,
1338 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX801){ "EF_AMDGPU_MACH_AMDGCN_GFX801", ELF::EF_AMDGPU_MACH_AMDGCN_GFX801
}
,
1339 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX802){ "EF_AMDGPU_MACH_AMDGCN_GFX802", ELF::EF_AMDGPU_MACH_AMDGCN_GFX802
}
,
1340 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX803){ "EF_AMDGPU_MACH_AMDGCN_GFX803", ELF::EF_AMDGPU_MACH_AMDGCN_GFX803
}
,
1341 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX810){ "EF_AMDGPU_MACH_AMDGCN_GFX810", ELF::EF_AMDGPU_MACH_AMDGCN_GFX810
}
,
1342 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX900){ "EF_AMDGPU_MACH_AMDGCN_GFX900", ELF::EF_AMDGPU_MACH_AMDGCN_GFX900
}
,
1343 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX902){ "EF_AMDGPU_MACH_AMDGCN_GFX902", ELF::EF_AMDGPU_MACH_AMDGCN_GFX902
}
,
1344 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX904){ "EF_AMDGPU_MACH_AMDGCN_GFX904", ELF::EF_AMDGPU_MACH_AMDGCN_GFX904
}
,
1345 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX906){ "EF_AMDGPU_MACH_AMDGCN_GFX906", ELF::EF_AMDGPU_MACH_AMDGCN_GFX906
}
,
1346 LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_XNACK){ "EF_AMDGPU_XNACK", ELF::EF_AMDGPU_XNACK }
1347};
1348
1349static const EnumEntry<unsigned> ElfHeaderRISCVFlags[] = {
1350 LLVM_READOBJ_ENUM_ENT(ELF, EF_RISCV_RVC){ "EF_RISCV_RVC", ELF::EF_RISCV_RVC },
1351 LLVM_READOBJ_ENUM_ENT(ELF, EF_RISCV_FLOAT_ABI_SINGLE){ "EF_RISCV_FLOAT_ABI_SINGLE", ELF::EF_RISCV_FLOAT_ABI_SINGLE
}
,
1352 LLVM_READOBJ_ENUM_ENT(ELF, EF_RISCV_FLOAT_ABI_DOUBLE){ "EF_RISCV_FLOAT_ABI_DOUBLE", ELF::EF_RISCV_FLOAT_ABI_DOUBLE
}
,
1353 LLVM_READOBJ_ENUM_ENT(ELF, EF_RISCV_FLOAT_ABI_QUAD){ "EF_RISCV_FLOAT_ABI_QUAD", ELF::EF_RISCV_FLOAT_ABI_QUAD },
1354 LLVM_READOBJ_ENUM_ENT(ELF, EF_RISCV_RVE){ "EF_RISCV_RVE", ELF::EF_RISCV_RVE }
1355};
1356
1357static const EnumEntry<unsigned> ElfSymOtherFlags[] = {
1358 LLVM_READOBJ_ENUM_ENT(ELF, STV_INTERNAL){ "STV_INTERNAL", ELF::STV_INTERNAL },
1359 LLVM_READOBJ_ENUM_ENT(ELF, STV_HIDDEN){ "STV_HIDDEN", ELF::STV_HIDDEN },
1360 LLVM_READOBJ_ENUM_ENT(ELF, STV_PROTECTED){ "STV_PROTECTED", ELF::STV_PROTECTED }
1361};
1362
1363static const EnumEntry<unsigned> ElfMipsSymOtherFlags[] = {
1364 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL){ "STO_MIPS_OPTIONAL", ELF::STO_MIPS_OPTIONAL },
1365 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT){ "STO_MIPS_PLT", ELF::STO_MIPS_PLT },
1366 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PIC){ "STO_MIPS_PIC", ELF::STO_MIPS_PIC },
1367 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MICROMIPS){ "STO_MIPS_MICROMIPS", ELF::STO_MIPS_MICROMIPS }
1368};
1369
1370static const EnumEntry<unsigned> ElfMips16SymOtherFlags[] = {
1371 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL){ "STO_MIPS_OPTIONAL", ELF::STO_MIPS_OPTIONAL },
1372 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT){ "STO_MIPS_PLT", ELF::STO_MIPS_PLT },
1373 LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MIPS16){ "STO_MIPS_MIPS16", ELF::STO_MIPS_MIPS16 }
1374};
1375
1376static const char *getElfMipsOptionsOdkType(unsigned Odk) {
1377 switch (Odk) {
1378 LLVM_READOBJ_ENUM_CASE(ELF, ODK_NULL)case ELF::ODK_NULL: return "ODK_NULL";;
1379 LLVM_READOBJ_ENUM_CASE(ELF, ODK_REGINFO)case ELF::ODK_REGINFO: return "ODK_REGINFO";;
1380 LLVM_READOBJ_ENUM_CASE(ELF, ODK_EXCEPTIONS)case ELF::ODK_EXCEPTIONS: return "ODK_EXCEPTIONS";;
1381 LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAD)case ELF::ODK_PAD: return "ODK_PAD";;
1382 LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWPATCH)case ELF::ODK_HWPATCH: return "ODK_HWPATCH";;
1383 LLVM_READOBJ_ENUM_CASE(ELF, ODK_FILL)case ELF::ODK_FILL: return "ODK_FILL";;
1384 LLVM_READOBJ_ENUM_CASE(ELF, ODK_TAGS)case ELF::ODK_TAGS: return "ODK_TAGS";;
1385 LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWAND)case ELF::ODK_HWAND: return "ODK_HWAND";;
1386 LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWOR)case ELF::ODK_HWOR: return "ODK_HWOR";;
1387 LLVM_READOBJ_ENUM_CASE(ELF, ODK_GP_GROUP)case ELF::ODK_GP_GROUP: return "ODK_GP_GROUP";;
1388 LLVM_READOBJ_ENUM_CASE(ELF, ODK_IDENT)case ELF::ODK_IDENT: return "ODK_IDENT";;
1389 LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAGESIZE)case ELF::ODK_PAGESIZE: return "ODK_PAGESIZE";;
1390 default:
1391 return "Unknown";
1392 }
1393}
1394
1395template <typename ELFT>
1396ELFDumper<ELFT>::ELFDumper(const ELFFile<ELFT> *Obj, ScopedPrinter &Writer)
1397 : ObjDumper(Writer), Obj(Obj) {
1398 SmallVector<const Elf_Phdr *, 4> LoadSegments;
1399 for (const Elf_Phdr &Phdr : unwrapOrError(Obj->program_headers())) {
1400 if (Phdr.p_type == ELF::PT_DYNAMIC) {
1401 DynamicTable = createDRIFrom(&Phdr, sizeof(Elf_Dyn));
1402 continue;
1403 }
1404 if (Phdr.p_type != ELF::PT_LOAD || Phdr.p_filesz == 0)
1405 continue;
1406 LoadSegments.push_back(&Phdr);
1407 }
1408
1409 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
1410 switch (Sec.sh_type) {
1411 case ELF::SHT_SYMTAB:
1412 if (DotSymtabSec != nullptr)
1413 reportError("Multiple SHT_SYMTAB");
1414 DotSymtabSec = &Sec;
1415 break;
1416 case ELF::SHT_DYNSYM:
1417 if (DynSymRegion.Size)
1418 reportError("Multiple SHT_DYNSYM");
1419 DynSymRegion = createDRIFrom(&Sec);
1420 // This is only used (if Elf_Shdr present)for naming section in GNU style
1421 DynSymtabName = unwrapOrError(Obj->getSectionName(&Sec));
1422 DynamicStringTable = unwrapOrError(Obj->getStringTableForSymtab(Sec));
1423 break;
1424 case ELF::SHT_SYMTAB_SHNDX:
1425 ShndxTable = unwrapOrError(Obj->getSHNDXTable(Sec));
1426 break;
1427 case ELF::SHT_GNU_versym:
1428 if (dot_gnu_version_sec != nullptr)
1429 reportError("Multiple SHT_GNU_versym");
1430 dot_gnu_version_sec = &Sec;
1431 break;
1432 case ELF::SHT_GNU_verdef:
1433 if (dot_gnu_version_d_sec != nullptr)
1434 reportError("Multiple SHT_GNU_verdef");
1435 dot_gnu_version_d_sec = &Sec;
1436 break;
1437 case ELF::SHT_GNU_verneed:
1438 if (dot_gnu_version_r_sec != nullptr)
1439 reportError("Multiple SHT_GNU_verneed");
1440 dot_gnu_version_r_sec = &Sec;
1441 break;
1442 case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
1443 if (DotCGProfileSec != nullptr)
1444 reportError("Multiple .note.llvm.cgprofile");
1445 DotCGProfileSec = &Sec;
1446 break;
1447 case ELF::SHT_LLVM_ADDRSIG:
1448 if (DotAddrsigSec != nullptr)
1449 reportError("Multiple .llvm_addrsig");
1450 DotAddrsigSec = &Sec;
1451 break;
1452 }
1453 }
1454
1455 parseDynamicTable(LoadSegments);
1456
1457 if (opts::Output == opts::GNU)
1458 ELFDumperStyle.reset(new GNUStyle<ELFT>(Writer, this));
1459 else
1460 ELFDumperStyle.reset(new LLVMStyle<ELFT>(Writer, this));
1461}
1462
1463template <typename ELFT>
1464void ELFDumper<ELFT>::parseDynamicTable(
1465 ArrayRef<const Elf_Phdr *> LoadSegments) {
1466 auto toMappedAddr = [&](uint64_t VAddr) -> const uint8_t * {
1467 const Elf_Phdr *const *I =
1468 std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr,
1469 [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
1470 return VAddr < Phdr->p_vaddr;
1471 });
1472 if (I == LoadSegments.begin())
1473 report_fatal_error("Virtual address is not in any segment");
1474 --I;
1475 const Elf_Phdr &Phdr = **I;
1476 uint64_t Delta = VAddr - Phdr.p_vaddr;
1477 if (Delta >= Phdr.p_filesz)
1478 report_fatal_error("Virtual address is not in any segment");
1479 return Obj->base() + Phdr.p_offset + Delta;
1480 };
1481
1482 uint64_t SONameOffset = 0;
1483 const char *StringTableBegin = nullptr;
1484 uint64_t StringTableSize = 0;
1485 for (const Elf_Dyn &Dyn : dynamic_table()) {
1486 switch (Dyn.d_tag) {
1487 case ELF::DT_HASH:
1488 HashTable =
1489 reinterpret_cast<const Elf_Hash *>(toMappedAddr(Dyn.getPtr()));
1490 break;
1491 case ELF::DT_GNU_HASH:
1492 GnuHashTable =
1493 reinterpret_cast<const Elf_GnuHash *>(toMappedAddr(Dyn.getPtr()));
1494 break;
1495 case ELF::DT_STRTAB:
1496 StringTableBegin = (const char *)toMappedAddr(Dyn.getPtr());
1497 break;
1498 case ELF::DT_STRSZ:
1499 StringTableSize = Dyn.getVal();
1500 break;
1501 case ELF::DT_SYMTAB:
1502 DynSymRegion.Addr = toMappedAddr(Dyn.getPtr());
1503 DynSymRegion.EntSize = sizeof(Elf_Sym);
1504 break;
1505 case ELF::DT_RELA:
1506 DynRelaRegion.Addr = toMappedAddr(Dyn.getPtr());
1507 break;
1508 case ELF::DT_RELASZ:
1509 DynRelaRegion.Size = Dyn.getVal();
1510 break;
1511 case ELF::DT_RELAENT:
1512 DynRelaRegion.EntSize = Dyn.getVal();
1513 break;
1514 case ELF::DT_SONAME:
1515 SONameOffset = Dyn.getVal();
1516 break;
1517 case ELF::DT_REL:
1518 DynRelRegion.Addr = toMappedAddr(Dyn.getPtr());
1519 break;
1520 case ELF::DT_RELSZ:
1521 DynRelRegion.Size = Dyn.getVal();
1522 break;
1523 case ELF::DT_RELENT:
1524 DynRelRegion.EntSize = Dyn.getVal();
1525 break;
1526 case ELF::DT_RELR:
1527 case ELF::DT_ANDROID_RELR:
1528 DynRelrRegion.Addr = toMappedAddr(Dyn.getPtr());
1529 break;
1530 case ELF::DT_RELRSZ:
1531 case ELF::DT_ANDROID_RELRSZ:
1532 DynRelrRegion.Size = Dyn.getVal();
1533 break;
1534 case ELF::DT_RELRENT:
1535 case ELF::DT_ANDROID_RELRENT:
1536 DynRelrRegion.EntSize = Dyn.getVal();
1537 break;
1538 case ELF::DT_PLTREL:
1539 if (Dyn.getVal() == DT_REL)
1540 DynPLTRelRegion.EntSize = sizeof(Elf_Rel);
1541 else if (Dyn.getVal() == DT_RELA)
1542 DynPLTRelRegion.EntSize = sizeof(Elf_Rela);
1543 else
1544 reportError(Twine("unknown DT_PLTREL value of ") +
1545 Twine((uint64_t)Dyn.getVal()));
1546 break;
1547 case ELF::DT_JMPREL:
1548 DynPLTRelRegion.Addr = toMappedAddr(Dyn.getPtr());
1549 break;
1550 case ELF::DT_PLTRELSZ:
1551 DynPLTRelRegion.Size = Dyn.getVal();
1552 break;
1553 }
1554 }
1555 if (StringTableBegin)
1556 DynamicStringTable = StringRef(StringTableBegin, StringTableSize);
1557 if (SONameOffset)
1558 SOName = getDynamicString(SONameOffset);
1559}
1560
1561template <typename ELFT>
1562typename ELFDumper<ELFT>::Elf_Rel_Range ELFDumper<ELFT>::dyn_rels() const {
1563 return DynRelRegion.getAsArrayRef<Elf_Rel>();
1564}
1565
1566template <typename ELFT>
1567typename ELFDumper<ELFT>::Elf_Rela_Range ELFDumper<ELFT>::dyn_relas() const {
1568 return DynRelaRegion.getAsArrayRef<Elf_Rela>();
1569}
1570
1571template <typename ELFT>
1572typename ELFDumper<ELFT>::Elf_Relr_Range ELFDumper<ELFT>::dyn_relrs() const {
1573 return DynRelrRegion.getAsArrayRef<Elf_Relr>();
1574}
1575
1576template<class ELFT>
1577void ELFDumper<ELFT>::printFileHeaders() {
1578 ELFDumperStyle->printFileHeaders(Obj);
1579}
1580
1581template<class ELFT>
1582void ELFDumper<ELFT>::printSections() {
1583 ELFDumperStyle->printSections(Obj);
1584}
1585
1586template<class ELFT>
1587void ELFDumper<ELFT>::printRelocations() {
1588 ELFDumperStyle->printRelocations(Obj);
1589}
1590
1591template <class ELFT> void ELFDumper<ELFT>::printProgramHeaders() {
1592 ELFDumperStyle->printProgramHeaders(Obj);
1593}
1594
1595template <class ELFT> void ELFDumper<ELFT>::printDynamicRelocations() {
1596 ELFDumperStyle->printDynamicRelocations(Obj);
1597}
1598
1599template<class ELFT>
1600void ELFDumper<ELFT>::printSymbols() {
1601 ELFDumperStyle->printSymbols(Obj);
1602}
1603
1604template<class ELFT>
1605void ELFDumper<ELFT>::printDynamicSymbols() {
1606 ELFDumperStyle->printDynamicSymbols(Obj);
1607}
1608
1609template <class ELFT> void ELFDumper<ELFT>::printHashHistogram() {
1610 ELFDumperStyle->printHashHistogram(Obj);
1611}
1612
1613template <class ELFT> void ELFDumper<ELFT>::printCGProfile() {
1614 ELFDumperStyle->printCGProfile(Obj);
1615}
1616
1617template <class ELFT> void ELFDumper<ELFT>::printNotes() {
1618 ELFDumperStyle->printNotes(Obj);
1619}
1620
1621template <class ELFT> void ELFDumper<ELFT>::printELFLinkerOptions() {
1622 ELFDumperStyle->printELFLinkerOptions(Obj);
1623}
1624
1625static const char *getTypeString(unsigned Arch, uint64_t Type) {
1626#define DYNAMIC_TAG(n, v)
1627 switch (Arch) {
1628 case EM_HEXAGON:
1629 switch (Type) {
1630#define HEXAGON_DYNAMIC_TAG(name, value) \
1631 case DT_##name: \
1632 return #name;
1633#include "llvm/BinaryFormat/DynamicTags.def"
1634#undef HEXAGON_DYNAMIC_TAG
1635 }
1636
1637 case EM_MIPS:
1638 switch (Type) {
1639#define MIPS_DYNAMIC_TAG(name, value) \
1640 case DT_##name: \
1641 return #name;
1642#include "llvm/BinaryFormat/DynamicTags.def"
1643#undef MIPS_DYNAMIC_TAG
1644 }
1645
1646 case EM_PPC64:
1647 switch(Type) {
1648#define PPC64_DYNAMIC_TAG(name, value) \
1649 case DT_##name: \
1650 return #name;
1651#include "llvm/BinaryFormat/DynamicTags.def"
1652#undef PPC64_DYNAMIC_TAG
1653 }
1654 }
1655#undef DYNAMIC_TAG
1656 switch (Type) {
1657// Now handle all dynamic tags except the architecture specific ones
1658#define MIPS_DYNAMIC_TAG(name, value)
1659#define HEXAGON_DYNAMIC_TAG(name, value)
1660#define PPC64_DYNAMIC_TAG(name, value)
1661// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
1662#define DYNAMIC_TAG_MARKER(name, value)
1663#define DYNAMIC_TAG(name, value) \
1664 case DT_##name: \
1665 return #name;
1666#include "llvm/BinaryFormat/DynamicTags.def"
1667#undef DYNAMIC_TAG
1668#undef MIPS_DYNAMIC_TAG
1669#undef HEXAGON_DYNAMIC_TAG
1670#undef PPC64_DYNAMIC_TAG
1671#undef DYNAMIC_TAG_MARKER
1672 default: return "unknown";
1673 }
1674}
1675
1676#define LLVM_READOBJ_DT_FLAG_ENT(prefix, enum) \
1677 { #enum, prefix##_##enum }
1678
1679static const EnumEntry<unsigned> ElfDynamicDTFlags[] = {
1680 LLVM_READOBJ_DT_FLAG_ENT(DF, ORIGIN),
1681 LLVM_READOBJ_DT_FLAG_ENT(DF, SYMBOLIC),
1682 LLVM_READOBJ_DT_FLAG_ENT(DF, TEXTREL),
1683 LLVM_READOBJ_DT_FLAG_ENT(DF, BIND_NOW),
1684 LLVM_READOBJ_DT_FLAG_ENT(DF, STATIC_TLS)
1685};
1686
1687static const EnumEntry<unsigned> ElfDynamicDTFlags1[] = {
1688 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOW),
1689 LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAL),
1690 LLVM_READOBJ_DT_FLAG_ENT(DF_1, GROUP),
1691 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODELETE),
1692 LLVM_READOBJ_DT_FLAG_ENT(DF_1, LOADFLTR),
1693 LLVM_READOBJ_DT_FLAG_ENT(DF_1, INITFIRST),
1694 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOOPEN),
1695 LLVM_READOBJ_DT_FLAG_ENT(DF_1, ORIGIN),
1696 LLVM_READOBJ_DT_FLAG_ENT(DF_1, DIRECT),
1697 LLVM_READOBJ_DT_FLAG_ENT(DF_1, TRANS),
1698 LLVM_READOBJ_DT_FLAG_ENT(DF_1, INTERPOSE),
1699 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODEFLIB),
1700 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODUMP),
1701 LLVM_READOBJ_DT_FLAG_ENT(DF_1, CONFALT),
1702 LLVM_READOBJ_DT_FLAG_ENT(DF_1, ENDFILTEE),
1703 LLVM_READOBJ_DT_FLAG_ENT(DF_1, DISPRELDNE),
1704 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODIRECT),
1705 LLVM_READOBJ_DT_FLAG_ENT(DF_1, IGNMULDEF),
1706 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOKSYMS),
1707 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOHDR),
1708 LLVM_READOBJ_DT_FLAG_ENT(DF_1, EDITED),
1709 LLVM_READOBJ_DT_FLAG_ENT(DF_1, NORELOC),
1710 LLVM_READOBJ_DT_FLAG_ENT(DF_1, SYMINTPOSE),
1711 LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAUDIT),
1712 LLVM_READOBJ_DT_FLAG_ENT(DF_1, SINGLETON)
1713};
1714
1715static const EnumEntry<unsigned> ElfDynamicDTMipsFlags[] = {
1716 LLVM_READOBJ_DT_FLAG_ENT(RHF, NONE),
1717 LLVM_READOBJ_DT_FLAG_ENT(RHF, QUICKSTART),
1718 LLVM_READOBJ_DT_FLAG_ENT(RHF, NOTPOT),
1719 LLVM_READOBJ_DT_FLAG_ENT(RHS, NO_LIBRARY_REPLACEMENT),
1720 LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_MOVE),
1721 LLVM_READOBJ_DT_FLAG_ENT(RHF, SGI_ONLY),
1722 LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_INIT),
1723 LLVM_READOBJ_DT_FLAG_ENT(RHF, DELTA_C_PLUS_PLUS),
1724 LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_START_INIT),
1725 LLVM_READOBJ_DT_FLAG_ENT(RHF, PIXIE),
1726 LLVM_READOBJ_DT_FLAG_ENT(RHF, DEFAULT_DELAY_LOAD),
1727 LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTART),
1728 LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTARTED),
1729 LLVM_READOBJ_DT_FLAG_ENT(RHF, CORD),
1730 LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_UNRES_UNDEF),
1731 LLVM_READOBJ_DT_FLAG_ENT(RHF, RLD_ORDER_SAFE)
1732};
1733
1734#undef LLVM_READOBJ_DT_FLAG_ENT
1735
1736template <typename T, typename TFlag>
1737void printFlags(T Value, ArrayRef<EnumEntry<TFlag>> Flags, raw_ostream &OS) {
1738 using FlagEntry = EnumEntry<TFlag>;
1739 using FlagVector = SmallVector<FlagEntry, 10>;
1740 FlagVector SetFlags;
1741
1742 for (const auto &Flag : Flags) {
1743 if (Flag.Value == 0)
1744 continue;
1745
1746 if ((Value & Flag.Value) == Flag.Value)
1747 SetFlags.push_back(Flag);
1748 }
1749
1750 for (const auto &Flag : SetFlags) {
1751 OS << Flag.Name << " ";
1752 }
1753}
1754
1755template <class ELFT>
1756StringRef ELFDumper<ELFT>::getDynamicString(uint64_t Value) const {
1757 if (Value >= DynamicStringTable.size())
1758 reportError("Invalid dynamic string table reference");
1759 return StringRef(DynamicStringTable.data() + Value);
1760}
1761
1762static void printLibrary(raw_ostream &OS, const Twine &Tag, const Twine &Name) {
1763 OS << Tag << ": [" << Name << "]";
1764}
1765
1766template <class ELFT>
1767void ELFDumper<ELFT>::printValue(uint64_t Type, uint64_t Value) {
1768 raw_ostream &OS = W.getOStream();
1769 const char* ConvChar = (opts::Output == opts::GNU) ? "0x%" PRIx64"l" "x" : "0x%" PRIX64"l" "X";
1770 switch (Type) {
1771 case DT_PLTREL:
1772 if (Value == DT_REL) {
1773 OS << "REL";
1774 break;
1775 } else if (Value == DT_RELA) {
1776 OS << "RELA";
1777 break;
1778 }
1779 LLVM_FALLTHROUGH[[clang::fallthrough]];
1780 case DT_PLTGOT:
1781 case DT_HASH:
1782 case DT_STRTAB:
1783 case DT_SYMTAB:
1784 case DT_RELA:
1785 case DT_INIT:
1786 case DT_FINI:
1787 case DT_REL:
1788 case DT_JMPREL:
1789 case DT_INIT_ARRAY:
1790 case DT_FINI_ARRAY:
1791 case DT_PREINIT_ARRAY:
1792 case DT_DEBUG:
1793 case DT_VERDEF:
1794 case DT_VERNEED:
1795 case DT_VERSYM:
1796 case DT_GNU_HASH:
1797 case DT_NULL:
1798 case DT_MIPS_BASE_ADDRESS:
1799 case DT_MIPS_GOTSYM:
1800 case DT_MIPS_RLD_MAP:
1801 case DT_MIPS_RLD_MAP_REL:
1802 case DT_MIPS_PLTGOT:
1803 case DT_MIPS_OPTIONS:
1804 OS << format(ConvChar, Value);
1805 break;
1806 case DT_RELACOUNT:
1807 case DT_RELCOUNT:
1808 case DT_VERDEFNUM:
1809 case DT_VERNEEDNUM:
1810 case DT_MIPS_RLD_VERSION:
1811 case DT_MIPS_LOCAL_GOTNO:
1812 case DT_MIPS_SYMTABNO:
1813 case DT_MIPS_UNREFEXTNO:
1814 OS << Value;
1815 break;
1816 case DT_PLTRELSZ:
1817 case DT_RELASZ:
1818 case DT_RELAENT:
1819 case DT_STRSZ:
1820 case DT_SYMENT:
1821 case DT_RELSZ:
1822 case DT_RELENT:
1823 case DT_INIT_ARRAYSZ:
1824 case DT_FINI_ARRAYSZ:
1825 case DT_PREINIT_ARRAYSZ:
1826 case DT_ANDROID_RELSZ:
1827 case DT_ANDROID_RELASZ:
1828 OS << Value << " (bytes)";
1829 break;
1830 case DT_NEEDED:
1831 printLibrary(OS, "Shared library", getDynamicString(Value));
1832 break;
1833 case DT_SONAME:
1834 printLibrary(OS, "Library soname", getDynamicString(Value));
1835 break;
1836 case DT_AUXILIARY:
1837 printLibrary(OS, "Auxiliary library", getDynamicString(Value));
1838 break;
1839 case DT_FILTER:
1840 printLibrary(OS, "Filter library", getDynamicString(Value));
1841 break;
1842 case DT_RPATH:
1843 case DT_RUNPATH:
1844 OS << getDynamicString(Value);
1845 break;
1846 case DT_MIPS_FLAGS:
1847 printFlags(Value, makeArrayRef(ElfDynamicDTMipsFlags), OS);
1848 break;
1849 case DT_FLAGS:
1850 printFlags(Value, makeArrayRef(ElfDynamicDTFlags), OS);
1851 break;
1852 case DT_FLAGS_1:
1853 printFlags(Value, makeArrayRef(ElfDynamicDTFlags1), OS);
1854 break;
1855 default:
1856 OS << format(ConvChar, Value);
1857 break;
1858 }
1859}
1860
1861template<class ELFT>
1862void ELFDumper<ELFT>::printUnwindInfo() {
1863 const unsigned Machine = Obj->getHeader()->e_machine;
1864 if (Machine == EM_386 || Machine == EM_X86_64) {
1865 DwarfCFIEH::PrinterContext<ELFT> Ctx(W, Obj);
1866 return Ctx.printUnwindInformation();
1867 }
1868 W.startLine() << "UnwindInfo not implemented.\n";
1869}
1870
1871namespace {
1872
1873template <> void ELFDumper<ELF32LE>::printUnwindInfo() {
1874 const unsigned Machine = Obj->getHeader()->e_machine;
1875 if (Machine == EM_ARM) {
1876 ARM::EHABI::PrinterContext<ELF32LE> Ctx(W, Obj, DotSymtabSec);
1877 return Ctx.PrintUnwindInformation();
1878 }
1879 W.startLine() << "UnwindInfo not implemented.\n";
1880}
1881
1882} // end anonymous namespace
1883
1884template<class ELFT>
1885void ELFDumper<ELFT>::printDynamicTable() {
1886 auto I = dynamic_table().begin();
1887 auto E = dynamic_table().end();
1888
1889 if (I == E)
1890 return;
1891
1892 --E;
1893 while (I != E && E->getTag() == ELF::DT_NULL)
1894 --E;
1895 if (E->getTag() != ELF::DT_NULL)
1896 ++E;
1897 ++E;
1898
1899 ptrdiff_t Total = std::distance(I, E);
1900 if (Total == 0)
1901 return;
1902
1903 raw_ostream &OS = W.getOStream();
1904 W.startLine() << "DynamicSection [ (" << Total << " entries)\n";
1905
1906 bool Is64 = ELFT::Is64Bits;
1907
1908 W.startLine()
1909 << " Tag" << (Is64 ? " " : " ") << "Type"
1910 << " " << "Name/Value\n";
1911 while (I != E) {
1912 const Elf_Dyn &Entry = *I;
1913 uintX_t Tag = Entry.getTag();
1914 ++I;
1915 W.startLine() << " " << format_hex(Tag, Is64 ? 18 : 10, opts::Output != opts::GNU) << " "
1916 << format("%-21s", getTypeString(Obj->getHeader()->e_machine, Tag));
1917 printValue(Tag, Entry.getVal());
1918 OS << "\n";
1919 }
1920
1921 W.startLine() << "]\n";
1922}
1923
1924template<class ELFT>
1925void ELFDumper<ELFT>::printNeededLibraries() {
1926 ListScope D(W, "NeededLibraries");
1927
1928 using LibsTy = std::vector<StringRef>;
1929 LibsTy Libs;
1930
1931 for (const auto &Entry : dynamic_table())
1932 if (Entry.d_tag == ELF::DT_NEEDED)
1933 Libs.push_back(getDynamicString(Entry.d_un.d_val));
1934
1935 std::stable_sort(Libs.begin(), Libs.end());
1936
1937 for (const auto &L : Libs)
1938 W.startLine() << L << "\n";
1939}
1940
1941
1942template <typename ELFT>
1943void ELFDumper<ELFT>::printHashTable() {
1944 DictScope D(W, "HashTable");
1945 if (!HashTable)
1946 return;
1947 W.printNumber("Num Buckets", HashTable->nbucket);
1948 W.printNumber("Num Chains", HashTable->nchain);
1949 W.printList("Buckets", HashTable->buckets());
1950 W.printList("Chains", HashTable->chains());
1951}
1952
1953template <typename ELFT>
1954void ELFDumper<ELFT>::printGnuHashTable() {
1955 DictScope D(W, "GnuHashTable");
1956 if (!GnuHashTable)
1957 return;
1958 W.printNumber("Num Buckets", GnuHashTable->nbuckets);
1959 W.printNumber("First Hashed Symbol Index", GnuHashTable->symndx);
1960 W.printNumber("Num Mask Words", GnuHashTable->maskwords);
1961 W.printNumber("Shift Count", GnuHashTable->shift2);
1962 W.printHexList("Bloom Filter", GnuHashTable->filter());
1963 W.printList("Buckets", GnuHashTable->buckets());
1964 Elf_Sym_Range Syms = dynamic_symbols();
1965 unsigned NumSyms = std::distance(Syms.begin(), Syms.end());
1966 if (!NumSyms)
1967 reportError("No dynamic symbol section");
1968 W.printHexList("Values", GnuHashTable->values(NumSyms));
1969}
1970
1971template <typename ELFT> void ELFDumper<ELFT>::printLoadName() {
1972 W.printString("LoadName", SOName);
1973}
1974
1975template <class ELFT>
1976void ELFDumper<ELFT>::printAttributes() {
1977 W.startLine() << "Attributes not implemented.\n";
1978}
1979
1980namespace {
1981
1982template <> void ELFDumper<ELF32LE>::printAttributes() {
1983 if (Obj->getHeader()->e_machine != EM_ARM) {
1984 W.startLine() << "Attributes not implemented.\n";
1985 return;
1986 }
1987
1988 DictScope BA(W, "BuildAttributes");
1989 for (const ELFO::Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
1990 if (Sec.sh_type != ELF::SHT_ARM_ATTRIBUTES)
1991 continue;
1992
1993 ArrayRef<uint8_t> Contents = unwrapOrError(Obj->getSectionContents(&Sec));
1994 if (Contents[0] != ARMBuildAttrs::Format_Version) {
1995 errs() << "unrecognised FormatVersion: 0x"
1996 << Twine::utohexstr(Contents[0]) << '\n';
1997 continue;
1998 }
1999
2000 W.printHex("FormatVersion", Contents[0]);
2001 if (Contents.size() == 1)
2002 continue;
2003
2004 ARMAttributeParser(&W).Parse(Contents, true);
2005 }
2006}
2007
2008template <class ELFT> class MipsGOTParser {
2009public:
2010 TYPEDEF_ELF_TYPES(ELFT)using ELFO = ELFFile<ELFT>; using Elf_Addr = typename ELFT
::Addr; using Elf_Shdr = typename ELFT::Shdr; using Elf_Sym =
typename ELFT::Sym; using Elf_Dyn = typename ELFT::Dyn; using
Elf_Dyn_Range = typename ELFT::DynRange; using Elf_Rel = typename
ELFT::Rel; using Elf_Rela = typename ELFT::Rela; using Elf_Relr
= typename ELFT::Relr; using Elf_Rel_Range = typename ELFT::
RelRange; using Elf_Rela_Range = typename ELFT::RelaRange; using
Elf_Relr_Range = typename ELFT::RelrRange; using Elf_Phdr = typename
ELFT::Phdr; using Elf_Half = typename ELFT::Half; using Elf_Ehdr
= typename ELFT::Ehdr; using Elf_Word = typename ELFT::Word;
using Elf_Hash = typename ELFT::Hash; using Elf_GnuHash = typename
ELFT::GnuHash; using Elf_Note = typename ELFT::Note; using Elf_Sym_Range
= typename ELFT::SymRange; using Elf_Versym = typename ELFT::
Versym; using Elf_Verneed = typename ELFT::Verneed; using Elf_Vernaux
= typename ELFT::Vernaux; using Elf_Verdef = typename ELFT::
Verdef; using Elf_Verdaux = typename ELFT::Verdaux; using Elf_CGProfile
= typename ELFT::CGProfile; using uintX_t = typename ELFT::uint
;
2011 using Entry = typename ELFO::Elf_Addr;
2012 using Entries = ArrayRef<Entry>;
2013
2014 const bool IsStatic;
2015 const ELFO * const Obj;
2016
2017 MipsGOTParser(const ELFO *Obj, Elf_Dyn_Range DynTable, Elf_Sym_Range DynSyms);
2018
2019 bool hasGot() const { return !GotEntries.empty(); }
2020 bool hasPlt() const { return !PltEntries.empty(); }
2021
2022 uint64_t getGp() const;
2023
2024 const Entry *getGotLazyResolver() const;
2025 const Entry *getGotModulePointer() const;
2026 const Entry *getPltLazyResolver() const;
2027 const Entry *getPltModulePointer() const;
2028
2029 Entries getLocalEntries() const;
2030 Entries getGlobalEntries() const;
2031 Entries getOtherEntries() const;
2032 Entries getPltEntries() const;
2033
2034 uint64_t getGotAddress(const Entry * E) const;
2035 int64_t getGotOffset(const Entry * E) const;
2036 const Elf_Sym *getGotSym(const Entry *E) const;
2037
2038 uint64_t getPltAddress(const Entry * E) const;
2039 const Elf_Sym *getPltSym(const Entry *E) const;
2040
2041 StringRef getPltStrTable() const { return PltStrTable; }
2042
2043private:
2044 const Elf_Shdr *GotSec;
2045 size_t LocalNum;
2046 size_t GlobalNum;
2047
2048 const Elf_Shdr *PltSec;
2049 const Elf_Shdr *PltRelSec;
2050 const Elf_Shdr *PltSymTable;
2051 Elf_Sym_Range GotDynSyms;
2052 StringRef PltStrTable;
2053
2054 Entries GotEntries;
2055 Entries PltEntries;
2056};
2057
2058} // end anonymous namespace
2059
2060template <class ELFT>
2061MipsGOTParser<ELFT>::MipsGOTParser(const ELFO *Obj, Elf_Dyn_Range DynTable,
2062 Elf_Sym_Range DynSyms)
2063 : IsStatic(DynTable.empty()), Obj(Obj), GotSec(nullptr), LocalNum(0),
2064 GlobalNum(0), PltSec(nullptr), PltRelSec(nullptr), PltSymTable(nullptr) {
2065 // See "Global Offset Table" in Chapter 5 in the following document
2066 // for detailed GOT description.
2067 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
2068
2069 // Find static GOT secton.
2070 if (IsStatic) {
2071 GotSec = findSectionByName(*Obj, ".got");
2072 if (!GotSec)
2073 reportError("Cannot find .got section");
2074
2075 ArrayRef<uint8_t> Content = unwrapOrError(Obj->getSectionContents(GotSec));
2076 GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()),
2077 Content.size() / sizeof(Entry));
2078 LocalNum = GotEntries.size();
2079 return;
2080 }
2081
2082 // Lookup dynamic table tags which define GOT/PLT layouts.
2083 Optional<uint64_t> DtPltGot;
2084 Optional<uint64_t> DtLocalGotNum;
2085 Optional<uint64_t> DtGotSym;
2086 Optional<uint64_t> DtMipsPltGot;
2087 Optional<uint64_t> DtJmpRel;
2088 for (const auto &Entry : DynTable) {
2089 switch (Entry.getTag()) {
2090 case ELF::DT_PLTGOT:
2091 DtPltGot = Entry.getVal();
2092 break;
2093 case ELF::DT_MIPS_LOCAL_GOTNO:
2094 DtLocalGotNum = Entry.getVal();
2095 break;
2096 case ELF::DT_MIPS_GOTSYM:
2097 DtGotSym = Entry.getVal();
2098 break;
2099 case ELF::DT_MIPS_PLTGOT:
2100 DtMipsPltGot = Entry.getVal();
2101 break;
2102 case ELF::DT_JMPREL:
2103 DtJmpRel = Entry.getVal();
2104 break;
2105 }
2106 }
2107
2108 // Find dynamic GOT section.
2109 if (DtPltGot || DtLocalGotNum || DtGotSym) {
2110 if (!DtPltGot)
2111 report_fatal_error("Cannot find PLTGOT dynamic table tag.");
2112 if (!DtLocalGotNum)
2113 report_fatal_error("Cannot find MIPS_LOCAL_GOTNO dynamic table tag.");
2114 if (!DtGotSym)
2115 report_fatal_error("Cannot find MIPS_GOTSYM dynamic table tag.");
2116
2117 size_t DynSymTotal = DynSyms.size();
2118 if (*DtGotSym > DynSymTotal)
2119 reportError("MIPS_GOTSYM exceeds a number of dynamic symbols");
2120
2121 GotSec = findNotEmptySectionByAddress(Obj, *DtPltGot);
2122 if (!GotSec)
2123 reportError("There is no not empty GOT section at 0x" +
2124 Twine::utohexstr(*DtPltGot));
2125
2126 LocalNum = *DtLocalGotNum;
2127 GlobalNum = DynSymTotal - *DtGotSym;
2128
2129 ArrayRef<uint8_t> Content = unwrapOrError(Obj->getSectionContents(GotSec));
2130 GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()),
2131 Content.size() / sizeof(Entry));
2132 GotDynSyms = DynSyms.drop_front(*DtGotSym);
2133 }
2134
2135 // Find PLT section.
2136 if (DtMipsPltGot || DtJmpRel) {
2137 if (!DtMipsPltGot)
2138 report_fatal_error("Cannot find MIPS_PLTGOT dynamic table tag.");
2139 if (!DtJmpRel)
2140 report_fatal_error("Cannot find JMPREL dynamic table tag.");
2141
2142 PltSec = findNotEmptySectionByAddress(Obj, *DtMipsPltGot);
2143 if (!PltSec)
2144 report_fatal_error("There is no not empty PLTGOT section at 0x " +
2145 Twine::utohexstr(*DtMipsPltGot));
2146
2147 PltRelSec = findNotEmptySectionByAddress(Obj, *DtJmpRel);
2148 if (!PltRelSec)
2149 report_fatal_error("There is no not empty RELPLT section at 0x" +
2150 Twine::utohexstr(*DtJmpRel));
2151
2152 ArrayRef<uint8_t> PltContent =
2153 unwrapOrError(Obj->getSectionContents(PltSec));
2154 PltEntries = Entries(reinterpret_cast<const Entry *>(PltContent.data()),
2155 PltContent.size() / sizeof(Entry));
2156
2157 PltSymTable = unwrapOrError(Obj->getSection(PltRelSec->sh_link));
2158 PltStrTable = unwrapOrError(Obj->getStringTableForSymtab(*PltSymTable));
2159 }
2160}
2161
2162template <class ELFT> uint64_t MipsGOTParser<ELFT>::getGp() const {
2163 return GotSec->sh_addr + 0x7ff0;
2164}
2165
2166template <class ELFT>
2167const typename MipsGOTParser<ELFT>::Entry *
2168MipsGOTParser<ELFT>::getGotLazyResolver() const {
2169 return LocalNum > 0 ? &GotEntries[0] : nullptr;
2170}
2171
2172template <class ELFT>
2173const typename MipsGOTParser<ELFT>::Entry *
2174MipsGOTParser<ELFT>::getGotModulePointer() const {
2175 if (LocalNum < 2)
2176 return nullptr;
2177 const Entry &E = GotEntries[1];
2178 if ((E >> (sizeof(Entry) * 8 - 1)) == 0)
2179 return nullptr;
2180 return &E;
2181}
2182
2183template <class ELFT>
2184typename MipsGOTParser<ELFT>::Entries
2185MipsGOTParser<ELFT>::getLocalEntries() const {
2186 size_t Skip = getGotModulePointer() ? 2 : 1;
2187 if (LocalNum - Skip <= 0)
2188 return Entries();
2189 return GotEntries.slice(Skip, LocalNum - Skip);
2190}
2191
2192template <class ELFT>
2193typename MipsGOTParser<ELFT>::Entries
2194MipsGOTParser<ELFT>::getGlobalEntries() const {
2195 if (GlobalNum == 0)
2196 return Entries();
2197 return GotEntries.slice(LocalNum, GlobalNum);
2198}
2199
2200template <class ELFT>
2201typename MipsGOTParser<ELFT>::Entries
2202MipsGOTParser<ELFT>::getOtherEntries() const {
2203 size_t OtherNum = GotEntries.size() - LocalNum - GlobalNum;
2204 if (OtherNum == 0)
2205 return Entries();
2206 return GotEntries.slice(LocalNum + GlobalNum, OtherNum);
2207}
2208
2209template <class ELFT>
2210uint64_t MipsGOTParser<ELFT>::getGotAddress(const Entry *E) const {
2211 int64_t Offset = std::distance(GotEntries.data(), E) * sizeof(Entry);
2212 return GotSec->sh_addr + Offset;
2213}
2214
2215template <class ELFT>
2216int64_t MipsGOTParser<ELFT>::getGotOffset(const Entry *E) const {
2217 int64_t Offset = std::distance(GotEntries.data(), E) * sizeof(Entry);
2218 return Offset - 0x7ff0;
2219}
2220
2221template <class ELFT>
2222const typename MipsGOTParser<ELFT>::Elf_Sym *
2223MipsGOTParser<ELFT>::getGotSym(const Entry *E) const {
2224 int64_t Offset = std::distance(GotEntries.data(), E);
2225 return &GotDynSyms[Offset - LocalNum];
2226}
2227
2228template <class ELFT>
2229const typename MipsGOTParser<ELFT>::Entry *
2230MipsGOTParser<ELFT>::getPltLazyResolver() const {
2231 return PltEntries.empty() ? nullptr : &PltEntries[0];
2232}
2233
2234template <class ELFT>
2235const typename MipsGOTParser<ELFT>::Entry *
2236MipsGOTParser<ELFT>::getPltModulePointer() const {
2237 return PltEntries.size() < 2 ? nullptr : &PltEntries[1];
2238}
2239
2240template <class ELFT>
2241typename MipsGOTParser<ELFT>::Entries
2242MipsGOTParser<ELFT>::getPltEntries() const {
2243 if (PltEntries.size() <= 2)
2244 return Entries();
2245 return PltEntries.slice(2, PltEntries.size() - 2);
2246}
2247
2248template <class ELFT>
2249uint64_t MipsGOTParser<ELFT>::getPltAddress(const Entry *E) const {
2250 int64_t Offset = std::distance(PltEntries.data(), E) * sizeof(Entry);
2251 return PltSec->sh_addr + Offset;
2252}
2253
2254template <class ELFT>
2255const typename MipsGOTParser<ELFT>::Elf_Sym *
2256MipsGOTParser<ELFT>::getPltSym(const Entry *E) const {
2257 int64_t Offset = std::distance(getPltEntries().data(), E);
2258 if (PltRelSec->sh_type == ELF::SHT_REL) {
2259 Elf_Rel_Range Rels = unwrapOrError(Obj->rels(PltRelSec));
2260 return unwrapOrError(Obj->getRelocationSymbol(&Rels[Offset], PltSymTable));
2261 } else {
2262 Elf_Rela_Range Rels = unwrapOrError(Obj->relas(PltRelSec));
2263 return unwrapOrError(Obj->getRelocationSymbol(&Rels[Offset], PltSymTable));
2264 }
2265}
2266
2267template <class ELFT> void ELFDumper<ELFT>::printMipsPLTGOT() {
2268 if (Obj->getHeader()->e_machine != EM_MIPS)
2269 reportError("MIPS PLT GOT is available for MIPS targets only");
2270
2271 MipsGOTParser<ELFT> Parser(Obj, dynamic_table(), dynamic_symbols());
2272 if (Parser.hasGot())
2273 ELFDumperStyle->printMipsGOT(Parser);
2274 if (Parser.hasPlt())
2275 ELFDumperStyle->printMipsPLT(Parser);
2276}
2277
2278static const EnumEntry<unsigned> ElfMipsISAExtType[] = {
2279 {"None", Mips::AFL_EXT_NONE},
2280 {"Broadcom SB-1", Mips::AFL_EXT_SB1},
2281 {"Cavium Networks Octeon", Mips::AFL_EXT_OCTEON},
2282 {"Cavium Networks Octeon2", Mips::AFL_EXT_OCTEON2},
2283 {"Cavium Networks OcteonP", Mips::AFL_EXT_OCTEONP},
2284 {"Cavium Networks Octeon3", Mips::AFL_EXT_OCTEON3},
2285 {"LSI R4010", Mips::AFL_EXT_4010},
2286 {"Loongson 2E", Mips::AFL_EXT_LOONGSON_2E},
2287 {"Loongson 2F", Mips::AFL_EXT_LOONGSON_2F},
2288 {"Loongson 3A", Mips::AFL_EXT_LOONGSON_3A},
2289 {"MIPS R4650", Mips::AFL_EXT_4650},
2290 {"MIPS R5900", Mips::AFL_EXT_5900},
2291 {"MIPS R10000", Mips::AFL_EXT_10000},
2292 {"NEC VR4100", Mips::AFL_EXT_4100},
2293 {"NEC VR4111/VR4181", Mips::AFL_EXT_4111},
2294 {"NEC VR4120", Mips::AFL_EXT_4120},
2295 {"NEC VR5400", Mips::AFL_EXT_5400},
2296 {"NEC VR5500", Mips::AFL_EXT_5500},
2297 {"RMI Xlr", Mips::AFL_EXT_XLR},
2298 {"Toshiba R3900", Mips::AFL_EXT_3900}
2299};
2300
2301static const EnumEntry<unsigned> ElfMipsASEFlags[] = {
2302 {"DSP", Mips::AFL_ASE_DSP},
2303 {"DSPR2", Mips::AFL_ASE_DSPR2},
2304 {"Enhanced VA Scheme", Mips::AFL_ASE_EVA},
2305 {"MCU", Mips::AFL_ASE_MCU},
2306 {"MDMX", Mips::AFL_ASE_MDMX},
2307 {"MIPS-3D", Mips::AFL_ASE_MIPS3D},
2308 {"MT", Mips::AFL_ASE_MT},
2309 {"SmartMIPS", Mips::AFL_ASE_SMARTMIPS},
2310 {"VZ", Mips::AFL_ASE_VIRT},
2311 {"MSA", Mips::AFL_ASE_MSA},
2312 {"MIPS16", Mips::AFL_ASE_MIPS16},
2313 {"microMIPS", Mips::AFL_ASE_MICROMIPS},
2314 {"XPA", Mips::AFL_ASE_XPA},
2315 {"CRC", Mips::AFL_ASE_CRC},
2316 {"GINV", Mips::AFL_ASE_GINV},
2317};
2318
2319static const EnumEntry<unsigned> ElfMipsFpABIType[] = {
2320 {"Hard or soft float", Mips::Val_GNU_MIPS_ABI_FP_ANY},
2321 {"Hard float (double precision)", Mips::Val_GNU_MIPS_ABI_FP_DOUBLE},
2322 {"Hard float (single precision)", Mips::Val_GNU_MIPS_ABI_FP_SINGLE},
2323 {"Soft float", Mips::Val_GNU_MIPS_ABI_FP_SOFT},
2324 {"Hard float (MIPS32r2 64-bit FPU 12 callee-saved)",
2325 Mips::Val_GNU_MIPS_ABI_FP_OLD_64},
2326 {"Hard float (32-bit CPU, Any FPU)", Mips::Val_GNU_MIPS_ABI_FP_XX},
2327 {"Hard float (32-bit CPU, 64-bit FPU)", Mips::Val_GNU_MIPS_ABI_FP_64},
2328 {"Hard float compat (32-bit CPU, 64-bit FPU)",
2329 Mips::Val_GNU_MIPS_ABI_FP_64A}
2330};
2331
2332static const EnumEntry<unsigned> ElfMipsFlags1[] {
2333 {"ODDSPREG", Mips::AFL_FLAGS1_ODDSPREG},
2334};
2335
2336static int getMipsRegisterSize(uint8_t Flag) {
2337 switch (Flag) {
2338 case Mips::AFL_REG_NONE:
2339 return 0;
2340 case Mips::AFL_REG_32:
2341 return 32;
2342 case Mips::AFL_REG_64:
2343 return 64;
2344 case Mips::AFL_REG_128:
2345 return 128;
2346 default:
2347 return -1;
2348 }
2349}
2350
2351template <class ELFT> void ELFDumper<ELFT>::printMipsABIFlags() {
2352 const Elf_Shdr *Shdr = findSectionByName(*Obj, ".MIPS.abiflags");
2353 if (!Shdr) {
2354 W.startLine() << "There is no .MIPS.abiflags section in the file.\n";
2355 return;
2356 }
2357 ArrayRef<uint8_t> Sec = unwrapOrError(Obj->getSectionContents(Shdr));
2358 if (Sec.size() != sizeof(Elf_Mips_ABIFlags<ELFT>)) {
2359 W.startLine() << "The .MIPS.abiflags section has a wrong size.\n";
2360 return;
2361 }
2362
2363 auto *Flags = reinterpret_cast<const Elf_Mips_ABIFlags<ELFT> *>(Sec.data());
2364
2365 raw_ostream &OS = W.getOStream();
2366 DictScope GS(W, "MIPS ABI Flags");
2367
2368 W.printNumber("Version", Flags->version);
2369 W.startLine() << "ISA: ";
2370 if (Flags->isa_rev <= 1)
2371 OS << format("MIPS%u", Flags->isa_level);
2372 else
2373 OS << format("MIPS%ur%u", Flags->isa_level, Flags->isa_rev);
2374 OS << "\n";
2375 W.printEnum("ISA Extension", Flags->isa_ext, makeArrayRef(ElfMipsISAExtType));
2376 W.printFlags("ASEs", Flags->ases, makeArrayRef(ElfMipsASEFlags));
2377 W.printEnum("FP ABI", Flags->fp_abi, makeArrayRef(ElfMipsFpABIType));
2378 W.printNumber("GPR size", getMipsRegisterSize(Flags->gpr_size));
2379 W.printNumber("CPR1 size", getMipsRegisterSize(Flags->cpr1_size));
2380 W.printNumber("CPR2 size", getMipsRegisterSize(Flags->cpr2_size));
2381 W.printFlags("Flags 1", Flags->flags1, makeArrayRef(ElfMipsFlags1));
2382 W.printHex("Flags 2", Flags->flags2);
2383}
2384
2385template <class ELFT>
2386static void printMipsReginfoData(ScopedPrinter &W,
2387 const Elf_Mips_RegInfo<ELFT> &Reginfo) {
2388 W.printHex("GP", Reginfo.ri_gp_value);
2389 W.printHex("General Mask", Reginfo.ri_gprmask);
2390 W.printHex("Co-Proc Mask0", Reginfo.ri_cprmask[0]);
2391 W.printHex("Co-Proc Mask1", Reginfo.ri_cprmask[1]);
2392 W.printHex("Co-Proc Mask2", Reginfo.ri_cprmask[2]);
2393 W.printHex("Co-Proc Mask3", Reginfo.ri_cprmask[3]);
2394}
2395
2396template <class ELFT> void ELFDumper<ELFT>::printMipsReginfo() {
2397 const Elf_Shdr *Shdr = findSectionByName(*Obj, ".reginfo");
2398 if (!Shdr) {
2399 W.startLine() << "There is no .reginfo section in the file.\n";
2400 return;
2401 }
2402 ArrayRef<uint8_t> Sec = unwrapOrError(Obj->getSectionContents(Shdr));
2403 if (Sec.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) {
2404 W.startLine() << "The .reginfo section has a wrong size.\n";
2405 return;
2406 }
2407
2408 DictScope GS(W, "MIPS RegInfo");
2409 auto *Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(Sec.data());
2410 printMipsReginfoData(W, *Reginfo);
2411}
2412
2413template <class ELFT> void ELFDumper<ELFT>::printMipsOptions() {
2414 const Elf_Shdr *Shdr = findSectionByName(*Obj, ".MIPS.options");
2415 if (!Shdr) {
2416 W.startLine() << "There is no .MIPS.options section in the file.\n";
2417 return;
2418 }
2419
2420 DictScope GS(W, "MIPS Options");
2421
2422 ArrayRef<uint8_t> Sec = unwrapOrError(Obj->getSectionContents(Shdr));
2423 while (!Sec.empty()) {
2424 if (Sec.size() < sizeof(Elf_Mips_Options<ELFT>)) {
2425 W.startLine() << "The .MIPS.options section has a wrong size.\n";
2426 return;
2427 }
2428 auto *O = reinterpret_cast<const Elf_Mips_Options<ELFT> *>(Sec.data());
2429 DictScope GS(W, getElfMipsOptionsOdkType(O->kind));
2430 switch (O->kind) {
2431 case ODK_REGINFO:
2432 printMipsReginfoData(W, O->getRegInfo());
2433 break;
2434 default:
2435 W.startLine() << "Unsupported MIPS options tag.\n";
2436 break;
2437 }
2438 Sec = Sec.slice(O->size);
2439 }
2440}
2441
2442template <class ELFT> void ELFDumper<ELFT>::printStackMap() const {
2443 const Elf_Shdr *StackMapSection = nullptr;
2444 for (const auto &Sec : unwrapOrError(Obj->sections())) {
2445 StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
2446 if (Name == ".llvm_stackmaps") {
2447 StackMapSection = &Sec;
2448 break;
2449 }
2450 }
2451
2452 if (!StackMapSection)
2453 return;
2454
2455 ArrayRef<uint8_t> StackMapContentsArray =
2456 unwrapOrError(Obj->getSectionContents(StackMapSection));
2457
2458 prettyPrintStackMap(
2459 W, StackMapV2Parser<ELFT::TargetEndianness>(StackMapContentsArray));
2460}
2461
2462template <class ELFT> void ELFDumper<ELFT>::printGroupSections() {
2463 ELFDumperStyle->printGroupSections(Obj);
2464}
2465
2466template <class ELFT> void ELFDumper<ELFT>::printAddrsig() {
2467 ELFDumperStyle->printAddrsig(Obj);
2468}
2469
2470static inline void printFields(formatted_raw_ostream &OS, StringRef Str1,
2471 StringRef Str2) {
2472 OS.PadToColumn(2u);
2473 OS << Str1;
2474 OS.PadToColumn(37u);
2475 OS << Str2 << "\n";
2476 OS.flush();
2477}
2478
2479template <class ELFT>
2480static std::string getSectionHeadersNumString(const ELFFile<ELFT> *Obj) {
2481 const typename ELFT::Ehdr *ElfHeader = Obj->getHeader();
2482 if (ElfHeader->e_shnum != 0)
2483 return to_string(ElfHeader->e_shnum);
2484
2485 ArrayRef<typename ELFT::Shdr> Arr = unwrapOrError(Obj->sections());
2486 if (Arr.empty())
2487 return "0";
2488 return "0 (" + to_string(Arr[0].sh_size) + ")";
2489}
2490
2491template <class ELFT>
2492static std::string getSectionHeaderTableIndexString(const ELFFile<ELFT> *Obj) {
2493 const typename ELFT::Ehdr *ElfHeader = Obj->getHeader();
2494 if (ElfHeader->e_shstrndx != SHN_XINDEX)
2495 return to_string(ElfHeader->e_shstrndx);
2496
2497 ArrayRef<typename ELFT::Shdr> Arr = unwrapOrError(Obj->sections());
2498 if (Arr.empty())
2499 return "65535 (corrupt: out of range)";
2500 return to_string(ElfHeader->e_shstrndx) + " (" + to_string(Arr[0].sh_link) + ")";
2501}
2502
2503template <class ELFT> void GNUStyle<ELFT>::printFileHeaders(const ELFO *Obj) {
2504 const Elf_Ehdr *e = Obj->getHeader();
2505 OS << "ELF Header:\n";
2506 OS << " Magic: ";
2507 std::string Str;
2508 for (int i = 0; i < ELF::EI_NIDENT; i++)
2509 OS << format(" %02x", static_cast<int>(e->e_ident[i]));
2510 OS << "\n";
2511 Str = printEnum(e->e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
2512 printFields(OS, "Class:", Str);
2513 Str = printEnum(e->e_ident[ELF::EI_DATA], makeArrayRef(ElfDataEncoding));
2514 printFields(OS, "Data:", Str);
2515 OS.PadToColumn(2u);
2516 OS << "Version:";
2517 OS.PadToColumn(37u);
2518 OS << to_hexString(e->e_ident[ELF::EI_VERSION]);
2519 if (e->e_version == ELF::EV_CURRENT)
2520 OS << " (current)";
2521 OS << "\n";
2522 Str = printEnum(e->e_ident[ELF::EI_OSABI], makeArrayRef(ElfOSABI));
2523 printFields(OS, "OS/ABI:", Str);
2524 Str = "0x" + to_hexString(e->e_ident[ELF::EI_ABIVERSION]);
2525 printFields(OS, "ABI Version:", Str);
2526 Str = printEnum(e->e_type, makeArrayRef(ElfObjectFileType));
2527 printFields(OS, "Type:", Str);
2528 Str = printEnum(e->e_machine, makeArrayRef(ElfMachineType));
2529 printFields(OS, "Machine:", Str);
2530 Str = "0x" + to_hexString(e->e_version);
2531 printFields(OS, "Version:", Str);
2532 Str = "0x" + to_hexString(e->e_entry);
2533 printFields(OS, "Entry point address:", Str);
2534 Str = to_string(e->e_phoff) + " (bytes into file)";
2535 printFields(OS, "Start of program headers:", Str);
2536 Str = to_string(e->e_shoff) + " (bytes into file)";
2537 printFields(OS, "Start of section headers:", Str);
2538 Str = "0x" + to_hexString(e->e_flags);
2539 printFields(OS, "Flags:", Str);
2540 Str = to_string(e->e_ehsize) + " (bytes)";
2541 printFields(OS, "Size of this header:", Str);
2542 Str = to_string(e->e_phentsize) + " (bytes)";
2543 printFields(OS, "Size of program headers:", Str);
2544 Str = to_string(e->e_phnum);
2545 printFields(OS, "Number of program headers:", Str);
2546 Str = to_string(e->e_shentsize) + " (bytes)";
2547 printFields(OS, "Size of section headers:", Str);
2548 Str = getSectionHeadersNumString(Obj);
2549 printFields(OS, "Number of section headers:", Str);
2550 Str = getSectionHeaderTableIndexString(Obj);
2551 printFields(OS, "Section header string table index:", Str);
2552}
2553
2554namespace {
2555struct GroupMember {
2556 StringRef Name;
2557 uint64_t Index;
2558};
2559
2560struct GroupSection {
2561 StringRef Name;
2562 StringRef Signature;
2563 uint64_t ShName;
2564 uint64_t Index;
2565 uint32_t Link;
2566 uint32_t Info;
2567 uint32_t Type;
2568 std::vector<GroupMember> Members;
2569};
2570
2571template <class ELFT>
2572std::vector<GroupSection> getGroups(const ELFFile<ELFT> *Obj) {
2573 using Elf_Shdr = typename ELFT::Shdr;
2574 using Elf_Sym = typename ELFT::Sym;
2575 using Elf_Word = typename ELFT::Word;
2576
2577 std::vector<GroupSection> Ret;
2578 uint64_t I = 0;
2579 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
2580 ++I;
2581 if (Sec.sh_type != ELF::SHT_GROUP)
2582 continue;
2583
2584 const Elf_Shdr *Symtab = unwrapOrError(Obj->getSection(Sec.sh_link));
2585 StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*Symtab));
2586 const Elf_Sym *Sym =
2587 unwrapOrError(Obj->template getEntry<Elf_Sym>(Symtab, Sec.sh_info));
2588 auto Data =
2589 unwrapOrError(Obj->template getSectionContentsAsArray<Elf_Word>(&Sec));
2590
2591 StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
2592 StringRef Signature = StrTable.data() + Sym->st_name;
2593 Ret.push_back({Name,
2594 Signature,
2595 Sec.sh_name,
2596 I - 1,
2597 Sec.sh_link,
2598 Sec.sh_info,
2599 Data[0],
2600 {}});
2601
2602 std::vector<GroupMember> &GM = Ret.back().Members;
2603 for (uint32_t Ndx : Data.slice(1)) {
2604 auto Sec = unwrapOrError(Obj->getSection(Ndx));
2605 const StringRef Name = unwrapOrError(Obj->getSectionName(Sec));
2606 GM.push_back({Name, Ndx});
2607 }
2608 }
2609 return Ret;
2610}
2611
2612DenseMap<uint64_t, const GroupSection *>
2613mapSectionsToGroups(ArrayRef<GroupSection> Groups) {
2614 DenseMap<uint64_t, const GroupSection *> Ret;
2615 for (const GroupSection &G : Groups)
2616 for (const GroupMember &GM : G.Members)
2617 Ret.insert({GM.Index, &G});
2618 return Ret;
2619}
2620
2621} // namespace
2622
2623template <class ELFT> void GNUStyle<ELFT>::printGroupSections(const ELFO *Obj) {
2624 std::vector<GroupSection> V = getGroups<ELFT>(Obj);
2625 DenseMap<uint64_t, const GroupSection *> Map = mapSectionsToGroups(V);
2626 for (const GroupSection &G : V) {
2627 OS << "\n"
2628 << getGroupType(G.Type) << " group section ["
2629 << format_decimal(G.Index, 5) << "] `" << G.Name << "' [" << G.Signature
2630 << "] contains " << G.Members.size() << " sections:\n"
2631 << " [Index] Name\n";
2632 for (const GroupMember &GM : G.Members) {
2633 const GroupSection *MainGroup = Map[GM.Index];
2634 if (MainGroup != &G) {
2635 OS.flush();
2636 errs() << "Error: section [" << format_decimal(GM.Index, 5)
2637 << "] in group section [" << format_decimal(G.Index, 5)
2638 << "] already in group section ["
2639 << format_decimal(MainGroup->Index, 5) << "]";
2640 errs().flush();
2641 continue;
2642 }
2643 OS << " [" << format_decimal(GM.Index, 5) << "] " << GM.Name << "\n";
2644 }
2645 }
2646
2647 if (V.empty())
2648 OS << "There are no section groups in this file.\n";
2649}
2650
2651template <class ELFT>
2652void GNUStyle<ELFT>::printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab,
2653 const Elf_Rela &R, bool IsRela) {
2654 std::string Offset, Info, Addend, Value;
2655 SmallString<32> RelocName;
2656 StringRef TargetName;
2657 const Elf_Sym *Sym = nullptr;
2658 unsigned Width = ELFT::Is64Bits ? 16 : 8;
2659 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
2660
2661 // First two fields are bit width dependent. The rest of them are after are
2662 // fixed width.
2663 Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias};
2664 Obj->getRelocationTypeName(R.getType(Obj->isMips64EL()), RelocName);
2665 Sym = unwrapOrError(Obj->getRelocationSymbol(&R, SymTab));
2666 if (Sym && Sym->getType() == ELF::STT_SECTION) {
2667 const Elf_Shdr *Sec = unwrapOrError(
2668 Obj->getSection(Sym, SymTab, this->dumper()->getShndxTable()));
2669 TargetName = unwrapOrError(Obj->getSectionName(Sec));
2670 } else if (Sym) {
2671 StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*SymTab));
2672 TargetName = unwrapOrError(Sym->getName(StrTable));
2673 }
2674
2675 if (Sym && IsRela) {
2676 if (R.r_addend < 0)
2677 Addend = " - ";
2678 else
2679 Addend = " + ";
2680 }
2681
2682 Offset = to_string(format_hex_no_prefix(R.r_offset, Width));
2683 Info = to_string(format_hex_no_prefix(R.r_info, Width));
2684
2685 int64_t RelAddend = R.r_addend;
2686 if (IsRela)
2687 Addend += to_hexString(std::abs(RelAddend), false);
2688
2689 if (Sym)
2690 Value = to_string(format_hex_no_prefix(Sym->getValue(), Width));
2691
2692 Fields[0].Str = Offset;
2693 Fields[1].Str = Info;
2694 Fields[2].Str = RelocName;
2695 Fields[3].Str = Value;
2696 Fields[4].Str = TargetName;
2697 for (auto &field : Fields)
2698 printField(field);
2699 OS << Addend;
2700 OS << "\n";
2701}
2702
2703template <class ELFT> void GNUStyle<ELFT>::printRelocHeader(unsigned SType) {
2704 bool IsRela = SType == ELF::SHT_RELA || SType == ELF::SHT_ANDROID_RELA;
2705 bool IsRelr = SType == ELF::SHT_RELR || SType == ELF::SHT_ANDROID_RELR;
2706 if (ELFT::Is64Bits)
2707 OS << " ";
2708 else
2709 OS << " ";
2710 if (IsRelr && opts::RawRelr)
2711 OS << "Data ";
2712 else
2713 OS << "Offset";
2714 if (ELFT::Is64Bits)
2715 OS << " Info Type"
2716 << " Symbol's Value Symbol's Name";
2717 else
2718 OS << " Info Type Sym. Value Symbol's Name";
2719 if (IsRela)
2720 OS << " + Addend";
2721 OS << "\n";
2722}
2723
2724template <class ELFT> void GNUStyle<ELFT>::printRelocations(const ELFO *Obj) {
2725 bool HasRelocSections = false;
2726 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
2727 if (Sec.sh_type != ELF::SHT_REL &&
2728 Sec.sh_type != ELF::SHT_RELA &&
2729 Sec.sh_type != ELF::SHT_RELR &&
2730 Sec.sh_type != ELF::SHT_ANDROID_REL &&
2731 Sec.sh_type != ELF::SHT_ANDROID_RELA &&
2732 Sec.sh_type != ELF::SHT_ANDROID_RELR)
2733 continue;
2734 HasRelocSections = true;
2735 StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
2736 unsigned Entries = Sec.getEntityCount();
2737 std::vector<Elf_Rela> AndroidRelas;
2738 if (Sec.sh_type == ELF::SHT_ANDROID_REL ||
2739 Sec.sh_type == ELF::SHT_ANDROID_RELA) {
2740 // Android's packed relocation section needs to be unpacked first
2741 // to get the actual number of entries.
2742 AndroidRelas = unwrapOrError(Obj->android_relas(&Sec));
2743 Entries = AndroidRelas.size();
2744 }
2745 std::vector<Elf_Rela> RelrRelas;
2746 if (!opts::RawRelr && (Sec.sh_type == ELF::SHT_RELR ||
2747 Sec.sh_type == ELF::SHT_ANDROID_RELR)) {
2748 // .relr.dyn relative relocation section needs to be unpacked first
2749 // to get the actual number of entries.
2750 Elf_Relr_Range Relrs = unwrapOrError(Obj->relrs(&Sec));
2751 RelrRelas = unwrapOrError(Obj->decode_relrs(Relrs));
2752 Entries = RelrRelas.size();
2753 }
2754 uintX_t Offset = Sec.sh_offset;
2755 OS << "\nRelocation section '" << Name << "' at offset 0x"
2756 << to_hexString(Offset, false) << " contains " << Entries
2757 << " entries:\n";
2758 printRelocHeader(Sec.sh_type);
2759 const Elf_Shdr *SymTab = unwrapOrError(Obj->getSection(Sec.sh_link));
2760 switch (Sec.sh_type) {
2761 case ELF::SHT_REL:
2762 for (const auto &R : unwrapOrError(Obj->rels(&Sec))) {
2763 Elf_Rela Rela;
2764 Rela.r_offset = R.r_offset;
2765 Rela.r_info = R.r_info;
2766 Rela.r_addend = 0;
2767 printRelocation(Obj, SymTab, Rela, false);
2768 }
2769 break;
2770 case ELF::SHT_RELA:
2771 for (const auto &R : unwrapOrError(Obj->relas(&Sec)))
2772 printRelocation(Obj, SymTab, R, true);
2773 break;
2774 case ELF::SHT_RELR:
2775 case ELF::SHT_ANDROID_RELR:
2776 if (opts::RawRelr)
2777 for (const auto &R : unwrapOrError(Obj->relrs(&Sec)))
2778 OS << to_string(format_hex_no_prefix(R, ELFT::Is64Bits ? 16 : 8))
2779 << "\n";
2780 else
2781 for (const auto &R : RelrRelas)
2782 printRelocation(Obj, SymTab, R, false);
2783 break;
2784 case ELF::SHT_ANDROID_REL:
2785 case ELF::SHT_ANDROID_RELA:
2786 for (const auto &R : AndroidRelas)
2787 printRelocation(Obj, SymTab, R, Sec.sh_type == ELF::SHT_ANDROID_RELA);
2788 break;
2789 }
2790 }
2791 if (!HasRelocSections)
2792 OS << "\nThere are no relocations in this file.\n";
2793}
2794
2795std::string getSectionTypeString(unsigned Arch, unsigned Type) {
2796 using namespace ELF;
2797
2798 switch (Arch) {
2799 case EM_ARM:
2800 switch (Type) {
2801 case SHT_ARM_EXIDX:
2802 return "ARM_EXIDX";
2803 case SHT_ARM_PREEMPTMAP:
2804 return "ARM_PREEMPTMAP";
2805 case SHT_ARM_ATTRIBUTES:
2806 return "ARM_ATTRIBUTES";
2807 case SHT_ARM_DEBUGOVERLAY:
2808 return "ARM_DEBUGOVERLAY";
2809 case SHT_ARM_OVERLAYSECTION:
2810 return "ARM_OVERLAYSECTION";
2811 }
2812 case EM_X86_64:
2813 switch (Type) {
2814 case SHT_X86_64_UNWIND:
2815 return "X86_64_UNWIND";
2816 }
2817 case EM_MIPS:
2818 case EM_MIPS_RS3_LE:
2819 switch (Type) {
2820 case SHT_MIPS_REGINFO:
2821 return "MIPS_REGINFO";
2822 case SHT_MIPS_OPTIONS:
2823 return "MIPS_OPTIONS";
2824 case SHT_MIPS_ABIFLAGS:
2825 return "MIPS_ABIFLAGS";
2826 case SHT_MIPS_DWARF:
2827 return "SHT_MIPS_DWARF";
2828 }
2829 }
2830 switch (Type) {
2831 case SHT_NULL:
2832 return "NULL";
2833 case SHT_PROGBITS:
2834 return "PROGBITS";
2835 case SHT_SYMTAB:
2836 return "SYMTAB";
2837 case SHT_STRTAB:
2838 return "STRTAB";
2839 case SHT_RELA:
2840 return "RELA";
2841 case SHT_HASH:
2842 return "HASH";
2843 case SHT_DYNAMIC:
2844 return "DYNAMIC";
2845 case SHT_NOTE:
2846 return "NOTE";
2847 case SHT_NOBITS:
2848 return "NOBITS";
2849 case SHT_REL:
2850 return "REL";
2851 case SHT_SHLIB:
2852 return "SHLIB";
2853 case SHT_DYNSYM:
2854 return "DYNSYM";
2855 case SHT_INIT_ARRAY:
2856 return "INIT_ARRAY";
2857 case SHT_FINI_ARRAY:
2858 return "FINI_ARRAY";
2859 case SHT_PREINIT_ARRAY:
2860 return "PREINIT_ARRAY";
2861 case SHT_GROUP:
2862 return "GROUP";
2863 case SHT_SYMTAB_SHNDX:
2864 return "SYMTAB SECTION INDICES";
2865 case SHT_RELR:
2866 case SHT_ANDROID_RELR:
2867 return "RELR";
2868 case SHT_LLVM_ODRTAB:
2869 return "LLVM_ODRTAB";
2870 case SHT_LLVM_LINKER_OPTIONS:
2871 return "LLVM_LINKER_OPTIONS";
2872 case SHT_LLVM_CALL_GRAPH_PROFILE:
2873 return "LLVM_CALL_GRAPH_PROFILE";
2874 case SHT_LLVM_ADDRSIG:
2875 return "LLVM_ADDRSIG";
2876 // FIXME: Parse processor specific GNU attributes
2877 case SHT_GNU_ATTRIBUTES:
2878 return "ATTRIBUTES";
2879 case SHT_GNU_HASH:
2880 return "GNU_HASH";
2881 case SHT_GNU_verdef:
2882 return "VERDEF";
2883 case SHT_GNU_verneed:
2884 return "VERNEED";
2885 case SHT_GNU_versym:
2886 return "VERSYM";
2887 default:
2888 return "";
2889 }
2890 return "";
2891}
2892
2893template <class ELFT> void GNUStyle<ELFT>::printSections(const ELFO *Obj) {
2894 size_t SectionIndex = 0;
2895 std::string Number, Type, Size, Address, Offset, Flags, Link, Info, EntrySize,
2896 Alignment;
2897 unsigned Bias;
2898 unsigned Width;
2899
2900 if (ELFT::Is64Bits) {
2901 Bias = 0;
2902 Width = 16;
2903 } else {
2904 Bias = 8;
2905 Width = 8;
2906 }
2907
2908 ArrayRef<Elf_Shdr> Sections = unwrapOrError(Obj->sections());
2909 OS << "There are " << to_string(Sections.size())
2910 << " section headers, starting at offset "
2911 << "0x" << to_hexString(Obj->getHeader()->e_shoff, false) << ":\n\n";
2912 OS << "Section Headers:\n";
2913 Field Fields[11] = {{"[Nr]", 2},
2914 {"Name", 7},
2915 {"Type", 25},
2916 {"Address", 41},
2917 {"Off", 58 - Bias},
2918 {"Size", 65 - Bias},
2919 {"ES", 72 - Bias},
2920 {"Flg", 75 - Bias},
2921 {"Lk", 79 - Bias},
2922 {"Inf", 82 - Bias},
2923 {"Al", 86 - Bias}};
2924 for (auto &f : Fields)
2925 printField(f);
2926 OS << "\n";
2927
2928 for (const Elf_Shdr &Sec : Sections) {
2929 Number = to_string(SectionIndex);
2930 Fields[0].Str = Number;
2931 Fields[1].Str = unwrapOrError(Obj->getSectionName(&Sec));
2932 Type = getSectionTypeString(Obj->getHeader()->e_machine, Sec.sh_type);
2933 Fields[2].Str = Type;
2934 Address = to_string(format_hex_no_prefix(Sec.sh_addr, Width));
2935 Fields[3].Str = Address;
2936 Offset = to_string(format_hex_no_prefix(Sec.sh_offset, 6));
2937 Fields[4].Str = Offset;
2938 Size = to_string(format_hex_no_prefix(Sec.sh_size, 6));
2939 Fields[5].Str = Size;
2940 EntrySize = to_string(format_hex_no_prefix(Sec.sh_entsize, 2));
2941 Fields[6].Str = EntrySize;
2942 Flags = getGNUFlags(Sec.sh_flags);
2943 Fields[7].Str = Flags;
2944 Link = to_string(Sec.sh_link);
2945 Fields[8].Str = Link;
2946 Info = to_string(Sec.sh_info);
2947 Fields[9].Str = Info;
2948 Alignment = to_string(Sec.sh_addralign);
2949 Fields[10].Str = Alignment;
2950 OS.PadToColumn(Fields[0].Column);
2951 OS << "[" << right_justify(Fields[0].Str, 2) << "]";
2952 for (int i = 1; i < 7; i++)
2953 printField(Fields[i]);
2954 OS.PadToColumn(Fields[7].Column);
2955 OS << right_justify(Fields[7].Str, 3);
2956 OS.PadToColumn(Fields[8].Column);
2957 OS << right_justify(Fields[8].Str, 2);
2958 OS.PadToColumn(Fields[9].Column);
2959 OS << right_justify(Fields[9].Str, 3);
2960 OS.PadToColumn(Fields[10].Column);
2961 OS << right_justify(Fields[10].Str, 2);
2962 OS << "\n";
2963 ++SectionIndex;
2964 }
2965 OS << "Key to Flags:\n"
2966 << " W (write), A (alloc), X (execute), M (merge), S (strings), l "
2967 "(large)\n"
2968 << " I (info), L (link order), G (group), T (TLS), E (exclude),\
2969 x (unknown)\n"
2970 << " O (extra OS processing required) o (OS specific),\
2971 p (processor specific)\n";
2972}
2973
2974template <class ELFT>
2975void GNUStyle<ELFT>::printSymtabMessage(const ELFO *Obj, StringRef Name,
2976 size_t Entries) {
2977 if (!Name.empty())
2978 OS << "\nSymbol table '" << Name << "' contains " << Entries
2979 << " entries:\n";
2980 else
2981 OS << "\n Symbol table for image:\n";
2982
2983 if (ELFT::Is64Bits)
2984 OS << " Num: Value Size Type Bind Vis Ndx Name\n";
2985 else
2986 OS << " Num: Value Size Type Bind Vis Ndx Name\n";
2987}
2988
2989template <class ELFT>
2990std::string GNUStyle<ELFT>::getSymbolSectionNdx(const ELFO *Obj,
2991 const Elf_Sym *Symbol,
2992 const Elf_Sym *FirstSym) {
2993 unsigned SectionIndex = Symbol->st_shndx;
2994 switch (SectionIndex) {
2995 case ELF::SHN_UNDEF:
2996 return "UND";
2997 case ELF::SHN_ABS:
2998 return "ABS";
2999 case ELF::SHN_COMMON:
3000 return "COM";
3001 case ELF::SHN_XINDEX:
3002 SectionIndex = unwrapOrError(object::getExtendedSymbolTableIndex<ELFT>(
3003 Symbol, FirstSym, this->dumper()->getShndxTable()));
3004 LLVM_FALLTHROUGH[[clang::fallthrough]];
3005 default:
3006 // Find if:
3007 // Processor specific
3008 if (SectionIndex >= ELF::SHN_LOPROC && SectionIndex <= ELF::SHN_HIPROC)
3009 return std::string("PRC[0x") +
3010 to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3011 // OS specific
3012 if (SectionIndex >= ELF::SHN_LOOS && SectionIndex <= ELF::SHN_HIOS)
3013 return std::string("OS[0x") +
3014 to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3015 // Architecture reserved:
3016 if (SectionIndex >= ELF::SHN_LORESERVE &&
3017 SectionIndex <= ELF::SHN_HIRESERVE)
3018 return std::string("RSV[0x") +
3019 to_string(format_hex_no_prefix(SectionIndex, 4)) + "]";
3020 // A normal section with an index
3021 return to_string(format_decimal(SectionIndex, 3));
3022 }
3023}
3024
3025template <class ELFT>
3026void GNUStyle<ELFT>::printSymbol(const ELFO *Obj, const Elf_Sym *Symbol,
3027 const Elf_Sym *FirstSym, StringRef StrTable,
3028 bool IsDynamic) {
3029 static int Idx = 0;
3030 static bool Dynamic = true;
3031 size_t Width;
3032
3033 // If this function was called with a different value from IsDynamic
3034 // from last call, happens when we move from dynamic to static symbol
3035 // table, "Num" field should be reset.
3036 if (!Dynamic != !IsDynamic) {
3037 Idx = 0;
3038 Dynamic = false;
3039 }
3040 std::string Num, Name, Value, Size, Binding, Type, Visibility, Section;
3041 unsigned Bias = 0;
3042 if (ELFT::Is64Bits) {
3043 Bias = 8;
3044 Width = 16;
3045 } else {
3046 Bias = 0;
3047 Width = 8;
3048 }
3049 Field Fields[8] = {0, 8, 17 + Bias, 23 + Bias,
3050 31 + Bias, 38 + Bias, 47 + Bias, 51 + Bias};
3051 Num = to_string(format_decimal(Idx++, 6)) + ":";
3052 Value = to_string(format_hex_no_prefix(Symbol->st_value, Width));
3053 Size = to_string(format_decimal(Symbol->st_size, 5));
3054 unsigned char SymbolType = Symbol->getType();
3055 if (Obj->getHeader()->e_machine == ELF::EM_AMDGPU &&
3056 SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
3057 Type = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
3058 else
3059 Type = printEnum(SymbolType, makeArrayRef(ElfSymbolTypes));
3060 unsigned Vis = Symbol->getVisibility();
3061 Binding = printEnum(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
3062 Visibility = printEnum(Vis, makeArrayRef(ElfSymbolVisibilities));
3063 Section = getSymbolSectionNdx(Obj, Symbol, FirstSym);
3064 Name = this->dumper()->getFullSymbolName(Symbol, StrTable, IsDynamic);
3065 Fields[0].Str = Num;
3066 Fields[1].Str = Value;
3067 Fields[2].Str = Size;
3068 Fields[3].Str = Type;
3069 Fields[4].Str = Binding;
3070 Fields[5].Str = Visibility;
3071 Fields[6].Str = Section;
3072 Fields[7].Str = Name;
3073 for (auto &Entry : Fields)
3074 printField(Entry);
3075 OS << "\n";
3076}
3077template <class ELFT>
3078void GNUStyle<ELFT>::printHashedSymbol(const ELFO *Obj, const Elf_Sym *FirstSym,
3079 uint32_t Sym, StringRef StrTable,
3080 uint32_t Bucket) {
3081 std::string Num, Buc, Name, Value, Size, Binding, Type, Visibility, Section;
3082 unsigned Width, Bias = 0;
3083 if (ELFT::Is64Bits) {
3084 Bias = 8;
3085 Width = 16;
3086 } else {
3087 Bias = 0;
3088 Width = 8;
3089 }
3090 Field Fields[9] = {0, 6, 11, 20 + Bias, 25 + Bias,
3091 34 + Bias, 41 + Bias, 49 + Bias, 53 + Bias};
3092 Num = to_string(format_decimal(Sym, 5));
3093 Buc = to_string(format_decimal(Bucket, 3)) + ":";
3094
3095 const auto Symbol = FirstSym + Sym;
3096 Value = to_string(format_hex_no_prefix(Symbol->st_value, Width));
3097 Size = to_string(format_decimal(Symbol->st_size, 5));
3098 unsigned char SymbolType = Symbol->getType();
3099 if (Obj->getHeader()->e_machine == ELF::EM_AMDGPU &&
3100 SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
3101 Type = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
3102 else
3103 Type = printEnum(SymbolType, makeArrayRef(ElfSymbolTypes));
3104 unsigned Vis = Symbol->getVisibility();
3105 Binding = printEnum(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
3106 Visibility = printEnum(Vis, makeArrayRef(ElfSymbolVisibilities));
3107 Section = getSymbolSectionNdx(Obj, Symbol, FirstSym);
3108 Name = this->dumper()->getFullSymbolName(Symbol, StrTable, true);
3109 Fields[0].Str = Num;
3110 Fields[1].Str = Buc;
3111 Fields[2].Str = Value;
3112 Fields[3].Str = Size;
3113 Fields[4].Str = Type;
3114 Fields[5].Str = Binding;
3115 Fields[6].Str = Visibility;
3116 Fields[7].Str = Section;
3117 Fields[8].Str = Name;
3118 for (auto &Entry : Fields)
3119 printField(Entry);
3120 OS << "\n";
3121}
3122
3123template <class ELFT> void GNUStyle<ELFT>::printSymbols(const ELFO *Obj) {
3124 if (opts::DynamicSymbols)
3125 return;
3126 this->dumper()->printSymbolsHelper(true);
3127 this->dumper()->printSymbolsHelper(false);
3128}
3129
3130template <class ELFT>
3131void GNUStyle<ELFT>::printDynamicSymbols(const ELFO *Obj) {
3132 if (this->dumper()->getDynamicStringTable().empty())
1
Assuming the condition is false
2
Taking false branch
3133 return;
3134 auto StringTable = this->dumper()->getDynamicStringTable();
3135 auto DynSyms = this->dumper()->dynamic_symbols();
3136 auto GnuHash = this->dumper()->getGnuHashTable();
3137 auto SysVHash = this->dumper()->getHashTable();
3
Calling 'ELFDumper::getHashTable'
4
Returning from 'ELFDumper::getHashTable'
5
'SysVHash' initialized here
3138
3139 // If no hash or .gnu.hash found, try using symbol table
3140 if (GnuHash == nullptr && SysVHash == nullptr)
6
Assuming the condition is true
7
Assuming pointer value is null
8
Taking true branch
3141 this->dumper()->printSymbolsHelper(true);
3142
3143 // Try printing .hash
3144 if (this->dumper()->getHashTable()) {
9
Assuming the condition is true
10
Taking true branch
3145 OS << "\n Symbol table of .hash for image:\n";
3146 if (ELFT::Is64Bits)
11
Taking true branch
3147 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3148 else
3149 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3150 OS << "\n";
3151
3152 uint32_t NBuckets = SysVHash->nbucket;
12
Called C++ object pointer is null
3153 uint32_t NChains = SysVHash->nchain;
3154 auto Buckets = SysVHash->buckets();
3155 auto Chains = SysVHash->chains();
3156 for (uint32_t Buc = 0; Buc < NBuckets; Buc++) {
3157 if (Buckets[Buc] == ELF::STN_UNDEF)
3158 continue;
3159 for (uint32_t Ch = Buckets[Buc]; Ch < NChains; Ch = Chains[Ch]) {
3160 if (Ch == ELF::STN_UNDEF)
3161 break;
3162 printHashedSymbol(Obj, &DynSyms[0], Ch, StringTable, Buc);
3163 }
3164 }
3165 }
3166
3167 // Try printing .gnu.hash
3168 if (GnuHash) {
3169 OS << "\n Symbol table of .gnu.hash for image:\n";
3170 if (ELFT::Is64Bits)
3171 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3172 else
3173 OS << " Num Buc: Value Size Type Bind Vis Ndx Name";
3174 OS << "\n";
3175 uint32_t NBuckets = GnuHash->nbuckets;
3176 auto Buckets = GnuHash->buckets();
3177 for (uint32_t Buc = 0; Buc < NBuckets; Buc++) {
3178 if (Buckets[Buc] == ELF::STN_UNDEF)
3179 continue;
3180 uint32_t Index = Buckets[Buc];
3181 uint32_t GnuHashable = Index - GnuHash->symndx;
3182 // Print whole chain
3183 while (true) {
3184 printHashedSymbol(Obj, &DynSyms[0], Index++, StringTable, Buc);
3185 // Chain ends at symbol with stopper bit
3186 if ((GnuHash->values(DynSyms.size())[GnuHashable++] & 1) == 1)
3187 break;
3188 }
3189 }
3190 }
3191}
3192
3193static inline std::string printPhdrFlags(unsigned Flag) {
3194 std::string Str;
3195 Str = (Flag & PF_R) ? "R" : " ";
3196 Str += (Flag & PF_W) ? "W" : " ";
3197 Str += (Flag & PF_X) ? "E" : " ";
3198 return Str;
3199}
3200
3201// SHF_TLS sections are only in PT_TLS, PT_LOAD or PT_GNU_RELRO
3202// PT_TLS must only have SHF_TLS sections
3203template <class ELFT>
3204bool GNUStyle<ELFT>::checkTLSSections(const Elf_Phdr &Phdr,
3205 const Elf_Shdr &Sec) {
3206 return (((Sec.sh_flags & ELF::SHF_TLS) &&
3207 ((Phdr.p_type == ELF::PT_TLS) || (Phdr.p_type == ELF::PT_LOAD) ||
3208 (Phdr.p_type == ELF::PT_GNU_RELRO))) ||
3209 (!(Sec.sh_flags & ELF::SHF_TLS) && Phdr.p_type != ELF::PT_TLS));
3210}
3211
3212// Non-SHT_NOBITS must have its offset inside the segment
3213// Only non-zero section can be at end of segment
3214template <class ELFT>
3215bool GNUStyle<ELFT>::checkoffsets(const Elf_Phdr &Phdr, const Elf_Shdr &Sec) {
3216 if (Sec.sh_type == ELF::SHT_NOBITS)
3217 return true;
3218 bool IsSpecial =
3219 (Sec.sh_type == ELF::SHT_NOBITS) && ((Sec.sh_flags & ELF::SHF_TLS) != 0);
3220 // .tbss is special, it only has memory in PT_TLS and has NOBITS properties
3221 auto SectionSize =
3222 (IsSpecial && Phdr.p_type != ELF::PT_TLS) ? 0 : Sec.sh_size;
3223 if (Sec.sh_offset >= Phdr.p_offset)
3224 return ((Sec.sh_offset + SectionSize <= Phdr.p_filesz + Phdr.p_offset)
3225 /*only non-zero sized sections at end*/ &&
3226 (Sec.sh_offset + 1 <= Phdr.p_offset + Phdr.p_filesz));
3227 return false;
3228}
3229
3230// SHF_ALLOC must have VMA inside segment
3231// Only non-zero section can be at end of segment
3232template <class ELFT>
3233bool GNUStyle<ELFT>::checkVMA(const Elf_Phdr &Phdr, const Elf_Shdr &Sec) {
3234 if (!(Sec.sh_flags & ELF::SHF_ALLOC))
3235 return true;
3236 bool IsSpecial =
3237 (Sec.sh_type == ELF::SHT_NOBITS) && ((Sec.sh_flags & ELF::SHF_TLS) != 0);
3238 // .tbss is special, it only has memory in PT_TLS and has NOBITS properties
3239 auto SectionSize =
3240 (IsSpecial && Phdr.p_type != ELF::PT_TLS) ? 0 : Sec.sh_size;
3241 if (Sec.sh_addr >= Phdr.p_vaddr)
3242 return ((Sec.sh_addr + SectionSize <= Phdr.p_vaddr + Phdr.p_memsz) &&
3243 (Sec.sh_addr + 1 <= Phdr.p_vaddr + Phdr.p_memsz));
3244 return false;
3245}
3246
3247// No section with zero size must be at start or end of PT_DYNAMIC
3248template <class ELFT>
3249bool GNUStyle<ELFT>::checkPTDynamic(const Elf_Phdr &Phdr, const Elf_Shdr &Sec) {
3250 if (Phdr.p_type != ELF::PT_DYNAMIC || Sec.sh_size != 0 || Phdr.p_memsz == 0)
3251 return true;
3252 // Is section within the phdr both based on offset and VMA ?
3253 return ((Sec.sh_type == ELF::SHT_NOBITS) ||
3254 (Sec.sh_offset > Phdr.p_offset &&
3255 Sec.sh_offset < Phdr.p_offset + Phdr.p_filesz)) &&
3256 (!(Sec.sh_flags & ELF::SHF_ALLOC) ||
3257 (Sec.sh_addr > Phdr.p_vaddr && Sec.sh_addr < Phdr.p_memsz));
3258}
3259
3260template <class ELFT>
3261void GNUStyle<ELFT>::printProgramHeaders(const ELFO *Obj) {
3262 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
3263 unsigned Width = ELFT::Is64Bits ? 18 : 10;
3264 unsigned SizeWidth = ELFT::Is64Bits ? 8 : 7;
3265 std::string Type, Offset, VMA, LMA, FileSz, MemSz, Flag, Align;
3266
3267 const Elf_Ehdr *Header = Obj->getHeader();
3268 Field Fields[8] = {2, 17, 26, 37 + Bias,
3269 48 + Bias, 56 + Bias, 64 + Bias, 68 + Bias};
3270 OS << "\nElf file type is "
3271 << printEnum(Header->e_type, makeArrayRef(ElfObjectFileType)) << "\n"
3272 << "Entry point " << format_hex(Header->e_entry, 3) << "\n"
3273 << "There are " << Header->e_phnum << " program headers,"
3274 << " starting at offset " << Header->e_phoff << "\n\n"
3275 << "Program Headers:\n";
3276 if (ELFT::Is64Bits)
3277 OS << " Type Offset VirtAddr PhysAddr "
3278 << " FileSiz MemSiz Flg Align\n";
3279 else
3280 OS << " Type Offset VirtAddr PhysAddr FileSiz "
3281 << "MemSiz Flg Align\n";
3282 for (const auto &Phdr : unwrapOrError(Obj->program_headers())) {
3283 Type = getElfPtType(Header->e_machine, Phdr.p_type);
3284 Offset = to_string(format_hex(Phdr.p_offset, 8));
3285 VMA = to_string(format_hex(Phdr.p_vaddr, Width));
3286 LMA = to_string(format_hex(Phdr.p_paddr, Width));
3287 FileSz = to_string(format_hex(Phdr.p_filesz, SizeWidth));
3288 MemSz = to_string(format_hex(Phdr.p_memsz, SizeWidth));
3289 Flag = printPhdrFlags(Phdr.p_flags);
3290 Align = to_string(format_hex(Phdr.p_align, 1));
3291 Fields[0].Str = Type;
3292 Fields[1].Str = Offset;
3293 Fields[2].Str = VMA;
3294 Fields[3].Str = LMA;
3295 Fields[4].Str = FileSz;
3296 Fields[5].Str = MemSz;
3297 Fields[6].Str = Flag;
3298 Fields[7].Str = Align;
3299 for (auto Field : Fields)
3300 printField(Field);
3301 if (Phdr.p_type == ELF::PT_INTERP) {
3302 OS << "\n [Requesting program interpreter: ";
3303 OS << reinterpret_cast<const char *>(Obj->base()) + Phdr.p_offset << "]";
3304 }
3305 OS << "\n";
3306 }
3307 OS << "\n Section to Segment mapping:\n Segment Sections...\n";
3308 int Phnum = 0;
3309 for (const Elf_Phdr &Phdr : unwrapOrError(Obj->program_headers())) {
3310 std::string Sections;
3311 OS << format(" %2.2d ", Phnum++);
3312 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
3313 // Check if each section is in a segment and then print mapping.
3314 // readelf additionally makes sure it does not print zero sized sections
3315 // at end of segments and for PT_DYNAMIC both start and end of section
3316 // .tbss must only be shown in PT_TLS section.
3317 bool TbssInNonTLS = (Sec.sh_type == ELF::SHT_NOBITS) &&
3318 ((Sec.sh_flags & ELF::SHF_TLS) != 0) &&
3319 Phdr.p_type != ELF::PT_TLS;
3320 if (!TbssInNonTLS && checkTLSSections(Phdr, Sec) &&
3321 checkoffsets(Phdr, Sec) && checkVMA(Phdr, Sec) &&
3322 checkPTDynamic(Phdr, Sec) && (Sec.sh_type != ELF::SHT_NULL))
3323 Sections += unwrapOrError(Obj->getSectionName(&Sec)).str() + " ";
3324 }
3325 OS << Sections << "\n";
3326 OS.flush();
3327 }
3328}
3329
3330template <class ELFT>
3331void GNUStyle<ELFT>::printDynamicRelocation(const ELFO *Obj, Elf_Rela R,
3332 bool IsRela) {
3333 SmallString<32> RelocName;
3334 StringRef SymbolName;
3335 unsigned Width = ELFT::Is64Bits ? 16 : 8;
3336 unsigned Bias = ELFT::Is64Bits ? 8 : 0;
3337 // First two fields are bit width dependent. The rest of them are after are
3338 // fixed width.
3339 Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias};
3340
3341 uint32_t SymIndex = R.getSymbol(Obj->isMips64EL());
3342 const Elf_Sym *Sym = this->dumper()->dynamic_symbols().begin() + SymIndex;
3343 Obj->getRelocationTypeName(R.getType(Obj->isMips64EL()), RelocName);
3344 SymbolName =
3345 unwrapOrError(Sym->getName(this->dumper()->getDynamicStringTable()));
3346 std::string Addend, Info, Offset, Value;
3347 Offset = to_string(format_hex_no_prefix(R.r_offset, Width));
3348 Info = to_string(format_hex_no_prefix(R.r_info, Width));
3349 Value = to_string(format_hex_no_prefix(Sym->getValue(), Width));
3350 int64_t RelAddend = R.r_addend;
3351 if (!SymbolName.empty() && IsRela) {
3352 if (R.r_addend < 0)
3353 Addend = " - ";
3354 else
3355 Addend = " + ";
3356 }
3357
3358 if (SymbolName.empty() && Sym->getValue() == 0)
3359 Value = "";
3360
3361 if (IsRela)
3362 Addend += to_string(format_hex_no_prefix(std::abs(RelAddend), 1));
3363
3364
3365 Fields[0].Str = Offset;
3366 Fields[1].Str = Info;
3367 Fields[2].Str = RelocName.c_str();
3368 Fields[3].Str = Value;
3369 Fields[4].Str = SymbolName;
3370 for (auto &Field : Fields)
3371 printField(Field);
3372 OS << Addend;
3373 OS << "\n";
3374}
3375
3376template <class ELFT>
3377void GNUStyle<ELFT>::printDynamicRelocations(const ELFO *Obj) {
3378 const DynRegionInfo &DynRelRegion = this->dumper()->getDynRelRegion();
3379 const DynRegionInfo &DynRelaRegion = this->dumper()->getDynRelaRegion();
3380 const DynRegionInfo &DynRelrRegion = this->dumper()->getDynRelrRegion();
3381 const DynRegionInfo &DynPLTRelRegion = this->dumper()->getDynPLTRelRegion();
3382 if (DynRelaRegion.Size > 0) {
3383 OS << "\n'RELA' relocation section at offset "
3384 << format_hex(reinterpret_cast<const uint8_t *>(DynRelaRegion.Addr) -
3385 Obj->base(),
3386 1) << " contains " << DynRelaRegion.Size << " bytes:\n";
3387 printRelocHeader(ELF::SHT_RELA);
3388 for (const Elf_Rela &Rela : this->dumper()->dyn_relas())
3389 printDynamicRelocation(Obj, Rela, true);
3390 }
3391 if (DynRelRegion.Size > 0) {
3392 OS << "\n'REL' relocation section at offset "
3393 << format_hex(reinterpret_cast<const uint8_t *>(DynRelRegion.Addr) -
3394 Obj->base(),
3395 1) << " contains " << DynRelRegion.Size << " bytes:\n";
3396 printRelocHeader(ELF::SHT_REL);
3397 for (const Elf_Rel &Rel : this->dumper()->dyn_rels()) {
3398 Elf_Rela Rela;
3399 Rela.r_offset = Rel.r_offset;
3400 Rela.r_info = Rel.r_info;
3401 Rela.r_addend = 0;
3402 printDynamicRelocation(Obj, Rela, false);
3403 }
3404 }
3405 if (DynRelrRegion.Size > 0) {
3406 OS << "\n'RELR' relocation section at offset "
3407 << format_hex(reinterpret_cast<const uint8_t *>(DynRelrRegion.Addr) -
3408 Obj->base(),
3409 1) << " contains " << DynRelrRegion.Size << " bytes:\n";
3410 printRelocHeader(ELF::SHT_REL);
3411 Elf_Relr_Range Relrs = this->dumper()->dyn_relrs();
3412 std::vector<Elf_Rela> RelrRelas = unwrapOrError(Obj->decode_relrs(Relrs));
3413 for (const Elf_Rela &Rela : RelrRelas) {
3414 printDynamicRelocation(Obj, Rela, false);
3415 }
3416 }
3417 if (DynPLTRelRegion.Size) {
3418 OS << "\n'PLT' relocation section at offset "
3419 << format_hex(reinterpret_cast<const uint8_t *>(DynPLTRelRegion.Addr) -
3420 Obj->base(),
3421 1) << " contains " << DynPLTRelRegion.Size << " bytes:\n";
3422 }
3423 if (DynPLTRelRegion.EntSize == sizeof(Elf_Rela)) {
3424 printRelocHeader(ELF::SHT_RELA);
3425 for (const Elf_Rela &Rela : DynPLTRelRegion.getAsArrayRef<Elf_Rela>())
3426 printDynamicRelocation(Obj, Rela, true);
3427 } else {
3428 printRelocHeader(ELF::SHT_REL);
3429 for (const Elf_Rel &Rel : DynPLTRelRegion.getAsArrayRef<Elf_Rel>()) {
3430 Elf_Rela Rela;
3431 Rela.r_offset = Rel.r_offset;
3432 Rela.r_info = Rel.r_info;
3433 Rela.r_addend = 0;
3434 printDynamicRelocation(Obj, Rela, false);
3435 }
3436 }
3437}
3438
3439// Hash histogram shows statistics of how efficient the hash was for the
3440// dynamic symbol table. The table shows number of hash buckets for different
3441// lengths of chains as absolute number and percentage of the total buckets.
3442// Additionally cumulative coverage of symbols for each set of buckets.
3443template <class ELFT>
3444void GNUStyle<ELFT>::printHashHistogram(const ELFFile<ELFT> *Obj) {
3445
3446 const Elf_Hash *HashTable = this->dumper()->getHashTable();
3447 const Elf_GnuHash *GnuHashTable = this->dumper()->getGnuHashTable();
3448
3449 // Print histogram for .hash section
3450 if (HashTable) {
3451 size_t NBucket = HashTable->nbucket;
3452 size_t NChain = HashTable->nchain;
3453 ArrayRef<Elf_Word> Buckets = HashTable->buckets();
3454 ArrayRef<Elf_Word> Chains = HashTable->chains();
3455 size_t TotalSyms = 0;
3456 // If hash table is correct, we have at least chains with 0 length
3457 size_t MaxChain = 1;
3458 size_t CumulativeNonZero = 0;
3459
3460 if (NChain == 0 || NBucket == 0)
3461 return;
3462
3463 std::vector<size_t> ChainLen(NBucket, 0);
3464 // Go over all buckets and and note chain lengths of each bucket (total
3465 // unique chain lengths).
3466 for (size_t B = 0; B < NBucket; B++) {
3467 for (size_t C = Buckets[B]; C > 0 && C < NChain; C = Chains[C])
3468 if (MaxChain <= ++ChainLen[B])
3469 MaxChain++;
3470 TotalSyms += ChainLen[B];
3471 }
3472
3473 if (!TotalSyms)
3474 return;
3475
3476 std::vector<size_t> Count(MaxChain, 0) ;
3477 // Count how long is the chain for each bucket
3478 for (size_t B = 0; B < NBucket; B++)
3479 ++Count[ChainLen[B]];
3480 // Print Number of buckets with each chain lengths and their cumulative
3481 // coverage of the symbols
3482 OS << "Histogram for bucket list length (total of " << NBucket
3483 << " buckets)\n"
3484 << " Length Number % of total Coverage\n";
3485 for (size_t I = 0; I < MaxChain; I++) {
3486 CumulativeNonZero += Count[I] * I;
3487 OS << format("%7lu %-10lu (%5.1f%%) %5.1f%%\n", I, Count[I],
3488 (Count[I] * 100.0) / NBucket,
3489 (CumulativeNonZero * 100.0) / TotalSyms);
3490 }
3491 }
3492
3493 // Print histogram for .gnu.hash section
3494 if (GnuHashTable) {
3495 size_t NBucket = GnuHashTable->nbuckets;
3496 ArrayRef<Elf_Word> Buckets = GnuHashTable->buckets();
3497 unsigned NumSyms = this->dumper()->dynamic_symbols().size();
3498 if (!NumSyms)
3499 return;
3500 ArrayRef<Elf_Word> Chains = GnuHashTable->values(NumSyms);
3501 size_t Symndx = GnuHashTable->symndx;
3502 size_t TotalSyms = 0;
3503 size_t MaxChain = 1;
3504 size_t CumulativeNonZero = 0;
3505
3506 if (Chains.empty() || NBucket == 0)
3507 return;
3508
3509 std::vector<size_t> ChainLen(NBucket, 0);
3510
3511 for (size_t B = 0; B < NBucket; B++) {
3512 if (!Buckets[B])
3513 continue;
3514 size_t Len = 1;
3515 for (size_t C = Buckets[B] - Symndx;
3516 C < Chains.size() && (Chains[C] & 1) == 0; C++)
3517 if (MaxChain < ++Len)
3518 MaxChain++;
3519 ChainLen[B] = Len;
3520 TotalSyms += Len;
3521 }
3522 MaxChain++;
3523
3524 if (!TotalSyms)
3525 return;
3526
3527 std::vector<size_t> Count(MaxChain, 0) ;
3528 for (size_t B = 0; B < NBucket; B++)
3529 ++Count[ChainLen[B]];
3530 // Print Number of buckets with each chain lengths and their cumulative
3531 // coverage of the symbols
3532 OS << "Histogram for `.gnu.hash' bucket list length (total of " << NBucket
3533 << " buckets)\n"
3534 << " Length Number % of total Coverage\n";
3535 for (size_t I = 0; I <MaxChain; I++) {
3536 CumulativeNonZero += Count[I] * I;
3537 OS << format("%7lu %-10lu (%5.1f%%) %5.1f%%\n", I, Count[I],
3538 (Count[I] * 100.0) / NBucket,
3539 (CumulativeNonZero * 100.0) / TotalSyms);
3540 }
3541 }
3542}
3543
3544template <class ELFT>
3545void GNUStyle<ELFT>::printCGProfile(const ELFFile<ELFT> *Obj) {
3546 OS << "GNUStyle::printCGProfile not implemented\n";
3547}
3548
3549template <class ELFT>
3550void GNUStyle<ELFT>::printAddrsig(const ELFFile<ELFT> *Obj) {
3551 OS << "GNUStyle::printAddrsig not implemented\n";
3552}
3553
3554static std::string getGNUNoteTypeName(const uint32_t NT) {
3555 static const struct {
3556 uint32_t ID;
3557 const char *Name;
3558 } Notes[] = {
3559 {ELF::NT_GNU_ABI_TAG, "NT_GNU_ABI_TAG (ABI version tag)"},
3560 {ELF::NT_GNU_HWCAP, "NT_GNU_HWCAP (DSO-supplied software HWCAP info)"},
3561 {ELF::NT_GNU_BUILD_ID, "NT_GNU_BUILD_ID (unique build ID bitstring)"},
3562 {ELF::NT_GNU_GOLD_VERSION, "NT_GNU_GOLD_VERSION (gold version)"},
3563 {ELF::NT_GNU_PROPERTY_TYPE_0, "NT_GNU_PROPERTY_TYPE_0 (property note)"},
3564 };
3565
3566 for (const auto &Note : Notes)
3567 if (Note.ID == NT)
3568 return std::string(Note.Name);
3569
3570 std::string string;
3571 raw_string_ostream OS(string);
3572 OS << format("Unknown note type (0x%08x)", NT);
3573 return OS.str();
3574}
3575
3576static std::string getFreeBSDNoteTypeName(const uint32_t NT) {
3577 static const struct {
3578 uint32_t ID;
3579 const char *Name;
3580 } Notes[] = {
3581 {ELF::NT_FREEBSD_THRMISC, "NT_THRMISC (thrmisc structure)"},
3582 {ELF::NT_FREEBSD_PROCSTAT_PROC, "NT_PROCSTAT_PROC (proc data)"},
3583 {ELF::NT_FREEBSD_PROCSTAT_FILES, "NT_PROCSTAT_FILES (files data)"},
3584 {ELF::NT_FREEBSD_PROCSTAT_VMMAP, "NT_PROCSTAT_VMMAP (vmmap data)"},
3585 {ELF::NT_FREEBSD_PROCSTAT_GROUPS, "NT_PROCSTAT_GROUPS (groups data)"},
3586 {ELF::NT_FREEBSD_PROCSTAT_UMASK, "NT_PROCSTAT_UMASK (umask data)"},
3587 {ELF::NT_FREEBSD_PROCSTAT_RLIMIT, "NT_PROCSTAT_RLIMIT (rlimit data)"},
3588 {ELF::NT_FREEBSD_PROCSTAT_OSREL, "NT_PROCSTAT_OSREL (osreldate data)"},
3589 {ELF::NT_FREEBSD_PROCSTAT_PSSTRINGS,
3590 "NT_PROCSTAT_PSSTRINGS (ps_strings data)"},
3591 {ELF::NT_FREEBSD_PROCSTAT_AUXV, "NT_PROCSTAT_AUXV (auxv data)"},
3592 };
3593
3594 for (const auto &Note : Notes)
3595 if (Note.ID == NT)
3596 return std::string(Note.Name);
3597
3598 std::string string;
3599 raw_string_ostream OS(string);
3600 OS << format("Unknown note type (0x%08x)", NT);
3601 return OS.str();
3602}
3603
3604static std::string getAMDGPUNoteTypeName(const uint32_t NT) {
3605 static const struct {
3606 uint32_t ID;
3607 const char *Name;
3608 } Notes[] = {
3609 {ELF::NT_AMD_AMDGPU_HSA_METADATA,
3610 "NT_AMD_AMDGPU_HSA_METADATA (HSA Metadata)"},
3611 {ELF::NT_AMD_AMDGPU_ISA,
3612 "NT_AMD_AMDGPU_ISA (ISA Version)"},
3613 {ELF::NT_AMD_AMDGPU_PAL_METADATA,
3614 "NT_AMD_AMDGPU_PAL_METADATA (PAL Metadata)"}
3615 };
3616
3617 for (const auto &Note : Notes)
3618 if (Note.ID == NT)
3619 return std::string(Note.Name);
3620
3621 std::string string;
3622 raw_string_ostream OS(string);
3623 OS << format("Unknown note type (0x%08x)", NT);
3624 return OS.str();
3625}
3626
3627template <typename ELFT>
3628static void printGNUProperty(raw_ostream &OS, uint32_t Type, uint32_t DataSize,
3629 ArrayRef<uint8_t> Data) {
3630 switch (Type) {
3631 default:
3632 OS << format(" <application-specific type 0x%x>\n", Type);
3633 return;
3634 case GNU_PROPERTY_STACK_SIZE: {
3635 OS << " stack size: ";
3636 if (DataSize == sizeof(typename ELFT::uint))
3637 OS << format("0x%llx\n",
3638 (uint64_t)(*(const typename ELFT::Addr *)Data.data()));
3639 else
3640 OS << format("<corrupt length: 0x%x>\n", DataSize);
3641 break;
3642 }
3643 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
3644 OS << " no copy on protected";
3645 if (DataSize)
3646 OS << format(" <corrupt length: 0x%x>", DataSize);
3647 OS << "\n";
3648 break;
3649 case GNU_PROPERTY_X86_FEATURE_1_AND:
3650 OS << " X86 features: ";
3651 if (DataSize != 4 && DataSize != 8) {
3652 OS << format("<corrupt length: 0x%x>\n", DataSize);
3653 break;
3654 }
3655 uint64_t CFProtection =
3656 (DataSize == 4)
3657 ? support::endian::read32<ELFT::TargetEndianness>(Data.data())
3658 : support::endian::read64<ELFT::TargetEndianness>(Data.data());
3659 if (CFProtection == 0) {
3660 OS << "none\n";
3661 break;
3662 }
3663 if (CFProtection & GNU_PROPERTY_X86_FEATURE_1_IBT) {
3664 OS << "IBT";
3665 CFProtection &= ~GNU_PROPERTY_X86_FEATURE_1_IBT;
3666 if (CFProtection)
3667 OS << ", ";
3668 }
3669 if (CFProtection & GNU_PROPERTY_X86_FEATURE_1_SHSTK) {
3670 OS << "SHSTK";
3671 CFProtection &= ~GNU_PROPERTY_X86_FEATURE_1_SHSTK;
3672 if (CFProtection)
3673 OS << ", ";
3674 }
3675 if (CFProtection)
3676 OS << format("<unknown flags: 0x%llx>", CFProtection);
3677 OS << "\n";
3678 break;
3679 }
3680}
3681
3682template <typename ELFT>
3683static void printGNUNote(raw_ostream &OS, uint32_t NoteType,
3684 ArrayRef<typename ELFT::Word> Words, size_t Size) {
3685 using Elf_Word = typename ELFT::Word;
3686
3687 switch (NoteType) {
3688 default:
3689 return;
3690 case ELF::NT_GNU_ABI_TAG: {
3691 static const char *OSNames[] = {
3692 "Linux", "Hurd", "Solaris", "FreeBSD", "NetBSD", "Syllable", "NaCl",
3693 };
3694
3695 StringRef OSName = "Unknown";
3696 if (Words[0] < array_lengthof(OSNames))
3697 OSName = OSNames[Words[0]];
3698 uint32_t Major = Words[1], Minor = Words[2], Patch = Words[3];
3699
3700 if (Words.size() < 4)
3701 OS << " <corrupt GNU_ABI_TAG>";
3702 else
3703 OS << " OS: " << OSName << ", ABI: " << Major << "." << Minor << "."
3704 << Patch;
3705 break;
3706 }
3707 case ELF::NT_GNU_BUILD_ID: {
3708 OS << " Build ID: ";
3709 ArrayRef<uint8_t> ID(reinterpret_cast<const uint8_t *>(Words.data()), Size);
3710 for (const auto &B : ID)
3711 OS << format_hex_no_prefix(B, 2);
3712 break;
3713 }
3714 case ELF::NT_GNU_GOLD_VERSION:
3715 OS << " Version: "
3716 << StringRef(reinterpret_cast<const char *>(Words.data()), Size);
3717 break;
3718 case ELF::NT_GNU_PROPERTY_TYPE_0:
3719 OS << " Properties:";
3720
3721 ArrayRef<uint8_t> Arr(reinterpret_cast<const uint8_t *>(Words.data()),
3722 Size);
3723 while (Arr.size() >= 8) {
3724 uint32_t Type = *reinterpret_cast<const Elf_Word *>(Arr.data());
3725 uint32_t DataSize = *reinterpret_cast<const Elf_Word *>(Arr.data() + 4);
3726 Arr = Arr.drop_front(8);
3727
3728 // Take padding size into account if present.
3729 uint64_t PaddedSize = alignTo(DataSize, sizeof(typename ELFT::uint));
3730 if (Arr.size() < PaddedSize) {
3731 OS << format(" <corrupt type (0x%x) datasz: 0x%x>\n", Type,
3732 DataSize);
3733 break;
3734 }
3735 printGNUProperty<ELFT>(OS, Type, DataSize, Arr.take_front(PaddedSize));
3736 Arr = Arr.drop_front(PaddedSize);
3737 }
3738
3739 if (!Arr.empty())
3740 OS << " <corrupted GNU_PROPERTY_TYPE_0>";
3741 break;
3742 }
3743 OS << '\n';
3744}
3745
3746template <typename ELFT>
3747static void printAMDGPUNote(raw_ostream &OS, uint32_t NoteType,
3748 ArrayRef<typename ELFT::Word> Words, size_t Size) {
3749 switch (NoteType) {
3750 default:
3751 return;
3752 case ELF::NT_AMD_AMDGPU_HSA_METADATA:
3753 OS << " HSA Metadata:\n"
3754 << StringRef(reinterpret_cast<const char *>(Words.data()), Size);
3755 break;
3756 case ELF::NT_AMD_AMDGPU_ISA:
3757 OS << " ISA Version:\n"
3758 << " "
3759 << StringRef(reinterpret_cast<const char *>(Words.data()), Size);
3760 break;
3761 case ELF::NT_AMD_AMDGPU_PAL_METADATA:
3762 const uint32_t *PALMetadataBegin = reinterpret_cast<const uint32_t *>(Words.data());
3763 const uint32_t *PALMetadataEnd = PALMetadataBegin + Size;
3764 std::vector<uint32_t> PALMetadata(PALMetadataBegin, PALMetadataEnd);
3765 std::string PALMetadataString;
3766 auto Error = AMDGPU::PALMD::toString(PALMetadata, PALMetadataString);
3767 OS << " PAL Metadata:\n";
3768 if (Error) {
3769 OS << " Invalid";
3770 return;
3771 }
3772 OS << PALMetadataString;
3773 break;
3774 }
3775 OS.flush();
3776}
3777
3778template <class ELFT>
3779void GNUStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
3780 const Elf_Ehdr *e = Obj->getHeader();
3781 bool IsCore = e->e_type == ELF::ET_CORE;
3782
3783 auto PrintHeader = [&](const typename ELFT::Off Offset,
3784 const typename ELFT::Addr Size) {
3785 OS << "Displaying notes found at file offset " << format_hex(Offset, 10)
3786 << " with length " << format_hex(Size, 10) << ":\n"
3787 << " Owner Data size\tDescription\n";
3788 };
3789
3790 auto ProcessNote = [&](const Elf_Note &Note) {
3791 StringRef Name = Note.getName();
3792 ArrayRef<Elf_Word> Descriptor = Note.getDesc();
3793 Elf_Word Type = Note.getType();
3794
3795 OS << " " << Name << std::string(22 - Name.size(), ' ')
3796 << format_hex(Descriptor.size(), 10) << '\t';
3797
3798 if (Name == "GNU") {
3799 OS << getGNUNoteTypeName(Type) << '\n';
3800 printGNUNote<ELFT>(OS, Type, Descriptor, Descriptor.size());
3801 } else if (Name == "FreeBSD") {
3802 OS << getFreeBSDNoteTypeName(Type) << '\n';
3803 } else if (Name == "AMD") {
3804 OS << getAMDGPUNoteTypeName(Type) << '\n';
3805 printAMDGPUNote<ELFT>(OS, Type, Descriptor, Descriptor.size());
3806 } else {
3807 OS << "Unknown note type: (" << format_hex(Type, 10) << ')';
3808 }
3809 OS << '\n';
3810 };
3811
3812 if (IsCore) {
3813 for (const auto &P : unwrapOrError(Obj->program_headers())) {
3814 if (P.p_type != PT_NOTE)
3815 continue;
3816 PrintHeader(P.p_offset, P.p_filesz);
3817 Error Err = Error::success();
3818 for (const auto &Note : Obj->notes(P, Err))
3819 ProcessNote(Note);
3820 if (Err)
3821 error(std::move(Err));
3822 }
3823 } else {
3824 for (const auto &S : unwrapOrError(Obj->sections())) {
3825 if (S.sh_type != SHT_NOTE)
3826 continue;
3827 PrintHeader(S.sh_offset, S.sh_size);
3828 Error Err = Error::success();
3829 for (const auto &Note : Obj->notes(S, Err))
3830 ProcessNote(Note);
3831 if (Err)
3832 error(std::move(Err));
3833 }
3834 }
3835}
3836
3837template <class ELFT>
3838void GNUStyle<ELFT>::printELFLinkerOptions(const ELFFile<ELFT> *Obj) {
3839 OS << "printELFLinkerOptions not implemented!\n";
3840}
3841
3842template <class ELFT>
3843void GNUStyle<ELFT>::printMipsGOT(const MipsGOTParser<ELFT> &Parser) {
3844 size_t Bias = ELFT::Is64Bits ? 8 : 0;
3845 auto PrintEntry = [&](const Elf_Addr *E, StringRef Purpose) {
3846 OS.PadToColumn(2);
3847 OS << format_hex_no_prefix(Parser.getGotAddress(E), 8 + Bias);
3848 OS.PadToColumn(11 + Bias);
3849 OS << format_decimal(Parser.getGotOffset(E), 6) << "(gp)";
3850 OS.PadToColumn(22 + Bias);
3851 OS << format_hex_no_prefix(*E, 8 + Bias);
3852 OS.PadToColumn(31 + 2 * Bias);
3853 OS << Purpose << "\n";
3854 };
3855
3856 OS << (Parser.IsStatic ? "Static GOT:\n" : "Primary GOT:\n");
3857 OS << " Canonical gp value: "
3858 << format_hex_no_prefix(Parser.getGp(), 8 + Bias) << "\n\n";
3859
3860 OS << " Reserved entries:\n";
3861 OS << " Address Access Initial Purpose\n";
3862 PrintEntry(Parser.getGotLazyResolver(), "Lazy resolver");
3863 if (Parser.getGotModulePointer())
3864 PrintEntry(Parser.getGotModulePointer(), "Module pointer (GNU extension)");
3865
3866 if (!Parser.getLocalEntries().empty()) {
3867 OS << "\n";
3868 OS << " Local entries:\n";
3869 OS << " Address Access Initial\n";
3870 for (auto &E : Parser.getLocalEntries())
3871 PrintEntry(&E, "");
3872 }
3873
3874 if (Parser.IsStatic)
3875 return;
3876
3877 if (!Parser.getGlobalEntries().empty()) {
3878 OS << "\n";
3879 OS << " Global entries:\n";
3880 OS << " Address Access Initial Sym.Val. Type Ndx Name\n";
3881 for (auto &E : Parser.getGlobalEntries()) {
3882 const Elf_Sym *Sym = Parser.getGotSym(&E);
3883 std::string SymName = this->dumper()->getFullSymbolName(
3884 Sym, this->dumper()->getDynamicStringTable(), false);
3885
3886 OS.PadToColumn(2);
3887 OS << to_string(format_hex_no_prefix(Parser.getGotAddress(&E), 8 + Bias));
3888 OS.PadToColumn(11 + Bias);
3889 OS << to_string(format_decimal(Parser.getGotOffset(&E), 6)) + "(gp)";
3890 OS.PadToColumn(22 + Bias);
3891 OS << to_string(format_hex_no_prefix(E, 8 + Bias));
3892 OS.PadToColumn(31 + 2 * Bias);
3893 OS << to_string(format_hex_no_prefix(Sym->st_value, 8 + Bias));
3894 OS.PadToColumn(40 + 3 * Bias);
3895 OS << printEnum(Sym->getType(), makeArrayRef(ElfSymbolTypes));
3896 OS.PadToColumn(48 + 3 * Bias);
3897 OS << getSymbolSectionNdx(Parser.Obj, Sym,
3898 this->dumper()->dynamic_symbols().begin());
3899 OS.PadToColumn(52 + 3 * Bias);
3900 OS << SymName << "\n";
3901 }
3902 }
3903
3904 if (!Parser.getOtherEntries().empty())
3905 OS << "\n Number of TLS and multi-GOT entries "
3906 << Parser.getOtherEntries().size() << "\n";
3907}
3908
3909template <class ELFT>
3910void GNUStyle<ELFT>::printMipsPLT(const MipsGOTParser<ELFT> &Parser) {
3911 size_t Bias = ELFT::Is64Bits ? 8 : 0;
3912 auto PrintEntry = [&](const Elf_Addr *E, StringRef Purpose) {
3913 OS.PadToColumn(2);
3914 OS << format_hex_no_prefix(Parser.getGotAddress(E), 8 + Bias);
3915 OS.PadToColumn(11 + Bias);
3916 OS << format_hex_no_prefix(*E, 8 + Bias);
3917 OS.PadToColumn(20 + 2 * Bias);
3918 OS << Purpose << "\n";
3919 };
3920
3921 OS << "PLT GOT:\n\n";
3922
3923 OS << " Reserved entries:\n";
3924 OS << " Address Initial Purpose\n";
3925 PrintEntry(Parser.getPltLazyResolver(), "PLT lazy resolver");
3926 if (Parser.getPltModulePointer())
3927 PrintEntry(Parser.getGotModulePointer(), "Module pointer");
3928
3929 if (!Parser.getPltEntries().empty()) {
3930 OS << "\n";
3931 OS << " Entries:\n";
3932 OS << " Address Initial Sym.Val. Type Ndx Name\n";
3933 for (auto &E : Parser.getPltEntries()) {
3934 const Elf_Sym *Sym = Parser.getPltSym(&E);
3935 std::string SymName = this->dumper()->getFullSymbolName(
3936 Sym, this->dumper()->getDynamicStringTable(), false);
3937
3938 OS.PadToColumn(2);
3939 OS << to_string(format_hex_no_prefix(Parser.getGotAddress(&E), 8 + Bias));
3940 OS.PadToColumn(11 + Bias);
3941 OS << to_string(format_hex_no_prefix(E, 8 + Bias));
3942 OS.PadToColumn(20 + 2 * Bias);
3943 OS << to_string(format_hex_no_prefix(Sym->st_value, 8 + Bias));
3944 OS.PadToColumn(29 + 3 * Bias);
3945 OS << printEnum(Sym->getType(), makeArrayRef(ElfSymbolTypes));
3946 OS.PadToColumn(37 + 3 * Bias);
3947 OS << getSymbolSectionNdx(Parser.Obj, Sym,
3948 this->dumper()->dynamic_symbols().begin());
3949 OS.PadToColumn(41 + 3 * Bias);
3950 OS << SymName << "\n";
3951 }
3952 }
3953}
3954
3955template <class ELFT> void LLVMStyle<ELFT>::printFileHeaders(const ELFO *Obj) {
3956 const Elf_Ehdr *e = Obj->getHeader();
3957 {
3958 DictScope D(W, "ElfHeader");
3959 {
3960 DictScope D(W, "Ident");
3961 W.printBinary("Magic", makeArrayRef(e->e_ident).slice(ELF::EI_MAG0, 4));
3962 W.printEnum("Class", e->e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
3963 W.printEnum("DataEncoding", e->e_ident[ELF::EI_DATA],
3964 makeArrayRef(ElfDataEncoding));
3965 W.printNumber("FileVersion", e->e_ident[ELF::EI_VERSION]);
3966
3967 auto OSABI = makeArrayRef(ElfOSABI);
3968 if (e->e_ident[ELF::EI_OSABI] >= ELF::ELFOSABI_FIRST_ARCH &&
3969 e->e_ident[ELF::EI_OSABI] <= ELF::ELFOSABI_LAST_ARCH) {
3970 switch (e->e_machine) {
3971 case ELF::EM_AMDGPU:
3972 OSABI = makeArrayRef(AMDGPUElfOSABI);
3973 break;
3974 case ELF::EM_ARM:
3975 OSABI = makeArrayRef(ARMElfOSABI);
3976 break;
3977 case ELF::EM_TI_C6000:
3978 OSABI = makeArrayRef(C6000ElfOSABI);
3979 break;
3980 }
3981 }
3982 W.printEnum("OS/ABI", e->e_ident[ELF::EI_OSABI], OSABI);
3983 W.printNumber("ABIVersion", e->e_ident[ELF::EI_ABIVERSION]);
3984 W.printBinary("Unused", makeArrayRef(e->e_ident).slice(ELF::EI_PAD));
3985 }
3986
3987 W.printEnum("Type", e->e_type, makeArrayRef(ElfObjectFileType));
3988 W.printEnum("Machine", e->e_machine, makeArrayRef(ElfMachineType));
3989 W.printNumber("Version", e->e_version);
3990 W.printHex("Entry", e->e_entry);
3991 W.printHex("ProgramHeaderOffset", e->e_phoff);
3992 W.printHex("SectionHeaderOffset", e->e_shoff);
3993 if (e->e_machine == EM_MIPS)
3994 W.printFlags("Flags", e->e_flags, makeArrayRef(ElfHeaderMipsFlags),
3995 unsigned(ELF::EF_MIPS_ARCH), unsigned(ELF::EF_MIPS_ABI),
3996 unsigned(ELF::EF_MIPS_MACH));
3997 else if (e->e_machine == EM_AMDGPU)
3998 W.printFlags("Flags", e->e_flags, makeArrayRef(ElfHeaderAMDGPUFlags),
3999 unsigned(ELF::EF_AMDGPU_MACH));
4000 else if (e->e_machine == EM_RISCV)
4001 W.printFlags("Flags", e->e_flags, makeArrayRef(ElfHeaderRISCVFlags));
4002 else
4003 W.printFlags("Flags", e->e_flags);
4004 W.printNumber("HeaderSize", e->e_ehsize);
4005 W.printNumber("ProgramHeaderEntrySize", e->e_phentsize);
4006 W.printNumber("ProgramHeaderCount", e->e_phnum);
4007 W.printNumber("SectionHeaderEntrySize", e->e_shentsize);
4008 W.printString("SectionHeaderCount", getSectionHeadersNumString(Obj));
4009 W.printString("StringTableSectionIndex", getSectionHeaderTableIndexString(Obj));
4010 }
4011}
4012
4013template <class ELFT>
4014void LLVMStyle<ELFT>::printGroupSections(const ELFO *Obj) {
4015 DictScope Lists(W, "Groups");
4016 std::vector<GroupSection> V = getGroups<ELFT>(Obj);
4017 DenseMap<uint64_t, const GroupSection *> Map = mapSectionsToGroups(V);
4018 for (const GroupSection &G : V) {
4019 DictScope D(W, "Group");
4020 W.printNumber("Name", G.Name, G.ShName);
4021 W.printNumber("Index", G.Index);
4022 W.printNumber("Link", G.Link);
4023 W.printNumber("Info", G.Info);
4024 W.printHex("Type", getGroupType(G.Type), G.Type);
4025 W.startLine() << "Signature: " << G.Signature << "\n";
4026
4027 ListScope L(W, "Section(s) in group");
4028 for (const GroupMember &GM : G.Members) {
4029 const GroupSection *MainGroup = Map[GM.Index];
4030 if (MainGroup != &G) {
4031 W.flush();
4032 errs() << "Error: " << GM.Name << " (" << GM.Index
4033 << ") in a group " + G.Name + " (" << G.Index
4034 << ") is already in a group " + MainGroup->Name + " ("
4035 << MainGroup->Index << ")\n";
4036 errs().flush();
4037 continue;
4038 }
4039 W.startLine() << GM.Name << " (" << GM.Index << ")\n";
4040 }
4041 }
4042
4043 if (V.empty())
4044 W.startLine() << "There are no group sections in the file.\n";
4045}
4046
4047template <class ELFT> void LLVMStyle<ELFT>::printRelocations(const ELFO *Obj) {
4048 ListScope D(W, "Relocations");
4049
4050 int SectionNumber = -1;
4051 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
4052 ++SectionNumber;
4053
4054 if (Sec.sh_type != ELF::SHT_REL &&
4055 Sec.sh_type != ELF::SHT_RELA &&
4056 Sec.sh_type != ELF::SHT_RELR &&
4057 Sec.sh_type != ELF::SHT_ANDROID_REL &&
4058 Sec.sh_type != ELF::SHT_ANDROID_RELA &&
4059 Sec.sh_type != ELF::SHT_ANDROID_RELR)
4060 continue;
4061
4062 StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
4063
4064 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
4065 W.indent();
4066
4067 printRelocations(&Sec, Obj);
4068
4069 W.unindent();
4070 W.startLine() << "}\n";
4071 }
4072}
4073
4074template <class ELFT>
4075void LLVMStyle<ELFT>::printRelocations(const Elf_Shdr *Sec, const ELFO *Obj) {
4076 const Elf_Shdr *SymTab = unwrapOrError(Obj->getSection(Sec->sh_link));
4077
4078 switch (Sec->sh_type) {
4079 case ELF::SHT_REL:
4080 for (const Elf_Rel &R : unwrapOrError(Obj->rels(Sec))) {
4081 Elf_Rela Rela;
4082 Rela.r_offset = R.r_offset;
4083 Rela.r_info = R.r_info;
4084 Rela.r_addend = 0;
4085 printRelocation(Obj, Rela, SymTab);
4086 }
4087 break;
4088 case ELF::SHT_RELA:
4089 for (const Elf_Rela &R : unwrapOrError(Obj->relas(Sec)))
4090 printRelocation(Obj, R, SymTab);
4091 break;
4092 case ELF::SHT_RELR:
4093 case ELF::SHT_ANDROID_RELR: {
4094 Elf_Relr_Range Relrs = unwrapOrError(Obj->relrs(Sec));
4095 if (opts::RawRelr) {
4096 for (const Elf_Relr &R : Relrs)
4097 W.startLine() << W.hex(R) << "\n";
4098 } else {
4099 std::vector<Elf_Rela> RelrRelas = unwrapOrError(Obj->decode_relrs(Relrs));
4100 for (const Elf_Rela &R : RelrRelas)
4101 printRelocation(Obj, R, SymTab);
4102 }
4103 break;
4104 }
4105 case ELF::SHT_ANDROID_REL:
4106 case ELF::SHT_ANDROID_RELA:
4107 for (const Elf_Rela &R : unwrapOrError(Obj->android_relas(Sec)))
4108 printRelocation(Obj, R, SymTab);
4109 break;
4110 }
4111}
4112
4113template <class ELFT>
4114void LLVMStyle<ELFT>::printRelocation(const ELFO *Obj, Elf_Rela Rel,
4115 const Elf_Shdr *SymTab) {
4116 SmallString<32> RelocName;
4117 Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
4118 StringRef TargetName;
4119 const Elf_Sym *Sym = unwrapOrError(Obj->getRelocationSymbol(&Rel, SymTab));
4120 if (Sym && Sym->getType() == ELF::STT_SECTION) {
4121 const Elf_Shdr *Sec = unwrapOrError(
4122 Obj->getSection(Sym, SymTab, this->dumper()->getShndxTable()));
4123 TargetName = unwrapOrError(Obj->getSectionName(Sec));
4124 } else if (Sym) {
4125 StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*SymTab));
4126 TargetName = unwrapOrError(Sym->getName(StrTable));
4127 }
4128
4129 if (opts::ExpandRelocs) {
4130 DictScope Group(W, "Relocation");
4131 W.printHex("Offset", Rel.r_offset);
4132 W.printNumber("Type", RelocName, (int)Rel.getType(Obj->isMips64EL()));
4133 W.printNumber("Symbol", !TargetName.empty() ? TargetName : "-",
4134 Rel.getSymbol(Obj->isMips64EL()));
4135 W.printHex("Addend", Rel.r_addend);
4136 } else {
4137 raw_ostream &OS = W.startLine();
4138 OS << W.hex(Rel.r_offset) << " " << RelocName << " "
4139 << (!TargetName.empty() ? TargetName : "-") << " "
4140 << W.hex(Rel.r_addend) << "\n";
4141 }
4142}
4143
4144template <class ELFT> void LLVMStyle<ELFT>::printSections(const ELFO *Obj) {
4145 ListScope SectionsD(W, "Sections");
4146
4147 int SectionIndex = -1;
4148 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
4149 ++SectionIndex;
4150
4151 StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
4152
4153 DictScope SectionD(W, "Section");
4154 W.printNumber("Index", SectionIndex);
4155 W.printNumber("Name", Name, Sec.sh_name);
4156 W.printHex(
4157 "Type",
4158 object::getELFSectionTypeName(Obj->getHeader()->e_machine, Sec.sh_type),
4159 Sec.sh_type);
4160 std::vector<EnumEntry<unsigned>> SectionFlags(std::begin(ElfSectionFlags),
4161 std::end(ElfSectionFlags));
4162 switch (Obj->getHeader()->e_machine) {
4163 case EM_ARM:
4164 SectionFlags.insert(SectionFlags.end(), std::begin(ElfARMSectionFlags),
4165 std::end(ElfARMSectionFlags));
4166 break;
4167 case EM_HEXAGON:
4168 SectionFlags.insert(SectionFlags.end(),
4169 std::begin(ElfHexagonSectionFlags),
4170 std::end(ElfHexagonSectionFlags));
4171 break;
4172 case EM_MIPS:
4173 SectionFlags.insert(SectionFlags.end(), std::begin(ElfMipsSectionFlags),
4174 std::end(ElfMipsSectionFlags));
4175 break;
4176 case EM_X86_64:
4177 SectionFlags.insert(SectionFlags.end(), std::begin(ElfX86_64SectionFlags),
4178 std::end(ElfX86_64SectionFlags));
4179 break;
4180 case EM_XCORE:
4181 SectionFlags.insert(SectionFlags.end(), std::begin(ElfXCoreSectionFlags),
4182 std::end(ElfXCoreSectionFlags));
4183 break;
4184 default:
4185 // Nothing to do.
4186 break;
4187 }
4188 W.printFlags("Flags", Sec.sh_flags, makeArrayRef(SectionFlags));
4189 W.printHex("Address", Sec.sh_addr);
4190 W.printHex("Offset", Sec.sh_offset);
4191 W.printNumber("Size", Sec.sh_size);
4192 W.printNumber("Link", Sec.sh_link);
4193 W.printNumber("Info", Sec.sh_info);
4194 W.printNumber("AddressAlignment", Sec.sh_addralign);
4195 W.printNumber("EntrySize", Sec.sh_entsize);
4196
4197 if (opts::SectionRelocations) {
4198 ListScope D(W, "Relocations");
4199 printRelocations(&Sec, Obj);
4200 }
4201
4202 if (opts::SectionSymbols) {
4203 ListScope D(W, "Symbols");
4204 const Elf_Shdr *Symtab = this->dumper()->getDotSymtabSec();
4205 StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*Symtab));
4206
4207 for (const Elf_Sym &Sym : unwrapOrError(Obj->symbols(Symtab))) {
4208 const Elf_Shdr *SymSec = unwrapOrError(
4209 Obj->getSection(&Sym, Symtab, this->dumper()->getShndxTable()));
4210 if (SymSec == &Sec)
4211 printSymbol(Obj, &Sym, unwrapOrError(Obj->symbols(Symtab)).begin(),
4212 StrTable, false);
4213 }
4214 }
4215
4216 if (opts::SectionData && Sec.sh_type != ELF::SHT_NOBITS) {
4217 ArrayRef<uint8_t> Data = unwrapOrError(Obj->getSectionContents(&Sec));
4218 W.printBinaryBlock("SectionData",
4219 StringRef((const char *)Data.data(), Data.size()));
4220 }
4221 }
4222}
4223
4224template <class ELFT>
4225void LLVMStyle<ELFT>::printSymbol(const ELFO *Obj, const Elf_Sym *Symbol,
4226 const Elf_Sym *First, StringRef StrTable,
4227 bool IsDynamic) {
4228 unsigned SectionIndex = 0;
4229 StringRef SectionName;
4230 this->dumper()->getSectionNameIndex(Symbol, First, SectionName, SectionIndex);
4231 std::string FullSymbolName =
4232 this->dumper()->getFullSymbolName(Symbol, StrTable, IsDynamic);
4233 unsigned char SymbolType = Symbol->getType();
4234
4235 DictScope D(W, "Symbol");
4236 W.printNumber("Name", FullSymbolName, Symbol->st_name);
4237 W.printHex("Value", Symbol->st_value);
4238 W.printNumber("Size", Symbol->st_size);
4239 W.printEnum("Binding", Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
4240 if (Obj->getHeader()->e_machine == ELF::EM_AMDGPU &&
4241 SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
4242 W.printEnum("Type", SymbolType, makeArrayRef(AMDGPUSymbolTypes));
4243 else
4244 W.printEnum("Type", SymbolType, makeArrayRef(ElfSymbolTypes));
4245 if (Symbol->st_other == 0)
4246 // Usually st_other flag is zero. Do not pollute the output
4247 // by flags enumeration in that case.
4248 W.printNumber("Other", 0);
4249 else {
4250 std::vector<EnumEntry<unsigned>> SymOtherFlags(std::begin(ElfSymOtherFlags),
4251 std::end(ElfSymOtherFlags));
4252 if (Obj->getHeader()->e_machine == EM_MIPS) {
4253 // Someones in their infinite wisdom decided to make STO_MIPS_MIPS16
4254 // flag overlapped with other ST_MIPS_xxx flags. So consider both
4255 // cases separately.
4256 if ((Symbol->st_other & STO_MIPS_MIPS16) == STO_MIPS_MIPS16)
4257 SymOtherFlags.insert(SymOtherFlags.end(),
4258 std::begin(ElfMips16SymOtherFlags),
4259 std::end(ElfMips16SymOtherFlags));
4260 else
4261 SymOtherFlags.insert(SymOtherFlags.end(),
4262 std::begin(ElfMipsSymOtherFlags),
4263 std::end(ElfMipsSymOtherFlags));
4264 }
4265 W.printFlags("Other", Symbol->st_other, makeArrayRef(SymOtherFlags), 0x3u);
4266 }
4267 W.printHex("Section", SectionName, SectionIndex);
4268}
4269
4270template <class ELFT> void LLVMStyle<ELFT>::printSymbols(const ELFO *Obj) {
4271 ListScope Group(W, "Symbols");
4272 this->dumper()->printSymbolsHelper(false);
4273}
4274
4275template <class ELFT>
4276void LLVMStyle<ELFT>::printDynamicSymbols(const ELFO *Obj) {
4277 ListScope Group(W, "DynamicSymbols");
4278 this->dumper()->printSymbolsHelper(true);
4279}
4280
4281template <class ELFT>
4282void LLVMStyle<ELFT>::printDynamicRelocations(const ELFO *Obj) {
4283 const DynRegionInfo &DynRelRegion = this->dumper()->getDynRelRegion();
4284 const DynRegionInfo &DynRelaRegion = this->dumper()->getDynRelaRegion();
4285 const DynRegionInfo &DynRelrRegion = this->dumper()->getDynRelrRegion();
4286 const DynRegionInfo &DynPLTRelRegion = this->dumper()->getDynPLTRelRegion();
4287 if (DynRelRegion.Size && DynRelaRegion.Size)
4288 report_fatal_error("There are both REL and RELA dynamic relocations");
4289 W.startLine() << "Dynamic Relocations {\n";
4290 W.indent();
4291 if (DynRelaRegion.Size > 0)
4292 for (const Elf_Rela &Rela : this->dumper()->dyn_relas())
4293 printDynamicRelocation(Obj, Rela);
4294 else
4295 for (const Elf_Rel &Rel : this->dumper()->dyn_rels()) {
4296 Elf_Rela Rela;
4297 Rela.r_offset = Rel.r_offset;
4298 Rela.r_info = Rel.r_info;
4299 Rela.r_addend = 0;
4300 printDynamicRelocation(Obj, Rela);
4301 }
4302 if (DynRelrRegion.Size > 0) {
4303 Elf_Relr_Range Relrs = this->dumper()->dyn_relrs();
4304 std::vector<Elf_Rela> RelrRelas = unwrapOrError(Obj->decode_relrs(Relrs));
4305 for (const Elf_Rela &Rela : RelrRelas)
4306 printDynamicRelocation(Obj, Rela);
4307 }
4308 if (DynPLTRelRegion.EntSize == sizeof(Elf_Rela))
4309 for (const Elf_Rela &Rela : DynPLTRelRegion.getAsArrayRef<Elf_Rela>())
4310 printDynamicRelocation(Obj, Rela);
4311 else
4312 for (const Elf_Rel &Rel : DynPLTRelRegion.getAsArrayRef<Elf_Rel>()) {
4313 Elf_Rela Rela;
4314 Rela.r_offset = Rel.r_offset;
4315 Rela.r_info = Rel.r_info;
4316 Rela.r_addend = 0;
4317 printDynamicRelocation(Obj, Rela);
4318 }
4319 W.unindent();
4320 W.startLine() << "}\n";
4321}
4322
4323template <class ELFT>
4324void LLVMStyle<ELFT>::printDynamicRelocation(const ELFO *Obj, Elf_Rela Rel) {
4325 SmallString<32> RelocName;
4326 Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
4327 StringRef SymbolName;
4328 uint32_t SymIndex = Rel.getSymbol(Obj->isMips64EL());
4329 const Elf_Sym *Sym = this->dumper()->dynamic_symbols().begin() + SymIndex;
4330 SymbolName =
4331 unwrapOrError(Sym->getName(this->dumper()->getDynamicStringTable()));
4332 if (opts::ExpandRelocs) {
4333 DictScope Group(W, "Relocation");
4334 W.printHex("Offset", Rel.r_offset);
4335 W.printNumber("Type", RelocName, (int)Rel.getType(Obj->isMips64EL()));
4336 W.printString("Symbol", !SymbolName.empty() ? SymbolName : "-");
4337 W.printHex("Addend", Rel.r_addend);
4338 } else {
4339 raw_ostream &OS = W.startLine();
4340 OS << W.hex(Rel.r_offset) << " " << RelocName << " "
4341 << (!SymbolName.empty() ? SymbolName : "-") << " "
4342 << W.hex(Rel.r_addend) << "\n";
4343 }
4344}
4345
4346template <class ELFT>
4347void LLVMStyle<ELFT>::printProgramHeaders(const ELFO *Obj) {
4348 ListScope L(W, "ProgramHeaders");
4349
4350 for (const Elf_Phdr &Phdr : unwrapOrError(Obj->program_headers())) {
4351 DictScope P(W, "ProgramHeader");
4352 W.printHex("Type",
4353 getElfSegmentType(Obj->getHeader()->e_machine, Phdr.p_type),
4354 Phdr.p_type);
4355 W.printHex("Offset", Phdr.p_offset);
4356 W.printHex("VirtualAddress", Phdr.p_vaddr);
4357 W.printHex("PhysicalAddress", Phdr.p_paddr);
4358 W.printNumber("FileSize", Phdr.p_filesz);
4359 W.printNumber("MemSize", Phdr.p_memsz);
4360 W.printFlags("Flags", Phdr.p_flags, makeArrayRef(ElfSegmentFlags));
4361 W.printNumber("Alignment", Phdr.p_align);
4362 }
4363}
4364
4365template <class ELFT>
4366void LLVMStyle<ELFT>::printHashHistogram(const ELFFile<ELFT> *Obj) {
4367 W.startLine() << "Hash Histogram not implemented!\n";
4368}
4369
4370template <class ELFT>
4371void LLVMStyle<ELFT>::printCGProfile(const ELFFile<ELFT> *Obj) {
4372 ListScope L(W, "CGProfile");
4373 if (!this->dumper()->getDotCGProfileSec())
4374 return;
4375 auto CGProfile =
4376 unwrapOrError(Obj->template getSectionContentsAsArray<Elf_CGProfile>(
4377 this->dumper()->getDotCGProfileSec()));
4378 for (const Elf_CGProfile &CGPE : CGProfile) {
4379 DictScope D(W, "CGProfileEntry");
4380 W.printNumber("From", this->dumper()->getStaticSymbolName(CGPE.cgp_from),
4381 CGPE.cgp_from);
4382 W.printNumber("To", this->dumper()->getStaticSymbolName(CGPE.cgp_to),
4383 CGPE.cgp_to);
4384 W.printNumber("Weight", CGPE.cgp_weight);
4385 }
4386}
4387
4388template <class ELFT>
4389void LLVMStyle<ELFT>::printAddrsig(const ELFFile<ELFT> *Obj) {
4390 ListScope L(W, "Addrsig");
4391 if (!this->dumper()->getDotAddrsigSec())
4392 return;
4393 ArrayRef<uint8_t> Contents = unwrapOrError(
4394 Obj->getSectionContents(this->dumper()->getDotAddrsigSec()));
4395 const uint8_t *Cur = Contents.begin();
4396 const uint8_t *End = Contents.end();
4397 while (Cur != End) {
4398 unsigned Size;
4399 const char *Err;
4400 uint64_t SymIndex = decodeULEB128(Cur, &Size, Contents.end(), &Err);
4401 if (Err)
4402 reportError(Err);
4403 W.printNumber("Sym", this->dumper()->getStaticSymbolName(SymIndex),
4404 SymIndex);
4405 Cur += Size;
4406 }
4407}
4408
4409template <class ELFT>
4410void LLVMStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
4411 W.startLine() << "printNotes not implemented!\n";
4412}
4413
4414template <class ELFT>
4415void LLVMStyle<ELFT>::printELFLinkerOptions(const ELFFile<ELFT> *Obj) {
4416 ListScope L(W, "LinkerOptions");
4417
4418 for (const Elf_Shdr &Shdr : unwrapOrError(Obj->sections())) {
4419 if (Shdr.sh_type != ELF::SHT_LLVM_LINKER_OPTIONS)
4420 continue;
4421
4422 ArrayRef<uint8_t> Contents = unwrapOrError(Obj->getSectionContents(&Shdr));
4423 for (const uint8_t *P = Contents.begin(), *E = Contents.end(); P < E; ) {
4424 StringRef Key = StringRef(reinterpret_cast<const char *>(P));
4425 StringRef Value =
4426 StringRef(reinterpret_cast<const char *>(P) + Key.size() + 1);
4427
4428 W.printString(Key, Value);
4429
4430 P = P + Key.size() + Value.size() + 2;
4431 }
4432 }
4433}
4434
4435template <class ELFT>
4436void LLVMStyle<ELFT>::printMipsGOT(const MipsGOTParser<ELFT> &Parser) {
4437 auto PrintEntry = [&](const Elf_Addr *E) {
4438 W.printHex("Address", Parser.getGotAddress(E));
4439 W.printNumber("Access", Parser.getGotOffset(E));
4440 W.printHex("Initial", *E);
4441 };
4442
4443 DictScope GS(W, Parser.IsStatic ? "Static GOT" : "Primary GOT");
4444
4445 W.printHex("Canonical gp value", Parser.getGp());
4446 {
4447 ListScope RS(W, "Reserved entries");
4448 {
4449 DictScope D(W, "Entry");
4450 PrintEntry(Parser.getGotLazyResolver());
4451 W.printString("Purpose", StringRef("Lazy resolver"));
4452 }
4453
4454 if (Parser.getGotModulePointer()) {
4455 DictScope D(W, "Entry");
4456 PrintEntry(Parser.getGotModulePointer());
4457 W.printString("Purpose", StringRef("Module pointer (GNU extension)"));
4458 }
4459 }
4460 {
4461 ListScope LS(W, "Local entries");
4462 for (auto &E : Parser.getLocalEntries()) {
4463 DictScope D(W, "Entry");
4464 PrintEntry(&E);
4465 }
4466 }
4467
4468 if (Parser.IsStatic)
4469 return;
4470
4471 {
4472 ListScope GS(W, "Global entries");
4473 for (auto &E : Parser.getGlobalEntries()) {
4474 DictScope D(W, "Entry");
4475
4476 PrintEntry(&E);
4477
4478 const Elf_Sym *Sym = Parser.getGotSym(&E);
4479 W.printHex("Value", Sym->st_value);
4480 W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes));
4481
4482 unsigned SectionIndex = 0;
4483 StringRef SectionName;
4484 this->dumper()->getSectionNameIndex(
4485 Sym, this->dumper()->dynamic_symbols().begin(), SectionName,
4486 SectionIndex);
4487 W.printHex("Section", SectionName, SectionIndex);
4488
4489 std::string SymName = this->dumper()->getFullSymbolName(
4490 Sym, this->dumper()->getDynamicStringTable(), true);
4491 W.printNumber("Name", SymName, Sym->st_name);
4492 }
4493 }
4494
4495 W.printNumber("Number of TLS and multi-GOT entries",
4496 uint64_t(Parser.getOtherEntries().size()));
4497}
4498
4499template <class ELFT>
4500void LLVMStyle<ELFT>::printMipsPLT(const MipsGOTParser<ELFT> &Parser) {
4501 auto PrintEntry = [&](const Elf_Addr *E) {
4502 W.printHex("Address", Parser.getPltAddress(E));
4503 W.printHex("Initial", *E);
4504 };
4505
4506 DictScope GS(W, "PLT GOT");
4507
4508 {
4509 ListScope RS(W, "Reserved entries");
4510 {
4511 DictScope D(W, "Entry");
4512 PrintEntry(Parser.getPltLazyResolver());
4513 W.printString("Purpose", StringRef("PLT lazy resolver"));
4514 }
4515
4516 if (auto E = Parser.getPltModulePointer()) {
4517 DictScope D(W, "Entry");
4518 PrintEntry(E);
4519 W.printString("Purpose", StringRef("Module pointer"));
4520 }
4521 }
4522 {
4523 ListScope LS(W, "Entries");
4524 for (auto &E : Parser.getPltEntries()) {
4525 DictScope D(W, "Entry");
4526 PrintEntry(&E);
4527
4528 const Elf_Sym *Sym = Parser.getPltSym(&E);
4529 W.printHex("Value", Sym->st_value);
4530 W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes));
4531
4532 unsigned SectionIndex = 0;
4533 StringRef SectionName;
4534 this->dumper()->getSectionNameIndex(
4535 Sym, this->dumper()->dynamic_symbols().begin(), SectionName,
4536 SectionIndex);
4537 W.printHex("Section", SectionName, SectionIndex);
4538
4539 std::string SymName =
4540 this->dumper()->getFullSymbolName(Sym, Parser.getPltStrTable(), true);
4541 W.printNumber("Name", SymName, Sym->st_name);
4542 }
4543 }
4544}