1 /* $NetBSD: crtbegin.c,v 1.23 2003/12/02 03:01:19 lukem Exp $ */
2 /* $DragonFly: src/lib/csu/common/Attic/crtbegin.c,v 1.3 2004/06/14 18:56:12 joerg Exp $ */
5 * Copyright (c) 1998, 2001, 2002 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Paul Kranenburg, Ross Harvey, and Jason R. Thorpe.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
41 * Run-time module which handles constructors and destructors,
42 * and NetBSD identification.
44 * The linker constructs the following arrays of pointers to global
45 * constructors and destructors. The first element contains the
46 * number of pointers in each or -1 to indicate that the run-time
47 * code should figure out how many there are. The tables are also
51 #include <sys/param.h> /* sysident.h requires `NetBSD' constant */
53 #include <sys/elf_common.h>
56 #if __GNUC_PREREQ__(3, 0)
57 #define USED_NOINLINE __attribute__((__used__,__noinline__))
59 #define USED_NOINLINE __attribute__((__unused__))
63 #include "dwarf2_eh.h"
67 static void (*__CTOR_LIST__[1])(void)
68 __attribute__((section(".ctors"))) = { (void *)-1 }; /* XXX */
69 static void (*__DTOR_LIST__[1])(void)
70 __attribute__((section(".dtors"))) = { (void *)-1 }; /* XXX */
73 static __EH_FRAME_CONST char __EH_FRAME_BEGIN__[]
74 __attribute__((section(".eh_frame"), aligned(4))) = { };
77 #if defined(JCR) && defined(__GNUC__)
78 extern void _Jv_RegisterClasses(void *) __attribute__((weak));
80 static void *__JCR_LIST__[]
81 __attribute__((section(".jcr"))) = { };
84 #if defined(DSO_HANDLE) && defined(__GNUC__)
86 * The __dso_handle variable is used to hang C++ local destructors off
87 * of. In the main program (i.e. using crtbegin.o), the value is 0.
88 * In shared objects (i.e. using crtbeginS.o), the value must be unique.
89 * The symbol is hidden, but the dynamic linker will still relocate it.
92 void *__dso_handle = &__dso_handle;
94 void *__dso_handle = NULL;
96 __asm(".hidden __dso_handle");
99 extern void __cxa_finalize(void *) __attribute__((weak));
103 #ifndef MD_CALL_STATIC_FUNCTION
104 #if defined(__GNUC__)
105 #define MD_CALL_STATIC_FUNCTION(section, func) \
106 static void __attribute__((__unused__)) \
107 __call_##func(void) \
109 __asm __volatile (".section " #section); \
111 __asm __volatile (".previous"); \
114 #error Need MD_CALL_STATIC_FUNCTION
116 #endif /* ! MD_CALL_STATIC_FUNCTION */
121 unsigned long i = (unsigned long) __CTOR_LIST__[0];
124 if (i == (unsigned long) -1) {
125 for (i = 1; __CTOR_LIST__[i] != NULL; i++)
129 p = __CTOR_LIST__ + i;
137 void (**p)(void) = __DTOR_LIST__ + 1;
143 static void USED_NOINLINE
144 __do_global_ctors_aux(void)
146 static int initialized;
148 #if defined(__GNUC__)
149 static struct dwarf2_eh_object object;
150 #endif /* __GNUC__ */
151 #endif /* DWARF2_EH */
157 #if defined(__GNUC__)
158 if (__register_frame_info != NULL)
159 __register_frame_info(__EH_FRAME_BEGIN__, &object);
160 #endif /* __GNUC__ */
161 #endif /* DWARF2_EH */
163 #if defined(JCR) && defined(__GNUC__)
164 if (__JCR_LIST__[0] != NULL && _Jv_RegisterClasses != NULL)
165 _Jv_RegisterClasses(__JCR_LIST__);
166 #endif /* JCR && __GNUC__ */
169 * Call global constructors.
174 MD_CALL_STATIC_FUNCTION(.init, __do_global_ctors_aux)
176 static void USED_NOINLINE
177 __do_global_dtors_aux(void)
184 #if defined(DSO_HANDLE) && defined(__GNUC__) && defined(SHARED)
186 * Call local destructors.
188 if (__cxa_finalize != NULL)
189 __cxa_finalize(__dso_handle);
190 #endif /* DSO_HANDLE && __GNUC__ && SHARED */
193 * Call global destructors.
198 #if defined(__GNUC__)
199 if (__deregister_frame_info != NULL)
200 __deregister_frame_info(__EH_FRAME_BEGIN__);
201 #endif /* __GNUC__ */
202 #endif /* DWARF2_EH */
206 MD_CALL_STATIC_FUNCTION(.fini, __do_global_dtors_aux)