Sun Jul 30 09:20:14 2023 UTC ()
ld.elf_so: Split hash functions into a separate file.

This way we can test them in isolation.

No functional change intended.


(riastradh)
diff -r1.146 -r1.147 src/libexec/ld.elf_so/Makefile
diff -r1.146 -r1.147 src/libexec/ld.elf_so/rtld.h
diff -r0 -r1.1 src/libexec/ld.elf_so/hash.c
diff -r0 -r1.1 src/libexec/ld.elf_so/hash.h
diff -r1.117 -r1.118 src/libexec/ld.elf_so/reloc.c
diff -r1.214 -r1.215 src/libexec/ld.elf_so/rtld.c
diff -r1.75 -r1.76 src/libexec/ld.elf_so/symbol.c

cvs diff -r1.146 -r1.147 src/libexec/ld.elf_so/Makefile (expand / switch to unified diff)

--- src/libexec/ld.elf_so/Makefile 2023/07/30 09:19:19 1.146
+++ src/libexec/ld.elf_so/Makefile 2023/07/30 09:20:14 1.147
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: Makefile,v 1.146 2023/07/30 09:19:19 riastradh Exp $ 1# $NetBSD: Makefile,v 1.147 2023/07/30 09:20:14 riastradh Exp $
2# 2#
3# NOTE: when changing ld.so, ensure that ldd still compiles. 3# NOTE: when changing ld.so, ensure that ldd still compiles.
4# 4#
5NOLIBCSANITIZER=# defined 5NOLIBCSANITIZER=# defined
6NOSANITIZER= # defined 6NOSANITIZER= # defined
7WARNS?=4 7WARNS?=4
8 8
9LDELFSO_MACHINE_ARCH?= ${MACHINE_ARCH} 9LDELFSO_MACHINE_ARCH?= ${MACHINE_ARCH}
10 10
11# This needs to be before bsd.init.mk 11# This needs to be before bsd.init.mk
12.if defined(BSD_MK_COMPAT_FILE) 12.if defined(BSD_MK_COMPAT_FILE)
13.include <${BSD_MK_COMPAT_FILE}> 13.include <${BSD_MK_COMPAT_FILE}>
14.endif 14.endif
@@ -66,26 +66,27 @@ PROG= ld.elf_so-${MLIBDIR} @@ -66,26 +66,27 @@ PROG= ld.elf_so-${MLIBDIR}
66CPPFLAGS+= -DRTLD_ARCH_SUBDIR=\"${MLIBDIR}\" 66CPPFLAGS+= -DRTLD_ARCH_SUBDIR=\"${MLIBDIR}\"
67. if ${MLIBDIR} == "o32" 67. if ${MLIBDIR} == "o32"
68. endif 68. endif
69.else 69.else
70PROG= ld.elf_so 70PROG= ld.elf_so
71.endif 71.endif
72 72
73CLIBOBJ!= cd ${NETBSDSRCDIR}/lib/libc && ${PRINTOBJDIR} 73CLIBOBJ!= cd ${NETBSDSRCDIR}/lib/libc && ${PRINTOBJDIR}
74 74
75SRCS+= compat.c 75SRCS+= compat.c
76SRCS+= debug.c 76SRCS+= debug.c
77SRCS+= diagassert.c 77SRCS+= diagassert.c
78SRCS+= expand.c 78SRCS+= expand.c
 79SRCS+= hash.c
79SRCS+= headers.c 80SRCS+= headers.c
80SRCS+= load.c 81SRCS+= load.c
81SRCS+= map_object.c 82SRCS+= map_object.c
82SRCS+= paths.c 83SRCS+= paths.c
83SRCS+= reloc.c 84SRCS+= reloc.c
84SRCS+= rtld.c 85SRCS+= rtld.c
85SRCS+= search.c 86SRCS+= search.c
86SRCS+= symbol.c 87SRCS+= symbol.c
87SRCS+= symver.c 88SRCS+= symver.c
88SRCS+= tls.c 89SRCS+= tls.c
89SRCS+= xmalloc.c 90SRCS+= xmalloc.c
90SRCS+= xprintf.c 91SRCS+= xprintf.c
91 92

cvs diff -r1.146 -r1.147 src/libexec/ld.elf_so/rtld.h (expand / switch to unified diff)

