unwind-forcedunwind.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Copyright (C) 2003, 2005, 2007, 2009, 2010 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
  14. not, see <http://www.gnu.org/licenses/>. */
  15. #include <dlfcn.h>
  16. #include <stdio.h>
  17. #include <unwind.h>
  18. #include <pthreadP.h>
  19. #define __libc_dlopen(x) dlopen(x, (RTLD_LOCAL | RTLD_LAZY))
  20. #define __libc_dlsym dlsym
  21. #define __libc_dlclose dlclose
  22. #define __libc_fatal(x) {/*write(STDERR_FILENO, x, strlen(x));*/ abort();}
  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. (_Unwind_State, struct _Unwind_Exception *, struct _Unwind_Context *);
  27. static _Unwind_Reason_Code (*libgcc_s_forcedunwind)
  28. (struct _Unwind_Exception *, _Unwind_Stop_Fn, void *);
  29. static _Unwind_Word (*libgcc_s_getcfa) (struct _Unwind_Context *);
  30. void
  31. __attribute_noinline__
  32. pthread_cancel_init (void)
  33. {
  34. void *resume, *personality, *forcedunwind, *getcfa;
  35. void *handle;
  36. if (__builtin_expect (libgcc_s_handle != NULL, 1))
  37. {
  38. /* Force gcc to reload all values. */
  39. __asm__ __volatile__ ("" ::: "memory");
  40. return;
  41. }
  42. handle = __libc_dlopen ("libgcc_s.so.1");
  43. if (handle == NULL
  44. || (resume = __libc_dlsym (handle, "_Unwind_Resume")) == NULL
  45. || (personality = __libc_dlsym (handle, "__gcc_personality_v0")) == NULL
  46. || (forcedunwind = __libc_dlsym (handle, "_Unwind_ForcedUnwind"))
  47. == NULL
  48. || (getcfa = __libc_dlsym (handle, "_Unwind_GetCFA")) == NULL
  49. #ifdef ARCH_CANCEL_INIT
  50. || ARCH_CANCEL_INIT (handle)
  51. #endif
  52. )
  53. __libc_fatal ("libgcc_s.so.1 must be installed for pthread_cancel to work\n");
  54. libgcc_s_resume = resume;
  55. libgcc_s_personality = personality;
  56. libgcc_s_forcedunwind = forcedunwind;
  57. libgcc_s_getcfa = getcfa;
  58. /* Make sure libgcc_s_getcfa is written last. Otherwise,
  59. pthread_cancel_init might return early even when the pointer the
  60. caller is interested in is not initialized yet. */
  61. atomic_write_barrier ();
  62. libgcc_s_handle = handle;
  63. }
  64. void
  65. __libc_freeres_fn_section
  66. __unwind_freeres (void)
  67. {
  68. void *handle = libgcc_s_handle;
  69. if (handle != NULL)
  70. {
  71. libgcc_s_handle = NULL;
  72. __libc_dlclose (handle);
  73. }
  74. }
  75. #ifdef __thumb__
  76. void
  77. _Unwind_Resume (struct _Unwind_Exception *exc)
  78. {
  79. if (__builtin_expect (libgcc_s_resume == NULL, 0))
  80. pthread_cancel_init ();
  81. libgcc_s_resume (exc);
  82. }
  83. #else
  84. /* It's vitally important that _Unwind_Resume not have a stack frame; the
  85. ARM unwinder relies on register state at entrance. So we write this in
  86. assembly. */
  87. __asm__ (
  88. " .globl _Unwind_Resume\n"
  89. " .type _Unwind_Resume, %function\n"
  90. "_Unwind_Resume:\n"
  91. " .cfi_sections .debug_frame\n"
  92. " " CFI_STARTPROC "\n"
  93. " stmfd sp!, {r4, r5, r6, lr}\n"
  94. " " CFI_ADJUST_CFA_OFFSET (16)" \n"
  95. " " CFI_REL_OFFSET (r4, 0) "\n"
  96. " " CFI_REL_OFFSET (r5, 4) "\n"
  97. " " CFI_REL_OFFSET (r6, 8) "\n"
  98. " " CFI_REL_OFFSET (lr, 12) "\n"
  99. " " CFI_REMEMBER_STATE "\n"
  100. " ldr r4, 1f\n"
  101. " ldr r5, 2f\n"
  102. "3: add r4, pc, r4\n"
  103. " ldr r3, [r4, r5]\n"
  104. " mov r6, r0\n"
  105. " cmp r3, #0\n"
  106. " beq 4f\n"
  107. "5: mov r0, r6\n"
  108. " ldmfd sp!, {r4, r5, r6, lr}\n"
  109. " " CFI_ADJUST_CFA_OFFSET (-16) "\n"
  110. " " CFI_RESTORE (r4) "\n"
  111. " " CFI_RESTORE (r5) "\n"
  112. " " CFI_RESTORE (r6) "\n"
  113. " " CFI_RESTORE (lr) "\n"
  114. " bx r3\n"
  115. " " CFI_RESTORE_STATE "\n"
  116. "4: bl pthread_cancel_init\n"
  117. " ldr r3, [r4, r5]\n"
  118. " b 5b\n"
  119. " " CFI_ENDPROC "\n"
  120. " .align 2\n"
  121. #ifdef __thumb2__
  122. "1: .word _GLOBAL_OFFSET_TABLE_ - 3b - 4\n"
  123. #else
  124. "1: .word _GLOBAL_OFFSET_TABLE_ - 3b - 8\n"
  125. #endif
  126. "2: .word libgcc_s_resume(GOTOFF)\n"
  127. " .size _Unwind_Resume, .-_Unwind_Resume\n"
  128. );
  129. #endif
  130. _Unwind_Reason_Code
  131. __gcc_personality_v0 (_Unwind_State state,
  132. struct _Unwind_Exception *ue_header,
  133. struct _Unwind_Context *context)
  134. {
  135. if (__builtin_expect (libgcc_s_personality == NULL, 0))
  136. pthread_cancel_init ();
  137. return libgcc_s_personality (state, ue_header, context);
  138. }
  139. _Unwind_Reason_Code
  140. _Unwind_ForcedUnwind (struct _Unwind_Exception *exc, _Unwind_Stop_Fn stop,
  141. void *stop_argument)
  142. {
  143. if (__builtin_expect (libgcc_s_forcedunwind == NULL, 0))
  144. pthread_cancel_init ();
  145. return libgcc_s_forcedunwind (exc, stop, stop_argument);
  146. }
  147. _Unwind_Word
  148. _Unwind_GetCFA (struct _Unwind_Context *context)
  149. {
  150. if (__builtin_expect (libgcc_s_getcfa == NULL, 0))
  151. pthread_cancel_init ();
  152. return libgcc_s_getcfa (context);
  153. }