unwind-forcedunwind.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (C) 2003, 2009 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>.
  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 License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. 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; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <dlfcn.h>
  17. #include <stdio.h>
  18. #include <unwind.h>
  19. #include <pthreadP.h>
  20. #define __libc_dlopen(x) dlopen(x, (RTLD_LOCAL | RTLD_LAZY))
  21. #define __libc_dlsym dlsym
  22. #define __libc_dlclose dlclose
  23. static void *libgcc_s_handle;
  24. static void (*libgcc_s_resume) (struct _Unwind_Exception *exc);
  25. static _Unwind_Reason_Code (*libgcc_s_personality)
  26. (int, _Unwind_Action, _Unwind_Exception_Class, struct _Unwind_Exception *,
  27. struct _Unwind_Context *);
  28. static _Unwind_Reason_Code (*libgcc_s_forcedunwind)
  29. (struct _Unwind_Exception *, _Unwind_Stop_Fn, void *);
  30. static _Unwind_Word (*libgcc_s_getcfa) (struct _Unwind_Context *);
  31. static void (*libgcc_s_sjlj_register) (struct SjLj_Function_Context *);
  32. static void (*libgcc_s_sjlj_unregister) (struct SjLj_Function_Context *);
  33. void
  34. __attribute_noinline__
  35. pthread_cancel_init (void)
  36. {
  37. void *resume, *personality, *forcedunwind, *getcfa;
  38. void *handle;
  39. void *sjlj_register, *sjlj_unregister;
  40. if (__builtin_expect (libgcc_s_handle != NULL, 1))
  41. {
  42. /* Force gcc to reload all values. */
  43. asm volatile ("" ::: "memory");
  44. return;
  45. }
  46. handle = __libc_dlopen ("libgcc_s.so.1");
  47. if (handle == NULL
  48. || (sjlj_register = __libc_dlsym (handle, "_Unwind_SjLj_Register")) == NULL
  49. || (sjlj_unregister = __libc_dlsym (handle, "_Unwind_SjLj_Unregister")) == NULL
  50. || (resume = __libc_dlsym (handle, "_Unwind_SjLj_Resume")) == NULL
  51. || (personality = __libc_dlsym (handle, "__gcc_personality_sj0")) == NULL
  52. || (forcedunwind = __libc_dlsym (handle, "_Unwind_SjLj_ForcedUnwind"))
  53. == NULL
  54. || (getcfa = __libc_dlsym (handle, "_Unwind_GetCFA")) == NULL
  55. )
  56. fprintf(stderr, "libgcc_s.so.1 must be installed for pthread_cancel to work\n");
  57. libgcc_s_resume = resume;
  58. libgcc_s_personality = personality;
  59. libgcc_s_forcedunwind = forcedunwind;
  60. libgcc_s_sjlj_register = sjlj_register;
  61. libgcc_s_sjlj_unregister = sjlj_unregister;
  62. libgcc_s_getcfa = getcfa;
  63. /* Make sure libgcc_s_getcfa is written last. Otherwise,
  64. pthread_cancel_init might return early even when the pointer the
  65. caller is interested in is not initialized yet. */
  66. atomic_write_barrier ();
  67. libgcc_s_handle = handle;
  68. }
  69. void
  70. __libc_freeres_fn_section
  71. __unwind_freeres (void)
  72. {
  73. void *handle = libgcc_s_handle;
  74. if (handle != NULL)
  75. {
  76. libgcc_s_handle = NULL;
  77. __libc_dlclose (handle);
  78. }
  79. }
  80. void
  81. _Unwind_Resume (struct _Unwind_Exception *exc)
  82. {
  83. if (__builtin_expect (libgcc_s_resume == NULL, 0))
  84. pthread_cancel_init ();
  85. libgcc_s_resume (exc);
  86. }
  87. _Unwind_Reason_Code
  88. __gcc_personality_v0 (int version, _Unwind_Action actions,
  89. _Unwind_Exception_Class exception_class,
  90. struct _Unwind_Exception *ue_header,
  91. struct _Unwind_Context *context)
  92. {
  93. if (__builtin_expect (libgcc_s_personality == NULL, 0))
  94. pthread_cancel_init ();
  95. return libgcc_s_personality (version, actions, exception_class,
  96. ue_header, context);
  97. }
  98. _Unwind_Reason_Code
  99. _Unwind_ForcedUnwind (struct _Unwind_Exception *exc, _Unwind_Stop_Fn stop,
  100. void *stop_argument)
  101. {
  102. if (__builtin_expect (libgcc_s_forcedunwind == NULL, 0))
  103. pthread_cancel_init ();
  104. return libgcc_s_forcedunwind (exc, stop, stop_argument);
  105. }
  106. _Unwind_Word
  107. _Unwind_GetCFA (struct _Unwind_Context *context)
  108. {
  109. if (__builtin_expect (libgcc_s_getcfa == NULL, 0))
  110. pthread_cancel_init ();
  111. return libgcc_s_getcfa (context);
  112. }
  113. void
  114. _Unwind_SjLj_Register (struct SjLj_Function_Context *fc)
  115. {
  116. if (__builtin_expect (libgcc_s_sjlj_register == NULL, 0))
  117. pthread_cancel_init ();
  118. libgcc_s_sjlj_register (fc);
  119. }
  120. void
  121. _Unwind_SjLj_Unregister (struct SjLj_Function_Context *fc)
  122. {
  123. if (__builtin_expect (libgcc_s_sjlj_unregister == NULL, 0))
  124. pthread_cancel_init ();
  125. libgcc_s_sjlj_unregister (fc);
  126. }