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