OSDN Git Service

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