xref: /netbsd-src/lib/libc/misc/initfini.c (revision ace5b9b5feb0e7608bd2da7a617428d2e1cf8aa3)
1*ace5b9b5Schristos /* 	$NetBSD: initfini.c,v 1.17 2024/01/20 14:52:48 christos Exp $	 */
2ec4d182dSad 
3ec4d182dSad /*-
4ec4d182dSad  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5ec4d182dSad  * All rights reserved.
6ec4d182dSad  *
7ec4d182dSad  * This code is derived from software contributed to The NetBSD Foundation
8ec4d182dSad  * by Andrew Doran.
9ec4d182dSad  *
10ec4d182dSad  * Redistribution and use in source and binary forms, with or without
11ec4d182dSad  * modification, are permitted provided that the following conditions
12ec4d182dSad  * are met:
13ec4d182dSad  * 1. Redistributions of source code must retain the above copyright
14ec4d182dSad  *    notice, this list of conditions and the following disclaimer.
15ec4d182dSad  * 2. Redistributions in binary form must reproduce the above copyright
16ec4d182dSad  *    notice, this list of conditions and the following disclaimer in the
17ec4d182dSad  *    documentation and/or other materials provided with the distribution.
18ec4d182dSad  *
19ec4d182dSad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20ec4d182dSad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21ec4d182dSad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22ec4d182dSad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23ec4d182dSad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24ec4d182dSad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25ec4d182dSad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26ec4d182dSad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27ec4d182dSad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28ec4d182dSad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29ec4d182dSad  * POSSIBILITY OF SUCH DAMAGE.
30ec4d182dSad  */
31ec4d182dSad 
3246f5cd4fSad #include <sys/cdefs.h>
33*ace5b9b5Schristos __RCSID("$NetBSD: initfini.c,v 1.17 2024/01/20 14:52:48 christos Exp $");
3446f5cd4fSad 
3546f5cd4fSad #ifdef _LIBC
3646f5cd4fSad #include "namespace.h"
3746f5cd4fSad #endif
3846f5cd4fSad 
395c3ccd6eSjoerg #include <sys/param.h>
405c3ccd6eSjoerg #include <sys/exec.h>
41aad59997Sjoerg #include <sys/tls.h>
425c3ccd6eSjoerg #include <stdbool.h>
43*ace5b9b5Schristos #include "extern.h"
44c510faceSchristos #include "csu-common.h"
455c3ccd6eSjoerg 
46ec4d182dSad void	__guard_setup(void);
47ec4d182dSad void	__libc_thr_init(void);
4895157b04Sxtraeme void	__libc_atexit_init(void);
49fbf4aa16Stron void	__libc_env_init(void);
50ec4d182dSad 
51aad59997Sjoerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
52aad59997Sjoerg __dso_hidden void	__libc_static_tls_setup(void);
53aad59997Sjoerg #endif
54aad59997Sjoerg 
5552a4c27eSjoerg #ifdef __weak_alias
5652a4c27eSjoerg __weak_alias(_dlauxinfo,___dlauxinfo)
5752a4c27eSjoerg static void *__libc_dlauxinfo;
5852a4c27eSjoerg 
5952a4c27eSjoerg void *___dlauxinfo(void) __pure;
6054eae2a1Sjoerg __weakref_visible void * real_dlauxinfo(void) __weak_reference(_dlauxinfo);
6152a4c27eSjoerg 
6252a4c27eSjoerg void *
___dlauxinfo(void)6352a4c27eSjoerg ___dlauxinfo(void)
6452a4c27eSjoerg {
6552a4c27eSjoerg 	return __libc_dlauxinfo;
6652a4c27eSjoerg }
6752a4c27eSjoerg #endif
6852a4c27eSjoerg 
695c3ccd6eSjoerg static bool libc_initialised;
705c3ccd6eSjoerg 
7152a4c27eSjoerg /*
7252a4c27eSjoerg  * Declare as common symbol to allow new libc with older binaries to
7352a4c27eSjoerg  * not trigger an undefined reference.
7452a4c27eSjoerg  */
75c510faceSchristos struct ps_strings *__ps_strings __common;
76c510faceSchristos char *__progname __common;
77c510faceSchristos char **environ __common;
785c3ccd6eSjoerg 
795c3ccd6eSjoerg /*
80a714102aSdholland  * _libc_init is called twice.  One call comes explicitly from crt0.o
81a714102aSdholland  * (for newer versions) and the other is via global constructor handling.
82a714102aSdholland  *
83a714102aSdholland  * In static binaries the explicit call is first; in dynamically linked
84a714102aSdholland  * binaries the global constructors of libc are called from ld.elf_so
85a714102aSdholland  * before crt0.o is reached.
86aa08bfeaSdholland  *
87aa08bfeaSdholland  * Note that __ps_strings is set by crt0.o. So in the dynamic case, it
88aa08bfeaSdholland  * hasn't been set yet when we get here, and __libc_dlauxinfo is not
89aa08bfeaSdholland  * (ever) assigned. But this is ok because __libc_dlauxinfo is only
90aa08bfeaSdholland  * used in static binaries, because it's there to substitute for the
91aa08bfeaSdholland  * dynamic linker. In static binaries __ps_strings will have been set
92aa08bfeaSdholland  * up when we get here and we get a valid __libc_dlauxinfo.
93aa08bfeaSdholland  *
94aa08bfeaSdholland  * This code causes problems for Emacs because Emacs's undump
95aa08bfeaSdholland  * mechanism saves the __ps_strings value from the startup execution;
96aa08bfeaSdholland  * then running the resulting binary it gets here before crt0 has
97aa08bfeaSdholland  * assigned the current execution's value to __ps_strings, and in an
98aa08bfeaSdholland  * environment with ASLR this can cause the assignment of
99aa08bfeaSdholland  * __libc_dlauxinfo to receive SIGSEGV.
1005c3ccd6eSjoerg  */
1018caf1030Smatt void __section(".text.startup")
_libc_init(void)1025c3ccd6eSjoerg _libc_init(void)
103ec4d182dSad {
104ec4d182dSad 
1055c3ccd6eSjoerg 	if (libc_initialised)
1065c3ccd6eSjoerg 		return;
1075c3ccd6eSjoerg 
1085c3ccd6eSjoerg 	libc_initialised = 1;
1095c3ccd6eSjoerg 
11054eae2a1Sjoerg 	/* Only initialize _dlauxinfo for static binaries. */
11154eae2a1Sjoerg 	if (__ps_strings != NULL && real_dlauxinfo == ___dlauxinfo)
11252a4c27eSjoerg 		__libc_dlauxinfo = __ps_strings->ps_argvstr +
1135c3ccd6eSjoerg 		    __ps_strings->ps_nargvstr + __ps_strings->ps_nenvstr + 2;
1145c3ccd6eSjoerg 
115ec4d182dSad 	/* For -fstack-protector */
116ec4d182dSad 	__guard_setup();
117ec4d182dSad 
118b5b3e9fbSad 	/* Atomic operations */
119b5b3e9fbSad 	__libc_atomic_init();
120b5b3e9fbSad 
121aad59997Sjoerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
122aad59997Sjoerg 	/* Initialize TLS for statically linked programs. */
123aad59997Sjoerg 	__libc_static_tls_setup();
124aad59997Sjoerg #endif
125aad59997Sjoerg 
126ec4d182dSad 	/* Threads */
127ec4d182dSad 	__libc_thr_init();
12895157b04Sxtraeme 
12995157b04Sxtraeme 	/* Initialize the atexit mutexes */
13095157b04Sxtraeme 	__libc_atexit_init();
131ec4d182dSad }
132