* Makefile.in (m68k-tdep.o, m68klinux-tdep.o): Update
[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 "frame.h"
26 #include "target.h"
27 #include "gdb_string.h"
28 #include "gdbtypes.h"
29 #include "osabi.h"
30 #include "m68k-tdep.h"
31 \f
32 /* Check whether insn1 and insn2 are parts of a signal trampoline.  */
33
34 #define IS_SIGTRAMP(insn1, insn2)                                       \
35   (/* addaw #20,sp; moveq #119,d0; trap #0 */                           \
36    (insn1 == 0xdefc0014 && insn2 == 0x70774e40)                         \
37    /* moveq #119,d0; trap #0 */                                         \
38    || insn1 == 0x70774e40)
39
40 #define IS_RT_SIGTRAMP(insn1, insn2)                                    \
41   (/* movel #173,d0; trap #0 */                                         \
42    (insn1 == 0x203c0000 && insn2 == 0x00ad4e40)                         \
43    /* moveq #82,d0; notb d0; trap #0 */                                 \
44    || (insn1 == 0x70524600 && (insn2 >> 16) == 0x4e40))
45
46 /* Return non-zero if PC points into the signal trampoline.  For the sake
47    of m68k_linux_frame_saved_pc we also distinguish between non-RT and RT
48    signal trampolines.  */
49
50 int
51 m68k_linux_in_sigtramp (CORE_ADDR pc)
52 {
53   CORE_ADDR sp;
54   char buf[12];
55   unsigned long insn0, insn1, insn2;
56
57   if (read_memory_nobpt (pc - 4, buf, sizeof (buf)))
58     return 0;
59   insn1 = extract_unsigned_integer (buf + 4, 4);
60   insn2 = extract_unsigned_integer (buf + 8, 4);
61   if (IS_SIGTRAMP (insn1, insn2))
62     return 1;
63   if (IS_RT_SIGTRAMP (insn1, insn2))
64     return 2;
65
66   insn0 = extract_unsigned_integer (buf, 4);
67   if (IS_SIGTRAMP (insn0, insn1))
68     return 1;
69   if (IS_RT_SIGTRAMP (insn0, insn1))
70     return 2;
71
72   insn0 = ((insn0 << 16) & 0xffffffff) | (insn1 >> 16);
73   insn1 = ((insn1 << 16) & 0xffffffff) | (insn2 >> 16);
74   if (IS_SIGTRAMP (insn0, insn1))
75     return 1;
76   if (IS_RT_SIGTRAMP (insn0, insn1))
77     return 2;
78
79   return 0;
80 }
81
82 /* Offset to saved PC in sigcontext, from <asm/sigcontext.h>.  */
83 #define SIGCONTEXT_PC_OFFSET 26
84
85 /* Offset to saved PC in ucontext, from <asm/ucontext.h>.  */
86 #define UCONTEXT_PC_OFFSET 88
87
88 /* Get saved user PC for sigtramp from sigcontext or ucontext.  */
89
90 static CORE_ADDR
91 m68k_linux_sigtramp_saved_pc (struct frame_info *frame)
92 {
93   CORE_ADDR sigcontext_addr;
94   char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
95   int ptrbytes = TARGET_PTR_BIT / TARGET_CHAR_BIT;
96   int sigcontext_offs = (2 * TARGET_INT_BIT) / TARGET_CHAR_BIT;
97
98   /* Get sigcontext address, it is the third parameter on the stack.  */
99   if (get_next_frame (frame))
100     sigcontext_addr
101       = read_memory_unsigned_integer (get_frame_base (get_next_frame (frame))
102                                       + FRAME_ARGS_SKIP
103                                       + sigcontext_offs,
104                                       ptrbytes);
105   else
106     sigcontext_addr
107       = read_memory_unsigned_integer (read_register (SP_REGNUM)
108                                       + sigcontext_offs,
109                                       ptrbytes);
110
111   /* Don't cause a memory_error when accessing sigcontext in case the
112      stack layout has changed or the stack is corrupt.  */
113   if (m68k_linux_in_sigtramp (get_frame_pc (frame)) == 2)
114     target_read_memory (sigcontext_addr + UCONTEXT_PC_OFFSET, buf, ptrbytes);
115   else
116     target_read_memory (sigcontext_addr + SIGCONTEXT_PC_OFFSET, buf, ptrbytes);
117   return extract_unsigned_integer (buf, ptrbytes);
118 }
119
120 /* Return the saved program counter for FRAME.  */
121
122 static CORE_ADDR
123 m68k_linux_frame_saved_pc (struct frame_info *frame)
124 {
125   if (get_frame_type (frame) == SIGTRAMP_FRAME)
126     return m68k_linux_sigtramp_saved_pc (frame);
127
128   return read_memory_unsigned_integer (get_frame_base (frame) + 4, 4);
129 }
130
131 /* The following definitions are appropriate when using the ELF
132    format, where floating point values are returned in fp0, pointer
133    values in a0 and other values in d0.  */
134
135 /* Extract from an array REGBUF containing the (raw) register state a
136    function return value of type TYPE, and copy that, in virtual
137    format, into VALBUF.  */
138
139 static void
140 m68k_linux_extract_return_value (struct type *type, char *regbuf, char *valbuf)
141 {
142   if (TYPE_CODE (type) == TYPE_CODE_FLT)
143     {
144        REGISTER_CONVERT_TO_VIRTUAL (FP0_REGNUM, type,
145                                     regbuf + REGISTER_BYTE (FP0_REGNUM),
146                                     valbuf);
147     }
148   else if (TYPE_CODE (type) == TYPE_CODE_PTR)
149     memcpy (valbuf, regbuf + REGISTER_BYTE (M68K_A0_REGNUM),
150             TYPE_LENGTH (type));
151   else
152     memcpy (valbuf,
153             regbuf + (TYPE_LENGTH (type) >= 4 ? 0 : 4 - TYPE_LENGTH (type)),
154             TYPE_LENGTH (type));
155 }
156
157 /* Write into appropriate registers a function return value of type
158    TYPE, given in virtual format.  */
159
160 static void
161 m68k_linux_store_return_value (struct type *type, char *valbuf)
162 {
163   if (TYPE_CODE (type) == TYPE_CODE_FLT)
164     {
165       char raw_buffer[REGISTER_RAW_SIZE (FP0_REGNUM)];
166       REGISTER_CONVERT_TO_RAW (type, FP0_REGNUM, valbuf, raw_buffer);
167       deprecated_write_register_bytes (REGISTER_BYTE (FP0_REGNUM),
168                                        raw_buffer, TYPE_LENGTH (type));
169     }
170   else
171     {
172       if (TYPE_CODE (type) == TYPE_CODE_PTR)
173         deprecated_write_register_bytes (REGISTER_BYTE (M68K_A0_REGNUM),
174                                          valbuf, TYPE_LENGTH (type));
175       deprecated_write_register_bytes (0, valbuf, TYPE_LENGTH (type));
176     }
177 }
178
179 /* Extract from an array REGBUF containing the (raw) register state
180    the address in which a function should return its structure value,
181    as a CORE_ADDR.  */
182
183 static CORE_ADDR
184 m68k_linux_extract_struct_value_address (char *regbuf)
185 {
186   return *(CORE_ADDR *) (regbuf + REGISTER_BYTE (M68K_A0_REGNUM));
187 }
188
189 static void
190 m68k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
191 {
192   set_gdbarch_deprecated_frame_saved_pc (gdbarch,
193                                          m68k_linux_frame_saved_pc);
194   set_gdbarch_deprecated_extract_return_value (gdbarch,
195                                                m68k_linux_extract_return_value);
196   set_gdbarch_deprecated_store_return_value (gdbarch,
197                                              m68k_linux_store_return_value);
198   set_gdbarch_deprecated_extract_struct_value_address (gdbarch,
199                                                        m68k_linux_extract_struct_value_address);
200 }
201
202 void
203 _initialize_m68k_linux_tdep (void)
204 {
205   gdbarch_register_osabi (bfd_arch_m68k, 0, GDB_OSABI_LINUX,
206                           m68k_linux_init_abi);
207 }