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 (CORE_ADDR);
35 static void i386_follow_jump (void);
37 static void codestream_read (unsigned char *, int);
39 static void codestream_seek (CORE_ADDR);
41 static unsigned char codestream_fill (int);
43 CORE_ADDR skip_trampoline_code (CORE_ADDR, char *);
45 static int gdb_print_insn_i386 (bfd_vma, disassemble_info *);
47 void _initialize_i386_tdep (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 (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 (char *, int,
91 struct cmd_list_element *);
92 static void set_disassembly_flavor (void);
94 /* Stdio style buffering was used to minimize calls to ptrace, but this
95 buffering did not take into account that the code section being accessed
96 may not be an even number of buffers long (even if the buffer is only
97 sizeof(int) long). In cases where the code section size happened to
98 be a non-integral number of buffers long, attempting to read the last
99 buffer would fail. Simply using target_read_memory and ignoring errors,
100 rather than read_memory, is not the correct solution, since legitimate
101 access errors would then be totally ignored. To properly handle this
102 situation and continue to use buffering would require that this code
103 be able to determine the minimum code section size granularity (not the
104 alignment of the section itself, since the actual failing case that
105 pointed out this problem had a section alignment of 4 but was not a
106 multiple of 4 bytes long), on a target by target basis, and then
107 adjust it's buffer size accordingly. This is messy, but potentially
108 feasible. It probably needs the bfd library's help and support. For
109 now, the buffer size is set to 1. (FIXME -fnf) */
111 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
112 static CORE_ADDR codestream_next_addr;
113 static CORE_ADDR codestream_addr;
114 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
115 static int codestream_off;
116 static int codestream_cnt;
118 #define codestream_tell() (codestream_addr + codestream_off)
119 #define codestream_peek() (codestream_cnt == 0 ? \
120 codestream_fill(1): codestream_buf[codestream_off])
121 #define codestream_get() (codestream_cnt-- == 0 ? \
122 codestream_fill(0) : codestream_buf[codestream_off++])
125 codestream_fill (peek_flag)
128 codestream_addr = codestream_next_addr;
129 codestream_next_addr += CODESTREAM_BUFSIZ;
131 codestream_cnt = CODESTREAM_BUFSIZ;
132 read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
135 return (codestream_peek ());
137 return (codestream_get ());
141 codestream_seek (place)
144 codestream_next_addr = place / CODESTREAM_BUFSIZ;
145 codestream_next_addr *= CODESTREAM_BUFSIZ;
148 while (codestream_tell () != place)
153 codestream_read (buf, count)
160 for (i = 0; i < count; i++)
161 *p++ = codestream_get ();
164 /* next instruction is a jump, move to target */
169 unsigned char buf[4];
175 pos = codestream_tell ();
178 if (codestream_peek () == 0x66)
184 switch (codestream_get ())
187 /* relative jump: if data16 == 0, disp32, else disp16 */
190 codestream_read (buf, 2);
191 delta = extract_signed_integer (buf, 2);
193 /* include size of jmp inst (including the 0x66 prefix). */
198 codestream_read (buf, 4);
199 delta = extract_signed_integer (buf, 4);
205 /* relative jump, disp8 (ignore data16) */
206 codestream_read (buf, 1);
207 /* Sign-extend it. */
208 delta = extract_signed_integer (buf, 1);
213 codestream_seek (pos);
217 * find & return amound a local space allocated, and advance codestream to
218 * first register push (if any)
220 * if entry sequence doesn't make sense, return -1, and leave
221 * codestream pointer random
225 i386_get_frame_setup (pc)
230 codestream_seek (pc);
234 op = codestream_get ();
236 if (op == 0x58) /* popl %eax */
239 * this function must start with
242 * xchgl %eax, (%esp) 0x87 0x04 0x24
243 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
245 * (the system 5 compiler puts out the second xchg
246 * inst, and the assembler doesn't try to optimize it,
247 * so the 'sib' form gets generated)
249 * this sequence is used to get the address of the return
250 * buffer for a function that returns a structure
253 unsigned char buf[4];
254 static unsigned char proto1[3] =
256 static unsigned char proto2[4] =
257 {0x87, 0x44, 0x24, 0x00};
258 pos = codestream_tell ();
259 codestream_read (buf, 4);
260 if (memcmp (buf, proto1, 3) == 0)
262 else if (memcmp (buf, proto2, 4) == 0)
265 codestream_seek (pos);
266 op = codestream_get (); /* update next opcode */
269 if (op == 0x68 || op == 0x6a)
272 * this function may start with
282 unsigned char buf[8];
284 /* Skip past the pushl instruction; it has either a one-byte
285 or a four-byte operand, depending on the opcode. */
286 pos = codestream_tell ();
291 codestream_seek (pos);
293 /* Read the following 8 bytes, which should be "call _probe" (6 bytes)
294 followed by "addl $4,%esp" (2 bytes). */
295 codestream_read (buf, sizeof (buf));
296 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
298 codestream_seek (pos);
299 op = codestream_get (); /* update next opcode */
302 if (op == 0x55) /* pushl %ebp */
304 /* check for movl %esp, %ebp - can be written two ways */
305 switch (codestream_get ())
308 if (codestream_get () != 0xec)
312 if (codestream_get () != 0xe5)
318 /* check for stack adjustment
322 * note: you can't subtract a 16 bit immediate
323 * from a 32 bit reg, so we don't have to worry
324 * about a data16 prefix
326 op = codestream_peek ();
329 /* subl with 8 bit immed */
331 if (codestream_get () != 0xec)
332 /* Some instruction starting with 0x83 other than subl. */
334 codestream_seek (codestream_tell () - 2);
337 /* subl with signed byte immediate
338 * (though it wouldn't make sense to be negative)
340 return (codestream_get ());
345 /* Maybe it is subl with 32 bit immedediate. */
347 if (codestream_get () != 0xec)
348 /* Some instruction starting with 0x81 other than subl. */
350 codestream_seek (codestream_tell () - 2);
353 /* It is subl with 32 bit immediate. */
354 codestream_read ((unsigned char *) buf, 4);
355 return extract_signed_integer (buf, 4);
365 /* enter instruction: arg is 16 bit unsigned immed */
366 codestream_read ((unsigned char *) buf, 2);
367 codestream_get (); /* flush final byte of enter instruction */
368 return extract_unsigned_integer (buf, 2);
373 /* Return number of args passed to a frame.
374 Can return -1, meaning no way to tell. */
377 i386_frame_num_args (fi)
378 struct frame_info *fi;
383 /* This loses because not only might the compiler not be popping the
384 args right after the function call, it might be popping args from both
385 this call and a previous one, and we would say there are more args
386 than there really are. */
390 struct frame_info *pfi;
392 /* on the 386, the instruction following the call could be:
394 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
395 anything else - zero args */
399 frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
401 /* In the absence of a frame pointer, GDB doesn't get correct values
402 for nameless arguments. Return -1, so it doesn't print any
403 nameless arguments. */
406 pfi = get_prev_frame (fi);
409 /* Note: this can happen if we are looking at the frame for
410 main, because FRAME_CHAIN_VALID won't let us go into
411 start. If we have debugging symbols, that's not really
412 a big deal; it just means it will only show as many arguments
413 to main as are declared. */
419 op = read_memory_integer (retpc, 1);
425 op = read_memory_integer (retpc + 1, 1);
427 /* addl $<signed imm 8 bits>, %esp */
428 return (read_memory_integer (retpc + 2, 1) & 0xff) / 4;
433 { /* add with 32 bit immediate */
434 op = read_memory_integer (retpc + 1, 1);
436 /* addl $<imm 32>, %esp */
437 return read_memory_integer (retpc + 2, 4) / 4;
450 * parse the first few instructions of the function to see
451 * what registers were stored.
453 * We handle these cases:
455 * The startup sequence can be at the start of the function,
456 * or the function can start with a branch to startup code at the end.
458 * %ebp can be set up with either the 'enter' instruction, or
459 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
460 * but was once used in the sys5 compiler)
462 * Local space is allocated just below the saved %ebp by either the
463 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
464 * a 16 bit unsigned argument for space to allocate, and the
465 * 'addl' instruction could have either a signed byte, or
468 * Next, the registers used by this function are pushed. In
469 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
470 * (and sometimes a harmless bug causes it to also save but not restore %eax);
471 * however, the code below is willing to see the pushes in any order,
472 * and will handle up to 8 of them.
474 * If the setup sequence is at the end of the function, then the
475 * next instruction will be a branch back to the start.
479 i386_frame_init_saved_regs (fip)
480 struct frame_info *fip;
484 CORE_ADDR dummy_bottom;
492 frame_saved_regs_zalloc (fip);
494 /* if frame is the end of a dummy, compute where the
497 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
499 /* check if the PC is in the stack, in a dummy frame */
500 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
502 /* all regs were saved by push_call_dummy () */
504 for (i = 0; i < NUM_REGS; i++)
506 adr -= REGISTER_RAW_SIZE (i);
507 fip->saved_regs[i] = adr;
512 pc = get_pc_function_start (fip->pc);
514 locals = i386_get_frame_setup (pc);
518 adr = fip->frame - 4 - locals;
519 for (i = 0; i < 8; i++)
521 op = codestream_get ();
522 if (op < 0x50 || op > 0x57)
524 #ifdef I386_REGNO_TO_SYMMETRY
525 /* Dynix uses different internal numbering. Ick. */
526 fip->saved_regs[I386_REGNO_TO_SYMMETRY (op - 0x50)] = adr;
528 fip->saved_regs[op - 0x50] = adr;
534 fip->saved_regs[PC_REGNUM] = fip->frame + 4;
535 fip->saved_regs[FP_REGNUM] = fip->frame;
538 /* return pc of first real instruction */
541 i386_skip_prologue (pc)
546 static unsigned char pic_pat[6] =
547 {0xe8, 0, 0, 0, 0, /* call 0x0 */
548 0x5b, /* popl %ebx */
552 if (i386_get_frame_setup (pc) < 0)
555 /* found valid frame setup - codestream now points to
556 * start of push instructions for saving registers
559 /* skip over register saves */
560 for (i = 0; i < 8; i++)
562 op = codestream_peek ();
563 /* break if not pushl inst */
564 if (op < 0x50 || op > 0x57)
569 /* The native cc on SVR4 in -K PIC mode inserts the following code to get
570 the address of the global offset table (GOT) into register %ebx.
573 movl %ebx,x(%ebp) (optional)
575 This code is with the rest of the prologue (at the end of the
576 function), so we have to skip it to get to the first real
577 instruction at the start of the function. */
579 pos = codestream_tell ();
580 for (i = 0; i < 6; i++)
582 op = codestream_get ();
583 if (pic_pat[i] != op)
588 unsigned char buf[4];
591 op = codestream_get ();
592 if (op == 0x89) /* movl %ebx, x(%ebp) */
594 op = codestream_get ();
595 if (op == 0x5d) /* one byte offset from %ebp */
598 codestream_read (buf, 1);
600 else if (op == 0x9d) /* four byte offset from %ebp */
603 codestream_read (buf, 4);
605 else /* unexpected instruction */
607 op = codestream_get ();
610 if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
615 codestream_seek (pos);
619 return (codestream_tell ());
623 i386_push_dummy_frame ()
625 CORE_ADDR sp = read_register (SP_REGNUM);
627 char regbuf[MAX_REGISTER_RAW_SIZE];
629 sp = push_word (sp, read_register (PC_REGNUM));
630 sp = push_word (sp, read_register (FP_REGNUM));
631 write_register (FP_REGNUM, sp);
632 for (regnum = 0; regnum < NUM_REGS; regnum++)
634 read_register_gen (regnum, regbuf);
635 sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
637 write_register (SP_REGNUM, sp);
643 struct frame_info *frame = get_current_frame ();
646 char regbuf[MAX_REGISTER_RAW_SIZE];
648 fp = FRAME_FP (frame);
649 i386_frame_init_saved_regs (frame);
651 for (regnum = 0; regnum < NUM_REGS; regnum++)
654 adr = frame->saved_regs[regnum];
657 read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
658 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
659 REGISTER_RAW_SIZE (regnum));
662 write_register (FP_REGNUM, read_memory_integer (fp, 4));
663 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
664 write_register (SP_REGNUM, fp + 8);
665 flush_cached_frames ();
668 #ifdef GET_LONGJMP_TARGET
670 /* Figure out where the longjmp will land. Slurp the args out of the stack.
671 We expect the first arg to be a pointer to the jmp_buf structure from which
672 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
673 This routine returns true on success. */
676 get_longjmp_target (pc)
679 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
680 CORE_ADDR sp, jb_addr;
682 sp = read_register (SP_REGNUM);
684 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
686 TARGET_PTR_BIT / TARGET_CHAR_BIT))
689 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
691 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
692 TARGET_PTR_BIT / TARGET_CHAR_BIT))
695 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
700 #endif /* GET_LONGJMP_TARGET */
702 /* These registers are used for returning integers (and on some
703 targets also for returning `struct' and `union' values when their
704 size and alignment match an integer type. */
705 #define LOW_RETURN_REGNUM 0 /* %eax */
706 #define HIGH_RETURN_REGNUM 2 /* %edx */
708 /* Extract from an array REGBUF containing the (raw) register state, a
709 function return value of TYPE, and copy that, in virtual format,
713 i386_extract_return_value (struct type *type, char *regbuf, char *valbuf)
715 int len = TYPE_LENGTH (type);
717 if (TYPE_CODE_FLT == TYPE_CODE (type))
721 warning ("Cannot find floating-point return value.");
722 memset (valbuf, 0, len);
725 /* Floating-point return values can be found in %st(0). */
726 if (len == TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT
727 && TARGET_LONG_DOUBLE_FORMAT == &floatformat_i387_ext)
729 /* Copy straight over, but take care of the padding. */
730 memcpy (valbuf, ®buf[REGISTER_BYTE (FP0_REGNUM)],
732 memset (valbuf + FPU_REG_RAW_SIZE, 0, len - FPU_REG_RAW_SIZE);
736 /* Convert the extended floating-point number found in
737 %st(0) to the desired type. This is probably not exactly
738 how it would happen on the target itself, but it is the
741 floatformat_to_doublest (&floatformat_i387_ext,
742 ®buf[REGISTER_BYTE (FP0_REGNUM)], &val);
743 store_floating (valbuf, TYPE_LENGTH (type), val);
748 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
749 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
752 memcpy (valbuf, ®buf[REGISTER_BYTE (LOW_RETURN_REGNUM)], len);
753 else if (len <= (low_size + high_size))
756 ®buf[REGISTER_BYTE (LOW_RETURN_REGNUM)], low_size);
757 memcpy (valbuf + low_size,
758 ®buf[REGISTER_BYTE (HIGH_RETURN_REGNUM)], len - low_size);
761 internal_error ("Cannot extract return value of %d bytes long.", len);
765 /* Convert data from raw format for register REGNUM in buffer FROM to
766 virtual format with type TYPE in buffer TO. In principle both
767 formats are identical except that the virtual format has two extra
768 bytes appended that aren't used. We set these to zero. */
771 i386_register_convert_to_virtual (int regnum, struct type *type,
772 char *from, char *to)
774 /* Copy straight over, but take care of the padding. */
775 memcpy (to, from, FPU_REG_RAW_SIZE);
776 memset (to + FPU_REG_RAW_SIZE, 0, TYPE_LENGTH (type) - FPU_REG_RAW_SIZE);
779 /* Convert data from virtual format with type TYPE in buffer FROM to
780 raw format for register REGNUM in buffer TO. Simply omit the two
784 i386_register_convert_to_raw (struct type *type, int regnum,
785 char *from, char *to)
787 memcpy (to, from, FPU_REG_RAW_SIZE);
791 #ifdef I386V4_SIGTRAMP_SAVED_PC
792 /* Get saved user PC for sigtramp from the pushed ucontext on the stack
793 for all three variants of SVR4 sigtramps. */
796 i386v4_sigtramp_saved_pc (frame)
797 struct frame_info *frame;
799 CORE_ADDR saved_pc_offset = 4;
802 find_pc_partial_function (frame->pc, &name, NULL, NULL);
805 if (STREQ (name, "_sigreturn"))
806 saved_pc_offset = 132 + 14 * 4;
807 else if (STREQ (name, "_sigacthandler"))
808 saved_pc_offset = 80 + 14 * 4;
809 else if (STREQ (name, "sigvechandler"))
810 saved_pc_offset = 120 + 14 * 4;
814 return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
815 return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
817 #endif /* I386V4_SIGTRAMP_SAVED_PC */
820 #ifdef STATIC_TRANSFORM_NAME
821 /* SunPRO encodes the static variables. This is not related to C++ mangling,
822 it is done for C too. */
825 sunpro_static_transform_name (name)
829 if (IS_STATIC_TRANSFORM_NAME (name))
831 /* For file-local statics there will be a period, a bunch
832 of junk (the contents of which match a string given in the
833 N_OPT), a period and the name. For function-local statics
834 there will be a bunch of junk (which seems to change the
835 second character from 'A' to 'B'), a period, the name of the
836 function, and the name. So just skip everything before the
838 p = strrchr (name, '.');
844 #endif /* STATIC_TRANSFORM_NAME */
848 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
851 skip_trampoline_code (pc, name)
855 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
857 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
858 struct minimal_symbol *indsym =
859 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
860 char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
864 if (strncmp (symname, "__imp_", 6) == 0
865 || strncmp (symname, "_imp_", 5) == 0)
866 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
869 return 0; /* not a trampoline */
873 gdb_print_insn_i386 (memaddr, info)
875 disassemble_info *info;
877 if (disassembly_flavor == att_flavor)
878 return print_insn_i386_att (memaddr, info);
879 else if (disassembly_flavor == intel_flavor)
880 return print_insn_i386_intel (memaddr, info);
881 /* Never reached - disassembly_flavour is always either att_flavor
886 /* If the disassembly mode is intel, we have to also switch the
887 bfd mach_type. This function is run in the set disassembly_flavor
888 command, and does that. */
891 set_disassembly_flavor_sfunc (args, from_tty, c)
894 struct cmd_list_element *c;
896 set_disassembly_flavor ();
900 set_disassembly_flavor ()
902 if (disassembly_flavor == att_flavor)
903 set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386);
904 else if (disassembly_flavor == intel_flavor)
905 set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386_intel_syntax);
910 _initialize_i386_tdep ()
912 /* Initialize the table saying where each register starts in the
918 for (i = 0; i < MAX_NUM_REGS; i++)
920 i386_register_byte[i] = offset;
921 offset += i386_register_raw_size[i];
925 /* Initialize the table of virtual register sizes. */
929 for (i = 0; i < MAX_NUM_REGS; i++)
930 i386_register_virtual_size[i] = TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (i));
933 tm_print_insn = gdb_print_insn_i386;
934 tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
936 /* Add the variable that controls the disassembly flavor */
938 struct cmd_list_element *new_cmd;
940 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
943 "Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
944 and the default value is \"att\".",
946 new_cmd->function.sfunc = set_disassembly_flavor_sfunc;
947 add_show_from_set (new_cmd, &showlist);
950 /* Finally, initialize the disassembly flavor to the default given
951 in the disassembly_flavor variable */
953 set_disassembly_flavor ();