Update copyright notices with scripts/update-copyrights
[glibc.git] / ports / sysdeps / unix / sysv / linux / arm / nptl / unwind-forcedunwind.c
blob6ccd9b43a1223c7fb01fb52cf282380231645639
1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library. If not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <dlfcn.h>
20 #include <stdio.h>
21 #include <unwind.h>
22 #include <pthreadP.h>
24 static void *libgcc_s_handle;
25 static void (*libgcc_s_resume) (struct _Unwind_Exception *exc);
26 static _Unwind_Reason_Code (*libgcc_s_personality)
27 (_Unwind_State, struct _Unwind_Exception *, struct _Unwind_Context *);
28 static _Unwind_Reason_Code (*libgcc_s_forcedunwind)
29 (struct _Unwind_Exception *, _Unwind_Stop_Fn, void *);
30 static _Unwind_Word (*libgcc_s_getcfa) (struct _Unwind_Context *);
32 void
33 __attribute_noinline__
34 pthread_cancel_init (void)
36 void *resume, *personality, *forcedunwind, *getcfa;
37 void *handle;
39 if (__builtin_expect (libgcc_s_handle != NULL, 1))
41 /* Force gcc to reload all values. */
42 asm volatile ("" ::: "memory");
43 return;
46 handle = __libc_dlopen ("libgcc_s.so.1");
48 if (handle == NULL
49 || (resume = __libc_dlsym (handle, "_Unwind_Resume")) == NULL
50 || (personality = __libc_dlsym (handle, "__gcc_personality_v0")) == NULL
51 || (forcedunwind = __libc_dlsym (handle, "_Unwind_ForcedUnwind"))
52 == NULL
53 || (getcfa = __libc_dlsym (handle, "_Unwind_GetCFA")) == NULL
54 #ifdef ARCH_CANCEL_INIT
55 || ARCH_CANCEL_INIT (handle)
56 #endif
58 __libc_fatal ("libgcc_s.so.1 must be installed for pthread_cancel to work\n");
60 libgcc_s_resume = resume;
61 libgcc_s_personality = personality;
62 libgcc_s_forcedunwind = forcedunwind;
63 libgcc_s_getcfa = getcfa;
64 /* Make sure libgcc_s_getcfa is written last. Otherwise,
65 pthread_cancel_init might return early even when the pointer the
66 caller is interested in is not initialized yet. */
67 atomic_write_barrier ();
68 libgcc_s_handle = handle;
71 void
72 __libc_freeres_fn_section
73 __unwind_freeres (void)
75 void *handle = libgcc_s_handle;
76 if (handle != NULL)
78 libgcc_s_handle = NULL;
79 __libc_dlclose (handle);
83 /* It's vitally important that _Unwind_Resume not have a stack frame; the
84 ARM unwinder relies on register state at entrance. So we write this in
85 assembly. */
87 #define STR1(S) #S
88 #define STR(S) STR1(S)
90 asm (
91 " .globl _Unwind_Resume\n"
92 " .type _Unwind_Resume, %function\n"
93 "_Unwind_Resume:\n"
94 " .cfi_sections .debug_frame\n"
95 " " CFI_STARTPROC "\n"
96 " push {r4, r5, r6, lr}\n"
97 " " CFI_ADJUST_CFA_OFFSET (16)" \n"
98 " " CFI_REL_OFFSET (r4, 0) "\n"
99 " " CFI_REL_OFFSET (r5, 4) "\n"
100 " " CFI_REL_OFFSET (r6, 8) "\n"
101 " " CFI_REL_OFFSET (lr, 12) "\n"
102 " " CFI_REMEMBER_STATE "\n"
103 " ldr r4, 1f\n"
104 " ldr r5, 2f\n"
105 "3: add r4, pc, r4\n"
106 " ldr r3, [r4, r5]\n"
107 " mov r6, r0\n"
108 " cmp r3, #0\n"
109 " beq 4f\n"
110 "5: mov r0, r6\n"
111 " pop {r4, r5, r6, lr}\n"
112 " " CFI_ADJUST_CFA_OFFSET (-16) "\n"
113 " " CFI_RESTORE (r4) "\n"
114 " " CFI_RESTORE (r5) "\n"
115 " " CFI_RESTORE (r6) "\n"
116 " " CFI_RESTORE (lr) "\n"
117 " bx r3\n"
118 " " CFI_RESTORE_STATE "\n"
119 "4: bl pthread_cancel_init\n"
120 " ldr r3, [r4, r5]\n"
121 " b 5b\n"
122 " " CFI_ENDPROC "\n"
123 " .align 2\n"
124 "1: .word _GLOBAL_OFFSET_TABLE_ - 3b - " STR (PC_OFS) "\n"
125 "2: .word libgcc_s_resume(GOTOFF)\n"
126 " .size _Unwind_Resume, .-_Unwind_Resume\n"
129 _Unwind_Reason_Code
130 __gcc_personality_v0 (_Unwind_State state,
131 struct _Unwind_Exception *ue_header,
132 struct _Unwind_Context *context)
134 if (__builtin_expect (libgcc_s_personality == NULL, 0))
135 pthread_cancel_init ();
137 return libgcc_s_personality (state, ue_header, context);
140 _Unwind_Reason_Code
141 _Unwind_ForcedUnwind (struct _Unwind_Exception *exc, _Unwind_Stop_Fn stop,
142 void *stop_argument)
144 if (__builtin_expect (libgcc_s_forcedunwind == NULL, 0))
145 pthread_cancel_init ();
147 return libgcc_s_forcedunwind (exc, stop, stop_argument);
150 _Unwind_Word
151 _Unwind_GetCFA (struct _Unwind_Context *context)
153 if (__builtin_expect (libgcc_s_getcfa == NULL, 0))
154 pthread_cancel_init ();
156 return libgcc_s_getcfa (context);