1/* Target-dependent code for GNU/Linux running on PA-RISC, for GDB.
2
3   Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010, 2011
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 "osabi.h"
24#include "target.h"
25#include "objfiles.h"
26#include "solib-svr4.h"
27#include "glibc-tdep.h"
28#include "frame-unwind.h"
29#include "trad-frame.h"
30#include "dwarf2-frame.h"
31#include "value.h"
32#include "regset.h"
33#include "regcache.h"
34#include "hppa-tdep.h"
35#include "linux-tdep.h"
36#include "elf/common.h"
37
38/* Map DWARF DBX register numbers to GDB register numbers.  */
39static int
40hppa_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
41{
42  /* The general registers and the sar are the same in both sets.  */
43  if (reg <= 32)
44    return reg;
45
46  /* fr4-fr31 (left and right halves) are mapped from 72.  */
47  if (reg >= 72 && reg <= 72 + 28 * 2)
48    return HPPA_FP4_REGNUM + (reg - 72);
49
50  warning (_("Unmapped DWARF DBX Register #%d encountered."), reg);
51  return -1;
52}
53
54static void
55hppa_linux_target_write_pc (struct regcache *regcache, CORE_ADDR v)
56{
57  /* Probably this should be done by the kernel, but it isn't.  */
58  regcache_cooked_write_unsigned (regcache, HPPA_PCOQ_HEAD_REGNUM, v | 0x3);
59  regcache_cooked_write_unsigned (regcache,
60				  HPPA_PCOQ_TAIL_REGNUM, (v + 4) | 0x3);
61}
62
63/* An instruction to match.  */
64struct insn_pattern
65{
66  unsigned int data;            /* See if it matches this....  */
67  unsigned int mask;            /* ... with this mask.  */
68};
69
70static struct insn_pattern hppa_sigtramp[] = {
71  /* ldi 0, %r25 or ldi 1, %r25 */
72  { 0x34190000, 0xfffffffd },
73  /* ldi __NR_rt_sigreturn, %r20 */
74  { 0x3414015a, 0xffffffff },
75  /* be,l 0x100(%sr2, %r0), %sr0, %r31 */
76  { 0xe4008200, 0xffffffff },
77  /* nop */
78  { 0x08000240, 0xffffffff },
79  { 0, 0 }
80};
81
82#define HPPA_MAX_INSN_PATTERN_LEN (4)
83
84/* Return non-zero if the instructions at PC match the series
85   described in PATTERN, or zero otherwise.  PATTERN is an array of
86   'struct insn_pattern' objects, terminated by an entry whose mask is
87   zero.
88
89   When the match is successful, fill INSN[i] with what PATTERN[i]
90   matched.  */
91static int
92insns_match_pattern (struct gdbarch *gdbarch, CORE_ADDR pc,
93                     struct insn_pattern *pattern,
94                     unsigned int *insn)
95{
96  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
97  int i;
98  CORE_ADDR npc = pc;
99
100  for (i = 0; pattern[i].mask; i++)
101    {
102      char buf[4];
103
104      target_read_memory (npc, buf, 4);
105      insn[i] = extract_unsigned_integer (buf, 4, byte_order);
106      if ((insn[i] & pattern[i].mask) == pattern[i].data)
107        npc += 4;
108      else
109        return 0;
110    }
111  return 1;
112}
113
114/* Signal frames.  */
115
116/* (This is derived from MD_FALLBACK_FRAME_STATE_FOR in gcc.)
117
118   Unfortunately, because of various bugs and changes to the kernel,
119   we have several cases to deal with.
120
121   In 2.4, the signal trampoline is 4 bytes, and pc should point directly at
122   the beginning of the trampoline and struct rt_sigframe.
123
124   In <= 2.6.5-rc2-pa3, the signal trampoline is 9 bytes, and pc points at
125   the 4th word in the trampoline structure.  This is wrong, it should point
126   at the 5th word.  This is fixed in 2.6.5-rc2-pa4.
127
128   To detect these cases, we first take pc, align it to 64-bytes
129   to get the beginning of the signal frame, and then check offsets 0, 4
130   and 5 to see if we found the beginning of the trampoline.  This will
131   tell us how to locate the sigcontext structure.
132
133   Note that with a 2.4 64-bit kernel, the signal context is not properly
134   passed back to userspace so the unwind will not work correctly.  */
135static CORE_ADDR
136hppa_linux_sigtramp_find_sigcontext (struct gdbarch *gdbarch, CORE_ADDR pc)
137{
138  unsigned int dummy[HPPA_MAX_INSN_PATTERN_LEN];
139  int offs = 0;
140  int try;
141  /* offsets to try to find the trampoline */
142  static int pcoffs[] = { 0, 4*4, 5*4 };
143  /* offsets to the rt_sigframe structure */
144  static int sfoffs[] = { 4*4, 10*4, 10*4 };
145  CORE_ADDR sp;
146
147  /* Most of the time, this will be correct.  The one case when this will
148     fail is if the user defined an alternate stack, in which case the
149     beginning of the stack will not be align_down (pc, 64).  */
150  sp = align_down (pc, 64);
151
152  /* rt_sigreturn trampoline:
153     3419000x ldi 0, %r25 or ldi 1, %r25   (x = 0 or 2)
154     3414015a ldi __NR_rt_sigreturn, %r20
155     e4008200 be,l 0x100(%sr2, %r0), %sr0, %r31
156     08000240 nop  */
157
158  for (try = 0; try < ARRAY_SIZE (pcoffs); try++)
159    {
160      if (insns_match_pattern (gdbarch, sp + pcoffs[try],
161			       hppa_sigtramp, dummy))
162	{
163          offs = sfoffs[try];
164	  break;
165	}
166    }
167
168  if (offs == 0)
169    {
170      if (insns_match_pattern (gdbarch, pc, hppa_sigtramp, dummy))
171	{
172	  /* sigaltstack case: we have no way of knowing which offset to
173	     use in this case; default to new kernel handling.  If this is
174	     wrong the unwinding will fail.  */
175	  try = 2;
176	  sp = pc - pcoffs[try];
177	}
178      else
179      {
180        return 0;
181      }
182    }
183
184  /* sp + sfoffs[try] points to a struct rt_sigframe, which contains
185     a struct siginfo and a struct ucontext.  struct ucontext contains
186     a struct sigcontext.  Return an offset to this sigcontext here.  Too
187     bad we cannot include system specific headers :-(.
188     sizeof(struct siginfo) == 128
189     offsetof(struct ucontext, uc_mcontext) == 24.  */
190  return sp + sfoffs[try] + 128 + 24;
191}
192
193struct hppa_linux_sigtramp_unwind_cache
194{
195  CORE_ADDR base;
196  struct trad_frame_saved_reg *saved_regs;
197};
198
199static struct hppa_linux_sigtramp_unwind_cache *
200hppa_linux_sigtramp_frame_unwind_cache (struct frame_info *this_frame,
201					void **this_cache)
202{
203  struct gdbarch *gdbarch = get_frame_arch (this_frame);
204  struct hppa_linux_sigtramp_unwind_cache *info;
205  CORE_ADDR pc, scptr;
206  int i;
207
208  if (*this_cache)
209    return *this_cache;
210
211  info = FRAME_OBSTACK_ZALLOC (struct hppa_linux_sigtramp_unwind_cache);
212  *this_cache = info;
213  info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
214
215  pc = get_frame_pc (this_frame);
216  scptr = hppa_linux_sigtramp_find_sigcontext (gdbarch, pc);
217
218  /* structure of struct sigcontext:
219
220     struct sigcontext {
221	unsigned long sc_flags;
222	unsigned long sc_gr[32];
223	unsigned long long sc_fr[32];
224	unsigned long sc_iasq[2];
225	unsigned long sc_iaoq[2];
226	unsigned long sc_sar;           */
227
228  /* Skip sc_flags.  */
229  scptr += 4;
230
231  /* GR[0] is the psw.  */
232  info->saved_regs[HPPA_IPSW_REGNUM].addr = scptr;
233  scptr += 4;
234
235  /* General registers.  */
236  for (i = 1; i < 32; i++)
237    {
238      info->saved_regs[HPPA_R0_REGNUM + i].addr = scptr;
239      scptr += 4;
240    }
241
242  /* Pad to long long boundary.  */
243  scptr += 4;
244
245  /* FP regs; FP0-3 are not restored.  */
246  scptr += (8 * 4);
247
248  for (i = 4; i < 32; i++)
249    {
250      info->saved_regs[HPPA_FP0_REGNUM + (i * 2)].addr = scptr;
251      scptr += 4;
252      info->saved_regs[HPPA_FP0_REGNUM + (i * 2) + 1].addr = scptr;
253      scptr += 4;
254    }
255
256  /* IASQ/IAOQ.  */
257  info->saved_regs[HPPA_PCSQ_HEAD_REGNUM].addr = scptr;
258  scptr += 4;
259  info->saved_regs[HPPA_PCSQ_TAIL_REGNUM].addr = scptr;
260  scptr += 4;
261
262  info->saved_regs[HPPA_PCOQ_HEAD_REGNUM].addr = scptr;
263  scptr += 4;
264  info->saved_regs[HPPA_PCOQ_TAIL_REGNUM].addr = scptr;
265  scptr += 4;
266
267  info->saved_regs[HPPA_SAR_REGNUM].addr = scptr;
268
269  info->base = get_frame_register_unsigned (this_frame, HPPA_SP_REGNUM);
270
271  return info;
272}
273
274static void
275hppa_linux_sigtramp_frame_this_id (struct frame_info *this_frame,
276				   void **this_prologue_cache,
277				   struct frame_id *this_id)
278{
279  struct hppa_linux_sigtramp_unwind_cache *info
280    = hppa_linux_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);
281  *this_id = frame_id_build (info->base, get_frame_pc (this_frame));
282}
283
284static struct value *
285hppa_linux_sigtramp_frame_prev_register (struct frame_info *this_frame,
286					 void **this_prologue_cache,
287					 int regnum)
288{
289  struct hppa_linux_sigtramp_unwind_cache *info
290    = hppa_linux_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);
291  return hppa_frame_prev_register_helper (this_frame,
292					  info->saved_regs, regnum);
293}
294
295/* hppa-linux always uses "new-style" rt-signals.  The signal handler's return
296   address should point to a signal trampoline on the stack.  The signal
297   trampoline is embedded in a rt_sigframe structure that is aligned on
298   the stack.  We take advantage of the fact that sp must be 64-byte aligned,
299   and the trampoline is small, so by rounding down the trampoline address
300   we can find the beginning of the struct rt_sigframe.  */
301static int
302hppa_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
303				   struct frame_info *this_frame,
304				   void **this_prologue_cache)
305{
306  struct gdbarch *gdbarch = get_frame_arch (this_frame);
307  CORE_ADDR pc = get_frame_pc (this_frame);
308
309  if (hppa_linux_sigtramp_find_sigcontext (gdbarch, pc))
310    return 1;
311
312  return 0;
313}
314
315static const struct frame_unwind hppa_linux_sigtramp_frame_unwind = {
316  SIGTRAMP_FRAME,
317  default_frame_unwind_stop_reason,
318  hppa_linux_sigtramp_frame_this_id,
319  hppa_linux_sigtramp_frame_prev_register,
320  NULL,
321  hppa_linux_sigtramp_frame_sniffer
322};
323
324/* Attempt to find (and return) the global pointer for the given
325   function.
326
327   This is a rather nasty bit of code searchs for the .dynamic section
328   in the objfile corresponding to the pc of the function we're trying
329   to call.  Once it finds the addresses at which the .dynamic section
330   lives in the child process, it scans the Elf32_Dyn entries for a
331   DT_PLTGOT tag.  If it finds one of these, the corresponding
332   d_un.d_ptr value is the global pointer.  */
333
334static CORE_ADDR
335hppa_linux_find_global_pointer (struct gdbarch *gdbarch,
336				struct value *function)
337{
338  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
339  struct obj_section *faddr_sect;
340  CORE_ADDR faddr;
341
342  faddr = value_as_address (function);
343
344  /* Is this a plabel? If so, dereference it to get the gp value.  */
345  if (faddr & 2)
346    {
347      int status;
348      char buf[4];
349
350      faddr &= ~3;
351
352      status = target_read_memory (faddr + 4, buf, sizeof (buf));
353      if (status == 0)
354	return extract_unsigned_integer (buf, sizeof (buf), byte_order);
355    }
356
357  /* If the address is in the plt section, then the real function hasn't
358     yet been fixed up by the linker so we cannot determine the gp of
359     that function.  */
360  if (in_plt_section (faddr, NULL))
361    return 0;
362
363  faddr_sect = find_pc_section (faddr);
364  if (faddr_sect != NULL)
365    {
366      struct obj_section *osect;
367
368      ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
369	{
370	  if (strcmp (osect->the_bfd_section->name, ".dynamic") == 0)
371	    break;
372	}
373
374      if (osect < faddr_sect->objfile->sections_end)
375	{
376	  CORE_ADDR addr, endaddr;
377
378	  addr = obj_section_addr (osect);
379	  endaddr = obj_section_endaddr (osect);
380
381	  while (addr < endaddr)
382	    {
383	      int status;
384	      LONGEST tag;
385	      char buf[4];
386
387	      status = target_read_memory (addr, buf, sizeof (buf));
388	      if (status != 0)
389		break;
390	      tag = extract_signed_integer (buf, sizeof (buf), byte_order);
391
392	      if (tag == DT_PLTGOT)
393		{
394		  CORE_ADDR global_pointer;
395
396		  status = target_read_memory (addr + 4, buf, sizeof (buf));
397		  if (status != 0)
398		    break;
399		  global_pointer = extract_unsigned_integer (buf, sizeof (buf),
400							     byte_order);
401		  /* The payoff...  */
402		  return global_pointer;
403		}
404
405	      if (tag == DT_NULL)
406		break;
407
408	      addr += 8;
409	    }
410	}
411    }
412  return 0;
413}
414
415/*
416 * Registers saved in a coredump:
417 * gr0..gr31
418 * sr0..sr7
419 * iaoq0..iaoq1
420 * iasq0..iasq1
421 * sar, iir, isr, ior, ipsw
422 * cr0, cr24..cr31
423 * cr8,9,12,13
424 * cr10, cr15
425 */
426
427#define GR_REGNUM(_n)	(HPPA_R0_REGNUM+_n)
428#define TR_REGNUM(_n)	(HPPA_TR0_REGNUM+_n)
429static const int greg_map[] =
430  {
431    GR_REGNUM(0), GR_REGNUM(1), GR_REGNUM(2), GR_REGNUM(3),
432    GR_REGNUM(4), GR_REGNUM(5), GR_REGNUM(6), GR_REGNUM(7),
433    GR_REGNUM(8), GR_REGNUM(9), GR_REGNUM(10), GR_REGNUM(11),
434    GR_REGNUM(12), GR_REGNUM(13), GR_REGNUM(14), GR_REGNUM(15),
435    GR_REGNUM(16), GR_REGNUM(17), GR_REGNUM(18), GR_REGNUM(19),
436    GR_REGNUM(20), GR_REGNUM(21), GR_REGNUM(22), GR_REGNUM(23),
437    GR_REGNUM(24), GR_REGNUM(25), GR_REGNUM(26), GR_REGNUM(27),
438    GR_REGNUM(28), GR_REGNUM(29), GR_REGNUM(30), GR_REGNUM(31),
439
440    HPPA_SR4_REGNUM+1, HPPA_SR4_REGNUM+2, HPPA_SR4_REGNUM+3, HPPA_SR4_REGNUM+4,
441    HPPA_SR4_REGNUM, HPPA_SR4_REGNUM+5, HPPA_SR4_REGNUM+6, HPPA_SR4_REGNUM+7,
442
443    HPPA_PCOQ_HEAD_REGNUM, HPPA_PCOQ_TAIL_REGNUM,
444    HPPA_PCSQ_HEAD_REGNUM, HPPA_PCSQ_TAIL_REGNUM,
445
446    HPPA_SAR_REGNUM, HPPA_IIR_REGNUM, HPPA_ISR_REGNUM, HPPA_IOR_REGNUM,
447    HPPA_IPSW_REGNUM, HPPA_RCR_REGNUM,
448
449    TR_REGNUM(0), TR_REGNUM(1), TR_REGNUM(2), TR_REGNUM(3),
450    TR_REGNUM(4), TR_REGNUM(5), TR_REGNUM(6), TR_REGNUM(7),
451
452    HPPA_PID0_REGNUM, HPPA_PID1_REGNUM, HPPA_PID2_REGNUM, HPPA_PID3_REGNUM,
453    HPPA_CCR_REGNUM, HPPA_EIEM_REGNUM,
454  };
455
456static void
457hppa_linux_supply_regset (const struct regset *regset,
458			  struct regcache *regcache,
459			  int regnum, const void *regs, size_t len)
460{
461  struct gdbarch *arch = get_regcache_arch (regcache);
462  struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
463  const char *buf = regs;
464  int i, offset;
465
466  offset = 0;
467  for (i = 0; i < ARRAY_SIZE (greg_map); i++)
468    {
469      if (regnum == greg_map[i] || regnum == -1)
470        regcache_raw_supply (regcache, greg_map[i], buf + offset);
471
472      offset += tdep->bytes_per_address;
473    }
474}
475
476static void
477hppa_linux_supply_fpregset (const struct regset *regset,
478			    struct regcache *regcache,
479			    int regnum, const void *regs, size_t len)
480{
481  const char *buf = regs;
482  int i, offset;
483
484  offset = 0;
485  for (i = 0; i < 64; i++)
486    {
487      if (regnum == HPPA_FP0_REGNUM + i || regnum == -1)
488        regcache_raw_supply (regcache, HPPA_FP0_REGNUM + i,
489			     buf + offset);
490      offset += 4;
491    }
492}
493
494/* HPPA Linux kernel register set.  */
495static struct regset hppa_linux_regset =
496{
497  NULL,
498  hppa_linux_supply_regset
499};
500
501static struct regset hppa_linux_fpregset =
502{
503  NULL,
504  hppa_linux_supply_fpregset
505};
506
507static const struct regset *
508hppa_linux_regset_from_core_section (struct gdbarch *gdbarch,
509				     const char *sect_name,
510				     size_t sect_size)
511{
512  if (strcmp (sect_name, ".reg") == 0)
513    return &hppa_linux_regset;
514  else if (strcmp (sect_name, ".reg2") == 0)
515    return &hppa_linux_fpregset;
516
517  return NULL;
518}
519
520
521/* Forward declarations.  */
522extern initialize_file_ftype _initialize_hppa_linux_tdep;
523
524static void
525hppa_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
526{
527  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
528
529  linux_init_abi (info, gdbarch);
530
531  /* GNU/Linux is always ELF.  */
532  tdep->is_elf = 1;
533
534  tdep->find_global_pointer = hppa_linux_find_global_pointer;
535
536  set_gdbarch_write_pc (gdbarch, hppa_linux_target_write_pc);
537
538  frame_unwind_append_unwinder (gdbarch, &hppa_linux_sigtramp_frame_unwind);
539
540  /* GNU/Linux uses SVR4-style shared libraries.  */
541  set_solib_svr4_fetch_link_map_offsets
542    (gdbarch, svr4_ilp32_fetch_link_map_offsets);
543
544  tdep->in_solib_call_trampoline = hppa_in_solib_call_trampoline;
545  set_gdbarch_skip_trampoline_code (gdbarch, hppa_skip_trampoline_code);
546
547  /* GNU/Linux uses the dynamic linker included in the GNU C Library.  */
548  set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
549
550  /* On hppa-linux, currently, sizeof(long double) == 8.  There has been
551     some discussions to support 128-bit long double, but it requires some
552     more work in gcc and glibc first.  */
553  set_gdbarch_long_double_bit (gdbarch, 64);
554
555  set_gdbarch_regset_from_core_section
556    (gdbarch, hppa_linux_regset_from_core_section);
557
558  set_gdbarch_dwarf2_reg_to_regnum (gdbarch, hppa_dwarf_reg_to_regnum);
559
560  /* Enable TLS support.  */
561  set_gdbarch_fetch_tls_load_module_address (gdbarch,
562                                             svr4_fetch_objfile_link_map);
563}
564
565void
566_initialize_hppa_linux_tdep (void)
567{
568  gdbarch_register_osabi (bfd_arch_hppa, 0, GDB_OSABI_LINUX,
569			  hppa_linux_init_abi);
570  gdbarch_register_osabi (bfd_arch_hppa, bfd_mach_hppa20w,
571			  GDB_OSABI_LINUX, hppa_linux_init_abi);
572}
573