1 /* Common unwinding code for ARM EABI and C6X.
2 Copyright (C) 2004-2021 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 /* Definitions for C++ runtime support routines. We make these weak
54 declarations to avoid pulling in libsupc++ unnecessarily. */
55 typedef unsigned char bool;
57 typedef struct _ZSt9type_info type_info; /* This names C++ type_info type */
58 enum __cxa_type_match_result
62 ctm_succeeded_with_ptr_to_base = 2
65 void __attribute__((weak)) __cxa_call_unexpected(_Unwind_Control_Block *ucbp);
66 bool __attribute__((weak)) __cxa_begin_cleanup(_Unwind_Control_Block *ucbp);
67 enum __cxa_type_match_result __attribute__((weak)) __cxa_type_match
68 (_Unwind_Control_Block *ucbp, const type_info *rttip,
69 bool is_reference, void **matched_object);
71 _Unwind_Ptr __attribute__((weak))
72 __gnu_Unwind_Find_exidx (_Unwind_Ptr, int *);
74 #define EXIDX_CANTUNWIND 1
75 #define uint32_highbit (((_uw) 1) << 31)
77 #define UCB_FORCED_STOP_FN(ucbp) ((ucbp)->unwinder_cache.reserved1)
78 #define UCB_PR_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved2)
79 #define UCB_SAVED_CALLSITE_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved3)
80 #define UCB_FORCED_STOP_ARG(ucbp) ((ucbp)->unwinder_cache.reserved4)
81 #define UCB_PR_GOT(ucbp) ((ucbp)->unwinder_cache.reserved5)
83 /* Unwind descriptors. */
97 /* An exception index table entry. */
99 typedef struct __EIT_entry
107 /* Only used in FDPIC case. */
115 /* Assembly helper functions. */
117 /* Restore core register state. Never returns. */
118 void __attribute__((noreturn)) restore_core_regs (struct core_regs *);
121 /* Restore coprocessor state after phase1 unwinding. */
122 static void restore_non_core_regs (phase1_vrs * vrs);
124 /* A better way to do this would probably be to compare the absolute address
125 with a segment relative relocation of the same symbol. */
127 extern int __text_start;
128 extern int __data_start;
130 /* The exception index table location. */
131 extern __EIT_entry __exidx_start;
132 extern __EIT_entry __exidx_end;
134 /* Core unwinding functions. */
136 /* Calculate the address encoded by a 31-bit self-relative offset at address
138 static inline _uw selfrel_offset31 (const _uw *p);
140 static _uw __gnu_unwind_get_pr_addr (int idx);
142 static void _Unwind_DebugHook (void *, void *)
143 __attribute__ ((__noinline__, __used__, __noclone__));
145 /* This function is called during unwinding. It is intended as a hook
146 for a debugger to intercept exceptions. CFA is the CFA of the
147 target frame. HANDLER is the PC to which control will be
151 _Unwind_DebugHook (void *cfa __attribute__ ((__unused__)),
152 void *handler __attribute__ ((__unused__)))
154 /* We only want to use stap probes starting with v3. Earlier
155 versions added too much startup cost. */
156 #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
157 STAP_PROBE2 (libgcc, unwind, cfa, handler);
163 /* This is a wrapper to be called when we need to restore core registers.
164 It will call `_Unwind_DebugHook' before restoring the registers, thus
165 making it possible to intercept and debug exceptions.
167 When calling `_Unwind_DebugHook', the first argument (the CFA) is zero
168 because we are not interested in it. However, it must be there (even
169 being zero) because GDB expects to find it when using the probe. */
171 #define uw_restore_core_regs(TARGET, CORE) \
174 void *handler = __builtin_frob_return_addr ((void *) VRS_PC (TARGET)); \
175 _Unwind_DebugHook (0, handler); \
176 restore_core_regs (CORE); \
180 /* Perform a binary search for RETURN_ADDRESS in TABLE. The table contains
183 static const __EIT_entry *
184 search_EIT_table (const __EIT_entry * table, int nrec, _uw return_address)
191 return (__EIT_entry *) 0;
198 n = (left + right) / 2;
199 this_fn = selfrel_offset31 (&table[n].fnoffset);
201 next_fn = selfrel_offset31 (&table[n + 1].fnoffset) - 1;
203 next_fn = (_uw)0 - 1;
205 if (return_address < this_fn)
208 return (__EIT_entry *) 0;
211 else if (return_address <= next_fn)
219 /* VFP is not restored, but this is sufficient to allow unwinding. */
220 static _Unwind_Reason_Code
221 __gnu_personality_sigframe_fdpic (_Unwind_State state,
222 _Unwind_Control_Block *ucbp,
223 _Unwind_Context *context)
227 unsigned int funcdesc;
228 unsigned int handler;
229 unsigned int first_handler_instruction;
232 _Unwind_VRS_Get (context, _UVRSC_CORE, R_SP, _UVRSD_UINT32, &sp);
233 _Unwind_VRS_Get (context, _UVRSC_CORE, R_PC, _UVRSD_UINT32, &pc);
235 funcdesc = *(unsigned int *)((pc & ~1) + FDPIC_FUNCDESC_OFFSET);
236 handler = *(unsigned int *)(funcdesc);
237 first_handler_instruction = *(unsigned int *)(handler & ~1);
239 /* Adjust SP to point to the start of registers according to
241 if (first_handler_instruction == ARM_SET_R7_RT_SIGRETURN
242 || first_handler_instruction == THUMB2_SET_R7_RT_SIGRETURN)
243 sp += ARM_NEW_RT_SIGFRAME_UCONTEXT
244 + ARM_UCONTEXT_SIGCONTEXT
247 sp += ARM_UCONTEXT_SIGCONTEXT
249 /* Restore regs saved on stack by the kernel. */
250 for (i = 0; i < 16; i++)
251 _Unwind_VRS_Set (context, _UVRSC_CORE, i, _UVRSD_UINT32, sp + 4 * i);
253 return _URC_CONTINUE_UNWIND;
257 /* Find the exception index table eintry for the given address.
258 Fill in the relevant fields of the UCB.
259 Returns _URC_FAILURE if an error occurred, _URC_OK on success. */
261 static _Unwind_Reason_Code
262 get_eit_entry (_Unwind_Control_Block *ucbp, _uw return_address)
264 const __EIT_entry * eitp;
267 /* The return address is the address of the instruction following the
268 call instruction (plus one in thumb mode). If this was the last
269 instruction in the function the address will lie in the following
270 function. Subtract 2 from the address so that it points within the call
271 instruction itself. */
274 if (__gnu_Unwind_Find_exidx)
276 eitp = (const __EIT_entry *) __gnu_Unwind_Find_exidx (return_address,
281 /* If we are unwinding a signal handler then perhaps we have
282 reached a trampoline. Try to detect jump to restorer
284 _uw *pc = (_uw *)((return_address+2) & ~1);
285 if ((pc[0] == FDPIC_LDR_R12_WITH_FUNCDESC
286 && pc[1] == FDPIC_LDR_R9_WITH_GOT
287 && pc[2] == FDPIC_LDR_PC_WITH_RESTORER)
288 || (pc[0] == FDPIC_T2_LDR_R12_WITH_FUNCDESC
289 && pc[1] == FDPIC_T2_LDR_R9_WITH_GOT
290 && pc[2] == FDPIC_T2_LDR_PC_WITH_RESTORER))
292 struct funcdesc_t *funcdesc
293 = (struct funcdesc_t *) &__gnu_personality_sigframe_fdpic;
295 UCB_PR_ADDR (ucbp) = funcdesc->ptr;
296 UCB_PR_GOT (ucbp) = funcdesc->got;
301 UCB_PR_ADDR (ucbp) = 0;
307 eitp = &__exidx_start;
308 nrec = &__exidx_end - &__exidx_start;
311 eitp = search_EIT_table (eitp, nrec, return_address);
316 /* If we are unwinding a signal handler then perhaps we have
317 reached a trampoline. Try to detect jump to restorer
319 _uw *pc = (_uw *)((return_address+2) & ~1);
320 if ((pc[0] == FDPIC_LDR_R12_WITH_FUNCDESC
321 && pc[1] == FDPIC_LDR_R9_WITH_GOT
322 && pc[2] == FDPIC_LDR_PC_WITH_RESTORER)
323 || (pc[0] == FDPIC_T2_LDR_R12_WITH_FUNCDESC
324 && pc[1] == FDPIC_T2_LDR_R9_WITH_GOT
325 && pc[2] == FDPIC_T2_LDR_PC_WITH_RESTORER))
327 struct funcdesc_t *funcdesc
328 = (struct funcdesc_t *) &__gnu_personality_sigframe_fdpic;
330 UCB_PR_ADDR (ucbp) = funcdesc->ptr;
331 UCB_PR_GOT (ucbp) = funcdesc->got;
336 UCB_PR_ADDR (ucbp) = 0;
339 ucbp->pr_cache.fnstart = selfrel_offset31 (&eitp->fnoffset);
341 /* Can this frame be unwound at all? */
342 if (eitp->content == EXIDX_CANTUNWIND)
345 /* If we are unwinding a signal handler then perhaps we have
346 reached a trampoline. Try to detect jump to restorer
348 _uw *pc = (_uw *)((return_address+2) & ~1);
349 if ((pc[0] == FDPIC_LDR_R12_WITH_FUNCDESC
350 && pc[1] == FDPIC_LDR_R9_WITH_GOT
351 && pc[2] == FDPIC_LDR_PC_WITH_RESTORER)
352 || (pc[0] == FDPIC_T2_LDR_R12_WITH_FUNCDESC
353 && pc[1] == FDPIC_T2_LDR_R9_WITH_GOT
354 && pc[2] == FDPIC_T2_LDR_PC_WITH_RESTORER))
356 struct funcdesc_t *funcdesc
357 = (struct funcdesc_t *) &__gnu_personality_sigframe_fdpic;
359 UCB_PR_ADDR (ucbp) = funcdesc->ptr;
360 UCB_PR_GOT (ucbp) = funcdesc->got;
365 UCB_PR_ADDR (ucbp) = 0;
366 return _URC_END_OF_STACK;
369 /* Obtain the address of the "real" __EHT_Header word. */
371 if (eitp->content & uint32_highbit)
373 /* It is immediate data. */
374 ucbp->pr_cache.ehtp = (_Unwind_EHT_Header *)&eitp->content;
375 ucbp->pr_cache.additional = 1;
379 /* The low 31 bits of the content field are a self-relative
380 offset to an _Unwind_EHT_Entry structure. */
381 ucbp->pr_cache.ehtp =
382 (_Unwind_EHT_Header *) selfrel_offset31 (&eitp->content);
383 ucbp->pr_cache.additional = 0;
386 /* Discover the personality routine address. */
387 if (*ucbp->pr_cache.ehtp & (1u << 31))
389 /* One of the predefined standard routines. */
390 _uw idx = (*(_uw *) ucbp->pr_cache.ehtp >> 24) & 0xf;
393 struct funcdesc_t *funcdesc
394 = (struct funcdesc_t *) __gnu_unwind_get_pr_addr (idx);
397 UCB_PR_ADDR (ucbp) = funcdesc->ptr;
398 UCB_PR_GOT (ucbp) = funcdesc->got;
401 UCB_PR_ADDR (ucbp) = 0;
404 UCB_PR_ADDR (ucbp) = __gnu_unwind_get_pr_addr (idx);
406 if (UCB_PR_ADDR (ucbp) == 0)
414 /* Execute region offset to PR */
415 UCB_PR_ADDR (ucbp) = selfrel_offset31 (ucbp->pr_cache.ehtp);
418 = (unsigned int) _Unwind_gnu_Find_got ((_Unwind_Ptr) UCB_PR_ADDR (ucbp));
425 /* Perform phase2 unwinding. VRS is the initial virtual register state. */
427 static void __attribute__((noreturn))
428 unwind_phase2 (_Unwind_Control_Block * ucbp, phase2_vrs * vrs)
430 _Unwind_Reason_Code pr_result;
434 /* Find the entry for this routine. */
435 if (get_eit_entry (ucbp, VRS_PC(vrs)) != _URC_OK)
438 UCB_SAVED_CALLSITE_ADDR (ucbp) = VRS_PC(vrs);
440 /* Call the pr to decide what to do. */
443 volatile struct funcdesc_t funcdesc;
444 funcdesc.ptr = UCB_PR_ADDR (ucbp);
445 funcdesc.got = UCB_PR_GOT (ucbp);
446 pr_result = ((personality_routine) &funcdesc)
447 (_US_UNWIND_FRAME_STARTING, ucbp, (_Unwind_Context *) vrs);
450 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
451 (_US_UNWIND_FRAME_STARTING, ucbp, (_Unwind_Context *) vrs);
454 while (pr_result == _URC_CONTINUE_UNWIND);
456 if (pr_result != _URC_INSTALL_CONTEXT)
460 /* r9 could have been lost due to PLT jump. Restore correct value. */
461 vrs->core.r[FDPIC_REGNUM] = _Unwind_gnu_Find_got (VRS_PC (vrs));
464 uw_restore_core_regs (vrs, &vrs->core);
467 /* Perform phase2 forced unwinding. */
469 static _Unwind_Reason_Code
470 unwind_phase2_forced (_Unwind_Control_Block *ucbp, phase2_vrs *entry_vrs,
473 _Unwind_Stop_Fn stop_fn = (_Unwind_Stop_Fn) UCB_FORCED_STOP_FN (ucbp);
474 void *stop_arg = (void *)UCB_FORCED_STOP_ARG (ucbp);
475 _Unwind_Reason_Code pr_result = 0;
476 /* We use phase1_vrs here even though we do not demand save, for the
478 phase1_vrs saved_vrs, next_vrs;
480 /* Save the core registers. */
481 saved_vrs.core = entry_vrs->core;
482 /* We don't need to demand-save the non-core registers, because we
483 unwind in a single pass. */
484 saved_vrs.demand_save_flags = 0;
486 /* Unwind until we reach a propagation barrier. */
489 _Unwind_State action;
490 _Unwind_Reason_Code entry_code;
491 _Unwind_Reason_Code stop_code;
493 /* Find the entry for this routine. */
494 entry_code = get_eit_entry (ucbp, VRS_PC (&saved_vrs));
498 action = _US_UNWIND_FRAME_RESUME | _US_FORCE_UNWIND;
502 action = _US_UNWIND_FRAME_STARTING | _US_FORCE_UNWIND;
504 if (entry_code == _URC_OK)
506 UCB_SAVED_CALLSITE_ADDR (ucbp) = VRS_PC (&saved_vrs);
508 next_vrs = saved_vrs;
510 /* Call the pr to decide what to do. */
513 volatile struct funcdesc_t funcdesc;
514 funcdesc.ptr = UCB_PR_ADDR (ucbp);
515 funcdesc.got = UCB_PR_GOT (ucbp);
516 pr_result = ((personality_routine) &funcdesc)
517 (action, ucbp, (void *) &next_vrs);
520 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
521 (action, ucbp, (void *) &next_vrs);
524 saved_vrs.prev_sp = VRS_SP (&next_vrs);
528 /* Treat any failure as the end of unwinding, to cope more
529 gracefully with missing EH information. Mixed EH and
530 non-EH within one object will usually result in failure,
531 because the .ARM.exidx tables do not indicate the end
532 of the code to which they apply; but mixed EH and non-EH
533 shared objects should return an unwind failure at the
534 entry of a non-EH shared object. */
535 action |= _US_END_OF_STACK;
537 saved_vrs.prev_sp = VRS_SP (&saved_vrs);
540 stop_code = stop_fn (1, action, ucbp->exception_class, ucbp,
541 (void *)&saved_vrs, stop_arg);
542 if (stop_code != _URC_NO_REASON)
545 if (entry_code != _URC_OK)
548 saved_vrs = next_vrs;
550 while (pr_result == _URC_CONTINUE_UNWIND);
552 if (pr_result != _URC_INSTALL_CONTEXT)
554 /* Some sort of failure has occurred in the pr and probably the
555 pr returned _URC_FAILURE. */
560 /* r9 could have been lost due to PLT jump. Restore correct value. */
561 saved_vrs.core.r[FDPIC_REGNUM] = _Unwind_gnu_Find_got (VRS_PC (&saved_vrs));
564 uw_restore_core_regs (&saved_vrs, &saved_vrs.core);
567 /* This is a very limited implementation of _Unwind_GetCFA. It returns
568 the stack pointer as it is about to be unwound, and is only valid
569 while calling the stop function during forced unwinding. If the
570 current personality routine result is going to run a cleanup, this
571 will not be the CFA; but when the frame is really unwound, it will
575 _Unwind_GetCFA (_Unwind_Context *context)
577 return ((phase1_vrs *) context)->prev_sp;
580 /* Perform phase1 unwinding. UCBP is the exception being thrown, and
581 entry_VRS is the register state on entry to _Unwind_RaiseException. */
584 __gnu_Unwind_RaiseException (_Unwind_Control_Block *, phase2_vrs *);
587 __gnu_Unwind_RaiseException (_Unwind_Control_Block * ucbp,
588 phase2_vrs * entry_vrs)
590 phase1_vrs saved_vrs;
591 _Unwind_Reason_Code pr_result;
593 /* Set the pc to the call site. */
594 VRS_PC (entry_vrs) = VRS_RETURN(entry_vrs);
596 /* Save the core registers. */
597 saved_vrs.core = entry_vrs->core;
598 /* Set demand-save flags. */
599 saved_vrs.demand_save_flags = ~(_uw) 0;
601 /* Unwind until we reach a propagation barrier. */
604 /* Find the entry for this routine. */
605 if (get_eit_entry (ucbp, VRS_PC (&saved_vrs)) != _URC_OK)
608 /* Call the pr to decide what to do. */
611 volatile struct funcdesc_t funcdesc;
612 funcdesc.ptr = UCB_PR_ADDR (ucbp);
613 funcdesc.got = UCB_PR_GOT (ucbp);
614 pr_result = ((personality_routine) &funcdesc)
615 (_US_VIRTUAL_UNWIND_FRAME, ucbp, (void *) &saved_vrs);
618 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
619 (_US_VIRTUAL_UNWIND_FRAME, ucbp, (void *) &saved_vrs);
622 while (pr_result == _URC_CONTINUE_UNWIND);
624 /* We've unwound as far as we want to go, so restore the original
626 restore_non_core_regs (&saved_vrs);
627 if (pr_result != _URC_HANDLER_FOUND)
629 /* Some sort of failure has occurred in the pr and probably the
630 pr returned _URC_FAILURE. */
634 unwind_phase2 (ucbp, entry_vrs);
637 /* Resume unwinding after a cleanup has been run. UCBP is the exception
638 being thrown and ENTRY_VRS is the register state on entry to
641 __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *,
642 _Unwind_Stop_Fn, void *, phase2_vrs *);
645 __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *ucbp,
646 _Unwind_Stop_Fn stop_fn, void *stop_arg,
647 phase2_vrs *entry_vrs)
649 UCB_FORCED_STOP_FN (ucbp) = (_uw) stop_fn;
650 UCB_FORCED_STOP_ARG (ucbp) = (_uw) stop_arg;
652 /* Set the pc to the call site. */
653 VRS_PC (entry_vrs) = VRS_RETURN(entry_vrs);
655 return unwind_phase2_forced (ucbp, entry_vrs, 0);
659 __gnu_Unwind_Resume (_Unwind_Control_Block *, phase2_vrs *);
662 __gnu_Unwind_Resume (_Unwind_Control_Block * ucbp, phase2_vrs * entry_vrs)
664 _Unwind_Reason_Code pr_result;
666 /* Recover the saved address. */
667 VRS_PC (entry_vrs) = UCB_SAVED_CALLSITE_ADDR (ucbp);
669 if (UCB_FORCED_STOP_FN (ucbp))
671 unwind_phase2_forced (ucbp, entry_vrs, 1);
673 /* We can't return failure at this point. */
677 /* Call the cached PR. */
680 volatile struct funcdesc_t funcdesc;
681 funcdesc.ptr = UCB_PR_ADDR (ucbp);
682 funcdesc.got = UCB_PR_GOT (ucbp);
683 pr_result = ((personality_routine) &funcdesc)
684 (_US_UNWIND_FRAME_RESUME, ucbp, (_Unwind_Context *) entry_vrs);
687 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
688 (_US_UNWIND_FRAME_RESUME, ucbp, (_Unwind_Context *) entry_vrs);
693 case _URC_INSTALL_CONTEXT:
694 /* Upload the registers to enter the landing pad. */
696 /* r9 could have been lost due to PLT jump. Restore correct value. */
697 entry_vrs->core.r[FDPIC_REGNUM] = _Unwind_gnu_Find_got (VRS_PC (entry_vrs));
699 uw_restore_core_regs (entry_vrs, &entry_vrs->core);
701 case _URC_CONTINUE_UNWIND:
702 /* Continue unwinding the next frame. */
703 unwind_phase2 (ucbp, entry_vrs);
711 __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block *, phase2_vrs *);
714 __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block * ucbp,
715 phase2_vrs * entry_vrs)
717 if (!UCB_FORCED_STOP_FN (ucbp))
718 return __gnu_Unwind_RaiseException (ucbp, entry_vrs);
720 /* Set the pc to the call site. */
721 VRS_PC (entry_vrs) = VRS_RETURN (entry_vrs);
722 /* Continue unwinding the next frame. */
723 return unwind_phase2_forced (ucbp, entry_vrs, 0);
726 /* Clean up an exception object when unwinding is complete. */
728 _Unwind_Complete (_Unwind_Control_Block * ucbp __attribute__((unused)))
733 /* Free an exception. */
736 _Unwind_DeleteException (_Unwind_Exception * exc)
738 if (exc->exception_cleanup)
739 (*exc->exception_cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exc);
743 /* Perform stack backtrace through unwind data. */
745 __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument,
746 phase2_vrs * entry_vrs);
748 __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument,
749 phase2_vrs * entry_vrs)
751 phase1_vrs saved_vrs;
752 _Unwind_Reason_Code code;
754 _Unwind_Control_Block ucb;
755 _Unwind_Control_Block *ucbp = &ucb;
757 /* Set the pc to the call site. */
758 VRS_PC (entry_vrs) = VRS_RETURN (entry_vrs);
760 /* Save the core registers. */
761 saved_vrs.core = entry_vrs->core;
762 /* Set demand-save flags. */
763 saved_vrs.demand_save_flags = ~(_uw) 0;
767 /* Find the entry for this routine. */
768 if (get_eit_entry (ucbp, VRS_PC (&saved_vrs)) != _URC_OK)
774 /* The dwarf unwinder assumes the context structure holds things
775 like the function and LSDA pointers. The ARM implementation
776 caches these in the exception header (UCB). To avoid
777 rewriting everything we make the virtual IP register point at
779 _Unwind_SetGR((_Unwind_Context *)&saved_vrs, UNWIND_POINTER_REG, (_Unwind_Ptr) ucbp);
781 /* Call trace function. */
782 if ((*trace) ((_Unwind_Context *) &saved_vrs, trace_argument)
789 /* Call the pr to decide what to do. */
792 volatile struct funcdesc_t funcdesc;
793 funcdesc.ptr = UCB_PR_ADDR (ucbp);
794 funcdesc.got = UCB_PR_GOT (ucbp);
795 code = ((personality_routine) &funcdesc)
796 (_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND,
797 ucbp, (void *) &saved_vrs);
800 code = ((personality_routine) UCB_PR_ADDR (ucbp))
801 (_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND,
802 ucbp, (void *) &saved_vrs);
805 while (code != _URC_END_OF_STACK
806 && code != _URC_FAILURE);
808 restore_non_core_regs (&saved_vrs);
813 /* Common implementation for ARM ABI defined personality routines.
814 ID is the index of the personality routine, other arguments are as defined
815 by __aeabi_unwind_cpp_pr{0,1,2}. */
817 static _Unwind_Reason_Code
818 __gnu_unwind_pr_common (_Unwind_State state,
819 _Unwind_Control_Block *ucbp,
820 _Unwind_Context *context,
823 __gnu_unwind_state uws;
828 int phase2_call_unexpected_after_unwind = 0;
830 int forced_unwind = state & _US_FORCE_UNWIND;
832 state &= _US_ACTION_MASK;
834 data = (_uw *) ucbp->pr_cache.ehtp;
835 uws.data = *(data++);
845 uws.words_left = (uws.data >> 16) & 0xff;
848 data += uws.words_left;
851 /* Restore the saved pointer. */
852 if (state == _US_UNWIND_FRAME_RESUME)
853 data = (_uw *) ucbp->cleanup_cache.bitpattern[0];
855 if ((ucbp->pr_cache.additional & 1) == 0)
857 /* Process descriptors. */
865 len = ((EHT32 *) data)->length;
866 offset = ((EHT32 *) data)->offset;
871 len = ((EHT16 *) data)->length;
872 offset = ((EHT16 *) data)->offset;
876 fnstart = ucbp->pr_cache.fnstart + (offset & ~1);
877 addr = _Unwind_GetGR (context, R_PC);
878 in_range = (fnstart <= addr && addr < fnstart + (len & ~1));
880 switch (((offset & 1) << 1) | (len & 1))
884 if (state != _US_VIRTUAL_UNWIND_FRAME
887 /* Cleanup in range, and we are running cleanups. */
890 /* Landing pad address is 31-bit pc-relative offset. */
891 lp = selfrel_offset31 (data);
893 /* Save the exception data pointer. */
894 ucbp->cleanup_cache.bitpattern[0] = (_uw) data;
895 if (!__cxa_begin_cleanup (ucbp))
897 /* Setup the VRS to enter the landing pad. */
898 _Unwind_SetGR (context, R_PC, lp);
899 return _URC_INSTALL_CONTEXT;
901 /* Cleanup not in range, or we are in stage 1. */
907 if (state == _US_VIRTUAL_UNWIND_FRAME)
911 /* Check for a barrier. */
913 bool is_reference = (data[0] & uint32_highbit) != 0;
915 enum __cxa_type_match_result match_type;
917 /* Check for no-throw areas. */
918 if (data[1] == (_uw) -2)
921 /* The thrown object immediately follows the ECB. */
922 matched = (void *)(ucbp + 1);
923 if (data[1] != (_uw) -1)
925 /* Match a catch specification. */
926 rtti = _Unwind_decode_typeinfo_ptr (0,
928 match_type = __cxa_type_match (ucbp,
934 match_type = ctm_succeeded;
938 ucbp->barrier_cache.sp =
939 _Unwind_GetGR (context, R_SP);
940 // ctm_succeeded_with_ptr_to_base really
941 // means _c_t_m indirected the pointer
942 // object. We have to reconstruct the
943 // additional pointer layer by using a temporary.
944 if (match_type == ctm_succeeded_with_ptr_to_base)
946 ucbp->barrier_cache.bitpattern[2]
948 ucbp->barrier_cache.bitpattern[0]
949 = (_uw) &ucbp->barrier_cache.bitpattern[2];
952 ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
953 ucbp->barrier_cache.bitpattern[1] = (_uw) data;
954 return _URC_HANDLER_FOUND;
957 /* Handler out of range, or not matched. */
959 else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
960 && ucbp->barrier_cache.bitpattern[1] == (_uw) data)
962 /* Matched a previous propagation barrier. */
965 /* Setup for entry to the handler. */
966 lp = selfrel_offset31 (data);
967 _Unwind_SetGR (context, R_PC, lp);
968 _Unwind_SetGR (context, 0, (_uw) ucbp);
969 return _URC_INSTALL_CONTEXT;
971 /* Catch handler not matched. Advance to the next descriptor. */
976 rtti_count = data[0] & 0x7fffffff;
977 /* Exception specification. */
978 if (state == _US_VIRTUAL_UNWIND_FRAME)
980 if (in_range && (!forced_unwind || !rtti_count))
982 /* Match against the exception specification. */
987 for (i = 0; i < rtti_count; i++)
989 matched = (void *)(ucbp + 1);
990 rtti = _Unwind_decode_typeinfo_ptr (0,
992 if (__cxa_type_match (ucbp, (type_info *) rtti, 0,
999 /* Exception does not match the spec. */
1000 ucbp->barrier_cache.sp =
1001 _Unwind_GetGR (context, R_SP);
1002 ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
1003 ucbp->barrier_cache.bitpattern[1] = (_uw) data;
1004 return _URC_HANDLER_FOUND;
1007 /* Handler out of range, or exception is permitted. */
1009 else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
1010 && ucbp->barrier_cache.bitpattern[1] == (_uw) data)
1012 /* Matched a previous propagation barrier. */
1014 /* Record the RTTI list for __cxa_call_unexpected. */
1015 ucbp->barrier_cache.bitpattern[1] = rtti_count;
1016 ucbp->barrier_cache.bitpattern[2] = 0;
1017 ucbp->barrier_cache.bitpattern[3] = 4;
1018 ucbp->barrier_cache.bitpattern[4] = (_uw) &data[1];
1020 if (data[0] & uint32_highbit)
1022 data += rtti_count + 1;
1023 /* Setup for entry to the handler. */
1024 lp = selfrel_offset31 (data);
1026 _Unwind_SetGR (context, R_PC, lp);
1027 _Unwind_SetGR (context, 0, (_uw) ucbp);
1028 return _URC_INSTALL_CONTEXT;
1031 phase2_call_unexpected_after_unwind = 1;
1033 if (data[0] & uint32_highbit)
1035 data += rtti_count + 1;
1039 /* Should never happen. */
1040 return _URC_FAILURE;
1042 /* Finished processing this descriptor. */
1048 /* 24-bit ecoding */
1049 if (__gnu_unwind_24bit (context, uws.data, id == 4) != _URC_OK)
1050 return _URC_FAILURE;
1054 if (__gnu_unwind_execute (context, &uws) != _URC_OK)
1055 return _URC_FAILURE;
1058 if (phase2_call_unexpected_after_unwind)
1060 /* Enter __cxa_unexpected as if called from the call site. */
1061 _Unwind_SetGR (context, R_LR, _Unwind_GetGR (context, R_PC));
1062 _Unwind_SetGR (context, R_PC, (_uw) &__cxa_call_unexpected);
1063 return _URC_INSTALL_CONTEXT;
1066 return _URC_CONTINUE_UNWIND;