GDB (API)
/home/stan/gdb/src/gdb/i386-tdep.c
Go to the documentation of this file.
00001 /* Intel 386 target-dependent stuff.
00002 
00003    Copyright (C) 1988-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "opcode/i386.h"
00022 #include "arch-utils.h"
00023 #include "command.h"
00024 #include "dummy-frame.h"
00025 #include "dwarf2-frame.h"
00026 #include "doublest.h"
00027 #include "frame.h"
00028 #include "frame-base.h"
00029 #include "frame-unwind.h"
00030 #include "inferior.h"
00031 #include "gdbcmd.h"
00032 #include "gdbcore.h"
00033 #include "gdbtypes.h"
00034 #include "objfiles.h"
00035 #include "osabi.h"
00036 #include "regcache.h"
00037 #include "reggroups.h"
00038 #include "regset.h"
00039 #include "symfile.h"
00040 #include "symtab.h"
00041 #include "target.h"
00042 #include "value.h"
00043 #include "dis-asm.h"
00044 #include "disasm.h"
00045 #include "remote.h"
00046 #include "exceptions.h"
00047 #include "gdb_assert.h"
00048 #include "gdb_string.h"
00049 
00050 #include "i386-tdep.h"
00051 #include "i387-tdep.h"
00052 #include "i386-xstate.h"
00053 
00054 #include "record.h"
00055 #include "record-full.h"
00056 #include <stdint.h>
00057 
00058 #include "features/i386/i386.c"
00059 #include "features/i386/i386-avx.c"
00060 #include "features/i386/i386-mmx.c"
00061 
00062 #include "ax.h"
00063 #include "ax-gdb.h"
00064 
00065 #include "stap-probe.h"
00066 #include "user-regs.h"
00067 #include "cli/cli-utils.h"
00068 #include "expression.h"
00069 #include "parser-defs.h"
00070 #include <ctype.h>
00071 
00072 /* Register names.  */
00073 
00074 static const char *i386_register_names[] =
00075 {
00076   "eax",   "ecx",    "edx",   "ebx",
00077   "esp",   "ebp",    "esi",   "edi",
00078   "eip",   "eflags", "cs",    "ss",
00079   "ds",    "es",     "fs",    "gs",
00080   "st0",   "st1",    "st2",   "st3",
00081   "st4",   "st5",    "st6",   "st7",
00082   "fctrl", "fstat",  "ftag",  "fiseg",
00083   "fioff", "foseg",  "fooff", "fop",
00084   "xmm0",  "xmm1",   "xmm2",  "xmm3",
00085   "xmm4",  "xmm5",   "xmm6",  "xmm7",
00086   "mxcsr"
00087 };
00088 
00089 static const char *i386_ymm_names[] =
00090 {
00091   "ymm0",  "ymm1",   "ymm2",  "ymm3",
00092   "ymm4",  "ymm5",   "ymm6",  "ymm7",
00093 };
00094 
00095 static const char *i386_ymmh_names[] =
00096 {
00097   "ymm0h",  "ymm1h",   "ymm2h",  "ymm3h",
00098   "ymm4h",  "ymm5h",   "ymm6h",  "ymm7h",
00099 };
00100 
00101 /* Register names for MMX pseudo-registers.  */
00102 
00103 static const char *i386_mmx_names[] =
00104 {
00105   "mm0", "mm1", "mm2", "mm3",
00106   "mm4", "mm5", "mm6", "mm7"
00107 };
00108 
00109 /* Register names for byte pseudo-registers.  */
00110 
00111 static const char *i386_byte_names[] =
00112 {
00113   "al", "cl", "dl", "bl", 
00114   "ah", "ch", "dh", "bh"
00115 };
00116 
00117 /* Register names for word pseudo-registers.  */
00118 
00119 static const char *i386_word_names[] =
00120 {
00121   "ax", "cx", "dx", "bx",
00122   "", "bp", "si", "di"
00123 };
00124 
00125 /* MMX register?  */
00126 
00127 static int
00128 i386_mmx_regnum_p (struct gdbarch *gdbarch, int regnum)
00129 {
00130   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00131   int mm0_regnum = tdep->mm0_regnum;
00132 
00133   if (mm0_regnum < 0)
00134     return 0;
00135 
00136   regnum -= mm0_regnum;
00137   return regnum >= 0 && regnum < tdep->num_mmx_regs;
00138 }
00139 
00140 /* Byte register?  */
00141 
00142 int
00143 i386_byte_regnum_p (struct gdbarch *gdbarch, int regnum)
00144 {
00145   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00146 
00147   regnum -= tdep->al_regnum;
00148   return regnum >= 0 && regnum < tdep->num_byte_regs;
00149 }
00150 
00151 /* Word register?  */
00152 
00153 int
00154 i386_word_regnum_p (struct gdbarch *gdbarch, int regnum)
00155 {
00156   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00157 
00158   regnum -= tdep->ax_regnum;
00159   return regnum >= 0 && regnum < tdep->num_word_regs;
00160 }
00161 
00162 /* Dword register?  */
00163 
00164 int
00165 i386_dword_regnum_p (struct gdbarch *gdbarch, int regnum)
00166 {
00167   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00168   int eax_regnum = tdep->eax_regnum;
00169 
00170   if (eax_regnum < 0)
00171     return 0;
00172 
00173   regnum -= eax_regnum;
00174   return regnum >= 0 && regnum < tdep->num_dword_regs;
00175 }
00176 
00177 static int
00178 i386_ymmh_regnum_p (struct gdbarch *gdbarch, int regnum)
00179 {
00180   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00181   int ymm0h_regnum = tdep->ymm0h_regnum;
00182 
00183   if (ymm0h_regnum < 0)
00184     return 0;
00185 
00186   regnum -= ymm0h_regnum;
00187   return regnum >= 0 && regnum < tdep->num_ymm_regs;
00188 }
00189 
00190 /* AVX register?  */
00191 
00192 int
00193 i386_ymm_regnum_p (struct gdbarch *gdbarch, int regnum)
00194 {
00195   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00196   int ymm0_regnum = tdep->ymm0_regnum;
00197 
00198   if (ymm0_regnum < 0)
00199     return 0;
00200 
00201   regnum -= ymm0_regnum;
00202   return regnum >= 0 && regnum < tdep->num_ymm_regs;
00203 }
00204 
00205 /* SSE register?  */
00206 
00207 int
00208 i386_xmm_regnum_p (struct gdbarch *gdbarch, int regnum)
00209 {
00210   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00211   int num_xmm_regs = I387_NUM_XMM_REGS (tdep);
00212 
00213   if (num_xmm_regs == 0)
00214     return 0;
00215 
00216   regnum -= I387_XMM0_REGNUM (tdep);
00217   return regnum >= 0 && regnum < num_xmm_regs;
00218 }
00219 
00220 static int
00221 i386_mxcsr_regnum_p (struct gdbarch *gdbarch, int regnum)
00222 {
00223   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00224 
00225   if (I387_NUM_XMM_REGS (tdep) == 0)
00226     return 0;
00227 
00228   return (regnum == I387_MXCSR_REGNUM (tdep));
00229 }
00230 
00231 /* FP register?  */
00232 
00233 int
00234 i386_fp_regnum_p (struct gdbarch *gdbarch, int regnum)
00235 {
00236   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00237 
00238   if (I387_ST0_REGNUM (tdep) < 0)
00239     return 0;
00240 
00241   return (I387_ST0_REGNUM (tdep) <= regnum
00242           && regnum < I387_FCTRL_REGNUM (tdep));
00243 }
00244 
00245 int
00246 i386_fpc_regnum_p (struct gdbarch *gdbarch, int regnum)
00247 {
00248   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00249 
00250   if (I387_ST0_REGNUM (tdep) < 0)
00251     return 0;
00252 
00253   return (I387_FCTRL_REGNUM (tdep) <= regnum 
00254           && regnum < I387_XMM0_REGNUM (tdep));
00255 }
00256 
00257 /* Return the name of register REGNUM, or the empty string if it is
00258    an anonymous register.  */
00259 
00260 static const char *
00261 i386_register_name (struct gdbarch *gdbarch, int regnum)
00262 {
00263   /* Hide the upper YMM registers.  */
00264   if (i386_ymmh_regnum_p (gdbarch, regnum))
00265     return "";
00266 
00267   return tdesc_register_name (gdbarch, regnum);
00268 }
00269 
00270 /* Return the name of register REGNUM.  */
00271 
00272 const char *
00273 i386_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
00274 {
00275   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00276   if (i386_mmx_regnum_p (gdbarch, regnum))
00277     return i386_mmx_names[regnum - I387_MM0_REGNUM (tdep)];
00278   else if (i386_ymm_regnum_p (gdbarch, regnum))
00279     return i386_ymm_names[regnum - tdep->ymm0_regnum];
00280   else if (i386_byte_regnum_p (gdbarch, regnum))
00281     return i386_byte_names[regnum - tdep->al_regnum];
00282   else if (i386_word_regnum_p (gdbarch, regnum))
00283     return i386_word_names[regnum - tdep->ax_regnum];
00284 
00285   internal_error (__FILE__, __LINE__, _("invalid regnum"));
00286 }
00287 
00288 /* Convert a dbx register number REG to the appropriate register
00289    number used by GDB.  */
00290 
00291 static int
00292 i386_dbx_reg_to_regnum (struct gdbarch *gdbarch, int reg)
00293 {
00294   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00295 
00296   /* This implements what GCC calls the "default" register map
00297      (dbx_register_map[]).  */
00298 
00299   if (reg >= 0 && reg <= 7)
00300     {
00301       /* General-purpose registers.  The debug info calls %ebp
00302          register 4, and %esp register 5.  */
00303       if (reg == 4)
00304         return 5;
00305       else if (reg == 5)
00306         return 4;
00307       else return reg;
00308     }
00309   else if (reg >= 12 && reg <= 19)
00310     {
00311       /* Floating-point registers.  */
00312       return reg - 12 + I387_ST0_REGNUM (tdep);
00313     }
00314   else if (reg >= 21 && reg <= 28)
00315     {
00316       /* SSE registers.  */
00317       int ymm0_regnum = tdep->ymm0_regnum;
00318 
00319       if (ymm0_regnum >= 0
00320           && i386_xmm_regnum_p (gdbarch, reg))
00321         return reg - 21 + ymm0_regnum;
00322       else
00323         return reg - 21 + I387_XMM0_REGNUM (tdep);
00324     }
00325   else if (reg >= 29 && reg <= 36)
00326     {
00327       /* MMX registers.  */
00328       return reg - 29 + I387_MM0_REGNUM (tdep);
00329     }
00330 
00331   /* This will hopefully provoke a warning.  */
00332   return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
00333 }
00334 
00335 /* Convert SVR4 register number REG to the appropriate register number
00336    used by GDB.  */
00337 
00338 static int
00339 i386_svr4_reg_to_regnum (struct gdbarch *gdbarch, int reg)
00340 {
00341   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00342 
00343   /* This implements the GCC register map that tries to be compatible
00344      with the SVR4 C compiler for DWARF (svr4_dbx_register_map[]).  */
00345 
00346   /* The SVR4 register numbering includes %eip and %eflags, and
00347      numbers the floating point registers differently.  */
00348   if (reg >= 0 && reg <= 9)
00349     {
00350       /* General-purpose registers.  */
00351       return reg;
00352     }
00353   else if (reg >= 11 && reg <= 18)
00354     {
00355       /* Floating-point registers.  */
00356       return reg - 11 + I387_ST0_REGNUM (tdep);
00357     }
00358   else if (reg >= 21 && reg <= 36)
00359     {
00360       /* The SSE and MMX registers have the same numbers as with dbx.  */
00361       return i386_dbx_reg_to_regnum (gdbarch, reg);
00362     }
00363 
00364   switch (reg)
00365     {
00366     case 37: return I387_FCTRL_REGNUM (tdep);
00367     case 38: return I387_FSTAT_REGNUM (tdep);
00368     case 39: return I387_MXCSR_REGNUM (tdep);
00369     case 40: return I386_ES_REGNUM;
00370     case 41: return I386_CS_REGNUM;
00371     case 42: return I386_SS_REGNUM;
00372     case 43: return I386_DS_REGNUM;
00373     case 44: return I386_FS_REGNUM;
00374     case 45: return I386_GS_REGNUM;
00375     }
00376 
00377   /* This will hopefully provoke a warning.  */
00378   return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
00379 }
00380 
00381 
00382 
00383 /* This is the variable that is set with "set disassembly-flavor", and
00384    its legitimate values.  */
00385 static const char att_flavor[] = "att";
00386 static const char intel_flavor[] = "intel";
00387 static const char *const valid_flavors[] =
00388 {
00389   att_flavor,
00390   intel_flavor,
00391   NULL
00392 };
00393 static const char *disassembly_flavor = att_flavor;
00394 
00395 
00396 /* Use the program counter to determine the contents and size of a
00397    breakpoint instruction.  Return a pointer to a string of bytes that
00398    encode a breakpoint instruction, store the length of the string in
00399    *LEN and optionally adjust *PC to point to the correct memory
00400    location for inserting the breakpoint.
00401 
00402    On the i386 we have a single breakpoint that fits in a single byte
00403    and can be inserted anywhere.
00404 
00405    This function is 64-bit safe.  */
00406 
00407 static const gdb_byte *
00408 i386_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
00409 {
00410   static gdb_byte break_insn[] = { 0xcc }; /* int 3 */
00411 
00412   *len = sizeof (break_insn);
00413   return break_insn;
00414 }
00415 
00416 /* Displaced instruction handling.  */
00417 
00418 /* Skip the legacy instruction prefixes in INSN.
00419    Not all prefixes are valid for any particular insn
00420    but we needn't care, the insn will fault if it's invalid.
00421    The result is a pointer to the first opcode byte,
00422    or NULL if we run off the end of the buffer.  */
00423 
00424 static gdb_byte *
00425 i386_skip_prefixes (gdb_byte *insn, size_t max_len)
00426 {
00427   gdb_byte *end = insn + max_len;
00428 
00429   while (insn < end)
00430     {
00431       switch (*insn)
00432         {
00433         case DATA_PREFIX_OPCODE:
00434         case ADDR_PREFIX_OPCODE:
00435         case CS_PREFIX_OPCODE:
00436         case DS_PREFIX_OPCODE:
00437         case ES_PREFIX_OPCODE:
00438         case FS_PREFIX_OPCODE:
00439         case GS_PREFIX_OPCODE:
00440         case SS_PREFIX_OPCODE:
00441         case LOCK_PREFIX_OPCODE:
00442         case REPE_PREFIX_OPCODE:
00443         case REPNE_PREFIX_OPCODE:
00444           ++insn;
00445           continue;
00446         default:
00447           return insn;
00448         }
00449     }
00450 
00451   return NULL;
00452 }
00453 
00454 static int
00455 i386_absolute_jmp_p (const gdb_byte *insn)
00456 {
00457   /* jmp far (absolute address in operand).  */
00458   if (insn[0] == 0xea)
00459     return 1;
00460 
00461   if (insn[0] == 0xff)
00462     {
00463       /* jump near, absolute indirect (/4).  */
00464       if ((insn[1] & 0x38) == 0x20)
00465         return 1;
00466 
00467       /* jump far, absolute indirect (/5).  */
00468       if ((insn[1] & 0x38) == 0x28)
00469         return 1;
00470     }
00471 
00472   return 0;
00473 }
00474 
00475 static int
00476 i386_absolute_call_p (const gdb_byte *insn)
00477 {
00478   /* call far, absolute.  */
00479   if (insn[0] == 0x9a)
00480     return 1;
00481 
00482   if (insn[0] == 0xff)
00483     {
00484       /* Call near, absolute indirect (/2).  */
00485       if ((insn[1] & 0x38) == 0x10)
00486         return 1;
00487 
00488       /* Call far, absolute indirect (/3).  */
00489       if ((insn[1] & 0x38) == 0x18)
00490         return 1;
00491     }
00492 
00493   return 0;
00494 }
00495 
00496 static int
00497 i386_ret_p (const gdb_byte *insn)
00498 {
00499   switch (insn[0])
00500     {
00501     case 0xc2: /* ret near, pop N bytes.  */
00502     case 0xc3: /* ret near */
00503     case 0xca: /* ret far, pop N bytes.  */
00504     case 0xcb: /* ret far */
00505     case 0xcf: /* iret */
00506       return 1;
00507 
00508     default:
00509       return 0;
00510     }
00511 }
00512 
00513 static int
00514 i386_call_p (const gdb_byte *insn)
00515 {
00516   if (i386_absolute_call_p (insn))
00517     return 1;
00518 
00519   /* call near, relative.  */
00520   if (insn[0] == 0xe8)
00521     return 1;
00522 
00523   return 0;
00524 }
00525 
00526 /* Return non-zero if INSN is a system call, and set *LENGTHP to its
00527    length in bytes.  Otherwise, return zero.  */
00528 
00529 static int
00530 i386_syscall_p (const gdb_byte *insn, int *lengthp)
00531 {
00532   /* Is it 'int $0x80'?  */
00533   if ((insn[0] == 0xcd && insn[1] == 0x80)
00534       /* Or is it 'sysenter'?  */
00535       || (insn[0] == 0x0f && insn[1] == 0x34)
00536       /* Or is it 'syscall'?  */
00537       || (insn[0] == 0x0f && insn[1] == 0x05))
00538     {
00539       *lengthp = 2;
00540       return 1;
00541     }
00542 
00543   return 0;
00544 }
00545 
00546 /* Some kernels may run one past a syscall insn, so we have to cope.
00547    Otherwise this is just simple_displaced_step_copy_insn.  */
00548 
00549 struct displaced_step_closure *
00550 i386_displaced_step_copy_insn (struct gdbarch *gdbarch,
00551                                CORE_ADDR from, CORE_ADDR to,
00552                                struct regcache *regs)
00553 {
00554   size_t len = gdbarch_max_insn_length (gdbarch);
00555   gdb_byte *buf = xmalloc (len);
00556 
00557   read_memory (from, buf, len);
00558 
00559   /* GDB may get control back after the insn after the syscall.
00560      Presumably this is a kernel bug.
00561      If this is a syscall, make sure there's a nop afterwards.  */
00562   {
00563     int syscall_length;
00564     gdb_byte *insn;
00565 
00566     insn = i386_skip_prefixes (buf, len);
00567     if (insn != NULL && i386_syscall_p (insn, &syscall_length))
00568       insn[syscall_length] = NOP_OPCODE;
00569   }
00570 
00571   write_memory (to, buf, len);
00572 
00573   if (debug_displaced)
00574     {
00575       fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
00576                           paddress (gdbarch, from), paddress (gdbarch, to));
00577       displaced_step_dump_bytes (gdb_stdlog, buf, len);
00578     }
00579 
00580   return (struct displaced_step_closure *) buf;
00581 }
00582 
00583 /* Fix up the state of registers and memory after having single-stepped
00584    a displaced instruction.  */
00585 
00586 void
00587 i386_displaced_step_fixup (struct gdbarch *gdbarch,
00588                            struct displaced_step_closure *closure,
00589                            CORE_ADDR from, CORE_ADDR to,
00590                            struct regcache *regs)
00591 {
00592   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00593 
00594   /* The offset we applied to the instruction's address.
00595      This could well be negative (when viewed as a signed 32-bit
00596      value), but ULONGEST won't reflect that, so take care when
00597      applying it.  */
00598   ULONGEST insn_offset = to - from;
00599 
00600   /* Since we use simple_displaced_step_copy_insn, our closure is a
00601      copy of the instruction.  */
00602   gdb_byte *insn = (gdb_byte *) closure;
00603   /* The start of the insn, needed in case we see some prefixes.  */
00604   gdb_byte *insn_start = insn;
00605 
00606   if (debug_displaced)
00607     fprintf_unfiltered (gdb_stdlog,
00608                         "displaced: fixup (%s, %s), "
00609                         "insn = 0x%02x 0x%02x ...\n",
00610                         paddress (gdbarch, from), paddress (gdbarch, to),
00611                         insn[0], insn[1]);
00612 
00613   /* The list of issues to contend with here is taken from
00614      resume_execution in arch/i386/kernel/kprobes.c, Linux 2.6.20.
00615      Yay for Free Software!  */
00616 
00617   /* Relocate the %eip, if necessary.  */
00618 
00619   /* The instruction recognizers we use assume any leading prefixes
00620      have been skipped.  */
00621   {
00622     /* This is the size of the buffer in closure.  */
00623     size_t max_insn_len = gdbarch_max_insn_length (gdbarch);
00624     gdb_byte *opcode = i386_skip_prefixes (insn, max_insn_len);
00625     /* If there are too many prefixes, just ignore the insn.
00626        It will fault when run.  */
00627     if (opcode != NULL)
00628       insn = opcode;
00629   }
00630 
00631   /* Except in the case of absolute or indirect jump or call
00632      instructions, or a return instruction, the new eip is relative to
00633      the displaced instruction; make it relative.  Well, signal
00634      handler returns don't need relocation either, but we use the
00635      value of %eip to recognize those; see below.  */
00636   if (! i386_absolute_jmp_p (insn)
00637       && ! i386_absolute_call_p (insn)
00638       && ! i386_ret_p (insn))
00639     {
00640       ULONGEST orig_eip;
00641       int insn_len;
00642 
00643       regcache_cooked_read_unsigned (regs, I386_EIP_REGNUM, &orig_eip);
00644 
00645       /* A signal trampoline system call changes the %eip, resuming
00646          execution of the main program after the signal handler has
00647          returned.  That makes them like 'return' instructions; we
00648          shouldn't relocate %eip.
00649 
00650          But most system calls don't, and we do need to relocate %eip.
00651 
00652          Our heuristic for distinguishing these cases: if stepping
00653          over the system call instruction left control directly after
00654          the instruction, the we relocate --- control almost certainly
00655          doesn't belong in the displaced copy.  Otherwise, we assume
00656          the instruction has put control where it belongs, and leave
00657          it unrelocated.  Goodness help us if there are PC-relative
00658          system calls.  */
00659       if (i386_syscall_p (insn, &insn_len)
00660           && orig_eip != to + (insn - insn_start) + insn_len
00661           /* GDB can get control back after the insn after the syscall.
00662              Presumably this is a kernel bug.
00663              i386_displaced_step_copy_insn ensures its a nop,
00664              we add one to the length for it.  */
00665           && orig_eip != to + (insn - insn_start) + insn_len + 1)
00666         {
00667           if (debug_displaced)
00668             fprintf_unfiltered (gdb_stdlog,
00669                                 "displaced: syscall changed %%eip; "
00670                                 "not relocating\n");
00671         }
00672       else
00673         {
00674           ULONGEST eip = (orig_eip - insn_offset) & 0xffffffffUL;
00675 
00676           /* If we just stepped over a breakpoint insn, we don't backup
00677              the pc on purpose; this is to match behaviour without
00678              stepping.  */
00679 
00680           regcache_cooked_write_unsigned (regs, I386_EIP_REGNUM, eip);
00681 
00682           if (debug_displaced)
00683             fprintf_unfiltered (gdb_stdlog,
00684                                 "displaced: "
00685                                 "relocated %%eip from %s to %s\n",
00686                                 paddress (gdbarch, orig_eip),
00687                                 paddress (gdbarch, eip));
00688         }
00689     }
00690 
00691   /* If the instruction was PUSHFL, then the TF bit will be set in the
00692      pushed value, and should be cleared.  We'll leave this for later,
00693      since GDB already messes up the TF flag when stepping over a
00694      pushfl.  */
00695 
00696   /* If the instruction was a call, the return address now atop the
00697      stack is the address following the copied instruction.  We need
00698      to make it the address following the original instruction.  */
00699   if (i386_call_p (insn))
00700     {
00701       ULONGEST esp;
00702       ULONGEST retaddr;
00703       const ULONGEST retaddr_len = 4;
00704 
00705       regcache_cooked_read_unsigned (regs, I386_ESP_REGNUM, &esp);
00706       retaddr = read_memory_unsigned_integer (esp, retaddr_len, byte_order);
00707       retaddr = (retaddr - insn_offset) & 0xffffffffUL;
00708       write_memory_unsigned_integer (esp, retaddr_len, byte_order, retaddr);
00709 
00710       if (debug_displaced)
00711         fprintf_unfiltered (gdb_stdlog,
00712                             "displaced: relocated return addr at %s to %s\n",
00713                             paddress (gdbarch, esp),
00714                             paddress (gdbarch, retaddr));
00715     }
00716 }
00717 
00718 static void
00719 append_insns (CORE_ADDR *to, ULONGEST len, const gdb_byte *buf)
00720 {
00721   target_write_memory (*to, buf, len);
00722   *to += len;
00723 }
00724 
00725 static void
00726 i386_relocate_instruction (struct gdbarch *gdbarch,
00727                            CORE_ADDR *to, CORE_ADDR oldloc)
00728 {
00729   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00730   gdb_byte buf[I386_MAX_INSN_LEN];
00731   int offset = 0, rel32, newrel;
00732   int insn_length;
00733   gdb_byte *insn = buf;
00734 
00735   read_memory (oldloc, buf, I386_MAX_INSN_LEN);
00736 
00737   insn_length = gdb_buffered_insn_length (gdbarch, insn,
00738                                           I386_MAX_INSN_LEN, oldloc);
00739 
00740   /* Get past the prefixes.  */
00741   insn = i386_skip_prefixes (insn, I386_MAX_INSN_LEN);
00742 
00743   /* Adjust calls with 32-bit relative addresses as push/jump, with
00744      the address pushed being the location where the original call in
00745      the user program would return to.  */
00746   if (insn[0] == 0xe8)
00747     {
00748       gdb_byte push_buf[16];
00749       unsigned int ret_addr;
00750 
00751       /* Where "ret" in the original code will return to.  */
00752       ret_addr = oldloc + insn_length;
00753       push_buf[0] = 0x68; /* pushq $...  */
00754       store_unsigned_integer (&push_buf[1], 4, byte_order, ret_addr);
00755       /* Push the push.  */
00756       append_insns (to, 5, push_buf);
00757 
00758       /* Convert the relative call to a relative jump.  */
00759       insn[0] = 0xe9;
00760 
00761       /* Adjust the destination offset.  */
00762       rel32 = extract_signed_integer (insn + 1, 4, byte_order);
00763       newrel = (oldloc - *to) + rel32;
00764       store_signed_integer (insn + 1, 4, byte_order, newrel);
00765 
00766       if (debug_displaced)
00767         fprintf_unfiltered (gdb_stdlog,
00768                             "Adjusted insn rel32=%s at %s to"
00769                             " rel32=%s at %s\n",
00770                             hex_string (rel32), paddress (gdbarch, oldloc),
00771                             hex_string (newrel), paddress (gdbarch, *to));
00772 
00773       /* Write the adjusted jump into its displaced location.  */
00774       append_insns (to, 5, insn);
00775       return;
00776     }
00777 
00778   /* Adjust jumps with 32-bit relative addresses.  Calls are already
00779      handled above.  */
00780   if (insn[0] == 0xe9)
00781     offset = 1;
00782   /* Adjust conditional jumps.  */
00783   else if (insn[0] == 0x0f && (insn[1] & 0xf0) == 0x80)
00784     offset = 2;
00785 
00786   if (offset)
00787     {
00788       rel32 = extract_signed_integer (insn + offset, 4, byte_order);
00789       newrel = (oldloc - *to) + rel32;
00790       store_signed_integer (insn + offset, 4, byte_order, newrel);
00791       if (debug_displaced)
00792         fprintf_unfiltered (gdb_stdlog,
00793                             "Adjusted insn rel32=%s at %s to"
00794                             " rel32=%s at %s\n",
00795                             hex_string (rel32), paddress (gdbarch, oldloc),
00796                             hex_string (newrel), paddress (gdbarch, *to));
00797     }
00798 
00799   /* Write the adjusted instructions into their displaced
00800      location.  */
00801   append_insns (to, insn_length, buf);
00802 }
00803 
00804 
00805 #ifdef I386_REGNO_TO_SYMMETRY
00806 #error "The Sequent Symmetry is no longer supported."
00807 #endif
00808 
00809 /* According to the System V ABI, the registers %ebp, %ebx, %edi, %esi
00810    and %esp "belong" to the calling function.  Therefore these
00811    registers should be saved if they're going to be modified.  */
00812 
00813 /* The maximum number of saved registers.  This should include all
00814    registers mentioned above, and %eip.  */
00815 #define I386_NUM_SAVED_REGS     I386_NUM_GREGS
00816 
00817 struct i386_frame_cache
00818 {
00819   /* Base address.  */
00820   CORE_ADDR base;
00821   int base_p;
00822   LONGEST sp_offset;
00823   CORE_ADDR pc;
00824 
00825   /* Saved registers.  */
00826   CORE_ADDR saved_regs[I386_NUM_SAVED_REGS];
00827   CORE_ADDR saved_sp;
00828   int saved_sp_reg;
00829   int pc_in_eax;
00830 
00831   /* Stack space reserved for local variables.  */
00832   long locals;
00833 };
00834 
00835 /* Allocate and initialize a frame cache.  */
00836 
00837 static struct i386_frame_cache *
00838 i386_alloc_frame_cache (void)
00839 {
00840   struct i386_frame_cache *cache;
00841   int i;
00842 
00843   cache = FRAME_OBSTACK_ZALLOC (struct i386_frame_cache);
00844 
00845   /* Base address.  */
00846   cache->base_p = 0;
00847   cache->base = 0;
00848   cache->sp_offset = -4;
00849   cache->pc = 0;
00850 
00851   /* Saved registers.  We initialize these to -1 since zero is a valid
00852      offset (that's where %ebp is supposed to be stored).  */
00853   for (i = 0; i < I386_NUM_SAVED_REGS; i++)
00854     cache->saved_regs[i] = -1;
00855   cache->saved_sp = 0;
00856   cache->saved_sp_reg = -1;
00857   cache->pc_in_eax = 0;
00858 
00859   /* Frameless until proven otherwise.  */
00860   cache->locals = -1;
00861 
00862   return cache;
00863 }
00864 
00865 /* If the instruction at PC is a jump, return the address of its
00866    target.  Otherwise, return PC.  */
00867 
00868 static CORE_ADDR
00869 i386_follow_jump (struct gdbarch *gdbarch, CORE_ADDR pc)
00870 {
00871   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00872   gdb_byte op;
00873   long delta = 0;
00874   int data16 = 0;
00875 
00876   if (target_read_memory (pc, &op, 1))
00877     return pc;
00878 
00879   if (op == 0x66)
00880     {
00881       data16 = 1;
00882       op = read_memory_unsigned_integer (pc + 1, 1, byte_order);
00883     }
00884 
00885   switch (op)
00886     {
00887     case 0xe9:
00888       /* Relative jump: if data16 == 0, disp32, else disp16.  */
00889       if (data16)
00890         {
00891           delta = read_memory_integer (pc + 2, 2, byte_order);
00892 
00893           /* Include the size of the jmp instruction (including the
00894              0x66 prefix).  */
00895           delta += 4;
00896         }
00897       else
00898         {
00899           delta = read_memory_integer (pc + 1, 4, byte_order);
00900 
00901           /* Include the size of the jmp instruction.  */
00902           delta += 5;
00903         }
00904       break;
00905     case 0xeb:
00906       /* Relative jump, disp8 (ignore data16).  */
00907       delta = read_memory_integer (pc + data16 + 1, 1, byte_order);
00908 
00909       delta += data16 + 2;
00910       break;
00911     }
00912 
00913   return pc + delta;
00914 }
00915 
00916 /* Check whether PC points at a prologue for a function returning a
00917    structure or union.  If so, it updates CACHE and returns the
00918    address of the first instruction after the code sequence that
00919    removes the "hidden" argument from the stack or CURRENT_PC,
00920    whichever is smaller.  Otherwise, return PC.  */
00921 
00922 static CORE_ADDR
00923 i386_analyze_struct_return (CORE_ADDR pc, CORE_ADDR current_pc,
00924                             struct i386_frame_cache *cache)
00925 {
00926   /* Functions that return a structure or union start with:
00927 
00928         popl %eax             0x58
00929         xchgl %eax, (%esp)    0x87 0x04 0x24
00930      or xchgl %eax, 0(%esp)   0x87 0x44 0x24 0x00
00931 
00932      (the System V compiler puts out the second `xchg' instruction,
00933      and the assembler doesn't try to optimize it, so the 'sib' form
00934      gets generated).  This sequence is used to get the address of the
00935      return buffer for a function that returns a structure.  */
00936   static gdb_byte proto1[3] = { 0x87, 0x04, 0x24 };
00937   static gdb_byte proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
00938   gdb_byte buf[4];
00939   gdb_byte op;
00940 
00941   if (current_pc <= pc)
00942     return pc;
00943 
00944   if (target_read_memory (pc, &op, 1))
00945     return pc;
00946 
00947   if (op != 0x58)               /* popl %eax */
00948     return pc;
00949 
00950   if (target_read_memory (pc + 1, buf, 4))
00951     return pc;
00952 
00953   if (memcmp (buf, proto1, 3) != 0 && memcmp (buf, proto2, 4) != 0)
00954     return pc;
00955 
00956   if (current_pc == pc)
00957     {
00958       cache->sp_offset += 4;
00959       return current_pc;
00960     }
00961 
00962   if (current_pc == pc + 1)
00963     {
00964       cache->pc_in_eax = 1;
00965       return current_pc;
00966     }
00967   
00968   if (buf[1] == proto1[1])
00969     return pc + 4;
00970   else
00971     return pc + 5;
00972 }
00973 
00974 static CORE_ADDR
00975 i386_skip_probe (CORE_ADDR pc)
00976 {
00977   /* A function may start with
00978 
00979         pushl constant
00980         call _probe
00981         addl $4, %esp
00982            
00983      followed by
00984 
00985         pushl %ebp
00986 
00987      etc.  */
00988   gdb_byte buf[8];
00989   gdb_byte op;
00990 
00991   if (target_read_memory (pc, &op, 1))
00992     return pc;
00993 
00994   if (op == 0x68 || op == 0x6a)
00995     {
00996       int delta;
00997 
00998       /* Skip past the `pushl' instruction; it has either a one-byte or a
00999          four-byte operand, depending on the opcode.  */
01000       if (op == 0x68)
01001         delta = 5;
01002       else
01003         delta = 2;
01004 
01005       /* Read the following 8 bytes, which should be `call _probe' (6
01006          bytes) followed by `addl $4,%esp' (2 bytes).  */
01007       read_memory (pc + delta, buf, sizeof (buf));
01008       if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
01009         pc += delta + sizeof (buf);
01010     }
01011 
01012   return pc;
01013 }
01014 
01015 /* GCC 4.1 and later, can put code in the prologue to realign the
01016    stack pointer.  Check whether PC points to such code, and update
01017    CACHE accordingly.  Return the first instruction after the code
01018    sequence or CURRENT_PC, whichever is smaller.  If we don't
01019    recognize the code, return PC.  */
01020 
01021 static CORE_ADDR
01022 i386_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
01023                           struct i386_frame_cache *cache)
01024 {
01025   /* There are 2 code sequences to re-align stack before the frame
01026      gets set up:
01027 
01028         1. Use a caller-saved saved register:
01029 
01030                 leal  4(%esp), %reg
01031                 andl  $-XXX, %esp
01032                 pushl -4(%reg)
01033 
01034         2. Use a callee-saved saved register:
01035 
01036                 pushl %reg
01037                 leal  8(%esp), %reg
01038                 andl  $-XXX, %esp
01039                 pushl -4(%reg)
01040 
01041      "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:
01042      
01043         0x83 0xe4 0xf0                  andl $-16, %esp
01044         0x81 0xe4 0x00 0xff 0xff 0xff   andl $-256, %esp
01045    */
01046 
01047   gdb_byte buf[14];
01048   int reg;
01049   int offset, offset_and;
01050   static int regnums[8] = {
01051     I386_EAX_REGNUM,            /* %eax */
01052     I386_ECX_REGNUM,            /* %ecx */
01053     I386_EDX_REGNUM,            /* %edx */
01054     I386_EBX_REGNUM,            /* %ebx */
01055     I386_ESP_REGNUM,            /* %esp */
01056     I386_EBP_REGNUM,            /* %ebp */
01057     I386_ESI_REGNUM,            /* %esi */
01058     I386_EDI_REGNUM             /* %edi */
01059   };
01060 
01061   if (target_read_memory (pc, buf, sizeof buf))
01062     return pc;
01063 
01064   /* Check caller-saved saved register.  The first instruction has
01065      to be "leal 4(%esp), %reg".  */
01066   if (buf[0] == 0x8d && buf[2] == 0x24 && buf[3] == 0x4)
01067     {
01068       /* MOD must be binary 10 and R/M must be binary 100.  */
01069       if ((buf[1] & 0xc7) != 0x44)
01070         return pc;
01071 
01072       /* REG has register number.  */
01073       reg = (buf[1] >> 3) & 7;
01074       offset = 4;
01075     }
01076   else
01077     {
01078       /* Check callee-saved saved register.  The first instruction
01079          has to be "pushl %reg".  */
01080       if ((buf[0] & 0xf8) != 0x50)
01081         return pc;
01082 
01083       /* Get register.  */
01084       reg = buf[0] & 0x7;
01085 
01086       /* The next instruction has to be "leal 8(%esp), %reg".  */
01087       if (buf[1] != 0x8d || buf[3] != 0x24 || buf[4] != 0x8)
01088         return pc;
01089 
01090       /* MOD must be binary 10 and R/M must be binary 100.  */
01091       if ((buf[2] & 0xc7) != 0x44)
01092         return pc;
01093       
01094       /* REG has register number.  Registers in pushl and leal have to
01095          be the same.  */
01096       if (reg != ((buf[2] >> 3) & 7))
01097         return pc;
01098 
01099       offset = 5;
01100     }
01101 
01102   /* Rigister can't be %esp nor %ebp.  */
01103   if (reg == 4 || reg == 5)
01104     return pc;
01105 
01106   /* The next instruction has to be "andl $-XXX, %esp".  */
01107   if (buf[offset + 1] != 0xe4
01108       || (buf[offset] != 0x81 && buf[offset] != 0x83))
01109     return pc;
01110 
01111   offset_and = offset;
01112   offset += buf[offset] == 0x81 ? 6 : 3;
01113 
01114   /* The next instruction has to be "pushl -4(%reg)".  8bit -4 is
01115      0xfc.  REG must be binary 110 and MOD must be binary 01.  */
01116   if (buf[offset] != 0xff
01117       || buf[offset + 2] != 0xfc
01118       || (buf[offset + 1] & 0xf8) != 0x70)
01119     return pc;
01120 
01121   /* R/M has register.  Registers in leal and pushl have to be the
01122      same.  */
01123   if (reg != (buf[offset + 1] & 7))
01124     return pc;
01125 
01126   if (current_pc > pc + offset_and)
01127     cache->saved_sp_reg = regnums[reg];
01128 
01129   return min (pc + offset + 3, current_pc);
01130 }
01131 
01132 /* Maximum instruction length we need to handle.  */
01133 #define I386_MAX_MATCHED_INSN_LEN       6
01134 
01135 /* Instruction description.  */
01136 struct i386_insn
01137 {
01138   size_t len;
01139   gdb_byte insn[I386_MAX_MATCHED_INSN_LEN];
01140   gdb_byte mask[I386_MAX_MATCHED_INSN_LEN];
01141 };
01142 
01143 /* Return whether instruction at PC matches PATTERN.  */
01144 
01145 static int
01146 i386_match_pattern (CORE_ADDR pc, struct i386_insn pattern)
01147 {
01148   gdb_byte op;
01149 
01150   if (target_read_memory (pc, &op, 1))
01151     return 0;
01152 
01153   if ((op & pattern.mask[0]) == pattern.insn[0])
01154     {
01155       gdb_byte buf[I386_MAX_MATCHED_INSN_LEN - 1];
01156       int insn_matched = 1;
01157       size_t i;
01158 
01159       gdb_assert (pattern.len > 1);
01160       gdb_assert (pattern.len <= I386_MAX_MATCHED_INSN_LEN);
01161 
01162       if (target_read_memory (pc + 1, buf, pattern.len - 1))
01163         return 0;
01164 
01165       for (i = 1; i < pattern.len; i++)
01166         {
01167           if ((buf[i - 1] & pattern.mask[i]) != pattern.insn[i])
01168             insn_matched = 0;
01169         }
01170       return insn_matched;
01171     }
01172   return 0;
01173 }
01174 
01175 /* Search for the instruction at PC in the list INSN_PATTERNS.  Return
01176    the first instruction description that matches.  Otherwise, return
01177    NULL.  */
01178 
01179 static struct i386_insn *
01180 i386_match_insn (CORE_ADDR pc, struct i386_insn *insn_patterns)
01181 {
01182   struct i386_insn *pattern;
01183 
01184   for (pattern = insn_patterns; pattern->len > 0; pattern++)
01185     {
01186       if (i386_match_pattern (pc, *pattern))
01187         return pattern;
01188     }
01189 
01190   return NULL;
01191 }
01192 
01193 /* Return whether PC points inside a sequence of instructions that
01194    matches INSN_PATTERNS.  */
01195 
01196 static int
01197 i386_match_insn_block (CORE_ADDR pc, struct i386_insn *insn_patterns)
01198 {
01199   CORE_ADDR current_pc;
01200   int ix, i;
01201   struct i386_insn *insn;
01202 
01203   insn = i386_match_insn (pc, insn_patterns);
01204   if (insn == NULL)
01205     return 0;
01206 
01207   current_pc = pc;
01208   ix = insn - insn_patterns;
01209   for (i = ix - 1; i >= 0; i--)
01210     {
01211       current_pc -= insn_patterns[i].len;
01212 
01213       if (!i386_match_pattern (current_pc, insn_patterns[i]))
01214         return 0;
01215     }
01216 
01217   current_pc = pc + insn->len;
01218   for (insn = insn_patterns + ix + 1; insn->len > 0; insn++)
01219     {
01220       if (!i386_match_pattern (current_pc, *insn))
01221         return 0;
01222 
01223       current_pc += insn->len;
01224     }
01225 
01226   return 1;
01227 }
01228 
01229 /* Some special instructions that might be migrated by GCC into the
01230    part of the prologue that sets up the new stack frame.  Because the
01231    stack frame hasn't been setup yet, no registers have been saved
01232    yet, and only the scratch registers %eax, %ecx and %edx can be
01233    touched.  */
01234 
01235 struct i386_insn i386_frame_setup_skip_insns[] =
01236 {
01237   /* Check for `movb imm8, r' and `movl imm32, r'.
01238     
01239      ??? Should we handle 16-bit operand-sizes here?  */
01240 
01241   /* `movb imm8, %al' and `movb imm8, %ah' */
01242   /* `movb imm8, %cl' and `movb imm8, %ch' */
01243   { 2, { 0xb0, 0x00 }, { 0xfa, 0x00 } },
01244   /* `movb imm8, %dl' and `movb imm8, %dh' */
01245   { 2, { 0xb2, 0x00 }, { 0xfb, 0x00 } },
01246   /* `movl imm32, %eax' and `movl imm32, %ecx' */
01247   { 5, { 0xb8 }, { 0xfe } },
01248   /* `movl imm32, %edx' */
01249   { 5, { 0xba }, { 0xff } },
01250 
01251   /* Check for `mov imm32, r32'.  Note that there is an alternative
01252      encoding for `mov m32, %eax'.
01253 
01254      ??? Should we handle SIB adressing here?
01255      ??? Should we handle 16-bit operand-sizes here?  */
01256 
01257   /* `movl m32, %eax' */
01258   { 5, { 0xa1 }, { 0xff } },
01259   /* `movl m32, %eax' and `mov; m32, %ecx' */
01260   { 6, { 0x89, 0x05 }, {0xff, 0xf7 } },
01261   /* `movl m32, %edx' */
01262   { 6, { 0x89, 0x15 }, {0xff, 0xff } },
01263 
01264   /* Check for `xorl r32, r32' and the equivalent `subl r32, r32'.
01265      Because of the symmetry, there are actually two ways to encode
01266      these instructions; opcode bytes 0x29 and 0x2b for `subl' and
01267      opcode bytes 0x31 and 0x33 for `xorl'.  */
01268 
01269   /* `subl %eax, %eax' */
01270   { 2, { 0x29, 0xc0 }, { 0xfd, 0xff } },
01271   /* `subl %ecx, %ecx' */
01272   { 2, { 0x29, 0xc9 }, { 0xfd, 0xff } },
01273   /* `subl %edx, %edx' */
01274   { 2, { 0x29, 0xd2 }, { 0xfd, 0xff } },
01275   /* `xorl %eax, %eax' */
01276   { 2, { 0x31, 0xc0 }, { 0xfd, 0xff } },
01277   /* `xorl %ecx, %ecx' */
01278   { 2, { 0x31, 0xc9 }, { 0xfd, 0xff } },
01279   /* `xorl %edx, %edx' */
01280   { 2, { 0x31, 0xd2 }, { 0xfd, 0xff } },
01281   { 0 }
01282 };
01283 
01284 
01285 /* Check whether PC points to a no-op instruction.  */
01286 static CORE_ADDR
01287 i386_skip_noop (CORE_ADDR pc)
01288 {
01289   gdb_byte op;
01290   int check = 1;
01291 
01292   if (target_read_memory (pc, &op, 1))
01293     return pc;
01294 
01295   while (check) 
01296     {
01297       check = 0;
01298       /* Ignore `nop' instruction.  */
01299       if (op == 0x90) 
01300         {
01301           pc += 1;
01302           if (target_read_memory (pc, &op, 1))
01303             return pc;
01304           check = 1;
01305         }
01306       /* Ignore no-op instruction `mov %edi, %edi'.
01307          Microsoft system dlls often start with
01308          a `mov %edi,%edi' instruction.
01309          The 5 bytes before the function start are
01310          filled with `nop' instructions.
01311          This pattern can be used for hot-patching:
01312          The `mov %edi, %edi' instruction can be replaced by a
01313          near jump to the location of the 5 `nop' instructions
01314          which can be replaced by a 32-bit jump to anywhere
01315          in the 32-bit address space.  */
01316 
01317       else if (op == 0x8b)
01318         {
01319           if (target_read_memory (pc + 1, &op, 1))
01320             return pc;
01321 
01322           if (op == 0xff)
01323             {
01324               pc += 2;
01325               if (target_read_memory (pc, &op, 1))
01326                 return pc;
01327 
01328               check = 1;
01329             }
01330         }
01331     }
01332   return pc; 
01333 }
01334 
01335 /* Check whether PC points at a code that sets up a new stack frame.
01336    If so, it updates CACHE and returns the address of the first
01337    instruction after the sequence that sets up the frame or LIMIT,
01338    whichever is smaller.  If we don't recognize the code, return PC.  */
01339 
01340 static CORE_ADDR
01341 i386_analyze_frame_setup (struct gdbarch *gdbarch,
01342                           CORE_ADDR pc, CORE_ADDR limit,
01343                           struct i386_frame_cache *cache)
01344 {
01345   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01346   struct i386_insn *insn;
01347   gdb_byte op;
01348   int skip = 0;
01349 
01350   if (limit <= pc)
01351     return limit;
01352 
01353   if (target_read_memory (pc, &op, 1))
01354     return pc;
01355 
01356   if (op == 0x55)               /* pushl %ebp */
01357     {
01358       /* Take into account that we've executed the `pushl %ebp' that
01359          starts this instruction sequence.  */
01360       cache->saved_regs[I386_EBP_REGNUM] = 0;
01361       cache->sp_offset += 4;
01362       pc++;
01363 
01364       /* If that's all, return now.  */
01365       if (limit <= pc)
01366         return limit;
01367 
01368       /* Check for some special instructions that might be migrated by
01369          GCC into the prologue and skip them.  At this point in the
01370          prologue, code should only touch the scratch registers %eax,
01371          %ecx and %edx, so while the number of posibilities is sheer,
01372          it is limited.
01373 
01374          Make sure we only skip these instructions if we later see the
01375          `movl %esp, %ebp' that actually sets up the frame.  */
01376       while (pc + skip < limit)
01377         {
01378           insn = i386_match_insn (pc + skip, i386_frame_setup_skip_insns);
01379           if (insn == NULL)
01380             break;
01381 
01382           skip += insn->len;
01383         }
01384 
01385       /* If that's all, return now.  */
01386       if (limit <= pc + skip)
01387         return limit;
01388 
01389       if (target_read_memory (pc + skip, &op, 1))
01390         return pc + skip;
01391 
01392       /* The i386 prologue looks like
01393 
01394          push   %ebp
01395          mov    %esp,%ebp
01396          sub    $0x10,%esp
01397 
01398          and a different prologue can be generated for atom.
01399 
01400          push   %ebp
01401          lea    (%esp),%ebp
01402          lea    -0x10(%esp),%esp
01403 
01404          We handle both of them here.  */
01405 
01406       switch (op)
01407         {
01408           /* Check for `movl %esp, %ebp' -- can be written in two ways.  */
01409         case 0x8b:
01410           if (read_memory_unsigned_integer (pc + skip + 1, 1, byte_order)
01411               != 0xec)
01412             return pc;
01413           pc += (skip + 2);
01414           break;
01415         case 0x89:
01416           if (read_memory_unsigned_integer (pc + skip + 1, 1, byte_order)
01417               != 0xe5)
01418             return pc;
01419           pc += (skip + 2);
01420           break;
01421         case 0x8d: /* Check for 'lea (%ebp), %ebp'.  */
01422           if (read_memory_unsigned_integer (pc + skip + 1, 2, byte_order)
01423               != 0x242c)
01424             return pc;
01425           pc += (skip + 3);
01426           break;
01427         default:
01428           return pc;
01429         }
01430 
01431       /* OK, we actually have a frame.  We just don't know how large
01432          it is yet.  Set its size to zero.  We'll adjust it if
01433          necessary.  We also now commit to skipping the special
01434          instructions mentioned before.  */
01435       cache->locals = 0;
01436 
01437       /* If that's all, return now.  */
01438       if (limit <= pc)
01439         return limit;
01440 
01441       /* Check for stack adjustment 
01442 
01443             subl $XXX, %esp
01444          or
01445             lea -XXX(%esp),%esp
01446 
01447          NOTE: You can't subtract a 16-bit immediate from a 32-bit
01448          reg, so we don't have to worry about a data16 prefix.  */
01449       if (target_read_memory (pc, &op, 1))
01450         return pc;
01451       if (op == 0x83)
01452         {
01453           /* `subl' with 8-bit immediate.  */
01454           if (read_memory_unsigned_integer (pc + 1, 1, byte_order) != 0xec)
01455             /* Some instruction starting with 0x83 other than `subl'.  */
01456             return pc;
01457 
01458           /* `subl' with signed 8-bit immediate (though it wouldn't
01459              make sense to be negative).  */
01460           cache->locals = read_memory_integer (pc + 2, 1, byte_order);
01461           return pc + 3;
01462         }
01463       else if (op == 0x81)
01464         {
01465           /* Maybe it is `subl' with a 32-bit immediate.  */
01466           if (read_memory_unsigned_integer (pc + 1, 1, byte_order) != 0xec)
01467             /* Some instruction starting with 0x81 other than `subl'.  */
01468             return pc;
01469 
01470           /* It is `subl' with a 32-bit immediate.  */
01471           cache->locals = read_memory_integer (pc + 2, 4, byte_order);
01472           return pc + 6;
01473         }
01474       else if (op == 0x8d)
01475         {
01476           /* The ModR/M byte is 0x64.  */
01477           if (read_memory_unsigned_integer (pc + 1, 1, byte_order) != 0x64)
01478             return pc;
01479           /* 'lea' with 8-bit displacement.  */
01480           cache->locals = -1 * read_memory_integer (pc + 3, 1, byte_order);
01481           return pc + 4;
01482         }
01483       else
01484         {
01485           /* Some instruction other than `subl' nor 'lea'.  */
01486           return pc;
01487         }
01488     }
01489   else if (op == 0xc8)          /* enter */
01490     {
01491       cache->locals = read_memory_unsigned_integer (pc + 1, 2, byte_order);
01492       return pc + 4;
01493     }
01494 
01495   return pc;
01496 }
01497 
01498 /* Check whether PC points at code that saves registers on the stack.
01499    If so, it updates CACHE and returns the address of the first
01500    instruction after the register saves or CURRENT_PC, whichever is
01501    smaller.  Otherwise, return PC.  */
01502 
01503 static CORE_ADDR
01504 i386_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
01505                              struct i386_frame_cache *cache)
01506 {
01507   CORE_ADDR offset = 0;
01508   gdb_byte op;
01509   int i;
01510 
01511   if (cache->locals > 0)
01512     offset -= cache->locals;
01513   for (i = 0; i < 8 && pc < current_pc; i++)
01514     {
01515       if (target_read_memory (pc, &op, 1))
01516         return pc;
01517       if (op < 0x50 || op > 0x57)
01518         break;
01519 
01520       offset -= 4;
01521       cache->saved_regs[op - 0x50] = offset;
01522       cache->sp_offset += 4;
01523       pc++;
01524     }
01525 
01526   return pc;
01527 }
01528 
01529 /* Do a full analysis of the prologue at PC and update CACHE
01530    accordingly.  Bail out early if CURRENT_PC is reached.  Return the
01531    address where the analysis stopped.
01532 
01533    We handle these cases:
01534 
01535    The startup sequence can be at the start of the function, or the
01536    function can start with a branch to startup code at the end.
01537 
01538    %ebp can be set up with either the 'enter' instruction, or "pushl
01539    %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
01540    once used in the System V compiler).
01541 
01542    Local space is allocated just below the saved %ebp by either the
01543    'enter' instruction, or by "subl $<size>, %esp".  'enter' has a
01544    16-bit unsigned argument for space to allocate, and the 'addl'
01545    instruction could have either a signed byte, or 32-bit immediate.
01546 
01547    Next, the registers used by this function are pushed.  With the
01548    System V compiler they will always be in the order: %edi, %esi,
01549    %ebx (and sometimes a harmless bug causes it to also save but not
01550    restore %eax); however, the code below is willing to see the pushes
01551    in any order, and will handle up to 8 of them.
01552  
01553    If the setup sequence is at the end of the function, then the next
01554    instruction will be a branch back to the start.  */
01555 
01556 static CORE_ADDR
01557 i386_analyze_prologue (struct gdbarch *gdbarch,
01558                        CORE_ADDR pc, CORE_ADDR current_pc,
01559                        struct i386_frame_cache *cache)
01560 {
01561   pc = i386_skip_noop (pc);
01562   pc = i386_follow_jump (gdbarch, pc);
01563   pc = i386_analyze_struct_return (pc, current_pc, cache);
01564   pc = i386_skip_probe (pc);
01565   pc = i386_analyze_stack_align (pc, current_pc, cache);
01566   pc = i386_analyze_frame_setup (gdbarch, pc, current_pc, cache);
01567   return i386_analyze_register_saves (pc, current_pc, cache);
01568 }
01569 
01570 /* Return PC of first real instruction.  */
01571 
01572 static CORE_ADDR
01573 i386_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
01574 {
01575   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01576 
01577   static gdb_byte pic_pat[6] =
01578   {
01579     0xe8, 0, 0, 0, 0,           /* call 0x0 */
01580     0x5b,                       /* popl %ebx */
01581   };
01582   struct i386_frame_cache cache;
01583   CORE_ADDR pc;
01584   gdb_byte op;
01585   int i;
01586   CORE_ADDR func_addr;
01587 
01588   if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
01589     {
01590       CORE_ADDR post_prologue_pc
01591         = skip_prologue_using_sal (gdbarch, func_addr);
01592       struct symtab *s = find_pc_symtab (func_addr);
01593 
01594       /* Clang always emits a line note before the prologue and another
01595          one after.  We trust clang to emit usable line notes.  */
01596       if (post_prologue_pc
01597           && (s != NULL
01598               && s->producer != NULL
01599               && strncmp (s->producer, "clang ", sizeof ("clang ") - 1) == 0))
01600         return max (start_pc, post_prologue_pc);
01601     }
01602  
01603   cache.locals = -1;
01604   pc = i386_analyze_prologue (gdbarch, start_pc, 0xffffffff, &cache);
01605   if (cache.locals < 0)
01606     return start_pc;
01607 
01608   /* Found valid frame setup.  */
01609 
01610   /* The native cc on SVR4 in -K PIC mode inserts the following code
01611      to get the address of the global offset table (GOT) into register
01612      %ebx:
01613 
01614         call    0x0
01615         popl    %ebx
01616         movl    %ebx,x(%ebp)    (optional)
01617         addl    y,%ebx
01618 
01619      This code is with the rest of the prologue (at the end of the
01620      function), so we have to skip it to get to the first real
01621      instruction at the start of the function.  */
01622 
01623   for (i = 0; i < 6; i++)
01624     {
01625       if (target_read_memory (pc + i, &op, 1))
01626         return pc;
01627 
01628       if (pic_pat[i] != op)
01629         break;
01630     }
01631   if (i == 6)
01632     {
01633       int delta = 6;
01634 
01635       if (target_read_memory (pc + delta, &op, 1))
01636         return pc;
01637 
01638       if (op == 0x89)           /* movl %ebx, x(%ebp) */
01639         {
01640           op = read_memory_unsigned_integer (pc + delta + 1, 1, byte_order);
01641 
01642           if (op == 0x5d)       /* One byte offset from %ebp.  */
01643             delta += 3;
01644           else if (op == 0x9d)  /* Four byte offset from %ebp.  */
01645             delta += 6;
01646           else                  /* Unexpected instruction.  */
01647             delta = 0;
01648 
01649           if (target_read_memory (pc + delta, &op, 1))
01650             return pc;
01651         }
01652 
01653       /* addl y,%ebx */
01654       if (delta > 0 && op == 0x81
01655           && read_memory_unsigned_integer (pc + delta + 1, 1, byte_order)
01656              == 0xc3)
01657         {
01658           pc += delta + 6;
01659         }
01660     }
01661 
01662   /* If the function starts with a branch (to startup code at the end)
01663      the last instruction should bring us back to the first
01664      instruction of the real code.  */
01665   if (i386_follow_jump (gdbarch, start_pc) != start_pc)
01666     pc = i386_follow_jump (gdbarch, pc);
01667 
01668   return pc;
01669 }
01670 
01671 /* Check that the code pointed to by PC corresponds to a call to
01672    __main, skip it if so.  Return PC otherwise.  */
01673 
01674 CORE_ADDR
01675 i386_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
01676 {
01677   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01678   gdb_byte op;
01679 
01680   if (target_read_memory (pc, &op, 1))
01681     return pc;
01682   if (op == 0xe8)
01683     {
01684       gdb_byte buf[4];
01685 
01686       if (target_read_memory (pc + 1, buf, sizeof buf) == 0)
01687         {
01688           /* Make sure address is computed correctly as a 32bit
01689              integer even if CORE_ADDR is 64 bit wide.  */
01690           struct bound_minimal_symbol s;
01691           CORE_ADDR call_dest;
01692 
01693           call_dest = pc + 5 + extract_signed_integer (buf, 4, byte_order);
01694           call_dest = call_dest & 0xffffffffU;
01695           s = lookup_minimal_symbol_by_pc (call_dest);
01696           if (s.minsym != NULL
01697               && SYMBOL_LINKAGE_NAME (s.minsym) != NULL
01698               && strcmp (SYMBOL_LINKAGE_NAME (s.minsym), "__main") == 0)
01699             pc += 5;
01700         }
01701     }
01702 
01703   return pc;
01704 }
01705 
01706 /* This function is 64-bit safe.  */
01707 
01708 static CORE_ADDR
01709 i386_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
01710 {
01711   gdb_byte buf[8];
01712 
01713   frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
01714   return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
01715 }
01716 
01717 
01718 /* Normal frames.  */
01719 
01720 static void
01721 i386_frame_cache_1 (struct frame_info *this_frame,
01722                     struct i386_frame_cache *cache)
01723 {
01724   struct gdbarch *gdbarch = get_frame_arch (this_frame);
01725   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01726   gdb_byte buf[4];
01727   int i;
01728 
01729   cache->pc = get_frame_func (this_frame);
01730 
01731   /* In principle, for normal frames, %ebp holds the frame pointer,
01732      which holds the base address for the current stack frame.
01733      However, for functions that don't need it, the frame pointer is
01734      optional.  For these "frameless" functions the frame pointer is
01735      actually the frame pointer of the calling frame.  Signal
01736      trampolines are just a special case of a "frameless" function.
01737      They (usually) share their frame pointer with the frame that was
01738      in progress when the signal occurred.  */
01739 
01740   get_frame_register (this_frame, I386_EBP_REGNUM, buf);
01741   cache->base = extract_unsigned_integer (buf, 4, byte_order);
01742   if (cache->base == 0)
01743     {
01744       cache->base_p = 1;
01745       return;
01746     }
01747 
01748   /* For normal frames, %eip is stored at 4(%ebp).  */
01749   cache->saved_regs[I386_EIP_REGNUM] = 4;
01750 
01751   if (cache->pc != 0)
01752     i386_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
01753                            cache);
01754 
01755   if (cache->locals < 0)
01756     {
01757       /* We didn't find a valid frame, which means that CACHE->base
01758          currently holds the frame pointer for our calling frame.  If
01759          we're at the start of a function, or somewhere half-way its
01760          prologue, the function's frame probably hasn't been fully
01761          setup yet.  Try to reconstruct the base address for the stack
01762          frame by looking at the stack pointer.  For truly "frameless"
01763          functions this might work too.  */
01764 
01765       if (cache->saved_sp_reg != -1)
01766         {
01767           /* Saved stack pointer has been saved.  */
01768           get_frame_register (this_frame, cache->saved_sp_reg, buf);
01769           cache->saved_sp = extract_unsigned_integer (buf, 4, byte_order);
01770 
01771           /* We're halfway aligning the stack.  */
01772           cache->base = ((cache->saved_sp - 4) & 0xfffffff0) - 4;
01773           cache->saved_regs[I386_EIP_REGNUM] = cache->saved_sp - 4;
01774 
01775           /* This will be added back below.  */
01776           cache->saved_regs[I386_EIP_REGNUM] -= cache->base;
01777         }
01778       else if (cache->pc != 0
01779                || target_read_memory (get_frame_pc (this_frame), buf, 1))
01780         {
01781           /* We're in a known function, but did not find a frame
01782              setup.  Assume that the function does not use %ebp.
01783              Alternatively, we may have jumped to an invalid
01784              address; in that case there is definitely no new
01785              frame in %ebp.  */
01786           get_frame_register (this_frame, I386_ESP_REGNUM, buf);
01787           cache->base = extract_unsigned_integer (buf, 4, byte_order)
01788                         + cache->sp_offset;
01789         }
01790       else
01791         /* We're in an unknown function.  We could not find the start
01792            of the function to analyze the prologue; our best option is
01793            to assume a typical frame layout with the caller's %ebp
01794            saved.  */
01795         cache->saved_regs[I386_EBP_REGNUM] = 0;
01796     }
01797 
01798   if (cache->saved_sp_reg != -1)
01799     {
01800       /* Saved stack pointer has been saved (but the SAVED_SP_REG
01801          register may be unavailable).  */
01802       if (cache->saved_sp == 0
01803           && deprecated_frame_register_read (this_frame,
01804                                              cache->saved_sp_reg, buf))
01805         cache->saved_sp = extract_unsigned_integer (buf, 4, byte_order);
01806     }
01807   /* Now that we have the base address for the stack frame we can
01808      calculate the value of %esp in the calling frame.  */
01809   else if (cache->saved_sp == 0)
01810     cache->saved_sp = cache->base + 8;
01811 
01812   /* Adjust all the saved registers such that they contain addresses
01813      instead of offsets.  */
01814   for (i = 0; i < I386_NUM_SAVED_REGS; i++)
01815     if (cache->saved_regs[i] != -1)
01816       cache->saved_regs[i] += cache->base;
01817 
01818   cache->base_p = 1;
01819 }
01820 
01821 static struct i386_frame_cache *
01822 i386_frame_cache (struct frame_info *this_frame, void **this_cache)
01823 {
01824   volatile struct gdb_exception ex;
01825   struct i386_frame_cache *cache;
01826 
01827   if (*this_cache)
01828     return *this_cache;
01829 
01830   cache = i386_alloc_frame_cache ();
01831   *this_cache = cache;
01832 
01833   TRY_CATCH (ex, RETURN_MASK_ERROR)
01834     {
01835       i386_frame_cache_1 (this_frame, cache);
01836     }
01837   if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
01838     throw_exception (ex);
01839 
01840   return cache;
01841 }
01842 
01843 static void
01844 i386_frame_this_id (struct frame_info *this_frame, void **this_cache,
01845                     struct frame_id *this_id)
01846 {
01847   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
01848 
01849   /* This marks the outermost frame.  */
01850   if (cache->base == 0)
01851     return;
01852 
01853   /* See the end of i386_push_dummy_call.  */
01854   (*this_id) = frame_id_build (cache->base + 8, cache->pc);
01855 }
01856 
01857 static enum unwind_stop_reason
01858 i386_frame_unwind_stop_reason (struct frame_info *this_frame,
01859                                void **this_cache)
01860 {
01861   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
01862 
01863   if (!cache->base_p)
01864     return UNWIND_UNAVAILABLE;
01865 
01866   /* This marks the outermost frame.  */
01867   if (cache->base == 0)
01868     return UNWIND_OUTERMOST;
01869 
01870   return UNWIND_NO_REASON;
01871 }
01872 
01873 static struct value *
01874 i386_frame_prev_register (struct frame_info *this_frame, void **this_cache,
01875                           int regnum)
01876 {
01877   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
01878 
01879   gdb_assert (regnum >= 0);
01880 
01881   /* The System V ABI says that:
01882 
01883      "The flags register contains the system flags, such as the
01884      direction flag and the carry flag.  The direction flag must be
01885      set to the forward (that is, zero) direction before entry and
01886      upon exit from a function.  Other user flags have no specified
01887      role in the standard calling sequence and are not preserved."
01888 
01889      To guarantee the "upon exit" part of that statement we fake a
01890      saved flags register that has its direction flag cleared.
01891 
01892      Note that GCC doesn't seem to rely on the fact that the direction
01893      flag is cleared after a function return; it always explicitly
01894      clears the flag before operations where it matters.
01895 
01896      FIXME: kettenis/20030316: I'm not quite sure whether this is the
01897      right thing to do.  The way we fake the flags register here makes
01898      it impossible to change it.  */
01899 
01900   if (regnum == I386_EFLAGS_REGNUM)
01901     {
01902       ULONGEST val;
01903 
01904       val = get_frame_register_unsigned (this_frame, regnum);
01905       val &= ~(1 << 10);
01906       return frame_unwind_got_constant (this_frame, regnum, val);
01907     }
01908 
01909   if (regnum == I386_EIP_REGNUM && cache->pc_in_eax)
01910     return frame_unwind_got_register (this_frame, regnum, I386_EAX_REGNUM);
01911 
01912   if (regnum == I386_ESP_REGNUM
01913       && (cache->saved_sp != 0 || cache->saved_sp_reg != -1))
01914     {
01915       /* If the SP has been saved, but we don't know where, then this
01916          means that SAVED_SP_REG register was found unavailable back
01917          when we built the cache.  */
01918       if (cache->saved_sp == 0)
01919         return frame_unwind_got_register (this_frame, regnum,
01920                                           cache->saved_sp_reg);
01921       else
01922         return frame_unwind_got_constant (this_frame, regnum,
01923                                           cache->saved_sp);
01924     }
01925 
01926   if (regnum < I386_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
01927     return frame_unwind_got_memory (this_frame, regnum,
01928                                     cache->saved_regs[regnum]);
01929 
01930   return frame_unwind_got_register (this_frame, regnum, regnum);
01931 }
01932 
01933 static const struct frame_unwind i386_frame_unwind =
01934 {
01935   NORMAL_FRAME,
01936   i386_frame_unwind_stop_reason,
01937   i386_frame_this_id,
01938   i386_frame_prev_register,
01939   NULL,
01940   default_frame_sniffer
01941 };
01942 
01943 /* Normal frames, but in a function epilogue.  */
01944 
01945 /* The epilogue is defined here as the 'ret' instruction, which will
01946    follow any instruction such as 'leave' or 'pop %ebp' that destroys
01947    the function's stack frame.  */
01948 
01949 static int
01950 i386_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
01951 {
01952   gdb_byte insn;
01953   struct symtab *symtab;
01954 
01955   symtab = find_pc_symtab (pc);
01956   if (symtab && symtab->epilogue_unwind_valid)
01957     return 0;
01958 
01959   if (target_read_memory (pc, &insn, 1))
01960     return 0;   /* Can't read memory at pc.  */
01961 
01962   if (insn != 0xc3)     /* 'ret' instruction.  */
01963     return 0;
01964 
01965   return 1;
01966 }
01967 
01968 static int
01969 i386_epilogue_frame_sniffer (const struct frame_unwind *self,
01970                              struct frame_info *this_frame,
01971                              void **this_prologue_cache)
01972 {
01973   if (frame_relative_level (this_frame) == 0)
01974     return i386_in_function_epilogue_p (get_frame_arch (this_frame),
01975                                         get_frame_pc (this_frame));
01976   else
01977     return 0;
01978 }
01979 
01980 static struct i386_frame_cache *
01981 i386_epilogue_frame_cache (struct frame_info *this_frame, void **this_cache)
01982 {
01983   volatile struct gdb_exception ex;
01984   struct i386_frame_cache *cache;
01985   CORE_ADDR sp;
01986 
01987   if (*this_cache)
01988     return *this_cache;
01989 
01990   cache = i386_alloc_frame_cache ();
01991   *this_cache = cache;
01992 
01993   TRY_CATCH (ex, RETURN_MASK_ERROR)
01994     {
01995       cache->pc = get_frame_func (this_frame);
01996 
01997       /* At this point the stack looks as if we just entered the
01998          function, with the return address at the top of the
01999          stack.  */
02000       sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);
02001       cache->base = sp + cache->sp_offset;
02002       cache->saved_sp = cache->base + 8;
02003       cache->saved_regs[I386_EIP_REGNUM] = cache->base + 4;
02004 
02005       cache->base_p = 1;
02006     }
02007   if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
02008     throw_exception (ex);
02009 
02010   return cache;
02011 }
02012 
02013 static enum unwind_stop_reason
02014 i386_epilogue_frame_unwind_stop_reason (struct frame_info *this_frame,
02015                                         void **this_cache)
02016 {
02017   struct i386_frame_cache *cache =
02018     i386_epilogue_frame_cache (this_frame, this_cache);
02019 
02020   if (!cache->base_p)
02021     return UNWIND_UNAVAILABLE;
02022 
02023   return UNWIND_NO_REASON;
02024 }
02025 
02026 static void
02027 i386_epilogue_frame_this_id (struct frame_info *this_frame,
02028                              void **this_cache,
02029                              struct frame_id *this_id)
02030 {
02031   struct i386_frame_cache *cache =
02032     i386_epilogue_frame_cache (this_frame, this_cache);
02033 
02034   if (!cache->base_p)
02035     return;
02036 
02037   (*this_id) = frame_id_build (cache->base + 8, cache->pc);
02038 }
02039 
02040 static struct value *
02041 i386_epilogue_frame_prev_register (struct frame_info *this_frame,
02042                                    void **this_cache, int regnum)
02043 {
02044   /* Make sure we've initialized the cache.  */
02045   i386_epilogue_frame_cache (this_frame, this_cache);
02046 
02047   return i386_frame_prev_register (this_frame, this_cache, regnum);
02048 }
02049 
02050 static const struct frame_unwind i386_epilogue_frame_unwind =
02051 {
02052   NORMAL_FRAME,
02053   i386_epilogue_frame_unwind_stop_reason,
02054   i386_epilogue_frame_this_id,
02055   i386_epilogue_frame_prev_register,
02056   NULL, 
02057   i386_epilogue_frame_sniffer
02058 };
02059 
02060 
02061 /* Stack-based trampolines.  */
02062 
02063 /* These trampolines are used on cross x86 targets, when taking the
02064    address of a nested function.  When executing these trampolines,
02065    no stack frame is set up, so we are in a similar situation as in
02066    epilogues and i386_epilogue_frame_this_id can be re-used.  */
02067 
02068 /* Static chain passed in register.  */
02069 
02070 struct i386_insn i386_tramp_chain_in_reg_insns[] =
02071 {
02072   /* `movl imm32, %eax' and `movl imm32, %ecx' */
02073   { 5, { 0xb8 }, { 0xfe } },
02074 
02075   /* `jmp imm32' */
02076   { 5, { 0xe9 }, { 0xff } },
02077 
02078   {0}
02079 };
02080 
02081 /* Static chain passed on stack (when regparm=3).  */
02082 
02083 struct i386_insn i386_tramp_chain_on_stack_insns[] =
02084 {
02085   /* `push imm32' */
02086   { 5, { 0x68 }, { 0xff } },
02087 
02088   /* `jmp imm32' */
02089   { 5, { 0xe9 }, { 0xff } },
02090 
02091   {0}
02092 };
02093 
02094 /* Return whether PC points inside a stack trampoline.   */
02095 
02096 static int
02097 i386_in_stack_tramp_p (CORE_ADDR pc)
02098 {
02099   gdb_byte insn;
02100   const char *name;
02101 
02102   /* A stack trampoline is detected if no name is associated
02103     to the current pc and if it points inside a trampoline
02104     sequence.  */
02105 
02106   find_pc_partial_function (pc, &name, NULL, NULL);
02107   if (name)
02108     return 0;
02109 
02110   if (target_read_memory (pc, &insn, 1))
02111     return 0;
02112 
02113   if (!i386_match_insn_block (pc, i386_tramp_chain_in_reg_insns)
02114       && !i386_match_insn_block (pc, i386_tramp_chain_on_stack_insns))
02115     return 0;
02116 
02117   return 1;
02118 }
02119 
02120 static int
02121 i386_stack_tramp_frame_sniffer (const struct frame_unwind *self,
02122                                 struct frame_info *this_frame,
02123                                 void **this_cache)
02124 {
02125   if (frame_relative_level (this_frame) == 0)
02126     return i386_in_stack_tramp_p (get_frame_pc (this_frame));
02127   else
02128     return 0;
02129 }
02130 
02131 static const struct frame_unwind i386_stack_tramp_frame_unwind =
02132 {
02133   NORMAL_FRAME,
02134   i386_epilogue_frame_unwind_stop_reason,
02135   i386_epilogue_frame_this_id,
02136   i386_epilogue_frame_prev_register,
02137   NULL, 
02138   i386_stack_tramp_frame_sniffer
02139 };
02140 
02141 /* Generate a bytecode expression to get the value of the saved PC.  */
02142 
02143 static void
02144 i386_gen_return_address (struct gdbarch *gdbarch,
02145                          struct agent_expr *ax, struct axs_value *value,
02146                          CORE_ADDR scope)
02147 {
02148   /* The following sequence assumes the traditional use of the base
02149      register.  */
02150   ax_reg (ax, I386_EBP_REGNUM);
02151   ax_const_l (ax, 4);
02152   ax_simple (ax, aop_add);
02153   value->type = register_type (gdbarch, I386_EIP_REGNUM);
02154   value->kind = axs_lvalue_memory;
02155 }
02156 
02157 
02158 /* Signal trampolines.  */
02159 
02160 static struct i386_frame_cache *
02161 i386_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
02162 {
02163   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02164   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02165   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02166   volatile struct gdb_exception ex;
02167   struct i386_frame_cache *cache;
02168   CORE_ADDR addr;
02169   gdb_byte buf[4];
02170 
02171   if (*this_cache)
02172     return *this_cache;
02173 
02174   cache = i386_alloc_frame_cache ();
02175 
02176   TRY_CATCH (ex, RETURN_MASK_ERROR)
02177     {
02178       get_frame_register (this_frame, I386_ESP_REGNUM, buf);
02179       cache->base = extract_unsigned_integer (buf, 4, byte_order) - 4;
02180 
02181       addr = tdep->sigcontext_addr (this_frame);
02182       if (tdep->sc_reg_offset)
02183         {
02184           int i;
02185 
02186           gdb_assert (tdep->sc_num_regs <= I386_NUM_SAVED_REGS);
02187 
02188           for (i = 0; i < tdep->sc_num_regs; i++)
02189             if (tdep->sc_reg_offset[i] != -1)
02190               cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
02191         }
02192       else
02193         {
02194           cache->saved_regs[I386_EIP_REGNUM] = addr + tdep->sc_pc_offset;
02195           cache->saved_regs[I386_ESP_REGNUM] = addr + tdep->sc_sp_offset;
02196         }
02197 
02198       cache->base_p = 1;
02199     }
02200   if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
02201     throw_exception (ex);
02202 
02203   *this_cache = cache;
02204   return cache;
02205 }
02206 
02207 static enum unwind_stop_reason
02208 i386_sigtramp_frame_unwind_stop_reason (struct frame_info *this_frame,
02209                                         void **this_cache)
02210 {
02211   struct i386_frame_cache *cache =
02212     i386_sigtramp_frame_cache (this_frame, this_cache);
02213 
02214   if (!cache->base_p)
02215     return UNWIND_UNAVAILABLE;
02216 
02217   return UNWIND_NO_REASON;
02218 }
02219 
02220 static void
02221 i386_sigtramp_frame_this_id (struct frame_info *this_frame, void **this_cache,
02222                              struct frame_id *this_id)
02223 {
02224   struct i386_frame_cache *cache =
02225     i386_sigtramp_frame_cache (this_frame, this_cache);
02226 
02227   if (!cache->base_p)
02228     return;
02229 
02230   /* See the end of i386_push_dummy_call.  */
02231   (*this_id) = frame_id_build (cache->base + 8, get_frame_pc (this_frame));
02232 }
02233 
02234 static struct value *
02235 i386_sigtramp_frame_prev_register (struct frame_info *this_frame,
02236                                    void **this_cache, int regnum)
02237 {
02238   /* Make sure we've initialized the cache.  */
02239   i386_sigtramp_frame_cache (this_frame, this_cache);
02240 
02241   return i386_frame_prev_register (this_frame, this_cache, regnum);
02242 }
02243 
02244 static int
02245 i386_sigtramp_frame_sniffer (const struct frame_unwind *self,
02246                              struct frame_info *this_frame,
02247                              void **this_prologue_cache)
02248 {
02249   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
02250 
02251   /* We shouldn't even bother if we don't have a sigcontext_addr
02252      handler.  */
02253   if (tdep->sigcontext_addr == NULL)
02254     return 0;
02255 
02256   if (tdep->sigtramp_p != NULL)
02257     {
02258       if (tdep->sigtramp_p (this_frame))
02259         return 1;
02260     }
02261 
02262   if (tdep->sigtramp_start != 0)
02263     {
02264       CORE_ADDR pc = get_frame_pc (this_frame);
02265 
02266       gdb_assert (tdep->sigtramp_end != 0);
02267       if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
02268         return 1;
02269     }
02270 
02271   return 0;
02272 }
02273 
02274 static const struct frame_unwind i386_sigtramp_frame_unwind =
02275 {
02276   SIGTRAMP_FRAME,
02277   i386_sigtramp_frame_unwind_stop_reason,
02278   i386_sigtramp_frame_this_id,
02279   i386_sigtramp_frame_prev_register,
02280   NULL,
02281   i386_sigtramp_frame_sniffer
02282 };
02283 
02284 
02285 static CORE_ADDR
02286 i386_frame_base_address (struct frame_info *this_frame, void **this_cache)
02287 {
02288   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
02289 
02290   return cache->base;
02291 }
02292 
02293 static const struct frame_base i386_frame_base =
02294 {
02295   &i386_frame_unwind,
02296   i386_frame_base_address,
02297   i386_frame_base_address,
02298   i386_frame_base_address
02299 };
02300 
02301 static struct frame_id
02302 i386_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
02303 {
02304   CORE_ADDR fp;
02305 
02306   fp = get_frame_register_unsigned (this_frame, I386_EBP_REGNUM);
02307 
02308   /* See the end of i386_push_dummy_call.  */
02309   return frame_id_build (fp + 8, get_frame_pc (this_frame));
02310 }
02311 
02312 /* _Decimal128 function return values need 16-byte alignment on the
02313    stack.  */
02314 
02315 static CORE_ADDR
02316 i386_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
02317 {
02318   return sp & -(CORE_ADDR)16;
02319 }
02320 
02321 
02322 /* Figure out where the longjmp will land.  Slurp the args out of the
02323    stack.  We expect the first arg to be a pointer to the jmp_buf
02324    structure from which we extract the address that we will land at.
02325    This address is copied into PC.  This routine returns non-zero on
02326    success.  */
02327 
02328 static int
02329 i386_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
02330 {
02331   gdb_byte buf[4];
02332   CORE_ADDR sp, jb_addr;
02333   struct gdbarch *gdbarch = get_frame_arch (frame);
02334   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02335   int jb_pc_offset = gdbarch_tdep (gdbarch)->jb_pc_offset;
02336 
02337   /* If JB_PC_OFFSET is -1, we have no way to find out where the
02338      longjmp will land.  */
02339   if (jb_pc_offset == -1)
02340     return 0;
02341 
02342   get_frame_register (frame, I386_ESP_REGNUM, buf);
02343   sp = extract_unsigned_integer (buf, 4, byte_order);
02344   if (target_read_memory (sp + 4, buf, 4))
02345     return 0;
02346 
02347   jb_addr = extract_unsigned_integer (buf, 4, byte_order);
02348   if (target_read_memory (jb_addr + jb_pc_offset, buf, 4))
02349     return 0;
02350 
02351   *pc = extract_unsigned_integer (buf, 4, byte_order);
02352   return 1;
02353 }
02354 
02355 
02356 /* Check whether TYPE must be 16-byte-aligned when passed as a
02357    function argument.  16-byte vectors, _Decimal128 and structures or
02358    unions containing such types must be 16-byte-aligned; other
02359    arguments are 4-byte-aligned.  */
02360 
02361 static int
02362 i386_16_byte_align_p (struct type *type)
02363 {
02364   type = check_typedef (type);
02365   if ((TYPE_CODE (type) == TYPE_CODE_DECFLOAT
02366        || (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)))
02367       && TYPE_LENGTH (type) == 16)
02368     return 1;
02369   if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
02370     return i386_16_byte_align_p (TYPE_TARGET_TYPE (type));
02371   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
02372       || TYPE_CODE (type) == TYPE_CODE_UNION)
02373     {
02374       int i;
02375       for (i = 0; i < TYPE_NFIELDS (type); i++)
02376         {
02377           if (i386_16_byte_align_p (TYPE_FIELD_TYPE (type, i)))
02378             return 1;
02379         }
02380     }
02381   return 0;
02382 }
02383 
02384 /* Implementation for set_gdbarch_push_dummy_code.  */
02385 
02386 static CORE_ADDR
02387 i386_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp, CORE_ADDR funaddr,
02388                       struct value **args, int nargs, struct type *value_type,
02389                       CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
02390                       struct regcache *regcache)
02391 {
02392   /* Use 0xcc breakpoint - 1 byte.  */
02393   *bp_addr = sp - 1;
02394   *real_pc = funaddr;
02395 
02396   /* Keep the stack aligned.  */
02397   return sp - 16;
02398 }
02399 
02400 static CORE_ADDR
02401 i386_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
02402                       struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
02403                       struct value **args, CORE_ADDR sp, int struct_return,
02404                       CORE_ADDR struct_addr)
02405 {
02406   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02407   gdb_byte buf[4];
02408   int i;
02409   int write_pass;
02410   int args_space = 0;
02411 
02412   /* Determine the total space required for arguments and struct
02413      return address in a first pass (allowing for 16-byte-aligned
02414      arguments), then push arguments in a second pass.  */
02415 
02416   for (write_pass = 0; write_pass < 2; write_pass++)
02417     {
02418       int args_space_used = 0;
02419 
02420       if (struct_return)
02421         {
02422           if (write_pass)
02423             {
02424               /* Push value address.  */
02425               store_unsigned_integer (buf, 4, byte_order, struct_addr);
02426               write_memory (sp, buf, 4);
02427               args_space_used += 4;
02428             }
02429           else
02430             args_space += 4;
02431         }
02432 
02433       for (i = 0; i < nargs; i++)
02434         {
02435           int len = TYPE_LENGTH (value_enclosing_type (args[i]));
02436 
02437           if (write_pass)
02438             {
02439               if (i386_16_byte_align_p (value_enclosing_type (args[i])))
02440                 args_space_used = align_up (args_space_used, 16);
02441 
02442               write_memory (sp + args_space_used,
02443                             value_contents_all (args[i]), len);
02444               /* The System V ABI says that:
02445 
02446               "An argument's size is increased, if necessary, to make it a
02447               multiple of [32-bit] words.  This may require tail padding,
02448               depending on the size of the argument."
02449 
02450               This makes sure the stack stays word-aligned.  */
02451               args_space_used += align_up (len, 4);
02452             }
02453           else
02454             {
02455               if (i386_16_byte_align_p (value_enclosing_type (args[i])))
02456                 args_space = align_up (args_space, 16);
02457               args_space += align_up (len, 4);
02458             }
02459         }
02460 
02461       if (!write_pass)
02462         {
02463           sp -= args_space;
02464 
02465           /* The original System V ABI only requires word alignment,
02466              but modern incarnations need 16-byte alignment in order
02467              to support SSE.  Since wasting a few bytes here isn't
02468              harmful we unconditionally enforce 16-byte alignment.  */
02469           sp &= ~0xf;
02470         }
02471     }
02472 
02473   /* Store return address.  */
02474   sp -= 4;
02475   store_unsigned_integer (buf, 4, byte_order, bp_addr);
02476   write_memory (sp, buf, 4);
02477 
02478   /* Finally, update the stack pointer...  */
02479   store_unsigned_integer (buf, 4, byte_order, sp);
02480   regcache_cooked_write (regcache, I386_ESP_REGNUM, buf);
02481 
02482   /* ...and fake a frame pointer.  */
02483   regcache_cooked_write (regcache, I386_EBP_REGNUM, buf);
02484 
02485   /* MarkK wrote: This "+ 8" is all over the place:
02486      (i386_frame_this_id, i386_sigtramp_frame_this_id,
02487      i386_dummy_id).  It's there, since all frame unwinders for
02488      a given target have to agree (within a certain margin) on the
02489      definition of the stack address of a frame.  Otherwise frame id
02490      comparison might not work correctly.  Since DWARF2/GCC uses the
02491      stack address *before* the function call as a frame's CFA.  On
02492      the i386, when %ebp is used as a frame pointer, the offset
02493      between the contents %ebp and the CFA as defined by GCC.  */
02494   return sp + 8;
02495 }
02496 
02497 /* These registers are used for returning integers (and on some
02498    targets also for returning `struct' and `union' values when their
02499    size and alignment match an integer type).  */
02500 #define LOW_RETURN_REGNUM       I386_EAX_REGNUM /* %eax */
02501 #define HIGH_RETURN_REGNUM      I386_EDX_REGNUM /* %edx */
02502 
02503 /* Read, for architecture GDBARCH, a function return value of TYPE
02504    from REGCACHE, and copy that into VALBUF.  */
02505 
02506 static void
02507 i386_extract_return_value (struct gdbarch *gdbarch, struct type *type,
02508                            struct regcache *regcache, gdb_byte *valbuf)
02509 {
02510   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02511   int len = TYPE_LENGTH (type);
02512   gdb_byte buf[I386_MAX_REGISTER_SIZE];
02513 
02514   if (TYPE_CODE (type) == TYPE_CODE_FLT)
02515     {
02516       if (tdep->st0_regnum < 0)
02517         {
02518           warning (_("Cannot find floating-point return value."));
02519           memset (valbuf, 0, len);
02520           return;
02521         }
02522 
02523       /* Floating-point return values can be found in %st(0).  Convert
02524          its contents to the desired type.  This is probably not
02525          exactly how it would happen on the target itself, but it is
02526          the best we can do.  */
02527       regcache_raw_read (regcache, I386_ST0_REGNUM, buf);
02528       convert_typed_floating (buf, i387_ext_type (gdbarch), valbuf, type);
02529     }
02530   else
02531     {
02532       int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
02533       int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
02534 
02535       if (len <= low_size)
02536         {
02537           regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
02538           memcpy (valbuf, buf, len);
02539         }
02540       else if (len <= (low_size + high_size))
02541         {
02542           regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
02543           memcpy (valbuf, buf, low_size);
02544           regcache_raw_read (regcache, HIGH_RETURN_REGNUM, buf);
02545           memcpy (valbuf + low_size, buf, len - low_size);
02546         }
02547       else
02548         internal_error (__FILE__, __LINE__,
02549                         _("Cannot extract return value of %d bytes long."),
02550                         len);
02551     }
02552 }
02553 
02554 /* Write, for architecture GDBARCH, a function return value of TYPE
02555    from VALBUF into REGCACHE.  */
02556 
02557 static void
02558 i386_store_return_value (struct gdbarch *gdbarch, struct type *type,
02559                          struct regcache *regcache, const gdb_byte *valbuf)
02560 {
02561   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02562   int len = TYPE_LENGTH (type);
02563 
02564   if (TYPE_CODE (type) == TYPE_CODE_FLT)
02565     {
02566       ULONGEST fstat;
02567       gdb_byte buf[I386_MAX_REGISTER_SIZE];
02568 
02569       if (tdep->st0_regnum < 0)
02570         {
02571           warning (_("Cannot set floating-point return value."));
02572           return;
02573         }
02574 
02575       /* Returning floating-point values is a bit tricky.  Apart from
02576          storing the return value in %st(0), we have to simulate the
02577          state of the FPU at function return point.  */
02578 
02579       /* Convert the value found in VALBUF to the extended
02580          floating-point format used by the FPU.  This is probably
02581          not exactly how it would happen on the target itself, but
02582          it is the best we can do.  */
02583       convert_typed_floating (valbuf, type, buf, i387_ext_type (gdbarch));
02584       regcache_raw_write (regcache, I386_ST0_REGNUM, buf);
02585 
02586       /* Set the top of the floating-point register stack to 7.  The
02587          actual value doesn't really matter, but 7 is what a normal
02588          function return would end up with if the program started out
02589          with a freshly initialized FPU.  */
02590       regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
02591       fstat |= (7 << 11);
02592       regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM (tdep), fstat);
02593 
02594       /* Mark %st(1) through %st(7) as empty.  Since we set the top of
02595          the floating-point register stack to 7, the appropriate value
02596          for the tag word is 0x3fff.  */
02597       regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM (tdep), 0x3fff);
02598     }
02599   else
02600     {
02601       int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
02602       int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
02603 
02604       if (len <= low_size)
02605         regcache_raw_write_part (regcache, LOW_RETURN_REGNUM, 0, len, valbuf);
02606       else if (len <= (low_size + high_size))
02607         {
02608           regcache_raw_write (regcache, LOW_RETURN_REGNUM, valbuf);
02609           regcache_raw_write_part (regcache, HIGH_RETURN_REGNUM, 0,
02610                                    len - low_size, valbuf + low_size);
02611         }
02612       else
02613         internal_error (__FILE__, __LINE__,
02614                         _("Cannot store return value of %d bytes long."), len);
02615     }
02616 }
02617 
02618 
02619 /* This is the variable that is set with "set struct-convention", and
02620    its legitimate values.  */
02621 static const char default_struct_convention[] = "default";
02622 static const char pcc_struct_convention[] = "pcc";
02623 static const char reg_struct_convention[] = "reg";
02624 static const char *const valid_conventions[] =
02625 {
02626   default_struct_convention,
02627   pcc_struct_convention,
02628   reg_struct_convention,
02629   NULL
02630 };
02631 static const char *struct_convention = default_struct_convention;
02632 
02633 /* Return non-zero if TYPE, which is assumed to be a structure,
02634    a union type, or an array type, should be returned in registers
02635    for architecture GDBARCH.  */
02636 
02637 static int
02638 i386_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
02639 {
02640   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02641   enum type_code code = TYPE_CODE (type);
02642   int len = TYPE_LENGTH (type);
02643 
02644   gdb_assert (code == TYPE_CODE_STRUCT
02645               || code == TYPE_CODE_UNION
02646               || code == TYPE_CODE_ARRAY);
02647 
02648   if (struct_convention == pcc_struct_convention
02649       || (struct_convention == default_struct_convention
02650           && tdep->struct_return == pcc_struct_return))
02651     return 0;
02652 
02653   /* Structures consisting of a single `float', `double' or 'long
02654      double' member are returned in %st(0).  */
02655   if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
02656     {
02657       type = check_typedef (TYPE_FIELD_TYPE (type, 0));
02658       if (TYPE_CODE (type) == TYPE_CODE_FLT)
02659         return (len == 4 || len == 8 || len == 12);
02660     }
02661 
02662   return (len == 1 || len == 2 || len == 4 || len == 8);
02663 }
02664 
02665 /* Determine, for architecture GDBARCH, how a return value of TYPE
02666    should be returned.  If it is supposed to be returned in registers,
02667    and READBUF is non-zero, read the appropriate value from REGCACHE,
02668    and copy it into READBUF.  If WRITEBUF is non-zero, write the value
02669    from WRITEBUF into REGCACHE.  */
02670 
02671 static enum return_value_convention
02672 i386_return_value (struct gdbarch *gdbarch, struct value *function,
02673                    struct type *type, struct regcache *regcache,
02674                    gdb_byte *readbuf, const gdb_byte *writebuf)
02675 {
02676   enum type_code code = TYPE_CODE (type);
02677 
02678   if (((code == TYPE_CODE_STRUCT
02679         || code == TYPE_CODE_UNION
02680         || code == TYPE_CODE_ARRAY)
02681        && !i386_reg_struct_return_p (gdbarch, type))
02682       /* Complex double and long double uses the struct return covention.  */
02683       || (code == TYPE_CODE_COMPLEX && TYPE_LENGTH (type) == 16)
02684       || (code == TYPE_CODE_COMPLEX && TYPE_LENGTH (type) == 24)
02685       /* 128-bit decimal float uses the struct return convention.  */
02686       || (code == TYPE_CODE_DECFLOAT && TYPE_LENGTH (type) == 16))
02687     {
02688       /* The System V ABI says that:
02689 
02690          "A function that returns a structure or union also sets %eax
02691          to the value of the original address of the caller's area
02692          before it returns.  Thus when the caller receives control
02693          again, the address of the returned object resides in register
02694          %eax and can be used to access the object."
02695 
02696          So the ABI guarantees that we can always find the return
02697          value just after the function has returned.  */
02698 
02699       /* Note that the ABI doesn't mention functions returning arrays,
02700          which is something possible in certain languages such as Ada.
02701          In this case, the value is returned as if it was wrapped in
02702          a record, so the convention applied to records also applies
02703          to arrays.  */
02704 
02705       if (readbuf)
02706         {
02707           ULONGEST addr;
02708 
02709           regcache_raw_read_unsigned (regcache, I386_EAX_REGNUM, &addr);
02710           read_memory (addr, readbuf, TYPE_LENGTH (type));
02711         }
02712 
02713       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
02714     }
02715 
02716   /* This special case is for structures consisting of a single
02717      `float', `double' or 'long double' member.  These structures are
02718      returned in %st(0).  For these structures, we call ourselves
02719      recursively, changing TYPE into the type of the first member of
02720      the structure.  Since that should work for all structures that
02721      have only one member, we don't bother to check the member's type
02722      here.  */
02723   if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
02724     {
02725       type = check_typedef (TYPE_FIELD_TYPE (type, 0));
02726       return i386_return_value (gdbarch, function, type, regcache,
02727                                 readbuf, writebuf);
02728     }
02729 
02730   if (readbuf)
02731     i386_extract_return_value (gdbarch, type, regcache, readbuf);
02732   if (writebuf)
02733     i386_store_return_value (gdbarch, type, regcache, writebuf);
02734 
02735   return RETURN_VALUE_REGISTER_CONVENTION;
02736 }
02737 
02738 
02739 struct type *
02740 i387_ext_type (struct gdbarch *gdbarch)
02741 {
02742   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02743 
02744   if (!tdep->i387_ext_type)
02745     {
02746       tdep->i387_ext_type = tdesc_find_type (gdbarch, "i387_ext");
02747       gdb_assert (tdep->i387_ext_type != NULL);
02748     }
02749 
02750   return tdep->i387_ext_type;
02751 }
02752 
02753 /* Construct vector type for pseudo YMM registers.  We can't use
02754    tdesc_find_type since YMM isn't described in target description.  */
02755 
02756 static struct type *
02757 i386_ymm_type (struct gdbarch *gdbarch)
02758 {
02759   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02760 
02761   if (!tdep->i386_ymm_type)
02762     {
02763       const struct builtin_type *bt = builtin_type (gdbarch);
02764 
02765       /* The type we're building is this: */
02766 #if 0
02767       union __gdb_builtin_type_vec256i
02768       {
02769         int128_t uint128[2];
02770         int64_t v2_int64[4];
02771         int32_t v4_int32[8];
02772         int16_t v8_int16[16];
02773         int8_t v16_int8[32];
02774         double v2_double[4];
02775         float v4_float[8];
02776       };
02777 #endif
02778 
02779       struct type *t;
02780 
02781       t = arch_composite_type (gdbarch,
02782                                "__gdb_builtin_type_vec256i", TYPE_CODE_UNION);
02783       append_composite_type_field (t, "v8_float",
02784                                    init_vector_type (bt->builtin_float, 8));
02785       append_composite_type_field (t, "v4_double",
02786                                    init_vector_type (bt->builtin_double, 4));
02787       append_composite_type_field (t, "v32_int8",
02788                                    init_vector_type (bt->builtin_int8, 32));
02789       append_composite_type_field (t, "v16_int16",
02790                                    init_vector_type (bt->builtin_int16, 16));
02791       append_composite_type_field (t, "v8_int32",
02792                                    init_vector_type (bt->builtin_int32, 8));
02793       append_composite_type_field (t, "v4_int64",
02794                                    init_vector_type (bt->builtin_int64, 4));
02795       append_composite_type_field (t, "v2_int128",
02796                                    init_vector_type (bt->builtin_int128, 2));
02797 
02798       TYPE_VECTOR (t) = 1;
02799       TYPE_NAME (t) = "builtin_type_vec256i";
02800       tdep->i386_ymm_type = t;
02801     }
02802 
02803   return tdep->i386_ymm_type;
02804 }
02805 
02806 /* Construct vector type for MMX registers.  */
02807 static struct type *
02808 i386_mmx_type (struct gdbarch *gdbarch)
02809 {
02810   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02811 
02812   if (!tdep->i386_mmx_type)
02813     {
02814       const struct builtin_type *bt = builtin_type (gdbarch);
02815 
02816       /* The type we're building is this: */
02817 #if 0
02818       union __gdb_builtin_type_vec64i
02819       {
02820         int64_t uint64;
02821         int32_t v2_int32[2];
02822         int16_t v4_int16[4];
02823         int8_t v8_int8[8];
02824       };
02825 #endif
02826 
02827       struct type *t;
02828 
02829       t = arch_composite_type (gdbarch,
02830                                "__gdb_builtin_type_vec64i", TYPE_CODE_UNION);
02831 
02832       append_composite_type_field (t, "uint64", bt->builtin_int64);
02833       append_composite_type_field (t, "v2_int32",
02834                                    init_vector_type (bt->builtin_int32, 2));
02835       append_composite_type_field (t, "v4_int16",
02836                                    init_vector_type (bt->builtin_int16, 4));
02837       append_composite_type_field (t, "v8_int8",
02838                                    init_vector_type (bt->builtin_int8, 8));
02839 
02840       TYPE_VECTOR (t) = 1;
02841       TYPE_NAME (t) = "builtin_type_vec64i";
02842       tdep->i386_mmx_type = t;
02843     }
02844 
02845   return tdep->i386_mmx_type;
02846 }
02847 
02848 /* Return the GDB type object for the "standard" data type of data in
02849    register REGNUM.  */
02850 
02851 struct type *
02852 i386_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
02853 {
02854   if (i386_mmx_regnum_p (gdbarch, regnum))
02855     return i386_mmx_type (gdbarch);
02856   else if (i386_ymm_regnum_p (gdbarch, regnum))
02857     return i386_ymm_type (gdbarch);
02858   else
02859     {
02860       const struct builtin_type *bt = builtin_type (gdbarch);
02861       if (i386_byte_regnum_p (gdbarch, regnum))
02862         return bt->builtin_int8;
02863       else if (i386_word_regnum_p (gdbarch, regnum))
02864         return bt->builtin_int16;
02865       else if (i386_dword_regnum_p (gdbarch, regnum))
02866         return bt->builtin_int32;
02867     }
02868 
02869   internal_error (__FILE__, __LINE__, _("invalid regnum"));
02870 }
02871 
02872 /* Map a cooked register onto a raw register or memory.  For the i386,
02873    the MMX registers need to be mapped onto floating point registers.  */
02874 
02875 static int
02876 i386_mmx_regnum_to_fp_regnum (struct regcache *regcache, int regnum)
02877 {
02878   struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
02879   int mmxreg, fpreg;
02880   ULONGEST fstat;
02881   int tos;
02882 
02883   mmxreg = regnum - tdep->mm0_regnum;
02884   regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
02885   tos = (fstat >> 11) & 0x7;
02886   fpreg = (mmxreg + tos) % 8;
02887 
02888   return (I387_ST0_REGNUM (tdep) + fpreg);
02889 }
02890 
02891 /* A helper function for us by i386_pseudo_register_read_value and
02892    amd64_pseudo_register_read_value.  It does all the work but reads
02893    the data into an already-allocated value.  */
02894 
02895 void
02896 i386_pseudo_register_read_into_value (struct gdbarch *gdbarch,
02897                                       struct regcache *regcache,
02898                                       int regnum,
02899                                       struct value *result_value)
02900 {
02901   gdb_byte raw_buf[MAX_REGISTER_SIZE];
02902   enum register_status status;
02903   gdb_byte *buf = value_contents_raw (result_value);
02904 
02905   if (i386_mmx_regnum_p (gdbarch, regnum))
02906     {
02907       int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
02908 
02909       /* Extract (always little endian).  */
02910       status = regcache_raw_read (regcache, fpnum, raw_buf);
02911       if (status != REG_VALID)
02912         mark_value_bytes_unavailable (result_value, 0,
02913                                       TYPE_LENGTH (value_type (result_value)));
02914       else
02915         memcpy (buf, raw_buf, register_size (gdbarch, regnum));
02916     }
02917   else
02918     {
02919       struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02920 
02921       if (i386_ymm_regnum_p (gdbarch, regnum))
02922         {
02923           regnum -= tdep->ymm0_regnum;
02924 
02925           /* Extract (always little endian).  Read lower 128bits.  */
02926           status = regcache_raw_read (regcache,
02927                                       I387_XMM0_REGNUM (tdep) + regnum,
02928                                       raw_buf);
02929           if (status != REG_VALID)
02930             mark_value_bytes_unavailable (result_value, 0, 16);
02931           else
02932             memcpy (buf, raw_buf, 16);
02933           /* Read upper 128bits.  */
02934           status = regcache_raw_read (regcache,
02935                                       tdep->ymm0h_regnum + regnum,
02936                                       raw_buf);
02937           if (status != REG_VALID)
02938             mark_value_bytes_unavailable (result_value, 16, 32);
02939           else
02940             memcpy (buf + 16, raw_buf, 16);
02941         }
02942       else if (i386_word_regnum_p (gdbarch, regnum))
02943         {
02944           int gpnum = regnum - tdep->ax_regnum;
02945 
02946           /* Extract (always little endian).  */
02947           status = regcache_raw_read (regcache, gpnum, raw_buf);
02948           if (status != REG_VALID)
02949             mark_value_bytes_unavailable (result_value, 0,
02950                                           TYPE_LENGTH (value_type (result_value)));
02951           else
02952             memcpy (buf, raw_buf, 2);
02953         }
02954       else if (i386_byte_regnum_p (gdbarch, regnum))
02955         {
02956           /* Check byte pseudo registers last since this function will
02957              be called from amd64_pseudo_register_read, which handles
02958              byte pseudo registers differently.  */
02959           int gpnum = regnum - tdep->al_regnum;
02960 
02961           /* Extract (always little endian).  We read both lower and
02962              upper registers.  */
02963           status = regcache_raw_read (regcache, gpnum % 4, raw_buf);
02964           if (status != REG_VALID)
02965             mark_value_bytes_unavailable (result_value, 0,
02966                                           TYPE_LENGTH (value_type (result_value)));
02967           else if (gpnum >= 4)
02968             memcpy (buf, raw_buf + 1, 1);
02969           else
02970             memcpy (buf, raw_buf, 1);
02971         }
02972       else
02973         internal_error (__FILE__, __LINE__, _("invalid regnum"));
02974     }
02975 }
02976 
02977 static struct value *
02978 i386_pseudo_register_read_value (struct gdbarch *gdbarch,
02979                                  struct regcache *regcache,
02980                                  int regnum)
02981 {
02982   struct value *result;
02983 
02984   result = allocate_value (register_type (gdbarch, regnum));
02985   VALUE_LVAL (result) = lval_register;
02986   VALUE_REGNUM (result) = regnum;
02987 
02988   i386_pseudo_register_read_into_value (gdbarch, regcache, regnum, result);
02989 
02990   return result;
02991 }
02992 
02993 void
02994 i386_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
02995                             int regnum, const gdb_byte *buf)
02996 {
02997   gdb_byte raw_buf[MAX_REGISTER_SIZE];
02998 
02999   if (i386_mmx_regnum_p (gdbarch, regnum))
03000     {
03001       int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
03002 
03003       /* Read ...  */
03004       regcache_raw_read (regcache, fpnum, raw_buf);
03005       /* ... Modify ... (always little endian).  */
03006       memcpy (raw_buf, buf, register_size (gdbarch, regnum));
03007       /* ... Write.  */
03008       regcache_raw_write (regcache, fpnum, raw_buf);
03009     }
03010   else
03011     {
03012       struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
03013 
03014       if (i386_ymm_regnum_p (gdbarch, regnum))
03015         {
03016           regnum -= tdep->ymm0_regnum;
03017 
03018           /* ... Write lower 128bits.  */
03019           regcache_raw_write (regcache,
03020                              I387_XMM0_REGNUM (tdep) + regnum,
03021                              buf);
03022           /* ... Write upper 128bits.  */
03023           regcache_raw_write (regcache,
03024                              tdep->ymm0h_regnum + regnum,
03025                              buf + 16);
03026         }
03027       else if (i386_word_regnum_p (gdbarch, regnum))
03028         {
03029           int gpnum = regnum - tdep->ax_regnum;
03030 
03031           /* Read ...  */
03032           regcache_raw_read (regcache, gpnum, raw_buf);
03033           /* ... Modify ... (always little endian).  */
03034           memcpy (raw_buf, buf, 2);
03035           /* ... Write.  */
03036           regcache_raw_write (regcache, gpnum, raw_buf);
03037         }
03038       else if (i386_byte_regnum_p (gdbarch, regnum))
03039         {
03040           /* Check byte pseudo registers last since this function will
03041              be called from amd64_pseudo_register_read, which handles
03042              byte pseudo registers differently.  */
03043           int gpnum = regnum - tdep->al_regnum;
03044 
03045           /* Read ...  We read both lower and upper registers.  */
03046           regcache_raw_read (regcache, gpnum % 4, raw_buf);
03047           /* ... Modify ... (always little endian).  */
03048           if (gpnum >= 4)
03049             memcpy (raw_buf + 1, buf, 1);
03050           else
03051             memcpy (raw_buf, buf, 1);
03052           /* ... Write.  */
03053           regcache_raw_write (regcache, gpnum % 4, raw_buf);
03054         }
03055       else
03056         internal_error (__FILE__, __LINE__, _("invalid regnum"));
03057     }
03058 }
03059 
03060 
03061 /* Return the register number of the register allocated by GCC after
03062    REGNUM, or -1 if there is no such register.  */
03063 
03064 static int
03065 i386_next_regnum (int regnum)
03066 {
03067   /* GCC allocates the registers in the order:
03068 
03069      %eax, %edx, %ecx, %ebx, %esi, %edi, %ebp, %esp, ...
03070 
03071      Since storing a variable in %esp doesn't make any sense we return
03072      -1 for %ebp and for %esp itself.  */
03073   static int next_regnum[] =
03074   {
03075     I386_EDX_REGNUM,            /* Slot for %eax.  */
03076     I386_EBX_REGNUM,            /* Slot for %ecx.  */
03077     I386_ECX_REGNUM,            /* Slot for %edx.  */
03078     I386_ESI_REGNUM,            /* Slot for %ebx.  */
03079     -1, -1,                     /* Slots for %esp and %ebp.  */
03080     I386_EDI_REGNUM,            /* Slot for %esi.  */
03081     I386_EBP_REGNUM             /* Slot for %edi.  */
03082   };
03083 
03084   if (regnum >= 0 && regnum < sizeof (next_regnum) / sizeof (next_regnum[0]))
03085     return next_regnum[regnum];
03086 
03087   return -1;
03088 }
03089 
03090 /* Return nonzero if a value of type TYPE stored in register REGNUM
03091    needs any special handling.  */
03092 
03093 static int
03094 i386_convert_register_p (struct gdbarch *gdbarch,
03095                          int regnum, struct type *type)
03096 {
03097   int len = TYPE_LENGTH (type);
03098 
03099   /* Values may be spread across multiple registers.  Most debugging
03100      formats aren't expressive enough to specify the locations, so
03101      some heuristics is involved.  Right now we only handle types that
03102      have a length that is a multiple of the word size, since GCC
03103      doesn't seem to put any other types into registers.  */
03104   if (len > 4 && len % 4 == 0)
03105     {
03106       int last_regnum = regnum;
03107 
03108       while (len > 4)
03109         {
03110           last_regnum = i386_next_regnum (last_regnum);
03111           len -= 4;
03112         }
03113 
03114       if (last_regnum != -1)
03115         return 1;
03116     }
03117 
03118   return i387_convert_register_p (gdbarch, regnum, type);
03119 }
03120 
03121 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
03122    return its contents in TO.  */
03123 
03124 static int
03125 i386_register_to_value (struct frame_info *frame, int regnum,
03126                         struct type *type, gdb_byte *to,
03127                         int *optimizedp, int *unavailablep)
03128 {
03129   struct gdbarch *gdbarch = get_frame_arch (frame);
03130   int len = TYPE_LENGTH (type);
03131 
03132   if (i386_fp_regnum_p (gdbarch, regnum))
03133     return i387_register_to_value (frame, regnum, type, to,
03134                                    optimizedp, unavailablep);
03135 
03136   /* Read a value spread across multiple registers.  */
03137 
03138   gdb_assert (len > 4 && len % 4 == 0);
03139 
03140   while (len > 0)
03141     {
03142       gdb_assert (regnum != -1);
03143       gdb_assert (register_size (gdbarch, regnum) == 4);
03144 
03145       if (!get_frame_register_bytes (frame, regnum, 0,
03146                                      register_size (gdbarch, regnum),
03147                                      to, optimizedp, unavailablep))
03148         return 0;
03149 
03150       regnum = i386_next_regnum (regnum);
03151       len -= 4;
03152       to += 4;
03153     }
03154 
03155   *optimizedp = *unavailablep = 0;
03156   return 1;
03157 }
03158 
03159 /* Write the contents FROM of a value of type TYPE into register
03160    REGNUM in frame FRAME.  */
03161 
03162 static void
03163 i386_value_to_register (struct frame_info *frame, int regnum,
03164                         struct type *type, const gdb_byte *from)
03165 {
03166   int len = TYPE_LENGTH (type);
03167 
03168   if (i386_fp_regnum_p (get_frame_arch (frame), regnum))
03169     {
03170       i387_value_to_register (frame, regnum, type, from);
03171       return;
03172     }
03173 
03174   /* Write a value spread across multiple registers.  */
03175 
03176   gdb_assert (len > 4 && len % 4 == 0);
03177 
03178   while (len > 0)
03179     {
03180       gdb_assert (regnum != -1);
03181       gdb_assert (register_size (get_frame_arch (frame), regnum) == 4);
03182 
03183       put_frame_register (frame, regnum, from);
03184       regnum = i386_next_regnum (regnum);
03185       len -= 4;
03186       from += 4;
03187     }
03188 }
03189 
03190 /* Supply register REGNUM from the buffer specified by GREGS and LEN
03191    in the general-purpose register set REGSET to register cache
03192    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
03193 
03194 void
03195 i386_supply_gregset (const struct regset *regset, struct regcache *regcache,
03196                      int regnum, const void *gregs, size_t len)
03197 {
03198   const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
03199   const gdb_byte *regs = gregs;
03200   int i;
03201 
03202   gdb_assert (len == tdep->sizeof_gregset);
03203 
03204   for (i = 0; i < tdep->gregset_num_regs; i++)
03205     {
03206       if ((regnum == i || regnum == -1)
03207           && tdep->gregset_reg_offset[i] != -1)
03208         regcache_raw_supply (regcache, i, regs + tdep->gregset_reg_offset[i]);
03209     }
03210 }
03211 
03212 /* Collect register REGNUM from the register cache REGCACHE and store
03213    it in the buffer specified by GREGS and LEN as described by the
03214    general-purpose register set REGSET.  If REGNUM is -1, do this for
03215    all registers in REGSET.  */
03216 
03217 void
03218 i386_collect_gregset (const struct regset *regset,
03219                       const struct regcache *regcache,
03220                       int regnum, void *gregs, size_t len)
03221 {
03222   const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
03223   gdb_byte *regs = gregs;
03224   int i;
03225 
03226   gdb_assert (len == tdep->sizeof_gregset);
03227 
03228   for (i = 0; i < tdep->gregset_num_regs; i++)
03229     {
03230       if ((regnum == i || regnum == -1)
03231           && tdep->gregset_reg_offset[i] != -1)
03232         regcache_raw_collect (regcache, i, regs + tdep->gregset_reg_offset[i]);
03233     }
03234 }
03235 
03236 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
03237    in the floating-point register set REGSET to register cache
03238    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
03239 
03240 static void
03241 i386_supply_fpregset (const struct regset *regset, struct regcache *regcache,
03242                       int regnum, const void *fpregs, size_t len)
03243 {
03244   const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
03245 
03246   if (len == I387_SIZEOF_FXSAVE)
03247     {
03248       i387_supply_fxsave (regcache, regnum, fpregs);
03249       return;
03250     }
03251 
03252   gdb_assert (len == tdep->sizeof_fpregset);
03253   i387_supply_fsave (regcache, regnum, fpregs);
03254 }
03255 
03256 /* Collect register REGNUM from the register cache REGCACHE and store
03257    it in the buffer specified by FPREGS and LEN as described by the
03258    floating-point register set REGSET.  If REGNUM is -1, do this for
03259    all registers in REGSET.  */
03260 
03261 static void
03262 i386_collect_fpregset (const struct regset *regset,
03263                        const struct regcache *regcache,
03264                        int regnum, void *fpregs, size_t len)
03265 {
03266   const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
03267 
03268   if (len == I387_SIZEOF_FXSAVE)
03269     {
03270       i387_collect_fxsave (regcache, regnum, fpregs);
03271       return;
03272     }
03273 
03274   gdb_assert (len == tdep->sizeof_fpregset);
03275   i387_collect_fsave (regcache, regnum, fpregs);
03276 }
03277 
03278 /* Similar to i386_supply_fpregset, but use XSAVE extended state.  */
03279 
03280 static void
03281 i386_supply_xstateregset (const struct regset *regset,
03282                           struct regcache *regcache, int regnum,
03283                           const void *xstateregs, size_t len)
03284 {
03285   i387_supply_xsave (regcache, regnum, xstateregs);
03286 }
03287 
03288 /* Similar to i386_collect_fpregset , but use XSAVE extended state.  */
03289 
03290 static void
03291 i386_collect_xstateregset (const struct regset *regset,
03292                            const struct regcache *regcache,
03293                            int regnum, void *xstateregs, size_t len)
03294 {
03295   i387_collect_xsave (regcache, regnum, xstateregs, 1);
03296 }
03297 
03298 /* Return the appropriate register set for the core section identified
03299    by SECT_NAME and SECT_SIZE.  */
03300 
03301 const struct regset *
03302 i386_regset_from_core_section (struct gdbarch *gdbarch,
03303                                const char *sect_name, size_t sect_size)
03304 {
03305   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
03306 
03307   if (strcmp (sect_name, ".reg") == 0 && sect_size == tdep->sizeof_gregset)
03308     {
03309       if (tdep->gregset == NULL)
03310         tdep->gregset = regset_alloc (gdbarch, i386_supply_gregset,
03311                                       i386_collect_gregset);
03312       return tdep->gregset;
03313     }
03314 
03315   if ((strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
03316       || (strcmp (sect_name, ".reg-xfp") == 0
03317           && sect_size == I387_SIZEOF_FXSAVE))
03318     {
03319       if (tdep->fpregset == NULL)
03320         tdep->fpregset = regset_alloc (gdbarch, i386_supply_fpregset,
03321                                        i386_collect_fpregset);
03322       return tdep->fpregset;
03323     }
03324 
03325   if (strcmp (sect_name, ".reg-xstate") == 0)
03326     {
03327       if (tdep->xstateregset == NULL)
03328         tdep->xstateregset = regset_alloc (gdbarch,
03329                                            i386_supply_xstateregset,
03330                                            i386_collect_xstateregset);
03331 
03332       return tdep->xstateregset;
03333     }
03334 
03335   return NULL;
03336 }
03337 
03338 
03339 /* Stuff for WIN32 PE style DLL's but is pretty generic really.  */
03340 
03341 CORE_ADDR
03342 i386_pe_skip_trampoline_code (struct frame_info *frame,
03343                               CORE_ADDR pc, char *name)
03344 {
03345   struct gdbarch *gdbarch = get_frame_arch (frame);
03346   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
03347 
03348   /* jmp *(dest) */
03349   if (pc && read_memory_unsigned_integer (pc, 2, byte_order) == 0x25ff)
03350     {
03351       unsigned long indirect =
03352         read_memory_unsigned_integer (pc + 2, 4, byte_order);
03353       struct minimal_symbol *indsym =
03354         indirect ? lookup_minimal_symbol_by_pc (indirect).minsym : 0;
03355       const char *symname = indsym ? SYMBOL_LINKAGE_NAME (indsym) : 0;
03356 
03357       if (symname)
03358         {
03359           if (strncmp (symname, "__imp_", 6) == 0
03360               || strncmp (symname, "_imp_", 5) == 0)
03361             return name ? 1 :
03362                    read_memory_unsigned_integer (indirect, 4, byte_order);
03363         }
03364     }
03365   return 0;                     /* Not a trampoline.  */
03366 }
03367 
03368 
03369 /* Return whether the THIS_FRAME corresponds to a sigtramp
03370    routine.  */
03371 
03372 int
03373 i386_sigtramp_p (struct frame_info *this_frame)
03374 {
03375   CORE_ADDR pc = get_frame_pc (this_frame);
03376   const char *name;
03377 
03378   find_pc_partial_function (pc, &name, NULL, NULL);
03379   return (name && strcmp ("_sigtramp", name) == 0);
03380 }
03381 
03382 
03383 /* We have two flavours of disassembly.  The machinery on this page
03384    deals with switching between those.  */
03385 
03386 static int
03387 i386_print_insn (bfd_vma pc, struct disassemble_info *info)
03388 {
03389   gdb_assert (disassembly_flavor == att_flavor
03390               || disassembly_flavor == intel_flavor);
03391 
03392   /* FIXME: kettenis/20020915: Until disassembler_options is properly
03393      constified, cast to prevent a compiler warning.  */
03394   info->disassembler_options = (char *) disassembly_flavor;
03395 
03396   return print_insn_i386 (pc, info);
03397 }
03398 
03399 
03400 /* There are a few i386 architecture variants that differ only
03401    slightly from the generic i386 target.  For now, we don't give them
03402    their own source file, but include them here.  As a consequence,
03403    they'll always be included.  */
03404 
03405 /* System V Release 4 (SVR4).  */
03406 
03407 /* Return whether THIS_FRAME corresponds to a SVR4 sigtramp
03408    routine.  */
03409 
03410 static int
03411 i386_svr4_sigtramp_p (struct frame_info *this_frame)
03412 {
03413   CORE_ADDR pc = get_frame_pc (this_frame);
03414   const char *name;
03415 
03416   /* The origin of these symbols is currently unknown.  */
03417   find_pc_partial_function (pc, &name, NULL, NULL);
03418   return (name && (strcmp ("_sigreturn", name) == 0
03419                    || strcmp ("sigvechandler", name) == 0));
03420 }
03421 
03422 /* Assuming THIS_FRAME is for a SVR4 sigtramp routine, return the
03423    address of the associated sigcontext (ucontext) structure.  */
03424 
03425 static CORE_ADDR
03426 i386_svr4_sigcontext_addr (struct frame_info *this_frame)
03427 {
03428   struct gdbarch *gdbarch = get_frame_arch (this_frame);
03429   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
03430   gdb_byte buf[4];
03431   CORE_ADDR sp;
03432 
03433   get_frame_register (this_frame, I386_ESP_REGNUM, buf);
03434   sp = extract_unsigned_integer (buf, 4, byte_order);
03435 
03436   return read_memory_unsigned_integer (sp + 8, 4, byte_order);
03437 }
03438 
03439 
03440 
03441 /* Implementation of `gdbarch_stap_is_single_operand', as defined in
03442    gdbarch.h.  */
03443 
03444 int
03445 i386_stap_is_single_operand (struct gdbarch *gdbarch, const char *s)
03446 {
03447   return (*s == '$' /* Literal number.  */
03448           || (isdigit (*s) && s[1] == '(' && s[2] == '%') /* Displacement.  */
03449           || (*s == '(' && s[1] == '%') /* Register indirection.  */
03450           || (*s == '%' && isalpha (s[1]))); /* Register access.  */
03451 }
03452 
03453 /* Implementation of `gdbarch_stap_parse_special_token', as defined in
03454    gdbarch.h.  */
03455 
03456 int
03457 i386_stap_parse_special_token (struct gdbarch *gdbarch,
03458                                struct stap_parse_info *p)
03459 {
03460   /* In order to parse special tokens, we use a state-machine that go
03461      through every known token and try to get a match.  */
03462   enum
03463     {
03464       TRIPLET,
03465       THREE_ARG_DISPLACEMENT,
03466       DONE
03467     } current_state;
03468 
03469   current_state = TRIPLET;
03470 
03471   /* The special tokens to be parsed here are:
03472 
03473      - `register base + (register index * size) + offset', as represented
03474      in `(%rcx,%rax,8)', or `[OFFSET](BASE_REG,INDEX_REG[,SIZE])'.
03475 
03476      - Operands of the form `-8+3+1(%rbp)', which must be interpreted as
03477      `*(-8 + 3 - 1 + (void *) $eax)'.  */
03478 
03479   while (current_state != DONE)
03480     {
03481       const char *s = p->arg;
03482 
03483       switch (current_state)
03484         {
03485         case TRIPLET:
03486             {
03487               if (isdigit (*s) || *s == '-' || *s == '+')
03488                 {
03489                   int got_minus[3];
03490                   int i;
03491                   long displacements[3];
03492                   const char *start;
03493                   char *regname;
03494                   int len;
03495                   struct stoken str;
03496                   char *endp;
03497 
03498                   got_minus[0] = 0;
03499                   if (*s == '+')
03500                     ++s;
03501                   else if (*s == '-')
03502                     {
03503                       ++s;
03504                       got_minus[0] = 1;
03505                     }
03506 
03507                   displacements[0] = strtol (s, &endp, 10);
03508                   s = endp;
03509 
03510                   if (*s != '+' && *s != '-')
03511                     {
03512                       /* We are not dealing with a triplet.  */
03513                       break;
03514                     }
03515 
03516                   got_minus[1] = 0;
03517                   if (*s == '+')
03518                     ++s;
03519                   else
03520                     {
03521                       ++s;
03522                       got_minus[1] = 1;
03523                     }
03524 
03525                   displacements[1] = strtol (s, &endp, 10);
03526                   s = endp;
03527 
03528                   if (*s != '+' && *s != '-')
03529                     {
03530                       /* We are not dealing with a triplet.  */
03531                       break;
03532                     }
03533 
03534                   got_minus[2] = 0;
03535                   if (*s == '+')
03536                     ++s;
03537                   else
03538                     {
03539                       ++s;
03540                       got_minus[2] = 1;
03541                     }
03542 
03543                   displacements[2] = strtol (s, &endp, 10);
03544                   s = endp;
03545 
03546                   if (*s != '(' || s[1] != '%')
03547                     break;
03548 
03549                   s += 2;
03550                   start = s;
03551 
03552                   while (isalnum (*s))
03553                     ++s;
03554 
03555                   if (*s++ != ')')
03556                     break;
03557 
03558                   len = s - start;
03559                   regname = alloca (len + 1);
03560 
03561                   strncpy (regname, start, len);
03562                   regname[len] = '\0';
03563 
03564                   if (user_reg_map_name_to_regnum (gdbarch,
03565                                                    regname, len) == -1)
03566                     error (_("Invalid register name `%s' "
03567                              "on expression `%s'."),
03568                            regname, p->saved_arg);
03569 
03570                   for (i = 0; i < 3; i++)
03571                     {
03572                       write_exp_elt_opcode (OP_LONG);
03573                       write_exp_elt_type
03574                         (builtin_type (gdbarch)->builtin_long);
03575                       write_exp_elt_longcst (displacements[i]);
03576                       write_exp_elt_opcode (OP_LONG);
03577                       if (got_minus[i])
03578                         write_exp_elt_opcode (UNOP_NEG);
03579                     }
03580 
03581                   write_exp_elt_opcode (OP_REGISTER);
03582                   str.ptr = regname;
03583                   str.length = len;
03584                   write_exp_string (str);
03585                   write_exp_elt_opcode (OP_REGISTER);
03586 
03587                   write_exp_elt_opcode (UNOP_CAST);
03588                   write_exp_elt_type (builtin_type (gdbarch)->builtin_data_ptr);
03589                   write_exp_elt_opcode (UNOP_CAST);
03590 
03591                   write_exp_elt_opcode (BINOP_ADD);
03592                   write_exp_elt_opcode (BINOP_ADD);
03593                   write_exp_elt_opcode (BINOP_ADD);
03594 
03595                   write_exp_elt_opcode (UNOP_CAST);
03596                   write_exp_elt_type (lookup_pointer_type (p->arg_type));
03597                   write_exp_elt_opcode (UNOP_CAST);
03598 
03599                   write_exp_elt_opcode (UNOP_IND);
03600 
03601                   p->arg = s;
03602 
03603                   return 1;
03604                 }
03605               break;
03606             }
03607         case THREE_ARG_DISPLACEMENT:
03608             {
03609               if (isdigit (*s) || *s == '(' || *s == '-' || *s == '+')
03610                 {
03611                   int offset_minus = 0;
03612                   long offset = 0;
03613                   int size_minus = 0;
03614                   long size = 0;
03615                   const char *start;
03616                   char *base;
03617                   int len_base;
03618                   char *index;
03619                   int len_index;
03620                   struct stoken base_token, index_token;
03621 
03622                   if (*s == '+')
03623                     ++s;
03624                   else if (*s == '-')
03625                     {
03626                       ++s;
03627                       offset_minus = 1;
03628                     }
03629 
03630                   if (offset_minus && !isdigit (*s))
03631                     break;
03632 
03633                   if (isdigit (*s))
03634                     {
03635                       char *endp;
03636 
03637                       offset = strtol (s, &endp, 10);
03638                       s = endp;
03639                     }
03640 
03641                   if (*s != '(' || s[1] != '%')
03642                     break;
03643 
03644                   s += 2;
03645                   start = s;
03646 
03647                   while (isalnum (*s))
03648                     ++s;
03649 
03650                   if (*s != ',' || s[1] != '%')
03651                     break;
03652 
03653                   len_base = s - start;
03654                   base = alloca (len_base + 1);
03655                   strncpy (base, start, len_base);
03656                   base[len_base] = '\0';
03657 
03658                   if (user_reg_map_name_to_regnum (gdbarch,
03659                                                    base, len_base) == -1)
03660                     error (_("Invalid register name `%s' "
03661                              "on expression `%s'."),
03662                            base, p->saved_arg);
03663 
03664                   s += 2;
03665                   start = s;
03666 
03667                   while (isalnum (*s))
03668                     ++s;
03669 
03670                   len_index = s - start;
03671                   index = alloca (len_index + 1);
03672                   strncpy (index, start, len_index);
03673                   index[len_index] = '\0';
03674 
03675                   if (user_reg_map_name_to_regnum (gdbarch,
03676                                                    index, len_index) == -1)
03677                     error (_("Invalid register name `%s' "
03678                              "on expression `%s'."),
03679                            index, p->saved_arg);
03680 
03681                   if (*s != ',' && *s != ')')
03682                     break;
03683 
03684                   if (*s == ',')
03685                     {
03686                       char *endp;
03687 
03688                       ++s;
03689                       if (*s == '+')
03690                         ++s;
03691                       else if (*s == '-')
03692                         {
03693                           ++s;
03694                           size_minus = 1;
03695                         }
03696 
03697                       size = strtol (s, &endp, 10);
03698                       s = endp;
03699 
03700                       if (*s != ')')
03701                         break;
03702                     }
03703 
03704                   ++s;
03705 
03706                   if (offset)
03707                     {
03708                       write_exp_elt_opcode (OP_LONG);
03709                       write_exp_elt_type
03710                         (builtin_type (gdbarch)->builtin_long);
03711                       write_exp_elt_longcst (offset);
03712                       write_exp_elt_opcode (OP_LONG);
03713                       if (offset_minus)
03714                         write_exp_elt_opcode (UNOP_NEG);
03715                     }
03716 
03717                   write_exp_elt_opcode (OP_REGISTER);
03718                   base_token.ptr = base;
03719                   base_token.length = len_base;
03720                   write_exp_string (base_token);
03721                   write_exp_elt_opcode (OP_REGISTER);
03722 
03723                   if (offset)
03724                     write_exp_elt_opcode (BINOP_ADD);
03725 
03726                   write_exp_elt_opcode (OP_REGISTER);
03727                   index_token.ptr = index;
03728                   index_token.length = len_index;
03729                   write_exp_string (index_token);
03730                   write_exp_elt_opcode (OP_REGISTER);
03731 
03732                   if (size)
03733                     {
03734                       write_exp_elt_opcode (OP_LONG);
03735                       write_exp_elt_type
03736                         (builtin_type (gdbarch)->builtin_long);
03737                       write_exp_elt_longcst (size);
03738                       write_exp_elt_opcode (OP_LONG);
03739                       if (size_minus)
03740                         write_exp_elt_opcode (UNOP_NEG);
03741                       write_exp_elt_opcode (BINOP_MUL);
03742                     }
03743 
03744                   write_exp_elt_opcode (BINOP_ADD);
03745 
03746                   write_exp_elt_opcode (UNOP_CAST);
03747                   write_exp_elt_type (lookup_pointer_type (p->arg_type));
03748                   write_exp_elt_opcode (UNOP_CAST);
03749 
03750                   write_exp_elt_opcode (UNOP_IND);
03751 
03752                   p->arg = s;
03753 
03754                   return 1;
03755                 }
03756               break;
03757             }
03758         }
03759 
03760       /* Advancing to the next state.  */
03761       ++current_state;
03762     }
03763 
03764   return 0;
03765 }
03766 
03767 
03768 
03769 /* Generic ELF.  */
03770 
03771 void
03772 i386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
03773 {
03774   /* We typically use stabs-in-ELF with the SVR4 register numbering.  */
03775   set_gdbarch_stab_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
03776 
03777   /* Registering SystemTap handlers.  */
03778   set_gdbarch_stap_integer_prefix (gdbarch, "$");
03779   set_gdbarch_stap_register_prefix (gdbarch, "%");
03780   set_gdbarch_stap_register_indirection_prefix (gdbarch, "(");
03781   set_gdbarch_stap_register_indirection_suffix (gdbarch, ")");
03782   set_gdbarch_stap_is_single_operand (gdbarch,
03783                                       i386_stap_is_single_operand);
03784   set_gdbarch_stap_parse_special_token (gdbarch,
03785                                         i386_stap_parse_special_token);
03786 }
03787 
03788 /* System V Release 4 (SVR4).  */
03789 
03790 void
03791 i386_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
03792 {
03793   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
03794 
03795   /* System V Release 4 uses ELF.  */
03796   i386_elf_init_abi (info, gdbarch);
03797 
03798   /* System V Release 4 has shared libraries.  */
03799   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
03800 
03801   tdep->sigtramp_p = i386_svr4_sigtramp_p;
03802   tdep->sigcontext_addr = i386_svr4_sigcontext_addr;
03803   tdep->sc_pc_offset = 36 + 14 * 4;
03804   tdep->sc_sp_offset = 36 + 17 * 4;
03805 
03806   tdep->jb_pc_offset = 20;
03807 }
03808 
03809 /* DJGPP.  */
03810 
03811 static void
03812 i386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
03813 {
03814   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
03815 
03816   /* DJGPP doesn't have any special frames for signal handlers.  */
03817   tdep->sigtramp_p = NULL;
03818 
03819   tdep->jb_pc_offset = 36;
03820 
03821   /* DJGPP does not support the SSE registers.  */
03822   if (! tdesc_has_registers (info.target_desc))
03823     tdep->tdesc = tdesc_i386_mmx;
03824 
03825   /* Native compiler is GCC, which uses the SVR4 register numbering
03826      even in COFF and STABS.  See the comment in i386_gdbarch_init,
03827      before the calls to set_gdbarch_stab_reg_to_regnum and
03828      set_gdbarch_sdb_reg_to_regnum.  */
03829   set_gdbarch_stab_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
03830   set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
03831 
03832   set_gdbarch_has_dos_based_file_system (gdbarch, 1);
03833 }
03834 
03835 
03836 /* i386 register groups.  In addition to the normal groups, add "mmx"
03837    and "sse".  */
03838 
03839 static struct reggroup *i386_sse_reggroup;
03840 static struct reggroup *i386_mmx_reggroup;
03841 
03842 static void
03843 i386_init_reggroups (void)
03844 {
03845   i386_sse_reggroup = reggroup_new ("sse", USER_REGGROUP);
03846   i386_mmx_reggroup = reggroup_new ("mmx", USER_REGGROUP);
03847 }
03848 
03849 static void
03850 i386_add_reggroups (struct gdbarch *gdbarch)
03851 {
03852   reggroup_add (gdbarch, i386_sse_reggroup);
03853   reggroup_add (gdbarch, i386_mmx_reggroup);
03854   reggroup_add (gdbarch, general_reggroup);
03855   reggroup_add (gdbarch, float_reggroup);
03856   reggroup_add (gdbarch, all_reggroup);
03857   reggroup_add (gdbarch, save_reggroup);
03858   reggroup_add (gdbarch, restore_reggroup);
03859   reggroup_add (gdbarch, vector_reggroup);
03860   reggroup_add (gdbarch, system_reggroup);
03861 }
03862 
03863 int
03864 i386_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
03865                           struct reggroup *group)
03866 {
03867   const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
03868   int fp_regnum_p, mmx_regnum_p, xmm_regnum_p, mxcsr_regnum_p,
03869       ymm_regnum_p, ymmh_regnum_p;
03870 
03871   /* Don't include pseudo registers, except for MMX, in any register
03872      groups.  */
03873   if (i386_byte_regnum_p (gdbarch, regnum))
03874     return 0;
03875 
03876   if (i386_word_regnum_p (gdbarch, regnum))
03877     return 0;
03878 
03879   if (i386_dword_regnum_p (gdbarch, regnum))
03880     return 0;
03881 
03882   mmx_regnum_p = i386_mmx_regnum_p (gdbarch, regnum);
03883   if (group == i386_mmx_reggroup)
03884     return mmx_regnum_p;
03885 
03886   xmm_regnum_p = i386_xmm_regnum_p (gdbarch, regnum);
03887   mxcsr_regnum_p = i386_mxcsr_regnum_p (gdbarch, regnum);
03888   if (group == i386_sse_reggroup)
03889     return xmm_regnum_p || mxcsr_regnum_p;
03890 
03891   ymm_regnum_p = i386_ymm_regnum_p (gdbarch, regnum);
03892   if (group == vector_reggroup)
03893     return (mmx_regnum_p
03894             || ymm_regnum_p
03895             || mxcsr_regnum_p
03896             || (xmm_regnum_p
03897                 && ((tdep->xcr0 & I386_XSTATE_AVX_MASK)
03898                     == I386_XSTATE_SSE_MASK)));
03899 
03900   fp_regnum_p = (i386_fp_regnum_p (gdbarch, regnum)
03901                  || i386_fpc_regnum_p (gdbarch, regnum));
03902   if (group == float_reggroup)
03903     return fp_regnum_p;
03904 
03905   /* For "info reg all", don't include upper YMM registers nor XMM
03906      registers when AVX is supported.  */
03907   ymmh_regnum_p = i386_ymmh_regnum_p (gdbarch, regnum);
03908   if (group == all_reggroup
03909       && ((xmm_regnum_p
03910            && (tdep->xcr0 & I386_XSTATE_AVX))
03911           || ymmh_regnum_p))
03912     return 0;
03913 
03914   if (group == general_reggroup)
03915     return (!fp_regnum_p
03916             && !mmx_regnum_p
03917             && !mxcsr_regnum_p
03918             && !xmm_regnum_p
03919             && !ymm_regnum_p
03920             && !ymmh_regnum_p);
03921 
03922   return default_register_reggroup_p (gdbarch, regnum, group);
03923 }
03924 
03925 
03926 /* Get the ARGIth function argument for the current function.  */
03927 
03928 static CORE_ADDR
03929 i386_fetch_pointer_argument (struct frame_info *frame, int argi, 
03930                              struct type *type)
03931 {
03932   struct gdbarch *gdbarch = get_frame_arch (frame);
03933   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
03934   CORE_ADDR sp = get_frame_register_unsigned (frame, I386_ESP_REGNUM);
03935   return read_memory_unsigned_integer (sp + (4 * (argi + 1)), 4, byte_order);
03936 }
03937 
03938 static void
03939 i386_skip_permanent_breakpoint (struct regcache *regcache)
03940 {
03941   CORE_ADDR current_pc = regcache_read_pc (regcache);
03942 
03943  /* On i386, breakpoint is exactly 1 byte long, so we just
03944     adjust the PC in the regcache.  */
03945   current_pc += 1;
03946   regcache_write_pc (regcache, current_pc);
03947 }
03948 
03949 
03950 #define PREFIX_REPZ     0x01
03951 #define PREFIX_REPNZ    0x02
03952 #define PREFIX_LOCK     0x04
03953 #define PREFIX_DATA     0x08
03954 #define PREFIX_ADDR     0x10
03955 
03956 /* operand size */
03957 enum
03958 {
03959   OT_BYTE = 0,
03960   OT_WORD,
03961   OT_LONG,
03962   OT_QUAD,
03963   OT_DQUAD,
03964 };
03965 
03966 /* i386 arith/logic operations */
03967 enum
03968 {
03969   OP_ADDL,
03970   OP_ORL,
03971   OP_ADCL,
03972   OP_SBBL,
03973   OP_ANDL,
03974   OP_SUBL,
03975   OP_XORL,
03976   OP_CMPL,
03977 };
03978 
03979 struct i386_record_s
03980 {
03981   struct gdbarch *gdbarch;
03982   struct regcache *regcache;
03983   CORE_ADDR orig_addr;
03984   CORE_ADDR addr;
03985   int aflag;
03986   int dflag;
03987   int override;
03988   uint8_t modrm;
03989   uint8_t mod, reg, rm;
03990   int ot;
03991   uint8_t rex_x;
03992   uint8_t rex_b;
03993   int rip_offset;
03994   int popl_esp_hack;
03995   const int *regmap;
03996 };
03997 
03998 /* Parse the "modrm" part of the memory address irp->addr points at.
03999    Returns -1 if something goes wrong, 0 otherwise.  */
04000 
04001 static int
04002 i386_record_modrm (struct i386_record_s *irp)
04003 {
04004   struct gdbarch *gdbarch = irp->gdbarch;
04005 
04006   if (record_read_memory (gdbarch, irp->addr, &irp->modrm, 1))
04007     return -1;
04008 
04009   irp->addr++;
04010   irp->mod = (irp->modrm >> 6) & 3;
04011   irp->reg = (irp->modrm >> 3) & 7;
04012   irp->rm = irp->modrm & 7;
04013 
04014   return 0;
04015 }
04016 
04017 /* Extract the memory address that the current instruction writes to,
04018    and return it in *ADDR.  Return -1 if something goes wrong.  */
04019 
04020 static int
04021 i386_record_lea_modrm_addr (struct i386_record_s *irp, uint64_t *addr)
04022 {
04023   struct gdbarch *gdbarch = irp->gdbarch;
04024   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
04025   gdb_byte buf[4];
04026   ULONGEST offset64;
04027 
04028   *addr = 0;
04029   if (irp->aflag)
04030     {
04031       /* 32 bits */
04032       int havesib = 0;
04033       uint8_t scale = 0;
04034       uint8_t byte;
04035       uint8_t index = 0;
04036       uint8_t base = irp->rm;
04037 
04038       if (base == 4)
04039         {
04040           havesib = 1;
04041           if (record_read_memory (gdbarch, irp->addr, &byte, 1))
04042             return -1;
04043           irp->addr++;
04044           scale = (byte >> 6) & 3;
04045           index = ((byte >> 3) & 7) | irp->rex_x;
04046           base = (byte & 7);
04047         }
04048       base |= irp->rex_b;
04049 
04050       switch (irp->mod)
04051         {
04052         case 0:
04053           if ((base & 7) == 5)
04054             {
04055               base = 0xff;
04056               if (record_read_memory (gdbarch, irp->addr, buf, 4))
04057                 return -1;
04058               irp->addr += 4;
04059               *addr = extract_signed_integer (buf, 4, byte_order);
04060               if (irp->regmap[X86_RECORD_R8_REGNUM] && !havesib)
04061                 *addr += irp->addr + irp->rip_offset;
04062             }
04063           break;
04064         case 1:
04065           if (record_read_memory (gdbarch, irp->addr, buf, 1))
04066             return -1;
04067           irp->addr++;
04068           *addr = (int8_t) buf[0];
04069           break;
04070         case 2:
04071           if (record_read_memory (gdbarch, irp->addr, buf, 4))
04072             return -1;
04073           *addr = extract_signed_integer (buf, 4, byte_order);
04074           irp->addr += 4;
04075           break;
04076         }
04077 
04078       offset64 = 0;
04079       if (base != 0xff)
04080         {
04081           if (base == 4 && irp->popl_esp_hack)
04082             *addr += irp->popl_esp_hack;
04083           regcache_raw_read_unsigned (irp->regcache, irp->regmap[base],
04084                                       &offset64);
04085         }
04086       if (irp->aflag == 2)
04087         {
04088           *addr += offset64;
04089         }
04090       else
04091         *addr = (uint32_t) (offset64 + *addr);
04092 
04093       if (havesib && (index != 4 || scale != 0))
04094         {
04095           regcache_raw_read_unsigned (irp->regcache, irp->regmap[index],
04096                                       &offset64);
04097           if (irp->aflag == 2)
04098             *addr += offset64 << scale;
04099           else
04100             *addr = (uint32_t) (*addr + (offset64 << scale));
04101         }
04102     }
04103   else
04104     {
04105       /* 16 bits */
04106       switch (irp->mod)
04107         {
04108         case 0:
04109           if (irp->rm == 6)
04110             {
04111               if (record_read_memory (gdbarch, irp->addr, buf, 2))
04112                 return -1;
04113               irp->addr += 2;
04114               *addr = extract_signed_integer (buf, 2, byte_order);
04115               irp->rm = 0;
04116               goto no_rm;
04117             }
04118           break;
04119         case 1:
04120           if (record_read_memory (gdbarch, irp->addr, buf, 1))
04121             return -1;
04122           irp->addr++;
04123           *addr = (int8_t) buf[0];
04124           break;
04125         case 2:
04126           if (record_read_memory (gdbarch, irp->addr, buf, 2))
04127             return -1;
04128           irp->addr += 2;
04129           *addr = extract_signed_integer (buf, 2, byte_order);
04130           break;
04131         }
04132 
04133       switch (irp->rm)
04134         {
04135         case 0:
04136           regcache_raw_read_unsigned (irp->regcache,
04137                                       irp->regmap[X86_RECORD_REBX_REGNUM],
04138                                       &offset64);
04139           *addr = (uint32_t) (*addr + offset64);
04140           regcache_raw_read_unsigned (irp->regcache,
04141                                       irp->regmap[X86_RECORD_RESI_REGNUM],
04142                                       &offset64);
04143           *addr = (uint32_t) (*addr + offset64);
04144           break;
04145         case 1:
04146           regcache_raw_read_unsigned (irp->regcache,
04147                                       irp->regmap[X86_RECORD_REBX_REGNUM],
04148                                       &offset64);
04149           *addr = (uint32_t) (*addr + offset64);
04150           regcache_raw_read_unsigned (irp->regcache,
04151                                       irp->regmap[X86_RECORD_REDI_REGNUM],
04152                                       &offset64);
04153           *addr = (uint32_t) (*addr + offset64);
04154           break;
04155         case 2:
04156           regcache_raw_read_unsigned (irp->regcache,
04157                                       irp->regmap[X86_RECORD_REBP_REGNUM],
04158                                       &offset64);
04159           *addr = (uint32_t) (*addr + offset64);
04160           regcache_raw_read_unsigned (irp->regcache,
04161                                       irp->regmap[X86_RECORD_RESI_REGNUM],
04162                                       &offset64);
04163           *addr = (uint32_t) (*addr + offset64);
04164           break;
04165         case 3:
04166           regcache_raw_read_unsigned (irp->regcache,
04167                                       irp->regmap[X86_RECORD_REBP_REGNUM],
04168                                       &offset64);
04169           *addr = (uint32_t) (*addr + offset64);
04170           regcache_raw_read_unsigned (irp->regcache,
04171                                       irp->regmap[X86_RECORD_REDI_REGNUM],
04172                                       &offset64);
04173           *addr = (uint32_t) (*addr + offset64);
04174           break;
04175         case 4:
04176           regcache_raw_read_unsigned (irp->regcache,
04177                                       irp->regmap[X86_RECORD_RESI_REGNUM],
04178                                       &offset64);
04179           *addr = (uint32_t) (*addr + offset64);
04180           break;
04181         case 5:
04182           regcache_raw_read_unsigned (irp->regcache,
04183                                       irp->regmap[X86_RECORD_REDI_REGNUM],
04184                                       &offset64);
04185           *addr = (uint32_t) (*addr + offset64);
04186           break;
04187         case 6:
04188           regcache_raw_read_unsigned (irp->regcache,
04189                                       irp->regmap[X86_RECORD_REBP_REGNUM],
04190                                       &offset64);
04191           *addr = (uint32_t) (*addr + offset64);
04192           break;
04193         case 7:
04194           regcache_raw_read_unsigned (irp->regcache,
04195                                       irp->regmap[X86_RECORD_REBX_REGNUM],
04196                                       &offset64);
04197           *addr = (uint32_t) (*addr + offset64);
04198           break;
04199         }
04200       *addr &= 0xffff;
04201     }
04202 
04203  no_rm:
04204   return 0;
04205 }
04206 
04207 /* Record the address and contents of the memory that will be changed
04208    by the current instruction.  Return -1 if something goes wrong, 0
04209    otherwise.  */
04210 
04211 static int
04212 i386_record_lea_modrm (struct i386_record_s *irp)
04213 {
04214   struct gdbarch *gdbarch = irp->gdbarch;
04215   uint64_t addr;
04216 
04217   if (irp->override >= 0)
04218     {
04219       if (record_full_memory_query)
04220         {
04221           int q;
04222 
04223           target_terminal_ours ();
04224           q = yquery (_("\
04225 Process record ignores the memory change of instruction at address %s\n\
04226 because it can't get the value of the segment register.\n\
04227 Do you want to stop the program?"),
04228                       paddress (gdbarch, irp->orig_addr));
04229             target_terminal_inferior ();
04230             if (q)
04231               return -1;
04232         }
04233 
04234       return 0;
04235     }
04236 
04237   if (i386_record_lea_modrm_addr (irp, &addr))
04238     return -1;
04239 
04240   if (record_full_arch_list_add_mem (addr, 1 << irp->ot))
04241     return -1;
04242 
04243   return 0;
04244 }
04245 
04246 /* Record the effects of a push operation.  Return -1 if something
04247    goes wrong, 0 otherwise.  */
04248 
04249 static int
04250 i386_record_push (struct i386_record_s *irp, int size)
04251 {
04252   ULONGEST addr;
04253 
04254   if (record_full_arch_list_add_reg (irp->regcache,
04255                                      irp->regmap[X86_RECORD_RESP_REGNUM]))
04256     return -1;
04257   regcache_raw_read_unsigned (irp->regcache,
04258                               irp->regmap[X86_RECORD_RESP_REGNUM],
04259                               &addr);
04260   if (record_full_arch_list_add_mem ((CORE_ADDR) addr - size, size))
04261     return -1;
04262 
04263   return 0;
04264 }
04265 
04266 
04267 /* Defines contents to record.  */
04268 #define I386_SAVE_FPU_REGS              0xfffd
04269 #define I386_SAVE_FPU_ENV               0xfffe
04270 #define I386_SAVE_FPU_ENV_REG_STACK     0xffff
04271 
04272 /* Record the values of the floating point registers which will be
04273    changed by the current instruction.  Returns -1 if something is
04274    wrong, 0 otherwise.  */
04275 
04276 static int i386_record_floats (struct gdbarch *gdbarch,
04277                                struct i386_record_s *ir,
04278                                uint32_t iregnum)
04279 {
04280   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
04281   int i;
04282 
04283   /* Oza: Because of floating point insn push/pop of fpu stack is going to
04284      happen.  Currently we store st0-st7 registers, but we need not store all
04285      registers all the time, in future we use ftag register and record only
04286      those who are not marked as an empty.  */
04287 
04288   if (I386_SAVE_FPU_REGS == iregnum)
04289     {
04290       for (i = I387_ST0_REGNUM (tdep); i <= I387_ST0_REGNUM (tdep) + 7; i++)
04291         {
04292           if (record_full_arch_list_add_reg (ir->regcache, i))
04293             return -1;
04294         }
04295     }
04296   else if (I386_SAVE_FPU_ENV == iregnum)
04297     {
04298       for (i = I387_FCTRL_REGNUM (tdep); i <= I387_FOP_REGNUM (tdep); i++)
04299               {
04300               if (record_full_arch_list_add_reg (ir->regcache, i))
04301                 return -1;
04302               }
04303     }
04304   else if (I386_SAVE_FPU_ENV_REG_STACK == iregnum)
04305     {
04306       for (i = I387_ST0_REGNUM (tdep); i <= I387_FOP_REGNUM (tdep); i++)
04307       {
04308         if (record_full_arch_list_add_reg (ir->regcache, i))
04309           return -1;
04310       }
04311     }
04312   else if ((iregnum >= I387_ST0_REGNUM (tdep)) &&
04313            (iregnum <= I387_FOP_REGNUM (tdep)))
04314     {
04315       if (record_full_arch_list_add_reg (ir->regcache,iregnum))
04316         return -1;
04317     }
04318   else
04319     {
04320       /* Parameter error.  */
04321       return -1;
04322     }
04323   if(I386_SAVE_FPU_ENV != iregnum)
04324     {
04325     for (i = I387_FCTRL_REGNUM (tdep); i <= I387_FOP_REGNUM (tdep); i++)
04326       {
04327       if (record_full_arch_list_add_reg (ir->regcache, i))
04328         return -1;
04329       }
04330     }
04331   return 0;
04332 }
04333 
04334 /* Parse the current instruction, and record the values of the
04335    registers and memory that will be changed by the current
04336    instruction.  Returns -1 if something goes wrong, 0 otherwise.  */
04337 
04338 #define I386_RECORD_FULL_ARCH_LIST_ADD_REG(regnum) \
04339     record_full_arch_list_add_reg (ir.regcache, ir.regmap[(regnum)])
04340 
04341 int
04342 i386_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
04343                      CORE_ADDR input_addr)
04344 {
04345   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
04346   int prefixes = 0;
04347   int regnum = 0;
04348   uint32_t opcode;
04349   uint8_t opcode8;
04350   ULONGEST addr;
04351   gdb_byte buf[MAX_REGISTER_SIZE];
04352   struct i386_record_s ir;
04353   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
04354   uint8_t rex_w = -1;
04355   uint8_t rex_r = 0;
04356 
04357   memset (&ir, 0, sizeof (struct i386_record_s));
04358   ir.regcache = regcache;
04359   ir.addr = input_addr;
04360   ir.orig_addr = input_addr;
04361   ir.aflag = 1;
04362   ir.dflag = 1;
04363   ir.override = -1;
04364   ir.popl_esp_hack = 0;
04365   ir.regmap = tdep->record_regmap;
04366   ir.gdbarch = gdbarch;
04367 
04368   if (record_debug > 1)
04369     fprintf_unfiltered (gdb_stdlog, "Process record: i386_process_record "
04370                                     "addr = %s\n",
04371                         paddress (gdbarch, ir.addr));
04372 
04373   /* prefixes */
04374   while (1)
04375     {
04376       if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
04377         return -1;
04378       ir.addr++;
04379       switch (opcode8)  /* Instruction prefixes */
04380         {
04381         case REPE_PREFIX_OPCODE:
04382           prefixes |= PREFIX_REPZ;
04383           break;
04384         case REPNE_PREFIX_OPCODE:
04385           prefixes |= PREFIX_REPNZ;
04386           break;
04387         case LOCK_PREFIX_OPCODE:
04388           prefixes |= PREFIX_LOCK;
04389           break;
04390         case CS_PREFIX_OPCODE:
04391           ir.override = X86_RECORD_CS_REGNUM;
04392           break;
04393         case SS_PREFIX_OPCODE:
04394           ir.override = X86_RECORD_SS_REGNUM;
04395           break;
04396         case DS_PREFIX_OPCODE:
04397           ir.override = X86_RECORD_DS_REGNUM;
04398           break;
04399         case ES_PREFIX_OPCODE:
04400           ir.override = X86_RECORD_ES_REGNUM;
04401           break;
04402         case FS_PREFIX_OPCODE:
04403           ir.override = X86_RECORD_FS_REGNUM;
04404           break;
04405         case GS_PREFIX_OPCODE:
04406           ir.override = X86_RECORD_GS_REGNUM;
04407           break;
04408         case DATA_PREFIX_OPCODE:
04409           prefixes |= PREFIX_DATA;
04410           break;
04411         case ADDR_PREFIX_OPCODE:
04412           prefixes |= PREFIX_ADDR;
04413           break;
04414         case 0x40:      /* i386 inc %eax */
04415         case 0x41:      /* i386 inc %ecx */
04416         case 0x42:      /* i386 inc %edx */
04417         case 0x43:      /* i386 inc %ebx */
04418         case 0x44:      /* i386 inc %esp */
04419         case 0x45:      /* i386 inc %ebp */
04420         case 0x46:      /* i386 inc %esi */
04421         case 0x47:      /* i386 inc %edi */
04422         case 0x48:      /* i386 dec %eax */
04423         case 0x49:      /* i386 dec %ecx */
04424         case 0x4a:      /* i386 dec %edx */
04425         case 0x4b:      /* i386 dec %ebx */
04426         case 0x4c:      /* i386 dec %esp */
04427         case 0x4d:      /* i386 dec %ebp */
04428         case 0x4e:      /* i386 dec %esi */
04429         case 0x4f:      /* i386 dec %edi */
04430           if (ir.regmap[X86_RECORD_R8_REGNUM])  /* 64 bit target */
04431             {
04432                /* REX */
04433                rex_w = (opcode8 >> 3) & 1;
04434                rex_r = (opcode8 & 0x4) << 1;
04435                ir.rex_x = (opcode8 & 0x2) << 2;
04436                ir.rex_b = (opcode8 & 0x1) << 3;
04437             }
04438           else                                  /* 32 bit target */
04439             goto out_prefixes;
04440           break;
04441         default:
04442           goto out_prefixes;
04443           break;
04444         }
04445     }
04446  out_prefixes:
04447   if (ir.regmap[X86_RECORD_R8_REGNUM] && rex_w == 1)
04448     {
04449       ir.dflag = 2;
04450     }
04451   else
04452     {
04453       if (prefixes & PREFIX_DATA)
04454         ir.dflag ^= 1;
04455     }
04456   if (prefixes & PREFIX_ADDR)
04457     ir.aflag ^= 1;
04458   else if (ir.regmap[X86_RECORD_R8_REGNUM])
04459     ir.aflag = 2;
04460 
04461   /* Now check op code.  */
04462   opcode = (uint32_t) opcode8;
04463  reswitch:
04464   switch (opcode)
04465     {
04466     case 0x0f:
04467       if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
04468         return -1;
04469       ir.addr++;
04470       opcode = (uint32_t) opcode8 | 0x0f00;
04471       goto reswitch;
04472       break;
04473 
04474     case 0x00:    /* arith & logic */
04475     case 0x01:
04476     case 0x02:
04477     case 0x03:
04478     case 0x04:
04479     case 0x05:
04480     case 0x08:
04481     case 0x09:
04482     case 0x0a:
04483     case 0x0b:
04484     case 0x0c:
04485     case 0x0d:
04486     case 0x10:
04487     case 0x11:
04488     case 0x12:
04489     case 0x13:
04490     case 0x14:
04491     case 0x15:
04492     case 0x18:
04493     case 0x19:
04494     case 0x1a:
04495     case 0x1b:
04496     case 0x1c:
04497     case 0x1d:
04498     case 0x20:
04499     case 0x21:
04500     case 0x22:
04501     case 0x23:
04502     case 0x24:
04503     case 0x25:
04504     case 0x28:
04505     case 0x29:
04506     case 0x2a:
04507     case 0x2b:
04508     case 0x2c:
04509     case 0x2d:
04510     case 0x30:
04511     case 0x31:
04512     case 0x32:
04513     case 0x33:
04514     case 0x34:
04515     case 0x35:
04516     case 0x38:
04517     case 0x39:
04518     case 0x3a:
04519     case 0x3b:
04520     case 0x3c:
04521     case 0x3d:
04522       if (((opcode >> 3) & 7) != OP_CMPL)
04523         {
04524           if ((opcode & 1) == 0)
04525             ir.ot = OT_BYTE;
04526           else
04527             ir.ot = ir.dflag + OT_WORD;
04528 
04529           switch ((opcode >> 1) & 3)
04530             {
04531             case 0:    /* OP Ev, Gv */
04532               if (i386_record_modrm (&ir))
04533                 return -1;
04534               if (ir.mod != 3)
04535                 {
04536                   if (i386_record_lea_modrm (&ir))
04537                     return -1;
04538                 }
04539               else
04540                 {
04541                   ir.rm |= ir.rex_b;
04542                   if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
04543                     ir.rm &= 0x3;
04544                   I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
04545                 }
04546               break;
04547             case 1:    /* OP Gv, Ev */
04548               if (i386_record_modrm (&ir))
04549                 return -1;
04550               ir.reg |= rex_r;
04551               if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
04552                 ir.reg &= 0x3;
04553               I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
04554               break;
04555             case 2:    /* OP A, Iv */
04556               I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
04557               break;
04558             }
04559         }
04560       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04561       break;
04562 
04563     case 0x80:    /* GRP1 */
04564     case 0x81:
04565     case 0x82:
04566     case 0x83:
04567       if (i386_record_modrm (&ir))
04568         return -1;
04569 
04570       if (ir.reg != OP_CMPL)
04571         {
04572           if ((opcode & 1) == 0)
04573             ir.ot = OT_BYTE;
04574           else
04575             ir.ot = ir.dflag + OT_WORD;
04576 
04577           if (ir.mod != 3)
04578             {
04579               if (opcode == 0x83)
04580                 ir.rip_offset = 1;
04581               else
04582                 ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
04583               if (i386_record_lea_modrm (&ir))
04584                 return -1;
04585             }
04586           else
04587             I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
04588         }
04589       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04590       break;
04591 
04592     case 0x40:      /* inc */
04593     case 0x41:
04594     case 0x42:
04595     case 0x43:
04596     case 0x44:
04597     case 0x45:
04598     case 0x46:
04599     case 0x47:
04600 
04601     case 0x48:      /* dec */
04602     case 0x49:
04603     case 0x4a:
04604     case 0x4b:
04605     case 0x4c:
04606     case 0x4d:
04607     case 0x4e:
04608     case 0x4f:
04609 
04610       I386_RECORD_FULL_ARCH_LIST_ADD_REG (opcode & 7);
04611       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04612       break;
04613 
04614     case 0xf6:    /* GRP3 */
04615     case 0xf7:
04616       if ((opcode & 1) == 0)
04617         ir.ot = OT_BYTE;
04618       else
04619         ir.ot = ir.dflag + OT_WORD;
04620       if (i386_record_modrm (&ir))
04621         return -1;
04622 
04623       if (ir.mod != 3 && ir.reg == 0)
04624         ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
04625 
04626       switch (ir.reg)
04627         {
04628         case 0:    /* test */
04629           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04630           break;
04631         case 2:    /* not */
04632         case 3:    /* neg */
04633           if (ir.mod != 3)
04634             {
04635               if (i386_record_lea_modrm (&ir))
04636                 return -1;
04637             }
04638           else
04639             {
04640               ir.rm |= ir.rex_b;
04641               if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
04642                 ir.rm &= 0x3;
04643               I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
04644             }
04645           if (ir.reg == 3)  /* neg */
04646             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04647           break;
04648         case 4:    /* mul  */
04649         case 5:    /* imul */
04650         case 6:    /* div  */
04651         case 7:    /* idiv */
04652           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
04653           if (ir.ot != OT_BYTE)
04654             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
04655           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04656           break;
04657         default:
04658           ir.addr -= 2;
04659           opcode = opcode << 8 | ir.modrm;
04660           goto no_support;
04661           break;
04662         }
04663       break;
04664 
04665     case 0xfe:    /* GRP4 */
04666     case 0xff:    /* GRP5 */
04667       if (i386_record_modrm (&ir))
04668         return -1;
04669       if (ir.reg >= 2 && opcode == 0xfe)
04670         {
04671           ir.addr -= 2;
04672           opcode = opcode << 8 | ir.modrm;
04673           goto no_support;
04674         }
04675       switch (ir.reg)
04676         {
04677         case 0:    /* inc */
04678         case 1:    /* dec */
04679           if ((opcode & 1) == 0)
04680             ir.ot = OT_BYTE;
04681           else
04682             ir.ot = ir.dflag + OT_WORD;
04683           if (ir.mod != 3)
04684             {
04685               if (i386_record_lea_modrm (&ir))
04686                 return -1;
04687             }
04688           else
04689             {
04690               ir.rm |= ir.rex_b;
04691               if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
04692                 ir.rm &= 0x3;
04693               I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
04694             }
04695           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04696           break;
04697         case 2:    /* call */
04698           if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
04699             ir.dflag = 2;
04700           if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
04701             return -1;
04702           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04703           break;
04704         case 3:    /* lcall */
04705           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
04706           if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
04707             return -1;
04708           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04709           break;
04710         case 4:    /* jmp  */
04711         case 5:    /* ljmp */
04712           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04713           break;
04714         case 6:    /* push */
04715           if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
04716             ir.dflag = 2;
04717           if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
04718             return -1;
04719           break;
04720         default:
04721           ir.addr -= 2;
04722           opcode = opcode << 8 | ir.modrm;
04723           goto no_support;
04724           break;
04725         }
04726       break;
04727 
04728     case 0x84:    /* test */
04729     case 0x85:
04730     case 0xa8:
04731     case 0xa9:
04732       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04733       break;
04734 
04735     case 0x98:    /* CWDE/CBW */
04736       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
04737       break;
04738 
04739     case 0x99:    /* CDQ/CWD */
04740       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
04741       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
04742       break;
04743 
04744     case 0x0faf:  /* imul */
04745     case 0x69:
04746     case 0x6b:
04747       ir.ot = ir.dflag + OT_WORD;
04748       if (i386_record_modrm (&ir))
04749         return -1;
04750       if (opcode == 0x69)
04751         ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
04752       else if (opcode == 0x6b)
04753         ir.rip_offset = 1;
04754       ir.reg |= rex_r;
04755       if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
04756         ir.reg &= 0x3;
04757       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
04758       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04759       break;
04760 
04761     case 0x0fc0:  /* xadd */
04762     case 0x0fc1:
04763       if ((opcode & 1) == 0)
04764         ir.ot = OT_BYTE;
04765       else
04766         ir.ot = ir.dflag + OT_WORD;
04767       if (i386_record_modrm (&ir))
04768         return -1;
04769       ir.reg |= rex_r;
04770       if (ir.mod == 3)
04771         {
04772           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
04773             ir.reg &= 0x3;
04774           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
04775           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
04776             ir.rm &= 0x3;
04777           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
04778         }
04779       else
04780         {
04781           if (i386_record_lea_modrm (&ir))
04782             return -1;
04783           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
04784             ir.reg &= 0x3;
04785           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
04786         }
04787       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04788       break;
04789 
04790     case 0x0fb0:  /* cmpxchg */
04791     case 0x0fb1:
04792       if ((opcode & 1) == 0)
04793         ir.ot = OT_BYTE;
04794       else
04795         ir.ot = ir.dflag + OT_WORD;
04796       if (i386_record_modrm (&ir))
04797         return -1;
04798       if (ir.mod == 3)
04799         {
04800           ir.reg |= rex_r;
04801           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
04802           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
04803             ir.reg &= 0x3;
04804           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
04805         }
04806       else
04807         {
04808           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
04809           if (i386_record_lea_modrm (&ir))
04810             return -1;
04811         }
04812       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04813       break;
04814 
04815     case 0x0fc7:    /* cmpxchg8b */
04816       if (i386_record_modrm (&ir))
04817         return -1;
04818       if (ir.mod == 3)
04819         {
04820           ir.addr -= 2;
04821           opcode = opcode << 8 | ir.modrm;
04822           goto no_support;
04823         }
04824       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
04825       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
04826       if (i386_record_lea_modrm (&ir))
04827         return -1;
04828       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04829       break;
04830 
04831     case 0x50:    /* push */
04832     case 0x51:
04833     case 0x52:
04834     case 0x53:
04835     case 0x54:
04836     case 0x55:
04837     case 0x56:
04838     case 0x57:
04839     case 0x68:
04840     case 0x6a:
04841       if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
04842         ir.dflag = 2;
04843       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
04844         return -1;
04845       break;
04846 
04847     case 0x06:    /* push es */
04848     case 0x0e:    /* push cs */
04849     case 0x16:    /* push ss */
04850     case 0x1e:    /* push ds */
04851       if (ir.regmap[X86_RECORD_R8_REGNUM])
04852         {
04853           ir.addr -= 1;
04854           goto no_support;
04855         }
04856       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
04857         return -1;
04858       break;
04859 
04860     case 0x0fa0:    /* push fs */
04861     case 0x0fa8:    /* push gs */
04862       if (ir.regmap[X86_RECORD_R8_REGNUM])
04863         {
04864           ir.addr -= 2;
04865           goto no_support;
04866         }
04867       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
04868         return -1;
04869       break;
04870 
04871     case 0x60:    /* pusha */
04872       if (ir.regmap[X86_RECORD_R8_REGNUM])
04873         {
04874           ir.addr -= 1;
04875           goto no_support;
04876         }
04877       if (i386_record_push (&ir, 1 << (ir.dflag + 4)))
04878         return -1;
04879       break;
04880 
04881     case 0x58:    /* pop */
04882     case 0x59:
04883     case 0x5a:
04884     case 0x5b:
04885     case 0x5c:
04886     case 0x5d:
04887     case 0x5e:
04888     case 0x5f:
04889       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
04890       I386_RECORD_FULL_ARCH_LIST_ADD_REG ((opcode & 0x7) | ir.rex_b);
04891       break;
04892 
04893     case 0x61:    /* popa */
04894       if (ir.regmap[X86_RECORD_R8_REGNUM])
04895         {
04896           ir.addr -= 1;
04897           goto no_support;
04898         }
04899       for (regnum = X86_RECORD_REAX_REGNUM; 
04900            regnum <= X86_RECORD_REDI_REGNUM;
04901            regnum++)
04902         I386_RECORD_FULL_ARCH_LIST_ADD_REG (regnum);
04903       break;
04904 
04905     case 0x8f:    /* pop */
04906       if (ir.regmap[X86_RECORD_R8_REGNUM])
04907         ir.ot = ir.dflag ? OT_QUAD : OT_WORD;
04908       else
04909         ir.ot = ir.dflag + OT_WORD;
04910       if (i386_record_modrm (&ir))
04911         return -1;
04912       if (ir.mod == 3)
04913         I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
04914       else
04915         {
04916           ir.popl_esp_hack = 1 << ir.ot;
04917           if (i386_record_lea_modrm (&ir))
04918             return -1;
04919         }
04920       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
04921       break;
04922 
04923     case 0xc8:    /* enter */
04924       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBP_REGNUM);
04925       if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
04926         ir.dflag = 2;
04927       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
04928         return -1;
04929       break;
04930 
04931     case 0xc9:    /* leave */
04932       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
04933       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBP_REGNUM);
04934       break;
04935 
04936     case 0x07:    /* pop es */
04937       if (ir.regmap[X86_RECORD_R8_REGNUM])
04938         {
04939           ir.addr -= 1;
04940           goto no_support;
04941         }
04942       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
04943       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_ES_REGNUM);
04944       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04945       break;
04946 
04947     case 0x17:    /* pop ss */
04948       if (ir.regmap[X86_RECORD_R8_REGNUM])
04949         {
04950           ir.addr -= 1;
04951           goto no_support;
04952         }
04953       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
04954       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_SS_REGNUM);
04955       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04956       break;
04957 
04958     case 0x1f:    /* pop ds */
04959       if (ir.regmap[X86_RECORD_R8_REGNUM])
04960         {
04961           ir.addr -= 1;
04962           goto no_support;
04963         }
04964       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
04965       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_DS_REGNUM);
04966       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04967       break;
04968 
04969     case 0x0fa1:    /* pop fs */
04970       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
04971       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_FS_REGNUM);
04972       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04973       break;
04974 
04975     case 0x0fa9:    /* pop gs */
04976       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
04977       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_GS_REGNUM);
04978       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
04979       break;
04980 
04981     case 0x88:    /* mov */
04982     case 0x89:
04983     case 0xc6:
04984     case 0xc7:
04985       if ((opcode & 1) == 0)
04986         ir.ot = OT_BYTE;
04987       else
04988         ir.ot = ir.dflag + OT_WORD;
04989 
04990       if (i386_record_modrm (&ir))
04991         return -1;
04992 
04993       if (ir.mod != 3)
04994         {
04995           if (opcode == 0xc6 || opcode == 0xc7)
04996             ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
04997           if (i386_record_lea_modrm (&ir))
04998             return -1;
04999         }
05000       else
05001         {
05002           if (opcode == 0xc6 || opcode == 0xc7)
05003             ir.rm |= ir.rex_b;
05004           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
05005             ir.rm &= 0x3;
05006           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
05007         }
05008       break;
05009 
05010     case 0x8a:    /* mov */
05011     case 0x8b:
05012       if ((opcode & 1) == 0)
05013         ir.ot = OT_BYTE;
05014       else
05015         ir.ot = ir.dflag + OT_WORD;
05016       if (i386_record_modrm (&ir))
05017         return -1;
05018       ir.reg |= rex_r;
05019       if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
05020         ir.reg &= 0x3;
05021       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
05022       break;
05023 
05024     case 0x8c:    /* mov seg */
05025       if (i386_record_modrm (&ir))
05026         return -1;
05027       if (ir.reg > 5)
05028         {
05029           ir.addr -= 2;
05030           opcode = opcode << 8 | ir.modrm;
05031           goto no_support;
05032         }
05033 
05034       if (ir.mod == 3)
05035         I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
05036       else
05037         {
05038           ir.ot = OT_WORD;
05039           if (i386_record_lea_modrm (&ir))
05040             return -1;
05041         }
05042       break;
05043 
05044     case 0x8e:    /* mov seg */
05045       if (i386_record_modrm (&ir))
05046         return -1;
05047       switch (ir.reg)
05048         {
05049         case 0:
05050           regnum = X86_RECORD_ES_REGNUM;
05051           break;
05052         case 2:
05053           regnum = X86_RECORD_SS_REGNUM;
05054           break;
05055         case 3:
05056           regnum = X86_RECORD_DS_REGNUM;
05057           break;
05058         case 4:
05059           regnum = X86_RECORD_FS_REGNUM;
05060           break;
05061         case 5:
05062           regnum = X86_RECORD_GS_REGNUM;
05063           break;
05064         default:
05065           ir.addr -= 2;
05066           opcode = opcode << 8 | ir.modrm;
05067           goto no_support;
05068           break;
05069         }
05070       I386_RECORD_FULL_ARCH_LIST_ADD_REG (regnum);
05071       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05072       break;
05073 
05074     case 0x0fb6:    /* movzbS */
05075     case 0x0fb7:    /* movzwS */
05076     case 0x0fbe:    /* movsbS */
05077     case 0x0fbf:    /* movswS */
05078       if (i386_record_modrm (&ir))
05079         return -1;
05080       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
05081       break;
05082 
05083     case 0x8d:      /* lea */
05084       if (i386_record_modrm (&ir))
05085         return -1;
05086       if (ir.mod == 3)
05087         {
05088           ir.addr -= 2;
05089           opcode = opcode << 8 | ir.modrm;
05090           goto no_support;
05091         }
05092       ir.ot = ir.dflag;
05093       ir.reg |= rex_r;
05094       if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
05095         ir.reg &= 0x3;
05096       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
05097       break;
05098 
05099     case 0xa0:    /* mov EAX */
05100     case 0xa1:
05101 
05102     case 0xd7:    /* xlat */
05103       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
05104       break;
05105 
05106     case 0xa2:    /* mov EAX */
05107     case 0xa3:
05108       if (ir.override >= 0)
05109         {
05110           if (record_full_memory_query)
05111             {
05112               int q;
05113 
05114               target_terminal_ours ();
05115               q = yquery (_("\
05116 Process record ignores the memory change of instruction at address %s\n\
05117 because it can't get the value of the segment register.\n\
05118 Do you want to stop the program?"),
05119                           paddress (gdbarch, ir.orig_addr));
05120               target_terminal_inferior ();
05121               if (q)
05122                 return -1;
05123             }
05124         }
05125       else
05126         {
05127           if ((opcode & 1) == 0)
05128             ir.ot = OT_BYTE;
05129           else
05130             ir.ot = ir.dflag + OT_WORD;
05131           if (ir.aflag == 2)
05132             {
05133               if (record_read_memory (gdbarch, ir.addr, buf, 8))
05134                 return -1;
05135               ir.addr += 8;
05136               addr = extract_unsigned_integer (buf, 8, byte_order);
05137             }
05138           else if (ir.aflag)
05139             {
05140               if (record_read_memory (gdbarch, ir.addr, buf, 4))
05141                 return -1;
05142               ir.addr += 4;
05143               addr = extract_unsigned_integer (buf, 4, byte_order);
05144             }
05145           else
05146             {
05147               if (record_read_memory (gdbarch, ir.addr, buf, 2))
05148                 return -1;
05149               ir.addr += 2;
05150               addr = extract_unsigned_integer (buf, 2, byte_order);
05151             }
05152           if (record_full_arch_list_add_mem (addr, 1 << ir.ot))
05153             return -1;
05154         }
05155       break;
05156 
05157     case 0xb0:    /* mov R, Ib */
05158     case 0xb1:
05159     case 0xb2:
05160     case 0xb3:
05161     case 0xb4:
05162     case 0xb5:
05163     case 0xb6:
05164     case 0xb7:
05165       I386_RECORD_FULL_ARCH_LIST_ADD_REG ((ir.regmap[X86_RECORD_R8_REGNUM])
05166                                           ? ((opcode & 0x7) | ir.rex_b)
05167                                           : ((opcode & 0x7) & 0x3));
05168       break;
05169 
05170     case 0xb8:    /* mov R, Iv */
05171     case 0xb9:
05172     case 0xba:
05173     case 0xbb:
05174     case 0xbc:
05175     case 0xbd:
05176     case 0xbe:
05177     case 0xbf:
05178       I386_RECORD_FULL_ARCH_LIST_ADD_REG ((opcode & 0x7) | ir.rex_b);
05179       break;
05180 
05181     case 0x91:    /* xchg R, EAX */
05182     case 0x92:
05183     case 0x93:
05184     case 0x94:
05185     case 0x95:
05186     case 0x96:
05187     case 0x97:
05188       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
05189       I386_RECORD_FULL_ARCH_LIST_ADD_REG (opcode & 0x7);
05190       break;
05191 
05192     case 0x86:    /* xchg Ev, Gv */
05193     case 0x87:
05194       if ((opcode & 1) == 0)
05195         ir.ot = OT_BYTE;
05196       else
05197         ir.ot = ir.dflag + OT_WORD;
05198       if (i386_record_modrm (&ir))
05199         return -1;
05200       if (ir.mod == 3)
05201         {
05202           ir.rm |= ir.rex_b;
05203           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
05204             ir.rm &= 0x3;
05205           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
05206         }
05207       else
05208         {
05209           if (i386_record_lea_modrm (&ir))
05210             return -1;
05211         }
05212       ir.reg |= rex_r;
05213       if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
05214         ir.reg &= 0x3;
05215       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
05216       break;
05217 
05218     case 0xc4:    /* les Gv */
05219     case 0xc5:    /* lds Gv */
05220       if (ir.regmap[X86_RECORD_R8_REGNUM])
05221         {
05222           ir.addr -= 1;
05223           goto no_support;
05224         }
05225       /* FALLTHROUGH */
05226     case 0x0fb2:    /* lss Gv */
05227     case 0x0fb4:    /* lfs Gv */
05228     case 0x0fb5:    /* lgs Gv */
05229       if (i386_record_modrm (&ir))
05230         return -1;
05231       if (ir.mod == 3)
05232         {
05233           if (opcode > 0xff)
05234             ir.addr -= 3;
05235           else
05236             ir.addr -= 2;
05237           opcode = opcode << 8 | ir.modrm;
05238           goto no_support;
05239         }
05240       switch (opcode)
05241         {
05242         case 0xc4:    /* les Gv */
05243           regnum = X86_RECORD_ES_REGNUM;
05244           break;
05245         case 0xc5:    /* lds Gv */
05246           regnum = X86_RECORD_DS_REGNUM;
05247           break;
05248         case 0x0fb2:  /* lss Gv */
05249           regnum = X86_RECORD_SS_REGNUM;
05250           break;
05251         case 0x0fb4:  /* lfs Gv */
05252           regnum = X86_RECORD_FS_REGNUM;
05253           break;
05254         case 0x0fb5:  /* lgs Gv */
05255           regnum = X86_RECORD_GS_REGNUM;
05256           break;
05257         }
05258       I386_RECORD_FULL_ARCH_LIST_ADD_REG (regnum);
05259       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
05260       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05261       break;
05262 
05263     case 0xc0:    /* shifts */
05264     case 0xc1:
05265     case 0xd0:
05266     case 0xd1:
05267     case 0xd2:
05268     case 0xd3:
05269       if ((opcode & 1) == 0)
05270         ir.ot = OT_BYTE;
05271       else
05272         ir.ot = ir.dflag + OT_WORD;
05273       if (i386_record_modrm (&ir))
05274         return -1;
05275       if (ir.mod != 3 && (opcode == 0xd2 || opcode == 0xd3))
05276         {
05277           if (i386_record_lea_modrm (&ir))
05278             return -1;
05279         }
05280       else
05281         {
05282           ir.rm |= ir.rex_b;
05283           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
05284             ir.rm &= 0x3;
05285           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
05286         }
05287       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05288       break;
05289 
05290     case 0x0fa4:
05291     case 0x0fa5:
05292     case 0x0fac:
05293     case 0x0fad:
05294       if (i386_record_modrm (&ir))
05295         return -1;
05296       if (ir.mod == 3)
05297         {
05298           if (record_full_arch_list_add_reg (ir.regcache, ir.rm))
05299             return -1;
05300         }
05301       else
05302         {
05303           if (i386_record_lea_modrm (&ir))
05304             return -1;
05305         }
05306       break;
05307 
05308     case 0xd8:    /* Floats.  */
05309     case 0xd9:
05310     case 0xda:
05311     case 0xdb:
05312     case 0xdc:
05313     case 0xdd:
05314     case 0xde:
05315     case 0xdf:
05316       if (i386_record_modrm (&ir))
05317         return -1;
05318       ir.reg |= ((opcode & 7) << 3);
05319       if (ir.mod != 3)
05320         {
05321           /* Memory.  */
05322           uint64_t addr64;
05323 
05324           if (i386_record_lea_modrm_addr (&ir, &addr64))
05325             return -1;
05326           switch (ir.reg)
05327             {
05328             case 0x02:
05329             case 0x12:
05330             case 0x22:
05331             case 0x32:
05332               /* For fcom, ficom nothing to do.  */
05333               break;
05334             case 0x03:
05335             case 0x13:
05336             case 0x23:
05337             case 0x33:
05338               /* For fcomp, ficomp pop FPU stack, store all.  */
05339               if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
05340                 return -1;
05341               break;
05342             case 0x00:
05343             case 0x01:
05344             case 0x04:
05345             case 0x05:
05346             case 0x06:
05347             case 0x07:
05348             case 0x10:
05349             case 0x11:
05350             case 0x14:
05351             case 0x15:
05352             case 0x16:
05353             case 0x17:
05354             case 0x20:
05355             case 0x21:
05356             case 0x24:
05357             case 0x25:
05358             case 0x26:
05359             case 0x27:
05360             case 0x30:
05361             case 0x31:
05362             case 0x34:
05363             case 0x35:
05364             case 0x36:
05365             case 0x37:
05366               /* For fadd, fmul, fsub, fsubr, fdiv, fdivr, fiadd, fimul,
05367                  fisub, fisubr, fidiv, fidivr, modR/M.reg is an extension
05368                  of code,  always affects st(0) register.  */
05369               if (i386_record_floats (gdbarch, &ir, I387_ST0_REGNUM (tdep)))
05370                 return -1;
05371               break;
05372             case 0x08:
05373             case 0x0a:
05374             case 0x0b:
05375             case 0x18:
05376             case 0x19:
05377             case 0x1a:
05378             case 0x1b:
05379             case 0x1d:
05380             case 0x28:
05381             case 0x29:
05382             case 0x2a:
05383             case 0x2b:
05384             case 0x38:
05385             case 0x39:
05386             case 0x3a:
05387             case 0x3b:
05388             case 0x3c:
05389             case 0x3d:
05390               switch (ir.reg & 7)
05391                 {
05392                 case 0:
05393                   /* Handling fld, fild.  */
05394                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
05395                     return -1;
05396                   break;
05397                 case 1:
05398                   switch (ir.reg >> 4)
05399                     {
05400                     case 0:
05401                       if (record_full_arch_list_add_mem (addr64, 4))
05402                         return -1;
05403                       break;
05404                     case 2:
05405                       if (record_full_arch_list_add_mem (addr64, 8))
05406                         return -1;
05407                       break;
05408                     case 3:
05409                       break;
05410                     default:
05411                       if (record_full_arch_list_add_mem (addr64, 2))
05412                         return -1;
05413                       break;
05414                     }
05415                   break;
05416                 default:
05417                   switch (ir.reg >> 4)
05418                     {
05419                     case 0:
05420                       if (record_full_arch_list_add_mem (addr64, 4))
05421                         return -1;
05422                       if (3 == (ir.reg & 7))
05423                         {
05424                           /* For fstp m32fp.  */
05425                           if (i386_record_floats (gdbarch, &ir,
05426                                                   I386_SAVE_FPU_REGS))
05427                             return -1;
05428                         }
05429                       break;
05430                     case 1:
05431                       if (record_full_arch_list_add_mem (addr64, 4))
05432                         return -1;
05433                       if ((3 == (ir.reg & 7))
05434                           || (5 == (ir.reg & 7))
05435                           || (7 == (ir.reg & 7)))
05436                         {
05437                           /* For fstp insn.  */
05438                           if (i386_record_floats (gdbarch, &ir,
05439                                                   I386_SAVE_FPU_REGS))
05440                             return -1;
05441                         }
05442                       break;
05443                     case 2:
05444                       if (record_full_arch_list_add_mem (addr64, 8))
05445                         return -1;
05446                       if (3 == (ir.reg & 7))
05447                         {
05448                           /* For fstp m64fp.  */
05449                           if (i386_record_floats (gdbarch, &ir,
05450                                                   I386_SAVE_FPU_REGS))
05451                             return -1;
05452                         }
05453                       break;
05454                     case 3:
05455                       if ((3 <= (ir.reg & 7)) && (6 <= (ir.reg & 7)))
05456                         {
05457                           /* For fistp, fbld, fild, fbstp.  */
05458                           if (i386_record_floats (gdbarch, &ir,
05459                                                   I386_SAVE_FPU_REGS))
05460                             return -1;
05461                         }
05462                       /* Fall through */
05463                     default:
05464                       if (record_full_arch_list_add_mem (addr64, 2))
05465                         return -1;
05466                       break;
05467                     }
05468                   break;
05469                 }
05470               break;
05471             case 0x0c:
05472               /* Insn fldenv.  */
05473               if (i386_record_floats (gdbarch, &ir,
05474                                       I386_SAVE_FPU_ENV_REG_STACK))
05475                 return -1;
05476               break;
05477             case 0x0d:
05478               /* Insn fldcw.  */
05479               if (i386_record_floats (gdbarch, &ir, I387_FCTRL_REGNUM (tdep)))
05480                 return -1;
05481               break;
05482             case 0x2c:
05483               /* Insn frstor.  */
05484               if (i386_record_floats (gdbarch, &ir,
05485                                       I386_SAVE_FPU_ENV_REG_STACK))
05486                 return -1;
05487               break;
05488             case 0x0e:
05489               if (ir.dflag)
05490                 {
05491                   if (record_full_arch_list_add_mem (addr64, 28))
05492                     return -1;
05493                 }
05494               else
05495                 {
05496                   if (record_full_arch_list_add_mem (addr64, 14))
05497                     return -1;
05498                 }
05499               break;
05500             case 0x0f:
05501             case 0x2f:
05502               if (record_full_arch_list_add_mem (addr64, 2))
05503                 return -1;
05504               /* Insn fstp, fbstp.  */
05505               if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
05506                 return -1;
05507               break;
05508             case 0x1f:
05509             case 0x3e:
05510               if (record_full_arch_list_add_mem (addr64, 10))
05511                 return -1;
05512               break;
05513             case 0x2e:
05514               if (ir.dflag)
05515                 {
05516                   if (record_full_arch_list_add_mem (addr64, 28))
05517                     return -1;
05518                   addr64 += 28;
05519                 }
05520               else
05521                 {
05522                   if (record_full_arch_list_add_mem (addr64, 14))
05523                     return -1;
05524                   addr64 += 14;
05525                 }
05526               if (record_full_arch_list_add_mem (addr64, 80))
05527                 return -1;
05528               /* Insn fsave.  */
05529               if (i386_record_floats (gdbarch, &ir,
05530                                       I386_SAVE_FPU_ENV_REG_STACK))
05531                 return -1;
05532               break;
05533             case 0x3f:
05534               if (record_full_arch_list_add_mem (addr64, 8))
05535                 return -1;
05536               /* Insn fistp.  */
05537               if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
05538                 return -1;
05539               break;
05540             default:
05541               ir.addr -= 2;
05542               opcode = opcode << 8 | ir.modrm;
05543               goto no_support;
05544               break;
05545             }
05546         }
05547       /* Opcode is an extension of modR/M byte.  */
05548       else
05549         {
05550           switch (opcode)
05551             {
05552             case 0xd8:
05553               if (i386_record_floats (gdbarch, &ir, I387_ST0_REGNUM (tdep)))
05554                 return -1;
05555               break;
05556             case 0xd9:
05557               if (0x0c == (ir.modrm >> 4))
05558                 {
05559                   if ((ir.modrm & 0x0f) <= 7)
05560                     {
05561                       if (i386_record_floats (gdbarch, &ir,
05562                                               I386_SAVE_FPU_REGS))
05563                         return -1;
05564                     }
05565                   else
05566                     {
05567                       if (i386_record_floats (gdbarch, &ir,
05568                                               I387_ST0_REGNUM (tdep)))
05569                         return -1;
05570                       /* If only st(0) is changing, then we have already
05571                          recorded.  */
05572                       if ((ir.modrm & 0x0f) - 0x08)
05573                         {
05574                           if (i386_record_floats (gdbarch, &ir,
05575                                                   I387_ST0_REGNUM (tdep) +
05576                                                   ((ir.modrm & 0x0f) - 0x08)))
05577                             return -1;
05578                         }
05579                     }
05580                 }
05581               else
05582                 {
05583                   switch (ir.modrm)
05584                     {
05585                     case 0xe0:
05586                     case 0xe1:
05587                     case 0xf0:
05588                     case 0xf5:
05589                     case 0xf8:
05590                     case 0xfa:
05591                     case 0xfc:
05592                     case 0xfe:
05593                     case 0xff:
05594                       if (i386_record_floats (gdbarch, &ir,
05595                                               I387_ST0_REGNUM (tdep)))
05596                         return -1;
05597                       break;
05598                     case 0xf1:
05599                     case 0xf2:
05600                     case 0xf3:
05601                     case 0xf4:
05602                     case 0xf6:
05603                     case 0xf7:
05604                     case 0xe8:
05605                     case 0xe9:
05606                     case 0xea:
05607                     case 0xeb:
05608                     case 0xec:
05609                     case 0xed:
05610                     case 0xee:
05611                     case 0xf9:
05612                     case 0xfb:
05613                       if (i386_record_floats (gdbarch, &ir,
05614                                               I386_SAVE_FPU_REGS))
05615                         return -1;
05616                       break;
05617                     case 0xfd:
05618                       if (i386_record_floats (gdbarch, &ir,
05619                                               I387_ST0_REGNUM (tdep)))
05620                         return -1;
05621                       if (i386_record_floats (gdbarch, &ir,
05622                                               I387_ST0_REGNUM (tdep) + 1))
05623                         return -1;
05624                       break;
05625                     }
05626                 }
05627               break;
05628             case 0xda:
05629               if (0xe9 == ir.modrm)
05630                 {
05631                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
05632                     return -1;
05633                 }
05634               else if ((0x0c == ir.modrm >> 4) || (0x0d == ir.modrm >> 4))
05635                 {
05636                   if (i386_record_floats (gdbarch, &ir,
05637                                           I387_ST0_REGNUM (tdep)))
05638                     return -1;
05639                   if (((ir.modrm & 0x0f) > 0) && ((ir.modrm & 0x0f) <= 7))
05640                     {
05641                       if (i386_record_floats (gdbarch, &ir,
05642                                               I387_ST0_REGNUM (tdep) +
05643                                               (ir.modrm & 0x0f)))
05644                         return -1;
05645                     }
05646                   else if ((ir.modrm & 0x0f) - 0x08)
05647                     {
05648                       if (i386_record_floats (gdbarch, &ir,
05649                                               I387_ST0_REGNUM (tdep) +
05650                                               ((ir.modrm & 0x0f) - 0x08)))
05651                         return -1;
05652                     }
05653                 }
05654               break;
05655             case 0xdb:
05656               if (0xe3 == ir.modrm)
05657                 {
05658                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_ENV))
05659                     return -1;
05660                 }
05661               else if ((0x0c == ir.modrm >> 4) || (0x0d == ir.modrm >> 4))
05662                 {
05663                   if (i386_record_floats (gdbarch, &ir,
05664                                           I387_ST0_REGNUM (tdep)))
05665                     return -1;
05666                   if (((ir.modrm & 0x0f) > 0) && ((ir.modrm & 0x0f) <= 7))
05667                     {
05668                       if (i386_record_floats (gdbarch, &ir,
05669                                               I387_ST0_REGNUM (tdep) +
05670                                               (ir.modrm & 0x0f)))
05671                         return -1;
05672                     }
05673                   else if ((ir.modrm & 0x0f) - 0x08)
05674                     {
05675                       if (i386_record_floats (gdbarch, &ir,
05676                                               I387_ST0_REGNUM (tdep) +
05677                                               ((ir.modrm & 0x0f) - 0x08)))
05678                         return -1;
05679                     }
05680                 }
05681               break;
05682             case 0xdc:
05683               if ((0x0c == ir.modrm >> 4)
05684                   || (0x0d == ir.modrm >> 4)
05685                   || (0x0f == ir.modrm >> 4))
05686                 {
05687                   if ((ir.modrm & 0x0f) <= 7)
05688                     {
05689                       if (i386_record_floats (gdbarch, &ir,
05690                                               I387_ST0_REGNUM (tdep) +
05691                                               (ir.modrm & 0x0f)))
05692                         return -1;
05693                     }
05694                   else
05695                     {
05696                       if (i386_record_floats (gdbarch, &ir,
05697                                               I387_ST0_REGNUM (tdep) +
05698                                               ((ir.modrm & 0x0f) - 0x08)))
05699                         return -1;
05700                     }
05701                 }
05702               break;
05703             case 0xdd:
05704               if (0x0c == ir.modrm >> 4)
05705                 {
05706                   if (i386_record_floats (gdbarch, &ir,
05707                                           I387_FTAG_REGNUM (tdep)))
05708                     return -1;
05709                 }
05710               else if ((0x0d == ir.modrm >> 4) || (0x0e == ir.modrm >> 4))
05711                 {
05712                   if ((ir.modrm & 0x0f) <= 7)
05713                     {
05714                       if (i386_record_floats (gdbarch, &ir,
05715                                               I387_ST0_REGNUM (tdep) +
05716                                               (ir.modrm & 0x0f)))
05717                         return -1;
05718                     }
05719                   else
05720                     {
05721                       if (i386_record_floats (gdbarch, &ir,
05722                                               I386_SAVE_FPU_REGS))
05723                         return -1;
05724                     }
05725                 }
05726               break;
05727             case 0xde:
05728               if ((0x0c == ir.modrm >> 4)
05729                   || (0x0e == ir.modrm >> 4)
05730                   || (0x0f == ir.modrm >> 4)
05731                   || (0xd9 == ir.modrm))
05732                 {
05733                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
05734                     return -1;
05735                 }
05736               break;
05737             case 0xdf:
05738               if (0xe0 == ir.modrm)
05739                 {
05740                   if (record_full_arch_list_add_reg (ir.regcache,
05741                                                      I386_EAX_REGNUM))
05742                     return -1;
05743                 }
05744               else if ((0x0f == ir.modrm >> 4) || (0x0e == ir.modrm >> 4))
05745                 {
05746                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
05747                     return -1;
05748                 }
05749               break;
05750             }
05751         }
05752       break;
05753       /* string ops */
05754     case 0xa4:    /* movsS */
05755     case 0xa5:
05756     case 0xaa:    /* stosS */
05757     case 0xab:
05758     case 0x6c:    /* insS */
05759     case 0x6d:
05760       regcache_raw_read_unsigned (ir.regcache,
05761                                   ir.regmap[X86_RECORD_RECX_REGNUM],
05762                                   &addr);
05763       if (addr)
05764         {
05765           ULONGEST es, ds;
05766 
05767           if ((opcode & 1) == 0)
05768             ir.ot = OT_BYTE;
05769           else
05770             ir.ot = ir.dflag + OT_WORD;
05771           regcache_raw_read_unsigned (ir.regcache,
05772                                       ir.regmap[X86_RECORD_REDI_REGNUM],
05773                                       &addr);
05774 
05775           regcache_raw_read_unsigned (ir.regcache,
05776                                       ir.regmap[X86_RECORD_ES_REGNUM],
05777                                       &es);
05778           regcache_raw_read_unsigned (ir.regcache,
05779                                       ir.regmap[X86_RECORD_DS_REGNUM],
05780                                       &ds);
05781           if (ir.aflag && (es != ds))
05782             {
05783               /* addr += ((uint32_t) read_register (I386_ES_REGNUM)) << 4; */
05784               if (record_full_memory_query)
05785                 {
05786                   int q;
05787 
05788                   target_terminal_ours ();
05789                   q = yquery (_("\
05790 Process record ignores the memory change of instruction at address %s\n\
05791 because it can't get the value of the segment register.\n\
05792 Do you want to stop the program?"),
05793                               paddress (gdbarch, ir.orig_addr));
05794                   target_terminal_inferior ();
05795                   if (q)
05796                     return -1;
05797                 }
05798             }
05799           else
05800             {
05801               if (record_full_arch_list_add_mem (addr, 1 << ir.ot))
05802                 return -1;
05803             }
05804 
05805           if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
05806             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
05807           if (opcode == 0xa4 || opcode == 0xa5)
05808             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
05809           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
05810           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05811         }
05812       break;
05813 
05814     case 0xa6:    /* cmpsS */
05815     case 0xa7:
05816       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
05817       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
05818       if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
05819         I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
05820       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05821       break;
05822 
05823     case 0xac:    /* lodsS */
05824     case 0xad:
05825       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
05826       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
05827       if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
05828         I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
05829       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05830       break;
05831 
05832     case 0xae:    /* scasS */
05833     case 0xaf:
05834       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
05835       if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
05836         I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
05837       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05838       break;
05839 
05840     case 0x6e:    /* outsS */
05841     case 0x6f:
05842       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
05843       if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
05844         I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
05845       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05846       break;
05847 
05848     case 0xe4:    /* port I/O */
05849     case 0xe5:
05850     case 0xec:
05851     case 0xed:
05852       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05853       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
05854       break;
05855 
05856     case 0xe6:
05857     case 0xe7:
05858     case 0xee:
05859     case 0xef:
05860       break;
05861 
05862       /* control */
05863     case 0xc2:    /* ret im */
05864     case 0xc3:    /* ret */
05865       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
05866       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05867       break;
05868 
05869     case 0xca:    /* lret im */
05870     case 0xcb:    /* lret */
05871     case 0xcf:    /* iret */
05872       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
05873       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
05874       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05875       break;
05876 
05877     case 0xe8:    /* call im */
05878       if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
05879         ir.dflag = 2;
05880       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
05881         return -1;
05882       break;
05883 
05884     case 0x9a:    /* lcall im */
05885       if (ir.regmap[X86_RECORD_R8_REGNUM])
05886         {
05887           ir.addr -= 1;
05888           goto no_support;
05889         }
05890       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
05891       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
05892         return -1;
05893       break;
05894 
05895     case 0xe9:    /* jmp im */
05896     case 0xea:    /* ljmp im */
05897     case 0xeb:    /* jmp Jb */
05898     case 0x70:    /* jcc Jb */
05899     case 0x71:
05900     case 0x72:
05901     case 0x73:
05902     case 0x74:
05903     case 0x75:
05904     case 0x76:
05905     case 0x77:
05906     case 0x78:
05907     case 0x79:
05908     case 0x7a:
05909     case 0x7b:
05910     case 0x7c:
05911     case 0x7d:
05912     case 0x7e:
05913     case 0x7f:
05914     case 0x0f80:  /* jcc Jv */
05915     case 0x0f81:
05916     case 0x0f82:
05917     case 0x0f83:
05918     case 0x0f84:
05919     case 0x0f85:
05920     case 0x0f86:
05921     case 0x0f87:
05922     case 0x0f88:
05923     case 0x0f89:
05924     case 0x0f8a:
05925     case 0x0f8b:
05926     case 0x0f8c:
05927     case 0x0f8d:
05928     case 0x0f8e:
05929     case 0x0f8f:
05930       break;
05931 
05932     case 0x0f90:  /* setcc Gv */
05933     case 0x0f91:
05934     case 0x0f92:
05935     case 0x0f93:
05936     case 0x0f94:
05937     case 0x0f95:
05938     case 0x0f96:
05939     case 0x0f97:
05940     case 0x0f98:
05941     case 0x0f99:
05942     case 0x0f9a:
05943     case 0x0f9b:
05944     case 0x0f9c:
05945     case 0x0f9d:
05946     case 0x0f9e:
05947     case 0x0f9f:
05948       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05949       ir.ot = OT_BYTE;
05950       if (i386_record_modrm (&ir))
05951         return -1;
05952       if (ir.mod == 3)
05953         I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rex_b ? (ir.rm | ir.rex_b)
05954                                             : (ir.rm & 0x3));
05955       else
05956         {
05957           if (i386_record_lea_modrm (&ir))
05958             return -1;
05959         }
05960       break;
05961 
05962     case 0x0f40:    /* cmov Gv, Ev */
05963     case 0x0f41:
05964     case 0x0f42:
05965     case 0x0f43:
05966     case 0x0f44:
05967     case 0x0f45:
05968     case 0x0f46:
05969     case 0x0f47:
05970     case 0x0f48:
05971     case 0x0f49:
05972     case 0x0f4a:
05973     case 0x0f4b:
05974     case 0x0f4c:
05975     case 0x0f4d:
05976     case 0x0f4e:
05977     case 0x0f4f:
05978       if (i386_record_modrm (&ir))
05979         return -1;
05980       ir.reg |= rex_r;
05981       if (ir.dflag == OT_BYTE)
05982         ir.reg &= 0x3;
05983       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
05984       break;
05985 
05986       /* flags */
05987     case 0x9c:    /* pushf */
05988       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05989       if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
05990         ir.dflag = 2;
05991       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
05992         return -1;
05993       break;
05994 
05995     case 0x9d:    /* popf */
05996       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
05997       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
05998       break;
05999 
06000     case 0x9e:    /* sahf */
06001       if (ir.regmap[X86_RECORD_R8_REGNUM])
06002         {
06003           ir.addr -= 1;
06004           goto no_support;
06005         }
06006       /* FALLTHROUGH */
06007     case 0xf5:    /* cmc */
06008     case 0xf8:    /* clc */
06009     case 0xf9:    /* stc */
06010     case 0xfc:    /* cld */
06011     case 0xfd:    /* std */
06012       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06013       break;
06014 
06015     case 0x9f:    /* lahf */
06016       if (ir.regmap[X86_RECORD_R8_REGNUM])
06017         {
06018           ir.addr -= 1;
06019           goto no_support;
06020         }
06021       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06022       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
06023       break;
06024 
06025       /* bit operations */
06026     case 0x0fba:    /* bt/bts/btr/btc Gv, im */
06027       ir.ot = ir.dflag + OT_WORD;
06028       if (i386_record_modrm (&ir))
06029         return -1;
06030       if (ir.reg < 4)
06031         {
06032           ir.addr -= 2;
06033           opcode = opcode << 8 | ir.modrm;
06034           goto no_support;
06035         }
06036       if (ir.reg != 4)
06037         {
06038           if (ir.mod == 3)
06039             I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
06040           else
06041             {
06042               if (i386_record_lea_modrm (&ir))
06043                 return -1;
06044             }
06045         }
06046       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06047       break;
06048 
06049     case 0x0fa3:    /* bt Gv, Ev */
06050       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06051       break;
06052 
06053     case 0x0fab:    /* bts */
06054     case 0x0fb3:    /* btr */
06055     case 0x0fbb:    /* btc */
06056       ir.ot = ir.dflag + OT_WORD;
06057       if (i386_record_modrm (&ir))
06058         return -1;
06059       if (ir.mod == 3)
06060         I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
06061       else
06062         {
06063           uint64_t addr64;
06064           if (i386_record_lea_modrm_addr (&ir, &addr64))
06065             return -1;
06066           regcache_raw_read_unsigned (ir.regcache,
06067                                       ir.regmap[ir.reg | rex_r],
06068                                       &addr);
06069           switch (ir.dflag)
06070             {
06071             case 0:
06072               addr64 += ((int16_t) addr >> 4) << 4;
06073               break;
06074             case 1:
06075               addr64 += ((int32_t) addr >> 5) << 5;
06076               break;
06077             case 2:
06078               addr64 += ((int64_t) addr >> 6) << 6;
06079               break;
06080             }
06081           if (record_full_arch_list_add_mem (addr64, 1 << ir.ot))
06082             return -1;
06083           if (i386_record_lea_modrm (&ir))
06084             return -1;
06085         }
06086       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06087       break;
06088 
06089     case 0x0fbc:    /* bsf */
06090     case 0x0fbd:    /* bsr */
06091       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
06092       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06093       break;
06094 
06095       /* bcd */
06096     case 0x27:    /* daa */
06097     case 0x2f:    /* das */
06098     case 0x37:    /* aaa */
06099     case 0x3f:    /* aas */
06100     case 0xd4:    /* aam */
06101     case 0xd5:    /* aad */
06102       if (ir.regmap[X86_RECORD_R8_REGNUM])
06103         {
06104           ir.addr -= 1;
06105           goto no_support;
06106         }
06107       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
06108       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06109       break;
06110 
06111       /* misc */
06112     case 0x90:    /* nop */
06113       if (prefixes & PREFIX_LOCK)
06114         {
06115           ir.addr -= 1;
06116           goto no_support;
06117         }
06118       break;
06119 
06120     case 0x9b:    /* fwait */
06121       if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
06122         return -1;
06123       opcode = (uint32_t) opcode8;
06124       ir.addr++;
06125       goto reswitch;
06126       break;
06127 
06128       /* XXX */
06129     case 0xcc:    /* int3 */
06130       printf_unfiltered (_("Process record does not support instruction "
06131                            "int3.\n"));
06132       ir.addr -= 1;
06133       goto no_support;
06134       break;
06135 
06136       /* XXX */
06137     case 0xcd:    /* int */
06138       {
06139         int ret;
06140         uint8_t interrupt;
06141         if (record_read_memory (gdbarch, ir.addr, &interrupt, 1))
06142           return -1;
06143         ir.addr++;
06144         if (interrupt != 0x80
06145             || tdep->i386_intx80_record == NULL)
06146           {
06147             printf_unfiltered (_("Process record does not support "
06148                                  "instruction int 0x%02x.\n"),
06149                                interrupt);
06150             ir.addr -= 2;
06151             goto no_support;
06152           }
06153         ret = tdep->i386_intx80_record (ir.regcache);
06154         if (ret)
06155           return ret;
06156       }
06157       break;
06158 
06159       /* XXX */
06160     case 0xce:    /* into */
06161       printf_unfiltered (_("Process record does not support "
06162                            "instruction into.\n"));
06163       ir.addr -= 1;
06164       goto no_support;
06165       break;
06166 
06167     case 0xfa:    /* cli */
06168     case 0xfb:    /* sti */
06169       break;
06170 
06171     case 0x62:    /* bound */
06172       printf_unfiltered (_("Process record does not support "
06173                            "instruction bound.\n"));
06174       ir.addr -= 1;
06175       goto no_support;
06176       break;
06177 
06178     case 0x0fc8:    /* bswap reg */
06179     case 0x0fc9:
06180     case 0x0fca:
06181     case 0x0fcb:
06182     case 0x0fcc:
06183     case 0x0fcd:
06184     case 0x0fce:
06185     case 0x0fcf:
06186       I386_RECORD_FULL_ARCH_LIST_ADD_REG ((opcode & 7) | ir.rex_b);
06187       break;
06188 
06189     case 0xd6:    /* salc */
06190       if (ir.regmap[X86_RECORD_R8_REGNUM])
06191         {
06192           ir.addr -= 1;
06193           goto no_support;
06194         }
06195       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
06196       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06197       break;
06198 
06199     case 0xe0:    /* loopnz */
06200     case 0xe1:    /* loopz */
06201     case 0xe2:    /* loop */
06202     case 0xe3:    /* jecxz */
06203       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
06204       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06205       break;
06206 
06207     case 0x0f30:    /* wrmsr */
06208       printf_unfiltered (_("Process record does not support "
06209                            "instruction wrmsr.\n"));
06210       ir.addr -= 2;
06211       goto no_support;
06212       break;
06213 
06214     case 0x0f32:    /* rdmsr */
06215       printf_unfiltered (_("Process record does not support "
06216                            "instruction rdmsr.\n"));
06217       ir.addr -= 2;
06218       goto no_support;
06219       break;
06220 
06221     case 0x0f31:    /* rdtsc */
06222       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
06223       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
06224       break;
06225 
06226     case 0x0f34:    /* sysenter */
06227       {
06228         int ret;
06229         if (ir.regmap[X86_RECORD_R8_REGNUM])
06230           {
06231             ir.addr -= 2;
06232             goto no_support;
06233           }
06234         if (tdep->i386_sysenter_record == NULL)
06235           {
06236             printf_unfiltered (_("Process record does not support "
06237                                  "instruction sysenter.\n"));
06238             ir.addr -= 2;
06239             goto no_support;
06240           }
06241         ret = tdep->i386_sysenter_record (ir.regcache);
06242         if (ret)
06243           return ret;
06244       }
06245       break;
06246 
06247     case 0x0f35:    /* sysexit */
06248       printf_unfiltered (_("Process record does not support "
06249                            "instruction sysexit.\n"));
06250       ir.addr -= 2;
06251       goto no_support;
06252       break;
06253 
06254     case 0x0f05:    /* syscall */
06255       {
06256         int ret;
06257         if (tdep->i386_syscall_record == NULL)
06258           {
06259             printf_unfiltered (_("Process record does not support "
06260                                  "instruction syscall.\n"));
06261             ir.addr -= 2;
06262             goto no_support;
06263           }
06264         ret = tdep->i386_syscall_record (ir.regcache);
06265         if (ret)
06266           return ret;
06267       }
06268       break;
06269 
06270     case 0x0f07:    /* sysret */
06271       printf_unfiltered (_("Process record does not support "
06272                            "instruction sysret.\n"));
06273       ir.addr -= 2;
06274       goto no_support;
06275       break;
06276 
06277     case 0x0fa2:    /* cpuid */
06278       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
06279       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
06280       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
06281       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBX_REGNUM);
06282       break;
06283 
06284     case 0xf4:    /* hlt */
06285       printf_unfiltered (_("Process record does not support "
06286                            "instruction hlt.\n"));
06287       ir.addr -= 1;
06288       goto no_support;
06289       break;
06290 
06291     case 0x0f00:
06292       if (i386_record_modrm (&ir))
06293         return -1;
06294       switch (ir.reg)
06295         {
06296         case 0:  /* sldt */
06297         case 1:  /* str  */
06298           if (ir.mod == 3)
06299             I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
06300           else
06301             {
06302               ir.ot = OT_WORD;
06303               if (i386_record_lea_modrm (&ir))
06304                 return -1;
06305             }
06306           break;
06307         case 2:  /* lldt */
06308         case 3:  /* ltr */
06309           break;
06310         case 4:  /* verr */
06311         case 5:  /* verw */
06312           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06313           break;
06314         default:
06315           ir.addr -= 3;
06316           opcode = opcode << 8 | ir.modrm;
06317           goto no_support;
06318           break;
06319         }
06320       break;
06321 
06322     case 0x0f01:
06323       if (i386_record_modrm (&ir))
06324         return -1;
06325       switch (ir.reg)
06326         {
06327         case 0:  /* sgdt */
06328           {
06329             uint64_t addr64;
06330 
06331             if (ir.mod == 3)
06332               {
06333                 ir.addr -= 3;
06334                 opcode = opcode << 8 | ir.modrm;
06335                 goto no_support;
06336               }
06337             if (ir.override >= 0)
06338               {
06339                 if (record_full_memory_query)
06340                   {
06341                     int q;
06342 
06343                     target_terminal_ours ();
06344                     q = yquery (_("\
06345 Process record ignores the memory change of instruction at address %s\n\
06346 because it can't get the value of the segment register.\n\
06347 Do you want to stop the program?"),
06348                                 paddress (gdbarch, ir.orig_addr));
06349                     target_terminal_inferior ();
06350                     if (q)
06351                       return -1;
06352                   }
06353               }
06354             else
06355               {
06356                 if (i386_record_lea_modrm_addr (&ir, &addr64))
06357                   return -1;
06358                 if (record_full_arch_list_add_mem (addr64, 2))
06359                   return -1;
06360                 addr64 += 2;
06361                 if (ir.regmap[X86_RECORD_R8_REGNUM])
06362                   {
06363                     if (record_full_arch_list_add_mem (addr64, 8))
06364                       return -1;
06365                   }
06366                 else
06367                   {
06368                     if (record_full_arch_list_add_mem (addr64, 4))
06369                       return -1;
06370                   }
06371               }
06372           }
06373           break;
06374         case 1:
06375           if (ir.mod == 3)
06376             {
06377               switch (ir.rm)
06378                 {
06379                 case 0:  /* monitor */
06380                   break;
06381                 case 1:  /* mwait */
06382                   I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06383                   break;
06384                 default:
06385                   ir.addr -= 3;
06386                   opcode = opcode << 8 | ir.modrm;
06387                   goto no_support;
06388                   break;
06389                 }
06390             }
06391           else
06392             {
06393               /* sidt */
06394               if (ir.override >= 0)
06395                 {
06396                   if (record_full_memory_query)
06397                     {
06398                       int q;
06399 
06400                       target_terminal_ours ();
06401                       q = yquery (_("\
06402 Process record ignores the memory change of instruction at address %s\n\
06403 because it can't get the value of the segment register.\n\
06404 Do you want to stop the program?"),
06405                                   paddress (gdbarch, ir.orig_addr));
06406                       target_terminal_inferior ();
06407                       if (q)
06408                         return -1;
06409                     }
06410                 }
06411               else
06412                 {
06413                   uint64_t addr64;
06414 
06415                   if (i386_record_lea_modrm_addr (&ir, &addr64))
06416                     return -1;
06417                   if (record_full_arch_list_add_mem (addr64, 2))
06418                     return -1;
06419                   addr64 += 2;
06420                   if (ir.regmap[X86_RECORD_R8_REGNUM])
06421                     {
06422                       if (record_full_arch_list_add_mem (addr64, 8))
06423                         return -1;
06424                     }
06425                   else
06426                     {
06427                       if (record_full_arch_list_add_mem (addr64, 4))
06428                         return -1;
06429                     }
06430                 }
06431             }
06432           break;
06433         case 2:  /* lgdt */
06434           if (ir.mod == 3)
06435             {
06436               /* xgetbv */
06437               if (ir.rm == 0)
06438                 {
06439                   I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
06440                   I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
06441                   break;
06442                 }
06443               /* xsetbv */
06444               else if (ir.rm == 1)
06445                 break;
06446             }
06447         case 3:  /* lidt */
06448           if (ir.mod == 3)
06449             {
06450               ir.addr -= 3;
06451               opcode = opcode << 8 | ir.modrm;
06452               goto no_support;
06453             }
06454           break;
06455         case 4:  /* smsw */
06456           if (ir.mod == 3)
06457             {
06458               if (record_full_arch_list_add_reg (ir.regcache, ir.rm | ir.rex_b))
06459                 return -1;
06460             }
06461           else
06462             {
06463               ir.ot = OT_WORD;
06464               if (i386_record_lea_modrm (&ir))
06465                 return -1;
06466             }
06467           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06468           break;
06469         case 6:  /* lmsw */
06470           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06471           break;
06472         case 7:  /* invlpg */
06473           if (ir.mod == 3)
06474             {
06475               if (ir.rm == 0 && ir.regmap[X86_RECORD_R8_REGNUM])
06476                 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_GS_REGNUM);
06477               else
06478                 {
06479                   ir.addr -= 3;
06480                   opcode = opcode << 8 | ir.modrm;
06481                   goto no_support;
06482                 }
06483             }
06484           else
06485             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06486           break;
06487         default:
06488           ir.addr -= 3;
06489           opcode = opcode << 8 | ir.modrm;
06490           goto no_support;
06491           break;
06492         }
06493       break;
06494 
06495     case 0x0f08:    /* invd */
06496     case 0x0f09:    /* wbinvd */
06497       break;
06498 
06499     case 0x63:    /* arpl */
06500       if (i386_record_modrm (&ir))
06501         return -1;
06502       if (ir.mod == 3 || ir.regmap[X86_RECORD_R8_REGNUM])
06503         {
06504           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.regmap[X86_RECORD_R8_REGNUM]
06505                                               ? (ir.reg | rex_r) : ir.rm);
06506         }
06507       else
06508         {
06509           ir.ot = ir.dflag ? OT_LONG : OT_WORD;
06510           if (i386_record_lea_modrm (&ir))
06511             return -1;
06512         }
06513       if (!ir.regmap[X86_RECORD_R8_REGNUM])
06514         I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06515       break;
06516 
06517     case 0x0f02:    /* lar */
06518     case 0x0f03:    /* lsl */
06519       if (i386_record_modrm (&ir))
06520         return -1;
06521       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
06522       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06523       break;
06524 
06525     case 0x0f18:
06526       if (i386_record_modrm (&ir))
06527         return -1;
06528       if (ir.mod == 3 && ir.reg == 3)
06529         {
06530           ir.addr -= 3;
06531           opcode = opcode << 8 | ir.modrm;
06532           goto no_support;
06533         }
06534       break;
06535 
06536     case 0x0f19:
06537     case 0x0f1a:
06538     case 0x0f1b:
06539     case 0x0f1c:
06540     case 0x0f1d:
06541     case 0x0f1e:
06542     case 0x0f1f:
06543       /* nop (multi byte) */
06544       break;
06545 
06546     case 0x0f20:    /* mov reg, crN */
06547     case 0x0f22:    /* mov crN, reg */
06548       if (i386_record_modrm (&ir))
06549         return -1;
06550       if ((ir.modrm & 0xc0) != 0xc0)
06551         {
06552           ir.addr -= 3;
06553           opcode = opcode << 8 | ir.modrm;
06554           goto no_support;
06555         }
06556       switch (ir.reg)
06557         {
06558         case 0:
06559         case 2:
06560         case 3:
06561         case 4:
06562         case 8:
06563           if (opcode & 2)
06564             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06565           else
06566             I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
06567           break;
06568         default:
06569           ir.addr -= 3;
06570           opcode = opcode << 8 | ir.modrm;
06571           goto no_support;
06572           break;
06573         }
06574       break;
06575 
06576     case 0x0f21:    /* mov reg, drN */
06577     case 0x0f23:    /* mov drN, reg */
06578       if (i386_record_modrm (&ir))
06579         return -1;
06580       if ((ir.modrm & 0xc0) != 0xc0 || ir.reg == 4
06581           || ir.reg == 5 || ir.reg >= 8)
06582         {
06583           ir.addr -= 3;
06584           opcode = opcode << 8 | ir.modrm;
06585           goto no_support;
06586         }
06587       if (opcode & 2)
06588         I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06589       else
06590         I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
06591       break;
06592 
06593     case 0x0f06:    /* clts */
06594       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06595       break;
06596 
06597     /* MMX 3DNow! SSE SSE2 SSE3 SSSE3 SSE4 */
06598 
06599     case 0x0f0d:    /* 3DNow! prefetch */
06600       break;
06601 
06602     case 0x0f0e:    /* 3DNow! femms */
06603     case 0x0f77:    /* emms */
06604       if (i386_fpc_regnum_p (gdbarch, I387_FTAG_REGNUM(tdep)))
06605         goto no_support;
06606       record_full_arch_list_add_reg (ir.regcache, I387_FTAG_REGNUM(tdep));
06607       break;
06608 
06609     case 0x0f0f:    /* 3DNow! data */
06610       if (i386_record_modrm (&ir))
06611         return -1;
06612       if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
06613         return -1;
06614       ir.addr++;
06615       switch (opcode8)
06616         {
06617         case 0x0c:    /* 3DNow! pi2fw */
06618         case 0x0d:    /* 3DNow! pi2fd */
06619         case 0x1c:    /* 3DNow! pf2iw */
06620         case 0x1d:    /* 3DNow! pf2id */
06621         case 0x8a:    /* 3DNow! pfnacc */
06622         case 0x8e:    /* 3DNow! pfpnacc */
06623         case 0x90:    /* 3DNow! pfcmpge */
06624         case 0x94:    /* 3DNow! pfmin */
06625         case 0x96:    /* 3DNow! pfrcp */
06626         case 0x97:    /* 3DNow! pfrsqrt */
06627         case 0x9a:    /* 3DNow! pfsub */
06628         case 0x9e:    /* 3DNow! pfadd */
06629         case 0xa0:    /* 3DNow! pfcmpgt */
06630         case 0xa4:    /* 3DNow! pfmax */
06631         case 0xa6:    /* 3DNow! pfrcpit1 */
06632         case 0xa7:    /* 3DNow! pfrsqit1 */
06633         case 0xaa:    /* 3DNow! pfsubr */
06634         case 0xae:    /* 3DNow! pfacc */
06635         case 0xb0:    /* 3DNow! pfcmpeq */
06636         case 0xb4:    /* 3DNow! pfmul */
06637         case 0xb6:    /* 3DNow! pfrcpit2 */
06638         case 0xb7:    /* 3DNow! pmulhrw */
06639         case 0xbb:    /* 3DNow! pswapd */
06640         case 0xbf:    /* 3DNow! pavgusb */
06641           if (!i386_mmx_regnum_p (gdbarch, I387_MM0_REGNUM (tdep) + ir.reg))
06642             goto no_support_3dnow_data;
06643           record_full_arch_list_add_reg (ir.regcache, ir.reg);
06644           break;
06645 
06646         default:
06647 no_support_3dnow_data:
06648           opcode = (opcode << 8) | opcode8;
06649           goto no_support;
06650           break;
06651         }
06652       break;
06653 
06654     case 0x0faa:    /* rsm */
06655       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06656       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
06657       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
06658       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
06659       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBX_REGNUM);
06660       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
06661       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBP_REGNUM);
06662       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
06663       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
06664       break;
06665 
06666     case 0x0fae:
06667       if (i386_record_modrm (&ir))
06668         return -1;
06669       switch(ir.reg)
06670         {
06671         case 0:    /* fxsave */
06672           {
06673             uint64_t tmpu64;
06674 
06675             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06676             if (i386_record_lea_modrm_addr (&ir, &tmpu64))
06677               return -1;
06678             if (record_full_arch_list_add_mem (tmpu64, 512))
06679               return -1;
06680           }
06681           break;
06682 
06683         case 1:    /* fxrstor */
06684           {
06685             int i;
06686 
06687             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
06688 
06689             for (i = I387_MM0_REGNUM (tdep);
06690                  i386_mmx_regnum_p (gdbarch, i); i++)
06691               record_full_arch_list_add_reg (ir.regcache, i);
06692 
06693             for (i = I387_XMM0_REGNUM (tdep);
06694                  i386_xmm_regnum_p (gdbarch, i); i++)
06695               record_full_arch_list_add_reg (ir.regcache, i);
06696 
06697             if (i386_mxcsr_regnum_p (gdbarch, I387_MXCSR_REGNUM(tdep)))
06698               record_full_arch_list_add_reg (ir.regcache,
06699                                              I387_MXCSR_REGNUM(tdep));
06700 
06701             for (i = I387_ST0_REGNUM (tdep);
06702                  i386_fp_regnum_p (gdbarch, i); i++)
06703               record_full_arch_list_add_reg (ir.regcache, i);
06704 
06705             for (i = I387_FCTRL_REGNUM (tdep);
06706                  i386_fpc_regnum_p (gdbarch, i); i++)
06707               record_full_arch_list_add_reg (ir.regcache, i);
06708           }
06709           break;
06710 
06711         case 2:    /* ldmxcsr */
06712           if (!i386_mxcsr_regnum_p (gdbarch, I387_MXCSR_REGNUM(tdep)))
06713             goto no_support;
06714           record_full_arch_list_add_reg (ir.regcache, I387_MXCSR_REGNUM(tdep));
06715           break;
06716 
06717         case 3:    /* stmxcsr */
06718           ir.ot = OT_LONG;
06719           if (i386_record_lea_modrm (&ir))
06720             return -1;
06721           break;
06722 
06723         case 5:    /* lfence */
06724         case 6:    /* mfence */
06725         case 7:    /* sfence clflush */
06726           break;
06727 
06728         default:
06729           opcode = (opcode << 8) | ir.modrm;
06730           goto no_support;
06731           break;
06732         }
06733       break;
06734 
06735     case 0x0fc3:    /* movnti */
06736       ir.ot = (ir.dflag == 2) ? OT_QUAD : OT_LONG;
06737       if (i386_record_modrm (&ir))
06738         return -1;
06739       if (ir.mod == 3)
06740         goto no_support;
06741       ir.reg |= rex_r;
06742       if (i386_record_lea_modrm (&ir))
06743         return -1;
06744       break;
06745 
06746     /* Add prefix to opcode.  */
06747     case 0x0f10:
06748     case 0x0f11:
06749     case 0x0f12:
06750     case 0x0f13:
06751     case 0x0f14:
06752     case 0x0f15:
06753     case 0x0f16:
06754     case 0x0f17:
06755     case 0x0f28:
06756     case 0x0f29:
06757     case 0x0f2a:
06758     case 0x0f2b:
06759     case 0x0f2c:
06760     case 0x0f2d:
06761     case 0x0f2e:
06762     case 0x0f2f:
06763     case 0x0f38:
06764     case 0x0f39:
06765     case 0x0f3a:
06766     case 0x0f50:
06767     case 0x0f51:
06768     case 0x0f52:
06769     case 0x0f53:
06770     case 0x0f54:
06771     case 0x0f55:
06772     case 0x0f56:
06773     case 0x0f57:
06774     case 0x0f58:
06775     case 0x0f59:
06776     case 0x0f5a:
06777     case 0x0f5b:
06778     case 0x0f5c:
06779     case 0x0f5d:
06780     case 0x0f5e:
06781     case 0x0f5f:
06782     case 0x0f60:
06783     case 0x0f61:
06784     case 0x0f62:
06785     case 0x0f63:
06786     case 0x0f64:
06787     case 0x0f65:
06788     case 0x0f66:
06789     case 0x0f67:
06790     case 0x0f68:
06791     case 0x0f69:
06792     case 0x0f6a:
06793     case 0x0f6b:
06794     case 0x0f6c:
06795     case 0x0f6d:
06796     case 0x0f6e:
06797     case 0x0f6f:
06798     case 0x0f70:
06799     case 0x0f71:
06800     case 0x0f72:
06801     case 0x0f73:
06802     case 0x0f74:
06803     case 0x0f75:
06804     case 0x0f76:
06805     case 0x0f7c:
06806     case 0x0f7d:
06807     case 0x0f7e:
06808     case 0x0f7f:
06809     case 0x0fb8:
06810     case 0x0fc2:
06811     case 0x0fc4:
06812     case 0x0fc5:
06813     case 0x0fc6:
06814     case 0x0fd0:
06815     case 0x0fd1:
06816     case 0x0fd2:
06817     case 0x0fd3:
06818     case 0x0fd4:
06819     case 0x0fd5:
06820     case 0x0fd6:
06821     case 0x0fd7:
06822     case 0x0fd8:
06823     case 0x0fd9:
06824     case 0x0fda:
06825     case 0x0fdb:
06826     case 0x0fdc:
06827     case 0x0fdd:
06828     case 0x0fde:
06829     case 0x0fdf:
06830     case 0x0fe0:
06831     case 0x0fe1:
06832     case 0x0fe2:
06833     case 0x0fe3:
06834     case 0x0fe4:
06835     case 0x0fe5:
06836     case 0x0fe6:
06837     case 0x0fe7:
06838     case 0x0fe8:
06839     case 0x0fe9:
06840     case 0x0fea:
06841     case 0x0feb:
06842     case 0x0fec:
06843     case 0x0fed:
06844     case 0x0fee:
06845     case 0x0fef:
06846     case 0x0ff0:
06847     case 0x0ff1:
06848     case 0x0ff2:
06849     case 0x0ff3:
06850     case 0x0ff4:
06851     case 0x0ff5:
06852     case 0x0ff6:
06853     case 0x0ff7:
06854     case 0x0ff8:
06855     case 0x0ff9:
06856     case 0x0ffa:
06857     case 0x0ffb:
06858     case 0x0ffc:
06859     case 0x0ffd:
06860     case 0x0ffe:
06861       switch (prefixes)
06862         {
06863         case PREFIX_REPNZ:
06864           opcode |= 0xf20000;
06865           break;
06866         case PREFIX_DATA:
06867           opcode |= 0x660000;
06868           break;
06869         case PREFIX_REPZ:
06870           opcode |= 0xf30000;
06871           break;
06872         }
06873 reswitch_prefix_add:
06874       switch (opcode)
06875         {
06876         case 0x0f38:
06877         case 0x660f38:
06878         case 0xf20f38:
06879         case 0x0f3a:
06880         case 0x660f3a:
06881           if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
06882             return -1;
06883           ir.addr++;
06884           opcode = (uint32_t) opcode8 | opcode << 8;
06885           goto reswitch_prefix_add;
06886           break;
06887 
06888         case 0x0f10:        /* movups */
06889         case 0x660f10:      /* movupd */
06890         case 0xf30f10:      /* movss */
06891         case 0xf20f10:      /* movsd */
06892         case 0x0f12:        /* movlps */
06893         case 0x660f12:      /* movlpd */
06894         case 0xf30f12:      /* movsldup */
06895         case 0xf20f12:      /* movddup */
06896         case 0x0f14:        /* unpcklps */
06897         case 0x660f14:      /* unpcklpd */
06898         case 0x0f15:        /* unpckhps */
06899         case 0x660f15:      /* unpckhpd */
06900         case 0x0f16:        /* movhps */
06901         case 0x660f16:      /* movhpd */
06902         case 0xf30f16:      /* movshdup */
06903         case 0x0f28:        /* movaps */
06904         case 0x660f28:      /* movapd */
06905         case 0x0f2a:        /* cvtpi2ps */
06906         case 0x660f2a:      /* cvtpi2pd */
06907         case 0xf30f2a:      /* cvtsi2ss */
06908         case 0xf20f2a:      /* cvtsi2sd */
06909         case 0x0f2c:        /* cvttps2pi */
06910         case 0x660f2c:      /* cvttpd2pi */
06911         case 0x0f2d:        /* cvtps2pi */
06912         case 0x660f2d:      /* cvtpd2pi */
06913         case 0x660f3800:    /* pshufb */
06914         case 0x660f3801:    /* phaddw */
06915         case 0x660f3802:    /* phaddd */
06916         case 0x660f3803:    /* phaddsw */
06917         case 0x660f3804:    /* pmaddubsw */
06918         case 0x660f3805:    /* phsubw */
06919         case 0x660f3806:    /* phsubd */
06920         case 0x660f3807:    /* phsubsw */
06921         case 0x660f3808:    /* psignb */
06922         case 0x660f3809:    /* psignw */
06923         case 0x660f380a:    /* psignd */
06924         case 0x660f380b:    /* pmulhrsw */
06925         case 0x660f3810:    /* pblendvb */
06926         case 0x660f3814:    /* blendvps */
06927         case 0x660f3815:    /* blendvpd */
06928         case 0x660f381c:    /* pabsb */
06929         case 0x660f381d:    /* pabsw */
06930         case 0x660f381e:    /* pabsd */
06931         case 0x660f3820:    /* pmovsxbw */
06932         case 0x660f3821:    /* pmovsxbd */
06933         case 0x660f3822:    /* pmovsxbq */
06934         case 0x660f3823:    /* pmovsxwd */
06935         case 0x660f3824:    /* pmovsxwq */
06936         case 0x660f3825:    /* pmovsxdq */
06937         case 0x660f3828:    /* pmuldq */
06938         case 0x660f3829:    /* pcmpeqq */
06939         case 0x660f382a:    /* movntdqa */
06940         case 0x660f3a08:    /* roundps */
06941         case 0x660f3a09:    /* roundpd */
06942         case 0x660f3a0a:    /* roundss */
06943         case 0x660f3a0b:    /* roundsd */
06944         case 0x660f3a0c:    /* blendps */
06945         case 0x660f3a0d:    /* blendpd */
06946         case 0x660f3a0e:    /* pblendw */
06947         case 0x660f3a0f:    /* palignr */
06948         case 0x660f3a20:    /* pinsrb */
06949         case 0x660f3a21:    /* insertps */
06950         case 0x660f3a22:    /* pinsrd pinsrq */
06951         case 0x660f3a40:    /* dpps */
06952         case 0x660f3a41:    /* dppd */
06953         case 0x660f3a42:    /* mpsadbw */
06954         case 0x660f3a60:    /* pcmpestrm */
06955         case 0x660f3a61:    /* pcmpestri */
06956         case 0x660f3a62:    /* pcmpistrm */
06957         case 0x660f3a63:    /* pcmpistri */
06958         case 0x0f51:        /* sqrtps */
06959         case 0x660f51:      /* sqrtpd */
06960         case 0xf20f51:      /* sqrtsd */
06961         case 0xf30f51:      /* sqrtss */
06962         case 0x0f52:        /* rsqrtps */
06963         case 0xf30f52:      /* rsqrtss */
06964         case 0x0f53:        /* rcpps */
06965         case 0xf30f53:      /* rcpss */
06966         case 0x0f54:        /* andps */
06967         case 0x660f54:      /* andpd */
06968         case 0x0f55:        /* andnps */
06969         case 0x660f55:      /* andnpd */
06970         case 0x0f56:        /* orps */
06971         case 0x660f56:      /* orpd */
06972         case 0x0f57:        /* xorps */
06973         case 0x660f57:      /* xorpd */
06974         case 0x0f58:        /* addps */
06975         case 0x660f58:      /* addpd */
06976         case 0xf20f58:      /* addsd */
06977         case 0xf30f58:      /* addss */
06978         case 0x0f59:        /* mulps */
06979         case 0x660f59:      /* mulpd */
06980         case 0xf20f59:      /* mulsd */
06981         case 0xf30f59:      /* mulss */
06982         case 0x0f5a:        /* cvtps2pd */
06983         case 0x660f5a:      /* cvtpd2ps */
06984         case 0xf20f5a:      /* cvtsd2ss */
06985         case 0xf30f5a:      /* cvtss2sd */
06986         case 0x0f5b:        /* cvtdq2ps */
06987         case 0x660f5b:      /* cvtps2dq */
06988         case 0xf30f5b:      /* cvttps2dq */
06989         case 0x0f5c:        /* subps */
06990         case 0x660f5c:      /* subpd */
06991         case 0xf20f5c:      /* subsd */
06992         case 0xf30f5c:      /* subss */
06993         case 0x0f5d:        /* minps */
06994         case 0x660f5d:      /* minpd */
06995         case 0xf20f5d:      /* minsd */
06996         case 0xf30f5d:      /* minss */
06997         case 0x0f5e:        /* divps */
06998         case 0x660f5e:      /* divpd */
06999         case 0xf20f5e:      /* divsd */
07000         case 0xf30f5e:      /* divss */
07001         case 0x0f5f:        /* maxps */
07002         case 0x660f5f:      /* maxpd */
07003         case 0xf20f5f:      /* maxsd */
07004         case 0xf30f5f:      /* maxss */
07005         case 0x660f60:      /* punpcklbw */
07006         case 0x660f61:      /* punpcklwd */
07007         case 0x660f62:      /* punpckldq */
07008         case 0x660f63:      /* packsswb */
07009         case 0x660f64:      /* pcmpgtb */
07010         case 0x660f65:      /* pcmpgtw */
07011         case 0x660f66:      /* pcmpgtd */
07012         case 0x660f67:      /* packuswb */
07013         case 0x660f68:      /* punpckhbw */
07014         case 0x660f69:      /* punpckhwd */
07015         case 0x660f6a:      /* punpckhdq */
07016         case 0x660f6b:      /* packssdw */
07017         case 0x660f6c:      /* punpcklqdq */
07018         case 0x660f6d:      /* punpckhqdq */
07019         case 0x660f6e:      /* movd */
07020         case 0x660f6f:      /* movdqa */
07021         case 0xf30f6f:      /* movdqu */
07022         case 0x660f70:      /* pshufd */
07023         case 0xf20f70:      /* pshuflw */
07024         case 0xf30f70:      /* pshufhw */
07025         case 0x660f74:      /* pcmpeqb */
07026         case 0x660f75:      /* pcmpeqw */
07027         case 0x660f76:      /* pcmpeqd */
07028         case 0x660f7c:      /* haddpd */
07029         case 0xf20f7c:      /* haddps */
07030         case 0x660f7d:      /* hsubpd */
07031         case 0xf20f7d:      /* hsubps */
07032         case 0xf30f7e:      /* movq */
07033         case 0x0fc2:        /* cmpps */
07034         case 0x660fc2:      /* cmppd */
07035         case 0xf20fc2:      /* cmpsd */
07036         case 0xf30fc2:      /* cmpss */
07037         case 0x660fc4:      /* pinsrw */
07038         case 0x0fc6:        /* shufps */
07039         case 0x660fc6:      /* shufpd */
07040         case 0x660fd0:      /* addsubpd */
07041         case 0xf20fd0:      /* addsubps */
07042         case 0x660fd1:      /* psrlw */
07043         case 0x660fd2:      /* psrld */
07044         case 0x660fd3:      /* psrlq */
07045         case 0x660fd4:      /* paddq */
07046         case 0x660fd5:      /* pmullw */
07047         case 0xf30fd6:      /* movq2dq */
07048         case 0x660fd8:      /* psubusb */
07049         case 0x660fd9:      /* psubusw */
07050         case 0x660fda:      /* pminub */
07051         case 0x660fdb:      /* pand */
07052         case 0x660fdc:      /* paddusb */
07053         case 0x660fdd:      /* paddusw */
07054         case 0x660fde:      /* pmaxub */
07055         case 0x660fdf:      /* pandn */
07056         case 0x660fe0:      /* pavgb */
07057         case 0x660fe1:      /* psraw */
07058         case 0x660fe2:      /* psrad */
07059         case 0x660fe3:      /* pavgw */
07060         case 0x660fe4:      /* pmulhuw */
07061         case 0x660fe5:      /* pmulhw */
07062         case 0x660fe6:      /* cvttpd2dq */
07063         case 0xf20fe6:      /* cvtpd2dq */
07064         case 0xf30fe6:      /* cvtdq2pd */
07065         case 0x660fe8:      /* psubsb */
07066         case 0x660fe9:      /* psubsw */
07067         case 0x660fea:      /* pminsw */
07068         case 0x660feb:      /* por */
07069         case 0x660fec:      /* paddsb */
07070         case 0x660fed:      /* paddsw */
07071         case 0x660fee:      /* pmaxsw */
07072         case 0x660fef:      /* pxor */
07073         case 0xf20ff0:      /* lddqu */
07074         case 0x660ff1:      /* psllw */
07075         case 0x660ff2:      /* pslld */
07076         case 0x660ff3:      /* psllq */
07077         case 0x660ff4:      /* pmuludq */
07078         case 0x660ff5:      /* pmaddwd */
07079         case 0x660ff6:      /* psadbw */
07080         case 0x660ff8:      /* psubb */
07081         case 0x660ff9:      /* psubw */
07082         case 0x660ffa:      /* psubd */
07083         case 0x660ffb:      /* psubq */
07084         case 0x660ffc:      /* paddb */
07085         case 0x660ffd:      /* paddw */
07086         case 0x660ffe:      /* paddd */
07087           if (i386_record_modrm (&ir))
07088             return -1;
07089           ir.reg |= rex_r;
07090           if (!i386_xmm_regnum_p (gdbarch, I387_XMM0_REGNUM (tdep) + ir.reg))
07091             goto no_support;
07092           record_full_arch_list_add_reg (ir.regcache,
07093                                          I387_XMM0_REGNUM (tdep) + ir.reg);
07094           if ((opcode & 0xfffffffc) == 0x660f3a60)
07095             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
07096           break;
07097 
07098         case 0x0f11:        /* movups */
07099         case 0x660f11:      /* movupd */
07100         case 0xf30f11:      /* movss */
07101         case 0xf20f11:      /* movsd */
07102         case 0x0f13:        /* movlps */
07103         case 0x660f13:      /* movlpd */
07104         case 0x0f17:        /* movhps */
07105         case 0x660f17:      /* movhpd */
07106         case 0x0f29:        /* movaps */
07107         case 0x660f29:      /* movapd */
07108         case 0x660f3a14:    /* pextrb */
07109         case 0x660f3a15:    /* pextrw */
07110         case 0x660f3a16:    /* pextrd pextrq */
07111         case 0x660f3a17:    /* extractps */
07112         case 0x660f7f:      /* movdqa */
07113         case 0xf30f7f:      /* movdqu */
07114           if (i386_record_modrm (&ir))
07115             return -1;
07116           if (ir.mod == 3)
07117             {
07118               if (opcode == 0x0f13 || opcode == 0x660f13
07119                   || opcode == 0x0f17 || opcode == 0x660f17)
07120                 goto no_support;
07121               ir.rm |= ir.rex_b;
07122               if (!i386_xmm_regnum_p (gdbarch,
07123                                       I387_XMM0_REGNUM (tdep) + ir.rm))
07124                 goto no_support;
07125               record_full_arch_list_add_reg (ir.regcache,
07126                                              I387_XMM0_REGNUM (tdep) + ir.rm);
07127             }
07128           else
07129             {
07130               switch (opcode)
07131                 {
07132                   case 0x660f3a14:
07133                     ir.ot = OT_BYTE;
07134                     break;
07135                   case 0x660f3a15:
07136                     ir.ot = OT_WORD;
07137                     break;
07138                   case 0x660f3a16:
07139                     ir.ot = OT_LONG;
07140                     break;
07141                   case 0x660f3a17:
07142                     ir.ot = OT_QUAD;
07143                     break;
07144                   default:
07145                     ir.ot = OT_DQUAD;
07146                     break;
07147                 }
07148               if (i386_record_lea_modrm (&ir))
07149                 return -1;
07150             }
07151           break;
07152 
07153         case 0x0f2b:      /* movntps */
07154         case 0x660f2b:    /* movntpd */
07155         case 0x0fe7:      /* movntq */
07156         case 0x660fe7:    /* movntdq */
07157           if (ir.mod == 3)
07158             goto no_support;
07159           if (opcode == 0x0fe7)
07160             ir.ot = OT_QUAD;
07161           else
07162             ir.ot = OT_DQUAD;
07163           if (i386_record_lea_modrm (&ir))
07164             return -1;
07165           break;
07166 
07167         case 0xf30f2c:      /* cvttss2si */
07168         case 0xf20f2c:      /* cvttsd2si */
07169         case 0xf30f2d:      /* cvtss2si */
07170         case 0xf20f2d:      /* cvtsd2si */
07171         case 0xf20f38f0:    /* crc32 */
07172         case 0xf20f38f1:    /* crc32 */
07173         case 0x0f50:        /* movmskps */
07174         case 0x660f50:      /* movmskpd */
07175         case 0x0fc5:        /* pextrw */
07176         case 0x660fc5:      /* pextrw */
07177         case 0x0fd7:        /* pmovmskb */
07178         case 0x660fd7:      /* pmovmskb */
07179           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
07180           break;
07181 
07182         case 0x0f3800:    /* pshufb */
07183         case 0x0f3801:    /* phaddw */
07184         case 0x0f3802:    /* phaddd */
07185         case 0x0f3803:    /* phaddsw */
07186         case 0x0f3804:    /* pmaddubsw */
07187         case 0x0f3805:    /* phsubw */
07188         case 0x0f3806:    /* phsubd */
07189         case 0x0f3807:    /* phsubsw */
07190         case 0x0f3808:    /* psignb */
07191         case 0x0f3809:    /* psignw */
07192         case 0x0f380a:    /* psignd */
07193         case 0x0f380b:    /* pmulhrsw */
07194         case 0x0f381c:    /* pabsb */
07195         case 0x0f381d:    /* pabsw */
07196         case 0x0f381e:    /* pabsd */
07197         case 0x0f382b:    /* packusdw */
07198         case 0x0f3830:    /* pmovzxbw */
07199         case 0x0f3831:    /* pmovzxbd */
07200         case 0x0f3832:    /* pmovzxbq */
07201         case 0x0f3833:    /* pmovzxwd */
07202         case 0x0f3834:    /* pmovzxwq */
07203         case 0x0f3835:    /* pmovzxdq */
07204         case 0x0f3837:    /* pcmpgtq */
07205         case 0x0f3838:    /* pminsb */
07206         case 0x0f3839:    /* pminsd */
07207         case 0x0f383a:    /* pminuw */
07208         case 0x0f383b:    /* pminud */
07209         case 0x0f383c:    /* pmaxsb */
07210         case 0x0f383d:    /* pmaxsd */
07211         case 0x0f383e:    /* pmaxuw */
07212         case 0x0f383f:    /* pmaxud */
07213         case 0x0f3840:    /* pmulld */
07214         case 0x0f3841:    /* phminposuw */
07215         case 0x0f3a0f:    /* palignr */
07216         case 0x0f60:      /* punpcklbw */
07217         case 0x0f61:      /* punpcklwd */
07218         case 0x0f62:      /* punpckldq */
07219         case 0x0f63:      /* packsswb */
07220         case 0x0f64:      /* pcmpgtb */
07221         case 0x0f65:      /* pcmpgtw */
07222         case 0x0f66:      /* pcmpgtd */
07223         case 0x0f67:      /* packuswb */
07224         case 0x0f68:      /* punpckhbw */
07225         case 0x0f69:      /* punpckhwd */
07226         case 0x0f6a:      /* punpckhdq */
07227         case 0x0f6b:      /* packssdw */
07228         case 0x0f6e:      /* movd */
07229         case 0x0f6f:      /* movq */
07230         case 0x0f70:      /* pshufw */
07231         case 0x0f74:      /* pcmpeqb */
07232         case 0x0f75:      /* pcmpeqw */
07233         case 0x0f76:      /* pcmpeqd */
07234         case 0x0fc4:      /* pinsrw */
07235         case 0x0fd1:      /* psrlw */
07236         case 0x0fd2:      /* psrld */
07237         case 0x0fd3:      /* psrlq */
07238         case 0x0fd4:      /* paddq */
07239         case 0x0fd5:      /* pmullw */
07240         case 0xf20fd6:    /* movdq2q */
07241         case 0x0fd8:      /* psubusb */
07242         case 0x0fd9:      /* psubusw */
07243         case 0x0fda:      /* pminub */
07244         case 0x0fdb:      /* pand */
07245         case 0x0fdc:      /* paddusb */
07246         case 0x0fdd:      /* paddusw */
07247         case 0x0fde:      /* pmaxub */
07248         case 0x0fdf:      /* pandn */
07249         case 0x0fe0:      /* pavgb */
07250         case 0x0fe1:      /* psraw */
07251         case 0x0fe2:      /* psrad */
07252         case 0x0fe3:      /* pavgw */
07253         case 0x0fe4:      /* pmulhuw */
07254         case 0x0fe5:      /* pmulhw */
07255         case 0x0fe8:      /* psubsb */
07256         case 0x0fe9:      /* psubsw */
07257         case 0x0fea:      /* pminsw */
07258         case 0x0feb:      /* por */
07259         case 0x0fec:      /* paddsb */
07260         case 0x0fed:      /* paddsw */
07261         case 0x0fee:      /* pmaxsw */
07262         case 0x0fef:      /* pxor */
07263         case 0x0ff1:      /* psllw */
07264         case 0x0ff2:      /* pslld */
07265         case 0x0ff3:      /* psllq */
07266         case 0x0ff4:      /* pmuludq */
07267         case 0x0ff5:      /* pmaddwd */
07268         case 0x0ff6:      /* psadbw */
07269         case 0x0ff8:      /* psubb */
07270         case 0x0ff9:      /* psubw */
07271         case 0x0ffa:      /* psubd */
07272         case 0x0ffb:      /* psubq */
07273         case 0x0ffc:      /* paddb */
07274         case 0x0ffd:      /* paddw */
07275         case 0x0ffe:      /* paddd */
07276           if (i386_record_modrm (&ir))
07277             return -1;
07278           if (!i386_mmx_regnum_p (gdbarch, I387_MM0_REGNUM (tdep) + ir.reg))
07279             goto no_support;
07280           record_full_arch_list_add_reg (ir.regcache,
07281                                          I387_MM0_REGNUM (tdep) + ir.reg);
07282           break;
07283 
07284         case 0x0f71:    /* psllw */
07285         case 0x0f72:    /* pslld */
07286         case 0x0f73:    /* psllq */
07287           if (i386_record_modrm (&ir))
07288             return -1;
07289           if (!i386_mmx_regnum_p (gdbarch, I387_MM0_REGNUM (tdep) + ir.rm))
07290             goto no_support;
07291           record_full_arch_list_add_reg (ir.regcache,
07292                                          I387_MM0_REGNUM (tdep) + ir.rm);
07293           break;
07294 
07295         case 0x660f71:    /* psllw */
07296         case 0x660f72:    /* pslld */
07297         case 0x660f73:    /* psllq */
07298           if (i386_record_modrm (&ir))
07299             return -1;
07300           ir.rm |= ir.rex_b;
07301           if (!i386_xmm_regnum_p (gdbarch, I387_XMM0_REGNUM (tdep) + ir.rm))
07302             goto no_support;
07303           record_full_arch_list_add_reg (ir.regcache,
07304                                          I387_XMM0_REGNUM (tdep) + ir.rm);
07305           break;
07306 
07307         case 0x0f7e:      /* movd */
07308         case 0x660f7e:    /* movd */
07309           if (i386_record_modrm (&ir))
07310             return -1;
07311           if (ir.mod == 3)
07312             I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
07313           else
07314             {
07315               if (ir.dflag == 2)
07316                 ir.ot = OT_QUAD;
07317               else
07318                 ir.ot = OT_LONG;
07319               if (i386_record_lea_modrm (&ir))
07320                 return -1;
07321             }
07322           break;
07323 
07324         case 0x0f7f:    /* movq */
07325           if (i386_record_modrm (&ir))
07326             return -1;
07327           if (ir.mod == 3)
07328             {
07329               if (!i386_mmx_regnum_p (gdbarch, I387_MM0_REGNUM (tdep) + ir.rm))
07330                 goto no_support;
07331               record_full_arch_list_add_reg (ir.regcache,
07332                                              I387_MM0_REGNUM (tdep) + ir.rm);
07333             }
07334           else
07335             {
07336               ir.ot = OT_QUAD;
07337               if (i386_record_lea_modrm (&ir))
07338                 return -1;
07339             }
07340           break;
07341 
07342         case 0xf30fb8:    /* popcnt */
07343           if (i386_record_modrm (&ir))
07344             return -1;
07345           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
07346           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
07347           break;
07348 
07349         case 0x660fd6:    /* movq */
07350           if (i386_record_modrm (&ir))
07351             return -1;
07352           if (ir.mod == 3)
07353             {
07354               ir.rm |= ir.rex_b;
07355               if (!i386_xmm_regnum_p (gdbarch,
07356                                       I387_XMM0_REGNUM (tdep) + ir.rm))
07357                 goto no_support;
07358               record_full_arch_list_add_reg (ir.regcache,
07359                                              I387_XMM0_REGNUM (tdep) + ir.rm);
07360             }
07361           else
07362             {
07363               ir.ot = OT_QUAD;
07364               if (i386_record_lea_modrm (&ir))
07365                 return -1;
07366             }
07367           break;
07368 
07369         case 0x660f3817:    /* ptest */
07370         case 0x0f2e:        /* ucomiss */
07371         case 0x660f2e:      /* ucomisd */
07372         case 0x0f2f:        /* comiss */
07373         case 0x660f2f:      /* comisd */
07374           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
07375           break;
07376 
07377         case 0x0ff7:    /* maskmovq */
07378           regcache_raw_read_unsigned (ir.regcache,
07379                                       ir.regmap[X86_RECORD_REDI_REGNUM],
07380                                       &addr);
07381           if (record_full_arch_list_add_mem (addr, 64))
07382             return -1;
07383           break;
07384 
07385         case 0x660ff7:    /* maskmovdqu */
07386           regcache_raw_read_unsigned (ir.regcache,
07387                                       ir.regmap[X86_RECORD_REDI_REGNUM],
07388                                       &addr);
07389           if (record_full_arch_list_add_mem (addr, 128))
07390             return -1;
07391           break;
07392 
07393         default:
07394           goto no_support;
07395           break;
07396         }
07397       break;
07398 
07399     default:
07400       goto no_support;
07401       break;
07402     }
07403 
07404   /* In the future, maybe still need to deal with need_dasm.  */
07405   I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REIP_REGNUM);
07406   if (record_full_arch_list_add_end ())
07407     return -1;
07408 
07409   return 0;
07410 
07411  no_support:
07412   printf_unfiltered (_("Process record does not support instruction 0x%02x "
07413                        "at address %s.\n"),
07414                      (unsigned int) (opcode),
07415                      paddress (gdbarch, ir.orig_addr));
07416   return -1;
07417 }
07418 
07419 static const int i386_record_regmap[] =
07420 {
07421   I386_EAX_REGNUM, I386_ECX_REGNUM, I386_EDX_REGNUM, I386_EBX_REGNUM,
07422   I386_ESP_REGNUM, I386_EBP_REGNUM, I386_ESI_REGNUM, I386_EDI_REGNUM,
07423   0, 0, 0, 0, 0, 0, 0, 0,
07424   I386_EIP_REGNUM, I386_EFLAGS_REGNUM, I386_CS_REGNUM, I386_SS_REGNUM,
07425   I386_DS_REGNUM, I386_ES_REGNUM, I386_FS_REGNUM, I386_GS_REGNUM
07426 };
07427 
07428 /* Check that the given address appears suitable for a fast
07429    tracepoint, which on x86-64 means that we need an instruction of at
07430    least 5 bytes, so that we can overwrite it with a 4-byte-offset
07431    jump and not have to worry about program jumps to an address in the
07432    middle of the tracepoint jump.  On x86, it may be possible to use
07433    4-byte jumps with a 2-byte offset to a trampoline located in the
07434    bottom 64 KiB of memory.  Returns 1 if OK, and writes a size
07435    of instruction to replace, and 0 if not, plus an explanatory
07436    string.  */
07437 
07438 static int
07439 i386_fast_tracepoint_valid_at (struct gdbarch *gdbarch,
07440                                CORE_ADDR addr, int *isize, char **msg)
07441 {
07442   int len, jumplen;
07443   static struct ui_file *gdb_null = NULL;
07444 
07445   /*  Ask the target for the minimum instruction length supported.  */
07446   jumplen = target_get_min_fast_tracepoint_insn_len ();
07447 
07448   if (jumplen < 0)
07449     {
07450       /* If the target does not support the get_min_fast_tracepoint_insn_len
07451          operation, assume that fast tracepoints will always be implemented
07452          using 4-byte relative jumps on both x86 and x86-64.  */
07453       jumplen = 5;
07454     }
07455   else if (jumplen == 0)
07456     {
07457       /* If the target does support get_min_fast_tracepoint_insn_len but
07458          returns zero, then the IPA has not loaded yet.  In this case,
07459          we optimistically assume that truncated 2-byte relative jumps
07460          will be available on x86, and compensate later if this assumption
07461          turns out to be incorrect.  On x86-64 architectures, 4-byte relative
07462          jumps will always be used.  */
07463       jumplen = (register_size (gdbarch, 0) == 8) ? 5 : 4;
07464     }
07465 
07466   /* Dummy file descriptor for the disassembler.  */
07467   if (!gdb_null)
07468     gdb_null = ui_file_new ();
07469 
07470   /* Check for fit.  */
07471   len = gdb_print_insn (gdbarch, addr, gdb_null, NULL);
07472   if (isize)
07473     *isize = len;
07474 
07475   if (len < jumplen)
07476     {
07477       /* Return a bit of target-specific detail to add to the caller's
07478          generic failure message.  */
07479       if (msg)
07480         *msg = xstrprintf (_("; instruction is only %d bytes long, "
07481                              "need at least %d bytes for the jump"),
07482                            len, jumplen);
07483       return 0;
07484     }
07485   else
07486     {
07487       if (msg)
07488         *msg = NULL;
07489       return 1;
07490     }
07491 }
07492 
07493 static int
07494 i386_validate_tdesc_p (struct gdbarch_tdep *tdep,
07495                        struct tdesc_arch_data *tdesc_data)
07496 {
07497   const struct target_desc *tdesc = tdep->tdesc;
07498   const struct tdesc_feature *feature_core;
07499   const struct tdesc_feature *feature_sse, *feature_avx;
07500   int i, num_regs, valid_p;
07501 
07502   if (! tdesc_has_registers (tdesc))
07503     return 0;
07504 
07505   /* Get core registers.  */
07506   feature_core = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.core");
07507   if (feature_core == NULL)
07508     return 0;
07509 
07510   /* Get SSE registers.  */
07511   feature_sse = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.sse");
07512 
07513   /* Try AVX registers.  */
07514   feature_avx = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx");
07515 
07516   valid_p = 1;
07517 
07518   /* The XCR0 bits.  */
07519   if (feature_avx)
07520     {
07521       /* AVX register description requires SSE register description.  */
07522       if (!feature_sse)
07523         return 0;
07524 
07525       tdep->xcr0 = I386_XSTATE_AVX_MASK;
07526 
07527       /* It may have been set by OSABI initialization function.  */
07528       if (tdep->num_ymm_regs == 0)
07529         {
07530           tdep->ymmh_register_names = i386_ymmh_names;
07531           tdep->num_ymm_regs = 8;
07532           tdep->ymm0h_regnum = I386_YMM0H_REGNUM;
07533         }
07534 
07535       for (i = 0; i < tdep->num_ymm_regs; i++)
07536         valid_p &= tdesc_numbered_register (feature_avx, tdesc_data,
07537                                             tdep->ymm0h_regnum + i,
07538                                             tdep->ymmh_register_names[i]);
07539     }
07540   else if (feature_sse)
07541     tdep->xcr0 = I386_XSTATE_SSE_MASK;
07542   else
07543     {
07544       tdep->xcr0 = I386_XSTATE_X87_MASK;
07545       tdep->num_xmm_regs = 0;
07546     }
07547 
07548   num_regs = tdep->num_core_regs;
07549   for (i = 0; i < num_regs; i++)
07550     valid_p &= tdesc_numbered_register (feature_core, tdesc_data, i,
07551                                         tdep->register_names[i]);
07552 
07553   if (feature_sse)
07554     {
07555       /* Need to include %mxcsr, so add one.  */
07556       num_regs += tdep->num_xmm_regs + 1;
07557       for (; i < num_regs; i++)
07558         valid_p &= tdesc_numbered_register (feature_sse, tdesc_data, i,
07559                                             tdep->register_names[i]);
07560     }
07561 
07562   return valid_p;
07563 }
07564 
07565 
07566 static struct gdbarch *
07567 i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
07568 {
07569   struct gdbarch_tdep *tdep;
07570   struct gdbarch *gdbarch;
07571   struct tdesc_arch_data *tdesc_data;
07572   const struct target_desc *tdesc;
07573   int mm0_regnum;
07574   int ymm0_regnum;
07575 
07576   /* If there is already a candidate, use it.  */
07577   arches = gdbarch_list_lookup_by_info (arches, &info);
07578   if (arches != NULL)
07579     return arches->gdbarch;
07580 
07581   /* Allocate space for the new architecture.  */
07582   tdep = XCALLOC (1, struct gdbarch_tdep);
07583   gdbarch = gdbarch_alloc (&info, tdep);
07584 
07585   /* General-purpose registers.  */
07586   tdep->gregset = NULL;
07587   tdep->gregset_reg_offset = NULL;
07588   tdep->gregset_num_regs = I386_NUM_GREGS;
07589   tdep->sizeof_gregset = 0;
07590 
07591   /* Floating-point registers.  */
07592   tdep->fpregset = NULL;
07593   tdep->sizeof_fpregset = I387_SIZEOF_FSAVE;
07594 
07595   tdep->xstateregset = NULL;
07596 
07597   /* The default settings include the FPU registers, the MMX registers
07598      and the SSE registers.  This can be overridden for a specific ABI
07599      by adjusting the members `st0_regnum', `mm0_regnum' and
07600      `num_xmm_regs' of `struct gdbarch_tdep', otherwise the registers
07601      will show up in the output of "info all-registers".  */
07602 
07603   tdep->st0_regnum = I386_ST0_REGNUM;
07604 
07605   /* I386_NUM_XREGS includes %mxcsr, so substract one.  */
07606   tdep->num_xmm_regs = I386_NUM_XREGS - 1;
07607 
07608   tdep->jb_pc_offset = -1;
07609   tdep->struct_return = pcc_struct_return;
07610   tdep->sigtramp_start = 0;
07611   tdep->sigtramp_end = 0;
07612   tdep->sigtramp_p = i386_sigtramp_p;
07613   tdep->sigcontext_addr = NULL;
07614   tdep->sc_reg_offset = NULL;
07615   tdep->sc_pc_offset = -1;
07616   tdep->sc_sp_offset = -1;
07617 
07618   tdep->xsave_xcr0_offset = -1;
07619 
07620   tdep->record_regmap = i386_record_regmap;
07621 
07622   set_gdbarch_long_long_align_bit (gdbarch, 32);
07623 
07624   /* The format used for `long double' on almost all i386 targets is
07625      the i387 extended floating-point format.  In fact, of all targets
07626      in the GCC 2.95 tree, only OSF/1 does it different, and insists
07627      on having a `long double' that's not `long' at all.  */
07628   set_gdbarch_long_double_format (gdbarch, floatformats_i387_ext);
07629 
07630   /* Although the i387 extended floating-point has only 80 significant
07631      bits, a `long double' actually takes up 96, probably to enforce
07632      alignment.  */
07633   set_gdbarch_long_double_bit (gdbarch, 96);
07634 
07635   /* Register numbers of various important registers.  */
07636   set_gdbarch_sp_regnum (gdbarch, I386_ESP_REGNUM); /* %esp */
07637   set_gdbarch_pc_regnum (gdbarch, I386_EIP_REGNUM); /* %eip */
07638   set_gdbarch_ps_regnum (gdbarch, I386_EFLAGS_REGNUM); /* %eflags */
07639   set_gdbarch_fp0_regnum (gdbarch, I386_ST0_REGNUM); /* %st(0) */
07640 
07641   /* NOTE: kettenis/20040418: GCC does have two possible register
07642      numbering schemes on the i386: dbx and SVR4.  These schemes
07643      differ in how they number %ebp, %esp, %eflags, and the
07644      floating-point registers, and are implemented by the arrays
07645      dbx_register_map[] and svr4_dbx_register_map in
07646      gcc/config/i386.c.  GCC also defines a third numbering scheme in
07647      gcc/config/i386.c, which it designates as the "default" register
07648      map used in 64bit mode.  This last register numbering scheme is
07649      implemented in dbx64_register_map, and is used for AMD64; see
07650      amd64-tdep.c.
07651 
07652      Currently, each GCC i386 target always uses the same register
07653      numbering scheme across all its supported debugging formats
07654      i.e. SDB (COFF), stabs and DWARF 2.  This is because
07655      gcc/sdbout.c, gcc/dbxout.c and gcc/dwarf2out.c all use the
07656      DBX_REGISTER_NUMBER macro which is defined by each target's
07657      respective config header in a manner independent of the requested
07658      output debugging format.
07659 
07660      This does not match the arrangement below, which presumes that
07661      the SDB and stabs numbering schemes differ from the DWARF and
07662      DWARF 2 ones.  The reason for this arrangement is that it is
07663      likely to get the numbering scheme for the target's
07664      default/native debug format right.  For targets where GCC is the
07665      native compiler (FreeBSD, NetBSD, OpenBSD, GNU/Linux) or for
07666      targets where the native toolchain uses a different numbering
07667      scheme for a particular debug format (stabs-in-ELF on Solaris)
07668      the defaults below will have to be overridden, like
07669      i386_elf_init_abi() does.  */
07670 
07671   /* Use the dbx register numbering scheme for stabs and COFF.  */
07672   set_gdbarch_stab_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum);
07673   set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum);
07674 
07675   /* Use the SVR4 register numbering scheme for DWARF 2.  */
07676   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
07677 
07678   /* We don't set gdbarch_stab_reg_to_regnum, since ECOFF doesn't seem to
07679      be in use on any of the supported i386 targets.  */
07680 
07681   set_gdbarch_print_float_info (gdbarch, i387_print_float_info);
07682 
07683   set_gdbarch_get_longjmp_target (gdbarch, i386_get_longjmp_target);
07684 
07685   /* Call dummy code.  */
07686   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
07687   set_gdbarch_push_dummy_code (gdbarch, i386_push_dummy_code);
07688   set_gdbarch_push_dummy_call (gdbarch, i386_push_dummy_call);
07689   set_gdbarch_frame_align (gdbarch, i386_frame_align);
07690 
07691   set_gdbarch_convert_register_p (gdbarch, i386_convert_register_p);
07692   set_gdbarch_register_to_value (gdbarch,  i386_register_to_value);
07693   set_gdbarch_value_to_register (gdbarch, i386_value_to_register);
07694 
07695   set_gdbarch_return_value (gdbarch, i386_return_value);
07696 
07697   set_gdbarch_skip_prologue (gdbarch, i386_skip_prologue);
07698 
07699   /* Stack grows downward.  */
07700   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
07701 
07702   set_gdbarch_breakpoint_from_pc (gdbarch, i386_breakpoint_from_pc);
07703   set_gdbarch_decr_pc_after_break (gdbarch, 1);
07704   set_gdbarch_max_insn_length (gdbarch, I386_MAX_INSN_LEN);
07705 
07706   set_gdbarch_frame_args_skip (gdbarch, 8);
07707 
07708   set_gdbarch_print_insn (gdbarch, i386_print_insn);
07709 
07710   set_gdbarch_dummy_id (gdbarch, i386_dummy_id);
07711 
07712   set_gdbarch_unwind_pc (gdbarch, i386_unwind_pc);
07713 
07714   /* Add the i386 register groups.  */
07715   i386_add_reggroups (gdbarch);
07716   tdep->register_reggroup_p = i386_register_reggroup_p;
07717 
07718   /* Helper for function argument information.  */
07719   set_gdbarch_fetch_pointer_argument (gdbarch, i386_fetch_pointer_argument);
07720 
07721   /* Hook the function epilogue frame unwinder.  This unwinder is
07722      appended to the list first, so that it supercedes the DWARF
07723      unwinder in function epilogues (where the DWARF unwinder
07724      currently fails).  */
07725   frame_unwind_append_unwinder (gdbarch, &i386_epilogue_frame_unwind);
07726 
07727   /* Hook in the DWARF CFI frame unwinder.  This unwinder is appended
07728      to the list before the prologue-based unwinders, so that DWARF
07729      CFI info will be used if it is available.  */
07730   dwarf2_append_unwinders (gdbarch);
07731 
07732   frame_base_set_default (gdbarch, &i386_frame_base);
07733 
07734   /* Pseudo registers may be changed by amd64_init_abi.  */
07735   set_gdbarch_pseudo_register_read_value (gdbarch,
07736                                           i386_pseudo_register_read_value);
07737   set_gdbarch_pseudo_register_write (gdbarch, i386_pseudo_register_write);
07738 
07739   set_tdesc_pseudo_register_type (gdbarch, i386_pseudo_register_type);
07740   set_tdesc_pseudo_register_name (gdbarch, i386_pseudo_register_name);
07741 
07742   /* Override the normal target description method to make the AVX
07743      upper halves anonymous.  */
07744   set_gdbarch_register_name (gdbarch, i386_register_name);
07745 
07746   /* Even though the default ABI only includes general-purpose registers,
07747      floating-point registers and the SSE registers, we have to leave a
07748      gap for the upper AVX registers.  */
07749   set_gdbarch_num_regs (gdbarch, I386_AVX_NUM_REGS);
07750 
07751   /* Get the x86 target description from INFO.  */
07752   tdesc = info.target_desc;
07753   if (! tdesc_has_registers (tdesc))
07754     tdesc = tdesc_i386;
07755   tdep->tdesc = tdesc;
07756 
07757   tdep->num_core_regs = I386_NUM_GREGS + I387_NUM_REGS;
07758   tdep->register_names = i386_register_names;
07759 
07760   /* No upper YMM registers.  */
07761   tdep->ymmh_register_names = NULL;
07762   tdep->ymm0h_regnum = -1;
07763 
07764   tdep->num_byte_regs = 8;
07765   tdep->num_word_regs = 8;
07766   tdep->num_dword_regs = 0;
07767   tdep->num_mmx_regs = 8;
07768   tdep->num_ymm_regs = 0;
07769 
07770   tdesc_data = tdesc_data_alloc ();
07771 
07772   set_gdbarch_relocate_instruction (gdbarch, i386_relocate_instruction);
07773 
07774   set_gdbarch_gen_return_address (gdbarch, i386_gen_return_address);
07775 
07776   /* Hook in ABI-specific overrides, if they have been registered.  */
07777   info.tdep_info = (void *) tdesc_data;
07778   gdbarch_init_osabi (info, gdbarch);
07779 
07780   if (!i386_validate_tdesc_p (tdep, tdesc_data))
07781     {
07782       tdesc_data_cleanup (tdesc_data);
07783       xfree (tdep);
07784       gdbarch_free (gdbarch);
07785       return NULL;
07786     }
07787 
07788   /* Wire in pseudo registers.  Number of pseudo registers may be
07789      changed.  */
07790   set_gdbarch_num_pseudo_regs (gdbarch, (tdep->num_byte_regs
07791                                          + tdep->num_word_regs
07792                                          + tdep->num_dword_regs
07793                                          + tdep->num_mmx_regs
07794                                          + tdep->num_ymm_regs));
07795 
07796   /* Target description may be changed.  */
07797   tdesc = tdep->tdesc;
07798 
07799   tdesc_use_registers (gdbarch, tdesc, tdesc_data);
07800 
07801   /* Override gdbarch_register_reggroup_p set in tdesc_use_registers.  */
07802   set_gdbarch_register_reggroup_p (gdbarch, tdep->register_reggroup_p);
07803 
07804   /* Make %al the first pseudo-register.  */
07805   tdep->al_regnum = gdbarch_num_regs (gdbarch);
07806   tdep->ax_regnum = tdep->al_regnum + tdep->num_byte_regs;
07807 
07808   ymm0_regnum = tdep->ax_regnum + tdep->num_word_regs;
07809   if (tdep->num_dword_regs)
07810     {
07811       /* Support dword pseudo-register if it hasn't been disabled.  */
07812       tdep->eax_regnum = ymm0_regnum;
07813       ymm0_regnum += tdep->num_dword_regs;
07814     }
07815   else
07816     tdep->eax_regnum = -1;
07817 
07818   mm0_regnum = ymm0_regnum;
07819   if (tdep->num_ymm_regs)
07820     {
07821       /* Support YMM pseudo-register if it is available.  */
07822       tdep->ymm0_regnum = ymm0_regnum;
07823       mm0_regnum += tdep->num_ymm_regs;
07824     }
07825   else
07826     tdep->ymm0_regnum = -1;
07827 
07828   if (tdep->num_mmx_regs != 0)
07829     {
07830       /* Support MMX pseudo-register if MMX hasn't been disabled.  */
07831       tdep->mm0_regnum = mm0_regnum;
07832     }
07833   else
07834     tdep->mm0_regnum = -1;
07835 
07836   /* Hook in the legacy prologue-based unwinders last (fallback).  */
07837   frame_unwind_append_unwinder (gdbarch, &i386_stack_tramp_frame_unwind);
07838   frame_unwind_append_unwinder (gdbarch, &i386_sigtramp_frame_unwind);
07839   frame_unwind_append_unwinder (gdbarch, &i386_frame_unwind);
07840 
07841   /* If we have a register mapping, enable the generic core file
07842      support, unless it has already been enabled.  */
07843   if (tdep->gregset_reg_offset
07844       && !gdbarch_regset_from_core_section_p (gdbarch))
07845     set_gdbarch_regset_from_core_section (gdbarch,
07846                                           i386_regset_from_core_section);
07847 
07848   set_gdbarch_skip_permanent_breakpoint (gdbarch,
07849                                          i386_skip_permanent_breakpoint);
07850 
07851   set_gdbarch_fast_tracepoint_valid_at (gdbarch,
07852                                         i386_fast_tracepoint_valid_at);
07853 
07854   return gdbarch;
07855 }
07856 
07857 static enum gdb_osabi
07858 i386_coff_osabi_sniffer (bfd *abfd)
07859 {
07860   if (strcmp (bfd_get_target (abfd), "coff-go32-exe") == 0
07861       || strcmp (bfd_get_target (abfd), "coff-go32") == 0)
07862     return GDB_OSABI_GO32;
07863 
07864   return GDB_OSABI_UNKNOWN;
07865 }
07866 
07867 
07868 /* Provide a prototype to silence -Wmissing-prototypes.  */
07869 void _initialize_i386_tdep (void);
07870 
07871 void
07872 _initialize_i386_tdep (void)
07873 {
07874   register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
07875 
07876   /* Add the variable that controls the disassembly flavor.  */
07877   add_setshow_enum_cmd ("disassembly-flavor", no_class, valid_flavors,
07878                         &disassembly_flavor, _("\
07879 Set the disassembly flavor."), _("\
07880 Show the disassembly flavor."), _("\
07881 The valid values are \"att\" and \"intel\", and the default value is \"att\"."),
07882                         NULL,
07883                         NULL, /* FIXME: i18n: */
07884                         &setlist, &showlist);
07885 
07886   /* Add the variable that controls the convention for returning
07887      structs.  */
07888   add_setshow_enum_cmd ("struct-convention", no_class, valid_conventions,
07889                         &struct_convention, _("\
07890 Set the convention for returning small structs."), _("\
07891 Show the convention for returning small structs."), _("\
07892 Valid values are \"default\", \"pcc\" and \"reg\", and the default value\n\
07893 is \"default\"."),
07894                         NULL,
07895                         NULL, /* FIXME: i18n: */
07896                         &setlist, &showlist);
07897 
07898   gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
07899                                   i386_coff_osabi_sniffer);
07900 
07901   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_SVR4,
07902                           i386_svr4_init_abi);
07903   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_GO32,
07904                           i386_go32_init_abi);
07905 
07906   /* Initialize the i386-specific register groups.  */
07907   i386_init_reggroups ();
07908 
07909   /* Initialize the standard target descriptions.  */
07910   initialize_tdesc_i386 ();
07911   initialize_tdesc_i386_mmx ();
07912   initialize_tdesc_i386_avx ();
07913 
07914   /* Tell remote stub that we support XML target description.  */
07915   register_remote_support_xml ("i386");
07916 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines