Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 1 | //===--------------------- cxa_exception_storage.cpp ----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | // |
| 9 | // This file implements the storage for the "Caught Exception Stack" |
Jonathan Roelofs | c82e02d | 2014-02-12 04:49:09 +0000 | [diff] [blame] | 10 | // http://mentorembedded.github.io/cxx-abi/abi-eh.html (section 2.2.2) |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "cxa_exception.hpp" |
| 15 | |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 16 | #include <__threading_support> |
Jonathan Roelofs | c285efa | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 17 | |
Asiri Rathnayake | b62a4dd | 2016-09-21 09:09:32 +0000 | [diff] [blame] | 18 | #if defined(_LIBCXXABI_HAS_NO_THREADS) |
Jonathan Roelofs | c285efa | 2014-05-06 21:30:56 +0000 | [diff] [blame] | 19 | |
| 20 | namespace __cxxabiv1 { |
| 21 | extern "C" { |
| 22 | static __cxa_eh_globals eh_globals; |
| 23 | __cxa_eh_globals *__cxa_get_globals() { return &eh_globals; } |
| 24 | __cxa_eh_globals *__cxa_get_globals_fast() { return &eh_globals; } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | #elif defined(HAS_THREAD_LOCAL) |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 29 | |
| 30 | namespace __cxxabiv1 { |
| 31 | |
| 32 | namespace { |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 33 | __cxa_eh_globals * __globals () { |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 34 | static thread_local __cxa_eh_globals eh_globals; |
| 35 | return &eh_globals; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | extern "C" { |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 40 | __cxa_eh_globals * __cxa_get_globals () { return __globals (); } |
| 41 | __cxa_eh_globals * __cxa_get_globals_fast () { return __globals (); } |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | #else |
| 46 | |
Nick Kledzik | f4429fe | 2011-08-02 01:34:26 +0000 | [diff] [blame] | 47 | #include "abort_message.h" |
Igor Kudrin | ace6572 | 2016-10-07 08:48:28 +0000 | [diff] [blame] | 48 | #include "fallback_malloc.h" |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 49 | |
Asiri Rathnayake | 5180673 | 2016-10-13 15:05:19 +0000 | [diff] [blame] | 50 | // In general, we treat all threading errors as fatal. |
Nick Kledzik | f4429fe | 2011-08-02 01:34:26 +0000 | [diff] [blame] | 51 | // We cannot call std::terminate() because that will in turn |
| 52 | // call __cxa_get_globals() and cause infinite recursion. |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 53 | |
| 54 | namespace __cxxabiv1 { |
| 55 | namespace { |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 56 | std::__libcpp_tls_key key_; |
| 57 | std::__libcpp_exec_once_flag flag_ = _LIBCPP_EXEC_ONCE_INITIALIZER; |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 58 | |
Shoaib Meenai | eb5fa06 | 2017-08-08 00:54:33 +0000 | [diff] [blame] | 59 | void _LIBCPP_TLS_DESTRUCTOR_CC destruct_ (void *p) { |
Igor Kudrin | ace6572 | 2016-10-07 08:48:28 +0000 | [diff] [blame] | 60 | __free_with_fallback ( p ); |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 61 | if ( 0 != std::__libcpp_tls_set ( key_, NULL ) ) |
Nick Kledzik | f4429fe | 2011-08-02 01:34:26 +0000 | [diff] [blame] | 62 | abort_message("cannot zero out thread value for __cxa_get_globals()"); |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 65 | void construct_ () { |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 66 | if ( 0 != std::__libcpp_tls_create ( &key_, destruct_ ) ) |
Asiri Rathnayake | 5180673 | 2016-10-13 15:05:19 +0000 | [diff] [blame] | 67 | abort_message("cannot create thread specific key for __cxa_get_globals()"); |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | extern "C" { |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 72 | __cxa_eh_globals * __cxa_get_globals () { |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 73 | // Try to get the globals for this thread |
| 74 | __cxa_eh_globals* retVal = __cxa_get_globals_fast (); |
| 75 | |
| 76 | // If this is the first time we've been asked for these globals, create them |
| 77 | if ( NULL == retVal ) { |
| 78 | retVal = static_cast<__cxa_eh_globals*> |
Igor Kudrin | ace6572 | 2016-10-07 08:48:28 +0000 | [diff] [blame] | 79 | (__calloc_with_fallback (1, sizeof (__cxa_eh_globals))); |
Nick Kledzik | f4429fe | 2011-08-02 01:34:26 +0000 | [diff] [blame] | 80 | if ( NULL == retVal ) |
| 81 | abort_message("cannot allocate __cxa_eh_globals"); |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 82 | if ( 0 != std::__libcpp_tls_set ( key_, retVal ) ) |
| 83 | abort_message("std::__libcpp_tls_set failure in __cxa_get_globals()"); |
Nick Kledzik | f4429fe | 2011-08-02 01:34:26 +0000 | [diff] [blame] | 84 | } |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 85 | return retVal; |
| 86 | } |
| 87 | |
Howard Hinnant | f6d4cba | 2012-01-25 19:19:13 +0000 | [diff] [blame] | 88 | // Note that this implementation will reliably return NULL if not |
Howard Hinnant | 6d00fef | 2013-02-15 15:48:49 +0000 | [diff] [blame] | 89 | // preceded by a call to __cxa_get_globals(). This is an extension |
Howard Hinnant | f6d4cba | 2012-01-25 19:19:13 +0000 | [diff] [blame] | 90 | // to the Itanium ABI and is taken advantage of in several places in |
| 91 | // libc++abi. |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 92 | __cxa_eh_globals * __cxa_get_globals_fast () { |
Howard Hinnant | 928afcd | 2012-01-23 23:55:58 +0000 | [diff] [blame] | 93 | // First time through, create the key. |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 94 | if (0 != std::__libcpp_execute_once(&flag_, construct_)) |
Asiri Rathnayake | 5180673 | 2016-10-13 15:05:19 +0000 | [diff] [blame] | 95 | abort_message("execute once failure in __cxa_get_globals_fast()"); |
Howard Hinnant | 7da9b96 | 2012-01-30 16:07:00 +0000 | [diff] [blame] | 96 | // static int init = construct_(); |
Asiri Rathnayake | 71ba287 | 2017-01-03 12:58:34 +0000 | [diff] [blame] | 97 | return static_cast<__cxa_eh_globals*>(std::__libcpp_tls_get(key_)); |
Marshall Clow | 61b898e | 2011-07-20 14:53:53 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } |
| 101 | } |
| 102 | #endif |