1 /* The implementation of exception handling primitives for Objective-C.
2 Copyright (C) 2004 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 2, 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 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 /* As a special exception, if you link this library with files compiled
22 with GCC to produce an executable, this does not cause the resulting
23 executable to be covered by the GNU General Public License. This
24 exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
29 #include "objc/objc-api.h"
31 #include "unwind-pe.h"
34 #ifdef __ARM_EABI_UNWINDER__
36 const _Unwind_Exception_Class __objc_exception_class
37 = {'G', 'N', 'U', 'C', 'O', 'B', 'J', 'C'};
41 /* This is the exception class we report -- "GNUCOBJC". */
42 static const _Unwind_Exception_Class __objc_exception_class
43 = ((((((((_Unwind_Exception_Class
) 'G'
44 << 8 | (_Unwind_Exception_Class
) 'N')
45 << 8 | (_Unwind_Exception_Class
) 'U')
46 << 8 | (_Unwind_Exception_Class
) 'C')
47 << 8 | (_Unwind_Exception_Class
) 'O')
48 << 8 | (_Unwind_Exception_Class
) 'B')
49 << 8 | (_Unwind_Exception_Class
) 'J')
50 << 8 | (_Unwind_Exception_Class
) 'C');
54 /* This is the object that is passed around by the Objective C runtime
55 to represent the exception in flight. */
59 /* This bit is needed in order to interact with the unwind runtime. */
60 struct _Unwind_Exception base
;
62 /* The actual object we want to throw. Note: must come immediately after
66 #ifdef __ARM_EABI_UNWINDER__
67 /* Note: we use the barrier cache defined in the unwind control block for
70 /* Cache some internal unwind data between phase 1 and phase 2. */
71 _Unwind_Ptr landingPad
;
72 int handlerSwitchValue
;
78 struct lsda_header_info
82 _Unwind_Ptr ttype_base
;
83 const unsigned char *TType
;
84 const unsigned char *action_table
;
85 unsigned char ttype_encoding
;
86 unsigned char call_site_encoding
;
89 static const unsigned char *
90 parse_lsda_header (struct _Unwind_Context
*context
, const unsigned char *p
,
91 struct lsda_header_info
*info
)
94 unsigned char lpstart_encoding
;
96 info
->Start
= (context
? _Unwind_GetRegionStart (context
) : 0);
98 /* Find @LPStart, the base to which landing pad offsets are relative. */
99 lpstart_encoding
= *p
++;
100 if (lpstart_encoding
!= DW_EH_PE_omit
)
101 p
= read_encoded_value (context
, lpstart_encoding
, p
, &info
->LPStart
);
103 info
->LPStart
= info
->Start
;
105 /* Find @TType, the base of the handler and exception spec type data. */
106 info
->ttype_encoding
= *p
++;
107 if (info
->ttype_encoding
!= DW_EH_PE_omit
)
109 p
= read_uleb128 (p
, &tmp
);
110 info
->TType
= p
+ tmp
;
115 /* The encoding and length of the call-site table; the action table
116 immediately follows. */
117 info
->call_site_encoding
= *p
++;
118 p
= read_uleb128 (p
, &tmp
);
119 info
->action_table
= p
+ tmp
;
124 #ifdef __ARM_EABI_UNWINDER__
127 get_ttype_entry (struct lsda_header_info
*info
, _uleb128_t i
)
131 ptr
= (_Unwind_Ptr
) (info
->TType
- (i
* 4));
132 ptr
= _Unwind_decode_target2 (ptr
);
135 return objc_get_class ((const char *) ptr
);
143 get_ttype_entry (struct lsda_header_info
*info
, _Unwind_Word i
)
147 i
*= size_of_encoded_value (info
->ttype_encoding
);
148 read_encoded_value_with_base (info
->ttype_encoding
, info
->ttype_base
,
149 info
->TType
- i
, &ptr
);
151 /* NULL ptr means catch-all. */
153 return objc_get_class ((const char *) ptr
);
160 /* Like unto the method of the same name on Object, but takes an id. */
161 /* ??? Does this bork the meta-type system? Can/should we look up an
162 isKindOf method on the id? */
165 isKindOf (id value
, Class target
)
169 /* NULL target is catch-all. */
173 for (c
= value
->class_pointer
; c
; c
= class_get_super_class (c
))
179 /* Using a different personality function name causes link failures
180 when trying to mix code using different exception handling models. */
181 #ifdef SJLJ_EXCEPTIONS
182 #define PERSONALITY_FUNCTION __gnu_objc_personality_sj0
183 #define __builtin_eh_return_data_regno(x) x
185 #define PERSONALITY_FUNCTION __gnu_objc_personality_v0
188 #ifdef __ARM_EABI_UNWINDER__
190 #define CONTINUE_UNWINDING \
193 if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
194 return _URC_FAILURE; \
195 return _URC_CONTINUE_UNWIND; \
200 PERSONALITY_FUNCTION (_Unwind_State state
,
201 struct _Unwind_Exception
*ue_header
,
202 struct _Unwind_Context
*context
)
205 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
208 PERSONALITY_FUNCTION (int version
,
209 _Unwind_Action actions
,
210 _Unwind_Exception_Class exception_class
,
211 struct _Unwind_Exception
*ue_header
,
212 struct _Unwind_Context
*context
)
215 struct ObjcException
*xh
= (struct ObjcException
*) ue_header
;
217 struct lsda_header_info info
;
218 const unsigned char *language_specific_data
;
219 const unsigned char *action_record
;
220 const unsigned char *p
;
221 _Unwind_Ptr landing_pad
, ip
;
222 int handler_switch_value
;
223 int saw_cleanup
= 0, saw_handler
, foreign_exception
;
225 int ip_before_insn
= 0;
227 #ifdef __ARM_EABI_UNWINDER__
228 _Unwind_Action actions
;
230 switch (state
& _US_ACTION_MASK
)
232 case _US_VIRTUAL_UNWIND_FRAME
:
233 actions
= _UA_SEARCH_PHASE
;
236 case _US_UNWIND_FRAME_STARTING
:
237 actions
= _UA_CLEANUP_PHASE
;
238 if (!(state
& _US_FORCE_UNWIND
)
239 && ue_header
->barrier_cache
.sp
== _Unwind_GetGR (context
, 13))
240 actions
|= _UA_HANDLER_FRAME
;
243 case _US_UNWIND_FRAME_RESUME
:
250 actions
|= state
& _US_FORCE_UNWIND
;
252 /* TODO: Foreign exceptions need some attention (e.g. rethrowing doesn't
254 foreign_exception
= 0;
256 /* The dwarf unwinder assumes the context structure holds things like the
257 function and LSDA pointers. The ARM implementation caches these in
258 the exception header (UCB). To avoid rewriting everything we make the
259 virtual IP register point at the UCB. */
260 ip
= (_Unwind_Ptr
) ue_header
;
261 _Unwind_SetGR (context
, 12, ip
);
263 #else /* !__ARM_EABI_UNWINDER. */
264 /* Interface version check. */
266 return _URC_FATAL_PHASE1_ERROR
;
268 foreign_exception
= (exception_class
!= __objc_exception_class
);
271 /* Shortcut for phase 2 found handler for domestic exception. */
272 if (actions
== (_UA_CLEANUP_PHASE
| _UA_HANDLER_FRAME
)
273 && !foreign_exception
)
275 #ifdef __ARM_EABI_UNWINDER__
276 handler_switch_value
= (int) ue_header
->barrier_cache
.bitpattern
[1];
277 landing_pad
= (_Unwind_Ptr
) ue_header
->barrier_cache
.bitpattern
[3];
279 handler_switch_value
= xh
->handlerSwitchValue
;
280 landing_pad
= xh
->landingPad
;
282 goto install_context
;
285 language_specific_data
= (const unsigned char *)
286 _Unwind_GetLanguageSpecificData (context
);
288 /* If no LSDA, then there are no handlers or cleanups. */
289 if (! language_specific_data
)
292 /* Parse the LSDA header. */
293 p
= parse_lsda_header (context
, language_specific_data
, &info
);
294 info
.ttype_base
= base_of_encoded_value (info
.ttype_encoding
, context
);
295 #ifdef HAVE_GETIPINFO
296 ip
= _Unwind_GetIPInfo (context
, &ip_before_insn
);
298 ip
= _Unwind_GetIP (context
);
304 handler_switch_value
= 0;
306 #ifdef SJLJ_EXCEPTIONS
307 /* The given "IP" is an index into the call-site table, with two
308 exceptions -- -1 means no-action, and 0 means terminate. But
309 since we're using uleb128 values, we've not got random access
312 return _URC_CONTINUE_UNWIND
;
315 _uleb128_t cs_lp
, cs_action
;
318 p
= read_uleb128 (p
, &cs_lp
);
319 p
= read_uleb128 (p
, &cs_action
);
323 /* Can never have null landing pad for sjlj -- that would have
324 been indicated by a -1 call site index. */
325 landing_pad
= cs_lp
+ 1;
327 action_record
= info
.action_table
+ cs_action
- 1;
328 goto found_something
;
331 /* Search the call-site table for the action associated with this IP. */
332 while (p
< info
.action_table
)
334 _Unwind_Ptr cs_start
, cs_len
, cs_lp
;
335 _uleb128_t cs_action
;
337 /* Note that all call-site encodings are "absolute" displacements. */
338 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_start
);
339 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_len
);
340 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_lp
);
341 p
= read_uleb128 (p
, &cs_action
);
343 /* The table is sorted, so if we've passed the ip, stop. */
344 if (ip
< info
.Start
+ cs_start
)
345 p
= info
.action_table
;
346 else if (ip
< info
.Start
+ cs_start
+ cs_len
)
349 landing_pad
= info
.LPStart
+ cs_lp
;
351 action_record
= info
.action_table
+ cs_action
- 1;
352 goto found_something
;
355 #endif /* SJLJ_EXCEPTIONS */
357 /* If ip is not present in the table, C++ would call terminate. */
358 /* ??? As with Java, it's perhaps better to tweek the LSDA to
359 that no-action is mapped to no-entry. */
366 if (landing_pad
== 0)
368 /* If ip is present, and has a null landing pad, there are
369 no cleanups or handlers to be run. */
371 else if (action_record
== 0)
373 /* If ip is present, has a non-null landing pad, and a null
374 action table offset, then there are only cleanups present.
375 Cleanups use a zero switch value, as set above. */
380 /* Otherwise we have a catch handler. */
381 _sleb128_t ar_filter
, ar_disp
;
386 p
= read_sleb128 (p
, &ar_filter
);
387 read_sleb128 (p
, &ar_disp
);
391 /* Zero filter values are cleanups. */
395 /* During forced unwinding, we only run cleanups. With a
396 foreign exception class, we have no class info to match. */
397 else if ((actions
& _UA_FORCE_UNWIND
) || foreign_exception
)
400 else if (ar_filter
> 0)
402 /* Positive filter values are handlers. */
404 Class catch_type
= get_ttype_entry (&info
, ar_filter
);
406 if (isKindOf (xh
->value
, catch_type
))
408 handler_switch_value
= ar_filter
;
415 /* Negative filter values are exception specifications,
416 which Objective-C does not use. */
422 action_record
= p
+ ar_disp
;
426 if (! saw_handler
&& ! saw_cleanup
)
429 if (actions
& _UA_SEARCH_PHASE
)
434 /* For domestic exceptions, we cache data from phase 1 for phase 2. */
435 if (!foreign_exception
)
437 #ifdef __ARM_EABI_UNWINDER__
438 ue_header
->barrier_cache
.sp
= _Unwind_GetGR (context
, 13);
439 ue_header
->barrier_cache
.bitpattern
[1] = (_uw
) handler_switch_value
;
440 ue_header
->barrier_cache
.bitpattern
[3] = (_uw
) landing_pad
;
442 xh
->handlerSwitchValue
= handler_switch_value
;
443 xh
->landingPad
= landing_pad
;
446 return _URC_HANDLER_FOUND
;
450 if (saw_cleanup
== 0)
452 return_object
= xh
->value
;
453 if (!(actions
& _UA_SEARCH_PHASE
))
454 _Unwind_DeleteException(&xh
->base
);
457 _Unwind_SetGR (context
, __builtin_eh_return_data_regno (0),
458 __builtin_extend_pointer (saw_cleanup
? xh
: return_object
));
459 _Unwind_SetGR (context
, __builtin_eh_return_data_regno (1),
460 handler_switch_value
);
461 _Unwind_SetIP (context
, landing_pad
);
462 return _URC_INSTALL_CONTEXT
;
466 __objc_exception_cleanup (_Unwind_Reason_Code code
__attribute__((unused
)),
467 struct _Unwind_Exception
*exc
)
473 objc_exception_throw (id value
)
475 struct ObjcException
*header
= calloc (1, sizeof (*header
));
477 memcpy (&header
->base
.exception_class
, &__objc_exception_class
,
478 sizeof (__objc_exception_class
));
479 header
->base
.exception_cleanup
= __objc_exception_cleanup
;
480 header
->value
= value
;
482 #ifdef SJLJ_EXCEPTIONS
483 _Unwind_SjLj_RaiseException (&header
->base
);
485 _Unwind_RaiseException (&header
->base
);
488 /* Some sort of unwinding error. */