[llvm] c1d19a8 - [ELF] Provide the GNU hash function in libObject

Joseph Huber via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 5 09:05:10 PDT 2022


Author: Joseph Huber
Date: 2022-09-05T11:04:57-05:00
New Revision: c1d19a8489466270bb1c3639dcdabbda1d4c57d7

URL: https://github.com/llvm/llvm-project/commit/c1d19a8489466270bb1c3639dcdabbda1d4c57d7
DIFF: https://github.com/llvm/llvm-project/commit/c1d19a8489466270bb1c3639dcdabbda1d4c57d7.diff

LOG: [ELF] Provide the GNU hash function in libObject

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.

Reviewed By: MaskRay, jhenderson

Differential Revision: https://reviews.llvm.org/D132696

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index c6f749fac4032..4af2de5493a38 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -2397,13 +2397,6 @@ void GnuHashTableSection::writeTo(uint8_t *buf) {
   }
 }
 
-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.

diff  --git a/llvm/include/llvm/Object/ELF.h b/llvm/include/llvm/Object/ELF.h
index 794d29fd99133..1a987cf2d7126 100644
--- a/llvm/include/llvm/Object/ELF.h
+++ b/llvm/include/llvm/Object/ELF.h
@@ -1223,6 +1223,16 @@ inline unsigned hashSysV(StringRef SymbolName) {
   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;
+}
+
 } // end namespace object
 } // end namespace llvm
 


        


More information about the llvm-commits mailing list