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