1 /* Intel 386 target-dependent stuff.
3 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002 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 "gdb_assert.h"
39 #include "i386-tdep.h"
41 /* Names of the registers. The first 10 registers match the register
42 numbering scheme used by GCC for stabs and DWARF. */
43 static char *i386_register_names
[] =
45 "eax", "ecx", "edx", "ebx",
46 "esp", "ebp", "esi", "edi",
47 "eip", "eflags", "cs", "ss",
48 "ds", "es", "fs", "gs",
49 "st0", "st1", "st2", "st3",
50 "st4", "st5", "st6", "st7",
51 "fctrl", "fstat", "ftag", "fiseg",
52 "fioff", "foseg", "fooff", "fop",
53 "xmm0", "xmm1", "xmm2", "xmm3",
54 "xmm4", "xmm5", "xmm6", "xmm7",
58 /* i386_register_offset[i] is the offset into the register file of the
59 start of register number i. We initialize this from
60 i386_register_size. */
61 static int i386_register_offset
[I386_SSE_NUM_REGS
];
63 /* i386_register_size[i] is the number of bytes of storage in GDB's
64 register array occupied by register i. */
65 static int i386_register_size
[I386_SSE_NUM_REGS
] = {
79 /* Return the name of register REG. */
82 i386_register_name (int reg
)
86 if (reg
>= sizeof (i386_register_names
) / sizeof (*i386_register_names
))
89 return i386_register_names
[reg
];
92 /* Return the offset into the register array of the start of register
95 i386_register_byte (int reg
)
97 return i386_register_offset
[reg
];
100 /* Return the number of bytes of storage in GDB's register array
101 occupied by register REG. */
104 i386_register_raw_size (int reg
)
106 return i386_register_size
[reg
];
109 /* Convert stabs register number REG to the appropriate register
110 number used by GDB. */
113 i386_stab_reg_to_regnum (int reg
)
115 /* This implements what GCC calls the "default" register map. */
116 if (reg
>= 0 && reg
<= 7)
118 /* General registers. */
121 else if (reg
>= 12 && reg
<= 19)
123 /* Floating-point registers. */
124 return reg
- 12 + FP0_REGNUM
;
126 else if (reg
>= 21 && reg
<= 28)
129 return reg
- 21 + XMM0_REGNUM
;
131 else if (reg
>= 29 && reg
<= 36)
134 /* FIXME: kettenis/2001-07-28: Should we have the MMX registers
135 as pseudo-registers? */
136 return reg
- 29 + FP0_REGNUM
;
139 /* This will hopefully provoke a warning. */
140 return NUM_REGS
+ NUM_PSEUDO_REGS
;
143 /* Convert DWARF register number REG to the appropriate register
144 number used by GDB. */
147 i386_dwarf_reg_to_regnum (int reg
)
149 /* The DWARF register numbering includes %eip and %eflags, and
150 numbers the floating point registers differently. */
151 if (reg
>= 0 && reg
<= 9)
153 /* General registers. */
156 else if (reg
>= 11 && reg
<= 18)
158 /* Floating-point registers. */
159 return reg
- 11 + FP0_REGNUM
;
163 /* The SSE and MMX registers have identical numbers as in stabs. */
164 return i386_stab_reg_to_regnum (reg
);
167 /* This will hopefully provoke a warning. */
168 return NUM_REGS
+ NUM_PSEUDO_REGS
;
172 /* This is the variable that is set with "set disassembly-flavor", and
173 its legitimate values. */
174 static const char att_flavor
[] = "att";
175 static const char intel_flavor
[] = "intel";
176 static const char *valid_flavors
[] =
182 static const char *disassembly_flavor
= att_flavor
;
184 /* Stdio style buffering was used to minimize calls to ptrace, but
185 this buffering did not take into account that the code section
186 being accessed may not be an even number of buffers long (even if
187 the buffer is only sizeof(int) long). In cases where the code
188 section size happened to be a non-integral number of buffers long,
189 attempting to read the last buffer would fail. Simply using
190 target_read_memory and ignoring errors, rather than read_memory, is
191 not the correct solution, since legitimate access errors would then
192 be totally ignored. To properly handle this situation and continue
193 to use buffering would require that this code be able to determine
194 the minimum code section size granularity (not the alignment of the
195 section itself, since the actual failing case that pointed out this
196 problem had a section alignment of 4 but was not a multiple of 4
197 bytes long), on a target by target basis, and then adjust it's
198 buffer size accordingly. This is messy, but potentially feasible.
199 It probably needs the bfd library's help and support. For now, the
200 buffer size is set to 1. (FIXME -fnf) */
202 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
203 static CORE_ADDR codestream_next_addr
;
204 static CORE_ADDR codestream_addr
;
205 static unsigned char codestream_buf
[CODESTREAM_BUFSIZ
];
206 static int codestream_off
;
207 static int codestream_cnt
;
209 #define codestream_tell() (codestream_addr + codestream_off)
210 #define codestream_peek() \
211 (codestream_cnt == 0 ? \
212 codestream_fill(1) : codestream_buf[codestream_off])
213 #define codestream_get() \
214 (codestream_cnt-- == 0 ? \
215 codestream_fill(0) : codestream_buf[codestream_off++])
218 codestream_fill (int peek_flag
)
220 codestream_addr
= codestream_next_addr
;
221 codestream_next_addr
+= CODESTREAM_BUFSIZ
;
223 codestream_cnt
= CODESTREAM_BUFSIZ
;
224 read_memory (codestream_addr
, (char *) codestream_buf
, CODESTREAM_BUFSIZ
);
227 return (codestream_peek ());
229 return (codestream_get ());
233 codestream_seek (CORE_ADDR place
)
235 codestream_next_addr
= place
/ CODESTREAM_BUFSIZ
;
236 codestream_next_addr
*= CODESTREAM_BUFSIZ
;
239 while (codestream_tell () != place
)
244 codestream_read (unsigned char *buf
, int count
)
249 for (i
= 0; i
< count
; i
++)
250 *p
++ = codestream_get ();
254 /* If the next instruction is a jump, move to its target. */
257 i386_follow_jump (void)
259 unsigned char buf
[4];
265 pos
= codestream_tell ();
268 if (codestream_peek () == 0x66)
274 switch (codestream_get ())
277 /* Relative jump: if data16 == 0, disp32, else disp16. */
280 codestream_read (buf
, 2);
281 delta
= extract_signed_integer (buf
, 2);
283 /* Include the size of the jmp instruction (including the
289 codestream_read (buf
, 4);
290 delta
= extract_signed_integer (buf
, 4);
296 /* Relative jump, disp8 (ignore data16). */
297 codestream_read (buf
, 1);
298 /* Sign-extend it. */
299 delta
= extract_signed_integer (buf
, 1);
304 codestream_seek (pos
);
307 /* Find & return the amount a local space allocated, and advance the
308 codestream to the first register push (if any).
310 If the entry sequence doesn't make sense, return -1, and leave
311 codestream pointer at a random spot. */
314 i386_get_frame_setup (CORE_ADDR pc
)
318 codestream_seek (pc
);
322 op
= codestream_get ();
324 if (op
== 0x58) /* popl %eax */
326 /* This function must start with
329 xchgl %eax, (%esp) 0x87 0x04 0x24
330 or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
332 (the System V compiler puts out the second `xchg'
333 instruction, and the assembler doesn't try to optimize it, so
334 the 'sib' form gets generated). This sequence is used to get
335 the address of the return buffer for a function that returns
338 unsigned char buf
[4];
339 static unsigned char proto1
[3] = { 0x87, 0x04, 0x24 };
340 static unsigned char proto2
[4] = { 0x87, 0x44, 0x24, 0x00 };
342 pos
= codestream_tell ();
343 codestream_read (buf
, 4);
344 if (memcmp (buf
, proto1
, 3) == 0)
346 else if (memcmp (buf
, proto2
, 4) == 0)
349 codestream_seek (pos
);
350 op
= codestream_get (); /* Update next opcode. */
353 if (op
== 0x68 || op
== 0x6a)
355 /* This function may start with
367 unsigned char buf
[8];
369 /* Skip past the `pushl' instruction; it has either a one-byte
370 or a four-byte operand, depending on the opcode. */
371 pos
= codestream_tell ();
376 codestream_seek (pos
);
378 /* Read the following 8 bytes, which should be "call _probe" (6
379 bytes) followed by "addl $4,%esp" (2 bytes). */
380 codestream_read (buf
, sizeof (buf
));
381 if (buf
[0] == 0xe8 && buf
[6] == 0xc4 && buf
[7] == 0x4)
383 codestream_seek (pos
);
384 op
= codestream_get (); /* Update next opcode. */
387 if (op
== 0x55) /* pushl %ebp */
389 /* Check for "movl %esp, %ebp" -- can be written in two ways. */
390 switch (codestream_get ())
393 if (codestream_get () != 0xec)
397 if (codestream_get () != 0xe5)
403 /* Check for stack adjustment
407 NOTE: You can't subtract a 16 bit immediate from a 32 bit
408 reg, so we don't have to worry about a data16 prefix. */
409 op
= codestream_peek ();
412 /* `subl' with 8 bit immediate. */
414 if (codestream_get () != 0xec)
415 /* Some instruction starting with 0x83 other than `subl'. */
417 codestream_seek (codestream_tell () - 2);
420 /* `subl' with signed byte immediate (though it wouldn't
421 make sense to be negative). */
422 return (codestream_get ());
427 /* Maybe it is `subl' with a 32 bit immedediate. */
429 if (codestream_get () != 0xec)
430 /* Some instruction starting with 0x81 other than `subl'. */
432 codestream_seek (codestream_tell () - 2);
435 /* It is `subl' with a 32 bit immediate. */
436 codestream_read ((unsigned char *) buf
, 4);
437 return extract_signed_integer (buf
, 4);
447 /* `enter' with 16 bit unsigned immediate. */
448 codestream_read ((unsigned char *) buf
, 2);
449 codestream_get (); /* Flush final byte of enter instruction. */
450 return extract_unsigned_integer (buf
, 2);
455 /* Return the chain-pointer for FRAME. In the case of the i386, the
456 frame's nominal address is the address of a 4-byte word containing
457 the calling frame's address. */
460 i386_frame_chain (struct frame_info
*frame
)
462 if (frame
->signal_handler_caller
)
465 if (! inside_entry_file (frame
->pc
))
466 return read_memory_unsigned_integer (frame
->frame
, 4);
471 /* Determine whether the function invocation represented by FRAME does
472 not have a from on the stack associated with it. If it does not,
473 return non-zero, otherwise return zero. */
476 i386_frameless_function_invocation (struct frame_info
*frame
)
478 if (frame
->signal_handler_caller
)
481 return frameless_look_for_prologue (frame
);
484 /* Return the saved program counter for FRAME. */
487 i386_frame_saved_pc (struct frame_info
*frame
)
489 if (frame
->signal_handler_caller
)
491 CORE_ADDR (*sigtramp_saved_pc
) (struct frame_info
*);
492 sigtramp_saved_pc
= gdbarch_tdep (current_gdbarch
)->sigtramp_saved_pc
;
494 gdb_assert (sigtramp_saved_pc
!= NULL
);
495 return sigtramp_saved_pc (frame
);
498 return read_memory_unsigned_integer (frame
->frame
+ 4, 4);
501 /* Immediately after a function call, return the saved pc. */
504 i386_saved_pc_after_call (struct frame_info
*frame
)
506 return read_memory_unsigned_integer (read_register (SP_REGNUM
), 4);
509 /* Return number of args passed to a frame.
510 Can return -1, meaning no way to tell. */
513 i386_frame_num_args (struct frame_info
*fi
)
518 /* This loses because not only might the compiler not be popping the
519 args right after the function call, it might be popping args from
520 both this call and a previous one, and we would say there are
521 more args than there really are. */
525 struct frame_info
*pfi
;
527 /* On the i386, the instruction following the call could be:
529 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
530 anything else - zero args. */
534 frameless
= FRAMELESS_FUNCTION_INVOCATION (fi
);
536 /* In the absence of a frame pointer, GDB doesn't get correct
537 values for nameless arguments. Return -1, so it doesn't print
538 any nameless arguments. */
541 pfi
= get_prev_frame (fi
);
544 /* NOTE: This can happen if we are looking at the frame for
545 main, because FRAME_CHAIN_VALID won't let us go into start.
546 If we have debugging symbols, that's not really a big deal;
547 it just means it will only show as many arguments to main as
554 op
= read_memory_integer (retpc
, 1);
555 if (op
== 0x59) /* pop %ecx */
559 op
= read_memory_integer (retpc
+ 1, 1);
561 /* addl $<signed imm 8 bits>, %esp */
562 return (read_memory_integer (retpc
+ 2, 1) & 0xff) / 4;
566 else if (op
== 0x81) /* `add' with 32 bit immediate. */
568 op
= read_memory_integer (retpc
+ 1, 1);
570 /* addl $<imm 32>, %esp */
571 return read_memory_integer (retpc
+ 2, 4) / 4;
583 /* Parse the first few instructions the function to see what registers
586 We handle these cases:
588 The startup sequence can be at the start of the function, or the
589 function can start with a branch to startup code at the end.
591 %ebp can be set up with either the 'enter' instruction, or "pushl
592 %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
593 once used in the System V compiler).
595 Local space is allocated just below the saved %ebp by either the
596 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a 16
597 bit unsigned argument for space to allocate, and the 'addl'
598 instruction could have either a signed byte, or 32 bit immediate.
600 Next, the registers used by this function are pushed. With the
601 System V compiler they will always be in the order: %edi, %esi,
602 %ebx (and sometimes a harmless bug causes it to also save but not
603 restore %eax); however, the code below is willing to see the pushes
604 in any order, and will handle up to 8 of them.
606 If the setup sequence is at the end of the function, then the next
607 instruction will be a branch back to the start. */
610 i386_frame_init_saved_regs (struct frame_info
*fip
)
614 CORE_ADDR dummy_bottom
;
622 frame_saved_regs_zalloc (fip
);
624 /* If the frame is the end of a dummy, compute where the beginning
626 dummy_bottom
= fip
->frame
- 4 - REGISTER_BYTES
- CALL_DUMMY_LENGTH
;
628 /* Check if the PC points in the stack, in a dummy frame. */
629 if (dummy_bottom
<= fip
->pc
&& fip
->pc
<= fip
->frame
)
631 /* All registers were saved by push_call_dummy. */
633 for (i
= 0; i
< NUM_REGS
; i
++)
635 addr
-= REGISTER_RAW_SIZE (i
);
636 fip
->saved_regs
[i
] = addr
;
641 pc
= get_pc_function_start (fip
->pc
);
643 locals
= i386_get_frame_setup (pc
);
647 addr
= fip
->frame
- 4 - locals
;
648 for (i
= 0; i
< 8; i
++)
650 op
= codestream_get ();
651 if (op
< 0x50 || op
> 0x57)
653 #ifdef I386_REGNO_TO_SYMMETRY
654 /* Dynix uses different internal numbering. Ick. */
655 fip
->saved_regs
[I386_REGNO_TO_SYMMETRY (op
- 0x50)] = addr
;
657 fip
->saved_regs
[op
- 0x50] = addr
;
663 fip
->saved_regs
[PC_REGNUM
] = fip
->frame
+ 4;
664 fip
->saved_regs
[FP_REGNUM
] = fip
->frame
;
667 /* Return PC of first real instruction. */
670 i386_skip_prologue (int pc
)
674 static unsigned char pic_pat
[6] =
675 { 0xe8, 0, 0, 0, 0, /* call 0x0 */
676 0x5b, /* popl %ebx */
680 if (i386_get_frame_setup (pc
) < 0)
683 /* Found valid frame setup -- codestream now points to start of push
684 instructions for saving registers. */
686 /* Skip over register saves. */
687 for (i
= 0; i
< 8; i
++)
689 op
= codestream_peek ();
690 /* Break if not `pushl' instrunction. */
691 if (op
< 0x50 || op
> 0x57)
696 /* The native cc on SVR4 in -K PIC mode inserts the following code
697 to get the address of the global offset table (GOT) into register
702 movl %ebx,x(%ebp) (optional)
705 This code is with the rest of the prologue (at the end of the
706 function), so we have to skip it to get to the first real
707 instruction at the start of the function. */
709 pos
= codestream_tell ();
710 for (i
= 0; i
< 6; i
++)
712 op
= codestream_get ();
713 if (pic_pat
[i
] != op
)
718 unsigned char buf
[4];
721 op
= codestream_get ();
722 if (op
== 0x89) /* movl %ebx, x(%ebp) */
724 op
= codestream_get ();
725 if (op
== 0x5d) /* One byte offset from %ebp. */
728 codestream_read (buf
, 1);
730 else if (op
== 0x9d) /* Four byte offset from %ebp. */
733 codestream_read (buf
, 4);
735 else /* Unexpected instruction. */
737 op
= codestream_get ();
740 if (delta
> 0 && op
== 0x81 && codestream_get () == 0xc3)
745 codestream_seek (pos
);
749 return (codestream_tell ());
753 i386_push_dummy_frame (void)
755 CORE_ADDR sp
= read_register (SP_REGNUM
);
758 char regbuf
[MAX_REGISTER_RAW_SIZE
];
760 sp
= push_word (sp
, read_register (PC_REGNUM
));
761 sp
= push_word (sp
, read_register (FP_REGNUM
));
763 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
765 read_register_gen (regnum
, regbuf
);
766 sp
= push_bytes (sp
, regbuf
, REGISTER_RAW_SIZE (regnum
));
768 write_register (SP_REGNUM
, sp
);
769 write_register (FP_REGNUM
, fp
);
772 /* Insert the (relative) function address into the call sequence
776 i386_fix_call_dummy (char *dummy
, CORE_ADDR pc
, CORE_ADDR fun
, int nargs
,
777 struct value
**args
, struct type
*type
, int gcc_p
)
779 int from
, to
, delta
, loc
;
781 loc
= (int)(read_register (SP_REGNUM
) - CALL_DUMMY_LENGTH
);
786 *((char *)(dummy
) + 1) = (delta
& 0xff);
787 *((char *)(dummy
) + 2) = ((delta
>> 8) & 0xff);
788 *((char *)(dummy
) + 3) = ((delta
>> 16) & 0xff);
789 *((char *)(dummy
) + 4) = ((delta
>> 24) & 0xff);
793 i386_pop_frame (void)
795 struct frame_info
*frame
= get_current_frame ();
798 char regbuf
[MAX_REGISTER_RAW_SIZE
];
800 fp
= FRAME_FP (frame
);
801 i386_frame_init_saved_regs (frame
);
803 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
806 addr
= frame
->saved_regs
[regnum
];
809 read_memory (addr
, regbuf
, REGISTER_RAW_SIZE (regnum
));
810 write_register_bytes (REGISTER_BYTE (regnum
), regbuf
,
811 REGISTER_RAW_SIZE (regnum
));
814 write_register (FP_REGNUM
, read_memory_integer (fp
, 4));
815 write_register (PC_REGNUM
, read_memory_integer (fp
+ 4, 4));
816 write_register (SP_REGNUM
, fp
+ 8);
817 flush_cached_frames ();
821 /* Figure out where the longjmp will land. Slurp the args out of the
822 stack. We expect the first arg to be a pointer to the jmp_buf
823 structure from which we extract the address that we will land at.
824 This address is copied into PC. This routine returns true on
828 i386_get_longjmp_target (CORE_ADDR
*pc
)
831 CORE_ADDR sp
, jb_addr
;
832 int jb_pc_offset
= gdbarch_tdep (current_gdbarch
)->jb_pc_offset
;
834 /* If JB_PC_OFFSET is -1, we have no way to find out where the
835 longjmp will land. */
836 if (jb_pc_offset
== -1)
839 sp
= read_register (SP_REGNUM
);
840 if (target_read_memory (sp
+ 4, buf
, 4))
843 jb_addr
= extract_address (buf
, 4);
844 if (target_read_memory (jb_addr
+ jb_pc_offset
, buf
, 4))
847 *pc
= extract_address (buf
, 4);
853 i386_push_arguments (int nargs
, struct value
**args
, CORE_ADDR sp
,
854 int struct_return
, CORE_ADDR struct_addr
)
856 sp
= default_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
);
863 store_address (buf
, 4, struct_addr
);
864 write_memory (sp
, buf
, 4);
871 i386_store_struct_return (CORE_ADDR addr
, CORE_ADDR sp
)
873 /* Do nothing. Everything was already done by i386_push_arguments. */
876 /* These registers are used for returning integers (and on some
877 targets also for returning `struct' and `union' values when their
878 size and alignment match an integer type). */
879 #define LOW_RETURN_REGNUM 0 /* %eax */
880 #define HIGH_RETURN_REGNUM 2 /* %edx */
882 /* Extract from an array REGBUF containing the (raw) register state, a
883 function return value of TYPE, and copy that, in virtual format,
887 i386_extract_return_value (struct type
*type
, char *regbuf
, char *valbuf
)
889 int len
= TYPE_LENGTH (type
);
891 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
892 && TYPE_NFIELDS (type
) == 1)
894 i386_extract_return_value (TYPE_FIELD_TYPE (type
, 0), regbuf
, valbuf
);
898 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
902 warning ("Cannot find floating-point return value.");
903 memset (valbuf
, 0, len
);
907 /* Floating-point return values can be found in %st(0). Convert
908 its contents to the desired type. This is probably not
909 exactly how it would happen on the target itself, but it is
910 the best we can do. */
911 convert_typed_floating (®buf
[REGISTER_BYTE (FP0_REGNUM
)],
912 builtin_type_i387_ext
, valbuf
, type
);
916 int low_size
= REGISTER_RAW_SIZE (LOW_RETURN_REGNUM
);
917 int high_size
= REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM
);
920 memcpy (valbuf
, ®buf
[REGISTER_BYTE (LOW_RETURN_REGNUM
)], len
);
921 else if (len
<= (low_size
+ high_size
))
924 ®buf
[REGISTER_BYTE (LOW_RETURN_REGNUM
)], low_size
);
925 memcpy (valbuf
+ low_size
,
926 ®buf
[REGISTER_BYTE (HIGH_RETURN_REGNUM
)], len
- low_size
);
929 internal_error (__FILE__
, __LINE__
,
930 "Cannot extract return value of %d bytes long.", len
);
934 /* Write into the appropriate registers a function return value stored
935 in VALBUF of type TYPE, given in virtual format. */
938 i386_store_return_value (struct type
*type
, char *valbuf
)
940 int len
= TYPE_LENGTH (type
);
942 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
943 && TYPE_NFIELDS (type
) == 1)
945 i386_store_return_value (TYPE_FIELD_TYPE (type
, 0), valbuf
);
949 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
952 char buf
[FPU_REG_RAW_SIZE
];
956 warning ("Cannot set floating-point return value.");
960 /* Returning floating-point values is a bit tricky. Apart from
961 storing the return value in %st(0), we have to simulate the
962 state of the FPU at function return point. */
964 /* Convert the value found in VALBUF to the extended
965 floating-point format used by the FPU. This is probably
966 not exactly how it would happen on the target itself, but
967 it is the best we can do. */
968 convert_typed_floating (valbuf
, type
, buf
, builtin_type_i387_ext
);
969 write_register_bytes (REGISTER_BYTE (FP0_REGNUM
), buf
,
972 /* Set the top of the floating-point register stack to 7. The
973 actual value doesn't really matter, but 7 is what a normal
974 function return would end up with if the program started out
975 with a freshly initialized FPU. */
976 fstat
= read_register (FSTAT_REGNUM
);
978 write_register (FSTAT_REGNUM
, fstat
);
980 /* Mark %st(1) through %st(7) as empty. Since we set the top of
981 the floating-point register stack to 7, the appropriate value
982 for the tag word is 0x3fff. */
983 write_register (FTAG_REGNUM
, 0x3fff);
987 int low_size
= REGISTER_RAW_SIZE (LOW_RETURN_REGNUM
);
988 int high_size
= REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM
);
991 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM
), valbuf
, len
);
992 else if (len
<= (low_size
+ high_size
))
994 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM
),
996 write_register_bytes (REGISTER_BYTE (HIGH_RETURN_REGNUM
),
997 valbuf
+ low_size
, len
- low_size
);
1000 internal_error (__FILE__
, __LINE__
,
1001 "Cannot store return value of %d bytes long.", len
);
1005 /* Extract from an array REGBUF containing the (raw) register state
1006 the address in which a function should return its structure value,
1010 i386_extract_struct_value_address (char *regbuf
)
1012 return extract_address (®buf
[REGISTER_BYTE (LOW_RETURN_REGNUM
)],
1013 REGISTER_RAW_SIZE (LOW_RETURN_REGNUM
));
1017 /* This is the variable that is set with "set struct-convention", and
1018 its legitimate values. */
1019 static const char default_struct_convention
[] = "default";
1020 static const char pcc_struct_convention
[] = "pcc";
1021 static const char reg_struct_convention
[] = "reg";
1022 static const char *valid_conventions
[] =
1024 default_struct_convention
,
1025 pcc_struct_convention
,
1026 reg_struct_convention
,
1029 static const char *struct_convention
= default_struct_convention
;
1032 i386_use_struct_convention (int gcc_p
, struct type
*type
)
1034 enum struct_return struct_return
;
1036 if (struct_convention
== default_struct_convention
)
1037 struct_return
= gdbarch_tdep (current_gdbarch
)->struct_return
;
1038 else if (struct_convention
== pcc_struct_convention
)
1039 struct_return
= pcc_struct_return
;
1041 struct_return
= reg_struct_return
;
1043 return generic_use_struct_convention (struct_return
== reg_struct_return
,
1048 /* Return the GDB type object for the "standard" data type of data in
1049 register REGNUM. Perhaps %esi and %edi should go here, but
1050 potentially they could be used for things other than address. */
1053 i386_register_virtual_type (int regnum
)
1055 if (regnum
== PC_REGNUM
|| regnum
== FP_REGNUM
|| regnum
== SP_REGNUM
)
1056 return lookup_pointer_type (builtin_type_void
);
1058 if (IS_FP_REGNUM (regnum
))
1059 return builtin_type_i387_ext
;
1061 if (IS_SSE_REGNUM (regnum
))
1062 return builtin_type_vec128i
;
1064 return builtin_type_int
;
1067 /* Return true iff register REGNUM's virtual format is different from
1068 its raw format. Note that this definition assumes that the host
1069 supports IEEE 32-bit floats, since it doesn't say that SSE
1070 registers need conversion. Even if we can't find a counterexample,
1071 this is still sloppy. */
1074 i386_register_convertible (int regnum
)
1076 return IS_FP_REGNUM (regnum
);
1079 /* Convert data from raw format for register REGNUM in buffer FROM to
1080 virtual format with type TYPE in buffer TO. */
1083 i386_register_convert_to_virtual (int regnum
, struct type
*type
,
1084 char *from
, char *to
)
1086 gdb_assert (IS_FP_REGNUM (regnum
));
1088 /* We only support floating-point values. */
1089 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1091 warning ("Cannot convert floating-point register value "
1092 "to non-floating-point type.");
1093 memset (to
, 0, TYPE_LENGTH (type
));
1097 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
1098 the extended floating-point format used by the FPU. */
1099 convert_typed_floating (from
, builtin_type_i387_ext
, to
, type
);
1102 /* Convert data from virtual format with type TYPE in buffer FROM to
1103 raw format for register REGNUM in buffer TO. */
1106 i386_register_convert_to_raw (struct type
*type
, int regnum
,
1107 char *from
, char *to
)
1109 gdb_assert (IS_FP_REGNUM (regnum
));
1111 /* We only support floating-point values. */
1112 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1114 warning ("Cannot convert non-floating-point type "
1115 "to floating-point register value.");
1116 memset (to
, 0, TYPE_LENGTH (type
));
1120 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
1121 to the extended floating-point format used by the FPU. */
1122 convert_typed_floating (from
, type
, to
, builtin_type_i387_ext
);
1126 #ifdef STATIC_TRANSFORM_NAME
1127 /* SunPRO encodes the static variables. This is not related to C++
1128 mangling, it is done for C too. */
1131 sunpro_static_transform_name (char *name
)
1134 if (IS_STATIC_TRANSFORM_NAME (name
))
1136 /* For file-local statics there will be a period, a bunch of
1137 junk (the contents of which match a string given in the
1138 N_OPT), a period and the name. For function-local statics
1139 there will be a bunch of junk (which seems to change the
1140 second character from 'A' to 'B'), a period, the name of the
1141 function, and the name. So just skip everything before the
1143 p
= strrchr (name
, '.');
1149 #endif /* STATIC_TRANSFORM_NAME */
1152 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
1155 skip_trampoline_code (CORE_ADDR pc
, char *name
)
1157 if (pc
&& read_memory_unsigned_integer (pc
, 2) == 0x25ff) /* jmp *(dest) */
1159 unsigned long indirect
= read_memory_unsigned_integer (pc
+ 2, 4);
1160 struct minimal_symbol
*indsym
=
1161 indirect
? lookup_minimal_symbol_by_pc (indirect
) : 0;
1162 char *symname
= indsym
? SYMBOL_NAME (indsym
) : 0;
1166 if (strncmp (symname
, "__imp_", 6) == 0
1167 || strncmp (symname
, "_imp_", 5) == 0)
1168 return name
? 1 : read_memory_unsigned_integer (indirect
, 4);
1171 return 0; /* Not a trampoline. */
1175 /* Return non-zero if PC and NAME show that we are in a signal
1179 i386_pc_in_sigtramp (CORE_ADDR pc
, char *name
)
1181 return (name
&& strcmp ("_sigtramp", name
) == 0);
1185 /* We have two flavours of disassembly. The machinery on this page
1186 deals with switching between those. */
1189 gdb_print_insn_i386 (bfd_vma memaddr
, disassemble_info
*info
)
1191 if (disassembly_flavor
== att_flavor
)
1192 return print_insn_i386_att (memaddr
, info
);
1193 else if (disassembly_flavor
== intel_flavor
)
1194 return print_insn_i386_intel (memaddr
, info
);
1195 /* Never reached -- disassembly_flavour is always either att_flavor
1197 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
1201 /* There are a few i386 architecture variants that differ only
1202 slightly from the generic i386 target. For now, we don't give them
1203 their own source file, but include them here. As a consequence,
1204 they'll always be included. */
1206 /* System V Release 4 (SVR4). */
1209 i386_svr4_pc_in_sigtramp (CORE_ADDR pc
, char *name
)
1211 return (name
&& (strcmp ("_sigreturn", name
) == 0
1212 || strcmp ("_sigacthandler", name
) == 0
1213 || strcmp ("sigvechandler", name
) == 0));
1216 /* Get saved user PC for sigtramp from the pushed ucontext on the
1217 stack for all three variants of SVR4 sigtramps. */
1220 i386_svr4_sigtramp_saved_pc (struct frame_info
*frame
)
1222 CORE_ADDR saved_pc_offset
= 4;
1225 find_pc_partial_function (frame
->pc
, &name
, NULL
, NULL
);
1228 if (strcmp (name
, "_sigreturn") == 0)
1229 saved_pc_offset
= 132 + 14 * 4;
1230 else if (strcmp (name
, "_sigacthandler") == 0)
1231 saved_pc_offset
= 80 + 14 * 4;
1232 else if (strcmp (name
, "sigvechandler") == 0)
1233 saved_pc_offset
= 120 + 14 * 4;
1237 return read_memory_integer (frame
->next
->frame
+ saved_pc_offset
, 4);
1238 return read_memory_integer (read_register (SP_REGNUM
) + saved_pc_offset
, 4);
1245 i386_go32_pc_in_sigtramp (CORE_ADDR pc
, char *name
)
1247 /* DJGPP doesn't have any special frames for signal handlers. */
1255 i386_elf_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1257 /* We typically use stabs-in-ELF with the DWARF register numbering. */
1258 set_gdbarch_stab_reg_to_regnum (gdbarch
, i386_dwarf_reg_to_regnum
);
1261 /* System V Release 4 (SVR4). */
1264 i386_svr4_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1266 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1268 /* System V Release 4 uses ELF. */
1269 i386_elf_init_abi (info
, gdbarch
);
1271 /* FIXME: kettenis/20020511: Why do we override this function here? */
1272 set_gdbarch_frame_chain_valid (gdbarch
, func_frame_chain_valid
);
1274 set_gdbarch_pc_in_sigtramp (gdbarch
, i386_svr4_pc_in_sigtramp
);
1275 tdep
->sigtramp_saved_pc
= i386_svr4_sigtramp_saved_pc
;
1277 tdep
->jb_pc_offset
= 20;
1283 i386_go32_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1285 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1287 set_gdbarch_pc_in_sigtramp (gdbarch
, i386_go32_pc_in_sigtramp
);
1289 tdep
->jb_pc_offset
= 36;
1295 i386_nw_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1297 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1299 /* FIXME: kettenis/20020511: Why do we override this function here? */
1300 set_gdbarch_frame_chain_valid (gdbarch
, func_frame_chain_valid
);
1302 tdep
->jb_pc_offset
= 24;
1307 i386_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
1309 struct gdbarch_tdep
*tdep
;
1310 struct gdbarch
*gdbarch
;
1311 enum gdb_osabi osabi
= GDB_OSABI_UNKNOWN
;
1313 /* Try to determine the OS ABI of the object we're loading. */
1314 if (info
.abfd
!= NULL
)
1315 osabi
= gdbarch_lookup_osabi (info
.abfd
);
1317 /* Find a candidate among extant architectures. */
1318 for (arches
= gdbarch_list_lookup_by_info (arches
, &info
);
1320 arches
= gdbarch_list_lookup_by_info (arches
->next
, &info
))
1322 /* Make sure the OS ABI selection matches. */
1323 tdep
= gdbarch_tdep (arches
->gdbarch
);
1324 if (tdep
&& tdep
->osabi
== osabi
)
1325 return arches
->gdbarch
;
1328 /* Allocate space for the new architecture. */
1329 tdep
= XMALLOC (struct gdbarch_tdep
);
1330 gdbarch
= gdbarch_alloc (&info
, tdep
);
1332 tdep
->osabi
= osabi
;
1334 /* The i386 default settings don't include the SSE registers.
1335 FIXME: kettenis/20020614: They do include the FPU registers for
1336 now, which probably is not quite right. */
1337 tdep
->num_xmm_regs
= 0;
1339 tdep
->jb_pc_offset
= -1;
1340 tdep
->struct_return
= pcc_struct_return
;
1341 tdep
->sigtramp_saved_pc
= NULL
;
1342 tdep
->sigtramp_start
= 0;
1343 tdep
->sigtramp_end
= 0;
1344 tdep
->sc_pc_offset
= -1;
1346 /* The format used for `long double' on almost all i386 targets is
1347 the i387 extended floating-point format. In fact, of all targets
1348 in the GCC 2.95 tree, only OSF/1 does it different, and insists
1349 on having a `long double' that's not `long' at all. */
1350 set_gdbarch_long_double_format (gdbarch
, &floatformat_i387_ext
);
1352 /* Although the i386 extended floating-point has only 80 significant
1353 bits, a `long double' actually takes up 96, probably to enforce
1355 set_gdbarch_long_double_bit (gdbarch
, 96);
1357 /* NOTE: tm-i386aix.h, tm-i386bsd.h, tm-i386os9k.h, tm-ptx.h,
1358 tm-symmetry.h currently override this. Sigh. */
1359 set_gdbarch_num_regs (gdbarch
, I386_NUM_GREGS
+ I386_NUM_FREGS
);
1361 set_gdbarch_sp_regnum (gdbarch
, 4);
1362 set_gdbarch_fp_regnum (gdbarch
, 5);
1363 set_gdbarch_pc_regnum (gdbarch
, 8);
1364 set_gdbarch_ps_regnum (gdbarch
, 9);
1365 set_gdbarch_fp0_regnum (gdbarch
, 16);
1367 /* Use the "default" register numbering scheme for stabs and COFF. */
1368 set_gdbarch_stab_reg_to_regnum (gdbarch
, i386_stab_reg_to_regnum
);
1369 set_gdbarch_sdb_reg_to_regnum (gdbarch
, i386_stab_reg_to_regnum
);
1371 /* Use the DWARF register numbering scheme for DWARF and DWARF 2. */
1372 set_gdbarch_dwarf_reg_to_regnum (gdbarch
, i386_dwarf_reg_to_regnum
);
1373 set_gdbarch_dwarf2_reg_to_regnum (gdbarch
, i386_dwarf_reg_to_regnum
);
1375 /* We don't define ECOFF_REG_TO_REGNUM, since ECOFF doesn't seem to
1376 be in use on any of the supported i386 targets. */
1378 set_gdbarch_register_name (gdbarch
, i386_register_name
);
1379 set_gdbarch_register_size (gdbarch
, 4);
1380 set_gdbarch_register_bytes (gdbarch
, I386_SIZEOF_GREGS
+ I386_SIZEOF_FREGS
);
1381 set_gdbarch_register_byte (gdbarch
, i386_register_byte
);
1382 set_gdbarch_register_raw_size (gdbarch
, i386_register_raw_size
);
1383 set_gdbarch_max_register_raw_size (gdbarch
, 16);
1384 set_gdbarch_max_register_virtual_size (gdbarch
, 16);
1386 set_gdbarch_get_longjmp_target (gdbarch
, i386_get_longjmp_target
);
1388 set_gdbarch_use_generic_dummy_frames (gdbarch
, 0);
1390 /* Call dummy code. */
1391 set_gdbarch_call_dummy_location (gdbarch
, ON_STACK
);
1392 set_gdbarch_call_dummy_breakpoint_offset (gdbarch
, 5);
1393 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch
, 1);
1394 set_gdbarch_call_dummy_p (gdbarch
, 1);
1395 set_gdbarch_call_dummy_stack_adjust_p (gdbarch
, 0);
1397 set_gdbarch_get_saved_register (gdbarch
, generic_get_saved_register
);
1398 set_gdbarch_push_arguments (gdbarch
, i386_push_arguments
);
1400 set_gdbarch_pc_in_call_dummy (gdbarch
, pc_in_call_dummy_on_stack
);
1402 set_gdbarch_use_struct_convention (gdbarch
, i386_use_struct_convention
);
1404 /* The following redefines make backtracing through sigtramp work.
1405 They manufacture a fake sigtramp frame and obtain the saved pc in
1406 sigtramp from the sigcontext structure which is pushed by the
1407 kernel on the user stack, along with a pointer to it. */
1409 set_gdbarch_frame_chain (gdbarch
, i386_frame_chain
);
1410 set_gdbarch_frame_chain_valid (gdbarch
, file_frame_chain_valid
);
1411 set_gdbarch_frame_saved_pc (gdbarch
, i386_frame_saved_pc
);
1412 set_gdbarch_saved_pc_after_call (gdbarch
, i386_saved_pc_after_call
);
1413 set_gdbarch_pc_in_sigtramp (gdbarch
, i386_pc_in_sigtramp
);
1415 /* Hook in ABI-specific overrides, if they have been registered. */
1416 gdbarch_init_osabi (info
, gdbarch
, osabi
);
1421 static enum gdb_osabi
1422 i386_coff_osabi_sniffer (bfd
*abfd
)
1424 if (strcmp (bfd_get_target (abfd
), "coff-go32-exe") == 0)
1425 return GDB_OSABI_GO32
;
1427 return GDB_OSABI_UNKNOWN
;
1430 static enum gdb_osabi
1431 i386_nlm_osabi_sniffer (bfd
*abfd
)
1433 return GDB_OSABI_NETWARE
;
1437 /* Provide a prototype to silence -Wmissing-prototypes. */
1438 void _initialize_i386_tdep (void);
1441 _initialize_i386_tdep (void)
1443 register_gdbarch_init (bfd_arch_i386
, i386_gdbarch_init
);
1445 /* Initialize the table saying where each register starts in the
1451 for (i
= 0; i
< I386_SSE_NUM_REGS
; i
++)
1453 i386_register_offset
[i
] = offset
;
1454 offset
+= i386_register_size
[i
];
1458 tm_print_insn
= gdb_print_insn_i386
;
1459 tm_print_insn_info
.mach
= bfd_lookup_arch (bfd_arch_i386
, 0)->mach
;
1461 /* Add the variable that controls the disassembly flavor. */
1463 struct cmd_list_element
*new_cmd
;
1465 new_cmd
= add_set_enum_cmd ("disassembly-flavor", no_class
,
1467 &disassembly_flavor
,
1469 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1470 and the default value is \"att\".",
1472 add_show_from_set (new_cmd
, &showlist
);
1475 /* Add the variable that controls the convention for returning
1478 struct cmd_list_element
*new_cmd
;
1480 new_cmd
= add_set_enum_cmd ("struct-convention", no_class
,
1482 &struct_convention
, "\
1483 Set the convention for returning small structs, valid values \
1484 are \"default\", \"pcc\" and \"reg\", and the default value is \"default\".",
1486 add_show_from_set (new_cmd
, &showlist
);
1489 gdbarch_register_osabi_sniffer (bfd_arch_i386
, bfd_target_coff_flavour
,
1490 i386_coff_osabi_sniffer
);
1491 gdbarch_register_osabi_sniffer (bfd_arch_i386
, bfd_target_nlm_flavour
,
1492 i386_nlm_osabi_sniffer
);
1494 gdbarch_register_osabi (bfd_arch_i386
, GDB_OSABI_SVR4
,
1495 i386_svr4_init_abi
);
1496 gdbarch_register_osabi (bfd_arch_i386
, GDB_OSABI_GO32
,
1497 i386_go32_init_abi
);
1498 gdbarch_register_osabi (bfd_arch_i386
, GDB_OSABI_NETWARE
,