Merge from vendor branch NTPD:
[dragonfly.git] / lib / csu / common / crtbegin.c
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 $ */
3
4 /*-
5  * Copyright (c) 1998, 2001, 2002 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Paul Kranenburg, Ross Harvey, and Jason R. Thorpe.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
26  *
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.
38  */
39
40 /*
41  * Run-time module which handles constructors and destructors,
42  * and NetBSD identification.
43  *
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
48  * null-terminated.
49  */
50
51 #include <sys/param.h>          /* sysident.h requires `NetBSD' constant */
52 #include <sys/exec.h>
53 #include <sys/elf_common.h>
54 #include <stdlib.h>
55
56 #if __GNUC_PREREQ__(3, 0)
57 #define USED_NOINLINE __attribute__((__used__,__noinline__))
58 #else
59 #define USED_NOINLINE __attribute__((__unused__))
60 #endif
61
62 #ifdef DWARF2_EH
63 #include "dwarf2_eh.h"
64 #endif
65 #include "dot_init.h"
66
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 */
71
72 #ifdef DWARF2_EH
73 static __EH_FRAME_CONST char __EH_FRAME_BEGIN__[]
74     __attribute__((section(".eh_frame"), aligned(4))) = { };
75 #endif
76
77 #if defined(JCR) && defined(__GNUC__)
78 extern void _Jv_RegisterClasses(void *) __attribute__((weak));
79
80 static void *__JCR_LIST__[]
81     __attribute__((section(".jcr"))) = { };
82 #endif
83
84 #if defined(DSO_HANDLE) && defined(__GNUC__)
85 /*
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.
90  */
91 #ifdef SHARED
92 void    *__dso_handle = &__dso_handle;
93 #else
94 void    *__dso_handle = NULL;
95 #endif
96 __asm(".hidden  __dso_handle");
97
98 #ifdef SHARED
99 extern void __cxa_finalize(void *) __attribute__((weak));
100 #endif
101 #endif
102
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)                                                     \
108 {                                                                       \
109         __asm __volatile (".section " #section);                        \
110         func();                                                         \
111         __asm __volatile (".previous");                                 \
112 }
113 #else
114 #error Need MD_CALL_STATIC_FUNCTION
115 #endif
116 #endif /* ! MD_CALL_STATIC_FUNCTION */
117
118 static void
119 __ctors(void)
120 {
121         unsigned long i = (unsigned long) __CTOR_LIST__[0];
122         void (**p)(void);
123
124         if (i == (unsigned long) -1)  {
125                 for (i = 1; __CTOR_LIST__[i] != NULL; i++)
126                         ;
127                 i--;
128         }
129         p = __CTOR_LIST__ + i;
130         while (i--)
131                 (**p--)();
132 }
133
134 static void
135 __dtors(void)
136 {
137         void (**p)(void) = __DTOR_LIST__ + 1;
138
139         while (*p)
140                 (**p++)();
141 }
142
143 static void USED_NOINLINE
144 __do_global_ctors_aux(void)
145 {
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 */
152
153         if (!initialized) {
154                 initialized = 1;
155
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 */
162
163 #if defined(JCR) && defined(__GNUC__)
164                 if (__JCR_LIST__[0] != NULL && _Jv_RegisterClasses != NULL)
165                         _Jv_RegisterClasses(__JCR_LIST__);
166 #endif /* JCR && __GNUC__ */
167
168                 /*
169                  * Call global constructors.
170                  */
171                 __ctors();
172         }
173 }
174 MD_CALL_STATIC_FUNCTION(.init, __do_global_ctors_aux)
175
176 static void USED_NOINLINE
177 __do_global_dtors_aux(void)
178 {
179         static int finished;
180
181         if (finished)
182                 return;
183
184 #if defined(DSO_HANDLE) && defined(__GNUC__) && defined(SHARED)
185         /*
186          * Call local destructors.
187          */
188         if (__cxa_finalize != NULL)
189                 __cxa_finalize(__dso_handle);
190 #endif /* DSO_HANDLE && __GNUC__ && SHARED */
191
192         /*
193          * Call global destructors.
194          */
195         __dtors();
196
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 */
203
204         finished = 1;
205 }
206 MD_CALL_STATIC_FUNCTION(.fini, __do_global_dtors_aux)