[llvm] r241156 - Return ErrorOr from getSection.

Rafael Espindola rafael.espindola at gmail.com
Wed Jul 1 05:56:27 PDT 2015


Author: rafael
Date: Wed Jul  1 07:56:27 2015
New Revision: 241156

URL: http://llvm.org/viewvc/llvm-project?rev=241156&view=rev
Log:
Return ErrorOr from getSection.

This also improves the logic of what is an error:

* getSection(uint_32): only return an error if the index is out of bounds. The
  index 0 corresponds to a perfectly valid entry.
* getSection(Elf_Sym): Returns null for symbols that normally don't have
  sections and error for out of bound indexes.

In many places this just moves the report_fatal_error up the stack, but those
can then be fixed in smaller patches.

Modified:
    llvm/trunk/include/llvm/Object/ELF.h
    llvm/trunk/include/llvm/Object/ELFObjectFile.h
    llvm/trunk/include/llvm/Object/Error.h
    llvm/trunk/lib/Object/Error.cpp
    llvm/trunk/test/Object/invalid.test
    llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
    llvm/trunk/tools/llvm-readobj/ARMEHABIPrinter.h
    llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
    llvm/trunk/tools/obj2yaml/elf2yaml.cpp

Modified: llvm/trunk/include/llvm/Object/ELF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELF.h?rev=241156&r1=241155&r2=241156&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELF.h (original)
+++ llvm/trunk/include/llvm/Object/ELF.h Wed Jul  1 07:56:27 2015
@@ -357,8 +357,8 @@ public:
   uintX_t getStringTableIndex() const;
   ELF::Elf64_Word getExtendedSymbolTableIndex(const Elf_Sym *symb) const;
   const Elf_Ehdr *getHeader() const { return Header; }
-  const Elf_Shdr *getSection(const Elf_Sym *symb) const;
-  const Elf_Shdr *getSection(uint32_t Index) const;
+  ErrorOr<const Elf_Shdr *> getSection(const Elf_Sym *symb) const;
+  ErrorOr<const Elf_Shdr *> getSection(uint32_t Index) const;
   const Elf_Sym *getSymbol(uint32_t index) const;
 
   ErrorOr<StringRef> getStaticSymbolName(const Elf_Sym *Symb) const;
@@ -465,11 +465,12 @@ ELFFile<ELFT>::getExtendedSymbolTableInd
 }
 
 template <class ELFT>
-const typename ELFFile<ELFT>::Elf_Shdr *
+ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
 ELFFile<ELFT>::getSection(const Elf_Sym *symb) const {
-  if (symb->st_shndx == ELF::SHN_XINDEX)
+  uint32_t Index = symb->st_shndx;
+  if (Index == ELF::SHN_XINDEX)
     return getSection(ExtendedSymbolTable.lookup(symb));
-  if (symb->st_shndx >= ELF::SHN_LORESERVE)
+  if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
     return nullptr;
   return getSection(symb->st_shndx);
 }
@@ -532,7 +533,10 @@ std::pair<const typename ELFFile<ELFT>::
 ELFFile<ELFT>::getRelocationSymbol(const Elf_Shdr *Sec, const RelT *Rel) const {
   if (!Sec->sh_link)
     return std::make_pair(nullptr, nullptr);
-  const Elf_Shdr *SymTable = getSection(Sec->sh_link);
+  ErrorOr<const Elf_Shdr *> SymTableOrErr = getSection(Sec->sh_link);
+  if (std::error_code EC = SymTableOrErr.getError())
+    report_fatal_error(EC.message());
+  const Elf_Shdr *SymTable = *SymTableOrErr;
   return std::make_pair(
       SymTable, getEntry<Elf_Sym>(SymTable, Rel->getSymbol(isMips64EL())));
 }
@@ -615,7 +619,10 @@ ELFFile<ELFT>::ELFFile(StringRef Object,
         return;
       }
       dot_symtab_sec = &Sec;
-      ErrorOr<StringRef> SymtabOrErr = getStringTable(getSection(Sec.sh_link));
+      ErrorOr<const Elf_Shdr *> SectionOrErr = getSection(Sec.sh_link);
+      if ((EC = SectionOrErr.getError()))
+        return;
+      ErrorOr<StringRef> SymtabOrErr = getStringTable(*SectionOrErr);
       if ((EC = SymtabOrErr.getError()))
         return;
       DotStrtab = *SymtabOrErr;
