2011-03-21 Daniel Jacobowitz <dan@codesourcery.com>
[official-gcc.git] / gcc / config / arm / unwind-arm.c
blob4a9e2325c39afca79d05148be46ff72663a8b5cd
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
8 later version.
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/>. */
24 #include "unwind.h"
26 /* We add a prototype for abort here to avoid creating a dependency on
27 target headers. */
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,
40 bool is_reference,
41 void **matched_object);
43 _Unwind_Ptr __attribute__((weak))
44 __gnu_Unwind_Find_exidx (_Unwind_Ptr, int *);
46 /* Misc constants. */
47 #define R_IP 12
48 #define R_SP 13
49 #define R_LR 14
50 #define R_PC 15
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)
60 struct core_regs
62 _uw r[16];
65 /* We use normal integer types here to avoid the compiler generating
66 coprocessor instructions. */
67 struct vfp_regs
69 _uw64 d[16];
70 _uw pad;
73 struct vfpv3_regs
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). */
77 _uw64 d[16];
80 struct fpa_reg
82 _uw w[3];
85 struct fpa_regs
87 struct fpa_reg f[8];
90 struct wmmxd_regs
92 _uw64 wd[16];
95 struct wmmxc_regs
97 _uw wc[4];
100 /* Unwind descriptors. */
102 typedef struct
104 _uw16 length;
105 _uw16 offset;
106 } EHT16;
108 typedef struct
110 _uw length;
111 _uw offset;
112 } EHT32;
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
121 unwinding. */
123 typedef struct
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. */
129 struct vfp_regs vfp;
130 struct vfpv3_regs vfp_regs_16_to_31;
131 struct fpa_regs fpa;
132 struct wmmxd_regs wmmxd;
133 struct wmmxc_regs wmmxc;
134 } phase1_vrs;
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
141 saved if not set. */
142 #define DEMAND_SAVE_WMMXC 16 /* iWMMXt control registers have been
143 saved if not set. */
145 /* This must match the structure created by the assembly wrappers. */
146 typedef struct
148 _uw demand_save_flags;
149 struct core_regs core;
150 } phase2_vrs;
153 /* An exception index table entry. */
155 typedef struct __EIT_entry
157 _uw fnoffset;
158 _uw content;
159 } __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
182 16 through 31. */
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. */
187 static void
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);
194 else
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,
229 _uw regno,
230 _Unwind_VRS_DataRepresentation representation,
231 void *valuep)
233 phase1_vrs *vrs = (phase1_vrs *) context;
235 switch (regclass)
237 case _UVRSC_CORE:
238 if (representation != _UVRSD_UINT32
239 || regno > 15)
240 return _UVRSR_FAILED;
241 *(_uw *) valuep = vrs->core.r[regno];
242 return _UVRSR_OK;
244 case _UVRSC_VFP:
245 case _UVRSC_FPA:
246 case _UVRSC_WMMXD:
247 case _UVRSC_WMMXC:
248 return _UVRSR_NOT_IMPLEMENTED;
250 default:
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,
260 _uw regno,
261 _Unwind_VRS_DataRepresentation representation,
262 void *valuep)
264 phase1_vrs *vrs = (phase1_vrs *) context;
266 switch (regclass)
268 case _UVRSC_CORE:
269 if (representation != _UVRSD_UINT32
270 || regno > 15)
271 return _UVRSR_FAILED;
273 vrs->core.r[regno] = *(_uw *) valuep;
274 return _UVRSR_OK;
276 case _UVRSC_VFP:
277 case _UVRSC_FPA:
278 case _UVRSC_WMMXD:
279 case _UVRSC_WMMXC:
280 return _UVRSR_NOT_IMPLEMENTED;
282 default:
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,
292 _uw discriminator,
293 _Unwind_VRS_DataRepresentation representation)
295 phase1_vrs *vrs = (phase1_vrs *) context;
297 switch (regclass)
299 case _UVRSC_CORE:
301 _uw *ptr;
302 _uw mask;
303 int i;
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++)
313 if (mask & (1 << 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;
320 return _UVRSR_OK;
322 case _UVRSC_VFP:
324 _uw start = discriminator >> 16;
325 _uw count = discriminator & 0xffff;
326 struct vfp_regs tmp;
327 struct vfpv3_regs tmp_16_to_31;
328 int tmp_count;
329 _uw *sp;
330 _uw *dest;
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). */
343 if (start >= 16)
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);
362 else
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);
383 else
385 /* Save registers 0 .. 15 if required. */
386 if (start < 16)
387 __gnu_Unwind_Save_VFP_D (&tmp);
389 /* Save VFPv3 registers 16 .. 31 if required. */
390 if (num_vfpv3_regs)
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];
401 if (tmp_count > 0)
403 tmp_count *= 2;
404 dest = (_uw *) &tmp.d[start];
405 while (tmp_count--)
406 *(dest++) = *(sp++);
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--)
418 *(dest++) = *(sp++);
421 /* Skip the format word space if using FLDMX/FSTMX format. */
422 if (representation == _UVRSD_VFPX)
423 sp++;
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);
431 else
433 /* Restore registers 0 .. 15 if required. */
434 if (start < 16)
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);
442 return _UVRSR_OK;
444 case _UVRSC_FPA:
445 return _UVRSR_NOT_IMPLEMENTED;
447 case _UVRSC_WMMXD:
449 _uw start = discriminator >> 16;
450 _uw count = discriminator & 0xffff;
451 struct wmmxd_regs tmp;
452 _uw *sp;
453 _uw *dest;
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];
474 count *= 2;
475 while (count--)
476 *(dest++) = *(sp++);
478 /* Set the new stack pointer. */
479 vrs->core.r[R_SP] = (_uw) sp;
481 /* Reload the registers. */
482 __gnu_Unwind_Restore_WMMXD (&tmp);
484 return _UVRSR_OK;
486 case _UVRSC_WMMXC:
488 int i;
489 struct wmmxc_regs tmp;
490 _uw *sp;
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))
510 tmp.wc[i] = *(sp++);
512 /* Set the new stack pointer. */
513 vrs->core.r[R_SP] = (_uw) sp;
515 /* Reload the registers. */
516 __gnu_Unwind_Restore_WMMXC (&tmp);
518 return _UVRSR_OK;
520 default:
521 return _UVRSR_FAILED;
526 /* Core unwinding functions. */
528 /* Calculate the address encoded by a 31-bit self-relative offset at address
529 P. */
530 static inline _uw
531 selfrel_offset31 (const _uw *p)
533 _uw offset;
535 offset = *p;
536 /* Sign extend to 32 bits. */
537 if (offset & (1 << 30))
538 offset |= 1u << 31;
539 else
540 offset &= ~(1u << 31);
542 return offset + (_uw) p;
546 /* Perform a binary search for RETURN_ADDRESS in TABLE. The table contains
547 NREC entries. */
549 static const __EIT_entry *
550 search_EIT_table (const __EIT_entry * table, int nrec, _uw return_address)
552 _uw next_fn;
553 _uw this_fn;
554 int n, left, right;
556 if (nrec == 0)
557 return (__EIT_entry *) 0;
559 left = 0;
560 right = nrec - 1;
562 while (1)
564 n = (left + right) / 2;
565 this_fn = selfrel_offset31 (&table[n].fnoffset);
566 if (n != nrec - 1)
567 next_fn = selfrel_offset31 (&table[n + 1].fnoffset) - 1;
568 else
569 next_fn = (_uw)0 - 1;
571 if (return_address < this_fn)
573 if (n == left)
574 return (__EIT_entry *) 0;
575 right = n - 1;
577 else if (return_address <= next_fn)
578 return &table[n];
579 else
580 left = n + 1;
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;
592 int nrec;
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. */
599 return_address -= 2;
601 if (__gnu_Unwind_Find_exidx)
603 eitp = (const __EIT_entry *) __gnu_Unwind_Find_exidx (return_address,
604 &nrec);
605 if (!eitp)
607 UCB_PR_ADDR (ucbp) = 0;
608 return _URC_FAILURE;
611 else
613 eitp = &__exidx_start;
614 nrec = &__exidx_end - &__exidx_start;
617 eitp = search_EIT_table (eitp, nrec, return_address);
619 if (!eitp)
621 UCB_PR_ADDR (ucbp) = 0;
622 return _URC_FAILURE;
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;
641 else
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;
655 if (idx == 0)
656 UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr0;
657 else if (idx == 1)
658 UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr1;
659 else if (idx == 2)
660 UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr2;
661 else
662 { /* Failed */
663 UCB_PR_ADDR (ucbp) = 0;
664 return _URC_FAILURE;
667 else
669 /* Execute region offset to PR */
670 UCB_PR_ADDR (ucbp) = selfrel_offset31 (ucbp->pr_cache.ehtp);
672 return _URC_OK;
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)
687 abort ();
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)
698 abort();
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,
707 int resuming)
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
713 prev_sp field. */
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]);
732 if (resuming)
734 action = _US_UNWIND_FRAME_RESUME | _US_FORCE_UNWIND;
735 resuming = 0;
737 else
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];
752 else
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)
769 return _URC_FAILURE;
771 if (entry_code != _URC_OK)
772 return entry_code;
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. */
782 return _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
793 be. */
795 _Unwind_Word
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. */
804 _Unwind_Reason_Code
805 __gnu_Unwind_RaiseException (_Unwind_Control_Block *, phase2_vrs *);
807 _Unwind_Reason_Code
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)
827 return _URC_FAILURE;
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
836 register state. */
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. */
842 return _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
850 _Unwind_Resume. */
851 _Unwind_Reason_Code
852 __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *,
853 _Unwind_Stop_Fn, void *, phase2_vrs *);
855 _Unwind_Reason_Code
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);
869 _Unwind_Reason_Code
870 __gnu_Unwind_Resume (_Unwind_Control_Block *, phase2_vrs *);
872 _Unwind_Reason_Code
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. */
885 abort ();
888 /* Call the cached PR. */
889 pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
890 (_US_UNWIND_FRAME_RESUME, ucbp, (_Unwind_Context *) entry_vrs);
892 switch (pr_result)
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);
902 default:
903 abort ();
907 _Unwind_Reason_Code
908 __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block *, phase2_vrs *);
910 _Unwind_Reason_Code
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. */
924 void
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. */
941 void
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. */
950 _Unwind_Reason_Code
951 __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument,
952 phase2_vrs * entry_vrs);
953 _Unwind_Reason_Code
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)
976 code = _URC_FAILURE;
977 break;
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
984 the UCB. */
985 _Unwind_SetGR((_Unwind_Context *)&saved_vrs, 12, (_Unwind_Ptr) ucbp);
987 /* Call trace function. */
988 if ((*trace) ((_Unwind_Context *) &saved_vrs, trace_argument)
989 != _URC_NO_REASON)
991 code = _URC_FAILURE;
992 break;
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);
1003 restore_non_core_regs (&saved_vrs);
1004 return code;
1008 /* Common implementation for ARM ABI defined personality routines.
1009 ID is the index of the personality routine, other arguments are as defined
1010 by __aeabi_unwind_cpp_pr{0,1,2}. */
1012 static _Unwind_Reason_Code
1013 __gnu_unwind_pr_common (_Unwind_State state,
1014 _Unwind_Control_Block *ucbp,
1015 _Unwind_Context *context,
1016 int id)
1018 __gnu_unwind_state uws;
1019 _uw *data;
1020 _uw offset;
1021 _uw len;
1022 _uw rtti_count;
1023 int phase2_call_unexpected_after_unwind = 0;
1024 int in_range = 0;
1025 int forced_unwind = state & _US_FORCE_UNWIND;
1027 state &= _US_ACTION_MASK;
1029 data = (_uw *) ucbp->pr_cache.ehtp;
1030 uws.data = *(data++);
1031 uws.next = data;
1032 if (id == 0)
1034 uws.data <<= 8;
1035 uws.words_left = 0;
1036 uws.bytes_left = 3;
1038 else
1040 uws.words_left = (uws.data >> 16) & 0xff;
1041 uws.data <<= 16;
1042 uws.bytes_left = 2;
1043 data += uws.words_left;
1046 /* Restore the saved pointer. */
1047 if (state == _US_UNWIND_FRAME_RESUME)
1048 data = (_uw *) ucbp->cleanup_cache.bitpattern[0];
1050 if ((ucbp->pr_cache.additional & 1) == 0)
1052 /* Process descriptors. */
1053 while (*data)
1055 _uw addr;
1056 _uw fnstart;
1058 if (id == 2)
1060 len = ((EHT32 *) data)->length;
1061 offset = ((EHT32 *) data)->offset;
1062 data += 2;
1064 else
1066 len = ((EHT16 *) data)->length;
1067 offset = ((EHT16 *) data)->offset;
1068 data++;
1071 fnstart = ucbp->pr_cache.fnstart + (offset & ~1);
1072 addr = _Unwind_GetGR (context, R_PC);
1073 in_range = (fnstart <= addr && addr < fnstart + (len & ~1));
1075 switch (((offset & 1) << 1) | (len & 1))
1077 case 0:
1078 /* Cleanup. */
1079 if (state != _US_VIRTUAL_UNWIND_FRAME
1080 && in_range)
1082 /* Cleanup in range, and we are running cleanups. */
1083 _uw lp;
1085 /* Landing pad address is 31-bit pc-relative offset. */
1086 lp = selfrel_offset31 (data);
1087 data++;
1088 /* Save the exception data pointer. */
1089 ucbp->cleanup_cache.bitpattern[0] = (_uw) data;
1090 if (!__cxa_begin_cleanup (ucbp))
1091 return _URC_FAILURE;
1092 /* Setup the VRS to enter the landing pad. */
1093 _Unwind_SetGR (context, R_PC, lp);
1094 return _URC_INSTALL_CONTEXT;
1096 /* Cleanup not in range, or we are in stage 1. */
1097 data++;
1098 break;
1100 case 1:
1101 /* Catch handler. */
1102 if (state == _US_VIRTUAL_UNWIND_FRAME)
1104 if (in_range)
1106 /* Check for a barrier. */
1107 _uw rtti;
1108 bool is_reference = (data[0] & uint32_highbit) != 0;
1109 void *matched;
1111 /* Check for no-throw areas. */
1112 if (data[1] == (_uw) -2)
1113 return _URC_FAILURE;
1115 /* The thrown object immediately follows the ECB. */
1116 matched = (void *)(ucbp + 1);
1117 if (data[1] != (_uw) -1)
1119 /* Match a catch specification. */
1120 rtti = _Unwind_decode_target2 ((_uw) &data[1]);
1121 if (!__cxa_type_match (ucbp, (type_info *) rtti,
1122 is_reference,
1123 &matched))
1124 matched = (void *)0;
1127 if (matched)
1129 ucbp->barrier_cache.sp =
1130 _Unwind_GetGR (context, R_SP);
1131 ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
1132 ucbp->barrier_cache.bitpattern[1] = (_uw) data;
1133 return _URC_HANDLER_FOUND;
1136 /* Handler out of range, or not matched. */
1138 else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
1139 && ucbp->barrier_cache.bitpattern[1] == (_uw) data)
1141 /* Matched a previous propagation barrier. */
1142 _uw lp;
1144 /* Setup for entry to the handler. */
1145 lp = selfrel_offset31 (data);
1146 _Unwind_SetGR (context, R_PC, lp);
1147 _Unwind_SetGR (context, 0, (_uw) ucbp);
1148 return _URC_INSTALL_CONTEXT;
1150 /* Catch handler not matched. Advance to the next descriptor. */
1151 data += 2;
1152 break;
1154 case 2:
1155 rtti_count = data[0] & 0x7fffffff;
1156 /* Exception specification. */
1157 if (state == _US_VIRTUAL_UNWIND_FRAME)
1159 if (in_range && (!forced_unwind || !rtti_count))
1161 /* Match against the exception specification. */
1162 _uw i;
1163 _uw rtti;
1164 void *matched;
1166 for (i = 0; i < rtti_count; i++)
1168 matched = (void *)(ucbp + 1);
1169 rtti = _Unwind_decode_target2 ((_uw) &data[i + 1]);
1170 if (__cxa_type_match (ucbp, (type_info *) rtti, 0,
1171 &matched))
1172 break;
1175 if (i == rtti_count)
1177 /* Exception does not match the spec. */
1178 ucbp->barrier_cache.sp =
1179 _Unwind_GetGR (context, R_SP);
1180 ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
1181 ucbp->barrier_cache.bitpattern[1] = (_uw) data;
1182 return _URC_HANDLER_FOUND;
1185 /* Handler out of range, or exception is permitted. */
1187 else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
1188 && ucbp->barrier_cache.bitpattern[1] == (_uw) data)
1190 /* Matched a previous propagation barrier. */
1191 _uw lp;
1192 /* Record the RTTI list for __cxa_call_unexpected. */
1193 ucbp->barrier_cache.bitpattern[1] = rtti_count;
1194 ucbp->barrier_cache.bitpattern[2] = 0;
1195 ucbp->barrier_cache.bitpattern[3] = 4;
1196 ucbp->barrier_cache.bitpattern[4] = (_uw) &data[1];
1198 if (data[0] & uint32_highbit)
1200 data += rtti_count + 1;
1201 /* Setup for entry to the handler. */
1202 lp = selfrel_offset31 (data);
1203 data++;
1204 _Unwind_SetGR (context, R_PC, lp);
1205 _Unwind_SetGR (context, 0, (_uw) ucbp);
1206 return _URC_INSTALL_CONTEXT;
1208 else
1209 phase2_call_unexpected_after_unwind = 1;
1211 if (data[0] & uint32_highbit)
1212 data++;
1213 data += rtti_count + 1;
1214 break;
1216 default:
1217 /* Should never happen. */
1218 return _URC_FAILURE;
1220 /* Finished processing this descriptor. */
1224 if (__gnu_unwind_execute (context, &uws) != _URC_OK)
1225 return _URC_FAILURE;
1227 if (phase2_call_unexpected_after_unwind)
1229 /* Enter __cxa_unexpected as if called from the call site. */
1230 _Unwind_SetGR (context, R_LR, _Unwind_GetGR (context, R_PC));
1231 _Unwind_SetGR (context, R_PC, (_uw) &__cxa_call_unexpected);
1232 return _URC_INSTALL_CONTEXT;
1235 return _URC_CONTINUE_UNWIND;
1239 /* ABI defined personality routine entry points. */
1241 _Unwind_Reason_Code
1242 __aeabi_unwind_cpp_pr0 (_Unwind_State state,
1243 _Unwind_Control_Block *ucbp,
1244 _Unwind_Context *context)
1246 return __gnu_unwind_pr_common (state, ucbp, context, 0);
1249 _Unwind_Reason_Code
1250 __aeabi_unwind_cpp_pr1 (_Unwind_State state,
1251 _Unwind_Control_Block *ucbp,
1252 _Unwind_Context *context)
1254 return __gnu_unwind_pr_common (state, ucbp, context, 1);
1257 _Unwind_Reason_Code
1258 __aeabi_unwind_cpp_pr2 (_Unwind_State state,
1259 _Unwind_Control_Block *ucbp,
1260 _Unwind_Context *context)
1262 return __gnu_unwind_pr_common (state, ucbp, context, 2);