From bd13cb19f5e15e9e9a92a536e755fd93a97a67f6 Mon Sep 17 00:00:00 2001 From: Florian Weimer Date: Fri, 19 Aug 2022 11:16:32 +0200 Subject: scripts/glibcelf.py: Add hashing support ELF and GNU hashes can now be computed using the elf_hash and gnu_hash functions. Reviewed-by: Carlos O'Donell Tested-by: Carlos O'Donell --- scripts/glibcelf.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'scripts') diff --git a/scripts/glibcelf.py b/scripts/glibcelf.py index de0509130e..5c8f46f590 100644 --- a/scripts/glibcelf.py +++ b/scripts/glibcelf.py @@ -1158,5 +1158,24 @@ class Image: self._stringtab[sh_link] = strtab return strtab +def elf_hash(s): + """Computes the ELF hash of the string.""" + acc = 0 + for ch in s: + if type(ch) is not int: + ch = ord(ch) + acc = ((acc << 4) + ch) & 0xffffffff + top = acc & 0xf0000000 + acc = (acc ^ (top >> 24)) & ~top + return acc + +def gnu_hash(s): + """Computes the GNU hash of the string.""" + h = 5381 + for ch in s: + if type(ch) is not int: + ch = ord(ch) + h = (h * 33 + ch) & 0xffffffff + return h __all__ = [name for name in dir() if name[0].isupper()] -- cgit 1.4.1