Include string.h in common-defs.h
[external/binutils.git] / gdb / alpha-linux-tdep.c
1 /* Target-dependent code for GNU/Linux on Alpha.
2    Copyright (C) 2002-2014 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include "defs.h"
20 #include "frame.h"
21 #include "osabi.h"
22 #include "solib-svr4.h"
23 #include "symtab.h"
24 #include "regset.h"
25 #include "regcache.h"
26 #include "linux-tdep.h"
27 #include "alpha-tdep.h"
28
29 /* This enum represents the signals' numbers on the Alpha
30    architecture.  It just contains the signal definitions which are
31    different from the generic implementation.
32
33    It is derived from the file <arch/alpha/include/uapi/asm/signal.h>,
34    from the Linux kernel tree.  */
35
36 enum
37   {
38     /* SIGABRT is the same as in the generic implementation, but is
39        defined here because SIGIOT depends on it.  */
40     ALPHA_LINUX_SIGABRT = 6,
41     ALPHA_LINUX_SIGEMT = 7,
42     ALPHA_LINUX_SIGBUS = 10,
43     ALPHA_LINUX_SIGSYS = 12,
44     ALPHA_LINUX_SIGURG = 16,
45     ALPHA_LINUX_SIGSTOP = 17,
46     ALPHA_LINUX_SIGTSTP = 18,
47     ALPHA_LINUX_SIGCONT = 19,
48     ALPHA_LINUX_SIGCHLD = 20,
49     ALPHA_LINUX_SIGIO = 23,
50     ALPHA_LINUX_SIGINFO = 29,
51     ALPHA_LINUX_SIGUSR1 = 30,
52     ALPHA_LINUX_SIGUSR2 = 31,
53     ALPHA_LINUX_SIGPOLL = ALPHA_LINUX_SIGIO,
54     ALPHA_LINUX_SIGPWR = ALPHA_LINUX_SIGINFO,
55     ALPHA_LINUX_SIGIOT = ALPHA_LINUX_SIGABRT,
56   };
57
58 /* Under GNU/Linux, signal handler invocations can be identified by
59    the designated code sequence that is used to return from a signal
60    handler.  In particular, the return address of a signal handler
61    points to a sequence that copies $sp to $16, loads $0 with the
62    appropriate syscall number, and finally enters the kernel.
63
64    This is somewhat complicated in that:
65      (1) the expansion of the "mov" assembler macro has changed over
66          time, from "bis src,src,dst" to "bis zero,src,dst",
67      (2) the kernel has changed from using "addq" to "lda" to load the
68          syscall number,
69      (3) there is a "normal" sigreturn and an "rt" sigreturn which
70          has a different stack layout.  */
71
72 static long
73 alpha_linux_sigtramp_offset_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
74 {
75   switch (alpha_read_insn (gdbarch, pc))
76     {
77     case 0x47de0410:            /* bis $30,$30,$16 */
78     case 0x47fe0410:            /* bis $31,$30,$16 */
79       return 0;
80
81     case 0x43ecf400:            /* addq $31,103,$0 */
82     case 0x201f0067:            /* lda $0,103($31) */
83     case 0x201f015f:            /* lda $0,351($31) */
84       return 4;
85
86     case 0x00000083:            /* call_pal callsys */
87       return 8;
88
89     default:
90       return -1;
91     }
92 }
93
94 static LONGEST
95 alpha_linux_sigtramp_offset (struct gdbarch *gdbarch, CORE_ADDR pc)
96 {
97   long i, off;
98
99   if (pc & 3)
100     return -1;
101
102   /* Guess where we might be in the sequence.  */
103   off = alpha_linux_sigtramp_offset_1 (gdbarch, pc);
104   if (off < 0)
105     return -1;
106
107   /* Verify that the other two insns of the sequence are as we expect.  */
108   pc -= off;
109   for (i = 0; i < 12; i += 4)
110     {
111       if (i == off)
112         continue;
113       if (alpha_linux_sigtramp_offset_1 (gdbarch, pc + i) != i)
114         return -1;
115     }
116
117   return off;
118 }
119
120 static int
121 alpha_linux_pc_in_sigtramp (struct gdbarch *gdbarch,
122                             CORE_ADDR pc, const char *func_name)
123 {
124   return alpha_linux_sigtramp_offset (gdbarch, pc) >= 0;
125 }
126
127 static CORE_ADDR
128 alpha_linux_sigcontext_addr (struct frame_info *this_frame)
129 {
130   struct gdbarch *gdbarch = get_frame_arch (this_frame);
131   CORE_ADDR pc;
132   ULONGEST sp;
133   long off;
134
135   pc = get_frame_pc (this_frame);
136   sp = get_frame_register_unsigned (this_frame, ALPHA_SP_REGNUM);
137
138   off = alpha_linux_sigtramp_offset (gdbarch, pc);
139   gdb_assert (off >= 0);
140
141   /* __NR_rt_sigreturn has a couple of structures on the stack.  This is:
142
143         struct rt_sigframe {
144           struct siginfo info;
145           struct ucontext uc;
146         };
147
148         offsetof (struct rt_sigframe, uc.uc_mcontext);  */
149
150   if (alpha_read_insn (gdbarch, pc - off + 4) == 0x201f015f)
151     return sp + 176;
152
153   /* __NR_sigreturn has the sigcontext structure at the top of the stack.  */
154   return sp;
155 }
156
157 /* Supply register REGNUM from the buffer specified by GREGS and LEN
158    in the general-purpose register set REGSET to register cache
159    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
160
161 static void
162 alpha_linux_supply_gregset (const struct regset *regset,
163                             struct regcache *regcache,
164                             int regnum, const void *gregs, size_t len)
165 {
166   const gdb_byte *regs = gregs;
167   int i;
168   gdb_assert (len >= 32 * 8);
169
170   for (i = 0; i < ALPHA_ZERO_REGNUM; i++)
171     {
172       if (regnum == i || regnum == -1)
173         regcache_raw_supply (regcache, i, regs + i * 8);
174     }
175
176   if (regnum == ALPHA_PC_REGNUM || regnum == -1)
177     regcache_raw_supply (regcache, ALPHA_PC_REGNUM, regs + 31 * 8);
178
179   if (regnum == ALPHA_UNIQUE_REGNUM || regnum == -1)
180     regcache_raw_supply (regcache, ALPHA_UNIQUE_REGNUM,
181                          len >= 33 * 8 ? regs + 32 * 8 : NULL);
182 }
183
184 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
185    in the floating-point register set REGSET to register cache
186    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
187
188 static void
189 alpha_linux_supply_fpregset (const struct regset *regset,
190                              struct regcache *regcache,
191                              int regnum, const void *fpregs, size_t len)
192 {
193   const gdb_byte *regs = fpregs;
194   int i;
195   gdb_assert (len >= 32 * 8);
196
197   for (i = ALPHA_FP0_REGNUM; i < ALPHA_FP0_REGNUM + 31; i++)
198     {
199       if (regnum == i || regnum == -1)
200         regcache_raw_supply (regcache, i, regs + (i - ALPHA_FP0_REGNUM) * 8);
201     }
202
203   if (regnum == ALPHA_FPCR_REGNUM || regnum == -1)
204     regcache_raw_supply (regcache, ALPHA_FPCR_REGNUM, regs + 31 * 8);
205 }
206
207 static const struct regset alpha_linux_gregset =
208 {
209   NULL,
210   alpha_linux_supply_gregset
211 };
212
213 static const struct regset alpha_linux_fpregset =
214 {
215   NULL,
216   alpha_linux_supply_fpregset
217 };
218
219 /* Return the appropriate register set for the core section identified
220    by SECT_NAME and SECT_SIZE.  */
221
222 static const struct regset *
223 alpha_linux_regset_from_core_section (struct gdbarch *gdbarch,
224                                       const char *sect_name, size_t sect_size)
225 {
226   if (strcmp (sect_name, ".reg") == 0 && sect_size >= 32 * 8)
227     return &alpha_linux_gregset;
228
229   if (strcmp (sect_name, ".reg2") == 0 && sect_size >= 32 * 8)
230     return &alpha_linux_fpregset;
231
232   return NULL;
233 }
234
235 /* Implementation of `gdbarch_gdb_signal_from_target', as defined in
236    gdbarch.h.  */
237
238 static enum gdb_signal
239 alpha_linux_gdb_signal_from_target (struct gdbarch *gdbarch,
240                                     int signal)
241 {
242   switch (signal)
243     {
244     case ALPHA_LINUX_SIGEMT:
245       return GDB_SIGNAL_EMT;
246
247     case ALPHA_LINUX_SIGBUS:
248       return GDB_SIGNAL_BUS;
249
250     case ALPHA_LINUX_SIGSYS:
251       return GDB_SIGNAL_SYS;
252
253     case ALPHA_LINUX_SIGURG:
254       return GDB_SIGNAL_URG;
255
256     case ALPHA_LINUX_SIGSTOP:
257       return GDB_SIGNAL_STOP;
258
259     case ALPHA_LINUX_SIGTSTP:
260       return GDB_SIGNAL_TSTP;
261
262     case ALPHA_LINUX_SIGCONT:
263       return GDB_SIGNAL_CONT;
264
265     case ALPHA_LINUX_SIGCHLD:
266       return GDB_SIGNAL_CHLD;
267
268     /* No way to differentiate between SIGIO and SIGPOLL.
269        Therefore, we just handle the first one.  */
270     case ALPHA_LINUX_SIGIO:
271       return GDB_SIGNAL_IO;
272
273     /* No way to differentiate between SIGINFO and SIGPWR.
274        Therefore, we just handle the first one.  */
275     case ALPHA_LINUX_SIGINFO:
276       return GDB_SIGNAL_INFO;
277
278     case ALPHA_LINUX_SIGUSR1:
279       return GDB_SIGNAL_USR1;
280
281     case ALPHA_LINUX_SIGUSR2:
282       return GDB_SIGNAL_USR2;
283     }
284
285   return linux_gdb_signal_from_target (gdbarch, signal);
286 }
287
288 /* Implementation of `gdbarch_gdb_signal_to_target', as defined in
289    gdbarch.h.  */
290
291 static int
292 alpha_linux_gdb_signal_to_target (struct gdbarch *gdbarch,
293                                   enum gdb_signal signal)
294 {
295   switch (signal)
296     {
297     case GDB_SIGNAL_EMT:
298       return ALPHA_LINUX_SIGEMT;
299
300     case GDB_SIGNAL_BUS:
301       return ALPHA_LINUX_SIGBUS;
302
303     case GDB_SIGNAL_SYS:
304       return ALPHA_LINUX_SIGSYS;
305
306     case GDB_SIGNAL_URG:
307       return ALPHA_LINUX_SIGURG;
308
309     case GDB_SIGNAL_STOP:
310       return ALPHA_LINUX_SIGSTOP;
311
312     case GDB_SIGNAL_TSTP:
313       return ALPHA_LINUX_SIGTSTP;
314
315     case GDB_SIGNAL_CONT:
316       return ALPHA_LINUX_SIGCONT;
317
318     case GDB_SIGNAL_CHLD:
319       return ALPHA_LINUX_SIGCHLD;
320
321     case GDB_SIGNAL_IO:
322       return ALPHA_LINUX_SIGIO;
323
324     case GDB_SIGNAL_INFO:
325       return ALPHA_LINUX_SIGINFO;
326
327     case GDB_SIGNAL_USR1:
328       return ALPHA_LINUX_SIGUSR1;
329
330     case GDB_SIGNAL_USR2:
331       return ALPHA_LINUX_SIGUSR2;
332
333     case GDB_SIGNAL_POLL:
334       return ALPHA_LINUX_SIGPOLL;
335
336     case GDB_SIGNAL_PWR:
337       return ALPHA_LINUX_SIGPWR;
338     }
339
340   return linux_gdb_signal_to_target (gdbarch, signal);
341 }
342
343 static void
344 alpha_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
345 {
346   struct gdbarch_tdep *tdep;
347
348   linux_init_abi (info, gdbarch);
349
350   /* Hook into the DWARF CFI frame unwinder.  */
351   alpha_dwarf2_init_abi (info, gdbarch);
352
353   /* Hook into the MDEBUG frame unwinder.  */
354   alpha_mdebug_init_abi (info, gdbarch);
355
356   tdep = gdbarch_tdep (gdbarch);
357   tdep->dynamic_sigtramp_offset = alpha_linux_sigtramp_offset;
358   tdep->sigcontext_addr = alpha_linux_sigcontext_addr;
359   tdep->pc_in_sigtramp = alpha_linux_pc_in_sigtramp;
360   tdep->jb_pc = 2;
361   tdep->jb_elt_size = 8;
362
363   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
364
365   set_solib_svr4_fetch_link_map_offsets
366     (gdbarch, svr4_lp64_fetch_link_map_offsets);
367
368   /* Enable TLS support.  */
369   set_gdbarch_fetch_tls_load_module_address (gdbarch,
370                                              svr4_fetch_objfile_link_map);
371
372   set_gdbarch_regset_from_core_section
373     (gdbarch, alpha_linux_regset_from_core_section);
374
375   set_gdbarch_gdb_signal_from_target (gdbarch,
376                                       alpha_linux_gdb_signal_from_target);
377   set_gdbarch_gdb_signal_to_target (gdbarch,
378                                     alpha_linux_gdb_signal_to_target);
379 }
380
381 /* Provide a prototype to silence -Wmissing-prototypes.  */
382 extern initialize_file_ftype _initialize_alpha_linux_tdep;
383
384 void
385 _initialize_alpha_linux_tdep (void)
386 {
387   gdbarch_register_osabi (bfd_arch_alpha, 0, GDB_OSABI_LINUX,
388                           alpha_linux_init_abi);
389 }