]> gcc.gnu.org Git - gcc.git/blame - gcc/crtstuff.c
* mips.h: Fix some type-o's from a previous change.
[gcc.git] / gcc / crtstuff.c
CommitLineData
dc17e9e9
RS
1/* Specialized bits of code needed to support construction and
2 destruction of file-scope objects in C++ code.
35a42f5f 3 Copyright (C) 1991, 94-97, 1998 Free Software Foundation, Inc.
d0f8fcea 4 Contributed by Ron Guilmette (rfg@monkeys.com).
dc17e9e9
RS
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
940d9d63
RK
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
dc17e9e9
RS
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
37ce5b86
CH
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.)
dc17e9e9
RS
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"
0021b564 55#include "defaults.h"
956d6950
JL
56#include <stddef.h>
57#include "frame.h"
dc17e9e9 58
68d69835
JM
59/* Provide default definitions for the pseudo-ops used to switch to the
60 .ctors and .dtors sections.
61
62 Note that we want to give these sections the SHF_WRITE attribute
63 because these sections will actually contain data (i.e. tables of
64 addresses of functions in the current root executable or shared library
65 file) and, in the case of a shared library, the relocatable addresses
66 will have to be properly resolved/relocated (and then written into) by
67 the dynamic linker when it actually attaches the given shared library
68 to the executing process. (Note that on SVR4, you may wish to use the
69 `-z text' option to the ELF linker, when building a shared library, as
70 an additional check that you are doing everything right. But if you do
71 use the `-z text' option when building a shared library, you will get
72 errors unless the .ctors and .dtors sections are marked as writable
73 via the SHF_WRITE attribute.) */
74
dc17e9e9 75#ifndef CTORS_SECTION_ASM_OP
68d69835 76#define CTORS_SECTION_ASM_OP ".section\t.ctors,\"aw\""
dc17e9e9
RS
77#endif
78#ifndef DTORS_SECTION_ASM_OP
68d69835 79#define DTORS_SECTION_ASM_OP ".section\t.dtors,\"aw\""
dc17e9e9 80#endif
0021b564
JM
81#if !defined (EH_FRAME_SECTION_ASM_OP) && defined (DWARF2_UNWIND_INFO) && defined(ASM_OUTPUT_SECTION_NAME)
82#define EH_FRAME_SECTION_ASM_OP ".section\t.eh_frame,\"aw\""
83#endif
dc17e9e9 84
68d69835
JM
85#ifdef OBJECT_FORMAT_ELF
86
87/* Declare a pointer to void function type. */
88typedef void (*func_ptr) (void);
89#define STATIC static
90
91#else /* OBJECT_FORMAT_ELF */
92
dc17e9e9
RS
93#include "gbl-ctors.h"
94
95#ifndef ON_EXIT
96#define ON_EXIT(a, b)
97#endif
68d69835
JM
98#define STATIC
99
100#endif /* OBJECT_FORMAT_ELF */
dc17e9e9
RS
101
102#ifdef CRT_BEGIN
103
104#ifdef INIT_SECTION_ASM_OP
105
68d69835
JM
106#ifdef OBJECT_FORMAT_ELF
107
108/* Run all the global destructors on exit from the program. */
109
110/* Some systems place the number of pointers in the first word of the
111 table. On SVR4 however, that word is -1. In all cases, the table is
112 null-terminated. On SVR4, we start from the beginning of the list and
113 invoke each per-compilation-unit destructor routine in order
114 until we find that null.
115
116 Note that this function MUST be static. There will be one of these
117 functions in each root executable and one in each shared library, but
118 although they all have the same code, each one is unique in that it
119 refers to one particular associated `__DTOR_LIST__' which belongs to the
b40b9d93
MS
120 same particular root executable or shared library file.
121
122 On some systems, this routine is run more than once from the .fini,
123 when exit is called recursively, so we arrange to remember where in
124 the list we left off processing, and we resume at that point,
125 should we be re-invoked. */
68d69835 126
0021b564 127static char __EH_FRAME_BEGIN__[];
68d69835
JM
128static func_ptr __DTOR_LIST__[];
129static void
130__do_global_dtors_aux ()
131{
b40b9d93 132 static func_ptr *p = __DTOR_LIST__ + 1;
5041a61c
JL
133 static int completed = 0;
134
135 if (completed)
136 return;
137
b40b9d93
MS
138 while (*p)
139 {
140 p++;
141 (*(p-1)) ();
142 }
0021b564
JM
143
144#ifdef EH_FRAME_SECTION_ASM_OP
6d8ccdbb 145 __deregister_frame_info (__EH_FRAME_BEGIN__);
0021b564 146#endif
5041a61c 147 completed = 1;
68d69835
JM
148}
149
5041a61c 150
68d69835 151/* Stick a call to __do_global_dtors_aux into the .fini section. */
0f41302f 152
68d69835
JM
153static void
154fini_dummy ()
155{
156 asm (FINI_SECTION_ASM_OP);
157 __do_global_dtors_aux ();
158#ifdef FORCE_FINI_SECTION_ALIGN
159 FORCE_FINI_SECTION_ALIGN;
160#endif
161 asm (TEXT_SECTION_ASM_OP);
162}
163
0021b564 164#ifdef EH_FRAME_SECTION_ASM_OP
6d8ccdbb
JL
165/* Stick a call to __register_frame_info into the .init section. For some
166 reason calls with no arguments work more reliably in .init, so stick the
167 call in another function. */
0021b564
JM
168
169static void
170frame_dummy ()
171{
956d6950 172 static struct object object;
6d8ccdbb 173 __register_frame_info (__EH_FRAME_BEGIN__, &object);
0021b564
JM
174}
175
176static void
177init_dummy ()
178{
179 asm (INIT_SECTION_ASM_OP);
180 frame_dummy ();
181#ifdef FORCE_INIT_SECTION_ALIGN
182 FORCE_INIT_SECTION_ALIGN;
183#endif
184 asm (TEXT_SECTION_ASM_OP);
185}
186#endif /* EH_FRAME_SECTION_ASM_OP */
187
68d69835
JM
188#else /* OBJECT_FORMAT_ELF */
189
b335c2cc 190/* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o
b71a4aa9 191 and once in crtend.o). It must be declared static to avoid a link
b335c2cc
TW
192 error. Here, we define __do_global_ctors as an externally callable
193 function. It is externally callable so that __main can invoke it when
194 INVOKE__main is defined. This has the additional effect of forcing cc1
195 to switch to the .text section. */
0f41302f 196
93c3d169 197static void __do_global_ctors_aux ();
329d2160
RS
198void __do_global_ctors ()
199{
200#ifdef INVOKE__main /* If __main won't actually call __do_global_ctors
201 then it doesn't matter what's inside the function.
202 The inside of __do_global_ctors_aux is called
203 automatically in that case.
204 And the Alliant fx2800 linker crashes
205 on this reference. So prevent the crash. */
206 __do_global_ctors_aux ();
207#endif
208}
dc17e9e9
RS
209
210asm (INIT_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
211
5378943d
RK
212/* On some svr4 systems, the initial .init section preamble code provided in
213 crti.o may do something, such as bump the stack, which we have to
214 undo before we reach the function prologue code for __do_global_ctors
215 (directly below). For such systems, define the macro INIT_SECTION_PREAMBLE
0f41302f 216 to expand into the code needed to undo the actions of the crti.o file. */
5378943d
RK
217
218#ifdef INIT_SECTION_PREAMBLE
219 INIT_SECTION_PREAMBLE;
220#endif
221
dc17e9e9
RS
222/* A routine to invoke all of the global constructors upon entry to the
223 program. We put this into the .init section (for systems that have
224 such a thing) so that we can properly perform the construction of
225 file-scope static-storage C++ objects within shared libraries. */
226
227static void
b335c2cc 228__do_global_ctors_aux () /* prologue goes in .init section */
dc17e9e9 229{
5868ca20
JW
230#ifdef FORCE_INIT_SECTION_ALIGN
231 FORCE_INIT_SECTION_ALIGN; /* Explicit align before switch to .text */
232#endif
dc17e9e9
RS
233 asm (TEXT_SECTION_ASM_OP); /* don't put epilogue and body in .init */
234 DO_GLOBAL_CTORS_BODY;
235 ON_EXIT (__do_global_dtors, 0);
236}
237
68d69835 238#endif /* OBJECT_FORMAT_ELF */
fe1fd353
JM
239
240#else /* defined(INIT_SECTION_ASM_OP) */
241
9593ce03 242#ifdef HAS_INIT_SECTION
fe1fd353
JM
243/* This case is used by the Irix 6 port, which supports named sections but
244 not an SVR4-style .fini section. __do_global_dtors can be non-static
0021b564
JM
245 in this case because we protect it with -hidden_symbol. */
246
247static char __EH_FRAME_BEGIN__[];
fe1fd353
JM
248static func_ptr __DTOR_LIST__[];
249void
250__do_global_dtors ()
251{
252 func_ptr *p;
253 for (p = __DTOR_LIST__ + 1; *p; p++)
254 (*p) ();
0021b564
JM
255
256#ifdef EH_FRAME_SECTION_ASM_OP
6d8ccdbb 257 __deregister_frame_info (__EH_FRAME_BEGIN__);
0021b564 258#endif
fe1fd353 259}
35a42f5f
JW
260
261#ifdef EH_FRAME_SECTION_ASM_OP
262/* Define a function here to call __register_frame. crtend.o is linked in
263 after libgcc.a, and hence can't call libgcc.a functions directly. That
264 can lead to unresolved function references. */
265void
266__frame_dummy ()
267{
268 static struct object object;
269 __register_frame_info (__EH_FRAME_BEGIN__, &object);
270}
271#endif
9593ce03 272#endif
fe1fd353 273
dc17e9e9
RS
274#endif /* defined(INIT_SECTION_ASM_OP) */
275
276/* Force cc1 to switch to .data section. */
277static func_ptr force_to_data[0] = { };
278
68d69835
JM
279/* NOTE: In order to be able to support SVR4 shared libraries, we arrange
280 to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
281 __DTOR_END__ } per root executable and also one set of these symbols
282 per shared library. So in any given whole process image, we may have
283 multiple definitions of each of these symbols. In order to prevent
284 these definitions from conflicting with one another, and in order to
285 ensure that the proper lists are used for the initialization/finalization
286 of each individual shared library (respectively), we give these symbols
287 only internal (i.e. `static') linkage, and we also make it a point to
288 refer to only the __CTOR_END__ symbol in crtend.o and the __DTOR_LIST__
289 symbol in crtbegin.o, where they are defined. */
290
dc17e9e9
RS
291/* The -1 is a flag to __do_global_[cd]tors
292 indicating that this table does not start with a count of elements. */
b335c2cc
TW
293#ifdef CTOR_LIST_BEGIN
294CTOR_LIST_BEGIN;
295#else
dc17e9e9 296asm (CTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
68d69835 297STATIC func_ptr __CTOR_LIST__[1] = { (func_ptr) (-1) };
b335c2cc 298#endif
dc17e9e9 299
b335c2cc
TW
300#ifdef DTOR_LIST_BEGIN
301DTOR_LIST_BEGIN;
302#else
dc17e9e9 303asm (DTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
68d69835 304STATIC func_ptr __DTOR_LIST__[1] = { (func_ptr) (-1) };
b335c2cc 305#endif
dc17e9e9 306
0021b564
JM
307#ifdef EH_FRAME_SECTION_ASM_OP
308/* Stick a label at the beginning of the frame unwind info so we can register
309 and deregister it with the exception handling library code. */
310
311asm (EH_FRAME_SECTION_ASM_OP);
312#ifdef INIT_SECTION_ASM_OP
313STATIC
314#endif
315char __EH_FRAME_BEGIN__[] = { };
316#endif /* EH_FRAME_SECTION_ASM_OP */
317
dc17e9e9
RS
318#endif /* defined(CRT_BEGIN) */
319
320#ifdef CRT_END
321
322#ifdef INIT_SECTION_ASM_OP
323
68d69835 324#ifdef OBJECT_FORMAT_ELF
dc17e9e9 325
68d69835
JM
326static func_ptr __CTOR_END__[];
327static void
328__do_global_ctors_aux ()
329{
330 func_ptr *p;
331 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
332 (*p) ();
333}
334
335/* Stick a call to __do_global_ctors_aux into the .init section. */
0f41302f 336
68d69835
JM
337static void
338init_dummy ()
339{
340 asm (INIT_SECTION_ASM_OP);
341 __do_global_ctors_aux ();
342#ifdef FORCE_INIT_SECTION_ALIGN
343 FORCE_INIT_SECTION_ALIGN;
344#endif
345 asm (TEXT_SECTION_ASM_OP);
bced43dd 346
956d6950 347/* This is a kludge. The i386 GNU/Linux dynamic linker needs ___brk_addr,
d0f8fcea
RK
348 __environ and atexit (). We have to make sure they are in the .dynsym
349 section. We accomplish it by making a dummy call here. This
0f41302f 350 code is never reached. */
bced43dd 351
d0f8fcea 352#if defined(__linux__) && defined(__PIC__) && defined(__i386__)
bced43dd
RK
353 {
354 extern void *___brk_addr;
355 extern char **__environ;
356
357 ___brk_addr = __environ;
358 atexit ();
359 }
360#endif
68d69835
JM
361}
362
363#else /* OBJECT_FORMAT_ELF */
364
365/* Stick the real initialization code, followed by a normal sort of
366 function epilogue at the very end of the .init section for this
367 entire root executable file or for this entire shared library file.
368
369 Note that we use some tricks here to get *just* the body and just
370 a function epilogue (but no function prologue) into the .init
9faa82d8 371 section of the crtend.o file. Specifically, we switch to the .text
68d69835
JM
372 section, start to define a function, and then we switch to the .init
373 section just before the body code.
374
375 Earlier on, we put the corresponding function prologue into the .init
376 section of the crtbegin.o file (which will be linked in first).
377
378 Note that we want to invoke all constructors for C++ file-scope static-
379 storage objects AFTER any other possible initialization actions which
380 may be performed by the code in the .init section contributions made by
381 other libraries, etc. That's because those other initializations may
382 include setup operations for very primitive things (e.g. initializing
383 the state of the floating-point coprocessor, etc.) which should be done
0f41302f 384 before we start to execute any of the user's code. */
dc17e9e9
RS
385
386static void
b335c2cc 387__do_global_ctors_aux () /* prologue goes in .text section */
dc17e9e9
RS
388{
389 asm (INIT_SECTION_ASM_OP);
390 DO_GLOBAL_CTORS_BODY;
391 ON_EXIT (__do_global_dtors, 0);
392} /* epilogue and body go in .init section */
393
68d69835
JM
394#endif /* OBJECT_FORMAT_ELF */
395
fe1fd353
JM
396#else /* defined(INIT_SECTION_ASM_OP) */
397
9593ce03 398#ifdef HAS_INIT_SECTION
fe1fd353
JM
399/* This case is used by the Irix 6 port, which supports named sections but
400 not an SVR4-style .init section. __do_global_ctors can be non-static
0021b564 401 in this case because we protect it with -hidden_symbol. */
fe1fd353
JM
402static func_ptr __CTOR_END__[];
403void
404__do_global_ctors ()
405{
406 func_ptr *p;
0021b564 407#ifdef EH_FRAME_SECTION_ASM_OP
35a42f5f 408 __frame_dummy ();
0021b564 409#endif
fe1fd353
JM
410 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
411 (*p) ();
412}
9593ce03 413#endif
fe1fd353 414
dc17e9e9
RS
415#endif /* defined(INIT_SECTION_ASM_OP) */
416
417/* Force cc1 to switch to .data section. */
418static func_ptr force_to_data[0] = { };
419
68d69835
JM
420/* Put a word containing zero at the end of each of our two lists of function
421 addresses. Note that the words defined here go into the .ctors and .dtors
422 sections of the crtend.o file, and since that file is always linked in
423 last, these words naturally end up at the very ends of the two lists
424 contained in these two sections. */
425
b335c2cc
TW
426#ifdef CTOR_LIST_END
427CTOR_LIST_END;
428#else
dc17e9e9 429asm (CTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
68d69835 430STATIC func_ptr __CTOR_END__[1] = { (func_ptr) 0 };
b335c2cc 431#endif
dc17e9e9 432
b335c2cc
TW
433#ifdef DTOR_LIST_END
434DTOR_LIST_END;
435#else
dc17e9e9 436asm (DTORS_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */
68d69835 437STATIC func_ptr __DTOR_END__[1] = { (func_ptr) 0 };
b335c2cc 438#endif
dc17e9e9 439
0021b564
JM
440#ifdef EH_FRAME_SECTION_ASM_OP
441/* Terminate the frame unwind info section with a 4byte 0 as a sentinel;
442 this would be the 'length' field in a real FDE. */
443
444typedef unsigned int ui32 __attribute__ ((mode (SI)));
445asm (EH_FRAME_SECTION_ASM_OP);
446STATIC ui32 __FRAME_END__[] = { 0 };
447#endif /* EH_FRAME_SECTION */
448
dc17e9e9 449#endif /* defined(CRT_END) */
This page took 0.388085 seconds and 5 git commands to generate.