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,
20 Boston, MA 02111-1307, USA. */
23 #include "gdb_string.h"
28 #include "floatformat.h"
33 static long i386_get_frame_setup
PARAMS ((CORE_ADDR
));
35 static void i386_follow_jump
PARAMS ((void));
37 static void codestream_read
PARAMS ((unsigned char *, int));
39 static void codestream_seek
PARAMS ((CORE_ADDR
));
41 static unsigned char codestream_fill
PARAMS ((int));
43 CORE_ADDR skip_trampoline_code
PARAMS ((CORE_ADDR
, char *));
45 static int gdb_print_insn_i386 (bfd_vma
, disassemble_info
*);
47 void _initialize_i386_tdep
PARAMS ((void));
49 /* This is the variable the is set with "set disassembly-flavor",
50 and its legitimate values. */
51 static char att_flavor
[] = "att";
52 static char intel_flavor
[] = "intel";
53 static char *valid_flavors
[] =
59 static char *disassembly_flavor
= att_flavor
;
61 /* This is used to keep the bfd arch_info in sync with the disassembly flavor. */
62 static void set_disassembly_flavor_sfunc
PARAMS ((char *, int, struct cmd_list_element
*));
63 static void set_disassembly_flavor ();
65 void (*disassembly_flavor_hook
) PARAMS ((char *args
, int from_tty
));
67 /* Stdio style buffering was used to minimize calls to ptrace, but this
68 buffering did not take into account that the code section being accessed
69 may not be an even number of buffers long (even if the buffer is only
70 sizeof(int) long). In cases where the code section size happened to
71 be a non-integral number of buffers long, attempting to read the last
72 buffer would fail. Simply using target_read_memory and ignoring errors,
73 rather than read_memory, is not the correct solution, since legitimate
74 access errors would then be totally ignored. To properly handle this
75 situation and continue to use buffering would require that this code
76 be able to determine the minimum code section size granularity (not the
77 alignment of the section itself, since the actual failing case that
78 pointed out this problem had a section alignment of 4 but was not a
79 multiple of 4 bytes long), on a target by target basis, and then
80 adjust it's buffer size accordingly. This is messy, but potentially
81 feasible. It probably needs the bfd library's help and support. For
82 now, the buffer size is set to 1. (FIXME -fnf) */
84 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
85 static CORE_ADDR codestream_next_addr
;
86 static CORE_ADDR codestream_addr
;
87 static unsigned char codestream_buf
[CODESTREAM_BUFSIZ
];
88 static int codestream_off
;
89 static int codestream_cnt
;
91 #define codestream_tell() (codestream_addr + codestream_off)
92 #define codestream_peek() (codestream_cnt == 0 ? \
93 codestream_fill(1): codestream_buf[codestream_off])
94 #define codestream_get() (codestream_cnt-- == 0 ? \
95 codestream_fill(0) : codestream_buf[codestream_off++])
98 codestream_fill (peek_flag
)
101 codestream_addr
= codestream_next_addr
;
102 codestream_next_addr
+= CODESTREAM_BUFSIZ
;
104 codestream_cnt
= CODESTREAM_BUFSIZ
;
105 read_memory (codestream_addr
, (char *) codestream_buf
, CODESTREAM_BUFSIZ
);
108 return (codestream_peek ());
110 return (codestream_get ());
114 codestream_seek (place
)
117 codestream_next_addr
= place
/ CODESTREAM_BUFSIZ
;
118 codestream_next_addr
*= CODESTREAM_BUFSIZ
;
121 while (codestream_tell () != place
)
126 codestream_read (buf
, count
)
133 for (i
= 0; i
< count
; i
++)
134 *p
++ = codestream_get ();
137 /* next instruction is a jump, move to target */
142 unsigned char buf
[4];
148 pos
= codestream_tell ();
151 if (codestream_peek () == 0x66)
157 switch (codestream_get ())
160 /* relative jump: if data16 == 0, disp32, else disp16 */
163 codestream_read (buf
, 2);
164 delta
= extract_signed_integer (buf
, 2);
166 /* include size of jmp inst (including the 0x66 prefix). */
171 codestream_read (buf
, 4);
172 delta
= extract_signed_integer (buf
, 4);
178 /* relative jump, disp8 (ignore data16) */
179 codestream_read (buf
, 1);
180 /* Sign-extend it. */
181 delta
= extract_signed_integer (buf
, 1);
186 codestream_seek (pos
);
190 * find & return amound a local space allocated, and advance codestream to
191 * first register push (if any)
193 * if entry sequence doesn't make sense, return -1, and leave
194 * codestream pointer random
198 i386_get_frame_setup (pc
)
203 codestream_seek (pc
);
207 op
= codestream_get ();
209 if (op
== 0x58) /* popl %eax */
212 * this function must start with
215 * xchgl %eax, (%esp) 0x87 0x04 0x24
216 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
218 * (the system 5 compiler puts out the second xchg
219 * inst, and the assembler doesn't try to optimize it,
220 * so the 'sib' form gets generated)
222 * this sequence is used to get the address of the return
223 * buffer for a function that returns a structure
226 unsigned char buf
[4];
227 static unsigned char proto1
[3] =
229 static unsigned char proto2
[4] =
230 {0x87, 0x44, 0x24, 0x00};
231 pos
= codestream_tell ();
232 codestream_read (buf
, 4);
233 if (memcmp (buf
, proto1
, 3) == 0)
235 else if (memcmp (buf
, proto2
, 4) == 0)
238 codestream_seek (pos
);
239 op
= codestream_get (); /* update next opcode */
242 if (op
== 0x68 || op
== 0x6a)
245 * this function may start with
255 unsigned char buf
[8];
257 /* Skip past the pushl instruction; it has either a one-byte
258 or a four-byte operand, depending on the opcode. */
259 pos
= codestream_tell ();
264 codestream_seek (pos
);
266 /* Read the following 8 bytes, which should be "call _probe" (6 bytes)
267 followed by "addl $4,%esp" (2 bytes). */
268 codestream_read (buf
, sizeof (buf
));
269 if (buf
[0] == 0xe8 && buf
[6] == 0xc4 && buf
[7] == 0x4)
271 codestream_seek (pos
);
272 op
= codestream_get (); /* update next opcode */
275 if (op
== 0x55) /* pushl %ebp */
277 /* check for movl %esp, %ebp - can be written two ways */
278 switch (codestream_get ())
281 if (codestream_get () != 0xec)
285 if (codestream_get () != 0xe5)
291 /* check for stack adjustment
295 * note: you can't subtract a 16 bit immediate
296 * from a 32 bit reg, so we don't have to worry
297 * about a data16 prefix
299 op
= codestream_peek ();
302 /* subl with 8 bit immed */
304 if (codestream_get () != 0xec)
305 /* Some instruction starting with 0x83 other than subl. */
307 codestream_seek (codestream_tell () - 2);
310 /* subl with signed byte immediate
311 * (though it wouldn't make sense to be negative)
313 return (codestream_get ());
318 /* Maybe it is subl with 32 bit immedediate. */
320 if (codestream_get () != 0xec)
321 /* Some instruction starting with 0x81 other than subl. */
323 codestream_seek (codestream_tell () - 2);
326 /* It is subl with 32 bit immediate. */
327 codestream_read ((unsigned char *) buf
, 4);
328 return extract_signed_integer (buf
, 4);
338 /* enter instruction: arg is 16 bit unsigned immed */
339 codestream_read ((unsigned char *) buf
, 2);
340 codestream_get (); /* flush final byte of enter instruction */
341 return extract_unsigned_integer (buf
, 2);
346 /* Return number of args passed to a frame.
347 Can return -1, meaning no way to tell. */
350 i386_frame_num_args (fi
)
351 struct frame_info
*fi
;
356 /* This loses because not only might the compiler not be popping the
357 args right after the function call, it might be popping args from both
358 this call and a previous one, and we would say there are more args
359 than there really are. */
363 struct frame_info
*pfi
;
365 /* on the 386, the instruction following the call could be:
367 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
368 anything else - zero args */
372 frameless
= FRAMELESS_FUNCTION_INVOCATION (fi
);
374 /* In the absence of a frame pointer, GDB doesn't get correct values
375 for nameless arguments. Return -1, so it doesn't print any
376 nameless arguments. */
379 pfi
= get_prev_frame (fi
);
382 /* Note: this can happen if we are looking at the frame for
383 main, because FRAME_CHAIN_VALID won't let us go into
384 start. If we have debugging symbols, that's not really
385 a big deal; it just means it will only show as many arguments
386 to main as are declared. */
392 op
= read_memory_integer (retpc
, 1);
398 op
= read_memory_integer (retpc
+ 1, 1);
400 /* addl $<signed imm 8 bits>, %esp */
401 return (read_memory_integer (retpc
+ 2, 1) & 0xff) / 4;
406 { /* add with 32 bit immediate */
407 op
= read_memory_integer (retpc
+ 1, 1);
409 /* addl $<imm 32>, %esp */
410 return read_memory_integer (retpc
+ 2, 4) / 4;
423 * parse the first few instructions of the function to see
424 * what registers were stored.
426 * We handle these cases:
428 * The startup sequence can be at the start of the function,
429 * or the function can start with a branch to startup code at the end.
431 * %ebp can be set up with either the 'enter' instruction, or
432 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
433 * but was once used in the sys5 compiler)
435 * Local space is allocated just below the saved %ebp by either the
436 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
437 * a 16 bit unsigned argument for space to allocate, and the
438 * 'addl' instruction could have either a signed byte, or
441 * Next, the registers used by this function are pushed. In
442 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
443 * (and sometimes a harmless bug causes it to also save but not restore %eax);
444 * however, the code below is willing to see the pushes in any order,
445 * and will handle up to 8 of them.
447 * If the setup sequence is at the end of the function, then the
448 * next instruction will be a branch back to the start.
452 i386_frame_find_saved_regs (fip
, fsrp
)
453 struct frame_info
*fip
;
454 struct frame_saved_regs
*fsrp
;
458 CORE_ADDR dummy_bottom
;
463 memset (fsrp
, 0, sizeof *fsrp
);
465 /* if frame is the end of a dummy, compute where the
468 dummy_bottom
= fip
->frame
- 4 - REGISTER_BYTES
- CALL_DUMMY_LENGTH
;
470 /* check if the PC is in the stack, in a dummy frame */
471 if (dummy_bottom
<= fip
->pc
&& fip
->pc
<= fip
->frame
)
473 /* all regs were saved by push_call_dummy () */
475 for (i
= 0; i
< NUM_REGS
; i
++)
477 adr
-= REGISTER_RAW_SIZE (i
);
483 pc
= get_pc_function_start (fip
->pc
);
485 locals
= i386_get_frame_setup (pc
);
489 adr
= fip
->frame
- 4 - locals
;
490 for (i
= 0; i
< 8; i
++)
492 op
= codestream_get ();
493 if (op
< 0x50 || op
> 0x57)
495 #ifdef I386_REGNO_TO_SYMMETRY
496 /* Dynix uses different internal numbering. Ick. */
497 fsrp
->regs
[I386_REGNO_TO_SYMMETRY (op
- 0x50)] = adr
;
499 fsrp
->regs
[op
- 0x50] = adr
;
505 fsrp
->regs
[PC_REGNUM
] = fip
->frame
+ 4;
506 fsrp
->regs
[FP_REGNUM
] = fip
->frame
;
509 /* return pc of first real instruction */
512 i386_skip_prologue (pc
)
517 static unsigned char pic_pat
[6] =
518 {0xe8, 0, 0, 0, 0, /* call 0x0 */
519 0x5b, /* popl %ebx */
523 if (i386_get_frame_setup (pc
) < 0)
526 /* found valid frame setup - codestream now points to
527 * start of push instructions for saving registers
530 /* skip over register saves */
531 for (i
= 0; i
< 8; i
++)
533 op
= codestream_peek ();
534 /* break if not pushl inst */
535 if (op
< 0x50 || op
> 0x57)
540 /* The native cc on SVR4 in -K PIC mode inserts the following code to get
541 the address of the global offset table (GOT) into register %ebx.
544 movl %ebx,x(%ebp) (optional)
546 This code is with the rest of the prologue (at the end of the
547 function), so we have to skip it to get to the first real
548 instruction at the start of the function. */
550 pos
= codestream_tell ();
551 for (i
= 0; i
< 6; i
++)
553 op
= codestream_get ();
554 if (pic_pat
[i
] != op
)
559 unsigned char buf
[4];
562 op
= codestream_get ();
563 if (op
== 0x89) /* movl %ebx, x(%ebp) */
565 op
= codestream_get ();
566 if (op
== 0x5d) /* one byte offset from %ebp */
569 codestream_read (buf
, 1);
571 else if (op
== 0x9d) /* four byte offset from %ebp */
574 codestream_read (buf
, 4);
576 else /* unexpected instruction */
578 op
= codestream_get ();
581 if (delta
> 0 && op
== 0x81 && codestream_get () == 0xc3)
586 codestream_seek (pos
);
590 return (codestream_tell ());
594 i386_push_dummy_frame ()
596 CORE_ADDR sp
= read_register (SP_REGNUM
);
598 char regbuf
[MAX_REGISTER_RAW_SIZE
];
600 sp
= push_word (sp
, read_register (PC_REGNUM
));
601 sp
= push_word (sp
, read_register (FP_REGNUM
));
602 write_register (FP_REGNUM
, sp
);
603 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
605 read_register_gen (regnum
, regbuf
);
606 sp
= push_bytes (sp
, regbuf
, REGISTER_RAW_SIZE (regnum
));
608 write_register (SP_REGNUM
, sp
);
614 struct frame_info
*frame
= get_current_frame ();
617 struct frame_saved_regs fsr
;
618 char regbuf
[MAX_REGISTER_RAW_SIZE
];
620 fp
= FRAME_FP (frame
);
621 get_frame_saved_regs (frame
, &fsr
);
622 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
625 adr
= fsr
.regs
[regnum
];
628 read_memory (adr
, regbuf
, REGISTER_RAW_SIZE (regnum
));
629 write_register_bytes (REGISTER_BYTE (regnum
), regbuf
,
630 REGISTER_RAW_SIZE (regnum
));
633 write_register (FP_REGNUM
, read_memory_integer (fp
, 4));
634 write_register (PC_REGNUM
, read_memory_integer (fp
+ 4, 4));
635 write_register (SP_REGNUM
, fp
+ 8);
636 flush_cached_frames ();
639 #ifdef GET_LONGJMP_TARGET
641 /* Figure out where the longjmp will land. Slurp the args out of the stack.
642 We expect the first arg to be a pointer to the jmp_buf structure from which
643 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
644 This routine returns true on success. */
647 get_longjmp_target (pc
)
650 char buf
[TARGET_PTR_BIT
/ TARGET_CHAR_BIT
];
651 CORE_ADDR sp
, jb_addr
;
653 sp
= read_register (SP_REGNUM
);
655 if (target_read_memory (sp
+ SP_ARG0
, /* Offset of first arg on stack */
657 TARGET_PTR_BIT
/ TARGET_CHAR_BIT
))
660 jb_addr
= extract_address (buf
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
662 if (target_read_memory (jb_addr
+ JB_PC
* JB_ELEMENT_SIZE
, buf
,
663 TARGET_PTR_BIT
/ TARGET_CHAR_BIT
))
666 *pc
= extract_address (buf
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
671 #endif /* GET_LONGJMP_TARGET */
674 i386_extract_return_value (type
, regbuf
, valbuf
)
676 char regbuf
[REGISTER_BYTES
];
679 /* On AIX, floating point values are returned in floating point registers. */
680 #ifdef I386_AIX_TARGET
681 if (TYPE_CODE_FLT
== TYPE_CODE (type
))
684 /* 387 %st(0), gcc uses this */
685 floatformat_to_double (&floatformat_i387_ext
,
686 ®buf
[REGISTER_BYTE (FP0_REGNUM
)],
688 store_floating (valbuf
, TYPE_LENGTH (type
), d
);
691 #endif /* I386_AIX_TARGET */
693 memcpy (valbuf
, regbuf
, TYPE_LENGTH (type
));
697 #ifdef I386V4_SIGTRAMP_SAVED_PC
698 /* Get saved user PC for sigtramp from the pushed ucontext on the stack
699 for all three variants of SVR4 sigtramps. */
702 i386v4_sigtramp_saved_pc (frame
)
703 struct frame_info
*frame
;
705 CORE_ADDR saved_pc_offset
= 4;
708 find_pc_partial_function (frame
->pc
, &name
, NULL
, NULL
);
711 if (STREQ (name
, "_sigreturn"))
712 saved_pc_offset
= 132 + 14 * 4;
713 else if (STREQ (name
, "_sigacthandler"))
714 saved_pc_offset
= 80 + 14 * 4;
715 else if (STREQ (name
, "sigvechandler"))
716 saved_pc_offset
= 120 + 14 * 4;
720 return read_memory_integer (frame
->next
->frame
+ saved_pc_offset
, 4);
721 return read_memory_integer (read_register (SP_REGNUM
) + saved_pc_offset
, 4);
723 #endif /* I386V4_SIGTRAMP_SAVED_PC */
725 #ifdef STATIC_TRANSFORM_NAME
726 /* SunPRO encodes the static variables. This is not related to C++ mangling,
727 it is done for C too. */
730 sunpro_static_transform_name (name
)
734 if (IS_STATIC_TRANSFORM_NAME (name
))
736 /* For file-local statics there will be a period, a bunch
737 of junk (the contents of which match a string given in the
738 N_OPT), a period and the name. For function-local statics
739 there will be a bunch of junk (which seems to change the
740 second character from 'A' to 'B'), a period, the name of the
741 function, and the name. So just skip everything before the
743 p
= strrchr (name
, '.');
749 #endif /* STATIC_TRANSFORM_NAME */
753 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
756 skip_trampoline_code (pc
, name
)
760 if (pc
&& read_memory_unsigned_integer (pc
, 2) == 0x25ff) /* jmp *(dest) */
762 unsigned long indirect
= read_memory_unsigned_integer (pc
+ 2, 4);
763 struct minimal_symbol
*indsym
=
764 indirect
? lookup_minimal_symbol_by_pc (indirect
) : 0;
765 char *symname
= indsym
? SYMBOL_NAME (indsym
) : 0;
769 if (strncmp (symname
, "__imp_", 6) == 0
770 || strncmp (symname
, "_imp_", 5) == 0)
771 return name
? 1 : read_memory_unsigned_integer (indirect
, 4);
774 return 0; /* not a trampoline */
778 gdb_print_insn_i386 (memaddr
, info
)
780 disassemble_info
*info
;
782 if (disassembly_flavor
== att_flavor
)
783 return print_insn_i386_att (memaddr
, info
);
784 else if (disassembly_flavor
== intel_flavor
)
785 return print_insn_i386_intel (memaddr
, info
);
786 /* Never reached - disassembly_flavour is always either att_flavor
791 /* If the disassembly mode is intel, we have to also switch the
792 bfd mach_type. This function is run in the set disassembly_flavor
793 command, and does that. */
796 set_disassembly_flavor_sfunc (args
, from_tty
, c
)
799 struct cmd_list_element
*c
;
801 set_disassembly_flavor ();
803 if (disassembly_flavor_hook
!= NULL
)
804 disassembly_flavor_hook (args
, from_tty
);
808 set_disassembly_flavor ()
810 if (disassembly_flavor
== att_flavor
)
811 set_architecture_from_arch_mach (bfd_arch_i386
, bfd_mach_i386_i386
);
812 else if (disassembly_flavor
== intel_flavor
)
813 set_architecture_from_arch_mach (bfd_arch_i386
, bfd_mach_i386_i386_intel_syntax
);
817 _initialize_i386_tdep ()
819 struct cmd_list_element
*new_cmd
;
821 tm_print_insn
= gdb_print_insn_i386
;
822 tm_print_insn_info
.mach
= bfd_lookup_arch (bfd_arch_i386
, 0)->mach
;
824 /* Add the variable that controls the disassembly flavor */
826 new_cmd
= add_set_enum_cmd ("disassembly-flavor", no_class
,
828 (char *) &disassembly_flavor
,
829 "Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
830 and the default value is \"att\".",
832 new_cmd
->function
.sfunc
= set_disassembly_flavor_sfunc
;
833 add_show_from_set (new_cmd
, &showlist
);
835 /* Finally, initialize the disassembly flavor to the default given
836 in the disassembly_flavor variable */
838 set_disassembly_flavor ();