* rs6000-tdep.c (rs6000_breakpoint_from_pc): Change big_breakpoint
[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
d6434f39
JG
31extern struct obstack frame_cache_obstack;
32
41abdfbd 33extern int errno;
41abdfbd
JG
34
35/* Nonzero if we just simulated a single step break. */
36int one_stepped;
37
41abdfbd
JG
38/* Breakpoint shadows for the single step instructions will be kept here. */
39
40static struct sstep_breaks {
030fb5cb
JK
41 /* Address, or 0 if this is not in use. */
42 CORE_ADDR address;
43 /* Shadow contents. */
44 char data[4];
41abdfbd
JG
45} stepBreaks[2];
46
05d52ace
PS
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. */
50
51CORE_ADDR (*find_toc_address_hook) PARAMS ((CORE_ADDR)) = NULL;
ecf4059f 52
05d52ace 53/* Static function prototypes */
ecf4059f 54
cd8a3d84
SS
55static CORE_ADDR branch_dest PARAMS ((int opcode, int instr, CORE_ADDR pc,
56 CORE_ADDR safety));
ecf4059f 57
cd8a3d84
SS
58static void frame_get_cache_fsr PARAMS ((struct frame_info *fi,
59 struct rs6000_framedata *fdatap));
60
368f1e77
FF
61static void pop_dummy_frame PARAMS ((void));
62
cd8a3d84 63/* Calculate the destination of a branch/jump. Return -1 if not a branch. */
41abdfbd 64
ecf4059f 65static CORE_ADDR
41abdfbd 66branch_dest (opcode, instr, pc, safety)
ecf4059f
JG
67 int opcode;
68 int instr;
69 CORE_ADDR pc;
70 CORE_ADDR safety;
41abdfbd 71{
ecf4059f 72 CORE_ADDR dest;
41abdfbd
JG
73 int immediate;
74 int absolute;
75 int ext_op;
76
77 absolute = (int) ((instr >> 1) & 1);
78
79 switch (opcode) {
80 case 18 :
ecf4059f 81 immediate = ((instr & ~3) << 6) >> 6; /* br unconditional */
dc59e982
MM
82 if (absolute)
83 dest = immediate;
84 else
85 dest = pc + immediate;
86 break;
41abdfbd
JG
87
88 case 16 :
dc59e982 89 immediate = ((instr & ~3) << 16) >> 16; /* br conditional */
41abdfbd
JG
90 if (absolute)
91 dest = immediate;
92 else
93 dest = pc + immediate;
94 break;
95
96 case 19 :
97 ext_op = (instr>>1) & 0x3ff;
98
99 if (ext_op == 16) /* br conditional register */
0c6c5eeb
PS
100 {
101 dest = read_register (LR_REGNUM) & ~3;
102
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)
108 {
109 struct frame_info *fi;
110
111 fi = get_current_frame ();
112 if (fi != NULL)
113 dest = read_memory_integer (fi->frame + SIG_FRAME_PC_OFFSET,
114 4);
115 }
116 }
41abdfbd
JG
117
118 else if (ext_op == 528) /* br cond to count reg */
9aa31e91
JK
119 {
120 dest = read_register (CTR_REGNUM) & ~3;
121
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;
127 }
41abdfbd
JG
128 else return -1;
129 break;
130
131 default: return -1;
132 }
818de002 133 return (dest < TEXT_SEGMENT_BASE) ? safety : dest;
41abdfbd
JG
134}
135
136
afcad54a
AC
137/* Sequence of bytes for breakpoint instruction. */
138
139#define BIG_BREAKPOINT { 0x7d, 0x82, 0x10, 0x08 }
140#define LITTLE_BREAKPOINT { 0x08, 0x10, 0x82, 0x7d }
141
142unsigned char *
143rs6000_breakpoint_from_pc (bp_addr, bp_size)
144 CORE_ADDR *bp_addr;
145 int *bp_size;
146{
07137a11
AC
147 static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
148 static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
afcad54a
AC
149 *bp_size = 4;
150 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
151 return big_breakpoint;
152 else
153 return little_breakpoint;
154}
155
41abdfbd
JG
156
157/* AIX does not support PT_STEP. Simulate it. */
158
997cc2c0 159void
41abdfbd 160single_step (signal)
b607efe7 161 enum target_signal signal;
41abdfbd
JG
162{
163#define INSNLEN(OPCODE) 4
164
5c172b4b
MM
165 static char le_breakp[] = LITTLE_BREAKPOINT;
166 static char be_breakp[] = BIG_BREAKPOINT;
167 char *breakp = TARGET_BYTE_ORDER == BIG_ENDIAN ? be_breakp : le_breakp;
030fb5cb
JK
168 int ii, insn;
169 CORE_ADDR loc;
170 CORE_ADDR breaks[2];
171 int opcode;
41abdfbd
JG
172
173 if (!one_stepped) {
41abdfbd
JG
174 loc = read_pc ();
175
b112f2ae 176 insn = read_memory_integer (loc, 4);
41abdfbd
JG
177
178 breaks[0] = loc + INSNLEN(insn);
179 opcode = insn >> 26;
180 breaks[1] = branch_dest (opcode, insn, loc, breaks[0]);
181
818de002
PB
182 /* Don't put two breakpoints on the same address. */
183 if (breaks[1] == breaks[0])
184 breaks[1] = -1;
185
030fb5cb 186 stepBreaks[1].address = 0;
41abdfbd
JG
187
188 for (ii=0; ii < 2; ++ii) {
189
190 /* ignore invalid breakpoint. */
191 if ( breaks[ii] == -1)
192 continue;
193
030fb5cb 194 read_memory (breaks[ii], stepBreaks[ii].data, 4);
41abdfbd 195
030fb5cb 196 write_memory (breaks[ii], breakp, 4);
41abdfbd
JG
197 stepBreaks[ii].address = breaks[ii];
198 }
199
200 one_stepped = 1;
997cc2c0 201 } else {
41abdfbd
JG
202
203 /* remove step breakpoints. */
204 for (ii=0; ii < 2; ++ii)
030fb5cb 205 if (stepBreaks[ii].address != 0)
41abdfbd 206 write_memory
030fb5cb 207 (stepBreaks[ii].address, stepBreaks[ii].data, 4);
41abdfbd
JG
208
209 one_stepped = 0;
210 }
997cc2c0 211 errno = 0; /* FIXME, don't ignore errors! */
030fb5cb 212 /* What errors? {read,write}_memory call error(). */
41abdfbd 213}
41abdfbd
JG
214
215
068c9fd6
MM
216/* return pc value after skipping a function prologue and also return
217 information about a function frame.
41abdfbd 218
068c9fd6
MM
219 in struct rs6000_frameinfo fdata:
220 - frameless is TRUE, if function does not have a frame.
221 - nosavedpc is TRUE, if function does not save %pc value in its frame.
222 - offset is the number of bytes used in the frame to save registers.
223 - saved_gpr is the number of the first saved gpr.
224 - saved_fpr is the number of the first saved fpr.
225 - alloca_reg is the number of the register used for alloca() handling.
226 Otherwise -1.
227 - gpr_offset is the offset of the saved gprs
228 - fpr_offset is the offset of the saved fprs
229 - lr_offset is the offset of the saved lr
230 - cr_offset is the offset of the saved cr
231 */
232
233#define SIGNED_SHORT(x) \
234 ((sizeof (short) == 2) \
235 ? ((int)(short)(x)) \
236 : ((int)((((x) & 0xffff) ^ 0x8000) - 0x8000)))
237
238#define GET_SRC_REG(x) (((x) >> 21) & 0x1f)
239
240CORE_ADDR
241skip_prologue (pc, fdata)
242 CORE_ADDR pc;
243 struct rs6000_framedata *fdata;
41abdfbd 244{
068c9fd6 245 CORE_ADDR orig_pc = pc;
34df79fc 246 char buf[4];
34df79fc 247 unsigned long op;
4b4c6c96 248 long offset = 0;
068c9fd6
MM
249 int lr_reg = 0;
250 int cr_reg = 0;
251 int reg;
4b4c6c96 252 int framep = 0;
65eaea27 253 int minimal_toc_loaded = 0;
068c9fd6
MM
254 static struct rs6000_framedata zero_frame;
255
256 *fdata = zero_frame;
257 fdata->saved_gpr = -1;
258 fdata->saved_fpr = -1;
259 fdata->alloca_reg = -1;
260 fdata->frameless = 1;
261 fdata->nosavedpc = 1;
41abdfbd 262
34df79fc 263 if (target_read_memory (pc, buf, 4))
41abdfbd 264 return pc; /* Can't access it -- assume no prologue. */
41abdfbd
JG
265
266 /* Assume that subsequent fetches can fail with low probability. */
068c9fd6
MM
267 pc -= 4;
268 for (;;)
269 {
270 pc += 4;
271 op = read_memory_integer (pc, 4);
41abdfbd 272
068c9fd6
MM
273 if ((op & 0xfc1fffff) == 0x7c0802a6) { /* mflr Rx */
274 lr_reg = (op & 0x03e00000) | 0x90010000;
275 continue;
276
277 } else if ((op & 0xfc1fffff) == 0x7c000026) { /* mfcr Rx */
278 cr_reg = (op & 0x03e00000) | 0x90010000;
279 continue;
280
281 } else if ((op & 0xfc1f0000) == 0xd8010000) { /* stfd Rx,NUM(r1) */
282 reg = GET_SRC_REG (op);
283 if (fdata->saved_fpr == -1 || fdata->saved_fpr > reg) {
284 fdata->saved_fpr = reg;
4b4c6c96 285 fdata->fpr_offset = SIGNED_SHORT (op) + offset;
068c9fd6
MM
286 }
287 continue;
288
289 } else if (((op & 0xfc1f0000) == 0xbc010000) || /* stm Rx, NUM(r1) */
75621b2b
MS
290 ((op & 0xfc1f0000) == 0x90010000 && /* st rx,NUM(r1),
291 rx >= r13 */
068c9fd6
MM
292 (op & 0x03e00000) >= 0x01a00000)) {
293
294 reg = GET_SRC_REG (op);
295 if (fdata->saved_gpr == -1 || fdata->saved_gpr > reg) {
296 fdata->saved_gpr = reg;
4b4c6c96 297 fdata->gpr_offset = SIGNED_SHORT (op) + offset;
068c9fd6
MM
298 }
299 continue;
300
75621b2b
MS
301 } else if ((op & 0xffff0000) == 0x3c000000) { /* addis 0,0,NUM, used
302 for >= 32k frames */
068c9fd6 303 fdata->offset = (op & 0x0000ffff) << 16;
65eaea27 304 fdata->frameless = 0;
068c9fd6
MM
305 continue;
306
75621b2b
MS
307 } else if ((op & 0xffff0000) == 0x60000000) { /* ori 0,0,NUM, 2nd ha
308 lf of >= 32k frames */
068c9fd6 309 fdata->offset |= (op & 0x0000ffff);
65eaea27 310 fdata->frameless = 0;
068c9fd6
MM
311 continue;
312
75621b2b
MS
313 } else if ((op & 0xffff0000) == lr_reg) { /* st Rx,NUM(r1)
314 where Rx == lr */
4b4c6c96 315 fdata->lr_offset = SIGNED_SHORT (op) + offset;
068c9fd6
MM
316 fdata->nosavedpc = 0;
317 lr_reg = 0;
318 continue;
319
75621b2b
MS
320 } else if ((op & 0xffff0000) == cr_reg) { /* st Rx,NUM(r1)
321 where Rx == cr */
4b4c6c96 322 fdata->cr_offset = SIGNED_SHORT (op) + offset;
068c9fd6
MM
323 cr_reg = 0;
324 continue;
325
75621b2b
MS
326 } else if (op == 0x48000005) { /* bl .+4 used in
327 -mrelocatable */
4b4c6c96
MM
328 continue;
329
65eaea27
JL
330 } else if (op == 0x48000004) { /* b .+4 (xlc) */
331 break;
332
75621b2b
MS
333 } else if (((op & 0xffff0000) == 0x801e0000 || /* lwz 0,NUM(r30), used
334 in V.4 -mrelocatable */
335 op == 0x7fc0f214) && /* add r30,r0,r30, used
336 in V.4 -mrelocatable */
4b4c6c96
MM
337 lr_reg == 0x901e0000) {
338 continue;
339
75621b2b
MS
340 } else if ((op & 0xffff0000) == 0x3fc00000 || /* addis 30,0,foo@ha, used
341 in V.4 -mminimal-toc */
4b4c6c96
MM
342 (op & 0xffff0000) == 0x3bde0000) { /* addi 30,30,foo@l */
343 continue;
344
75621b2b
MS
345 } else if ((op & 0xfc000000) == 0x48000000) { /* bl foo,
346 to save fprs??? */
965dde97 347
65eaea27 348 fdata->frameless = 0;
965dde97
PS
349 /* Don't skip over the subroutine call if it is not within the first
350 three instructions of the prologue. */
351 if ((pc - orig_pc) > 8)
352 break;
353
068c9fd6
MM
354 op = read_memory_integer (pc+4, 4);
355
356 /* At this point, make sure this is not a trampoline function
357 (a function that simply calls another functions, and nothing else).
358 If the next is not a nop, this branch was part of the function
359 prologue. */
360
361 if (op == 0x4def7b82 || op == 0) /* crorc 15, 15, 15 */
75621b2b
MS
362 break; /* don't skip over
363 this branch */
068c9fd6
MM
364 continue;
365
4b4c6c96 366 /* update stack pointer */
068c9fd6 367 } else if ((op & 0xffff0000) == 0x94210000) { /* stu r1,NUM(r1) */
65eaea27 368 fdata->frameless = 0;
4b4c6c96
MM
369 fdata->offset = SIGNED_SHORT (op);
370 offset = fdata->offset;
371 continue;
1eeba686 372
068c9fd6 373 } else if (op == 0x7c21016e) { /* stwux 1,1,0 */
65eaea27 374 fdata->frameless = 0;
4b4c6c96
MM
375 offset = fdata->offset;
376 continue;
41abdfbd 377
4b4c6c96 378 /* Load up minimal toc pointer */
65eaea27
JL
379 } else if ((op >> 22) == 0x20f
380 && ! minimal_toc_loaded) { /* l r31,... or l r30,... */
381 minimal_toc_loaded = 1;
4b4c6c96 382 continue;
cdb1cc92 383
4b4c6c96
MM
384 /* store parameters in stack */
385 } else if ((op & 0xfc1f0000) == 0x90010000 || /* st rx,NUM(r1) */
386 (op & 0xfc1f0000) == 0xd8010000 || /* stfd Rx,NUM(r1) */
387 (op & 0xfc1f0000) == 0xfc010000) { /* frsp, fp?,NUM(r1) */
388 continue;
e137e850 389
4b4c6c96
MM
390 /* store parameters in stack via frame pointer */
391 } else if (framep &&
58b4fad2 392 ((op & 0xfc1f0000) == 0x901f0000 || /* st rx,NUM(r1) */
4b4c6c96 393 (op & 0xfc1f0000) == 0xd81f0000 || /* stfd Rx,NUM(r1) */
58b4fad2 394 (op & 0xfc1f0000) == 0xfc1f0000)) { /* frsp, fp?,NUM(r1) */
4b4c6c96 395 continue;
e137e850 396
4b4c6c96
MM
397 /* Set up frame pointer */
398 } else if (op == 0x603f0000 /* oril r31, r1, 0x0 */
399 || op == 0x7c3f0b78) { /* mr r31, r1 */
65eaea27 400 fdata->frameless = 0;
4b4c6c96 401 framep = 1;
965dde97 402 fdata->alloca_reg = 31;
4b4c6c96 403 continue;
41abdfbd 404
65eaea27
JL
405 /* Another way to set up the frame pointer. */
406 } else if ((op & 0xfc1fffff) == 0x38010000) { /* addi rX, r1, 0x0 */
407 fdata->frameless = 0;
408 framep = 1;
409 fdata->alloca_reg = (op & ~0x38010000) >> 21;
410 continue;
411
4b4c6c96
MM
412 } else {
413 break;
414 }
41abdfbd 415 }
068c9fd6 416
507e4004
PB
417#if 0
418/* I have problems with skipping over __main() that I need to address
419 * sometime. Previously, I used to use misc_function_vector which
420 * didn't work as well as I wanted to be. -MGO */
421
422 /* If the first thing after skipping a prolog is a branch to a function,
423 this might be a call to an initializer in main(), introduced by gcc2.
424 We'd like to skip over it as well. Fortunately, xlc does some extra
425 work before calling a function right after a prologue, thus we can
426 single out such gcc2 behaviour. */
427
428
429 if ((op & 0xfc000001) == 0x48000001) { /* bl foo, an initializer function? */
430 op = read_memory_integer (pc+4, 4);
431
432 if (op == 0x4def7b82) { /* cror 0xf, 0xf, 0xf (nop) */
433
434 /* check and see if we are in main. If so, skip over this initializer
435 function as well. */
436
437 tmp = find_pc_misc_function (pc);
2e4964ad 438 if (tmp >= 0 && STREQ (misc_function_vector [tmp].name, "main"))
507e4004
PB
439 return pc + 8;
440 }
441 }
442#endif /* 0 */
443
4b4c6c96 444 fdata->offset = - fdata->offset;
41abdfbd
JG
445 return pc;
446}
447
818de002 448
41abdfbd
JG
449/*************************************************************************
450 Support for creating pushind a dummy frame into the stack, and popping
451 frames, etc.
452*************************************************************************/
453
818de002
PB
454/* The total size of dummy frame is 436, which is;
455
456 32 gpr's - 128 bytes
457 32 fpr's - 256 "
458 7 the rest - 28 "
459 and 24 extra bytes for the callee's link area. The last 24 bytes
460 for the link area might not be necessary, since it will be taken
461 care of by push_arguments(). */
462
463#define DUMMY_FRAME_SIZE 436
464
41abdfbd
JG
465#define DUMMY_FRAME_ADDR_SIZE 10
466
467/* Make sure you initialize these in somewhere, in case gdb gives up what it
818de002 468 was debugging and starts debugging something else. FIXMEibm */
41abdfbd
JG
469
470static int dummy_frame_count = 0;
471static int dummy_frame_size = 0;
472static CORE_ADDR *dummy_frame_addr = 0;
473
474extern int stop_stack_dummy;
475
476/* push a dummy frame into stack, save all register. Currently we are saving
477 only gpr's and fpr's, which is not good enough! FIXMEmgo */
478
ecf4059f 479void
41abdfbd
JG
480push_dummy_frame ()
481{
359a097f
JK
482 /* stack pointer. */
483 CORE_ADDR sp;
b112f2ae
JK
484 /* Same thing, target byte order. */
485 char sp_targ[4];
359a097f
JK
486
487 /* link register. */
488 CORE_ADDR pc;
489 /* Same thing, target byte order. */
490 char pc_targ[4];
491
3a4f9786
MT
492 /* Needed to figure out where to save the dummy link area.
493 FIXME: There should be an easier way to do this, no? tiemann 9/9/95. */
494 struct rs6000_framedata fdata;
495
41abdfbd
JG
496 int ii;
497
5f1c39ef 498 target_fetch_registers (-1);
6c6afbb9 499
41abdfbd
JG
500 if (dummy_frame_count >= dummy_frame_size) {
501 dummy_frame_size += DUMMY_FRAME_ADDR_SIZE;
502 if (dummy_frame_addr)
503 dummy_frame_addr = (CORE_ADDR*) xrealloc
504 (dummy_frame_addr, sizeof(CORE_ADDR) * (dummy_frame_size));
505 else
506 dummy_frame_addr = (CORE_ADDR*)
507 xmalloc (sizeof(CORE_ADDR) * (dummy_frame_size));
508 }
509
510 sp = read_register(SP_REGNUM);
359a097f 511 pc = read_register(PC_REGNUM);
5816555b 512 store_address (pc_targ, 4, pc);
41abdfbd 513
75621b2b 514 skip_prologue (get_pc_function_start (pc) + FUNCTION_START_OFFSET, &fdata);
3a4f9786 515
41abdfbd
JG
516 dummy_frame_addr [dummy_frame_count++] = sp;
517
518 /* Be careful! If the stack pointer is not decremented first, then kernel
6c6afbb9 519 thinks he is free to use the space underneath it. And kernel actually
41abdfbd
JG
520 uses that area for IPC purposes when executing ptrace(2) calls. So
521 before writing register values into the new frame, decrement and update
522 %sp first in order to secure your frame. */
523
3a4f9786
MT
524 /* FIXME: We don't check if the stack really has this much space.
525 This is a problem on the ppc simulator (which only grants one page
526 (4096 bytes) by default. */
527
818de002 528 write_register (SP_REGNUM, sp-DUMMY_FRAME_SIZE);
41abdfbd 529
41abdfbd
JG
530 /* gdb relies on the state of current_frame. We'd better update it,
531 otherwise things like do_registers_info() wouldn't work properly! */
532
533 flush_cached_frames ();
41abdfbd
JG
534
535 /* save program counter in link register's space. */
4f8710e6
PS
536 write_memory (sp + (fdata.lr_offset ? fdata.lr_offset : DEFAULT_LR_SAVE),
537 pc_targ, 4);
41abdfbd 538
6c6afbb9 539 /* save all floating point and general purpose registers here. */
41abdfbd
JG
540
541 /* fpr's, f0..f31 */
542 for (ii = 0; ii < 32; ++ii)
543 write_memory (sp-8-(ii*8), &registers[REGISTER_BYTE (31-ii+FP0_REGNUM)], 8);
544
545 /* gpr's r0..r31 */
546 for (ii=1; ii <=32; ++ii)
547 write_memory (sp-256-(ii*4), &registers[REGISTER_BYTE (32-ii)], 4);
548
818de002
PB
549 /* so far, 32*2 + 32 words = 384 bytes have been written.
550 7 extra registers in our register set: pc, ps, cnd, lr, cnt, xer, mq */
551
552 for (ii=1; ii <= (LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii) {
553 write_memory (sp-384-(ii*4),
75621b2b 554 &registers[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4);
818de002
PB
555 }
556
557 /* Save sp or so called back chain right here. */
b112f2ae
JK
558 store_address (sp_targ, 4, sp);
559 write_memory (sp-DUMMY_FRAME_SIZE, sp_targ, 4);
818de002 560 sp -= DUMMY_FRAME_SIZE;
41abdfbd
JG
561
562 /* And finally, this is the back chain. */
359a097f 563 write_memory (sp+8, pc_targ, 4);
41abdfbd
JG
564}
565
566
567/* Pop a dummy frame.
568
569 In rs6000 when we push a dummy frame, we save all of the registers. This
570 is usually done before user calls a function explicitly.
571
818de002
PB
572 After a dummy frame is pushed, some instructions are copied into stack,
573 and stack pointer is decremented even more. Since we don't have a frame
574 pointer to get back to the parent frame of the dummy, we start having
575 trouble poping it. Therefore, we keep a dummy frame stack, keeping
576 addresses of dummy frames as such. When poping happens and when we
577 detect that was a dummy frame, we pop it back to its parent by using
578 dummy frame stack (`dummy_frame_addr' array).
ecf4059f
JG
579
580FIXME: This whole concept is broken. You should be able to detect
581a dummy stack frame *on the user's stack itself*. When you do,
582then you know the format of that stack frame -- including its
583saved SP register! There should *not* be a separate stack in the
d6434f39 584GDB process that keeps track of these dummy frames! -- gnu@cygnus.com Aug92
41abdfbd
JG
585 */
586
368f1e77 587static void
41abdfbd
JG
588pop_dummy_frame ()
589{
590 CORE_ADDR sp, pc;
591 int ii;
592 sp = dummy_frame_addr [--dummy_frame_count];
593
594 /* restore all fpr's. */
595 for (ii = 1; ii <= 32; ++ii)
596 read_memory (sp-(ii*8), &registers[REGISTER_BYTE (32-ii+FP0_REGNUM)], 8);
597
598 /* restore all gpr's */
599 for (ii=1; ii <= 32; ++ii) {
600 read_memory (sp-256-(ii*4), &registers[REGISTER_BYTE (32-ii)], 4);
601 }
602
818de002
PB
603 /* restore the rest of the registers. */
604 for (ii=1; ii <=(LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii)
605 read_memory (sp-384-(ii*4),
606 &registers[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4);
607
608 read_memory (sp-(DUMMY_FRAME_SIZE-8),
3a4f9786 609 &registers [REGISTER_BYTE(PC_REGNUM)], 4);
41abdfbd
JG
610
611 /* when a dummy frame was being pushed, we had to decrement %sp first, in
612 order to secure astack space. Thus, saved %sp (or %r1) value, is not the
613 one we should restore. Change it with the one we need. */
614
75621b2b 615 memcpy (&registers [REGISTER_BYTE(FP_REGNUM)], (char *) &sp, sizeof (int));
41abdfbd
JG
616
617 /* Now we can restore all registers. */
618
5f1c39ef 619 target_store_registers (-1);
41abdfbd
JG
620 pc = read_pc ();
621 flush_cached_frames ();
41abdfbd
JG
622}
623
624
625/* pop the innermost frame, go back to the caller. */
626
ecf4059f 627void
41abdfbd
JG
628pop_frame ()
629{
359a097f 630 CORE_ADDR pc, lr, sp, prev_sp; /* %pc, %lr, %sp */
63641491 631 struct rs6000_framedata fdata;
669caa9c 632 struct frame_info *frame = get_current_frame ();
41abdfbd 633 int addr, ii;
41abdfbd
JG
634
635 pc = read_pc ();
669caa9c 636 sp = FRAME_FP (frame);
41abdfbd 637
0ec1e44d
MA
638 if (stop_stack_dummy)
639 {
640#ifdef USE_GENERIC_DUMMY_FRAMES
641 generic_pop_dummy_frame ();
642 flush_cached_frames ();
643 return;
644#else
645 if (dummy_frame_count)
646 pop_dummy_frame ();
647 return;
648#endif
649 }
41abdfbd 650
07aa9fdc
PS
651 /* Make sure that all registers are valid. */
652 read_register_bytes (0, NULL, REGISTER_BYTES);
653
41abdfbd
JG
654 /* figure out previous %pc value. If the function is frameless, it is
655 still in the link register, otherwise walk the frames and retrieve the
656 saved %pc value in the previous frame. */
657
34a1a3bf 658 addr = get_pc_function_start (frame->pc) + FUNCTION_START_OFFSET;
068c9fd6 659 (void) skip_prologue (addr, &fdata);
41abdfbd 660
6c6afbb9 661 if (fdata.frameless)
07aa9fdc
PS
662 prev_sp = sp;
663 else
664 prev_sp = read_memory_integer (sp, 4);
068c9fd6 665 if (fdata.lr_offset == 0)
41abdfbd
JG
666 lr = read_register (LR_REGNUM);
667 else
068c9fd6 668 lr = read_memory_integer (prev_sp + fdata.lr_offset, 4);
41abdfbd
JG
669
670 /* reset %pc value. */
671 write_register (PC_REGNUM, lr);
672
673 /* reset register values if any was saved earlier. */
6c6afbb9 674 addr = prev_sp - fdata.offset;
41abdfbd 675
6c6afbb9 676 if (fdata.saved_gpr != -1)
669caa9c 677 for (ii = fdata.saved_gpr; ii <= 31; ++ii) {
41abdfbd 678 read_memory (addr, &registers [REGISTER_BYTE (ii)], 4);
cdb1cc92 679 addr += 4;
41abdfbd
JG
680 }
681
6c6afbb9 682 if (fdata.saved_fpr != -1)
669caa9c 683 for (ii = fdata.saved_fpr; ii <= 31; ++ii) {
41abdfbd
JG
684 read_memory (addr, &registers [REGISTER_BYTE (ii+FP0_REGNUM)], 8);
685 addr += 8;
686 }
687
688 write_register (SP_REGNUM, prev_sp);
5f1c39ef 689 target_store_registers (-1);
41abdfbd 690 flush_cached_frames ();
41abdfbd
JG
691}
692
41abdfbd
JG
693/* fixup the call sequence of a dummy function, with the real function address.
694 its argumets will be passed by gdb. */
695
ecf4059f 696void
2f163bc3 697rs6000_fix_call_dummy (dummyname, pc, fun, nargs, args, type, gcc_p)
cd8a3d84
SS
698 char *dummyname;
699 CORE_ADDR pc;
700 CORE_ADDR fun;
2f163bc3
SS
701 int nargs;
702 value_ptr *args;
703 struct type *type;
704 int gcc_p;
41abdfbd
JG
705{
706#define TOC_ADDR_OFFSET 20
707#define TARGET_ADDR_OFFSET 28
708
709 int ii;
ecf4059f 710 CORE_ADDR target_addr;
41abdfbd 711
05d52ace
PS
712 if (find_toc_address_hook != NULL)
713 {
714 CORE_ADDR tocvalue;
41abdfbd 715
05d52ace
PS
716 tocvalue = (*find_toc_address_hook) (fun);
717 ii = *(int*)((char*)dummyname + TOC_ADDR_OFFSET);
718 ii = (ii & 0xffff0000) | (tocvalue >> 16);
719 *(int*)((char*)dummyname + TOC_ADDR_OFFSET) = ii;
41abdfbd 720
05d52ace
PS
721 ii = *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4);
722 ii = (ii & 0xffff0000) | (tocvalue & 0x0000ffff);
723 *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4) = ii;
724 }
41abdfbd 725
05d52ace 726 target_addr = fun;
41abdfbd
JG
727 ii = *(int*)((char*)dummyname + TARGET_ADDR_OFFSET);
728 ii = (ii & 0xffff0000) | (target_addr >> 16);
729 *(int*)((char*)dummyname + TARGET_ADDR_OFFSET) = ii;
730
731 ii = *(int*)((char*)dummyname + TARGET_ADDR_OFFSET+4);
732 ii = (ii & 0xffff0000) | (target_addr & 0x0000ffff);
733 *(int*)((char*)dummyname + TARGET_ADDR_OFFSET+4) = ii;
734}
735
cd8a3d84
SS
736/* Pass the arguments in either registers, or in the stack. In RS6000,
737 the first eight words of the argument list (that might be less than
738 eight parameters if some parameters occupy more than one word) are
739 passed in r3..r11 registers. float and double parameters are
740 passed in fpr's, in addition to that. Rest of the parameters if any
741 are passed in user stack. There might be cases in which half of the
742 parameter is copied into registers, the other half is pushed into
41abdfbd
JG
743 stack.
744
745 If the function is returning a structure, then the return address is passed
94b4f756 746 in r3, then the first 7 words of the parameters can be passed in registers,
41abdfbd
JG
747 starting from r4. */
748
749CORE_ADDR
750push_arguments (nargs, args, sp, struct_return, struct_addr)
cd8a3d84
SS
751 int nargs;
752 value_ptr *args;
753 CORE_ADDR sp;
754 int struct_return;
755 CORE_ADDR struct_addr;
41abdfbd 756{
368f1e77
FF
757 int ii;
758 int len = 0;
41abdfbd
JG
759 int argno; /* current argument number */
760 int argbytes; /* current argument byte */
761 char tmp_buffer [50];
41abdfbd 762 int f_argno = 0; /* current floating point argno */
0ec1e44d 763
368f1e77 764 value_ptr arg = 0;
940d5967 765 struct type *type;
41abdfbd 766
368f1e77 767 CORE_ADDR saved_sp;
41abdfbd 768
0ec1e44d 769#ifndef USE_GENERIC_DUMMY_FRAMES
41abdfbd 770 if ( dummy_frame_count <= 0)
199b2450 771 printf_unfiltered ("FATAL ERROR -push_arguments()! frame not found!!\n");
0ec1e44d 772#endif /* GENERIC_DUMMY_FRAMES */
41abdfbd
JG
773
774 /* The first eight words of ther arguments are passed in registers. Copy
775 them appropriately.
776
777 If the function is returning a `struct', then the first word (which
778 will be passed in r3) is used for struct return address. In that
779 case we should advance one word and start from r4 register to copy
780 parameters. */
781
782 ii = struct_return ? 1 : 0;
783
0ec1e44d
MA
784/*
785effectively indirect call... gcc does...
786
787return_val example( float, int);
788
789eabi:
790 float in fp0, int in r3
791 offset of stack on overflow 8/16
792 for varargs, must go by type.
793power open:
794 float in r3&r4, int in r5
795 offset of stack on overflow different
796both:
797 return in r3 or f0. If no float, must study how gcc emulates floats;
798 pay attention to arg promotion.
799 User may have to cast\args to handle promotion correctly
800 since gdb won't know if prototype supplied or not.
801*/
802
41abdfbd
JG
803 for (argno=0, argbytes=0; argno < nargs && ii<8; ++ii) {
804
5222ca60 805 arg = args[argno];
59c84318 806 type = check_typedef (VALUE_TYPE (arg));
940d5967 807 len = TYPE_LENGTH (type);
41abdfbd 808
940d5967 809 if (TYPE_CODE (type) == TYPE_CODE_FLT) {
41abdfbd
JG
810
811 /* floating point arguments are passed in fpr's, as well as gpr's.
812 There are 13 fpr's reserved for passing parameters. At this point
813 there is no way we would run out of them. */
814
815 if (len > 8)
199b2450 816 printf_unfiltered (
41abdfbd
JG
817"Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
818
75621b2b
MS
819 memcpy (&registers[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)],
820 VALUE_CONTENTS (arg),
821 len);
41abdfbd
JG
822 ++f_argno;
823 }
824
825 if (len > 4) {
826
827 /* Argument takes more than one register. */
828 while (argbytes < len) {
75621b2b 829 memset (&registers[REGISTER_BYTE(ii+3)], 0, sizeof(int));
ade40d31 830 memcpy (&registers[REGISTER_BYTE(ii+3)],
75621b2b
MS
831 ((char*)VALUE_CONTENTS (arg))+argbytes,
832 (len - argbytes) > 4 ? 4 : len - argbytes);
41abdfbd
JG
833 ++ii, argbytes += 4;
834
835 if (ii >= 8)
836 goto ran_out_of_registers_for_arguments;
837 }
838 argbytes = 0;
839 --ii;
840 }
841 else { /* Argument can fit in one register. No problem. */
75621b2b 842 memset (&registers[REGISTER_BYTE(ii+3)], 0, sizeof(int));
ade40d31 843 memcpy (&registers[REGISTER_BYTE(ii+3)], VALUE_CONTENTS (arg), len);
41abdfbd
JG
844 }
845 ++argno;
846 }
847
848ran_out_of_registers_for_arguments:
849
0ec1e44d
MA
850#ifdef USE_GENERIC_DUMMY_FRAMES
851 saved_sp = read_sp ();
852#else
41abdfbd
JG
853 /* location for 8 parameters are always reserved. */
854 sp -= 4 * 8;
855
856 /* another six words for back chain, TOC register, link register, etc. */
857 sp -= 24;
0ec1e44d 858#endif /* GENERIC_DUMMY_FRAMES */
41abdfbd
JG
859 /* if there are more arguments, allocate space for them in
860 the stack, then push them starting from the ninth one. */
861
862 if ((argno < nargs) || argbytes) {
863 int space = 0, jj;
41abdfbd
JG
864
865 if (argbytes) {
866 space += ((len - argbytes + 3) & -4);
867 jj = argno + 1;
868 }
869 else
870 jj = argno;
871
872 for (; jj < nargs; ++jj) {
940d5967 873 value_ptr val = args[jj];
41abdfbd
JG
874 space += ((TYPE_LENGTH (VALUE_TYPE (val))) + 3) & -4;
875 }
876
877 /* add location required for the rest of the parameters */
878 space = (space + 7) & -8;
879 sp -= space;
880
881 /* This is another instance we need to be concerned about securing our
882 stack space. If we write anything underneath %sp (r1), we might conflict
883 with the kernel who thinks he is free to use this area. So, update %sp
884 first before doing anything else. */
885
886 write_register (SP_REGNUM, sp);
887
41abdfbd
JG
888 /* if the last argument copied into the registers didn't fit there
889 completely, push the rest of it into stack. */
890
891 if (argbytes) {
75621b2b
MS
892 write_memory (sp+24+(ii*4),
893 ((char*)VALUE_CONTENTS (arg))+argbytes,
894 len - argbytes);
41abdfbd
JG
895 ++argno;
896 ii += ((len - argbytes + 3) & -4) / 4;
897 }
898
899 /* push the rest of the arguments into stack. */
900 for (; argno < nargs; ++argno) {
901
5222ca60 902 arg = args[argno];
940d5967
PB
903 type = check_typedef (VALUE_TYPE (arg));
904 len = TYPE_LENGTH (type);
41abdfbd
JG
905
906
907 /* float types should be passed in fpr's, as well as in the stack. */
940d5967 908 if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13) {
41abdfbd
JG
909
910 if (len > 8)
199b2450 911 printf_unfiltered (
41abdfbd
JG
912"Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
913
75621b2b
MS
914 memcpy (&registers[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)],
915 VALUE_CONTENTS (arg),
916 len);
41abdfbd
JG
917 ++f_argno;
918 }
919
359a097f 920 write_memory (sp+24+(ii*4), (char *) VALUE_CONTENTS (arg), len);
41abdfbd
JG
921 ii += ((len + 3) & -4) / 4;
922 }
923 }
6c6afbb9 924 else
41abdfbd
JG
925 /* Secure stack areas first, before doing anything else. */
926 write_register (SP_REGNUM, sp);
927
0ec1e44d
MA
928#ifndef USE_GENERIC_DUMMY_FRAMES
929/* we want to copy 24 bytes of target's frame to dummy's frame,
930 then set back chain to point to new frame. */
931
41abdfbd
JG
932 saved_sp = dummy_frame_addr [dummy_frame_count - 1];
933 read_memory (saved_sp, tmp_buffer, 24);
934 write_memory (sp, tmp_buffer, 24);
0ec1e44d 935#endif /* GENERIC_DUMMY_FRAMES */
41abdfbd 936
b112f2ae
JK
937 /* set back chain properly */
938 store_address (tmp_buffer, 4, saved_sp);
939 write_memory (sp, tmp_buffer, 4);
41abdfbd 940
5f1c39ef 941 target_store_registers (-1);
41abdfbd
JG
942 return sp;
943}
0ec1e44d
MA
944#ifdef ELF_OBJECT_FORMAT
945
946/* Function: ppc_push_return_address (pc, sp)
947 Set up the return address for the inferior function call. */
948
949CORE_ADDR
950ppc_push_return_address (pc, sp)
951 CORE_ADDR pc;
952 CORE_ADDR sp;
953{
954 write_register (LR_REGNUM, CALL_DUMMY_ADDRESS ());
955 return sp;
956}
957
958#endif
41abdfbd
JG
959
960/* a given return value in `regbuf' with a type `valtype', extract and copy its
961 value into `valbuf' */
962
ecf4059f 963void
41abdfbd 964extract_return_value (valtype, regbuf, valbuf)
cd8a3d84
SS
965 struct type *valtype;
966 char regbuf[REGISTER_BYTES];
967 char *valbuf;
41abdfbd 968{
07781ac0 969 int offset = 0;
41abdfbd
JG
970
971 if (TYPE_CODE (valtype) == TYPE_CODE_FLT) {
972
973 double dd; float ff;
974 /* floats and doubles are returned in fpr1. fpr's have a size of 8 bytes.
975 We need to truncate the return value into float size (4 byte) if
976 necessary. */
977
978 if (TYPE_LENGTH (valtype) > 4) /* this is a double */
75621b2b
MS
979 memcpy (valbuf,
980 &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)],
981 TYPE_LENGTH (valtype));
41abdfbd 982 else { /* float */
ade40d31 983 memcpy (&dd, &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)], 8);
41abdfbd 984 ff = (float)dd;
ade40d31 985 memcpy (valbuf, &ff, sizeof(float));
41abdfbd
JG
986 }
987 }
07781ac0 988 else {
41abdfbd 989 /* return value is copied starting from r3. */
07781ac0
PS
990 if (TARGET_BYTE_ORDER == BIG_ENDIAN
991 && TYPE_LENGTH (valtype) < REGISTER_RAW_SIZE (3))
992 offset = REGISTER_RAW_SIZE (3) - TYPE_LENGTH (valtype);
993
75621b2b
MS
994 memcpy (valbuf,
995 regbuf + REGISTER_BYTE (3) + offset,
07781ac0
PS
996 TYPE_LENGTH (valtype));
997 }
41abdfbd
JG
998}
999
1000
ecf4059f
JG
1001/* keep structure return address in this variable.
1002 FIXME: This is a horrid kludge which should not be allowed to continue
1003 living. This only allows a single nested call to a structure-returning
1004 function. Come on, guys! -- gnu@cygnus.com, Aug 92 */
41abdfbd
JG
1005
1006CORE_ADDR rs6000_struct_return_address;
1007
1008
c2e4669f
JG
1009/* Indirect function calls use a piece of trampoline code to do context
1010 switching, i.e. to set the new TOC table. Skip such code if we are on
1011 its first instruction (as when we have single-stepped to here).
07aa9fdc
PS
1012 Also skip shared library trampoline code (which is different from
1013 indirect function call trampolines).
c2e4669f
JG
1014 Result is desired PC to step until, or NULL if we are not in
1015 trampoline code. */
41abdfbd 1016
ecf4059f 1017CORE_ADDR
41abdfbd 1018skip_trampoline_code (pc)
cd8a3d84 1019 CORE_ADDR pc;
41abdfbd
JG
1020{
1021 register unsigned int ii, op;
07aa9fdc 1022 CORE_ADDR solib_target_pc;
41abdfbd
JG
1023
1024 static unsigned trampoline_code[] = {
1025 0x800b0000, /* l r0,0x0(r11) */
1026 0x90410014, /* st r2,0x14(r1) */
1027 0x7c0903a6, /* mtctr r0 */
1028 0x804b0004, /* l r2,0x4(r11) */
1029 0x816b0008, /* l r11,0x8(r11) */
1030 0x4e800420, /* bctr */
1031 0x4e800020, /* br */
1032 0
1033 };
1034
07aa9fdc
PS
1035 /* If pc is in a shared library trampoline, return its target. */
1036 solib_target_pc = find_solib_trampoline_target (pc);
1037 if (solib_target_pc)
1038 return solib_target_pc;
1039
41abdfbd
JG
1040 for (ii=0; trampoline_code[ii]; ++ii) {
1041 op = read_memory_integer (pc + (ii*4), 4);
1042 if (op != trampoline_code [ii])
359a097f 1043 return 0;
41abdfbd
JG
1044 }
1045 ii = read_register (11); /* r11 holds destination addr */
1046 pc = read_memory_integer (ii, 4); /* (r11) value */
1047 return pc;
1048}
1049
068c9fd6 1050/* Determines whether the function FI has a frame on the stack or not. */
cd8a3d84 1051
ecf4059f 1052int
068c9fd6
MM
1053frameless_function_invocation (fi)
1054 struct frame_info *fi;
ecf4059f
JG
1055{
1056 CORE_ADDR func_start;
63641491 1057 struct rs6000_framedata fdata;
ecf4059f 1058
4cc56716
PS
1059 /* Don't even think about framelessness except on the innermost frame
1060 or if the function was interrupted by a signal. */
1061 if (fi->next != NULL && !fi->next->signal_handler_caller)
b0e932ad
JK
1062 return 0;
1063
4cc56716 1064 func_start = get_pc_function_start (fi->pc);
ecf4059f
JG
1065
1066 /* If we failed to find the start of the function, it is a mistake
1067 to inspect the instructions. */
1068
1069 if (!func_start)
4cc56716
PS
1070 {
1071 /* A frame with a zero PC is usually created by dereferencing a NULL
1072 function pointer, normally causing an immediate core dump of the
1073 inferior. Mark function as frameless, as the inferior has no chance
1074 of setting up a stack frame. */
1075 if (fi->pc == 0)
1076 return 1;
1077 else
1078 return 0;
1079 }
ecf4059f 1080
4cc56716 1081 func_start += FUNCTION_START_OFFSET;
068c9fd6
MM
1082 (void) skip_prologue (func_start, &fdata);
1083 return fdata.frameless;
ecf4059f
JG
1084}
1085
068c9fd6 1086/* Return the PC saved in a frame */
cd8a3d84 1087
068c9fd6
MM
1088unsigned long
1089frame_saved_pc (fi)
1090 struct frame_info *fi;
1091{
1092 CORE_ADDR func_start;
1093 struct rs6000_framedata fdata;
068c9fd6 1094
965dde97
PS
1095 if (fi->signal_handler_caller)
1096 return read_memory_integer (fi->frame + SIG_FRAME_PC_OFFSET, 4);
1097
0ec1e44d
MA
1098#ifdef USE_GENERIC_DUMMY_FRAMES
1099 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
1100 return generic_read_register_dummy(fi->pc, fi->frame, PC_REGNUM);
1101#endif /* GENERIC_DUMMY_FRAMES */
1102
068c9fd6
MM
1103 func_start = get_pc_function_start (fi->pc) + FUNCTION_START_OFFSET;
1104
1105 /* If we failed to find the start of the function, it is a mistake
1106 to inspect the instructions. */
1107 if (!func_start)
1108 return 0;
1109
1110 (void) skip_prologue (func_start, &fdata);
068c9fd6 1111
4b4c6c96 1112 if (fdata.lr_offset == 0 && fi->next != NULL)
4cc56716
PS
1113 {
1114 if (fi->next->signal_handler_caller)
1115 return read_memory_integer (fi->next->frame + SIG_FRAME_LR_OFFSET, 4);
1116 else
1117 return read_memory_integer (rs6000_frame_chain (fi) + DEFAULT_LR_SAVE,
1118 4);
1119 }
4b4c6c96
MM
1120
1121 if (fdata.lr_offset == 0)
1122 return read_register (LR_REGNUM);
1123
068c9fd6
MM
1124 return read_memory_integer (rs6000_frame_chain (fi) + fdata.lr_offset, 4);
1125}
ecf4059f
JG
1126
1127/* If saved registers of frame FI are not known yet, read and cache them.
63641491 1128 &FDATAP contains rs6000_framedata; TDATAP can be NULL,
ecf4059f
JG
1129 in which case the framedata are read. */
1130
1131static void
1132frame_get_cache_fsr (fi, fdatap)
1133 struct frame_info *fi;
63641491 1134 struct rs6000_framedata *fdatap;
ecf4059f
JG
1135{
1136 int ii;
1137 CORE_ADDR frame_addr;
63641491 1138 struct rs6000_framedata work_fdata;
ecf4059f
JG
1139
1140 if (fi->cache_fsr)
1141 return;
1142
1143 if (fdatap == NULL) {
1144 fdatap = &work_fdata;
068c9fd6 1145 (void) skip_prologue (get_pc_function_start (fi->pc), fdatap);
ecf4059f
JG
1146 }
1147
1148 fi->cache_fsr = (struct frame_saved_regs *)
1149 obstack_alloc (&frame_cache_obstack, sizeof (struct frame_saved_regs));
4ed97c9a 1150 memset (fi->cache_fsr, '\0', sizeof (struct frame_saved_regs));
ecf4059f
JG
1151
1152 if (fi->prev && fi->prev->frame)
1153 frame_addr = fi->prev->frame;
1154 else
1155 frame_addr = read_memory_integer (fi->frame, 4);
1156
1157 /* if != -1, fdatap->saved_fpr is the smallest number of saved_fpr.
965dde97 1158 All fpr's from saved_fpr to fp31 are saved. */
ecf4059f
JG
1159
1160 if (fdatap->saved_fpr >= 0) {
965dde97
PS
1161 int fpr_offset = frame_addr + fdatap->fpr_offset;
1162 for (ii = fdatap->saved_fpr; ii < 32; ii++) {
1163 fi->cache_fsr->regs [FP0_REGNUM + ii] = fpr_offset;
1164 fpr_offset += 8;
1165 }
ecf4059f
JG
1166 }
1167
1168 /* if != -1, fdatap->saved_gpr is the smallest number of saved_gpr.
965dde97 1169 All gpr's from saved_gpr to gpr31 are saved. */
ecf4059f 1170
965dde97
PS
1171 if (fdatap->saved_gpr >= 0) {
1172 int gpr_offset = frame_addr + fdatap->gpr_offset;
1173 for (ii = fdatap->saved_gpr; ii < 32; ii++) {
1174 fi->cache_fsr->regs [ii] = gpr_offset;
1175 gpr_offset += 4;
1176 }
1177 }
1178
1179 /* If != 0, fdatap->cr_offset is the offset from the frame that holds
1180 the CR. */
1181 if (fdatap->cr_offset != 0)
1182 fi->cache_fsr->regs [CR_REGNUM] = frame_addr + fdatap->cr_offset;
1183
1184 /* If != 0, fdatap->lr_offset is the offset from the frame that holds
1185 the LR. */
1186 if (fdatap->lr_offset != 0)
1187 fi->cache_fsr->regs [LR_REGNUM] = frame_addr + fdatap->lr_offset;
ecf4059f
JG
1188}
1189
1190/* Return the address of a frame. This is the inital %sp value when the frame
1191 was first allocated. For functions calling alloca(), it might be saved in
1192 an alloca register. */
1193
1194CORE_ADDR
1195frame_initial_stack_address (fi)
1196 struct frame_info *fi;
1197{
1198 CORE_ADDR tmpaddr;
63641491 1199 struct rs6000_framedata fdata;
ecf4059f
JG
1200 struct frame_info *callee_fi;
1201
1202 /* if the initial stack pointer (frame address) of this frame is known,
1203 just return it. */
1204
1205 if (fi->initial_sp)
1206 return fi->initial_sp;
1207
1208 /* find out if this function is using an alloca register.. */
1209
068c9fd6 1210 (void) skip_prologue (get_pc_function_start (fi->pc), &fdata);
ecf4059f
JG
1211
1212 /* if saved registers of this frame are not known yet, read and cache them. */
1213
1214 if (!fi->cache_fsr)
1215 frame_get_cache_fsr (fi, &fdata);
1216
1217 /* If no alloca register used, then fi->frame is the value of the %sp for
1218 this frame, and it is good enough. */
1219
1220 if (fdata.alloca_reg < 0) {
1221 fi->initial_sp = fi->frame;
1222 return fi->initial_sp;
1223 }
1224
1225 /* This function has an alloca register. If this is the top-most frame
1226 (with the lowest address), the value in alloca register is good. */
1227
1228 if (!fi->next)
1229 return fi->initial_sp = read_register (fdata.alloca_reg);
1230
1231 /* Otherwise, this is a caller frame. Callee has usually already saved
1232 registers, but there are exceptions (such as when the callee
1233 has no parameters). Find the address in which caller's alloca
1234 register is saved. */
1235
1236 for (callee_fi = fi->next; callee_fi; callee_fi = callee_fi->next) {
1237
1238 if (!callee_fi->cache_fsr)
cdb1cc92 1239 frame_get_cache_fsr (callee_fi, NULL);
ecf4059f
JG
1240
1241 /* this is the address in which alloca register is saved. */
1242
1243 tmpaddr = callee_fi->cache_fsr->regs [fdata.alloca_reg];
1244 if (tmpaddr) {
1245 fi->initial_sp = read_memory_integer (tmpaddr, 4);
1246 return fi->initial_sp;
1247 }
1248
1249 /* Go look into deeper levels of the frame chain to see if any one of
1250 the callees has saved alloca register. */
1251 }
1252
1253 /* If alloca register was not saved, by the callee (or any of its callees)
1254 then the value in the register is still good. */
1255
1256 return fi->initial_sp = read_register (fdata.alloca_reg);
1257}
1258
669caa9c 1259CORE_ADDR
f3649227
JK
1260rs6000_frame_chain (thisframe)
1261 struct frame_info *thisframe;
1262{
669caa9c 1263 CORE_ADDR fp;
0ec1e44d
MA
1264
1265#ifdef USE_GENERIC_DUMMY_FRAMES
1266 if (PC_IN_CALL_DUMMY (thisframe->pc, thisframe->frame, thisframe->frame))
1267 return thisframe->frame; /* dummy frame same as caller's frame */
1268#endif /* GENERIC_DUMMY_FRAMES */
1269
1270 if (inside_entry_file (thisframe->pc) ||
1271 thisframe->pc == entry_point_address ())
f3649227 1272 return 0;
0ec1e44d 1273
cee86be3 1274 if (thisframe->signal_handler_caller)
9ed8604f 1275 fp = read_memory_integer (thisframe->frame + SIG_FRAME_FP_OFFSET, 4);
4cc56716
PS
1276 else if (thisframe->next != NULL
1277 && thisframe->next->signal_handler_caller
1278 && frameless_function_invocation (thisframe))
1279 /* A frameless function interrupted by a signal did not change the
1280 frame pointer. */
1281 fp = FRAME_FP (thisframe);
cee86be3
JK
1282 else
1283 fp = read_memory_integer ((thisframe)->frame, 4);
1284
0ec1e44d
MA
1285#ifdef USE_GENERIC_DUMMY_FRAMES
1286 {
1287 CORE_ADDR fpp, lr;
1288
1289 lr = read_register (LR_REGNUM);
1290 if (lr == entry_point_address ())
1291 if (fp != 0 && (fpp = read_memory_integer (fp, 4)) != 0)
1292 if (PC_IN_CALL_DUMMY (lr, fpp, fpp))
1293 return fpp;
1294 }
1295#endif /* GENERIC_DUMMY_FRAMES */
f3649227
JK
1296 return fp;
1297}
ecf4059f 1298\f
65eaea27
JL
1299/* Return nonzero if ADDR (a function pointer) is in the data space and
1300 is therefore a special function pointer. */
1301
1302int
1303is_magic_function_pointer (addr)
1304 CORE_ADDR addr;
1305{
1306 struct obj_section *s;
1307
1308 s = find_pc_section (addr);
1309 if (s && s->the_bfd_section->flags & SEC_CODE)
1310 return 0;
1311 else
1312 return 1;
1313}
1314
5c172b4b
MM
1315#ifdef GDB_TARGET_POWERPC
1316int
1317gdb_print_insn_powerpc (memaddr, info)
1318 bfd_vma memaddr;
1319 disassemble_info *info;
1320{
1321 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1322 return print_insn_big_powerpc (memaddr, info);
1323 else
1324 return print_insn_little_powerpc (memaddr, info);
1325}
1326#endif
1327
0ec1e44d
MA
1328/* Function: get_saved_register
1329 Just call the generic_get_saved_register function. */
1330
70423641 1331#ifdef USE_GENERIC_DUMMY_FRAMES
0ec1e44d
MA
1332void
1333get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
1334 char *raw_buffer;
1335 int *optimized;
1336 CORE_ADDR *addrp;
1337 struct frame_info *frame;
1338 int regnum;
1339 enum lval_type *lval;
1340{
1341 generic_get_saved_register (raw_buffer, optimized, addrp,
1342 frame, regnum, lval);
1343}
70423641 1344#endif
0ec1e44d
MA
1345
1346
18b46e7c
SS
1347void
1348_initialize_rs6000_tdep ()
1349{
1350 /* FIXME, this should not be decided via ifdef. */
1351#ifdef GDB_TARGET_POWERPC
5c172b4b 1352 tm_print_insn = gdb_print_insn_powerpc;
18b46e7c
SS
1353#else
1354 tm_print_insn = print_insn_rs6000;
1355#endif
1356}
This page took 0.415473 seconds and 4 git commands to generate.