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