* m68k-tdep.h (enum m68k_flavour): New.
[platform/upstream/binutils.git] / gdb / m68kbsd-tdep.c
1 /* Target-dependent code for Motorola 68000 BSD's.
2
3    Copyright (C) 2004, 2005, 2007 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., 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301, USA.  */
21
22 #include "defs.h"
23 #include "arch-utils.h"
24 #include "frame.h"
25 #include "osabi.h"
26 #include "regcache.h"
27 #include "regset.h"
28 #include "trad-frame.h"
29 #include "tramp-frame.h"
30
31 #include "gdb_assert.h"
32 #include "gdb_string.h"
33
34 #include "m68k-tdep.h"
35 #include "solib-svr4.h"
36
37 /* Core file support.  */
38
39 /* Sizeof `struct reg' in <machine/reg.h>.  */
40 #define M68KBSD_SIZEOF_GREGS    (18 * 4)
41
42 /* Sizeof `struct fpreg' in <machine/reg.h.  */
43 #define M68KBSD_SIZEOF_FPREGS   (((8 * 3) + 3) * 4)
44
45 int
46 m68kbsd_fpreg_offset (int regnum)
47 {
48   int fp_len = TYPE_LENGTH (gdbarch_register_type (current_gdbarch, regnum));
49   
50   if (regnum >= M68K_FPC_REGNUM)
51     return 8 * fp_len + (regnum - M68K_FPC_REGNUM) * 4;
52
53   return (regnum - M68K_FP0_REGNUM) * fp_len;
54 }
55
56 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
57    in the floating-point register set REGSET to register cache
58    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
59
60 static void
61 m68kbsd_supply_fpregset (const struct regset *regset,
62                          struct regcache *regcache,
63                          int regnum, const void *fpregs, size_t len)
64 {
65   const gdb_byte *regs = fpregs;
66   int i;
67
68   gdb_assert (len >= M68KBSD_SIZEOF_FPREGS);
69
70   for (i = M68K_FP0_REGNUM; i <= M68K_PC_REGNUM; i++)
71     {
72       if (regnum == i || regnum == -1)
73         regcache_raw_supply (regcache, i, regs + m68kbsd_fpreg_offset (i));
74     }
75 }
76
77 /* Supply register REGNUM from the buffer specified by GREGS and LEN
78    in the general-purpose register set REGSET to register cache
79    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
80
81 static void
82 m68kbsd_supply_gregset (const struct regset *regset,
83                         struct regcache *regcache,
84                         int regnum, const void *gregs, size_t len)
85 {
86   const gdb_byte *regs = gregs;
87   int i;
88
89   gdb_assert (len >= M68KBSD_SIZEOF_GREGS);
90
91   for (i = M68K_D0_REGNUM; i <= M68K_PC_REGNUM; i++)
92     {
93       if (regnum == i || regnum == -1)
94         regcache_raw_supply (regcache, i, regs + i * 4);
95     }
96
97   if (len >= M68KBSD_SIZEOF_GREGS + M68KBSD_SIZEOF_FPREGS)
98     {
99       regs += M68KBSD_SIZEOF_GREGS;
100       len -= M68KBSD_SIZEOF_GREGS;
101       m68kbsd_supply_fpregset (regset, regcache, regnum, regs, len);
102     }
103 }
104
105 /* Motorola 68000 register sets.  */
106
107 static struct regset m68kbsd_gregset =
108 {
109   NULL,
110   m68kbsd_supply_gregset
111 };
112
113 static struct regset m68kbsd_fpregset =
114 {
115   NULL,
116   m68kbsd_supply_fpregset
117 };
118
119 /* Return the appropriate register set for the core section identified
120    by SECT_NAME and SECT_SIZE.  */
121
122 static const struct regset *
123 m68kbsd_regset_from_core_section (struct gdbarch *gdbarch,
124                                   const char *sect_name, size_t sect_size)
125 {
126   if (strcmp (sect_name, ".reg") == 0 && sect_size >= M68KBSD_SIZEOF_GREGS)
127     return &m68kbsd_gregset;
128
129   if (strcmp (sect_name, ".reg2") == 0 && sect_size >= M68KBSD_SIZEOF_FPREGS)
130     return &m68kbsd_fpregset;
131
132   return NULL;
133 }
134 \f
135
136 /* Signal trampolines.  */
137
138 static void
139 m68kobsd_sigtramp_cache_init (const struct tramp_frame *self,
140                               struct frame_info *next_frame,
141                               struct trad_frame_cache *this_cache,
142                               CORE_ADDR func)
143 {
144   CORE_ADDR addr, base, pc;
145   int regnum;
146
147   base = frame_unwind_register_unsigned (next_frame, M68K_SP_REGNUM);
148
149   /* The 'addql #4,%sp' instruction at offset 8 adjusts the stack
150      pointer.  Adjust the frame base accordingly.  */
151   pc = frame_unwind_register_unsigned (next_frame, M68K_PC_REGNUM);
152   if ((pc - func) > 8)
153     base -= 4;
154
155   /* Get frame pointer, stack pointer, program counter and processor
156      state from `struct sigcontext'.  */
157   addr = get_frame_memory_unsigned (next_frame, base + 8, 4);
158   trad_frame_set_reg_addr (this_cache, M68K_FP_REGNUM, addr + 8);
159   trad_frame_set_reg_addr (this_cache, M68K_SP_REGNUM, addr + 12);
160   trad_frame_set_reg_addr (this_cache, M68K_PC_REGNUM, addr + 20);
161   trad_frame_set_reg_addr (this_cache, M68K_PS_REGNUM, addr + 24);
162
163   /* The sc_ap member of `struct sigcontext' points to additional
164      hardware state.  Here we find the missing registers.  */
165   addr = get_frame_memory_unsigned (next_frame, addr + 16, 4) + 4;
166   for (regnum = M68K_D0_REGNUM; regnum < M68K_FP_REGNUM; regnum++, addr += 4)
167     trad_frame_set_reg_addr (this_cache, regnum, addr);
168
169   /* Construct the frame ID using the function start.  */
170   trad_frame_set_id (this_cache, frame_id_build (base, func));
171 }
172
173 static const struct tramp_frame m68kobsd_sigtramp = {
174   SIGTRAMP_FRAME,
175   2,
176   {
177     { 0x206f, -1 }, { 0x000c, -1},      /* moveal %sp@(12),%a0 */
178     { 0x4e90, -1 },                     /* jsr %a0@ */
179     { 0x588f, -1 },                     /* addql #4,%sp */
180     { 0x4e41, -1 },                     /* trap #1 */
181     { 0x2f40, -1 }, { 0x0004, -1 },     /* moveal %d0,%sp@(4) */
182     { 0x7001, -1 },                     /* moveq #SYS_exit,%d0 */
183     { 0x4e40, -1 },                     /* trap #0 */
184     { TRAMP_SENTINEL_INSN, -1 }
185   },
186   m68kobsd_sigtramp_cache_init
187 };
188 \f
189
190 static void
191 m68kbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
192 {
193   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
194
195   tdep->jb_pc = 5;
196   tdep->jb_elt_size = 4;
197
198   set_gdbarch_decr_pc_after_break (gdbarch, 2);
199
200   set_gdbarch_regset_from_core_section
201     (gdbarch, m68kbsd_regset_from_core_section);
202 }
203
204 /* OpenBSD and NetBSD a.out.  */
205
206 static void
207 m68kbsd_aout_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
208 {
209   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
210
211   m68kbsd_init_abi (info, gdbarch);
212
213   tdep->struct_return = reg_struct_return;
214
215   tramp_frame_prepend_unwinder (gdbarch, &m68kobsd_sigtramp);
216 }
217
218 /* NetBSD ELF.  */
219
220 static void
221 m68kbsd_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
222 {
223   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
224
225   m68kbsd_init_abi (info, gdbarch);
226
227   /* NetBSD ELF uses the SVR4 ABI.  */
228   m68k_svr4_init_abi (info, gdbarch);
229   tdep->struct_return = pcc_struct_return;
230
231   /* NetBSD ELF uses SVR4-style shared libraries.  */
232   set_solib_svr4_fetch_link_map_offsets
233     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
234 }
235 \f
236
237 static enum gdb_osabi
238 m68kbsd_aout_osabi_sniffer (bfd *abfd)
239 {
240   if (strcmp (bfd_get_target (abfd), "a.out-m68k-netbsd") == 0
241       || strcmp (bfd_get_target (abfd), "a.out-m68k4k-netbsd") == 0)
242     return GDB_OSABI_NETBSD_AOUT;
243
244   return GDB_OSABI_UNKNOWN;
245 }
246
247 static enum gdb_osabi
248 m68kbsd_core_osabi_sniffer (bfd *abfd)
249 {
250   if (strcmp (bfd_get_target (abfd), "netbsd-core") == 0)
251     return GDB_OSABI_NETBSD_AOUT;
252
253   return GDB_OSABI_UNKNOWN;
254 }
255 \f
256
257 /* Provide a prototype to silence -Wmissing-prototypes.  */
258 void _initialize_m68kbsd_tdep (void);
259
260 void
261 _initialize_m68kbsd_tdep (void)
262 {
263   gdbarch_register_osabi_sniffer (bfd_arch_m68k, bfd_target_aout_flavour,
264                                   m68kbsd_aout_osabi_sniffer);
265
266   /* BFD doesn't set a flavour for NetBSD style a.out core files.  */
267   gdbarch_register_osabi_sniffer (bfd_arch_m68k, bfd_target_unknown_flavour,
268                                   m68kbsd_core_osabi_sniffer);
269
270   gdbarch_register_osabi (bfd_arch_m68k, 0, GDB_OSABI_NETBSD_AOUT,
271                           m68kbsd_aout_init_abi);
272   gdbarch_register_osabi (bfd_arch_m68k, 0, GDB_OSABI_NETBSD_ELF,
273                           m68kbsd_elf_init_abi);
274 }