1 /* $NetBSD: crtbegin.c,v 1.28 2006/05/19 19:11:12 christos Exp $ */
4 * Copyright (c) 1998, 2001, 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg, Ross Harvey, and Jason R. Thorpe.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Run-time module which handles constructors and destructors,
34 * and NetBSD identification.
36 * The linker constructs the following arrays of pointers to global
37 * constructors and destructors. The first element contains the
38 * number of pointers in each or -1 to indicate that the run-time
39 * code should figure out how many there are. The tables are also
43 #include <sys/param.h>
45 #include <sys/exec_elf.h>
49 * WE SHOULD BE USING GCC-SUPPLIED crtbegin.o FOR GCC 3.3 AND
52 #if __GNUC_PREREQ__(3, 3)
53 #error "Use GCC-supplied crtbegin.o"
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
)