Merge -r 127928:132243 from trunk
[official-gcc.git] / gcc / config / arm / unwind-arm.c
blobd9f8a855925c86d16bfcfb256fa2909f15d5d160
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
8 later version.
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
17 executable.)
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. */
28 #include "unwind.h"
30 /* We add a prototype for abort here to avoid creating a dependency on
31 target headers. */
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,
44 bool is_reference,
45 void **matched_object);
47 _Unwind_Ptr __attribute__((weak))
48 __gnu_Unwind_Find_exidx (_Unwind_Ptr, int *);
50 /* Misc constants. */
51 #define R_IP 12
52 #define R_SP 13
53 #define R_LR 14
54 #define R_PC 15
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)
64 struct core_regs
66 _uw r[16];
69 /* We use normal integer types here to avoid the compiler generating
70 coprocessor instructions. */
71 struct vfp_regs
73 _uw64 d[16];
74 _uw pad;
77 struct vfpv3_regs
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). */
81 _uw64 d[16];
84 struct fpa_reg
86 _uw w[3];
89 struct fpa_regs
91 struct fpa_reg f[8];
94 struct wmmxd_regs
96 _uw64 wd[16];
99 struct wmmxc_regs
101 _uw wc[4];
104 /* Unwind descriptors. */
106 typedef struct
108 _uw16 length;
109 _uw16 offset;
110 } EHT16;
112 typedef struct
114 _uw length;
115 _uw offset;
116 } EHT32;
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
125 unwinding. */
127 typedef struct
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. */
133 struct vfp_regs vfp;
134 struct vfpv3_regs vfp_regs_16_to_31;
135 struct fpa_regs fpa;
136 struct wmmxd_regs wmmxd;
137 struct wmmxc_regs wmmxc;
138 } phase1_vrs;
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
145 saved if not set. */
146 #define DEMAND_SAVE_WMMXC 16 /* iWMMXt control registers have been
147 saved if not set. */
149 /* This must match the structure created by the assembly wrappers. */
150 typedef struct
152 _uw demand_save_flags;
153 struct core_regs core;
154 } phase2_vrs;
157 /* An exception index table entry. */
159 typedef struct __EIT_entry
161 _uw fnoffset;
162 _uw content;
163 } __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
186 16 through 31. */
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. */
191 static void
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);
198 else
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,
233 _uw regno,
234 _Unwind_VRS_DataRepresentation representation,
235 void *valuep)
237 phase1_vrs *vrs = (phase1_vrs *) context;
239 switch (regclass)
241 case _UVRSC_CORE:
242 if (representation != _UVRSD_UINT32
243 || regno > 15)
244 return _UVRSR_FAILED;
245 *(_uw *) valuep = vrs->core.r[regno];
246 return _UVRSR_OK;
248 case _UVRSC_VFP:
249 case _UVRSC_FPA:
250 case _UVRSC_WMMXD:
251 case _UVRSC_WMMXC:
252 return _UVRSR_NOT_IMPLEMENTED;
254 default:
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,
264 _uw regno,
265 _Unwind_VRS_DataRepresentation representation,
266 void *valuep)
268 phase1_vrs *vrs = (phase1_vrs *) context;
270 switch (regclass)
272 case _UVRSC_CORE:
273 if (representation != _UVRSD_UINT32
274 || regno > 15)
275 return _UVRSR_FAILED;
277 vrs->core.r[regno] = *(_uw *) valuep;
278 return _UVRSR_OK;
280 case _UVRSC_VFP:
281 case _UVRSC_FPA:
282 case _UVRSC_WMMXD:
283 case _UVRSC_WMMXC:
284 return _UVRSR_NOT_IMPLEMENTED;
286 default:
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,
296 _uw discriminator,
297 _Unwind_VRS_DataRepresentation representation)
299 phase1_vrs *vrs = (phase1_vrs *) context;
301 switch (regclass)
303 case _UVRSC_CORE:
305 _uw *ptr;
306 _uw mask;
307 int i;
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++)
317 if (mask & (1 << 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;
324 return _UVRSR_OK;
326 case _UVRSC_VFP:
328 _uw start = discriminator >> 16;
329 _uw count = discriminator & 0xffff;
330 struct vfp_regs tmp;
331 struct vfpv3_regs tmp_16_to_31;
332 int tmp_count;
333 _uw *sp;
334 _uw *dest;
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). */
347 if (start >= 16)
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);
366 else
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);
387 else
389 /* Save registers 0 .. 15 if required. */
390 if (start < 16)
391 __gnu_Unwind_Save_VFP_D (&tmp);
393 /* Save VFPv3 registers 16 .. 31 if required. */
394 if (num_vfpv3_regs)
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];
405 if (tmp_count > 0)
407 tmp_count *= 2;
408 dest = (_uw *) &tmp.d[start];
409 while (tmp_count--)
410 *(dest++) = *(sp++);
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--)
422 *(dest++) = *(sp++);
425 /* Skip the format word space if using FLDMX/FSTMX format. */
426 if (representation == _UVRSD_VFPX)
427 sp++;
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);
435 else
437 /* Restore registers 0 .. 15 if required. */
438 if (start < 16)
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);
446 return _UVRSR_OK;
448 case _UVRSC_FPA:
449 return _UVRSR_NOT_IMPLEMENTED;
451 case _UVRSC_WMMXD:
453 _uw start = discriminator >> 16;
454 _uw count = discriminator & 0xffff;
455 struct wmmxd_regs tmp;
456 _uw *sp;
457 _uw *dest;
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];
478 count *= 2;
479 while (count--)
480 *(dest++) = *(sp++);
482 /* Set the new stack pointer. */
483 vrs->core.r[R_SP] = (_uw) sp;
485 /* Reload the registers. */
486 __gnu_Unwind_Restore_WMMXD (&tmp);
488 return _UVRSR_OK;
490 case _UVRSC_WMMXC:
492 int i;
493 struct wmmxc_regs tmp;
494 _uw *sp;
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))
514 tmp.wc[i] = *(sp++);
516 /* Set the new stack pointer. */
517 vrs->core.r[R_SP] = (_uw) sp;
519 /* Reload the registers. */
520 __gnu_Unwind_Restore_WMMXC (&tmp);
522 return _UVRSR_OK;
524 default:
525 return _UVRSR_FAILED;
530 /* Core unwinding functions. */
532 /* Calculate the address encoded by a 31-bit self-relative offset at address
533 P. */
534 static inline _uw
535 selfrel_offset31 (const _uw *p)
537 _uw offset;
539 offset = *p;
540 /* Sign extend to 32 bits. */
541 if (offset & (1 << 30))
542 offset |= 1u << 31;
543 else
544 offset &= ~(1u << 31);
546 return offset + (_uw) p;
550 /* Perform a binary search for RETURN_ADDRESS in TABLE. The table contains
551 NREC entries. */
553 static const __EIT_entry *
554 search_EIT_table (const __EIT_entry * table, int nrec, _uw return_address)
556 _uw next_fn;
557 _uw this_fn;
558 int n, left, right;
560 if (nrec == 0)
561 return (__EIT_entry *) 0;
563 left = 0;
564 right = nrec - 1;
566 while (1)
568 n = (left + right) / 2;
569 this_fn = selfrel_offset31 (&table[n].fnoffset);
570 if (n != nrec - 1)
571 next_fn = selfrel_offset31 (&table[n + 1].fnoffset) - 1;
572 else
573 next_fn = (_uw)0 - 1;
575 if (return_address < this_fn)
577 if (n == left)
578 return (__EIT_entry *) 0;
579 right = n - 1;
581 else if (return_address <= next_fn)
582 return &table[n];
583 else
584 left = n + 1;
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;
596 int nrec;
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. */
603 return_address -= 2;
605 if (__gnu_Unwind_Find_exidx)
607 eitp = (const __EIT_entry *) __gnu_Unwind_Find_exidx (return_address,
608 &nrec);
609 if (!eitp)
611 UCB_PR_ADDR (ucbp) = 0;
612 return _URC_FAILURE;
615 else
617 eitp = &__exidx_start;
618 nrec = &__exidx_end - &__exidx_start;
621 eitp = search_EIT_table (eitp, nrec, return_address);
623 if (!eitp)
625 UCB_PR_ADDR (ucbp) = 0;
626 return _URC_FAILURE;
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;
645 else
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;
659 if (idx == 0)
660 UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr0;
661 else if (idx == 1)
662 UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr1;
663 else if (idx == 2)
664 UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr2;
665 else
666 { /* Failed */
667 UCB_PR_ADDR (ucbp) = 0;
668 return _URC_FAILURE;
671 else
673 /* Execute region offset to PR */
674 UCB_PR_ADDR (ucbp) = selfrel_offset31 (ucbp->pr_cache.ehtp);
676 return _URC_OK;
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)
691 abort ();
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)
702 abort();
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,
711 int resuming)
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
717 prev_sp field. */
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]);
736 if (resuming)
738 action = _US_UNWIND_FRAME_RESUME | _US_FORCE_UNWIND;
739 resuming = 0;
741 else
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];
756 else
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)
773 return _URC_FAILURE;
775 if (entry_code != _URC_OK)
776 return entry_code;
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. */
786 return _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
797 be. */
799 _Unwind_Word
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. */
808 _Unwind_Reason_Code
809 __gnu_Unwind_RaiseException (_Unwind_Control_Block *, phase2_vrs *);
811 _Unwind_Reason_Code
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)
831 return _URC_FAILURE;
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
840 register state. */
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. */
846 return _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
854 _Unwind_Resume. */
855 _Unwind_Reason_Code
856 __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *,
857 _Unwind_Stop_Fn, void *, phase2_vrs *);
859 _Unwind_Reason_Code
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);
873 _Unwind_Reason_Code
874 __gnu_Unwind_Resume (_Unwind_Control_Block *, phase2_vrs *);
876 _Unwind_Reason_Code
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. */
889 abort ();
892 /* Call the cached PR. */
893 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
894 (_US_UNWIND_FRAME_RESUME, ucbp, (_Unwind_Context *) entry_vrs);
896 switch (pr_result)
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);
906 default:
907 abort ();
911 _Unwind_Reason_Code
912 __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block *, phase2_vrs *);
914 _Unwind_Reason_Code
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. */
928 void
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. */
945 void
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. */
954 _Unwind_Reason_Code
955 __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument,
956 phase2_vrs * entry_vrs);
957 _Unwind_Reason_Code
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)
980 code = _URC_FAILURE;
981 break;
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
988 the UCB. */
989 _Unwind_SetGR((_Unwind_Context *)&saved_vrs, 12, (_Unwind_Ptr) ucbp);
991 /* Call trace function. */
992 if ((*trace) ((_Unwind_Context *) &saved_vrs, trace_argument)
993 != _URC_NO_REASON)
995 code = _URC_FAILURE;
996 break;
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);
1007 finish:
1008 restore_non_core_regs (&saved_vrs);
1009 return code;
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,
1021 int id)
1023 __gnu_unwind_state uws;
1024 _uw *data;
1025 _uw offset;
1026 _uw len;
1027 _uw rtti_count;
1028 int phase2_call_unexpected_after_unwind = 0;
1029 int in_range = 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++);
1036 uws.next = data;
1037 if (id == 0)
1039 uws.data <<= 8;
1040 uws.words_left = 0;
1041 uws.bytes_left = 3;
1043 else
1045 uws.words_left = (uws.data >> 16) & 0xff;
1046 uws.data <<= 16;
1047 uws.bytes_left = 2;
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. */
1058 while (*data)
1060 _uw addr;
1061 _uw fnstart;
1063 if (id == 2)
1065 len = ((EHT32 *) data)->length;
1066 offset = ((EHT32 *) data)->offset;
1067 data += 2;
1069 else
1071 len = ((EHT16 *) data)->length;
1072 offset = ((EHT16 *) data)->offset;
1073 data++;
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))
1082 case 0:
1083 /* Cleanup. */
1084 if (state != _US_VIRTUAL_UNWIND_FRAME
1085 && in_range)
1087 /* Cleanup in range, and we are running cleanups. */
1088 _uw lp;
1090 /* Landing pad address is 31-bit pc-relative offset. */
1091 lp = selfrel_offset31 (data);
1092 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. */
1102 data++;
1103 break;
1105 case 1:
1106 /* Catch handler. */
1107 if (state == _US_VIRTUAL_UNWIND_FRAME)
1109 if (in_range)
1111 /* Check for a barrier. */
1112 _uw rtti;
1113 bool is_reference = (data[0] & uint32_highbit) != 0;
1114 void *matched;
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,
1127 is_reference,
1128 &matched))
1129 matched = (void *)0;
1132 if (matched)
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. */
1147 _uw lp;
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. */
1156 data += 2;
1157 break;
1159 case 2:
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. */
1167 _uw i;
1168 _uw rtti;
1169 void *matched;
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,
1176 &matched))
1177 break;
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. */
1196 _uw lp;
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;
1205 else
1207 data += rtti_count + 1;
1208 /* Setup for entry to the handler. */
1209 lp = selfrel_offset31 (data);
1210 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)
1217 data++;
1218 data += rtti_count + 1;
1219 break;
1221 default:
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. */
1246 _Unwind_Reason_Code
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);
1254 _Unwind_Reason_Code
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);
1262 _Unwind_Reason_Code
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);