1 /* Redirection of malloc inside the dynamic linker.
2 Copyright (C) 2020-2023 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 /* The dynamic linker needs to use its own minimal malloc before libc
20 has been relocated, and the libc malloc afterwards. The active
21 malloc implementation is reached via the __rtld_* function pointers
22 declared below. They are initialized to the minimal malloc by
23 __rtld_malloc_init_stubs, and set to the final implementation by
24 __rtld_malloc_init_real. */
26 #ifndef _RTLD_MALLOC_H
27 #define _RTLD_MALLOC_H
31 extern __typeof (calloc) *__rtld_calloc attribute_hidden;
32 extern __typeof (free) *__rtld_free attribute_hidden;
33 extern __typeof (malloc) *__rtld_malloc attribute_hidden;
34 extern __typeof (realloc) *__rtld_realloc attribute_hidden;
36 /* Wrapper functions which call through the function pointers above.
37 Note that it is not supported to take the address of those
38 functions. Instead the function pointers must be used
41 __extern_inline void *
42 calloc (size_t a, size_t b)
44 return __rtld_calloc (a, b);
53 __extern_inline void *
56 return __rtld_malloc (size);
59 __extern_inline void *
60 realloc (void *ptr, size_t size)
62 return __rtld_realloc (ptr, size);
65 /* Called after the first self-relocation to activate the minimal malloc
67 void __rtld_malloc_init_stubs (void) attribute_hidden;
69 /* Return false if the active malloc is the ld.so minimal malloc, true
70 if it is the full implementation from libc.so. */
71 _Bool __rtld_malloc_is_complete (void) attribute_hidden;
73 /* Called shortly before the final self-relocation (when RELRO
74 variables are still writable) to activate the real malloc
75 implementation. MAIN_MAP is the link map of the executable. */
77 void __rtld_malloc_init_real (struct link_map *main_map) attribute_hidden;
79 #else /* !IS_IN (rtld) */
81 /* This allows static/non-rtld builds to get a pointer to the
82 functions, in the same way that is required inside rtld. */
83 # define __rtld_calloc (&calloc)
84 # define __rtld_free (&free)
85 # define __rtld_malloc (&malloc)
86 # define __rtld_realloc (&realloc)
88 #endif /* !IS_IN (rtld) */
89 #endif /* _RTLD_MALLOC_H */