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