annotate libgcc/unwind-seh.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Structured Exception Handling (SEH) runtime interface routines.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2010-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it
kono
parents:
diff changeset
7 under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
8 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
9 any later version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT
kono
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
kono
parents:
diff changeset
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
kono
parents:
diff changeset
14 License for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
17 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
18 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
21 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
23 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 #include "tconfig.h"
kono
parents:
diff changeset
26 #include "tsystem.h"
kono
parents:
diff changeset
27 #include "coretypes.h"
kono
parents:
diff changeset
28 #include "tm.h"
kono
parents:
diff changeset
29 #include "unwind.h"
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 /* At the moment everything is written for x64, but in theory this could
kono
parents:
diff changeset
34 also be used for i386, arm, mips and other extant embedded Windows. */
kono
parents:
diff changeset
35 #ifndef __x86_64__
kono
parents:
diff changeset
36 #error "Unsupported architecture."
kono
parents:
diff changeset
37 #endif
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 /* Define GCC's exception codes. See
kono
parents:
diff changeset
40 http://msdn.microsoft.com/en-us/library/het71c37(v=VS.80).aspx
kono
parents:
diff changeset
41 In particular, MS defines bits:
kono
parents:
diff changeset
42 [31:30] = 3 (error), 2 (warning), 1 (info), 0 (success)
kono
parents:
diff changeset
43 [29] = 1 (user-defined)
kono
parents:
diff changeset
44 [28] = 0 (reserved)
kono
parents:
diff changeset
45 We define bits:
kono
parents:
diff changeset
46 [24:27] = type
kono
parents:
diff changeset
47 [0:23] = magic
kono
parents:
diff changeset
48 We set "magic" to "GCC", which is similar to MVC++ which uses "msc"
kono
parents:
diff changeset
49 as the low 3 bytes of its user-defined codes for C++ exceptions.
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 We define the ExceptionInformation entries as follows:
kono
parents:
diff changeset
52 [0] = _Unwind_Exception pointer
kono
parents:
diff changeset
53 [1] = target frame
kono
parents:
diff changeset
54 [2] = target ip
kono
parents:
diff changeset
55 [3] = target rdx
kono
parents:
diff changeset
56 */
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 #define STATUS_USER_DEFINED (1U << 29)
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 #define GCC_MAGIC (('G' << 16) | ('C' << 8) | 'C')
kono
parents:
diff changeset
61 #define GCC_EXCEPTION(TYPE) \
kono
parents:
diff changeset
62 (STATUS_USER_DEFINED | ((TYPE) << 24) | GCC_MAGIC)
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 #define STATUS_GCC_THROW GCC_EXCEPTION (0)
kono
parents:
diff changeset
65 #define STATUS_GCC_UNWIND GCC_EXCEPTION (1)
kono
parents:
diff changeset
66 #define STATUS_GCC_FORCED GCC_EXCEPTION (2)
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 struct _Unwind_Context
kono
parents:
diff changeset
70 {
kono
parents:
diff changeset
71 _Unwind_Word cfa;
kono
parents:
diff changeset
72 _Unwind_Word ra;
kono
parents:
diff changeset
73 _Unwind_Word reg[2];
kono
parents:
diff changeset
74 PDISPATCHER_CONTEXT disp;
kono
parents:
diff changeset
75 };
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 /* Get the value of register INDEX as saved in CONTEXT. */
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 _Unwind_Word
kono
parents:
diff changeset
80 _Unwind_GetGR (struct _Unwind_Context *c, int index)
kono
parents:
diff changeset
81 {
kono
parents:
diff changeset
82 if (index < 0 || index >= 2)
kono
parents:
diff changeset
83 abort ();
kono
parents:
diff changeset
84 return c->reg[index];
kono
parents:
diff changeset
85 }
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 void
kono
parents:
diff changeset
90 _Unwind_SetGR (struct _Unwind_Context *c, int index, _Unwind_Word val)
kono
parents:
diff changeset
91 {
kono
parents:
diff changeset
92 if (index < 0 || index >= 2)
kono
parents:
diff changeset
93 abort ();
kono
parents:
diff changeset
94 c->reg[index] = val;
kono
parents:
diff changeset
95 }
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 /* Get the value of the CFA as saved in CONTEXT. */
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 _Unwind_Word
kono
parents:
diff changeset
100 _Unwind_GetCFA (struct _Unwind_Context *c)
kono
parents:
diff changeset
101 {
kono
parents:
diff changeset
102 return c->cfa;
kono
parents:
diff changeset
103 }
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 /* Retrieve the return address for CONTEXT. */
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 _Unwind_Ptr
kono
parents:
diff changeset
108 _Unwind_GetIP (struct _Unwind_Context *c)
kono
parents:
diff changeset
109 {
kono
parents:
diff changeset
110 return c->ra;
kono
parents:
diff changeset
111 }
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 /* Retrieve the return address and flag whether that IP is before
kono
parents:
diff changeset
114 or after first not yet fully executed instruction. */
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 _Unwind_Ptr
kono
parents:
diff changeset
117 _Unwind_GetIPInfo (struct _Unwind_Context *c, int *ip_before_insn)
kono
parents:
diff changeset
118 {
kono
parents:
diff changeset
119 /* ??? Is there a concept of a signal context properly? There's
kono
parents:
diff changeset
120 obviously an UNWP_PUSH_MACHFRAME opcode, but the runtime might
kono
parents:
diff changeset
121 have arranged for that not to matter, really. */
kono
parents:
diff changeset
122 *ip_before_insn = 0;
kono
parents:
diff changeset
123 return c->ra;
kono
parents:
diff changeset
124 }
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 /* Overwrite the return address for CONTEXT with VAL. */
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 void
kono
parents:
diff changeset
129 _Unwind_SetIP (struct _Unwind_Context *c, _Unwind_Ptr val)
kono
parents:
diff changeset
130 {
kono
parents:
diff changeset
131 c->ra = val;
kono
parents:
diff changeset
132 }
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 void *
kono
parents:
diff changeset
135 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *c)
kono
parents:
diff changeset
136 {
kono
parents:
diff changeset
137 return c->disp->HandlerData;
kono
parents:
diff changeset
138 }
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 _Unwind_Ptr
kono
parents:
diff changeset
141 _Unwind_GetRegionStart (struct _Unwind_Context *c)
kono
parents:
diff changeset
142 {
kono
parents:
diff changeset
143 return c->disp->FunctionEntry->BeginAddress + c->disp->ImageBase;
kono
parents:
diff changeset
144 }
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 void *
kono
parents:
diff changeset
147 _Unwind_FindEnclosingFunction (void *pc)
kono
parents:
diff changeset
148 {
kono
parents:
diff changeset
149 PRUNTIME_FUNCTION entry;
kono
parents:
diff changeset
150 ULONG64 ImageBase;
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 entry = RtlLookupFunctionEntry ((ULONG64)pc, &ImageBase, NULL);
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 return (entry ? (void *)(entry->BeginAddress + ImageBase) : NULL);
kono
parents:
diff changeset
155 }
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 _Unwind_Ptr
kono
parents:
diff changeset
158 _Unwind_GetDataRelBase (struct _Unwind_Context *c ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
159 {
kono
parents:
diff changeset
160 return 0;
kono
parents:
diff changeset
161 }
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 _Unwind_Ptr
kono
parents:
diff changeset
164 _Unwind_GetTextRelBase (struct _Unwind_Context *c)
kono
parents:
diff changeset
165 {
kono
parents:
diff changeset
166 return c->disp->ImageBase;
kono
parents:
diff changeset
167 }
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 /* The two-phase unwind process that GCC uses is ordered differently
kono
parents:
diff changeset
171 from the two-phase unwind process that SEH uses. The mechansism
kono
parents:
diff changeset
172 that GCC uses is to have the filter return _URC_HANDER_FOUND; the
kono
parents:
diff changeset
173 mechanism that SEH uses is for the filter function call back into
kono
parents:
diff changeset
174 the unwinder.
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 An Ideal port to SEH would have GCC emit handler functions that
kono
parents:
diff changeset
177 can be called, given a pointer to the "EstablisherFrame" (i.e.
kono
parents:
diff changeset
178 the frame pointer base of the user-level function) can manipulate
kono
parents:
diff changeset
179 the user-level variables within the user-level function's stack
kono
parents:
diff changeset
180 frame. Once done manipulating the variables, it would return
kono
parents:
diff changeset
181 a ExceptionContinueSearch, and the unwind process would continue.
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 GCC has always done things a bit differently. We continue to
kono
parents:
diff changeset
184 transfer control back into the user-level function which, once
kono
parents:
diff changeset
185 done manipulating the user-level variables, re-throws the exception. */
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 /* The "real" language-specific personality handler forwards to here
kono
parents:
diff changeset
188 where we handle the MS SEH state and transforms it into the GCC
kono
parents:
diff changeset
189 unwind state as per GCC's <unwind.h>, at which point we defer to
kono
parents:
diff changeset
190 the regular language-specfic exception handler, which is passed in. */
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 EXCEPTION_DISPOSITION
kono
parents:
diff changeset
193 _GCC_specific_handler (PEXCEPTION_RECORD ms_exc, void *this_frame,
kono
parents:
diff changeset
194 PCONTEXT ms_orig_context, PDISPATCHER_CONTEXT ms_disp,
kono
parents:
diff changeset
195 _Unwind_Personality_Fn gcc_per)
kono
parents:
diff changeset
196 {
kono
parents:
diff changeset
197 DWORD ms_flags = ms_exc->ExceptionFlags;
kono
parents:
diff changeset
198 DWORD ms_code = ms_exc->ExceptionCode;
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 struct _Unwind_Exception *gcc_exc
kono
parents:
diff changeset
201 = (struct _Unwind_Exception *) ms_exc->ExceptionInformation[0];
kono
parents:
diff changeset
202 struct _Unwind_Context gcc_context;
kono
parents:
diff changeset
203 _Unwind_Action gcc_action;
kono
parents:
diff changeset
204 _Unwind_Reason_Code gcc_reason;
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 if (ms_flags & EXCEPTION_TARGET_UNWIND)
kono
parents:
diff changeset
207 {
kono
parents:
diff changeset
208 /* This frame is known to be the target frame. We've already
kono
parents:
diff changeset
209 "installed" the target_ip and RAX value via the arguments
kono
parents:
diff changeset
210 to RtlUnwindEx. All that's left is to set the RDX value
kono
parents:
diff changeset
211 and "continue" to have the context installed. */
kono
parents:
diff changeset
212 ms_disp->ContextRecord->Rdx = ms_exc->ExceptionInformation[3];
kono
parents:
diff changeset
213 return ExceptionContinueSearch;
kono
parents:
diff changeset
214 }
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 if (ms_code == STATUS_GCC_UNWIND)
kono
parents:
diff changeset
217 {
kono
parents:
diff changeset
218 /* This is a colliding exception that we threw so that we could
kono
parents:
diff changeset
219 cancel the already in-flight exception and stop in a frame
kono
parents:
diff changeset
220 that wanted to perform some unwind action. The only relevant
kono
parents:
diff changeset
221 test is that we're the target frame. */
kono
parents:
diff changeset
222 if (ms_exc->ExceptionInformation[1] == (_Unwind_Ptr) this_frame)
kono
parents:
diff changeset
223 {
kono
parents:
diff changeset
224 RtlUnwindEx (this_frame, (PVOID) ms_exc->ExceptionInformation[2],
kono
parents:
diff changeset
225 ms_exc, gcc_exc, ms_orig_context,
kono
parents:
diff changeset
226 ms_disp->HistoryTable);
kono
parents:
diff changeset
227 abort ();
kono
parents:
diff changeset
228 }
kono
parents:
diff changeset
229 return ExceptionContinueSearch;
kono
parents:
diff changeset
230 }
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 gcc_context.cfa = ms_disp->ContextRecord->Rsp;
kono
parents:
diff changeset
233 gcc_context.ra = ms_disp->ControlPc;
kono
parents:
diff changeset
234 gcc_context.reg[0] = 0xdeadbeef; /* These are write-only. */
kono
parents:
diff changeset
235 gcc_context.reg[1] = 0xdeadbeef;
kono
parents:
diff changeset
236 gcc_context.disp = ms_disp;
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 if (ms_code == STATUS_GCC_FORCED)
kono
parents:
diff changeset
239 {
kono
parents:
diff changeset
240 _Unwind_Stop_Fn stop = (_Unwind_Stop_Fn) gcc_exc->private_[0];
kono
parents:
diff changeset
241 void *stop_argument = (void *) gcc_exc->private_[4];
kono
parents:
diff changeset
242
kono
parents:
diff changeset
243 gcc_action = _UA_FORCE_UNWIND | _UA_CLEANUP_PHASE;
kono
parents:
diff changeset
244
kono
parents:
diff changeset
245 stop (1, gcc_action, gcc_exc->exception_class, gcc_exc,
kono
parents:
diff changeset
246 &gcc_context, stop_argument);
kono
parents:
diff changeset
247
kono
parents:
diff changeset
248 goto phase2;
kono
parents:
diff changeset
249 }
kono
parents:
diff changeset
250
kono
parents:
diff changeset
251 /* ??? TODO: handling non-gcc user-defined exceptions as foreign. */
kono
parents:
diff changeset
252 if (ms_code != STATUS_GCC_THROW)
kono
parents:
diff changeset
253 return ExceptionContinueSearch;
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 if (ms_flags & (EXCEPTION_UNWINDING | EXCEPTION_EXIT_UNWIND))
kono
parents:
diff changeset
256 {
kono
parents:
diff changeset
257 /* This is Phase 2. */
kono
parents:
diff changeset
258 /* We know this isn't the target frame because we've already tested
kono
parents:
diff changeset
259 EXCEPTION_TARGET_UNWIND. The remaining possibility is that the
kono
parents:
diff changeset
260 gcc personality has unwind code to run. */
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 gcc_action = _UA_CLEANUP_PHASE;
kono
parents:
diff changeset
263 phase2:
kono
parents:
diff changeset
264 gcc_reason = gcc_per (1, gcc_action, gcc_exc->exception_class,
kono
parents:
diff changeset
265 gcc_exc, &gcc_context);
kono
parents:
diff changeset
266
kono
parents:
diff changeset
267 if (gcc_reason == _URC_CONTINUE_UNWIND)
kono
parents:
diff changeset
268 return ExceptionContinueSearch;
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 if (gcc_reason == _URC_INSTALL_CONTEXT)
kono
parents:
diff changeset
271 {
kono
parents:
diff changeset
272 /* Scratch space for the bits for the unwind catch. */
kono
parents:
diff changeset
273 ms_exc->ExceptionInformation[1] = (_Unwind_Ptr) this_frame;
kono
parents:
diff changeset
274 ms_exc->ExceptionInformation[2] = gcc_context.ra;
kono
parents:
diff changeset
275 ms_exc->ExceptionInformation[3] = gcc_context.reg[1];
kono
parents:
diff changeset
276
kono
parents:
diff changeset
277 /* Cancel the current exception by raising another. */
kono
parents:
diff changeset
278 RaiseException (STATUS_GCC_UNWIND, EXCEPTION_NONCONTINUABLE,
kono
parents:
diff changeset
279 4, ms_exc->ExceptionInformation);
kono
parents:
diff changeset
280
kono
parents:
diff changeset
281 /* Is RaiseException declared noreturn? */
kono
parents:
diff changeset
282 }
kono
parents:
diff changeset
283
kono
parents:
diff changeset
284 /* In _Unwind_RaiseException_Phase2 we return _URC_FATAL_PHASE2_ERROR. */
kono
parents:
diff changeset
285 }
kono
parents:
diff changeset
286 else
kono
parents:
diff changeset
287 {
kono
parents:
diff changeset
288 /* This is Phase 1. */
kono
parents:
diff changeset
289 gcc_reason = gcc_per (1, _UA_SEARCH_PHASE, gcc_exc->exception_class,
kono
parents:
diff changeset
290 gcc_exc, &gcc_context);
kono
parents:
diff changeset
291
kono
parents:
diff changeset
292 if (gcc_reason == _URC_CONTINUE_UNWIND)
kono
parents:
diff changeset
293 return ExceptionContinueSearch;
kono
parents:
diff changeset
294
kono
parents:
diff changeset
295 if (gcc_reason == _URC_HANDLER_FOUND)
kono
parents:
diff changeset
296 {
kono
parents:
diff changeset
297 /* We really need some of the information that GCC's personality
kono
parents:
diff changeset
298 routines compute during phase 2 right now, like the target IP.
kono
parents:
diff changeset
299 Go ahead and ask for it now, and cache it. */
kono
parents:
diff changeset
300 gcc_reason = gcc_per (1, _UA_CLEANUP_PHASE | _UA_HANDLER_FRAME,
kono
parents:
diff changeset
301 gcc_exc->exception_class, gcc_exc,
kono
parents:
diff changeset
302 &gcc_context);
kono
parents:
diff changeset
303 if (gcc_reason != _URC_INSTALL_CONTEXT)
kono
parents:
diff changeset
304 abort ();
kono
parents:
diff changeset
305
kono
parents:
diff changeset
306 gcc_exc->private_[1] = (_Unwind_Ptr) this_frame;
kono
parents:
diff changeset
307 gcc_exc->private_[2] = gcc_context.ra;
kono
parents:
diff changeset
308 gcc_exc->private_[3] = gcc_context.reg[1];
kono
parents:
diff changeset
309
kono
parents:
diff changeset
310 ms_exc->NumberParameters = 4;
kono
parents:
diff changeset
311 ms_exc->ExceptionInformation[1] = (_Unwind_Ptr) this_frame;
kono
parents:
diff changeset
312 ms_exc->ExceptionInformation[2] = gcc_context.ra;
kono
parents:
diff changeset
313 ms_exc->ExceptionInformation[3] = gcc_context.reg[1];
kono
parents:
diff changeset
314
kono
parents:
diff changeset
315 /* Begin phase 2. Perform the unwinding. */
kono
parents:
diff changeset
316 RtlUnwindEx (this_frame, (PVOID)gcc_context.ra, ms_exc,
kono
parents:
diff changeset
317 (PVOID)gcc_context.reg[0], ms_orig_context,
kono
parents:
diff changeset
318 ms_disp->HistoryTable);
kono
parents:
diff changeset
319 }
kono
parents:
diff changeset
320
kono
parents:
diff changeset
321 /* In _Unwind_RaiseException we return _URC_FATAL_PHASE1_ERROR. */
kono
parents:
diff changeset
322 }
kono
parents:
diff changeset
323 abort ();
kono
parents:
diff changeset
324 }
kono
parents:
diff changeset
325
kono
parents:
diff changeset
326 /* Raise an exception, passing along the given exception object. */
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 _Unwind_Reason_Code
kono
parents:
diff changeset
329 _Unwind_RaiseException (struct _Unwind_Exception *exc)
kono
parents:
diff changeset
330 {
kono
parents:
diff changeset
331 memset (exc->private_, 0, sizeof (exc->private_));
kono
parents:
diff changeset
332
kono
parents:
diff changeset
333 /* The ExceptionInformation array will have only 1 element, EXC. */
kono
parents:
diff changeset
334 RaiseException (STATUS_GCC_THROW, 0, 1, (ULONG_PTR *)&exc);
kono
parents:
diff changeset
335
kono
parents:
diff changeset
336 /* The exception handler installed in crt0 will continue any GCC
kono
parents:
diff changeset
337 exception that reaches there (and isn't marked non-continuable).
kono
parents:
diff changeset
338 Returning allows the C++ runtime to call std::terminate. */
kono
parents:
diff changeset
339 return _URC_END_OF_STACK;
kono
parents:
diff changeset
340 }
kono
parents:
diff changeset
341
kono
parents:
diff changeset
342 /* Resume propagation of an existing exception. This is used after
kono
parents:
diff changeset
343 e.g. executing cleanup code, and not to implement rethrowing. */
kono
parents:
diff changeset
344
kono
parents:
diff changeset
345 void
kono
parents:
diff changeset
346 _Unwind_Resume (struct _Unwind_Exception *gcc_exc)
kono
parents:
diff changeset
347 {
kono
parents:
diff changeset
348 UNWIND_HISTORY_TABLE ms_history;
kono
parents:
diff changeset
349 EXCEPTION_RECORD ms_exc;
kono
parents:
diff changeset
350 CONTEXT ms_context;
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 memset (&ms_exc, 0, sizeof(ms_exc));
kono
parents:
diff changeset
353 memset (&ms_history, 0, sizeof(ms_history));
kono
parents:
diff changeset
354
kono
parents:
diff changeset
355 /* ??? Not 100% perfect, since we aren't passing on the *original*
kono
parents:
diff changeset
356 exception context, but should be good enough. */
kono
parents:
diff changeset
357 ms_exc.ExceptionCode = STATUS_GCC_THROW;
kono
parents:
diff changeset
358 ms_exc.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
kono
parents:
diff changeset
359 ms_exc.NumberParameters = 4;
kono
parents:
diff changeset
360 ms_exc.ExceptionInformation[0] = (ULONG_PTR) gcc_exc;
kono
parents:
diff changeset
361 ms_exc.ExceptionInformation[1] = gcc_exc->private_[1];
kono
parents:
diff changeset
362 ms_exc.ExceptionInformation[2] = gcc_exc->private_[2];
kono
parents:
diff changeset
363 ms_exc.ExceptionInformation[3] = gcc_exc->private_[3];
kono
parents:
diff changeset
364
kono
parents:
diff changeset
365 ms_context.ContextFlags = CONTEXT_ALL;
kono
parents:
diff changeset
366 RtlCaptureContext (&ms_context);
kono
parents:
diff changeset
367
kono
parents:
diff changeset
368 RtlUnwindEx ((void *) gcc_exc->private_[1], (PVOID)gcc_exc->private_[2],
kono
parents:
diff changeset
369 &ms_exc, gcc_exc, &ms_context, &ms_history);
kono
parents:
diff changeset
370
kono
parents:
diff changeset
371 /* Is RtlUnwindEx declared noreturn? */
kono
parents:
diff changeset
372 abort ();
kono
parents:
diff changeset
373 }
kono
parents:
diff changeset
374
kono
parents:
diff changeset
375 static _Unwind_Reason_Code
kono
parents:
diff changeset
376 _Unwind_ForcedUnwind_Phase2 (struct _Unwind_Exception *exc)
kono
parents:
diff changeset
377 {
kono
parents:
diff changeset
378 _Unwind_Stop_Fn stop;
kono
parents:
diff changeset
379 void * stop_argument;
kono
parents:
diff changeset
380
kono
parents:
diff changeset
381 RaiseException (STATUS_GCC_FORCED, 0, 1, (ULONG_PTR *)&exc);
kono
parents:
diff changeset
382
kono
parents:
diff changeset
383 /* If we get here, we got to top-of-stack. */
kono
parents:
diff changeset
384 /* ??? We no longer have a context pointer to pass in. */
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 stop = (_Unwind_Stop_Fn) exc->private_[0];
kono
parents:
diff changeset
387 stop_argument = (void *) exc->private_[4];
kono
parents:
diff changeset
388 stop (1, _UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK,
kono
parents:
diff changeset
389 exc->exception_class, exc, NULL, stop_argument);
kono
parents:
diff changeset
390
kono
parents:
diff changeset
391 return _UA_END_OF_STACK;
kono
parents:
diff changeset
392 }
kono
parents:
diff changeset
393
kono
parents:
diff changeset
394 _Unwind_Reason_Code
kono
parents:
diff changeset
395 _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *exc)
kono
parents:
diff changeset
396 {
kono
parents:
diff changeset
397 if (exc->private_[0] == 0)
kono
parents:
diff changeset
398 _Unwind_RaiseException (exc);
kono
parents:
diff changeset
399 else
kono
parents:
diff changeset
400 _Unwind_ForcedUnwind_Phase2 (exc);
kono
parents:
diff changeset
401 abort ();
kono
parents:
diff changeset
402 }
kono
parents:
diff changeset
403
kono
parents:
diff changeset
404 /* Raise an exception for forced unwinding. */
kono
parents:
diff changeset
405
kono
parents:
diff changeset
406 _Unwind_Reason_Code
kono
parents:
diff changeset
407 _Unwind_ForcedUnwind (struct _Unwind_Exception *exc,
kono
parents:
diff changeset
408 _Unwind_Stop_Fn stop, void * stop_argument)
kono
parents:
diff changeset
409 {
kono
parents:
diff changeset
410 /* ??? This is a hack that only works with _GCC_specific_handler.
kono
parents:
diff changeset
411 There's no way to invoke STOP within frames that use a different
kono
parents:
diff changeset
412 exception handler. This is essentially just good enough to run
kono
parents:
diff changeset
413 the code within the gcc testsuite. */
kono
parents:
diff changeset
414
kono
parents:
diff changeset
415 memset (exc->private_, 0, sizeof (exc->private_));
kono
parents:
diff changeset
416 exc->private_[0] = (_Unwind_Ptr) stop;
kono
parents:
diff changeset
417 exc->private_[4] = (_Unwind_Ptr) stop_argument;
kono
parents:
diff changeset
418
kono
parents:
diff changeset
419 return _Unwind_ForcedUnwind_Phase2 (exc);
kono
parents:
diff changeset
420 }
kono
parents:
diff changeset
421
kono
parents:
diff changeset
422 /* A convenience function that calls the exception_cleanup field. */
kono
parents:
diff changeset
423
kono
parents:
diff changeset
424 void
kono
parents:
diff changeset
425 _Unwind_DeleteException (struct _Unwind_Exception *exc)
kono
parents:
diff changeset
426 {
kono
parents:
diff changeset
427 if (exc->exception_cleanup)
kono
parents:
diff changeset
428 (*exc->exception_cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exc);
kono
parents:
diff changeset
429 }
kono
parents:
diff changeset
430
kono
parents:
diff changeset
431 /* Perform stack backtrace through unwind data. */
kono
parents:
diff changeset
432
kono
parents:
diff changeset
433 _Unwind_Reason_Code
kono
parents:
diff changeset
434 _Unwind_Backtrace(_Unwind_Trace_Fn trace,
kono
parents:
diff changeset
435 void *trace_argument)
kono
parents:
diff changeset
436 {
kono
parents:
diff changeset
437 UNWIND_HISTORY_TABLE ms_history;
kono
parents:
diff changeset
438 CONTEXT ms_context;
kono
parents:
diff changeset
439 struct _Unwind_Context gcc_context;
kono
parents:
diff changeset
440 DISPATCHER_CONTEXT disp_context;
kono
parents:
diff changeset
441
kono
parents:
diff changeset
442 memset (&ms_history, 0, sizeof(ms_history));
kono
parents:
diff changeset
443 memset (&gcc_context, 0, sizeof(gcc_context));
kono
parents:
diff changeset
444 memset (&disp_context, 0, sizeof(disp_context));
kono
parents:
diff changeset
445
kono
parents:
diff changeset
446 ms_context.ContextFlags = CONTEXT_ALL;
kono
parents:
diff changeset
447 RtlCaptureContext (&ms_context);
kono
parents:
diff changeset
448
kono
parents:
diff changeset
449 gcc_context.disp = &disp_context;
kono
parents:
diff changeset
450 gcc_context.disp->ContextRecord = &ms_context;
kono
parents:
diff changeset
451 gcc_context.disp->HistoryTable = &ms_history;
kono
parents:
diff changeset
452
kono
parents:
diff changeset
453 while (1)
kono
parents:
diff changeset
454 {
kono
parents:
diff changeset
455 gcc_context.disp->ControlPc = ms_context.Rip;
kono
parents:
diff changeset
456 gcc_context.disp->FunctionEntry
kono
parents:
diff changeset
457 = RtlLookupFunctionEntry (ms_context.Rip, &gcc_context.disp->ImageBase,
kono
parents:
diff changeset
458 &ms_history);
kono
parents:
diff changeset
459
kono
parents:
diff changeset
460 if (!gcc_context.disp->FunctionEntry)
kono
parents:
diff changeset
461 return _URC_END_OF_STACK;
kono
parents:
diff changeset
462
kono
parents:
diff changeset
463 gcc_context.disp->LanguageHandler
kono
parents:
diff changeset
464 = RtlVirtualUnwind (0, gcc_context.disp->ImageBase, ms_context.Rip,
kono
parents:
diff changeset
465 gcc_context.disp->FunctionEntry, &ms_context,
kono
parents:
diff changeset
466 &gcc_context.disp->HandlerData,
kono
parents:
diff changeset
467 &gcc_context.disp->EstablisherFrame, NULL);
kono
parents:
diff changeset
468
kono
parents:
diff changeset
469 /* Call trace function. */
kono
parents:
diff changeset
470 if (trace (&gcc_context, trace_argument) != _URC_NO_REASON)
kono
parents:
diff changeset
471 return _URC_FATAL_PHASE1_ERROR;
kono
parents:
diff changeset
472
kono
parents:
diff changeset
473 /* ??? Check for invalid stack pointer. */
kono
parents:
diff changeset
474 if (ms_context.Rip == 0)
kono
parents:
diff changeset
475 return _URC_END_OF_STACK;
kono
parents:
diff changeset
476 }
kono
parents:
diff changeset
477 }
kono
parents:
diff changeset
478 #endif /* __SEH__ && !defined (__USING_SJLJ_EXCEPTIONS__) */