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