* Fix for PR 18665, from sky branch.
[binutils-gdb.git] / gdb / rs6000-tdep.c
blobd851c77dc8ca32238b96113b0f5d2bad9f85f738
1 /* Target-dependent code for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "defs.h"
22 #include "frame.h"
23 #include "inferior.h"
24 #include "symtab.h"
25 #include "target.h"
26 #include "gdbcore.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "xcoffsolib.h"
31 extern int errno;
33 /* Breakpoint shadows for the single step instructions will be kept here. */
35 static struct sstep_breaks {
36 /* Address, or 0 if this is not in use. */
37 CORE_ADDR address;
38 /* Shadow contents. */
39 char data[4];
40 } stepBreaks[2];
42 /* Hook for determining the TOC address when calling functions in the
43 inferior under AIX. The initialization code in rs6000-nat.c sets
44 this hook to point to find_toc_address. */
46 CORE_ADDR (*find_toc_address_hook) PARAMS ((CORE_ADDR)) = NULL;
48 /* Static function prototypes */
50 static CORE_ADDR branch_dest PARAMS ((int opcode, int instr, CORE_ADDR pc,
51 CORE_ADDR safety));
53 static void frame_get_saved_regs PARAMS ((struct frame_info *fi,
54 struct rs6000_framedata *fdatap));
56 static void pop_dummy_frame PARAMS ((void));
58 static CORE_ADDR frame_initial_stack_address PARAMS ((struct frame_info *));
60 /* Fill in fi->saved_regs */
62 struct frame_extra_info
64 /* Functions calling alloca() change the value of the stack
65 pointer. We need to use initial stack pointer (which is saved in
66 r31 by gcc) in such cases. If a compiler emits traceback table,
67 then we should use the alloca register specified in traceback
68 table. FIXME. */
69 CORE_ADDR initial_sp; /* initial stack pointer. */ \
72 void
73 rs6000_init_extra_frame_info (fromleaf, fi)
74 int fromleaf;
75 struct frame_info *fi;
77 fi->extra_info = (struct frame_extra_info*)
78 frame_obstack_alloc (sizeof (struct frame_extra_info));
79 fi->extra_info->initial_sp = 0;
80 if (fi->next != (CORE_ADDR) 0
81 && fi->pc < TEXT_SEGMENT_BASE)
82 /* We're in get_prev_frame_info */
83 /* and this is a special signal frame. */
84 /* (fi->pc will be some low address in the kernel, */
85 /* to which the signal handler returns). */
86 fi->signal_handler_caller = 1;
90 void
91 rs6000_frame_init_saved_regs (fi)
92 struct frame_info *fi;
94 frame_get_saved_regs (fi, NULL);
97 CORE_ADDR
98 rs6000_frame_args_address (fi)
99 struct frame_info *fi;
101 if (fi->extra_info->initial_sp != 0)
102 return fi->extra_info->initial_sp;
103 else
104 return frame_initial_stack_address (fi);
108 /* Calculate the destination of a branch/jump. Return -1 if not a branch. */
110 static CORE_ADDR
111 branch_dest (opcode, instr, pc, safety)
112 int opcode;
113 int instr;
114 CORE_ADDR pc;
115 CORE_ADDR safety;
117 CORE_ADDR dest;
118 int immediate;
119 int absolute;
120 int ext_op;
122 absolute = (int) ((instr >> 1) & 1);
124 switch (opcode) {
125 case 18 :
126 immediate = ((instr & ~3) << 6) >> 6; /* br unconditional */
127 if (absolute)
128 dest = immediate;
129 else
130 dest = pc + immediate;
131 break;
133 case 16 :
134 immediate = ((instr & ~3) << 16) >> 16; /* br conditional */
135 if (absolute)
136 dest = immediate;
137 else
138 dest = pc + immediate;
139 break;
141 case 19 :
142 ext_op = (instr>>1) & 0x3ff;
144 if (ext_op == 16) /* br conditional register */
146 dest = read_register (LR_REGNUM) & ~3;
148 /* If we are about to return from a signal handler, dest is
149 something like 0x3c90. The current frame is a signal handler
150 caller frame, upon completion of the sigreturn system call
151 execution will return to the saved PC in the frame. */
152 if (dest < TEXT_SEGMENT_BASE)
154 struct frame_info *fi;
156 fi = get_current_frame ();
157 if (fi != NULL)
158 dest = read_memory_integer (fi->frame + SIG_FRAME_PC_OFFSET,
163 else if (ext_op == 528) /* br cond to count reg */
165 dest = read_register (CTR_REGNUM) & ~3;
167 /* If we are about to execute a system call, dest is something
168 like 0x22fc or 0x3b00. Upon completion the system call
169 will return to the address in the link register. */
170 if (dest < TEXT_SEGMENT_BASE)
171 dest = read_register (LR_REGNUM) & ~3;
173 else return -1;
174 break;
176 default: return -1;
178 return (dest < TEXT_SEGMENT_BASE) ? safety : dest;
182 /* Sequence of bytes for breakpoint instruction. */
184 #define BIG_BREAKPOINT { 0x7d, 0x82, 0x10, 0x08 }
185 #define LITTLE_BREAKPOINT { 0x08, 0x10, 0x82, 0x7d }
187 unsigned char *
188 rs6000_breakpoint_from_pc (bp_addr, bp_size)
189 CORE_ADDR *bp_addr;
190 int *bp_size;
192 static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
193 static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
194 *bp_size = 4;
195 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
196 return big_breakpoint;
197 else
198 return little_breakpoint;
202 /* AIX does not support PT_STEP. Simulate it. */
204 void
205 rs6000_software_single_step (signal, insert_breakpoints_p)
206 enum target_signal signal;
207 int insert_breakpoints_p;
209 #define INSNLEN(OPCODE) 4
211 static char le_breakp[] = LITTLE_BREAKPOINT;
212 static char be_breakp[] = BIG_BREAKPOINT;
213 char *breakp = TARGET_BYTE_ORDER == BIG_ENDIAN ? be_breakp : le_breakp;
214 int ii, insn;
215 CORE_ADDR loc;
216 CORE_ADDR breaks[2];
217 int opcode;
219 if (insert_breakpoints_p) {
221 loc = read_pc ();
223 insn = read_memory_integer (loc, 4);
225 breaks[0] = loc + INSNLEN(insn);
226 opcode = insn >> 26;
227 breaks[1] = branch_dest (opcode, insn, loc, breaks[0]);
229 /* Don't put two breakpoints on the same address. */
230 if (breaks[1] == breaks[0])
231 breaks[1] = -1;
233 stepBreaks[1].address = 0;
235 for (ii=0; ii < 2; ++ii) {
237 /* ignore invalid breakpoint. */
238 if ( breaks[ii] == -1)
239 continue;
241 read_memory (breaks[ii], stepBreaks[ii].data, 4);
243 write_memory (breaks[ii], breakp, 4);
244 stepBreaks[ii].address = breaks[ii];
247 } else {
249 /* remove step breakpoints. */
250 for (ii=0; ii < 2; ++ii)
251 if (stepBreaks[ii].address != 0)
252 write_memory
253 (stepBreaks[ii].address, stepBreaks[ii].data, 4);
256 errno = 0; /* FIXME, don't ignore errors! */
257 /* What errors? {read,write}_memory call error(). */
261 /* return pc value after skipping a function prologue and also return
262 information about a function frame.
264 in struct rs6000_framedata fdata:
265 - frameless is TRUE, if function does not have a frame.
266 - nosavedpc is TRUE, if function does not save %pc value in its frame.
267 - offset is the initial size of this stack frame --- the amount by
268 which we decrement the sp to allocate the frame.
269 - saved_gpr is the number of the first saved gpr.
270 - saved_fpr is the number of the first saved fpr.
271 - alloca_reg is the number of the register used for alloca() handling.
272 Otherwise -1.
273 - gpr_offset is the offset of the first saved gpr from the previous frame.
274 - fpr_offset is the offset of the first saved fpr from the previous frame.
275 - lr_offset is the offset of the saved lr
276 - cr_offset is the offset of the saved cr
279 #define SIGNED_SHORT(x) \
280 ((sizeof (short) == 2) \
281 ? ((int)(short)(x)) \
282 : ((int)((((x) & 0xffff) ^ 0x8000) - 0x8000)))
284 #define GET_SRC_REG(x) (((x) >> 21) & 0x1f)
286 CORE_ADDR
287 skip_prologue (pc, fdata)
288 CORE_ADDR pc;
289 struct rs6000_framedata *fdata;
291 CORE_ADDR orig_pc = pc;
292 char buf[4];
293 unsigned long op;
294 long offset = 0;
295 int lr_reg = 0;
296 int cr_reg = 0;
297 int reg;
298 int framep = 0;
299 int minimal_toc_loaded = 0;
300 static struct rs6000_framedata zero_frame;
302 *fdata = zero_frame;
303 fdata->saved_gpr = -1;
304 fdata->saved_fpr = -1;
305 fdata->alloca_reg = -1;
306 fdata->frameless = 1;
307 fdata->nosavedpc = 1;
309 if (target_read_memory (pc, buf, 4))
310 return pc; /* Can't access it -- assume no prologue. */
312 /* Assume that subsequent fetches can fail with low probability. */
313 pc -= 4;
314 for (;;)
316 pc += 4;
317 op = read_memory_integer (pc, 4);
319 if ((op & 0xfc1fffff) == 0x7c0802a6) { /* mflr Rx */
320 lr_reg = (op & 0x03e00000) | 0x90010000;
321 continue;
323 } else if ((op & 0xfc1fffff) == 0x7c000026) { /* mfcr Rx */
324 cr_reg = (op & 0x03e00000) | 0x90010000;
325 continue;
327 } else if ((op & 0xfc1f0000) == 0xd8010000) { /* stfd Rx,NUM(r1) */
328 reg = GET_SRC_REG (op);
329 if (fdata->saved_fpr == -1 || fdata->saved_fpr > reg) {
330 fdata->saved_fpr = reg;
331 fdata->fpr_offset = SIGNED_SHORT (op) + offset;
333 continue;
335 } else if (((op & 0xfc1f0000) == 0xbc010000) || /* stm Rx, NUM(r1) */
336 ((op & 0xfc1f0000) == 0x90010000 && /* st rx,NUM(r1),
337 rx >= r13 */
338 (op & 0x03e00000) >= 0x01a00000)) {
340 reg = GET_SRC_REG (op);
341 if (fdata->saved_gpr == -1 || fdata->saved_gpr > reg) {
342 fdata->saved_gpr = reg;
343 fdata->gpr_offset = SIGNED_SHORT (op) + offset;
345 continue;
347 } else if ((op & 0xffff0000) == 0x3c000000) { /* addis 0,0,NUM, used
348 for >= 32k frames */
349 fdata->offset = (op & 0x0000ffff) << 16;
350 fdata->frameless = 0;
351 continue;
353 } else if ((op & 0xffff0000) == 0x60000000) { /* ori 0,0,NUM, 2nd ha
354 lf of >= 32k frames */
355 fdata->offset |= (op & 0x0000ffff);
356 fdata->frameless = 0;
357 continue;
359 } else if ((op & 0xffff0000) == lr_reg) { /* st Rx,NUM(r1)
360 where Rx == lr */
361 fdata->lr_offset = SIGNED_SHORT (op) + offset;
362 fdata->nosavedpc = 0;
363 lr_reg = 0;
364 continue;
366 } else if ((op & 0xffff0000) == cr_reg) { /* st Rx,NUM(r1)
367 where Rx == cr */
368 fdata->cr_offset = SIGNED_SHORT (op) + offset;
369 cr_reg = 0;
370 continue;
372 } else if (op == 0x48000005) { /* bl .+4 used in
373 -mrelocatable */
374 continue;
376 } else if (op == 0x48000004) { /* b .+4 (xlc) */
377 break;
379 } else if (((op & 0xffff0000) == 0x801e0000 || /* lwz 0,NUM(r30), used
380 in V.4 -mrelocatable */
381 op == 0x7fc0f214) && /* add r30,r0,r30, used
382 in V.4 -mrelocatable */
383 lr_reg == 0x901e0000) {
384 continue;
386 } else if ((op & 0xffff0000) == 0x3fc00000 || /* addis 30,0,foo@ha, used
387 in V.4 -mminimal-toc */
388 (op & 0xffff0000) == 0x3bde0000) { /* addi 30,30,foo@l */
389 continue;
391 } else if ((op & 0xfc000000) == 0x48000000) { /* bl foo,
392 to save fprs??? */
394 fdata->frameless = 0;
395 /* Don't skip over the subroutine call if it is not within the first
396 three instructions of the prologue. */
397 if ((pc - orig_pc) > 8)
398 break;
400 op = read_memory_integer (pc+4, 4);
402 /* At this point, make sure this is not a trampoline function
403 (a function that simply calls another functions, and nothing else).
404 If the next is not a nop, this branch was part of the function
405 prologue. */
407 if (op == 0x4def7b82 || op == 0) /* crorc 15, 15, 15 */
408 break; /* don't skip over
409 this branch */
410 continue;
412 /* update stack pointer */
413 } else if ((op & 0xffff0000) == 0x94210000) { /* stu r1,NUM(r1) */
414 fdata->frameless = 0;
415 fdata->offset = SIGNED_SHORT (op);
416 offset = fdata->offset;
417 continue;
419 } else if (op == 0x7c21016e) { /* stwux 1,1,0 */
420 fdata->frameless = 0;
421 offset = fdata->offset;
422 continue;
424 /* Load up minimal toc pointer */
425 } else if ((op >> 22) == 0x20f
426 && ! minimal_toc_loaded) { /* l r31,... or l r30,... */
427 minimal_toc_loaded = 1;
428 continue;
430 /* store parameters in stack */
431 } else if ((op & 0xfc1f0000) == 0x90010000 || /* st rx,NUM(r1) */
432 (op & 0xfc1f0000) == 0xd8010000 || /* stfd Rx,NUM(r1) */
433 (op & 0xfc1f0000) == 0xfc010000) { /* frsp, fp?,NUM(r1) */
434 continue;
436 /* store parameters in stack via frame pointer */
437 } else if (framep &&
438 ((op & 0xfc1f0000) == 0x901f0000 || /* st rx,NUM(r1) */
439 (op & 0xfc1f0000) == 0xd81f0000 || /* stfd Rx,NUM(r1) */
440 (op & 0xfc1f0000) == 0xfc1f0000)) { /* frsp, fp?,NUM(r1) */
441 continue;
443 /* Set up frame pointer */
444 } else if (op == 0x603f0000 /* oril r31, r1, 0x0 */
445 || op == 0x7c3f0b78) { /* mr r31, r1 */
446 fdata->frameless = 0;
447 framep = 1;
448 fdata->alloca_reg = 31;
449 continue;
451 /* Another way to set up the frame pointer. */
452 } else if ((op & 0xfc1fffff) == 0x38010000) { /* addi rX, r1, 0x0 */
453 fdata->frameless = 0;
454 framep = 1;
455 fdata->alloca_reg = (op & ~0x38010000) >> 21;
456 continue;
458 } else {
459 break;
463 #if 0
464 /* I have problems with skipping over __main() that I need to address
465 * sometime. Previously, I used to use misc_function_vector which
466 * didn't work as well as I wanted to be. -MGO */
468 /* If the first thing after skipping a prolog is a branch to a function,
469 this might be a call to an initializer in main(), introduced by gcc2.
470 We'd like to skip over it as well. Fortunately, xlc does some extra
471 work before calling a function right after a prologue, thus we can
472 single out such gcc2 behaviour. */
475 if ((op & 0xfc000001) == 0x48000001) { /* bl foo, an initializer function? */
476 op = read_memory_integer (pc+4, 4);
478 if (op == 0x4def7b82) { /* cror 0xf, 0xf, 0xf (nop) */
480 /* check and see if we are in main. If so, skip over this initializer
481 function as well. */
483 tmp = find_pc_misc_function (pc);
484 if (tmp >= 0 && STREQ (misc_function_vector [tmp].name, "main"))
485 return pc + 8;
488 #endif /* 0 */
490 fdata->offset = - fdata->offset;
491 return pc;
495 /*************************************************************************
496 Support for creating pushind a dummy frame into the stack, and popping
497 frames, etc.
498 *************************************************************************/
500 /* The total size of dummy frame is 436, which is;
502 32 gpr's - 128 bytes
503 32 fpr's - 256 "
504 7 the rest - 28 "
505 and 24 extra bytes for the callee's link area. The last 24 bytes
506 for the link area might not be necessary, since it will be taken
507 care of by push_arguments(). */
509 #define DUMMY_FRAME_SIZE 436
511 #define DUMMY_FRAME_ADDR_SIZE 10
513 /* Make sure you initialize these in somewhere, in case gdb gives up what it
514 was debugging and starts debugging something else. FIXMEibm */
516 static int dummy_frame_count = 0;
517 static int dummy_frame_size = 0;
518 static CORE_ADDR *dummy_frame_addr = 0;
520 extern int stop_stack_dummy;
522 /* push a dummy frame into stack, save all register. Currently we are saving
523 only gpr's and fpr's, which is not good enough! FIXMEmgo */
525 void
526 push_dummy_frame ()
528 /* stack pointer. */
529 CORE_ADDR sp;
530 /* Same thing, target byte order. */
531 char sp_targ[4];
533 /* link register. */
534 CORE_ADDR pc;
535 /* Same thing, target byte order. */
536 char pc_targ[4];
538 /* Needed to figure out where to save the dummy link area.
539 FIXME: There should be an easier way to do this, no? tiemann 9/9/95. */
540 struct rs6000_framedata fdata;
542 int ii;
544 target_fetch_registers (-1);
546 if (dummy_frame_count >= dummy_frame_size) {
547 dummy_frame_size += DUMMY_FRAME_ADDR_SIZE;
548 if (dummy_frame_addr)
549 dummy_frame_addr = (CORE_ADDR*) xrealloc
550 (dummy_frame_addr, sizeof(CORE_ADDR) * (dummy_frame_size));
551 else
552 dummy_frame_addr = (CORE_ADDR*)
553 xmalloc (sizeof(CORE_ADDR) * (dummy_frame_size));
556 sp = read_register(SP_REGNUM);
557 pc = read_register(PC_REGNUM);
558 store_address (pc_targ, 4, pc);
560 skip_prologue (get_pc_function_start (pc), &fdata);
562 dummy_frame_addr [dummy_frame_count++] = sp;
564 /* Be careful! If the stack pointer is not decremented first, then kernel
565 thinks he is free to use the space underneath it. And kernel actually
566 uses that area for IPC purposes when executing ptrace(2) calls. So
567 before writing register values into the new frame, decrement and update
568 %sp first in order to secure your frame. */
570 /* FIXME: We don't check if the stack really has this much space.
571 This is a problem on the ppc simulator (which only grants one page
572 (4096 bytes) by default. */
574 write_register (SP_REGNUM, sp-DUMMY_FRAME_SIZE);
576 /* gdb relies on the state of current_frame. We'd better update it,
577 otherwise things like do_registers_info() wouldn't work properly! */
579 flush_cached_frames ();
581 /* save program counter in link register's space. */
582 write_memory (sp + (fdata.lr_offset ? fdata.lr_offset : DEFAULT_LR_SAVE),
583 pc_targ, 4);
585 /* save all floating point and general purpose registers here. */
587 /* fpr's, f0..f31 */
588 for (ii = 0; ii < 32; ++ii)
589 write_memory (sp-8-(ii*8), &registers[REGISTER_BYTE (31-ii+FP0_REGNUM)], 8);
591 /* gpr's r0..r31 */
592 for (ii=1; ii <=32; ++ii)
593 write_memory (sp-256-(ii*4), &registers[REGISTER_BYTE (32-ii)], 4);
595 /* so far, 32*2 + 32 words = 384 bytes have been written.
596 7 extra registers in our register set: pc, ps, cnd, lr, cnt, xer, mq */
598 for (ii=1; ii <= (LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii) {
599 write_memory (sp-384-(ii*4),
600 &registers[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4);
603 /* Save sp or so called back chain right here. */
604 store_address (sp_targ, 4, sp);
605 write_memory (sp-DUMMY_FRAME_SIZE, sp_targ, 4);
606 sp -= DUMMY_FRAME_SIZE;
608 /* And finally, this is the back chain. */
609 write_memory (sp+8, pc_targ, 4);
613 /* Pop a dummy frame.
615 In rs6000 when we push a dummy frame, we save all of the registers. This
616 is usually done before user calls a function explicitly.
618 After a dummy frame is pushed, some instructions are copied into stack,
619 and stack pointer is decremented even more. Since we don't have a frame
620 pointer to get back to the parent frame of the dummy, we start having
621 trouble poping it. Therefore, we keep a dummy frame stack, keeping
622 addresses of dummy frames as such. When poping happens and when we
623 detect that was a dummy frame, we pop it back to its parent by using
624 dummy frame stack (`dummy_frame_addr' array).
626 FIXME: This whole concept is broken. You should be able to detect
627 a dummy stack frame *on the user's stack itself*. When you do,
628 then you know the format of that stack frame -- including its
629 saved SP register! There should *not* be a separate stack in the
630 GDB process that keeps track of these dummy frames! -- gnu@cygnus.com Aug92
633 static void
634 pop_dummy_frame ()
636 CORE_ADDR sp, pc;
637 int ii;
638 sp = dummy_frame_addr [--dummy_frame_count];
640 /* restore all fpr's. */
641 for (ii = 1; ii <= 32; ++ii)
642 read_memory (sp-(ii*8), &registers[REGISTER_BYTE (32-ii+FP0_REGNUM)], 8);
644 /* restore all gpr's */
645 for (ii=1; ii <= 32; ++ii) {
646 read_memory (sp-256-(ii*4), &registers[REGISTER_BYTE (32-ii)], 4);
649 /* restore the rest of the registers. */
650 for (ii=1; ii <=(LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii)
651 read_memory (sp-384-(ii*4),
652 &registers[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4);
654 read_memory (sp-(DUMMY_FRAME_SIZE-8),
655 &registers [REGISTER_BYTE(PC_REGNUM)], 4);
657 /* when a dummy frame was being pushed, we had to decrement %sp first, in
658 order to secure astack space. Thus, saved %sp (or %r1) value, is not the
659 one we should restore. Change it with the one we need. */
661 memcpy (&registers [REGISTER_BYTE(FP_REGNUM)], (char *) &sp, sizeof (int));
663 /* Now we can restore all registers. */
665 target_store_registers (-1);
666 pc = read_pc ();
667 flush_cached_frames ();
671 /* pop the innermost frame, go back to the caller. */
673 void
674 pop_frame ()
676 CORE_ADDR pc, lr, sp, prev_sp; /* %pc, %lr, %sp */
677 struct rs6000_framedata fdata;
678 struct frame_info *frame = get_current_frame ();
679 int addr, ii;
681 pc = read_pc ();
682 sp = FRAME_FP (frame);
684 if (stop_stack_dummy)
686 #ifdef USE_GENERIC_DUMMY_FRAMES
687 generic_pop_dummy_frame ();
688 flush_cached_frames ();
689 return;
690 #else
691 if (dummy_frame_count)
692 pop_dummy_frame ();
693 return;
694 #endif
697 /* Make sure that all registers are valid. */
698 read_register_bytes (0, NULL, REGISTER_BYTES);
700 /* figure out previous %pc value. If the function is frameless, it is
701 still in the link register, otherwise walk the frames and retrieve the
702 saved %pc value in the previous frame. */
704 addr = get_pc_function_start (frame->pc);
705 (void) skip_prologue (addr, &fdata);
707 if (fdata.frameless)
708 prev_sp = sp;
709 else
710 prev_sp = read_memory_integer (sp, 4);
711 if (fdata.lr_offset == 0)
712 lr = read_register (LR_REGNUM);
713 else
714 lr = read_memory_integer (prev_sp + fdata.lr_offset, 4);
716 /* reset %pc value. */
717 write_register (PC_REGNUM, lr);
719 /* reset register values if any was saved earlier. */
721 if (fdata.saved_gpr != -1)
723 addr = prev_sp + fdata.gpr_offset;
724 for (ii = fdata.saved_gpr; ii <= 31; ++ii) {
725 read_memory (addr, &registers [REGISTER_BYTE (ii)], 4);
726 addr += 4;
730 if (fdata.saved_fpr != -1)
732 addr = prev_sp + fdata.fpr_offset;
733 for (ii = fdata.saved_fpr; ii <= 31; ++ii) {
734 read_memory (addr, &registers [REGISTER_BYTE (ii+FP0_REGNUM)], 8);
735 addr += 8;
739 write_register (SP_REGNUM, prev_sp);
740 target_store_registers (-1);
741 flush_cached_frames ();
744 /* fixup the call sequence of a dummy function, with the real function address.
745 its argumets will be passed by gdb. */
747 void
748 rs6000_fix_call_dummy (dummyname, pc, fun, nargs, args, type, gcc_p)
749 char *dummyname;
750 CORE_ADDR pc;
751 CORE_ADDR fun;
752 int nargs;
753 value_ptr *args;
754 struct type *type;
755 int gcc_p;
757 #define TOC_ADDR_OFFSET 20
758 #define TARGET_ADDR_OFFSET 28
760 int ii;
761 CORE_ADDR target_addr;
763 if (find_toc_address_hook != NULL)
765 CORE_ADDR tocvalue;
767 tocvalue = (*find_toc_address_hook) (fun);
768 ii = *(int*)((char*)dummyname + TOC_ADDR_OFFSET);
769 ii = (ii & 0xffff0000) | (tocvalue >> 16);
770 *(int*)((char*)dummyname + TOC_ADDR_OFFSET) = ii;
772 ii = *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4);
773 ii = (ii & 0xffff0000) | (tocvalue & 0x0000ffff);
774 *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4) = ii;
777 target_addr = fun;
778 ii = *(int*)((char*)dummyname + TARGET_ADDR_OFFSET);
779 ii = (ii & 0xffff0000) | (target_addr >> 16);
780 *(int*)((char*)dummyname + TARGET_ADDR_OFFSET) = ii;
782 ii = *(int*)((char*)dummyname + TARGET_ADDR_OFFSET+4);
783 ii = (ii & 0xffff0000) | (target_addr & 0x0000ffff);
784 *(int*)((char*)dummyname + TARGET_ADDR_OFFSET+4) = ii;
787 /* Pass the arguments in either registers, or in the stack. In RS6000,
788 the first eight words of the argument list (that might be less than
789 eight parameters if some parameters occupy more than one word) are
790 passed in r3..r11 registers. float and double parameters are
791 passed in fpr's, in addition to that. Rest of the parameters if any
792 are passed in user stack. There might be cases in which half of the
793 parameter is copied into registers, the other half is pushed into
794 stack.
796 If the function is returning a structure, then the return address is passed
797 in r3, then the first 7 words of the parameters can be passed in registers,
798 starting from r4. */
800 CORE_ADDR
801 push_arguments (nargs, args, sp, struct_return, struct_addr)
802 int nargs;
803 value_ptr *args;
804 CORE_ADDR sp;
805 int struct_return;
806 CORE_ADDR struct_addr;
808 int ii;
809 int len = 0;
810 int argno; /* current argument number */
811 int argbytes; /* current argument byte */
812 char tmp_buffer [50];
813 int f_argno = 0; /* current floating point argno */
815 value_ptr arg = 0;
816 struct type *type;
818 CORE_ADDR saved_sp;
820 #ifndef USE_GENERIC_DUMMY_FRAMES
821 if ( dummy_frame_count <= 0)
822 printf_unfiltered ("FATAL ERROR -push_arguments()! frame not found!!\n");
823 #endif /* GENERIC_DUMMY_FRAMES */
825 /* The first eight words of ther arguments are passed in registers. Copy
826 them appropriately.
828 If the function is returning a `struct', then the first word (which
829 will be passed in r3) is used for struct return address. In that
830 case we should advance one word and start from r4 register to copy
831 parameters. */
833 ii = struct_return ? 1 : 0;
836 effectively indirect call... gcc does...
838 return_val example( float, int);
840 eabi:
841 float in fp0, int in r3
842 offset of stack on overflow 8/16
843 for varargs, must go by type.
844 power open:
845 float in r3&r4, int in r5
846 offset of stack on overflow different
847 both:
848 return in r3 or f0. If no float, must study how gcc emulates floats;
849 pay attention to arg promotion.
850 User may have to cast\args to handle promotion correctly
851 since gdb won't know if prototype supplied or not.
854 for (argno=0, argbytes=0; argno < nargs && ii<8; ++ii) {
856 arg = args[argno];
857 type = check_typedef (VALUE_TYPE (arg));
858 len = TYPE_LENGTH (type);
860 if (TYPE_CODE (type) == TYPE_CODE_FLT) {
862 /* floating point arguments are passed in fpr's, as well as gpr's.
863 There are 13 fpr's reserved for passing parameters. At this point
864 there is no way we would run out of them. */
866 if (len > 8)
867 printf_unfiltered (
868 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
870 memcpy (&registers[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)],
871 VALUE_CONTENTS (arg),
872 len);
873 ++f_argno;
876 if (len > 4) {
878 /* Argument takes more than one register. */
879 while (argbytes < len) {
880 memset (&registers[REGISTER_BYTE(ii+3)], 0, sizeof(int));
881 memcpy (&registers[REGISTER_BYTE(ii+3)],
882 ((char*)VALUE_CONTENTS (arg))+argbytes,
883 (len - argbytes) > 4 ? 4 : len - argbytes);
884 ++ii, argbytes += 4;
886 if (ii >= 8)
887 goto ran_out_of_registers_for_arguments;
889 argbytes = 0;
890 --ii;
892 else { /* Argument can fit in one register. No problem. */
893 memset (&registers[REGISTER_BYTE(ii+3)], 0, sizeof(int));
894 memcpy (&registers[REGISTER_BYTE(ii+3)], VALUE_CONTENTS (arg), len);
896 ++argno;
899 ran_out_of_registers_for_arguments:
901 #ifdef USE_GENERIC_DUMMY_FRAMES
902 saved_sp = read_sp ();
903 #else
904 /* location for 8 parameters are always reserved. */
905 sp -= 4 * 8;
907 /* another six words for back chain, TOC register, link register, etc. */
908 sp -= 24;
909 #endif /* GENERIC_DUMMY_FRAMES */
910 /* if there are more arguments, allocate space for them in
911 the stack, then push them starting from the ninth one. */
913 if ((argno < nargs) || argbytes) {
914 int space = 0, jj;
916 if (argbytes) {
917 space += ((len - argbytes + 3) & -4);
918 jj = argno + 1;
920 else
921 jj = argno;
923 for (; jj < nargs; ++jj) {
924 value_ptr val = args[jj];
925 space += ((TYPE_LENGTH (VALUE_TYPE (val))) + 3) & -4;
928 /* add location required for the rest of the parameters */
929 space = (space + 7) & -8;
930 sp -= space;
932 /* This is another instance we need to be concerned about securing our
933 stack space. If we write anything underneath %sp (r1), we might conflict
934 with the kernel who thinks he is free to use this area. So, update %sp
935 first before doing anything else. */
937 write_register (SP_REGNUM, sp);
939 /* if the last argument copied into the registers didn't fit there
940 completely, push the rest of it into stack. */
942 if (argbytes) {
943 write_memory (sp+24+(ii*4),
944 ((char*)VALUE_CONTENTS (arg))+argbytes,
945 len - argbytes);
946 ++argno;
947 ii += ((len - argbytes + 3) & -4) / 4;
950 /* push the rest of the arguments into stack. */
951 for (; argno < nargs; ++argno) {
953 arg = args[argno];
954 type = check_typedef (VALUE_TYPE (arg));
955 len = TYPE_LENGTH (type);
958 /* float types should be passed in fpr's, as well as in the stack. */
959 if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13) {
961 if (len > 8)
962 printf_unfiltered (
963 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
965 memcpy (&registers[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)],
966 VALUE_CONTENTS (arg),
967 len);
968 ++f_argno;
971 write_memory (sp+24+(ii*4), (char *) VALUE_CONTENTS (arg), len);
972 ii += ((len + 3) & -4) / 4;
975 else
976 /* Secure stack areas first, before doing anything else. */
977 write_register (SP_REGNUM, sp);
979 #ifndef USE_GENERIC_DUMMY_FRAMES
980 /* we want to copy 24 bytes of target's frame to dummy's frame,
981 then set back chain to point to new frame. */
983 saved_sp = dummy_frame_addr [dummy_frame_count - 1];
984 read_memory (saved_sp, tmp_buffer, 24);
985 write_memory (sp, tmp_buffer, 24);
986 #endif /* GENERIC_DUMMY_FRAMES */
988 /* set back chain properly */
989 store_address (tmp_buffer, 4, saved_sp);
990 write_memory (sp, tmp_buffer, 4);
992 target_store_registers (-1);
993 return sp;
995 #ifdef ELF_OBJECT_FORMAT
997 /* Function: ppc_push_return_address (pc, sp)
998 Set up the return address for the inferior function call. */
1000 CORE_ADDR
1001 ppc_push_return_address (pc, sp)
1002 CORE_ADDR pc;
1003 CORE_ADDR sp;
1005 write_register (LR_REGNUM, CALL_DUMMY_ADDRESS ());
1006 return sp;
1009 #endif
1011 /* a given return value in `regbuf' with a type `valtype', extract and copy its
1012 value into `valbuf' */
1014 void
1015 extract_return_value (valtype, regbuf, valbuf)
1016 struct type *valtype;
1017 char regbuf[REGISTER_BYTES];
1018 char *valbuf;
1020 int offset = 0;
1022 if (TYPE_CODE (valtype) == TYPE_CODE_FLT) {
1024 double dd; float ff;
1025 /* floats and doubles are returned in fpr1. fpr's have a size of 8 bytes.
1026 We need to truncate the return value into float size (4 byte) if
1027 necessary. */
1029 if (TYPE_LENGTH (valtype) > 4) /* this is a double */
1030 memcpy (valbuf,
1031 &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)],
1032 TYPE_LENGTH (valtype));
1033 else { /* float */
1034 memcpy (&dd, &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)], 8);
1035 ff = (float)dd;
1036 memcpy (valbuf, &ff, sizeof(float));
1039 else {
1040 /* return value is copied starting from r3. */
1041 if (TARGET_BYTE_ORDER == BIG_ENDIAN
1042 && TYPE_LENGTH (valtype) < REGISTER_RAW_SIZE (3))
1043 offset = REGISTER_RAW_SIZE (3) - TYPE_LENGTH (valtype);
1045 memcpy (valbuf,
1046 regbuf + REGISTER_BYTE (3) + offset,
1047 TYPE_LENGTH (valtype));
1052 /* keep structure return address in this variable.
1053 FIXME: This is a horrid kludge which should not be allowed to continue
1054 living. This only allows a single nested call to a structure-returning
1055 function. Come on, guys! -- gnu@cygnus.com, Aug 92 */
1057 CORE_ADDR rs6000_struct_return_address;
1060 /* Indirect function calls use a piece of trampoline code to do context
1061 switching, i.e. to set the new TOC table. Skip such code if we are on
1062 its first instruction (as when we have single-stepped to here).
1063 Also skip shared library trampoline code (which is different from
1064 indirect function call trampolines).
1065 Result is desired PC to step until, or NULL if we are not in
1066 trampoline code. */
1068 CORE_ADDR
1069 skip_trampoline_code (pc)
1070 CORE_ADDR pc;
1072 register unsigned int ii, op;
1073 CORE_ADDR solib_target_pc;
1075 static unsigned trampoline_code[] = {
1076 0x800b0000, /* l r0,0x0(r11) */
1077 0x90410014, /* st r2,0x14(r1) */
1078 0x7c0903a6, /* mtctr r0 */
1079 0x804b0004, /* l r2,0x4(r11) */
1080 0x816b0008, /* l r11,0x8(r11) */
1081 0x4e800420, /* bctr */
1082 0x4e800020, /* br */
1086 /* If pc is in a shared library trampoline, return its target. */
1087 solib_target_pc = find_solib_trampoline_target (pc);
1088 if (solib_target_pc)
1089 return solib_target_pc;
1091 for (ii=0; trampoline_code[ii]; ++ii) {
1092 op = read_memory_integer (pc + (ii*4), 4);
1093 if (op != trampoline_code [ii])
1094 return 0;
1096 ii = read_register (11); /* r11 holds destination addr */
1097 pc = read_memory_integer (ii, 4); /* (r11) value */
1098 return pc;
1101 /* Determines whether the function FI has a frame on the stack or not. */
1104 frameless_function_invocation (fi)
1105 struct frame_info *fi;
1107 CORE_ADDR func_start;
1108 struct rs6000_framedata fdata;
1110 /* Don't even think about framelessness except on the innermost frame
1111 or if the function was interrupted by a signal. */
1112 if (fi->next != NULL && !fi->next->signal_handler_caller)
1113 return 0;
1115 func_start = get_pc_function_start (fi->pc);
1117 /* If we failed to find the start of the function, it is a mistake
1118 to inspect the instructions. */
1120 if (!func_start)
1122 /* A frame with a zero PC is usually created by dereferencing a NULL
1123 function pointer, normally causing an immediate core dump of the
1124 inferior. Mark function as frameless, as the inferior has no chance
1125 of setting up a stack frame. */
1126 if (fi->pc == 0)
1127 return 1;
1128 else
1129 return 0;
1132 (void) skip_prologue (func_start, &fdata);
1133 return fdata.frameless;
1136 /* Return the PC saved in a frame */
1138 unsigned long
1139 frame_saved_pc (fi)
1140 struct frame_info *fi;
1142 CORE_ADDR func_start;
1143 struct rs6000_framedata fdata;
1145 if (fi->signal_handler_caller)
1146 return read_memory_integer (fi->frame + SIG_FRAME_PC_OFFSET, 4);
1148 #ifdef USE_GENERIC_DUMMY_FRAMES
1149 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
1150 return generic_read_register_dummy(fi->pc, fi->frame, PC_REGNUM);
1151 #endif /* GENERIC_DUMMY_FRAMES */
1153 func_start = get_pc_function_start (fi->pc);
1155 /* If we failed to find the start of the function, it is a mistake
1156 to inspect the instructions. */
1157 if (!func_start)
1158 return 0;
1160 (void) skip_prologue (func_start, &fdata);
1162 if (fdata.lr_offset == 0 && fi->next != NULL)
1164 if (fi->next->signal_handler_caller)
1165 return read_memory_integer (fi->next->frame + SIG_FRAME_LR_OFFSET, 4);
1166 else
1167 return read_memory_integer (rs6000_frame_chain (fi) + DEFAULT_LR_SAVE,
1171 if (fdata.lr_offset == 0)
1172 return read_register (LR_REGNUM);
1174 return read_memory_integer (rs6000_frame_chain (fi) + fdata.lr_offset, 4);
1177 /* If saved registers of frame FI are not known yet, read and cache them.
1178 &FDATAP contains rs6000_framedata; TDATAP can be NULL,
1179 in which case the framedata are read. */
1181 static void
1182 frame_get_saved_regs (fi, fdatap)
1183 struct frame_info *fi;
1184 struct rs6000_framedata *fdatap;
1186 int ii;
1187 CORE_ADDR frame_addr;
1188 struct rs6000_framedata work_fdata;
1190 if (fi->saved_regs)
1191 return;
1193 if (fdatap == NULL)
1195 fdatap = &work_fdata;
1196 (void) skip_prologue (get_pc_function_start (fi->pc), fdatap);
1199 frame_saved_regs_zalloc (fi);
1201 /* If there were any saved registers, figure out parent's stack
1202 pointer. */
1203 /* The following is true only if the frame doesn't have a call to
1204 alloca(), FIXME. */
1206 if (fdatap->saved_fpr == 0 && fdatap->saved_gpr == 0
1207 && fdatap->lr_offset == 0 && fdatap->cr_offset == 0)
1208 frame_addr = 0;
1209 else if (fi->prev && fi->prev->frame)
1210 frame_addr = fi->prev->frame;
1211 else
1212 frame_addr = read_memory_integer (fi->frame, 4);
1214 /* if != -1, fdatap->saved_fpr is the smallest number of saved_fpr.
1215 All fpr's from saved_fpr to fp31 are saved. */
1217 if (fdatap->saved_fpr >= 0)
1219 int i;
1220 int fpr_offset = frame_addr + fdatap->fpr_offset;
1221 for (i = fdatap->saved_fpr; i < 32; i++)
1223 fi->saved_regs [FP0_REGNUM + i] = fpr_offset;
1224 fpr_offset += 8;
1228 /* if != -1, fdatap->saved_gpr is the smallest number of saved_gpr.
1229 All gpr's from saved_gpr to gpr31 are saved. */
1231 if (fdatap->saved_gpr >= 0)
1233 int i;
1234 int gpr_offset = frame_addr + fdatap->gpr_offset;
1235 for (i = fdatap->saved_gpr; i < 32; i++)
1237 fi->saved_regs [i] = gpr_offset;
1238 gpr_offset += 4;
1242 /* If != 0, fdatap->cr_offset is the offset from the frame that holds
1243 the CR. */
1244 if (fdatap->cr_offset != 0)
1245 fi->saved_regs [CR_REGNUM] = frame_addr + fdatap->cr_offset;
1247 /* If != 0, fdatap->lr_offset is the offset from the frame that holds
1248 the LR. */
1249 if (fdatap->lr_offset != 0)
1250 fi->saved_regs [LR_REGNUM] = frame_addr + fdatap->lr_offset;
1253 /* Return the address of a frame. This is the inital %sp value when the frame
1254 was first allocated. For functions calling alloca(), it might be saved in
1255 an alloca register. */
1257 static CORE_ADDR
1258 frame_initial_stack_address (fi)
1259 struct frame_info *fi;
1261 CORE_ADDR tmpaddr;
1262 struct rs6000_framedata fdata;
1263 struct frame_info *callee_fi;
1265 /* if the initial stack pointer (frame address) of this frame is known,
1266 just return it. */
1268 if (fi->extra_info->initial_sp)
1269 return fi->extra_info->initial_sp;
1271 /* find out if this function is using an alloca register.. */
1273 (void) skip_prologue (get_pc_function_start (fi->pc), &fdata);
1275 /* if saved registers of this frame are not known yet, read and cache them. */
1277 if (!fi->saved_regs)
1278 frame_get_saved_regs (fi, &fdata);
1280 /* If no alloca register used, then fi->frame is the value of the %sp for
1281 this frame, and it is good enough. */
1283 if (fdata.alloca_reg < 0)
1285 fi->extra_info->initial_sp = fi->frame;
1286 return fi->extra_info->initial_sp;
1289 /* This function has an alloca register. If this is the top-most frame
1290 (with the lowest address), the value in alloca register is good. */
1292 if (!fi->next)
1293 return fi->extra_info->initial_sp = read_register (fdata.alloca_reg);
1295 /* Otherwise, this is a caller frame. Callee has usually already saved
1296 registers, but there are exceptions (such as when the callee
1297 has no parameters). Find the address in which caller's alloca
1298 register is saved. */
1300 for (callee_fi = fi->next; callee_fi; callee_fi = callee_fi->next) {
1302 if (!callee_fi->saved_regs)
1303 frame_get_saved_regs (callee_fi, NULL);
1305 /* this is the address in which alloca register is saved. */
1307 tmpaddr = callee_fi->saved_regs [fdata.alloca_reg];
1308 if (tmpaddr) {
1309 fi->extra_info->initial_sp = read_memory_integer (tmpaddr, 4);
1310 return fi->extra_info->initial_sp;
1313 /* Go look into deeper levels of the frame chain to see if any one of
1314 the callees has saved alloca register. */
1317 /* If alloca register was not saved, by the callee (or any of its callees)
1318 then the value in the register is still good. */
1320 fi->extra_info->initial_sp = read_register (fdata.alloca_reg);
1321 return fi->extra_info->initial_sp;
1324 CORE_ADDR
1325 rs6000_frame_chain (thisframe)
1326 struct frame_info *thisframe;
1328 CORE_ADDR fp;
1330 #ifdef USE_GENERIC_DUMMY_FRAMES
1331 if (PC_IN_CALL_DUMMY (thisframe->pc, thisframe->frame, thisframe->frame))
1332 return thisframe->frame; /* dummy frame same as caller's frame */
1333 #endif /* GENERIC_DUMMY_FRAMES */
1335 if (inside_entry_file (thisframe->pc) ||
1336 thisframe->pc == entry_point_address ())
1337 return 0;
1339 if (thisframe->signal_handler_caller)
1340 fp = read_memory_integer (thisframe->frame + SIG_FRAME_FP_OFFSET, 4);
1341 else if (thisframe->next != NULL
1342 && thisframe->next->signal_handler_caller
1343 && frameless_function_invocation (thisframe))
1344 /* A frameless function interrupted by a signal did not change the
1345 frame pointer. */
1346 fp = FRAME_FP (thisframe);
1347 else
1348 fp = read_memory_integer ((thisframe)->frame, 4);
1350 #ifdef USE_GENERIC_DUMMY_FRAMES
1352 CORE_ADDR fpp, lr;
1354 lr = read_register (LR_REGNUM);
1355 if (lr == entry_point_address ())
1356 if (fp != 0 && (fpp = read_memory_integer (fp, 4)) != 0)
1357 if (PC_IN_CALL_DUMMY (lr, fpp, fpp))
1358 return fpp;
1360 #endif /* GENERIC_DUMMY_FRAMES */
1361 return fp;
1364 /* Return nonzero if ADDR (a function pointer) is in the data space and
1365 is therefore a special function pointer. */
1368 is_magic_function_pointer (addr)
1369 CORE_ADDR addr;
1371 struct obj_section *s;
1373 s = find_pc_section (addr);
1374 if (s && s->the_bfd_section->flags & SEC_CODE)
1375 return 0;
1376 else
1377 return 1;
1380 #ifdef GDB_TARGET_POWERPC
1382 gdb_print_insn_powerpc (memaddr, info)
1383 bfd_vma memaddr;
1384 disassemble_info *info;
1386 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1387 return print_insn_big_powerpc (memaddr, info);
1388 else
1389 return print_insn_little_powerpc (memaddr, info);
1391 #endif
1393 /* Function: get_saved_register
1394 Just call the generic_get_saved_register function. */
1396 #ifdef USE_GENERIC_DUMMY_FRAMES
1397 void
1398 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
1399 char *raw_buffer;
1400 int *optimized;
1401 CORE_ADDR *addrp;
1402 struct frame_info *frame;
1403 int regnum;
1404 enum lval_type *lval;
1406 generic_get_saved_register (raw_buffer, optimized, addrp,
1407 frame, regnum, lval);
1409 #endif
1412 void
1413 _initialize_rs6000_tdep ()
1415 /* FIXME, this should not be decided via ifdef. */
1416 #ifdef GDB_TARGET_POWERPC
1417 tm_print_insn = gdb_print_insn_powerpc;
1418 #else
1419 tm_print_insn = print_insn_rs6000;
1420 #endif