2 * Copyright 2010-2011 PathScale, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
15 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #pragma weak pthread_key_create
39 #pragma weak pthread_setspecific
40 #pragma weak pthread_getspecific
41 #pragma weak pthread_once
42 #ifdef LIBCXXRT_WEAK_LOCKS
43 #pragma weak pthread_mutex_lock
44 #define pthread_mutex_lock(mtx) do {\
45 if (pthread_mutex_lock) pthread_mutex_lock(mtx);\
47 #pragma weak pthread_mutex_unlock
48 #define pthread_mutex_unlock(mtx) do {\
49 if (pthread_mutex_unlock) pthread_mutex_unlock(mtx);\
51 #pragma weak pthread_cond_signal
52 #define pthread_cond_signal(cv) do {\
53 if (pthread_cond_signal) pthread_cond_signal(cv);\
55 #pragma weak pthread_cond_wait
56 #define pthread_cond_wait(cv, mtx) do {\
57 if (pthread_cond_wait) pthread_cond_wait(cv, mtx);\
61 using namespace ABI_NAMESPACE;
64 * Saves the result of the landing pad that we have found. For ARM, this is
65 * stored in the generic unwind structure, while on other platforms it is
66 * stored in the C++ exception.
68 static void saveLandingPad(struct _Unwind_Context *context,
69 struct _Unwind_Exception *ucb,
70 struct __cxa_exception *ex,
72 dw_eh_ptr_t landingPad)
75 // On ARM, we store the saved exception in the generic part of the structure
76 ucb->barrier_cache.sp = _Unwind_GetGR(context, 13);
77 ucb->barrier_cache.bitpattern[1] = (uint32_t)selector;
78 ucb->barrier_cache.bitpattern[3] = (uint32_t)landingPad;
80 // Cache the results for the phase 2 unwind, if we found a handler
81 // and this is not a foreign exception.
84 ex->handlerSwitchValue = selector;
85 ex->catchTemp = landingPad;
90 * Loads the saved landing pad. Returns 1 on success, 0 on failure.
92 static int loadLandingPad(struct _Unwind_Context *context,
93 struct _Unwind_Exception *ucb,
94 struct __cxa_exception *ex,
95 unsigned long *selector,
96 dw_eh_ptr_t *landingPad)
99 *selector = ucb->barrier_cache.bitpattern[1];
100 *landingPad = (dw_eh_ptr_t)ucb->barrier_cache.bitpattern[3];
105 *selector = ex->handlerSwitchValue;
106 *landingPad = (dw_eh_ptr_t)ex->catchTemp;
113 static inline _Unwind_Reason_Code continueUnwinding(struct _Unwind_Exception *ex,
114 struct _Unwind_Context *context)
117 if (__gnu_unwind_frame(ex, context) != _URC_OK) { return _URC_FAILURE; }
119 return _URC_CONTINUE_UNWIND;
123 extern "C" void __cxa_free_exception(void *thrown_exception);
124 extern "C" void __cxa_free_dependent_exception(void *thrown_exception);
125 extern "C" void* __dynamic_cast(const void *sub,
126 const __class_type_info *src,
127 const __class_type_info *dst,
128 ptrdiff_t src2dst_offset);
131 * The type of a handler that has been found.
138 * A cleanup - the exception will propagate through this frame, but code
139 * must be run when this happens.
143 * A catch statement. The exception will not propagate past this frame
144 * (without an explicit rethrow).
150 * Per-thread info required by the runtime. We store a single structure
151 * pointer in thread-local storage, because this tends to be a scarce resource
152 * and it's impolite to steal all of it and not leave any for the rest of the
155 * Instances of this structure are allocated lazily - at most one per thread -
156 * and are destroyed on thread termination.
158 struct __cxa_thread_info
160 /** The termination handler for this thread. */
161 terminate_handler terminateHandler;
162 /** The unexpected exception handler for this thread. */
163 unexpected_handler unexpectedHandler;
165 * The number of emergency buffers held by this thread. This is 0 in
166 * normal operation - the emergency buffers are only used when malloc()
167 * fails to return memory for allocating an exception. Threads are not
168 * permitted to hold more than 4 emergency buffers (as per recommendation
169 * in ABI spec [3.3.1]).
171 int emergencyBuffersHeld;
173 * The exception currently running in a cleanup.
175 _Unwind_Exception *currentCleanup;
177 * Our state with respect to foreign exceptions. Usually none, set to
178 * caught if we have just caught an exception and rethrown if we are
186 } foreign_exception_state;
188 * The public part of this structure, accessible from outside of this
191 __cxa_eh_globals globals;
194 * Dependent exception. This
196 struct __cxa_dependent_exception
199 void *primaryException;
201 std::type_info *exceptionType;
202 void (*exceptionDestructor) (void *);
203 unexpected_handler unexpectedHandler;
204 terminate_handler terminateHandler;
205 __cxa_exception *nextException;
208 _Unwind_Exception *nextCleanup;
211 int handlerSwitchValue;
212 const char *actionRecord;
213 const char *languageSpecificData;
217 void *primaryException;
219 _Unwind_Exception unwindHeader;
229 virtual ~exception() throw();
230 virtual const char* what() const throw();
236 * Class of exceptions to distinguish between this and other exception types.
238 * The first four characters are the vendor ID. Currently, we use GNUC,
239 * because we aim for ABI-compatibility with the GNU implementation, and
240 * various checks may test for equality of the class, which is incorrect.
242 static const uint64_t exception_class =
243 EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\0');
245 * Class used for dependent exceptions.
247 static const uint64_t dependent_exception_class =
248 EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\x01');
250 * The low four bytes of the exception class, indicating that we conform to the
251 * Itanium C++ ABI. This is currently unused, but should be used in the future
252 * if we change our exception class, to allow this library and libsupc++ to be
253 * linked to the same executable and both to interoperate.
255 static const uint32_t abi_exception_class =
256 GENERIC_EXCEPTION_CLASS('C', '+', '+', '\0');
258 static bool isCXXException(uint64_t cls)
260 return (cls == exception_class) || (cls == dependent_exception_class);
263 static bool isDependentException(uint64_t cls)
265 return cls == dependent_exception_class;
268 static __cxa_exception *exceptionFromPointer(void *ex)
270 return (__cxa_exception*)((char*)ex -
271 offsetof(struct __cxa_exception, unwindHeader));
273 static __cxa_exception *realExceptionFromException(__cxa_exception *ex)
275 if (!isDependentException(ex->unwindHeader.exception_class)) { return ex; }
276 return ((__cxa_exception*)(((__cxa_dependent_exception*)ex)->primaryException))-1;
282 // Forward declaration of standard library terminate() function used to
284 void terminate(void);
287 using namespace ABI_NAMESPACE;
291 /** The global termination handler. */
292 static terminate_handler terminateHandler = abort;
293 /** The global unexpected exception handler. */
294 static unexpected_handler unexpectedHandler = std::terminate;
296 /** Key used for thread-local data. */
297 static pthread_key_t eh_key;
301 * Cleanup function, allowing foreign exception handlers to correctly destroy
302 * this exception if they catch it.
304 static void exception_cleanup(_Unwind_Reason_Code reason,
305 struct _Unwind_Exception *ex)
307 __cxa_free_exception((void*)ex);
309 static void dependent_exception_cleanup(_Unwind_Reason_Code reason,
310 struct _Unwind_Exception *ex)
313 __cxa_free_dependent_exception((void*)ex);
317 * Recursively walk a list of exceptions and delete them all in post-order.
319 static void free_exception_list(__cxa_exception *ex)
321 if (0 != ex->nextException)
323 free_exception_list(ex->nextException);
325 // __cxa_free_exception() expects to be passed the thrown object, which
326 // immediately follows the exception, not the exception itself
327 __cxa_free_exception(ex+1);
331 * Cleanup function called when a thread exists to make certain that all of the
332 * per-thread data is deleted.
334 static void thread_cleanup(void* thread_info)
336 __cxa_thread_info *info = (__cxa_thread_info*)thread_info;
337 if (info->globals.caughtExceptions)
339 // If this is a foreign exception, ask it to clean itself up.
340 if (info->foreign_exception_state != __cxa_thread_info::none)
342 _Unwind_Exception *e = (_Unwind_Exception*)info->globals.caughtExceptions;
343 e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
347 free_exception_list(info->globals.caughtExceptions);
355 * Once control used to protect the key creation.
357 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
360 * We may not be linked against a full pthread implementation. If we're not,
361 * then we need to fake the thread-local storage by storing 'thread-local'
362 * things in a global.
366 * Thread-local storage for a single-threaded program.
368 static __cxa_thread_info singleThreadInfo;
372 static void init_key(void)
374 if ((0 == pthread_key_create) ||
375 (0 == pthread_setspecific) ||
376 (0 == pthread_getspecific))
381 pthread_key_create(&eh_key, thread_cleanup);
382 pthread_setspecific(eh_key, (void*)0x42);
383 fakeTLS = (pthread_getspecific(eh_key) != (void*)0x42);
384 pthread_setspecific(eh_key, 0);
388 * Returns the thread info structure, creating it if it is not already created.
390 static __cxa_thread_info *thread_info()
392 if ((0 == pthread_once) || pthread_once(&once_control, init_key))
396 if (fakeTLS) { return &singleThreadInfo; }
397 __cxa_thread_info *info = (__cxa_thread_info*)pthread_getspecific(eh_key);
400 info = (__cxa_thread_info*)calloc(1, sizeof(__cxa_thread_info));
401 pthread_setspecific(eh_key, info);
406 * Fast version of thread_info(). May fail if thread_info() is not called on
407 * this thread at least once already.
409 static __cxa_thread_info *thread_info_fast()
411 if (fakeTLS) { return &singleThreadInfo; }
412 return (__cxa_thread_info*)pthread_getspecific(eh_key);
415 * ABI function returning the __cxa_eh_globals structure.
417 extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals(void)
419 return &(thread_info()->globals);
422 * Version of __cxa_get_globals() assuming that __cxa_get_globals() has already
423 * been called at least once by this thread.
425 extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals_fast(void)
427 return &(thread_info_fast()->globals);
431 * An emergency allocation reserved for when malloc fails. This is treated as
432 * 16 buffers of 1KB each.
434 static char emergency_buffer[16384];
436 * Flag indicating whether each buffer is allocated.
438 static bool buffer_allocated[16];
440 * Lock used to protect emergency allocation.
442 static pthread_mutex_t emergency_malloc_lock = PTHREAD_MUTEX_INITIALIZER;
444 * Condition variable used to wait when two threads are both trying to use the
445 * emergency malloc() buffer at once.
447 static pthread_cond_t emergency_malloc_wait = PTHREAD_COND_INITIALIZER;
450 * Allocates size bytes from the emergency allocation mechanism, if possible.
451 * This function will fail if size is over 1KB or if this thread already has 4
452 * emergency buffers. If all emergency buffers are allocated, it will sleep
453 * until one becomes available.
455 static char *emergency_malloc(size_t size)
457 if (size > 1024) { return 0; }
459 __cxa_thread_info *info = thread_info();
460 // Only 4 emergency buffers allowed per thread!
461 if (info->emergencyBuffersHeld > 3) { return 0; }
463 pthread_mutex_lock(&emergency_malloc_lock);
467 // While we were sleeping on the lock, another thread might have free'd
468 // enough memory for us to use, so try the allocation again - no point
469 // using the emergency buffer if there is some real memory that we can
471 void *m = calloc(1, size);
474 pthread_mutex_unlock(&emergency_malloc_lock);
477 for (int i=0 ; i<16 ; i++)
479 if (!buffer_allocated[i])
482 buffer_allocated[i] = true;
486 // If there still isn't a buffer available, then sleep on the condition
487 // variable. This will be signalled when another thread releases one
488 // of the emergency buffers.
491 pthread_cond_wait(&emergency_malloc_wait, &emergency_malloc_lock);
494 pthread_mutex_unlock(&emergency_malloc_lock);
495 info->emergencyBuffersHeld++;
496 return emergency_buffer + (1024 * buffer);
500 * Frees a buffer returned by emergency_malloc().
502 * Note: Neither this nor emergency_malloc() is particularly efficient. This
503 * should not matter, because neither will be called in normal operation - they
504 * are only used when the program runs out of memory, which should not happen
507 static void emergency_malloc_free(char *ptr)
510 // Find the buffer corresponding to this pointer.
511 for (int i=0 ; i<16 ; i++)
513 if (ptr == (void*)(emergency_buffer + (1024 * i)))
520 "Trying to free something that is not an emergency buffer!");
521 // emergency_malloc() is expected to return 0-initialized data. We don't
522 // zero the buffer when allocating it, because the static buffers will
523 // begin life containing 0 values.
524 memset((void*)ptr, 0, 1024);
525 // Signal the condition variable to wake up any threads that are blocking
526 // waiting for some space in the emergency buffer
527 pthread_mutex_lock(&emergency_malloc_lock);
528 // In theory, we don't need to do this with the lock held. In practice,
529 // our array of bools will probably be updated using 32-bit or 64-bit
530 // memory operations, so this update may clobber adjacent values.
531 buffer_allocated[buffer] = false;
532 pthread_cond_signal(&emergency_malloc_wait);
533 pthread_mutex_unlock(&emergency_malloc_lock);
536 static char *alloc_or_die(size_t size)
538 char *buffer = (char*)calloc(1, size);
540 // If calloc() doesn't want to give us any memory, try using an emergency
544 buffer = emergency_malloc(size);
545 // This is only reached if the allocation is greater than 1KB, and
546 // anyone throwing objects that big really should know better.
549 fprintf(stderr, "Out of memory attempting to allocate exception\n");
555 static void free_exception(char *e)
557 // If this allocation is within the address range of the emergency buffer,
558 // don't call free() because it was not allocated with malloc()
559 if ((e > emergency_buffer) &&
560 (e < (emergency_buffer + sizeof(emergency_buffer))))
562 emergency_malloc_free(e);
571 * Allocates an exception structure. Returns a pointer to the space that can
572 * be used to store an object of thrown_size bytes. This function will use an
573 * emergency buffer if malloc() fails, and may block if there are no such
576 extern "C" void *__cxa_allocate_exception(size_t thrown_size)
578 size_t size = thrown_size + sizeof(__cxa_exception);
579 char *buffer = alloc_or_die(size);
580 return buffer+sizeof(__cxa_exception);
583 extern "C" void *__cxa_allocate_dependent_exception(void)
585 size_t size = sizeof(__cxa_dependent_exception);
586 char *buffer = alloc_or_die(size);
587 return buffer+sizeof(__cxa_dependent_exception);
591 * __cxa_free_exception() is called when an exception was thrown in between
592 * calling __cxa_allocate_exception() and actually throwing the exception.
593 * This happens when the object's copy constructor throws an exception.
595 * In this implementation, it is also called by __cxa_end_catch() and during
598 extern "C" void __cxa_free_exception(void *thrown_exception)
600 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
601 // Free the object that was thrown, calling its destructor
602 if (0 != ex->exceptionDestructor)
606 ex->exceptionDestructor(thrown_exception);
610 // FIXME: Check that this is really what the spec says to do.
615 free_exception((char*)ex);
618 static void releaseException(__cxa_exception *exception)
620 if (isDependentException(exception->unwindHeader.exception_class))
622 __cxa_free_dependent_exception(exception+1);
625 if (__sync_sub_and_fetch(&exception->referenceCount, 1) == 0)
627 // __cxa_free_exception() expects to be passed the thrown object,
628 // which immediately follows the exception, not the exception
630 __cxa_free_exception(exception+1);
634 void __cxa_free_dependent_exception(void *thrown_exception)
636 __cxa_dependent_exception *ex = ((__cxa_dependent_exception*)thrown_exception) - 1;
637 assert(isDependentException(ex->unwindHeader.exception_class));
638 if (ex->primaryException)
640 releaseException(realExceptionFromException((__cxa_exception*)ex));
642 free_exception((char*)ex);
646 * Callback function used with _Unwind_Backtrace().
648 * Prints a stack trace. Used only for debugging help.
650 * Note: As of FreeBSD 8.1, dladd() still doesn't work properly, so this only
651 * correctly prints function names from public, relocatable, symbols.
653 static _Unwind_Reason_Code trace(struct _Unwind_Context *context, void *c)
657 dladdr((void*)(uintptr_t)__cxa_current_exception_type, &myinfo);
658 void *ip = (void*)_Unwind_GetIP(context);
660 if (dladdr(ip, &info) != 0)
662 if (mylookup == 0 || strcmp(info.dli_fname, myinfo.dli_fname) != 0)
664 printf("%p:%s() in %s\n", ip, info.dli_sname, info.dli_fname);
667 return _URC_CONTINUE_UNWIND;
671 * Report a failure that occurred when attempting to throw an exception.
673 * If the failure happened by falling off the end of the stack without finding
674 * a handler, prints a back trace before aborting.
676 static void report_failure(_Unwind_Reason_Code err, __cxa_exception *thrown_exception)
681 case _URC_FATAL_PHASE1_ERROR:
682 fprintf(stderr, "Fatal error during phase 1 unwinding\n");
685 case _URC_FATAL_PHASE2_ERROR:
686 fprintf(stderr, "Fatal error during phase 2 unwinding\n");
689 case _URC_END_OF_STACK:
690 fprintf(stderr, "Terminating due to uncaught exception %p",
691 (void*)thrown_exception);
692 thrown_exception = realExceptionFromException(thrown_exception);
693 static const __class_type_info *e_ti =
694 static_cast<const __class_type_info*>(&typeid(std::exception));
695 const __class_type_info *throw_ti =
696 dynamic_cast<const __class_type_info*>(thrown_exception->exceptionType);
700 (std::exception*)e_ti->cast_to((void*)(thrown_exception+1),
704 fprintf(stderr, " '%s'", e->what());
708 size_t bufferSize = 128;
709 char *demangled = (char*)malloc(bufferSize);
710 const char *mangled = thrown_exception->exceptionType->name();
712 demangled = __cxa_demangle(mangled, demangled, &bufferSize, &status);
713 fprintf(stderr, " of type %s\n",
714 status == 0 ? (const char*)demangled : mangled);
715 if (status == 0) { free(demangled); }
716 // Print a back trace if no handler is found.
717 // TODO: Make this optional
719 _Unwind_Backtrace(trace, 0);
726 static void throw_exception(__cxa_exception *ex)
728 __cxa_thread_info *info = thread_info();
729 ex->unexpectedHandler = info->unexpectedHandler;
730 if (0 == ex->unexpectedHandler)
732 ex->unexpectedHandler = unexpectedHandler;
734 ex->terminateHandler = info->terminateHandler;
735 if (0 == ex->terminateHandler)
737 ex->terminateHandler = terminateHandler;
739 info->globals.uncaughtExceptions++;
741 _Unwind_Reason_Code err = _Unwind_RaiseException(&ex->unwindHeader);
742 // The _Unwind_RaiseException() function should not return, it should
743 // unwind the stack past this function. If it does return, then something
745 report_failure(err, ex);
750 * ABI function for throwing an exception. Takes the object to be thrown (the
751 * pointer returned by __cxa_allocate_exception()), the type info for the
752 * pointee, and the destructor (if there is one) as arguments.
754 extern "C" void __cxa_throw(void *thrown_exception,
755 std::type_info *tinfo,
758 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
760 ex->referenceCount = 1;
761 ex->exceptionType = tinfo;
763 ex->exceptionDestructor = dest;
765 ex->unwindHeader.exception_class = exception_class;
766 ex->unwindHeader.exception_cleanup = exception_cleanup;
771 extern "C" void __cxa_rethrow_primary_exception(void* thrown_exception)
773 if (NULL == thrown_exception) { return; }
775 __cxa_exception *original = exceptionFromPointer(thrown_exception);
776 __cxa_dependent_exception *ex = ((__cxa_dependent_exception*)__cxa_allocate_dependent_exception())-1;
778 ex->primaryException = thrown_exception;
779 __cxa_increment_exception_refcount(thrown_exception);
781 ex->exceptionType = original->exceptionType;
782 ex->unwindHeader.exception_class = dependent_exception_class;
783 ex->unwindHeader.exception_cleanup = dependent_exception_cleanup;
785 throw_exception((__cxa_exception*)ex);
788 extern "C" void *__cxa_current_primary_exception(void)
790 __cxa_eh_globals* globals = __cxa_get_globals();
791 __cxa_exception *ex = globals->caughtExceptions;
793 if (0 == ex) { return NULL; }
794 ex = realExceptionFromException(ex);
795 __sync_fetch_and_add(&ex->referenceCount, 1);
799 extern "C" void __cxa_increment_exception_refcount(void* thrown_exception)
801 if (NULL == thrown_exception) { return; }
802 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
803 if (isDependentException(ex->unwindHeader.exception_class)) { return; }
804 __sync_fetch_and_add(&ex->referenceCount, 1);
806 extern "C" void __cxa_decrement_exception_refcount(void* thrown_exception)
808 if (NULL == thrown_exception) { return; }
809 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
810 releaseException(ex);
814 * ABI function. Rethrows the current exception. Does not remove the
815 * exception from the stack or decrement its handler count - the compiler is
816 * expected to set the landing pad for this function to the end of the catch
817 * block, and then call _Unwind_Resume() to continue unwinding once
818 * __cxa_end_catch() has been called and any cleanup code has been run.
820 extern "C" void __cxa_rethrow()
822 __cxa_thread_info *ti = thread_info();
823 __cxa_eh_globals *globals = &ti->globals;
824 // Note: We don't remove this from the caught list here, because
825 // __cxa_end_catch will be called when we unwind out of the try block. We
826 // could probably make this faster by providing an alternative rethrow
827 // function and ensuring that all cleanup code is run before calling it, so
828 // we can skip the top stack frame when unwinding.
829 __cxa_exception *ex = globals->caughtExceptions;
834 "Attempting to rethrow an exception that doesn't exist!\n");
838 if (ti->foreign_exception_state != __cxa_thread_info::none)
840 ti->foreign_exception_state = __cxa_thread_info::rethrown;
841 _Unwind_Exception *e = (_Unwind_Exception*)ex;
842 _Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(e);
843 report_failure(err, ex);
847 assert(ex->handlerCount > 0 && "Rethrowing uncaught exception!");
849 // ex->handlerCount will be decremented in __cxa_end_catch in enclosing
852 // Make handler count negative. This will tell __cxa_end_catch that
853 // exception was rethrown and exception object should not be destroyed
854 // when handler count become zero
855 ex->handlerCount = -ex->handlerCount;
857 // Continue unwinding the stack with this exception. This should unwind to
858 // the place in the caller where __cxa_end_catch() is called. The caller
859 // will then run cleanup code and bounce the exception back with
861 _Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(&ex->unwindHeader);
862 report_failure(err, ex);
866 * Returns the type_info object corresponding to the filter.
868 static std::type_info *get_type_info_entry(_Unwind_Context *context,
872 // Get the address of the record in the table.
873 dw_eh_ptr_t record = lsda->type_table -
874 dwarf_size_of_fixed_size_field(lsda->type_table_encoding)*filter;
876 dw_eh_ptr_t start = record;
877 // Read the value, but it's probably an indirect reference...
878 int64_t offset = read_value(lsda->type_table_encoding, &record);
880 // (If the entry is 0, don't try to dereference it. That would be bad.)
881 if (offset == 0) { return 0; }
883 // ...so we need to resolve it
884 return (std::type_info*)resolve_indirect_value(context,
885 lsda->type_table_encoding, offset, start);
891 * Checks the type signature found in a handler against the type of the thrown
892 * object. If ex is 0 then it is assumed to be a foreign exception and only
895 static bool check_type_signature(__cxa_exception *ex,
896 const std::type_info *type,
899 void *exception_ptr = (void*)(ex+1);
900 const std::type_info *ex_type = ex ? ex->exceptionType : 0;
902 bool is_ptr = ex ? ex_type->__is_pointer_p() : false;
905 exception_ptr = *(void**)exception_ptr;
907 // Always match a catchall, even with a foreign exception
909 // Note: A 0 here is a catchall, not a cleanup, so we return true to
910 // indicate that we found a catch.
915 adjustedPtr = exception_ptr;
920 if (0 == ex) { return false; }
922 // If the types are the same, no casting is needed.
923 if (*type == *ex_type)
925 adjustedPtr = exception_ptr;
930 if (type->__do_catch(ex_type, &exception_ptr, 1))
932 adjustedPtr = exception_ptr;
939 * Checks whether the exception matches the type specifiers in this action
940 * record. If the exception only matches cleanups, then this returns false.
941 * If it matches a catch (including a catchall) then it returns true.
943 * The selector argument is used to return the selector that is passed in the
944 * second exception register when installing the context.
946 static handler_type check_action_record(_Unwind_Context *context,
948 dw_eh_ptr_t action_record,
950 unsigned long *selector,
953 if (!action_record) { return handler_cleanup; }
954 handler_type found = handler_none;
955 while (action_record)
957 int filter = read_sleb128(&action_record);
958 dw_eh_ptr_t action_record_offset_base = action_record;
959 int displacement = read_sleb128(&action_record);
960 action_record = displacement ?
961 action_record_offset_base + displacement : 0;
962 // We only check handler types for C++ exceptions - foreign exceptions
963 // are only allowed for cleanups and catchalls.
966 std::type_info *handler_type = get_type_info_entry(context, lsda, filter);
967 if (check_type_signature(ex, handler_type, adjustedPtr))
970 return handler_catch;
973 else if (filter < 0 && 0 != ex)
975 bool matched = false;
979 std::type_info *handler_type = get_type_info_entry(context, lsda, filter--);
982 if (check_type_signature(ex, handler_type, adjustedPtr))
987 handler_type = get_type_info_entry(context, lsda, filter--);
990 unsigned char *type_index = ((unsigned char*)lsda->type_table - filter - 1);
993 std::type_info *handler_type = get_type_info_entry(context, lsda, *(type_index++));
994 // If the exception spec matches a permitted throw type for
995 // this function, don't report a handler - we are allowed to
996 // propagate this exception out.
997 if (check_type_signature(ex, handler_type, adjustedPtr))
1004 if (matched) { continue; }
1005 // If we don't find an allowed exception spec, we need to install
1006 // the context for this action. The landing pad will then call the
1007 // unexpected exception function. Treat this as a catch
1008 return handler_catch;
1010 else if (filter == 0)
1013 found = handler_cleanup;
1019 static void pushCleanupException(_Unwind_Exception *exceptionObject,
1020 __cxa_exception *ex)
1023 __cxa_thread_info *info = thread_info_fast();
1027 if (ex->cleanupCount > 1)
1029 assert(exceptionObject == info->currentCleanup);
1032 ex->nextCleanup = info->currentCleanup;
1034 info->currentCleanup = exceptionObject;
1039 * The exception personality function. This is referenced in the unwinding
1040 * DWARF metadata and is called by the unwind library for each C++ stack frame
1041 * containing catch or cleanup code.
1044 BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0)
1045 // This personality function is for version 1 of the ABI. If you use it
1046 // with a future version of the ABI, it won't know what to do, so it
1047 // reports a fatal error and give up before it breaks anything.
1050 return _URC_FATAL_PHASE1_ERROR;
1052 __cxa_exception *ex = 0;
1053 __cxa_exception *realEx = 0;
1055 // If this exception is throw by something else then we can't make any
1056 // assumptions about its layout beyond the fields declared in
1057 // _Unwind_Exception.
1058 bool foreignException = !isCXXException(exceptionClass);
1060 // If this isn't a foreign exception, then we have a C++ exception structure
1061 if (!foreignException)
1063 ex = exceptionFromPointer(exceptionObject);
1064 realEx = realExceptionFromException(ex);
1067 unsigned char *lsda_addr =
1068 (unsigned char*)_Unwind_GetLanguageSpecificData(context);
1070 // No LSDA implies no landing pads - try the next frame
1071 if (0 == lsda_addr) { return continueUnwinding(exceptionObject, context); }
1073 // These two variables define how the exception will be handled.
1074 dwarf_eh_action action = {0};
1075 unsigned long selector = 0;
1077 // During the search phase, we do a complete lookup. If we return
1078 // _URC_HANDLER_FOUND, then the phase 2 unwind will call this function with
1079 // a _UA_HANDLER_FRAME action, telling us to install the handler frame. If
1080 // we return _URC_CONTINUE_UNWIND, we may be called again later with a
1081 // _UA_CLEANUP_PHASE action for this frame.
1083 // The point of the two-stage unwind allows us to entirely avoid any stack
1084 // unwinding if there is no handler. If there are just cleanups found,
1085 // then we can just panic call an abort function.
1087 // Matching a handler is much more expensive than matching a cleanup,
1088 // because we don't need to bother doing type comparisons (or looking at
1089 // the type table at all) for a cleanup. This means that there is no need
1090 // to cache the result of finding a cleanup, because it's (quite) quick to
1091 // look it up again from the action table.
1092 if (actions & _UA_SEARCH_PHASE)
1094 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1096 if (!dwarf_eh_find_callsite(context, &lsda, &action))
1098 // EH range not found. This happens if exception is thrown and not
1099 // caught inside a cleanup (destructor). We should call
1100 // terminate() in this case. The catchTemp (landing pad) field of
1101 // exception object will contain null when personality function is
1102 // called with _UA_HANDLER_FRAME action for phase 2 unwinding.
1103 return _URC_HANDLER_FOUND;
1106 handler_type found_handler = check_action_record(context, &lsda,
1107 action.action_record, realEx, &selector, ex->adjustedPtr);
1108 // If there's no action record, we've only found a cleanup, so keep
1109 // searching for something real
1110 if (found_handler == handler_catch)
1112 // Cache the results for the phase 2 unwind, if we found a handler
1113 // and this is not a foreign exception.
1116 saveLandingPad(context, exceptionObject, ex, selector, action.landing_pad);
1117 ex->languageSpecificData = (const char*)lsda_addr;
1118 ex->actionRecord = (const char*)action.action_record;
1119 // ex->adjustedPtr is set when finding the action record.
1121 return _URC_HANDLER_FOUND;
1123 return continueUnwinding(exceptionObject, context);
1127 // If this is a foreign exception, we didn't have anywhere to cache the
1128 // lookup stuff, so we need to do it again. If this is either a forced
1129 // unwind, a foreign exception, or a cleanup, then we just install the
1130 // context for a cleanup.
1131 if (!(actions & _UA_HANDLER_FRAME))
1134 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1135 dwarf_eh_find_callsite(context, &lsda, &action);
1136 if (0 == action.landing_pad) { return continueUnwinding(exceptionObject, context); }
1137 handler_type found_handler = check_action_record(context, &lsda,
1138 action.action_record, realEx, &selector, ex->adjustedPtr);
1139 // Ignore handlers this time.
1140 if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); }
1141 pushCleanupException(exceptionObject, ex);
1143 else if (foreignException)
1145 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1146 dwarf_eh_find_callsite(context, &lsda, &action);
1147 check_action_record(context, &lsda, action.action_record, realEx,
1148 &selector, ex->adjustedPtr);
1150 else if (ex->catchTemp == 0)
1152 // Uncaught exception in cleanup, calling terminate
1157 // Restore the saved info if we saved some last time.
1158 loadLandingPad(context, exceptionObject, ex, &selector, &action.landing_pad);
1160 ex->handlerSwitchValue = 0;
1164 _Unwind_SetIP(context, (unsigned long)action.landing_pad);
1165 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
1166 (unsigned long)exceptionObject);
1167 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), selector);
1169 return _URC_INSTALL_CONTEXT;
1173 * ABI function called when entering a catch statement. The argument is the
1174 * pointer passed out of the personality function. This is always the start of
1175 * the _Unwind_Exception object. The return value for this function is the
1176 * pointer to the caught exception, which is either the adjusted pointer (for
1177 * C++ exceptions) of the unadjusted pointer (for foreign exceptions).
1179 #if __GNUC__ > 3 && __GNUC_MINOR__ > 2
1180 extern "C" void *__cxa_begin_catch(void *e) throw()
1182 extern "C" void *__cxa_begin_catch(void *e)
1185 // We can't call the fast version here, because if the first exception that
1186 // we see is a foreign exception then we won't have called it yet.
1187 __cxa_thread_info *ti = thread_info();
1188 __cxa_eh_globals *globals = &ti->globals;
1189 globals->uncaughtExceptions--;
1190 _Unwind_Exception *exceptionObject = (_Unwind_Exception*)e;
1192 if (isCXXException(exceptionObject->exception_class))
1194 __cxa_exception *ex = exceptionFromPointer(exceptionObject);
1196 if (ex->handlerCount == 0)
1198 // Add this to the front of the list of exceptions being handled
1199 // and increment its handler count so that it won't be deleted
1201 ex->nextException = globals->caughtExceptions;
1202 globals->caughtExceptions = ex;
1205 if (ex->handlerCount < 0)
1207 // Rethrown exception is catched before end of catch block.
1208 // Clear the rethrow flag (make value positive) - we are allowed
1209 // to delete this exception at the end of the catch block, as long
1210 // as it isn't thrown again later.
1222 // __cxa_begin_catch() <- we are here
1225 ex->handlerCount = -ex->handlerCount + 1;
1231 ti->foreign_exception_state = __cxa_thread_info::none;
1233 return ex->adjustedPtr;
1237 // If this is a foreign exception, then we need to be able to
1238 // store it. We can't chain foreign exceptions, so we give up
1239 // if there are already some outstanding ones.
1240 if (globals->caughtExceptions != 0)
1244 globals->caughtExceptions = (__cxa_exception*)exceptionObject;
1245 ti->foreign_exception_state = __cxa_thread_info::caught;
1247 // exceptionObject is the pointer to the _Unwind_Exception within the
1248 // __cxa_exception. The throw object is after this
1249 return ((char*)exceptionObject + sizeof(_Unwind_Exception));
1255 * ABI function called when exiting a catch block. This will free the current
1256 * exception if it is no longer referenced in other catch blocks.
1258 extern "C" void __cxa_end_catch()
1260 // We can call the fast version here because the slow version is called in
1261 // __cxa_throw(), which must have been called before we end a catch block
1262 __cxa_thread_info *ti = thread_info_fast();
1263 __cxa_eh_globals *globals = &ti->globals;
1264 __cxa_exception *ex = globals->caughtExceptions;
1266 assert(0 != ex && "Ending catch when no exception is on the stack!");
1268 if (ti->foreign_exception_state != __cxa_thread_info::none)
1270 globals->caughtExceptions = 0;
1271 if (ti->foreign_exception_state != __cxa_thread_info::rethrown)
1273 _Unwind_Exception *e = (_Unwind_Exception*)ti->globals.caughtExceptions;
1274 e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
1276 ti->foreign_exception_state = __cxa_thread_info::none;
1280 bool deleteException = true;
1282 if (ex->handlerCount < 0)
1284 // exception was rethrown. Exception should not be deleted even if
1285 // handlerCount become zero.
1295 // __cxa_end_catch(); <- we are here
1301 deleteException = false;
1308 if (ex->handlerCount == 0)
1310 globals->caughtExceptions = ex->nextException;
1311 if (deleteException)
1313 releaseException(ex);
1319 * ABI function. Returns the type of the current exception.
1321 extern "C" std::type_info *__cxa_current_exception_type()
1323 __cxa_eh_globals *globals = __cxa_get_globals();
1324 __cxa_exception *ex = globals->caughtExceptions;
1325 return ex ? ex->exceptionType : 0;
1329 * ABI function, called when an exception specification is violated.
1331 * This function does not return.
1333 extern "C" void __cxa_call_unexpected(void*exception)
1335 _Unwind_Exception *exceptionObject = (_Unwind_Exception*)exception;
1336 if (exceptionObject->exception_class == exception_class)
1338 __cxa_exception *ex = exceptionFromPointer(exceptionObject);
1339 if (ex->unexpectedHandler)
1341 ex->unexpectedHandler();
1342 // Should not be reached.
1347 // Should not be reached.
1352 * ABI function, returns the adjusted pointer to the exception object.
1354 extern "C" void *__cxa_get_exception_ptr(void *exceptionObject)
1356 return exceptionFromPointer(exceptionObject)->adjustedPtr;
1360 * As an extension, we provide the ability for the unexpected and terminate
1361 * handlers to be thread-local. We default to the standards-compliant
1362 * behaviour where they are global.
1364 static bool thread_local_handlers = false;
1370 * Sets whether unexpected and terminate handlers should be thread-local.
1372 void set_use_thread_local_handlers(bool flag) throw()
1374 thread_local_handlers = flag;
1377 * Sets a thread-local unexpected handler.
1379 unexpected_handler set_unexpected(unexpected_handler f) throw()
1381 static __cxa_thread_info *info = thread_info();
1382 unexpected_handler old = info->unexpectedHandler;
1383 info->unexpectedHandler = f;
1387 * Sets a thread-local terminate handler.
1389 terminate_handler set_terminate(terminate_handler f) throw()
1391 static __cxa_thread_info *info = thread_info();
1392 terminate_handler old = info->terminateHandler;
1393 info->terminateHandler = f;
1401 * Sets the function that will be called when an exception specification is
1404 unexpected_handler set_unexpected(unexpected_handler f) throw()
1406 if (thread_local_handlers) { return pathscale::set_unexpected(f); }
1408 return ATOMIC_SWAP(&unexpectedHandler, f);
1411 * Sets the function that is called to terminate the program.
1413 terminate_handler set_terminate(terminate_handler f) throw()
1415 if (thread_local_handlers) { return pathscale::set_terminate(f); }
1417 return ATOMIC_SWAP(&terminateHandler, f);
1420 * Terminates the program, calling a custom terminate implementation if
1425 static __cxa_thread_info *info = thread_info();
1426 if (0 != info && 0 != info->terminateHandler)
1428 info->terminateHandler();
1429 // Should not be reached - a terminate handler is not expected to
1436 * Called when an unexpected exception is encountered (i.e. an exception
1437 * violates an exception specification). This calls abort() unless a
1438 * custom handler has been set..
1442 static __cxa_thread_info *info = thread_info();
1443 if (0 != info && 0 != info->unexpectedHandler)
1445 info->unexpectedHandler();
1446 // Should not be reached - a terminate handler is not expected to
1450 unexpectedHandler();
1453 * Returns whether there are any exceptions currently being thrown that
1454 * have not been caught. This can occur inside a nested catch statement.
1456 bool uncaught_exception() throw()
1458 __cxa_thread_info *info = thread_info();
1459 return info->globals.uncaughtExceptions != 0;
1462 * Returns the current unexpected handler.
1464 unexpected_handler get_unexpected() throw()
1466 __cxa_thread_info *info = thread_info();
1467 if (info->unexpectedHandler)
1469 return info->unexpectedHandler;
1471 return ATOMIC_LOAD(&unexpectedHandler);
1474 * Returns the current terminate handler.
1476 terminate_handler get_terminate() throw()
1478 __cxa_thread_info *info = thread_info();
1479 if (info->terminateHandler)
1481 return info->terminateHandler;
1483 return ATOMIC_LOAD(&terminateHandler);
1487 extern "C" _Unwind_Exception *__cxa_get_cleanup(void)
1489 __cxa_thread_info *info = thread_info_fast();
1490 _Unwind_Exception *exceptionObject = info->currentCleanup;
1491 if (isCXXException(exceptionObject->exception_class))
1493 __cxa_exception *ex = exceptionFromPointer(exceptionObject);
1495 if (ex->cleanupCount == 0)
1497 info->currentCleanup = ex->nextCleanup;
1498 ex->nextCleanup = 0;
1503 info->currentCleanup = 0;
1505 return exceptionObject;
1509 ".pushsection .text.__cxa_end_cleanup \n"
1510 ".global __cxa_end_cleanup \n"
1511 ".type __cxa_end_cleanup, \"function\" \n"
1512 "__cxa_end_cleanup: \n"
1513 " push {r1, r2, r3, r4} \n"
1514 " bl __cxa_get_cleanup \n"
1515 " push {r1, r2, r3, r4} \n"
1516 " b _Unwind_Resume \n"