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