1 /* Recursive locking implementation for the dynamic loader.  NPTL version.
2    Copyright (C) 2021-2022 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 /* Use the mutex implementation in libc (assuming PTHREAD_IN_LIBC).  */
20 
21 #include <assert.h>
22 #include <first-versions.h>
23 #include <ldsodefs.h>
24 
25 __typeof (pthread_mutex_lock) *___rtld_mutex_lock attribute_relro;
26 __typeof (pthread_mutex_unlock) *___rtld_mutex_unlock attribute_relro;
27 
28 void
__rtld_mutex_init(void)29 __rtld_mutex_init (void)
30 {
31   /* There is an implicit assumption here that the lock counters are
32      zero and this function is called while nothing is locked.  For
33      early initialization of the mutex functions this is true because
34      it happens directly in dl_main in elf/rtld.c, and not some ELF
35      constructor while holding loader locks.  */
36 
37   struct link_map *libc_map = GL (dl_ns)[LM_ID_BASE].libc_map;
38 
39   const ElfW(Sym) *sym
40     = _dl_lookup_direct (libc_map, "pthread_mutex_lock",
41                          0x4f152227, /* dl_new_hash output.  */
42                          FIRST_VERSION_libc_pthread_mutex_lock_STRING,
43                          FIRST_VERSION_libc_pthread_mutex_lock_HASH);
44   assert (sym != NULL);
45   ___rtld_mutex_lock = DL_SYMBOL_ADDRESS (libc_map, sym);
46 
47   sym = _dl_lookup_direct (libc_map, "pthread_mutex_unlock",
48                            0x7dd7aaaa, /* dl_new_hash output.  */
49                            FIRST_VERSION_libc_pthread_mutex_unlock_STRING,
50                            FIRST_VERSION_libc_pthread_mutex_unlock_HASH);
51   assert (sym != NULL);
52   ___rtld_mutex_unlock = DL_SYMBOL_ADDRESS (libc_map, sym);
53 }
54