unwind-forcedunwind.c 4.7 KB

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