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