OSDN Git Service

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