From b1a3c0d428b1368c1fa4f0d03681d026d689ae34 Mon Sep 17 00:00:00 2001 From: Shankar Easwaran Date: Tue, 13 Nov 2012 18:26:22 +0000 Subject: [PATCH] Adding convenience function to ELF Header llvm-svn: 167852 --- llvm/include/llvm/Object/ELF.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/llvm/include/llvm/Object/ELF.h b/llvm/include/llvm/Object/ELF.h index 466de93..a2723c7 100644 --- a/llvm/include/llvm/Object/ELF.h +++ b/llvm/include/llvm/Object/ELF.h @@ -609,6 +609,7 @@ public: const Elf_Dyn *getDyn(DataRefImpl DynData) const; error_code getSymbolVersion(SymbolRef Symb, StringRef &Version, bool &IsDefault) const; + uint64_t getSymbolIndex(const Elf_Sym *sym) const; protected: virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const; virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const; @@ -2094,6 +2095,21 @@ ELFObjectFile::ELFObjectFile(MemoryBuffer *Object } } +// Get the symbol table index in the symtab section given a symbol +template +uint64_t ELFObjectFile + ::getSymbolIndex(const Elf_Sym *Sym) const { + assert(SymbolTableSections.size() == 1 && "Only one symbol table supported!"); + const Elf_Shdr *SymTab = *SymbolTableSections.begin(); + uintptr_t SymLoc = uintptr_t(Sym); + uintptr_t SymTabLoc = uintptr_t(base() + SymTab->sh_offset); + assert(SymLoc > SymTabLoc && "Symbol not in symbol table!"); + uint64_t SymOffset = SymLoc - SymTabLoc; + assert(SymOffset % SymTab->sh_entsize == 0 && + "Symbol not multiple of symbol size!"); + return SymOffset / SymTab->sh_entsize; +} + template symbol_iterator ELFObjectFile ::begin_symbols() const { -- 2.7.4