1999-01-19 Fernando Nasser <fnasser@totem.to.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / rs6000-tdep.c
CommitLineData
41abdfbd 1/* Target-dependent code for GDB, the GNU debugger.
211b564e 2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997
07aa9fdc 3 Free Software Foundation, Inc.
41abdfbd
JG
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
6c9638b4 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
41abdfbd 20
41abdfbd 21#include "defs.h"
41abdfbd
JG
22#include "frame.h"
23#include "inferior.h"
24#include "symtab.h"
25#include "target.h"
030fb5cb 26#include "gdbcore.h"
65eaea27
JL
27#include "symfile.h"
28#include "objfiles.h"
2aefe6e4
JK
29#include "xcoffsolib.h"
30
41abdfbd 31extern int errno;
41abdfbd 32
41abdfbd
JG
33/* Breakpoint shadows for the single step instructions will be kept here. */
34
35static struct sstep_breaks {
030fb5cb
JK
36 /* Address, or 0 if this is not in use. */
37 CORE_ADDR address;
38 /* Shadow contents. */
39 char data[4];
41abdfbd
JG
40} stepBreaks[2];
41
05d52ace
PS
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. */
45
46CORE_ADDR (*find_toc_address_hook) PARAMS ((CORE_ADDR)) = NULL;
ecf4059f 47
05d52ace 48/* Static function prototypes */
ecf4059f 49
cd8a3d84
SS
50static CORE_ADDR branch_dest PARAMS ((int opcode, int instr, CORE_ADDR pc,
51 CORE_ADDR safety));
ecf4059f 52
86a51f41
AC
53static void frame_get_saved_regs PARAMS ((struct frame_info *fi,
54 struct rs6000_framedata *fdatap));
cd8a3d84 55
368f1e77
FF
56static void pop_dummy_frame PARAMS ((void));
57
86a51f41
AC
58static CORE_ADDR frame_initial_stack_address PARAMS ((struct frame_info *));
59
60/* Fill in fi->saved_regs */
61
62struct frame_extra_info
63{
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. */ \
70};
71
72void
73rs6000_init_extra_frame_info (fromleaf, fi)
74 int fromleaf;
75 struct frame_info *fi;
76{
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;
87}
88
89
90void
91rs6000_frame_init_saved_regs (fi)
92 struct frame_info *fi;
93{
94 frame_get_saved_regs (fi, NULL);
95}
96
97CORE_ADDR
98rs6000_frame_args_address (fi)
99 struct frame_info *fi;
100{
101 if (fi->extra_info->initial_sp != 0)
102 return fi->extra_info->initial_sp;
103 else
104 return frame_initial_stack_address (fi);
105}
106
107
cd8a3d84 108/* Calculate the destination of a branch/jump. Return -1 if not a branch. */
41abdfbd 109
ecf4059f 110static CORE_ADDR
41abdfbd 111branch_dest (opcode, instr, pc, safety)
ecf4059f
JG
112 int opcode;
113 int instr;
114 CORE_ADDR pc;
115 CORE_ADDR safety;
41abdfbd 116{
ecf4059f 117 CORE_ADDR dest;
41abdfbd
JG
118 int immediate;
119 int absolute;
120 int ext_op;
121
122 absolute = (int) ((instr >> 1) & 1);
123
124 switch (opcode) {
125 case 18 :
ecf4059f 126 immediate = ((instr & ~3) << 6) >> 6; /* br unconditional */
dc59e982
MM
127 if (absolute)
128 dest = immediate;
129 else
130 dest = pc + immediate;
131 break;
41abdfbd
JG
132
133 case 16 :
dc59e982 134 immediate = ((instr & ~3) << 16) >> 16; /* br conditional */
41abdfbd
JG
135 if (absolute)
136 dest = immediate;
137 else
138 dest = pc + immediate;
139 break;
140
141 case 19 :
142 ext_op = (instr>>1) & 0x3ff;
143
144 if (ext_op == 16) /* br conditional register */
0c6c5eeb
PS
145 {
146 dest = read_register (LR_REGNUM) & ~3;
147
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)
153 {
154 struct frame_info *fi;
155
156 fi = get_current_frame ();
157 if (fi != NULL)
158 dest = read_memory_integer (fi->frame + SIG_FRAME_PC_OFFSET,
159 4);
160 }
161 }
41abdfbd
JG
162
163 else if (ext_op == 528) /* br cond to count reg */
9aa31e91
JK
164 {
165 dest = read_register (CTR_REGNUM) & ~3;
166
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;
172 }
41abdfbd
JG
173 else return -1;
174 break;
175
176 default: return -1;
177 }
818de002 178 return (dest < TEXT_SEGMENT_BASE) ? safety : dest;
41abdfbd
JG
179}
180
181
afcad54a
AC
182/* Sequence of bytes for breakpoint instruction. */
183
184#define BIG_BREAKPOINT { 0x7d, 0x82, 0x10, 0x08 }
185#define LITTLE_BREAKPOINT { 0x08, 0x10, 0x82, 0x7d }
186
187unsigned char *
188rs6000_breakpoint_from_pc (bp_addr, bp_size)
189 CORE_ADDR *bp_addr;
190 int *bp_size;
191{
07137a11
AC
192 static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
193 static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
afcad54a
AC
194 *bp_size = 4;
195 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
196 return big_breakpoint;
197 else
198 return little_breakpoint;
199}
200
41abdfbd
JG
201
202/* AIX does not support PT_STEP. Simulate it. */
203
997cc2c0 204void
02331869 205rs6000_software_single_step (signal, insert_breakpoints_p)
b607efe7 206 enum target_signal signal;
02331869 207 int insert_breakpoints_p;
41abdfbd
JG
208{
209#define INSNLEN(OPCODE) 4
210
5c172b4b
MM
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;
030fb5cb
JK
214 int ii, insn;
215 CORE_ADDR loc;
216 CORE_ADDR breaks[2];
217 int opcode;
41abdfbd 218
02331869
AC
219 if (insert_breakpoints_p) {
220
41abdfbd
JG
221 loc = read_pc ();
222
b112f2ae 223 insn = read_memory_integer (loc, 4);
41abdfbd
JG
224
225 breaks[0] = loc + INSNLEN(insn);
226 opcode = insn >> 26;
227 breaks[1] = branch_dest (opcode, insn, loc, breaks[0]);
228
818de002
PB
229 /* Don't put two breakpoints on the same address. */
230 if (breaks[1] == breaks[0])
231 breaks[1] = -1;
232
030fb5cb 233 stepBreaks[1].address = 0;
41abdfbd
JG
234
235 for (ii=0; ii < 2; ++ii) {
236
237 /* ignore invalid breakpoint. */
238 if ( breaks[ii] == -1)
239 continue;
240
030fb5cb 241 read_memory (breaks[ii], stepBreaks[ii].data, 4);
41abdfbd 242
030fb5cb 243 write_memory (breaks[ii], breakp, 4);
41abdfbd
JG
244 stepBreaks[ii].address = breaks[ii];
245 }
246
997cc2c0 247 } else {
41abdfbd
JG
248
249 /* remove step breakpoints. */
250 for (ii=0; ii < 2; ++ii)
030fb5cb 251 if (stepBreaks[ii].address != 0)
41abdfbd 252 write_memory
030fb5cb 253 (stepBreaks[ii].address, stepBreaks[ii].data, 4);
41abdfbd 254
41abdfbd 255 }
997cc2c0 256 errno = 0; /* FIXME, don't ignore errors! */
030fb5cb 257 /* What errors? {read,write}_memory call error(). */
41abdfbd 258}
41abdfbd
JG
259
260
068c9fd6
MM
261/* return pc value after skipping a function prologue and also return
262 information about a function frame.
41abdfbd 263
63d7a4f1 264 in struct rs6000_framedata fdata:
068c9fd6
MM
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.
63d7a4f1
JB
267 - offset is the initial size of this stack frame --- the amount by
268 which we decrement the sp to allocate the frame.
068c9fd6
MM
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.
63d7a4f1
JB
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.
068c9fd6
MM
275 - lr_offset is the offset of the saved lr
276 - cr_offset is the offset of the saved cr
63d7a4f1 277*/
068c9fd6
MM
278
279#define SIGNED_SHORT(x) \
280 ((sizeof (short) == 2) \
281 ? ((int)(short)(x)) \
282 : ((int)((((x) & 0xffff) ^ 0x8000) - 0x8000)))
283
284#define GET_SRC_REG(x) (((x) >> 21) & 0x1f)
285
286CORE_ADDR
287skip_prologue (pc, fdata)
288 CORE_ADDR pc;
289 struct rs6000_framedata *fdata;
41abdfbd 290{
068c9fd6 291 CORE_ADDR orig_pc = pc;
34df79fc 292 char buf[4];
34df79fc 293 unsigned long op;
4b4c6c96 294 long offset = 0;
068c9fd6
MM
295 int lr_reg = 0;
296 int cr_reg = 0;
297 int reg;
4b4c6c96 298 int framep = 0;
65eaea27 299 int minimal_toc_loaded = 0;
068c9fd6
MM
300 static struct rs6000_framedata zero_frame;
301
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;
41abdfbd 308
34df79fc 309 if (target_read_memory (pc, buf, 4))
41abdfbd 310 return pc; /* Can't access it -- assume no prologue. */
41abdfbd
JG
311
312 /* Assume that subsequent fetches can fail with low probability. */
068c9fd6
MM
313 pc -= 4;
314 for (;;)
315 {
316 pc += 4;
317 op = read_memory_integer (pc, 4);
41abdfbd 318
068c9fd6
MM
319 if ((op & 0xfc1fffff) == 0x7c0802a6) { /* mflr Rx */
320 lr_reg = (op & 0x03e00000) | 0x90010000;
321 continue;
322
323 } else if ((op & 0xfc1fffff) == 0x7c000026) { /* mfcr Rx */
324 cr_reg = (op & 0x03e00000) | 0x90010000;
325 continue;
326
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;
4b4c6c96 331 fdata->fpr_offset = SIGNED_SHORT (op) + offset;
068c9fd6
MM
332 }
333 continue;
334
335 } else if (((op & 0xfc1f0000) == 0xbc010000) || /* stm Rx, NUM(r1) */
75621b2b
MS
336 ((op & 0xfc1f0000) == 0x90010000 && /* st rx,NUM(r1),
337 rx >= r13 */
068c9fd6
MM
338 (op & 0x03e00000) >= 0x01a00000)) {
339
340 reg = GET_SRC_REG (op);
341 if (fdata->saved_gpr == -1 || fdata->saved_gpr > reg) {
342 fdata->saved_gpr = reg;
4b4c6c96 343 fdata->gpr_offset = SIGNED_SHORT (op) + offset;
068c9fd6
MM
344 }
345 continue;
346
75621b2b
MS
347 } else if ((op & 0xffff0000) == 0x3c000000) { /* addis 0,0,NUM, used
348 for >= 32k frames */
068c9fd6 349 fdata->offset = (op & 0x0000ffff) << 16;
65eaea27 350 fdata->frameless = 0;
068c9fd6
MM
351 continue;
352
75621b2b
MS
353 } else if ((op & 0xffff0000) == 0x60000000) { /* ori 0,0,NUM, 2nd ha
354 lf of >= 32k frames */
068c9fd6 355 fdata->offset |= (op & 0x0000ffff);
65eaea27 356 fdata->frameless = 0;
068c9fd6
MM
357 continue;
358
75621b2b
MS
359 } else if ((op & 0xffff0000) == lr_reg) { /* st Rx,NUM(r1)
360 where Rx == lr */
4b4c6c96 361 fdata->lr_offset = SIGNED_SHORT (op) + offset;
068c9fd6
MM
362 fdata->nosavedpc = 0;
363 lr_reg = 0;
364 continue;
365
75621b2b
MS
366 } else if ((op & 0xffff0000) == cr_reg) { /* st Rx,NUM(r1)
367 where Rx == cr */
4b4c6c96 368 fdata->cr_offset = SIGNED_SHORT (op) + offset;
068c9fd6
MM
369 cr_reg = 0;
370 continue;
371
75621b2b
MS
372 } else if (op == 0x48000005) { /* bl .+4 used in
373 -mrelocatable */
4b4c6c96
MM
374 continue;
375
65eaea27
JL
376 } else if (op == 0x48000004) { /* b .+4 (xlc) */
377 break;
378
75621b2b
MS
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 */
4b4c6c96
MM
383 lr_reg == 0x901e0000) {
384 continue;
385
75621b2b
MS
386 } else if ((op & 0xffff0000) == 0x3fc00000 || /* addis 30,0,foo@ha, used
387 in V.4 -mminimal-toc */
4b4c6c96
MM
388 (op & 0xffff0000) == 0x3bde0000) { /* addi 30,30,foo@l */
389 continue;
390
75621b2b
MS
391 } else if ((op & 0xfc000000) == 0x48000000) { /* bl foo,
392 to save fprs??? */
965dde97 393
65eaea27 394 fdata->frameless = 0;
965dde97
PS
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;
399
068c9fd6
MM
400 op = read_memory_integer (pc+4, 4);
401
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. */
406
407 if (op == 0x4def7b82 || op == 0) /* crorc 15, 15, 15 */
75621b2b
MS
408 break; /* don't skip over
409 this branch */
068c9fd6
MM
410 continue;
411
4b4c6c96 412 /* update stack pointer */
068c9fd6 413 } else if ((op & 0xffff0000) == 0x94210000) { /* stu r1,NUM(r1) */
65eaea27 414 fdata->frameless = 0;
4b4c6c96
MM
415 fdata->offset = SIGNED_SHORT (op);
416 offset = fdata->offset;
417 continue;
1eeba686 418
068c9fd6 419 } else if (op == 0x7c21016e) { /* stwux 1,1,0 */
65eaea27 420 fdata->frameless = 0;
4b4c6c96
MM
421 offset = fdata->offset;
422 continue;
41abdfbd 423
4b4c6c96 424 /* Load up minimal toc pointer */
65eaea27
JL
425 } else if ((op >> 22) == 0x20f
426 && ! minimal_toc_loaded) { /* l r31,... or l r30,... */
427 minimal_toc_loaded = 1;
4b4c6c96 428 continue;
cdb1cc92 429
4b4c6c96
MM
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;
e137e850 435
4b4c6c96
MM
436 /* store parameters in stack via frame pointer */
437 } else if (framep &&
58b4fad2 438 ((op & 0xfc1f0000) == 0x901f0000 || /* st rx,NUM(r1) */
4b4c6c96 439 (op & 0xfc1f0000) == 0xd81f0000 || /* stfd Rx,NUM(r1) */
58b4fad2 440 (op & 0xfc1f0000) == 0xfc1f0000)) { /* frsp, fp?,NUM(r1) */
4b4c6c96 441 continue;
e137e850 442
4b4c6c96
MM
443 /* Set up frame pointer */
444 } else if (op == 0x603f0000 /* oril r31, r1, 0x0 */
445 || op == 0x7c3f0b78) { /* mr r31, r1 */
65eaea27 446 fdata->frameless = 0;
4b4c6c96 447 framep = 1;
965dde97 448 fdata->alloca_reg = 31;
4b4c6c96 449 continue;
41abdfbd 450
65eaea27
JL
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;
457
4b4c6c96
MM
458 } else {
459 break;
460 }
41abdfbd 461 }
068c9fd6 462
507e4004
PB
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 */
467
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. */
473
474
475 if ((op & 0xfc000001) == 0x48000001) { /* bl foo, an initializer function? */
476 op = read_memory_integer (pc+4, 4);
477
478 if (op == 0x4def7b82) { /* cror 0xf, 0xf, 0xf (nop) */
479
480 /* check and see if we are in main. If so, skip over this initializer
481 function as well. */
482
483 tmp = find_pc_misc_function (pc);
2e4964ad 484 if (tmp >= 0 && STREQ (misc_function_vector [tmp].name, "main"))
507e4004
PB
485 return pc + 8;
486 }
487 }
488#endif /* 0 */
489
4b4c6c96 490 fdata->offset = - fdata->offset;
41abdfbd
JG
491 return pc;
492}
493
818de002 494
41abdfbd
JG
495/*************************************************************************
496 Support for creating pushind a dummy frame into the stack, and popping
497 frames, etc.
498*************************************************************************/
499
818de002
PB
500/* The total size of dummy frame is 436, which is;
501
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(). */
508
509#define DUMMY_FRAME_SIZE 436
510
41abdfbd
JG
511#define DUMMY_FRAME_ADDR_SIZE 10
512
513/* Make sure you initialize these in somewhere, in case gdb gives up what it
818de002 514 was debugging and starts debugging something else. FIXMEibm */
41abdfbd
JG
515
516static int dummy_frame_count = 0;
517static int dummy_frame_size = 0;
518static CORE_ADDR *dummy_frame_addr = 0;
519
520extern int stop_stack_dummy;
521
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 */
524
ecf4059f 525void
41abdfbd
JG
526push_dummy_frame ()
527{
359a097f
JK
528 /* stack pointer. */
529 CORE_ADDR sp;
b112f2ae
JK
530 /* Same thing, target byte order. */
531 char sp_targ[4];
359a097f
JK
532
533 /* link register. */
534 CORE_ADDR pc;
535 /* Same thing, target byte order. */
536 char pc_targ[4];
537
3a4f9786
MT
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;
541
41abdfbd
JG
542 int ii;
543
5f1c39ef 544 target_fetch_registers (-1);
6c6afbb9 545
41abdfbd
JG
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));
554 }
555
556 sp = read_register(SP_REGNUM);
359a097f 557 pc = read_register(PC_REGNUM);
5816555b 558 store_address (pc_targ, 4, pc);
41abdfbd 559
86a51f41 560 skip_prologue (get_pc_function_start (pc), &fdata);
3a4f9786 561
41abdfbd
JG
562 dummy_frame_addr [dummy_frame_count++] = sp;
563
564 /* Be careful! If the stack pointer is not decremented first, then kernel
6c6afbb9 565 thinks he is free to use the space underneath it. And kernel actually
41abdfbd
JG
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. */
569
3a4f9786
MT
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. */
573
818de002 574 write_register (SP_REGNUM, sp-DUMMY_FRAME_SIZE);
41abdfbd 575
41abdfbd
JG
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! */
578
579 flush_cached_frames ();
41abdfbd
JG
580
581 /* save program counter in link register's space. */
4f8710e6
PS
582 write_memory (sp + (fdata.lr_offset ? fdata.lr_offset : DEFAULT_LR_SAVE),
583 pc_targ, 4);
41abdfbd 584
6c6afbb9 585 /* save all floating point and general purpose registers here. */
41abdfbd
JG
586
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);
590
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);
594
818de002
PB
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 */
597
598 for (ii=1; ii <= (LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii) {
599 write_memory (sp-384-(ii*4),
75621b2b 600 &registers[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4);
818de002
PB
601 }
602
603 /* Save sp or so called back chain right here. */
b112f2ae
JK
604 store_address (sp_targ, 4, sp);
605 write_memory (sp-DUMMY_FRAME_SIZE, sp_targ, 4);
818de002 606 sp -= DUMMY_FRAME_SIZE;
41abdfbd
JG
607
608 /* And finally, this is the back chain. */
359a097f 609 write_memory (sp+8, pc_targ, 4);
41abdfbd
JG
610}
611
612
613/* Pop a dummy frame.
614
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.
617
818de002
PB
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).
ecf4059f
JG
625
626FIXME: This whole concept is broken. You should be able to detect
627a dummy stack frame *on the user's stack itself*. When you do,
628then you know the format of that stack frame -- including its
629saved SP register! There should *not* be a separate stack in the
d6434f39 630GDB process that keeps track of these dummy frames! -- gnu@cygnus.com Aug92
41abdfbd
JG
631 */
632
368f1e77 633static void
41abdfbd
JG
634pop_dummy_frame ()
635{
636 CORE_ADDR sp, pc;
637 int ii;
638 sp = dummy_frame_addr [--dummy_frame_count];
639
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);
643
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);
647 }
648
818de002
PB
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);
653
654 read_memory (sp-(DUMMY_FRAME_SIZE-8),
3a4f9786 655 &registers [REGISTER_BYTE(PC_REGNUM)], 4);
41abdfbd
JG
656
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. */
660
75621b2b 661 memcpy (&registers [REGISTER_BYTE(FP_REGNUM)], (char *) &sp, sizeof (int));
41abdfbd
JG
662
663 /* Now we can restore all registers. */
664
5f1c39ef 665 target_store_registers (-1);
41abdfbd
JG
666 pc = read_pc ();
667 flush_cached_frames ();
41abdfbd
JG
668}
669
670
671/* pop the innermost frame, go back to the caller. */
672
ecf4059f 673void
41abdfbd
JG
674pop_frame ()
675{
359a097f 676 CORE_ADDR pc, lr, sp, prev_sp; /* %pc, %lr, %sp */
63641491 677 struct rs6000_framedata fdata;
669caa9c 678 struct frame_info *frame = get_current_frame ();
41abdfbd 679 int addr, ii;
41abdfbd
JG
680
681 pc = read_pc ();
669caa9c 682 sp = FRAME_FP (frame);
41abdfbd 683
0ec1e44d
MA
684 if (stop_stack_dummy)
685 {
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
695 }
41abdfbd 696
07aa9fdc
PS
697 /* Make sure that all registers are valid. */
698 read_register_bytes (0, NULL, REGISTER_BYTES);
699
41abdfbd
JG
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. */
703
86a51f41 704 addr = get_pc_function_start (frame->pc);
068c9fd6 705 (void) skip_prologue (addr, &fdata);
41abdfbd 706
6c6afbb9 707 if (fdata.frameless)
07aa9fdc
PS
708 prev_sp = sp;
709 else
710 prev_sp = read_memory_integer (sp, 4);
068c9fd6 711 if (fdata.lr_offset == 0)
41abdfbd
JG
712 lr = read_register (LR_REGNUM);
713 else
068c9fd6 714 lr = read_memory_integer (prev_sp + fdata.lr_offset, 4);
41abdfbd
JG
715
716 /* reset %pc value. */
717 write_register (PC_REGNUM, lr);
718
719 /* reset register values if any was saved earlier. */
41abdfbd 720
6c6afbb9 721 if (fdata.saved_gpr != -1)
63d7a4f1
JB
722 {
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;
727 }
41abdfbd
JG
728 }
729
6c6afbb9 730 if (fdata.saved_fpr != -1)
63d7a4f1
JB
731 {
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;
736 }
737 }
41abdfbd
JG
738
739 write_register (SP_REGNUM, prev_sp);
5f1c39ef 740 target_store_registers (-1);
41abdfbd 741 flush_cached_frames ();
41abdfbd
JG
742}
743
41abdfbd
JG
744/* fixup the call sequence of a dummy function, with the real function address.
745 its argumets will be passed by gdb. */
746
ecf4059f 747void
2f163bc3 748rs6000_fix_call_dummy (dummyname, pc, fun, nargs, args, type, gcc_p)
cd8a3d84
SS
749 char *dummyname;
750 CORE_ADDR pc;
751 CORE_ADDR fun;
2f163bc3
SS
752 int nargs;
753 value_ptr *args;
754 struct type *type;
755 int gcc_p;
41abdfbd
JG
756{
757#define TOC_ADDR_OFFSET 20
758#define TARGET_ADDR_OFFSET 28
759
760 int ii;
ecf4059f 761 CORE_ADDR target_addr;
41abdfbd 762
05d52ace
PS
763 if (find_toc_address_hook != NULL)
764 {
765 CORE_ADDR tocvalue;
41abdfbd 766
05d52ace
PS
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;
41abdfbd 771
05d52ace
PS
772 ii = *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4);
773 ii = (ii & 0xffff0000) | (tocvalue & 0x0000ffff);
774 *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4) = ii;
775 }
41abdfbd 776
05d52ace 777 target_addr = fun;
41abdfbd
JG
778 ii = *(int*)((char*)dummyname + TARGET_ADDR_OFFSET);
779 ii = (ii & 0xffff0000) | (target_addr >> 16);
780 *(int*)((char*)dummyname + TARGET_ADDR_OFFSET) = ii;
781
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;
785}
786
cd8a3d84
SS
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
41abdfbd
JG
794 stack.
795
796 If the function is returning a structure, then the return address is passed
94b4f756 797 in r3, then the first 7 words of the parameters can be passed in registers,
41abdfbd
JG
798 starting from r4. */
799
800CORE_ADDR
801push_arguments (nargs, args, sp, struct_return, struct_addr)
cd8a3d84
SS
802 int nargs;
803 value_ptr *args;
804 CORE_ADDR sp;
805 int struct_return;
806 CORE_ADDR struct_addr;
41abdfbd 807{
368f1e77
FF
808 int ii;
809 int len = 0;
41abdfbd
JG
810 int argno; /* current argument number */
811 int argbytes; /* current argument byte */
812 char tmp_buffer [50];
41abdfbd 813 int f_argno = 0; /* current floating point argno */
0ec1e44d 814
368f1e77 815 value_ptr arg = 0;
940d5967 816 struct type *type;
41abdfbd 817
368f1e77 818 CORE_ADDR saved_sp;
41abdfbd 819
0ec1e44d 820#ifndef USE_GENERIC_DUMMY_FRAMES
41abdfbd 821 if ( dummy_frame_count <= 0)
199b2450 822 printf_unfiltered ("FATAL ERROR -push_arguments()! frame not found!!\n");
0ec1e44d 823#endif /* GENERIC_DUMMY_FRAMES */
41abdfbd
JG
824
825 /* The first eight words of ther arguments are passed in registers. Copy
826 them appropriately.
827
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. */
832
833 ii = struct_return ? 1 : 0;
834
0ec1e44d
MA
835/*
836effectively indirect call... gcc does...
837
838return_val example( float, int);
839
840eabi:
841 float in fp0, int in r3
842 offset of stack on overflow 8/16
843 for varargs, must go by type.
844power open:
845 float in r3&r4, int in r5
846 offset of stack on overflow different
847both:
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.
852*/
853
41abdfbd
JG
854 for (argno=0, argbytes=0; argno < nargs && ii<8; ++ii) {
855
5222ca60 856 arg = args[argno];
59c84318 857 type = check_typedef (VALUE_TYPE (arg));
940d5967 858 len = TYPE_LENGTH (type);
41abdfbd 859
940d5967 860 if (TYPE_CODE (type) == TYPE_CODE_FLT) {
41abdfbd
JG
861
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. */
865
866 if (len > 8)
199b2450 867 printf_unfiltered (
41abdfbd
JG
868"Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
869
75621b2b
MS
870 memcpy (&registers[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)],
871 VALUE_CONTENTS (arg),
872 len);
41abdfbd
JG
873 ++f_argno;
874 }
875
876 if (len > 4) {
877
878 /* Argument takes more than one register. */
879 while (argbytes < len) {
75621b2b 880 memset (&registers[REGISTER_BYTE(ii+3)], 0, sizeof(int));
ade40d31 881 memcpy (&registers[REGISTER_BYTE(ii+3)],
75621b2b
MS
882 ((char*)VALUE_CONTENTS (arg))+argbytes,
883 (len - argbytes) > 4 ? 4 : len - argbytes);
41abdfbd
JG
884 ++ii, argbytes += 4;
885
886 if (ii >= 8)
887 goto ran_out_of_registers_for_arguments;
888 }
889 argbytes = 0;
890 --ii;
891 }
892 else { /* Argument can fit in one register. No problem. */
75621b2b 893 memset (&registers[REGISTER_BYTE(ii+3)], 0, sizeof(int));
ade40d31 894 memcpy (&registers[REGISTER_BYTE(ii+3)], VALUE_CONTENTS (arg), len);
41abdfbd
JG
895 }
896 ++argno;
897 }
898
899ran_out_of_registers_for_arguments:
900
0ec1e44d
MA
901#ifdef USE_GENERIC_DUMMY_FRAMES
902 saved_sp = read_sp ();
903#else
41abdfbd
JG
904 /* location for 8 parameters are always reserved. */
905 sp -= 4 * 8;
906
907 /* another six words for back chain, TOC register, link register, etc. */
908 sp -= 24;
0ec1e44d 909#endif /* GENERIC_DUMMY_FRAMES */
41abdfbd
JG
910 /* if there are more arguments, allocate space for them in
911 the stack, then push them starting from the ninth one. */
912
913 if ((argno < nargs) || argbytes) {
914 int space = 0, jj;
41abdfbd
JG
915
916 if (argbytes) {
917 space += ((len - argbytes + 3) & -4);
918 jj = argno + 1;
919 }
920 else
921 jj = argno;
922
923 for (; jj < nargs; ++jj) {
940d5967 924 value_ptr val = args[jj];
41abdfbd
JG
925 space += ((TYPE_LENGTH (VALUE_TYPE (val))) + 3) & -4;
926 }
927
928 /* add location required for the rest of the parameters */
929 space = (space + 7) & -8;
930 sp -= space;
931
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. */
936
937 write_register (SP_REGNUM, sp);
938
41abdfbd
JG
939 /* if the last argument copied into the registers didn't fit there
940 completely, push the rest of it into stack. */
941
942 if (argbytes) {
75621b2b
MS
943 write_memory (sp+24+(ii*4),
944 ((char*)VALUE_CONTENTS (arg))+argbytes,
945 len - argbytes);
41abdfbd
JG
946 ++argno;
947 ii += ((len - argbytes + 3) & -4) / 4;
948 }
949
950 /* push the rest of the arguments into stack. */
951 for (; argno < nargs; ++argno) {
952
5222ca60 953 arg = args[argno];
940d5967
PB
954 type = check_typedef (VALUE_TYPE (arg));
955 len = TYPE_LENGTH (type);
41abdfbd
JG
956
957
958 /* float types should be passed in fpr's, as well as in the stack. */
940d5967 959 if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13) {
41abdfbd
JG
960
961 if (len > 8)
199b2450 962 printf_unfiltered (
41abdfbd
JG
963"Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
964
75621b2b
MS
965 memcpy (&registers[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)],
966 VALUE_CONTENTS (arg),
967 len);
41abdfbd
JG
968 ++f_argno;
969 }
970
359a097f 971 write_memory (sp+24+(ii*4), (char *) VALUE_CONTENTS (arg), len);
41abdfbd
JG
972 ii += ((len + 3) & -4) / 4;
973 }
974 }
6c6afbb9 975 else
41abdfbd
JG
976 /* Secure stack areas first, before doing anything else. */
977 write_register (SP_REGNUM, sp);
978
0ec1e44d
MA
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. */
982
41abdfbd
JG
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);
0ec1e44d 986#endif /* GENERIC_DUMMY_FRAMES */
41abdfbd 987
b112f2ae
JK
988 /* set back chain properly */
989 store_address (tmp_buffer, 4, saved_sp);
990 write_memory (sp, tmp_buffer, 4);
41abdfbd 991
5f1c39ef 992 target_store_registers (-1);
41abdfbd
JG
993 return sp;
994}
0ec1e44d
MA
995#ifdef ELF_OBJECT_FORMAT
996
997/* Function: ppc_push_return_address (pc, sp)
998 Set up the return address for the inferior function call. */
999
1000CORE_ADDR
1001ppc_push_return_address (pc, sp)
1002 CORE_ADDR pc;
1003 CORE_ADDR sp;
1004{
1005 write_register (LR_REGNUM, CALL_DUMMY_ADDRESS ());
1006 return sp;
1007}
1008
1009#endif
41abdfbd
JG
1010
1011/* a given return value in `regbuf' with a type `valtype', extract and copy its
1012 value into `valbuf' */
1013
ecf4059f 1014void
41abdfbd 1015extract_return_value (valtype, regbuf, valbuf)
cd8a3d84
SS
1016 struct type *valtype;
1017 char regbuf[REGISTER_BYTES];
1018 char *valbuf;
41abdfbd 1019{
07781ac0 1020 int offset = 0;
41abdfbd
JG
1021
1022 if (TYPE_CODE (valtype) == TYPE_CODE_FLT) {
1023
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. */
1028
1029 if (TYPE_LENGTH (valtype) > 4) /* this is a double */
75621b2b
MS
1030 memcpy (valbuf,
1031 &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)],
1032 TYPE_LENGTH (valtype));
41abdfbd 1033 else { /* float */
ade40d31 1034 memcpy (&dd, &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)], 8);
41abdfbd 1035 ff = (float)dd;
ade40d31 1036 memcpy (valbuf, &ff, sizeof(float));
41abdfbd
JG
1037 }
1038 }
07781ac0 1039 else {
41abdfbd 1040 /* return value is copied starting from r3. */
07781ac0
PS
1041 if (TARGET_BYTE_ORDER == BIG_ENDIAN
1042 && TYPE_LENGTH (valtype) < REGISTER_RAW_SIZE (3))
1043 offset = REGISTER_RAW_SIZE (3) - TYPE_LENGTH (valtype);
1044
75621b2b
MS
1045 memcpy (valbuf,
1046 regbuf + REGISTER_BYTE (3) + offset,
07781ac0
PS
1047 TYPE_LENGTH (valtype));
1048 }
41abdfbd
JG
1049}
1050
1051
ecf4059f
JG
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 */
41abdfbd
JG
1056
1057CORE_ADDR rs6000_struct_return_address;
1058
1059
c2e4669f
JG
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).
07aa9fdc
PS
1063 Also skip shared library trampoline code (which is different from
1064 indirect function call trampolines).
c2e4669f
JG
1065 Result is desired PC to step until, or NULL if we are not in
1066 trampoline code. */
41abdfbd 1067
ecf4059f 1068CORE_ADDR
41abdfbd 1069skip_trampoline_code (pc)
cd8a3d84 1070 CORE_ADDR pc;
41abdfbd
JG
1071{
1072 register unsigned int ii, op;
07aa9fdc 1073 CORE_ADDR solib_target_pc;
41abdfbd
JG
1074
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 */
1083 0
1084 };
1085
07aa9fdc
PS
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;
1090
41abdfbd
JG
1091 for (ii=0; trampoline_code[ii]; ++ii) {
1092 op = read_memory_integer (pc + (ii*4), 4);
1093 if (op != trampoline_code [ii])
359a097f 1094 return 0;
41abdfbd
JG
1095 }
1096 ii = read_register (11); /* r11 holds destination addr */
1097 pc = read_memory_integer (ii, 4); /* (r11) value */
1098 return pc;
1099}
1100
068c9fd6 1101/* Determines whether the function FI has a frame on the stack or not. */
cd8a3d84 1102
ecf4059f 1103int
068c9fd6
MM
1104frameless_function_invocation (fi)
1105 struct frame_info *fi;
ecf4059f
JG
1106{
1107 CORE_ADDR func_start;
63641491 1108 struct rs6000_framedata fdata;
ecf4059f 1109
4cc56716
PS
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)
b0e932ad
JK
1113 return 0;
1114
4cc56716 1115 func_start = get_pc_function_start (fi->pc);
ecf4059f
JG
1116
1117 /* If we failed to find the start of the function, it is a mistake
1118 to inspect the instructions. */
1119
1120 if (!func_start)
4cc56716
PS
1121 {
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;
1130 }
ecf4059f 1131
068c9fd6
MM
1132 (void) skip_prologue (func_start, &fdata);
1133 return fdata.frameless;
ecf4059f
JG
1134}
1135
068c9fd6 1136/* Return the PC saved in a frame */
cd8a3d84 1137
068c9fd6
MM
1138unsigned long
1139frame_saved_pc (fi)
1140 struct frame_info *fi;
1141{
1142 CORE_ADDR func_start;
1143 struct rs6000_framedata fdata;
068c9fd6 1144
965dde97
PS
1145 if (fi->signal_handler_caller)
1146 return read_memory_integer (fi->frame + SIG_FRAME_PC_OFFSET, 4);
1147
0ec1e44d
MA
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 */
1152
86a51f41 1153 func_start = get_pc_function_start (fi->pc);
068c9fd6
MM
1154
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;
1159
1160 (void) skip_prologue (func_start, &fdata);
068c9fd6 1161
4b4c6c96 1162 if (fdata.lr_offset == 0 && fi->next != NULL)
4cc56716
PS
1163 {
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,
1168 4);
1169 }
4b4c6c96
MM
1170
1171 if (fdata.lr_offset == 0)
1172 return read_register (LR_REGNUM);
1173
068c9fd6
MM
1174 return read_memory_integer (rs6000_frame_chain (fi) + fdata.lr_offset, 4);
1175}
ecf4059f
JG
1176
1177/* If saved registers of frame FI are not known yet, read and cache them.
63641491 1178 &FDATAP contains rs6000_framedata; TDATAP can be NULL,
ecf4059f
JG
1179 in which case the framedata are read. */
1180
1181static void
86a51f41 1182frame_get_saved_regs (fi, fdatap)
ecf4059f 1183 struct frame_info *fi;
63641491 1184 struct rs6000_framedata *fdatap;
ecf4059f
JG
1185{
1186 int ii;
1187 CORE_ADDR frame_addr;
63641491 1188 struct rs6000_framedata work_fdata;
ecf4059f 1189
86a51f41 1190 if (fi->saved_regs)
ecf4059f
JG
1191 return;
1192
86a51f41
AC
1193 if (fdatap == NULL)
1194 {
1195 fdatap = &work_fdata;
1196 (void) skip_prologue (get_pc_function_start (fi->pc), fdatap);
1197 }
1198
1199 frame_saved_regs_zalloc (fi);
ecf4059f 1200
86a51f41
AC
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. */
ecf4059f 1205
86a51f41
AC
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)
ecf4059f
JG
1210 frame_addr = fi->prev->frame;
1211 else
1212 frame_addr = read_memory_integer (fi->frame, 4);
1213
1214 /* if != -1, fdatap->saved_fpr is the smallest number of saved_fpr.
965dde97 1215 All fpr's from saved_fpr to fp31 are saved. */
ecf4059f 1216
86a51f41
AC
1217 if (fdatap->saved_fpr >= 0)
1218 {
1219 int i;
1220 int fpr_offset = frame_addr + fdatap->fpr_offset;
1221 for (i = fdatap->saved_fpr; i < 32; i++)
1222 {
1223 fi->saved_regs [FP0_REGNUM + i] = fpr_offset;
1224 fpr_offset += 8;
1225 }
965dde97 1226 }
ecf4059f
JG
1227
1228 /* if != -1, fdatap->saved_gpr is the smallest number of saved_gpr.
965dde97 1229 All gpr's from saved_gpr to gpr31 are saved. */
86a51f41
AC
1230
1231 if (fdatap->saved_gpr >= 0)
1232 {
1233 int i;
1234 int gpr_offset = frame_addr + fdatap->gpr_offset;
1235 for (i = fdatap->saved_gpr; i < 32; i++)
1236 {
1237 fi->saved_regs [i] = gpr_offset;
1238 gpr_offset += 4;
1239 }
965dde97 1240 }
965dde97
PS
1241
1242 /* If != 0, fdatap->cr_offset is the offset from the frame that holds
1243 the CR. */
1244 if (fdatap->cr_offset != 0)
86a51f41 1245 fi->saved_regs [CR_REGNUM] = frame_addr + fdatap->cr_offset;
965dde97
PS
1246
1247 /* If != 0, fdatap->lr_offset is the offset from the frame that holds
1248 the LR. */
1249 if (fdatap->lr_offset != 0)
86a51f41 1250 fi->saved_regs [LR_REGNUM] = frame_addr + fdatap->lr_offset;
ecf4059f
JG
1251}
1252
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. */
1256
86a51f41 1257static CORE_ADDR
ecf4059f
JG
1258frame_initial_stack_address (fi)
1259 struct frame_info *fi;
1260{
1261 CORE_ADDR tmpaddr;
63641491 1262 struct rs6000_framedata fdata;
ecf4059f
JG
1263 struct frame_info *callee_fi;
1264
1265 /* if the initial stack pointer (frame address) of this frame is known,
1266 just return it. */
1267
86a51f41
AC
1268 if (fi->extra_info->initial_sp)
1269 return fi->extra_info->initial_sp;
ecf4059f
JG
1270
1271 /* find out if this function is using an alloca register.. */
1272
068c9fd6 1273 (void) skip_prologue (get_pc_function_start (fi->pc), &fdata);
ecf4059f
JG
1274
1275 /* if saved registers of this frame are not known yet, read and cache them. */
1276
86a51f41
AC
1277 if (!fi->saved_regs)
1278 frame_get_saved_regs (fi, &fdata);
ecf4059f
JG
1279
1280 /* If no alloca register used, then fi->frame is the value of the %sp for
1281 this frame, and it is good enough. */
1282
86a51f41
AC
1283 if (fdata.alloca_reg < 0)
1284 {
1285 fi->extra_info->initial_sp = fi->frame;
1286 return fi->extra_info->initial_sp;
1287 }
ecf4059f
JG
1288
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. */
1291
1292 if (!fi->next)
86a51f41 1293 return fi->extra_info->initial_sp = read_register (fdata.alloca_reg);
ecf4059f
JG
1294
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. */
1299
1300 for (callee_fi = fi->next; callee_fi; callee_fi = callee_fi->next) {
1301
86a51f41
AC
1302 if (!callee_fi->saved_regs)
1303 frame_get_saved_regs (callee_fi, NULL);
ecf4059f
JG
1304
1305 /* this is the address in which alloca register is saved. */
1306
86a51f41 1307 tmpaddr = callee_fi->saved_regs [fdata.alloca_reg];
ecf4059f 1308 if (tmpaddr) {
86a51f41
AC
1309 fi->extra_info->initial_sp = read_memory_integer (tmpaddr, 4);
1310 return fi->extra_info->initial_sp;
ecf4059f
JG
1311 }
1312
1313 /* Go look into deeper levels of the frame chain to see if any one of
1314 the callees has saved alloca register. */
1315 }
1316
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. */
1319
86a51f41
AC
1320 fi->extra_info->initial_sp = read_register (fdata.alloca_reg);
1321 return fi->extra_info->initial_sp;
ecf4059f
JG
1322}
1323
669caa9c 1324CORE_ADDR
f3649227
JK
1325rs6000_frame_chain (thisframe)
1326 struct frame_info *thisframe;
1327{
669caa9c 1328 CORE_ADDR fp;
0ec1e44d
MA
1329
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 */
1334
1335 if (inside_entry_file (thisframe->pc) ||
1336 thisframe->pc == entry_point_address ())
f3649227 1337 return 0;
0ec1e44d 1338
cee86be3 1339 if (thisframe->signal_handler_caller)
9ed8604f 1340 fp = read_memory_integer (thisframe->frame + SIG_FRAME_FP_OFFSET, 4);
4cc56716
PS
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);
cee86be3
JK
1347 else
1348 fp = read_memory_integer ((thisframe)->frame, 4);
1349
0ec1e44d
MA
1350#ifdef USE_GENERIC_DUMMY_FRAMES
1351 {
1352 CORE_ADDR fpp, lr;
1353
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;
1359 }
1360#endif /* GENERIC_DUMMY_FRAMES */
f3649227
JK
1361 return fp;
1362}
ecf4059f 1363\f
65eaea27
JL
1364/* Return nonzero if ADDR (a function pointer) is in the data space and
1365 is therefore a special function pointer. */
1366
1367int
1368is_magic_function_pointer (addr)
1369 CORE_ADDR addr;
1370{
1371 struct obj_section *s;
1372
1373 s = find_pc_section (addr);
1374 if (s && s->the_bfd_section->flags & SEC_CODE)
1375 return 0;
1376 else
1377 return 1;
1378}
1379
5c172b4b
MM
1380#ifdef GDB_TARGET_POWERPC
1381int
1382gdb_print_insn_powerpc (memaddr, info)
1383 bfd_vma memaddr;
1384 disassemble_info *info;
1385{
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);
1390}
1391#endif
1392
0ec1e44d
MA
1393/* Function: get_saved_register
1394 Just call the generic_get_saved_register function. */
1395
70423641 1396#ifdef USE_GENERIC_DUMMY_FRAMES
0ec1e44d
MA
1397void
1398get_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;
1405{
1406 generic_get_saved_register (raw_buffer, optimized, addrp,
1407 frame, regnum, lval);
1408}
70423641 1409#endif
0ec1e44d
MA
1410
1411
18b46e7c
SS
1412void
1413_initialize_rs6000_tdep ()
1414{
1415 /* FIXME, this should not be decided via ifdef. */
1416#ifdef GDB_TARGET_POWERPC
5c172b4b 1417 tm_print_insn = gdb_print_insn_powerpc;
18b46e7c
SS
1418#else
1419 tm_print_insn = print_insn_rs6000;
1420#endif
1421}
This page took 0.486097 seconds and 4 git commands to generate.