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