From dbb81881955d641bc873442e75874a5cb160f4ee Mon Sep 17 00:00:00 2001 From: Georgii Rymar Date: Mon, 7 Sep 2020 15:30:38 +0300 Subject: [PATCH] [llvm-readobj/elf] - Generalize the code for printing dynamic relocations. NFCI. Currently we have 2 large `printDynamicRelocations` methods that have a very similar code for GNU/LLVM styles. This patch removes the duplication and renames them to `printDynamicReloc` for consistency. Differential revision: https://reviews.llvm.org/D87087 --- llvm/tools/llvm-readobj/ELFDumper.cpp | 91 +++++++++++---------------- 1 file changed, 37 insertions(+), 54 deletions(-) diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index d7312eaf2c93..9c1b2e320937 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -774,7 +774,11 @@ protected: 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 @@ private: 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 @@ private: 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 @@ static void printRelocHeaderFields(formatted_raw_ostream &OS, unsigned SType) { } 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 @@ RelSymbol getSymbolForReloc(const ELFFile &Obj, StringRef FileName, } // 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::printDynamic() { } 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::printDynamic() { } 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 =