1130803Smarcel/* Target-dependent code for FreeBSD/i386.
2130803Smarcel
3130803Smarcel   Copyright 2003, 2004 Free Software Foundation, Inc.
4130803Smarcel
5130803Smarcel   This file is part of GDB.
6130803Smarcel
7130803Smarcel   This program is free software; you can redistribute it and/or modify
8130803Smarcel   it under the terms of the GNU General Public License as published by
9130803Smarcel   the Free Software Foundation; either version 2 of the License, or
10130803Smarcel   (at your option) any later version.
11130803Smarcel
12130803Smarcel   This program is distributed in the hope that it will be useful,
13130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
14130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15130803Smarcel   GNU General Public License for more details.
16130803Smarcel
17130803Smarcel   You should have received a copy of the GNU General Public License
18130803Smarcel   along with this program; if not, write to the Free Software
19130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
20130803Smarcel   Boston, MA 02111-1307, USA.  */
21130803Smarcel
22130803Smarcel#include "defs.h"
23130803Smarcel#include "arch-utils.h"
24130803Smarcel#include "osabi.h"
25130803Smarcel
26130803Smarcel#include "i386-tdep.h"
27130803Smarcel#include "i387-tdep.h"
28130803Smarcel#include "solib-svr4.h"
29130803Smarcel
30130803Smarcel/* FreeBSD 3.0-RELEASE or later.  */
31130803Smarcel
32130803Smarcel/* From <machine/reg.h>.  */
33130803Smarcelstatic int i386fbsd_r_reg_offset[] =
34130803Smarcel{
35130803Smarcel  9 * 4, 8 * 4, 7 * 4, 6 * 4,	/* %eax, %ecx, %edx, %ebx */
36130803Smarcel  15 * 4, 4 * 4,		/* %esp, %ebp */
37130803Smarcel  3 * 4, 2 * 4,			/* %esi, %edi */
38130803Smarcel  12 * 4, 14 * 4,		/* %eip, %eflags */
39130803Smarcel  13 * 4, 16 * 4,		/* %cs, %ss */
40130803Smarcel  1 * 4, 0 * 4, -1, -1		/* %ds, %es, %fs, %gs */
41130803Smarcel};
42130803Smarcel
43130803Smarcel/* Sigtramp routine location.  */
44130803SmarcelCORE_ADDR i386fbsd_sigtramp_start_addr = 0xbfbfdf20;
45130803SmarcelCORE_ADDR i386fbsd_sigtramp_end_addr = 0xbfbfdff0;
46130803Smarcel
47130803Smarcel/* From <machine/signal.h>.  */
48130803Smarcelstatic int i386fbsd_sc_reg_offset[] =
49130803Smarcel{
50130803Smarcel  8 + 14 * 4,			/* %eax */
51130803Smarcel  8 + 13 * 4,			/* %ecx */
52130803Smarcel  8 + 12 * 4,			/* %edx */
53130803Smarcel  8 + 11 * 4,			/* %ebx */
54130803Smarcel  8 + 0 * 4,                    /* %esp */
55130803Smarcel  8 + 1 * 4,                    /* %ebp */
56130803Smarcel  8 + 10 * 4,                   /* %esi */
57130803Smarcel  8 + 9 * 4,                    /* %edi */
58130803Smarcel  8 + 3 * 4,                    /* %eip */
59130803Smarcel  8 + 4 * 4,                    /* %eflags */
60130803Smarcel  8 + 7 * 4,                    /* %cs */
61130803Smarcel  8 + 8 * 4,                    /* %ss */
62130803Smarcel  8 + 6 * 4,                    /* %ds */
63130803Smarcel  8 + 5 * 4,                    /* %es */
64130803Smarcel  8 + 15 * 4,			/* %fs */
65130803Smarcel  8 + 16 * 4			/* %gs */
66130803Smarcel};
67130803Smarcel
68130803Smarcelstatic void
69130803Smarceli386fbsdaout_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
70130803Smarcel{
71130803Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
72130803Smarcel
73130803Smarcel  /* Obviously FreeBSD is BSD-based.  */
74130803Smarcel  i386bsd_init_abi (info, gdbarch);
75130803Smarcel
76130803Smarcel  /* FreeBSD has a different `struct reg', and reserves some space for
77130803Smarcel     its FPU emulator in `struct fpreg'.  */
78130803Smarcel  tdep->gregset_reg_offset = i386fbsd_r_reg_offset;
79130803Smarcel  tdep->gregset_num_regs = ARRAY_SIZE (i386fbsd_r_reg_offset);
80130803Smarcel  tdep->sizeof_gregset = 18 * 4;
81130803Smarcel  tdep->sizeof_fpregset = 176;
82130803Smarcel
83130803Smarcel  /* FreeBSD uses -freg-struct-return by default.  */
84130803Smarcel  tdep->struct_return = reg_struct_return;
85130803Smarcel
86130803Smarcel  /* FreeBSD uses a different memory layout.  */
87130803Smarcel  tdep->sigtramp_start = i386fbsd_sigtramp_start_addr;
88130803Smarcel  tdep->sigtramp_end = i386fbsd_sigtramp_end_addr;
89130803Smarcel
90130803Smarcel  /* FreeBSD has a more complete `struct sigcontext'.  */
91130803Smarcel  tdep->sc_reg_offset = i386fbsd_sc_reg_offset;
92130803Smarcel  tdep->sc_num_regs = ARRAY_SIZE (i386fbsd_sc_reg_offset);
93130803Smarcel}
94130803Smarcel
95130803Smarcelstatic void
96130803Smarceli386fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
97130803Smarcel{
98130803Smarcel  /* It's almost identical to FreeBSD a.out.  */
99130803Smarcel  i386fbsdaout_init_abi (info, gdbarch);
100130803Smarcel
101130803Smarcel  /* Except that it uses ELF.  */
102130803Smarcel  i386_elf_init_abi (info, gdbarch);
103130803Smarcel
104130803Smarcel  /* FreeBSD ELF uses SVR4-style shared libraries.  */
105130803Smarcel  set_gdbarch_in_solib_call_trampoline
106130803Smarcel    (gdbarch, generic_in_solib_call_trampoline);
107130803Smarcel  set_solib_svr4_fetch_link_map_offsets
108130803Smarcel    (gdbarch, svr4_ilp32_fetch_link_map_offsets);
109130803Smarcel}
110130803Smarcel
111130803Smarcel/* FreeBSD 4.0-RELEASE or later.  */
112130803Smarcel
113130803Smarcel/* From <machine/reg.h>.  */
114130803Smarcelstatic int i386fbsd4_r_reg_offset[] =
115130803Smarcel{
116130803Smarcel  10 * 4, 9 * 4, 8 * 4, 7 * 4,	/* %eax, %ecx, %edx, %ebx */
117130803Smarcel  16 * 4, 5 * 4,		/* %esp, %ebp */
118130803Smarcel  4 * 4, 3 * 4,			/* %esi, %edi */
119130803Smarcel  13 * 4, 15 * 4,		/* %eip, %eflags */
120130803Smarcel  14 * 4, 17 * 4,		/* %cs, %ss */
121130803Smarcel  2 * 4, 1 * 4, 0 * 4, 18 * 4	/* %ds, %es, %fs, %gs */
122130803Smarcel};
123130803Smarcel
124130803Smarcel/* From <machine/signal.h>.  */
125130803Smarcelint i386fbsd4_sc_reg_offset[] =
126130803Smarcel{
127130803Smarcel  20 + 11 * 4,			/* %eax */
128130803Smarcel  20 + 10 * 4,			/* %ecx */
129130803Smarcel  20 + 9 * 4,			/* %edx */
130130803Smarcel  20 + 8 * 4,			/* %ebx */
131130803Smarcel  20 + 17 * 4,			/* %esp */
132130803Smarcel  20 + 6 * 4,			/* %ebp */
133130803Smarcel  20 + 5 * 4,			/* %esi */
134130803Smarcel  20 + 4 * 4,			/* %edi */
135130803Smarcel  20 + 14 * 4,			/* %eip */
136130803Smarcel  20 + 16 * 4,			/* %eflags */
137130803Smarcel  20 + 15 * 4,			/* %cs */
138130803Smarcel  20 + 18 * 4,			/* %ss */
139130803Smarcel  20 + 3 * 4,			/* %ds */
140130803Smarcel  20 + 2 * 4,			/* %es */
141130803Smarcel  20 + 1 * 4,			/* %fs */
142130803Smarcel  20 + 0 * 4			/* %gs */
143130803Smarcel};
144130803Smarcel
145130803Smarcelstatic void
146130803Smarceli386fbsd4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
147130803Smarcel{
148130803Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
149130803Smarcel
150130803Smarcel  /* Inherit stuff from older releases.  We assume that FreeBSD
151130803Smarcel     4.0-RELEASE always uses ELF.  */
152130803Smarcel  i386fbsd_init_abi (info, gdbarch);
153130803Smarcel
154130803Smarcel  /* FreeBSD 4.0 introduced a new `struct reg'.  */
155130803Smarcel  tdep->gregset_reg_offset = i386fbsd4_r_reg_offset;
156130803Smarcel  tdep->gregset_num_regs = ARRAY_SIZE (i386fbsd4_r_reg_offset);
157130803Smarcel  tdep->sizeof_gregset = 19 * 4;
158130803Smarcel
159130803Smarcel  /* FreeBSD 4.0 introduced a new `struct sigcontext'.  */
160130803Smarcel  tdep->sc_reg_offset = i386fbsd4_sc_reg_offset;
161130803Smarcel  tdep->sc_num_regs = ARRAY_SIZE (i386fbsd4_sc_reg_offset);
162130803Smarcel}
163130803Smarcel
164130803Smarcel
165130803Smarcel/* Provide a prototype to silence -Wmissing-prototypes.  */
166130803Smarcelvoid _initialize_i386fbsd_tdep (void);
167130803Smarcel
168130803Smarcelvoid
169130803Smarcel_initialize_i386fbsd_tdep (void)
170130803Smarcel{
171130803Smarcel  gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_FREEBSD_AOUT,
172130803Smarcel			  i386fbsdaout_init_abi);
173130803Smarcel  gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_FREEBSD_ELF,
174130803Smarcel			  i386fbsd4_init_abi);
175130803Smarcel}
176