1 |
282 |
jeremybenn |
/* .init/.fini section handling + C++ global constructor/destructor handling.
|
2 |
|
|
This file is based on crtstuff.c, sol2-crti.asm, sol2-crtn.asm.
|
3 |
|
|
|
4 |
|
|
Copyright (C) 1995, 1997, 1998, 2009 Free Software Foundation, Inc.
|
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
|
10 |
|
|
the Free Software Foundation; either version 3, or (at your option)
|
11 |
|
|
any later version.
|
12 |
|
|
|
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 |
|
|
|
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/>. */
|
26 |
|
|
|
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 |
|
|
|
44 |
|
|
static func_ptr __CTOR_LIST__[1] __attribute__ ((section (".ctors")))
|
45 |
|
|
= { (func_ptr) (-1) };
|
46 |
|
|
|
47 |
|
|
static func_ptr __DTOR_LIST__[1] __attribute__ ((section (".dtors")))
|
48 |
|
|
= { (func_ptr) (-1) };
|
49 |
|
|
|
50 |
|
|
/* Run all the global destructors on exit from the program. */
|
51 |
|
|
|
52 |
|
|
/* Some systems place the number of pointers in the first word of the
|
53 |
|
|
table. On SVR4 however, that word is -1. In all cases, the table is
|
54 |
|
|
null-terminated. On SVR4, we start from the beginning of the list and
|
55 |
|
|
invoke each per-compilation-unit destructor routine in order
|
56 |
|
|
until we find that null.
|
57 |
|
|
|
58 |
|
|
Note that this function MUST be static. There will be one of these
|
59 |
|
|
functions in each root executable and one in each shared library, but
|
60 |
|
|
although they all have the same code, each one is unique in that it
|
61 |
|
|
refers to one particular associated `__DTOR_LIST__' which belongs to the
|
62 |
|
|
same particular root executable or shared library file. */
|
63 |
|
|
|
64 |
|
|
static void __do_global_dtors (void)
|
65 |
|
|
asm ("__do_global_dtors") __attribute__ ((section (".text")));
|
66 |
|
|
|
67 |
|
|
static void
|
68 |
|
|
__do_global_dtors (void)
|
69 |
|
|
{
|
70 |
|
|
func_ptr *p;
|
71 |
|
|
for (p = __DTOR_LIST__ + 1; *p; p++)
|
72 |
|
|
(*p) ();
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
/* .init section start.
|
76 |
|
|
This must appear at the start of the .init section. */
|
77 |
|
|
|
78 |
|
|
asm ("\n\
|
79 |
|
|
.section .init\n\
|
80 |
|
|
.global init\n\
|
81 |
|
|
.word 0\n\
|
82 |
|
|
init:\n\
|
83 |
|
|
st blink,[sp,4]\n\
|
84 |
|
|
st fp,[sp]\n\
|
85 |
|
|
mov fp,sp\n\
|
86 |
|
|
sub sp,sp,16\n\
|
87 |
|
|
");
|
88 |
|
|
|
89 |
|
|
/* .fini section start.
|
90 |
|
|
This must appear at the start of the .init section. */
|
91 |
|
|
|
92 |
|
|
asm ("\n\
|
93 |
|
|
.section .fini\n\
|
94 |
|
|
.global fini\n\
|
95 |
|
|
.word 0\n\
|
96 |
|
|
fini:\n\
|
97 |
|
|
st blink,[sp,4]\n\
|
98 |
|
|
st fp,[sp]\n\
|
99 |
|
|
mov fp,sp\n\
|
100 |
|
|
sub sp,sp,16\n\
|
101 |
|
|
bl.nd __do_global_dtors\n\
|
102 |
|
|
");
|
103 |
|
|
|
104 |
|
|
#endif /* CRT_INIT */
|
105 |
|
|
|
106 |
|
|
#ifdef CRT_FINI
|
107 |
|
|
|
108 |
|
|
/* Put a word containing zero at the end of each of our two lists of function
|
109 |
|
|
addresses. Note that the words defined here go into the .ctors and .dtors
|
110 |
|
|
sections of the crtend.o file, and since that file is always linked in
|
111 |
|
|
last, these words naturally end up at the very ends of the two lists
|
112 |
|
|
contained in these two sections. */
|
113 |
|
|
|
114 |
|
|
static func_ptr __CTOR_END__[1] __attribute__ ((section (".ctors")))
|
115 |
|
|
= { (func_ptr) 0 };
|
116 |
|
|
|
117 |
|
|
static func_ptr __DTOR_END__[1] __attribute__ ((section (".dtors")))
|
118 |
|
|
= { (func_ptr) 0 };
|
119 |
|
|
|
120 |
|
|
/* Run all global constructors for the program.
|
121 |
|
|
Note that they are run in reverse order. */
|
122 |
|
|
|
123 |
|
|
static void __do_global_ctors (void)
|
124 |
|
|
asm ("__do_global_ctors") __attribute__ ((section (".text")));
|
125 |
|
|
|
126 |
|
|
static void
|
127 |
|
|
__do_global_ctors (void)
|
128 |
|
|
{
|
129 |
|
|
func_ptr *p;
|
130 |
|
|
for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
|
131 |
|
|
(*p) ();
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
/* .init section end.
|
135 |
|
|
This must live at the end of the .init section. */
|
136 |
|
|
|
137 |
|
|
asm ("\n\
|
138 |
|
|
.section .init\n\
|
139 |
|
|
bl.nd __do_global_ctors\n\
|
140 |
|
|
ld blink,[fp,4]\n\
|
141 |
|
|
j.d blink\n\
|
142 |
|
|
ld.a fp,[sp,16]\n\
|
143 |
|
|
");
|
144 |
|
|
|
145 |
|
|
/* .fini section end.
|
146 |
|
|
This must live at the end of the .fini section. */
|
147 |
|
|
|
148 |
|
|
asm ("\n\
|
149 |
|
|
.section .fini\n\
|
150 |
|
|
ld blink,[fp,4]\n\
|
151 |
|
|
j.d blink\n\
|
152 |
|
|
ld.a fp,[sp,16]\n\
|
153 |
|
|
");
|
154 |
|
|
|
155 |
|
|
#endif /* CRT_FINI */
|