6fdf677e710ae80f5359927d665b506951500fcc
[platform/upstream/binutils.git] / gdb / shnbsd-tdep.c
1 /* Target-dependent code for NetBSD/sh.
2
3    Copyright (C) 2002, 2003, 2006, 2007 Free Software Foundation, Inc.
4
5    Contributed by Wasabi Systems, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor,
22    Boston, MA 02110-1301, USA.  */
23
24 #include "defs.h"
25 #include "gdbcore.h"
26 #include "regcache.h"
27 #include "regset.h"
28 #include "value.h"
29 #include "osabi.h"
30
31 #include "gdb_assert.h"
32 #include "gdb_string.h"
33
34 #include "sh-tdep.h"
35 #include "shnbsd-tdep.h"
36 #include "solib-svr4.h"
37
38 /* Convert an r0-r15 register number into an offset into a ptrace
39    register structure.  */
40 static const int regmap[] =
41 {
42   (20 * 4),     /* r0 */
43   (19 * 4),     /* r1 */
44   (18 * 4),     /* r2 */ 
45   (17 * 4),     /* r3 */ 
46   (16 * 4),     /* r4 */
47   (15 * 4),     /* r5 */
48   (14 * 4),     /* r6 */
49   (13 * 4),     /* r7 */
50   (12 * 4),     /* r8 */ 
51   (11 * 4),     /* r9 */
52   (10 * 4),     /* r10 */
53   ( 9 * 4),     /* r11 */
54   ( 8 * 4),     /* r12 */
55   ( 7 * 4),     /* r13 */
56   ( 6 * 4),     /* r14 */
57   ( 5 * 4),     /* r15 */
58 };
59
60 /* Sizeof `struct reg' in <machine/reg.h>.  */
61 #define SHNBSD_SIZEOF_GREGS     (21 * 4)
62
63 /* Supply register REGNUM from the buffer specified by GREGS and LEN
64    in the general-purpose register set REGSET to register cache
65    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
66
67 static void
68 shnbsd_supply_gregset (const struct regset *regset,
69                        struct regcache *regcache,
70                        int regnum, const void *gregs, size_t len)
71 {
72   const gdb_byte *regs = gregs;
73   int i;
74
75   gdb_assert (len >= SHNBSD_SIZEOF_GREGS);
76
77   if (regnum == gdbarch_pc_regnum (current_gdbarch) || regnum == -1)
78     regcache_raw_supply (regcache,
79                          gdbarch_pc_regnum (current_gdbarch),
80                          regs + (0 * 4));
81
82   if (regnum == SR_REGNUM || regnum == -1)
83     regcache_raw_supply (regcache, SR_REGNUM, regs + (1 * 4));
84
85   if (regnum == PR_REGNUM || regnum == -1)
86     regcache_raw_supply (regcache, PR_REGNUM, regs + (2 * 4));
87
88   if (regnum == MACH_REGNUM || regnum == -1)
89     regcache_raw_supply (regcache, MACH_REGNUM, regs + (3 * 4));
90
91   if (regnum == MACL_REGNUM || regnum == -1)
92     regcache_raw_supply (regcache, MACL_REGNUM, regs + (4 * 4));
93
94   for (i = R0_REGNUM; i <= (R0_REGNUM + 15); i++)
95     {
96       if (regnum == i || regnum == -1)
97         regcache_raw_supply (regcache, i, regs + regmap[i - R0_REGNUM]);
98     }
99 }
100
101 /* Collect register REGNUM in the general-purpose register set
102    REGSET. from register cache REGCACHE into the buffer specified by
103    GREGS and LEN.  If REGNUM is -1, do this for all registers in
104    REGSET.  */
105
106 static void
107 shnbsd_collect_gregset (const struct regset *regset,
108                         const struct regcache *regcache,
109                         int regnum, void *gregs, size_t len)
110 {
111   gdb_byte *regs = gregs;
112   int i;
113
114   gdb_assert (len >= SHNBSD_SIZEOF_GREGS);
115
116   if (regnum == gdbarch_pc_regnum (current_gdbarch) || regnum == -1)
117     regcache_raw_collect (regcache, gdbarch_pc_regnum (current_gdbarch),
118                           regs + (0 * 4));
119
120   if (regnum == SR_REGNUM || regnum == -1)
121     regcache_raw_collect (regcache, SR_REGNUM, regs + (1 * 4));
122
123   if (regnum == PR_REGNUM || regnum == -1)
124     regcache_raw_collect (regcache, PR_REGNUM, regs + (2 * 4));
125
126   if (regnum == MACH_REGNUM || regnum == -1)
127     regcache_raw_collect (regcache, MACH_REGNUM, regs + (3 * 4));
128
129   if (regnum == MACL_REGNUM || regnum == -1)
130     regcache_raw_collect (regcache, MACL_REGNUM, regs + (4 * 4));
131
132   for (i = R0_REGNUM; i <= (R0_REGNUM + 15); i++)
133     {
134       if (regnum == i || regnum == -1)
135         regcache_raw_collect (regcache, i, regs + regmap[i - R0_REGNUM]);
136     }
137 }
138
139 /* SH register sets.  */
140
141 static struct regset shnbsd_gregset =
142 {
143   NULL,
144   shnbsd_supply_gregset,
145   shnbsd_collect_gregset
146 };
147
148 /* Return the appropriate register set for the core section identified
149    by SECT_NAME and SECT_SIZE.  */
150
151 const struct regset *
152 shnbsd_regset_from_core_section (struct gdbarch *gdbarch,
153                                  const char *sect_name, size_t sect_size)
154 {
155   if (strcmp (sect_name, ".reg") == 0 && sect_size >= SHNBSD_SIZEOF_GREGS)
156     return &shnbsd_gregset;
157
158   return NULL;
159 }
160
161 void
162 shnbsd_supply_reg (struct regcache *regcache, const char *regs, int regnum)
163 {
164   shnbsd_supply_gregset (&shnbsd_gregset, regcache, regnum,
165                          regs, SHNBSD_SIZEOF_GREGS);
166 }
167
168 void
169 shnbsd_fill_reg (const struct regcache *regcache, char *regs, int regnum)
170 {
171   shnbsd_collect_gregset (&shnbsd_gregset, regcache, regnum,
172                           regs, SHNBSD_SIZEOF_GREGS);
173 }
174 \f
175
176 static void
177 shnbsd_init_abi (struct gdbarch_info info,
178                   struct gdbarch *gdbarch)
179 {
180   set_gdbarch_regset_from_core_section
181     (gdbarch, shnbsd_regset_from_core_section);
182
183   set_solib_svr4_fetch_link_map_offsets
184     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
185 }
186 \f
187
188 /* OpenBSD uses uses the traditional NetBSD core file format, even for
189    ports that use ELF.  */
190 #define GDB_OSABI_NETBSD_CORE GDB_OSABI_OPENBSD_ELF
191
192 static enum gdb_osabi
193 shnbsd_core_osabi_sniffer (bfd *abfd)
194 {
195   if (strcmp (bfd_get_target (abfd), "netbsd-core") == 0)
196     return GDB_OSABI_NETBSD_CORE;
197
198   return GDB_OSABI_UNKNOWN;
199 }
200
201 void
202 _initialize_shnbsd_tdep (void)
203 {
204   /* BFD doesn't set a flavour for NetBSD style a.out core files.  */
205   gdbarch_register_osabi_sniffer (bfd_arch_sh, bfd_target_unknown_flavour,
206                                   shnbsd_core_osabi_sniffer);
207
208   gdbarch_register_osabi (bfd_arch_sh, 0, GDB_OSABI_NETBSD_ELF,
209                           shnbsd_init_abi);
210   gdbarch_register_osabi (bfd_arch_sh, 0, GDB_OSABI_OPENBSD_ELF,
211                           shnbsd_init_abi);
212 }