* Fixing typo that caused infinite loop upon PKE MPG.
[binutils-gdb.git] / gdb / rs6000-tdep.c
blob90893c01347713e52a43974aaac247bf1f20c36f
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 struct obstack frame_cache_obstack;
33 extern int errno;
35 /* Nonzero if we just simulated a single step break. */
36 int one_stepped;
38 /* Breakpoint shadows for the single step instructions will be kept here. */
40 static struct sstep_breaks {
41 /* Address, or 0 if this is not in use. */
42 CORE_ADDR address;
43 /* Shadow contents. */
44 char data[4];
45 } stepBreaks[2];
47 /* Hook for determining the TOC address when calling functions in the
48 inferior under AIX. The initialization code in rs6000-nat.c sets
49 this hook to point to find_toc_address. */
51 CORE_ADDR (*find_toc_address_hook) PARAMS ((CORE_ADDR)) = NULL;
53 /* Static function prototypes */
55 static CORE_ADDR branch_dest PARAMS ((int opcode, int instr, CORE_ADDR pc,
56 CORE_ADDR safety));
58 static void frame_get_cache_fsr PARAMS ((struct frame_info *fi,
59 struct rs6000_framedata *fdatap));
61 static void pop_dummy_frame PARAMS ((void));
63 /* Calculate the destination of a branch/jump. Return -1 if not a branch. */
65 static CORE_ADDR
66 branch_dest (opcode, instr, pc, safety)
67 int opcode;
68 int instr;
69 CORE_ADDR pc;
70 CORE_ADDR safety;
72 CORE_ADDR dest;
73 int immediate;
74 int absolute;
75 int ext_op;
77 absolute = (int) ((instr >> 1) & 1);
79 switch (opcode) {
80 case 18 :
81 immediate = ((instr & ~3) << 6) >> 6; /* br unconditional */
82 if (absolute)
83 dest = immediate;
84 else
85 dest = pc + immediate;
86 break;
88 case 16 :
89 immediate = ((instr & ~3) << 16) >> 16; /* br conditional */
90 if (absolute)
91 dest = immediate;
92 else
93 dest = pc + immediate;
94 break;
96 case 19 :
97 ext_op = (instr>>1) & 0x3ff;
99 if (ext_op == 16) /* br conditional register */
101 dest = read_register (LR_REGNUM) & ~3;
103 /* If we are about to return from a signal handler, dest is
104 something like 0x3c90. The current frame is a signal handler
105 caller frame, upon completion of the sigreturn system call
106 execution will return to the saved PC in the frame. */
107 if (dest < TEXT_SEGMENT_BASE)
109 struct frame_info *fi;
111 fi = get_current_frame ();
112 if (fi != NULL)
113 dest = read_memory_integer (fi->frame + SIG_FRAME_PC_OFFSET,
118 else if (ext_op == 528) /* br cond to count reg */
120 dest = read_register (CTR_REGNUM) & ~3;
122 /* If we are about to execute a system call, dest is something
123 like 0x22fc or 0x3b00. Upon completion the system call
124 will return to the address in the link register. */
125 if (dest < TEXT_SEGMENT_BASE)
126 dest = read_register (LR_REGNUM) & ~3;
128 else return -1;
129 break;
131 default: return -1;
133 return (dest < TEXT_SEGMENT_BASE) ? safety : dest;
138 /* AIX does not support PT_STEP. Simulate it. */
140 void
141 single_step (signal)
142 enum target_signal signal;
144 #define INSNLEN(OPCODE) 4
146 static char le_breakp[] = LITTLE_BREAKPOINT;
147 static char be_breakp[] = BIG_BREAKPOINT;
148 char *breakp = TARGET_BYTE_ORDER == BIG_ENDIAN ? be_breakp : le_breakp;
149 int ii, insn;
150 CORE_ADDR loc;
151 CORE_ADDR breaks[2];
152 int opcode;
154 if (!one_stepped) {
155 loc = read_pc ();
157 insn = read_memory_integer (loc, 4);
159 breaks[0] = loc + INSNLEN(insn);
160 opcode = insn >> 26;
161 breaks[1] = branch_dest (opcode, insn, loc, breaks[0]);
163 /* Don't put two breakpoints on the same address. */
164 if (breaks[1] == breaks[0])
165 breaks[1] = -1;
167 stepBreaks[1].address = 0;
169 for (ii=0; ii < 2; ++ii) {
171 /* ignore invalid breakpoint. */
172 if ( breaks[ii] == -1)
173 continue;
175 read_memory (breaks[ii], stepBreaks[ii].data, 4);
177 write_memory (breaks[ii], breakp, 4);
178 stepBreaks[ii].address = breaks[ii];
181 one_stepped = 1;
182 } else {
184 /* remove step breakpoints. */
185 for (ii=0; ii < 2; ++ii)
186 if (stepBreaks[ii].address != 0)
187 write_memory
188 (stepBreaks[ii].address, stepBreaks[ii].data, 4);
190 one_stepped = 0;
192 errno = 0; /* FIXME, don't ignore errors! */
193 /* What errors? {read,write}_memory call error(). */
197 /* return pc value after skipping a function prologue and also return
198 information about a function frame.
200 in struct rs6000_frameinfo fdata:
201 - frameless is TRUE, if function does not have a frame.
202 - nosavedpc is TRUE, if function does not save %pc value in its frame.
203 - offset is the number of bytes used in the frame to save registers.
204 - saved_gpr is the number of the first saved gpr.
205 - saved_fpr is the number of the first saved fpr.
206 - alloca_reg is the number of the register used for alloca() handling.
207 Otherwise -1.
208 - gpr_offset is the offset of the saved gprs
209 - fpr_offset is the offset of the saved fprs
210 - lr_offset is the offset of the saved lr
211 - cr_offset is the offset of the saved cr
214 #define SIGNED_SHORT(x) \
215 ((sizeof (short) == 2) \
216 ? ((int)(short)(x)) \
217 : ((int)((((x) & 0xffff) ^ 0x8000) - 0x8000)))
219 #define GET_SRC_REG(x) (((x) >> 21) & 0x1f)
221 CORE_ADDR
222 skip_prologue (pc, fdata)
223 CORE_ADDR pc;
224 struct rs6000_framedata *fdata;
226 CORE_ADDR orig_pc = pc;
227 char buf[4];
228 unsigned long op;
229 long offset = 0;
230 int lr_reg = 0;
231 int cr_reg = 0;
232 int reg;
233 int framep = 0;
234 int minimal_toc_loaded = 0;
235 static struct rs6000_framedata zero_frame;
237 *fdata = zero_frame;
238 fdata->saved_gpr = -1;
239 fdata->saved_fpr = -1;
240 fdata->alloca_reg = -1;
241 fdata->frameless = 1;
242 fdata->nosavedpc = 1;
244 if (target_read_memory (pc, buf, 4))
245 return pc; /* Can't access it -- assume no prologue. */
247 /* Assume that subsequent fetches can fail with low probability. */
248 pc -= 4;
249 for (;;)
251 pc += 4;
252 op = read_memory_integer (pc, 4);
254 if ((op & 0xfc1fffff) == 0x7c0802a6) { /* mflr Rx */
255 lr_reg = (op & 0x03e00000) | 0x90010000;
256 continue;
258 } else if ((op & 0xfc1fffff) == 0x7c000026) { /* mfcr Rx */
259 cr_reg = (op & 0x03e00000) | 0x90010000;
260 continue;
262 } else if ((op & 0xfc1f0000) == 0xd8010000) { /* stfd Rx,NUM(r1) */
263 reg = GET_SRC_REG (op);
264 if (fdata->saved_fpr == -1 || fdata->saved_fpr > reg) {
265 fdata->saved_fpr = reg;
266 fdata->fpr_offset = SIGNED_SHORT (op) + offset;
268 continue;
270 } else if (((op & 0xfc1f0000) == 0xbc010000) || /* stm Rx, NUM(r1) */
271 ((op & 0xfc1f0000) == 0x90010000 && /* st rx,NUM(r1), rx >= r13 */
272 (op & 0x03e00000) >= 0x01a00000)) {
274 reg = GET_SRC_REG (op);
275 if (fdata->saved_gpr == -1 || fdata->saved_gpr > reg) {
276 fdata->saved_gpr = reg;
277 fdata->gpr_offset = SIGNED_SHORT (op) + offset;
279 continue;
281 } else if ((op & 0xffff0000) == 0x3c000000) { /* addis 0,0,NUM, used for >= 32k frames */
282 fdata->offset = (op & 0x0000ffff) << 16;
283 fdata->frameless = 0;
284 continue;
286 } else if ((op & 0xffff0000) == 0x60000000) { /* ori 0,0,NUM, 2nd half of >= 32k frames */
287 fdata->offset |= (op & 0x0000ffff);
288 fdata->frameless = 0;
289 continue;
291 } else if ((op & 0xffff0000) == lr_reg) { /* st Rx,NUM(r1) where Rx == lr */
292 fdata->lr_offset = SIGNED_SHORT (op) + offset;
293 fdata->nosavedpc = 0;
294 lr_reg = 0;
295 continue;
297 } else if ((op & 0xffff0000) == cr_reg) { /* st Rx,NUM(r1) where Rx == cr */
298 fdata->cr_offset = SIGNED_SHORT (op) + offset;
299 cr_reg = 0;
300 continue;
302 } else if (op == 0x48000005) { /* bl .+4 used in -mrelocatable */
303 continue;
305 } else if (op == 0x48000004) { /* b .+4 (xlc) */
306 break;
308 } else if (((op & 0xffff0000) == 0x801e0000 || /* lwz 0,NUM(r30), used in V.4 -mrelocatable */
309 op == 0x7fc0f214) && /* add r30,r0,r30, used in V.4 -mrelocatable */
310 lr_reg == 0x901e0000) {
311 continue;
313 } else if ((op & 0xffff0000) == 0x3fc00000 || /* addis 30,0,foo@ha, used in V.4 -mminimal-toc */
314 (op & 0xffff0000) == 0x3bde0000) { /* addi 30,30,foo@l */
315 continue;
317 } else if ((op & 0xfc000000) == 0x48000000) { /* bl foo, to save fprs??? */
319 fdata->frameless = 0;
320 /* Don't skip over the subroutine call if it is not within the first
321 three instructions of the prologue. */
322 if ((pc - orig_pc) > 8)
323 break;
325 op = read_memory_integer (pc+4, 4);
327 /* At this point, make sure this is not a trampoline function
328 (a function that simply calls another functions, and nothing else).
329 If the next is not a nop, this branch was part of the function
330 prologue. */
332 if (op == 0x4def7b82 || op == 0) /* crorc 15, 15, 15 */
333 break; /* don't skip over this branch */
335 continue;
337 /* update stack pointer */
338 } else if ((op & 0xffff0000) == 0x94210000) { /* stu r1,NUM(r1) */
339 fdata->frameless = 0;
340 fdata->offset = SIGNED_SHORT (op);
341 offset = fdata->offset;
342 continue;
344 } else if (op == 0x7c21016e) { /* stwux 1,1,0 */
345 fdata->frameless = 0;
346 offset = fdata->offset;
347 continue;
349 /* Load up minimal toc pointer */
350 } else if ((op >> 22) == 0x20f
351 && ! minimal_toc_loaded) { /* l r31,... or l r30,... */
352 minimal_toc_loaded = 1;
353 continue;
355 /* store parameters in stack */
356 } else if ((op & 0xfc1f0000) == 0x90010000 || /* st rx,NUM(r1) */
357 (op & 0xfc1f0000) == 0xd8010000 || /* stfd Rx,NUM(r1) */
358 (op & 0xfc1f0000) == 0xfc010000) { /* frsp, fp?,NUM(r1) */
359 continue;
361 /* store parameters in stack via frame pointer */
362 } else if (framep &&
363 ((op & 0xfc1f0000) == 0x901f0000 || /* st rx,NUM(r1) */
364 (op & 0xfc1f0000) == 0xd81f0000 || /* stfd Rx,NUM(r1) */
365 (op & 0xfc1f0000) == 0xfc1f0000)) { /* frsp, fp?,NUM(r1) */
366 continue;
368 /* Set up frame pointer */
369 } else if (op == 0x603f0000 /* oril r31, r1, 0x0 */
370 || op == 0x7c3f0b78) { /* mr r31, r1 */
371 fdata->frameless = 0;
372 framep = 1;
373 fdata->alloca_reg = 31;
374 continue;
376 /* Another way to set up the frame pointer. */
377 } else if ((op & 0xfc1fffff) == 0x38010000) { /* addi rX, r1, 0x0 */
378 fdata->frameless = 0;
379 framep = 1;
380 fdata->alloca_reg = (op & ~0x38010000) >> 21;
381 continue;
383 } else {
384 break;
388 #if 0
389 /* I have problems with skipping over __main() that I need to address
390 * sometime. Previously, I used to use misc_function_vector which
391 * didn't work as well as I wanted to be. -MGO */
393 /* If the first thing after skipping a prolog is a branch to a function,
394 this might be a call to an initializer in main(), introduced by gcc2.
395 We'd like to skip over it as well. Fortunately, xlc does some extra
396 work before calling a function right after a prologue, thus we can
397 single out such gcc2 behaviour. */
400 if ((op & 0xfc000001) == 0x48000001) { /* bl foo, an initializer function? */
401 op = read_memory_integer (pc+4, 4);
403 if (op == 0x4def7b82) { /* cror 0xf, 0xf, 0xf (nop) */
405 /* check and see if we are in main. If so, skip over this initializer
406 function as well. */
408 tmp = find_pc_misc_function (pc);
409 if (tmp >= 0 && STREQ (misc_function_vector [tmp].name, "main"))
410 return pc + 8;
413 #endif /* 0 */
415 fdata->offset = - fdata->offset;
416 return pc;
420 /*************************************************************************
421 Support for creating pushind a dummy frame into the stack, and popping
422 frames, etc.
423 *************************************************************************/
425 /* The total size of dummy frame is 436, which is;
427 32 gpr's - 128 bytes
428 32 fpr's - 256 "
429 7 the rest - 28 "
430 and 24 extra bytes for the callee's link area. The last 24 bytes
431 for the link area might not be necessary, since it will be taken
432 care of by push_arguments(). */
434 #define DUMMY_FRAME_SIZE 436
436 #define DUMMY_FRAME_ADDR_SIZE 10
438 /* Make sure you initialize these in somewhere, in case gdb gives up what it
439 was debugging and starts debugging something else. FIXMEibm */
441 static int dummy_frame_count = 0;
442 static int dummy_frame_size = 0;
443 static CORE_ADDR *dummy_frame_addr = 0;
445 extern int stop_stack_dummy;
447 /* push a dummy frame into stack, save all register. Currently we are saving
448 only gpr's and fpr's, which is not good enough! FIXMEmgo */
450 void
451 push_dummy_frame ()
453 /* stack pointer. */
454 CORE_ADDR sp;
455 /* Same thing, target byte order. */
456 char sp_targ[4];
458 /* link register. */
459 CORE_ADDR pc;
460 /* Same thing, target byte order. */
461 char pc_targ[4];
463 /* Needed to figure out where to save the dummy link area.
464 FIXME: There should be an easier way to do this, no? tiemann 9/9/95. */
465 struct rs6000_framedata fdata;
467 int ii;
469 target_fetch_registers (-1);
471 if (dummy_frame_count >= dummy_frame_size) {
472 dummy_frame_size += DUMMY_FRAME_ADDR_SIZE;
473 if (dummy_frame_addr)
474 dummy_frame_addr = (CORE_ADDR*) xrealloc
475 (dummy_frame_addr, sizeof(CORE_ADDR) * (dummy_frame_size));
476 else
477 dummy_frame_addr = (CORE_ADDR*)
478 xmalloc (sizeof(CORE_ADDR) * (dummy_frame_size));
481 sp = read_register(SP_REGNUM);
482 pc = read_register(PC_REGNUM);
483 store_address (pc_targ, 4, pc);
485 (void) skip_prologue (get_pc_function_start (pc) + FUNCTION_START_OFFSET, &fdata);
487 dummy_frame_addr [dummy_frame_count++] = sp;
489 /* Be careful! If the stack pointer is not decremented first, then kernel
490 thinks he is free to use the space underneath it. And kernel actually
491 uses that area for IPC purposes when executing ptrace(2) calls. So
492 before writing register values into the new frame, decrement and update
493 %sp first in order to secure your frame. */
495 /* FIXME: We don't check if the stack really has this much space.
496 This is a problem on the ppc simulator (which only grants one page
497 (4096 bytes) by default. */
499 write_register (SP_REGNUM, sp-DUMMY_FRAME_SIZE);
501 /* gdb relies on the state of current_frame. We'd better update it,
502 otherwise things like do_registers_info() wouldn't work properly! */
504 flush_cached_frames ();
506 /* save program counter in link register's space. */
507 write_memory (sp + (fdata.lr_offset ? fdata.lr_offset : DEFAULT_LR_SAVE),
508 pc_targ, 4);
510 /* save all floating point and general purpose registers here. */
512 /* fpr's, f0..f31 */
513 for (ii = 0; ii < 32; ++ii)
514 write_memory (sp-8-(ii*8), &registers[REGISTER_BYTE (31-ii+FP0_REGNUM)], 8);
516 /* gpr's r0..r31 */
517 for (ii=1; ii <=32; ++ii)
518 write_memory (sp-256-(ii*4), &registers[REGISTER_BYTE (32-ii)], 4);
520 /* so far, 32*2 + 32 words = 384 bytes have been written.
521 7 extra registers in our register set: pc, ps, cnd, lr, cnt, xer, mq */
523 for (ii=1; ii <= (LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii) {
524 write_memory (sp-384-(ii*4),
525 &registers[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4);
528 /* Save sp or so called back chain right here. */
529 store_address (sp_targ, 4, sp);
530 write_memory (sp-DUMMY_FRAME_SIZE, sp_targ, 4);
531 sp -= DUMMY_FRAME_SIZE;
533 /* And finally, this is the back chain. */
534 write_memory (sp+8, pc_targ, 4);
538 /* Pop a dummy frame.
540 In rs6000 when we push a dummy frame, we save all of the registers. This
541 is usually done before user calls a function explicitly.
543 After a dummy frame is pushed, some instructions are copied into stack,
544 and stack pointer is decremented even more. Since we don't have a frame
545 pointer to get back to the parent frame of the dummy, we start having
546 trouble poping it. Therefore, we keep a dummy frame stack, keeping
547 addresses of dummy frames as such. When poping happens and when we
548 detect that was a dummy frame, we pop it back to its parent by using
549 dummy frame stack (`dummy_frame_addr' array).
551 FIXME: This whole concept is broken. You should be able to detect
552 a dummy stack frame *on the user's stack itself*. When you do,
553 then you know the format of that stack frame -- including its
554 saved SP register! There should *not* be a separate stack in the
555 GDB process that keeps track of these dummy frames! -- gnu@cygnus.com Aug92
558 static void
559 pop_dummy_frame ()
561 CORE_ADDR sp, pc;
562 int ii;
563 sp = dummy_frame_addr [--dummy_frame_count];
565 /* restore all fpr's. */
566 for (ii = 1; ii <= 32; ++ii)
567 read_memory (sp-(ii*8), &registers[REGISTER_BYTE (32-ii+FP0_REGNUM)], 8);
569 /* restore all gpr's */
570 for (ii=1; ii <= 32; ++ii) {
571 read_memory (sp-256-(ii*4), &registers[REGISTER_BYTE (32-ii)], 4);
574 /* restore the rest of the registers. */
575 for (ii=1; ii <=(LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii)
576 read_memory (sp-384-(ii*4),
577 &registers[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4);
579 read_memory (sp-(DUMMY_FRAME_SIZE-8),
580 &registers [REGISTER_BYTE(PC_REGNUM)], 4);
582 /* when a dummy frame was being pushed, we had to decrement %sp first, in
583 order to secure astack space. Thus, saved %sp (or %r1) value, is not the
584 one we should restore. Change it with the one we need. */
586 *(int*)&registers [REGISTER_BYTE(FP_REGNUM)] = sp;
588 /* Now we can restore all registers. */
590 target_store_registers (-1);
591 pc = read_pc ();
592 flush_cached_frames ();
596 /* pop the innermost frame, go back to the caller. */
598 void
599 pop_frame ()
601 CORE_ADDR pc, lr, sp, prev_sp; /* %pc, %lr, %sp */
602 struct rs6000_framedata fdata;
603 struct frame_info *frame = get_current_frame ();
604 int addr, ii;
606 pc = read_pc ();
607 sp = FRAME_FP (frame);
609 if (stop_stack_dummy && dummy_frame_count) {
610 pop_dummy_frame ();
611 return;
614 /* Make sure that all registers are valid. */
615 read_register_bytes (0, NULL, REGISTER_BYTES);
617 /* figure out previous %pc value. If the function is frameless, it is
618 still in the link register, otherwise walk the frames and retrieve the
619 saved %pc value in the previous frame. */
621 addr = get_pc_function_start (frame->pc) + FUNCTION_START_OFFSET;
622 (void) skip_prologue (addr, &fdata);
624 if (fdata.frameless)
625 prev_sp = sp;
626 else
627 prev_sp = read_memory_integer (sp, 4);
628 if (fdata.lr_offset == 0)
629 lr = read_register (LR_REGNUM);
630 else
631 lr = read_memory_integer (prev_sp + fdata.lr_offset, 4);
633 /* reset %pc value. */
634 write_register (PC_REGNUM, lr);
636 /* reset register values if any was saved earlier. */
637 addr = prev_sp - fdata.offset;
639 if (fdata.saved_gpr != -1)
640 for (ii = fdata.saved_gpr; ii <= 31; ++ii) {
641 read_memory (addr, &registers [REGISTER_BYTE (ii)], 4);
642 addr += 4;
645 if (fdata.saved_fpr != -1)
646 for (ii = fdata.saved_fpr; ii <= 31; ++ii) {
647 read_memory (addr, &registers [REGISTER_BYTE (ii+FP0_REGNUM)], 8);
648 addr += 8;
651 write_register (SP_REGNUM, prev_sp);
652 target_store_registers (-1);
653 flush_cached_frames ();
656 /* fixup the call sequence of a dummy function, with the real function address.
657 its argumets will be passed by gdb. */
659 void
660 rs6000_fix_call_dummy (dummyname, pc, fun, nargs, args, type, gcc_p)
661 char *dummyname;
662 CORE_ADDR pc;
663 CORE_ADDR fun;
664 int nargs;
665 value_ptr *args;
666 struct type *type;
667 int gcc_p;
669 #define TOC_ADDR_OFFSET 20
670 #define TARGET_ADDR_OFFSET 28
672 int ii;
673 CORE_ADDR target_addr;
675 if (find_toc_address_hook != NULL)
677 CORE_ADDR tocvalue;
679 tocvalue = (*find_toc_address_hook) (fun);
680 ii = *(int*)((char*)dummyname + TOC_ADDR_OFFSET);
681 ii = (ii & 0xffff0000) | (tocvalue >> 16);
682 *(int*)((char*)dummyname + TOC_ADDR_OFFSET) = ii;
684 ii = *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4);
685 ii = (ii & 0xffff0000) | (tocvalue & 0x0000ffff);
686 *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4) = ii;
689 target_addr = fun;
690 ii = *(int*)((char*)dummyname + TARGET_ADDR_OFFSET);
691 ii = (ii & 0xffff0000) | (target_addr >> 16);
692 *(int*)((char*)dummyname + TARGET_ADDR_OFFSET) = ii;
694 ii = *(int*)((char*)dummyname + TARGET_ADDR_OFFSET+4);
695 ii = (ii & 0xffff0000) | (target_addr & 0x0000ffff);
696 *(int*)((char*)dummyname + TARGET_ADDR_OFFSET+4) = ii;
699 /* Pass the arguments in either registers, or in the stack. In RS6000,
700 the first eight words of the argument list (that might be less than
701 eight parameters if some parameters occupy more than one word) are
702 passed in r3..r11 registers. float and double parameters are
703 passed in fpr's, in addition to that. Rest of the parameters if any
704 are passed in user stack. There might be cases in which half of the
705 parameter is copied into registers, the other half is pushed into
706 stack.
708 If the function is returning a structure, then the return address is passed
709 in r3, then the first 7 words of the parameters can be passed in registers,
710 starting from r4. */
712 CORE_ADDR
713 push_arguments (nargs, args, sp, struct_return, struct_addr)
714 int nargs;
715 value_ptr *args;
716 CORE_ADDR sp;
717 int struct_return;
718 CORE_ADDR struct_addr;
720 int ii;
721 int len = 0;
722 int argno; /* current argument number */
723 int argbytes; /* current argument byte */
724 char tmp_buffer [50];
725 int f_argno = 0; /* current floating point argno */
726 value_ptr arg = 0;
727 struct type *type;
729 CORE_ADDR saved_sp;
731 if ( dummy_frame_count <= 0)
732 printf_unfiltered ("FATAL ERROR -push_arguments()! frame not found!!\n");
734 /* The first eight words of ther arguments are passed in registers. Copy
735 them appropriately.
737 If the function is returning a `struct', then the first word (which
738 will be passed in r3) is used for struct return address. In that
739 case we should advance one word and start from r4 register to copy
740 parameters. */
742 ii = struct_return ? 1 : 0;
744 for (argno=0, argbytes=0; argno < nargs && ii<8; ++ii) {
746 arg = args[argno];
747 type = check_typedef (VALUE_TYPE (arg));
748 len = TYPE_LENGTH (type);
750 if (TYPE_CODE (type) == TYPE_CODE_FLT) {
752 /* floating point arguments are passed in fpr's, as well as gpr's.
753 There are 13 fpr's reserved for passing parameters. At this point
754 there is no way we would run out of them. */
756 if (len > 8)
757 printf_unfiltered (
758 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
760 memcpy (&registers[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)], VALUE_CONTENTS (arg),
761 len);
762 ++f_argno;
765 if (len > 4) {
767 /* Argument takes more than one register. */
768 while (argbytes < len) {
770 *(int*)&registers[REGISTER_BYTE(ii+3)] = 0;
771 memcpy (&registers[REGISTER_BYTE(ii+3)],
772 ((char*)VALUE_CONTENTS (arg))+argbytes,
773 (len - argbytes) > 4 ? 4 : len - argbytes);
774 ++ii, argbytes += 4;
776 if (ii >= 8)
777 goto ran_out_of_registers_for_arguments;
779 argbytes = 0;
780 --ii;
782 else { /* Argument can fit in one register. No problem. */
783 *(int*)&registers[REGISTER_BYTE(ii+3)] = 0;
784 memcpy (&registers[REGISTER_BYTE(ii+3)], VALUE_CONTENTS (arg), len);
786 ++argno;
789 ran_out_of_registers_for_arguments:
791 /* location for 8 parameters are always reserved. */
792 sp -= 4 * 8;
794 /* another six words for back chain, TOC register, link register, etc. */
795 sp -= 24;
797 /* if there are more arguments, allocate space for them in
798 the stack, then push them starting from the ninth one. */
800 if ((argno < nargs) || argbytes) {
801 int space = 0, jj;
803 if (argbytes) {
804 space += ((len - argbytes + 3) & -4);
805 jj = argno + 1;
807 else
808 jj = argno;
810 for (; jj < nargs; ++jj) {
811 value_ptr val = args[jj];
812 space += ((TYPE_LENGTH (VALUE_TYPE (val))) + 3) & -4;
815 /* add location required for the rest of the parameters */
816 space = (space + 7) & -8;
817 sp -= space;
819 /* This is another instance we need to be concerned about securing our
820 stack space. If we write anything underneath %sp (r1), we might conflict
821 with the kernel who thinks he is free to use this area. So, update %sp
822 first before doing anything else. */
824 write_register (SP_REGNUM, sp);
826 /* if the last argument copied into the registers didn't fit there
827 completely, push the rest of it into stack. */
829 if (argbytes) {
830 write_memory (
831 sp+24+(ii*4), ((char*)VALUE_CONTENTS (arg))+argbytes, len - argbytes);
832 ++argno;
833 ii += ((len - argbytes + 3) & -4) / 4;
836 /* push the rest of the arguments into stack. */
837 for (; argno < nargs; ++argno) {
839 arg = args[argno];
840 type = check_typedef (VALUE_TYPE (arg));
841 len = TYPE_LENGTH (type);
844 /* float types should be passed in fpr's, as well as in the stack. */
845 if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13) {
847 if (len > 8)
848 printf_unfiltered (
849 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
851 memcpy (&registers[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)], VALUE_CONTENTS (arg),
852 len);
853 ++f_argno;
856 write_memory (sp+24+(ii*4), (char *) VALUE_CONTENTS (arg), len);
857 ii += ((len + 3) & -4) / 4;
860 else
861 /* Secure stack areas first, before doing anything else. */
862 write_register (SP_REGNUM, sp);
864 saved_sp = dummy_frame_addr [dummy_frame_count - 1];
865 read_memory (saved_sp, tmp_buffer, 24);
866 write_memory (sp, tmp_buffer, 24);
868 /* set back chain properly */
869 store_address (tmp_buffer, 4, saved_sp);
870 write_memory (sp, tmp_buffer, 4);
872 target_store_registers (-1);
873 return sp;
876 /* a given return value in `regbuf' with a type `valtype', extract and copy its
877 value into `valbuf' */
879 void
880 extract_return_value (valtype, regbuf, valbuf)
881 struct type *valtype;
882 char regbuf[REGISTER_BYTES];
883 char *valbuf;
885 int offset = 0;
887 if (TYPE_CODE (valtype) == TYPE_CODE_FLT) {
889 double dd; float ff;
890 /* floats and doubles are returned in fpr1. fpr's have a size of 8 bytes.
891 We need to truncate the return value into float size (4 byte) if
892 necessary. */
894 if (TYPE_LENGTH (valtype) > 4) /* this is a double */
895 memcpy (valbuf, &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)],
896 TYPE_LENGTH (valtype));
897 else { /* float */
898 memcpy (&dd, &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)], 8);
899 ff = (float)dd;
900 memcpy (valbuf, &ff, sizeof(float));
903 else {
904 /* return value is copied starting from r3. */
905 if (TARGET_BYTE_ORDER == BIG_ENDIAN
906 && TYPE_LENGTH (valtype) < REGISTER_RAW_SIZE (3))
907 offset = REGISTER_RAW_SIZE (3) - TYPE_LENGTH (valtype);
909 memcpy (valbuf, regbuf + REGISTER_BYTE (3) + offset,
910 TYPE_LENGTH (valtype));
915 /* keep structure return address in this variable.
916 FIXME: This is a horrid kludge which should not be allowed to continue
917 living. This only allows a single nested call to a structure-returning
918 function. Come on, guys! -- gnu@cygnus.com, Aug 92 */
920 CORE_ADDR rs6000_struct_return_address;
923 /* Indirect function calls use a piece of trampoline code to do context
924 switching, i.e. to set the new TOC table. Skip such code if we are on
925 its first instruction (as when we have single-stepped to here).
926 Also skip shared library trampoline code (which is different from
927 indirect function call trampolines).
928 Result is desired PC to step until, or NULL if we are not in
929 trampoline code. */
931 CORE_ADDR
932 skip_trampoline_code (pc)
933 CORE_ADDR pc;
935 register unsigned int ii, op;
936 CORE_ADDR solib_target_pc;
938 static unsigned trampoline_code[] = {
939 0x800b0000, /* l r0,0x0(r11) */
940 0x90410014, /* st r2,0x14(r1) */
941 0x7c0903a6, /* mtctr r0 */
942 0x804b0004, /* l r2,0x4(r11) */
943 0x816b0008, /* l r11,0x8(r11) */
944 0x4e800420, /* bctr */
945 0x4e800020, /* br */
949 /* If pc is in a shared library trampoline, return its target. */
950 solib_target_pc = find_solib_trampoline_target (pc);
951 if (solib_target_pc)
952 return solib_target_pc;
954 for (ii=0; trampoline_code[ii]; ++ii) {
955 op = read_memory_integer (pc + (ii*4), 4);
956 if (op != trampoline_code [ii])
957 return 0;
959 ii = read_register (11); /* r11 holds destination addr */
960 pc = read_memory_integer (ii, 4); /* (r11) value */
961 return pc;
964 /* Determines whether the function FI has a frame on the stack or not. */
967 frameless_function_invocation (fi)
968 struct frame_info *fi;
970 CORE_ADDR func_start;
971 struct rs6000_framedata fdata;
973 /* Don't even think about framelessness except on the innermost frame
974 or if the function was interrupted by a signal. */
975 if (fi->next != NULL && !fi->next->signal_handler_caller)
976 return 0;
978 func_start = get_pc_function_start (fi->pc);
980 /* If we failed to find the start of the function, it is a mistake
981 to inspect the instructions. */
983 if (!func_start)
985 /* A frame with a zero PC is usually created by dereferencing a NULL
986 function pointer, normally causing an immediate core dump of the
987 inferior. Mark function as frameless, as the inferior has no chance
988 of setting up a stack frame. */
989 if (fi->pc == 0)
990 return 1;
991 else
992 return 0;
995 func_start += FUNCTION_START_OFFSET;
996 (void) skip_prologue (func_start, &fdata);
997 return fdata.frameless;
1000 /* Return the PC saved in a frame */
1002 unsigned long
1003 frame_saved_pc (fi)
1004 struct frame_info *fi;
1006 CORE_ADDR func_start;
1007 struct rs6000_framedata fdata;
1009 if (fi->signal_handler_caller)
1010 return read_memory_integer (fi->frame + SIG_FRAME_PC_OFFSET, 4);
1012 func_start = get_pc_function_start (fi->pc) + FUNCTION_START_OFFSET;
1014 /* If we failed to find the start of the function, it is a mistake
1015 to inspect the instructions. */
1016 if (!func_start)
1017 return 0;
1019 (void) skip_prologue (func_start, &fdata);
1021 if (fdata.lr_offset == 0 && fi->next != NULL)
1023 if (fi->next->signal_handler_caller)
1024 return read_memory_integer (fi->next->frame + SIG_FRAME_LR_OFFSET, 4);
1025 else
1026 return read_memory_integer (rs6000_frame_chain (fi) + DEFAULT_LR_SAVE,
1030 if (fdata.lr_offset == 0)
1031 return read_register (LR_REGNUM);
1033 return read_memory_integer (rs6000_frame_chain (fi) + fdata.lr_offset, 4);
1036 /* If saved registers of frame FI are not known yet, read and cache them.
1037 &FDATAP contains rs6000_framedata; TDATAP can be NULL,
1038 in which case the framedata are read. */
1040 static void
1041 frame_get_cache_fsr (fi, fdatap)
1042 struct frame_info *fi;
1043 struct rs6000_framedata *fdatap;
1045 int ii;
1046 CORE_ADDR frame_addr;
1047 struct rs6000_framedata work_fdata;
1049 if (fi->cache_fsr)
1050 return;
1052 if (fdatap == NULL) {
1053 fdatap = &work_fdata;
1054 (void) skip_prologue (get_pc_function_start (fi->pc), fdatap);
1057 fi->cache_fsr = (struct frame_saved_regs *)
1058 obstack_alloc (&frame_cache_obstack, sizeof (struct frame_saved_regs));
1059 memset (fi->cache_fsr, '\0', sizeof (struct frame_saved_regs));
1061 if (fi->prev && fi->prev->frame)
1062 frame_addr = fi->prev->frame;
1063 else
1064 frame_addr = read_memory_integer (fi->frame, 4);
1066 /* if != -1, fdatap->saved_fpr is the smallest number of saved_fpr.
1067 All fpr's from saved_fpr to fp31 are saved. */
1069 if (fdatap->saved_fpr >= 0) {
1070 int fpr_offset = frame_addr + fdatap->fpr_offset;
1071 for (ii = fdatap->saved_fpr; ii < 32; ii++) {
1072 fi->cache_fsr->regs [FP0_REGNUM + ii] = fpr_offset;
1073 fpr_offset += 8;
1077 /* if != -1, fdatap->saved_gpr is the smallest number of saved_gpr.
1078 All gpr's from saved_gpr to gpr31 are saved. */
1080 if (fdatap->saved_gpr >= 0) {
1081 int gpr_offset = frame_addr + fdatap->gpr_offset;
1082 for (ii = fdatap->saved_gpr; ii < 32; ii++) {
1083 fi->cache_fsr->regs [ii] = gpr_offset;
1084 gpr_offset += 4;
1088 /* If != 0, fdatap->cr_offset is the offset from the frame that holds
1089 the CR. */
1090 if (fdatap->cr_offset != 0)
1091 fi->cache_fsr->regs [CR_REGNUM] = frame_addr + fdatap->cr_offset;
1093 /* If != 0, fdatap->lr_offset is the offset from the frame that holds
1094 the LR. */
1095 if (fdatap->lr_offset != 0)
1096 fi->cache_fsr->regs [LR_REGNUM] = frame_addr + fdatap->lr_offset;
1099 /* Return the address of a frame. This is the inital %sp value when the frame
1100 was first allocated. For functions calling alloca(), it might be saved in
1101 an alloca register. */
1103 CORE_ADDR
1104 frame_initial_stack_address (fi)
1105 struct frame_info *fi;
1107 CORE_ADDR tmpaddr;
1108 struct rs6000_framedata fdata;
1109 struct frame_info *callee_fi;
1111 /* if the initial stack pointer (frame address) of this frame is known,
1112 just return it. */
1114 if (fi->initial_sp)
1115 return fi->initial_sp;
1117 /* find out if this function is using an alloca register.. */
1119 (void) skip_prologue (get_pc_function_start (fi->pc), &fdata);
1121 /* if saved registers of this frame are not known yet, read and cache them. */
1123 if (!fi->cache_fsr)
1124 frame_get_cache_fsr (fi, &fdata);
1126 /* If no alloca register used, then fi->frame is the value of the %sp for
1127 this frame, and it is good enough. */
1129 if (fdata.alloca_reg < 0) {
1130 fi->initial_sp = fi->frame;
1131 return fi->initial_sp;
1134 /* This function has an alloca register. If this is the top-most frame
1135 (with the lowest address), the value in alloca register is good. */
1137 if (!fi->next)
1138 return fi->initial_sp = read_register (fdata.alloca_reg);
1140 /* Otherwise, this is a caller frame. Callee has usually already saved
1141 registers, but there are exceptions (such as when the callee
1142 has no parameters). Find the address in which caller's alloca
1143 register is saved. */
1145 for (callee_fi = fi->next; callee_fi; callee_fi = callee_fi->next) {
1147 if (!callee_fi->cache_fsr)
1148 frame_get_cache_fsr (callee_fi, NULL);
1150 /* this is the address in which alloca register is saved. */
1152 tmpaddr = callee_fi->cache_fsr->regs [fdata.alloca_reg];
1153 if (tmpaddr) {
1154 fi->initial_sp = read_memory_integer (tmpaddr, 4);
1155 return fi->initial_sp;
1158 /* Go look into deeper levels of the frame chain to see if any one of
1159 the callees has saved alloca register. */
1162 /* If alloca register was not saved, by the callee (or any of its callees)
1163 then the value in the register is still good. */
1165 return fi->initial_sp = read_register (fdata.alloca_reg);
1168 CORE_ADDR
1169 rs6000_frame_chain (thisframe)
1170 struct frame_info *thisframe;
1172 CORE_ADDR fp;
1173 if (inside_entry_file ((thisframe)->pc))
1174 return 0;
1175 if (thisframe->signal_handler_caller)
1176 fp = read_memory_integer (thisframe->frame + SIG_FRAME_FP_OFFSET, 4);
1177 else if (thisframe->next != NULL
1178 && thisframe->next->signal_handler_caller
1179 && frameless_function_invocation (thisframe))
1180 /* A frameless function interrupted by a signal did not change the
1181 frame pointer. */
1182 fp = FRAME_FP (thisframe);
1183 else
1184 fp = read_memory_integer ((thisframe)->frame, 4);
1186 return fp;
1189 /* Return nonzero if ADDR (a function pointer) is in the data space and
1190 is therefore a special function pointer. */
1193 is_magic_function_pointer (addr)
1194 CORE_ADDR addr;
1196 struct obj_section *s;
1198 s = find_pc_section (addr);
1199 if (s && s->the_bfd_section->flags & SEC_CODE)
1200 return 0;
1201 else
1202 return 1;
1205 #ifdef GDB_TARGET_POWERPC
1207 gdb_print_insn_powerpc (memaddr, info)
1208 bfd_vma memaddr;
1209 disassemble_info *info;
1211 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1212 return print_insn_big_powerpc (memaddr, info);
1213 else
1214 return print_insn_little_powerpc (memaddr, info);
1216 #endif
1218 void
1219 _initialize_rs6000_tdep ()
1221 /* FIXME, this should not be decided via ifdef. */
1222 #ifdef GDB_TARGET_POWERPC
1223 tm_print_insn = gdb_print_insn_powerpc;
1224 #else
1225 tm_print_insn = print_insn_rs6000;
1226 #endif