OSDN Git Service

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