[llvm] r285874 - Split getSection in two.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 2 19:25:00 PDT 2016


Author: rafael
Date: Wed Nov  2 21:24:59 2016
New Revision: 285874

URL: http://llvm.org/viewvc/llvm-project?rev=285874&view=rev
Log:
Split getSection in two.

This will allow avoiding repeated error checking in a few cases.

Modified:
    llvm/trunk/include/llvm/Object/ELF.h

Modified: llvm/trunk/include/llvm/Object/ELF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELF.h?rev=285874&r1=285873&r2=285874&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELF.h (original)
+++ llvm/trunk/include/llvm/Object/ELF.h Wed Nov  2 21:24:59 2016
@@ -179,6 +179,14 @@ typedef ELFFile<ELFType<support::big, fa
 typedef ELFFile<ELFType<support::big, true>> ELF64BEFile;
 
 template <class ELFT>
+inline ErrorOr<const typename ELFT::Shdr *>
+getSection(typename ELFT::ShdrRange Sections, uint32_t Index) {
+  if (Index >= Sections.size())
+    return object_error::invalid_section_index;
+  return &Sections[Index];
+}
+
+template <class ELFT>
 uint32_t ELFFile<ELFT>::getExtendedSymbolTableIndex(
     const Elf_Sym *Sym, const Elf_Shdr *SymTab,
     ArrayRef<Elf_Word> ShndxTable) const {
@@ -202,12 +210,17 @@ ErrorOr<const typename ELFT::Shdr *>
 ELFFile<ELFT>::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
                           ArrayRef<Elf_Word> ShndxTable) const {
   uint32_t Index = Sym->st_shndx;
+  if (Index == ELF::SHN_UNDEF ||
+      (Index >= ELF::SHN_LORESERVE && Index != ELF::SHN_XINDEX))
+    return nullptr;
+
   if (Index == ELF::SHN_XINDEX)
-    return getSection(getExtendedSymbolTableIndex(Sym, SymTab, ShndxTable));
+    Index = getExtendedSymbolTableIndex(Sym, SymTab, ShndxTable);
 
-  if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
-    return nullptr;
-  return getSection(Sym->st_shndx);
+  auto SectionsOrErr = sections();
+  if (std::error_code EC = SectionsOrErr.getError())
+    return EC;
+  return object::getSection<ELFT>(*SectionsOrErr, Index);
 }
 
 template <class ELFT>
@@ -387,10 +400,7 @@ ELFFile<ELFT>::getSection(uint32_t Index
   auto TableOrErr = sections();
   if (std::error_code EC = TableOrErr.getError())
     return EC;
-  ArrayRef<Elf_Shdr> Table = *TableOrErr;
-  if (Index >= Table.size())
-    return object_error::invalid_section_index;
-  return &Table[Index];
+  return object::getSection<ELFT>(*TableOrErr, Index);
 }
 
 template <class ELFT>




More information about the llvm-commits mailing list