OSDN Git Service

Move constructor/destructor handling into target hooks.
[pf3gnuchains/gcc-fork.git] / gcc / crtstuff.c
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 Free Software Foundation, Inc.
5    Contributed by Ron Guilmette (rfg@monkeys.com).
6
7 This file is part of GNU CC.
8
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 In addition to the permissions in the GNU General Public License, the
15 Free Software Foundation gives you unlimited permission to link the
16 compiled version of this file into combinations with other programs,
17 and to distribute those combinations without any restriction coming
18 from the use of this file.  (The General Public License restrictions
19 do apply in other respects; for example, they cover modification of
20 the file, and distribution when not linked into a combine
21 executable.)
22
23 GNU CC is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with GNU CC; see the file COPYING.  If not, write to
30 the Free Software Foundation, 59 Temple Place - Suite 330,
31 Boston, MA 02111-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 CRT_CALL_STATIC_FUNCTION
66 # define CRT_CALL_STATIC_FUNCTION(func) func ()
67 #endif
68
69 /* We do not want to add the weak attribute to the declarations of these
70    routines in unwind-dw2-fde.h because that will cause the definition of
71    these symbols to be weak as well.
72
73    This exposes a core issue, how to handle creating weak references vs
74    how to create weak definitions.  Either we have to have the definition
75    of TARGET_WEAK_ATTRIBUTE be conditional in the shared header files or
76    have a second declaration if we want a function's references to be weak,
77    but not its definition.
78
79    Making TARGET_WEAK_ATTRIBUTE conditional seems like a good solution until
80    one thinks about scaling to larger problems -- ie, the condition under
81    which TARGET_WEAK_ATTRIBUTE is active will eventually get far too
82    complicated.
83
84    So, we take an approach similar to #pragma weak -- we have a second
85    declaration for functions that we want to have weak references.
86
87    Neither way is particularly good.  */
88    
89 /* References to __register_frame_info and __deregister_frame_info should
90    be weak in this file if at all possible.  */
91 extern void __register_frame_info (void *, struct object *)
92                                   TARGET_ATTRIBUTE_WEAK;
93 extern void __register_frame_info_bases (void *, struct object *,
94                                          void *, void *)
95                                   TARGET_ATTRIBUTE_WEAK;
96 extern void *__deregister_frame_info (void *)
97                                      TARGET_ATTRIBUTE_WEAK;
98 extern void *__deregister_frame_info_bases (void *)
99                                      TARGET_ATTRIBUTE_WEAK;
100
101 #ifndef OBJECT_FORMAT_MACHO
102
103 #ifdef OBJECT_FORMAT_ELF
104
105 /*  Declare a pointer to void function type.  */
106 typedef void (*func_ptr) (void);
107 #define STATIC static
108
109 #else  /* OBJECT_FORMAT_ELF */
110
111 #include "gbl-ctors.h"
112
113 #define STATIC
114
115 #endif /* OBJECT_FORMAT_ELF */
116
117 #ifdef CRT_BEGIN
118
119 #ifdef INIT_SECTION_ASM_OP
120
121 #ifdef OBJECT_FORMAT_ELF
122
123 /* Declare the __dso_handle variable.  It should have a unique value
124    in every shared-object; in a main program its value is zero.  The
125    object should in any case be protected.  This means the instance
126    in one DSO or the main program is not used in another object.  The
127    dynamic linker takes care of this.  */
128
129 /* XXX Ideally the following should be implemented using
130        __attribute__ ((__visibility__ ("hidden")))
131    but the __attribute__ support is not yet there.  */
132 #ifdef HAVE_GAS_HIDDEN
133 asm (".hidden\t__dso_handle");
134 #endif
135
136 #ifdef CRTSTUFFS_O
137 void *__dso_handle = &__dso_handle;
138 #else
139 void *__dso_handle = 0;
140 #endif
141
142 /* The __cxa_finalize function may not be available so we use only a
143    weak declaration.  */
144 extern void __cxa_finalize (void *) TARGET_ATTRIBUTE_WEAK;
145
146 /* Run all the global destructors on exit from the program.  */
147  
148 /* Some systems place the number of pointers in the first word of the
149    table.  On SVR4 however, that word is -1.  In all cases, the table is
150    null-terminated.  On SVR4, we start from the beginning of the list and
151    invoke each per-compilation-unit destructor routine in order
152    until we find that null.
153
154    Note that this function MUST be static.  There will be one of these
155    functions in each root executable and one in each shared library, but
156    although they all have the same code, each one is unique in that it
157    refers to one particular associated `__DTOR_LIST__' which belongs to the
158    same particular root executable or shared library file.
159
160    On some systems, this routine is run more than once from the .fini,
161    when exit is called recursively, so we arrange to remember where in
162    the list we left off processing, and we resume at that point,
163    should we be re-invoked.  */
164
165 static char __EH_FRAME_BEGIN__[];
166 static func_ptr __DTOR_LIST__[];
167 static void
168 __do_global_dtors_aux (void)
169 {
170   static func_ptr *p = __DTOR_LIST__ + 1;
171   static int completed;
172   func_ptr f;
173
174   if (__builtin_expect (completed, 0))
175     return;
176
177 #ifdef CRTSTUFFS_O
178   if (__cxa_finalize)
179     __cxa_finalize (__dso_handle);
180 #endif
181
182   while ((f = *p))
183     {
184       p++;
185       f ();
186     }
187
188 #ifdef EH_FRAME_SECTION_NAME
189 #if defined(CRT_GET_RFIB_TEXT) || defined(CRT_GET_RFIB_DATA)
190   /* If we used the new __register_frame_info_bases interface,
191      make sure that we deregister from the same place.  */
192   if (__deregister_frame_info_bases)
193     __deregister_frame_info_bases (__EH_FRAME_BEGIN__);
194 #else
195   if (__deregister_frame_info)
196     __deregister_frame_info (__EH_FRAME_BEGIN__);
197 #endif
198 #endif
199
200   completed = 1;
201 }
202
203
204 /* Stick a call to __do_global_dtors_aux into the .fini section.  */
205
206 static void __attribute__ ((__unused__))
207 fini_dummy (void)
208 {
209   asm (FINI_SECTION_ASM_OP);
210   CRT_CALL_STATIC_FUNCTION (__do_global_dtors_aux);
211 #ifdef FORCE_FINI_SECTION_ALIGN
212   FORCE_FINI_SECTION_ALIGN;
213 #endif
214   asm (TEXT_SECTION_ASM_OP);
215 }
216
217 #ifdef EH_FRAME_SECTION_NAME
218 /* Stick a call to __register_frame_info into the .init section.  For some
219    reason calls with no arguments work more reliably in .init, so stick the
220    call in another function.  */
221
222 static void
223 frame_dummy (void)
224 {
225   static struct object object;
226 #if defined(CRT_GET_RFIB_TEXT) || defined(CRT_GET_RFIB_DATA)
227   void *tbase, *dbase;
228 #ifdef CRT_GET_RFIB_TEXT
229   CRT_GET_RFIB_TEXT (tbase);
230 #else
231   tbase = 0;
232 #endif
233 #ifdef CRT_GET_RFIB_DATA
234   CRT_GET_RFIB_DATA (dbase);
235 #else
236   dbase = 0;
237 #endif
238   if (__register_frame_info_bases)
239     __register_frame_info_bases (__EH_FRAME_BEGIN__, &object, tbase, dbase);
240 #else
241   if (__register_frame_info)
242     __register_frame_info (__EH_FRAME_BEGIN__, &object);
243 #endif
244 }
245
246 static void __attribute__ ((__unused__))
247 init_dummy (void)
248 {
249   asm (INIT_SECTION_ASM_OP);
250   CRT_CALL_STATIC_FUNCTION (frame_dummy);
251 #ifdef FORCE_INIT_SECTION_ALIGN
252   FORCE_INIT_SECTION_ALIGN;
253 #endif
254   asm (TEXT_SECTION_ASM_OP);
255 }
256 #endif /* EH_FRAME_SECTION_NAME */
257
258 #else  /* OBJECT_FORMAT_ELF */
259
260 /* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o
261    and once in crtend.o).  It must be declared static to avoid a link
262    error.  Here, we define __do_global_ctors as an externally callable
263    function.  It is externally callable so that __main can invoke it when
264    INVOKE__main is defined.  This has the additional effect of forcing cc1
265    to switch to the .text section.  */
266
267 static void __do_global_ctors_aux (void);
268 void
269 __do_global_ctors (void)
270 {
271 #ifdef INVOKE__main  /* If __main won't actually call __do_global_ctors
272                         then it doesn't matter what's inside the function.
273                         The inside of __do_global_ctors_aux is called
274                         automatically in that case.
275                         And the Alliant fx2800 linker crashes
276                         on this reference.  So prevent the crash.  */
277   __do_global_ctors_aux ();
278 #endif
279 }
280
281 asm (INIT_SECTION_ASM_OP);      /* cc1 doesn't know that we are switching! */
282
283 /* On some svr4 systems, the initial .init section preamble code provided in
284    crti.o may do something, such as bump the stack, which we have to 
285    undo before we reach the function prologue code for __do_global_ctors 
286    (directly below).  For such systems, define the macro INIT_SECTION_PREAMBLE
287    to expand into the code needed to undo the actions of the crti.o file.  */
288
289 #ifdef INIT_SECTION_PREAMBLE
290   INIT_SECTION_PREAMBLE;
291 #endif
292
293 /* A routine to invoke all of the global constructors upon entry to the
294    program.  We put this into the .init section (for systems that have
295    such a thing) so that we can properly perform the construction of
296    file-scope static-storage C++ objects within shared libraries.   */
297
298 static void
299 __do_global_ctors_aux (void)    /* prologue goes in .init section */
300 {
301 #ifdef FORCE_INIT_SECTION_ALIGN
302   FORCE_INIT_SECTION_ALIGN;     /* Explicit align before switch to .text */
303 #endif
304   asm (TEXT_SECTION_ASM_OP);    /* don't put epilogue and body in .init */
305   DO_GLOBAL_CTORS_BODY;
306   atexit (__do_global_dtors);
307 }
308
309 #endif /* OBJECT_FORMAT_ELF */
310
311 #else /* defined(INIT_SECTION_ASM_OP) */
312
313 #ifdef HAS_INIT_SECTION
314 /* This case is used by the Irix 6 port, which supports named sections but
315    not an SVR4-style .fini section.  __do_global_dtors can be non-static
316    in this case because we protect it with -hidden_symbol.  */
317
318 static char __EH_FRAME_BEGIN__[];
319 static func_ptr __DTOR_LIST__[];
320 void
321 __do_global_dtors (void)
322 {
323   func_ptr *p, f;
324   for (p = __DTOR_LIST__ + 1; (f = *p); p++)
325     f ();
326
327 #ifdef EH_FRAME_SECTION_NAME
328   if (__deregister_frame_info)
329     __deregister_frame_info (__EH_FRAME_BEGIN__);
330 #endif
331 }
332
333 #ifdef EH_FRAME_SECTION_NAME
334 /* Define a function here to call __register_frame.  crtend.o is linked in
335    after libgcc.a, and hence can't call libgcc.a functions directly.  That
336    can lead to unresolved function references.  */
337 void
338 __frame_dummy (void)
339 {
340   static struct object object;
341   if (__register_frame_info)
342     __register_frame_info (__EH_FRAME_BEGIN__, &object);
343 }
344 #endif
345 #endif
346
347 #endif /* defined(INIT_SECTION_ASM_OP) */
348
349 /* NOTE:  In order to be able to support SVR4 shared libraries, we arrange
350    to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
351    __DTOR_END__ } per root executable and also one set of these symbols
352    per shared library.  So in any given whole process image, we may have
353    multiple definitions of each of these symbols.  In order to prevent
354    these definitions from conflicting with one another, and in order to
355    ensure that the proper lists are used for the initialization/finalization
356    of each individual shared library (respectively), we give these symbols
357    only internal (i.e. `static') linkage, and we also make it a point to
358    refer to only the __CTOR_END__ symbol in crtend.o and the __DTOR_LIST__
359    symbol in crtbegin.o, where they are defined.  */
360
361 /* The -1 is a flag to __do_global_[cd]tors indicating that this table
362    does not start with a count of elements.  */
363 #ifdef CTOR_LIST_BEGIN
364 CTOR_LIST_BEGIN;
365
366 #elif defined(CTORS_SECTION_ASM_OP)
367 /* Hack: force cc1 to switch to .data section early, so that assembling
368    __CTOR_LIST__ does not undo our behind-the-back change to .ctors.  */
369 static func_ptr force_to_data[1] __attribute__ ((__unused__)) = { };
370 asm (CTORS_SECTION_ASM_OP);
371 STATIC func_ptr __CTOR_LIST__[1]
372   __attribute__ ((__unused__))
373   = { (func_ptr) (-1) };
374
375 #else
376 STATIC func_ptr __CTOR_LIST__[1]
377   __attribute__ ((__unused__, section(".ctors")))
378   = { (func_ptr) (-1) };
379
380 #endif
381
382 #ifdef DTOR_LIST_BEGIN
383 DTOR_LIST_BEGIN;
384 #elif defined(DTORS_SECTION_ASM_OP)
385 asm (DTORS_SECTION_ASM_OP);
386 STATIC func_ptr __DTOR_LIST__[1] = { (func_ptr) (-1) };
387 #else
388 STATIC func_ptr __DTOR_LIST__[1]
389   __attribute__((section(".dtors")))
390   = { (func_ptr) (-1) };
391 #endif
392
393 #ifdef EH_FRAME_SECTION_NAME
394 /* Stick a label at the beginning of the frame unwind info so we can register
395    and deregister it with the exception handling library code.  */
396 #ifdef INIT_SECTION_ASM_OP
397 STATIC
398 #endif
399 char __EH_FRAME_BEGIN__[]
400      __attribute__((section(EH_FRAME_SECTION_NAME)))
401      = { };
402 #endif /* EH_FRAME_SECTION_NAME */
403
404 #ifdef JCR_SECTION_NAME
405 /* Stick a label at the beginning of the java class registration info
406    so we can register them properly.  */
407
408 STATIC void *__JCR_LIST__[] __attribute__ ((unused, section(JCR_SECTION_NAME)))
409   = { 0 };
410 #endif /* JCR_SECTION_NAME */
411
412 #endif /* defined(CRT_BEGIN) */
413
414 #ifdef CRT_END
415
416 #ifdef INIT_SECTION_ASM_OP
417
418 #ifdef OBJECT_FORMAT_ELF
419
420 #ifdef JCR_SECTION_NAME
421 extern void _Jv_RegisterClasses (void *) __attribute__((weak));
422 static void *__JCR_END__[];
423 #endif
424
425 static func_ptr __CTOR_END__[];
426 static void
427 __do_global_ctors_aux (void)
428 {
429   func_ptr *p;
430 #ifdef JCR_SECTION_NAME
431   void **jcr;
432   if (_Jv_RegisterClasses)
433     {
434       for (jcr = __JCR_END__ - 1; *jcr != NULL; jcr--);
435       if (*(jcr + 1))
436         _Jv_RegisterClasses (jcr + 1);
437     }
438 #endif
439   for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
440     (*p) ();
441 }
442
443 /* Stick a call to __do_global_ctors_aux into the .init section.  */
444
445 static void __attribute__ ((__unused__))
446 init_dummy (void)
447 {
448   asm (INIT_SECTION_ASM_OP);
449   CRT_CALL_STATIC_FUNCTION (__do_global_ctors_aux);
450 #ifdef FORCE_INIT_SECTION_ALIGN
451   FORCE_INIT_SECTION_ALIGN;
452 #endif
453   asm (TEXT_SECTION_ASM_OP);
454 #ifdef CRT_END_INIT_DUMMY
455   CRT_END_INIT_DUMMY;
456 #endif
457 }
458
459 #else  /* OBJECT_FORMAT_ELF */
460
461 /* Stick the real initialization code, followed by a normal sort of
462    function epilogue at the very end of the .init section for this
463    entire root executable file or for this entire shared library file.
464
465    Note that we use some tricks here to get *just* the body and just
466    a function epilogue (but no function prologue) into the .init
467    section of the crtend.o file.  Specifically, we switch to the .text
468    section, start to define a function, and then we switch to the .init
469    section just before the body code.
470
471    Earlier on, we put the corresponding function prologue into the .init
472    section of the crtbegin.o file (which will be linked in first).
473
474    Note that we want to invoke all constructors for C++ file-scope static-
475    storage objects AFTER any other possible initialization actions which
476    may be performed by the code in the .init section contributions made by
477    other libraries, etc.  That's because those other initializations may
478    include setup operations for very primitive things (e.g. initializing
479    the state of the floating-point coprocessor, etc.) which should be done
480    before we start to execute any of the user's code.  */
481
482 static void
483 __do_global_ctors_aux (void)    /* prologue goes in .text section */
484 {
485   asm (INIT_SECTION_ASM_OP);
486   DO_GLOBAL_CTORS_BODY;
487   atexit (__do_global_dtors);
488 }                               /* epilogue and body go in .init section */
489
490 #ifdef FORCE_INIT_SECTION_ALIGN
491 FORCE_INIT_SECTION_ALIGN;
492 #endif
493
494 asm (TEXT_SECTION_ASM_OP);
495
496 #endif /* OBJECT_FORMAT_ELF */
497
498 #else /* defined(INIT_SECTION_ASM_OP) */
499
500 #ifdef HAS_INIT_SECTION
501 /* This case is used by the Irix 6 port, which supports named sections but
502    not an SVR4-style .init section.  __do_global_ctors can be non-static
503    in this case because we protect it with -hidden_symbol.  */
504 static func_ptr __CTOR_END__[];
505 #ifdef EH_FRAME_SECTION_NAME
506 extern void __frame_dummy (void);
507 #endif
508 void
509 __do_global_ctors (void)
510 {
511   func_ptr *p;
512 #ifdef EH_FRAME_SECTION_NAME
513   __frame_dummy ();
514 #endif
515   for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
516     (*p) ();
517 }
518 #endif
519
520 #endif /* defined(INIT_SECTION_ASM_OP) */
521
522 /* Put a word containing zero at the end of each of our two lists of function
523    addresses.  Note that the words defined here go into the .ctors and .dtors
524    sections of the crtend.o file, and since that file is always linked in
525    last, these words naturally end up at the very ends of the two lists
526    contained in these two sections.  */
527
528 #ifdef CTOR_LIST_END
529 CTOR_LIST_END;
530
531 #elif defined(CTORS_SECTION_ASM_OP)
532 /* Hack: force cc1 to switch to .data section early, so that assembling
533    __CTOR_LIST__ does not undo our behind-the-back change to .ctors.  */
534 static func_ptr force_to_data[1] __attribute__ ((__unused__)) = { };
535 asm (CTORS_SECTION_ASM_OP);
536 STATIC func_ptr __CTOR_END__[1] = { (func_ptr) 0 };
537
538 #else
539 STATIC func_ptr __CTOR_END__[1]
540   __attribute__((section(".ctors")))
541   = { (func_ptr) 0 };
542 #endif
543
544 #ifdef DTOR_LIST_END
545 DTOR_LIST_END;
546 #elif defined(DTORS_SECTION_ASM_OP)
547 asm (DTORS_SECTION_ASM_OP);
548 STATIC func_ptr __DTOR_END__[1] __attribute__ ((unused))
549   = { (func_ptr) 0 };
550 #else
551 STATIC func_ptr __DTOR_END__[1]
552   __attribute__((unused, section(".dtors")))
553   = { (func_ptr) 0 };
554 #endif
555
556 #ifdef EH_FRAME_SECTION_NAME
557 /* Terminate the frame unwind info section with a 4byte 0 as a sentinel;
558    this would be the 'length' field in a real FDE.  */
559 STATIC int __FRAME_END__[]
560      __attribute__ ((unused, mode(SI), section(EH_FRAME_SECTION_NAME)))
561      = { 0 };
562 #endif /* EH_FRAME_SECTION */
563
564 #ifdef JCR_SECTION_NAME
565 /* Stick a label at the beginning of the java class registration info
566    so we can register them properly.  */
567
568 STATIC void *__JCR_END__[1] 
569      __attribute__ ((unused, section(JCR_SECTION_NAME))) = { 0 };
570 #endif /* JCR_SECTION_NAME */
571
572 #endif /* defined(CRT_END) */
573
574 #else  /* OBJECT_FORMAT_MACHO */
575
576 /* For Mach-O format executables, we assume that the system's runtime is
577    smart enough to handle constructors and destructors, but doesn't have
578    an init section (if it can't even handle constructors/destructors
579    you should be using INVOKE__main, not crtstuff). All we need to do
580    is install/deinstall the frame information for exceptions. We do this
581    by putting a constructor in crtbegin.o and a destructor in crtend.o.
582
583    crtend.o also puts in the terminating zero in the frame information
584    segment.  */
585
586 /* The crtstuff for other object formats use the symbol __EH_FRAME_BEGIN__
587    to figure out the start of the exception frame, but here we use
588    getsectbynamefromheader to find this value. Either method would work,
589    but this method avoids creating any global symbols, which seems
590    cleaner.  */
591
592 #include <mach-o/ldsyms.h>
593 extern const struct section *
594   getsectbynamefromheader (const struct mach_header *,
595                            const char *, const char *);
596
597 #ifdef CRT_BEGIN
598
599 static void __reg_frame_ctor (void) __attribute__ ((constructor));
600
601 static void
602 __reg_frame_ctor (void)
603 {
604   static struct object object;
605   const struct section *eh_frame;
606
607   eh_frame = getsectbynamefromheader (&_mh_execute_header,
608                                       "__TEXT", "__eh_frame");
609   __register_frame_info ((void *) eh_frame->addr, &object);
610 }
611
612 #endif /* CRT_BEGIN */
613
614 #ifdef CRT_END
615
616 static void __dereg_frame_dtor (void) __attribute__ ((destructor));
617
618 static void
619 __dereg_frame_dtor (void)
620 {
621   const struct section *eh_frame;
622
623   eh_frame = getsectbynamefromheader (&_mh_execute_header,
624                                       "__TEXT", "__eh_frame");
625   __deregister_frame_info ((void *) eh_frame->addr);
626 }
627
628 /* Terminate the frame section with a final zero.  */
629 STATIC int __FRAME_END__[]
630      __attribute__ ((unused, mode(SI), section(EH_FRAME_SECTION_NAME)))
631      = { 0 };
632 #endif /* CRT_END */
633
634 #endif /* OBJECT_FORMAT_MACHO */