| 1 | /* Copyright (C) 2003-2020 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Jakub Jelinek <jakub@redhat.com>. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public License as |
| 7 | published by the Free Software Foundation; either version 2.1 of the |
| 8 | License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; see the file COPYING.LIB. If |
| 17 | not, see <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <dlfcn.h> |
| 20 | #include <stdio.h> |
| 21 | #include <unwind.h> |
| 22 | #include <pthreadP.h> |
| 23 | #include <sysdep.h> |
| 24 | #include <gnu/lib-names.h> |
| 25 | #include <unwind-resume.h> |
| 26 | |
| 27 | static void *libgcc_s_handle; |
| 28 | void (*__libgcc_s_resume) (struct _Unwind_Exception *exc) |
| 29 | attribute_hidden __attribute__ ((noreturn)); |
| 30 | static _Unwind_Reason_Code (*libgcc_s_personality) PERSONALITY_PROTO; |
| 31 | static _Unwind_Reason_Code (*libgcc_s_forcedunwind) |
| 32 | (struct _Unwind_Exception *, _Unwind_Stop_Fn, void *); |
| 33 | static _Unwind_Word (*libgcc_s_getcfa) (struct _Unwind_Context *); |
| 34 | |
| 35 | void |
| 36 | __attribute_noinline__ |
| 37 | pthread_cancel_init (void) |
| 38 | { |
| 39 | void *resume; |
| 40 | void *personality; |
| 41 | void *forcedunwind; |
| 42 | void *getcfa; |
| 43 | void *handle; |
| 44 | |
| 45 | if (__glibc_likely (libgcc_s_handle != NULL)) |
| 46 | { |
| 47 | /* Force gcc to reload all values. */ |
| 48 | asm volatile ("" ::: "memory" ); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | /* See include/dlfcn.h. Use of __libc_dlopen requires RTLD_NOW. */ |
| 53 | handle = __libc_dlopen (LIBGCC_S_SO); |
| 54 | |
| 55 | if (handle == NULL |
| 56 | || (resume = __libc_dlsym (handle, "_Unwind_Resume" )) == NULL |
| 57 | || (personality = __libc_dlsym (handle, "__gcc_personality_v0" )) == NULL |
| 58 | || (forcedunwind = __libc_dlsym (handle, "_Unwind_ForcedUnwind" )) |
| 59 | == NULL |
| 60 | || (getcfa = __libc_dlsym (handle, "_Unwind_GetCFA" )) == NULL |
| 61 | #ifdef ARCH_CANCEL_INIT |
| 62 | || ARCH_CANCEL_INIT (handle) |
| 63 | #endif |
| 64 | ) |
| 65 | __libc_fatal (LIBGCC_S_SO " must be installed for pthread_cancel to work\n" ); |
| 66 | |
| 67 | PTR_MANGLE (resume); |
| 68 | __libgcc_s_resume = resume; |
| 69 | PTR_MANGLE (personality); |
| 70 | libgcc_s_personality = personality; |
| 71 | PTR_MANGLE (forcedunwind); |
| 72 | libgcc_s_forcedunwind = forcedunwind; |
| 73 | PTR_MANGLE (getcfa); |
| 74 | libgcc_s_getcfa = getcfa; |
| 75 | /* Make sure libgcc_s_handle is written last. Otherwise, |
| 76 | pthread_cancel_init might return early even when the pointer the |
| 77 | caller is interested in is not initialized yet. */ |
| 78 | atomic_write_barrier (); |
| 79 | libgcc_s_handle = handle; |
| 80 | } |
| 81 | |
| 82 | /* Register for cleanup in libpthread.so. */ |
| 83 | void |
| 84 | __nptl_unwind_freeres (void) |
| 85 | { |
| 86 | void *handle = libgcc_s_handle; |
| 87 | if (handle != NULL) |
| 88 | { |
| 89 | libgcc_s_handle = NULL; |
| 90 | __libc_dlclose (handle); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | #if !HAVE_ARCH_UNWIND_RESUME |
| 95 | void |
| 96 | _Unwind_Resume (struct _Unwind_Exception *exc) |
| 97 | { |
| 98 | if (__glibc_unlikely (libgcc_s_handle == NULL)) |
| 99 | pthread_cancel_init (); |
| 100 | else |
| 101 | atomic_read_barrier (); |
| 102 | |
| 103 | void (*resume) (struct _Unwind_Exception *exc) = __libgcc_s_resume; |
| 104 | PTR_DEMANGLE (resume); |
| 105 | resume (exc); |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | _Unwind_Reason_Code |
| 110 | __gcc_personality_v0 PERSONALITY_PROTO |
| 111 | { |
| 112 | if (__glibc_unlikely (libgcc_s_handle == NULL)) |
| 113 | pthread_cancel_init (); |
| 114 | else |
| 115 | atomic_read_barrier (); |
| 116 | |
| 117 | __typeof (libgcc_s_personality) personality = libgcc_s_personality; |
| 118 | PTR_DEMANGLE (personality); |
| 119 | return (*personality) PERSONALITY_ARGS; |
| 120 | } |
| 121 | |
| 122 | _Unwind_Reason_Code |
| 123 | _Unwind_ForcedUnwind (struct _Unwind_Exception *exc, _Unwind_Stop_Fn stop, |
| 124 | void *stop_argument) |
| 125 | { |
| 126 | if (__glibc_unlikely (libgcc_s_handle == NULL)) |
| 127 | pthread_cancel_init (); |
| 128 | else |
| 129 | atomic_read_barrier (); |
| 130 | |
| 131 | _Unwind_Reason_Code (*forcedunwind) |
| 132 | (struct _Unwind_Exception *, _Unwind_Stop_Fn, void *) |
| 133 | = libgcc_s_forcedunwind; |
| 134 | PTR_DEMANGLE (forcedunwind); |
| 135 | return forcedunwind (exc, stop, stop_argument); |
| 136 | } |
| 137 | |
| 138 | _Unwind_Word |
| 139 | _Unwind_GetCFA (struct _Unwind_Context *context) |
| 140 | { |
| 141 | if (__glibc_unlikely (libgcc_s_handle == NULL)) |
| 142 | pthread_cancel_init (); |
| 143 | else |
| 144 | atomic_read_barrier (); |
| 145 | |
| 146 | _Unwind_Word (*getcfa) (struct _Unwind_Context *) = libgcc_s_getcfa; |
| 147 | PTR_DEMANGLE (getcfa); |
| 148 | return getcfa (context); |
| 149 | } |
| 150 | |