]>
Commit | Line | Data |
---|---|---|
9d13f5c4 | 1 | /* Specialized bits of code needed to support construction and |
2 | destruction of file-scope objects in C++ code. | |
31dcf09a | 3 | Copyright (C) 1991, 1994, 1995, 1996 Free Software Foundation, Inc. |
4 | Contributed by Ron Guilmette (rfg@monkeys.com). | |
9d13f5c4 | 5 | |
6 | This file is part of GNU CC. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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 | |
8d62a21c | 20 | the Free Software Foundation, 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. */ | |
9d13f5c4 | 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 | ||
5dd605c6 | 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.) | |
9d13f5c4 | 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" | |
55 | ||
b7c87ff2 | 56 | /* Provide default definitions for the pseudo-ops used to switch to the |
57 | .ctors and .dtors sections. | |
58 | ||
59 | Note that we want to give these sections the SHF_WRITE attribute | |
60 | because these sections will actually contain data (i.e. tables of | |
61 | addresses of functions in the current root executable or shared library | |
62 | file) and, in the case of a shared library, the relocatable addresses | |
63 | will have to be properly resolved/relocated (and then written into) by | |
64 | the dynamic linker when it actually attaches the given shared library | |
65 | to the executing process. (Note that on SVR4, you may wish to use the | |
66 | `-z text' option to the ELF linker, when building a shared library, as | |
67 | an additional check that you are doing everything right. But if you do | |
68 | use the `-z text' option when building a shared library, you will get | |
69 | errors unless the .ctors and .dtors sections are marked as writable | |
70 | via the SHF_WRITE attribute.) */ | |
71 | ||
9d13f5c4 | 72 | #ifndef CTORS_SECTION_ASM_OP |
b7c87ff2 | 73 | #define CTORS_SECTION_ASM_OP ".section\t.ctors,\"aw\"" |
9d13f5c4 | 74 | #endif |
75 | #ifndef DTORS_SECTION_ASM_OP | |
b7c87ff2 | 76 | #define DTORS_SECTION_ASM_OP ".section\t.dtors,\"aw\"" |
9d13f5c4 | 77 | #endif |
78 | ||
b7c87ff2 | 79 | #ifdef OBJECT_FORMAT_ELF |
80 | ||
81 | /* Declare a pointer to void function type. */ | |
82 | typedef void (*func_ptr) (void); | |
83 | #define STATIC static | |
84 | ||
85 | #else /* OBJECT_FORMAT_ELF */ | |
86 | ||
9d13f5c4 | 87 | #include "gbl-ctors.h" |
88 | ||
89 | #ifndef ON_EXIT | |
90 | #define ON_EXIT(a, b) | |
91 | #endif | |
b7c87ff2 | 92 | #define STATIC |
93 | ||
94 | #endif /* OBJECT_FORMAT_ELF */ | |
9d13f5c4 | 95 | |
96 | #ifdef CRT_BEGIN | |
97 | ||
98 | #ifdef INIT_SECTION_ASM_OP | |
99 | ||
b7c87ff2 | 100 | #ifdef OBJECT_FORMAT_ELF |
101 | ||
102 | /* Run all the global destructors on exit from the program. */ | |
103 | ||
104 | /* Some systems place the number of pointers in the first word of the | |
105 | table. On SVR4 however, that word is -1. In all cases, the table is | |
106 | null-terminated. On SVR4, we start from the beginning of the list and | |
107 | invoke each per-compilation-unit destructor routine in order | |
108 | until we find that null. | |
109 | ||
110 | Note that this function MUST be static. There will be one of these | |
111 | functions in each root executable and one in each shared library, but | |
112 | although they all have the same code, each one is unique in that it | |
113 | refers to one particular associated `__DTOR_LIST__' which belongs to the | |
114 | same particular root executable or shared library file. */ | |
115 | ||
116 | static func_ptr __DTOR_LIST__[]; | |
117 | static void | |
118 | __do_global_dtors_aux () | |
119 | { | |
120 | func_ptr *p; | |
121 | for (p = __DTOR_LIST__ + 1; *p; p++) | |
122 | (*p) (); | |
123 | } | |
124 | ||
125 | /* Stick a call to __do_global_dtors_aux into the .fini section. */ | |
a92771b8 | 126 | |
b7c87ff2 | 127 | static void |
128 | fini_dummy () | |
129 | { | |
130 | asm (FINI_SECTION_ASM_OP); | |
131 | __do_global_dtors_aux (); | |
132 | #ifdef FORCE_FINI_SECTION_ALIGN | |
133 | FORCE_FINI_SECTION_ALIGN; | |
134 | #endif | |
135 | asm (TEXT_SECTION_ASM_OP); | |
136 | } | |
137 | ||
138 | #else /* OBJECT_FORMAT_ELF */ | |
139 | ||
b13ae905 | 140 | /* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o |
d8fa92ab | 141 | and once in crtend.o). It must be declared static to avoid a link |
b13ae905 | 142 | error. Here, we define __do_global_ctors as an externally callable |
143 | function. It is externally callable so that __main can invoke it when | |
144 | INVOKE__main is defined. This has the additional effect of forcing cc1 | |
145 | to switch to the .text section. */ | |
a92771b8 | 146 | |
ee45242a | 147 | static void __do_global_ctors_aux (); |
84b64a19 | 148 | void __do_global_ctors () |
149 | { | |
150 | #ifdef INVOKE__main /* If __main won't actually call __do_global_ctors | |
151 | then it doesn't matter what's inside the function. | |
152 | The inside of __do_global_ctors_aux is called | |
153 | automatically in that case. | |
154 | And the Alliant fx2800 linker crashes | |
155 | on this reference. So prevent the crash. */ | |
156 | __do_global_ctors_aux (); | |
157 | #endif | |
158 | } | |
9d13f5c4 | 159 | |
160 | asm (INIT_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */ | |
161 | ||
3c67709e | 162 | /* On some svr4 systems, the initial .init section preamble code provided in |
163 | crti.o may do something, such as bump the stack, which we have to | |
164 | undo before we reach the function prologue code for __do_global_ctors | |
165 | (directly below). For such systems, define the macro INIT_SECTION_PREAMBLE | |
a92771b8 | 166 | to expand into the code needed to undo the actions of the crti.o file. */ |
3c67709e | 167 | |
168 | #ifdef INIT_SECTION_PREAMBLE | |
169 | INIT_SECTION_PREAMBLE; | |
170 | #endif | |
171 | ||
9d13f5c4 | 172 | /* A routine to invoke all of the global constructors upon entry to the |
173 | program. We put this into the .init section (for systems that have | |
174 | such a thing) so that we can properly perform the construction of | |
175 | file-scope static-storage C++ objects within shared libraries. */ | |
176 | ||
177 | static void | |
b13ae905 | 178 | __do_global_ctors_aux () /* prologue goes in .init section */ |
9d13f5c4 | 179 | { |
635d52b7 | 180 | #ifdef FORCE_INIT_SECTION_ALIGN |
181 | FORCE_INIT_SECTION_ALIGN; /* Explicit align before switch to .text */ | |
182 | #endif | |
9d13f5c4 | 183 | asm (TEXT_SECTION_ASM_OP); /* don't put epilogue and body in .init */ |
184 | DO_GLOBAL_CTORS_BODY; | |
185 | ON_EXIT (__do_global_dtors, 0); | |
186 | } | |
187 | ||
b7c87ff2 | 188 | #endif /* OBJECT_FORMAT_ELF */ |
8313a782 | 189 | |
190 | #else /* defined(INIT_SECTION_ASM_OP) */ | |
191 | ||
192 | /* This case is used by the Irix 6 port, which supports named sections but | |
193 | not an SVR4-style .fini section. __do_global_dtors can be non-static | |
194 | in this case because the -fini switch to ld binds strongly. */ | |
195 | static func_ptr __DTOR_LIST__[]; | |
196 | void | |
197 | __do_global_dtors () | |
198 | { | |
199 | func_ptr *p; | |
200 | for (p = __DTOR_LIST__ + 1; *p; p++) | |
201 | (*p) (); | |
202 | } | |
203 | ||
9d13f5c4 | 204 | #endif /* defined(INIT_SECTION_ASM_OP) */ |
205 | ||
206 | /* Force cc1 to switch to .data section. */ | |
207 | static func_ptr force_to_data[0] = { }; | |
208 | ||
b7c87ff2 | 209 | /* NOTE: In order to be able to support SVR4 shared libraries, we arrange |
210 | to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__, | |
211 | __DTOR_END__ } per root executable and also one set of these symbols | |
212 | per shared library. So in any given whole process image, we may have | |
213 | multiple definitions of each of these symbols. In order to prevent | |
214 | these definitions from conflicting with one another, and in order to | |
215 | ensure that the proper lists are used for the initialization/finalization | |
216 | of each individual shared library (respectively), we give these symbols | |
217 | only internal (i.e. `static') linkage, and we also make it a point to | |
218 | refer to only the __CTOR_END__ symbol in crtend.o and the __DTOR_LIST__ | |
219 | symbol in crtbegin.o, where they are defined. */ | |
220 | ||
9d13f5c4 | 221 | /* The -1 is a flag to __do_global_[cd]tors |
222 | indicating that this table does not start with a count of elements. */ | |
b13ae905 | 223 | #ifdef CTOR_LIST_BEGIN |
224 | CTOR_LIST_BEGIN; | |
225 | #else | |
9d13f5c4 | 226 | asm (CTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */ |
b7c87ff2 | 227 | STATIC func_ptr __CTOR_LIST__[1] = { (func_ptr) (-1) }; |
b13ae905 | 228 | #endif |
9d13f5c4 | 229 | |
b13ae905 | 230 | #ifdef DTOR_LIST_BEGIN |
231 | DTOR_LIST_BEGIN; | |
232 | #else | |
9d13f5c4 | 233 | asm (DTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */ |
b7c87ff2 | 234 | STATIC func_ptr __DTOR_LIST__[1] = { (func_ptr) (-1) }; |
b13ae905 | 235 | #endif |
9d13f5c4 | 236 | |
237 | #endif /* defined(CRT_BEGIN) */ | |
238 | ||
239 | #ifdef CRT_END | |
240 | ||
241 | #ifdef INIT_SECTION_ASM_OP | |
242 | ||
b7c87ff2 | 243 | #ifdef OBJECT_FORMAT_ELF |
9d13f5c4 | 244 | |
b7c87ff2 | 245 | static func_ptr __CTOR_END__[]; |
246 | static void | |
247 | __do_global_ctors_aux () | |
248 | { | |
249 | func_ptr *p; | |
250 | for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--) | |
251 | (*p) (); | |
252 | } | |
253 | ||
254 | /* Stick a call to __do_global_ctors_aux into the .init section. */ | |
a92771b8 | 255 | |
b7c87ff2 | 256 | static void |
257 | init_dummy () | |
258 | { | |
259 | asm (INIT_SECTION_ASM_OP); | |
260 | __do_global_ctors_aux (); | |
261 | #ifdef FORCE_INIT_SECTION_ALIGN | |
262 | FORCE_INIT_SECTION_ALIGN; | |
263 | #endif | |
264 | asm (TEXT_SECTION_ASM_OP); | |
baf8c510 | 265 | |
31dcf09a | 266 | /* This is a kludge. The i386 Linux dynamic linker needs ___brk_addr, |
267 | __environ and atexit (). We have to make sure they are in the .dynsym | |
268 | section. We accomplish it by making a dummy call here. This | |
a92771b8 | 269 | code is never reached. */ |
baf8c510 | 270 | |
31dcf09a | 271 | #if defined(__linux__) && defined(__PIC__) && defined(__i386__) |
baf8c510 | 272 | { |
273 | extern void *___brk_addr; | |
274 | extern char **__environ; | |
275 | ||
276 | ___brk_addr = __environ; | |
277 | atexit (); | |
278 | } | |
279 | #endif | |
b7c87ff2 | 280 | } |
281 | ||
282 | #else /* OBJECT_FORMAT_ELF */ | |
283 | ||
284 | /* Stick the real initialization code, followed by a normal sort of | |
285 | function epilogue at the very end of the .init section for this | |
286 | entire root executable file or for this entire shared library file. | |
287 | ||
288 | Note that we use some tricks here to get *just* the body and just | |
289 | a function epilogue (but no function prologue) into the .init | |
c3418f42 | 290 | section of the crtend.o file. Specifically, we switch to the .text |
b7c87ff2 | 291 | section, start to define a function, and then we switch to the .init |
292 | section just before the body code. | |
293 | ||
294 | Earlier on, we put the corresponding function prologue into the .init | |
295 | section of the crtbegin.o file (which will be linked in first). | |
296 | ||
297 | Note that we want to invoke all constructors for C++ file-scope static- | |
298 | storage objects AFTER any other possible initialization actions which | |
299 | may be performed by the code in the .init section contributions made by | |
300 | other libraries, etc. That's because those other initializations may | |
301 | include setup operations for very primitive things (e.g. initializing | |
302 | the state of the floating-point coprocessor, etc.) which should be done | |
a92771b8 | 303 | before we start to execute any of the user's code. */ |
9d13f5c4 | 304 | |
305 | static void | |
b13ae905 | 306 | __do_global_ctors_aux () /* prologue goes in .text section */ |
9d13f5c4 | 307 | { |
308 | asm (INIT_SECTION_ASM_OP); | |
309 | DO_GLOBAL_CTORS_BODY; | |
310 | ON_EXIT (__do_global_dtors, 0); | |
311 | } /* epilogue and body go in .init section */ | |
312 | ||
b7c87ff2 | 313 | #endif /* OBJECT_FORMAT_ELF */ |
314 | ||
8313a782 | 315 | #else /* defined(INIT_SECTION_ASM_OP) */ |
316 | ||
317 | /* This case is used by the Irix 6 port, which supports named sections but | |
318 | not an SVR4-style .init section. __do_global_ctors can be non-static | |
319 | in this case because the -init switch to ld binds strongly. */ | |
320 | static func_ptr __CTOR_END__[]; | |
321 | void | |
322 | __do_global_ctors () | |
323 | { | |
324 | func_ptr *p; | |
325 | for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--) | |
326 | (*p) (); | |
327 | } | |
328 | ||
9d13f5c4 | 329 | #endif /* defined(INIT_SECTION_ASM_OP) */ |
330 | ||
331 | /* Force cc1 to switch to .data section. */ | |
332 | static func_ptr force_to_data[0] = { }; | |
333 | ||
b7c87ff2 | 334 | /* Put a word containing zero at the end of each of our two lists of function |
335 | addresses. Note that the words defined here go into the .ctors and .dtors | |
336 | sections of the crtend.o file, and since that file is always linked in | |
337 | last, these words naturally end up at the very ends of the two lists | |
338 | contained in these two sections. */ | |
339 | ||
b13ae905 | 340 | #ifdef CTOR_LIST_END |
341 | CTOR_LIST_END; | |
342 | #else | |
9d13f5c4 | 343 | asm (CTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */ |
b7c87ff2 | 344 | STATIC func_ptr __CTOR_END__[1] = { (func_ptr) 0 }; |
b13ae905 | 345 | #endif |
9d13f5c4 | 346 | |
b13ae905 | 347 | #ifdef DTOR_LIST_END |
348 | DTOR_LIST_END; | |
349 | #else | |
9d13f5c4 | 350 | asm (DTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */ |
b7c87ff2 | 351 | STATIC func_ptr __DTOR_END__[1] = { (func_ptr) 0 }; |
b13ae905 | 352 | #endif |
9d13f5c4 | 353 | |
354 | #endif /* defined(CRT_END) */ |