initfini.c revision 1.12
11.12Sdholland/* $NetBSD: initfini.c,v 1.12 2016/11/26 20:38:20 dholland 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.12Sdholland__RCSID("$NetBSD: initfini.c,v 1.12 2016/11/26 20:38:20 dholland 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.8Sjoerg 441.8Sjoergvoid _libc_init(void) __attribute__((__constructor__, __used__)); 451.1Sad 461.1Sadvoid __guard_setup(void); 471.1Sadvoid __libc_thr_init(void); 481.3Sadvoid __libc_atomic_init(void); 491.4Sxtraemevoid __libc_atexit_init(void); 501.7Stronvoid __libc_env_init(void); 511.1Sad 521.9Sjoerg#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 531.9Sjoerg__dso_hidden void __libc_static_tls_setup(void); 541.9Sjoerg#endif 551.9Sjoerg 561.10Sjoerg#ifdef __weak_alias 571.10Sjoerg__weak_alias(_dlauxinfo,___dlauxinfo) 581.10Sjoergstatic void *__libc_dlauxinfo; 591.10Sjoerg 601.10Sjoergvoid *___dlauxinfo(void) __pure; 611.10Sjoerg 621.10Sjoergvoid * 631.10Sjoerg___dlauxinfo(void) 641.10Sjoerg{ 651.10Sjoerg return __libc_dlauxinfo; 661.10Sjoerg} 671.10Sjoerg#endif 681.10Sjoerg 691.8Sjoergstatic bool libc_initialised; 701.8Sjoerg 711.8Sjoergvoid _libc_init(void); 721.8Sjoerg 731.10Sjoerg/* 741.10Sjoerg * Declare as common symbol to allow new libc with older binaries to 751.10Sjoerg * not trigger an undefined reference. 761.10Sjoerg */ 771.8Sjoergstruct ps_strings *__ps_strings; 781.8Sjoerg 791.8Sjoerg/* 801.12Sdholland * _libc_init is called twice. One call comes explicitly from crt0.o 811.12Sdholland * (for newer versions) and the other is via global constructor handling. 821.12Sdholland * 831.12Sdholland * In static binaries the explicit call is first; in dynamically linked 841.12Sdholland * binaries the global constructors of libc are called from ld.elf_so 851.12Sdholland * before crt0.o is reached. 861.8Sjoerg */ 871.11Smattvoid __section(".text.startup") 881.8Sjoerg_libc_init(void) 891.1Sad{ 901.1Sad 911.8Sjoerg if (libc_initialised) 921.8Sjoerg return; 931.8Sjoerg 941.8Sjoerg libc_initialised = 1; 951.8Sjoerg 961.8Sjoerg if (__ps_strings != NULL) 971.10Sjoerg __libc_dlauxinfo = __ps_strings->ps_argvstr + 981.8Sjoerg __ps_strings->ps_nargvstr + __ps_strings->ps_nenvstr + 2; 991.8Sjoerg 1001.1Sad /* For -fstack-protector */ 1011.1Sad __guard_setup(); 1021.1Sad 1031.3Sad /* Atomic operations */ 1041.3Sad __libc_atomic_init(); 1051.3Sad 1061.9Sjoerg#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 1071.9Sjoerg /* Initialize TLS for statically linked programs. */ 1081.9Sjoerg __libc_static_tls_setup(); 1091.9Sjoerg#endif 1101.9Sjoerg 1111.1Sad /* Threads */ 1121.1Sad __libc_thr_init(); 1131.4Sxtraeme 1141.4Sxtraeme /* Initialize the atexit mutexes */ 1151.4Sxtraeme __libc_atexit_init(); 1161.7Stron 1171.7Stron /* Initialize environment memory RB tree. */ 1181.7Stron __libc_env_init(); 1191.1Sad} 120