1 // GNU D Compiler exception personality routines.
2 // Copyright (C) 2011-2021 Free Software Foundation, Inc.
4 // GCC is free software; you can redistribute it and/or modify it under
5 // the terms of the GNU General Public License as published by the Free
6 // Software Foundation; either version 3, or (at your option) any later
9 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
10 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 // Under Section 7 of GPL version 3, you are granted additional
15 // permissions described in the GCC Runtime Library Exception, version
16 // 3.1, as published by the Free Software Foundation.
18 // You should have received a copy of the GNU General Public License and
19 // a copy of the GCC Runtime Library Exception along with this program;
20 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
21 // <http://www.gnu.org/licenses/>.
23 // This code is based on the libstdc++ exception handling routines.
31 import gcc.attributes;
35 int _d_isbaseof(ClassInfo, ClassInfo);
36 void _d_createTrace(Object, void*);
40 * Declare all known and handled exception classes.
41 * D exceptions -- "GNUCD\0\0\0".
42 * C++ exceptions -- "GNUCC++\0"
43 * C++ dependent exceptions -- "GNUCC++\x01"
45 static if (GNU_ARM_EABI_Unwinder)
47 enum _Unwind_Exception_Class gdcExceptionClass = "GNUCD\0\0\0";
48 enum _Unwind_Exception_Class gxxExceptionClass = "GNUCC++\0";
49 enum _Unwind_Exception_Class gxxDependentExceptionClass = "GNUCC++\x01";
53 enum _Unwind_Exception_Class gdcExceptionClass =
54 (cast(_Unwind_Exception_Class)'G' << 56) |
55 (cast(_Unwind_Exception_Class)'N' << 48) |
56 (cast(_Unwind_Exception_Class)'U' << 40) |
57 (cast(_Unwind_Exception_Class)'C' << 32) |
58 (cast(_Unwind_Exception_Class)'D' << 24);
60 enum _Unwind_Exception_Class gxxExceptionClass =
61 (cast(_Unwind_Exception_Class)'G' << 56) |
62 (cast(_Unwind_Exception_Class)'N' << 48) |
63 (cast(_Unwind_Exception_Class)'U' << 40) |
64 (cast(_Unwind_Exception_Class)'C' << 32) |
65 (cast(_Unwind_Exception_Class)'C' << 24) |
66 (cast(_Unwind_Exception_Class)'+' << 16) |
67 (cast(_Unwind_Exception_Class)'+' << 8) |
68 (cast(_Unwind_Exception_Class)0 << 0);
70 enum _Unwind_Exception_Class gxxDependentExceptionClass =
71 gxxExceptionClass + 1;
75 * Checks for GDC exception class.
77 bool isGdcExceptionClass(_Unwind_Exception_Class c) @nogc
79 static if (GNU_ARM_EABI_Unwinder)
81 return c[0] == gdcExceptionClass[0]
82 && c[1] == gdcExceptionClass[1]
83 && c[2] == gdcExceptionClass[2]
84 && c[3] == gdcExceptionClass[3]
85 && c[4] == gdcExceptionClass[4]
86 && c[5] == gdcExceptionClass[5]
87 && c[6] == gdcExceptionClass[6]
88 && c[7] == gdcExceptionClass[7];
92 return c == gdcExceptionClass;
97 * Checks for any C++ exception class.
99 bool isGxxExceptionClass(_Unwind_Exception_Class c) @nogc
101 static if (GNU_ARM_EABI_Unwinder)
103 return c[0] == gxxExceptionClass[0]
104 && c[1] == gxxExceptionClass[1]
105 && c[2] == gxxExceptionClass[2]
106 && c[3] == gxxExceptionClass[3]
107 && c[4] == gxxExceptionClass[4]
108 && c[5] == gxxExceptionClass[5]
109 && c[6] == gxxExceptionClass[6]
110 && (c[7] == gxxExceptionClass[7]
111 || c[7] == gxxDependentExceptionClass[7]);
115 return c == gxxExceptionClass
116 || c == gxxDependentExceptionClass;
121 * Checks for primary or dependent, but not that it is a C++ exception.
123 bool isDependentException(_Unwind_Exception_Class c) @nogc
125 static if (GNU_ARM_EABI_Unwinder)
126 return (c[7] == '\x01');
132 * A D exception object consists of a header, which is a wrapper
133 * around an unwind object header with additional D specific
134 * information, prefixed by the exception object itself.
136 struct ExceptionHeader
138 // Because of a lack of __aligned__ style attribute, our object
139 // and the unwind object are the first two fields.
140 static if (Throwable.alignof < _Unwind_Exception.alignof)
141 ubyte[_Unwind_Exception.alignof - Throwable.alignof] pad;
143 // The object being thrown. The compiled code expects this to
144 // be immediately before the generic exception header.
147 // The generic exception header.
148 _Unwind_Exception unwindHeader;
150 static assert(unwindHeader.offsetof - object.offsetof == object.sizeof);
152 // Cache handler details between Phase 1 and Phase 2.
153 static if (GNU_ARM_EABI_Unwinder)
159 // Which catch was found.
162 // Language Specific Data Area for function enclosing the handler.
163 const(ubyte)* languageSpecificData;
165 // Pointer to catch code.
166 _Unwind_Ptr landingPad;
168 // Canonical Frame Address (CFA) for the enclosing handler.
169 _Unwind_Word canonicalFrameAddress;
172 // Stack other thrown exceptions in current thread through here.
173 ExceptionHeader* next;
175 // Thread local stack of chained exceptions.
176 static ExceptionHeader* stack;
178 // Pre-allocate storage for 1 instance per thread.
179 // Use calloc/free for multiple exceptions in flight.
180 static ExceptionHeader ehstorage;
183 * Allocate and initialize an ExceptionHeader.
185 static ExceptionHeader* create(Throwable o) @nogc
187 auto eh = &ehstorage;
189 // Check exception object in use.
192 eh = cast(ExceptionHeader*) __builtin_calloc(ExceptionHeader.sizeof, 1);
193 // Out of memory while throwing - not much else can be done.
195 terminate("out of memory", __LINE__);
199 eh.unwindHeader.exception_class = gdcExceptionClass;
205 * Free ExceptionHeader that was created by create().
207 static void free(ExceptionHeader* eh) @nogc
209 *eh = ExceptionHeader.init;
210 if (eh != &ehstorage)
215 * Push this onto stack of chained exceptions.
224 * Pop and return top of chained exception stack.
226 static ExceptionHeader* pop() @nogc
234 * Save stage1 handler information in the exception object.
236 static void save(_Unwind_Exception* unwindHeader,
237 _Unwind_Word cfa, int handler,
238 const(ubyte)* lsda, _Unwind_Ptr landingPad) @nogc
240 static if (GNU_ARM_EABI_Unwinder)
242 unwindHeader.barrier_cache.sp = cfa;
243 unwindHeader.barrier_cache.bitpattern[1] = cast(_uw)handler;
244 unwindHeader.barrier_cache.bitpattern[2] = cast(_uw)lsda;
245 unwindHeader.barrier_cache.bitpattern[3] = cast(_uw)landingPad;
249 ExceptionHeader* eh = toExceptionHeader(unwindHeader);
250 eh.canonicalFrameAddress = cfa;
251 eh.handler = handler;
252 eh.languageSpecificData = lsda;
253 eh.landingPad = landingPad;
258 * Restore the catch handler data saved during phase1.
260 static void restore(_Unwind_Exception* unwindHeader, out int handler,
261 out const(ubyte)* lsda, out _Unwind_Ptr landingPad,
262 out _Unwind_Word cfa) @nogc
264 static if (GNU_ARM_EABI_Unwinder)
266 cfa = unwindHeader.barrier_cache.sp;
267 handler = cast(int)unwindHeader.barrier_cache.bitpattern[1];
268 lsda = cast(ubyte*)unwindHeader.barrier_cache.bitpattern[2];
269 landingPad = cast(_Unwind_Ptr)unwindHeader.barrier_cache.bitpattern[3];
273 ExceptionHeader* eh = toExceptionHeader(unwindHeader);
274 cfa = eh.canonicalFrameAddress;
275 handler = eh.handler;
276 lsda = eh.languageSpecificData;
277 landingPad = cast(_Unwind_Ptr)eh.landingPad;
282 * Look at the chain of inflight exceptions and pick the class type that'll
283 * be looked for in catch clauses.
285 static ClassInfo getClassInfo(_Unwind_Exception* unwindHeader) @nogc
287 ExceptionHeader* eh = toExceptionHeader(unwindHeader);
288 // The first thrown Exception at the top of the stack takes precedence
289 // over others that are inflight, unless an Error was thrown, in which
290 // case, we search for error handlers instead.
291 Throwable ehobject = eh.object;
292 for (ExceptionHeader* ehn = eh.next; ehn; ehn = ehn.next)
294 Error e = cast(Error)ehobject;
295 if (e is null || (cast(Error)ehn.object) !is null)
296 ehobject = ehn.object;
298 return ehobject.classinfo;
302 * Convert from pointer to unwindHeader to pointer to ExceptionHeader
303 * that it is embedded inside of.
305 static ExceptionHeader* toExceptionHeader(_Unwind_Exception* exc) @nogc
307 return cast(ExceptionHeader*)(cast(void*)exc - ExceptionHeader.unwindHeader.offsetof);
312 * Map to C++ std::type_info's virtual functions from D,
313 * being careful to not require linking with libstdc++.
314 * So it is given a different name.
316 extern(C++) interface CxxTypeInfo
320 bool __is_pointer_p() const;
321 bool __is_function_p() const;
322 bool __do_catch(const CxxTypeInfo, void**, uint) const;
323 bool __do_upcast(const void*, void**) const;
327 * Structure of a C++ exception, represented as a C structure.
328 * See unwind-cxx.h for the full definition.
330 struct CxaExceptionHeader
334 CxxTypeInfo exceptionType;
335 void* primaryException;
337 void function(void*) exceptionDestructor;
338 void function() unexpectedHandler;
339 void function() terminateHandler;
340 CxaExceptionHeader* nextException;
343 static if (GNU_ARM_EABI_Unwinder)
345 CxaExceptionHeader* nextPropagatingException;
346 int propagationCount;
350 int handlerSwitchValue;
351 const(ubyte)* actionRecord;
352 const(ubyte)* languageSpecificData;
353 _Unwind_Ptr catchTemp;
357 _Unwind_Exception unwindHeader;
360 * There's no saving between phases, so only cache pointer.
361 * __cxa_begin_catch expects this to be set.
363 static void save(_Unwind_Exception* unwindHeader, void* thrownPtr) @nogc
365 static if (GNU_ARM_EABI_Unwinder)
366 unwindHeader.barrier_cache.bitpattern[0] = cast(_uw) thrownPtr;
369 auto eh = toExceptionHeader(unwindHeader);
370 eh.adjustedPtr = thrownPtr;
375 * Get pointer to the thrown object if the thrown object type behind the
376 * exception is implicitly convertible to the catch type.
378 static void* getAdjustedPtr(_Unwind_Exception* exc, CxxTypeInfo catchType)
382 // A dependent C++ exceptions is just a wrapper around the unwind header.
383 // A primary C++ exception has the thrown object located immediately after it.
384 if (isDependentException(exc.exception_class))
385 thrownPtr = toExceptionHeader(exc).primaryException;
387 thrownPtr = cast(void*)(exc + 1);
389 // Pointer types need to adjust the actual pointer, not the pointer that is
390 // the exception object. This also has the effect of passing pointer types
391 // "by value" through the __cxa_begin_catch return value.
392 const throw_type = (cast(CxaExceptionHeader*)thrownPtr - 1).exceptionType;
394 if (throw_type.__is_pointer_p())
395 thrownPtr = *cast(void**)thrownPtr;
397 // Pointer adjustment may be necessary due to multiple inheritance
398 if (catchType is throw_type
399 || catchType.__do_catch(throw_type, &thrownPtr, 1))
406 * Convert from pointer to unwindHeader to pointer to CxaExceptionHeader
407 * that it is embedded inside of.
409 static CxaExceptionHeader* toExceptionHeader(_Unwind_Exception* exc) @nogc
411 return cast(CxaExceptionHeader*)(exc + 1) - 1;
416 * Called if exception handling must be abandoned for any reason.
418 private void terminate(string msg, uint line) @nogc
420 import core.stdc.stdio;
421 import core.stdc.stdlib;
423 static bool terminating;
426 fputs("terminate called recursively\n", stderr);
431 fprintf(stderr, "gcc.deh(%u): %.*s\n", line, cast(int)msg.length, msg.ptr);
437 * Called when fibers switch contexts.
439 extern(C) void* _d_eh_swapContext(void* newContext) nothrow @nogc
441 auto old = ExceptionHeader.stack;
442 ExceptionHeader.stack = cast(ExceptionHeader*)newContext;
447 * Called before starting a catch. Returns the exception object.
449 extern(C) void* __gdc_begin_catch(_Unwind_Exception* unwindHeader)
451 ExceptionHeader* header = ExceptionHeader.toExceptionHeader(unwindHeader);
453 void* objectp = cast(void*)header.object;
455 // Something went wrong when stacking up chained headers...
456 if (header != ExceptionHeader.pop())
457 terminate("catch error", __LINE__);
459 // Handling for this exception is complete.
460 _Unwind_DeleteException(&header.unwindHeader);
466 * Perform a throw, D style. Throw will unwind through this call,
467 * so there better not be any handlers or exception thrown here.
469 extern(C) void _d_throw(Throwable object)
471 // If possible, avoid always allocating new memory for exception headers.
472 ExceptionHeader *eh = ExceptionHeader.create(object);
474 // Add to thrown exception stack.
477 // Called by unwinder when exception object needs destruction by other than our code.
478 extern(C) void exception_cleanup(_Unwind_Reason_Code code, _Unwind_Exception* exc)
480 // If we haven't been caught by a foreign handler, then this is
481 // some sort of unwind error. In that case just die immediately.
482 // _Unwind_DeleteException in the HP-UX IA64 libunwind library
483 // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
484 // like the GCC _Unwind_DeleteException function does.
485 if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON)
486 terminate("uncaught exception", __LINE__);
488 auto eh = ExceptionHeader.toExceptionHeader(exc);
489 ExceptionHeader.free(eh);
492 eh.unwindHeader.exception_cleanup = &exception_cleanup;
494 // Runtime now expects us to do this first before unwinding.
495 _d_createTrace(eh.object, null);
497 // We're happy with setjmp/longjmp exceptions or region-based
498 // exception handlers: entry points are provided here for both.
499 _Unwind_Reason_Code r = void;
501 version (GNU_SjLj_Exceptions)
502 r = _Unwind_SjLj_RaiseException(&eh.unwindHeader);
504 r = _Unwind_RaiseException(&eh.unwindHeader);
506 // If code == _URC_END_OF_STACK, then we reached top of stack without finding
507 // a handler for the exception. Since each thread is run in a try/catch,
508 // this oughtn't happen. If code is something else, we encountered some sort
509 // of heinous lossage from which we could not recover. As is the way of such
510 // things, almost certainly we will have crashed before now, rather than
511 // actually being able to diagnose the problem.
512 if (r == _URC_END_OF_STACK)
513 terminate("uncaught exception", __LINE__);
515 terminate("unwind error", __LINE__);
518 static if (GNU_ARM_EABI_Unwinder)
520 enum personality_fn_attributes = attribute("target", ("general-regs-only"));
524 enum personality_fn_attributes = "";
528 * Read and extract information from the LSDA (.gcc_except_table section).
530 @personality_fn_attributes
531 _Unwind_Reason_Code scanLSDA(const(ubyte)* lsda, _Unwind_Exception_Class exceptionClass,
532 _Unwind_Action actions, _Unwind_Exception* unwindHeader,
533 _Unwind_Context* context, _Unwind_Word cfa,
534 out _Unwind_Ptr landingPad, out int handler)
536 // If no LSDA, then there are no handlers or cleanups.
538 return CONTINUE_UNWINDING(unwindHeader, context);
540 // Parse the LSDA header
543 auto Start = (context ? _Unwind_GetRegionStart(context) : 0);
545 // Find @LPStart, the base to which landing pad offsets are relative.
546 ubyte LPStartEncoding = *p++;
547 _Unwind_Ptr LPStart = 0;
549 if (LPStartEncoding != DW_EH_PE_omit)
550 LPStart = read_encoded_value(context, LPStartEncoding, &p);
554 // Find @TType, the base of the handler and exception spec type data.
555 ubyte TTypeEncoding = *p++;
556 const(ubyte)* TType = null;
558 if (TTypeEncoding != DW_EH_PE_omit)
560 static if (__traits(compiles, _TTYPE_ENCODING))
562 // Older ARM EABI toolchains set this value incorrectly, so use a
563 // hardcoded OS-specific format.
564 TTypeEncoding = _TTYPE_ENCODING;
566 auto TTbase = read_uleb128(&p);
570 // The encoding and length of the call-site table; the action table
571 // immediately follows.
572 ubyte CSEncoding = *p++;
573 auto CSTableSize = read_uleb128(&p);
574 const(ubyte)* actionTable = p + CSTableSize;
576 auto TTypeBase = base_of_encoded_value(TTypeEncoding, context);
578 // Get instruction pointer (ip) at start of instruction that threw.
579 version (CRuntime_Glibc)
582 auto ip = _Unwind_GetIPInfo(context, &ip_before_insn);
588 auto ip = _Unwind_GetIP(context);
592 bool saw_cleanup = false;
593 bool saw_handler = false;
594 const(ubyte)* actionRecord = null;
596 version (GNU_SjLj_Exceptions)
598 // The given "IP" is an index into the call-site table, with two
599 // exceptions -- -1 means no-action, and 0 means terminate.
600 // But since we're using uleb128 values, we've not got random
601 // access to the array.
602 if (cast(int) ip <= 0)
604 return _URC_CONTINUE_UNWIND;
608 _uleb128_t CSLandingPad, CSAction;
611 CSLandingPad = read_uleb128(&p);
612 CSAction = read_uleb128(&p);
616 // Can never have null landing pad for sjlj -- that would have
617 // been indicated by a -1 call site index.
618 landingPad = CSLandingPad + 1;
620 actionRecord = actionTable + CSAction - 1;
625 // Search the call-site table for the action associated with this IP.
626 while (p < actionTable)
628 // Note that all call-site encodings are "absolute" displacements.
629 auto CSStart = read_encoded_value(null, CSEncoding, &p);
630 auto CSLen = read_encoded_value(null, CSEncoding, &p);
631 auto CSLandingPad = read_encoded_value(null, CSEncoding, &p);
632 auto CSAction = read_uleb128(&p);
634 // The table is sorted, so if we've passed the ip, stop.
635 if (ip < Start + CSStart)
637 else if (ip < Start + CSStart + CSLen)
640 landingPad = LPStart + CSLandingPad;
642 actionRecord = actionTable + CSAction - 1;
650 // IP is present, but has a null landing pad.
651 // No cleanups or handlers to be run.
653 else if (actionRecord is null)
655 // If ip is present, has a non-null landing pad, and a null
656 // action table offset, then there are only cleanups present.
657 // Cleanups use a zero switch value, as set above.
662 // Otherwise we have a catch handler or exception specification.
663 handler = actionTableLookup(actions, unwindHeader, actionRecord,
664 exceptionClass, TTypeBase,
665 TType, TTypeEncoding,
666 saw_handler, saw_cleanup);
669 // IP is not in table. No associated cleanups.
670 if (!saw_handler && !saw_cleanup)
671 return CONTINUE_UNWINDING(unwindHeader, context);
673 if (actions & _UA_SEARCH_PHASE)
676 return CONTINUE_UNWINDING(unwindHeader, context);
678 // For domestic exceptions, we cache data from phase 1 for phase 2.
679 if (isGdcExceptionClass(exceptionClass))
680 ExceptionHeader.save(unwindHeader, cfa, handler, lsda, landingPad);
682 return _URC_HANDLER_FOUND;
689 * Look up and return the handler index of the classType in Action Table.
691 int actionTableLookup(_Unwind_Action actions, _Unwind_Exception* unwindHeader,
692 const(ubyte)* actionRecord, _Unwind_Exception_Class exceptionClass,
693 _Unwind_Ptr TTypeBase, const(ubyte)* TType,
695 out bool saw_handler, out bool saw_cleanup)
697 ClassInfo thrownType;
698 if (isGdcExceptionClass(exceptionClass))
700 thrownType = ExceptionHeader.getClassInfo(unwindHeader);
705 auto ap = actionRecord;
706 auto ARFilter = read_sleb128(&ap);
708 auto ARDisp = read_sleb128(&ap);
712 // Zero filter values are cleanups.
715 else if (actions & _UA_FORCE_UNWIND)
717 // During forced unwinding, we only run cleanups.
719 else if (ARFilter > 0)
721 // Positive filter values are handlers.
722 auto encodedSize = size_of_encoded_value(TTypeEncoding);
724 // ARFilter is the negative index from TType, which is where
725 // the ClassInfo is stored.
726 const(ubyte)* tp = TType - ARFilter * encodedSize;
728 auto entry = read_encoded_value_with_base(TTypeEncoding, TTypeBase, &tp);
729 ClassInfo ci = cast(ClassInfo)cast(void*)(entry);
731 // D does not have catch-all handlers, and so the following
732 // assumes that we will never handle a null value.
735 if (ci.classinfo is __cpp_type_info_ptr.classinfo
736 && isGxxExceptionClass(exceptionClass))
738 // catchType is the catch clause type_info.
739 auto catchType = cast(CxxTypeInfo)((cast(__cpp_type_info_ptr)cast(void*)ci).ptr);
740 auto thrownPtr = CxaExceptionHeader.getAdjustedPtr(unwindHeader, catchType);
742 if (thrownPtr !is null)
744 if (actions & _UA_SEARCH_PHASE)
745 CxaExceptionHeader.save(unwindHeader, thrownPtr);
747 return cast(int)ARFilter;
750 else if (isGdcExceptionClass(exceptionClass)
751 && _d_isbaseof(thrownType, ci))
754 return cast(int)ARFilter;
758 // ??? What to do about other GNU language exceptions.
763 // Negative filter values are exception specifications,
764 // which D does not use.
770 actionRecord = apn + ARDisp;
777 * Called when the personality function has found neither a cleanup or handler.
778 * To support ARM EABI personality routines, that must also unwind the stack.
780 @personality_fn_attributes
781 _Unwind_Reason_Code CONTINUE_UNWINDING(_Unwind_Exception* unwindHeader, _Unwind_Context* context)
783 static if (GNU_ARM_EABI_Unwinder)
785 if (__gnu_unwind_frame(unwindHeader, context) != _URC_OK)
788 return _URC_CONTINUE_UNWIND;
792 * Using a different personality function name causes link failures
793 * when trying to mix code using different exception handling models.
795 version (GNU_SEH_Exceptions)
797 enum PERSONALITY_FUNCTION = "__gdc_personality_imp";
799 extern(C) EXCEPTION_DISPOSITION __gdc_personality_seh0(void* ms_exc, void* this_frame,
800 void* ms_orig_context, void* ms_disp)
802 return _GCC_specific_handler(ms_exc, this_frame, ms_orig_context,
803 ms_disp, &gdc_personality);
806 else version (GNU_SjLj_Exceptions)
808 enum PERSONALITY_FUNCTION = "__gdc_personality_sj0";
810 private int __builtin_eh_return_data_regno(int x) { return x; }
814 enum PERSONALITY_FUNCTION = "__gdc_personality_v0";
818 * The "personality" function, specific to each language.
820 static if (GNU_ARM_EABI_Unwinder)
822 pragma(mangle, PERSONALITY_FUNCTION)
823 @personality_fn_attributes
824 extern(C) _Unwind_Reason_Code gdc_personality(_Unwind_State state,
825 _Unwind_Exception* unwindHeader,
826 _Unwind_Context* context)
828 _Unwind_Action actions;
830 switch (state & _US_ACTION_MASK)
832 case _US_VIRTUAL_UNWIND_FRAME:
833 // If the unwind state pattern is (_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND)
834 // then we don't need to search for any handler as it is not a real exception.
835 // Just unwind the stack.
836 if (state & _US_FORCE_UNWIND)
837 return CONTINUE_UNWINDING(unwindHeader, context);
838 actions = _UA_SEARCH_PHASE;
841 case _US_UNWIND_FRAME_STARTING:
842 actions = _UA_CLEANUP_PHASE;
843 if (!(state & _US_FORCE_UNWIND)
844 && unwindHeader.barrier_cache.sp == _Unwind_GetGR(context, UNWIND_STACK_REG))
845 actions |= _UA_HANDLER_FRAME;
848 case _US_UNWIND_FRAME_RESUME:
849 return CONTINUE_UNWINDING(unwindHeader, context);
852 terminate("unwind error", __LINE__);
854 actions |= state & _US_FORCE_UNWIND;
856 // The dwarf unwinder assumes the context structure holds things like
857 // the function and LSDA pointers. The ARM implementation caches these
858 // in the exception header (UCB). To avoid rewriting everything we make
859 // the virtual IP register point at the UCB.
860 _Unwind_SetGR(context, UNWIND_POINTER_REG, cast(_Unwind_Ptr)unwindHeader);
862 return __gdc_personality(actions, unwindHeader.exception_class,
863 unwindHeader, context);
868 pragma(mangle, PERSONALITY_FUNCTION)
869 extern(C) _Unwind_Reason_Code gdc_personality(int iversion,
870 _Unwind_Action actions,
871 _Unwind_Exception_Class exceptionClass,
872 _Unwind_Exception* unwindHeader,
873 _Unwind_Context* context)
875 // Interface version check.
877 return _URC_FATAL_PHASE1_ERROR;
879 return __gdc_personality(actions, exceptionClass, unwindHeader, context);
883 @personality_fn_attributes
884 private _Unwind_Reason_Code __gdc_personality(_Unwind_Action actions,
885 _Unwind_Exception_Class exceptionClass,
886 _Unwind_Exception* unwindHeader,
887 _Unwind_Context* context)
890 _Unwind_Ptr landingPad;
894 // Shortcut for phase 2 found handler for domestic exception.
895 if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
896 && isGdcExceptionClass(exceptionClass))
898 ExceptionHeader.restore(unwindHeader, handler, lsda, landingPad, cfa);
899 // Shouldn't have cached a null landing pad in phase 1.
901 terminate("unwind error", __LINE__);
905 lsda = cast(ubyte*)_Unwind_GetLanguageSpecificData(context);
907 static if (GNU_ARM_EABI_Unwinder)
908 cfa = _Unwind_GetGR(context, UNWIND_STACK_REG);
910 cfa = _Unwind_GetCFA(context);
912 auto result = scanLSDA(lsda, exceptionClass, actions, unwindHeader,
913 context, cfa, landingPad, handler);
915 // Positive on handler found in phase 1, continue unwinding, or failure.
920 // Unexpected negative handler, call terminate directly.
922 terminate("unwind error", __LINE__);
924 // We can't use any of the deh routines with foreign exceptions,
925 // because they all expect unwindHeader to be an ExceptionHeader.
926 if (isGdcExceptionClass(exceptionClass))
928 // If there are any in-flight exceptions being thrown, chain our
929 // current object onto the end of the prevous object.
930 ExceptionHeader* eh = ExceptionHeader.toExceptionHeader(unwindHeader);
931 auto currentLsd = lsda;
932 auto currentCfa = cfa;
933 bool bypassed = false;
937 ExceptionHeader* ehn = eh.next;
938 const(ubyte)* nextLsd;
939 _Unwind_Ptr nextLandingPad;
940 _Unwind_Word nextCfa;
943 ExceptionHeader.restore(&ehn.unwindHeader, nextHandler, nextLsd, nextLandingPad, nextCfa);
945 Error e = cast(Error)eh.object;
946 if (e !is null && !cast(Error)ehn.object)
948 // We found an Error, bypass the exception chain.
949 currentLsd = nextLsd;
950 currentCfa = nextCfa;
956 // Don't combine when the exceptions are from different functions.
957 if (currentLsd != nextLsd && currentCfa != nextCfa)
960 // Add our object onto the end of the existing chain.
961 Throwable n = ehn.object;
966 // Replace our exception object with in-flight one
967 eh.object = ehn.object;
968 if (nextHandler != handler && !bypassed)
970 handler = nextHandler;
971 ExceptionHeader.save(unwindHeader, cfa, handler, lsda, landingPad);
974 // Exceptions chained, can now throw away the previous header.
976 _Unwind_DeleteException(&ehn.unwindHeader);
981 eh = ExceptionHeader.toExceptionHeader(unwindHeader);
982 Error e = cast(Error)eh.object;
984 e.bypassedException = ehn.object;
986 _Unwind_DeleteException(&ehn.unwindHeader);
990 // Set up registers and jump to cleanup or handler.
991 // For targets with pointers smaller than the word size, we must extend the
992 // pointer, and this extension is target dependent.
993 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
994 cast(_Unwind_Ptr)unwindHeader);
995 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), handler);
996 _Unwind_SetIP(context, landingPad);
998 return _URC_INSTALL_CONTEXT;