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

Shankar Easwaran shankare at codeaurora.org
Thu Nov 1 07:26:44 PDT 2012


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

-------------- next part --------------
Index: include/llvm/Object/ELF.h
===================================================================
--- include/llvm/Object/ELF.h	(revision 167130)
+++ include/llvm/Object/ELF.h	(working copy)
@@ -169,6 +169,7 @@
   Elf_Half      st_shndx; // Which section (header table index) it's defined in
   Elf_Addr      st_value; // Value or address associated with the symbol
   Elf_Xword     st_size;  // Size of the symbol
+
 };
 
 template<support::endianness target_endianness, bool is64Bits>
@@ -184,6 +185,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
@@ -604,6 +616,8 @@
   error_code      getSymbolName(const Elf_Shdr *section,
                                 const Elf_Sym *Symb,
                                 StringRef &Res) const;
+  error_code      getSymbolIndex(const Elf_Sym *sym,
+				 uint64_t &symbol_index);
   error_code      getSectionName(const Elf_Shdr *section,
                                  StringRef &Res) const;
   const Elf_Dyn  *getDyn(DataRefImpl DynData) const;
@@ -2094,7 +2108,31 @@
   }
 }
 
+// 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>
+error_code ELFObjectFile<target_endianness, is64Bits>::getSymbolIndex(const Elf_Sym *sym, uint64_t &sym_index) 
+{
+  error_code ec;
+  for (symbol_iterator si = begin_symbols(),
+                       se = end_symbols(); si != se; si.increment(ec)) {
+    if (ec)
+      return ec;
+    
+    DataRefImpl CSym = si->getRawDataRefImpl();
+    
+    const Elf_Sym *current_sym = getSymbol(CSym);
+    
+    if (*current_sym == *sym) {
+      sym_index = CSym.d.a;
+      return object_error::success;
+    }
+  }
+  return object_error::parse_failed;
+}
+
+
+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