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