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