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