]> granicus.if.org Git - llvm/commitdiff
Split out a getSectionIndex.
authorRafael Espindola <rafael.espindola@gmail.com>
Thu, 3 Nov 2016 14:24:53 +0000 (14:24 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Thu, 3 Nov 2016 14:24:53 +0000 (14:24 +0000)
That code is currently duplicated in lld.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@285915 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Object/ELF.h

index 9420b1e7bd53972033057b964ca46c905f468fb2..27cd7145a3e96dc15b8382a4283327220fcbf8b5 100644 (file)
@@ -154,6 +154,8 @@ public:
   getExtendedSymbolTableIndex(const Elf_Sym *Sym, const Elf_Sym *FirstSym,
                               ArrayRef<Elf_Word> ShndxTable) const;
   const Elf_Ehdr *getHeader() const { return Header; }
+  ErrorOr<uint32_t> getSectionIndex(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
+                                    ArrayRef<Elf_Word> ShndxTable) const;
   ErrorOr<const Elf_Shdr *> getSection(const Elf_Sym *Sym,
                                        const Elf_Shdr *SymTab,
                                        ArrayRef<Elf_Word> ShndxTable) const;
@@ -214,21 +216,31 @@ ErrorOr<uint32_t> ELFFile<ELFT>::getExtendedSymbolTableIndex(
 }
 
 template <class ELFT>
-ErrorOr<const typename ELFT::Shdr *>
-ELFFile<ELFT>::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
-                          ArrayRef<Elf_Word> ShndxTable) const {
+ErrorOr<uint32_t>
+ELFFile<ELFT>::getSectionIndex(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) {
     auto ErrorOrIndex = getExtendedSymbolTableIndex(Sym, SymTab, ShndxTable);
     if (std::error_code EC = ErrorOrIndex.getError())
       return EC;
-    Index = *ErrorOrIndex;
+    return *ErrorOrIndex;
   }
+  if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
+    return 0;
+  return Index;
+}
 
+template <class ELFT>
+ErrorOr<const typename ELFT::Shdr *>
+ELFFile<ELFT>::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
+                          ArrayRef<Elf_Word> ShndxTable) const {
+  ErrorOr<uint32_t> IndexOrErr = getSectionIndex(Sym, SymTab, ShndxTable);
+  if (std::error_code EC = IndexOrErr.getError())
+    return EC;
+  uint32_t Index = *IndexOrErr;
+  if (Index == 0)
+    return nullptr;
   auto SectionsOrErr = sections();
   if (std::error_code EC = SectionsOrErr.getError())
     return EC;