* Makefile (m68klinux-nat.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 "m68k-tdep.h"
30 \f
31 /* Check whether insn1 and insn2 are parts of a signal trampoline.  */
32
33 #define IS_SIGTRAMP(insn1, insn2)                                       \
34   (/* addaw #20,sp; moveq #119,d0; trap #0 */                           \
35    (insn1 == 0xdefc0014 && insn2 == 0x70774e40)                         \
36    /* moveq #119,d0; trap #0 */                                         \
37    || insn1 == 0x70774e40)
38
39 #define IS_RT_SIGTRAMP(insn1, insn2)                                    \
40   (/* movel #173,d0; trap #0 */                                         \
41    (insn1 == 0x203c0000 && insn2 == 0x00ad4e40)                         \
42    /* moveq #82,d0; notb d0; trap #0 */                                 \
43    || (insn1 == 0x70524600 && (insn2 >> 16) == 0x4e40))
44
45 /* Return non-zero if PC points into the signal trampoline.  For the sake
46    of m68k_linux_frame_saved_pc we also distinguish between non-RT and RT
47    signal trampolines.  */
48
49 int
50 m68k_linux_in_sigtramp (CORE_ADDR pc)
51 {
52   CORE_ADDR sp;
53   char buf[12];
54   unsigned long insn0, insn1, insn2;
55
56   if (read_memory_nobpt (pc - 4, buf, sizeof (buf)))
57     return 0;
58   insn1 = extract_unsigned_integer (buf + 4, 4);
59   insn2 = extract_unsigned_integer (buf + 8, 4);
60   if (IS_SIGTRAMP (insn1, insn2))
61     return 1;
62   if (IS_RT_SIGTRAMP (insn1, insn2))
63     return 2;
64
65   insn0 = extract_unsigned_integer (buf, 4);
66   if (IS_SIGTRAMP (insn0, insn1))
67     return 1;
68   if (IS_RT_SIGTRAMP (insn0, insn1))
69     return 2;
70
71   insn0 = (insn0 << 16) | (insn1 >> 16);
72   insn1 = (insn1 << 16) | (insn2 >> 16);
73   if (IS_SIGTRAMP (insn0, insn1))
74     return 1;
75   if (IS_RT_SIGTRAMP (insn0, insn1))
76     return 2;
77
78   return 0;
79 }
80
81 /* Offset to saved PC in sigcontext, from <asm/sigcontext.h>.  */
82 #define SIGCONTEXT_PC_OFFSET 26
83
84 /* Offset to saved PC in ucontext, from <asm/ucontext.h>.  */
85 #define UCONTEXT_PC_OFFSET 88
86
87 /* Get saved user PC for sigtramp from sigcontext or ucontext.  */
88
89 static CORE_ADDR
90 m68k_linux_sigtramp_saved_pc (struct frame_info *frame)
91 {
92   CORE_ADDR sigcontext_addr;
93   char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
94   int ptrbytes = TARGET_PTR_BIT / TARGET_CHAR_BIT;
95   int sigcontext_offs = (2 * TARGET_INT_BIT) / TARGET_CHAR_BIT;
96
97   /* Get sigcontext address, it is the third parameter on the stack.  */
98   if (get_next_frame (frame))
99     sigcontext_addr
100       = read_memory_unsigned_integer (get_frame_base (get_next_frame (frame))
101                                       + FRAME_ARGS_SKIP
102                                       + sigcontext_offs,
103                                       ptrbytes);
104   else
105     sigcontext_addr
106       = read_memory_unsigned_integer (read_register (SP_REGNUM)
107                                       + sigcontext_offs,
108                                       ptrbytes);
109
110   /* Don't cause a memory_error when accessing sigcontext in case the
111      stack layout has changed or the stack is corrupt.  */
112   if (m68k_linux_in_sigtramp (get_frame_pc (frame)) == 2)
113     target_read_memory (sigcontext_addr + UCONTEXT_PC_OFFSET, buf, ptrbytes);
114   else
115     target_read_memory (sigcontext_addr + SIGCONTEXT_PC_OFFSET, buf, ptrbytes);
116   return extract_unsigned_integer (buf, ptrbytes);
117 }
118
119 /* Return the saved program counter for FRAME.  */
120
121 CORE_ADDR
122 m68k_linux_frame_saved_pc (struct frame_info *frame)
123 {
124   if (get_frame_type (frame) == SIGTRAMP_FRAME)
125     return m68k_linux_sigtramp_saved_pc (frame);
126
127   return read_memory_unsigned_integer (get_frame_base (frame) + 4, 4);
128 }
129
130 void
131 m68k_linux_extract_return_value (struct type *type, char *regbuf, char *valbuf)
132 {
133   if (TYPE_CODE (type) == TYPE_CODE_FLT)
134     {
135        REGISTER_CONVERT_TO_VIRTUAL (FP0_REGNUM, type,
136                                     regbuf + REGISTER_BYTE (FP0_REGNUM),
137                                     valbuf);
138     }
139   else if (TYPE_CODE (type) == TYPE_CODE_PTR)
140     memcpy (valbuf, regbuf + REGISTER_BYTE (M68K_A0_REGNUM),
141             TYPE_LENGTH (type));
142   else
143     memcpy (valbuf,
144             regbuf + (TYPE_LENGTH (type) >= 4 ? 0 : 4 - TYPE_LENGTH (type)),
145             TYPE_LENGTH (type));
146 }
147
148 void
149 m68k_linux_store_return_value (struct type *type, char *valbuf)
150 {
151   if (TYPE_CODE (type) == TYPE_CODE_FLT)
152     {
153       char raw_buffer[REGISTER_RAW_SIZE (FP0_REGNUM)];
154       REGISTER_CONVERT_TO_RAW (type, FP0_REGNUM, valbuf, raw_buffer);
155       deprecated_write_register_bytes (REGISTER_BYTE (FP0_REGNUM),
156                                        raw_buffer, TYPE_LENGTH (type));
157     }
158   else
159     {
160       if (TYPE_CODE (type) == TYPE_CODE_PTR)
161         deprecated_write_register_bytes (REGISTER_BYTE (M68K_A0_REGNUM),
162                                          valbuf, TYPE_LENGTH (type));
163       deprecated_write_register_bytes (0, valbuf, TYPE_LENGTH (type));
164     }
165 }
166
167 CORE_ADDR
168 m68k_linux_extract_struct_value_address (char *regbuf)
169 {
170   return *(CORE_ADDR *) (regbuf + REGISTER_BYTE (M68K_A0_REGNUM));
171 }