ifunc-init.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* IFUNC generic definitions.
  2. This file is part of the GNU C Library.
  3. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  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. <http://www.gnu.org/licenses/>. */
  15. /* These macros are used to implement ifunc selection in C. To implement
  16. an ifunc function, foo, which returns the address of __foo_impl1 or
  17. __foo_impl2:
  18. #define foo __redirect_foo
  19. #include <foo.h>
  20. #undef foo
  21. #define SYMBOL_NAME foo
  22. #include <ifunc-init.h>
  23. extern __typeof (REDIRECT_NAME) OPTIMIZE (impl1) attribute_hidden;
  24. extern __typeof (REDIRECT_NAME) OPTIMIZE (impl2) attribute_hidden;
  25. static inline void *
  26. foo_selector (void)
  27. {
  28. if (condition)
  29. return OPTIMIZE (impl2);
  30. return OPTIMIZE (impl1);
  31. }
  32. libc_ifunc_redirected (__redirect_foo, foo, IFUNC_SELECTOR ());
  33. */
  34. #define PASTER1(x,y) x##_##y
  35. #define EVALUATOR1(x,y) PASTER1 (x,y)
  36. #define PASTER2(x,y) __##x##_##y
  37. #define EVALUATOR2(x,y) PASTER2 (x,y)
  38. /* Basically set '__redirect_<symbol>' to use as type definition,
  39. '__<symbol>_<variant>' as the optimized implementation and
  40. '<symbol>_ifunc_selector' as the IFUNC selector. */
  41. #define REDIRECT_NAME EVALUATOR1 (__redirect, SYMBOL_NAME)
  42. #define OPTIMIZE(name) EVALUATOR2 (SYMBOL_NAME, name)
  43. #define IFUNC_SELECTOR EVALUATOR1 (SYMBOL_NAME, ifunc_selector)