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