Commit | Line | Data |
---|---|---|
f2ebc25f JK |
1 | /* Intel 386 target-dependent stuff. |
2 | Copyright (C) 1988, 1989, 1991 Free Software Foundation, Inc. | |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
7d9884b9 | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
7d9884b9 JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
7d9884b9 | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
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 | |
7d9884b9 JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 RP |
19 | |
20 | #include <stdio.h> | |
21 | #include "defs.h" | |
bd5635a1 RP |
22 | #include "frame.h" |
23 | #include "inferior.h" | |
24 | #include "gdbcore.h" | |
25 | ||
f2ebc25f | 26 | /* helper functions for tm-i386.h */ |
bd5635a1 RP |
27 | |
28 | /* stdio style buffering to minimize calls to ptrace */ | |
29 | static CORE_ADDR codestream_next_addr; | |
30 | static CORE_ADDR codestream_addr; | |
31 | static unsigned char codestream_buf[sizeof (int)]; | |
32 | static int codestream_off; | |
33 | static int codestream_cnt; | |
34 | ||
35 | #define codestream_tell() (codestream_addr + codestream_off) | |
36 | #define codestream_peek() (codestream_cnt == 0 ? \ | |
37 | codestream_fill(1): codestream_buf[codestream_off]) | |
38 | #define codestream_get() (codestream_cnt-- == 0 ? \ | |
39 | codestream_fill(0) : codestream_buf[codestream_off++]) | |
40 | ||
41 | static unsigned char | |
42 | codestream_fill (peek_flag) | |
43 | { | |
44 | codestream_addr = codestream_next_addr; | |
45 | codestream_next_addr += sizeof (int); | |
46 | codestream_off = 0; | |
47 | codestream_cnt = sizeof (int); | |
48 | read_memory (codestream_addr, | |
49 | (unsigned char *)codestream_buf, | |
50 | sizeof (int)); | |
51 | ||
52 | if (peek_flag) | |
53 | return (codestream_peek()); | |
54 | else | |
55 | return (codestream_get()); | |
56 | } | |
57 | ||
58 | static void | |
59 | codestream_seek (place) | |
60 | { | |
61 | codestream_next_addr = place & -sizeof (int); | |
62 | codestream_cnt = 0; | |
63 | codestream_fill (1); | |
64 | while (codestream_tell() != place) | |
65 | codestream_get (); | |
66 | } | |
67 | ||
68 | static void | |
69 | codestream_read (buf, count) | |
70 | unsigned char *buf; | |
71 | { | |
72 | unsigned char *p; | |
73 | int i; | |
74 | p = buf; | |
75 | for (i = 0; i < count; i++) | |
76 | *p++ = codestream_get (); | |
77 | } | |
78 | ||
79 | /* next instruction is a jump, move to target */ | |
80 | static | |
81 | i386_follow_jump () | |
82 | { | |
83 | int long_delta; | |
84 | short short_delta; | |
85 | char byte_delta; | |
86 | int data16; | |
87 | int pos; | |
88 | ||
89 | pos = codestream_tell (); | |
90 | ||
91 | data16 = 0; | |
92 | if (codestream_peek () == 0x66) | |
93 | { | |
94 | codestream_get (); | |
95 | data16 = 1; | |
96 | } | |
97 | ||
98 | switch (codestream_get ()) | |
99 | { | |
100 | case 0xe9: | |
101 | /* relative jump: if data16 == 0, disp32, else disp16 */ | |
102 | if (data16) | |
103 | { | |
104 | codestream_read ((unsigned char *)&short_delta, 2); | |
f2ebc25f JK |
105 | |
106 | /* include size of jmp inst (including the 0x66 prefix). */ | |
107 | pos += short_delta + 4; | |
bd5635a1 RP |
108 | } |
109 | else | |
110 | { | |
111 | codestream_read ((unsigned char *)&long_delta, 4); | |
112 | pos += long_delta + 5; | |
113 | } | |
114 | break; | |
115 | case 0xeb: | |
116 | /* relative jump, disp8 (ignore data16) */ | |
117 | codestream_read ((unsigned char *)&byte_delta, 1); | |
118 | pos += byte_delta + 2; | |
119 | break; | |
120 | } | |
f2ebc25f | 121 | codestream_seek (pos); |
bd5635a1 RP |
122 | } |
123 | ||
124 | /* | |
125 | * find & return amound a local space allocated, and advance codestream to | |
126 | * first register push (if any) | |
127 | * | |
128 | * if entry sequence doesn't make sense, return -1, and leave | |
129 | * codestream pointer random | |
130 | */ | |
131 | static long | |
132 | i386_get_frame_setup (pc) | |
133 | { | |
134 | unsigned char op; | |
135 | ||
136 | codestream_seek (pc); | |
137 | ||
138 | i386_follow_jump (); | |
139 | ||
140 | op = codestream_get (); | |
141 | ||
142 | if (op == 0x58) /* popl %eax */ | |
143 | { | |
144 | /* | |
145 | * this function must start with | |
146 | * | |
147 | * popl %eax 0x58 | |
148 | * xchgl %eax, (%esp) 0x87 0x04 0x24 | |
149 | * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00 | |
150 | * | |
151 | * (the system 5 compiler puts out the second xchg | |
152 | * inst, and the assembler doesn't try to optimize it, | |
153 | * so the 'sib' form gets generated) | |
154 | * | |
155 | * this sequence is used to get the address of the return | |
156 | * buffer for a function that returns a structure | |
157 | */ | |
158 | int pos; | |
159 | unsigned char buf[4]; | |
160 | static unsigned char proto1[3] = { 0x87,0x04,0x24 }; | |
161 | static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 }; | |
162 | pos = codestream_tell (); | |
163 | codestream_read (buf, 4); | |
164 | if (bcmp (buf, proto1, 3) == 0) | |
165 | pos += 3; | |
166 | else if (bcmp (buf, proto2, 4) == 0) | |
167 | pos += 4; | |
168 | ||
169 | codestream_seek (pos); | |
170 | op = codestream_get (); /* update next opcode */ | |
171 | } | |
172 | ||
173 | if (op == 0x55) /* pushl %ebp */ | |
174 | { | |
175 | /* check for movl %esp, %ebp - can be written two ways */ | |
176 | switch (codestream_get ()) | |
177 | { | |
178 | case 0x8b: | |
179 | if (codestream_get () != 0xec) | |
180 | return (-1); | |
181 | break; | |
182 | case 0x89: | |
183 | if (codestream_get () != 0xe5) | |
184 | return (-1); | |
185 | break; | |
186 | default: | |
187 | return (-1); | |
188 | } | |
189 | /* check for stack adjustment | |
190 | * | |
191 | * subl $XXX, %esp | |
192 | * | |
193 | * note: you can't subtract a 16 bit immediate | |
194 | * from a 32 bit reg, so we don't have to worry | |
195 | * about a data16 prefix | |
196 | */ | |
197 | op = codestream_peek (); | |
198 | if (op == 0x83) | |
199 | { | |
200 | /* subl with 8 bit immed */ | |
201 | codestream_get (); | |
202 | if (codestream_get () != 0xec) | |
203 | /* Some instruction starting with 0x83 other than subl. */ | |
204 | { | |
205 | codestream_seek (codestream_tell () - 2); | |
206 | return 0; | |
207 | } | |
208 | /* subl with signed byte immediate | |
209 | * (though it wouldn't make sense to be negative) | |
210 | */ | |
211 | return (codestream_get()); | |
212 | } | |
213 | else if (op == 0x81) | |
214 | { | |
215 | /* subl with 32 bit immed */ | |
216 | int locals; | |
217 | codestream_get(); | |
218 | if (codestream_get () != 0xec) | |
219 | /* Some instruction starting with 0x81 other than subl. */ | |
220 | { | |
221 | codestream_seek (codestream_tell () - 2); | |
222 | return 0; | |
223 | } | |
224 | /* subl with 32 bit immediate */ | |
225 | codestream_read ((unsigned char *)&locals, 4); | |
f2ebc25f | 226 | SWAP_TARGET_AND_HOST (&locals, 4); |
bd5635a1 RP |
227 | return (locals); |
228 | } | |
229 | else | |
230 | { | |
231 | return (0); | |
232 | } | |
233 | } | |
234 | else if (op == 0xc8) | |
235 | { | |
236 | /* enter instruction: arg is 16 bit unsigned immed */ | |
237 | unsigned short slocals; | |
238 | codestream_read ((unsigned char *)&slocals, 2); | |
f2ebc25f | 239 | SWAP_TARGET_AND_HOST (&slocals, 2); |
bd5635a1 RP |
240 | codestream_get (); /* flush final byte of enter instruction */ |
241 | return (slocals); | |
242 | } | |
243 | return (-1); | |
244 | } | |
245 | ||
246 | /* Return number of args passed to a frame. | |
247 | Can return -1, meaning no way to tell. */ | |
248 | ||
249 | /* on the 386, the instruction following the call could be: | |
250 | * popl %ecx - one arg | |
251 | * addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits | |
252 | * anything else - zero args | |
253 | */ | |
254 | ||
255 | int | |
256 | i386_frame_num_args (fi) | |
257 | struct frame_info fi; | |
258 | { | |
259 | int retpc; | |
260 | unsigned char op; | |
261 | struct frame_info *pfi; | |
262 | ||
263 | int frameless; | |
264 | ||
265 | FRAMELESS_FUNCTION_INVOCATION (fi, frameless); | |
266 | if (frameless) | |
267 | /* In the absence of a frame pointer, GDB doesn't get correct values | |
268 | for nameless arguments. Return -1, so it doesn't print any | |
269 | nameless arguments. */ | |
270 | return -1; | |
271 | ||
272 | pfi = get_prev_frame_info ((fi)); | |
273 | if (pfi == 0) | |
274 | { | |
275 | /* Note: this can happen if we are looking at the frame for | |
276 | main, because FRAME_CHAIN_VALID won't let us go into | |
277 | start. If we have debugging symbols, that's not really | |
278 | a big deal; it just means it will only show as many arguments | |
279 | to main as are declared. */ | |
280 | return -1; | |
281 | } | |
282 | else | |
283 | { | |
284 | retpc = pfi->pc; | |
285 | op = read_memory_integer (retpc, 1); | |
286 | if (op == 0x59) | |
287 | /* pop %ecx */ | |
288 | return 1; | |
289 | else if (op == 0x83) | |
290 | { | |
291 | op = read_memory_integer (retpc+1, 1); | |
292 | if (op == 0xc4) | |
293 | /* addl $<signed imm 8 bits>, %esp */ | |
294 | return (read_memory_integer (retpc+2,1)&0xff)/4; | |
295 | else | |
296 | return 0; | |
297 | } | |
298 | else if (op == 0x81) | |
299 | { /* add with 32 bit immediate */ | |
300 | op = read_memory_integer (retpc+1, 1); | |
301 | if (op == 0xc4) | |
302 | /* addl $<imm 32>, %esp */ | |
303 | return read_memory_integer (retpc+2, 4) / 4; | |
304 | else | |
305 | return 0; | |
306 | } | |
307 | else | |
308 | { | |
309 | return 0; | |
310 | } | |
311 | } | |
312 | } | |
313 | ||
314 | /* | |
315 | * parse the first few instructions of the function to see | |
316 | * what registers were stored. | |
317 | * | |
318 | * We handle these cases: | |
319 | * | |
320 | * The startup sequence can be at the start of the function, | |
321 | * or the function can start with a branch to startup code at the end. | |
322 | * | |
323 | * %ebp can be set up with either the 'enter' instruction, or | |
324 | * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful, | |
325 | * but was once used in the sys5 compiler) | |
326 | * | |
327 | * Local space is allocated just below the saved %ebp by either the | |
328 | * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has | |
329 | * a 16 bit unsigned argument for space to allocate, and the | |
330 | * 'addl' instruction could have either a signed byte, or | |
331 | * 32 bit immediate. | |
332 | * | |
333 | * Next, the registers used by this function are pushed. In | |
334 | * the sys5 compiler they will always be in the order: %edi, %esi, %ebx | |
335 | * (and sometimes a harmless bug causes it to also save but not restore %eax); | |
336 | * however, the code below is willing to see the pushes in any order, | |
337 | * and will handle up to 8 of them. | |
338 | * | |
339 | * If the setup sequence is at the end of the function, then the | |
340 | * next instruction will be a branch back to the start. | |
341 | */ | |
342 | ||
343 | i386_frame_find_saved_regs (fip, fsrp) | |
344 | struct frame_info *fip; | |
345 | struct frame_saved_regs *fsrp; | |
346 | { | |
347 | long locals; | |
348 | unsigned char *p; | |
349 | unsigned char op; | |
350 | CORE_ADDR dummy_bottom; | |
351 | CORE_ADDR adr; | |
352 | int i; | |
353 | ||
354 | bzero (fsrp, sizeof *fsrp); | |
355 | ||
356 | /* if frame is the end of a dummy, compute where the | |
357 | * beginning would be | |
358 | */ | |
359 | dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH; | |
360 | ||
361 | /* check if the PC is in the stack, in a dummy frame */ | |
362 | if (dummy_bottom <= fip->pc && fip->pc <= fip->frame) | |
363 | { | |
364 | /* all regs were saved by push_call_dummy () */ | |
365 | adr = fip->frame; | |
366 | for (i = 0; i < NUM_REGS; i++) | |
367 | { | |
368 | adr -= REGISTER_RAW_SIZE (i); | |
369 | fsrp->regs[i] = adr; | |
370 | } | |
371 | return; | |
372 | } | |
373 | ||
374 | locals = i386_get_frame_setup (get_pc_function_start (fip->pc)); | |
375 | ||
376 | if (locals >= 0) | |
377 | { | |
378 | adr = fip->frame - 4 - locals; | |
379 | for (i = 0; i < 8; i++) | |
380 | { | |
381 | op = codestream_get (); | |
382 | if (op < 0x50 || op > 0x57) | |
383 | break; | |
384 | fsrp->regs[op - 0x50] = adr; | |
385 | adr -= 4; | |
386 | } | |
387 | } | |
388 | ||
389 | fsrp->regs[PC_REGNUM] = fip->frame + 4; | |
390 | fsrp->regs[FP_REGNUM] = fip->frame; | |
391 | } | |
392 | ||
393 | /* return pc of first real instruction */ | |
394 | i386_skip_prologue (pc) | |
395 | { | |
396 | unsigned char op; | |
397 | int i; | |
398 | ||
399 | if (i386_get_frame_setup (pc) < 0) | |
400 | return (pc); | |
401 | ||
402 | /* found valid frame setup - codestream now points to | |
403 | * start of push instructions for saving registers | |
404 | */ | |
405 | ||
406 | /* skip over register saves */ | |
407 | for (i = 0; i < 8; i++) | |
408 | { | |
409 | op = codestream_peek (); | |
410 | /* break if not pushl inst */ | |
411 | if (op < 0x50 || op > 0x57) | |
412 | break; | |
413 | codestream_get (); | |
414 | } | |
415 | ||
416 | i386_follow_jump (); | |
417 | ||
418 | return (codestream_tell ()); | |
419 | } | |
420 | ||
421 | i386_push_dummy_frame () | |
422 | { | |
423 | CORE_ADDR sp = read_register (SP_REGNUM); | |
424 | int regnum; | |
425 | char regbuf[MAX_REGISTER_RAW_SIZE]; | |
426 | ||
427 | sp = push_word (sp, read_register (PC_REGNUM)); | |
428 | sp = push_word (sp, read_register (FP_REGNUM)); | |
429 | write_register (FP_REGNUM, sp); | |
430 | for (regnum = 0; regnum < NUM_REGS; regnum++) | |
431 | { | |
432 | read_register_gen (regnum, regbuf); | |
433 | sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum)); | |
434 | } | |
435 | write_register (SP_REGNUM, sp); | |
436 | } | |
437 | ||
438 | i386_pop_frame () | |
439 | { | |
440 | FRAME frame = get_current_frame (); | |
441 | CORE_ADDR fp; | |
442 | int regnum; | |
443 | struct frame_saved_regs fsr; | |
444 | struct frame_info *fi; | |
445 | char regbuf[MAX_REGISTER_RAW_SIZE]; | |
446 | ||
447 | fi = get_frame_info (frame); | |
448 | fp = fi->frame; | |
449 | get_frame_saved_regs (fi, &fsr); | |
450 | for (regnum = 0; regnum < NUM_REGS; regnum++) | |
451 | { | |
452 | CORE_ADDR adr; | |
453 | adr = fsr.regs[regnum]; | |
454 | if (adr) | |
455 | { | |
456 | read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum)); | |
457 | write_register_bytes (REGISTER_BYTE (regnum), regbuf, | |
458 | REGISTER_RAW_SIZE (regnum)); | |
459 | } | |
460 | } | |
461 | write_register (FP_REGNUM, read_memory_integer (fp, 4)); | |
462 | write_register (PC_REGNUM, read_memory_integer (fp + 4, 4)); | |
463 | write_register (SP_REGNUM, fp + 8); | |
464 | flush_cached_frames (); | |
465 | set_current_frame ( create_new_frame (read_register (FP_REGNUM), | |
466 | read_pc ())); | |
467 | } |