]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/crtstuff.c
* SERVICE: Update from the FSF.
[thirdparty/gcc.git] / gcc / crtstuff.c
CommitLineData
9d13f5c4 1/* Specialized bits of code needed to support construction and
2 destruction of file-scope objects in C++ code.
997d68fe 3 Copyright (C) 1991, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
31dcf09a 4 Contributed by Ron Guilmette (rfg@monkeys.com).
9d13f5c4 5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
8d62a21c 20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
9d13f5c4 22
23/* As a special exception, if you link this library with files
24 compiled with GCC to produce an executable, this does not cause
25 the resulting executable to be covered by the GNU General Public License.
26 This exception does not however invalidate any other reasons why
27 the executable file might be covered by the GNU General Public License. */
28
29/* This file is a bit like libgcc1.c/libgcc2.c in that it is compiled
30 multiple times and yields multiple .o files.
31
32 This file is useful on target machines where the object file format
33 supports multiple "user-defined" sections (e.g. COFF, ELF, ROSE). On
34 such systems, this file allows us to avoid running collect (or any
35 other such slow and painful kludge). Additionally, if the target
36 system supports a .init section, this file allows us to support the
37 linking of C++ code with a non-C++ main program.
38
39 Note that if INIT_SECTION_ASM_OP is defined in the tm.h file, then
40 this file *will* make use of the .init section. If that symbol is
41 not defined however, then the .init section will not be used.
42
5dd605c6 43 Currently, only ELF and COFF are supported. It is likely however that
44 ROSE could also be supported, if someone was willing to do the work to
45 make whatever (small?) adaptations are needed. (Some work may be
46 needed on the ROSE assembler and linker also.)
9d13f5c4 47
48 This file must be compiled with gcc. */
49
50/* It is incorrect to include config.h here, because this file is being
51 compiled for the target, and hence definitions concerning only the host
52 do not apply. */
53
54#include "tm.h"
d757b8c9 55#include "defaults.h"
ad87de1e 56#include <stddef.h>
57#include "frame.h"
9d13f5c4 58
5c61dffd 59#ifndef OBJECT_FORMAT_MACHO
60
b7c87ff2 61/* Provide default definitions for the pseudo-ops used to switch to the
62 .ctors and .dtors sections.
63
64 Note that we want to give these sections the SHF_WRITE attribute
65 because these sections will actually contain data (i.e. tables of
66 addresses of functions in the current root executable or shared library
67 file) and, in the case of a shared library, the relocatable addresses
68 will have to be properly resolved/relocated (and then written into) by
69 the dynamic linker when it actually attaches the given shared library
70 to the executing process. (Note that on SVR4, you may wish to use the
71 `-z text' option to the ELF linker, when building a shared library, as
72 an additional check that you are doing everything right. But if you do
73 use the `-z text' option when building a shared library, you will get
74 errors unless the .ctors and .dtors sections are marked as writable
75 via the SHF_WRITE attribute.) */
76
9d13f5c4 77#ifndef CTORS_SECTION_ASM_OP
b7c87ff2 78#define CTORS_SECTION_ASM_OP ".section\t.ctors,\"aw\""
9d13f5c4 79#endif
80#ifndef DTORS_SECTION_ASM_OP
b7c87ff2 81#define DTORS_SECTION_ASM_OP ".section\t.dtors,\"aw\""
9d13f5c4 82#endif
d757b8c9 83#if !defined (EH_FRAME_SECTION_ASM_OP) && defined (DWARF2_UNWIND_INFO) && defined(ASM_OUTPUT_SECTION_NAME)
84#define EH_FRAME_SECTION_ASM_OP ".section\t.eh_frame,\"aw\""
85#endif
9d13f5c4 86
b7c87ff2 87#ifdef OBJECT_FORMAT_ELF
88
89/* Declare a pointer to void function type. */
90typedef void (*func_ptr) (void);
91#define STATIC static
92
93#else /* OBJECT_FORMAT_ELF */
94
9d13f5c4 95#include "gbl-ctors.h"
96
97#ifndef ON_EXIT
98#define ON_EXIT(a, b)
99#endif
b7c87ff2 100#define STATIC
101
102#endif /* OBJECT_FORMAT_ELF */
9d13f5c4 103
104#ifdef CRT_BEGIN
105
106#ifdef INIT_SECTION_ASM_OP
107
b7c87ff2 108#ifdef OBJECT_FORMAT_ELF
109
110/* Run all the global destructors on exit from the program. */
111
112/* Some systems place the number of pointers in the first word of the
113 table. On SVR4 however, that word is -1. In all cases, the table is
114 null-terminated. On SVR4, we start from the beginning of the list and
115 invoke each per-compilation-unit destructor routine in order
116 until we find that null.
117
118 Note that this function MUST be static. There will be one of these
119 functions in each root executable and one in each shared library, but
120 although they all have the same code, each one is unique in that it
121 refers to one particular associated `__DTOR_LIST__' which belongs to the
113f9ca7 122 same particular root executable or shared library file.
123
124 On some systems, this routine is run more than once from the .fini,
125 when exit is called recursively, so we arrange to remember where in
126 the list we left off processing, and we resume at that point,
127 should we be re-invoked. */
b7c87ff2 128
d757b8c9 129static char __EH_FRAME_BEGIN__[];
b7c87ff2 130static func_ptr __DTOR_LIST__[];
131static void
132__do_global_dtors_aux ()
133{
113f9ca7 134 static func_ptr *p = __DTOR_LIST__ + 1;
dade0711 135 static int completed = 0;
136
137 if (completed)
138 return;
139
113f9ca7 140 while (*p)
141 {
142 p++;
143 (*(p-1)) ();
144 }
d757b8c9 145
146#ifdef EH_FRAME_SECTION_ASM_OP
c4fa4c4d 147 __deregister_frame_info (__EH_FRAME_BEGIN__);
d757b8c9 148#endif
dade0711 149 completed = 1;
b7c87ff2 150}
151
dade0711 152
b7c87ff2 153/* Stick a call to __do_global_dtors_aux into the .fini section. */
a92771b8 154
ebd9163c 155static void __attribute__ ((__unused__))
b7c87ff2 156fini_dummy ()
157{
158 asm (FINI_SECTION_ASM_OP);
159 __do_global_dtors_aux ();
160#ifdef FORCE_FINI_SECTION_ALIGN
161 FORCE_FINI_SECTION_ALIGN;
162#endif
163 asm (TEXT_SECTION_ASM_OP);
164}
165
d757b8c9 166#ifdef EH_FRAME_SECTION_ASM_OP
c4fa4c4d 167/* Stick a call to __register_frame_info into the .init section. For some
168 reason calls with no arguments work more reliably in .init, so stick the
169 call in another function. */
d757b8c9 170
171static void
172frame_dummy ()
173{
ad87de1e 174 static struct object object;
c4fa4c4d 175 __register_frame_info (__EH_FRAME_BEGIN__, &object);
d757b8c9 176}
177
ebd9163c 178static void __attribute__ ((__unused__))
d757b8c9 179init_dummy ()
180{
181 asm (INIT_SECTION_ASM_OP);
182 frame_dummy ();
183#ifdef FORCE_INIT_SECTION_ALIGN
184 FORCE_INIT_SECTION_ALIGN;
185#endif
186 asm (TEXT_SECTION_ASM_OP);
187}
188#endif /* EH_FRAME_SECTION_ASM_OP */
189
b7c87ff2 190#else /* OBJECT_FORMAT_ELF */
191
b13ae905 192/* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o
d8fa92ab 193 and once in crtend.o). It must be declared static to avoid a link
b13ae905 194 error. Here, we define __do_global_ctors as an externally callable
195 function. It is externally callable so that __main can invoke it when
196 INVOKE__main is defined. This has the additional effect of forcing cc1
197 to switch to the .text section. */
a92771b8 198
ee45242a 199static void __do_global_ctors_aux ();
84b64a19 200void __do_global_ctors ()
201{
202#ifdef INVOKE__main /* If __main won't actually call __do_global_ctors
203 then it doesn't matter what's inside the function.
204 The inside of __do_global_ctors_aux is called
205 automatically in that case.
206 And the Alliant fx2800 linker crashes
207 on this reference. So prevent the crash. */
208 __do_global_ctors_aux ();
209#endif
210}
9d13f5c4 211
212asm (INIT_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
213
3c67709e 214/* On some svr4 systems, the initial .init section preamble code provided in
215 crti.o may do something, such as bump the stack, which we have to
216 undo before we reach the function prologue code for __do_global_ctors
217 (directly below). For such systems, define the macro INIT_SECTION_PREAMBLE
a92771b8 218 to expand into the code needed to undo the actions of the crti.o file. */
3c67709e 219
220#ifdef INIT_SECTION_PREAMBLE
221 INIT_SECTION_PREAMBLE;
222#endif
223
9d13f5c4 224/* A routine to invoke all of the global constructors upon entry to the
225 program. We put this into the .init section (for systems that have
226 such a thing) so that we can properly perform the construction of
227 file-scope static-storage C++ objects within shared libraries. */
228
229static void
b13ae905 230__do_global_ctors_aux () /* prologue goes in .init section */
9d13f5c4 231{
635d52b7 232#ifdef FORCE_INIT_SECTION_ALIGN
233 FORCE_INIT_SECTION_ALIGN; /* Explicit align before switch to .text */
234#endif
9d13f5c4 235 asm (TEXT_SECTION_ASM_OP); /* don't put epilogue and body in .init */
236 DO_GLOBAL_CTORS_BODY;
237 ON_EXIT (__do_global_dtors, 0);
238}
239
b7c87ff2 240#endif /* OBJECT_FORMAT_ELF */
8313a782 241
242#else /* defined(INIT_SECTION_ASM_OP) */
243
4e04a2df 244#ifdef HAS_INIT_SECTION
8313a782 245/* This case is used by the Irix 6 port, which supports named sections but
246 not an SVR4-style .fini section. __do_global_dtors can be non-static
d757b8c9 247 in this case because we protect it with -hidden_symbol. */
248
249static char __EH_FRAME_BEGIN__[];
8313a782 250static func_ptr __DTOR_LIST__[];
251void
252__do_global_dtors ()
253{
254 func_ptr *p;
255 for (p = __DTOR_LIST__ + 1; *p; p++)
256 (*p) ();
d757b8c9 257
258#ifdef EH_FRAME_SECTION_ASM_OP
c4fa4c4d 259 __deregister_frame_info (__EH_FRAME_BEGIN__);
d757b8c9 260#endif
8313a782 261}
72ac783d 262
263#ifdef EH_FRAME_SECTION_ASM_OP
264/* Define a function here to call __register_frame. crtend.o is linked in
265 after libgcc.a, and hence can't call libgcc.a functions directly. That
266 can lead to unresolved function references. */
267void
268__frame_dummy ()
269{
270 static struct object object;
271 __register_frame_info (__EH_FRAME_BEGIN__, &object);
272}
273#endif
4e04a2df 274#endif
8313a782 275
9d13f5c4 276#endif /* defined(INIT_SECTION_ASM_OP) */
277
278/* Force cc1 to switch to .data section. */
cd03a192 279static func_ptr force_to_data[0] __attribute__ ((__unused__)) = { };
9d13f5c4 280
b7c87ff2 281/* NOTE: In order to be able to support SVR4 shared libraries, we arrange
282 to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
283 __DTOR_END__ } per root executable and also one set of these symbols
284 per shared library. So in any given whole process image, we may have
285 multiple definitions of each of these symbols. In order to prevent
286 these definitions from conflicting with one another, and in order to
287 ensure that the proper lists are used for the initialization/finalization
288 of each individual shared library (respectively), we give these symbols
289 only internal (i.e. `static') linkage, and we also make it a point to
290 refer to only the __CTOR_END__ symbol in crtend.o and the __DTOR_LIST__
291 symbol in crtbegin.o, where they are defined. */
292
9d13f5c4 293/* The -1 is a flag to __do_global_[cd]tors
294 indicating that this table does not start with a count of elements. */
b13ae905 295#ifdef CTOR_LIST_BEGIN
296CTOR_LIST_BEGIN;
297#else
9d13f5c4 298asm (CTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
cd03a192 299STATIC func_ptr __CTOR_LIST__[1] __attribute__ ((__unused__))
300 = { (func_ptr) (-1) };
b13ae905 301#endif
9d13f5c4 302
b13ae905 303#ifdef DTOR_LIST_BEGIN
304DTOR_LIST_BEGIN;
305#else
9d13f5c4 306asm (DTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
b7c87ff2 307STATIC func_ptr __DTOR_LIST__[1] = { (func_ptr) (-1) };
b13ae905 308#endif
9d13f5c4 309
d757b8c9 310#ifdef EH_FRAME_SECTION_ASM_OP
311/* Stick a label at the beginning of the frame unwind info so we can register
312 and deregister it with the exception handling library code. */
313
314asm (EH_FRAME_SECTION_ASM_OP);
315#ifdef INIT_SECTION_ASM_OP
316STATIC
317#endif
318char __EH_FRAME_BEGIN__[] = { };
319#endif /* EH_FRAME_SECTION_ASM_OP */
320
9d13f5c4 321#endif /* defined(CRT_BEGIN) */
322
323#ifdef CRT_END
324
325#ifdef INIT_SECTION_ASM_OP
326
b7c87ff2 327#ifdef OBJECT_FORMAT_ELF
9d13f5c4 328
b7c87ff2 329static func_ptr __CTOR_END__[];
330static void
331__do_global_ctors_aux ()
332{
333 func_ptr *p;
334 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
335 (*p) ();
336}
337
338/* Stick a call to __do_global_ctors_aux into the .init section. */
a92771b8 339
ebd9163c 340static void __attribute__ ((__unused__))
b7c87ff2 341init_dummy ()
342{
343 asm (INIT_SECTION_ASM_OP);
344 __do_global_ctors_aux ();
345#ifdef FORCE_INIT_SECTION_ALIGN
346 FORCE_INIT_SECTION_ALIGN;
347#endif
348 asm (TEXT_SECTION_ASM_OP);
baf8c510 349
ad87de1e 350/* This is a kludge. The i386 GNU/Linux dynamic linker needs ___brk_addr,
31dcf09a 351 __environ and atexit (). We have to make sure they are in the .dynsym
352 section. We accomplish it by making a dummy call here. This
a92771b8 353 code is never reached. */
baf8c510 354
31dcf09a 355#if defined(__linux__) && defined(__PIC__) && defined(__i386__)
baf8c510 356 {
357 extern void *___brk_addr;
358 extern char **__environ;
359
360 ___brk_addr = __environ;
361 atexit ();
362 }
363#endif
b7c87ff2 364}
365
366#else /* OBJECT_FORMAT_ELF */
367
368/* Stick the real initialization code, followed by a normal sort of
369 function epilogue at the very end of the .init section for this
370 entire root executable file or for this entire shared library file.
371
372 Note that we use some tricks here to get *just* the body and just
373 a function epilogue (but no function prologue) into the .init
c3418f42 374 section of the crtend.o file. Specifically, we switch to the .text
b7c87ff2 375 section, start to define a function, and then we switch to the .init
376 section just before the body code.
377
378 Earlier on, we put the corresponding function prologue into the .init
379 section of the crtbegin.o file (which will be linked in first).
380
381 Note that we want to invoke all constructors for C++ file-scope static-
382 storage objects AFTER any other possible initialization actions which
383 may be performed by the code in the .init section contributions made by
384 other libraries, etc. That's because those other initializations may
385 include setup operations for very primitive things (e.g. initializing
386 the state of the floating-point coprocessor, etc.) which should be done
a92771b8 387 before we start to execute any of the user's code. */
9d13f5c4 388
389static void
b13ae905 390__do_global_ctors_aux () /* prologue goes in .text section */
9d13f5c4 391{
392 asm (INIT_SECTION_ASM_OP);
393 DO_GLOBAL_CTORS_BODY;
394 ON_EXIT (__do_global_dtors, 0);
997d68fe 395} /* epilogue and body go in .init section */
396
220345e0 397#ifdef FORCE_INIT_SECTION_ALIGN
997d68fe 398FORCE_INIT_SECTION_ALIGN;
220345e0 399#endif
997d68fe 400
401asm (TEXT_SECTION_ASM_OP);
9d13f5c4 402
b7c87ff2 403#endif /* OBJECT_FORMAT_ELF */
404
8313a782 405#else /* defined(INIT_SECTION_ASM_OP) */
406
4e04a2df 407#ifdef HAS_INIT_SECTION
8313a782 408/* This case is used by the Irix 6 port, which supports named sections but
409 not an SVR4-style .init section. __do_global_ctors can be non-static
d757b8c9 410 in this case because we protect it with -hidden_symbol. */
8313a782 411static func_ptr __CTOR_END__[];
ebd9163c 412#ifdef EH_FRAME_SECTION_ASM_OP
413extern void __frame_dummy (void);
414#endif
8313a782 415void
416__do_global_ctors ()
417{
418 func_ptr *p;
d757b8c9 419#ifdef EH_FRAME_SECTION_ASM_OP
72ac783d 420 __frame_dummy ();
d757b8c9 421#endif
8313a782 422 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
423 (*p) ();
424}
4e04a2df 425#endif
8313a782 426
9d13f5c4 427#endif /* defined(INIT_SECTION_ASM_OP) */
428
429/* Force cc1 to switch to .data section. */
cd03a192 430static func_ptr force_to_data[0] __attribute__ ((__unused__)) = { };
9d13f5c4 431
b7c87ff2 432/* Put a word containing zero at the end of each of our two lists of function
433 addresses. Note that the words defined here go into the .ctors and .dtors
434 sections of the crtend.o file, and since that file is always linked in
435 last, these words naturally end up at the very ends of the two lists
436 contained in these two sections. */
437
b13ae905 438#ifdef CTOR_LIST_END
439CTOR_LIST_END;
440#else
9d13f5c4 441asm (CTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
b7c87ff2 442STATIC func_ptr __CTOR_END__[1] = { (func_ptr) 0 };
b13ae905 443#endif
9d13f5c4 444
b13ae905 445#ifdef DTOR_LIST_END
446DTOR_LIST_END;
447#else
9d13f5c4 448asm (DTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
cd03a192 449STATIC func_ptr __DTOR_END__[1] __attribute__ ((__unused__))
450 = { (func_ptr) 0 };
b13ae905 451#endif
9d13f5c4 452
d757b8c9 453#ifdef EH_FRAME_SECTION_ASM_OP
454/* Terminate the frame unwind info section with a 4byte 0 as a sentinel;
455 this would be the 'length' field in a real FDE. */
456
457typedef unsigned int ui32 __attribute__ ((mode (SI)));
458asm (EH_FRAME_SECTION_ASM_OP);
cd03a192 459STATIC ui32 __FRAME_END__[] __attribute__ ((__unused__)) = { 0 };
d757b8c9 460#endif /* EH_FRAME_SECTION */
461
9d13f5c4 462#endif /* defined(CRT_END) */
5c61dffd 463
464#else /* OBJECT_FORMAT_MACHO */
465
466/* For Mach-O format executables, we assume that the system's runtime is
467 smart enough to handle constructors and destructors, but doesn't have
468 an init section (if it can't even handle constructors/destructors
469 you should be using INVOKE__main, not crtstuff). All we need to do
470 is install/deinstall the frame information for exceptions. We do this
471 by putting a constructor in crtbegin.o and a destructor in crtend.o.
472
473 crtend.o also puts in the terminating zero in the frame information
474 segment. */
475
476/* The crtstuff for other object formats use the symbol __EH_FRAME_BEGIN__
477 to figure out the start of the exception frame, but here we use
478 getsectbynamefromheader to find this value. Either method would work,
479 but this method avoids creating any global symbols, which seems
480 cleaner. */
481
482#include <mach-o/ldsyms.h>
483extern const struct section *
484 getsectbynamefromheader (const struct mach_header *,
485 const char *, const char *);
486
487#ifdef CRT_BEGIN
488
489static void __reg_frame_ctor () __attribute__ ((constructor));
490
491static void
492__reg_frame_ctor ()
493{
494 static struct object object;
495 const struct section *eh_frame;
496
497 eh_frame = getsectbynamefromheader (&_mh_execute_header,
498 "__TEXT", "__eh_frame");
499 __register_frame_info ((void *) eh_frame->addr, &object);
500}
501
502#endif /* CRT_BEGIN */
503
504#ifdef CRT_END
505
506static void __dereg_frame_dtor () __attribute__ ((destructor));
507
508static
509void __dereg_frame_dtor ()
510{
511 const struct section *eh_frame;
512
513 eh_frame = getsectbynamefromheader (&_mh_execute_header,
514 "__TEXT", "__eh_frame");
515 __deregister_frame_info ((void *) eh_frame->addr);
516}
517
518/* Terminate the frame section with a final zero. */
519
520/* Force cc1 to switch to .data section. */
521static void * force_to_data[0] __attribute__ ((__unused__)) = { };
522
523typedef unsigned int ui32 __attribute__ ((mode (SI)));
524asm (EH_FRAME_SECTION_ASM_OP);
525static ui32 __FRAME_END__[] __attribute__ ((__unused__)) = { 0 };
526
527#endif /* CRT_END */
528
529#endif /* OBJECT_FORMAT_MACHO */
530