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