]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-close.c
Update.
[thirdparty/glibc.git] / elf / dl-close.c
CommitLineData
26b4d766 1/* Close a shared object opened by `_dl_open'.
af69217f 2 Copyright (C) 1996, 1997, 1998 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
37void
d0fc4041 38internal_function
ba79d61b
RM
39_dl_close (struct link_map *map)
40{
41 struct link_map **list;
6075607b 42 unsigned nsearchlist;
ba79d61b
RM
43 unsigned int i;
44
45 if (map->l_opencount == 0)
46 LOSE ("shared object not open");
47
26b4d766
UD
48 /* Acquire the lock. */
49 __libc_lock_lock (_dl_load_lock);
50
ba79d61b 51 /* Decrement the reference count. */
a709dd43 52 if (map->l_opencount > 1 || map->l_type != lt_loaded)
26b4d766
UD
53 {
54 /* There are still references to this object. Do nothing more. */
a709dd43 55 --map->l_opencount;
26b4d766
UD
56 __libc_lock_unlock (_dl_load_lock);
57 return;
58 }
ba79d61b 59
a709dd43 60 list = map->l_searchlist;
6075607b 61 nsearchlist = map->l_nsearchlist;
a709dd43
UD
62
63 /* Call all termination functions at once. */
6075607b 64 for (i = 0; i < nsearchlist; ++i)
a709dd43
UD
65 {
66 struct link_map *imap = list[i];
b48abe3c 67 if (imap->l_opencount == 1 && imap->l_type == lt_loaded
7a68c94a
UD
68 && imap->l_info[DT_FINI]
69 /* Skip any half-cooked objects that were never initialized. */
70 && imap->l_init_called)
a709dd43 71 {
b48abe3c
UD
72 /* When debugging print a message first. */
73 if (_dl_debug_impcalls)
74 _dl_debug_message (1, "\ncalling fini: ", imap->l_name,
75 "\n\n", NULL);
76 /* Call its termination function. */
77 (*(void (*) (void)) ((void *) imap->l_addr
78 + imap->l_info[DT_FINI]->d_un.d_ptr)) ();
a709dd43
UD
79 }
80 }
81
4d6acc61
RM
82 /* Notify the debugger we are about to remove some loaded objects. */
83 _r_debug.r_state = RT_DELETE;
84 _dl_debug_state ();
85
ba79d61b
RM
86 /* The search list contains a counted reference to each object it
87 points to, the 0th elt being MAP itself. Decrement the reference
88 counts on all the objects MAP depends on. */
6075607b 89 for (i = 0; i < nsearchlist; ++i)
ba79d61b
RM
90 --list[i]->l_opencount;
91
ba79d61b
RM
92 /* Check each element of the search list to see if all references to
93 it are gone. */
6075607b 94 for (i = 0; i < nsearchlist; ++i)
ba79d61b 95 {
af69217f
UD
96 struct link_map *imap = list[i];
97 if (imap->l_opencount == 0 && imap->l_type == lt_loaded)
ba79d61b 98 {
a8a1269d
UD
99 struct libname_list *lnp;
100
ba79d61b
RM
101 /* That was the last reference, and this was a dlopen-loaded
102 object. We can unmap it. */
af69217f 103 if (imap->l_global)
ba79d61b
RM
104 {
105 /* This object is in the global scope list. Remove it. */
106 struct link_map **tail = _dl_global_scope_end;
107 do
108 --tail;
af69217f 109 while (*tail != imap);
e9fc7bbb 110 while (tail < _dl_global_scope_end)
3dd90163
UD
111 {
112 tail[0] = tail[1];
113 ++tail;
114 }
ba79d61b 115 --_dl_global_scope_end;
ba79d61b
RM
116 }
117
a8a1269d
UD
118 /* We can unmap all the maps at once. We determined the
119 length when we loaded the object and `munmap' call does
120 the rest. */
121 __munmap ((void *) imap->l_map_start,
122 imap->l_map_end - imap->l_map_start);
22bc7978 123
ba79d61b 124 /* Finally, unlink the data structure and free it. */
7afab53d
UD
125#ifdef PIC
126 /* We will unlink the first object only if this is a statically
127 linked program. */
128 assert (imap->l_prev != NULL);
6a805a0b 129 imap->l_prev->l_next = imap->l_next;
7afab53d
UD
130#else
131 if (imap->l_prev != NULL)
af69217f 132 imap->l_prev->l_next = imap->l_next;
7afab53d
UD
133 else
134 _dl_loaded = imap->l_next;
135#endif
af69217f
UD
136 if (imap->l_next)
137 imap->l_next->l_prev = imap->l_prev;
a709dd43 138 if (imap->l_searchlist && imap->l_searchlist != list)
af69217f 139 free (imap->l_searchlist);
a8a1269d
UD
140
141 if (imap->l_versions != NULL)
142 free (imap->l_versions);
143 if (imap->l_origin != NULL)
144 free ((char *) imap->l_origin);
145
146 /* These names always is allocated. */
147 free (imap->l_name);
148 lnp = imap->l_libname;
149 do
150 {
76156ea1 151 struct libname_list *this = lnp;
a8a1269d 152 lnp = lnp->next;
76156ea1 153 free (this);
a8a1269d
UD
154 }
155 while (lnp != NULL);
a8a1269d 156
af69217f 157 free (imap);
ba79d61b
RM
158 }
159 }
160
161 free (list);
4d6acc61
RM
162
163 /* Notify the debugger those objects are finalized and gone. */
164 _r_debug.r_state = RT_CONSISTENT;
165 _dl_debug_state ();
26b4d766
UD
166
167 /* Release the lock. */
168 __libc_lock_unlock (_dl_load_lock);
ba79d61b 169}