* m68k-tdep.h (enum struct_return): Define.
[external/binutils.git] / gdb / m68klinux-tdep.c
1 /* Motorola m68k target-dependent support for GNU/Linux.
2
3    Copyright 1996, 1998, 2000, 2001, 2002, 2003 Free Software Foundation,
4    Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "gdbcore.h"
25 #include "doublest.h"
26 #include "floatformat.h"
27 #include "frame.h"
28 #include "target.h"
29 #include "gdb_string.h"
30 #include "gdbtypes.h"
31 #include "osabi.h"
32 #include "regcache.h"
33 #include "objfiles.h"
34 #include "symtab.h"
35 #include "m68k-tdep.h"
36 \f
37 /* Offsets (in target ints) into jmp_buf.  */
38
39 #define M68K_LINUX_JB_ELEMENT_SIZE 4
40 #define M68K_LINUX_JB_PC 7
41
42 /* Check whether insn1 and insn2 are parts of a signal trampoline.  */
43
44 #define IS_SIGTRAMP(insn1, insn2)                                       \
45   (/* addaw #20,sp; moveq #119,d0; trap #0 */                           \
46    (insn1 == 0xdefc0014 && insn2 == 0x70774e40)                         \
47    /* moveq #119,d0; trap #0 */                                         \
48    || insn1 == 0x70774e40)
49
50 #define IS_RT_SIGTRAMP(insn1, insn2)                                    \
51   (/* movel #173,d0; trap #0 */                                         \
52    (insn1 == 0x203c0000 && insn2 == 0x00ad4e40)                         \
53    /* moveq #82,d0; notb d0; trap #0 */                                 \
54    || (insn1 == 0x70524600 && (insn2 >> 16) == 0x4e40))
55
56 /* Return non-zero if PC points into the signal trampoline.  For the
57    sake of m68k_linux_get_sigtramp_info we also distinguish between
58    non-RT and RT signal trampolines.  */
59
60 static int
61 m68k_linux_pc_in_sigtramp (CORE_ADDR pc, char *name)
62 {
63   CORE_ADDR sp;
64   char buf[12];
65   unsigned long insn0, insn1, insn2;
66
67   if (read_memory_nobpt (pc - 4, buf, sizeof (buf)))
68     return 0;
69   insn1 = extract_unsigned_integer (buf + 4, 4);
70   insn2 = extract_unsigned_integer (buf + 8, 4);
71   if (IS_SIGTRAMP (insn1, insn2))
72     return 1;
73   if (IS_RT_SIGTRAMP (insn1, insn2))
74     return 2;
75
76   insn0 = extract_unsigned_integer (buf, 4);
77   if (IS_SIGTRAMP (insn0, insn1))
78     return 1;
79   if (IS_RT_SIGTRAMP (insn0, insn1))
80     return 2;
81
82   insn0 = ((insn0 << 16) & 0xffffffff) | (insn1 >> 16);
83   insn1 = ((insn1 << 16) & 0xffffffff) | (insn2 >> 16);
84   if (IS_SIGTRAMP (insn0, insn1))
85     return 1;
86   if (IS_RT_SIGTRAMP (insn0, insn1))
87     return 2;
88
89   return 0;
90 }
91
92 /* From <asm/sigcontext.h>.  */
93 static int m68k_linux_sigcontext_reg_offset[M68K_NUM_REGS] =
94 {
95   2 * 4,                        /* %d0 */
96   3 * 4,                        /* %d1 */
97   -1,                           /* %d2 */
98   -1,                           /* %d3 */
99   -1,                           /* %d4 */
100   -1,                           /* %d5 */
101   -1,                           /* %d6 */
102   -1,                           /* %d7 */
103   4 * 4,                        /* %a0 */
104   5 * 4,                        /* %a1 */
105   -1,                           /* %a2 */
106   -1,                           /* %a3 */
107   -1,                           /* %a4 */
108   -1,                           /* %a5 */
109   -1,                           /* %fp */
110   1 * 4,                        /* %sp */
111   5 * 4 + 2,                    /* %sr */
112   6 * 4 + 2,                    /* %pc */
113   8 * 4,                        /* %fp0 */
114   11 * 4,                       /* %fp1 */
115   -1,                           /* %fp2 */
116   -1,                           /* %fp3 */
117   -1,                           /* %fp4 */
118   -1,                           /* %fp5 */
119   -1,                           /* %fp6 */
120   -1,                           /* %fp7 */
121   14 * 4,                       /* %fpcr */
122   15 * 4,                       /* %fpsr */
123   16 * 4                        /* %fpiaddr */
124 };
125
126 /* From <asm/ucontext.h>.  */
127 static int m68k_linux_ucontext_reg_offset[M68K_NUM_REGS] =
128 {
129   6 * 4,                        /* %d0 */
130   7 * 4,                        /* %d1 */
131   8 * 4,                        /* %d2 */
132   9 * 4,                        /* %d3 */
133   10 * 4,                       /* %d4 */
134   11 * 4,                       /* %d5 */
135   12 * 4,                       /* %d6 */
136   13 * 4,                       /* %d7 */
137   14 * 4,                       /* %a0 */
138   15 * 4,                       /* %a1 */
139   16 * 4,                       /* %a2 */
140   17 * 4,                       /* %a3 */
141   18 * 4,                       /* %a4 */
142   19 * 4,                       /* %a5 */
143   20 * 4,                       /* %fp */
144   21 * 4,                       /* %sp */
145   23 * 4,                       /* %sr */
146   22 * 4,                       /* %pc */
147   27 * 4,                       /* %fp0 */
148   30 * 4,                       /* %fp1 */
149   33 * 4,                       /* %fp2 */
150   36 * 4,                       /* %fp3 */
151   39 * 4,                       /* %fp4 */
152   42 * 4,                       /* %fp5 */
153   45 * 4,                       /* %fp6 */
154   48 * 4,                       /* %fp7 */
155   24 * 4,                       /* %fpcr */
156   25 * 4,                       /* %fpsr */
157   26 * 4                        /* %fpiaddr */
158 };
159
160
161 /* Get info about saved registers in sigtramp.  */
162
163 static struct m68k_sigtramp_info
164 m68k_linux_get_sigtramp_info (struct frame_info *next_frame)
165 {
166   CORE_ADDR sp;
167   char buf[4];
168   struct m68k_sigtramp_info info;
169
170   frame_unwind_register (next_frame, M68K_SP_REGNUM, buf);
171   sp = extract_unsigned_integer (buf, 4);
172
173   /* Get sigcontext address, it is the third parameter on the stack.  */
174   info.sigcontext_addr = read_memory_unsigned_integer (sp + 8, 4);
175
176   if (m68k_linux_pc_in_sigtramp (frame_pc_unwind (next_frame), 0) == 2)
177     info.sc_reg_offset = m68k_linux_ucontext_reg_offset;
178   else
179     info.sc_reg_offset = m68k_linux_sigcontext_reg_offset;
180   return info;
181 }
182
183 /* Extract from an array REGBUF containing the (raw) register state, a
184    function return value of TYPE, and copy that, in virtual format,
185    into VALBUF.  */
186
187 static void
188 m68k_linux_extract_return_value (struct type *type, struct regcache *regcache,
189                                  void *valbuf)
190 {
191   int len = TYPE_LENGTH (type);
192   char buf[M68K_MAX_REGISTER_SIZE];
193
194   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
195       && TYPE_NFIELDS (type) == 1)
196     {
197       m68k_linux_extract_return_value (TYPE_FIELD_TYPE (type, 0), regcache,
198                                        valbuf);
199       return;
200     }
201
202   if (TYPE_CODE (type) == TYPE_CODE_FLT)
203     {
204       regcache_raw_read (regcache, M68K_FP0_REGNUM, buf);
205       convert_typed_floating (buf, builtin_type_m68881_ext, valbuf, type);
206     }
207   else if (TYPE_CODE (type) == TYPE_CODE_PTR)
208     regcache_raw_read (regcache, M68K_A0_REGNUM, valbuf);
209   else
210     {
211       if (len <= 4)
212         {
213           regcache_raw_read (regcache, M68K_D0_REGNUM, buf);
214           memcpy (valbuf, buf + (4 - len), len);
215         }
216       else if (len <= 8)
217         {
218           regcache_raw_read (regcache, M68K_D0_REGNUM, buf);
219           memcpy (valbuf, buf + (8 - len), len - 4);
220           regcache_raw_read (regcache, M68K_D1_REGNUM,
221                              (char *) valbuf + (len - 4));
222         }
223       else
224         internal_error (__FILE__, __LINE__,
225                         "Cannot extract return value of %d bytes long.", len);
226     }
227 }
228
229 /* Write into the appropriate registers a function return value stored
230    in VALBUF of type TYPE, given in virtual format.  */
231
232 static void
233 m68k_linux_store_return_value (struct type *type, struct regcache *regcache,
234                                const void *valbuf)
235 {
236   int len = TYPE_LENGTH (type);
237
238   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
239       && TYPE_NFIELDS (type) == 1)
240     {
241       m68k_linux_store_return_value (TYPE_FIELD_TYPE (type, 0), regcache,
242                                      valbuf);
243       return;
244     }
245
246   if (TYPE_CODE (type) == TYPE_CODE_FLT)
247     {
248       char buf[M68K_MAX_REGISTER_SIZE];
249       convert_typed_floating (valbuf, type, buf, builtin_type_m68881_ext);
250       regcache_raw_write (regcache, M68K_FP0_REGNUM, buf);
251     }
252   else if (TYPE_CODE (type) == TYPE_CODE_PTR)
253     regcache_raw_write (regcache, M68K_A0_REGNUM, valbuf);
254   else
255     {
256       if (len <= 4)
257         regcache_raw_write_part (regcache, M68K_D0_REGNUM,
258                                  4 - len, len, valbuf);
259       else if (len <= 8)
260         {
261           regcache_raw_write_part (regcache, M68K_D1_REGNUM, 8 - len,
262                                    len - 4, valbuf);
263           regcache_raw_write (regcache, M68K_D0_REGNUM,
264                               (char *) valbuf + (len - 4));
265         }
266       else
267         internal_error (__FILE__, __LINE__,
268                         "Cannot store return value of %d bytes long.", len);
269     }
270 }
271
272 /* Extract from an array REGBUF containing the (raw) register state
273    the address in which a function should return its structure value,
274    as a CORE_ADDR.  */
275
276 static CORE_ADDR
277 m68k_linux_extract_struct_value_address (struct regcache *regcache)
278 {
279   char buf[4];
280
281   regcache_cooked_read (regcache, M68K_A0_REGNUM, buf);
282   return extract_unsigned_integer (buf, 4);
283 }
284
285 static void
286 m68k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
287 {
288   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
289
290   tdep->jb_pc = M68K_LINUX_JB_PC;
291   tdep->jb_elt_size = M68K_LINUX_JB_ELEMENT_SIZE;
292   tdep->get_sigtramp_info = m68k_linux_get_sigtramp_info;
293   tdep->struct_return = reg_struct_return;
294
295   set_gdbarch_extract_return_value (gdbarch, m68k_linux_extract_return_value);
296   set_gdbarch_store_return_value (gdbarch, m68k_linux_store_return_value);
297   set_gdbarch_extract_struct_value_address (gdbarch,
298                                             m68k_linux_extract_struct_value_address);
299
300   set_gdbarch_pc_in_sigtramp (gdbarch, m68k_linux_pc_in_sigtramp);
301
302   /* Shared library handling.  */
303   set_gdbarch_in_solib_call_trampoline (gdbarch, in_plt_section);
304   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
305 }
306
307 void
308 _initialize_m68k_linux_tdep (void)
309 {
310   gdbarch_register_osabi (bfd_arch_m68k, 0, GDB_OSABI_LINUX,
311                           m68k_linux_init_abi);
312 }