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