1 | /* Redirection of malloc inside the dynamic linker. |
2 | Copyright (C) 2020-2021 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 | /* 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. */ |
25 | |
26 | #ifndef _RTLD_MALLOC_H |
27 | #define _RTLD_MALLOC_H |
28 | |
29 | #if IS_IN (rtld) |
30 | |
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; |
35 | |
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 |
39 | directly. */ |
40 | |
41 | __extern_inline void * |
42 | calloc (size_t a, size_t b) |
43 | { |
44 | return __rtld_calloc (a, b); |
45 | } |
46 | |
47 | __extern_inline void |
48 | free (void *ptr) |
49 | { |
50 | __rtld_free (ptr); |
51 | } |
52 | |
53 | __extern_inline void * |
54 | malloc (size_t size) |
55 | { |
56 | return __rtld_malloc (size); |
57 | } |
58 | |
59 | __extern_inline void * |
60 | realloc (void *ptr, size_t size) |
61 | { |
62 | return __rtld_realloc (ptr, size); |
63 | } |
64 | |
65 | /* Called after the first self-relocation to activate the minimal malloc |
66 | implementation. */ |
67 | void __rtld_malloc_init_stubs (void) attribute_hidden; |
68 | |
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; |
72 | |
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. */ |
76 | struct link_map; |
77 | void __rtld_malloc_init_real (struct link_map *main_map) attribute_hidden; |
78 | |
79 | #else /* !IS_IN (rtld) */ |
80 | |
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) |
87 | |
88 | #endif /* !IS_IN (rtld) */ |
89 | #endif /* _RTLD_MALLOC_H */ |
90 | |