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