Skip to content

Commit

Permalink
Split out a getSectionIndex.
Browse files Browse the repository at this point in the history
That code is currently duplicated in lld.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@285915 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
espindola committed Nov 3, 2016
1 parent 6300980 commit 58c4c96
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions include/llvm/Object/ELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ class ELFFile {
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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 58c4c96

Please sign in to comment.