xref: /openbsd/gnu/usr.bin/binutils/gdb/hppa-tdep.c (revision 4ffb5c22)
1 /* Target-dependent code for the HP PA architecture, for GDB.
2 
3    Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4    1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
5    Foundation, Inc.
6 
7    Contributed by the Center for Software Science at the
8    University of Utah (pa-gdb-bugs@cs.utah.edu).
9 
10    This file is part of GDB.
11 
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 2 of the License, or
15    (at your option) any later version.
16 
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21 
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place - Suite 330,
25    Boston, MA 02111-1307, USA.  */
26 
27 #include "defs.h"
28 #include "bfd.h"
29 #include "inferior.h"
30 #include "regcache.h"
31 #include "completer.h"
32 #include "osabi.h"
33 #include "gdb_assert.h"
34 #include "arch-utils.h"
35 /* For argument passing to the inferior */
36 #include "symtab.h"
37 #include "dis-asm.h"
38 #include "dwarf2-frame.h"
39 #include "trad-frame.h"
40 #include "frame-unwind.h"
41 #include "frame-base.h"
42 
43 #include "gdbcore.h"
44 #include "gdbcmd.h"
45 #include "objfiles.h"
46 #include "hppa-tdep.h"
47 
48 static int hppa_debug = 0;
49 
50 /* Some local constants.  */
51 static const int hppa32_num_regs = 128;
52 static const int hppa64_num_regs = 96;
53 
54 /* hppa-specific object data -- unwind and solib info.
55    TODO/maybe: think about splitting this into two parts; the unwind data is
56    common to all hppa targets, but is only used in this file; we can register
57    that separately and make this static. The solib data is probably hpux-
58    specific, so we can create a separate extern objfile_data that is registered
59    by hppa-hpux-tdep.c and shared with pa64solib.c and somsolib.c.  */
60 const struct objfile_data *hppa_objfile_priv_data = NULL;
61 
62 /* Get at various relevent fields of an instruction word. */
63 #define MASK_5 0x1f
64 #define MASK_11 0x7ff
65 #define MASK_14 0x3fff
66 #define MASK_21 0x1fffff
67 
68 /* Sizes (in bytes) of the native unwind entries.  */
69 #define UNWIND_ENTRY_SIZE 16
70 #define STUB_UNWIND_ENTRY_SIZE 8
71 
72 /* FIXME: brobecker 2002-11-07: We will likely be able to make the
73    following functions static, once we hppa is partially multiarched.  */
74 int hppa_pc_requires_run_before_use (CORE_ADDR pc);
75 int hppa_instruction_nullified (void);
76 
77 /* Handle 32/64-bit struct return conventions.  */
78 
79 static enum return_value_convention
hppa32_return_value(struct gdbarch * gdbarch,struct type * type,struct regcache * regcache,void * readbuf,const void * writebuf)80 hppa32_return_value (struct gdbarch *gdbarch,
81 		     struct type *type, struct regcache *regcache,
82 		     void *readbuf, const void *writebuf)
83 {
84   if (TYPE_LENGTH (type) <= 2 * 4)
85     {
86       /* The value always lives in the right hand end of the register
87 	 (or register pair)?  */
88       int b;
89       int reg = TYPE_CODE (type) == TYPE_CODE_FLT ? HPPA_FP4_REGNUM : 28;
90       int part = TYPE_LENGTH (type) % 4;
91       /* The left hand register contains only part of the value,
92 	 transfer that first so that the rest can be xfered as entire
93 	 4-byte registers.  */
94       if (part > 0)
95 	{
96 	  if (readbuf != NULL)
97 	    regcache_cooked_read_part (regcache, reg, 4 - part,
98 				       part, readbuf);
99 	  if (writebuf != NULL)
100 	    regcache_cooked_write_part (regcache, reg, 4 - part,
101 					part, writebuf);
102 	  reg++;
103 	}
104       /* Now transfer the remaining register values.  */
105       for (b = part; b < TYPE_LENGTH (type); b += 4)
106 	{
107 	  if (readbuf != NULL)
108 	    regcache_cooked_read (regcache, reg, (char *) readbuf + b);
109 	  if (writebuf != NULL)
110 	    regcache_cooked_write (regcache, reg, (const char *) writebuf + b);
111 	  reg++;
112 	}
113       return RETURN_VALUE_REGISTER_CONVENTION;
114     }
115   else
116     return RETURN_VALUE_STRUCT_CONVENTION;
117 }
118 
119 static enum return_value_convention
hppa64_return_value(struct gdbarch * gdbarch,struct type * type,struct regcache * regcache,void * readbuf,const void * writebuf)120 hppa64_return_value (struct gdbarch *gdbarch,
121 		     struct type *type, struct regcache *regcache,
122 		     void *readbuf, const void *writebuf)
123 {
124   /* RM: Floats are returned in FR4R, doubles in FR4.  Integral values
125      are in r28, padded on the left.  Aggregates less that 65 bits are
126      in r28, right padded.  Aggregates upto 128 bits are in r28 and
127      r29, right padded.  */
128   if (TYPE_CODE (type) == TYPE_CODE_FLT
129       && TYPE_LENGTH (type) <= 8)
130     {
131       /* Floats are right aligned?  */
132       int offset = register_size (gdbarch, HPPA_FP4_REGNUM) - TYPE_LENGTH (type);
133       if (readbuf != NULL)
134 	regcache_cooked_read_part (regcache, HPPA_FP4_REGNUM, offset,
135 				   TYPE_LENGTH (type), readbuf);
136       if (writebuf != NULL)
137 	regcache_cooked_write_part (regcache, HPPA_FP4_REGNUM, offset,
138 				    TYPE_LENGTH (type), writebuf);
139       return RETURN_VALUE_REGISTER_CONVENTION;
140     }
141   else if (TYPE_LENGTH (type) <= 8 && is_integral_type (type))
142     {
143       /* Integrals are right aligned.  */
144       int offset = register_size (gdbarch, HPPA_FP4_REGNUM) - TYPE_LENGTH (type);
145       if (readbuf != NULL)
146 	regcache_cooked_read_part (regcache, 28, offset,
147 				   TYPE_LENGTH (type), readbuf);
148       if (writebuf != NULL)
149 	regcache_cooked_write_part (regcache, 28, offset,
150 				    TYPE_LENGTH (type), writebuf);
151       return RETURN_VALUE_REGISTER_CONVENTION;
152     }
153   else if (TYPE_LENGTH (type) <= 2 * 8)
154     {
155       /* Composite values are left aligned.  */
156       int b;
157       for (b = 0; b < TYPE_LENGTH (type); b += 8)
158 	{
159 	  int part = min (8, TYPE_LENGTH (type) - b);
160 	  if (readbuf != NULL)
161 	    regcache_cooked_read_part (regcache, 28 + b / 8, 0, part,
162 				       (char *) readbuf + b);
163 	  if (writebuf != NULL)
164 	    regcache_cooked_write_part (regcache, 28 + b / 8, 0, part,
165 					(const char *) writebuf + b);
166 	}
167       return RETURN_VALUE_REGISTER_CONVENTION;
168     }
169   else
170     return RETURN_VALUE_STRUCT_CONVENTION;
171 }
172 
173 /* Routines to extract various sized constants out of hppa
174    instructions. */
175 
176 /* This assumes that no garbage lies outside of the lower bits of
177    value. */
178 
179 int
hppa_sign_extend(unsigned val,unsigned bits)180 hppa_sign_extend (unsigned val, unsigned bits)
181 {
182   return (int) (val >> (bits - 1) ? (-1 << bits) | val : val);
183 }
184 
185 /* For many immediate values the sign bit is the low bit! */
186 
187 int
hppa_low_hppa_sign_extend(unsigned val,unsigned bits)188 hppa_low_hppa_sign_extend (unsigned val, unsigned bits)
189 {
190   return (int) ((val & 0x1 ? (-1 << (bits - 1)) : 0) | val >> 1);
191 }
192 
193 /* Extract the bits at positions between FROM and TO, using HP's numbering
194    (MSB = 0). */
195 
196 int
hppa_get_field(unsigned word,int from,int to)197 hppa_get_field (unsigned word, int from, int to)
198 {
199   return ((word) >> (31 - (to)) & ((1 << ((to) - (from) + 1)) - 1));
200 }
201 
202 /* extract the immediate field from a ld{bhw}s instruction */
203 
204 int
hppa_extract_5_load(unsigned word)205 hppa_extract_5_load (unsigned word)
206 {
207   return hppa_low_hppa_sign_extend (word >> 16 & MASK_5, 5);
208 }
209 
210 /* extract the immediate field from a break instruction */
211 
212 unsigned
hppa_extract_5r_store(unsigned word)213 hppa_extract_5r_store (unsigned word)
214 {
215   return (word & MASK_5);
216 }
217 
218 /* extract the immediate field from a {sr}sm instruction */
219 
220 unsigned
hppa_extract_5R_store(unsigned word)221 hppa_extract_5R_store (unsigned word)
222 {
223   return (word >> 16 & MASK_5);
224 }
225 
226 /* extract a 14 bit immediate field */
227 
228 int
hppa_extract_14(unsigned word)229 hppa_extract_14 (unsigned word)
230 {
231   return hppa_low_hppa_sign_extend (word & MASK_14, 14);
232 }
233 
234 /* extract a 21 bit constant */
235 
236 int
hppa_extract_21(unsigned word)237 hppa_extract_21 (unsigned word)
238 {
239   int val;
240 
241   word &= MASK_21;
242   word <<= 11;
243   val = hppa_get_field (word, 20, 20);
244   val <<= 11;
245   val |= hppa_get_field (word, 9, 19);
246   val <<= 2;
247   val |= hppa_get_field (word, 5, 6);
248   val <<= 5;
249   val |= hppa_get_field (word, 0, 4);
250   val <<= 2;
251   val |= hppa_get_field (word, 7, 8);
252   return hppa_sign_extend (val, 21) << 11;
253 }
254 
255 /* extract a 17 bit constant from branch instructions, returning the
256    19 bit signed value. */
257 
258 int
hppa_extract_17(unsigned word)259 hppa_extract_17 (unsigned word)
260 {
261   return hppa_sign_extend (hppa_get_field (word, 19, 28) |
262 		      hppa_get_field (word, 29, 29) << 10 |
263 		      hppa_get_field (word, 11, 15) << 11 |
264 		      (word & 0x1) << 16, 17) << 2;
265 }
266 
267 CORE_ADDR
hppa_symbol_address(const char * sym)268 hppa_symbol_address(const char *sym)
269 {
270   struct minimal_symbol *minsym;
271 
272   minsym = lookup_minimal_symbol (sym, NULL, NULL);
273   if (minsym)
274     return SYMBOL_VALUE_ADDRESS (minsym);
275   else
276     return (CORE_ADDR)-1;
277 }
278 
279 
280 /* Compare the start address for two unwind entries returning 1 if
281    the first address is larger than the second, -1 if the second is
282    larger than the first, and zero if they are equal.  */
283 
284 static int
compare_unwind_entries(const void * arg1,const void * arg2)285 compare_unwind_entries (const void *arg1, const void *arg2)
286 {
287   const struct unwind_table_entry *a = arg1;
288   const struct unwind_table_entry *b = arg2;
289 
290   if (a->region_start > b->region_start)
291     return 1;
292   else if (a->region_start < b->region_start)
293     return -1;
294   else
295     return 0;
296 }
297 
298 static void
record_text_segment_lowaddr(bfd * abfd,asection * section,void * data)299 record_text_segment_lowaddr (bfd *abfd, asection *section, void *data)
300 {
301   if ((section->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
302        == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
303     {
304       bfd_vma value = section->vma - section->filepos;
305       CORE_ADDR *low_text_segment_address = (CORE_ADDR *)data;
306 
307       if (value < *low_text_segment_address)
308           *low_text_segment_address = value;
309     }
310 }
311 
312 static void
internalize_unwinds(struct objfile * objfile,struct unwind_table_entry * table,asection * section,unsigned int entries,unsigned int size,CORE_ADDR text_offset)313 internalize_unwinds (struct objfile *objfile, struct unwind_table_entry *table,
314 		     asection *section, unsigned int entries, unsigned int size,
315 		     CORE_ADDR text_offset)
316 {
317   /* We will read the unwind entries into temporary memory, then
318      fill in the actual unwind table.  */
319 
320   if (size > 0)
321     {
322       unsigned long tmp;
323       unsigned i;
324       char *buf = alloca (size);
325       CORE_ADDR low_text_segment_address;
326 
327       /* For ELF targets, then unwinds are supposed to
328 	 be segment relative offsets instead of absolute addresses.
329 
330 	 Note that when loading a shared library (text_offset != 0) the
331 	 unwinds are already relative to the text_offset that will be
332 	 passed in.  */
333       if (gdbarch_tdep (current_gdbarch)->is_elf && text_offset == 0)
334 	{
335           low_text_segment_address = -1;
336 
337 	  bfd_map_over_sections (objfile->obfd,
338 				 record_text_segment_lowaddr,
339 				 &low_text_segment_address);
340 
341 	  text_offset = low_text_segment_address;
342 	}
343 
344       bfd_get_section_contents (objfile->obfd, section, buf, 0, size);
345 
346       /* Now internalize the information being careful to handle host/target
347          endian issues.  */
348       for (i = 0; i < entries; i++)
349 	{
350 	  table[i].region_start = bfd_get_32 (objfile->obfd,
351 					      (bfd_byte *) buf);
352 	  table[i].region_start += text_offset;
353 	  buf += 4;
354 	  table[i].region_end = bfd_get_32 (objfile->obfd, (bfd_byte *) buf);
355 	  table[i].region_end += text_offset;
356 	  buf += 4;
357 	  tmp = bfd_get_32 (objfile->obfd, (bfd_byte *) buf);
358 	  buf += 4;
359 	  table[i].Cannot_unwind = (tmp >> 31) & 0x1;
360 	  table[i].Millicode = (tmp >> 30) & 0x1;
361 	  table[i].Millicode_save_sr0 = (tmp >> 29) & 0x1;
362 	  table[i].Region_description = (tmp >> 27) & 0x3;
363 	  table[i].reserved1 = (tmp >> 26) & 0x1;
364 	  table[i].Entry_SR = (tmp >> 25) & 0x1;
365 	  table[i].Entry_FR = (tmp >> 21) & 0xf;
366 	  table[i].Entry_GR = (tmp >> 16) & 0x1f;
367 	  table[i].Args_stored = (tmp >> 15) & 0x1;
368 	  table[i].Variable_Frame = (tmp >> 14) & 0x1;
369 	  table[i].Separate_Package_Body = (tmp >> 13) & 0x1;
370 	  table[i].Frame_Extension_Millicode = (tmp >> 12) & 0x1;
371 	  table[i].Stack_Overflow_Check = (tmp >> 11) & 0x1;
372 	  table[i].Two_Instruction_SP_Increment = (tmp >> 10) & 0x1;
373 	  table[i].Ada_Region = (tmp >> 9) & 0x1;
374 	  table[i].cxx_info = (tmp >> 8) & 0x1;
375 	  table[i].cxx_try_catch = (tmp >> 7) & 0x1;
376 	  table[i].sched_entry_seq = (tmp >> 6) & 0x1;
377 	  table[i].reserved2 = (tmp >> 5) & 0x1;
378 	  table[i].Save_SP = (tmp >> 4) & 0x1;
379 	  table[i].Save_RP = (tmp >> 3) & 0x1;
380 	  table[i].Save_MRP_in_frame = (tmp >> 2) & 0x1;
381 	  table[i].extn_ptr_defined = (tmp >> 1) & 0x1;
382 	  table[i].Cleanup_defined = tmp & 0x1;
383 	  tmp = bfd_get_32 (objfile->obfd, (bfd_byte *) buf);
384 	  buf += 4;
385 	  table[i].MPE_XL_interrupt_marker = (tmp >> 31) & 0x1;
386 	  table[i].HP_UX_interrupt_marker = (tmp >> 30) & 0x1;
387 	  table[i].Large_frame = (tmp >> 29) & 0x1;
388 	  table[i].Pseudo_SP_Set = (tmp >> 28) & 0x1;
389 	  table[i].reserved4 = (tmp >> 27) & 0x1;
390 	  table[i].Total_frame_size = tmp & 0x7ffffff;
391 
392 	  /* Stub unwinds are handled elsewhere. */
393 	  table[i].stub_unwind.stub_type = 0;
394 	  table[i].stub_unwind.padding = 0;
395 	}
396     }
397 }
398 
399 /* Read in the backtrace information stored in the `$UNWIND_START$' section of
400    the object file.  This info is used mainly by find_unwind_entry() to find
401    out the stack frame size and frame pointer used by procedures.  We put
402    everything on the psymbol obstack in the objfile so that it automatically
403    gets freed when the objfile is destroyed.  */
404 
405 static void
read_unwind_info(struct objfile * objfile)406 read_unwind_info (struct objfile *objfile)
407 {
408   asection *unwind_sec, *stub_unwind_sec;
409   unsigned unwind_size, stub_unwind_size, total_size;
410   unsigned index, unwind_entries;
411   unsigned stub_entries, total_entries;
412   CORE_ADDR text_offset;
413   struct hppa_unwind_info *ui;
414   struct hppa_objfile_private *obj_private;
415 
416   text_offset = ANOFFSET (objfile->section_offsets, 0);
417   ui = (struct hppa_unwind_info *) obstack_alloc (&objfile->objfile_obstack,
418 					   sizeof (struct hppa_unwind_info));
419 
420   ui->table = NULL;
421   ui->cache = NULL;
422   ui->last = -1;
423 
424   /* For reasons unknown the HP PA64 tools generate multiple unwinder
425      sections in a single executable.  So we just iterate over every
426      section in the BFD looking for unwinder sections intead of trying
427      to do a lookup with bfd_get_section_by_name.
428 
429      First determine the total size of the unwind tables so that we
430      can allocate memory in a nice big hunk.  */
431   total_entries = 0;
432   for (unwind_sec = objfile->obfd->sections;
433        unwind_sec;
434        unwind_sec = unwind_sec->next)
435     {
436       if (strcmp (unwind_sec->name, "$UNWIND_START$") == 0
437 	  || strcmp (unwind_sec->name, ".PARISC.unwind") == 0)
438 	{
439 	  unwind_size = bfd_section_size (objfile->obfd, unwind_sec);
440 	  unwind_entries = unwind_size / UNWIND_ENTRY_SIZE;
441 
442 	  total_entries += unwind_entries;
443 	}
444     }
445 
446   /* Now compute the size of the stub unwinds.  Note the ELF tools do not
447      use stub unwinds at the curren time.  */
448   stub_unwind_sec = bfd_get_section_by_name (objfile->obfd, "$UNWIND_END$");
449 
450   if (stub_unwind_sec)
451     {
452       stub_unwind_size = bfd_section_size (objfile->obfd, stub_unwind_sec);
453       stub_entries = stub_unwind_size / STUB_UNWIND_ENTRY_SIZE;
454     }
455   else
456     {
457       stub_unwind_size = 0;
458       stub_entries = 0;
459     }
460 
461   /* Compute total number of unwind entries and their total size.  */
462   total_entries += stub_entries;
463   total_size = total_entries * sizeof (struct unwind_table_entry);
464 
465   /* Allocate memory for the unwind table.  */
466   ui->table = (struct unwind_table_entry *)
467     obstack_alloc (&objfile->objfile_obstack, total_size);
468   ui->last = total_entries - 1;
469 
470   /* Now read in each unwind section and internalize the standard unwind
471      entries.  */
472   index = 0;
473   for (unwind_sec = objfile->obfd->sections;
474        unwind_sec;
475        unwind_sec = unwind_sec->next)
476     {
477       if (strcmp (unwind_sec->name, "$UNWIND_START$") == 0
478 	  || strcmp (unwind_sec->name, ".PARISC.unwind") == 0)
479 	{
480 	  unwind_size = bfd_section_size (objfile->obfd, unwind_sec);
481 	  unwind_entries = unwind_size / UNWIND_ENTRY_SIZE;
482 
483 	  internalize_unwinds (objfile, &ui->table[index], unwind_sec,
484 			       unwind_entries, unwind_size, text_offset);
485 	  index += unwind_entries;
486 	}
487     }
488 
489   /* Now read in and internalize the stub unwind entries.  */
490   if (stub_unwind_size > 0)
491     {
492       unsigned int i;
493       char *buf = alloca (stub_unwind_size);
494 
495       /* Read in the stub unwind entries.  */
496       bfd_get_section_contents (objfile->obfd, stub_unwind_sec, buf,
497 				0, stub_unwind_size);
498 
499       /* Now convert them into regular unwind entries.  */
500       for (i = 0; i < stub_entries; i++, index++)
501 	{
502 	  /* Clear out the next unwind entry.  */
503 	  memset (&ui->table[index], 0, sizeof (struct unwind_table_entry));
504 
505 	  /* Convert offset & size into region_start and region_end.
506 	     Stuff away the stub type into "reserved" fields.  */
507 	  ui->table[index].region_start = bfd_get_32 (objfile->obfd,
508 						      (bfd_byte *) buf);
509 	  ui->table[index].region_start += text_offset;
510 	  buf += 4;
511 	  ui->table[index].stub_unwind.stub_type = bfd_get_8 (objfile->obfd,
512 							  (bfd_byte *) buf);
513 	  buf += 2;
514 	  ui->table[index].region_end
515 	    = ui->table[index].region_start + 4 *
516 	    (bfd_get_16 (objfile->obfd, (bfd_byte *) buf) - 1);
517 	  buf += 2;
518 	}
519 
520     }
521 
522   /* Unwind table needs to be kept sorted.  */
523   qsort (ui->table, total_entries, sizeof (struct unwind_table_entry),
524 	 compare_unwind_entries);
525 
526   /* Keep a pointer to the unwind information.  */
527   obj_private = (struct hppa_objfile_private *)
528 	        objfile_data (objfile, hppa_objfile_priv_data);
529   if (obj_private == NULL)
530     {
531       obj_private = (struct hppa_objfile_private *)
532 	obstack_alloc (&objfile->objfile_obstack,
533                        sizeof (struct hppa_objfile_private));
534       set_objfile_data (objfile, hppa_objfile_priv_data, obj_private);
535       obj_private->unwind_info = NULL;
536       obj_private->so_info = NULL;
537       obj_private->dp = 0;
538     }
539   obj_private->unwind_info = ui;
540 }
541 
542 /* Lookup the unwind (stack backtrace) info for the given PC.  We search all
543    of the objfiles seeking the unwind table entry for this PC.  Each objfile
544    contains a sorted list of struct unwind_table_entry.  Since we do a binary
545    search of the unwind tables, we depend upon them to be sorted.  */
546 
547 struct unwind_table_entry *
find_unwind_entry(CORE_ADDR pc)548 find_unwind_entry (CORE_ADDR pc)
549 {
550   int first, middle, last;
551   struct objfile *objfile;
552   struct hppa_objfile_private *priv;
553 
554   if (hppa_debug)
555     fprintf_unfiltered (gdb_stdlog, "{ find_unwind_entry 0x%s -> ",
556 		        paddr_nz (pc));
557 
558   /* A function at address 0?  Not in HP-UX! */
559   if (pc == (CORE_ADDR) 0)
560     {
561       if (hppa_debug)
562 	fprintf_unfiltered (gdb_stdlog, "NULL }\n");
563       return NULL;
564     }
565 
566   ALL_OBJFILES (objfile)
567   {
568     struct hppa_unwind_info *ui;
569     ui = NULL;
570     priv = objfile_data (objfile, hppa_objfile_priv_data);
571     if (priv)
572       ui = ((struct hppa_objfile_private *) priv)->unwind_info;
573 
574     if (!ui)
575       {
576 	read_unwind_info (objfile);
577         priv = objfile_data (objfile, hppa_objfile_priv_data);
578 	if (priv == NULL)
579 	  error ("Internal error reading unwind information.");
580         ui = ((struct hppa_objfile_private *) priv)->unwind_info;
581       }
582 
583     /* First, check the cache */
584 
585     if (ui->cache
586 	&& pc >= ui->cache->region_start
587 	&& pc <= ui->cache->region_end)
588       {
589 	if (hppa_debug)
590 	  fprintf_unfiltered (gdb_stdlog, "0x%s (cached) }\n",
591             paddr_nz ((CORE_ADDR) ui->cache));
592         return ui->cache;
593       }
594 
595     /* Not in the cache, do a binary search */
596 
597     first = 0;
598     last = ui->last;
599 
600     while (first <= last)
601       {
602 	middle = (first + last) / 2;
603 	if (pc >= ui->table[middle].region_start
604 	    && pc <= ui->table[middle].region_end)
605 	  {
606 	    ui->cache = &ui->table[middle];
607 	    if (hppa_debug)
608 	      fprintf_unfiltered (gdb_stdlog, "0x%s }\n",
609                 paddr_nz ((CORE_ADDR) ui->cache));
610 	    return &ui->table[middle];
611 	  }
612 
613 	if (pc < ui->table[middle].region_start)
614 	  last = middle - 1;
615 	else
616 	  first = middle + 1;
617       }
618   }				/* ALL_OBJFILES() */
619 
620   if (hppa_debug)
621     fprintf_unfiltered (gdb_stdlog, "NULL (not found) }\n");
622 
623   return NULL;
624 }
625 
626 static const unsigned char *
hppa_breakpoint_from_pc(CORE_ADDR * pc,int * len)627 hppa_breakpoint_from_pc (CORE_ADDR *pc, int *len)
628 {
629   static const unsigned char breakpoint[] = {0x00, 0x01, 0x00, 0x04};
630   (*len) = sizeof (breakpoint);
631   return breakpoint;
632 }
633 
634 /* Return the name of a register.  */
635 
636 const char *
hppa32_register_name(int i)637 hppa32_register_name (int i)
638 {
639   static char *names[] = {
640     "flags",  "r1",      "rp",     "r3",
641     "r4",     "r5",      "r6",     "r7",
642     "r8",     "r9",      "r10",    "r11",
643     "r12",    "r13",     "r14",    "r15",
644     "r16",    "r17",     "r18",    "r19",
645     "r20",    "r21",     "r22",    "r23",
646     "r24",    "r25",     "r26",    "dp",
647     "ret0",   "ret1",    "sp",     "r31",
648     "sar",    "pcoqh",   "pcsqh",  "pcoqt",
649     "pcsqt",  "eiem",    "iir",    "isr",
650     "ior",    "ipsw",    "goto",   "sr4",
651     "sr0",    "sr1",     "sr2",    "sr3",
652     "sr5",    "sr6",     "sr7",    "cr0",
653     "cr8",    "cr9",     "ccr",    "cr12",
654     "cr13",   "cr24",    "cr25",   "cr26",
655     "mpsfu_high","mpsfu_low","mpsfu_ovflo","pad",
656     "fpsr",    "fpe1",   "fpe2",   "fpe3",
657     "fpe4",   "fpe5",    "fpe6",   "fpe7",
658     "fr4",     "fr4R",   "fr5",    "fr5R",
659     "fr6",    "fr6R",    "fr7",    "fr7R",
660     "fr8",     "fr8R",   "fr9",    "fr9R",
661     "fr10",   "fr10R",   "fr11",   "fr11R",
662     "fr12",    "fr12R",  "fr13",   "fr13R",
663     "fr14",   "fr14R",   "fr15",   "fr15R",
664     "fr16",    "fr16R",  "fr17",   "fr17R",
665     "fr18",   "fr18R",   "fr19",   "fr19R",
666     "fr20",    "fr20R",  "fr21",   "fr21R",
667     "fr22",   "fr22R",   "fr23",   "fr23R",
668     "fr24",    "fr24R",  "fr25",   "fr25R",
669     "fr26",   "fr26R",   "fr27",   "fr27R",
670     "fr28",    "fr28R",  "fr29",   "fr29R",
671     "fr30",   "fr30R",   "fr31",   "fr31R"
672   };
673   if (i < 0 || i >= (sizeof (names) / sizeof (*names)))
674     return NULL;
675   else
676     return names[i];
677 }
678 
679 const char *
hppa64_register_name(int i)680 hppa64_register_name (int i)
681 {
682   static char *names[] = {
683     "flags",  "r1",      "rp",     "r3",
684     "r4",     "r5",      "r6",     "r7",
685     "r8",     "r9",      "r10",    "r11",
686     "r12",    "r13",     "r14",    "r15",
687     "r16",    "r17",     "r18",    "r19",
688     "r20",    "r21",     "r22",    "r23",
689     "r24",    "r25",     "r26",    "dp",
690     "ret0",   "ret1",    "sp",     "r31",
691     "sar",    "pcoqh",   "pcsqh",  "pcoqt",
692     "pcsqt",  "eiem",    "iir",    "isr",
693     "ior",    "ipsw",    "goto",   "sr4",
694     "sr0",    "sr1",     "sr2",    "sr3",
695     "sr5",    "sr6",     "sr7",    "cr0",
696     "cr8",    "cr9",     "ccr",    "cr12",
697     "cr13",   "cr24",    "cr25",   "cr26",
698     "mpsfu_high","mpsfu_low","mpsfu_ovflo","pad",
699     "fpsr",    "fpe1",   "fpe2",   "fpe3",
700     "fr4",    "fr5",     "fr6",    "fr7",
701     "fr8",     "fr9",    "fr10",   "fr11",
702     "fr12",   "fr13",    "fr14",   "fr15",
703     "fr16",    "fr17",   "fr18",   "fr19",
704     "fr20",   "fr21",    "fr22",   "fr23",
705     "fr24",    "fr25",   "fr26",   "fr27",
706     "fr28",  "fr29",    "fr30",   "fr31"
707   };
708   if (i < 0 || i >= (sizeof (names) / sizeof (*names)))
709     return NULL;
710   else
711     return names[i];
712 }
713 
714 /* This function pushes a stack frame with arguments as part of the
715    inferior function calling mechanism.
716 
717    This is the version of the function for the 32-bit PA machines, in
718    which later arguments appear at lower addresses.  (The stack always
719    grows towards higher addresses.)
720 
721    We simply allocate the appropriate amount of stack space and put
722    arguments into their proper slots.  */
723 
724 CORE_ADDR
hppa32_push_dummy_call(struct gdbarch * gdbarch,struct value * function,struct regcache * regcache,CORE_ADDR bp_addr,int nargs,struct value ** args,CORE_ADDR sp,int struct_return,CORE_ADDR struct_addr)725 hppa32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
726 			struct regcache *regcache, CORE_ADDR bp_addr,
727 			int nargs, struct value **args, CORE_ADDR sp,
728 			int struct_return, CORE_ADDR struct_addr)
729 {
730   /* Stack base address at which any pass-by-reference parameters are
731      stored.  */
732   CORE_ADDR struct_end = 0;
733   /* Stack base address at which the first parameter is stored.  */
734   CORE_ADDR param_end = 0;
735 
736   /* The inner most end of the stack after all the parameters have
737      been pushed.  */
738   CORE_ADDR new_sp = 0;
739 
740   /* Two passes.  First pass computes the location of everything,
741      second pass writes the bytes out.  */
742   int write_pass;
743 
744   /* Global pointer (r19) of the function we are trying to call.  */
745   CORE_ADDR gp;
746 
747   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
748 
749   for (write_pass = 0; write_pass < 2; write_pass++)
750     {
751       CORE_ADDR struct_ptr = 0;
752       /* The first parameter goes into sp-36, each stack slot is 4-bytes.
753          struct_ptr is adjusted for each argument below, so the first
754 	 argument will end up at sp-36.  */
755       CORE_ADDR param_ptr = 32;
756       int i;
757       int small_struct = 0;
758 
759       for (i = 0; i < nargs; i++)
760 	{
761 	  struct value *arg = args[i];
762 	  struct type *type = check_typedef (VALUE_TYPE (arg));
763 	  /* The corresponding parameter that is pushed onto the
764 	     stack, and [possibly] passed in a register.  */
765 	  char param_val[8];
766 	  int param_len;
767 	  memset (param_val, 0, sizeof param_val);
768 	  if (TYPE_LENGTH (type) > 8)
769 	    {
770 	      /* Large parameter, pass by reference.  Store the value
771 		 in "struct" area and then pass its address.  */
772 	      param_len = 4;
773 	      struct_ptr += align_up (TYPE_LENGTH (type), 8);
774 	      if (write_pass)
775 		write_memory (struct_end - struct_ptr, VALUE_CONTENTS (arg),
776 			      TYPE_LENGTH (type));
777 	      store_unsigned_integer (param_val, 4, struct_end - struct_ptr);
778 	    }
779 	  else if (TYPE_CODE (type) == TYPE_CODE_INT
780 		   || TYPE_CODE (type) == TYPE_CODE_ENUM)
781 	    {
782 	      /* Integer value store, right aligned.  "unpack_long"
783 		 takes care of any sign-extension problems.  */
784 	      param_len = align_up (TYPE_LENGTH (type), 4);
785 	      store_unsigned_integer (param_val, param_len,
786 				      unpack_long (type,
787 						   VALUE_CONTENTS (arg)));
788 	    }
789 	  else if (TYPE_CODE (type) == TYPE_CODE_FLT)
790             {
791 	      /* Floating point value store, right aligned.  */
792 	      param_len = align_up (TYPE_LENGTH (type), 4);
793 	      memcpy (param_val, VALUE_CONTENTS (arg), param_len);
794             }
795 	  else
796 	    {
797 	      param_len = align_up (TYPE_LENGTH (type), 4);
798 
799 	      /* Small struct value are stored right-aligned.  */
800 	      memcpy (param_val + param_len - TYPE_LENGTH (type),
801 		      VALUE_CONTENTS (arg), TYPE_LENGTH (type));
802 
803 	      /* Structures of size 5, 6 and 7 bytes are special in that
804 	         the higher-ordered word is stored in the lower-ordered
805 		 argument, and even though it is a 8-byte quantity the
806 		 registers need not be 8-byte aligned.  */
807 	      if (param_len > 4 && param_len < 8)
808 		small_struct = 1;
809 	    }
810 
811 	  param_ptr += param_len;
812 	  if (param_len == 8 && !small_struct)
813             param_ptr = align_up (param_ptr, 8);
814 
815 	  /* First 4 non-FP arguments are passed in gr26-gr23.
816 	     First 4 32-bit FP arguments are passed in fr4L-fr7L.
817 	     First 2 64-bit FP arguments are passed in fr5 and fr7.
818 
819 	     The rest go on the stack, starting at sp-36, towards lower
820 	     addresses.  8-byte arguments must be aligned to a 8-byte
821 	     stack boundary.  */
822 	  if (write_pass)
823 	    {
824 	      write_memory (param_end - param_ptr, param_val, param_len);
825 
826 	      /* There are some cases when we don't know the type
827 		 expected by the callee (e.g. for variadic functions), so
828 		 pass the parameters in both general and fp regs.  */
829 	      if (param_ptr <= 48)
830 		{
831 		  int grreg = 26 - (param_ptr - 36) / 4;
832 		  int fpLreg = 72 + (param_ptr - 36) / 4 * 2;
833 		  int fpreg = 74 + (param_ptr - 32) / 8 * 4;
834 
835 		  regcache_cooked_write (regcache, grreg, param_val);
836 		  regcache_cooked_write (regcache, fpLreg, param_val);
837 
838 		  if (param_len > 4)
839 		    {
840 		      regcache_cooked_write (regcache, grreg + 1,
841 					     param_val + 4);
842 
843 		      regcache_cooked_write (regcache, fpreg, param_val);
844 		      regcache_cooked_write (regcache, fpreg + 1,
845 					     param_val + 4);
846 		    }
847 		}
848 	    }
849 	}
850 
851       /* Update the various stack pointers.  */
852       if (!write_pass)
853 	{
854 	  struct_end = sp + align_up (struct_ptr, 64);
855 	  /* PARAM_PTR already accounts for all the arguments passed
856 	     by the user.  However, the ABI mandates minimum stack
857 	     space allocations for outgoing arguments.  The ABI also
858 	     mandates minimum stack alignments which we must
859 	     preserve.  */
860 	  param_end = struct_end + align_up (param_ptr, 64);
861 	}
862     }
863 
864   /* If a structure has to be returned, set up register 28 to hold its
865      address */
866   if (struct_return)
867     write_register (28, struct_addr);
868 
869   gp = tdep->find_global_pointer (function);
870 
871   if (gp != 0)
872     write_register (19, gp);
873 
874   /* Set the return address.  */
875   regcache_cooked_write_unsigned (regcache, HPPA_RP_REGNUM, bp_addr);
876 
877   /* Update the Stack Pointer.  */
878   regcache_cooked_write_unsigned (regcache, HPPA_SP_REGNUM, param_end);
879 
880   return param_end;
881 }
882 
883 /* This function pushes a stack frame with arguments as part of the
884    inferior function calling mechanism.
885 
886    This is the version for the PA64, in which later arguments appear
887    at higher addresses.  (The stack always grows towards higher
888    addresses.)
889 
890    We simply allocate the appropriate amount of stack space and put
891    arguments into their proper slots.
892 
893    This ABI also requires that the caller provide an argument pointer
894    to the callee, so we do that too.  */
895 
896 CORE_ADDR
hppa64_push_dummy_call(struct gdbarch * gdbarch,struct value * function,struct regcache * regcache,CORE_ADDR bp_addr,int nargs,struct value ** args,CORE_ADDR sp,int struct_return,CORE_ADDR struct_addr)897 hppa64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
898 			struct regcache *regcache, CORE_ADDR bp_addr,
899 			int nargs, struct value **args, CORE_ADDR sp,
900 			int struct_return, CORE_ADDR struct_addr)
901 {
902   /* NOTE: cagney/2004-02-27: This is a guess - its implemented by
903      reverse engineering testsuite failures.  */
904 
905   /* Stack base address at which any pass-by-reference parameters are
906      stored.  */
907   CORE_ADDR struct_end = 0;
908   /* Stack base address at which the first parameter is stored.  */
909   CORE_ADDR param_end = 0;
910 
911   /* The inner most end of the stack after all the parameters have
912      been pushed.  */
913   CORE_ADDR new_sp = 0;
914 
915   /* Two passes.  First pass computes the location of everything,
916      second pass writes the bytes out.  */
917   int write_pass;
918   for (write_pass = 0; write_pass < 2; write_pass++)
919     {
920       CORE_ADDR struct_ptr = 0;
921       CORE_ADDR param_ptr = 0;
922       int i;
923       for (i = 0; i < nargs; i++)
924 	{
925 	  struct value *arg = args[i];
926 	  struct type *type = check_typedef (VALUE_TYPE (arg));
927 	  if ((TYPE_CODE (type) == TYPE_CODE_INT
928 	       || TYPE_CODE (type) == TYPE_CODE_ENUM)
929 	      && TYPE_LENGTH (type) <= 8)
930 	    {
931 	      /* Integer value store, right aligned.  "unpack_long"
932 		 takes care of any sign-extension problems.  */
933 	      param_ptr += 8;
934 	      if (write_pass)
935 		{
936 		  ULONGEST val = unpack_long (type, VALUE_CONTENTS (arg));
937 		  int reg = 27 - param_ptr / 8;
938 		  write_memory_unsigned_integer (param_end - param_ptr,
939 						 val, 8);
940 		  if (reg >= 19)
941 		    regcache_cooked_write_unsigned (regcache, reg, val);
942 		}
943 	    }
944 	  else
945 	    {
946 	      /* Small struct value, store left aligned?  */
947 	      int reg;
948 	      if (TYPE_LENGTH (type) > 8)
949 		{
950 		  param_ptr = align_up (param_ptr, 16);
951 		  reg = 26 - param_ptr / 8;
952 		  param_ptr += align_up (TYPE_LENGTH (type), 16);
953 		}
954 	      else
955 		{
956 		  param_ptr = align_up (param_ptr, 8);
957 		  reg = 26 - param_ptr / 8;
958 		  param_ptr += align_up (TYPE_LENGTH (type), 8);
959 		}
960 	      if (write_pass)
961 		{
962 		  int byte;
963 		  write_memory (param_end - param_ptr, VALUE_CONTENTS (arg),
964 				TYPE_LENGTH (type));
965 		  for (byte = 0; byte < TYPE_LENGTH (type); byte += 8)
966 		    {
967 		      if (reg >= 19)
968 			{
969 			  int len = min (8, TYPE_LENGTH (type) - byte);
970 			  regcache_cooked_write_part (regcache, reg, 0, len,
971 						      VALUE_CONTENTS (arg) + byte);
972 			}
973 		      reg--;
974 		    }
975 		}
976 	    }
977 	}
978       /* Update the various stack pointers.  */
979       if (!write_pass)
980 	{
981 	  struct_end = sp + struct_ptr;
982 	  /* PARAM_PTR already accounts for all the arguments passed
983 	     by the user.  However, the ABI mandates minimum stack
984 	     space allocations for outgoing arguments.  The ABI also
985 	     mandates minimum stack alignments which we must
986 	     preserve.  */
987 	  param_end = struct_end + max (align_up (param_ptr, 16), 64);
988 	}
989     }
990 
991   /* If a structure has to be returned, set up register 28 to hold its
992      address */
993   if (struct_return)
994     write_register (28, struct_addr);
995 
996   /* Set the return address.  */
997   regcache_cooked_write_unsigned (regcache, HPPA_RP_REGNUM, bp_addr);
998 
999   /* Update the Stack Pointer.  */
1000   regcache_cooked_write_unsigned (regcache, HPPA_SP_REGNUM, param_end + 64);
1001 
1002   /* The stack will have 32 bytes of additional space for a frame marker.  */
1003   return param_end + 64;
1004 }
1005 
1006 static CORE_ADDR
hppa32_convert_from_func_ptr_addr(struct gdbarch * gdbarch,CORE_ADDR addr,struct target_ops * targ)1007 hppa32_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
1008 				   CORE_ADDR addr,
1009 				   struct target_ops *targ)
1010 {
1011   if (addr & 2)
1012     {
1013       CORE_ADDR plabel;
1014 
1015       plabel = addr & ~3;
1016       target_read_memory(plabel, (char *)&addr, 4);
1017     }
1018 
1019   return addr;
1020 }
1021 
1022 static CORE_ADDR
hppa32_frame_align(struct gdbarch * gdbarch,CORE_ADDR addr)1023 hppa32_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
1024 {
1025   /* HP frames are 64-byte (or cache line) aligned (yes that's _byte_
1026      and not _bit_)!  */
1027   return align_up (addr, 64);
1028 }
1029 
1030 /* Force all frames to 16-byte alignment.  Better safe than sorry.  */
1031 
1032 static CORE_ADDR
hppa64_frame_align(struct gdbarch * gdbarch,CORE_ADDR addr)1033 hppa64_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
1034 {
1035   /* Just always 16-byte align.  */
1036   return align_up (addr, 16);
1037 }
1038 
1039 
1040 /* Get the PC from %r31 if currently in a syscall.  Also mask out privilege
1041    bits.  */
1042 
1043 static CORE_ADDR
hppa_target_read_pc(ptid_t ptid)1044 hppa_target_read_pc (ptid_t ptid)
1045 {
1046   int flags = read_register_pid (HPPA_FLAGS_REGNUM, ptid);
1047 
1048   /* The following test does not belong here.  It is OS-specific, and belongs
1049      in native code.  */
1050   /* Test SS_INSYSCALL */
1051   if (flags & 2)
1052     return read_register_pid (31, ptid) & ~0x3;
1053 
1054   return read_register_pid (HPPA_PCOQ_HEAD_REGNUM, ptid) & ~0x3;
1055 }
1056 
1057 /* Write out the PC.  If currently in a syscall, then also write the new
1058    PC value into %r31.  */
1059 
1060 static void
hppa_target_write_pc(CORE_ADDR v,ptid_t ptid)1061 hppa_target_write_pc (CORE_ADDR v, ptid_t ptid)
1062 {
1063   int flags = read_register_pid (HPPA_FLAGS_REGNUM, ptid);
1064 
1065   /* The following test does not belong here.  It is OS-specific, and belongs
1066      in native code.  */
1067   /* If in a syscall, then set %r31.  Also make sure to get the
1068      privilege bits set correctly.  */
1069   /* Test SS_INSYSCALL */
1070   if (flags & 2)
1071     write_register_pid (31, v | 0x3, ptid);
1072 
1073   write_register_pid (HPPA_PCOQ_HEAD_REGNUM, v, ptid);
1074   write_register_pid (HPPA_PCOQ_TAIL_REGNUM, v + 4, ptid);
1075 }
1076 
1077 /* return the alignment of a type in bytes. Structures have the maximum
1078    alignment required by their fields. */
1079 
1080 static int
hppa_alignof(struct type * type)1081 hppa_alignof (struct type *type)
1082 {
1083   int max_align, align, i;
1084   CHECK_TYPEDEF (type);
1085   switch (TYPE_CODE (type))
1086     {
1087     case TYPE_CODE_PTR:
1088     case TYPE_CODE_INT:
1089     case TYPE_CODE_FLT:
1090       return TYPE_LENGTH (type);
1091     case TYPE_CODE_ARRAY:
1092       return hppa_alignof (TYPE_FIELD_TYPE (type, 0));
1093     case TYPE_CODE_STRUCT:
1094     case TYPE_CODE_UNION:
1095       max_align = 1;
1096       for (i = 0; i < TYPE_NFIELDS (type); i++)
1097 	{
1098 	  /* Bit fields have no real alignment. */
1099 	  /* if (!TYPE_FIELD_BITPOS (type, i)) */
1100 	  if (!TYPE_FIELD_BITSIZE (type, i))	/* elz: this should be bitsize */
1101 	    {
1102 	      align = hppa_alignof (TYPE_FIELD_TYPE (type, i));
1103 	      max_align = max (max_align, align);
1104 	    }
1105 	}
1106       return max_align;
1107     default:
1108       return 4;
1109     }
1110 }
1111 
1112 /* For the given instruction (INST), return any adjustment it makes
1113    to the stack pointer or zero for no adjustment.
1114 
1115    This only handles instructions commonly found in prologues.  */
1116 
1117 static int
prologue_inst_adjust_sp(unsigned long inst)1118 prologue_inst_adjust_sp (unsigned long inst)
1119 {
1120   /* This must persist across calls.  */
1121   static int save_high21;
1122 
1123   /* The most common way to perform a stack adjustment ldo X(sp),sp */
1124   if ((inst & 0xffffc000) == 0x37de0000)
1125     return hppa_extract_14 (inst);
1126 
1127   /* stwm X,D(sp) */
1128   if ((inst & 0xffe00000) == 0x6fc00000)
1129     return hppa_extract_14 (inst);
1130 
1131   /* std,ma X,D(sp) */
1132   if ((inst & 0xffe00008) == 0x73c00008)
1133     return (inst & 0x1 ? -1 << 13 : 0) | (((inst >> 4) & 0x3ff) << 3);
1134 
1135   /* addil high21,%r1; ldo low11,(%r1),%r30)
1136      save high bits in save_high21 for later use.  */
1137   if ((inst & 0xffe00000) == 0x28200000)
1138     {
1139       save_high21 = hppa_extract_21 (inst);
1140       return 0;
1141     }
1142 
1143   if ((inst & 0xffff0000) == 0x343e0000)
1144     return save_high21 + hppa_extract_14 (inst);
1145 
1146   /* fstws as used by the HP compilers.  */
1147   if ((inst & 0xffffffe0) == 0x2fd01220)
1148     return hppa_extract_5_load (inst);
1149 
1150   /* No adjustment.  */
1151   return 0;
1152 }
1153 
1154 /* Return nonzero if INST is a branch of some kind, else return zero.  */
1155 
1156 static int
is_branch(unsigned long inst)1157 is_branch (unsigned long inst)
1158 {
1159   switch (inst >> 26)
1160     {
1161     case 0x20:
1162     case 0x21:
1163     case 0x22:
1164     case 0x23:
1165     case 0x27:
1166     case 0x28:
1167     case 0x29:
1168     case 0x2a:
1169     case 0x2b:
1170     case 0x2f:
1171     case 0x30:
1172     case 0x31:
1173     case 0x32:
1174     case 0x33:
1175     case 0x38:
1176     case 0x39:
1177     case 0x3a:
1178     case 0x3b:
1179       return 1;
1180 
1181     default:
1182       return 0;
1183     }
1184 }
1185 
1186 /* Return the register number for a GR which is saved by INST or
1187    zero it INST does not save a GR.  */
1188 
1189 static int
inst_saves_gr(unsigned long inst)1190 inst_saves_gr (unsigned long inst)
1191 {
1192   /* Does it look like a stw?  */
1193   if ((inst >> 26) == 0x1a || (inst >> 26) == 0x1b
1194       || (inst >> 26) == 0x1f
1195       || ((inst >> 26) == 0x1f
1196 	  && ((inst >> 6) == 0xa)))
1197     return hppa_extract_5R_store (inst);
1198 
1199   /* Does it look like a std?  */
1200   if ((inst >> 26) == 0x1c
1201       || ((inst >> 26) == 0x03
1202 	  && ((inst >> 6) & 0xf) == 0xb))
1203     return hppa_extract_5R_store (inst);
1204 
1205   /* Does it look like a stwm?  GCC & HPC may use this in prologues. */
1206   if ((inst >> 26) == 0x1b)
1207     return hppa_extract_5R_store (inst);
1208 
1209   /* Does it look like sth or stb?  HPC versions 9.0 and later use these
1210      too.  */
1211   if ((inst >> 26) == 0x19 || (inst >> 26) == 0x18
1212       || ((inst >> 26) == 0x3
1213 	  && (((inst >> 6) & 0xf) == 0x8
1214 	      || (inst >> 6) & 0xf) == 0x9))
1215     return hppa_extract_5R_store (inst);
1216 
1217   return 0;
1218 }
1219 
1220 /* Return the register number for a FR which is saved by INST or
1221    zero it INST does not save a FR.
1222 
1223    Note we only care about full 64bit register stores (that's the only
1224    kind of stores the prologue will use).
1225 
1226    FIXME: What about argument stores with the HP compiler in ANSI mode? */
1227 
1228 static int
inst_saves_fr(unsigned long inst)1229 inst_saves_fr (unsigned long inst)
1230 {
1231   /* is this an FSTD ? */
1232   if ((inst & 0xfc00dfc0) == 0x2c001200)
1233     return hppa_extract_5r_store (inst);
1234   if ((inst & 0xfc000002) == 0x70000002)
1235     return hppa_extract_5R_store (inst);
1236   /* is this an FSTW ? */
1237   if ((inst & 0xfc00df80) == 0x24001200)
1238     return hppa_extract_5r_store (inst);
1239   if ((inst & 0xfc000002) == 0x7c000000)
1240     return hppa_extract_5R_store (inst);
1241   return 0;
1242 }
1243 
1244 /* Advance PC across any function entry prologue instructions
1245    to reach some "real" code.
1246 
1247    Use information in the unwind table to determine what exactly should
1248    be in the prologue.  */
1249 
1250 
1251 CORE_ADDR
skip_prologue_hard_way(CORE_ADDR pc)1252 skip_prologue_hard_way (CORE_ADDR pc)
1253 {
1254   char buf[4];
1255   CORE_ADDR orig_pc = pc;
1256   unsigned long inst, stack_remaining, save_gr, save_fr, save_rp, save_sp;
1257   unsigned long args_stored, status, i, restart_gr, restart_fr;
1258   struct unwind_table_entry *u;
1259 
1260   restart_gr = 0;
1261   restart_fr = 0;
1262 
1263 restart:
1264   u = find_unwind_entry (pc);
1265   if (!u)
1266     return pc;
1267 
1268   /* If we are not at the beginning of a function, then return now. */
1269   if ((pc & ~0x3) != u->region_start)
1270     return pc;
1271 
1272   /* This is how much of a frame adjustment we need to account for.  */
1273   stack_remaining = u->Total_frame_size << 3;
1274 
1275   /* Magic register saves we want to know about.  */
1276   save_rp = u->Save_RP;
1277   save_sp = u->Save_SP;
1278 
1279   /* An indication that args may be stored into the stack.  Unfortunately
1280      the HPUX compilers tend to set this in cases where no args were
1281      stored too!.  */
1282   args_stored = 1;
1283 
1284   /* Turn the Entry_GR field into a bitmask.  */
1285   save_gr = 0;
1286   for (i = 3; i < u->Entry_GR + 3; i++)
1287     {
1288       /* Frame pointer gets saved into a special location.  */
1289       if (u->Save_SP && i == HPPA_FP_REGNUM)
1290 	continue;
1291 
1292       save_gr |= (1 << i);
1293     }
1294   save_gr &= ~restart_gr;
1295 
1296   /* Turn the Entry_FR field into a bitmask too.  */
1297   save_fr = 0;
1298   for (i = 12; i < u->Entry_FR + 12; i++)
1299     save_fr |= (1 << i);
1300   save_fr &= ~restart_fr;
1301 
1302   /* Loop until we find everything of interest or hit a branch.
1303 
1304      For unoptimized GCC code and for any HP CC code this will never ever
1305      examine any user instructions.
1306 
1307      For optimzied GCC code we're faced with problems.  GCC will schedule
1308      its prologue and make prologue instructions available for delay slot
1309      filling.  The end result is user code gets mixed in with the prologue
1310      and a prologue instruction may be in the delay slot of the first branch
1311      or call.
1312 
1313      Some unexpected things are expected with debugging optimized code, so
1314      we allow this routine to walk past user instructions in optimized
1315      GCC code.  */
1316   while (save_gr || save_fr || save_rp || save_sp || stack_remaining > 0
1317 	 || args_stored)
1318     {
1319       unsigned int reg_num;
1320       unsigned long old_stack_remaining, old_save_gr, old_save_fr;
1321       unsigned long old_save_rp, old_save_sp, next_inst;
1322 
1323       /* Save copies of all the triggers so we can compare them later
1324          (only for HPC).  */
1325       old_save_gr = save_gr;
1326       old_save_fr = save_fr;
1327       old_save_rp = save_rp;
1328       old_save_sp = save_sp;
1329       old_stack_remaining = stack_remaining;
1330 
1331       status = deprecated_read_memory_nobpt (pc, buf, 4);
1332       inst = extract_unsigned_integer (buf, 4);
1333 
1334       /* Yow! */
1335       if (status != 0)
1336 	return pc;
1337 
1338       /* Note the interesting effects of this instruction.  */
1339       stack_remaining -= prologue_inst_adjust_sp (inst);
1340 
1341       /* There are limited ways to store the return pointer into the
1342 	 stack.  */
1343       if (inst == 0x6bc23fd9 || inst == 0x0fc212c1)
1344 	save_rp = 0;
1345 
1346       /* These are the only ways we save SP into the stack.  At this time
1347          the HP compilers never bother to save SP into the stack.  */
1348       if ((inst & 0xffffc000) == 0x6fc10000
1349 	  || (inst & 0xffffc00c) == 0x73c10008)
1350 	save_sp = 0;
1351 
1352       /* Are we loading some register with an offset from the argument
1353          pointer?  */
1354       if ((inst & 0xffe00000) == 0x37a00000
1355 	  || (inst & 0xffffffe0) == 0x081d0240)
1356 	{
1357 	  pc += 4;
1358 	  continue;
1359 	}
1360 
1361       /* Account for general and floating-point register saves.  */
1362       reg_num = inst_saves_gr (inst);
1363       save_gr &= ~(1 << reg_num);
1364 
1365       /* Ugh.  Also account for argument stores into the stack.
1366          Unfortunately args_stored only tells us that some arguments
1367          where stored into the stack.  Not how many or what kind!
1368 
1369          This is a kludge as on the HP compiler sets this bit and it
1370          never does prologue scheduling.  So once we see one, skip past
1371          all of them.   We have similar code for the fp arg stores below.
1372 
1373          FIXME.  Can still die if we have a mix of GR and FR argument
1374          stores!  */
1375       if (reg_num >= (TARGET_PTR_BIT == 64 ? 19 : 23) && reg_num <= 26)
1376 	{
1377 	  while (reg_num >= (TARGET_PTR_BIT == 64 ? 19 : 23) && reg_num <= 26)
1378 	    {
1379 	      pc += 4;
1380 	      status = deprecated_read_memory_nobpt (pc, buf, 4);
1381 	      inst = extract_unsigned_integer (buf, 4);
1382 	      if (status != 0)
1383 		return pc;
1384 	      reg_num = inst_saves_gr (inst);
1385 	    }
1386 	  args_stored = 0;
1387 	  continue;
1388 	}
1389 
1390       reg_num = inst_saves_fr (inst);
1391       save_fr &= ~(1 << reg_num);
1392 
1393       status = deprecated_read_memory_nobpt (pc + 4, buf, 4);
1394       next_inst = extract_unsigned_integer (buf, 4);
1395 
1396       /* Yow! */
1397       if (status != 0)
1398 	return pc;
1399 
1400       /* We've got to be read to handle the ldo before the fp register
1401          save.  */
1402       if ((inst & 0xfc000000) == 0x34000000
1403 	  && inst_saves_fr (next_inst) >= 4
1404 	  && inst_saves_fr (next_inst) <= (TARGET_PTR_BIT == 64 ? 11 : 7))
1405 	{
1406 	  /* So we drop into the code below in a reasonable state.  */
1407 	  reg_num = inst_saves_fr (next_inst);
1408 	  pc -= 4;
1409 	}
1410 
1411       /* Ugh.  Also account for argument stores into the stack.
1412          This is a kludge as on the HP compiler sets this bit and it
1413          never does prologue scheduling.  So once we see one, skip past
1414          all of them.  */
1415       if (reg_num >= 4 && reg_num <= (TARGET_PTR_BIT == 64 ? 11 : 7))
1416 	{
1417 	  while (reg_num >= 4 && reg_num <= (TARGET_PTR_BIT == 64 ? 11 : 7))
1418 	    {
1419 	      pc += 8;
1420 	      status = deprecated_read_memory_nobpt (pc, buf, 4);
1421 	      inst = extract_unsigned_integer (buf, 4);
1422 	      if (status != 0)
1423 		return pc;
1424 	      if ((inst & 0xfc000000) != 0x34000000)
1425 		break;
1426 	      status = deprecated_read_memory_nobpt (pc + 4, buf, 4);
1427 	      next_inst = extract_unsigned_integer (buf, 4);
1428 	      if (status != 0)
1429 		return pc;
1430 	      reg_num = inst_saves_fr (next_inst);
1431 	    }
1432 	  args_stored = 0;
1433 	  continue;
1434 	}
1435 
1436       /* Quit if we hit any kind of branch.  This can happen if a prologue
1437          instruction is in the delay slot of the first call/branch.  */
1438       if (is_branch (inst))
1439 	break;
1440 
1441       /* What a crock.  The HP compilers set args_stored even if no
1442          arguments were stored into the stack (boo hiss).  This could
1443          cause this code to then skip a bunch of user insns (up to the
1444          first branch).
1445 
1446          To combat this we try to identify when args_stored was bogusly
1447          set and clear it.   We only do this when args_stored is nonzero,
1448          all other resources are accounted for, and nothing changed on
1449          this pass.  */
1450       if (args_stored
1451        && !(save_gr || save_fr || save_rp || save_sp || stack_remaining > 0)
1452 	  && old_save_gr == save_gr && old_save_fr == save_fr
1453 	  && old_save_rp == save_rp && old_save_sp == save_sp
1454 	  && old_stack_remaining == stack_remaining)
1455 	break;
1456 
1457       /* Bump the PC.  */
1458       pc += 4;
1459     }
1460 
1461   /* We've got a tenative location for the end of the prologue.  However
1462      because of limitations in the unwind descriptor mechanism we may
1463      have went too far into user code looking for the save of a register
1464      that does not exist.  So, if there registers we expected to be saved
1465      but never were, mask them out and restart.
1466 
1467      This should only happen in optimized code, and should be very rare.  */
1468   if (save_gr || (save_fr && !(restart_fr || restart_gr)))
1469     {
1470       pc = orig_pc;
1471       restart_gr = save_gr;
1472       restart_fr = save_fr;
1473       goto restart;
1474     }
1475 
1476   return pc;
1477 }
1478 
1479 
1480 /* Return the address of the PC after the last prologue instruction if
1481    we can determine it from the debug symbols.  Else return zero.  */
1482 
1483 static CORE_ADDR
after_prologue(CORE_ADDR pc)1484 after_prologue (CORE_ADDR pc)
1485 {
1486   struct symtab_and_line sal;
1487   CORE_ADDR func_addr, func_end;
1488   struct symbol *f;
1489 
1490   /* If we can not find the symbol in the partial symbol table, then
1491      there is no hope we can determine the function's start address
1492      with this code.  */
1493   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
1494     return 0;
1495 
1496   /* Get the line associated with FUNC_ADDR.  */
1497   sal = find_pc_line (func_addr, 0);
1498 
1499   /* There are only two cases to consider.  First, the end of the source line
1500      is within the function bounds.  In that case we return the end of the
1501      source line.  Second is the end of the source line extends beyond the
1502      bounds of the current function.  We need to use the slow code to
1503      examine instructions in that case.
1504 
1505      Anything else is simply a bug elsewhere.  Fixing it here is absolutely
1506      the wrong thing to do.  In fact, it should be entirely possible for this
1507      function to always return zero since the slow instruction scanning code
1508      is supposed to *always* work.  If it does not, then it is a bug.  */
1509   if (sal.end < func_end)
1510     return sal.end;
1511   else
1512     return 0;
1513 }
1514 
1515 /* To skip prologues, I use this predicate.  Returns either PC itself
1516    if the code at PC does not look like a function prologue; otherwise
1517    returns an address that (if we're lucky) follows the prologue.  If
1518    LENIENT, then we must skip everything which is involved in setting
1519    up the frame (it's OK to skip more, just so long as we don't skip
1520    anything which might clobber the registers which are being saved.
1521    Currently we must not skip more on the alpha, but we might the lenient
1522    stuff some day.  */
1523 
1524 static CORE_ADDR
hppa_skip_prologue(CORE_ADDR pc)1525 hppa_skip_prologue (CORE_ADDR pc)
1526 {
1527   unsigned long inst;
1528   int offset;
1529   CORE_ADDR post_prologue_pc;
1530   char buf[4];
1531 
1532   /* See if we can determine the end of the prologue via the symbol table.
1533      If so, then return either PC, or the PC after the prologue, whichever
1534      is greater.  */
1535 
1536   post_prologue_pc = after_prologue (pc);
1537 
1538   /* If after_prologue returned a useful address, then use it.  Else
1539      fall back on the instruction skipping code.
1540 
1541      Some folks have claimed this causes problems because the breakpoint
1542      may be the first instruction of the prologue.  If that happens, then
1543      the instruction skipping code has a bug that needs to be fixed.  */
1544   if (post_prologue_pc != 0)
1545     return max (pc, post_prologue_pc);
1546   else
1547     return (skip_prologue_hard_way (pc));
1548 }
1549 
1550 struct hppa_frame_cache
1551 {
1552   CORE_ADDR base;
1553   struct trad_frame_saved_reg *saved_regs;
1554 };
1555 
1556 static struct hppa_frame_cache *
hppa_frame_cache(struct frame_info * next_frame,void ** this_cache)1557 hppa_frame_cache (struct frame_info *next_frame, void **this_cache)
1558 {
1559   struct hppa_frame_cache *cache;
1560   long saved_gr_mask;
1561   long saved_fr_mask;
1562   CORE_ADDR this_sp;
1563   long frame_size;
1564   struct unwind_table_entry *u;
1565   CORE_ADDR prologue_end;
1566   int i;
1567 
1568   if (hppa_debug)
1569     fprintf_unfiltered (gdb_stdlog, "{ hppa_frame_cache (frame=%d) -> ",
1570       frame_relative_level(next_frame));
1571 
1572   if ((*this_cache) != NULL)
1573     {
1574       if (hppa_debug)
1575         fprintf_unfiltered (gdb_stdlog, "base=0x%s (cached) }",
1576           paddr_nz (((struct hppa_frame_cache *)*this_cache)->base));
1577       return (*this_cache);
1578     }
1579   cache = FRAME_OBSTACK_ZALLOC (struct hppa_frame_cache);
1580   (*this_cache) = cache;
1581   cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
1582 
1583   /* Yow! */
1584   u = find_unwind_entry (frame_pc_unwind (next_frame));
1585   if (!u)
1586     {
1587       if (hppa_debug)
1588         fprintf_unfiltered (gdb_stdlog, "base=NULL (no unwind entry) }");
1589       return (*this_cache);
1590     }
1591 
1592   /* Turn the Entry_GR field into a bitmask.  */
1593   saved_gr_mask = 0;
1594   for (i = 3; i < u->Entry_GR + 3; i++)
1595     {
1596       /* Frame pointer gets saved into a special location.  */
1597       if (u->Save_SP && i == HPPA_FP_REGNUM)
1598 	continue;
1599 
1600       saved_gr_mask |= (1 << i);
1601     }
1602 
1603   /* Turn the Entry_FR field into a bitmask too.  */
1604   saved_fr_mask = 0;
1605   for (i = 12; i < u->Entry_FR + 12; i++)
1606     saved_fr_mask |= (1 << i);
1607 
1608   /* Loop until we find everything of interest or hit a branch.
1609 
1610      For unoptimized GCC code and for any HP CC code this will never ever
1611      examine any user instructions.
1612 
1613      For optimized GCC code we're faced with problems.  GCC will schedule
1614      its prologue and make prologue instructions available for delay slot
1615      filling.  The end result is user code gets mixed in with the prologue
1616      and a prologue instruction may be in the delay slot of the first branch
1617      or call.
1618 
1619      Some unexpected things are expected with debugging optimized code, so
1620      we allow this routine to walk past user instructions in optimized
1621      GCC code.  */
1622   {
1623     int final_iteration = 0;
1624     CORE_ADDR pc, end_pc;
1625     int looking_for_sp = u->Save_SP;
1626     int looking_for_rp = u->Save_RP;
1627     int fp_loc = -1;
1628 
1629     /* We have to use hppa_skip_prologue instead of just
1630        skip_prologue_using_sal, in case we stepped into a function without
1631        symbol information.  hppa_skip_prologue also bounds the returned
1632        pc by the passed in pc, so it will not return a pc in the next
1633        function.  */
1634 
1635     /* We used to use frame_func_unwind () to locate the beginning of the
1636        function to pass to skip_prologue ().  However, when objects are
1637        compiled without debug symbols, frame_func_unwind can return the wrong
1638        function (or 0).  We can do better than that by using unwind records.  */
1639 
1640     prologue_end = hppa_skip_prologue (u->region_start);
1641     end_pc = frame_pc_unwind (next_frame);
1642 
1643     if (prologue_end != 0 && end_pc > prologue_end)
1644       end_pc = prologue_end;
1645 
1646     frame_size = 0;
1647 
1648     for (pc = u->region_start;
1649 	 ((saved_gr_mask || saved_fr_mask
1650 	   || looking_for_sp || looking_for_rp
1651 	   || frame_size < (u->Total_frame_size << 3))
1652 	  && pc < end_pc);
1653 	 pc += 4)
1654       {
1655 	int reg;
1656 	char buf4[4];
1657 	long status = deprecated_read_memory_nobpt (pc, buf4, sizeof buf4);
1658 	long inst = extract_unsigned_integer (buf4, sizeof buf4);
1659 
1660 	/* Note the interesting effects of this instruction.  */
1661 	frame_size += prologue_inst_adjust_sp (inst);
1662 
1663 	/* There are limited ways to store the return pointer into the
1664 	   stack.  */
1665 	if (inst == 0x6bc23fd9) /* stw rp,-0x14(sr0,sp) */
1666 	  {
1667 	    looking_for_rp = 0;
1668 	    cache->saved_regs[HPPA_RP_REGNUM].addr = -20;
1669 	  }
1670 	else if (inst == 0x6bc23fd1) /* stw rp,-0x18(sr0,sp) */
1671 	  {
1672 	    looking_for_rp = 0;
1673 	    cache->saved_regs[HPPA_RP_REGNUM].addr = -24;
1674 	  }
1675 	else if (inst == 0x0fc212c1) /* std rp,-0x10(sr0,sp) */
1676 	  {
1677 	    looking_for_rp = 0;
1678 	    cache->saved_regs[HPPA_RP_REGNUM].addr = -16;
1679 	  }
1680 
1681 	/* Check to see if we saved SP into the stack.  This also
1682 	   happens to indicate the location of the saved frame
1683 	   pointer.  */
1684 	if ((inst & 0xffffc000) == 0x6fc10000  /* stw,ma r1,N(sr0,sp) */
1685 	    || (inst & 0xffffc00c) == 0x73c10008) /* std,ma r1,N(sr0,sp) */
1686 	  {
1687 	    looking_for_sp = 0;
1688 	    cache->saved_regs[HPPA_FP_REGNUM].addr = 0;
1689 	  }
1690 
1691 	/* Account for general and floating-point register saves.  */
1692 	reg = inst_saves_gr (inst);
1693 	if (reg >= 3 && reg <= 18
1694 	    && (!u->Save_SP || reg != HPPA_FP_REGNUM))
1695 	  {
1696 	    saved_gr_mask &= ~(1 << reg);
1697 	    if ((inst >> 26) == 0x1b && hppa_extract_14 (inst) >= 0)
1698 	      /* stwm with a positive displacement is a _post_
1699 		 _modify_.  */
1700 	      cache->saved_regs[reg].addr = 0;
1701 	    else if ((inst & 0xfc00000c) == 0x70000008)
1702 	      /* A std has explicit post_modify forms.  */
1703 	      cache->saved_regs[reg].addr = 0;
1704 	    else
1705 	      {
1706 		CORE_ADDR offset;
1707 
1708 		if ((inst >> 26) == 0x1c)
1709 		  offset = (inst & 0x1 ? -1 << 13 : 0) | (((inst >> 4) & 0x3ff) << 3);
1710 		else if ((inst >> 26) == 0x03)
1711 		  offset = hppa_low_hppa_sign_extend (inst & 0x1f, 5);
1712 		else
1713 		  offset = hppa_extract_14 (inst);
1714 
1715 		/* Handle code with and without frame pointers.  */
1716 		if (u->Save_SP)
1717 		  cache->saved_regs[reg].addr = offset;
1718 		else
1719 		  cache->saved_regs[reg].addr = (u->Total_frame_size << 3) + offset;
1720 	      }
1721 	  }
1722 
1723 	/* GCC handles callee saved FP regs a little differently.
1724 
1725 	   It emits an instruction to put the value of the start of
1726 	   the FP store area into %r1.  It then uses fstds,ma with a
1727 	   basereg of %r1 for the stores.
1728 
1729 	   HP CC emits them at the current stack pointer modifying the
1730 	   stack pointer as it stores each register.  */
1731 
1732 	/* ldo X(%r3),%r1 or ldo X(%r30),%r1.  */
1733 	if ((inst & 0xffffc000) == 0x34610000
1734 	    || (inst & 0xffffc000) == 0x37c10000)
1735 	  fp_loc = hppa_extract_14 (inst);
1736 
1737 	reg = inst_saves_fr (inst);
1738 	if (reg >= 12 && reg <= 21)
1739 	  {
1740 	    /* Note +4 braindamage below is necessary because the FP
1741 	       status registers are internally 8 registers rather than
1742 	       the expected 4 registers.  */
1743 	    saved_fr_mask &= ~(1 << reg);
1744 	    if (fp_loc == -1)
1745 	      {
1746 		/* 1st HP CC FP register store.  After this
1747 		   instruction we've set enough state that the GCC and
1748 		   HPCC code are both handled in the same manner.  */
1749 		cache->saved_regs[reg + HPPA_FP4_REGNUM + 4].addr = 0;
1750 		fp_loc = 8;
1751 	      }
1752 	    else
1753 	      {
1754 		cache->saved_regs[reg + HPPA_FP0_REGNUM + 4].addr = fp_loc;
1755 		fp_loc += 8;
1756 	      }
1757 	  }
1758 
1759 	/* Quit if we hit any kind of branch the previous iteration. */
1760 	if (final_iteration)
1761 	  break;
1762 	/* We want to look precisely one instruction beyond the branch
1763 	   if we have not found everything yet.  */
1764 	if (is_branch (inst))
1765 	  final_iteration = 1;
1766       }
1767   }
1768 
1769   {
1770     /* The frame base always represents the value of %sp at entry to
1771        the current function (and is thus equivalent to the "saved"
1772        stack pointer.  */
1773     CORE_ADDR this_sp = frame_unwind_register_unsigned (next_frame, HPPA_SP_REGNUM);
1774     CORE_ADDR fp;
1775 
1776     if (hppa_debug)
1777       fprintf_unfiltered (gdb_stdlog, " (this_sp=0x%s, pc=0x%s, "
1778 		          "prologue_end=0x%s) ",
1779 		          paddr_nz (this_sp),
1780 			  paddr_nz (frame_pc_unwind (next_frame)),
1781 			  paddr_nz (prologue_end));
1782 
1783      /* Check to see if a frame pointer is available, and use it for
1784         frame unwinding if it is.
1785 
1786         There are some situations where we need to rely on the frame
1787         pointer to do stack unwinding.  For example, if a function calls
1788         alloca (), the stack pointer can get adjusted inside the body of
1789         the function.  In this case, the ABI requires that the compiler
1790         maintain a frame pointer for the function.
1791 
1792         The unwind record has a flag (alloca_frame) that indicates that
1793         a function has a variable frame; unfortunately, gcc/binutils
1794         does not set this flag.  Instead, whenever a frame pointer is used
1795         and saved on the stack, the Save_SP flag is set.  We use this to
1796         decide whether to use the frame pointer for unwinding.
1797 
1798 	fp may be zero if it is not available in an inner frame because
1799 	it has been modified by not yet saved.
1800 
1801         TODO: For the HP compiler, maybe we should use the alloca_frame flag
1802 	instead of Save_SP.  */
1803 
1804      fp = frame_unwind_register_unsigned (next_frame, HPPA_FP_REGNUM);
1805 
1806      if (frame_pc_unwind (next_frame) >= prologue_end
1807          && u->Save_SP && fp != 0)
1808       {
1809  	cache->base = fp;
1810 
1811  	if (hppa_debug)
1812 	  fprintf_unfiltered (gdb_stdlog, " (base=0x%s) [frame pointer] }",
1813  	    paddr_nz (cache->base));
1814       }
1815      else if (u->Save_SP
1816 	      && trad_frame_addr_p (cache->saved_regs, HPPA_SP_REGNUM))
1817       {
1818             /* Both we're expecting the SP to be saved and the SP has been
1819 	       saved.  The entry SP value is saved at this frame's SP
1820 	       address.  */
1821             cache->base = read_memory_integer (this_sp, TARGET_PTR_BIT / 8);
1822 
1823 	    if (hppa_debug)
1824 	      fprintf_unfiltered (gdb_stdlog, " (base=0x%s) [saved] }",
1825 			          paddr_nz (cache->base));
1826       }
1827     else
1828       {
1829         /* The prologue has been slowly allocating stack space.  Adjust
1830 	   the SP back.  */
1831         cache->base = this_sp - frame_size;
1832 	if (hppa_debug)
1833 	  fprintf_unfiltered (gdb_stdlog, " (base=0x%s) [unwind adjust] } ",
1834 			      paddr_nz (cache->base));
1835 
1836       }
1837     trad_frame_set_value (cache->saved_regs, HPPA_SP_REGNUM, cache->base);
1838   }
1839 
1840   /* The PC is found in the "return register", "Millicode" uses "r31"
1841      as the return register while normal code uses "rp".  */
1842   if (u->Millicode)
1843     {
1844       if (trad_frame_addr_p (cache->saved_regs, 31))
1845         cache->saved_regs[HPPA_PCOQ_HEAD_REGNUM] = cache->saved_regs[31];
1846       else
1847 	{
1848 	  ULONGEST r31 = frame_unwind_register_unsigned (next_frame, 31);
1849 	  trad_frame_set_value (cache->saved_regs, HPPA_PCOQ_HEAD_REGNUM, r31);
1850         }
1851     }
1852   else
1853     {
1854       if (trad_frame_addr_p (cache->saved_regs, HPPA_RP_REGNUM))
1855         cache->saved_regs[HPPA_PCOQ_HEAD_REGNUM] = cache->saved_regs[HPPA_RP_REGNUM];
1856       else
1857 	{
1858 	  ULONGEST rp = frame_unwind_register_unsigned (next_frame, HPPA_RP_REGNUM);
1859 	  trad_frame_set_value (cache->saved_regs, HPPA_PCOQ_HEAD_REGNUM, rp);
1860 	}
1861     }
1862 
1863   /* If the frame pointer was not saved in this frame, but we should be saving
1864      it, set it to an invalid value so that another frame will not pick up the
1865      wrong frame pointer.  This can happen if we start unwinding after the
1866      frame pointer has been modified, but before we've saved it to the
1867      stack.  */
1868   if (u->Save_SP && !trad_frame_addr_p (cache->saved_regs, HPPA_FP_REGNUM))
1869     trad_frame_set_value (cache->saved_regs, HPPA_FP_REGNUM, 0);
1870 
1871   {
1872     /* Convert all the offsets into addresses.  */
1873     int reg;
1874     for (reg = 0; reg < NUM_REGS; reg++)
1875       {
1876 	if (trad_frame_addr_p (cache->saved_regs, reg))
1877 	  cache->saved_regs[reg].addr += cache->base;
1878       }
1879   }
1880 
1881   if (hppa_debug)
1882     fprintf_unfiltered (gdb_stdlog, "base=0x%s }",
1883       paddr_nz (((struct hppa_frame_cache *)*this_cache)->base));
1884   return (*this_cache);
1885 }
1886 
1887 static void
hppa_frame_this_id(struct frame_info * next_frame,void ** this_cache,struct frame_id * this_id)1888 hppa_frame_this_id (struct frame_info *next_frame, void **this_cache,
1889 			   struct frame_id *this_id)
1890 {
1891   struct hppa_frame_cache *info;
1892   CORE_ADDR pc = frame_pc_unwind (next_frame);
1893   struct unwind_table_entry *u;
1894 
1895   info = hppa_frame_cache (next_frame, this_cache);
1896   u = find_unwind_entry (pc);
1897 
1898   (*this_id) = frame_id_build (info->base, u->region_start);
1899 }
1900 
1901 static void
hppa_frame_prev_register(struct frame_info * next_frame,void ** this_cache,int regnum,int * optimizedp,enum lval_type * lvalp,CORE_ADDR * addrp,int * realnump,void * valuep)1902 hppa_frame_prev_register (struct frame_info *next_frame,
1903 			  void **this_cache,
1904 			  int regnum, int *optimizedp,
1905 			  enum lval_type *lvalp, CORE_ADDR *addrp,
1906 			  int *realnump, void *valuep)
1907 {
1908   struct hppa_frame_cache *info = hppa_frame_cache (next_frame, this_cache);
1909   hppa_frame_prev_register_helper (next_frame, info->saved_regs, regnum,
1910 		                   optimizedp, lvalp, addrp, realnump, valuep);
1911 }
1912 
1913 static const struct frame_unwind hppa_frame_unwind =
1914 {
1915   NORMAL_FRAME,
1916   hppa_frame_this_id,
1917   hppa_frame_prev_register
1918 };
1919 
1920 static const struct frame_unwind *
hppa_frame_unwind_sniffer(struct frame_info * next_frame)1921 hppa_frame_unwind_sniffer (struct frame_info *next_frame)
1922 {
1923   CORE_ADDR pc = frame_pc_unwind (next_frame);
1924 
1925   if (find_unwind_entry (pc))
1926     return &hppa_frame_unwind;
1927 
1928   return NULL;
1929 }
1930 
1931 /* This is a generic fallback frame unwinder that kicks in if we fail all
1932    the other ones.  Normally we would expect the stub and regular unwinder
1933    to work, but in some cases we might hit a function that just doesn't
1934    have any unwind information available.  In this case we try to do
1935    unwinding solely based on code reading.  This is obviously going to be
1936    slow, so only use this as a last resort.  Currently this will only
1937    identify the stack and pc for the frame.  */
1938 
1939 static struct hppa_frame_cache *
hppa_fallback_frame_cache(struct frame_info * next_frame,void ** this_cache)1940 hppa_fallback_frame_cache (struct frame_info *next_frame, void **this_cache)
1941 {
1942   struct hppa_frame_cache *cache;
1943   unsigned int frame_size;
1944   int found_rp;
1945   CORE_ADDR pc, start_pc, end_pc, cur_pc;
1946 
1947   if (hppa_debug)
1948     fprintf_unfiltered (gdb_stdlog, "{ hppa_fallback_frame_cache (frame=%d)-> ",
1949       frame_relative_level(next_frame));
1950 
1951   cache = FRAME_OBSTACK_ZALLOC (struct hppa_frame_cache);
1952   (*this_cache) = cache;
1953   cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
1954 
1955   pc = frame_func_unwind (next_frame);
1956   cur_pc = frame_pc_unwind (next_frame);
1957   frame_size = 0;
1958   found_rp = 0;
1959 
1960   find_pc_partial_function (pc, NULL, &start_pc, &end_pc);
1961 
1962   if (start_pc == 0 || end_pc == 0)
1963     {
1964       error ("Cannot find bounds of current function (@0x%s), unwinding will "
1965 	     "fail.", paddr_nz (pc));
1966       return cache;
1967     }
1968 
1969   if (end_pc > cur_pc)
1970     end_pc = cur_pc;
1971 
1972   for (pc = start_pc; pc < end_pc; pc += 4)
1973     {
1974       unsigned int insn;
1975 
1976       insn = read_memory_unsigned_integer (pc, 4);
1977 
1978       frame_size += prologue_inst_adjust_sp (insn);
1979 
1980       /* There are limited ways to store the return pointer into the
1981 	 stack.  */
1982       if (insn == 0x6bc23fd9) /* stw rp,-0x14(sr0,sp) */
1983 	 {
1984 	   cache->saved_regs[HPPA_RP_REGNUM].addr = -20;
1985 	   found_rp = 1;
1986 	 }
1987       else if (insn == 0x0fc212c1) /* std rp,-0x10(sr0,sp) */
1988 	 {
1989 	   cache->saved_regs[HPPA_RP_REGNUM].addr = -16;
1990 	   found_rp = 1;
1991 	 }
1992     }
1993 
1994   if (hppa_debug)
1995     fprintf_unfiltered (gdb_stdlog, " frame_size = %d, found_rp = %d }\n",
1996       frame_size, found_rp);
1997 
1998   cache->base = frame_unwind_register_unsigned (next_frame, HPPA_SP_REGNUM) - frame_size;
1999   trad_frame_set_value (cache->saved_regs, HPPA_SP_REGNUM, cache->base);
2000 
2001   if (trad_frame_addr_p (cache->saved_regs, HPPA_RP_REGNUM))
2002     {
2003       cache->saved_regs[HPPA_RP_REGNUM].addr += cache->base;
2004       cache->saved_regs[HPPA_PCOQ_HEAD_REGNUM] = cache->saved_regs[HPPA_RP_REGNUM];
2005     }
2006   else
2007     {
2008       ULONGEST rp = frame_unwind_register_unsigned (next_frame, HPPA_RP_REGNUM);
2009       trad_frame_set_value (cache->saved_regs, HPPA_PCOQ_HEAD_REGNUM, rp);
2010     }
2011 
2012   return cache;
2013 }
2014 
2015 static void
hppa_fallback_frame_this_id(struct frame_info * next_frame,void ** this_cache,struct frame_id * this_id)2016 hppa_fallback_frame_this_id (struct frame_info *next_frame, void **this_cache,
2017 			     struct frame_id *this_id)
2018 {
2019   struct hppa_frame_cache *info =
2020     hppa_fallback_frame_cache (next_frame, this_cache);
2021   (*this_id) = frame_id_build (info->base, frame_func_unwind (next_frame));
2022 }
2023 
2024 static void
hppa_fallback_frame_prev_register(struct frame_info * next_frame,void ** this_cache,int regnum,int * optimizedp,enum lval_type * lvalp,CORE_ADDR * addrp,int * realnump,void * valuep)2025 hppa_fallback_frame_prev_register (struct frame_info *next_frame,
2026 			  void **this_cache,
2027 			  int regnum, int *optimizedp,
2028 			  enum lval_type *lvalp, CORE_ADDR *addrp,
2029 			  int *realnump, void *valuep)
2030 {
2031   struct hppa_frame_cache *info =
2032     hppa_fallback_frame_cache (next_frame, this_cache);
2033   hppa_frame_prev_register_helper (next_frame, info->saved_regs, regnum,
2034 		                   optimizedp, lvalp, addrp, realnump, valuep);
2035 }
2036 
2037 static const struct frame_unwind hppa_fallback_frame_unwind =
2038 {
2039   NORMAL_FRAME,
2040   hppa_fallback_frame_this_id,
2041   hppa_fallback_frame_prev_register
2042 };
2043 
2044 static const struct frame_unwind *
hppa_fallback_unwind_sniffer(struct frame_info * next_frame)2045 hppa_fallback_unwind_sniffer (struct frame_info *next_frame)
2046 {
2047   return &hppa_fallback_frame_unwind;
2048 }
2049 
2050 /* Stub frames, used for all kinds of call stubs.  */
2051 struct hppa_stub_unwind_cache
2052 {
2053   CORE_ADDR base;
2054   struct trad_frame_saved_reg *saved_regs;
2055 };
2056 
2057 static struct hppa_stub_unwind_cache *
hppa_stub_frame_unwind_cache(struct frame_info * next_frame,void ** this_cache)2058 hppa_stub_frame_unwind_cache (struct frame_info *next_frame,
2059 			      void **this_cache)
2060 {
2061   struct gdbarch *gdbarch = get_frame_arch (next_frame);
2062   struct hppa_stub_unwind_cache *info;
2063   struct unwind_table_entry *u;
2064 
2065   if (*this_cache)
2066     return *this_cache;
2067 
2068   info = FRAME_OBSTACK_ZALLOC (struct hppa_stub_unwind_cache);
2069   *this_cache = info;
2070   info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
2071 
2072   info->base = frame_unwind_register_unsigned (next_frame, HPPA_SP_REGNUM);
2073 
2074   if (gdbarch_osabi (gdbarch) == GDB_OSABI_HPUX_SOM)
2075     {
2076       /* HPUX uses export stubs in function calls; the export stub clobbers
2077          the return value of the caller, and, later restores it from the
2078 	 stack.  */
2079       u = find_unwind_entry (frame_pc_unwind (next_frame));
2080 
2081       if (u && u->stub_unwind.stub_type == EXPORT)
2082 	{
2083           info->saved_regs[HPPA_PCOQ_HEAD_REGNUM].addr = info->base - 24;
2084 
2085 	  return info;
2086 	}
2087     }
2088 
2089   /* By default we assume that stubs do not change the rp.  */
2090   info->saved_regs[HPPA_PCOQ_HEAD_REGNUM].realreg = HPPA_RP_REGNUM;
2091 
2092   return info;
2093 }
2094 
2095 static void
hppa_stub_frame_this_id(struct frame_info * next_frame,void ** this_prologue_cache,struct frame_id * this_id)2096 hppa_stub_frame_this_id (struct frame_info *next_frame,
2097 			 void **this_prologue_cache,
2098 			 struct frame_id *this_id)
2099 {
2100   struct hppa_stub_unwind_cache *info
2101     = hppa_stub_frame_unwind_cache (next_frame, this_prologue_cache);
2102   *this_id = frame_id_build (info->base, frame_pc_unwind (next_frame));
2103 }
2104 
2105 static void
hppa_stub_frame_prev_register(struct frame_info * next_frame,void ** this_prologue_cache,int regnum,int * optimizedp,enum lval_type * lvalp,CORE_ADDR * addrp,int * realnump,void * valuep)2106 hppa_stub_frame_prev_register (struct frame_info *next_frame,
2107 			       void **this_prologue_cache,
2108 			       int regnum, int *optimizedp,
2109 			       enum lval_type *lvalp, CORE_ADDR *addrp,
2110 			       int *realnump, void *valuep)
2111 {
2112   struct hppa_stub_unwind_cache *info
2113     = hppa_stub_frame_unwind_cache (next_frame, this_prologue_cache);
2114   hppa_frame_prev_register_helper (next_frame, info->saved_regs, regnum,
2115 		                   optimizedp, lvalp, addrp, realnump, valuep);
2116 }
2117 
2118 static const struct frame_unwind hppa_stub_frame_unwind = {
2119   NORMAL_FRAME,
2120   hppa_stub_frame_this_id,
2121   hppa_stub_frame_prev_register
2122 };
2123 
2124 static const struct frame_unwind *
hppa_stub_unwind_sniffer(struct frame_info * next_frame)2125 hppa_stub_unwind_sniffer (struct frame_info *next_frame)
2126 {
2127   CORE_ADDR pc = frame_pc_unwind (next_frame);
2128 
2129   if (pc == 0
2130       || IN_SOLIB_CALL_TRAMPOLINE (pc, NULL)
2131       || IN_SOLIB_RETURN_TRAMPOLINE (pc, NULL))
2132     return &hppa_stub_frame_unwind;
2133   return NULL;
2134 }
2135 
2136 static struct frame_id
hppa_unwind_dummy_id(struct gdbarch * gdbarch,struct frame_info * next_frame)2137 hppa_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
2138 {
2139   return frame_id_build (frame_unwind_register_unsigned (next_frame,
2140 							 HPPA_SP_REGNUM),
2141 			 frame_pc_unwind (next_frame));
2142 }
2143 
2144 static CORE_ADDR
hppa_unwind_pc(struct gdbarch * gdbarch,struct frame_info * next_frame)2145 hppa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
2146 {
2147   return frame_unwind_register_signed (next_frame, HPPA_PCOQ_HEAD_REGNUM) & ~3;
2148 }
2149 
2150 /* Instead of this nasty cast, add a method pvoid() that prints out a
2151    host VOID data type (remember %p isn't portable).  */
2152 
2153 static CORE_ADDR
hppa_pointer_to_address_hack(void * ptr)2154 hppa_pointer_to_address_hack (void *ptr)
2155 {
2156   gdb_assert (sizeof (ptr) == TYPE_LENGTH (builtin_type_void_data_ptr));
2157   return POINTER_TO_ADDRESS (builtin_type_void_data_ptr, &ptr);
2158 }
2159 
2160 static void
unwind_command(char * exp,int from_tty)2161 unwind_command (char *exp, int from_tty)
2162 {
2163   CORE_ADDR address;
2164   struct unwind_table_entry *u;
2165 
2166   /* If we have an expression, evaluate it and use it as the address.  */
2167 
2168   if (exp != 0 && *exp != 0)
2169     address = parse_and_eval_address (exp);
2170   else
2171     return;
2172 
2173   u = find_unwind_entry (address);
2174 
2175   if (!u)
2176     {
2177       printf_unfiltered ("Can't find unwind table entry for %s\n", exp);
2178       return;
2179     }
2180 
2181   printf_unfiltered ("unwind_table_entry (0x%s):\n",
2182 		     paddr_nz (hppa_pointer_to_address_hack (u)));
2183 
2184   printf_unfiltered ("\tregion_start = ");
2185   print_address (u->region_start, gdb_stdout);
2186   gdb_flush (gdb_stdout);
2187 
2188   printf_unfiltered ("\n\tregion_end = ");
2189   print_address (u->region_end, gdb_stdout);
2190   gdb_flush (gdb_stdout);
2191 
2192 #define pif(FLD) if (u->FLD) printf_unfiltered (" "#FLD);
2193 
2194   printf_unfiltered ("\n\tflags =");
2195   pif (Cannot_unwind);
2196   pif (Millicode);
2197   pif (Millicode_save_sr0);
2198   pif (Entry_SR);
2199   pif (Args_stored);
2200   pif (Variable_Frame);
2201   pif (Separate_Package_Body);
2202   pif (Frame_Extension_Millicode);
2203   pif (Stack_Overflow_Check);
2204   pif (Two_Instruction_SP_Increment);
2205   pif (Ada_Region);
2206   pif (Save_SP);
2207   pif (Save_RP);
2208   pif (Save_MRP_in_frame);
2209   pif (extn_ptr_defined);
2210   pif (Cleanup_defined);
2211   pif (MPE_XL_interrupt_marker);
2212   pif (HP_UX_interrupt_marker);
2213   pif (Large_frame);
2214 
2215   putchar_unfiltered ('\n');
2216 
2217 #define pin(FLD) printf_unfiltered ("\t"#FLD" = 0x%x\n", u->FLD);
2218 
2219   pin (Region_description);
2220   pin (Entry_FR);
2221   pin (Entry_GR);
2222   pin (Total_frame_size);
2223 }
2224 
2225 void
hppa_skip_permanent_breakpoint(void)2226 hppa_skip_permanent_breakpoint (void)
2227 {
2228   /* To step over a breakpoint instruction on the PA takes some
2229      fiddling with the instruction address queue.
2230 
2231      When we stop at a breakpoint, the IA queue front (the instruction
2232      we're executing now) points at the breakpoint instruction, and
2233      the IA queue back (the next instruction to execute) points to
2234      whatever instruction we would execute after the breakpoint, if it
2235      were an ordinary instruction.  This is the case even if the
2236      breakpoint is in the delay slot of a branch instruction.
2237 
2238      Clearly, to step past the breakpoint, we need to set the queue
2239      front to the back.  But what do we put in the back?  What
2240      instruction comes after that one?  Because of the branch delay
2241      slot, the next insn is always at the back + 4.  */
2242   write_register (HPPA_PCOQ_HEAD_REGNUM, read_register (HPPA_PCOQ_TAIL_REGNUM));
2243   write_register (HPPA_PCSQ_HEAD_REGNUM, read_register (HPPA_PCSQ_TAIL_REGNUM));
2244 
2245   write_register (HPPA_PCOQ_TAIL_REGNUM, read_register (HPPA_PCOQ_TAIL_REGNUM) + 4);
2246   /* We can leave the tail's space the same, since there's no jump.  */
2247 }
2248 
2249 int
hppa_pc_requires_run_before_use(CORE_ADDR pc)2250 hppa_pc_requires_run_before_use (CORE_ADDR pc)
2251 {
2252   /* Sometimes we may pluck out a minimal symbol that has a negative address.
2253 
2254      An example of this occurs when an a.out is linked against a foo.sl.
2255      The foo.sl defines a global bar(), and the a.out declares a signature
2256      for bar().  However, the a.out doesn't directly call bar(), but passes
2257      its address in another call.
2258 
2259      If you have this scenario and attempt to "break bar" before running,
2260      gdb will find a minimal symbol for bar() in the a.out.  But that
2261      symbol's address will be negative.  What this appears to denote is
2262      an index backwards from the base of the procedure linkage table (PLT)
2263      into the data linkage table (DLT), the end of which is contiguous
2264      with the start of the PLT.  This is clearly not a valid address for
2265      us to set a breakpoint on.
2266 
2267      Note that one must be careful in how one checks for a negative address.
2268      0xc0000000 is a legitimate address of something in a shared text
2269      segment, for example.  Since I don't know what the possible range
2270      is of these "really, truly negative" addresses that come from the
2271      minimal symbols, I'm resorting to the gross hack of checking the
2272      top byte of the address for all 1's.  Sigh.  */
2273 
2274   return (!target_has_stack && (pc & 0xFF000000));
2275 }
2276 
2277 int
hppa_instruction_nullified(void)2278 hppa_instruction_nullified (void)
2279 {
2280   /* brobecker 2002/11/07: Couldn't we use a ULONGEST here? It would
2281      avoid the type cast.  I'm leaving it as is for now as I'm doing
2282      semi-mechanical multiarching-related changes.  */
2283   const int ipsw = (int) read_register (HPPA_IPSW_REGNUM);
2284   const int flags = (int) read_register (HPPA_FLAGS_REGNUM);
2285 
2286   return ((ipsw & 0x00200000) && !(flags & 0x2));
2287 }
2288 
2289 /* Return the GDB type object for the "standard" data type of data
2290    in register N.  */
2291 
2292 static struct type *
hppa32_register_type(struct gdbarch * gdbarch,int reg_nr)2293 hppa32_register_type (struct gdbarch *gdbarch, int reg_nr)
2294 {
2295    if (reg_nr < HPPA_FP4_REGNUM)
2296      return builtin_type_uint32;
2297    else
2298      return builtin_type_ieee_single_big;
2299 }
2300 
2301 /* Return the GDB type object for the "standard" data type of data
2302    in register N.  hppa64 version.  */
2303 
2304 static struct type *
hppa64_register_type(struct gdbarch * gdbarch,int reg_nr)2305 hppa64_register_type (struct gdbarch *gdbarch, int reg_nr)
2306 {
2307    if (reg_nr < HPPA_FP4_REGNUM)
2308      return builtin_type_uint64;
2309    else
2310      return builtin_type_ieee_double_big;
2311 }
2312 
2313 /* Return True if REGNUM is not a register available to the user
2314    through ptrace().  */
2315 
2316 static int
hppa_cannot_store_register(int regnum)2317 hppa_cannot_store_register (int regnum)
2318 {
2319   return (regnum == 0
2320           || regnum == HPPA_PCSQ_HEAD_REGNUM
2321           || (regnum >= HPPA_PCSQ_TAIL_REGNUM && regnum < HPPA_IPSW_REGNUM)
2322           || (regnum > HPPA_IPSW_REGNUM && regnum < HPPA_FP4_REGNUM));
2323 
2324 }
2325 
2326 static CORE_ADDR
hppa_smash_text_address(CORE_ADDR addr)2327 hppa_smash_text_address (CORE_ADDR addr)
2328 {
2329   /* The low two bits of the PC on the PA contain the privilege level.
2330      Some genius implementing a (non-GCC) compiler apparently decided
2331      this means that "addresses" in a text section therefore include a
2332      privilege level, and thus symbol tables should contain these bits.
2333      This seems like a bonehead thing to do--anyway, it seems to work
2334      for our purposes to just ignore those bits.  */
2335 
2336   return (addr &= ~0x3);
2337 }
2338 
2339 /* Get the ith function argument for the current function.  */
2340 CORE_ADDR
hppa_fetch_pointer_argument(struct frame_info * frame,int argi,struct type * type)2341 hppa_fetch_pointer_argument (struct frame_info *frame, int argi,
2342 			     struct type *type)
2343 {
2344   CORE_ADDR addr;
2345   get_frame_register (frame, HPPA_R0_REGNUM + 26 - argi, &addr);
2346   return addr;
2347 }
2348 
2349 static void
hppa_pseudo_register_read(struct gdbarch * gdbarch,struct regcache * regcache,int regnum,void * buf)2350 hppa_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
2351 			   int regnum, void *buf)
2352 {
2353     ULONGEST tmp;
2354 
2355     regcache_raw_read_unsigned (regcache, regnum, &tmp);
2356     if (regnum == HPPA_PCOQ_HEAD_REGNUM || regnum == HPPA_PCOQ_TAIL_REGNUM)
2357       tmp &= ~0x3;
2358     store_unsigned_integer (buf, sizeof(tmp), tmp);
2359 }
2360 
2361 static CORE_ADDR
hppa_find_global_pointer(struct value * function)2362 hppa_find_global_pointer (struct value *function)
2363 {
2364   return 0;
2365 }
2366 
2367 void
hppa_frame_prev_register_helper(struct frame_info * next_frame,struct trad_frame_saved_reg saved_regs[],int regnum,int * optimizedp,enum lval_type * lvalp,CORE_ADDR * addrp,int * realnump,void * valuep)2368 hppa_frame_prev_register_helper (struct frame_info *next_frame,
2369 			         struct trad_frame_saved_reg saved_regs[],
2370 				 int regnum, int *optimizedp,
2371 				 enum lval_type *lvalp, CORE_ADDR *addrp,
2372 				 int *realnump, void *valuep)
2373 {
2374   if (regnum == HPPA_PCOQ_TAIL_REGNUM)
2375     {
2376       if (valuep)
2377 	{
2378 	  CORE_ADDR pc;
2379 
2380 	  trad_frame_get_prev_register (next_frame, saved_regs,
2381 					HPPA_PCOQ_HEAD_REGNUM, optimizedp,
2382 					lvalp, addrp, realnump, valuep);
2383 
2384 	  pc = extract_unsigned_integer (valuep, 4);
2385 	  store_unsigned_integer (valuep, 4, pc + 4);
2386 	}
2387 
2388       /* It's a computed value.  */
2389       *optimizedp = 0;
2390       *lvalp = not_lval;
2391       *addrp = 0;
2392       *realnump = -1;
2393       return;
2394     }
2395 
2396   trad_frame_get_prev_register (next_frame, saved_regs, regnum,
2397 				optimizedp, lvalp, addrp, realnump, valuep);
2398 }
2399 
2400 static void
hppa_dwarf2_frame_init_reg(struct gdbarch * gdbarch,int regnum,struct dwarf2_frame_state_reg * reg)2401 hppa_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
2402                             struct dwarf2_frame_state_reg *reg)
2403 {
2404   if (regnum == HPPA_PCOQ_HEAD_REGNUM)
2405     reg->how = DWARF2_FRAME_REG_RA;
2406   else if (regnum == HPPA_SP_REGNUM)
2407     reg->how = DWARF2_FRAME_REG_CFA;
2408 }
2409 
2410 
2411 /* Here is a table of C type sizes on hppa with various compiles
2412    and options.  I measured this on PA 9000/800 with HP-UX 11.11
2413    and these compilers:
2414 
2415      /usr/ccs/bin/cc    HP92453-01 A.11.01.21
2416      /opt/ansic/bin/cc  HP92453-01 B.11.11.28706.GP
2417      /opt/aCC/bin/aCC   B3910B A.03.45
2418      gcc                gcc 3.3.2 native hppa2.0w-hp-hpux11.11
2419 
2420      cc            : 1 2 4 4 8 : 4 8 -- : 4 4
2421      ansic +DA1.1  : 1 2 4 4 8 : 4 8 16 : 4 4
2422      ansic +DA2.0  : 1 2 4 4 8 : 4 8 16 : 4 4
2423      ansic +DA2.0W : 1 2 4 8 8 : 4 8 16 : 8 8
2424      acc   +DA1.1  : 1 2 4 4 8 : 4 8 16 : 4 4
2425      acc   +DA2.0  : 1 2 4 4 8 : 4 8 16 : 4 4
2426      acc   +DA2.0W : 1 2 4 8 8 : 4 8 16 : 8 8
2427      gcc           : 1 2 4 4 8 : 4 8 16 : 4 4
2428 
2429    Each line is:
2430 
2431      compiler and options
2432      char, short, int, long, long long
2433      float, double, long double
2434      char *, void (*)()
2435 
2436    So all these compilers use either ILP32 or LP64 model.
2437    TODO: gcc has more options so it needs more investigation.
2438 
2439    For floating point types, see:
2440 
2441      http://docs.hp.com/hpux/pdf/B3906-90006.pdf
2442      HP-UX floating-point guide, hpux 11.00
2443 
2444    -- chastain 2003-12-18  */
2445 
2446 static struct gdbarch *
hppa_gdbarch_init(struct gdbarch_info info,struct gdbarch_list * arches)2447 hppa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2448 {
2449   struct gdbarch_tdep *tdep;
2450   struct gdbarch *gdbarch;
2451 
2452   /* Try to determine the ABI of the object we are loading.  */
2453   if (info.abfd != NULL && info.osabi == GDB_OSABI_UNKNOWN)
2454     {
2455       /* If it's a SOM file, assume it's HP/UX SOM.  */
2456       if (bfd_get_flavour (info.abfd) == bfd_target_som_flavour)
2457 	info.osabi = GDB_OSABI_HPUX_SOM;
2458     }
2459 
2460   /* find a candidate among the list of pre-declared architectures.  */
2461   arches = gdbarch_list_lookup_by_info (arches, &info);
2462   if (arches != NULL)
2463     return (arches->gdbarch);
2464 
2465   /* If none found, then allocate and initialize one.  */
2466   tdep = XZALLOC (struct gdbarch_tdep);
2467   gdbarch = gdbarch_alloc (&info, tdep);
2468 
2469   /* Determine from the bfd_arch_info structure if we are dealing with
2470      a 32 or 64 bits architecture.  If the bfd_arch_info is not available,
2471      then default to a 32bit machine.  */
2472   if (info.bfd_arch_info != NULL)
2473     tdep->bytes_per_address =
2474       info.bfd_arch_info->bits_per_address / info.bfd_arch_info->bits_per_byte;
2475   else
2476     tdep->bytes_per_address = 4;
2477 
2478   tdep->find_global_pointer = hppa_find_global_pointer;
2479 
2480   /* Some parts of the gdbarch vector depend on whether we are running
2481      on a 32 bits or 64 bits target.  */
2482   switch (tdep->bytes_per_address)
2483     {
2484       case 4:
2485         set_gdbarch_num_regs (gdbarch, hppa32_num_regs);
2486         set_gdbarch_register_name (gdbarch, hppa32_register_name);
2487         set_gdbarch_register_type (gdbarch, hppa32_register_type);
2488         break;
2489       case 8:
2490         set_gdbarch_num_regs (gdbarch, hppa64_num_regs);
2491         set_gdbarch_register_name (gdbarch, hppa64_register_name);
2492         set_gdbarch_register_type (gdbarch, hppa64_register_type);
2493         break;
2494       default:
2495         internal_error (__FILE__, __LINE__, "Unsupported address size: %d",
2496                         tdep->bytes_per_address);
2497     }
2498 
2499   set_gdbarch_long_bit (gdbarch, tdep->bytes_per_address * TARGET_CHAR_BIT);
2500   set_gdbarch_ptr_bit (gdbarch, tdep->bytes_per_address * TARGET_CHAR_BIT);
2501 
2502   /* The following gdbarch vector elements are the same in both ILP32
2503      and LP64, but might show differences some day.  */
2504   set_gdbarch_long_long_bit (gdbarch, 64);
2505   set_gdbarch_long_double_bit (gdbarch, 128);
2506   set_gdbarch_long_double_format (gdbarch, &floatformat_ia64_quad_big);
2507 
2508   /* The following gdbarch vector elements do not depend on the address
2509      size, or in any other gdbarch element previously set.  */
2510   set_gdbarch_skip_prologue (gdbarch, hppa_skip_prologue);
2511   set_gdbarch_inner_than (gdbarch, core_addr_greaterthan);
2512   set_gdbarch_sp_regnum (gdbarch, HPPA_SP_REGNUM);
2513   set_gdbarch_fp0_regnum (gdbarch, HPPA_FP0_REGNUM);
2514   set_gdbarch_cannot_store_register (gdbarch, hppa_cannot_store_register);
2515   set_gdbarch_cannot_fetch_register (gdbarch, hppa_cannot_store_register);
2516   set_gdbarch_addr_bits_remove (gdbarch, hppa_smash_text_address);
2517   set_gdbarch_smash_text_address (gdbarch, hppa_smash_text_address);
2518   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
2519   set_gdbarch_read_pc (gdbarch, hppa_target_read_pc);
2520   set_gdbarch_write_pc (gdbarch, hppa_target_write_pc);
2521 
2522   /* Helper for function argument information.  */
2523   set_gdbarch_fetch_pointer_argument (gdbarch, hppa_fetch_pointer_argument);
2524 
2525   set_gdbarch_print_insn (gdbarch, print_insn_hppa);
2526 
2527   /* When a hardware watchpoint triggers, we'll move the inferior past
2528      it by removing all eventpoints; stepping past the instruction
2529      that caused the trigger; reinserting eventpoints; and checking
2530      whether any watched location changed.  */
2531   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
2532 
2533   /* Inferior function call methods.  */
2534   switch (tdep->bytes_per_address)
2535     {
2536     case 4:
2537       set_gdbarch_push_dummy_call (gdbarch, hppa32_push_dummy_call);
2538       set_gdbarch_frame_align (gdbarch, hppa32_frame_align);
2539       set_gdbarch_convert_from_func_ptr_addr
2540         (gdbarch, hppa32_convert_from_func_ptr_addr);
2541       break;
2542     case 8:
2543       set_gdbarch_push_dummy_call (gdbarch, hppa64_push_dummy_call);
2544       set_gdbarch_frame_align (gdbarch, hppa64_frame_align);
2545       break;
2546     default:
2547       internal_error (__FILE__, __LINE__, "bad switch");
2548     }
2549 
2550   /* Struct return methods.  */
2551   switch (tdep->bytes_per_address)
2552     {
2553     case 4:
2554       set_gdbarch_return_value (gdbarch, hppa32_return_value);
2555       break;
2556     case 8:
2557       set_gdbarch_return_value (gdbarch, hppa64_return_value);
2558       break;
2559     default:
2560       internal_error (__FILE__, __LINE__, "bad switch");
2561     }
2562 
2563   set_gdbarch_breakpoint_from_pc (gdbarch, hppa_breakpoint_from_pc);
2564   set_gdbarch_pseudo_register_read (gdbarch, hppa_pseudo_register_read);
2565 
2566   /* Frame unwind methods.  */
2567   set_gdbarch_unwind_dummy_id (gdbarch, hppa_unwind_dummy_id);
2568   set_gdbarch_unwind_pc (gdbarch, hppa_unwind_pc);
2569 
2570   /* Hook in ABI-specific overrides, if they have been registered.  */
2571   gdbarch_init_osabi (info, gdbarch);
2572 
2573   /* Hook in the DWARF CFI frame unwinder.  */
2574   dwarf2_frame_set_init_reg (gdbarch, hppa_dwarf2_frame_init_reg);
2575   frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
2576 
2577   /* Hook in the default unwinders.  */
2578   frame_unwind_append_sniffer (gdbarch, hppa_stub_unwind_sniffer);
2579   frame_unwind_append_sniffer (gdbarch, hppa_frame_unwind_sniffer);
2580   frame_unwind_append_sniffer (gdbarch, hppa_fallback_unwind_sniffer);
2581 
2582   return gdbarch;
2583 }
2584 
2585 static void
hppa_dump_tdep(struct gdbarch * current_gdbarch,struct ui_file * file)2586 hppa_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
2587 {
2588   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
2589 
2590   fprintf_unfiltered (file, "bytes_per_address = %d\n",
2591                       tdep->bytes_per_address);
2592   fprintf_unfiltered (file, "elf = %s\n", tdep->is_elf ? "yes" : "no");
2593 }
2594 
2595 void
_initialize_hppa_tdep(void)2596 _initialize_hppa_tdep (void)
2597 {
2598   struct cmd_list_element *c;
2599   void break_at_finish_command (char *arg, int from_tty);
2600   void tbreak_at_finish_command (char *arg, int from_tty);
2601   void break_at_finish_at_depth_command (char *arg, int from_tty);
2602 
2603   gdbarch_register (bfd_arch_hppa, hppa_gdbarch_init, hppa_dump_tdep);
2604 
2605   hppa_objfile_priv_data = register_objfile_data ();
2606 
2607   add_cmd ("unwind", class_maintenance, unwind_command,
2608 	   "Print unwind table entry at given address.",
2609 	   &maintenanceprintlist);
2610 
2611   deprecate_cmd (add_com ("xbreak", class_breakpoint,
2612 			  break_at_finish_command,
2613 			  concat ("Set breakpoint at procedure exit. \n\
2614 Argument may be function name, or \"*\" and an address.\n\
2615 If function is specified, break at end of code for that function.\n\
2616 If an address is specified, break at the end of the function that contains \n\
2617 that exact address.\n",
2618 		   "With no arg, uses current execution address of selected stack frame.\n\
2619 This is useful for breaking on return to a stack frame.\n\
2620 \n\
2621 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
2622 \n\
2623 Do \"help breakpoints\" for info on other commands dealing with breakpoints.", NULL)), NULL);
2624   deprecate_cmd (add_com_alias ("xb", "xbreak", class_breakpoint, 1), NULL);
2625   deprecate_cmd (add_com_alias ("xbr", "xbreak", class_breakpoint, 1), NULL);
2626   deprecate_cmd (add_com_alias ("xbre", "xbreak", class_breakpoint, 1), NULL);
2627   deprecate_cmd (add_com_alias ("xbrea", "xbreak", class_breakpoint, 1), NULL);
2628 
2629   deprecate_cmd (c = add_com ("txbreak", class_breakpoint,
2630 			      tbreak_at_finish_command,
2631 "Set temporary breakpoint at procedure exit.  Either there should\n\
2632 be no argument or the argument must be a depth.\n"), NULL);
2633   set_cmd_completer (c, location_completer);
2634 
2635   if (xdb_commands)
2636     deprecate_cmd (add_com ("bx", class_breakpoint,
2637 			    break_at_finish_at_depth_command,
2638 "Set breakpoint at procedure exit.  Either there should\n\
2639 be no argument or the argument must be a depth.\n"), NULL);
2640 
2641   /* Debug this files internals. */
2642   deprecated_add_show_from_set
2643     (add_set_cmd ("hppa", class_maintenance, var_zinteger,
2644 		  &hppa_debug, "Set hppa debugging.\n\
2645 When non-zero, hppa specific debugging is enabled.", &setdebuglist),
2646      &showdebuglist);
2647 }
2648