1 /* $OpenBSD: crtbeginS.c,v 1.11 2010/05/01 11:32:43 kettenis Exp $ */ 2 /* $NetBSD: crtbegin.c,v 1.1 1996/09/12 16:59:03 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1993 Paul Kranenburg 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Paul Kranenburg. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 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 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Run-time module for GNU C++ compiled shared libraries. 36 * 37 * The linker constructs the following arrays of pointers to global 38 * constructors and destructors. The first element contains the 39 * number of pointers in each. 40 * The tables are also null-terminated. 41 42 */ 43 #include <stdlib.h> 44 45 #include "md_init.h" 46 #include "os-note-elf.h" 47 #include "extern.h" 48 49 /* 50 * java class registration hooks 51 */ 52 53 #if (__GNUC__ > 2) 54 static void *__JCR_LIST__[] 55 __attribute__((section(".jcr"), aligned(sizeof(void*)))) = { }; 56 57 extern void _Jv_RegisterClasses (void *) 58 __attribute__((weak)); 59 #endif 60 61 /* 62 * Include support for the __cxa_atexit/__cxa_finalize C++ abi for 63 * gcc > 2.x. __dso_handle is NULL in the main program and a unique 64 * value for each C++ shared library. For more info on this API, see: 65 * 66 * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor 67 */ 68 69 #if (__GNUC__ > 2) 70 void *__dso_handle = &__dso_handle; 71 __asm(".hidden __dso_handle"); 72 73 extern void __cxa_finalize(void *) __attribute__((weak)); 74 #endif 75 76 static init_f __CTOR_LIST__[1] 77 __attribute__((section(".ctors"))) = { (void *)-1 }; /* XXX */ 78 static init_f __DTOR_LIST__[1] 79 __attribute__((section(".dtors"))) = { (void *)-1 }; /* XXX */ 80 81 static void __dtors(void) __used; 82 static void __ctors(void) __used; 83 84 void 85 __dtors(void) 86 { 87 unsigned long i = (unsigned long) __DTOR_LIST__[0]; 88 init_f *p; 89 90 if (i == -1) { 91 for (i = 1; __DTOR_LIST__[i] != NULL; i++) 92 ; 93 i--; 94 } 95 p = __DTOR_LIST__ + i; 96 while (i--) { 97 (**p--)(); 98 } 99 } 100 101 static void 102 __ctors(void) 103 { 104 init_f *p = __CTOR_LIST__ + 1; 105 106 while (*p) { 107 (**p++)(); 108 } 109 } 110 void _init(void); 111 void _fini(void); 112 static void _do_init(void) __used; 113 static void _do_fini(void) __used; 114 115 MD_SECTION_PROLOGUE(".init", _init); 116 117 MD_SECTION_PROLOGUE(".fini", _fini); 118 119 MD_SECT_CALL_FUNC(".init", _do_init); 120 MD_SECT_CALL_FUNC(".fini", _do_fini); 121 122 void 123 _do_init(void) 124 { 125 static int initialized = 0; 126 127 /* 128 * Call global constructors. 129 * Arrange to call global destructors at exit. 130 */ 131 if (!initialized) { 132 initialized = 1; 133 134 #if (__GNUC__ > 2) 135 if (__JCR_LIST__[0] && _Jv_RegisterClasses) 136 _Jv_RegisterClasses(__JCR_LIST__); 137 #endif 138 139 __ctors(); 140 } 141 } 142 143 void 144 _do_fini(void) 145 { 146 static int finalized = 0; 147 if (!finalized) { 148 finalized = 1; 149 150 #if (__GNUC__ > 2) 151 if (__cxa_finalize != NULL) 152 __cxa_finalize(__dso_handle); 153 #endif 154 155 /* 156 * since the _init() function sets up the destructors to 157 * be called by atexit, do not call the destructors here. 158 */ 159 __dtors(); 160 } 161 } 162