Module Name: src Committed By: sborrill Date: Fri Aug 11 12:13:10 UTC 2023
Modified Files: src/distrib/sets/lists/debug [netbsd-9]: mi src/distrib/sets/lists/tests [netbsd-9]: mi src/libexec/ld.elf_so [netbsd-9]: Makefile symbol.c src/tests/libexec/ld.elf_so [netbsd-9]: Makefile Added Files: src/libexec/ld.elf_so [netbsd-9]: hash.c hash.h src/tests/libexec/ld.elf_so [netbsd-9]: t_hash.c Log Message: Pull up the following revisions(s) (requested by riastradh in ticket #1712): distrib/sets/lists/debug/mi: revision 1.409 via patch distrib/sets/lists/tests/mi: revision 1.1280 via patch libexec/ld.elf_so/Makefile: revision 1.145-1.147 via patch libexec/ld.elf_so/hash.c: revision 1.1 via patch libexec/ld.elf_so/hash.h: revision 1.1 via patch libexec/ld.elf_so/symbol.c: revision 1.74-1.76 via patch tests/libexec/ld.elf_so/Makefile: revision 1.21 via patch tests/libexec/ld.elf_so/t_hash.c: revision 1.1 via patch ld.elf_so: Fix sysv elf hash on edge cases like `ZZZZZW9p' on LP64 platforms where long is 64-bit. To generate a diff of this commit: cvs rdiff -u -r1.285.2.1 -r1.285.2.2 src/distrib/sets/lists/debug/mi cvs rdiff -u -r1.818.2.2 -r1.818.2.3 src/distrib/sets/lists/tests/mi cvs rdiff -u -r1.141.2.1 -r1.141.2.2 src/libexec/ld.elf_so/Makefile cvs rdiff -u -r0 -r1.1.6.2 src/libexec/ld.elf_so/hash.c \ src/libexec/ld.elf_so/hash.h cvs rdiff -u -r1.69 -r1.69.6.1 src/libexec/ld.elf_so/symbol.c cvs rdiff -u -r1.10.2.1 -r1.10.2.2 src/tests/libexec/ld.elf_so/Makefile cvs rdiff -u -r0 -r1.1.6.2 src/tests/libexec/ld.elf_so/t_hash.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/distrib/sets/lists/debug/mi diff -u src/distrib/sets/lists/debug/mi:1.285.2.1 src/distrib/sets/lists/debug/mi:1.285.2.2 --- src/distrib/sets/lists/debug/mi:1.285.2.1 Fri Aug 4 12:55:45 2023 +++ src/distrib/sets/lists/debug/mi Fri Aug 11 12:13:09 2023 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.285.2.1 2023/08/04 12:55:45 martin Exp $ +# $NetBSD: mi,v 1.285.2.2 2023/08/11 12:13:09 sborrill Exp $ ./etc/mtree/set.debug comp-sys-root ./usr/lib comp-sys-usr compatdir ./usr/lib/i18n/libBIG5_g.a comp-c-debuglib debuglib,compatfile @@ -2314,6 +2314,7 @@ ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_dlerror-false.debug tests-libexec-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_dlinfo.debug tests-libexec-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_dlvsym.debug tests-libexec-debug debug,atf,pic,compattestfile +./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_hash.debug tests-libexec-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_ifunc.debug tests-libexec-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_tls_extern.debug tests-libexec-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/net/bpf/t_bpf.debug tests-net-debug debug,atf,rump Index: src/distrib/sets/lists/tests/mi diff -u src/distrib/sets/lists/tests/mi:1.818.2.2 src/distrib/sets/lists/tests/mi:1.818.2.3 --- src/distrib/sets/lists/tests/mi:1.818.2.2 Fri Aug 4 12:55:44 2023 +++ src/distrib/sets/lists/tests/mi Fri Aug 11 12:13:09 2023 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.818.2.2 2023/08/04 12:55:44 martin Exp $ +# $NetBSD: mi,v 1.818.2.3 2023/08/11 12:13:09 sborrill Exp $ # # Note: don't delete entries from here - mark them as "obsolete" instead. # @@ -3419,6 +3419,7 @@ ./usr/tests/libexec/ld.elf_so/t_dlerror-false tests-libexec-tests compattestfile,atf,pic ./usr/tests/libexec/ld.elf_so/t_dlinfo tests-libexec-tests compattestfile,atf,pic ./usr/tests/libexec/ld.elf_so/t_dlvsym tests-libexec-tests compattestfile,atf,pic +./usr/tests/libexec/ld.elf_so/t_hash tests-libexec-tests compattestfile,atf,pic ./usr/tests/libexec/ld.elf_so/t_ifunc tests-libexec-tests compattestfile,atf,pic ./usr/tests/libexec/ld.elf_so/t_thread_local_dtor tests-libexec-tests compattestfile,atf,pic ./usr/tests/libexec/ld.elf_so/t_tls_extern tests-libexec-tests compattestfile,atf,pic Index: src/libexec/ld.elf_so/Makefile diff -u src/libexec/ld.elf_so/Makefile:1.141.2.1 src/libexec/ld.elf_so/Makefile:1.141.2.2 --- src/libexec/ld.elf_so/Makefile:1.141.2.1 Tue Nov 26 08:12:26 2019 +++ src/libexec/ld.elf_so/Makefile Fri Aug 11 12:13:10 2023 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.141.2.1 2019/11/26 08:12:26 martin Exp $ +# $NetBSD: Makefile,v 1.141.2.2 2023/08/11 12:13:10 sborrill Exp $ # # NOTE: when changing ld.so, ensure that ldd still compiles. # @@ -70,9 +70,23 @@ PROG= ld.elf_so CLIBOBJ!= cd ${NETBSDSRCDIR}/lib/libc && ${PRINTOBJDIR} -SRCS+= rtld.c reloc.c symbol.c xmalloc.c xprintf.c debug.c \ - map_object.c load.c search.c headers.c paths.c expand.c \ - tls.c symver.c diagassert.c compat.c +SRCS+= compat.c +SRCS+= debug.c +SRCS+= diagassert.c +SRCS+= expand.c +SRCS+= hash.c +SRCS+= headers.c +SRCS+= load.c +SRCS+= map_object.c +SRCS+= paths.c +SRCS+= reloc.c +SRCS+= rtld.c +SRCS+= search.c +SRCS+= symbol.c +SRCS+= symver.c +SRCS+= tls.c +SRCS+= xmalloc.c +SRCS+= xprintf.c .if ${USE_FORT} == "yes" .PATH.c: ${NETBSDSRCDIR}/lib/libc/misc Index: src/libexec/ld.elf_so/symbol.c diff -u src/libexec/ld.elf_so/symbol.c:1.69 src/libexec/ld.elf_so/symbol.c:1.69.6.1 --- src/libexec/ld.elf_so/symbol.c:1.69 Wed Aug 9 18:44:32 2017 +++ src/libexec/ld.elf_so/symbol.c Fri Aug 11 12:13:10 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: symbol.c,v 1.69 2017/08/09 18:44:32 joerg Exp $ */ +/* $NetBSD: symbol.c,v 1.69.6.1 2023/08/11 12:13:10 sborrill Exp $ */ /* * Copyright 1996 John D. Polstra. @@ -40,7 +40,7 @@ #include <sys/cdefs.h> #ifndef lint -__RCSID("$NetBSD: symbol.c,v 1.69 2017/08/09 18:44:32 joerg Exp $"); +__RCSID("$NetBSD: symbol.c,v 1.69.6.1 2023/08/11 12:13:10 sborrill Exp $"); #endif /* not lint */ #include <err.h> @@ -80,29 +80,6 @@ _rtld_donelist_check(DoneList *dlp, cons return false; } -/* - * Hash function for symbol table lookup. Don't even think about changing - * this. It is specified by the System V ABI. - */ -unsigned long -_rtld_elf_hash(const char *name) -{ - const unsigned char *p = (const unsigned char *) name; - unsigned long h = 0; - unsigned long g; - unsigned long c; - - for (; __predict_true((c = *p) != '\0'); p++) { - h <<= 4; - h += c; - if ((g = h & 0xf0000000) != 0) { - h ^= g; - h ^= g >> 24; - } - } - return (h); -} - const Elf_Sym * _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist, const Obj_Entry **defobj_out, u_int flags, const Ver_Entry *ventry, Index: src/tests/libexec/ld.elf_so/Makefile diff -u src/tests/libexec/ld.elf_so/Makefile:1.10.2.1 src/tests/libexec/ld.elf_so/Makefile:1.10.2.2 --- src/tests/libexec/ld.elf_so/Makefile:1.10.2.1 Fri Aug 4 12:55:46 2023 +++ src/tests/libexec/ld.elf_so/Makefile Fri Aug 11 12:13:10 2023 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.10.2.1 2023/08/04 12:55:46 martin Exp $ +# $NetBSD: Makefile,v 1.10.2.2 2023/08/11 12:13:10 sborrill Exp $ # NOMAN= # defined @@ -30,6 +30,12 @@ TESTSDIR= ${TESTSBASE}/libexec/ld.elf_so TESTS_C+= t_dlerror-cleared t_dlerror-false t_dlinfo t_dlvsym t_ifunc TESTS_C+= t_tls_extern +TESTS_C+= t_hash + +.PATH: ${NETBSDSRCDIR}/libexec/ld.elf_so +SRCS.t_hash+= t_hash.c +SRCS.t_hash+= hash.c +CPPFLAGS.t_hash.c+= -I${NETBSDSRCDIR}/libexec/ld.elf_so LDADD.t_dlerror-false= -Wl,-rpath,/var/nonexistent/lib LDADD.t_dlvsym= -Wl,-rpath,${TESTSDIR}/h_helper_symver_dso2 Added files: Index: src/libexec/ld.elf_so/hash.c diff -u /dev/null src/libexec/ld.elf_so/hash.c:1.1.6.2 --- /dev/null Fri Aug 11 12:13:10 2023 +++ src/libexec/ld.elf_so/hash.c Fri Aug 11 12:13:10 2023 @@ -0,0 +1,65 @@ +/* $NetBSD: hash.c,v 1.1.6.2 2023/08/11 12:13:10 sborrill Exp $ */ + +/* + * Copyright 1996 John D. Polstra. + * Copyright 1996 Matt Thomas <m...@3am-software.com> + * Copyright 2002 Charles M. Hannum <r...@ihack.net> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by John Polstra. + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Dynamic linker for ELF. + * + * John Polstra <j...@polstra.com>. + */ + +#include <sys/cdefs.h> +#ifndef lint +__RCSID("$NetBSD: hash.c,v 1.1.6.2 2023/08/11 12:13:10 sborrill Exp $"); +#endif /* not lint */ + +#include <stdint.h> + +#include "hash.h" + +/* + * SysV hash function for symbol table lookup. It is a slightly optimized + * version of the hash specified by the System V ABI. + */ +Elf32_Word +_rtld_elf_hash(const char *name) +{ + const unsigned char *p = (const unsigned char *) name; + Elf32_Word h = 0; + + while (__predict_true(*p != '\0')) { + h = (h << 4) + *p++; + h ^= (h >> 24) & 0xf0; + } + return (h & 0x0fffffff); +} Index: src/libexec/ld.elf_so/hash.h diff -u /dev/null src/libexec/ld.elf_so/hash.h:1.1.6.2 --- /dev/null Fri Aug 11 12:13:10 2023 +++ src/libexec/ld.elf_so/hash.h Fri Aug 11 12:13:10 2023 @@ -0,0 +1,41 @@ +/* $NetBSD: hash.h,v 1.1.6.2 2023/08/11 12:13:10 sborrill Exp $ */ + +/* + * Copyright 1996 John D. Polstra. + * Copyright 1996 Matt Thomas <m...@3am-software.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by John Polstra. + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef RTLD_HASH_H +#define RTLD_HASH_H + +#include <sys/exec_elf.h> + +Elf32_Word _rtld_elf_hash(const char *); + +#endif /* RTLD_HASH_H */ Index: src/tests/libexec/ld.elf_so/t_hash.c diff -u /dev/null src/tests/libexec/ld.elf_so/t_hash.c:1.1.6.2 --- /dev/null Fri Aug 11 12:13:10 2023 +++ src/tests/libexec/ld.elf_so/t_hash.c Fri Aug 11 12:13:10 2023 @@ -0,0 +1,169 @@ +/* $NetBSD: t_hash.c,v 1.1.6.2 2023/08/11 12:13:10 sborrill Exp $ */ + +/*- + * Copyright (c) 2023 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> + +#include <atf-c.h> +#include <stdint.h> + +#include "hash.h" + +/* known-answer tests */ +struct kat { + const char *in; + unsigned long long out; +}; + +/* + * From the SysV spec, with uint64_t instead of unsigned long to + * illustrate the problem on all systems, not just LP64 ones. + * + * https://www.sco.com/developers/devspecs/gabi41.pdf#page=95 + */ +static uint64_t +sysv_broken_hash(const char *name) +{ + uint64_t h = 0, g; + + while (*name) { + h = (h << 4) + *name++; + if ((g = h & 0xf0000000) != 0) + h ^= g >> 24; + h &= ~g; + } + + return h; +} + +ATF_TC(sysv); +ATF_TC_HEAD(sysv, tc) +{ + atf_tc_set_md_var(tc, "descr", "SysV hash (32-bit)"); +} +ATF_TC_BODY(sysv, tc) +{ + static const struct kat kat[] = { + { "", 0x00000000 }, + { "a", 0x00000061 }, + { "aa", 0x00000671 }, + { "aaa", 0x00006771 }, + { "aaaa", 0x00067771 }, + { "aaaaa", 0x00677771 }, + { "aaaaaa", 0x06777771 }, + { "aaaaaaa", 0x07777711 }, + { "aaaaaaaa", 0x07777101 }, + { "aaaaaaaaa", 0x07771001 }, + { "ab", 0x00000672 }, + { "abc", 0x00006783 }, + { "abcd", 0x00067894 }, + { "abcde", 0x006789a5 }, + { "abcdef", 0x06789ab6 }, + { "abcdefg", 0x0789aba7 }, + { "abcdefgh", 0x089abaa8 }, + { "abcdefghi", 0x09abaa69 }, + /* https://maskray.me/blog/2023-04-12-elf-hash-function */ + { "Z", 0x0000005a }, + { "ZZ", 0x000005fa }, + { "ZZZ", 0x00005ffa }, + { "ZZZZ", 0x0005fffa }, + { "ZZZZZ", 0x005ffffa }, + { "ZZZZZW", 0x05fffff7 }, + { "ZZZZZW9", 0x0ffffff9 }, + { "ZZZZZW9p", 0x00000000 }, + { "pneumonoultramicroscopicsilicovolcanoconiosis", + 0x051706b3 }, + }; + unsigned i; + + for (i = 0; i < __arraycount(kat); i++) { + unsigned long long h = _rtld_elf_hash(kat[i].in); + + ATF_CHECK_EQ_MSG(h, kat[i].out, + "[%u] _rtld_elf_hash(\"%s\") = 0x%08llx != 0x%08llx", + i, kat[i].in, h, kat[i].out); + } +} + +ATF_TC(sysv_broken); +ATF_TC_HEAD(sysv_broken, tc) +{ + atf_tc_set_md_var(tc, "descr", + "SysV hash (broken with 64-bit unsigned long)"); +} +ATF_TC_BODY(sysv_broken, tc) +{ + static const struct kat kat[] = { + { "", 0x00000000 }, + { "a", 0x00000061 }, + { "aa", 0x00000671 }, + { "aaa", 0x00006771 }, + { "aaaa", 0x00067771 }, + { "aaaaa", 0x00677771 }, + { "aaaaaa", 0x06777771 }, + { "aaaaaaa", 0x07777711 }, + { "aaaaaaaa", 0x07777101 }, + { "aaaaaaaaa", 0x07771001 }, + { "ab", 0x00000672 }, + { "abc", 0x00006783 }, + { "abcd", 0x00067894 }, + { "abcde", 0x006789a5 }, + { "abcdef", 0x06789ab6 }, + { "abcdefg", 0x0789aba7 }, + { "abcdefgh", 0x089abaa8 }, + { "abcdefghi", 0x09abaa69 }, + /* https://maskray.me/blog/2023-04-12-elf-hash-function */ + { "Z", 0x0000005a }, + { "ZZ", 0x000005fa }, + { "ZZZ", 0x00005ffa }, + { "ZZZZ", 0x0005fffa }, + { "ZZZZZ", 0x005ffffa }, + { "ZZZZZW", 0x05fffff7 }, + { "ZZZZZW9", 0x0ffffff9 }, + { "ZZZZZW9p", 0x100000000 }, /* XXX */ + { "pneumonoultramicroscopicsilicovolcanoconiosis", + 0x051706b3 }, + }; + unsigned i; + + for (i = 0; i < __arraycount(kat); i++) { + unsigned long long h = sysv_broken_hash(kat[i].in); + + ATF_CHECK_EQ_MSG(h, kat[i].out, + "[%u] sysv_broken_hash(\"%s\") = 0x%08llx != 0x%08llx", + i, kat[i].in, h, kat[i].out); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, sysv); + ATF_TP_ADD_TC(tp, sysv_broken); + + return atf_no_error(); +}