[llvm-commits] Patch to add a convenience function in ELF.h

Michael Spencer bigcheesegs at gmail.com
Fri Nov 2 16:09:37 PDT 2012


On Thu, Nov 1, 2012 at 7:26 AM, Shankar Easwaran
<shankare at codeaurora.org> wrote:
> Hi,
>
> This is a patch to add a convenience function in ELF.h to add support for
> getting to the symbol table index given a Elf_Sym.
>
> Thanks
>
> Shankar Easwaran
>
> --
> Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by
> the Linux Foundation
>

There's no need to use a loop to get the index.

@@ -604,6 +604,10 @@ public:
   error_code      getSymbolName(const Elf_Shdr *section,
                                 const Elf_Sym *Symb,
                                 StringRef &Res) const;
+
+  /// \brief Get the symbol table index for the given symbol.
+  uint64_t        getSymbolIndex(const Elf_Sym *Symb) const;
+
   error_code      getSectionName(const Elf_Shdr *section,
                                  StringRef &Res) const;
   const Elf_Dyn  *getDyn(DataRefImpl DynData) const;
@@ -2490,6 +2494,20 @@ error_code ELFObjectFile<target_endianness, is64Bits>
 }

 template<support::endianness target_endianness, bool is64Bits>
+uint64_t ELFObjectFile<target_endianness, is64Bits>
+                      ::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<support::endianness target_endianness, bool is64Bits>
 error_code ELFObjectFile<target_endianness, is64Bits>
                         ::getSectionName(const Elf_Shdr *section,
                                         StringRef &Result) const {


- Michael Spencer



More information about the llvm-commits mailing list