]>
Commit | Line | Data |
---|---|---|
26b4d766 | 1 | /* Close a shared object opened by `_dl_open'. |
482eec0d | 2 | Copyright (C) 1996, 1997, 1998, 1999 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 RM |
21 | #include <dlfcn.h> |
22 | #include <stdlib.h> | |
8d6468d0 | 23 | #include <string.h> |
a853022c UD |
24 | #include <bits/libc-lock.h> |
25 | #include <elf/ldsodefs.h> | |
ba79d61b RM |
26 | #include <sys/types.h> |
27 | #include <sys/mman.h> | |
28 | ||
29 | ||
26b4d766 UD |
30 | /* During the program run we must not modify the global data of |
31 | loaded shared object simultanously in two threads. Therefore we | |
32 | protect `dlopen' and `dlclose' in dlclose.c. */ | |
33 | __libc_lock_define (extern, _dl_load_lock) | |
34 | ||
ba79d61b RM |
35 | #define LOSE(s) _dl_signal_error (0, map->l_name, s) |
36 | ||
37 | void | |
d0fc4041 | 38 | internal_function |
94e365c6 | 39 | _dl_close (void *_map) |
ba79d61b RM |
40 | { |
41 | struct link_map **list; | |
94e365c6 | 42 | struct link_map *map = _map; |
6075607b | 43 | unsigned nsearchlist; |
ba79d61b RM |
44 | unsigned int i; |
45 | ||
46 | if (map->l_opencount == 0) | |
47 | LOSE ("shared object not open"); | |
48 | ||
26b4d766 UD |
49 | /* Acquire the lock. */ |
50 | __libc_lock_lock (_dl_load_lock); | |
51 | ||
ba79d61b | 52 | /* Decrement the reference count. */ |
a709dd43 | 53 | if (map->l_opencount > 1 || map->l_type != lt_loaded) |
26b4d766 UD |
54 | { |
55 | /* There are still references to this object. Do nothing more. */ | |
a709dd43 | 56 | --map->l_opencount; |
26b4d766 UD |
57 | __libc_lock_unlock (_dl_load_lock); |
58 | return; | |
59 | } | |
ba79d61b | 60 | |
be935610 UD |
61 | list = map->l_searchlist.r_list; |
62 | nsearchlist = map->l_searchlist.r_nlist; | |
a709dd43 UD |
63 | |
64 | /* Call all termination functions at once. */ | |
6075607b | 65 | for (i = 0; i < nsearchlist; ++i) |
a709dd43 UD |
66 | { |
67 | struct link_map *imap = list[i]; | |
b48abe3c | 68 | if (imap->l_opencount == 1 && imap->l_type == lt_loaded |
7a68c94a UD |
69 | && imap->l_info[DT_FINI] |
70 | /* Skip any half-cooked objects that were never initialized. */ | |
71 | && imap->l_init_called) | |
a709dd43 | 72 | { |
b48abe3c UD |
73 | /* When debugging print a message first. */ |
74 | if (_dl_debug_impcalls) | |
75 | _dl_debug_message (1, "\ncalling fini: ", imap->l_name, | |
76 | "\n\n", NULL); | |
77 | /* Call its termination function. */ | |
78 | (*(void (*) (void)) ((void *) imap->l_addr | |
79 | + imap->l_info[DT_FINI]->d_un.d_ptr)) (); | |
a709dd43 UD |
80 | } |
81 | } | |
82 | ||
4d6acc61 RM |
83 | /* Notify the debugger we are about to remove some loaded objects. */ |
84 | _r_debug.r_state = RT_DELETE; | |
85 | _dl_debug_state (); | |
86 | ||
ba79d61b RM |
87 | /* The search list contains a counted reference to each object it |
88 | points to, the 0th elt being MAP itself. Decrement the reference | |
89 | counts on all the objects MAP depends on. */ | |
6075607b | 90 | for (i = 0; i < nsearchlist; ++i) |
ba79d61b RM |
91 | --list[i]->l_opencount; |
92 | ||
ba79d61b RM |
93 | /* Check each element of the search list to see if all references to |
94 | it are gone. */ | |
6075607b | 95 | for (i = 0; i < nsearchlist; ++i) |
ba79d61b | 96 | { |
af69217f UD |
97 | struct link_map *imap = list[i]; |
98 | if (imap->l_opencount == 0 && imap->l_type == lt_loaded) | |
ba79d61b | 99 | { |
a8a1269d UD |
100 | struct libname_list *lnp; |
101 | ||
ba79d61b RM |
102 | /* That was the last reference, and this was a dlopen-loaded |
103 | object. We can unmap it. */ | |
af69217f | 104 | if (imap->l_global) |
ba79d61b RM |
105 | { |
106 | /* This object is in the global scope list. Remove it. */ | |
82df2969 | 107 | int cnt = _dl_main_searchlist->r_nlist; |
be935610 | 108 | |
ba79d61b | 109 | do |
50b65db1 | 110 | --cnt; |
be935610 | 111 | while (_dl_main_searchlist->r_list[cnt] != imap); |
482eec0d | 112 | |
50b65db1 UD |
113 | /* The object was already correctly registered. */ |
114 | while (++cnt < _dl_main_searchlist->r_nlist) | |
115 | _dl_main_searchlist->r_list[cnt - 1] | |
116 | = _dl_main_searchlist->r_list[cnt]; | |
117 | ||
118 | --_dl_main_searchlist->r_nlist; | |
ba79d61b RM |
119 | } |
120 | ||
a8a1269d | 121 | /* We can unmap all the maps at once. We determined the |
4ce636da UD |
122 | start address and length when we loaded the object and |
123 | the `munmap' call does the rest. */ | |
a8a1269d UD |
124 | __munmap ((void *) imap->l_map_start, |
125 | imap->l_map_end - imap->l_map_start); | |
22bc7978 | 126 | |
ba79d61b | 127 | /* Finally, unlink the data structure and free it. */ |
7afab53d UD |
128 | #ifdef PIC |
129 | /* We will unlink the first object only if this is a statically | |
130 | linked program. */ | |
131 | assert (imap->l_prev != NULL); | |
6a805a0b | 132 | imap->l_prev->l_next = imap->l_next; |
7afab53d UD |
133 | #else |
134 | if (imap->l_prev != NULL) | |
af69217f | 135 | imap->l_prev->l_next = imap->l_next; |
7afab53d UD |
136 | else |
137 | _dl_loaded = imap->l_next; | |
138 | #endif | |
af69217f UD |
139 | if (imap->l_next) |
140 | imap->l_next->l_prev = imap->l_prev; | |
a8a1269d UD |
141 | |
142 | if (imap->l_versions != NULL) | |
143 | free (imap->l_versions); | |
1c3a6f19 | 144 | if (imap->l_origin != NULL && imap->l_origin != (char *) -1) |
a8a1269d UD |
145 | free ((char *) imap->l_origin); |
146 | ||
4ce636da | 147 | /* This name always is allocated. */ |
a8a1269d | 148 | free (imap->l_name); |
4ce636da | 149 | /* Remove the list with all the names of the shared object. */ |
a8a1269d UD |
150 | lnp = imap->l_libname; |
151 | do | |
152 | { | |
76156ea1 | 153 | struct libname_list *this = lnp; |
a8a1269d | 154 | lnp = lnp->next; |
76156ea1 | 155 | free (this); |
a8a1269d UD |
156 | } |
157 | while (lnp != NULL); | |
a8a1269d | 158 | |
4ce636da | 159 | /* Remove the searchlists. */ |
be935610 | 160 | if (imap->l_searchlist.r_duplist != imap->l_searchlist.r_list) |
4ce636da | 161 | { |
be935610 UD |
162 | /* If a r_list exists there always also is a r_duplist. */ |
163 | assert (imap->l_searchlist.r_list != NULL); | |
164 | free (imap->l_searchlist.r_duplist); | |
4ce636da | 165 | } |
be935610 UD |
166 | if (imap != map && imap->l_searchlist.r_list != NULL) |
167 | free (imap->l_searchlist.r_list); | |
4ce636da | 168 | |
7bcaca43 | 169 | if (imap->l_phdr_allocated) |
15925412 | 170 | free ((void *) imap->l_phdr); |
7bcaca43 | 171 | |
af69217f | 172 | free (imap); |
ba79d61b RM |
173 | } |
174 | } | |
175 | ||
176 | free (list); | |
4d6acc61 | 177 | |
d3556ac9 UD |
178 | if (_dl_global_scope_alloc != 0 |
179 | && _dl_main_searchlist->r_nlist == _dl_initial_searchlist.r_nlist) | |
180 | { | |
181 | /* All object dynamically loaded by the program are unloaded. Free | |
182 | the memory allocated for the global scope variable. */ | |
183 | struct link_map **old = _dl_main_searchlist->r_list; | |
184 | ||
185 | /* Put the old map in. */ | |
186 | _dl_main_searchlist->r_list = _dl_initial_searchlist.r_list; | |
187 | /* Signal that the original map is used. */ | |
188 | _dl_global_scope_alloc = 0; | |
189 | ||
190 | /* Now free the old map. */ | |
191 | free (old); | |
192 | } | |
193 | ||
4d6acc61 RM |
194 | /* Notify the debugger those objects are finalized and gone. */ |
195 | _r_debug.r_state = RT_CONSISTENT; | |
196 | _dl_debug_state (); | |
26b4d766 UD |
197 | |
198 | /* Release the lock. */ | |
199 | __libc_lock_unlock (_dl_load_lock); | |
ba79d61b | 200 | } |