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