* hppa-tdep.c (frame_chain): Rework so that it correctly
[deliverable/binutils-gdb.git] / gdb / hppa-tdep.c
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, 1993 Free Software Foundation, Inc.
4
5 Contributed by the Center for Software Science at the
6 University of Utah (pa-gdb-bugs@cs.utah.edu).
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23
24 #include "defs.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "value.h"
28
29 /* For argument passing to the inferior */
30 #include "symtab.h"
31
32 #ifdef USG
33 #include <sys/types.h>
34 #endif
35
36 #include <sys/param.h>
37 #include <sys/dir.h>
38 #include <signal.h>
39 #include <sys/ioctl.h>
40
41 #ifdef COFF_ENCAPSULATE
42 #include "a.out.encap.h"
43 #else
44 #include <a.out.h>
45 #endif
46 #ifndef N_SET_MAGIC
47 #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
48 #endif
49
50 /*#include <sys/user.h> After a.out.h */
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <machine/psl.h>
54 #include "wait.h"
55
56 #include "gdbcore.h"
57 #include "gdbcmd.h"
58 #include "target.h"
59 #include "symfile.h"
60 #include "objfiles.h"
61
62 static int restore_pc_queue PARAMS ((struct frame_saved_regs *fsr));
63 static int hppa_alignof PARAMS ((struct type *arg));
64 static FRAME_ADDR dig_fp_from_stack PARAMS ((FRAME frame,
65 struct unwind_table_entry *u));
66 CORE_ADDR frame_saved_pc PARAMS ((FRAME frame));
67
68 \f
69 /* Routines to extract various sized constants out of hppa
70 instructions. */
71
72 /* This assumes that no garbage lies outside of the lower bits of
73 value. */
74
75 int
76 sign_extend (val, bits)
77 unsigned val, bits;
78 {
79 return (int)(val >> bits - 1 ? (-1 << bits) | val : val);
80 }
81
82 /* For many immediate values the sign bit is the low bit! */
83
84 int
85 low_sign_extend (val, bits)
86 unsigned val, bits;
87 {
88 return (int)((val & 0x1 ? (-1 << (bits - 1)) : 0) | val >> 1);
89 }
90 /* extract the immediate field from a ld{bhw}s instruction */
91
92 unsigned
93 get_field (val, from, to)
94 unsigned val, from, to;
95 {
96 val = val >> 31 - to;
97 return val & ((1 << 32 - from) - 1);
98 }
99
100 unsigned
101 set_field (val, from, to, new_val)
102 unsigned *val, from, to;
103 {
104 unsigned mask = ~((1 << (to - from + 1)) << (31 - from));
105 return *val = *val & mask | (new_val << (31 - from));
106 }
107
108 /* extract a 3-bit space register number from a be, ble, mtsp or mfsp */
109
110 extract_3 (word)
111 unsigned word;
112 {
113 return GET_FIELD (word, 18, 18) << 2 | GET_FIELD (word, 16, 17);
114 }
115
116 extract_5_load (word)
117 unsigned word;
118 {
119 return low_sign_extend (word >> 16 & MASK_5, 5);
120 }
121
122 /* extract the immediate field from a st{bhw}s instruction */
123
124 int
125 extract_5_store (word)
126 unsigned word;
127 {
128 return low_sign_extend (word & MASK_5, 5);
129 }
130
131 /* extract the immediate field from a break instruction */
132
133 unsigned
134 extract_5r_store (word)
135 unsigned word;
136 {
137 return (word & MASK_5);
138 }
139
140 /* extract the immediate field from a {sr}sm instruction */
141
142 unsigned
143 extract_5R_store (word)
144 unsigned word;
145 {
146 return (word >> 16 & MASK_5);
147 }
148
149 /* extract an 11 bit immediate field */
150
151 int
152 extract_11 (word)
153 unsigned word;
154 {
155 return low_sign_extend (word & MASK_11, 11);
156 }
157
158 /* extract a 14 bit immediate field */
159
160 int
161 extract_14 (word)
162 unsigned word;
163 {
164 return low_sign_extend (word & MASK_14, 14);
165 }
166
167 /* deposit a 14 bit constant in a word */
168
169 unsigned
170 deposit_14 (opnd, word)
171 int opnd;
172 unsigned word;
173 {
174 unsigned sign = (opnd < 0 ? 1 : 0);
175
176 return word | ((unsigned)opnd << 1 & MASK_14) | sign;
177 }
178
179 /* extract a 21 bit constant */
180
181 int
182 extract_21 (word)
183 unsigned word;
184 {
185 int val;
186
187 word &= MASK_21;
188 word <<= 11;
189 val = GET_FIELD (word, 20, 20);
190 val <<= 11;
191 val |= GET_FIELD (word, 9, 19);
192 val <<= 2;
193 val |= GET_FIELD (word, 5, 6);
194 val <<= 5;
195 val |= GET_FIELD (word, 0, 4);
196 val <<= 2;
197 val |= GET_FIELD (word, 7, 8);
198 return sign_extend (val, 21) << 11;
199 }
200
201 /* deposit a 21 bit constant in a word. Although 21 bit constants are
202 usually the top 21 bits of a 32 bit constant, we assume that only
203 the low 21 bits of opnd are relevant */
204
205 unsigned
206 deposit_21 (opnd, word)
207 unsigned opnd, word;
208 {
209 unsigned val = 0;
210
211 val |= GET_FIELD (opnd, 11 + 14, 11 + 18);
212 val <<= 2;
213 val |= GET_FIELD (opnd, 11 + 12, 11 + 13);
214 val <<= 2;
215 val |= GET_FIELD (opnd, 11 + 19, 11 + 20);
216 val <<= 11;
217 val |= GET_FIELD (opnd, 11 + 1, 11 + 11);
218 val <<= 1;
219 val |= GET_FIELD (opnd, 11 + 0, 11 + 0);
220 return word | val;
221 }
222
223 /* extract a 12 bit constant from branch instructions */
224
225 int
226 extract_12 (word)
227 unsigned word;
228 {
229 return sign_extend (GET_FIELD (word, 19, 28) |
230 GET_FIELD (word, 29, 29) << 10 |
231 (word & 0x1) << 11, 12) << 2;
232 }
233
234 /* extract a 17 bit constant from branch instructions, returning the
235 19 bit signed value. */
236
237 int
238 extract_17 (word)
239 unsigned word;
240 {
241 return sign_extend (GET_FIELD (word, 19, 28) |
242 GET_FIELD (word, 29, 29) << 10 |
243 GET_FIELD (word, 11, 15) << 11 |
244 (word & 0x1) << 16, 17) << 2;
245 }
246 \f
247 /* Lookup the unwind (stack backtrace) info for the given PC. We search all
248 of the objfiles seeking the unwind table entry for this PC. Each objfile
249 contains a sorted list of struct unwind_table_entry. Since we do a binary
250 search of the unwind tables, we depend upon them to be sorted. */
251
252 static struct unwind_table_entry *
253 find_unwind_entry(pc)
254 CORE_ADDR pc;
255 {
256 int first, middle, last;
257 struct objfile *objfile;
258
259 ALL_OBJFILES (objfile)
260 {
261 struct obj_unwind_info *ui;
262
263 ui = OBJ_UNWIND_INFO (objfile);
264
265 if (!ui)
266 continue;
267
268 /* First, check the cache */
269
270 if (ui->cache
271 && pc >= ui->cache->region_start
272 && pc <= ui->cache->region_end)
273 return ui->cache;
274
275 /* Not in the cache, do a binary search */
276
277 first = 0;
278 last = ui->last;
279
280 while (first <= last)
281 {
282 middle = (first + last) / 2;
283 if (pc >= ui->table[middle].region_start
284 && pc <= ui->table[middle].region_end)
285 {
286 ui->cache = &ui->table[middle];
287 return &ui->table[middle];
288 }
289
290 if (pc < ui->table[middle].region_start)
291 last = middle - 1;
292 else
293 first = middle + 1;
294 }
295 } /* ALL_OBJFILES() */
296 return NULL;
297 }
298
299 /* Called when no unwind descriptor was found for PC. Returns 1 if it
300 appears that PC is in a linker stub. */
301 static int pc_in_linker_stub PARAMS ((CORE_ADDR));
302
303 static int
304 pc_in_linker_stub (pc)
305 CORE_ADDR pc;
306 {
307 int found_magic_instruction = 0;
308 int i;
309 char buf[4];
310
311 /* If unable to read memory, assume pc is not in a linker stub. */
312 if (target_read_memory (pc, buf, 4) != 0)
313 return 0;
314
315 /* We are looking for something like
316
317 ; $$dyncall jams RP into this special spot in the frame (RP')
318 ; before calling the "call stub"
319 ldw -18(sp),rp
320
321 ldsid (rp),r1 ; Get space associated with RP into r1
322 mtsp r1,sp ; Move it into space register 0
323 be,n 0(sr0),rp) ; back to your regularly scheduled program
324 */
325
326 /* Maximum known linker stub size is 4 instructions. Search forward
327 from the given PC, then backward. */
328 for (i = 0; i < 4; i++)
329 {
330 /* If we hit something with an unwind, stop searching this direction. */
331
332 if (find_unwind_entry (pc + i * 4) != 0)
333 break;
334
335 /* Check for ldsid (rp),r1 which is the magic instruction for a
336 return from a cross-space function call. */
337 if (read_memory_integer (pc + i * 4, 4) == 0x004010a1)
338 {
339 found_magic_instruction = 1;
340 break;
341 }
342 /* Add code to handle long call/branch and argument relocation stubs
343 here. */
344 }
345
346 if (found_magic_instruction != 0)
347 return 1;
348
349 /* Now look backward. */
350 for (i = 0; i < 4; i++)
351 {
352 /* If we hit something with an unwind, stop searching this direction. */
353
354 if (find_unwind_entry (pc - i * 4) != 0)
355 break;
356
357 /* Check for ldsid (rp),r1 which is the magic instruction for a
358 return from a cross-space function call. */
359 if (read_memory_integer (pc - i * 4, 4) == 0x004010a1)
360 {
361 found_magic_instruction = 1;
362 break;
363 }
364 /* Add code to handle long call/branch and argument relocation stubs
365 here. */
366 }
367 return found_magic_instruction;
368 }
369
370 static int
371 find_return_regnum(pc)
372 CORE_ADDR pc;
373 {
374 struct unwind_table_entry *u;
375
376 u = find_unwind_entry (pc);
377
378 if (!u)
379 return RP_REGNUM;
380
381 if (u->Millicode)
382 return 31;
383
384 return RP_REGNUM;
385 }
386
387 /* Return size of frame, or -1 if we should use a frame pointer. */
388 int
389 find_proc_framesize(pc)
390 CORE_ADDR pc;
391 {
392 struct unwind_table_entry *u;
393
394 u = find_unwind_entry (pc);
395
396 if (!u)
397 {
398 if (pc_in_linker_stub (pc))
399 /* Linker stubs have a zero size frame. */
400 return 0;
401 else
402 return -1;
403 }
404
405 if (u->Save_SP)
406 /* If this bit is set, it means there is a frame pointer and we should
407 use it. */
408 return -1;
409
410 return u->Total_frame_size << 3;
411 }
412
413 /* Return offset from sp at which rp is saved, or 0 if not saved. */
414 static int rp_saved PARAMS ((CORE_ADDR));
415
416 static int
417 rp_saved (pc)
418 CORE_ADDR pc;
419 {
420 struct unwind_table_entry *u;
421
422 u = find_unwind_entry (pc);
423
424 if (!u)
425 {
426 if (pc_in_linker_stub (pc))
427 /* This is the so-called RP'. */
428 return -24;
429 else
430 return 0;
431 }
432
433 if (u->Save_RP)
434 return -20;
435 else
436 return 0;
437 }
438 \f
439 int
440 frameless_function_invocation (frame)
441 FRAME frame;
442 {
443 struct unwind_table_entry *u;
444
445 u = find_unwind_entry (frame->pc);
446
447 if (u == 0)
448 return frameless_look_for_prologue (frame);
449
450 return (u->Total_frame_size == 0);
451 }
452
453 CORE_ADDR
454 saved_pc_after_call (frame)
455 FRAME frame;
456 {
457 int ret_regnum;
458
459 ret_regnum = find_return_regnum (get_frame_pc (frame));
460
461 return read_register (ret_regnum) & ~0x3;
462 }
463 \f
464 CORE_ADDR
465 frame_saved_pc (frame)
466 FRAME frame;
467 {
468 CORE_ADDR pc = get_frame_pc (frame);
469
470 if (frameless_function_invocation (frame))
471 {
472 int ret_regnum;
473
474 ret_regnum = find_return_regnum (pc);
475
476 return read_register (ret_regnum) & ~0x3;
477 }
478 else
479 {
480 int rp_offset = rp_saved (pc);
481
482 if (rp_offset == 0)
483 return read_register (RP_REGNUM) & ~0x3;
484 else
485 return read_memory_integer (frame->frame + rp_offset, 4) & ~0x3;
486 }
487 }
488 \f
489 /* We need to correct the PC and the FP for the outermost frame when we are
490 in a system call. */
491
492 void
493 init_extra_frame_info (fromleaf, frame)
494 int fromleaf;
495 struct frame_info *frame;
496 {
497 int flags;
498 int framesize;
499
500 if (frame->next) /* Only do this for outermost frame */
501 return;
502
503 flags = read_register (FLAGS_REGNUM);
504 if (flags & 2) /* In system call? */
505 frame->pc = read_register (31) & ~0x3;
506
507 /* The outermost frame is always derived from PC-framesize */
508 framesize = find_proc_framesize(frame->pc);
509 if (framesize == -1)
510 frame->frame = read_register (FP_REGNUM);
511 else
512 frame->frame = read_register (SP_REGNUM) - framesize;
513
514 if (!frameless_function_invocation (frame)) /* Frameless? */
515 return; /* No, quit now */
516
517 /* For frameless functions, we need to look at the caller's frame */
518 framesize = find_proc_framesize(FRAME_SAVED_PC(frame));
519 if (framesize != -1)
520 frame->frame -= framesize;
521 }
522 \f
523 /* Given a GDB frame, determine the address of the calling function's frame.
524 This will be used to create a new GDB frame struct, and then
525 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
526
527 This may involve searching through prologues for several functions
528 at boundaries where GCC calls HP C code, or where code which has
529 a frame pointer calls code without a frame pointer. */
530
531
532 FRAME_ADDR
533 frame_chain (frame)
534 struct frame_info *frame;
535 {
536 int my_framesize, caller_framesize;
537 struct unwind_table_entry *u;
538
539 /* Get frame sizes for the current frame and the frame of the
540 caller. */
541 my_framesize = find_proc_framesize (frame->pc);
542 caller_framesize = find_proc_framesize (FRAME_SAVED_PC(frame));
543
544 /* If caller does not have a frame pointer, then its frame
545 can be found at current_frame - caller_framesize. */
546 if (caller_framesize != -1)
547 return frame->frame - caller_framesize;
548
549 /* Both caller and callee have frame pointers and are GCC compiled
550 (SAVE_SP bit in unwind descriptor is on for both functions.
551 The previous frame pointer is found at the top of the current frame. */
552 if (caller_framesize == -1 && my_framesize == -1)
553 return read_memory_integer (frame->frame, 4);
554
555 /* Caller has a frame pointer, but callee does not. This is a little
556 more difficult as GCC and HP C lay out locals and callee register save
557 areas very differently.
558
559 The previous frame pointer could be in a register, or in one of
560 several areas on the stack.
561
562 Walk from the current frame to the innermost frame examining
563 unwind descriptors to determine if %r4 ever gets saved into the
564 stack. If so return whatever value got saved into the stack.
565 If it was never saved in the stack, then the value in %r4 is still
566 valid, so use it.
567
568 We use information from unwind descriptors to determine if %r4
569 is saved into the stack (Entry_GR field has this information). */
570
571 while (frame)
572 {
573 u = find_unwind_entry (frame->pc);
574
575 if (!u)
576 {
577 /* We could find this information by examining prologues. This
578 is necessary to deal with stripped executables. */
579 warning ("Unable to find unwind for PC 0x%x -- Help!", frame->pc);
580 return 0;
581 }
582
583 /* Entry_GR specifies the number of callee-saved general registers
584 saved in the stack. It starts at %r3, so %r4 would be 2. */
585 if (u->Entry_GR >= 2 || u->Save_SP)
586 break;
587 else
588 frame = frame->next;
589 }
590
591 if (frame)
592 {
593 /* We may have walked down the chain into a function with a frame
594 pointer. */
595 if (u->Save_SP)
596 return read_memory_integer (frame->frame, 4);
597 /* %r4 was saved somewhere in the stack. Dig it out. */
598 else
599 return dig_fp_from_stack (frame, u);
600 }
601 else
602 {
603 /* The value in %r4 was never saved into the stack (thus %r4 still
604 holds the value of the previous frame pointer). */
605 return read_register (4);
606 }
607 }
608
609 /* Given a frame and an unwind descriptor return the value for %fr (aka fp)
610 which was saved into the stack. FIXME: Why can't we just use the standard
611 saved_regs stuff? */
612
613 static FRAME_ADDR
614 dig_fp_from_stack (frame, u)
615 FRAME frame;
616 struct unwind_table_entry *u;
617 {
618 CORE_ADDR pc = u->region_start;
619
620 /* Search the function for the save of %r4. */
621 while (pc != u->region_end)
622 {
623 char buf[4];
624 unsigned long inst;
625 int status;
626
627 /* We need only look for the standard stw %r4,X(%sp) instruction,
628 the other variants (eg stwm) are only used on the first register
629 save (eg %r3). */
630 status = target_read_memory (pc, buf, 4);
631 inst = extract_unsigned_integer (buf, 4);
632
633 if (status != 0)
634 memory_error (status, pc);
635
636 /* Check for stw %r4,X(%sp). */
637 if ((inst & 0xffffc000) == 0x6bc40000)
638 {
639 /* Found the instruction which saves %r4. The offset (relative
640 to this frame) is framesize + immed14 (derived from the
641 store instruction). */
642 int offset = (u->Total_frame_size << 3) + extract_14 (inst);
643
644 return read_memory_integer (frame->frame + offset, 4);
645 }
646
647 /* Keep looking. */
648 pc += 4;
649 }
650
651 warning ("Unable to find %%r4 in stack.\n");
652 return 0;
653 }
654
655 \f
656 /* To see if a frame chain is valid, see if the caller looks like it
657 was compiled with gcc. */
658
659 int
660 frame_chain_valid (chain, thisframe)
661 FRAME_ADDR chain;
662 FRAME thisframe;
663 {
664 struct minimal_symbol *msym_us;
665 struct minimal_symbol *msym_start;
666 struct unwind_table_entry *u;
667
668 if (!chain)
669 return 0;
670
671 u = find_unwind_entry (thisframe->pc);
672
673 /* We can't just check that the same of msym_us is "_start", because
674 someone idiotically decided that they were going to make a Ltext_end
675 symbol with the same address. This Ltext_end symbol is totally
676 indistinguishable (as nearly as I can tell) from the symbol for a function
677 which is (legitimately, since it is in the user's namespace)
678 named Ltext_end, so we can't just ignore it. */
679 msym_us = lookup_minimal_symbol_by_pc (FRAME_SAVED_PC (thisframe));
680 msym_start = lookup_minimal_symbol ("_start", NULL);
681 if (msym_us
682 && msym_start
683 && SYMBOL_VALUE_ADDRESS (msym_us) == SYMBOL_VALUE_ADDRESS (msym_start))
684 return 0;
685
686 if (u == NULL)
687 return 1;
688
689 if (u->Save_SP || u->Total_frame_size)
690 return 1;
691
692 if (pc_in_linker_stub (thisframe->pc))
693 return 1;
694
695 return 0;
696 }
697
698 /*
699 * These functions deal with saving and restoring register state
700 * around a function call in the inferior. They keep the stack
701 * double-word aligned; eventually, on an hp700, the stack will have
702 * to be aligned to a 64-byte boundary.
703 */
704
705 int
706 push_dummy_frame ()
707 {
708 register CORE_ADDR sp;
709 register int regnum;
710 int int_buffer;
711 double freg_buffer;
712
713 /* Space for "arguments"; the RP goes in here. */
714 sp = read_register (SP_REGNUM) + 48;
715 int_buffer = read_register (RP_REGNUM) | 0x3;
716 write_memory (sp - 20, (char *)&int_buffer, 4);
717
718 int_buffer = read_register (FP_REGNUM);
719 write_memory (sp, (char *)&int_buffer, 4);
720
721 write_register (FP_REGNUM, sp);
722
723 sp += 8;
724
725 for (regnum = 1; regnum < 32; regnum++)
726 if (regnum != RP_REGNUM && regnum != FP_REGNUM)
727 sp = push_word (sp, read_register (regnum));
728
729 sp += 4;
730
731 for (regnum = FP0_REGNUM; regnum < NUM_REGS; regnum++)
732 {
733 read_register_bytes (REGISTER_BYTE (regnum), (char *)&freg_buffer, 8);
734 sp = push_bytes (sp, (char *)&freg_buffer, 8);
735 }
736 sp = push_word (sp, read_register (IPSW_REGNUM));
737 sp = push_word (sp, read_register (SAR_REGNUM));
738 sp = push_word (sp, read_register (PCOQ_HEAD_REGNUM));
739 sp = push_word (sp, read_register (PCSQ_HEAD_REGNUM));
740 sp = push_word (sp, read_register (PCOQ_TAIL_REGNUM));
741 sp = push_word (sp, read_register (PCSQ_TAIL_REGNUM));
742 write_register (SP_REGNUM, sp);
743 }
744
745 find_dummy_frame_regs (frame, frame_saved_regs)
746 struct frame_info *frame;
747 struct frame_saved_regs *frame_saved_regs;
748 {
749 CORE_ADDR fp = frame->frame;
750 int i;
751
752 frame_saved_regs->regs[RP_REGNUM] = fp - 20 & ~0x3;
753 frame_saved_regs->regs[FP_REGNUM] = fp;
754 frame_saved_regs->regs[1] = fp + 8;
755
756 for (fp += 12, i = 3; i < 32; i++)
757 {
758 if (i != FP_REGNUM)
759 {
760 frame_saved_regs->regs[i] = fp;
761 fp += 4;
762 }
763 }
764
765 fp += 4;
766 for (i = FP0_REGNUM; i < NUM_REGS; i++, fp += 8)
767 frame_saved_regs->regs[i] = fp;
768
769 frame_saved_regs->regs[IPSW_REGNUM] = fp;
770 frame_saved_regs->regs[SAR_REGNUM] = fp + 4;
771 frame_saved_regs->regs[PCOQ_HEAD_REGNUM] = fp + 8;
772 frame_saved_regs->regs[PCSQ_HEAD_REGNUM] = fp + 12;
773 frame_saved_regs->regs[PCOQ_TAIL_REGNUM] = fp + 16;
774 frame_saved_regs->regs[PCSQ_TAIL_REGNUM] = fp + 20;
775 }
776
777 int
778 hppa_pop_frame ()
779 {
780 register FRAME frame = get_current_frame ();
781 register CORE_ADDR fp;
782 register int regnum;
783 struct frame_saved_regs fsr;
784 struct frame_info *fi;
785 double freg_buffer;
786
787 fi = get_frame_info (frame);
788 fp = fi->frame;
789 get_frame_saved_regs (fi, &fsr);
790
791 if (fsr.regs[IPSW_REGNUM]) /* Restoring a call dummy frame */
792 restore_pc_queue (&fsr);
793
794 for (regnum = 31; regnum > 0; regnum--)
795 if (fsr.regs[regnum])
796 write_register (regnum, read_memory_integer (fsr.regs[regnum], 4));
797
798 for (regnum = NUM_REGS - 1; regnum >= FP0_REGNUM ; regnum--)
799 if (fsr.regs[regnum])
800 {
801 read_memory (fsr.regs[regnum], (char *)&freg_buffer, 8);
802 write_register_bytes (REGISTER_BYTE (regnum), (char *)&freg_buffer, 8);
803 }
804
805 if (fsr.regs[IPSW_REGNUM])
806 write_register (IPSW_REGNUM,
807 read_memory_integer (fsr.regs[IPSW_REGNUM], 4));
808
809 if (fsr.regs[SAR_REGNUM])
810 write_register (SAR_REGNUM,
811 read_memory_integer (fsr.regs[SAR_REGNUM], 4));
812
813 /* If the PC was explicitly saved, then just restore it. */
814 if (fsr.regs[PCOQ_TAIL_REGNUM])
815 write_register (PCOQ_TAIL_REGNUM,
816 read_memory_integer (fsr.regs[PCOQ_TAIL_REGNUM], 4));
817
818 /* Else use the value in %rp to set the new PC. */
819 else
820 target_write_pc (read_register (RP_REGNUM));
821
822 write_register (FP_REGNUM, read_memory_integer (fp, 4));
823
824 if (fsr.regs[IPSW_REGNUM]) /* call dummy */
825 write_register (SP_REGNUM, fp - 48);
826 else
827 write_register (SP_REGNUM, fp);
828
829 flush_cached_frames ();
830 set_current_frame (create_new_frame (read_register (FP_REGNUM),
831 read_pc ()));
832 }
833
834 /*
835 * After returning to a dummy on the stack, restore the instruction
836 * queue space registers. */
837
838 static int
839 restore_pc_queue (fsr)
840 struct frame_saved_regs *fsr;
841 {
842 CORE_ADDR pc = read_pc ();
843 CORE_ADDR new_pc = read_memory_integer (fsr->regs[PCOQ_HEAD_REGNUM], 4);
844 int pid;
845 WAITTYPE w;
846 int insn_count;
847
848 /* Advance past break instruction in the call dummy. */
849 write_register (PCOQ_HEAD_REGNUM, pc + 4);
850 write_register (PCOQ_TAIL_REGNUM, pc + 8);
851
852 /*
853 * HPUX doesn't let us set the space registers or the space
854 * registers of the PC queue through ptrace. Boo, hiss.
855 * Conveniently, the call dummy has this sequence of instructions
856 * after the break:
857 * mtsp r21, sr0
858 * ble,n 0(sr0, r22)
859 *
860 * So, load up the registers and single step until we are in the
861 * right place.
862 */
863
864 write_register (21, read_memory_integer (fsr->regs[PCSQ_HEAD_REGNUM], 4));
865 write_register (22, new_pc);
866
867 for (insn_count = 0; insn_count < 3; insn_count++)
868 {
869 resume (1, 0);
870 target_wait(&w);
871
872 if (!WIFSTOPPED (w))
873 {
874 stop_signal = WTERMSIG (w);
875 terminal_ours_for_output ();
876 printf ("\nProgram terminated with signal %d, %s\n",
877 stop_signal, safe_strsignal (stop_signal));
878 fflush (stdout);
879 return 0;
880 }
881 }
882 fetch_inferior_registers (-1);
883 return 1;
884 }
885
886 CORE_ADDR
887 hppa_push_arguments (nargs, args, sp, struct_return, struct_addr)
888 int nargs;
889 value *args;
890 CORE_ADDR sp;
891 int struct_return;
892 CORE_ADDR struct_addr;
893 {
894 /* array of arguments' offsets */
895 int *offset = (int *)alloca(nargs * sizeof (int));
896 int cum = 0;
897 int i, alignment;
898
899 for (i = 0; i < nargs; i++)
900 {
901 /* Coerce chars to int & float to double if necessary */
902 args[i] = value_arg_coerce (args[i]);
903
904 cum += TYPE_LENGTH (VALUE_TYPE (args[i]));
905
906 /* value must go at proper alignment. Assume alignment is a
907 power of two.*/
908 alignment = hppa_alignof (VALUE_TYPE (args[i]));
909 if (cum % alignment)
910 cum = (cum + alignment) & -alignment;
911 offset[i] = -cum;
912 }
913 sp += max ((cum + 7) & -8, 16);
914
915 for (i = 0; i < nargs; i++)
916 write_memory (sp + offset[i], VALUE_CONTENTS (args[i]),
917 TYPE_LENGTH (VALUE_TYPE (args[i])));
918
919 if (struct_return)
920 write_register (28, struct_addr);
921 return sp + 32;
922 }
923
924 /*
925 * Insert the specified number of args and function address
926 * into a call sequence of the above form stored at DUMMYNAME.
927 *
928 * On the hppa we need to call the stack dummy through $$dyncall.
929 * Therefore our version of FIX_CALL_DUMMY takes an extra argument,
930 * real_pc, which is the location where gdb should start up the
931 * inferior to do the function call.
932 */
933
934 CORE_ADDR
935 hppa_fix_call_dummy (dummy, pc, fun, nargs, args, type, gcc_p)
936 REGISTER_TYPE *dummy;
937 CORE_ADDR pc;
938 CORE_ADDR fun;
939 int nargs;
940 value *args;
941 struct type *type;
942 int gcc_p;
943 {
944 CORE_ADDR dyncall_addr, sr4export_addr;
945 struct minimal_symbol *msymbol;
946 int flags = read_register (FLAGS_REGNUM);
947
948 msymbol = lookup_minimal_symbol ("$$dyncall", (struct objfile *) NULL);
949 if (msymbol == NULL)
950 error ("Can't find an address for $$dyncall trampoline");
951
952 dyncall_addr = SYMBOL_VALUE_ADDRESS (msymbol);
953
954 msymbol = lookup_minimal_symbol ("_sr4export", (struct objfile *) NULL);
955 if (msymbol == NULL)
956 error ("Can't find an address for _sr4export trampoline");
957
958 sr4export_addr = SYMBOL_VALUE_ADDRESS (msymbol);
959
960 dummy[9] = deposit_21 (fun >> 11, dummy[9]);
961 dummy[10] = deposit_14 (fun & MASK_11, dummy[10]);
962 dummy[12] = deposit_21 (sr4export_addr >> 11, dummy[12]);
963 dummy[13] = deposit_14 (sr4export_addr & MASK_11, dummy[13]);
964
965 write_register (22, pc);
966
967 /* If we are in a syscall, then we should call the stack dummy
968 directly. $$dyncall is not needed as the kernel sets up the
969 space id registers properly based on the value in %r31. In
970 fact calling $$dyncall will not work because the value in %r22
971 will be clobbered on the syscall exit path. */
972 if (flags & 2)
973 return pc;
974 else
975 return dyncall_addr;
976
977 }
978
979 /* Get the PC from %r31 if currently in a syscall. Also mask out privilege
980 bits. */
981 CORE_ADDR
982 target_read_pc ()
983 {
984 int flags = read_register (FLAGS_REGNUM);
985
986 if (flags & 2)
987 return read_register (31) & ~0x3;
988 return read_register (PC_REGNUM) & ~0x3;
989 }
990
991 /* Write out the PC. If currently in a syscall, then also write the new
992 PC value into %r31. */
993 void
994 target_write_pc (v)
995 CORE_ADDR v;
996 {
997 int flags = read_register (FLAGS_REGNUM);
998
999 /* If in a syscall, then set %r31. Also make sure to get the
1000 privilege bits set correctly. */
1001 if (flags & 2)
1002 write_register (31, (long) (v | 0x3));
1003
1004 write_register (PC_REGNUM, (long) v);
1005 write_register (NPC_REGNUM, (long) v + 4);
1006 }
1007
1008 /* return the alignment of a type in bytes. Structures have the maximum
1009 alignment required by their fields. */
1010
1011 static int
1012 hppa_alignof (arg)
1013 struct type *arg;
1014 {
1015 int max_align, align, i;
1016 switch (TYPE_CODE (arg))
1017 {
1018 case TYPE_CODE_PTR:
1019 case TYPE_CODE_INT:
1020 case TYPE_CODE_FLT:
1021 return TYPE_LENGTH (arg);
1022 case TYPE_CODE_ARRAY:
1023 return hppa_alignof (TYPE_FIELD_TYPE (arg, 0));
1024 case TYPE_CODE_STRUCT:
1025 case TYPE_CODE_UNION:
1026 max_align = 2;
1027 for (i = 0; i < TYPE_NFIELDS (arg); i++)
1028 {
1029 /* Bit fields have no real alignment. */
1030 if (!TYPE_FIELD_BITPOS (arg, i))
1031 {
1032 align = hppa_alignof (TYPE_FIELD_TYPE (arg, i));
1033 max_align = max (max_align, align);
1034 }
1035 }
1036 return max_align;
1037 default:
1038 return 4;
1039 }
1040 }
1041
1042 /* Print the register regnum, or all registers if regnum is -1 */
1043
1044 pa_do_registers_info (regnum, fpregs)
1045 int regnum;
1046 int fpregs;
1047 {
1048 char raw_regs [REGISTER_BYTES];
1049 int i;
1050
1051 for (i = 0; i < NUM_REGS; i++)
1052 read_relative_register_raw_bytes (i, raw_regs + REGISTER_BYTE (i));
1053 if (regnum == -1)
1054 pa_print_registers (raw_regs, regnum, fpregs);
1055 else if (regnum < FP0_REGNUM)
1056 printf ("%s %x\n", reg_names[regnum], *(long *)(raw_regs +
1057 REGISTER_BYTE (regnum)));
1058 else
1059 pa_print_fp_reg (regnum);
1060 }
1061
1062 pa_print_registers (raw_regs, regnum, fpregs)
1063 char *raw_regs;
1064 int regnum;
1065 int fpregs;
1066 {
1067 int i;
1068
1069 for (i = 0; i < 18; i++)
1070 printf ("%8.8s: %8x %8.8s: %8x %8.8s: %8x %8.8s: %8x\n",
1071 reg_names[i],
1072 *(int *)(raw_regs + REGISTER_BYTE (i)),
1073 reg_names[i + 18],
1074 *(int *)(raw_regs + REGISTER_BYTE (i + 18)),
1075 reg_names[i + 36],
1076 *(int *)(raw_regs + REGISTER_BYTE (i + 36)),
1077 reg_names[i + 54],
1078 *(int *)(raw_regs + REGISTER_BYTE (i + 54)));
1079
1080 if (fpregs)
1081 for (i = 72; i < NUM_REGS; i++)
1082 pa_print_fp_reg (i);
1083 }
1084
1085 pa_print_fp_reg (i)
1086 int i;
1087 {
1088 unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
1089 unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
1090 REGISTER_TYPE val;
1091
1092 /* Get the data in raw format, then convert also to virtual format. */
1093 read_relative_register_raw_bytes (i, raw_buffer);
1094 REGISTER_CONVERT_TO_VIRTUAL (i, raw_buffer, virtual_buffer);
1095
1096 fputs_filtered (reg_names[i], stdout);
1097 print_spaces_filtered (15 - strlen (reg_names[i]), stdout);
1098
1099 val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, stdout, 0,
1100 1, 0, Val_pretty_default);
1101 printf_filtered ("\n");
1102 }
1103
1104 /* Function calls that pass into a new compilation unit must pass through a
1105 small piece of code that does long format (`external' in HPPA parlance)
1106 jumps. We figure out where the trampoline is going to end up, and return
1107 the PC of the final destination. If we aren't in a trampoline, we just
1108 return NULL.
1109
1110 For computed calls, we just extract the new PC from r22. */
1111
1112 CORE_ADDR
1113 skip_trampoline_code (pc, name)
1114 CORE_ADDR pc;
1115 char *name;
1116 {
1117 long inst0, inst1;
1118 static CORE_ADDR dyncall = 0;
1119 struct minimal_symbol *msym;
1120
1121 /* FIXME XXX - dyncall must be initialized whenever we get a new exec file */
1122
1123 if (!dyncall)
1124 {
1125 msym = lookup_minimal_symbol ("$$dyncall", NULL);
1126 if (msym)
1127 dyncall = SYMBOL_VALUE_ADDRESS (msym);
1128 else
1129 dyncall = -1;
1130 }
1131
1132 if (pc == dyncall)
1133 return (CORE_ADDR)(read_register (22) & ~0x3);
1134
1135 inst0 = read_memory_integer (pc, 4);
1136 inst1 = read_memory_integer (pc+4, 4);
1137
1138 if ( (inst0 & 0xffe00000) == 0x20200000 /* ldil xxx, r1 */
1139 && (inst1 & 0xffe0e002) == 0xe0202002) /* be,n yyy(sr4, r1) */
1140 pc = extract_21 (inst0) + extract_17 (inst1);
1141 else
1142 pc = (CORE_ADDR)NULL;
1143
1144 return pc;
1145 }
1146
1147 /* Advance PC across any function entry prologue instructions
1148 to reach some "real" code. */
1149
1150 /* skip (stw rp, -20(0,sp)); copy 4,1; copy sp, 4; stwm 1,framesize(sp)
1151 for gcc, or (stw rp, -20(0,sp); stwm 1, framesize(sp) for hcc */
1152
1153 CORE_ADDR
1154 skip_prologue(pc)
1155 CORE_ADDR pc;
1156 {
1157 char buf[4];
1158 unsigned long inst;
1159 int status;
1160
1161 status = target_read_memory (pc, buf, 4);
1162 inst = extract_unsigned_integer (buf, 4);
1163 if (status != 0)
1164 return pc;
1165
1166 if (inst == 0x6BC23FD9) /* stw rp,-20(sp) */
1167 {
1168 if (read_memory_integer (pc + 4, 4) == 0x8040241) /* copy r4,r1 */
1169 pc += 16;
1170 else if ((read_memory_integer (pc + 4, 4) & ~MASK_14) == 0x68810000) /* stw r1,(r4) */
1171 pc += 8;
1172 }
1173 else if (read_memory_integer (pc, 4) == 0x8040241) /* copy r4,r1 */
1174 pc += 12;
1175 else if ((read_memory_integer (pc, 4) & ~MASK_14) == 0x68810000) /* stw r1,(r4) */
1176 pc += 4;
1177
1178 return pc;
1179 }
1180
1181 #ifdef MAINTENANCE_CMDS
1182
1183 static void
1184 unwind_command (exp, from_tty)
1185 char *exp;
1186 int from_tty;
1187 {
1188 CORE_ADDR address;
1189 union
1190 {
1191 int *foo;
1192 struct unwind_table_entry *u;
1193 } xxx;
1194
1195 /* If we have an expression, evaluate it and use it as the address. */
1196
1197 if (exp != 0 && *exp != 0)
1198 address = parse_and_eval_address (exp);
1199 else
1200 return;
1201
1202 xxx.u = find_unwind_entry (address);
1203
1204 if (!xxx.u)
1205 {
1206 printf ("Can't find unwind table entry for PC 0x%x\n", address);
1207 return;
1208 }
1209
1210 printf ("%08x\n%08X\n%08X\n%08X\n", xxx.foo[0], xxx.foo[1], xxx.foo[2],
1211 xxx.foo[3]);
1212 }
1213
1214 void
1215 _initialize_hppa_tdep ()
1216 {
1217 add_cmd ("unwind", class_maintenance, unwind_command,
1218 "Print unwind table entry at given address.",
1219 &maintenanceprintlist);
1220 }
1221
1222 #endif /* MAINTENANCE_CMDS */
This page took 0.054023 seconds and 5 git commands to generate.