1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2003-2005 Hewlett-Packard Co
3        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
25
26#define UNW_LOCAL_ONLY
27
28#include <setjmp.h>
29
30#include "libunwind_i.h"
31#include "jmpbuf.h"
32#include "setjmp_i.h"
33
34#if !defined(_NSIG)
35# if defined(_SIG_MAXSIG)
36#  define _NSIG (_SIG_MAXSIG - 1)
37# elif defined(NSIG)
38#  define _NSIG NSIG
39# endif
40#endif
41
42#if defined(__GLIBC__)
43#if __GLIBC_PREREQ(2, 4)
44
45/* Starting with glibc-2.4, {sig,}setjmp in GLIBC obfuscates the
46   register values in jmp_buf by XORing them with a "random"
47   canary value.
48
49   This makes it impossible to implement longjmp, as we
50   can never match wp[JB_SP], unless we decode the canary first.
51
52   Doing so is possible, but doesn't appear to be worth the trouble,
53   so we simply defer to glibc siglongjmp here.  */
54
55#define siglongjmp __nonworking_siglongjmp
56static void siglongjmp (sigjmp_buf env, int val) UNUSED;
57#endif
58#endif /* __GLIBC_PREREQ */
59
60void
61siglongjmp (sigjmp_buf env, int val)
62{
63  unw_word_t *wp = (unw_word_t *) env;
64  extern int _UI_siglongjmp_cont;
65  extern int _UI_longjmp_cont;
66  unw_context_t uc;
67  unw_cursor_t c;
68  unw_word_t sp;
69  int *cont;
70
71  if (unw_getcontext (&uc) < 0 || unw_init_local (&c, &uc) < 0)
72    abort ();
73
74  do
75    {
76      if (unw_get_reg (&c, UNW_REG_SP, &sp) < 0)
77        abort ();
78#ifdef __FreeBSD__
79      if (sp != wp[JB_SP] + sizeof(unw_word_t))
80#else
81      if (sp != wp[JB_SP])
82#endif
83        continue;
84
85      if (!bsp_match (&c, wp))
86        continue;
87
88      /* found the right frame: */
89
90      /* default to resuming without restoring signal-mask */
91      cont = &_UI_longjmp_cont;
92
93      /* Order of evaluation is important here: if unw_resume()
94         restores signal mask, we must set it up appropriately, even
95         if wp[JB_MASK_SAVED] is FALSE.  */
96      if (!resume_restores_sigmask (&c, wp) && wp[JB_MASK_SAVED])
97        {
98          /* sigmask was saved */
99#if defined(__linux__) || defined(__sun)
100          if (UNW_NUM_EH_REGS < 4 || _NSIG > 16 * sizeof (unw_word_t))
101            /* signal mask doesn't fit into EH arguments and we can't
102               put it on the stack without overwriting something
103               else... */
104            abort ();
105          else
106            if (unw_set_reg (&c, UNW_REG_EH + 2, wp[JB_MASK]) < 0
107                || (_NSIG > 8 * sizeof (unw_word_t)
108                    && unw_set_reg (&c, UNW_REG_EH + 3, wp[JB_MASK + 1]) < 0))
109              abort ();
110#elif defined(__FreeBSD__)
111          if (unw_set_reg (&c, UNW_REG_EH + 2, &wp[JB_MASK]) < 0)
112              abort();
113#else
114#error Port me
115#endif
116          cont = &_UI_siglongjmp_cont;
117        }
118
119      if (unw_set_reg (&c, UNW_REG_EH + 0, wp[JB_RP]) < 0
120          || unw_set_reg (&c, UNW_REG_EH + 1, val) < 0
121          || unw_set_reg (&c, UNW_REG_IP, (unw_word_t) (uintptr_t) cont))
122        abort ();
123
124      unw_resume (&c);
125
126      abort ();
127    }
128  while (unw_step (&c) > 0);
129
130  abort ();
131}
132