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