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