1 /* dlclose -- Close a handle opened by `dlopen'.
2 Copyright (C) 1995 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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
23 #include <sys/types.h>
27 #define LOSE(s) _dl_signal_error (0, map->l_name, s)
30 dlclose (void *handle
)
34 struct link_map
*map
= handle
;
36 if (map
->l_opencount
== 0)
37 LOSE ("shared object not open");
39 /* Decrement the reference count. */
42 if (map
->l_opencount
== 0 && map
->l_type
== lt_loaded
)
44 /* That was the last reference, and this was a dlopen-loaded
45 object. We can unmap it. */
48 if (map
->l_info
[DT_FINI
])
49 /* Call its termination function. */
50 (*(void (*) (void)) ((void *) map
->l_addr
+
51 map
->l_info
[DT_FINI
]->d_un
.d_ptr
)) ();
53 if (map
->l_info
[DT_NEEDED
])
55 /* Also close all the dependencies. */
57 = (void *) map
->l_addr
+ map
->l_info
[DT_STRTAB
]->d_un
.d_ptr
;
59 for (d
= map
->l_ld
; d
->d_tag
!= DT_NULL
; ++d
)
60 if (d
->d_tag
== DT_NEEDED
)
62 /* It must already be open, since this one needed it;
63 so dlopen will just find us its `struct link_map'
64 and bump its reference count. */
65 struct link_map
*o
, *dep
66 = dlopen (strtab
+ d
->d_un
.d_val
, RTLD_LAZY
);
67 --dep
->l_opencount
; /* Lose the ref from that dlopen. */
68 /* Now we have the handle; we can close it for real. */
76 /* Unmap the segments. */
77 for (ph
= map
->l_phdr
; ph
< &map
->l_phdr
[map
->l_phnum
]; ++ph
)
78 if (ph
->p_type
== PT_LOAD
)
80 Elf32_Addr mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
81 Elf32_Addr mapend
= ((ph
->p_vaddr
+ ph
->p_memsz
83 & ~(ph
->p_align
- 1));
84 munmap ((caddr_t
) mapstart
, mapend
- mapstart
);
87 /* Finally, unlink the data structure and free it. */
88 map
->l_prev
->l_next
= map
->l_next
;
90 map
->l_next
->l_prev
= map
->l_prev
;
95 return _dlerror_run (doit
) ? -1 : 0;