2005-04-29 Jim Tison <jtison@us.ibm.com>
[official-gcc.git] / gcc / crtstuff.c
blob6fd365577b90fa75eb157352481e48104d6e4e35
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, 2002, 2003, 2004 Free Software Foundation, Inc.
5 Contributed by Ron Guilmette (rfg@monkeys.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 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 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
24 WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 for more details.
28 You should have received a copy of the GNU General Public License
29 along with GCC; see the file COPYING. If not, write to the Free
30 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
31 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 #define IN_LIBGCC2
60 /* We include auto-host.h here to get HAVE_GAS_HIDDEN. This is
61 supposedly valid even though this is a "target" file. */
62 #include "auto-host.h"
63 #include "tconfig.h"
64 #include "tsystem.h"
65 #include "coretypes.h"
66 #include "tm.h"
67 #include "unwind-dw2-fde.h"
69 #ifndef FORCE_CODE_SECTION_ALIGN
70 # define FORCE_CODE_SECTION_ALIGN
71 #endif
73 #ifndef CRT_CALL_STATIC_FUNCTION
74 # define CRT_CALL_STATIC_FUNCTION(SECTION_OP, FUNC) \
75 static void __attribute__((__used__)) \
76 call_ ## FUNC (void) \
77 { \
78 asm (SECTION_OP); \
79 FUNC (); \
80 FORCE_CODE_SECTION_ALIGN \
81 asm (TEXT_SECTION_ASM_OP); \
83 #endif
85 #if defined(OBJECT_FORMAT_ELF) && defined(HAVE_LD_EH_FRAME_HDR) \
86 && !defined(inhibit_libc) && !defined(CRTSTUFFT_O) \
87 && defined(__GLIBC__) && __GLIBC__ >= 2
88 #include <link.h>
89 # if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
90 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
91 # define USE_PT_GNU_EH_FRAME
92 # endif
93 #endif
94 #if defined(EH_FRAME_SECTION_NAME) && !defined(USE_PT_GNU_EH_FRAME)
95 # define USE_EH_FRAME_REGISTRY
96 #endif
97 #if defined(EH_FRAME_SECTION_NAME) && EH_TABLES_CAN_BE_READ_ONLY
98 # define EH_FRAME_SECTION_CONST const
99 #else
100 # define EH_FRAME_SECTION_CONST
101 #endif
103 /* We do not want to add the weak attribute to the declarations of these
104 routines in unwind-dw2-fde.h because that will cause the definition of
105 these symbols to be weak as well.
107 This exposes a core issue, how to handle creating weak references vs
108 how to create weak definitions. Either we have to have the definition
109 of TARGET_WEAK_ATTRIBUTE be conditional in the shared header files or
110 have a second declaration if we want a function's references to be weak,
111 but not its definition.
113 Making TARGET_WEAK_ATTRIBUTE conditional seems like a good solution until
114 one thinks about scaling to larger problems -- i.e., the condition under
115 which TARGET_WEAK_ATTRIBUTE is active will eventually get far too
116 complicated.
118 So, we take an approach similar to #pragma weak -- we have a second
119 declaration for functions that we want to have weak references.
121 Neither way is particularly good. */
123 /* References to __register_frame_info and __deregister_frame_info should
124 be weak in this file if at all possible. */
125 extern void __register_frame_info (const void *, struct object *)
126 TARGET_ATTRIBUTE_WEAK;
127 extern void __register_frame_info_bases (const void *, struct object *,
128 void *, void *)
129 TARGET_ATTRIBUTE_WEAK;
130 extern void *__deregister_frame_info (const void *)
131 TARGET_ATTRIBUTE_WEAK;
132 extern void *__deregister_frame_info_bases (const void *)
133 TARGET_ATTRIBUTE_WEAK;
134 extern void __do_global_ctors_1 (void);
136 /* Likewise for _Jv_RegisterClasses. */
137 extern void _Jv_RegisterClasses (void *) TARGET_ATTRIBUTE_WEAK;
139 #ifdef OBJECT_FORMAT_ELF
141 /* Declare a pointer to void function type. */
142 typedef void (*func_ptr) (void);
143 #define STATIC static
145 #else /* OBJECT_FORMAT_ELF */
147 #include "gbl-ctors.h"
149 #define STATIC
151 #endif /* OBJECT_FORMAT_ELF */
153 #ifdef CRT_BEGIN
155 /* NOTE: In order to be able to support SVR4 shared libraries, we arrange
156 to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
157 __DTOR_END__ } per root executable and also one set of these symbols
158 per shared library. So in any given whole process image, we may have
159 multiple definitions of each of these symbols. In order to prevent
160 these definitions from conflicting with one another, and in order to
161 ensure that the proper lists are used for the initialization/finalization
162 of each individual shared library (respectively), we give these symbols
163 only internal (i.e. `static') linkage, and we also make it a point to
164 refer to only the __CTOR_END__ symbol in crtend.o and the __DTOR_LIST__
165 symbol in crtbegin.o, where they are defined. */
167 /* The -1 is a flag to __do_global_[cd]tors indicating that this table
168 does not start with a count of elements. */
169 #ifdef CTOR_LIST_BEGIN
170 CTOR_LIST_BEGIN;
171 #elif defined(CTORS_SECTION_ASM_OP)
172 /* Hack: force cc1 to switch to .data section early, so that assembling
173 __CTOR_LIST__ does not undo our behind-the-back change to .ctors. */
174 static func_ptr force_to_data[1] __attribute__ ((__unused__)) = { };
175 asm (CTORS_SECTION_ASM_OP);
176 STATIC func_ptr __CTOR_LIST__[1]
177 __attribute__ ((__unused__, aligned(sizeof(func_ptr))))
178 = { (func_ptr) (-1) };
179 #else
180 STATIC func_ptr __CTOR_LIST__[1]
181 __attribute__ ((__unused__, section(".ctors"), aligned(sizeof(func_ptr))))
182 = { (func_ptr) (-1) };
183 #endif /* __CTOR_LIST__ alternatives */
185 #ifdef DTOR_LIST_BEGIN
186 DTOR_LIST_BEGIN;
187 #elif defined(DTORS_SECTION_ASM_OP)
188 asm (DTORS_SECTION_ASM_OP);
189 STATIC func_ptr __DTOR_LIST__[1]
190 __attribute__ ((aligned(sizeof(func_ptr))))
191 = { (func_ptr) (-1) };
192 #else
193 STATIC func_ptr __DTOR_LIST__[1]
194 __attribute__((section(".dtors"), aligned(sizeof(func_ptr))))
195 = { (func_ptr) (-1) };
196 #endif /* __DTOR_LIST__ alternatives */
198 #ifdef USE_EH_FRAME_REGISTRY
199 /* Stick a label at the beginning of the frame unwind info so we can register
200 and deregister it with the exception handling library code. */
201 STATIC EH_FRAME_SECTION_CONST char __EH_FRAME_BEGIN__[]
202 __attribute__((section(EH_FRAME_SECTION_NAME), aligned(4)))
203 = { };
204 #endif /* USE_EH_FRAME_REGISTRY */
206 #ifdef JCR_SECTION_NAME
207 /* Stick a label at the beginning of the java class registration info
208 so we can register them properly. */
209 STATIC void *__JCR_LIST__[]
210 __attribute__ ((unused, section(JCR_SECTION_NAME), aligned(sizeof(void*))))
211 = { };
212 #endif /* JCR_SECTION_NAME */
214 #if defined(INIT_SECTION_ASM_OP) || defined(INIT_ARRAY_SECTION_ASM_OP)
216 #ifdef OBJECT_FORMAT_ELF
218 /* Declare the __dso_handle variable. It should have a unique value
219 in every shared-object; in a main program its value is zero. The
220 object should in any case be protected. This means the instance
221 in one DSO or the main program is not used in another object. The
222 dynamic linker takes care of this. */
224 #ifdef HAVE_GAS_HIDDEN
225 extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
226 #endif
227 #ifdef CRTSTUFFS_O
228 void *__dso_handle = &__dso_handle;
229 #else
230 void *__dso_handle = 0;
231 #endif
233 /* The __cxa_finalize function may not be available so we use only a
234 weak declaration. */
235 extern void __cxa_finalize (void *) TARGET_ATTRIBUTE_WEAK;
237 /* Run all the global destructors on exit from the program. */
239 /* Some systems place the number of pointers in the first word of the
240 table. On SVR4 however, that word is -1. In all cases, the table is
241 null-terminated. On SVR4, we start from the beginning of the list and
242 invoke each per-compilation-unit destructor routine in order
243 until we find that null.
245 Note that this function MUST be static. There will be one of these
246 functions in each root executable and one in each shared library, but
247 although they all have the same code, each one is unique in that it
248 refers to one particular associated `__DTOR_LIST__' which belongs to the
249 same particular root executable or shared library file.
251 On some systems, this routine is run more than once from the .fini,
252 when exit is called recursively, so we arrange to remember where in
253 the list we left off processing, and we resume at that point,
254 should we be re-invoked. */
256 static void __attribute__((used))
257 __do_global_dtors_aux (void)
259 #ifndef FINI_ARRAY_SECTION_ASM_OP
260 static func_ptr *p = __DTOR_LIST__ + 1;
261 func_ptr f;
262 #endif /* !defined(FINI_ARRAY_SECTION_ASM_OP) */
263 static _Bool completed;
265 if (__builtin_expect (completed, 0))
266 return;
268 #ifdef CRTSTUFFS_O
269 if (__cxa_finalize)
270 __cxa_finalize (__dso_handle);
271 #endif
273 #ifdef FINI_ARRAY_SECTION_ASM_OP
274 /* If we are using .fini_array then destructors will be run via that
275 mechanism. */
276 #else /* !defined (FINI_ARRAY_SECTION_ASM_OP) */
277 while ((f = *p))
279 p++;
280 f ();
282 #endif /* !defined(FINI_ARRAY_SECTION_ASM_OP) */
284 #ifdef USE_EH_FRAME_REGISTRY
285 #ifdef CRT_GET_RFIB_DATA
286 /* If we used the new __register_frame_info_bases interface,
287 make sure that we deregister from the same place. */
288 if (__deregister_frame_info_bases)
289 __deregister_frame_info_bases (__EH_FRAME_BEGIN__);
290 #else
291 if (__deregister_frame_info)
292 __deregister_frame_info (__EH_FRAME_BEGIN__);
293 #endif
294 #endif
296 completed = 1;
299 /* Stick a call to __do_global_dtors_aux into the .fini section. */
300 #ifdef FINI_SECTION_ASM_OP
301 CRT_CALL_STATIC_FUNCTION (FINI_SECTION_ASM_OP, __do_global_dtors_aux)
302 #else /* !defined(FINI_SECTION_ASM_OP) */
303 static func_ptr __do_global_dtors_aux_fini_array_entry[]
304 __attribute__ ((__unused__, section(".fini_array")))
305 = { __do_global_dtors_aux };
306 #endif /* !defined(FINI_SECTION_ASM_OP) */
308 #if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME)
309 /* Stick a call to __register_frame_info into the .init section. For some
310 reason calls with no arguments work more reliably in .init, so stick the
311 call in another function. */
313 static void __attribute__((used))
314 frame_dummy (void)
316 #ifdef USE_EH_FRAME_REGISTRY
317 static struct object object;
318 #ifdef CRT_GET_RFIB_DATA
319 void *tbase, *dbase;
320 tbase = 0;
321 CRT_GET_RFIB_DATA (dbase);
322 if (__register_frame_info_bases)
323 __register_frame_info_bases (__EH_FRAME_BEGIN__, &object, tbase, dbase);
324 #else
325 if (__register_frame_info)
326 __register_frame_info (__EH_FRAME_BEGIN__, &object);
327 #endif /* CRT_GET_RFIB_DATA */
328 #endif /* USE_EH_FRAME_REGISTRY */
329 #ifdef JCR_SECTION_NAME
330 if (__JCR_LIST__[0])
332 void (*register_classes) (void *) = _Jv_RegisterClasses;
333 __asm ("" : "+r" (register_classes));
334 if (register_classes)
335 register_classes (__JCR_LIST__);
337 #endif /* JCR_SECTION_NAME */
340 #ifdef INIT_SECTION_ASM_OP
341 CRT_CALL_STATIC_FUNCTION (INIT_SECTION_ASM_OP, frame_dummy)
342 #else /* defined(INIT_SECTION_ASM_OP) */
343 static func_ptr __frame_dummy_init_array_entry[]
344 __attribute__ ((__unused__, section(".init_array")))
345 = { frame_dummy };
346 #endif /* !defined(INIT_SECTION_ASM_OP) */
347 #endif /* USE_EH_FRAME_REGISTRY || JCR_SECTION_NAME */
349 #else /* OBJECT_FORMAT_ELF */
351 /* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o
352 and once in crtend.o). It must be declared static to avoid a link
353 error. Here, we define __do_global_ctors as an externally callable
354 function. It is externally callable so that __main can invoke it when
355 INVOKE__main is defined. This has the additional effect of forcing cc1
356 to switch to the .text section. */
358 static void __do_global_ctors_aux (void);
359 void
360 __do_global_ctors (void)
362 #ifdef INVOKE__main
363 /* If __main won't actually call __do_global_ctors then it doesn't matter
364 what's inside the function. The inside of __do_global_ctors_aux is
365 called automatically in that case. And the Alliant fx2800 linker
366 crashes on this reference. So prevent the crash. */
367 __do_global_ctors_aux ();
368 #endif
371 asm (INIT_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
373 /* A routine to invoke all of the global constructors upon entry to the
374 program. We put this into the .init section (for systems that have
375 such a thing) so that we can properly perform the construction of
376 file-scope static-storage C++ objects within shared libraries. */
378 static void __attribute__((used))
379 __do_global_ctors_aux (void) /* prologue goes in .init section */
381 FORCE_CODE_SECTION_ALIGN /* explicit align before switch to .text */
382 asm (TEXT_SECTION_ASM_OP); /* don't put epilogue and body in .init */
383 DO_GLOBAL_CTORS_BODY;
384 atexit (__do_global_dtors);
387 #endif /* OBJECT_FORMAT_ELF */
389 #elif defined(HAS_INIT_SECTION) /* ! INIT_SECTION_ASM_OP */
391 extern void __do_global_dtors (void);
393 /* This case is used by the Irix 6 port, which supports named sections but
394 not an SVR4-style .fini section. __do_global_dtors can be non-static
395 in this case because we protect it with -hidden_symbol. */
397 void
398 __do_global_dtors (void)
400 func_ptr *p, f;
401 for (p = __DTOR_LIST__ + 1; (f = *p); p++)
402 f ();
404 #ifdef USE_EH_FRAME_REGISTRY
405 if (__deregister_frame_info)
406 __deregister_frame_info (__EH_FRAME_BEGIN__);
407 #endif
410 #if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME)
411 /* A helper function for __do_global_ctors, which is in crtend.o. Here
412 in crtbegin.o, we can reference a couple of symbols not visible there.
413 Plus, since we're before libgcc.a, we have no problems referencing
414 functions from there. */
415 void
416 __do_global_ctors_1(void)
418 #ifdef USE_EH_FRAME_REGISTRY
419 static struct object object;
420 if (__register_frame_info)
421 __register_frame_info (__EH_FRAME_BEGIN__, &object);
422 #endif
423 #ifdef JCR_SECTION_NAME
424 if (__JCR_LIST__[0])
426 void (*register_classes) (void *) = _Jv_RegisterClasses;
427 __asm ("" : "+r" (register_classes));
428 if (register_classes)
429 register_classes (__JCR_LIST__);
431 #endif
433 #endif /* USE_EH_FRAME_REGISTRY || JCR_SECTION_NAME */
435 #else /* ! INIT_SECTION_ASM_OP && ! HAS_INIT_SECTION */
436 #error "What are you doing with crtstuff.c, then?"
437 #endif
439 #elif defined(CRT_END) /* ! CRT_BEGIN */
441 /* Put a word containing zero at the end of each of our two lists of function
442 addresses. Note that the words defined here go into the .ctors and .dtors
443 sections of the crtend.o file, and since that file is always linked in
444 last, these words naturally end up at the very ends of the two lists
445 contained in these two sections. */
447 #ifdef CTOR_LIST_END
448 CTOR_LIST_END;
449 #elif defined(CTORS_SECTION_ASM_OP)
450 /* Hack: force cc1 to switch to .data section early, so that assembling
451 __CTOR_LIST__ does not undo our behind-the-back change to .ctors. */
452 static func_ptr force_to_data[1] __attribute__ ((__unused__)) = { };
453 asm (CTORS_SECTION_ASM_OP);
454 STATIC func_ptr __CTOR_END__[1]
455 __attribute__((aligned(sizeof(func_ptr))))
456 = { (func_ptr) 0 };
457 #else
458 STATIC func_ptr __CTOR_END__[1]
459 __attribute__((section(".ctors"), aligned(sizeof(func_ptr))))
460 = { (func_ptr) 0 };
461 #endif
463 #ifdef DTOR_LIST_END
464 DTOR_LIST_END;
465 #elif defined(DTORS_SECTION_ASM_OP)
466 asm (DTORS_SECTION_ASM_OP);
467 STATIC func_ptr __DTOR_END__[1]
468 __attribute__ ((unused, aligned(sizeof(func_ptr))))
469 = { (func_ptr) 0 };
470 #else
471 STATIC func_ptr __DTOR_END__[1]
472 __attribute__((unused, section(".dtors"), aligned(sizeof(func_ptr))))
473 = { (func_ptr) 0 };
474 #endif
476 #ifdef EH_FRAME_SECTION_NAME
477 /* Terminate the frame unwind info section with a 4byte 0 as a sentinel;
478 this would be the 'length' field in a real FDE. */
479 # if __INT_MAX__ == 2147483647
480 typedef int int32;
481 # elif __LONG_MAX__ == 2147483647
482 typedef long int32;
483 # elif __SHRT_MAX__ == 2147483647
484 typedef short int32;
485 # else
486 # error "Missing a 4 byte integer"
487 # endif
488 STATIC EH_FRAME_SECTION_CONST int32 __FRAME_END__[]
489 __attribute__ ((unused, section(EH_FRAME_SECTION_NAME),
490 aligned(sizeof(int32))))
491 = { 0 };
492 #endif /* EH_FRAME_SECTION_NAME */
494 #ifdef JCR_SECTION_NAME
495 /* Null terminate the .jcr section array. */
496 STATIC void *__JCR_END__[1]
497 __attribute__ ((unused, section(JCR_SECTION_NAME),
498 aligned(sizeof(void *))))
499 = { 0 };
500 #endif /* JCR_SECTION_NAME */
502 #ifdef INIT_ARRAY_SECTION_ASM_OP
504 /* If we are using .init_array, there is nothing to do. */
506 #elif defined(INIT_SECTION_ASM_OP)
508 #ifdef OBJECT_FORMAT_ELF
509 static void __attribute__((used))
510 __do_global_ctors_aux (void)
512 func_ptr *p;
513 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
514 (*p) ();
517 /* Stick a call to __do_global_ctors_aux into the .init section. */
518 CRT_CALL_STATIC_FUNCTION (INIT_SECTION_ASM_OP, __do_global_ctors_aux)
519 #else /* OBJECT_FORMAT_ELF */
521 /* Stick the real initialization code, followed by a normal sort of
522 function epilogue at the very end of the .init section for this
523 entire root executable file or for this entire shared library file.
525 Note that we use some tricks here to get *just* the body and just
526 a function epilogue (but no function prologue) into the .init
527 section of the crtend.o file. Specifically, we switch to the .text
528 section, start to define a function, and then we switch to the .init
529 section just before the body code.
531 Earlier on, we put the corresponding function prologue into the .init
532 section of the crtbegin.o file (which will be linked in first).
534 Note that we want to invoke all constructors for C++ file-scope static-
535 storage objects AFTER any other possible initialization actions which
536 may be performed by the code in the .init section contributions made by
537 other libraries, etc. That's because those other initializations may
538 include setup operations for very primitive things (e.g. initializing
539 the state of the floating-point coprocessor, etc.) which should be done
540 before we start to execute any of the user's code. */
542 static void
543 __do_global_ctors_aux (void) /* prologue goes in .text section */
545 asm (INIT_SECTION_ASM_OP);
546 DO_GLOBAL_CTORS_BODY;
547 atexit (__do_global_dtors);
548 } /* epilogue and body go in .init section */
550 FORCE_CODE_SECTION_ALIGN
551 asm (TEXT_SECTION_ASM_OP);
553 #endif /* OBJECT_FORMAT_ELF */
555 #elif defined(HAS_INIT_SECTION) /* ! INIT_SECTION_ASM_OP */
557 extern void __do_global_ctors (void);
559 /* This case is used by the Irix 6 port, which supports named sections but
560 not an SVR4-style .init section. __do_global_ctors can be non-static
561 in this case because we protect it with -hidden_symbol. */
562 void
563 __do_global_ctors (void)
565 func_ptr *p;
566 #if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME)
567 __do_global_ctors_1();
568 #endif
569 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
570 (*p) ();
573 #else /* ! INIT_SECTION_ASM_OP && ! HAS_INIT_SECTION */
574 #error "What are you doing with crtstuff.c, then?"
575 #endif
577 #else /* ! CRT_BEGIN && ! CRT_END */
578 #error "One of CRT_BEGIN or CRT_END must be defined."
579 #endif