49deb961b35ed4541acbb352afab13557dbdb518
[jlayton/glibc.git] / elf / dl-close.c
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.
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.  */
19
20 #include <dlfcn.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <bits/libc-lock.h>
24 #include <elf/ldsodefs.h>
25 #include <sys/types.h>
26 #include <sys/mman.h>
27
28
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)
33
34 #define LOSE(s) _dl_signal_error (0, map->l_name, s)
35
36 void
37 internal_function
38 _dl_close (struct link_map *map)
39 {
40   struct link_map **list;
41   unsigned int i;
42
43   if (map->l_opencount == 0)
44     LOSE ("shared object not open");
45
46   /* Acquire the lock.  */
47   __libc_lock_lock (_dl_load_lock);
48
49   /* Decrement the reference count.  */
50   if (map->l_opencount > 1 || map->l_type != lt_loaded)
51     {
52       /* There are still references to this object.  Do nothing more.  */
53       --map->l_opencount;
54       __libc_lock_unlock (_dl_load_lock);
55       return;
56     }
57
58   list = map->l_searchlist;
59
60   /* Call all termination functions at once.  */
61   for (i = 0; i < map->l_nsearchlist; ++i)
62     {
63       struct link_map *imap = list[i];
64       if (imap->l_opencount == 1 && imap->l_type == lt_loaded)
65         {
66           if (imap->l_info[DT_FINI])
67             /* Call its termination function.  */
68             (*(void (*) (void)) ((void *) imap->l_addr
69                                  + imap->l_info[DT_FINI]->d_un.d_ptr)) ();
70         }
71     }
72
73   /* Notify the debugger we are about to remove some loaded objects.  */
74   _r_debug.r_state = RT_DELETE;
75   _dl_debug_state ();
76
77   /* The search list contains a counted reference to each object it
78      points to, the 0th elt being MAP itself.  Decrement the reference
79      counts on all the objects MAP depends on.  */
80   for (i = 0; i < map->l_nsearchlist; ++i)
81     --list[i]->l_opencount;
82
83   /* Check each element of the search list to see if all references to
84      it are gone.  */
85   for (i = 0; i < map->l_nsearchlist; ++i)
86     {
87       struct link_map *imap = list[i];
88       if (imap->l_opencount == 0 && imap->l_type == lt_loaded)
89         {
90           /* That was the last reference, and this was a dlopen-loaded
91              object.  We can unmap it.  */
92           const ElfW(Phdr) *ph;
93           const ElfW(Phdr) *first, *last;
94           ElfW(Addr) mapstart, mapend;
95
96           if (imap->l_global)
97             {
98               /* This object is in the global scope list.  Remove it.  */
99               struct link_map **tail = _dl_global_scope_end;
100               do
101                 --tail;
102               while (*tail != imap);
103               while (tail < _dl_global_scope_end)
104                 {
105                   tail[0] = tail[1];
106                   ++tail;
107                 }
108               --_dl_global_scope_end;
109             }
110
111           /* We can unmap all the maps at once.  We just have to determine
112              the length and the `munmap' call does the rest.  */
113           first = last = NULL;
114           for (ph = imap->l_phdr; ph < imap->l_phdr + imap->l_phnum; ++ph)
115             if (ph->p_type == PT_LOAD)
116               {
117                 if (first == NULL)
118                   first = ph;
119                 last = ph;
120               }
121
122           /* Now we have all the information we need for the unmapping.
123              See the method used in `_dl_map_object_from_fd'.  */
124           mapstart = first->p_vaddr & ~(first->p_align - 1);
125           mapend = last->p_vaddr + last->p_memsz;
126           __munmap ((caddr_t) (imap->l_addr + mapstart), mapend - mapstart);
127
128           /* Finally, unlink the data structure and free it.  */
129           if (imap->l_prev)
130             imap->l_prev->l_next = imap->l_next;
131           if (imap->l_next)
132             imap->l_next->l_prev = imap->l_prev;
133           if (imap->l_searchlist && imap->l_searchlist != list)
134             free (imap->l_searchlist);
135           free (imap);
136         }
137     }
138
139   free (list);
140
141   /* Notify the debugger those objects are finalized and gone.  */
142   _r_debug.r_state = RT_CONSISTENT;
143   _dl_debug_state ();
144
145   /* Release the lock.  */
146   __libc_lock_unlock (_dl_load_lock);
147 }