1 /* .init/.fini section handling + C++ global constructor/destructor handling.
2 This file is based on crtstuff.c, sol2-crti.asm, sol2-crtn.asm.
4 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
6 This file is part of GNU CC.
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)
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
22 GNU CC is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with GNU CC; see the file COPYING. If not, write to
29 the Free Software Foundation, 59 Temple Place - Suite 330,
30 Boston, MA 02111-1307, USA. */
32 /* Declare a pointer to void function type. */
33 typedef void (*func_ptr
) (void);
37 /* NOTE: In order to be able to support SVR4 shared libraries, we arrange
38 to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
39 __DTOR_END__ } per root executable and also one set of these symbols
40 per shared library. So in any given whole process image, we may have
41 multiple definitions of each of these symbols. In order to prevent
42 these definitions from conflicting with one another, and in order to
43 ensure that the proper lists are used for the initialization/finalization
44 of each individual shared library (respectively), we give these symbols
45 only internal (i.e. `static') linkage, and we also make it a point to
46 refer to only the __CTOR_END__ symbol in crtfini.o and the __DTOR_LIST__
47 symbol in crtinit.o, where they are defined. */
49 static func_ptr __CTOR_LIST__
[1]
50 __attribute__ ((section (".ctors")))
51 = { (func_ptr
) (-1) };
53 static func_ptr __DTOR_LIST__
[1]
54 __attribute__ ((section (".dtors")))
55 = { (func_ptr
) (-1) };
57 /* Run all the global destructors on exit from the program. */
59 /* Some systems place the number of pointers in the first word of the
60 table. On SVR4 however, that word is -1. In all cases, the table is
61 null-terminated. On SVR4, we start from the beginning of the list and
62 invoke each per-compilation-unit destructor routine in order
63 until we find that null.
65 Note that this function MUST be static. There will be one of these
66 functions in each root executable and one in each shared library, but
67 although they all have the same code, each one is unique in that it
68 refers to one particular associated `__DTOR_LIST__' which belongs to the
69 same particular root executable or shared library file. */
71 static void __do_global_dtors ()
72 asm ("__do_global_dtors") __attribute__ ((section (".text")));
79 for (p
= __DTOR_LIST__
+ 1; *p
; p
++)
83 /* .init section start.
84 This must appear at the start of the .init section. */
87 .section .init,\"ax\",@progbits\n\
94 seth r0, #shigh(__fini)\n\
95 add3 r0, r0, #low(__fini)\n\
100 /* .fini section start.
101 This must appear at the start of the .init section. */
104 .section .fini,\"ax\",@progbits\n\
111 bl __do_global_dtors\n\
115 #endif /* CRT_INIT */
119 /* Put a word containing zero at the end of each of our two lists of function
120 addresses. Note that the words defined here go into the .ctors and .dtors
121 sections of the crtend.o file, and since that file is always linked in
122 last, these words naturally end up at the very ends of the two lists
123 contained in these two sections. */
125 static func_ptr __CTOR_END__
[1]
126 __attribute__ ((section (".ctors")))
129 static func_ptr __DTOR_END__
[1]
130 __attribute__ ((section (".dtors")))
133 /* Run all global constructors for the program.
134 Note that they are run in reverse order. */
136 static void __do_global_ctors ()
137 asm ("__do_global_ctors") __attribute__ ((section (".text")));
144 for (p
= __CTOR_END__
- 1; *p
!= (func_ptr
) -1; p
--)
148 /* .init section end.
149 This must live at the end of the .init section. */
152 .section .init,\"ax\",@progbits\n\
153 bl __do_global_ctors\n\
161 /* .fini section end.
162 This must live at the end of the .fini section. */
165 .section .fini,\"ax\",@progbits\n\
173 #endif /* CRT_FINI */