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