[PATCH] D132696: [ELF] Provide the GNU hash function in libObject

Joseph Huber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 25 12:03:01 PDT 2022


jhuber6 created this revision.
jhuber6 added reviewers: JonChesterfield, MaskRay, jhenderson, tra, ronlieb, alexander-shaposhnikov.
Herald added subscribers: StephenFan, arichardson, emaste.
Herald added a project: All.
jhuber6 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

GNU uses a different hashing function compared to the sys-V standard
function already provided in libObject. This is already used internally
in LLD for generating synthetic sections. This patch simply extracts
this definition and makes it availible to other users of `libObject`.
This is done in preparation for supporting symbol name lookups via the
GNU hash table.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132696

Files:
  lld/ELF/SyntheticSections.cpp
  llvm/include/llvm/Object/ELF.h


Index: llvm/include/llvm/Object/ELF.h
===================================================================
--- llvm/include/llvm/Object/ELF.h
+++ llvm/include/llvm/Object/ELF.h
@@ -86,6 +86,16 @@
   return h;
 }
 
+/// This function returns the hash value for a symbol in the .dynsym section
+/// for the GNU hash table. The implementation is defined in the GNU hash ABI.
+/// REF : https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=bfd/elf.c#l222
+inline uint32_t hashGnu(StringRef Name) {
+  uint32_t H = 5381;
+  for (uint8_t C : Name)
+    H = (H << 5) + H + C;
+  return H;
+}
+
 // Subclasses of ELFFile may need this for template instantiation
 inline std::pair<unsigned char, unsigned char>
 getElfArchType(StringRef Object) {
Index: lld/ELF/SyntheticSections.cpp
===================================================================
--- lld/ELF/SyntheticSections.cpp
+++ lld/ELF/SyntheticSections.cpp
@@ -2406,13 +2406,6 @@
   }
 }
 
-static uint32_t hashGnu(StringRef name) {
-  uint32_t h = 5381;
-  for (uint8_t c : name)
-    h = (h << 5) + h + c;
-  return h;
-}
-
 // Add symbols to this symbol hash table. Note that this function
 // destructively sort a given vector -- which is needed because
 // GNU-style hash table places some sorting requirements.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132696.455681.patch
Type: text/x-patch
Size: 1273 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220825/9ba9e0d9/attachment.bin>


More information about the llvm-commits mailing list