1 /* Common unwinding code for ARM EABI and C6X.
2 Copyright (C) 2004-2019 Free Software Foundation, Inc.
3 Contributed by Paul Brook
5 This file is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
10 This file is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 Under Section 7 of GPL version 3, you are granted additional
16 permissions described in the GCC Runtime Library Exception, version
17 3.1, as published by the Free Software Foundation.
19 You should have received a copy of the GNU General Public License and
20 a copy of the GCC Runtime Library Exception along with this program;
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 <http://www.gnu.org/licenses/>. */
28 /* Used for SystemTap unwinder probe. */
34 /* Load r7 with rt_sigreturn value. */
35 #define ARM_SET_R7_RT_SIGRETURN 0xe3a070ad /* mov r7, #0xad */
36 #define THUMB2_SET_R7_RT_SIGRETURN 0x07adf04f /* mov.w r7, #0xad */
38 /* FDPIC jump to restorer sequence. */
39 #define FDPIC_LDR_R12_WITH_FUNCDESC 0xe59fc004 /* ldr r12, [pc, #4] */
40 #define FDPIC_LDR_R9_WITH_GOT 0xe59c9004 /* ldr r9, [r12, #4] */
41 #define FDPIC_LDR_PC_WITH_RESTORER 0xe59cf000 /* ldr pc, [r12] */
42 #define FDPIC_T2_LDR_R12_WITH_FUNCDESC 0xc008f8df /* ldr.w r12, [pc, #8] */
43 #define FDPIC_T2_LDR_R9_WITH_GOT 0x9004f8dc /* ldr.w r9, [r12, #4] */
44 #define FDPIC_T2_LDR_PC_WITH_RESTORER 0xf000f8dc /* ldr.w pc, [r12] */
45 #define FDPIC_FUNCDESC_OFFSET 12
47 /* Signal frame offsets. */
48 #define ARM_NEW_RT_SIGFRAME_UCONTEXT 0x80
49 #define ARM_UCONTEXT_SIGCONTEXT 0x14
50 #define ARM_SIGCONTEXT_R0 0xc
53 /* We add a prototype for abort here to avoid creating a dependency on
55 extern void abort (void);
57 /* Definitions for C++ runtime support routines. We make these weak
58 declarations to avoid pulling in libsupc++ unnecessarily. */
59 typedef unsigned char bool;
61 typedef struct _ZSt9type_info type_info; /* This names C++ type_info type */
62 enum __cxa_type_match_result
66 ctm_succeeded_with_ptr_to_base = 2
69 void __attribute__((weak)) __cxa_call_unexpected(_Unwind_Control_Block *ucbp);
70 bool __attribute__((weak)) __cxa_begin_cleanup(_Unwind_Control_Block *ucbp);
71 enum __cxa_type_match_result __attribute__((weak)) __cxa_type_match
72 (_Unwind_Control_Block *ucbp, const type_info *rttip,
73 bool is_reference, void **matched_object);
75 _Unwind_Ptr __attribute__((weak))
76 __gnu_Unwind_Find_exidx (_Unwind_Ptr, int *);
78 #define EXIDX_CANTUNWIND 1
79 #define uint32_highbit (((_uw) 1) << 31)
81 #define UCB_FORCED_STOP_FN(ucbp) ((ucbp)->unwinder_cache.reserved1)
82 #define UCB_PR_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved2)
83 #define UCB_SAVED_CALLSITE_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved3)
84 #define UCB_FORCED_STOP_ARG(ucbp) ((ucbp)->unwinder_cache.reserved4)
85 #define UCB_PR_GOT(ucbp) ((ucbp)->unwinder_cache.reserved5)
87 /* Unwind descriptors. */
101 /* An exception index table entry. */
103 typedef struct __EIT_entry
111 /* Only used in FDPIC case. */
119 /* Assembly helper functions. */
121 /* Restore core register state. Never returns. */
122 void __attribute__((noreturn)) restore_core_regs (struct core_regs *);
125 /* Restore coprocessor state after phase1 unwinding. */
126 static void restore_non_core_regs (phase1_vrs * vrs);
128 /* A better way to do this would probably be to compare the absolute address
129 with a segment relative relocation of the same symbol. */
131 extern int __text_start;
132 extern int __data_start;
134 /* The exception index table location. */
135 extern __EIT_entry __exidx_start;
136 extern __EIT_entry __exidx_end;
138 /* Core unwinding functions. */
140 /* Calculate the address encoded by a 31-bit self-relative offset at address
142 static inline _uw selfrel_offset31 (const _uw *p);
144 static _uw __gnu_unwind_get_pr_addr (int idx);
146 static void _Unwind_DebugHook (void *, void *)
147 __attribute__ ((__noinline__, __used__, __noclone__));
149 /* This function is called during unwinding. It is intended as a hook
150 for a debugger to intercept exceptions. CFA is the CFA of the
151 target frame. HANDLER is the PC to which control will be
155 _Unwind_DebugHook (void *cfa __attribute__ ((__unused__)),
156 void *handler __attribute__ ((__unused__)))
158 /* We only want to use stap probes starting with v3. Earlier
159 versions added too much startup cost. */
160 #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
161 STAP_PROBE2 (libgcc, unwind, cfa, handler);
167 /* This is a wrapper to be called when we need to restore core registers.
168 It will call `_Unwind_DebugHook' before restoring the registers, thus
169 making it possible to intercept and debug exceptions.
171 When calling `_Unwind_DebugHook', the first argument (the CFA) is zero
172 because we are not interested in it. However, it must be there (even
173 being zero) because GDB expects to find it when using the probe. */
175 #define uw_restore_core_regs(TARGET, CORE) \
178 void *handler = __builtin_frob_return_addr ((void *) VRS_PC (TARGET)); \
179 _Unwind_DebugHook (0, handler); \
180 restore_core_regs (CORE); \
184 /* Perform a binary search for RETURN_ADDRESS in TABLE. The table contains
187 static const __EIT_entry *
188 search_EIT_table (const __EIT_entry * table, int nrec, _uw return_address)
195 return (__EIT_entry *) 0;
202 n = (left + right) / 2;
203 this_fn = selfrel_offset31 (&table[n].fnoffset);
205 next_fn = selfrel_offset31 (&table[n + 1].fnoffset) - 1;
207 next_fn = (_uw)0 - 1;
209 if (return_address < this_fn)
212 return (__EIT_entry *) 0;
215 else if (return_address <= next_fn)
223 /* VFP is not restored, but this is sufficient to allow unwinding. */
224 static _Unwind_Reason_Code
225 __gnu_personality_sigframe_fdpic (_Unwind_State state,
226 _Unwind_Control_Block *ucbp,
227 _Unwind_Context *context)
231 unsigned int funcdesc;
232 unsigned int handler;
233 unsigned int first_handler_instruction;
236 _Unwind_VRS_Get (context, _UVRSC_CORE, R_SP, _UVRSD_UINT32, &sp);
237 _Unwind_VRS_Get (context, _UVRSC_CORE, R_PC, _UVRSD_UINT32, &pc);
239 funcdesc = *(unsigned int *)((pc & ~1) + FDPIC_FUNCDESC_OFFSET);
240 handler = *(unsigned int *)(funcdesc);
241 first_handler_instruction = *(unsigned int *)(handler & ~1);
243 /* Adjust SP to point to the start of registers according to
245 if (first_handler_instruction == ARM_SET_R7_RT_SIGRETURN
246 || first_handler_instruction == THUMB2_SET_R7_RT_SIGRETURN)
247 sp += ARM_NEW_RT_SIGFRAME_UCONTEXT
248 + ARM_UCONTEXT_SIGCONTEXT
251 sp += ARM_UCONTEXT_SIGCONTEXT
253 /* Restore regs saved on stack by the kernel. */
254 for (i = 0; i < 16; i++)
255 _Unwind_VRS_Set (context, _UVRSC_CORE, i, _UVRSD_UINT32, sp + 4 * i);
257 return _URC_CONTINUE_UNWIND;
261 /* Find the exception index table eintry for the given address.
262 Fill in the relevant fields of the UCB.
263 Returns _URC_FAILURE if an error occurred, _URC_OK on success. */
265 static _Unwind_Reason_Code
266 get_eit_entry (_Unwind_Control_Block *ucbp, _uw return_address)
268 const __EIT_entry * eitp;
271 /* The return address is the address of the instruction following the
272 call instruction (plus one in thumb mode). If this was the last
273 instruction in the function the address will lie in the following
274 function. Subtract 2 from the address so that it points within the call
275 instruction itself. */
278 if (__gnu_Unwind_Find_exidx)
280 eitp = (const __EIT_entry *) __gnu_Unwind_Find_exidx (return_address,
285 /* If we are unwinding a signal handler then perhaps we have
286 reached a trampoline. Try to detect jump to restorer
288 _uw *pc = (_uw *)((return_address+2) & ~1);
289 if ((pc[0] == FDPIC_LDR_R12_WITH_FUNCDESC
290 && pc[1] == FDPIC_LDR_R9_WITH_GOT
291 && pc[2] == FDPIC_LDR_PC_WITH_RESTORER)
292 || (pc[0] == FDPIC_T2_LDR_R12_WITH_FUNCDESC
293 && pc[1] == FDPIC_T2_LDR_R9_WITH_GOT
294 && pc[2] == FDPIC_T2_LDR_PC_WITH_RESTORER))
296 struct funcdesc_t *funcdesc
297 = (struct funcdesc_t *) &__gnu_personality_sigframe_fdpic;
299 UCB_PR_ADDR (ucbp) = funcdesc->ptr;
300 UCB_PR_GOT (ucbp) = funcdesc->got;
305 UCB_PR_ADDR (ucbp) = 0;
311 eitp = &__exidx_start;
312 nrec = &__exidx_end - &__exidx_start;
315 eitp = search_EIT_table (eitp, nrec, return_address);
320 /* If we are unwinding a signal handler then perhaps we have
321 reached a trampoline. Try to detect jump to restorer
323 _uw *pc = (_uw *)((return_address+2) & ~1);
324 if ((pc[0] == FDPIC_LDR_R12_WITH_FUNCDESC
325 && pc[1] == FDPIC_LDR_R9_WITH_GOT
326 && pc[2] == FDPIC_LDR_PC_WITH_RESTORER)
327 || (pc[0] == FDPIC_T2_LDR_R12_WITH_FUNCDESC
328 && pc[1] == FDPIC_T2_LDR_R9_WITH_GOT
329 && pc[2] == FDPIC_T2_LDR_PC_WITH_RESTORER))
331 struct funcdesc_t *funcdesc
332 = (struct funcdesc_t *) &__gnu_personality_sigframe_fdpic;
334 UCB_PR_ADDR (ucbp) = funcdesc->ptr;
335 UCB_PR_GOT (ucbp) = funcdesc->got;
340 UCB_PR_ADDR (ucbp) = 0;
343 ucbp->pr_cache.fnstart = selfrel_offset31 (&eitp->fnoffset);
345 /* Can this frame be unwound at all? */
346 if (eitp->content == EXIDX_CANTUNWIND)
349 /* If we are unwinding a signal handler then perhaps we have
350 reached a trampoline. Try to detect jump to restorer
352 _uw *pc = (_uw *)((return_address+2) & ~1);
353 if ((pc[0] == FDPIC_LDR_R12_WITH_FUNCDESC
354 && pc[1] == FDPIC_LDR_R9_WITH_GOT
355 && pc[2] == FDPIC_LDR_PC_WITH_RESTORER)
356 || (pc[0] == FDPIC_T2_LDR_R12_WITH_FUNCDESC
357 && pc[1] == FDPIC_T2_LDR_R9_WITH_GOT
358 && pc[2] == FDPIC_T2_LDR_PC_WITH_RESTORER))
360 struct funcdesc_t *funcdesc
361 = (struct funcdesc_t *) &__gnu_personality_sigframe_fdpic;
363 UCB_PR_ADDR (ucbp) = funcdesc->ptr;
364 UCB_PR_GOT (ucbp) = funcdesc->got;
369 UCB_PR_ADDR (ucbp) = 0;
370 return _URC_END_OF_STACK;
373 /* Obtain the address of the "real" __EHT_Header word. */
375 if (eitp->content & uint32_highbit)
377 /* It is immediate data. */
378 ucbp->pr_cache.ehtp = (_Unwind_EHT_Header *)&eitp->content;
379 ucbp->pr_cache.additional = 1;
383 /* The low 31 bits of the content field are a self-relative
384 offset to an _Unwind_EHT_Entry structure. */
385 ucbp->pr_cache.ehtp =
386 (_Unwind_EHT_Header *) selfrel_offset31 (&eitp->content);
387 ucbp->pr_cache.additional = 0;
390 /* Discover the personality routine address. */
391 if (*ucbp->pr_cache.ehtp & (1u << 31))
393 /* One of the predefined standard routines. */
394 _uw idx = (*(_uw *) ucbp->pr_cache.ehtp >> 24) & 0xf;
397 struct funcdesc_t *funcdesc
398 = (struct funcdesc_t *) __gnu_unwind_get_pr_addr (idx);
401 UCB_PR_ADDR (ucbp) = funcdesc->ptr;
402 UCB_PR_GOT (ucbp) = funcdesc->got;
405 UCB_PR_ADDR (ucbp) = 0;
408 UCB_PR_ADDR (ucbp) = __gnu_unwind_get_pr_addr (idx);
410 if (UCB_PR_ADDR (ucbp) == 0)
418 /* Execute region offset to PR */
419 UCB_PR_ADDR (ucbp) = selfrel_offset31 (ucbp->pr_cache.ehtp);
422 = (unsigned int) gnu_Unwind_Find_got ((_Unwind_Ptr) UCB_PR_ADDR (ucbp));
429 /* Perform phase2 unwinding. VRS is the initial virtual register state. */
431 static void __attribute__((noreturn))
432 unwind_phase2 (_Unwind_Control_Block * ucbp, phase2_vrs * vrs)
434 _Unwind_Reason_Code pr_result;
438 /* Find the entry for this routine. */
439 if (get_eit_entry (ucbp, VRS_PC(vrs)) != _URC_OK)
442 UCB_SAVED_CALLSITE_ADDR (ucbp) = VRS_PC(vrs);
444 /* Call the pr to decide what to do. */
447 volatile struct funcdesc_t funcdesc;
448 funcdesc.ptr = UCB_PR_ADDR (ucbp);
449 funcdesc.got = UCB_PR_GOT (ucbp);
450 pr_result = ((personality_routine) &funcdesc)
451 (_US_UNWIND_FRAME_STARTING, ucbp, (_Unwind_Context *) vrs);
454 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
455 (_US_UNWIND_FRAME_STARTING, ucbp, (_Unwind_Context *) vrs);
458 while (pr_result == _URC_CONTINUE_UNWIND);
460 if (pr_result != _URC_INSTALL_CONTEXT)
464 /* r9 could have been lost due to PLT jump. Restore correct value. */
465 vrs->core.r[FDPIC_REGNUM] = gnu_Unwind_Find_got (VRS_PC (vrs));
468 uw_restore_core_regs (vrs, &vrs->core);
471 /* Perform phase2 forced unwinding. */
473 static _Unwind_Reason_Code
474 unwind_phase2_forced (_Unwind_Control_Block *ucbp, phase2_vrs *entry_vrs,
477 _Unwind_Stop_Fn stop_fn = (_Unwind_Stop_Fn) UCB_FORCED_STOP_FN (ucbp);
478 void *stop_arg = (void *)UCB_FORCED_STOP_ARG (ucbp);
479 _Unwind_Reason_Code pr_result = 0;
480 /* We use phase1_vrs here even though we do not demand save, for the
482 phase1_vrs saved_vrs, next_vrs;
484 /* Save the core registers. */
485 saved_vrs.core = entry_vrs->core;
486 /* We don't need to demand-save the non-core registers, because we
487 unwind in a single pass. */
488 saved_vrs.demand_save_flags = 0;
490 /* Unwind until we reach a propagation barrier. */
493 _Unwind_State action;
494 _Unwind_Reason_Code entry_code;
495 _Unwind_Reason_Code stop_code;
497 /* Find the entry for this routine. */
498 entry_code = get_eit_entry (ucbp, VRS_PC (&saved_vrs));
502 action = _US_UNWIND_FRAME_RESUME | _US_FORCE_UNWIND;
506 action = _US_UNWIND_FRAME_STARTING | _US_FORCE_UNWIND;
508 if (entry_code == _URC_OK)
510 UCB_SAVED_CALLSITE_ADDR (ucbp) = VRS_PC (&saved_vrs);
512 next_vrs = saved_vrs;
514 /* Call the pr to decide what to do. */
517 volatile struct funcdesc_t funcdesc;
518 funcdesc.ptr = UCB_PR_ADDR (ucbp);
519 funcdesc.got = UCB_PR_GOT (ucbp);
520 pr_result = ((personality_routine) &funcdesc)
521 (action, ucbp, (void *) &next_vrs);
524 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
525 (action, ucbp, (void *) &next_vrs);
528 saved_vrs.prev_sp = VRS_SP (&next_vrs);
532 /* Treat any failure as the end of unwinding, to cope more
533 gracefully with missing EH information. Mixed EH and
534 non-EH within one object will usually result in failure,
535 because the .ARM.exidx tables do not indicate the end
536 of the code to which they apply; but mixed EH and non-EH
537 shared objects should return an unwind failure at the
538 entry of a non-EH shared object. */
539 action |= _US_END_OF_STACK;
541 saved_vrs.prev_sp = VRS_SP (&saved_vrs);
544 stop_code = stop_fn (1, action, ucbp->exception_class, ucbp,
545 (void *)&saved_vrs, stop_arg);
546 if (stop_code != _URC_NO_REASON)
549 if (entry_code != _URC_OK)
552 saved_vrs = next_vrs;
554 while (pr_result == _URC_CONTINUE_UNWIND);
556 if (pr_result != _URC_INSTALL_CONTEXT)
558 /* Some sort of failure has occurred in the pr and probably the
559 pr returned _URC_FAILURE. */
564 /* r9 could have been lost due to PLT jump. Restore correct value. */
565 saved_vrs.core.r[FDPIC_REGNUM] = gnu_Unwind_Find_got (VRS_PC (&saved_vrs));
568 uw_restore_core_regs (&saved_vrs, &saved_vrs.core);
571 /* This is a very limited implementation of _Unwind_GetCFA. It returns
572 the stack pointer as it is about to be unwound, and is only valid
573 while calling the stop function during forced unwinding. If the
574 current personality routine result is going to run a cleanup, this
575 will not be the CFA; but when the frame is really unwound, it will
579 _Unwind_GetCFA (_Unwind_Context *context)
581 return ((phase1_vrs *) context)->prev_sp;
584 /* Perform phase1 unwinding. UCBP is the exception being thrown, and
585 entry_VRS is the register state on entry to _Unwind_RaiseException. */
588 __gnu_Unwind_RaiseException (_Unwind_Control_Block *, phase2_vrs *);
591 __gnu_Unwind_RaiseException (_Unwind_Control_Block * ucbp,
592 phase2_vrs * entry_vrs)
594 phase1_vrs saved_vrs;
595 _Unwind_Reason_Code pr_result;
597 /* Set the pc to the call site. */
598 VRS_PC (entry_vrs) = VRS_RETURN(entry_vrs);
600 /* Save the core registers. */
601 saved_vrs.core = entry_vrs->core;
602 /* Set demand-save flags. */
603 saved_vrs.demand_save_flags = ~(_uw) 0;
605 /* Unwind until we reach a propagation barrier. */
608 /* Find the entry for this routine. */
609 if (get_eit_entry (ucbp, VRS_PC (&saved_vrs)) != _URC_OK)
612 /* Call the pr to decide what to do. */
615 volatile struct funcdesc_t funcdesc;
616 funcdesc.ptr = UCB_PR_ADDR (ucbp);
617 funcdesc.got = UCB_PR_GOT (ucbp);
618 pr_result = ((personality_routine) &funcdesc)
619 (_US_VIRTUAL_UNWIND_FRAME, ucbp, (void *) &saved_vrs);
622 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
623 (_US_VIRTUAL_UNWIND_FRAME, ucbp, (void *) &saved_vrs);
626 while (pr_result == _URC_CONTINUE_UNWIND);
628 /* We've unwound as far as we want to go, so restore the original
630 restore_non_core_regs (&saved_vrs);
631 if (pr_result != _URC_HANDLER_FOUND)
633 /* Some sort of failure has occurred in the pr and probably the
634 pr returned _URC_FAILURE. */
638 unwind_phase2 (ucbp, entry_vrs);
641 /* Resume unwinding after a cleanup has been run. UCBP is the exception
642 being thrown and ENTRY_VRS is the register state on entry to
645 __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *,
646 _Unwind_Stop_Fn, void *, phase2_vrs *);
649 __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *ucbp,
650 _Unwind_Stop_Fn stop_fn, void *stop_arg,
651 phase2_vrs *entry_vrs)
653 UCB_FORCED_STOP_FN (ucbp) = (_uw) stop_fn;
654 UCB_FORCED_STOP_ARG (ucbp) = (_uw) stop_arg;
656 /* Set the pc to the call site. */
657 VRS_PC (entry_vrs) = VRS_RETURN(entry_vrs);
659 return unwind_phase2_forced (ucbp, entry_vrs, 0);
663 __gnu_Unwind_Resume (_Unwind_Control_Block *, phase2_vrs *);
666 __gnu_Unwind_Resume (_Unwind_Control_Block * ucbp, phase2_vrs * entry_vrs)
668 _Unwind_Reason_Code pr_result;
670 /* Recover the saved address. */
671 VRS_PC (entry_vrs) = UCB_SAVED_CALLSITE_ADDR (ucbp);
673 if (UCB_FORCED_STOP_FN (ucbp))
675 unwind_phase2_forced (ucbp, entry_vrs, 1);
677 /* We can't return failure at this point. */
681 /* Call the cached PR. */
684 volatile struct funcdesc_t funcdesc;
685 funcdesc.ptr = UCB_PR_ADDR (ucbp);
686 funcdesc.got = UCB_PR_GOT (ucbp);
687 pr_result = ((personality_routine) &funcdesc)
688 (_US_UNWIND_FRAME_RESUME, ucbp, (_Unwind_Context *) entry_vrs);
691 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
692 (_US_UNWIND_FRAME_RESUME, ucbp, (_Unwind_Context *) entry_vrs);
697 case _URC_INSTALL_CONTEXT:
698 /* Upload the registers to enter the landing pad. */
700 /* r9 could have been lost due to PLT jump. Restore correct value. */
701 entry_vrs->core.r[FDPIC_REGNUM] = gnu_Unwind_Find_got (VRS_PC (entry_vrs));
703 uw_restore_core_regs (entry_vrs, &entry_vrs->core);
705 case _URC_CONTINUE_UNWIND:
706 /* Continue unwinding the next frame. */
707 unwind_phase2 (ucbp, entry_vrs);
715 __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block *, phase2_vrs *);
718 __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block * ucbp,
719 phase2_vrs * entry_vrs)
721 if (!UCB_FORCED_STOP_FN (ucbp))
722 return __gnu_Unwind_RaiseException (ucbp, entry_vrs);
724 /* Set the pc to the call site. */
725 VRS_PC (entry_vrs) = VRS_RETURN (entry_vrs);
726 /* Continue unwinding the next frame. */
727 return unwind_phase2_forced (ucbp, entry_vrs, 0);
730 /* Clean up an exception object when unwinding is complete. */
732 _Unwind_Complete (_Unwind_Control_Block * ucbp __attribute__((unused)))
737 /* Free an exception. */
740 _Unwind_DeleteException (_Unwind_Exception * exc)
742 if (exc->exception_cleanup)
743 (*exc->exception_cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exc);
747 /* Perform stack backtrace through unwind data. */
749 __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument,
750 phase2_vrs * entry_vrs);
752 __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument,
753 phase2_vrs * entry_vrs)
755 phase1_vrs saved_vrs;
756 _Unwind_Reason_Code code;
758 _Unwind_Control_Block ucb;
759 _Unwind_Control_Block *ucbp = &ucb;
761 /* Set the pc to the call site. */
762 VRS_PC (entry_vrs) = VRS_RETURN (entry_vrs);
764 /* Save the core registers. */
765 saved_vrs.core = entry_vrs->core;
766 /* Set demand-save flags. */
767 saved_vrs.demand_save_flags = ~(_uw) 0;
771 /* Find the entry for this routine. */
772 if (get_eit_entry (ucbp, VRS_PC (&saved_vrs)) != _URC_OK)
778 /* The dwarf unwinder assumes the context structure holds things
779 like the function and LSDA pointers. The ARM implementation
780 caches these in the exception header (UCB). To avoid
781 rewriting everything we make the virtual IP register point at
783 _Unwind_SetGR((_Unwind_Context *)&saved_vrs, UNWIND_POINTER_REG, (_Unwind_Ptr) ucbp);
785 /* Call trace function. */
786 if ((*trace) ((_Unwind_Context *) &saved_vrs, trace_argument)
793 /* Call the pr to decide what to do. */
796 volatile struct funcdesc_t funcdesc;
797 funcdesc.ptr = UCB_PR_ADDR (ucbp);
798 funcdesc.got = UCB_PR_GOT (ucbp);
799 code = ((personality_routine) &funcdesc)
800 (_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND,
801 ucbp, (void *) &saved_vrs);
804 code = ((personality_routine) UCB_PR_ADDR (ucbp))
805 (_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND,
806 ucbp, (void *) &saved_vrs);
809 while (code != _URC_END_OF_STACK
810 && code != _URC_FAILURE);
812 restore_non_core_regs (&saved_vrs);
817 /* Common implementation for ARM ABI defined personality routines.
818 ID is the index of the personality routine, other arguments are as defined
819 by __aeabi_unwind_cpp_pr{0,1,2}. */
821 static _Unwind_Reason_Code
822 __gnu_unwind_pr_common (_Unwind_State state,
823 _Unwind_Control_Block *ucbp,
824 _Unwind_Context *context,
827 __gnu_unwind_state uws;
832 int phase2_call_unexpected_after_unwind = 0;
834 int forced_unwind = state & _US_FORCE_UNWIND;
836 state &= _US_ACTION_MASK;
838 data = (_uw *) ucbp->pr_cache.ehtp;
839 uws.data = *(data++);
849 uws.words_left = (uws.data >> 16) & 0xff;
852 data += uws.words_left;
855 /* Restore the saved pointer. */
856 if (state == _US_UNWIND_FRAME_RESUME)
857 data = (_uw *) ucbp->cleanup_cache.bitpattern[0];
859 if ((ucbp->pr_cache.additional & 1) == 0)
861 /* Process descriptors. */
869 len = ((EHT32 *) data)->length;
870 offset = ((EHT32 *) data)->offset;
875 len = ((EHT16 *) data)->length;
876 offset = ((EHT16 *) data)->offset;
880 fnstart = ucbp->pr_cache.fnstart + (offset & ~1);
881 addr = _Unwind_GetGR (context, R_PC);
882 in_range = (fnstart <= addr && addr < fnstart + (len & ~1));
884 switch (((offset & 1) << 1) | (len & 1))
888 if (state != _US_VIRTUAL_UNWIND_FRAME
891 /* Cleanup in range, and we are running cleanups. */
894 /* Landing pad address is 31-bit pc-relative offset. */
895 lp = selfrel_offset31 (data);
897 /* Save the exception data pointer. */
898 ucbp->cleanup_cache.bitpattern[0] = (_uw) data;
899 if (!__cxa_begin_cleanup (ucbp))
901 /* Setup the VRS to enter the landing pad. */
902 _Unwind_SetGR (context, R_PC, lp);
903 return _URC_INSTALL_CONTEXT;
905 /* Cleanup not in range, or we are in stage 1. */
911 if (state == _US_VIRTUAL_UNWIND_FRAME)
915 /* Check for a barrier. */
917 bool is_reference = (data[0] & uint32_highbit) != 0;
919 enum __cxa_type_match_result match_type;
921 /* Check for no-throw areas. */
922 if (data[1] == (_uw) -2)
925 /* The thrown object immediately follows the ECB. */
926 matched = (void *)(ucbp + 1);
927 if (data[1] != (_uw) -1)
929 /* Match a catch specification. */
930 rtti = _Unwind_decode_typeinfo_ptr (0,
932 match_type = __cxa_type_match (ucbp,
938 match_type = ctm_succeeded;
942 ucbp->barrier_cache.sp =
943 _Unwind_GetGR (context, R_SP);
944 // ctm_succeeded_with_ptr_to_base really
945 // means _c_t_m indirected the pointer
946 // object. We have to reconstruct the
947 // additional pointer layer by using a temporary.
948 if (match_type == ctm_succeeded_with_ptr_to_base)
950 ucbp->barrier_cache.bitpattern[2]
952 ucbp->barrier_cache.bitpattern[0]
953 = (_uw) &ucbp->barrier_cache.bitpattern[2];
956 ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
957 ucbp->barrier_cache.bitpattern[1] = (_uw) data;
958 return _URC_HANDLER_FOUND;
961 /* Handler out of range, or not matched. */
963 else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
964 && ucbp->barrier_cache.bitpattern[1] == (_uw) data)
966 /* Matched a previous propagation barrier. */
969 /* Setup for entry to the handler. */
970 lp = selfrel_offset31 (data);
971 _Unwind_SetGR (context, R_PC, lp);
972 _Unwind_SetGR (context, 0, (_uw) ucbp);
973 return _URC_INSTALL_CONTEXT;
975 /* Catch handler not matched. Advance to the next descriptor. */
980 rtti_count = data[0] & 0x7fffffff;
981 /* Exception specification. */
982 if (state == _US_VIRTUAL_UNWIND_FRAME)
984 if (in_range && (!forced_unwind || !rtti_count))
986 /* Match against the exception specification. */
991 for (i = 0; i < rtti_count; i++)
993 matched = (void *)(ucbp + 1);
994 rtti = _Unwind_decode_typeinfo_ptr (0,
996 if (__cxa_type_match (ucbp, (type_info *) rtti, 0,
1001 if (i == rtti_count)
1003 /* Exception does not match the spec. */
1004 ucbp->barrier_cache.sp =
1005 _Unwind_GetGR (context, R_SP);
1006 ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
1007 ucbp->barrier_cache.bitpattern[1] = (_uw) data;
1008 return _URC_HANDLER_FOUND;
1011 /* Handler out of range, or exception is permitted. */
1013 else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
1014 && ucbp->barrier_cache.bitpattern[1] == (_uw) data)
1016 /* Matched a previous propagation barrier. */
1018 /* Record the RTTI list for __cxa_call_unexpected. */
1019 ucbp->barrier_cache.bitpattern[1] = rtti_count;
1020 ucbp->barrier_cache.bitpattern[2] = 0;
1021 ucbp->barrier_cache.bitpattern[3] = 4;
1022 ucbp->barrier_cache.bitpattern[4] = (_uw) &data[1];
1024 if (data[0] & uint32_highbit)
1026 data += rtti_count + 1;
1027 /* Setup for entry to the handler. */
1028 lp = selfrel_offset31 (data);
1030 _Unwind_SetGR (context, R_PC, lp);
1031 _Unwind_SetGR (context, 0, (_uw) ucbp);
1032 return _URC_INSTALL_CONTEXT;
1035 phase2_call_unexpected_after_unwind = 1;
1037 if (data[0] & uint32_highbit)
1039 data += rtti_count + 1;
1043 /* Should never happen. */
1044 return _URC_FAILURE;
1046 /* Finished processing this descriptor. */
1052 /* 24-bit ecoding */
1053 if (__gnu_unwind_24bit (context, uws.data, id == 4) != _URC_OK)
1054 return _URC_FAILURE;
1058 if (__gnu_unwind_execute (context, &uws) != _URC_OK)
1059 return _URC_FAILURE;
1062 if (phase2_call_unexpected_after_unwind)
1064 /* Enter __cxa_unexpected as if called from the call site. */
1065 _Unwind_SetGR (context, R_LR, _Unwind_GetGR (context, R_PC));
1066 _Unwind_SetGR (context, R_PC, (_uw) &__cxa_call_unexpected);
1067 return _URC_INSTALL_CONTEXT;
1070 return _URC_CONTINUE_UNWIND;