]>
Commit | Line | Data |
---|---|---|
dc17e9e9 RS |
1 | /* Specialized bits of code needed to support construction and |
2 | destruction of file-scope objects in C++ code. | |
7adcbafe | 3 | Copyright (C) 1991-2022 Free Software Foundation, Inc. |
d0f8fcea | 4 | Contributed by Ron Guilmette (rfg@monkeys.com). |
dc17e9e9 | 5 | |
1322177d | 6 | This file is part of GCC. |
dc17e9e9 | 7 | |
1322177d LB |
8 | GCC is free software; you can redistribute it and/or modify it under |
9 | the terms of the GNU General Public License as published by the Free | |
748086b7 | 10 | Software Foundation; either version 3, or (at your option) any later |
1322177d | 11 | version. |
dc17e9e9 | 12 | |
1322177d LB |
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
dc17e9e9 | 17 | |
748086b7 JJ |
18 | Under Section 7 of GPL version 3, you are granted additional |
19 | permissions described in the GCC Runtime Library Exception, version | |
20 | 3.1, as published by the Free Software Foundation. | |
21 | ||
22 | You should have received a copy of the GNU General Public License and | |
23 | a copy of the GCC Runtime Library Exception along with this program; | |
24 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
25 | <http://www.gnu.org/licenses/>. */ | |
dc17e9e9 | 26 | |
7857f134 | 27 | /* This file is a bit like libgcc2.c in that it is compiled |
dc17e9e9 RS |
28 | multiple times and yields multiple .o files. |
29 | ||
30 | This file is useful on target machines where the object file format | |
31 | supports multiple "user-defined" sections (e.g. COFF, ELF, ROSE). On | |
32 | such systems, this file allows us to avoid running collect (or any | |
33 | other such slow and painful kludge). Additionally, if the target | |
34 | system supports a .init section, this file allows us to support the | |
35 | linking of C++ code with a non-C++ main program. | |
36 | ||
37 | Note that if INIT_SECTION_ASM_OP is defined in the tm.h file, then | |
38 | this file *will* make use of the .init section. If that symbol is | |
39 | not defined however, then the .init section will not be used. | |
40 | ||
37ce5b86 CH |
41 | Currently, only ELF and COFF are supported. It is likely however that |
42 | ROSE could also be supported, if someone was willing to do the work to | |
43 | make whatever (small?) adaptations are needed. (Some work may be | |
44 | needed on the ROSE assembler and linker also.) | |
dc17e9e9 RS |
45 | |
46 | This file must be compiled with gcc. */ | |
47 | ||
56d6849d NS |
48 | /* Target machine header files require this define. */ |
49 | #define IN_LIBGCC2 | |
50 | ||
9d48ad93 NS |
51 | /* FIXME: Including auto-host is incorrect, but until we have |
52 | identified the set of defines that need to go into auto-target.h, | |
53 | this will have to do. */ | |
54 | #include "auto-host.h" | |
c6f9ea3b | 55 | #undef caddr_t |
3bea02d1 JM |
56 | #undef pid_t |
57 | #undef rlim_t | |
58 | #undef ssize_t | |
3bea02d1 | 59 | #undef vfork |
eaf4e618 | 60 | #include "tconfig.h" |
2e39bdbe | 61 | #include "tsystem.h" |
4977bab6 ZW |
62 | #include "coretypes.h" |
63 | #include "tm.h" | |
852b75ed | 64 | #include "libgcc_tm.h" |
52a11cbf | 65 | #include "unwind-dw2-fde.h" |
dc17e9e9 | 66 | |
cea3bd3e RH |
67 | #ifndef FORCE_CODE_SECTION_ALIGN |
68 | # define FORCE_CODE_SECTION_ALIGN | |
69 | #endif | |
70 | ||
036cfb36 | 71 | #ifndef CRT_CALL_STATIC_FUNCTION |
cea3bd3e RH |
72 | # define CRT_CALL_STATIC_FUNCTION(SECTION_OP, FUNC) \ |
73 | static void __attribute__((__used__)) \ | |
74 | call_ ## FUNC (void) \ | |
75 | { \ | |
76 | asm (SECTION_OP); \ | |
77 | FUNC (); \ | |
78 | FORCE_CODE_SECTION_ALIGN \ | |
53d68b9f | 79 | asm (__LIBGCC_TEXT_SECTION_ASM_OP__); \ |
cea3bd3e | 80 | } |
036cfb36 AO |
81 | #endif |
82 | ||
dbed5a9b | 83 | #if defined(TARGET_DL_ITERATE_PHDR) && \ |
ab955692 | 84 | (defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__)) |
dbed5a9b JM |
85 | #define BSD_DL_ITERATE_PHDR_AVAILABLE |
86 | #endif | |
87 | ||
2208d2ac LR |
88 | #if defined(OBJECT_FORMAT_ELF) \ |
89 | && !defined(OBJECT_FORMAT_FLAT) \ | |
90 | && defined(HAVE_LD_EH_FRAME_HDR) \ | |
91 | && !defined(inhibit_libc) && !defined(CRTSTUFFT_O) \ | |
dbed5a9b | 92 | && defined(BSD_DL_ITERATE_PHDR_AVAILABLE) |
2208d2ac LR |
93 | #include <link.h> |
94 | # define USE_PT_GNU_EH_FRAME | |
95 | #endif | |
96 | ||
6961669f RO |
97 | #if defined(OBJECT_FORMAT_ELF) \ |
98 | && !defined(OBJECT_FORMAT_FLAT) \ | |
99 | && defined(HAVE_LD_EH_FRAME_HDR) && defined(TARGET_DL_ITERATE_PHDR) \ | |
100 | && !defined(inhibit_libc) && !defined(CRTSTUFFT_O) \ | |
101 | && defined(__sun__) && defined(__svr4__) | |
102 | #include <link.h> | |
103 | # define USE_PT_GNU_EH_FRAME | |
104 | #endif | |
105 | ||
abe92a04 RS |
106 | #if defined(OBJECT_FORMAT_ELF) \ |
107 | && !defined(OBJECT_FORMAT_FLAT) \ | |
108 | && defined(HAVE_LD_EH_FRAME_HDR) \ | |
72c7c913 | 109 | && !defined(inhibit_libc) && !defined(CRTSTUFFT_O) \ |
275b60d6 JJ |
110 | && defined(__GLIBC__) && __GLIBC__ >= 2 |
111 | #include <link.h> | |
212db394 JZ |
112 | /* uClibc pretends to be glibc 2.2 and DT_CONFIG is defined in its link.h. |
113 | But it doesn't use PT_GNU_EH_FRAME ELF segment currently. */ | |
114 | # if !defined(__UCLIBC__) \ | |
115 | && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \ | |
275b60d6 JJ |
116 | || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG))) |
117 | # define USE_PT_GNU_EH_FRAME | |
118 | # endif | |
119 | #endif | |
d2468141 JM |
120 | |
121 | #if defined(OBJECT_FORMAT_ELF) \ | |
122 | && !defined(OBJECT_FORMAT_FLAT) \ | |
123 | && defined(HAVE_LD_EH_FRAME_HDR) \ | |
124 | && !defined(CRTSTUFFT_O) \ | |
125 | && defined(inhibit_libc) \ | |
126 | && (defined(__GLIBC__) || defined(__gnu_linux__) || defined(__GNU__)) | |
127 | /* On systems using glibc, an inhibit_libc build of libgcc is only | |
128 | part of a bootstrap process. Build the same crt*.o as would be | |
129 | built with headers present, so that it is not necessary to build | |
130 | glibc more than once for the bootstrap to converge. */ | |
131 | # define USE_PT_GNU_EH_FRAME | |
132 | #endif | |
133 | ||
598a1586 HPN |
134 | #ifdef USE_EH_FRAME_REGISTRY_ALWAYS |
135 | # ifndef __LIBGCC_EH_FRAME_SECTION_NAME__ | |
136 | # error "Can't use explicit exception-frame-registration without __LIBGCC_EH_FRAME_SECTION_NAME__" | |
137 | # endif | |
138 | #endif | |
139 | #if defined(__LIBGCC_EH_FRAME_SECTION_NAME__) && (!defined(USE_PT_GNU_EH_FRAME) || defined(USE_EH_FRAME_REGISTRY_ALWAYS)) | |
275b60d6 JJ |
140 | # define USE_EH_FRAME_REGISTRY |
141 | #endif | |
53d68b9f JM |
142 | #if defined(__LIBGCC_EH_FRAME_SECTION_NAME__) \ |
143 | && __LIBGCC_EH_TABLES_CAN_BE_READ_ONLY__ | |
96d0f4dc JJ |
144 | # define EH_FRAME_SECTION_CONST const |
145 | #else | |
146 | # define EH_FRAME_SECTION_CONST | |
147 | #endif | |
275b60d6 | 148 | |
3fc95619 UD |
149 | #if !defined(DTOR_LIST_END) && defined(OBJECT_FORMAT_ELF) \ |
150 | && defined(HAVE_GAS_HIDDEN) && !defined(FINI_ARRAY_SECTION_ASM_OP) | |
151 | # define HIDDEN_DTOR_LIST_END | |
152 | #endif | |
153 | ||
b0dc7e4c RH |
154 | #if !defined(USE_TM_CLONE_REGISTRY) && defined(OBJECT_FORMAT_ELF) |
155 | # define USE_TM_CLONE_REGISTRY 1 | |
62ec242e JL |
156 | #elif !defined(USE_TM_CLONE_REGISTRY) |
157 | # define USE_TM_CLONE_REGISTRY 0 | |
b0dc7e4c RH |
158 | #endif |
159 | ||
8f08ea1e | 160 | /* We do not want to add the weak attribute to the declarations of these |
52a11cbf RH |
161 | routines in unwind-dw2-fde.h because that will cause the definition of |
162 | these symbols to be weak as well. | |
8f08ea1e L |
163 | |
164 | This exposes a core issue, how to handle creating weak references vs | |
165 | how to create weak definitions. Either we have to have the definition | |
166 | of TARGET_WEAK_ATTRIBUTE be conditional in the shared header files or | |
167 | have a second declaration if we want a function's references to be weak, | |
168 | but not its definition. | |
169 | ||
170 | Making TARGET_WEAK_ATTRIBUTE conditional seems like a good solution until | |
454ff5cb | 171 | one thinks about scaling to larger problems -- i.e., the condition under |
8f08ea1e L |
172 | which TARGET_WEAK_ATTRIBUTE is active will eventually get far too |
173 | complicated. | |
174 | ||
175 | So, we take an approach similar to #pragma weak -- we have a second | |
176 | declaration for functions that we want to have weak references. | |
177 | ||
178 | Neither way is particularly good. */ | |
b8698a0f | 179 | |
8f08ea1e L |
180 | /* References to __register_frame_info and __deregister_frame_info should |
181 | be weak in this file if at all possible. */ | |
a30794da | 182 | extern void __register_frame_info (const void *, struct object *) |
8f08ea1e | 183 | TARGET_ATTRIBUTE_WEAK; |
a30794da | 184 | extern void __register_frame_info_bases (const void *, struct object *, |
1066e2b5 RH |
185 | void *, void *) |
186 | TARGET_ATTRIBUTE_WEAK; | |
a30794da | 187 | extern void *__deregister_frame_info (const void *) |
8f08ea1e | 188 | TARGET_ATTRIBUTE_WEAK; |
a30794da | 189 | extern void *__deregister_frame_info_bases (const void *) |
101fa48c | 190 | TARGET_ATTRIBUTE_WEAK; |
525996eb | 191 | extern void __do_global_ctors_1 (void); |
8f08ea1e | 192 | |
b0dc7e4c RH |
193 | /* Likewise for transactional memory clone tables. */ |
194 | extern void _ITM_registerTMCloneTable (void *, size_t) TARGET_ATTRIBUTE_WEAK; | |
195 | extern void _ITM_deregisterTMCloneTable (void *) TARGET_ATTRIBUTE_WEAK; | |
196 | ||
68d69835 JM |
197 | #ifdef OBJECT_FORMAT_ELF |
198 | ||
199 | /* Declare a pointer to void function type. */ | |
200 | typedef void (*func_ptr) (void); | |
201 | #define STATIC static | |
202 | ||
203 | #else /* OBJECT_FORMAT_ELF */ | |
204 | ||
dc17e9e9 RS |
205 | #include "gbl-ctors.h" |
206 | ||
68d69835 JM |
207 | #define STATIC |
208 | ||
209 | #endif /* OBJECT_FORMAT_ELF */ | |
dc17e9e9 RS |
210 | |
211 | #ifdef CRT_BEGIN | |
212 | ||
213f974a RH |
213 | /* NOTE: In order to be able to support SVR4 shared libraries, we arrange |
214 | to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__, | |
215 | __DTOR_END__ } per root executable and also one set of these symbols | |
216 | per shared library. So in any given whole process image, we may have | |
217 | multiple definitions of each of these symbols. In order to prevent | |
218 | these definitions from conflicting with one another, and in order to | |
219 | ensure that the proper lists are used for the initialization/finalization | |
220 | of each individual shared library (respectively), we give these symbols | |
221 | only internal (i.e. `static') linkage, and we also make it a point to | |
222 | refer to only the __CTOR_END__ symbol in crtend.o and the __DTOR_LIST__ | |
223 | symbol in crtbegin.o, where they are defined. */ | |
224 | ||
9ce7ad7f L |
225 | /* No need for .ctors/.dtors section if linker can place them in |
226 | .init_array/.fini_array section. */ | |
227 | #ifndef USE_INITFINI_ARRAY | |
213f974a RH |
228 | /* The -1 is a flag to __do_global_[cd]tors indicating that this table |
229 | does not start with a count of elements. */ | |
230 | #ifdef CTOR_LIST_BEGIN | |
231 | CTOR_LIST_BEGIN; | |
53d68b9f | 232 | #elif defined(__LIBGCC_CTORS_SECTION_ASM_OP__) |
213f974a RH |
233 | /* Hack: force cc1 to switch to .data section early, so that assembling |
234 | __CTOR_LIST__ does not undo our behind-the-back change to .ctors. */ | |
e4fa83d3 | 235 | static func_ptr force_to_data[1] __attribute__ ((__used__)) = { }; |
53d68b9f | 236 | asm (__LIBGCC_CTORS_SECTION_ASM_OP__); |
213f974a | 237 | STATIC func_ptr __CTOR_LIST__[1] |
d16017da | 238 | __attribute__ ((__used__, aligned(__alignof__(func_ptr)))) |
213f974a RH |
239 | = { (func_ptr) (-1) }; |
240 | #else | |
241 | STATIC func_ptr __CTOR_LIST__[1] | |
d16017da | 242 | __attribute__ ((__used__, section(".ctors"), aligned(__alignof__(func_ptr)))) |
213f974a RH |
243 | = { (func_ptr) (-1) }; |
244 | #endif /* __CTOR_LIST__ alternatives */ | |
245 | ||
246 | #ifdef DTOR_LIST_BEGIN | |
247 | DTOR_LIST_BEGIN; | |
53d68b9f JM |
248 | #elif defined(__LIBGCC_DTORS_SECTION_ASM_OP__) |
249 | asm (__LIBGCC_DTORS_SECTION_ASM_OP__); | |
213f974a | 250 | STATIC func_ptr __DTOR_LIST__[1] |
d16017da | 251 | __attribute__ ((aligned(__alignof__(func_ptr)))) |
213f974a RH |
252 | = { (func_ptr) (-1) }; |
253 | #else | |
254 | STATIC func_ptr __DTOR_LIST__[1] | |
d16017da | 255 | __attribute__((section(".dtors"), aligned(__alignof__(func_ptr)))) |
213f974a RH |
256 | = { (func_ptr) (-1) }; |
257 | #endif /* __DTOR_LIST__ alternatives */ | |
9ce7ad7f | 258 | #endif /* USE_INITFINI_ARRAY */ |
213f974a | 259 | |
618939de | 260 | #ifdef USE_EH_FRAME_REGISTRY |
213f974a RH |
261 | /* Stick a label at the beginning of the frame unwind info so we can register |
262 | and deregister it with the exception handling library code. */ | |
96d0f4dc | 263 | STATIC EH_FRAME_SECTION_CONST char __EH_FRAME_BEGIN__[] |
53d68b9f | 264 | __attribute__((section(__LIBGCC_EH_FRAME_SECTION_NAME__), aligned(4))) |
213f974a | 265 | = { }; |
618939de | 266 | #endif /* USE_EH_FRAME_REGISTRY */ |
213f974a | 267 | |
b0dc7e4c RH |
268 | #if USE_TM_CLONE_REGISTRY |
269 | STATIC func_ptr __TMC_LIST__[] | |
d16017da | 270 | __attribute__((used, section(".tm_clone_table"), aligned(__alignof__(void*)))) |
b0dc7e4c | 271 | = { }; |
7546aa9d | 272 | # ifdef HAVE_GAS_HIDDEN |
b0dc7e4c | 273 | extern func_ptr __TMC_END__[] __attribute__((__visibility__ ("hidden"))); |
7546aa9d RH |
274 | # endif |
275 | ||
276 | static inline void | |
277 | deregister_tm_clones (void) | |
278 | { | |
279 | void (*fn) (void *); | |
280 | ||
281 | #ifdef HAVE_GAS_HIDDEN | |
62a7df9f MG |
282 | func_ptr *end = __TMC_END__; |
283 | // Do not optimize the comparison to false. | |
284 | __asm ("" : "+g" (end)); | |
285 | if (__TMC_LIST__ == end) | |
7546aa9d RH |
286 | return; |
287 | #else | |
288 | if (__TMC_LIST__[0] == NULL) | |
289 | return; | |
290 | #endif | |
291 | ||
292 | fn = _ITM_deregisterTMCloneTable; | |
293 | __asm ("" : "+r" (fn)); | |
294 | if (fn) | |
295 | fn (__TMC_LIST__); | |
296 | } | |
297 | ||
298 | static inline void | |
299 | register_tm_clones (void) | |
300 | { | |
301 | void (*fn) (void *, size_t); | |
302 | size_t size; | |
303 | ||
304 | #ifdef HAVE_GAS_HIDDEN | |
62a7df9f MG |
305 | func_ptr *end = __TMC_END__; |
306 | // Do not optimize the comparison to false. | |
307 | __asm ("" : "+g" (end)); | |
308 | size = (end - __TMC_LIST__) / 2; | |
7546aa9d RH |
309 | #else |
310 | for (size = 0; __TMC_LIST__[size * 2] != NULL; size++) | |
311 | continue; | |
312 | #endif | |
313 | if (size == 0) | |
314 | return; | |
315 | ||
316 | fn = _ITM_registerTMCloneTable; | |
317 | __asm ("" : "+r" (fn)); | |
318 | if (fn) | |
319 | fn (__TMC_LIST__, size); | |
320 | } | |
b0dc7e4c RH |
321 | #endif /* USE_TM_CLONE_REGISTRY */ |
322 | ||
53d68b9f JM |
323 | #if defined(__LIBGCC_INIT_SECTION_ASM_OP__) \ |
324 | || defined(__LIBGCC_INIT_ARRAY_SECTION_ASM_OP__) | |
29d2c7a5 | 325 | |
68d69835 JM |
326 | #ifdef OBJECT_FORMAT_ELF |
327 | ||
34a53788 | 328 | #if DEFAULT_USE_CXA_ATEXIT |
fc693822 | 329 | /* Declare the __dso_handle variable. It should have a unique value |
6a9c5260 UD |
330 | in every shared-object; in a main program its value is zero. The |
331 | object should in any case be protected. This means the instance | |
332 | in one DSO or the main program is not used in another object. The | |
34a53788 JL |
333 | dynamic linker takes care of this. |
334 | If __cxa_atexit is not being used, __dso_handle will not be used and | |
335 | doesn't need to be defined. */ | |
6a9c5260 | 336 | |
a0cfeb0f DD |
337 | #ifdef TARGET_LIBGCC_SDATA_SECTION |
338 | extern void *__dso_handle __attribute__ ((__section__ (TARGET_LIBGCC_SDATA_SECTION))); | |
339 | #endif | |
6a9c5260 | 340 | #ifdef HAVE_GAS_HIDDEN |
47bd70b5 | 341 | extern void *__dso_handle __attribute__ ((__visibility__ ("hidden"))); |
6a9c5260 | 342 | #endif |
fc693822 MM |
343 | #ifdef CRTSTUFFS_O |
344 | void *__dso_handle = &__dso_handle; | |
345 | #else | |
346 | void *__dso_handle = 0; | |
347 | #endif | |
34a53788 | 348 | #endif /* DEFAULT_USE_CXA_ATEXIT */ |
fc693822 MM |
349 | |
350 | /* The __cxa_finalize function may not be available so we use only a | |
351 | weak declaration. */ | |
352 | extern void __cxa_finalize (void *) TARGET_ATTRIBUTE_WEAK; | |
353 | ||
68d69835 | 354 | /* Run all the global destructors on exit from the program. */ |
b8698a0f | 355 | |
68d69835 JM |
356 | /* Some systems place the number of pointers in the first word of the |
357 | table. On SVR4 however, that word is -1. In all cases, the table is | |
358 | null-terminated. On SVR4, we start from the beginning of the list and | |
359 | invoke each per-compilation-unit destructor routine in order | |
360 | until we find that null. | |
361 | ||
362 | Note that this function MUST be static. There will be one of these | |
363 | functions in each root executable and one in each shared library, but | |
364 | although they all have the same code, each one is unique in that it | |
365 | refers to one particular associated `__DTOR_LIST__' which belongs to the | |
b40b9d93 MS |
366 | same particular root executable or shared library file. |
367 | ||
368 | On some systems, this routine is run more than once from the .fini, | |
369 | when exit is called recursively, so we arrange to remember where in | |
370 | the list we left off processing, and we resume at that point, | |
ad8b7783 | 371 | should we be re-invoked. |
68d69835 | 372 | |
ad8b7783 JL |
373 | This routine does not need to be run if none of the following clauses are |
374 | true, as it will not do anything, so can be removed. */ | |
375 | #if defined(CRTSTUFFS_O) || !defined(FINI_ARRAY_SECTION_ASM_OP) \ | |
21a098f4 | 376 | || USE_TM_CLONE_REGISTRY || defined(USE_EH_FRAME_REGISTRY) |
cea3bd3e | 377 | static void __attribute__((used)) |
3cc22c31 | 378 | __do_global_dtors_aux (void) |
68d69835 | 379 | { |
7abc66b1 | 380 | static _Bool completed; |
5041a61c | 381 | |
1066e2b5 | 382 | if (__builtin_expect (completed, 0)) |
5041a61c JL |
383 | return; |
384 | ||
84479e3a | 385 | #if DEFAULT_USE_CXA_ATEXIT |
2a14e214 MM |
386 | #ifdef CRTSTUFFS_O |
387 | if (__cxa_finalize) | |
fc693822 | 388 | __cxa_finalize (__dso_handle); |
2a14e214 | 389 | #endif |
84479e3a | 390 | #endif |
fc693822 | 391 | |
7abc66b1 JB |
392 | #ifdef FINI_ARRAY_SECTION_ASM_OP |
393 | /* If we are using .fini_array then destructors will be run via that | |
394 | mechanism. */ | |
3fc95619 UD |
395 | #elif defined(HIDDEN_DTOR_LIST_END) |
396 | { | |
397 | /* Safer version that makes sure only .dtors function pointers are | |
398 | called even if the static variable is maliciously changed. */ | |
399 | extern func_ptr __DTOR_END__[] __attribute__((visibility ("hidden"))); | |
400 | static size_t dtor_idx; | |
401 | const size_t max_idx = __DTOR_END__ - __DTOR_LIST__ - 1; | |
2405e0ea | 402 | func_ptr *dtor_list; |
3fc95619 | 403 | |
2405e0ea | 404 | __asm ("" : "=g" (dtor_list) : "0" (__DTOR_LIST__)); |
3fc95619 | 405 | while (dtor_idx < max_idx) |
2405e0ea | 406 | dtor_list[++dtor_idx] (); |
3fc95619 | 407 | } |
7abc66b1 | 408 | #else /* !defined (FINI_ARRAY_SECTION_ASM_OP) */ |
3fc95619 UD |
409 | { |
410 | static func_ptr *p = __DTOR_LIST__ + 1; | |
411 | func_ptr f; | |
412 | ||
413 | while ((f = *p)) | |
414 | { | |
415 | p++; | |
416 | f (); | |
417 | } | |
418 | } | |
7abc66b1 | 419 | #endif /* !defined(FINI_ARRAY_SECTION_ASM_OP) */ |
0021b564 | 420 | |
b0dc7e4c | 421 | #if USE_TM_CLONE_REGISTRY |
7546aa9d | 422 | deregister_tm_clones (); |
b0dc7e4c RH |
423 | #endif /* USE_TM_CLONE_REGISTRY */ |
424 | ||
275b60d6 | 425 | #ifdef USE_EH_FRAME_REGISTRY |
f691dc3b | 426 | #ifdef CRT_GET_RFIB_DATA |
101fa48c RH |
427 | /* If we used the new __register_frame_info_bases interface, |
428 | make sure that we deregister from the same place. */ | |
429 | if (__deregister_frame_info_bases) | |
430 | __deregister_frame_info_bases (__EH_FRAME_BEGIN__); | |
431 | #else | |
8f08ea1e L |
432 | if (__deregister_frame_info) |
433 | __deregister_frame_info (__EH_FRAME_BEGIN__); | |
0021b564 | 434 | #endif |
101fa48c RH |
435 | #endif |
436 | ||
5041a61c | 437 | completed = 1; |
68d69835 JM |
438 | } |
439 | ||
440 | /* Stick a call to __do_global_dtors_aux into the .fini section. */ | |
7abc66b1 | 441 | #ifdef FINI_SECTION_ASM_OP |
cea3bd3e | 442 | CRT_CALL_STATIC_FUNCTION (FINI_SECTION_ASM_OP, __do_global_dtors_aux) |
6d83cb32 | 443 | #elif defined (FINI_ARRAY_SECTION_ASM_OP) |
11189793 CL |
444 | #if defined(__FDPIC__) |
445 | __asm__("\t.equ\t__do_global_dtors_aux_alias, __do_global_dtors_aux\n"); | |
446 | extern char __do_global_dtors_aux_alias; | |
447 | static void *__do_global_dtors_aux_fini_array_entry[] | |
448 | __attribute__ ((__used__, section(".fini_array"), aligned(sizeof(void *)))) | |
449 | = { &__do_global_dtors_aux_alias }; | |
450 | #else /* defined(__FDPIC__) */ | |
7abc66b1 | 451 | static func_ptr __do_global_dtors_aux_fini_array_entry[] |
d16017da JL |
452 | __attribute__ ((__used__, section(".fini_array"), |
453 | aligned(__alignof__(func_ptr)))) = { __do_global_dtors_aux }; | |
11189793 | 454 | #endif /* defined(__FDPIC__) */ |
6d83cb32 DR |
455 | #else /* !FINI_SECTION_ASM_OP && !FINI_ARRAY_SECTION_ASM_OP */ |
456 | static void __attribute__((used)) | |
457 | __do_global_dtors_aux_1 (void) | |
458 | { | |
459 | atexit (__do_global_dtors_aux); | |
460 | } | |
53d68b9f JM |
461 | CRT_CALL_STATIC_FUNCTION (__LIBGCC_INIT_SECTION_ASM_OP__, |
462 | __do_global_dtors_aux_1) | |
6d83cb32 | 463 | #endif |
ad8b7783 | 464 | #endif /* defined(CRTSTUFFS_O) || !defined(FINI_ARRAY_SECTION_ASM_OP) |
21a098f4 | 465 | || USE_TM_CLONE_REGISTRY || defined(USE_EH_FRAME_REGISTRY) */ |
68d69835 | 466 | |
62ec242e | 467 | #if defined(USE_EH_FRAME_REGISTRY) || USE_TM_CLONE_REGISTRY |
6d8ccdbb JL |
468 | /* Stick a call to __register_frame_info into the .init section. For some |
469 | reason calls with no arguments work more reliably in .init, so stick the | |
470 | call in another function. */ | |
0021b564 | 471 | |
cea3bd3e | 472 | static void __attribute__((used)) |
3cc22c31 | 473 | frame_dummy (void) |
0021b564 | 474 | { |
275b60d6 | 475 | #ifdef USE_EH_FRAME_REGISTRY |
956d6950 | 476 | static struct object object; |
f691dc3b | 477 | #ifdef CRT_GET_RFIB_DATA |
1066e2b5 | 478 | void *tbase, *dbase; |
1066e2b5 | 479 | tbase = 0; |
1066e2b5 | 480 | CRT_GET_RFIB_DATA (dbase); |
1066e2b5 RH |
481 | if (__register_frame_info_bases) |
482 | __register_frame_info_bases (__EH_FRAME_BEGIN__, &object, tbase, dbase); | |
483 | #else | |
8f08ea1e L |
484 | if (__register_frame_info) |
485 | __register_frame_info (__EH_FRAME_BEGIN__, &object); | |
f691dc3b | 486 | #endif /* CRT_GET_RFIB_DATA */ |
4505024e | 487 | #endif /* USE_EH_FRAME_REGISTRY */ |
b0dc7e4c | 488 | |
b0dc7e4c | 489 | #if USE_TM_CLONE_REGISTRY |
7546aa9d | 490 | register_tm_clones (); |
b0dc7e4c | 491 | #endif /* USE_TM_CLONE_REGISTRY */ |
0021b564 JM |
492 | } |
493 | ||
53d68b9f JM |
494 | #ifdef __LIBGCC_INIT_SECTION_ASM_OP__ |
495 | CRT_CALL_STATIC_FUNCTION (__LIBGCC_INIT_SECTION_ASM_OP__, frame_dummy) | |
496 | #else /* defined(__LIBGCC_INIT_SECTION_ASM_OP__) */ | |
11189793 CL |
497 | #if defined(__FDPIC__) |
498 | __asm__("\t.equ\t__frame_dummy_alias, frame_dummy\n"); | |
499 | extern char __frame_dummy_alias; | |
500 | static void *__frame_dummy_init_array_entry[] | |
501 | __attribute__ ((__used__, section(".init_array"), aligned(sizeof(void *)))) | |
502 | = { &__frame_dummy_alias }; | |
503 | #else /* defined(__FDPIC__) */ | |
7abc66b1 | 504 | static func_ptr __frame_dummy_init_array_entry[] |
d16017da JL |
505 | __attribute__ ((__used__, section(".init_array"), |
506 | aligned(__alignof__(func_ptr)))) = { frame_dummy }; | |
11189793 | 507 | #endif /* defined(__FDPIC__) */ |
53d68b9f | 508 | #endif /* !defined(__LIBGCC_INIT_SECTION_ASM_OP__) */ |
5a79befb | 509 | #endif /* USE_EH_FRAME_REGISTRY || USE_TM_CLONE_REGISTRY */ |
0021b564 | 510 | |
68d69835 JM |
511 | #else /* OBJECT_FORMAT_ELF */ |
512 | ||
b335c2cc | 513 | /* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o |
b71a4aa9 | 514 | and once in crtend.o). It must be declared static to avoid a link |
b335c2cc TW |
515 | error. Here, we define __do_global_ctors as an externally callable |
516 | function. It is externally callable so that __main can invoke it when | |
517 | INVOKE__main is defined. This has the additional effect of forcing cc1 | |
518 | to switch to the .text section. */ | |
0f41302f | 519 | |
d1e51320 | 520 | static void __do_global_ctors_aux (void); |
3cc22c31 JL |
521 | void |
522 | __do_global_ctors (void) | |
329d2160 | 523 | { |
29d2c7a5 RH |
524 | #ifdef INVOKE__main |
525 | /* If __main won't actually call __do_global_ctors then it doesn't matter | |
526 | what's inside the function. The inside of __do_global_ctors_aux is | |
527 | called automatically in that case. And the Alliant fx2800 linker | |
528 | crashes on this reference. So prevent the crash. */ | |
329d2160 RS |
529 | __do_global_ctors_aux (); |
530 | #endif | |
531 | } | |
dc17e9e9 | 532 | |
53d68b9f | 533 | asm (__LIBGCC_INIT_SECTION_ASM_OP__); /* cc1 doesn't know that we are switching! */ |
dc17e9e9 RS |
534 | |
535 | /* A routine to invoke all of the global constructors upon entry to the | |
536 | program. We put this into the .init section (for systems that have | |
537 | such a thing) so that we can properly perform the construction of | |
6d2f8887 | 538 | file-scope static-storage C++ objects within shared libraries. */ |
dc17e9e9 | 539 | |
cea3bd3e | 540 | static void __attribute__((used)) |
3cc22c31 | 541 | __do_global_ctors_aux (void) /* prologue goes in .init section */ |
dc17e9e9 | 542 | { |
cea3bd3e | 543 | FORCE_CODE_SECTION_ALIGN /* explicit align before switch to .text */ |
53d68b9f | 544 | asm (__LIBGCC_TEXT_SECTION_ASM_OP__); /* don't put epilogue and body in .init */ |
dc17e9e9 | 545 | DO_GLOBAL_CTORS_BODY; |
451fbdf2 | 546 | atexit (__do_global_dtors); |
dc17e9e9 RS |
547 | } |
548 | ||
68d69835 | 549 | #endif /* OBJECT_FORMAT_ELF */ |
fe1fd353 | 550 | |
53d68b9f | 551 | #elif defined(HAS_INIT_SECTION) /* ! __LIBGCC_INIT_SECTION_ASM_OP__ */ |
fe1fd353 | 552 | |
af8e5a0e RO |
553 | extern void __do_global_dtors (void); |
554 | ||
fe1fd353 JM |
555 | /* This case is used by the Irix 6 port, which supports named sections but |
556 | not an SVR4-style .fini section. __do_global_dtors can be non-static | |
0021b564 JM |
557 | in this case because we protect it with -hidden_symbol. */ |
558 | ||
fe1fd353 | 559 | void |
3cc22c31 | 560 | __do_global_dtors (void) |
fe1fd353 | 561 | { |
1066e2b5 RH |
562 | func_ptr *p, f; |
563 | for (p = __DTOR_LIST__ + 1; (f = *p); p++) | |
564 | f (); | |
0021b564 | 565 | |
b0dc7e4c | 566 | #if USE_TM_CLONE_REGISTRY |
7546aa9d | 567 | deregister_tm_clones (); |
b0dc7e4c RH |
568 | #endif /* USE_TM_CLONE_REGISTRY */ |
569 | ||
275b60d6 | 570 | #ifdef USE_EH_FRAME_REGISTRY |
8f08ea1e L |
571 | if (__deregister_frame_info) |
572 | __deregister_frame_info (__EH_FRAME_BEGIN__); | |
0021b564 | 573 | #endif |
fe1fd353 | 574 | } |
35a42f5f | 575 | |
62ec242e | 576 | #if defined(USE_EH_FRAME_REGISTRY) || USE_TM_CLONE_REGISTRY |
29d2c7a5 RH |
577 | /* A helper function for __do_global_ctors, which is in crtend.o. Here |
578 | in crtbegin.o, we can reference a couple of symbols not visible there. | |
579 | Plus, since we're before libgcc.a, we have no problems referencing | |
580 | functions from there. */ | |
35a42f5f | 581 | void |
29d2c7a5 | 582 | __do_global_ctors_1(void) |
35a42f5f | 583 | { |
275b60d6 | 584 | #ifdef USE_EH_FRAME_REGISTRY |
35a42f5f | 585 | static struct object object; |
8f08ea1e L |
586 | if (__register_frame_info) |
587 | __register_frame_info (__EH_FRAME_BEGIN__, &object); | |
b335c2cc | 588 | #endif |
b0dc7e4c | 589 | |
b0dc7e4c | 590 | #if USE_TM_CLONE_REGISTRY |
7546aa9d | 591 | register_tm_clones (); |
b0dc7e4c | 592 | #endif /* USE_TM_CLONE_REGISTRY */ |
213f974a | 593 | } |
5a79befb | 594 | #endif /* USE_EH_FRAME_REGISTRY || USE_TM_CLONE_REGISTRY */ |
6351543d | 595 | |
53d68b9f | 596 | #else /* ! __LIBGCC_INIT_SECTION_ASM_OP__ && ! HAS_INIT_SECTION */ |
29d2c7a5 RH |
597 | #error "What are you doing with crtstuff.c, then?" |
598 | #endif | |
599 | ||
600 | #elif defined(CRT_END) /* ! CRT_BEGIN */ | |
601 | ||
9ce7ad7f L |
602 | /* No need for .ctors/.dtors section if linker can place them in |
603 | .init_array/.fini_array section. */ | |
604 | #ifndef USE_INITFINI_ARRAY | |
29d2c7a5 RH |
605 | /* Put a word containing zero at the end of each of our two lists of function |
606 | addresses. Note that the words defined here go into the .ctors and .dtors | |
607 | sections of the crtend.o file, and since that file is always linked in | |
608 | last, these words naturally end up at the very ends of the two lists | |
609 | contained in these two sections. */ | |
610 | ||
611 | #ifdef CTOR_LIST_END | |
612 | CTOR_LIST_END; | |
53d68b9f | 613 | #elif defined(__LIBGCC_CTORS_SECTION_ASM_OP__) |
29d2c7a5 RH |
614 | /* Hack: force cc1 to switch to .data section early, so that assembling |
615 | __CTOR_LIST__ does not undo our behind-the-back change to .ctors. */ | |
e4fa83d3 | 616 | static func_ptr force_to_data[1] __attribute__ ((__used__)) = { }; |
53d68b9f | 617 | asm (__LIBGCC_CTORS_SECTION_ASM_OP__); |
29d2c7a5 | 618 | STATIC func_ptr __CTOR_END__[1] |
d16017da | 619 | __attribute__((aligned(__alignof__(func_ptr)))) |
29d2c7a5 RH |
620 | = { (func_ptr) 0 }; |
621 | #else | |
622 | STATIC func_ptr __CTOR_END__[1] | |
d16017da | 623 | __attribute__((section(".ctors"), aligned(__alignof__(func_ptr)))) |
29d2c7a5 RH |
624 | = { (func_ptr) 0 }; |
625 | #endif | |
626 | ||
627 | #ifdef DTOR_LIST_END | |
628 | DTOR_LIST_END; | |
3fc95619 | 629 | #elif defined(HIDDEN_DTOR_LIST_END) |
53d68b9f JM |
630 | #ifdef __LIBGCC_DTORS_SECTION_ASM_OP__ |
631 | asm (__LIBGCC_DTORS_SECTION_ASM_OP__); | |
3fc95619 UD |
632 | #endif |
633 | func_ptr __DTOR_END__[1] | |
b8053194 | 634 | __attribute__ ((used, |
53d68b9f | 635 | #ifndef __LIBGCC_DTORS_SECTION_ASM_OP__ |
3fc95619 UD |
636 | section(".dtors"), |
637 | #endif | |
d16017da | 638 | aligned(__alignof__(func_ptr)), visibility ("hidden"))) |
3fc95619 | 639 | = { (func_ptr) 0 }; |
53d68b9f JM |
640 | #elif defined(__LIBGCC_DTORS_SECTION_ASM_OP__) |
641 | asm (__LIBGCC_DTORS_SECTION_ASM_OP__); | |
29d2c7a5 | 642 | STATIC func_ptr __DTOR_END__[1] |
d16017da | 643 | __attribute__ ((used, aligned(__alignof__(func_ptr)))) |
29d2c7a5 RH |
644 | = { (func_ptr) 0 }; |
645 | #else | |
646 | STATIC func_ptr __DTOR_END__[1] | |
d16017da | 647 | __attribute__((used, section(".dtors"), aligned(__alignof__(func_ptr)))) |
29d2c7a5 RH |
648 | = { (func_ptr) 0 }; |
649 | #endif | |
9ce7ad7f | 650 | #endif /* USE_INITFINI_ARRAY */ |
dc17e9e9 | 651 | |
53d68b9f | 652 | #ifdef __LIBGCC_EH_FRAME_SECTION_NAME__ |
29d2c7a5 RH |
653 | /* Terminate the frame unwind info section with a 4byte 0 as a sentinel; |
654 | this would be the 'length' field in a real FDE. */ | |
61f03aba RH |
655 | # if __INT_MAX__ == 2147483647 |
656 | typedef int int32; | |
657 | # elif __LONG_MAX__ == 2147483647 | |
658 | typedef long int32; | |
659 | # elif __SHRT_MAX__ == 2147483647 | |
660 | typedef short int32; | |
661 | # else | |
662 | # error "Missing a 4 byte integer" | |
663 | # endif | |
664 | STATIC EH_FRAME_SECTION_CONST int32 __FRAME_END__[] | |
53d68b9f | 665 | __attribute__ ((used, section(__LIBGCC_EH_FRAME_SECTION_NAME__), |
d16017da | 666 | aligned(__alignof__(int32)))) |
29d2c7a5 | 667 | = { 0 }; |
53d68b9f | 668 | #endif /* __LIBGCC_EH_FRAME_SECTION_NAME__ */ |
29d2c7a5 | 669 | |
b0dc7e4c | 670 | #if USE_TM_CLONE_REGISTRY |
7546aa9d RH |
671 | # ifndef HAVE_GAS_HIDDEN |
672 | static | |
673 | # endif | |
b0dc7e4c | 674 | func_ptr __TMC_END__[] |
d16017da JL |
675 | __attribute__((used, section(".tm_clone_table"), |
676 | aligned(__alignof__(void *)))) | |
7546aa9d RH |
677 | # ifdef HAVE_GAS_HIDDEN |
678 | __attribute__((__visibility__ ("hidden"))) = { }; | |
679 | # else | |
680 | = { 0, 0 }; | |
681 | # endif | |
b0dc7e4c RH |
682 | #endif /* USE_TM_CLONE_REGISTRY */ |
683 | ||
53d68b9f | 684 | #ifdef __LIBGCC_INIT_ARRAY_SECTION_ASM_OP__ |
7abc66b1 JB |
685 | |
686 | /* If we are using .init_array, there is nothing to do. */ | |
687 | ||
53d68b9f | 688 | #elif defined(__LIBGCC_INIT_SECTION_ASM_OP__) |
dc17e9e9 | 689 | |
68d69835 | 690 | #ifdef OBJECT_FORMAT_ELF |
cea3bd3e | 691 | static void __attribute__((used)) |
3cc22c31 | 692 | __do_global_ctors_aux (void) |
68d69835 JM |
693 | { |
694 | func_ptr *p; | |
695 | for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--) | |
696 | (*p) (); | |
697 | } | |
698 | ||
699 | /* Stick a call to __do_global_ctors_aux into the .init section. */ | |
53d68b9f | 700 | CRT_CALL_STATIC_FUNCTION (__LIBGCC_INIT_SECTION_ASM_OP__, __do_global_ctors_aux) |
68d69835 JM |
701 | #else /* OBJECT_FORMAT_ELF */ |
702 | ||
703 | /* Stick the real initialization code, followed by a normal sort of | |
704 | function epilogue at the very end of the .init section for this | |
705 | entire root executable file or for this entire shared library file. | |
706 | ||
707 | Note that we use some tricks here to get *just* the body and just | |
708 | a function epilogue (but no function prologue) into the .init | |
9faa82d8 | 709 | section of the crtend.o file. Specifically, we switch to the .text |
68d69835 JM |
710 | section, start to define a function, and then we switch to the .init |
711 | section just before the body code. | |
712 | ||
713 | Earlier on, we put the corresponding function prologue into the .init | |
714 | section of the crtbegin.o file (which will be linked in first). | |
715 | ||
716 | Note that we want to invoke all constructors for C++ file-scope static- | |
717 | storage objects AFTER any other possible initialization actions which | |
718 | may be performed by the code in the .init section contributions made by | |
719 | other libraries, etc. That's because those other initializations may | |
720 | include setup operations for very primitive things (e.g. initializing | |
721 | the state of the floating-point coprocessor, etc.) which should be done | |
0f41302f | 722 | before we start to execute any of the user's code. */ |
dc17e9e9 RS |
723 | |
724 | static void | |
3cc22c31 | 725 | __do_global_ctors_aux (void) /* prologue goes in .text section */ |
dc17e9e9 | 726 | { |
53d68b9f | 727 | asm (__LIBGCC_INIT_SECTION_ASM_OP__); |
dc17e9e9 | 728 | DO_GLOBAL_CTORS_BODY; |
451fbdf2 | 729 | atexit (__do_global_dtors); |
e5e809f4 JL |
730 | } /* epilogue and body go in .init section */ |
731 | ||
cea3bd3e | 732 | FORCE_CODE_SECTION_ALIGN |
53d68b9f | 733 | asm (__LIBGCC_TEXT_SECTION_ASM_OP__); |
dc17e9e9 | 734 | |
68d69835 JM |
735 | #endif /* OBJECT_FORMAT_ELF */ |
736 | ||
53d68b9f | 737 | #elif defined(HAS_INIT_SECTION) /* ! __LIBGCC_INIT_SECTION_ASM_OP__ */ |
fe1fd353 | 738 | |
af8e5a0e RO |
739 | extern void __do_global_ctors (void); |
740 | ||
fe1fd353 JM |
741 | /* This case is used by the Irix 6 port, which supports named sections but |
742 | not an SVR4-style .init section. __do_global_ctors can be non-static | |
0021b564 | 743 | in this case because we protect it with -hidden_symbol. */ |
fe1fd353 | 744 | void |
3cc22c31 | 745 | __do_global_ctors (void) |
fe1fd353 JM |
746 | { |
747 | func_ptr *p; | |
62ec242e | 748 | #if defined(USE_EH_FRAME_REGISTRY) || USE_TM_CLONE_REGISTRY |
29d2c7a5 | 749 | __do_global_ctors_1(); |
0021b564 | 750 | #endif |
fe1fd353 JM |
751 | for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--) |
752 | (*p) (); | |
753 | } | |
68d69835 | 754 | |
53d68b9f | 755 | #else /* ! __LIBGCC_INIT_SECTION_ASM_OP__ && ! HAS_INIT_SECTION */ |
29d2c7a5 | 756 | #error "What are you doing with crtstuff.c, then?" |
b335c2cc | 757 | #endif |
dc17e9e9 | 758 | |
29d2c7a5 RH |
759 | #else /* ! CRT_BEGIN && ! CRT_END */ |
760 | #error "One of CRT_BEGIN or CRT_END must be defined." | |
b335c2cc | 761 | #endif |