1 /* Motorola m68k target-dependent support for GNU/Linux.
2 
3    Copyright (C) 1996, 1998, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009,
4    2010, 2011 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 3 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, see <http://www.gnu.org/licenses/>.  */
20 
21 #include "defs.h"
22 #include "gdbcore.h"
23 #include "doublest.h"
24 #include "floatformat.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 #include "trad-frame.h"
35 #include "frame-unwind.h"
36 #include "glibc-tdep.h"
37 #include "solib-svr4.h"
38 #include "auxv.h"
39 #include "observer.h"
40 #include "elf/common.h"
41 #include "linux-tdep.h"
42 
43 /* Offsets (in target ints) into jmp_buf.  */
44 
45 #define M68K_LINUX_JB_ELEMENT_SIZE 4
46 #define M68K_LINUX_JB_PC 7
47 
48 /* Check whether insn1 and insn2 are parts of a signal trampoline.  */
49 
50 #define IS_SIGTRAMP(insn1, insn2)					\
51   (/* addaw #20,sp; moveq #119,d0; trap #0 */				\
52    (insn1 == 0xdefc0014 && insn2 == 0x70774e40)				\
53    /* moveq #119,d0; trap #0 */						\
54    || insn1 == 0x70774e40)
55 
56 #define IS_RT_SIGTRAMP(insn1, insn2)					\
57   (/* movel #173,d0; trap #0 */						\
58    (insn1 == 0x203c0000 && insn2 == 0x00ad4e40)				\
59    /* moveq #82,d0; notb d0; trap #0 */					\
60    || (insn1 == 0x70524600 && (insn2 >> 16) == 0x4e40))
61 
62 /* Return non-zero if THIS_FRAME corresponds to a signal trampoline.  For
63    the sake of m68k_linux_get_sigtramp_info we also distinguish between
64    non-RT and RT signal trampolines.  */
65 
66 static int
m68k_linux_pc_in_sigtramp(struct frame_info * this_frame)67 m68k_linux_pc_in_sigtramp (struct frame_info *this_frame)
68 {
69   struct gdbarch *gdbarch = get_frame_arch (this_frame);
70   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
71   CORE_ADDR sp;
72   gdb_byte buf[12];
73   unsigned long insn0, insn1, insn2;
74   CORE_ADDR pc = get_frame_pc (this_frame);
75 
76   if (!safe_frame_unwind_memory (this_frame, pc - 4, buf, sizeof (buf)))
77     return 0;
78   insn1 = extract_unsigned_integer (buf + 4, 4, byte_order);
79   insn2 = extract_unsigned_integer (buf + 8, 4, byte_order);
80   if (IS_SIGTRAMP (insn1, insn2))
81     return 1;
82   if (IS_RT_SIGTRAMP (insn1, insn2))
83     return 2;
84 
85   insn0 = extract_unsigned_integer (buf, 4, byte_order);
86   if (IS_SIGTRAMP (insn0, insn1))
87     return 1;
88   if (IS_RT_SIGTRAMP (insn0, insn1))
89     return 2;
90 
91   insn0 = ((insn0 << 16) & 0xffffffff) | (insn1 >> 16);
92   insn1 = ((insn1 << 16) & 0xffffffff) | (insn2 >> 16);
93   if (IS_SIGTRAMP (insn0, insn1))
94     return 1;
95   if (IS_RT_SIGTRAMP (insn0, insn1))
96     return 2;
97 
98   return 0;
99 }
100 
101 /* From <asm/sigcontext.h>.  */
102 static int m68k_linux_sigcontext_reg_offset[M68K_NUM_REGS] =
103 {
104   2 * 4,			/* %d0 */
105   3 * 4,			/* %d1 */
106   -1,				/* %d2 */
107   -1,				/* %d3 */
108   -1,				/* %d4 */
109   -1,				/* %d5 */
110   -1,				/* %d6 */
111   -1,				/* %d7 */
112   4 * 4,			/* %a0 */
113   5 * 4,			/* %a1 */
114   -1,				/* %a2 */
115   -1,				/* %a3 */
116   -1,				/* %a4 */
117   -1,				/* %a5 */
118   -1,				/* %fp */
119   1 * 4,			/* %sp */
120   6 * 4,			/* %sr */
121   6 * 4 + 2,			/* %pc */
122   8 * 4,			/* %fp0 */
123   11 * 4,			/* %fp1 */
124   -1,				/* %fp2 */
125   -1,				/* %fp3 */
126   -1,				/* %fp4 */
127   -1,				/* %fp5 */
128   -1,				/* %fp6 */
129   -1,				/* %fp7 */
130   14 * 4,			/* %fpcr */
131   15 * 4,			/* %fpsr */
132   16 * 4			/* %fpiaddr */
133 };
134 
135 static int m68k_uclinux_sigcontext_reg_offset[M68K_NUM_REGS] =
136 {
137   2 * 4,			/* %d0 */
138   3 * 4,			/* %d1 */
139   -1,				/* %d2 */
140   -1,				/* %d3 */
141   -1,				/* %d4 */
142   -1,				/* %d5 */
143   -1,				/* %d6 */
144   -1,				/* %d7 */
145   4 * 4,			/* %a0 */
146   5 * 4,			/* %a1 */
147   -1,				/* %a2 */
148   -1,				/* %a3 */
149   -1,				/* %a4 */
150   6 * 4,			/* %a5 */
151   -1,				/* %fp */
152   1 * 4,			/* %sp */
153   7 * 4,			/* %sr */
154   7 * 4 + 2,			/* %pc */
155   -1,				/* %fp0 */
156   -1,				/* %fp1 */
157   -1,				/* %fp2 */
158   -1,				/* %fp3 */
159   -1,				/* %fp4 */
160   -1,				/* %fp5 */
161   -1,				/* %fp6 */
162   -1,				/* %fp7 */
163   -1,				/* %fpcr */
164   -1,				/* %fpsr */
165   -1				/* %fpiaddr */
166 };
167 
168 /* From <asm/ucontext.h>.  */
169 static int m68k_linux_ucontext_reg_offset[M68K_NUM_REGS] =
170 {
171   6 * 4,			/* %d0 */
172   7 * 4,			/* %d1 */
173   8 * 4,			/* %d2 */
174   9 * 4,			/* %d3 */
175   10 * 4,			/* %d4 */
176   11 * 4,			/* %d5 */
177   12 * 4,			/* %d6 */
178   13 * 4,			/* %d7 */
179   14 * 4,			/* %a0 */
180   15 * 4,			/* %a1 */
181   16 * 4,			/* %a2 */
182   17 * 4,			/* %a3 */
183   18 * 4,			/* %a4 */
184   19 * 4,			/* %a5 */
185   20 * 4,			/* %fp */
186   21 * 4,			/* %sp */
187   23 * 4,			/* %sr */
188   22 * 4,			/* %pc */
189   27 * 4,			/* %fp0 */
190   30 * 4,			/* %fp1 */
191   33 * 4,			/* %fp2 */
192   36 * 4,			/* %fp3 */
193   39 * 4,			/* %fp4 */
194   42 * 4,			/* %fp5 */
195   45 * 4,			/* %fp6 */
196   48 * 4,			/* %fp7 */
197   24 * 4,			/* %fpcr */
198   25 * 4,			/* %fpsr */
199   26 * 4			/* %fpiaddr */
200 };
201 
202 
203 /* Get info about saved registers in sigtramp.  */
204 
205 struct m68k_linux_sigtramp_info
206 {
207   /* Address of sigcontext.  */
208   CORE_ADDR sigcontext_addr;
209 
210   /* Offset of registers in `struct sigcontext'.  */
211   int *sc_reg_offset;
212 };
213 
214 /* Nonzero if running on uClinux.  */
215 static int target_is_uclinux;
216 
217 static void
m68k_linux_inferior_created(struct target_ops * objfile,int from_tty)218 m68k_linux_inferior_created (struct target_ops *objfile, int from_tty)
219 {
220   /* Record that we will need to re-evaluate whether we are running on a
221      uClinux or normal GNU/Linux target (see m68k_linux_get_sigtramp_info).  */
222   target_is_uclinux = -1;
223 }
224 
225 static struct m68k_linux_sigtramp_info
m68k_linux_get_sigtramp_info(struct frame_info * this_frame)226 m68k_linux_get_sigtramp_info (struct frame_info *this_frame)
227 {
228   struct gdbarch *gdbarch = get_frame_arch (this_frame);
229   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
230   CORE_ADDR sp;
231   struct m68k_linux_sigtramp_info info;
232 
233   if (target_is_uclinux == -1)
234     {
235       /* Determine whether we are running on a uClinux or normal GNU/Linux
236          target so we can use the correct sigcontext layouts.  */
237       CORE_ADDR dummy;
238 
239       target_is_uclinux
240         = (target_auxv_search (&current_target, AT_NULL, &dummy) > 0
241 	   && target_auxv_search (&current_target, AT_PAGESZ, &dummy) == 0);
242     }
243 
244   sp = get_frame_register_unsigned (this_frame, M68K_SP_REGNUM);
245 
246   /* Get sigcontext address, it is the third parameter on the stack.  */
247   info.sigcontext_addr = read_memory_unsigned_integer (sp + 8, 4, byte_order);
248 
249   if (m68k_linux_pc_in_sigtramp (this_frame) == 2)
250     info.sc_reg_offset = m68k_linux_ucontext_reg_offset;
251   else
252     info.sc_reg_offset = (target_is_uclinux
253 			  ? m68k_uclinux_sigcontext_reg_offset
254 			  : m68k_linux_sigcontext_reg_offset);
255   return info;
256 }
257 
258 /* Signal trampolines.  */
259 
260 static struct trad_frame_cache *
m68k_linux_sigtramp_frame_cache(struct frame_info * this_frame,void ** this_cache)261 m68k_linux_sigtramp_frame_cache (struct frame_info *this_frame,
262 				 void **this_cache)
263 {
264   struct frame_id this_id;
265   struct trad_frame_cache *cache;
266   struct gdbarch *gdbarch = get_frame_arch (this_frame);
267   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
268   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
269   struct m68k_linux_sigtramp_info info;
270   gdb_byte buf[4];
271   int i;
272 
273   if (*this_cache)
274     return *this_cache;
275 
276   cache = trad_frame_cache_zalloc (this_frame);
277 
278   /* FIXME: cagney/2004-05-01: This is is long standing broken code.
279      The frame ID's code address should be the start-address of the
280      signal trampoline and not the current PC within that
281      trampoline.  */
282   get_frame_register (this_frame, M68K_SP_REGNUM, buf);
283   /* See the end of m68k_push_dummy_call.  */
284   this_id = frame_id_build (extract_unsigned_integer (buf, 4, byte_order)
285 			    - 4 + 8, get_frame_pc (this_frame));
286   trad_frame_set_id (cache, this_id);
287 
288   info = m68k_linux_get_sigtramp_info (this_frame);
289 
290   for (i = 0; i < M68K_NUM_REGS; i++)
291     if (info.sc_reg_offset[i] != -1)
292       trad_frame_set_reg_addr (cache, i,
293 			       info.sigcontext_addr + info.sc_reg_offset[i]);
294 
295   *this_cache = cache;
296   return cache;
297 }
298 
299 static void
m68k_linux_sigtramp_frame_this_id(struct frame_info * this_frame,void ** this_cache,struct frame_id * this_id)300 m68k_linux_sigtramp_frame_this_id (struct frame_info *this_frame,
301 				   void **this_cache,
302 				   struct frame_id *this_id)
303 {
304   struct trad_frame_cache *cache =
305     m68k_linux_sigtramp_frame_cache (this_frame, this_cache);
306   trad_frame_get_id (cache, this_id);
307 }
308 
309 static struct value *
m68k_linux_sigtramp_frame_prev_register(struct frame_info * this_frame,void ** this_cache,int regnum)310 m68k_linux_sigtramp_frame_prev_register (struct frame_info *this_frame,
311 					 void **this_cache,
312 					 int regnum)
313 {
314   /* Make sure we've initialized the cache.  */
315   struct trad_frame_cache *cache =
316     m68k_linux_sigtramp_frame_cache (this_frame, this_cache);
317   return trad_frame_get_register (cache, this_frame, regnum);
318 }
319 
320 static int
m68k_linux_sigtramp_frame_sniffer(const struct frame_unwind * self,struct frame_info * this_frame,void ** this_prologue_cache)321 m68k_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
322 				   struct frame_info *this_frame,
323 				   void **this_prologue_cache)
324 {
325   return m68k_linux_pc_in_sigtramp (this_frame);
326 }
327 
328 static const struct frame_unwind m68k_linux_sigtramp_frame_unwind =
329 {
330   SIGTRAMP_FRAME,
331   default_frame_unwind_stop_reason,
332   m68k_linux_sigtramp_frame_this_id,
333   m68k_linux_sigtramp_frame_prev_register,
334   NULL,
335   m68k_linux_sigtramp_frame_sniffer
336 };
337 
338 static void
m68k_linux_init_abi(struct gdbarch_info info,struct gdbarch * gdbarch)339 m68k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
340 {
341   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
342 
343   linux_init_abi (info, gdbarch);
344 
345   tdep->jb_pc = M68K_LINUX_JB_PC;
346   tdep->jb_elt_size = M68K_LINUX_JB_ELEMENT_SIZE;
347 
348   /* GNU/Linux uses a calling convention that's similar to SVR4.  It
349      returns integer values in %d0/%d1, pointer values in %a0 and
350      floating values in %fp0, just like SVR4, but uses %a1 to pass the
351      address to store a structure value.  It also returns small
352      structures in registers instead of memory.  */
353   m68k_svr4_init_abi (info, gdbarch);
354   tdep->struct_value_regnum = M68K_A1_REGNUM;
355   tdep->struct_return = reg_struct_return;
356 
357   set_gdbarch_decr_pc_after_break (gdbarch, 2);
358 
359   frame_unwind_append_unwinder (gdbarch, &m68k_linux_sigtramp_frame_unwind);
360 
361   /* Shared library handling.  */
362 
363   /* GNU/Linux uses SVR4-style shared libraries.  */
364   set_solib_svr4_fetch_link_map_offsets (gdbarch,
365 					 svr4_ilp32_fetch_link_map_offsets);
366 
367   /* GNU/Linux uses the dynamic linker included in the GNU C Library.  */
368   set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
369 
370   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
371 
372   /* Enable TLS support.  */
373   set_gdbarch_fetch_tls_load_module_address (gdbarch,
374                                              svr4_fetch_objfile_link_map);
375 }
376 
377 /* Provide a prototype to silence -Wmissing-prototypes.  */
378 extern initialize_file_ftype _initialize_m68k_linux_tdep;
379 
380 void
_initialize_m68k_linux_tdep(void)381 _initialize_m68k_linux_tdep (void)
382 {
383   gdbarch_register_osabi (bfd_arch_m68k, 0, GDB_OSABI_LINUX,
384 			  m68k_linux_init_abi);
385   observer_attach_inferior_created (m68k_linux_inferior_created);
386 }
387