unwind-arm-common.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /* Common unwinding code for ARM EABI and C6X.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. Contributed by Paul Brook
  4. This file is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. This file is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "tconfig.h"
  20. #include "tsystem.h"
  21. #include "unwind.h"
  22. /* Used for SystemTap unwinder probe. */
  23. #ifdef HAVE_SYS_SDT_H
  24. #include <sys/sdt.h>
  25. #endif
  26. /* We add a prototype for abort here to avoid creating a dependency on
  27. target headers. */
  28. extern void abort (void);
  29. /* Definitions for C++ runtime support routines. We make these weak
  30. declarations to avoid pulling in libsupc++ unnecessarily. */
  31. typedef unsigned char bool;
  32. typedef struct _ZSt9type_info type_info; /* This names C++ type_info type */
  33. enum __cxa_type_match_result
  34. {
  35. ctm_failed = 0,
  36. ctm_succeeded = 1,
  37. ctm_succeeded_with_ptr_to_base = 2
  38. };
  39. void __attribute__((weak)) __cxa_call_unexpected(_Unwind_Control_Block *ucbp);
  40. bool __attribute__((weak)) __cxa_begin_cleanup(_Unwind_Control_Block *ucbp);
  41. enum __cxa_type_match_result __attribute__((weak)) __cxa_type_match
  42. (_Unwind_Control_Block *ucbp, const type_info *rttip,
  43. bool is_reference, void **matched_object);
  44. _Unwind_Ptr __attribute__((weak))
  45. __gnu_Unwind_Find_exidx (_Unwind_Ptr, int *);
  46. #define EXIDX_CANTUNWIND 1
  47. #define uint32_highbit (((_uw) 1) << 31)
  48. #define UCB_FORCED_STOP_FN(ucbp) ((ucbp)->unwinder_cache.reserved1)
  49. #define UCB_PR_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved2)
  50. #define UCB_SAVED_CALLSITE_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved3)
  51. #define UCB_FORCED_STOP_ARG(ucbp) ((ucbp)->unwinder_cache.reserved4)
  52. /* Unwind descriptors. */
  53. typedef struct
  54. {
  55. _uw16 length;
  56. _uw16 offset;
  57. } EHT16;
  58. typedef struct
  59. {
  60. _uw length;
  61. _uw offset;
  62. } EHT32;
  63. /* An exception index table entry. */
  64. typedef struct __EIT_entry
  65. {
  66. _uw fnoffset;
  67. _uw content;
  68. } __EIT_entry;
  69. /* Assembly helper functions. */
  70. /* Restore core register state. Never returns. */
  71. void __attribute__((noreturn)) restore_core_regs (struct core_regs *);
  72. /* Restore coprocessor state after phase1 unwinding. */
  73. static void restore_non_core_regs (phase1_vrs * vrs);
  74. /* A better way to do this would probably be to compare the absolute address
  75. with a segment relative relocation of the same symbol. */
  76. extern int __text_start;
  77. extern int __data_start;
  78. /* The exception index table location. */
  79. extern __EIT_entry __exidx_start;
  80. extern __EIT_entry __exidx_end;
  81. /* Core unwinding functions. */
  82. /* Calculate the address encoded by a 31-bit self-relative offset at address
  83. P. */
  84. static inline _uw selfrel_offset31 (const _uw *p);
  85. static _uw __gnu_unwind_get_pr_addr (int idx);
  86. static void _Unwind_DebugHook (void *, void *)
  87. __attribute__ ((__noinline__, __used__, __noclone__));
  88. /* This function is called during unwinding. It is intended as a hook
  89. for a debugger to intercept exceptions. CFA is the CFA of the
  90. target frame. HANDLER is the PC to which control will be
  91. transferred. */
  92. static void
  93. _Unwind_DebugHook (void *cfa __attribute__ ((__unused__)),
  94. void *handler __attribute__ ((__unused__)))
  95. {
  96. /* We only want to use stap probes starting with v3. Earlier
  97. versions added too much startup cost. */
  98. #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
  99. STAP_PROBE2 (libgcc, unwind, cfa, handler);
  100. #else
  101. asm ("");
  102. #endif
  103. }
  104. /* This is a wrapper to be called when we need to restore core registers.
  105. It will call `_Unwind_DebugHook' before restoring the registers, thus
  106. making it possible to intercept and debug exceptions.
  107. When calling `_Unwind_DebugHook', the first argument (the CFA) is zero
  108. because we are not interested in it. However, it must be there (even
  109. being zero) because GDB expects to find it when using the probe. */
  110. #define uw_restore_core_regs(TARGET, CORE) \
  111. do \
  112. { \
  113. void *handler = __builtin_frob_return_addr ((void *) VRS_PC (TARGET)); \
  114. _Unwind_DebugHook (0, handler); \
  115. restore_core_regs (CORE); \
  116. } \
  117. while (0)
  118. /* Perform a binary search for RETURN_ADDRESS in TABLE. The table contains
  119. NREC entries. */
  120. static const __EIT_entry *
  121. search_EIT_table (const __EIT_entry * table, int nrec, _uw return_address)
  122. {
  123. _uw next_fn;
  124. _uw this_fn;
  125. int n, left, right;
  126. if (nrec == 0)
  127. return (__EIT_entry *) 0;
  128. left = 0;
  129. right = nrec - 1;
  130. while (1)
  131. {
  132. n = (left + right) / 2;
  133. this_fn = selfrel_offset31 (&table[n].fnoffset);
  134. if (n != nrec - 1)
  135. next_fn = selfrel_offset31 (&table[n + 1].fnoffset) - 1;
  136. else
  137. next_fn = (_uw)0 - 1;
  138. if (return_address < this_fn)
  139. {
  140. if (n == left)
  141. return (__EIT_entry *) 0;
  142. right = n - 1;
  143. }
  144. else if (return_address <= next_fn)
  145. return &table[n];
  146. else
  147. left = n + 1;
  148. }
  149. }
  150. /* Find the exception index table eintry for the given address.
  151. Fill in the relevant fields of the UCB.
  152. Returns _URC_FAILURE if an error occurred, _URC_OK on success. */
  153. static _Unwind_Reason_Code
  154. get_eit_entry (_Unwind_Control_Block *ucbp, _uw return_address)
  155. {
  156. const __EIT_entry * eitp;
  157. int nrec;
  158. /* The return address is the address of the instruction following the
  159. call instruction (plus one in thumb mode). If this was the last
  160. instruction in the function the address will lie in the following
  161. function. Subtract 2 from the address so that it points within the call
  162. instruction itself. */
  163. return_address -= 2;
  164. if (__gnu_Unwind_Find_exidx)
  165. {
  166. eitp = (const __EIT_entry *) __gnu_Unwind_Find_exidx (return_address,
  167. &nrec);
  168. if (!eitp)
  169. {
  170. UCB_PR_ADDR (ucbp) = 0;
  171. return _URC_FAILURE;
  172. }
  173. }
  174. else
  175. {
  176. eitp = &__exidx_start;
  177. nrec = &__exidx_end - &__exidx_start;
  178. }
  179. eitp = search_EIT_table (eitp, nrec, return_address);
  180. if (!eitp)
  181. {
  182. UCB_PR_ADDR (ucbp) = 0;
  183. return _URC_FAILURE;
  184. }
  185. ucbp->pr_cache.fnstart = selfrel_offset31 (&eitp->fnoffset);
  186. /* Can this frame be unwound at all? */
  187. if (eitp->content == EXIDX_CANTUNWIND)
  188. {
  189. UCB_PR_ADDR (ucbp) = 0;
  190. return _URC_END_OF_STACK;
  191. }
  192. /* Obtain the address of the "real" __EHT_Header word. */
  193. if (eitp->content & uint32_highbit)
  194. {
  195. /* It is immediate data. */
  196. ucbp->pr_cache.ehtp = (_Unwind_EHT_Header *)&eitp->content;
  197. ucbp->pr_cache.additional = 1;
  198. }
  199. else
  200. {
  201. /* The low 31 bits of the content field are a self-relative
  202. offset to an _Unwind_EHT_Entry structure. */
  203. ucbp->pr_cache.ehtp =
  204. (_Unwind_EHT_Header *) selfrel_offset31 (&eitp->content);
  205. ucbp->pr_cache.additional = 0;
  206. }
  207. /* Discover the personality routine address. */
  208. if (*ucbp->pr_cache.ehtp & (1u << 31))
  209. {
  210. /* One of the predefined standard routines. */
  211. _uw idx = (*(_uw *) ucbp->pr_cache.ehtp >> 24) & 0xf;
  212. UCB_PR_ADDR (ucbp) = __gnu_unwind_get_pr_addr (idx);
  213. if (UCB_PR_ADDR (ucbp) == 0)
  214. {
  215. /* Failed */
  216. return _URC_FAILURE;
  217. }
  218. }
  219. else
  220. {
  221. /* Execute region offset to PR */
  222. UCB_PR_ADDR (ucbp) = selfrel_offset31 (ucbp->pr_cache.ehtp);
  223. }
  224. return _URC_OK;
  225. }
  226. /* Perform phase2 unwinding. VRS is the initial virtual register state. */
  227. static void __attribute__((noreturn))
  228. unwind_phase2 (_Unwind_Control_Block * ucbp, phase2_vrs * vrs)
  229. {
  230. _Unwind_Reason_Code pr_result;
  231. do
  232. {
  233. /* Find the entry for this routine. */
  234. if (get_eit_entry (ucbp, VRS_PC(vrs)) != _URC_OK)
  235. abort ();
  236. UCB_SAVED_CALLSITE_ADDR (ucbp) = VRS_PC(vrs);
  237. /* Call the pr to decide what to do. */
  238. pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
  239. (_US_UNWIND_FRAME_STARTING, ucbp, (_Unwind_Context *) vrs);
  240. }
  241. while (pr_result == _URC_CONTINUE_UNWIND);
  242. if (pr_result != _URC_INSTALL_CONTEXT)
  243. abort();
  244. uw_restore_core_regs (vrs, &vrs->core);
  245. }
  246. /* Perform phase2 forced unwinding. */
  247. static _Unwind_Reason_Code
  248. unwind_phase2_forced (_Unwind_Control_Block *ucbp, phase2_vrs *entry_vrs,
  249. int resuming)
  250. {
  251. _Unwind_Stop_Fn stop_fn = (_Unwind_Stop_Fn) UCB_FORCED_STOP_FN (ucbp);
  252. void *stop_arg = (void *)UCB_FORCED_STOP_ARG (ucbp);
  253. _Unwind_Reason_Code pr_result = 0;
  254. /* We use phase1_vrs here even though we do not demand save, for the
  255. prev_sp field. */
  256. phase1_vrs saved_vrs, next_vrs;
  257. /* Save the core registers. */
  258. saved_vrs.core = entry_vrs->core;
  259. /* We don't need to demand-save the non-core registers, because we
  260. unwind in a single pass. */
  261. saved_vrs.demand_save_flags = 0;
  262. /* Unwind until we reach a propagation barrier. */
  263. do
  264. {
  265. _Unwind_State action;
  266. _Unwind_Reason_Code entry_code;
  267. _Unwind_Reason_Code stop_code;
  268. /* Find the entry for this routine. */
  269. entry_code = get_eit_entry (ucbp, VRS_PC (&saved_vrs));
  270. if (resuming)
  271. {
  272. action = _US_UNWIND_FRAME_RESUME | _US_FORCE_UNWIND;
  273. resuming = 0;
  274. }
  275. else
  276. action = _US_UNWIND_FRAME_STARTING | _US_FORCE_UNWIND;
  277. if (entry_code == _URC_OK)
  278. {
  279. UCB_SAVED_CALLSITE_ADDR (ucbp) = VRS_PC (&saved_vrs);
  280. next_vrs = saved_vrs;
  281. /* Call the pr to decide what to do. */
  282. pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
  283. (action, ucbp, (void *) &next_vrs);
  284. saved_vrs.prev_sp = VRS_SP (&next_vrs);
  285. }
  286. else
  287. {
  288. /* Treat any failure as the end of unwinding, to cope more
  289. gracefully with missing EH information. Mixed EH and
  290. non-EH within one object will usually result in failure,
  291. because the .ARM.exidx tables do not indicate the end
  292. of the code to which they apply; but mixed EH and non-EH
  293. shared objects should return an unwind failure at the
  294. entry of a non-EH shared object. */
  295. action |= _US_END_OF_STACK;
  296. saved_vrs.prev_sp = VRS_SP (&saved_vrs);
  297. }
  298. stop_code = stop_fn (1, action, ucbp->exception_class, ucbp,
  299. (void *)&saved_vrs, stop_arg);
  300. if (stop_code != _URC_NO_REASON)
  301. return _URC_FAILURE;
  302. if (entry_code != _URC_OK)
  303. return entry_code;
  304. saved_vrs = next_vrs;
  305. }
  306. while (pr_result == _URC_CONTINUE_UNWIND);
  307. if (pr_result != _URC_INSTALL_CONTEXT)
  308. {
  309. /* Some sort of failure has occurred in the pr and probably the
  310. pr returned _URC_FAILURE. */
  311. return _URC_FAILURE;
  312. }
  313. uw_restore_core_regs (&saved_vrs, &saved_vrs.core);
  314. }
  315. /* This is a very limited implementation of _Unwind_GetCFA. It returns
  316. the stack pointer as it is about to be unwound, and is only valid
  317. while calling the stop function during forced unwinding. If the
  318. current personality routine result is going to run a cleanup, this
  319. will not be the CFA; but when the frame is really unwound, it will
  320. be. */
  321. _Unwind_Word
  322. _Unwind_GetCFA (_Unwind_Context *context)
  323. {
  324. return ((phase1_vrs *) context)->prev_sp;
  325. }
  326. /* Perform phase1 unwinding. UCBP is the exception being thrown, and
  327. entry_VRS is the register state on entry to _Unwind_RaiseException. */
  328. _Unwind_Reason_Code
  329. __gnu_Unwind_RaiseException (_Unwind_Control_Block *, phase2_vrs *);
  330. _Unwind_Reason_Code
  331. __gnu_Unwind_RaiseException (_Unwind_Control_Block * ucbp,
  332. phase2_vrs * entry_vrs)
  333. {
  334. phase1_vrs saved_vrs;
  335. _Unwind_Reason_Code pr_result;
  336. /* Set the pc to the call site. */
  337. VRS_PC (entry_vrs) = VRS_RETURN(entry_vrs);
  338. /* Save the core registers. */
  339. saved_vrs.core = entry_vrs->core;
  340. /* Set demand-save flags. */
  341. saved_vrs.demand_save_flags = ~(_uw) 0;
  342. /* Unwind until we reach a propagation barrier. */
  343. do
  344. {
  345. /* Find the entry for this routine. */
  346. if (get_eit_entry (ucbp, VRS_PC (&saved_vrs)) != _URC_OK)
  347. return _URC_FAILURE;
  348. /* Call the pr to decide what to do. */
  349. pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
  350. (_US_VIRTUAL_UNWIND_FRAME, ucbp, (void *) &saved_vrs);
  351. }
  352. while (pr_result == _URC_CONTINUE_UNWIND);
  353. /* We've unwound as far as we want to go, so restore the original
  354. register state. */
  355. restore_non_core_regs (&saved_vrs);
  356. if (pr_result != _URC_HANDLER_FOUND)
  357. {
  358. /* Some sort of failure has occurred in the pr and probably the
  359. pr returned _URC_FAILURE. */
  360. return _URC_FAILURE;
  361. }
  362. unwind_phase2 (ucbp, entry_vrs);
  363. }
  364. /* Resume unwinding after a cleanup has been run. UCBP is the exception
  365. being thrown and ENTRY_VRS is the register state on entry to
  366. _Unwind_Resume. */
  367. _Unwind_Reason_Code
  368. __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *,
  369. _Unwind_Stop_Fn, void *, phase2_vrs *);
  370. _Unwind_Reason_Code
  371. __gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *ucbp,
  372. _Unwind_Stop_Fn stop_fn, void *stop_arg,
  373. phase2_vrs *entry_vrs)
  374. {
  375. UCB_FORCED_STOP_FN (ucbp) = (_uw) stop_fn;
  376. UCB_FORCED_STOP_ARG (ucbp) = (_uw) stop_arg;
  377. /* Set the pc to the call site. */
  378. VRS_PC (entry_vrs) = VRS_RETURN(entry_vrs);
  379. return unwind_phase2_forced (ucbp, entry_vrs, 0);
  380. }
  381. _Unwind_Reason_Code
  382. __gnu_Unwind_Resume (_Unwind_Control_Block *, phase2_vrs *);
  383. _Unwind_Reason_Code
  384. __gnu_Unwind_Resume (_Unwind_Control_Block * ucbp, phase2_vrs * entry_vrs)
  385. {
  386. _Unwind_Reason_Code pr_result;
  387. /* Recover the saved address. */
  388. VRS_PC (entry_vrs) = UCB_SAVED_CALLSITE_ADDR (ucbp);
  389. if (UCB_FORCED_STOP_FN (ucbp))
  390. {
  391. unwind_phase2_forced (ucbp, entry_vrs, 1);
  392. /* We can't return failure at this point. */
  393. abort ();
  394. }
  395. /* Call the cached PR. */
  396. pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
  397. (_US_UNWIND_FRAME_RESUME, ucbp, (_Unwind_Context *) entry_vrs);
  398. switch (pr_result)
  399. {
  400. case _URC_INSTALL_CONTEXT:
  401. /* Upload the registers to enter the landing pad. */
  402. uw_restore_core_regs (entry_vrs, &entry_vrs->core);
  403. case _URC_CONTINUE_UNWIND:
  404. /* Continue unwinding the next frame. */
  405. unwind_phase2 (ucbp, entry_vrs);
  406. default:
  407. abort ();
  408. }
  409. }
  410. _Unwind_Reason_Code
  411. __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block *, phase2_vrs *);
  412. _Unwind_Reason_Code
  413. __gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block * ucbp,
  414. phase2_vrs * entry_vrs)
  415. {
  416. if (!UCB_FORCED_STOP_FN (ucbp))
  417. return __gnu_Unwind_RaiseException (ucbp, entry_vrs);
  418. /* Set the pc to the call site. */
  419. VRS_PC (entry_vrs) = VRS_RETURN (entry_vrs);
  420. /* Continue unwinding the next frame. */
  421. return unwind_phase2_forced (ucbp, entry_vrs, 0);
  422. }
  423. /* Clean up an exception object when unwinding is complete. */
  424. void
  425. _Unwind_Complete (_Unwind_Control_Block * ucbp __attribute__((unused)))
  426. {
  427. }
  428. /* Free an exception. */
  429. void
  430. _Unwind_DeleteException (_Unwind_Exception * exc)
  431. {
  432. if (exc->exception_cleanup)
  433. (*exc->exception_cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exc);
  434. }
  435. /* Perform stack backtrace through unwind data. */
  436. _Unwind_Reason_Code
  437. __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument,
  438. phase2_vrs * entry_vrs);
  439. _Unwind_Reason_Code
  440. __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument,
  441. phase2_vrs * entry_vrs)
  442. {
  443. phase1_vrs saved_vrs;
  444. _Unwind_Reason_Code code;
  445. _Unwind_Control_Block ucb;
  446. _Unwind_Control_Block *ucbp = &ucb;
  447. /* Set the pc to the call site. */
  448. VRS_PC (entry_vrs) = VRS_RETURN (entry_vrs);
  449. /* Save the core registers. */
  450. saved_vrs.core = entry_vrs->core;
  451. /* Set demand-save flags. */
  452. saved_vrs.demand_save_flags = ~(_uw) 0;
  453. do
  454. {
  455. /* Find the entry for this routine. */
  456. if (get_eit_entry (ucbp, VRS_PC (&saved_vrs)) != _URC_OK)
  457. {
  458. code = _URC_FAILURE;
  459. break;
  460. }
  461. /* The dwarf unwinder assumes the context structure holds things
  462. like the function and LSDA pointers. The ARM implementation
  463. caches these in the exception header (UCB). To avoid
  464. rewriting everything we make the virtual IP register point at
  465. the UCB. */
  466. _Unwind_SetGR((_Unwind_Context *)&saved_vrs, UNWIND_POINTER_REG, (_Unwind_Ptr) ucbp);
  467. /* Call trace function. */
  468. if ((*trace) ((_Unwind_Context *) &saved_vrs, trace_argument)
  469. != _URC_NO_REASON)
  470. {
  471. code = _URC_FAILURE;
  472. break;
  473. }
  474. /* Call the pr to decide what to do. */
  475. code = ((personality_routine) UCB_PR_ADDR (ucbp))
  476. (_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND,
  477. ucbp, (void *) &saved_vrs);
  478. }
  479. while (code != _URC_END_OF_STACK
  480. && code != _URC_FAILURE);
  481. restore_non_core_regs (&saved_vrs);
  482. return code;
  483. }
  484. /* Common implementation for ARM ABI defined personality routines.
  485. ID is the index of the personality routine, other arguments are as defined
  486. by __aeabi_unwind_cpp_pr{0,1,2}. */
  487. static _Unwind_Reason_Code
  488. __gnu_unwind_pr_common (_Unwind_State state,
  489. _Unwind_Control_Block *ucbp,
  490. _Unwind_Context *context,
  491. int id)
  492. {
  493. __gnu_unwind_state uws;
  494. _uw *data;
  495. _uw offset;
  496. _uw len;
  497. _uw rtti_count;
  498. int phase2_call_unexpected_after_unwind = 0;
  499. int in_range = 0;
  500. int forced_unwind = state & _US_FORCE_UNWIND;
  501. state &= _US_ACTION_MASK;
  502. data = (_uw *) ucbp->pr_cache.ehtp;
  503. uws.data = *(data++);
  504. uws.next = data;
  505. if (id == 0)
  506. {
  507. uws.data <<= 8;
  508. uws.words_left = 0;
  509. uws.bytes_left = 3;
  510. }
  511. else if (id < 3)
  512. {
  513. uws.words_left = (uws.data >> 16) & 0xff;
  514. uws.data <<= 16;
  515. uws.bytes_left = 2;
  516. data += uws.words_left;
  517. }
  518. /* Restore the saved pointer. */
  519. if (state == _US_UNWIND_FRAME_RESUME)
  520. data = (_uw *) ucbp->cleanup_cache.bitpattern[0];
  521. if ((ucbp->pr_cache.additional & 1) == 0)
  522. {
  523. /* Process descriptors. */
  524. while (*data)
  525. {
  526. _uw addr;
  527. _uw fnstart;
  528. if (id == 2)
  529. {
  530. len = ((EHT32 *) data)->length;
  531. offset = ((EHT32 *) data)->offset;
  532. data += 2;
  533. }
  534. else
  535. {
  536. len = ((EHT16 *) data)->length;
  537. offset = ((EHT16 *) data)->offset;
  538. data++;
  539. }
  540. fnstart = ucbp->pr_cache.fnstart + (offset & ~1);
  541. addr = _Unwind_GetGR (context, R_PC);
  542. in_range = (fnstart <= addr && addr < fnstart + (len & ~1));
  543. switch (((offset & 1) << 1) | (len & 1))
  544. {
  545. case 0:
  546. /* Cleanup. */
  547. if (state != _US_VIRTUAL_UNWIND_FRAME
  548. && in_range)
  549. {
  550. /* Cleanup in range, and we are running cleanups. */
  551. _uw lp;
  552. /* Landing pad address is 31-bit pc-relative offset. */
  553. lp = selfrel_offset31 (data);
  554. data++;
  555. /* Save the exception data pointer. */
  556. ucbp->cleanup_cache.bitpattern[0] = (_uw) data;
  557. if (!__cxa_begin_cleanup (ucbp))
  558. return _URC_FAILURE;
  559. /* Setup the VRS to enter the landing pad. */
  560. _Unwind_SetGR (context, R_PC, lp);
  561. return _URC_INSTALL_CONTEXT;
  562. }
  563. /* Cleanup not in range, or we are in stage 1. */
  564. data++;
  565. break;
  566. case 1:
  567. /* Catch handler. */
  568. if (state == _US_VIRTUAL_UNWIND_FRAME)
  569. {
  570. if (in_range)
  571. {
  572. /* Check for a barrier. */
  573. _uw rtti;
  574. bool is_reference = (data[0] & uint32_highbit) != 0;
  575. void *matched;
  576. enum __cxa_type_match_result match_type;
  577. /* Check for no-throw areas. */
  578. if (data[1] == (_uw) -2)
  579. return _URC_FAILURE;
  580. /* The thrown object immediately follows the ECB. */
  581. matched = (void *)(ucbp + 1);
  582. if (data[1] != (_uw) -1)
  583. {
  584. /* Match a catch specification. */
  585. rtti = _Unwind_decode_typeinfo_ptr (0,
  586. (_uw) &data[1]);
  587. match_type = __cxa_type_match (ucbp,
  588. (type_info *) rtti,
  589. is_reference,
  590. &matched);
  591. }
  592. else
  593. match_type = ctm_succeeded;
  594. if (match_type)
  595. {
  596. ucbp->barrier_cache.sp =
  597. _Unwind_GetGR (context, R_SP);
  598. // ctm_succeeded_with_ptr_to_base really
  599. // means _c_t_m indirected the pointer
  600. // object. We have to reconstruct the
  601. // additional pointer layer by using a temporary.
  602. if (match_type == ctm_succeeded_with_ptr_to_base)
  603. {
  604. ucbp->barrier_cache.bitpattern[2]
  605. = (_uw) matched;
  606. ucbp->barrier_cache.bitpattern[0]
  607. = (_uw) &ucbp->barrier_cache.bitpattern[2];
  608. }
  609. else
  610. ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
  611. ucbp->barrier_cache.bitpattern[1] = (_uw) data;
  612. return _URC_HANDLER_FOUND;
  613. }
  614. }
  615. /* Handler out of range, or not matched. */
  616. }
  617. else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
  618. && ucbp->barrier_cache.bitpattern[1] == (_uw) data)
  619. {
  620. /* Matched a previous propagation barrier. */
  621. _uw lp;
  622. /* Setup for entry to the handler. */
  623. lp = selfrel_offset31 (data);
  624. _Unwind_SetGR (context, R_PC, lp);
  625. _Unwind_SetGR (context, 0, (_uw) ucbp);
  626. return _URC_INSTALL_CONTEXT;
  627. }
  628. /* Catch handler not matched. Advance to the next descriptor. */
  629. data += 2;
  630. break;
  631. case 2:
  632. rtti_count = data[0] & 0x7fffffff;
  633. /* Exception specification. */
  634. if (state == _US_VIRTUAL_UNWIND_FRAME)
  635. {
  636. if (in_range && (!forced_unwind || !rtti_count))
  637. {
  638. /* Match against the exception specification. */
  639. _uw i;
  640. _uw rtti;
  641. void *matched;
  642. for (i = 0; i < rtti_count; i++)
  643. {
  644. matched = (void *)(ucbp + 1);
  645. rtti = _Unwind_decode_typeinfo_ptr (0,
  646. (_uw) &data[i + 1]);
  647. if (__cxa_type_match (ucbp, (type_info *) rtti, 0,
  648. &matched))
  649. break;
  650. }
  651. if (i == rtti_count)
  652. {
  653. /* Exception does not match the spec. */
  654. ucbp->barrier_cache.sp =
  655. _Unwind_GetGR (context, R_SP);
  656. ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
  657. ucbp->barrier_cache.bitpattern[1] = (_uw) data;
  658. return _URC_HANDLER_FOUND;
  659. }
  660. }
  661. /* Handler out of range, or exception is permitted. */
  662. }
  663. else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
  664. && ucbp->barrier_cache.bitpattern[1] == (_uw) data)
  665. {
  666. /* Matched a previous propagation barrier. */
  667. _uw lp;
  668. /* Record the RTTI list for __cxa_call_unexpected. */
  669. ucbp->barrier_cache.bitpattern[1] = rtti_count;
  670. ucbp->barrier_cache.bitpattern[2] = 0;
  671. ucbp->barrier_cache.bitpattern[3] = 4;
  672. ucbp->barrier_cache.bitpattern[4] = (_uw) &data[1];
  673. if (data[0] & uint32_highbit)
  674. {
  675. data += rtti_count + 1;
  676. /* Setup for entry to the handler. */
  677. lp = selfrel_offset31 (data);
  678. data++;
  679. _Unwind_SetGR (context, R_PC, lp);
  680. _Unwind_SetGR (context, 0, (_uw) ucbp);
  681. return _URC_INSTALL_CONTEXT;
  682. }
  683. else
  684. phase2_call_unexpected_after_unwind = 1;
  685. }
  686. if (data[0] & uint32_highbit)
  687. data++;
  688. data += rtti_count + 1;
  689. break;
  690. default:
  691. /* Should never happen. */
  692. return _URC_FAILURE;
  693. }
  694. /* Finished processing this descriptor. */
  695. }
  696. }
  697. if (id >= 3)
  698. {
  699. /* 24-bit ecoding */
  700. if (__gnu_unwind_24bit (context, uws.data, id == 4) != _URC_OK)
  701. return _URC_FAILURE;
  702. }
  703. else
  704. {
  705. if (__gnu_unwind_execute (context, &uws) != _URC_OK)
  706. return _URC_FAILURE;
  707. }
  708. if (phase2_call_unexpected_after_unwind)
  709. {
  710. /* Enter __cxa_unexpected as if called from the call site. */
  711. _Unwind_SetGR (context, R_LR, _Unwind_GetGR (context, R_PC));
  712. _Unwind_SetGR (context, R_PC, (_uw) &__cxa_call_unexpected);
  713. return _URC_INSTALL_CONTEXT;
  714. }
  715. return _URC_CONTINUE_UNWIND;
  716. }