1 /* Call the termination functions of loaded shared objects.
2    Copyright (C) 1995-2021 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 Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the 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    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <assert.h>
20 #include <string.h>
21 #include <ldsodefs.h>
22 #include <elf-initfini.h>
23 
24 
25 /* Type of the constructor functions.  */
26 typedef void (*fini_t) (void);
27 
28 
29 void
_dl_fini(void)30 _dl_fini (void)
31 {
32   /* Lots of fun ahead.  We have to call the destructors for all still
33      loaded objects, in all namespaces.  The problem is that the ELF
34      specification now demands that dependencies between the modules
35      are taken into account.  I.e., the destructor for a module is
36      called before the ones for any of its dependencies.
37 
38      To make things more complicated, we cannot simply use the reverse
39      order of the constructors.  Since the user might have loaded objects
40      using `dlopen' there are possibly several other modules with its
41      dependencies to be taken into account.  Therefore we have to start
42      determining the order of the modules once again from the beginning.  */
43 
44   /* We run the destructors of the main namespaces last.  As for the
45      other namespaces, we pick run the destructors in them in reverse
46      order of the namespace ID.  */
47 #ifdef SHARED
48   int do_audit = 0;
49  again:
50 #endif
51   for (Lmid_t ns = GL(dl_nns) - 1; ns >= 0; --ns)
52     {
53       /* Protect against concurrent loads and unloads.  */
54       __rtld_lock_lock_recursive (GL(dl_load_lock));
55 
56       unsigned int nloaded = GL(dl_ns)[ns]._ns_nloaded;
57       /* No need to do anything for empty namespaces or those used for
58 	 auditing DSOs.  */
59       if (nloaded == 0
60 #ifdef SHARED
61 	  || GL(dl_ns)[ns]._ns_loaded->l_auditing != do_audit
62 #endif
63 	  )
64 	__rtld_lock_unlock_recursive (GL(dl_load_lock));
65       else
66 	{
67 	  /* Now we can allocate an array to hold all the pointers and
68 	     copy the pointers in.  */
69 	  struct link_map *maps[nloaded];
70 
71 	  unsigned int i;
72 	  struct link_map *l;
73 	  assert (nloaded != 0 || GL(dl_ns)[ns]._ns_loaded == NULL);
74 	  for (l = GL(dl_ns)[ns]._ns_loaded, i = 0; l != NULL; l = l->l_next)
75 	    /* Do not handle ld.so in secondary namespaces.  */
76 	    if (l == l->l_real)
77 	      {
78 		assert (i < nloaded);
79 
80 		maps[i] = l;
81 		l->l_idx = i;
82 		++i;
83 
84 		/* Bump l_direct_opencount of all objects so that they
85 		   are not dlclose()ed from underneath us.  */
86 		++l->l_direct_opencount;
87 	      }
88 	  assert (ns != LM_ID_BASE || i == nloaded);
89 	  assert (ns == LM_ID_BASE || i == nloaded || i == nloaded - 1);
90 	  unsigned int nmaps = i;
91 
92 	  /* Now we have to do the sorting.  We can skip looking for the
93 	     binary itself which is at the front of the search list for
94 	     the main namespace.  */
95 	  _dl_sort_maps (maps, nmaps, (ns == LM_ID_BASE), true);
96 
97 	  /* We do not rely on the linked list of loaded object anymore
98 	     from this point on.  We have our own list here (maps).  The
99 	     various members of this list cannot vanish since the open
100 	     count is too high and will be decremented in this loop.  So
101 	     we release the lock so that some code which might be called
102 	     from a destructor can directly or indirectly access the
103 	     lock.  */
104 	  __rtld_lock_unlock_recursive (GL(dl_load_lock));
105 
106 	  /* 'maps' now contains the objects in the right order.  Now
107 	     call the destructors.  We have to process this array from
108 	     the front.  */
109 	  for (i = 0; i < nmaps; ++i)
110 	    {
111 	      struct link_map *l = maps[i];
112 
113 	      if (l->l_init_called)
114 		{
115 		  /* Make sure nothing happens if we are called twice.  */
116 		  l->l_init_called = 0;
117 
118 		  /* Is there a destructor function?  */
119 		  if (l->l_info[DT_FINI_ARRAY] != NULL
120 		      || (ELF_INITFINI && l->l_info[DT_FINI] != NULL))
121 		    {
122 		      /* When debugging print a message first.  */
123 		      if (__builtin_expect (GLRO(dl_debug_mask)
124 					    & DL_DEBUG_IMPCALLS, 0))
125 			_dl_debug_printf ("\ncalling fini: %s [%lu]\n\n",
126 					  DSO_FILENAME (l->l_name),
127 					  ns);
128 
129 		      /* First see whether an array is given.  */
130 		      if (l->l_info[DT_FINI_ARRAY] != NULL)
131 			{
132 			  ElfW(Addr) *array =
133 			    (ElfW(Addr) *) (l->l_addr
134 					    + l->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
135 			  unsigned int i = (l->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
136 					    / sizeof (ElfW(Addr)));
137 			  while (i-- > 0)
138 			    ((fini_t) array[i]) ();
139 			}
140 
141 		      /* Next try the old-style destructor.  */
142 		      if (ELF_INITFINI && l->l_info[DT_FINI] != NULL)
143 			DL_CALL_DT_FINI
144 			  (l, l->l_addr + l->l_info[DT_FINI]->d_un.d_ptr);
145 		    }
146 
147 #ifdef SHARED
148 		  /* Auditing checkpoint: another object closed.  */
149 		  _dl_audit_objclose (l);
150 #endif
151 		}
152 
153 	      /* Correct the previous increment.  */
154 	      --l->l_direct_opencount;
155 	    }
156 	}
157     }
158 
159 #ifdef SHARED
160   if (! do_audit && GLRO(dl_naudit) > 0)
161     {
162       do_audit = 1;
163       goto again;
164     }
165 
166   if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS))
167     _dl_debug_printf ("\nruntime linker statistics:\n"
168 		      "           final number of relocations: %lu\n"
169 		      "final number of relocations from cache: %lu\n",
170 		      GL(dl_num_relocations),
171 		      GL(dl_num_cache_relocations));
172 #endif
173 }
174