* class.c (check_bitfield_decl): New function, split out from
[official-gcc.git] / gcc / crtstuff.c
bloba17433a0c43eb58d7b4cecfbb4ebb7efc517ca3a
1 /* Specialized bits of code needed to support construction and
2 destruction of file-scope objects in C++ code.
3 Copyright (C) 1991, 1994-1999 Free Software Foundation, Inc.
4 Contributed by Ron Guilmette (rfg@monkeys.com).
6 This file is part of GNU CC.
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.
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.
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. */
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. */
29 /* This file is a bit like libgcc1.c/libgcc2.c in that it is compiled
30 multiple times and yields multiple .o files.
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.
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.
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.)
48 This file must be compiled with gcc. */
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. */
54 #include "tm.h"
55 #include "defaults.h"
56 #include <stddef.h>
57 #include "frame.h"
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.
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.
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.
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.
77 Neither way is particularly good. */
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;
84 extern void *__deregister_frame_info (void *)
85 TARGET_ATTRIBUTE_WEAK;
87 #ifndef OBJECT_FORMAT_MACHO
89 /* Provide default definitions for the pseudo-ops used to switch to the
90 .ctors and .dtors sections.
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.) */
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
112 #ifdef OBJECT_FORMAT_ELF
114 /* Declare a pointer to void function type. */
115 typedef void (*func_ptr) (void);
116 #define STATIC static
118 #else /* OBJECT_FORMAT_ELF */
120 #include "gbl-ctors.h"
122 #define STATIC
124 #endif /* OBJECT_FORMAT_ELF */
126 #ifdef CRT_BEGIN
128 #ifdef INIT_SECTION_ASM_OP
130 #ifdef OBJECT_FORMAT_ELF
132 /* Run all the global destructors on exit from the program. */
134 /* Some systems place the number of pointers in the first word of the
135 table. On SVR4 however, that word is -1. In all cases, the table is
136 null-terminated. On SVR4, we start from the beginning of the list and
137 invoke each per-compilation-unit destructor routine in order
138 until we find that null.
140 Note that this function MUST be static. There will be one of these
141 functions in each root executable and one in each shared library, but
142 although they all have the same code, each one is unique in that it
143 refers to one particular associated `__DTOR_LIST__' which belongs to the
144 same particular root executable or shared library file.
146 On some systems, this routine is run more than once from the .fini,
147 when exit is called recursively, so we arrange to remember where in
148 the list we left off processing, and we resume at that point,
149 should we be re-invoked. */
151 static char __EH_FRAME_BEGIN__[];
152 static func_ptr __DTOR_LIST__[];
153 static void
154 __do_global_dtors_aux (void)
156 static func_ptr *p = __DTOR_LIST__ + 1;
157 static int completed = 0;
159 if (completed)
160 return;
162 while (*p)
164 p++;
165 (*(p-1)) ();
168 #ifdef EH_FRAME_SECTION_ASM_OP
169 if (__deregister_frame_info)
170 __deregister_frame_info (__EH_FRAME_BEGIN__);
171 #endif
172 completed = 1;
176 /* Stick a call to __do_global_dtors_aux into the .fini section. */
178 static void __attribute__ ((__unused__))
179 fini_dummy (void)
181 asm (FINI_SECTION_ASM_OP);
182 __do_global_dtors_aux ();
183 #ifdef FORCE_FINI_SECTION_ALIGN
184 FORCE_FINI_SECTION_ALIGN;
185 #endif
186 asm (TEXT_SECTION_ASM_OP);
189 #ifdef EH_FRAME_SECTION_ASM_OP
190 /* Stick a call to __register_frame_info into the .init section. For some
191 reason calls with no arguments work more reliably in .init, so stick the
192 call in another function. */
194 static void
195 frame_dummy (void)
197 static struct object object;
198 if (__register_frame_info)
199 __register_frame_info (__EH_FRAME_BEGIN__, &object);
202 static void __attribute__ ((__unused__))
203 init_dummy (void)
205 asm (INIT_SECTION_ASM_OP);
206 frame_dummy ();
207 #ifdef FORCE_INIT_SECTION_ALIGN
208 FORCE_INIT_SECTION_ALIGN;
209 #endif
210 asm (TEXT_SECTION_ASM_OP);
212 #endif /* EH_FRAME_SECTION_ASM_OP */
214 #else /* OBJECT_FORMAT_ELF */
216 /* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o
217 and once in crtend.o). It must be declared static to avoid a link
218 error. Here, we define __do_global_ctors as an externally callable
219 function. It is externally callable so that __main can invoke it when
220 INVOKE__main is defined. This has the additional effect of forcing cc1
221 to switch to the .text section. */
223 static void __do_global_ctors_aux ();
224 void
225 __do_global_ctors (void)
227 #ifdef INVOKE__main /* If __main won't actually call __do_global_ctors
228 then it doesn't matter what's inside the function.
229 The inside of __do_global_ctors_aux is called
230 automatically in that case.
231 And the Alliant fx2800 linker crashes
232 on this reference. So prevent the crash. */
233 __do_global_ctors_aux ();
234 #endif
237 asm (INIT_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
239 /* On some svr4 systems, the initial .init section preamble code provided in
240 crti.o may do something, such as bump the stack, which we have to
241 undo before we reach the function prologue code for __do_global_ctors
242 (directly below). For such systems, define the macro INIT_SECTION_PREAMBLE
243 to expand into the code needed to undo the actions of the crti.o file. */
245 #ifdef INIT_SECTION_PREAMBLE
246 INIT_SECTION_PREAMBLE;
247 #endif
249 /* A routine to invoke all of the global constructors upon entry to the
250 program. We put this into the .init section (for systems that have
251 such a thing) so that we can properly perform the construction of
252 file-scope static-storage C++ objects within shared libraries. */
254 static void
255 __do_global_ctors_aux (void) /* prologue goes in .init section */
257 #ifdef FORCE_INIT_SECTION_ALIGN
258 FORCE_INIT_SECTION_ALIGN; /* Explicit align before switch to .text */
259 #endif
260 asm (TEXT_SECTION_ASM_OP); /* don't put epilogue and body in .init */
261 DO_GLOBAL_CTORS_BODY;
262 atexit (__do_global_dtors);
265 #endif /* OBJECT_FORMAT_ELF */
267 #else /* defined(INIT_SECTION_ASM_OP) */
269 #ifdef HAS_INIT_SECTION
270 /* This case is used by the Irix 6 port, which supports named sections but
271 not an SVR4-style .fini section. __do_global_dtors can be non-static
272 in this case because we protect it with -hidden_symbol. */
274 static char __EH_FRAME_BEGIN__[];
275 static func_ptr __DTOR_LIST__[];
276 void
277 __do_global_dtors (void)
279 func_ptr *p;
280 for (p = __DTOR_LIST__ + 1; *p; p++)
281 (*p) ();
283 #ifdef EH_FRAME_SECTION_ASM_OP
284 if (__deregister_frame_info)
285 __deregister_frame_info (__EH_FRAME_BEGIN__);
286 #endif
289 #ifdef EH_FRAME_SECTION_ASM_OP
290 /* Define a function here to call __register_frame. crtend.o is linked in
291 after libgcc.a, and hence can't call libgcc.a functions directly. That
292 can lead to unresolved function references. */
293 void
294 __frame_dummy (void)
296 static struct object object;
297 if (__register_frame_info)
298 __register_frame_info (__EH_FRAME_BEGIN__, &object);
300 #endif
301 #endif
303 #endif /* defined(INIT_SECTION_ASM_OP) */
305 /* Force cc1 to switch to .data section. */
306 static func_ptr force_to_data[0] __attribute__ ((__unused__)) = { };
308 /* NOTE: In order to be able to support SVR4 shared libraries, we arrange
309 to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
310 __DTOR_END__ } per root executable and also one set of these symbols
311 per shared library. So in any given whole process image, we may have
312 multiple definitions of each of these symbols. In order to prevent
313 these definitions from conflicting with one another, and in order to
314 ensure that the proper lists are used for the initialization/finalization
315 of each individual shared library (respectively), we give these symbols
316 only internal (i.e. `static') linkage, and we also make it a point to
317 refer to only the __CTOR_END__ symbol in crtend.o and the __DTOR_LIST__
318 symbol in crtbegin.o, where they are defined. */
320 /* The -1 is a flag to __do_global_[cd]tors
321 indicating that this table does not start with a count of elements. */
322 #ifdef CTOR_LIST_BEGIN
323 CTOR_LIST_BEGIN;
324 #else
325 asm (CTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
326 STATIC func_ptr __CTOR_LIST__[1] __attribute__ ((__unused__))
327 = { (func_ptr) (-1) };
328 #endif
330 #ifdef DTOR_LIST_BEGIN
331 DTOR_LIST_BEGIN;
332 #else
333 asm (DTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
334 STATIC func_ptr __DTOR_LIST__[1] = { (func_ptr) (-1) };
335 #endif
337 #ifdef EH_FRAME_SECTION_ASM_OP
338 /* Stick a label at the beginning of the frame unwind info so we can register
339 and deregister it with the exception handling library code. */
341 asm (EH_FRAME_SECTION_ASM_OP);
342 #ifdef INIT_SECTION_ASM_OP
343 STATIC
344 #endif
345 char __EH_FRAME_BEGIN__[] = { };
346 #endif /* EH_FRAME_SECTION_ASM_OP */
348 #endif /* defined(CRT_BEGIN) */
350 #ifdef CRT_END
352 #ifdef INIT_SECTION_ASM_OP
354 #ifdef OBJECT_FORMAT_ELF
356 static func_ptr __CTOR_END__[];
357 static void
358 __do_global_ctors_aux (void)
360 func_ptr *p;
361 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
362 (*p) ();
365 /* Stick a call to __do_global_ctors_aux into the .init section. */
367 static void __attribute__ ((__unused__))
368 init_dummy (void)
370 asm (INIT_SECTION_ASM_OP);
371 __do_global_ctors_aux ();
372 #ifdef FORCE_INIT_SECTION_ALIGN
373 FORCE_INIT_SECTION_ALIGN;
374 #endif
375 asm (TEXT_SECTION_ASM_OP);
377 /* This is a kludge. The i386 GNU/Linux dynamic linker needs ___brk_addr,
378 __environ and atexit (). We have to make sure they are in the .dynsym
379 section. We accomplish it by making a dummy call here. This
380 code is never reached. */
382 #if defined(__linux__) && defined(__PIC__) && defined(__i386__)
384 extern void *___brk_addr;
385 extern char **__environ;
387 ___brk_addr = __environ;
388 atexit ();
390 #endif
393 #else /* OBJECT_FORMAT_ELF */
395 /* Stick the real initialization code, followed by a normal sort of
396 function epilogue at the very end of the .init section for this
397 entire root executable file or for this entire shared library file.
399 Note that we use some tricks here to get *just* the body and just
400 a function epilogue (but no function prologue) into the .init
401 section of the crtend.o file. Specifically, we switch to the .text
402 section, start to define a function, and then we switch to the .init
403 section just before the body code.
405 Earlier on, we put the corresponding function prologue into the .init
406 section of the crtbegin.o file (which will be linked in first).
408 Note that we want to invoke all constructors for C++ file-scope static-
409 storage objects AFTER any other possible initialization actions which
410 may be performed by the code in the .init section contributions made by
411 other libraries, etc. That's because those other initializations may
412 include setup operations for very primitive things (e.g. initializing
413 the state of the floating-point coprocessor, etc.) which should be done
414 before we start to execute any of the user's code. */
416 static void
417 __do_global_ctors_aux (void) /* prologue goes in .text section */
419 asm (INIT_SECTION_ASM_OP);
420 DO_GLOBAL_CTORS_BODY;
421 atexit (__do_global_dtors);
422 } /* epilogue and body go in .init section */
424 #ifdef FORCE_INIT_SECTION_ALIGN
425 FORCE_INIT_SECTION_ALIGN;
426 #endif
428 asm (TEXT_SECTION_ASM_OP);
430 #endif /* OBJECT_FORMAT_ELF */
432 #else /* defined(INIT_SECTION_ASM_OP) */
434 #ifdef HAS_INIT_SECTION
435 /* This case is used by the Irix 6 port, which supports named sections but
436 not an SVR4-style .init section. __do_global_ctors can be non-static
437 in this case because we protect it with -hidden_symbol. */
438 static func_ptr __CTOR_END__[];
439 #ifdef EH_FRAME_SECTION_ASM_OP
440 extern void __frame_dummy (void);
441 #endif
442 void
443 __do_global_ctors (void)
445 func_ptr *p;
446 #ifdef EH_FRAME_SECTION_ASM_OP
447 __frame_dummy ();
448 #endif
449 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
450 (*p) ();
452 #endif
454 #endif /* defined(INIT_SECTION_ASM_OP) */
456 /* Force cc1 to switch to .data section. */
457 static func_ptr force_to_data[0] __attribute__ ((__unused__)) = { };
459 /* Put a word containing zero at the end of each of our two lists of function
460 addresses. Note that the words defined here go into the .ctors and .dtors
461 sections of the crtend.o file, and since that file is always linked in
462 last, these words naturally end up at the very ends of the two lists
463 contained in these two sections. */
465 #ifdef CTOR_LIST_END
466 CTOR_LIST_END;
467 #else
468 asm (CTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
469 STATIC func_ptr __CTOR_END__[1] = { (func_ptr) 0 };
470 #endif
472 #ifdef DTOR_LIST_END
473 DTOR_LIST_END;
474 #else
475 asm (DTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
476 STATIC func_ptr __DTOR_END__[1] __attribute__ ((__unused__))
477 = { (func_ptr) 0 };
478 #endif
480 #ifdef EH_FRAME_SECTION_ASM_OP
481 /* Terminate the frame unwind info section with a 4byte 0 as a sentinel;
482 this would be the 'length' field in a real FDE. */
484 typedef unsigned int ui32 __attribute__ ((mode (SI)));
485 asm (EH_FRAME_SECTION_ASM_OP);
486 STATIC ui32 __FRAME_END__[] __attribute__ ((__unused__)) = { 0 };
487 #endif /* EH_FRAME_SECTION */
489 #endif /* defined(CRT_END) */
491 #else /* OBJECT_FORMAT_MACHO */
493 /* For Mach-O format executables, we assume that the system's runtime is
494 smart enough to handle constructors and destructors, but doesn't have
495 an init section (if it can't even handle constructors/destructors
496 you should be using INVOKE__main, not crtstuff). All we need to do
497 is install/deinstall the frame information for exceptions. We do this
498 by putting a constructor in crtbegin.o and a destructor in crtend.o.
500 crtend.o also puts in the terminating zero in the frame information
501 segment. */
503 /* The crtstuff for other object formats use the symbol __EH_FRAME_BEGIN__
504 to figure out the start of the exception frame, but here we use
505 getsectbynamefromheader to find this value. Either method would work,
506 but this method avoids creating any global symbols, which seems
507 cleaner. */
509 #include <mach-o/ldsyms.h>
510 extern const struct section *
511 getsectbynamefromheader (const struct mach_header *,
512 const char *, const char *);
514 #ifdef CRT_BEGIN
516 static void __reg_frame_ctor () __attribute__ ((constructor));
518 static void
519 __reg_frame_ctor (void)
521 static struct object object;
522 const struct section *eh_frame;
524 eh_frame = getsectbynamefromheader (&_mh_execute_header,
525 "__TEXT", "__eh_frame");
526 __register_frame_info ((void *) eh_frame->addr, &object);
529 #endif /* CRT_BEGIN */
531 #ifdef CRT_END
533 static void __dereg_frame_dtor () __attribute__ ((destructor));
535 static
536 void
537 __dereg_frame_dtor (void)
539 const struct section *eh_frame;
541 eh_frame = getsectbynamefromheader (&_mh_execute_header,
542 "__TEXT", "__eh_frame");
543 __deregister_frame_info ((void *) eh_frame->addr);
546 /* Terminate the frame section with a final zero. */
548 /* Force cc1 to switch to .data section. */
549 static void * force_to_data[0] __attribute__ ((__unused__)) = { };
551 typedef unsigned int ui32 __attribute__ ((mode (SI)));
552 asm (EH_FRAME_SECTION_ASM_OP);
553 static ui32 __FRAME_END__[] __attribute__ ((__unused__)) = { 0 };
555 #endif /* CRT_END */
557 #endif /* OBJECT_FORMAT_MACHO */