* coffread.c (coff_symtab_read) [SEM]: Remove code, macro can
[external/binutils.git] / gdb / hppa-tdep.c
1 /* Target-dependent code for the HP PA architecture, for GDB.
2    Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
3    Free Software Foundation, Inc.
4
5    Contributed by the Center for Software Science at the
6    University of Utah (pa-gdb-bugs@cs.utah.edu).
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
23
24 #include "defs.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "value.h"
28
29 /* For argument passing to the inferior */
30 #include "symtab.h"
31
32 #ifdef USG
33 #include <sys/types.h>
34 #endif
35
36 #include <sys/param.h>
37 #include <signal.h>
38
39 #ifdef COFF_ENCAPSULATE
40 #include "a.out.encap.h"
41 #else
42 #endif
43
44 /*#include <sys/user.h>         After a.out.h  */
45 #include <sys/file.h>
46 #include "gdb_stat.h"
47 #include "wait.h"
48
49 #include "gdbcore.h"
50 #include "gdbcmd.h"
51 #include "target.h"
52 #include "symfile.h"
53 #include "objfiles.h"
54
55 static int extract_5_load PARAMS ((unsigned int));
56
57 static unsigned extract_5R_store PARAMS ((unsigned int));
58
59 static unsigned extract_5r_store PARAMS ((unsigned int));
60
61 static void find_dummy_frame_regs PARAMS ((struct frame_info *,
62                                            struct frame_saved_regs *));
63
64 static int find_proc_framesize PARAMS ((CORE_ADDR));
65
66 static int find_return_regnum PARAMS ((CORE_ADDR));
67
68 struct unwind_table_entry *find_unwind_entry PARAMS ((CORE_ADDR));
69
70 static int extract_17 PARAMS ((unsigned int));
71
72 static unsigned deposit_21 PARAMS ((unsigned int, unsigned int));
73
74 static int extract_21 PARAMS ((unsigned));
75
76 static unsigned deposit_14 PARAMS ((int, unsigned int));
77
78 static int extract_14 PARAMS ((unsigned));
79
80 static void unwind_command PARAMS ((char *, int));
81
82 static int low_sign_extend PARAMS ((unsigned int, unsigned int));
83
84 static int sign_extend PARAMS ((unsigned int, unsigned int));
85
86 static int restore_pc_queue PARAMS ((struct frame_saved_regs *));
87
88 static int hppa_alignof PARAMS ((struct type *));
89
90 static int prologue_inst_adjust_sp PARAMS ((unsigned long));
91
92 static int is_branch PARAMS ((unsigned long));
93
94 static int inst_saves_gr PARAMS ((unsigned long));
95
96 static int inst_saves_fr PARAMS ((unsigned long));
97
98 static int pc_in_interrupt_handler PARAMS ((CORE_ADDR));
99
100 static int pc_in_linker_stub PARAMS ((CORE_ADDR));
101
102 static int compare_unwind_entries PARAMS ((const void *, const void *));
103
104 static void read_unwind_info PARAMS ((struct objfile *));
105
106 static void internalize_unwinds PARAMS ((struct objfile *,
107                                          struct unwind_table_entry *,
108                                          asection *, unsigned int,
109                                          unsigned int, CORE_ADDR));
110 static void pa_print_registers PARAMS ((char *, int, int));
111 static void pa_print_fp_reg PARAMS ((int));
112
113 \f
114 /* Routines to extract various sized constants out of hppa 
115    instructions. */
116
117 /* This assumes that no garbage lies outside of the lower bits of 
118    value. */
119
120 static int
121 sign_extend (val, bits)
122      unsigned val, bits;
123 {
124   return (int)(val >> (bits - 1) ? (-1 << bits) | val : val);
125 }
126
127 /* For many immediate values the sign bit is the low bit! */
128
129 static int
130 low_sign_extend (val, bits)
131      unsigned val, bits;
132 {
133   return (int)((val & 0x1 ? (-1 << (bits - 1)) : 0) | val >> 1);
134 }
135
136 /* extract the immediate field from a ld{bhw}s instruction */
137
138 #if 0
139
140 unsigned
141 get_field (val, from, to)
142      unsigned val, from, to;
143 {
144   val = val >> 31 - to;
145   return val & ((1 << 32 - from) - 1);
146 }
147
148 unsigned
149 set_field (val, from, to, new_val)
150      unsigned *val, from, to;
151 {
152   unsigned mask = ~((1 << (to - from + 1)) << (31 - from));
153   return *val = *val & mask | (new_val << (31 - from));
154 }
155
156 /* extract a 3-bit space register number from a be, ble, mtsp or mfsp */
157
158 int
159 extract_3 (word)
160      unsigned word;
161 {
162   return GET_FIELD (word, 18, 18) << 2 | GET_FIELD (word, 16, 17);
163 }
164
165 #endif
166
167 static int
168 extract_5_load (word)
169      unsigned word;
170 {
171   return low_sign_extend (word >> 16 & MASK_5, 5);
172 }
173
174 #if 0
175
176 /* extract the immediate field from a st{bhw}s instruction */
177
178 int
179 extract_5_store (word)
180      unsigned word;
181 {
182   return low_sign_extend (word & MASK_5, 5);
183 }
184
185 #endif  /* 0 */
186        
187 /* extract the immediate field from a break instruction */
188
189 static unsigned
190 extract_5r_store (word)
191      unsigned word;
192 {
193   return (word & MASK_5);
194 }
195
196 /* extract the immediate field from a {sr}sm instruction */
197
198 static unsigned
199 extract_5R_store (word)
200      unsigned word;
201 {
202   return (word >> 16 & MASK_5);
203 }
204
205 /* extract an 11 bit immediate field */
206
207 #if 0
208
209 int
210 extract_11 (word)
211      unsigned word;
212 {
213   return low_sign_extend (word & MASK_11, 11);
214 }
215
216 #endif
217
218 /* extract a 14 bit immediate field */
219
220 static int
221 extract_14 (word)
222      unsigned word;
223 {
224   return low_sign_extend (word & MASK_14, 14);
225 }
226
227 /* deposit a 14 bit constant in a word */
228
229 static unsigned
230 deposit_14 (opnd, word)
231      int opnd;
232      unsigned word;
233 {
234   unsigned sign = (opnd < 0 ? 1 : 0);
235
236   return word | ((unsigned)opnd << 1 & MASK_14)  | sign;
237 }
238
239 /* extract a 21 bit constant */
240
241 static int
242 extract_21 (word)
243      unsigned word;
244 {
245   int val;
246
247   word &= MASK_21;
248   word <<= 11;
249   val = GET_FIELD (word, 20, 20);
250   val <<= 11;
251   val |= GET_FIELD (word, 9, 19);
252   val <<= 2;
253   val |= GET_FIELD (word, 5, 6);
254   val <<= 5;
255   val |= GET_FIELD (word, 0, 4);
256   val <<= 2;
257   val |= GET_FIELD (word, 7, 8);
258   return sign_extend (val, 21) << 11;
259 }
260
261 /* deposit a 21 bit constant in a word. Although 21 bit constants are
262    usually the top 21 bits of a 32 bit constant, we assume that only
263    the low 21 bits of opnd are relevant */
264
265 static unsigned
266 deposit_21 (opnd, word)
267      unsigned opnd, word;
268 {
269   unsigned val = 0;
270
271   val |= GET_FIELD (opnd, 11 + 14, 11 + 18);
272   val <<= 2;
273   val |= GET_FIELD (opnd, 11 + 12, 11 + 13);
274   val <<= 2;
275   val |= GET_FIELD (opnd, 11 + 19, 11 + 20);
276   val <<= 11;
277   val |= GET_FIELD (opnd, 11 + 1, 11 + 11);
278   val <<= 1;
279   val |= GET_FIELD (opnd, 11 + 0, 11 + 0);
280   return word | val;
281 }
282
283 /* extract a 12 bit constant from branch instructions */
284
285 #if 0
286
287 int
288 extract_12 (word)
289      unsigned word;
290 {
291   return sign_extend (GET_FIELD (word, 19, 28) |
292                       GET_FIELD (word, 29, 29) << 10 |
293                       (word & 0x1) << 11, 12) << 2;
294 }
295
296 /* Deposit a 17 bit constant in an instruction (like bl). */
297
298 unsigned int
299 deposit_17 (opnd, word)
300      unsigned opnd, word;
301 {
302   word |= GET_FIELD (opnd, 15 + 0, 15 + 0); /* w */
303   word |= GET_FIELD (opnd, 15 + 1, 15 + 5) << 16; /* w1 */
304   word |= GET_FIELD (opnd, 15 + 6, 15 + 6) << 2; /* w2[10] */
305   word |= GET_FIELD (opnd, 15 + 7, 15 + 16) << 3; /* w2[0..9] */
306
307   return word;
308 }
309
310 #endif
311
312 /* extract a 17 bit constant from branch instructions, returning the
313    19 bit signed value. */
314
315 static int
316 extract_17 (word)
317      unsigned word;
318 {
319   return sign_extend (GET_FIELD (word, 19, 28) |
320                       GET_FIELD (word, 29, 29) << 10 |
321                       GET_FIELD (word, 11, 15) << 11 |
322                       (word & 0x1) << 16, 17) << 2;
323 }
324 \f
325
326 /* Compare the start address for two unwind entries returning 1 if 
327    the first address is larger than the second, -1 if the second is
328    larger than the first, and zero if they are equal.  */
329
330 static int
331 compare_unwind_entries (arg1, arg2)
332      const void *arg1;
333      const void *arg2;
334 {
335   const struct unwind_table_entry *a = arg1;
336   const struct unwind_table_entry *b = arg2;
337
338   if (a->region_start > b->region_start)
339     return 1;
340   else if (a->region_start < b->region_start)
341     return -1;
342   else
343     return 0;
344 }
345
346 static void
347 internalize_unwinds (objfile, table, section, entries, size, text_offset)
348      struct objfile *objfile;
349      struct unwind_table_entry *table;
350      asection *section;
351      unsigned int entries, size;
352      CORE_ADDR text_offset;
353 {
354   /* We will read the unwind entries into temporary memory, then
355      fill in the actual unwind table.  */
356   if (size > 0)
357     {
358       unsigned long tmp;
359       unsigned i;
360       char *buf = alloca (size);
361
362       bfd_get_section_contents (objfile->obfd, section, buf, 0, size);
363
364       /* Now internalize the information being careful to handle host/target
365          endian issues.  */
366       for (i = 0; i < entries; i++)
367         {
368           table[i].region_start = bfd_get_32 (objfile->obfd,
369                                                   (bfd_byte *)buf);
370           table[i].region_start += text_offset;
371           buf += 4;
372           table[i].region_end = bfd_get_32 (objfile->obfd, (bfd_byte *)buf);
373           table[i].region_end += text_offset;
374           buf += 4;
375           tmp = bfd_get_32 (objfile->obfd, (bfd_byte *)buf);
376           buf += 4;
377           table[i].Cannot_unwind = (tmp >> 31) & 0x1;
378           table[i].Millicode = (tmp >> 30) & 0x1;
379           table[i].Millicode_save_sr0 = (tmp >> 29) & 0x1;
380           table[i].Region_description = (tmp >> 27) & 0x3;
381           table[i].reserved1 = (tmp >> 26) & 0x1;
382           table[i].Entry_SR = (tmp >> 25) & 0x1;
383           table[i].Entry_FR = (tmp >> 21) & 0xf;
384           table[i].Entry_GR = (tmp >> 16) & 0x1f;
385           table[i].Args_stored = (tmp >> 15) & 0x1;
386           table[i].Variable_Frame = (tmp >> 14) & 0x1;
387           table[i].Separate_Package_Body = (tmp >> 13) & 0x1;
388           table[i].Frame_Extension_Millicode = (tmp >> 12 ) & 0x1;
389           table[i].Stack_Overflow_Check = (tmp >> 11) & 0x1;
390           table[i].Two_Instruction_SP_Increment = (tmp >> 10) & 0x1;
391           table[i].Ada_Region = (tmp >> 9) & 0x1;
392           table[i].reserved2 = (tmp >> 5) & 0xf;
393           table[i].Save_SP = (tmp >> 4) & 0x1;
394           table[i].Save_RP = (tmp >> 3) & 0x1;
395           table[i].Save_MRP_in_frame = (tmp >> 2) & 0x1;
396           table[i].extn_ptr_defined = (tmp >> 1) & 0x1;
397           table[i].Cleanup_defined = tmp & 0x1;
398           tmp = bfd_get_32 (objfile->obfd, (bfd_byte *)buf);
399           buf += 4;
400           table[i].MPE_XL_interrupt_marker = (tmp >> 31) & 0x1;
401           table[i].HP_UX_interrupt_marker = (tmp >> 30) & 0x1;
402           table[i].Large_frame = (tmp >> 29) & 0x1;
403           table[i].reserved4 = (tmp >> 27) & 0x3;
404           table[i].Total_frame_size = tmp & 0x7ffffff;
405         }
406     }
407 }
408
409 /* Read in the backtrace information stored in the `$UNWIND_START$' section of
410    the object file.  This info is used mainly by find_unwind_entry() to find
411    out the stack frame size and frame pointer used by procedures.  We put
412    everything on the psymbol obstack in the objfile so that it automatically
413    gets freed when the objfile is destroyed.  */
414
415 static void
416 read_unwind_info (objfile)
417      struct objfile *objfile;
418 {
419   asection *unwind_sec, *elf_unwind_sec, *stub_unwind_sec;
420   unsigned unwind_size, elf_unwind_size, stub_unwind_size, total_size;
421   unsigned index, unwind_entries, elf_unwind_entries;
422   unsigned stub_entries, total_entries;
423   CORE_ADDR text_offset;
424   struct obj_unwind_info *ui;
425
426   text_offset = ANOFFSET (objfile->section_offsets, 0);
427   ui = (struct obj_unwind_info *)obstack_alloc (&objfile->psymbol_obstack,
428                                                 sizeof (struct obj_unwind_info));
429
430   ui->table = NULL;
431   ui->cache = NULL;
432   ui->last = -1;
433
434   /* Get hooks to all unwind sections.   Note there is no linker-stub unwind
435      section in ELF at the moment.  */
436   unwind_sec = bfd_get_section_by_name (objfile->obfd, "$UNWIND_START$");
437   elf_unwind_sec = bfd_get_section_by_name (objfile->obfd, ".PARISC.unwind");
438   stub_unwind_sec = bfd_get_section_by_name (objfile->obfd, "$UNWIND_END$");
439
440   /* Get sizes and unwind counts for all sections.  */
441   if (unwind_sec)
442     {
443       unwind_size = bfd_section_size (objfile->obfd, unwind_sec);
444       unwind_entries = unwind_size / UNWIND_ENTRY_SIZE;
445     }
446   else
447     {
448       unwind_size = 0;
449       unwind_entries = 0;
450     }
451
452   if (elf_unwind_sec)
453     {
454       elf_unwind_size = bfd_section_size (objfile->obfd, elf_unwind_sec);
455       elf_unwind_entries = elf_unwind_size / UNWIND_ENTRY_SIZE;
456     }
457   else
458     {
459       elf_unwind_size = 0;
460       elf_unwind_entries = 0;
461     }
462
463   if (stub_unwind_sec)
464     {
465       stub_unwind_size = bfd_section_size (objfile->obfd, stub_unwind_sec);
466       stub_entries = stub_unwind_size / STUB_UNWIND_ENTRY_SIZE;
467     }
468   else
469     {
470       stub_unwind_size = 0;
471       stub_entries = 0;
472     }
473
474   /* Compute total number of unwind entries and their total size.  */
475   total_entries = unwind_entries + elf_unwind_entries + stub_entries;
476   total_size = total_entries * sizeof (struct unwind_table_entry);
477
478   /* Allocate memory for the unwind table.  */
479   ui->table = obstack_alloc (&objfile->psymbol_obstack, total_size);
480   ui->last = total_entries - 1;
481
482   /* Internalize the standard unwind entries.  */
483   index = 0;
484   internalize_unwinds (objfile, &ui->table[index], unwind_sec,
485                        unwind_entries, unwind_size, text_offset);
486   index += unwind_entries;
487   internalize_unwinds (objfile, &ui->table[index], elf_unwind_sec,
488                        elf_unwind_entries, elf_unwind_size, text_offset);
489   index += elf_unwind_entries;
490
491   /* Now internalize the stub unwind entries.  */
492   if (stub_unwind_size > 0)
493     {
494       unsigned int i;
495       char *buf = alloca (stub_unwind_size);
496
497       /* Read in the stub unwind entries.  */
498       bfd_get_section_contents (objfile->obfd, stub_unwind_sec, buf,
499                                 0, stub_unwind_size);
500
501       /* Now convert them into regular unwind entries.  */
502       for (i = 0; i < stub_entries; i++, index++)
503         {
504           /* Clear out the next unwind entry.  */
505           memset (&ui->table[index], 0, sizeof (struct unwind_table_entry));
506
507           /* Convert offset & size into region_start and region_end.  
508              Stuff away the stub type into "reserved" fields.  */
509           ui->table[index].region_start = bfd_get_32 (objfile->obfd,
510                                                       (bfd_byte *) buf);
511           ui->table[index].region_start += text_offset;
512           buf += 4;
513           ui->table[index].stub_type = bfd_get_8 (objfile->obfd,
514                                                   (bfd_byte *) buf);
515           buf += 2;
516           ui->table[index].region_end
517             = ui->table[index].region_start + 4 * 
518               (bfd_get_16 (objfile->obfd, (bfd_byte *) buf) - 1);
519           buf += 2;
520         }
521
522     }
523
524   /* Unwind table needs to be kept sorted.  */
525   qsort (ui->table, total_entries, sizeof (struct unwind_table_entry),
526          compare_unwind_entries);
527
528   /* Keep a pointer to the unwind information.  */
529   objfile->obj_private = (PTR) ui;
530 }
531
532 /* Lookup the unwind (stack backtrace) info for the given PC.  We search all
533    of the objfiles seeking the unwind table entry for this PC.  Each objfile
534    contains a sorted list of struct unwind_table_entry.  Since we do a binary
535    search of the unwind tables, we depend upon them to be sorted.  */
536
537 struct unwind_table_entry *
538 find_unwind_entry(pc)
539      CORE_ADDR pc;
540 {
541   int first, middle, last;
542   struct objfile *objfile;
543
544   ALL_OBJFILES (objfile)
545     {
546       struct obj_unwind_info *ui;
547
548       ui = OBJ_UNWIND_INFO (objfile);
549
550       if (!ui)
551         {
552           read_unwind_info (objfile);
553           ui = OBJ_UNWIND_INFO (objfile);
554         }
555
556       /* First, check the cache */
557
558       if (ui->cache
559           && pc >= ui->cache->region_start
560           && pc <= ui->cache->region_end)
561         return ui->cache;
562
563       /* Not in the cache, do a binary search */
564
565       first = 0;
566       last = ui->last;
567
568       while (first <= last)
569         {
570           middle = (first + last) / 2;
571           if (pc >= ui->table[middle].region_start
572               && pc <= ui->table[middle].region_end)
573             {
574               ui->cache = &ui->table[middle];
575               return &ui->table[middle];
576             }
577
578           if (pc < ui->table[middle].region_start)
579             last = middle - 1;
580           else
581             first = middle + 1;
582         }
583     }                           /* ALL_OBJFILES() */
584   return NULL;
585 }
586
587 /* Return the adjustment necessary to make for addresses on the stack
588    as presented by hpread.c.
589
590    This is necessary because of the stack direction on the PA and the
591    bizarre way in which someone (?) decided they wanted to handle
592    frame pointerless code in GDB.  */
593 int
594 hpread_adjust_stack_address (func_addr)
595      CORE_ADDR func_addr;
596 {
597   struct unwind_table_entry *u;
598
599   u = find_unwind_entry (func_addr);
600   if (!u)
601     return 0;
602   else
603     return u->Total_frame_size << 3;
604 }
605
606 /* Called to determine if PC is in an interrupt handler of some
607    kind.  */
608
609 static int
610 pc_in_interrupt_handler (pc)
611      CORE_ADDR pc;
612 {
613   struct unwind_table_entry *u;
614   struct minimal_symbol *msym_us;
615
616   u = find_unwind_entry (pc);
617   if (!u)
618     return 0;
619
620   /* Oh joys.  HPUX sets the interrupt bit for _sigreturn even though
621      its frame isn't a pure interrupt frame.  Deal with this.  */
622   msym_us = lookup_minimal_symbol_by_pc (pc);
623
624   return u->HP_UX_interrupt_marker && !IN_SIGTRAMP (pc, SYMBOL_NAME (msym_us));
625 }
626
627 /* Called when no unwind descriptor was found for PC.  Returns 1 if it
628    appears that PC is in a linker stub.  */
629
630 static int
631 pc_in_linker_stub (pc)
632      CORE_ADDR pc;
633 {
634   int found_magic_instruction = 0;
635   int i;
636   char buf[4];
637
638   /* If unable to read memory, assume pc is not in a linker stub.  */
639   if (target_read_memory (pc, buf, 4) != 0)
640     return 0;
641
642   /* We are looking for something like
643
644      ; $$dyncall jams RP into this special spot in the frame (RP')
645      ; before calling the "call stub"
646      ldw     -18(sp),rp
647
648      ldsid   (rp),r1         ; Get space associated with RP into r1
649      mtsp    r1,sp           ; Move it into space register 0
650      be,n    0(sr0),rp)      ; back to your regularly scheduled program
651      */
652
653   /* Maximum known linker stub size is 4 instructions.  Search forward
654      from the given PC, then backward.  */
655   for (i = 0; i < 4; i++)
656     {
657       /* If we hit something with an unwind, stop searching this direction.  */
658
659       if (find_unwind_entry (pc + i * 4) != 0)
660         break;
661
662       /* Check for ldsid (rp),r1 which is the magic instruction for a 
663          return from a cross-space function call.  */
664       if (read_memory_integer (pc + i * 4, 4) == 0x004010a1)
665         {
666           found_magic_instruction = 1;
667           break;
668         }
669       /* Add code to handle long call/branch and argument relocation stubs
670          here.  */
671     }
672
673   if (found_magic_instruction != 0)
674     return 1;
675
676   /* Now look backward.  */
677   for (i = 0; i < 4; i++)
678     {
679       /* If we hit something with an unwind, stop searching this direction.  */
680
681       if (find_unwind_entry (pc - i * 4) != 0)
682         break;
683
684       /* Check for ldsid (rp),r1 which is the magic instruction for a 
685          return from a cross-space function call.  */
686       if (read_memory_integer (pc - i * 4, 4) == 0x004010a1)
687         {
688           found_magic_instruction = 1;
689           break;
690         }
691       /* Add code to handle long call/branch and argument relocation stubs
692          here.  */
693     }
694   return found_magic_instruction;
695 }
696
697 static int
698 find_return_regnum(pc)
699      CORE_ADDR pc;
700 {
701   struct unwind_table_entry *u;
702
703   u = find_unwind_entry (pc);
704
705   if (!u)
706     return RP_REGNUM;
707
708   if (u->Millicode)
709     return 31;
710
711   return RP_REGNUM;
712 }
713
714 /* Return size of frame, or -1 if we should use a frame pointer.  */
715 static int
716 find_proc_framesize (pc)
717      CORE_ADDR pc;
718 {
719   struct unwind_table_entry *u;
720   struct minimal_symbol *msym_us;
721
722   u = find_unwind_entry (pc);
723
724   if (!u)
725     {
726       if (pc_in_linker_stub (pc))
727         /* Linker stubs have a zero size frame.  */
728         return 0;
729       else
730         return -1;
731     }
732
733   msym_us = lookup_minimal_symbol_by_pc (pc);
734
735   /* If Save_SP is set, and we're not in an interrupt or signal caller,
736      then we have a frame pointer.  Use it.  */
737   if (u->Save_SP && !pc_in_interrupt_handler (pc)
738       && !IN_SIGTRAMP (pc, SYMBOL_NAME (msym_us)))
739     return -1;
740
741   return u->Total_frame_size << 3;
742 }
743
744 /* Return offset from sp at which rp is saved, or 0 if not saved.  */
745 static int rp_saved PARAMS ((CORE_ADDR));
746
747 static int
748 rp_saved (pc)
749      CORE_ADDR pc;
750 {
751   struct unwind_table_entry *u;
752
753   u = find_unwind_entry (pc);
754
755   if (!u)
756     {
757       if (pc_in_linker_stub (pc))
758         /* This is the so-called RP'.  */
759         return -24;
760       else
761         return 0;
762     }
763
764   if (u->Save_RP)
765     return -20;
766   else if (u->stub_type != 0)
767     {
768       switch (u->stub_type)
769         {
770         case EXPORT:
771         case IMPORT:
772           return -24;
773         case PARAMETER_RELOCATION:
774           return -8;
775         default:
776           return 0;
777         }
778     }
779   else
780     return 0;
781 }
782 \f
783 int
784 frameless_function_invocation (frame)
785      struct frame_info *frame;
786 {
787   struct unwind_table_entry *u;
788
789   u = find_unwind_entry (frame->pc);
790
791   if (u == 0)
792     return 0;
793
794   return (u->Total_frame_size == 0 && u->stub_type == 0);
795 }
796
797 CORE_ADDR
798 saved_pc_after_call (frame)
799      struct frame_info *frame;
800 {
801   int ret_regnum;
802   CORE_ADDR pc;
803   struct unwind_table_entry *u;
804
805   ret_regnum = find_return_regnum (get_frame_pc (frame));
806   pc = read_register (ret_regnum) & ~0x3;
807   
808   /* If PC is in a linker stub, then we need to dig the address
809      the stub will return to out of the stack.  */
810   u = find_unwind_entry (pc);
811   if (u && u->stub_type != 0)
812     return FRAME_SAVED_PC (frame);
813   else
814     return pc;
815 }
816 \f
817 CORE_ADDR
818 hppa_frame_saved_pc (frame)
819      struct frame_info *frame;
820 {
821   CORE_ADDR pc = get_frame_pc (frame);
822   struct unwind_table_entry *u;
823
824   /* BSD, HPUX & OSF1 all lay out the hardware state in the same manner
825      at the base of the frame in an interrupt handler.  Registers within
826      are saved in the exact same order as GDB numbers registers.  How
827      convienent.  */
828   if (pc_in_interrupt_handler (pc))
829     return read_memory_integer (frame->frame + PC_REGNUM * 4, 4) & ~0x3;
830
831 #ifdef FRAME_SAVED_PC_IN_SIGTRAMP
832   /* Deal with signal handler caller frames too.  */
833   if (frame->signal_handler_caller)
834     {
835       CORE_ADDR rp;
836       FRAME_SAVED_PC_IN_SIGTRAMP (frame, &rp);
837       return rp & ~0x3;
838     }
839 #endif
840
841   if (frameless_function_invocation (frame))
842     {
843       int ret_regnum;
844
845       ret_regnum = find_return_regnum (pc);
846
847       /* If the next frame is an interrupt frame or a signal
848          handler caller, then we need to look in the saved
849          register area to get the return pointer (the values
850          in the registers may not correspond to anything useful).  */
851       if (frame->next 
852           && (frame->next->signal_handler_caller
853               || pc_in_interrupt_handler (frame->next->pc)))
854         {
855           struct frame_saved_regs saved_regs;
856
857           get_frame_saved_regs (frame->next, &saved_regs);
858           if (read_memory_integer (saved_regs.regs[FLAGS_REGNUM], 4) & 0x2)
859             {
860               pc = read_memory_integer (saved_regs.regs[31], 4) & ~0x3;
861
862               /* Syscalls are really two frames.  The syscall stub itself
863                  with a return pointer in %rp and the kernel call with
864                  a return pointer in %r31.  We return the %rp variant
865                  if %r31 is the same as frame->pc.  */
866               if (pc == frame->pc)
867                 pc = read_memory_integer (saved_regs.regs[RP_REGNUM], 4) & ~0x3;
868             }
869           else
870             pc = read_memory_integer (saved_regs.regs[RP_REGNUM], 4) & ~0x3;
871         }
872       else
873         pc = read_register (ret_regnum) & ~0x3;
874     }
875   else
876     {
877       int rp_offset;
878
879 restart:
880       rp_offset = rp_saved (pc);
881       /* Similar to code in frameless function case.  If the next
882          frame is a signal or interrupt handler, then dig the right
883          information out of the saved register info.  */
884       if (rp_offset == 0
885           && frame->next
886           && (frame->next->signal_handler_caller
887               || pc_in_interrupt_handler (frame->next->pc)))
888         {
889           struct frame_saved_regs saved_regs;
890
891           get_frame_saved_regs (frame->next, &saved_regs);
892           if (read_memory_integer (saved_regs.regs[FLAGS_REGNUM], 4) & 0x2)
893             {
894               pc = read_memory_integer (saved_regs.regs[31], 4) & ~0x3;
895
896               /* Syscalls are really two frames.  The syscall stub itself
897                  with a return pointer in %rp and the kernel call with
898                  a return pointer in %r31.  We return the %rp variant
899                  if %r31 is the same as frame->pc.  */
900               if (pc == frame->pc)
901                 pc = read_memory_integer (saved_regs.regs[RP_REGNUM], 4) & ~0x3;
902             }
903           else
904             pc = read_memory_integer (saved_regs.regs[RP_REGNUM], 4) & ~0x3;
905         }
906       else if (rp_offset == 0)
907         pc = read_register (RP_REGNUM) & ~0x3;
908       else
909         pc = read_memory_integer (frame->frame + rp_offset, 4) & ~0x3;
910     }
911
912   /* If PC is inside a linker stub, then dig out the address the stub
913      will return to. 
914
915      Don't do this for long branch stubs.  Why?  For some unknown reason
916      _start is marked as a long branch stub in hpux10.  */
917   u = find_unwind_entry (pc);
918   if (u && u->stub_type != 0
919       && u->stub_type != LONG_BRANCH)
920     {
921       unsigned int insn;
922
923       /* If this is a dynamic executable, and we're in a signal handler,
924          then the call chain will eventually point us into the stub for
925          _sigreturn.  Unlike most cases, we'll be pointed to the branch
926          to the real sigreturn rather than the code after the real branch!. 
927
928          Else, try to dig the address the stub will return to in the normal
929          fashion.  */
930       insn = read_memory_integer (pc, 4);
931       if ((insn & 0xfc00e000) == 0xe8000000)
932         return (pc + extract_17 (insn) + 8) & ~0x3;
933       else
934         goto restart;
935     }
936
937   return pc;
938 }
939 \f
940 /* We need to correct the PC and the FP for the outermost frame when we are
941    in a system call.  */
942
943 void
944 init_extra_frame_info (fromleaf, frame)
945      int fromleaf;
946      struct frame_info *frame;
947 {
948   int flags;
949   int framesize;
950
951   if (frame->next && !fromleaf)
952     return;
953
954   /* If the next frame represents a frameless function invocation
955      then we have to do some adjustments that are normally done by
956      FRAME_CHAIN.  (FRAME_CHAIN is not called in this case.)  */
957   if (fromleaf)
958     {
959       /* Find the framesize of *this* frame without peeking at the PC
960          in the current frame structure (it isn't set yet).  */
961       framesize = find_proc_framesize (FRAME_SAVED_PC (get_next_frame (frame)));
962
963       /* Now adjust our base frame accordingly.  If we have a frame pointer
964          use it, else subtract the size of this frame from the current
965          frame.  (we always want frame->frame to point at the lowest address
966          in the frame).  */
967       if (framesize == -1)
968         frame->frame = read_register (FP_REGNUM);
969       else
970         frame->frame -= framesize;
971       return;
972     }
973
974   flags = read_register (FLAGS_REGNUM);
975   if (flags & 2)        /* In system call? */
976     frame->pc = read_register (31) & ~0x3;
977
978   /* The outermost frame is always derived from PC-framesize
979
980      One might think frameless innermost frames should have
981      a frame->frame that is the same as the parent's frame->frame.
982      That is wrong; frame->frame in that case should be the *high*
983      address of the parent's frame.  It's complicated as hell to
984      explain, but the parent *always* creates some stack space for
985      the child.  So the child actually does have a frame of some
986      sorts, and its base is the high address in its parent's frame.  */
987   framesize = find_proc_framesize(frame->pc);
988   if (framesize == -1)
989     frame->frame = read_register (FP_REGNUM);
990   else
991     frame->frame = read_register (SP_REGNUM) - framesize;
992 }
993 \f
994 /* Given a GDB frame, determine the address of the calling function's frame.
995    This will be used to create a new GDB frame struct, and then
996    INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
997
998    This may involve searching through prologues for several functions
999    at boundaries where GCC calls HP C code, or where code which has
1000    a frame pointer calls code without a frame pointer.  */
1001
1002 CORE_ADDR
1003 frame_chain (frame)
1004      struct frame_info *frame;
1005 {
1006   int my_framesize, caller_framesize;
1007   struct unwind_table_entry *u;
1008   CORE_ADDR frame_base;
1009   struct frame_info *tmp_frame;
1010
1011   /* Handle HPUX, BSD, and OSF1 style interrupt frames first.  These
1012      are easy; at *sp we have a full save state strucutre which we can
1013      pull the old stack pointer from.  Also see frame_saved_pc for
1014      code to dig a saved PC out of the save state structure.  */
1015   if (pc_in_interrupt_handler (frame->pc))
1016     frame_base = read_memory_integer (frame->frame + SP_REGNUM * 4, 4);
1017 #ifdef FRAME_BASE_BEFORE_SIGTRAMP
1018   else if (frame->signal_handler_caller)
1019     {
1020       FRAME_BASE_BEFORE_SIGTRAMP (frame, &frame_base);
1021     }
1022 #endif
1023   else
1024     frame_base = frame->frame;
1025
1026   /* Get frame sizes for the current frame and the frame of the 
1027      caller.  */
1028   my_framesize = find_proc_framesize (frame->pc);
1029   caller_framesize = find_proc_framesize (FRAME_SAVED_PC(frame));
1030
1031   /* If caller does not have a frame pointer, then its frame
1032      can be found at current_frame - caller_framesize.  */
1033   if (caller_framesize != -1)
1034     return frame_base - caller_framesize;
1035
1036   /* Both caller and callee have frame pointers and are GCC compiled
1037      (SAVE_SP bit in unwind descriptor is on for both functions.
1038      The previous frame pointer is found at the top of the current frame.  */
1039   if (caller_framesize == -1 && my_framesize == -1)
1040     return read_memory_integer (frame_base, 4);
1041
1042   /* Caller has a frame pointer, but callee does not.  This is a little
1043      more difficult as GCC and HP C lay out locals and callee register save
1044      areas very differently.
1045
1046      The previous frame pointer could be in a register, or in one of 
1047      several areas on the stack.
1048
1049      Walk from the current frame to the innermost frame examining 
1050      unwind descriptors to determine if %r3 ever gets saved into the
1051      stack.  If so return whatever value got saved into the stack.
1052      If it was never saved in the stack, then the value in %r3 is still
1053      valid, so use it. 
1054
1055      We use information from unwind descriptors to determine if %r3
1056      is saved into the stack (Entry_GR field has this information).  */
1057
1058   tmp_frame = frame;
1059   while (tmp_frame)
1060     {
1061       u = find_unwind_entry (tmp_frame->pc);
1062
1063       if (!u)
1064         {
1065           /* We could find this information by examining prologues.  I don't
1066              think anyone has actually written any tools (not even "strip")
1067              which leave them out of an executable, so maybe this is a moot
1068              point.  */
1069           warning ("Unable to find unwind for PC 0x%x -- Help!", tmp_frame->pc);
1070           return 0;
1071         }
1072
1073       /* Entry_GR specifies the number of callee-saved general registers
1074          saved in the stack.  It starts at %r3, so %r3 would be 1.  */
1075       if (u->Entry_GR >= 1 || u->Save_SP
1076           || tmp_frame->signal_handler_caller
1077           || pc_in_interrupt_handler (tmp_frame->pc))
1078         break;
1079       else
1080         tmp_frame = tmp_frame->next;
1081     }
1082
1083   if (tmp_frame)
1084     {
1085       /* We may have walked down the chain into a function with a frame
1086          pointer.  */
1087       if (u->Save_SP
1088           && !tmp_frame->signal_handler_caller
1089           && !pc_in_interrupt_handler (tmp_frame->pc))
1090         return read_memory_integer (tmp_frame->frame, 4);
1091       /* %r3 was saved somewhere in the stack.  Dig it out.  */
1092       else 
1093         {
1094           struct frame_saved_regs saved_regs;
1095
1096           /* Sick.
1097
1098              For optimization purposes many kernels don't have the
1099              callee saved registers into the save_state structure upon
1100              entry into the kernel for a syscall; the optimization
1101              is usually turned off if the process is being traced so
1102              that the debugger can get full register state for the
1103              process.
1104               
1105              This scheme works well except for two cases:
1106
1107                * Attaching to a process when the process is in the
1108                kernel performing a system call (debugger can't get
1109                full register state for the inferior process since
1110                the process wasn't being traced when it entered the
1111                system call).
1112
1113                * Register state is not complete if the system call
1114                causes the process to core dump.
1115
1116
1117              The following heinous code is an attempt to deal with
1118              the lack of register state in a core dump.  It will
1119              fail miserably if the function which performs the
1120              system call has a variable sized stack frame.  */
1121
1122           get_frame_saved_regs (tmp_frame, &saved_regs);
1123
1124           /* Abominable hack.  */
1125           if (current_target.to_has_execution == 0
1126               && ((saved_regs.regs[FLAGS_REGNUM]
1127                    && (read_memory_integer (saved_regs.regs[FLAGS_REGNUM], 4)
1128                        & 0x2))
1129                   || (saved_regs.regs[FLAGS_REGNUM] == 0
1130                       && read_register (FLAGS_REGNUM) & 0x2)))
1131             {
1132               u = find_unwind_entry (FRAME_SAVED_PC (frame));
1133               if (!u)
1134                 return read_memory_integer (saved_regs.regs[FP_REGNUM], 4);
1135               else
1136                 return frame_base - (u->Total_frame_size << 3);
1137             }
1138         
1139           return read_memory_integer (saved_regs.regs[FP_REGNUM], 4);
1140         }
1141     }
1142   else
1143     {
1144       struct frame_saved_regs saved_regs;
1145
1146       /* Get the innermost frame.  */
1147       tmp_frame = frame;
1148       while (tmp_frame->next != NULL)
1149         tmp_frame = tmp_frame->next;
1150
1151       get_frame_saved_regs (tmp_frame, &saved_regs);
1152       /* Abominable hack.  See above.  */
1153       if (current_target.to_has_execution == 0
1154           && ((saved_regs.regs[FLAGS_REGNUM]
1155                && (read_memory_integer (saved_regs.regs[FLAGS_REGNUM], 4)
1156                    & 0x2))
1157               || (saved_regs.regs[FLAGS_REGNUM] == 0
1158                   && read_register (FLAGS_REGNUM)  & 0x2)))
1159         {
1160           u = find_unwind_entry (FRAME_SAVED_PC (frame));
1161           if (!u)
1162             return read_memory_integer (saved_regs.regs[FP_REGNUM], 4);
1163            else
1164             return frame_base - (u->Total_frame_size << 3);
1165         }
1166         
1167       /* The value in %r3 was never saved into the stack (thus %r3 still
1168          holds the value of the previous frame pointer).  */
1169       return read_register (FP_REGNUM);
1170     }
1171 }
1172
1173 \f
1174 /* To see if a frame chain is valid, see if the caller looks like it
1175    was compiled with gcc. */
1176
1177 int
1178 frame_chain_valid (chain, thisframe)
1179      CORE_ADDR chain;
1180      struct frame_info *thisframe;
1181 {
1182   struct minimal_symbol *msym_us;
1183   struct minimal_symbol *msym_start;
1184   struct unwind_table_entry *u, *next_u = NULL;
1185   struct frame_info *next;
1186
1187   if (!chain)
1188     return 0;
1189
1190   u = find_unwind_entry (thisframe->pc);
1191
1192   if (u == NULL)
1193     return 1;
1194
1195   /* We can't just check that the same of msym_us is "_start", because
1196      someone idiotically decided that they were going to make a Ltext_end
1197      symbol with the same address.  This Ltext_end symbol is totally
1198      indistinguishable (as nearly as I can tell) from the symbol for a function
1199      which is (legitimately, since it is in the user's namespace)
1200      named Ltext_end, so we can't just ignore it.  */
1201   msym_us = lookup_minimal_symbol_by_pc (FRAME_SAVED_PC (thisframe));
1202   msym_start = lookup_minimal_symbol ("_start", NULL, NULL);
1203   if (msym_us
1204       && msym_start
1205       && SYMBOL_VALUE_ADDRESS (msym_us) == SYMBOL_VALUE_ADDRESS (msym_start))
1206     return 0;
1207
1208   /* Grrrr.  Some new idiot decided that they don't want _start for the
1209      PRO configurations; $START$ calls main directly....  Deal with it.  */
1210   msym_start = lookup_minimal_symbol ("$START$", NULL, NULL);
1211   if (msym_us
1212       && msym_start
1213       && SYMBOL_VALUE_ADDRESS (msym_us) == SYMBOL_VALUE_ADDRESS (msym_start))
1214     return 0;
1215
1216   next = get_next_frame (thisframe);
1217   if (next)
1218     next_u = find_unwind_entry (next->pc);
1219
1220   /* If this frame does not save SP, has no stack, isn't a stub,
1221      and doesn't "call" an interrupt routine or signal handler caller,
1222      then its not valid.  */
1223   if (u->Save_SP || u->Total_frame_size || u->stub_type != 0
1224       || (thisframe->next && thisframe->next->signal_handler_caller)
1225       || (next_u && next_u->HP_UX_interrupt_marker))
1226     return 1;
1227
1228   if (pc_in_linker_stub (thisframe->pc))
1229     return 1;
1230
1231   return 0;
1232 }
1233
1234 /*
1235  * These functions deal with saving and restoring register state
1236  * around a function call in the inferior. They keep the stack
1237  * double-word aligned; eventually, on an hp700, the stack will have
1238  * to be aligned to a 64-byte boundary.
1239  */
1240
1241 void
1242 push_dummy_frame (inf_status)
1243      struct inferior_status *inf_status;
1244 {
1245   CORE_ADDR sp, pc, pcspace;
1246   register int regnum;
1247   int int_buffer;
1248   double freg_buffer;
1249
1250   /* Oh, what a hack.  If we're trying to perform an inferior call
1251      while the inferior is asleep, we have to make sure to clear
1252      the "in system call" bit in the flag register (the call will
1253      start after the syscall returns, so we're no longer in the system
1254      call!)  This state is kept in "inf_status", change it there.
1255
1256      We also need a number of horrid hacks to deal with lossage in the
1257      PC queue registers (apparently they're not valid when the in syscall
1258      bit is set).  */
1259   pc = target_read_pc (inferior_pid);
1260   int_buffer = read_register (FLAGS_REGNUM);
1261   if (int_buffer & 0x2)
1262     {
1263       unsigned int sid;
1264       int_buffer &= ~0x2;
1265       memcpy (inf_status->registers, &int_buffer, 4);
1266       memcpy (inf_status->registers + REGISTER_BYTE (PCOQ_HEAD_REGNUM), &pc, 4);
1267       pc += 4;
1268       memcpy (inf_status->registers + REGISTER_BYTE (PCOQ_TAIL_REGNUM), &pc, 4);
1269       pc -= 4;
1270       sid = (pc >> 30) & 0x3;
1271       if (sid == 0)
1272         pcspace = read_register (SR4_REGNUM);
1273       else
1274         pcspace = read_register (SR4_REGNUM + 4 + sid);
1275       memcpy (inf_status->registers + REGISTER_BYTE (PCSQ_HEAD_REGNUM),
1276               &pcspace, 4);
1277       memcpy (inf_status->registers + REGISTER_BYTE (PCSQ_TAIL_REGNUM),
1278               &pcspace, 4);
1279     }
1280   else
1281     pcspace = read_register (PCSQ_HEAD_REGNUM);
1282
1283   /* Space for "arguments"; the RP goes in here. */
1284   sp = read_register (SP_REGNUM) + 48;
1285   int_buffer = read_register (RP_REGNUM) | 0x3;
1286   write_memory (sp - 20, (char *)&int_buffer, 4);
1287
1288   int_buffer = read_register (FP_REGNUM);
1289   write_memory (sp, (char *)&int_buffer, 4);
1290
1291   write_register (FP_REGNUM, sp);
1292
1293   sp += 8;
1294
1295   for (regnum = 1; regnum < 32; regnum++)
1296     if (regnum != RP_REGNUM && regnum != FP_REGNUM)
1297       sp = push_word (sp, read_register (regnum));
1298
1299   sp += 4;
1300
1301   for (regnum = FP0_REGNUM; regnum < NUM_REGS; regnum++)
1302     {
1303       read_register_bytes (REGISTER_BYTE (regnum), (char *)&freg_buffer, 8);
1304       sp = push_bytes (sp, (char *)&freg_buffer, 8);
1305     }
1306   sp = push_word (sp, read_register (IPSW_REGNUM));
1307   sp = push_word (sp, read_register (SAR_REGNUM));
1308   sp = push_word (sp, pc);
1309   sp = push_word (sp, pcspace);
1310   sp = push_word (sp, pc + 4);
1311   sp = push_word (sp, pcspace);
1312   write_register (SP_REGNUM, sp);
1313 }
1314
1315 static void
1316 find_dummy_frame_regs (frame, frame_saved_regs)
1317      struct frame_info *frame;
1318      struct frame_saved_regs *frame_saved_regs;
1319 {
1320   CORE_ADDR fp = frame->frame;
1321   int i;
1322
1323   frame_saved_regs->regs[RP_REGNUM] = (fp - 20) & ~0x3;
1324   frame_saved_regs->regs[FP_REGNUM] = fp;
1325   frame_saved_regs->regs[1] = fp + 8;
1326
1327   for (fp += 12, i = 3; i < 32; i++)
1328     {
1329       if (i != FP_REGNUM)
1330         {
1331           frame_saved_regs->regs[i] = fp;
1332           fp += 4;
1333         }
1334     }
1335
1336   fp += 4;
1337   for (i = FP0_REGNUM; i < NUM_REGS; i++, fp += 8)
1338     frame_saved_regs->regs[i] = fp;
1339
1340   frame_saved_regs->regs[IPSW_REGNUM] = fp;
1341   frame_saved_regs->regs[SAR_REGNUM] = fp + 4;
1342   frame_saved_regs->regs[PCOQ_HEAD_REGNUM] = fp + 8;
1343   frame_saved_regs->regs[PCSQ_HEAD_REGNUM] = fp + 12;
1344   frame_saved_regs->regs[PCOQ_TAIL_REGNUM] = fp + 16;
1345   frame_saved_regs->regs[PCSQ_TAIL_REGNUM] = fp + 20;
1346 }
1347
1348 void
1349 hppa_pop_frame ()
1350 {
1351   register struct frame_info *frame = get_current_frame ();
1352   register CORE_ADDR fp, npc, target_pc;
1353   register int regnum;
1354   struct frame_saved_regs fsr;
1355   double freg_buffer;
1356
1357   fp = FRAME_FP (frame);
1358   get_frame_saved_regs (frame, &fsr);
1359
1360 #ifndef NO_PC_SPACE_QUEUE_RESTORE
1361   if (fsr.regs[IPSW_REGNUM])    /* Restoring a call dummy frame */
1362     restore_pc_queue (&fsr);
1363 #endif
1364
1365   for (regnum = 31; regnum > 0; regnum--)
1366     if (fsr.regs[regnum])
1367       write_register (regnum, read_memory_integer (fsr.regs[regnum], 4));
1368
1369   for (regnum = NUM_REGS - 1; regnum >= FP0_REGNUM ; regnum--)
1370     if (fsr.regs[regnum])
1371       {
1372         read_memory (fsr.regs[regnum], (char *)&freg_buffer, 8);
1373         write_register_bytes (REGISTER_BYTE (regnum), (char *)&freg_buffer, 8);
1374       }
1375
1376   if (fsr.regs[IPSW_REGNUM])
1377     write_register (IPSW_REGNUM,
1378                     read_memory_integer (fsr.regs[IPSW_REGNUM], 4));
1379
1380   if (fsr.regs[SAR_REGNUM])
1381     write_register (SAR_REGNUM,
1382                     read_memory_integer (fsr.regs[SAR_REGNUM], 4));
1383
1384   /* If the PC was explicitly saved, then just restore it.  */
1385   if (fsr.regs[PCOQ_TAIL_REGNUM])
1386     {
1387       npc = read_memory_integer (fsr.regs[PCOQ_TAIL_REGNUM], 4);
1388       write_register (PCOQ_TAIL_REGNUM, npc);
1389     }
1390   /* Else use the value in %rp to set the new PC.  */
1391   else 
1392     {
1393       npc = read_register (RP_REGNUM);
1394       write_pc (npc);
1395     }
1396
1397   write_register (FP_REGNUM, read_memory_integer (fp, 4));
1398
1399   if (fsr.regs[IPSW_REGNUM])    /* call dummy */
1400     write_register (SP_REGNUM, fp - 48);
1401   else
1402     write_register (SP_REGNUM, fp);
1403
1404   /* The PC we just restored may be inside a return trampoline.  If so
1405      we want to restart the inferior and run it through the trampoline.
1406
1407      Do this by setting a momentary breakpoint at the location the
1408      trampoline returns to. 
1409
1410      Don't skip through the trampoline if we're popping a dummy frame.  */
1411   target_pc = SKIP_TRAMPOLINE_CODE (npc & ~0x3) & ~0x3;
1412   if (target_pc && !fsr.regs[IPSW_REGNUM])
1413     {
1414       struct symtab_and_line sal;
1415       struct breakpoint *breakpoint;
1416       struct cleanup *old_chain;
1417
1418       /* Set up our breakpoint.   Set it to be silent as the MI code
1419          for "return_command" will print the frame we returned to.  */
1420       sal = find_pc_line (target_pc, 0);
1421       sal.pc = target_pc;
1422       breakpoint = set_momentary_breakpoint (sal, NULL, bp_finish);
1423       breakpoint->silent = 1;
1424
1425       /* So we can clean things up.  */
1426       old_chain = make_cleanup (delete_breakpoint, breakpoint);
1427
1428       /* Start up the inferior.  */
1429       clear_proceed_status ();
1430       proceed_to_finish = 1;
1431       proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
1432
1433       /* Perform our cleanups.  */
1434       do_cleanups (old_chain);
1435     }
1436   flush_cached_frames ();
1437 }
1438
1439 /*
1440  * After returning to a dummy on the stack, restore the instruction
1441  * queue space registers. */
1442
1443 static int
1444 restore_pc_queue (fsr)
1445      struct frame_saved_regs *fsr;
1446 {
1447   CORE_ADDR pc = read_pc ();
1448   CORE_ADDR new_pc = read_memory_integer (fsr->regs[PCOQ_HEAD_REGNUM], 4);
1449   struct target_waitstatus w;
1450   int insn_count;
1451
1452   /* Advance past break instruction in the call dummy. */
1453   write_register (PCOQ_HEAD_REGNUM, pc + 4);
1454   write_register (PCOQ_TAIL_REGNUM, pc + 8);
1455
1456   /*
1457    * HPUX doesn't let us set the space registers or the space
1458    * registers of the PC queue through ptrace. Boo, hiss.
1459    * Conveniently, the call dummy has this sequence of instructions
1460    * after the break:
1461    *    mtsp r21, sr0
1462    *    ble,n 0(sr0, r22)
1463    *
1464    * So, load up the registers and single step until we are in the
1465    * right place.
1466    */
1467
1468   write_register (21, read_memory_integer (fsr->regs[PCSQ_HEAD_REGNUM], 4));
1469   write_register (22, new_pc);
1470
1471   for (insn_count = 0; insn_count < 3; insn_count++)
1472     {
1473       /* FIXME: What if the inferior gets a signal right now?  Want to
1474          merge this into wait_for_inferior (as a special kind of
1475          watchpoint?  By setting a breakpoint at the end?  Is there
1476          any other choice?  Is there *any* way to do this stuff with
1477          ptrace() or some equivalent?).  */
1478       resume (1, 0);
1479       target_wait (inferior_pid, &w);
1480
1481       if (w.kind == TARGET_WAITKIND_SIGNALLED)
1482         {
1483           stop_signal = w.value.sig;
1484           terminal_ours_for_output ();
1485           printf_unfiltered ("\nProgram terminated with signal %s, %s.\n",
1486                              target_signal_to_name (stop_signal),
1487                              target_signal_to_string (stop_signal));
1488           gdb_flush (gdb_stdout);
1489           return 0;
1490         }
1491     }
1492   target_terminal_ours ();
1493   target_fetch_registers (-1);
1494   return 1;
1495 }
1496
1497 CORE_ADDR
1498 hppa_push_arguments (nargs, args, sp, struct_return, struct_addr)
1499      int nargs;
1500      value_ptr *args;
1501      CORE_ADDR sp;
1502      int struct_return;
1503      CORE_ADDR struct_addr;
1504 {
1505   /* array of arguments' offsets */
1506   int *offset = (int *)alloca(nargs * sizeof (int));
1507   int cum = 0;
1508   int i, alignment;
1509   
1510   for (i = 0; i < nargs; i++)
1511     {
1512       cum += TYPE_LENGTH (VALUE_TYPE (args[i]));
1513
1514     /* value must go at proper alignment. Assume alignment is a
1515          power of two.*/
1516       alignment = hppa_alignof (VALUE_TYPE (args[i]));
1517       if (cum % alignment)
1518         cum = (cum + alignment) & -alignment;
1519       offset[i] = -cum;
1520     }
1521   sp += max ((cum + 7) & -8, 16);
1522
1523   for (i = 0; i < nargs; i++)
1524     write_memory (sp + offset[i], VALUE_CONTENTS (args[i]),
1525                   TYPE_LENGTH (VALUE_TYPE (args[i])));
1526
1527   if (struct_return)
1528     write_register (28, struct_addr);
1529   return sp + 32;
1530 }
1531
1532 /*
1533  * Insert the specified number of args and function address
1534  * into a call sequence of the above form stored at DUMMYNAME.
1535  *
1536  * On the hppa we need to call the stack dummy through $$dyncall.
1537  * Therefore our version of FIX_CALL_DUMMY takes an extra argument,
1538  * real_pc, which is the location where gdb should start up the
1539  * inferior to do the function call.
1540  */
1541
1542 CORE_ADDR
1543 hppa_fix_call_dummy (dummy, pc, fun, nargs, args, type, gcc_p)
1544      char *dummy;
1545      CORE_ADDR pc;
1546      CORE_ADDR fun;
1547      int nargs;
1548      value_ptr *args;
1549      struct type *type;
1550      int gcc_p;
1551 {
1552   CORE_ADDR dyncall_addr;
1553   struct minimal_symbol *msymbol;
1554   struct minimal_symbol *trampoline;
1555   int flags = read_register (FLAGS_REGNUM);
1556   struct unwind_table_entry *u;
1557
1558   trampoline = NULL;
1559   msymbol = lookup_minimal_symbol ("$$dyncall", NULL, NULL);
1560   if (msymbol == NULL)
1561     error ("Can't find an address for $$dyncall trampoline");
1562
1563   dyncall_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1564
1565   /* FUN could be a procedure label, in which case we have to get
1566      its real address and the value of its GOT/DP.  */
1567   if (fun & 0x2)
1568     {
1569       /* Get the GOT/DP value for the target function.  It's
1570          at *(fun+4).  Note the call dummy is *NOT* allowed to
1571          trash %r19 before calling the target function.  */
1572       write_register (19, read_memory_integer ((fun & ~0x3) + 4, 4));
1573
1574       /* Now get the real address for the function we are calling, it's
1575          at *fun.  */
1576       fun = (CORE_ADDR) read_memory_integer (fun & ~0x3, 4);
1577     }
1578   else
1579     {
1580
1581 #ifndef GDB_TARGET_IS_PA_ELF
1582       /* FUN could be either an export stub, or the real address of a
1583          function in a shared library.  We must call an import stub
1584          rather than the export stub or real function for lazy binding
1585          to work correctly.  */
1586       if (som_solib_get_got_by_pc (fun))
1587         {
1588           struct objfile *objfile;
1589           struct minimal_symbol *funsymbol, *stub_symbol;
1590           CORE_ADDR newfun = 0;
1591
1592           funsymbol = lookup_minimal_symbol_by_pc (fun);
1593           if (!funsymbol)
1594             error ("Unable to find minimal symbol for target fucntion.\n");
1595
1596           /* Search all the object files for an import symbol with the
1597              right name. */
1598           ALL_OBJFILES (objfile)
1599             {
1600               stub_symbol = lookup_minimal_symbol (SYMBOL_NAME (funsymbol),
1601                                                    NULL, objfile);
1602               /* Found a symbol with the right name.  */
1603               if (stub_symbol)
1604                 {
1605                   struct unwind_table_entry *u;
1606                   /* It must be a shared library trampoline.  */
1607                   if (MSYMBOL_TYPE (stub_symbol) != mst_solib_trampoline)
1608                     continue;
1609
1610                   /* It must also be an import stub.  */
1611                   u = find_unwind_entry (SYMBOL_VALUE (stub_symbol));
1612                   if (!u || u->stub_type != IMPORT)
1613                     continue;
1614
1615                   /* OK.  Looks like the correct import stub.  */
1616                   newfun = SYMBOL_VALUE (stub_symbol);
1617                   fun = newfun;
1618                 }
1619             }
1620           if (newfun == 0)
1621             write_register (19, som_solib_get_got_by_pc (fun));
1622         }
1623 #endif
1624     }
1625
1626   /* If we are calling an import stub (eg calling into a dynamic library)
1627      then have sr4export call the magic __d_plt_call routine which is linked
1628      in from end.o.  (You can't use _sr4export to call the import stub as
1629      the value in sp-24 will get fried and you end up returning to the
1630      wrong location.  You can't call the import stub directly as the code
1631      to bind the PLT entry to a function can't return to a stack address.)  */
1632   u = find_unwind_entry (fun);
1633   if (u && u->stub_type == IMPORT)
1634     {
1635       CORE_ADDR new_fun;
1636
1637       /* Prefer __gcc_plt_call over the HP supplied routine because
1638          __gcc_plt_call works for any number of arguments.  */
1639       trampoline = lookup_minimal_symbol ("__gcc_plt_call", NULL, NULL);
1640       if (trampoline == NULL)
1641         trampoline = lookup_minimal_symbol ("__d_plt_call", NULL, NULL);
1642
1643       if (trampoline == NULL)
1644         error ("Can't find an address for __d_plt_call or __gcc_plt_call trampoline");
1645
1646       /* This is where sr4export will jump to.  */
1647       new_fun = SYMBOL_VALUE_ADDRESS (trampoline);
1648
1649       if (strcmp (SYMBOL_NAME (trampoline), "__d_plt_call") == 0)
1650         {
1651           /* We have to store the address of the stub in __shlib_funcptr.  */
1652           msymbol = lookup_minimal_symbol ("__shlib_funcptr", NULL,
1653                                            (struct objfile *)NULL);
1654           if (msymbol == NULL)
1655             error ("Can't find an address for __shlib_funcptr");
1656
1657           target_write_memory (SYMBOL_VALUE_ADDRESS (msymbol), (char *)&fun, 4);
1658
1659           /* We want sr4export to call __d_plt_call, so we claim it is
1660              the final target.  Clear trampoline.  */
1661           fun = new_fun;
1662           trampoline = NULL;
1663         }
1664     }
1665
1666   /* Store upper 21 bits of function address into ldil.  fun will either be
1667      the final target (most cases) or __d_plt_call when calling into a shared
1668      library and __gcc_plt_call is not available.  */
1669   store_unsigned_integer
1670     (&dummy[FUNC_LDIL_OFFSET],
1671      INSTRUCTION_SIZE,
1672      deposit_21 (fun >> 11,
1673                  extract_unsigned_integer (&dummy[FUNC_LDIL_OFFSET],
1674                                            INSTRUCTION_SIZE)));
1675
1676   /* Store lower 11 bits of function address into ldo */
1677   store_unsigned_integer
1678     (&dummy[FUNC_LDO_OFFSET],
1679      INSTRUCTION_SIZE,
1680      deposit_14 (fun & MASK_11,
1681                  extract_unsigned_integer (&dummy[FUNC_LDO_OFFSET],
1682                                            INSTRUCTION_SIZE)));
1683 #ifdef SR4EXPORT_LDIL_OFFSET
1684
1685   {
1686     CORE_ADDR trampoline_addr;
1687
1688     /* We may still need sr4export's address too.  */
1689
1690     if (trampoline == NULL)
1691       {
1692         msymbol = lookup_minimal_symbol ("_sr4export", NULL, NULL);
1693         if (msymbol == NULL)
1694           error ("Can't find an address for _sr4export trampoline");
1695
1696         trampoline_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1697       }
1698     else
1699       trampoline_addr = SYMBOL_VALUE_ADDRESS (trampoline);
1700
1701
1702     /* Store upper 21 bits of trampoline's address into ldil */
1703     store_unsigned_integer
1704       (&dummy[SR4EXPORT_LDIL_OFFSET],
1705        INSTRUCTION_SIZE,
1706        deposit_21 (trampoline_addr >> 11,
1707                    extract_unsigned_integer (&dummy[SR4EXPORT_LDIL_OFFSET],
1708                                              INSTRUCTION_SIZE)));
1709
1710     /* Store lower 11 bits of trampoline's address into ldo */
1711     store_unsigned_integer
1712       (&dummy[SR4EXPORT_LDO_OFFSET],
1713        INSTRUCTION_SIZE,
1714        deposit_14 (trampoline_addr & MASK_11,
1715                    extract_unsigned_integer (&dummy[SR4EXPORT_LDO_OFFSET],
1716                                              INSTRUCTION_SIZE)));
1717   }
1718 #endif
1719
1720   write_register (22, pc);
1721
1722   /* If we are in a syscall, then we should call the stack dummy
1723      directly.  $$dyncall is not needed as the kernel sets up the
1724      space id registers properly based on the value in %r31.  In
1725      fact calling $$dyncall will not work because the value in %r22
1726      will be clobbered on the syscall exit path. 
1727
1728      Similarly if the current PC is in a shared library.  Note however,
1729      this scheme won't work if the shared library isn't mapped into
1730      the same space as the stack.  */
1731   if (flags & 2)
1732     return pc;
1733 #ifndef GDB_TARGET_IS_PA_ELF
1734   else if (som_solib_get_got_by_pc (target_read_pc (inferior_pid)))
1735     return pc;
1736 #endif
1737   else
1738     return dyncall_addr;
1739
1740 }
1741
1742 /* Get the PC from %r31 if currently in a syscall.  Also mask out privilege
1743    bits.  */
1744
1745 CORE_ADDR
1746 target_read_pc (pid)
1747      int pid;
1748 {
1749   int flags = read_register_pid (FLAGS_REGNUM, pid);
1750
1751   /* The following test does not belong here.  It is OS-specific, and belongs
1752      in native code.  */
1753   /* Test SS_INSYSCALL */
1754   if (flags & 2)
1755     return read_register_pid (31, pid) & ~0x3;
1756
1757   return read_register_pid (PC_REGNUM, pid) & ~0x3;
1758 }
1759
1760 /* Write out the PC.  If currently in a syscall, then also write the new
1761    PC value into %r31.  */
1762
1763 void
1764 target_write_pc (v, pid)
1765      CORE_ADDR v;
1766      int pid;
1767 {
1768   int flags = read_register_pid (FLAGS_REGNUM, pid);
1769
1770   /* The following test does not belong here.  It is OS-specific, and belongs
1771      in native code.  */
1772   /* If in a syscall, then set %r31.  Also make sure to get the 
1773      privilege bits set correctly.  */
1774   /* Test SS_INSYSCALL */
1775   if (flags & 2)
1776     write_register_pid (31, v | 0x3, pid);
1777
1778   write_register_pid (PC_REGNUM, v, pid);
1779   write_register_pid (NPC_REGNUM, v + 4, pid);
1780 }
1781
1782 /* return the alignment of a type in bytes. Structures have the maximum
1783    alignment required by their fields. */
1784
1785 static int
1786 hppa_alignof (type)
1787      struct type *type;
1788 {
1789   int max_align, align, i;
1790   CHECK_TYPEDEF (type);
1791   switch (TYPE_CODE (type))
1792     {
1793     case TYPE_CODE_PTR:
1794     case TYPE_CODE_INT:
1795     case TYPE_CODE_FLT:
1796       return TYPE_LENGTH (type);
1797     case TYPE_CODE_ARRAY:
1798       return hppa_alignof (TYPE_FIELD_TYPE (type, 0));
1799     case TYPE_CODE_STRUCT:
1800     case TYPE_CODE_UNION:
1801       max_align = 1;
1802       for (i = 0; i < TYPE_NFIELDS (type); i++)
1803         {
1804           /* Bit fields have no real alignment. */
1805           if (!TYPE_FIELD_BITPOS (type, i))
1806             {
1807               align = hppa_alignof (TYPE_FIELD_TYPE (type, i));
1808               max_align = max (max_align, align);
1809             }
1810         }
1811       return max_align;
1812     default:
1813       return 4;
1814     }
1815 }
1816
1817 /* Print the register regnum, or all registers if regnum is -1 */
1818
1819 void
1820 pa_do_registers_info (regnum, fpregs)
1821      int regnum;
1822      int fpregs;
1823 {
1824   char raw_regs [REGISTER_BYTES];
1825   int i;
1826   
1827   for (i = 0; i < NUM_REGS; i++)
1828     read_relative_register_raw_bytes (i, raw_regs + REGISTER_BYTE (i));
1829   if (regnum == -1)
1830     pa_print_registers (raw_regs, regnum, fpregs);
1831   else if (regnum < FP0_REGNUM)
1832     printf_unfiltered ("%s %x\n", reg_names[regnum], *(long *)(raw_regs +
1833                                                     REGISTER_BYTE (regnum)));
1834   else
1835     pa_print_fp_reg (regnum);
1836 }
1837
1838 static void
1839 pa_print_registers (raw_regs, regnum, fpregs)
1840      char *raw_regs;
1841      int regnum;
1842      int fpregs;
1843 {
1844   int i,j;
1845   long val;
1846
1847   for (i = 0; i < 18; i++)
1848     {
1849       for (j = 0; j < 4; j++)
1850         {
1851           val =
1852             extract_signed_integer (raw_regs + REGISTER_BYTE (i+(j*18)), 4);
1853           printf_unfiltered ("%8.8s: %8x  ", reg_names[i+(j*18)], val);
1854         }
1855       printf_unfiltered ("\n");
1856     }
1857   
1858   if (fpregs)
1859     for (i = 72; i < NUM_REGS; i++)
1860       pa_print_fp_reg (i);
1861 }
1862
1863 static void
1864 pa_print_fp_reg (i)
1865      int i;
1866 {
1867   unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
1868   unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
1869
1870   /* Get 32bits of data.  */
1871   read_relative_register_raw_bytes (i, raw_buffer);
1872
1873   /* Put it in the buffer.  No conversions are ever necessary.  */
1874   memcpy (virtual_buffer, raw_buffer, REGISTER_RAW_SIZE (i));
1875
1876   fputs_filtered (reg_names[i], gdb_stdout);
1877   print_spaces_filtered (8 - strlen (reg_names[i]), gdb_stdout);
1878   fputs_filtered ("(single precision)     ", gdb_stdout);
1879
1880   val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, gdb_stdout, 0,
1881              1, 0, Val_pretty_default);
1882   printf_filtered ("\n");
1883
1884   /* If "i" is even, then this register can also be a double-precision
1885      FP register.  Dump it out as such.  */
1886   if ((i % 2) == 0)
1887     {
1888       /* Get the data in raw format for the 2nd half.  */
1889       read_relative_register_raw_bytes (i + 1, raw_buffer);
1890
1891       /* Copy it into the appropriate part of the virtual buffer.  */
1892       memcpy (virtual_buffer + REGISTER_RAW_SIZE (i), raw_buffer,
1893               REGISTER_RAW_SIZE (i));
1894
1895       /* Dump it as a double.  */
1896       fputs_filtered (reg_names[i], gdb_stdout);
1897       print_spaces_filtered (8 - strlen (reg_names[i]), gdb_stdout);
1898       fputs_filtered ("(double precision)     ", gdb_stdout);
1899
1900       val_print (builtin_type_double, virtual_buffer, 0, gdb_stdout, 0,
1901                  1, 0, Val_pretty_default);
1902       printf_filtered ("\n");
1903     }
1904 }
1905
1906 /* Return one if PC is in the call path of a trampoline, else return zero.
1907
1908    Note we return one for *any* call trampoline (long-call, arg-reloc), not
1909    just shared library trampolines (import, export).  */
1910
1911 int
1912 in_solib_call_trampoline (pc, name)
1913      CORE_ADDR pc;
1914      char *name;
1915 {
1916   struct minimal_symbol *minsym;
1917   struct unwind_table_entry *u;
1918   static CORE_ADDR dyncall = 0;
1919   static CORE_ADDR sr4export = 0;
1920
1921 /* FIXME XXX - dyncall and sr4export must be initialized whenever we get a
1922    new exec file */
1923
1924   /* First see if PC is in one of the two C-library trampolines.  */
1925   if (!dyncall)
1926     {
1927       minsym = lookup_minimal_symbol ("$$dyncall", NULL, NULL);
1928       if (minsym)
1929         dyncall = SYMBOL_VALUE_ADDRESS (minsym);
1930       else
1931         dyncall = -1;
1932     }
1933
1934   if (!sr4export)
1935     {
1936       minsym = lookup_minimal_symbol ("_sr4export", NULL, NULL);
1937       if (minsym)
1938         sr4export = SYMBOL_VALUE_ADDRESS (minsym);
1939       else
1940         sr4export = -1;
1941     }
1942
1943   if (pc == dyncall || pc == sr4export)
1944     return 1;
1945
1946   /* Get the unwind descriptor corresponding to PC, return zero
1947      if no unwind was found.  */
1948   u = find_unwind_entry (pc);
1949   if (!u)
1950     return 0;
1951
1952   /* If this isn't a linker stub, then return now.  */
1953   if (u->stub_type == 0)
1954     return 0;
1955
1956   /* By definition a long-branch stub is a call stub.  */
1957   if (u->stub_type == LONG_BRANCH)
1958     return 1;
1959
1960   /* The call and return path execute the same instructions within
1961      an IMPORT stub!  So an IMPORT stub is both a call and return
1962      trampoline.  */
1963   if (u->stub_type == IMPORT)
1964     return 1;
1965
1966   /* Parameter relocation stubs always have a call path and may have a
1967      return path.  */
1968   if (u->stub_type == PARAMETER_RELOCATION
1969       || u->stub_type == EXPORT)
1970     {
1971       CORE_ADDR addr;
1972
1973       /* Search forward from the current PC until we hit a branch
1974          or the end of the stub.  */
1975       for (addr = pc; addr <= u->region_end; addr += 4)
1976         {
1977           unsigned long insn;
1978
1979           insn = read_memory_integer (addr, 4);
1980
1981           /* Does it look like a bl?  If so then it's the call path, if
1982              we find a bv or be first, then we're on the return path.  */
1983           if ((insn & 0xfc00e000) == 0xe8000000)
1984             return 1;
1985           else if ((insn & 0xfc00e001) == 0xe800c000
1986                    || (insn & 0xfc000000) == 0xe0000000)
1987             return 0;
1988         }
1989
1990       /* Should never happen.  */
1991       warning ("Unable to find branch in parameter relocation stub.\n");
1992       return 0;
1993     }
1994
1995   /* Unknown stub type.  For now, just return zero.  */
1996   return 0;
1997 }
1998
1999 /* Return one if PC is in the return path of a trampoline, else return zero.
2000
2001    Note we return one for *any* call trampoline (long-call, arg-reloc), not
2002    just shared library trampolines (import, export).  */
2003
2004 int
2005 in_solib_return_trampoline (pc, name)
2006      CORE_ADDR pc;
2007      char *name;
2008 {
2009   struct unwind_table_entry *u;
2010
2011   /* Get the unwind descriptor corresponding to PC, return zero
2012      if no unwind was found.  */
2013   u = find_unwind_entry (pc);
2014   if (!u)
2015     return 0;
2016
2017   /* If this isn't a linker stub or it's just a long branch stub, then
2018      return zero.  */
2019   if (u->stub_type == 0 || u->stub_type == LONG_BRANCH)
2020     return 0;
2021
2022   /* The call and return path execute the same instructions within
2023      an IMPORT stub!  So an IMPORT stub is both a call and return
2024      trampoline.  */
2025   if (u->stub_type == IMPORT)
2026     return 1;
2027
2028   /* Parameter relocation stubs always have a call path and may have a
2029      return path.  */
2030   if (u->stub_type == PARAMETER_RELOCATION
2031       || u->stub_type == EXPORT)
2032     {
2033       CORE_ADDR addr;
2034
2035       /* Search forward from the current PC until we hit a branch
2036          or the end of the stub.  */
2037       for (addr = pc; addr <= u->region_end; addr += 4)
2038         {
2039           unsigned long insn;
2040
2041           insn = read_memory_integer (addr, 4);
2042
2043           /* Does it look like a bl?  If so then it's the call path, if
2044              we find a bv or be first, then we're on the return path.  */
2045           if ((insn & 0xfc00e000) == 0xe8000000)
2046             return 0;
2047           else if ((insn & 0xfc00e001) == 0xe800c000
2048                    || (insn & 0xfc000000) == 0xe0000000)
2049             return 1;
2050         }
2051
2052       /* Should never happen.  */
2053       warning ("Unable to find branch in parameter relocation stub.\n");
2054       return 0;
2055     }
2056
2057   /* Unknown stub type.  For now, just return zero.  */
2058   return 0;
2059
2060 }
2061
2062 /* Figure out if PC is in a trampoline, and if so find out where
2063    the trampoline will jump to.  If not in a trampoline, return zero.
2064
2065    Simple code examination probably is not a good idea since the code
2066    sequences in trampolines can also appear in user code.
2067
2068    We use unwinds and information from the minimal symbol table to
2069    determine when we're in a trampoline.  This won't work for ELF
2070    (yet) since it doesn't create stub unwind entries.  Whether or
2071    not ELF will create stub unwinds or normal unwinds for linker
2072    stubs is still being debated.
2073
2074    This should handle simple calls through dyncall or sr4export,
2075    long calls, argument relocation stubs, and dyncall/sr4export
2076    calling an argument relocation stub.  It even handles some stubs
2077    used in dynamic executables.  */
2078
2079 CORE_ADDR
2080 skip_trampoline_code (pc, name)
2081      CORE_ADDR pc;
2082      char *name;
2083 {
2084   long orig_pc = pc;
2085   long prev_inst, curr_inst, loc;
2086   static CORE_ADDR dyncall = 0;
2087   static CORE_ADDR sr4export = 0;
2088   struct minimal_symbol *msym;
2089   struct unwind_table_entry *u;
2090
2091 /* FIXME XXX - dyncall and sr4export must be initialized whenever we get a
2092    new exec file */
2093
2094   if (!dyncall)
2095     {
2096       msym = lookup_minimal_symbol ("$$dyncall", NULL, NULL);
2097       if (msym)
2098         dyncall = SYMBOL_VALUE_ADDRESS (msym);
2099       else
2100         dyncall = -1;
2101     }
2102
2103   if (!sr4export)
2104     {
2105       msym = lookup_minimal_symbol ("_sr4export", NULL, NULL);
2106       if (msym)
2107         sr4export = SYMBOL_VALUE_ADDRESS (msym);
2108       else
2109         sr4export = -1;
2110     }
2111
2112   /* Addresses passed to dyncall may *NOT* be the actual address
2113      of the function.  So we may have to do something special.  */
2114   if (pc == dyncall)
2115     {
2116       pc = (CORE_ADDR) read_register (22);
2117
2118       /* If bit 30 (counting from the left) is on, then pc is the address of
2119          the PLT entry for this function, not the address of the function
2120          itself.  Bit 31 has meaning too, but only for MPE.  */
2121       if (pc & 0x2)
2122         pc = (CORE_ADDR) read_memory_integer (pc & ~0x3, 4);
2123     }
2124   else if (pc == sr4export)
2125     pc = (CORE_ADDR) (read_register (22));
2126
2127   /* Get the unwind descriptor corresponding to PC, return zero
2128      if no unwind was found.  */
2129   u = find_unwind_entry (pc);
2130   if (!u)
2131     return 0;
2132
2133   /* If this isn't a linker stub, then return now.  */
2134   if (u->stub_type == 0)
2135     return orig_pc == pc ? 0 : pc & ~0x3;
2136
2137   /* It's a stub.  Search for a branch and figure out where it goes.
2138      Note we have to handle multi insn branch sequences like ldil;ble.
2139      Most (all?) other branches can be determined by examining the contents
2140      of certain registers and the stack.  */
2141   loc = pc;
2142   curr_inst = 0;
2143   prev_inst = 0;
2144   while (1)
2145     {
2146       /* Make sure we haven't walked outside the range of this stub.  */
2147       if (u != find_unwind_entry (loc))
2148         {
2149           warning ("Unable to find branch in linker stub");
2150           return orig_pc == pc ? 0 : pc & ~0x3;
2151         }
2152
2153       prev_inst = curr_inst;
2154       curr_inst = read_memory_integer (loc, 4);
2155
2156       /* Does it look like a branch external using %r1?  Then it's the
2157          branch from the stub to the actual function.  */
2158       if ((curr_inst & 0xffe0e000) == 0xe0202000)
2159         {
2160           /* Yup.  See if the previous instruction loaded
2161              a value into %r1.  If so compute and return the jump address.  */
2162           if ((prev_inst & 0xffe00000) == 0x20200000)
2163             return (extract_21 (prev_inst) + extract_17 (curr_inst)) & ~0x3;
2164           else
2165             {
2166               warning ("Unable to find ldil X,%%r1 before ble Y(%%sr4,%%r1).");
2167               return orig_pc == pc ? 0 : pc & ~0x3;
2168             }
2169         }
2170
2171       /* Does it look like a be 0(sr0,%r21)?  That's the branch from an
2172          import stub to an export stub.
2173
2174          It is impossible to determine the target of the branch via
2175          simple examination of instructions and/or data (consider
2176          that the address in the plabel may be the address of the
2177          bind-on-reference routine in the dynamic loader).
2178
2179          So we have try an alternative approach.
2180
2181          Get the name of the symbol at our current location; it should
2182          be a stub symbol with the same name as the symbol in the
2183          shared library.
2184
2185          Then lookup a minimal symbol with the same name; we should
2186          get the minimal symbol for the target routine in the shared
2187          library as those take precedence of import/export stubs.  */
2188       if (curr_inst == 0xe2a00000)
2189         {
2190           struct minimal_symbol *stubsym, *libsym;
2191
2192           stubsym = lookup_minimal_symbol_by_pc (loc);
2193           if (stubsym == NULL)
2194             {
2195               warning ("Unable to find symbol for 0x%x", loc);
2196               return orig_pc == pc ? 0 : pc & ~0x3;
2197             }
2198
2199           libsym = lookup_minimal_symbol (SYMBOL_NAME (stubsym), NULL, NULL);
2200           if (libsym == NULL)
2201             {
2202               warning ("Unable to find library symbol for %s\n",
2203                        SYMBOL_NAME (stubsym));
2204               return orig_pc == pc ? 0 : pc & ~0x3;
2205             }
2206
2207           return SYMBOL_VALUE (libsym);
2208         }
2209
2210       /* Does it look like bl X,%rp or bl X,%r0?  Another way to do a
2211          branch from the stub to the actual function.  */
2212       else if ((curr_inst & 0xffe0e000) == 0xe8400000
2213                || (curr_inst & 0xffe0e000) == 0xe8000000)
2214         return (loc + extract_17 (curr_inst) + 8) & ~0x3;
2215
2216       /* Does it look like bv (rp)?   Note this depends on the
2217          current stack pointer being the same as the stack
2218          pointer in the stub itself!  This is a branch on from the
2219          stub back to the original caller.  */
2220       else if ((curr_inst & 0xffe0e000) == 0xe840c000)
2221         {
2222           /* Yup.  See if the previous instruction loaded
2223              rp from sp - 8.  */
2224           if (prev_inst == 0x4bc23ff1)
2225             return (read_memory_integer
2226                     (read_register (SP_REGNUM) - 8, 4)) & ~0x3;
2227           else
2228             {
2229               warning ("Unable to find restore of %%rp before bv (%%rp).");
2230               return orig_pc == pc ? 0 : pc & ~0x3;
2231             }
2232         }
2233
2234       /* What about be,n 0(sr0,%rp)?  It's just another way we return to
2235          the original caller from the stub.  Used in dynamic executables.  */
2236       else if (curr_inst == 0xe0400002)
2237         {
2238           /* The value we jump to is sitting in sp - 24.  But that's
2239              loaded several instructions before the be instruction.
2240              I guess we could check for the previous instruction being
2241              mtsp %r1,%sr0 if we want to do sanity checking.  */
2242           return (read_memory_integer 
2243                   (read_register (SP_REGNUM) - 24, 4)) & ~0x3;
2244         }
2245
2246       /* Haven't found the branch yet, but we're still in the stub.
2247          Keep looking.  */
2248       loc += 4;
2249     }
2250 }
2251
2252 /* For the given instruction (INST), return any adjustment it makes
2253    to the stack pointer or zero for no adjustment. 
2254
2255    This only handles instructions commonly found in prologues.  */
2256
2257 static int
2258 prologue_inst_adjust_sp (inst)
2259      unsigned long inst;
2260 {
2261   /* This must persist across calls.  */
2262   static int save_high21;
2263
2264   /* The most common way to perform a stack adjustment ldo X(sp),sp */
2265   if ((inst & 0xffffc000) == 0x37de0000)
2266     return extract_14 (inst);
2267
2268   /* stwm X,D(sp) */
2269   if ((inst & 0xffe00000) == 0x6fc00000)
2270     return extract_14 (inst);
2271
2272   /* addil high21,%r1; ldo low11,(%r1),%r30)
2273      save high bits in save_high21 for later use.  */
2274   if ((inst & 0xffe00000) == 0x28200000)
2275     {
2276       save_high21 = extract_21 (inst);
2277       return 0;
2278     }
2279
2280   if ((inst & 0xffff0000) == 0x343e0000)
2281     return save_high21 + extract_14 (inst);
2282
2283   /* fstws as used by the HP compilers.  */
2284   if ((inst & 0xffffffe0) == 0x2fd01220)
2285     return extract_5_load (inst);
2286
2287   /* No adjustment.  */
2288   return 0;
2289 }
2290
2291 /* Return nonzero if INST is a branch of some kind, else return zero.  */
2292
2293 static int
2294 is_branch (inst)
2295      unsigned long inst;
2296 {
2297   switch (inst >> 26)
2298     {
2299     case 0x20:
2300     case 0x21:
2301     case 0x22:
2302     case 0x23:
2303     case 0x28:
2304     case 0x29:
2305     case 0x2a:
2306     case 0x2b:
2307     case 0x30:
2308     case 0x31:
2309     case 0x32:
2310     case 0x33:
2311     case 0x38:
2312     case 0x39:
2313     case 0x3a:
2314       return 1;
2315
2316     default:
2317       return 0;
2318     }
2319 }
2320
2321 /* Return the register number for a GR which is saved by INST or
2322    zero it INST does not save a GR.  */
2323
2324 static int
2325 inst_saves_gr (inst)
2326      unsigned long inst;
2327 {
2328   /* Does it look like a stw?  */
2329   if ((inst >> 26) == 0x1a)
2330     return extract_5R_store (inst);
2331
2332   /* Does it look like a stwm?  GCC & HPC may use this in prologues. */
2333   if ((inst >> 26) == 0x1b)
2334     return extract_5R_store (inst);
2335
2336   /* Does it look like sth or stb?  HPC versions 9.0 and later use these
2337      too.  */
2338   if ((inst >> 26) == 0x19 || (inst >> 26) == 0x18)
2339     return extract_5R_store (inst);
2340       
2341   return 0;
2342 }
2343
2344 /* Return the register number for a FR which is saved by INST or
2345    zero it INST does not save a FR.
2346
2347    Note we only care about full 64bit register stores (that's the only
2348    kind of stores the prologue will use).
2349
2350    FIXME: What about argument stores with the HP compiler in ANSI mode? */
2351
2352 static int
2353 inst_saves_fr (inst)
2354      unsigned long inst;
2355 {
2356   if ((inst & 0xfc00dfc0) == 0x2c001200)
2357     return extract_5r_store (inst);
2358   return 0;
2359 }
2360
2361 /* Advance PC across any function entry prologue instructions
2362    to reach some "real" code. 
2363
2364    Use information in the unwind table to determine what exactly should
2365    be in the prologue.  */
2366
2367 CORE_ADDR
2368 skip_prologue (pc)
2369      CORE_ADDR pc;
2370 {
2371   char buf[4];
2372   CORE_ADDR orig_pc = pc;
2373   unsigned long inst, stack_remaining, save_gr, save_fr, save_rp, save_sp;
2374   unsigned long args_stored, status, i, restart_gr, restart_fr;
2375   struct unwind_table_entry *u;
2376
2377   restart_gr = 0;
2378   restart_fr = 0;
2379
2380 restart:
2381   u = find_unwind_entry (pc);
2382   if (!u)
2383     return pc;
2384
2385   /* If we are not at the beginning of a function, then return now.  */
2386   if ((pc & ~0x3) != u->region_start)
2387     return pc;
2388
2389   /* This is how much of a frame adjustment we need to account for.  */
2390   stack_remaining = u->Total_frame_size << 3;
2391
2392   /* Magic register saves we want to know about.  */
2393   save_rp = u->Save_RP;
2394   save_sp = u->Save_SP;
2395
2396   /* An indication that args may be stored into the stack.  Unfortunately
2397      the HPUX compilers tend to set this in cases where no args were
2398      stored too!.  */
2399   args_stored = 1;
2400
2401   /* Turn the Entry_GR field into a bitmask.  */
2402   save_gr = 0;
2403   for (i = 3; i < u->Entry_GR + 3; i++)
2404     {
2405       /* Frame pointer gets saved into a special location.  */
2406       if (u->Save_SP && i == FP_REGNUM)
2407         continue;
2408
2409       save_gr |= (1 << i);
2410     }
2411   save_gr &= ~restart_gr;
2412
2413   /* Turn the Entry_FR field into a bitmask too.  */
2414   save_fr = 0;
2415   for (i = 12; i < u->Entry_FR + 12; i++)
2416     save_fr |= (1 << i);
2417   save_fr &= ~restart_fr;
2418
2419   /* Loop until we find everything of interest or hit a branch.
2420
2421      For unoptimized GCC code and for any HP CC code this will never ever
2422      examine any user instructions.
2423
2424      For optimzied GCC code we're faced with problems.  GCC will schedule
2425      its prologue and make prologue instructions available for delay slot
2426      filling.  The end result is user code gets mixed in with the prologue
2427      and a prologue instruction may be in the delay slot of the first branch
2428      or call.
2429
2430      Some unexpected things are expected with debugging optimized code, so
2431      we allow this routine to walk past user instructions in optimized
2432      GCC code.  */
2433   while (save_gr || save_fr || save_rp || save_sp || stack_remaining > 0
2434          || args_stored)
2435     {
2436       unsigned int reg_num;
2437       unsigned long old_stack_remaining, old_save_gr, old_save_fr;
2438       unsigned long old_save_rp, old_save_sp, next_inst;
2439
2440       /* Save copies of all the triggers so we can compare them later
2441          (only for HPC).  */
2442       old_save_gr = save_gr;
2443       old_save_fr = save_fr;
2444       old_save_rp = save_rp;
2445       old_save_sp = save_sp;
2446       old_stack_remaining = stack_remaining;
2447
2448       status = target_read_memory (pc, buf, 4);
2449       inst = extract_unsigned_integer (buf, 4);
2450        
2451       /* Yow! */
2452       if (status != 0)
2453         return pc;
2454
2455       /* Note the interesting effects of this instruction.  */
2456       stack_remaining -= prologue_inst_adjust_sp (inst);
2457
2458       /* There is only one instruction used for saving RP into the stack.  */
2459       if (inst == 0x6bc23fd9)
2460         save_rp = 0;
2461
2462       /* This is the only way we save SP into the stack.  At this time
2463          the HP compilers never bother to save SP into the stack.  */
2464       if ((inst & 0xffffc000) == 0x6fc10000)
2465         save_sp = 0;
2466
2467       /* Account for general and floating-point register saves.  */
2468       reg_num = inst_saves_gr (inst);
2469       save_gr &= ~(1 << reg_num);
2470
2471       /* Ugh.  Also account for argument stores into the stack.
2472          Unfortunately args_stored only tells us that some arguments
2473          where stored into the stack.  Not how many or what kind!
2474
2475          This is a kludge as on the HP compiler sets this bit and it
2476          never does prologue scheduling.  So once we see one, skip past
2477          all of them.   We have similar code for the fp arg stores below.
2478
2479          FIXME.  Can still die if we have a mix of GR and FR argument
2480          stores!  */
2481       if (reg_num >= 23 && reg_num <= 26)
2482         {
2483           while (reg_num >= 23 && reg_num <= 26)
2484             {
2485               pc += 4;
2486               status = target_read_memory (pc, buf, 4);
2487               inst = extract_unsigned_integer (buf, 4);
2488               if (status != 0)
2489                 return pc;
2490               reg_num = inst_saves_gr (inst);
2491             }
2492           args_stored = 0;
2493           continue;
2494         }
2495
2496       reg_num = inst_saves_fr (inst);
2497       save_fr &= ~(1 << reg_num);
2498
2499       status = target_read_memory (pc + 4, buf, 4);
2500       next_inst = extract_unsigned_integer (buf, 4);
2501        
2502       /* Yow! */
2503       if (status != 0)
2504         return pc;
2505
2506       /* We've got to be read to handle the ldo before the fp register
2507          save.  */
2508       if ((inst & 0xfc000000) == 0x34000000
2509           && inst_saves_fr (next_inst) >= 4
2510           && inst_saves_fr (next_inst) <= 7)
2511         {
2512           /* So we drop into the code below in a reasonable state.  */
2513           reg_num = inst_saves_fr (next_inst);
2514           pc -= 4;
2515         }
2516
2517       /* Ugh.  Also account for argument stores into the stack.
2518          This is a kludge as on the HP compiler sets this bit and it
2519          never does prologue scheduling.  So once we see one, skip past
2520          all of them.  */
2521       if (reg_num >= 4 && reg_num <= 7)
2522         {
2523           while (reg_num >= 4 && reg_num <= 7)
2524             {
2525               pc += 8;
2526               status = target_read_memory (pc, buf, 4);
2527               inst = extract_unsigned_integer (buf, 4);
2528               if (status != 0)
2529                 return pc;
2530               if ((inst & 0xfc000000) != 0x34000000)
2531                 break;
2532               status = target_read_memory (pc + 4, buf, 4);
2533               next_inst = extract_unsigned_integer (buf, 4);
2534               if (status != 0)
2535                 return pc;
2536               reg_num = inst_saves_fr (next_inst);
2537             }
2538           args_stored = 0;
2539           continue;
2540         }
2541
2542       /* Quit if we hit any kind of branch.  This can happen if a prologue
2543          instruction is in the delay slot of the first call/branch.  */
2544       if (is_branch (inst))
2545         break;
2546
2547       /* What a crock.  The HP compilers set args_stored even if no
2548          arguments were stored into the stack (boo hiss).  This could
2549          cause this code to then skip a bunch of user insns (up to the
2550          first branch).
2551
2552          To combat this we try to identify when args_stored was bogusly
2553          set and clear it.   We only do this when args_stored is nonzero,
2554          all other resources are accounted for, and nothing changed on
2555          this pass.  */
2556       if (args_stored
2557           && ! (save_gr || save_fr || save_rp || save_sp || stack_remaining > 0)
2558           && old_save_gr == save_gr && old_save_fr == save_fr
2559           && old_save_rp == save_rp && old_save_sp == save_sp
2560           && old_stack_remaining == stack_remaining)
2561         break;
2562       
2563       /* Bump the PC.  */
2564       pc += 4;
2565     }
2566
2567   /* We've got a tenative location for the end of the prologue.  However
2568      because of limitations in the unwind descriptor mechanism we may
2569      have went too far into user code looking for the save of a register
2570      that does not exist.  So, if there registers we expected to be saved
2571      but never were, mask them out and restart.
2572
2573      This should only happen in optimized code, and should be very rare.  */
2574   if (save_gr || (save_fr && ! (restart_fr || restart_gr)))
2575     {
2576       pc = orig_pc;
2577       restart_gr = save_gr;
2578       restart_fr = save_fr;
2579       goto restart;
2580     }
2581
2582   return pc;
2583 }
2584
2585 /* Put here the code to store, into a struct frame_saved_regs,
2586    the addresses of the saved registers of frame described by FRAME_INFO.
2587    This includes special registers such as pc and fp saved in special
2588    ways in the stack frame.  sp is even more special:
2589    the address we return for it IS the sp for the next frame.  */
2590
2591 void
2592 hppa_frame_find_saved_regs (frame_info, frame_saved_regs)
2593      struct frame_info *frame_info;
2594      struct frame_saved_regs *frame_saved_regs;
2595 {
2596   CORE_ADDR pc;
2597   struct unwind_table_entry *u;
2598   unsigned long inst, stack_remaining, save_gr, save_fr, save_rp, save_sp;
2599   int status, i, reg;
2600   char buf[4];
2601   int fp_loc = -1;
2602
2603   /* Zero out everything.  */
2604   memset (frame_saved_regs, '\0', sizeof (struct frame_saved_regs));
2605
2606   /* Call dummy frames always look the same, so there's no need to
2607      examine the dummy code to determine locations of saved registers;
2608      instead, let find_dummy_frame_regs fill in the correct offsets
2609      for the saved registers.  */
2610   if ((frame_info->pc >= frame_info->frame
2611        && frame_info->pc <= (frame_info->frame + CALL_DUMMY_LENGTH
2612                              + 32 * 4 +  (NUM_REGS - FP0_REGNUM) * 8
2613                              + 6 * 4))) 
2614     find_dummy_frame_regs (frame_info, frame_saved_regs);
2615
2616   /* Interrupt handlers are special too.  They lay out the register
2617      state in the exact same order as the register numbers in GDB.  */
2618   if (pc_in_interrupt_handler (frame_info->pc))
2619     {
2620       for (i = 0; i < NUM_REGS; i++)
2621         {
2622           /* SP is a little special.  */
2623           if (i == SP_REGNUM)
2624             frame_saved_regs->regs[SP_REGNUM]
2625               = read_memory_integer (frame_info->frame + SP_REGNUM * 4, 4);
2626           else
2627             frame_saved_regs->regs[i] = frame_info->frame + i * 4;
2628         }
2629       return;
2630     }
2631
2632 #ifdef FRAME_FIND_SAVED_REGS_IN_SIGTRAMP
2633   /* Handle signal handler callers.  */
2634   if (frame_info->signal_handler_caller)
2635     {
2636       FRAME_FIND_SAVED_REGS_IN_SIGTRAMP (frame_info, frame_saved_regs);
2637       return;
2638     }
2639 #endif
2640
2641   /* Get the starting address of the function referred to by the PC
2642      saved in frame.  */
2643   pc = get_pc_function_start (frame_info->pc);
2644
2645   /* Yow! */
2646   u = find_unwind_entry (pc);
2647   if (!u)
2648     return;
2649
2650   /* This is how much of a frame adjustment we need to account for.  */
2651   stack_remaining = u->Total_frame_size << 3;
2652
2653   /* Magic register saves we want to know about.  */
2654   save_rp = u->Save_RP;
2655   save_sp = u->Save_SP;
2656
2657   /* Turn the Entry_GR field into a bitmask.  */
2658   save_gr = 0;
2659   for (i = 3; i < u->Entry_GR + 3; i++)
2660     {
2661       /* Frame pointer gets saved into a special location.  */
2662       if (u->Save_SP && i == FP_REGNUM)
2663         continue;
2664
2665       save_gr |= (1 << i);
2666     }
2667
2668   /* Turn the Entry_FR field into a bitmask too.  */
2669   save_fr = 0;
2670   for (i = 12; i < u->Entry_FR + 12; i++)
2671     save_fr |= (1 << i);
2672
2673   /* The frame always represents the value of %sp at entry to the
2674      current function (and is thus equivalent to the "saved" stack
2675      pointer.  */
2676   frame_saved_regs->regs[SP_REGNUM] = frame_info->frame;
2677
2678   /* Loop until we find everything of interest or hit a branch.
2679
2680      For unoptimized GCC code and for any HP CC code this will never ever
2681      examine any user instructions.
2682
2683      For optimzied GCC code we're faced with problems.  GCC will schedule
2684      its prologue and make prologue instructions available for delay slot
2685      filling.  The end result is user code gets mixed in with the prologue
2686      and a prologue instruction may be in the delay slot of the first branch
2687      or call.
2688
2689      Some unexpected things are expected with debugging optimized code, so
2690      we allow this routine to walk past user instructions in optimized
2691      GCC code.  */
2692   while (save_gr || save_fr || save_rp || save_sp || stack_remaining > 0)
2693     {
2694       status = target_read_memory (pc, buf, 4);
2695       inst = extract_unsigned_integer (buf, 4);
2696
2697       /* Yow! */
2698       if (status != 0)
2699         return;
2700
2701       /* Note the interesting effects of this instruction.  */
2702       stack_remaining -= prologue_inst_adjust_sp (inst);
2703
2704       /* There is only one instruction used for saving RP into the stack.  */
2705       if (inst == 0x6bc23fd9)
2706         {
2707           save_rp = 0;
2708           frame_saved_regs->regs[RP_REGNUM] = frame_info->frame - 20;
2709         }
2710
2711       /* Just note that we found the save of SP into the stack.  The
2712          value for frame_saved_regs was computed above.  */
2713       if ((inst & 0xffffc000) == 0x6fc10000)
2714         save_sp = 0;
2715
2716       /* Account for general and floating-point register saves.  */
2717       reg = inst_saves_gr (inst);
2718       if (reg >= 3 && reg <= 18
2719           && (!u->Save_SP || reg != FP_REGNUM))
2720         {
2721           save_gr &= ~(1 << reg);
2722
2723           /* stwm with a positive displacement is a *post modify*.  */
2724           if ((inst >> 26) == 0x1b
2725               && extract_14 (inst) >= 0)
2726             frame_saved_regs->regs[reg] = frame_info->frame;
2727           else
2728             {
2729               /* Handle code with and without frame pointers.  */
2730               if (u->Save_SP)
2731                 frame_saved_regs->regs[reg]
2732                   = frame_info->frame + extract_14 (inst);
2733               else
2734                 frame_saved_regs->regs[reg]
2735                   = frame_info->frame + (u->Total_frame_size << 3)
2736                     + extract_14 (inst);
2737             }
2738         }
2739
2740
2741       /* GCC handles callee saved FP regs a little differently.  
2742
2743          It emits an instruction to put the value of the start of
2744          the FP store area into %r1.  It then uses fstds,ma with
2745          a basereg of %r1 for the stores.
2746
2747          HP CC emits them at the current stack pointer modifying
2748          the stack pointer as it stores each register.  */
2749
2750       /* ldo X(%r3),%r1 or ldo X(%r30),%r1.  */
2751       if ((inst & 0xffffc000) == 0x34610000
2752           || (inst & 0xffffc000) == 0x37c10000)
2753         fp_loc = extract_14 (inst);
2754         
2755       reg = inst_saves_fr (inst);
2756       if (reg >= 12 && reg <= 21)
2757         {
2758           /* Note +4 braindamage below is necessary because the FP status
2759              registers are internally 8 registers rather than the expected
2760              4 registers.  */
2761           save_fr &= ~(1 << reg);
2762           if (fp_loc == -1)
2763             {
2764               /* 1st HP CC FP register store.  After this instruction
2765                  we've set enough state that the GCC and HPCC code are
2766                  both handled in the same manner.  */
2767               frame_saved_regs->regs[reg + FP4_REGNUM + 4] = frame_info->frame;
2768               fp_loc = 8;
2769             }
2770           else
2771             {
2772               frame_saved_regs->regs[reg + FP0_REGNUM + 4]
2773                 = frame_info->frame + fp_loc;
2774               fp_loc += 8;
2775             }
2776         }
2777
2778       /* Quit if we hit any kind of branch.  This can happen if a prologue
2779          instruction is in the delay slot of the first call/branch.  */
2780       if (is_branch (inst))
2781         break;
2782
2783       /* Bump the PC.  */
2784       pc += 4;
2785     }
2786 }
2787
2788 #ifdef MAINTENANCE_CMDS
2789
2790 static void
2791 unwind_command (exp, from_tty)
2792      char *exp;
2793      int from_tty;
2794 {
2795   CORE_ADDR address;
2796   struct unwind_table_entry *u;
2797
2798   /* If we have an expression, evaluate it and use it as the address.  */
2799
2800   if (exp != 0 && *exp != 0)
2801     address = parse_and_eval_address (exp);
2802   else
2803     return;
2804
2805   u = find_unwind_entry (address);
2806
2807   if (!u)
2808     {
2809       printf_unfiltered ("Can't find unwind table entry for %s\n", exp);
2810       return;
2811     }
2812
2813   printf_unfiltered ("unwind_table_entry (0x%x):\n", u);
2814
2815   printf_unfiltered ("\tregion_start = ");
2816   print_address (u->region_start, gdb_stdout);
2817
2818   printf_unfiltered ("\n\tregion_end = ");
2819   print_address (u->region_end, gdb_stdout);
2820
2821 #ifdef __STDC__
2822 #define pif(FLD) if (u->FLD) printf_unfiltered (" "#FLD);
2823 #else
2824 #define pif(FLD) if (u->FLD) printf_unfiltered (" FLD");
2825 #endif
2826
2827   printf_unfiltered ("\n\tflags =");
2828   pif (Cannot_unwind);
2829   pif (Millicode);
2830   pif (Millicode_save_sr0);
2831   pif (Entry_SR);
2832   pif (Args_stored);
2833   pif (Variable_Frame);
2834   pif (Separate_Package_Body);
2835   pif (Frame_Extension_Millicode);
2836   pif (Stack_Overflow_Check);
2837   pif (Two_Instruction_SP_Increment);
2838   pif (Ada_Region);
2839   pif (Save_SP);
2840   pif (Save_RP);
2841   pif (Save_MRP_in_frame);
2842   pif (extn_ptr_defined);
2843   pif (Cleanup_defined);
2844   pif (MPE_XL_interrupt_marker);
2845   pif (HP_UX_interrupt_marker);
2846   pif (Large_frame);
2847
2848   putchar_unfiltered ('\n');
2849
2850 #ifdef __STDC__
2851 #define pin(FLD) printf_unfiltered ("\t"#FLD" = 0x%x\n", u->FLD);
2852 #else
2853 #define pin(FLD) printf_unfiltered ("\tFLD = 0x%x\n", u->FLD);
2854 #endif
2855
2856   pin (Region_description);
2857   pin (Entry_FR);
2858   pin (Entry_GR);
2859   pin (Total_frame_size);
2860 }
2861 #endif /* MAINTENANCE_CMDS */
2862
2863 void
2864 _initialize_hppa_tdep ()
2865 {
2866   tm_print_insn = print_insn_hppa;
2867
2868 #ifdef MAINTENANCE_CMDS
2869   add_cmd ("unwind", class_maintenance, unwind_command,
2870            "Print unwind table entry at given address.",
2871            &maintenanceprintlist);
2872 #endif /* MAINTENANCE_CMDS */
2873 }