Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /* SJLJ exception handling and frame unwind runtime interface routines.
  2.    Copyright (C) 1997-2015 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 it
  7.    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, but WITHOUT
  12.    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13.    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  14.    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.  
  20.    You should have received a copy of the GNU General Public License and
  21.    a copy of the GCC Runtime Library Exception along with this program;
  22.    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
  23.    <http://www.gnu.org/licenses/>.  */
  24.  
  25. #include "tconfig.h"
  26. #include "tsystem.h"
  27. #include "coretypes.h"
  28. #include "tm.h"
  29. #include "libgcc_tm.h"
  30. #include "unwind.h"
  31. #include "gthr.h"
  32.  
  33. #ifdef __USING_SJLJ_EXCEPTIONS__
  34.  
  35. #ifdef __LIBGCC_DONT_USE_BUILTIN_SETJMP__
  36. #ifndef inhibit_libc
  37. #include <setjmp.h>
  38. #else
  39. typedef void *jmp_buf[__LIBGCC_JMP_BUF_SIZE__];
  40. extern void longjmp(jmp_buf, int) __attribute__((noreturn));
  41. #endif
  42. #else
  43. #define longjmp __builtin_longjmp
  44. #endif
  45.  
  46. /* The setjmp side is dealt with in the except.c file.  */
  47. #undef setjmp
  48. #define setjmp setjmp_should_not_be_used_in_this_file
  49.  
  50.  
  51. /* This structure is allocated on the stack of the target function.
  52.    This must match the definition created in except.c:init_eh.  */
  53. struct SjLj_Function_Context
  54. {
  55.   /* This is the chain through all registered contexts.  It is
  56.      filled in by _Unwind_SjLj_Register.  */
  57.   struct SjLj_Function_Context *prev;
  58.  
  59.   /* This is assigned in by the target function before every call
  60.      to the index of the call site in the lsda.  It is assigned by
  61.      the personality routine to the landing pad index.  */
  62.   int call_site;
  63.  
  64.   /* This is how data is returned from the personality routine to
  65.      the target function's handler.  */
  66.   _Unwind_Word data[4];
  67.  
  68.   /* These are filled in once by the target function before any
  69.      exceptions are expected to be handled.  */
  70.   _Unwind_Personality_Fn personality;
  71.   void *lsda;
  72.  
  73. #ifdef __LIBGCC_DONT_USE_BUILTIN_SETJMP__
  74.   /* We don't know what sort of alignment requirements the system
  75.      jmp_buf has.  We over estimated in except.c, and now we have
  76.      to match that here just in case the system *didn't* have more
  77.      restrictive requirements.  */
  78.   jmp_buf jbuf __attribute__((aligned));
  79. #else
  80.   void *jbuf[];
  81. #endif
  82. };
  83.  
  84. struct _Unwind_Context
  85. {
  86.   struct SjLj_Function_Context *fc;
  87. };
  88.  
  89. typedef struct
  90. {
  91.   _Unwind_Personality_Fn personality;
  92. } _Unwind_FrameState;
  93.  
  94. /* Manage the chain of registered function contexts.  */
  95.  
  96. /* Single threaded fallback chain.  */
  97. static struct SjLj_Function_Context *fc_static;
  98.  
  99. #if __GTHREADS
  100. static __gthread_key_t fc_key;
  101. static int use_fc_key = -1;
  102.  
  103. static void
  104. fc_key_init (void)
  105. {
  106.   use_fc_key = __gthread_key_create (&fc_key, 0) == 0;
  107. }
  108.  
  109. static void
  110. fc_key_init_once (void)
  111. {
  112.   static __gthread_once_t once = __GTHREAD_ONCE_INIT;
  113.   if (__gthread_once (&once, fc_key_init) != 0 || use_fc_key < 0)
  114.     use_fc_key = 0;
  115. }
  116. #endif
  117.  
  118. void
  119. _Unwind_SjLj_Register (struct SjLj_Function_Context *fc)
  120. {
  121. #if __GTHREADS
  122.   if (use_fc_key < 0)
  123.     fc_key_init_once ();
  124.  
  125.   if (use_fc_key)
  126.     {
  127.       fc->prev = __gthread_getspecific (fc_key);
  128.       __gthread_setspecific (fc_key, fc);
  129.     }
  130.   else
  131. #endif
  132.     {
  133.       fc->prev = fc_static;
  134.       fc_static = fc;
  135.     }
  136. }
  137.  
  138. static inline struct SjLj_Function_Context *
  139. _Unwind_SjLj_GetContext (void)
  140. {
  141. #if __GTHREADS
  142.   if (use_fc_key < 0)
  143.     fc_key_init_once ();
  144.  
  145.   if (use_fc_key)
  146.     return __gthread_getspecific (fc_key);
  147. #endif
  148.   return fc_static;
  149. }
  150.  
  151. static inline void
  152. _Unwind_SjLj_SetContext (struct SjLj_Function_Context *fc)
  153. {
  154. #if __GTHREADS
  155.   if (use_fc_key < 0)
  156.     fc_key_init_once ();
  157.  
  158.   if (use_fc_key)
  159.     __gthread_setspecific (fc_key, fc);
  160.   else
  161. #endif
  162.     fc_static = fc;
  163. }
  164.  
  165. void
  166. _Unwind_SjLj_Unregister (struct SjLj_Function_Context *fc)
  167. {
  168.   _Unwind_SjLj_SetContext (fc->prev);
  169. }
  170.  
  171. /* Get/set the return data value at INDEX in CONTEXT.  */
  172.  
  173. _Unwind_Word
  174. _Unwind_GetGR (struct _Unwind_Context *context, int index)
  175. {
  176.   return context->fc->data[index];
  177. }
  178.  
  179. /* Get the value of the CFA as saved in CONTEXT.  */
  180.  
  181. _Unwind_Word
  182. _Unwind_GetCFA (struct _Unwind_Context *context __attribute__((unused)))
  183. {
  184.   /* ??? Ideally __builtin_setjmp places the CFA in the jmpbuf.  */
  185.  
  186. #ifndef __LIBGCC_DONT_USE_BUILTIN_SETJMP__
  187.   /* This is a crude imitation of the CFA: the saved stack pointer.
  188.      This is roughly the CFA of the frame before CONTEXT.  When using the
  189.      DWARF-2 unwinder _Unwind_GetCFA returns the CFA of the frame described
  190.      by CONTEXT instead; but for DWARF-2 the cleanups associated with
  191.      CONTEXT have already been run, and for SJLJ they have not yet been.  */
  192.   if (context->fc != NULL)
  193.     return (_Unwind_Word) context->fc->jbuf[2];
  194. #endif
  195.  
  196.   /* Otherwise we're out of luck for now.  */
  197.   return (_Unwind_Word) 0;
  198. }
  199.  
  200. void
  201. _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
  202. {
  203.   context->fc->data[index] = val;
  204. }
  205.  
  206. /* Get the call-site index as saved in CONTEXT.  */
  207.  
  208. _Unwind_Ptr
  209. _Unwind_GetIP (struct _Unwind_Context *context)
  210. {
  211.   return context->fc->call_site + 1;
  212. }
  213.  
  214. _Unwind_Ptr
  215. _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
  216. {
  217.   *ip_before_insn = 0;
  218.   if (context->fc != NULL)
  219.     return context->fc->call_site + 1;
  220.   else
  221.     return 0;
  222. }
  223.  
  224. /* Set the return landing pad index in CONTEXT.  */
  225.  
  226. void
  227. _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
  228. {
  229.   context->fc->call_site = val - 1;
  230. }
  231.  
  232. void *
  233. _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
  234. {
  235.   return context->fc->lsda;
  236. }
  237.  
  238. _Unwind_Ptr
  239. _Unwind_GetRegionStart (struct _Unwind_Context *context __attribute__((unused)) )
  240. {
  241.   return 0;
  242. }
  243.  
  244. void *
  245. _Unwind_FindEnclosingFunction (void *pc __attribute__((unused)))
  246. {
  247.   return NULL;
  248. }
  249.  
  250. #ifndef __ia64__
  251. _Unwind_Ptr
  252. _Unwind_GetDataRelBase (struct _Unwind_Context *context __attribute__((unused)) )
  253. {
  254.   return 0;
  255. }
  256.  
  257. _Unwind_Ptr
  258. _Unwind_GetTextRelBase (struct _Unwind_Context *context __attribute__((unused)) )
  259. {
  260.   return 0;
  261. }
  262. #endif
  263. static inline _Unwind_Reason_Code
  264. uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
  265. {
  266.   if (context->fc == NULL)
  267.     {
  268.       fs->personality = NULL;
  269.       return _URC_END_OF_STACK;
  270.     }
  271.   else
  272.     {
  273.       fs->personality = context->fc->personality;
  274.       return _URC_NO_REASON;
  275.     }
  276. }
  277.  
  278. static inline void
  279. uw_update_context (struct _Unwind_Context *context,
  280.                    _Unwind_FrameState *fs __attribute__((unused)) )
  281. {
  282.   context->fc = context->fc->prev;
  283. }
  284.  
  285. static void
  286. uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
  287. {
  288.   _Unwind_SjLj_Unregister (context->fc);
  289.   uw_update_context (context, fs);
  290. }
  291.  
  292. static inline void
  293. uw_init_context (struct _Unwind_Context *context)
  294. {
  295.   context->fc = _Unwind_SjLj_GetContext ();
  296. }
  297.  
  298. static void __attribute__((noreturn))
  299. uw_install_context (struct _Unwind_Context *current __attribute__((unused)),
  300.                     struct _Unwind_Context *target)
  301. {
  302.   _Unwind_SjLj_SetContext (target->fc);
  303.   longjmp (target->fc->jbuf, 1);
  304. }
  305.  
  306. static inline _Unwind_Ptr
  307. uw_identify_context (struct _Unwind_Context *context)
  308. {
  309.   return (_Unwind_Ptr) context->fc;
  310. }
  311.  
  312.  
  313. /* Play games with unwind symbols so that we can have call frame
  314.    and sjlj symbols in the same shared library.  Not that you can
  315.    use them simultaneously...  */
  316. #define _Unwind_RaiseException          _Unwind_SjLj_RaiseException
  317. #define _Unwind_ForcedUnwind            _Unwind_SjLj_ForcedUnwind
  318. #define _Unwind_Resume                  _Unwind_SjLj_Resume
  319. #define _Unwind_Resume_or_Rethrow       _Unwind_SjLj_Resume_or_Rethrow
  320.  
  321. #include "unwind.inc"
  322.  
  323. #endif /* USING_SJLJ_EXCEPTIONS */
  324.