1 /* Intel 386 target-dependent stuff.
2 Copyright (C) 1988, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB 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 1, or (at your option)
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #include <sys/types.h>
31 #include <sys/param.h>
35 #include <sys/ioctl.h>
40 #define N_SET_MAGIC(exec, val) ((exec).magic = (val))
42 #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
49 /* helper functions for tm-i386.h */
51 /* stdio style buffering to minimize calls to ptrace */
52 static CORE_ADDR codestream_next_addr;
53 static CORE_ADDR codestream_addr;
54 static unsigned char codestream_buf[sizeof (int)];
55 static int codestream_off;
56 static int codestream_cnt;
58 #define codestream_tell() (codestream_addr + codestream_off)
59 #define codestream_peek() (codestream_cnt == 0 ? \
60 codestream_fill(1): codestream_buf[codestream_off])
61 #define codestream_get() (codestream_cnt-- == 0 ? \
62 codestream_fill(0) : codestream_buf[codestream_off++])
65 codestream_fill (peek_flag)
67 codestream_addr = codestream_next_addr;
68 codestream_next_addr += sizeof (int);
70 codestream_cnt = sizeof (int);
71 read_memory (codestream_addr,
72 (unsigned char *)codestream_buf,
76 return (codestream_peek());
78 return (codestream_get());
82 codestream_seek (place)
84 codestream_next_addr = place & -sizeof (int);
87 while (codestream_tell() != place)
92 codestream_read (buf, count)
98 for (i = 0; i < count; i++)
99 *p++ = codestream_get ();
102 /* next instruction is a jump, move to target */
112 pos = codestream_tell ();
115 if (codestream_peek () == 0x66)
121 switch (codestream_get ())
124 /* relative jump: if data16 == 0, disp32, else disp16 */
127 codestream_read ((unsigned char *)&short_delta, 2);
129 /* include size of jmp inst (including the 0x66 prefix). */
130 pos += short_delta + 4;
134 codestream_read ((unsigned char *)&long_delta, 4);
135 pos += long_delta + 5;
139 /* relative jump, disp8 (ignore data16) */
140 codestream_read ((unsigned char *)&byte_delta, 1);
141 pos += byte_delta + 2;
144 codestream_seek (pos);
148 * find & return amound a local space allocated, and advance codestream to
149 * first register push (if any)
151 * if entry sequence doesn't make sense, return -1, and leave
152 * codestream pointer random
155 i386_get_frame_setup (pc)
159 codestream_seek (pc);
163 op = codestream_get ();
165 if (op == 0x58) /* popl %eax */
168 * this function must start with
171 * xchgl %eax, (%esp) 0x87 0x04 0x24
172 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
174 * (the system 5 compiler puts out the second xchg
175 * inst, and the assembler doesn't try to optimize it,
176 * so the 'sib' form gets generated)
178 * this sequence is used to get the address of the return
179 * buffer for a function that returns a structure
182 unsigned char buf[4];
183 static unsigned char proto1[3] = { 0x87,0x04,0x24 };
184 static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
185 pos = codestream_tell ();
186 codestream_read (buf, 4);
187 if (bcmp (buf, proto1, 3) == 0)
189 else if (bcmp (buf, proto2, 4) == 0)
192 codestream_seek (pos);
193 op = codestream_get (); /* update next opcode */
196 if (op == 0x55) /* pushl %ebp */
198 /* check for movl %esp, %ebp - can be written two ways */
199 switch (codestream_get ())
202 if (codestream_get () != 0xec)
206 if (codestream_get () != 0xe5)
212 /* check for stack adjustment
216 * note: you can't subtract a 16 bit immediate
217 * from a 32 bit reg, so we don't have to worry
218 * about a data16 prefix
220 op = codestream_peek ();
223 /* subl with 8 bit immed */
225 if (codestream_get () != 0xec)
226 /* Some instruction starting with 0x83 other than subl. */
228 codestream_seek (codestream_tell () - 2);
231 /* subl with signed byte immediate
232 * (though it wouldn't make sense to be negative)
234 return (codestream_get());
238 /* subl with 32 bit immed */
241 if (codestream_get () != 0xec)
242 /* Some instruction starting with 0x81 other than subl. */
244 codestream_seek (codestream_tell () - 2);
247 /* subl with 32 bit immediate */
248 codestream_read ((unsigned char *)&locals, 4);
249 SWAP_TARGET_AND_HOST (&locals, 4);
259 /* enter instruction: arg is 16 bit unsigned immed */
260 unsigned short slocals;
261 codestream_read ((unsigned char *)&slocals, 2);
262 SWAP_TARGET_AND_HOST (&slocals, 2);
263 codestream_get (); /* flush final byte of enter instruction */
269 /* Return number of args passed to a frame.
270 Can return -1, meaning no way to tell. */
272 /* on the 386, the instruction following the call could be:
273 * popl %ecx - one arg
274 * addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
275 * anything else - zero args
279 i386_frame_num_args (fi)
280 struct frame_info fi;
284 struct frame_info *pfi;
288 FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
290 /* In the absence of a frame pointer, GDB doesn't get correct values
291 for nameless arguments. Return -1, so it doesn't print any
292 nameless arguments. */
295 pfi = get_prev_frame_info ((fi));
298 /* Note: this can happen if we are looking at the frame for
299 main, because FRAME_CHAIN_VALID won't let us go into
300 start. If we have debugging symbols, that's not really
301 a big deal; it just means it will only show as many arguments
302 to main as are declared. */
308 op = read_memory_integer (retpc, 1);
314 op = read_memory_integer (retpc+1, 1);
316 /* addl $<signed imm 8 bits>, %esp */
317 return (read_memory_integer (retpc+2,1)&0xff)/4;
322 { /* add with 32 bit immediate */
323 op = read_memory_integer (retpc+1, 1);
325 /* addl $<imm 32>, %esp */
326 return read_memory_integer (retpc+2, 4) / 4;
338 * parse the first few instructions of the function to see
339 * what registers were stored.
341 * We handle these cases:
343 * The startup sequence can be at the start of the function,
344 * or the function can start with a branch to startup code at the end.
346 * %ebp can be set up with either the 'enter' instruction, or
347 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
348 * but was once used in the sys5 compiler)
350 * Local space is allocated just below the saved %ebp by either the
351 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
352 * a 16 bit unsigned argument for space to allocate, and the
353 * 'addl' instruction could have either a signed byte, or
356 * Next, the registers used by this function are pushed. In
357 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
358 * (and sometimes a harmless bug causes it to also save but not restore %eax);
359 * however, the code below is willing to see the pushes in any order,
360 * and will handle up to 8 of them.
362 * If the setup sequence is at the end of the function, then the
363 * next instruction will be a branch back to the start.
366 i386_frame_find_saved_regs (fip, fsrp)
367 struct frame_info *fip;
368 struct frame_saved_regs *fsrp;
373 CORE_ADDR dummy_bottom;
377 bzero (fsrp, sizeof *fsrp);
379 /* if frame is the end of a dummy, compute where the
382 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
384 /* check if the PC is in the stack, in a dummy frame */
385 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
387 /* all regs were saved by push_call_dummy () */
389 for (i = 0; i < NUM_REGS; i++)
391 adr -= REGISTER_RAW_SIZE (i);
397 locals = i386_get_frame_setup (get_pc_function_start (fip->pc));
401 adr = fip->frame - 4 - locals;
402 for (i = 0; i < 8; i++)
404 op = codestream_get ();
405 if (op < 0x50 || op > 0x57)
407 fsrp->regs[op - 0x50] = adr;
412 fsrp->regs[PC_REGNUM] = fip->frame + 4;
413 fsrp->regs[FP_REGNUM] = fip->frame;
416 /* return pc of first real instruction */
417 i386_skip_prologue (pc)
422 if (i386_get_frame_setup (pc) < 0)
425 /* found valid frame setup - codestream now points to
426 * start of push instructions for saving registers
429 /* skip over register saves */
430 for (i = 0; i < 8; i++)
432 op = codestream_peek ();
433 /* break if not pushl inst */
434 if (op < 0x50 || op > 0x57)
441 return (codestream_tell ());
444 i386_push_dummy_frame ()
446 CORE_ADDR sp = read_register (SP_REGNUM);
448 char regbuf[MAX_REGISTER_RAW_SIZE];
450 sp = push_word (sp, read_register (PC_REGNUM));
451 sp = push_word (sp, read_register (FP_REGNUM));
452 write_register (FP_REGNUM, sp);
453 for (regnum = 0; regnum < NUM_REGS; regnum++)
455 read_register_gen (regnum, regbuf);
456 sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
458 write_register (SP_REGNUM, sp);
463 FRAME frame = get_current_frame ();
466 struct frame_saved_regs fsr;
467 struct frame_info *fi;
468 char regbuf[MAX_REGISTER_RAW_SIZE];
470 fi = get_frame_info (frame);
472 get_frame_saved_regs (fi, &fsr);
473 for (regnum = 0; regnum < NUM_REGS; regnum++)
476 adr = fsr.regs[regnum];
479 read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
480 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
481 REGISTER_RAW_SIZE (regnum));
484 write_register (FP_REGNUM, read_memory_integer (fp, 4));
485 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
486 write_register (SP_REGNUM, fp + 8);
487 flush_cached_frames ();
488 set_current_frame ( create_new_frame (read_register (FP_REGNUM),