1/* Target-dependent code for HP-UX on PA-RISC.
2
3   Copyright (C) 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4
5   This file is part of GDB.
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 3 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20#include "defs.h"
21#include "arch-utils.h"
22#include "gdbcore.h"
23#include "osabi.h"
24#include "frame.h"
25#include "frame-unwind.h"
26#include "trad-frame.h"
27#include "symtab.h"
28#include "objfiles.h"
29#include "inferior.h"
30#include "infcall.h"
31#include "observer.h"
32#include "hppa-tdep.h"
33#include "solib-som.h"
34#include "solib-pa64.h"
35#include "regset.h"
36#include "regcache.h"
37#include "exceptions.h"
38
39#include "gdb_string.h"
40
41#define IS_32BIT_TARGET(_gdbarch) \
42	((gdbarch_tdep (_gdbarch))->bytes_per_address == 4)
43
44/* Bit in the `ss_flag' member of `struct save_state' that indicates
45   that the 64-bit register values are live.  From
46   <machine/save_state.h>.  */
47#define HPPA_HPUX_SS_WIDEREGS		0x40
48
49/* Offsets of various parts of `struct save_state'.  From
50   <machine/save_state.h>.  */
51#define HPPA_HPUX_SS_FLAGS_OFFSET	0
52#define HPPA_HPUX_SS_NARROW_OFFSET	4
53#define HPPA_HPUX_SS_FPBLOCK_OFFSET 	256
54#define HPPA_HPUX_SS_WIDE_OFFSET        640
55
56/* The size of `struct save_state.  */
57#define HPPA_HPUX_SAVE_STATE_SIZE	1152
58
59/* The size of `struct pa89_save_state', which corresponds to PA-RISC
60   1.1, the lowest common denominator that we support.  */
61#define HPPA_HPUX_PA89_SAVE_STATE_SIZE	512
62
63
64/* Forward declarations.  */
65extern void _initialize_hppa_hpux_tdep (void);
66extern initialize_file_ftype _initialize_hppa_hpux_tdep;
67
68static int
69in_opd_section (CORE_ADDR pc)
70{
71  struct obj_section *s;
72  int retval = 0;
73
74  s = find_pc_section (pc);
75
76  retval = (s != NULL
77	    && s->the_bfd_section->name != NULL
78	    && strcmp (s->the_bfd_section->name, ".opd") == 0);
79  return (retval);
80}
81
82/* Return one if PC is in the call path of a trampoline, else return zero.
83
84   Note we return one for *any* call trampoline (long-call, arg-reloc), not
85   just shared library trampolines (import, export).  */
86
87static int
88hppa32_hpux_in_solib_call_trampoline (CORE_ADDR pc, char *name)
89{
90  struct minimal_symbol *minsym;
91  struct unwind_table_entry *u;
92
93  /* First see if PC is in one of the two C-library trampolines.  */
94  if (pc == hppa_symbol_address("$$dyncall")
95      || pc == hppa_symbol_address("_sr4export"))
96    return 1;
97
98  minsym = lookup_minimal_symbol_by_pc (pc);
99  if (minsym && strcmp (DEPRECATED_SYMBOL_NAME (minsym), ".stub") == 0)
100    return 1;
101
102  /* Get the unwind descriptor corresponding to PC, return zero
103     if no unwind was found.  */
104  u = find_unwind_entry (pc);
105  if (!u)
106    return 0;
107
108  /* If this isn't a linker stub, then return now.  */
109  if (u->stub_unwind.stub_type == 0)
110    return 0;
111
112  /* By definition a long-branch stub is a call stub.  */
113  if (u->stub_unwind.stub_type == LONG_BRANCH)
114    return 1;
115
116  /* The call and return path execute the same instructions within
117     an IMPORT stub!  So an IMPORT stub is both a call and return
118     trampoline.  */
119  if (u->stub_unwind.stub_type == IMPORT)
120    return 1;
121
122  /* Parameter relocation stubs always have a call path and may have a
123     return path.  */
124  if (u->stub_unwind.stub_type == PARAMETER_RELOCATION
125      || u->stub_unwind.stub_type == EXPORT)
126    {
127      CORE_ADDR addr;
128
129      /* Search forward from the current PC until we hit a branch
130         or the end of the stub.  */
131      for (addr = pc; addr <= u->region_end; addr += 4)
132	{
133	  unsigned long insn;
134
135	  insn = read_memory_integer (addr, 4);
136
137	  /* Does it look like a bl?  If so then it's the call path, if
138	     we find a bv or be first, then we're on the return path.  */
139	  if ((insn & 0xfc00e000) == 0xe8000000)
140	    return 1;
141	  else if ((insn & 0xfc00e001) == 0xe800c000
142		   || (insn & 0xfc000000) == 0xe0000000)
143	    return 0;
144	}
145
146      /* Should never happen.  */
147      warning (_("Unable to find branch in parameter relocation stub."));
148      return 0;
149    }
150
151  /* Unknown stub type.  For now, just return zero.  */
152  return 0;
153}
154
155static int
156hppa64_hpux_in_solib_call_trampoline (CORE_ADDR pc, char *name)
157{
158  /* PA64 has a completely different stub/trampoline scheme.  Is it
159     better?  Maybe.  It's certainly harder to determine with any
160     certainty that we are in a stub because we can not refer to the
161     unwinders to help.
162
163     The heuristic is simple.  Try to lookup the current PC value in th
164     minimal symbol table.  If that fails, then assume we are not in a
165     stub and return.
166
167     Then see if the PC value falls within the section bounds for the
168     section containing the minimal symbol we found in the first
169     step.  If it does, then assume we are not in a stub and return.
170
171     Finally peek at the instructions to see if they look like a stub.  */
172  struct minimal_symbol *minsym;
173  asection *sec;
174  CORE_ADDR addr;
175  int insn, i;
176
177  minsym = lookup_minimal_symbol_by_pc (pc);
178  if (! minsym)
179    return 0;
180
181  sec = SYMBOL_BFD_SECTION (minsym);
182
183  if (bfd_get_section_vma (sec->owner, sec) <= pc
184      && pc < (bfd_get_section_vma (sec->owner, sec)
185		 + bfd_section_size (sec->owner, sec)))
186      return 0;
187
188  /* We might be in a stub.  Peek at the instructions.  Stubs are 3
189     instructions long. */
190  insn = read_memory_integer (pc, 4);
191
192  /* Find out where we think we are within the stub.  */
193  if ((insn & 0xffffc00e) == 0x53610000)
194    addr = pc;
195  else if ((insn & 0xffffffff) == 0xe820d000)
196    addr = pc - 4;
197  else if ((insn & 0xffffc00e) == 0x537b0000)
198    addr = pc - 8;
199  else
200    return 0;
201
202  /* Now verify each insn in the range looks like a stub instruction.  */
203  insn = read_memory_integer (addr, 4);
204  if ((insn & 0xffffc00e) != 0x53610000)
205    return 0;
206
207  /* Now verify each insn in the range looks like a stub instruction.  */
208  insn = read_memory_integer (addr + 4, 4);
209  if ((insn & 0xffffffff) != 0xe820d000)
210    return 0;
211
212  /* Now verify each insn in the range looks like a stub instruction.  */
213  insn = read_memory_integer (addr + 8, 4);
214  if ((insn & 0xffffc00e) != 0x537b0000)
215    return 0;
216
217  /* Looks like a stub.  */
218  return 1;
219}
220
221/* Return one if PC is in the return path of a trampoline, else return zero.
222
223   Note we return one for *any* call trampoline (long-call, arg-reloc), not
224   just shared library trampolines (import, export).  */
225
226static int
227hppa_hpux_in_solib_return_trampoline (CORE_ADDR pc, char *name)
228{
229  struct unwind_table_entry *u;
230
231  /* Get the unwind descriptor corresponding to PC, return zero
232     if no unwind was found.  */
233  u = find_unwind_entry (pc);
234  if (!u)
235    return 0;
236
237  /* If this isn't a linker stub or it's just a long branch stub, then
238     return zero.  */
239  if (u->stub_unwind.stub_type == 0 || u->stub_unwind.stub_type == LONG_BRANCH)
240    return 0;
241
242  /* The call and return path execute the same instructions within
243     an IMPORT stub!  So an IMPORT stub is both a call and return
244     trampoline.  */
245  if (u->stub_unwind.stub_type == IMPORT)
246    return 1;
247
248  /* Parameter relocation stubs always have a call path and may have a
249     return path.  */
250  if (u->stub_unwind.stub_type == PARAMETER_RELOCATION
251      || u->stub_unwind.stub_type == EXPORT)
252    {
253      CORE_ADDR addr;
254
255      /* Search forward from the current PC until we hit a branch
256         or the end of the stub.  */
257      for (addr = pc; addr <= u->region_end; addr += 4)
258	{
259	  unsigned long insn;
260
261	  insn = read_memory_integer (addr, 4);
262
263	  /* Does it look like a bl?  If so then it's the call path, if
264	     we find a bv or be first, then we're on the return path.  */
265	  if ((insn & 0xfc00e000) == 0xe8000000)
266	    return 0;
267	  else if ((insn & 0xfc00e001) == 0xe800c000
268		   || (insn & 0xfc000000) == 0xe0000000)
269	    return 1;
270	}
271
272      /* Should never happen.  */
273      warning (_("Unable to find branch in parameter relocation stub."));
274      return 0;
275    }
276
277  /* Unknown stub type.  For now, just return zero.  */
278  return 0;
279
280}
281
282/* Figure out if PC is in a trampoline, and if so find out where
283   the trampoline will jump to.  If not in a trampoline, return zero.
284
285   Simple code examination probably is not a good idea since the code
286   sequences in trampolines can also appear in user code.
287
288   We use unwinds and information from the minimal symbol table to
289   determine when we're in a trampoline.  This won't work for ELF
290   (yet) since it doesn't create stub unwind entries.  Whether or
291   not ELF will create stub unwinds or normal unwinds for linker
292   stubs is still being debated.
293
294   This should handle simple calls through dyncall or sr4export,
295   long calls, argument relocation stubs, and dyncall/sr4export
296   calling an argument relocation stub.  It even handles some stubs
297   used in dynamic executables.  */
298
299static CORE_ADDR
300hppa_hpux_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
301{
302  long orig_pc = pc;
303  long prev_inst, curr_inst, loc;
304  struct minimal_symbol *msym;
305  struct unwind_table_entry *u;
306
307  /* Addresses passed to dyncall may *NOT* be the actual address
308     of the function.  So we may have to do something special.  */
309  if (pc == hppa_symbol_address("$$dyncall"))
310    {
311      pc = (CORE_ADDR) get_frame_register_unsigned (frame, 22);
312
313      /* If bit 30 (counting from the left) is on, then pc is the address of
314         the PLT entry for this function, not the address of the function
315         itself.  Bit 31 has meaning too, but only for MPE.  */
316      if (pc & 0x2)
317	pc = (CORE_ADDR) read_memory_integer
318			   (pc & ~0x3, gdbarch_ptr_bit (current_gdbarch) / 8);
319    }
320  if (pc == hppa_symbol_address("$$dyncall_external"))
321    {
322      pc = (CORE_ADDR) get_frame_register_unsigned (frame, 22);
323      pc = (CORE_ADDR) read_memory_integer
324			 (pc & ~0x3, gdbarch_ptr_bit (current_gdbarch) / 8);
325    }
326  else if (pc == hppa_symbol_address("_sr4export"))
327    pc = (CORE_ADDR) get_frame_register_unsigned (frame, 22);
328
329  /* Get the unwind descriptor corresponding to PC, return zero
330     if no unwind was found.  */
331  u = find_unwind_entry (pc);
332  if (!u)
333    return 0;
334
335  /* If this isn't a linker stub, then return now.  */
336  /* elz: attention here! (FIXME) because of a compiler/linker
337     error, some stubs which should have a non zero stub_unwind.stub_type
338     have unfortunately a value of zero. So this function would return here
339     as if we were not in a trampoline. To fix this, we go look at the partial
340     symbol information, which reports this guy as a stub.
341     (FIXME): Unfortunately, we are not that lucky: it turns out that the
342     partial symbol information is also wrong sometimes. This is because
343     when it is entered (somread.c::som_symtab_read()) it can happen that
344     if the type of the symbol (from the som) is Entry, and the symbol is
345     in a shared library, then it can also be a trampoline.  This would
346     be OK, except that I believe the way they decide if we are ina shared library
347     does not work. SOOOO..., even if we have a regular function w/o trampolines
348     its minimal symbol can be assigned type mst_solib_trampoline.
349     Also, if we find that the symbol is a real stub, then we fix the unwind
350     descriptor, and define the stub type to be EXPORT.
351     Hopefully this is correct most of the times. */
352  if (u->stub_unwind.stub_type == 0)
353    {
354
355/* elz: NOTE (FIXME!) once the problem with the unwind information is fixed
356   we can delete all the code which appears between the lines */
357/*--------------------------------------------------------------------------*/
358      msym = lookup_minimal_symbol_by_pc (pc);
359
360      if (msym == NULL || MSYMBOL_TYPE (msym) != mst_solib_trampoline)
361	return orig_pc == pc ? 0 : pc & ~0x3;
362
363      else if (msym != NULL && MSYMBOL_TYPE (msym) == mst_solib_trampoline)
364	{
365	  struct objfile *objfile;
366	  struct minimal_symbol *msymbol;
367	  int function_found = 0;
368
369	  /* go look if there is another minimal symbol with the same name as
370	     this one, but with type mst_text. This would happen if the msym
371	     is an actual trampoline, in which case there would be another
372	     symbol with the same name corresponding to the real function */
373
374	  ALL_MSYMBOLS (objfile, msymbol)
375	  {
376	    if (MSYMBOL_TYPE (msymbol) == mst_text
377		&& DEPRECATED_STREQ (DEPRECATED_SYMBOL_NAME (msymbol), DEPRECATED_SYMBOL_NAME (msym)))
378	      {
379		function_found = 1;
380		break;
381	      }
382	  }
383
384	  if (function_found)
385	    /* the type of msym is correct (mst_solib_trampoline), but
386	       the unwind info is wrong, so set it to the correct value */
387	    u->stub_unwind.stub_type = EXPORT;
388	  else
389	    /* the stub type info in the unwind is correct (this is not a
390	       trampoline), but the msym type information is wrong, it
391	       should be mst_text. So we need to fix the msym, and also
392	       get out of this function */
393	    {
394	      MSYMBOL_TYPE (msym) = mst_text;
395	      return orig_pc == pc ? 0 : pc & ~0x3;
396	    }
397	}
398
399/*--------------------------------------------------------------------------*/
400    }
401
402  /* It's a stub.  Search for a branch and figure out where it goes.
403     Note we have to handle multi insn branch sequences like ldil;ble.
404     Most (all?) other branches can be determined by examining the contents
405     of certain registers and the stack.  */
406
407  loc = pc;
408  curr_inst = 0;
409  prev_inst = 0;
410  while (1)
411    {
412      /* Make sure we haven't walked outside the range of this stub.  */
413      if (u != find_unwind_entry (loc))
414	{
415	  warning (_("Unable to find branch in linker stub"));
416	  return orig_pc == pc ? 0 : pc & ~0x3;
417	}
418
419      prev_inst = curr_inst;
420      curr_inst = read_memory_integer (loc, 4);
421
422      /* Does it look like a branch external using %r1?  Then it's the
423         branch from the stub to the actual function.  */
424      if ((curr_inst & 0xffe0e000) == 0xe0202000)
425	{
426	  /* Yup.  See if the previous instruction loaded
427	     a value into %r1.  If so compute and return the jump address.  */
428	  if ((prev_inst & 0xffe00000) == 0x20200000)
429	    return (hppa_extract_21 (prev_inst) + hppa_extract_17 (curr_inst)) & ~0x3;
430	  else
431	    {
432	      warning (_("Unable to find ldil X,%%r1 before ble Y(%%sr4,%%r1)."));
433	      return orig_pc == pc ? 0 : pc & ~0x3;
434	    }
435	}
436
437      /* Does it look like a be 0(sr0,%r21)? OR
438         Does it look like a be, n 0(sr0,%r21)? OR
439         Does it look like a bve (r21)? (this is on PA2.0)
440         Does it look like a bve, n(r21)? (this is also on PA2.0)
441         That's the branch from an
442         import stub to an export stub.
443
444         It is impossible to determine the target of the branch via
445         simple examination of instructions and/or data (consider
446         that the address in the plabel may be the address of the
447         bind-on-reference routine in the dynamic loader).
448
449         So we have try an alternative approach.
450
451         Get the name of the symbol at our current location; it should
452         be a stub symbol with the same name as the symbol in the
453         shared library.
454
455         Then lookup a minimal symbol with the same name; we should
456         get the minimal symbol for the target routine in the shared
457         library as those take precedence of import/export stubs.  */
458      if ((curr_inst == 0xe2a00000) ||
459	  (curr_inst == 0xe2a00002) ||
460	  (curr_inst == 0xeaa0d000) ||
461	  (curr_inst == 0xeaa0d002))
462	{
463	  struct minimal_symbol *stubsym, *libsym;
464
465	  stubsym = lookup_minimal_symbol_by_pc (loc);
466	  if (stubsym == NULL)
467	    {
468	      warning (_("Unable to find symbol for 0x%lx"), loc);
469	      return orig_pc == pc ? 0 : pc & ~0x3;
470	    }
471
472	  libsym = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (stubsym), NULL, NULL);
473	  if (libsym == NULL)
474	    {
475	      warning (_("Unable to find library symbol for %s."),
476		       DEPRECATED_SYMBOL_NAME (stubsym));
477	      return orig_pc == pc ? 0 : pc & ~0x3;
478	    }
479
480	  return SYMBOL_VALUE (libsym);
481	}
482
483      /* Does it look like bl X,%rp or bl X,%r0?  Another way to do a
484         branch from the stub to the actual function.  */
485      /*elz */
486      else if ((curr_inst & 0xffe0e000) == 0xe8400000
487	       || (curr_inst & 0xffe0e000) == 0xe8000000
488	       || (curr_inst & 0xffe0e000) == 0xe800A000)
489	return (loc + hppa_extract_17 (curr_inst) + 8) & ~0x3;
490
491      /* Does it look like bv (rp)?   Note this depends on the
492         current stack pointer being the same as the stack
493         pointer in the stub itself!  This is a branch on from the
494         stub back to the original caller.  */
495      /*else if ((curr_inst & 0xffe0e000) == 0xe840c000) */
496      else if ((curr_inst & 0xffe0f000) == 0xe840c000)
497	{
498	  /* Yup.  See if the previous instruction loaded
499	     rp from sp - 8.  */
500	  if (prev_inst == 0x4bc23ff1)
501	    {
502	      CORE_ADDR sp;
503	      sp = get_frame_register_unsigned (frame, HPPA_SP_REGNUM);
504	      return read_memory_integer (sp - 8, 4) & ~0x3;
505	    }
506	  else
507	    {
508	      warning (_("Unable to find restore of %%rp before bv (%%rp)."));
509	      return orig_pc == pc ? 0 : pc & ~0x3;
510	    }
511	}
512
513      /* elz: added this case to capture the new instruction
514         at the end of the return part of an export stub used by
515         the PA2.0: BVE, n (rp) */
516      else if ((curr_inst & 0xffe0f000) == 0xe840d000)
517	{
518	  return (read_memory_integer
519		  (get_frame_register_unsigned (frame, HPPA_SP_REGNUM) - 24,
520		   gdbarch_ptr_bit (current_gdbarch) / 8)) & ~0x3;
521	}
522
523      /* What about be,n 0(sr0,%rp)?  It's just another way we return to
524         the original caller from the stub.  Used in dynamic executables.  */
525      else if (curr_inst == 0xe0400002)
526	{
527	  /* The value we jump to is sitting in sp - 24.  But that's
528	     loaded several instructions before the be instruction.
529	     I guess we could check for the previous instruction being
530	     mtsp %r1,%sr0 if we want to do sanity checking.  */
531	  return (read_memory_integer
532		  (get_frame_register_unsigned (frame, HPPA_SP_REGNUM) - 24,
533		   gdbarch_ptr_bit (current_gdbarch) / 8)) & ~0x3;
534	}
535
536      /* Haven't found the branch yet, but we're still in the stub.
537         Keep looking.  */
538      loc += 4;
539    }
540}
541
542static void
543hppa_skip_permanent_breakpoint (struct regcache *regcache)
544{
545  /* To step over a breakpoint instruction on the PA takes some
546     fiddling with the instruction address queue.
547
548     When we stop at a breakpoint, the IA queue front (the instruction
549     we're executing now) points at the breakpoint instruction, and
550     the IA queue back (the next instruction to execute) points to
551     whatever instruction we would execute after the breakpoint, if it
552     were an ordinary instruction.  This is the case even if the
553     breakpoint is in the delay slot of a branch instruction.
554
555     Clearly, to step past the breakpoint, we need to set the queue
556     front to the back.  But what do we put in the back?  What
557     instruction comes after that one?  Because of the branch delay
558     slot, the next insn is always at the back + 4.  */
559
560  ULONGEST pcoq_tail, pcsq_tail;
561  regcache_cooked_read_unsigned (regcache, HPPA_PCOQ_TAIL_REGNUM, &pcoq_tail);
562  regcache_cooked_read_unsigned (regcache, HPPA_PCSQ_TAIL_REGNUM, &pcsq_tail);
563
564  regcache_cooked_write_unsigned (regcache, HPPA_PCOQ_HEAD_REGNUM, pcoq_tail);
565  regcache_cooked_write_unsigned (regcache, HPPA_PCSQ_HEAD_REGNUM, pcsq_tail);
566
567  regcache_cooked_write_unsigned (regcache, HPPA_PCOQ_TAIL_REGNUM, pcoq_tail + 4);
568  /* We can leave the tail's space the same, since there's no jump.  */
569}
570
571
572/* Signal frames.  */
573struct hppa_hpux_sigtramp_unwind_cache
574{
575  CORE_ADDR base;
576  struct trad_frame_saved_reg *saved_regs;
577};
578
579static int hppa_hpux_tramp_reg[] = {
580  HPPA_SAR_REGNUM,
581  HPPA_PCOQ_HEAD_REGNUM,
582  HPPA_PCSQ_HEAD_REGNUM,
583  HPPA_PCOQ_TAIL_REGNUM,
584  HPPA_PCSQ_TAIL_REGNUM,
585  HPPA_EIEM_REGNUM,
586  HPPA_IIR_REGNUM,
587  HPPA_ISR_REGNUM,
588  HPPA_IOR_REGNUM,
589  HPPA_IPSW_REGNUM,
590  -1,
591  HPPA_SR4_REGNUM,
592  HPPA_SR4_REGNUM + 1,
593  HPPA_SR4_REGNUM + 2,
594  HPPA_SR4_REGNUM + 3,
595  HPPA_SR4_REGNUM + 4,
596  HPPA_SR4_REGNUM + 5,
597  HPPA_SR4_REGNUM + 6,
598  HPPA_SR4_REGNUM + 7,
599  HPPA_RCR_REGNUM,
600  HPPA_PID0_REGNUM,
601  HPPA_PID1_REGNUM,
602  HPPA_CCR_REGNUM,
603  HPPA_PID2_REGNUM,
604  HPPA_PID3_REGNUM,
605  HPPA_TR0_REGNUM,
606  HPPA_TR0_REGNUM + 1,
607  HPPA_TR0_REGNUM + 2,
608  HPPA_CR27_REGNUM
609};
610
611static struct hppa_hpux_sigtramp_unwind_cache *
612hppa_hpux_sigtramp_frame_unwind_cache (struct frame_info *next_frame,
613				       void **this_cache)
614
615{
616  struct gdbarch *gdbarch = get_frame_arch (next_frame);
617  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
618  struct hppa_hpux_sigtramp_unwind_cache *info;
619  unsigned int flag;
620  CORE_ADDR sp, scptr, off;
621  int i, incr, szoff;
622
623  if (*this_cache)
624    return *this_cache;
625
626  info = FRAME_OBSTACK_ZALLOC (struct hppa_hpux_sigtramp_unwind_cache);
627  *this_cache = info;
628  info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
629
630  sp = frame_unwind_register_unsigned (next_frame, HPPA_SP_REGNUM);
631
632  if (IS_32BIT_TARGET (gdbarch))
633    scptr = sp - 1352;
634  else
635    scptr = sp - 1520;
636
637  off = scptr;
638
639  /* See /usr/include/machine/save_state.h for the structure of the save_state_t
640     structure. */
641
642  flag = read_memory_unsigned_integer(scptr + HPPA_HPUX_SS_FLAGS_OFFSET, 4);
643
644  if (!(flag & HPPA_HPUX_SS_WIDEREGS))
645    {
646      /* Narrow registers. */
647      off = scptr + HPPA_HPUX_SS_NARROW_OFFSET;
648      incr = 4;
649      szoff = 0;
650    }
651  else
652    {
653      /* Wide registers. */
654      off = scptr + HPPA_HPUX_SS_WIDE_OFFSET + 8;
655      incr = 8;
656      szoff = (tdep->bytes_per_address == 4 ? 4 : 0);
657    }
658
659  for (i = 1; i < 32; i++)
660    {
661      info->saved_regs[HPPA_R0_REGNUM + i].addr = off + szoff;
662      off += incr;
663    }
664
665  for (i = 0; i < ARRAY_SIZE (hppa_hpux_tramp_reg); i++)
666    {
667      if (hppa_hpux_tramp_reg[i] > 0)
668        info->saved_regs[hppa_hpux_tramp_reg[i]].addr = off + szoff;
669
670      off += incr;
671    }
672
673  /* TODO: fp regs */
674
675  info->base = frame_unwind_register_unsigned (next_frame, HPPA_SP_REGNUM);
676
677  return info;
678}
679
680static void
681hppa_hpux_sigtramp_frame_this_id (struct frame_info *next_frame,
682				   void **this_prologue_cache,
683				   struct frame_id *this_id)
684{
685  struct hppa_hpux_sigtramp_unwind_cache *info
686    = hppa_hpux_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
687  *this_id = frame_id_build (info->base, frame_pc_unwind (next_frame));
688}
689
690static void
691hppa_hpux_sigtramp_frame_prev_register (struct frame_info *next_frame,
692					void **this_prologue_cache,
693					int regnum, int *optimizedp,
694					enum lval_type *lvalp,
695					CORE_ADDR *addrp,
696					int *realnump, gdb_byte *valuep)
697{
698  struct hppa_hpux_sigtramp_unwind_cache *info
699    = hppa_hpux_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
700  hppa_frame_prev_register_helper (next_frame, info->saved_regs, regnum,
701		                   optimizedp, lvalp, addrp, realnump, valuep);
702}
703
704static const struct frame_unwind hppa_hpux_sigtramp_frame_unwind = {
705  SIGTRAMP_FRAME,
706  hppa_hpux_sigtramp_frame_this_id,
707  hppa_hpux_sigtramp_frame_prev_register
708};
709
710static const struct frame_unwind *
711hppa_hpux_sigtramp_unwind_sniffer (struct frame_info *next_frame)
712{
713  struct unwind_table_entry *u;
714  CORE_ADDR pc = frame_pc_unwind (next_frame);
715
716  u = find_unwind_entry (pc);
717
718  /* If this is an export stub, try to get the unwind descriptor for
719     the actual function itself.  */
720  if (u && u->stub_unwind.stub_type == EXPORT)
721    {
722      gdb_byte buf[HPPA_INSN_SIZE];
723      unsigned long insn;
724
725      if (!safe_frame_unwind_memory (next_frame, u->region_start,
726				     buf, sizeof buf))
727	return NULL;
728
729      insn = extract_unsigned_integer (buf, sizeof buf);
730      if ((insn & 0xffe0e000) == 0xe8400000)
731	u = find_unwind_entry(u->region_start + hppa_extract_17 (insn) + 8);
732    }
733
734  if (u && u->HP_UX_interrupt_marker)
735    return &hppa_hpux_sigtramp_frame_unwind;
736
737  return NULL;
738}
739
740static CORE_ADDR
741hppa32_hpux_find_global_pointer (struct value *function)
742{
743  CORE_ADDR faddr;
744
745  faddr = value_as_address (function);
746
747  /* Is this a plabel? If so, dereference it to get the gp value.  */
748  if (faddr & 2)
749    {
750      int status;
751      char buf[4];
752
753      faddr &= ~3;
754
755      status = target_read_memory (faddr + 4, buf, sizeof (buf));
756      if (status == 0)
757	return extract_unsigned_integer (buf, sizeof (buf));
758    }
759
760  return gdbarch_tdep (current_gdbarch)->solib_get_got_by_pc (faddr);
761}
762
763static CORE_ADDR
764hppa64_hpux_find_global_pointer (struct value *function)
765{
766  CORE_ADDR faddr;
767  char buf[32];
768
769  faddr = value_as_address (function);
770
771  if (in_opd_section (faddr))
772    {
773      target_read_memory (faddr, buf, sizeof (buf));
774      return extract_unsigned_integer (&buf[24], 8);
775    }
776  else
777    {
778      return gdbarch_tdep (current_gdbarch)->solib_get_got_by_pc (faddr);
779    }
780}
781
782static unsigned int ldsid_pattern[] = {
783  0x000010a0, /* ldsid (rX),rY */
784  0x00001820, /* mtsp rY,sr0 */
785  0xe0000000  /* be,n (sr0,rX) */
786};
787
788static CORE_ADDR
789hppa_hpux_search_pattern (CORE_ADDR start, CORE_ADDR end,
790			  unsigned int *patterns, int count)
791{
792  int num_insns = (end - start + HPPA_INSN_SIZE) / HPPA_INSN_SIZE;
793  unsigned int *insns;
794  gdb_byte *buf;
795  int offset, i;
796
797  buf = alloca (num_insns * HPPA_INSN_SIZE);
798  insns = alloca (num_insns * sizeof (unsigned int));
799
800  read_memory (start, buf, num_insns * HPPA_INSN_SIZE);
801  for (i = 0; i < num_insns; i++, buf += HPPA_INSN_SIZE)
802    insns[i] = extract_unsigned_integer (buf, HPPA_INSN_SIZE);
803
804  for (offset = 0; offset <= num_insns - count; offset++)
805    {
806      for (i = 0; i < count; i++)
807        {
808	  if ((insns[offset + i] & patterns[i]) != patterns[i])
809	    break;
810	}
811      if (i == count)
812        break;
813    }
814
815  if (offset <= num_insns - count)
816    return start + offset * HPPA_INSN_SIZE;
817  else
818    return 0;
819}
820
821static CORE_ADDR
822hppa32_hpux_search_dummy_call_sequence (struct gdbarch *gdbarch, CORE_ADDR pc,
823					int *argreg)
824{
825  struct objfile *obj;
826  struct obj_section *sec;
827  struct hppa_objfile_private *priv;
828  struct frame_info *frame;
829  struct unwind_table_entry *u;
830  CORE_ADDR addr, rp;
831  char buf[4];
832  unsigned int insn;
833
834  sec = find_pc_section (pc);
835  obj = sec->objfile;
836  priv = objfile_data (obj, hppa_objfile_priv_data);
837
838  if (!priv)
839    priv = hppa_init_objfile_priv_data (obj);
840  if (!priv)
841    error (_("Internal error creating objfile private data."));
842
843  /* Use the cached value if we have one.  */
844  if (priv->dummy_call_sequence_addr != 0)
845    {
846      *argreg = priv->dummy_call_sequence_reg;
847      return priv->dummy_call_sequence_addr;
848    }
849
850  /* First try a heuristic; if we are in a shared library call, our return
851     pointer is likely to point at an export stub.  */
852  frame = get_current_frame ();
853  rp = frame_unwind_register_unsigned (frame, 2);
854  u = find_unwind_entry (rp);
855  if (u && u->stub_unwind.stub_type == EXPORT)
856    {
857      addr = hppa_hpux_search_pattern (u->region_start, u->region_end,
858				       ldsid_pattern,
859				       ARRAY_SIZE (ldsid_pattern));
860      if (addr)
861	goto found_pattern;
862    }
863
864  /* Next thing to try is to look for an export stub.  */
865  if (priv->unwind_info)
866    {
867      int i;
868
869      for (i = 0; i < priv->unwind_info->last; i++)
870        {
871	  struct unwind_table_entry *u;
872	  u = &priv->unwind_info->table[i];
873	  if (u->stub_unwind.stub_type == EXPORT)
874	    {
875	      addr = hppa_hpux_search_pattern (u->region_start, u->region_end,
876					       ldsid_pattern,
877					       ARRAY_SIZE (ldsid_pattern));
878	      if (addr)
879	        {
880		  goto found_pattern;
881		}
882	    }
883	}
884    }
885
886  /* Finally, if this is the main executable, try to locate a sequence
887     from noshlibs */
888  addr = hppa_symbol_address ("noshlibs");
889  sec = find_pc_section (addr);
890
891  if (sec && sec->objfile == obj)
892    {
893      CORE_ADDR start, end;
894
895      find_pc_partial_function (addr, NULL, &start, &end);
896      if (start != 0 && end != 0)
897        {
898	  addr = hppa_hpux_search_pattern (start, end, ldsid_pattern,
899					   ARRAY_SIZE (ldsid_pattern));
900	  if (addr)
901	    goto found_pattern;
902        }
903    }
904
905  /* Can't find a suitable sequence.  */
906  return 0;
907
908found_pattern:
909  target_read_memory (addr, buf, sizeof (buf));
910  insn = extract_unsigned_integer (buf, sizeof (buf));
911  priv->dummy_call_sequence_addr = addr;
912  priv->dummy_call_sequence_reg = (insn >> 21) & 0x1f;
913
914  *argreg = priv->dummy_call_sequence_reg;
915  return priv->dummy_call_sequence_addr;
916}
917
918static CORE_ADDR
919hppa64_hpux_search_dummy_call_sequence (struct gdbarch *gdbarch, CORE_ADDR pc,
920					int *argreg)
921{
922  struct objfile *obj;
923  struct obj_section *sec;
924  struct hppa_objfile_private *priv;
925  CORE_ADDR addr;
926  struct minimal_symbol *msym;
927  int i;
928
929  sec = find_pc_section (pc);
930  obj = sec->objfile;
931  priv = objfile_data (obj, hppa_objfile_priv_data);
932
933  if (!priv)
934    priv = hppa_init_objfile_priv_data (obj);
935  if (!priv)
936    error (_("Internal error creating objfile private data."));
937
938  /* Use the cached value if we have one.  */
939  if (priv->dummy_call_sequence_addr != 0)
940    {
941      *argreg = priv->dummy_call_sequence_reg;
942      return priv->dummy_call_sequence_addr;
943    }
944
945  /* FIXME: Without stub unwind information, locating a suitable sequence is
946     fairly difficult.  For now, we implement a very naive and inefficient
947     scheme; try to read in blocks of code, and look for a "bve,n (rp)"
948     instruction.  These are likely to occur at the end of functions, so
949     we only look at the last two instructions of each function.  */
950  for (i = 0, msym = obj->msymbols; i < obj->minimal_symbol_count; i++, msym++)
951    {
952      CORE_ADDR begin, end;
953      char *name;
954      gdb_byte buf[2 * HPPA_INSN_SIZE];
955      int offset;
956
957      find_pc_partial_function (SYMBOL_VALUE_ADDRESS (msym), &name,
958      				&begin, &end);
959
960      if (name == NULL || begin == 0 || end == 0)
961        continue;
962
963      if (target_read_memory (end - sizeof (buf), buf, sizeof (buf)) == 0)
964        {
965	  for (offset = 0; offset < sizeof (buf); offset++)
966	    {
967	      unsigned int insn;
968
969	      insn = extract_unsigned_integer (buf + offset, HPPA_INSN_SIZE);
970	      if (insn == 0xe840d002) /* bve,n (rp) */
971	        {
972		  addr = (end - sizeof (buf)) + offset;
973		  goto found_pattern;
974		}
975	    }
976	}
977    }
978
979  /* Can't find a suitable sequence.  */
980  return 0;
981
982found_pattern:
983  priv->dummy_call_sequence_addr = addr;
984  /* Right now we only look for a "bve,l (rp)" sequence, so the register is
985     always HPPA_RP_REGNUM.  */
986  priv->dummy_call_sequence_reg = HPPA_RP_REGNUM;
987
988  *argreg = priv->dummy_call_sequence_reg;
989  return priv->dummy_call_sequence_addr;
990}
991
992static CORE_ADDR
993hppa_hpux_find_import_stub_for_addr (CORE_ADDR funcaddr)
994{
995  struct objfile *objfile;
996  struct minimal_symbol *funsym, *stubsym;
997  CORE_ADDR stubaddr;
998
999  funsym = lookup_minimal_symbol_by_pc (funcaddr);
1000  stubaddr = 0;
1001
1002  ALL_OBJFILES (objfile)
1003    {
1004      stubsym = lookup_minimal_symbol_solib_trampoline
1005	(SYMBOL_LINKAGE_NAME (funsym), objfile);
1006
1007      if (stubsym)
1008	{
1009	  struct unwind_table_entry *u;
1010
1011	  u = find_unwind_entry (SYMBOL_VALUE (stubsym));
1012	  if (u == NULL
1013	      || (u->stub_unwind.stub_type != IMPORT
1014		  && u->stub_unwind.stub_type != IMPORT_SHLIB))
1015	    continue;
1016
1017          stubaddr = SYMBOL_VALUE (stubsym);
1018
1019	  /* If we found an IMPORT stub, then we can stop searching;
1020	     if we found an IMPORT_SHLIB, we want to continue the search
1021	     in the hopes that we will find an IMPORT stub.  */
1022	  if (u->stub_unwind.stub_type == IMPORT)
1023	    break;
1024	}
1025    }
1026
1027  return stubaddr;
1028}
1029
1030static int
1031hppa_hpux_sr_for_addr (CORE_ADDR addr)
1032{
1033  int sr;
1034  /* The space register to use is encoded in the top 2 bits of the address.  */
1035  sr = addr >> (gdbarch_tdep (current_gdbarch)->bytes_per_address * 8 - 2);
1036  return sr + 4;
1037}
1038
1039static CORE_ADDR
1040hppa_hpux_find_dummy_bpaddr (CORE_ADDR addr)
1041{
1042  /* In order for us to restore the space register to its starting state,
1043     we need the dummy trampoline to return to the an instruction address in
1044     the same space as where we started the call.  We used to place the
1045     breakpoint near the current pc, however, this breaks nested dummy calls
1046     as the nested call will hit the breakpoint address and terminate
1047     prematurely.  Instead, we try to look for an address in the same space to
1048     put the breakpoint.
1049
1050     This is similar in spirit to putting the breakpoint at the "entry point"
1051     of an executable.  */
1052
1053  struct obj_section *sec;
1054  struct unwind_table_entry *u;
1055  struct minimal_symbol *msym;
1056  CORE_ADDR func;
1057  int i;
1058
1059  sec = find_pc_section (addr);
1060  if (sec)
1061    {
1062      /* First try the lowest address in the section; we can use it as long
1063         as it is "regular" code (i.e. not a stub) */
1064      u = find_unwind_entry (sec->addr);
1065      if (!u || u->stub_unwind.stub_type == 0)
1066        return sec->addr;
1067
1068      /* Otherwise, we need to find a symbol for a regular function.  We
1069         do this by walking the list of msymbols in the objfile.  The symbol
1070	 we find should not be the same as the function that was passed in.  */
1071
1072      /* FIXME: this is broken, because we can find a function that will be
1073         called by the dummy call target function, which will still not
1074	 work.  */
1075
1076      find_pc_partial_function (addr, NULL, &func, NULL);
1077      for (i = 0, msym = sec->objfile->msymbols;
1078      	   i < sec->objfile->minimal_symbol_count;
1079	   i++, msym++)
1080	{
1081	  u = find_unwind_entry (SYMBOL_VALUE_ADDRESS (msym));
1082	  if (func != SYMBOL_VALUE_ADDRESS (msym)
1083	      && (!u || u->stub_unwind.stub_type == 0))
1084	    return SYMBOL_VALUE_ADDRESS (msym);
1085	}
1086    }
1087
1088  warning (_("Cannot find suitable address to place dummy breakpoint; nested "
1089	     "calls may fail."));
1090  return addr - 4;
1091}
1092
1093static CORE_ADDR
1094hppa_hpux_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
1095			   CORE_ADDR funcaddr, int using_gcc,
1096			   struct value **args, int nargs,
1097			   struct type *value_type,
1098			   CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
1099			   struct regcache *regcache)
1100{
1101  CORE_ADDR pc, stubaddr;
1102  int argreg = 0;
1103
1104  pc = read_pc ();
1105
1106  /* Note: we don't want to pass a function descriptor here; push_dummy_call
1107     fills in the PIC register for us.  */
1108  funcaddr = gdbarch_convert_from_func_ptr_addr (gdbarch, funcaddr, NULL);
1109
1110  /* The simple case is where we call a function in the same space that we are
1111     currently in; in that case we don't really need to do anything.  */
1112  if (hppa_hpux_sr_for_addr (pc) == hppa_hpux_sr_for_addr (funcaddr))
1113    {
1114      /* Intraspace call.  */
1115      *bp_addr = hppa_hpux_find_dummy_bpaddr (pc);
1116      *real_pc = funcaddr;
1117      regcache_cooked_write_unsigned (regcache, HPPA_RP_REGNUM, *bp_addr);
1118
1119      return sp;
1120    }
1121
1122  /* In order to make an interspace call, we need to go through a stub.
1123     gcc supplies an appropriate stub called "__gcc_plt_call", however, if
1124     an application is compiled with HP compilers then this stub is not
1125     available.  We used to fallback to "__d_plt_call", however that stub
1126     is not entirely useful for us because it doesn't do an interspace
1127     return back to the caller.  Also, on hppa64-hpux, there is no
1128     __gcc_plt_call available.  In order to keep the code uniform, we
1129     instead don't use either of these stubs, but instead write our own
1130     onto the stack.
1131
1132     A problem arises since the stack is located in a different space than
1133     code, so in order to branch to a stack stub, we will need to do an
1134     interspace branch.  Previous versions of gdb did this by modifying code
1135     at the current pc and doing single-stepping to set the pcsq.  Since this
1136     is highly undesirable, we use a different scheme:
1137
1138     All we really need to do the branch to the stub is a short instruction
1139     sequence like this:
1140
1141     PA1.1:
1142      		ldsid (rX),r1
1143		mtsp r1,sr0
1144		be,n (sr0,rX)
1145
1146     PA2.0:
1147      		bve,n (sr0,rX)
1148
1149     Instead of writing these sequences ourselves, we can find it in
1150     the instruction stream that belongs to the current space.  While this
1151     seems difficult at first, we are actually guaranteed to find the sequences
1152     in several places:
1153
1154     For 32-bit code:
1155     - in export stubs for shared libraries
1156     - in the "noshlibs" routine in the main module
1157
1158     For 64-bit code:
1159     - at the end of each "regular" function
1160
1161     We cache the address of these sequences in the objfile's private data
1162     since these operations can potentially be quite expensive.
1163
1164     So, what we do is:
1165     - write a stack trampoline
1166     - look for a suitable instruction sequence in the current space
1167     - point the sequence at the trampoline
1168     - set the return address of the trampoline to the current space
1169       (see hppa_hpux_find_dummy_call_bpaddr)
1170     - set the continuing address of the "dummy code" as the sequence.
1171
1172*/
1173
1174  if (IS_32BIT_TARGET (gdbarch))
1175    {
1176      static unsigned int hppa32_tramp[] = {
1177        0x0fdf1291, /* stw r31,-8(,sp) */
1178        0x02c010a1, /* ldsid (,r22),r1 */
1179        0x00011820, /* mtsp r1,sr0 */
1180        0xe6c00000, /* be,l 0(sr0,r22),%sr0,%r31 */
1181        0x081f0242, /* copy r31,rp */
1182        0x0fd11082, /* ldw -8(,sp),rp */
1183        0x004010a1, /* ldsid (,rp),r1 */
1184        0x00011820, /* mtsp r1,sr0 */
1185        0xe0400000, /* be 0(sr0,rp) */
1186        0x08000240  /* nop */
1187      };
1188
1189      /* for hppa32, we must call the function through a stub so that on
1190         return it can return to the space of our trampoline.  */
1191      stubaddr = hppa_hpux_find_import_stub_for_addr (funcaddr);
1192      if (stubaddr == 0)
1193        error (_("Cannot call external function not referenced by application "
1194	       "(no import stub).\n"));
1195      regcache_cooked_write_unsigned (regcache, 22, stubaddr);
1196
1197      write_memory (sp, (char *)&hppa32_tramp, sizeof (hppa32_tramp));
1198
1199      *bp_addr = hppa_hpux_find_dummy_bpaddr (pc);
1200      regcache_cooked_write_unsigned (regcache, 31, *bp_addr);
1201
1202      *real_pc = hppa32_hpux_search_dummy_call_sequence (gdbarch, pc, &argreg);
1203      if (*real_pc == 0)
1204        error (_("Cannot make interspace call from here."));
1205
1206      regcache_cooked_write_unsigned (regcache, argreg, sp);
1207
1208      sp += sizeof (hppa32_tramp);
1209    }
1210  else
1211    {
1212      static unsigned int hppa64_tramp[] = {
1213        0xeac0f000, /* bve,l (r22),%r2 */
1214        0x0fdf12d1, /* std r31,-8(,sp) */
1215        0x0fd110c2, /* ldd -8(,sp),rp */
1216        0xe840d002, /* bve,n (rp) */
1217        0x08000240  /* nop */
1218      };
1219
1220      /* for hppa64, we don't need to call through a stub; all functions
1221         return via a bve.  */
1222      regcache_cooked_write_unsigned (regcache, 22, funcaddr);
1223      write_memory (sp, (char *)&hppa64_tramp, sizeof (hppa64_tramp));
1224
1225      *bp_addr = pc - 4;
1226      regcache_cooked_write_unsigned (regcache, 31, *bp_addr);
1227
1228      *real_pc = hppa64_hpux_search_dummy_call_sequence (gdbarch, pc, &argreg);
1229      if (*real_pc == 0)
1230        error (_("Cannot make interspace call from here."));
1231
1232      regcache_cooked_write_unsigned (regcache, argreg, sp);
1233
1234      sp += sizeof (hppa64_tramp);
1235    }
1236
1237  sp = gdbarch_frame_align (gdbarch, sp);
1238
1239  return sp;
1240}
1241
1242
1243
1244static void
1245hppa_hpux_supply_ss_narrow (struct regcache *regcache,
1246			    int regnum, const char *save_state)
1247{
1248  const char *ss_narrow = save_state + HPPA_HPUX_SS_NARROW_OFFSET;
1249  int i, offset = 0;
1250
1251  for (i = HPPA_R1_REGNUM; i < HPPA_FP0_REGNUM; i++)
1252    {
1253      if (regnum == i || regnum == -1)
1254	regcache_raw_supply (regcache, i, ss_narrow + offset);
1255
1256      offset += 4;
1257    }
1258}
1259
1260static void
1261hppa_hpux_supply_ss_fpblock (struct regcache *regcache,
1262			     int regnum, const char *save_state)
1263{
1264  const char *ss_fpblock = save_state + HPPA_HPUX_SS_FPBLOCK_OFFSET;
1265  int i, offset = 0;
1266
1267  /* FIXME: We view the floating-point state as 64 single-precision
1268     registers for 32-bit code, and 32 double-precision register for
1269     64-bit code.  This distinction is artificial and should be
1270     eliminated.  If that ever happens, we should remove the if-clause
1271     below.  */
1272
1273  if (register_size (get_regcache_arch (regcache), HPPA_FP0_REGNUM) == 4)
1274    {
1275      for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 64; i++)
1276	{
1277	  if (regnum == i || regnum == -1)
1278	    regcache_raw_supply (regcache, i, ss_fpblock + offset);
1279
1280	  offset += 4;
1281	}
1282    }
1283  else
1284    {
1285      for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 32; i++)
1286	{
1287	  if (regnum == i || regnum == -1)
1288	    regcache_raw_supply (regcache, i, ss_fpblock + offset);
1289
1290	  offset += 8;
1291	}
1292    }
1293}
1294
1295static void
1296hppa_hpux_supply_ss_wide (struct regcache *regcache,
1297			  int regnum, const char *save_state)
1298{
1299  const char *ss_wide = save_state + HPPA_HPUX_SS_WIDE_OFFSET;
1300  int i, offset = 8;
1301
1302  if (register_size (get_regcache_arch (regcache), HPPA_R1_REGNUM) == 4)
1303    offset += 4;
1304
1305  for (i = HPPA_R1_REGNUM; i < HPPA_FP0_REGNUM; i++)
1306    {
1307      if (regnum == i || regnum == -1)
1308	regcache_raw_supply (regcache, i, ss_wide + offset);
1309
1310      offset += 8;
1311    }
1312}
1313
1314static void
1315hppa_hpux_supply_save_state (const struct regset *regset,
1316			     struct regcache *regcache,
1317			     int regnum, const void *regs, size_t len)
1318{
1319  const char *proc_info = regs;
1320  const char *save_state = proc_info + 8;
1321  ULONGEST flags;
1322
1323  flags = extract_unsigned_integer (save_state + HPPA_HPUX_SS_FLAGS_OFFSET, 4);
1324  if (regnum == -1 || regnum == HPPA_FLAGS_REGNUM)
1325    {
1326      struct gdbarch *arch = get_regcache_arch (regcache);
1327      size_t size = register_size (arch, HPPA_FLAGS_REGNUM);
1328      char buf[8];
1329
1330      store_unsigned_integer (buf, size, flags);
1331      regcache_raw_supply (regcache, HPPA_FLAGS_REGNUM, buf);
1332    }
1333
1334  /* If the SS_WIDEREGS flag is set, we really do need the full
1335     `struct save_state'.  */
1336  if (flags & HPPA_HPUX_SS_WIDEREGS && len < HPPA_HPUX_SAVE_STATE_SIZE)
1337    error (_("Register set contents too small"));
1338
1339  if (flags & HPPA_HPUX_SS_WIDEREGS)
1340    hppa_hpux_supply_ss_wide (regcache, regnum, save_state);
1341  else
1342    hppa_hpux_supply_ss_narrow (regcache, regnum, save_state);
1343
1344  hppa_hpux_supply_ss_fpblock (regcache, regnum, save_state);
1345}
1346
1347/* HP-UX register set.  */
1348
1349static struct regset hppa_hpux_regset =
1350{
1351  NULL,
1352  hppa_hpux_supply_save_state
1353};
1354
1355static const struct regset *
1356hppa_hpux_regset_from_core_section (struct gdbarch *gdbarch,
1357				    const char *sect_name, size_t sect_size)
1358{
1359  if (strcmp (sect_name, ".reg") == 0
1360      && sect_size >= HPPA_HPUX_PA89_SAVE_STATE_SIZE + 8)
1361    return &hppa_hpux_regset;
1362
1363  return NULL;
1364}
1365
1366
1367/* Bit in the `ss_flag' member of `struct save_state' that indicates
1368   the state was saved from a system call.  From
1369   <machine/save_state.h>.  */
1370#define HPPA_HPUX_SS_INSYSCALL	0x02
1371
1372static CORE_ADDR
1373hppa_hpux_read_pc (struct regcache *regcache)
1374{
1375  ULONGEST flags;
1376
1377  /* If we're currently in a system call return the contents of %r31.  */
1378  regcache_cooked_read_unsigned (regcache, HPPA_FLAGS_REGNUM, &flags);
1379  if (flags & HPPA_HPUX_SS_INSYSCALL)
1380    {
1381      ULONGEST pc;
1382      regcache_cooked_read_unsigned (regcache, HPPA_R31_REGNUM, &pc);
1383      return pc & ~0x3;
1384    }
1385
1386  return hppa_read_pc (regcache);
1387}
1388
1389static void
1390hppa_hpux_write_pc (struct regcache *regcache, CORE_ADDR pc)
1391{
1392  ULONGEST flags;
1393
1394  /* If we're currently in a system call also write PC into %r31.  */
1395  regcache_cooked_read_unsigned (regcache, HPPA_FLAGS_REGNUM, &flags);
1396  if (flags & HPPA_HPUX_SS_INSYSCALL)
1397    regcache_cooked_write_unsigned (regcache, HPPA_R31_REGNUM, pc | 0x3);
1398
1399  return hppa_write_pc (regcache, pc);
1400}
1401
1402static CORE_ADDR
1403hppa_hpux_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1404{
1405  ULONGEST flags;
1406
1407  /* If we're currently in a system call return the contents of %r31.  */
1408  flags = frame_unwind_register_unsigned (next_frame, HPPA_FLAGS_REGNUM);
1409  if (flags & HPPA_HPUX_SS_INSYSCALL)
1410    return frame_unwind_register_unsigned (next_frame, HPPA_R31_REGNUM) & ~0x3;
1411
1412  return hppa_unwind_pc (gdbarch, next_frame);
1413}
1414
1415
1416/* Given the current value of the pc, check to see if it is inside a stub, and
1417   if so, change the value of the pc to point to the caller of the stub.
1418   NEXT_FRAME is the next frame in the current list of frames.
1419   BASE contains to stack frame base of the current frame.
1420   SAVE_REGS is the register file stored in the frame cache. */
1421static void
1422hppa_hpux_unwind_adjust_stub (struct frame_info *next_frame, CORE_ADDR base,
1423			      struct trad_frame_saved_reg *saved_regs)
1424{
1425  int optimized, realreg;
1426  enum lval_type lval;
1427  CORE_ADDR addr;
1428  char buffer[sizeof(ULONGEST)];
1429  ULONGEST val;
1430  CORE_ADDR stubpc;
1431  struct unwind_table_entry *u;
1432
1433  trad_frame_get_prev_register (next_frame, saved_regs,
1434				HPPA_PCOQ_HEAD_REGNUM,
1435				&optimized, &lval, &addr, &realreg, buffer);
1436  val = extract_unsigned_integer (buffer,
1437				  register_size (get_frame_arch (next_frame),
1438      				  		 HPPA_PCOQ_HEAD_REGNUM));
1439
1440  u = find_unwind_entry (val);
1441  if (u && u->stub_unwind.stub_type == EXPORT)
1442    {
1443      stubpc = read_memory_integer
1444		 (base - 24, gdbarch_ptr_bit (current_gdbarch) / 8);
1445      trad_frame_set_value (saved_regs, HPPA_PCOQ_HEAD_REGNUM, stubpc);
1446    }
1447  else if (hppa_symbol_address ("__gcc_plt_call")
1448           == get_pc_function_start (val))
1449    {
1450      stubpc = read_memory_integer
1451		 (base - 8, gdbarch_ptr_bit (current_gdbarch) / 8);
1452      trad_frame_set_value (saved_regs, HPPA_PCOQ_HEAD_REGNUM, stubpc);
1453    }
1454}
1455
1456static void
1457hppa_hpux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1458{
1459  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1460
1461  if (IS_32BIT_TARGET (gdbarch))
1462    tdep->in_solib_call_trampoline = hppa32_hpux_in_solib_call_trampoline;
1463  else
1464    tdep->in_solib_call_trampoline = hppa64_hpux_in_solib_call_trampoline;
1465
1466  tdep->unwind_adjust_stub = hppa_hpux_unwind_adjust_stub;
1467
1468  set_gdbarch_in_solib_return_trampoline
1469    (gdbarch, hppa_hpux_in_solib_return_trampoline);
1470  set_gdbarch_skip_trampoline_code (gdbarch, hppa_hpux_skip_trampoline_code);
1471
1472  set_gdbarch_push_dummy_code (gdbarch, hppa_hpux_push_dummy_code);
1473  set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1474
1475  set_gdbarch_read_pc (gdbarch, hppa_hpux_read_pc);
1476  set_gdbarch_write_pc (gdbarch, hppa_hpux_write_pc);
1477  set_gdbarch_unwind_pc (gdbarch, hppa_hpux_unwind_pc);
1478  set_gdbarch_skip_permanent_breakpoint
1479    (gdbarch, hppa_skip_permanent_breakpoint);
1480
1481  set_gdbarch_regset_from_core_section
1482    (gdbarch, hppa_hpux_regset_from_core_section);
1483
1484  frame_unwind_append_sniffer (gdbarch, hppa_hpux_sigtramp_unwind_sniffer);
1485}
1486
1487static void
1488hppa_hpux_som_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1489{
1490  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1491
1492  tdep->is_elf = 0;
1493
1494  tdep->find_global_pointer = hppa32_hpux_find_global_pointer;
1495
1496  hppa_hpux_init_abi (info, gdbarch);
1497  som_solib_select (tdep);
1498}
1499
1500static void
1501hppa_hpux_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1502{
1503  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1504
1505  tdep->is_elf = 1;
1506  tdep->find_global_pointer = hppa64_hpux_find_global_pointer;
1507
1508  hppa_hpux_init_abi (info, gdbarch);
1509  pa64_solib_select (tdep);
1510}
1511
1512static enum gdb_osabi
1513hppa_hpux_core_osabi_sniffer (bfd *abfd)
1514{
1515  if (strcmp (bfd_get_target (abfd), "hpux-core") == 0)
1516    return GDB_OSABI_HPUX_SOM;
1517  else if (strcmp (bfd_get_target (abfd), "elf64-hppa") == 0)
1518    {
1519      asection *section;
1520
1521      section = bfd_get_section_by_name (abfd, ".kernel");
1522      if (section)
1523        {
1524	  bfd_size_type size;
1525	  char *contents;
1526
1527	  size = bfd_section_size (abfd, section);
1528	  contents = alloca (size);
1529 	  if (bfd_get_section_contents (abfd, section, contents,
1530	  				(file_ptr) 0, size)
1531	      && strcmp (contents, "HP-UX") == 0)
1532	    return GDB_OSABI_HPUX_ELF;
1533	}
1534    }
1535
1536  return GDB_OSABI_UNKNOWN;
1537}
1538
1539void
1540_initialize_hppa_hpux_tdep (void)
1541{
1542  /* BFD doesn't set a flavour for HP-UX style core files.  It doesn't
1543     set the architecture either.  */
1544  gdbarch_register_osabi_sniffer (bfd_arch_unknown,
1545				  bfd_target_unknown_flavour,
1546				  hppa_hpux_core_osabi_sniffer);
1547  gdbarch_register_osabi_sniffer (bfd_arch_hppa,
1548                                  bfd_target_elf_flavour,
1549				  hppa_hpux_core_osabi_sniffer);
1550
1551  gdbarch_register_osabi (bfd_arch_hppa, 0, GDB_OSABI_HPUX_SOM,
1552                          hppa_hpux_som_init_abi);
1553  gdbarch_register_osabi (bfd_arch_hppa, bfd_mach_hppa20w, GDB_OSABI_HPUX_ELF,
1554                          hppa_hpux_elf_init_abi);
1555}
1556