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 /* Get rid of these defines as soon as there are two functions
60 to implement different disassembly flavors. */
61 #define print_insn_i386_att print_insn_i386
62 #define print_insn_i386_intel print_insn_i386
64 /* Stdio style buffering was used to minimize calls to ptrace, but this
65 buffering did not take into account that the code section being accessed
66 may not be an even number of buffers long (even if the buffer is only
67 sizeof(int) long). In cases where the code section size happened to
68 be a non-integral number of buffers long, attempting to read the last
69 buffer would fail. Simply using target_read_memory and ignoring errors,
70 rather than read_memory, is not the correct solution, since legitimate
71 access errors would then be totally ignored. To properly handle this
72 situation and continue to use buffering would require that this code
73 be able to determine the minimum code section size granularity (not the
74 alignment of the section itself, since the actual failing case that
75 pointed out this problem had a section alignment of 4 but was not a
76 multiple of 4 bytes long), on a target by target basis, and then
77 adjust it's buffer size accordingly. This is messy, but potentially
78 feasible. It probably needs the bfd library's help and support. For
79 now, the buffer size is set to 1. (FIXME -fnf) */
81 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
82 static CORE_ADDR codestream_next_addr
;
83 static CORE_ADDR codestream_addr
;
84 static unsigned char codestream_buf
[CODESTREAM_BUFSIZ
];
85 static int codestream_off
;
86 static int codestream_cnt
;
88 #define codestream_tell() (codestream_addr + codestream_off)
89 #define codestream_peek() (codestream_cnt == 0 ? \
90 codestream_fill(1): codestream_buf[codestream_off])
91 #define codestream_get() (codestream_cnt-- == 0 ? \
92 codestream_fill(0) : codestream_buf[codestream_off++])
95 codestream_fill (peek_flag
)
98 codestream_addr
= codestream_next_addr
;
99 codestream_next_addr
+= CODESTREAM_BUFSIZ
;
101 codestream_cnt
= CODESTREAM_BUFSIZ
;
102 read_memory (codestream_addr
, (char *) codestream_buf
, CODESTREAM_BUFSIZ
);
105 return (codestream_peek());
107 return (codestream_get());
111 codestream_seek (place
)
114 codestream_next_addr
= place
/ CODESTREAM_BUFSIZ
;
115 codestream_next_addr
*= CODESTREAM_BUFSIZ
;
118 while (codestream_tell() != place
)
123 codestream_read (buf
, count
)
130 for (i
= 0; i
< count
; i
++)
131 *p
++ = codestream_get ();
134 /* next instruction is a jump, move to target */
139 unsigned char buf
[4];
145 pos
= codestream_tell ();
148 if (codestream_peek () == 0x66)
154 switch (codestream_get ())
157 /* relative jump: if data16 == 0, disp32, else disp16 */
160 codestream_read (buf
, 2);
161 delta
= extract_signed_integer (buf
, 2);
163 /* include size of jmp inst (including the 0x66 prefix). */
168 codestream_read (buf
, 4);
169 delta
= extract_signed_integer (buf
, 4);
175 /* relative jump, disp8 (ignore data16) */
176 codestream_read (buf
, 1);
177 /* Sign-extend it. */
178 delta
= extract_signed_integer (buf
, 1);
183 codestream_seek (pos
);
187 * find & return amound a local space allocated, and advance codestream to
188 * first register push (if any)
190 * if entry sequence doesn't make sense, return -1, and leave
191 * codestream pointer random
195 i386_get_frame_setup (pc
)
200 codestream_seek (pc
);
204 op
= codestream_get ();
206 if (op
== 0x58) /* popl %eax */
209 * this function must start with
212 * xchgl %eax, (%esp) 0x87 0x04 0x24
213 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
215 * (the system 5 compiler puts out the second xchg
216 * inst, and the assembler doesn't try to optimize it,
217 * so the 'sib' form gets generated)
219 * this sequence is used to get the address of the return
220 * buffer for a function that returns a structure
223 unsigned char buf
[4];
224 static unsigned char proto1
[3] = { 0x87,0x04,0x24 };
225 static unsigned char proto2
[4] = { 0x87,0x44,0x24,0x00 };
226 pos
= codestream_tell ();
227 codestream_read (buf
, 4);
228 if (memcmp (buf
, proto1
, 3) == 0)
230 else if (memcmp (buf
, proto2
, 4) == 0)
233 codestream_seek (pos
);
234 op
= codestream_get (); /* update next opcode */
237 if (op
== 0x68 || op
== 0x6a)
240 * this function may start with
250 unsigned char buf
[8];
252 /* Skip past the pushl instruction; it has either a one-byte
253 or a four-byte operand, depending on the opcode. */
254 pos
= codestream_tell ();
259 codestream_seek (pos
);
261 /* Read the following 8 bytes, which should be "call _probe" (6 bytes)
262 followed by "addl $4,%esp" (2 bytes). */
263 codestream_read (buf
, sizeof (buf
));
264 if (buf
[0] == 0xe8 && buf
[6] == 0xc4 && buf
[7] == 0x4)
266 codestream_seek (pos
);
267 op
= codestream_get (); /* update next opcode */
270 if (op
== 0x55) /* pushl %ebp */
272 /* check for movl %esp, %ebp - can be written two ways */
273 switch (codestream_get ())
276 if (codestream_get () != 0xec)
280 if (codestream_get () != 0xe5)
286 /* check for stack adjustment
290 * note: you can't subtract a 16 bit immediate
291 * from a 32 bit reg, so we don't have to worry
292 * about a data16 prefix
294 op
= codestream_peek ();
297 /* subl with 8 bit immed */
299 if (codestream_get () != 0xec)
300 /* Some instruction starting with 0x83 other than subl. */
302 codestream_seek (codestream_tell () - 2);
305 /* subl with signed byte immediate
306 * (though it wouldn't make sense to be negative)
308 return (codestream_get());
313 /* Maybe it is subl with 32 bit immedediate. */
315 if (codestream_get () != 0xec)
316 /* Some instruction starting with 0x81 other than subl. */
318 codestream_seek (codestream_tell () - 2);
321 /* It is subl with 32 bit immediate. */
322 codestream_read ((unsigned char *)buf
, 4);
323 return extract_signed_integer (buf
, 4);
333 /* enter instruction: arg is 16 bit unsigned immed */
334 codestream_read ((unsigned char *)buf
, 2);
335 codestream_get (); /* flush final byte of enter instruction */
336 return extract_unsigned_integer (buf
, 2);
341 /* Return number of args passed to a frame.
342 Can return -1, meaning no way to tell. */
345 i386_frame_num_args (fi
)
346 struct frame_info
*fi
;
351 /* This loses because not only might the compiler not be popping the
352 args right after the function call, it might be popping args from both
353 this call and a previous one, and we would say there are more args
354 than there really are. */
358 struct frame_info
*pfi
;
360 /* on the 386, the instruction following the call could be:
362 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
363 anything else - zero args */
367 FRAMELESS_FUNCTION_INVOCATION (fi
, frameless
);
369 /* In the absence of a frame pointer, GDB doesn't get correct values
370 for nameless arguments. Return -1, so it doesn't print any
371 nameless arguments. */
374 pfi
= get_prev_frame_info (fi
);
377 /* Note: this can happen if we are looking at the frame for
378 main, because FRAME_CHAIN_VALID won't let us go into
379 start. If we have debugging symbols, that's not really
380 a big deal; it just means it will only show as many arguments
381 to main as are declared. */
387 op
= read_memory_integer (retpc
, 1);
393 op
= read_memory_integer (retpc
+1, 1);
395 /* addl $<signed imm 8 bits>, %esp */
396 return (read_memory_integer (retpc
+2,1)&0xff)/4;
401 { /* add with 32 bit immediate */
402 op
= read_memory_integer (retpc
+1, 1);
404 /* addl $<imm 32>, %esp */
405 return read_memory_integer (retpc
+2, 4) / 4;
418 * parse the first few instructions of the function to see
419 * what registers were stored.
421 * We handle these cases:
423 * The startup sequence can be at the start of the function,
424 * or the function can start with a branch to startup code at the end.
426 * %ebp can be set up with either the 'enter' instruction, or
427 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
428 * but was once used in the sys5 compiler)
430 * Local space is allocated just below the saved %ebp by either the
431 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
432 * a 16 bit unsigned argument for space to allocate, and the
433 * 'addl' instruction could have either a signed byte, or
436 * Next, the registers used by this function are pushed. In
437 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
438 * (and sometimes a harmless bug causes it to also save but not restore %eax);
439 * however, the code below is willing to see the pushes in any order,
440 * and will handle up to 8 of them.
442 * If the setup sequence is at the end of the function, then the
443 * next instruction will be a branch back to the start.
447 i386_frame_find_saved_regs (fip
, fsrp
)
448 struct frame_info
*fip
;
449 struct frame_saved_regs
*fsrp
;
453 CORE_ADDR dummy_bottom
;
458 memset (fsrp
, 0, sizeof *fsrp
);
460 /* if frame is the end of a dummy, compute where the
463 dummy_bottom
= fip
->frame
- 4 - REGISTER_BYTES
- CALL_DUMMY_LENGTH
;
465 /* check if the PC is in the stack, in a dummy frame */
466 if (dummy_bottom
<= fip
->pc
&& fip
->pc
<= fip
->frame
)
468 /* all regs were saved by push_call_dummy () */
470 for (i
= 0; i
< NUM_REGS
; i
++)
472 adr
-= REGISTER_RAW_SIZE (i
);
478 pc
= get_pc_function_start (fip
->pc
);
480 locals
= i386_get_frame_setup (pc
);
484 adr
= fip
->frame
- 4 - locals
;
485 for (i
= 0; i
< 8; i
++)
487 op
= codestream_get ();
488 if (op
< 0x50 || op
> 0x57)
490 #ifdef I386_REGNO_TO_SYMMETRY
491 /* Dynix uses different internal numbering. Ick. */
492 fsrp
->regs
[I386_REGNO_TO_SYMMETRY(op
- 0x50)] = adr
;
494 fsrp
->regs
[op
- 0x50] = adr
;
500 fsrp
->regs
[PC_REGNUM
] = fip
->frame
+ 4;
501 fsrp
->regs
[FP_REGNUM
] = fip
->frame
;
504 /* return pc of first real instruction */
507 i386_skip_prologue (pc
)
512 static unsigned char pic_pat
[6] = { 0xe8, 0, 0, 0, 0, /* call 0x0 */
513 0x5b, /* popl %ebx */
517 if (i386_get_frame_setup (pc
) < 0)
520 /* found valid frame setup - codestream now points to
521 * start of push instructions for saving registers
524 /* skip over register saves */
525 for (i
= 0; i
< 8; i
++)
527 op
= codestream_peek ();
528 /* break if not pushl inst */
529 if (op
< 0x50 || op
> 0x57)
534 /* The native cc on SVR4 in -K PIC mode inserts the following code to get
535 the address of the global offset table (GOT) into register %ebx.
538 movl %ebx,x(%ebp) (optional)
540 This code is with the rest of the prologue (at the end of the
541 function), so we have to skip it to get to the first real
542 instruction at the start of the function. */
544 pos
= codestream_tell ();
545 for (i
= 0; i
< 6; i
++)
547 op
= codestream_get ();
548 if (pic_pat
[i
] != op
)
553 unsigned char buf
[4];
556 op
= codestream_get ();
557 if (op
== 0x89) /* movl %ebx, x(%ebp) */
559 op
= codestream_get ();
560 if (op
== 0x5d) /* one byte offset from %ebp */
563 codestream_read (buf
, 1);
565 else if (op
== 0x9d) /* four byte offset from %ebp */
568 codestream_read (buf
, 4);
570 else /* unexpected instruction */
572 op
= codestream_get ();
575 if (delta
> 0 && op
== 0x81 && codestream_get () == 0xc3)
580 codestream_seek (pos
);
584 return (codestream_tell ());
588 i386_push_dummy_frame ()
590 CORE_ADDR sp
= read_register (SP_REGNUM
);
592 char regbuf
[MAX_REGISTER_RAW_SIZE
];
594 sp
= push_word (sp
, read_register (PC_REGNUM
));
595 sp
= push_word (sp
, read_register (FP_REGNUM
));
596 write_register (FP_REGNUM
, sp
);
597 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
599 read_register_gen (regnum
, regbuf
);
600 sp
= push_bytes (sp
, regbuf
, REGISTER_RAW_SIZE (regnum
));
602 write_register (SP_REGNUM
, sp
);
608 struct frame_info
*frame
= get_current_frame ();
611 struct frame_saved_regs fsr
;
612 char regbuf
[MAX_REGISTER_RAW_SIZE
];
614 fp
= FRAME_FP (frame
);
615 get_frame_saved_regs (frame
, &fsr
);
616 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
619 adr
= fsr
.regs
[regnum
];
622 read_memory (adr
, regbuf
, REGISTER_RAW_SIZE (regnum
));
623 write_register_bytes (REGISTER_BYTE (regnum
), regbuf
,
624 REGISTER_RAW_SIZE (regnum
));
627 write_register (FP_REGNUM
, read_memory_integer (fp
, 4));
628 write_register (PC_REGNUM
, read_memory_integer (fp
+ 4, 4));
629 write_register (SP_REGNUM
, fp
+ 8);
630 flush_cached_frames ();
633 #ifdef GET_LONGJMP_TARGET
635 /* Figure out where the longjmp will land. Slurp the args out of the stack.
636 We expect the first arg to be a pointer to the jmp_buf structure from which
637 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
638 This routine returns true on success. */
641 get_longjmp_target(pc
)
644 char buf
[TARGET_PTR_BIT
/ TARGET_CHAR_BIT
];
645 CORE_ADDR sp
, jb_addr
;
647 sp
= read_register (SP_REGNUM
);
649 if (target_read_memory (sp
+ SP_ARG0
, /* Offset of first arg on stack */
651 TARGET_PTR_BIT
/ TARGET_CHAR_BIT
))
654 jb_addr
= extract_address (buf
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
656 if (target_read_memory (jb_addr
+ JB_PC
* JB_ELEMENT_SIZE
, buf
,
657 TARGET_PTR_BIT
/ TARGET_CHAR_BIT
))
660 *pc
= extract_address (buf
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
665 #endif /* GET_LONGJMP_TARGET */
668 i386_extract_return_value(type
, regbuf
, valbuf
)
670 char regbuf
[REGISTER_BYTES
];
673 /* On AIX, floating point values are returned in floating point registers. */
674 #ifdef I386_AIX_TARGET
675 if (TYPE_CODE_FLT
== TYPE_CODE(type
))
678 /* 387 %st(0), gcc uses this */
679 floatformat_to_double (&floatformat_i387_ext
,
680 ®buf
[REGISTER_BYTE(FP0_REGNUM
)],
682 store_floating (valbuf
, TYPE_LENGTH (type
), d
);
685 #endif /* I386_AIX_TARGET */
687 memcpy (valbuf
, regbuf
, TYPE_LENGTH (type
));
691 #ifdef I386V4_SIGTRAMP_SAVED_PC
692 /* Get saved user PC for sigtramp from the pushed ucontext on the stack
693 for all three variants of SVR4 sigtramps. */
696 i386v4_sigtramp_saved_pc (frame
)
697 struct frame_info
*frame
;
699 CORE_ADDR saved_pc_offset
= 4;
702 find_pc_partial_function (frame
->pc
, &name
, NULL
, NULL
);
705 if (STREQ (name
, "_sigreturn"))
706 saved_pc_offset
= 132 + 14 * 4;
707 else if (STREQ (name
, "_sigacthandler"))
708 saved_pc_offset
= 80 + 14 * 4;
709 else if (STREQ (name
, "sigvechandler"))
710 saved_pc_offset
= 120 + 14 * 4;
714 return read_memory_integer (frame
->next
->frame
+ saved_pc_offset
, 4);
715 return read_memory_integer (read_register (SP_REGNUM
) + saved_pc_offset
, 4);
717 #endif /* I386V4_SIGTRAMP_SAVED_PC */
719 #ifdef STATIC_TRANSFORM_NAME
720 /* SunPRO encodes the static variables. This is not related to C++ mangling,
721 it is done for C too. */
724 sunpro_static_transform_name (name
)
728 if (IS_STATIC_TRANSFORM_NAME (name
))
730 /* For file-local statics there will be a period, a bunch
731 of junk (the contents of which match a string given in the
732 N_OPT), a period and the name. For function-local statics
733 there will be a bunch of junk (which seems to change the
734 second character from 'A' to 'B'), a period, the name of the
735 function, and the name. So just skip everything before the
737 p
= strrchr (name
, '.');
743 #endif /* STATIC_TRANSFORM_NAME */
747 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
750 skip_trampoline_code (pc
, name
)
754 if (pc
&& read_memory_unsigned_integer (pc
, 2) == 0x25ff) /* jmp *(dest) */
756 unsigned long indirect
= read_memory_unsigned_integer (pc
+2, 4);
757 struct minimal_symbol
*indsym
=
758 indirect
? lookup_minimal_symbol_by_pc (indirect
) : 0;
759 char *symname
= indsym
? SYMBOL_NAME(indsym
) : 0;
763 if (strncmp (symname
,"__imp_", 6) == 0
764 || strncmp (symname
,"_imp_", 5) == 0)
765 return name
? 1 : read_memory_unsigned_integer (indirect
, 4);
768 return 0; /* not a trampoline */
772 gdb_print_insn_i386 (memaddr
, info
)
774 disassemble_info
* info
;
776 if (disassembly_flavor
== att_flavor
)
777 print_insn_i386_att (memaddr
, info
);
778 else if (disassembly_flavor
== intel_flavor
)
779 print_insn_i386_intel (memaddr
, info
);
784 _initialize_i386_tdep ()
786 tm_print_insn
= gdb_print_insn_i386
;
787 tm_print_insn_info
.mach
= bfd_lookup_arch (bfd_arch_i386
, 0)->mach
;
789 /* Add the variable that controls the disassembly flavor */
791 add_set_enum_cmd ("disassembly-flavor", no_class
,
793 (char *) &disassembly_flavor
,
794 "Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
795 and the default value is \"att\".",