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