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