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