rtld-malloc.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* Redirection of malloc inside the dynamic linker.
  2. Copyright (C) 2020-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. /* The dynamic linker needs to use its own minimal malloc before libc
  16. has been relocated, and the libc malloc afterwards. The active
  17. malloc implementation is reached via the __rtld_* function pointers
  18. declared below. They are initialized to the minimal malloc by
  19. __rtld_malloc_init_stubs, and set to the final implementation by
  20. __rtld_malloc_init_real. */
  21. #ifndef _RTLD_MALLOC_H
  22. #define _RTLD_MALLOC_H
  23. #if IS_IN (rtld)
  24. extern __typeof (calloc) *__rtld_calloc attribute_hidden;
  25. extern __typeof (free) *__rtld_free attribute_hidden;
  26. extern __typeof (malloc) *__rtld_malloc attribute_hidden;
  27. extern __typeof (realloc) *__rtld_realloc attribute_hidden;
  28. /* Wrapper functions which call through the function pointers above.
  29. Note that it is not supported to take the address of those
  30. functions. Instead the function pointers must be used
  31. directly. */
  32. __extern_inline void *
  33. calloc (size_t a, size_t b)
  34. {
  35. return __rtld_calloc (a, b);
  36. }
  37. __extern_inline void
  38. free (void *ptr)
  39. {
  40. __rtld_free (ptr);
  41. }
  42. __extern_inline void *
  43. malloc (size_t size)
  44. {
  45. return __rtld_malloc (size);
  46. }
  47. __extern_inline void *
  48. realloc (void *ptr, size_t size)
  49. {
  50. return __rtld_realloc (ptr, size);
  51. }
  52. /* Called after the first self-relocation to activate the minimal malloc
  53. implementation. */
  54. void __rtld_malloc_init_stubs (void) attribute_hidden;
  55. /* Return false if the active malloc is the ld.so minimal malloc, true
  56. if it is the full implementation from libc.so. */
  57. _Bool __rtld_malloc_is_complete (void) attribute_hidden;
  58. /* Called shortly before the final self-relocation (when RELRO
  59. variables are still writable) to activate the real malloc
  60. implementation. MAIN_MAP is the link map of the executable. */
  61. struct link_map;
  62. void __rtld_malloc_init_real (struct link_map *main_map) attribute_hidden;
  63. #else /* !IS_IN (rtld) */
  64. /* This allows static/non-rtld builds to get a pointer to the
  65. functions, in the same way that is required inside rtld. */
  66. # define __rtld_calloc (&calloc)
  67. # define __rtld_free (&free)
  68. # define __rtld_malloc (&malloc)
  69. # define __rtld_realloc (&realloc)
  70. #endif /* !IS_IN (rtld) */
  71. #endif /* _RTLD_MALLOC_H */