| 1 | /* Target-dependent code for GDB, the GNU debugger. |
| 2 | Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "frame.h" |
| 22 | #include "inferior.h" |
| 23 | #include "symtab.h" |
| 24 | #include "target.h" |
| 25 | |
| 26 | #include <sys/param.h> |
| 27 | #include <sys/dir.h> |
| 28 | #include <sys/user.h> |
| 29 | #include <signal.h> |
| 30 | #include <sys/ioctl.h> |
| 31 | #include <fcntl.h> |
| 32 | |
| 33 | #include <sys/ptrace.h> |
| 34 | #include <sys/reg.h> |
| 35 | |
| 36 | #include <a.out.h> |
| 37 | #include <sys/file.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/core.h> |
| 40 | |
| 41 | extern int errno; |
| 42 | extern int attach_flag; |
| 43 | |
| 44 | /* Nonzero if we just simulated a single step break. */ |
| 45 | int one_stepped; |
| 46 | |
| 47 | |
| 48 | /* Breakpoint shadows for the single step instructions will be kept here. */ |
| 49 | |
| 50 | static struct sstep_breaks { |
| 51 | int address; |
| 52 | int data; |
| 53 | } stepBreaks[2]; |
| 54 | |
| 55 | |
| 56 | /* |
| 57 | * Calculate the destination of a branch/jump. Return -1 if not a branch. |
| 58 | */ |
| 59 | static int |
| 60 | branch_dest (opcode, instr, pc, safety) |
| 61 | int opcode, instr, pc, safety; |
| 62 | { |
| 63 | register long offset; |
| 64 | unsigned dest; |
| 65 | int immediate; |
| 66 | int absolute; |
| 67 | int ext_op; |
| 68 | |
| 69 | absolute = (int) ((instr >> 1) & 1); |
| 70 | |
| 71 | switch (opcode) { |
| 72 | case 18 : |
| 73 | immediate = ((instr & ~3) << 6) >> 6; /* br unconditionl */ |
| 74 | |
| 75 | case 16 : |
| 76 | if (opcode != 18) /* br conditional */ |
| 77 | immediate = ((instr & ~3) << 16) >> 16; |
| 78 | if (absolute) |
| 79 | dest = immediate; |
| 80 | else |
| 81 | dest = pc + immediate; |
| 82 | break; |
| 83 | |
| 84 | case 19 : |
| 85 | ext_op = (instr>>1) & 0x3ff; |
| 86 | |
| 87 | if (ext_op == 16) /* br conditional register */ |
| 88 | dest = read_register (LR_REGNUM) & ~3; |
| 89 | |
| 90 | else if (ext_op == 528) /* br cond to count reg */ |
| 91 | dest = read_register (CTR_REGNUM) & ~3; |
| 92 | |
| 93 | else return -1; |
| 94 | break; |
| 95 | |
| 96 | default: return -1; |
| 97 | } |
| 98 | return (dest < TEXT_SEGMENT_BASE) ? safety : dest; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | /* AIX does not support PT_STEP. Simulate it. */ |
| 104 | |
| 105 | int |
| 106 | single_step (signal) |
| 107 | int signal; |
| 108 | { |
| 109 | #define INSNLEN(OPCODE) 4 |
| 110 | |
| 111 | static char breakp[] = BREAKPOINT; |
| 112 | int ii, insn, ret, loc; |
| 113 | int breaks[2], opcode; |
| 114 | |
| 115 | if (!one_stepped) { |
| 116 | extern CORE_ADDR text_start; |
| 117 | loc = read_pc (); |
| 118 | |
| 119 | ret = read_memory (loc, &insn, sizeof (int)); |
| 120 | if (ret) |
| 121 | printf ("Error in single_step()!!\n"); |
| 122 | |
| 123 | breaks[0] = loc + INSNLEN(insn); |
| 124 | opcode = insn >> 26; |
| 125 | breaks[1] = branch_dest (opcode, insn, loc, breaks[0]); |
| 126 | |
| 127 | /* Don't put two breakpoints on the same address. */ |
| 128 | if (breaks[1] == breaks[0]) |
| 129 | breaks[1] = -1; |
| 130 | |
| 131 | stepBreaks[1].address = -1; |
| 132 | |
| 133 | for (ii=0; ii < 2; ++ii) { |
| 134 | |
| 135 | /* ignore invalid breakpoint. */ |
| 136 | if ( breaks[ii] == -1) |
| 137 | continue; |
| 138 | |
| 139 | read_memory (breaks[ii], &(stepBreaks[ii].data), sizeof(int)); |
| 140 | |
| 141 | ret = write_memory (breaks[ii], breakp, sizeof(int)); |
| 142 | stepBreaks[ii].address = breaks[ii]; |
| 143 | } |
| 144 | |
| 145 | one_stepped = 1; |
| 146 | ptrace (PT_CONTINUE, inferior_pid, 1, signal, 0); |
| 147 | } |
| 148 | else { |
| 149 | |
| 150 | /* remove step breakpoints. */ |
| 151 | for (ii=0; ii < 2; ++ii) |
| 152 | if (stepBreaks[ii].address != -1) |
| 153 | write_memory |
| 154 | (stepBreaks[ii].address, &(stepBreaks[ii].data), sizeof(int)); |
| 155 | |
| 156 | one_stepped = 0; |
| 157 | } |
| 158 | errno = 0; |
| 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | /* return pc value after skipping a function prologue. */ |
| 164 | |
| 165 | skip_prologue (pc) |
| 166 | int pc; |
| 167 | { |
| 168 | unsigned int tmp; |
| 169 | unsigned int op; |
| 170 | |
| 171 | if (target_read_memory (pc, (char *)&op, sizeof (op))) |
| 172 | return pc; /* Can't access it -- assume no prologue. */ |
| 173 | SWAP_TARGET_AND_HOST (&op, sizeof (op)); |
| 174 | |
| 175 | /* Assume that subsequent fetches can fail with low probability. */ |
| 176 | |
| 177 | if (op == 0x7c0802a6) { /* mflr r0 */ |
| 178 | pc += 4; |
| 179 | op = read_memory_integer (pc, 4); |
| 180 | } |
| 181 | |
| 182 | if ((op & 0xfc00003e) == 0x7c000026) { /* mfcr Rx */ |
| 183 | pc += 4; |
| 184 | op = read_memory_integer (pc, 4); |
| 185 | } |
| 186 | |
| 187 | if ((op & 0xfc000000) == 0x48000000) { /* bl foo, to save fprs??? */ |
| 188 | pc += 4; |
| 189 | op = read_memory_integer (pc, 4); |
| 190 | |
| 191 | /* At this point, make sure this is not a trampoline function |
| 192 | (a function that simply calls another functions, and nothing else). |
| 193 | If the next is not a nop, this branch was part of the function |
| 194 | prologue. */ |
| 195 | |
| 196 | if (op == 0x4def7b82 || /* crorc 15, 15, 15 */ |
| 197 | op == 0x0) |
| 198 | return pc - 4; /* don't skip over this branch */ |
| 199 | } |
| 200 | |
| 201 | if ((op & 0xfc1f0000) == 0xbc010000) { /* stm Rx, NUM(r1) */ |
| 202 | pc += 4; |
| 203 | op = read_memory_integer (pc, 4); |
| 204 | } |
| 205 | |
| 206 | while (((tmp = op >> 16) == 0x9001) || /* st r0, NUM(r1) */ |
| 207 | (tmp == 0x9421) || /* stu r1, NUM(r1) */ |
| 208 | (op == 0x93e1fffc)) /* st r31,-4(r1) */ |
| 209 | { |
| 210 | pc += 4; |
| 211 | op = read_memory_integer (pc, 4); |
| 212 | } |
| 213 | |
| 214 | while ((tmp = (op >> 22)) == 0x20f) { /* l r31, ... or */ |
| 215 | pc += 4; /* l r30, ... */ |
| 216 | op = read_memory_integer (pc, 4); |
| 217 | } |
| 218 | |
| 219 | /* store parameters into stack */ |
| 220 | while( |
| 221 | (op & 0xfc1f0000) == 0xd8010000 || /* stfd Rx,NUM(r1) */ |
| 222 | (op & 0xfc1f0000) == 0x90010000 || /* st r?, NUM(r1) */ |
| 223 | (op & 0xfc000000) == 0xfc000000 || /* frsp, fp?, .. */ |
| 224 | (op & 0xd0000000) == 0xd0000000) /* stfs, fp?, .. */ |
| 225 | { |
| 226 | pc += 4; /* store fpr double */ |
| 227 | op = read_memory_integer (pc, 4); |
| 228 | } |
| 229 | |
| 230 | if (op == 0x603f0000) { /* oril r31, r1, 0x0 */ |
| 231 | pc += 4; /* this happens if r31 is used as */ |
| 232 | op = read_memory_integer (pc, 4); /* frame ptr. (gcc does that) */ |
| 233 | |
| 234 | tmp = 0; |
| 235 | while ((op >> 16) == (0x907f + tmp)) { /* st r3, NUM(r31) */ |
| 236 | pc += 4; /* st r4, NUM(r31), ... */ |
| 237 | op = read_memory_integer (pc, 4); |
| 238 | tmp += 0x20; |
| 239 | } |
| 240 | } |
| 241 | #if 0 |
| 242 | /* I have problems with skipping over __main() that I need to address |
| 243 | * sometime. Previously, I used to use misc_function_vector which |
| 244 | * didn't work as well as I wanted to be. -MGO */ |
| 245 | |
| 246 | /* If the first thing after skipping a prolog is a branch to a function, |
| 247 | this might be a call to an initializer in main(), introduced by gcc2. |
| 248 | We'd like to skip over it as well. Fortunately, xlc does some extra |
| 249 | work before calling a function right after a prologue, thus we can |
| 250 | single out such gcc2 behaviour. */ |
| 251 | |
| 252 | |
| 253 | if ((op & 0xfc000001) == 0x48000001) { /* bl foo, an initializer function? */ |
| 254 | op = read_memory_integer (pc+4, 4); |
| 255 | |
| 256 | if (op == 0x4def7b82) { /* cror 0xf, 0xf, 0xf (nop) */ |
| 257 | |
| 258 | /* check and see if we are in main. If so, skip over this initializer |
| 259 | function as well. */ |
| 260 | |
| 261 | tmp = find_pc_misc_function (pc); |
| 262 | if (tmp >= 0 && !strcmp (misc_function_vector [tmp].name, "main")) |
| 263 | return pc + 8; |
| 264 | } |
| 265 | } |
| 266 | #endif /* 0 */ |
| 267 | |
| 268 | return pc; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | /* text start and end addresses in virtual memory. */ |
| 273 | |
| 274 | CORE_ADDR text_start; |
| 275 | CORE_ADDR text_end; |
| 276 | |
| 277 | |
| 278 | /************************************************************************* |
| 279 | Support for creating pushind a dummy frame into the stack, and popping |
| 280 | frames, etc. |
| 281 | *************************************************************************/ |
| 282 | |
| 283 | /* The total size of dummy frame is 436, which is; |
| 284 | |
| 285 | 32 gpr's - 128 bytes |
| 286 | 32 fpr's - 256 " |
| 287 | 7 the rest - 28 " |
| 288 | and 24 extra bytes for the callee's link area. The last 24 bytes |
| 289 | for the link area might not be necessary, since it will be taken |
| 290 | care of by push_arguments(). */ |
| 291 | |
| 292 | #define DUMMY_FRAME_SIZE 436 |
| 293 | |
| 294 | #define DUMMY_FRAME_ADDR_SIZE 10 |
| 295 | |
| 296 | /* Make sure you initialize these in somewhere, in case gdb gives up what it |
| 297 | was debugging and starts debugging something else. FIXMEibm */ |
| 298 | |
| 299 | static int dummy_frame_count = 0; |
| 300 | static int dummy_frame_size = 0; |
| 301 | static CORE_ADDR *dummy_frame_addr = 0; |
| 302 | |
| 303 | extern int stop_stack_dummy; |
| 304 | |
| 305 | /* push a dummy frame into stack, save all register. Currently we are saving |
| 306 | only gpr's and fpr's, which is not good enough! FIXMEmgo */ |
| 307 | |
| 308 | push_dummy_frame () |
| 309 | { |
| 310 | int sp, pc; /* stack pointer and link register */ |
| 311 | int ii; |
| 312 | |
| 313 | fetch_inferior_registers (-1); |
| 314 | |
| 315 | if (dummy_frame_count >= dummy_frame_size) { |
| 316 | dummy_frame_size += DUMMY_FRAME_ADDR_SIZE; |
| 317 | if (dummy_frame_addr) |
| 318 | dummy_frame_addr = (CORE_ADDR*) xrealloc |
| 319 | (dummy_frame_addr, sizeof(CORE_ADDR) * (dummy_frame_size)); |
| 320 | else |
| 321 | dummy_frame_addr = (CORE_ADDR*) |
| 322 | xmalloc (sizeof(CORE_ADDR) * (dummy_frame_size)); |
| 323 | } |
| 324 | |
| 325 | sp = read_register(SP_REGNUM); |
| 326 | pc = read_register(PC_REGNUM); |
| 327 | |
| 328 | dummy_frame_addr [dummy_frame_count++] = sp; |
| 329 | |
| 330 | /* Be careful! If the stack pointer is not decremented first, then kernel |
| 331 | thinks he is free to use the space underneath it. And kernel actually |
| 332 | uses that area for IPC purposes when executing ptrace(2) calls. So |
| 333 | before writing register values into the new frame, decrement and update |
| 334 | %sp first in order to secure your frame. */ |
| 335 | |
| 336 | write_register (SP_REGNUM, sp-DUMMY_FRAME_SIZE); |
| 337 | |
| 338 | /* gdb relies on the state of current_frame. We'd better update it, |
| 339 | otherwise things like do_registers_info() wouldn't work properly! */ |
| 340 | |
| 341 | flush_cached_frames (); |
| 342 | set_current_frame (create_new_frame (sp-DUMMY_FRAME_SIZE, pc)); |
| 343 | |
| 344 | /* save program counter in link register's space. */ |
| 345 | write_memory (sp+8, &pc, 4); |
| 346 | |
| 347 | /* save all floating point and general purpose registers here. */ |
| 348 | |
| 349 | /* fpr's, f0..f31 */ |
| 350 | for (ii = 0; ii < 32; ++ii) |
| 351 | write_memory (sp-8-(ii*8), ®isters[REGISTER_BYTE (31-ii+FP0_REGNUM)], 8); |
| 352 | |
| 353 | /* gpr's r0..r31 */ |
| 354 | for (ii=1; ii <=32; ++ii) |
| 355 | write_memory (sp-256-(ii*4), ®isters[REGISTER_BYTE (32-ii)], 4); |
| 356 | |
| 357 | /* so far, 32*2 + 32 words = 384 bytes have been written. |
| 358 | 7 extra registers in our register set: pc, ps, cnd, lr, cnt, xer, mq */ |
| 359 | |
| 360 | for (ii=1; ii <= (LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii) { |
| 361 | write_memory (sp-384-(ii*4), |
| 362 | ®isters[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4); |
| 363 | } |
| 364 | |
| 365 | /* Save sp or so called back chain right here. */ |
| 366 | write_memory (sp-DUMMY_FRAME_SIZE, &sp, 4); |
| 367 | sp -= DUMMY_FRAME_SIZE; |
| 368 | |
| 369 | /* And finally, this is the back chain. */ |
| 370 | write_memory (sp+8, &pc, 4); |
| 371 | } |
| 372 | |
| 373 | |
| 374 | /* Pop a dummy frame. |
| 375 | |
| 376 | In rs6000 when we push a dummy frame, we save all of the registers. This |
| 377 | is usually done before user calls a function explicitly. |
| 378 | |
| 379 | After a dummy frame is pushed, some instructions are copied into stack, |
| 380 | and stack pointer is decremented even more. Since we don't have a frame |
| 381 | pointer to get back to the parent frame of the dummy, we start having |
| 382 | trouble poping it. Therefore, we keep a dummy frame stack, keeping |
| 383 | addresses of dummy frames as such. When poping happens and when we |
| 384 | detect that was a dummy frame, we pop it back to its parent by using |
| 385 | dummy frame stack (`dummy_frame_addr' array). |
| 386 | */ |
| 387 | |
| 388 | pop_dummy_frame () |
| 389 | { |
| 390 | CORE_ADDR sp, pc; |
| 391 | int ii; |
| 392 | sp = dummy_frame_addr [--dummy_frame_count]; |
| 393 | |
| 394 | /* restore all fpr's. */ |
| 395 | for (ii = 1; ii <= 32; ++ii) |
| 396 | read_memory (sp-(ii*8), ®isters[REGISTER_BYTE (32-ii+FP0_REGNUM)], 8); |
| 397 | |
| 398 | /* restore all gpr's */ |
| 399 | for (ii=1; ii <= 32; ++ii) { |
| 400 | read_memory (sp-256-(ii*4), ®isters[REGISTER_BYTE (32-ii)], 4); |
| 401 | } |
| 402 | |
| 403 | /* restore the rest of the registers. */ |
| 404 | for (ii=1; ii <=(LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii) |
| 405 | read_memory (sp-384-(ii*4), |
| 406 | ®isters[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4); |
| 407 | |
| 408 | read_memory (sp-(DUMMY_FRAME_SIZE-8), |
| 409 | ®isters [REGISTER_BYTE(PC_REGNUM)], 4); |
| 410 | |
| 411 | /* when a dummy frame was being pushed, we had to decrement %sp first, in |
| 412 | order to secure astack space. Thus, saved %sp (or %r1) value, is not the |
| 413 | one we should restore. Change it with the one we need. */ |
| 414 | |
| 415 | *(int*)®isters [REGISTER_BYTE(FP_REGNUM)] = sp; |
| 416 | |
| 417 | /* Now we can restore all registers. */ |
| 418 | |
| 419 | store_inferior_registers (-1); |
| 420 | pc = read_pc (); |
| 421 | flush_cached_frames (); |
| 422 | set_current_frame (create_new_frame (sp, pc)); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | /* pop the innermost frame, go back to the caller. */ |
| 427 | |
| 428 | pop_frame () |
| 429 | { |
| 430 | int pc, lr, sp, prev_sp; /* %pc, %lr, %sp */ |
| 431 | struct aix_framedata fdata; |
| 432 | FRAME fr = get_current_frame (); |
| 433 | int addr, ii; |
| 434 | |
| 435 | pc = read_pc (); |
| 436 | sp = FRAME_FP (fr); |
| 437 | |
| 438 | if (stop_stack_dummy && dummy_frame_count) { |
| 439 | pop_dummy_frame (); |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | /* figure out previous %pc value. If the function is frameless, it is |
| 444 | still in the link register, otherwise walk the frames and retrieve the |
| 445 | saved %pc value in the previous frame. */ |
| 446 | |
| 447 | addr = get_pc_function_start (fr->pc) + FUNCTION_START_OFFSET; |
| 448 | function_frame_info (addr, &fdata); |
| 449 | |
| 450 | read_memory (sp, &prev_sp, 4); |
| 451 | if (fdata.frameless) |
| 452 | lr = read_register (LR_REGNUM); |
| 453 | else |
| 454 | read_memory (prev_sp+8, &lr, 4); |
| 455 | |
| 456 | /* reset %pc value. */ |
| 457 | write_register (PC_REGNUM, lr); |
| 458 | |
| 459 | /* reset register values if any was saved earlier. */ |
| 460 | addr = prev_sp - fdata.offset; |
| 461 | |
| 462 | if (fdata.saved_gpr != -1) |
| 463 | for (ii=fdata.saved_gpr; ii <= 31; ++ii) { |
| 464 | read_memory (addr, ®isters [REGISTER_BYTE (ii)], 4); |
| 465 | addr += sizeof (int); |
| 466 | } |
| 467 | |
| 468 | if (fdata.saved_fpr != -1) |
| 469 | for (ii=fdata.saved_fpr; ii <= 31; ++ii) { |
| 470 | read_memory (addr, ®isters [REGISTER_BYTE (ii+FP0_REGNUM)], 8); |
| 471 | addr += 8; |
| 472 | } |
| 473 | |
| 474 | write_register (SP_REGNUM, prev_sp); |
| 475 | store_inferior_registers (-1); |
| 476 | flush_cached_frames (); |
| 477 | set_current_frame (create_new_frame (prev_sp, lr)); |
| 478 | } |
| 479 | |
| 480 | |
| 481 | /* fixup the call sequence of a dummy function, with the real function address. |
| 482 | its argumets will be passed by gdb. */ |
| 483 | |
| 484 | fix_call_dummy(dummyname, pc, fun, nargs, type) |
| 485 | char *dummyname; |
| 486 | int pc; |
| 487 | int fun; |
| 488 | int nargs; /* not used */ |
| 489 | int type; /* not used */ |
| 490 | |
| 491 | { |
| 492 | #define TOC_ADDR_OFFSET 20 |
| 493 | #define TARGET_ADDR_OFFSET 28 |
| 494 | |
| 495 | int ii; |
| 496 | unsigned long target_addr; |
| 497 | unsigned long tocvalue; |
| 498 | |
| 499 | target_addr = fun; |
| 500 | tocvalue = find_toc_address (target_addr); |
| 501 | |
| 502 | ii = *(int*)((char*)dummyname + TOC_ADDR_OFFSET); |
| 503 | ii = (ii & 0xffff0000) | (tocvalue >> 16); |
| 504 | *(int*)((char*)dummyname + TOC_ADDR_OFFSET) = ii; |
| 505 | |
| 506 | ii = *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4); |
| 507 | ii = (ii & 0xffff0000) | (tocvalue & 0x0000ffff); |
| 508 | *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4) = ii; |
| 509 | |
| 510 | ii = *(int*)((char*)dummyname + TARGET_ADDR_OFFSET); |
| 511 | ii = (ii & 0xffff0000) | (target_addr >> 16); |
| 512 | *(int*)((char*)dummyname + TARGET_ADDR_OFFSET) = ii; |
| 513 | |
| 514 | ii = *(int*)((char*)dummyname + TARGET_ADDR_OFFSET+4); |
| 515 | ii = (ii & 0xffff0000) | (target_addr & 0x0000ffff); |
| 516 | *(int*)((char*)dummyname + TARGET_ADDR_OFFSET+4) = ii; |
| 517 | } |
| 518 | |
| 519 | |
| 520 | |
| 521 | /* return information about a function frame. |
| 522 | in struct aix_frameinfo fdata: |
| 523 | - frameless is TRUE, if function does not save %pc value in its frame. |
| 524 | - offset is the number of bytes used in the frame to save registers. |
| 525 | - saved_gpr is the number of the first saved gpr. |
| 526 | - saved_fpr is the number of the first saved fpr. |
| 527 | - alloca_reg is the number of the register used for alloca() handling. |
| 528 | Otherwise -1. |
| 529 | */ |
| 530 | function_frame_info (pc, fdata) |
| 531 | int pc; |
| 532 | struct aix_framedata *fdata; |
| 533 | { |
| 534 | unsigned int tmp; |
| 535 | register unsigned int op; |
| 536 | |
| 537 | fdata->offset = 0; |
| 538 | fdata->saved_gpr = fdata->saved_fpr = fdata->alloca_reg = -1; |
| 539 | |
| 540 | op = read_memory_integer (pc, 4); |
| 541 | if (op == 0x7c0802a6) { /* mflr r0 */ |
| 542 | pc += 4; |
| 543 | op = read_memory_integer (pc, 4); |
| 544 | fdata->frameless = 0; |
| 545 | } |
| 546 | else /* else, this is a frameless invocation */ |
| 547 | fdata->frameless = 1; |
| 548 | |
| 549 | |
| 550 | if ((op & 0xfc00003e) == 0x7c000026) { /* mfcr Rx */ |
| 551 | pc += 4; |
| 552 | op = read_memory_integer (pc, 4); |
| 553 | } |
| 554 | |
| 555 | if ((op & 0xfc000000) == 0x48000000) { /* bl foo, to save fprs??? */ |
| 556 | pc += 4; |
| 557 | op = read_memory_integer (pc, 4); |
| 558 | /* At this point, make sure this is not a trampoline function |
| 559 | (a function that simply calls another functions, and nothing else). |
| 560 | If the next is not a nop, this branch was part of the function |
| 561 | prologue. */ |
| 562 | |
| 563 | if (op == 0x4def7b82 || /* crorc 15, 15, 15 */ |
| 564 | op == 0x0) |
| 565 | return; /* prologue is over */ |
| 566 | } |
| 567 | |
| 568 | if ((op & 0xfc1f0000) == 0xd8010000) { /* stfd Rx,NUM(r1) */ |
| 569 | pc += 4; /* store floating register double */ |
| 570 | op = read_memory_integer (pc, 4); |
| 571 | } |
| 572 | |
| 573 | if ((op & 0xfc1f0000) == 0xbc010000) { /* stm Rx, NUM(r1) */ |
| 574 | int tmp2; |
| 575 | fdata->saved_gpr = (op >> 21) & 0x1f; |
| 576 | tmp2 = op & 0xffff; |
| 577 | if (tmp2 > 0x7fff) |
| 578 | tmp2 = 0xffff0000 | tmp2; |
| 579 | |
| 580 | if (tmp2 < 0) { |
| 581 | tmp2 = tmp2 * -1; |
| 582 | fdata->saved_fpr = (tmp2 - ((32 - fdata->saved_gpr) * 4)) / 8; |
| 583 | if ( fdata->saved_fpr > 0) |
| 584 | fdata->saved_fpr = 32 - fdata->saved_fpr; |
| 585 | else |
| 586 | fdata->saved_fpr = -1; |
| 587 | } |
| 588 | fdata->offset = tmp2; |
| 589 | pc += 4; |
| 590 | op = read_memory_integer (pc, 4); |
| 591 | } |
| 592 | |
| 593 | while (((tmp = op >> 16) == 0x9001) || /* st r0, NUM(r1) */ |
| 594 | (tmp == 0x9421) || /* stu r1, NUM(r1) */ |
| 595 | (op == 0x93e1fffc)) /* st r31,-4(r1) */ |
| 596 | { |
| 597 | /* gcc takes a short cut and uses this instruction to save r31 only. */ |
| 598 | |
| 599 | if (op == 0x93e1fffc) { |
| 600 | if (fdata->offset) |
| 601 | /* fatal ("Unrecognized prolog."); */ |
| 602 | printf ("Unrecognized prolog!\n"); |
| 603 | |
| 604 | fdata->saved_gpr = 31; |
| 605 | fdata->offset = 4; |
| 606 | } |
| 607 | pc += 4; |
| 608 | op = read_memory_integer (pc, 4); |
| 609 | } |
| 610 | |
| 611 | while ((tmp = (op >> 22)) == 0x20f) { /* l r31, ... or */ |
| 612 | pc += 4; /* l r30, ... */ |
| 613 | op = read_memory_integer (pc, 4); |
| 614 | } |
| 615 | |
| 616 | /* store parameters into stack */ |
| 617 | while( |
| 618 | (op & 0xfc1f0000) == 0xd8010000 || /* stfd Rx,NUM(r1) */ |
| 619 | (op & 0xfc1f0000) == 0x90010000 || /* st r?, NUM(r1) */ |
| 620 | (op & 0xfc000000) == 0xfc000000 || /* frsp, fp?, .. */ |
| 621 | (op & 0xd0000000) == 0xd0000000) /* stfs, fp?, .. */ |
| 622 | { |
| 623 | pc += 4; /* store fpr double */ |
| 624 | op = read_memory_integer (pc, 4); |
| 625 | } |
| 626 | |
| 627 | if (op == 0x603f0000) /* oril r31, r1, 0x0 */ |
| 628 | fdata->alloca_reg = 31; |
| 629 | } |
| 630 | |
| 631 | |
| 632 | /* Pass the arguments in either registers, or in the stack. In RS6000, the first |
| 633 | eight words of the argument list (that might be less than eight parameters if |
| 634 | some parameters occupy more than one word) are passed in r3..r11 registers. |
| 635 | float and double parameters are passed in fpr's, in addition to that. Rest of |
| 636 | the parameters if any are passed in user stack. There might be cases in which |
| 637 | half of the parameter is copied into registers, the other half is pushed into |
| 638 | stack. |
| 639 | |
| 640 | If the function is returning a structure, then the return address is passed |
| 641 | in r3, then the first 7 words of the parametes can be passed in registers, |
| 642 | starting from r4. */ |
| 643 | |
| 644 | CORE_ADDR |
| 645 | push_arguments (nargs, args, sp, struct_return, struct_addr) |
| 646 | int nargs; |
| 647 | value *args; |
| 648 | CORE_ADDR sp; |
| 649 | int struct_return; |
| 650 | CORE_ADDR struct_addr; |
| 651 | { |
| 652 | int ii, len; |
| 653 | int argno; /* current argument number */ |
| 654 | int argbytes; /* current argument byte */ |
| 655 | char tmp_buffer [50]; |
| 656 | value arg; |
| 657 | int f_argno = 0; /* current floating point argno */ |
| 658 | |
| 659 | CORE_ADDR saved_sp, pc; |
| 660 | |
| 661 | if ( dummy_frame_count <= 0) |
| 662 | printf ("FATAL ERROR -push_arguments()! frame not found!!\n"); |
| 663 | |
| 664 | /* The first eight words of ther arguments are passed in registers. Copy |
| 665 | them appropriately. |
| 666 | |
| 667 | If the function is returning a `struct', then the first word (which |
| 668 | will be passed in r3) is used for struct return address. In that |
| 669 | case we should advance one word and start from r4 register to copy |
| 670 | parameters. */ |
| 671 | |
| 672 | ii = struct_return ? 1 : 0; |
| 673 | |
| 674 | for (argno=0, argbytes=0; argno < nargs && ii<8; ++ii) { |
| 675 | |
| 676 | arg = value_arg_coerce (args[argno]); |
| 677 | len = TYPE_LENGTH (VALUE_TYPE (arg)); |
| 678 | |
| 679 | if (TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_FLT) { |
| 680 | |
| 681 | /* floating point arguments are passed in fpr's, as well as gpr's. |
| 682 | There are 13 fpr's reserved for passing parameters. At this point |
| 683 | there is no way we would run out of them. */ |
| 684 | |
| 685 | if (len > 8) |
| 686 | printf ( |
| 687 | "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno); |
| 688 | |
| 689 | bcopy (VALUE_CONTENTS (arg), |
| 690 | ®isters[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)], len); |
| 691 | ++f_argno; |
| 692 | } |
| 693 | |
| 694 | if (len > 4) { |
| 695 | |
| 696 | /* Argument takes more than one register. */ |
| 697 | while (argbytes < len) { |
| 698 | |
| 699 | *(int*)®isters[REGISTER_BYTE(ii+3)] = 0; |
| 700 | bcopy ( ((char*)VALUE_CONTENTS (arg))+argbytes, |
| 701 | ®isters[REGISTER_BYTE(ii+3)], |
| 702 | (len - argbytes) > 4 ? 4 : len - argbytes); |
| 703 | ++ii, argbytes += 4; |
| 704 | |
| 705 | if (ii >= 8) |
| 706 | goto ran_out_of_registers_for_arguments; |
| 707 | } |
| 708 | argbytes = 0; |
| 709 | --ii; |
| 710 | } |
| 711 | else { /* Argument can fit in one register. No problem. */ |
| 712 | *(int*)®isters[REGISTER_BYTE(ii+3)] = 0; |
| 713 | bcopy (VALUE_CONTENTS (arg), ®isters[REGISTER_BYTE(ii+3)], len); |
| 714 | } |
| 715 | ++argno; |
| 716 | } |
| 717 | |
| 718 | ran_out_of_registers_for_arguments: |
| 719 | |
| 720 | /* location for 8 parameters are always reserved. */ |
| 721 | sp -= 4 * 8; |
| 722 | |
| 723 | /* another six words for back chain, TOC register, link register, etc. */ |
| 724 | sp -= 24; |
| 725 | |
| 726 | /* if there are more arguments, allocate space for them in |
| 727 | the stack, then push them starting from the ninth one. */ |
| 728 | |
| 729 | if ((argno < nargs) || argbytes) { |
| 730 | int space = 0, jj; |
| 731 | value val; |
| 732 | |
| 733 | if (argbytes) { |
| 734 | space += ((len - argbytes + 3) & -4); |
| 735 | jj = argno + 1; |
| 736 | } |
| 737 | else |
| 738 | jj = argno; |
| 739 | |
| 740 | for (; jj < nargs; ++jj) { |
| 741 | val = value_arg_coerce (args[jj]); |
| 742 | space += ((TYPE_LENGTH (VALUE_TYPE (val))) + 3) & -4; |
| 743 | } |
| 744 | |
| 745 | /* add location required for the rest of the parameters */ |
| 746 | space = (space + 7) & -8; |
| 747 | sp -= space; |
| 748 | |
| 749 | /* This is another instance we need to be concerned about securing our |
| 750 | stack space. If we write anything underneath %sp (r1), we might conflict |
| 751 | with the kernel who thinks he is free to use this area. So, update %sp |
| 752 | first before doing anything else. */ |
| 753 | |
| 754 | write_register (SP_REGNUM, sp); |
| 755 | |
| 756 | /* if the last argument copied into the registers didn't fit there |
| 757 | completely, push the rest of it into stack. */ |
| 758 | |
| 759 | if (argbytes) { |
| 760 | write_memory ( |
| 761 | sp+24+(ii*4), ((char*)VALUE_CONTENTS (arg))+argbytes, len - argbytes); |
| 762 | ++argno; |
| 763 | ii += ((len - argbytes + 3) & -4) / 4; |
| 764 | } |
| 765 | |
| 766 | /* push the rest of the arguments into stack. */ |
| 767 | for (; argno < nargs; ++argno) { |
| 768 | |
| 769 | arg = value_arg_coerce (args[argno]); |
| 770 | len = TYPE_LENGTH (VALUE_TYPE (arg)); |
| 771 | |
| 772 | |
| 773 | /* float types should be passed in fpr's, as well as in the stack. */ |
| 774 | if (TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_FLT && f_argno < 13) { |
| 775 | |
| 776 | if (len > 8) |
| 777 | printf ( |
| 778 | "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno); |
| 779 | |
| 780 | bcopy (VALUE_CONTENTS (arg), |
| 781 | ®isters[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)], len); |
| 782 | ++f_argno; |
| 783 | } |
| 784 | |
| 785 | write_memory (sp+24+(ii*4), VALUE_CONTENTS (arg), len); |
| 786 | ii += ((len + 3) & -4) / 4; |
| 787 | } |
| 788 | } |
| 789 | else |
| 790 | /* Secure stack areas first, before doing anything else. */ |
| 791 | write_register (SP_REGNUM, sp); |
| 792 | |
| 793 | saved_sp = dummy_frame_addr [dummy_frame_count - 1]; |
| 794 | read_memory (saved_sp, tmp_buffer, 24); |
| 795 | write_memory (sp, tmp_buffer, 24); |
| 796 | |
| 797 | write_memory (sp, &saved_sp, 4); /* set back chain properly */ |
| 798 | |
| 799 | store_inferior_registers (-1); |
| 800 | return sp; |
| 801 | } |
| 802 | |
| 803 | /* a given return value in `regbuf' with a type `valtype', extract and copy its |
| 804 | value into `valbuf' */ |
| 805 | |
| 806 | extract_return_value (valtype, regbuf, valbuf) |
| 807 | struct type *valtype; |
| 808 | char regbuf[REGISTER_BYTES]; |
| 809 | char *valbuf; |
| 810 | { |
| 811 | |
| 812 | if (TYPE_CODE (valtype) == TYPE_CODE_FLT) { |
| 813 | |
| 814 | double dd; float ff; |
| 815 | /* floats and doubles are returned in fpr1. fpr's have a size of 8 bytes. |
| 816 | We need to truncate the return value into float size (4 byte) if |
| 817 | necessary. */ |
| 818 | |
| 819 | if (TYPE_LENGTH (valtype) > 4) /* this is a double */ |
| 820 | bcopy (®buf[REGISTER_BYTE (FP0_REGNUM + 1)], valbuf, |
| 821 | TYPE_LENGTH (valtype)); |
| 822 | else { /* float */ |
| 823 | bcopy (®buf[REGISTER_BYTE (FP0_REGNUM + 1)], &dd, 8); |
| 824 | ff = (float)dd; |
| 825 | bcopy (&ff, valbuf, sizeof(float)); |
| 826 | } |
| 827 | } |
| 828 | else |
| 829 | /* return value is copied starting from r3. */ |
| 830 | bcopy (®buf[REGISTER_BYTE (3)], valbuf, TYPE_LENGTH (valtype)); |
| 831 | } |
| 832 | |
| 833 | |
| 834 | /* keep keep structure return address in this variable. */ |
| 835 | |
| 836 | CORE_ADDR rs6000_struct_return_address; |
| 837 | |
| 838 | |
| 839 | /* Throw away this debugging code. FIXMEmgo. */ |
| 840 | print_frame(fram) |
| 841 | int fram; |
| 842 | { |
| 843 | int ii, val; |
| 844 | for (ii=0; ii<40; ++ii) { |
| 845 | if ((ii % 4) == 0) |
| 846 | printf ("\n"); |
| 847 | val = read_memory_integer (fram + ii * 4, 4); |
| 848 | printf ("0x%08x\t", val); |
| 849 | } |
| 850 | printf ("\n"); |
| 851 | } |
| 852 | |
| 853 | |
| 854 | |
| 855 | /* Indirect function calls use a piece of trampoline code to do context |
| 856 | switching, i.e. to set the new TOC table. Skip such code if we are on |
| 857 | its first instruction (as when we have single-stepped to here). |
| 858 | Result is desired PC to step until, or NULL if we are not in |
| 859 | trampoline code. */ |
| 860 | |
| 861 | skip_trampoline_code (pc) |
| 862 | int pc; |
| 863 | { |
| 864 | register unsigned int ii, op; |
| 865 | |
| 866 | static unsigned trampoline_code[] = { |
| 867 | 0x800b0000, /* l r0,0x0(r11) */ |
| 868 | 0x90410014, /* st r2,0x14(r1) */ |
| 869 | 0x7c0903a6, /* mtctr r0 */ |
| 870 | 0x804b0004, /* l r2,0x4(r11) */ |
| 871 | 0x816b0008, /* l r11,0x8(r11) */ |
| 872 | 0x4e800420, /* bctr */ |
| 873 | 0x4e800020, /* br */ |
| 874 | 0 |
| 875 | }; |
| 876 | |
| 877 | for (ii=0; trampoline_code[ii]; ++ii) { |
| 878 | op = read_memory_integer (pc + (ii*4), 4); |
| 879 | if (op != trampoline_code [ii]) |
| 880 | return NULL; |
| 881 | } |
| 882 | ii = read_register (11); /* r11 holds destination addr */ |
| 883 | pc = read_memory_integer (ii, 4); /* (r11) value */ |
| 884 | return pc; |
| 885 | } |
| 886 | |