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