OSDN Git Service

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