diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -278,8 +278,13 @@ StringRef getSymbolVersionByIndex(StringRef StrTab, uint32_t VersionSymbolIndex, bool &IsDefault) const; - void printSymbolsHelper(bool IsDynamic) const; + void printVersionSymbolSection(const ELFFile *Obj, + const Elf_Shdr *Sec) const; + void printVersionDefinitionSection(const ELFFile *Obj, + const Elf_Shdr *Sec) const; + void printVersionDependencySection(const ELFFile *Obj, + const Elf_Shdr *Sec) const; const Elf_Shdr *getDotSymtabSec() const { return DotSymtabSec; } const Elf_Shdr *getDotCGProfileSec() const { return DotCGProfileSec; } const Elf_Shdr *getDotAddrsigSec() const { return DotAddrsigSec; } @@ -346,12 +351,13 @@ virtual void printProgramHeaders(const ELFFile *Obj, bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) = 0; - virtual void printVersionSymbolSection(const ELFFile *Obj, - const Elf_Shdr *Sec) = 0; - virtual void printVersionDefinitionSection(const ELFFile *Obj, - const Elf_Shdr *Sec) = 0; - virtual void printVersionDependencySection(const ELFFile *Obj, - const Elf_Shdr *Sec) = 0; + virtual void printVersionSymbol(const ELFFile *Obj, const Elf_Shdr *Sec, + StringRef SecName, StringRef StrTab, + const uint8_t *VersymBuf) = 0; + virtual void printVersionDefinition(const ELFFile *Obj, + const Elf_Shdr *Sec) = 0; + virtual void printVersionDependency(const ELFFile *Obj, + const Elf_Shdr *Sec) = 0; virtual void printHashHistogram(const ELFFile *Obj) = 0; virtual void printCGProfile(const ELFFile *Obj) = 0; virtual void printAddrsig(const ELFFile *Obj) = 0; @@ -386,12 +392,13 @@ size_t Offset) override; void printProgramHeaders(const ELFO *Obj, bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) override; - void printVersionSymbolSection(const ELFFile *Obj, - const Elf_Shdr *Sec) override; - void printVersionDefinitionSection(const ELFFile *Obj, - const Elf_Shdr *Sec) override; - void printVersionDependencySection(const ELFFile *Obj, - const Elf_Shdr *Sec) override; + void printVersionSymbol(const ELFFile *Obj, const Elf_Shdr *Sec, + StringRef SecName, StringRef StrTab, + const uint8_t *VersymBuf) override; + void printVersionDefinition(const ELFFile *Obj, + const Elf_Shdr *Sec) override; + void printVersionDependency(const ELFFile *Obj, + const Elf_Shdr *Sec) override; void printHashHistogram(const ELFFile *Obj) override; void printCGProfile(const ELFFile *Obj) override; void printAddrsig(const ELFFile *Obj) override; @@ -488,12 +495,13 @@ void printDynamicRelocations(const ELFO *Obj) override; void printProgramHeaders(const ELFO *Obj, bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) override; - void printVersionSymbolSection(const ELFFile *Obj, - const Elf_Shdr *Sec) override; - void printVersionDefinitionSection(const ELFFile *Obj, - const Elf_Shdr *Sec) override; - void printVersionDependencySection(const ELFFile *Obj, - const Elf_Shdr *Sec) override; + void printVersionSymbol(const ELFFile *Obj, const Elf_Shdr *Sec, + StringRef SecName, StringRef StrTab, + const uint8_t *VersymBuf) override; + void printVersionDefinition(const ELFFile *Obj, + const Elf_Shdr *Sec) override; + void printVersionDependency(const ELFFile *Obj, + const Elf_Shdr *Sec) override; void printHashHistogram(const ELFFile *Obj) override; void printCGProfile(const ELFFile *Obj) override; void printAddrsig(const ELFFile *Obj) override; @@ -656,6 +664,38 @@ return this->getSymbolVersionByIndex(StrTab, Versym->vs_index, IsDefault); } +template +void ELFDumper::printVersionSymbolSection(const ELFFile *Obj, + const Elf_Shdr *Sec) const { + if (!Sec) + return; + + StringRef SecName = unwrapOrError(Obj->getSectionName(Sec)); + StringRef StrTab = getDynamicStringTable(); + const uint8_t *VersymBuf = + reinterpret_cast(Obj->base() + Sec->sh_offset); + + ELFDumperStyle->printVersionSymbol(Obj, Sec, SecName, StrTab, VersymBuf); +} + +template +void ELFDumper::printVersionDefinitionSection(const ELFFile *Obj, + const Elf_Shdr *Sec) const { + if (!Sec) + return; + + ELFDumperStyle->printVersionDefinition(Obj, Sec); +} + +template +void ELFDumper::printVersionDependencySection(const ELFFile *Obj, + const Elf_Shdr *Sec) const { + if (!Sec) + return; + + ELFDumperStyle->printVersionDependency(Obj, Sec); +} + static std::string maybeDemangle(StringRef Name) { return opts::Demangle ? demangle(Name) : Name.str(); } @@ -1520,16 +1560,13 @@ template void ELFDumper::printVersionInfo() { // Dump version symbol section. - ELFDumperStyle->printVersionSymbolSection(ObjF->getELFFile(), - SymbolVersionSection); + printVersionSymbolSection(ObjF->getELFFile(), SymbolVersionSection); // Dump version definition section. - ELFDumperStyle->printVersionDefinitionSection(ObjF->getELFFile(), - SymbolVersionDefSection); + printVersionDefinitionSection(ObjF->getELFFile(), SymbolVersionDefSection); // Dump version dependency section. - ELFDumperStyle->printVersionDependencySection(ObjF->getELFFile(), - SymbolVersionNeedSection); + printVersionDependencySection(ObjF->getELFFile(), SymbolVersionNeedSection); } template void ELFDumper::printDynamicRelocations() { @@ -3353,12 +3390,10 @@ } template -void GNUStyle::printVersionSymbolSection(const ELFFile *Obj, - const Elf_Shdr *Sec) { - if (!Sec) - return; - - StringRef SecName = unwrapOrError(Obj->getSectionName(Sec)); +void GNUStyle::printVersionSymbol(const ELFFile *Obj, + const Elf_Shdr *Sec, StringRef SecName, + StringRef StrTab, + const uint8_t *VersymBuf) { uint64_t Entries = Sec->sh_size / sizeof(Elf_Versym); OS << "Version symbols section '" << SecName << "' " @@ -3366,15 +3401,11 @@ const Elf_Shdr *SymTab = unwrapOrError(Obj->getSection(Sec->sh_link)); StringRef SymTabName = unwrapOrError(Obj->getSectionName(SymTab)); + const ELFDumper *Dumper = this->dumper(); OS << " Addr: " << format_hex_no_prefix(Sec->sh_addr, 16) << " Offset: " << format_hex(Sec->sh_offset, 8) << " Link: " << Sec->sh_link << " (" << SymTabName << ")\n"; - const uint8_t *VersymBuf = - reinterpret_cast(Obj->base() + Sec->sh_offset); - const ELFDumper *Dumper = this->dumper(); - StringRef StrTable = Dumper->getDynamicStringTable(); - // readelf prints 4 entries per line. for (uint64_t VersymRow = 0; VersymRow < Entries; VersymRow += 4) { OS << " " << format_hex_no_prefix(VersymRow, 3) << ":"; @@ -3397,7 +3428,7 @@ bool IsDefault = true; std::string VersionName = Dumper->getSymbolVersionByIndex( - StrTable, Versym->vs_index, IsDefault); + StrTab, Versym->vs_index, IsDefault); if (!VersionName.empty()) VersionName = "(" + VersionName + ")"; @@ -3413,21 +3444,15 @@ } template -void GNUStyle::printVersionDefinitionSection(const ELFFile *Obj, - const Elf_Shdr *Sec) { - if (!Sec) - return; - +void GNUStyle::printVersionDefinition(const ELFFile *Obj, + const Elf_Shdr *Sec) { StringRef SecName = unwrapOrError(Obj->getSectionName(Sec)); OS << "Dumper for " << SecName << " is not implemented\n"; } template -void GNUStyle::printVersionDependencySection(const ELFFile *Obj, - const Elf_Shdr *Sec) { - if (!Sec) - return; - +void GNUStyle::printVersionDependency(const ELFFile *Obj, + const Elf_Shdr *Sec) { StringRef SecName = unwrapOrError(Obj->getSectionName(Sec)); OS << "Dumper for " << SecName << " is not implemented\n"; } @@ -4534,22 +4559,18 @@ } template -void LLVMStyle::printVersionSymbolSection(const ELFFile *Obj, - const Elf_Shdr *Sec) { +void LLVMStyle::printVersionSymbol(const ELFFile *Obj, + const Elf_Shdr *Sec, StringRef SecName, + StringRef StrTab, + const uint8_t *VersymBuf) { DictScope SS(W, "Version symbols"); - if (!Sec) - return; - StringRef SecName = unwrapOrError(Obj->getSectionName(Sec)); W.printNumber("Section Name", SecName, Sec->sh_name); W.printHex("Address", Sec->sh_addr); W.printHex("Offset", Sec->sh_offset); W.printNumber("Link", Sec->sh_link); - const uint8_t *VersymBuf = - reinterpret_cast(Obj->base() + Sec->sh_offset); const ELFDumper *Dumper = this->dumper(); - StringRef StrTable = Dumper->getDynamicStringTable(); // Same number of entries in the dynamic symbol table (DT_SYMTAB). ListScope Syms(W, "Symbols"); @@ -4557,7 +4578,7 @@ DictScope S(W, "Symbol"); const Elf_Versym *Versym = reinterpret_cast(VersymBuf); std::string FullSymbolName = - Dumper->getFullSymbolName(&Sym, StrTable, true /* IsDynamic */); + Dumper->getFullSymbolName(&Sym, StrTab, true /* IsDynamic */); W.printNumber("Version", Versym->vs_index & VERSYM_VERSION); W.printString("Name", FullSymbolName); VersymBuf += sizeof(Elf_Versym); @@ -4565,11 +4586,9 @@ } template -void LLVMStyle::printVersionDefinitionSection(const ELFFile *Obj, - const Elf_Shdr *Sec) { +void LLVMStyle::printVersionDefinition(const ELFFile *Obj, + const Elf_Shdr *Sec) { DictScope SD(W, "SHT_GNU_verdef"); - if (!Sec) - return; const uint8_t *SecStartAddress = reinterpret_cast(Obj->base() + Sec->sh_offset); @@ -4612,11 +4631,9 @@ } template -void LLVMStyle::printVersionDependencySection(const ELFFile *Obj, - const Elf_Shdr *Sec) { +void LLVMStyle::printVersionDependency(const ELFFile *Obj, + const Elf_Shdr *Sec) { DictScope SD(W, "SHT_GNU_verneed"); - if (!Sec) - return; const uint8_t *SecData = reinterpret_cast(Obj->base() + Sec->sh_offset);