symbolptr.c 840 B

123456789101112131415161718192021222324252627282930313233
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2024 Google LLC
  4. *
  5. * Example for symbol pointers. When compiled with Clang, gendwarfkyms
  6. * uses a symbol pointer for `f`.
  7. *
  8. * $ clang -g -c examples/symbolptr.c -o examples/symbolptr.o
  9. * $ echo -e "f\ng\np" | ./gendwarfksyms -d examples/symbolptr.o
  10. */
  11. /* Kernel macros for userspace testing. */
  12. #ifndef __used
  13. #define __used __attribute__((__used__))
  14. #endif
  15. #ifndef __section
  16. #define __section(section) __attribute__((__section__(section)))
  17. #endif
  18. #define __GENDWARFKSYMS_EXPORT(sym) \
  19. static typeof(sym) *__gendwarfksyms_ptr_##sym __used \
  20. __section(".discard.gendwarfksyms") = &sym;
  21. extern void f(unsigned int arg);
  22. void g(int *arg);
  23. void g(int *arg) {}
  24. struct s;
  25. extern struct s *p;
  26. __GENDWARFKSYMS_EXPORT(f);
  27. __GENDWARFKSYMS_EXPORT(g);
  28. __GENDWARFKSYMS_EXPORT(p);