From d1f0655afc21154c004d1e1f0f27b9d51d7bb381 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 3 Nov 2016 02:24:59 +0000 Subject: [PATCH] Split getSection in two. This will allow avoiding repeated error checking in a few cases. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@285874 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/ELF.h | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index b23ec6cc319..71f30ab16dd 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -178,6 +178,14 @@ typedef ELFFile> ELF64LEFile; typedef ELFFile> ELF32BEFile; typedef ELFFile> ELF64BEFile; +template +inline ErrorOr +getSection(typename ELFT::ShdrRange Sections, uint32_t Index) { + if (Index >= Sections.size()) + return object_error::invalid_section_index; + return &Sections[Index]; +} + template uint32_t ELFFile::getExtendedSymbolTableIndex( const Elf_Sym *Sym, const Elf_Shdr *SymTab, @@ -202,12 +210,17 @@ ErrorOr ELFFile::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab, ArrayRef 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(*SectionsOrErr, Index); } template @@ -387,10 +400,7 @@ ELFFile::getSection(uint32_t Index) const { auto TableOrErr = sections(); if (std::error_code EC = TableOrErr.getError()) return EC; - ArrayRef Table = *TableOrErr; - if (Index >= Table.size()) - return object_error::invalid_section_index; - return &Table[Index]; + return object::getSection(*TableOrErr, Index); } template -- 2.40.0