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 @@ -279,6 +279,12 @@ 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; } @@ -345,12 +351,12 @@ 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) = 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; @@ -385,12 +391,12 @@ 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) 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; @@ -487,12 +493,12 @@ 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) 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; @@ -653,6 +659,35 @@ 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(); + ELFDumperStyle->printVersionSymbol(Obj, Sec, SecName, StrTab); +} + +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(); } @@ -1516,16 +1551,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() { @@ -3349,12 +3381,9 @@ } 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) { uint64_t Entries = Sec->sh_size / sizeof(Elf_Versym); OS << "Version symbols section '" << SecName << "' " @@ -3362,14 +3391,14 @@ 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) { @@ -3393,7 +3422,7 @@ bool IsDefault = true; std::string VersionName = Dumper->getSymbolVersionByIndex( - StrTable, Versym->vs_index, IsDefault); + StrTab, Versym->vs_index, IsDefault); if (!VersionName.empty()) VersionName = "(" + VersionName + ")"; @@ -3409,21 +3438,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"; } @@ -4529,21 +4552,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) { 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 = (const uint8_t *)Obj->base() + Sec->sh_offset; const ELFDumper *Dumper = this->dumper(); - StringRef StrTable = Dumper->getDynamicStringTable(); + const uint8_t *VersymBuf = + reinterpret_cast(Obj->base() + Sec->sh_offset); // Same number of entries in the dynamic symbol table (DT_SYMTAB). ListScope Syms(W, "Symbols"); @@ -4551,7 +4571,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); @@ -4559,14 +4579,12 @@ } 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 = - (const uint8_t *)Obj->base() + Sec->sh_offset; + reinterpret_cast(Obj->base() + Sec->sh_offset); const uint8_t *SecEndAddress = SecStartAddress + Sec->sh_size; const uint8_t *VerdefBuf = SecStartAddress; const Elf_Shdr *StrTab = unwrapOrError(Obj->getSection(Sec->sh_link)); @@ -4606,11 +4624,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 = (const uint8_t *)Obj->base() + Sec->sh_offset; const Elf_Shdr *StrTab = unwrapOrError(Obj->getSection(Sec->sh_link));