1/* Target-dependent code for FreeBSD/i386.
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 "arch-utils.h"
24#include "osabi.h"
25
26#include "i386-tdep.h"
27#include "i387-tdep.h"
28#include "solib-svr4.h"
29
30/* FreeBSD 3.0-RELEASE or later.  */
31
32/* From <machine/reg.h>.  */
33static int i386fbsd_r_reg_offset[] =
34{
35  9 * 4, 8 * 4, 7 * 4, 6 * 4,	/* %eax, %ecx, %edx, %ebx */
36  15 * 4, 4 * 4,		/* %esp, %ebp */
37  3 * 4, 2 * 4,			/* %esi, %edi */
38  12 * 4, 14 * 4,		/* %eip, %eflags */
39  13 * 4, 16 * 4,		/* %cs, %ss */
40  1 * 4, 0 * 4, -1, -1		/* %ds, %es, %fs, %gs */
41};
42
43/* Sigtramp routine location.  */
44CORE_ADDR i386fbsd_sigtramp_start_addr = 0xbfbfdf20;
45CORE_ADDR i386fbsd_sigtramp_end_addr = 0xbfbfdff0;
46
47/* From <machine/signal.h>.  */
48static int i386fbsd_sc_reg_offset[] =
49{
50  8 + 14 * 4,			/* %eax */
51  8 + 13 * 4,			/* %ecx */
52  8 + 12 * 4,			/* %edx */
53  8 + 11 * 4,			/* %ebx */
54  8 + 0 * 4,                    /* %esp */
55  8 + 1 * 4,                    /* %ebp */
56  8 + 10 * 4,                   /* %esi */
57  8 + 9 * 4,                    /* %edi */
58  8 + 3 * 4,                    /* %eip */
59  8 + 4 * 4,                    /* %eflags */
60  8 + 7 * 4,                    /* %cs */
61  8 + 8 * 4,                    /* %ss */
62  8 + 6 * 4,                    /* %ds */
63  8 + 5 * 4,                    /* %es */
64  8 + 15 * 4,			/* %fs */
65  8 + 16 * 4			/* %gs */
66};
67
68static void
69i386fbsdaout_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
70{
71  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
72
73  /* Obviously FreeBSD is BSD-based.  */
74  i386bsd_init_abi (info, gdbarch);
75
76  /* FreeBSD has a different `struct reg', and reserves some space for
77     its FPU emulator in `struct fpreg'.  */
78  tdep->gregset_reg_offset = i386fbsd_r_reg_offset;
79  tdep->gregset_num_regs = ARRAY_SIZE (i386fbsd_r_reg_offset);
80  tdep->sizeof_gregset = 18 * 4;
81  tdep->sizeof_fpregset = 176;
82
83  /* FreeBSD uses -freg-struct-return by default.  */
84  tdep->struct_return = reg_struct_return;
85
86  /* FreeBSD uses a different memory layout.  */
87  tdep->sigtramp_start = i386fbsd_sigtramp_start_addr;
88  tdep->sigtramp_end = i386fbsd_sigtramp_end_addr;
89
90  /* FreeBSD has a more complete `struct sigcontext'.  */
91  tdep->sc_reg_offset = i386fbsd_sc_reg_offset;
92  tdep->sc_num_regs = ARRAY_SIZE (i386fbsd_sc_reg_offset);
93}
94
95static void
96i386fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
97{
98  /* It's almost identical to FreeBSD a.out.  */
99  i386fbsdaout_init_abi (info, gdbarch);
100
101  /* Except that it uses ELF.  */
102  i386_elf_init_abi (info, gdbarch);
103
104  /* FreeBSD ELF uses SVR4-style shared libraries.  */
105  set_gdbarch_in_solib_call_trampoline
106    (gdbarch, generic_in_solib_call_trampoline);
107  set_solib_svr4_fetch_link_map_offsets
108    (gdbarch, svr4_ilp32_fetch_link_map_offsets);
109}
110
111/* FreeBSD 4.0-RELEASE or later.  */
112
113/* From <machine/reg.h>.  */
114static int i386fbsd4_r_reg_offset[] =
115{
116  10 * 4, 9 * 4, 8 * 4, 7 * 4,	/* %eax, %ecx, %edx, %ebx */
117  16 * 4, 5 * 4,		/* %esp, %ebp */
118  4 * 4, 3 * 4,			/* %esi, %edi */
119  13 * 4, 15 * 4,		/* %eip, %eflags */
120  14 * 4, 17 * 4,		/* %cs, %ss */
121  2 * 4, 1 * 4, 0 * 4, 18 * 4	/* %ds, %es, %fs, %gs */
122};
123
124/* From <machine/signal.h>.  */
125int i386fbsd4_sc_reg_offset[] =
126{
127  20 + 11 * 4,			/* %eax */
128  20 + 10 * 4,			/* %ecx */
129  20 + 9 * 4,			/* %edx */
130  20 + 8 * 4,			/* %ebx */
131  20 + 17 * 4,			/* %esp */
132  20 + 6 * 4,			/* %ebp */
133  20 + 5 * 4,			/* %esi */
134  20 + 4 * 4,			/* %edi */
135  20 + 14 * 4,			/* %eip */
136  20 + 16 * 4,			/* %eflags */
137  20 + 15 * 4,			/* %cs */
138  20 + 18 * 4,			/* %ss */
139  20 + 3 * 4,			/* %ds */
140  20 + 2 * 4,			/* %es */
141  20 + 1 * 4,			/* %fs */
142  20 + 0 * 4			/* %gs */
143};
144
145static void
146i386fbsd4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
147{
148  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
149
150  /* Inherit stuff from older releases.  We assume that FreeBSD
151     4.0-RELEASE always uses ELF.  */
152  i386fbsd_init_abi (info, gdbarch);
153
154  /* FreeBSD 4.0 introduced a new `struct reg'.  */
155  tdep->gregset_reg_offset = i386fbsd4_r_reg_offset;
156  tdep->gregset_num_regs = ARRAY_SIZE (i386fbsd4_r_reg_offset);
157  tdep->sizeof_gregset = 19 * 4;
158
159  /* FreeBSD 4.0 introduced a new `struct sigcontext'.  */
160  tdep->sc_reg_offset = i386fbsd4_sc_reg_offset;
161  tdep->sc_num_regs = ARRAY_SIZE (i386fbsd4_sc_reg_offset);
162}
163
164
165/* Provide a prototype to silence -Wmissing-prototypes.  */
166void _initialize_i386fbsd_tdep (void);
167
168void
169_initialize_i386fbsd_tdep (void)
170{
171  gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_FREEBSD_AOUT,
172			  i386fbsdaout_init_abi);
173  gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_FREEBSD_ELF,
174			  i386fbsd4_init_abi);
175}
176