1 /* Close a shared object opened by `_dl_open'.
2 Copyright (C) 1996, 1997, 1998 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. */
23 #include <bits/libc-lock.h>
24 #include <elf/ldsodefs.h>
25 #include <sys/types.h>
29 /* During the program run we must not modify the global data of
30 loaded shared object simultanously in two threads. Therefore we
31 protect `dlopen' and `dlclose' in dlclose.c. */
32 __libc_lock_define (extern, _dl_load_lock
)
34 #define LOSE(s) _dl_signal_error (0, map->l_name, s)
38 _dl_close (struct link_map
*map
)
40 struct link_map
**list
;
43 if (map
->l_opencount
== 0)
44 LOSE ("shared object not open");
46 /* Acquire the lock. */
47 __libc_lock_lock (_dl_load_lock
);
49 /* Decrement the reference count. */
50 if (map
->l_opencount
> 1 || map
->l_type
!= lt_loaded
)
52 /* There are still references to this object. Do nothing more. */
54 __libc_lock_unlock (_dl_load_lock
);
58 list
= map
->l_searchlist
;
60 /* Call all termination functions at once. */
61 for (i
= 0; i
< map
->l_nsearchlist
; ++i
)
63 struct link_map
*imap
= list
[i
];
64 if (imap
->l_opencount
== 1 && imap
->l_type
== lt_loaded
65 && imap
->l_info
[DT_FINI
]
66 /* Skip any half-cooked objects that were never initialized. */
67 && imap
->l_init_called
)
69 /* When debugging print a message first. */
70 if (_dl_debug_impcalls
)
71 _dl_debug_message (1, "\ncalling fini: ", imap
->l_name
,
73 /* Call its termination function. */
74 (*(void (*) (void)) ((void *) imap
->l_addr
75 + imap
->l_info
[DT_FINI
]->d_un
.d_ptr
)) ();
79 /* Notify the debugger we are about to remove some loaded objects. */
80 _r_debug
.r_state
= RT_DELETE
;
83 /* The search list contains a counted reference to each object it
84 points to, the 0th elt being MAP itself. Decrement the reference
85 counts on all the objects MAP depends on. */
86 for (i
= 0; i
< map
->l_nsearchlist
; ++i
)
87 --list
[i
]->l_opencount
;
89 /* Check each element of the search list to see if all references to
91 for (i
= 0; i
< map
->l_nsearchlist
; ++i
)
93 struct link_map
*imap
= list
[i
];
94 if (imap
->l_opencount
== 0 && imap
->l_type
== lt_loaded
)
96 /* That was the last reference, and this was a dlopen-loaded
97 object. We can unmap it. */
99 const ElfW(Phdr
) *first
, *last
;
100 ElfW(Addr
) mapstart
, mapend
;
104 /* This object is in the global scope list. Remove it. */
105 struct link_map
**tail
= _dl_global_scope_end
;
108 while (*tail
!= imap
);
109 while (tail
< _dl_global_scope_end
)
114 --_dl_global_scope_end
;
117 /* We can unmap all the maps at once. We just have to determine
118 the length and the `munmap' call does the rest. */
120 for (ph
= imap
->l_phdr
; ph
< imap
->l_phdr
+ imap
->l_phnum
; ++ph
)
121 if (ph
->p_type
== PT_LOAD
)
128 /* Now we have all the information we need for the unmapping.
129 See the method used in `_dl_map_object_from_fd'. */
130 mapstart
= first
->p_vaddr
& ~(first
->p_align
- 1);
131 mapend
= last
->p_vaddr
+ last
->p_memsz
;
132 __munmap ((caddr_t
) (imap
->l_addr
+ mapstart
), mapend
- mapstart
);
134 /* Finally, unlink the data structure and free it. */
136 imap
->l_prev
->l_next
= imap
->l_next
;
138 imap
->l_next
->l_prev
= imap
->l_prev
;
139 if (imap
->l_searchlist
&& imap
->l_searchlist
!= list
)
140 free (imap
->l_searchlist
);
147 /* Notify the debugger those objects are finalized and gone. */
148 _r_debug
.r_state
= RT_CONSISTENT
;
151 /* Release the lock. */
152 __libc_lock_unlock (_dl_load_lock
);