]> git.ipfire.org Git - thirdparty/gcc.git/blame - libobjc/exception.c
rs6000: Check -+0 and NaN for smax/smin generation
[thirdparty/gcc.git] / libobjc / exception.c
CommitLineData
a776161b 1/* The implementation of exception handling primitives for Objective-C.
8d9254fc 2 Copyright (C) 2004-2020 Free Software Foundation, Inc.
a776161b
RH
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
748086b7 8Free Software Foundation; either version 3, or (at your option) any
a776161b
RH
9later version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT
12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14License for more details.
15
748086b7
JJ
16Under Section 7 of GPL version 3, you are granted additional
17permissions described in the GCC Runtime Library Exception, version
183.1, as published by the Free Software Foundation.
a776161b 19
748086b7
JJ
20You should have received a copy of the GNU General Public License and
21a copy of the GCC Runtime Library Exception along with this program;
22see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23<http://www.gnu.org/licenses/>. */
a776161b 24
6dead247 25#include "objc-private/common.h"
a776161b 26#include <stdlib.h>
049bc404 27#include "config.h"
be05b0f5 28#include "objc/runtime.h"
e30511ed 29#include "objc/objc-exception.h"
a776161b
RH
30#include "unwind.h"
31#include "unwind-pe.h"
5be9cdc1 32#include <string.h> /* For memcpy */
a776161b 33
e30511ed
NP
34/* 'is_kind_of_exception_matcher' is our default exception matcher -
35 it determines if the object 'exception' is of class 'catch_class',
575584a9 36 or of a subclass. */
e30511ed
NP
37static int
38is_kind_of_exception_matcher (Class catch_class, id exception)
39{
40 /* NULL catch_class is catch-all (eg, @catch (id object)). */
41 if (catch_class == Nil)
42 return 1;
43
575584a9
NP
44 /* If exception is nil (eg, @throw nil;), then it can only be
45 catched by a catch-all (eg, @catch (id object)). */
e30511ed
NP
46 if (exception != nil)
47 {
48 Class c;
49
50 for (c = exception->class_pointer; c != Nil;
be05b0f5 51 c = class_getSuperclass (c))
e30511ed
NP
52 if (c == catch_class)
53 return 1;
54 }
55 return 0;
56}
57
58/* The exception matcher currently in use. */
59static objc_exception_matcher
60__objc_exception_matcher = is_kind_of_exception_matcher;
61
62objc_exception_matcher
2461ab4b 63objc_setExceptionMatcher (objc_exception_matcher new_matcher)
e30511ed
NP
64{
65 objc_exception_matcher old_matcher = __objc_exception_matcher;
66 __objc_exception_matcher = new_matcher;
67 return old_matcher;
68}
69
70/* The uncaught exception handler currently in use. */
71static objc_uncaught_exception_handler
72__objc_uncaught_exception_handler = NULL;
73
74objc_uncaught_exception_handler
2461ab4b
NP
75objc_setUncaughtExceptionHandler (objc_uncaught_exception_handler
76 new_handler)
e30511ed
NP
77{
78 objc_uncaught_exception_handler old_handler
79 = __objc_uncaught_exception_handler;
80 __objc_uncaught_exception_handler = new_handler;
81 return old_handler;
82}
83
84
a776161b 85\f
3f6383d3
JB
86#ifdef __ARM_EABI_UNWINDER__
87
88const _Unwind_Exception_Class __objc_exception_class
89 = {'G', 'N', 'U', 'C', 'O', 'B', 'J', 'C'};
90
91#else
92
a776161b 93/* This is the exception class we report -- "GNUCOBJC". */
3f6383d3
JB
94static 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');
103
104#endif
a776161b
RH
105
106/* This is the object that is passed around by the Objective C runtime
107 to represent the exception in flight. */
a776161b
RH
108struct ObjcException
109{
110 /* This bit is needed in order to interact with the unwind runtime. */
111 struct _Unwind_Exception base;
112
575584a9
NP
113 /* The actual object we want to throw. Note: must come immediately
114 after unwind header. */
a776161b
RH
115 id value;
116
3f6383d3 117#ifdef __ARM_EABI_UNWINDER__
575584a9
NP
118 /* Note: we use the barrier cache defined in the unwind control
119 block for ARM EABI. */
3f6383d3 120#else
a776161b
RH
121 /* Cache some internal unwind data between phase 1 and phase 2. */
122 _Unwind_Ptr landingPad;
123 int handlerSwitchValue;
3f6383d3 124#endif
a776161b
RH
125};
126
127\f
128
129struct lsda_header_info
130{
131 _Unwind_Ptr Start;
132 _Unwind_Ptr LPStart;
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;
138};
139
140static const unsigned char *
141parse_lsda_header (struct _Unwind_Context *context, const unsigned char *p,
142 struct lsda_header_info *info)
143{
30cad60d 144 _uleb128_t tmp;
a776161b
RH
145 unsigned char lpstart_encoding;
146
147 info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
148
575584a9
NP
149 /* Find @LPStart, the base to which landing pad offsets are
150 relative. */
a776161b
RH
151 lpstart_encoding = *p++;
152 if (lpstart_encoding != DW_EH_PE_omit)
153 p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
154 else
155 info->LPStart = info->Start;
156
575584a9
NP
157 /* Find @TType, the base of the handler and exception spec type
158 data. */
a776161b
RH
159 info->ttype_encoding = *p++;
160 if (info->ttype_encoding != DW_EH_PE_omit)
161 {
eb405c46
PB
162#if _GLIBCXX_OVERRIDE_TTYPE_ENCODING
163 /* Older ARM EABI toolchains set this value incorrectly, so use a
164 hardcoded OS-specific format. */
165 info->ttype_encoding = _GLIBCXX_OVERRIDE_TTYPE_ENCODING;
166#endif
a776161b
RH
167 p = read_uleb128 (p, &tmp);
168 info->TType = p + tmp;
169 }
170 else
171 info->TType = 0;
172
173 /* The encoding and length of the call-site table; the action table
174 immediately follows. */
175 info->call_site_encoding = *p++;
176 p = read_uleb128 (p, &tmp);
177 info->action_table = p + tmp;
178
179 return p;
180}
181
182static Class
183get_ttype_entry (struct lsda_header_info *info, _Unwind_Word i)
184{
185 _Unwind_Ptr ptr;
186
187 i *= size_of_encoded_value (info->ttype_encoding);
188 read_encoded_value_with_base (info->ttype_encoding, info->ttype_base,
189 info->TType - i, &ptr);
190
be05b0f5
NP
191 /* NULL ptr means catch-all. Note that if the class is not found,
192 this will abort the program. */
a776161b 193 if (ptr)
be05b0f5 194 return objc_getRequiredClass ((const char *) ptr);
a776161b
RH
195 else
196 return 0;
197}
198
a776161b 199/* Using a different personality function name causes link failures
575584a9
NP
200 when trying to mix code using different exception handling
201 models. */
9b92a9f3 202#ifdef __USING_SJLJ_EXCEPTIONS__
a776161b
RH
203#define PERSONALITY_FUNCTION __gnu_objc_personality_sj0
204#define __builtin_eh_return_data_regno(x) x
9b92a9f3 205#elif defined(__SEH__)
bf1431e3 206#define PERSONALITY_FUNCTION __gnu_objc_personality_imp
a776161b
RH
207#else
208#define PERSONALITY_FUNCTION __gnu_objc_personality_v0
209#endif
210
3f6383d3
JB
211#ifdef __ARM_EABI_UNWINDER__
212
213#define CONTINUE_UNWINDING \
214 do \
215 { \
216 if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
217 return _URC_FAILURE; \
218 return _URC_CONTINUE_UNWIND; \
219 } \
220 while (0)
221
222_Unwind_Reason_Code
48528842 223__attribute__((target ("general-regs-only")))
3f6383d3
JB
224PERSONALITY_FUNCTION (_Unwind_State state,
225 struct _Unwind_Exception *ue_header,
226 struct _Unwind_Context *context)
227#else
228
229#define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
230
3e676928 231#if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
bf1431e3
TG
232static
233#endif
a776161b
RH
234_Unwind_Reason_Code
235PERSONALITY_FUNCTION (int version,
236 _Unwind_Action actions,
237 _Unwind_Exception_Class exception_class,
238 struct _Unwind_Exception *ue_header,
239 struct _Unwind_Context *context)
3f6383d3 240#endif
a776161b
RH
241{
242 struct ObjcException *xh = (struct ObjcException *) ue_header;
243
244 struct lsda_header_info info;
245 const unsigned char *language_specific_data;
246 const unsigned char *action_record;
247 const unsigned char *p;
248 _Unwind_Ptr landing_pad, ip;
249 int handler_switch_value;
3f6383d3 250 int saw_cleanup = 0, saw_handler, foreign_exception;
ee1658f3 251 void *return_object;
3f6383d3
JB
252 int ip_before_insn = 0;
253
254#ifdef __ARM_EABI_UNWINDER__
255 _Unwind_Action actions;
256
257 switch (state & _US_ACTION_MASK)
258 {
259 case _US_VIRTUAL_UNWIND_FRAME:
260 actions = _UA_SEARCH_PHASE;
261 break;
262
263 case _US_UNWIND_FRAME_STARTING:
264 actions = _UA_CLEANUP_PHASE;
265 if (!(state & _US_FORCE_UNWIND)
266 && ue_header->barrier_cache.sp == _Unwind_GetGR (context, 13))
267 actions |= _UA_HANDLER_FRAME;
268 break;
269
270 case _US_UNWIND_FRAME_RESUME:
271 CONTINUE_UNWINDING;
272 break;
273
274 default:
275 abort();
276 }
277 actions |= state & _US_FORCE_UNWIND;
278
575584a9
NP
279 /* TODO: Foreign exceptions need some attention (e.g. rethrowing
280 doesn't work). */
3f6383d3 281 foreign_exception = 0;
a776161b 282
575584a9
NP
283 /* The dwarf unwinder assumes the context structure holds things
284 like the function and LSDA pointers. The ARM implementation
285 caches these in the exception header (UCB). To avoid rewriting
286 everything we make the virtual IP register point at the UCB. */
3f6383d3
JB
287 ip = (_Unwind_Ptr) ue_header;
288 _Unwind_SetGR (context, 12, ip);
289
290#else /* !__ARM_EABI_UNWINDER. */
a776161b
RH
291 /* Interface version check. */
292 if (version != 1)
293 return _URC_FATAL_PHASE1_ERROR;
3f6383d3
JB
294
295 foreign_exception = (exception_class != __objc_exception_class);
296#endif
a776161b
RH
297
298 /* Shortcut for phase 2 found handler for domestic exception. */
299 if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
3f6383d3 300 && !foreign_exception)
a776161b 301 {
3f6383d3
JB
302#ifdef __ARM_EABI_UNWINDER__
303 handler_switch_value = (int) ue_header->barrier_cache.bitpattern[1];
304 landing_pad = (_Unwind_Ptr) ue_header->barrier_cache.bitpattern[3];
305#else
a776161b
RH
306 handler_switch_value = xh->handlerSwitchValue;
307 landing_pad = xh->landingPad;
3f6383d3 308#endif
a776161b
RH
309 goto install_context;
310 }
311
312 language_specific_data = (const unsigned char *)
313 _Unwind_GetLanguageSpecificData (context);
314
315 /* If no LSDA, then there are no handlers or cleanups. */
316 if (! language_specific_data)
3f6383d3 317 CONTINUE_UNWINDING;
a776161b
RH
318
319 /* Parse the LSDA header. */
320 p = parse_lsda_header (context, language_specific_data, &info);
321 info.ttype_base = base_of_encoded_value (info.ttype_encoding, context);
3f6383d3
JB
322#ifdef HAVE_GETIPINFO
323 ip = _Unwind_GetIPInfo (context, &ip_before_insn);
324#else
b01bd5fe 325 ip = _Unwind_GetIP (context);
3f6383d3
JB
326#endif
327 if (!ip_before_insn)
328 --ip;
a776161b
RH
329 landing_pad = 0;
330 action_record = 0;
331 handler_switch_value = 0;
332
9b92a9f3 333#ifdef __USING_SJLJ_EXCEPTIONS__
a776161b
RH
334 /* The given "IP" is an index into the call-site table, with two
335 exceptions -- -1 means no-action, and 0 means terminate. But
575584a9
NP
336 since we're using uleb128 values, we've not got random access to
337 the array. */
a776161b
RH
338 if ((int) ip < 0)
339 return _URC_CONTINUE_UNWIND;
340 else
341 {
30cad60d 342 _uleb128_t cs_lp, cs_action;
a776161b
RH
343 do
344 {
345 p = read_uleb128 (p, &cs_lp);
346 p = read_uleb128 (p, &cs_action);
347 }
348 while (--ip);
349
350 /* Can never have null landing pad for sjlj -- that would have
351 been indicated by a -1 call site index. */
352 landing_pad = cs_lp + 1;
353 if (cs_action)
354 action_record = info.action_table + cs_action - 1;
355 goto found_something;
356 }
357#else
575584a9
NP
358 /* Search the call-site table for the action associated with this
359 IP. */
a776161b
RH
360 while (p < info.action_table)
361 {
362 _Unwind_Ptr cs_start, cs_len, cs_lp;
30cad60d 363 _uleb128_t cs_action;
a776161b 364
575584a9
NP
365 /* Note that all call-site encodings are "absolute"
366 displacements. */
a776161b
RH
367 p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
368 p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
369 p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
370 p = read_uleb128 (p, &cs_action);
371
372 /* The table is sorted, so if we've passed the ip, stop. */
373 if (ip < info.Start + cs_start)
374 p = info.action_table;
375 else if (ip < info.Start + cs_start + cs_len)
376 {
377 if (cs_lp)
378 landing_pad = info.LPStart + cs_lp;
379 if (cs_action)
380 action_record = info.action_table + cs_action - 1;
381 goto found_something;
382 }
383 }
9b92a9f3 384#endif /* __USING_SJLJ_EXCEPTIONS__ */
a776161b
RH
385
386 /* If ip is not present in the table, C++ would call terminate. */
575584a9
NP
387 /* ??? As with Java, it's perhaps better to tweek the LSDA to that
388 no-action is mapped to no-entry. */
3f6383d3 389 CONTINUE_UNWINDING;
a776161b
RH
390
391 found_something:
392 saw_cleanup = 0;
393 saw_handler = 0;
394
395 if (landing_pad == 0)
396 {
575584a9
NP
397 /* If ip is present, and has a null landing pad, there are no
398 cleanups or handlers to be run. */
a776161b
RH
399 }
400 else if (action_record == 0)
401 {
402 /* If ip is present, has a non-null landing pad, and a null
403 action table offset, then there are only cleanups present.
404 Cleanups use a zero switch value, as set above. */
405 saw_cleanup = 1;
406 }
407 else
408 {
409 /* Otherwise we have a catch handler. */
30cad60d 410 _sleb128_t ar_filter, ar_disp;
a776161b
RH
411
412 while (1)
413 {
414 p = action_record;
415 p = read_sleb128 (p, &ar_filter);
416 read_sleb128 (p, &ar_disp);
417
418 if (ar_filter == 0)
419 {
420 /* Zero filter values are cleanups. */
421 saw_cleanup = 1;
422 }
423
424 /* During forced unwinding, we only run cleanups. With a
575584a9
NP
425 foreign exception class, we have no class info to
426 match. */
3f6383d3 427 else if ((actions & _UA_FORCE_UNWIND) || foreign_exception)
a776161b
RH
428 ;
429
430 else if (ar_filter > 0)
431 {
432 /* Positive filter values are handlers. */
a776161b
RH
433 Class catch_type = get_ttype_entry (&info, ar_filter);
434
e30511ed 435 if ((*__objc_exception_matcher) (catch_type, xh->value))
a776161b
RH
436 {
437 handler_switch_value = ar_filter;
438 saw_handler = 1;
439 break;
440 }
441 }
442 else
443 {
444 /* Negative filter values are exception specifications,
445 which Objective-C does not use. */
446 abort ();
447 }
448
449 if (ar_disp == 0)
450 break;
451 action_record = p + ar_disp;
452 }
453 }
454
455 if (! saw_handler && ! saw_cleanup)
3f6383d3 456 CONTINUE_UNWINDING;
a776161b
RH
457
458 if (actions & _UA_SEARCH_PHASE)
459 {
460 if (!saw_handler)
3f6383d3 461 CONTINUE_UNWINDING;
a776161b 462
575584a9
NP
463 /* For domestic exceptions, we cache data from phase 1 for phase
464 2. */
3f6383d3 465 if (!foreign_exception)
a776161b 466 {
3f6383d3
JB
467#ifdef __ARM_EABI_UNWINDER__
468 ue_header->barrier_cache.sp = _Unwind_GetGR (context, 13);
469 ue_header->barrier_cache.bitpattern[1] = (_uw) handler_switch_value;
470 ue_header->barrier_cache.bitpattern[3] = (_uw) landing_pad;
471#else
a776161b
RH
472 xh->handlerSwitchValue = handler_switch_value;
473 xh->landingPad = landing_pad;
3f6383d3 474#endif
a776161b
RH
475 }
476 return _URC_HANDLER_FOUND;
477 }
478
479 install_context:
ee1658f3
MK
480 if (saw_cleanup == 0)
481 {
482 return_object = xh->value;
483 if (!(actions & _UA_SEARCH_PHASE))
484 _Unwind_DeleteException(&xh->base);
485 }
486
a776161b 487 _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
ee1658f3 488 __builtin_extend_pointer (saw_cleanup ? xh : return_object));
a776161b
RH
489 _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
490 handler_switch_value);
491 _Unwind_SetIP (context, landing_pad);
492 return _URC_INSTALL_CONTEXT;
493}
494
495static void
496__objc_exception_cleanup (_Unwind_Reason_Code code __attribute__((unused)),
497 struct _Unwind_Exception *exc)
498{
499 free (exc);
500}
501
502void
e30511ed 503objc_exception_throw (id exception)
a776161b
RH
504{
505 struct ObjcException *header = calloc (1, sizeof (*header));
e30511ed 506
3f6383d3
JB
507 memcpy (&header->base.exception_class, &__objc_exception_class,
508 sizeof (__objc_exception_class));
a776161b 509 header->base.exception_cleanup = __objc_exception_cleanup;
e30511ed 510 header->value = exception;
a776161b 511
9b92a9f3 512#ifdef __USING_SJLJ_EXCEPTIONS__
a776161b
RH
513 _Unwind_SjLj_RaiseException (&header->base);
514#else
515 _Unwind_RaiseException (&header->base);
516#endif
517
e30511ed 518 /* No exception handler was installed. Call the uncaught exception
575584a9 519 handler if any is defined. */
e30511ed
NP
520 if (__objc_uncaught_exception_handler != 0)
521 {
522 (*__objc_uncaught_exception_handler) (exception);
523 }
524
a776161b
RH
525 abort ();
526}
e30511ed 527
3e676928 528#if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
bf1431e3
TG
529EXCEPTION_DISPOSITION
530__gnu_objc_personality_seh0 (PEXCEPTION_RECORD ms_exc, void *this_frame,
531 PCONTEXT ms_orig_context,
532 PDISPATCHER_CONTEXT ms_disp)
533{
534 return _GCC_specific_handler (ms_exc, this_frame, ms_orig_context,
535 ms_disp, __gnu_objc_personality_imp);
536}
3e676928 537#endif