* amd64obsd-tdep.c: Include "regset.h" and "i387-tdep.h". Fix
[external/binutils.git] / gdb / amd64obsd-tdep.c
1 /* Target-dependent code for OpenBSD/amd64.
2
3    Copyright 2003, 2004 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "gdbcore.h"
25 #include "osabi.h"
26 #include "regset.h"
27 #include "target.h"
28
29 #include "gdb_assert.h"
30 #include "gdb_string.h"
31
32 #include "x86-64-tdep.h"
33 #include "i387-tdep.h"
34
35 /* Support for core dumps.  */
36
37 static void
38 amd64obsd_supply_regset (const struct regset *regset,
39                          struct regcache *regcache, int regnum,
40                          const void *regs, size_t len)
41 {
42   const struct gdbarch_tdep *tdep = regset->descr;
43
44   gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE);
45
46   i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
47   x86_64_supply_fxsave (regcache, regnum, (char *)regs + tdep->sizeof_gregset);
48 }
49
50 static const struct regset *
51 amd64obsd_regset_from_core_section (struct gdbarch *gdbarch,
52                                     const char *sect_name, size_t sect_size)
53 {
54   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
55
56   /* OpenBSD core dumps don't use seperate register sets for the
57      general-purpose and floating-point registers.  */
58
59   if (strcmp (sect_name, ".reg") == 0
60       && sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE)
61     {
62       if (tdep->gregset == NULL)
63         {
64           tdep->gregset = XMALLOC (struct regset);
65           tdep->gregset->descr = tdep;
66           tdep->gregset->supply_regset = amd64obsd_supply_regset;
67         }
68       return tdep->gregset;
69     }
70
71   return NULL;
72 }
73 \f
74
75 /* Support for signal handlers.  */
76
77 static const int amd64obsd_page_size = 4096;
78
79 static int
80 amd64obsd_pc_in_sigtramp (CORE_ADDR pc, char *name)
81 {
82   CORE_ADDR start_pc = (pc & ~(amd64obsd_page_size - 1));
83   const char sigreturn[] =
84   {
85     0x48, 0xc7, 0xc0,
86     0x67, 0x00, 0x00, 0x00,     /* movq $SYS_sigreturn, %rax */
87     0x0f, 0x05                  /* syscall */
88   };
89   char *buf;
90
91   if (name)
92     return 0;
93
94   /* If we can't read the instructions at START_PC, return zero.  */
95   buf = alloca (sizeof sigreturn);
96   if (target_read_memory (start_pc + 0x7, buf, sizeof sigreturn))
97     return 0;
98
99   /* Check for sigreturn(2).  */
100   if (memcmp (buf, sigreturn, sizeof sigreturn))
101     return 0;
102
103   return 1;
104 }
105
106 /* Assuming NEXT_FRAME is for a frame following a BSD sigtramp
107    routine, return the address of the associated sigcontext structure.  */
108
109 static CORE_ADDR
110 amd64obsd_sigcontext_addr (struct frame_info *next_frame)
111 {
112   /* The %rsp register points at `struct sigcontext' upon entry of a
113      signal trampoline.  */
114   return frame_unwind_register_unsigned (next_frame, X86_64_RSP_REGNUM);
115 }
116 \f
117 /* OpenBSD 3.5 or later.  */
118
119 /* Mapping between the general-purpose registers in `struct reg'
120    format and GDB's register cache layout.  */
121
122 /* From <machine/reg.h>.  */
123 int amd64obsd_r_reg_offset[] =
124 {
125   14 * 8,                       /* %rax */
126   13 * 8,                       /* %rbx */
127   3 * 8,                        /* %rcx */
128   2 * 8,                        /* %rdx */
129   1 * 8,                        /* %rsi */
130   0 * 8,                        /* %rdi */
131   12 * 8,                       /* %rbp */
132   15 * 8,                       /* %rsp */
133   4 * 8,                        /* %r8 .. */
134   5 * 8,
135   6 * 8,
136   7 * 8,
137   8 * 8,
138   9 * 8,
139   10 * 8,
140   11 * 8,                       /* ... %r15 */
141   16 * 8,                       /* %rip */
142   17 * 8,                       /* %eflags */
143   18 * 8,                       /* %cs */
144   19 * 8,                       /* %ss */
145   20 * 8,                       /* %ds */
146   21 * 8,                       /* %es */
147   22 * 8,                       /* %fs */
148   23 * 8                        /* %gs */
149 };
150
151 /* From <machine/signal.h>.  */
152 static int amd64obsd_sc_reg_offset[] =
153 {
154   14 * 8,                       /* %rax */
155   13 * 8,                       /* %rbx */
156   3 * 8,                        /* %rcx */
157   2 * 8,                        /* %rdx */
158   1 * 8,                        /* %rsi */
159   0 * 8,                        /* %rdi */
160   12 * 8,                       /* %rbp */
161   24 * 8,                       /* %rsp */
162   4 * 8,                        /* %r8 ... */
163   5 * 8,
164   6 * 8,
165   7 * 8,
166   8 * 8,
167   9 * 8,
168   10 * 8,
169   11 * 8,                       /* ... %r15 */
170   21 * 8,                       /* %rip */
171   23 * 8,                       /* %eflags */
172   22 * 8,                       /* %cs */
173   25 * 8,                       /* %ss */
174   18 * 8,                       /* %ds */
175   17 * 8,                       /* %es */
176   16 * 8,                       /* %fs */
177   15 * 8                        /* %gs */
178 };
179
180 static void
181 amd64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
182 {
183   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
184
185   x86_64_init_abi (info, gdbarch);
186
187   /* Initialize general-purpose register set details.  */
188   tdep->gregset_reg_offset = amd64obsd_r_reg_offset;
189   tdep->gregset_num_regs = ARRAY_SIZE (amd64obsd_r_reg_offset);
190   tdep->sizeof_gregset = 24 * 8;
191
192   set_gdbarch_regset_from_core_section (gdbarch,
193                                         amd64obsd_regset_from_core_section);
194
195   tdep->jb_pc_offset = 7 * 8;
196
197   set_gdbarch_pc_in_sigtramp (gdbarch, amd64obsd_pc_in_sigtramp);
198   tdep->sigcontext_addr = amd64obsd_sigcontext_addr;
199   tdep->sc_reg_offset = amd64obsd_sc_reg_offset;
200   tdep->sc_num_regs = ARRAY_SIZE (amd64obsd_sc_reg_offset);
201 }
202 \f
203
204 /* Provide a prototype to silence -Wmissing-prototypes.  */
205 void _initialize_amd64obsd_tdep (void);
206
207 void
208 _initialize_amd64obsd_tdep (void)
209 {
210   /* The OpenBSD/amd64 native dependent code makes this assumption.  */
211   gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset) == X86_64_NUM_GREGS);
212
213   gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
214                           GDB_OSABI_OPENBSD_ELF, amd64obsd_init_abi);
215
216   /* OpenBSD uses traditional (a.out) NetBSD-style core dumps.  */
217   gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
218                           GDB_OSABI_NETBSD_AOUT, amd64obsd_init_abi);
219 }