2.3.4-2.fc3.5
[glibc.git] / elf / dl-fini.c
blob09643968288f477f21ae4f9c08cbd4cc67530aa7
1 /* Call the termination functions of loaded shared objects.
2 Copyright (C) 1995,96,1998-2002,2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
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. */
20 #include <alloca.h>
21 #include <assert.h>
22 #include <string.h>
23 #include <ldsodefs.h>
26 void
27 internal_function
28 _dl_sort_fini (struct link_map *l, struct link_map **maps, size_t nmaps,
29 char *used, Lmid_t ns)
31 if (ns == LM_ID_BASE)
32 /* The main executable always comes first. */
33 l = l->l_next;
35 for (; l != NULL; l = l->l_next)
36 /* Do not handle ld.so in secondary namespaces and object which
37 are not removed. */
38 if (l == l->l_real && l->l_idx != -1)
40 /* Find the place in the 'maps' array. */
41 unsigned int j;
42 for (j = ns == LM_ID_BASE ? 1 : 0; maps[j] != l; ++j)
43 assert (j < nmaps);
45 /* Find all object for which the current one is a dependency
46 and move the found object (if necessary) in front. */
47 for (unsigned int k = j + 1; k < nmaps; ++k)
49 struct link_map **runp = maps[k]->l_initfini;
50 if (runp != NULL)
52 while (*runp != NULL)
53 if (*runp == l)
55 struct link_map *here = maps[k];
57 /* Move it now. */
58 memmove (&maps[j] + 1,
59 &maps[j], (k - j) * sizeof (struct link_map *));
60 maps[j] = here;
62 if (used != NULL)
64 char here_used = used[k];
66 memmove (&used[j] + 1,
67 &used[j], (k - j) * sizeof (char));
68 used[j] = here_used;
71 ++j;
73 break;
75 else
76 ++runp;
79 if (__builtin_expect (maps[k]->l_reldeps != NULL, 0))
81 unsigned int m = maps[k]->l_reldepsact;
82 struct link_map **relmaps = maps[k]->l_reldeps;
84 while (m-- > 0)
86 if (relmaps[m] == l)
88 struct link_map *here = maps[k];
90 /* Move it now. */
91 memmove (&maps[j] + 1,
92 &maps[j],
93 (k - j) * sizeof (struct link_map *));
94 maps[j] = here;
96 if (used != NULL)
98 char here_used = used[k];
100 memmove (&used[j] + 1,
101 &used[j], (k - j) * sizeof (char));
102 used[j] = here_used;
105 break;
113 #if !defined SHARED || defined IS_IN_rtld
114 /* Type of the constructor functions. */
115 typedef void (*fini_t) (void);
117 void
118 internal_function
119 _dl_fini (void)
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.
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;
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 for (Lmid_t ns = DL_NNS - 1; ns >= 0; --ns)
140 /* Protect against concurrent loads and unloads. */
141 __rtld_lock_lock_recursive (GL(dl_load_lock));
143 unsigned int nloaded = GL(dl_ns)[ns]._ns_nloaded;
145 /* XXX Could it be (in static binaries) that there is no object
146 loaded? */
147 assert (ns != LM_ID_BASE || nloaded > 0);
149 /* Now we can allocate an array to hold all the pointers and copy
150 the pointers in. */
151 if (maps_size < nloaded * sizeof (struct link_map *))
153 if (maps_size == 0)
155 maps_size = nloaded * sizeof (struct link_map *);
156 maps = (struct link_map **) alloca (maps_size);
158 else
159 maps = (struct link_map **)
160 extend_alloca (maps, maps_size,
161 nloaded * sizeof (struct link_map *));
164 unsigned int i;
165 struct link_map *l;
166 for (l = GL(dl_ns)[ns]._ns_loaded, i = 0; l != NULL; l = l->l_next)
167 /* Do not handle ld.so in secondary namespaces. */
168 if (l == l->l_real)
170 assert (i < nloaded);
172 maps[i] = l;
173 l->l_idx = i;
174 ++i;
176 /* Bump l_direct_opencount of all objects so that they are
177 not dlclose()ed from underneath us. */
178 ++l->l_direct_opencount;
180 assert (ns != LM_ID_BASE || i == nloaded);
181 assert (ns == LM_ID_BASE || i == nloaded || i == nloaded - 1);
182 unsigned int nmaps = i;
184 if (nmaps != 0)
185 /* Now we have to do the sorting. */
186 _dl_sort_fini (GL(dl_ns)[ns]._ns_loaded, maps, nmaps, NULL, ns);
188 /* We do not rely on the linked list of loaded object anymore from
189 this point on. We have our own list here (maps). The various
190 members of this list cannot vanish since the open count is too
191 high and will be decremented in this loop. So we release the
192 lock so that some code which might be called from a destructor
193 can directly or indirectly access the lock. */
194 __rtld_lock_unlock_recursive (GL(dl_load_lock));
196 /* 'maps' now contains the objects in the right order. Now call the
197 destructors. We have to process this array from the front. */
198 for (i = 0; i < nmaps; ++i)
200 l = maps[i];
202 if (l->l_init_called)
204 /* Make sure nothing happens if we are called twice. */
205 l->l_init_called = 0;
207 /* Don't call the destructors for objects we are not
208 supposed to. */
209 if (l->l_name[0] == '\0' && l->l_type == lt_executable)
210 continue;
212 /* Is there a destructor function? */
213 if (l->l_info[DT_FINI_ARRAY] == NULL
214 && l->l_info[DT_FINI] == NULL)
215 continue;
217 /* When debugging print a message first. */
218 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS,
220 _dl_debug_printf ("\ncalling fini: %s [%lu]\n\n",
221 l->l_name[0] ? l->l_name : rtld_progname,
222 ns);
224 /* First see whether an array is given. */
225 if (l->l_info[DT_FINI_ARRAY] != NULL)
227 ElfW(Addr) *array =
228 (ElfW(Addr) *) (l->l_addr
229 + l->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
230 unsigned int i = (l->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
231 / sizeof (ElfW(Addr)));
232 while (i-- > 0)
233 ((fini_t) array[i]) ();
236 /* Next try the old-style destructor. */
237 if (l->l_info[DT_FINI] != NULL)
238 ((fini_t) DL_DT_FINI_ADDRESS (l, l->l_addr + l->l_info[DT_FINI]->d_un.d_ptr)) ();
241 /* Correct the previous increment. */
242 --l->l_direct_opencount;
246 #ifdef SHARED
247 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS, 0))
248 _dl_debug_printf ("\nruntime linker statistics:\n"
249 " final number of relocations: %lu\n"
250 "final number of relocations from cache: %lu\n",
251 GL(dl_num_relocations),
252 GL(dl_num_cache_relocations));
253 #endif
255 #endif