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