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