Skip to content

Commit

Permalink
Split getSection in two.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
espindola committed Nov 3, 2016
1 parent 6e15e36 commit d1f0655
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions include/llvm/Object/ELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ typedef ELFFile<ELFType<support::little, true>> ELF64LEFile;
typedef ELFFile<ELFType<support::big, false>> ELF32BEFile;
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,
Expand All @@ -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>
Expand Down Expand Up @@ -387,10 +400,7 @@ ELFFile<ELFT>::getSection(uint32_t Index) const {
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>
Expand Down

0 comments on commit d1f0655

Please sign in to comment.