Index: llvm/tools/llvm-readobj/ELFDumper.cpp =================================================================== --- llvm/tools/llvm-readobj/ELFDumper.cpp +++ llvm/tools/llvm-readobj/ELFDumper.cpp @@ -753,6 +753,9 @@ const Elf_Shdr *Sec, const Elf_Shdr *SymTab) = 0; virtual void printRelrReloc(const Elf_Relr &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; @@ -878,6 +881,8 @@ 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; std::string getSymbolSectionNdx(const Elf_Sym *Symbol, const Elf_Sym *FirstSym); template void printDynamicRelocation(const RelTy &R); @@ -3604,14 +3609,20 @@ void GNUStyle::printRelReloc(const Elf_Rel &R, unsigned RelIndex, const Elf_Shdr *Sec, const Elf_Shdr *SymTab) { - printRelRelaReloc(R, RelIndex, *Sec, SymTab); + if (Sec) + printRelRelaReloc(R, RelIndex, *Sec, SymTab); + else + printDynamicRelocation(R); } template void GNUStyle::printRelaReloc(const Elf_Rela &R, unsigned RelIndex, const Elf_Shdr *Sec, const Elf_Shdr *SymTab) { - printRelRelaReloc(R, RelIndex, *Sec, SymTab); + if (Sec) + printRelRelaReloc(R, RelIndex, *Sec, SymTab); + else + printDynamicRelocation(R); } template void GNUStyle::printRelrReloc(const Elf_Relr &R) { @@ -3705,10 +3716,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); @@ -4442,38 +4452,42 @@ } template void GNUStyle::printDynamicRelocations() { - const DynRegionInfo &DynRelRegion = this->dumper()->getDynRelRegion(); + this->printDynamicRelocationsHelper(); +} + +template void DumpStyle::printDynamicRelocationsHelper() { const DynRegionInfo &DynRelaRegion = this->dumper()->getDynRelaRegion(); - const DynRegionInfo &DynRelrRegion = this->dumper()->getDynRelrRegion(); - const DynRegionInfo &DynPLTRelRegion = this->dumper()->getDynPLTRelRegion(); 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(Rela); + printRelaReloc(Rela, /*RelNdx=*/0, /*Sec=*/nullptr, /*Symtab=*/nullptr); } + + 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(Rel); + printRelReloc(Rel, /*RelNdx=*/0, /*Sec=*/nullptr, /*Symtab=*/nullptr); } + + 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(R); + for (const Elf_Rel &R : Obj.decode_relrs(Relrs)) + printRelReloc(R, /*RelNdx=*/0, /*Sec=*/nullptr, /*Symtab=*/nullptr); } + + 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(Rela); + printRelaReloc(Rela, /*RelNdx=*/0, /*Sec=*/nullptr, /*Symtab=*/nullptr); } else { - printDynamicRelocHeader(this->Obj, OS, ELF::SHT_REL, "PLT", - DynPLTRelRegion); + printDynamicRelocHeader(ELF::SHT_REL, "PLT", DynPLTRelRegion); for (const Elf_Rel &Rel : DynPLTRelRegion.getAsArrayRef()) - printDynamicRelocation(Rel); + printRelReloc(Rel, /*RelNdx=*/0, /*Sec=*/nullptr, /*Symtab=*/nullptr); } } } @@ -6150,14 +6164,20 @@ void LLVMStyle::printRelReloc(const Elf_Rel &R, unsigned RelIndex, const Elf_Shdr *Sec, const Elf_Shdr *SymTab) { - printRelRelaReloc(R, RelIndex, *Sec, SymTab); + if (Sec) + printRelRelaReloc(R, RelIndex, *Sec, SymTab); + else + printDynamicRelocation(R); } template void LLVMStyle::printRelaReloc(const Elf_Rela &R, unsigned RelIndex, const Elf_Shdr *Sec, const Elf_Shdr *SymTab) { - printRelRelaReloc(R, RelIndex, *Sec, SymTab); + if (Sec) + printRelRelaReloc(R, RelIndex, *Sec, SymTab); + else + printDynamicRelocation(R); } template void LLVMStyle::printRelrReloc(const Elf_Relr &R) { @@ -6377,34 +6397,9 @@ } 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(); - W.startLine() << "Dynamic Relocations {\n"; W.indent(); - if (DynRelaRegion.Size > 0) { - for (const Elf_Rela &Rela : this->dumper()->dyn_relas()) - printDynamicRelocation(Rela); - } - if (DynRelRegion.Size > 0) { - for (const Elf_Rel &Rel : this->dumper()->dyn_rels()) - printDynamicRelocation(Rel); - } - - if (DynRelrRegion.Size > 0) { - Elf_Relr_Range Relrs = this->dumper()->dyn_relrs(); - for (const Elf_Rel &R : this->Obj.decode_relrs(Relrs)) - printDynamicRelocation(R); - } - if (DynPLTRelRegion.EntSize == sizeof(Elf_Rela)) - for (const Elf_Rela &Rela : DynPLTRelRegion.getAsArrayRef()) - printDynamicRelocation(Rela); - else - for (const Elf_Rel &Rel : DynPLTRelRegion.getAsArrayRef()) - printDynamicRelocation(Rel); - + this->printDynamicRelocationsHelper(); W.unindent(); W.startLine() << "}\n"; }