Update.
[glibc.git] / elf / dl-close.c
blob324f49a0ec2e6e7fa7aebd0ac5aa6e00b42f8a22
1 /* Close a shared object opened by `_dl_open'.
2 Copyright (C) 1996, 1997, 1998, 1999 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 <stdlib.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
25 #include <elf/ldsodefs.h>
26 #include <sys/types.h>
27 #include <sys/mman.h>
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)
35 #define LOSE(s) _dl_signal_error (0, map->l_name, s)
37 void
38 internal_function
39 _dl_close (void *_map)
41 struct link_map **list;
42 struct link_map *map = _map;
43 unsigned nsearchlist;
44 unsigned int i;
46 if (map->l_opencount == 0)
47 LOSE ("shared object not open");
49 /* Acquire the lock. */
50 __libc_lock_lock (_dl_load_lock);
52 /* Decrement the reference count. */
53 if (map->l_opencount > 1 || map->l_type != lt_loaded)
55 /* There are still references to this object. Do nothing more. */
56 --map->l_opencount;
57 __libc_lock_unlock (_dl_load_lock);
58 return;
61 list = map->l_searchlist.r_list;
62 nsearchlist = map->l_searchlist.r_nlist;
64 /* Call all termination functions at once. */
65 for (i = 0; i < nsearchlist; ++i)
67 struct link_map *imap = list[i];
68 if (imap->l_opencount == 1 && imap->l_type == lt_loaded
69 && imap->l_info[DT_FINI]
70 /* Skip any half-cooked objects that were never initialized. */
71 && imap->l_init_called)
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)) ();
83 /* Notify the debugger we are about to remove some loaded objects. */
84 _r_debug.r_state = RT_DELETE;
85 _dl_debug_state ();
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. */
90 for (i = 0; i < nsearchlist; ++i)
91 --list[i]->l_opencount;
93 /* Check each element of the search list to see if all references to
94 it are gone. */
95 for (i = 0; i < nsearchlist; ++i)
97 struct link_map *imap = list[i];
98 if (imap->l_opencount == 0 && imap->l_type == lt_loaded)
100 struct libname_list *lnp;
102 /* That was the last reference, and this was a dlopen-loaded
103 object. We can unmap it. */
104 if (imap->l_global)
106 /* This object is in the global scope list. Remove it. */
107 int cnt = _dl_main_searchlist->r_nlist;
110 --cnt;
111 while (_dl_main_searchlist->r_list[cnt] != imap);
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];
118 --_dl_main_searchlist->r_nlist;
121 /* We can unmap all the maps at once. We determined the
122 start address and length when we loaded the object and
123 the `munmap' call does the rest. */
124 __munmap ((void *) imap->l_map_start,
125 imap->l_map_end - imap->l_map_start);
127 /* Finally, unlink the data structure and free it. */
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);
132 imap->l_prev->l_next = imap->l_next;
133 #else
134 if (imap->l_prev != NULL)
135 imap->l_prev->l_next = imap->l_next;
136 else
137 _dl_loaded = imap->l_next;
138 #endif
139 if (imap->l_next)
140 imap->l_next->l_prev = imap->l_prev;
142 if (imap->l_versions != NULL)
143 free (imap->l_versions);
144 if (imap->l_origin != NULL && imap->l_origin != (char *) -1)
145 free ((char *) imap->l_origin);
147 /* This name always is allocated. */
148 free (imap->l_name);
149 /* Remove the list with all the names of the shared object. */
150 lnp = imap->l_libname;
153 struct libname_list *this = lnp;
154 lnp = lnp->next;
155 free (this);
157 while (lnp != NULL);
159 /* Remove the searchlists. */
160 if (imap->l_searchlist.r_duplist != imap->l_searchlist.r_list)
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);
166 if (imap != map && imap->l_searchlist.r_list != NULL)
167 free (imap->l_searchlist.r_list);
169 if (imap->l_phdr_allocated)
170 free ((void *) imap->l_phdr);
172 free (imap);
176 free (list);
178 if (_dl_global_scope_alloc != 0
179 && _dl_main_searchlist->r_nlist == _dl_initial_searchlist.r_nlist)
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;
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;
190 /* Now free the old map. */
191 free (old);
194 /* Notify the debugger those objects are finalized and gone. */
195 _r_debug.r_state = RT_CONSISTENT;
196 _dl_debug_state ();
198 /* Release the lock. */
199 __libc_lock_unlock (_dl_load_lock);