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

Shankar Easwaran shankare at codeaurora.org
Wed Nov 7 10:20:29 PST 2012


Hi Mike, Eric,

Attached is the diff with the appropriate changes as per comments.

Ok to commit ?

Thanks

Shankar Easwaran

On 11/5/2012 9:28 AM, Shankar Easwaran wrote:
> Hi,
>
> Nice!, I will change it to the version thats mentioned below.
>
> Thanks Mike for the nice approach.
>
> Shankar Easwaran
>
> On 11/2/2012 6:09 PM, Michael Spencer wrote:
>> 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
>>
>
>


-- 
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by the Linux Foundation

-------------- next part --------------
Index: ELF.h
===================================================================
--- ELF.h	(revision 167054)
+++ ELF.h	(working copy)
@@ -184,6 +184,17 @@
   void setBindingAndType(unsigned char b, unsigned char t) {
     st_info = (b << 4) + (t & 0x0f);
   }
+
+  bool operator ==(const Elf_Sym_Impl<target_endianness, is64Bits> &rhs) const {
+    if ((this->st_name == rhs.st_name) &&
+        (this->st_info == rhs.st_info) &&
+        (this->st_other == rhs.st_other) &&
+        (this->st_shndx == rhs.st_shndx) &&
+        (this->st_value == rhs.st_value) &&
+        (this->st_size == rhs.st_size))
+      return true;	
+    return false;
+  }
 };
 
 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
@@ -609,6 +620,7 @@
   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,7 +2106,23 @@
   }
 }
 
+// Get the symbol table index in the symtab section given a symbol
+// returns an error code if the symbol is not found
 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>
 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
                              ::begin_symbols() const {
   DataRefImpl SymbolData;


More information about the llvm-commits mailing list