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