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