bug-dl-leaf-lib.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Make sure dlopen/dlclose are not marked as leaf functions.
  2. Copyright (C) 2013-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 bug-dl-leaf.c file will call our lib_main directly. We do this to
  16. keep things simple -- no need to use --export-dynamic with the linker
  17. or build the main ELF as a PIE.
  18. The lib_main func will modify some of its state while dlopening and
  19. dlclosing the bug-dl-leaf-lib-cb.so library. The constructors and
  20. destructors in that library will call back into this library to also
  21. muck with state (the check_val_xxx funcs).
  22. If dlclose/dlopen are marked as "leaf" functions, then with newer
  23. versions of gcc, the state modification won't work correctly. */
  24. #include <assert.h>
  25. #include <dlfcn.h>
  26. static int val = 1;
  27. static int called = 0;
  28. void check_val_init (void)
  29. {
  30. called = 1;
  31. assert (val == 2);
  32. }
  33. void check_val_fini (void)
  34. {
  35. called = 2;
  36. assert (val == 4);
  37. }
  38. int lib_main (void)
  39. {
  40. int ret __attribute__ ((unused));
  41. void *hdl;
  42. /* Make sure the constructor sees the updated val. */
  43. val = 2;
  44. hdl = dlopen ("bug-dl-leaf-lib-cb.so", RTLD_GLOBAL | RTLD_LAZY);
  45. val = 3;
  46. assert (hdl);
  47. assert (called == 1);
  48. /* Make sure the destructor sees the updated val. */
  49. val = 4;
  50. ret = dlclose (hdl);
  51. val = 5;
  52. assert (ret == 0);
  53. assert (called == 2);
  54. return !val;
  55. }