1 /* ARM EABI compliant unwinding routines.
2 Copyright (C) 2004, 2005 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 2, or (at your option) any
10 In addition to the permissions in the GNU General Public License, the
11 Free Software Foundation gives you unlimited permission to link the
12 compiled version of this file into combinations with other programs,
13 and to distribute those combinations without any restriction coming
14 from the use of this file. (The General Public License restrictions
15 do apply in other respects; for example, they cover modification of
16 the file, and distribution when not linked into a combine
19 This file is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; see the file COPYING. If not, write to
26 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
27 Boston, MA 02110-1301, USA. */
30 /* We add a prototype for abort here to avoid creating a dependency on
32 extern void abort (void);
34 /* Definitions for C++ runtime support routines. We make these weak
35 declarations to avoid pulling in libsupc++ unnecessarily. */
36 typedef unsigned char bool;
38 typedef struct _ZSt9type_info type_info
; /* This names C++ type_info type */
40 void __attribute__((weak
)) __cxa_call_unexpected(_Unwind_Control_Block
*ucbp
);
41 bool __attribute__((weak
)) __cxa_begin_cleanup(_Unwind_Control_Block
*ucbp
);
42 bool __attribute__((weak
)) __cxa_type_match(_Unwind_Control_Block
*ucbp
,
43 const type_info
*rttip
,
45 void **matched_object
);
47 _Unwind_Ptr
__attribute__((weak
))
48 __gnu_Unwind_Find_exidx (_Unwind_Ptr
, int *);
56 #define EXIDX_CANTUNWIND 1
57 #define uint32_highbit (((_uw) 1) << 31)
59 #define UCB_FORCED_STOP_FN(ucbp) ((ucbp)->unwinder_cache.reserved1)
60 #define UCB_PR_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved2)
61 #define UCB_SAVED_CALLSITE_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved3)
62 #define UCB_FORCED_STOP_ARG(ucbp) ((ucbp)->unwinder_cache.reserved4)
69 /* We use normal integer types here to avoid the compiler generating
70 coprocessor instructions. */
79 /* Always populated via VSTM, so no need for the "pad" field from
80 vfp_regs (which is used to store the format word for FSTMX). */
104 /* Unwind descriptors. */
118 /* The ABI specifies that the unwind routines may only use core registers,
119 except when actually manipulating coprocessor state. This allows
120 us to write one implementation that works on all platforms by
121 demand-saving coprocessor registers.
123 During unwinding we hold the coprocessor state in the actual hardware
124 registers and allocate demand-save areas for use during phase1
129 /* The first fields must be the same as a phase2_vrs. */
130 _uw demand_save_flags
;
131 struct core_regs core
;
132 _uw prev_sp
; /* Only valid during forced unwinding. */
134 struct vfpv3_regs vfp_regs_16_to_31
;
136 struct wmmxd_regs wmmxd
;
137 struct wmmxc_regs wmmxc
;
140 #define DEMAND_SAVE_VFP 1 /* VFP state has been saved if not set */
141 #define DEMAND_SAVE_VFP_D 2 /* VFP state is for FLDMD/FSTMD if set */
142 #define DEMAND_SAVE_VFP_V3 4 /* VFPv3 state for regs 16 .. 31 has
143 been saved if not set */
144 #define DEMAND_SAVE_WMMXD 8 /* iWMMXt data registers have been
146 #define DEMAND_SAVE_WMMXC 16 /* iWMMXt control registers have been
149 /* This must match the structure created by the assembly wrappers. */
152 _uw demand_save_flags
;
153 struct core_regs core
;
157 /* An exception index table entry. */
159 typedef struct __EIT_entry
165 /* Assembly helper functions. */
167 /* Restore core register state. Never returns. */
168 void __attribute__((noreturn
)) restore_core_regs (struct core_regs
*);
171 /* Coprocessor register state manipulation functions. */
173 /* Routines for FLDMX/FSTMX format... */
174 void __gnu_Unwind_Save_VFP (struct vfp_regs
* p
);
175 void __gnu_Unwind_Restore_VFP (struct vfp_regs
* p
);
176 void __gnu_Unwind_Save_WMMXD (struct wmmxd_regs
* p
);
177 void __gnu_Unwind_Restore_WMMXD (struct wmmxd_regs
* p
);
178 void __gnu_Unwind_Save_WMMXC (struct wmmxc_regs
* p
);
179 void __gnu_Unwind_Restore_WMMXC (struct wmmxc_regs
* p
);
181 /* ...and those for FLDMD/FSTMD format... */
182 void __gnu_Unwind_Save_VFP_D (struct vfp_regs
* p
);
183 void __gnu_Unwind_Restore_VFP_D (struct vfp_regs
* p
);
185 /* ...and those for VLDM/VSTM format, saving/restoring only registers
187 void __gnu_Unwind_Save_VFP_D_16_to_31 (struct vfpv3_regs
* p
);
188 void __gnu_Unwind_Restore_VFP_D_16_to_31 (struct vfpv3_regs
* p
);
190 /* Restore coprocessor state after phase1 unwinding. */
192 restore_non_core_regs (phase1_vrs
* vrs
)
194 if ((vrs
->demand_save_flags
& DEMAND_SAVE_VFP
) == 0)
196 if (vrs
->demand_save_flags
& DEMAND_SAVE_VFP_D
)
197 __gnu_Unwind_Restore_VFP_D (&vrs
->vfp
);
199 __gnu_Unwind_Restore_VFP (&vrs
->vfp
);
202 if ((vrs
->demand_save_flags
& DEMAND_SAVE_VFP_V3
) == 0)
203 __gnu_Unwind_Restore_VFP_D_16_to_31 (&vrs
->vfp_regs_16_to_31
);
205 if ((vrs
->demand_save_flags
& DEMAND_SAVE_WMMXD
) == 0)
206 __gnu_Unwind_Restore_WMMXD (&vrs
->wmmxd
);
207 if ((vrs
->demand_save_flags
& DEMAND_SAVE_WMMXC
) == 0)
208 __gnu_Unwind_Restore_WMMXC (&vrs
->wmmxc
);
211 /* A better way to do this would probably be to compare the absolute address
212 with a segment relative relocation of the same symbol. */
214 extern int __text_start
;
215 extern int __data_start
;
217 /* The exception index table location. */
218 extern __EIT_entry __exidx_start
;
219 extern __EIT_entry __exidx_end
;
221 /* ABI defined personality routines. */
222 extern _Unwind_Reason_Code
__aeabi_unwind_cpp_pr0 (_Unwind_State
,
223 _Unwind_Control_Block
*, _Unwind_Context
*);// __attribute__((weak));
224 extern _Unwind_Reason_Code
__aeabi_unwind_cpp_pr1 (_Unwind_State
,
225 _Unwind_Control_Block
*, _Unwind_Context
*) __attribute__((weak
));
226 extern _Unwind_Reason_Code
__aeabi_unwind_cpp_pr2 (_Unwind_State
,
227 _Unwind_Control_Block
*, _Unwind_Context
*) __attribute__((weak
));
229 /* ABI defined routine to store a virtual register to memory. */
231 _Unwind_VRS_Result
_Unwind_VRS_Get (_Unwind_Context
*context
,
232 _Unwind_VRS_RegClass regclass
,
234 _Unwind_VRS_DataRepresentation representation
,
237 phase1_vrs
*vrs
= (phase1_vrs
*) context
;
242 if (representation
!= _UVRSD_UINT32
244 return _UVRSR_FAILED
;
245 *(_uw
*) valuep
= vrs
->core
.r
[regno
];
252 return _UVRSR_NOT_IMPLEMENTED
;
255 return _UVRSR_FAILED
;
260 /* ABI defined function to load a virtual register from memory. */
262 _Unwind_VRS_Result
_Unwind_VRS_Set (_Unwind_Context
*context
,
263 _Unwind_VRS_RegClass regclass
,
265 _Unwind_VRS_DataRepresentation representation
,
268 phase1_vrs
*vrs
= (phase1_vrs
*) context
;
273 if (representation
!= _UVRSD_UINT32
275 return _UVRSR_FAILED
;
277 vrs
->core
.r
[regno
] = *(_uw
*) valuep
;
284 return _UVRSR_NOT_IMPLEMENTED
;
287 return _UVRSR_FAILED
;
292 /* ABI defined function to pop registers off the stack. */
294 _Unwind_VRS_Result
_Unwind_VRS_Pop (_Unwind_Context
*context
,
295 _Unwind_VRS_RegClass regclass
,
297 _Unwind_VRS_DataRepresentation representation
)
299 phase1_vrs
*vrs
= (phase1_vrs
*) context
;
309 if (representation
!= _UVRSD_UINT32
)
310 return _UVRSR_FAILED
;
312 mask
= discriminator
& 0xffff;
313 ptr
= (_uw
*) vrs
->core
.r
[R_SP
];
314 /* Pop the requested registers. */
315 for (i
= 0; i
< 16; i
++)
318 vrs
->core
.r
[i
] = *(ptr
++);
320 /* Writeback the stack pointer value if it wasn't restored. */
321 if ((mask
& (1 << R_SP
)) == 0)
322 vrs
->core
.r
[R_SP
] = (_uw
) ptr
;
328 _uw start
= discriminator
>> 16;
329 _uw count
= discriminator
& 0xffff;
331 struct vfpv3_regs tmp_16_to_31
;
335 int num_vfpv3_regs
= 0;
337 /* We use an approximation here by bounding _UVRSD_DOUBLE
338 register numbers at 32 always, since we can't detect if
339 VFPv3 isn't present (in such a case the upper limit is 16). */
340 if ((representation
!= _UVRSD_VFPX
&& representation
!= _UVRSD_DOUBLE
)
341 || start
+ count
> (representation
== _UVRSD_VFPX
? 16 : 32)
342 || (representation
== _UVRSD_VFPX
&& start
>= 16))
343 return _UVRSR_FAILED
;
345 /* Check if we're being asked to pop VFPv3-only registers
346 (numbers 16 through 31). */
348 num_vfpv3_regs
= count
;
349 else if (start
+ count
> 16)
350 num_vfpv3_regs
= start
+ count
- 16;
352 if (num_vfpv3_regs
&& representation
!= _UVRSD_DOUBLE
)
353 return _UVRSR_FAILED
;
355 /* Demand-save coprocessor registers for stage1. */
356 if (start
< 16 && (vrs
->demand_save_flags
& DEMAND_SAVE_VFP
))
358 vrs
->demand_save_flags
&= ~DEMAND_SAVE_VFP
;
360 if (representation
== _UVRSD_DOUBLE
)
362 /* Save in FLDMD/FSTMD format. */
363 vrs
->demand_save_flags
|= DEMAND_SAVE_VFP_D
;
364 __gnu_Unwind_Save_VFP_D (&vrs
->vfp
);
368 /* Save in FLDMX/FSTMX format. */
369 vrs
->demand_save_flags
&= ~DEMAND_SAVE_VFP_D
;
370 __gnu_Unwind_Save_VFP (&vrs
->vfp
);
374 if (num_vfpv3_regs
> 0
375 && (vrs
->demand_save_flags
& DEMAND_SAVE_VFP_V3
))
377 vrs
->demand_save_flags
&= ~DEMAND_SAVE_VFP_V3
;
378 __gnu_Unwind_Save_VFP_D_16_to_31 (&vrs
->vfp_regs_16_to_31
);
381 /* Restore the registers from the stack. Do this by saving the
382 current VFP registers to a memory area, moving the in-memory
383 values into that area, and restoring from the whole area.
384 For _UVRSD_VFPX we assume FSTMX standard format 1. */
385 if (representation
== _UVRSD_VFPX
)
386 __gnu_Unwind_Save_VFP (&tmp
);
389 /* Save registers 0 .. 15 if required. */
391 __gnu_Unwind_Save_VFP_D (&tmp
);
393 /* Save VFPv3 registers 16 .. 31 if required. */
395 __gnu_Unwind_Save_VFP_D_16_to_31 (&tmp_16_to_31
);
398 /* Work out how many registers below register 16 need popping. */
399 tmp_count
= num_vfpv3_regs
> 0 ? 16 - start
: count
;
401 /* Copy registers below 16, if needed.
402 The stack address is only guaranteed to be word aligned, so
403 we can't use doubleword copies. */
404 sp
= (_uw
*) vrs
->core
.r
[R_SP
];
408 dest
= (_uw
*) &tmp
.d
[start
];
413 /* Copy VFPv3 registers numbered >= 16, if needed. */
414 if (num_vfpv3_regs
> 0)
416 /* num_vfpv3_regs is needed below, so copy it. */
417 int tmp_count_2
= num_vfpv3_regs
* 2;
418 int vfpv3_start
= start
< 16 ? 16 : start
;
420 dest
= (_uw
*) &tmp_16_to_31
.d
[vfpv3_start
- 16];
421 while (tmp_count_2
--)
425 /* Skip the format word space if using FLDMX/FSTMX format. */
426 if (representation
== _UVRSD_VFPX
)
429 /* Set the new stack pointer. */
430 vrs
->core
.r
[R_SP
] = (_uw
) sp
;
432 /* Reload the registers. */
433 if (representation
== _UVRSD_VFPX
)
434 __gnu_Unwind_Restore_VFP (&tmp
);
437 /* Restore registers 0 .. 15 if required. */
439 __gnu_Unwind_Restore_VFP_D (&tmp
);
441 /* Restore VFPv3 registers 16 .. 31 if required. */
442 if (num_vfpv3_regs
> 0)
443 __gnu_Unwind_Restore_VFP_D_16_to_31 (&tmp_16_to_31
);
449 return _UVRSR_NOT_IMPLEMENTED
;
453 _uw start
= discriminator
>> 16;
454 _uw count
= discriminator
& 0xffff;
455 struct wmmxd_regs tmp
;
459 if ((representation
!= _UVRSD_UINT64
) || start
+ count
> 16)
460 return _UVRSR_FAILED
;
462 if (vrs
->demand_save_flags
& DEMAND_SAVE_WMMXD
)
464 /* Demand-save resisters for stage1. */
465 vrs
->demand_save_flags
&= ~DEMAND_SAVE_WMMXD
;
466 __gnu_Unwind_Save_WMMXD (&vrs
->wmmxd
);
469 /* Restore the registers from the stack. Do this by saving the
470 current WMMXD registers to a memory area, moving the in-memory
471 values into that area, and restoring from the whole area. */
472 __gnu_Unwind_Save_WMMXD (&tmp
);
474 /* The stack address is only guaranteed to be word aligned, so
475 we can't use doubleword copies. */
476 sp
= (_uw
*) vrs
->core
.r
[R_SP
];
477 dest
= (_uw
*) &tmp
.wd
[start
];
482 /* Set the new stack pointer. */
483 vrs
->core
.r
[R_SP
] = (_uw
) sp
;
485 /* Reload the registers. */
486 __gnu_Unwind_Restore_WMMXD (&tmp
);
493 struct wmmxc_regs tmp
;
496 if ((representation
!= _UVRSD_UINT32
) || discriminator
> 16)
497 return _UVRSR_FAILED
;
499 if (vrs
->demand_save_flags
& DEMAND_SAVE_WMMXC
)
501 /* Demand-save resisters for stage1. */
502 vrs
->demand_save_flags
&= ~DEMAND_SAVE_WMMXC
;
503 __gnu_Unwind_Save_WMMXC (&vrs
->wmmxc
);
506 /* Restore the registers from the stack. Do this by saving the
507 current WMMXC registers to a memory area, moving the in-memory
508 values into that area, and restoring from the whole area. */
509 __gnu_Unwind_Save_WMMXC (&tmp
);
511 sp
= (_uw
*) vrs
->core
.r
[R_SP
];
512 for (i
= 0; i
< 4; i
++)
513 if (discriminator
& (1 << i
))
516 /* Set the new stack pointer. */
517 vrs
->core
.r
[R_SP
] = (_uw
) sp
;
519 /* Reload the registers. */
520 __gnu_Unwind_Restore_WMMXC (&tmp
);
525 return _UVRSR_FAILED
;
530 /* Core unwinding functions. */
532 /* Calculate the address encoded by a 31-bit self-relative offset at address
535 selfrel_offset31 (const _uw
*p
)
540 /* Sign extend to 32 bits. */
541 if (offset
& (1 << 30))
544 offset
&= ~(1u << 31);
546 return offset
+ (_uw
) p
;
550 /* Perform a binary search for RETURN_ADDRESS in TABLE. The table contains
553 static const __EIT_entry
*
554 search_EIT_table (const __EIT_entry
* table
, int nrec
, _uw return_address
)
561 return (__EIT_entry
*) 0;
568 n
= (left
+ right
) / 2;
569 this_fn
= selfrel_offset31 (&table
[n
].fnoffset
);
571 next_fn
= selfrel_offset31 (&table
[n
+ 1].fnoffset
) - 1;
573 next_fn
= (_uw
)0 - 1;
575 if (return_address
< this_fn
)
578 return (__EIT_entry
*) 0;
581 else if (return_address
<= next_fn
)
588 /* Find the exception index table eintry for the given address.
589 Fill in the relevant fields of the UCB.
590 Returns _URC_FAILURE if an error occurred, _URC_OK on success. */
592 static _Unwind_Reason_Code
593 get_eit_entry (_Unwind_Control_Block
*ucbp
, _uw return_address
)
595 const __EIT_entry
* eitp
;
598 /* The return address is the address of the instruction following the
599 call instruction (plus one in thumb mode). If this was the last
600 instruction in the function the address will lie in the following
601 function. Subtract 2 from the address so that it points within the call
602 instruction itself. */
605 if (__gnu_Unwind_Find_exidx
)
607 eitp
= (const __EIT_entry
*) __gnu_Unwind_Find_exidx (return_address
,
611 UCB_PR_ADDR (ucbp
) = 0;
617 eitp
= &__exidx_start
;
618 nrec
= &__exidx_end
- &__exidx_start
;
621 eitp
= search_EIT_table (eitp
, nrec
, return_address
);
625 UCB_PR_ADDR (ucbp
) = 0;
628 ucbp
->pr_cache
.fnstart
= selfrel_offset31 (&eitp
->fnoffset
);
630 /* Can this frame be unwound at all? */
631 if (eitp
->content
== EXIDX_CANTUNWIND
)
633 UCB_PR_ADDR (ucbp
) = 0;
634 return _URC_END_OF_STACK
;
637 /* Obtain the address of the "real" __EHT_Header word. */
639 if (eitp
->content
& uint32_highbit
)
641 /* It is immediate data. */
642 ucbp
->pr_cache
.ehtp
= (_Unwind_EHT_Header
*)&eitp
->content
;
643 ucbp
->pr_cache
.additional
= 1;
647 /* The low 31 bits of the content field are a self-relative
648 offset to an _Unwind_EHT_Entry structure. */
649 ucbp
->pr_cache
.ehtp
=
650 (_Unwind_EHT_Header
*) selfrel_offset31 (&eitp
->content
);
651 ucbp
->pr_cache
.additional
= 0;
654 /* Discover the personality routine address. */
655 if (*ucbp
->pr_cache
.ehtp
& (1u << 31))
657 /* One of the predefined standard routines. */
658 _uw idx
= (*(_uw
*) ucbp
->pr_cache
.ehtp
>> 24) & 0xf;
660 UCB_PR_ADDR (ucbp
) = (_uw
) &__aeabi_unwind_cpp_pr0
;
662 UCB_PR_ADDR (ucbp
) = (_uw
) &__aeabi_unwind_cpp_pr1
;
664 UCB_PR_ADDR (ucbp
) = (_uw
) &__aeabi_unwind_cpp_pr2
;
667 UCB_PR_ADDR (ucbp
) = 0;
673 /* Execute region offset to PR */
674 UCB_PR_ADDR (ucbp
) = selfrel_offset31 (ucbp
->pr_cache
.ehtp
);
680 /* Perform phase2 unwinding. VRS is the initial virtual register state. */
682 static void __attribute__((noreturn
))
683 unwind_phase2 (_Unwind_Control_Block
* ucbp
, phase2_vrs
* vrs
)
685 _Unwind_Reason_Code pr_result
;
689 /* Find the entry for this routine. */
690 if (get_eit_entry (ucbp
, vrs
->core
.r
[R_PC
]) != _URC_OK
)
693 UCB_SAVED_CALLSITE_ADDR (ucbp
) = vrs
->core
.r
[R_PC
];
695 /* Call the pr to decide what to do. */
696 pr_result
= ((personality_routine
) UCB_PR_ADDR (ucbp
))
697 (_US_UNWIND_FRAME_STARTING
, ucbp
, (_Unwind_Context
*) vrs
);
699 while (pr_result
== _URC_CONTINUE_UNWIND
);
701 if (pr_result
!= _URC_INSTALL_CONTEXT
)
704 restore_core_regs (&vrs
->core
);
707 /* Perform phase2 forced unwinding. */
709 static _Unwind_Reason_Code
710 unwind_phase2_forced (_Unwind_Control_Block
*ucbp
, phase2_vrs
*entry_vrs
,
713 _Unwind_Stop_Fn stop_fn
= (_Unwind_Stop_Fn
) UCB_FORCED_STOP_FN (ucbp
);
714 void *stop_arg
= (void *)UCB_FORCED_STOP_ARG (ucbp
);
715 _Unwind_Reason_Code pr_result
= 0;
716 /* We use phase1_vrs here even though we do not demand save, for the
718 phase1_vrs saved_vrs
, next_vrs
;
720 /* Save the core registers. */
721 saved_vrs
.core
= entry_vrs
->core
;
722 /* We don't need to demand-save the non-core registers, because we
723 unwind in a single pass. */
724 saved_vrs
.demand_save_flags
= 0;
726 /* Unwind until we reach a propagation barrier. */
729 _Unwind_State action
;
730 _Unwind_Reason_Code entry_code
;
731 _Unwind_Reason_Code stop_code
;
733 /* Find the entry for this routine. */
734 entry_code
= get_eit_entry (ucbp
, saved_vrs
.core
.r
[R_PC
]);
738 action
= _US_UNWIND_FRAME_RESUME
| _US_FORCE_UNWIND
;
742 action
= _US_UNWIND_FRAME_STARTING
| _US_FORCE_UNWIND
;
744 if (entry_code
== _URC_OK
)
746 UCB_SAVED_CALLSITE_ADDR (ucbp
) = saved_vrs
.core
.r
[R_PC
];
748 next_vrs
= saved_vrs
;
750 /* Call the pr to decide what to do. */
751 pr_result
= ((personality_routine
) UCB_PR_ADDR (ucbp
))
752 (action
, ucbp
, (void *) &next_vrs
);
754 saved_vrs
.prev_sp
= next_vrs
.core
.r
[R_SP
];
758 /* Treat any failure as the end of unwinding, to cope more
759 gracefully with missing EH information. Mixed EH and
760 non-EH within one object will usually result in failure,
761 because the .ARM.exidx tables do not indicate the end
762 of the code to which they apply; but mixed EH and non-EH
763 shared objects should return an unwind failure at the
764 entry of a non-EH shared object. */
765 action
|= _US_END_OF_STACK
;
767 saved_vrs
.prev_sp
= saved_vrs
.core
.r
[R_SP
];
770 stop_code
= stop_fn (1, action
, ucbp
->exception_class
, ucbp
,
771 (void *)&saved_vrs
, stop_arg
);
772 if (stop_code
!= _URC_NO_REASON
)
775 if (entry_code
!= _URC_OK
)
778 saved_vrs
= next_vrs
;
780 while (pr_result
== _URC_CONTINUE_UNWIND
);
782 if (pr_result
!= _URC_INSTALL_CONTEXT
)
784 /* Some sort of failure has occurred in the pr and probably the
785 pr returned _URC_FAILURE. */
789 restore_core_regs (&saved_vrs
.core
);
792 /* This is a very limited implementation of _Unwind_GetCFA. It returns
793 the stack pointer as it is about to be unwound, and is only valid
794 while calling the stop function during forced unwinding. If the
795 current personality routine result is going to run a cleanup, this
796 will not be the CFA; but when the frame is really unwound, it will
800 _Unwind_GetCFA (_Unwind_Context
*context
)
802 return ((phase1_vrs
*) context
)->prev_sp
;
805 /* Perform phase1 unwinding. UCBP is the exception being thrown, and
806 entry_VRS is the register state on entry to _Unwind_RaiseException. */
809 __gnu_Unwind_RaiseException (_Unwind_Control_Block
*, phase2_vrs
*);
812 __gnu_Unwind_RaiseException (_Unwind_Control_Block
* ucbp
,
813 phase2_vrs
* entry_vrs
)
815 phase1_vrs saved_vrs
;
816 _Unwind_Reason_Code pr_result
;
818 /* Set the pc to the call site. */
819 entry_vrs
->core
.r
[R_PC
] = entry_vrs
->core
.r
[R_LR
];
821 /* Save the core registers. */
822 saved_vrs
.core
= entry_vrs
->core
;
823 /* Set demand-save flags. */
824 saved_vrs
.demand_save_flags
= ~(_uw
) 0;
826 /* Unwind until we reach a propagation barrier. */
829 /* Find the entry for this routine. */
830 if (get_eit_entry (ucbp
, saved_vrs
.core
.r
[R_PC
]) != _URC_OK
)
833 /* Call the pr to decide what to do. */
834 pr_result
= ((personality_routine
) UCB_PR_ADDR (ucbp
))
835 (_US_VIRTUAL_UNWIND_FRAME
, ucbp
, (void *) &saved_vrs
);
837 while (pr_result
== _URC_CONTINUE_UNWIND
);
839 /* We've unwound as far as we want to go, so restore the original
841 restore_non_core_regs (&saved_vrs
);
842 if (pr_result
!= _URC_HANDLER_FOUND
)
844 /* Some sort of failure has occurred in the pr and probably the
845 pr returned _URC_FAILURE. */
849 unwind_phase2 (ucbp
, entry_vrs
);
852 /* Resume unwinding after a cleanup has been run. UCBP is the exception
853 being thrown and ENTRY_VRS is the register state on entry to
856 __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block
*,
857 _Unwind_Stop_Fn
, void *, phase2_vrs
*);
860 __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block
*ucbp
,
861 _Unwind_Stop_Fn stop_fn
, void *stop_arg
,
862 phase2_vrs
*entry_vrs
)
864 UCB_FORCED_STOP_FN (ucbp
) = (_uw
) stop_fn
;
865 UCB_FORCED_STOP_ARG (ucbp
) = (_uw
) stop_arg
;
867 /* Set the pc to the call site. */
868 entry_vrs
->core
.r
[R_PC
] = entry_vrs
->core
.r
[R_LR
];
870 return unwind_phase2_forced (ucbp
, entry_vrs
, 0);
874 __gnu_Unwind_Resume (_Unwind_Control_Block
*, phase2_vrs
*);
877 __gnu_Unwind_Resume (_Unwind_Control_Block
* ucbp
, phase2_vrs
* entry_vrs
)
879 _Unwind_Reason_Code pr_result
;
881 /* Recover the saved address. */
882 entry_vrs
->core
.r
[R_PC
] = UCB_SAVED_CALLSITE_ADDR (ucbp
);
884 if (UCB_FORCED_STOP_FN (ucbp
))
886 unwind_phase2_forced (ucbp
, entry_vrs
, 1);
888 /* We can't return failure at this point. */
892 /* Call the cached PR. */
893 pr_result
= ((personality_routine
) UCB_PR_ADDR (ucbp
))
894 (_US_UNWIND_FRAME_RESUME
, ucbp
, (_Unwind_Context
*) entry_vrs
);
898 case _URC_INSTALL_CONTEXT
:
899 /* Upload the registers to enter the landing pad. */
900 restore_core_regs (&entry_vrs
->core
);
902 case _URC_CONTINUE_UNWIND
:
903 /* Continue unwinding the next frame. */
904 unwind_phase2 (ucbp
, entry_vrs
);
912 __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block
*, phase2_vrs
*);
915 __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block
* ucbp
,
916 phase2_vrs
* entry_vrs
)
918 if (!UCB_FORCED_STOP_FN (ucbp
))
919 return __gnu_Unwind_RaiseException (ucbp
, entry_vrs
);
921 /* Set the pc to the call site. */
922 entry_vrs
->core
.r
[R_PC
] = entry_vrs
->core
.r
[R_LR
];
923 /* Continue unwinding the next frame. */
924 return unwind_phase2_forced (ucbp
, entry_vrs
, 0);
927 /* Clean up an exception object when unwinding is complete. */
929 _Unwind_Complete (_Unwind_Control_Block
* ucbp
__attribute__((unused
)))
934 /* Get the _Unwind_Control_Block from an _Unwind_Context. */
936 static inline _Unwind_Control_Block
*
937 unwind_UCB_from_context (_Unwind_Context
* context
)
939 return (_Unwind_Control_Block
*) _Unwind_GetGR (context
, R_IP
);
943 /* Free an exception. */
946 _Unwind_DeleteException (_Unwind_Exception
* exc
)
948 if (exc
->exception_cleanup
)
949 (*exc
->exception_cleanup
) (_URC_FOREIGN_EXCEPTION_CAUGHT
, exc
);
953 /* Perform stack backtrace through unwind data. */
955 __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace
, void * trace_argument
,
956 phase2_vrs
* entry_vrs
);
958 __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace
, void * trace_argument
,
959 phase2_vrs
* entry_vrs
)
961 phase1_vrs saved_vrs
;
962 _Unwind_Reason_Code code
;
964 _Unwind_Control_Block ucb
;
965 _Unwind_Control_Block
*ucbp
= &ucb
;
967 /* Set the pc to the call site. */
968 entry_vrs
->core
.r
[R_PC
] = entry_vrs
->core
.r
[R_LR
];
970 /* Save the core registers. */
971 saved_vrs
.core
= entry_vrs
->core
;
972 /* Set demand-save flags. */
973 saved_vrs
.demand_save_flags
= ~(_uw
) 0;
977 /* Find the entry for this routine. */
978 if (get_eit_entry (ucbp
, saved_vrs
.core
.r
[R_PC
]) != _URC_OK
)
984 /* The dwarf unwinder assumes the context structure holds things
985 like the function and LSDA pointers. The ARM implementation
986 caches these in the exception header (UCB). To avoid
987 rewriting everything we make the virtual IP register point at
989 _Unwind_SetGR((_Unwind_Context
*)&saved_vrs
, 12, (_Unwind_Ptr
) ucbp
);
991 /* Call trace function. */
992 if ((*trace
) ((_Unwind_Context
*) &saved_vrs
, trace_argument
)
999 /* Call the pr to decide what to do. */
1000 code
= ((personality_routine
) UCB_PR_ADDR (ucbp
))
1001 (_US_VIRTUAL_UNWIND_FRAME
| _US_FORCE_UNWIND
,
1002 ucbp
, (void *) &saved_vrs
);
1004 while (code
!= _URC_END_OF_STACK
1005 && code
!= _URC_FAILURE
);
1008 restore_non_core_regs (&saved_vrs
);
1013 /* Common implementation for ARM ABI defined personality routines.
1014 ID is the index of the personality routine, other arguments are as defined
1015 by __aeabi_unwind_cpp_pr{0,1,2}. */
1017 static _Unwind_Reason_Code
1018 __gnu_unwind_pr_common (_Unwind_State state
,
1019 _Unwind_Control_Block
*ucbp
,
1020 _Unwind_Context
*context
,
1023 __gnu_unwind_state uws
;
1028 int phase2_call_unexpected_after_unwind
= 0;
1030 int forced_unwind
= state
& _US_FORCE_UNWIND
;
1032 state
&= _US_ACTION_MASK
;
1034 data
= (_uw
*) ucbp
->pr_cache
.ehtp
;
1035 uws
.data
= *(data
++);
1045 uws
.words_left
= (uws
.data
>> 16) & 0xff;
1048 data
+= uws
.words_left
;
1051 /* Restore the saved pointer. */
1052 if (state
== _US_UNWIND_FRAME_RESUME
)
1053 data
= (_uw
*) ucbp
->cleanup_cache
.bitpattern
[0];
1055 if ((ucbp
->pr_cache
.additional
& 1) == 0)
1057 /* Process descriptors. */
1065 len
= ((EHT32
*) data
)->length
;
1066 offset
= ((EHT32
*) data
)->offset
;
1071 len
= ((EHT16
*) data
)->length
;
1072 offset
= ((EHT16
*) data
)->offset
;
1076 fnstart
= ucbp
->pr_cache
.fnstart
+ (offset
& ~1);
1077 addr
= _Unwind_GetGR (context
, R_PC
);
1078 in_range
= (fnstart
<= addr
&& addr
< fnstart
+ (len
& ~1));
1080 switch (((offset
& 1) << 1) | (len
& 1))
1084 if (state
!= _US_VIRTUAL_UNWIND_FRAME
1087 /* Cleanup in range, and we are running cleanups. */
1090 /* Landing pad address is 31-bit pc-relative offset. */
1091 lp
= selfrel_offset31 (data
);
1093 /* Save the exception data pointer. */
1094 ucbp
->cleanup_cache
.bitpattern
[0] = (_uw
) data
;
1095 if (!__cxa_begin_cleanup (ucbp
))
1096 return _URC_FAILURE
;
1097 /* Setup the VRS to enter the landing pad. */
1098 _Unwind_SetGR (context
, R_PC
, lp
);
1099 return _URC_INSTALL_CONTEXT
;
1101 /* Cleanup not in range, or we are in stage 1. */
1106 /* Catch handler. */
1107 if (state
== _US_VIRTUAL_UNWIND_FRAME
)
1111 /* Check for a barrier. */
1113 bool is_reference
= (data
[0] & uint32_highbit
) != 0;
1116 /* Check for no-throw areas. */
1117 if (data
[1] == (_uw
) -2)
1118 return _URC_FAILURE
;
1120 /* The thrown object immediately follows the ECB. */
1121 matched
= (void *)(ucbp
+ 1);
1122 if (data
[1] != (_uw
) -1)
1124 /* Match a catch specification. */
1125 rtti
= _Unwind_decode_target2 ((_uw
) &data
[1]);
1126 if (!__cxa_type_match (ucbp
, (type_info
*) rtti
,
1129 matched
= (void *)0;
1134 ucbp
->barrier_cache
.sp
=
1135 _Unwind_GetGR (context
, R_SP
);
1136 ucbp
->barrier_cache
.bitpattern
[0] = (_uw
) matched
;
1137 ucbp
->barrier_cache
.bitpattern
[1] = (_uw
) data
;
1138 return _URC_HANDLER_FOUND
;
1141 /* Handler out of range, or not matched. */
1143 else if (ucbp
->barrier_cache
.sp
== _Unwind_GetGR (context
, R_SP
)
1144 && ucbp
->barrier_cache
.bitpattern
[1] == (_uw
) data
)
1146 /* Matched a previous propagation barrier. */
1149 /* Setup for entry to the handler. */
1150 lp
= selfrel_offset31 (data
);
1151 _Unwind_SetGR (context
, R_PC
, lp
);
1152 _Unwind_SetGR (context
, 0, (_uw
) ucbp
);
1153 return _URC_INSTALL_CONTEXT
;
1155 /* Catch handler not matched. Advance to the next descriptor. */
1160 rtti_count
= data
[0] & 0x7fffffff;
1161 /* Exception specification. */
1162 if (state
== _US_VIRTUAL_UNWIND_FRAME
)
1164 if (in_range
&& (!forced_unwind
|| !rtti_count
))
1166 /* Match against the exception specification. */
1171 for (i
= 0; i
< rtti_count
; i
++)
1173 matched
= (void *)(ucbp
+ 1);
1174 rtti
= _Unwind_decode_target2 ((_uw
) &data
[i
+ 1]);
1175 if (__cxa_type_match (ucbp
, (type_info
*) rtti
, 0,
1180 if (i
== rtti_count
)
1182 /* Exception does not match the spec. */
1183 ucbp
->barrier_cache
.sp
=
1184 _Unwind_GetGR (context
, R_SP
);
1185 ucbp
->barrier_cache
.bitpattern
[0] = (_uw
) matched
;
1186 ucbp
->barrier_cache
.bitpattern
[1] = (_uw
) data
;
1187 return _URC_HANDLER_FOUND
;
1190 /* Handler out of range, or exception is permitted. */
1192 else if (ucbp
->barrier_cache
.sp
== _Unwind_GetGR (context
, R_SP
)
1193 && ucbp
->barrier_cache
.bitpattern
[1] == (_uw
) data
)
1195 /* Matched a previous propagation barrier. */
1197 /* Record the RTTI list for __cxa_call_unexpected. */
1198 ucbp
->barrier_cache
.bitpattern
[1] = rtti_count
;
1199 ucbp
->barrier_cache
.bitpattern
[2] = 0;
1200 ucbp
->barrier_cache
.bitpattern
[3] = 4;
1201 ucbp
->barrier_cache
.bitpattern
[4] = (_uw
) &data
[1];
1203 if (data
[0] & uint32_highbit
)
1204 phase2_call_unexpected_after_unwind
= 1;
1207 data
+= rtti_count
+ 1;
1208 /* Setup for entry to the handler. */
1209 lp
= selfrel_offset31 (data
);
1211 _Unwind_SetGR (context
, R_PC
, lp
);
1212 _Unwind_SetGR (context
, 0, (_uw
) ucbp
);
1213 return _URC_INSTALL_CONTEXT
;
1216 if (data
[0] & uint32_highbit
)
1218 data
+= rtti_count
+ 1;
1222 /* Should never happen. */
1223 return _URC_FAILURE
;
1225 /* Finished processing this descriptor. */
1229 if (__gnu_unwind_execute (context
, &uws
) != _URC_OK
)
1230 return _URC_FAILURE
;
1232 if (phase2_call_unexpected_after_unwind
)
1234 /* Enter __cxa_unexpected as if called from the call site. */
1235 _Unwind_SetGR (context
, R_LR
, _Unwind_GetGR (context
, R_PC
));
1236 _Unwind_SetGR (context
, R_PC
, (_uw
) &__cxa_call_unexpected
);
1237 return _URC_INSTALL_CONTEXT
;
1240 return _URC_CONTINUE_UNWIND
;
1244 /* ABI defined personality routine entry points. */
1247 __aeabi_unwind_cpp_pr0 (_Unwind_State state
,
1248 _Unwind_Control_Block
*ucbp
,
1249 _Unwind_Context
*context
)
1251 return __gnu_unwind_pr_common (state
, ucbp
, context
, 0);
1255 __aeabi_unwind_cpp_pr1 (_Unwind_State state
,
1256 _Unwind_Control_Block
*ucbp
,
1257 _Unwind_Context
*context
)
1259 return __gnu_unwind_pr_common (state
, ucbp
, context
, 1);
1263 __aeabi_unwind_cpp_pr2 (_Unwind_State state
,
1264 _Unwind_Control_Block
*ucbp
,
1265 _Unwind_Context
*context
)
1267 return __gnu_unwind_pr_common (state
, ucbp
, context
, 2);