1 /* ARM EABI compliant unwinding routines.
2    Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3    Contributed by Paul Brook
4 
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.
9 
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.)
18 
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.
23 
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 #define __ARM_STATIC_INLINE
29 #include "unwind.h"
30 
31 /* We add a prototype for abort here to avoid creating a dependency on
32    target headers.  */
33 extern void abort (void);
34 
35 /* Definitions for C++ runtime support routines.  We make these weak
36    declarations to avoid pulling in libsupc++ unnecessarily.  */
37 typedef unsigned char bool;
38 
39 typedef struct _ZSt9type_info type_info; /* This names C++ type_info type */
40 
41 void __attribute__((weak)) __cxa_call_unexpected(_Unwind_Control_Block *ucbp);
42 bool __attribute__((weak)) __cxa_begin_cleanup(_Unwind_Control_Block *ucbp);
43 bool __attribute__((weak)) __cxa_type_match(_Unwind_Control_Block *ucbp,
44 					    const type_info *rttip,
45 					    void **matched_object);
46 
47 _Unwind_Ptr __attribute__((weak))
48 __gnu_Unwind_Find_exidx (_Unwind_Ptr, int *);
49 
50 /* Misc constants.  */
51 #define R_IP	12
52 #define R_SP	13
53 #define R_LR	14
54 #define R_PC	15
55 
56 #define EXIDX_CANTUNWIND 1
57 #define uint32_highbit (((_uw) 1) << 31)
58 
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)
63 
64 struct core_regs
65 {
66   _uw r[16];
67 };
68 
69 /* We use normal integer types here to avoid the compiler generating
70    coprocessor instructions.  */
71 struct vfp_regs
72 {
73   _uw64 d[16];
74   _uw pad;
75 };
76 
77 struct fpa_reg
78 {
79   _uw w[3];
80 };
81 
82 struct fpa_regs
83 {
84   struct fpa_reg f[8];
85 };
86 
87 /* Unwind descriptors.  */
88 
89 typedef struct
90 {
91   _uw16 length;
92   _uw16 offset;
93 } EHT16;
94 
95 typedef struct
96 {
97   _uw length;
98   _uw offset;
99 } EHT32;
100 
101 /* The ABI specifies that the unwind routines may only use core registers,
102    except when actually manipulating coprocessor state.  This allows
103    us to write one implementation that works on all platforms by
104    demand-saving coprocessor registers.
105 
106    During unwinding we hold the coprocessor state in the actual hardware
107    registers and allocate demand-save areas for use during phase1
108    unwinding.  */
109 
110 typedef struct
111 {
112   /* The first fields must be the same as a phase2_vrs.  */
113   _uw demand_save_flags;
114   struct core_regs core;
115   _uw prev_sp; /* Only valid during forced unwinding.  */
116   struct vfp_regs vfp;
117   struct fpa_regs fpa;
118 } phase1_vrs;
119 
120 #define DEMAND_SAVE_VFP 1
121 
122 /* This must match the structure created by the assembly wrappers.  */
123 typedef struct
124 {
125   _uw demand_save_flags;
126   struct core_regs core;
127 } phase2_vrs;
128 
129 
130 /* An exception index table entry.  */
131 
132 typedef struct __EIT_entry
133 {
134   _uw fnoffset;
135   _uw content;
136 } __EIT_entry;
137 
138 /* Assembly helper functions.  */
139 
140 /* Restore core register state.  Never returns.  */
141 void __attribute__((noreturn)) restore_core_regs (struct core_regs *);
142 
143 
144 /* Coprocessor register state manipulation functions.  */
145 
146 void __gnu_Unwind_Save_VFP (struct vfp_regs * p);
147 void __gnu_Unwind_Restore_VFP (struct vfp_regs * p);
148 
149 /* Restore coprocessor state after phase1 unwinding.  */
150 static void
restore_non_core_regs(phase1_vrs * vrs)151 restore_non_core_regs (phase1_vrs * vrs)
152 {
153   if ((vrs->demand_save_flags & DEMAND_SAVE_VFP) == 0)
154     __gnu_Unwind_Restore_VFP (&vrs->vfp);
155 }
156 
157 /* A better way to do this would probably be to compare the absolute address
158    with a segment relative relocation of the same symbol.  */
159 
160 extern int __text_start;
161 extern int __data_start;
162 
163 /* The exception index table location.  */
164 extern __EIT_entry __exidx_start;
165 extern __EIT_entry __exidx_end;
166 
167 /* ABI defined personality routines.  */
168 extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr0 (_Unwind_State,
169     _Unwind_Control_Block *, _Unwind_Context *);// __attribute__((weak));
170 extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr1 (_Unwind_State,
171     _Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));
172 extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr2 (_Unwind_State,
173     _Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));
174 
175 /* ABI defined routine to store a virtual register to memory.  */
176 
_Unwind_VRS_Get(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,_uw regno,_Unwind_VRS_DataRepresentation representation,void * valuep)177 _Unwind_VRS_Result _Unwind_VRS_Get (_Unwind_Context *context,
178 				    _Unwind_VRS_RegClass regclass,
179 				    _uw regno,
180 				    _Unwind_VRS_DataRepresentation representation,
181 				    void *valuep)
182 {
183   phase1_vrs *vrs = (phase1_vrs *) context;
184 
185   switch (regclass)
186     {
187     case _UVRSC_CORE:
188       if (representation != _UVRSD_UINT32
189 	  || regno > 15)
190 	return _UVRSR_FAILED;
191       *(_uw *) valuep = vrs->core.r[regno];
192       return _UVRSR_OK;
193 
194     case _UVRSC_VFP:
195     case _UVRSC_FPA:
196     case _UVRSC_WMMXD:
197     case _UVRSC_WMMXC:
198       return _UVRSR_NOT_IMPLEMENTED;
199 
200     default:
201       return _UVRSR_FAILED;
202     }
203 }
204 
205 
206 /* ABI defined function to load a virtual register from memory.  */
207 
_Unwind_VRS_Set(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,_uw regno,_Unwind_VRS_DataRepresentation representation,void * valuep)208 _Unwind_VRS_Result _Unwind_VRS_Set (_Unwind_Context *context,
209 				    _Unwind_VRS_RegClass regclass,
210 				    _uw regno,
211 				    _Unwind_VRS_DataRepresentation representation,
212 				    void *valuep)
213 {
214   phase1_vrs *vrs = (phase1_vrs *) context;
215 
216   switch (regclass)
217     {
218     case _UVRSC_CORE:
219       if (representation != _UVRSD_UINT32
220 	  || regno > 15)
221 	return _UVRSR_FAILED;
222 
223       vrs->core.r[regno] = *(_uw *) valuep;
224       return _UVRSR_OK;
225 
226     case _UVRSC_VFP:
227     case _UVRSC_FPA:
228     case _UVRSC_WMMXD:
229     case _UVRSC_WMMXC:
230       return _UVRSR_NOT_IMPLEMENTED;
231 
232     default:
233       return _UVRSR_FAILED;
234     }
235 }
236 
237 
238 /* ABI defined function to pop registers off the stack.  */
239 
_Unwind_VRS_Pop(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,_uw discriminator,_Unwind_VRS_DataRepresentation representation)240 _Unwind_VRS_Result _Unwind_VRS_Pop (_Unwind_Context *context,
241 				    _Unwind_VRS_RegClass regclass,
242 				    _uw discriminator,
243 				    _Unwind_VRS_DataRepresentation representation)
244 {
245   phase1_vrs *vrs = (phase1_vrs *) context;
246 
247   switch (regclass)
248     {
249     case _UVRSC_CORE:
250       {
251 	_uw *ptr;
252 	_uw mask;
253 	int i;
254 
255 	if (representation != _UVRSD_UINT32)
256 	  return _UVRSR_FAILED;
257 
258 	mask = discriminator & 0xffff;
259 	ptr = (_uw *) vrs->core.r[R_SP];
260 	/* Pop the requested registers.  */
261 	for (i = 0; i < 16; i++)
262 	  {
263 	    if (mask & (1 << i))
264 	      vrs->core.r[i] = *(ptr++);
265 	  }
266 	/* Writeback the stack pointer value if it wasn't restored.  */
267 	if ((mask & (1 << R_SP)) == 0)
268 	  vrs->core.r[R_SP] = (_uw) ptr;
269       }
270       return _UVRSR_OK;
271 
272     case _UVRSC_VFP:
273       {
274 	_uw start = discriminator >> 16;
275 	_uw count = discriminator & 0xffff;
276 	struct vfp_regs tmp;
277 	_uw *sp;
278 	_uw *dest;
279 
280 	if ((representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
281 	    || start + count > 16)
282 	  return _UVRSR_FAILED;
283 
284 	if (vrs->demand_save_flags & DEMAND_SAVE_VFP)
285 	  {
286 	    /* Demand-save resisters for stage1.  */
287 	    vrs->demand_save_flags &= ~DEMAND_SAVE_VFP;
288 	    __gnu_Unwind_Save_VFP (&vrs->vfp);
289 	  }
290 
291 	/* Restore the registers from the stack.  Do this by saving the
292 	   current VFP registers to a memory area, moving the in-memory
293 	   values into that area, and restoring from the whole area.
294 	   For _UVRSD_VFPX we assume FSTMX standard format 1.  */
295 	__gnu_Unwind_Save_VFP (&tmp);
296 
297 	/* The stack address is only guaranteed to be word aligned, so
298 	   we can't use doubleword copies.  */
299 	sp = (_uw *) vrs->core.r[R_SP];
300 	dest = (_uw *) &tmp.d[start];
301 	count *= 2;
302 	while (count--)
303 	  *(dest++) = *(sp++);
304 
305 	/* Skip the pad word */
306 	if (representation == _UVRSD_VFPX)
307 	  sp++;
308 
309 	/* Set the new stack pointer.  */
310 	vrs->core.r[R_SP] = (_uw) sp;
311 
312 	/* Reload the registers.  */
313 	__gnu_Unwind_Restore_VFP (&tmp);
314       }
315       return _UVRSR_OK;
316 
317     case _UVRSC_FPA:
318     case _UVRSC_WMMXD:
319     case _UVRSC_WMMXC:
320       return _UVRSR_NOT_IMPLEMENTED;
321 
322     default:
323       return _UVRSR_FAILED;
324     }
325 }
326 
327 
328 /* Core unwinding functions.  */
329 
330 /* Calculate the address encoded by a 31-bit self-relative offset at address
331    P.  */
332 static inline _uw
selfrel_offset31(const _uw * p)333 selfrel_offset31 (const _uw *p)
334 {
335   _uw offset;
336 
337   offset = *p;
338   /* Sign extend to 32 bits.  */
339   if (offset & (1 << 30))
340     offset |= 1u << 31;
341   else
342     offset &= ~(1u << 31);
343 
344   return offset + (_uw) p;
345 }
346 
347 
348 /* Perform a binary search for RETURN_ADDRESS in TABLE.  The table contains
349    NREC entries.  */
350 
351 static const __EIT_entry *
search_EIT_table(const __EIT_entry * table,int nrec,_uw return_address)352 search_EIT_table (const __EIT_entry * table, int nrec, _uw return_address)
353 {
354   _uw next_fn;
355   _uw this_fn;
356   int n, left, right;
357 
358   if (nrec == 0)
359     return (__EIT_entry *) 0;
360 
361   left = 0;
362   right = nrec - 1;
363 
364   while (1)
365     {
366       n = (left + right) / 2;
367       this_fn = selfrel_offset31 (&table[n].fnoffset);
368       if (n != nrec - 1)
369 	next_fn = selfrel_offset31 (&table[n + 1].fnoffset) - 1;
370       else
371 	next_fn = (_uw)0 - 1;
372 
373       if (return_address < this_fn)
374 	{
375 	  if (n == left)
376 	    return (__EIT_entry *) 0;
377 	  right = n - 1;
378 	}
379       else if (return_address <= next_fn)
380 	return &table[n];
381       else
382 	left = n + 1;
383     }
384 }
385 
386 /* Find the exception index table eintry for the given address.
387    Fill in the relevant fields of the UCB.
388    Returns _URC_FAILURE if an error occurred, _URC_OK on success.  */
389 
390 static _Unwind_Reason_Code
get_eit_entry(_Unwind_Control_Block * ucbp,_uw return_address)391 get_eit_entry (_Unwind_Control_Block *ucbp, _uw return_address)
392 {
393   const __EIT_entry * eitp;
394   int nrec;
395 
396   /* The return address is the address of the instruction following the
397      call instruction (plus one in thumb mode).  If this was the last
398      instruction in the function the address will lie in the following
399      function.  Subtract 2 from the address so that it points within the call
400      instruction itself.  */
401   return_address -= 2;
402 
403   if (__gnu_Unwind_Find_exidx)
404     {
405       eitp = (const __EIT_entry *) __gnu_Unwind_Find_exidx (return_address,
406 							    &nrec);
407       if (!eitp)
408 	{
409 	  UCB_PR_ADDR (ucbp) = 0;
410 	  return _URC_FAILURE;
411 	}
412     }
413   else
414     {
415       eitp = &__exidx_start;
416       nrec = &__exidx_end - &__exidx_start;
417     }
418 
419   eitp = search_EIT_table (eitp, nrec, return_address);
420 
421   if (!eitp)
422     {
423       UCB_PR_ADDR (ucbp) = 0;
424       return _URC_FAILURE;
425     }
426   ucbp->pr_cache.fnstart = selfrel_offset31 (&eitp->fnoffset);
427 
428   /* Can this frame be unwound at all?  */
429   if (eitp->content == EXIDX_CANTUNWIND)
430     {
431       UCB_PR_ADDR (ucbp) = 0;
432       return _URC_END_OF_STACK;
433     }
434 
435   /* Obtain the address of the "real" __EHT_Header word.  */
436 
437   if (eitp->content & uint32_highbit)
438     {
439       /* It is immediate data.  */
440       ucbp->pr_cache.ehtp = (_Unwind_EHT_Header *)&eitp->content;
441       ucbp->pr_cache.additional = 1;
442     }
443   else
444     {
445       /* The low 31 bits of the content field are a self-relative
446 	 offset to an _Unwind_EHT_Entry structure.  */
447       ucbp->pr_cache.ehtp =
448 	(_Unwind_EHT_Header *) selfrel_offset31 (&eitp->content);
449       ucbp->pr_cache.additional = 0;
450     }
451 
452   /* Discover the personality routine address.  */
453   if (*ucbp->pr_cache.ehtp & (1u << 31))
454     {
455       /* One of the predefined standard routines.  */
456       _uw idx = (*(_uw *) ucbp->pr_cache.ehtp >> 24) & 0xf;
457       if (idx == 0)
458 	UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr0;
459       else if (idx == 1)
460 	UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr1;
461       else if (idx == 2)
462 	UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr2;
463       else
464 	{ /* Failed */
465 	  UCB_PR_ADDR (ucbp) = 0;
466 	  return _URC_FAILURE;
467 	}
468     }
469   else
470     {
471       /* Execute region offset to PR */
472       UCB_PR_ADDR (ucbp) = selfrel_offset31 (ucbp->pr_cache.ehtp);
473     }
474   return _URC_OK;
475 }
476 
477 
478 /* Perform phase2 unwinding.  VRS is the initial virtual register state.  */
479 
480 static void __attribute__((noreturn))
unwind_phase2(_Unwind_Control_Block * ucbp,phase2_vrs * vrs)481 unwind_phase2 (_Unwind_Control_Block * ucbp, phase2_vrs * vrs)
482 {
483   _Unwind_Reason_Code pr_result;
484 
485   do
486     {
487       /* Find the entry for this routine.  */
488       if (get_eit_entry (ucbp, vrs->core.r[R_PC]) != _URC_OK)
489 	abort ();
490 
491       UCB_SAVED_CALLSITE_ADDR (ucbp) = vrs->core.r[R_PC];
492 
493       /* Call the pr to decide what to do.  */
494       pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
495 	(_US_UNWIND_FRAME_STARTING, ucbp, (_Unwind_Context *) vrs);
496     }
497   while (pr_result == _URC_CONTINUE_UNWIND);
498 
499   if (pr_result != _URC_INSTALL_CONTEXT)
500     abort();
501 
502   restore_core_regs (&vrs->core);
503 }
504 
505 /* Perform phase2 forced unwinding.  */
506 
507 static _Unwind_Reason_Code
unwind_phase2_forced(_Unwind_Control_Block * ucbp,phase2_vrs * entry_vrs,int resuming)508 unwind_phase2_forced (_Unwind_Control_Block *ucbp, phase2_vrs *entry_vrs,
509 		      int resuming)
510 {
511   _Unwind_Stop_Fn stop_fn = (_Unwind_Stop_Fn) UCB_FORCED_STOP_FN (ucbp);
512   void *stop_arg = (void *)UCB_FORCED_STOP_ARG (ucbp);
513   _Unwind_Reason_Code pr_result = 0;
514   /* We use phase1_vrs here even though we do not demand save, for the
515      prev_sp field.  */
516   phase1_vrs saved_vrs, next_vrs;
517 
518   /* Save the core registers.  */
519   saved_vrs.core = entry_vrs->core;
520   /* We don't need to demand-save the non-core registers, because we
521      unwind in a single pass.  */
522   saved_vrs.demand_save_flags = 0;
523 
524   /* Unwind until we reach a propagation barrier.  */
525   do
526     {
527       _Unwind_State action;
528       _Unwind_Reason_Code entry_code;
529       _Unwind_Reason_Code stop_code;
530 
531       /* Find the entry for this routine.  */
532       entry_code = get_eit_entry (ucbp, saved_vrs.core.r[R_PC]);
533 
534       if (resuming)
535 	{
536 	  action = _US_UNWIND_FRAME_RESUME | _US_FORCE_UNWIND;
537 	  resuming = 0;
538 	}
539       else
540 	action = _US_UNWIND_FRAME_STARTING | _US_FORCE_UNWIND;
541 
542       if (entry_code == _URC_OK)
543 	{
544 	  UCB_SAVED_CALLSITE_ADDR (ucbp) = saved_vrs.core.r[R_PC];
545 
546 	  next_vrs = saved_vrs;
547 
548 	  /* Call the pr to decide what to do.  */
549 	  pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
550 	    (action, ucbp, (void *) &next_vrs);
551 
552 	  saved_vrs.prev_sp = next_vrs.core.r[R_SP];
553 	}
554       else
555 	{
556 	  /* Treat any failure as the end of unwinding, to cope more
557 	     gracefully with missing EH information.  Mixed EH and
558 	     non-EH within one object will usually result in failure,
559 	     because the .ARM.exidx tables do not indicate the end
560 	     of the code to which they apply; but mixed EH and non-EH
561 	     shared objects should return an unwind failure at the
562 	     entry of a non-EH shared object.  */
563 	  action |= _US_END_OF_STACK;
564 
565 	  saved_vrs.prev_sp = saved_vrs.core.r[R_SP];
566 	}
567 
568       stop_code = stop_fn (1, action, ucbp->exception_class, ucbp,
569 			   (void *)&saved_vrs, stop_arg);
570       if (stop_code != _URC_NO_REASON)
571 	return _URC_FAILURE;
572 
573       if (entry_code != _URC_OK)
574 	return entry_code;
575 
576       saved_vrs = next_vrs;
577     }
578   while (pr_result == _URC_CONTINUE_UNWIND);
579 
580   if (pr_result != _URC_INSTALL_CONTEXT)
581     {
582       /* Some sort of failure has occurred in the pr and probably the
583 	 pr returned _URC_FAILURE.  */
584       return _URC_FAILURE;
585     }
586 
587   restore_core_regs (&saved_vrs.core);
588 }
589 
590 /* This is a very limited implementation of _Unwind_GetCFA.  It returns
591    the stack pointer as it is about to be unwound, and is only valid
592    while calling the stop function during forced unwinding.  If the
593    current personality routine result is going to run a cleanup, this
594    will not be the CFA; but when the frame is really unwound, it will
595    be.  */
596 
597 _Unwind_Word
_Unwind_GetCFA(_Unwind_Context * context)598 _Unwind_GetCFA (_Unwind_Context *context)
599 {
600   return ((phase1_vrs *) context)->prev_sp;
601 }
602 
603 /* Perform phase1 unwinding.  UCBP is the exception being thrown, and
604    entry_VRS is the register state on entry to _Unwind_RaiseException.  */
605 
606 _Unwind_Reason_Code
607 __gnu_Unwind_RaiseException (_Unwind_Control_Block *, phase2_vrs *);
608 
609 _Unwind_Reason_Code
__gnu_Unwind_RaiseException(_Unwind_Control_Block * ucbp,phase2_vrs * entry_vrs)610 __gnu_Unwind_RaiseException (_Unwind_Control_Block * ucbp,
611 			     phase2_vrs * entry_vrs)
612 {
613   phase1_vrs saved_vrs;
614   _Unwind_Reason_Code pr_result;
615 
616   /* Set the pc to the call site.  */
617   entry_vrs->core.r[R_PC] = entry_vrs->core.r[R_LR];
618 
619   /* Save the core registers.  */
620   saved_vrs.core = entry_vrs->core;
621   /* Set demand-save flags.  */
622   saved_vrs.demand_save_flags = ~(_uw) 0;
623 
624   /* Unwind until we reach a propagation barrier.  */
625   do
626     {
627       /* Find the entry for this routine.  */
628       if ((pr_result = get_eit_entry (ucbp, saved_vrs.core.r[R_PC])) != _URC_OK)
629 	return pr_result;
630 
631       /* Call the pr to decide what to do.  */
632       pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
633 	(_US_VIRTUAL_UNWIND_FRAME, ucbp, (void *) &saved_vrs);
634     }
635   while (pr_result == _URC_CONTINUE_UNWIND);
636 
637   /* We've unwound as far as we want to go, so restore the original
638      register state.  */
639   restore_non_core_regs (&saved_vrs);
640   if (pr_result != _URC_HANDLER_FOUND)
641     {
642       /* Some sort of failure has occurred in the pr and probably the
643 	 pr returned _URC_FAILURE.  */
644       return _URC_FAILURE;
645     }
646 
647   unwind_phase2 (ucbp, entry_vrs);
648 }
649 
650 /* Resume unwinding after a cleanup has been run.  UCBP is the exception
651    being thrown and ENTRY_VRS is the register state on entry to
652    _Unwind_Resume.  */
653 _Unwind_Reason_Code
654 __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *,
655 			   _Unwind_Stop_Fn, void *, phase2_vrs *);
656 
657 _Unwind_Reason_Code
__gnu_Unwind_ForcedUnwind(_Unwind_Control_Block * ucbp,_Unwind_Stop_Fn stop_fn,void * stop_arg,phase2_vrs * entry_vrs)658 __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *ucbp,
659 			   _Unwind_Stop_Fn stop_fn, void *stop_arg,
660 			   phase2_vrs *entry_vrs)
661 {
662   UCB_FORCED_STOP_FN (ucbp) = (_uw) stop_fn;
663   UCB_FORCED_STOP_ARG (ucbp) = (_uw) stop_arg;
664 
665   /* Set the pc to the call site.  */
666   entry_vrs->core.r[R_PC] = entry_vrs->core.r[R_LR];
667 
668   return unwind_phase2_forced (ucbp, entry_vrs, 0);
669 }
670 
671 _Unwind_Reason_Code
672 __gnu_Unwind_Resume (_Unwind_Control_Block *, phase2_vrs *);
673 
674 _Unwind_Reason_Code
__gnu_Unwind_Resume(_Unwind_Control_Block * ucbp,phase2_vrs * entry_vrs)675 __gnu_Unwind_Resume (_Unwind_Control_Block * ucbp, phase2_vrs * entry_vrs)
676 {
677   _Unwind_Reason_Code pr_result;
678 
679   /* Recover the saved address.  */
680   entry_vrs->core.r[R_PC] = UCB_SAVED_CALLSITE_ADDR (ucbp);
681 
682   if (UCB_FORCED_STOP_FN (ucbp))
683     {
684       unwind_phase2_forced (ucbp, entry_vrs, 1);
685 
686       /* We can't return failure at this point.  */
687       abort ();
688     }
689 
690   /* Call the cached PR.  */
691   pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
692 	(_US_UNWIND_FRAME_RESUME, ucbp, (_Unwind_Context *) entry_vrs);
693 
694   switch (pr_result)
695     {
696     case _URC_INSTALL_CONTEXT:
697       /* Upload the registers to enter the landing pad.  */
698       restore_core_regs (&entry_vrs->core);
699 
700     case _URC_CONTINUE_UNWIND:
701       /* Continue unwinding the next frame.  */
702       unwind_phase2 (ucbp, entry_vrs);
703 
704     default:
705       abort ();
706     }
707 }
708 
709 _Unwind_Reason_Code
710 __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block *, phase2_vrs *);
711 
712 _Unwind_Reason_Code
__gnu_Unwind_Resume_or_Rethrow(_Unwind_Control_Block * ucbp,phase2_vrs * entry_vrs)713 __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block * ucbp,
714 				phase2_vrs * entry_vrs)
715 {
716   if (!UCB_FORCED_STOP_FN (ucbp))
717     return __gnu_Unwind_RaiseException (ucbp, entry_vrs);
718 
719   /* Set the pc to the call site.  */
720   entry_vrs->core.r[R_PC] = entry_vrs->core.r[R_LR];
721   /* Continue unwinding the next frame.  */
722   return unwind_phase2_forced (ucbp, entry_vrs, 0);
723 }
724 
725 /* Clean up an exception object when unwinding is complete.  */
726 void
_Unwind_Complete(_Unwind_Control_Block * ucbp)727 _Unwind_Complete (_Unwind_Control_Block * ucbp __attribute__((unused)))
728 {
729 }
730 
731 
732 /* Get the _Unwind_Control_Block from an _Unwind_Context.  */
733 
734 static inline _Unwind_Control_Block *
unwind_UCB_from_context(_Unwind_Context * context)735 unwind_UCB_from_context (_Unwind_Context * context)
736 {
737   return (_Unwind_Control_Block *) _Unwind_GetGR (context, R_IP);
738 }
739 
740 
741 /* Free an exception.  */
742 
743 void
_Unwind_DeleteException(_Unwind_Exception * exc)744 _Unwind_DeleteException (_Unwind_Exception * exc)
745 {
746   if (exc->exception_cleanup)
747     (*exc->exception_cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exc);
748 }
749 
750 
751 /* Perform stack backtrace through unwind data.  */
752 _Unwind_Reason_Code
753 __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument,
754 		       phase2_vrs * entry_vrs);
755 _Unwind_Reason_Code
__gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace,void * trace_argument,phase2_vrs * entry_vrs)756 __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument,
757 		       phase2_vrs * entry_vrs)
758 {
759   phase1_vrs saved_vrs;
760   _Unwind_Reason_Code code;
761 
762   _Unwind_Control_Block ucb;
763   _Unwind_Control_Block *ucbp = &ucb;
764 
765   /* Set the pc to the call site.  */
766   entry_vrs->core.r[R_PC] = entry_vrs->core.r[R_LR];
767 
768   /* Save the core registers.  */
769   saved_vrs.core = entry_vrs->core;
770   /* Set demand-save flags.  */
771   saved_vrs.demand_save_flags = ~(_uw) 0;
772 
773   do
774     {
775       /* Find the entry for this routine.  */
776       if ((code = get_eit_entry (ucbp, saved_vrs.core.r[R_PC])) != _URC_OK)
777 	  break;
778 
779       /* The dwarf unwinder assumes the context structure holds things
780 	 like the function and LSDA pointers.  The ARM implementation
781 	 caches these in the exception header (UCB).  To avoid
782 	 rewriting everything we make the virtual IP register point at
783 	 the UCB.  */
784       _Unwind_SetGR((_Unwind_Context *)&saved_vrs, 12, (_Unwind_Ptr) ucbp);
785 
786       /* Call trace function.  */
787       if ((*trace) ((_Unwind_Context *) &saved_vrs, trace_argument)
788 	  != _URC_NO_REASON)
789 	{
790 	  code = _URC_FAILURE;
791 	  break;
792 	}
793 
794       /* Call the pr to decide what to do.  */
795       code = ((personality_routine) UCB_PR_ADDR (ucbp))
796 	(_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND,
797 	 ucbp, (void *) &saved_vrs);
798     }
799   while (code != _URC_END_OF_STACK
800 	 && code != _URC_FAILURE);
801 
802  finish:
803   restore_non_core_regs (&saved_vrs);
804   return code;
805 }
806 
807 
808 /* Common implementation for ARM ABI defined personality routines.
809    ID is the index of the personality routine, other arguments are as defined
810    by __aeabi_unwind_cpp_pr{0,1,2}.  */
811 
812 static _Unwind_Reason_Code
__gnu_unwind_pr_common(_Unwind_State state,_Unwind_Control_Block * ucbp,_Unwind_Context * context,int id)813 __gnu_unwind_pr_common (_Unwind_State state,
814 			_Unwind_Control_Block *ucbp,
815 			_Unwind_Context *context,
816 			int id)
817 {
818   __gnu_unwind_state uws;
819   _uw *data;
820   _uw offset;
821   _uw len;
822   _uw rtti_count;
823   int phase2_call_unexpected_after_unwind = 0;
824   int in_range = 0;
825   int forced_unwind = state & _US_FORCE_UNWIND;
826 
827   state &= _US_ACTION_MASK;
828 
829   data = (_uw *) ucbp->pr_cache.ehtp;
830   uws.data = *(data++);
831   uws.next = data;
832   if (id == 0)
833     {
834       uws.data <<= 8;
835       uws.words_left = 0;
836       uws.bytes_left = 3;
837     }
838   else
839     {
840       uws.words_left = (uws.data >> 16) & 0xff;
841       uws.data <<= 16;
842       uws.bytes_left = 2;
843       data += uws.words_left;
844     }
845 
846   /* Restore the saved pointer.  */
847   if (state == _US_UNWIND_FRAME_RESUME)
848     data = (_uw *) ucbp->cleanup_cache.bitpattern[0];
849 
850   if ((ucbp->pr_cache.additional & 1) == 0)
851     {
852       /* Process descriptors.  */
853       while (*data)
854 	{
855 	  _uw addr;
856 	  _uw fnstart;
857 
858 	  if (id == 2)
859 	    {
860 	      len = ((EHT32 *) data)->length;
861 	      offset = ((EHT32 *) data)->offset;
862 	      data += 2;
863 	    }
864 	  else
865 	    {
866 	      len = ((EHT16 *) data)->length;
867 	      offset = ((EHT16 *) data)->offset;
868 	      data++;
869 	    }
870 
871 	  fnstart = ucbp->pr_cache.fnstart + (offset & ~1);
872 	  addr = _Unwind_GetGR (context, R_PC);
873 	  in_range = (fnstart <= addr && addr < fnstart + (len & ~1));
874 
875 	  switch (((offset & 1) << 1) | (len & 1))
876 	    {
877 	    case 0:
878 	      /* Cleanup.  */
879 	      if (state != _US_VIRTUAL_UNWIND_FRAME
880 		  && in_range)
881 		{
882 		  /* Cleanup in range, and we are running cleanups.  */
883 		  _uw lp;
884 
885 		  /* Landing pad address is 31-bit pc-relative offset.  */
886 		  lp = selfrel_offset31 (data);
887 		  data++;
888 		  /* Save the exception data pointer.  */
889 		  ucbp->cleanup_cache.bitpattern[0] = (_uw) data;
890 		  if (!__cxa_begin_cleanup (ucbp))
891 		    return _URC_FAILURE;
892 		  /* Setup the VRS to enter the landing pad.  */
893 		  _Unwind_SetGR (context, R_PC, lp);
894 		  return _URC_INSTALL_CONTEXT;
895 		}
896 	      /* Cleanup not in range, or we are in stage 1.  */
897 	      data++;
898 	      break;
899 
900 	    case 1:
901 	      /* Catch handler.  */
902 	      if (state == _US_VIRTUAL_UNWIND_FRAME)
903 		{
904 		  if (in_range)
905 		    {
906 		      /* Check for a barrier.  */
907 		      _uw rtti;
908 		      void *matched;
909 
910 		      /* Check for no-throw areas.  */
911 		      if (data[1] == (_uw) -2)
912 			return _URC_FAILURE;
913 
914 		      /* The thrown object immediately follows the ECB.  */
915 		      matched = (void *)(ucbp + 1);
916 		      if (data[1] != (_uw) -1)
917 			{
918 			  /* Match a catch specification.  */
919 			  rtti = _Unwind_decode_target2 ((_uw) &data[1]);
920 			  if (!__cxa_type_match (ucbp, (type_info *) rtti,
921 						 &matched))
922 			    matched = (void *)0;
923 			}
924 
925 		      if (matched)
926 			{
927 			  ucbp->barrier_cache.sp =
928 			    _Unwind_GetGR (context, R_SP);
929 			  ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
930 			  ucbp->barrier_cache.bitpattern[1] = (_uw) data;
931 			  return _URC_HANDLER_FOUND;
932 			}
933 		    }
934 		  /* Handler out of range, or not matched.  */
935 		}
936 	      else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
937 		       && ucbp->barrier_cache.bitpattern[1] == (_uw) data)
938 		{
939 		  /* Matched a previous propagation barrier.  */
940 		  _uw lp;
941 
942 		  /* Setup for entry to the handler.  */
943 		  lp = selfrel_offset31 (data);
944 		  _Unwind_SetGR (context, R_PC, lp);
945 		  _Unwind_SetGR (context, 0, (_uw) ucbp);
946 		  return _URC_INSTALL_CONTEXT;
947 		}
948 	      /* Catch handler not matched.  Advance to the next descriptor.  */
949 	      data += 2;
950 	      break;
951 
952 	    case 2:
953 	      rtti_count = data[0] & 0x7fffffff;
954 	      /* Exception specification.  */
955 	      if (state == _US_VIRTUAL_UNWIND_FRAME)
956 		{
957 		  if (in_range && (!forced_unwind || !rtti_count))
958 		    {
959 		      /* Match against the exception specification.  */
960 		      _uw i;
961 		      _uw rtti;
962 		      void *matched;
963 
964 		      for (i = 0; i < rtti_count; i++)
965 			{
966 			  matched = (void *)(ucbp + 1);
967 			  rtti = _Unwind_decode_target2 ((_uw) &data[i + 1]);
968 			  if (__cxa_type_match (ucbp, (type_info *) rtti,
969 						&matched))
970 			    break;
971 			}
972 
973 		      if (i == rtti_count)
974 			{
975 			  /* Exception does not match the spec.  */
976 			  ucbp->barrier_cache.sp =
977 			    _Unwind_GetGR (context, R_SP);
978 			  ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
979 			  ucbp->barrier_cache.bitpattern[1] = (_uw) data;
980 			  return _URC_HANDLER_FOUND;
981 			}
982 		    }
983 		  /* Handler out of range, or exception is permitted.  */
984 		}
985 	      else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
986 		       && ucbp->barrier_cache.bitpattern[1] == (_uw) data)
987 		{
988 		  /* Matched a previous propagation barrier.  */
989 		  _uw lp;
990 		  /* Record the RTTI list for __cxa_call_unexpected.  */
991 		  ucbp->barrier_cache.bitpattern[1] = rtti_count;
992 		  ucbp->barrier_cache.bitpattern[2] = 0;
993 		  ucbp->barrier_cache.bitpattern[3] = 4;
994 		  ucbp->barrier_cache.bitpattern[4] = (_uw) &data[1];
995 
996 		  if (data[0] & uint32_highbit)
997 		    phase2_call_unexpected_after_unwind = 1;
998 		  else
999 		    {
1000 		      data += rtti_count + 1;
1001 		      /* Setup for entry to the handler.  */
1002 		      lp = selfrel_offset31 (data);
1003 		      data++;
1004 		      _Unwind_SetGR (context, R_PC, lp);
1005 		      _Unwind_SetGR (context, 0, (_uw) ucbp);
1006 		      return _URC_INSTALL_CONTEXT;
1007 		    }
1008 		}
1009 	      if (data[0] & uint32_highbit)
1010 		data++;
1011 	      data += rtti_count + 1;
1012 	      break;
1013 
1014 	    default:
1015 	      /* Should never happen.  */
1016 	      return _URC_FAILURE;
1017 	    }
1018 	  /* Finished processing this descriptor.  */
1019 	}
1020     }
1021 
1022   if (__gnu_unwind_execute (context, &uws) != _URC_OK)
1023     return _URC_FAILURE;
1024 
1025   if (phase2_call_unexpected_after_unwind)
1026     {
1027       /* Enter __cxa_unexpected as if called from the call site.  */
1028       _Unwind_SetGR (context, R_LR, _Unwind_GetGR (context, R_PC));
1029       _Unwind_SetGR (context, R_PC, (_uw) &__cxa_call_unexpected);
1030       return _URC_INSTALL_CONTEXT;
1031     }
1032 
1033   return _URC_CONTINUE_UNWIND;
1034 }
1035 
1036 
1037 /* ABI defined personality routine entry points.  */
1038 
1039 _Unwind_Reason_Code
__aeabi_unwind_cpp_pr0(_Unwind_State state,_Unwind_Control_Block * ucbp,_Unwind_Context * context)1040 __aeabi_unwind_cpp_pr0 (_Unwind_State state,
1041 			_Unwind_Control_Block *ucbp,
1042 			_Unwind_Context *context)
1043 {
1044   return __gnu_unwind_pr_common (state, ucbp, context, 0);
1045 }
1046 
1047 _Unwind_Reason_Code
__aeabi_unwind_cpp_pr1(_Unwind_State state,_Unwind_Control_Block * ucbp,_Unwind_Context * context)1048 __aeabi_unwind_cpp_pr1 (_Unwind_State state,
1049 			_Unwind_Control_Block *ucbp,
1050 			_Unwind_Context *context)
1051 {
1052   return __gnu_unwind_pr_common (state, ucbp, context, 1);
1053 }
1054 
1055 _Unwind_Reason_Code
__aeabi_unwind_cpp_pr2(_Unwind_State state,_Unwind_Control_Block * ucbp,_Unwind_Context * context)1056 __aeabi_unwind_cpp_pr2 (_Unwind_State state,
1057 			_Unwind_Control_Block *ucbp,
1058 			_Unwind_Context *context)
1059 {
1060   return __gnu_unwind_pr_common (state, ucbp, context, 2);
1061 }
1062 
1063 /* These two should never be used.  */
1064 _Unwind_Ptr
_Unwind_GetDataRelBase(_Unwind_Context * context)1065 _Unwind_GetDataRelBase (_Unwind_Context *context __attribute__ ((unused)))
1066 {
1067   abort ();
1068 }
1069 
1070 _Unwind_Ptr
_Unwind_GetTextRelBase(_Unwind_Context * context)1071 _Unwind_GetTextRelBase (_Unwind_Context *context __attribute__ ((unused)))
1072 {
1073   abort ();
1074 }
1075 
1076 #ifdef __FreeBSD__
1077 /* FreeBSD expects these to be functions */
1078 _Unwind_Ptr
_Unwind_GetIP(struct _Unwind_Context * context)1079 _Unwind_GetIP (struct _Unwind_Context *context)
1080 {
1081   return _Unwind_GetGR (context, 15) & ~(_Unwind_Word)1;
1082 }
1083 
1084 _Unwind_Ptr
_Unwind_GetIPInfo(struct _Unwind_Context * context,int * ip_before_insn)1085 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
1086 {
1087   *ip_before_insn = 0;
1088   return _Unwind_GetGR (context, 15) & ~(_Unwind_Word)1;
1089 }
1090 
1091 void
_Unwind_SetIP(struct _Unwind_Context * context,_Unwind_Ptr val)1092 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
1093 {
1094   _Unwind_SetGR (context, 15, val | (_Unwind_GetGR (context, 15) & 1));
1095 }
1096 
1097 #endif
1098