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