]>
Commit | Line | Data |
---|---|---|
d66e34cd | 1 | /* Call the termination functions of loaded shared objects. |
a461b147 | 2 | Copyright (C) 1995,96,1998-2002,2004 Free Software Foundation, Inc. |
afd4eb37 | 3 | This file is part of the GNU C Library. |
d66e34cd | 4 | |
afd4eb37 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
d66e34cd | 9 | |
afd4eb37 UD |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
d66e34cd | 14 | |
41bdb6e2 AJ |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
d66e34cd | 19 | |
e7c036b3 | 20 | #include <alloca.h> |
dacc8ffa UD |
21 | #include <assert.h> |
22 | #include <string.h> | |
a42195db | 23 | #include <ldsodefs.h> |
d66e34cd | 24 | |
dacc8ffa UD |
25 | |
26 | /* Type of the constructor functions. */ | |
27 | typedef void (*fini_t) (void); | |
28 | ||
29 | ||
d66e34cd | 30 | void |
d0fc4041 | 31 | internal_function |
d66e34cd RM |
32 | _dl_fini (void) |
33 | { | |
dacc8ffa | 34 | /* Lots of fun ahead. We have to call the destructors for all still |
c0f62c56 UD |
35 | loaded objects, in all namespaces. The problem is that the ELF |
36 | specification now demands that dependencies between the modules | |
37 | are taken into account. I.e., the destructor for a module is | |
38 | called before the ones for any of its dependencies. | |
dacc8ffa UD |
39 | |
40 | To make things more complicated, we cannot simply use the reverse | |
41 | order of the constructors. Since the user might have loaded objects | |
42 | using `dlopen' there are possibly several other modules with its | |
43 | dependencies to be taken into account. Therefore we have to start | |
44 | determining the order of the modules once again from the beginning. */ | |
dacc8ffa | 45 | unsigned int i; |
28725d82 | 46 | unsigned int nloaded; |
d66e34cd | 47 | struct link_map *l; |
c0f62c56 UD |
48 | struct link_map **maps = NULL; |
49 | size_t maps_size = 0; | |
50 | ||
51 | /* We First run the destructors of the main namespaces, then the | |
52 | other ones. The order should not matter since the namespace | |
53 | content is supposed to be independent. But we can have auditing | |
54 | code in a auxiliaty namespace and we want it to monitor the | |
55 | destructors. */ | |
56 | for (Lmid_t cnt = 0; cnt < DL_NNS; ++cnt) | |
3b3938c9 | 57 | { |
c0f62c56 UD |
58 | /* Protect against concurrent loads and unloads. */ |
59 | __rtld_lock_lock_recursive (GL(dl_load_lock)); | |
1ebba33e | 60 | |
c0f62c56 | 61 | nloaded = GL(dl_ns)[cnt]._ns_nloaded; |
3b3938c9 | 62 | |
c0f62c56 UD |
63 | /* XXX Could it be (in static binaries) that there is no object |
64 | loaded? */ | |
65 | assert (cnt != LM_ID_BASE || nloaded > 0); | |
dacc8ffa | 66 | |
c0f62c56 UD |
67 | /* Now we can allocate an array to hold all the pointers and copy |
68 | the pointers in. */ | |
69 | if (maps_size < nloaded * sizeof (struct link_map *)) | |
70 | { | |
71 | if (maps_size == 0) | |
72 | { | |
73 | maps_size = nloaded * sizeof (struct link_map *); | |
74 | maps = (struct link_map **) alloca (maps_size); | |
75 | } | |
76 | else | |
77 | maps = (struct link_map **) | |
78 | extend_alloca (maps, maps_size, | |
79 | nloaded * sizeof (struct link_map *)); | |
80 | } | |
dacc8ffa | 81 | |
c0f62c56 | 82 | for (l = GL(dl_ns)[cnt]._ns_loaded, i = 0; l != NULL; l = l->l_next) |
dacc8ffa | 83 | { |
c0f62c56 UD |
84 | assert (i < nloaded); |
85 | ||
86 | /* Do not handle ld.so in secondary namespaces. */ | |
87 | if (l == l->l_real) | |
dacc8ffa | 88 | { |
c0f62c56 UD |
89 | maps[i++] = l; |
90 | ||
91 | /* Bump l_opencount of all objects so that they are not | |
92 | dlclose()ed from underneath us. */ | |
93 | ++l->l_opencount; | |
dacc8ffa | 94 | } |
c0f62c56 UD |
95 | } |
96 | assert (cnt != LM_ID_BASE || i == nloaded); | |
97 | assert (cnt == LM_ID_BASE || i == nloaded || i == nloaded - 1); | |
98 | unsigned int nmaps = i; | |
cf197e41 | 99 | |
c0f62c56 UD |
100 | if (nmaps != 0) |
101 | { | |
102 | /* Now we have to do the sorting. */ | |
103 | l = GL(dl_ns)[cnt]._ns_loaded; | |
104 | if (cnt == LM_ID_BASE) | |
105 | /* The main executable always comes first. */ | |
106 | l = l->l_next; | |
107 | for (; l != NULL; l = l->l_next) | |
cf197e41 | 108 | { |
c0f62c56 UD |
109 | unsigned int j; |
110 | unsigned int k; | |
cf197e41 | 111 | |
c0f62c56 UD |
112 | /* Find the place in the 'maps' array. */ |
113 | for (j = 1; maps[j] != l; ++j) | |
114 | ; | |
115 | ||
116 | /* Find all object for which the current one is a dependency and | |
117 | move the found object (if necessary) in front. */ | |
118 | for (k = j + 1; k < nmaps; ++k) | |
cf197e41 | 119 | { |
c0f62c56 UD |
120 | struct link_map **runp = maps[k]->l_initfini; |
121 | if (runp != NULL) | |
cf197e41 | 122 | { |
c0f62c56 UD |
123 | while (*runp != NULL) |
124 | if (*runp == l) | |
125 | { | |
126 | struct link_map *here = maps[k]; | |
127 | ||
128 | /* Move it now. */ | |
129 | memmove (&maps[j] + 1, | |
130 | &maps[j], | |
131 | (k - j) * sizeof (struct link_map *)); | |
132 | maps[j++] = here; | |
133 | ||
134 | break; | |
135 | } | |
136 | else | |
137 | ++runp; | |
138 | } | |
cf197e41 | 139 | |
c0f62c56 UD |
140 | if (__builtin_expect (maps[k]->l_reldeps != NULL, 0)) |
141 | { | |
142 | unsigned int m = maps[k]->l_reldepsact; | |
143 | struct link_map **relmaps = maps[k]->l_reldeps; | |
144 | ||
145 | while (m-- > 0) | |
146 | { | |
147 | if (relmaps[m] == l) | |
148 | { | |
149 | struct link_map *here = maps[k]; | |
150 | ||
151 | /* Move it now. */ | |
152 | memmove (&maps[j] + 1, | |
153 | &maps[j], | |
154 | (k - j) * sizeof (struct link_map *)); | |
155 | maps[j] = here; | |
156 | ||
157 | break; | |
158 | } | |
159 | } | |
cf197e41 | 160 | } |
cf197e41 UD |
161 | } |
162 | } | |
dacc8ffa | 163 | } |
dacc8ffa | 164 | |
c0f62c56 UD |
165 | /* We do not rely on the linked list of loaded object anymore from |
166 | this point on. We have our own list here (maps). The various | |
167 | members of this list cannot vanish since the open count is too | |
168 | high and will be decremented in this loop. So we release the | |
169 | lock so that some code which might be called from a destructor | |
170 | can directly or indirectly access the lock. */ | |
171 | __rtld_lock_unlock_recursive (GL(dl_load_lock)); | |
172 | ||
173 | /* 'maps' now contains the objects in the right order. Now call the | |
174 | destructors. We have to process this array from the front. */ | |
175 | for (i = 0; i < nmaps; ++i) | |
dacc8ffa | 176 | { |
c0f62c56 | 177 | l = maps[i]; |
dacc8ffa | 178 | |
c0f62c56 | 179 | if (l->l_init_called) |
dacc8ffa | 180 | { |
c0f62c56 UD |
181 | /* Make sure nothing happens if we are called twice. */ |
182 | l->l_init_called = 0; | |
183 | ||
184 | /* Don't call the destructors for objects we are not | |
185 | supposed to. */ | |
186 | if (l->l_name[0] == '\0' && l->l_type == lt_executable) | |
187 | continue; | |
188 | ||
189 | /* Is there a destructor function? */ | |
190 | if (l->l_info[DT_FINI_ARRAY] == NULL | |
191 | && l->l_info[DT_FINI] == NULL) | |
192 | continue; | |
193 | ||
194 | /* When debugging print a message first. */ | |
195 | if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS, | |
196 | 0)) | |
197 | _dl_debug_printf ("\ncalling fini: %s [%lu]\n\n", | |
198 | l->l_name[0] ? l->l_name : rtld_progname, | |
199 | cnt); | |
200 | ||
201 | /* First see whether an array is given. */ | |
202 | if (l->l_info[DT_FINI_ARRAY] != NULL) | |
203 | { | |
204 | ElfW(Addr) *array = | |
205 | (ElfW(Addr) *) (l->l_addr | |
206 | + l->l_info[DT_FINI_ARRAY]->d_un.d_ptr); | |
207 | unsigned int i = (l->l_info[DT_FINI_ARRAYSZ]->d_un.d_val | |
208 | / sizeof (ElfW(Addr))); | |
209 | while (i-- > 0) | |
210 | ((fini_t) array[i]) (); | |
211 | } | |
212 | ||
213 | /* Next try the old-style destructor. */ | |
214 | if (l->l_info[DT_FINI] != NULL) | |
215 | ((fini_t) DL_DT_FINI_ADDRESS (l, l->l_addr + l->l_info[DT_FINI]->d_un.d_ptr)) (); | |
dacc8ffa | 216 | } |
d66e34cd | 217 | |
c0f62c56 UD |
218 | /* Correct the previous increment. */ |
219 | --l->l_opencount; | |
dacc8ffa UD |
220 | } |
221 | } | |
9836cfe7 | 222 | |
afdca0f2 | 223 | if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS, 0)) |
42af49f8 UD |
224 | _dl_debug_printf ("\nruntime linker statistics:\n" |
225 | " final number of relocations: %lu\n" | |
226 | "final number of relocations from cache: %lu\n", | |
227 | GL(dl_num_relocations), | |
228 | GL(dl_num_cache_relocations)); | |
d66e34cd | 229 | } |