[llvm-branch-commits] [llvm] c1f87dc - Refactored getSectionAndRelocations
Aiden Grossman via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Feb 25 02:41:29 PST 2023
Author: Aiden Grossman
Date: 2023-02-25T10:30:32Z
New Revision: c1f87dca0e8e13964ccd04718792da5cf4b3ca49
URL: https://github.com/llvm/llvm-project/commit/c1f87dca0e8e13964ccd04718792da5cf4b3ca49
DIFF: https://github.com/llvm/llvm-project/commit/c1f87dca0e8e13964ccd04718792da5cf4b3ca49.diff
LOG: Refactored getSectionAndRelocations
Added:
Modified:
llvm/include/llvm/Object/ELF.h
llvm/lib/Object/ELF.cpp
llvm/tools/llvm-readobj/ELFDumper.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Object/ELF.h b/llvm/include/llvm/Object/ELF.h
index 1664ff96542f..aaa0418b9ffa 100644
--- 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 @@ class ELFFile {
Expected<ArrayRef<uint8_t>> getSectionContents(const Elf_Shdr &Sec) const;
Expected<ArrayRef<uint8_t>> getSegmentContents(const Elf_Phdr &Phdr) const;
Expected<std::vector<BBAddrMap>> decodeBBAddrMap(const Elf_Shdr &Sec) const;
+ Error getSectionAndRelocations(
+ std::function<Expected<bool>(const Elf_Shdr &)> IsMatch,
+ llvm::MapVector<const Elf_Shdr *, const Elf_Shdr *> &SecToRelocMap) const;
void createFakeSections();
};
diff --git a/llvm/lib/Object/ELF.cpp b/llvm/lib/Object/ELF.cpp
index 81c9a097170d..c6a9e13e4265 100644
--- a/llvm/lib/Object/ELF.cpp
+++ b/llvm/lib/Object/ELF.cpp
@@ -706,6 +706,43 @@ ELFFile<ELFT>::decodeBBAddrMap(const Elf_Shdr &Sec) const {
return FunctionEntries;
}
+template <class ELFT>
+Error ELFFile<ELFT>::getSectionAndRelocations(
+ std::function<Expected<bool>(const Elf_Shdr &)> IsMatch,
+ llvm::MapVector<const Elf_Shdr *, const Elf_Shdr *> &SecToRelocMap) const {
+ Error IsSuccess = Error::success();
+ for (const Elf_Shdr &Sec : cantFail(this->sections())) {
+ Expected<bool> 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<const Elf_Shdr *> RelSecOrErr = this->getSection(Sec.sh_info);
+ if (!RelSecOrErr) {
+ IsSuccess = joinErrors(
+ std::move(IsSuccess),
+ make_error<StringError>(std::error_code(),
+ describe(*this, Sec) +
+ ": failed to get a relocated section: " +
+ toString(RelSecOrErr.takeError())));
+ continue;
+ }
+ const Elf_Shdr *ContentsSec = *RelSecOrErr;
+ Expected<bool> DoesRelTargetMatch = IsMatch(*ContentsSec);
+ if(!DoesRelTargetMatch)
+ return DoesRelTargetMatch.takeError();
+ if (*DoesRelTargetMatch)
+ SecToRelocMap[ContentsSec] = &Sec;
+ }
+ return IsSuccess;
+}
+
template class llvm::object::ELFFile<ELF32LE>;
template class llvm::object::ELFFile<ELF32BE>;
template class llvm::object::ELFFile<ELF64LE>;
diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index 45fff0cc4a76..37394ae60572 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -326,7 +326,7 @@ template <typename ELFT> class ELFDumper : public ObjDumper {
/// Retrieves sections with corresponding relocation sections based on
/// IsMatch.
void getSectionAndRelocations(
- std::function<bool(const Elf_Shdr &)> IsMatch,
+ std::function<Expected<bool>(const Elf_Shdr &)> IsMatch,
llvm::MapVector<const Elf_Shdr *, const Elf_Shdr *> &SecToRelocMap);
const object::ELFObjectFile<ELFT> &ObjF;
@@ -6198,28 +6198,12 @@ void ELFDumper<ELFT>::printNonRelocatableStackSizes(
template <class ELFT>
void ELFDumper<ELFT>::getSectionAndRelocations(
- std::function<bool(const Elf_Shdr &)> IsMatch,
+ std::function<Expected<bool>(const Elf_Shdr &)> IsMatch,
llvm::MapVector<const Elf_Shdr *, const Elf_Shdr *> &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<const Elf_Shdr *> 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 <class ELFT>
More information about the llvm-branch-commits
mailing list