* sysdeps/m68k/fpu/e_log2f.c: Fix typo.
[glibc.git] / elf / dl-close.c
blob10a906d12e1c8ab71633d57432a73e68852dd250
1 /* Close a shared object opened by `_dl_open'.
2 Copyright (C) 1996,1997,1998,1999,2000,2001 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <assert.h>
21 #include <dlfcn.h>
22 #include <libintl.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <ldsodefs.h>
27 #include <sys/types.h>
28 #include <sys/mman.h>
31 /* Type of the constructor functions. */
32 typedef void (*fini_t) (void);
35 /* During the program run we must not modify the global data of
36 loaded shared object simultanously in two threads. Therefore we
37 protect `dlopen' and `dlclose' in dlclose.c. */
38 __libc_lock_define (extern, _dl_load_lock)
40 void
41 internal_function
42 _dl_close (void *_map)
44 struct reldep_list
46 struct link_map **rellist;
47 unsigned int nrellist;
48 struct reldep_list *next;
49 } *reldeps = NULL;
50 struct link_map **list;
51 struct link_map *map = _map;
52 unsigned int i;
53 unsigned int *new_opencount;
55 /* First see whether we can remove the object at all. */
56 if ((map->l_flags_1 & DF_1_NODELETE) && map->l_init_called)
57 /* Nope. Do nothing. */
58 return;
60 if (__builtin_expect (map->l_opencount, 1) == 0)
61 _dl_signal_error (0, map->l_name, N_("shared object not open"));
63 /* Acquire the lock. */
64 __libc_lock_lock (_dl_load_lock);
66 /* Decrement the reference count. */
67 if (map->l_opencount > 1 || map->l_type != lt_loaded)
69 /* There are still references to this object. Do nothing more. */
70 if (//__builtin_expect (_dl_debug_files, 0))
71 __builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0))
73 char buf[20];
75 buf[sizeof buf - 1] = '\0';
77 _dl_debug_printf ("\nclosing file=%s; opencount == %u\n",
78 map->l_name, map->l_opencount);
81 /* One decrement the object itself, not the dependencies. */
82 --map->l_opencount;
84 __libc_lock_unlock (_dl_load_lock);
85 return;
88 list = map->l_initfini;
90 /* Compute the new l_opencount values. */
91 new_opencount = (unsigned int *) alloca (map->l_searchlist.r_nlist
92 * sizeof (unsigned int));
93 for (i = 0; list[i] != NULL; ++i)
95 list[i]->l_idx = i;
96 new_opencount[i] = list[i]->l_opencount;
98 --new_opencount[0];
99 for (i = 1; list[i] != NULL; ++i)
100 if ((! (list[i]->l_flags_1 & DF_1_NODELETE) || ! list[i]->l_init_called)
101 /* Decrement counter. */
102 && --new_opencount[i] == 0
103 /* Test whether this object was also loaded directly. */
104 && list[i]->l_searchlist.r_list != NULL)
106 /* In this case we have the decrement all the dependencies of
107 this object. They are all in MAP's dependency list. */
108 unsigned int j;
109 struct link_map **dep_list = list[i]->l_searchlist.r_list;
111 for (j = 1; j < list[i]->l_searchlist.r_nlist; ++j)
112 if (! (dep_list[j]->l_flags_1 & DF_1_NODELETE)
113 || ! dep_list[j]->l_init_called)
115 assert (dep_list[j]->l_idx < map->l_searchlist.r_nlist);
116 --new_opencount[dep_list[j]->l_idx];
119 assert (new_opencount[0] == 0);
121 /* Call all termination functions at once. */
122 for (i = 0; list[i] != NULL; ++i)
124 struct link_map *imap = list[i];
125 if (new_opencount[i] == 0 && imap->l_type == lt_loaded
126 && (imap->l_info[DT_FINI] || imap->l_info[DT_FINI_ARRAY])
127 && (! (imap->l_flags_1 & DF_1_NODELETE) || ! imap->l_init_called)
128 /* Skip any half-cooked objects that were never initialized. */
129 && imap->l_init_called)
131 /* When debugging print a message first. */
132 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_IMPCALLS, 0))
133 _dl_debug_printf ("\ncalling fini: %s\n\n", imap->l_name);
135 /* Call its termination function. */
136 if (imap->l_info[DT_FINI_ARRAY] != NULL)
138 ElfW(Addr) *array =
139 (ElfW(Addr) *) (imap->l_addr
140 + imap->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
141 unsigned int sz = (imap->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
142 / sizeof (ElfW(Addr)));
143 unsigned int cnt;
145 for (cnt = 0; cnt < sz; ++cnt)
146 ((fini_t) (imap->l_addr + array[cnt])) ();
149 /* Next try the old-style destructor. */
150 if (imap->l_info[DT_FINI] != NULL)
151 (*(void (*) (void)) DL_DT_FINI_ADDRESS
152 (imap, (void *) imap->l_addr
153 + imap->l_info[DT_FINI]->d_un.d_ptr)) ();
156 /* Store the new l_opencount value. */
157 imap->l_opencount = new_opencount[i];
158 /* Just a sanity check. */
159 assert (imap->l_type == lt_loaded || imap->l_opencount > 0);
162 /* Notify the debugger we are about to remove some loaded objects. */
163 _r_debug.r_state = RT_DELETE;
164 _dl_debug_state ();
166 /* Check each element of the search list to see if all references to
167 it are gone. */
168 for (i = 0; list[i] != NULL; ++i)
170 struct link_map *imap = list[i];
171 if (imap->l_opencount == 0 && imap->l_type == lt_loaded)
173 struct libname_list *lnp;
175 /* That was the last reference, and this was a dlopen-loaded
176 object. We can unmap it. */
177 if (__builtin_expect (imap->l_global, 0))
179 /* This object is in the global scope list. Remove it. */
180 unsigned int cnt = _dl_main_searchlist->r_nlist;
183 --cnt;
184 while (_dl_main_searchlist->r_list[cnt] != imap);
186 /* The object was already correctly registered. */
187 while (++cnt < _dl_main_searchlist->r_nlist)
188 _dl_main_searchlist->r_list[cnt - 1]
189 = _dl_main_searchlist->r_list[cnt];
191 --_dl_main_searchlist->r_nlist;
194 /* We can unmap all the maps at once. We determined the
195 start address and length when we loaded the object and
196 the `munmap' call does the rest. */
197 DL_UNMAP (imap);
199 /* Finally, unlink the data structure and free it. */
200 #ifdef SHARED
201 /* We will unlink the first object only if this is a statically
202 linked program. */
203 assert (imap->l_prev != NULL);
204 imap->l_prev->l_next = imap->l_next;
205 #else
206 if (imap->l_prev != NULL)
207 imap->l_prev->l_next = imap->l_next;
208 else
209 _dl_loaded = imap->l_next;
210 #endif
211 --_dl_nloaded;
212 if (imap->l_next)
213 imap->l_next->l_prev = imap->l_prev;
215 if (imap->l_versions != NULL)
216 free (imap->l_versions);
217 if (imap->l_origin != NULL && imap->l_origin != (char *) -1)
218 free ((char *) imap->l_origin);
220 /* If the object has relocation dependencies save this
221 information for latter. */
222 if (__builtin_expect (imap->l_reldeps != NULL, 0))
224 struct reldep_list *newrel;
226 newrel = (struct reldep_list *) alloca (sizeof (*reldeps));
227 newrel->rellist = imap->l_reldeps;
228 newrel->nrellist = imap->l_reldepsact;
229 newrel->next = reldeps;
231 reldeps = newrel;
234 /* This name always is allocated. */
235 free (imap->l_name);
236 /* Remove the list with all the names of the shared object. */
237 lnp = imap->l_libname;
240 struct libname_list *this = lnp;
241 lnp = lnp->next;
242 free (this);
244 while (lnp != NULL);
246 /* Remove the searchlists. */
247 if (imap != map)
248 free (imap->l_initfini);
250 if (imap->l_phdr_allocated)
251 free ((void *) imap->l_phdr);
253 if (imap->l_rpath_dirs.dirs != (void *) -1)
254 free (imap->l_rpath_dirs.dirs);
255 if (imap->l_runpath_dirs.dirs != (void *) -1)
256 free (imap->l_runpath_dirs.dirs);
258 free (imap);
262 /* Notify the debugger those objects are finalized and gone. */
263 _r_debug.r_state = RT_CONSISTENT;
264 _dl_debug_state ();
266 /* Now we can perhaps also remove the modules for which we had
267 dependencies because of symbol lookup. */
268 while (__builtin_expect (reldeps != NULL, 0))
270 while (reldeps->nrellist-- > 0)
271 _dl_close (reldeps->rellist[reldeps->nrellist]);
273 free (reldeps->rellist);
275 reldeps = reldeps->next;
278 free (list);
280 /* Release the lock. */
281 __libc_lock_unlock (_dl_load_lock);
285 static void
286 free_mem (void)
288 if (__builtin_expect (_dl_global_scope_alloc, 0) != 0
289 && _dl_main_searchlist->r_nlist == _dl_initial_searchlist.r_nlist)
291 /* All object dynamically loaded by the program are unloaded. Free
292 the memory allocated for the global scope variable. */
293 struct link_map **old = _dl_main_searchlist->r_list;
295 /* Put the old map in. */
296 _dl_main_searchlist->r_list = _dl_initial_searchlist.r_list;
297 /* Signal that the original map is used. */
298 _dl_global_scope_alloc = 0;
300 /* Now free the old map. */
301 free (old);
304 text_set_element (__libc_subfreeres, free_mem);