OSDN Git Service

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