Add narrowing square root functions
[glibc.git] / elf / dl-close.c
blobf39001cab981b72309d6ec020935b1dbf367975a
1 /* Close a shared object opened by `_dl_open'.
2 Copyright (C) 1996-2021 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 <https://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <dlfcn.h>
21 #include <errno.h>
22 #include <libintl.h>
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <libc-lock.h>
29 #include <ldsodefs.h>
30 #include <sys/types.h>
31 #include <sys/mman.h>
32 #include <sysdep-cancel.h>
33 #include <tls.h>
34 #include <stap-probe.h>
36 #include <dl-unmap-segments.h>
39 /* Type of the constructor functions. */
40 typedef void (*fini_t) (void);
43 /* Special l_idx value used to indicate which objects remain loaded. */
44 #define IDX_STILL_USED -1
47 /* Returns true we an non-empty was found. */
48 static bool
49 remove_slotinfo (size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
50 bool should_be_there)
52 if (idx - disp >= listp->len)
54 if (listp->next == NULL)
56 /* The index is not actually valid in the slotinfo list,
57 because this object was closed before it was fully set
58 up due to some error. */
59 assert (! should_be_there);
61 else
63 if (remove_slotinfo (idx, listp->next, disp + listp->len,
64 should_be_there))
65 return true;
67 /* No non-empty entry. Search from the end of this element's
68 slotinfo array. */
69 idx = disp + listp->len;
72 else
74 struct link_map *old_map = listp->slotinfo[idx - disp].map;
76 /* The entry might still be in its unused state if we are closing an
77 object that wasn't fully set up. */
78 if (__glibc_likely (old_map != NULL))
80 /* Mark the entry as unused. These can be read concurrently. */
81 atomic_store_relaxed (&listp->slotinfo[idx - disp].gen,
82 GL(dl_tls_generation) + 1);
83 atomic_store_relaxed (&listp->slotinfo[idx - disp].map, NULL);
86 /* If this is not the last currently used entry no need to look
87 further. */
88 if (idx != GL(dl_tls_max_dtv_idx))
90 /* There is an unused dtv entry in the middle. */
91 GL(dl_tls_dtv_gaps) = true;
92 return true;
96 while (idx - disp > (disp == 0 ? 1 + GL(dl_tls_static_nelem) : 0))
98 --idx;
100 if (listp->slotinfo[idx - disp].map != NULL)
102 /* Found a new last used index. This can be read concurrently. */
103 atomic_store_relaxed (&GL(dl_tls_max_dtv_idx), idx);
104 return true;
108 /* No non-entry in this list element. */
109 return false;
112 /* Invoke dstructors for CLOSURE (a struct link_map *). Called with
113 exception handling temporarily disabled, to make errors fatal. */
114 static void
115 call_destructors (void *closure)
117 struct link_map *map = closure;
119 if (map->l_info[DT_FINI_ARRAY] != NULL)
121 ElfW(Addr) *array =
122 (ElfW(Addr) *) (map->l_addr
123 + map->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
124 unsigned int sz = (map->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
125 / sizeof (ElfW(Addr)));
127 while (sz-- > 0)
128 ((fini_t) array[sz]) ();
131 /* Next try the old-style destructor. */
132 if (map->l_info[DT_FINI] != NULL)
133 DL_CALL_DT_FINI (map, ((void *) map->l_addr
134 + map->l_info[DT_FINI]->d_un.d_ptr));
137 void
138 _dl_close_worker (struct link_map *map, bool force)
140 /* One less direct use. */
141 --map->l_direct_opencount;
143 /* If _dl_close is called recursively (some destructor call dlclose),
144 just record that the parent _dl_close will need to do garbage collection
145 again and return. */
146 static enum { not_pending, pending, rerun } dl_close_state;
148 if (map->l_direct_opencount > 0 || map->l_type != lt_loaded
149 || dl_close_state != not_pending)
151 if (map->l_direct_opencount == 0 && map->l_type == lt_loaded)
152 dl_close_state = rerun;
154 /* There are still references to this object. Do nothing more. */
155 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
156 _dl_debug_printf ("\nclosing file=%s; direct_opencount=%u\n",
157 map->l_name, map->l_direct_opencount);
159 return;
162 Lmid_t nsid = map->l_ns;
163 struct link_namespaces *ns = &GL(dl_ns)[nsid];
165 retry:
166 dl_close_state = pending;
168 bool any_tls = false;
169 const unsigned int nloaded = ns->_ns_nloaded;
170 char used[nloaded];
171 char done[nloaded];
172 struct link_map *maps[nloaded];
174 /* Run over the list and assign indexes to the link maps and enter
175 them into the MAPS array. */
176 int idx = 0;
177 for (struct link_map *l = ns->_ns_loaded; l != NULL; l = l->l_next)
179 l->l_idx = idx;
180 maps[idx] = l;
181 ++idx;
184 assert (idx == nloaded);
186 /* Prepare the bitmaps. */
187 memset (used, '\0', sizeof (used));
188 memset (done, '\0', sizeof (done));
190 /* Keep track of the lowest index link map we have covered already. */
191 int done_index = -1;
192 while (++done_index < nloaded)
194 struct link_map *l = maps[done_index];
196 if (done[done_index])
197 /* Already handled. */
198 continue;
200 /* Check whether this object is still used. */
201 if (l->l_type == lt_loaded
202 && l->l_direct_opencount == 0
203 && !l->l_nodelete_active
204 /* See CONCURRENCY NOTES in cxa_thread_atexit_impl.c to know why
205 acquire is sufficient and correct. */
206 && atomic_load_acquire (&l->l_tls_dtor_count) == 0
207 && !used[done_index])
208 continue;
210 /* We need this object and we handle it now. */
211 done[done_index] = 1;
212 used[done_index] = 1;
213 /* Signal the object is still needed. */
214 l->l_idx = IDX_STILL_USED;
216 /* Mark all dependencies as used. */
217 if (l->l_initfini != NULL)
219 /* We are always the zeroth entry, and since we don't include
220 ourselves in the dependency analysis start at 1. */
221 struct link_map **lp = &l->l_initfini[1];
222 while (*lp != NULL)
224 if ((*lp)->l_idx != IDX_STILL_USED)
226 assert ((*lp)->l_idx >= 0 && (*lp)->l_idx < nloaded);
228 if (!used[(*lp)->l_idx])
230 used[(*lp)->l_idx] = 1;
231 /* If we marked a new object as used, and we've
232 already processed it, then we need to go back
233 and process again from that point forward to
234 ensure we keep all of its dependencies also. */
235 if ((*lp)->l_idx - 1 < done_index)
236 done_index = (*lp)->l_idx - 1;
240 ++lp;
243 /* And the same for relocation dependencies. */
244 if (l->l_reldeps != NULL)
245 for (unsigned int j = 0; j < l->l_reldeps->act; ++j)
247 struct link_map *jmap = l->l_reldeps->list[j];
249 if (jmap->l_idx != IDX_STILL_USED)
251 assert (jmap->l_idx >= 0 && jmap->l_idx < nloaded);
253 if (!used[jmap->l_idx])
255 used[jmap->l_idx] = 1;
256 if (jmap->l_idx - 1 < done_index)
257 done_index = jmap->l_idx - 1;
263 /* Sort the entries. We can skip looking for the binary itself which is
264 at the front of the search list for the main namespace. */
265 _dl_sort_maps (maps + (nsid == LM_ID_BASE), nloaded - (nsid == LM_ID_BASE),
266 used + (nsid == LM_ID_BASE), true);
268 /* Call all termination functions at once. */
269 #ifdef SHARED
270 bool do_audit = GLRO(dl_naudit) > 0 && !ns->_ns_loaded->l_auditing;
271 #endif
272 bool unload_any = false;
273 bool scope_mem_left = false;
274 unsigned int unload_global = 0;
275 unsigned int first_loaded = ~0;
276 for (unsigned int i = 0; i < nloaded; ++i)
278 struct link_map *imap = maps[i];
280 /* All elements must be in the same namespace. */
281 assert (imap->l_ns == nsid);
283 if (!used[i])
285 assert (imap->l_type == lt_loaded && !imap->l_nodelete_active);
287 /* Call its termination function. Do not do it for
288 half-cooked objects. Temporarily disable exception
289 handling, so that errors are fatal. */
290 if (imap->l_init_called)
292 /* When debugging print a message first. */
293 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS,
295 _dl_debug_printf ("\ncalling fini: %s [%lu]\n\n",
296 imap->l_name, nsid);
298 if (imap->l_info[DT_FINI_ARRAY] != NULL
299 || imap->l_info[DT_FINI] != NULL)
300 _dl_catch_exception (NULL, call_destructors, imap);
303 #ifdef SHARED
304 /* Auditing checkpoint: we remove an object. */
305 if (__glibc_unlikely (do_audit))
307 struct audit_ifaces *afct = GLRO(dl_audit);
308 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
310 if (afct->objclose != NULL)
312 struct auditstate *state
313 = link_map_audit_state (imap, cnt);
314 /* Return value is ignored. */
315 (void) afct->objclose (&state->cookie);
318 afct = afct->next;
321 #endif
323 /* This object must not be used anymore. */
324 imap->l_removed = 1;
326 /* We indeed have an object to remove. */
327 unload_any = true;
329 if (imap->l_global)
330 ++unload_global;
332 /* Remember where the first dynamically loaded object is. */
333 if (i < first_loaded)
334 first_loaded = i;
336 /* Else used[i]. */
337 else if (imap->l_type == lt_loaded)
339 struct r_scope_elem *new_list = NULL;
341 if (imap->l_searchlist.r_list == NULL && imap->l_initfini != NULL)
343 /* The object is still used. But one of the objects we are
344 unloading right now is responsible for loading it. If
345 the current object does not have it's own scope yet we
346 have to create one. This has to be done before running
347 the finalizers.
349 To do this count the number of dependencies. */
350 unsigned int cnt;
351 for (cnt = 1; imap->l_initfini[cnt] != NULL; ++cnt)
354 /* We simply reuse the l_initfini list. */
355 imap->l_searchlist.r_list = &imap->l_initfini[cnt + 1];
356 imap->l_searchlist.r_nlist = cnt;
358 new_list = &imap->l_searchlist;
361 /* Count the number of scopes which remain after the unload.
362 When we add the local search list count it. Always add
363 one for the terminating NULL pointer. */
364 size_t remain = (new_list != NULL) + 1;
365 bool removed_any = false;
366 for (size_t cnt = 0; imap->l_scope[cnt] != NULL; ++cnt)
367 /* This relies on l_scope[] entries being always set either
368 to its own l_symbolic_searchlist address, or some map's
369 l_searchlist address. */
370 if (imap->l_scope[cnt] != &imap->l_symbolic_searchlist)
372 struct link_map *tmap = (struct link_map *)
373 ((char *) imap->l_scope[cnt]
374 - offsetof (struct link_map, l_searchlist));
375 assert (tmap->l_ns == nsid);
376 if (tmap->l_idx == IDX_STILL_USED)
377 ++remain;
378 else
379 removed_any = true;
381 else
382 ++remain;
384 if (removed_any)
386 /* Always allocate a new array for the scope. This is
387 necessary since we must be able to determine the last
388 user of the current array. If possible use the link map's
389 memory. */
390 size_t new_size;
391 struct r_scope_elem **newp;
393 #define SCOPE_ELEMS(imap) \
394 (sizeof (imap->l_scope_mem) / sizeof (imap->l_scope_mem[0]))
396 if (imap->l_scope != imap->l_scope_mem
397 && remain < SCOPE_ELEMS (imap))
399 new_size = SCOPE_ELEMS (imap);
400 newp = imap->l_scope_mem;
402 else
404 new_size = imap->l_scope_max;
405 newp = (struct r_scope_elem **)
406 malloc (new_size * sizeof (struct r_scope_elem *));
407 if (newp == NULL)
408 _dl_signal_error (ENOMEM, "dlclose", NULL,
409 N_("cannot create scope list"));
412 /* Copy over the remaining scope elements. */
413 remain = 0;
414 for (size_t cnt = 0; imap->l_scope[cnt] != NULL; ++cnt)
416 if (imap->l_scope[cnt] != &imap->l_symbolic_searchlist)
418 struct link_map *tmap = (struct link_map *)
419 ((char *) imap->l_scope[cnt]
420 - offsetof (struct link_map, l_searchlist));
421 if (tmap->l_idx != IDX_STILL_USED)
423 /* Remove the scope. Or replace with own map's
424 scope. */
425 if (new_list != NULL)
427 newp[remain++] = new_list;
428 new_list = NULL;
430 continue;
434 newp[remain++] = imap->l_scope[cnt];
436 newp[remain] = NULL;
438 struct r_scope_elem **old = imap->l_scope;
440 imap->l_scope = newp;
442 /* No user anymore, we can free it now. */
443 if (old != imap->l_scope_mem)
445 if (_dl_scope_free (old))
446 /* If _dl_scope_free used THREAD_GSCOPE_WAIT (),
447 no need to repeat it. */
448 scope_mem_left = false;
450 else
451 scope_mem_left = true;
453 imap->l_scope_max = new_size;
455 else if (new_list != NULL)
457 /* We didn't change the scope array, so reset the search
458 list. */
459 imap->l_searchlist.r_list = NULL;
460 imap->l_searchlist.r_nlist = 0;
463 /* The loader is gone, so mark the object as not having one.
464 Note: l_idx != IDX_STILL_USED -> object will be removed. */
465 if (imap->l_loader != NULL
466 && imap->l_loader->l_idx != IDX_STILL_USED)
467 imap->l_loader = NULL;
469 /* Remember where the first dynamically loaded object is. */
470 if (i < first_loaded)
471 first_loaded = i;
475 /* If there are no objects to unload, do nothing further. */
476 if (!unload_any)
477 goto out;
479 #ifdef SHARED
480 /* Auditing checkpoint: we will start deleting objects. */
481 if (__glibc_unlikely (do_audit))
483 struct link_map *head = ns->_ns_loaded;
484 struct audit_ifaces *afct = GLRO(dl_audit);
485 /* Do not call the functions for any auditing object. */
486 if (head->l_auditing == 0)
488 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
490 if (afct->activity != NULL)
492 struct auditstate *state = link_map_audit_state (head, cnt);
493 afct->activity (&state->cookie, LA_ACT_DELETE);
496 afct = afct->next;
500 #endif
502 /* Notify the debugger we are about to remove some loaded objects. */
503 struct r_debug *r = _dl_debug_initialize (0, nsid);
504 r->r_state = RT_DELETE;
505 _dl_debug_state ();
506 LIBC_PROBE (unmap_start, 2, nsid, r);
508 if (unload_global)
510 /* Some objects are in the global scope list. Remove them. */
511 struct r_scope_elem *ns_msl = ns->_ns_main_searchlist;
512 unsigned int i;
513 unsigned int j = 0;
514 unsigned int cnt = ns_msl->r_nlist;
516 while (cnt > 0 && ns_msl->r_list[cnt - 1]->l_removed)
517 --cnt;
519 if (cnt + unload_global == ns_msl->r_nlist)
520 /* Speed up removing most recently added objects. */
521 j = cnt;
522 else
523 for (i = 0; i < cnt; i++)
524 if (ns_msl->r_list[i]->l_removed == 0)
526 if (i != j)
527 ns_msl->r_list[j] = ns_msl->r_list[i];
528 j++;
530 ns_msl->r_nlist = j;
533 if (!RTLD_SINGLE_THREAD_P
534 && (unload_global
535 || scope_mem_left
536 || (GL(dl_scope_free_list) != NULL
537 && GL(dl_scope_free_list)->count)))
539 THREAD_GSCOPE_WAIT ();
541 /* Now we can free any queued old scopes. */
542 struct dl_scope_free_list *fsl = GL(dl_scope_free_list);
543 if (fsl != NULL)
544 while (fsl->count > 0)
545 free (fsl->list[--fsl->count]);
548 size_t tls_free_start;
549 size_t tls_free_end;
550 tls_free_start = tls_free_end = NO_TLS_OFFSET;
552 /* We modify the list of loaded objects. */
553 __rtld_lock_lock_recursive (GL(dl_load_write_lock));
555 /* Check each element of the search list to see if all references to
556 it are gone. */
557 for (unsigned int i = first_loaded; i < nloaded; ++i)
559 struct link_map *imap = maps[i];
560 if (!used[i])
562 assert (imap->l_type == lt_loaded);
564 /* That was the last reference, and this was a dlopen-loaded
565 object. We can unmap it. */
567 /* Remove the object from the dtv slotinfo array if it uses TLS. */
568 if (__glibc_unlikely (imap->l_tls_blocksize > 0))
570 any_tls = true;
572 if (GL(dl_tls_dtv_slotinfo_list) != NULL
573 && ! remove_slotinfo (imap->l_tls_modid,
574 GL(dl_tls_dtv_slotinfo_list), 0,
575 imap->l_init_called))
576 /* All dynamically loaded modules with TLS are unloaded. */
577 /* Can be read concurrently. */
578 atomic_store_relaxed (&GL(dl_tls_max_dtv_idx),
579 GL(dl_tls_static_nelem));
581 if (imap->l_tls_offset != NO_TLS_OFFSET
582 && imap->l_tls_offset != FORCED_DYNAMIC_TLS_OFFSET)
584 /* Collect a contiguous chunk built from the objects in
585 this search list, going in either direction. When the
586 whole chunk is at the end of the used area then we can
587 reclaim it. */
588 #if TLS_TCB_AT_TP
589 if (tls_free_start == NO_TLS_OFFSET
590 || (size_t) imap->l_tls_offset == tls_free_start)
592 /* Extend the contiguous chunk being reclaimed. */
593 tls_free_start
594 = imap->l_tls_offset - imap->l_tls_blocksize;
596 if (tls_free_end == NO_TLS_OFFSET)
597 tls_free_end = imap->l_tls_offset;
599 else if (imap->l_tls_offset - imap->l_tls_blocksize
600 == tls_free_end)
601 /* Extend the chunk backwards. */
602 tls_free_end = imap->l_tls_offset;
603 else
605 /* This isn't contiguous with the last chunk freed.
606 One of them will be leaked unless we can free
607 one block right away. */
608 if (tls_free_end == GL(dl_tls_static_used))
610 GL(dl_tls_static_used) = tls_free_start;
611 tls_free_end = imap->l_tls_offset;
612 tls_free_start
613 = tls_free_end - imap->l_tls_blocksize;
615 else if ((size_t) imap->l_tls_offset
616 == GL(dl_tls_static_used))
617 GL(dl_tls_static_used)
618 = imap->l_tls_offset - imap->l_tls_blocksize;
619 else if (tls_free_end < (size_t) imap->l_tls_offset)
621 /* We pick the later block. It has a chance to
622 be freed. */
623 tls_free_end = imap->l_tls_offset;
624 tls_free_start
625 = tls_free_end - imap->l_tls_blocksize;
628 #elif TLS_DTV_AT_TP
629 if (tls_free_start == NO_TLS_OFFSET)
631 tls_free_start = imap->l_tls_firstbyte_offset;
632 tls_free_end = (imap->l_tls_offset
633 + imap->l_tls_blocksize);
635 else if (imap->l_tls_firstbyte_offset == tls_free_end)
636 /* Extend the contiguous chunk being reclaimed. */
637 tls_free_end = imap->l_tls_offset + imap->l_tls_blocksize;
638 else if (imap->l_tls_offset + imap->l_tls_blocksize
639 == tls_free_start)
640 /* Extend the chunk backwards. */
641 tls_free_start = imap->l_tls_firstbyte_offset;
642 /* This isn't contiguous with the last chunk freed.
643 One of them will be leaked unless we can free
644 one block right away. */
645 else if (imap->l_tls_offset + imap->l_tls_blocksize
646 == GL(dl_tls_static_used))
647 GL(dl_tls_static_used) = imap->l_tls_firstbyte_offset;
648 else if (tls_free_end == GL(dl_tls_static_used))
650 GL(dl_tls_static_used) = tls_free_start;
651 tls_free_start = imap->l_tls_firstbyte_offset;
652 tls_free_end = imap->l_tls_offset + imap->l_tls_blocksize;
654 else if (tls_free_end < imap->l_tls_firstbyte_offset)
656 /* We pick the later block. It has a chance to
657 be freed. */
658 tls_free_start = imap->l_tls_firstbyte_offset;
659 tls_free_end = imap->l_tls_offset + imap->l_tls_blocksize;
661 #else
662 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
663 #endif
667 /* Reset unique symbols if forced. */
668 if (force)
670 struct unique_sym_table *tab = &ns->_ns_unique_sym_table;
671 __rtld_lock_lock_recursive (tab->lock);
672 struct unique_sym *entries = tab->entries;
673 if (entries != NULL)
675 size_t idx, size = tab->size;
676 for (idx = 0; idx < size; ++idx)
678 /* Clear unique symbol entries that belong to this
679 object. */
680 if (entries[idx].name != NULL
681 && entries[idx].map == imap)
683 entries[idx].name = NULL;
684 entries[idx].hashval = 0;
685 tab->n_elements--;
689 __rtld_lock_unlock_recursive (tab->lock);
692 /* We can unmap all the maps at once. We determined the
693 start address and length when we loaded the object and
694 the `munmap' call does the rest. */
695 DL_UNMAP (imap);
697 /* Finally, unlink the data structure and free it. */
698 #if DL_NNS == 1
699 /* The assert in the (imap->l_prev == NULL) case gives
700 the compiler license to warn that NS points outside
701 the dl_ns array bounds in that case (as nsid != LM_ID_BASE
702 is tantamount to nsid >= DL_NNS). That should be impossible
703 in this configuration, so just assert about it instead. */
704 assert (nsid == LM_ID_BASE);
705 assert (imap->l_prev != NULL);
706 #else
707 if (imap->l_prev == NULL)
709 assert (nsid != LM_ID_BASE);
710 ns->_ns_loaded = imap->l_next;
712 /* Update the pointer to the head of the list
713 we leave for debuggers to examine. */
714 r->r_map = (void *) ns->_ns_loaded;
716 else
717 #endif
718 imap->l_prev->l_next = imap->l_next;
720 --ns->_ns_nloaded;
721 if (imap->l_next != NULL)
722 imap->l_next->l_prev = imap->l_prev;
724 free (imap->l_versions);
725 if (imap->l_origin != (char *) -1)
726 free ((char *) imap->l_origin);
728 free (imap->l_reldeps);
730 /* Print debugging message. */
731 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
732 _dl_debug_printf ("\nfile=%s [%lu]; destroying link map\n",
733 imap->l_name, imap->l_ns);
735 /* This name always is allocated. */
736 free (imap->l_name);
737 /* Remove the list with all the names of the shared object. */
739 struct libname_list *lnp = imap->l_libname;
742 struct libname_list *this = lnp;
743 lnp = lnp->next;
744 if (!this->dont_free)
745 free (this);
747 while (lnp != NULL);
749 /* Remove the searchlists. */
750 free (imap->l_initfini);
752 /* Remove the scope array if we allocated it. */
753 if (imap->l_scope != imap->l_scope_mem)
754 free (imap->l_scope);
756 if (imap->l_phdr_allocated)
757 free ((void *) imap->l_phdr);
759 if (imap->l_rpath_dirs.dirs != (void *) -1)
760 free (imap->l_rpath_dirs.dirs);
761 if (imap->l_runpath_dirs.dirs != (void *) -1)
762 free (imap->l_runpath_dirs.dirs);
764 /* Clear GL(dl_initfirst) when freeing its link_map memory. */
765 if (imap == GL(dl_initfirst))
766 GL(dl_initfirst) = NULL;
768 free (imap);
772 __rtld_lock_unlock_recursive (GL(dl_load_write_lock));
774 /* If we removed any object which uses TLS bump the generation counter. */
775 if (any_tls)
777 size_t newgen = GL(dl_tls_generation) + 1;
778 if (__glibc_unlikely (newgen == 0))
779 _dl_fatal_printf ("TLS generation counter wrapped! Please report as described in "REPORT_BUGS_TO".\n");
780 /* Can be read concurrently. */
781 atomic_store_relaxed (&GL(dl_tls_generation), newgen);
783 if (tls_free_end == GL(dl_tls_static_used))
784 GL(dl_tls_static_used) = tls_free_start;
787 #ifdef SHARED
788 /* Auditing checkpoint: we have deleted all objects. */
789 if (__glibc_unlikely (do_audit))
791 struct link_map *head = ns->_ns_loaded;
792 /* If head is NULL, the namespace has become empty, and the
793 audit interface does not give us a way to signal
794 LA_ACT_CONSISTENT for it because the first loaded module is
795 used to identify the namespace.
797 Furthermore, do not notify auditors of the cleanup of a
798 failed audit module loading attempt. */
799 if (head != NULL && head->l_auditing == 0)
801 struct audit_ifaces *afct = GLRO(dl_audit);
802 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
804 if (afct->activity != NULL)
806 struct auditstate *state = link_map_audit_state (head, cnt);
807 afct->activity (&state->cookie, LA_ACT_CONSISTENT);
810 afct = afct->next;
814 #endif
816 if (__builtin_expect (ns->_ns_loaded == NULL, 0)
817 && nsid == GL(dl_nns) - 1)
819 --GL(dl_nns);
820 while (GL(dl_ns)[GL(dl_nns) - 1]._ns_loaded == NULL);
822 /* Notify the debugger those objects are finalized and gone. */
823 r->r_state = RT_CONSISTENT;
824 _dl_debug_state ();
825 LIBC_PROBE (unmap_complete, 2, nsid, r);
827 /* Recheck if we need to retry, release the lock. */
828 out:
829 if (dl_close_state == rerun)
830 goto retry;
832 dl_close_state = not_pending;
836 void
837 _dl_close (void *_map)
839 struct link_map *map = _map;
841 /* We must take the lock to examine the contents of map and avoid
842 concurrent dlopens. */
843 __rtld_lock_lock_recursive (GL(dl_load_lock));
845 /* At this point we are guaranteed nobody else is touching the list of
846 loaded maps, but a concurrent dlclose might have freed our map
847 before we took the lock. There is no way to detect this (see below)
848 so we proceed assuming this isn't the case. First see whether we
849 can remove the object at all. */
850 if (__glibc_unlikely (map->l_nodelete_active))
852 /* Nope. Do nothing. */
853 __rtld_lock_unlock_recursive (GL(dl_load_lock));
854 return;
857 /* At present this is an unreliable check except in the case where the
858 caller has recursively called dlclose and we are sure the link map
859 has not been freed. In a non-recursive dlclose the map itself
860 might have been freed and this access is potentially a data race
861 with whatever other use this memory might have now, or worse we
862 might silently corrupt memory if it looks enough like a link map.
863 POSIX has language in dlclose that appears to guarantee that this
864 should be a detectable case and given that dlclose should be threadsafe
865 we need this to be a reliable detection.
866 This is bug 20990. */
867 if (__builtin_expect (map->l_direct_opencount, 1) == 0)
869 __rtld_lock_unlock_recursive (GL(dl_load_lock));
870 _dl_signal_error (0, map->l_name, NULL, N_("shared object not open"));
873 _dl_close_worker (map, false);
875 __rtld_lock_unlock_recursive (GL(dl_load_lock));