OSDN Git Service

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