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