@@ -629,7 +636,10 @@ ELFFile<ELFT>::ELFFile(StringRef Object,
       DynSymRegion.Addr = base() + Sec.sh_offset;
       DynSymRegion.Size = Sec.sh_size;
       DynSymRegion.EntSize = Sec.sh_entsize;
-      const Elf_Shdr *DynStr = getSection(Sec.sh_link);
+      ErrorOr<const Elf_Shdr *> DynStrOrErr = getSection(Sec.sh_link);
+      if ((EC = DynStrOrErr.getError()))
+        return;
+      const Elf_Shdr *DynStr = *DynStrOrErr;
       DynStrRegion.Addr = base() + DynStr->sh_offset;
       DynStrRegion.Size = DynStr->sh_size;
       DynStrRegion.EntSize = DynStr->sh_entsize;
@@ -673,8 +683,11 @@ ELFFile<ELFT>::ELFFile(StringRef Object,
   }
 
   // Get string table sections.
-  ErrorOr<StringRef> SymtabOrErr =
-      getStringTable(getSection(getStringTableIndex()));
+  ErrorOr<const Elf_Shdr *> StrTabSecOrErr = getSection(getStringTableIndex());
+  if ((EC = StrTabSecOrErr.getError()))
+    return;
+
+  ErrorOr<StringRef> SymtabOrErr = getStringTable(*StrTabSecOrErr);
   if ((EC = SymtabOrErr.getError()))
     return;
   DotShstrtab = *SymtabOrErr;
@@ -825,7 +838,10 @@ StringRef ELFFile<ELFT>::getLoadName() c
 template <class ELFT>
 template <typename T>
 const T *ELFFile<ELFT>::getEntry(uint32_t Section, uint32_t Entry) const {
-  return getEntry<T>(getSection(Section), Entry);
+  ErrorOr<const Elf_Shdr *> Sec = getSection(Section);
+  if (std::error_code EC = Sec.getError())
+    report_fatal_error(EC.message());
+  return getEntry<T>(*Sec, Entry);
 }
 
 template <class ELFT>
@@ -837,17 +853,15 @@ const T *ELFFile<ELFT>::getEntry(const E
 }
 
 template <class ELFT>
-const typename ELFFile<ELFT>::Elf_Shdr *
-ELFFile<ELFT>::getSection(uint32_t index) const {
-  if (index == 0)
-    return nullptr;
-  if (!SectionHeaderTable || index >= getNumSections())
-    // FIXME: Proper error handling.
-    report_fatal_error("Invalid section index!");
+ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
+ELFFile<ELFT>::getSection(uint32_t Index) const {
+  assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
+  if (Index >= getNumSections())
+    return object_error::invalid_section_index;
 
   return reinterpret_cast<const Elf_Shdr *>(
-         reinterpret_cast<const char *>(SectionHeaderTable)
-         + (index * Header->e_shentsize));
+      reinterpret_cast<const char *>(SectionHeaderTable) +
+      (Index * Header->e_shentsize));
 }
 
 template <class ELFT>

Modified: llvm/trunk/include/llvm/Object/ELFObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELFObjectFile.h?rev=241156&r1=241155&r2=241156&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELFObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ELFObjectFile.h Wed Jul  1 07:56:27 2015
@@ -241,7 +241,10 @@ protected:
 
   /// \brief Get the relocation section that contains \a Rel.
   const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
-    return EF.getSection(Rel.d.a);
+    ErrorOr<const Elf_Shdr *> Sec = EF.getSection(Rel.d.a);
+    if (std::error_code EC = Sec.getError())
+      report_fatal_error(EC.message());
+    return *Sec;
   }
 
   const Elf_Rel *getRel(DataRefImpl Rel) const;
