]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgcc/crtstuff.c
* testsuite/ChangeLog: Fix last ChangeLog entry.
[thirdparty/gcc.git] / libgcc / crtstuff.c
1 /* Specialized bits of code needed to support construction and
2 destruction of file-scope objects in C++ code.
3 Copyright (C) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
4 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011
5 Free Software Foundation, Inc.
6 Contributed by Ron Guilmette (rfg@monkeys.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
19
20 Under Section 7 of GPL version 3, you are granted additional
21 permissions described in the GCC Runtime Library Exception, version
22 3.1, as published by the Free Software Foundation.
23
24 You should have received a copy of the GNU General Public License and
25 a copy of the GCC Runtime Library Exception along with this program;
26 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
27 <http://www.gnu.org/licenses/>. */
28
29 /* This file is a bit like libgcc2.c in that it is compiled
30 multiple times and yields multiple .o files.
31
32 This file is useful on target machines where the object file format
33 supports multiple "user-defined" sections (e.g. COFF, ELF, ROSE). On
34 such systems, this file allows us to avoid running collect (or any
35 other such slow and painful kludge). Additionally, if the target
36 system supports a .init section, this file allows us to support the
37 linking of C++ code with a non-C++ main program.
38
39 Note that if INIT_SECTION_ASM_OP is defined in the tm.h file, then
40 this file *will* make use of the .init section. If that symbol is
41 not defined however, then the .init section will not be used.
42
43 Currently, only ELF and COFF are supported. It is likely however that
44 ROSE could also be supported, if someone was willing to do the work to
45 make whatever (small?) adaptations are needed. (Some work may be
46 needed on the ROSE assembler and linker also.)
47
48 This file must be compiled with gcc. */
49
50 /* Target machine header files require this define. */
51 #define IN_LIBGCC2
52
53 /* FIXME: Including auto-host is incorrect, but until we have
54 identified the set of defines that need to go into auto-target.h,
55 this will have to do. */
56 #include "auto-host.h"
57 #undef pid_t
58 #undef rlim_t
59 #undef ssize_t
60 #undef vfork
61 #include "tconfig.h"
62 #include "tsystem.h"
63 #include "coretypes.h"
64 #include "tm.h"
65 #include "unwind-dw2-fde.h"
66
67 #ifndef FORCE_CODE_SECTION_ALIGN
68 # define FORCE_CODE_SECTION_ALIGN
69 #endif
70
71 #ifndef CRT_CALL_STATIC_FUNCTION
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 \
79 asm (TEXT_SECTION_ASM_OP); \
80 }
81 #endif
82
83 #if defined(OBJECT_FORMAT_ELF) \
84 && !defined(OBJECT_FORMAT_FLAT) \
85 && defined(HAVE_LD_EH_FRAME_HDR) \
86 && !defined(inhibit_libc) && !defined(CRTSTUFFT_O) \
87 && defined(__FreeBSD__) && __FreeBSD__ >= 7
88 #include <link.h>
89 # define USE_PT_GNU_EH_FRAME
90 #endif
91
92 #if defined(OBJECT_FORMAT_ELF) \
93 && !defined(OBJECT_FORMAT_FLAT) \
94 && defined(HAVE_LD_EH_FRAME_HDR) && defined(TARGET_DL_ITERATE_PHDR) \
95 && !defined(inhibit_libc) && !defined(CRTSTUFFT_O) \
96 && defined(__sun__) && defined(__svr4__)
97 #include <link.h>
98 # define USE_PT_GNU_EH_FRAME
99 #endif
100
101 #if defined(OBJECT_FORMAT_ELF) \
102 && !defined(OBJECT_FORMAT_FLAT) \
103 && defined(HAVE_LD_EH_FRAME_HDR) \
104 && !defined(inhibit_libc) && !defined(CRTSTUFFT_O) \
105 && defined(__GLIBC__) && __GLIBC__ >= 2
106 #include <link.h>
107 /* uClibc pretends to be glibc 2.2 and DT_CONFIG is defined in its link.h.
108 But it doesn't use PT_GNU_EH_FRAME ELF segment currently. */
109 # if !defined(__UCLIBC__) \
110 && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
111 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
112 # define USE_PT_GNU_EH_FRAME
113 # endif
114 #endif
115 #if defined(EH_FRAME_SECTION_NAME) && !defined(USE_PT_GNU_EH_FRAME)
116 # define USE_EH_FRAME_REGISTRY
117 #endif
118 #if defined(EH_FRAME_SECTION_NAME) && EH_TABLES_CAN_BE_READ_ONLY
119 # define EH_FRAME_SECTION_CONST const
120 #else
121 # define EH_FRAME_SECTION_CONST
122 #endif
123
124 #if !defined(DTOR_LIST_END) && defined(OBJECT_FORMAT_ELF) \
125 && defined(HAVE_GAS_HIDDEN) && !defined(FINI_ARRAY_SECTION_ASM_OP)
126 # define HIDDEN_DTOR_LIST_END
127 #endif
128
129 /* We do not want to add the weak attribute to the declarations of these
130 routines in unwind-dw2-fde.h because that will cause the definition of
131 these symbols to be weak as well.
132
133 This exposes a core issue, how to handle creating weak references vs
134 how to create weak definitions. Either we have to have the definition
135 of TARGET_WEAK_ATTRIBUTE be conditional in the shared header files or
136 have a second declaration if we want a function's references to be weak,
137 but not its definition.
138
139 Making TARGET_WEAK_ATTRIBUTE conditional seems like a good solution until
140 one thinks about scaling to larger problems -- i.e., the condition under
141 which TARGET_WEAK_ATTRIBUTE is active will eventually get far too
142 complicated.
143
144 So, we take an approach similar to #pragma weak -- we have a second
145 declaration for functions that we want to have weak references.
146
147 Neither way is particularly good. */
148
149 /* References to __register_frame_info and __deregister_frame_info should
150 be weak in this file if at all possible. */
151 extern void __register_frame_info (const void *, struct object *)
152 TARGET_ATTRIBUTE_WEAK;
153 extern void __register_frame_info_bases (const void *, struct object *,
154 void *, void *)
155 TARGET_ATTRIBUTE_WEAK;
156 extern void *__deregister_frame_info (const void *)
157 TARGET_ATTRIBUTE_WEAK;
158 extern void *__deregister_frame_info_bases (const void *)
159 TARGET_ATTRIBUTE_WEAK;
160 extern void __do_global_ctors_1 (void);
161
162 /* Likewise for _Jv_RegisterClasses. */
163 extern void _Jv_RegisterClasses (void *) TARGET_ATTRIBUTE_WEAK;
164
165 #ifdef OBJECT_FORMAT_ELF
166
167 /* Declare a pointer to void function type. */
168 typedef void (*func_ptr) (void);
169 #define STATIC static
170
171 #else /* OBJECT_FORMAT_ELF */
172
173 #include "gbl-ctors.h"
174
175 #define STATIC
176
177 #endif /* OBJECT_FORMAT_ELF */
178
179 #ifdef CRT_BEGIN
180
181 /* NOTE: In order to be able to support SVR4 shared libraries, we arrange
182 to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
183 __DTOR_END__ } per root executable and also one set of these symbols
184 per shared library. So in any given whole process image, we may have
185 multiple definitions of each of these symbols. In order to prevent
186 these definitions from conflicting with one another, and in order to
187 ensure that the proper lists are used for the initialization/finalization
188 of each individual shared library (respectively), we give these symbols
189 only internal (i.e. `static') linkage, and we also make it a point to
190 refer to only the __CTOR_END__ symbol in crtend.o and the __DTOR_LIST__
191 symbol in crtbegin.o, where they are defined. */
192
193 /* No need for .ctors/.dtors section if linker can place them in
194 .init_array/.fini_array section. */
195 #ifndef USE_INITFINI_ARRAY
196 /* The -1 is a flag to __do_global_[cd]tors indicating that this table
197 does not start with a count of elements. */
198 #ifdef CTOR_LIST_BEGIN
199 CTOR_LIST_BEGIN;
200 #elif defined(CTORS_SECTION_ASM_OP)
201 /* Hack: force cc1 to switch to .data section early, so that assembling
202 __CTOR_LIST__ does not undo our behind-the-back change to .ctors. */
203 static func_ptr force_to_data[1] __attribute__ ((__used__)) = { };
204 asm (CTORS_SECTION_ASM_OP);
205 STATIC func_ptr __CTOR_LIST__[1]
206 __attribute__ ((__used__, aligned(sizeof(func_ptr))))
207 = { (func_ptr) (-1) };
208 #else
209 STATIC func_ptr __CTOR_LIST__[1]
210 __attribute__ ((__used__, section(".ctors"), aligned(sizeof(func_ptr))))
211 = { (func_ptr) (-1) };
212 #endif /* __CTOR_LIST__ alternatives */
213
214 #ifdef DTOR_LIST_BEGIN
215 DTOR_LIST_BEGIN;
216 #elif defined(DTORS_SECTION_ASM_OP)
217 asm (DTORS_SECTION_ASM_OP);
218 STATIC func_ptr __DTOR_LIST__[1]
219 __attribute__ ((aligned(sizeof(func_ptr))))
220 = { (func_ptr) (-1) };
221 #else
222 STATIC func_ptr __DTOR_LIST__[1]
223 __attribute__((section(".dtors"), aligned(sizeof(func_ptr))))
224 = { (func_ptr) (-1) };
225 #endif /* __DTOR_LIST__ alternatives */
226 #endif /* USE_INITFINI_ARRAY */
227
228 #ifdef USE_EH_FRAME_REGISTRY
229 /* Stick a label at the beginning of the frame unwind info so we can register
230 and deregister it with the exception handling library code. */
231 STATIC EH_FRAME_SECTION_CONST char __EH_FRAME_BEGIN__[]
232 __attribute__((section(EH_FRAME_SECTION_NAME), aligned(4)))
233 = { };
234 #endif /* USE_EH_FRAME_REGISTRY */
235
236 #ifdef JCR_SECTION_NAME
237 /* Stick a label at the beginning of the java class registration info
238 so we can register them properly. */
239 STATIC void *__JCR_LIST__[]
240 __attribute__ ((used, section(JCR_SECTION_NAME), aligned(sizeof(void*))))
241 = { };
242 #endif /* JCR_SECTION_NAME */
243
244 #if defined(INIT_SECTION_ASM_OP) || defined(INIT_ARRAY_SECTION_ASM_OP)
245
246 #ifdef OBJECT_FORMAT_ELF
247
248 /* Declare the __dso_handle variable. It should have a unique value
249 in every shared-object; in a main program its value is zero. The
250 object should in any case be protected. This means the instance
251 in one DSO or the main program is not used in another object. The
252 dynamic linker takes care of this. */
253
254 #ifdef TARGET_LIBGCC_SDATA_SECTION
255 extern void *__dso_handle __attribute__ ((__section__ (TARGET_LIBGCC_SDATA_SECTION)));
256 #endif
257 #ifdef HAVE_GAS_HIDDEN
258 extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
259 #endif
260 #ifdef CRTSTUFFS_O
261 void *__dso_handle = &__dso_handle;
262 #else
263 void *__dso_handle = 0;
264 #endif
265
266 /* The __cxa_finalize function may not be available so we use only a
267 weak declaration. */
268 extern void __cxa_finalize (void *) TARGET_ATTRIBUTE_WEAK;
269
270 /* Run all the global destructors on exit from the program. */
271
272 /* Some systems place the number of pointers in the first word of the
273 table. On SVR4 however, that word is -1. In all cases, the table is
274 null-terminated. On SVR4, we start from the beginning of the list and
275 invoke each per-compilation-unit destructor routine in order
276 until we find that null.
277
278 Note that this function MUST be static. There will be one of these
279 functions in each root executable and one in each shared library, but
280 although they all have the same code, each one is unique in that it
281 refers to one particular associated `__DTOR_LIST__' which belongs to the
282 same particular root executable or shared library file.
283
284 On some systems, this routine is run more than once from the .fini,
285 when exit is called recursively, so we arrange to remember where in
286 the list we left off processing, and we resume at that point,
287 should we be re-invoked. */
288
289 static void __attribute__((used))
290 __do_global_dtors_aux (void)
291 {
292 static _Bool completed;
293
294 if (__builtin_expect (completed, 0))
295 return;
296
297 #ifdef CRTSTUFFS_O
298 if (__cxa_finalize)
299 __cxa_finalize (__dso_handle);
300 #endif
301
302 #ifdef FINI_ARRAY_SECTION_ASM_OP
303 /* If we are using .fini_array then destructors will be run via that
304 mechanism. */
305 #elif defined(HIDDEN_DTOR_LIST_END)
306 {
307 /* Safer version that makes sure only .dtors function pointers are
308 called even if the static variable is maliciously changed. */
309 extern func_ptr __DTOR_END__[] __attribute__((visibility ("hidden")));
310 static size_t dtor_idx;
311 const size_t max_idx = __DTOR_END__ - __DTOR_LIST__ - 1;
312 func_ptr f;
313
314 while (dtor_idx < max_idx)
315 {
316 f = __DTOR_LIST__[++dtor_idx];
317 f ();
318 }
319 }
320 #else /* !defined (FINI_ARRAY_SECTION_ASM_OP) */
321 {
322 static func_ptr *p = __DTOR_LIST__ + 1;
323 func_ptr f;
324
325 while ((f = *p))
326 {
327 p++;
328 f ();
329 }
330 }
331 #endif /* !defined(FINI_ARRAY_SECTION_ASM_OP) */
332
333 #ifdef USE_EH_FRAME_REGISTRY
334 #ifdef CRT_GET_RFIB_DATA
335 /* If we used the new __register_frame_info_bases interface,
336 make sure that we deregister from the same place. */
337 if (__deregister_frame_info_bases)
338 __deregister_frame_info_bases (__EH_FRAME_BEGIN__);
339 #else
340 if (__deregister_frame_info)
341 __deregister_frame_info (__EH_FRAME_BEGIN__);
342 #endif
343 #endif
344
345 completed = 1;
346 }
347
348 /* Stick a call to __do_global_dtors_aux into the .fini section. */
349 #ifdef FINI_SECTION_ASM_OP
350 CRT_CALL_STATIC_FUNCTION (FINI_SECTION_ASM_OP, __do_global_dtors_aux)
351 #elif defined (FINI_ARRAY_SECTION_ASM_OP)
352 static func_ptr __do_global_dtors_aux_fini_array_entry[]
353 __attribute__ ((__used__, section(".fini_array")))
354 = { __do_global_dtors_aux };
355 #else /* !FINI_SECTION_ASM_OP && !FINI_ARRAY_SECTION_ASM_OP */
356 static void __attribute__((used))
357 __do_global_dtors_aux_1 (void)
358 {
359 atexit (__do_global_dtors_aux);
360 }
361 CRT_CALL_STATIC_FUNCTION (INIT_SECTION_ASM_OP, __do_global_dtors_aux_1)
362 #endif
363
364 #if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME)
365 /* Stick a call to __register_frame_info into the .init section. For some
366 reason calls with no arguments work more reliably in .init, so stick the
367 call in another function. */
368
369 static void __attribute__((used))
370 frame_dummy (void)
371 {
372 #ifdef USE_EH_FRAME_REGISTRY
373 static struct object object;
374 #ifdef CRT_GET_RFIB_DATA
375 void *tbase, *dbase;
376 tbase = 0;
377 CRT_GET_RFIB_DATA (dbase);
378 if (__register_frame_info_bases)
379 __register_frame_info_bases (__EH_FRAME_BEGIN__, &object, tbase, dbase);
380 #else
381 if (__register_frame_info)
382 __register_frame_info (__EH_FRAME_BEGIN__, &object);
383 #endif /* CRT_GET_RFIB_DATA */
384 #endif /* USE_EH_FRAME_REGISTRY */
385 #ifdef JCR_SECTION_NAME
386 if (__JCR_LIST__[0])
387 {
388 void (*register_classes) (void *) = _Jv_RegisterClasses;
389 __asm ("" : "+r" (register_classes));
390 if (register_classes)
391 register_classes (__JCR_LIST__);
392 }
393 #endif /* JCR_SECTION_NAME */
394 }
395
396 #ifdef INIT_SECTION_ASM_OP
397 CRT_CALL_STATIC_FUNCTION (INIT_SECTION_ASM_OP, frame_dummy)
398 #else /* defined(INIT_SECTION_ASM_OP) */
399 static func_ptr __frame_dummy_init_array_entry[]
400 __attribute__ ((__used__, section(".init_array")))
401 = { frame_dummy };
402 #endif /* !defined(INIT_SECTION_ASM_OP) */
403 #endif /* USE_EH_FRAME_REGISTRY || JCR_SECTION_NAME */
404
405 #else /* OBJECT_FORMAT_ELF */
406
407 /* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o
408 and once in crtend.o). It must be declared static to avoid a link
409 error. Here, we define __do_global_ctors as an externally callable
410 function. It is externally callable so that __main can invoke it when
411 INVOKE__main is defined. This has the additional effect of forcing cc1
412 to switch to the .text section. */
413
414 static void __do_global_ctors_aux (void);
415 void
416 __do_global_ctors (void)
417 {
418 #ifdef INVOKE__main
419 /* If __main won't actually call __do_global_ctors then it doesn't matter
420 what's inside the function. The inside of __do_global_ctors_aux is
421 called automatically in that case. And the Alliant fx2800 linker
422 crashes on this reference. So prevent the crash. */
423 __do_global_ctors_aux ();
424 #endif
425 }
426
427 asm (INIT_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
428
429 /* A routine to invoke all of the global constructors upon entry to the
430 program. We put this into the .init section (for systems that have
431 such a thing) so that we can properly perform the construction of
432 file-scope static-storage C++ objects within shared libraries. */
433
434 static void __attribute__((used))
435 __do_global_ctors_aux (void) /* prologue goes in .init section */
436 {
437 FORCE_CODE_SECTION_ALIGN /* explicit align before switch to .text */
438 asm (TEXT_SECTION_ASM_OP); /* don't put epilogue and body in .init */
439 DO_GLOBAL_CTORS_BODY;
440 atexit (__do_global_dtors);
441 }
442
443 #endif /* OBJECT_FORMAT_ELF */
444
445 #elif defined(HAS_INIT_SECTION) /* ! INIT_SECTION_ASM_OP */
446
447 extern void __do_global_dtors (void);
448
449 /* This case is used by the Irix 6 port, which supports named sections but
450 not an SVR4-style .fini section. __do_global_dtors can be non-static
451 in this case because we protect it with -hidden_symbol. */
452
453 void
454 __do_global_dtors (void)
455 {
456 func_ptr *p, f;
457 for (p = __DTOR_LIST__ + 1; (f = *p); p++)
458 f ();
459
460 #ifdef USE_EH_FRAME_REGISTRY
461 if (__deregister_frame_info)
462 __deregister_frame_info (__EH_FRAME_BEGIN__);
463 #endif
464 }
465
466 #if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME)
467 /* A helper function for __do_global_ctors, which is in crtend.o. Here
468 in crtbegin.o, we can reference a couple of symbols not visible there.
469 Plus, since we're before libgcc.a, we have no problems referencing
470 functions from there. */
471 void
472 __do_global_ctors_1(void)
473 {
474 #ifdef USE_EH_FRAME_REGISTRY
475 static struct object object;
476 if (__register_frame_info)
477 __register_frame_info (__EH_FRAME_BEGIN__, &object);
478 #endif
479 #ifdef JCR_SECTION_NAME
480 if (__JCR_LIST__[0])
481 {
482 void (*register_classes) (void *) = _Jv_RegisterClasses;
483 __asm ("" : "+r" (register_classes));
484 if (register_classes)
485 register_classes (__JCR_LIST__);
486 }
487 #endif
488 }
489 #endif /* USE_EH_FRAME_REGISTRY || JCR_SECTION_NAME */
490
491 #else /* ! INIT_SECTION_ASM_OP && ! HAS_INIT_SECTION */
492 #error "What are you doing with crtstuff.c, then?"
493 #endif
494
495 #elif defined(CRT_END) /* ! CRT_BEGIN */
496
497 /* No need for .ctors/.dtors section if linker can place them in
498 .init_array/.fini_array section. */
499 #ifndef USE_INITFINI_ARRAY
500 /* Put a word containing zero at the end of each of our two lists of function
501 addresses. Note that the words defined here go into the .ctors and .dtors
502 sections of the crtend.o file, and since that file is always linked in
503 last, these words naturally end up at the very ends of the two lists
504 contained in these two sections. */
505
506 #ifdef CTOR_LIST_END
507 CTOR_LIST_END;
508 #elif defined(CTORS_SECTION_ASM_OP)
509 /* Hack: force cc1 to switch to .data section early, so that assembling
510 __CTOR_LIST__ does not undo our behind-the-back change to .ctors. */
511 static func_ptr force_to_data[1] __attribute__ ((__used__)) = { };
512 asm (CTORS_SECTION_ASM_OP);
513 STATIC func_ptr __CTOR_END__[1]
514 __attribute__((aligned(sizeof(func_ptr))))
515 = { (func_ptr) 0 };
516 #else
517 STATIC func_ptr __CTOR_END__[1]
518 __attribute__((section(".ctors"), aligned(sizeof(func_ptr))))
519 = { (func_ptr) 0 };
520 #endif
521
522 #ifdef DTOR_LIST_END
523 DTOR_LIST_END;
524 #elif defined(HIDDEN_DTOR_LIST_END)
525 #ifdef DTORS_SECTION_ASM_OP
526 asm (DTORS_SECTION_ASM_OP);
527 #endif
528 func_ptr __DTOR_END__[1]
529 __attribute__ ((used,
530 #ifndef DTORS_SECTION_ASM_OP
531 section(".dtors"),
532 #endif
533 aligned(sizeof(func_ptr)), visibility ("hidden")))
534 = { (func_ptr) 0 };
535 #elif defined(DTORS_SECTION_ASM_OP)
536 asm (DTORS_SECTION_ASM_OP);
537 STATIC func_ptr __DTOR_END__[1]
538 __attribute__ ((used, aligned(sizeof(func_ptr))))
539 = { (func_ptr) 0 };
540 #else
541 STATIC func_ptr __DTOR_END__[1]
542 __attribute__((used, section(".dtors"), aligned(sizeof(func_ptr))))
543 = { (func_ptr) 0 };
544 #endif
545 #endif /* USE_INITFINI_ARRAY */
546
547 #ifdef EH_FRAME_SECTION_NAME
548 /* Terminate the frame unwind info section with a 4byte 0 as a sentinel;
549 this would be the 'length' field in a real FDE. */
550 # if __INT_MAX__ == 2147483647
551 typedef int int32;
552 # elif __LONG_MAX__ == 2147483647
553 typedef long int32;
554 # elif __SHRT_MAX__ == 2147483647
555 typedef short int32;
556 # else
557 # error "Missing a 4 byte integer"
558 # endif
559 STATIC EH_FRAME_SECTION_CONST int32 __FRAME_END__[]
560 __attribute__ ((used, section(EH_FRAME_SECTION_NAME),
561 aligned(sizeof(int32))))
562 = { 0 };
563 #endif /* EH_FRAME_SECTION_NAME */
564
565 #ifdef JCR_SECTION_NAME
566 /* Null terminate the .jcr section array. */
567 STATIC void *__JCR_END__[1]
568 __attribute__ ((used, section(JCR_SECTION_NAME),
569 aligned(sizeof(void *))))
570 = { 0 };
571 #endif /* JCR_SECTION_NAME */
572
573 #ifdef INIT_ARRAY_SECTION_ASM_OP
574
575 /* If we are using .init_array, there is nothing to do. */
576
577 #elif defined(INIT_SECTION_ASM_OP)
578
579 #ifdef OBJECT_FORMAT_ELF
580 static void __attribute__((used))
581 __do_global_ctors_aux (void)
582 {
583 func_ptr *p;
584 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
585 (*p) ();
586 }
587
588 /* Stick a call to __do_global_ctors_aux into the .init section. */
589 CRT_CALL_STATIC_FUNCTION (INIT_SECTION_ASM_OP, __do_global_ctors_aux)
590 #else /* OBJECT_FORMAT_ELF */
591
592 /* Stick the real initialization code, followed by a normal sort of
593 function epilogue at the very end of the .init section for this
594 entire root executable file or for this entire shared library file.
595
596 Note that we use some tricks here to get *just* the body and just
597 a function epilogue (but no function prologue) into the .init
598 section of the crtend.o file. Specifically, we switch to the .text
599 section, start to define a function, and then we switch to the .init
600 section just before the body code.
601
602 Earlier on, we put the corresponding function prologue into the .init
603 section of the crtbegin.o file (which will be linked in first).
604
605 Note that we want to invoke all constructors for C++ file-scope static-
606 storage objects AFTER any other possible initialization actions which
607 may be performed by the code in the .init section contributions made by
608 other libraries, etc. That's because those other initializations may
609 include setup operations for very primitive things (e.g. initializing
610 the state of the floating-point coprocessor, etc.) which should be done
611 before we start to execute any of the user's code. */
612
613 static void
614 __do_global_ctors_aux (void) /* prologue goes in .text section */
615 {
616 asm (INIT_SECTION_ASM_OP);
617 DO_GLOBAL_CTORS_BODY;
618 atexit (__do_global_dtors);
619 } /* epilogue and body go in .init section */
620
621 FORCE_CODE_SECTION_ALIGN
622 asm (TEXT_SECTION_ASM_OP);
623
624 #endif /* OBJECT_FORMAT_ELF */
625
626 #elif defined(HAS_INIT_SECTION) /* ! INIT_SECTION_ASM_OP */
627
628 extern void __do_global_ctors (void);
629
630 /* This case is used by the Irix 6 port, which supports named sections but
631 not an SVR4-style .init section. __do_global_ctors can be non-static
632 in this case because we protect it with -hidden_symbol. */
633 void
634 __do_global_ctors (void)
635 {
636 func_ptr *p;
637 #if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME)
638 __do_global_ctors_1();
639 #endif
640 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
641 (*p) ();
642 }
643
644 #else /* ! INIT_SECTION_ASM_OP && ! HAS_INIT_SECTION */
645 #error "What are you doing with crtstuff.c, then?"
646 #endif
647
648 #else /* ! CRT_BEGIN && ! CRT_END */
649 #error "One of CRT_BEGIN or CRT_END must be defined."
650 #endif