set-hooks.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Macros for using symbol sets for running lists of functions.
  2. Copyright (C) 1994-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. #ifndef _SET_HOOKS_H
  16. #define _SET_HOOKS_H 1
  17. #define __need_size_t
  18. #include <stddef.h>
  19. #include <sys/cdefs.h>
  20. #include <libc-symbols.h>
  21. #include "set-hooks-arch.h"
  22. #ifdef symbol_set_define
  23. /* Define a hook variable called NAME. Functions put on this hook take
  24. arguments described by PROTO. Use `text_set_element (NAME, FUNCTION)'
  25. from include/libc-symbols.h to add a function to the hook. */
  26. # define DEFINE_HOOK(NAME, PROTO) \
  27. typedef void __##NAME##_hook_function_t PROTO; \
  28. symbol_set_define (NAME)
  29. # define DECLARE_HOOK(NAME, PROTO) \
  30. typedef void __##NAME##_hook_function_t PROTO;\
  31. symbol_set_declare (NAME)
  32. /* Run all the functions hooked on the set called NAME.
  33. Each function is called like this: `function ARGS'. */
  34. # define RUN_HOOK(NAME, ARGS) \
  35. do { \
  36. void *const *ptr; \
  37. for (ptr = (void *const *) symbol_set_first_element (NAME); \
  38. ! symbol_set_end_p (NAME, ptr); ++ptr) \
  39. (*(__##NAME##_hook_function_t *) *ptr) ARGS; \
  40. } while (0)
  41. /* Define a hook variable with NAME and PROTO, and a function called RUNNER
  42. which calls each function on the hook in turn, with ARGS. */
  43. # define DEFINE_HOOK_RUNNER(name, runner, proto, args) \
  44. DEFINE_HOOK (name, proto); \
  45. extern void runner proto; void runner proto { RUN_HOOK (name, args); }
  46. # ifdef SET_RELHOOK
  47. /* This is similar to RUN_RELHOOK, but the hooks were registered with
  48. * SET_RELHOOK so that a relative offset was computed by the linker
  49. * rather than an absolute address by the dynamic linker. */
  50. # define RUN_RELHOOK(NAME, ARGS) \
  51. do { \
  52. void *const *ptr; \
  53. for (ptr = (void *const *) symbol_set_first_element (NAME); \
  54. ! symbol_set_end_p (NAME, ptr); ++ptr) { \
  55. __##NAME##_hook_function_t *f = \
  56. (void*) ((uintptr_t) ptr + (ptrdiff_t) *ptr); \
  57. (*f) ARGS; \
  58. } \
  59. } while (0)
  60. # else
  61. # define SET_RELHOOK(NAME, HOOK) text_set_element (NAME, HOOK)
  62. # define RUN_RELHOOK(NAME, ARGS) RUN_HOOK(NAME, ARGS)
  63. # endif
  64. #else
  65. /* The system does not provide necessary support for this. */
  66. # define DEFINE_HOOK(NAME, PROTO)
  67. # define DECLARE_HOOK(NAME, PROTO)
  68. # define RUN_HOOK(NAME, ARGS)
  69. # define DEFINE_HOOK_RUNNER(name, runner, proto, args)
  70. # define SET_RELHOOK(NAME, HOOK)
  71. # define RUN_RELHOOK(NAME, ARGS)
  72. #endif
  73. #endif /* set-hooks.h */