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 /* This is used to keep the bfd arch_info in sync with the disassembly flavor. */
60 static void set_disassembly_flavor_sfunc PARAMS ((char *, int, struct cmd_list_element *));
61 static void set_disassembly_flavor ();
63 void (*disassembly_flavor_hook) PARAMS((char *args, int from_tty));
65 /* Stdio style buffering was used to minimize calls to ptrace, but this
66 buffering did not take into account that the code section being accessed
67 may not be an even number of buffers long (even if the buffer is only
68 sizeof(int) long). In cases where the code section size happened to
69 be a non-integral number of buffers long, attempting to read the last
70 buffer would fail. Simply using target_read_memory and ignoring errors,
71 rather than read_memory, is not the correct solution, since legitimate
72 access errors would then be totally ignored. To properly handle this
73 situation and continue to use buffering would require that this code
74 be able to determine the minimum code section size granularity (not the
75 alignment of the section itself, since the actual failing case that
76 pointed out this problem had a section alignment of 4 but was not a
77 multiple of 4 bytes long), on a target by target basis, and then
78 adjust it's buffer size accordingly. This is messy, but potentially
79 feasible. It probably needs the bfd library's help and support. For
80 now, the buffer size is set to 1. (FIXME -fnf) */
82 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
83 static CORE_ADDR codestream_next_addr;
84 static CORE_ADDR codestream_addr;
85 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
86 static int codestream_off;
87 static int codestream_cnt;
89 #define codestream_tell() (codestream_addr + codestream_off)
90 #define codestream_peek() (codestream_cnt == 0 ? \
91 codestream_fill(1): codestream_buf[codestream_off])
92 #define codestream_get() (codestream_cnt-- == 0 ? \
93 codestream_fill(0) : codestream_buf[codestream_off++])
96 codestream_fill (peek_flag)
99 codestream_addr = codestream_next_addr;
100 codestream_next_addr += CODESTREAM_BUFSIZ;
102 codestream_cnt = CODESTREAM_BUFSIZ;
103 read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
106 return (codestream_peek());
108 return (codestream_get());
112 codestream_seek (place)
115 codestream_next_addr = place / CODESTREAM_BUFSIZ;
116 codestream_next_addr *= CODESTREAM_BUFSIZ;
119 while (codestream_tell() != place)
124 codestream_read (buf, count)
131 for (i = 0; i < count; i++)
132 *p++ = codestream_get ();
135 /* next instruction is a jump, move to target */
140 unsigned char buf[4];
146 pos = codestream_tell ();
149 if (codestream_peek () == 0x66)
155 switch (codestream_get ())
158 /* relative jump: if data16 == 0, disp32, else disp16 */
161 codestream_read (buf, 2);
162 delta = extract_signed_integer (buf, 2);
164 /* include size of jmp inst (including the 0x66 prefix). */
169 codestream_read (buf, 4);
170 delta = extract_signed_integer (buf, 4);
176 /* relative jump, disp8 (ignore data16) */
177 codestream_read (buf, 1);
178 /* Sign-extend it. */
179 delta = extract_signed_integer (buf, 1);
184 codestream_seek (pos);
188 * find & return amound a local space allocated, and advance codestream to
189 * first register push (if any)
191 * if entry sequence doesn't make sense, return -1, and leave
192 * codestream pointer random
196 i386_get_frame_setup (pc)
201 codestream_seek (pc);
205 op = codestream_get ();
207 if (op == 0x58) /* popl %eax */
210 * this function must start with
213 * xchgl %eax, (%esp) 0x87 0x04 0x24
214 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
216 * (the system 5 compiler puts out the second xchg
217 * inst, and the assembler doesn't try to optimize it,
218 * so the 'sib' form gets generated)
220 * this sequence is used to get the address of the return
221 * buffer for a function that returns a structure
224 unsigned char buf[4];
225 static unsigned char proto1[3] = { 0x87,0x04,0x24 };
226 static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
227 pos = codestream_tell ();
228 codestream_read (buf, 4);
229 if (memcmp (buf, proto1, 3) == 0)
231 else if (memcmp (buf, proto2, 4) == 0)
234 codestream_seek (pos);
235 op = codestream_get (); /* update next opcode */
238 if (op == 0x68 || op == 0x6a)
241 * this function may start with
251 unsigned char buf[8];
253 /* Skip past the pushl instruction; it has either a one-byte
254 or a four-byte operand, depending on the opcode. */
255 pos = codestream_tell ();
260 codestream_seek (pos);
262 /* Read the following 8 bytes, which should be "call _probe" (6 bytes)
263 followed by "addl $4,%esp" (2 bytes). */
264 codestream_read (buf, sizeof (buf));
265 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
267 codestream_seek (pos);
268 op = codestream_get (); /* update next opcode */
271 if (op == 0x55) /* pushl %ebp */
273 /* check for movl %esp, %ebp - can be written two ways */
274 switch (codestream_get ())
277 if (codestream_get () != 0xec)
281 if (codestream_get () != 0xe5)
287 /* check for stack adjustment
291 * note: you can't subtract a 16 bit immediate
292 * from a 32 bit reg, so we don't have to worry
293 * about a data16 prefix
295 op = codestream_peek ();
298 /* subl with 8 bit immed */
300 if (codestream_get () != 0xec)
301 /* Some instruction starting with 0x83 other than subl. */
303 codestream_seek (codestream_tell () - 2);
306 /* subl with signed byte immediate
307 * (though it wouldn't make sense to be negative)
309 return (codestream_get());
314 /* Maybe it is subl with 32 bit immedediate. */
316 if (codestream_get () != 0xec)
317 /* Some instruction starting with 0x81 other than subl. */
319 codestream_seek (codestream_tell () - 2);
322 /* It is subl with 32 bit immediate. */
323 codestream_read ((unsigned char *)buf, 4);
324 return extract_signed_integer (buf, 4);
334 /* enter instruction: arg is 16 bit unsigned immed */
335 codestream_read ((unsigned char *)buf, 2);
336 codestream_get (); /* flush final byte of enter instruction */
337 return extract_unsigned_integer (buf, 2);
342 /* Return number of args passed to a frame.
343 Can return -1, meaning no way to tell. */
346 i386_frame_num_args (fi)
347 struct frame_info *fi;
352 /* This loses because not only might the compiler not be popping the
353 args right after the function call, it might be popping args from both
354 this call and a previous one, and we would say there are more args
355 than there really are. */
359 struct frame_info *pfi;
361 /* on the 386, the instruction following the call could be:
363 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
364 anything else - zero args */
368 FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
370 /* In the absence of a frame pointer, GDB doesn't get correct values
371 for nameless arguments. Return -1, so it doesn't print any
372 nameless arguments. */
375 pfi = get_prev_frame (fi);
378 /* Note: this can happen if we are looking at the frame for
379 main, because FRAME_CHAIN_VALID won't let us go into
380 start. If we have debugging symbols, that's not really
381 a big deal; it just means it will only show as many arguments
382 to main as are declared. */
388 op = read_memory_integer (retpc, 1);
394 op = read_memory_integer (retpc+1, 1);
396 /* addl $<signed imm 8 bits>, %esp */
397 return (read_memory_integer (retpc+2,1)&0xff)/4;
402 { /* add with 32 bit immediate */
403 op = read_memory_integer (retpc+1, 1);
405 /* addl $<imm 32>, %esp */
406 return read_memory_integer (retpc+2, 4) / 4;
419 * parse the first few instructions of the function to see
420 * what registers were stored.
422 * We handle these cases:
424 * The startup sequence can be at the start of the function,
425 * or the function can start with a branch to startup code at the end.
427 * %ebp can be set up with either the 'enter' instruction, or
428 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
429 * but was once used in the sys5 compiler)
431 * Local space is allocated just below the saved %ebp by either the
432 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
433 * a 16 bit unsigned argument for space to allocate, and the
434 * 'addl' instruction could have either a signed byte, or
437 * Next, the registers used by this function are pushed. In
438 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
439 * (and sometimes a harmless bug causes it to also save but not restore %eax);
440 * however, the code below is willing to see the pushes in any order,
441 * and will handle up to 8 of them.
443 * If the setup sequence is at the end of the function, then the
444 * next instruction will be a branch back to the start.
448 i386_frame_find_saved_regs (fip, fsrp)
449 struct frame_info *fip;
450 struct frame_saved_regs *fsrp;
454 CORE_ADDR dummy_bottom;
459 memset (fsrp, 0, sizeof *fsrp);
461 /* if frame is the end of a dummy, compute where the
464 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
466 /* check if the PC is in the stack, in a dummy frame */
467 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
469 /* all regs were saved by push_call_dummy () */
471 for (i = 0; i < NUM_REGS; i++)
473 adr -= REGISTER_RAW_SIZE (i);
479 pc = get_pc_function_start (fip->pc);
481 locals = i386_get_frame_setup (pc);
485 adr = fip->frame - 4 - locals;
486 for (i = 0; i < 8; i++)
488 op = codestream_get ();
489 if (op < 0x50 || op > 0x57)
491 #ifdef I386_REGNO_TO_SYMMETRY
492 /* Dynix uses different internal numbering. Ick. */
493 fsrp->regs[I386_REGNO_TO_SYMMETRY(op - 0x50)] = adr;
495 fsrp->regs[op - 0x50] = adr;
501 fsrp->regs[PC_REGNUM] = fip->frame + 4;
502 fsrp->regs[FP_REGNUM] = fip->frame;
505 /* return pc of first real instruction */
508 i386_skip_prologue (pc)
513 static unsigned char pic_pat[6] = { 0xe8, 0, 0, 0, 0, /* call 0x0 */
514 0x5b, /* popl %ebx */
518 if (i386_get_frame_setup (pc) < 0)
521 /* found valid frame setup - codestream now points to
522 * start of push instructions for saving registers
525 /* skip over register saves */
526 for (i = 0; i < 8; i++)
528 op = codestream_peek ();
529 /* break if not pushl inst */
530 if (op < 0x50 || op > 0x57)
535 /* The native cc on SVR4 in -K PIC mode inserts the following code to get
536 the address of the global offset table (GOT) into register %ebx.
539 movl %ebx,x(%ebp) (optional)
541 This code is with the rest of the prologue (at the end of the
542 function), so we have to skip it to get to the first real
543 instruction at the start of the function. */
545 pos = codestream_tell ();
546 for (i = 0; i < 6; i++)
548 op = codestream_get ();
549 if (pic_pat [i] != op)
554 unsigned char buf[4];
557 op = codestream_get ();
558 if (op == 0x89) /* movl %ebx, x(%ebp) */
560 op = codestream_get ();
561 if (op == 0x5d) /* one byte offset from %ebp */
564 codestream_read (buf, 1);
566 else if (op == 0x9d) /* four byte offset from %ebp */
569 codestream_read (buf, 4);
571 else /* unexpected instruction */
573 op = codestream_get ();
576 if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
581 codestream_seek (pos);
585 return (codestream_tell ());
589 i386_push_dummy_frame ()
591 CORE_ADDR sp = read_register (SP_REGNUM);
593 char regbuf[MAX_REGISTER_RAW_SIZE];
595 sp = push_word (sp, read_register (PC_REGNUM));
596 sp = push_word (sp, read_register (FP_REGNUM));
597 write_register (FP_REGNUM, sp);
598 for (regnum = 0; regnum < NUM_REGS; regnum++)
600 read_register_gen (regnum, regbuf);
601 sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
603 write_register (SP_REGNUM, sp);
609 struct frame_info *frame = get_current_frame ();
612 struct frame_saved_regs fsr;
613 char regbuf[MAX_REGISTER_RAW_SIZE];
615 fp = FRAME_FP (frame);
616 get_frame_saved_regs (frame, &fsr);
617 for (regnum = 0; regnum < NUM_REGS; regnum++)
620 adr = fsr.regs[regnum];
623 read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
624 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
625 REGISTER_RAW_SIZE (regnum));
628 write_register (FP_REGNUM, read_memory_integer (fp, 4));
629 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
630 write_register (SP_REGNUM, fp + 8);
631 flush_cached_frames ();
634 #ifdef GET_LONGJMP_TARGET
636 /* Figure out where the longjmp will land. Slurp the args out of the stack.
637 We expect the first arg to be a pointer to the jmp_buf structure from which
638 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
639 This routine returns true on success. */
642 get_longjmp_target(pc)
645 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
646 CORE_ADDR sp, jb_addr;
648 sp = read_register (SP_REGNUM);
650 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
652 TARGET_PTR_BIT / TARGET_CHAR_BIT))
655 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
657 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
658 TARGET_PTR_BIT / TARGET_CHAR_BIT))
661 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
666 #endif /* GET_LONGJMP_TARGET */
669 i386_extract_return_value(type, regbuf, valbuf)
671 char regbuf[REGISTER_BYTES];
674 /* On AIX, floating point values are returned in floating point registers. */
675 #ifdef I386_AIX_TARGET
676 if (TYPE_CODE_FLT == TYPE_CODE(type))
679 /* 387 %st(0), gcc uses this */
680 floatformat_to_double (&floatformat_i387_ext,
681 ®buf[REGISTER_BYTE(FP0_REGNUM)],
683 store_floating (valbuf, TYPE_LENGTH (type), d);
686 #endif /* I386_AIX_TARGET */
688 memcpy (valbuf, regbuf, TYPE_LENGTH (type));
692 #ifdef I386V4_SIGTRAMP_SAVED_PC
693 /* Get saved user PC for sigtramp from the pushed ucontext on the stack
694 for all three variants of SVR4 sigtramps. */
697 i386v4_sigtramp_saved_pc (frame)
698 struct frame_info *frame;
700 CORE_ADDR saved_pc_offset = 4;
703 find_pc_partial_function (frame->pc, &name, NULL, NULL);
706 if (STREQ (name, "_sigreturn"))
707 saved_pc_offset = 132 + 14 * 4;
708 else if (STREQ (name, "_sigacthandler"))
709 saved_pc_offset = 80 + 14 * 4;
710 else if (STREQ (name, "sigvechandler"))
711 saved_pc_offset = 120 + 14 * 4;
715 return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
716 return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
718 #endif /* I386V4_SIGTRAMP_SAVED_PC */
720 #ifdef STATIC_TRANSFORM_NAME
721 /* SunPRO encodes the static variables. This is not related to C++ mangling,
722 it is done for C too. */
725 sunpro_static_transform_name (name)
729 if (IS_STATIC_TRANSFORM_NAME (name))
731 /* For file-local statics there will be a period, a bunch
732 of junk (the contents of which match a string given in the
733 N_OPT), a period and the name. For function-local statics
734 there will be a bunch of junk (which seems to change the
735 second character from 'A' to 'B'), a period, the name of the
736 function, and the name. So just skip everything before the
738 p = strrchr (name, '.');
744 #endif /* STATIC_TRANSFORM_NAME */
748 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
751 skip_trampoline_code (pc, name)
755 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
757 unsigned long indirect = read_memory_unsigned_integer (pc+2, 4);
758 struct minimal_symbol *indsym =
759 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
760 char *symname = indsym ? SYMBOL_NAME(indsym) : 0;
764 if (strncmp (symname,"__imp_", 6) == 0
765 || strncmp (symname,"_imp_", 5) == 0)
766 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
769 return 0; /* not a trampoline */
773 gdb_print_insn_i386 (memaddr, info)
775 disassemble_info * info;
777 if (disassembly_flavor == att_flavor)
778 return print_insn_i386_att (memaddr, info);
779 else if (disassembly_flavor == intel_flavor)
780 return print_insn_i386_intel (memaddr, info);
781 /* Never reached - disassembly_flavour is always either att_flavor
786 /* If the disassembly mode is intel, we have to also switch the
787 bfd mach_type. This function is run in the set disassembly_flavor
788 command, and does that. */
791 set_disassembly_flavor_sfunc (args, from_tty, c)
794 struct cmd_list_element *c;
796 set_disassembly_flavor ();
798 if (disassembly_flavor_hook != NULL)
799 disassembly_flavor_hook(args, from_tty);
803 set_disassembly_flavor ()
805 if (disassembly_flavor == att_flavor)
806 set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386);
807 else if (disassembly_flavor == intel_flavor)
808 set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386_intel_syntax);
812 _initialize_i386_tdep ()
814 struct cmd_list_element *new_cmd;
816 tm_print_insn = gdb_print_insn_i386;
817 tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
819 /* Add the variable that controls the disassembly flavor */
821 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
823 (char *) &disassembly_flavor,
824 "Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
825 and the default value is \"att\".",
827 new_cmd->function.sfunc = set_disassembly_flavor_sfunc;
828 add_show_from_set(new_cmd, &showlist);
830 /* Finally, initialize the disassembly flavor to the default given
831 in the disassembly_flavor variable */
833 set_disassembly_flavor ();