* i386obsd-tdep.c: Include "regset.h", "gdb_assert.h" and
[external/binutils.git] / gdb / i386obsd-tdep.c
1 /* Target-dependent code for OpenBSD/i386.
2
3    Copyright 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002, 2003
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "arch-utils.h"
25 #include "gdbcore.h"
26 #include "regcache.h"
27 #include "regset.h"
28 #include "osabi.h"
29
30 #include "gdb_assert.h"
31 #include "gdb_string.h"
32
33 #include "i386-tdep.h"
34 #include "i387-tdep.h"
35
36 /* From <machine/reg.h>.  */
37 static int i386obsd_r_reg_offset[] =
38 {
39   0 * 4,                        /* %eax */
40   1 * 4,                        /* %ecx */
41   2 * 4,                        /* %edx */
42   3 * 4,                        /* %ebx */
43   4 * 4,                        /* %esp */
44   5 * 4,                        /* %ebp */
45   6 * 4,                        /* %esi */
46   7 * 4,                        /* %edi */
47   8 * 4,                        /* %eip */
48   9 * 4,                        /* %eflags */
49   10 * 4,                       /* %cs */
50   11 * 4,                       /* %ss */
51   12 * 4,                       /* %ds */
52   13 * 4,                       /* %es */
53   14 * 4,                       /* %fs */
54   15 * 4                        /* %gs */
55 };
56
57 static void
58 i386obsd_aout_supply_regset (const struct regset *regset,
59                              struct regcache *regcache, int regnum,
60                              const void *regs, size_t len)
61 {
62   const struct gdbarch_tdep *tdep = regset->descr;
63
64   gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE);
65
66   i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
67   i387_supply_fsave (regcache, regnum, (char *) regs + tdep->sizeof_gregset);
68 }
69
70 const struct regset *
71 i386obsd_aout_regset_from_core_section (struct gdbarch *gdbarch,
72                                         const char *sect_name,
73                                         size_t sect_size)
74 {
75   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
76
77   /* OpenBSD a.out core dumps don't use seperate register sets for the
78      general-purpose and floating-point registers.  */
79
80   if (strcmp (sect_name, ".reg") == 0
81       && sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE)
82     {
83       if (tdep->gregset == NULL)
84         {
85           tdep->gregset = XMALLOC (struct regset);
86           tdep->gregset->descr = tdep;
87           tdep->gregset->supply_regset = i386obsd_aout_supply_regset;
88         }
89       return tdep->gregset;
90     }
91
92   return NULL;
93 }
94
95 \f
96
97 CORE_ADDR i386obsd_sigtramp_start = 0xbfbfdf20;
98 CORE_ADDR i386obsd_sigtramp_end = 0xbfbfdff0;
99
100 /* From <machine/signal.h>.  */
101 int i386obsd_sc_reg_offset[I386_NUM_GREGS] =
102 {
103   10 * 4,                       /* %eax */
104   9 * 4,                        /* %ecx */
105   8 * 4,                        /* %edx */
106   7 * 4,                        /* %ebx */
107   14 * 4,                       /* %esp */
108   6 * 4,                        /* %ebp */
109   5 * 4,                        /* %esi */
110   4 * 4,                        /* %edi */
111   11 * 4,                       /* %eip */
112   13 * 4,                       /* %eflags */
113   12 * 4,                       /* %cs */
114   15 * 4,                       /* %ss */
115   3 * 4,                        /* %ds */
116   2 * 4,                        /* %es */
117   1 * 4,                        /* %fs */
118   0 * 4                         /* %gs */
119 };
120
121 static void 
122 i386obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
123 {
124   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
125
126   /* Obviously OpenBSD is BSD-based.  */
127   i386bsd_init_abi (info, gdbarch);
128
129   /* OpenBSD has a different `struct reg'.  */
130   tdep->gregset_reg_offset = i386obsd_r_reg_offset;
131   tdep->gregset_num_regs = ARRAY_SIZE (i386obsd_r_reg_offset);
132   tdep->sizeof_gregset = 16 * 4;
133
134   /* OpenBSD has a single register set.  */
135   set_gdbarch_regset_from_core_section
136     (gdbarch, i386obsd_aout_regset_from_core_section);
137
138   /* OpenBSD uses -freg-struct-return by default.  */
139   tdep->struct_return = reg_struct_return;
140
141   /* OpenBSD uses a different memory layout.  */
142   tdep->sigtramp_start = i386obsd_sigtramp_start;
143   tdep->sigtramp_end = i386obsd_sigtramp_end;
144
145   /* OpenBSD has a `struct sigcontext' that's different from the
146      origional 4.3 BSD.  */
147   tdep->sc_reg_offset = i386obsd_sc_reg_offset;
148   tdep->sc_num_regs = ARRAY_SIZE (i386obsd_sc_reg_offset);
149 }
150 \f
151
152 /* Provide a prototype to silence -Wmissing-prototypes.  */
153 void _initialize_i386obsd_tdep (void);
154
155 void
156 _initialize_i386obsd_tdep (void)
157 {
158   /* FIXME: kettenis/20021020: Since OpenBSD/i386 binaries are
159      indistingushable from NetBSD/i386 a.out binaries, building a GDB
160      that should support both these targets will probably not work as
161      expected.  */
162 #define GDB_OSABI_OPENBSD_AOUT GDB_OSABI_NETBSD_AOUT
163
164   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_OPENBSD_AOUT,
165                           i386obsd_init_abi);
166 }