1 /* Call the termination functions of loaded shared objects.
2 Copyright (C) 1995-2017 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 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.
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.
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 <http://www.gnu.org/licenses/>. */
24 /* Type of the constructor functions. */
25 typedef void (*fini_t
) (void);
29 _dl_sort_fini (struct link_map
**maps
, size_t nmaps
, char *used
, Lmid_t ns
)
31 /* A list of one element need not be sorted. */
35 /* We can skip looking for the binary itself which is at the front
36 of the search list for the main namespace. */
37 unsigned int i
= ns
== LM_ID_BASE
;
39 memset (seen
, 0, nmaps
* sizeof (seen
[0]));
42 /* Keep track of which object we looked at this round. */
44 struct link_map
*thisp
= maps
[i
];
46 /* Do not handle ld.so in secondary namespaces and object which
48 if (thisp
!= thisp
->l_real
|| thisp
->l_idx
== -1)
51 /* Find the last object in the list for which the current one is
52 a dependency and move the current object behind the object
53 with the dependency. */
54 unsigned int k
= nmaps
- 1;
57 struct link_map
**runp
= maps
[k
]->l_initfini
;
59 /* Look through the dependencies of the object. */
61 if (__glibc_unlikely (*runp
++ == thisp
))
64 /* Move the current object to the back past the last
65 object with it as the dependency. */
66 memmove (&maps
[i
], &maps
[i
+ 1],
67 (k
- i
) * sizeof (maps
[0]));
72 char here_used
= used
[i
];
73 memmove (&used
[i
], &used
[i
+ 1],
74 (k
- i
) * sizeof (used
[0]));
78 if (seen
[i
+ 1] > nmaps
- i
)
84 uint16_t this_seen
= seen
[i
];
85 memmove (&seen
[i
], &seen
[i
+ 1], (k
- i
) * sizeof (seen
[0]));
91 if (__glibc_unlikely (maps
[k
]->l_reldeps
!= NULL
))
93 unsigned int m
= maps
[k
]->l_reldeps
->act
;
94 struct link_map
**relmaps
= &maps
[k
]->l_reldeps
->list
[0];
96 /* Look through the relocation dependencies of the object. */
98 if (__glibc_unlikely (relmaps
[m
] == thisp
))
100 /* If a cycle exists with a link time dependency,
101 preserve the latter. */
102 struct link_map
**runp
= thisp
->l_initfini
;
104 while (*runp
!= NULL
)
105 if (__glibc_unlikely (*runp
++ == maps
[k
]))
119 memset (&seen
[i
], 0, (nmaps
- i
) * sizeof (seen
[0]));
129 /* Lots of fun ahead. We have to call the destructors for all still
130 loaded objects, in all namespaces. The problem is that the ELF
131 specification now demands that dependencies between the modules
132 are taken into account. I.e., the destructor for a module is
133 called before the ones for any of its dependencies.
135 To make things more complicated, we cannot simply use the reverse
136 order of the constructors. Since the user might have loaded objects
137 using `dlopen' there are possibly several other modules with its
138 dependencies to be taken into account. Therefore we have to start
139 determining the order of the modules once again from the beginning. */
141 /* We run the destructors of the main namespaces last. As for the
142 other namespaces, we pick run the destructors in them in reverse
143 order of the namespace ID. */
148 for (Lmid_t ns
= GL(dl_nns
) - 1; ns
>= 0; --ns
)
150 /* Protect against concurrent loads and unloads. */
151 __rtld_lock_lock_recursive (GL(dl_load_lock
));
153 unsigned int nloaded
= GL(dl_ns
)[ns
]._ns_nloaded
;
154 /* No need to do anything for empty namespaces or those used for
158 || GL(dl_ns
)[ns
]._ns_loaded
->l_auditing
!= do_audit
161 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
164 /* Now we can allocate an array to hold all the pointers and
165 copy the pointers in. */
166 struct link_map
*maps
[nloaded
];
170 assert (nloaded
!= 0 || GL(dl_ns
)[ns
]._ns_loaded
== NULL
);
171 for (l
= GL(dl_ns
)[ns
]._ns_loaded
, i
= 0; l
!= NULL
; l
= l
->l_next
)
172 /* Do not handle ld.so in secondary namespaces. */
175 assert (i
< nloaded
);
181 /* Bump l_direct_opencount of all objects so that they
182 are not dlclose()ed from underneath us. */
183 ++l
->l_direct_opencount
;
185 assert (ns
!= LM_ID_BASE
|| i
== nloaded
);
186 assert (ns
== LM_ID_BASE
|| i
== nloaded
|| i
== nloaded
- 1);
187 unsigned int nmaps
= i
;
189 /* Now we have to do the sorting. */
190 _dl_sort_fini (maps
, nmaps
, NULL
, ns
);
192 /* We do not rely on the linked list of loaded object anymore
193 from this point on. We have our own list here (maps). The
194 various members of this list cannot vanish since the open
195 count is too high and will be decremented in this loop. So
196 we release the lock so that some code which might be called
197 from a destructor can directly or indirectly access the
199 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
201 /* 'maps' now contains the objects in the right order. Now
202 call the destructors. We have to process this array from
204 for (i
= 0; i
< nmaps
; ++i
)
206 struct link_map
*l
= maps
[i
];
208 if (l
->l_init_called
)
210 /* Make sure nothing happens if we are called twice. */
211 l
->l_init_called
= 0;
213 /* Is there a destructor function? */
214 if (l
->l_info
[DT_FINI_ARRAY
] != NULL
215 || l
->l_info
[DT_FINI
] != NULL
)
217 /* When debugging print a message first. */
218 if (__builtin_expect (GLRO(dl_debug_mask
)
219 & DL_DEBUG_IMPCALLS
, 0))
220 _dl_debug_printf ("\ncalling fini: %s [%lu]\n\n",
221 DSO_FILENAME (l
->l_name
),
224 /* First see whether an array is given. */
225 if (l
->l_info
[DT_FINI_ARRAY
] != NULL
)
228 (ElfW(Addr
) *) (l
->l_addr
229 + l
->l_info
[DT_FINI_ARRAY
]->d_un
.d_ptr
);
230 unsigned int i
= (l
->l_info
[DT_FINI_ARRAYSZ
]->d_un
.d_val
231 / sizeof (ElfW(Addr
)));
233 ((fini_t
) array
[i
]) ();
236 /* Next try the old-style destructor. */
237 if (l
->l_info
[DT_FINI
] != NULL
)
239 (l
, l
->l_addr
+ l
->l_info
[DT_FINI
]->d_un
.d_ptr
);
243 /* Auditing checkpoint: another object closed. */
244 if (!do_audit
&& __builtin_expect (GLRO(dl_naudit
) > 0, 0))
246 struct audit_ifaces
*afct
= GLRO(dl_audit
);
247 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
249 if (afct
->objclose
!= NULL
)
250 /* Return value is ignored. */
251 (void) afct
->objclose (&l
->l_audit
[cnt
].cookie
);
259 /* Correct the previous increment. */
260 --l
->l_direct_opencount
;
266 if (! do_audit
&& GLRO(dl_naudit
) > 0)
272 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_STATISTICS
))
273 _dl_debug_printf ("\nruntime linker statistics:\n"
274 " final number of relocations: %lu\n"
275 "final number of relocations from cache: %lu\n",
276 GL(dl_num_relocations
),
277 GL(dl_num_cache_relocations
));