1 /* Motorola m68k target-dependent support for GNU/Linux.
2 
3    Copyright 1996, 1998, 2000, 2001, 2002, 2003, 2004
4    Free Software Foundation, 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 #include "trad-frame.h"
37 #include "frame-unwind.h"
38 #include "glibc-tdep.h"
39 #include "solib-svr4.h"
40 
41 /* Offsets (in target ints) into jmp_buf.  */
42 
43 #define M68K_LINUX_JB_ELEMENT_SIZE 4
44 #define M68K_LINUX_JB_PC 7
45 
46 /* Check whether insn1 and insn2 are parts of a signal trampoline.  */
47 
48 #define IS_SIGTRAMP(insn1, insn2)					\
49   (/* addaw #20,sp; moveq #119,d0; trap #0 */				\
50    (insn1 == 0xdefc0014 && insn2 == 0x70774e40)				\
51    /* moveq #119,d0; trap #0 */						\
52    || insn1 == 0x70774e40)
53 
54 #define IS_RT_SIGTRAMP(insn1, insn2)					\
55   (/* movel #173,d0; trap #0 */						\
56    (insn1 == 0x203c0000 && insn2 == 0x00ad4e40)				\
57    /* moveq #82,d0; notb d0; trap #0 */					\
58    || (insn1 == 0x70524600 && (insn2 >> 16) == 0x4e40))
59 
60 /* Return non-zero if PC points into the signal trampoline.  For the
61    sake of m68k_linux_get_sigtramp_info we also distinguish between
62    non-RT and RT signal trampolines.  */
63 
64 static int
m68k_linux_pc_in_sigtramp(CORE_ADDR pc,char * name)65 m68k_linux_pc_in_sigtramp (CORE_ADDR pc, char *name)
66 {
67   CORE_ADDR sp;
68   char buf[12];
69   unsigned long insn0, insn1, insn2;
70 
71   if (deprecated_read_memory_nobpt (pc - 4, buf, sizeof (buf)))
72     return 0;
73   insn1 = extract_unsigned_integer (buf + 4, 4);
74   insn2 = extract_unsigned_integer (buf + 8, 4);
75   if (IS_SIGTRAMP (insn1, insn2))
76     return 1;
77   if (IS_RT_SIGTRAMP (insn1, insn2))
78     return 2;
79 
80   insn0 = extract_unsigned_integer (buf, 4);
81   if (IS_SIGTRAMP (insn0, insn1))
82     return 1;
83   if (IS_RT_SIGTRAMP (insn0, insn1))
84     return 2;
85 
86   insn0 = ((insn0 << 16) & 0xffffffff) | (insn1 >> 16);
87   insn1 = ((insn1 << 16) & 0xffffffff) | (insn2 >> 16);
88   if (IS_SIGTRAMP (insn0, insn1))
89     return 1;
90   if (IS_RT_SIGTRAMP (insn0, insn1))
91     return 2;
92 
93   return 0;
94 }
95 
96 /* From <asm/sigcontext.h>.  */
97 static int m68k_linux_sigcontext_reg_offset[M68K_NUM_REGS] =
98 {
99   2 * 4,			/* %d0 */
100   3 * 4,			/* %d1 */
101   -1,				/* %d2 */
102   -1,				/* %d3 */
103   -1,				/* %d4 */
104   -1,				/* %d5 */
105   -1,				/* %d6 */
106   -1,				/* %d7 */
107   4 * 4,			/* %a0 */
108   5 * 4,			/* %a1 */
109   -1,				/* %a2 */
110   -1,				/* %a3 */
111   -1,				/* %a4 */
112   -1,				/* %a5 */
113   -1,				/* %fp */
114   1 * 4,			/* %sp */
115   5 * 4 + 2,			/* %sr */
116   6 * 4 + 2,			/* %pc */
117   8 * 4,			/* %fp0 */
118   11 * 4,			/* %fp1 */
119   -1,				/* %fp2 */
120   -1,				/* %fp3 */
121   -1,				/* %fp4 */
122   -1,				/* %fp5 */
123   -1,				/* %fp6 */
124   -1,				/* %fp7 */
125   14 * 4,			/* %fpcr */
126   15 * 4,			/* %fpsr */
127   16 * 4			/* %fpiaddr */
128 };
129 
130 /* From <asm/ucontext.h>.  */
131 static int m68k_linux_ucontext_reg_offset[M68K_NUM_REGS] =
132 {
133   6 * 4,			/* %d0 */
134   7 * 4,			/* %d1 */
135   8 * 4,			/* %d2 */
136   9 * 4,			/* %d3 */
137   10 * 4,			/* %d4 */
138   11 * 4,			/* %d5 */
139   12 * 4,			/* %d6 */
140   13 * 4,			/* %d7 */
141   14 * 4,			/* %a0 */
142   15 * 4,			/* %a1 */
143   16 * 4,			/* %a2 */
144   17 * 4,			/* %a3 */
145   18 * 4,			/* %a4 */
146   19 * 4,			/* %a5 */
147   20 * 4,			/* %fp */
148   21 * 4,			/* %sp */
149   23 * 4,			/* %sr */
150   22 * 4,			/* %pc */
151   27 * 4,			/* %fp0 */
152   30 * 4,			/* %fp1 */
153   33 * 4,			/* %fp2 */
154   36 * 4,			/* %fp3 */
155   39 * 4,			/* %fp4 */
156   42 * 4,			/* %fp5 */
157   45 * 4,			/* %fp6 */
158   48 * 4,			/* %fp7 */
159   24 * 4,			/* %fpcr */
160   25 * 4,			/* %fpsr */
161   26 * 4			/* %fpiaddr */
162 };
163 
164 
165 /* Get info about saved registers in sigtramp.  */
166 
167 struct m68k_linux_sigtramp_info
168 {
169   /* Address of sigcontext.  */
170   CORE_ADDR sigcontext_addr;
171 
172   /* Offset of registers in `struct sigcontext'.  */
173   int *sc_reg_offset;
174 };
175 
176 static struct m68k_linux_sigtramp_info
m68k_linux_get_sigtramp_info(struct frame_info * next_frame)177 m68k_linux_get_sigtramp_info (struct frame_info *next_frame)
178 {
179   CORE_ADDR sp;
180   char buf[4];
181   struct m68k_linux_sigtramp_info info;
182 
183   frame_unwind_register (next_frame, M68K_SP_REGNUM, buf);
184   sp = extract_unsigned_integer (buf, 4);
185 
186   /* Get sigcontext address, it is the third parameter on the stack.  */
187   info.sigcontext_addr = read_memory_unsigned_integer (sp + 8, 4);
188 
189   if (m68k_linux_pc_in_sigtramp (frame_pc_unwind (next_frame), 0) == 2)
190     info.sc_reg_offset = m68k_linux_ucontext_reg_offset;
191   else
192     info.sc_reg_offset = m68k_linux_sigcontext_reg_offset;
193   return info;
194 }
195 
196 /* Signal trampolines.  */
197 
198 static struct trad_frame_cache *
m68k_linux_sigtramp_frame_cache(struct frame_info * next_frame,void ** this_cache)199 m68k_linux_sigtramp_frame_cache (struct frame_info *next_frame,
200 				 void **this_cache)
201 {
202   struct frame_id this_id;
203   struct trad_frame_cache *cache;
204   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
205   struct m68k_linux_sigtramp_info info;
206   char buf[4];
207   int i;
208 
209   if (*this_cache)
210     return *this_cache;
211 
212   cache = trad_frame_cache_zalloc (next_frame);
213 
214   /* FIXME: cagney/2004-05-01: This is is long standing broken code.
215      The frame ID's code address should be the start-address of the
216      signal trampoline and not the current PC within that
217      trampoline.  */
218   frame_unwind_register (next_frame, M68K_SP_REGNUM, buf);
219   /* See the end of m68k_push_dummy_call.  */
220   this_id = frame_id_build (extract_unsigned_integer (buf, 4) - 4 + 8,
221 			    frame_pc_unwind (next_frame));
222   trad_frame_set_id (cache, this_id);
223 
224   info = m68k_linux_get_sigtramp_info (next_frame);
225 
226   for (i = 0; i < M68K_NUM_REGS; i++)
227     if (info.sc_reg_offset[i] != -1)
228       trad_frame_set_reg_addr (cache, i,
229 			       info.sigcontext_addr + info.sc_reg_offset[i]);
230 
231   *this_cache = cache;
232   return cache;
233 }
234 
235 static void
m68k_linux_sigtramp_frame_this_id(struct frame_info * next_frame,void ** this_cache,struct frame_id * this_id)236 m68k_linux_sigtramp_frame_this_id (struct frame_info *next_frame,
237 				   void **this_cache,
238 				   struct frame_id *this_id)
239 {
240   struct trad_frame_cache *cache =
241     m68k_linux_sigtramp_frame_cache (next_frame, this_cache);
242   trad_frame_get_id (cache, this_id);
243 }
244 
245 static void
m68k_linux_sigtramp_frame_prev_register(struct frame_info * next_frame,void ** this_cache,int regnum,int * optimizedp,enum lval_type * lvalp,CORE_ADDR * addrp,int * realnump,void * valuep)246 m68k_linux_sigtramp_frame_prev_register (struct frame_info *next_frame,
247 					 void **this_cache,
248 					 int regnum, int *optimizedp,
249 					 enum lval_type *lvalp,
250 					 CORE_ADDR *addrp,
251 					 int *realnump, void *valuep)
252 {
253   /* Make sure we've initialized the cache.  */
254   struct trad_frame_cache *cache =
255     m68k_linux_sigtramp_frame_cache (next_frame, this_cache);
256   trad_frame_get_register (cache, next_frame, regnum, optimizedp, lvalp,
257 			   addrp, realnump, valuep);
258 }
259 
260 static const struct frame_unwind m68k_linux_sigtramp_frame_unwind =
261 {
262   SIGTRAMP_FRAME,
263   m68k_linux_sigtramp_frame_this_id,
264   m68k_linux_sigtramp_frame_prev_register
265 };
266 
267 static const struct frame_unwind *
m68k_linux_sigtramp_frame_sniffer(struct frame_info * next_frame)268 m68k_linux_sigtramp_frame_sniffer (struct frame_info *next_frame)
269 {
270   CORE_ADDR pc = frame_pc_unwind (next_frame);
271   char *name;
272 
273   find_pc_partial_function (pc, &name, NULL, NULL);
274   if (m68k_linux_pc_in_sigtramp (pc, name))
275     return &m68k_linux_sigtramp_frame_unwind;
276 
277   return NULL;
278 }
279 
280 static void
m68k_linux_init_abi(struct gdbarch_info info,struct gdbarch * gdbarch)281 m68k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
282 {
283   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
284 
285   tdep->jb_pc = M68K_LINUX_JB_PC;
286   tdep->jb_elt_size = M68K_LINUX_JB_ELEMENT_SIZE;
287 
288   /* GNU/Linux uses a calling convention that's similar to SVR4.  It
289      returns integer values in %d0/%di, pointer values in %a0 and
290      floating values in %fp0, just like SVR4, but uses %a1 to pass the
291      address to store a structure value.  It also returns small
292      structures in registers instead of memory.  */
293   m68k_svr4_init_abi (info, gdbarch);
294   tdep->struct_value_regnum = M68K_A1_REGNUM;
295   tdep->struct_return = reg_struct_return;
296 
297   frame_unwind_append_sniffer (gdbarch, m68k_linux_sigtramp_frame_sniffer);
298 
299   /* Shared library handling.  */
300 
301   /* GNU/Linux uses SVR4-style shared libraries.  */
302   set_solib_svr4_fetch_link_map_offsets (gdbarch,
303 					 svr4_ilp32_fetch_link_map_offsets);
304 
305   /* GNU/Linux uses the dynamic linker included in the GNU C Library.  */
306   set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
307 
308   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
309 
310   /* Enable TLS support.  */
311   set_gdbarch_fetch_tls_load_module_address (gdbarch,
312                                              svr4_fetch_objfile_link_map);
313 }
314 
315 void
_initialize_m68k_linux_tdep(void)316 _initialize_m68k_linux_tdep (void)
317 {
318   gdbarch_register_osabi (bfd_arch_m68k, 0, GDB_OSABI_LINUX,
319 			  m68k_linux_init_abi);
320 }
321