1 /* .init/.fini section handling + C++ global constructor/destructor handling.
2 This file is based on crtstuff.c, sol2-crti.S, sol2-crtn.S.
4 Copyright (C) 1996-2014 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC 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 3, or (at your option)
13 GCC 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.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 /* Declare a pointer to void function type. */
28 typedef void (*func_ptr
) (void);
32 /* NOTE: In order to be able to support SVR4 shared libraries, we arrange
33 to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
34 __DTOR_END__ } per root executable and also one set of these symbols
35 per shared library. So in any given whole process image, we may have
36 multiple definitions of each of these symbols. In order to prevent
37 these definitions from conflicting with one another, and in order to
38 ensure that the proper lists are used for the initialization/finalization
39 of each individual shared library (respectively), we give these symbols
40 only internal (i.e. `static') linkage, and we also make it a point to
41 refer to only the __CTOR_END__ symbol in crtfini.o and the __DTOR_LIST__
42 symbol in crtinit.o, where they are defined. */
44 static func_ptr __CTOR_LIST__
[1]
45 __attribute__ ((used
, section (".ctors")))
46 = { (func_ptr
) (-1) };
48 static func_ptr __DTOR_LIST__
[1]
49 __attribute__ ((used
, section (".dtors")))
50 = { (func_ptr
) (-1) };
52 /* Run all the global destructors on exit from the program. */
54 /* Some systems place the number of pointers in the first word of the
55 table. On SVR4 however, that word is -1. In all cases, the table is
56 null-terminated. On SVR4, we start from the beginning of the list and
57 invoke each per-compilation-unit destructor routine in order
58 until we find that null.
60 Note that this function MUST be static. There will be one of these
61 functions in each root executable and one in each shared library, but
62 although they all have the same code, each one is unique in that it
63 refers to one particular associated `__DTOR_LIST__' which belongs to the
64 same particular root executable or shared library file. */
66 static void __do_global_dtors (void)
67 asm ("__do_global_dtors") __attribute__ ((used
, section (".text")));
70 __do_global_dtors (void)
74 for (p
= __DTOR_LIST__
+ 1; *p
; p
++)
78 /* .init section start.
79 This must appear at the start of the .init section. */
82 .section .init,\"ax\",@progbits\n\
89 seth r0, #shigh(__fini)\n\
90 add3 r0, r0, #low(__fini)\n\
95 /* .fini section start.
96 This must appear at the start of the .init section. */
99 .section .fini,\"ax\",@progbits\n\
106 bl __do_global_dtors\n\
110 #endif /* CRT_INIT */
114 /* Put a word containing zero at the end of each of our two lists of function
115 addresses. Note that the words defined here go into the .ctors and .dtors
116 sections of the crtend.o file, and since that file is always linked in
117 last, these words naturally end up at the very ends of the two lists
118 contained in these two sections. */
120 static func_ptr __CTOR_END__
[1]
121 __attribute__ ((used
, section (".ctors")))
124 static func_ptr __DTOR_END__
[1]
125 __attribute__ ((used
, section (".dtors")))
128 /* Run all global constructors for the program.
129 Note that they are run in reverse order. */
131 static void __do_global_ctors (void)
132 asm ("__do_global_ctors") __attribute__ ((used
, section (".text")));
135 __do_global_ctors (void)
139 for (p
= __CTOR_END__
- 1; *p
!= (func_ptr
) -1; p
--)
143 /* .init section end.
144 This must live at the end of the .init section. */
147 .section .init,\"ax\",@progbits\n\
148 bl __do_global_ctors\n\
156 /* .fini section end.
157 This must live at the end of the .fini section. */
160 .section .fini,\"ax\",@progbits\n\
168 #endif /* CRT_FINI */