unwind-forcedunwind.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright (C) 2003 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. static void (*libgcc_s_resume) (struct _Unwind_Exception *exc);
  23. static _Unwind_Reason_Code (*libgcc_s_personality)
  24. (int, _Unwind_Action, _Unwind_Exception_Class, struct _Unwind_Exception *,
  25. struct _Unwind_Context *);
  26. static _Unwind_Reason_Code (*libgcc_s_forcedunwind)
  27. (struct _Unwind_Exception *, _Unwind_Stop_Fn, void *);
  28. static _Unwind_Word (*libgcc_s_getcfa) (struct _Unwind_Context *);
  29. void
  30. pthread_cancel_init (void)
  31. {
  32. void *resume, *personality, *forcedunwind, *getcfa;
  33. void *handle;
  34. if (__builtin_expect (libgcc_s_getcfa != NULL, 1))
  35. return;
  36. handle = __libc_dlopen ("libgcc_s.so.1");
  37. if (handle == NULL
  38. || (resume = __libc_dlsym (handle, "_Unwind_Resume")) == NULL
  39. || (personality = __libc_dlsym (handle, "__gcc_personality_v0")) == NULL
  40. || (forcedunwind = __libc_dlsym (handle, "_Unwind_ForcedUnwind"))
  41. == NULL
  42. || (getcfa = __libc_dlsym (handle, "_Unwind_GetCFA")) == NULL
  43. #ifdef ARCH_CANCEL_INIT
  44. || ARCH_CANCEL_INIT (handle)
  45. #endif
  46. )
  47. {
  48. printf("libgcc_s.so.1 must be installed for pthread_cancel to work\n");
  49. abort();
  50. }
  51. libgcc_s_resume = resume;
  52. libgcc_s_personality = personality;
  53. libgcc_s_forcedunwind = forcedunwind;
  54. libgcc_s_getcfa = getcfa;
  55. }
  56. void
  57. _Unwind_Resume (struct _Unwind_Exception *exc)
  58. {
  59. if (__builtin_expect (libgcc_s_resume == NULL, 0))
  60. pthread_cancel_init ();
  61. libgcc_s_resume (exc);
  62. }
  63. _Unwind_Reason_Code
  64. __gcc_personality_v0 (int version, _Unwind_Action actions,
  65. _Unwind_Exception_Class exception_class,
  66. struct _Unwind_Exception *ue_header,
  67. struct _Unwind_Context *context)
  68. {
  69. if (__builtin_expect (libgcc_s_personality == NULL, 0))
  70. pthread_cancel_init ();
  71. return libgcc_s_personality (version, actions, exception_class,
  72. ue_header, context);
  73. }
  74. _Unwind_Reason_Code
  75. _Unwind_ForcedUnwind (struct _Unwind_Exception *exc, _Unwind_Stop_Fn stop,
  76. void *stop_argument)
  77. {
  78. if (__builtin_expect (libgcc_s_forcedunwind == NULL, 0))
  79. pthread_cancel_init ();
  80. return libgcc_s_forcedunwind (exc, stop, stop_argument);
  81. }
  82. _Unwind_Word
  83. _Unwind_GetCFA (struct _Unwind_Context *context)
  84. {
  85. if (__builtin_expect (libgcc_s_getcfa == NULL, 0))
  86. pthread_cancel_init ();
  87. return libgcc_s_getcfa (context);
  88. }