1 /* Intel 386 target-dependent stuff.
2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
29 #include "floatformat.h"
33 #include "arch-utils.h"
37 #include "i386-tdep.h"
38 #include "gdb_assert.h"
41 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
43 /* Names of the registers. The first 10 registers match the register
44 numbering scheme used by GCC for stabs and DWARF. */
45 static char *i386_register_names[] =
47 "eax", "ecx", "edx", "ebx",
48 "esp", "ebp", "esi", "edi",
49 "eip", "eflags", "cs", "ss",
50 "ds", "es", "fs", "gs",
51 "st0", "st1", "st2", "st3",
52 "st4", "st5", "st6", "st7",
53 "fctrl", "fstat", "ftag", "fiseg",
54 "fioff", "foseg", "fooff", "fop",
55 "xmm0", "xmm1", "xmm2", "xmm3",
56 "xmm4", "xmm5", "xmm6", "xmm7",
60 /* i386_register_offset[i] is the offset into the register file of the
61 start of register number i. We initialize this from
62 i386_register_size. */
63 static int i386_register_offset[MAX_NUM_REGS];
65 /* i386_register_size[i] is the number of bytes of storage in GDB's
66 register array occupied by register i. */
67 static int i386_register_size[MAX_NUM_REGS] = {
81 /* Return the name of register REG. */
84 i386_register_name (int reg)
88 if (reg >= sizeof (i386_register_names) / sizeof (*i386_register_names))
91 return i386_register_names[reg];
94 /* Return the offset into the register array of the start of register
97 i386_register_byte (int reg)
99 return i386_register_offset[reg];
102 /* Return the number of bytes of storage in GDB's register array
103 occupied by register REG. */
106 i386_register_raw_size (int reg)
108 return i386_register_size[reg];
111 /* Return the size in bytes of the virtual type of register REG. */
114 i386_register_virtual_size (int reg)
116 return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (reg));
119 /* Convert stabs register number REG to the appropriate register
120 number used by GDB. */
123 i386_stab_reg_to_regnum (int reg)
125 /* This implements what GCC calls the "default" register map. */
126 if (reg >= 0 && reg <= 7)
128 /* General registers. */
131 else if (reg >= 12 && reg <= 19)
133 /* Floating-point registers. */
134 return reg - 12 + FP0_REGNUM;
136 else if (reg >= 21 && reg <= 28)
139 return reg - 21 + XMM0_REGNUM;
141 else if (reg >= 29 && reg <= 36)
144 /* FIXME: kettenis/2001-07-28: Should we have the MMX registers
145 as pseudo-registers? */
146 return reg - 29 + FP0_REGNUM;
149 /* This will hopefully provoke a warning. */
150 return NUM_REGS + NUM_PSEUDO_REGS;
153 /* Convert Dwarf register number REG to the appropriate register
154 number used by GDB. */
157 i386_dwarf_reg_to_regnum (int reg)
159 /* The DWARF register numbering includes %eip and %eflags, and
160 numbers the floating point registers differently. */
161 if (reg >= 0 && reg <= 9)
163 /* General registers. */
166 else if (reg >= 11 && reg <= 18)
168 /* Floating-point registers. */
169 return reg - 11 + FP0_REGNUM;
173 /* The SSE and MMX registers have identical numbers as in stabs. */
174 return i386_stab_reg_to_regnum (reg);
177 /* This will hopefully provoke a warning. */
178 return NUM_REGS + NUM_PSEUDO_REGS;
182 /* This is the variable that is set with "set disassembly-flavor", and
183 its legitimate values. */
184 static const char att_flavor[] = "att";
185 static const char intel_flavor[] = "intel";
186 static const char *valid_flavors[] =
192 static const char *disassembly_flavor = att_flavor;
194 /* Stdio style buffering was used to minimize calls to ptrace, but
195 this buffering did not take into account that the code section
196 being accessed may not be an even number of buffers long (even if
197 the buffer is only sizeof(int) long). In cases where the code
198 section size happened to be a non-integral number of buffers long,
199 attempting to read the last buffer would fail. Simply using
200 target_read_memory and ignoring errors, rather than read_memory, is
201 not the correct solution, since legitimate access errors would then
202 be totally ignored. To properly handle this situation and continue
203 to use buffering would require that this code be able to determine
204 the minimum code section size granularity (not the alignment of the
205 section itself, since the actual failing case that pointed out this
206 problem had a section alignment of 4 but was not a multiple of 4
207 bytes long), on a target by target basis, and then adjust it's
208 buffer size accordingly. This is messy, but potentially feasible.
209 It probably needs the bfd library's help and support. For now, the
210 buffer size is set to 1. (FIXME -fnf) */
212 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
213 static CORE_ADDR codestream_next_addr;
214 static CORE_ADDR codestream_addr;
215 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
216 static int codestream_off;
217 static int codestream_cnt;
219 #define codestream_tell() (codestream_addr + codestream_off)
220 #define codestream_peek() \
221 (codestream_cnt == 0 ? \
222 codestream_fill(1) : codestream_buf[codestream_off])
223 #define codestream_get() \
224 (codestream_cnt-- == 0 ? \
225 codestream_fill(0) : codestream_buf[codestream_off++])
228 codestream_fill (int peek_flag)
230 codestream_addr = codestream_next_addr;
231 codestream_next_addr += CODESTREAM_BUFSIZ;
233 codestream_cnt = CODESTREAM_BUFSIZ;
234 read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
237 return (codestream_peek ());
239 return (codestream_get ());
243 codestream_seek (CORE_ADDR place)
245 codestream_next_addr = place / CODESTREAM_BUFSIZ;
246 codestream_next_addr *= CODESTREAM_BUFSIZ;
249 while (codestream_tell () != place)
254 codestream_read (unsigned char *buf, int count)
259 for (i = 0; i < count; i++)
260 *p++ = codestream_get ();
264 /* If the next instruction is a jump, move to its target. */
267 i386_follow_jump (void)
269 unsigned char buf[4];
275 pos = codestream_tell ();
278 if (codestream_peek () == 0x66)
284 switch (codestream_get ())
287 /* Relative jump: if data16 == 0, disp32, else disp16. */
290 codestream_read (buf, 2);
291 delta = extract_signed_integer (buf, 2);
293 /* Include the size of the jmp instruction (including the
299 codestream_read (buf, 4);
300 delta = extract_signed_integer (buf, 4);
306 /* Relative jump, disp8 (ignore data16). */
307 codestream_read (buf, 1);
308 /* Sign-extend it. */
309 delta = extract_signed_integer (buf, 1);
314 codestream_seek (pos);
317 /* Find & return the amount a local space allocated, and advance the
318 codestream to the first register push (if any).
320 If the entry sequence doesn't make sense, return -1, and leave
321 codestream pointer at a random spot. */
324 i386_get_frame_setup (CORE_ADDR pc)
328 codestream_seek (pc);
332 op = codestream_get ();
334 if (op == 0x58) /* popl %eax */
336 /* This function must start with
339 xchgl %eax, (%esp) 0x87 0x04 0x24
340 or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
342 (the System V compiler puts out the second `xchg'
343 instruction, and the assembler doesn't try to optimize it, so
344 the 'sib' form gets generated). This sequence is used to get
345 the address of the return buffer for a function that returns
348 unsigned char buf[4];
349 static unsigned char proto1[3] = { 0x87, 0x04, 0x24 };
350 static unsigned char proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
352 pos = codestream_tell ();
353 codestream_read (buf, 4);
354 if (memcmp (buf, proto1, 3) == 0)
356 else if (memcmp (buf, proto2, 4) == 0)
359 codestream_seek (pos);
360 op = codestream_get (); /* Update next opcode. */
363 if (op == 0x68 || op == 0x6a)
365 /* This function may start with
377 unsigned char buf[8];
379 /* Skip past the `pushl' instruction; it has either a one-byte
380 or a four-byte operand, depending on the opcode. */
381 pos = codestream_tell ();
386 codestream_seek (pos);
388 /* Read the following 8 bytes, which should be "call _probe" (6
389 bytes) followed by "addl $4,%esp" (2 bytes). */
390 codestream_read (buf, sizeof (buf));
391 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
393 codestream_seek (pos);
394 op = codestream_get (); /* Update next opcode. */
397 if (op == 0x55) /* pushl %ebp */
399 /* Check for "movl %esp, %ebp" -- can be written in two ways. */
400 switch (codestream_get ())
403 if (codestream_get () != 0xec)
407 if (codestream_get () != 0xe5)
413 /* Check for stack adjustment
417 NOTE: You can't subtract a 16 bit immediate from a 32 bit
418 reg, so we don't have to worry about a data16 prefix. */
419 op = codestream_peek ();
422 /* `subl' with 8 bit immediate. */
424 if (codestream_get () != 0xec)
425 /* Some instruction starting with 0x83 other than `subl'. */
427 codestream_seek (codestream_tell () - 2);
430 /* `subl' with signed byte immediate (though it wouldn't
431 make sense to be negative). */
432 return (codestream_get ());
437 /* Maybe it is `subl' with a 32 bit immedediate. */
439 if (codestream_get () != 0xec)
440 /* Some instruction starting with 0x81 other than `subl'. */
442 codestream_seek (codestream_tell () - 2);
445 /* It is `subl' with a 32 bit immediate. */
446 codestream_read ((unsigned char *) buf, 4);
447 return extract_signed_integer (buf, 4);
457 /* `enter' with 16 bit unsigned immediate. */
458 codestream_read ((unsigned char *) buf, 2);
459 codestream_get (); /* Flush final byte of enter instruction. */
460 return extract_unsigned_integer (buf, 2);
465 /* Return the chain-pointer for FRAME. In the case of the i386, the
466 frame's nominal address is the address of a 4-byte word containing
467 the calling frame's address. */
470 i386_frame_chain (struct frame_info *frame)
472 if (frame->signal_handler_caller)
475 if (! inside_entry_file (frame->pc))
476 return read_memory_unsigned_integer (frame->frame, 4);
481 /* Determine whether the function invocation represented by FRAME does
482 not have a from on the stack associated with it. If it does not,
483 return non-zero, otherwise return zero. */
486 i386_frameless_function_invocation (struct frame_info *frame)
488 if (frame->signal_handler_caller)
491 return frameless_look_for_prologue (frame);
494 /* Return the saved program counter for FRAME. */
497 i386_frame_saved_pc (struct frame_info *frame)
499 /* FIXME: kettenis/2001-05-09: Conditionalizing the next bit of code
500 on SIGCONTEXT_PC_OFFSET and I386V4_SIGTRAMP_SAVED_PC should be
501 considered a temporary hack. I plan to come up with something
502 better when we go multi-arch. */
503 #if defined (SIGCONTEXT_PC_OFFSET) || defined (I386V4_SIGTRAMP_SAVED_PC)
504 if (frame->signal_handler_caller)
505 return sigtramp_saved_pc (frame);
508 return read_memory_unsigned_integer (frame->frame + 4, 4);
512 i386go32_frame_saved_pc (struct frame_info *frame)
514 return read_memory_integer (frame->frame + 4, 4);
517 /* Immediately after a function call, return the saved pc. */
520 i386_saved_pc_after_call (struct frame_info *frame)
522 return read_memory_unsigned_integer (read_register (SP_REGNUM), 4);
525 /* Return number of args passed to a frame.
526 Can return -1, meaning no way to tell. */
529 i386_frame_num_args (struct frame_info *fi)
534 /* This loses because not only might the compiler not be popping the
535 args right after the function call, it might be popping args from
536 both this call and a previous one, and we would say there are
537 more args than there really are. */
541 struct frame_info *pfi;
543 /* On the i386, the instruction following the call could be:
545 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
546 anything else - zero args. */
550 frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
552 /* In the absence of a frame pointer, GDB doesn't get correct
553 values for nameless arguments. Return -1, so it doesn't print
554 any nameless arguments. */
557 pfi = get_prev_frame (fi);
560 /* NOTE: This can happen if we are looking at the frame for
561 main, because FRAME_CHAIN_VALID won't let us go into start.
562 If we have debugging symbols, that's not really a big deal;
563 it just means it will only show as many arguments to main as
570 op = read_memory_integer (retpc, 1);
571 if (op == 0x59) /* pop %ecx */
575 op = read_memory_integer (retpc + 1, 1);
577 /* addl $<signed imm 8 bits>, %esp */
578 return (read_memory_integer (retpc + 2, 1) & 0xff) / 4;
582 else if (op == 0x81) /* `add' with 32 bit immediate. */
584 op = read_memory_integer (retpc + 1, 1);
586 /* addl $<imm 32>, %esp */
587 return read_memory_integer (retpc + 2, 4) / 4;
599 /* Parse the first few instructions the function to see what registers
602 We handle these cases:
604 The startup sequence can be at the start of the function, or the
605 function can start with a branch to startup code at the end.
607 %ebp can be set up with either the 'enter' instruction, or "pushl
608 %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
609 once used in the System V compiler).
611 Local space is allocated just below the saved %ebp by either the
612 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a 16
613 bit unsigned argument for space to allocate, and the 'addl'
614 instruction could have either a signed byte, or 32 bit immediate.
616 Next, the registers used by this function are pushed. With the
617 System V compiler they will always be in the order: %edi, %esi,
618 %ebx (and sometimes a harmless bug causes it to also save but not
619 restore %eax); however, the code below is willing to see the pushes
620 in any order, and will handle up to 8 of them.
622 If the setup sequence is at the end of the function, then the next
623 instruction will be a branch back to the start. */
626 i386_frame_init_saved_regs (struct frame_info *fip)
630 CORE_ADDR dummy_bottom;
638 frame_saved_regs_zalloc (fip);
640 /* If the frame is the end of a dummy, compute where the beginning
642 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
644 /* Check if the PC points in the stack, in a dummy frame. */
645 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
647 /* All registers were saved by push_call_dummy. */
649 for (i = 0; i < NUM_REGS; i++)
651 addr -= REGISTER_RAW_SIZE (i);
652 fip->saved_regs[i] = addr;
657 pc = get_pc_function_start (fip->pc);
659 locals = i386_get_frame_setup (pc);
663 addr = fip->frame - 4 - locals;
664 for (i = 0; i < 8; i++)
666 op = codestream_get ();
667 if (op < 0x50 || op > 0x57)
669 #ifdef I386_REGNO_TO_SYMMETRY
670 /* Dynix uses different internal numbering. Ick. */
671 fip->saved_regs[I386_REGNO_TO_SYMMETRY (op - 0x50)] = addr;
673 fip->saved_regs[op - 0x50] = addr;
679 fip->saved_regs[PC_REGNUM] = fip->frame + 4;
680 fip->saved_regs[FP_REGNUM] = fip->frame;
683 /* Return PC of first real instruction. */
686 i386_skip_prologue (int pc)
690 static unsigned char pic_pat[6] =
691 { 0xe8, 0, 0, 0, 0, /* call 0x0 */
692 0x5b, /* popl %ebx */
696 if (i386_get_frame_setup (pc) < 0)
699 /* Found valid frame setup -- codestream now points to start of push
700 instructions for saving registers. */
702 /* Skip over register saves. */
703 for (i = 0; i < 8; i++)
705 op = codestream_peek ();
706 /* Break if not `pushl' instrunction. */
707 if (op < 0x50 || op > 0x57)
712 /* The native cc on SVR4 in -K PIC mode inserts the following code
713 to get the address of the global offset table (GOT) into register
718 movl %ebx,x(%ebp) (optional)
721 This code is with the rest of the prologue (at the end of the
722 function), so we have to skip it to get to the first real
723 instruction at the start of the function. */
725 pos = codestream_tell ();
726 for (i = 0; i < 6; i++)
728 op = codestream_get ();
729 if (pic_pat[i] != op)
734 unsigned char buf[4];
737 op = codestream_get ();
738 if (op == 0x89) /* movl %ebx, x(%ebp) */
740 op = codestream_get ();
741 if (op == 0x5d) /* One byte offset from %ebp. */
744 codestream_read (buf, 1);
746 else if (op == 0x9d) /* Four byte offset from %ebp. */
749 codestream_read (buf, 4);
751 else /* Unexpected instruction. */
753 op = codestream_get ();
756 if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
761 codestream_seek (pos);
765 return (codestream_tell ());
769 i386_push_dummy_frame (void)
771 CORE_ADDR sp = read_register (SP_REGNUM);
773 char regbuf[MAX_REGISTER_RAW_SIZE];
775 sp = push_word (sp, read_register (PC_REGNUM));
776 sp = push_word (sp, read_register (FP_REGNUM));
777 write_register (FP_REGNUM, sp);
778 for (regnum = 0; regnum < NUM_REGS; regnum++)
780 read_register_gen (regnum, regbuf);
781 sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
783 write_register (SP_REGNUM, sp);
786 /* Insert the (relative) function address into the call sequence
790 i386_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
791 struct value **args, struct type *type, int gcc_p)
793 int from, to, delta, loc;
795 loc = (int)(read_register (SP_REGNUM) - CALL_DUMMY_LENGTH);
800 *((char *)(dummy) + 1) = (delta & 0xff);
801 *((char *)(dummy) + 2) = ((delta >> 8) & 0xff);
802 *((char *)(dummy) + 3) = ((delta >> 16) & 0xff);
803 *((char *)(dummy) + 4) = ((delta >> 24) & 0xff);
807 i386_pop_frame (void)
809 struct frame_info *frame = get_current_frame ();
812 char regbuf[MAX_REGISTER_RAW_SIZE];
814 fp = FRAME_FP (frame);
815 i386_frame_init_saved_regs (frame);
817 for (regnum = 0; regnum < NUM_REGS; regnum++)
820 addr = frame->saved_regs[regnum];
823 read_memory (addr, regbuf, REGISTER_RAW_SIZE (regnum));
824 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
825 REGISTER_RAW_SIZE (regnum));
828 write_register (FP_REGNUM, read_memory_integer (fp, 4));
829 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
830 write_register (SP_REGNUM, fp + 8);
831 flush_cached_frames ();
835 #ifdef GET_LONGJMP_TARGET
837 /* Figure out where the longjmp will land. Slurp the args out of the
838 stack. We expect the first arg to be a pointer to the jmp_buf
839 structure from which we extract the pc (JB_PC) that we will land
840 at. The pc is copied into PC. This routine returns true on
844 get_longjmp_target (CORE_ADDR *pc)
846 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
847 CORE_ADDR sp, jb_addr;
849 sp = read_register (SP_REGNUM);
851 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack. */
853 TARGET_PTR_BIT / TARGET_CHAR_BIT))
856 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
858 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
859 TARGET_PTR_BIT / TARGET_CHAR_BIT))
862 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
867 #endif /* GET_LONGJMP_TARGET */
871 i386_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
872 int struct_return, CORE_ADDR struct_addr)
874 sp = default_push_arguments (nargs, args, sp, struct_return, struct_addr);
881 store_address (buf, 4, struct_addr);
882 write_memory (sp, buf, 4);
889 i386_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
891 /* Do nothing. Everything was already done by i386_push_arguments. */
894 /* These registers are used for returning integers (and on some
895 targets also for returning `struct' and `union' values when their
896 size and alignment match an integer type). */
897 #define LOW_RETURN_REGNUM 0 /* %eax */
898 #define HIGH_RETURN_REGNUM 2 /* %edx */
900 /* Extract from an array REGBUF containing the (raw) register state, a
901 function return value of TYPE, and copy that, in virtual format,
905 i386_extract_return_value (struct type *type, char *regbuf, char *valbuf)
907 int len = TYPE_LENGTH (type);
909 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
910 && TYPE_NFIELDS (type) == 1)
912 i386_extract_return_value (TYPE_FIELD_TYPE (type, 0), regbuf, valbuf);
916 if (TYPE_CODE (type) == TYPE_CODE_FLT)
920 warning ("Cannot find floating-point return value.");
921 memset (valbuf, 0, len);
925 /* Floating-point return values can be found in %st(0). Convert
926 its contents to the desired type. This is probably not
927 exactly how it would happen on the target itself, but it is
928 the best we can do. */
929 convert_typed_floating (®buf[REGISTER_BYTE (FP0_REGNUM)],
930 builtin_type_i387_ext, valbuf, type);
934 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
935 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
938 memcpy (valbuf, ®buf[REGISTER_BYTE (LOW_RETURN_REGNUM)], len);
939 else if (len <= (low_size + high_size))
942 ®buf[REGISTER_BYTE (LOW_RETURN_REGNUM)], low_size);
943 memcpy (valbuf + low_size,
944 ®buf[REGISTER_BYTE (HIGH_RETURN_REGNUM)], len - low_size);
947 internal_error (__FILE__, __LINE__,
948 "Cannot extract return value of %d bytes long.", len);
952 /* Write into the appropriate registers a function return value stored
953 in VALBUF of type TYPE, given in virtual format. */
956 i386_store_return_value (struct type *type, char *valbuf)
958 int len = TYPE_LENGTH (type);
960 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
961 && TYPE_NFIELDS (type) == 1)
963 i386_store_return_value (TYPE_FIELD_TYPE (type, 0), valbuf);
967 if (TYPE_CODE (type) == TYPE_CODE_FLT)
970 char buf[FPU_REG_RAW_SIZE];
974 warning ("Cannot set floating-point return value.");
978 /* Returning floating-point values is a bit tricky. Apart from
979 storing the return value in %st(0), we have to simulate the
980 state of the FPU at function return point. */
982 /* Convert the value found in VALBUF to the extended
983 floating-point format used by the FPU. This is probably
984 not exactly how it would happen on the target itself, but
985 it is the best we can do. */
986 convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
987 write_register_bytes (REGISTER_BYTE (FP0_REGNUM), buf,
990 /* Set the top of the floating-point register stack to 7. The
991 actual value doesn't really matter, but 7 is what a normal
992 function return would end up with if the program started out
993 with a freshly initialized FPU. */
994 fstat = read_register (FSTAT_REGNUM);
996 write_register (FSTAT_REGNUM, fstat);
998 /* Mark %st(1) through %st(7) as empty. Since we set the top of
999 the floating-point register stack to 7, the appropriate value
1000 for the tag word is 0x3fff. */
1001 write_register (FTAG_REGNUM, 0x3fff);
1005 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
1006 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
1008 if (len <= low_size)
1009 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM), valbuf, len);
1010 else if (len <= (low_size + high_size))
1012 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM),
1014 write_register_bytes (REGISTER_BYTE (HIGH_RETURN_REGNUM),
1015 valbuf + low_size, len - low_size);
1018 internal_error (__FILE__, __LINE__,
1019 "Cannot store return value of %d bytes long.", len);
1023 /* Extract from an array REGBUF containing the (raw) register state
1024 the address in which a function should return its structure value,
1028 i386_extract_struct_value_address (char *regbuf)
1030 return extract_address (®buf[REGISTER_BYTE (LOW_RETURN_REGNUM)],
1031 REGISTER_RAW_SIZE (LOW_RETURN_REGNUM));
1035 /* Return the GDB type object for the "standard" data type of data in
1036 register REGNUM. Perhaps %esi and %edi should go here, but
1037 potentially they could be used for things other than address. */
1040 i386_register_virtual_type (int regnum)
1042 if (regnum == PC_REGNUM || regnum == FP_REGNUM || regnum == SP_REGNUM)
1043 return lookup_pointer_type (builtin_type_void);
1045 if (IS_FP_REGNUM (regnum))
1046 return builtin_type_i387_ext;
1048 if (IS_SSE_REGNUM (regnum))
1049 return builtin_type_v4sf;
1051 return builtin_type_int;
1054 /* Return true iff register REGNUM's virtual format is different from
1055 its raw format. Note that this definition assumes that the host
1056 supports IEEE 32-bit floats, since it doesn't say that SSE
1057 registers need conversion. Even if we can't find a counterexample,
1058 this is still sloppy. */
1061 i386_register_convertible (int regnum)
1063 return IS_FP_REGNUM (regnum);
1066 /* Convert data from raw format for register REGNUM in buffer FROM to
1067 virtual format with type TYPE in buffer TO. */
1070 i386_register_convert_to_virtual (int regnum, struct type *type,
1071 char *from, char *to)
1073 gdb_assert (IS_FP_REGNUM (regnum));
1075 /* We only support floating-point values. */
1076 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1078 warning ("Cannot convert floating-point register value "
1079 "to non-floating-point type.");
1080 memset (to, 0, TYPE_LENGTH (type));
1084 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
1085 the extended floating-point format used by the FPU. */
1086 convert_typed_floating (from, builtin_type_i387_ext, to, type);
1089 /* Convert data from virtual format with type TYPE in buffer FROM to
1090 raw format for register REGNUM in buffer TO. */
1093 i386_register_convert_to_raw (struct type *type, int regnum,
1094 char *from, char *to)
1096 gdb_assert (IS_FP_REGNUM (regnum));
1098 /* We only support floating-point values. */
1099 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1101 warning ("Cannot convert non-floating-point type "
1102 "to floating-point register value.");
1103 memset (to, 0, TYPE_LENGTH (type));
1107 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
1108 to the extended floating-point format used by the FPU. */
1109 convert_typed_floating (from, type, to, builtin_type_i387_ext);
1113 #ifdef I386V4_SIGTRAMP_SAVED_PC
1114 /* Get saved user PC for sigtramp from the pushed ucontext on the
1115 stack for all three variants of SVR4 sigtramps. */
1118 i386v4_sigtramp_saved_pc (struct frame_info *frame)
1120 CORE_ADDR saved_pc_offset = 4;
1123 find_pc_partial_function (frame->pc, &name, NULL, NULL);
1126 if (STREQ (name, "_sigreturn"))
1127 saved_pc_offset = 132 + 14 * 4;
1128 else if (STREQ (name, "_sigacthandler"))
1129 saved_pc_offset = 80 + 14 * 4;
1130 else if (STREQ (name, "sigvechandler"))
1131 saved_pc_offset = 120 + 14 * 4;
1135 return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
1136 return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
1138 #endif /* I386V4_SIGTRAMP_SAVED_PC */
1141 #ifdef STATIC_TRANSFORM_NAME
1142 /* SunPRO encodes the static variables. This is not related to C++
1143 mangling, it is done for C too. */
1146 sunpro_static_transform_name (char *name)
1149 if (IS_STATIC_TRANSFORM_NAME (name))
1151 /* For file-local statics there will be a period, a bunch of
1152 junk (the contents of which match a string given in the
1153 N_OPT), a period and the name. For function-local statics
1154 there will be a bunch of junk (which seems to change the
1155 second character from 'A' to 'B'), a period, the name of the
1156 function, and the name. So just skip everything before the
1158 p = strrchr (name, '.');
1164 #endif /* STATIC_TRANSFORM_NAME */
1167 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
1170 skip_trampoline_code (CORE_ADDR pc, char *name)
1172 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
1174 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
1175 struct minimal_symbol *indsym =
1176 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
1177 char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
1181 if (strncmp (symname, "__imp_", 6) == 0
1182 || strncmp (symname, "_imp_", 5) == 0)
1183 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
1186 return 0; /* Not a trampoline. */
1190 /* We have two flavours of disassembly. The machinery on this page
1191 deals with switching between those. */
1194 gdb_print_insn_i386 (bfd_vma memaddr, disassemble_info *info)
1196 if (disassembly_flavor == att_flavor)
1197 return print_insn_i386_att (memaddr, info);
1198 else if (disassembly_flavor == intel_flavor)
1199 return print_insn_i386_intel (memaddr, info);
1200 /* Never reached -- disassembly_flavour is always either att_flavor
1202 internal_error (__FILE__, __LINE__, "failed internal consistency check");
1208 i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1210 struct gdbarch_tdep *tdep;
1211 struct gdbarch *gdbarch;
1213 /* For the moment there is only one i386 architecture. */
1215 return arches->gdbarch;
1217 /* Allocate space for the new architecture. */
1218 tdep = XMALLOC (struct gdbarch_tdep);
1219 gdbarch = gdbarch_alloc (&info, tdep);
1221 set_gdbarch_use_generic_dummy_frames (gdbarch, 0);
1223 /* Call dummy code. */
1224 set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1225 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 5);
1226 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1227 set_gdbarch_call_dummy_p (gdbarch, 1);
1228 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1230 set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
1231 set_gdbarch_push_arguments (gdbarch, i386_push_arguments);
1233 set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_on_stack);
1235 /* NOTE: tm-i386nw.h and tm-i386v4.h override this. */
1236 set_gdbarch_frame_chain_valid (gdbarch, file_frame_chain_valid);
1238 /* NOTE: tm-i386aix.h, tm-i386bsd.h, tm-i386os9k.h, tm-linux.h,
1239 tm-ptx.h, tm-symmetry.h currently override this. Sigh. */
1240 set_gdbarch_num_regs (gdbarch, NUM_GREGS + NUM_FREGS + NUM_SSE_REGS);
1245 /* Provide a prototype to silence -Wmissing-prototypes. */
1246 void _initialize_i386_tdep (void);
1249 _initialize_i386_tdep (void)
1251 register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
1253 /* Initialize the table saying where each register starts in the
1259 for (i = 0; i < MAX_NUM_REGS; i++)
1261 i386_register_offset[i] = offset;
1262 offset += i386_register_size[i];
1266 tm_print_insn = gdb_print_insn_i386;
1267 tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
1269 /* Add the variable that controls the disassembly flavor. */
1271 struct cmd_list_element *new_cmd;
1273 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
1275 &disassembly_flavor,
1277 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1278 and the default value is \"att\".",
1280 add_show_from_set (new_cmd, &showlist);