[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
This commit is contained in:
Georgii Rymar 2020-09-07 15:30:38 +03:00
parent 6670f5d1e6
commit dbb8188195

View file

@ -774,7 +774,11 @@ protected:
virtual void printReloc(const Relocation<ELFT> &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<ELFT> &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<StringRef> StrTable, bool IsDynamic,
bool NonVisibilityBitsUsed) override;
void printDynamicRelocHeader(unsigned Type, StringRef Name,
const DynRegionInfo &Reg) override;
void printDynamicReloc(const Relocation<ELFT> &R) override;
std::string getSymbolSectionNdx(const Elf_Sym *Symbol,
const Elf_Sym *FirstSym);
void printDynamicRelocation(const Relocation<ELFT> &R);
void printProgramHeaders();
void printSectionMapping();
void printGNUVersionSectionProlog(const typename ELFT::Shdr *Sec,
@ -951,8 +958,7 @@ private:
void printReloc(const Relocation<ELFT> &R, unsigned RelIndex,
const Elf_Shdr &Sec, const Elf_Shdr *SymTab) override;
void printRelrReloc(const Elf_Relr &R) override;
void printDynamicRelocation(const Relocation<ELFT> &R);
void printDynamicReloc(const Relocation<ELFT> &R) override;
void printSymbols();
void printDynamicSymbols();
@ -3694,10 +3700,9 @@ static void printRelocHeaderFields(formatted_raw_ostream &OS, unsigned SType) {
}
template <class ELFT>
static void printDynamicRelocHeader(const ELFFile<ELFT> &Obj,
formatted_raw_ostream &OS, unsigned Type,
StringRef Name, const DynRegionInfo &Reg) {
uint64_t Offset = Reg.Addr - Obj.base();
void GNUStyle<ELFT>::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<ELFT>(OS, Type);
@ -4376,7 +4381,7 @@ RelSymbol<ELFT> getSymbolForReloc(const ELFFile<ELFT> &Obj, StringRef FileName,
} // namespace
template <class ELFT>
void GNUStyle<ELFT>::printDynamicRelocation(const Relocation<ELFT> &R) {
void GNUStyle<ELFT>::printDynamicReloc(const Relocation<ELFT> &R) {
printRelRelaReloc(
R, getSymbolForReloc(this->Obj, this->FileName, this->dumper(), R));
}
@ -4424,39 +4429,43 @@ template <class ELFT> void GNUStyle<ELFT>::printDynamic() {
}
template <class ELFT> void GNUStyle<ELFT>::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 <class ELFT> void DumpStyle<ELFT>::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<ELFT>(Rela, IsMips64EL));
printDynamicReloc(Relocation<ELFT>(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<ELFT>(Rel, IsMips64EL));
printDynamicReloc(Relocation<ELFT>(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<ELFT>(R, IsMips64EL));
for (const Elf_Rel &Rel : Obj.decode_relrs(Relrs))
printDynamicReloc(Relocation<ELFT>(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<Elf_Rela>())
printDynamicRelocation(Relocation<ELFT>(Rela, IsMips64EL));
printDynamicReloc(Relocation<ELFT>(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<Elf_Rel>())
printDynamicRelocation(Relocation<ELFT>(Rel, IsMips64EL));
printDynamicReloc(Relocation<ELFT>(Rel, IsMips64EL));
}
}
}
@ -6344,41 +6353,15 @@ template <class ELFT> void LLVMStyle<ELFT>::printDynamic() {
}
template <class ELFT> void LLVMStyle<ELFT>::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<ELFT>(Rela, IsMips64EL));
}
if (DynRelRegion.Size > 0) {
for (const Elf_Rel &Rel : this->dumper()->dyn_rels())
printDynamicRelocation(Relocation<ELFT>(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<ELFT>(Rel, IsMips64EL));
}
if (DynPLTRelRegion.EntSize == sizeof(Elf_Rela))
for (const Elf_Rela &Rela : DynPLTRelRegion.getAsArrayRef<Elf_Rela>())
printDynamicRelocation(Relocation<ELFT>(Rela, IsMips64EL));
else
for (const Elf_Rel &Rel : DynPLTRelRegion.getAsArrayRef<Elf_Rel>())
printDynamicRelocation(Relocation<ELFT>(Rel, IsMips64EL));
this->printDynamicRelocationsHelper();
W.unindent();
W.startLine() << "}\n";
}
template <class ELFT>
void LLVMStyle<ELFT>::printDynamicRelocation(const Relocation<ELFT> &R) {
void LLVMStyle<ELFT>::printDynamicReloc(const Relocation<ELFT> &R) {
SmallString<32> RelocName;
this->Obj.getRelocationTypeName(R.Type, RelocName);
std::string SymbolName =