--- src/libexec/ld.elf_so/rtld.h 2023/06/04 01:24:56 1.146
+++ src/libexec/ld.elf_so/rtld.h 2023/07/30 09:20:14 1.147
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: rtld.h,v 1.146 2023/06/04 01:24:56 joerg Exp $ */ 1/* $NetBSD: rtld.h,v 1.147 2023/07/30 09:20:14 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright 1996 John D. Polstra. 4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt@3am-software.com> 5 * Copyright 1996 Matt Thomas <matt@3am-software.com>
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
@@ -432,28 +432,26 @@ int _rtld_relocate_objects(Obj_Entry *,  @@ -432,28 +432,26 @@ int _rtld_relocate_objects(Obj_Entry *,
432int _rtld_relocate_nonplt_objects(Obj_Entry *); 432int _rtld_relocate_nonplt_objects(Obj_Entry *);
433int _rtld_relocate_plt_lazy(Obj_Entry *); 433int _rtld_relocate_plt_lazy(Obj_Entry *);
434int _rtld_relocate_plt_objects(const Obj_Entry *); 434int _rtld_relocate_plt_objects(const Obj_Entry *);
435void _rtld_setup_pltgot(const Obj_Entry *); 435void _rtld_setup_pltgot(const Obj_Entry *);
436Elf_Addr _rtld_resolve_ifunc(const Obj_Entry *, const Elf_Sym *); 436Elf_Addr _rtld_resolve_ifunc(const Obj_Entry *, const Elf_Sym *);
437Elf_Addr _rtld_resolve_ifunc2(const Obj_Entry *, Elf_Addr); 437Elf_Addr _rtld_resolve_ifunc2(const Obj_Entry *, Elf_Addr);
438 438
439void _rtld_call_ifunc(Obj_Entry *, sigset_t *, u_int); 439void _rtld_call_ifunc(Obj_Entry *, sigset_t *, u_int);
440 440
441/* search.c */ 441/* search.c */
442Obj_Entry *_rtld_load_library(const char *, const Obj_Entry *, int); 442Obj_Entry *_rtld_load_library(const char *, const Obj_Entry *, int);
443 443
444/* symbol.c */ 444/* symbol.c */
445Elf32_Word _rtld_sysv_hash(const char *); 
446Elf32_Word _rtld_gnu_hash(const char *); 
447const Elf_Sym *_rtld_symlook_obj(const char *, Elf_Hash *, 445const Elf_Sym *_rtld_symlook_obj(const char *, Elf_Hash *,
448 const Obj_Entry *, u_int, const Ver_Entry *); 446 const Obj_Entry *, u_int, const Ver_Entry *);
449const Elf_Sym *_rtld_find_symdef(unsigned long, const Obj_Entry *, 447const Elf_Sym *_rtld_find_symdef(unsigned long, const Obj_Entry *,
450 const Obj_Entry **, u_int); 448 const Obj_Entry **, u_int);
451const Elf_Sym *_rtld_find_plt_symdef(unsigned long, const Obj_Entry *, 449const Elf_Sym *_rtld_find_plt_symdef(unsigned long, const Obj_Entry *,
452 const Obj_Entry **, bool); 450 const Obj_Entry **, bool);
453 451
454const Elf_Sym *_rtld_symlook_list(const char *, Elf_Hash *, 452const Elf_Sym *_rtld_symlook_list(const char *, Elf_Hash *,
455 const Objlist *, const Obj_Entry **, u_int, const Ver_Entry *, DoneList *); 453 const Objlist *, const Obj_Entry **, u_int, const Ver_Entry *, DoneList *);
456const Elf_Sym *_rtld_symlook_default(const char *, Elf_Hash *, 454const Elf_Sym *_rtld_symlook_default(const char *, Elf_Hash *,
457 const Obj_Entry *, const Obj_Entry **, u_int, const Ver_Entry *); 455 const Obj_Entry *, const Obj_Entry **, u_int, const Ver_Entry *);
458const Elf_Sym *_rtld_symlook_needed(const char *, Elf_Hash *, 456const Elf_Sym *_rtld_symlook_needed(const char *, Elf_Hash *,
459 const Needed_Entry *, const Obj_Entry **, u_int, const Ver_Entry *, 457 const Needed_Entry *, const Obj_Entry **, u_int, const Ver_Entry *,

File Added: src/libexec/ld.elf_so/hash.c
/*	$NetBSD: hash.c,v 1.1 2023/07/30 09:20:14 riastradh Exp $	 */

/*
 * Copyright 1996 John D. Polstra.
 * Copyright 1996 Matt Thomas <matt@3am-software.com>
 * Copyright 2002 Charles M. Hannum <root@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 <jdp@polstra.com>.
 */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: hash.c,v 1.1 2023/07/30 09:20:14 riastradh 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_sysv_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);
}

