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.
37 #pragma weak pthread_key_create
38 #pragma weak pthread_setspecific
39 #pragma weak pthread_getspecific
40 #pragma weak pthread_once
42 using namespace ABI_NAMESPACE;
45 * Saves the result of the landing pad that we have found. For ARM, this is
46 * stored in the generic unwind structure, while on other platforms it is
47 * stored in the C++ exception.
49 static void saveLandingPad(struct _Unwind_Context *context,
50 struct _Unwind_Exception *ucb,
51 struct __cxa_exception *ex,
53 dw_eh_ptr_t landingPad)
56 // On ARM, we store the saved exception in the generic part of the structure
57 ucb->barrier_cache.sp = _Unwind_GetGR(context, 13);
58 ucb->barrier_cache.bitpattern[1] = (uint32_t)selector;
59 ucb->barrier_cache.bitpattern[3] = (uint32_t)landingPad;
61 // Cache the results for the phase 2 unwind, if we found a handler
62 // and this is not a foreign exception.
65 ex->handlerSwitchValue = selector;
66 ex->catchTemp = landingPad;
71 * Loads the saved landing pad. Returns 1 on success, 0 on failure.
73 static int loadLandingPad(struct _Unwind_Context *context,
74 struct _Unwind_Exception *ucb,
75 struct __cxa_exception *ex,
76 unsigned long *selector,
77 dw_eh_ptr_t *landingPad)
80 *selector = ucb->barrier_cache.bitpattern[1];
81 *landingPad = (dw_eh_ptr_t)ucb->barrier_cache.bitpattern[3];
86 *selector = ex->handlerSwitchValue;
87 *landingPad = (dw_eh_ptr_t)ex->catchTemp;
94 static inline _Unwind_Reason_Code continueUnwinding(struct _Unwind_Exception *ex,
95 struct _Unwind_Context *context)
98 if (__gnu_unwind_frame(ex, context) != _URC_OK) { return _URC_FAILURE; }
100 return _URC_CONTINUE_UNWIND;
104 extern "C" void __cxa_free_exception(void *thrown_exception);
105 extern "C" void __cxa_free_dependent_exception(void *thrown_exception);
106 extern "C" void* __dynamic_cast(const void *sub,
107 const __class_type_info *src,
108 const __class_type_info *dst,
109 ptrdiff_t src2dst_offset);
112 * The type of a handler that has been found.
119 * A cleanup - the exception will propagate through this frame, but code
120 * must be run when this happens.
124 * A catch statement. The exception will not propagate past this frame
125 * (without an explicit rethrow).
131 * Per-thread info required by the runtime. We store a single structure
132 * pointer in thread-local storage, because this tends to be a scarce resource
133 * and it's impolite to steal all of it and not leave any for the rest of the
136 * Instances of this structure are allocated lazily - at most one per thread -
137 * and are destroyed on thread termination.
139 struct __cxa_thread_info
141 /** The termination handler for this thread. */
142 terminate_handler terminateHandler;
143 /** The unexpected exception handler for this thread. */
144 unexpected_handler unexpectedHandler;
146 * The number of emergency buffers held by this thread. This is 0 in
147 * normal operation - the emergency buffers are only used when malloc()
148 * fails to return memory for allocating an exception. Threads are not
149 * permitted to hold more than 4 emergency buffers (as per recommendation
150 * in ABI spec [3.3.1]).
152 int emergencyBuffersHeld;
154 * The exception currently running in a cleanup.
156 _Unwind_Exception *currentCleanup;
158 * The public part of this structure, accessible from outside of this
161 __cxa_eh_globals globals;
164 * Dependent exception. This
166 struct __cxa_dependent_exception
169 void *primaryException;
171 std::type_info *exceptionType;
172 void (*exceptionDestructor) (void *);
173 unexpected_handler unexpectedHandler;
174 terminate_handler terminateHandler;
175 __cxa_exception *nextException;
178 _Unwind_Exception *nextCleanup;
181 int handlerSwitchValue;
182 const char *actionRecord;
183 const char *languageSpecificData;
187 void *primaryException;
189 _Unwind_Exception unwindHeader;
199 virtual ~exception() throw();
200 virtual const char* what() const throw();
205 extern "C" std::type_info *__cxa_current_exception_type();
208 * Class of exceptions to distinguish between this and other exception types.
210 * The first four characters are the vendor ID. Currently, we use GNUC,
211 * because we aim for ABI-compatibility with the GNU implementation, and
212 * various checks may test for equality of the class, which is incorrect.
214 static const uint64_t exception_class =
215 EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\0');
217 * Class used for dependent exceptions.
219 static const uint64_t dependent_exception_class =
220 EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\x01');
222 * The low four bytes of the exception class, indicating that we conform to the
223 * Itanium C++ ABI. This is currently unused, but should be used in the future
224 * if we change our exception class, to allow this library and libsupc++ to be
225 * linked to the same executable and both to interoperate.
227 static const uint32_t abi_exception_class =
228 GENERIC_EXCEPTION_CLASS('C', '+', '+', '\0');
230 static bool isCXXException(uint64_t cls)
232 return (cls == exception_class) || (cls == dependent_exception_class);
235 static bool isDependentException(uint64_t cls)
237 return cls == dependent_exception_class;
240 static __cxa_exception *exceptionFromPointer(void *ex)
242 return (__cxa_exception*)((char*)ex -
243 offsetof(struct __cxa_exception, unwindHeader));
245 static __cxa_exception *realExceptionFromException(__cxa_exception *ex)
247 if (!isDependentException(ex->unwindHeader.exception_class)) { return ex; }
248 return ((__cxa_exception*)(((__cxa_dependent_exception*)ex)->primaryException))-1;
254 // Forward declaration of standard library terminate() function used to
256 void terminate(void);
259 using namespace ABI_NAMESPACE;
263 /** The global termination handler. */
264 static terminate_handler terminateHandler = abort;
265 /** The global unexpected exception handler. */
266 static unexpected_handler unexpectedHandler = std::terminate;
268 /** Key used for thread-local data. */
269 static pthread_key_t eh_key;
273 * Cleanup function, allowing foreign exception handlers to correctly destroy
274 * this exception if they catch it.
276 static void exception_cleanup(_Unwind_Reason_Code reason,
277 struct _Unwind_Exception *ex)
279 __cxa_free_exception((void*)ex);
281 static void dependent_exception_cleanup(_Unwind_Reason_Code reason,
282 struct _Unwind_Exception *ex)
285 __cxa_free_dependent_exception((void*)ex);
289 * Recursively walk a list of exceptions and delete them all in post-order.
291 static void free_exception_list(__cxa_exception *ex)
293 if (0 != ex->nextException)
295 free_exception_list(ex->nextException);
297 // __cxa_free_exception() expects to be passed the thrown object, which
298 // immediately follows the exception, not the exception itself
299 __cxa_free_exception(ex+1);
303 * Cleanup function called when a thread exists to make certain that all of the
304 * per-thread data is deleted.
306 static void thread_cleanup(void* thread_info)
308 __cxa_thread_info *info = (__cxa_thread_info*)thread_info;
309 if (info->globals.caughtExceptions)
311 free_exception_list(info->globals.caughtExceptions);
318 * Once control used to protect the key creation.
320 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
323 * We may not be linked against a full pthread implementation. If we're not,
324 * then we need to fake the thread-local storage by storing 'thread-local'
325 * things in a global.
329 * Thread-local storage for a single-threaded program.
331 static __cxa_thread_info singleThreadInfo;
335 static void init_key(void)
337 if ((0 == pthread_key_create) ||
338 (0 == pthread_setspecific) ||
339 (0 == pthread_getspecific))
344 pthread_key_create(&eh_key, thread_cleanup);
345 pthread_setspecific(eh_key, (void*)0x42);
346 fakeTLS = (pthread_getspecific(eh_key) != (void*)0x42);
347 pthread_setspecific(eh_key, 0);
351 * Returns the thread info structure, creating it if it is not already created.
353 static __cxa_thread_info *thread_info()
355 if ((0 == pthread_once) || pthread_once(&once_control, init_key))
359 if (fakeTLS) { return &singleThreadInfo; }
360 __cxa_thread_info *info = (__cxa_thread_info*)pthread_getspecific(eh_key);
363 info = (__cxa_thread_info*)calloc(1, sizeof(__cxa_thread_info));
364 pthread_setspecific(eh_key, info);
369 * Fast version of thread_info(). May fail if thread_info() is not called on
370 * this thread at least once already.
372 static __cxa_thread_info *thread_info_fast()
374 if (fakeTLS) { return &singleThreadInfo; }
375 return (__cxa_thread_info*)pthread_getspecific(eh_key);
378 * ABI function returning the __cxa_eh_globals structure.
380 extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals(void)
382 return &(thread_info()->globals);
385 * Version of __cxa_get_globals() assuming that __cxa_get_globals() has already
386 * been called at least once by this thread.
388 extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals_fast(void)
390 return &(thread_info_fast()->globals);
394 * An emergency allocation reserved for when malloc fails. This is treated as
395 * 16 buffers of 1KB each.
397 static char emergency_buffer[16384];
399 * Flag indicating whether each buffer is allocated.
401 static bool buffer_allocated[16];
403 * Lock used to protect emergency allocation.
405 static pthread_mutex_t emergency_malloc_lock = PTHREAD_MUTEX_INITIALIZER;
407 * Condition variable used to wait when two threads are both trying to use the
408 * emergency malloc() buffer at once.
410 static pthread_cond_t emergency_malloc_wait = PTHREAD_COND_INITIALIZER;
413 * Allocates size bytes from the emergency allocation mechanism, if possible.
414 * This function will fail if size is over 1KB or if this thread already has 4
415 * emergency buffers. If all emergency buffers are allocated, it will sleep
416 * until one becomes available.
418 static char *emergency_malloc(size_t size)
420 if (size > 1024) { return 0; }
422 __cxa_thread_info *info = thread_info();
423 // Only 4 emergency buffers allowed per thread!
424 if (info->emergencyBuffersHeld > 3) { return 0; }
426 pthread_mutex_lock(&emergency_malloc_lock);
430 // While we were sleeping on the lock, another thread might have free'd
431 // enough memory for us to use, so try the allocation again - no point
432 // using the emergency buffer if there is some real memory that we can
434 void *m = calloc(1, size);
437 pthread_mutex_unlock(&emergency_malloc_lock);
440 for (int i=0 ; i<16 ; i++)
442 if (!buffer_allocated[i])
445 buffer_allocated[i] = true;
449 // If there still isn't a buffer available, then sleep on the condition
450 // variable. This will be signalled when another thread releases one
451 // of the emergency buffers.
454 pthread_cond_wait(&emergency_malloc_wait, &emergency_malloc_lock);
457 pthread_mutex_unlock(&emergency_malloc_lock);
458 info->emergencyBuffersHeld++;
459 return emergency_buffer + (1024 * buffer);
463 * Frees a buffer returned by emergency_malloc().
465 * Note: Neither this nor emergency_malloc() is particularly efficient. This
466 * should not matter, because neither will be called in normal operation - they
467 * are only used when the program runs out of memory, which should not happen
470 static void emergency_malloc_free(char *ptr)
473 // Find the buffer corresponding to this pointer.
474 for (int i=0 ; i<16 ; i++)
476 if (ptr == (void*)(emergency_buffer + (1024 * i)))
483 "Trying to free something that is not an emergency buffer!");
484 // emergency_malloc() is expected to return 0-initialized data. We don't
485 // zero the buffer when allocating it, because the static buffers will
486 // begin life containing 0 values.
487 memset((void*)ptr, 0, 1024);
488 // Signal the condition variable to wake up any threads that are blocking
489 // waiting for some space in the emergency buffer
490 pthread_mutex_lock(&emergency_malloc_lock);
491 // In theory, we don't need to do this with the lock held. In practice,
492 // our array of bools will probably be updated using 32-bit or 64-bit
493 // memory operations, so this update may clobber adjacent values.
494 buffer_allocated[buffer] = false;
495 pthread_cond_signal(&emergency_malloc_wait);
496 pthread_mutex_unlock(&emergency_malloc_lock);
499 static char *alloc_or_die(size_t size)
501 char *buffer = (char*)calloc(1, size);
503 // If calloc() doesn't want to give us any memory, try using an emergency
507 buffer = emergency_malloc(size);
508 // This is only reached if the allocation is greater than 1KB, and
509 // anyone throwing objects that big really should know better.
512 fprintf(stderr, "Out of memory attempting to allocate exception\n");
518 static void free_exception(char *e)
520 // If this allocation is within the address range of the emergency buffer,
521 // don't call free() because it was not allocated with malloc()
522 if ((e > emergency_buffer) &&
523 (e < (emergency_buffer + sizeof(emergency_buffer))))
525 emergency_malloc_free(e);
534 * Allocates an exception structure. Returns a pointer to the space that can
535 * be used to store an object of thrown_size bytes. This function will use an
536 * emergency buffer if malloc() fails, and may block if there are no such
539 extern "C" void *__cxa_allocate_exception(size_t thrown_size)
541 size_t size = thrown_size + sizeof(__cxa_exception);
542 char *buffer = alloc_or_die(size);
543 return buffer+sizeof(__cxa_exception);
546 extern "C" void *__cxa_allocate_dependent_exception(void)
548 size_t size = sizeof(__cxa_dependent_exception);
549 char *buffer = alloc_or_die(size);
550 return buffer+sizeof(__cxa_dependent_exception);
554 * __cxa_free_exception() is called when an exception was thrown in between
555 * calling __cxa_allocate_exception() and actually throwing the exception.
556 * This happens when the object's copy constructor throws an exception.
558 * In this implementation, it is also called by __cxa_end_catch() and during
561 extern "C" void __cxa_free_exception(void *thrown_exception)
563 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
564 // Free the object that was thrown, calling its destructor
565 if (0 != ex->exceptionDestructor)
569 ex->exceptionDestructor(thrown_exception);
573 // FIXME: Check that this is really what the spec says to do.
578 free_exception((char*)ex);
581 static void releaseException(__cxa_exception *exception)
583 if (isDependentException(exception->unwindHeader.exception_class))
585 __cxa_free_dependent_exception(exception+1);
588 if (__sync_sub_and_fetch(&exception->referenceCount, 1) == 0)
590 // __cxa_free_exception() expects to be passed the thrown object,
591 // which immediately follows the exception, not the exception
593 __cxa_free_exception(exception+1);
597 void __cxa_free_dependent_exception(void *thrown_exception)
599 __cxa_dependent_exception *ex = ((__cxa_dependent_exception*)thrown_exception) - 1;
600 assert(isDependentException(ex->unwindHeader.exception_class));
601 if (ex->primaryException)
603 releaseException(realExceptionFromException((__cxa_exception*)ex));
605 free_exception((char*)ex);
609 * Callback function used with _Unwind_Backtrace().
611 * Prints a stack trace. Used only for debugging help.
613 * Note: As of FreeBSD 8.1, dladd() still doesn't work properly, so this only
614 * correctly prints function names from public, relocatable, symbols.
616 static _Unwind_Reason_Code trace(struct _Unwind_Context *context, void *c)
620 dladdr((void*)(uintptr_t)__cxa_current_exception_type, &myinfo);
621 void *ip = (void*)_Unwind_GetIP(context);
623 if (dladdr(ip, &info) != 0)
625 if (mylookup == 0 || strcmp(info.dli_fname, myinfo.dli_fname) != 0)
627 printf("%p:%s() in %s\n", ip, info.dli_sname, info.dli_fname);
630 return _URC_CONTINUE_UNWIND;
634 * Report a failure that occurred when attempting to throw an exception.
636 * If the failure happened by falling off the end of the stack without finding
637 * a handler, prints a back trace before aborting.
639 static void report_failure(_Unwind_Reason_Code err, __cxa_exception *thrown_exception)
644 case _URC_FATAL_PHASE1_ERROR:
645 fprintf(stderr, "Fatal error during phase 1 unwinding\n");
648 case _URC_FATAL_PHASE2_ERROR:
649 fprintf(stderr, "Fatal error during phase 2 unwinding\n");
652 case _URC_END_OF_STACK:
653 fprintf(stderr, "Terminating due to uncaught exception %p",
654 (void*)thrown_exception);
655 thrown_exception = realExceptionFromException(thrown_exception);
656 static const __class_type_info *e_ti =
657 static_cast<const __class_type_info*>(&typeid(std::exception));
658 const __class_type_info *throw_ti =
659 dynamic_cast<const __class_type_info*>(thrown_exception->exceptionType);
663 (std::exception*)e_ti->cast_to((void*)(thrown_exception+1),
667 fprintf(stderr, " '%s'", e->what());
671 size_t bufferSize = 128;
672 char *demangled = (char*)malloc(bufferSize);
673 const char *mangled = thrown_exception->exceptionType->name();
675 demangled = __cxa_demangle(mangled, demangled, &bufferSize, &status);
676 fprintf(stderr, " of type %s\n",
677 status == 0 ? (const char*)demangled : mangled);
678 if (status == 0) { free(demangled); }
679 // Print a back trace if no handler is found.
680 // TODO: Make this optional
681 _Unwind_Backtrace(trace, 0);
687 static void throw_exception(__cxa_exception *ex)
689 __cxa_thread_info *info = thread_info();
690 ex->unexpectedHandler = info->unexpectedHandler;
691 if (0 == ex->unexpectedHandler)
693 ex->unexpectedHandler = unexpectedHandler;
695 ex->terminateHandler = info->terminateHandler;
696 if (0 == ex->terminateHandler)
698 ex->terminateHandler = terminateHandler;
700 info->globals.uncaughtExceptions++;
702 _Unwind_Reason_Code err = _Unwind_RaiseException(&ex->unwindHeader);
703 // The _Unwind_RaiseException() function should not return, it should
704 // unwind the stack past this function. If it does return, then something
706 report_failure(err, ex);
711 * ABI function for throwing an exception. Takes the object to be thrown (the
712 * pointer returned by __cxa_allocate_exception()), the type info for the
713 * pointee, and the destructor (if there is one) as arguments.
715 extern "C" void __cxa_throw(void *thrown_exception,
716 std::type_info *tinfo,
719 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
721 ex->referenceCount = 1;
722 ex->exceptionType = tinfo;
724 ex->exceptionDestructor = dest;
726 ex->unwindHeader.exception_class = exception_class;
727 ex->unwindHeader.exception_cleanup = exception_cleanup;
732 extern "C" void __cxa_rethrow_primary_exception(void* thrown_exception)
734 if (NULL == thrown_exception) { return; }
736 __cxa_exception *original = exceptionFromPointer(thrown_exception);
737 __cxa_dependent_exception *ex = ((__cxa_dependent_exception*)__cxa_allocate_dependent_exception())-1;
739 ex->primaryException = thrown_exception;
740 __cxa_increment_exception_refcount(thrown_exception);
742 ex->exceptionType = original->exceptionType;
743 ex->unwindHeader.exception_class = dependent_exception_class;
744 ex->unwindHeader.exception_cleanup = dependent_exception_cleanup;
746 throw_exception((__cxa_exception*)ex);
749 extern "C" void *__cxa_current_primary_exception(void)
751 __cxa_eh_globals* globals = __cxa_get_globals();
752 __cxa_exception *ex = globals->caughtExceptions;
754 if (0 == ex) { return NULL; }
755 ex = realExceptionFromException(ex);
756 __sync_fetch_and_add(&ex->referenceCount, 1);
760 extern "C" void __cxa_increment_exception_refcount(void* thrown_exception)
762 if (NULL == thrown_exception) { return; }
763 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
764 if (isDependentException(ex->unwindHeader.exception_class)) { return; }
765 __sync_fetch_and_add(&ex->referenceCount, 1);
767 extern "C" void __cxa_decrement_exception_refcount(void* thrown_exception)
769 if (NULL == thrown_exception) { return; }
770 __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
771 releaseException(ex);
775 * ABI function. Rethrows the current exception. Does not remove the
776 * exception from the stack or decrement its handler count - the compiler is
777 * expected to set the landing pad for this function to the end of the catch
778 * block, and then call _Unwind_Resume() to continue unwinding once
779 * __cxa_end_catch() has been called and any cleanup code has been run.
781 extern "C" void __cxa_rethrow()
783 __cxa_eh_globals *globals = __cxa_get_globals();
784 // Note: We don't remove this from the caught list here, because
785 // __cxa_end_catch will be called when we unwind out of the try block. We
786 // could probably make this faster by providing an alternative rethrow
787 // function and ensuring that all cleanup code is run before calling it, so
788 // we can skip the top stack frame when unwinding.
789 __cxa_exception *ex = globals->caughtExceptions;
794 "Attempting to rethrow an exception that doesn't exist!\n");
798 assert(ex->handlerCount > 0 && "Rethrowing uncaught exception!");
800 // ex->handlerCount will be decremented in __cxa_end_catch in enclosing
803 // Make handler count negative. This will tell __cxa_end_catch that
804 // exception was rethrown and exception object should not be destroyed
805 // when handler count become zero
806 ex->handlerCount = -ex->handlerCount;
808 // Continue unwinding the stack with this exception. This should unwind to
809 // the place in the caller where __cxa_end_catch() is called. The caller
810 // will then run cleanup code and bounce the exception back with
812 _Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(&ex->unwindHeader);
813 report_failure(err, ex);
817 * Returns the type_info object corresponding to the filter.
819 static std::type_info *get_type_info_entry(_Unwind_Context *context,
823 // Get the address of the record in the table.
824 dw_eh_ptr_t record = lsda->type_table -
825 dwarf_size_of_fixed_size_field(lsda->type_table_encoding)*filter;
827 dw_eh_ptr_t start = record;
828 // Read the value, but it's probably an indirect reference...
829 int64_t offset = read_value(lsda->type_table_encoding, &record);
831 // (If the entry is 0, don't try to dereference it. That would be bad.)
832 if (offset == 0) { return 0; }
834 // ...so we need to resolve it
835 return (std::type_info*)resolve_indirect_value(context,
836 lsda->type_table_encoding, offset, start);
842 * Checks the type signature found in a handler against the type of the thrown
843 * object. If ex is 0 then it is assumed to be a foreign exception and only
846 static bool check_type_signature(__cxa_exception *ex,
847 const std::type_info *type,
850 void *exception_ptr = (void*)(ex+1);
851 const std::type_info *ex_type = ex->exceptionType;
853 bool is_ptr = ex_type->__is_pointer_p();
856 exception_ptr = *(void**)exception_ptr;
858 // Always match a catchall, even with a foreign exception
860 // Note: A 0 here is a catchall, not a cleanup, so we return true to
861 // indicate that we found a catch.
866 adjustedPtr = exception_ptr;
871 if (0 == ex) { return false; }
873 // If the types are the same, no casting is needed.
874 if (*type == *ex_type)
876 adjustedPtr = exception_ptr;
881 if (type->__do_catch(ex_type, &exception_ptr, 1))
883 adjustedPtr = exception_ptr;
890 * Checks whether the exception matches the type specifiers in this action
891 * record. If the exception only matches cleanups, then this returns false.
892 * If it matches a catch (including a catchall) then it returns true.
894 * The selector argument is used to return the selector that is passed in the
895 * second exception register when installing the context.
897 static handler_type check_action_record(_Unwind_Context *context,
899 dw_eh_ptr_t action_record,
901 unsigned long *selector,
904 if (!action_record) { return handler_cleanup; }
905 handler_type found = handler_none;
906 while (action_record)
908 int filter = read_sleb128(&action_record);
909 dw_eh_ptr_t action_record_offset_base = action_record;
910 int displacement = read_sleb128(&action_record);
911 action_record = displacement ?
912 action_record_offset_base + displacement : 0;
913 // We only check handler types for C++ exceptions - foreign exceptions
914 // are only allowed for cleanup.
915 if (filter > 0 && 0 != ex)
917 std::type_info *handler_type = get_type_info_entry(context, lsda, filter);
918 if (check_type_signature(ex, handler_type, adjustedPtr))
921 return handler_catch;
924 else if (filter < 0 && 0 != ex)
926 bool matched = false;
930 std::type_info *handler_type = get_type_info_entry(context, lsda, filter--);
933 if (check_type_signature(ex, handler_type, adjustedPtr))
938 handler_type = get_type_info_entry(context, lsda, filter--);
941 unsigned char *type_index = ((unsigned char*)lsda->type_table - filter - 1);
944 std::type_info *handler_type = get_type_info_entry(context, lsda, *(type_index++));
945 // If the exception spec matches a permitted throw type for
946 // this function, don't report a handler - we are allowed to
947 // propagate this exception out.
948 if (check_type_signature(ex, handler_type, adjustedPtr))
955 if (matched) { continue; }
956 // If we don't find an allowed exception spec, we need to install
957 // the context for this action. The landing pad will then call the
958 // unexpected exception function. Treat this as a catch
959 return handler_catch;
961 else if (filter == 0)
964 found = handler_cleanup;
970 static void pushCleanupException(_Unwind_Exception *exceptionObject,
974 __cxa_thread_info *info = thread_info_fast();
978 if (ex->cleanupCount > 1)
980 assert(exceptionObject == info->currentCleanup);
983 ex->nextCleanup = info->currentCleanup;
985 info->currentCleanup = exceptionObject;
990 * The exception personality function. This is referenced in the unwinding
991 * DWARF metadata and is called by the unwind library for each C++ stack frame
992 * containing catch or cleanup code.
995 BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0)
996 // This personality function is for version 1 of the ABI. If you use it
997 // with a future version of the ABI, it won't know what to do, so it
998 // reports a fatal error and give up before it breaks anything.
1001 return _URC_FATAL_PHASE1_ERROR;
1003 __cxa_exception *ex = 0;
1004 __cxa_exception *realEx = 0;
1006 // If this exception is throw by something else then we can't make any
1007 // assumptions about its layout beyond the fields declared in
1008 // _Unwind_Exception.
1009 bool foreignException = !isCXXException(exceptionClass);
1011 // If this isn't a foreign exception, then we have a C++ exception structure
1012 if (!foreignException)
1014 ex = exceptionFromPointer(exceptionObject);
1015 realEx = realExceptionFromException(ex);
1018 unsigned char *lsda_addr =
1019 (unsigned char*)_Unwind_GetLanguageSpecificData(context);
1021 // No LSDA implies no landing pads - try the next frame
1022 if (0 == lsda_addr) { return continueUnwinding(exceptionObject, context); }
1024 // These two variables define how the exception will be handled.
1025 dwarf_eh_action action = {0};
1026 unsigned long selector = 0;
1028 // During the search phase, we do a complete lookup. If we return
1029 // _URC_HANDLER_FOUND, then the phase 2 unwind will call this function with
1030 // a _UA_HANDLER_FRAME action, telling us to install the handler frame. If
1031 // we return _URC_CONTINUE_UNWIND, we may be called again later with a
1032 // _UA_CLEANUP_PHASE action for this frame.
1034 // The point of the two-stage unwind allows us to entirely avoid any stack
1035 // unwinding if there is no handler. If there are just cleanups found,
1036 // then we can just panic call an abort function.
1038 // Matching a handler is much more expensive than matching a cleanup,
1039 // because we don't need to bother doing type comparisons (or looking at
1040 // the type table at all) for a cleanup. This means that there is no need
1041 // to cache the result of finding a cleanup, because it's (quite) quick to
1042 // look it up again from the action table.
1043 if (actions & _UA_SEARCH_PHASE)
1045 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1047 if (!dwarf_eh_find_callsite(context, &lsda, &action))
1049 // EH range not found. This happens if exception is thrown and not
1050 // caught inside a cleanup (destructor). We should call
1051 // terminate() in this case. The catchTemp (landing pad) field of
1052 // exception object will contain null when personality function is
1053 // called with _UA_HANDLER_FRAME action for phase 2 unwinding.
1054 return _URC_HANDLER_FOUND;
1057 handler_type found_handler = check_action_record(context, &lsda,
1058 action.action_record, realEx, &selector, ex->adjustedPtr);
1059 // If there's no action record, we've only found a cleanup, so keep
1060 // searching for something real
1061 if (found_handler == handler_catch)
1063 // Cache the results for the phase 2 unwind, if we found a handler
1064 // and this is not a foreign exception.
1067 saveLandingPad(context, exceptionObject, ex, selector, action.landing_pad);
1068 ex->languageSpecificData = (const char*)lsda_addr;
1069 ex->actionRecord = (const char*)action.action_record;
1070 // ex->adjustedPtr is set when finding the action record.
1072 return _URC_HANDLER_FOUND;
1074 return continueUnwinding(exceptionObject, context);
1078 // If this is a foreign exception, we didn't have anywhere to cache the
1079 // lookup stuff, so we need to do it again. If this is either a forced
1080 // unwind, a foreign exception, or a cleanup, then we just install the
1081 // context for a cleanup.
1082 if (!(actions & _UA_HANDLER_FRAME))
1085 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1086 dwarf_eh_find_callsite(context, &lsda, &action);
1087 if (0 == action.landing_pad) { return continueUnwinding(exceptionObject, context); }
1088 handler_type found_handler = check_action_record(context, &lsda,
1089 action.action_record, realEx, &selector, ex->adjustedPtr);
1090 // Ignore handlers this time.
1091 if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); }
1092 pushCleanupException(exceptionObject, ex);
1094 else if (foreignException)
1096 struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1097 dwarf_eh_find_callsite(context, &lsda, &action);
1098 check_action_record(context, &lsda, action.action_record, realEx,
1099 &selector, ex->adjustedPtr);
1101 else if (ex->catchTemp == 0)
1103 // Uncaught exception in cleanup, calling terminate
1108 // Restore the saved info if we saved some last time.
1109 loadLandingPad(context, exceptionObject, ex, &selector, &action.landing_pad);
1111 ex->handlerSwitchValue = 0;
1115 _Unwind_SetIP(context, (unsigned long)action.landing_pad);
1116 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
1117 (unsigned long)exceptionObject);
1118 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), selector);
1120 return _URC_INSTALL_CONTEXT;
1124 * ABI function called when entering a catch statement. The argument is the
1125 * pointer passed out of the personality function. This is always the start of
1126 * the _Unwind_Exception object. The return value for this function is the
1127 * pointer to the caught exception, which is either the adjusted pointer (for
1128 * C++ exceptions) of the unadjusted pointer (for foreign exceptions).
1130 #if __GNUC__ > 3 && __GNUC_MINOR__ > 2
1131 extern "C" void *__cxa_begin_catch(void *e) throw()
1133 extern "C" void *__cxa_begin_catch(void *e)
1136 // Decrement the uncaught exceptions count
1137 __cxa_eh_globals *globals = __cxa_get_globals();
1138 globals->uncaughtExceptions--;
1139 _Unwind_Exception *exceptionObject = (_Unwind_Exception*)e;
1141 if (isCXXException(exceptionObject->exception_class))
1143 __cxa_exception *ex = exceptionFromPointer(exceptionObject);
1145 if (ex->handlerCount == 0)
1147 // Add this to the front of the list of exceptions being handled
1148 // and increment its handler count so that it won't be deleted
1150 ex->nextException = globals->caughtExceptions;
1151 globals->caughtExceptions = ex;
1154 if (ex->handlerCount < 0)
1156 // Rethrown exception is catched before end of catch block.
1157 // Clear the rethrow flag (make value positive) - we are allowed
1158 // to delete this exception at the end of the catch block, as long
1159 // as it isn't thrown again later.
1171 // __cxa_begin_catch() <- we are here
1174 ex->handlerCount = -ex->handlerCount + 1;
1181 return ex->adjustedPtr;
1183 // exceptionObject is the pointer to the _Unwind_Exception within the
1184 // __cxa_exception. The throw object is after this
1185 return ((char*)exceptionObject + sizeof(_Unwind_Exception));
1191 * ABI function called when exiting a catch block. This will free the current
1192 * exception if it is no longer referenced in other catch blocks.
1194 extern "C" void __cxa_end_catch()
1196 // We can call the fast version here because the slow version is called in
1197 // __cxa_throw(), which must have been called before we end a catch block
1198 __cxa_eh_globals *globals = __cxa_get_globals_fast();
1199 __cxa_exception *ex = globals->caughtExceptions;
1201 assert(0 != ex && "Ending catch when no exception is on the stack!");
1203 bool deleteException = true;
1205 if (ex->handlerCount < 0)
1207 // exception was rethrown. Exception should not be deleted even if
1208 // handlerCount become zero.
1218 // __cxa_end_catch(); <- we are here
1224 deleteException = false;
1231 if (ex->handlerCount == 0)
1233 globals->caughtExceptions = ex->nextException;
1234 if (deleteException)
1236 releaseException(ex);
1242 * ABI function. Returns the type of the current exception.
1244 extern "C" std::type_info *__cxa_current_exception_type()
1246 __cxa_eh_globals *globals = __cxa_get_globals();
1247 __cxa_exception *ex = globals->caughtExceptions;
1248 return ex ? ex->exceptionType : 0;
1252 * ABI function, called when an exception specification is violated.
1254 * This function does not return.
1256 extern "C" void __cxa_call_unexpected(void*exception)
1258 _Unwind_Exception *exceptionObject = (_Unwind_Exception*)exception;
1259 if (exceptionObject->exception_class == exception_class)
1261 __cxa_exception *ex = exceptionFromPointer(exceptionObject);
1262 if (ex->unexpectedHandler)
1264 ex->unexpectedHandler();
1265 // Should not be reached.
1270 // Should not be reached.
1275 * ABI function, returns the adjusted pointer to the exception object.
1277 extern "C" void *__cxa_get_exception_ptr(void *exceptionObject)
1279 return exceptionFromPointer(exceptionObject)->adjustedPtr;
1283 * As an extension, we provide the ability for the unexpected and terminate
1284 * handlers to be thread-local. We default to the standards-compliant
1285 * behaviour where they are global.
1287 static bool thread_local_handlers = false;
1293 * Sets whether unexpected and terminate handlers should be thread-local.
1295 void set_use_thread_local_handlers(bool flag) throw()
1297 thread_local_handlers = flag;
1300 * Sets a thread-local unexpected handler.
1302 unexpected_handler set_unexpected(unexpected_handler f) throw()
1304 static __cxa_thread_info *info = thread_info();
1305 unexpected_handler old = info->unexpectedHandler;
1306 info->unexpectedHandler = f;
1310 * Sets a thread-local terminate handler.
1312 terminate_handler set_terminate(terminate_handler f) throw()
1314 static __cxa_thread_info *info = thread_info();
1315 terminate_handler old = info->terminateHandler;
1316 info->terminateHandler = f;
1324 * Sets the function that will be called when an exception specification is
1327 unexpected_handler set_unexpected(unexpected_handler f) throw()
1329 if (thread_local_handlers) { return pathscale::set_unexpected(f); }
1331 return __sync_lock_test_and_set(&unexpectedHandler, f);
1334 * Sets the function that is called to terminate the program.
1336 terminate_handler set_terminate(terminate_handler f) throw()
1338 if (thread_local_handlers) { return pathscale::set_terminate(f); }
1339 return __sync_lock_test_and_set(&terminateHandler, f);
1342 * Terminates the program, calling a custom terminate implementation if
1347 static __cxa_thread_info *info = thread_info_fast();
1348 if (0 != info && 0 != info->terminateHandler)
1350 info->terminateHandler();
1351 // Should not be reached - a terminate handler is not expected to
1358 * Called when an unexpected exception is encountered (i.e. an exception
1359 * violates an exception specification). This calls abort() unless a
1360 * custom handler has been set..
1364 static __cxa_thread_info *info = thread_info_fast();
1365 if (0 != info && 0 != info->unexpectedHandler)
1367 info->unexpectedHandler();
1368 // Should not be reached - a terminate handler is not expected to
1372 unexpectedHandler();
1375 * Returns whether there are any exceptions currently being thrown that
1376 * have not been caught. This can occur inside a nested catch statement.
1378 bool uncaught_exception() throw()
1380 __cxa_thread_info *info = thread_info();
1381 return info->globals.uncaughtExceptions != 0;
1384 * Returns the current unexpected handler.
1386 unexpected_handler get_unexpected() throw()
1388 __cxa_thread_info *info = thread_info();
1389 if (info->unexpectedHandler)
1391 return info->unexpectedHandler;
1393 return unexpectedHandler;
1396 * Returns the current terminate handler.
1398 terminate_handler get_terminate() throw()
1400 __cxa_thread_info *info = thread_info();
1401 if (info->terminateHandler)
1403 return info->terminateHandler;
1405 return terminateHandler;
1409 extern "C" _Unwind_Exception *__cxa_get_cleanup(void)
1411 __cxa_thread_info *info = thread_info_fast();
1412 _Unwind_Exception *exceptionObject = info->currentCleanup;
1413 if (isCXXException(exceptionObject->exception_class))
1415 __cxa_exception *ex = exceptionFromPointer(exceptionObject);
1417 if (ex->cleanupCount == 0)
1419 info->currentCleanup = ex->nextCleanup;
1420 ex->nextCleanup = 0;
1425 info->currentCleanup = 0;
1427 return exceptionObject;
1431 ".pushsection .text.__cxa_end_cleanup \n"
1432 ".global __cxa_end_cleanup \n"
1433 ".type __cxa_end_cleanup, \"function\" \n"
1434 "__cxa_end_cleanup: \n"
1435 " push {r1, r2, r3, r4} \n"
1436 " bl __cxa_get_cleanup \n"
1437 " push {r1, r2, r3, r4} \n"
1438 " b _Unwind_Resume \n"