1619e65d55c5b7ba946454f97f2b9a0b5ce6db77
[gcc.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, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000 Free Software Foundation, Inc.
5 Contributed by Ron Guilmette (rfg@monkeys.com).
6
7 This file is part of GNU CC.
8
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.
13
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24 /* As a special exception, if you link this library with files
25 compiled with GCC to produce an executable, this does not cause
26 the resulting executable to be covered by the GNU General Public License.
27 This exception does not however invalidate any other reasons why
28 the executable file might be covered by the GNU General Public License. */
29
30 /* This file is a bit like libgcc1.c/libgcc2.c in that it is compiled
31 multiple times and yields multiple .o files.
32
33 This file is useful on target machines where the object file format
34 supports multiple "user-defined" sections (e.g. COFF, ELF, ROSE). On
35 such systems, this file allows us to avoid running collect (or any
36 other such slow and painful kludge). Additionally, if the target
37 system supports a .init section, this file allows us to support the
38 linking of C++ code with a non-C++ main program.
39
40 Note that if INIT_SECTION_ASM_OP is defined in the tm.h file, then
41 this file *will* make use of the .init section. If that symbol is
42 not defined however, then the .init section will not be used.
43
44 Currently, only ELF and COFF are supported. It is likely however that
45 ROSE could also be supported, if someone was willing to do the work to
46 make whatever (small?) adaptations are needed. (Some work may be
47 needed on the ROSE assembler and linker also.)
48
49 This file must be compiled with gcc. */
50
51 /* It is incorrect to include config.h here, because this file is being
52 compiled for the target, and hence definitions concerning only the host
53 do not apply. */
54
55 #include "tm.h"
56 #include "tsystem.h"
57
58 #include "defaults.h"
59 #include "frame.h"
60
61 /* We do not want to add the weak attribute to the declarations of these
62 routines in frame.h because that will cause the definition of these
63 symbols to be weak as well.
64
65 This exposes a core issue, how to handle creating weak references vs
66 how to create weak definitions. Either we have to have the definition
67 of TARGET_WEAK_ATTRIBUTE be conditional in the shared header files or
68 have a second declaration if we want a function's references to be weak,
69 but not its definition.
70
71 Making TARGET_WEAK_ATTRIBUTE conditional seems like a good solution until
72 one thinks about scaling to larger problems -- ie, the condition under
73 which TARGET_WEAK_ATTRIBUTE is active will eventually get far too
74 complicated.
75
76 So, we take an approach similar to #pragma weak -- we have a second
77 declaration for functions that we want to have weak references.
78
79 Neither way is particularly good. */
80
81 /* References to __register_frame_info and __deregister_frame_info should
82 be weak in this file if at all possible. */
83 extern void __register_frame_info (void *, struct object *)
84 TARGET_ATTRIBUTE_WEAK;
85
86 extern void *__deregister_frame_info (void *)
87 TARGET_ATTRIBUTE_WEAK;
88
89 #ifndef OBJECT_FORMAT_MACHO
90
91 /* Provide default definitions for the pseudo-ops used to switch to the
92 .ctors and .dtors sections.
93
94 Note that we want to give these sections the SHF_WRITE attribute
95 because these sections will actually contain data (i.e. tables of
96 addresses of functions in the current root executable or shared library
97 file) and, in the case of a shared library, the relocatable addresses
98 will have to be properly resolved/relocated (and then written into) by
99 the dynamic linker when it actually attaches the given shared library
100 to the executing process. (Note that on SVR4, you may wish to use the
101 `-z text' option to the ELF linker, when building a shared library, as
102 an additional check that you are doing everything right. But if you do
103 use the `-z text' option when building a shared library, you will get
104 errors unless the .ctors and .dtors sections are marked as writable
105 via the SHF_WRITE attribute.) */
106
107 #ifndef CTORS_SECTION_ASM_OP
108 #define CTORS_SECTION_ASM_OP ".section\t.ctors,\"aw\""
109 #endif
110 #ifndef DTORS_SECTION_ASM_OP
111 #define DTORS_SECTION_ASM_OP ".section\t.dtors,\"aw\""
112 #endif
113
114 #ifdef OBJECT_FORMAT_ELF
115
116 /* Declare a pointer to void function type. */
117 typedef void (*func_ptr) (void);
118 #define STATIC static
119
120 #else /* OBJECT_FORMAT_ELF */
121
122 #include "gbl-ctors.h"
123
124 #define STATIC
125
126 #endif /* OBJECT_FORMAT_ELF */
127
128 #ifdef CRT_BEGIN
129
130 #ifdef INIT_SECTION_ASM_OP
131
132 #ifdef OBJECT_FORMAT_ELF
133
134 /* Declare the __dso_handle variable. It should have a unique value
135 in every shared-object; in a main program its value is zero. */
136
137 #ifdef CRTSTUFFS_O
138 void *__dso_handle = &__dso_handle;
139 #else
140 void *__dso_handle = 0;
141 #endif
142
143 /* The __cxa_finalize function may not be available so we use only a
144 weak declaration. */
145 extern void __cxa_finalize (void *) TARGET_ATTRIBUTE_WEAK;
146
147 /* Run all the global destructors on exit from the program. */
148
149 /* Some systems place the number of pointers in the first word of the
150 table. On SVR4 however, that word is -1. In all cases, the table is
151 null-terminated. On SVR4, we start from the beginning of the list and
152 invoke each per-compilation-unit destructor routine in order
153 until we find that null.
154
155 Note that this function MUST be static. There will be one of these
156 functions in each root executable and one in each shared library, but
157 although they all have the same code, each one is unique in that it
158 refers to one particular associated `__DTOR_LIST__' which belongs to the
159 same particular root executable or shared library file.
160
161 On some systems, this routine is run more than once from the .fini,
162 when exit is called recursively, so we arrange to remember where in
163 the list we left off processing, and we resume at that point,
164 should we be re-invoked. */
165
166 static char __EH_FRAME_BEGIN__[];
167 static func_ptr __DTOR_LIST__[];
168 static void
169 __do_global_dtors_aux (void)
170 {
171 static func_ptr *p = __DTOR_LIST__ + 1;
172 static int completed = 0;
173
174 if (completed)
175 return;
176
177 #ifdef CRTSTUFFS_O
178 if (__cxa_finalize)
179 __cxa_finalize (__dso_handle);
180 #endif
181
182 while (*p)
183 {
184 p++;
185 (*(p-1)) ();
186 }
187
188 #ifdef EH_FRAME_SECTION_ASM_OP
189 if (__deregister_frame_info)
190 __deregister_frame_info (__EH_FRAME_BEGIN__);
191 #endif
192 completed = 1;
193 }
194
195
196 /* Stick a call to __do_global_dtors_aux into the .fini section. */
197
198 static void __attribute__ ((__unused__))
199 fini_dummy (void)
200 {
201 asm (FINI_SECTION_ASM_OP);
202 __do_global_dtors_aux ();
203 #ifdef FORCE_FINI_SECTION_ALIGN
204 FORCE_FINI_SECTION_ALIGN;
205 #endif
206 asm (TEXT_SECTION_ASM_OP);
207 }
208
209 #ifdef EH_FRAME_SECTION_ASM_OP
210 /* Stick a call to __register_frame_info into the .init section. For some
211 reason calls with no arguments work more reliably in .init, so stick the
212 call in another function. */
213
214 static void
215 frame_dummy (void)
216 {
217 static struct object object;
218 if (__register_frame_info)
219 __register_frame_info (__EH_FRAME_BEGIN__, &object);
220 }
221
222 static void __attribute__ ((__unused__))
223 init_dummy (void)
224 {
225 asm (INIT_SECTION_ASM_OP);
226 frame_dummy ();
227 #ifdef FORCE_INIT_SECTION_ALIGN
228 FORCE_INIT_SECTION_ALIGN;
229 #endif
230 asm (TEXT_SECTION_ASM_OP);
231 }
232 #endif /* EH_FRAME_SECTION_ASM_OP */
233
234 #else /* OBJECT_FORMAT_ELF */
235
236 /* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o
237 and once in crtend.o). It must be declared static to avoid a link
238 error. Here, we define __do_global_ctors as an externally callable
239 function. It is externally callable so that __main can invoke it when
240 INVOKE__main is defined. This has the additional effect of forcing cc1
241 to switch to the .text section. */
242
243 static void __do_global_ctors_aux (void);
244 void
245 __do_global_ctors (void)
246 {
247 #ifdef INVOKE__main /* If __main won't actually call __do_global_ctors
248 then it doesn't matter what's inside the function.
249 The inside of __do_global_ctors_aux is called
250 automatically in that case.
251 And the Alliant fx2800 linker crashes
252 on this reference. So prevent the crash. */
253 __do_global_ctors_aux ();
254 #endif
255 }
256
257 asm (INIT_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
258
259 /* On some svr4 systems, the initial .init section preamble code provided in
260 crti.o may do something, such as bump the stack, which we have to
261 undo before we reach the function prologue code for __do_global_ctors
262 (directly below). For such systems, define the macro INIT_SECTION_PREAMBLE
263 to expand into the code needed to undo the actions of the crti.o file. */
264
265 #ifdef INIT_SECTION_PREAMBLE
266 INIT_SECTION_PREAMBLE;
267 #endif
268
269 /* A routine to invoke all of the global constructors upon entry to the
270 program. We put this into the .init section (for systems that have
271 such a thing) so that we can properly perform the construction of
272 file-scope static-storage C++ objects within shared libraries. */
273
274 static void
275 __do_global_ctors_aux (void) /* prologue goes in .init section */
276 {
277 #ifdef FORCE_INIT_SECTION_ALIGN
278 FORCE_INIT_SECTION_ALIGN; /* Explicit align before switch to .text */
279 #endif
280 asm (TEXT_SECTION_ASM_OP); /* don't put epilogue and body in .init */
281 DO_GLOBAL_CTORS_BODY;
282 atexit (__do_global_dtors);
283 }
284
285 #endif /* OBJECT_FORMAT_ELF */
286
287 #else /* defined(INIT_SECTION_ASM_OP) */
288
289 #ifdef HAS_INIT_SECTION
290 /* This case is used by the Irix 6 port, which supports named sections but
291 not an SVR4-style .fini section. __do_global_dtors can be non-static
292 in this case because we protect it with -hidden_symbol. */
293
294 static char __EH_FRAME_BEGIN__[];
295 static func_ptr __DTOR_LIST__[];
296 void
297 __do_global_dtors (void)
298 {
299 func_ptr *p;
300 for (p = __DTOR_LIST__ + 1; *p; p++)
301 (*p) ();
302
303 #ifdef EH_FRAME_SECTION_ASM_OP
304 if (__deregister_frame_info)
305 __deregister_frame_info (__EH_FRAME_BEGIN__);
306 #endif
307 }
308
309 #ifdef EH_FRAME_SECTION_ASM_OP
310 /* Define a function here to call __register_frame. crtend.o is linked in
311 after libgcc.a, and hence can't call libgcc.a functions directly. That
312 can lead to unresolved function references. */
313 void
314 __frame_dummy (void)
315 {
316 static struct object object;
317 if (__register_frame_info)
318 __register_frame_info (__EH_FRAME_BEGIN__, &object);
319 }
320 #endif
321 #endif
322
323 #endif /* defined(INIT_SECTION_ASM_OP) */
324
325 /* Force cc1 to switch to .data section. */
326 static func_ptr force_to_data[0] __attribute__ ((__unused__)) = { };
327
328 /* NOTE: In order to be able to support SVR4 shared libraries, we arrange
329 to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
330 __DTOR_END__ } per root executable and also one set of these symbols
331 per shared library. So in any given whole process image, we may have
332 multiple definitions of each of these symbols. In order to prevent
333 these definitions from conflicting with one another, and in order to
334 ensure that the proper lists are used for the initialization/finalization
335 of each individual shared library (respectively), we give these symbols
336 only internal (i.e. `static') linkage, and we also make it a point to
337 refer to only the __CTOR_END__ symbol in crtend.o and the __DTOR_LIST__
338 symbol in crtbegin.o, where they are defined. */
339
340 /* The -1 is a flag to __do_global_[cd]tors
341 indicating that this table does not start with a count of elements. */
342 #ifdef CTOR_LIST_BEGIN
343 CTOR_LIST_BEGIN;
344 #else
345 asm (CTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
346 STATIC func_ptr __CTOR_LIST__[1] __attribute__ ((__unused__))
347 = { (func_ptr) (-1) };
348 #endif
349
350 #ifdef DTOR_LIST_BEGIN
351 DTOR_LIST_BEGIN;
352 #else
353 asm (DTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
354 STATIC func_ptr __DTOR_LIST__[1] = { (func_ptr) (-1) };
355 #endif
356
357 #ifdef EH_FRAME_SECTION_ASM_OP
358 /* Stick a label at the beginning of the frame unwind info so we can register
359 and deregister it with the exception handling library code. */
360
361 asm (EH_FRAME_SECTION_ASM_OP);
362 #ifdef INIT_SECTION_ASM_OP
363 STATIC
364 #endif
365 char __EH_FRAME_BEGIN__[] = { };
366 #endif /* EH_FRAME_SECTION_ASM_OP */
367
368 #endif /* defined(CRT_BEGIN) */
369
370 #ifdef CRT_END
371
372 #ifdef INIT_SECTION_ASM_OP
373
374 #ifdef OBJECT_FORMAT_ELF
375
376 static func_ptr __CTOR_END__[];
377 static void
378 __do_global_ctors_aux (void)
379 {
380 func_ptr *p;
381 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
382 (*p) ();
383 }
384
385 /* Stick a call to __do_global_ctors_aux into the .init section. */
386
387 static void __attribute__ ((__unused__))
388 init_dummy (void)
389 {
390 asm (INIT_SECTION_ASM_OP);
391 __do_global_ctors_aux ();
392 #ifdef FORCE_INIT_SECTION_ALIGN
393 FORCE_INIT_SECTION_ALIGN;
394 #endif
395 asm (TEXT_SECTION_ASM_OP);
396
397 /* This is a kludge. The i386 GNU/Linux dynamic linker needs ___brk_addr,
398 __environ and atexit (). We have to make sure they are in the .dynsym
399 section. We accomplish it by making a dummy call here. This
400 code is never reached. */
401
402 #if defined(__linux__) && defined(__PIC__) && defined(__i386__)
403 {
404 extern void *___brk_addr;
405 extern char **__environ;
406
407 ___brk_addr = __environ;
408 atexit (0);
409 }
410 #endif
411 }
412
413 #else /* OBJECT_FORMAT_ELF */
414
415 /* Stick the real initialization code, followed by a normal sort of
416 function epilogue at the very end of the .init section for this
417 entire root executable file or for this entire shared library file.
418
419 Note that we use some tricks here to get *just* the body and just
420 a function epilogue (but no function prologue) into the .init
421 section of the crtend.o file. Specifically, we switch to the .text
422 section, start to define a function, and then we switch to the .init
423 section just before the body code.
424
425 Earlier on, we put the corresponding function prologue into the .init
426 section of the crtbegin.o file (which will be linked in first).
427
428 Note that we want to invoke all constructors for C++ file-scope static-
429 storage objects AFTER any other possible initialization actions which
430 may be performed by the code in the .init section contributions made by
431 other libraries, etc. That's because those other initializations may
432 include setup operations for very primitive things (e.g. initializing
433 the state of the floating-point coprocessor, etc.) which should be done
434 before we start to execute any of the user's code. */
435
436 static void
437 __do_global_ctors_aux (void) /* prologue goes in .text section */
438 {
439 asm (INIT_SECTION_ASM_OP);
440 DO_GLOBAL_CTORS_BODY;
441 atexit (__do_global_dtors);
442 } /* epilogue and body go in .init section */
443
444 #ifdef FORCE_INIT_SECTION_ALIGN
445 FORCE_INIT_SECTION_ALIGN;
446 #endif
447
448 asm (TEXT_SECTION_ASM_OP);
449
450 #endif /* OBJECT_FORMAT_ELF */
451
452 #else /* defined(INIT_SECTION_ASM_OP) */
453
454 #ifdef HAS_INIT_SECTION
455 /* This case is used by the Irix 6 port, which supports named sections but
456 not an SVR4-style .init section. __do_global_ctors can be non-static
457 in this case because we protect it with -hidden_symbol. */
458 static func_ptr __CTOR_END__[];
459 #ifdef EH_FRAME_SECTION_ASM_OP
460 extern void __frame_dummy (void);
461 #endif
462 void
463 __do_global_ctors (void)
464 {
465 func_ptr *p;
466 #ifdef EH_FRAME_SECTION_ASM_OP
467 __frame_dummy ();
468 #endif
469 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
470 (*p) ();
471 }
472 #endif
473
474 #endif /* defined(INIT_SECTION_ASM_OP) */
475
476 /* Force cc1 to switch to .data section. */
477 static func_ptr force_to_data[0] __attribute__ ((__unused__)) = { };
478
479 /* Put a word containing zero at the end of each of our two lists of function
480 addresses. Note that the words defined here go into the .ctors and .dtors
481 sections of the crtend.o file, and since that file is always linked in
482 last, these words naturally end up at the very ends of the two lists
483 contained in these two sections. */
484
485 #ifdef CTOR_LIST_END
486 CTOR_LIST_END;
487 #else
488 asm (CTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
489 STATIC func_ptr __CTOR_END__[1] = { (func_ptr) 0 };
490 #endif
491
492 #ifdef DTOR_LIST_END
493 DTOR_LIST_END;
494 #else
495 asm (DTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
496 STATIC func_ptr __DTOR_END__[1] __attribute__ ((__unused__))
497 = { (func_ptr) 0 };
498 #endif
499
500 #ifdef EH_FRAME_SECTION_ASM_OP
501 /* Terminate the frame unwind info section with a 4byte 0 as a sentinel;
502 this would be the 'length' field in a real FDE. */
503
504 typedef unsigned int ui32 __attribute__ ((mode (SI)));
505 asm (EH_FRAME_SECTION_ASM_OP);
506 STATIC ui32 __FRAME_END__[] __attribute__ ((__unused__)) = { 0 };
507 #endif /* EH_FRAME_SECTION */
508
509 #endif /* defined(CRT_END) */
510
511 #else /* OBJECT_FORMAT_MACHO */
512
513 /* For Mach-O format executables, we assume that the system's runtime is
514 smart enough to handle constructors and destructors, but doesn't have
515 an init section (if it can't even handle constructors/destructors
516 you should be using INVOKE__main, not crtstuff). All we need to do
517 is install/deinstall the frame information for exceptions. We do this
518 by putting a constructor in crtbegin.o and a destructor in crtend.o.
519
520 crtend.o also puts in the terminating zero in the frame information
521 segment. */
522
523 /* The crtstuff for other object formats use the symbol __EH_FRAME_BEGIN__
524 to figure out the start of the exception frame, but here we use
525 getsectbynamefromheader to find this value. Either method would work,
526 but this method avoids creating any global symbols, which seems
527 cleaner. */
528
529 #include <mach-o/ldsyms.h>
530 extern const struct section *
531 getsectbynamefromheader (const struct mach_header *,
532 const char *, const char *);
533
534 #ifdef CRT_BEGIN
535
536 static void __reg_frame_ctor (void) __attribute__ ((constructor));
537
538 static void
539 __reg_frame_ctor (void)
540 {
541 static struct object object;
542 const struct section *eh_frame;
543
544 eh_frame = getsectbynamefromheader (&_mh_execute_header,
545 "__TEXT", "__eh_frame");
546 __register_frame_info ((void *) eh_frame->addr, &object);
547 }
548
549 #endif /* CRT_BEGIN */
550
551 #ifdef CRT_END
552
553 static void __dereg_frame_dtor (void) __attribute__ ((destructor));
554
555 static void
556 __dereg_frame_dtor (void)
557 {
558 const struct section *eh_frame;
559
560 eh_frame = getsectbynamefromheader (&_mh_execute_header,
561 "__TEXT", "__eh_frame");
562 __deregister_frame_info ((void *) eh_frame->addr);
563 }
564
565 /* Terminate the frame section with a final zero. */
566
567 /* Force cc1 to switch to .data section. */
568 static void * force_to_data[0] __attribute__ ((__unused__)) = { };
569
570 typedef unsigned int ui32 __attribute__ ((mode (SI)));
571 asm (EH_FRAME_SECTION_ASM_OP);
572 static ui32 __FRAME_END__[] __attribute__ ((__unused__)) = { 0 };
573
574 #endif /* CRT_END */
575
576 #endif /* OBJECT_FORMAT_MACHO */
577