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 /* i386_register_byte[i] is the offset into the register file of the
50 start of register number i. We initialize this from
51 i386_register_raw_size. */
52 int i386_register_byte[MAX_NUM_REGS];
54 /* i386_register_raw_size[i] is the number of bytes of storage in
55 GDB's register array occupied by register i. */
56 int i386_register_raw_size[MAX_NUM_REGS] = {
70 /* i386_register_virtual_size[i] is the size in bytes of the virtual
71 type of register i. */
72 int i386_register_virtual_size[MAX_NUM_REGS];
75 /* This is the variable the is set with "set disassembly-flavor",
76 and its legitimate values. */
77 static char att_flavor[] = "att";
78 static char intel_flavor[] = "intel";
79 static char *valid_flavors[] =
85 static char *disassembly_flavor = att_flavor;
87 static void i386_print_register PARAMS ((char *, int, int));
89 /* This is used to keep the bfd arch_info in sync with the disassembly flavor. */
90 static void set_disassembly_flavor_sfunc PARAMS ((char *, int, struct cmd_list_element *));
91 static void set_disassembly_flavor PARAMS ((void));
93 /* Stdio style buffering was used to minimize calls to ptrace, but this
94 buffering did not take into account that the code section being accessed
95 may not be an even number of buffers long (even if the buffer is only
96 sizeof(int) long). In cases where the code section size happened to
97 be a non-integral number of buffers long, attempting to read the last
98 buffer would fail. Simply using target_read_memory and ignoring errors,
99 rather than read_memory, is not the correct solution, since legitimate
100 access errors would then be totally ignored. To properly handle this
101 situation and continue to use buffering would require that this code
102 be able to determine the minimum code section size granularity (not the
103 alignment of the section itself, since the actual failing case that
104 pointed out this problem had a section alignment of 4 but was not a
105 multiple of 4 bytes long), on a target by target basis, and then
106 adjust it's buffer size accordingly. This is messy, but potentially
107 feasible. It probably needs the bfd library's help and support. For
108 now, the buffer size is set to 1. (FIXME -fnf) */
110 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
111 static CORE_ADDR codestream_next_addr;
112 static CORE_ADDR codestream_addr;
113 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
114 static int codestream_off;
115 static int codestream_cnt;
117 #define codestream_tell() (codestream_addr + codestream_off)
118 #define codestream_peek() (codestream_cnt == 0 ? \
119 codestream_fill(1): codestream_buf[codestream_off])
120 #define codestream_get() (codestream_cnt-- == 0 ? \
121 codestream_fill(0) : codestream_buf[codestream_off++])
124 codestream_fill (peek_flag)
127 codestream_addr = codestream_next_addr;
128 codestream_next_addr += CODESTREAM_BUFSIZ;
130 codestream_cnt = CODESTREAM_BUFSIZ;
131 read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
134 return (codestream_peek ());
136 return (codestream_get ());
140 codestream_seek (place)
143 codestream_next_addr = place / CODESTREAM_BUFSIZ;
144 codestream_next_addr *= CODESTREAM_BUFSIZ;
147 while (codestream_tell () != place)
152 codestream_read (buf, count)
159 for (i = 0; i < count; i++)
160 *p++ = codestream_get ();
163 /* next instruction is a jump, move to target */
168 unsigned char buf[4];
174 pos = codestream_tell ();
177 if (codestream_peek () == 0x66)
183 switch (codestream_get ())
186 /* relative jump: if data16 == 0, disp32, else disp16 */
189 codestream_read (buf, 2);
190 delta = extract_signed_integer (buf, 2);
192 /* include size of jmp inst (including the 0x66 prefix). */
197 codestream_read (buf, 4);
198 delta = extract_signed_integer (buf, 4);
204 /* relative jump, disp8 (ignore data16) */
205 codestream_read (buf, 1);
206 /* Sign-extend it. */
207 delta = extract_signed_integer (buf, 1);
212 codestream_seek (pos);
216 * find & return amound a local space allocated, and advance codestream to
217 * first register push (if any)
219 * if entry sequence doesn't make sense, return -1, and leave
220 * codestream pointer random
224 i386_get_frame_setup (pc)
229 codestream_seek (pc);
233 op = codestream_get ();
235 if (op == 0x58) /* popl %eax */
238 * this function must start with
241 * xchgl %eax, (%esp) 0x87 0x04 0x24
242 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
244 * (the system 5 compiler puts out the second xchg
245 * inst, and the assembler doesn't try to optimize it,
246 * so the 'sib' form gets generated)
248 * this sequence is used to get the address of the return
249 * buffer for a function that returns a structure
252 unsigned char buf[4];
253 static unsigned char proto1[3] =
255 static unsigned char proto2[4] =
256 {0x87, 0x44, 0x24, 0x00};
257 pos = codestream_tell ();
258 codestream_read (buf, 4);
259 if (memcmp (buf, proto1, 3) == 0)
261 else if (memcmp (buf, proto2, 4) == 0)
264 codestream_seek (pos);
265 op = codestream_get (); /* update next opcode */
268 if (op == 0x68 || op == 0x6a)
271 * this function may start with
281 unsigned char buf[8];
283 /* Skip past the pushl instruction; it has either a one-byte
284 or a four-byte operand, depending on the opcode. */
285 pos = codestream_tell ();
290 codestream_seek (pos);
292 /* Read the following 8 bytes, which should be "call _probe" (6 bytes)
293 followed by "addl $4,%esp" (2 bytes). */
294 codestream_read (buf, sizeof (buf));
295 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
297 codestream_seek (pos);
298 op = codestream_get (); /* update next opcode */
301 if (op == 0x55) /* pushl %ebp */
303 /* check for movl %esp, %ebp - can be written two ways */
304 switch (codestream_get ())
307 if (codestream_get () != 0xec)
311 if (codestream_get () != 0xe5)
317 /* check for stack adjustment
321 * note: you can't subtract a 16 bit immediate
322 * from a 32 bit reg, so we don't have to worry
323 * about a data16 prefix
325 op = codestream_peek ();
328 /* subl with 8 bit immed */
330 if (codestream_get () != 0xec)
331 /* Some instruction starting with 0x83 other than subl. */
333 codestream_seek (codestream_tell () - 2);
336 /* subl with signed byte immediate
337 * (though it wouldn't make sense to be negative)
339 return (codestream_get ());
344 /* Maybe it is subl with 32 bit immedediate. */
346 if (codestream_get () != 0xec)
347 /* Some instruction starting with 0x81 other than subl. */
349 codestream_seek (codestream_tell () - 2);
352 /* It is subl with 32 bit immediate. */
353 codestream_read ((unsigned char *) buf, 4);
354 return extract_signed_integer (buf, 4);
364 /* enter instruction: arg is 16 bit unsigned immed */
365 codestream_read ((unsigned char *) buf, 2);
366 codestream_get (); /* flush final byte of enter instruction */
367 return extract_unsigned_integer (buf, 2);
372 /* Return number of args passed to a frame.
373 Can return -1, meaning no way to tell. */
376 i386_frame_num_args (fi)
377 struct frame_info *fi;
382 /* This loses because not only might the compiler not be popping the
383 args right after the function call, it might be popping args from both
384 this call and a previous one, and we would say there are more args
385 than there really are. */
389 struct frame_info *pfi;
391 /* on the 386, the instruction following the call could be:
393 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
394 anything else - zero args */
398 frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
400 /* In the absence of a frame pointer, GDB doesn't get correct values
401 for nameless arguments. Return -1, so it doesn't print any
402 nameless arguments. */
405 pfi = get_prev_frame (fi);
408 /* Note: this can happen if we are looking at the frame for
409 main, because FRAME_CHAIN_VALID won't let us go into
410 start. If we have debugging symbols, that's not really
411 a big deal; it just means it will only show as many arguments
412 to main as are declared. */
418 op = read_memory_integer (retpc, 1);
424 op = read_memory_integer (retpc + 1, 1);
426 /* addl $<signed imm 8 bits>, %esp */
427 return (read_memory_integer (retpc + 2, 1) & 0xff) / 4;
432 { /* add with 32 bit immediate */
433 op = read_memory_integer (retpc + 1, 1);
435 /* addl $<imm 32>, %esp */
436 return read_memory_integer (retpc + 2, 4) / 4;
449 * parse the first few instructions of the function to see
450 * what registers were stored.
452 * We handle these cases:
454 * The startup sequence can be at the start of the function,
455 * or the function can start with a branch to startup code at the end.
457 * %ebp can be set up with either the 'enter' instruction, or
458 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
459 * but was once used in the sys5 compiler)
461 * Local space is allocated just below the saved %ebp by either the
462 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
463 * a 16 bit unsigned argument for space to allocate, and the
464 * 'addl' instruction could have either a signed byte, or
467 * Next, the registers used by this function are pushed. In
468 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
469 * (and sometimes a harmless bug causes it to also save but not restore %eax);
470 * however, the code below is willing to see the pushes in any order,
471 * and will handle up to 8 of them.
473 * If the setup sequence is at the end of the function, then the
474 * next instruction will be a branch back to the start.
478 i386_frame_init_saved_regs (fip)
479 struct frame_info *fip;
483 CORE_ADDR dummy_bottom;
491 frame_saved_regs_zalloc (fip);
493 /* if frame is the end of a dummy, compute where the
496 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
498 /* check if the PC is in the stack, in a dummy frame */
499 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
501 /* all regs were saved by push_call_dummy () */
503 for (i = 0; i < NUM_REGS; i++)
505 adr -= REGISTER_RAW_SIZE (i);
506 fip->saved_regs[i] = adr;
511 pc = get_pc_function_start (fip->pc);
513 locals = i386_get_frame_setup (pc);
517 adr = fip->frame - 4 - locals;
518 for (i = 0; i < 8; i++)
520 op = codestream_get ();
521 if (op < 0x50 || op > 0x57)
523 #ifdef I386_REGNO_TO_SYMMETRY
524 /* Dynix uses different internal numbering. Ick. */
525 fip->saved_regs[I386_REGNO_TO_SYMMETRY (op - 0x50)] = adr;
527 fip->saved_regs[op - 0x50] = adr;
533 fip->saved_regs[PC_REGNUM] = fip->frame + 4;
534 fip->saved_regs[FP_REGNUM] = fip->frame;
537 /* return pc of first real instruction */
540 i386_skip_prologue (pc)
545 static unsigned char pic_pat[6] =
546 {0xe8, 0, 0, 0, 0, /* call 0x0 */
547 0x5b, /* popl %ebx */
551 if (i386_get_frame_setup (pc) < 0)
554 /* found valid frame setup - codestream now points to
555 * start of push instructions for saving registers
558 /* skip over register saves */
559 for (i = 0; i < 8; i++)
561 op = codestream_peek ();
562 /* break if not pushl inst */
563 if (op < 0x50 || op > 0x57)
568 /* The native cc on SVR4 in -K PIC mode inserts the following code to get
569 the address of the global offset table (GOT) into register %ebx.
572 movl %ebx,x(%ebp) (optional)
574 This code is with the rest of the prologue (at the end of the
575 function), so we have to skip it to get to the first real
576 instruction at the start of the function. */
578 pos = codestream_tell ();
579 for (i = 0; i < 6; i++)
581 op = codestream_get ();
582 if (pic_pat[i] != op)
587 unsigned char buf[4];
590 op = codestream_get ();
591 if (op == 0x89) /* movl %ebx, x(%ebp) */
593 op = codestream_get ();
594 if (op == 0x5d) /* one byte offset from %ebp */
597 codestream_read (buf, 1);
599 else if (op == 0x9d) /* four byte offset from %ebp */
602 codestream_read (buf, 4);
604 else /* unexpected instruction */
606 op = codestream_get ();
609 if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
614 codestream_seek (pos);
618 return (codestream_tell ());
622 i386_push_dummy_frame ()
624 CORE_ADDR sp = read_register (SP_REGNUM);
626 char regbuf[MAX_REGISTER_RAW_SIZE];
628 sp = push_word (sp, read_register (PC_REGNUM));
629 sp = push_word (sp, read_register (FP_REGNUM));
630 write_register (FP_REGNUM, sp);
631 for (regnum = 0; regnum < NUM_REGS; regnum++)
633 read_register_gen (regnum, regbuf);
634 sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
636 write_register (SP_REGNUM, sp);
642 struct frame_info *frame = get_current_frame ();
645 char regbuf[MAX_REGISTER_RAW_SIZE];
647 fp = FRAME_FP (frame);
648 i386_frame_init_saved_regs (frame);
650 for (regnum = 0; regnum < NUM_REGS; regnum++)
653 adr = frame->saved_regs[regnum];
656 read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
657 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
658 REGISTER_RAW_SIZE (regnum));
661 write_register (FP_REGNUM, read_memory_integer (fp, 4));
662 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
663 write_register (SP_REGNUM, fp + 8);
664 flush_cached_frames ();
667 #ifdef GET_LONGJMP_TARGET
669 /* Figure out where the longjmp will land. Slurp the args out of the stack.
670 We expect the first arg to be a pointer to the jmp_buf structure from which
671 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
672 This routine returns true on success. */
675 get_longjmp_target (pc)
678 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
679 CORE_ADDR sp, jb_addr;
681 sp = read_register (SP_REGNUM);
683 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
685 TARGET_PTR_BIT / TARGET_CHAR_BIT))
688 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
690 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
691 TARGET_PTR_BIT / TARGET_CHAR_BIT))
694 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
699 #endif /* GET_LONGJMP_TARGET */
701 /* These registers are used for returning integers (and on some
702 targets also for returning `struct' and `union' values when their
703 size and alignment match an integer type. */
704 #define LOW_RETURN_REGNUM 0 /* %eax */
705 #define HIGH_RETURN_REGNUM 2 /* %edx */
707 /* Extract from an array REGBUF containing the (raw) register state, a
708 function return value of TYPE, and copy that, in virtual format,
712 i386_extract_return_value (struct type *type, char *regbuf, char *valbuf)
714 int len = TYPE_LENGTH (type);
716 if (TYPE_CODE_FLT == TYPE_CODE (type))
720 warning ("Cannot find floating-point return value.");
721 memset (valbuf, 0, len);
724 /* Floating-point return values can be found in %st(0). */
725 if (len == TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT
726 && TARGET_LONG_DOUBLE_FORMAT == &floatformat_i387_ext)
728 /* Copy straight over, but take care of the padding. */
729 memcpy (valbuf, ®buf[REGISTER_BYTE (FP0_REGNUM)],
731 memset (valbuf + FPU_REG_RAW_SIZE, 0, len - FPU_REG_RAW_SIZE);
735 /* Convert the extended floating-point number found in
736 %st(0) to the desired type. This is probably not exactly
737 how it would happen on the target itself, but it is the
740 floatformat_to_doublest (&floatformat_i387_ext,
741 ®buf[REGISTER_BYTE (FP0_REGNUM)], &val);
742 store_floating (valbuf, TYPE_LENGTH (type), val);
747 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
748 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
751 memcpy (valbuf, ®buf[REGISTER_BYTE (LOW_RETURN_REGNUM)], len);
752 else if (len <= (low_size + high_size))
755 ®buf[REGISTER_BYTE (LOW_RETURN_REGNUM)], low_size);
756 memcpy (valbuf + low_size,
757 ®buf[REGISTER_BYTE (HIGH_RETURN_REGNUM)], len - low_size);
760 internal_error ("Cannot extract return value of %d bytes long.", len);
764 /* Convert data from raw format for register REGNUM in buffer FROM to
765 virtual format with type TYPE in buffer TO. In principle both
766 formats are identical except that the virtual format has two extra
767 bytes appended that aren't used. We set these to zero. */
770 i386_register_convert_to_virtual (int regnum, struct type *type,
771 char *from, char *to)
773 /* Copy straight over, but take care of the padding. */
774 memcpy (to, from, FPU_REG_RAW_SIZE);
775 memset (to + FPU_REG_RAW_SIZE, 0, TYPE_LENGTH (type) - FPU_REG_RAW_SIZE);
778 /* Convert data from virtual format with type TYPE in buffer FROM to
779 raw format for register REGNUM in buffer TO. Simply omit the two
783 i386_register_convert_to_raw (struct type *type, int regnum,
784 char *from, char *to)
786 memcpy (to, from, FPU_REG_RAW_SIZE);
790 #ifdef I386V4_SIGTRAMP_SAVED_PC
791 /* Get saved user PC for sigtramp from the pushed ucontext on the stack
792 for all three variants of SVR4 sigtramps. */
795 i386v4_sigtramp_saved_pc (frame)
796 struct frame_info *frame;
798 CORE_ADDR saved_pc_offset = 4;
801 find_pc_partial_function (frame->pc, &name, NULL, NULL);
804 if (STREQ (name, "_sigreturn"))
805 saved_pc_offset = 132 + 14 * 4;
806 else if (STREQ (name, "_sigacthandler"))
807 saved_pc_offset = 80 + 14 * 4;
808 else if (STREQ (name, "sigvechandler"))
809 saved_pc_offset = 120 + 14 * 4;
813 return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
814 return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
816 #endif /* I386V4_SIGTRAMP_SAVED_PC */
819 #ifdef STATIC_TRANSFORM_NAME
820 /* SunPRO encodes the static variables. This is not related to C++ mangling,
821 it is done for C too. */
824 sunpro_static_transform_name (name)
828 if (IS_STATIC_TRANSFORM_NAME (name))
830 /* For file-local statics there will be a period, a bunch
831 of junk (the contents of which match a string given in the
832 N_OPT), a period and the name. For function-local statics
833 there will be a bunch of junk (which seems to change the
834 second character from 'A' to 'B'), a period, the name of the
835 function, and the name. So just skip everything before the
837 p = strrchr (name, '.');
843 #endif /* STATIC_TRANSFORM_NAME */
847 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
850 skip_trampoline_code (pc, name)
854 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
856 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
857 struct minimal_symbol *indsym =
858 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
859 char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
863 if (strncmp (symname, "__imp_", 6) == 0
864 || strncmp (symname, "_imp_", 5) == 0)
865 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
868 return 0; /* not a trampoline */
872 gdb_print_insn_i386 (memaddr, info)
874 disassemble_info *info;
876 if (disassembly_flavor == att_flavor)
877 return print_insn_i386_att (memaddr, info);
878 else if (disassembly_flavor == intel_flavor)
879 return print_insn_i386_intel (memaddr, info);
880 /* Never reached - disassembly_flavour is always either att_flavor
885 /* If the disassembly mode is intel, we have to also switch the
886 bfd mach_type. This function is run in the set disassembly_flavor
887 command, and does that. */
890 set_disassembly_flavor_sfunc (args, from_tty, c)
893 struct cmd_list_element *c;
895 set_disassembly_flavor ();
899 set_disassembly_flavor ()
901 if (disassembly_flavor == att_flavor)
902 set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386);
903 else if (disassembly_flavor == intel_flavor)
904 set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386_intel_syntax);
909 _initialize_i386_tdep ()
911 /* Initialize the table saying where each register starts in the
917 for (i = 0; i < MAX_NUM_REGS; i++)
919 i386_register_byte[i] = offset;
920 offset += i386_register_raw_size[i];
924 /* Initialize the table of virtual register sizes. */
928 for (i = 0; i < MAX_NUM_REGS; i++)
929 i386_register_virtual_size[i] = TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (i));
932 tm_print_insn = gdb_print_insn_i386;
933 tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
935 /* Add the variable that controls the disassembly flavor */
937 struct cmd_list_element *new_cmd;
939 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
942 "Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
943 and the default value is \"att\".",
945 new_cmd->function.sfunc = set_disassembly_flavor_sfunc;
946 add_show_from_set (new_cmd, &showlist);
949 /* Finally, initialize the disassembly flavor to the default given
950 in the disassembly_flavor variable */
952 set_disassembly_flavor ();