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