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