From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 3C1BB385828F for ; Fri, 19 Aug 2022 10:16:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3C1BB385828F Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-265-4ct8VpZvMu20ClW1lzCz7w-1; Fri, 19 Aug 2022 06:16:36 -0400 X-MC-Unique: 4ct8VpZvMu20ClW1lzCz7w-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0ED3B85A586 for ; Fri, 19 Aug 2022 10:16:36 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.206]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 69E1A492C3B for ; Fri, 19 Aug 2022 10:16:35 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 1/2] scripts/glibcelf.py: Add hashing support In-Reply-To: References: X-From-Line: 11d0aa3cb1c97a02853bc9b1a68f4f64fb4a5c13 Mon Sep 17 00:00:00 2001 Message-Id: <11d0aa3cb1c97a02853bc9b1a68f4f64fb4a5c13.1660903960.git.fweimer@redhat.com> Date: Fri, 19 Aug 2022 12:16:33 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 19 Aug 2022 10:16:39 -0000 ELF and GNU hashes can now be computed using the elf_hash and gnu_hash functions. --- elf/tst-glibcelf.py | 19 +++++++++++++++++++ scripts/glibcelf.py | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/elf/tst-glibcelf.py b/elf/tst-glibcelf.py index bf15a3bad4..e5026e2289 100644 --- a/elf/tst-glibcelf.py +++ b/elf/tst-glibcelf.py @@ -240,6 +240,24 @@ def check_constant_values(cc): error('{}: glibcelf has {!r}, has {!r}'.format( name, glibcelf_value, elf_h_value)) +def check_hashes(): + for name, expected_elf, expected_gnu in ( + ('', 0, 0x1505), + ('PPPPPPPPPPPP', 0, 0x9f105c45), + ('GLIBC_2.0', 0xd696910, 0xf66c3dd5), + ('GLIBC_2.34', 0x69691b4, 0xc3f3f90c), + ('GLIBC_PRIVATE', 0x963cf85, 0x692a260)): + for convert in (lambda x: x, lambda x: x.encode('UTF-8')): + name = convert(name) + actual_elf = glibcelf.elf_hash(name) + if actual_elf != expected_elf: + error('elf_hash({!r}): {:x} != 0x{:x}'.format( + name, actual_elf, expected_elf)) + actual_gnu = glibcelf.gnu_hash(name) + if actual_gnu != expected_gnu: + error('gnu_hash({!r}): {:x} != 0x{:x}'.format( + name, actual_gnu, expected_gnu)) + def main(): """The main entry point.""" parser = argparse.ArgumentParser( @@ -251,6 +269,7 @@ def main(): check_duplicates() check_constant_prefixes() check_constant_values(cc=args.cc) + check_hashes() if errors_encountered > 0: print("note: errors encountered:", errors_encountered) 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()] -- 2.37.1