eh_personality.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. // -*- C++ -*- The GNU C++ exception personality routine.
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of GCC.
  5. //
  6. // GCC is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. //
  11. // GCC is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // Under Section 7 of GPL version 3, you are granted additional
  17. // permissions described in the GCC Runtime Library Exception, version
  18. // 3.1, as published by the Free Software Foundation.
  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. #include <bits/c++config.h>
  24. #include <cstdlib>
  25. #include <bits/exception_defines.h>
  26. #include <cxxabi.h>
  27. #include "unwind-cxx.h"
  28. using namespace __cxxabiv1;
  29. #include "unwind-pe.h"
  30. struct lsda_header_info
  31. {
  32. _Unwind_Ptr Start;
  33. _Unwind_Ptr LPStart;
  34. _Unwind_Ptr ttype_base;
  35. const unsigned char *TType;
  36. const unsigned char *action_table;
  37. unsigned char ttype_encoding;
  38. unsigned char call_site_encoding;
  39. };
  40. static const unsigned char *
  41. parse_lsda_header (_Unwind_Context *context, const unsigned char *p,
  42. lsda_header_info *info)
  43. {
  44. _uleb128_t tmp;
  45. unsigned char lpstart_encoding;
  46. info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
  47. // Find @LPStart, the base to which landing pad offsets are relative.
  48. lpstart_encoding = *p++;
  49. if (lpstart_encoding != DW_EH_PE_omit)
  50. p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
  51. else
  52. info->LPStart = info->Start;
  53. // Find @TType, the base of the handler and exception spec type data.
  54. info->ttype_encoding = *p++;
  55. if (info->ttype_encoding != DW_EH_PE_omit)
  56. {
  57. #if _GLIBCXX_OVERRIDE_TTYPE_ENCODING
  58. /* Older ARM EABI toolchains set this value incorrectly, so use a
  59. hardcoded OS-specific format. */
  60. info->ttype_encoding = _GLIBCXX_OVERRIDE_TTYPE_ENCODING;
  61. #endif
  62. p = read_uleb128 (p, &tmp);
  63. info->TType = p + tmp;
  64. }
  65. else
  66. info->TType = 0;
  67. // The encoding and length of the call-site table; the action table
  68. // immediately follows.
  69. info->call_site_encoding = *p++;
  70. p = read_uleb128 (p, &tmp);
  71. info->action_table = p + tmp;
  72. return p;
  73. }
  74. // Return an element from a type table.
  75. static const std::type_info *
  76. get_ttype_entry (lsda_header_info *info, _uleb128_t i)
  77. {
  78. _Unwind_Ptr ptr;
  79. i *= size_of_encoded_value (info->ttype_encoding);
  80. read_encoded_value_with_base (
  81. #if __FDPIC__
  82. /* Force these flags to nake sure to
  83. take the GOT into account. */
  84. (DW_EH_PE_pcrel | DW_EH_PE_indirect),
  85. #else
  86. info->ttype_encoding,
  87. #endif
  88. info->ttype_base,
  89. info->TType - i, &ptr);
  90. return reinterpret_cast<const std::type_info *>(ptr);
  91. }
  92. #ifdef __ARM_EABI_UNWINDER__
  93. // The ABI provides a routine for matching exception object types.
  94. typedef _Unwind_Control_Block _throw_typet;
  95. #define get_adjusted_ptr(catch_type, throw_type, thrown_ptr_p) \
  96. (__cxa_type_match (throw_type, catch_type, false, thrown_ptr_p) \
  97. != ctm_failed)
  98. // Return true if THROW_TYPE matches one if the filter types.
  99. static bool
  100. check_exception_spec(lsda_header_info* info, _throw_typet* throw_type,
  101. void* thrown_ptr, _sleb128_t filter_value)
  102. {
  103. const _uleb128_t* e = ((const _uleb128_t*) info->TType)
  104. - filter_value - 1;
  105. while (1)
  106. {
  107. const std::type_info* catch_type;
  108. _uleb128_t tmp;
  109. tmp = *e;
  110. // Zero signals the end of the list. If we've not found
  111. // a match by now, then we've failed the specification.
  112. if (tmp == 0)
  113. return false;
  114. tmp = _Unwind_decode_typeinfo_ptr(info->ttype_base, (_Unwind_Word) e);
  115. // Match a ttype entry.
  116. catch_type = reinterpret_cast<const std::type_info*>(tmp);
  117. // ??? There is currently no way to ask the RTTI code about the
  118. // relationship between two types without reference to a specific
  119. // object. There should be; then we wouldn't need to mess with
  120. // thrown_ptr here.
  121. if (get_adjusted_ptr(catch_type, throw_type, &thrown_ptr))
  122. return true;
  123. // Advance to the next entry.
  124. e++;
  125. }
  126. }
  127. // Save stage1 handler information in the exception object
  128. static inline void
  129. save_caught_exception(struct _Unwind_Exception* ue_header,
  130. struct _Unwind_Context* context,
  131. void* thrown_ptr,
  132. int handler_switch_value,
  133. const unsigned char* language_specific_data,
  134. _Unwind_Ptr landing_pad,
  135. const unsigned char* action_record
  136. __attribute__((__unused__)))
  137. {
  138. ue_header->barrier_cache.sp = _Unwind_GetGR(context, UNWIND_STACK_REG);
  139. ue_header->barrier_cache.bitpattern[0] = (_uw) thrown_ptr;
  140. ue_header->barrier_cache.bitpattern[1]
  141. = (_uw) handler_switch_value;
  142. ue_header->barrier_cache.bitpattern[2]
  143. = (_uw) language_specific_data;
  144. ue_header->barrier_cache.bitpattern[3] = (_uw) landing_pad;
  145. }
  146. // Restore the catch handler data saved during phase1.
  147. static inline void
  148. restore_caught_exception(struct _Unwind_Exception* ue_header,
  149. int& handler_switch_value,
  150. const unsigned char*& language_specific_data,
  151. _Unwind_Ptr& landing_pad)
  152. {
  153. handler_switch_value = (int) ue_header->barrier_cache.bitpattern[1];
  154. language_specific_data =
  155. (const unsigned char*) ue_header->barrier_cache.bitpattern[2];
  156. landing_pad = (_Unwind_Ptr) ue_header->barrier_cache.bitpattern[3];
  157. }
  158. #define CONTINUE_UNWINDING \
  159. do \
  160. { \
  161. if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
  162. return _URC_FAILURE; \
  163. return _URC_CONTINUE_UNWIND; \
  164. } \
  165. while (0)
  166. // Return true if the filter spec is empty, ie throw().
  167. static bool
  168. empty_exception_spec (lsda_header_info *info, _Unwind_Sword filter_value)
  169. {
  170. const _Unwind_Word* e = ((const _Unwind_Word*) info->TType)
  171. - filter_value - 1;
  172. return *e == 0;
  173. }
  174. #else
  175. typedef const std::type_info _throw_typet;
  176. // Given the thrown type THROW_TYPE, pointer to a variable containing a
  177. // pointer to the exception object THROWN_PTR_P and a type CATCH_TYPE to
  178. // compare against, return whether or not there is a match and if so,
  179. // update *THROWN_PTR_P.
  180. static bool
  181. get_adjusted_ptr (const std::type_info *catch_type,
  182. const std::type_info *throw_type,
  183. void **thrown_ptr_p)
  184. {
  185. void *thrown_ptr = *thrown_ptr_p;
  186. // Pointer types need to adjust the actual pointer, not
  187. // the pointer to pointer that is the exception object.
  188. // This also has the effect of passing pointer types
  189. // "by value" through the __cxa_begin_catch return value.
  190. if (throw_type->__is_pointer_p ())
  191. thrown_ptr = *(void **) thrown_ptr;
  192. if (catch_type->__do_catch (throw_type, &thrown_ptr, 1))
  193. {
  194. *thrown_ptr_p = thrown_ptr;
  195. return true;
  196. }
  197. return false;
  198. }
  199. // Return true if THROW_TYPE matches one if the filter types.
  200. static bool
  201. check_exception_spec(lsda_header_info* info, _throw_typet* throw_type,
  202. void* thrown_ptr, _sleb128_t filter_value)
  203. {
  204. const unsigned char *e = info->TType - filter_value - 1;
  205. while (1)
  206. {
  207. const std::type_info *catch_type;
  208. _uleb128_t tmp;
  209. e = read_uleb128 (e, &tmp);
  210. // Zero signals the end of the list. If we've not found
  211. // a match by now, then we've failed the specification.
  212. if (tmp == 0)
  213. return false;
  214. // Match a ttype entry.
  215. catch_type = get_ttype_entry (info, tmp);
  216. // ??? There is currently no way to ask the RTTI code about the
  217. // relationship between two types without reference to a specific
  218. // object. There should be; then we wouldn't need to mess with
  219. // thrown_ptr here.
  220. if (get_adjusted_ptr (catch_type, throw_type, &thrown_ptr))
  221. return true;
  222. }
  223. }
  224. // Save stage1 handler information in the exception object
  225. static inline void
  226. save_caught_exception(struct _Unwind_Exception* ue_header,
  227. struct _Unwind_Context* context
  228. __attribute__((__unused__)),
  229. void* thrown_ptr,
  230. int handler_switch_value,
  231. const unsigned char* language_specific_data,
  232. _Unwind_Ptr landing_pad __attribute__((__unused__)),
  233. const unsigned char* action_record)
  234. {
  235. __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
  236. xh->handlerSwitchValue = handler_switch_value;
  237. xh->actionRecord = action_record;
  238. xh->languageSpecificData = language_specific_data;
  239. xh->adjustedPtr = thrown_ptr;
  240. // ??? Completely unknown what this field is supposed to be for.
  241. // ??? Need to cache TType encoding base for call_unexpected.
  242. xh->catchTemp = landing_pad;
  243. }
  244. // Restore the catch handler information saved during phase1.
  245. static inline void
  246. restore_caught_exception(struct _Unwind_Exception* ue_header,
  247. int& handler_switch_value,
  248. const unsigned char*& language_specific_data,
  249. _Unwind_Ptr& landing_pad)
  250. {
  251. __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
  252. handler_switch_value = xh->handlerSwitchValue;
  253. language_specific_data = xh->languageSpecificData;
  254. landing_pad = (_Unwind_Ptr) xh->catchTemp;
  255. }
  256. #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
  257. // Return true if the filter spec is empty, ie throw().
  258. static bool
  259. empty_exception_spec (lsda_header_info *info, _Unwind_Sword filter_value)
  260. {
  261. const unsigned char *e = info->TType - filter_value - 1;
  262. _uleb128_t tmp;
  263. e = read_uleb128 (e, &tmp);
  264. return tmp == 0;
  265. }
  266. #endif // !__ARM_EABI_UNWINDER__
  267. namespace __cxxabiv1
  268. {
  269. // Using a different personality function name causes link failures
  270. // when trying to mix code using different exception handling models.
  271. #ifdef __USING_SJLJ_EXCEPTIONS__
  272. #define PERSONALITY_FUNCTION __gxx_personality_sj0
  273. #define __builtin_eh_return_data_regno(x) x
  274. #elif defined(__SEH__)
  275. #define PERSONALITY_FUNCTION __gxx_personality_imp
  276. #else
  277. #define PERSONALITY_FUNCTION __gxx_personality_v0
  278. #endif
  279. #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
  280. static
  281. #else
  282. extern "C"
  283. #endif
  284. _Unwind_Reason_Code
  285. #ifdef __ARM_EABI_UNWINDER__
  286. __attribute__((target ("general-regs-only")))
  287. PERSONALITY_FUNCTION (_Unwind_State state,
  288. struct _Unwind_Exception* ue_header,
  289. struct _Unwind_Context* context)
  290. #else
  291. PERSONALITY_FUNCTION (int version,
  292. _Unwind_Action actions,
  293. _Unwind_Exception_Class exception_class,
  294. struct _Unwind_Exception *ue_header,
  295. struct _Unwind_Context *context)
  296. #endif
  297. {
  298. enum found_handler_type
  299. {
  300. found_nothing,
  301. found_terminate,
  302. found_cleanup,
  303. found_handler
  304. } found_type;
  305. lsda_header_info info;
  306. const unsigned char *language_specific_data;
  307. const unsigned char *action_record;
  308. const unsigned char *p;
  309. _Unwind_Ptr landing_pad, ip;
  310. int handler_switch_value;
  311. void* thrown_ptr = 0;
  312. bool foreign_exception;
  313. int ip_before_insn = 0;
  314. #ifdef __ARM_EABI_UNWINDER__
  315. _Unwind_Action actions;
  316. switch (state & _US_ACTION_MASK)
  317. {
  318. case _US_VIRTUAL_UNWIND_FRAME:
  319. // If the unwind state pattern is
  320. // _US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND
  321. // then we don't need to search for any handler as it is not a real
  322. // exception. Just unwind the stack.
  323. if (state & _US_FORCE_UNWIND)
  324. CONTINUE_UNWINDING;
  325. actions = _UA_SEARCH_PHASE;
  326. break;
  327. case _US_UNWIND_FRAME_STARTING:
  328. actions = _UA_CLEANUP_PHASE;
  329. if (!(state & _US_FORCE_UNWIND)
  330. && ue_header->barrier_cache.sp == _Unwind_GetGR(context,
  331. UNWIND_STACK_REG))
  332. actions |= _UA_HANDLER_FRAME;
  333. break;
  334. case _US_UNWIND_FRAME_RESUME:
  335. CONTINUE_UNWINDING;
  336. break;
  337. default:
  338. std::abort();
  339. }
  340. actions |= state & _US_FORCE_UNWIND;
  341. // We don't know which runtime we're working with, so can't check this.
  342. // However the ABI routines hide this from us, and we don't actually need
  343. // to know.
  344. foreign_exception = false;
  345. // The dwarf unwinder assumes the context structure holds things like the
  346. // function and LSDA pointers. The ARM implementation caches these in
  347. // the exception header (UCB). To avoid rewriting everything we make a
  348. // virtual scratch register point at the UCB.
  349. ip = (_Unwind_Ptr) ue_header;
  350. _Unwind_SetGR(context, UNWIND_POINTER_REG, ip);
  351. #else
  352. __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
  353. // Interface version check.
  354. if (version != 1)
  355. return _URC_FATAL_PHASE1_ERROR;
  356. foreign_exception = !__is_gxx_exception_class(exception_class);
  357. #endif
  358. // Shortcut for phase 2 found handler for domestic exception.
  359. if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
  360. && !foreign_exception)
  361. {
  362. restore_caught_exception(ue_header, handler_switch_value,
  363. language_specific_data, landing_pad);
  364. found_type = (landing_pad == 0 ? found_terminate : found_handler);
  365. goto install_context;
  366. }
  367. language_specific_data = (const unsigned char *)
  368. _Unwind_GetLanguageSpecificData (context);
  369. // If no LSDA, then there are no handlers or cleanups.
  370. if (! language_specific_data)
  371. CONTINUE_UNWINDING;
  372. // Parse the LSDA header.
  373. p = parse_lsda_header (context, language_specific_data, &info);
  374. info.ttype_base = base_of_encoded_value (info.ttype_encoding, context);
  375. #ifdef _GLIBCXX_HAVE_GETIPINFO
  376. ip = _Unwind_GetIPInfo (context, &ip_before_insn);
  377. #else
  378. ip = _Unwind_GetIP (context);
  379. #endif
  380. if (! ip_before_insn)
  381. --ip;
  382. landing_pad = 0;
  383. action_record = 0;
  384. handler_switch_value = 0;
  385. #ifdef __USING_SJLJ_EXCEPTIONS__
  386. // The given "IP" is an index into the call-site table, with two
  387. // exceptions -- -1 means no-action, and 0 means terminate. But
  388. // since we're using uleb128 values, we've not got random access
  389. // to the array.
  390. if ((int) ip < 0)
  391. return _URC_CONTINUE_UNWIND;
  392. else if (ip == 0)
  393. {
  394. // Fall through to set found_terminate.
  395. }
  396. else
  397. {
  398. _uleb128_t cs_lp, cs_action;
  399. do
  400. {
  401. p = read_uleb128 (p, &cs_lp);
  402. p = read_uleb128 (p, &cs_action);
  403. }
  404. while (--ip);
  405. // Can never have null landing pad for sjlj -- that would have
  406. // been indicated by a -1 call site index.
  407. landing_pad = cs_lp + 1;
  408. if (cs_action)
  409. action_record = info.action_table + cs_action - 1;
  410. goto found_something;
  411. }
  412. #else
  413. // Search the call-site table for the action associated with this IP.
  414. while (p < info.action_table)
  415. {
  416. _Unwind_Ptr cs_start, cs_len, cs_lp;
  417. _uleb128_t cs_action;
  418. // Note that all call-site encodings are "absolute" displacements.
  419. p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
  420. p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
  421. p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
  422. p = read_uleb128 (p, &cs_action);
  423. // The table is sorted, so if we've passed the ip, stop.
  424. if (ip < info.Start + cs_start)
  425. p = info.action_table;
  426. else if (ip < info.Start + cs_start + cs_len)
  427. {
  428. if (cs_lp)
  429. landing_pad = info.LPStart + cs_lp;
  430. if (cs_action)
  431. action_record = info.action_table + cs_action - 1;
  432. goto found_something;
  433. }
  434. }
  435. #endif // __USING_SJLJ_EXCEPTIONS__
  436. // If ip is not present in the table, call terminate. This is for
  437. // a destructor inside a cleanup, or a library routine the compiler
  438. // was not expecting to throw.
  439. found_type = found_terminate;
  440. goto do_something;
  441. found_something:
  442. if (landing_pad == 0)
  443. {
  444. // If ip is present, and has a null landing pad, there are
  445. // no cleanups or handlers to be run.
  446. found_type = found_nothing;
  447. }
  448. else if (action_record == 0)
  449. {
  450. // If ip is present, has a non-null landing pad, and a null
  451. // action table offset, then there are only cleanups present.
  452. // Cleanups use a zero switch value, as set above.
  453. found_type = found_cleanup;
  454. }
  455. else
  456. {
  457. // Otherwise we have a catch handler or exception specification.
  458. _sleb128_t ar_filter, ar_disp;
  459. const std::type_info* catch_type;
  460. _throw_typet* throw_type;
  461. bool saw_cleanup = false;
  462. bool saw_handler = false;
  463. #ifdef __ARM_EABI_UNWINDER__
  464. // ??? How does this work - more importantly, how does it interact with
  465. // dependent exceptions?
  466. throw_type = ue_header;
  467. if (actions & _UA_FORCE_UNWIND)
  468. {
  469. __GXX_INIT_FORCED_UNWIND_CLASS(ue_header->exception_class);
  470. }
  471. else if (!foreign_exception)
  472. thrown_ptr = __get_object_from_ue (ue_header);
  473. #else
  474. #if __cpp_rtti
  475. // During forced unwinding, match a magic exception type.
  476. if (actions & _UA_FORCE_UNWIND)
  477. {
  478. throw_type = &typeid(abi::__forced_unwind);
  479. }
  480. // With a foreign exception class, there's no exception type.
  481. // ??? What to do about GNU Java and GNU Ada exceptions?
  482. else if (foreign_exception)
  483. {
  484. throw_type = &typeid(abi::__foreign_exception);
  485. }
  486. else
  487. #endif
  488. {
  489. thrown_ptr = __get_object_from_ue (ue_header);
  490. throw_type = __get_exception_header_from_obj
  491. (thrown_ptr)->exceptionType;
  492. }
  493. #endif
  494. while (1)
  495. {
  496. p = action_record;
  497. p = read_sleb128 (p, &ar_filter);
  498. read_sleb128 (p, &ar_disp);
  499. if (ar_filter == 0)
  500. {
  501. // Zero filter values are cleanups.
  502. saw_cleanup = true;
  503. }
  504. else if (ar_filter > 0)
  505. {
  506. // Positive filter values are handlers.
  507. catch_type = get_ttype_entry (&info, ar_filter);
  508. // Null catch type is a catch-all handler; we can catch foreign
  509. // exceptions with this. Otherwise we must match types.
  510. if (! catch_type
  511. || (throw_type
  512. && get_adjusted_ptr (catch_type, throw_type,
  513. &thrown_ptr)))
  514. {
  515. saw_handler = true;
  516. break;
  517. }
  518. }
  519. else
  520. {
  521. // Negative filter values are exception specifications.
  522. // ??? How do foreign exceptions fit in? As far as I can
  523. // see we can't match because there's no __cxa_exception
  524. // object to stuff bits in for __cxa_call_unexpected to use.
  525. // Allow them iff the exception spec is non-empty. I.e.
  526. // a throw() specification results in __unexpected.
  527. if ((throw_type
  528. && !(actions & _UA_FORCE_UNWIND)
  529. && !foreign_exception)
  530. ? ! check_exception_spec (&info, throw_type, thrown_ptr,
  531. ar_filter)
  532. : empty_exception_spec (&info, ar_filter))
  533. {
  534. saw_handler = true;
  535. break;
  536. }
  537. }
  538. if (ar_disp == 0)
  539. break;
  540. action_record = p + ar_disp;
  541. }
  542. if (saw_handler)
  543. {
  544. handler_switch_value = ar_filter;
  545. found_type = found_handler;
  546. }
  547. else
  548. found_type = (saw_cleanup ? found_cleanup : found_nothing);
  549. }
  550. do_something:
  551. if (found_type == found_nothing)
  552. CONTINUE_UNWINDING;
  553. if (actions & _UA_SEARCH_PHASE)
  554. {
  555. if (found_type == found_cleanup)
  556. CONTINUE_UNWINDING;
  557. // For domestic exceptions, we cache data from phase 1 for phase 2.
  558. if (!foreign_exception)
  559. {
  560. save_caught_exception(ue_header, context, thrown_ptr,
  561. handler_switch_value, language_specific_data,
  562. landing_pad, action_record);
  563. }
  564. return _URC_HANDLER_FOUND;
  565. }
  566. install_context:
  567. // We can't use any of the cxa routines with foreign exceptions,
  568. // because they all expect ue_header to be a struct __cxa_exception.
  569. // So in that case, call terminate or unexpected directly.
  570. if ((actions & _UA_FORCE_UNWIND)
  571. || foreign_exception)
  572. {
  573. if (found_type == found_terminate)
  574. std::terminate ();
  575. else if (handler_switch_value < 0)
  576. {
  577. #pragma GCC diagnostic push
  578. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  579. __try
  580. { std::unexpected (); }
  581. __catch(...)
  582. { std::terminate (); }
  583. #pragma GCC diagnostic pop
  584. }
  585. }
  586. else
  587. {
  588. if (found_type == found_terminate)
  589. __cxa_call_terminate(ue_header);
  590. // Cache the TType base value for __cxa_call_unexpected, as we won't
  591. // have an _Unwind_Context then.
  592. if (handler_switch_value < 0)
  593. {
  594. parse_lsda_header (context, language_specific_data, &info);
  595. info.ttype_base = base_of_encoded_value (info.ttype_encoding,
  596. context);
  597. #ifdef __ARM_EABI_UNWINDER__
  598. const _Unwind_Word* e;
  599. _Unwind_Word n;
  600. e = ((const _Unwind_Word*) info.TType) - handler_switch_value - 1;
  601. // Count the number of rtti objects.
  602. n = 0;
  603. while (e[n] != 0)
  604. n++;
  605. // Count.
  606. ue_header->barrier_cache.bitpattern[1] = n;
  607. // Base
  608. ue_header->barrier_cache.bitpattern[2] = info.ttype_base;
  609. // Stride.
  610. ue_header->barrier_cache.bitpattern[3] = 4;
  611. // List head.
  612. ue_header->barrier_cache.bitpattern[4] = (_Unwind_Word) e;
  613. #else
  614. xh->catchTemp = base_of_encoded_value (info.ttype_encoding, context);
  615. #endif
  616. }
  617. }
  618. /* For targets with pointers smaller than the word size, we must extend the
  619. pointer, and this extension is target dependent. */
  620. _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
  621. __builtin_extend_pointer (ue_header));
  622. _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
  623. handler_switch_value);
  624. _Unwind_SetIP (context, landing_pad);
  625. #ifdef __ARM_EABI_UNWINDER__
  626. if (found_type == found_cleanup)
  627. __cxa_begin_cleanup(ue_header);
  628. #endif
  629. return _URC_INSTALL_CONTEXT;
  630. }
  631. /* The ARM EABI implementation of __cxa_call_unexpected is in a
  632. different file so that the personality routine (PR) can be used
  633. standalone. The generic routine shared datastructures with the PR
  634. so it is most convenient to implement it here. */
  635. #ifndef __ARM_EABI_UNWINDER__
  636. extern "C" void
  637. __cxa_call_unexpected (void *exc_obj_in)
  638. {
  639. _Unwind_Exception *exc_obj
  640. = reinterpret_cast <_Unwind_Exception *>(exc_obj_in);
  641. __cxa_begin_catch (exc_obj);
  642. // This function is a handler for our exception argument. If we exit
  643. // by throwing a different exception, we'll need the original cleaned up.
  644. struct end_catch_protect
  645. {
  646. end_catch_protect() { }
  647. ~end_catch_protect() { __cxa_end_catch(); }
  648. } end_catch_protect_obj;
  649. lsda_header_info info;
  650. __cxa_exception *xh = __get_exception_header_from_ue (exc_obj);
  651. const unsigned char *xh_lsda;
  652. _Unwind_Sword xh_switch_value;
  653. std::terminate_handler xh_terminate_handler;
  654. // If the unexpectedHandler rethrows the exception (e.g. to categorize it),
  655. // it will clobber data about the current handler. So copy the data out now.
  656. xh_lsda = xh->languageSpecificData;
  657. xh_switch_value = xh->handlerSwitchValue;
  658. xh_terminate_handler = xh->terminateHandler;
  659. info.ttype_base = (_Unwind_Ptr) xh->catchTemp;
  660. __try
  661. { __unexpected (xh->unexpectedHandler); }
  662. __catch(...)
  663. {
  664. // Get the exception thrown from unexpected.
  665. __cxa_eh_globals *globals = __cxa_get_globals_fast ();
  666. __cxa_exception *new_xh = globals->caughtExceptions;
  667. void *new_ptr = __get_object_from_ambiguous_exception (new_xh);
  668. // We don't quite have enough stuff cached; re-parse the LSDA.
  669. parse_lsda_header (0, xh_lsda, &info);
  670. // If this new exception meets the exception spec, allow it.
  671. if (check_exception_spec (&info, __get_exception_header_from_obj
  672. (new_ptr)->exceptionType,
  673. new_ptr, xh_switch_value))
  674. { __throw_exception_again; }
  675. // If the exception spec allows std::bad_exception, throw that.
  676. // We don't have a thrown object to compare against, but since
  677. // bad_exception doesn't have virtual bases, that's OK; just pass 0.
  678. #if __cpp_exceptions && __cpp_rtti
  679. const std::type_info &bad_exc = typeid (std::bad_exception);
  680. if (check_exception_spec (&info, &bad_exc, 0, xh_switch_value))
  681. throw std::bad_exception();
  682. #endif
  683. // Otherwise, die.
  684. __terminate (xh_terminate_handler);
  685. }
  686. }
  687. #endif
  688. #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
  689. extern "C"
  690. EXCEPTION_DISPOSITION
  691. __gxx_personality_seh0 (PEXCEPTION_RECORD ms_exc, void *this_frame,
  692. PCONTEXT ms_orig_context, PDISPATCHER_CONTEXT ms_disp)
  693. {
  694. return _GCC_specific_handler (ms_exc, this_frame, ms_orig_context,
  695. ms_disp, __gxx_personality_imp);
  696. }
  697. #endif /* SEH */
  698. } // namespace __cxxabiv1