184d9c625SLionel Sambuc /* $NetBSD: initfini.c,v 1.11 2013/08/19 22:14:37 matt Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c) 2007 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras * by Andrew Doran.
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras * are met:
132fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202fe8fb19SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212fe8fb19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222fe8fb19SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232fe8fb19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242fe8fb19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252fe8fb19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262fe8fb19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272fe8fb19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282fe8fb19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include <sys/cdefs.h>
3384d9c625SLionel Sambuc __RCSID("$NetBSD: initfini.c,v 1.11 2013/08/19 22:14:37 matt Exp $");
342fe8fb19SBen Gras
352fe8fb19SBen Gras #ifdef _LIBC
362fe8fb19SBen Gras #include "namespace.h"
372fe8fb19SBen Gras #endif
382fe8fb19SBen Gras
39f14fb602SLionel Sambuc #include <sys/param.h>
40f14fb602SLionel Sambuc #include <sys/exec.h>
41f14fb602SLionel Sambuc #include <sys/tls.h>
42f14fb602SLionel Sambuc #include <stdbool.h>
43f14fb602SLionel Sambuc
44f14fb602SLionel Sambuc void _libc_init(void) __attribute__((__constructor__, __used__));
452fe8fb19SBen Gras
462fe8fb19SBen Gras void __guard_setup(void);
472fe8fb19SBen Gras void __libc_thr_init(void);
482fe8fb19SBen Gras void __libc_atomic_init(void);
492fe8fb19SBen Gras void __libc_atexit_init(void);
502fe8fb19SBen Gras void __libc_env_init(void);
512fe8fb19SBen Gras
52f14fb602SLionel Sambuc #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
53f14fb602SLionel Sambuc __dso_hidden void __libc_static_tls_setup(void);
54f14fb602SLionel Sambuc #endif
55f14fb602SLionel Sambuc
56f14fb602SLionel Sambuc #ifdef __weak_alias
__weak_alias(_dlauxinfo,___dlauxinfo)57f14fb602SLionel Sambuc __weak_alias(_dlauxinfo,___dlauxinfo)
58f14fb602SLionel Sambuc static void *__libc_dlauxinfo;
59f14fb602SLionel Sambuc
60f14fb602SLionel Sambuc void *___dlauxinfo(void) __pure;
61f14fb602SLionel Sambuc
62f14fb602SLionel Sambuc void *
63f14fb602SLionel Sambuc ___dlauxinfo(void)
642fe8fb19SBen Gras {
65f14fb602SLionel Sambuc return __libc_dlauxinfo;
66f14fb602SLionel Sambuc }
67f14fb602SLionel Sambuc #endif
68f14fb602SLionel Sambuc
69f14fb602SLionel Sambuc static bool libc_initialised;
70f14fb602SLionel Sambuc
71f14fb602SLionel Sambuc void _libc_init(void);
72f14fb602SLionel Sambuc
73f14fb602SLionel Sambuc /*
74f14fb602SLionel Sambuc * Declare as common symbol to allow new libc with older binaries to
75f14fb602SLionel Sambuc * not trigger an undefined reference.
76f14fb602SLionel Sambuc */
77f14fb602SLionel Sambuc struct ps_strings *__ps_strings;
78f14fb602SLionel Sambuc
79f14fb602SLionel Sambuc /*
80f14fb602SLionel Sambuc * _libc_init is called twice. The first time explicitly by crt0.o
81f14fb602SLionel Sambuc * (for newer versions) and the second time as indirectly via _init().
82f14fb602SLionel Sambuc */
8384d9c625SLionel Sambuc void __section(".text.startup")
_libc_init(void)84f14fb602SLionel Sambuc _libc_init(void)
85f14fb602SLionel Sambuc {
86f14fb602SLionel Sambuc
87f14fb602SLionel Sambuc if (libc_initialised)
88f14fb602SLionel Sambuc return;
89f14fb602SLionel Sambuc
90f14fb602SLionel Sambuc libc_initialised = 1;
91f14fb602SLionel Sambuc
92f14fb602SLionel Sambuc if (__ps_strings != NULL)
93f14fb602SLionel Sambuc __libc_dlauxinfo = __ps_strings->ps_argvstr +
94f14fb602SLionel Sambuc __ps_strings->ps_nargvstr + __ps_strings->ps_nenvstr + 2;
952fe8fb19SBen Gras
962fe8fb19SBen Gras /* For -fstack-protector */
972fe8fb19SBen Gras __guard_setup();
982fe8fb19SBen Gras
99*0a6a1f1dSLionel Sambuc #if defined(__minix) && defined(_REENTRANT)
1002fe8fb19SBen Gras /* Atomic operations */
1012fe8fb19SBen Gras __libc_atomic_init();
102*0a6a1f1dSLionel Sambuc #endif /* defined(__minix) && defined(_REENTRANT) */
103f14fb602SLionel Sambuc
104f14fb602SLionel Sambuc #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
105f14fb602SLionel Sambuc /* Initialize TLS for statically linked programs. */
106f14fb602SLionel Sambuc __libc_static_tls_setup();
107f14fb602SLionel Sambuc #endif
1082fe8fb19SBen Gras
109*0a6a1f1dSLionel Sambuc #if defined(__minix) && defined(_REENTRANT)
1102fe8fb19SBen Gras /* Threads */
1112fe8fb19SBen Gras __libc_thr_init();
112*0a6a1f1dSLionel Sambuc #endif /* defined(__minix) && defined(_REENTRANT) */
1132fe8fb19SBen Gras
1142fe8fb19SBen Gras /* Initialize the atexit mutexes */
1152fe8fb19SBen Gras __libc_atexit_init();
1162fe8fb19SBen Gras
1172fe8fb19SBen Gras /* Initialize environment memory RB tree. */
1182fe8fb19SBen Gras __libc_env_init();
1192fe8fb19SBen Gras }
120