]>
Commit | Line | Data |
---|---|---|
8c5ca3b9 | 1 | /* .init/.fini section handling + C++ global constructor/destructor handling. |
45b86625 | 2 | This file is based on crtstuff.c, sol2-crti.S, sol2-crtn.S. |
8c5ca3b9 | 3 | |
8d9254fc | 4 | Copyright (C) 1996-2020 Free Software Foundation, Inc. |
6a728a2d SB |
5 | |
6 | This file is part of GCC. | |
7 | ||
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 | |
748086b7 | 10 | the Free Software Foundation; either version 3, or (at your option) |
6a728a2d SB |
11 | any later version. |
12 | ||
6a728a2d SB |
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. | |
17 | ||
748086b7 JJ |
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. | |
21 | ||
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/>. */ | |
8c5ca3b9 | 26 | |
8c5ca3b9 DE |
27 | /* Declare a pointer to void function type. */ |
28 | typedef void (*func_ptr) (void); | |
29 | ||
30 | #ifdef CRT_INIT | |
31 | ||
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. */ | |
43 | ||
dc9ed76a DE |
44 | static func_ptr __CTOR_LIST__[1] |
45 | __attribute__ ((used, section (".ctors"))) | |
8c5ca3b9 DE |
46 | = { (func_ptr) (-1) }; |
47 | ||
dc9ed76a DE |
48 | static func_ptr __DTOR_LIST__[1] |
49 | __attribute__ ((used, section (".dtors"))) | |
8c5ca3b9 DE |
50 | = { (func_ptr) (-1) }; |
51 | ||
52 | /* Run all the global destructors on exit from the program. */ | |
53 | ||
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. | |
59 | ||
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. */ | |
65 | ||
dc9ed76a DE |
66 | static void __do_global_dtors (void) |
67 | asm ("__do_global_dtors") __attribute__ ((used, section (".text"))); | |
8c5ca3b9 | 68 | |
dc9ed76a | 69 | static void |
6a728a2d | 70 | __do_global_dtors (void) |
8c5ca3b9 DE |
71 | { |
72 | func_ptr *p; | |
73 | ||
74 | for (p = __DTOR_LIST__ + 1; *p; p++) | |
75 | (*p) (); | |
76 | } | |
77 | ||
78 | /* .init section start. | |
79 | This must appear at the start of the .init section. */ | |
80 | ||
ab87f8c8 JL |
81 | asm ("\n\ |
82 | .section .init,\"ax\",@progbits\n\ | |
83 | .balign 4\n\ | |
84 | .global __init\n\ | |
85 | __init:\n\ | |
86 | push fp\n\ | |
87 | push lr\n\ | |
88 | mv fp,sp\n\ | |
63063278 AH |
89 | seth r0, #shigh(__fini)\n\ |
90 | add3 r0, r0, #low(__fini)\n\ | |
ab87f8c8 JL |
91 | bl atexit\n\ |
92 | .fillinsn\n\ | |
8c5ca3b9 DE |
93 | "); |
94 | ||
95 | /* .fini section start. | |
96 | This must appear at the start of the .init section. */ | |
97 | ||
ab87f8c8 JL |
98 | asm ("\n\ |
99 | .section .fini,\"ax\",@progbits\n\ | |
100 | .balign 4\n\ | |
101 | .global __fini\n\ | |
102 | __fini:\n\ | |
103 | push fp\n\ | |
104 | push lr\n\ | |
105 | mv fp,sp\n\ | |
106 | bl __do_global_dtors\n\ | |
107 | .fillinsn\n\ | |
8c5ca3b9 DE |
108 | "); |
109 | ||
110 | #endif /* CRT_INIT */ | |
111 | ||
112 | #ifdef CRT_FINI | |
113 | ||
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. */ | |
119 | ||
dc9ed76a DE |
120 | static func_ptr __CTOR_END__[1] |
121 | __attribute__ ((used, section (".ctors"))) | |
8c5ca3b9 DE |
122 | = { (func_ptr) 0 }; |
123 | ||
dc9ed76a DE |
124 | static func_ptr __DTOR_END__[1] |
125 | __attribute__ ((used, section (".dtors"))) | |
8c5ca3b9 DE |
126 | = { (func_ptr) 0 }; |
127 | ||
128 | /* Run all global constructors for the program. | |
129 | Note that they are run in reverse order. */ | |
130 | ||
dc9ed76a DE |
131 | static void __do_global_ctors (void) |
132 | asm ("__do_global_ctors") __attribute__ ((used, section (".text"))); | |
8c5ca3b9 | 133 | |
dc9ed76a | 134 | static void |
6a728a2d | 135 | __do_global_ctors (void) |
8c5ca3b9 DE |
136 | { |
137 | func_ptr *p; | |
138 | ||
139 | for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--) | |
140 | (*p) (); | |
141 | } | |
142 | ||
143 | /* .init section end. | |
144 | This must live at the end of the .init section. */ | |
145 | ||
ab87f8c8 JL |
146 | asm ("\n\ |
147 | .section .init,\"ax\",@progbits\n\ | |
148 | bl __do_global_ctors\n\ | |
149 | mv sp,fp\n\ | |
150 | pop lr\n\ | |
151 | pop fp\n\ | |
152 | jmp lr\n\ | |
153 | .fillinsn\n\ | |
8c5ca3b9 DE |
154 | "); |
155 | ||
156 | /* .fini section end. | |
157 | This must live at the end of the .fini section. */ | |
158 | ||
ab87f8c8 JL |
159 | asm ("\n\ |
160 | .section .fini,\"ax\",@progbits\n\ | |
161 | mv sp,fp\n\ | |
162 | pop lr\n\ | |
163 | pop fp\n\ | |
164 | jmp lr\n\ | |
165 | .fillinsn\n\ | |
8c5ca3b9 DE |
166 | "); |
167 | ||
168 | #endif /* CRT_FINI */ |