@@ -406,8 +409,11 @@ std::error_code ELFObjectFile<ELFT>::get
   const Elf_Ehdr *Header = EF.getHeader();
 
   if (Header->e_type == ELF::ET_REL) {
-    const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
-    if (Section != nullptr)
+    ErrorOr<const Elf_Shdr *> SectionOrErr = EF.getSection(ESym);
+    if (std::error_code EC = SectionOrErr.getError())
+      return EC;
+    const Elf_Shdr *Section = *SectionOrErr;
+    if (Section)
       Result += Section->sh_addr;
   }
 
@@ -511,14 +517,17 @@ uint32_t ELFObjectFile<ELFT>::getSymbolF
 template <class ELFT>
 section_iterator
 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
-  const Elf_Shdr *ESec = EF.getSection(ESym);
+  ErrorOr<const Elf_Shdr *> ESecOrErr = EF.getSection(ESym);
+  if (std::error_code EC = ESecOrErr.getError())
+    report_fatal_error(EC.message());
+
+  const Elf_Shdr *ESec = *ESecOrErr;
   if (!ESec)
     return section_end();
-  else {
-    DataRefImpl Sec;
-    Sec.p = reinterpret_cast<intptr_t>(ESec);
-    return section_iterator(SectionRef(Sec, this));
-  }
+
+  DataRefImpl Sec;
+  Sec.p = reinterpret_cast<intptr_t>(ESec);
+  return section_iterator(SectionRef(Sec, this));
 }
 
 template <class ELFT>
@@ -629,8 +638,10 @@ ELFObjectFile<ELFT>::getRelocatedSection
   if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
     return section_end();
 
-  const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
-  return section_iterator(SectionRef(toDRI(R), this));
+  ErrorOr<const Elf_Shdr *> R = EF.getSection(EShdr->sh_info);
+  if (std::error_code EC = R.getError())
+    report_fatal_error(EC.message());
+  return section_iterator(SectionRef(toDRI(*R), this));
 }
 
 // Relocations
@@ -651,7 +662,10 @@ ELFObjectFile<ELFT>::getRelocationSymbol
   if (!symbolIdx)
     return symbol_end();
 
-  const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
+  ErrorOr<const Elf_Shdr *> SymSecOrErr = EF.getSection(sec->sh_link);
+  if (std::error_code EC = SymSecOrErr.getError())
+    report_fatal_error(EC.message());
+  const Elf_Shdr *SymSec = *SymSecOrErr;
 
   DataRefImpl SymbolData;
   switch (SymSec->sh_type) {
@@ -676,8 +690,11 @@ ELFObjectFile<ELFT>::getRelocationAddres
 
   if (Header->e_type == ELF::ET_REL) {
     const Elf_Shdr *RelocationSec = getRelSection(Rel);
-    const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
-    return ROffset + RelocatedSec->sh_addr;
+    ErrorOr<const Elf_Shdr *> RelocatedSec =
+        EF.getSection(RelocationSec->sh_info);
+    if (std::error_code EC = RelocatedSec.getError())
+      return EC;
+    return ROffset + (*RelocatedSec)->sh_addr;
   }
   return ROffset;
 }

