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