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