Sync usage with man page.
[netbsd-mini2440.git] / lib / csu / common_elf / crtbegin.c
blob57be121f626e70df82211f75118f51fe59882bdd
1 /* $NetBSD: crtbegin.c,v 1.28 2006/05/19 19:11:12 christos Exp $ */
3 /*-
4 * Copyright (c) 1998, 2001, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
12 * are met:
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
40 * null-terminated.
43 #include <sys/param.h>
44 #include <sys/exec.h>
45 #include <sys/exec_elf.h>
46 #include <stdlib.h>
49 * WE SHOULD BE USING GCC-SUPPLIED crtbegin.o FOR GCC 3.3 AND
50 * LATER!!!
52 #if __GNUC_PREREQ__(3, 3)
53 #error "Use GCC-supplied crtbegin.o"
54 #endif
56 #if __GNUC_PREREQ__(3, 0)
57 #define USED_NOINLINE __attribute__((__used__,__noinline__))
58 #else
59 #define USED_NOINLINE __attribute__((__unused__))
60 #endif
62 #ifdef DWARF2_EH
63 #include "dwarf2_eh.h"
64 #endif
65 #include <dot_init.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 */
72 #ifdef DWARF2_EH
73 static __EH_FRAME_CONST char __EH_FRAME_BEGIN__[]
74 __attribute__((section(".eh_frame"), aligned(4))) = { };
75 #endif
77 #if defined(JCR) && defined(__GNUC__)
78 extern void _Jv_RegisterClasses(void *) __attribute__((weak));
80 static void *__JCR_LIST__[]
81 __attribute__((section(".jcr"))) = { };
82 #endif
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.
91 #ifdef SHARED
92 void *__dso_handle = &__dso_handle;
93 #else
94 void *__dso_handle = NULL;
95 #endif
96 __asm(".hidden __dso_handle");
98 #ifdef SHARED
99 extern void __cxa_finalize(void *) __attribute__((weak));
100 #endif
101 #endif
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); \
110 func(); \
111 __asm volatile (".previous"); \
113 #else
114 #error Need MD_CALL_STATIC_FUNCTION
115 #endif
116 #endif /* ! MD_CALL_STATIC_FUNCTION */
118 static void
119 __ctors(void)
121 unsigned long i = (unsigned long) __CTOR_LIST__[0];
122 void (**p)(void);
124 if (i == (unsigned long) -1) {
125 for (i = 1; __CTOR_LIST__[i] != NULL; i++)
127 i--;
129 p = __CTOR_LIST__ + i;
130 while (i--)
131 (**p--)();
134 static void
135 __dtors(void)
137 void (**p)(void) = __DTOR_LIST__ + 1;
139 while (*p)
140 (**p++)();
143 static void USED_NOINLINE
144 __do_global_ctors_aux(void)
146 static int initialized;
147 #ifdef DWARF2_EH
148 #if defined(__GNUC__)
149 static struct dwarf2_eh_object object;
150 #endif /* __GNUC__ */
151 #endif /* DWARF2_EH */
153 if (!initialized) {
154 initialized = 1;
156 #ifdef 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.
171 __ctors();
174 MD_CALL_STATIC_FUNCTION(.init, __do_global_ctors_aux)
176 static void USED_NOINLINE
177 __do_global_dtors_aux(void)
179 static int finished;
181 if (finished)
182 return;
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.
195 __dtors();
197 #ifdef DWARF2_EH
198 #if defined(__GNUC__)
199 if (__deregister_frame_info != NULL)
200 __deregister_frame_info(__EH_FRAME_BEGIN__);
201 #endif /* __GNUC__ */
202 #endif /* DWARF2_EH */
204 finished = 1;
206 MD_CALL_STATIC_FUNCTION(.fini, __do_global_dtors_aux)