unwind-sjlj.c 8.16 KB
Newer Older
1
/* SJLJ exception handling and frame unwind runtime interface routines.
2
   Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006
Kazu Hirata committed
3
   Free Software Foundation, Inc.
4

5
   This file is part of GCC.
6

7 8
   GCC is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
9 10 11
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

12 13 14 15 16 17 18 19 20
   In addition to the permissions in the GNU General Public License, the
   Free Software Foundation gives you unlimited permission to link the
   compiled version of this file into combinations with other programs,
   and to distribute those combinations without any restriction coming
   from the use of this file.  (The General Public License restrictions
   do apply in other respects; for example, they cover modification of
   the file, and distribution when not linked into a combined
   executable.)

21 22 23 24
   GCC is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.
25 26

   You should have received a copy of the GNU General Public License
27
   along with GCC; see the file COPYING.  If not, write to the Free
Kelley Cook committed
28 29
   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.  */
30 31 32

#include "tconfig.h"
#include "tsystem.h"
33 34
#include "coretypes.h"
#include "tm.h"
35 36 37
#include "unwind.h"
#include "gthr.h"

38
#ifdef __USING_SJLJ_EXCEPTIONS__
39 40

#ifdef DONT_USE_BUILTIN_SETJMP
41
#ifndef inhibit_libc
42 43
#include <setjmp.h>
#else
44 45 46 47
typedef void *jmp_buf[JMP_BUF_SIZE];
extern void longjmp(jmp_buf, int) __attribute__((noreturn));
#endif
#else
48 49 50
#define longjmp __builtin_longjmp
#endif

51 52 53 54 55
/* The setjmp side is dealt with in the except.c file.  */
#undef setjmp
#define setjmp setjmp_should_not_be_used_in_this_file


56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/* This structure is allocated on the stack of the target function.
   This must match the definition created in except.c:init_eh.  */
struct SjLj_Function_Context
{
  /* This is the chain through all registered contexts.  It is
     filled in by _Unwind_SjLj_Register.  */
  struct SjLj_Function_Context *prev;

  /* This is assigned in by the target function before every call
     to the index of the call site in the lsda.  It is assigned by
     the personality routine to the landing pad index.  */
  int call_site;

  /* This is how data is returned from the personality routine to
     the target function's handler.  */
  _Unwind_Word data[4];

  /* These are filled in once by the target function before any
     exceptions are expected to be handled.  */
  _Unwind_Personality_Fn personality;
  void *lsda;

#ifdef DONT_USE_BUILTIN_SETJMP
  /* We don't know what sort of alignment requirements the system
     jmp_buf has.  We over estimated in except.c, and now we have
Kazu Hirata committed
81
     to match that here just in case the system *didn't* have more
82 83 84 85 86 87 88 89 90 91 92 93
     restrictive requirements.  */
  jmp_buf jbuf __attribute__((aligned));
#else
  void *jbuf[];
#endif
};

struct _Unwind_Context
{
  struct SjLj_Function_Context *fc;
};

Kazu Hirata committed
94
typedef struct
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
{
  _Unwind_Personality_Fn personality;
} _Unwind_FrameState;


/* Manage the chain of registered function contexts.  */

/* Single threaded fallback chain.  */
static struct SjLj_Function_Context *fc_static;

#if __GTHREADS
static __gthread_key_t fc_key;
static int use_fc_key = -1;

static void
fc_key_init (void)
{
112
  use_fc_key = __gthread_key_create (&fc_key, 0) == 0;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
}

static void
fc_key_init_once (void)
{
  static __gthread_once_t once = __GTHREAD_ONCE_INIT;
  if (__gthread_once (&once, fc_key_init) != 0 || use_fc_key < 0)
    use_fc_key = 0;
}
#endif

void
_Unwind_SjLj_Register (struct SjLj_Function_Context *fc)
{
#if __GTHREADS
  if (use_fc_key < 0)
    fc_key_init_once ();

  if (use_fc_key)
    {
      fc->prev = __gthread_getspecific (fc_key);
      __gthread_setspecific (fc_key, fc);
    }
  else
#endif
    {
      fc->prev = fc_static;
      fc_static = fc;
    }
}

static inline struct SjLj_Function_Context *
_Unwind_SjLj_GetContext (void)
{
#if __GTHREADS
  if (use_fc_key < 0)
    fc_key_init_once ();

  if (use_fc_key)
    return __gthread_getspecific (fc_key);
#endif
  return fc_static;
}

static inline void
_Unwind_SjLj_SetContext (struct SjLj_Function_Context *fc)
{
#if __GTHREADS
  if (use_fc_key < 0)
    fc_key_init_once ();

  if (use_fc_key)
    __gthread_setspecific (fc_key, fc);
  else
#endif
    fc_static = fc;
}

