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