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