]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-fini.c
Fix ordering of DSO constructors and destructors.
[thirdparty/glibc.git] / elf / dl-fini.c
1 /* Call the termination functions of loaded shared objects.
2 Copyright (C) 1995,96,1998-2002,2004-2005,2009,2011
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
20
21 #include <alloca.h>
22 #include <assert.h>
23 #include <string.h>
24 #include <ldsodefs.h>
25
26
27 /* Type of the constructor functions. */
28 typedef void (*fini_t) (void);
29
30
31 void
32 internal_function
33 _dl_sort_fini (struct link_map *l, struct link_map **maps, size_t nmaps,
34 char *used, Lmid_t ns)
35 {
36 /* We can skip looking for the binary itself which is at the front
37 of the search list for the main namespace. */
38 assert (nmaps > 1);
39 unsigned int i = ns == LM_ID_BASE;
40 bool seen[nmaps];
41 memset (seen, false, nmaps * sizeof (seen[0]));
42 while (1)
43 {
44 /* Keep track of which object we looked at this round. */
45 seen[i] = true;
46 struct link_map *thisp = maps[i];
47
48 /* Do not handle ld.so in secondary namespaces and object which
49 are not removed. */
50 if (thisp != thisp->l_real || thisp->l_idx == -1)
51 goto skip;
52
53 /* Find the last object in the list for which the current one is
54 a dependency and move the current object behind the object
55 with the dependency. */
56 unsigned int k = nmaps - 1;
57 while (k > i)
58 {
59 struct link_map **runp = maps[k]->l_initfini;
60 if (runp != NULL)
61 /* Look through the dependencies of the object. */
62 while (*runp != NULL)
63 if (__builtin_expect (*runp++ == thisp, 0))
64 {
65 move:
66 /* Move the current object to the back past the last
67 object with it as the dependency. */
68 memmove (&maps[i], &maps[i + 1],
69 (k - i) * sizeof (maps[0]));
70 maps[k] = thisp;
71
72 if (used != NULL)
73 {
74 char here_used = used[i];
75 memmove (&used[i], &used[i + 1],
76 (k - i) * sizeof (used[0]));
77 used[k] = here_used;
78 }
79
80 if (seen[i + 1])
81 {
82 ++i;
83 goto next_clear;
84 }
85
86 memmove (&seen[i], &seen[i + 1], (k - i) * sizeof (seen[0]));
87 seen[k] = true;
88
89 goto next;
90 }
91
92 if (__builtin_expect (maps[k]->l_reldeps != NULL, 0))
93 {
94 unsigned int m = maps[k]->l_reldeps->act;
95 struct link_map **relmaps = &maps[k]->l_reldeps->list[0];
96
97 /* Look through the relocation dependencies of the object. */
98 while (m-- > 0)
99 if (__builtin_expect (relmaps[m] == thisp, 0))
100 goto move;
101 }
102
103 --k;
104 }
105
106 skip:
107 if (++i == nmaps)
108 break;
109 next_clear:
110 memset (&seen[i], false, (nmaps - i) * sizeof (seen[0]));
111
112 next:;
113 }
114 }
115
116
117 void
118 internal_function
119 _dl_fini (void)
120 {
121 /* Lots of fun ahead. We have to call the destructors for all still
122 loaded objects, in all namespaces. The problem is that the ELF
123 specification now demands that dependencies between the modules
124 are taken into account. I.e., the destructor for a module is
125 called before the ones for any of its dependencies.
126
127 To make things more complicated, we cannot simply use the reverse
128 order of the constructors. Since the user might have loaded objects
129 using `dlopen' there are possibly several other modules with its
130 dependencies to be taken into account. Therefore we have to start
131 determining the order of the modules once again from the beginning. */
132 struct link_map **maps = NULL;
133 size_t maps_size = 0;
134
135 /* We run the destructors of the main namespaces last. As for the
136 other namespaces, we pick run the destructors in them in reverse
137 order of the namespace ID. */
138 #ifdef SHARED
139 int do_audit = 0;
140 again:
141 #endif
142 for (Lmid_t ns = GL(dl_nns) - 1; ns >= 0; --ns)
143 {
144 /* Protect against concurrent loads and unloads. */
145 __rtld_lock_lock_recursive (GL(dl_load_lock));
146
147 unsigned int nmaps = 0;
148 unsigned int nloaded = GL(dl_ns)[ns]._ns_nloaded;
149 /* No need to do anything for empty namespaces or those used for
150 auditing DSOs. */
151 if (nloaded == 0
152 #ifdef SHARED
153 || GL(dl_ns)[ns]._ns_loaded->l_auditing != do_audit
154 #endif
155 )
156 goto out;
157
158 /* XXX Could it be (in static binaries) that there is no object
159 loaded? */
160 assert (ns != LM_ID_BASE || nloaded > 0);
161
162 /* Now we can allocate an array to hold all the pointers and copy
163 the pointers in. */
164 if (maps_size < nloaded * sizeof (struct link_map *))
165 {
166 if (maps_size == 0)
167 {
168 maps_size = nloaded * sizeof (struct link_map *);
169 maps = (struct link_map **) alloca (maps_size);
170 }
171 else
172 maps = (struct link_map **)
173 extend_alloca (maps, maps_size,
174 nloaded * sizeof (struct link_map *));
175 }
176
177 unsigned int i;
178 struct link_map *l;
179 assert (nloaded != 0 || GL(dl_ns)[ns]._ns_loaded == NULL);
180 for (l = GL(dl_ns)[ns]._ns_loaded, i = 0; l != NULL; l = l->l_next)
181 /* Do not handle ld.so in secondary namespaces. */
182 if (l == l->l_real)
183 {
184 assert (i < nloaded);
185
186 maps[i] = l;
187 l->l_idx = i;
188 ++i;
189
190 /* Bump l_direct_opencount of all objects so that they are
191 not dlclose()ed from underneath us. */
192 ++l->l_direct_opencount;
193 }
194 assert (ns != LM_ID_BASE || i == nloaded);
195 assert (ns == LM_ID_BASE || i == nloaded || i == nloaded - 1);
196 nmaps = i;
197
198 if (nmaps > 1)
199 /* Now we have to do the sorting. */
200 _dl_sort_fini (GL(dl_ns)[ns]._ns_loaded, maps, nmaps, NULL, ns);
201
202 /* We do not rely on the linked list of loaded object anymore from
203 this point on. We have our own list here (maps). The various
204 members of this list cannot vanish since the open count is too
205 high and will be decremented in this loop. So we release the
206 lock so that some code which might be called from a destructor
207 can directly or indirectly access the lock. */
208 out:
209 __rtld_lock_unlock_recursive (GL(dl_load_lock));
210
211 /* 'maps' now contains the objects in the right order. Now call the
212 destructors. We have to process this array from the front. */
213 for (i = 0; i < nmaps; ++i)
214 {
215 l = maps[i];
216
217 if (l->l_init_called)
218 {
219 /* Make sure nothing happens if we are called twice. */
220 l->l_init_called = 0;
221
222 /* Is there a destructor function? */
223 if (l->l_info[DT_FINI_ARRAY] != NULL
224 || l->l_info[DT_FINI] != NULL)
225 {
226 /* When debugging print a message first. */
227 if (__builtin_expect (GLRO(dl_debug_mask)
228 & DL_DEBUG_IMPCALLS, 0))
229 _dl_debug_printf ("\ncalling fini: %s [%lu]\n\n",
230 l->l_name[0] ? l->l_name : rtld_progname,
231 ns);
232
233 /* First see whether an array is given. */
234 if (l->l_info[DT_FINI_ARRAY] != NULL)
235 {
236 ElfW(Addr) *array =
237 (ElfW(Addr) *) (l->l_addr
238 + l->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
239 unsigned int i = (l->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
240 / sizeof (ElfW(Addr)));
241 while (i-- > 0)
242 ((fini_t) array[i]) ();
243 }
244
245 /* Next try the old-style destructor. */
246 if (l->l_info[DT_FINI] != NULL)
247 ((fini_t) DL_DT_FINI_ADDRESS (l, l->l_addr + l->l_info[DT_FINI]->d_un.d_ptr)) ();
248 }
249
250 #ifdef SHARED
251 /* Auditing checkpoint: another object closed. */
252 if (!do_audit && __builtin_expect (GLRO(dl_naudit) > 0, 0))
253 {
254 struct audit_ifaces *afct = GLRO(dl_audit);
255 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
256 {
257 if (afct->objclose != NULL)
258 /* Return value is ignored. */
259 (void) afct->objclose (&l->l_audit[cnt].cookie);
260
261 afct = afct->next;
262 }
263 }
264 #endif
265 }
266
267 /* Correct the previous increment. */
268 --l->l_direct_opencount;
269 }
270 }
271
272 #ifdef SHARED
273 if (! do_audit && GLRO(dl_naudit) > 0)
274 {
275 do_audit = 1;
276 goto again;
277 }
278
279 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS, 0))
280 _dl_debug_printf ("\nruntime linker statistics:\n"
281 " final number of relocations: %lu\n"
282 "final number of relocations from cache: %lu\n",
283 GL(dl_num_relocations),
284 GL(dl_num_cache_relocations));
285 #endif
286 }