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