crtstuff.c revision 117395
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 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, 59 Temple Place - Suite 330, Boston, MA
3102111-1307, 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/* It is incorrect to include config.h here, because this file is being
55   compiled for the target, and hence definitions concerning only the host
56   do not apply.  */
57
58/* We include auto-host.h here to get HAVE_GAS_HIDDEN.  This is
59   supposedly valid even though this is a "target" file.  */
60#include "auto-host.h"
61#include "tconfig.h"
62#include "tsystem.h"
63#include "unwind-dw2-fde.h"
64
65#ifndef FORCE_CODE_SECTION_ALIGN
66# define FORCE_CODE_SECTION_ALIGN
67#endif
68
69#ifndef CRT_CALL_STATIC_FUNCTION
70# define CRT_CALL_STATIC_FUNCTION(SECTION_OP, FUNC)	\
71static void __attribute__((__used__))			\
72call_ ## FUNC (void)					\
73{							\
74  asm (SECTION_OP);					\
75  FUNC ();						\
76  FORCE_CODE_SECTION_ALIGN				\
77  asm (TEXT_SECTION_ASM_OP);				\
78}
79#endif
80
81#if defined(OBJECT_FORMAT_ELF) && defined(HAVE_LD_EH_FRAME_HDR) \
82    && !defined(inhibit_libc) && !defined(CRTSTUFFT_O) \
83    && defined(__GLIBC__) && __GLIBC__ >= 2
84#include <link.h>
85# if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
86     || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
87#  define USE_PT_GNU_EH_FRAME
88# endif
89#endif
90#if defined(EH_FRAME_SECTION_NAME) && !defined(USE_PT_GNU_EH_FRAME)
91# define USE_EH_FRAME_REGISTRY
92#endif
93#if defined(EH_FRAME_SECTION_NAME) && defined(HAVE_LD_RO_RW_SECTION_MIXING)
94# define EH_FRAME_SECTION_CONST const
95#else
96# define EH_FRAME_SECTION_CONST
97#endif
98
99/* We do not want to add the weak attribute to the declarations of these
100   routines in unwind-dw2-fde.h because that will cause the definition of
101   these symbols to be weak as well.
102
103   This exposes a core issue, how to handle creating weak references vs
104   how to create weak definitions.  Either we have to have the definition
105   of TARGET_WEAK_ATTRIBUTE be conditional in the shared header files or
106   have a second declaration if we want a function's references to be weak,
107   but not its definition.
108
109   Making TARGET_WEAK_ATTRIBUTE conditional seems like a good solution until
110   one thinks about scaling to larger problems -- ie, the condition under
111   which TARGET_WEAK_ATTRIBUTE is active will eventually get far too
112   complicated.
113
114   So, we take an approach similar to #pragma weak -- we have a second
115   declaration for functions that we want to have weak references.
116
117   Neither way is particularly good.  */
118
119/* References to __register_frame_info and __deregister_frame_info should
120   be weak in this file if at all possible.  */
121extern void __register_frame_info (void *, struct object *)
122				  TARGET_ATTRIBUTE_WEAK;
123extern void __register_frame_info_bases (void *, struct object *,
124					 void *, void *)
125				  TARGET_ATTRIBUTE_WEAK;
126extern void *__deregister_frame_info (void *)
127				     TARGET_ATTRIBUTE_WEAK;
128extern void *__deregister_frame_info_bases (void *)
129				     TARGET_ATTRIBUTE_WEAK;
130
131/* Likewise for _Jv_RegisterClasses.  */
132extern void _Jv_RegisterClasses (void *) TARGET_ATTRIBUTE_WEAK;
133
134#ifdef OBJECT_FORMAT_ELF
135
136/*  Declare a pointer to void function type.  */
137typedef void (*func_ptr) (void);
138#define STATIC static
139
140#else  /* OBJECT_FORMAT_ELF */
141
142#include "gbl-ctors.h"
143
144#define STATIC
145
146#endif /* OBJECT_FORMAT_ELF */
147
148#ifdef CRT_BEGIN
149
150/* NOTE:  In order to be able to support SVR4 shared libraries, we arrange
151   to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
152   __DTOR_END__ } per root executable and also one set of these symbols
153   per shared library.  So in any given whole process image, we may have
154   multiple definitions of each of these symbols.  In order to prevent
155   these definitions from conflicting with one another, and in order to
156   ensure that the proper lists are used for the initialization/finalization
157   of each individual shared library (respectively), we give these symbols
158   only internal (i.e. `static') linkage, and we also make it a point to
159   refer to only the __CTOR_END__ symbol in crtend.o and the __DTOR_LIST__
160   symbol in crtbegin.o, where they are defined.  */
161
162/* The -1 is a flag to __do_global_[cd]tors indicating that this table
163   does not start with a count of elements.  */
164#ifdef CTOR_LIST_BEGIN
165CTOR_LIST_BEGIN;
166#elif defined(CTORS_SECTION_ASM_OP)
167/* Hack: force cc1 to switch to .data section early, so that assembling
168   __CTOR_LIST__ does not undo our behind-the-back change to .ctors.  */
169static func_ptr force_to_data[1] __attribute__ ((__unused__)) = { };
170asm (CTORS_SECTION_ASM_OP);
171STATIC func_ptr __CTOR_LIST__[1]
172  __attribute__ ((__unused__, aligned(sizeof(func_ptr))))
173  = { (func_ptr) (-1) };
174#else
175STATIC func_ptr __CTOR_LIST__[1]
176  __attribute__ ((__unused__, section(".ctors"), aligned(sizeof(func_ptr))))
177  = { (func_ptr) (-1) };
178#endif /* __CTOR_LIST__ alternatives */
179
180#ifdef DTOR_LIST_BEGIN
181DTOR_LIST_BEGIN;
182#elif defined(DTORS_SECTION_ASM_OP)
183asm (DTORS_SECTION_ASM_OP);
184STATIC func_ptr __DTOR_LIST__[1]
185  __attribute__ ((aligned(sizeof(func_ptr))))
186  = { (func_ptr) (-1) };
187#else
188STATIC func_ptr __DTOR_LIST__[1]
189  __attribute__((section(".dtors"), aligned(sizeof(func_ptr))))
190  = { (func_ptr) (-1) };
191#endif /* __DTOR_LIST__ alternatives */
192
193#ifdef USE_EH_FRAME_REGISTRY
194/* Stick a label at the beginning of the frame unwind info so we can register
195   and deregister it with the exception handling library code.  */
196STATIC EH_FRAME_SECTION_CONST char __EH_FRAME_BEGIN__[]
197     __attribute__((section(EH_FRAME_SECTION_NAME), aligned(4)))
198     = { };
199#endif /* USE_EH_FRAME_REGISTRY */
200
201#ifdef JCR_SECTION_NAME
202/* Stick a label at the beginning of the java class registration info
203   so we can register them properly.  */
204STATIC void *__JCR_LIST__[]
205  __attribute__ ((unused, section(JCR_SECTION_NAME), aligned(sizeof(void*))))
206  = { };
207#endif /* JCR_SECTION_NAME */
208
209#ifdef INIT_SECTION_ASM_OP
210
211#ifdef OBJECT_FORMAT_ELF
212
213/* Declare the __dso_handle variable.  It should have a unique value
214   in every shared-object; in a main program its value is zero.  The
215   object should in any case be protected.  This means the instance
216   in one DSO or the main program is not used in another object.  The
217   dynamic linker takes care of this.  */
218
219#ifdef HAVE_GAS_HIDDEN
220extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
221#endif
222#ifdef CRTSTUFFS_O
223void *__dso_handle = &__dso_handle;
224#else
225void *__dso_handle = 0;
226#endif
227
228/* The __cxa_finalize function may not be available so we use only a
229   weak declaration.  */
230extern void __cxa_finalize (void *) TARGET_ATTRIBUTE_WEAK;
231
232/* Run all the global destructors on exit from the program.  */
233
234/* Some systems place the number of pointers in the first word of the
235   table.  On SVR4 however, that word is -1.  In all cases, the table is
236   null-terminated.  On SVR4, we start from the beginning of the list and
237   invoke each per-compilation-unit destructor routine in order
238   until we find that null.
239
240   Note that this function MUST be static.  There will be one of these
241   functions in each root executable and one in each shared library, but
242   although they all have the same code, each one is unique in that it
243   refers to one particular associated `__DTOR_LIST__' which belongs to the
244   same particular root executable or shared library file.
245
246   On some systems, this routine is run more than once from the .fini,
247   when exit is called recursively, so we arrange to remember where in
248   the list we left off processing, and we resume at that point,
249   should we be re-invoked.  */
250
251static void __attribute__((used))
252__do_global_dtors_aux (void)
253{
254  static func_ptr *p = __DTOR_LIST__ + 1;
255  static _Bool completed;
256  func_ptr f;
257
258  if (__builtin_expect (completed, 0))
259    return;
260
261#ifdef CRTSTUFFS_O
262  if (__cxa_finalize)
263    __cxa_finalize (__dso_handle);
264#endif
265
266  while ((f = *p))
267    {
268      p++;
269      f ();
270    }
271
272#ifdef USE_EH_FRAME_REGISTRY
273#if defined(CRT_GET_RFIB_TEXT) || defined(CRT_GET_RFIB_DATA)
274  /* If we used the new __register_frame_info_bases interface,
275     make sure that we deregister from the same place.  */
276  if (__deregister_frame_info_bases)
277    __deregister_frame_info_bases (__EH_FRAME_BEGIN__);
278#else
279  if (__deregister_frame_info)
280    __deregister_frame_info (__EH_FRAME_BEGIN__);
281#endif
282#endif
283
284  completed = 1;
285}
286
287/* Stick a call to __do_global_dtors_aux into the .fini section.  */
288CRT_CALL_STATIC_FUNCTION (FINI_SECTION_ASM_OP, __do_global_dtors_aux)
289
290#if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME)
291/* Stick a call to __register_frame_info into the .init section.  For some
292   reason calls with no arguments work more reliably in .init, so stick the
293   call in another function.  */
294
295static void __attribute__((used))
296frame_dummy (void)
297{
298#ifdef USE_EH_FRAME_REGISTRY
299  static struct object object;
300#if defined(CRT_GET_RFIB_TEXT) || defined(CRT_GET_RFIB_DATA)
301  void *tbase, *dbase;
302#ifdef CRT_GET_RFIB_TEXT
303  CRT_GET_RFIB_TEXT (tbase);
304#else
305  tbase = 0;
306#endif
307#ifdef CRT_GET_RFIB_DATA
308  CRT_GET_RFIB_DATA (dbase);
309#else
310  dbase = 0;
311#endif
312  if (__register_frame_info_bases)
313    __register_frame_info_bases (__EH_FRAME_BEGIN__, &object, tbase, dbase);
314#else
315  if (__register_frame_info)
316    __register_frame_info (__EH_FRAME_BEGIN__, &object);
317#endif
318#endif /* USE_EH_FRAME_REGISTRY */
319#ifdef JCR_SECTION_NAME
320  if (__JCR_LIST__[0] && _Jv_RegisterClasses)
321    _Jv_RegisterClasses (__JCR_LIST__);
322#endif /* JCR_SECTION_NAME */
323}
324
325CRT_CALL_STATIC_FUNCTION (INIT_SECTION_ASM_OP, frame_dummy)
326#endif /* USE_EH_FRAME_REGISTRY || JCR_SECTION_NAME */
327
328#else  /* OBJECT_FORMAT_ELF */
329
330/* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o
331   and once in crtend.o).  It must be declared static to avoid a link
332   error.  Here, we define __do_global_ctors as an externally callable
333   function.  It is externally callable so that __main can invoke it when
334   INVOKE__main is defined.  This has the additional effect of forcing cc1
335   to switch to the .text section.  */
336
337static void __do_global_ctors_aux (void);
338void
339__do_global_ctors (void)
340{
341#ifdef INVOKE__main
342  /* If __main won't actually call __do_global_ctors then it doesn't matter
343     what's inside the function.  The inside of __do_global_ctors_aux is
344     called automatically in that case.  And the Alliant fx2800 linker
345     crashes on this reference.  So prevent the crash.  */
346  __do_global_ctors_aux ();
347#endif
348}
349
350asm (INIT_SECTION_ASM_OP);	/* cc1 doesn't know that we are switching! */
351
352/* On some svr4 systems, the initial .init section preamble code provided in
353   crti.o may do something, such as bump the stack, which we have to
354   undo before we reach the function prologue code for __do_global_ctors
355   (directly below).  For such systems, define the macro INIT_SECTION_PREAMBLE
356   to expand into the code needed to undo the actions of the crti.o file.  */
357
358#ifdef INIT_SECTION_PREAMBLE
359  INIT_SECTION_PREAMBLE;
360#endif
361
362/* A routine to invoke all of the global constructors upon entry to the
363   program.  We put this into the .init section (for systems that have
364   such a thing) so that we can properly perform the construction of
365   file-scope static-storage C++ objects within shared libraries.  */
366
367static void __attribute__((used))
368__do_global_ctors_aux (void)	/* prologue goes in .init section */
369{
370  FORCE_CODE_SECTION_ALIGN	/* explicit align before switch to .text */
371  asm (TEXT_SECTION_ASM_OP);	/* don't put epilogue and body in .init */
372  DO_GLOBAL_CTORS_BODY;
373  atexit (__do_global_dtors);
374}
375
376#endif /* OBJECT_FORMAT_ELF */
377
378#elif defined(HAS_INIT_SECTION) /* ! INIT_SECTION_ASM_OP */
379
380/* This case is used by the Irix 6 port, which supports named sections but
381   not an SVR4-style .fini section.  __do_global_dtors can be non-static
382   in this case because we protect it with -hidden_symbol.  */
383
384void
385__do_global_dtors (void)
386{
387  func_ptr *p, f;
388  for (p = __DTOR_LIST__ + 1; (f = *p); p++)
389    f ();
390
391#ifdef USE_EH_FRAME_REGISTRY
392  if (__deregister_frame_info)
393    __deregister_frame_info (__EH_FRAME_BEGIN__);
394#endif
395}
396
397#if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME)
398/* A helper function for __do_global_ctors, which is in crtend.o.  Here
399   in crtbegin.o, we can reference a couple of symbols not visible there.
400   Plus, since we're before libgcc.a, we have no problems referencing
401   functions from there.  */
402void
403__do_global_ctors_1(void)
404{
405#ifdef USE_EH_FRAME_REGISTRY
406  static struct object object;
407  if (__register_frame_info)
408    __register_frame_info (__EH_FRAME_BEGIN__, &object);
409#endif
410#ifdef JCR_SECTION_NAME
411  if (__JCR_LIST__[0] && _Jv_RegisterClasses)
412    _Jv_RegisterClasses (__JCR_LIST__);
413#endif
414}
415#endif /* USE_EH_FRAME_REGISTRY || JCR_SECTION_NAME */
416
417#else /* ! INIT_SECTION_ASM_OP && ! HAS_INIT_SECTION */
418#error "What are you doing with crtstuff.c, then?"
419#endif
420
421#elif defined(CRT_END) /* ! CRT_BEGIN */
422
423/* Put a word containing zero at the end of each of our two lists of function
424   addresses.  Note that the words defined here go into the .ctors and .dtors
425   sections of the crtend.o file, and since that file is always linked in
426   last, these words naturally end up at the very ends of the two lists
427   contained in these two sections.  */
428
429#ifdef CTOR_LIST_END
430CTOR_LIST_END;
431#elif defined(CTORS_SECTION_ASM_OP)
432/* Hack: force cc1 to switch to .data section early, so that assembling
433   __CTOR_LIST__ does not undo our behind-the-back change to .ctors.  */
434static func_ptr force_to_data[1] __attribute__ ((__unused__)) = { };
435asm (CTORS_SECTION_ASM_OP);
436STATIC func_ptr __CTOR_END__[1]
437  __attribute__((aligned(sizeof(func_ptr))))
438  = { (func_ptr) 0 };
439#else
440STATIC func_ptr __CTOR_END__[1]
441  __attribute__((section(".ctors"), aligned(sizeof(func_ptr))))
442  = { (func_ptr) 0 };
443#endif
444
445#ifdef DTOR_LIST_END
446DTOR_LIST_END;
447#elif defined(DTORS_SECTION_ASM_OP)
448asm (DTORS_SECTION_ASM_OP);
449STATIC func_ptr __DTOR_END__[1]
450  __attribute__ ((unused, aligned(sizeof(func_ptr))))
451  = { (func_ptr) 0 };
452#else
453STATIC func_ptr __DTOR_END__[1]
454  __attribute__((unused, section(".dtors"), aligned(sizeof(func_ptr))))
455  = { (func_ptr) 0 };
456#endif
457
458#ifdef EH_FRAME_SECTION_NAME
459/* Terminate the frame unwind info section with a 4byte 0 as a sentinel;
460   this would be the 'length' field in a real FDE.  */
461STATIC EH_FRAME_SECTION_CONST int __FRAME_END__[]
462     __attribute__ ((unused, mode(SI), section(EH_FRAME_SECTION_NAME),
463		     aligned(4)))
464     = { 0 };
465#endif /* EH_FRAME_SECTION_NAME */
466
467#ifdef JCR_SECTION_NAME
468/* Null terminate the .jcr section array.  */
469STATIC void *__JCR_END__[1]
470   __attribute__ ((unused, section(JCR_SECTION_NAME),
471		   aligned(sizeof(void *))))
472   = { 0 };
473#endif /* JCR_SECTION_NAME */
474
475#ifdef INIT_SECTION_ASM_OP
476
477#ifdef OBJECT_FORMAT_ELF
478static void __attribute__((used))
479__do_global_ctors_aux (void)
480{
481  func_ptr *p;
482  for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
483    (*p) ();
484}
485
486/* Stick a call to __do_global_ctors_aux into the .init section.  */
487CRT_CALL_STATIC_FUNCTION (INIT_SECTION_ASM_OP, __do_global_ctors_aux)
488#else  /* OBJECT_FORMAT_ELF */
489
490/* Stick the real initialization code, followed by a normal sort of
491   function epilogue at the very end of the .init section for this
492   entire root executable file or for this entire shared library file.
493
494   Note that we use some tricks here to get *just* the body and just
495   a function epilogue (but no function prologue) into the .init
496   section of the crtend.o file.  Specifically, we switch to the .text
497   section, start to define a function, and then we switch to the .init
498   section just before the body code.
499
500   Earlier on, we put the corresponding function prologue into the .init
501   section of the crtbegin.o file (which will be linked in first).
502
503   Note that we want to invoke all constructors for C++ file-scope static-
504   storage objects AFTER any other possible initialization actions which
505   may be performed by the code in the .init section contributions made by
506   other libraries, etc.  That's because those other initializations may
507   include setup operations for very primitive things (e.g. initializing
508   the state of the floating-point coprocessor, etc.) which should be done
509   before we start to execute any of the user's code.  */
510
511static void
512__do_global_ctors_aux (void)	/* prologue goes in .text section */
513{
514  asm (INIT_SECTION_ASM_OP);
515  DO_GLOBAL_CTORS_BODY;
516  atexit (__do_global_dtors);
517}				/* epilogue and body go in .init section */
518
519FORCE_CODE_SECTION_ALIGN
520asm (TEXT_SECTION_ASM_OP);
521
522#endif /* OBJECT_FORMAT_ELF */
523
524#elif defined(HAS_INIT_SECTION) /* ! INIT_SECTION_ASM_OP */
525
526/* This case is used by the Irix 6 port, which supports named sections but
527   not an SVR4-style .init section.  __do_global_ctors can be non-static
528   in this case because we protect it with -hidden_symbol.  */
529extern void __do_global_ctors_1(void);
530void
531__do_global_ctors (void)
532{
533  func_ptr *p;
534#if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME)
535  __do_global_ctors_1();
536#endif
537  for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
538    (*p) ();
539}
540
541#else /* ! INIT_SECTION_ASM_OP && ! HAS_INIT_SECTION */
542#error "What are you doing with crtstuff.c, then?"
543#endif
544
545#else /* ! CRT_BEGIN && ! CRT_END */
546#error "One of CRT_BEGIN or CRT_END must be defined."
547#endif
548