]> gcc.gnu.org Git - gcc.git/blame - gcc/crtstuff.c
*** empty log message ***
[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.
3
4 Written by Ron Guilmette (rfg@ncd.com) with help from Richard Stallman.
5
6Copyright (C) 1991 Free Software Foundation, Inc.
7
8This file is part of GNU CC.
9
10GNU CC is free software; you can redistribute it and/or modify
11it under the terms of the GNU General Public License as published by
12the Free Software Foundation; either version 2, or (at your option)
13any later version.
14
15GNU CC is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with GNU CC; see the file COPYING. If not, write to
22the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, 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 is actually supported.
45 The other formats probably need just alternative macro definitions.
46
47 This file must be compiled with gcc. */
48
49/* It is incorrect to include config.h here, because this file is being
50 compiled for the target, and hence definitions concerning only the host
51 do not apply. */
52
53#include "tm.h"
54
55#ifndef CTORS_SECTION_ASM_OP
f4ce55df 56#define CTORS_SECTION_ASM_OP ".section\t.ctors,\"a\",@progbits"
dc17e9e9
RS
57#endif
58#ifndef DTORS_SECTION_ASM_OP
f4ce55df 59#define DTORS_SECTION_ASM_OP ".section\t.dtors,\"a\",@progbits"
dc17e9e9
RS
60#endif
61
62#include "gbl-ctors.h"
63
64#ifndef ON_EXIT
65#define ON_EXIT(a, b)
66#endif
67
68#ifdef CRT_BEGIN
69
70#ifdef INIT_SECTION_ASM_OP
71
b335c2cc 72/* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o
b71a4aa9 73 and once in crtend.o). It must be declared static to avoid a link
b335c2cc
TW
74 error. Here, we define __do_global_ctors as an externally callable
75 function. It is externally callable so that __main can invoke it when
76 INVOKE__main is defined. This has the additional effect of forcing cc1
77 to switch to the .text section. */
93c3d169 78static void __do_global_ctors_aux ();
329d2160
RS
79void __do_global_ctors ()
80{
81#ifdef INVOKE__main /* If __main won't actually call __do_global_ctors
82 then it doesn't matter what's inside the function.
83 The inside of __do_global_ctors_aux is called
84 automatically in that case.
85 And the Alliant fx2800 linker crashes
86 on this reference. So prevent the crash. */
87 __do_global_ctors_aux ();
88#endif
89}
dc17e9e9
RS
90
91asm (INIT_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
92
62452fd9
RS
93/* On some svr4 systems, the .init section preamble code provided in
94 crti.o may do some evil things which we have to undo before we reach
95 the function prologue code for __do_global_ctors (directly below).
96 For such systems, define the macro INIT_SECTION_PREAMBLE to
97 expand into the code needed to undo the actions of the crti.o file. */
98
99#ifdef INIT_SECTION_PREAMBLE
100 INIT_SECTION_PREAMBLE;
101#endif
102
dc17e9e9
RS
103/* A routine to invoke all of the global constructors upon entry to the
104 program. We put this into the .init section (for systems that have
105 such a thing) so that we can properly perform the construction of
106 file-scope static-storage C++ objects within shared libraries. */
107
108static void
b335c2cc 109__do_global_ctors_aux () /* prologue goes in .init section */
dc17e9e9
RS
110{
111 asm (TEXT_SECTION_ASM_OP); /* don't put epilogue and body in .init */
112 DO_GLOBAL_CTORS_BODY;
113 ON_EXIT (__do_global_dtors, 0);
114}
115
116#endif /* defined(INIT_SECTION_ASM_OP) */
117
118/* Force cc1 to switch to .data section. */
119static func_ptr force_to_data[0] = { };
120
121/* The -1 is a flag to __do_global_[cd]tors
122 indicating that this table does not start with a count of elements. */
b335c2cc
TW
123#ifdef CTOR_LIST_BEGIN
124CTOR_LIST_BEGIN;
125#else
dc17e9e9
RS
126asm (CTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
127func_ptr __CTOR_LIST__[1] = { (func_ptr) (-1) };
b335c2cc 128#endif
dc17e9e9 129
b335c2cc
TW
130#ifdef DTOR_LIST_BEGIN
131DTOR_LIST_BEGIN;
132#else
dc17e9e9
RS
133asm (DTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
134func_ptr __DTOR_LIST__[1] = { (func_ptr) (-1) };
b335c2cc 135#endif
dc17e9e9
RS
136
137#endif /* defined(CRT_BEGIN) */
138
139#ifdef CRT_END
140
141#ifdef INIT_SECTION_ASM_OP
142
143/* A routine to invoke all of the global constructors upon entry to the
144 program. We put this into the .init section (for systems that have
145 such a thing) so that we can properly perform the construction of
146 file-scope static-storage C++ objects within shared libraries.
147
148 This must be virtually identical to the one above so that we can
149 insure that the function prologue from the one above works correctly
150 with the epilogue from this one. (They will both go into the .init
151 section as the first and last things (respectively) that the linker
152 will put in that section.)
153*/
154
155static void
b335c2cc 156__do_global_ctors_aux () /* prologue goes in .text section */
dc17e9e9
RS
157{
158 asm (INIT_SECTION_ASM_OP);
159 DO_GLOBAL_CTORS_BODY;
160 ON_EXIT (__do_global_dtors, 0);
161} /* epilogue and body go in .init section */
162
163#endif /* defined(INIT_SECTION_ASM_OP) */
164
165/* Force cc1 to switch to .data section. */
166static func_ptr force_to_data[0] = { };
167
b335c2cc
TW
168#ifdef CTOR_LIST_END
169CTOR_LIST_END;
170#else
dc17e9e9
RS
171asm (CTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
172func_ptr __CTOR_END__[1] = { (func_ptr) 0 };
b335c2cc 173#endif
dc17e9e9 174
b335c2cc
TW
175#ifdef DTOR_LIST_END
176DTOR_LIST_END;
177#else
dc17e9e9
RS
178asm (DTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
179func_ptr __DTOR_END__[1] = { (func_ptr) 0 };
b335c2cc 180#endif
dc17e9e9
RS
181
182#endif /* defined(CRT_END) */
This page took 0.063222 seconds and 5 git commands to generate.