1130803Smarcel/* Target-dependent code for NetBSD/amd64.
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 "frame.h"
25130803Smarcel#include "gdbcore.h"
26130803Smarcel#include "osabi.h"
27130803Smarcel
28130803Smarcel#include "gdb_assert.h"
29130803Smarcel
30130803Smarcel#include "amd64-tdep.h"
31130803Smarcel#include "nbsd-tdep.h"
32130803Smarcel#include "solib-svr4.h"
33130803Smarcel
34130803Smarcel/* Support for signal handlers.  */
35130803Smarcel
36130803Smarcel/* Assuming NEXT_FRAME is for a frame following a BSD sigtramp
37130803Smarcel   routine, return the address of the associated sigcontext structure.  */
38130803Smarcel
39130803Smarcelstatic CORE_ADDR
40130803Smarcelamd64nbsd_sigcontext_addr (struct frame_info *next_frame)
41130803Smarcel{
42130803Smarcel  CORE_ADDR sp;
43130803Smarcel
44130803Smarcel  /* The stack pointer points at `struct sigcontext' upon entry of a
45130803Smarcel     signal trampoline.  */
46130803Smarcel  sp = frame_unwind_register_unsigned (next_frame, AMD64_RSP_REGNUM);
47130803Smarcel  return sp;
48130803Smarcel}
49130803Smarcel
50130803Smarcel/* NetBSD 2.0 or later.  */
51130803Smarcel
52130803Smarcel/* Mapping between the general-purpose registers in `struct reg'
53130803Smarcel   format and GDB's register cache layout.  */
54130803Smarcel
55130803Smarcel/* From <machine/reg.h>.  */
56130803Smarcelint amd64nbsd_r_reg_offset[] =
57130803Smarcel{
58130803Smarcel  14 * 8,			/* %rax */
59130803Smarcel  13 * 8,			/* %rbx */
60130803Smarcel  3 * 8,			/* %rcx */
61130803Smarcel  2 * 8,			/* %rdx */
62130803Smarcel  1 * 8,			/* %rsi */
63130803Smarcel  0 * 8,			/* %rdi */
64130803Smarcel  12 * 8,			/* %rbp */
65130803Smarcel  24 * 8,			/* %rsp */
66130803Smarcel  4 * 8,			/* %r8 .. */
67130803Smarcel  5 * 8,
68130803Smarcel  6 * 8,
69130803Smarcel  7 * 8,
70130803Smarcel  8 * 8,
71130803Smarcel  9 * 8,
72130803Smarcel  10 * 8,
73130803Smarcel  11 * 8,			/* ... %r15 */
74130803Smarcel  21 * 8,			/* %rip */
75130803Smarcel  23 * 8,			/* %eflags */
76130803Smarcel  22 * 8,			/* %cs */
77130803Smarcel  25 * 8,			/* %ss */
78130803Smarcel  18 * 8,			/* %ds */
79130803Smarcel  17 * 8,			/* %es */
80130803Smarcel  16 * 8,			/* %fs */
81130803Smarcel  15 * 8			/* %gs */
82130803Smarcel};
83130803Smarcel
84130803Smarcelstatic void
85130803Smarcelamd64nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
86130803Smarcel{
87130803Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
88130803Smarcel  int *sc_reg_offset;
89130803Smarcel  int i;
90130803Smarcel
91130803Smarcel  /* Initialize general-purpose register set details first.  */
92130803Smarcel  tdep->gregset_reg_offset = amd64nbsd_r_reg_offset;
93130803Smarcel  tdep->gregset_num_regs = ARRAY_SIZE (amd64nbsd_r_reg_offset);
94130803Smarcel  tdep->sizeof_gregset = 26 * 8;
95130803Smarcel
96130803Smarcel  amd64_init_abi (info, gdbarch);
97130803Smarcel
98130803Smarcel  tdep->jb_pc_offset = 7 * 8;
99130803Smarcel
100130803Smarcel  /* NetBSD has its own convention for signal trampolines.  */
101130803Smarcel  set_gdbarch_pc_in_sigtramp (gdbarch, nbsd_pc_in_sigtramp);
102130803Smarcel  tdep->sigcontext_addr = amd64nbsd_sigcontext_addr;
103130803Smarcel
104130803Smarcel  /* Initialize the array with register offsets in `struct
105130803Smarcel     sigcontext'.  This `struct sigcontext' has an sc_mcontext member
106130803Smarcel     at offset 32, and in <machine/reg.h> we have an explicit comment
107130803Smarcel     saying that `struct reg' is the same as mcontext.__gregs.  */
108130803Smarcel  tdep->sc_num_regs = ARRAY_SIZE (amd64nbsd_r_reg_offset);
109130803Smarcel  tdep->sc_reg_offset = XCALLOC (tdep->sc_num_regs, int);
110130803Smarcel  for (i = 0; i < tdep->sc_num_regs; i++)
111130803Smarcel    {
112130803Smarcel      if (amd64nbsd_r_reg_offset[i] < 0)
113130803Smarcel	tdep->sc_reg_offset[i] = -1;
114130803Smarcel      else
115130803Smarcel	tdep->sc_reg_offset[i] = 32 + amd64nbsd_r_reg_offset[i];
116130803Smarcel    }
117130803Smarcel
118130803Smarcel  /* NetBSD uses SVR4-style shared libraries.  */
119130803Smarcel  set_solib_svr4_fetch_link_map_offsets
120130803Smarcel    (gdbarch, svr4_lp64_fetch_link_map_offsets);
121130803Smarcel}
122130803Smarcel
123130803Smarcel
124130803Smarcel/* Provide a prototype to silence -Wmissing-prototypes.  */
125130803Smarcelvoid _initialize_amd64nbsd_tdep (void);
126130803Smarcel
127130803Smarcelvoid
128130803Smarcel_initialize_amd64nbsd_ndep (void)
129130803Smarcel{
130130803Smarcel  /* The NetBSD/amd64 native dependent code makes this assumption.  */
131130803Smarcel  gdb_assert (ARRAY_SIZE (amd64nbsd_r_reg_offset) == AMD64_NUM_GREGS);
132130803Smarcel
133130803Smarcel  gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
134130803Smarcel			  GDB_OSABI_NETBSD_ELF, amd64nbsd_init_abi);
135130803Smarcel}
136