1 /* Intel 386 target-dependent stuff.
2 Copyright (C) 1988, 1989, 1991, 1994, 1995, 1996, 1998
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "gdb_string.h"
27 #include "floatformat.h"
32 static long i386_get_frame_setup
PARAMS ((CORE_ADDR
));
34 static void i386_follow_jump
PARAMS ((void));
36 static void codestream_read
PARAMS ((unsigned char *, int));
38 static void codestream_seek
PARAMS ((CORE_ADDR
));
40 static unsigned char codestream_fill
PARAMS ((int));
42 CORE_ADDR skip_trampoline_code
PARAMS ((CORE_ADDR
, char *));
44 static int gdb_print_insn_i386 (bfd_vma
, disassemble_info
*);
46 void _initialize_i386_tdep
PARAMS ((void));
48 /* This is the variable the is set with "set disassembly-flavor",
49 and its legitimate values. */
50 static char att_flavor
[] = "att";
51 static char intel_flavor
[] = "intel";
52 static char *valid_flavors
[] = {
57 static char *disassembly_flavor
= att_flavor
;
59 /* Stdio style buffering was used to minimize calls to ptrace, but this
60 buffering did not take into account that the code section being accessed
61 may not be an even number of buffers long (even if the buffer is only
62 sizeof(int) long). In cases where the code section size happened to
63 be a non-integral number of buffers long, attempting to read the last
64 buffer would fail. Simply using target_read_memory and ignoring errors,
65 rather than read_memory, is not the correct solution, since legitimate
66 access errors would then be totally ignored. To properly handle this
67 situation and continue to use buffering would require that this code
68 be able to determine the minimum code section size granularity (not the
69 alignment of the section itself, since the actual failing case that
70 pointed out this problem had a section alignment of 4 but was not a
71 multiple of 4 bytes long), on a target by target basis, and then
72 adjust it's buffer size accordingly. This is messy, but potentially
73 feasible. It probably needs the bfd library's help and support. For
74 now, the buffer size is set to 1. (FIXME -fnf) */
76 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
77 static CORE_ADDR codestream_next_addr
;
78 static CORE_ADDR codestream_addr
;
79 static unsigned char codestream_buf
[CODESTREAM_BUFSIZ
];
80 static int codestream_off
;
81 static int codestream_cnt
;
83 #define codestream_tell() (codestream_addr + codestream_off)
84 #define codestream_peek() (codestream_cnt == 0 ? \
85 codestream_fill(1): codestream_buf[codestream_off])
86 #define codestream_get() (codestream_cnt-- == 0 ? \
87 codestream_fill(0) : codestream_buf[codestream_off++])
90 codestream_fill (peek_flag
)
93 codestream_addr
= codestream_next_addr
;
94 codestream_next_addr
+= CODESTREAM_BUFSIZ
;
96 codestream_cnt
= CODESTREAM_BUFSIZ
;
97 read_memory (codestream_addr
, (char *) codestream_buf
, CODESTREAM_BUFSIZ
);
100 return (codestream_peek());
102 return (codestream_get());
106 codestream_seek (place
)
109 codestream_next_addr
= place
/ CODESTREAM_BUFSIZ
;
110 codestream_next_addr
*= CODESTREAM_BUFSIZ
;
113 while (codestream_tell() != place
)
118 codestream_read (buf
, count
)
125 for (i
= 0; i
< count
; i
++)
126 *p
++ = codestream_get ();
129 /* next instruction is a jump, move to target */
134 unsigned char buf
[4];
140 pos
= codestream_tell ();
143 if (codestream_peek () == 0x66)
149 switch (codestream_get ())
152 /* relative jump: if data16 == 0, disp32, else disp16 */
155 codestream_read (buf
, 2);
156 delta
= extract_signed_integer (buf
, 2);
158 /* include size of jmp inst (including the 0x66 prefix). */
163 codestream_read (buf
, 4);
164 delta
= extract_signed_integer (buf
, 4);
170 /* relative jump, disp8 (ignore data16) */
171 codestream_read (buf
, 1);
172 /* Sign-extend it. */
173 delta
= extract_signed_integer (buf
, 1);
178 codestream_seek (pos
);
182 * find & return amound a local space allocated, and advance codestream to
183 * first register push (if any)
185 * if entry sequence doesn't make sense, return -1, and leave
186 * codestream pointer random
190 i386_get_frame_setup (pc
)
195 codestream_seek (pc
);
199 op
= codestream_get ();
201 if (op
== 0x58) /* popl %eax */
204 * this function must start with
207 * xchgl %eax, (%esp) 0x87 0x04 0x24
208 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
210 * (the system 5 compiler puts out the second xchg
211 * inst, and the assembler doesn't try to optimize it,
212 * so the 'sib' form gets generated)
214 * this sequence is used to get the address of the return
215 * buffer for a function that returns a structure
218 unsigned char buf
[4];
219 static unsigned char proto1
[3] = { 0x87,0x04,0x24 };
220 static unsigned char proto2
[4] = { 0x87,0x44,0x24,0x00 };
221 pos
= codestream_tell ();
222 codestream_read (buf
, 4);
223 if (memcmp (buf
, proto1
, 3) == 0)
225 else if (memcmp (buf
, proto2
, 4) == 0)
228 codestream_seek (pos
);
229 op
= codestream_get (); /* update next opcode */
232 if (op
== 0x68 || op
== 0x6a)
235 * this function may start with
245 unsigned char buf
[8];
247 /* Skip past the pushl instruction; it has either a one-byte
248 or a four-byte operand, depending on the opcode. */
249 pos
= codestream_tell ();
254 codestream_seek (pos
);
256 /* Read the following 8 bytes, which should be "call _probe" (6 bytes)
257 followed by "addl $4,%esp" (2 bytes). */
258 codestream_read (buf
, sizeof (buf
));
259 if (buf
[0] == 0xe8 && buf
[6] == 0xc4 && buf
[7] == 0x4)
261 codestream_seek (pos
);
262 op
= codestream_get (); /* update next opcode */
265 if (op
== 0x55) /* pushl %ebp */
267 /* check for movl %esp, %ebp - can be written two ways */
268 switch (codestream_get ())
271 if (codestream_get () != 0xec)
275 if (codestream_get () != 0xe5)
281 /* check for stack adjustment
285 * note: you can't subtract a 16 bit immediate
286 * from a 32 bit reg, so we don't have to worry
287 * about a data16 prefix
289 op
= codestream_peek ();
292 /* subl with 8 bit immed */
294 if (codestream_get () != 0xec)
295 /* Some instruction starting with 0x83 other than subl. */
297 codestream_seek (codestream_tell () - 2);
300 /* subl with signed byte immediate
301 * (though it wouldn't make sense to be negative)
303 return (codestream_get());
308 /* Maybe it is subl with 32 bit immedediate. */
310 if (codestream_get () != 0xec)
311 /* Some instruction starting with 0x81 other than subl. */
313 codestream_seek (codestream_tell () - 2);
316 /* It is subl with 32 bit immediate. */
317 codestream_read ((unsigned char *)buf
, 4);
318 return extract_signed_integer (buf
, 4);
328 /* enter instruction: arg is 16 bit unsigned immed */
329 codestream_read ((unsigned char *)buf
, 2);
330 codestream_get (); /* flush final byte of enter instruction */
331 return extract_unsigned_integer (buf
, 2);
336 /* Return number of args passed to a frame.
337 Can return -1, meaning no way to tell. */
340 i386_frame_num_args (fi
)
341 struct frame_info
*fi
;
346 /* This loses because not only might the compiler not be popping the
347 args right after the function call, it might be popping args from both
348 this call and a previous one, and we would say there are more args
349 than there really are. */
353 struct frame_info
*pfi
;
355 /* on the 386, the instruction following the call could be:
357 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
358 anything else - zero args */
362 FRAMELESS_FUNCTION_INVOCATION (fi
, frameless
);
364 /* In the absence of a frame pointer, GDB doesn't get correct values
365 for nameless arguments. Return -1, so it doesn't print any
366 nameless arguments. */
369 pfi
= get_prev_frame_info (fi
);
372 /* Note: this can happen if we are looking at the frame for
373 main, because FRAME_CHAIN_VALID won't let us go into
374 start. If we have debugging symbols, that's not really
375 a big deal; it just means it will only show as many arguments
376 to main as are declared. */
382 op
= read_memory_integer (retpc
, 1);
388 op
= read_memory_integer (retpc
+1, 1);
390 /* addl $<signed imm 8 bits>, %esp */
391 return (read_memory_integer (retpc
+2,1)&0xff)/4;
396 { /* add with 32 bit immediate */
397 op
= read_memory_integer (retpc
+1, 1);
399 /* addl $<imm 32>, %esp */
400 return read_memory_integer (retpc
+2, 4) / 4;
413 * parse the first few instructions of the function to see
414 * what registers were stored.
416 * We handle these cases:
418 * The startup sequence can be at the start of the function,
419 * or the function can start with a branch to startup code at the end.
421 * %ebp can be set up with either the 'enter' instruction, or
422 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
423 * but was once used in the sys5 compiler)
425 * Local space is allocated just below the saved %ebp by either the
426 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
427 * a 16 bit unsigned argument for space to allocate, and the
428 * 'addl' instruction could have either a signed byte, or
431 * Next, the registers used by this function are pushed. In
432 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
433 * (and sometimes a harmless bug causes it to also save but not restore %eax);
434 * however, the code below is willing to see the pushes in any order,
435 * and will handle up to 8 of them.
437 * If the setup sequence is at the end of the function, then the
438 * next instruction will be a branch back to the start.
442 i386_frame_find_saved_regs (fip
, fsrp
)
443 struct frame_info
*fip
;
444 struct frame_saved_regs
*fsrp
;
448 CORE_ADDR dummy_bottom
;
453 memset (fsrp
, 0, sizeof *fsrp
);
455 /* if frame is the end of a dummy, compute where the
458 dummy_bottom
= fip
->frame
- 4 - REGISTER_BYTES
- CALL_DUMMY_LENGTH
;
460 /* check if the PC is in the stack, in a dummy frame */
461 if (dummy_bottom
<= fip
->pc
&& fip
->pc
<= fip
->frame
)
463 /* all regs were saved by push_call_dummy () */
465 for (i
= 0; i
< NUM_REGS
; i
++)
467 adr
-= REGISTER_RAW_SIZE (i
);
473 pc
= get_pc_function_start (fip
->pc
);
475 locals
= i386_get_frame_setup (pc
);
479 adr
= fip
->frame
- 4 - locals
;
480 for (i
= 0; i
< 8; i
++)
482 op
= codestream_get ();
483 if (op
< 0x50 || op
> 0x57)
485 #ifdef I386_REGNO_TO_SYMMETRY
486 /* Dynix uses different internal numbering. Ick. */
487 fsrp
->regs
[I386_REGNO_TO_SYMMETRY(op
- 0x50)] = adr
;
489 fsrp
->regs
[op
- 0x50] = adr
;
495 fsrp
->regs
[PC_REGNUM
] = fip
->frame
+ 4;
496 fsrp
->regs
[FP_REGNUM
] = fip
->frame
;
499 /* return pc of first real instruction */
502 i386_skip_prologue (pc
)
507 static unsigned char pic_pat
[6] = { 0xe8, 0, 0, 0, 0, /* call 0x0 */
508 0x5b, /* popl %ebx */
512 if (i386_get_frame_setup (pc
) < 0)
515 /* found valid frame setup - codestream now points to
516 * start of push instructions for saving registers
519 /* skip over register saves */
520 for (i
= 0; i
< 8; i
++)
522 op
= codestream_peek ();
523 /* break if not pushl inst */
524 if (op
< 0x50 || op
> 0x57)
529 /* The native cc on SVR4 in -K PIC mode inserts the following code to get
530 the address of the global offset table (GOT) into register %ebx.
533 movl %ebx,x(%ebp) (optional)
535 This code is with the rest of the prologue (at the end of the
536 function), so we have to skip it to get to the first real
537 instruction at the start of the function. */
539 pos
= codestream_tell ();
540 for (i
= 0; i
< 6; i
++)
542 op
= codestream_get ();
543 if (pic_pat
[i
] != op
)
548 unsigned char buf
[4];
551 op
= codestream_get ();
552 if (op
== 0x89) /* movl %ebx, x(%ebp) */
554 op
= codestream_get ();
555 if (op
== 0x5d) /* one byte offset from %ebp */
558 codestream_read (buf
, 1);
560 else if (op
== 0x9d) /* four byte offset from %ebp */
563 codestream_read (buf
, 4);
565 else /* unexpected instruction */
567 op
= codestream_get ();
570 if (delta
> 0 && op
== 0x81 && codestream_get () == 0xc3)
575 codestream_seek (pos
);
579 return (codestream_tell ());
583 i386_push_dummy_frame ()
585 CORE_ADDR sp
= read_register (SP_REGNUM
);
587 char regbuf
[MAX_REGISTER_RAW_SIZE
];
589 sp
= push_word (sp
, read_register (PC_REGNUM
));
590 sp
= push_word (sp
, read_register (FP_REGNUM
));
591 write_register (FP_REGNUM
, sp
);
592 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
594 read_register_gen (regnum
, regbuf
);
595 sp
= push_bytes (sp
, regbuf
, REGISTER_RAW_SIZE (regnum
));
597 write_register (SP_REGNUM
, sp
);
603 struct frame_info
*frame
= get_current_frame ();
606 struct frame_saved_regs fsr
;
607 char regbuf
[MAX_REGISTER_RAW_SIZE
];
609 fp
= FRAME_FP (frame
);
610 get_frame_saved_regs (frame
, &fsr
);
611 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
614 adr
= fsr
.regs
[regnum
];
617 read_memory (adr
, regbuf
, REGISTER_RAW_SIZE (regnum
));
618 write_register_bytes (REGISTER_BYTE (regnum
), regbuf
,
619 REGISTER_RAW_SIZE (regnum
));
622 write_register (FP_REGNUM
, read_memory_integer (fp
, 4));
623 write_register (PC_REGNUM
, read_memory_integer (fp
+ 4, 4));
624 write_register (SP_REGNUM
, fp
+ 8);
625 flush_cached_frames ();
628 #ifdef GET_LONGJMP_TARGET
630 /* Figure out where the longjmp will land. Slurp the args out of the stack.
631 We expect the first arg to be a pointer to the jmp_buf structure from which
632 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
633 This routine returns true on success. */
636 get_longjmp_target(pc
)
639 char buf
[TARGET_PTR_BIT
/ TARGET_CHAR_BIT
];
640 CORE_ADDR sp
, jb_addr
;
642 sp
= read_register (SP_REGNUM
);
644 if (target_read_memory (sp
+ SP_ARG0
, /* Offset of first arg on stack */
646 TARGET_PTR_BIT
/ TARGET_CHAR_BIT
))
649 jb_addr
= extract_address (buf
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
651 if (target_read_memory (jb_addr
+ JB_PC
* JB_ELEMENT_SIZE
, buf
,
652 TARGET_PTR_BIT
/ TARGET_CHAR_BIT
))
655 *pc
= extract_address (buf
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
660 #endif /* GET_LONGJMP_TARGET */
663 i386_extract_return_value(type
, regbuf
, valbuf
)
665 char regbuf
[REGISTER_BYTES
];
668 /* On AIX, floating point values are returned in floating point registers. */
669 #ifdef I386_AIX_TARGET
670 if (TYPE_CODE_FLT
== TYPE_CODE(type
))
673 /* 387 %st(0), gcc uses this */
674 floatformat_to_double (&floatformat_i387_ext
,
675 ®buf
[REGISTER_BYTE(FP0_REGNUM
)],
677 store_floating (valbuf
, TYPE_LENGTH (type
), d
);
680 #endif /* I386_AIX_TARGET */
682 memcpy (valbuf
, regbuf
, TYPE_LENGTH (type
));
686 #ifdef I386V4_SIGTRAMP_SAVED_PC
687 /* Get saved user PC for sigtramp from the pushed ucontext on the stack
688 for all three variants of SVR4 sigtramps. */
691 i386v4_sigtramp_saved_pc (frame
)
692 struct frame_info
*frame
;
694 CORE_ADDR saved_pc_offset
= 4;
697 find_pc_partial_function (frame
->pc
, &name
, NULL
, NULL
);
700 if (STREQ (name
, "_sigreturn"))
701 saved_pc_offset
= 132 + 14 * 4;
702 else if (STREQ (name
, "_sigacthandler"))
703 saved_pc_offset
= 80 + 14 * 4;
704 else if (STREQ (name
, "sigvechandler"))
705 saved_pc_offset
= 120 + 14 * 4;
709 return read_memory_integer (frame
->next
->frame
+ saved_pc_offset
, 4);
710 return read_memory_integer (read_register (SP_REGNUM
) + saved_pc_offset
, 4);
712 #endif /* I386V4_SIGTRAMP_SAVED_PC */
714 #ifdef STATIC_TRANSFORM_NAME
715 /* SunPRO encodes the static variables. This is not related to C++ mangling,
716 it is done for C too. */
719 sunpro_static_transform_name (name
)
723 if (IS_STATIC_TRANSFORM_NAME (name
))
725 /* For file-local statics there will be a period, a bunch
726 of junk (the contents of which match a string given in the
727 N_OPT), a period and the name. For function-local statics
728 there will be a bunch of junk (which seems to change the
729 second character from 'A' to 'B'), a period, the name of the
730 function, and the name. So just skip everything before the
732 p
= strrchr (name
, '.');
738 #endif /* STATIC_TRANSFORM_NAME */
742 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
745 skip_trampoline_code (pc
, name
)
749 if (pc
&& read_memory_unsigned_integer (pc
, 2) == 0x25ff) /* jmp *(dest) */
751 unsigned long indirect
= read_memory_unsigned_integer (pc
+2, 4);
752 struct minimal_symbol
*indsym
=
753 indirect
? lookup_minimal_symbol_by_pc (indirect
) : 0;
754 char *symname
= indsym
? SYMBOL_NAME(indsym
) : 0;
758 if (strncmp (symname
,"__imp_", 6) == 0
759 || strncmp (symname
,"_imp_", 5) == 0)
760 return name
? 1 : read_memory_unsigned_integer (indirect
, 4);
763 return 0; /* not a trampoline */
767 gdb_print_insn_i386 (memaddr
, info
)
769 disassemble_info
* info
;
771 if (disassembly_flavor
== att_flavor
)
772 return print_insn_i386_att (memaddr
, info
);
773 else if (disassembly_flavor
== intel_flavor
)
774 return print_insn_i386_intel (memaddr
, info
);
778 _initialize_i386_tdep ()
780 tm_print_insn
= gdb_print_insn_i386
;
781 tm_print_insn_info
.mach
= bfd_lookup_arch (bfd_arch_i386
, 0)->mach
;
783 /* Add the variable that controls the disassembly flavor */
785 add_set_enum_cmd ("disassembly-flavor", no_class
,
787 (char *) &disassembly_flavor
,
788 "Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
789 and the default value is \"att\".",