/*
 * Hash function for symbol table lookup.  Don't even think about changing
 * this.  It is specified by the GNU toolchain ABI.
 */
Elf32_Word
_rtld_gnu_hash(const char *name)
{
	const unsigned char *p = (const unsigned char *) name;
	uint_fast32_t h = 5381;
	unsigned char c;

	for (c = *p; c != '\0'; c = *++p)
		h = h * 33 + c;
	return (h & 0xffffffff);
}

File Added: src/libexec/ld.elf_so/hash.h
/*	$NetBSD: hash.h,v 1.1 2023/07/30 09:20:14 riastradh Exp $	 */

/*
 * Copyright 1996 John D. Polstra.
 * Copyright 1996 Matt Thomas <matt@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_sysv_hash(const char *);
Elf32_Word _rtld_gnu_hash(const char *);

#endif	/* RTLD_HASH_H */

cvs diff -r1.117 -r1.118 src/libexec/ld.elf_so/reloc.c (expand / switch to unified diff)

--- src/libexec/ld.elf_so/reloc.c 2021/12/04 08:53:34 1.117
+++ src/libexec/ld.elf_so/reloc.c 2023/07/30 09:20:14 1.118
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: reloc.c,v 1.117 2021/12/04 08:53:34 skrll Exp $ */ 1/* $NetBSD: reloc.c,v 1.118 2023/07/30 09:20:14 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright 1996 John D. Polstra. 4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt@3am-software.com> 5 * Copyright 1996 Matt Thomas <matt@3am-software.com>
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
@@ -29,43 +29,44 @@ @@ -29,43 +29,44 @@
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34/* 34/*
35 * Dynamic linker for ELF. 35 * Dynamic linker for ELF.
36 * 36 *
37 * John Polstra <jdp@polstra.com>. 37 * John Polstra <jdp@polstra.com>.
38 */ 38 */
39 39
40#include <sys/cdefs.h> 40#include <sys/cdefs.h>
41#ifndef lint 41#ifndef lint
42__RCSID("$NetBSD: reloc.c,v 1.117 2021/12/04 08:53:34 skrll Exp $"); 42__RCSID("$NetBSD: reloc.c,v 1.118 2023/07/30 09:20:14 riastradh Exp $");
43#endif /* not lint */ 43#endif /* not lint */
44 44
45#include <err.h> 45#include <err.h>
46#include <errno.h> 46#include <errno.h>
47#include <fcntl.h> 47#include <fcntl.h>
48#include <stdarg.h> 48#include <stdarg.h>
49#include <stdio.h> 49#include <stdio.h>
50#include <stdlib.h> 50#include <stdlib.h>
51#include <string.h> 51#include <string.h>
52#include <unistd.h> 52#include <unistd.h>
53#include <sys/types.h> 53#include <sys/types.h>
54#include <sys/mman.h> 54#include <sys/mman.h>
55#include <sys/bitops.h> 55#include <sys/bitops.h>
56#include <dirent.h> 56#include <dirent.h>
57 57
58#include "debug.h" 58#include "debug.h"
 59#include "hash.h"
