1 /* Intel 386 target-dependent stuff.
2 Copyright (C) 1988, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
26 /* helper functions for tm-i386.h */
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
;
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++])
42 codestream_fill (peek_flag
)
44 codestream_addr
= codestream_next_addr
;
45 codestream_next_addr
+= sizeof (int);
47 codestream_cnt
= sizeof (int);
48 read_memory (codestream_addr
,
49 (unsigned char *)codestream_buf
,
53 return (codestream_peek());
55 return (codestream_get());
59 codestream_seek (place
)
61 codestream_next_addr
= place
& -sizeof (int);
64 while (codestream_tell() != place
)
69 codestream_read (buf
, count
)
75 for (i
= 0; i
< count
; i
++)
76 *p
++ = codestream_get ();
79 /* next instruction is a jump, move to target */
89 pos
= codestream_tell ();
92 if (codestream_peek () == 0x66)
98 switch (codestream_get ())
101 /* relative jump: if data16 == 0, disp32, else disp16 */
104 codestream_read ((unsigned char *)&short_delta
, 2);
106 /* include size of jmp inst (including the 0x66 prefix). */
107 pos
+= short_delta
+ 4;
111 codestream_read ((unsigned char *)&long_delta
, 4);
112 pos
+= long_delta
+ 5;
116 /* relative jump, disp8 (ignore data16) */
117 codestream_read ((unsigned char *)&byte_delta
, 1);
118 pos
+= byte_delta
+ 2;
121 codestream_seek (pos
);
125 * find & return amound a local space allocated, and advance codestream to
126 * first register push (if any)
128 * if entry sequence doesn't make sense, return -1, and leave
129 * codestream pointer random
132 i386_get_frame_setup (pc
)
136 codestream_seek (pc
);
140 op
= codestream_get ();
142 if (op
== 0x58) /* popl %eax */
145 * this function must start with
148 * xchgl %eax, (%esp) 0x87 0x04 0x24
149 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
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)
155 * this sequence is used to get the address of the return
156 * buffer for a function that returns a structure
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)
166 else if (bcmp (buf
, proto2
, 4) == 0)
169 codestream_seek (pos
);
170 op
= codestream_get (); /* update next opcode */
173 if (op
== 0x55) /* pushl %ebp */
175 /* check for movl %esp, %ebp - can be written two ways */
176 switch (codestream_get ())
179 if (codestream_get () != 0xec)
183 if (codestream_get () != 0xe5)
189 /* check for stack adjustment
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
197 op
= codestream_peek ();
200 /* subl with 8 bit immed */
202 if (codestream_get () != 0xec)
203 /* Some instruction starting with 0x83 other than subl. */
205 codestream_seek (codestream_tell () - 2);
208 /* subl with signed byte immediate
209 * (though it wouldn't make sense to be negative)
211 return (codestream_get());
215 /* subl with 32 bit immed */
218 if (codestream_get () != 0xec)
219 /* Some instruction starting with 0x81 other than subl. */
221 codestream_seek (codestream_tell () - 2);
224 /* subl with 32 bit immediate */
225 codestream_read ((unsigned char *)&locals
, 4);
226 SWAP_TARGET_AND_HOST (&locals
, 4);
236 /* enter instruction: arg is 16 bit unsigned immed */
237 unsigned short slocals
;
238 codestream_read ((unsigned char *)&slocals
, 2);
239 SWAP_TARGET_AND_HOST (&slocals
, 2);
240 codestream_get (); /* flush final byte of enter instruction */
246 /* Return number of args passed to a frame.
247 Can return -1, meaning no way to tell. */
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
256 i386_frame_num_args (fi
)
257 struct frame_info fi
;
261 struct frame_info
*pfi
;
265 FRAMELESS_FUNCTION_INVOCATION (fi
, 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. */
272 pfi
= get_prev_frame_info ((fi
));
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. */
285 op
= read_memory_integer (retpc
, 1);
291 op
= read_memory_integer (retpc
+1, 1);
293 /* addl $<signed imm 8 bits>, %esp */
294 return (read_memory_integer (retpc
+2,1)&0xff)/4;
299 { /* add with 32 bit immediate */
300 op
= read_memory_integer (retpc
+1, 1);
302 /* addl $<imm 32>, %esp */
303 return read_memory_integer (retpc
+2, 4) / 4;
315 * parse the first few instructions of the function to see
316 * what registers were stored.
318 * We handle these cases:
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.
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)
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
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.
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.
343 i386_frame_find_saved_regs (fip
, fsrp
)
344 struct frame_info
*fip
;
345 struct frame_saved_regs
*fsrp
;
350 CORE_ADDR dummy_bottom
;
354 bzero (fsrp
, sizeof *fsrp
);
356 /* if frame is the end of a dummy, compute where the
359 dummy_bottom
= fip
->frame
- 4 - REGISTER_BYTES
- CALL_DUMMY_LENGTH
;
361 /* check if the PC is in the stack, in a dummy frame */
362 if (dummy_bottom
<= fip
->pc
&& fip
->pc
<= fip
->frame
)
364 /* all regs were saved by push_call_dummy () */
366 for (i
= 0; i
< NUM_REGS
; i
++)
368 adr
-= REGISTER_RAW_SIZE (i
);
374 locals
= i386_get_frame_setup (get_pc_function_start (fip
->pc
));
378 adr
= fip
->frame
- 4 - locals
;
379 for (i
= 0; i
< 8; i
++)
381 op
= codestream_get ();
382 if (op
< 0x50 || op
> 0x57)
384 fsrp
->regs
[op
- 0x50] = adr
;
389 fsrp
->regs
[PC_REGNUM
] = fip
->frame
+ 4;
390 fsrp
->regs
[FP_REGNUM
] = fip
->frame
;
393 /* return pc of first real instruction */
394 i386_skip_prologue (pc
)
399 if (i386_get_frame_setup (pc
) < 0)
402 /* found valid frame setup - codestream now points to
403 * start of push instructions for saving registers
406 /* skip over register saves */
407 for (i
= 0; i
< 8; i
++)
409 op
= codestream_peek ();
410 /* break if not pushl inst */
411 if (op
< 0x50 || op
> 0x57)
418 return (codestream_tell ());
421 i386_push_dummy_frame ()
423 CORE_ADDR sp
= read_register (SP_REGNUM
);
425 char regbuf
[MAX_REGISTER_RAW_SIZE
];
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
++)
432 read_register_gen (regnum
, regbuf
);
433 sp
= push_bytes (sp
, regbuf
, REGISTER_RAW_SIZE (regnum
));
435 write_register (SP_REGNUM
, sp
);
440 FRAME frame
= get_current_frame ();
443 struct frame_saved_regs fsr
;
444 struct frame_info
*fi
;
445 char regbuf
[MAX_REGISTER_RAW_SIZE
];
447 fi
= get_frame_info (frame
);
449 get_frame_saved_regs (fi
, &fsr
);
450 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
453 adr
= fsr
.regs
[regnum
];
456 read_memory (adr
, regbuf
, REGISTER_RAW_SIZE (regnum
));
457 write_register_bytes (REGISTER_BYTE (regnum
), regbuf
,
458 REGISTER_RAW_SIZE (regnum
));
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
),