Fix race condition.
[glibc.git] / elf / dl-close.c
blobb1bc9ecf0f0ddb1d3b9c4c550d1ee0efe25289ed
1 /* _dl_close -- Close a shared object opened by `_dl_open'.
2 Copyright (C) 1996, 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. */
20 #include <link.h>
21 #include <dlfcn.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/mman.h>
28 #define LOSE(s) _dl_signal_error (0, map->l_name, s)
30 void
31 _dl_close (struct link_map *map)
33 struct link_map **list;
34 unsigned int i;
36 if (map->l_opencount == 0)
37 LOSE ("shared object not open");
39 /* Decrement the reference count. */
40 if (--map->l_opencount > 0 || map->l_type != lt_loaded)
41 /* There are still references to this object. Do nothing more. */
42 return;
44 /* Notify the debugger we are about to remove some loaded objects. */
45 _r_debug.r_state = RT_DELETE;
46 _dl_debug_state ();
48 list = map->l_searchlist;
50 /* The search list contains a counted reference to each object it
51 points to, the 0th elt being MAP itself. Decrement the reference
52 counts on all the objects MAP depends on. */
53 for (i = 1; i < map->l_nsearchlist; ++i)
54 --list[i]->l_opencount;
56 /* Clear the search list so it doesn't get freed while we are still
57 using it. We have cached it in LIST and will free it when
58 finished. */
59 map->l_searchlist = NULL;
61 /* Check each element of the search list to see if all references to
62 it are gone. */
63 for (i = 0; i < map->l_nsearchlist; ++i)
65 struct link_map *imap = list[i];
66 if (imap->l_opencount == 0 && imap->l_type == lt_loaded)
68 /* That was the last reference, and this was a dlopen-loaded
69 object. We can unmap it. */
70 const ElfW(Phdr) *ph;
72 if (imap->l_info[DT_FINI])
73 /* Call its termination function. */
74 (*(void (*) (void)) ((void *) imap->l_addr
75 + imap->l_info[DT_FINI]->d_un.d_ptr)) ();
77 if (imap->l_global)
79 /* This object is in the global scope list. Remove it. */
80 struct link_map **tail = _dl_global_scope_end;
82 --tail;
83 while (*tail != imap);
84 --_dl_global_scope_end;
85 memcpy (tail, tail + 1, _dl_global_scope_end - tail);
86 _dl_global_scope_end[0] = NULL;
87 _dl_global_scope_end[1] = NULL;
90 /* Unmap the segments. */
91 for (ph = imap->l_phdr + (imap->l_phnum - 1);
92 ph >= imap->l_phdr; --ph)
93 if (ph->p_type == PT_LOAD)
95 ElfW(Addr) mapstart = ph->p_vaddr & ~(ph->p_align - 1);
96 ElfW(Addr) mapend = ((ph->p_vaddr + ph->p_memsz
97 + ph->p_align - 1)
98 & ~(ph->p_align - 1));
99 __munmap ((caddr_t) (imap->l_addr + mapstart),
100 mapend - mapstart);
103 /* Finally, unlink the data structure and free it. */
104 if (imap->l_prev)
105 imap->l_prev->l_next = imap->l_next;
106 if (imap->l_next)
107 imap->l_next->l_prev = imap->l_prev;
108 if (imap->l_searchlist)
109 free (imap->l_searchlist);
110 free (imap);
114 free (list);
116 /* Notify the debugger those objects are finalized and gone. */
117 _r_debug.r_state = RT_CONSISTENT;
118 _dl_debug_state ();