unwind-arm-common.inc 30 KB

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