15796c8dcSSimon Schubert /* Target-dependent code for FreeBSD/i386.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 2003-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This file is part of GDB.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert (at your option) any later version.
115796c8dcSSimon Schubert
125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
155796c8dcSSimon Schubert GNU General Public License for more details.
165796c8dcSSimon Schubert
175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "arch-utils.h"
225796c8dcSSimon Schubert #include "gdbcore.h"
235796c8dcSSimon Schubert #include "osabi.h"
245796c8dcSSimon Schubert #include "regcache.h"
255796c8dcSSimon Schubert
265796c8dcSSimon Schubert #include "gdb_assert.h"
275796c8dcSSimon Schubert
285796c8dcSSimon Schubert #include "i386-tdep.h"
295796c8dcSSimon Schubert #include "i387-tdep.h"
305796c8dcSSimon Schubert #include "bsd-uthread.h"
315796c8dcSSimon Schubert #include "solib-svr4.h"
325796c8dcSSimon Schubert
335796c8dcSSimon Schubert /* FreeBSD 3.0-RELEASE or later. */
345796c8dcSSimon Schubert
355796c8dcSSimon Schubert /* From <machine/reg.h>. */
365796c8dcSSimon Schubert static int i386fbsd_r_reg_offset[] =
375796c8dcSSimon Schubert {
385796c8dcSSimon Schubert 9 * 4, 8 * 4, 7 * 4, 6 * 4, /* %eax, %ecx, %edx, %ebx */
395796c8dcSSimon Schubert 15 * 4, 4 * 4, /* %esp, %ebp */
405796c8dcSSimon Schubert 3 * 4, 2 * 4, /* %esi, %edi */
415796c8dcSSimon Schubert 12 * 4, 14 * 4, /* %eip, %eflags */
425796c8dcSSimon Schubert 13 * 4, 16 * 4, /* %cs, %ss */
435796c8dcSSimon Schubert 1 * 4, 0 * 4, -1, -1 /* %ds, %es, %fs, %gs */
445796c8dcSSimon Schubert };
455796c8dcSSimon Schubert
465796c8dcSSimon Schubert /* Sigtramp routine location. */
475796c8dcSSimon Schubert CORE_ADDR i386fbsd_sigtramp_start_addr = 0xbfbfdf20;
485796c8dcSSimon Schubert CORE_ADDR i386fbsd_sigtramp_end_addr = 0xbfbfdff0;
495796c8dcSSimon Schubert
505796c8dcSSimon Schubert /* From <machine/signal.h>. */
515796c8dcSSimon Schubert int i386fbsd_sc_reg_offset[] =
525796c8dcSSimon Schubert {
535796c8dcSSimon Schubert 8 + 14 * 4, /* %eax */
545796c8dcSSimon Schubert 8 + 13 * 4, /* %ecx */
555796c8dcSSimon Schubert 8 + 12 * 4, /* %edx */
565796c8dcSSimon Schubert 8 + 11 * 4, /* %ebx */
575796c8dcSSimon Schubert 8 + 0 * 4, /* %esp */
585796c8dcSSimon Schubert 8 + 1 * 4, /* %ebp */
595796c8dcSSimon Schubert 8 + 10 * 4, /* %esi */
605796c8dcSSimon Schubert 8 + 9 * 4, /* %edi */
615796c8dcSSimon Schubert 8 + 3 * 4, /* %eip */
625796c8dcSSimon Schubert 8 + 4 * 4, /* %eflags */
635796c8dcSSimon Schubert 8 + 7 * 4, /* %cs */
645796c8dcSSimon Schubert 8 + 8 * 4, /* %ss */
655796c8dcSSimon Schubert 8 + 6 * 4, /* %ds */
665796c8dcSSimon Schubert 8 + 5 * 4, /* %es */
675796c8dcSSimon Schubert 8 + 15 * 4, /* %fs */
685796c8dcSSimon Schubert 8 + 16 * 4 /* %gs */
695796c8dcSSimon Schubert };
705796c8dcSSimon Schubert
715796c8dcSSimon Schubert /* From /usr/src/lib/libc/i386/gen/_setjmp.S. */
725796c8dcSSimon Schubert static int i386fbsd_jmp_buf_reg_offset[] =
735796c8dcSSimon Schubert {
745796c8dcSSimon Schubert -1, /* %eax */
755796c8dcSSimon Schubert -1, /* %ecx */
765796c8dcSSimon Schubert -1, /* %edx */
775796c8dcSSimon Schubert 1 * 4, /* %ebx */
785796c8dcSSimon Schubert 2 * 4, /* %esp */
795796c8dcSSimon Schubert 3 * 4, /* %ebp */
805796c8dcSSimon Schubert 4 * 4, /* %esi */
815796c8dcSSimon Schubert 5 * 4, /* %edi */
825796c8dcSSimon Schubert 0 * 4 /* %eip */
835796c8dcSSimon Schubert };
845796c8dcSSimon Schubert
855796c8dcSSimon Schubert static void
i386fbsd_supply_uthread(struct regcache * regcache,int regnum,CORE_ADDR addr)865796c8dcSSimon Schubert i386fbsd_supply_uthread (struct regcache *regcache,
875796c8dcSSimon Schubert int regnum, CORE_ADDR addr)
885796c8dcSSimon Schubert {
89*ef5ccd6cSJohn Marino gdb_byte buf[4];
905796c8dcSSimon Schubert int i;
915796c8dcSSimon Schubert
925796c8dcSSimon Schubert gdb_assert (regnum >= -1);
935796c8dcSSimon Schubert
945796c8dcSSimon Schubert for (i = 0; i < ARRAY_SIZE (i386fbsd_jmp_buf_reg_offset); i++)
955796c8dcSSimon Schubert {
965796c8dcSSimon Schubert if (i386fbsd_jmp_buf_reg_offset[i] != -1
975796c8dcSSimon Schubert && (regnum == -1 || regnum == i))
985796c8dcSSimon Schubert {
995796c8dcSSimon Schubert read_memory (addr + i386fbsd_jmp_buf_reg_offset[i], buf, 4);
1005796c8dcSSimon Schubert regcache_raw_supply (regcache, i, buf);
1015796c8dcSSimon Schubert }
1025796c8dcSSimon Schubert }
1035796c8dcSSimon Schubert }
1045796c8dcSSimon Schubert
1055796c8dcSSimon Schubert static void
i386fbsd_collect_uthread(const struct regcache * regcache,int regnum,CORE_ADDR addr)1065796c8dcSSimon Schubert i386fbsd_collect_uthread (const struct regcache *regcache,
1075796c8dcSSimon Schubert int regnum, CORE_ADDR addr)
1085796c8dcSSimon Schubert {
109*ef5ccd6cSJohn Marino gdb_byte buf[4];
1105796c8dcSSimon Schubert int i;
1115796c8dcSSimon Schubert
1125796c8dcSSimon Schubert gdb_assert (regnum >= -1);
1135796c8dcSSimon Schubert
1145796c8dcSSimon Schubert for (i = 0; i < ARRAY_SIZE (i386fbsd_jmp_buf_reg_offset); i++)
1155796c8dcSSimon Schubert {
1165796c8dcSSimon Schubert if (i386fbsd_jmp_buf_reg_offset[i] != -1
1175796c8dcSSimon Schubert && (regnum == -1 || regnum == i))
1185796c8dcSSimon Schubert {
1195796c8dcSSimon Schubert regcache_raw_collect (regcache, i, buf);
1205796c8dcSSimon Schubert write_memory (addr + i386fbsd_jmp_buf_reg_offset[i], buf, 4);
1215796c8dcSSimon Schubert }
1225796c8dcSSimon Schubert }
1235796c8dcSSimon Schubert }
1245796c8dcSSimon Schubert
1255796c8dcSSimon Schubert static void
i386fbsdaout_init_abi(struct gdbarch_info info,struct gdbarch * gdbarch)1265796c8dcSSimon Schubert i386fbsdaout_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1275796c8dcSSimon Schubert {
1285796c8dcSSimon Schubert struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1295796c8dcSSimon Schubert
1305796c8dcSSimon Schubert /* Obviously FreeBSD is BSD-based. */
1315796c8dcSSimon Schubert i386bsd_init_abi (info, gdbarch);
1325796c8dcSSimon Schubert
1335796c8dcSSimon Schubert /* FreeBSD has a different `struct reg', and reserves some space for
1345796c8dcSSimon Schubert its FPU emulator in `struct fpreg'. */
1355796c8dcSSimon Schubert tdep->gregset_reg_offset = i386fbsd_r_reg_offset;
1365796c8dcSSimon Schubert tdep->gregset_num_regs = ARRAY_SIZE (i386fbsd_r_reg_offset);
1375796c8dcSSimon Schubert tdep->sizeof_gregset = 18 * 4;
1385796c8dcSSimon Schubert tdep->sizeof_fpregset = 176;
1395796c8dcSSimon Schubert
1405796c8dcSSimon Schubert /* FreeBSD uses -freg-struct-return by default. */
1415796c8dcSSimon Schubert tdep->struct_return = reg_struct_return;
1425796c8dcSSimon Schubert
1435796c8dcSSimon Schubert /* FreeBSD uses a different memory layout. */
1445796c8dcSSimon Schubert tdep->sigtramp_start = i386fbsd_sigtramp_start_addr;
1455796c8dcSSimon Schubert tdep->sigtramp_end = i386fbsd_sigtramp_end_addr;
1465796c8dcSSimon Schubert
1475796c8dcSSimon Schubert /* FreeBSD has a more complete `struct sigcontext'. */
1485796c8dcSSimon Schubert tdep->sc_reg_offset = i386fbsd_sc_reg_offset;
1495796c8dcSSimon Schubert tdep->sc_num_regs = ARRAY_SIZE (i386fbsd_sc_reg_offset);
1505796c8dcSSimon Schubert
1515796c8dcSSimon Schubert /* FreeBSD provides a user-level threads implementation. */
1525796c8dcSSimon Schubert bsd_uthread_set_supply_uthread (gdbarch, i386fbsd_supply_uthread);
1535796c8dcSSimon Schubert bsd_uthread_set_collect_uthread (gdbarch, i386fbsd_collect_uthread);
1545796c8dcSSimon Schubert }
1555796c8dcSSimon Schubert
1565796c8dcSSimon Schubert static void
i386fbsd_init_abi(struct gdbarch_info info,struct gdbarch * gdbarch)1575796c8dcSSimon Schubert i386fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1585796c8dcSSimon Schubert {
1595796c8dcSSimon Schubert /* It's almost identical to FreeBSD a.out. */
1605796c8dcSSimon Schubert i386fbsdaout_init_abi (info, gdbarch);
1615796c8dcSSimon Schubert
1625796c8dcSSimon Schubert /* Except that it uses ELF. */
1635796c8dcSSimon Schubert i386_elf_init_abi (info, gdbarch);
1645796c8dcSSimon Schubert
1655796c8dcSSimon Schubert /* FreeBSD ELF uses SVR4-style shared libraries. */
1665796c8dcSSimon Schubert set_solib_svr4_fetch_link_map_offsets
1675796c8dcSSimon Schubert (gdbarch, svr4_ilp32_fetch_link_map_offsets);
1685796c8dcSSimon Schubert }
1695796c8dcSSimon Schubert
1705796c8dcSSimon Schubert /* FreeBSD 4.0-RELEASE or later. */
1715796c8dcSSimon Schubert
1725796c8dcSSimon Schubert /* From <machine/reg.h>. */
1735796c8dcSSimon Schubert static int i386fbsd4_r_reg_offset[] =
1745796c8dcSSimon Schubert {
1755796c8dcSSimon Schubert 10 * 4, 9 * 4, 8 * 4, 7 * 4, /* %eax, %ecx, %edx, %ebx */
1765796c8dcSSimon Schubert 16 * 4, 5 * 4, /* %esp, %ebp */
1775796c8dcSSimon Schubert 4 * 4, 3 * 4, /* %esi, %edi */
1785796c8dcSSimon Schubert 13 * 4, 15 * 4, /* %eip, %eflags */
1795796c8dcSSimon Schubert 14 * 4, 17 * 4, /* %cs, %ss */
1805796c8dcSSimon Schubert 2 * 4, 1 * 4, 0 * 4, 18 * 4 /* %ds, %es, %fs, %gs */
1815796c8dcSSimon Schubert };
1825796c8dcSSimon Schubert
1835796c8dcSSimon Schubert /* From <machine/signal.h>. */
1845796c8dcSSimon Schubert int i386fbsd4_sc_reg_offset[] =
1855796c8dcSSimon Schubert {
1865796c8dcSSimon Schubert 20 + 11 * 4, /* %eax */
1875796c8dcSSimon Schubert 20 + 10 * 4, /* %ecx */
1885796c8dcSSimon Schubert 20 + 9 * 4, /* %edx */
1895796c8dcSSimon Schubert 20 + 8 * 4, /* %ebx */
1905796c8dcSSimon Schubert 20 + 17 * 4, /* %esp */
1915796c8dcSSimon Schubert 20 + 6 * 4, /* %ebp */
1925796c8dcSSimon Schubert 20 + 5 * 4, /* %esi */
1935796c8dcSSimon Schubert 20 + 4 * 4, /* %edi */
1945796c8dcSSimon Schubert 20 + 14 * 4, /* %eip */
1955796c8dcSSimon Schubert 20 + 16 * 4, /* %eflags */
1965796c8dcSSimon Schubert 20 + 15 * 4, /* %cs */
1975796c8dcSSimon Schubert 20 + 18 * 4, /* %ss */
1985796c8dcSSimon Schubert 20 + 3 * 4, /* %ds */
1995796c8dcSSimon Schubert 20 + 2 * 4, /* %es */
2005796c8dcSSimon Schubert 20 + 1 * 4, /* %fs */
2015796c8dcSSimon Schubert 20 + 0 * 4 /* %gs */
2025796c8dcSSimon Schubert };
2035796c8dcSSimon Schubert
20469e0f06dSSimon Schubert void
i386fbsd4_init_abi(struct gdbarch_info info,struct gdbarch * gdbarch)2055796c8dcSSimon Schubert i386fbsd4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2065796c8dcSSimon Schubert {
2075796c8dcSSimon Schubert struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2085796c8dcSSimon Schubert
2095796c8dcSSimon Schubert /* Inherit stuff from older releases. We assume that FreeBSD
2105796c8dcSSimon Schubert 4.0-RELEASE always uses ELF. */
2115796c8dcSSimon Schubert i386fbsd_init_abi (info, gdbarch);
2125796c8dcSSimon Schubert
2135796c8dcSSimon Schubert /* FreeBSD 4.0 introduced a new `struct reg'. */
2145796c8dcSSimon Schubert tdep->gregset_reg_offset = i386fbsd4_r_reg_offset;
2155796c8dcSSimon Schubert tdep->gregset_num_regs = ARRAY_SIZE (i386fbsd4_r_reg_offset);
2165796c8dcSSimon Schubert tdep->sizeof_gregset = 19 * 4;
2175796c8dcSSimon Schubert
2185796c8dcSSimon Schubert /* FreeBSD 4.0 introduced a new `struct sigcontext'. */
2195796c8dcSSimon Schubert tdep->sc_reg_offset = i386fbsd4_sc_reg_offset;
2205796c8dcSSimon Schubert tdep->sc_num_regs = ARRAY_SIZE (i386fbsd4_sc_reg_offset);
2215796c8dcSSimon Schubert }
2225796c8dcSSimon Schubert
2235796c8dcSSimon Schubert
2245796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes. */
2255796c8dcSSimon Schubert void _initialize_i386fbsd_tdep (void);
2265796c8dcSSimon Schubert
2275796c8dcSSimon Schubert void
_initialize_i386fbsd_tdep(void)2285796c8dcSSimon Schubert _initialize_i386fbsd_tdep (void)
2295796c8dcSSimon Schubert {
2305796c8dcSSimon Schubert gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_FREEBSD_AOUT,
2315796c8dcSSimon Schubert i386fbsdaout_init_abi);
2325796c8dcSSimon Schubert gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_FREEBSD_ELF,
2335796c8dcSSimon Schubert i386fbsd4_init_abi);
2345796c8dcSSimon Schubert }
235