1/* DWARF2 EH unwinding support for AMD x86-64 and x86.
2   Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11In addition to the permissions in the GNU General Public License, the
12Free Software Foundation gives you unlimited permission to link the
13compiled version of this file with other programs, and to distribute
14those programs without any restriction coming from the use of this
15file.  (The General Public License restrictions do apply in other
16respects; for example, they cover modification of the file, and
17distribution when not linked into another program.)
18
19GCC is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22GNU General Public License for more details.
23
24You should have received a copy of the GNU General Public License
25along with GCC; see the file COPYING.  If not, write to
26the Free Software Foundation, 51 Franklin Street, Fifth Floor,
27Boston, MA 02110-1301, USA.  */
28
29/* Do code reading to identify a signal frame, and set the frame
30   state data appropriately.  See unwind-dw2.c for the structs.
31   Don't use this at all if inhibit_libc is used.  */
32
33#ifndef inhibit_libc
34
35#ifdef __x86_64__
36
37#include <signal.h>
38#include <sys/ucontext.h>
39
40#define MD_FALLBACK_FRAME_STATE_FOR x86_64_fallback_frame_state
41
42static _Unwind_Reason_Code
43x86_64_fallback_frame_state (struct _Unwind_Context *context,
44			     _Unwind_FrameState *fs)
45{
46  unsigned char *pc = context->ra;
47  struct sigcontext *sc;
48  long new_cfa;
49
50  /* movq __NR_rt_sigreturn, %rax ; syscall  */
51  if (*(unsigned char *)(pc+0) == 0x48
52      && *(unsigned long *)(pc+1) == 0x050f0000000fc0c7)
53    {
54      struct ucontext *uc_ = context->cfa;
55      /* The void * cast is necessary to avoid an aliasing warning.
56         The aliasing warning is correct, but should not be a problem
57         because it does not alias anything.  */
58      sc = (struct sigcontext *) (void *) &uc_->uc_mcontext;
59    }
60  else
61    return _URC_END_OF_STACK;
62
63  new_cfa = sc->rsp;
64  fs->cfa_how = CFA_REG_OFFSET;
65  /* Register 7 is rsp  */
66  fs->cfa_reg = 7;
67  fs->cfa_offset = new_cfa - (long) context->cfa;
68
69  /* The SVR4 register numbering macros aren't usable in libgcc.  */
70  fs->regs.reg[0].how = REG_SAVED_OFFSET;
71  fs->regs.reg[0].loc.offset = (long)&sc->rax - new_cfa;
72  fs->regs.reg[1].how = REG_SAVED_OFFSET;
73  fs->regs.reg[1].loc.offset = (long)&sc->rdx - new_cfa;
74  fs->regs.reg[2].how = REG_SAVED_OFFSET;
75  fs->regs.reg[2].loc.offset = (long)&sc->rcx - new_cfa;
76  fs->regs.reg[3].how = REG_SAVED_OFFSET;
77  fs->regs.reg[3].loc.offset = (long)&sc->rbx - new_cfa;
78  fs->regs.reg[4].how = REG_SAVED_OFFSET;
79  fs->regs.reg[4].loc.offset = (long)&sc->rsi - new_cfa;
80  fs->regs.reg[5].how = REG_SAVED_OFFSET;
81  fs->regs.reg[5].loc.offset = (long)&sc->rdi - new_cfa;
82  fs->regs.reg[6].how = REG_SAVED_OFFSET;
83  fs->regs.reg[6].loc.offset = (long)&sc->rbp - new_cfa;
84  fs->regs.reg[8].how = REG_SAVED_OFFSET;
85  fs->regs.reg[8].loc.offset = (long)&sc->r8 - new_cfa;
86  fs->regs.reg[9].how = REG_SAVED_OFFSET;
87  fs->regs.reg[9].loc.offset = (long)&sc->r9 - new_cfa;
88  fs->regs.reg[10].how = REG_SAVED_OFFSET;
89  fs->regs.reg[10].loc.offset = (long)&sc->r10 - new_cfa;
90  fs->regs.reg[11].how = REG_SAVED_OFFSET;
91  fs->regs.reg[11].loc.offset = (long)&sc->r11 - new_cfa;
92  fs->regs.reg[12].how = REG_SAVED_OFFSET;
93  fs->regs.reg[12].loc.offset = (long)&sc->r12 - new_cfa;
94  fs->regs.reg[13].how = REG_SAVED_OFFSET;
95  fs->regs.reg[13].loc.offset = (long)&sc->r13 - new_cfa;
96  fs->regs.reg[14].how = REG_SAVED_OFFSET;
97  fs->regs.reg[14].loc.offset = (long)&sc->r14 - new_cfa;
98  fs->regs.reg[15].how = REG_SAVED_OFFSET;
99  fs->regs.reg[15].loc.offset = (long)&sc->r15 - new_cfa;
100  fs->regs.reg[16].how = REG_SAVED_OFFSET;
101  fs->regs.reg[16].loc.offset = (long)&sc->rip - new_cfa;
102  fs->retaddr_column = 16;
103  fs->signal_frame = 1;
104  return _URC_NO_REASON;
105}
106
107#else /* ifdef __x86_64__  */
108
109/* There's no sys/ucontext.h for glibc 2.0, so no
110   signal-turned-exceptions for them.  There's also no configure-run for
111   the target, so we can't check on (e.g.) HAVE_SYS_UCONTEXT_H.  Using the
112   target libc version macro should be enough.  */
113#if !(__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
114
115#include <signal.h>
116#include <sys/ucontext.h>
117
118#define MD_FALLBACK_FRAME_STATE_FOR x86_fallback_frame_state
119
120static _Unwind_Reason_Code
121x86_fallback_frame_state (struct _Unwind_Context *context,
122			  _Unwind_FrameState *fs)
123{
124  unsigned char *pc = context->ra;
125  struct sigcontext *sc;
126  long new_cfa;
127
128  /* popl %eax ; movl $__NR_sigreturn,%eax ; int $0x80  */
129  if (*(unsigned short *)(pc+0) == 0xb858
130      && *(unsigned int *)(pc+2) == 119
131      && *(unsigned short *)(pc+6) == 0x80cd)
132    sc = context->cfa + 4;
133  /* movl $__NR_rt_sigreturn,%eax ; int $0x80  */
134  else if (*(unsigned char *)(pc+0) == 0xb8
135	   && *(unsigned int *)(pc+1) == 173
136	   && *(unsigned short *)(pc+5) == 0x80cd)
137    {
138      struct rt_sigframe {
139	int sig;
140	struct siginfo *pinfo;
141	void *puc;
142	struct siginfo info;
143	struct ucontext uc;
144      } *rt_ = context->cfa;
145      /* The void * cast is necessary to avoid an aliasing warning.
146         The aliasing warning is correct, but should not be a problem
147         because it does not alias anything.  */
148      sc = (struct sigcontext *) (void *) &rt_->uc.uc_mcontext;
149    }
150  else
151    return _URC_END_OF_STACK;
152
153  new_cfa = sc->REG_NAME(esp);
154  fs->cfa_how = CFA_REG_OFFSET;
155  fs->cfa_reg = 4;
156  fs->cfa_offset = new_cfa - (long) context->cfa;
157
158  /* The SVR4 register numbering macros aren't usable in libgcc.  */
159  fs->regs.reg[0].how = REG_SAVED_OFFSET;
160  fs->regs.reg[0].loc.offset = (long)&sc->REG_NAME(eax) - new_cfa;
161  fs->regs.reg[3].how = REG_SAVED_OFFSET;
162  fs->regs.reg[3].loc.offset = (long)&sc->REG_NAME(ebx) - new_cfa;
163  fs->regs.reg[1].how = REG_SAVED_OFFSET;
164  fs->regs.reg[1].loc.offset = (long)&sc->REG_NAME(ecx) - new_cfa;
165  fs->regs.reg[2].how = REG_SAVED_OFFSET;
166  fs->regs.reg[2].loc.offset = (long)&sc->REG_NAME(edx) - new_cfa;
167  fs->regs.reg[6].how = REG_SAVED_OFFSET;
168  fs->regs.reg[6].loc.offset = (long)&sc->REG_NAME(esi) - new_cfa;
169  fs->regs.reg[7].how = REG_SAVED_OFFSET;
170  fs->regs.reg[7].loc.offset = (long)&sc->REG_NAME(edi) - new_cfa;
171  fs->regs.reg[5].how = REG_SAVED_OFFSET;
172  fs->regs.reg[5].loc.offset = (long)&sc->REG_NAME(ebp) - new_cfa;
173  fs->regs.reg[8].how = REG_SAVED_OFFSET;
174  fs->regs.reg[8].loc.offset = (long)&sc->REG_NAME(eip) - new_cfa;
175  fs->retaddr_column = 8;
176  fs->signal_frame = 1;
177  return _URC_NO_REASON;
178}
179#endif /* not glibc 2.0 */
180#endif /* ifdef __x86_64__  */
181#endif /* ifdef inhibit_libc  */
182