xref: /openbsd-src/gnu/gcc/libstdc++-v3/libsupc++/eh_arm.cc (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // -*- C++ -*- ARM specific Exception handling support routines.
2*404b540aSrobert // Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3*404b540aSrobert //
4*404b540aSrobert // This file is part of GCC.
5*404b540aSrobert //
6*404b540aSrobert // GCC is free software; you can redistribute it and/or modify
7*404b540aSrobert // it under the terms of the GNU General Public License as published by
8*404b540aSrobert // the Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert // any later version.
10*404b540aSrobert //
11*404b540aSrobert // GCC is distributed in the hope that it will be useful,
12*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*404b540aSrobert // GNU General Public License for more details.
15*404b540aSrobert //
16*404b540aSrobert // You should have received a copy of the GNU General Public License
17*404b540aSrobert // along with GCC; see the file COPYING.  If not, write to
18*404b540aSrobert // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19*404b540aSrobert // Boston, MA 02110-1301, USA.
20*404b540aSrobert 
21*404b540aSrobert // As a special exception, you may use this file as part of a free software
22*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
23*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
24*404b540aSrobert // this file and link it with other files to produce an executable, this
25*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
26*404b540aSrobert // the GNU General Public License.  This exception does not however
27*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
28*404b540aSrobert // the GNU General Public License.
29*404b540aSrobert 
30*404b540aSrobert #include <cxxabi.h>
31*404b540aSrobert #include "unwind-cxx.h"
32*404b540aSrobert 
33*404b540aSrobert #ifdef __ARM_EABI_UNWINDER__
34*404b540aSrobert 
35*404b540aSrobert using namespace __cxxabiv1;
36*404b540aSrobert 
37*404b540aSrobert 
38*404b540aSrobert // Given the thrown type THROW_TYPE, pointer to a variable containing a
39*404b540aSrobert // pointer to the exception object THROWN_PTR_P and a type CATCH_TYPE to
40*404b540aSrobert // compare against, return whether or not there is a match and if so,
41*404b540aSrobert // update *THROWN_PTR_P.
42*404b540aSrobert 
43*404b540aSrobert extern "C" __cxa_type_match_result
__cxa_type_match(_Unwind_Exception * ue_header,const std::type_info * catch_type,bool is_reference,void ** thrown_ptr_p)44*404b540aSrobert __cxa_type_match(_Unwind_Exception* ue_header,
45*404b540aSrobert 		 const std::type_info* catch_type,
46*404b540aSrobert 		 bool is_reference __attribute__((__unused__)),
47*404b540aSrobert 		 void** thrown_ptr_p)
48*404b540aSrobert {
49*404b540aSrobert   if (!__is_gxx_exception_class(ue_header->exception_class))
50*404b540aSrobert     return ctm_failed;
51*404b540aSrobert 
52*404b540aSrobert   __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
53*404b540aSrobert   const std::type_info* throw_type = xh->exceptionType;
54*404b540aSrobert   void* thrown_ptr = *thrown_ptr_p;
55*404b540aSrobert 
56*404b540aSrobert   // Pointer types need to adjust the actual pointer, not
57*404b540aSrobert   // the pointer to pointer that is the exception object.
58*404b540aSrobert   // This also has the effect of passing pointer types
59*404b540aSrobert   // "by value" through the __cxa_begin_catch return value.
60*404b540aSrobert   if (throw_type->__is_pointer_p())
61*404b540aSrobert     thrown_ptr = *(void**) thrown_ptr;
62*404b540aSrobert 
63*404b540aSrobert   if (catch_type->__do_catch(throw_type, &thrown_ptr, 1))
64*404b540aSrobert     {
65*404b540aSrobert       *thrown_ptr_p = thrown_ptr;
66*404b540aSrobert 
67*404b540aSrobert       if (typeid(*catch_type) == typeid (typeid(void*)))
68*404b540aSrobert 	{
69*404b540aSrobert 	  const __pointer_type_info *catch_pointer_type =
70*404b540aSrobert 	    static_cast<const __pointer_type_info *> (catch_type);
71*404b540aSrobert 	  const __pointer_type_info *throw_pointer_type =
72*404b540aSrobert 	    static_cast<const __pointer_type_info *> (throw_type);
73*404b540aSrobert 
74*404b540aSrobert 	  if (typeid (*catch_pointer_type->__pointee) != typeid (void)
75*404b540aSrobert 	      && (*catch_pointer_type->__pointee !=
76*404b540aSrobert 		  *throw_pointer_type->__pointee))
77*404b540aSrobert 	    return ctm_succeeded_with_ptr_to_base;
78*404b540aSrobert 	}
79*404b540aSrobert 
80*404b540aSrobert       return ctm_succeeded;
81*404b540aSrobert     }
82*404b540aSrobert 
83*404b540aSrobert   return ctm_failed;
84*404b540aSrobert }
85*404b540aSrobert 
86*404b540aSrobert // ABI defined routine called at the start of a cleanup handler.
87*404b540aSrobert extern "C" bool
__cxa_begin_cleanup(_Unwind_Exception * ue_header)88*404b540aSrobert __cxa_begin_cleanup(_Unwind_Exception* ue_header)
89*404b540aSrobert {
90*404b540aSrobert   __cxa_eh_globals *globals = __cxa_get_globals();
91*404b540aSrobert   __cxa_exception *header = __get_exception_header_from_ue(ue_header);
92*404b540aSrobert   bool native = __is_gxx_exception_class(header->unwindHeader.exception_class);
93*404b540aSrobert 
94*404b540aSrobert 
95*404b540aSrobert   if (native)
96*404b540aSrobert     {
97*404b540aSrobert       header->propagationCount++;
98*404b540aSrobert       // Add it to the chain if this is the first time we've seen this
99*404b540aSrobert       // exception.
100*404b540aSrobert       if (header->propagationCount == 1)
101*404b540aSrobert 	{
102*404b540aSrobert 	  header->nextPropagatingException = globals->propagatingExceptions;
103*404b540aSrobert 	  globals->propagatingExceptions = header;
104*404b540aSrobert 	}
105*404b540aSrobert     }
106*404b540aSrobert   else
107*404b540aSrobert     {
108*404b540aSrobert       // Remember the exception object, so end_cleanup can return it.
109*404b540aSrobert       // These cannot be stacked, so we must abort if we already have
110*404b540aSrobert       // a propagating exception.
111*404b540aSrobert       if (globals->propagatingExceptions)
112*404b540aSrobert 	std::terminate ();
113*404b540aSrobert       globals->propagatingExceptions = header;
114*404b540aSrobert     }
115*404b540aSrobert 
116*404b540aSrobert   return true;
117*404b540aSrobert }
118*404b540aSrobert 
119*404b540aSrobert // Do the work for __cxa_end_cleanup.  Returns the currently propagating
120*404b540aSrobert // exception object.
121*404b540aSrobert extern "C" _Unwind_Exception *
__gnu_end_cleanup(void)122*404b540aSrobert __gnu_end_cleanup(void)
123*404b540aSrobert {
124*404b540aSrobert   __cxa_exception *header;
125*404b540aSrobert   __cxa_eh_globals *globals = __cxa_get_globals();
126*404b540aSrobert 
127*404b540aSrobert   header = globals->propagatingExceptions;
128*404b540aSrobert 
129*404b540aSrobert   // Check something hasn't gone horribly wrong.
130*404b540aSrobert   if (!header)
131*404b540aSrobert     std::terminate();
132*404b540aSrobert 
133*404b540aSrobert   if (__is_gxx_exception_class(header->unwindHeader.exception_class))
134*404b540aSrobert     {
135*404b540aSrobert       header->propagationCount--;
136*404b540aSrobert       if (header->propagationCount == 0)
137*404b540aSrobert 	{
138*404b540aSrobert 	  // Remove exception from chain.
139*404b540aSrobert 	  globals->propagatingExceptions = header->nextPropagatingException;
140*404b540aSrobert 	  header->nextPropagatingException = NULL;
141*404b540aSrobert 	}
142*404b540aSrobert     }
143*404b540aSrobert   else
144*404b540aSrobert     globals->propagatingExceptions = NULL;
145*404b540aSrobert 
146*404b540aSrobert   return &header->unwindHeader;
147*404b540aSrobert }
148*404b540aSrobert 
149*404b540aSrobert // Assembly wrapper to call __gnu_end_cleanup without clobbering r1-r3.
150*404b540aSrobert // Also push r4 to preserve stack alignment.
151*404b540aSrobert #ifdef __thumb__
152*404b540aSrobert asm (".global __cxa_end_cleanup\n"
153*404b540aSrobert "	.type __cxa_end_cleanup, \"function\"\n"
154*404b540aSrobert "	.thumb_func\n"
155*404b540aSrobert "__cxa_end_cleanup:\n"
156*404b540aSrobert "	push\t{r1, r2, r3, r4}\n"
157*404b540aSrobert "	bl\t__gnu_end_cleanup\n"
158*404b540aSrobert "	pop\t{r1, r2, r3, r4}\n"
159*404b540aSrobert "	bl\t_Unwind_Resume @ Never returns\n");
160*404b540aSrobert #else
161*404b540aSrobert asm (".global __cxa_end_cleanup\n"
162*404b540aSrobert "	.type __cxa_end_cleanup, \"function\"\n"
163*404b540aSrobert "__cxa_end_cleanup:\n"
164*404b540aSrobert "	stmfd\tsp!, {r1, r2, r3, r4}\n"
165*404b540aSrobert "	bl\t__gnu_end_cleanup\n"
166*404b540aSrobert "	ldmfd\tsp!, {r1, r2, r3, r4}\n"
167*404b540aSrobert "	bl\t_Unwind_Resume @ Never returns\n");
168*404b540aSrobert #endif
169*404b540aSrobert 
170*404b540aSrobert #endif
171