OSDN Git Service

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