59#include "rtld.h" 60#include "rtld.h"
60 61
61#ifndef RTLD_INHIBIT_COPY_RELOCS 62#ifndef RTLD_INHIBIT_COPY_RELOCS
62static int _rtld_do_copy_relocation(const Obj_Entry *, const Elf_Rela *); 63static int _rtld_do_copy_relocation(const Obj_Entry *, const Elf_Rela *);
63 64
64static int 65static int
65_rtld_do_copy_relocation(const Obj_Entry *dstobj, const Elf_Rela *rela) 66_rtld_do_copy_relocation(const Obj_Entry *dstobj, const Elf_Rela *rela)
66{ 67{
67 void *dstaddr = (void *)(dstobj->relocbase + rela->r_offset); 68 void *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
68 const Elf_Sym *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info); 69 const Elf_Sym *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
69 const char *name = dstobj->strtab + dstsym->st_name; 70 const char *name = dstobj->strtab + dstsym->st_name;
70 Elf_Hash hash; 71 Elf_Hash hash;
71 size_t size = dstsym->st_size; 72 size_t size = dstsym->st_size;

cvs diff -r1.214 -r1.215 src/libexec/ld.elf_so/rtld.c (expand / switch to unified diff)

--- src/libexec/ld.elf_so/rtld.c 2023/06/04 23:42:38 1.214
+++ src/libexec/ld.elf_so/rtld.c 2023/07/30 09:20:14 1.215
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: rtld.c,v 1.214 2023/06/04 23:42:38 riastradh Exp $ */ 1/* $NetBSD: rtld.c,v 1.215 2023/07/30 09:20:14 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright 1996 John D. Polstra. 4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt@3am-software.com> 5 * Copyright 1996 Matt Thomas <matt@3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root@ihack.net> 6 * Copyright 2002 Charles M. Hannum <root@ihack.net>
7 * All rights reserved. 7 * All rights reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
@@ -30,47 +30,49 @@ @@ -30,47 +30,49 @@
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */ 33 */
34 34
35/* 35/*
36 * Dynamic linker for ELF. 36 * Dynamic linker for ELF.
37 * 37 *
38 * John Polstra <jdp@polstra.com>. 38 * John Polstra <jdp@polstra.com>.
39 */ 39 */
40 40
41#include <sys/cdefs.h> 41#include <sys/cdefs.h>
42#ifndef lint 42#ifndef lint
43__RCSID("$NetBSD: rtld.c,v 1.214 2023/06/04 23:42:38 riastradh Exp $"); 43__RCSID("$NetBSD: rtld.c,v 1.215 2023/07/30 09:20:14 riastradh Exp $");
44#endif /* not lint */ 44#endif /* not lint */
45 45
46#include <sys/param.h> 46#include <sys/param.h>
47#include <sys/atomic.h> 47#include <sys/atomic.h>
48#include <sys/mman.h> 48#include <sys/mman.h>
49#include <err.h> 49#include <err.h>
50#include <errno.h> 50#include <errno.h>
51#include <fcntl.h> 51#include <fcntl.h>
52#include <lwp.h> 52#include <lwp.h>
53#include <stdarg.h> 53#include <stdarg.h>
54#include <stdio.h> 54#include <stdio.h>
55#include <stdlib.h> 55#include <stdlib.h>
56#include <string.h> 56#include <string.h>
57#include <unistd.h> 57#include <unistd.h>
58#include <dirent.h> 58#include <dirent.h>
59 59
60#include <ctype.h> 60#include <ctype.h>
61 61
62#include <dlfcn.h> 62#include <dlfcn.h>
 63
63#include "debug.h" 64#include "debug.h"
 65#include "hash.h"
64#include "rtld.h" 66#include "rtld.h"
65 67
66#if !defined(lint) 68#if !defined(lint)
67#include "sysident.h" 69#include "sysident.h"
68#endif 70#endif
69 71
70/* 72/*
71 * Function declarations. 73 * Function declarations.
72 */ 74 */
73static void _rtld_init(caddr_t, caddr_t, const char *); 75static void _rtld_init(caddr_t, caddr_t, const char *);
74static void _rtld_exit(void); 76static void _rtld_exit(void);
75 77
76Elf_Addr _rtld(Elf_Addr *, Elf_Addr); 78Elf_Addr _rtld(Elf_Addr *, Elf_Addr);

cvs diff -r1.75 -r1.76 src/libexec/ld.elf_so/symbol.c (expand / switch to unified diff)

--- src/libexec/ld.elf_so/symbol.c 2023/04/18 22:42:52 1.75
+++ src/libexec/ld.elf_so/symbol.c 2023/07/30 09:20:14 1.76
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: symbol.c,v 1.75 2023/04/18 22:42:52 christos Exp $ */ 1/* $NetBSD: symbol.c,v 1.76 2023/07/30 09:20:14 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright 1996 John D. Polstra. 4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt@3am-software.com> 5 * Copyright 1996 Matt Thomas <matt@3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root@ihack.net> 6 * Copyright 2002 Charles M. Hannum <root@ihack.net>
7 * All rights reserved. 7 * All rights reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
@@ -30,99 +30,67 @@ @@ -30,99 +30,67 @@
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */ 33 */
34 34
35/* 35/*
36 * Dynamic linker for ELF. 36 * Dynamic linker for ELF.
37 * 37 *
38 * John Polstra <jdp@polstra.com>. 38 * John Polstra <jdp@polstra.com>.
39 */ 39 */
40 40
41#include <sys/cdefs.h> 41#include <sys/cdefs.h>
42#ifndef lint 42#ifndef lint
43__RCSID("$NetBSD: symbol.c,v 1.75 2023/04/18 22:42:52 christos Exp $"); 43__RCSID("$NetBSD: symbol.c,v 1.76 2023/07/30 09:20:14 riastradh Exp $");
44#endif /* not lint */ 44#endif /* not lint */
45 45
46#include <err.h> 46#include <err.h>
47#include <errno.h> 47#include <errno.h>
48#include <fcntl.h> 48#include <fcntl.h>
49#include <stdarg.h> 49#include <stdarg.h>
50#include <stdio.h> 50#include <stdio.h>
51#include <stdlib.h> 51#include <stdlib.h>
52#include <string.h> 52#include <string.h>
53#include <unistd.h> 53#include <unistd.h>
54#include <sys/types.h> 54#include <sys/types.h>
55#include <sys/mman.h> 55#include <sys/mman.h>
56#include <sys/bitops.h> 56#include <sys/bitops.h>
57#include <dirent.h> 57#include <dirent.h>
58 58
59#include "debug.h" 59#include "debug.h"
 60#include "hash.h"
60#include "rtld.h" 61#include "rtld.h"
61 62
62/* 63/*
63 * If the given object is already in the donelist, return true. Otherwise 64 * If the given object is already in the donelist, return true. Otherwise
64 * add the object to the list and return false. 65 * add the object to the list and return false.
65 */ 66 */
66static bool 67static bool
67_rtld_donelist_check(DoneList *dlp, const Obj_Entry *obj) 68_rtld_donelist_check(DoneList *dlp, const Obj_Entry *obj)
68{ 69{
69 unsigned int i; 70 unsigned int i;
70 71
71 for (i = 0; i < dlp->num_used; i++) 72 for (i = 0; i < dlp->num_used; i++)
72 if (dlp->objs[i] == obj) 73 if (dlp->objs[i] == obj)
73 return true; 74 return true;
74 /* 75 /*
75 * Our donelist allocation may not always be sufficient as we're not 76 * Our donelist allocation may not always be sufficient as we're not
76 * thread safe. We'll handle it properly anyway. 77 * thread safe. We'll handle it properly anyway.
77 */ 78 */
78 if (dlp->num_used < dlp->num_alloc) 79 if (dlp->num_used < dlp->num_alloc)
79 dlp->objs[dlp->num_used++] = obj; 80 dlp->objs[dlp->num_used++] = obj;
80 return false; 81 return false;
81} 82}
82 83
83/* 
84 * SysV hash function for symbol table lookup. It is a slightly optimized 
85 * version of the hash specified by the System V ABI. 
86 */ 
87Elf32_Word 
88_rtld_sysv_hash(const char *name) 
89{ 
90 const unsigned char *p = (const unsigned char *) name; 
91 Elf32_Word h = 0; 
92 
93 while (__predict_true(*p != '\0')) { 
94 h = (h << 4) + *p++; 
95 h ^= (h >> 24) & 0xf0; 
96 } 
97 return (h & 0x0fffffff); 
98} 
99 
100/* 
101 * Hash function for symbol table lookup. Don't even think about changing 
102 * this. It is specified by the GNU toolchain ABI. 
103 */ 
104Elf32_Word 
105_rtld_gnu_hash(const char *name) 
106{ 
107 const unsigned char *p = (const unsigned char *) name; 
108 uint_fast32_t h = 5381; 
109 unsigned char c; 
110 
111 for (c = *p; c != '\0'; c = *++p) 
112 h = h * 33 + c; 
113 return (h & 0xffffffff); 
114} 
115 
116const Elf_Sym * 84const Elf_Sym *
117_rtld_symlook_list(const char *name, Elf_Hash *hash, const Objlist *objlist, 85_rtld_symlook_list(const char *name, Elf_Hash *hash, const Objlist *objlist,
118 const Obj_Entry **defobj_out, u_int flags, const Ver_Entry *ventry, 86 const Obj_Entry **defobj_out, u_int flags, const Ver_Entry *ventry,
119 DoneList *dlp) 87 DoneList *dlp)
120{ 88{
121 const Elf_Sym *symp; 89 const Elf_Sym *symp;
122 const Elf_Sym *def; 90 const Elf_Sym *def;
123 const Obj_Entry *defobj; 91 const Obj_Entry *defobj;
124 const Objlist_Entry *elm; 92 const Objlist_Entry *elm;
125 93
126 def = NULL; 94 def = NULL;
127 defobj = NULL; 95 defobj = NULL;
128 SIMPLEQ_FOREACH(elm, objlist, link) { 96 SIMPLEQ_FOREACH(elm, objlist, link) {