Modified: llvm/trunk/include/llvm/Object/Error.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/Error.h?rev=241156&r1=241155&r2=241156&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/Error.h (original)
+++ llvm/trunk/include/llvm/Object/Error.h Wed Jul  1 07:56:27 2015
@@ -28,6 +28,7 @@ enum class object_error {
   parse_failed,
   unexpected_eof,
   string_table_non_null_end,
+  invalid_section_index,
   bitcode_section_not_found,
   macho_small_load_command,
   macho_load_segment_too_many_sections,

Modified: llvm/trunk/lib/Object/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/Error.cpp?rev=241156&r1=241155&r2=241156&view=diff
==============================================================================
--- llvm/trunk/lib/Object/Error.cpp (original)
+++ llvm/trunk/lib/Object/Error.cpp Wed Jul  1 07:56:27 2015
@@ -43,6 +43,8 @@ std::string _object_error_category::mess
     return "The end of the file was unexpectedly encountered";
   case object_error::string_table_non_null_end:
     return "String table must end with a null terminator";
+  case object_error::invalid_section_index:
+    return "Invalid section index";
   case object_error::bitcode_section_not_found:
     return "Bitcode section not found in object file";
   case object_error::macho_small_load_command:

Modified: llvm/trunk/test/Object/invalid.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/invalid.test?rev=241156&r1=241155&r2=241156&view=diff
==============================================================================
--- llvm/trunk/test/Object/invalid.test (original)
+++ llvm/trunk/test/Object/invalid.test Wed Jul  1 07:56:27 2015
@@ -40,7 +40,7 @@ INVALID-SYM-SIZE: Invalid symbol size
 
 RUN: not llvm-readobj -t %p/Inputs/invalid-section-index.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-INDEX %s
 
-INVALID-SECTION-INDEX: Invalid section index!
+INVALID-SECTION-INDEX: Invalid section index
 
 RUN: not llvm-readobj -s %p/Inputs/invalid-section-size.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-SIZE %s
 INVALID-SECTION-SIZE: Invalid section header size

Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=241156&r1=241155&r2=241156&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Wed Jul  1 07:56:27 2015
@@ -319,12 +319,20 @@ static std::error_code getRelocationValu
   typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
   const ELFFile<ELFT> &EF = *Obj->getELFFile();
 
-  const Elf_Shdr *sec = EF.getSection(Rel.d.a);
-  const Elf_Shdr *SymTab = EF.getSection(sec->sh_link);
+  ErrorOr<const Elf_Shdr *> SecOrErr = EF.getSection(Rel.d.a);
+  if (std::error_code EC = SecOrErr.getError())
+    return EC;
+  const Elf_Shdr *Sec = *SecOrErr;
+  ErrorOr<const Elf_Shdr *> SymTabOrErr = EF.getSection(Sec->sh_link);
+  if (std::error_code EC = SymTabOrErr.getError())
+    return EC;
+  const Elf_Shdr *SymTab = *SymTabOrErr;
   assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
          SymTab->sh_type == ELF::SHT_DYNSYM);
-  const Elf_Shdr *StrTabSec = EF.getSection(SymTab->sh_link);
-  ErrorOr<StringRef> StrTabOrErr = EF.getStringTable(StrTabSec);
+  ErrorOr<const Elf_Shdr *> StrTabSec = EF.getSection(SymTab->sh_link);
+  if (std::error_code EC = StrTabSec.getError())
+    return EC;
+  ErrorOr<StringRef> StrTabOrErr = EF.getStringTable(*StrTabSec);
   if (std::error_code EC = StrTabOrErr.getError())
     return EC;
   StringRef StrTab = *StrTabOrErr;
@@ -332,7 +340,7 @@ static std::error_code getRelocationValu
   StringRef res;
   int64_t addend = 0;
   uint16_t symbol_index = 0;
-  switch (sec->sh_type) {
+  switch (Sec->sh_type) {
   default:
     return object_error::parse_failed;
   case ELF::SHT_REL: {
@@ -349,11 +357,13 @@ static std::error_code getRelocationValu
   }
   }
   const Elf_Sym *symb =
-      EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
+      EF.template getEntry<Elf_Sym>(Sec->sh_link, symbol_index);
   StringRef Target;
-  const Elf_Shdr *SymSec = EF.getSection(symb);
+  ErrorOr<const Elf_Shdr *> SymSec = EF.getSection(symb);
+  if (std::error_code EC = SymSec.getError())
+    return EC;
   if (symb->getType() == ELF::STT_SECTION) {
-    ErrorOr<StringRef> SecName = EF.getSectionName(SymSec);
+    ErrorOr<StringRef> SecName = EF.getSectionName(*SymSec);
     if (std::error_code EC = SecName.getError())
       return EC;
     Target = *SecName;

Modified: llvm/trunk/tools/llvm-readobj/ARMEHABIPrinter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ARMEHABIPrinter.h?rev=241156&r1=241155&r2=241156&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ARMEHABIPrinter.h (original)
+++ llvm/trunk/tools/llvm-readobj/ARMEHABIPrinter.h Wed Jul  1 07:56:27 2015
@@ -377,7 +377,10 @@ PrinterContext<ET>::FindExceptionTable(u
           std::pair<const Elf_Shdr *, const Elf_Sym *> Symbol =
               ELF->getRelocationSymbol(&Sec, &RelA);
 
-          return ELF->getSection(Symbol.second);
+          ErrorOr<const Elf_Shdr *> Ret = ELF->getSection(Symbol.second);
+          if (std::error_code EC = Ret.getError())
+            report_fatal_error(EC.message());
+          return *Ret;
         }
       }
     }

