From ab4a00983ac953b1eeb7ae644530a94cd1d7c5f0 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Wed, 23 Nov 2011 11:10:17 +0000 Subject: Added unwind headers from libobjc2. --- src/unwind-arm.h | 197 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/unwind-itanium.h | 170 ++++++++++++++++++++++++++++++++++++++++++++ src/unwind.h | 5 ++ 3 files changed, 372 insertions(+) create mode 100644 src/unwind-arm.h create mode 100644 src/unwind-itanium.h create mode 100644 src/unwind.h diff --git a/src/unwind-arm.h b/src/unwind-arm.h new file mode 100644 index 0000000..e041cbf --- /dev/null +++ b/src/unwind-arm.h @@ -0,0 +1,197 @@ +/** + * ARM-specific unwind definitions. These are taken from the ARM EHABI + * specification. + */ + typedef enum +{ + _URC_OK = 0, /* operation completed successfully */ + _URC_FOREIGN_EXCEPTION_CAUGHT = 1, + _URC_END_OF_STACK = 5, + _URC_HANDLER_FOUND = 6, + _URC_INSTALL_CONTEXT = 7, + _URC_CONTINUE_UNWIND = 8, + _URC_FAILURE = 9, /* unspecified failure of some kind */ + _URC_FATAL_PHASE1_ERROR = _URC_FAILURE +} _Unwind_Reason_Code; + +typedef uint32_t _Unwind_State; +#ifdef __clang__ +static const _Unwind_State _US_VIRTUAL_UNWIND_FRAME = 0; +static const _Unwind_State _US_UNWIND_FRAME_STARTING = 1; +static const _Unwind_State _US_UNWIND_FRAME_RESUME = 2; +#else // GCC fails at knowing what a constant expression is +# define _US_VIRTUAL_UNWIND_FRAME 0 +# define _US_UNWIND_FRAME_STARTING 1 +# define _US_UNWIND_FRAME_RESUME 2 +#endif + +typedef struct _Unwind_Context _Unwind_Context; + +typedef uint32_t _Unwind_EHT_Header; + +struct _Unwind_Exception +{ + uint64_t exception_class; + void (*exception_cleanup)(_Unwind_Reason_Code, struct _Unwind_Exception *); + /* Unwinder cache, private fields for the unwinder's use */ + struct + { + uint32_t reserved1; + uint32_t reserved2; + uint32_t reserved3; + uint32_t reserved4; + uint32_t reserved5; + /* init reserved1 to 0, then don't touch */ + } unwinder_cache; + /* Propagation barrier cache (valid after phase 1): */ + struct + { + uint32_t sp; + uint32_t bitpattern[5]; + } barrier_cache; + /* Cleanup cache (preserved over cleanup): */ + struct + { + uint32_t bitpattern[4]; + } cleanup_cache; + /* Pr cache (for pr's benefit): */ + struct + { + /** function start address */ + uint32_t fnstart; + /** pointer to EHT entry header word */ + _Unwind_EHT_Header *ehtp; + /** additional data */ + uint32_t additional; + uint32_t reserved1; + } pr_cache; + /** Force alignment of next item to 8-byte boundary */ + long long int :0; +}; + +/* Unwinding functions */ +_Unwind_Reason_Code _Unwind_RaiseException(struct _Unwind_Exception *ucbp); +void _Unwind_Resume(struct _Unwind_Exception *ucbp); +void _Unwind_Complete(struct _Unwind_Exception *ucbp); +void _Unwind_DeleteException(struct _Unwind_Exception *ucbp); +void *_Unwind_GetLanguageSpecificData(struct _Unwind_Context*); + +typedef enum +{ + _UVRSR_OK = 0, + _UVRSR_NOT_IMPLEMENTED = 1, + _UVRSR_FAILED = 2 +} _Unwind_VRS_Result; +typedef enum +{ + _UVRSC_CORE = 0, + _UVRSC_VFP = 1, + _UVRSC_WMMXD = 3, + _UVRSC_WMMXC = 4 +} _Unwind_VRS_RegClass; +typedef enum +{ + _UVRSD_UINT32 = 0, + _UVRSD_VFPX = 1, + _UVRSD_UINT64 = 3, + _UVRSD_FLOAT = 4, + _UVRSD_DOUBLE = 5 +} _Unwind_VRS_DataRepresentation; + +_Unwind_VRS_Result _Unwind_VRS_Get(_Unwind_Context *context, + _Unwind_VRS_RegClass regclass, + uint32_t regno, + _Unwind_VRS_DataRepresentation representation, + void *valuep); +_Unwind_VRS_Result _Unwind_VRS_Set(_Unwind_Context *context, + _Unwind_VRS_RegClass regclass, + uint32_t regno, + _Unwind_VRS_DataRepresentation representation, + void *valuep); + +/* Return the base-address for data references. */ +extern unsigned long _Unwind_GetDataRelBase(struct _Unwind_Context *); + +/* Return the base-address for text references. */ +extern unsigned long _Unwind_GetTextRelBase(struct _Unwind_Context *); +extern unsigned long _Unwind_GetRegionStart(struct _Unwind_Context *); + +/** + * The next set of functions are compatibility extensions, implementing Itanium + * ABI functions on top of ARM ones. + */ + +#define _UA_SEARCH_PHASE 1 +#define _UA_CLEANUP_PHASE 2 +#define _UA_HANDLER_FRAME 4 +#define _UA_FORCE_UNWIND 8 + +static inline unsigned long _Unwind_GetGR(struct _Unwind_Context *context, int reg) +{ + unsigned long val; + _Unwind_VRS_Get(context, _UVRSC_CORE, reg, _UVRSD_UINT32, &val); + return val; +} +static inline void _Unwind_SetGR(struct _Unwind_Context *context, int reg, unsigned long val) +{ + _Unwind_VRS_Set(context, _UVRSC_CORE, reg, _UVRSD_UINT32, &val); +} +static inline unsigned long _Unwind_GetIP(_Unwind_Context *context) +{ + // Low bit store the thumb state - discard it + return _Unwind_GetGR(context, 15) & ~1; +} +static inline void _Unwind_SetIP(_Unwind_Context *context, unsigned long val) +{ + // The lowest bit of the instruction pointer indicates whether we're in + // thumb or ARM mode. This is assumed to be fixed throughout a function, + // so must be propagated when setting the program counter. + unsigned long thumbState = _Unwind_GetGR(context, 15) & 1; + _Unwind_SetGR(context, 15, (val | thumbState)); +} + +/** GNU API function that unwinds the frame */ +_Unwind_Reason_Code __gnu_unwind_frame(struct _Unwind_Exception*, struct _Unwind_Context*); + + +#define DECLARE_PERSONALITY_FUNCTION(name) \ +_Unwind_Reason_Code name(_Unwind_State state,\ + struct _Unwind_Exception *exceptionObject,\ + struct _Unwind_Context *context); + +#define BEGIN_PERSONALITY_FUNCTION(name) \ +_Unwind_Reason_Code name(_Unwind_State state,\ + struct _Unwind_Exception *exceptionObject,\ + struct _Unwind_Context *context)\ +{\ + fprintf(stderr, "LSDA: %p\n", (void*)_Unwind_GetLanguageSpecificData(context));\ + fprintf(stderr, "IP: %p\n", (void*)_Unwind_GetIP(context));\ + int version = 1;\ + uint64_t exceptionClass = exceptionObject->exception_class;\ + int actions;\ + switch (state)\ + {\ + default: return _URC_FAILURE;\ + case _US_VIRTUAL_UNWIND_FRAME:\ + {\ + actions = _UA_SEARCH_PHASE;\ + break;\ + }\ + case _US_UNWIND_FRAME_STARTING:\ + {\ + actions = _UA_CLEANUP_PHASE;\ + if (exceptionObject->barrier_cache.sp == _Unwind_GetGR(context, 13))\ + {\ + actions |= _UA_HANDLER_FRAME;\ + }\ + break;\ + }\ + case _US_UNWIND_FRAME_RESUME:\ + {\ + return continueUnwinding(exceptionObject, context);\ + break;\ + }\ + }\ + _Unwind_SetGR (context, 12, (unsigned long)exceptionObject); + +#define CALL_PERSONALITY_FUNCTION(name) name(state,exceptionObject,context) diff --git a/src/unwind-itanium.h b/src/unwind-itanium.h new file mode 100644 index 0000000..16b3eed --- /dev/null +++ b/src/unwind-itanium.h @@ -0,0 +1,170 @@ +/* libunwind - a platform-independent unwind library + Copyright (C) 2003 Hewlett-Packard Co + Contributed by David Mosberger-Tang + +This file is part of libunwind. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +#ifndef _UNWIND_H +#define _UNWIND_H + +/* For uint64_t */ +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Minimal interface as per C++ ABI draft standard: + + http://www.codesourcery.com/cxx-abi/abi-eh.html */ + +typedef enum + { + _URC_NO_REASON = 0, + _URC_FOREIGN_EXCEPTION_CAUGHT = 1, + _URC_FATAL_PHASE2_ERROR = 2, + _URC_FATAL_PHASE1_ERROR = 3, + _URC_NORMAL_STOP = 4, + _URC_END_OF_STACK = 5, + _URC_HANDLER_FOUND = 6, + _URC_INSTALL_CONTEXT = 7, + _URC_CONTINUE_UNWIND = 8 + } +_Unwind_Reason_Code; + +typedef int _Unwind_Action; + +#define _UA_SEARCH_PHASE 1 +#define _UA_CLEANUP_PHASE 2 +#define _UA_HANDLER_FRAME 4 +#define _UA_FORCE_UNWIND 8 + +struct _Unwind_Context; /* opaque data-structure */ +struct _Unwind_Exception; /* forward-declaration */ + +typedef void (*_Unwind_Exception_Cleanup_Fn) (_Unwind_Reason_Code, + struct _Unwind_Exception *); + +typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn) (int, _Unwind_Action, + uint64_t, + struct _Unwind_Exception *, + struct _Unwind_Context *, + void *); + +/* The C++ ABI requires exception_class, private_1, and private_2 to + be of type uint64 and the entire structure to be + double-word-aligned. Please note that exception_class stays 64-bit + even on 32-bit machines for gcc compatibility. */ +struct _Unwind_Exception + { + uint64_t exception_class; + _Unwind_Exception_Cleanup_Fn exception_cleanup; + unsigned long private_1; + unsigned long private_2; + } __attribute__((__aligned__)); + +extern _Unwind_Reason_Code _Unwind_RaiseException (struct _Unwind_Exception *); +extern _Unwind_Reason_Code _Unwind_ForcedUnwind (struct _Unwind_Exception *, + _Unwind_Stop_Fn, void *); +extern void _Unwind_Resume (struct _Unwind_Exception *); +extern void _Unwind_DeleteException (struct _Unwind_Exception *); +extern unsigned long _Unwind_GetGR (struct _Unwind_Context *, int); +extern void _Unwind_SetGR (struct _Unwind_Context *, int, unsigned long); +extern unsigned long _Unwind_GetIP (struct _Unwind_Context *); +extern unsigned long _Unwind_GetIPInfo (struct _Unwind_Context *, int *); +extern void _Unwind_SetIP (struct _Unwind_Context *, unsigned long); +extern unsigned long _Unwind_GetLanguageSpecificData (struct _Unwind_Context*); +extern unsigned long _Unwind_GetRegionStart (struct _Unwind_Context *); + +#ifdef _GNU_SOURCE + +/* Callback for _Unwind_Backtrace(). The backtrace stops immediately + if the callback returns any value other than _URC_NO_REASON. */ +typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn) (struct _Unwind_Context *, + void *); + +/* See http://gcc.gnu.org/ml/gcc-patches/2001-09/msg00082.html for why + _UA_END_OF_STACK exists. */ +# define _UA_END_OF_STACK 16 + +/* If the unwind was initiated due to a forced unwind, resume that + operation, else re-raise the exception. This is used by + __cxa_rethrow(). */ +extern _Unwind_Reason_Code + _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *); + +/* See http://gcc.gnu.org/ml/gcc-patches/2003-09/msg00154.html for why + _Unwind_GetBSP() exists. */ +extern unsigned long _Unwind_GetBSP (struct _Unwind_Context *); + +/* Return the "canonical frame address" for the given context. + This is used by NPTL... */ +extern unsigned long _Unwind_GetCFA (struct _Unwind_Context *); + +/* Return the base-address for data references. */ +extern unsigned long _Unwind_GetDataRelBase (struct _Unwind_Context *); + +/* Return the base-address for text references. */ +extern unsigned long _Unwind_GetTextRelBase (struct _Unwind_Context *); + +/* Call _Unwind_Trace_Fn once for each stack-frame, without doing any + cleanup. The first frame for which the callback is invoked is the + one for the caller of _Unwind_Backtrace(). _Unwind_Backtrace() + returns _URC_END_OF_STACK when the backtrace stopped due to + reaching the end of the call-chain or _URC_FATAL_PHASE1_ERROR if it + stops for any other reason. */ +extern _Unwind_Reason_Code _Unwind_Backtrace (_Unwind_Trace_Fn, void *); + +/* Find the start-address of the procedure containing the specified IP + or NULL if it cannot be found (e.g., because the function has no + unwind info). Note: there is not necessarily a one-to-one + correspondence between source-level functions and procedures: some + functions don't have unwind-info and others are split into multiple + procedures. */ +extern void *_Unwind_FindEnclosingFunction (void *); + +/* See also Linux Standard Base Spec: + http://www.linuxbase.org/spec/refspecs/LSB_1.3.0/gLSB/gLSB/libgcc-s.html */ + +#endif /* _GNU_SOURCE */ + +#define DECLARE_PERSONALITY_FUNCTION(name) \ +_Unwind_Reason_Code name(int version,\ + _Unwind_Action actions,\ + uint64_t exceptionClass,\ + struct _Unwind_Exception *exceptionObject,\ + struct _Unwind_Context *context); +#define BEGIN_PERSONALITY_FUNCTION(name) \ +_Unwind_Reason_Code name(int version,\ + _Unwind_Action actions,\ + uint64_t exceptionClass,\ + struct _Unwind_Exception *exceptionObject,\ + struct _Unwind_Context *context)\ +{ + +#define CALL_PERSONALITY_FUNCTION(name) name(version, actions, exceptionClass, exceptionObject, context) + +#ifdef __cplusplus +} +#endif + +#endif /* _UNWIND_H */ diff --git a/src/unwind.h b/src/unwind.h new file mode 100644 index 0000000..76b6780 --- /dev/null +++ b/src/unwind.h @@ -0,0 +1,5 @@ +#ifdef __arm__ +#include "unwind-arm.h" +#else +#include "unwind-itanium.h" +#endif -- cgit v1.2.3 From f007779dc207227afd301fdc6270a42bf4e0a89f Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Wed, 23 Nov 2011 10:19:42 +0000 Subject: Use the ARM / Itanium EH abstraction layer. --- src/cxxabi.h | 2 +- src/dwarf_eh.h | 4 ++-- src/exception.cc | 8 ++------ 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/cxxabi.h b/src/cxxabi.h index db46247..b081806 100644 --- a/src/cxxabi.h +++ b/src/cxxabi.h @@ -1,7 +1,7 @@ #ifndef __CXXABI_H_ #define __CXXABI_H_ #include -#include +#include "unwind.h" namespace std { class type_info; diff --git a/src/dwarf_eh.h b/src/dwarf_eh.h index 0901751..35b9af1 100644 --- a/src/dwarf_eh.h +++ b/src/dwarf_eh.h @@ -16,10 +16,10 @@ // that it doesn't impact the rest of the program. #ifndef _GNU_SOURCE # define _GNU_SOURCE 1 -# include +# include "unwind.h" # undef _GNU_SOURCE #else -# include +# include "unwind.h" #endif #include diff --git a/src/exception.cc b/src/exception.cc index 66e0929..0abf18c 100644 --- a/src/exception.cc +++ b/src/exception.cc @@ -864,12 +864,8 @@ static handler_type check_action_record(_Unwind_Context *context, * DWARF metadata and is called by the unwind library for each C++ stack frame * containing catch or cleanup code. */ -extern "C" _Unwind_Reason_Code __gxx_personality_v0(int version, - _Unwind_Action actions, - uint64_t exceptionClass, - struct _Unwind_Exception *exceptionObject, - struct _Unwind_Context *context) -{ +extern "C" +BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0) // This personality function is for version 1 of the ABI. If you use it // with a future version of the ABI, it won't know what to do, so it // reports a fatal error and give up before it breaks anything. -- cgit v1.2.3 From c5a86232b046313db9845de6a4c74e2733177927 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Wed, 23 Nov 2011 10:35:55 +0000 Subject: And remember to save the temporaries in the ARM EH header if required... --- src/exception.cc | 77 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 9 deletions(-) diff --git a/src/exception.cc b/src/exception.cc index 0abf18c..1e60ee9 100644 --- a/src/exception.cc +++ b/src/exception.cc @@ -9,6 +9,67 @@ using namespace ABI_NAMESPACE; +/** + * Saves the result of the landing pad that we have found. For ARM, this is + * stored in the generic unwind structure, while on other platforms it is + * stored in the C++ exception. + */ +static void saveLandingPad(struct _Unwind_Context *context, + struct _Unwind_Exception *ucb, + struct __cxa_exception *ex, + int selector, + dw_eh_ptr_t landingPad) +{ +#ifdef __arm__ + // On ARM, we store the saved exception in the generic part of the structure + ucb->barrier_cache.sp = _Unwind_GetGR(context, 13); + ucb->barrier_cache.bitpattern[1] = (uint32_t)selector; + ucb->barrier_cache.bitpattern[3] = (uint32_t)landingPad; +#else + // Cache the results for the phase 2 unwind, if we found a handler + // and this is not a foreign exception. + if (ex) + { + ex->handlerSwitchValue = selector; + ex->catchTemp = landingPad; + } +#endif +} + +/** + * Loads the saved landing pad. Returns 1 on success, 0 on failure. + */ +static int loadLandingPad(struct _Unwind_Context *context, + struct _Unwind_Exception *ucb, + struct __cxa_exception *ex, + unsigned long *selector, + dw_eh_ptr_t *landingPad) +{ +#ifdef __arm__ + *selector = ucb->barrier_cache.bitpattern[1]; + *landingPad = (dw_eh_ptr_t)ucb->barrier_cache.bitpattern[3]; + return 1; +#else + if (ex) + { + *selector = ex->handlerSwitchValue; + *landingPad = (dw_eh_ptr_t)ex->catchTemp; + return 0; + } + return 0; +#endif +} + +static inline _Unwind_Reason_Code continueUnwinding(struct _Unwind_Exception *ex, + struct _Unwind_Context *context) +{ +#ifdef __arm__ + if (__gnu_unwind_frame(ex, context) != _URC_OK) { return _URC_FAILURE; } +#endif + return _URC_CONTINUE_UNWIND; +} + + extern "C" void __cxa_free_exception(void *thrown_exception); extern "C" void __cxa_free_dependent_exception(void *thrown_exception); extern "C" void* __dynamic_cast(const void *sub, @@ -892,7 +953,7 @@ BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0) (unsigned char*)_Unwind_GetLanguageSpecificData(context); // No LSDA implies no landing pads - try the next frame - if (0 == lsda_addr) { return _URC_CONTINUE_UNWIND; } + if (0 == lsda_addr) { return continueUnwinding(exceptionObject, context); } // These two variables define how the exception will be handled. dwarf_eh_action action = {0}; @@ -937,15 +998,14 @@ BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0) // and this is not a foreign exception. if (ex) { - ex->handlerSwitchValue = selector; - ex->actionRecord = (const char*)action.action_record; + saveLandingPad(context, exceptionObject, ex, selector, action.landing_pad); ex->languageSpecificData = (const char*)lsda_addr; - ex->catchTemp = action.landing_pad; + ex->actionRecord = (const char*)action.action_record; // ex->adjustedPtr is set when finding the action record. } return _URC_HANDLER_FOUND; } - return _URC_CONTINUE_UNWIND; + return continueUnwinding(exceptionObject, context); } @@ -958,11 +1018,11 @@ BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0) // cleanup struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr); dwarf_eh_find_callsite(context, &lsda, &action); - if (0 == action.landing_pad) { return _URC_CONTINUE_UNWIND; } + if (0 == action.landing_pad) { return continueUnwinding(exceptionObject, context); } handler_type found_handler = check_action_record(context, &lsda, action.action_record, realEx, &selector, ex->adjustedPtr); // Ignore handlers this time. - if (found_handler != handler_cleanup) { return _URC_CONTINUE_UNWIND; } + if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); } } else if (foreignException) { @@ -979,9 +1039,8 @@ BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0) else { // Restore the saved info if we saved some last time. - action.landing_pad = (dw_eh_ptr_t)ex->catchTemp; + loadLandingPad(context, exceptionObject, ex, &selector, &action.landing_pad); ex->catchTemp = 0; - selector = (unsigned long)ex->handlerSwitchValue; ex->handlerSwitchValue = 0; } -- cgit v1.2.3 From fa252d2522b4972ac2fdc376e4200db3234d9d3a Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Wed, 23 Nov 2011 11:14:44 +0000 Subject: Update readme - we do support the ARM ABI now (in theory, at least). --- README | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README b/README index b51f626..37ef4e3 100644 --- a/README +++ b/README @@ -28,4 +28,4 @@ Supported Platforms This code was initially developed on FreeBSD/x86, and has also been tested on FreeBSD/x86-64. It should work on other platforms that use the Code Sourcery ABI, for example Itanium, however this is untested. -Currently, this library has no support for ARM EABI (or any other ARM ABI). +This library also supports the ARM EH ABI. -- cgit v1.2.3 From 75ca8259acea380d867f136738e3835250ea0855 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Thu, 24 Nov 2011 20:03:52 +0000 Subject: Mostly-working ARM exceptions. Exception specifications are still broken - only the first is checked. --- src/dwarf_eh.h | 7 ++++++ src/exception.cc | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/unwind-arm.h | 10 ++++++--- src/unwind.h | 13 ++++++++++++ 4 files changed, 90 insertions(+), 5 deletions(-) diff --git a/src/dwarf_eh.h b/src/dwarf_eh.h index 35b9af1..02d8a11 100644 --- a/src/dwarf_eh.h +++ b/src/dwarf_eh.h @@ -193,6 +193,7 @@ static int64_t read_sleb128(dw_eh_ptr_t *data) static uint64_t read_value(char encoding, dw_eh_ptr_t *data) { enum dwarf_data_encoding type = get_encoding(encoding); + //fprintf(stderr, "Loading (encoding %x) from %p\n", (int)encoding, *data); uint64_t v; switch (type) { @@ -235,6 +236,7 @@ static uint64_t resolve_indirect_value(_Unwind_Context *c, int64_t v, dw_eh_ptr_t start) { + //fprintf(stderr, "resolving(encoding %x) from %p\n", (int)get_base(encoding), (void*)(intptr_t)v); switch (get_base(encoding)) { case DW_EH_PE_pcrel: @@ -251,6 +253,7 @@ static uint64_t resolve_indirect_value(_Unwind_Context *c, default: break; } + //fprintf(stderr, "is indirect %p\n", is_indirect(encoding)); // If this is an indirect value, then it is really the address of the real // value // TODO: Check whether this should really always be a pointer - it seems to @@ -332,6 +335,7 @@ static inline struct dwarf_eh_lsda parse_lsda(_Unwind_Context *context, // spec says, but does seem to be how G++ indicates this. lsda.type_table = 0; lsda.type_table_encoding = *data++; + //fprintf(stderr, "Type table Encoding: %x\n", (int)lsda.type_table_encoding); if (lsda.type_table_encoding != DW_EH_PE_omit) { v = read_uleb128(&data); @@ -340,6 +344,9 @@ static inline struct dwarf_eh_lsda parse_lsda(_Unwind_Context *context, lsda.type_table = type_table; //lsda.type_table = (uintptr_t*)(data + v); } +#if __arm__ + lsda.type_table_encoding = (DW_EH_PE_pcrel | DW_EH_PE_indirect); +#endif lsda.callsite_encoding = (enum dwarf_data_encoding)(*(data++)); diff --git a/src/exception.cc b/src/exception.cc index 1e60ee9..3b99735 100644 --- a/src/exception.cc +++ b/src/exception.cc @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "typeinfo.h" #include "dwarf_eh.h" @@ -25,7 +26,7 @@ static void saveLandingPad(struct _Unwind_Context *context, ucb->barrier_cache.sp = _Unwind_GetGR(context, 13); ucb->barrier_cache.bitpattern[1] = (uint32_t)selector; ucb->barrier_cache.bitpattern[3] = (uint32_t)landingPad; -#else +#endif // Cache the results for the phase 2 unwind, if we found a handler // and this is not a foreign exception. if (ex) @@ -33,7 +34,6 @@ static void saveLandingPad(struct _Unwind_Context *context, ex->handlerSwitchValue = selector; ex->catchTemp = landingPad; } -#endif } /** @@ -119,6 +119,10 @@ struct __cxa_thread_info * in ABI spec [3.3.1]). */ int emergencyBuffersHeld; + /** + * The exception currently running in a cleanup. + */ + _Unwind_Exception *currentCleanup; /** * The public part of this structure, accessible from outside of this * module. @@ -580,9 +584,11 @@ static void report_failure(_Unwind_Reason_Code err, __cxa_exception *thrown_exce case _URC_FATAL_PHASE1_ERROR: fprintf(stderr, "Fatal error during phase 1 unwinding\n"); break; +#ifndef __arm__ case _URC_FATAL_PHASE2_ERROR: fprintf(stderr, "Fatal error during phase 2 unwinding\n"); break; +#endif case _URC_END_OF_STACK: fprintf(stderr, "Terminating due to uncaught exception %p", (void*)thrown_exception); @@ -650,6 +656,7 @@ extern "C" void __cxa_throw(void *thrown_exception, std::type_info *tinfo, void(*dest)(void*)) { + fprintf(stderr, "Thrown exception: %p\n", thrown_exception); __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1; ex->referenceCount = 1; @@ -757,6 +764,7 @@ static std::type_info *get_type_info_entry(_Unwind_Context *context, // Get the address of the record in the table. dw_eh_ptr_t record = lsda->type_table - dwarf_size_of_fixed_size_field(lsda->type_table_encoding)*filter; + //record -= 4; dw_eh_ptr_t start = record; // Read the value, but it's probably an indirect reference... int64_t offset = read_value(lsda->type_table_encoding, &record); @@ -770,6 +778,7 @@ static std::type_info *get_type_info_entry(_Unwind_Context *context, } + /** * Checks the type signature found in a handler against the type of the thrown * object. If ex is 0 then it is assumed to be a foreign exception and only @@ -895,7 +904,14 @@ static handler_type check_action_record(_Unwind_Context *context, *selector = filter; while (*type_index) { +#ifdef __arm__ + //fprintf(stderr, "Filter: %d\n", filter); + std::type_info *handler_type = handler_type = get_type_info_entry(context, lsda, -filter - 1); + //fprintf(stderr, "Handler: %p\n", handler_type); + type_index++; +#else std::type_info *handler_type = get_type_info_entry(context, lsda, *(type_index++)); +#endif // If the exception spec matches a permitted throw type for // this function, don't report a handler - we are allowed to // propagate this exception out. @@ -1023,6 +1039,8 @@ BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0) action.action_record, realEx, &selector, ex->adjustedPtr); // Ignore handlers this time. if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); } + __cxa_thread_info *info = thread_info_fast(); + info->currentCleanup = exceptionObject; } else if (foreignException) { @@ -1066,6 +1084,7 @@ extern "C" void *__cxa_begin_catch(void *e) throw() extern "C" void *__cxa_begin_catch(void *e) #endif { + fprintf(stderr, "Entering catch\n"); // Decrement the uncaught exceptions count __cxa_eh_globals *globals = __cxa_get_globals(); globals->uncaughtExceptions--; @@ -1111,19 +1130,27 @@ extern "C" void *__cxa_begin_catch(void *e) ex->handlerCount++; } + fprintf(stderr, "Exception base pointer: %p\n", ((char*)exceptionObject + sizeof(_Unwind_Exception))); + fprintf(stderr, "Exception edjusted pointer: %p\n", ex->adjustedPtr); + fprintf(stderr, "Exception as int: %d\n", *(int*)((char*)exceptionObject + sizeof(_Unwind_Exception))); + return ex->adjustedPtr; } + fprintf(stderr, "Exception as int: %d\n", *(int*)((char*)exceptionObject + sizeof(_Unwind_Exception))); // exceptionObject is the pointer to the _Unwind_Exception within the // __cxa_exception. The throw object is after this return ((char*)exceptionObject + sizeof(_Unwind_Exception)); } + + /** * ABI function called when exiting a catch block. This will free the current * exception if it is no longer referenced in other catch blocks. */ extern "C" void __cxa_end_catch() { + fprintf(stderr, "Leaving catch\n"); // We can call the fast version here because the slow version is called in // __cxa_throw(), which must have been called before we end a catch block __cxa_eh_globals *globals = __cxa_get_globals_fast(); @@ -1169,6 +1196,20 @@ extern "C" void __cxa_end_catch() } } +/* +extern "C" void __cxa_end_cleanup(_Unwind_Exception* exceptionObject) +{ + _Unwind_Resume(0); +// fprintf(stderr, "Leaving cleanup\n"); +} + +extern "C" bool __cxa_begin_cleanup(_Unwind_Exception* exceptionObject) +{ + fprintf(stderr, "Entering cleanup\n"); + return true; +} +*/ + /** * ABI function. Returns the type of the current exception. */ @@ -1336,3 +1377,23 @@ namespace std return terminateHandler; } } +#ifdef __arm__ +extern "C" _Unwind_Exception *__cxa_get_cleanup(void) +{ + __cxa_thread_info *info = thread_info_fast(); + return info->currentCleanup; +} + +asm ( +".pushsection .text.__cxa_end_cleanup \n" +".global __cxa_end_cleanup \n" +".type __cxa_end_cleanup, \"function\" \n" +"__cxa_end_cleanup: \n" +" push {r1, r2, r3, r4} \n" +" bl __cxa_get_cleanup \n" +" push {r1, r2, r3, r4} \n" +" b _Unwind_Resume \n" +" bl abort \n" +".popsection \n" +); +#endif diff --git a/src/unwind-arm.h b/src/unwind-arm.h index e041cbf..f9ed429 100644 --- a/src/unwind-arm.h +++ b/src/unwind-arm.h @@ -116,6 +116,12 @@ extern unsigned long _Unwind_GetDataRelBase(struct _Unwind_Context *); extern unsigned long _Unwind_GetTextRelBase(struct _Unwind_Context *); extern unsigned long _Unwind_GetRegionStart(struct _Unwind_Context *); +typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn) (struct _Unwind_Context *, + void *); +extern _Unwind_Reason_Code _Unwind_Backtrace (_Unwind_Trace_Fn, void *); +extern _Unwind_Reason_Code + _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *); + /** * The next set of functions are compatibility extensions, implementing Itanium * ABI functions on top of ARM ones. @@ -164,8 +170,6 @@ _Unwind_Reason_Code name(_Unwind_State state,\ struct _Unwind_Exception *exceptionObject,\ struct _Unwind_Context *context)\ {\ - fprintf(stderr, "LSDA: %p\n", (void*)_Unwind_GetLanguageSpecificData(context));\ - fprintf(stderr, "IP: %p\n", (void*)_Unwind_GetIP(context));\ int version = 1;\ uint64_t exceptionClass = exceptionObject->exception_class;\ int actions;\ @@ -192,6 +196,6 @@ _Unwind_Reason_Code name(_Unwind_State state,\ break;\ }\ }\ - _Unwind_SetGR (context, 12, (unsigned long)exceptionObject); + _Unwind_SetGR (context, 12, (unsigned long)exceptionObject);\ #define CALL_PERSONALITY_FUNCTION(name) name(state,exceptionObject,context) diff --git a/src/unwind.h b/src/unwind.h index 76b6780..5089491 100644 --- a/src/unwind.h +++ b/src/unwind.h @@ -1,5 +1,18 @@ +#ifndef UNWIND_H_INCLUDED +#define UNWIND_H_INCLUDED + +#ifdef __cplusplus +extern "C" { +#endif + #ifdef __arm__ #include "unwind-arm.h" #else #include "unwind-itanium.h" #endif + +#ifdef __cplusplus +} +#endif + +#endif -- cgit v1.2.3 From c996267251a83097aa4bac0b350801e92d6fc20a Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Fri, 25 Nov 2011 11:24:48 +0000 Subject: ARM expects 32-bit guards for static variables. --- src/guard.cc | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/guard.cc b/src/guard.cc index 23a4bae..f236053 100644 --- a/src/guard.cc +++ b/src/guard.cc @@ -16,6 +16,59 @@ */ #include #include +#include + +#ifdef __arm__ +// ARM ABI - 32-bit guards. + +/** + * Acquires a lock on a guard, returning 0 if the object has already been + * initialised, and 1 if it has not. If the object is already constructed then + * this function just needs to read a byte from memory and return. + */ +extern "C" int __cxa_guard_acquire(volatile int32_t *guard_object) +{ + if ((1<<31) == *guard_object) { return 0; } + // If we can atomically move the value from 0 -> 1, then this is + // uninitialised. + if (__sync_bool_compare_and_swap(guard_object, 0, 1)) + { + return 1; + } + // If the value is not 0, some other thread was initialising this. Spin + // until it's finished. + while (__sync_bool_compare_and_swap(guard_object, (1<<31), (1<<31))) + { + // If the other thread aborted, then we grab the lock + if (__sync_bool_compare_and_swap(guard_object, 0, 1)) + { + return 1; + } + sched_yield(); + } + return 0; +} + +/** + * Releases the lock without marking the object as initialised. This function + * is called if initialising a static causes an exception to be thrown. + */ +extern "C" void __cxa_guard_abort(int32_t *guard_object) +{ + assert(__sync_bool_compare_and_swap(guard_object, 1, 0)); +} +/** + * Releases the guard and marks the object as initialised. This function is + * called after successful initialisation of a static. + */ +extern "C" void __cxa_guard_release(int32_t *guard_object) +{ + assert(__sync_bool_compare_and_swap(guard_object, 1, (1<<31))); +} + + +#else +// Itanium ABI: 64-bit guards /** * Returns a pointer to the low 32 bits in a 64-bit value, respecting the @@ -78,3 +131,4 @@ extern "C" void __cxa_guard_release(int64_t *guard_object) __cxa_guard_abort(guard_object); } +#endif -- cgit v1.2.3 From c106344e1ff40b3dc46304a05f1518150b07aa53 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Fri, 25 Nov 2011 12:16:58 +0000 Subject: Finished ARM EH enough that it passes the test suite. Still to do: properly handle nested cleanups. --- src/dwarf_eh.h | 4 ---- src/exception.cc | 45 +++++++++++++++------------------------------ 2 files changed, 15 insertions(+), 34 deletions(-) diff --git a/src/dwarf_eh.h b/src/dwarf_eh.h index 02d8a11..52e6369 100644 --- a/src/dwarf_eh.h +++ b/src/dwarf_eh.h @@ -193,7 +193,6 @@ static int64_t read_sleb128(dw_eh_ptr_t *data) static uint64_t read_value(char encoding, dw_eh_ptr_t *data) { enum dwarf_data_encoding type = get_encoding(encoding); - //fprintf(stderr, "Loading (encoding %x) from %p\n", (int)encoding, *data); uint64_t v; switch (type) { @@ -236,7 +235,6 @@ static uint64_t resolve_indirect_value(_Unwind_Context *c, int64_t v, dw_eh_ptr_t start) { - //fprintf(stderr, "resolving(encoding %x) from %p\n", (int)get_base(encoding), (void*)(intptr_t)v); switch (get_base(encoding)) { case DW_EH_PE_pcrel: @@ -253,7 +251,6 @@ static uint64_t resolve_indirect_value(_Unwind_Context *c, default: break; } - //fprintf(stderr, "is indirect %p\n", is_indirect(encoding)); // If this is an indirect value, then it is really the address of the real // value // TODO: Check whether this should really always be a pointer - it seems to @@ -335,7 +332,6 @@ static inline struct dwarf_eh_lsda parse_lsda(_Unwind_Context *context, // spec says, but does seem to be how G++ indicates this. lsda.type_table = 0; lsda.type_table_encoding = *data++; - //fprintf(stderr, "Type table Encoding: %x\n", (int)lsda.type_table_encoding); if (lsda.type_table_encoding != DW_EH_PE_omit) { v = read_uleb128(&data); diff --git a/src/exception.cc b/src/exception.cc index 3b99735..64e78a7 100644 --- a/src/exception.cc +++ b/src/exception.cc @@ -656,7 +656,6 @@ extern "C" void __cxa_throw(void *thrown_exception, std::type_info *tinfo, void(*dest)(void*)) { - fprintf(stderr, "Thrown exception: %p\n", thrown_exception); __cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1; ex->referenceCount = 1; @@ -899,19 +898,25 @@ static handler_type check_action_record(_Unwind_Context *context, } else if (filter < 0 && 0 != ex) { - unsigned char *type_index = ((unsigned char*)lsda->type_table - filter - 1); bool matched = false; *selector = filter; - while (*type_index) - { #ifdef __arm__ - //fprintf(stderr, "Filter: %d\n", filter); - std::type_info *handler_type = handler_type = get_type_info_entry(context, lsda, -filter - 1); - //fprintf(stderr, "Handler: %p\n", handler_type); - type_index++; + filter++; + std::type_info *handler_type = get_type_info_entry(context, lsda, filter--); + while (handler_type) + { + if (check_type_signature(ex, handler_type, adjustedPtr)) + { + matched = true; + break; + } + handler_type = get_type_info_entry(context, lsda, filter--); + } #else + unsigned char *type_index = ((unsigned char*)lsda->type_table - filter - 1); + while (*type_index) + { std::type_info *handler_type = get_type_info_entry(context, lsda, *(type_index++)); -#endif // If the exception spec matches a permitted throw type for // this function, don't report a handler - we are allowed to // propagate this exception out. @@ -921,6 +926,7 @@ static handler_type check_action_record(_Unwind_Context *context, break; } } +#endif if (matched) { continue; } // If we don't find an allowed exception spec, we need to install // the context for this action. The landing pad will then call the @@ -1084,7 +1090,6 @@ extern "C" void *__cxa_begin_catch(void *e) throw() extern "C" void *__cxa_begin_catch(void *e) #endif { - fprintf(stderr, "Entering catch\n"); // Decrement the uncaught exceptions count __cxa_eh_globals *globals = __cxa_get_globals(); globals->uncaughtExceptions--; @@ -1130,13 +1135,8 @@ extern "C" void *__cxa_begin_catch(void *e) ex->handlerCount++; } - fprintf(stderr, "Exception base pointer: %p\n", ((char*)exceptionObject + sizeof(_Unwind_Exception))); - fprintf(stderr, "Exception edjusted pointer: %p\n", ex->adjustedPtr); - fprintf(stderr, "Exception as int: %d\n", *(int*)((char*)exceptionObject + sizeof(_Unwind_Exception))); - return ex->adjustedPtr; } - fprintf(stderr, "Exception as int: %d\n", *(int*)((char*)exceptionObject + sizeof(_Unwind_Exception))); // exceptionObject is the pointer to the _Unwind_Exception within the // __cxa_exception. The throw object is after this return ((char*)exceptionObject + sizeof(_Unwind_Exception)); @@ -1150,7 +1150,6 @@ extern "C" void *__cxa_begin_catch(void *e) */ extern "C" void __cxa_end_catch() { - fprintf(stderr, "Leaving catch\n"); // We can call the fast version here because the slow version is called in // __cxa_throw(), which must have been called before we end a catch block __cxa_eh_globals *globals = __cxa_get_globals_fast(); @@ -1196,20 +1195,6 @@ extern "C" void __cxa_end_catch() } } -/* -extern "C" void __cxa_end_cleanup(_Unwind_Exception* exceptionObject) -{ - _Unwind_Resume(0); -// fprintf(stderr, "Leaving cleanup\n"); -} - -extern "C" bool __cxa_begin_cleanup(_Unwind_Exception* exceptionObject) -{ - fprintf(stderr, "Entering cleanup\n"); - return true; -} -*/ - /** * ABI function. Returns the type of the current exception. */ -- cgit v1.2.3 From c1e126a45fd8610347d428bea3dbf038a83002db Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Fri, 25 Nov 2011 12:31:23 +0000 Subject: And add support for nesting cleanups. --- src/cxxabi.h | 12 ++++++++++++ src/exception.cc | 28 +++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/cxxabi.h b/src/cxxabi.h index b081806..040d28d 100644 --- a/src/cxxabi.h +++ b/src/cxxabi.h @@ -87,6 +87,18 @@ struct __cxa_exception * handler count reaches 0 (which it doesn't with the top bit set). */ int handlerCount; +#ifdef __arm__ + /** + * The ARM EH ABI requires the unwind library to keep track of exceptions + * during cleanups. These support nesting, so we need to keep a list of + * them. + */ + _Unwind_Exception *nextCleanup; + /** + * The number of cleanups that are currently being run on this exception. + */ + int cleanupCount; +#endif /** * The selector value to be returned when installing the catch handler. * Used at the call site to determine which catch() block should execute. diff --git a/src/exception.cc b/src/exception.cc index 64e78a7..d53d997 100644 --- a/src/exception.cc +++ b/src/exception.cc @@ -143,6 +143,10 @@ struct __cxa_dependent_exception terminate_handler terminateHandler; __cxa_exception *nextException; int handlerCount; +#ifdef __arm__ + _Unwind_Exception *nextCleanup; + int cleanupCount; +#endif int handlerSwitchValue; const char *actionRecord; const char *languageSpecificData; @@ -942,6 +946,19 @@ static handler_type check_action_record(_Unwind_Context *context, return found; } +static void pushCleanupException(_Unwind_Exception *exceptionObject, + __cxa_exception *ex) +{ +#ifdef __arm__ + __cxa_thread_info *info = thread_info_fast(); + if (ex) + { + ex->nextCleanup = info->currentCleanup; + } + info->currentCleanup = exceptionObject; +#endif +} + /** * The exception personality function. This is referenced in the unwinding * DWARF metadata and is called by the unwind library for each C++ stack frame @@ -1045,8 +1062,7 @@ BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0) action.action_record, realEx, &selector, ex->adjustedPtr); // Ignore handlers this time. if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); } - __cxa_thread_info *info = thread_info_fast(); - info->currentCleanup = exceptionObject; + pushCleanupException(exceptionObject, ex); } else if (foreignException) { @@ -1366,7 +1382,13 @@ namespace std extern "C" _Unwind_Exception *__cxa_get_cleanup(void) { __cxa_thread_info *info = thread_info_fast(); - return info->currentCleanup; + _Unwind_Exception *exceptionObject = info->currentCleanup; + if (isCXXException(exceptionObject->exception_class)) + { + __cxa_exception *ex = exceptionFromPointer(exceptionObject); + info->currentCleanup = ex->nextCleanup; + } + return exceptionObject; } asm ( -- cgit v1.2.3 From 1be67aa8295314fb794c4e933d9bb7c7c33e0ca4 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Fri, 25 Nov 2011 15:37:14 +0000 Subject: Tweak the cleanup code slightly. --- src/exception.cc | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/exception.cc b/src/exception.cc index d53d997..abf6a10 100644 --- a/src/exception.cc +++ b/src/exception.cc @@ -953,6 +953,12 @@ static void pushCleanupException(_Unwind_Exception *exceptionObject, __cxa_thread_info *info = thread_info_fast(); if (ex) { + ex->cleanupCount++; + if (ex->cleanupCount > 1) + { + assert(exceptionObject == info->currentCleanup); + return; + } ex->nextCleanup = info->currentCleanup; } info->currentCleanup = exceptionObject; @@ -1386,7 +1392,16 @@ extern "C" _Unwind_Exception *__cxa_get_cleanup(void) if (isCXXException(exceptionObject->exception_class)) { __cxa_exception *ex = exceptionFromPointer(exceptionObject); - info->currentCleanup = ex->nextCleanup; + ex->cleanupCount--; + if (ex->cleanupCount == 0) + { + info->currentCleanup = ex->nextCleanup; + ex->nextCleanup = 0; + } + } + else + { + info->currentCleanup = 0; } return exceptionObject; } -- cgit v1.2.3 From 0f31431b38936983c803c10b241f55299818baad Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Thu, 24 Nov 2011 17:02:08 +0000 Subject: Add fake TLS support so that we work for single-threaded programs when not linked to pthread. --- src/exception.cc | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/exception.cc b/src/exception.cc index abf6a10..0896354 100644 --- a/src/exception.cc +++ b/src/exception.cc @@ -288,12 +288,25 @@ static void thread_cleanup(void* thread_info) */ static pthread_once_t once_control = PTHREAD_ONCE_INIT; +/** + * We may not be linked against a full pthread implementation. If we're not, + * then we need to fake the thread-local storage by storing 'thread-local' + * things in a global. + */ +static bool fakeTLS; +/** + * Thread-local storage for a single-threaded program. + */ +static __cxa_thread_info singleThreadInfo; /** * Initialise eh_key. */ static void init_key(void) { pthread_key_create(&eh_key, thread_cleanup); + pthread_setspecific(eh_key, (void*)0x42); + fakeTLS = (pthread_getspecific(eh_key) != (void*)0x42); + pthread_setspecific(eh_key, 0); } /** @@ -301,7 +314,11 @@ static void init_key(void) */ static __cxa_thread_info *thread_info() { - pthread_once(&once_control, init_key); + if (pthread_once(&once_control, init_key)) + { + fakeTLS = true; + } + if (fakeTLS) { return &singleThreadInfo; } __cxa_thread_info *info = (__cxa_thread_info*)pthread_getspecific(eh_key); if (0 == info) { @@ -316,6 +333,7 @@ static __cxa_thread_info *thread_info() */ static __cxa_thread_info *thread_info_fast() { + if (fakeTLS) { return &singleThreadInfo; } return (__cxa_thread_info*)pthread_getspecific(eh_key); } /** -- cgit v1.2.3 From 04a6546f5a628aaa29670a204e68c4d3120bb741 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Thu, 24 Nov 2011 17:03:55 +0000 Subject: Fixed a warning. --- src/memory.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/memory.cc b/src/memory.cc index 655c761..a5eb3e9 100644 --- a/src/memory.cc +++ b/src/memory.cc @@ -103,7 +103,7 @@ void * operator new[](size_t size) __attribute__((weak)) -void operator delete[](void * ptr) +void operator delete[](void * ptr) throw() { ::operator delete(ptr); } -- cgit v1.2.3 From c7d66e279e31c0523bcb8bc5b1dd5c98260f0848 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Thu, 24 Nov 2011 17:05:59 +0000 Subject: Use __sync_swap when available. --- src/memory.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/memory.cc b/src/memory.cc index a5eb3e9..027bc3f 100644 --- a/src/memory.cc +++ b/src/memory.cc @@ -11,6 +11,10 @@ #include #include "stdexcept.h" +#if !__has_builtin(__sync_swap) +#define __sync_swap __sync_lock_test_and_set +#endif + namespace std { struct nothrow_t {}; @@ -33,7 +37,7 @@ namespace std __attribute__((weak)) new_handler set_new_handler(new_handler handler) { - return __sync_lock_test_and_set(&new_handl, handler); + return __sync_swap(&new_handl, handler); } } -- cgit v1.2.3 From 8931d9e5180830a5433d16ae6b3ad8dd9e629512 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Thu, 24 Nov 2011 17:17:26 +0000 Subject: Make all pthread symbols weak and test before calling any of them. This should allow us to work on platforms like GNU/Linux that don't export pthread stubs in libc. --- src/exception.cc | 58 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/src/exception.cc b/src/exception.cc index 0896354..2da7454 100644 --- a/src/exception.cc +++ b/src/exception.cc @@ -8,6 +8,17 @@ #include "dwarf_eh.h" #include "cxxabi.h" +#pragma weak pthread_key_create +#pragma weak pthread_setspecific +#pragma weak pthread_getspecific +#pragma weak pthread_once +#pragma weak pthread_once +#pragma weak pthread_cond_signal +#pragma weak pthread_cond_wait +#pragma weak pthread_mutex_lock +#pragma weak pthread_mutex_unlock + + using namespace ABI_NAMESPACE; /** @@ -303,6 +314,13 @@ static __cxa_thread_info singleThreadInfo; */ static void init_key(void) { + if ((0 == pthread_key_create) || + (0 == pthread_setspecific) || + (0 == pthread_getspecific)) + { + fakeTLS = true; + return; + } pthread_key_create(&eh_key, thread_cleanup); pthread_setspecific(eh_key, (void*)0x42); fakeTLS = (pthread_getspecific(eh_key) != (void*)0x42); @@ -314,7 +332,7 @@ static void init_key(void) */ static __cxa_thread_info *thread_info() { - if (pthread_once(&once_control, init_key)) + if ((0 == pthread_once) || pthread_once(&once_control, init_key)) { fakeTLS = true; } @@ -385,7 +403,10 @@ static char *emergency_malloc(size_t size) // Only 4 emergency buffers allowed per thread! if (info->emergencyBuffersHeld > 3) { return 0; } - pthread_mutex_lock(&emergency_malloc_lock); + if (pthread_mutex_lock) + { + pthread_mutex_lock(&emergency_malloc_lock); + } int buffer = -1; while (buffer < 0) { @@ -396,7 +417,10 @@ static char *emergency_malloc(size_t size) void *m = calloc(1, size); if (0 != m) { - pthread_mutex_unlock(&emergency_malloc_lock); + if (pthread_mutex_unlock) + { + pthread_mutex_unlock(&emergency_malloc_lock); + } return (char*)m; } for (int i=0 ; i<16 ; i++) @@ -413,10 +437,24 @@ static char *emergency_malloc(size_t size) // of the emergency buffers. if (buffer < 0) { + // If we don't have pthread_cond_wait, then there is only one + // thread and it's already used all of the emergency buffers, so we + // have no alternative but to die. Calling abort() instead of + // terminate, because terminate can throw exceptions, which can + // bring us back here and infinite loop. + if (!pthread_cond_wait) + { + fputs("Terminating while out of memory trying to throw an exception", + stderr); + abort(); + } pthread_cond_wait(&emergency_malloc_wait, &emergency_malloc_lock); } } - pthread_mutex_unlock(&emergency_malloc_lock); + if (pthread_mutex_unlock) + { + pthread_mutex_unlock(&emergency_malloc_lock); + } info->emergencyBuffersHeld++; return emergency_buffer + (1024 * buffer); } @@ -449,13 +487,19 @@ static void emergency_malloc_free(char *ptr) memset((void*)ptr, 0, 1024); // Signal the condition variable to wake up any threads that are blocking // waiting for some space in the emergency buffer - pthread_mutex_lock(&emergency_malloc_lock); + if (pthread_mutex_lock) + { + pthread_mutex_lock(&emergency_malloc_lock); + } // In theory, we don't need to do this with the lock held. In practice, // our array of bools will probably be updated using 32-bit or 64-bit // memory operations, so this update may clobber adjacent values. buffer_allocated[buffer] = false; - pthread_cond_signal(&emergency_malloc_wait); - pthread_mutex_unlock(&emergency_malloc_lock); + if (pthread_cond_signal && pthread_mutex_unlock) + { + pthread_cond_signal(&emergency_malloc_wait); + pthread_mutex_unlock(&emergency_malloc_lock); + } } static char *alloc_or_die(size_t size) -- cgit v1.2.3 From ed2ad4589fe2dbf5bd560de64b68e8609ab1a42b Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Fri, 25 Nov 2011 09:14:05 +0000 Subject: Simplify the weak symbol tests so that they're only done for things that we don't expect any OS (even the silly one) to have stubs for. --- src/exception.cc | 44 ++++++-------------------------------------- 1 file changed, 6 insertions(+), 38 deletions(-) diff --git a/src/exception.cc b/src/exception.cc index 2da7454..43a9e2f 100644 --- a/src/exception.cc +++ b/src/exception.cc @@ -12,12 +12,6 @@ #pragma weak pthread_setspecific #pragma weak pthread_getspecific #pragma weak pthread_once -#pragma weak pthread_once -#pragma weak pthread_cond_signal -#pragma weak pthread_cond_wait -#pragma weak pthread_mutex_lock -#pragma weak pthread_mutex_unlock - using namespace ABI_NAMESPACE; @@ -403,10 +397,7 @@ static char *emergency_malloc(size_t size) // Only 4 emergency buffers allowed per thread! if (info->emergencyBuffersHeld > 3) { return 0; } - if (pthread_mutex_lock) - { - pthread_mutex_lock(&emergency_malloc_lock); - } + pthread_mutex_lock(&emergency_malloc_lock); int buffer = -1; while (buffer < 0) { @@ -417,10 +408,7 @@ static char *emergency_malloc(size_t size) void *m = calloc(1, size); if (0 != m) { - if (pthread_mutex_unlock) - { - pthread_mutex_unlock(&emergency_malloc_lock); - } + pthread_mutex_unlock(&emergency_malloc_lock); return (char*)m; } for (int i=0 ; i<16 ; i++) @@ -437,24 +425,10 @@ static char *emergency_malloc(size_t size) // of the emergency buffers. if (buffer < 0) { - // If we don't have pthread_cond_wait, then there is only one - // thread and it's already used all of the emergency buffers, so we - // have no alternative but to die. Calling abort() instead of - // terminate, because terminate can throw exceptions, which can - // bring us back here and infinite loop. - if (!pthread_cond_wait) - { - fputs("Terminating while out of memory trying to throw an exception", - stderr); - abort(); - } pthread_cond_wait(&emergency_malloc_wait, &emergency_malloc_lock); } } - if (pthread_mutex_unlock) - { - pthread_mutex_unlock(&emergency_malloc_lock); - } + pthread_mutex_unlock(&emergency_malloc_lock); info->emergencyBuffersHeld++; return emergency_buffer + (1024 * buffer); } @@ -487,19 +461,13 @@ static void emergency_malloc_free(char *ptr) memset((void*)ptr, 0, 1024); // Signal the condition variable to wake up any threads that are blocking // waiting for some space in the emergency buffer - if (pthread_mutex_lock) - { - pthread_mutex_lock(&emergency_malloc_lock); - } + pthread_mutex_lock(&emergency_malloc_lock); // In theory, we don't need to do this with the lock held. In practice, // our array of bools will probably be updated using 32-bit or 64-bit // memory operations, so this update may clobber adjacent values. buffer_allocated[buffer] = false; - if (pthread_cond_signal && pthread_mutex_unlock) - { - pthread_cond_signal(&emergency_malloc_wait); - pthread_mutex_unlock(&emergency_malloc_lock); - } + pthread_cond_signal(&emergency_malloc_wait); + pthread_mutex_unlock(&emergency_malloc_lock); } static char *alloc_or_die(size_t size) -- cgit v1.2.3 From f929524b3c4ac5e996725120966c818e15458f12 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Fri, 25 Nov 2011 11:46:22 +0000 Subject: Fix the build on compilers that don't have a __has_builtin --- src/memory.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/memory.cc b/src/memory.cc index 027bc3f..9a40b7f 100644 --- a/src/memory.cc +++ b/src/memory.cc @@ -11,6 +11,10 @@ #include #include "stdexcept.h" +#ifndef __has_builtin +#define __has_builtin(x) 0 +#endif + #if !__has_builtin(__sync_swap) #define __sync_swap __sync_lock_test_and_set #endif -- cgit v1.2.3