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 @@ -774,7 +774,11 @@ virtual void printReloc(const Relocation &R, unsigned RelIndex, const Elf_Shdr &Sec, const Elf_Shdr *SymTab) = 0; virtual void printRelrReloc(const Elf_Relr &R) = 0; + virtual void printDynamicReloc(const Relocation &R) = 0; void printRelocationsHelper(const Elf_Shdr &Sec); + void printDynamicRelocationsHelper(); + virtual void printDynamicRelocHeader(unsigned Type, StringRef Name, + const DynRegionInfo &Reg){}; StringRef getPrintableSectionName(const Elf_Shdr &Sec) const; @@ -894,9 +898,12 @@ void printSymbol(const Elf_Sym *Symbol, const Elf_Sym *First, Optional StrTable, bool IsDynamic, bool NonVisibilityBitsUsed) override; + void printDynamicRelocHeader(unsigned Type, StringRef Name, + const DynRegionInfo &Reg) override; + void printDynamicReloc(const Relocation &R) override; + std::string getSymbolSectionNdx(const Elf_Sym *Symbol, const Elf_Sym *FirstSym); - void printDynamicRelocation(const Relocation &R); void printProgramHeaders(); void printSectionMapping(); void printGNUVersionSectionProlog(const typename ELFT::Shdr *Sec, @@ -951,8 +958,7 @@ void printReloc(const Relocation &R, unsigned RelIndex, const Elf_Shdr &Sec, const Elf_Shdr *SymTab) override; void printRelrReloc(const Elf_Relr &R) override; - - void printDynamicRelocation(const Relocation &R); + void printDynamicReloc(const Relocation &R) override; void printSymbols(); void printDynamicSymbols(); @@ -3694,10 +3700,9 @@ } template -static void printDynamicRelocHeader(const ELFFile &Obj, - formatted_raw_ostream &OS, unsigned Type, - StringRef Name, const DynRegionInfo &Reg) { - uint64_t Offset = Reg.Addr - Obj.base(); +void GNUStyle::printDynamicRelocHeader(unsigned Type, StringRef Name, + const DynRegionInfo &Reg) { + uint64_t Offset = Reg.Addr - this->Obj.base(); OS << "\n'" << Name.str().c_str() << "' relocation section at offset 0x" << to_hexString(Offset, false) << " contains " << Reg.Size << " bytes:\n"; printRelocHeaderFields(OS, Type); @@ -4376,7 +4381,7 @@ } // namespace template -void GNUStyle::printDynamicRelocation(const Relocation &R) { +void GNUStyle::printDynamicReloc(const Relocation &R) { printRelRelaReloc( R, getSymbolForReloc(this->Obj, this->FileName, this->dumper(), R)); } @@ -4424,39 +4429,43 @@ } template void GNUStyle::printDynamicRelocations() { - const DynRegionInfo &DynRelRegion = this->dumper()->getDynRelRegion(); - const DynRegionInfo &DynRelaRegion = this->dumper()->getDynRelaRegion(); - const DynRegionInfo &DynRelrRegion = this->dumper()->getDynRelrRegion(); - const DynRegionInfo &DynPLTRelRegion = this->dumper()->getDynPLTRelRegion(); + this->printDynamicRelocationsHelper(); +} + +template void DumpStyle::printDynamicRelocationsHelper() { const bool IsMips64EL = this->Obj.isMips64EL(); + const DynRegionInfo &DynRelaRegion = this->dumper()->getDynRelaRegion(); if (DynRelaRegion.Size > 0) { - printDynamicRelocHeader(this->Obj, OS, ELF::SHT_RELA, "RELA", - DynRelaRegion); + printDynamicRelocHeader(ELF::SHT_RELA, "RELA", DynRelaRegion); for (const Elf_Rela &Rela : this->dumper()->dyn_relas()) - printDynamicRelocation(Relocation(Rela, IsMips64EL)); + printDynamicReloc(Relocation(Rela, IsMips64EL)); } + + const DynRegionInfo &DynRelRegion = this->dumper()->getDynRelRegion(); if (DynRelRegion.Size > 0) { - printDynamicRelocHeader(this->Obj, OS, ELF::SHT_REL, "REL", DynRelRegion); + printDynamicRelocHeader(ELF::SHT_REL, "REL", DynRelRegion); for (const Elf_Rel &Rel : this->dumper()->dyn_rels()) - printDynamicRelocation(Relocation(Rel, IsMips64EL)); + printDynamicReloc(Relocation(Rel, IsMips64EL)); } + + const DynRegionInfo &DynRelrRegion = this->dumper()->getDynRelrRegion(); if (DynRelrRegion.Size > 0) { - printDynamicRelocHeader(this->Obj, OS, ELF::SHT_REL, "RELR", DynRelrRegion); + printDynamicRelocHeader(ELF::SHT_REL, "RELR", DynRelrRegion); Elf_Relr_Range Relrs = this->dumper()->dyn_relrs(); - for (const Elf_Rel &R : this->Obj.decode_relrs(Relrs)) - printDynamicRelocation(Relocation(R, IsMips64EL)); + for (const Elf_Rel &Rel : Obj.decode_relrs(Relrs)) + printDynamicReloc(Relocation(Rel, IsMips64EL)); } + + const DynRegionInfo &DynPLTRelRegion = this->dumper()->getDynPLTRelRegion(); if (DynPLTRelRegion.Size) { if (DynPLTRelRegion.EntSize == sizeof(Elf_Rela)) { - printDynamicRelocHeader(this->Obj, OS, ELF::SHT_RELA, "PLT", - DynPLTRelRegion); + printDynamicRelocHeader(ELF::SHT_RELA, "PLT", DynPLTRelRegion); for (const Elf_Rela &Rela : DynPLTRelRegion.getAsArrayRef()) - printDynamicRelocation(Relocation(Rela, IsMips64EL)); + printDynamicReloc(Relocation(Rela, IsMips64EL)); } else { - printDynamicRelocHeader(this->Obj, OS, ELF::SHT_REL, "PLT", - DynPLTRelRegion); + printDynamicRelocHeader(ELF::SHT_REL, "PLT", DynPLTRelRegion); for (const Elf_Rel &Rel : DynPLTRelRegion.getAsArrayRef()) - printDynamicRelocation(Relocation(Rel, IsMips64EL)); + printDynamicReloc(Relocation(Rel, IsMips64EL)); } } } @@ -6344,41 +6353,15 @@ } template void LLVMStyle::printDynamicRelocations() { - const DynRegionInfo &DynRelRegion = this->dumper()->getDynRelRegion(); - const DynRegionInfo &DynRelaRegion = this->dumper()->getDynRelaRegion(); - const DynRegionInfo &DynRelrRegion = this->dumper()->getDynRelrRegion(); - const DynRegionInfo &DynPLTRelRegion = this->dumper()->getDynPLTRelRegion(); - const bool IsMips64EL = this->Obj.isMips64EL(); - W.startLine() << "Dynamic Relocations {\n"; W.indent(); - if (DynRelaRegion.Size > 0) { - for (const Elf_Rela &Rela : this->dumper()->dyn_relas()) - printDynamicRelocation(Relocation(Rela, IsMips64EL)); - } - if (DynRelRegion.Size > 0) { - for (const Elf_Rel &Rel : this->dumper()->dyn_rels()) - printDynamicRelocation(Relocation(Rel, IsMips64EL)); - } - - if (DynRelrRegion.Size > 0) { - Elf_Relr_Range Relrs = this->dumper()->dyn_relrs(); - for (const Elf_Rel &Rel : this->Obj.decode_relrs(Relrs)) - printDynamicRelocation(Relocation(Rel, IsMips64EL)); - } - if (DynPLTRelRegion.EntSize == sizeof(Elf_Rela)) - for (const Elf_Rela &Rela : DynPLTRelRegion.getAsArrayRef()) - printDynamicRelocation(Relocation(Rela, IsMips64EL)); - else - for (const Elf_Rel &Rel : DynPLTRelRegion.getAsArrayRef()) - printDynamicRelocation(Relocation(Rel, IsMips64EL)); - + this->printDynamicRelocationsHelper(); W.unindent(); W.startLine() << "}\n"; } template -void LLVMStyle::printDynamicRelocation(const Relocation &R) { +void LLVMStyle::printDynamicReloc(const Relocation &R) { SmallString<32> RelocName; this->Obj.getRelocationTypeName(R.Type, RelocName); std::string SymbolName =