1 // GNU D Compiler exception personality routines.
2 // Copyright (C) 2011-2025 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
) @nogc nothrow pure @safe;
36 void _d_createTrace(Throwable
, void*);
37 void _d_print_throwable(Throwable t
);
41 * Declare all known and handled exception classes.
42 * D exceptions -- "GNUCD\0\0\0".
43 * C++ exceptions -- "GNUCC++\0"
44 * C++ dependent exceptions -- "GNUCC++\x01"
46 static if (GNU_ARM_EABI_Unwinder
)
48 enum _Unwind_Exception_Class gdcExceptionClass
= "GNUCD\0\0\0";
49 enum _Unwind_Exception_Class gxxExceptionClass
= "GNUCC++\0";
50 enum _Unwind_Exception_Class gxxDependentExceptionClass
= "GNUCC++\x01";
54 enum _Unwind_Exception_Class gdcExceptionClass
=
55 (cast(_Unwind_Exception_Class
)'G' << 56) |
56 (cast(_Unwind_Exception_Class
)'N' << 48) |
57 (cast(_Unwind_Exception_Class
)'U' << 40) |
58 (cast(_Unwind_Exception_Class
)'C' << 32) |
59 (cast(_Unwind_Exception_Class
)'D' << 24);
61 enum _Unwind_Exception_Class gxxExceptionClass
=
62 (cast(_Unwind_Exception_Class
)'G' << 56) |
63 (cast(_Unwind_Exception_Class
)'N' << 48) |
64 (cast(_Unwind_Exception_Class
)'U' << 40) |
65 (cast(_Unwind_Exception_Class
)'C' << 32) |
66 (cast(_Unwind_Exception_Class
)'C' << 24) |
67 (cast(_Unwind_Exception_Class
)'+' << 16) |
68 (cast(_Unwind_Exception_Class
)'+' << 8) |
69 (cast(_Unwind_Exception_Class
)0 << 0);
71 enum _Unwind_Exception_Class gxxDependentExceptionClass
=
72 gxxExceptionClass
+ 1;
76 * Checks for GDC exception class.
78 bool isGdcExceptionClass(_Unwind_Exception_Class c
) @nogc
80 static if (GNU_ARM_EABI_Unwinder
)
82 return c
[0] == gdcExceptionClass
[0]
83 && c
[1] == gdcExceptionClass
[1]
84 && c
[2] == gdcExceptionClass
[2]
85 && c
[3] == gdcExceptionClass
[3]
86 && c
[4] == gdcExceptionClass
[4]
87 && c
[5] == gdcExceptionClass
[5]
88 && c
[6] == gdcExceptionClass
[6]
89 && c
[7] == gdcExceptionClass
[7];
93 return c
== gdcExceptionClass
;
98 * Checks for any C++ exception class.
100 bool isGxxExceptionClass(_Unwind_Exception_Class c
) @nogc
102 static if (GNU_ARM_EABI_Unwinder
)
104 return c
[0] == gxxExceptionClass
[0]
105 && c
[1] == gxxExceptionClass
[1]
106 && c
[2] == gxxExceptionClass
[2]
107 && c
[3] == gxxExceptionClass
[3]
108 && c
[4] == gxxExceptionClass
[4]
109 && c
[5] == gxxExceptionClass
[5]
110 && c
[6] == gxxExceptionClass
[6]
111 && (c
[7] == gxxExceptionClass
[7]
112 || c
[7] == gxxDependentExceptionClass
[7]);
116 return c
== gxxExceptionClass
117 || c
== gxxDependentExceptionClass
;
122 * Checks for primary or dependent, but not that it is a C++ exception.
124 bool isDependentException(_Unwind_Exception_Class c
) @nogc
126 static if (GNU_ARM_EABI_Unwinder
)
127 return (c
[7] == '\x01');
133 * A D exception object consists of a header, which is a wrapper
134 * around an unwind object header with additional D specific
135 * information, prefixed by the exception object itself.
137 struct ExceptionHeader
139 // Because of a lack of __aligned__ style attribute, our object
140 // and the unwind object are the first two fields.
141 static if (Throwable
.alignof
< _Unwind_Exception
.alignof
)
142 ubyte[_Unwind_Exception
.alignof
- Throwable
.alignof
] pad
;
144 // The object being thrown. The compiled code expects this to
145 // be immediately before the generic exception header.
148 // The generic exception header.
149 _Unwind_Exception unwindHeader
;
151 static assert(unwindHeader
.offsetof
- object
.offsetof
== object
.sizeof
);
153 // Cache handler details between Phase 1 and Phase 2.
154 static if (GNU_ARM_EABI_Unwinder
)
160 // Which catch was found.
163 // Language Specific Data Area for function enclosing the handler.
164 const(ubyte)* languageSpecificData
;
166 // Pointer to catch code.
167 _Unwind_Ptr landingPad
;
169 // Canonical Frame Address (CFA) for the enclosing handler.
170 _Unwind_Word canonicalFrameAddress
;
173 // Stack other thrown exceptions in current thread through here.
174 ExceptionHeader
* next
;
176 // Thread local stack of chained exceptions.
177 static ExceptionHeader
* stack
;
179 // Pre-allocate storage for 1 instance per thread.
180 // Use calloc/free for multiple exceptions in flight.
181 static ExceptionHeader ehstorage
;
184 * Allocate and initialize an ExceptionHeader.
186 static ExceptionHeader
* create(Throwable o
) @nogc
188 auto eh
= &ehstorage
;
190 // Check exception object in use.
193 eh
= cast(ExceptionHeader
*) __builtin_calloc(ExceptionHeader
.sizeof
, 1);
194 // Out of memory while throwing - not much else can be done.
196 terminate("out of memory", __LINE__
);
200 eh
.unwindHeader
.exception_class
= gdcExceptionClass
;
206 * Free ExceptionHeader that was created by create().
208 static void free(ExceptionHeader
* eh
) @nogc
210 __builtin_memset(eh
, 0, ExceptionHeader
.sizeof
);
211 if (eh
!= &ehstorage
)
216 * Push this onto stack of chained exceptions.
225 * Pop and return top of chained exception stack.
227 static ExceptionHeader
* pop() @nogc
235 * Save stage1 handler information in the exception object.
237 static void save(_Unwind_Exception
* unwindHeader
,
238 _Unwind_Word cfa
, int handler
,
239 const(ubyte)* lsda
, _Unwind_Ptr landingPad
) @nogc
241 static if (GNU_ARM_EABI_Unwinder
)
243 unwindHeader
.barrier_cache
.sp
= cfa
;
244 unwindHeader
.barrier_cache
.bitpattern
[1] = cast(_uw
)handler
;
245 unwindHeader
.barrier_cache
.bitpattern
[2] = cast(_uw
)lsda
;
246 unwindHeader
.barrier_cache
.bitpattern
[3] = cast(_uw
)landingPad
;
250 ExceptionHeader
* eh
= toExceptionHeader(unwindHeader
);
251 eh
.canonicalFrameAddress
= cfa
;
252 eh
.handler
= handler
;
253 eh
.languageSpecificData
= lsda
;
254 eh
.landingPad
= landingPad
;
259 * Restore the catch handler data saved during phase1.
261 static void restore(_Unwind_Exception
* unwindHeader
, out int handler
,
262 out const(ubyte)* lsda
, out _Unwind_Ptr landingPad
,
263 out _Unwind_Word cfa
) @nogc
265 static if (GNU_ARM_EABI_Unwinder
)
267 cfa
= unwindHeader
.barrier_cache
.sp
;
268 handler
= cast(int)unwindHeader
.barrier_cache
.bitpattern
[1];
269 lsda
= cast(ubyte*)unwindHeader
.barrier_cache
.bitpattern
[2];
270 landingPad
= cast(_Unwind_Ptr
)unwindHeader
.barrier_cache
.bitpattern
[3];
274 ExceptionHeader
* eh
= toExceptionHeader(unwindHeader
);
275 cfa
= eh
.canonicalFrameAddress
;
276 handler
= eh
.handler
;
277 lsda
= eh
.languageSpecificData
;
278 landingPad
= cast(_Unwind_Ptr
)eh
.landingPad
;
283 * Convert from pointer to unwindHeader to pointer to ExceptionHeader
284 * that it is embedded inside of.
286 static ExceptionHeader
* toExceptionHeader(_Unwind_Exception
* exc
) @nogc
288 return cast(ExceptionHeader
*)(cast(void*)exc
- ExceptionHeader
.unwindHeader
.offsetof
);
293 * Map to C++ std::type_info's virtual functions from D,
294 * being careful to not require linking with libstdc++.
295 * So it is given a different name.
297 extern(C
++) interface CxxTypeInfo
301 bool __is_pointer_p() const;
302 bool __is_function_p() const;
303 bool __do_catch(const CxxTypeInfo
, void**, uint) const;
304 bool __do_upcast(const void*, void**) const;
308 * Structure of a C++ exception, represented as a C structure.
309 * See unwind-cxx.h for the full definition.
311 struct CxaExceptionHeader
315 CxxTypeInfo exceptionType
;
316 void* primaryException
;
318 void function(void*) exceptionDestructor
;
319 void function() unexpectedHandler
;
320 void function() terminateHandler
;
321 CxaExceptionHeader
* nextException
;
324 static if (GNU_ARM_EABI_Unwinder
)
326 CxaExceptionHeader
* nextPropagatingException
;
327 int propagationCount
;
331 int handlerSwitchValue
;
332 const(ubyte)* actionRecord
;
333 const(ubyte)* languageSpecificData
;
334 _Unwind_Ptr catchTemp
;
338 _Unwind_Exception unwindHeader
;
341 * There's no saving between phases, so only cache pointer.
342 * __cxa_begin_catch expects this to be set.
344 static void save(_Unwind_Exception
* unwindHeader
, void* thrownPtr
) @nogc
346 static if (GNU_ARM_EABI_Unwinder
)
347 unwindHeader
.barrier_cache
.bitpattern
[0] = cast(_uw
) thrownPtr
;
350 auto eh
= toExceptionHeader(unwindHeader
);
351 eh
.adjustedPtr
= thrownPtr
;
356 * Get pointer to the thrown object if the thrown object type behind the
357 * exception is implicitly convertible to the catch type.
359 static void* getAdjustedPtr(_Unwind_Exception
* exc
, CxxTypeInfo catchType
)
363 // A dependent C++ exceptions is just a wrapper around the unwind header.
364 // A primary C++ exception has the thrown object located immediately after it.
365 if (isDependentException(exc
.exception_class
))
366 thrownPtr
= toExceptionHeader(exc
).primaryException
;
368 thrownPtr
= cast(void*)(exc
+ 1);
370 // Pointer types need to adjust the actual pointer, not the pointer that is
371 // the exception object. This also has the effect of passing pointer types
372 // "by value" through the __cxa_begin_catch return value.
373 const throw_type
= (cast(CxaExceptionHeader
*)thrownPtr
- 1).exceptionType
;
375 if (throw_type
.__is_pointer_p())
376 thrownPtr
= *cast(void**)thrownPtr
;
378 // Pointer adjustment may be necessary due to multiple inheritance
379 if (catchType
is throw_type
380 || catchType
.__do_catch(throw_type
, &thrownPtr
, 1))
387 * Convert from pointer to unwindHeader to pointer to CxaExceptionHeader
388 * that it is embedded inside of.
390 static CxaExceptionHeader
* toExceptionHeader(_Unwind_Exception
* exc
) @nogc
392 return cast(CxaExceptionHeader
*)(exc
+ 1) - 1;
397 * Called if exception handling must be abandoned for any reason.
399 private void terminate(string msg
, uint line
) @nogc
401 import core
.stdc
.stdio
;
402 import core
.stdc
.stdlib
;
404 static bool terminating
;
407 fputs("terminate called recursively\n", stderr
);
412 fprintf(stderr
, "gcc.deh(%u): %.*s\n", line
, cast(int)msg
.length
, msg
.ptr
);
418 * Called when fibers switch contexts.
420 extern(C
) void* _d_eh_swapContext(void* newContext
) nothrow @nogc
422 auto old
= ExceptionHeader
.stack
;
423 ExceptionHeader
.stack
= cast(ExceptionHeader
*)newContext
;
428 * Called before starting a catch. Returns the exception object.
430 extern(C
) void* __gdc_begin_catch(_Unwind_Exception
* unwindHeader
)
432 ExceptionHeader
* header
= ExceptionHeader
.toExceptionHeader(unwindHeader
);
434 void* objectp
= cast(void*)header
.object
;
435 // Remove our reference to the exception. We should not decrease its refcount,
436 // because we pass the object on to the caller.
437 header
.object
= null;
439 // Something went wrong when stacking up chained headers...
440 if (header
!= ExceptionHeader
.pop())
441 terminate("catch error", __LINE__
);
443 // Handling for this exception is complete.
444 _Unwind_DeleteException(&header
.unwindHeader
);
450 * Perform a throw, D style. Throw will unwind through this call,
451 * so there better not be any handlers or exception thrown here.
453 extern(C
) void _d_throw(Throwable object
)
455 // If possible, avoid always allocating new memory for exception headers.
456 ExceptionHeader
*eh
= ExceptionHeader
.create(object
);
458 // Add to thrown exception stack.
461 // Increment reference count if object is a refcounted Throwable.
462 auto refcount
= object
.refcount();
464 object
.refcount() = refcount
+ 1;
466 // Called by unwinder when exception object needs destruction by other than our code.
467 extern(C
) void exception_cleanup(_Unwind_Reason_Code code
, _Unwind_Exception
* exc
)
469 // If we haven't been caught by a foreign handler, then this is
470 // some sort of unwind error. In that case just die immediately.
471 // _Unwind_DeleteException in the HP-UX IA64 libunwind library
472 // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
473 // like the GCC _Unwind_DeleteException function does.
474 if (code
!= _URC_FOREIGN_EXCEPTION_CAUGHT
&& code
!= _URC_NO_REASON
)
475 terminate("uncaught exception", __LINE__
);
477 auto eh
= ExceptionHeader
.toExceptionHeader(exc
);
478 ExceptionHeader
.free(eh
);
481 eh
.unwindHeader
.exception_cleanup
= &exception_cleanup
;
483 // Runtime now expects us to do this first before unwinding.
484 _d_createTrace(eh
.object
, null);
486 // We're happy with setjmp/longjmp exceptions or region-based
487 // exception handlers: entry points are provided here for both.
488 _Unwind_Reason_Code r
= void;
490 version (GNU_SjLj_Exceptions
)
491 r
= _Unwind_SjLj_RaiseException(&eh
.unwindHeader
);
493 r
= _Unwind_RaiseException(&eh
.unwindHeader
);
495 // If code == _URC_END_OF_STACK, then we reached top of stack without finding
496 // a handler for the exception. Since each thread is run in a try/catch,
497 // this oughtn't happen. If code is something else, we encountered some sort
498 // of heinous lossage from which we could not recover. As is the way of such
499 // things, almost certainly we will have crashed before now, rather than
500 // actually being able to diagnose the problem.
501 if (r
== _URC_END_OF_STACK
)
503 __gdc_begin_catch(&eh
.unwindHeader
);
504 _d_print_throwable(object
);
505 terminate("uncaught exception", __LINE__
);
508 terminate("unwind error", __LINE__
);
511 static if (GNU_ARM_EABI_Unwinder
)
513 enum personality_fn_attributes
= attribute("target", ("general-regs-only"));
517 enum personality_fn_attributes
= "";
521 * Read and extract information from the LSDA (.gcc_except_table section).
523 @personality_fn_attributes
524 _Unwind_Reason_Code
scanLSDA(const(ubyte)* lsda
, _Unwind_Exception_Class exceptionClass
,
525 _Unwind_Action actions
, _Unwind_Exception
* unwindHeader
,
526 _Unwind_Context
* context
, _Unwind_Word cfa
,
527 out _Unwind_Ptr landingPad
, out int handler
)
529 // If no LSDA, then there are no handlers or cleanups.
531 return CONTINUE_UNWINDING(unwindHeader
, context
);
533 // Parse the LSDA header
536 auto Start
= (context ?
_Unwind_GetRegionStart(context
) : 0);
538 // Find @LPStart, the base to which landing pad offsets are relative.
539 ubyte LPStartEncoding
= *p
++;
540 _Unwind_Ptr LPStart
= 0;
542 if (LPStartEncoding
!= DW_EH_PE_omit
)
543 LPStart
= read_encoded_value(context
, LPStartEncoding
, p
);
547 // Find @TType, the base of the handler and exception spec type data.
548 ubyte TTypeEncoding
= *p
++;
549 const(ubyte)* TType
= null;
551 if (TTypeEncoding
!= DW_EH_PE_omit
)
553 static if (__traits(compiles
, _TTYPE_ENCODING
))
555 // Older ARM EABI toolchains set this value incorrectly, so use a
556 // hardcoded OS-specific format.
557 TTypeEncoding
= _TTYPE_ENCODING
;
559 auto TTbase
= read_uleb128(p
);
563 // The encoding and length of the call-site table; the action table
564 // immediately follows.
565 ubyte CSEncoding
= *p
++;
566 auto CSTableSize
= read_uleb128(p
);
567 const(ubyte)* actionTable
= p
+ CSTableSize
;
569 auto TTypeBase
= base_of_encoded_value(TTypeEncoding
, context
);
571 // Get instruction pointer (ip) at start of instruction that threw.
572 version (CRuntime_Glibc
)
575 auto ip
= _Unwind_GetIPInfo(context
, &ip_before_insn
);
581 auto ip
= _Unwind_GetIP(context
);
585 bool saw_cleanup
= false;
586 bool saw_handler
= false;
587 const(ubyte)* actionRecord
= null;
589 version (GNU_SjLj_Exceptions
)
591 // The given "IP" is an index into the call-site table, with two
592 // exceptions -- -1 means no-action, and 0 means terminate.
593 // But since we're using uleb128 values, we've not got random
594 // access to the array.
595 if (cast(int) ip
<= 0)
597 return _URC_CONTINUE_UNWIND
;
601 _uleb128_t CSLandingPad
, CSAction
;
604 CSLandingPad
= read_uleb128(p
);
605 CSAction
= read_uleb128(p
);
609 // Can never have null landing pad for sjlj -- that would have
610 // been indicated by a -1 call site index.
611 landingPad
= CSLandingPad
+ 1;
613 actionRecord
= actionTable
+ CSAction
- 1;
618 // Search the call-site table for the action associated with this IP.
619 while (p
< actionTable
)
621 // Note that all call-site encodings are "absolute" displacements.
622 auto CSStart
= read_encoded_value(null, CSEncoding
, p
);
623 auto CSLen
= read_encoded_value(null, CSEncoding
, p
);
624 auto CSLandingPad
= read_encoded_value(null, CSEncoding
, p
);
625 auto CSAction
= read_uleb128(p
);
627 // The table is sorted, so if we've passed the ip, stop.
628 if (ip
< Start
+ CSStart
)
630 else if (ip
< Start
+ CSStart
+ CSLen
)
633 landingPad
= LPStart
+ CSLandingPad
;
635 actionRecord
= actionTable
+ CSAction
- 1;
643 // IP is present, but has a null landing pad.
644 // No cleanups or handlers to be run.
646 else if (actionRecord
is null)
648 // If ip is present, has a non-null landing pad, and a null
649 // action table offset, then there are only cleanups present.
650 // Cleanups use a zero switch value, as set above.
655 // Otherwise we have a catch handler or exception specification.
656 handler
= actionTableLookup(actions
, unwindHeader
, actionRecord
,
657 lsda
, exceptionClass
, TTypeBase
,
658 TType
, TTypeEncoding
,
659 saw_handler
, saw_cleanup
);
662 // IP is not in table. No associated cleanups.
663 if (!saw_handler
&& !saw_cleanup
)
664 return CONTINUE_UNWINDING(unwindHeader
, context
);
666 if (actions
& _UA_SEARCH_PHASE
)
669 return CONTINUE_UNWINDING(unwindHeader
, context
);
671 // For domestic exceptions, we cache data from phase 1 for phase 2.
672 if (isGdcExceptionClass(exceptionClass
))
673 ExceptionHeader
.save(unwindHeader
, cfa
, handler
, lsda
, landingPad
);
675 return _URC_HANDLER_FOUND
;
682 * Look up and return the handler index of the classType in Action Table.
684 int actionTableLookup(_Unwind_Action actions
, _Unwind_Exception
* unwindHeader
,
685 const(ubyte)* actionRecord
, const(ubyte)* lsda
,
686 _Unwind_Exception_Class exceptionClass
,
687 _Unwind_Ptr TTypeBase
, const(ubyte)* TType
,
689 out bool saw_handler
, out bool saw_cleanup
)
691 ClassInfo thrownType
;
692 if (isGdcExceptionClass(exceptionClass
))
694 thrownType
= getClassInfo(unwindHeader
, lsda
);
699 auto ap
= actionRecord
;
700 auto ARFilter
= read_sleb128(ap
);
702 auto ARDisp
= read_sleb128(ap
);
706 // Zero filter values are cleanups.
709 else if (actions
& _UA_FORCE_UNWIND
)
711 // During forced unwinding, we only run cleanups.
713 else if (ARFilter
> 0)
715 // Positive filter values are handlers.
716 auto encodedSize
= size_of_encoded_value(TTypeEncoding
);
718 // ARFilter is the negative index from TType, which is where
719 // the ClassInfo is stored.
720 const(ubyte)* tp
= TType
- ARFilter
* encodedSize
;
722 auto entry
= read_encoded_value_with_base(TTypeEncoding
, TTypeBase
, tp
);
723 ClassInfo ci
= cast(ClassInfo
)cast(void*)(entry
);
725 // D does not have catch-all handlers, and so the following
726 // assumes that we will never handle a null value.
729 if (ci
.classinfo
is __cpp_type_info_ptr
.classinfo
730 && isGxxExceptionClass(exceptionClass
))
732 // catchType is the catch clause type_info.
733 auto catchType
= cast(CxxTypeInfo
)((cast(__cpp_type_info_ptr
)cast(void*)ci
).ptr
);
734 auto thrownPtr
= CxaExceptionHeader
.getAdjustedPtr(unwindHeader
, catchType
);
736 if (thrownPtr
!is null)
738 if (actions
& _UA_SEARCH_PHASE
)
739 CxaExceptionHeader
.save(unwindHeader
, thrownPtr
);
741 return cast(int)ARFilter
;
744 else if (isGdcExceptionClass(exceptionClass
)
745 && _d_isbaseof(thrownType
, ci
))
748 return cast(int)ARFilter
;
752 // ??? What to do about other GNU language exceptions.
757 // Negative filter values are exception specifications,
758 // which D does not use.
764 actionRecord
= apn
+ ARDisp
;
771 * Look at the chain of inflight exceptions and pick the class type that'll
772 * be looked for in catch clauses.
774 ClassInfo
getClassInfo(_Unwind_Exception
* unwindHeader
,
775 const(ubyte)* currentLsd
) @nogc
777 ExceptionHeader
* eh
= ExceptionHeader
.toExceptionHeader(unwindHeader
);
778 // The first thrown Exception at the top of the stack takes precedence
779 // over others that are inflight, unless an Error was thrown, in which
780 // case, we search for error handlers instead.
781 Throwable ehobject
= eh
.object
;
782 for (ExceptionHeader
* ehn
= eh
.next
; ehn
; ehn
= ehn
.next
)
784 const(ubyte)* nextLsd
= void;
785 _Unwind_Ptr nextLandingPad
= void;
786 _Unwind_Word nextCfa
= void;
787 int nextHandler
= void;
789 ExceptionHeader
.restore(&ehn
.unwindHeader
, nextHandler
, nextLsd
, nextLandingPad
, nextCfa
);
791 // Don't combine when the exceptions are from different functions.
792 if (currentLsd
!= nextLsd
)
795 Error e
= cast(Error
)ehobject
;
796 if (e
is null ||
(cast(Error
)ehn
.object
) !is null)
798 currentLsd
= nextLsd
;
799 ehobject
= ehn
.object
;
802 return ehobject
.classinfo
;
806 * Called when the personality function has found neither a cleanup or handler.
807 * To support ARM EABI personality routines, that must also unwind the stack.
809 @personality_fn_attributes
810 _Unwind_Reason_Code
CONTINUE_UNWINDING(_Unwind_Exception
* unwindHeader
, _Unwind_Context
* context
)
812 static if (GNU_ARM_EABI_Unwinder
)
814 if (__gnu_unwind_frame(unwindHeader
, context
) != _URC_OK
)
817 return _URC_CONTINUE_UNWIND
;
821 * Using a different personality function name causes link failures
822 * when trying to mix code using different exception handling models.
824 version (GNU_SEH_Exceptions
)
826 enum PERSONALITY_FUNCTION
= "__gdc_personality_imp";
828 extern(C
) EXCEPTION_DISPOSITION
__gdc_personality_seh0(void* ms_exc
, void* this_frame
,
829 void* ms_orig_context
, void* ms_disp
)
831 return _GCC_specific_handler(ms_exc
, this_frame
, ms_orig_context
,
832 ms_disp
, &gdc_personality
);
835 else version (GNU_SjLj_Exceptions
)
837 enum PERSONALITY_FUNCTION
= "__gdc_personality_sj0";
839 private int __builtin_eh_return_data_regno(int x
) { return x
; }
843 enum PERSONALITY_FUNCTION
= "__gdc_personality_v0";
847 * The "personality" function, specific to each language.
849 static if (GNU_ARM_EABI_Unwinder
)
851 pragma(mangle
, PERSONALITY_FUNCTION
)
852 @personality_fn_attributes
853 extern(C
) _Unwind_Reason_Code
gdc_personality(_Unwind_State state
,
854 _Unwind_Exception
* unwindHeader
,
855 _Unwind_Context
* context
)
857 _Unwind_Action actions
;
859 switch (state
& _US_ACTION_MASK
)
861 case _US_VIRTUAL_UNWIND_FRAME
:
862 // If the unwind state pattern is (_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND)
863 // then we don't need to search for any handler as it is not a real exception.
864 // Just unwind the stack.
865 if (state
& _US_FORCE_UNWIND
)
866 return CONTINUE_UNWINDING(unwindHeader
, context
);
867 actions
= _UA_SEARCH_PHASE
;
870 case _US_UNWIND_FRAME_STARTING
:
871 actions
= _UA_CLEANUP_PHASE
;
872 if (!(state
& _US_FORCE_UNWIND
)
873 && unwindHeader
.barrier_cache
.sp
== _Unwind_GetGR(context
, UNWIND_STACK_REG
))
874 actions |
= _UA_HANDLER_FRAME
;
877 case _US_UNWIND_FRAME_RESUME
:
878 return CONTINUE_UNWINDING(unwindHeader
, context
);
881 terminate("unwind error", __LINE__
);
883 actions |
= state
& _US_FORCE_UNWIND
;
885 // The dwarf unwinder assumes the context structure holds things like
886 // the function and LSDA pointers. The ARM implementation caches these
887 // in the exception header (UCB). To avoid rewriting everything we make
888 // the virtual IP register point at the UCB.
889 _Unwind_SetGR(context
, UNWIND_POINTER_REG
, cast(_Unwind_Ptr
)unwindHeader
);
891 return __gdc_personality(actions
, unwindHeader
.exception_class
,
892 unwindHeader
, context
);
897 pragma(mangle
, PERSONALITY_FUNCTION
)
898 extern(C
) _Unwind_Reason_Code
gdc_personality(int iversion
,
899 _Unwind_Action actions
,
900 _Unwind_Exception_Class exceptionClass
,
901 _Unwind_Exception
* unwindHeader
,
902 _Unwind_Context
* context
)
904 // Interface version check.
906 return _URC_FATAL_PHASE1_ERROR
;
908 return __gdc_personality(actions
, exceptionClass
, unwindHeader
, context
);
912 @personality_fn_attributes
913 private _Unwind_Reason_Code
__gdc_personality(_Unwind_Action actions
,
914 _Unwind_Exception_Class exceptionClass
,
915 _Unwind_Exception
* unwindHeader
,
916 _Unwind_Context
* context
)
919 _Unwind_Ptr landingPad
;
923 // Shortcut for phase 2 found handler for domestic exception.
924 if (actions
== (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME
)
925 && isGdcExceptionClass(exceptionClass
))
927 ExceptionHeader
.restore(unwindHeader
, handler
, lsda
, landingPad
, cfa
);
928 // Shouldn't have cached a null landing pad in phase 1.
930 terminate("unwind error", __LINE__
);
934 lsda
= cast(ubyte*)_Unwind_GetLanguageSpecificData(context
);
936 static if (GNU_ARM_EABI_Unwinder
)
937 cfa
= _Unwind_GetGR(context
, UNWIND_STACK_REG
);
939 cfa
= _Unwind_GetCFA(context
);
941 auto result
= scanLSDA(lsda
, exceptionClass
, actions
, unwindHeader
,
942 context
, cfa
, landingPad
, handler
);
944 // Positive on handler found in phase 1, continue unwinding, or failure.
949 // Unexpected negative handler, call terminate directly.
951 terminate("unwind error", __LINE__
);
953 // We can't use any of the deh routines with foreign exceptions,
954 // because they all expect unwindHeader to be an ExceptionHeader.
955 if (isGdcExceptionClass(exceptionClass
))
957 // If there are any in-flight exceptions being thrown, chain our
958 // current object onto the end of the prevous object.
959 ExceptionHeader
* eh
= ExceptionHeader
.toExceptionHeader(unwindHeader
);
960 auto currentLsd
= lsda
;
961 bool bypassed
= false;
965 ExceptionHeader
* ehn
= eh
.next
;
966 const(ubyte)* nextLsd
= void;
967 _Unwind_Ptr nextLandingPad
= void;
968 _Unwind_Word nextCfa
= void;
969 int nextHandler
= void;
971 ExceptionHeader
.restore(&ehn
.unwindHeader
, nextHandler
, nextLsd
, nextLandingPad
, nextCfa
);
973 Error e
= cast(Error
)eh
.object
;
974 if (e
!is null && !cast(Error
)ehn
.object
)
976 // We found an Error, bypass the exception chain.
977 currentLsd
= nextLsd
;
983 // Don't combine when the exceptions are from different functions.
984 if (currentLsd
!= nextLsd
)
987 // Add our object onto the end of the existing chain and replace
988 // our exception object with in-flight one.
989 eh
.object
= Throwable
.chainTogether(ehn
.object
, eh
.object
);
991 if (nextHandler
!= handler
&& !bypassed
)
993 handler
= nextHandler
;
994 ExceptionHeader
.save(unwindHeader
, cfa
, handler
, lsda
, landingPad
);
997 // Exceptions chained, can now throw away the previous header.
999 _Unwind_DeleteException(&ehn
.unwindHeader
);
1004 eh
= ExceptionHeader
.toExceptionHeader(unwindHeader
);
1005 Error e
= cast(Error
)eh
.object
;
1007 e
.bypassedException
= ehn
.object
;
1009 _Unwind_DeleteException(&ehn
.unwindHeader
);
1013 // Set up registers and jump to cleanup or handler.
1014 // For targets with pointers smaller than the word size, we must extend the
1015 // pointer, and this extension is target dependent.
1016 _Unwind_SetGR(context
, __builtin_eh_return_data_regno(0),
1017 cast(_Unwind_Ptr
)unwindHeader
);
1018 _Unwind_SetGR(context
, __builtin_eh_return_data_regno(1), handler
);
1019 _Unwind_SetIP(context
, landingPad
);
1021 return _URC_INSTALL_CONTEXT
;