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"
34 #include "arch-utils.h"
38 #include "gdb_assert.h"
40 #include "i386-tdep.h"
42 /* Names of the registers. The first 10 registers match the register
43 numbering scheme used by GCC for stabs and DWARF. */
44 static char *i386_register_names
[] =
46 "eax", "ecx", "edx", "ebx",
47 "esp", "ebp", "esi", "edi",
48 "eip", "eflags", "cs", "ss",
49 "ds", "es", "fs", "gs",
50 "st0", "st1", "st2", "st3",
51 "st4", "st5", "st6", "st7",
52 "fctrl", "fstat", "ftag", "fiseg",
53 "fioff", "foseg", "fooff", "fop",
54 "xmm0", "xmm1", "xmm2", "xmm3",
55 "xmm4", "xmm5", "xmm6", "xmm7",
59 /* Return the name of register REG. */
62 i386_register_name (int reg
)
66 if (reg
>= sizeof (i386_register_names
) / sizeof (*i386_register_names
))
69 return i386_register_names
[reg
];
72 /* Convert stabs register number REG to the appropriate register
73 number used by GDB. */
76 i386_stab_reg_to_regnum (int reg
)
78 /* This implements what GCC calls the "default" register map. */
79 if (reg
>= 0 && reg
<= 7)
81 /* General registers. */
84 else if (reg
>= 12 && reg
<= 19)
86 /* Floating-point registers. */
87 return reg
- 12 + FP0_REGNUM
;
89 else if (reg
>= 21 && reg
<= 28)
92 return reg
- 21 + XMM0_REGNUM
;
94 else if (reg
>= 29 && reg
<= 36)
97 /* FIXME: kettenis/2001-07-28: Should we have the MMX registers
98 as pseudo-registers? */
99 return reg
- 29 + FP0_REGNUM
;
102 /* This will hopefully provoke a warning. */
103 return NUM_REGS
+ NUM_PSEUDO_REGS
;
106 /* Convert DWARF register number REG to the appropriate register
107 number used by GDB. */
110 i386_dwarf_reg_to_regnum (int reg
)
112 /* The DWARF register numbering includes %eip and %eflags, and
113 numbers the floating point registers differently. */
114 if (reg
>= 0 && reg
<= 9)
116 /* General registers. */
119 else if (reg
>= 11 && reg
<= 18)
121 /* Floating-point registers. */
122 return reg
- 11 + FP0_REGNUM
;
126 /* The SSE and MMX registers have identical numbers as in stabs. */
127 return i386_stab_reg_to_regnum (reg
);
130 /* This will hopefully provoke a warning. */
131 return NUM_REGS
+ NUM_PSEUDO_REGS
;
135 /* This is the variable that is set with "set disassembly-flavor", and
136 its legitimate values. */
137 static const char att_flavor
[] = "att";
138 static const char intel_flavor
[] = "intel";
139 static const char *valid_flavors
[] =
145 static const char *disassembly_flavor
= att_flavor
;
147 /* Stdio style buffering was used to minimize calls to ptrace, but
148 this buffering did not take into account that the code section
149 being accessed may not be an even number of buffers long (even if
150 the buffer is only sizeof(int) long). In cases where the code
151 section size happened to be a non-integral number of buffers long,
152 attempting to read the last buffer would fail. Simply using
153 target_read_memory and ignoring errors, rather than read_memory, is
154 not the correct solution, since legitimate access errors would then
155 be totally ignored. To properly handle this situation and continue
156 to use buffering would require that this code be able to determine
157 the minimum code section size granularity (not the alignment of the
158 section itself, since the actual failing case that pointed out this
159 problem had a section alignment of 4 but was not a multiple of 4
160 bytes long), on a target by target basis, and then adjust it's
161 buffer size accordingly. This is messy, but potentially feasible.
162 It probably needs the bfd library's help and support. For now, the
163 buffer size is set to 1. (FIXME -fnf) */
165 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
166 static CORE_ADDR codestream_next_addr
;
167 static CORE_ADDR codestream_addr
;
168 static unsigned char codestream_buf
[CODESTREAM_BUFSIZ
];
169 static int codestream_off
;
170 static int codestream_cnt
;
172 #define codestream_tell() (codestream_addr + codestream_off)
173 #define codestream_peek() \
174 (codestream_cnt == 0 ? \
175 codestream_fill(1) : codestream_buf[codestream_off])
176 #define codestream_get() \
177 (codestream_cnt-- == 0 ? \
178 codestream_fill(0) : codestream_buf[codestream_off++])
181 codestream_fill (int peek_flag
)
183 codestream_addr
= codestream_next_addr
;
184 codestream_next_addr
+= CODESTREAM_BUFSIZ
;
186 codestream_cnt
= CODESTREAM_BUFSIZ
;
187 read_memory (codestream_addr
, (char *) codestream_buf
, CODESTREAM_BUFSIZ
);
190 return (codestream_peek ());
192 return (codestream_get ());
196 codestream_seek (CORE_ADDR place
)
198 codestream_next_addr
= place
/ CODESTREAM_BUFSIZ
;
199 codestream_next_addr
*= CODESTREAM_BUFSIZ
;
202 while (codestream_tell () != place
)
207 codestream_read (unsigned char *buf
, int count
)
212 for (i
= 0; i
< count
; i
++)
213 *p
++ = codestream_get ();
217 /* If the next instruction is a jump, move to its target. */
220 i386_follow_jump (void)
222 unsigned char buf
[4];
228 pos
= codestream_tell ();
231 if (codestream_peek () == 0x66)
237 switch (codestream_get ())
240 /* Relative jump: if data16 == 0, disp32, else disp16. */
243 codestream_read (buf
, 2);
244 delta
= extract_signed_integer (buf
, 2);
246 /* Include the size of the jmp instruction (including the
252 codestream_read (buf
, 4);
253 delta
= extract_signed_integer (buf
, 4);
259 /* Relative jump, disp8 (ignore data16). */
260 codestream_read (buf
, 1);
261 /* Sign-extend it. */
262 delta
= extract_signed_integer (buf
, 1);
267 codestream_seek (pos
);
270 /* Find & return the amount a local space allocated, and advance the
271 codestream to the first register push (if any).
273 If the entry sequence doesn't make sense, return -1, and leave
274 codestream pointer at a random spot. */
277 i386_get_frame_setup (CORE_ADDR pc
)
281 codestream_seek (pc
);
285 op
= codestream_get ();
287 if (op
== 0x58) /* popl %eax */
289 /* This function must start with
292 xchgl %eax, (%esp) 0x87 0x04 0x24
293 or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
295 (the System V compiler puts out the second `xchg'
296 instruction, and the assembler doesn't try to optimize it, so
297 the 'sib' form gets generated). This sequence is used to get
298 the address of the return buffer for a function that returns
301 unsigned char buf
[4];
302 static unsigned char proto1
[3] = { 0x87, 0x04, 0x24 };
303 static unsigned char proto2
[4] = { 0x87, 0x44, 0x24, 0x00 };
305 pos
= codestream_tell ();
306 codestream_read (buf
, 4);
307 if (memcmp (buf
, proto1
, 3) == 0)
309 else if (memcmp (buf
, proto2
, 4) == 0)
312 codestream_seek (pos
);
313 op
= codestream_get (); /* Update next opcode. */
316 if (op
== 0x68 || op
== 0x6a)
318 /* This function may start with
330 unsigned char buf
[8];
332 /* Skip past the `pushl' instruction; it has either a one-byte
333 or a four-byte operand, depending on the opcode. */
334 pos
= codestream_tell ();
339 codestream_seek (pos
);
341 /* Read the following 8 bytes, which should be "call _probe" (6
342 bytes) followed by "addl $4,%esp" (2 bytes). */
343 codestream_read (buf
, sizeof (buf
));
344 if (buf
[0] == 0xe8 && buf
[6] == 0xc4 && buf
[7] == 0x4)
346 codestream_seek (pos
);
347 op
= codestream_get (); /* Update next opcode. */
350 if (op
== 0x55) /* pushl %ebp */
352 /* Check for "movl %esp, %ebp" -- can be written in two ways. */
353 switch (codestream_get ())
356 if (codestream_get () != 0xec)
360 if (codestream_get () != 0xe5)
366 /* Check for stack adjustment
370 NOTE: You can't subtract a 16 bit immediate from a 32 bit
371 reg, so we don't have to worry about a data16 prefix. */
372 op
= codestream_peek ();
375 /* `subl' with 8 bit immediate. */
377 if (codestream_get () != 0xec)
378 /* Some instruction starting with 0x83 other than `subl'. */
380 codestream_seek (codestream_tell () - 2);
383 /* `subl' with signed byte immediate (though it wouldn't
384 make sense to be negative). */
385 return (codestream_get ());
390 /* Maybe it is `subl' with a 32 bit immedediate. */
392 if (codestream_get () != 0xec)
393 /* Some instruction starting with 0x81 other than `subl'. */
395 codestream_seek (codestream_tell () - 2);
398 /* It is `subl' with a 32 bit immediate. */
399 codestream_read ((unsigned char *) buf
, 4);
400 return extract_signed_integer (buf
, 4);
410 /* `enter' with 16 bit unsigned immediate. */
411 codestream_read ((unsigned char *) buf
, 2);
412 codestream_get (); /* Flush final byte of enter instruction. */
413 return extract_unsigned_integer (buf
, 2);
418 /* Signal trampolines don't have a meaningful frame. The frame
419 pointer value we use is actually the frame pointer of the calling
420 frame -- that is, the frame which was in progress when the signal
421 trampoline was entered. GDB mostly treats this frame pointer value
422 as a magic cookie. We detect the case of a signal trampoline by
423 looking at the SIGNAL_HANDLER_CALLER field, which is set based on
426 When a signal trampoline is invoked from a frameless function, we
427 essentially have two frameless functions in a row. In this case,
428 we use the same magic cookie for three frames in a row. We detect
429 this case by seeing whether the next frame has
430 SIGNAL_HANDLER_CALLER set, and, if it does, checking whether the
431 current frame is actually frameless. In this case, we need to get
432 the PC by looking at the SP register value stored in the signal
435 This should work in most cases except in horrible situations where
436 a signal occurs just as we enter a function but before the frame
437 has been set up. Incidentally, that's just what happens when we
438 call a function from GDB with a signal pending (there's a test in
439 the testsuite that makes this happen). Therefore we pretend that
440 we have a frameless function if we're stopped at the start of a
443 /* Return non-zero if we're dealing with a frameless signal, that is,
444 a signal trampoline invoked from a frameless function. */
447 i386_frameless_signal_p (struct frame_info
*frame
)
449 return (frame
->next
&& frame
->next
->signal_handler_caller
450 && (frameless_look_for_prologue (frame
)
451 || frame
->pc
== get_pc_function_start (frame
->pc
)));
454 /* Return the chain-pointer for FRAME. In the case of the i386, the
455 frame's nominal address is the address of a 4-byte word containing
456 the calling frame's address. */
459 i386_frame_chain (struct frame_info
*frame
)
461 if (PC_IN_CALL_DUMMY (frame
->pc
, 0, 0))
464 if (frame
->signal_handler_caller
465 || i386_frameless_signal_p (frame
))
468 if (! inside_entry_file (frame
->pc
))
469 return read_memory_unsigned_integer (frame
->frame
, 4);
474 /* Determine whether the function invocation represented by FRAME does
475 not have a from on the stack associated with it. If it does not,
476 return non-zero, otherwise return zero. */
479 i386_frameless_function_invocation (struct frame_info
*frame
)
481 if (frame
->signal_handler_caller
)
484 return frameless_look_for_prologue (frame
);
487 /* Assuming FRAME is for a sigtramp routine, return the saved program
491 i386_sigtramp_saved_pc (struct frame_info
*frame
)
493 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
496 addr
= tdep
->sigcontext_addr (frame
);
497 return read_memory_unsigned_integer (addr
+ tdep
->sc_pc_offset
, 4);
500 /* Assuming FRAME is for a sigtramp routine, return the saved stack
504 i386_sigtramp_saved_sp (struct frame_info
*frame
)
506 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
509 addr
= tdep
->sigcontext_addr (frame
);
510 return read_memory_unsigned_integer (addr
+ tdep
->sc_sp_offset
, 4);
513 /* Return the saved program counter for FRAME. */
516 i386_frame_saved_pc (struct frame_info
*frame
)
518 if (PC_IN_CALL_DUMMY (frame
->pc
, 0, 0))
519 return generic_read_register_dummy (frame
->pc
, frame
->frame
,
522 if (frame
->signal_handler_caller
)
523 return i386_sigtramp_saved_pc (frame
);
525 if (i386_frameless_signal_p (frame
))
527 CORE_ADDR sp
= i386_sigtramp_saved_sp (frame
->next
);
528 return read_memory_unsigned_integer (sp
, 4);
531 return read_memory_unsigned_integer (frame
->frame
+ 4, 4);
534 /* Immediately after a function call, return the saved pc. */
537 i386_saved_pc_after_call (struct frame_info
*frame
)
539 if (frame
->signal_handler_caller
)
540 return i386_sigtramp_saved_pc (frame
);
542 return read_memory_unsigned_integer (read_register (SP_REGNUM
), 4);
545 /* Return number of args passed to a frame.
546 Can return -1, meaning no way to tell. */
549 i386_frame_num_args (struct frame_info
*fi
)
554 /* This loses because not only might the compiler not be popping the
555 args right after the function call, it might be popping args from
556 both this call and a previous one, and we would say there are
557 more args than there really are. */
561 struct frame_info
*pfi
;
563 /* On the i386, the instruction following the call could be:
565 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
566 anything else - zero args. */
570 frameless
= FRAMELESS_FUNCTION_INVOCATION (fi
);
572 /* In the absence of a frame pointer, GDB doesn't get correct
573 values for nameless arguments. Return -1, so it doesn't print
574 any nameless arguments. */
577 pfi
= get_prev_frame (fi
);
580 /* NOTE: This can happen if we are looking at the frame for
581 main, because FRAME_CHAIN_VALID won't let us go into start.
582 If we have debugging symbols, that's not really a big deal;
583 it just means it will only show as many arguments to main as
590 op
= read_memory_integer (retpc
, 1);
591 if (op
== 0x59) /* pop %ecx */
595 op
= read_memory_integer (retpc
+ 1, 1);
597 /* addl $<signed imm 8 bits>, %esp */
598 return (read_memory_integer (retpc
+ 2, 1) & 0xff) / 4;
602 else if (op
== 0x81) /* `add' with 32 bit immediate. */
604 op
= read_memory_integer (retpc
+ 1, 1);
606 /* addl $<imm 32>, %esp */
607 return read_memory_integer (retpc
+ 2, 4) / 4;
619 /* Parse the first few instructions the function to see what registers
622 We handle these cases:
624 The startup sequence can be at the start of the function, or the
625 function can start with a branch to startup code at the end.
627 %ebp can be set up with either the 'enter' instruction, or "pushl
628 %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
629 once used in the System V compiler).
631 Local space is allocated just below the saved %ebp by either the
632 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a 16
633 bit unsigned argument for space to allocate, and the 'addl'
634 instruction could have either a signed byte, or 32 bit immediate.
636 Next, the registers used by this function are pushed. With the
637 System V compiler they will always be in the order: %edi, %esi,
638 %ebx (and sometimes a harmless bug causes it to also save but not
639 restore %eax); however, the code below is willing to see the pushes
640 in any order, and will handle up to 8 of them.
642 If the setup sequence is at the end of the function, then the next
643 instruction will be a branch back to the start. */
646 i386_frame_init_saved_regs (struct frame_info
*fip
)
657 frame_saved_regs_zalloc (fip
);
659 pc
= get_pc_function_start (fip
->pc
);
661 locals
= i386_get_frame_setup (pc
);
665 addr
= fip
->frame
- 4 - locals
;
666 for (i
= 0; i
< 8; i
++)
668 op
= codestream_get ();
669 if (op
< 0x50 || op
> 0x57)
671 #ifdef I386_REGNO_TO_SYMMETRY
672 /* Dynix uses different internal numbering. Ick. */
673 fip
->saved_regs
[I386_REGNO_TO_SYMMETRY (op
- 0x50)] = addr
;
675 fip
->saved_regs
[op
- 0x50] = addr
;
681 fip
->saved_regs
[PC_REGNUM
] = fip
->frame
+ 4;
682 fip
->saved_regs
[FP_REGNUM
] = fip
->frame
;
685 /* Return PC of first real instruction. */
688 i386_skip_prologue (CORE_ADDR pc
)
692 static unsigned char pic_pat
[6] =
693 { 0xe8, 0, 0, 0, 0, /* call 0x0 */
694 0x5b, /* popl %ebx */
698 if (i386_get_frame_setup (pc
) < 0)
701 /* Found valid frame setup -- codestream now points to start of push
702 instructions for saving registers. */
704 /* Skip over register saves. */
705 for (i
= 0; i
< 8; i
++)
707 op
= codestream_peek ();
708 /* Break if not `pushl' instrunction. */
709 if (op
< 0x50 || op
> 0x57)
714 /* The native cc on SVR4 in -K PIC mode inserts the following code
715 to get the address of the global offset table (GOT) into register
720 movl %ebx,x(%ebp) (optional)
723 This code is with the rest of the prologue (at the end of the
724 function), so we have to skip it to get to the first real
725 instruction at the start of the function. */
727 pos
= codestream_tell ();
728 for (i
= 0; i
< 6; i
++)
730 op
= codestream_get ();
731 if (pic_pat
[i
] != op
)
736 unsigned char buf
[4];
739 op
= codestream_get ();
740 if (op
== 0x89) /* movl %ebx, x(%ebp) */
742 op
= codestream_get ();
743 if (op
== 0x5d) /* One byte offset from %ebp. */
746 codestream_read (buf
, 1);
748 else if (op
== 0x9d) /* Four byte offset from %ebp. */
751 codestream_read (buf
, 4);
753 else /* Unexpected instruction. */
755 op
= codestream_get ();
758 if (delta
> 0 && op
== 0x81 && codestream_get () == 0xc3)
763 codestream_seek (pos
);
767 return (codestream_tell ());
770 /* Use the program counter to determine the contents and size of a
771 breakpoint instruction. Return a pointer to a string of bytes that
772 encode a breakpoint instruction, store the length of the string in
773 *LEN and optionally adjust *PC to point to the correct memory
774 location for inserting the breakpoint.
776 On the i386 we have a single breakpoint that fits in a single byte
777 and can be inserted anywhere. */
779 static const unsigned char *
780 i386_breakpoint_from_pc (CORE_ADDR
*pc
, int *len
)
782 static unsigned char break_insn
[] = { 0xcc }; /* int 3 */
784 *len
= sizeof (break_insn
);
788 /* Push the return address (pointing to the call dummy) onto the stack
789 and return the new value for the stack pointer. */
792 i386_push_return_address (CORE_ADDR pc
, CORE_ADDR sp
)
796 store_unsigned_integer (buf
, 4, CALL_DUMMY_ADDRESS ());
797 write_memory (sp
- 4, buf
, 4);
802 i386_do_pop_frame (struct frame_info
*frame
)
806 char regbuf
[I386_MAX_REGISTER_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 i386_pop_frame (void)
831 generic_pop_current_frame (i386_do_pop_frame
);
835 /* Figure out where the longjmp will land. Slurp the args out of the
836 stack. We expect the first arg to be a pointer to the jmp_buf
837 structure from which we extract the address that we will land at.
838 This address is copied into PC. This routine returns true on
842 i386_get_longjmp_target (CORE_ADDR
*pc
)
845 CORE_ADDR sp
, jb_addr
;
846 int jb_pc_offset
= gdbarch_tdep (current_gdbarch
)->jb_pc_offset
;
848 /* If JB_PC_OFFSET is -1, we have no way to find out where the
849 longjmp will land. */
850 if (jb_pc_offset
== -1)
853 sp
= read_register (SP_REGNUM
);
854 if (target_read_memory (sp
+ 4, buf
, 4))
857 jb_addr
= extract_address (buf
, 4);
858 if (target_read_memory (jb_addr
+ jb_pc_offset
, buf
, 4))
861 *pc
= extract_address (buf
, 4);
867 i386_push_arguments (int nargs
, struct value
**args
, CORE_ADDR sp
,
868 int struct_return
, CORE_ADDR struct_addr
)
870 sp
= default_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
);
877 store_address (buf
, 4, struct_addr
);
878 write_memory (sp
, buf
, 4);
885 i386_store_struct_return (CORE_ADDR addr
, CORE_ADDR sp
)
887 /* Do nothing. Everything was already done by i386_push_arguments. */
890 /* These registers are used for returning integers (and on some
891 targets also for returning `struct' and `union' values when their
892 size and alignment match an integer type). */
893 #define LOW_RETURN_REGNUM 0 /* %eax */
894 #define HIGH_RETURN_REGNUM 2 /* %edx */
896 /* Extract from an array REGBUF containing the (raw) register state, a
897 function return value of TYPE, and copy that, in virtual format,
901 i386_extract_return_value (struct type
*type
, struct regcache
*regcache
,
904 int len
= TYPE_LENGTH (type
);
905 char buf
[I386_MAX_REGISTER_SIZE
];
907 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
908 && TYPE_NFIELDS (type
) == 1)
910 i386_extract_return_value (TYPE_FIELD_TYPE (type
, 0), regcache
, valbuf
);
914 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
918 warning ("Cannot find floating-point return value.");
919 memset (valbuf
, 0, len
);
923 /* Floating-point return values can be found in %st(0). Convert
924 its contents to the desired type. This is probably not
925 exactly how it would happen on the target itself, but it is
926 the best we can do. */
927 regcache_raw_read (regcache
, FP0_REGNUM
, buf
);
928 convert_typed_floating (buf
, builtin_type_i387_ext
, valbuf
, type
);
932 int low_size
= REGISTER_RAW_SIZE (LOW_RETURN_REGNUM
);
933 int high_size
= REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM
);
937 regcache_raw_read (regcache
, LOW_RETURN_REGNUM
, buf
);
938 memcpy (valbuf
, buf
, len
);
940 else if (len
<= (low_size
+ high_size
))
942 regcache_raw_read (regcache
, LOW_RETURN_REGNUM
, buf
);
943 memcpy (valbuf
, buf
, low_size
);
944 regcache_raw_read (regcache
, HIGH_RETURN_REGNUM
, buf
);
945 memcpy (valbuf
+ low_size
, buf
, len
- low_size
);
948 internal_error (__FILE__
, __LINE__
,
949 "Cannot extract return value of %d bytes long.", len
);
953 /* Write into the appropriate registers a function return value stored
954 in VALBUF of type TYPE, given in virtual format. */
957 i386_store_return_value (struct type
*type
, char *valbuf
)
959 int len
= TYPE_LENGTH (type
);
961 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
962 && TYPE_NFIELDS (type
) == 1)
964 i386_store_return_value (TYPE_FIELD_TYPE (type
, 0), valbuf
);
968 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
971 char buf
[FPU_REG_RAW_SIZE
];
975 warning ("Cannot set floating-point return value.");
979 /* Returning floating-point values is a bit tricky. Apart from
980 storing the return value in %st(0), we have to simulate the
981 state of the FPU at function return point. */
983 /* Convert the value found in VALBUF to the extended
984 floating-point format used by the FPU. This is probably
985 not exactly how it would happen on the target itself, but
986 it is the best we can do. */
987 convert_typed_floating (valbuf
, type
, buf
, builtin_type_i387_ext
);
988 write_register_bytes (REGISTER_BYTE (FP0_REGNUM
), buf
,
991 /* Set the top of the floating-point register stack to 7. The
992 actual value doesn't really matter, but 7 is what a normal
993 function return would end up with if the program started out
994 with a freshly initialized FPU. */
995 fstat
= read_register (FSTAT_REGNUM
);
997 write_register (FSTAT_REGNUM
, fstat
);
999 /* Mark %st(1) through %st(7) as empty. Since we set the top of
1000 the floating-point register stack to 7, the appropriate value
1001 for the tag word is 0x3fff. */
1002 write_register (FTAG_REGNUM
, 0x3fff);
1006 int low_size
= REGISTER_RAW_SIZE (LOW_RETURN_REGNUM
);
1007 int high_size
= REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM
);
1009 if (len
<= low_size
)
1010 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM
), valbuf
, len
);
1011 else if (len
<= (low_size
+ high_size
))
1013 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM
),
1015 write_register_bytes (REGISTER_BYTE (HIGH_RETURN_REGNUM
),
1016 valbuf
+ low_size
, len
- low_size
);
1019 internal_error (__FILE__
, __LINE__
,
1020 "Cannot store return value of %d bytes long.", len
);
1024 /* Extract from an array REGBUF containing the (raw) register state
1025 the address in which a function should return its structure value,
1029 i386_extract_struct_value_address (struct regcache
*regcache
)
1031 return regcache_raw_read_as_address (regcache
, LOW_RETURN_REGNUM
);
1035 /* This is the variable that is set with "set struct-convention", and
1036 its legitimate values. */
1037 static const char default_struct_convention
[] = "default";
1038 static const char pcc_struct_convention
[] = "pcc";
1039 static const char reg_struct_convention
[] = "reg";
1040 static const char *valid_conventions
[] =
1042 default_struct_convention
,
1043 pcc_struct_convention
,
1044 reg_struct_convention
,
1047 static const char *struct_convention
= default_struct_convention
;
1050 i386_use_struct_convention (int gcc_p
, struct type
*type
)
1052 enum struct_return struct_return
;
1054 if (struct_convention
== default_struct_convention
)
1055 struct_return
= gdbarch_tdep (current_gdbarch
)->struct_return
;
1056 else if (struct_convention
== pcc_struct_convention
)
1057 struct_return
= pcc_struct_return
;
1059 struct_return
= reg_struct_return
;
1061 return generic_use_struct_convention (struct_return
== reg_struct_return
,
1066 /* Return the GDB type object for the "standard" data type of data in
1067 register REGNUM. Perhaps %esi and %edi should go here, but
1068 potentially they could be used for things other than address. */
1070 static struct type
*
1071 i386_register_virtual_type (int regnum
)
1073 if (regnum
== PC_REGNUM
|| regnum
== FP_REGNUM
|| regnum
== SP_REGNUM
)
1074 return lookup_pointer_type (builtin_type_void
);
1076 if (IS_FP_REGNUM (regnum
))
1077 return builtin_type_i387_ext
;
1079 if (IS_SSE_REGNUM (regnum
))
1080 return builtin_type_vec128i
;
1082 return builtin_type_int
;
1085 /* Return true iff register REGNUM's virtual format is different from
1086 its raw format. Note that this definition assumes that the host
1087 supports IEEE 32-bit floats, since it doesn't say that SSE
1088 registers need conversion. Even if we can't find a counterexample,
1089 this is still sloppy. */
1092 i386_register_convertible (int regnum
)
1094 return IS_FP_REGNUM (regnum
);
1097 /* Convert data from raw format for register REGNUM in buffer FROM to
1098 virtual format with type TYPE in buffer TO. */
1101 i386_register_convert_to_virtual (int regnum
, struct type
*type
,
1102 char *from
, char *to
)
1104 gdb_assert (IS_FP_REGNUM (regnum
));
1106 /* We only support floating-point values. */
1107 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1109 warning ("Cannot convert floating-point register value "
1110 "to non-floating-point type.");
1111 memset (to
, 0, TYPE_LENGTH (type
));
1115 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
1116 the extended floating-point format used by the FPU. */
1117 convert_typed_floating (from
, builtin_type_i387_ext
, to
, type
);
1120 /* Convert data from virtual format with type TYPE in buffer FROM to
1121 raw format for register REGNUM in buffer TO. */
1124 i386_register_convert_to_raw (struct type
*type
, int regnum
,
1125 char *from
, char *to
)
1127 gdb_assert (IS_FP_REGNUM (regnum
));
1129 /* We only support floating-point values. */
1130 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
1132 warning ("Cannot convert non-floating-point type "
1133 "to floating-point register value.");
1134 memset (to
, 0, TYPE_LENGTH (type
));
1138 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
1139 to the extended floating-point format used by the FPU. */
1140 convert_typed_floating (from
, type
, to
, builtin_type_i387_ext
);
1144 #ifdef STATIC_TRANSFORM_NAME
1145 /* SunPRO encodes the static variables. This is not related to C++
1146 mangling, it is done for C too. */
1149 sunpro_static_transform_name (char *name
)
1152 if (IS_STATIC_TRANSFORM_NAME (name
))
1154 /* For file-local statics there will be a period, a bunch of
1155 junk (the contents of which match a string given in the
1156 N_OPT), a period and the name. For function-local statics
1157 there will be a bunch of junk (which seems to change the
1158 second character from 'A' to 'B'), a period, the name of the
1159 function, and the name. So just skip everything before the
1161 p
= strrchr (name
, '.');
1167 #endif /* STATIC_TRANSFORM_NAME */
1170 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
1173 skip_trampoline_code (CORE_ADDR pc
, char *name
)
1175 if (pc
&& read_memory_unsigned_integer (pc
, 2) == 0x25ff) /* jmp *(dest) */
1177 unsigned long indirect
= read_memory_unsigned_integer (pc
+ 2, 4);
1178 struct minimal_symbol
*indsym
=
1179 indirect
? lookup_minimal_symbol_by_pc (indirect
) : 0;
1180 char *symname
= indsym
? SYMBOL_NAME (indsym
) : 0;
1184 if (strncmp (symname
, "__imp_", 6) == 0
1185 || strncmp (symname
, "_imp_", 5) == 0)
1186 return name
? 1 : read_memory_unsigned_integer (indirect
, 4);
1189 return 0; /* Not a trampoline. */
1193 /* Return non-zero if PC and NAME show that we are in a signal
1197 i386_pc_in_sigtramp (CORE_ADDR pc
, char *name
)
1199 return (name
&& strcmp ("_sigtramp", name
) == 0);
1203 /* We have two flavours of disassembly. The machinery on this page
1204 deals with switching between those. */
1207 gdb_print_insn_i386 (bfd_vma memaddr
, disassemble_info
*info
)
1209 if (disassembly_flavor
== att_flavor
)
1210 return print_insn_i386_att (memaddr
, info
);
1211 else if (disassembly_flavor
== intel_flavor
)
1212 return print_insn_i386_intel (memaddr
, info
);
1213 /* Never reached -- disassembly_flavour is always either att_flavor
1215 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
1219 /* There are a few i386 architecture variants that differ only
1220 slightly from the generic i386 target. For now, we don't give them
1221 their own source file, but include them here. As a consequence,
1222 they'll always be included. */
1224 /* System V Release 4 (SVR4). */
1227 i386_svr4_pc_in_sigtramp (CORE_ADDR pc
, char *name
)
1229 return (name
&& (strcmp ("_sigreturn", name
) == 0
1230 || strcmp ("_sigacthandler", name
) == 0
1231 || strcmp ("sigvechandler", name
) == 0));
1234 /* Get address of the pushed ucontext (sigcontext) on the stack for
1235 all three variants of SVR4 sigtramps. */
1238 i386_svr4_sigcontext_addr (struct frame_info
*frame
)
1240 int sigcontext_offset
= -1;
1243 find_pc_partial_function (frame
->pc
, &name
, NULL
, NULL
);
1246 if (strcmp (name
, "_sigreturn") == 0)
1247 sigcontext_offset
= 132;
1248 else if (strcmp (name
, "_sigacthandler") == 0)
1249 sigcontext_offset
= 80;
1250 else if (strcmp (name
, "sigvechandler") == 0)
1251 sigcontext_offset
= 120;
1254 gdb_assert (sigcontext_offset
!= -1);
1257 return frame
->next
->frame
+ sigcontext_offset
;
1258 return read_register (SP_REGNUM
) + sigcontext_offset
;
1265 i386_go32_pc_in_sigtramp (CORE_ADDR pc
, char *name
)
1267 /* DJGPP doesn't have any special frames for signal handlers. */
1275 i386_elf_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1277 /* We typically use stabs-in-ELF with the DWARF register numbering. */
1278 set_gdbarch_stab_reg_to_regnum (gdbarch
, i386_dwarf_reg_to_regnum
);
1281 /* System V Release 4 (SVR4). */
1284 i386_svr4_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1286 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1288 /* System V Release 4 uses ELF. */
1289 i386_elf_init_abi (info
, gdbarch
);
1291 /* FIXME: kettenis/20020511: Why do we override this function here? */
1292 set_gdbarch_frame_chain_valid (gdbarch
, func_frame_chain_valid
);
1294 set_gdbarch_pc_in_sigtramp (gdbarch
, i386_svr4_pc_in_sigtramp
);
1295 tdep
->sigcontext_addr
= i386_svr4_sigcontext_addr
;
1296 tdep
->sc_pc_offset
= 14 * 4;
1297 tdep
->sc_sp_offset
= 7 * 4;
1299 tdep
->jb_pc_offset
= 20;
1305 i386_go32_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1307 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1309 set_gdbarch_pc_in_sigtramp (gdbarch
, i386_go32_pc_in_sigtramp
);
1311 tdep
->jb_pc_offset
= 36;
1317 i386_nw_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1319 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1321 /* FIXME: kettenis/20020511: Why do we override this function here? */
1322 set_gdbarch_frame_chain_valid (gdbarch
, func_frame_chain_valid
);
1324 tdep
->jb_pc_offset
= 24;
1328 static struct gdbarch
*
1329 i386_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
1331 struct gdbarch_tdep
*tdep
;
1332 struct gdbarch
*gdbarch
;
1333 enum gdb_osabi osabi
= GDB_OSABI_UNKNOWN
;
1335 /* Try to determine the OS ABI of the object we're loading. */
1336 if (info
.abfd
!= NULL
)
1337 osabi
= gdbarch_lookup_osabi (info
.abfd
);
1339 /* Find a candidate among extant architectures. */
1340 for (arches
= gdbarch_list_lookup_by_info (arches
, &info
);
1342 arches
= gdbarch_list_lookup_by_info (arches
->next
, &info
))
1344 /* Make sure the OS ABI selection matches. */
1345 tdep
= gdbarch_tdep (arches
->gdbarch
);
1346 if (tdep
&& tdep
->osabi
== osabi
)
1347 return arches
->gdbarch
;
1350 /* Allocate space for the new architecture. */
1351 tdep
= XMALLOC (struct gdbarch_tdep
);
1352 gdbarch
= gdbarch_alloc (&info
, tdep
);
1354 tdep
->osabi
= osabi
;
1356 /* The i386 default settings don't include the SSE registers.
1357 FIXME: kettenis/20020614: They do include the FPU registers for
1358 now, which probably is not quite right. */
1359 tdep
->num_xmm_regs
= 0;
1361 tdep
->jb_pc_offset
= -1;
1362 tdep
->struct_return
= pcc_struct_return
;
1363 tdep
->sigtramp_start
= 0;
1364 tdep
->sigtramp_end
= 0;
1365 tdep
->sigcontext_addr
= NULL
;
1366 tdep
->sc_pc_offset
= -1;
1367 tdep
->sc_sp_offset
= -1;
1369 /* The format used for `long double' on almost all i386 targets is
1370 the i387 extended floating-point format. In fact, of all targets
1371 in the GCC 2.95 tree, only OSF/1 does it different, and insists
1372 on having a `long double' that's not `long' at all. */
1373 set_gdbarch_long_double_format (gdbarch
, &floatformat_i387_ext
);
1375 /* Although the i386 extended floating-point has only 80 significant
1376 bits, a `long double' actually takes up 96, probably to enforce
1378 set_gdbarch_long_double_bit (gdbarch
, 96);
1380 /* NOTE: tm-i386aix.h, tm-i386bsd.h, tm-i386os9k.h, tm-ptx.h,
1381 tm-symmetry.h currently override this. Sigh. */
1382 set_gdbarch_num_regs (gdbarch
, I386_NUM_GREGS
+ I386_NUM_FREGS
);
1384 set_gdbarch_sp_regnum (gdbarch
, 4);
1385 set_gdbarch_fp_regnum (gdbarch
, 5);
1386 set_gdbarch_pc_regnum (gdbarch
, 8);
1387 set_gdbarch_ps_regnum (gdbarch
, 9);
1388 set_gdbarch_fp0_regnum (gdbarch
, 16);
1390 /* Use the "default" register numbering scheme for stabs and COFF. */
1391 set_gdbarch_stab_reg_to_regnum (gdbarch
, i386_stab_reg_to_regnum
);
1392 set_gdbarch_sdb_reg_to_regnum (gdbarch
, i386_stab_reg_to_regnum
);
1394 /* Use the DWARF register numbering scheme for DWARF and DWARF 2. */
1395 set_gdbarch_dwarf_reg_to_regnum (gdbarch
, i386_dwarf_reg_to_regnum
);
1396 set_gdbarch_dwarf2_reg_to_regnum (gdbarch
, i386_dwarf_reg_to_regnum
);
1398 /* We don't define ECOFF_REG_TO_REGNUM, since ECOFF doesn't seem to
1399 be in use on any of the supported i386 targets. */
1401 set_gdbarch_register_name (gdbarch
, i386_register_name
);
1402 set_gdbarch_register_size (gdbarch
, 4);
1403 set_gdbarch_register_bytes (gdbarch
, I386_SIZEOF_GREGS
+ I386_SIZEOF_FREGS
);
1404 set_gdbarch_max_register_raw_size (gdbarch
, I386_MAX_REGISTER_SIZE
);
1405 set_gdbarch_max_register_virtual_size (gdbarch
, I386_MAX_REGISTER_SIZE
);
1406 set_gdbarch_register_virtual_type (gdbarch
, i386_register_virtual_type
);
1408 set_gdbarch_get_longjmp_target (gdbarch
, i386_get_longjmp_target
);
1410 set_gdbarch_use_generic_dummy_frames (gdbarch
, 1);
1412 /* Call dummy code. */
1413 set_gdbarch_call_dummy_location (gdbarch
, AT_ENTRY_POINT
);
1414 set_gdbarch_call_dummy_address (gdbarch
, entry_point_address
);
1415 set_gdbarch_call_dummy_start_offset (gdbarch
, 0);
1416 set_gdbarch_call_dummy_breakpoint_offset (gdbarch
, 0);
1417 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch
, 1);
1418 set_gdbarch_call_dummy_length (gdbarch
, 0);
1419 set_gdbarch_call_dummy_p (gdbarch
, 1);
1420 set_gdbarch_call_dummy_words (gdbarch
, NULL
);
1421 set_gdbarch_sizeof_call_dummy_words (gdbarch
, 0);
1422 set_gdbarch_call_dummy_stack_adjust_p (gdbarch
, 0);
1423 set_gdbarch_fix_call_dummy (gdbarch
, generic_fix_call_dummy
);
1425 set_gdbarch_register_convertible (gdbarch
, i386_register_convertible
);
1426 set_gdbarch_register_convert_to_virtual (gdbarch
,
1427 i386_register_convert_to_virtual
);
1428 set_gdbarch_register_convert_to_raw (gdbarch
, i386_register_convert_to_raw
);
1430 set_gdbarch_get_saved_register (gdbarch
, generic_get_saved_register
);
1431 set_gdbarch_push_arguments (gdbarch
, i386_push_arguments
);
1433 set_gdbarch_pc_in_call_dummy (gdbarch
, pc_in_call_dummy_at_entry_point
);
1435 /* "An argument's size is increased, if necessary, to make it a
1436 multiple of [32-bit] words. This may require tail padding,
1437 depending on the size of the argument" -- from the x86 ABI. */
1438 set_gdbarch_parm_boundary (gdbarch
, 32);
1440 set_gdbarch_extract_return_value (gdbarch
, i386_extract_return_value
);
1441 set_gdbarch_push_arguments (gdbarch
, i386_push_arguments
);
1442 set_gdbarch_push_dummy_frame (gdbarch
, generic_push_dummy_frame
);
1443 set_gdbarch_push_return_address (gdbarch
, i386_push_return_address
);
1444 set_gdbarch_pop_frame (gdbarch
, i386_pop_frame
);
1445 set_gdbarch_store_struct_return (gdbarch
, i386_store_struct_return
);
1446 set_gdbarch_store_return_value (gdbarch
, i386_store_return_value
);
1447 set_gdbarch_extract_struct_value_address (gdbarch
,
1448 i386_extract_struct_value_address
);
1449 set_gdbarch_use_struct_convention (gdbarch
, i386_use_struct_convention
);
1451 set_gdbarch_frame_init_saved_regs (gdbarch
, i386_frame_init_saved_regs
);
1452 set_gdbarch_skip_prologue (gdbarch
, i386_skip_prologue
);
1454 /* Stack grows downward. */
1455 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
1457 set_gdbarch_breakpoint_from_pc (gdbarch
, i386_breakpoint_from_pc
);
1458 set_gdbarch_decr_pc_after_break (gdbarch
, 1);
1459 set_gdbarch_function_start_offset (gdbarch
, 0);
1461 /* The following redefines make backtracing through sigtramp work.
1462 They manufacture a fake sigtramp frame and obtain the saved pc in
1463 sigtramp from the sigcontext structure which is pushed by the
1464 kernel on the user stack, along with a pointer to it. */
1466 set_gdbarch_frame_args_skip (gdbarch
, 8);
1467 set_gdbarch_frameless_function_invocation (gdbarch
,
1468 i386_frameless_function_invocation
);
1469 set_gdbarch_frame_chain (gdbarch
, i386_frame_chain
);
1470 set_gdbarch_frame_chain_valid (gdbarch
, generic_file_frame_chain_valid
);
1471 set_gdbarch_frame_saved_pc (gdbarch
, i386_frame_saved_pc
);
1472 set_gdbarch_frame_args_address (gdbarch
, default_frame_address
);
1473 set_gdbarch_frame_locals_address (gdbarch
, default_frame_address
);
1474 set_gdbarch_saved_pc_after_call (gdbarch
, i386_saved_pc_after_call
);
1475 set_gdbarch_frame_num_args (gdbarch
, i386_frame_num_args
);
1476 set_gdbarch_pc_in_sigtramp (gdbarch
, i386_pc_in_sigtramp
);
1478 /* Hook in ABI-specific overrides, if they have been registered. */
1479 gdbarch_init_osabi (info
, gdbarch
, osabi
);
1484 static enum gdb_osabi
1485 i386_coff_osabi_sniffer (bfd
*abfd
)
1487 if (strcmp (bfd_get_target (abfd
), "coff-go32-exe") == 0
1488 || strcmp (bfd_get_target (abfd
), "coff-go32") == 0)
1489 return GDB_OSABI_GO32
;
1491 return GDB_OSABI_UNKNOWN
;
1494 static enum gdb_osabi
1495 i386_nlm_osabi_sniffer (bfd
*abfd
)
1497 return GDB_OSABI_NETWARE
;
1501 /* Provide a prototype to silence -Wmissing-prototypes. */
1502 void _initialize_i386_tdep (void);
1505 _initialize_i386_tdep (void)
1507 register_gdbarch_init (bfd_arch_i386
, i386_gdbarch_init
);
1509 tm_print_insn
= gdb_print_insn_i386
;
1510 tm_print_insn_info
.mach
= bfd_lookup_arch (bfd_arch_i386
, 0)->mach
;
1512 /* Add the variable that controls the disassembly flavor. */
1514 struct cmd_list_element
*new_cmd
;
1516 new_cmd
= add_set_enum_cmd ("disassembly-flavor", no_class
,
1518 &disassembly_flavor
,
1520 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1521 and the default value is \"att\".",
1523 add_show_from_set (new_cmd
, &showlist
);
1526 /* Add the variable that controls the convention for returning
1529 struct cmd_list_element
*new_cmd
;
1531 new_cmd
= add_set_enum_cmd ("struct-convention", no_class
,
1533 &struct_convention
, "\
1534 Set the convention for returning small structs, valid values \
1535 are \"default\", \"pcc\" and \"reg\", and the default value is \"default\".",
1537 add_show_from_set (new_cmd
, &showlist
);
1540 gdbarch_register_osabi_sniffer (bfd_arch_i386
, bfd_target_coff_flavour
,
1541 i386_coff_osabi_sniffer
);
1542 gdbarch_register_osabi_sniffer (bfd_arch_i386
, bfd_target_nlm_flavour
,
1543 i386_nlm_osabi_sniffer
);
1545 gdbarch_register_osabi (bfd_arch_i386
, GDB_OSABI_SVR4
,
1546 i386_svr4_init_abi
);
1547 gdbarch_register_osabi (bfd_arch_i386
, GDB_OSABI_GO32
,
1548 i386_go32_init_abi
);
1549 gdbarch_register_osabi (bfd_arch_i386
, GDB_OSABI_NETWARE
,