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