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