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