[llvm] 8f576a7 - [llvm-readobj] - Simplify findSectionByName(). NFCI.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 4 01:23:23 PDT 2020


Author: Georgii Rymar
Date: 2020-08-04T11:13:02+03:00
New Revision: 8f576a75661668594ff0ac795e31cdb8df90780b

URL: https://github.com/llvm/llvm-project/commit/8f576a75661668594ff0ac795e31cdb8df90780b
DIFF: https://github.com/llvm/llvm-project/commit/8f576a75661668594ff0ac795e31cdb8df90780b.diff

LOG: [llvm-readobj] - Simplify findSectionByName(). NFCI.

It turns out that findSectionByName can return
const Elf_Shdr * instead of Expected<>, because its
code never returns an error currently (it reports warnings instead).

Differential revision: https://reviews.llvm.org/D85135

Added: 
    

Modified: 
    llvm/tools/llvm-readobj/ELFDumper.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index 2edca5aaa0b9..046ade9abf86 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -353,7 +353,7 @@ template <typename ELFT> class ELFDumper : public ObjDumper {
   void printSymbolsHelper(bool IsDynamic) const;
   std::string getDynamicEntry(uint64_t Type, uint64_t Value) const;
 
-  Expected<const typename ELFT::Shdr *> findSectionByName(StringRef Name) const;
+  const Elf_Shdr *findSectionByName(StringRef Name) const;
 
   const Elf_Shdr *getDotSymtabSec() const { return DotSymtabSec; }
   const Elf_Shdr *getDotCGProfileSec() const { return DotCGProfileSec; }
@@ -2454,7 +2454,7 @@ void printFlags(T Value, ArrayRef<EnumEntry<TFlag>> Flags, raw_ostream &OS) {
 }
 
 template <class ELFT>
-Expected<const typename ELFT::Shdr *>
+const typename ELFT::Shdr *
 ELFDumper<ELFT>::findSectionByName(StringRef Name) const {
   const ELFFile<ELFT> *Obj = ObjF->getELFFile();
   for (const Elf_Shdr &Shdr : cantFail(Obj->sections())) {
@@ -3009,12 +3009,7 @@ Error MipsGOTParser<ELFT>::findGOT(Elf_Dyn_Range DynTable,
 
   // Find static GOT secton.
   if (IsStatic) {
-    Expected<const Elf_Shdr *> GotOrErr = Dumper.findSectionByName(".got");
-    if (!GotOrErr)
-      return GotOrErr.takeError();
-    else
-      GotSec = *GotOrErr;
-
+    GotSec = Dumper.findSectionByName(".got");
     if (!GotSec)
       return Error::success();
 
@@ -3333,19 +3328,14 @@ static void printMipsReginfoData(ScopedPrinter &W,
 
 template <class ELFT> void ELFDumper<ELFT>::printMipsReginfo() {
   const ELFFile<ELFT> *Obj = ObjF->getELFFile();
-  Expected<const Elf_Shdr *> RegInfoOrErr = findSectionByName(".reginfo");
-  if (!RegInfoOrErr) {
-    reportUniqueWarning(RegInfoOrErr.takeError());
-    return;
-  }
-
-  if ((*RegInfoOrErr) == nullptr) {
+  const Elf_Shdr *RegInfo = findSectionByName(".reginfo");
+  if (!RegInfo) {
     W.startLine() << "There is no .reginfo section in the file.\n";
     return;
   }
 
   ArrayRef<uint8_t> Sec = unwrapOrError(ObjF->getFileName(),
-                                        Obj->getSectionContents(*RegInfoOrErr));
+                                        Obj->getSectionContents(RegInfo));
   if (Sec.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) {
     W.startLine() << "The .reginfo section has a wrong size.\n";
     return;
@@ -3358,21 +3348,16 @@ template <class ELFT> void ELFDumper<ELFT>::printMipsReginfo() {
 
 template <class ELFT> void ELFDumper<ELFT>::printMipsOptions() {
   const ELFFile<ELFT> *Obj = ObjF->getELFFile();
-  Expected<const Elf_Shdr *> MipsOptOrErr = findSectionByName(".MIPS.options");
-  if (!MipsOptOrErr) {
-    reportUniqueWarning(MipsOptOrErr.takeError());
-    return;
-  }
-
-  if ((*MipsOptOrErr) == nullptr) {
+  const Elf_Shdr *MipsOpts = findSectionByName(".MIPS.options");
+  if (!MipsOpts) {
     W.startLine() << "There is no .MIPS.options section in the file.\n";
     return;
   }
 
   DictScope GS(W, "MIPS Options");
 
-  ArrayRef<uint8_t> Sec = unwrapOrError(ObjF->getFileName(),
-                                        Obj->getSectionContents(*MipsOptOrErr));
+  ArrayRef<uint8_t> Sec =
+      unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(MipsOpts));
   while (!Sec.empty()) {
     if (Sec.size() < sizeof(Elf_Mips_Options<ELFT>)) {
       W.startLine() << "The .MIPS.options section has a wrong size.\n";
@@ -5982,16 +5967,13 @@ template <class ELFT>
 Expected<const Elf_Mips_ABIFlags<ELFT> *>
 getMipsAbiFlagsSection(const ELFObjectFile<ELFT> *ObjF,
                        const ELFDumper<ELFT> &Dumper) {
-  Expected<const typename ELFT::Shdr *> SecOrErr =
-      Dumper.findSectionByName(".MIPS.abiflags");
-  if (!SecOrErr)
-    return SecOrErr.takeError();
-  if (*SecOrErr == nullptr)
+  const typename ELFT::Shdr *Sec = Dumper.findSectionByName(".MIPS.abiflags");
+  if (Sec == nullptr)
     return nullptr;
 
   const ELFFile<ELFT> *Obj = ObjF->getELFFile();
   constexpr StringRef ErrPrefix = "unable to read the .MIPS.abiflags section: ";
-  Expected<ArrayRef<uint8_t>> DataOrErr = Obj->getSectionContents(*SecOrErr);
+  Expected<ArrayRef<uint8_t>> DataOrErr = Obj->getSectionContents(Sec);
   if (!DataOrErr)
     return createError(ErrPrefix + toString(DataOrErr.takeError()));
 


        


More information about the llvm-commits mailing list