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
[MAX_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
[MAX_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 /* Return the size in bytes of the virtual type of register REG. */
112 i386_register_virtual_size (int reg
)
114 return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (reg
));
117 /* Convert stabs register number REG to the appropriate register
118 number used by GDB. */
121 i386_stab_reg_to_regnum (int reg
)
123 /* This implements what GCC calls the "default" register map. */
124 if (reg
>= 0 && reg
<= 7)
126 /* General registers. */
129 else if (reg
>= 12 && reg
<= 19)
131 /* Floating-point registers. */
132 return reg
- 12 + FP0_REGNUM
;
134 else if (reg
>= 21 && reg
<= 28)
137 return reg
- 21 + XMM0_REGNUM
;
139 else if (reg
>= 29 && reg
<= 36)
142 /* FIXME: kettenis/2001-07-28: Should we have the MMX registers
143 as pseudo-registers? */
144 return reg
- 29 + FP0_REGNUM
;
147 /* This will hopefully provoke a warning. */
148 return NUM_REGS
+ NUM_PSEUDO_REGS
;
151 /* Convert DWARF register number REG to the appropriate register
152 number used by GDB. */
155 i386_dwarf_reg_to_regnum (int reg
)
157 /* The DWARF register numbering includes %eip and %eflags, and
158 numbers the floating point registers differently. */
159 if (reg
>= 0 && reg
<= 9)
161 /* General registers. */
164 else if (reg
>= 11 && reg
<= 18)
166 /* Floating-point registers. */
167 return reg
- 11 + FP0_REGNUM
;
171 /* The SSE and MMX registers have identical numbers as in stabs. */
172 return i386_stab_reg_to_regnum (reg
);
175 /* This will hopefully provoke a warning. */
176 return NUM_REGS
+ NUM_PSEUDO_REGS
;
180 /* This is the variable that is set with "set disassembly-flavor", and
181 its legitimate values. */
182 static const char att_flavor
[] = "att";
183 static const char intel_flavor
[] = "intel";
184 static const char *valid_flavors
[] =
190 static const char *disassembly_flavor
= att_flavor
;
192 /* Stdio style buffering was used to minimize calls to ptrace, but
193 this buffering did not take into account that the code section
194 being accessed may not be an even number of buffers long (even if
195 the buffer is only sizeof(int) long). In cases where the code
196 section size happened to be a non-integral number of buffers long,
197 attempting to read the last buffer would fail. Simply using
198 target_read_memory and ignoring errors, rather than read_memory, is
199 not the correct solution, since legitimate access errors would then
200 be totally ignored. To properly handle this situation and continue
201 to use buffering would require that this code be able to determine
202 the minimum code section size granularity (not the alignment of the
203 section itself, since the actual failing case that pointed out this
204 problem had a section alignment of 4 but was not a multiple of 4
205 bytes long), on a target by target basis, and then adjust it's
206 buffer size accordingly. This is messy, but potentially feasible.
207 It probably needs the bfd library's help and support. For now, the
208 buffer size is set to 1. (FIXME -fnf) */
210 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
211 static CORE_ADDR codestream_next_addr
;
212 static CORE_ADDR codestream_addr
;
213 static unsigned char codestream_buf
[CODESTREAM_BUFSIZ
];
214 static int codestream_off
;
215 static int codestream_cnt
;
217 #define codestream_tell() (codestream_addr + codestream_off)
218 #define codestream_peek() \
219 (codestream_cnt == 0 ? \
220 codestream_fill(1) : codestream_buf[codestream_off])
221 #define codestream_get() \
222 (codestream_cnt-- == 0 ? \
223 codestream_fill(0) : codestream_buf[codestream_off++])
226 codestream_fill (int peek_flag
)
228 codestream_addr
= codestream_next_addr
;
229 codestream_next_addr
+= CODESTREAM_BUFSIZ
;
231 codestream_cnt
= CODESTREAM_BUFSIZ
;
232 read_memory (codestream_addr
, (char *) codestream_buf
, CODESTREAM_BUFSIZ
);
235 return (codestream_peek ());
237 return (codestream_get ());
241 codestream_seek (CORE_ADDR place
)
243 codestream_next_addr
= place
/ CODESTREAM_BUFSIZ
;
244 codestream_next_addr
*= CODESTREAM_BUFSIZ
;
247 while (codestream_tell () != place
)
252 codestream_read (unsigned char *buf
, int count
)
257 for (i
= 0; i
< count
; i
++)
258 *p
++ = codestream_get ();
262 /* If the next instruction is a jump, move to its target. */
265 i386_follow_jump (void)
267 unsigned char buf
[4];
273 pos
= codestream_tell ();
276 if (codestream_peek () == 0x66)
282 switch (codestream_get ())
285 /* Relative jump: if data16 == 0, disp32, else disp16. */
288 codestream_read (buf
, 2);
289 delta
= extract_signed_integer (buf
, 2);
291 /* Include the size of the jmp instruction (including the
297 codestream_read (buf
, 4);
298 delta
= extract_signed_integer (buf
, 4);
304 /* Relative jump, disp8 (ignore data16). */
305 codestream_read (buf
, 1);
306 /* Sign-extend it. */
307 delta
= extract_signed_integer (buf
, 1);
312 codestream_seek (pos
);
315 /* Find & return the amount a local space allocated, and advance the
316 codestream to the first register push (if any).
318 If the entry sequence doesn't make sense, return -1, and leave
319 codestream pointer at a random spot. */
322 i386_get_frame_setup (CORE_ADDR pc
)
326 codestream_seek (pc
);
330 op
= codestream_get ();
332 if (op
== 0x58) /* popl %eax */
334 /* This function must start with
337 xchgl %eax, (%esp) 0x87 0x04 0x24
338 or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
340 (the System V compiler puts out the second `xchg'
341 instruction, and the assembler doesn't try to optimize it, so
342 the 'sib' form gets generated). This sequence is used to get
343 the address of the return buffer for a function that returns
346 unsigned char buf
[4];
347 static unsigned char proto1
[3] = { 0x87, 0x04, 0x24 };
348 static unsigned char proto2
[4] = { 0x87, 0x44, 0x24, 0x00 };
350 pos
= codestream_tell ();
351 codestream_read (buf
, 4);
352 if (memcmp (buf
, proto1
, 3) == 0)
354 else if (memcmp (buf
, proto2
, 4) == 0)
357 codestream_seek (pos
);
358 op
= codestream_get (); /* Update next opcode. */
361 if (op
== 0x68 || op
== 0x6a)
363 /* This function may start with
375 unsigned char buf
[8];
377 /* Skip past the `pushl' instruction; it has either a one-byte
378 or a four-byte operand, depending on the opcode. */
379 pos
= codestream_tell ();
384 codestream_seek (pos
);
386 /* Read the following 8 bytes, which should be "call _probe" (6
387 bytes) followed by "addl $4,%esp" (2 bytes). */
388 codestream_read (buf
, sizeof (buf
));
389 if (buf
[0] == 0xe8 && buf
[6] == 0xc4 && buf
[7] == 0x4)
391 codestream_seek (pos
);
392 op
= codestream_get (); /* Update next opcode. */
395 if (op
== 0x55) /* pushl %ebp */
397 /* Check for "movl %esp, %ebp" -- can be written in two ways. */
398 switch (codestream_get ())
401 if (codestream_get () != 0xec)
405 if (codestream_get () != 0xe5)
411 /* Check for stack adjustment
415 NOTE: You can't subtract a 16 bit immediate from a 32 bit
416 reg, so we don't have to worry about a data16 prefix. */
417 op
= codestream_peek ();
420 /* `subl' with 8 bit immediate. */
422 if (codestream_get () != 0xec)
423 /* Some instruction starting with 0x83 other than `subl'. */
425 codestream_seek (codestream_tell () - 2);
428 /* `subl' with signed byte immediate (though it wouldn't
429 make sense to be negative). */
430 return (codestream_get ());
435 /* Maybe it is `subl' with a 32 bit immedediate. */
437 if (codestream_get () != 0xec)
438 /* Some instruction starting with 0x81 other than `subl'. */
440 codestream_seek (codestream_tell () - 2);
443 /* It is `subl' with a 32 bit immediate. */
444 codestream_read ((unsigned char *) buf
, 4);
445 return extract_signed_integer (buf
, 4);
455 /* `enter' with 16 bit unsigned immediate. */
456 codestream_read ((unsigned char *) buf
, 2);
457 codestream_get (); /* Flush final byte of enter instruction. */
458 return extract_unsigned_integer (buf
, 2);
463 /* Return the chain-pointer for FRAME. In the case of the i386, the
464 frame's nominal address is the address of a 4-byte word containing
465 the calling frame's address. */
468 i386_frame_chain (struct frame_info
*frame
)
470 if (frame
->signal_handler_caller
)
473 if (! inside_entry_file (frame
->pc
))
474 return read_memory_unsigned_integer (frame
->frame
, 4);
479 /* Determine whether the function invocation represented by FRAME does
480 not have a from on the stack associated with it. If it does not,
481 return non-zero, otherwise return zero. */
484 i386_frameless_function_invocation (struct frame_info
*frame
)
486 if (frame
->signal_handler_caller
)
489 return frameless_look_for_prologue (frame
);
492 /* Return the saved program counter for FRAME. */
495 i386_frame_saved_pc (struct frame_info
*frame
)
497 if (frame
->signal_handler_caller
)
499 CORE_ADDR (*sigtramp_saved_pc
) (struct frame_info
*);
500 sigtramp_saved_pc
= gdbarch_tdep (current_gdbarch
)->sigtramp_saved_pc
;
502 gdb_assert (sigtramp_saved_pc
!= NULL
);
503 return sigtramp_saved_pc (frame
);
506 return read_memory_unsigned_integer (frame
->frame
+ 4, 4);
509 /* Immediately after a function call, return the saved pc. */
512 i386_saved_pc_after_call (struct frame_info
*frame
)
514 return read_memory_unsigned_integer (read_register (SP_REGNUM
), 4);
517 /* Return number of args passed to a frame.
518 Can return -1, meaning no way to tell. */
521 i386_frame_num_args (struct frame_info
*fi
)
526 /* This loses because not only might the compiler not be popping the
527 args right after the function call, it might be popping args from
528 both this call and a previous one, and we would say there are
529 more args than there really are. */
533 struct frame_info
*pfi
;
535 /* On the i386, the instruction following the call could be:
537 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
538 anything else - zero args. */
542 frameless
= FRAMELESS_FUNCTION_INVOCATION (fi
);
544 /* In the absence of a frame pointer, GDB doesn't get correct
545 values for nameless arguments. Return -1, so it doesn't print
546 any nameless arguments. */
549 pfi
= get_prev_frame (fi
);
552 /* NOTE: This can happen if we are looking at the frame for
553 main, because FRAME_CHAIN_VALID won't let us go into start.
554 If we have debugging symbols, that's not really a big deal;
555 it just means it will only show as many arguments to main as
562 op
= read_memory_integer (retpc
, 1);
563 if (op
== 0x59) /* pop %ecx */
567 op
= read_memory_integer (retpc
+ 1, 1);
569 /* addl $<signed imm 8 bits>, %esp */
570 return (read_memory_integer (retpc
+ 2, 1) & 0xff) / 4;
574 else if (op
== 0x81) /* `add' with 32 bit immediate. */
576 op
= read_memory_integer (retpc
+ 1, 1);
578 /* addl $<imm 32>, %esp */
579 return read_memory_integer (retpc
+ 2, 4) / 4;
591 /* Parse the first few instructions the function to see what registers
594 We handle these cases:
596 The startup sequence can be at the start of the function, or the
597 function can start with a branch to startup code at the end.
599 %ebp can be set up with either the 'enter' instruction, or "pushl
600 %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
601 once used in the System V compiler).
603 Local space is allocated just below the saved %ebp by either the
604 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a 16
605 bit unsigned argument for space to allocate, and the 'addl'
606 instruction could have either a signed byte, or 32 bit immediate.
608 Next, the registers used by this function are pushed. With the
609 System V compiler they will always be in the order: %edi, %esi,
610 %ebx (and sometimes a harmless bug causes it to also save but not
611 restore %eax); however, the code below is willing to see the pushes
612 in any order, and will handle up to 8 of them.
614 If the setup sequence is at the end of the function, then the next
615 instruction will be a branch back to the start. */
618 i386_frame_init_saved_regs (struct frame_info
*fip
)
622 CORE_ADDR dummy_bottom
;
630 frame_saved_regs_zalloc (fip
);
632 /* If the frame is the end of a dummy, compute where the beginning
634 dummy_bottom
= fip
->frame
- 4 - REGISTER_BYTES
- CALL_DUMMY_LENGTH
;
636 /* Check if the PC points in the stack, in a dummy frame. */
637 if (dummy_bottom
<= fip
->pc
&& fip
->pc
<= fip
->frame
)
639 /* All registers were saved by push_call_dummy. */
641 for (i
= 0; i
< NUM_REGS
; i
++)
643 addr
-= REGISTER_RAW_SIZE (i
);
644 fip
->saved_regs
[i
] = addr
;
649 pc
= get_pc_function_start (fip
->pc
);
651 locals
= i386_get_frame_setup (pc
);
655 addr
= fip
->frame
- 4 - locals
;
656 for (i
= 0; i
< 8; i
++)
658 op
= codestream_get ();
659 if (op
< 0x50 || op
> 0x57)
661 #ifdef I386_REGNO_TO_SYMMETRY
662 /* Dynix uses different internal numbering. Ick. */
663 fip
->saved_regs
[I386_REGNO_TO_SYMMETRY (op
- 0x50)] = addr
;
665 fip
->saved_regs
[op
- 0x50] = addr
;
671 fip
->saved_regs
[PC_REGNUM
] = fip
->frame
+ 4;
672 fip
->saved_regs
[FP_REGNUM
] = fip
->frame
;
675 /* Return PC of first real instruction. */
678 i386_skip_prologue (int pc
)
682 static unsigned char pic_pat
[6] =
683 { 0xe8, 0, 0, 0, 0, /* call 0x0 */
684 0x5b, /* popl %ebx */
688 if (i386_get_frame_setup (pc
) < 0)
691 /* Found valid frame setup -- codestream now points to start of push
692 instructions for saving registers. */
694 /* Skip over register saves. */
695 for (i
= 0; i
< 8; i
++)
697 op
= codestream_peek ();
698 /* Break if not `pushl' instrunction. */
699 if (op
< 0x50 || op
> 0x57)
704 /* The native cc on SVR4 in -K PIC mode inserts the following code
705 to get the address of the global offset table (GOT) into register
710 movl %ebx,x(%ebp) (optional)
713 This code is with the rest of the prologue (at the end of the
714 function), so we have to skip it to get to the first real
715 instruction at the start of the function. */
717 pos
= codestream_tell ();
718 for (i
= 0; i
< 6; i
++)
720 op
= codestream_get ();
721 if (pic_pat
[i
] != op
)
726 unsigned char buf
[4];
729 op
= codestream_get ();
730 if (op
== 0x89) /* movl %ebx, x(%ebp) */
732 op
= codestream_get ();
733 if (op
== 0x5d) /* One byte offset from %ebp. */
736 codestream_read (buf
, 1);
738 else if (op
== 0x9d) /* Four byte offset from %ebp. */
741 codestream_read (buf
, 4);
743 else /* Unexpected instruction. */
745 op
= codestream_get ();
748 if (delta
> 0 && op
== 0x81 && codestream_get () == 0xc3)
753 codestream_seek (pos
);
757 return (codestream_tell ());
761 i386_push_dummy_frame (void)
763 CORE_ADDR sp
= read_register (SP_REGNUM
);
766 char regbuf
[MAX_REGISTER_RAW_SIZE
];
768 sp
= push_word (sp
, read_register (PC_REGNUM
));
769 sp
= push_word (sp
, read_register (FP_REGNUM
));
771 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
773 read_register_gen (regnum
, regbuf
);
774 sp
= push_bytes (sp
, regbuf
, REGISTER_RAW_SIZE (regnum
));
776 write_register (SP_REGNUM
, sp
);
777 write_register (FP_REGNUM
, fp
);
780 /* Insert the (relative) function address into the call sequence
784 i386_fix_call_dummy (char *dummy
, CORE_ADDR pc
, CORE_ADDR fun
, int nargs
,
785 struct value
**args
, struct type
*type
, int gcc_p
)
787 int from
, to
, delta
, loc
;
789 loc
= (int)(read_register (SP_REGNUM
) - CALL_DUMMY_LENGTH
);
794 *((char *)(dummy
) + 1) = (delta
& 0xff);
795 *((char *)(dummy
) + 2) = ((delta
>> 8) & 0xff);
796 *((char *)(dummy
) + 3) = ((delta
>> 16) & 0xff);
797 *((char *)(dummy
) + 4) = ((delta
>> 24) & 0xff);
801 i386_pop_frame (void)
803 struct frame_info
*frame
= get_current_frame ();
806 char regbuf
[MAX_REGISTER_RAW_SIZE
];
808 fp
= FRAME_FP (frame
);
809 i386_frame_init_saved_regs (frame
);
811 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
814 addr
= frame
->saved_regs
[regnum
];
817 read_memory (addr
, regbuf
, REGISTER_RAW_SIZE (regnum
));
818 write_register_bytes (REGISTER_BYTE (regnum
), regbuf
,
819 REGISTER_RAW_SIZE (regnum
));
822 write_register (FP_REGNUM
, read_memory_integer (fp
, 4));
823 write_register (PC_REGNUM
, read_memory_integer (fp
+ 4, 4));
824 write_register (SP_REGNUM
, fp
+ 8);
825 flush_cached_frames ();
829 /* Figure out where the longjmp will land. Slurp the args out of the
830 stack. We expect the first arg to be a pointer to the jmp_buf
831 structure from which we extract the address that we will land at.
832 This address is copied into PC. This routine returns true on
836 i386_get_longjmp_target (CORE_ADDR
*pc
)
839 CORE_ADDR sp
, jb_addr
;
840 int jb_pc_offset
= gdbarch_tdep (current_gdbarch
)->jb_pc_offset
;
842 /* If JB_PC_OFFSET is -1, we have no way to find out where the
843 longjmp will land. */
844 if (jb_pc_offset
== -1)
847 sp
= read_register (SP_REGNUM
);
848 if (target_read_memory (sp
+ 4, buf
, 4))
851 jb_addr
= extract_address (buf
, 4);
852 if (target_read_memory (jb_addr
+ jb_pc_offset
, buf
, 4))
855 *pc
= extract_address (buf
, 4);
861 i386_push_arguments (int nargs
, struct value
**args
, CORE_ADDR sp
,
862 int struct_return
, CORE_ADDR struct_addr
)
864 sp
= default_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
);
871 store_address (buf
, 4, struct_addr
);
872 write_memory (sp
, buf
, 4);
879 i386_store_struct_return (CORE_ADDR addr
, CORE_ADDR sp
)
881 /* Do nothing. Everything was already done by i386_push_arguments. */
884 /* These registers are used for returning integers (and on some
885 targets also for returning `struct' and `union' values when their
886 size and alignment match an integer type). */
887 #define LOW_RETURN_REGNUM 0 /* %eax */
888 #define HIGH_RETURN_REGNUM 2 /* %edx */
890 /* Extract from an array REGBUF containing the (raw) register state, a
891 function return value of TYPE, and copy that, in virtual format,
895 i386_extract_return_value (struct type
*type
, char *regbuf
, char *valbuf
)
897 int len
= TYPE_LENGTH (type
);
899 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
900 && TYPE_NFIELDS (type
) == 1)
902 i386_extract_return_value (TYPE_FIELD_TYPE (type
, 0), regbuf
, valbuf
);
906 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
910 warning ("Cannot find floating-point return value.");
911 memset (valbuf
, 0, len
);
915 /* Floating-point return values can be found in %st(0). Convert
916 its contents to the desired type. This is probably not
917 exactly how it would happen on the target itself, but it is
918 the best we can do. */
919 convert_typed_floating (®buf
[REGISTER_BYTE (FP0_REGNUM
)],
920 builtin_type_i387_ext
, valbuf
, type
);
924 int low_size
= REGISTER_RAW_SIZE (LOW_RETURN_REGNUM
);
925 int high_size
= REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM
);
928 memcpy (valbuf
, ®buf
[REGISTER_BYTE (LOW_RETURN_REGNUM
)], len
);
929 else if (len
<= (low_size
+ high_size
))
932 ®buf
[REGISTER_BYTE (LOW_RETURN_REGNUM
)], low_size
);
933 memcpy (valbuf
+ low_size
,
934 ®buf
[REGISTER_BYTE (HIGH_RETURN_REGNUM
)], len
- low_size
);
937 internal_error (__FILE__
, __LINE__
,
938 "Cannot extract return value of %d bytes long.", len
);
942 /* Write into the appropriate registers a function return value stored
943 in VALBUF of type TYPE, given in virtual format. */
946 i386_store_return_value (struct type
*type
, char *valbuf
)
948 int len
= TYPE_LENGTH (type
);
950 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
951 && TYPE_NFIELDS (type
) == 1)
953 i386_store_return_value (TYPE_FIELD_TYPE (type
, 0), valbuf
);
957 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
960 char buf
[FPU_REG_RAW_SIZE
];
964 warning ("Cannot set floating-point return value.");
968 /* Returning floating-point values is a bit tricky. Apart from
969 storing the return value in %st(0), we have to simulate the
970 state of the FPU at function return point. */
972 /* Convert the value found in VALBUF to the extended
973 floating-point format used by the FPU. This is probably
974 not exactly how it would happen on the target itself, but
975 it is the best we can do. */
976 convert_typed_floating (valbuf
, type
, buf
, builtin_type_i387_ext
);
977 write_register_bytes (REGISTER_BYTE (FP0_REGNUM
), buf
,
980 /* Set the top of the floating-point register stack to 7. The
981 actual value doesn't really matter, but 7 is what a normal
982 function return would end up with if the program started out
983 with a freshly initialized FPU. */
984 fstat
= read_register (FSTAT_REGNUM
);
986 write_register (FSTAT_REGNUM
, fstat
);
988 /* Mark %st(1) through %st(7) as empty. Since we set the top of
989 the floating-point register stack to 7, the appropriate value
990 for the tag word is 0x3fff. */
991 write_register (FTAG_REGNUM
, 0x3fff);
995 int low_size
= REGISTER_RAW_SIZE (LOW_RETURN_REGNUM
);
996 int high_size
= REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM
);
999 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM
), valbuf
, len
);
1000 else if (len
<= (low_size
+ high_size
))
1002 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM
),
1004 write_register_bytes (REGISTER_BYTE (HIGH_RETURN_REGNUM
),
1005 valbuf
+ low_size
, len
- low_size
);
1008 internal_error (__FILE__
, __LINE__
,
1009 "Cannot store return value of %d bytes long.", len
);
1013 /* Extract from an array REGBUF containing the (raw) register state
1014 the address in which a function should return its structure value,
1018 i386_extract_struct_value_address (char *regbuf
)
1020 return extract_address (®buf
[REGISTER_BYTE (LOW_RETURN_REGNUM
)],
1021 REGISTER_RAW_SIZE (LOW_RETURN_REGNUM
));
1025 /* This is the variable that is set with "set struct-convention", and
1026 its legitimate values. */
1027 static const char default_struct_convention
[] = "default";
1028 static const char pcc_struct_convention
[] = "pcc";
1029 static const char reg_struct_convention
[] = "reg";
1030 static const char *valid_conventions
[] =
1032 default_struct_convention
,
1033 pcc_struct_convention
,
1034 reg_struct_convention
,
1037 static const char *struct_convention
= default_struct_convention
;
1040 i386_use_struct_convention (int gcc_p
, struct type
*type
)
1042 enum struct_return struct_return
;
1044 if (struct_convention
== default_struct_convention
)
1045 struct_return
= gdbarch_tdep (current_gdbarch
)->struct_return
;
1046 else if (struct_convention
== pcc_struct_convention
)
1047 struct_return
= pcc_struct_return
;
1049 struct_return
= reg_struct_return
;
1051 return generic_use_struct_convention (struct_return
== reg_struct_return
,
1056 /* Return the GDB type object for the "standard" data type of data in
1057 register REGNUM. Perhaps %esi and %edi should go here, but
1058 potentially they could be used for things other than address. */
1061 i386_register_virtual_type (int regnum
)
1063 if (regnum
== PC_REGNUM
|| regnum
== FP_REGNUM
|| regnum
== SP_REGNUM
)
1064 return lookup_pointer_type (builtin_type_void
);
1066 if (IS_FP_REGNUM (regnum
))
1067 return builtin_type_i387_ext
;
1069 if (IS_SSE_REGNUM (regnum
))
1070 return builtin_type_vec128i
;
1072 return builtin_type_int
;
1075 /* Return true iff register REGNUM's virtual format is different from
1076 its raw format. Note that this definition assumes that the host
1077 supports IEEE 32-bit floats, since it doesn't say that SSE
1078 registers need conversion. Even if we can't find a counterexample,
1079 this is still sloppy. */
1082 i386_register_convertible (int regnum
)
1084 return IS_FP_REGNUM (regnum
);
1087 /* Convert data from raw format for register REGNUM in buffer FROM to
1088 virtual format with type TYPE in buffer TO. */
1091 i386_register_convert_to_virtual (int regnum
, struct type
*type
,
1092 char *from
, char *to
)
1094 gdb_assert (IS_FP_REGNUM (regnum
));
1096 /* We only support floating-point values. */
1097 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1099 warning ("Cannot convert floating-point register value "
1100 "to non-floating-point type.");
1101 memset (to
, 0, TYPE_LENGTH (type
));
1105 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
1106 the extended floating-point format used by the FPU. */
1107 convert_typed_floating (from
, builtin_type_i387_ext
, to
, type
);
1110 /* Convert data from virtual format with type TYPE in buffer FROM to
1111 raw format for register REGNUM in buffer TO. */
1114 i386_register_convert_to_raw (struct type
*type
, int regnum
,
1115 char *from
, char *to
)
1117 gdb_assert (IS_FP_REGNUM (regnum
));
1119 /* We only support floating-point values. */
1120 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1122 warning ("Cannot convert non-floating-point type "
1123 "to floating-point register value.");
1124 memset (to
, 0, TYPE_LENGTH (type
));
1128 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
1129 to the extended floating-point format used by the FPU. */
1130 convert_typed_floating (from
, type
, to
, builtin_type_i387_ext
);
1134 #ifdef STATIC_TRANSFORM_NAME
1135 /* SunPRO encodes the static variables. This is not related to C++
1136 mangling, it is done for C too. */
1139 sunpro_static_transform_name (char *name
)
1142 if (IS_STATIC_TRANSFORM_NAME (name
))
1144 /* For file-local statics there will be a period, a bunch of
1145 junk (the contents of which match a string given in the
1146 N_OPT), a period and the name. For function-local statics
1147 there will be a bunch of junk (which seems to change the
1148 second character from 'A' to 'B'), a period, the name of the
1149 function, and the name. So just skip everything before the
1151 p
= strrchr (name
, '.');
1157 #endif /* STATIC_TRANSFORM_NAME */
1160 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
1163 skip_trampoline_code (CORE_ADDR pc
, char *name
)
1165 if (pc
&& read_memory_unsigned_integer (pc
, 2) == 0x25ff) /* jmp *(dest) */
1167 unsigned long indirect
= read_memory_unsigned_integer (pc
+ 2, 4);
1168 struct minimal_symbol
*indsym
=
1169 indirect
? lookup_minimal_symbol_by_pc (indirect
) : 0;
1170 char *symname
= indsym
? SYMBOL_NAME (indsym
) : 0;
1174 if (strncmp (symname
, "__imp_", 6) == 0
1175 || strncmp (symname
, "_imp_", 5) == 0)
1176 return name
? 1 : read_memory_unsigned_integer (indirect
, 4);
1179 return 0; /* Not a trampoline. */
1183 /* Return non-zero if PC and NAME show that we are in a signal
1187 i386_pc_in_sigtramp (CORE_ADDR pc
, char *name
)
1189 return (name
&& strcmp ("_sigtramp", name
) == 0);
1193 /* We have two flavours of disassembly. The machinery on this page
1194 deals with switching between those. */
1197 gdb_print_insn_i386 (bfd_vma memaddr
, disassemble_info
*info
)
1199 if (disassembly_flavor
== att_flavor
)
1200 return print_insn_i386_att (memaddr
, info
);
1201 else if (disassembly_flavor
== intel_flavor
)
1202 return print_insn_i386_intel (memaddr
, info
);
1203 /* Never reached -- disassembly_flavour is always either att_flavor
1205 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
1209 /* There are a few i386 architecture variants that differ only
1210 slightly from the generic i386 target. For now, we don't give them
1211 their own source file, but include them here. As a consequence,
1212 they'll always be included. */
1214 /* System V Release 4 (SVR4). */
1217 i386_svr4_pc_in_sigtramp (CORE_ADDR pc
, char *name
)
1219 return (name
&& (strcmp ("_sigreturn", name
) == 0
1220 || strcmp ("_sigacthandler", name
) == 0
1221 || strcmp ("sigvechandler", name
) == 0));
1224 /* Get saved user PC for sigtramp from the pushed ucontext on the
1225 stack for all three variants of SVR4 sigtramps. */
1228 i386_svr4_sigtramp_saved_pc (struct frame_info
*frame
)
1230 CORE_ADDR saved_pc_offset
= 4;
1233 find_pc_partial_function (frame
->pc
, &name
, NULL
, NULL
);
1236 if (strcmp (name
, "_sigreturn") == 0)
1237 saved_pc_offset
= 132 + 14 * 4;
1238 else if (strcmp (name
, "_sigacthandler") == 0)
1239 saved_pc_offset
= 80 + 14 * 4;
1240 else if (strcmp (name
, "sigvechandler") == 0)
1241 saved_pc_offset
= 120 + 14 * 4;
1245 return read_memory_integer (frame
->next
->frame
+ saved_pc_offset
, 4);
1246 return read_memory_integer (read_register (SP_REGNUM
) + saved_pc_offset
, 4);
1253 i386_go32_pc_in_sigtramp (CORE_ADDR pc
, char *name
)
1255 /* DJGPP doesn't have any special frames for signal handlers. */
1263 i386_elf_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1265 /* We typically use stabs-in-ELF with the DWARF register numbering. */
1266 set_gdbarch_stab_reg_to_regnum (gdbarch
, i386_dwarf_reg_to_regnum
);
1269 /* System V Release 4 (SVR4). */
1272 i386_svr4_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1274 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1276 /* System V Release 4 uses ELF. */
1277 i386_elf_init_abi (info
, gdbarch
);
1279 /* FIXME: kettenis/20020511: Why do we override this function here? */
1280 set_gdbarch_frame_chain_valid (gdbarch
, func_frame_chain_valid
);
1282 set_gdbarch_pc_in_sigtramp (gdbarch
, i386_svr4_pc_in_sigtramp
);
1283 tdep
->sigtramp_saved_pc
= i386_svr4_sigtramp_saved_pc
;
1285 tdep
->jb_pc_offset
= 20;
1291 i386_go32_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1293 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1295 set_gdbarch_pc_in_sigtramp (gdbarch
, i386_go32_pc_in_sigtramp
);
1297 tdep
->jb_pc_offset
= 36;
1303 i386_nw_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1305 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1307 /* FIXME: kettenis/20020511: Why do we override this function here? */
1308 set_gdbarch_frame_chain_valid (gdbarch
, func_frame_chain_valid
);
1310 tdep
->jb_pc_offset
= 24;
1315 i386_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
1317 struct gdbarch_tdep
*tdep
;
1318 struct gdbarch
*gdbarch
;
1319 enum gdb_osabi osabi
= GDB_OSABI_UNKNOWN
;
1321 /* Try to determine the OS ABI of the object we're loading. */
1322 if (info
.abfd
!= NULL
)
1323 osabi
= gdbarch_lookup_osabi (info
.abfd
);
1325 /* Find a candidate among extant architectures. */
1326 for (arches
= gdbarch_list_lookup_by_info (arches
, &info
);
1328 arches
= gdbarch_list_lookup_by_info (arches
->next
, &info
))
1330 /* Make sure the OS ABI selection matches. */
1331 tdep
= gdbarch_tdep (arches
->gdbarch
);
1332 if (tdep
&& tdep
->osabi
== osabi
)
1333 return arches
->gdbarch
;
1336 /* Allocate space for the new architecture. */
1337 tdep
= XMALLOC (struct gdbarch_tdep
);
1338 gdbarch
= gdbarch_alloc (&info
, tdep
);
1340 tdep
->osabi
= osabi
;
1342 /* The i386 default settings don't include the SSE registers.
1343 FIXME: kettenis/20020509: They do include the FPU registers for
1344 now, which is not quite right. */
1345 tdep
->num_xmm_regs
= 0;
1347 tdep
->jb_pc_offset
= -1;
1348 tdep
->struct_return
= pcc_struct_return
;
1349 tdep
->sigtramp_saved_pc
= NULL
;
1350 tdep
->sigtramp_start
= 0;
1351 tdep
->sigtramp_end
= 0;
1352 tdep
->sc_pc_offset
= -1;
1354 /* The format used for `long double' on almost all i386 targets is
1355 the i387 extended floating-point format. In fact, of all targets
1356 in the GCC 2.95 tree, only OSF/1 does it different, and insists
1357 on having a `long double' that's not `long' at all. */
1358 set_gdbarch_long_double_format (gdbarch
, &floatformat_i387_ext
);
1360 /* Although the i386 extended floating-point has only 80 significant
1361 bits, a `long double' actually takes up 96, probably to enforce
1363 set_gdbarch_long_double_bit (gdbarch
, 96);
1365 set_gdbarch_get_longjmp_target (gdbarch
, i386_get_longjmp_target
);
1367 set_gdbarch_use_generic_dummy_frames (gdbarch
, 0);
1369 /* Call dummy code. */
1370 set_gdbarch_call_dummy_location (gdbarch
, ON_STACK
);
1371 set_gdbarch_call_dummy_breakpoint_offset (gdbarch
, 5);
1372 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch
, 1);
1373 set_gdbarch_call_dummy_p (gdbarch
, 1);
1374 set_gdbarch_call_dummy_stack_adjust_p (gdbarch
, 0);
1376 set_gdbarch_get_saved_register (gdbarch
, generic_get_saved_register
);
1377 set_gdbarch_push_arguments (gdbarch
, i386_push_arguments
);
1379 set_gdbarch_pc_in_call_dummy (gdbarch
, pc_in_call_dummy_on_stack
);
1381 set_gdbarch_use_struct_convention (gdbarch
, i386_use_struct_convention
);
1383 /* The following redefines make backtracing through sigtramp work.
1384 They manufacture a fake sigtramp frame and obtain the saved pc in
1385 sigtramp from the sigcontext structure which is pushed by the
1386 kernel on the user stack, along with a pointer to it. */
1388 set_gdbarch_frame_chain (gdbarch
, i386_frame_chain
);
1389 set_gdbarch_frame_chain_valid (gdbarch
, file_frame_chain_valid
);
1390 set_gdbarch_frame_saved_pc (gdbarch
, i386_frame_saved_pc
);
1391 set_gdbarch_saved_pc_after_call (gdbarch
, i386_saved_pc_after_call
);
1392 set_gdbarch_pc_in_sigtramp (gdbarch
, i386_pc_in_sigtramp
);
1394 /* NOTE: tm-i386aix.h, tm-i386bsd.h, tm-i386os9k.h, tm-ptx.h,
1395 tm-symmetry.h currently override this. Sigh. */
1396 set_gdbarch_num_regs (gdbarch
, I386_NUM_GREGS
+ I386_NUM_FREGS
);
1398 /* Use the "default" register numbering scheme for stabs and COFF. */
1399 set_gdbarch_stab_reg_to_regnum (gdbarch
, i386_stab_reg_to_regnum
);
1400 set_gdbarch_sdb_reg_to_regnum (gdbarch
, i386_stab_reg_to_regnum
);
1402 /* Use the DWARF register numbering scheme for DWARF and DWARF 2. */
1403 set_gdbarch_dwarf_reg_to_regnum (gdbarch
, i386_dwarf_reg_to_regnum
);
1404 set_gdbarch_dwarf2_reg_to_regnum (gdbarch
, i386_dwarf_reg_to_regnum
);
1406 /* We don't define ECOFF_REG_TO_REGNUM, since ECOFF doesn't seem to
1407 be in use on any of the supported i386 targets. */
1409 set_gdbarch_register_bytes (gdbarch
, I386_SIZEOF_GREGS
+ I386_SIZEOF_FREGS
);
1410 set_gdbarch_register_name (gdbarch
, i386_register_name
);
1411 set_gdbarch_register_byte (gdbarch
, i386_register_byte
);
1412 set_gdbarch_register_raw_size (gdbarch
, i386_register_raw_size
);
1414 /* Hook in ABI-specific overrides, if they have been registered. */
1415 gdbarch_init_osabi (info
, gdbarch
, osabi
);
1420 static enum gdb_osabi
1421 i386_coff_osabi_sniffer (bfd
*abfd
)
1423 if (strcmp (bfd_get_target (abfd
), "coff-go32-exe") == 0)
1424 return GDB_OSABI_GO32
;
1426 return GDB_OSABI_UNKNOWN
;
1429 static enum gdb_osabi
1430 i386_nlm_osabi_sniffer (bfd
*abfd
)
1432 return GDB_OSABI_NETWARE
;
1436 /* Provide a prototype to silence -Wmissing-prototypes. */
1437 void _initialize_i386_tdep (void);
1440 _initialize_i386_tdep (void)
1442 register_gdbarch_init (bfd_arch_i386
, i386_gdbarch_init
);
1444 /* Initialize the table saying where each register starts in the
1450 for (i
= 0; i
< MAX_NUM_REGS
; i
++)
1452 i386_register_offset
[i
] = offset
;
1453 offset
+= i386_register_size
[i
];
1457 tm_print_insn
= gdb_print_insn_i386
;
1458 tm_print_insn_info
.mach
= bfd_lookup_arch (bfd_arch_i386
, 0)->mach
;
1460 /* Add the variable that controls the disassembly flavor. */
1462 struct cmd_list_element
*new_cmd
;
1464 new_cmd
= add_set_enum_cmd ("disassembly-flavor", no_class
,
1466 &disassembly_flavor
,
1468 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1469 and the default value is \"att\".",
1471 add_show_from_set (new_cmd
, &showlist
);
1474 /* Add the variable that controls the convention for returning
1477 struct cmd_list_element
*new_cmd
;
1479 new_cmd
= add_set_enum_cmd ("struct-convention", no_class
,
1481 &struct_convention
, "\
1482 Set the convention for returning small structs, valid values \
1483 are \"default\", \"pcc\" and \"reg\", and the default value is \"default\".",
1485 add_show_from_set (new_cmd
, &showlist
);
1488 gdbarch_register_osabi_sniffer (bfd_arch_i386
, bfd_target_coff_flavour
,
1489 i386_coff_osabi_sniffer
);
1490 gdbarch_register_osabi_sniffer (bfd_arch_i386
, bfd_target_nlm_flavour
,
1491 i386_nlm_osabi_sniffer
);
1493 gdbarch_register_osabi (bfd_arch_i386
, GDB_OSABI_SVR4
,
1494 i386_svr4_init_abi
);
1495 gdbarch_register_osabi (bfd_arch_i386
, GDB_OSABI_GO32
,
1496 i386_go32_init_abi
);
1497 gdbarch_register_osabi (bfd_arch_i386
, GDB_OSABI_NETWARE
,