]> sourceware.org Git - glibc.git/blame - elf/dl-close.c
Update.
[glibc.git] / elf / dl-close.c
CommitLineData
26b4d766 1/* Close a shared object opened by `_dl_open'.
dacc8ffa 2 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
afd4eb37
UD
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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. */
ba79d61b 19
7afab53d 20#include <assert.h>
ba79d61b 21#include <dlfcn.h>
8e17ea58 22#include <libintl.h>
ba79d61b 23#include <stdlib.h>
8d6468d0 24#include <string.h>
a853022c 25#include <bits/libc-lock.h>
b8445829 26#include <ldsodefs.h>
ba79d61b
RM
27#include <sys/types.h>
28#include <sys/mman.h>
29
b35e21f4
UD
30#include <stdio-common/_itoa.h>
31
ba79d61b 32
dacc8ffa
UD
33/* Type of the constructor functions. */
34typedef void (*fini_t) (void);
35
36
26b4d766
UD
37/* During the program run we must not modify the global data of
38 loaded shared object simultanously in two threads. Therefore we
39 protect `dlopen' and `dlclose' in dlclose.c. */
40__libc_lock_define (extern, _dl_load_lock)
41
ba79d61b 42void
d0fc4041 43internal_function
94e365c6 44_dl_close (void *_map)
ba79d61b
RM
45{
46 struct link_map **list;
cf197e41 47 struct link_map **rellist;
94e365c6 48 struct link_map *map = _map;
cf197e41
UD
49 unsigned int nsearchlist;
50 unsigned int nrellist;
ba79d61b
RM
51 unsigned int i;
52
bf8b3e74
UD
53 /* First see whether we can remove the object at all. */
54 if (map->l_flags_1 & DF_1_NODELETE)
55 /* Nope. Do nothing. */
56 return;
57
ba79d61b 58 if (map->l_opencount == 0)
8e17ea58 59 _dl_signal_error (0, map->l_name, N_("shared object not open"));
ba79d61b 60
26b4d766
UD
61 /* Acquire the lock. */
62 __libc_lock_lock (_dl_load_lock);
63
8699e7b1
UD
64 list = map->l_searchlist.r_list;
65 nsearchlist = map->l_searchlist.r_nlist;
66
ba79d61b 67 /* Decrement the reference count. */
a709dd43 68 if (map->l_opencount > 1 || map->l_type != lt_loaded)
26b4d766
UD
69 {
70 /* There are still references to this object. Do nothing more. */
b35e21f4
UD
71 if (__builtin_expect (_dl_debug_files, 0))
72 {
73 char buf[20];
74
75 buf[sizeof buf - 1] = '\0';
76
77 _dl_debug_message (1, "\nclosing file=", map->l_name,
78 "; opencount == ",
79 _itoa_word (map->l_opencount,
80 buf + sizeof buf - 1, 10, 0),
81 "\n", NULL);
82 }
83
8699e7b1
UD
84 for (i = 0; i < nsearchlist; ++i)
85 if (! (list[i]->l_flags_1 & DF_1_NODELETE))
86 --list[i]->l_opencount;
87
26b4d766
UD
88 __libc_lock_unlock (_dl_load_lock);
89 return;
90 }
ba79d61b 91
cf197e41
UD
92 rellist = map->l_reldeps;
93 nrellist = map->l_reldepsact;
94
a709dd43 95 /* Call all termination functions at once. */
6075607b 96 for (i = 0; i < nsearchlist; ++i)
a709dd43 97 {
dacc8ffa 98 struct link_map *imap = map->l_initfini[i];
b48abe3c 99 if (imap->l_opencount == 1 && imap->l_type == lt_loaded
dacc8ffa 100 && (imap->l_info[DT_FINI] || imap->l_info[DT_FINI_ARRAY])
172b90bb 101 && ! (imap->l_flags_1 & DF_1_NODELETE)
7a68c94a
UD
102 /* Skip any half-cooked objects that were never initialized. */
103 && imap->l_init_called)
a709dd43 104 {
b48abe3c 105 /* When debugging print a message first. */
466a0ec9 106 if (__builtin_expect (_dl_debug_impcalls, 0))
b48abe3c
UD
107 _dl_debug_message (1, "\ncalling fini: ", imap->l_name,
108 "\n\n", NULL);
dacc8ffa 109
b48abe3c 110 /* Call its termination function. */
dacc8ffa
UD
111 if (imap->l_info[DT_FINI_ARRAY] != NULL)
112 {
113 ElfW(Addr) *array =
114 (ElfW(Addr) *) (imap->l_addr
115 + imap->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
116 unsigned int sz = (imap->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
117 / sizeof (ElfW(Addr)));
118 unsigned int cnt;
119
120 for (cnt = 0; cnt < sz; ++cnt)
121 ((fini_t) (imap->l_addr + array[cnt])) ();
122 }
123
124 /* Next try the old-style destructor. */
125 if (imap->l_info[DT_FINI] != NULL)
126 (*(void (*) (void)) ((void *) imap->l_addr
127 + imap->l_info[DT_FINI]->d_un.d_ptr)) ();
a709dd43
UD
128 }
129 }
130
4d6acc61
RM
131 /* Notify the debugger we are about to remove some loaded objects. */
132 _r_debug.r_state = RT_DELETE;
133 _dl_debug_state ();
134
ba79d61b
RM
135 /* The search list contains a counted reference to each object it
136 points to, the 0th elt being MAP itself. Decrement the reference
137 counts on all the objects MAP depends on. */
6075607b 138 for (i = 0; i < nsearchlist; ++i)
bf8b3e74
UD
139 if (! (list[i]->l_flags_1 & DF_1_NODELETE))
140 --list[i]->l_opencount;
ba79d61b 141
ba79d61b
RM
142 /* Check each element of the search list to see if all references to
143 it are gone. */
6075607b 144 for (i = 0; i < nsearchlist; ++i)
ba79d61b 145 {
af69217f
UD
146 struct link_map *imap = list[i];
147 if (imap->l_opencount == 0 && imap->l_type == lt_loaded)
ba79d61b 148 {
a8a1269d
UD
149 struct libname_list *lnp;
150
ba79d61b
RM
151 /* That was the last reference, and this was a dlopen-loaded
152 object. We can unmap it. */
af69217f 153 if (imap->l_global)
ba79d61b
RM
154 {
155 /* This object is in the global scope list. Remove it. */
82df2969 156 int cnt = _dl_main_searchlist->r_nlist;
be935610 157
ba79d61b 158 do
50b65db1 159 --cnt;
be935610 160 while (_dl_main_searchlist->r_list[cnt] != imap);
482eec0d 161
50b65db1
UD
162 /* The object was already correctly registered. */
163 while (++cnt < _dl_main_searchlist->r_nlist)
164 _dl_main_searchlist->r_list[cnt - 1]
165 = _dl_main_searchlist->r_list[cnt];
166
167 --_dl_main_searchlist->r_nlist;
ba79d61b
RM
168 }
169
a8a1269d 170 /* We can unmap all the maps at once. We determined the
4ce636da
UD
171 start address and length when we loaded the object and
172 the `munmap' call does the rest. */
09bf6406 173 DL_UNMAP (imap);
22bc7978 174
ba79d61b 175 /* Finally, unlink the data structure and free it. */
b5567b2a 176#ifdef SHARED
7afab53d
UD
177 /* We will unlink the first object only if this is a statically
178 linked program. */
179 assert (imap->l_prev != NULL);
6a805a0b 180 imap->l_prev->l_next = imap->l_next;
7afab53d
UD
181#else
182 if (imap->l_prev != NULL)
af69217f 183 imap->l_prev->l_next = imap->l_next;
7afab53d
UD
184 else
185 _dl_loaded = imap->l_next;
186#endif
1ebba33e 187 --_dl_nloaded;
af69217f
UD
188 if (imap->l_next)
189 imap->l_next->l_prev = imap->l_prev;
a8a1269d
UD
190
191 if (imap->l_versions != NULL)
192 free (imap->l_versions);
1c3a6f19 193 if (imap->l_origin != NULL && imap->l_origin != (char *) -1)
a8a1269d
UD
194 free ((char *) imap->l_origin);
195
4ce636da 196 /* This name always is allocated. */
a8a1269d 197 free (imap->l_name);
4ce636da 198 /* Remove the list with all the names of the shared object. */
a8a1269d
UD
199 lnp = imap->l_libname;
200 do
201 {
76156ea1 202 struct libname_list *this = lnp;
a8a1269d 203 lnp = lnp->next;
76156ea1 204 free (this);
a8a1269d
UD
205 }
206 while (lnp != NULL);
a8a1269d 207
4ce636da 208 /* Remove the searchlists. */
dacc8ffa 209 if (imap != map)
4ce636da 210 {
07a3d63e
UD
211 if (imap->l_searchlist.r_list != NULL)
212 free (imap->l_searchlist.r_list);
213 else if (imap->l_initfini != NULL)
214 free (imap->l_initfini);
4ce636da 215 }
4ce636da 216
7bcaca43 217 if (imap->l_phdr_allocated)
15925412 218 free ((void *) imap->l_phdr);
7bcaca43 219
f55727ca
UD
220 if (imap->l_rpath_dirs.dirs != (void *) -1)
221 free (imap->l_rpath_dirs.dirs);
222 if (imap->l_runpath_dirs.dirs != (void *) -1)
223 free (imap->l_runpath_dirs.dirs);
224
af69217f 225 free (imap);
ba79d61b
RM
226 }
227 }
228
cf197e41
UD
229 /* Now we can perhaps also remove the modules for which we had
230 dependencies because of symbol lookup. */
231 if (rellist != NULL)
232 {
233 while (nrellist-- > 0)
234 _dl_close (rellist[nrellist]);
235
236 free (rellist);
237 }
238
ba79d61b 239 free (list);
4d6acc61 240
d3556ac9
UD
241 if (_dl_global_scope_alloc != 0
242 && _dl_main_searchlist->r_nlist == _dl_initial_searchlist.r_nlist)
243 {
244 /* All object dynamically loaded by the program are unloaded. Free
245 the memory allocated for the global scope variable. */
246 struct link_map **old = _dl_main_searchlist->r_list;
247
248 /* Put the old map in. */
249 _dl_main_searchlist->r_list = _dl_initial_searchlist.r_list;
250 /* Signal that the original map is used. */
251 _dl_global_scope_alloc = 0;
252
253 /* Now free the old map. */
254 free (old);
255 }
256
4d6acc61
RM
257 /* Notify the debugger those objects are finalized and gone. */
258 _r_debug.r_state = RT_CONSISTENT;
259 _dl_debug_state ();
26b4d766
UD
260
261 /* Release the lock. */
262 __libc_lock_unlock (_dl_load_lock);
ba79d61b 263}
This page took 0.142063 seconds and 5 git commands to generate.