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