dl-mutex.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* Recursive locking implementation for the dynamic loader. NPTL version.
  2. Copyright (C) 2021-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* Use the mutex implementation in libc (assuming PTHREAD_IN_LIBC). */
  16. #include <assert.h>
  17. #include <first-versions.h>
  18. #include <ldsodefs.h>
  19. __typeof (pthread_mutex_lock) *___rtld_mutex_lock attribute_relro;
  20. __typeof (pthread_mutex_unlock) *___rtld_mutex_unlock attribute_relro;
  21. void
  22. __rtld_mutex_init (void)
  23. {
  24. /* There is an implicit assumption here that the lock counters are
  25. zero and this function is called while nothing is locked. For
  26. early initialization of the mutex functions this is true because
  27. it happens directly in dl_main in elf/rtld.c, and not some ELF
  28. constructor while holding loader locks. */
  29. struct link_map *libc_map = GL (dl_ns)[LM_ID_BASE].libc_map;
  30. const ElfW(Sym) *sym
  31. = _dl_lookup_direct (libc_map, "pthread_mutex_lock",
  32. 0x4f152227, /* dl_new_hash output. */
  33. FIRST_VERSION_libc_pthread_mutex_lock_STRING,
  34. FIRST_VERSION_libc_pthread_mutex_lock_HASH);
  35. assert (sym != NULL);
  36. ___rtld_mutex_lock = DL_SYMBOL_ADDRESS (libc_map, sym);
  37. sym = _dl_lookup_direct (libc_map, "pthread_mutex_unlock",
  38. 0x7dd7aaaa, /* dl_new_hash output. */
  39. FIRST_VERSION_libc_pthread_mutex_unlock_STRING,
  40. FIRST_VERSION_libc_pthread_mutex_unlock_HASH);
  41. assert (sym != NULL);
  42. ___rtld_mutex_unlock = DL_SYMBOL_ADDRESS (libc_map, sym);
  43. }