Modified: llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ELFDumper.cpp?rev=241156&r1=241155&r2=241156&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ELFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/ELFDumper.cpp Wed Jul  1 07:56:27 2015
@@ -162,8 +162,9 @@ getSectionNameIndex(const ELFO &Obj, con
   else {
     if (SectionIndex == SHN_XINDEX)
       SectionIndex = Obj.getExtendedSymbolTableIndex(&*Symbol);
-    const typename ELFO::Elf_Shdr *Sec = Obj.getSection(SectionIndex);
-    SectionName = errorOrDefault(Obj.getSectionName(Sec));
+    ErrorOr<const typename ELFO::Elf_Shdr *> Sec = Obj.getSection(SectionIndex);
+    if (!error(Sec.getError()))
+      SectionName = errorOrDefault(Obj.getSectionName(*Sec));
   }
 }
 
@@ -644,7 +645,10 @@ void ELFDumper<ELFT>::printSections() {
     if (opts::SectionSymbols) {
       ListScope D(W, "Symbols");
       for (const typename ELFO::Elf_Sym &Sym : Obj->symbols()) {
-        if (Obj->getSection(&Sym) == &Sec)
+        ErrorOr<const Elf_Shdr *> SymSec = Obj->getSection(&Sym);
+        if (!SymSec)
+          continue;
+        if (*SymSec == &Sec)
           printSymbol(&Sym, false);
       }
     }
@@ -746,16 +750,20 @@ void ELFDumper<ELFT>::printRelocation(co
   std::pair<const Elf_Shdr *, const Elf_Sym *> Sym =
       Obj->getRelocationSymbol(Sec, &Rel);
   if (Sym.second && Sym.second->getType() == ELF::STT_SECTION) {
-    const Elf_Shdr *Sec = Obj->getSection(Sym.second);
-    ErrorOr<StringRef> SecName = Obj->getSectionName(Sec);
-    if (SecName)
-      TargetName = SecName.get();
+    ErrorOr<const Elf_Shdr *> Sec = Obj->getSection(Sym.second);
+    if (!error(Sec.getError())) {
+      ErrorOr<StringRef> SecName = Obj->getSectionName(*Sec);
+      if (SecName)
+        TargetName = SecName.get();
+    }
   } else if (Sym.first) {
     const Elf_Shdr *SymTable = Sym.first;
-    const Elf_Shdr *StrTableSec = Obj->getSection(SymTable->sh_link);
-    ErrorOr<StringRef> StrTableOrErr = Obj->getStringTable(StrTableSec);
-    if (!error(StrTableOrErr.getError()))
-      TargetName = errorOrDefault(Sym.second->getName(*StrTableOrErr));
+    ErrorOr<const Elf_Shdr *> StrTableSec = Obj->getSection(SymTable->sh_link);
+    if (!error(StrTableSec.getError())) {
+      ErrorOr<StringRef> StrTableOrErr = Obj->getStringTable(*StrTableSec);
+      if (!error(StrTableOrErr.getError()))
+        TargetName = errorOrDefault(Sym.second->getName(*StrTableOrErr));
+    }
   }
 
   if (opts::ExpandRelocs) {

Modified: llvm/trunk/tools/obj2yaml/elf2yaml.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/obj2yaml/elf2yaml.cpp?rev=241156&r1=241155&r2=241156&view=diff
==============================================================================
--- llvm/trunk/tools/obj2yaml/elf2yaml.cpp (original)
+++ llvm/trunk/tools/obj2yaml/elf2yaml.cpp Wed Jul  1 07:56:27 2015
@@ -157,7 +157,10 @@ std::error_code ELFDumper<ELFT>::dumpSym
     return EC;
   S.Name = NameOrErr.get();
 
-  const Elf_Shdr *Shdr = Obj.getSection(&*Sym);
+  ErrorOr<const Elf_Shdr *> ShdrOrErr = Obj.getSection(&*Sym);
+  if (std::error_code EC = ShdrOrErr.getError())
+    return EC;
+  const Elf_Shdr *Shdr = *ShdrOrErr;
   if (!Shdr)
     return obj2yaml_error::success;
 
@@ -183,8 +186,10 @@ std::error_code ELFDumper<ELFT>::dumpRel
     return obj2yaml_error::success;
 
   const Elf_Shdr *SymTab = NamePair.first;
-  const Elf_Shdr *StrTabSec = Obj.getSection(SymTab->sh_link);
-  ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(StrTabSec);
+  ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
+  if (std::error_code EC = StrTabSec.getError())
+    return EC;
+  ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
   if (std::error_code EC = StrTabOrErr.getError())
     return EC;
   StringRef StrTab = *StrTabOrErr;
@@ -211,12 +216,13 @@ std::error_code ELFDumper<ELFT>::dumpCom
   S.Name = NameOrErr.get();
 
   if (Shdr->sh_link != ELF::SHN_UNDEF) {
-    if (const Elf_Shdr *LinkSection = Obj.getSection(Shdr->sh_link)) {
-      NameOrErr = Obj.getSectionName(LinkSection);
-      if (std::error_code EC = NameOrErr.getError())
-        return EC;
-      S.Link = NameOrErr.get();
-    }
+    ErrorOr<const Elf_Shdr *> LinkSection = Obj.getSection(Shdr->sh_link);
+    if (std::error_code EC = LinkSection.getError())
+      return EC;
+    NameOrErr = Obj.getSectionName(*LinkSection);
+    if (std::error_code EC = NameOrErr.getError())
+      return EC;
+    S.Link = NameOrErr.get();
   }
 
   return obj2yaml_error::success;
@@ -229,12 +235,14 @@ ELFDumper<ELFT>::dumpCommonRelocationSec
   if (std::error_code EC = dumpCommonSection(Shdr, S))
     return EC;
 
-  if (const Elf_Shdr *InfoSection = Obj.getSection(Shdr->sh_info)) {
-    ErrorOr<StringRef> NameOrErr = Obj.getSectionName(InfoSection);
-    if (std::error_code EC = NameOrErr.getError())
-      return EC;
-    S.Info = NameOrErr.get();
-  }
+  ErrorOr<const Elf_Shdr *> InfoSection = Obj.getSection(Shdr->sh_info);
+  if (std::error_code EC = InfoSection.getError())
+    return EC;
+
+  ErrorOr<StringRef> NameOrErr = Obj.getSectionName(*InfoSection);
+  if (std::error_code EC = NameOrErr.getError())
+    return EC;
+  S.Info = NameOrErr.get();
 
   return obj2yaml_error::success;
 }
@@ -304,9 +312,13 @@ ErrorOr<ELFYAML::Group *> ELFDumper<ELFT
     return EC;
   // Get sh_info which is the signature.
   const Elf_Sym *symbol = Obj.getSymbol(Shdr->sh_info);
-  const Elf_Shdr *symtab = Obj.getSection(Shdr->sh_link);
-  const Elf_Shdr *StrTabSec = Obj.getSection(symtab->sh_link);
-  ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(StrTabSec);
+  ErrorOr<const Elf_Shdr *> Symtab = Obj.getSection(Shdr->sh_link);
+  if (std::error_code EC = Symtab.getError())
+    return EC;
+  ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection((*Symtab)->sh_link);
+  if (std::error_code EC = StrTabSec.getError())
+    return EC;
+  ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
   if (std::error_code EC = StrTabOrErr.getError())
     return EC;
   StringRef StrTab = *StrTabOrErr;
@@ -325,8 +337,10 @@ ErrorOr<ELFYAML::Group *> ELFDumper<ELFT
     if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
       s.sectionNameOrType = "GRP_COMDAT";
     } else {
-      const Elf_Shdr *sHdr = Obj.getSection(groupMembers[i]);
-      ErrorOr<StringRef> sectionName = Obj.getSectionName(sHdr);
+      ErrorOr<const Elf_Shdr *> sHdr = Obj.getSection(groupMembers[i]);
+      if (std::error_code EC = sHdr.getError())
+        return EC;
+      ErrorOr<StringRef> sectionName = Obj.getSectionName(*sHdr);
       if (std::error_code ec = sectionName.getError())
         return ec;
       s.sectionNameOrType = *sectionName;





More information about the llvm-commits mailing list