OSDN Git Service

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