194aa3
commit bd13cb19f5e15e9e9a92a536e755fd93a97a67f6
194aa3
Author: Florian Weimer <fweimer@redhat.com>
194aa3
Date:   Fri Aug 19 11:16:32 2022 +0200
194aa3
194aa3
    scripts/glibcelf.py: Add hashing support
194aa3
    
194aa3
    ELF and GNU hashes can now be computed using the elf_hash and
194aa3
    gnu_hash functions.
194aa3
    
194aa3
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
194aa3
    Tested-by: Carlos O'Donell <carlos@redhat.com>
194aa3
194aa3
diff --git a/elf/tst-glibcelf.py b/elf/tst-glibcelf.py
194aa3
index bf15a3bad4479e08..e5026e2289df206b 100644
194aa3
--- a/elf/tst-glibcelf.py
194aa3
+++ b/elf/tst-glibcelf.py
194aa3
@@ -240,6 +240,24 @@ def check_constant_values(cc):
194aa3
             error('{}: glibcelf has {!r}, <elf.h> has {!r}'.format(
194aa3
                 name, glibcelf_value, elf_h_value))
194aa3
 
194aa3
+def check_hashes():
194aa3
+    for name, expected_elf, expected_gnu in (
194aa3
+            ('', 0, 0x1505),
194aa3
+            ('PPPPPPPPPPPP', 0, 0x9f105c45),
194aa3
+            ('GLIBC_2.0', 0xd696910, 0xf66c3dd5),
194aa3
+            ('GLIBC_2.34', 0x69691b4, 0xc3f3f90c),
194aa3
+            ('GLIBC_PRIVATE', 0x963cf85, 0x692a260)):
194aa3
+        for convert in (lambda x: x, lambda x: x.encode('UTF-8')):
194aa3
+            name = convert(name)
194aa3
+            actual_elf = glibcelf.elf_hash(name)
194aa3
+            if actual_elf != expected_elf:
194aa3
+                error('elf_hash({!r}): {:x} != 0x{:x}'.format(
194aa3
+                    name, actual_elf, expected_elf))
194aa3
+            actual_gnu = glibcelf.gnu_hash(name)
194aa3
+            if actual_gnu != expected_gnu:
194aa3
+                error('gnu_hash({!r}): {:x} != 0x{:x}'.format(
194aa3
+                    name, actual_gnu, expected_gnu))
194aa3
+
194aa3
 def main():
194aa3
     """The main entry point."""
194aa3
     parser = argparse.ArgumentParser(
194aa3
@@ -251,6 +269,7 @@ def main():
194aa3
     check_duplicates()
194aa3
     check_constant_prefixes()
194aa3
     check_constant_values(cc=args.cc)
194aa3
+    check_hashes()
194aa3
 
194aa3
     if errors_encountered > 0:
194aa3
         print("note: errors encountered:", errors_encountered)
194aa3
diff --git a/scripts/glibcelf.py b/scripts/glibcelf.py
194aa3
index de0509130ed9ad47..5c8f46f590722384 100644
194aa3
--- a/scripts/glibcelf.py
194aa3
+++ b/scripts/glibcelf.py
194aa3
@@ -1158,5 +1158,24 @@ class Image:
194aa3
         self._stringtab[sh_link] = strtab
194aa3
         return strtab
194aa3
 
194aa3
+def elf_hash(s):
194aa3
+    """Computes the ELF hash of the string."""
194aa3
+    acc = 0
194aa3
+    for ch in s:
194aa3
+        if type(ch) is not int:
194aa3
+            ch = ord(ch)
194aa3
+        acc = ((acc << 4) + ch) & 0xffffffff
194aa3
+        top = acc & 0xf0000000
194aa3
+        acc = (acc ^ (top >> 24)) & ~top
194aa3
+    return acc
194aa3
+
194aa3
+def gnu_hash(s):
194aa3
+    """Computes the GNU hash of the string."""
194aa3
+    h = 5381
194aa3
+    for ch in s:
194aa3
+        if type(ch) is not int:
194aa3
+            ch = ord(ch)
194aa3
+        h = (h * 33 + ch) & 0xffffffff
194aa3
+    return h
194aa3
 
194aa3
 __all__ = [name for name in dir() if name[0].isupper()]