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 "gdb_assert.h"
41 #include "i386-tdep.h"
44 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
46 /* Names of the registers. The first 10 registers match the register
47 numbering scheme used by GCC for stabs and DWARF. */
48 static char *i386_register_names
[] =
50 "eax", "ecx", "edx", "ebx",
51 "esp", "ebp", "esi", "edi",
52 "eip", "eflags", "cs", "ss",
53 "ds", "es", "fs", "gs",
54 "st0", "st1", "st2", "st3",
55 "st4", "st5", "st6", "st7",
56 "fctrl", "fstat", "ftag", "fiseg",
57 "fioff", "foseg", "fooff", "fop",
58 "xmm0", "xmm1", "xmm2", "xmm3",
59 "xmm4", "xmm5", "xmm6", "xmm7",
63 /* i386_register_offset[i] is the offset into the register file of the
64 start of register number i. We initialize this from
65 i386_register_size. */
66 static int i386_register_offset
[MAX_NUM_REGS
];
68 /* i386_register_size[i] is the number of bytes of storage in GDB's
69 register array occupied by register i. */
70 static int i386_register_size
[MAX_NUM_REGS
] = {
84 /* Return the name of register REG. */
87 i386_register_name (int reg
)
91 if (reg
>= sizeof (i386_register_names
) / sizeof (*i386_register_names
))
94 return i386_register_names
[reg
];
97 /* Return the offset into the register array of the start of register
100 i386_register_byte (int reg
)
102 return i386_register_offset
[reg
];
105 /* Return the number of bytes of storage in GDB's register array
106 occupied by register REG. */
109 i386_register_raw_size (int reg
)
111 return i386_register_size
[reg
];
114 /* Return the size in bytes of the virtual type of register REG. */
117 i386_register_virtual_size (int reg
)
119 return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (reg
));
122 /* Convert stabs register number REG to the appropriate register
123 number used by GDB. */
126 i386_stab_reg_to_regnum (int reg
)
128 /* This implements what GCC calls the "default" register map. */
129 if (reg
>= 0 && reg
<= 7)
131 /* General registers. */
134 else if (reg
>= 12 && reg
<= 19)
136 /* Floating-point registers. */
137 return reg
- 12 + FP0_REGNUM
;
139 else if (reg
>= 21 && reg
<= 28)
142 return reg
- 21 + XMM0_REGNUM
;
144 else if (reg
>= 29 && reg
<= 36)
147 /* FIXME: kettenis/2001-07-28: Should we have the MMX registers
148 as pseudo-registers? */
149 return reg
- 29 + FP0_REGNUM
;
152 /* This will hopefully provoke a warning. */
153 return NUM_REGS
+ NUM_PSEUDO_REGS
;
156 /* Convert Dwarf register number REG to the appropriate register
157 number used by GDB. */
160 i386_dwarf_reg_to_regnum (int reg
)
162 /* The DWARF register numbering includes %eip and %eflags, and
163 numbers the floating point registers differently. */
164 if (reg
>= 0 && reg
<= 9)
166 /* General registers. */
169 else if (reg
>= 11 && reg
<= 18)
171 /* Floating-point registers. */
172 return reg
- 11 + FP0_REGNUM
;
176 /* The SSE and MMX registers have identical numbers as in stabs. */
177 return i386_stab_reg_to_regnum (reg
);
180 /* This will hopefully provoke a warning. */
181 return NUM_REGS
+ NUM_PSEUDO_REGS
;
185 /* This is the variable that is set with "set disassembly-flavor", and
186 its legitimate values. */
187 static const char att_flavor
[] = "att";
188 static const char intel_flavor
[] = "intel";
189 static const char *valid_flavors
[] =
195 static const char *disassembly_flavor
= att_flavor
;
197 /* Stdio style buffering was used to minimize calls to ptrace, but
198 this buffering did not take into account that the code section
199 being accessed may not be an even number of buffers long (even if
200 the buffer is only sizeof(int) long). In cases where the code
201 section size happened to be a non-integral number of buffers long,
202 attempting to read the last buffer would fail. Simply using
203 target_read_memory and ignoring errors, rather than read_memory, is
204 not the correct solution, since legitimate access errors would then
205 be totally ignored. To properly handle this situation and continue
206 to use buffering would require that this code be able to determine
207 the minimum code section size granularity (not the alignment of the
208 section itself, since the actual failing case that pointed out this
209 problem had a section alignment of 4 but was not a multiple of 4
210 bytes long), on a target by target basis, and then adjust it's
211 buffer size accordingly. This is messy, but potentially feasible.
212 It probably needs the bfd library's help and support. For now, the
213 buffer size is set to 1. (FIXME -fnf) */
215 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
216 static CORE_ADDR codestream_next_addr
;
217 static CORE_ADDR codestream_addr
;
218 static unsigned char codestream_buf
[CODESTREAM_BUFSIZ
];
219 static int codestream_off
;
220 static int codestream_cnt
;
222 #define codestream_tell() (codestream_addr + codestream_off)
223 #define codestream_peek() \
224 (codestream_cnt == 0 ? \
225 codestream_fill(1) : codestream_buf[codestream_off])
226 #define codestream_get() \
227 (codestream_cnt-- == 0 ? \
228 codestream_fill(0) : codestream_buf[codestream_off++])
231 codestream_fill (int peek_flag
)
233 codestream_addr
= codestream_next_addr
;
234 codestream_next_addr
+= CODESTREAM_BUFSIZ
;
236 codestream_cnt
= CODESTREAM_BUFSIZ
;
237 read_memory (codestream_addr
, (char *) codestream_buf
, CODESTREAM_BUFSIZ
);
240 return (codestream_peek ());
242 return (codestream_get ());
246 codestream_seek (CORE_ADDR place
)
248 codestream_next_addr
= place
/ CODESTREAM_BUFSIZ
;
249 codestream_next_addr
*= CODESTREAM_BUFSIZ
;
252 while (codestream_tell () != place
)
257 codestream_read (unsigned char *buf
, int count
)
262 for (i
= 0; i
< count
; i
++)
263 *p
++ = codestream_get ();
267 /* If the next instruction is a jump, move to its target. */
270 i386_follow_jump (void)
272 unsigned char buf
[4];
278 pos
= codestream_tell ();
281 if (codestream_peek () == 0x66)
287 switch (codestream_get ())
290 /* Relative jump: if data16 == 0, disp32, else disp16. */
293 codestream_read (buf
, 2);
294 delta
= extract_signed_integer (buf
, 2);
296 /* Include the size of the jmp instruction (including the
302 codestream_read (buf
, 4);
303 delta
= extract_signed_integer (buf
, 4);
309 /* Relative jump, disp8 (ignore data16). */
310 codestream_read (buf
, 1);
311 /* Sign-extend it. */
312 delta
= extract_signed_integer (buf
, 1);
317 codestream_seek (pos
);
320 /* Find & return the amount a local space allocated, and advance the
321 codestream to the first register push (if any).
323 If the entry sequence doesn't make sense, return -1, and leave
324 codestream pointer at a random spot. */
327 i386_get_frame_setup (CORE_ADDR pc
)
331 codestream_seek (pc
);
335 op
= codestream_get ();
337 if (op
== 0x58) /* popl %eax */
339 /* This function must start with
342 xchgl %eax, (%esp) 0x87 0x04 0x24
343 or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
345 (the System V compiler puts out the second `xchg'
346 instruction, and the assembler doesn't try to optimize it, so
347 the 'sib' form gets generated). This sequence is used to get
348 the address of the return buffer for a function that returns
351 unsigned char buf
[4];
352 static unsigned char proto1
[3] = { 0x87, 0x04, 0x24 };
353 static unsigned char proto2
[4] = { 0x87, 0x44, 0x24, 0x00 };
355 pos
= codestream_tell ();
356 codestream_read (buf
, 4);
357 if (memcmp (buf
, proto1
, 3) == 0)
359 else if (memcmp (buf
, proto2
, 4) == 0)
362 codestream_seek (pos
);
363 op
= codestream_get (); /* Update next opcode. */
366 if (op
== 0x68 || op
== 0x6a)
368 /* This function may start with
380 unsigned char buf
[8];
382 /* Skip past the `pushl' instruction; it has either a one-byte
383 or a four-byte operand, depending on the opcode. */
384 pos
= codestream_tell ();
389 codestream_seek (pos
);
391 /* Read the following 8 bytes, which should be "call _probe" (6
392 bytes) followed by "addl $4,%esp" (2 bytes). */
393 codestream_read (buf
, sizeof (buf
));
394 if (buf
[0] == 0xe8 && buf
[6] == 0xc4 && buf
[7] == 0x4)
396 codestream_seek (pos
);
397 op
= codestream_get (); /* Update next opcode. */
400 if (op
== 0x55) /* pushl %ebp */
402 /* Check for "movl %esp, %ebp" -- can be written in two ways. */
403 switch (codestream_get ())
406 if (codestream_get () != 0xec)
410 if (codestream_get () != 0xe5)
416 /* Check for stack adjustment
420 NOTE: You can't subtract a 16 bit immediate from a 32 bit
421 reg, so we don't have to worry about a data16 prefix. */
422 op
= codestream_peek ();
425 /* `subl' with 8 bit immediate. */
427 if (codestream_get () != 0xec)
428 /* Some instruction starting with 0x83 other than `subl'. */
430 codestream_seek (codestream_tell () - 2);
433 /* `subl' with signed byte immediate (though it wouldn't
434 make sense to be negative). */
435 return (codestream_get ());
440 /* Maybe it is `subl' with a 32 bit immedediate. */
442 if (codestream_get () != 0xec)
443 /* Some instruction starting with 0x81 other than `subl'. */
445 codestream_seek (codestream_tell () - 2);
448 /* It is `subl' with a 32 bit immediate. */
449 codestream_read ((unsigned char *) buf
, 4);
450 return extract_signed_integer (buf
, 4);
460 /* `enter' with 16 bit unsigned immediate. */
461 codestream_read ((unsigned char *) buf
, 2);
462 codestream_get (); /* Flush final byte of enter instruction. */
463 return extract_unsigned_integer (buf
, 2);
468 /* Return the chain-pointer for FRAME. In the case of the i386, the
469 frame's nominal address is the address of a 4-byte word containing
470 the calling frame's address. */
473 i386_frame_chain (struct frame_info
*frame
)
475 if (frame
->signal_handler_caller
)
478 if (! inside_entry_file (frame
->pc
))
479 return read_memory_unsigned_integer (frame
->frame
, 4);
484 /* Determine whether the function invocation represented by FRAME does
485 not have a from on the stack associated with it. If it does not,
486 return non-zero, otherwise return zero. */
489 i386_frameless_function_invocation (struct frame_info
*frame
)
491 if (frame
->signal_handler_caller
)
494 return frameless_look_for_prologue (frame
);
497 /* Return the saved program counter for FRAME. */
500 i386_frame_saved_pc (struct frame_info
*frame
)
502 /* FIXME: kettenis/2001-05-09: Conditionalizing the next bit of code
503 on SIGCONTEXT_PC_OFFSET and I386V4_SIGTRAMP_SAVED_PC should be
504 considered a temporary hack. I plan to come up with something
505 better when we go multi-arch. */
506 #if defined (SIGCONTEXT_PC_OFFSET) || defined (I386V4_SIGTRAMP_SAVED_PC)
507 if (frame
->signal_handler_caller
)
508 return sigtramp_saved_pc (frame
);
511 return read_memory_unsigned_integer (frame
->frame
+ 4, 4);
515 i386go32_frame_saved_pc (struct frame_info
*frame
)
517 return read_memory_integer (frame
->frame
+ 4, 4);
520 /* Immediately after a function call, return the saved pc. */
523 i386_saved_pc_after_call (struct frame_info
*frame
)
525 return read_memory_unsigned_integer (read_register (SP_REGNUM
), 4);
528 /* Return number of args passed to a frame.
529 Can return -1, meaning no way to tell. */
532 i386_frame_num_args (struct frame_info
*fi
)
537 /* This loses because not only might the compiler not be popping the
538 args right after the function call, it might be popping args from
539 both this call and a previous one, and we would say there are
540 more args than there really are. */
544 struct frame_info
*pfi
;
546 /* On the i386, the instruction following the call could be:
548 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
549 anything else - zero args. */
553 frameless
= FRAMELESS_FUNCTION_INVOCATION (fi
);
555 /* In the absence of a frame pointer, GDB doesn't get correct
556 values for nameless arguments. Return -1, so it doesn't print
557 any nameless arguments. */
560 pfi
= get_prev_frame (fi
);
563 /* NOTE: This can happen if we are looking at the frame for
564 main, because FRAME_CHAIN_VALID won't let us go into start.
565 If we have debugging symbols, that's not really a big deal;
566 it just means it will only show as many arguments to main as
573 op
= read_memory_integer (retpc
, 1);
574 if (op
== 0x59) /* pop %ecx */
578 op
= read_memory_integer (retpc
+ 1, 1);
580 /* addl $<signed imm 8 bits>, %esp */
581 return (read_memory_integer (retpc
+ 2, 1) & 0xff) / 4;
585 else if (op
== 0x81) /* `add' with 32 bit immediate. */
587 op
= read_memory_integer (retpc
+ 1, 1);
589 /* addl $<imm 32>, %esp */
590 return read_memory_integer (retpc
+ 2, 4) / 4;
602 /* Parse the first few instructions the function to see what registers
605 We handle these cases:
607 The startup sequence can be at the start of the function, or the
608 function can start with a branch to startup code at the end.
610 %ebp can be set up with either the 'enter' instruction, or "pushl
611 %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
612 once used in the System V compiler).
614 Local space is allocated just below the saved %ebp by either the
615 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a 16
616 bit unsigned argument for space to allocate, and the 'addl'
617 instruction could have either a signed byte, or 32 bit immediate.
619 Next, the registers used by this function are pushed. With the
620 System V compiler they will always be in the order: %edi, %esi,
621 %ebx (and sometimes a harmless bug causes it to also save but not
622 restore %eax); however, the code below is willing to see the pushes
623 in any order, and will handle up to 8 of them.
625 If the setup sequence is at the end of the function, then the next
626 instruction will be a branch back to the start. */
629 i386_frame_init_saved_regs (struct frame_info
*fip
)
633 CORE_ADDR dummy_bottom
;
641 frame_saved_regs_zalloc (fip
);
643 /* If the frame is the end of a dummy, compute where the beginning
645 dummy_bottom
= fip
->frame
- 4 - REGISTER_BYTES
- CALL_DUMMY_LENGTH
;
647 /* Check if the PC points in the stack, in a dummy frame. */
648 if (dummy_bottom
<= fip
->pc
&& fip
->pc
<= fip
->frame
)
650 /* All registers were saved by push_call_dummy. */
652 for (i
= 0; i
< NUM_REGS
; i
++)
654 addr
-= REGISTER_RAW_SIZE (i
);
655 fip
->saved_regs
[i
] = addr
;
660 pc
= get_pc_function_start (fip
->pc
);
662 locals
= i386_get_frame_setup (pc
);
666 addr
= fip
->frame
- 4 - locals
;
667 for (i
= 0; i
< 8; i
++)
669 op
= codestream_get ();
670 if (op
< 0x50 || op
> 0x57)
672 #ifdef I386_REGNO_TO_SYMMETRY
673 /* Dynix uses different internal numbering. Ick. */
674 fip
->saved_regs
[I386_REGNO_TO_SYMMETRY (op
- 0x50)] = addr
;
676 fip
->saved_regs
[op
- 0x50] = addr
;
682 fip
->saved_regs
[PC_REGNUM
] = fip
->frame
+ 4;
683 fip
->saved_regs
[FP_REGNUM
] = fip
->frame
;
686 /* Return PC of first real instruction. */
689 i386_skip_prologue (int pc
)
693 static unsigned char pic_pat
[6] =
694 { 0xe8, 0, 0, 0, 0, /* call 0x0 */
695 0x5b, /* popl %ebx */
699 if (i386_get_frame_setup (pc
) < 0)
702 /* Found valid frame setup -- codestream now points to start of push
703 instructions for saving registers. */
705 /* Skip over register saves. */
706 for (i
= 0; i
< 8; i
++)
708 op
= codestream_peek ();
709 /* Break if not `pushl' instrunction. */
710 if (op
< 0x50 || op
> 0x57)
715 /* The native cc on SVR4 in -K PIC mode inserts the following code
716 to get the address of the global offset table (GOT) into register
721 movl %ebx,x(%ebp) (optional)
724 This code is with the rest of the prologue (at the end of the
725 function), so we have to skip it to get to the first real
726 instruction at the start of the function. */
728 pos
= codestream_tell ();
729 for (i
= 0; i
< 6; i
++)
731 op
= codestream_get ();
732 if (pic_pat
[i
] != op
)
737 unsigned char buf
[4];
740 op
= codestream_get ();
741 if (op
== 0x89) /* movl %ebx, x(%ebp) */
743 op
= codestream_get ();
744 if (op
== 0x5d) /* One byte offset from %ebp. */
747 codestream_read (buf
, 1);
749 else if (op
== 0x9d) /* Four byte offset from %ebp. */
752 codestream_read (buf
, 4);
754 else /* Unexpected instruction. */
756 op
= codestream_get ();
759 if (delta
> 0 && op
== 0x81 && codestream_get () == 0xc3)
764 codestream_seek (pos
);
768 return (codestream_tell ());
772 i386_push_dummy_frame (void)
774 CORE_ADDR sp
= read_register (SP_REGNUM
);
777 char regbuf
[MAX_REGISTER_RAW_SIZE
];
779 sp
= push_word (sp
, read_register (PC_REGNUM
));
780 sp
= push_word (sp
, read_register (FP_REGNUM
));
782 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
784 read_register_gen (regnum
, regbuf
);
785 sp
= push_bytes (sp
, regbuf
, REGISTER_RAW_SIZE (regnum
));
787 write_register (SP_REGNUM
, sp
);
788 write_register (FP_REGNUM
, fp
);
791 /* Insert the (relative) function address into the call sequence
795 i386_fix_call_dummy (char *dummy
, CORE_ADDR pc
, CORE_ADDR fun
, int nargs
,
796 struct value
**args
, struct type
*type
, int gcc_p
)
798 int from
, to
, delta
, loc
;
800 loc
= (int)(read_register (SP_REGNUM
) - CALL_DUMMY_LENGTH
);
805 *((char *)(dummy
) + 1) = (delta
& 0xff);
806 *((char *)(dummy
) + 2) = ((delta
>> 8) & 0xff);
807 *((char *)(dummy
) + 3) = ((delta
>> 16) & 0xff);
808 *((char *)(dummy
) + 4) = ((delta
>> 24) & 0xff);
812 i386_pop_frame (void)
814 struct frame_info
*frame
= get_current_frame ();
817 char regbuf
[MAX_REGISTER_RAW_SIZE
];
819 fp
= FRAME_FP (frame
);
820 i386_frame_init_saved_regs (frame
);
822 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
825 addr
= frame
->saved_regs
[regnum
];
828 read_memory (addr
, regbuf
, REGISTER_RAW_SIZE (regnum
));
829 write_register_bytes (REGISTER_BYTE (regnum
), regbuf
,
830 REGISTER_RAW_SIZE (regnum
));
833 write_register (FP_REGNUM
, read_memory_integer (fp
, 4));
834 write_register (PC_REGNUM
, read_memory_integer (fp
+ 4, 4));
835 write_register (SP_REGNUM
, fp
+ 8);
836 flush_cached_frames ();
840 #ifdef GET_LONGJMP_TARGET
842 /* Figure out where the longjmp will land. Slurp the args out of the
843 stack. We expect the first arg to be a pointer to the jmp_buf
844 structure from which we extract the pc (JB_PC) that we will land
845 at. The pc is copied into PC. This routine returns true on
849 get_longjmp_target (CORE_ADDR
*pc
)
851 char buf
[TARGET_PTR_BIT
/ TARGET_CHAR_BIT
];
852 CORE_ADDR sp
, jb_addr
;
854 sp
= read_register (SP_REGNUM
);
856 if (target_read_memory (sp
+ SP_ARG0
, /* Offset of first arg on stack. */
858 TARGET_PTR_BIT
/ TARGET_CHAR_BIT
))
861 jb_addr
= extract_address (buf
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
863 if (target_read_memory (jb_addr
+ JB_PC
* JB_ELEMENT_SIZE
, buf
,
864 TARGET_PTR_BIT
/ TARGET_CHAR_BIT
))
867 *pc
= extract_address (buf
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
872 #endif /* GET_LONGJMP_TARGET */
876 i386_push_arguments (int nargs
, struct value
**args
, CORE_ADDR sp
,
877 int struct_return
, CORE_ADDR struct_addr
)
879 sp
= default_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
);
886 store_address (buf
, 4, struct_addr
);
887 write_memory (sp
, buf
, 4);
894 i386_store_struct_return (CORE_ADDR addr
, CORE_ADDR sp
)
896 /* Do nothing. Everything was already done by i386_push_arguments. */
899 /* These registers are used for returning integers (and on some
900 targets also for returning `struct' and `union' values when their
901 size and alignment match an integer type). */
902 #define LOW_RETURN_REGNUM 0 /* %eax */
903 #define HIGH_RETURN_REGNUM 2 /* %edx */
905 /* Extract from an array REGBUF containing the (raw) register state, a
906 function return value of TYPE, and copy that, in virtual format,
910 i386_extract_return_value (struct type
*type
, char *regbuf
, char *valbuf
)
912 int len
= TYPE_LENGTH (type
);
914 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
915 && TYPE_NFIELDS (type
) == 1)
917 i386_extract_return_value (TYPE_FIELD_TYPE (type
, 0), regbuf
, valbuf
);
921 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
925 warning ("Cannot find floating-point return value.");
926 memset (valbuf
, 0, len
);
930 /* Floating-point return values can be found in %st(0). Convert
931 its contents to the desired type. This is probably not
932 exactly how it would happen on the target itself, but it is
933 the best we can do. */
934 convert_typed_floating (®buf
[REGISTER_BYTE (FP0_REGNUM
)],
935 builtin_type_i387_ext
, valbuf
, type
);
939 int low_size
= REGISTER_RAW_SIZE (LOW_RETURN_REGNUM
);
940 int high_size
= REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM
);
943 memcpy (valbuf
, ®buf
[REGISTER_BYTE (LOW_RETURN_REGNUM
)], len
);
944 else if (len
<= (low_size
+ high_size
))
947 ®buf
[REGISTER_BYTE (LOW_RETURN_REGNUM
)], low_size
);
948 memcpy (valbuf
+ low_size
,
949 ®buf
[REGISTER_BYTE (HIGH_RETURN_REGNUM
)], len
- low_size
);
952 internal_error (__FILE__
, __LINE__
,
953 "Cannot extract return value of %d bytes long.", len
);
957 /* Write into the appropriate registers a function return value stored
958 in VALBUF of type TYPE, given in virtual format. */
961 i386_store_return_value (struct type
*type
, char *valbuf
)
963 int len
= TYPE_LENGTH (type
);
965 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
966 && TYPE_NFIELDS (type
) == 1)
968 i386_store_return_value (TYPE_FIELD_TYPE (type
, 0), valbuf
);
972 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
975 char buf
[FPU_REG_RAW_SIZE
];
979 warning ("Cannot set floating-point return value.");
983 /* Returning floating-point values is a bit tricky. Apart from
984 storing the return value in %st(0), we have to simulate the
985 state of the FPU at function return point. */
987 /* Convert the value found in VALBUF to the extended
988 floating-point format used by the FPU. This is probably
989 not exactly how it would happen on the target itself, but
990 it is the best we can do. */
991 convert_typed_floating (valbuf
, type
, buf
, builtin_type_i387_ext
);
992 write_register_bytes (REGISTER_BYTE (FP0_REGNUM
), buf
,
995 /* Set the top of the floating-point register stack to 7. The
996 actual value doesn't really matter, but 7 is what a normal
997 function return would end up with if the program started out
998 with a freshly initialized FPU. */
999 fstat
= read_register (FSTAT_REGNUM
);
1001 write_register (FSTAT_REGNUM
, fstat
);
1003 /* Mark %st(1) through %st(7) as empty. Since we set the top of
1004 the floating-point register stack to 7, the appropriate value
1005 for the tag word is 0x3fff. */
1006 write_register (FTAG_REGNUM
, 0x3fff);
1010 int low_size
= REGISTER_RAW_SIZE (LOW_RETURN_REGNUM
);
1011 int high_size
= REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM
);
1013 if (len
<= low_size
)
1014 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM
), valbuf
, len
);
1015 else if (len
<= (low_size
+ high_size
))
1017 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM
),
1019 write_register_bytes (REGISTER_BYTE (HIGH_RETURN_REGNUM
),
1020 valbuf
+ low_size
, len
- low_size
);
1023 internal_error (__FILE__
, __LINE__
,
1024 "Cannot store return value of %d bytes long.", len
);
1028 /* Extract from an array REGBUF containing the (raw) register state
1029 the address in which a function should return its structure value,
1033 i386_extract_struct_value_address (char *regbuf
)
1035 return extract_address (®buf
[REGISTER_BYTE (LOW_RETURN_REGNUM
)],
1036 REGISTER_RAW_SIZE (LOW_RETURN_REGNUM
));
1040 /* Return the GDB type object for the "standard" data type of data in
1041 register REGNUM. Perhaps %esi and %edi should go here, but
1042 potentially they could be used for things other than address. */
1045 i386_register_virtual_type (int regnum
)
1047 if (regnum
== PC_REGNUM
|| regnum
== FP_REGNUM
|| regnum
== SP_REGNUM
)
1048 return lookup_pointer_type (builtin_type_void
);
1050 if (IS_FP_REGNUM (regnum
))
1051 return builtin_type_i387_ext
;
1053 if (IS_SSE_REGNUM (regnum
))
1054 return builtin_type_v4sf
;
1056 return builtin_type_int
;
1059 /* Return true iff register REGNUM's virtual format is different from
1060 its raw format. Note that this definition assumes that the host
1061 supports IEEE 32-bit floats, since it doesn't say that SSE
1062 registers need conversion. Even if we can't find a counterexample,
1063 this is still sloppy. */
1066 i386_register_convertible (int regnum
)
1068 return IS_FP_REGNUM (regnum
);
1071 /* Convert data from raw format for register REGNUM in buffer FROM to
1072 virtual format with type TYPE in buffer TO. */
1075 i386_register_convert_to_virtual (int regnum
, struct type
*type
,
1076 char *from
, char *to
)
1078 gdb_assert (IS_FP_REGNUM (regnum
));
1080 /* We only support floating-point values. */
1081 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1083 warning ("Cannot convert floating-point register value "
1084 "to non-floating-point type.");
1085 memset (to
, 0, TYPE_LENGTH (type
));
1089 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
1090 the extended floating-point format used by the FPU. */
1091 convert_typed_floating (from
, builtin_type_i387_ext
, to
, type
);
1094 /* Convert data from virtual format with type TYPE in buffer FROM to
1095 raw format for register REGNUM in buffer TO. */
1098 i386_register_convert_to_raw (struct type
*type
, int regnum
,
1099 char *from
, char *to
)
1101 gdb_assert (IS_FP_REGNUM (regnum
));
1103 /* We only support floating-point values. */
1104 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1106 warning ("Cannot convert non-floating-point type "
1107 "to floating-point register value.");
1108 memset (to
, 0, TYPE_LENGTH (type
));
1112 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
1113 to the extended floating-point format used by the FPU. */
1114 convert_typed_floating (from
, type
, to
, builtin_type_i387_ext
);
1118 #ifdef I386V4_SIGTRAMP_SAVED_PC
1119 /* Get saved user PC for sigtramp from the pushed ucontext on the
1120 stack for all three variants of SVR4 sigtramps. */
1123 i386v4_sigtramp_saved_pc (struct frame_info
*frame
)
1125 CORE_ADDR saved_pc_offset
= 4;
1128 find_pc_partial_function (frame
->pc
, &name
, NULL
, NULL
);
1131 if (STREQ (name
, "_sigreturn"))
1132 saved_pc_offset
= 132 + 14 * 4;
1133 else if (STREQ (name
, "_sigacthandler"))
1134 saved_pc_offset
= 80 + 14 * 4;
1135 else if (STREQ (name
, "sigvechandler"))
1136 saved_pc_offset
= 120 + 14 * 4;
1140 return read_memory_integer (frame
->next
->frame
+ saved_pc_offset
, 4);
1141 return read_memory_integer (read_register (SP_REGNUM
) + saved_pc_offset
, 4);
1143 #endif /* I386V4_SIGTRAMP_SAVED_PC */
1146 #ifdef STATIC_TRANSFORM_NAME
1147 /* SunPRO encodes the static variables. This is not related to C++
1148 mangling, it is done for C too. */
1151 sunpro_static_transform_name (char *name
)
1154 if (IS_STATIC_TRANSFORM_NAME (name
))
1156 /* For file-local statics there will be a period, a bunch of
1157 junk (the contents of which match a string given in the
1158 N_OPT), a period and the name. For function-local statics
1159 there will be a bunch of junk (which seems to change the
1160 second character from 'A' to 'B'), a period, the name of the
1161 function, and the name. So just skip everything before the
1163 p
= strrchr (name
, '.');
1169 #endif /* STATIC_TRANSFORM_NAME */
1172 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
1175 skip_trampoline_code (CORE_ADDR pc
, char *name
)
1177 if (pc
&& read_memory_unsigned_integer (pc
, 2) == 0x25ff) /* jmp *(dest) */
1179 unsigned long indirect
= read_memory_unsigned_integer (pc
+ 2, 4);
1180 struct minimal_symbol
*indsym
=
1181 indirect
? lookup_minimal_symbol_by_pc (indirect
) : 0;
1182 char *symname
= indsym
? SYMBOL_NAME (indsym
) : 0;
1186 if (strncmp (symname
, "__imp_", 6) == 0
1187 || strncmp (symname
, "_imp_", 5) == 0)
1188 return name
? 1 : read_memory_unsigned_integer (indirect
, 4);
1191 return 0; /* Not a trampoline. */
1195 /* We have two flavours of disassembly. The machinery on this page
1196 deals with switching between those. */
1199 gdb_print_insn_i386 (bfd_vma memaddr
, disassemble_info
*info
)
1201 if (disassembly_flavor
== att_flavor
)
1202 return print_insn_i386_att (memaddr
, info
);
1203 else if (disassembly_flavor
== intel_flavor
)
1204 return print_insn_i386_intel (memaddr
, info
);
1205 /* Never reached -- disassembly_flavour is always either att_flavor
1207 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
1212 process_note_abi_tag_sections (bfd
*abfd
, asection
*sect
, void *obj
)
1214 int *os_ident_ptr
= obj
;
1216 unsigned int sect_size
;
1218 name
= bfd_get_section_name (abfd
, sect
);
1219 sect_size
= bfd_section_size (abfd
, sect
);
1220 if (strcmp (name
, ".note.ABI-tag") == 0 && sect_size
> 0)
1222 unsigned int name_length
, data_length
, note_type
;
1223 char *note
= alloca (sect_size
);
1225 bfd_get_section_contents (abfd
, sect
, note
,
1226 (file_ptr
) 0, (bfd_size_type
) sect_size
);
1228 name_length
= bfd_h_get_32 (abfd
, note
);
1229 data_length
= bfd_h_get_32 (abfd
, note
+ 4);
1230 note_type
= bfd_h_get_32 (abfd
, note
+ 8);
1232 if (name_length
== 4 && data_length
== 16 && note_type
== 1
1233 && strcmp (note
+ 12, "GNU") == 0)
1235 int os_number
= bfd_h_get_32 (abfd
, note
+ 16);
1237 /* The case numbers are from abi-tags in glibc. */
1241 *os_ident_ptr
= ELFOSABI_LINUX
;
1244 *os_ident_ptr
= ELFOSABI_HURD
;
1247 *os_ident_ptr
= ELFOSABI_SOLARIS
;
1250 internal_error (__FILE__
, __LINE__
,
1251 "process_note_abi_sections: "
1252 "unknown OS number %d", os_number
);
1260 i386_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
1262 struct gdbarch_tdep
*tdep
;
1263 struct gdbarch
*gdbarch
;
1266 if (info
.abfd
!= NULL
1267 && bfd_get_flavour (info
.abfd
) == bfd_target_elf_flavour
)
1269 os_ident
= elf_elfheader (info
.abfd
)->e_ident
[EI_OSABI
];
1271 /* If os_ident is 0, it is not necessarily the case that we're
1272 on a SYSV system. (ELFOSABI_NONE is defined to be 0.)
1273 GNU/Linux uses a note section to record OS/ABI info, but
1274 leaves e_ident[EI_OSABI] zero. So we have to check for note
1276 if (os_ident
== ELFOSABI_NONE
)
1277 bfd_map_over_sections (info
.abfd
,
1278 process_note_abi_tag_sections
,
1281 /* If that didn't help us, revert to some non-standard checks. */
1282 if (os_ident
== ELFOSABI_NONE
)
1284 /* FreeBSD folks are naughty; they stored the string
1285 "FreeBSD" in the padding of the e_ident field of the ELF
1287 if (strcmp (&elf_elfheader (info
.abfd
)->e_ident
[8], "FreeBSD") == 0)
1288 os_ident
= ELFOSABI_FREEBSD
;
1294 for (arches
= gdbarch_list_lookup_by_info (arches
, &info
);
1296 arches
= gdbarch_list_lookup_by_info (arches
->next
, &info
))
1298 if (gdbarch_tdep (current_gdbarch
)->os_ident
!= os_ident
)
1300 return arches
->gdbarch
;
1303 /* Allocate space for the new architecture. */
1304 tdep
= XMALLOC (struct gdbarch_tdep
);
1305 gdbarch
= gdbarch_alloc (&info
, tdep
);
1307 tdep
->os_ident
= os_ident
;
1309 /* FIXME: kettenis/2001-11-24: Although not all IA-32 processors
1310 have the SSE registers, it's easier to set the default to 8. */
1311 tdep
->num_xmm_regs
= 8;
1313 set_gdbarch_use_generic_dummy_frames (gdbarch
, 0);
1315 /* Call dummy code. */
1316 set_gdbarch_call_dummy_location (gdbarch
, ON_STACK
);
1317 set_gdbarch_call_dummy_breakpoint_offset (gdbarch
, 5);
1318 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch
, 1);
1319 set_gdbarch_call_dummy_p (gdbarch
, 1);
1320 set_gdbarch_call_dummy_stack_adjust_p (gdbarch
, 0);
1322 set_gdbarch_get_saved_register (gdbarch
, generic_get_saved_register
);
1323 set_gdbarch_push_arguments (gdbarch
, i386_push_arguments
);
1325 set_gdbarch_pc_in_call_dummy (gdbarch
, pc_in_call_dummy_on_stack
);
1327 /* NOTE: tm-i386nw.h and tm-i386v4.h override this. */
1328 set_gdbarch_frame_chain_valid (gdbarch
, file_frame_chain_valid
);
1330 /* NOTE: tm-i386aix.h, tm-i386bsd.h, tm-i386os9k.h, tm-linux.h,
1331 tm-ptx.h, tm-symmetry.h currently override this. Sigh. */
1332 set_gdbarch_num_regs (gdbarch
, NUM_GREGS
+ NUM_FREGS
+ NUM_SSE_REGS
);
1337 /* Provide a prototype to silence -Wmissing-prototypes. */
1338 void _initialize_i386_tdep (void);
1341 _initialize_i386_tdep (void)
1343 register_gdbarch_init (bfd_arch_i386
, i386_gdbarch_init
);
1345 /* Initialize the table saying where each register starts in the
1351 for (i
= 0; i
< MAX_NUM_REGS
; i
++)
1353 i386_register_offset
[i
] = offset
;
1354 offset
+= i386_register_size
[i
];
1358 tm_print_insn
= gdb_print_insn_i386
;
1359 tm_print_insn_info
.mach
= bfd_lookup_arch (bfd_arch_i386
, 0)->mach
;
1361 /* Add the variable that controls the disassembly flavor. */
1363 struct cmd_list_element
*new_cmd
;
1365 new_cmd
= add_set_enum_cmd ("disassembly-flavor", no_class
,
1367 &disassembly_flavor
,
1369 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1370 and the default value is \"att\".",
1372 add_show_from_set (new_cmd
, &showlist
);