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