Update.
[glibc.git] / elf / dl-close.c
blob99d52d02e2ae496722eca6e2bc0fa12892555ce4
1 /* Close a shared object opened by `_dl_open'.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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 link_map **list;
45 struct link_map **rellist;
46 struct link_map *map = _map;
47 unsigned int nsearchlist;
48 unsigned int nrellist;
49 unsigned int i;
51 /* First see whether we can remove the object at all. */
52 if (map->l_flags_1 & DF_1_NODELETE)
53 /* Nope. Do nothing. */
54 return;
56 if (map->l_opencount == 0)
57 _dl_signal_error (0, map->l_name, N_("shared object not open"));
59 /* Acquire the lock. */
60 __libc_lock_lock (_dl_load_lock);
62 /* Decrement the reference count. */
63 if (map->l_opencount > 1 || map->l_type != lt_loaded)
65 /* There are still references to this object. Do nothing more. */
66 --map->l_opencount;
67 __libc_lock_unlock (_dl_load_lock);
68 return;
71 list = map->l_searchlist.r_list;
72 nsearchlist = map->l_searchlist.r_nlist;
74 rellist = map->l_reldeps;
75 nrellist = map->l_reldepsact;
77 /* Call all termination functions at once. */
78 for (i = 0; i < nsearchlist; ++i)
80 struct link_map *imap = map->l_initfini[i];
81 if (imap->l_opencount == 1 && imap->l_type == lt_loaded
82 && (imap->l_info[DT_FINI] || imap->l_info[DT_FINI_ARRAY])
83 /* Skip any half-cooked objects that were never initialized. */
84 && imap->l_init_called)
86 /* When debugging print a message first. */
87 if (__builtin_expect (_dl_debug_impcalls, 0))
88 _dl_debug_message (1, "\ncalling fini: ", imap->l_name,
89 "\n\n", NULL);
91 /* Call its termination function. */
92 if (imap->l_info[DT_FINI_ARRAY] != NULL)
94 ElfW(Addr) *array =
95 (ElfW(Addr) *) (imap->l_addr
96 + imap->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
97 unsigned int sz = (imap->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
98 / sizeof (ElfW(Addr)));
99 unsigned int cnt;
101 for (cnt = 0; cnt < sz; ++cnt)
102 ((fini_t) (imap->l_addr + array[cnt])) ();
105 /* Next try the old-style destructor. */
106 if (imap->l_info[DT_FINI] != NULL)
107 (*(void (*) (void)) ((void *) imap->l_addr
108 + imap->l_info[DT_FINI]->d_un.d_ptr)) ();
112 /* Notify the debugger we are about to remove some loaded objects. */
113 _r_debug.r_state = RT_DELETE;
114 _dl_debug_state ();
116 /* The search list contains a counted reference to each object it
117 points to, the 0th elt being MAP itself. Decrement the reference
118 counts on all the objects MAP depends on. */
119 for (i = 0; i < nsearchlist; ++i)
120 if (! (list[i]->l_flags_1 & DF_1_NODELETE))
121 --list[i]->l_opencount;
123 /* Check each element of the search list to see if all references to
124 it are gone. */
125 for (i = 0; i < nsearchlist; ++i)
127 struct link_map *imap = list[i];
128 if (imap->l_opencount == 0 && imap->l_type == lt_loaded)
130 struct libname_list *lnp;
132 /* That was the last reference, and this was a dlopen-loaded
133 object. We can unmap it. */
134 if (imap->l_global)
136 /* This object is in the global scope list. Remove it. */
137 int cnt = _dl_main_searchlist->r_nlist;
140 --cnt;
141 while (_dl_main_searchlist->r_list[cnt] != imap);
143 /* The object was already correctly registered. */
144 while (++cnt < _dl_main_searchlist->r_nlist)
145 _dl_main_searchlist->r_list[cnt - 1]
146 = _dl_main_searchlist->r_list[cnt];
148 --_dl_main_searchlist->r_nlist;
151 /* We can unmap all the maps at once. We determined the
152 start address and length when we loaded the object and
153 the `munmap' call does the rest. */
154 __munmap ((void *) imap->l_map_start,
155 imap->l_map_end - imap->l_map_start);
157 /* Finally, unlink the data structure and free it. */
158 #ifdef SHARED
159 /* We will unlink the first object only if this is a statically
160 linked program. */
161 assert (imap->l_prev != NULL);
162 imap->l_prev->l_next = imap->l_next;
163 #else
164 if (imap->l_prev != NULL)
165 imap->l_prev->l_next = imap->l_next;
166 else
167 _dl_loaded = imap->l_next;
168 #endif
169 if (imap->l_next)
170 imap->l_next->l_prev = imap->l_prev;
172 if (imap->l_versions != NULL)
173 free (imap->l_versions);
174 if (imap->l_origin != NULL && imap->l_origin != (char *) -1)
175 free ((char *) imap->l_origin);
177 /* This name always is allocated. */
178 free (imap->l_name);
179 /* Remove the list with all the names of the shared object. */
180 lnp = imap->l_libname;
183 struct libname_list *this = lnp;
184 lnp = lnp->next;
185 free (this);
187 while (lnp != NULL);
189 /* Remove the searchlists. */
190 if (imap != map)
192 if (imap->l_searchlist.r_list != NULL)
193 free (imap->l_searchlist.r_list);
194 else if (imap->l_initfini != NULL)
195 free (imap->l_initfini);
198 if (imap->l_phdr_allocated)
199 free ((void *) imap->l_phdr);
201 free (imap);
205 /* Now we can perhaps also remove the modules for which we had
206 dependencies because of symbol lookup. */
207 if (rellist != NULL)
209 while (nrellist-- > 0)
210 _dl_close (rellist[nrellist]);
212 free (rellist);
215 free (list);
217 if (_dl_global_scope_alloc != 0
218 && _dl_main_searchlist->r_nlist == _dl_initial_searchlist.r_nlist)
220 /* All object dynamically loaded by the program are unloaded. Free
221 the memory allocated for the global scope variable. */
222 struct link_map **old = _dl_main_searchlist->r_list;
224 /* Put the old map in. */
225 _dl_main_searchlist->r_list = _dl_initial_searchlist.r_list;
226 /* Signal that the original map is used. */
227 _dl_global_scope_alloc = 0;
229 /* Now free the old map. */
230 free (old);
233 /* Notify the debugger those objects are finalized and gone. */
234 _r_debug.r_state = RT_CONSISTENT;
235 _dl_debug_state ();
237 /* Release the lock. */
238 __libc_lock_unlock (_dl_load_lock);