1 /* ARM EABI compliant unwinding routines.
2 Copyright (C) 2004, 2005, 2009 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/>. */
26 /* We add a prototype for abort here to avoid creating a dependency on
28 extern void abort (void);
30 /* Definitions for C++ runtime support routines. We make these weak
31 declarations to avoid pulling in libsupc++ unnecessarily. */
32 typedef unsigned char bool;
34 typedef struct _ZSt9type_info type_info; /* This names C++ type_info type */
36 void __attribute__((weak)) __cxa_call_unexpected(_Unwind_Control_Block *ucbp);
37 bool __attribute__((weak)) __cxa_begin_cleanup(_Unwind_Control_Block *ucbp);
38 bool __attribute__((weak)) __cxa_type_match(_Unwind_Control_Block *ucbp,
39 const type_info *rttip,
41 void **matched_object);
43 _Unwind_Ptr __attribute__((weak))
44 __gnu_Unwind_Find_exidx (_Unwind_Ptr, int *);
52 #define EXIDX_CANTUNWIND 1
53 #define uint32_highbit (((_uw) 1) << 31)
55 #define UCB_FORCED_STOP_FN(ucbp) ((ucbp)->unwinder_cache.reserved1)
56 #define UCB_PR_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved2)
57 #define UCB_SAVED_CALLSITE_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved3)
58 #define UCB_FORCED_STOP_ARG(ucbp) ((ucbp)->unwinder_cache.reserved4)
65 /* We use normal integer types here to avoid the compiler generating
66 coprocessor instructions. */
75 /* Always populated via VSTM, so no need for the "pad" field from
76 vfp_regs (which is used to store the format word for FSTMX). */
100 /* Unwind descriptors. */
114 /* The ABI specifies that the unwind routines may only use core registers,
115 except when actually manipulating coprocessor state. This allows
116 us to write one implementation that works on all platforms by
117 demand-saving coprocessor registers.
119 During unwinding we hold the coprocessor state in the actual hardware
120 registers and allocate demand-save areas for use during phase1
125 /* The first fields must be the same as a phase2_vrs. */
126 _uw demand_save_flags;
127 struct core_regs core;
128 _uw prev_sp; /* Only valid during forced unwinding. */
130 struct vfpv3_regs vfp_regs_16_to_31;
132 struct wmmxd_regs wmmxd;
133 struct wmmxc_regs wmmxc;
136 #define DEMAND_SAVE_VFP 1 /* VFP state has been saved if not set */
137 #define DEMAND_SAVE_VFP_D 2 /* VFP state is for FLDMD/FSTMD if set */
138 #define DEMAND_SAVE_VFP_V3 4 /* VFPv3 state for regs 16 .. 31 has
139 been saved if not set */
140 #define DEMAND_SAVE_WMMXD 8 /* iWMMXt data registers have been
142 #define DEMAND_SAVE_WMMXC 16 /* iWMMXt control registers have been
145 /* This must match the structure created by the assembly wrappers. */
148 _uw demand_save_flags;
149 struct core_regs core;
153 /* An exception index table entry. */
155 typedef struct __EIT_entry
161 /* Assembly helper functions. */
163 /* Restore core register state. Never returns. */
164 void __attribute__((noreturn)) restore_core_regs (struct core_regs *);
167 /* Coprocessor register state manipulation functions. */
169 /* Routines for FLDMX/FSTMX format... */
170 void __gnu_Unwind_Save_VFP (struct vfp_regs * p);
171 void __gnu_Unwind_Restore_VFP (struct vfp_regs * p);
172 void __gnu_Unwind_Save_WMMXD (struct wmmxd_regs * p);
173 void __gnu_Unwind_Restore_WMMXD (struct wmmxd_regs * p);
174 void __gnu_Unwind_Save_WMMXC (struct wmmxc_regs * p);
175 void __gnu_Unwind_Restore_WMMXC (struct wmmxc_regs * p);
177 /* ...and those for FLDMD/FSTMD format... */
178 void __gnu_Unwind_Save_VFP_D (struct vfp_regs * p);
179 void __gnu_Unwind_Restore_VFP_D (struct vfp_regs * p);
181 /* ...and those for VLDM/VSTM format, saving/restoring only registers
183 void __gnu_Unwind_Save_VFP_D_16_to_31 (struct vfpv3_regs * p);
184 void __gnu_Unwind_Restore_VFP_D_16_to_31 (struct vfpv3_regs * p);
186 /* Restore coprocessor state after phase1 unwinding. */
188 restore_non_core_regs (phase1_vrs * vrs)
190 if ((vrs->demand_save_flags & DEMAND_SAVE_VFP) == 0)
192 if (vrs->demand_save_flags & DEMAND_SAVE_VFP_D)
193 __gnu_Unwind_Restore_VFP_D (&vrs->vfp);
195 __gnu_Unwind_Restore_VFP (&vrs->vfp);
198 if ((vrs->demand_save_flags & DEMAND_SAVE_VFP_V3) == 0)
199 __gnu_Unwind_Restore_VFP_D_16_to_31 (&vrs->vfp_regs_16_to_31);
201 if ((vrs->demand_save_flags & DEMAND_SAVE_WMMXD) == 0)
202 __gnu_Unwind_Restore_WMMXD (&vrs->wmmxd);
203 if ((vrs->demand_save_flags & DEMAND_SAVE_WMMXC) == 0)
204 __gnu_Unwind_Restore_WMMXC (&vrs->wmmxc);
207 /* A better way to do this would probably be to compare the absolute address
208 with a segment relative relocation of the same symbol. */
210 extern int __text_start;
211 extern int __data_start;
213 /* The exception index table location. */
214 extern __EIT_entry __exidx_start;
215 extern __EIT_entry __exidx_end;
217 /* ABI defined personality routines. */
218 extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr0 (_Unwind_State,
219 _Unwind_Control_Block *, _Unwind_Context *);// __attribute__((weak));
220 extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr1 (_Unwind_State,
221 _Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));
222 extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr2 (_Unwind_State,
223 _Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));
225 /* ABI defined routine to store a virtual register to memory. */
227 _Unwind_VRS_Result _Unwind_VRS_Get (_Unwind_Context *context,
228 _Unwind_VRS_RegClass regclass,
230 _Unwind_VRS_DataRepresentation representation,
233 phase1_vrs *vrs = (phase1_vrs *) context;
238 if (representation != _UVRSD_UINT32
240 return _UVRSR_FAILED;
241 *(_uw *) valuep = vrs->core.r[regno];
248 return _UVRSR_NOT_IMPLEMENTED;
251 return _UVRSR_FAILED;
256 /* ABI defined function to load a virtual register from memory. */
258 _Unwind_VRS_Result _Unwind_VRS_Set (_Unwind_Context *context,
259 _Unwind_VRS_RegClass regclass,
261 _Unwind_VRS_DataRepresentation representation,
264 phase1_vrs *vrs = (phase1_vrs *) context;
269 if (representation != _UVRSD_UINT32
271 return _UVRSR_FAILED;
273 vrs->core.r[regno] = *(_uw *) valuep;
280 return _UVRSR_NOT_IMPLEMENTED;
283 return _UVRSR_FAILED;
288 /* ABI defined function to pop registers off the stack. */
290 _Unwind_VRS_Result _Unwind_VRS_Pop (_Unwind_Context *context,
291 _Unwind_VRS_RegClass regclass,
293 _Unwind_VRS_DataRepresentation representation)
295 phase1_vrs *vrs = (phase1_vrs *) context;
305 if (representation != _UVRSD_UINT32)
306 return _UVRSR_FAILED;
308 mask = discriminator & 0xffff;
309 ptr = (_uw *) vrs->core.r[R_SP];
310 /* Pop the requested registers. */
311 for (i = 0; i < 16; i++)
314 vrs->core.r[i] = *(ptr++);
316 /* Writeback the stack pointer value if it wasn't restored. */
317 if ((mask & (1 << R_SP)) == 0)
318 vrs->core.r[R_SP] = (_uw) ptr;
324 _uw start = discriminator >> 16;
325 _uw count = discriminator & 0xffff;
327 struct vfpv3_regs tmp_16_to_31;
331 int num_vfpv3_regs = 0;
333 /* We use an approximation here by bounding _UVRSD_DOUBLE
334 register numbers at 32 always, since we can't detect if
335 VFPv3 isn't present (in such a case the upper limit is 16). */
336 if ((representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
337 || start + count > (representation == _UVRSD_VFPX ? 16 : 32)
338 || (representation == _UVRSD_VFPX && start >= 16))
339 return _UVRSR_FAILED;
341 /* Check if we're being asked to pop VFPv3-only registers
342 (numbers 16 through 31). */
344 num_vfpv3_regs = count;
345 else if (start + count > 16)
346 num_vfpv3_regs = start + count - 16;
348 if (num_vfpv3_regs && representation != _UVRSD_DOUBLE)
349 return _UVRSR_FAILED;
351 /* Demand-save coprocessor registers for stage1. */
352 if (start < 16 && (vrs->demand_save_flags & DEMAND_SAVE_VFP))
354 vrs->demand_save_flags &= ~DEMAND_SAVE_VFP;
356 if (representation == _UVRSD_DOUBLE)
358 /* Save in FLDMD/FSTMD format. */
359 vrs->demand_save_flags |= DEMAND_SAVE_VFP_D;
360 __gnu_Unwind_Save_VFP_D (&vrs->vfp);
364 /* Save in FLDMX/FSTMX format. */
365 vrs->demand_save_flags &= ~DEMAND_SAVE_VFP_D;
366 __gnu_Unwind_Save_VFP (&vrs->vfp);
370 if (num_vfpv3_regs > 0
371 && (vrs->demand_save_flags & DEMAND_SAVE_VFP_V3))
373 vrs->demand_save_flags &= ~DEMAND_SAVE_VFP_V3;
374 __gnu_Unwind_Save_VFP_D_16_to_31 (&vrs->vfp_regs_16_to_31);
377 /* Restore the registers from the stack. Do this by saving the
378 current VFP registers to a memory area, moving the in-memory
379 values into that area, and restoring from the whole area.
380 For _UVRSD_VFPX we assume FSTMX standard format 1. */
381 if (representation == _UVRSD_VFPX)
382 __gnu_Unwind_Save_VFP (&tmp);
385 /* Save registers 0 .. 15 if required. */
387 __gnu_Unwind_Save_VFP_D (&tmp);
389 /* Save VFPv3 registers 16 .. 31 if required. */
391 __gnu_Unwind_Save_VFP_D_16_to_31 (&tmp_16_to_31);
394 /* Work out how many registers below register 16 need popping. */
395 tmp_count = num_vfpv3_regs > 0 ? 16 - start : count;
397 /* Copy registers below 16, if needed.
398 The stack address is only guaranteed to be word aligned, so
399 we can't use doubleword copies. */
400 sp = (_uw *) vrs->core.r[R_SP];
404 dest = (_uw *) &tmp.d[start];
409 /* Copy VFPv3 registers numbered >= 16, if needed. */
410 if (num_vfpv3_regs > 0)
412 /* num_vfpv3_regs is needed below, so copy it. */
413 int tmp_count_2 = num_vfpv3_regs * 2;
414 int vfpv3_start = start < 16 ? 16 : start;
416 dest = (_uw *) &tmp_16_to_31.d[vfpv3_start - 16];
417 while (tmp_count_2--)
421 /* Skip the format word space if using FLDMX/FSTMX format. */
422 if (representation == _UVRSD_VFPX)
425 /* Set the new stack pointer. */
426 vrs->core.r[R_SP] = (_uw) sp;
428 /* Reload the registers. */
429 if (representation == _UVRSD_VFPX)
430 __gnu_Unwind_Restore_VFP (&tmp);
433 /* Restore registers 0 .. 15 if required. */
435 __gnu_Unwind_Restore_VFP_D (&tmp);
437 /* Restore VFPv3 registers 16 .. 31 if required. */
438 if (num_vfpv3_regs > 0)
439 __gnu_Unwind_Restore_VFP_D_16_to_31 (&tmp_16_to_31);
445 return _UVRSR_NOT_IMPLEMENTED;
449 _uw start = discriminator >> 16;
450 _uw count = discriminator & 0xffff;
451 struct wmmxd_regs tmp;
455 if ((representation != _UVRSD_UINT64) || start + count > 16)
456 return _UVRSR_FAILED;
458 if (vrs->demand_save_flags & DEMAND_SAVE_WMMXD)
460 /* Demand-save resisters for stage1. */
461 vrs->demand_save_flags &= ~DEMAND_SAVE_WMMXD;
462 __gnu_Unwind_Save_WMMXD (&vrs->wmmxd);
465 /* Restore the registers from the stack. Do this by saving the
466 current WMMXD registers to a memory area, moving the in-memory
467 values into that area, and restoring from the whole area. */
468 __gnu_Unwind_Save_WMMXD (&tmp);
470 /* The stack address is only guaranteed to be word aligned, so
471 we can't use doubleword copies. */
472 sp = (_uw *) vrs->core.r[R_SP];
473 dest = (_uw *) &tmp.wd[start];
478 /* Set the new stack pointer. */
479 vrs->core.r[R_SP] = (_uw) sp;
481 /* Reload the registers. */
482 __gnu_Unwind_Restore_WMMXD (&tmp);
489 struct wmmxc_regs tmp;
492 if ((representation != _UVRSD_UINT32) || discriminator > 16)
493 return _UVRSR_FAILED;
495 if (vrs->demand_save_flags & DEMAND_SAVE_WMMXC)
497 /* Demand-save resisters for stage1. */
498 vrs->demand_save_flags &= ~DEMAND_SAVE_WMMXC;
499 __gnu_Unwind_Save_WMMXC (&vrs->wmmxc);
502 /* Restore the registers from the stack. Do this by saving the
503 current WMMXC registers to a memory area, moving the in-memory
504 values into that area, and restoring from the whole area. */
505 __gnu_Unwind_Save_WMMXC (&tmp);
507 sp = (_uw *) vrs->core.r[R_SP];
508 for (i = 0; i < 4; i++)
509 if (discriminator & (1 << i))
512 /* Set the new stack pointer. */
513 vrs->core.r[R_SP] = (_uw) sp;
515 /* Reload the registers. */
516 __gnu_Unwind_Restore_WMMXC (&tmp);
521 return _UVRSR_FAILED;
526 /* Core unwinding functions. */
528 /* Calculate the address encoded by a 31-bit self-relative offset at address
531 selfrel_offset31 (const _uw *p)
536 /* Sign extend to 32 bits. */
537 if (offset & (1 << 30))
540 offset &= ~(1u << 31);
542 return offset + (_uw) p;
546 /* Perform a binary search for RETURN_ADDRESS in TABLE. The table contains
549 static const __EIT_entry *
550 search_EIT_table (const __EIT_entry * table, int nrec, _uw return_address)
557 return (__EIT_entry *) 0;
564 n = (left + right) / 2;
565 this_fn = selfrel_offset31 (&table[n].fnoffset);
567 next_fn = selfrel_offset31 (&table[n + 1].fnoffset) - 1;
569 next_fn = (_uw)0 - 1;
571 if (return_address < this_fn)
574 return (__EIT_entry *) 0;
577 else if (return_address <= next_fn)
584 /* Find the exception index table eintry for the given address.
585 Fill in the relevant fields of the UCB.
586 Returns _URC_FAILURE if an error occurred, _URC_OK on success. */
588 static _Unwind_Reason_Code
589 get_eit_entry (_Unwind_Control_Block *ucbp, _uw return_address)
591 const __EIT_entry * eitp;
594 /* The return address is the address of the instruction following the
595 call instruction (plus one in thumb mode). If this was the last
596 instruction in the function the address will lie in the following
597 function. Subtract 2 from the address so that it points within the call
598 instruction itself. */
601 if (__gnu_Unwind_Find_exidx)
603 eitp = (const __EIT_entry *) __gnu_Unwind_Find_exidx (return_address,
607 UCB_PR_ADDR (ucbp) = 0;
613 eitp = &__exidx_start;
614 nrec = &__exidx_end - &__exidx_start;
617 eitp = search_EIT_table (eitp, nrec, return_address);
621 UCB_PR_ADDR (ucbp) = 0;
624 ucbp->pr_cache.fnstart = selfrel_offset31 (&eitp->fnoffset);
626 /* Can this frame be unwound at all? */
627 if (eitp->content == EXIDX_CANTUNWIND)
629 UCB_PR_ADDR (ucbp) = 0;
630 return _URC_END_OF_STACK;
633 /* Obtain the address of the "real" __EHT_Header word. */
635 if (eitp->content & uint32_highbit)
637 /* It is immediate data. */
638 ucbp->pr_cache.ehtp = (_Unwind_EHT_Header *)&eitp->content;
639 ucbp->pr_cache.additional = 1;
643 /* The low 31 bits of the content field are a self-relative
644 offset to an _Unwind_EHT_Entry structure. */
645 ucbp->pr_cache.ehtp =
646 (_Unwind_EHT_Header *) selfrel_offset31 (&eitp->content);
647 ucbp->pr_cache.additional = 0;
650 /* Discover the personality routine address. */
651 if (*ucbp->pr_cache.ehtp & (1u << 31))
653 /* One of the predefined standard routines. */
654 _uw idx = (*(_uw *) ucbp->pr_cache.ehtp >> 24) & 0xf;
656 UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr0;
658 UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr1;
660 UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr2;
663 UCB_PR_ADDR (ucbp) = 0;
669 /* Execute region offset to PR */
670 UCB_PR_ADDR (ucbp) = selfrel_offset31 (ucbp->pr_cache.ehtp);
676 /* Perform phase2 unwinding. VRS is the initial virtual register state. */
678 static void __attribute__((noreturn))
679 unwind_phase2 (_Unwind_Control_Block * ucbp, phase2_vrs * vrs)
681 _Unwind_Reason_Code pr_result;
685 /* Find the entry for this routine. */
686 if (get_eit_entry (ucbp, vrs->core.r[R_PC]) != _URC_OK)
689 UCB_SAVED_CALLSITE_ADDR (ucbp) = vrs->core.r[R_PC];
691 /* Call the pr to decide what to do. */
692 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
693 (_US_UNWIND_FRAME_STARTING, ucbp, (_Unwind_Context *) vrs);
695 while (pr_result == _URC_CONTINUE_UNWIND);
697 if (pr_result != _URC_INSTALL_CONTEXT)
700 restore_core_regs (&vrs->core);
703 /* Perform phase2 forced unwinding. */
705 static _Unwind_Reason_Code
706 unwind_phase2_forced (_Unwind_Control_Block *ucbp, phase2_vrs *entry_vrs,
709 _Unwind_Stop_Fn stop_fn = (_Unwind_Stop_Fn) UCB_FORCED_STOP_FN (ucbp);
710 void *stop_arg = (void *)UCB_FORCED_STOP_ARG (ucbp);
711 _Unwind_Reason_Code pr_result = 0;
712 /* We use phase1_vrs here even though we do not demand save, for the
714 phase1_vrs saved_vrs, next_vrs;
716 /* Save the core registers. */
717 saved_vrs.core = entry_vrs->core;
718 /* We don't need to demand-save the non-core registers, because we
719 unwind in a single pass. */
720 saved_vrs.demand_save_flags = 0;
722 /* Unwind until we reach a propagation barrier. */
725 _Unwind_State action;
726 _Unwind_Reason_Code entry_code;
727 _Unwind_Reason_Code stop_code;
729 /* Find the entry for this routine. */
730 entry_code = get_eit_entry (ucbp, saved_vrs.core.r[R_PC]);
734 action = _US_UNWIND_FRAME_RESUME | _US_FORCE_UNWIND;
738 action = _US_UNWIND_FRAME_STARTING | _US_FORCE_UNWIND;
740 if (entry_code == _URC_OK)
742 UCB_SAVED_CALLSITE_ADDR (ucbp) = saved_vrs.core.r[R_PC];
744 next_vrs = saved_vrs;
746 /* Call the pr to decide what to do. */
747 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
748 (action, ucbp, (void *) &next_vrs);
750 saved_vrs.prev_sp = next_vrs.core.r[R_SP];
754 /* Treat any failure as the end of unwinding, to cope more
755 gracefully with missing EH information. Mixed EH and
756 non-EH within one object will usually result in failure,
757 because the .ARM.exidx tables do not indicate the end
758 of the code to which they apply; but mixed EH and non-EH
759 shared objects should return an unwind failure at the
760 entry of a non-EH shared object. */
761 action |= _US_END_OF_STACK;
763 saved_vrs.prev_sp = saved_vrs.core.r[R_SP];
766 stop_code = stop_fn (1, action, ucbp->exception_class, ucbp,
767 (void *)&saved_vrs, stop_arg);
768 if (stop_code != _URC_NO_REASON)
771 if (entry_code != _URC_OK)
774 saved_vrs = next_vrs;
776 while (pr_result == _URC_CONTINUE_UNWIND);
778 if (pr_result != _URC_INSTALL_CONTEXT)
780 /* Some sort of failure has occurred in the pr and probably the
781 pr returned _URC_FAILURE. */
785 restore_core_regs (&saved_vrs.core);
788 /* This is a very limited implementation of _Unwind_GetCFA. It returns
789 the stack pointer as it is about to be unwound, and is only valid
790 while calling the stop function during forced unwinding. If the
791 current personality routine result is going to run a cleanup, this
792 will not be the CFA; but when the frame is really unwound, it will
796 _Unwind_GetCFA (_Unwind_Context *context)
798 return ((phase1_vrs *) context)->prev_sp;
801 /* Perform phase1 unwinding. UCBP is the exception being thrown, and
802 entry_VRS is the register state on entry to _Unwind_RaiseException. */
805 __gnu_Unwind_RaiseException (_Unwind_Control_Block *, phase2_vrs *);
808 __gnu_Unwind_RaiseException (_Unwind_Control_Block * ucbp,
809 phase2_vrs * entry_vrs)
811 phase1_vrs saved_vrs;
812 _Unwind_Reason_Code pr_result;
814 /* Set the pc to the call site. */
815 entry_vrs->core.r[R_PC] = entry_vrs->core.r[R_LR];
817 /* Save the core registers. */
818 saved_vrs.core = entry_vrs->core;
819 /* Set demand-save flags. */
820 saved_vrs.demand_save_flags = ~(_uw) 0;
822 /* Unwind until we reach a propagation barrier. */
825 /* Find the entry for this routine. */
826 if (get_eit_entry (ucbp, saved_vrs.core.r[R_PC]) != _URC_OK)
829 /* Call the pr to decide what to do. */
830 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
831 (_US_VIRTUAL_UNWIND_FRAME, ucbp, (void *) &saved_vrs);
833 while (pr_result == _URC_CONTINUE_UNWIND);
835 /* We've unwound as far as we want to go, so restore the original
837 restore_non_core_regs (&saved_vrs);
838 if (pr_result != _URC_HANDLER_FOUND)
840 /* Some sort of failure has occurred in the pr and probably the
841 pr returned _URC_FAILURE. */
845 unwind_phase2 (ucbp, entry_vrs);
848 /* Resume unwinding after a cleanup has been run. UCBP is the exception
849 being thrown and ENTRY_VRS is the register state on entry to
852 __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *,
853 _Unwind_Stop_Fn, void *, phase2_vrs *);
856 __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *ucbp,
857 _Unwind_Stop_Fn stop_fn, void *stop_arg,
858 phase2_vrs *entry_vrs)
860 UCB_FORCED_STOP_FN (ucbp) = (_uw) stop_fn;
861 UCB_FORCED_STOP_ARG (ucbp) = (_uw) stop_arg;
863 /* Set the pc to the call site. */
864 entry_vrs->core.r[R_PC] = entry_vrs->core.r[R_LR];
866 return unwind_phase2_forced (ucbp, entry_vrs, 0);
870 __gnu_Unwind_Resume (_Unwind_Control_Block *, phase2_vrs *);
873 __gnu_Unwind_Resume (_Unwind_Control_Block * ucbp, phase2_vrs * entry_vrs)
875 _Unwind_Reason_Code pr_result;
877 /* Recover the saved address. */
878 entry_vrs->core.r[R_PC] = UCB_SAVED_CALLSITE_ADDR (ucbp);
880 if (UCB_FORCED_STOP_FN (ucbp))
882 unwind_phase2_forced (ucbp, entry_vrs, 1);
884 /* We can't return failure at this point. */
888 /* Call the cached PR. */
889 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
890 (_US_UNWIND_FRAME_RESUME, ucbp, (_Unwind_Context *) entry_vrs);
894 case _URC_INSTALL_CONTEXT:
895 /* Upload the registers to enter the landing pad. */
896 restore_core_regs (&entry_vrs->core);
898 case _URC_CONTINUE_UNWIND:
899 /* Continue unwinding the next frame. */
900 unwind_phase2 (ucbp, entry_vrs);
908 __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block *, phase2_vrs *);
911 __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block * ucbp,
912 phase2_vrs * entry_vrs)
914 if (!UCB_FORCED_STOP_FN (ucbp))
915 return __gnu_Unwind_RaiseException (ucbp, entry_vrs);
917 /* Set the pc to the call site. */
918 entry_vrs->core.r[R_PC] = entry_vrs->core.r[R_LR];
919 /* Continue unwinding the next frame. */
920 return unwind_phase2_forced (ucbp, entry_vrs, 0);
923 /* Clean up an exception object when unwinding is complete. */
925 _Unwind_Complete (_Unwind_Control_Block * ucbp __attribute__((unused)))
930 /* Get the _Unwind_Control_Block from an _Unwind_Context. */
932 static inline _Unwind_Control_Block *
933 unwind_UCB_from_context (_Unwind_Context * context)
935 return (_Unwind_Control_Block *) _Unwind_GetGR (context, R_IP);
939 /* Free an exception. */
942 _Unwind_DeleteException (_Unwind_Exception * exc)
944 if (exc->exception_cleanup)
945 (*exc->exception_cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exc);
949 /* Perform stack backtrace through unwind data. */
951 __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument,
952 phase2_vrs * entry_vrs);
954 __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument,
955 phase2_vrs * entry_vrs)
957 phase1_vrs saved_vrs;
958 _Unwind_Reason_Code code;
960 _Unwind_Control_Block ucb;
961 _Unwind_Control_Block *ucbp = &ucb;
963 /* Set the pc to the call site. */
964 entry_vrs->core.r[R_PC] = entry_vrs->core.r[R_LR];
966 /* Save the core registers. */
967 saved_vrs.core = entry_vrs->core;
968 /* Set demand-save flags. */
969 saved_vrs.demand_save_flags = ~(_uw) 0;
973 /* Find the entry for this routine. */
974 if (get_eit_entry (ucbp, saved_vrs.core.r[R_PC]) != _URC_OK)
980 /* The dwarf unwinder assumes the context structure holds things
981 like the function and LSDA pointers. The ARM implementation
982 caches these in the exception header (UCB). To avoid
983 rewriting everything we make the virtual IP register point at
985 _Unwind_SetGR((_Unwind_Context *)&saved_vrs, 12, (_Unwind_Ptr) ucbp);
987 /* Call trace function. */
988 if ((*trace) ((_Unwind_Context *) &saved_vrs, trace_argument)
995 /* Call the pr to decide what to do. */
996 code = ((personality_routine) UCB_PR_ADDR (ucbp))
997 (_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND,
998 ucbp, (void *) &saved_vrs);
1000 while (code != _URC_END_OF_STACK
1001 && code != _URC_FAILURE);
1004 restore_non_core_regs (&saved_vrs);
1009 /* Common implementation for ARM ABI defined personality routines.
1010 ID is the index of the personality routine, other arguments are as defined
1011 by __aeabi_unwind_cpp_pr{0,1,2}. */
1013 static _Unwind_Reason_Code
1014 __gnu_unwind_pr_common (_Unwind_State state,
1015 _Unwind_Control_Block *ucbp,
1016 _Unwind_Context *context,
1019 __gnu_unwind_state uws;
1024 int phase2_call_unexpected_after_unwind = 0;
1026 int forced_unwind = state & _US_FORCE_UNWIND;
1028 state &= _US_ACTION_MASK;
1030 data = (_uw *) ucbp->pr_cache.ehtp;
1031 uws.data = *(data++);
1041 uws.words_left = (uws.data >> 16) & 0xff;
1044 data += uws.words_left;
1047 /* Restore the saved pointer. */
1048 if (state == _US_UNWIND_FRAME_RESUME)
1049 data = (_uw *) ucbp->cleanup_cache.bitpattern[0];
1051 if ((ucbp->pr_cache.additional & 1) == 0)
1053 /* Process descriptors. */
1061 len = ((EHT32 *) data)->length;
1062 offset = ((EHT32 *) data)->offset;
1067 len = ((EHT16 *) data)->length;
1068 offset = ((EHT16 *) data)->offset;
1072 fnstart = ucbp->pr_cache.fnstart + (offset & ~1);
1073 addr = _Unwind_GetGR (context, R_PC);
1074 in_range = (fnstart <= addr && addr < fnstart + (len & ~1));
1076 switch (((offset & 1) << 1) | (len & 1))
1080 if (state != _US_VIRTUAL_UNWIND_FRAME
1083 /* Cleanup in range, and we are running cleanups. */
1086 /* Landing pad address is 31-bit pc-relative offset. */
1087 lp = selfrel_offset31 (data);
1089 /* Save the exception data pointer. */
1090 ucbp->cleanup_cache.bitpattern[0] = (_uw) data;
1091 if (!__cxa_begin_cleanup (ucbp))
1092 return _URC_FAILURE;
1093 /* Setup the VRS to enter the landing pad. */
1094 _Unwind_SetGR (context, R_PC, lp);
1095 return _URC_INSTALL_CONTEXT;
1097 /* Cleanup not in range, or we are in stage 1. */
1102 /* Catch handler. */
1103 if (state == _US_VIRTUAL_UNWIND_FRAME)
1107 /* Check for a barrier. */
1109 bool is_reference = (data[0] & uint32_highbit) != 0;
1112 /* Check for no-throw areas. */
1113 if (data[1] == (_uw) -2)
1114 return _URC_FAILURE;
1116 /* The thrown object immediately follows the ECB. */
1117 matched = (void *)(ucbp + 1);
1118 if (data[1] != (_uw) -1)
1120 /* Match a catch specification. */
1121 rtti = _Unwind_decode_target2 ((_uw) &data[1]);
1122 if (!__cxa_type_match (ucbp, (type_info *) rtti,
1125 matched = (void *)0;
1130 ucbp->barrier_cache.sp =
1131 _Unwind_GetGR (context, R_SP);
1132 ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
1133 ucbp->barrier_cache.bitpattern[1] = (_uw) data;
1134 return _URC_HANDLER_FOUND;
1137 /* Handler out of range, or not matched. */
1139 else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
1140 && ucbp->barrier_cache.bitpattern[1] == (_uw) data)
1142 /* Matched a previous propagation barrier. */
1145 /* Setup for entry to the handler. */
1146 lp = selfrel_offset31 (data);
1147 _Unwind_SetGR (context, R_PC, lp);
1148 _Unwind_SetGR (context, 0, (_uw) ucbp);
1149 return _URC_INSTALL_CONTEXT;
1151 /* Catch handler not matched. Advance to the next descriptor. */
1156 rtti_count = data[0] & 0x7fffffff;
1157 /* Exception specification. */
1158 if (state == _US_VIRTUAL_UNWIND_FRAME)
1160 if (in_range && (!forced_unwind || !rtti_count))
1162 /* Match against the exception specification. */
1167 for (i = 0; i < rtti_count; i++)
1169 matched = (void *)(ucbp + 1);
1170 rtti = _Unwind_decode_target2 ((_uw) &data[i + 1]);
1171 if (__cxa_type_match (ucbp, (type_info *) rtti, 0,
1176 if (i == rtti_count)
1178 /* Exception does not match the spec. */
1179 ucbp->barrier_cache.sp =
1180 _Unwind_GetGR (context, R_SP);
1181 ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
1182 ucbp->barrier_cache.bitpattern[1] = (_uw) data;
1183 return _URC_HANDLER_FOUND;
1186 /* Handler out of range, or exception is permitted. */
1188 else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
1189 && ucbp->barrier_cache.bitpattern[1] == (_uw) data)
1191 /* Matched a previous propagation barrier. */
1193 /* Record the RTTI list for __cxa_call_unexpected. */
1194 ucbp->barrier_cache.bitpattern[1] = rtti_count;
1195 ucbp->barrier_cache.bitpattern[2] = 0;
1196 ucbp->barrier_cache.bitpattern[3] = 4;
1197 ucbp->barrier_cache.bitpattern[4] = (_uw) &data[1];
1199 if (data[0] & uint32_highbit)
1200 phase2_call_unexpected_after_unwind = 1;
1203 data += rtti_count + 1;
1204 /* Setup for entry to the handler. */
1205 lp = selfrel_offset31 (data);
1207 _Unwind_SetGR (context, R_PC, lp);
1208 _Unwind_SetGR (context, 0, (_uw) ucbp);
1209 return _URC_INSTALL_CONTEXT;
1212 if (data[0] & uint32_highbit)
1214 data += rtti_count + 1;
1218 /* Should never happen. */
1219 return _URC_FAILURE;
1221 /* Finished processing this descriptor. */
1225 if (__gnu_unwind_execute (context, &uws) != _URC_OK)
1226 return _URC_FAILURE;
1228 if (phase2_call_unexpected_after_unwind)
1230 /* Enter __cxa_unexpected as if called from the call site. */
1231 _Unwind_SetGR (context, R_LR, _Unwind_GetGR (context, R_PC));
1232 _Unwind_SetGR (context, R_PC, (_uw) &__cxa_call_unexpected);
1233 return _URC_INSTALL_CONTEXT;
1236 return _URC_CONTINUE_UNWIND;
1240 /* ABI defined personality routine entry points. */
1243 __aeabi_unwind_cpp_pr0 (_Unwind_State state,
1244 _Unwind_Control_Block *ucbp,
1245 _Unwind_Context *context)
1247 return __gnu_unwind_pr_common (state, ucbp, context, 0);
1251 __aeabi_unwind_cpp_pr1 (_Unwind_State state,
1252 _Unwind_Control_Block *ucbp,
1253 _Unwind_Context *context)
1255 return __gnu_unwind_pr_common (state, ucbp, context, 1);
1259 __aeabi_unwind_cpp_pr2 (_Unwind_State state,
1260 _Unwind_Control_Block *ucbp,
1261 _Unwind_Context *context)
1263 return __gnu_unwind_pr_common (state, ucbp, context, 2);