diff --git a/llvm/include/llvm/Object/ELF.h b/llvm/include/llvm/Object/ELF.h --- a/llvm/include/llvm/Object/ELF.h +++ b/llvm/include/llvm/Object/ELF.h @@ -14,6 +14,7 @@ #define LLVM_OBJECT_ELF_H #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/MapVector.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" @@ -392,6 +393,9 @@ Expected> getSectionContents(const Elf_Shdr &Sec) const; Expected> getSegmentContents(const Elf_Phdr &Phdr) const; Expected> decodeBBAddrMap(const Elf_Shdr &Sec) const; + Error getSectionAndRelocations( + std::function(const Elf_Shdr &)> IsMatch, + llvm::MapVector &SecToRelocMap) const; void createFakeSections(); }; diff --git a/llvm/lib/Object/ELF.cpp b/llvm/lib/Object/ELF.cpp --- a/llvm/lib/Object/ELF.cpp +++ b/llvm/lib/Object/ELF.cpp @@ -706,6 +706,43 @@ return FunctionEntries; } +template +Error ELFFile::getSectionAndRelocations( + std::function(const Elf_Shdr &)> IsMatch, + llvm::MapVector &SecToRelocMap) const { + Error IsSuccess = Error::success(); + for (const Elf_Shdr &Sec : cantFail(this->sections())) { + Expected DoesSectionMatch = IsMatch(Sec); + if(!DoesSectionMatch) + return joinErrors(std::move(IsSuccess), DoesSectionMatch.takeError()); + if (*DoesSectionMatch) + if (SecToRelocMap.insert(std::make_pair(&Sec, (const Elf_Shdr *)nullptr)) + .second) + continue; + + if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL) + continue; + + Expected RelSecOrErr = this->getSection(Sec.sh_info); + if (!RelSecOrErr) { + IsSuccess = joinErrors( + std::move(IsSuccess), + make_error(std::error_code(), + describe(*this, Sec) + + ": failed to get a relocated section: " + + toString(RelSecOrErr.takeError()))); + continue; + } + const Elf_Shdr *ContentsSec = *RelSecOrErr; + Expected DoesRelTargetMatch = IsMatch(*ContentsSec); + if(!DoesRelTargetMatch) + return DoesRelTargetMatch.takeError(); + if (*DoesRelTargetMatch) + SecToRelocMap[ContentsSec] = &Sec; + } + return IsSuccess; +} + template class llvm::object::ELFFile; template class llvm::object::ELFFile; template class llvm::object::ELFFile; 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 @@ -326,7 +326,7 @@ /// Retrieves sections with corresponding relocation sections based on /// IsMatch. void getSectionAndRelocations( - std::function IsMatch, + std::function(const Elf_Shdr &)> IsMatch, llvm::MapVector &SecToRelocMap); const object::ELFObjectFile &ObjF; @@ -6198,28 +6198,12 @@ template void ELFDumper::getSectionAndRelocations( - std::function IsMatch, + std::function(const Elf_Shdr &)> IsMatch, llvm::MapVector &SecToRelocMap) { - for (const Elf_Shdr &Sec : cantFail(Obj.sections())) { - if (IsMatch(Sec)) - if (SecToRelocMap.insert(std::make_pair(&Sec, (const Elf_Shdr *)nullptr)) - .second) - continue; - - if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL) - continue; - - Expected RelSecOrErr = Obj.getSection(Sec.sh_info); - if (!RelSecOrErr) { - reportUniqueWarning(describe(Sec) + - ": failed to get a relocated section: " + - toString(RelSecOrErr.takeError())); - continue; - } - const Elf_Shdr *ContentsSec = *RelSecOrErr; - if (IsMatch(*ContentsSec)) - SecToRelocMap[ContentsSec] = &Sec; - } + Error RecoverableErrors = Obj.getSectionAndRelocations(IsMatch, SecToRelocMap); + handleAllErrors(std::move(RecoverableErrors), [&](const StringError &SE) { + reportUniqueWarning(SE.getMessage()); + }); } template