universally apply our cflags (no vsx, no altivec..)
[glibc.git] / fbtl / unwind.c
1 /* Copyright (C) 2003-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>
4 and Richard Henderson <rth@redhat.com>, 2003.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <setjmp.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include "pthreadP.h"
25 #include <jmpbuf-unwind.h>
26
27 #ifdef _STACK_GROWS_DOWN
28 # define FRAME_LEFT(frame, other, adj) \
29 ((uintptr_t) frame - adj >= (uintptr_t) other - adj)
30 #elif _STACK_GROWS_UP
31 # define FRAME_LEFT(frame, other, adj) \
32 ((uintptr_t) frame - adj <= (uintptr_t) other - adj)
33 #else
34 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
35 #endif
36
37 static _Unwind_Reason_Code
38 unwind_stop (int version, _Unwind_Action actions,
39 _Unwind_Exception_Class exc_class,
40 struct _Unwind_Exception *exc_obj,
41 struct _Unwind_Context *context, void *stop_parameter)
42 {
43 struct pthread_unwind_buf *buf = stop_parameter;
44 struct pthread *self = THREAD_SELF;
45 struct _pthread_cleanup_buffer *curp = THREAD_GETMEM (self, cleanup);
46 int do_longjump = 0;
47
48 /* Adjust all pointers used in comparisons, so that top of thread's
49 stack is at the top of address space. Without that, things break
50 if stack is allocated above the main stack. */
51 uintptr_t adj = (uintptr_t) self->stackblock + self->stackblock_size;
52
53 /* Do longjmp if we're at "end of stack", aka "end of unwind data".
54 We assume there are only C frame without unwind data in between
55 here and the jmp_buf target. Otherwise simply note that the CFA
56 of a function is NOT within it's stack frame; it's the SP of the
57 previous frame. */
58 if ((actions & _UA_END_OF_STACK)
59 || ! _JMPBUF_CFA_UNWINDS_ADJ (buf->cancel_jmp_buf[0].jmp_buf, context,
60 adj))
61 do_longjump = 1;
62
63 if (__glibc_unlikely (curp != NULL))
64 {
65 /* Handle the compatibility stuff. Execute all handlers
66 registered with the old method which would be unwound by this
67 step. */
68 struct _pthread_cleanup_buffer *oldp = buf->priv.data.cleanup;
69 void *cfa = (void *) (_Unwind_Ptr) _Unwind_GetCFA (context);
70
71 if (curp != oldp && (do_longjump || FRAME_LEFT (cfa, curp, adj)))
72 {
73 do
74 {
75 /* Pointer to the next element. */
76 struct _pthread_cleanup_buffer *nextp = curp->__prev;
77
78 /* Call the handler. */
79 curp->__routine (curp->__arg);
80
81 /* To the next. */
82 curp = nextp;
83 }
84 while (curp != oldp
85 && (do_longjump || FRAME_LEFT (cfa, curp, adj)));
86
87 /* Mark the current element as handled. */
88 THREAD_SETMEM (self, cleanup, curp);
89 }
90 }
91
92 if (do_longjump)
93 __libc_unwind_longjmp ((struct __jmp_buf_tag *) buf->cancel_jmp_buf, 1);
94
95 return _URC_NO_REASON;
96 }
97
98
99 static void
100 unwind_cleanup (_Unwind_Reason_Code reason, struct _Unwind_Exception *exc)
101 {
102 /* When we get here a C++ catch block didn't rethrow the object. We
103 cannot handle this case and therefore abort. */
104 #if 0 // does not work due to multiple macro expansions
105 # define STR_N_LEN(str) str, strlen (str)
106 INTERNAL_SYSCALL_DECL (err);
107 INTERNAL_SYSCALL (write, err, 3, STDERR_FILENO,
108 STR_N_LEN ("FATAL: exception not rethrown\n"));
109 #else
110 # define UNWIND_FAILED_MSG "FATAL: exception not rethrown\n"
111 INTERNAL_SYSCALL_DECL (err);
112 INTERNAL_SYSCALL (write, err, 3, STDERR_FILENO,
113 UNWIND_FAILED_MSG, strlen(UNWIND_FAILED_MSG));
114 #endif
115 abort ();
116 }
117
118
119 void
120 __cleanup_fct_attribute __attribute ((noreturn))
121 __pthread_unwind (__pthread_unwind_buf_t *buf)
122 {
123 struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
124 struct pthread *self = THREAD_SELF;
125
126 /* This is not a catchable exception, so don't provide any details about
127 the exception type. We do need to initialize the field though. */
128 THREAD_SETMEM (self, exc.exception_class, 0);
129 THREAD_SETMEM (self, exc.exception_cleanup, &unwind_cleanup);
130
131 _Unwind_ForcedUnwind (&self->exc, unwind_stop, ibuf);
132 /* NOTREACHED */
133
134 /* We better do not get here. */
135 abort ();
136 }
137 hidden_def (__pthread_unwind)
138
139
140 void
141 __cleanup_fct_attribute __attribute ((noreturn))
142 __pthread_unwind_next (__pthread_unwind_buf_t *buf)
143 {
144 struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
145
146 __pthread_unwind ((__pthread_unwind_buf_t *) ibuf->priv.data.prev);
147 }
148 hidden_def (__pthread_unwind_next)