poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Implementation of ELF hash


From: Jose E. Marchesi
Subject: Re: Implementation of ELF hash
Date: Wed, 26 Jan 2022 18:28:28 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Hi Jan.

> I've been recreationally hacking on an ELF file, and had a need to
> implement the ELF hashing algorithm. This might be useful for other
> people as well, so maybe it could be included in the ELF pickle. I've
> included the implementation below.

Thank you very much for sharing.  It is definitely useful.

I have committed it on your behalf after a few trivial style changes, as
below.

(Just in time for poke 2.0 ;))

>From 48922e5f7ef696cdf040475a16a3de313bc4d396 Mon Sep 17 00:00:00 2001
From: Jan Seeger <jan.seeger@thenybble.de>
Date: Wed, 26 Jan 2022 18:23:42 +0100
Subject: [PATCH] elf-common.pk: add ELF hashing function

2022-01-26  Jan Seeger  <jan.seeger@thenybble.de>

        * pickles/elf-common.pk (elf_hash): New function.
---
 ChangeLog             |  4 ++++
 pickles/elf-common.pk | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index cc569c9a..cc1be9c0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2022-01-26  Jan Seeger  <jan.seeger@thenybble.de>
+
+       * pickles/elf-common.pk (elf_hash): New function.
+
 2022-01-26  Jose E. Marchesi  <jemarch@gnu.org>
 
        * doc/poke.texi (String Formatting): New section.
diff --git a/pickles/elf-common.pk b/pickles/elf-common.pk
index 236c3e05..6fb68a81 100644
--- a/pickles/elf-common.pk
+++ b/pickles/elf-common.pk
@@ -127,6 +127,25 @@ fun elf_compress_algorithm_p = (uint<64> ch_type) int:
           || (ch_type >= ELFCOMPRESS_LOPROC && ch_type <= ELFCOMPRESS_HIPROC));
 }
 
+/* ELF hashing function.  */
+
+fun elf_hash = (string input) Elf_Word:
+{
+  var h = 0 as uint<32>;
+  var high = 0 as uint<32>;
+
+  for (c in input)
+  {
+    h = (h <<. 4) + c;
+    high = h & 0xf0000000;
+    if (high)
+      h = h ^ (high .>> 24);
+    h = h & ~high;
+  };
+
+ return h;
+}
+
 /* Dynamic tags.  */
 
 var DT_NULL = 0,  /* Tags the end of the dynamic array.  */
-- 
2.11.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]