OSDN Git Service

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