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