initfini.c revision 1.15
11.15Schristos/* 	$NetBSD: initfini.c,v 1.15 2021/04/20 21:42:32 christos Exp $	 */
21.1Sad
31.1Sad/*-
41.1Sad * Copyright (c) 2007 The NetBSD Foundation, Inc.
51.1Sad * All rights reserved.
61.1Sad *
71.1Sad * This code is derived from software contributed to The NetBSD Foundation
81.1Sad * by Andrew Doran.
91.1Sad *
101.1Sad * Redistribution and use in source and binary forms, with or without
111.1Sad * modification, are permitted provided that the following conditions
121.1Sad * are met:
131.1Sad * 1. Redistributions of source code must retain the above copyright
141.1Sad *    notice, this list of conditions and the following disclaimer.
151.1Sad * 2. Redistributions in binary form must reproduce the above copyright
161.1Sad *    notice, this list of conditions and the following disclaimer in the
171.1Sad *    documentation and/or other materials provided with the distribution.
181.1Sad *
191.1Sad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sad * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sad * POSSIBILITY OF SUCH DAMAGE.
301.1Sad */
311.1Sad
321.2Sad#include <sys/cdefs.h>
331.15Schristos__RCSID("$NetBSD: initfini.c,v 1.15 2021/04/20 21:42:32 christos Exp $");
341.2Sad
351.2Sad#ifdef _LIBC
361.2Sad#include "namespace.h"
371.2Sad#endif
381.2Sad
391.8Sjoerg#include <sys/param.h>
401.8Sjoerg#include <sys/exec.h>
411.9Sjoerg#include <sys/tls.h>
421.8Sjoerg#include <stdbool.h>
431.15Schristos#include "csu-common.h"
441.8Sjoerg
451.8Sjoergvoid	_libc_init(void) __attribute__((__constructor__, __used__));
461.1Sad
471.1Sadvoid	__guard_setup(void);
481.1Sadvoid	__libc_thr_init(void);
491.3Sadvoid	__libc_atomic_init(void);
501.4Sxtraemevoid	__libc_atexit_init(void);
511.7Stronvoid	__libc_env_init(void);
521.1Sad
531.9Sjoerg#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
541.9Sjoerg__dso_hidden void	__libc_static_tls_setup(void);
551.9Sjoerg#endif
561.9Sjoerg
571.10Sjoerg#ifdef __weak_alias
581.10Sjoerg__weak_alias(_dlauxinfo,___dlauxinfo)
591.10Sjoergstatic void *__libc_dlauxinfo;
601.10Sjoerg
611.10Sjoergvoid *___dlauxinfo(void) __pure;
621.14Sjoerg__weakref_visible void * real_dlauxinfo(void) __weak_reference(_dlauxinfo);
631.10Sjoerg
641.10Sjoergvoid *
651.10Sjoerg___dlauxinfo(void)
661.10Sjoerg{
671.10Sjoerg	return __libc_dlauxinfo;
681.10Sjoerg}
691.10Sjoerg#endif
701.10Sjoerg
711.8Sjoergstatic bool libc_initialised;
721.8Sjoerg
731.8Sjoergvoid _libc_init(void);
741.8Sjoerg
751.10Sjoerg/*
761.10Sjoerg * Declare as common symbol to allow new libc with older binaries to
771.10Sjoerg * not trigger an undefined reference.
781.10Sjoerg */
791.15Schristosstruct ps_strings *__ps_strings __common;
801.15Schristoschar *__progname __common;
811.15Schristoschar **environ __common;
821.8Sjoerg
831.8Sjoerg/*
841.12Sdholland * _libc_init is called twice.  One call comes explicitly from crt0.o
851.12Sdholland * (for newer versions) and the other is via global constructor handling.
861.12Sdholland *
871.12Sdholland * In static binaries the explicit call is first; in dynamically linked
881.12Sdholland * binaries the global constructors of libc are called from ld.elf_so
891.12Sdholland * before crt0.o is reached.
901.13Sdholland *
911.13Sdholland * Note that __ps_strings is set by crt0.o. So in the dynamic case, it
921.13Sdholland * hasn't been set yet when we get here, and __libc_dlauxinfo is not
931.13Sdholland * (ever) assigned. But this is ok because __libc_dlauxinfo is only
941.13Sdholland * used in static binaries, because it's there to substitute for the
951.13Sdholland * dynamic linker. In static binaries __ps_strings will have been set
961.13Sdholland * up when we get here and we get a valid __libc_dlauxinfo.
971.13Sdholland *
981.13Sdholland * This code causes problems for Emacs because Emacs's undump
991.13Sdholland * mechanism saves the __ps_strings value from the startup execution;
1001.13Sdholland * then running the resulting binary it gets here before crt0 has
1011.13Sdholland * assigned the current execution's value to __ps_strings, and in an
1021.13Sdholland * environment with ASLR this can cause the assignment of
1031.13Sdholland * __libc_dlauxinfo to receive SIGSEGV.
1041.8Sjoerg */
1051.11Smattvoid __section(".text.startup")
1061.8Sjoerg_libc_init(void)
1071.1Sad{
1081.1Sad
1091.8Sjoerg	if (libc_initialised)
1101.8Sjoerg		return;
1111.8Sjoerg
1121.8Sjoerg	libc_initialised = 1;
1131.8Sjoerg
1141.14Sjoerg	/* Only initialize _dlauxinfo for static binaries. */
1151.14Sjoerg	if (__ps_strings != NULL && real_dlauxinfo == ___dlauxinfo)
1161.10Sjoerg		__libc_dlauxinfo = __ps_strings->ps_argvstr +
1171.8Sjoerg		    __ps_strings->ps_nargvstr + __ps_strings->ps_nenvstr + 2;
1181.8Sjoerg
1191.1Sad	/* For -fstack-protector */
1201.1Sad	__guard_setup();
1211.1Sad
1221.3Sad	/* Atomic operations */
1231.3Sad	__libc_atomic_init();
1241.3Sad
1251.9Sjoerg#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
1261.9Sjoerg	/* Initialize TLS for statically linked programs. */
1271.9Sjoerg	__libc_static_tls_setup();
1281.9Sjoerg#endif
1291.9Sjoerg
1301.1Sad	/* Threads */
1311.1Sad	__libc_thr_init();
1321.4Sxtraeme
1331.4Sxtraeme	/* Initialize the atexit mutexes */
1341.4Sxtraeme	__libc_atexit_init();
1351.7Stron
1361.7Stron	/* Initialize environment memory RB tree. */
1371.7Stron	__libc_env_init();
1381.1Sad}
139