Index: llvm/tools/llvm-readobj/ELFDumper.cpp =================================================================== --- llvm/tools/llvm-readobj/ELFDumper.cpp +++ llvm/tools/llvm-readobj/ELFDumper.cpp @@ -781,7 +781,12 @@ 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 forEachRelocationDo( + const Elf_Shdr &Sec, bool RawRelr, + llvm::function_ref &, unsigned, + const Elf_Shdr &, const Elf_Shdr *)> + RelRelaFn, + llvm::function_ref RelrFn); void printDynamicRelocationsHelper(); virtual void printDynamicRelocHeader(unsigned Type, StringRef Name, const DynRegionInfo &Reg){}; @@ -3782,7 +3787,11 @@ << to_hexString(Offset, false) << " contains " << EntriesNum << " entries:\n"; printRelocHeaderFields(OS, Sec.sh_type); - this->printRelocationsHelper(Sec); + this->forEachRelocationDo( + Sec, opts::RawRelr, + [&](const Relocation &R, unsigned Ndx, const Elf_Shdr &Sec, + const Elf_Shdr *SymTab) { printReloc(R, Ndx, Sec, SymTab); }, + [&](const Elf_Relr &R) { printRelrReloc(R); }); } if (!HasRelocSections) OS << "\nThere are no relocations in this file.\n"; @@ -5610,7 +5619,12 @@ } template -void DumpStyle::printRelocationsHelper(const Elf_Shdr &Sec) { +void DumpStyle::forEachRelocationDo( + const Elf_Shdr &Sec, bool RawRelr, + llvm::function_ref &, unsigned, + const Elf_Shdr &, const Elf_Shdr *)> + RelRelaFn, + llvm::function_ref RelrFn) { auto Warn = [&](Error &&E, const Twine &Prefix = "unable to read relocations from") { this->reportUniqueWarning(createError(Prefix + " " + describe(Obj, Sec) + @@ -5636,7 +5650,7 @@ case ELF::SHT_REL: if (Expected RangeOrErr = Obj.rels(Sec)) { for (const Elf_Rel &R : *RangeOrErr) - printReloc(Relocation(R, IsMips64EL), ++RelNdx, Sec, SymTab); + RelRelaFn(Relocation(R, IsMips64EL), ++RelNdx, Sec, SymTab); } else { Warn(RangeOrErr.takeError()); } @@ -5644,7 +5658,7 @@ case ELF::SHT_RELA: if (Expected RangeOrErr = Obj.relas(Sec)) { for (const Elf_Rela &R : *RangeOrErr) - printReloc(Relocation(R, IsMips64EL), ++RelNdx, Sec, SymTab); + RelRelaFn(Relocation(R, IsMips64EL), ++RelNdx, Sec, SymTab); } else { Warn(RangeOrErr.takeError()); } @@ -5656,22 +5670,22 @@ Warn(RangeOrErr.takeError()); break; } - if (opts::RawRelr) { + if (RawRelr) { for (const Elf_Relr &R : *RangeOrErr) - printRelrReloc(R); + RelrFn(R); break; } for (const Elf_Rel &R : Obj.decode_relrs(*RangeOrErr)) - printReloc(Relocation(R, IsMips64EL), ++RelNdx, Sec, - /*SymTab=*/nullptr); + RelRelaFn(Relocation(R, IsMips64EL), ++RelNdx, Sec, + /*SymTab=*/nullptr); break; } case ELF::SHT_ANDROID_REL: case ELF::SHT_ANDROID_RELA: if (Expected> RelasOrErr = Obj.android_relas(Sec)) { for (const Elf_Rela &R : *RelasOrErr) - printReloc(Relocation(R, IsMips64EL), ++RelNdx, Sec, SymTab); + RelRelaFn(Relocation(R, IsMips64EL), ++RelNdx, Sec, SymTab); } else { Warn(RelasOrErr.takeError()); } @@ -6275,7 +6289,11 @@ unsigned SecNdx = &Sec - &cantFail(this->Obj.sections()).front(); W.startLine() << "Section (" << SecNdx << ") " << Name << " {\n"; W.indent(); - this->printRelocationsHelper(Sec); + this->forEachRelocationDo( + Sec, opts::RawRelr, + [&](const Relocation &R, unsigned Ndx, const Elf_Shdr &Sec, + const Elf_Shdr *SymTab) { printReloc(R, Ndx, Sec, SymTab); }, + [&](const Elf_Relr &R) { printRelrReloc(R); }); W.unindent(); W.startLine() << "}\n"; } @@ -6346,7 +6364,11 @@ if (opts::SectionRelocations) { ListScope D(W, "Relocations"); - this->printRelocationsHelper(Sec); + this->forEachRelocationDo( + Sec, opts::RawRelr, + [&](const Relocation &R, unsigned Ndx, const Elf_Shdr &Sec, + const Elf_Shdr *SymTab) { printReloc(R, Ndx, Sec, SymTab); }, + [&](const Elf_Relr &R) { printRelrReloc(R); }); } if (opts::SectionSymbols) {