1 /* Intel 386 target-dependent stuff.
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "opcode/i386.h"
24 #include "arch-utils.h"
26 #include "dummy-frame.h"
27 #include "dwarf2-frame.h"
30 #include "frame-base.h"
31 #include "frame-unwind.h"
39 #include "reggroups.h"
47 #include "gdb_assert.h"
48 #include "gdb_string.h"
50 #include "i386-tdep.h"
51 #include "i387-tdep.h"
55 static char *i386_register_names[] =
57 "eax", "ecx", "edx", "ebx",
58 "esp", "ebp", "esi", "edi",
59 "eip", "eflags", "cs", "ss",
60 "ds", "es", "fs", "gs",
61 "st0", "st1", "st2", "st3",
62 "st4", "st5", "st6", "st7",
63 "fctrl", "fstat", "ftag", "fiseg",
64 "fioff", "foseg", "fooff", "fop",
65 "xmm0", "xmm1", "xmm2", "xmm3",
66 "xmm4", "xmm5", "xmm6", "xmm7",
70 static const int i386_num_register_names = ARRAY_SIZE (i386_register_names);
72 /* Register names for MMX pseudo-registers. */
74 static char *i386_mmx_names[] =
76 "mm0", "mm1", "mm2", "mm3",
77 "mm4", "mm5", "mm6", "mm7"
80 static const int i386_num_mmx_regs = ARRAY_SIZE (i386_mmx_names);
83 i386_mmx_regnum_p (struct gdbarch *gdbarch, int regnum)
85 int mm0_regnum = gdbarch_tdep (gdbarch)->mm0_regnum;
90 return (regnum >= mm0_regnum && regnum < mm0_regnum + i386_num_mmx_regs);
96 i386_sse_regnum_p (struct gdbarch *gdbarch, int regnum)
98 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
100 if (I387_NUM_XMM_REGS (tdep) == 0)
103 return (I387_XMM0_REGNUM (tdep) <= regnum
104 && regnum < I387_MXCSR_REGNUM (tdep));
108 i386_mxcsr_regnum_p (struct gdbarch *gdbarch, int regnum)
110 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
112 if (I387_NUM_XMM_REGS (tdep) == 0)
115 return (regnum == I387_MXCSR_REGNUM (tdep));
121 i386_fp_regnum_p (struct gdbarch *gdbarch, int regnum)
123 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
125 if (I387_ST0_REGNUM (tdep) < 0)
128 return (I387_ST0_REGNUM (tdep) <= regnum
129 && regnum < I387_FCTRL_REGNUM (tdep));
133 i386_fpc_regnum_p (struct gdbarch *gdbarch, int regnum)
135 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
137 if (I387_ST0_REGNUM (tdep) < 0)
140 return (I387_FCTRL_REGNUM (tdep) <= regnum
141 && regnum < I387_XMM0_REGNUM (tdep));
144 /* Return the name of register REGNUM. */
147 i386_register_name (struct gdbarch *gdbarch, int regnum)
149 if (i386_mmx_regnum_p (gdbarch, regnum))
150 return i386_mmx_names[regnum - I387_MM0_REGNUM (gdbarch_tdep (gdbarch))];
152 if (regnum >= 0 && regnum < i386_num_register_names)
153 return i386_register_names[regnum];
158 /* Convert a dbx register number REG to the appropriate register
159 number used by GDB. */
162 i386_dbx_reg_to_regnum (struct gdbarch *gdbarch, int reg)
164 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
166 /* This implements what GCC calls the "default" register map
167 (dbx_register_map[]). */
169 if (reg >= 0 && reg <= 7)
171 /* General-purpose registers. The debug info calls %ebp
172 register 4, and %esp register 5. */
179 else if (reg >= 12 && reg <= 19)
181 /* Floating-point registers. */
182 return reg - 12 + I387_ST0_REGNUM (tdep);
184 else if (reg >= 21 && reg <= 28)
187 return reg - 21 + I387_XMM0_REGNUM (tdep);
189 else if (reg >= 29 && reg <= 36)
192 return reg - 29 + I387_MM0_REGNUM (tdep);
195 /* This will hopefully provoke a warning. */
196 return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
199 /* Convert SVR4 register number REG to the appropriate register number
203 i386_svr4_reg_to_regnum (struct gdbarch *gdbarch, int reg)
205 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
207 /* This implements the GCC register map that tries to be compatible
208 with the SVR4 C compiler for DWARF (svr4_dbx_register_map[]). */
210 /* The SVR4 register numbering includes %eip and %eflags, and
211 numbers the floating point registers differently. */
212 if (reg >= 0 && reg <= 9)
214 /* General-purpose registers. */
217 else if (reg >= 11 && reg <= 18)
219 /* Floating-point registers. */
220 return reg - 11 + I387_ST0_REGNUM (tdep);
222 else if (reg >= 21 && reg <= 36)
224 /* The SSE and MMX registers have the same numbers as with dbx. */
225 return i386_dbx_reg_to_regnum (gdbarch, reg);
230 case 37: return I387_FCTRL_REGNUM (tdep);
231 case 38: return I387_FSTAT_REGNUM (tdep);
232 case 39: return I387_MXCSR_REGNUM (tdep);
233 case 40: return I386_ES_REGNUM;
234 case 41: return I386_CS_REGNUM;
235 case 42: return I386_SS_REGNUM;
236 case 43: return I386_DS_REGNUM;
237 case 44: return I386_FS_REGNUM;
238 case 45: return I386_GS_REGNUM;
241 /* This will hopefully provoke a warning. */
242 return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
247 /* This is the variable that is set with "set disassembly-flavor", and
248 its legitimate values. */
249 static const char att_flavor[] = "att";
250 static const char intel_flavor[] = "intel";
251 static const char *valid_flavors[] =
257 static const char *disassembly_flavor = att_flavor;
260 /* Use the program counter to determine the contents and size of a
261 breakpoint instruction. Return a pointer to a string of bytes that
262 encode a breakpoint instruction, store the length of the string in
263 *LEN and optionally adjust *PC to point to the correct memory
264 location for inserting the breakpoint.
266 On the i386 we have a single breakpoint that fits in a single byte
267 and can be inserted anywhere.
269 This function is 64-bit safe. */
271 static const gdb_byte *
272 i386_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
274 static gdb_byte break_insn[] = { 0xcc }; /* int 3 */
276 *len = sizeof (break_insn);
280 /* Displaced instruction handling. */
282 /* Skip the legacy instruction prefixes in INSN.
283 Not all prefixes are valid for any particular insn
284 but we needn't care, the insn will fault if it's invalid.
285 The result is a pointer to the first opcode byte,
286 or NULL if we run off the end of the buffer. */
289 i386_skip_prefixes (gdb_byte *insn, size_t max_len)
291 gdb_byte *end = insn + max_len;
297 case DATA_PREFIX_OPCODE:
298 case ADDR_PREFIX_OPCODE:
299 case CS_PREFIX_OPCODE:
300 case DS_PREFIX_OPCODE:
301 case ES_PREFIX_OPCODE:
302 case FS_PREFIX_OPCODE:
303 case GS_PREFIX_OPCODE:
304 case SS_PREFIX_OPCODE:
305 case LOCK_PREFIX_OPCODE:
306 case REPE_PREFIX_OPCODE:
307 case REPNE_PREFIX_OPCODE:
319 i386_absolute_jmp_p (const gdb_byte *insn)
321 /* jmp far (absolute address in operand) */
327 /* jump near, absolute indirect (/4) */
328 if ((insn[1] & 0x38) == 0x20)
331 /* jump far, absolute indirect (/5) */
332 if ((insn[1] & 0x38) == 0x28)
340 i386_absolute_call_p (const gdb_byte *insn)
342 /* call far, absolute */
348 /* Call near, absolute indirect (/2) */
349 if ((insn[1] & 0x38) == 0x10)
352 /* Call far, absolute indirect (/3) */
353 if ((insn[1] & 0x38) == 0x18)
361 i386_ret_p (const gdb_byte *insn)
365 case 0xc2: /* ret near, pop N bytes */
366 case 0xc3: /* ret near */
367 case 0xca: /* ret far, pop N bytes */
368 case 0xcb: /* ret far */
369 case 0xcf: /* iret */
378 i386_call_p (const gdb_byte *insn)
380 if (i386_absolute_call_p (insn))
383 /* call near, relative */
390 /* Return non-zero if INSN is a system call, and set *LENGTHP to its
391 length in bytes. Otherwise, return zero. */
394 i386_syscall_p (const gdb_byte *insn, ULONGEST *lengthp)
405 /* Fix up the state of registers and memory after having single-stepped
406 a displaced instruction. */
409 i386_displaced_step_fixup (struct gdbarch *gdbarch,
410 struct displaced_step_closure *closure,
411 CORE_ADDR from, CORE_ADDR to,
412 struct regcache *regs)
414 /* The offset we applied to the instruction's address.
415 This could well be negative (when viewed as a signed 32-bit
416 value), but ULONGEST won't reflect that, so take care when
418 ULONGEST insn_offset = to - from;
420 /* Since we use simple_displaced_step_copy_insn, our closure is a
421 copy of the instruction. */
422 gdb_byte *insn = (gdb_byte *) closure;
423 /* The start of the insn, needed in case we see some prefixes. */
424 gdb_byte *insn_start = insn;
427 fprintf_unfiltered (gdb_stdlog,
428 "displaced: fixup (0x%s, 0x%s), "
429 "insn = 0x%02x 0x%02x ...\n",
430 paddr_nz (from), paddr_nz (to), insn[0], insn[1]);
432 /* The list of issues to contend with here is taken from
433 resume_execution in arch/i386/kernel/kprobes.c, Linux 2.6.20.
434 Yay for Free Software! */
436 /* Relocate the %eip, if necessary. */
438 /* The instruction recognizers we use assume any leading prefixes
439 have been skipped. */
441 /* This is the size of the buffer in closure. */
442 size_t max_insn_len = gdbarch_max_insn_length (gdbarch);
443 gdb_byte *opcode = i386_skip_prefixes (insn, max_insn_len);
444 /* If there are too many prefixes, just ignore the insn.
445 It will fault when run. */
450 /* Except in the case of absolute or indirect jump or call
451 instructions, or a return instruction, the new eip is relative to
452 the displaced instruction; make it relative. Well, signal
453 handler returns don't need relocation either, but we use the
454 value of %eip to recognize those; see below. */
455 if (! i386_absolute_jmp_p (insn)
456 && ! i386_absolute_call_p (insn)
457 && ! i386_ret_p (insn))
462 regcache_cooked_read_unsigned (regs, I386_EIP_REGNUM, &orig_eip);
464 /* A signal trampoline system call changes the %eip, resuming
465 execution of the main program after the signal handler has
466 returned. That makes them like 'return' instructions; we
467 shouldn't relocate %eip.
469 But most system calls don't, and we do need to relocate %eip.
471 Our heuristic for distinguishing these cases: if stepping
472 over the system call instruction left control directly after
473 the instruction, the we relocate --- control almost certainly
474 doesn't belong in the displaced copy. Otherwise, we assume
475 the instruction has put control where it belongs, and leave
476 it unrelocated. Goodness help us if there are PC-relative
478 if (i386_syscall_p (insn, &insn_len)
479 && orig_eip != to + (insn - insn_start) + insn_len)
482 fprintf_unfiltered (gdb_stdlog,
483 "displaced: syscall changed %%eip; "
488 ULONGEST eip = (orig_eip - insn_offset) & 0xffffffffUL;
490 /* If we just stepped over a breakpoint insn, we don't backup
491 the pc on purpose; this is to match behaviour without
494 regcache_cooked_write_unsigned (regs, I386_EIP_REGNUM, eip);
497 fprintf_unfiltered (gdb_stdlog,
499 "relocated %%eip from 0x%s to 0x%s\n",
500 paddr_nz (orig_eip), paddr_nz (eip));
504 /* If the instruction was PUSHFL, then the TF bit will be set in the
505 pushed value, and should be cleared. We'll leave this for later,
506 since GDB already messes up the TF flag when stepping over a
509 /* If the instruction was a call, the return address now atop the
510 stack is the address following the copied instruction. We need
511 to make it the address following the original instruction. */
512 if (i386_call_p (insn))
516 const ULONGEST retaddr_len = 4;
518 regcache_cooked_read_unsigned (regs, I386_ESP_REGNUM, &esp);
519 retaddr = read_memory_unsigned_integer (esp, retaddr_len);
520 retaddr = (retaddr - insn_offset) & 0xffffffffUL;
521 write_memory_unsigned_integer (esp, retaddr_len, retaddr);
524 fprintf_unfiltered (gdb_stdlog,
525 "displaced: relocated return addr at 0x%s "
532 #ifdef I386_REGNO_TO_SYMMETRY
533 #error "The Sequent Symmetry is no longer supported."
536 /* According to the System V ABI, the registers %ebp, %ebx, %edi, %esi
537 and %esp "belong" to the calling function. Therefore these
538 registers should be saved if they're going to be modified. */
540 /* The maximum number of saved registers. This should include all
541 registers mentioned above, and %eip. */
542 #define I386_NUM_SAVED_REGS I386_NUM_GREGS
544 struct i386_frame_cache
551 /* Saved registers. */
552 CORE_ADDR saved_regs[I386_NUM_SAVED_REGS];
557 /* Stack space reserved for local variables. */
561 /* Allocate and initialize a frame cache. */
563 static struct i386_frame_cache *
564 i386_alloc_frame_cache (void)
566 struct i386_frame_cache *cache;
569 cache = FRAME_OBSTACK_ZALLOC (struct i386_frame_cache);
573 cache->sp_offset = -4;
576 /* Saved registers. We initialize these to -1 since zero is a valid
577 offset (that's where %ebp is supposed to be stored). */
578 for (i = 0; i < I386_NUM_SAVED_REGS; i++)
579 cache->saved_regs[i] = -1;
581 cache->saved_sp_reg = -1;
582 cache->pc_in_eax = 0;
584 /* Frameless until proven otherwise. */
590 /* If the instruction at PC is a jump, return the address of its
591 target. Otherwise, return PC. */
594 i386_follow_jump (CORE_ADDR pc)
600 target_read_memory (pc, &op, 1);
604 op = read_memory_unsigned_integer (pc + 1, 1);
610 /* Relative jump: if data16 == 0, disp32, else disp16. */
613 delta = read_memory_integer (pc + 2, 2);
615 /* Include the size of the jmp instruction (including the
621 delta = read_memory_integer (pc + 1, 4);
623 /* Include the size of the jmp instruction. */
628 /* Relative jump, disp8 (ignore data16). */
629 delta = read_memory_integer (pc + data16 + 1, 1);
638 /* Check whether PC points at a prologue for a function returning a
639 structure or union. If so, it updates CACHE and returns the
640 address of the first instruction after the code sequence that
641 removes the "hidden" argument from the stack or CURRENT_PC,
642 whichever is smaller. Otherwise, return PC. */
645 i386_analyze_struct_return (CORE_ADDR pc, CORE_ADDR current_pc,
646 struct i386_frame_cache *cache)
648 /* Functions that return a structure or union start with:
651 xchgl %eax, (%esp) 0x87 0x04 0x24
652 or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
654 (the System V compiler puts out the second `xchg' instruction,
655 and the assembler doesn't try to optimize it, so the 'sib' form
656 gets generated). This sequence is used to get the address of the
657 return buffer for a function that returns a structure. */
658 static gdb_byte proto1[3] = { 0x87, 0x04, 0x24 };
659 static gdb_byte proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
663 if (current_pc <= pc)
666 target_read_memory (pc, &op, 1);
668 if (op != 0x58) /* popl %eax */
671 target_read_memory (pc + 1, buf, 4);
672 if (memcmp (buf, proto1, 3) != 0 && memcmp (buf, proto2, 4) != 0)
675 if (current_pc == pc)
677 cache->sp_offset += 4;
681 if (current_pc == pc + 1)
683 cache->pc_in_eax = 1;
687 if (buf[1] == proto1[1])
694 i386_skip_probe (CORE_ADDR pc)
696 /* A function may start with
710 target_read_memory (pc, &op, 1);
712 if (op == 0x68 || op == 0x6a)
716 /* Skip past the `pushl' instruction; it has either a one-byte or a
717 four-byte operand, depending on the opcode. */
723 /* Read the following 8 bytes, which should be `call _probe' (6
724 bytes) followed by `addl $4,%esp' (2 bytes). */
725 read_memory (pc + delta, buf, sizeof (buf));
726 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
727 pc += delta + sizeof (buf);
733 /* GCC 4.1 and later, can put code in the prologue to realign the
734 stack pointer. Check whether PC points to such code, and update
735 CACHE accordingly. Return the first instruction after the code
736 sequence or CURRENT_PC, whichever is smaller. If we don't
737 recognize the code, return PC. */
740 i386_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
741 struct i386_frame_cache *cache)
743 /* There are 2 code sequences to re-align stack before the frame
746 1. Use a caller-saved saved register:
752 2. Use a callee-saved saved register:
759 "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:
761 0x83 0xe4 0xf0 andl $-16, %esp
762 0x81 0xe4 0x00 0xff 0xff 0xff andl $-256, %esp
767 int offset, offset_and;
768 static int regnums[8] = {
769 I386_EAX_REGNUM, /* %eax */
770 I386_ECX_REGNUM, /* %ecx */
771 I386_EDX_REGNUM, /* %edx */
772 I386_EBX_REGNUM, /* %ebx */
773 I386_ESP_REGNUM, /* %esp */
774 I386_EBP_REGNUM, /* %ebp */
775 I386_ESI_REGNUM, /* %esi */
776 I386_EDI_REGNUM /* %edi */
779 if (target_read_memory (pc, buf, sizeof buf))
782 /* Check caller-saved saved register. The first instruction has
783 to be "leal 4(%esp), %reg". */
784 if (buf[0] == 0x8d && buf[2] == 0x24 && buf[3] == 0x4)
786 /* MOD must be binary 10 and R/M must be binary 100. */
787 if ((buf[1] & 0xc7) != 0x44)
790 /* REG has register number. */
791 reg = (buf[1] >> 3) & 7;
796 /* Check callee-saved saved register. The first instruction
797 has to be "pushl %reg". */
798 if ((buf[0] & 0xf8) != 0x50)
804 /* The next instruction has to be "leal 8(%esp), %reg". */
805 if (buf[1] != 0x8d || buf[3] != 0x24 || buf[4] != 0x8)
808 /* MOD must be binary 10 and R/M must be binary 100. */
809 if ((buf[2] & 0xc7) != 0x44)
812 /* REG has register number. Registers in pushl and leal have to
814 if (reg != ((buf[2] >> 3) & 7))
820 /* Rigister can't be %esp nor %ebp. */
821 if (reg == 4 || reg == 5)
824 /* The next instruction has to be "andl $-XXX, %esp". */
825 if (buf[offset + 1] != 0xe4
826 || (buf[offset] != 0x81 && buf[offset] != 0x83))
830 offset += buf[offset] == 0x81 ? 6 : 3;
832 /* The next instruction has to be "pushl -4(%reg)". 8bit -4 is
833 0xfc. REG must be binary 110 and MOD must be binary 01. */
834 if (buf[offset] != 0xff
835 || buf[offset + 2] != 0xfc
836 || (buf[offset + 1] & 0xf8) != 0x70)
839 /* R/M has register. Registers in leal and pushl have to be the
841 if (reg != (buf[offset + 1] & 7))
844 if (current_pc > pc + offset_and)
845 cache->saved_sp_reg = regnums[reg];
847 return min (pc + offset + 3, current_pc);
850 /* Maximum instruction length we need to handle. */
851 #define I386_MAX_MATCHED_INSN_LEN 6
853 /* Instruction description. */
857 gdb_byte insn[I386_MAX_MATCHED_INSN_LEN];
858 gdb_byte mask[I386_MAX_MATCHED_INSN_LEN];
861 /* Search for the instruction at PC in the list SKIP_INSNS. Return
862 the first instruction description that matches. Otherwise, return
865 static struct i386_insn *
866 i386_match_insn (CORE_ADDR pc, struct i386_insn *skip_insns)
868 struct i386_insn *insn;
871 target_read_memory (pc, &op, 1);
873 for (insn = skip_insns; insn->len > 0; insn++)
875 if ((op & insn->mask[0]) == insn->insn[0])
877 gdb_byte buf[I386_MAX_MATCHED_INSN_LEN - 1];
878 int insn_matched = 1;
881 gdb_assert (insn->len > 1);
882 gdb_assert (insn->len <= I386_MAX_MATCHED_INSN_LEN);
884 target_read_memory (pc + 1, buf, insn->len - 1);
885 for (i = 1; i < insn->len; i++)
887 if ((buf[i - 1] & insn->mask[i]) != insn->insn[i])
899 /* Some special instructions that might be migrated by GCC into the
900 part of the prologue that sets up the new stack frame. Because the
901 stack frame hasn't been setup yet, no registers have been saved
902 yet, and only the scratch registers %eax, %ecx and %edx can be
905 struct i386_insn i386_frame_setup_skip_insns[] =
907 /* Check for `movb imm8, r' and `movl imm32, r'.
909 ??? Should we handle 16-bit operand-sizes here? */
911 /* `movb imm8, %al' and `movb imm8, %ah' */
912 /* `movb imm8, %cl' and `movb imm8, %ch' */
913 { 2, { 0xb0, 0x00 }, { 0xfa, 0x00 } },
914 /* `movb imm8, %dl' and `movb imm8, %dh' */
915 { 2, { 0xb2, 0x00 }, { 0xfb, 0x00 } },
916 /* `movl imm32, %eax' and `movl imm32, %ecx' */
917 { 5, { 0xb8 }, { 0xfe } },
918 /* `movl imm32, %edx' */
919 { 5, { 0xba }, { 0xff } },
921 /* Check for `mov imm32, r32'. Note that there is an alternative
922 encoding for `mov m32, %eax'.
924 ??? Should we handle SIB adressing here?
925 ??? Should we handle 16-bit operand-sizes here? */
927 /* `movl m32, %eax' */
928 { 5, { 0xa1 }, { 0xff } },
929 /* `movl m32, %eax' and `mov; m32, %ecx' */
930 { 6, { 0x89, 0x05 }, {0xff, 0xf7 } },
931 /* `movl m32, %edx' */
932 { 6, { 0x89, 0x15 }, {0xff, 0xff } },
934 /* Check for `xorl r32, r32' and the equivalent `subl r32, r32'.
935 Because of the symmetry, there are actually two ways to encode
936 these instructions; opcode bytes 0x29 and 0x2b for `subl' and
937 opcode bytes 0x31 and 0x33 for `xorl'. */
939 /* `subl %eax, %eax' */
940 { 2, { 0x29, 0xc0 }, { 0xfd, 0xff } },
941 /* `subl %ecx, %ecx' */
942 { 2, { 0x29, 0xc9 }, { 0xfd, 0xff } },
943 /* `subl %edx, %edx' */
944 { 2, { 0x29, 0xd2 }, { 0xfd, 0xff } },
945 /* `xorl %eax, %eax' */
946 { 2, { 0x31, 0xc0 }, { 0xfd, 0xff } },
947 /* `xorl %ecx, %ecx' */
948 { 2, { 0x31, 0xc9 }, { 0xfd, 0xff } },
949 /* `xorl %edx, %edx' */
950 { 2, { 0x31, 0xd2 }, { 0xfd, 0xff } },
955 /* Check whether PC points to a no-op instruction. */
957 i386_skip_noop (CORE_ADDR pc)
962 target_read_memory (pc, &op, 1);
967 /* Ignore `nop' instruction. */
971 target_read_memory (pc, &op, 1);
974 /* Ignore no-op instruction `mov %edi, %edi'.
975 Microsoft system dlls often start with
976 a `mov %edi,%edi' instruction.
977 The 5 bytes before the function start are
978 filled with `nop' instructions.
979 This pattern can be used for hot-patching:
980 The `mov %edi, %edi' instruction can be replaced by a
981 near jump to the location of the 5 `nop' instructions
982 which can be replaced by a 32-bit jump to anywhere
983 in the 32-bit address space. */
987 target_read_memory (pc + 1, &op, 1);
991 target_read_memory (pc, &op, 1);
999 /* Check whether PC points at a code that sets up a new stack frame.
1000 If so, it updates CACHE and returns the address of the first
1001 instruction after the sequence that sets up the frame or LIMIT,
1002 whichever is smaller. If we don't recognize the code, return PC. */
1005 i386_analyze_frame_setup (CORE_ADDR pc, CORE_ADDR limit,
1006 struct i386_frame_cache *cache)
1008 struct i386_insn *insn;
1015 target_read_memory (pc, &op, 1);
1017 if (op == 0x55) /* pushl %ebp */
1019 /* Take into account that we've executed the `pushl %ebp' that
1020 starts this instruction sequence. */
1021 cache->saved_regs[I386_EBP_REGNUM] = 0;
1022 cache->sp_offset += 4;
1025 /* If that's all, return now. */
1029 /* Check for some special instructions that might be migrated by
1030 GCC into the prologue and skip them. At this point in the
1031 prologue, code should only touch the scratch registers %eax,
1032 %ecx and %edx, so while the number of posibilities is sheer,
1035 Make sure we only skip these instructions if we later see the
1036 `movl %esp, %ebp' that actually sets up the frame. */
1037 while (pc + skip < limit)
1039 insn = i386_match_insn (pc + skip, i386_frame_setup_skip_insns);
1046 /* If that's all, return now. */
1047 if (limit <= pc + skip)
1050 target_read_memory (pc + skip, &op, 1);
1052 /* Check for `movl %esp, %ebp' -- can be written in two ways. */
1056 if (read_memory_unsigned_integer (pc + skip + 1, 1) != 0xec)
1060 if (read_memory_unsigned_integer (pc + skip + 1, 1) != 0xe5)
1067 /* OK, we actually have a frame. We just don't know how large
1068 it is yet. Set its size to zero. We'll adjust it if
1069 necessary. We also now commit to skipping the special
1070 instructions mentioned before. */
1074 /* If that's all, return now. */
1078 /* Check for stack adjustment
1082 NOTE: You can't subtract a 16-bit immediate from a 32-bit
1083 reg, so we don't have to worry about a data16 prefix. */
1084 target_read_memory (pc, &op, 1);
1087 /* `subl' with 8-bit immediate. */
1088 if (read_memory_unsigned_integer (pc + 1, 1) != 0xec)
1089 /* Some instruction starting with 0x83 other than `subl'. */
1092 /* `subl' with signed 8-bit immediate (though it wouldn't
1093 make sense to be negative). */
1094 cache->locals = read_memory_integer (pc + 2, 1);
1097 else if (op == 0x81)
1099 /* Maybe it is `subl' with a 32-bit immediate. */
1100 if (read_memory_unsigned_integer (pc + 1, 1) != 0xec)
1101 /* Some instruction starting with 0x81 other than `subl'. */
1104 /* It is `subl' with a 32-bit immediate. */
1105 cache->locals = read_memory_integer (pc + 2, 4);
1110 /* Some instruction other than `subl'. */
1114 else if (op == 0xc8) /* enter */
1116 cache->locals = read_memory_unsigned_integer (pc + 1, 2);
1123 /* Check whether PC points at code that saves registers on the stack.
1124 If so, it updates CACHE and returns the address of the first
1125 instruction after the register saves or CURRENT_PC, whichever is
1126 smaller. Otherwise, return PC. */
1129 i386_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
1130 struct i386_frame_cache *cache)
1132 CORE_ADDR offset = 0;
1136 if (cache->locals > 0)
1137 offset -= cache->locals;
1138 for (i = 0; i < 8 && pc < current_pc; i++)
1140 target_read_memory (pc, &op, 1);
1141 if (op < 0x50 || op > 0x57)
1145 cache->saved_regs[op - 0x50] = offset;
1146 cache->sp_offset += 4;
1153 /* Do a full analysis of the prologue at PC and update CACHE
1154 accordingly. Bail out early if CURRENT_PC is reached. Return the
1155 address where the analysis stopped.
1157 We handle these cases:
1159 The startup sequence can be at the start of the function, or the
1160 function can start with a branch to startup code at the end.
1162 %ebp can be set up with either the 'enter' instruction, or "pushl
1163 %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
1164 once used in the System V compiler).
1166 Local space is allocated just below the saved %ebp by either the
1167 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a
1168 16-bit unsigned argument for space to allocate, and the 'addl'
1169 instruction could have either a signed byte, or 32-bit immediate.
1171 Next, the registers used by this function are pushed. With the
1172 System V compiler they will always be in the order: %edi, %esi,
1173 %ebx (and sometimes a harmless bug causes it to also save but not
1174 restore %eax); however, the code below is willing to see the pushes
1175 in any order, and will handle up to 8 of them.
1177 If the setup sequence is at the end of the function, then the next
1178 instruction will be a branch back to the start. */
1181 i386_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
1182 struct i386_frame_cache *cache)
1184 pc = i386_skip_noop (pc);
1185 pc = i386_follow_jump (pc);
1186 pc = i386_analyze_struct_return (pc, current_pc, cache);
1187 pc = i386_skip_probe (pc);
1188 pc = i386_analyze_stack_align (pc, current_pc, cache);
1189 pc = i386_analyze_frame_setup (pc, current_pc, cache);
1190 return i386_analyze_register_saves (pc, current_pc, cache);
1193 /* Return PC of first real instruction. */
1196 i386_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
1198 static gdb_byte pic_pat[6] =
1200 0xe8, 0, 0, 0, 0, /* call 0x0 */
1201 0x5b, /* popl %ebx */
1203 struct i386_frame_cache cache;
1209 pc = i386_analyze_prologue (start_pc, 0xffffffff, &cache);
1210 if (cache.locals < 0)
1213 /* Found valid frame setup. */
1215 /* The native cc on SVR4 in -K PIC mode inserts the following code
1216 to get the address of the global offset table (GOT) into register
1221 movl %ebx,x(%ebp) (optional)
1224 This code is with the rest of the prologue (at the end of the
1225 function), so we have to skip it to get to the first real
1226 instruction at the start of the function. */
1228 for (i = 0; i < 6; i++)
1230 target_read_memory (pc + i, &op, 1);
1231 if (pic_pat[i] != op)
1238 target_read_memory (pc + delta, &op, 1);
1240 if (op == 0x89) /* movl %ebx, x(%ebp) */
1242 op = read_memory_unsigned_integer (pc + delta + 1, 1);
1244 if (op == 0x5d) /* One byte offset from %ebp. */
1246 else if (op == 0x9d) /* Four byte offset from %ebp. */
1248 else /* Unexpected instruction. */
1251 target_read_memory (pc + delta, &op, 1);
1255 if (delta > 0 && op == 0x81
1256 && read_memory_unsigned_integer (pc + delta + 1, 1) == 0xc3)
1262 /* If the function starts with a branch (to startup code at the end)
1263 the last instruction should bring us back to the first
1264 instruction of the real code. */
1265 if (i386_follow_jump (start_pc) != start_pc)
1266 pc = i386_follow_jump (pc);
1271 /* Check that the code pointed to by PC corresponds to a call to
1272 __main, skip it if so. Return PC otherwise. */
1275 i386_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1279 target_read_memory (pc, &op, 1);
1284 if (target_read_memory (pc + 1, buf, sizeof buf) == 0)
1286 /* Make sure address is computed correctly as a 32bit
1287 integer even if CORE_ADDR is 64 bit wide. */
1288 struct minimal_symbol *s;
1289 CORE_ADDR call_dest = pc + 5 + extract_signed_integer (buf, 4);
1291 call_dest = call_dest & 0xffffffffU;
1292 s = lookup_minimal_symbol_by_pc (call_dest);
1294 && SYMBOL_LINKAGE_NAME (s) != NULL
1295 && strcmp (SYMBOL_LINKAGE_NAME (s), "__main") == 0)
1303 /* This function is 64-bit safe. */
1306 i386_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1310 frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
1311 return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
1315 /* Normal frames. */
1317 static struct i386_frame_cache *
1318 i386_frame_cache (struct frame_info *this_frame, void **this_cache)
1320 struct i386_frame_cache *cache;
1327 cache = i386_alloc_frame_cache ();
1328 *this_cache = cache;
1330 /* In principle, for normal frames, %ebp holds the frame pointer,
1331 which holds the base address for the current stack frame.
1332 However, for functions that don't need it, the frame pointer is
1333 optional. For these "frameless" functions the frame pointer is
1334 actually the frame pointer of the calling frame. Signal
1335 trampolines are just a special case of a "frameless" function.
1336 They (usually) share their frame pointer with the frame that was
1337 in progress when the signal occurred. */
1339 get_frame_register (this_frame, I386_EBP_REGNUM, buf);
1340 cache->base = extract_unsigned_integer (buf, 4);
1341 if (cache->base == 0)
1344 /* For normal frames, %eip is stored at 4(%ebp). */
1345 cache->saved_regs[I386_EIP_REGNUM] = 4;
1347 cache->pc = get_frame_func (this_frame);
1349 i386_analyze_prologue (cache->pc, get_frame_pc (this_frame), cache);
1351 if (cache->saved_sp_reg != -1)
1353 /* Saved stack pointer has been saved. */
1354 get_frame_register (this_frame, cache->saved_sp_reg, buf);
1355 cache->saved_sp = extract_unsigned_integer(buf, 4);
1358 if (cache->locals < 0)
1360 /* We didn't find a valid frame, which means that CACHE->base
1361 currently holds the frame pointer for our calling frame. If
1362 we're at the start of a function, or somewhere half-way its
1363 prologue, the function's frame probably hasn't been fully
1364 setup yet. Try to reconstruct the base address for the stack
1365 frame by looking at the stack pointer. For truly "frameless"
1366 functions this might work too. */
1368 if (cache->saved_sp_reg != -1)
1370 /* We're halfway aligning the stack. */
1371 cache->base = ((cache->saved_sp - 4) & 0xfffffff0) - 4;
1372 cache->saved_regs[I386_EIP_REGNUM] = cache->saved_sp - 4;
1374 /* This will be added back below. */
1375 cache->saved_regs[I386_EIP_REGNUM] -= cache->base;
1379 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
1380 cache->base = extract_unsigned_integer (buf, 4) + cache->sp_offset;
1384 /* Now that we have the base address for the stack frame we can
1385 calculate the value of %esp in the calling frame. */
1386 if (cache->saved_sp == 0)
1387 cache->saved_sp = cache->base + 8;
1389 /* Adjust all the saved registers such that they contain addresses
1390 instead of offsets. */
1391 for (i = 0; i < I386_NUM_SAVED_REGS; i++)
1392 if (cache->saved_regs[i] != -1)
1393 cache->saved_regs[i] += cache->base;
1399 i386_frame_this_id (struct frame_info *this_frame, void **this_cache,
1400 struct frame_id *this_id)
1402 struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
1404 /* This marks the outermost frame. */
1405 if (cache->base == 0)
1408 /* See the end of i386_push_dummy_call. */
1409 (*this_id) = frame_id_build (cache->base + 8, cache->pc);
1412 static struct value *
1413 i386_frame_prev_register (struct frame_info *this_frame, void **this_cache,
1416 struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
1418 gdb_assert (regnum >= 0);
1420 /* The System V ABI says that:
1422 "The flags register contains the system flags, such as the
1423 direction flag and the carry flag. The direction flag must be
1424 set to the forward (that is, zero) direction before entry and
1425 upon exit from a function. Other user flags have no specified
1426 role in the standard calling sequence and are not preserved."
1428 To guarantee the "upon exit" part of that statement we fake a
1429 saved flags register that has its direction flag cleared.
1431 Note that GCC doesn't seem to rely on the fact that the direction
1432 flag is cleared after a function return; it always explicitly
1433 clears the flag before operations where it matters.
1435 FIXME: kettenis/20030316: I'm not quite sure whether this is the
1436 right thing to do. The way we fake the flags register here makes
1437 it impossible to change it. */
1439 if (regnum == I386_EFLAGS_REGNUM)
1443 val = get_frame_register_unsigned (this_frame, regnum);
1445 return frame_unwind_got_constant (this_frame, regnum, val);
1448 if (regnum == I386_EIP_REGNUM && cache->pc_in_eax)
1449 return frame_unwind_got_register (this_frame, regnum, I386_EAX_REGNUM);
1451 if (regnum == I386_ESP_REGNUM && cache->saved_sp)
1452 return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
1454 if (regnum < I386_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
1455 return frame_unwind_got_memory (this_frame, regnum,
1456 cache->saved_regs[regnum]);
1458 return frame_unwind_got_register (this_frame, regnum, regnum);
1461 static const struct frame_unwind i386_frame_unwind =
1465 i386_frame_prev_register,
1467 default_frame_sniffer
1471 /* Signal trampolines. */
1473 static struct i386_frame_cache *
1474 i386_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
1476 struct i386_frame_cache *cache;
1477 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
1484 cache = i386_alloc_frame_cache ();
1486 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
1487 cache->base = extract_unsigned_integer (buf, 4) - 4;
1489 addr = tdep->sigcontext_addr (this_frame);
1490 if (tdep->sc_reg_offset)
1494 gdb_assert (tdep->sc_num_regs <= I386_NUM_SAVED_REGS);
1496 for (i = 0; i < tdep->sc_num_regs; i++)
1497 if (tdep->sc_reg_offset[i] != -1)
1498 cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
1502 cache->saved_regs[I386_EIP_REGNUM] = addr + tdep->sc_pc_offset;
1503 cache->saved_regs[I386_ESP_REGNUM] = addr + tdep->sc_sp_offset;
1506 *this_cache = cache;
1511 i386_sigtramp_frame_this_id (struct frame_info *this_frame, void **this_cache,
1512 struct frame_id *this_id)
1514 struct i386_frame_cache *cache =
1515 i386_sigtramp_frame_cache (this_frame, this_cache);
1517 /* See the end of i386_push_dummy_call. */
1518 (*this_id) = frame_id_build (cache->base + 8, get_frame_pc (this_frame));
1521 static struct value *
1522 i386_sigtramp_frame_prev_register (struct frame_info *this_frame,
1523 void **this_cache, int regnum)
1525 /* Make sure we've initialized the cache. */
1526 i386_sigtramp_frame_cache (this_frame, this_cache);
1528 return i386_frame_prev_register (this_frame, this_cache, regnum);
1532 i386_sigtramp_frame_sniffer (const struct frame_unwind *self,
1533 struct frame_info *this_frame,
1534 void **this_prologue_cache)
1536 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
1538 /* We shouldn't even bother if we don't have a sigcontext_addr
1540 if (tdep->sigcontext_addr == NULL)
1543 if (tdep->sigtramp_p != NULL)
1545 if (tdep->sigtramp_p (this_frame))
1549 if (tdep->sigtramp_start != 0)
1551 CORE_ADDR pc = get_frame_pc (this_frame);
1553 gdb_assert (tdep->sigtramp_end != 0);
1554 if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
1561 static const struct frame_unwind i386_sigtramp_frame_unwind =
1564 i386_sigtramp_frame_this_id,
1565 i386_sigtramp_frame_prev_register,
1567 i386_sigtramp_frame_sniffer
1572 i386_frame_base_address (struct frame_info *this_frame, void **this_cache)
1574 struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
1579 static const struct frame_base i386_frame_base =
1582 i386_frame_base_address,
1583 i386_frame_base_address,
1584 i386_frame_base_address
1587 static struct frame_id
1588 i386_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1592 fp = get_frame_register_unsigned (this_frame, I386_EBP_REGNUM);
1594 /* See the end of i386_push_dummy_call. */
1595 return frame_id_build (fp + 8, get_frame_pc (this_frame));
1599 /* Figure out where the longjmp will land. Slurp the args out of the
1600 stack. We expect the first arg to be a pointer to the jmp_buf
1601 structure from which we extract the address that we will land at.
1602 This address is copied into PC. This routine returns non-zero on
1606 i386_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
1609 CORE_ADDR sp, jb_addr;
1610 struct gdbarch *gdbarch = get_frame_arch (frame);
1611 int jb_pc_offset = gdbarch_tdep (gdbarch)->jb_pc_offset;
1613 /* If JB_PC_OFFSET is -1, we have no way to find out where the
1614 longjmp will land. */
1615 if (jb_pc_offset == -1)
1618 get_frame_register (frame, I386_ESP_REGNUM, buf);
1619 sp = extract_unsigned_integer (buf, 4);
1620 if (target_read_memory (sp + 4, buf, 4))
1623 jb_addr = extract_unsigned_integer (buf, 4);
1624 if (target_read_memory (jb_addr + jb_pc_offset, buf, 4))
1627 *pc = extract_unsigned_integer (buf, 4);
1632 /* Check whether TYPE must be 16-byte-aligned when passed as a
1633 function argument. 16-byte vectors, _Decimal128 and structures or
1634 unions containing such types must be 16-byte-aligned; other
1635 arguments are 4-byte-aligned. */
1638 i386_16_byte_align_p (struct type *type)
1640 type = check_typedef (type);
1641 if ((TYPE_CODE (type) == TYPE_CODE_DECFLOAT
1642 || (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)))
1643 && TYPE_LENGTH (type) == 16)
1645 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1646 return i386_16_byte_align_p (TYPE_TARGET_TYPE (type));
1647 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1648 || TYPE_CODE (type) == TYPE_CODE_UNION)
1651 for (i = 0; i < TYPE_NFIELDS (type); i++)
1653 if (i386_16_byte_align_p (TYPE_FIELD_TYPE (type, i)))
1661 i386_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1662 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
1663 struct value **args, CORE_ADDR sp, int struct_return,
1664 CORE_ADDR struct_addr)
1671 /* Determine the total space required for arguments and struct
1672 return address in a first pass (allowing for 16-byte-aligned
1673 arguments), then push arguments in a second pass. */
1675 for (write_pass = 0; write_pass < 2; write_pass++)
1677 int args_space_used = 0;
1678 int have_16_byte_aligned_arg = 0;
1684 /* Push value address. */
1685 store_unsigned_integer (buf, 4, struct_addr);
1686 write_memory (sp, buf, 4);
1687 args_space_used += 4;
1693 for (i = 0; i < nargs; i++)
1695 int len = TYPE_LENGTH (value_enclosing_type (args[i]));
1699 if (i386_16_byte_align_p (value_enclosing_type (args[i])))
1700 args_space_used = align_up (args_space_used, 16);
1702 write_memory (sp + args_space_used,
1703 value_contents_all (args[i]), len);
1704 /* The System V ABI says that:
1706 "An argument's size is increased, if necessary, to make it a
1707 multiple of [32-bit] words. This may require tail padding,
1708 depending on the size of the argument."
1710 This makes sure the stack stays word-aligned. */
1711 args_space_used += align_up (len, 4);
1715 if (i386_16_byte_align_p (value_enclosing_type (args[i])))
1717 args_space = align_up (args_space, 16);
1718 have_16_byte_aligned_arg = 1;
1720 args_space += align_up (len, 4);
1726 if (have_16_byte_aligned_arg)
1727 args_space = align_up (args_space, 16);
1732 /* Store return address. */
1734 store_unsigned_integer (buf, 4, bp_addr);
1735 write_memory (sp, buf, 4);
1737 /* Finally, update the stack pointer... */
1738 store_unsigned_integer (buf, 4, sp);
1739 regcache_cooked_write (regcache, I386_ESP_REGNUM, buf);
1741 /* ...and fake a frame pointer. */
1742 regcache_cooked_write (regcache, I386_EBP_REGNUM, buf);
1744 /* MarkK wrote: This "+ 8" is all over the place:
1745 (i386_frame_this_id, i386_sigtramp_frame_this_id,
1746 i386_dummy_id). It's there, since all frame unwinders for
1747 a given target have to agree (within a certain margin) on the
1748 definition of the stack address of a frame. Otherwise frame id
1749 comparison might not work correctly. Since DWARF2/GCC uses the
1750 stack address *before* the function call as a frame's CFA. On
1751 the i386, when %ebp is used as a frame pointer, the offset
1752 between the contents %ebp and the CFA as defined by GCC. */
1756 /* These registers are used for returning integers (and on some
1757 targets also for returning `struct' and `union' values when their
1758 size and alignment match an integer type). */
1759 #define LOW_RETURN_REGNUM I386_EAX_REGNUM /* %eax */
1760 #define HIGH_RETURN_REGNUM I386_EDX_REGNUM /* %edx */
1762 /* Read, for architecture GDBARCH, a function return value of TYPE
1763 from REGCACHE, and copy that into VALBUF. */
1766 i386_extract_return_value (struct gdbarch *gdbarch, struct type *type,
1767 struct regcache *regcache, gdb_byte *valbuf)
1769 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1770 int len = TYPE_LENGTH (type);
1771 gdb_byte buf[I386_MAX_REGISTER_SIZE];
1773 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1775 if (tdep->st0_regnum < 0)
1777 warning (_("Cannot find floating-point return value."));
1778 memset (valbuf, 0, len);
1782 /* Floating-point return values can be found in %st(0). Convert
1783 its contents to the desired type. This is probably not
1784 exactly how it would happen on the target itself, but it is
1785 the best we can do. */
1786 regcache_raw_read (regcache, I386_ST0_REGNUM, buf);
1787 convert_typed_floating (buf, builtin_type_i387_ext, valbuf, type);
1791 int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
1792 int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
1794 if (len <= low_size)
1796 regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
1797 memcpy (valbuf, buf, len);
1799 else if (len <= (low_size + high_size))
1801 regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
1802 memcpy (valbuf, buf, low_size);
1803 regcache_raw_read (regcache, HIGH_RETURN_REGNUM, buf);
1804 memcpy (valbuf + low_size, buf, len - low_size);
1807 internal_error (__FILE__, __LINE__,
1808 _("Cannot extract return value of %d bytes long."), len);
1812 /* Write, for architecture GDBARCH, a function return value of TYPE
1813 from VALBUF into REGCACHE. */
1816 i386_store_return_value (struct gdbarch *gdbarch, struct type *type,
1817 struct regcache *regcache, const gdb_byte *valbuf)
1819 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1820 int len = TYPE_LENGTH (type);
1822 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1825 gdb_byte buf[I386_MAX_REGISTER_SIZE];
1827 if (tdep->st0_regnum < 0)
1829 warning (_("Cannot set floating-point return value."));
1833 /* Returning floating-point values is a bit tricky. Apart from
1834 storing the return value in %st(0), we have to simulate the
1835 state of the FPU at function return point. */
1837 /* Convert the value found in VALBUF to the extended
1838 floating-point format used by the FPU. This is probably
1839 not exactly how it would happen on the target itself, but
1840 it is the best we can do. */
1841 convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
1842 regcache_raw_write (regcache, I386_ST0_REGNUM, buf);
1844 /* Set the top of the floating-point register stack to 7. The
1845 actual value doesn't really matter, but 7 is what a normal
1846 function return would end up with if the program started out
1847 with a freshly initialized FPU. */
1848 regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
1850 regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM (tdep), fstat);
1852 /* Mark %st(1) through %st(7) as empty. Since we set the top of
1853 the floating-point register stack to 7, the appropriate value
1854 for the tag word is 0x3fff. */
1855 regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM (tdep), 0x3fff);
1859 int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
1860 int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
1862 if (len <= low_size)
1863 regcache_raw_write_part (regcache, LOW_RETURN_REGNUM, 0, len, valbuf);
1864 else if (len <= (low_size + high_size))
1866 regcache_raw_write (regcache, LOW_RETURN_REGNUM, valbuf);
1867 regcache_raw_write_part (regcache, HIGH_RETURN_REGNUM, 0,
1868 len - low_size, valbuf + low_size);
1871 internal_error (__FILE__, __LINE__,
1872 _("Cannot store return value of %d bytes long."), len);
1877 /* This is the variable that is set with "set struct-convention", and
1878 its legitimate values. */
1879 static const char default_struct_convention[] = "default";
1880 static const char pcc_struct_convention[] = "pcc";
1881 static const char reg_struct_convention[] = "reg";
1882 static const char *valid_conventions[] =
1884 default_struct_convention,
1885 pcc_struct_convention,
1886 reg_struct_convention,
1889 static const char *struct_convention = default_struct_convention;
1891 /* Return non-zero if TYPE, which is assumed to be a structure,
1892 a union type, or an array type, should be returned in registers
1893 for architecture GDBARCH. */
1896 i386_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
1898 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1899 enum type_code code = TYPE_CODE (type);
1900 int len = TYPE_LENGTH (type);
1902 gdb_assert (code == TYPE_CODE_STRUCT
1903 || code == TYPE_CODE_UNION
1904 || code == TYPE_CODE_ARRAY);
1906 if (struct_convention == pcc_struct_convention
1907 || (struct_convention == default_struct_convention
1908 && tdep->struct_return == pcc_struct_return))
1911 /* Structures consisting of a single `float', `double' or 'long
1912 double' member are returned in %st(0). */
1913 if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
1915 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1916 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1917 return (len == 4 || len == 8 || len == 12);
1920 return (len == 1 || len == 2 || len == 4 || len == 8);
1923 /* Determine, for architecture GDBARCH, how a return value of TYPE
1924 should be returned. If it is supposed to be returned in registers,
1925 and READBUF is non-zero, read the appropriate value from REGCACHE,
1926 and copy it into READBUF. If WRITEBUF is non-zero, write the value
1927 from WRITEBUF into REGCACHE. */
1929 static enum return_value_convention
1930 i386_return_value (struct gdbarch *gdbarch, struct type *func_type,
1931 struct type *type, struct regcache *regcache,
1932 gdb_byte *readbuf, const gdb_byte *writebuf)
1934 enum type_code code = TYPE_CODE (type);
1936 if (((code == TYPE_CODE_STRUCT
1937 || code == TYPE_CODE_UNION
1938 || code == TYPE_CODE_ARRAY)
1939 && !i386_reg_struct_return_p (gdbarch, type))
1940 /* 128-bit decimal float uses the struct return convention. */
1941 || (code == TYPE_CODE_DECFLOAT && TYPE_LENGTH (type) == 16))
1943 /* The System V ABI says that:
1945 "A function that returns a structure or union also sets %eax
1946 to the value of the original address of the caller's area
1947 before it returns. Thus when the caller receives control
1948 again, the address of the returned object resides in register
1949 %eax and can be used to access the object."
1951 So the ABI guarantees that we can always find the return
1952 value just after the function has returned. */
1954 /* Note that the ABI doesn't mention functions returning arrays,
1955 which is something possible in certain languages such as Ada.
1956 In this case, the value is returned as if it was wrapped in
1957 a record, so the convention applied to records also applies
1964 regcache_raw_read_unsigned (regcache, I386_EAX_REGNUM, &addr);
1965 read_memory (addr, readbuf, TYPE_LENGTH (type));
1968 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
1971 /* This special case is for structures consisting of a single
1972 `float', `double' or 'long double' member. These structures are
1973 returned in %st(0). For these structures, we call ourselves
1974 recursively, changing TYPE into the type of the first member of
1975 the structure. Since that should work for all structures that
1976 have only one member, we don't bother to check the member's type
1978 if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
1980 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1981 return i386_return_value (gdbarch, func_type, type, regcache,
1986 i386_extract_return_value (gdbarch, type, regcache, readbuf);
1988 i386_store_return_value (gdbarch, type, regcache, writebuf);
1990 return RETURN_VALUE_REGISTER_CONVENTION;
1994 /* Type for %eflags. */
1995 struct type *i386_eflags_type;
1997 /* Type for %mxcsr. */
1998 struct type *i386_mxcsr_type;
2000 /* Construct types for ISA-specific registers. */
2002 i386_init_types (void)
2006 type = init_flags_type ("builtin_type_i386_eflags", 4);
2007 append_flags_type_flag (type, 0, "CF");
2008 append_flags_type_flag (type, 1, NULL);
2009 append_flags_type_flag (type, 2, "PF");
2010 append_flags_type_flag (type, 4, "AF");
2011 append_flags_type_flag (type, 6, "ZF");
2012 append_flags_type_flag (type, 7, "SF");
2013 append_flags_type_flag (type, 8, "TF");
2014 append_flags_type_flag (type, 9, "IF");
2015 append_flags_type_flag (type, 10, "DF");
2016 append_flags_type_flag (type, 11, "OF");
2017 append_flags_type_flag (type, 14, "NT");
2018 append_flags_type_flag (type, 16, "RF");
2019 append_flags_type_flag (type, 17, "VM");
2020 append_flags_type_flag (type, 18, "AC");
2021 append_flags_type_flag (type, 19, "VIF");
2022 append_flags_type_flag (type, 20, "VIP");
2023 append_flags_type_flag (type, 21, "ID");
2024 i386_eflags_type = type;
2026 type = init_flags_type ("builtin_type_i386_mxcsr", 4);
2027 append_flags_type_flag (type, 0, "IE");
2028 append_flags_type_flag (type, 1, "DE");
2029 append_flags_type_flag (type, 2, "ZE");
2030 append_flags_type_flag (type, 3, "OE");
2031 append_flags_type_flag (type, 4, "UE");
2032 append_flags_type_flag (type, 5, "PE");
2033 append_flags_type_flag (type, 6, "DAZ");
2034 append_flags_type_flag (type, 7, "IM");
2035 append_flags_type_flag (type, 8, "DM");
2036 append_flags_type_flag (type, 9, "ZM");
2037 append_flags_type_flag (type, 10, "OM");
2038 append_flags_type_flag (type, 11, "UM");
2039 append_flags_type_flag (type, 12, "PM");
2040 append_flags_type_flag (type, 15, "FZ");
2041 i386_mxcsr_type = type;
2044 /* Construct vector type for MMX registers. */
2046 i386_mmx_type (struct gdbarch *gdbarch)
2048 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2050 if (!tdep->i386_mmx_type)
2052 /* The type we're building is this: */
2054 union __gdb_builtin_type_vec64i
2057 int32_t v2_int32[2];
2058 int16_t v4_int16[4];
2065 t = init_composite_type ("__gdb_builtin_type_vec64i", TYPE_CODE_UNION);
2066 append_composite_type_field (t, "uint64", builtin_type_int64);
2067 append_composite_type_field (t, "v2_int32",
2068 init_vector_type (builtin_type_int32, 2));
2069 append_composite_type_field (t, "v4_int16",
2070 init_vector_type (builtin_type_int16, 4));
2071 append_composite_type_field (t, "v8_int8",
2072 init_vector_type (builtin_type_int8, 8));
2074 TYPE_VECTOR (t) = 1;
2075 TYPE_NAME (t) = "builtin_type_vec64i";
2076 tdep->i386_mmx_type = t;
2079 return tdep->i386_mmx_type;
2083 i386_sse_type (struct gdbarch *gdbarch)
2085 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2087 if (!tdep->i386_sse_type)
2089 /* The type we're building is this: */
2091 union __gdb_builtin_type_vec128i
2094 int64_t v2_int64[2];
2095 int32_t v4_int32[4];
2096 int16_t v8_int16[8];
2097 int8_t v16_int8[16];
2098 double v2_double[2];
2105 t = init_composite_type ("__gdb_builtin_type_vec128i", TYPE_CODE_UNION);
2106 append_composite_type_field (t, "v4_float",
2107 init_vector_type (builtin_type (gdbarch)
2108 ->builtin_float, 4));
2109 append_composite_type_field (t, "v2_double",
2110 init_vector_type (builtin_type (gdbarch)
2111 ->builtin_double, 2));
2112 append_composite_type_field (t, "v16_int8",
2113 init_vector_type (builtin_type_int8, 16));
2114 append_composite_type_field (t, "v8_int16",
2115 init_vector_type (builtin_type_int16, 8));
2116 append_composite_type_field (t, "v4_int32",
2117 init_vector_type (builtin_type_int32, 4));
2118 append_composite_type_field (t, "v2_int64",
2119 init_vector_type (builtin_type_int64, 2));
2120 append_composite_type_field (t, "uint128", builtin_type_int128);
2122 TYPE_VECTOR (t) = 1;
2123 TYPE_NAME (t) = "builtin_type_vec128i";
2124 tdep->i386_sse_type = t;
2127 return tdep->i386_sse_type;
2130 /* Return the GDB type object for the "standard" data type of data in
2131 register REGNUM. Perhaps %esi and %edi should go here, but
2132 potentially they could be used for things other than address. */
2134 static struct type *
2135 i386_register_type (struct gdbarch *gdbarch, int regnum)
2137 if (regnum == I386_EIP_REGNUM)
2138 return builtin_type (gdbarch)->builtin_func_ptr;
2140 if (regnum == I386_EFLAGS_REGNUM)
2141 return i386_eflags_type;
2143 if (regnum == I386_EBP_REGNUM || regnum == I386_ESP_REGNUM)
2144 return builtin_type (gdbarch)->builtin_data_ptr;
2146 if (i386_fp_regnum_p (gdbarch, regnum))
2147 return builtin_type_i387_ext;
2149 if (i386_mmx_regnum_p (gdbarch, regnum))
2150 return i386_mmx_type (gdbarch);
2152 if (i386_sse_regnum_p (gdbarch, regnum))
2153 return i386_sse_type (gdbarch);
2155 if (regnum == I387_MXCSR_REGNUM (gdbarch_tdep (gdbarch)))
2156 return i386_mxcsr_type;
2158 return builtin_type (gdbarch)->builtin_int;
2161 /* Map a cooked register onto a raw register or memory. For the i386,
2162 the MMX registers need to be mapped onto floating point registers. */
2165 i386_mmx_regnum_to_fp_regnum (struct regcache *regcache, int regnum)
2167 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
2172 mmxreg = regnum - tdep->mm0_regnum;
2173 regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
2174 tos = (fstat >> 11) & 0x7;
2175 fpreg = (mmxreg + tos) % 8;
2177 return (I387_ST0_REGNUM (tdep) + fpreg);
2181 i386_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
2182 int regnum, gdb_byte *buf)
2184 if (i386_mmx_regnum_p (gdbarch, regnum))
2186 gdb_byte mmx_buf[MAX_REGISTER_SIZE];
2187 int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
2189 /* Extract (always little endian). */
2190 regcache_raw_read (regcache, fpnum, mmx_buf);
2191 memcpy (buf, mmx_buf, register_size (gdbarch, regnum));
2194 regcache_raw_read (regcache, regnum, buf);
2198 i386_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
2199 int regnum, const gdb_byte *buf)
2201 if (i386_mmx_regnum_p (gdbarch, regnum))
2203 gdb_byte mmx_buf[MAX_REGISTER_SIZE];
2204 int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
2207 regcache_raw_read (regcache, fpnum, mmx_buf);
2208 /* ... Modify ... (always little endian). */
2209 memcpy (mmx_buf, buf, register_size (gdbarch, regnum));
2211 regcache_raw_write (regcache, fpnum, mmx_buf);
2214 regcache_raw_write (regcache, regnum, buf);
2218 /* Return the register number of the register allocated by GCC after
2219 REGNUM, or -1 if there is no such register. */
2222 i386_next_regnum (int regnum)
2224 /* GCC allocates the registers in the order:
2226 %eax, %edx, %ecx, %ebx, %esi, %edi, %ebp, %esp, ...
2228 Since storing a variable in %esp doesn't make any sense we return
2229 -1 for %ebp and for %esp itself. */
2230 static int next_regnum[] =
2232 I386_EDX_REGNUM, /* Slot for %eax. */
2233 I386_EBX_REGNUM, /* Slot for %ecx. */
2234 I386_ECX_REGNUM, /* Slot for %edx. */
2235 I386_ESI_REGNUM, /* Slot for %ebx. */
2236 -1, -1, /* Slots for %esp and %ebp. */
2237 I386_EDI_REGNUM, /* Slot for %esi. */
2238 I386_EBP_REGNUM /* Slot for %edi. */
2241 if (regnum >= 0 && regnum < sizeof (next_regnum) / sizeof (next_regnum[0]))
2242 return next_regnum[regnum];
2247 /* Return nonzero if a value of type TYPE stored in register REGNUM
2248 needs any special handling. */
2251 i386_convert_register_p (struct gdbarch *gdbarch, int regnum, struct type *type)
2253 int len = TYPE_LENGTH (type);
2255 /* Values may be spread across multiple registers. Most debugging
2256 formats aren't expressive enough to specify the locations, so
2257 some heuristics is involved. Right now we only handle types that
2258 have a length that is a multiple of the word size, since GCC
2259 doesn't seem to put any other types into registers. */
2260 if (len > 4 && len % 4 == 0)
2262 int last_regnum = regnum;
2266 last_regnum = i386_next_regnum (last_regnum);
2270 if (last_regnum != -1)
2274 return i387_convert_register_p (gdbarch, regnum, type);
2277 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
2278 return its contents in TO. */
2281 i386_register_to_value (struct frame_info *frame, int regnum,
2282 struct type *type, gdb_byte *to)
2284 struct gdbarch *gdbarch = get_frame_arch (frame);
2285 int len = TYPE_LENGTH (type);
2287 /* FIXME: kettenis/20030609: What should we do if REGNUM isn't
2288 available in FRAME (i.e. if it wasn't saved)? */
2290 if (i386_fp_regnum_p (gdbarch, regnum))
2292 i387_register_to_value (frame, regnum, type, to);
2296 /* Read a value spread across multiple registers. */
2298 gdb_assert (len > 4 && len % 4 == 0);
2302 gdb_assert (regnum != -1);
2303 gdb_assert (register_size (gdbarch, regnum) == 4);
2305 get_frame_register (frame, regnum, to);
2306 regnum = i386_next_regnum (regnum);
2312 /* Write the contents FROM of a value of type TYPE into register
2313 REGNUM in frame FRAME. */
2316 i386_value_to_register (struct frame_info *frame, int regnum,
2317 struct type *type, const gdb_byte *from)
2319 int len = TYPE_LENGTH (type);
2321 if (i386_fp_regnum_p (get_frame_arch (frame), regnum))
2323 i387_value_to_register (frame, regnum, type, from);
2327 /* Write a value spread across multiple registers. */
2329 gdb_assert (len > 4 && len % 4 == 0);
2333 gdb_assert (regnum != -1);
2334 gdb_assert (register_size (get_frame_arch (frame), regnum) == 4);
2336 put_frame_register (frame, regnum, from);
2337 regnum = i386_next_regnum (regnum);
2343 /* Supply register REGNUM from the buffer specified by GREGS and LEN
2344 in the general-purpose register set REGSET to register cache
2345 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
2348 i386_supply_gregset (const struct regset *regset, struct regcache *regcache,
2349 int regnum, const void *gregs, size_t len)
2351 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
2352 const gdb_byte *regs = gregs;
2355 gdb_assert (len == tdep->sizeof_gregset);
2357 for (i = 0; i < tdep->gregset_num_regs; i++)
2359 if ((regnum == i || regnum == -1)
2360 && tdep->gregset_reg_offset[i] != -1)
2361 regcache_raw_supply (regcache, i, regs + tdep->gregset_reg_offset[i]);
2365 /* Collect register REGNUM from the register cache REGCACHE and store
2366 it in the buffer specified by GREGS and LEN as described by the
2367 general-purpose register set REGSET. If REGNUM is -1, do this for
2368 all registers in REGSET. */
2371 i386_collect_gregset (const struct regset *regset,
2372 const struct regcache *regcache,
2373 int regnum, void *gregs, size_t len)
2375 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
2376 gdb_byte *regs = gregs;
2379 gdb_assert (len == tdep->sizeof_gregset);
2381 for (i = 0; i < tdep->gregset_num_regs; i++)
2383 if ((regnum == i || regnum == -1)
2384 && tdep->gregset_reg_offset[i] != -1)
2385 regcache_raw_collect (regcache, i, regs + tdep->gregset_reg_offset[i]);
2389 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
2390 in the floating-point register set REGSET to register cache
2391 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
2394 i386_supply_fpregset (const struct regset *regset, struct regcache *regcache,
2395 int regnum, const void *fpregs, size_t len)
2397 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
2399 if (len == I387_SIZEOF_FXSAVE)
2401 i387_supply_fxsave (regcache, regnum, fpregs);
2405 gdb_assert (len == tdep->sizeof_fpregset);
2406 i387_supply_fsave (regcache, regnum, fpregs);
2409 /* Collect register REGNUM from the register cache REGCACHE and store
2410 it in the buffer specified by FPREGS and LEN as described by the
2411 floating-point register set REGSET. If REGNUM is -1, do this for
2412 all registers in REGSET. */
2415 i386_collect_fpregset (const struct regset *regset,
2416 const struct regcache *regcache,
2417 int regnum, void *fpregs, size_t len)
2419 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
2421 if (len == I387_SIZEOF_FXSAVE)
2423 i387_collect_fxsave (regcache, regnum, fpregs);
2427 gdb_assert (len == tdep->sizeof_fpregset);
2428 i387_collect_fsave (regcache, regnum, fpregs);
2431 /* Return the appropriate register set for the core section identified
2432 by SECT_NAME and SECT_SIZE. */
2434 const struct regset *
2435 i386_regset_from_core_section (struct gdbarch *gdbarch,
2436 const char *sect_name, size_t sect_size)
2438 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2440 if (strcmp (sect_name, ".reg") == 0 && sect_size == tdep->sizeof_gregset)
2442 if (tdep->gregset == NULL)
2443 tdep->gregset = regset_alloc (gdbarch, i386_supply_gregset,
2444 i386_collect_gregset);
2445 return tdep->gregset;
2448 if ((strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
2449 || (strcmp (sect_name, ".reg-xfp") == 0
2450 && sect_size == I387_SIZEOF_FXSAVE))
2452 if (tdep->fpregset == NULL)
2453 tdep->fpregset = regset_alloc (gdbarch, i386_supply_fpregset,
2454 i386_collect_fpregset);
2455 return tdep->fpregset;
2462 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
2465 i386_pe_skip_trampoline_code (CORE_ADDR pc, char *name)
2467 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
2469 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
2470 struct minimal_symbol *indsym =
2471 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
2472 char *symname = indsym ? SYMBOL_LINKAGE_NAME (indsym) : 0;
2476 if (strncmp (symname, "__imp_", 6) == 0
2477 || strncmp (symname, "_imp_", 5) == 0)
2478 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
2481 return 0; /* Not a trampoline. */
2485 /* Return whether the THIS_FRAME corresponds to a sigtramp
2489 i386_sigtramp_p (struct frame_info *this_frame)
2491 CORE_ADDR pc = get_frame_pc (this_frame);
2494 find_pc_partial_function (pc, &name, NULL, NULL);
2495 return (name && strcmp ("_sigtramp", name) == 0);
2499 /* We have two flavours of disassembly. The machinery on this page
2500 deals with switching between those. */
2503 i386_print_insn (bfd_vma pc, struct disassemble_info *info)
2505 gdb_assert (disassembly_flavor == att_flavor
2506 || disassembly_flavor == intel_flavor);
2508 /* FIXME: kettenis/20020915: Until disassembler_options is properly
2509 constified, cast to prevent a compiler warning. */
2510 info->disassembler_options = (char *) disassembly_flavor;
2512 return print_insn_i386 (pc, info);
2516 /* There are a few i386 architecture variants that differ only
2517 slightly from the generic i386 target. For now, we don't give them
2518 their own source file, but include them here. As a consequence,
2519 they'll always be included. */
2521 /* System V Release 4 (SVR4). */
2523 /* Return whether THIS_FRAME corresponds to a SVR4 sigtramp
2527 i386_svr4_sigtramp_p (struct frame_info *this_frame)
2529 CORE_ADDR pc = get_frame_pc (this_frame);
2532 /* UnixWare uses _sigacthandler. The origin of the other symbols is
2533 currently unknown. */
2534 find_pc_partial_function (pc, &name, NULL, NULL);
2535 return (name && (strcmp ("_sigreturn", name) == 0
2536 || strcmp ("_sigacthandler", name) == 0
2537 || strcmp ("sigvechandler", name) == 0));
2540 /* Assuming THIS_FRAME is for a SVR4 sigtramp routine, return the
2541 address of the associated sigcontext (ucontext) structure. */
2544 i386_svr4_sigcontext_addr (struct frame_info *this_frame)
2549 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
2550 sp = extract_unsigned_integer (buf, 4);
2552 return read_memory_unsigned_integer (sp + 8, 4);
2559 i386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2561 /* We typically use stabs-in-ELF with the SVR4 register numbering. */
2562 set_gdbarch_stab_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
2565 /* System V Release 4 (SVR4). */
2568 i386_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2570 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2572 /* System V Release 4 uses ELF. */
2573 i386_elf_init_abi (info, gdbarch);
2575 /* System V Release 4 has shared libraries. */
2576 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
2578 tdep->sigtramp_p = i386_svr4_sigtramp_p;
2579 tdep->sigcontext_addr = i386_svr4_sigcontext_addr;
2580 tdep->sc_pc_offset = 36 + 14 * 4;
2581 tdep->sc_sp_offset = 36 + 17 * 4;
2583 tdep->jb_pc_offset = 20;
2589 i386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2591 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2593 /* DJGPP doesn't have any special frames for signal handlers. */
2594 tdep->sigtramp_p = NULL;
2596 tdep->jb_pc_offset = 36;
2600 /* i386 register groups. In addition to the normal groups, add "mmx"
2603 static struct reggroup *i386_sse_reggroup;
2604 static struct reggroup *i386_mmx_reggroup;
2607 i386_init_reggroups (void)
2609 i386_sse_reggroup = reggroup_new ("sse", USER_REGGROUP);
2610 i386_mmx_reggroup = reggroup_new ("mmx", USER_REGGROUP);
2614 i386_add_reggroups (struct gdbarch *gdbarch)
2616 reggroup_add (gdbarch, i386_sse_reggroup);
2617 reggroup_add (gdbarch, i386_mmx_reggroup);
2618 reggroup_add (gdbarch, general_reggroup);
2619 reggroup_add (gdbarch, float_reggroup);
2620 reggroup_add (gdbarch, all_reggroup);
2621 reggroup_add (gdbarch, save_reggroup);
2622 reggroup_add (gdbarch, restore_reggroup);
2623 reggroup_add (gdbarch, vector_reggroup);
2624 reggroup_add (gdbarch, system_reggroup);
2628 i386_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
2629 struct reggroup *group)
2631 int sse_regnum_p = (i386_sse_regnum_p (gdbarch, regnum)
2632 || i386_mxcsr_regnum_p (gdbarch, regnum));
2633 int fp_regnum_p = (i386_fp_regnum_p (gdbarch, regnum)
2634 || i386_fpc_regnum_p (gdbarch, regnum));
2635 int mmx_regnum_p = (i386_mmx_regnum_p (gdbarch, regnum));
2637 if (group == i386_mmx_reggroup)
2638 return mmx_regnum_p;
2639 if (group == i386_sse_reggroup)
2640 return sse_regnum_p;
2641 if (group == vector_reggroup)
2642 return (mmx_regnum_p || sse_regnum_p);
2643 if (group == float_reggroup)
2645 if (group == general_reggroup)
2646 return (!fp_regnum_p && !mmx_regnum_p && !sse_regnum_p);
2648 return default_register_reggroup_p (gdbarch, regnum, group);
2652 /* Get the ARGIth function argument for the current function. */
2655 i386_fetch_pointer_argument (struct frame_info *frame, int argi,
2658 CORE_ADDR sp = get_frame_register_unsigned (frame, I386_ESP_REGNUM);
2659 return read_memory_unsigned_integer (sp + (4 * (argi + 1)), 4);
2663 i386_skip_permanent_breakpoint (struct regcache *regcache)
2665 CORE_ADDR current_pc = regcache_read_pc (regcache);
2667 /* On i386, breakpoint is exactly 1 byte long, so we just
2668 adjust the PC in the regcache. */
2670 regcache_write_pc (regcache, current_pc);
2675 static struct gdbarch *
2676 i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2678 struct gdbarch_tdep *tdep;
2679 struct gdbarch *gdbarch;
2681 /* If there is already a candidate, use it. */
2682 arches = gdbarch_list_lookup_by_info (arches, &info);
2684 return arches->gdbarch;
2686 /* Allocate space for the new architecture. */
2687 tdep = XCALLOC (1, struct gdbarch_tdep);
2688 gdbarch = gdbarch_alloc (&info, tdep);
2690 /* General-purpose registers. */
2691 tdep->gregset = NULL;
2692 tdep->gregset_reg_offset = NULL;
2693 tdep->gregset_num_regs = I386_NUM_GREGS;
2694 tdep->sizeof_gregset = 0;
2696 /* Floating-point registers. */
2697 tdep->fpregset = NULL;
2698 tdep->sizeof_fpregset = I387_SIZEOF_FSAVE;
2700 /* The default settings include the FPU registers, the MMX registers
2701 and the SSE registers. This can be overridden for a specific ABI
2702 by adjusting the members `st0_regnum', `mm0_regnum' and
2703 `num_xmm_regs' of `struct gdbarch_tdep', otherwise the registers
2704 will show up in the output of "info all-registers". Ideally we
2705 should try to autodetect whether they are available, such that we
2706 can prevent "info all-registers" from displaying registers that
2709 NOTE: kevinb/2003-07-13: ... if it's a choice between printing
2710 [the SSE registers] always (even when they don't exist) or never
2711 showing them to the user (even when they do exist), I prefer the
2712 former over the latter. */
2714 tdep->st0_regnum = I386_ST0_REGNUM;
2716 /* The MMX registers are implemented as pseudo-registers. Put off
2717 calculating the register number for %mm0 until we know the number
2718 of raw registers. */
2719 tdep->mm0_regnum = 0;
2721 /* I386_NUM_XREGS includes %mxcsr, so substract one. */
2722 tdep->num_xmm_regs = I386_NUM_XREGS - 1;
2724 tdep->jb_pc_offset = -1;
2725 tdep->struct_return = pcc_struct_return;
2726 tdep->sigtramp_start = 0;
2727 tdep->sigtramp_end = 0;
2728 tdep->sigtramp_p = i386_sigtramp_p;
2729 tdep->sigcontext_addr = NULL;
2730 tdep->sc_reg_offset = NULL;
2731 tdep->sc_pc_offset = -1;
2732 tdep->sc_sp_offset = -1;
2734 /* The format used for `long double' on almost all i386 targets is
2735 the i387 extended floating-point format. In fact, of all targets
2736 in the GCC 2.95 tree, only OSF/1 does it different, and insists
2737 on having a `long double' that's not `long' at all. */
2738 set_gdbarch_long_double_format (gdbarch, floatformats_i387_ext);
2740 /* Although the i387 extended floating-point has only 80 significant
2741 bits, a `long double' actually takes up 96, probably to enforce
2743 set_gdbarch_long_double_bit (gdbarch, 96);
2745 /* The default ABI includes general-purpose registers,
2746 floating-point registers, and the SSE registers. */
2747 set_gdbarch_num_regs (gdbarch, I386_SSE_NUM_REGS);
2748 set_gdbarch_register_name (gdbarch, i386_register_name);
2749 set_gdbarch_register_type (gdbarch, i386_register_type);
2751 /* Register numbers of various important registers. */
2752 set_gdbarch_sp_regnum (gdbarch, I386_ESP_REGNUM); /* %esp */
2753 set_gdbarch_pc_regnum (gdbarch, I386_EIP_REGNUM); /* %eip */
2754 set_gdbarch_ps_regnum (gdbarch, I386_EFLAGS_REGNUM); /* %eflags */
2755 set_gdbarch_fp0_regnum (gdbarch, I386_ST0_REGNUM); /* %st(0) */
2757 /* NOTE: kettenis/20040418: GCC does have two possible register
2758 numbering schemes on the i386: dbx and SVR4. These schemes
2759 differ in how they number %ebp, %esp, %eflags, and the
2760 floating-point registers, and are implemented by the arrays
2761 dbx_register_map[] and svr4_dbx_register_map in
2762 gcc/config/i386.c. GCC also defines a third numbering scheme in
2763 gcc/config/i386.c, which it designates as the "default" register
2764 map used in 64bit mode. This last register numbering scheme is
2765 implemented in dbx64_register_map, and is used for AMD64; see
2768 Currently, each GCC i386 target always uses the same register
2769 numbering scheme across all its supported debugging formats
2770 i.e. SDB (COFF), stabs and DWARF 2. This is because
2771 gcc/sdbout.c, gcc/dbxout.c and gcc/dwarf2out.c all use the
2772 DBX_REGISTER_NUMBER macro which is defined by each target's
2773 respective config header in a manner independent of the requested
2774 output debugging format.
2776 This does not match the arrangement below, which presumes that
2777 the SDB and stabs numbering schemes differ from the DWARF and
2778 DWARF 2 ones. The reason for this arrangement is that it is
2779 likely to get the numbering scheme for the target's
2780 default/native debug format right. For targets where GCC is the
2781 native compiler (FreeBSD, NetBSD, OpenBSD, GNU/Linux) or for
2782 targets where the native toolchain uses a different numbering
2783 scheme for a particular debug format (stabs-in-ELF on Solaris)
2784 the defaults below will have to be overridden, like
2785 i386_elf_init_abi() does. */
2787 /* Use the dbx register numbering scheme for stabs and COFF. */
2788 set_gdbarch_stab_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum);
2789 set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum);
2791 /* Use the SVR4 register numbering scheme for DWARF 2. */
2792 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
2794 /* We don't set gdbarch_stab_reg_to_regnum, since ECOFF doesn't seem to
2795 be in use on any of the supported i386 targets. */
2797 set_gdbarch_print_float_info (gdbarch, i387_print_float_info);
2799 set_gdbarch_get_longjmp_target (gdbarch, i386_get_longjmp_target);
2801 /* Call dummy code. */
2802 set_gdbarch_push_dummy_call (gdbarch, i386_push_dummy_call);
2804 set_gdbarch_convert_register_p (gdbarch, i386_convert_register_p);
2805 set_gdbarch_register_to_value (gdbarch, i386_register_to_value);
2806 set_gdbarch_value_to_register (gdbarch, i386_value_to_register);
2808 set_gdbarch_return_value (gdbarch, i386_return_value);
2810 set_gdbarch_skip_prologue (gdbarch, i386_skip_prologue);
2812 /* Stack grows downward. */
2813 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2815 set_gdbarch_breakpoint_from_pc (gdbarch, i386_breakpoint_from_pc);
2816 set_gdbarch_decr_pc_after_break (gdbarch, 1);
2817 set_gdbarch_max_insn_length (gdbarch, I386_MAX_INSN_LEN);
2819 set_gdbarch_frame_args_skip (gdbarch, 8);
2821 /* Wire in the MMX registers. */
2822 set_gdbarch_num_pseudo_regs (gdbarch, i386_num_mmx_regs);
2823 set_gdbarch_pseudo_register_read (gdbarch, i386_pseudo_register_read);
2824 set_gdbarch_pseudo_register_write (gdbarch, i386_pseudo_register_write);
2826 set_gdbarch_print_insn (gdbarch, i386_print_insn);
2828 set_gdbarch_dummy_id (gdbarch, i386_dummy_id);
2830 set_gdbarch_unwind_pc (gdbarch, i386_unwind_pc);
2832 /* Add the i386 register groups. */
2833 i386_add_reggroups (gdbarch);
2834 set_gdbarch_register_reggroup_p (gdbarch, i386_register_reggroup_p);
2836 /* Helper for function argument information. */
2837 set_gdbarch_fetch_pointer_argument (gdbarch, i386_fetch_pointer_argument);
2839 /* Hook in the DWARF CFI frame unwinder. */
2840 dwarf2_append_unwinders (gdbarch);
2842 frame_base_set_default (gdbarch, &i386_frame_base);
2844 /* Hook in ABI-specific overrides, if they have been registered. */
2845 gdbarch_init_osabi (info, gdbarch);
2847 frame_unwind_append_unwinder (gdbarch, &i386_sigtramp_frame_unwind);
2848 frame_unwind_append_unwinder (gdbarch, &i386_frame_unwind);
2850 /* If we have a register mapping, enable the generic core file
2851 support, unless it has already been enabled. */
2852 if (tdep->gregset_reg_offset
2853 && !gdbarch_regset_from_core_section_p (gdbarch))
2854 set_gdbarch_regset_from_core_section (gdbarch,
2855 i386_regset_from_core_section);
2857 /* Unless support for MMX has been disabled, make %mm0 the first
2859 if (tdep->mm0_regnum == 0)
2860 tdep->mm0_regnum = gdbarch_num_regs (gdbarch);
2862 set_gdbarch_skip_permanent_breakpoint (gdbarch,
2863 i386_skip_permanent_breakpoint);
2868 static enum gdb_osabi
2869 i386_coff_osabi_sniffer (bfd *abfd)
2871 if (strcmp (bfd_get_target (abfd), "coff-go32-exe") == 0
2872 || strcmp (bfd_get_target (abfd), "coff-go32") == 0)
2873 return GDB_OSABI_GO32;
2875 return GDB_OSABI_UNKNOWN;
2879 /* Provide a prototype to silence -Wmissing-prototypes. */
2880 void _initialize_i386_tdep (void);
2883 _initialize_i386_tdep (void)
2885 register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
2887 /* Add the variable that controls the disassembly flavor. */
2888 add_setshow_enum_cmd ("disassembly-flavor", no_class, valid_flavors,
2889 &disassembly_flavor, _("\
2890 Set the disassembly flavor."), _("\
2891 Show the disassembly flavor."), _("\
2892 The valid values are \"att\" and \"intel\", and the default value is \"att\"."),
2894 NULL, /* FIXME: i18n: */
2895 &setlist, &showlist);
2897 /* Add the variable that controls the convention for returning
2899 add_setshow_enum_cmd ("struct-convention", no_class, valid_conventions,
2900 &struct_convention, _("\
2901 Set the convention for returning small structs."), _("\
2902 Show the convention for returning small structs."), _("\
2903 Valid values are \"default\", \"pcc\" and \"reg\", and the default value\n\
2906 NULL, /* FIXME: i18n: */
2907 &setlist, &showlist);
2909 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
2910 i386_coff_osabi_sniffer);
2912 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_SVR4,
2913 i386_svr4_init_abi);
2914 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_GO32,
2915 i386_go32_init_abi);
2917 /* Initialize the i386-specific register groups & types. */
2918 i386_init_reggroups ();