* utils.c (quit): Also call gdb_flush on standard output and error.
[deliverable/binutils-gdb.git] / gdb / i386-tdep.c
CommitLineData
f2ebc25f
JK
1/* Intel 386 target-dependent stuff.
2 Copyright (C) 1988, 1989, 1991 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
7d9884b9 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
7d9884b9
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
7d9884b9 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
7d9884b9
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
bd5635a1 20#include "defs.h"
bd5635a1
RP
21#include "frame.h"
22#include "inferior.h"
23#include "gdbcore.h"
51b57ded 24#include "target.h"
bd5635a1 25
d747e0af
MT
26static long
27i386_get_frame_setup PARAMS ((int));
28
29static void
30i386_follow_jump PARAMS ((void));
31
32static void
33codestream_read PARAMS ((unsigned char *, int));
34
35static void
36codestream_seek PARAMS ((int));
37
38static unsigned char
39codestream_fill PARAMS ((int));
40
f2ebc25f 41/* helper functions for tm-i386.h */
bd5635a1 42
d747e0af
MT
43/* Stdio style buffering was used to minimize calls to ptrace, but this
44 buffering did not take into account that the code section being accessed
45 may not be an even number of buffers long (even if the buffer is only
46 sizeof(int) long). In cases where the code section size happened to
47 be a non-integral number of buffers long, attempting to read the last
48 buffer would fail. Simply using target_read_memory and ignoring errors,
49 rather than read_memory, is not the correct solution, since legitimate
50 access errors would then be totally ignored. To properly handle this
51 situation and continue to use buffering would require that this code
52 be able to determine the minimum code section size granularity (not the
53 alignment of the section itself, since the actual failing case that
54 pointed out this problem had a section alignment of 4 but was not a
55 multiple of 4 bytes long), on a target by target basis, and then
56 adjust it's buffer size accordingly. This is messy, but potentially
57 feasible. It probably needs the bfd library's help and support. For
58 now, the buffer size is set to 1. (FIXME -fnf) */
59
60#define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
bd5635a1
RP
61static CORE_ADDR codestream_next_addr;
62static CORE_ADDR codestream_addr;
d747e0af 63static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
bd5635a1
RP
64static int codestream_off;
65static int codestream_cnt;
66
67#define codestream_tell() (codestream_addr + codestream_off)
68#define codestream_peek() (codestream_cnt == 0 ? \
69 codestream_fill(1): codestream_buf[codestream_off])
70#define codestream_get() (codestream_cnt-- == 0 ? \
71 codestream_fill(0) : codestream_buf[codestream_off++])
72
73static unsigned char
74codestream_fill (peek_flag)
d747e0af 75 int peek_flag;
bd5635a1
RP
76{
77 codestream_addr = codestream_next_addr;
d747e0af 78 codestream_next_addr += CODESTREAM_BUFSIZ;
bd5635a1 79 codestream_off = 0;
d747e0af 80 codestream_cnt = CODESTREAM_BUFSIZ;
34df79fc 81 read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
bd5635a1
RP
82
83 if (peek_flag)
84 return (codestream_peek());
85 else
86 return (codestream_get());
87}
88
89static void
90codestream_seek (place)
d747e0af 91 int place;
bd5635a1 92{
d747e0af
MT
93 codestream_next_addr = place / CODESTREAM_BUFSIZ;
94 codestream_next_addr *= CODESTREAM_BUFSIZ;
bd5635a1
RP
95 codestream_cnt = 0;
96 codestream_fill (1);
97 while (codestream_tell() != place)
98 codestream_get ();
99}
100
101static void
102codestream_read (buf, count)
103 unsigned char *buf;
d747e0af 104 int count;
bd5635a1
RP
105{
106 unsigned char *p;
107 int i;
108 p = buf;
109 for (i = 0; i < count; i++)
110 *p++ = codestream_get ();
111}
112
113/* next instruction is a jump, move to target */
d747e0af
MT
114
115static void
bd5635a1
RP
116i386_follow_jump ()
117{
118 int long_delta;
119 short short_delta;
120 char byte_delta;
121 int data16;
122 int pos;
123
124 pos = codestream_tell ();
125
126 data16 = 0;
127 if (codestream_peek () == 0x66)
128 {
129 codestream_get ();
130 data16 = 1;
131 }
132
133 switch (codestream_get ())
134 {
135 case 0xe9:
136 /* relative jump: if data16 == 0, disp32, else disp16 */
137 if (data16)
138 {
139 codestream_read ((unsigned char *)&short_delta, 2);
f2ebc25f
JK
140
141 /* include size of jmp inst (including the 0x66 prefix). */
142 pos += short_delta + 4;
bd5635a1
RP
143 }
144 else
145 {
146 codestream_read ((unsigned char *)&long_delta, 4);
147 pos += long_delta + 5;
148 }
149 break;
150 case 0xeb:
151 /* relative jump, disp8 (ignore data16) */
152 codestream_read ((unsigned char *)&byte_delta, 1);
153 pos += byte_delta + 2;
154 break;
155 }
f2ebc25f 156 codestream_seek (pos);
bd5635a1
RP
157}
158
159/*
160 * find & return amound a local space allocated, and advance codestream to
161 * first register push (if any)
162 *
163 * if entry sequence doesn't make sense, return -1, and leave
164 * codestream pointer random
165 */
d747e0af 166
bd5635a1
RP
167static long
168i386_get_frame_setup (pc)
d747e0af 169 int pc;
bd5635a1
RP
170{
171 unsigned char op;
172
173 codestream_seek (pc);
174
175 i386_follow_jump ();
176
177 op = codestream_get ();
178
179 if (op == 0x58) /* popl %eax */
180 {
181 /*
182 * this function must start with
183 *
184 * popl %eax 0x58
185 * xchgl %eax, (%esp) 0x87 0x04 0x24
186 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
187 *
188 * (the system 5 compiler puts out the second xchg
189 * inst, and the assembler doesn't try to optimize it,
190 * so the 'sib' form gets generated)
191 *
192 * this sequence is used to get the address of the return
193 * buffer for a function that returns a structure
194 */
195 int pos;
196 unsigned char buf[4];
197 static unsigned char proto1[3] = { 0x87,0x04,0x24 };
198 static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
199 pos = codestream_tell ();
200 codestream_read (buf, 4);
51b57ded 201 if (memcmp (buf, proto1, 3) == 0)
bd5635a1 202 pos += 3;
51b57ded 203 else if (memcmp (buf, proto2, 4) == 0)
bd5635a1
RP
204 pos += 4;
205
206 codestream_seek (pos);
207 op = codestream_get (); /* update next opcode */
208 }
209
210 if (op == 0x55) /* pushl %ebp */
211 {
212 /* check for movl %esp, %ebp - can be written two ways */
213 switch (codestream_get ())
214 {
215 case 0x8b:
216 if (codestream_get () != 0xec)
217 return (-1);
218 break;
219 case 0x89:
220 if (codestream_get () != 0xe5)
221 return (-1);
222 break;
223 default:
224 return (-1);
225 }
226 /* check for stack adjustment
227 *
228 * subl $XXX, %esp
229 *
230 * note: you can't subtract a 16 bit immediate
231 * from a 32 bit reg, so we don't have to worry
232 * about a data16 prefix
233 */
234 op = codestream_peek ();
235 if (op == 0x83)
236 {
237 /* subl with 8 bit immed */
238 codestream_get ();
239 if (codestream_get () != 0xec)
240 /* Some instruction starting with 0x83 other than subl. */
241 {
242 codestream_seek (codestream_tell () - 2);
243 return 0;
244 }
245 /* subl with signed byte immediate
246 * (though it wouldn't make sense to be negative)
247 */
248 return (codestream_get());
249 }
250 else if (op == 0x81)
251 {
34df79fc
JK
252 char buf[4];
253 /* Maybe it is subl with 32 bit immedediate. */
bd5635a1
RP
254 codestream_get();
255 if (codestream_get () != 0xec)
256 /* Some instruction starting with 0x81 other than subl. */
257 {
258 codestream_seek (codestream_tell () - 2);
259 return 0;
260 }
34df79fc
JK
261 /* It is subl with 32 bit immediate. */
262 codestream_read ((unsigned char *)buf, 4);
263 return extract_signed_integer (buf, 4);
bd5635a1
RP
264 }
265 else
266 {
267 return (0);
268 }
269 }
270 else if (op == 0xc8)
271 {
34df79fc 272 char buf[2];
bd5635a1 273 /* enter instruction: arg is 16 bit unsigned immed */
34df79fc 274 codestream_read ((unsigned char *)buf, 2);
bd5635a1 275 codestream_get (); /* flush final byte of enter instruction */
34df79fc 276 return extract_unsigned_integer (buf, 2);
bd5635a1
RP
277 }
278 return (-1);
279}
280
281/* Return number of args passed to a frame.
282 Can return -1, meaning no way to tell. */
283
bd5635a1
RP
284int
285i386_frame_num_args (fi)
d747e0af 286 struct frame_info *fi;
bd5635a1 287{
34df79fc
JK
288#if 1
289 return -1;
290#else
291 /* This loses because not only might the compiler not be popping the
292 args right after the function call, it might be popping args from both
293 this call and a previous one, and we would say there are more args
294 than there really are. */
295
bd5635a1
RP
296 int retpc;
297 unsigned char op;
298 struct frame_info *pfi;
299
34df79fc
JK
300 /* on the 386, the instruction following the call could be:
301 popl %ecx - one arg
302 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
303 anything else - zero args */
304
bd5635a1
RP
305 int frameless;
306
307 FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
308 if (frameless)
309 /* In the absence of a frame pointer, GDB doesn't get correct values
310 for nameless arguments. Return -1, so it doesn't print any
311 nameless arguments. */
312 return -1;
313
d747e0af 314 pfi = get_prev_frame_info (fi);
bd5635a1
RP
315 if (pfi == 0)
316 {
317 /* Note: this can happen if we are looking at the frame for
318 main, because FRAME_CHAIN_VALID won't let us go into
319 start. If we have debugging symbols, that's not really
320 a big deal; it just means it will only show as many arguments
321 to main as are declared. */
322 return -1;
323 }
324 else
325 {
326 retpc = pfi->pc;
327 op = read_memory_integer (retpc, 1);
328 if (op == 0x59)
329 /* pop %ecx */
330 return 1;
331 else if (op == 0x83)
332 {
333 op = read_memory_integer (retpc+1, 1);
334 if (op == 0xc4)
335 /* addl $<signed imm 8 bits>, %esp */
336 return (read_memory_integer (retpc+2,1)&0xff)/4;
337 else
338 return 0;
339 }
340 else if (op == 0x81)
341 { /* add with 32 bit immediate */
342 op = read_memory_integer (retpc+1, 1);
343 if (op == 0xc4)
344 /* addl $<imm 32>, %esp */
345 return read_memory_integer (retpc+2, 4) / 4;
346 else
347 return 0;
348 }
349 else
350 {
351 return 0;
352 }
353 }
34df79fc 354#endif
bd5635a1
RP
355}
356
357/*
358 * parse the first few instructions of the function to see
359 * what registers were stored.
360 *
361 * We handle these cases:
362 *
363 * The startup sequence can be at the start of the function,
364 * or the function can start with a branch to startup code at the end.
365 *
366 * %ebp can be set up with either the 'enter' instruction, or
367 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
368 * but was once used in the sys5 compiler)
369 *
370 * Local space is allocated just below the saved %ebp by either the
371 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
372 * a 16 bit unsigned argument for space to allocate, and the
373 * 'addl' instruction could have either a signed byte, or
374 * 32 bit immediate.
375 *
376 * Next, the registers used by this function are pushed. In
377 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
378 * (and sometimes a harmless bug causes it to also save but not restore %eax);
379 * however, the code below is willing to see the pushes in any order,
380 * and will handle up to 8 of them.
381 *
382 * If the setup sequence is at the end of the function, then the
383 * next instruction will be a branch back to the start.
384 */
385
d747e0af 386void
bd5635a1
RP
387i386_frame_find_saved_regs (fip, fsrp)
388 struct frame_info *fip;
389 struct frame_saved_regs *fsrp;
390{
391 long locals;
bd5635a1
RP
392 unsigned char op;
393 CORE_ADDR dummy_bottom;
394 CORE_ADDR adr;
395 int i;
396
34df79fc 397 memset (fsrp, 0, sizeof *fsrp);
bd5635a1
RP
398
399 /* if frame is the end of a dummy, compute where the
400 * beginning would be
401 */
402 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
403
404 /* check if the PC is in the stack, in a dummy frame */
405 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
406 {
407 /* all regs were saved by push_call_dummy () */
408 adr = fip->frame;
409 for (i = 0; i < NUM_REGS; i++)
410 {
411 adr -= REGISTER_RAW_SIZE (i);
412 fsrp->regs[i] = adr;
413 }
414 return;
415 }
416
417 locals = i386_get_frame_setup (get_pc_function_start (fip->pc));
418
419 if (locals >= 0)
420 {
421 adr = fip->frame - 4 - locals;
422 for (i = 0; i < 8; i++)
423 {
424 op = codestream_get ();
425 if (op < 0x50 || op > 0x57)
426 break;
427 fsrp->regs[op - 0x50] = adr;
428 adr -= 4;
429 }
430 }
431
432 fsrp->regs[PC_REGNUM] = fip->frame + 4;
433 fsrp->regs[FP_REGNUM] = fip->frame;
434}
435
436/* return pc of first real instruction */
d747e0af
MT
437
438int
bd5635a1 439i386_skip_prologue (pc)
d747e0af 440 int pc;
bd5635a1
RP
441{
442 unsigned char op;
443 int i;
444
445 if (i386_get_frame_setup (pc) < 0)
446 return (pc);
447
448 /* found valid frame setup - codestream now points to
449 * start of push instructions for saving registers
450 */
451
452 /* skip over register saves */
453 for (i = 0; i < 8; i++)
454 {
455 op = codestream_peek ();
456 /* break if not pushl inst */
457 if (op < 0x50 || op > 0x57)
458 break;
459 codestream_get ();
460 }
461
462 i386_follow_jump ();
463
464 return (codestream_tell ());
465}
466
d747e0af 467void
bd5635a1
RP
468i386_push_dummy_frame ()
469{
470 CORE_ADDR sp = read_register (SP_REGNUM);
471 int regnum;
472 char regbuf[MAX_REGISTER_RAW_SIZE];
473
474 sp = push_word (sp, read_register (PC_REGNUM));
475 sp = push_word (sp, read_register (FP_REGNUM));
476 write_register (FP_REGNUM, sp);
477 for (regnum = 0; regnum < NUM_REGS; regnum++)
478 {
479 read_register_gen (regnum, regbuf);
480 sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
481 }
482 write_register (SP_REGNUM, sp);
483}
484
d747e0af 485void
bd5635a1
RP
486i386_pop_frame ()
487{
488 FRAME frame = get_current_frame ();
489 CORE_ADDR fp;
490 int regnum;
491 struct frame_saved_regs fsr;
492 struct frame_info *fi;
493 char regbuf[MAX_REGISTER_RAW_SIZE];
494
495 fi = get_frame_info (frame);
496 fp = fi->frame;
497 get_frame_saved_regs (fi, &fsr);
498 for (regnum = 0; regnum < NUM_REGS; regnum++)
499 {
500 CORE_ADDR adr;
501 adr = fsr.regs[regnum];
502 if (adr)
503 {
504 read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
505 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
506 REGISTER_RAW_SIZE (regnum));
507 }
508 }
509 write_register (FP_REGNUM, read_memory_integer (fp, 4));
510 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
511 write_register (SP_REGNUM, fp + 8);
512 flush_cached_frames ();
513 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
514 read_pc ()));
515}
d747e0af 516
51b57ded
FF
517#ifdef GET_LONGJMP_TARGET
518
519/* Figure out where the longjmp will land. Slurp the args out of the stack.
520 We expect the first arg to be a pointer to the jmp_buf structure from which
521 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
522 This routine returns true on success. */
523
524int
525get_longjmp_target(pc)
526 CORE_ADDR *pc;
527{
34df79fc 528 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
51b57ded
FF
529 CORE_ADDR sp, jb_addr;
530
34df79fc 531 sp = read_register (SP_REGNUM);
51b57ded 532
34df79fc
JK
533 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
534 buf,
535 TARGET_PTR_BIT / TARGET_CHAR_BIT))
51b57ded
FF
536 return 0;
537
34df79fc 538 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
51b57ded 539
34df79fc
JK
540 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
541 TARGET_PTR_BIT / TARGET_CHAR_BIT))
51b57ded
FF
542 return 0;
543
34df79fc 544 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
51b57ded
FF
545
546 return 1;
547}
548
549#endif /* GET_LONGJMP_TARGET */
34df79fc
JK
550
551#ifdef I386_AIX_TARGET
552/* On AIX, floating point values are returned in floating point registers. */
553
554void
555i386_extract_return_value(type, regbuf, valbuf)
556 struct type *type;
557 char regbuf[REGISTER_BYTES];
558 char *valbuf;
559{
560 if (TYPE_CODE_FLT == TYPE_CODE(type))
561 {
562 extern struct ext_format ext_format_i387;
563 double d;
564 /* 387 %st(0), gcc uses this */
565 ieee_extended_to_double (&ext_format_i387,
566 &regbuf[REGISTER_BYTE(FP0_REGNUM)],
567 &d);
568 switch (TYPE_LENGTH(type))
569 {
570 case 4: /* float */
571 {
572 float f = (float) d;
573 memcpy (valbuf, &f, 4);
574 break;
575 }
576 case 8: /* double */
577 memcpy (valbuf, &d, 8);
578 break;
579 default:
580 error("Unknown floating point size");
581 break;
582 }
583 }
584 else
585 {
586 memcpy (valbuf, regbuf, TYPE_LENGTH (type));
587 }
588}
589#endif /* I386_AIX_TARGET */
This page took 0.143092 seconds and 4 git commands to generate.