OSDN Git Service

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