1 /* Machine-dependent code which would otherwise be in inflow.c and core.c,
2 for GDB, the GNU debugger. This code is for the HP PA-RISC cpu.
3 Copyright 1986, 1987, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
5 Contributed by the Center for Software Science at the
6 University of Utah (pa-gdb-bugs@cs.utah.edu).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 /* For argument passing to the inferior */
33 #include <sys/types.h>
36 #include <sys/param.h>
39 #include <sys/ioctl.h>
41 #ifdef COFF_ENCAPSULATE
42 #include "a.out.encap.h"
47 #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
50 /*#include <sys/user.h> After a.out.h */
53 #include <machine/psl.h>
61 /* Last modification time of executable file.
62 Also used in source.c to compare against mtime of a source file. */
64 extern int exec_mtime
;
66 /* Virtual addresses of bounds of the two areas of memory in the core file. */
68 /* extern CORE_ADDR data_start; */
69 extern CORE_ADDR data_end
;
70 extern CORE_ADDR stack_start
;
71 extern CORE_ADDR stack_end
;
73 /* Virtual addresses of bounds of two areas of memory in the exec file.
74 Note that the data area in the exec file is used only when there is no core file. */
76 extern CORE_ADDR text_start
;
77 extern CORE_ADDR text_end
;
79 extern CORE_ADDR exec_data_start
;
80 extern CORE_ADDR exec_data_end
;
82 /* Address in executable file of start of text area data. */
84 extern int text_offset
;
86 /* Address in executable file of start of data area data. */
88 extern int exec_data_offset
;
90 /* Address in core file of start of data area data. */
92 extern int data_offset
;
94 /* Address in core file of start of stack area data. */
96 extern int stack_offset
;
98 struct header file_hdr
;
99 struct som_exec_auxhdr exec_hdr
;
101 /* Routines to extract various sized constants out of hppa
104 /* This assumes that no garbage lies outside of the lower bits of
108 sign_extend (val
, bits
)
111 return (int)(val
>> bits
- 1 ? (-1 << bits
) | val
: val
);
114 /* For many immediate values the sign bit is the low bit! */
117 low_sign_extend (val
, bits
)
120 return (int)((val
& 0x1 ? (-1 << (bits
- 1)) : 0) | val
>> 1);
122 /* extract the immediate field from a ld{bhw}s instruction */
127 get_field (val
, from
, to
)
128 unsigned val
, from
, to
;
130 val
= val
>> 31 - to
;
131 return val
& ((1 << 32 - from
) - 1);
135 set_field (val
, from
, to
, new_val
)
136 unsigned *val
, from
, to
;
138 unsigned mask
= ~((1 << (to
- from
+ 1)) << (31 - from
));
139 return *val
= *val
& mask
| (new_val
<< (31 - from
));
142 /* extract a 3-bit space register number from a be, ble, mtsp or mfsp */
147 return GET_FIELD (word
, 18, 18) << 2 | GET_FIELD (word
, 16, 17);
150 extract_5_load (word
)
153 return low_sign_extend (word
>> 16 & MASK_5
, 5);
156 /* extract the immediate field from a st{bhw}s instruction */
159 extract_5_store (word
)
162 return low_sign_extend (word
& MASK_5
, 5);
165 /* extract an 11 bit immediate field */
171 return low_sign_extend (word
& MASK_11
, 11);
174 /* extract a 14 bit immediate field */
180 return low_sign_extend (word
& MASK_14
, 14);
183 /* deposit a 14 bit constant in a word */
186 deposit_14 (opnd
, word
)
190 unsigned sign
= (opnd
< 0 ? 1 : 0);
192 return word
| ((unsigned)opnd
<< 1 & MASK_14
) | sign
;
195 /* extract a 21 bit constant */
205 val
= GET_FIELD (word
, 20, 20);
207 val
|= GET_FIELD (word
, 9, 19);
209 val
|= GET_FIELD (word
, 5, 6);
211 val
|= GET_FIELD (word
, 0, 4);
213 val
|= GET_FIELD (word
, 7, 8);
214 return sign_extend (val
, 21) << 11;
217 /* deposit a 21 bit constant in a word. Although 21 bit constants are
218 usually the top 21 bits of a 32 bit constant, we assume that only
219 the low 21 bits of opnd are relevant */
222 deposit_21 (opnd
, word
)
227 val
|= GET_FIELD (opnd
, 11 + 14, 11 + 18);
229 val
|= GET_FIELD (opnd
, 11 + 12, 11 + 13);
231 val
|= GET_FIELD (opnd
, 11 + 19, 11 + 20);
233 val
|= GET_FIELD (opnd
, 11 + 1, 11 + 11);
235 val
|= GET_FIELD (opnd
, 11 + 0, 11 + 0);
239 /* extract a 12 bit constant from branch instructions */
245 return sign_extend (GET_FIELD (word
, 19, 28) |
246 GET_FIELD (word
, 29, 29) << 10 |
247 (word
& 0x1) << 11, 12) << 2;
250 /* extract a 17 bit constant from branch instructions, returning the
251 19 bit signed value. */
257 return sign_extend (GET_FIELD (word
, 19, 28) |
258 GET_FIELD (word
, 29, 29) << 10 |
259 GET_FIELD (word
, 11, 15) << 11 |
260 (word
& 0x1) << 16, 17) << 2;
265 static struct unwind_table_entry
*
266 find_unwind_entry(pc
)
269 static struct unwind_table_entry
*unwind
= NULL
, *unwind_end
;
270 struct unwind_table_entry
*u
;
277 asection
*unwind_sec
;
279 unwind_sec
= bfd_get_section_by_name (exec_bfd
, "$UNWIND_START$");
284 size
= bfd_section_size (exec_bfd
, unwind_sec
);
285 unwind
= malloc (size
);
286 unwind_end
= unwind
+ size
/sizeof (struct unwind_table_entry
);
288 bfd_get_section_contents (exec_bfd
, unwind_sec
, unwind
, 0, size
);
292 for (u
= unwind
; u
< unwind_end
; u
++)
294 if (pc
>= u
->region_start
295 && pc
<= u
->region_end
)
302 find_return_regnum(pc
)
305 struct unwind_table_entry
*u
;
307 u
= find_unwind_entry (pc
);
319 find_proc_framesize(pc
)
322 struct unwind_table_entry
*u
;
324 u
= find_unwind_entry (pc
);
329 return u
->Total_frame_size
<< 3;
333 saved_pc_after_call (frame
)
338 ret_regnum
= find_return_regnum (get_frame_pc (frame
));
340 return read_register (ret_regnum
) & ~0x3;
344 frame_saved_pc (frame
)
349 CORE_ADDR pc
= get_frame_pc (frame
);
352 ret_regnum
= find_return_regnum (pc
);
354 return read_register (ret_regnum
) & ~0x3;
356 return read_memory_integer (frame
->frame
- 20, 4) & ~0x3;
359 /* We need to correct the PC and the FP for the outermost frame when we are
363 init_extra_frame_info (fromleaf
, frame
)
365 struct frame_info
*frame
;
370 if (frame
->next
) /* Only do this for outermost frame */
373 flags
= read_register (FLAGS_REGNUM
);
374 if (flags
& 2) /* In system call? */
375 frame
->pc
= read_register (31) & ~0x3;
377 /* The outermost frame is always derived from PC-framesize */
378 framesize
= find_proc_framesize(frame
->pc
);
380 frame
->frame
= read_register (FP_REGNUM
);
382 frame
->frame
= read_register (SP_REGNUM
) - framesize
;
384 if (framesize
!= 0) /* Frameless? */
387 /* For frameless functions, we need to look at the caller's frame */
388 framesize
= find_proc_framesize(FRAME_SAVED_PC(frame
));
390 frame
->frame
-= framesize
;
395 struct frame_info
*frame
;
399 framesize
= find_proc_framesize(FRAME_SAVED_PC(frame
));
402 return frame
->frame
- framesize
;
404 return read_memory_integer (frame
->frame
, 4);
407 /* To see if a frame chain is valid, see if the caller looks like it
408 was compiled with gcc. */
410 int frame_chain_valid (chain
, thisframe
)
414 if (chain
&& (chain
> 0x60000000))
416 CORE_ADDR pc
= get_pc_function_start (FRAME_SAVED_PC (thisframe
));
417 if (inside_entry_file (pc
))
419 /* look for stw rp, -20(0,sp); copy 4,1; copy sp, 4 */
420 if (read_memory_integer (pc
, 4) == 0x6BC23FD9)
423 if (read_memory_integer (pc
, 4) == 0x8040241 &&
424 read_memory_integer (pc
+ 4, 4) == 0x81E0244)
433 /* Some helper functions. gcc_p returns 1 if the function beginning at
434 pc appears to have been compiled with gcc. hpux_cc_p returns 1 if
435 fn was compiled with hpux cc. gcc functions look like :
437 stw rp,-0x14(sp) ; optional
440 stwm r1,framesize(sp)
442 hpux cc functions look like:
444 stw rp,-0x14(sp) ; optional.
451 if (read_memory_integer (pc
, 4) == 0x6BC23FD9)
454 if (read_memory_integer (pc
, 4) == 0x8040241 &&
455 read_memory_integer (pc
+ 4, 4) == 0x81E0244)
461 * These functions deal with saving and restoring register state
462 * around a function call in the inferior. They keep the stack
463 * double-word aligned; eventually, on an hp700, the stack will have
464 * to be aligned to a 64-byte boundary.
470 register CORE_ADDR sp
= read_register (SP_REGNUM
);
474 /* Space for "arguments"; the RP goes in here. */
476 int_buffer
= read_register (RP_REGNUM
) | 0x3;
477 write_memory (sp
- 20, (char *)&int_buffer
, 4);
478 int_buffer
= read_register (FP_REGNUM
);
479 write_memory (sp
, (char *)&int_buffer
, 4);
480 write_register (FP_REGNUM
, sp
);
482 for (regnum
= 1; regnum
< 32; regnum
++)
483 if (regnum
!= RP_REGNUM
&& regnum
!= FP_REGNUM
)
484 sp
= push_word (sp
, read_register (regnum
));
486 for (regnum
= FP0_REGNUM
; regnum
< NUM_REGS
; regnum
++)
487 { read_register_bytes (REGISTER_BYTE (regnum
), (char *)&freg_buffer
, 8);
488 sp
= push_bytes (sp
, (char *)&freg_buffer
, 8);}
489 sp
= push_word (sp
, read_register (IPSW_REGNUM
));
490 sp
= push_word (sp
, read_register (SAR_REGNUM
));
491 sp
= push_word (sp
, read_register (PCOQ_HEAD_REGNUM
));
492 sp
= push_word (sp
, read_register (PCSQ_HEAD_REGNUM
));
493 sp
= push_word (sp
, read_register (PCOQ_TAIL_REGNUM
));
494 sp
= push_word (sp
, read_register (PCSQ_TAIL_REGNUM
));
495 write_register (SP_REGNUM
, sp
);
498 find_dummy_frame_regs (frame
, frame_saved_regs
)
499 struct frame_info
*frame
;
500 struct frame_saved_regs
*frame_saved_regs
;
502 CORE_ADDR fp
= frame
->frame
;
505 frame_saved_regs
->regs
[RP_REGNUM
] = fp
- 20 & ~0x3;
506 frame_saved_regs
->regs
[FP_REGNUM
] = fp
;
507 frame_saved_regs
->regs
[1] = fp
+ 8;
508 frame_saved_regs
->regs
[3] = fp
+ 12;
509 for (fp
+= 16, i
= 5; i
< 32; fp
+= 4, i
++)
510 frame_saved_regs
->regs
[i
] = fp
;
512 for (i
= FP0_REGNUM
; i
< NUM_REGS
; i
++, fp
+= 8)
513 frame_saved_regs
->regs
[i
] = fp
;
514 frame_saved_regs
->regs
[IPSW_REGNUM
] = fp
; fp
+= 4;
515 frame_saved_regs
->regs
[SAR_REGNUM
] = fp
; fp
+= 4;
516 frame_saved_regs
->regs
[PCOQ_HEAD_REGNUM
] = fp
; fp
+=4;
517 frame_saved_regs
->regs
[PCSQ_HEAD_REGNUM
] = fp
; fp
+=4;
518 frame_saved_regs
->regs
[PCOQ_TAIL_REGNUM
] = fp
; fp
+=4;
519 frame_saved_regs
->regs
[PCSQ_TAIL_REGNUM
] = fp
;
525 register FRAME frame
= get_current_frame ();
526 register CORE_ADDR fp
;
528 struct frame_saved_regs fsr
;
529 struct frame_info
*fi
;
531 fi
= get_frame_info (frame
);
533 get_frame_saved_regs (fi
, &fsr
);
534 if (fsr
.regs
[IPSW_REGNUM
]) /* Restoring a call dummy frame */
535 hp_restore_pc_queue (&fsr
);
536 for (regnum
= 31; regnum
> 0; regnum
--)
537 if (fsr
.regs
[regnum
])
538 write_register (regnum
, read_memory_integer (fsr
.regs
[regnum
], 4));
539 for (regnum
= NUM_REGS
- 1; regnum
>= FP0_REGNUM
; regnum
--)
540 if (fsr
.regs
[regnum
])
541 { read_memory (fsr
.regs
[regnum
], (char *)&freg_buffer
, 8);
542 write_register_bytes (REGISTER_BYTE (regnum
), (char *)&freg_buffer
, 8);
544 if (fsr
.regs
[IPSW_REGNUM
])
545 write_register (IPSW_REGNUM
,
546 read_memory_integer (fsr
.regs
[IPSW_REGNUM
], 4));
547 if (fsr
.regs
[SAR_REGNUM
])
548 write_register (SAR_REGNUM
,
549 read_memory_integer (fsr
.regs
[SAR_REGNUM
], 4));
550 if (fsr
.regs
[PCOQ_TAIL_REGNUM
])
551 write_register (PCOQ_TAIL_REGNUM
,
552 read_memory_integer (fsr
.regs
[PCOQ_TAIL_REGNUM
], 4));
553 write_register (FP_REGNUM
, read_memory_integer (fp
, 4));
554 if (fsr
.regs
[IPSW_REGNUM
]) /* call dummy */
555 write_register (SP_REGNUM
, fp
- 48);
557 write_register (SP_REGNUM
, fp
);
558 flush_cached_frames ();
559 set_current_frame (create_new_frame (read_register (FP_REGNUM
),
564 * After returning to a dummy on the stack, restore the instruction
565 * queue space registers. */
568 hp_restore_pc_queue (fsr
)
569 struct frame_saved_regs
*fsr
;
571 CORE_ADDR pc
= read_pc ();
572 CORE_ADDR new_pc
= read_memory_integer (fsr
->regs
[PCOQ_HEAD_REGNUM
], 4);
577 /* Advance past break instruction in the call dummy. */
578 pc
+= 4; write_register (PCOQ_HEAD_REGNUM
, pc
);
579 pc
+= 4; write_register (PCOQ_TAIL_REGNUM
, pc
);
582 * HPUX doesn't let us set the space registers or the space
583 * registers of the PC queue through ptrace. Boo, hiss.
584 * Conveniently, the call dummy has this sequence of instructions
589 * So, load up the registers and single step until we are in the
593 write_register (21, read_memory_integer (fsr
->regs
[PCSQ_HEAD_REGNUM
], 4));
594 write_register (22, new_pc
);
596 for (insn_count
= 0; insn_count
< 3; insn_count
++)
603 stop_signal
= WTERMSIG (w
);
604 terminal_ours_for_output ();
605 printf ("\nProgram terminated with signal %d, %s\n",
606 stop_signal
, safe_strsignal (stop_signal
));
611 fetch_inferior_registers (-1);
616 hp_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
)
621 CORE_ADDR struct_addr
;
623 /* array of arguments' offsets */
624 int *offset
= (int *)alloca(nargs
);
628 for (i
= 0; i
< nargs
; i
++)
630 cum
+= TYPE_LENGTH (VALUE_TYPE (args
[i
]));
631 /* value must go at proper alignment. Assume alignment is a
633 alignment
= hp_alignof (VALUE_TYPE (args
[i
]));
635 cum
= (cum
+ alignment
) & -alignment
;
638 sp
+= min ((cum
+ 7) & -8, 16);
639 for (i
= 0; i
< nargs
; i
++)
641 write_memory (sp
+ offset
[i
], VALUE_CONTENTS (args
[i
]),
642 TYPE_LENGTH (VALUE_TYPE (args
[i
])));
645 write_register (28, struct_addr
);
649 /* return the alignment of a type in bytes. Structures have the maximum
650 alignment required by their fields. */
656 int max_align
, align
, i
;
657 switch (TYPE_CODE (arg
))
662 return TYPE_LENGTH (arg
);
663 case TYPE_CODE_ARRAY
:
664 return hp_alignof (TYPE_FIELD_TYPE (arg
, 0));
665 case TYPE_CODE_STRUCT
:
666 case TYPE_CODE_UNION
:
668 for (i
= 0; i
< TYPE_NFIELDS (arg
); i
++)
670 /* Bit fields have no real alignment. */
671 if (!TYPE_FIELD_BITPOS (arg
, i
))
673 align
= hp_alignof (TYPE_FIELD_TYPE (arg
, i
));
674 max_align
= max (max_align
, align
);
683 /* Print the register regnum, or all registers if regnum is -1 */
685 pa_do_registers_info (regnum
, fpregs
)
689 char raw_regs
[REGISTER_BYTES
];
692 for (i
= 0; i
< NUM_REGS
; i
++)
693 read_relative_register_raw_bytes (i
, raw_regs
+ REGISTER_BYTE (i
));
695 pa_print_registers (raw_regs
, regnum
, fpregs
);
696 else if (regnum
< FP0_REGNUM
)
698 printf ("%s %x\n", reg_names
[regnum
], *(long *)(raw_regs
+
699 REGISTER_BYTE (regnum
)));
702 pa_print_fp_reg (regnum
);
705 pa_print_registers (raw_regs
, regnum
, fpregs
)
712 for (i
= 0; i
< 18; i
++)
713 printf ("%8.8s: %8x %8.8s: %8x %8.8s: %8x %8.8s: %8x\n",
715 *(int *)(raw_regs
+ REGISTER_BYTE (i
)),
717 *(int *)(raw_regs
+ REGISTER_BYTE (i
+ 18)),
719 *(int *)(raw_regs
+ REGISTER_BYTE (i
+ 36)),
721 *(int *)(raw_regs
+ REGISTER_BYTE (i
+ 54)));
724 for (i
= 72; i
< NUM_REGS
; i
++)
731 unsigned char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
732 unsigned char virtual_buffer
[MAX_REGISTER_VIRTUAL_SIZE
];
735 /* Get the data in raw format, then convert also to virtual format. */
736 read_relative_register_raw_bytes (i
, raw_buffer
);
737 REGISTER_CONVERT_TO_VIRTUAL (i
, raw_buffer
, virtual_buffer
);
739 fputs_filtered (reg_names
[i
], stdout
);
740 print_spaces_filtered (15 - strlen (reg_names
[i
]), stdout
);
742 val_print (REGISTER_VIRTUAL_TYPE (i
), virtual_buffer
, 0, stdout
, 0,
743 1, 0, Val_pretty_default
);
744 printf_filtered ("\n");
748 /* Function calls that pass into a new compilation unit must pass through a
749 small piece of code that does long format (`external' in HPPA parlance)
750 jumps. We figure out where the trampoline is going to end up, and return
751 the PC of the final destination. If we aren't in a trampoline, we just
754 For computed calls, we just extract the new PC from r22. */
757 skip_trampoline_code (pc
, name
)
762 static CORE_ADDR dyncall
= 0;
763 struct minimal_symbol
*msym
;
765 /* FIXME XXX - dyncall must be initialized whenever we get a new exec file */
769 msym
= lookup_minimal_symbol ("$$dyncall", NULL
);
771 dyncall
= SYMBOL_VALUE_ADDRESS (msym
);
777 return (CORE_ADDR
)(read_register (22) & ~0x3);
779 inst0
= read_memory_integer (pc
, 4);
780 inst1
= read_memory_integer (pc
+4, 4);
782 if ( (inst0
& 0xffe00000) == 0x20200000 /* ldil xxx, r1 */
783 && (inst1
& 0xffe0e002) == 0xe0202002) /* be,n yyy(sr4, r1) */
784 pc
= extract_21 (inst0
) + extract_17 (inst1
);
786 pc
= (CORE_ADDR
)NULL
;
792 unwind_command (exp
, from_tty
)
800 struct unwind_table_entry
*u
;
803 /* If we have an expression, evaluate it and use it as the address. */
805 if (exp
!= 0 && *exp
!= 0)
806 address
= parse_and_eval_address (exp
);
810 xxx
.u
= find_unwind_entry (address
);
814 printf ("Can't find unwind table entry for PC 0x%x\n", address
);
818 printf ("%08x\n%08X\n%08X\n%08X\n", xxx
.foo
[0], xxx
.foo
[1], xxx
.foo
[2],
823 _initialize_hppah_tdep ()
825 add_com ("unwind", class_obscure
, unwind_command
, "Print unwind info\n");
827 (add_set_cmd ("use_unwind", class_obscure
, var_boolean
,
829 "Set the usage of unwind info", &setlist
),