void
_Unwind_SjLj_Unregister (struct SjLj_Function_Context *fc)
{
  _Unwind_SjLj_SetContext (fc->prev);
}


/* Get/set the return data value at INDEX in CONTEXT.  */

_Unwind_Word
_Unwind_GetGR (struct _Unwind_Context *context, int index)
{
  return context->fc->data[index];
}

186 187 188
/* Get the value of the CFA as saved in CONTEXT.  */

_Unwind_Word
189
_Unwind_GetCFA (struct _Unwind_Context *context __attribute__((unused)))
190 191
{
  /* ??? Ideally __builtin_setjmp places the CFA in the jmpbuf.  */
192 193 194 195 196 197 198 199 200 201 202 203

#ifndef DONT_USE_BUILTIN_SETJMP
  /* This is a crude imitation of the CFA: the saved stack pointer.
     This is roughly the CFA of the frame before CONTEXT.  When using the
     DWARF-2 unwinder _Unwind_GetCFA returns the CFA of the frame described
     by CONTEXT instead; but for DWARF-2 the cleanups associated with
     CONTEXT have already been run, and for SJLJ they have not yet been.  */
  if (context->fc != NULL)
    return (_Unwind_Word) context->fc->jbuf[2];
#endif

  /* Otherwise we're out of luck for now.  */
204
  return (_Unwind_Word) 0;
205 206
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220
void
_Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
{
  context->fc->data[index] = val;
}

/* Get the call-site index as saved in CONTEXT.  */

_Unwind_Ptr
_Unwind_GetIP (struct _Unwind_Context *context)
{
  return context->fc->call_site + 1;
}

221 222 223 224
_Unwind_Ptr
_Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
{
  *ip_before_insn = 0;
225 226 227 228
  if (context->fc != NULL)
    return context->fc->call_site + 1;
  else
    return 0;
229 230
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
/* Set the return landing pad index in CONTEXT.  */

void
_Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
{
  context->fc->call_site = val - 1;
}

void *
_Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
{
  return context->fc->lsda;
}

_Unwind_Ptr
246
_Unwind_GetRegionStart (struct _Unwind_Context *context __attribute__((unused)) )
247 248 249 250
{
  return 0;
}

251
void *
252
_Unwind_FindEnclosingFunction (void *pc __attribute__((unused)))
253 254 255 256
{
  return NULL;
}

257 258
#ifndef __ia64__
_Unwind_Ptr
259
_Unwind_GetDataRelBase (struct _Unwind_Context *context __attribute__((unused)) )
260 261 262 263 264
{
  return 0;
}

_Unwind_Ptr
265
_Unwind_GetTextRelBase (struct _Unwind_Context *context __attribute__((unused)) )
266 267 268 269
{
  return 0;
}
#endif
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

static inline _Unwind_Reason_Code
uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
{
  if (context->fc == NULL)
    {
      fs->personality = NULL;
      return _URC_END_OF_STACK;
    }
  else
    {
      fs->personality = context->fc->personality;
      return _URC_NO_REASON;
    }
}

static inline void
uw_update_context (struct _Unwind_Context *context,
		   _Unwind_FrameState *fs __attribute__((unused)) )
{
  context->fc = context->fc->prev;
}

293 294 295 296 297 298 299
static void
uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
{
  _Unwind_SjLj_Unregister (context->fc);
  uw_update_context (context, fs);
}

Kazu Hirata committed
300
static inline void
301 302 303 304 305
uw_init_context (struct _Unwind_Context *context)
{
  context->fc = _Unwind_SjLj_GetContext ();
}

306 307 308 309 310 311 312
static void __attribute__((noreturn))
uw_install_context (struct _Unwind_Context *current __attribute__((unused)),
                    struct _Unwind_Context *target)
{
  _Unwind_SjLj_SetContext (target->fc);
  longjmp (target->fc->jbuf, 1);
}
313 314 315 316 317 318 319 320 321 322 323 324 325 326

static inline _Unwind_Ptr
uw_identify_context (struct _Unwind_Context *context)
{
  return (_Unwind_Ptr) context->fc;
}


/* Play games with unwind symbols so that we can have call frame
   and sjlj symbols in the same shared library.  Not that you can
   use them simultaneously...  */
#define _Unwind_RaiseException		_Unwind_SjLj_RaiseException
#define _Unwind_ForcedUnwind		_Unwind_SjLj_ForcedUnwind
#define _Unwind_Resume			_Unwind_SjLj_Resume
327
#define _Unwind_Resume_or_Rethrow	_Unwind_SjLj_Resume_or_Rethrow
328 329 330 331

#include "unwind.inc"

#endif /* USING_SJLJ_EXCEPTIONS */