1 /* The implementation of exception handling primitives for Objective-C.
2 Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #include "objc-private/common.h"
28 #include "objc/runtime.h"
29 #include "objc/objc-exception.h"
31 #include "unwind-pe.h"
32 #include <string.h> /* For memcpy */
34 /* 'is_kind_of_exception_matcher' is our default exception matcher -
35 it determines if the object 'exception' is of class 'catch_class',
38 is_kind_of_exception_matcher (Class catch_class
, id exception
)
40 /* NULL catch_class is catch-all (eg, @catch (id object)). */
41 if (catch_class
== Nil
)
44 /* If exception is nil (eg, @throw nil;), then it can only be
45 catched by a catch-all (eg, @catch (id object)). */
50 for (c
= exception
->class_pointer
; c
!= Nil
;
51 c
= class_getSuperclass (c
))
58 /* The exception matcher currently in use. */
59 static objc_exception_matcher
60 __objc_exception_matcher
= is_kind_of_exception_matcher
;
62 objc_exception_matcher
63 objc_setExceptionMatcher (objc_exception_matcher new_matcher
)
65 objc_exception_matcher old_matcher
= __objc_exception_matcher
;
66 __objc_exception_matcher
= new_matcher
;
70 /* The uncaught exception handler currently in use. */
71 static objc_uncaught_exception_handler
72 __objc_uncaught_exception_handler
= NULL
;
74 objc_uncaught_exception_handler
75 objc_setUncaughtExceptionHandler (objc_uncaught_exception_handler
78 objc_uncaught_exception_handler old_handler
79 = __objc_uncaught_exception_handler
;
80 __objc_uncaught_exception_handler
= new_handler
;
86 #ifdef __ARM_EABI_UNWINDER__
88 const _Unwind_Exception_Class __objc_exception_class
89 = {'G', 'N', 'U', 'C', 'O', 'B', 'J', 'C'};
93 /* This is the exception class we report -- "GNUCOBJC". */
94 static const _Unwind_Exception_Class __objc_exception_class
95 = ((((((((_Unwind_Exception_Class
) 'G'
96 << 8 | (_Unwind_Exception_Class
) 'N')
97 << 8 | (_Unwind_Exception_Class
) 'U')
98 << 8 | (_Unwind_Exception_Class
) 'C')
99 << 8 | (_Unwind_Exception_Class
) 'O')
100 << 8 | (_Unwind_Exception_Class
) 'B')
101 << 8 | (_Unwind_Exception_Class
) 'J')
102 << 8 | (_Unwind_Exception_Class
) 'C');
106 /* This is the object that is passed around by the Objective C runtime
107 to represent the exception in flight. */
110 /* This bit is needed in order to interact with the unwind runtime. */
111 struct _Unwind_Exception base
;
113 /* The actual object we want to throw. Note: must come immediately
114 after unwind header. */
117 #ifdef __ARM_EABI_UNWINDER__
118 /* Note: we use the barrier cache defined in the unwind control
119 block for ARM EABI. */
121 /* Cache some internal unwind data between phase 1 and phase 2. */
122 _Unwind_Ptr landingPad
;
123 int handlerSwitchValue
;
129 struct lsda_header_info
133 _Unwind_Ptr ttype_base
;
134 const unsigned char *TType
;
135 const unsigned char *action_table
;
136 unsigned char ttype_encoding
;
137 unsigned char call_site_encoding
;
140 static const unsigned char *
141 parse_lsda_header (struct _Unwind_Context
*context
, const unsigned char *p
,
142 struct lsda_header_info
*info
)
145 unsigned char lpstart_encoding
;
147 info
->Start
= (context
? _Unwind_GetRegionStart (context
) : 0);
149 /* Find @LPStart, the base to which landing pad offsets are
151 lpstart_encoding
= *p
++;
152 if (lpstart_encoding
!= DW_EH_PE_omit
)
153 p
= read_encoded_value (context
, lpstart_encoding
, p
, &info
->LPStart
);
155 info
->LPStart
= info
->Start
;
157 /* Find @TType, the base of the handler and exception spec type
159 info
->ttype_encoding
= *p
++;
160 if (info
->ttype_encoding
!= DW_EH_PE_omit
)
162 p
= read_uleb128 (p
, &tmp
);
163 info
->TType
= p
+ tmp
;
168 /* The encoding and length of the call-site table; the action table
169 immediately follows. */
170 info
->call_site_encoding
= *p
++;
171 p
= read_uleb128 (p
, &tmp
);
172 info
->action_table
= p
+ tmp
;
177 #ifdef __ARM_EABI_UNWINDER__
180 get_ttype_entry (struct lsda_header_info
*info
, _uleb128_t i
)
184 ptr
= (_Unwind_Ptr
) (info
->TType
- (i
* 4));
185 ptr
= _Unwind_decode_target2 (ptr
);
187 /* NULL ptr means catch-all. Note that if the class is not found,
188 this will abort the program. */
190 return objc_getRequiredClass ((const char *) ptr
);
198 get_ttype_entry (struct lsda_header_info
*info
, _Unwind_Word i
)
202 i
*= size_of_encoded_value (info
->ttype_encoding
);
203 read_encoded_value_with_base (info
->ttype_encoding
, info
->ttype_base
,
204 info
->TType
- i
, &ptr
);
206 /* NULL ptr means catch-all. Note that if the class is not found,
207 this will abort the program. */
209 return objc_getRequiredClass ((const char *) ptr
);
216 /* Using a different personality function name causes link failures
217 when trying to mix code using different exception handling
219 #ifdef SJLJ_EXCEPTIONS
220 #define PERSONALITY_FUNCTION __gnu_objc_personality_sj0
221 #define __builtin_eh_return_data_regno(x) x
223 #define PERSONALITY_FUNCTION __gnu_objc_personality_v0
226 #ifdef __ARM_EABI_UNWINDER__
228 #define CONTINUE_UNWINDING \
231 if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
232 return _URC_FAILURE; \
233 return _URC_CONTINUE_UNWIND; \
238 PERSONALITY_FUNCTION (_Unwind_State state
,
239 struct _Unwind_Exception
*ue_header
,
240 struct _Unwind_Context
*context
)
243 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
246 PERSONALITY_FUNCTION (int version
,
247 _Unwind_Action actions
,
248 _Unwind_Exception_Class exception_class
,
249 struct _Unwind_Exception
*ue_header
,
250 struct _Unwind_Context
*context
)
253 struct ObjcException
*xh
= (struct ObjcException
*) ue_header
;
255 struct lsda_header_info info
;
256 const unsigned char *language_specific_data
;
257 const unsigned char *action_record
;
258 const unsigned char *p
;
259 _Unwind_Ptr landing_pad
, ip
;
260 int handler_switch_value
;
261 int saw_cleanup
= 0, saw_handler
, foreign_exception
;
263 int ip_before_insn
= 0;
265 #ifdef __ARM_EABI_UNWINDER__
266 _Unwind_Action actions
;
268 switch (state
& _US_ACTION_MASK
)
270 case _US_VIRTUAL_UNWIND_FRAME
:
271 actions
= _UA_SEARCH_PHASE
;
274 case _US_UNWIND_FRAME_STARTING
:
275 actions
= _UA_CLEANUP_PHASE
;
276 if (!(state
& _US_FORCE_UNWIND
)
277 && ue_header
->barrier_cache
.sp
== _Unwind_GetGR (context
, 13))
278 actions
|= _UA_HANDLER_FRAME
;
281 case _US_UNWIND_FRAME_RESUME
:
288 actions
|= state
& _US_FORCE_UNWIND
;
290 /* TODO: Foreign exceptions need some attention (e.g. rethrowing
292 foreign_exception
= 0;
294 /* The dwarf unwinder assumes the context structure holds things
295 like the function and LSDA pointers. The ARM implementation
296 caches these in the exception header (UCB). To avoid rewriting
297 everything we make the virtual IP register point at the UCB. */
298 ip
= (_Unwind_Ptr
) ue_header
;
299 _Unwind_SetGR (context
, 12, ip
);
301 #else /* !__ARM_EABI_UNWINDER. */
302 /* Interface version check. */
304 return _URC_FATAL_PHASE1_ERROR
;
306 foreign_exception
= (exception_class
!= __objc_exception_class
);
309 /* Shortcut for phase 2 found handler for domestic exception. */
310 if (actions
== (_UA_CLEANUP_PHASE
| _UA_HANDLER_FRAME
)
311 && !foreign_exception
)
313 #ifdef __ARM_EABI_UNWINDER__
314 handler_switch_value
= (int) ue_header
->barrier_cache
.bitpattern
[1];
315 landing_pad
= (_Unwind_Ptr
) ue_header
->barrier_cache
.bitpattern
[3];
317 handler_switch_value
= xh
->handlerSwitchValue
;
318 landing_pad
= xh
->landingPad
;
320 goto install_context
;
323 language_specific_data
= (const unsigned char *)
324 _Unwind_GetLanguageSpecificData (context
);
326 /* If no LSDA, then there are no handlers or cleanups. */
327 if (! language_specific_data
)
330 /* Parse the LSDA header. */
331 p
= parse_lsda_header (context
, language_specific_data
, &info
);
332 info
.ttype_base
= base_of_encoded_value (info
.ttype_encoding
, context
);
333 #ifdef HAVE_GETIPINFO
334 ip
= _Unwind_GetIPInfo (context
, &ip_before_insn
);
336 ip
= _Unwind_GetIP (context
);
342 handler_switch_value
= 0;
344 #ifdef SJLJ_EXCEPTIONS
345 /* The given "IP" is an index into the call-site table, with two
346 exceptions -- -1 means no-action, and 0 means terminate. But
347 since we're using uleb128 values, we've not got random access to
350 return _URC_CONTINUE_UNWIND
;
353 _uleb128_t cs_lp
, cs_action
;
356 p
= read_uleb128 (p
, &cs_lp
);
357 p
= read_uleb128 (p
, &cs_action
);
361 /* Can never have null landing pad for sjlj -- that would have
362 been indicated by a -1 call site index. */
363 landing_pad
= cs_lp
+ 1;
365 action_record
= info
.action_table
+ cs_action
- 1;
366 goto found_something
;
369 /* Search the call-site table for the action associated with this
371 while (p
< info
.action_table
)
373 _Unwind_Ptr cs_start
, cs_len
, cs_lp
;
374 _uleb128_t cs_action
;
376 /* Note that all call-site encodings are "absolute"
378 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_start
);
379 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_len
);
380 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_lp
);
381 p
= read_uleb128 (p
, &cs_action
);
383 /* The table is sorted, so if we've passed the ip, stop. */
384 if (ip
< info
.Start
+ cs_start
)
385 p
= info
.action_table
;
386 else if (ip
< info
.Start
+ cs_start
+ cs_len
)
389 landing_pad
= info
.LPStart
+ cs_lp
;
391 action_record
= info
.action_table
+ cs_action
- 1;
392 goto found_something
;
395 #endif /* SJLJ_EXCEPTIONS */
397 /* If ip is not present in the table, C++ would call terminate. */
398 /* ??? As with Java, it's perhaps better to tweek the LSDA to that
399 no-action is mapped to no-entry. */
406 if (landing_pad
== 0)
408 /* If ip is present, and has a null landing pad, there are no
409 cleanups or handlers to be run. */
411 else if (action_record
== 0)
413 /* If ip is present, has a non-null landing pad, and a null
414 action table offset, then there are only cleanups present.
415 Cleanups use a zero switch value, as set above. */
420 /* Otherwise we have a catch handler. */
421 _sleb128_t ar_filter
, ar_disp
;
426 p
= read_sleb128 (p
, &ar_filter
);
427 read_sleb128 (p
, &ar_disp
);
431 /* Zero filter values are cleanups. */
435 /* During forced unwinding, we only run cleanups. With a
436 foreign exception class, we have no class info to
438 else if ((actions
& _UA_FORCE_UNWIND
) || foreign_exception
)
441 else if (ar_filter
> 0)
443 /* Positive filter values are handlers. */
444 Class catch_type
= get_ttype_entry (&info
, ar_filter
);
446 if ((*__objc_exception_matcher
) (catch_type
, xh
->value
))
448 handler_switch_value
= ar_filter
;
455 /* Negative filter values are exception specifications,
456 which Objective-C does not use. */
462 action_record
= p
+ ar_disp
;
466 if (! saw_handler
&& ! saw_cleanup
)
469 if (actions
& _UA_SEARCH_PHASE
)
474 /* For domestic exceptions, we cache data from phase 1 for phase
476 if (!foreign_exception
)
478 #ifdef __ARM_EABI_UNWINDER__
479 ue_header
->barrier_cache
.sp
= _Unwind_GetGR (context
, 13);
480 ue_header
->barrier_cache
.bitpattern
[1] = (_uw
) handler_switch_value
;
481 ue_header
->barrier_cache
.bitpattern
[3] = (_uw
) landing_pad
;
483 xh
->handlerSwitchValue
= handler_switch_value
;
484 xh
->landingPad
= landing_pad
;
487 return _URC_HANDLER_FOUND
;
491 if (saw_cleanup
== 0)
493 return_object
= xh
->value
;
494 if (!(actions
& _UA_SEARCH_PHASE
))
495 _Unwind_DeleteException(&xh
->base
);
498 _Unwind_SetGR (context
, __builtin_eh_return_data_regno (0),
499 __builtin_extend_pointer (saw_cleanup
? xh
: return_object
));
500 _Unwind_SetGR (context
, __builtin_eh_return_data_regno (1),
501 handler_switch_value
);
502 _Unwind_SetIP (context
, landing_pad
);
503 return _URC_INSTALL_CONTEXT
;
507 __objc_exception_cleanup (_Unwind_Reason_Code code
__attribute__((unused
)),
508 struct _Unwind_Exception
*exc
)
514 objc_exception_throw (id exception
)
516 struct ObjcException
*header
= calloc (1, sizeof (*header
));
518 memcpy (&header
->base
.exception_class
, &__objc_exception_class
,
519 sizeof (__objc_exception_class
));
520 header
->base
.exception_cleanup
= __objc_exception_cleanup
;
521 header
->value
= exception
;
523 #ifdef SJLJ_EXCEPTIONS
524 _Unwind_SjLj_RaiseException (&header
->base
);
526 _Unwind_RaiseException (&header
->base
);
529 /* No exception handler was installed. Call the uncaught exception
530 handler if any is defined. */
531 if (__objc_uncaught_exception_handler
!= 0)
533 (*__objc_uncaught_exception_handler
) (exception
);