unwind-forcedunwind.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (C) 2003, 2005 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 <string.h>
  18. #include <unwind.h>
  19. #include <unistd.h>
  20. #include <pthreadP.h>
  21. #define __libc_dlopen(x) dlopen(x, (RTLD_LOCAL | RTLD_LAZY))
  22. #define __libc_dlsym dlsym
  23. static void (*libgcc_s_resume) (struct _Unwind_Exception *exc);
  24. static _Unwind_Reason_Code (*libgcc_s_personality)
  25. (_Unwind_State, struct _Unwind_Exception *, 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. # define STR_N_LEN(str) str, strlen (str)
  49. INTERNAL_SYSCALL_DECL (err);
  50. INTERNAL_SYSCALL (write, err, 3, STDERR_FILENO,
  51. STR_N_LEN ("libgcc_s.so.1 must be installed for pthread_cancel to work\n"));
  52. abort ();
  53. }
  54. libgcc_s_resume = resume;
  55. libgcc_s_personality = personality;
  56. libgcc_s_forcedunwind = forcedunwind;
  57. libgcc_s_getcfa = getcfa;
  58. }
  59. /* It's vitally important that _Unwind_Resume not have a stack frame; the
  60. ARM unwinder relies on register state at entrance. So we write this in
  61. assembly. */
  62. asm (
  63. #ifdef __thumb__
  64. " .code 32"
  65. #endif
  66. " .globl _Unwind_Resume\n"
  67. " .type _Unwind_Resume, %function\n"
  68. "_Unwind_Resume:\n"
  69. " stmfd sp!, {r4, r5, r6, lr}\n"
  70. " ldr r4, 1f\n"
  71. " ldr r5, 2f\n"
  72. "3: add r4, pc, r4\n"
  73. " ldr r3, [r4, r5]\n"
  74. " mov r6, r0\n"
  75. " cmp r3, #0\n"
  76. " beq 4f\n"
  77. "5: mov r0, r6\n"
  78. " ldmfd sp!, {r4, r5, r6, lr}\n"
  79. " bx r3\n"
  80. "4: bl pthread_cancel_init\n"
  81. " ldr r3, [r4, r5]\n"
  82. " b 5b\n"
  83. "1: .word _GLOBAL_OFFSET_TABLE_ - 3b - 8\n"
  84. "2: .word libgcc_s_resume(GOTOFF)\n"
  85. " .size _Unwind_Resume, .-_Unwind_Resume\n"
  86. #ifdef __thumb__
  87. " .code 16"
  88. #endif
  89. );
  90. _Unwind_Reason_Code
  91. __gcc_personality_v0 (_Unwind_State state,
  92. struct _Unwind_Exception *ue_header,
  93. struct _Unwind_Context *context)
  94. {
  95. if (__builtin_expect (libgcc_s_personality == NULL, 0))
  96. pthread_cancel_init ();
  97. return libgcc_s_personality (state, ue_header, context);
  98. }
  99. _Unwind_Reason_Code
  100. _Unwind_ForcedUnwind (struct _Unwind_Exception *exc, _Unwind_Stop_Fn stop,
  101. void *stop_argument)
  102. {
  103. if (__builtin_expect (libgcc_s_forcedunwind == NULL, 0))
  104. pthread_cancel_init ();
  105. return libgcc_s_forcedunwind (exc, stop, stop_argument);
  106. }
  107. _Unwind_Word
  108. _Unwind_GetCFA (struct _Unwind_Context *context)
  109. {
  110. if (__builtin_expect (libgcc_s_getcfa == NULL, 0))
  111. pthread_cancel_init ();
  112. return libgcc_s_getcfa (context);
  113. }