Add fast tracepoints.
[platform/upstream/binutils.git] / gdb / i386-tdep.c
1 /* Intel 386 target-dependent stuff.
2
3    Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4    1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5    2010 Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "opcode/i386.h"
24 #include "arch-utils.h"
25 #include "command.h"
26 #include "dummy-frame.h"
27 #include "dwarf2-frame.h"
28 #include "doublest.h"
29 #include "frame.h"
30 #include "frame-base.h"
31 #include "frame-unwind.h"
32 #include "inferior.h"
33 #include "gdbcmd.h"
34 #include "gdbcore.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "osabi.h"
38 #include "regcache.h"
39 #include "reggroups.h"
40 #include "regset.h"
41 #include "symfile.h"
42 #include "symtab.h"
43 #include "target.h"
44 #include "value.h"
45 #include "dis-asm.h"
46 #include "disasm.h"
47
48 #include "gdb_assert.h"
49 #include "gdb_string.h"
50
51 #include "i386-tdep.h"
52 #include "i387-tdep.h"
53
54 #include "record.h"
55 #include <stdint.h>
56
57 /* Register names.  */
58
59 static char *i386_register_names[] =
60 {
61   "eax",   "ecx",    "edx",   "ebx",
62   "esp",   "ebp",    "esi",   "edi",
63   "eip",   "eflags", "cs",    "ss",
64   "ds",    "es",     "fs",    "gs",
65   "st0",   "st1",    "st2",   "st3",
66   "st4",   "st5",    "st6",   "st7",
67   "fctrl", "fstat",  "ftag",  "fiseg",
68   "fioff", "foseg",  "fooff", "fop",
69   "xmm0",  "xmm1",   "xmm2",  "xmm3",
70   "xmm4",  "xmm5",   "xmm6",  "xmm7",
71   "mxcsr"
72 };
73
74 static const int i386_num_register_names = ARRAY_SIZE (i386_register_names);
75
76 /* Register names for MMX pseudo-registers.  */
77
78 static char *i386_mmx_names[] =
79 {
80   "mm0", "mm1", "mm2", "mm3",
81   "mm4", "mm5", "mm6", "mm7"
82 };
83
84 static const int i386_num_mmx_regs = ARRAY_SIZE (i386_mmx_names);
85
86 static int
87 i386_mmx_regnum_p (struct gdbarch *gdbarch, int regnum)
88 {
89   int mm0_regnum = gdbarch_tdep (gdbarch)->mm0_regnum;
90
91   if (mm0_regnum < 0)
92     return 0;
93
94   return (regnum >= mm0_regnum && regnum < mm0_regnum + i386_num_mmx_regs);
95 }
96
97 /* SSE register?  */
98
99 static int
100 i386_sse_regnum_p (struct gdbarch *gdbarch, int regnum)
101 {
102   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
103
104   if (I387_NUM_XMM_REGS (tdep) == 0)
105     return 0;
106
107   return (I387_XMM0_REGNUM (tdep) <= regnum
108           && regnum < I387_MXCSR_REGNUM (tdep));
109 }
110
111 static int
112 i386_mxcsr_regnum_p (struct gdbarch *gdbarch, int regnum)
113 {
114   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
115
116   if (I387_NUM_XMM_REGS (tdep) == 0)
117     return 0;
118
119   return (regnum == I387_MXCSR_REGNUM (tdep));
120 }
121
122 /* FP register?  */
123
124 int
125 i386_fp_regnum_p (struct gdbarch *gdbarch, int regnum)
126 {
127   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
128
129   if (I387_ST0_REGNUM (tdep) < 0)
130     return 0;
131
132   return (I387_ST0_REGNUM (tdep) <= regnum
133           && regnum < I387_FCTRL_REGNUM (tdep));
134 }
135
136 int
137 i386_fpc_regnum_p (struct gdbarch *gdbarch, int regnum)
138 {
139   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
140
141   if (I387_ST0_REGNUM (tdep) < 0)
142     return 0;
143
144   return (I387_FCTRL_REGNUM (tdep) <= regnum 
145           && regnum < I387_XMM0_REGNUM (tdep));
146 }
147
148 /* Return the name of register REGNUM.  */
149
150 const char *
151 i386_register_name (struct gdbarch *gdbarch, int regnum)
152 {
153   if (i386_mmx_regnum_p (gdbarch, regnum))
154     return i386_mmx_names[regnum - I387_MM0_REGNUM (gdbarch_tdep (gdbarch))];
155
156   if (regnum >= 0 && regnum < i386_num_register_names)
157     return i386_register_names[regnum];
158
159   return NULL;
160 }
161
162 /* Convert a dbx register number REG to the appropriate register
163    number used by GDB.  */
164
165 static int
166 i386_dbx_reg_to_regnum (struct gdbarch *gdbarch, int reg)
167 {
168   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
169
170   /* This implements what GCC calls the "default" register map
171      (dbx_register_map[]).  */
172
173   if (reg >= 0 && reg <= 7)
174     {
175       /* General-purpose registers.  The debug info calls %ebp
176          register 4, and %esp register 5.  */
177       if (reg == 4)
178         return 5;
179       else if (reg == 5)
180         return 4;
181       else return reg;
182     }
183   else if (reg >= 12 && reg <= 19)
184     {
185       /* Floating-point registers.  */
186       return reg - 12 + I387_ST0_REGNUM (tdep);
187     }
188   else if (reg >= 21 && reg <= 28)
189     {
190       /* SSE registers.  */
191       return reg - 21 + I387_XMM0_REGNUM (tdep);
192     }
193   else if (reg >= 29 && reg <= 36)
194     {
195       /* MMX registers.  */
196       return reg - 29 + I387_MM0_REGNUM (tdep);
197     }
198
199   /* This will hopefully provoke a warning.  */
200   return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
201 }
202
203 /* Convert SVR4 register number REG to the appropriate register number
204    used by GDB.  */
205
206 static int
207 i386_svr4_reg_to_regnum (struct gdbarch *gdbarch, int reg)
208 {
209   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
210
211   /* This implements the GCC register map that tries to be compatible
212      with the SVR4 C compiler for DWARF (svr4_dbx_register_map[]).  */
213
214   /* The SVR4 register numbering includes %eip and %eflags, and
215      numbers the floating point registers differently.  */
216   if (reg >= 0 && reg <= 9)
217     {
218       /* General-purpose registers.  */
219       return reg;
220     }
221   else if (reg >= 11 && reg <= 18)
222     {
223       /* Floating-point registers.  */
224       return reg - 11 + I387_ST0_REGNUM (tdep);
225     }
226   else if (reg >= 21 && reg <= 36)
227     {
228       /* The SSE and MMX registers have the same numbers as with dbx.  */
229       return i386_dbx_reg_to_regnum (gdbarch, reg);
230     }
231
232   switch (reg)
233     {
234     case 37: return I387_FCTRL_REGNUM (tdep);
235     case 38: return I387_FSTAT_REGNUM (tdep);
236     case 39: return I387_MXCSR_REGNUM (tdep);
237     case 40: return I386_ES_REGNUM;
238     case 41: return I386_CS_REGNUM;
239     case 42: return I386_SS_REGNUM;
240     case 43: return I386_DS_REGNUM;
241     case 44: return I386_FS_REGNUM;
242     case 45: return I386_GS_REGNUM;
243     }
244
245   /* This will hopefully provoke a warning.  */
246   return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
247 }
248
249 \f
250
251 /* This is the variable that is set with "set disassembly-flavor", and
252    its legitimate values.  */
253 static const char att_flavor[] = "att";
254 static const char intel_flavor[] = "intel";
255 static const char *valid_flavors[] =
256 {
257   att_flavor,
258   intel_flavor,
259   NULL
260 };
261 static const char *disassembly_flavor = att_flavor;
262 \f
263
264 /* Use the program counter to determine the contents and size of a
265    breakpoint instruction.  Return a pointer to a string of bytes that
266    encode a breakpoint instruction, store the length of the string in
267    *LEN and optionally adjust *PC to point to the correct memory
268    location for inserting the breakpoint.
269
270    On the i386 we have a single breakpoint that fits in a single byte
271    and can be inserted anywhere.
272
273    This function is 64-bit safe.  */
274
275 static const gdb_byte *
276 i386_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
277 {
278   static gdb_byte break_insn[] = { 0xcc }; /* int 3 */
279
280   *len = sizeof (break_insn);
281   return break_insn;
282 }
283 \f
284 /* Displaced instruction handling.  */
285
286 /* Skip the legacy instruction prefixes in INSN.
287    Not all prefixes are valid for any particular insn
288    but we needn't care, the insn will fault if it's invalid.
289    The result is a pointer to the first opcode byte,
290    or NULL if we run off the end of the buffer.  */
291
292 static gdb_byte *
293 i386_skip_prefixes (gdb_byte *insn, size_t max_len)
294 {
295   gdb_byte *end = insn + max_len;
296
297   while (insn < end)
298     {
299       switch (*insn)
300         {
301         case DATA_PREFIX_OPCODE:
302         case ADDR_PREFIX_OPCODE:
303         case CS_PREFIX_OPCODE:
304         case DS_PREFIX_OPCODE:
305         case ES_PREFIX_OPCODE:
306         case FS_PREFIX_OPCODE:
307         case GS_PREFIX_OPCODE:
308         case SS_PREFIX_OPCODE:
309         case LOCK_PREFIX_OPCODE:
310         case REPE_PREFIX_OPCODE:
311         case REPNE_PREFIX_OPCODE:
312           ++insn;
313           continue;
314         default:
315           return insn;
316         }
317     }
318
319   return NULL;
320 }
321
322 static int
323 i386_absolute_jmp_p (const gdb_byte *insn)
324 {
325   /* jmp far (absolute address in operand) */
326   if (insn[0] == 0xea)
327     return 1;
328
329   if (insn[0] == 0xff)
330     {
331       /* jump near, absolute indirect (/4) */
332       if ((insn[1] & 0x38) == 0x20)
333         return 1;
334
335       /* jump far, absolute indirect (/5) */
336       if ((insn[1] & 0x38) == 0x28)
337         return 1;
338     }
339
340   return 0;
341 }
342
343 static int
344 i386_absolute_call_p (const gdb_byte *insn)
345 {
346   /* call far, absolute */
347   if (insn[0] == 0x9a)
348     return 1;
349
350   if (insn[0] == 0xff)
351     {
352       /* Call near, absolute indirect (/2) */
353       if ((insn[1] & 0x38) == 0x10)
354         return 1;
355
356       /* Call far, absolute indirect (/3) */
357       if ((insn[1] & 0x38) == 0x18)
358         return 1;
359     }
360
361   return 0;
362 }
363
364 static int
365 i386_ret_p (const gdb_byte *insn)
366 {
367   switch (insn[0])
368     {
369     case 0xc2: /* ret near, pop N bytes */
370     case 0xc3: /* ret near */
371     case 0xca: /* ret far, pop N bytes */
372     case 0xcb: /* ret far */
373     case 0xcf: /* iret */
374       return 1;
375
376     default:
377       return 0;
378     }
379 }
380
381 static int
382 i386_call_p (const gdb_byte *insn)
383 {
384   if (i386_absolute_call_p (insn))
385     return 1;
386
387   /* call near, relative */
388   if (insn[0] == 0xe8)
389     return 1;
390
391   return 0;
392 }
393
394 /* Return non-zero if INSN is a system call, and set *LENGTHP to its
395    length in bytes.  Otherwise, return zero.  */
396
397 static int
398 i386_syscall_p (const gdb_byte *insn, ULONGEST *lengthp)
399 {
400   if (insn[0] == 0xcd)
401     {
402       *lengthp = 2;
403       return 1;
404     }
405
406   return 0;
407 }
408
409 /* Fix up the state of registers and memory after having single-stepped
410    a displaced instruction.  */
411
412 void
413 i386_displaced_step_fixup (struct gdbarch *gdbarch,
414                            struct displaced_step_closure *closure,
415                            CORE_ADDR from, CORE_ADDR to,
416                            struct regcache *regs)
417 {
418   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
419
420   /* The offset we applied to the instruction's address.
421      This could well be negative (when viewed as a signed 32-bit
422      value), but ULONGEST won't reflect that, so take care when
423      applying it.  */
424   ULONGEST insn_offset = to - from;
425
426   /* Since we use simple_displaced_step_copy_insn, our closure is a
427      copy of the instruction.  */
428   gdb_byte *insn = (gdb_byte *) closure;
429   /* The start of the insn, needed in case we see some prefixes.  */
430   gdb_byte *insn_start = insn;
431
432   if (debug_displaced)
433     fprintf_unfiltered (gdb_stdlog,
434                         "displaced: fixup (%s, %s), "
435                         "insn = 0x%02x 0x%02x ...\n",
436                         paddress (gdbarch, from), paddress (gdbarch, to),
437                         insn[0], insn[1]);
438
439   /* The list of issues to contend with here is taken from
440      resume_execution in arch/i386/kernel/kprobes.c, Linux 2.6.20.
441      Yay for Free Software!  */
442
443   /* Relocate the %eip, if necessary.  */
444
445   /* The instruction recognizers we use assume any leading prefixes
446      have been skipped.  */
447   {
448     /* This is the size of the buffer in closure.  */
449     size_t max_insn_len = gdbarch_max_insn_length (gdbarch);
450     gdb_byte *opcode = i386_skip_prefixes (insn, max_insn_len);
451     /* If there are too many prefixes, just ignore the insn.
452        It will fault when run.  */
453     if (opcode != NULL)
454       insn = opcode;
455   }
456
457   /* Except in the case of absolute or indirect jump or call
458      instructions, or a return instruction, the new eip is relative to
459      the displaced instruction; make it relative.  Well, signal
460      handler returns don't need relocation either, but we use the
461      value of %eip to recognize those; see below.  */
462   if (! i386_absolute_jmp_p (insn)
463       && ! i386_absolute_call_p (insn)
464       && ! i386_ret_p (insn))
465     {
466       ULONGEST orig_eip;
467       ULONGEST insn_len;
468
469       regcache_cooked_read_unsigned (regs, I386_EIP_REGNUM, &orig_eip);
470
471       /* A signal trampoline system call changes the %eip, resuming
472          execution of the main program after the signal handler has
473          returned.  That makes them like 'return' instructions; we
474          shouldn't relocate %eip.
475
476          But most system calls don't, and we do need to relocate %eip.
477
478          Our heuristic for distinguishing these cases: if stepping
479          over the system call instruction left control directly after
480          the instruction, the we relocate --- control almost certainly
481          doesn't belong in the displaced copy.  Otherwise, we assume
482          the instruction has put control where it belongs, and leave
483          it unrelocated.  Goodness help us if there are PC-relative
484          system calls.  */
485       if (i386_syscall_p (insn, &insn_len)
486           && orig_eip != to + (insn - insn_start) + insn_len)
487         {
488           if (debug_displaced)
489             fprintf_unfiltered (gdb_stdlog,
490                                 "displaced: syscall changed %%eip; "
491                                 "not relocating\n");
492         }
493       else
494         {
495           ULONGEST eip = (orig_eip - insn_offset) & 0xffffffffUL;
496
497           /* If we just stepped over a breakpoint insn, we don't backup
498              the pc on purpose; this is to match behaviour without
499              stepping.  */
500
501           regcache_cooked_write_unsigned (regs, I386_EIP_REGNUM, eip);
502
503           if (debug_displaced)
504             fprintf_unfiltered (gdb_stdlog,
505                                 "displaced: "
506                                 "relocated %%eip from %s to %s\n",
507                                 paddress (gdbarch, orig_eip),
508                                 paddress (gdbarch, eip));
509         }
510     }
511
512   /* If the instruction was PUSHFL, then the TF bit will be set in the
513      pushed value, and should be cleared.  We'll leave this for later,
514      since GDB already messes up the TF flag when stepping over a
515      pushfl.  */
516
517   /* If the instruction was a call, the return address now atop the
518      stack is the address following the copied instruction.  We need
519      to make it the address following the original instruction.  */
520   if (i386_call_p (insn))
521     {
522       ULONGEST esp;
523       ULONGEST retaddr;
524       const ULONGEST retaddr_len = 4;
525
526       regcache_cooked_read_unsigned (regs, I386_ESP_REGNUM, &esp);
527       retaddr = read_memory_unsigned_integer (esp, retaddr_len, byte_order);
528       retaddr = (retaddr - insn_offset) & 0xffffffffUL;
529       write_memory_unsigned_integer (esp, retaddr_len, byte_order, retaddr);
530
531       if (debug_displaced)
532         fprintf_unfiltered (gdb_stdlog,
533                             "displaced: relocated return addr at %s to %s\n",
534                             paddress (gdbarch, esp),
535                             paddress (gdbarch, retaddr));
536     }
537 }
538 \f
539 #ifdef I386_REGNO_TO_SYMMETRY
540 #error "The Sequent Symmetry is no longer supported."
541 #endif
542
543 /* According to the System V ABI, the registers %ebp, %ebx, %edi, %esi
544    and %esp "belong" to the calling function.  Therefore these
545    registers should be saved if they're going to be modified.  */
546
547 /* The maximum number of saved registers.  This should include all
548    registers mentioned above, and %eip.  */
549 #define I386_NUM_SAVED_REGS     I386_NUM_GREGS
550
551 struct i386_frame_cache
552 {
553   /* Base address.  */
554   CORE_ADDR base;
555   LONGEST sp_offset;
556   CORE_ADDR pc;
557
558   /* Saved registers.  */
559   CORE_ADDR saved_regs[I386_NUM_SAVED_REGS];
560   CORE_ADDR saved_sp;
561   int saved_sp_reg;
562   int pc_in_eax;
563
564   /* Stack space reserved for local variables.  */
565   long locals;
566 };
567
568 /* Allocate and initialize a frame cache.  */
569
570 static struct i386_frame_cache *
571 i386_alloc_frame_cache (void)
572 {
573   struct i386_frame_cache *cache;
574   int i;
575
576   cache = FRAME_OBSTACK_ZALLOC (struct i386_frame_cache);
577
578   /* Base address.  */
579   cache->base = 0;
580   cache->sp_offset = -4;
581   cache->pc = 0;
582
583   /* Saved registers.  We initialize these to -1 since zero is a valid
584      offset (that's where %ebp is supposed to be stored).  */
585   for (i = 0; i < I386_NUM_SAVED_REGS; i++)
586     cache->saved_regs[i] = -1;
587   cache->saved_sp = 0;
588   cache->saved_sp_reg = -1;
589   cache->pc_in_eax = 0;
590
591   /* Frameless until proven otherwise.  */
592   cache->locals = -1;
593
594   return cache;
595 }
596
597 /* If the instruction at PC is a jump, return the address of its
598    target.  Otherwise, return PC.  */
599
600 static CORE_ADDR
601 i386_follow_jump (struct gdbarch *gdbarch, CORE_ADDR pc)
602 {
603   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
604   gdb_byte op;
605   long delta = 0;
606   int data16 = 0;
607
608   target_read_memory (pc, &op, 1);
609   if (op == 0x66)
610     {
611       data16 = 1;
612       op = read_memory_unsigned_integer (pc + 1, 1, byte_order);
613     }
614
615   switch (op)
616     {
617     case 0xe9:
618       /* Relative jump: if data16 == 0, disp32, else disp16.  */
619       if (data16)
620         {
621           delta = read_memory_integer (pc + 2, 2, byte_order);
622
623           /* Include the size of the jmp instruction (including the
624              0x66 prefix).  */
625           delta += 4;
626         }
627       else
628         {
629           delta = read_memory_integer (pc + 1, 4, byte_order);
630
631           /* Include the size of the jmp instruction.  */
632           delta += 5;
633         }
634       break;
635     case 0xeb:
636       /* Relative jump, disp8 (ignore data16).  */
637       delta = read_memory_integer (pc + data16 + 1, 1, byte_order);
638
639       delta += data16 + 2;
640       break;
641     }
642
643   return pc + delta;
644 }
645
646 /* Check whether PC points at a prologue for a function returning a
647    structure or union.  If so, it updates CACHE and returns the
648    address of the first instruction after the code sequence that
649    removes the "hidden" argument from the stack or CURRENT_PC,
650    whichever is smaller.  Otherwise, return PC.  */
651
652 static CORE_ADDR
653 i386_analyze_struct_return (CORE_ADDR pc, CORE_ADDR current_pc,
654                             struct i386_frame_cache *cache)
655 {
656   /* Functions that return a structure or union start with:
657
658         popl %eax             0x58
659         xchgl %eax, (%esp)    0x87 0x04 0x24
660      or xchgl %eax, 0(%esp)   0x87 0x44 0x24 0x00
661
662      (the System V compiler puts out the second `xchg' instruction,
663      and the assembler doesn't try to optimize it, so the 'sib' form
664      gets generated).  This sequence is used to get the address of the
665      return buffer for a function that returns a structure.  */
666   static gdb_byte proto1[3] = { 0x87, 0x04, 0x24 };
667   static gdb_byte proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
668   gdb_byte buf[4];
669   gdb_byte op;
670
671   if (current_pc <= pc)
672     return pc;
673
674   target_read_memory (pc, &op, 1);
675
676   if (op != 0x58)               /* popl %eax */
677     return pc;
678
679   target_read_memory (pc + 1, buf, 4);
680   if (memcmp (buf, proto1, 3) != 0 && memcmp (buf, proto2, 4) != 0)
681     return pc;
682
683   if (current_pc == pc)
684     {
685       cache->sp_offset += 4;
686       return current_pc;
687     }
688
689   if (current_pc == pc + 1)
690     {
691       cache->pc_in_eax = 1;
692       return current_pc;
693     }
694   
695   if (buf[1] == proto1[1])
696     return pc + 4;
697   else
698     return pc + 5;
699 }
700
701 static CORE_ADDR
702 i386_skip_probe (CORE_ADDR pc)
703 {
704   /* A function may start with
705
706         pushl constant
707         call _probe
708         addl $4, %esp
709            
710      followed by
711
712         pushl %ebp
713
714      etc.  */
715   gdb_byte buf[8];
716   gdb_byte op;
717
718   target_read_memory (pc, &op, 1);
719
720   if (op == 0x68 || op == 0x6a)
721     {
722       int delta;
723
724       /* Skip past the `pushl' instruction; it has either a one-byte or a
725          four-byte operand, depending on the opcode.  */
726       if (op == 0x68)
727         delta = 5;
728       else
729         delta = 2;
730
731       /* Read the following 8 bytes, which should be `call _probe' (6
732          bytes) followed by `addl $4,%esp' (2 bytes).  */
733       read_memory (pc + delta, buf, sizeof (buf));
734       if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
735         pc += delta + sizeof (buf);
736     }
737
738   return pc;
739 }
740
741 /* GCC 4.1 and later, can put code in the prologue to realign the
742    stack pointer.  Check whether PC points to such code, and update
743    CACHE accordingly.  Return the first instruction after the code
744    sequence or CURRENT_PC, whichever is smaller.  If we don't
745    recognize the code, return PC.  */
746
747 static CORE_ADDR
748 i386_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
749                           struct i386_frame_cache *cache)
750 {
751   /* There are 2 code sequences to re-align stack before the frame
752      gets set up:
753
754         1. Use a caller-saved saved register:
755
756                 leal  4(%esp), %reg
757                 andl  $-XXX, %esp
758                 pushl -4(%reg)
759
760         2. Use a callee-saved saved register:
761
762                 pushl %reg
763                 leal  8(%esp), %reg
764                 andl  $-XXX, %esp
765                 pushl -4(%reg)
766
767      "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:
768      
769         0x83 0xe4 0xf0                  andl $-16, %esp
770         0x81 0xe4 0x00 0xff 0xff 0xff   andl $-256, %esp
771    */
772
773   gdb_byte buf[14];
774   int reg;
775   int offset, offset_and;
776   static int regnums[8] = {
777     I386_EAX_REGNUM,            /* %eax */
778     I386_ECX_REGNUM,            /* %ecx */
779     I386_EDX_REGNUM,            /* %edx */
780     I386_EBX_REGNUM,            /* %ebx */
781     I386_ESP_REGNUM,            /* %esp */
782     I386_EBP_REGNUM,            /* %ebp */
783     I386_ESI_REGNUM,            /* %esi */
784     I386_EDI_REGNUM             /* %edi */
785   };
786
787   if (target_read_memory (pc, buf, sizeof buf))
788     return pc;
789
790   /* Check caller-saved saved register.  The first instruction has
791      to be "leal 4(%esp), %reg".  */
792   if (buf[0] == 0x8d && buf[2] == 0x24 && buf[3] == 0x4)
793     {
794       /* MOD must be binary 10 and R/M must be binary 100.  */
795       if ((buf[1] & 0xc7) != 0x44)
796         return pc;
797
798       /* REG has register number.  */
799       reg = (buf[1] >> 3) & 7;
800       offset = 4;
801     }
802   else
803     {
804       /* Check callee-saved saved register.  The first instruction
805          has to be "pushl %reg".  */
806       if ((buf[0] & 0xf8) != 0x50)
807         return pc;
808
809       /* Get register.  */
810       reg = buf[0] & 0x7;
811
812       /* The next instruction has to be "leal 8(%esp), %reg".  */
813       if (buf[1] != 0x8d || buf[3] != 0x24 || buf[4] != 0x8)
814         return pc;
815
816       /* MOD must be binary 10 and R/M must be binary 100.  */
817       if ((buf[2] & 0xc7) != 0x44)
818         return pc;
819       
820       /* REG has register number.  Registers in pushl and leal have to
821          be the same.  */
822       if (reg != ((buf[2] >> 3) & 7))
823         return pc;
824
825       offset = 5;
826     }
827
828   /* Rigister can't be %esp nor %ebp.  */
829   if (reg == 4 || reg == 5)
830     return pc;
831
832   /* The next instruction has to be "andl $-XXX, %esp".  */
833   if (buf[offset + 1] != 0xe4
834       || (buf[offset] != 0x81 && buf[offset] != 0x83))
835     return pc;
836
837   offset_and = offset;
838   offset += buf[offset] == 0x81 ? 6 : 3;
839
840   /* The next instruction has to be "pushl -4(%reg)".  8bit -4 is
841      0xfc.  REG must be binary 110 and MOD must be binary 01.  */
842   if (buf[offset] != 0xff
843       || buf[offset + 2] != 0xfc
844       || (buf[offset + 1] & 0xf8) != 0x70)
845     return pc;
846
847   /* R/M has register.  Registers in leal and pushl have to be the
848      same.  */
849   if (reg != (buf[offset + 1] & 7))
850     return pc;
851
852   if (current_pc > pc + offset_and)
853     cache->saved_sp_reg = regnums[reg];
854
855   return min (pc + offset + 3, current_pc);
856 }
857
858 /* Maximum instruction length we need to handle.  */
859 #define I386_MAX_MATCHED_INSN_LEN       6
860
861 /* Instruction description.  */
862 struct i386_insn
863 {
864   size_t len;
865   gdb_byte insn[I386_MAX_MATCHED_INSN_LEN];
866   gdb_byte mask[I386_MAX_MATCHED_INSN_LEN];
867 };
868
869 /* Search for the instruction at PC in the list SKIP_INSNS.  Return
870    the first instruction description that matches.  Otherwise, return
871    NULL.  */
872
873 static struct i386_insn *
874 i386_match_insn (CORE_ADDR pc, struct i386_insn *skip_insns)
875 {
876   struct i386_insn *insn;
877   gdb_byte op;
878
879   target_read_memory (pc, &op, 1);
880
881   for (insn = skip_insns; insn->len > 0; insn++)
882     {
883       if ((op & insn->mask[0]) == insn->insn[0])
884         {
885           gdb_byte buf[I386_MAX_MATCHED_INSN_LEN - 1];
886           int insn_matched = 1;
887           size_t i;
888
889           gdb_assert (insn->len > 1);
890           gdb_assert (insn->len <= I386_MAX_MATCHED_INSN_LEN);
891
892           target_read_memory (pc + 1, buf, insn->len - 1);
893           for (i = 1; i < insn->len; i++)
894             {
895               if ((buf[i - 1] & insn->mask[i]) != insn->insn[i])
896                 insn_matched = 0;
897             }
898
899           if (insn_matched)
900             return insn;
901         }
902     }
903
904   return NULL;
905 }
906
907 /* Some special instructions that might be migrated by GCC into the
908    part of the prologue that sets up the new stack frame.  Because the
909    stack frame hasn't been setup yet, no registers have been saved
910    yet, and only the scratch registers %eax, %ecx and %edx can be
911    touched.  */
912
913 struct i386_insn i386_frame_setup_skip_insns[] =
914 {
915   /* Check for `movb imm8, r' and `movl imm32, r'. 
916     
917      ??? Should we handle 16-bit operand-sizes here?  */
918
919   /* `movb imm8, %al' and `movb imm8, %ah' */
920   /* `movb imm8, %cl' and `movb imm8, %ch' */
921   { 2, { 0xb0, 0x00 }, { 0xfa, 0x00 } },
922   /* `movb imm8, %dl' and `movb imm8, %dh' */
923   { 2, { 0xb2, 0x00 }, { 0xfb, 0x00 } },
924   /* `movl imm32, %eax' and `movl imm32, %ecx' */
925   { 5, { 0xb8 }, { 0xfe } },
926   /* `movl imm32, %edx' */
927   { 5, { 0xba }, { 0xff } },
928
929   /* Check for `mov imm32, r32'.  Note that there is an alternative
930      encoding for `mov m32, %eax'.
931
932      ??? Should we handle SIB adressing here?
933      ??? Should we handle 16-bit operand-sizes here?  */
934
935   /* `movl m32, %eax' */
936   { 5, { 0xa1 }, { 0xff } },
937   /* `movl m32, %eax' and `mov; m32, %ecx' */
938   { 6, { 0x89, 0x05 }, {0xff, 0xf7 } },
939   /* `movl m32, %edx' */
940   { 6, { 0x89, 0x15 }, {0xff, 0xff } },
941
942   /* Check for `xorl r32, r32' and the equivalent `subl r32, r32'.
943      Because of the symmetry, there are actually two ways to encode
944      these instructions; opcode bytes 0x29 and 0x2b for `subl' and
945      opcode bytes 0x31 and 0x33 for `xorl'.  */
946
947   /* `subl %eax, %eax' */
948   { 2, { 0x29, 0xc0 }, { 0xfd, 0xff } },
949   /* `subl %ecx, %ecx' */
950   { 2, { 0x29, 0xc9 }, { 0xfd, 0xff } },
951   /* `subl %edx, %edx' */
952   { 2, { 0x29, 0xd2 }, { 0xfd, 0xff } },
953   /* `xorl %eax, %eax' */
954   { 2, { 0x31, 0xc0 }, { 0xfd, 0xff } },
955   /* `xorl %ecx, %ecx' */
956   { 2, { 0x31, 0xc9 }, { 0xfd, 0xff } },
957   /* `xorl %edx, %edx' */
958   { 2, { 0x31, 0xd2 }, { 0xfd, 0xff } },
959   { 0 }
960 };
961
962
963 /* Check whether PC points to a no-op instruction.  */
964 static CORE_ADDR
965 i386_skip_noop (CORE_ADDR pc)
966 {
967   gdb_byte op;
968   int check = 1;
969
970   target_read_memory (pc, &op, 1);
971
972   while (check) 
973     {
974       check = 0;
975       /* Ignore `nop' instruction.  */
976       if (op == 0x90) 
977         {
978           pc += 1;
979           target_read_memory (pc, &op, 1);
980           check = 1;
981         }
982       /* Ignore no-op instruction `mov %edi, %edi'.
983          Microsoft system dlls often start with
984          a `mov %edi,%edi' instruction.
985          The 5 bytes before the function start are
986          filled with `nop' instructions.
987          This pattern can be used for hot-patching:
988          The `mov %edi, %edi' instruction can be replaced by a
989          near jump to the location of the 5 `nop' instructions
990          which can be replaced by a 32-bit jump to anywhere
991          in the 32-bit address space.  */
992
993       else if (op == 0x8b)
994         {
995           target_read_memory (pc + 1, &op, 1);
996           if (op == 0xff)
997             {
998               pc += 2;
999               target_read_memory (pc, &op, 1);
1000               check = 1;
1001             }
1002         }
1003     }
1004   return pc; 
1005 }
1006
1007 /* Check whether PC points at a code that sets up a new stack frame.
1008    If so, it updates CACHE and returns the address of the first
1009    instruction after the sequence that sets up the frame or LIMIT,
1010    whichever is smaller.  If we don't recognize the code, return PC.  */
1011
1012 static CORE_ADDR
1013 i386_analyze_frame_setup (struct gdbarch *gdbarch,
1014                           CORE_ADDR pc, CORE_ADDR limit,
1015                           struct i386_frame_cache *cache)
1016 {
1017   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1018   struct i386_insn *insn;
1019   gdb_byte op;
1020   int skip = 0;
1021
1022   if (limit <= pc)
1023     return limit;
1024
1025   target_read_memory (pc, &op, 1);
1026
1027   if (op == 0x55)               /* pushl %ebp */
1028     {
1029       /* Take into account that we've executed the `pushl %ebp' that
1030          starts this instruction sequence.  */
1031       cache->saved_regs[I386_EBP_REGNUM] = 0;
1032       cache->sp_offset += 4;
1033       pc++;
1034
1035       /* If that's all, return now.  */
1036       if (limit <= pc)
1037         return limit;
1038
1039       /* Check for some special instructions that might be migrated by
1040          GCC into the prologue and skip them.  At this point in the
1041          prologue, code should only touch the scratch registers %eax,
1042          %ecx and %edx, so while the number of posibilities is sheer,
1043          it is limited.
1044
1045          Make sure we only skip these instructions if we later see the
1046          `movl %esp, %ebp' that actually sets up the frame.  */
1047       while (pc + skip < limit)
1048         {
1049           insn = i386_match_insn (pc + skip, i386_frame_setup_skip_insns);
1050           if (insn == NULL)
1051             break;
1052
1053           skip += insn->len;
1054         }
1055
1056       /* If that's all, return now.  */
1057       if (limit <= pc + skip)
1058         return limit;
1059
1060       target_read_memory (pc + skip, &op, 1);
1061
1062       /* Check for `movl %esp, %ebp' -- can be written in two ways.  */
1063       switch (op)
1064         {
1065         case 0x8b:
1066           if (read_memory_unsigned_integer (pc + skip + 1, 1, byte_order)
1067               != 0xec)
1068             return pc;
1069           break;
1070         case 0x89:
1071           if (read_memory_unsigned_integer (pc + skip + 1, 1, byte_order)
1072               != 0xe5)
1073             return pc;
1074           break;
1075         default:
1076           return pc;
1077         }
1078
1079       /* OK, we actually have a frame.  We just don't know how large
1080          it is yet.  Set its size to zero.  We'll adjust it if
1081          necessary.  We also now commit to skipping the special
1082          instructions mentioned before.  */
1083       cache->locals = 0;
1084       pc += (skip + 2);
1085
1086       /* If that's all, return now.  */
1087       if (limit <= pc)
1088         return limit;
1089
1090       /* Check for stack adjustment 
1091
1092             subl $XXX, %esp
1093
1094          NOTE: You can't subtract a 16-bit immediate from a 32-bit
1095          reg, so we don't have to worry about a data16 prefix.  */
1096       target_read_memory (pc, &op, 1);
1097       if (op == 0x83)
1098         {
1099           /* `subl' with 8-bit immediate.  */
1100           if (read_memory_unsigned_integer (pc + 1, 1, byte_order) != 0xec)
1101             /* Some instruction starting with 0x83 other than `subl'.  */
1102             return pc;
1103
1104           /* `subl' with signed 8-bit immediate (though it wouldn't
1105              make sense to be negative).  */
1106           cache->locals = read_memory_integer (pc + 2, 1, byte_order);
1107           return pc + 3;
1108         }
1109       else if (op == 0x81)
1110         {
1111           /* Maybe it is `subl' with a 32-bit immediate.  */
1112           if (read_memory_unsigned_integer (pc + 1, 1, byte_order) != 0xec)
1113             /* Some instruction starting with 0x81 other than `subl'.  */
1114             return pc;
1115
1116           /* It is `subl' with a 32-bit immediate.  */
1117           cache->locals = read_memory_integer (pc + 2, 4, byte_order);
1118           return pc + 6;
1119         }
1120       else
1121         {
1122           /* Some instruction other than `subl'.  */
1123           return pc;
1124         }
1125     }
1126   else if (op == 0xc8)          /* enter */
1127     {
1128       cache->locals = read_memory_unsigned_integer (pc + 1, 2, byte_order);
1129       return pc + 4;
1130     }
1131
1132   return pc;
1133 }
1134
1135 /* Check whether PC points at code that saves registers on the stack.
1136    If so, it updates CACHE and returns the address of the first
1137    instruction after the register saves or CURRENT_PC, whichever is
1138    smaller.  Otherwise, return PC.  */
1139
1140 static CORE_ADDR
1141 i386_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
1142                              struct i386_frame_cache *cache)
1143 {
1144   CORE_ADDR offset = 0;
1145   gdb_byte op;
1146   int i;
1147
1148   if (cache->locals > 0)
1149     offset -= cache->locals;
1150   for (i = 0; i < 8 && pc < current_pc; i++)
1151     {
1152       target_read_memory (pc, &op, 1);
1153       if (op < 0x50 || op > 0x57)
1154         break;
1155
1156       offset -= 4;
1157       cache->saved_regs[op - 0x50] = offset;
1158       cache->sp_offset += 4;
1159       pc++;
1160     }
1161
1162   return pc;
1163 }
1164
1165 /* Do a full analysis of the prologue at PC and update CACHE
1166    accordingly.  Bail out early if CURRENT_PC is reached.  Return the
1167    address where the analysis stopped.
1168
1169    We handle these cases:
1170
1171    The startup sequence can be at the start of the function, or the
1172    function can start with a branch to startup code at the end.
1173
1174    %ebp can be set up with either the 'enter' instruction, or "pushl
1175    %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
1176    once used in the System V compiler).
1177
1178    Local space is allocated just below the saved %ebp by either the
1179    'enter' instruction, or by "subl $<size>, %esp".  'enter' has a
1180    16-bit unsigned argument for space to allocate, and the 'addl'
1181    instruction could have either a signed byte, or 32-bit immediate.
1182
1183    Next, the registers used by this function are pushed.  With the
1184    System V compiler they will always be in the order: %edi, %esi,
1185    %ebx (and sometimes a harmless bug causes it to also save but not
1186    restore %eax); however, the code below is willing to see the pushes
1187    in any order, and will handle up to 8 of them.
1188  
1189    If the setup sequence is at the end of the function, then the next
1190    instruction will be a branch back to the start.  */
1191
1192 static CORE_ADDR
1193 i386_analyze_prologue (struct gdbarch *gdbarch,
1194                        CORE_ADDR pc, CORE_ADDR current_pc,
1195                        struct i386_frame_cache *cache)
1196 {
1197   pc = i386_skip_noop (pc);
1198   pc = i386_follow_jump (gdbarch, pc);
1199   pc = i386_analyze_struct_return (pc, current_pc, cache);
1200   pc = i386_skip_probe (pc);
1201   pc = i386_analyze_stack_align (pc, current_pc, cache);
1202   pc = i386_analyze_frame_setup (gdbarch, pc, current_pc, cache);
1203   return i386_analyze_register_saves (pc, current_pc, cache);
1204 }
1205
1206 /* Return PC of first real instruction.  */
1207
1208 static CORE_ADDR
1209 i386_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
1210 {
1211   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1212
1213   static gdb_byte pic_pat[6] =
1214   {
1215     0xe8, 0, 0, 0, 0,           /* call 0x0 */
1216     0x5b,                       /* popl %ebx */
1217   };
1218   struct i386_frame_cache cache;
1219   CORE_ADDR pc;
1220   gdb_byte op;
1221   int i;
1222
1223   cache.locals = -1;
1224   pc = i386_analyze_prologue (gdbarch, start_pc, 0xffffffff, &cache);
1225   if (cache.locals < 0)
1226     return start_pc;
1227
1228   /* Found valid frame setup.  */
1229
1230   /* The native cc on SVR4 in -K PIC mode inserts the following code
1231      to get the address of the global offset table (GOT) into register
1232      %ebx:
1233
1234         call    0x0
1235         popl    %ebx
1236         movl    %ebx,x(%ebp)    (optional)
1237         addl    y,%ebx
1238
1239      This code is with the rest of the prologue (at the end of the
1240      function), so we have to skip it to get to the first real
1241      instruction at the start of the function.  */
1242
1243   for (i = 0; i < 6; i++)
1244     {
1245       target_read_memory (pc + i, &op, 1);
1246       if (pic_pat[i] != op)
1247         break;
1248     }
1249   if (i == 6)
1250     {
1251       int delta = 6;
1252
1253       target_read_memory (pc + delta, &op, 1);
1254
1255       if (op == 0x89)           /* movl %ebx, x(%ebp) */
1256         {
1257           op = read_memory_unsigned_integer (pc + delta + 1, 1, byte_order);
1258
1259           if (op == 0x5d)       /* One byte offset from %ebp.  */
1260             delta += 3;
1261           else if (op == 0x9d)  /* Four byte offset from %ebp.  */
1262             delta += 6;
1263           else                  /* Unexpected instruction.  */
1264             delta = 0;
1265
1266           target_read_memory (pc + delta, &op, 1);
1267         }
1268
1269       /* addl y,%ebx */
1270       if (delta > 0 && op == 0x81
1271           && read_memory_unsigned_integer (pc + delta + 1, 1, byte_order)
1272              == 0xc3)
1273         {
1274           pc += delta + 6;
1275         }
1276     }
1277
1278   /* If the function starts with a branch (to startup code at the end)
1279      the last instruction should bring us back to the first
1280      instruction of the real code.  */
1281   if (i386_follow_jump (gdbarch, start_pc) != start_pc)
1282     pc = i386_follow_jump (gdbarch, pc);
1283
1284   return pc;
1285 }
1286
1287 /* Check that the code pointed to by PC corresponds to a call to
1288    __main, skip it if so.  Return PC otherwise.  */
1289
1290 CORE_ADDR
1291 i386_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1292 {
1293   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1294   gdb_byte op;
1295
1296   target_read_memory (pc, &op, 1);
1297   if (op == 0xe8)
1298     {
1299       gdb_byte buf[4];
1300
1301       if (target_read_memory (pc + 1, buf, sizeof buf) == 0)
1302         {
1303           /* Make sure address is computed correctly as a 32bit
1304              integer even if CORE_ADDR is 64 bit wide.  */
1305           struct minimal_symbol *s;
1306           CORE_ADDR call_dest;
1307
1308           call_dest = pc + 5 + extract_signed_integer (buf, 4, byte_order);
1309           call_dest = call_dest & 0xffffffffU;
1310           s = lookup_minimal_symbol_by_pc (call_dest);
1311           if (s != NULL
1312               && SYMBOL_LINKAGE_NAME (s) != NULL
1313               && strcmp (SYMBOL_LINKAGE_NAME (s), "__main") == 0)
1314             pc += 5;
1315         }
1316     }
1317
1318   return pc;
1319 }
1320
1321 /* This function is 64-bit safe.  */
1322
1323 static CORE_ADDR
1324 i386_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1325 {
1326   gdb_byte buf[8];
1327
1328   frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
1329   return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
1330 }
1331 \f
1332
1333 /* Normal frames.  */
1334
1335 static struct i386_frame_cache *
1336 i386_frame_cache (struct frame_info *this_frame, void **this_cache)
1337 {
1338   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1339   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1340   struct i386_frame_cache *cache;
1341   gdb_byte buf[4];
1342   int i;
1343
1344   if (*this_cache)
1345     return *this_cache;
1346
1347   cache = i386_alloc_frame_cache ();
1348   *this_cache = cache;
1349
1350   /* In principle, for normal frames, %ebp holds the frame pointer,
1351      which holds the base address for the current stack frame.
1352      However, for functions that don't need it, the frame pointer is
1353      optional.  For these "frameless" functions the frame pointer is
1354      actually the frame pointer of the calling frame.  Signal
1355      trampolines are just a special case of a "frameless" function.
1356      They (usually) share their frame pointer with the frame that was
1357      in progress when the signal occurred.  */
1358
1359   get_frame_register (this_frame, I386_EBP_REGNUM, buf);
1360   cache->base = extract_unsigned_integer (buf, 4, byte_order);
1361   if (cache->base == 0)
1362     return cache;
1363
1364   /* For normal frames, %eip is stored at 4(%ebp).  */
1365   cache->saved_regs[I386_EIP_REGNUM] = 4;
1366
1367   cache->pc = get_frame_func (this_frame);
1368   if (cache->pc != 0)
1369     i386_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
1370                            cache);
1371
1372   if (cache->saved_sp_reg != -1)
1373     {
1374       /* Saved stack pointer has been saved.  */
1375       get_frame_register (this_frame, cache->saved_sp_reg, buf);
1376       cache->saved_sp = extract_unsigned_integer (buf, 4, byte_order);
1377     }
1378
1379   if (cache->locals < 0)
1380     {
1381       /* We didn't find a valid frame, which means that CACHE->base
1382          currently holds the frame pointer for our calling frame.  If
1383          we're at the start of a function, or somewhere half-way its
1384          prologue, the function's frame probably hasn't been fully
1385          setup yet.  Try to reconstruct the base address for the stack
1386          frame by looking at the stack pointer.  For truly "frameless"
1387          functions this might work too.  */
1388
1389       if (cache->saved_sp_reg != -1)
1390         {
1391           /* We're halfway aligning the stack.  */
1392           cache->base = ((cache->saved_sp - 4) & 0xfffffff0) - 4;
1393           cache->saved_regs[I386_EIP_REGNUM] = cache->saved_sp - 4;
1394
1395           /* This will be added back below.  */
1396           cache->saved_regs[I386_EIP_REGNUM] -= cache->base;
1397         }
1398       else
1399         {
1400           get_frame_register (this_frame, I386_ESP_REGNUM, buf);
1401           cache->base = extract_unsigned_integer (buf, 4, byte_order)
1402                         + cache->sp_offset;
1403         }
1404     }
1405
1406   /* Now that we have the base address for the stack frame we can
1407      calculate the value of %esp in the calling frame.  */
1408   if (cache->saved_sp == 0)
1409     cache->saved_sp = cache->base + 8;
1410
1411   /* Adjust all the saved registers such that they contain addresses
1412      instead of offsets.  */
1413   for (i = 0; i < I386_NUM_SAVED_REGS; i++)
1414     if (cache->saved_regs[i] != -1)
1415       cache->saved_regs[i] += cache->base;
1416
1417   return cache;
1418 }
1419
1420 static void
1421 i386_frame_this_id (struct frame_info *this_frame, void **this_cache,
1422                     struct frame_id *this_id)
1423 {
1424   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
1425
1426   /* This marks the outermost frame.  */
1427   if (cache->base == 0)
1428     return;
1429
1430   /* See the end of i386_push_dummy_call.  */
1431   (*this_id) = frame_id_build (cache->base + 8, cache->pc);
1432 }
1433
1434 static struct value *
1435 i386_frame_prev_register (struct frame_info *this_frame, void **this_cache,
1436                           int regnum)
1437 {
1438   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
1439
1440   gdb_assert (regnum >= 0);
1441
1442   /* The System V ABI says that:
1443
1444      "The flags register contains the system flags, such as the
1445      direction flag and the carry flag.  The direction flag must be
1446      set to the forward (that is, zero) direction before entry and
1447      upon exit from a function.  Other user flags have no specified
1448      role in the standard calling sequence and are not preserved."
1449
1450      To guarantee the "upon exit" part of that statement we fake a
1451      saved flags register that has its direction flag cleared.
1452
1453      Note that GCC doesn't seem to rely on the fact that the direction
1454      flag is cleared after a function return; it always explicitly
1455      clears the flag before operations where it matters.
1456
1457      FIXME: kettenis/20030316: I'm not quite sure whether this is the
1458      right thing to do.  The way we fake the flags register here makes
1459      it impossible to change it.  */
1460
1461   if (regnum == I386_EFLAGS_REGNUM)
1462     {
1463       ULONGEST val;
1464
1465       val = get_frame_register_unsigned (this_frame, regnum);
1466       val &= ~(1 << 10);
1467       return frame_unwind_got_constant (this_frame, regnum, val);
1468     }
1469
1470   if (regnum == I386_EIP_REGNUM && cache->pc_in_eax)
1471     return frame_unwind_got_register (this_frame, regnum, I386_EAX_REGNUM);
1472
1473   if (regnum == I386_ESP_REGNUM && cache->saved_sp)
1474     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
1475
1476   if (regnum < I386_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
1477     return frame_unwind_got_memory (this_frame, regnum,
1478                                     cache->saved_regs[regnum]);
1479
1480   return frame_unwind_got_register (this_frame, regnum, regnum);
1481 }
1482
1483 static const struct frame_unwind i386_frame_unwind =
1484 {
1485   NORMAL_FRAME,
1486   i386_frame_this_id,
1487   i386_frame_prev_register,
1488   NULL,
1489   default_frame_sniffer
1490 };
1491
1492 /* Normal frames, but in a function epilogue.  */
1493
1494 /* The epilogue is defined here as the 'ret' instruction, which will
1495    follow any instruction such as 'leave' or 'pop %ebp' that destroys
1496    the function's stack frame.  */
1497
1498 static int
1499 i386_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
1500 {
1501   gdb_byte insn;
1502
1503   if (target_read_memory (pc, &insn, 1))
1504     return 0;   /* Can't read memory at pc.  */
1505
1506   if (insn != 0xc3)     /* 'ret' instruction.  */
1507     return 0;
1508
1509   return 1;
1510 }
1511
1512 static int
1513 i386_epilogue_frame_sniffer (const struct frame_unwind *self,
1514                              struct frame_info *this_frame,
1515                              void **this_prologue_cache)
1516 {
1517   if (frame_relative_level (this_frame) == 0)
1518     return i386_in_function_epilogue_p (get_frame_arch (this_frame),
1519                                         get_frame_pc (this_frame));
1520   else
1521     return 0;
1522 }
1523
1524 static struct i386_frame_cache *
1525 i386_epilogue_frame_cache (struct frame_info *this_frame, void **this_cache)
1526 {
1527   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1528   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1529   struct i386_frame_cache *cache;
1530   gdb_byte buf[4];
1531
1532   if (*this_cache)
1533     return *this_cache;
1534
1535   cache = i386_alloc_frame_cache ();
1536   *this_cache = cache;
1537
1538   /* Cache base will be %esp plus cache->sp_offset (-4).  */
1539   get_frame_register (this_frame, I386_ESP_REGNUM, buf);
1540   cache->base = extract_unsigned_integer (buf, 4, 
1541                                           byte_order) + cache->sp_offset;
1542
1543   /* Cache pc will be the frame func.  */
1544   cache->pc = get_frame_pc (this_frame);
1545
1546   /* The saved %esp will be at cache->base plus 8.  */
1547   cache->saved_sp = cache->base + 8;
1548
1549   /* The saved %eip will be at cache->base plus 4.  */
1550   cache->saved_regs[I386_EIP_REGNUM] = cache->base + 4;
1551
1552   return cache;
1553 }
1554
1555 static void
1556 i386_epilogue_frame_this_id (struct frame_info *this_frame,
1557                              void **this_cache,
1558                              struct frame_id *this_id)
1559 {
1560   struct i386_frame_cache *cache = i386_epilogue_frame_cache (this_frame,
1561                                                               this_cache);
1562
1563   (*this_id) = frame_id_build (cache->base + 8, cache->pc);
1564 }
1565
1566 static const struct frame_unwind i386_epilogue_frame_unwind =
1567 {
1568   NORMAL_FRAME,
1569   i386_epilogue_frame_this_id,
1570   i386_frame_prev_register,
1571   NULL, 
1572   i386_epilogue_frame_sniffer
1573 };
1574 \f
1575
1576 /* Signal trampolines.  */
1577
1578 static struct i386_frame_cache *
1579 i386_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
1580 {
1581   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1582   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1583   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1584   struct i386_frame_cache *cache;
1585   CORE_ADDR addr;
1586   gdb_byte buf[4];
1587
1588   if (*this_cache)
1589     return *this_cache;
1590
1591   cache = i386_alloc_frame_cache ();
1592
1593   get_frame_register (this_frame, I386_ESP_REGNUM, buf);
1594   cache->base = extract_unsigned_integer (buf, 4, byte_order) - 4;
1595
1596   addr = tdep->sigcontext_addr (this_frame);
1597   if (tdep->sc_reg_offset)
1598     {
1599       int i;
1600
1601       gdb_assert (tdep->sc_num_regs <= I386_NUM_SAVED_REGS);
1602
1603       for (i = 0; i < tdep->sc_num_regs; i++)
1604         if (tdep->sc_reg_offset[i] != -1)
1605           cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
1606     }
1607   else
1608     {
1609       cache->saved_regs[I386_EIP_REGNUM] = addr + tdep->sc_pc_offset;
1610       cache->saved_regs[I386_ESP_REGNUM] = addr + tdep->sc_sp_offset;
1611     }
1612
1613   *this_cache = cache;
1614   return cache;
1615 }
1616
1617 static void
1618 i386_sigtramp_frame_this_id (struct frame_info *this_frame, void **this_cache,
1619                              struct frame_id *this_id)
1620 {
1621   struct i386_frame_cache *cache =
1622     i386_sigtramp_frame_cache (this_frame, this_cache);
1623
1624   /* See the end of i386_push_dummy_call.  */
1625   (*this_id) = frame_id_build (cache->base + 8, get_frame_pc (this_frame));
1626 }
1627
1628 static struct value *
1629 i386_sigtramp_frame_prev_register (struct frame_info *this_frame,
1630                                    void **this_cache, int regnum)
1631 {
1632   /* Make sure we've initialized the cache.  */
1633   i386_sigtramp_frame_cache (this_frame, this_cache);
1634
1635   return i386_frame_prev_register (this_frame, this_cache, regnum);
1636 }
1637
1638 static int
1639 i386_sigtramp_frame_sniffer (const struct frame_unwind *self,
1640                              struct frame_info *this_frame,
1641                              void **this_prologue_cache)
1642 {
1643   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
1644
1645   /* We shouldn't even bother if we don't have a sigcontext_addr
1646      handler.  */
1647   if (tdep->sigcontext_addr == NULL)
1648     return 0;
1649
1650   if (tdep->sigtramp_p != NULL)
1651     {
1652       if (tdep->sigtramp_p (this_frame))
1653         return 1;
1654     }
1655
1656   if (tdep->sigtramp_start != 0)
1657     {
1658       CORE_ADDR pc = get_frame_pc (this_frame);
1659
1660       gdb_assert (tdep->sigtramp_end != 0);
1661       if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
1662         return 1;
1663     }
1664
1665   return 0;
1666 }
1667
1668 static const struct frame_unwind i386_sigtramp_frame_unwind =
1669 {
1670   SIGTRAMP_FRAME,
1671   i386_sigtramp_frame_this_id,
1672   i386_sigtramp_frame_prev_register,
1673   NULL,
1674   i386_sigtramp_frame_sniffer
1675 };
1676 \f
1677
1678 static CORE_ADDR
1679 i386_frame_base_address (struct frame_info *this_frame, void **this_cache)
1680 {
1681   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
1682
1683   return cache->base;
1684 }
1685
1686 static const struct frame_base i386_frame_base =
1687 {
1688   &i386_frame_unwind,
1689   i386_frame_base_address,
1690   i386_frame_base_address,
1691   i386_frame_base_address
1692 };
1693
1694 static struct frame_id
1695 i386_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1696 {
1697   CORE_ADDR fp;
1698
1699   fp = get_frame_register_unsigned (this_frame, I386_EBP_REGNUM);
1700
1701   /* See the end of i386_push_dummy_call.  */
1702   return frame_id_build (fp + 8, get_frame_pc (this_frame));
1703 }
1704 \f
1705
1706 /* Figure out where the longjmp will land.  Slurp the args out of the
1707    stack.  We expect the first arg to be a pointer to the jmp_buf
1708    structure from which we extract the address that we will land at.
1709    This address is copied into PC.  This routine returns non-zero on
1710    success.  */
1711
1712 static int
1713 i386_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
1714 {
1715   gdb_byte buf[4];
1716   CORE_ADDR sp, jb_addr;
1717   struct gdbarch *gdbarch = get_frame_arch (frame);
1718   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1719   int jb_pc_offset = gdbarch_tdep (gdbarch)->jb_pc_offset;
1720
1721   /* If JB_PC_OFFSET is -1, we have no way to find out where the
1722      longjmp will land.  */
1723   if (jb_pc_offset == -1)
1724     return 0;
1725
1726   get_frame_register (frame, I386_ESP_REGNUM, buf);
1727   sp = extract_unsigned_integer (buf, 4, byte_order);
1728   if (target_read_memory (sp + 4, buf, 4))
1729     return 0;
1730
1731   jb_addr = extract_unsigned_integer (buf, 4, byte_order);
1732   if (target_read_memory (jb_addr + jb_pc_offset, buf, 4))
1733     return 0;
1734
1735   *pc = extract_unsigned_integer (buf, 4, byte_order);
1736   return 1;
1737 }
1738 \f
1739
1740 /* Check whether TYPE must be 16-byte-aligned when passed as a
1741    function argument.  16-byte vectors, _Decimal128 and structures or
1742    unions containing such types must be 16-byte-aligned; other
1743    arguments are 4-byte-aligned.  */
1744
1745 static int
1746 i386_16_byte_align_p (struct type *type)
1747 {
1748   type = check_typedef (type);
1749   if ((TYPE_CODE (type) == TYPE_CODE_DECFLOAT
1750        || (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)))
1751       && TYPE_LENGTH (type) == 16)
1752     return 1;
1753   if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1754     return i386_16_byte_align_p (TYPE_TARGET_TYPE (type));
1755   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1756       || TYPE_CODE (type) == TYPE_CODE_UNION)
1757     {
1758       int i;
1759       for (i = 0; i < TYPE_NFIELDS (type); i++)
1760         {
1761           if (i386_16_byte_align_p (TYPE_FIELD_TYPE (type, i)))
1762             return 1;
1763         }
1764     }
1765   return 0;
1766 }
1767
1768 static CORE_ADDR
1769 i386_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1770                       struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
1771                       struct value **args, CORE_ADDR sp, int struct_return,
1772                       CORE_ADDR struct_addr)
1773 {
1774   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1775   gdb_byte buf[4];
1776   int i;
1777   int write_pass;
1778   int args_space = 0;
1779
1780   /* Determine the total space required for arguments and struct
1781      return address in a first pass (allowing for 16-byte-aligned
1782      arguments), then push arguments in a second pass.  */
1783
1784   for (write_pass = 0; write_pass < 2; write_pass++)
1785     {
1786       int args_space_used = 0;
1787       int have_16_byte_aligned_arg = 0;
1788
1789       if (struct_return)
1790         {
1791           if (write_pass)
1792             {
1793               /* Push value address.  */
1794               store_unsigned_integer (buf, 4, byte_order, struct_addr);
1795               write_memory (sp, buf, 4);
1796               args_space_used += 4;
1797             }
1798           else
1799             args_space += 4;
1800         }
1801
1802       for (i = 0; i < nargs; i++)
1803         {
1804           int len = TYPE_LENGTH (value_enclosing_type (args[i]));
1805
1806           if (write_pass)
1807             {
1808               if (i386_16_byte_align_p (value_enclosing_type (args[i])))
1809                 args_space_used = align_up (args_space_used, 16);
1810
1811               write_memory (sp + args_space_used,
1812                             value_contents_all (args[i]), len);
1813               /* The System V ABI says that:
1814
1815               "An argument's size is increased, if necessary, to make it a
1816               multiple of [32-bit] words.  This may require tail padding,
1817               depending on the size of the argument."
1818
1819               This makes sure the stack stays word-aligned.  */
1820               args_space_used += align_up (len, 4);
1821             }
1822           else
1823             {
1824               if (i386_16_byte_align_p (value_enclosing_type (args[i])))
1825                 {
1826                   args_space = align_up (args_space, 16);
1827                   have_16_byte_aligned_arg = 1;
1828                 }
1829               args_space += align_up (len, 4);
1830             }
1831         }
1832
1833       if (!write_pass)
1834         {
1835           if (have_16_byte_aligned_arg)
1836             args_space = align_up (args_space, 16);
1837           sp -= args_space;
1838         }
1839     }
1840
1841   /* Store return address.  */
1842   sp -= 4;
1843   store_unsigned_integer (buf, 4, byte_order, bp_addr);
1844   write_memory (sp, buf, 4);
1845
1846   /* Finally, update the stack pointer...  */
1847   store_unsigned_integer (buf, 4, byte_order, sp);
1848   regcache_cooked_write (regcache, I386_ESP_REGNUM, buf);
1849
1850   /* ...and fake a frame pointer.  */
1851   regcache_cooked_write (regcache, I386_EBP_REGNUM, buf);
1852
1853   /* MarkK wrote: This "+ 8" is all over the place:
1854      (i386_frame_this_id, i386_sigtramp_frame_this_id,
1855      i386_dummy_id).  It's there, since all frame unwinders for
1856      a given target have to agree (within a certain margin) on the
1857      definition of the stack address of a frame.  Otherwise frame id
1858      comparison might not work correctly.  Since DWARF2/GCC uses the
1859      stack address *before* the function call as a frame's CFA.  On
1860      the i386, when %ebp is used as a frame pointer, the offset
1861      between the contents %ebp and the CFA as defined by GCC.  */
1862   return sp + 8;
1863 }
1864
1865 /* These registers are used for returning integers (and on some
1866    targets also for returning `struct' and `union' values when their
1867    size and alignment match an integer type).  */
1868 #define LOW_RETURN_REGNUM       I386_EAX_REGNUM /* %eax */
1869 #define HIGH_RETURN_REGNUM      I386_EDX_REGNUM /* %edx */
1870
1871 /* Read, for architecture GDBARCH, a function return value of TYPE
1872    from REGCACHE, and copy that into VALBUF.  */
1873
1874 static void
1875 i386_extract_return_value (struct gdbarch *gdbarch, struct type *type,
1876                            struct regcache *regcache, gdb_byte *valbuf)
1877 {
1878   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1879   int len = TYPE_LENGTH (type);
1880   gdb_byte buf[I386_MAX_REGISTER_SIZE];
1881
1882   if (TYPE_CODE (type) == TYPE_CODE_FLT)
1883     {
1884       if (tdep->st0_regnum < 0)
1885         {
1886           warning (_("Cannot find floating-point return value."));
1887           memset (valbuf, 0, len);
1888           return;
1889         }
1890
1891       /* Floating-point return values can be found in %st(0).  Convert
1892          its contents to the desired type.  This is probably not
1893          exactly how it would happen on the target itself, but it is
1894          the best we can do.  */
1895       regcache_raw_read (regcache, I386_ST0_REGNUM, buf);
1896       convert_typed_floating (buf, i387_ext_type (gdbarch), valbuf, type);
1897     }
1898   else
1899     {
1900       int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
1901       int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
1902
1903       if (len <= low_size)
1904         {
1905           regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
1906           memcpy (valbuf, buf, len);
1907         }
1908       else if (len <= (low_size + high_size))
1909         {
1910           regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
1911           memcpy (valbuf, buf, low_size);
1912           regcache_raw_read (regcache, HIGH_RETURN_REGNUM, buf);
1913           memcpy (valbuf + low_size, buf, len - low_size);
1914         }
1915       else
1916         internal_error (__FILE__, __LINE__,
1917                         _("Cannot extract return value of %d bytes long."), len);
1918     }
1919 }
1920
1921 /* Write, for architecture GDBARCH, a function return value of TYPE
1922    from VALBUF into REGCACHE.  */
1923
1924 static void
1925 i386_store_return_value (struct gdbarch *gdbarch, struct type *type,
1926                          struct regcache *regcache, const gdb_byte *valbuf)
1927 {
1928   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1929   int len = TYPE_LENGTH (type);
1930
1931   if (TYPE_CODE (type) == TYPE_CODE_FLT)
1932     {
1933       ULONGEST fstat;
1934       gdb_byte buf[I386_MAX_REGISTER_SIZE];
1935
1936       if (tdep->st0_regnum < 0)
1937         {
1938           warning (_("Cannot set floating-point return value."));
1939           return;
1940         }
1941
1942       /* Returning floating-point values is a bit tricky.  Apart from
1943          storing the return value in %st(0), we have to simulate the
1944          state of the FPU at function return point.  */
1945
1946       /* Convert the value found in VALBUF to the extended
1947          floating-point format used by the FPU.  This is probably
1948          not exactly how it would happen on the target itself, but
1949          it is the best we can do.  */
1950       convert_typed_floating (valbuf, type, buf, i387_ext_type (gdbarch));
1951       regcache_raw_write (regcache, I386_ST0_REGNUM, buf);
1952
1953       /* Set the top of the floating-point register stack to 7.  The
1954          actual value doesn't really matter, but 7 is what a normal
1955          function return would end up with if the program started out
1956          with a freshly initialized FPU.  */
1957       regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
1958       fstat |= (7 << 11);
1959       regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM (tdep), fstat);
1960
1961       /* Mark %st(1) through %st(7) as empty.  Since we set the top of
1962          the floating-point register stack to 7, the appropriate value
1963          for the tag word is 0x3fff.  */
1964       regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM (tdep), 0x3fff);
1965     }
1966   else
1967     {
1968       int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
1969       int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
1970
1971       if (len <= low_size)
1972         regcache_raw_write_part (regcache, LOW_RETURN_REGNUM, 0, len, valbuf);
1973       else if (len <= (low_size + high_size))
1974         {
1975           regcache_raw_write (regcache, LOW_RETURN_REGNUM, valbuf);
1976           regcache_raw_write_part (regcache, HIGH_RETURN_REGNUM, 0,
1977                                    len - low_size, valbuf + low_size);
1978         }
1979       else
1980         internal_error (__FILE__, __LINE__,
1981                         _("Cannot store return value of %d bytes long."), len);
1982     }
1983 }
1984 \f
1985
1986 /* This is the variable that is set with "set struct-convention", and
1987    its legitimate values.  */
1988 static const char default_struct_convention[] = "default";
1989 static const char pcc_struct_convention[] = "pcc";
1990 static const char reg_struct_convention[] = "reg";
1991 static const char *valid_conventions[] =
1992 {
1993   default_struct_convention,
1994   pcc_struct_convention,
1995   reg_struct_convention,
1996   NULL
1997 };
1998 static const char *struct_convention = default_struct_convention;
1999
2000 /* Return non-zero if TYPE, which is assumed to be a structure,
2001    a union type, or an array type, should be returned in registers
2002    for architecture GDBARCH.  */
2003
2004 static int
2005 i386_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
2006 {
2007   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2008   enum type_code code = TYPE_CODE (type);
2009   int len = TYPE_LENGTH (type);
2010
2011   gdb_assert (code == TYPE_CODE_STRUCT
2012               || code == TYPE_CODE_UNION
2013               || code == TYPE_CODE_ARRAY);
2014
2015   if (struct_convention == pcc_struct_convention
2016       || (struct_convention == default_struct_convention
2017           && tdep->struct_return == pcc_struct_return))
2018     return 0;
2019
2020   /* Structures consisting of a single `float', `double' or 'long
2021      double' member are returned in %st(0).  */
2022   if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
2023     {
2024       type = check_typedef (TYPE_FIELD_TYPE (type, 0));
2025       if (TYPE_CODE (type) == TYPE_CODE_FLT)
2026         return (len == 4 || len == 8 || len == 12);
2027     }
2028
2029   return (len == 1 || len == 2 || len == 4 || len == 8);
2030 }
2031
2032 /* Determine, for architecture GDBARCH, how a return value of TYPE
2033    should be returned.  If it is supposed to be returned in registers,
2034    and READBUF is non-zero, read the appropriate value from REGCACHE,
2035    and copy it into READBUF.  If WRITEBUF is non-zero, write the value
2036    from WRITEBUF into REGCACHE.  */
2037
2038 static enum return_value_convention
2039 i386_return_value (struct gdbarch *gdbarch, struct type *func_type,
2040                    struct type *type, struct regcache *regcache,
2041                    gdb_byte *readbuf, const gdb_byte *writebuf)
2042 {
2043   enum type_code code = TYPE_CODE (type);
2044
2045   if (((code == TYPE_CODE_STRUCT
2046         || code == TYPE_CODE_UNION
2047         || code == TYPE_CODE_ARRAY)
2048        && !i386_reg_struct_return_p (gdbarch, type))
2049       /* 128-bit decimal float uses the struct return convention.  */
2050       || (code == TYPE_CODE_DECFLOAT && TYPE_LENGTH (type) == 16))
2051     {
2052       /* The System V ABI says that:
2053
2054          "A function that returns a structure or union also sets %eax
2055          to the value of the original address of the caller's area
2056          before it returns.  Thus when the caller receives control
2057          again, the address of the returned object resides in register
2058          %eax and can be used to access the object."
2059
2060          So the ABI guarantees that we can always find the return
2061          value just after the function has returned.  */
2062
2063       /* Note that the ABI doesn't mention functions returning arrays,
2064          which is something possible in certain languages such as Ada.
2065          In this case, the value is returned as if it was wrapped in
2066          a record, so the convention applied to records also applies
2067          to arrays.  */
2068
2069       if (readbuf)
2070         {
2071           ULONGEST addr;
2072
2073           regcache_raw_read_unsigned (regcache, I386_EAX_REGNUM, &addr);
2074           read_memory (addr, readbuf, TYPE_LENGTH (type));
2075         }
2076
2077       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
2078     }
2079
2080   /* This special case is for structures consisting of a single
2081      `float', `double' or 'long double' member.  These structures are
2082      returned in %st(0).  For these structures, we call ourselves
2083      recursively, changing TYPE into the type of the first member of
2084      the structure.  Since that should work for all structures that
2085      have only one member, we don't bother to check the member's type
2086      here.  */
2087   if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
2088     {
2089       type = check_typedef (TYPE_FIELD_TYPE (type, 0));
2090       return i386_return_value (gdbarch, func_type, type, regcache,
2091                                 readbuf, writebuf);
2092     }
2093
2094   if (readbuf)
2095     i386_extract_return_value (gdbarch, type, regcache, readbuf);
2096   if (writebuf)
2097     i386_store_return_value (gdbarch, type, regcache, writebuf);
2098
2099   return RETURN_VALUE_REGISTER_CONVENTION;
2100 }
2101 \f
2102
2103 /* Construct types for ISA-specific registers.  */
2104 struct type *
2105 i386_eflags_type (struct gdbarch *gdbarch)
2106 {
2107   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2108
2109   if (!tdep->i386_eflags_type)
2110     {
2111       struct type *type;
2112
2113       type = arch_flags_type (gdbarch, "builtin_type_i386_eflags", 4);
2114       append_flags_type_flag (type, 0, "CF");
2115       append_flags_type_flag (type, 1, NULL);
2116       append_flags_type_flag (type, 2, "PF");
2117       append_flags_type_flag (type, 4, "AF");
2118       append_flags_type_flag (type, 6, "ZF");
2119       append_flags_type_flag (type, 7, "SF");
2120       append_flags_type_flag (type, 8, "TF");
2121       append_flags_type_flag (type, 9, "IF");
2122       append_flags_type_flag (type, 10, "DF");
2123       append_flags_type_flag (type, 11, "OF");
2124       append_flags_type_flag (type, 14, "NT");
2125       append_flags_type_flag (type, 16, "RF");
2126       append_flags_type_flag (type, 17, "VM");
2127       append_flags_type_flag (type, 18, "AC");
2128       append_flags_type_flag (type, 19, "VIF");
2129       append_flags_type_flag (type, 20, "VIP");
2130       append_flags_type_flag (type, 21, "ID");
2131
2132       tdep->i386_eflags_type = type;
2133     }
2134
2135   return tdep->i386_eflags_type;
2136 }
2137
2138 struct type *
2139 i386_mxcsr_type (struct gdbarch *gdbarch)
2140 {
2141   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2142
2143   if (!tdep->i386_mxcsr_type)
2144     {
2145       struct type *type;
2146
2147       type = arch_flags_type (gdbarch, "builtin_type_i386_mxcsr", 4);
2148       append_flags_type_flag (type, 0, "IE");
2149       append_flags_type_flag (type, 1, "DE");
2150       append_flags_type_flag (type, 2, "ZE");
2151       append_flags_type_flag (type, 3, "OE");
2152       append_flags_type_flag (type, 4, "UE");
2153       append_flags_type_flag (type, 5, "PE");
2154       append_flags_type_flag (type, 6, "DAZ");
2155       append_flags_type_flag (type, 7, "IM");
2156       append_flags_type_flag (type, 8, "DM");
2157       append_flags_type_flag (type, 9, "ZM");
2158       append_flags_type_flag (type, 10, "OM");
2159       append_flags_type_flag (type, 11, "UM");
2160       append_flags_type_flag (type, 12, "PM");
2161       append_flags_type_flag (type, 15, "FZ");
2162
2163       tdep->i386_mxcsr_type = type;
2164     }
2165
2166   return tdep->i386_mxcsr_type;
2167 }
2168
2169 struct type *
2170 i387_ext_type (struct gdbarch *gdbarch)
2171 {
2172   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2173
2174   if (!tdep->i387_ext_type)
2175     tdep->i387_ext_type
2176       = arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
2177                          floatformats_i387_ext);
2178
2179   return tdep->i387_ext_type;
2180 }
2181
2182 /* Construct vector type for MMX registers.  */
2183 struct type *
2184 i386_mmx_type (struct gdbarch *gdbarch)
2185 {
2186   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2187
2188   if (!tdep->i386_mmx_type)
2189     {
2190       const struct builtin_type *bt = builtin_type (gdbarch);
2191
2192       /* The type we're building is this: */
2193 #if 0
2194       union __gdb_builtin_type_vec64i
2195       {
2196         int64_t uint64;
2197         int32_t v2_int32[2];
2198         int16_t v4_int16[4];
2199         int8_t v8_int8[8];
2200       };
2201 #endif
2202
2203       struct type *t;
2204
2205       t = arch_composite_type (gdbarch,
2206                                "__gdb_builtin_type_vec64i", TYPE_CODE_UNION);
2207
2208       append_composite_type_field (t, "uint64", bt->builtin_int64);
2209       append_composite_type_field (t, "v2_int32",
2210                                    init_vector_type (bt->builtin_int32, 2));
2211       append_composite_type_field (t, "v4_int16",
2212                                    init_vector_type (bt->builtin_int16, 4));
2213       append_composite_type_field (t, "v8_int8",
2214                                    init_vector_type (bt->builtin_int8, 8));
2215
2216       TYPE_VECTOR (t) = 1;
2217       TYPE_NAME (t) = "builtin_type_vec64i";
2218       tdep->i386_mmx_type = t;
2219     }
2220
2221   return tdep->i386_mmx_type;
2222 }
2223
2224 struct type *
2225 i386_sse_type (struct gdbarch *gdbarch)
2226 {
2227   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2228
2229   if (!tdep->i386_sse_type)
2230     {
2231       const struct builtin_type *bt = builtin_type (gdbarch);
2232
2233       /* The type we're building is this: */
2234 #if 0
2235       union __gdb_builtin_type_vec128i
2236       {
2237         int128_t uint128;
2238         int64_t v2_int64[2];
2239         int32_t v4_int32[4];
2240         int16_t v8_int16[8];
2241         int8_t v16_int8[16];
2242         double v2_double[2];
2243         float v4_float[4];
2244       };
2245 #endif
2246
2247       struct type *t;
2248
2249       t = arch_composite_type (gdbarch,
2250                                "__gdb_builtin_type_vec128i", TYPE_CODE_UNION);
2251       append_composite_type_field (t, "v4_float",
2252                                    init_vector_type (bt->builtin_float, 4));
2253       append_composite_type_field (t, "v2_double",
2254                                    init_vector_type (bt->builtin_double, 2));
2255       append_composite_type_field (t, "v16_int8",
2256                                    init_vector_type (bt->builtin_int8, 16));
2257       append_composite_type_field (t, "v8_int16",
2258                                    init_vector_type (bt->builtin_int16, 8));
2259       append_composite_type_field (t, "v4_int32",
2260                                    init_vector_type (bt->builtin_int32, 4));
2261       append_composite_type_field (t, "v2_int64",
2262                                    init_vector_type (bt->builtin_int64, 2));
2263       append_composite_type_field (t, "uint128", bt->builtin_int128);
2264
2265       TYPE_VECTOR (t) = 1;
2266       TYPE_NAME (t) = "builtin_type_vec128i";
2267       tdep->i386_sse_type = t;
2268     }
2269
2270   return tdep->i386_sse_type;
2271 }
2272
2273 /* Return the GDB type object for the "standard" data type of data in
2274    register REGNUM.  Perhaps %esi and %edi should go here, but
2275    potentially they could be used for things other than address.  */
2276
2277 static struct type *
2278 i386_register_type (struct gdbarch *gdbarch, int regnum)
2279 {
2280   if (regnum == I386_EIP_REGNUM)
2281     return builtin_type (gdbarch)->builtin_func_ptr;
2282
2283   if (regnum == I386_EFLAGS_REGNUM)
2284     return i386_eflags_type (gdbarch);
2285
2286   if (regnum == I386_EBP_REGNUM || regnum == I386_ESP_REGNUM)
2287     return builtin_type (gdbarch)->builtin_data_ptr;
2288
2289   if (i386_fp_regnum_p (gdbarch, regnum))
2290     return i387_ext_type (gdbarch);
2291
2292   if (i386_mmx_regnum_p (gdbarch, regnum))
2293     return i386_mmx_type (gdbarch);
2294
2295   if (i386_sse_regnum_p (gdbarch, regnum))
2296     return i386_sse_type (gdbarch);
2297
2298   if (regnum == I387_MXCSR_REGNUM (gdbarch_tdep (gdbarch)))
2299     return i386_mxcsr_type (gdbarch);
2300
2301   return builtin_type (gdbarch)->builtin_int;
2302 }
2303
2304 /* Map a cooked register onto a raw register or memory.  For the i386,
2305    the MMX registers need to be mapped onto floating point registers.  */
2306
2307 static int
2308 i386_mmx_regnum_to_fp_regnum (struct regcache *regcache, int regnum)
2309 {
2310   struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
2311   int mmxreg, fpreg;
2312   ULONGEST fstat;
2313   int tos;
2314
2315   mmxreg = regnum - tdep->mm0_regnum;
2316   regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
2317   tos = (fstat >> 11) & 0x7;
2318   fpreg = (mmxreg + tos) % 8;
2319
2320   return (I387_ST0_REGNUM (tdep) + fpreg);
2321 }
2322
2323 static void
2324 i386_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
2325                            int regnum, gdb_byte *buf)
2326 {
2327   if (i386_mmx_regnum_p (gdbarch, regnum))
2328     {
2329       gdb_byte mmx_buf[MAX_REGISTER_SIZE];
2330       int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
2331
2332       /* Extract (always little endian).  */
2333       regcache_raw_read (regcache, fpnum, mmx_buf);
2334       memcpy (buf, mmx_buf, register_size (gdbarch, regnum));
2335     }
2336   else
2337     regcache_raw_read (regcache, regnum, buf);
2338 }
2339
2340 static void
2341 i386_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
2342                             int regnum, const gdb_byte *buf)
2343 {
2344   if (i386_mmx_regnum_p (gdbarch, regnum))
2345     {
2346       gdb_byte mmx_buf[MAX_REGISTER_SIZE];
2347       int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
2348
2349       /* Read ...  */
2350       regcache_raw_read (regcache, fpnum, mmx_buf);
2351       /* ... Modify ... (always little endian).  */
2352       memcpy (mmx_buf, buf, register_size (gdbarch, regnum));
2353       /* ... Write.  */
2354       regcache_raw_write (regcache, fpnum, mmx_buf);
2355     }
2356   else
2357     regcache_raw_write (regcache, regnum, buf);
2358 }
2359 \f
2360
2361 /* Return the register number of the register allocated by GCC after
2362    REGNUM, or -1 if there is no such register.  */
2363
2364 static int
2365 i386_next_regnum (int regnum)
2366 {
2367   /* GCC allocates the registers in the order:
2368
2369      %eax, %edx, %ecx, %ebx, %esi, %edi, %ebp, %esp, ...
2370
2371      Since storing a variable in %esp doesn't make any sense we return
2372      -1 for %ebp and for %esp itself.  */
2373   static int next_regnum[] =
2374   {
2375     I386_EDX_REGNUM,            /* Slot for %eax.  */
2376     I386_EBX_REGNUM,            /* Slot for %ecx.  */
2377     I386_ECX_REGNUM,            /* Slot for %edx.  */
2378     I386_ESI_REGNUM,            /* Slot for %ebx.  */
2379     -1, -1,                     /* Slots for %esp and %ebp.  */
2380     I386_EDI_REGNUM,            /* Slot for %esi.  */
2381     I386_EBP_REGNUM             /* Slot for %edi.  */
2382   };
2383
2384   if (regnum >= 0 && regnum < sizeof (next_regnum) / sizeof (next_regnum[0]))
2385     return next_regnum[regnum];
2386
2387   return -1;
2388 }
2389
2390 /* Return nonzero if a value of type TYPE stored in register REGNUM
2391    needs any special handling.  */
2392
2393 static int
2394 i386_convert_register_p (struct gdbarch *gdbarch, int regnum, struct type *type)
2395 {
2396   int len = TYPE_LENGTH (type);
2397
2398   /* Values may be spread across multiple registers.  Most debugging
2399      formats aren't expressive enough to specify the locations, so
2400      some heuristics is involved.  Right now we only handle types that
2401      have a length that is a multiple of the word size, since GCC
2402      doesn't seem to put any other types into registers.  */
2403   if (len > 4 && len % 4 == 0)
2404     {
2405       int last_regnum = regnum;
2406
2407       while (len > 4)
2408         {
2409           last_regnum = i386_next_regnum (last_regnum);
2410           len -= 4;
2411         }
2412
2413       if (last_regnum != -1)
2414         return 1;
2415     }
2416
2417   return i387_convert_register_p (gdbarch, regnum, type);
2418 }
2419
2420 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
2421    return its contents in TO.  */
2422
2423 static void
2424 i386_register_to_value (struct frame_info *frame, int regnum,
2425                         struct type *type, gdb_byte *to)
2426 {
2427   struct gdbarch *gdbarch = get_frame_arch (frame);
2428   int len = TYPE_LENGTH (type);
2429
2430   /* FIXME: kettenis/20030609: What should we do if REGNUM isn't
2431      available in FRAME (i.e. if it wasn't saved)?  */
2432
2433   if (i386_fp_regnum_p (gdbarch, regnum))
2434     {
2435       i387_register_to_value (frame, regnum, type, to);
2436       return;
2437     }
2438
2439   /* Read a value spread across multiple registers.  */
2440
2441   gdb_assert (len > 4 && len % 4 == 0);
2442
2443   while (len > 0)
2444     {
2445       gdb_assert (regnum != -1);
2446       gdb_assert (register_size (gdbarch, regnum) == 4);
2447
2448       get_frame_register (frame, regnum, to);
2449       regnum = i386_next_regnum (regnum);
2450       len -= 4;
2451       to += 4;
2452     }
2453 }
2454
2455 /* Write the contents FROM of a value of type TYPE into register
2456    REGNUM in frame FRAME.  */
2457
2458 static void
2459 i386_value_to_register (struct frame_info *frame, int regnum,
2460                         struct type *type, const gdb_byte *from)
2461 {
2462   int len = TYPE_LENGTH (type);
2463
2464   if (i386_fp_regnum_p (get_frame_arch (frame), regnum))
2465     {
2466       i387_value_to_register (frame, regnum, type, from);
2467       return;
2468     }
2469
2470   /* Write a value spread across multiple registers.  */
2471
2472   gdb_assert (len > 4 && len % 4 == 0);
2473
2474   while (len > 0)
2475     {
2476       gdb_assert (regnum != -1);
2477       gdb_assert (register_size (get_frame_arch (frame), regnum) == 4);
2478
2479       put_frame_register (frame, regnum, from);
2480       regnum = i386_next_regnum (regnum);
2481       len -= 4;
2482       from += 4;
2483     }
2484 }
2485 \f
2486 /* Supply register REGNUM from the buffer specified by GREGS and LEN
2487    in the general-purpose register set REGSET to register cache
2488    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
2489
2490 void
2491 i386_supply_gregset (const struct regset *regset, struct regcache *regcache,
2492                      int regnum, const void *gregs, size_t len)
2493 {
2494   const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
2495   const gdb_byte *regs = gregs;
2496   int i;
2497
2498   gdb_assert (len == tdep->sizeof_gregset);
2499
2500   for (i = 0; i < tdep->gregset_num_regs; i++)
2501     {
2502       if ((regnum == i || regnum == -1)
2503           && tdep->gregset_reg_offset[i] != -1)
2504         regcache_raw_supply (regcache, i, regs + tdep->gregset_reg_offset[i]);
2505     }
2506 }
2507
2508 /* Collect register REGNUM from the register cache REGCACHE and store
2509    it in the buffer specified by GREGS and LEN as described by the
2510    general-purpose register set REGSET.  If REGNUM is -1, do this for
2511    all registers in REGSET.  */
2512
2513 void
2514 i386_collect_gregset (const struct regset *regset,
2515                       const struct regcache *regcache,
2516                       int regnum, void *gregs, size_t len)
2517 {
2518   const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
2519   gdb_byte *regs = gregs;
2520   int i;
2521
2522   gdb_assert (len == tdep->sizeof_gregset);
2523
2524   for (i = 0; i < tdep->gregset_num_regs; i++)
2525     {
2526       if ((regnum == i || regnum == -1)
2527           && tdep->gregset_reg_offset[i] != -1)
2528         regcache_raw_collect (regcache, i, regs + tdep->gregset_reg_offset[i]);
2529     }
2530 }
2531
2532 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
2533    in the floating-point register set REGSET to register cache
2534    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
2535
2536 static void
2537 i386_supply_fpregset (const struct regset *regset, struct regcache *regcache,
2538                       int regnum, const void *fpregs, size_t len)
2539 {
2540   const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
2541
2542   if (len == I387_SIZEOF_FXSAVE)
2543     {
2544       i387_supply_fxsave (regcache, regnum, fpregs);
2545       return;
2546     }
2547
2548   gdb_assert (len == tdep->sizeof_fpregset);
2549   i387_supply_fsave (regcache, regnum, fpregs);
2550 }
2551
2552 /* Collect register REGNUM from the register cache REGCACHE and store
2553    it in the buffer specified by FPREGS and LEN as described by the
2554    floating-point register set REGSET.  If REGNUM is -1, do this for
2555    all registers in REGSET.  */
2556
2557 static void
2558 i386_collect_fpregset (const struct regset *regset,
2559                        const struct regcache *regcache,
2560                        int regnum, void *fpregs, size_t len)
2561 {
2562   const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
2563
2564   if (len == I387_SIZEOF_FXSAVE)
2565     {
2566       i387_collect_fxsave (regcache, regnum, fpregs);
2567       return;
2568     }
2569
2570   gdb_assert (len == tdep->sizeof_fpregset);
2571   i387_collect_fsave (regcache, regnum, fpregs);
2572 }
2573
2574 /* Return the appropriate register set for the core section identified
2575    by SECT_NAME and SECT_SIZE.  */
2576
2577 const struct regset *
2578 i386_regset_from_core_section (struct gdbarch *gdbarch,
2579                                const char *sect_name, size_t sect_size)
2580 {
2581   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2582
2583   if (strcmp (sect_name, ".reg") == 0 && sect_size == tdep->sizeof_gregset)
2584     {
2585       if (tdep->gregset == NULL)
2586         tdep->gregset = regset_alloc (gdbarch, i386_supply_gregset,
2587                                       i386_collect_gregset);
2588       return tdep->gregset;
2589     }
2590
2591   if ((strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
2592       || (strcmp (sect_name, ".reg-xfp") == 0
2593           && sect_size == I387_SIZEOF_FXSAVE))
2594     {
2595       if (tdep->fpregset == NULL)
2596         tdep->fpregset = regset_alloc (gdbarch, i386_supply_fpregset,
2597                                        i386_collect_fpregset);
2598       return tdep->fpregset;
2599     }
2600
2601   return NULL;
2602 }
2603 \f
2604
2605 /* Stuff for WIN32 PE style DLL's but is pretty generic really.  */
2606
2607 CORE_ADDR
2608 i386_pe_skip_trampoline_code (struct frame_info *frame,
2609                               CORE_ADDR pc, char *name)
2610 {
2611   struct gdbarch *gdbarch = get_frame_arch (frame);
2612   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2613
2614   /* jmp *(dest) */
2615   if (pc && read_memory_unsigned_integer (pc, 2, byte_order) == 0x25ff)
2616     {
2617       unsigned long indirect =
2618         read_memory_unsigned_integer (pc + 2, 4, byte_order);
2619       struct minimal_symbol *indsym =
2620         indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
2621       char *symname = indsym ? SYMBOL_LINKAGE_NAME (indsym) : 0;
2622
2623       if (symname)
2624         {
2625           if (strncmp (symname, "__imp_", 6) == 0
2626               || strncmp (symname, "_imp_", 5) == 0)
2627             return name ? 1 :
2628                    read_memory_unsigned_integer (indirect, 4, byte_order);
2629         }
2630     }
2631   return 0;                     /* Not a trampoline.  */
2632 }
2633 \f
2634
2635 /* Return whether the THIS_FRAME corresponds to a sigtramp
2636    routine.  */
2637
2638 int
2639 i386_sigtramp_p (struct frame_info *this_frame)
2640 {
2641   CORE_ADDR pc = get_frame_pc (this_frame);
2642   char *name;
2643
2644   find_pc_partial_function (pc, &name, NULL, NULL);
2645   return (name && strcmp ("_sigtramp", name) == 0);
2646 }
2647 \f
2648
2649 /* We have two flavours of disassembly.  The machinery on this page
2650    deals with switching between those.  */
2651
2652 static int
2653 i386_print_insn (bfd_vma pc, struct disassemble_info *info)
2654 {
2655   gdb_assert (disassembly_flavor == att_flavor
2656               || disassembly_flavor == intel_flavor);
2657
2658   /* FIXME: kettenis/20020915: Until disassembler_options is properly
2659      constified, cast to prevent a compiler warning.  */
2660   info->disassembler_options = (char *) disassembly_flavor;
2661
2662   return print_insn_i386 (pc, info);
2663 }
2664 \f
2665
2666 /* There are a few i386 architecture variants that differ only
2667    slightly from the generic i386 target.  For now, we don't give them
2668    their own source file, but include them here.  As a consequence,
2669    they'll always be included.  */
2670
2671 /* System V Release 4 (SVR4).  */
2672
2673 /* Return whether THIS_FRAME corresponds to a SVR4 sigtramp
2674    routine.  */
2675
2676 static int
2677 i386_svr4_sigtramp_p (struct frame_info *this_frame)
2678 {
2679   CORE_ADDR pc = get_frame_pc (this_frame);
2680   char *name;
2681
2682   /* UnixWare uses _sigacthandler.  The origin of the other symbols is
2683      currently unknown.  */
2684   find_pc_partial_function (pc, &name, NULL, NULL);
2685   return (name && (strcmp ("_sigreturn", name) == 0
2686                    || strcmp ("_sigacthandler", name) == 0
2687                    || strcmp ("sigvechandler", name) == 0));
2688 }
2689
2690 /* Assuming THIS_FRAME is for a SVR4 sigtramp routine, return the
2691    address of the associated sigcontext (ucontext) structure.  */
2692
2693 static CORE_ADDR
2694 i386_svr4_sigcontext_addr (struct frame_info *this_frame)
2695 {
2696   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2697   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2698   gdb_byte buf[4];
2699   CORE_ADDR sp;
2700
2701   get_frame_register (this_frame, I386_ESP_REGNUM, buf);
2702   sp = extract_unsigned_integer (buf, 4, byte_order);
2703
2704   return read_memory_unsigned_integer (sp + 8, 4, byte_order);
2705 }
2706 \f
2707
2708 /* Generic ELF.  */
2709
2710 void
2711 i386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2712 {
2713   /* We typically use stabs-in-ELF with the SVR4 register numbering.  */
2714   set_gdbarch_stab_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
2715 }
2716
2717 /* System V Release 4 (SVR4).  */
2718
2719 void
2720 i386_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2721 {
2722   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2723
2724   /* System V Release 4 uses ELF.  */
2725   i386_elf_init_abi (info, gdbarch);
2726
2727   /* System V Release 4 has shared libraries.  */
2728   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
2729
2730   tdep->sigtramp_p = i386_svr4_sigtramp_p;
2731   tdep->sigcontext_addr = i386_svr4_sigcontext_addr;
2732   tdep->sc_pc_offset = 36 + 14 * 4;
2733   tdep->sc_sp_offset = 36 + 17 * 4;
2734
2735   tdep->jb_pc_offset = 20;
2736 }
2737
2738 /* DJGPP.  */
2739
2740 static void
2741 i386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2742 {
2743   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2744
2745   /* DJGPP doesn't have any special frames for signal handlers.  */
2746   tdep->sigtramp_p = NULL;
2747
2748   tdep->jb_pc_offset = 36;
2749
2750   /* DJGPP does not support the SSE registers.  */
2751   tdep->num_xmm_regs = 0;
2752   set_gdbarch_num_regs (gdbarch, I386_NUM_GREGS + I386_NUM_FREGS);
2753
2754   /* Native compiler is GCC, which uses the SVR4 register numbering
2755      even in COFF and STABS.  See the comment in i386_gdbarch_init,
2756      before the calls to set_gdbarch_stab_reg_to_regnum and
2757      set_gdbarch_sdb_reg_to_regnum.  */
2758   set_gdbarch_stab_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
2759   set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
2760 }
2761 \f
2762
2763 /* i386 register groups.  In addition to the normal groups, add "mmx"
2764    and "sse".  */
2765
2766 static struct reggroup *i386_sse_reggroup;
2767 static struct reggroup *i386_mmx_reggroup;
2768
2769 static void
2770 i386_init_reggroups (void)
2771 {
2772   i386_sse_reggroup = reggroup_new ("sse", USER_REGGROUP);
2773   i386_mmx_reggroup = reggroup_new ("mmx", USER_REGGROUP);
2774 }
2775
2776 static void
2777 i386_add_reggroups (struct gdbarch *gdbarch)
2778 {
2779   reggroup_add (gdbarch, i386_sse_reggroup);
2780   reggroup_add (gdbarch, i386_mmx_reggroup);
2781   reggroup_add (gdbarch, general_reggroup);
2782   reggroup_add (gdbarch, float_reggroup);
2783   reggroup_add (gdbarch, all_reggroup);
2784   reggroup_add (gdbarch, save_reggroup);
2785   reggroup_add (gdbarch, restore_reggroup);
2786   reggroup_add (gdbarch, vector_reggroup);
2787   reggroup_add (gdbarch, system_reggroup);
2788 }
2789
2790 int
2791 i386_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
2792                           struct reggroup *group)
2793 {
2794   int sse_regnum_p = (i386_sse_regnum_p (gdbarch, regnum)
2795                       || i386_mxcsr_regnum_p (gdbarch, regnum));
2796   int fp_regnum_p = (i386_fp_regnum_p (gdbarch, regnum)
2797                      || i386_fpc_regnum_p (gdbarch, regnum));
2798   int mmx_regnum_p = (i386_mmx_regnum_p (gdbarch, regnum));
2799
2800   if (group == i386_mmx_reggroup)
2801     return mmx_regnum_p;
2802   if (group == i386_sse_reggroup)
2803     return sse_regnum_p;
2804   if (group == vector_reggroup)
2805     return (mmx_regnum_p || sse_regnum_p);
2806   if (group == float_reggroup)
2807     return fp_regnum_p;
2808   if (group == general_reggroup)
2809     return (!fp_regnum_p && !mmx_regnum_p && !sse_regnum_p);
2810
2811   return default_register_reggroup_p (gdbarch, regnum, group);
2812 }
2813 \f
2814
2815 /* Get the ARGIth function argument for the current function.  */
2816
2817 static CORE_ADDR
2818 i386_fetch_pointer_argument (struct frame_info *frame, int argi, 
2819                              struct type *type)
2820 {
2821   struct gdbarch *gdbarch = get_frame_arch (frame);
2822   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2823   CORE_ADDR sp = get_frame_register_unsigned  (frame, I386_ESP_REGNUM);
2824   return read_memory_unsigned_integer (sp + (4 * (argi + 1)), 4, byte_order);
2825 }
2826
2827 static void
2828 i386_skip_permanent_breakpoint (struct regcache *regcache)
2829 {
2830   CORE_ADDR current_pc = regcache_read_pc (regcache);
2831
2832  /* On i386, breakpoint is exactly 1 byte long, so we just
2833     adjust the PC in the regcache.  */
2834   current_pc += 1;
2835   regcache_write_pc (regcache, current_pc);
2836 }
2837
2838
2839 #define PREFIX_REPZ     0x01
2840 #define PREFIX_REPNZ    0x02
2841 #define PREFIX_LOCK     0x04
2842 #define PREFIX_DATA     0x08
2843 #define PREFIX_ADDR     0x10
2844
2845 /* operand size */
2846 enum
2847 {
2848   OT_BYTE = 0,
2849   OT_WORD,
2850   OT_LONG,
2851   OT_QUAD,
2852 };
2853
2854 /* i386 arith/logic operations */
2855 enum
2856 {
2857   OP_ADDL,
2858   OP_ORL,
2859   OP_ADCL,
2860   OP_SBBL,
2861   OP_ANDL,
2862   OP_SUBL,
2863   OP_XORL,
2864   OP_CMPL,
2865 };
2866
2867 struct i386_record_s
2868 {
2869   struct gdbarch *gdbarch;
2870   struct regcache *regcache;
2871   CORE_ADDR orig_addr;
2872   CORE_ADDR addr;
2873   int aflag;
2874   int dflag;
2875   int override;
2876   uint8_t modrm;
2877   uint8_t mod, reg, rm;
2878   int ot;
2879   uint8_t rex_x;
2880   uint8_t rex_b;
2881   int rip_offset;
2882   int popl_esp_hack;
2883   const int *regmap;
2884 };
2885
2886 /* Parse "modrm" part in current memory address that irp->addr point to
2887    Return -1 if something wrong. */
2888
2889 static int
2890 i386_record_modrm (struct i386_record_s *irp)
2891 {
2892   struct gdbarch *gdbarch = irp->gdbarch;
2893
2894   if (target_read_memory (irp->addr, &irp->modrm, 1))
2895     {
2896       if (record_debug)
2897         printf_unfiltered (_("Process record: error reading memory at "
2898                              "addr %s len = 1.\n"),
2899                            paddress (gdbarch, irp->addr));
2900       return -1;
2901     }
2902   irp->addr++;
2903   irp->mod = (irp->modrm >> 6) & 3;
2904   irp->reg = (irp->modrm >> 3) & 7;
2905   irp->rm = irp->modrm & 7;
2906
2907   return 0;
2908 }
2909
2910 /* Get the memory address that current instruction  write to and set it to
2911    the argument "addr".
2912    Return -1 if something wrong. */
2913
2914 static int
2915 i386_record_lea_modrm_addr (struct i386_record_s *irp, uint64_t *addr)
2916 {
2917   struct gdbarch *gdbarch = irp->gdbarch;
2918   uint8_t tmpu8;
2919   int16_t tmpi16;
2920   int32_t tmpi32;
2921   ULONGEST tmpulongest;
2922
2923   *addr = 0;
2924   if (irp->aflag)
2925     {
2926       /* 32 bits */
2927       int havesib = 0;
2928       uint8_t scale = 0;
2929       uint8_t index = 0;
2930       uint8_t base = irp->rm;
2931
2932       if (base == 4)
2933         {
2934           havesib = 1;
2935           if (target_read_memory (irp->addr, &tmpu8, 1))
2936             {
2937               if (record_debug)
2938                 printf_unfiltered (_("Process record: error reading memory "
2939                                      "at addr %s len = 1.\n"),
2940                                    paddress (gdbarch, irp->addr));
2941               return -1;
2942             }
2943           irp->addr++;
2944           scale = (tmpu8 >> 6) & 3;
2945           index = ((tmpu8 >> 3) & 7) | irp->rex_x;
2946           base = (tmpu8 & 7);
2947         }
2948       base |= irp->rex_b;
2949
2950       switch (irp->mod)
2951         {
2952         case 0:
2953           if ((base & 7) == 5)
2954             {
2955               base = 0xff;
2956               if (target_read_memory (irp->addr, (gdb_byte *) &tmpi32, 4))
2957                 {
2958                   if (record_debug)
2959                     printf_unfiltered (_("Process record: error reading "
2960                                          "memory at addr %s len = 4.\n"),
2961                                        paddress (gdbarch, irp->addr));
2962                   return -1;
2963                 }
2964               irp->addr += 4;
2965               *addr = tmpi32;
2966               if (irp->regmap[X86_RECORD_R8_REGNUM] && !havesib)
2967                 *addr += irp->addr + irp->rip_offset;
2968             }
2969           else
2970             {
2971               *addr = 0;
2972             }
2973           break;
2974         case 1:
2975           if (target_read_memory (irp->addr, &tmpu8, 1))
2976             {
2977               if (record_debug)
2978                 printf_unfiltered (_("Process record: error reading memory "
2979                                      "at addr %s len = 1.\n"),
2980                                    paddress (gdbarch, irp->addr));
2981               return -1;
2982             }
2983           irp->addr++;
2984           *addr = (int8_t) tmpu8;
2985           break;
2986         case 2:
2987           if (target_read_memory (irp->addr, (gdb_byte *) &tmpi32, 4))
2988             {
2989               if (record_debug)
2990                 printf_unfiltered (_("Process record: error reading memory "
2991                                      "at addr %s len = 4.\n"),
2992                                    paddress (gdbarch, irp->addr));
2993               return -1;
2994             }
2995           *addr = tmpi32;
2996           irp->addr += 4;
2997           break;
2998         }
2999
3000       tmpulongest = 0;
3001       if (base != 0xff)
3002         {
3003           if (base == 4 && irp->popl_esp_hack)
3004             *addr += irp->popl_esp_hack;
3005           regcache_raw_read_unsigned (irp->regcache, irp->regmap[base],
3006                                       &tmpulongest);
3007         }
3008       if (irp->aflag == 2)
3009         {
3010           *addr += tmpulongest;
3011         }
3012       else
3013         *addr = (uint32_t) (tmpulongest + *addr);
3014
3015       if (havesib && (index != 4 || scale != 0))
3016         {
3017           regcache_raw_read_unsigned (irp->regcache, irp->regmap[index],
3018                                       &tmpulongest);
3019           if (irp->aflag == 2)
3020             *addr += tmpulongest << scale;
3021           else
3022             *addr = (uint32_t) (*addr + (tmpulongest << scale));
3023         }
3024     }
3025   else
3026     {
3027       /* 16 bits */
3028       switch (irp->mod)
3029         {
3030         case 0:
3031           if (irp->rm == 6)
3032             {
3033               if (target_read_memory
3034                   (irp->addr, (gdb_byte *) &tmpi16, 2))
3035                 {
3036                   if (record_debug)
3037                     printf_unfiltered (_("Process record: error reading "
3038                                          "memory at addr %s len = 2.\n"),
3039                                        paddress (gdbarch, irp->addr));
3040                   return -1;
3041                 }
3042               irp->addr += 2;
3043               *addr = tmpi16;
3044               irp->rm = 0;
3045               goto no_rm;
3046             }
3047           else
3048             {
3049               *addr = 0;
3050             }
3051           break;
3052         case 1:
3053           if (target_read_memory (irp->addr, &tmpu8, 1))
3054             {
3055               if (record_debug)
3056                 printf_unfiltered (_("Process record: error reading memory "
3057                                      "at addr %s len = 1.\n"),
3058                                    paddress (gdbarch, irp->addr));
3059               return -1;
3060             }
3061           irp->addr++;
3062           *addr = (int8_t) tmpu8;
3063           break;
3064         case 2:
3065           if (target_read_memory (irp->addr, (gdb_byte *) &tmpi16, 2))
3066             {
3067               if (record_debug)
3068                 printf_unfiltered (_("Process record: error reading memory "
3069                                      "at addr %s len = 2.\n"),
3070                                    paddress (gdbarch, irp->addr));
3071               return -1;
3072             }
3073           irp->addr += 2;
3074           *addr = tmpi16;
3075           break;
3076         }
3077
3078       switch (irp->rm)
3079         {
3080         case 0:
3081           regcache_raw_read_unsigned (irp->regcache,
3082                                       irp->regmap[X86_RECORD_REBX_REGNUM],
3083                                       &tmpulongest);
3084           *addr = (uint32_t) (*addr + tmpulongest);
3085           regcache_raw_read_unsigned (irp->regcache,
3086                                       irp->regmap[X86_RECORD_RESI_REGNUM],
3087                                       &tmpulongest);
3088           *addr = (uint32_t) (*addr + tmpulongest);
3089           break;
3090         case 1:
3091           regcache_raw_read_unsigned (irp->regcache,
3092                                       irp->regmap[X86_RECORD_REBX_REGNUM],
3093                                       &tmpulongest);
3094           *addr = (uint32_t) (*addr + tmpulongest);
3095           regcache_raw_read_unsigned (irp->regcache,
3096                                       irp->regmap[X86_RECORD_REDI_REGNUM],
3097                                       &tmpulongest);
3098           *addr = (uint32_t) (*addr + tmpulongest);
3099           break;
3100         case 2:
3101           regcache_raw_read_unsigned (irp->regcache,
3102                                       irp->regmap[X86_RECORD_REBP_REGNUM],
3103                                       &tmpulongest);
3104           *addr = (uint32_t) (*addr + tmpulongest);
3105           regcache_raw_read_unsigned (irp->regcache,
3106                                       irp->regmap[X86_RECORD_RESI_REGNUM],
3107                                       &tmpulongest);
3108           *addr = (uint32_t) (*addr + tmpulongest);
3109           break;
3110         case 3:
3111           regcache_raw_read_unsigned (irp->regcache,
3112                                       irp->regmap[X86_RECORD_REBP_REGNUM],
3113                                       &tmpulongest);
3114           *addr = (uint32_t) (*addr + tmpulongest);
3115           regcache_raw_read_unsigned (irp->regcache,
3116                                       irp->regmap[X86_RECORD_REDI_REGNUM],
3117                                       &tmpulongest);
3118           *addr = (uint32_t) (*addr + tmpulongest);
3119           break;
3120         case 4:
3121           regcache_raw_read_unsigned (irp->regcache,
3122                                       irp->regmap[X86_RECORD_RESI_REGNUM],
3123                                       &tmpulongest);
3124           *addr = (uint32_t) (*addr + tmpulongest);
3125           break;
3126         case 5:
3127           regcache_raw_read_unsigned (irp->regcache,
3128                                       irp->regmap[X86_RECORD_REDI_REGNUM],
3129                                       &tmpulongest);
3130           *addr = (uint32_t) (*addr + tmpulongest);
3131           break;
3132         case 6:
3133           regcache_raw_read_unsigned (irp->regcache,
3134                                       irp->regmap[X86_RECORD_REBP_REGNUM],
3135                                       &tmpulongest);
3136           *addr = (uint32_t) (*addr + tmpulongest);
3137           break;
3138         case 7:
3139           regcache_raw_read_unsigned (irp->regcache,
3140                                       irp->regmap[X86_RECORD_REBX_REGNUM],
3141                                       &tmpulongest);
3142           *addr = (uint32_t) (*addr + tmpulongest);
3143           break;
3144         }
3145       *addr &= 0xffff;
3146     }
3147
3148  no_rm:
3149   return 0;
3150 }
3151
3152 /* Record the value of the memory that willbe changed in current instruction
3153    to "record_arch_list".
3154    Return -1 if something wrong. */
3155
3156 static int
3157 i386_record_lea_modrm (struct i386_record_s *irp)
3158 {
3159   struct gdbarch *gdbarch = irp->gdbarch;
3160   uint64_t addr;
3161
3162   if (irp->override >= 0)
3163     {
3164       warning (_("Process record ignores the memory change "
3165                  "of instruction at address %s because it "
3166                  "can't get the value of the segment register."),
3167                paddress (gdbarch, irp->orig_addr));
3168       return 0;
3169     }
3170
3171   if (i386_record_lea_modrm_addr (irp, &addr))
3172     return -1;
3173
3174   if (record_arch_list_add_mem (addr, 1 << irp->ot))
3175     return -1;
3176
3177   return 0;
3178 }
3179
3180 /* Record the push operation to "record_arch_list".
3181    Return -1 if something wrong. */
3182
3183 static int
3184 i386_record_push (struct i386_record_s *irp, int size)
3185 {
3186   ULONGEST tmpulongest;
3187
3188   if (record_arch_list_add_reg (irp->regcache,
3189                                 irp->regmap[X86_RECORD_RESP_REGNUM]))
3190     return -1;
3191   regcache_raw_read_unsigned (irp->regcache,
3192                               irp->regmap[X86_RECORD_RESP_REGNUM],
3193                               &tmpulongest);
3194   if (record_arch_list_add_mem ((CORE_ADDR) tmpulongest - size, size))
3195     return -1;
3196
3197   return 0;
3198 }
3199
3200
3201 /* Defines contents to record.  */
3202 #define I386_SAVE_FPU_REGS              0xfffd
3203 #define I386_SAVE_FPU_ENV               0xfffe
3204 #define I386_SAVE_FPU_ENV_REG_STACK     0xffff
3205
3206 /* Record the value of floating point registers which will be changed by the
3207    current instruction to "record_arch_list".  Return -1 if something is wrong.
3208 */
3209
3210 static int i386_record_floats (struct gdbarch *gdbarch,
3211                                struct i386_record_s *ir,
3212                                uint32_t iregnum)
3213 {
3214   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3215   int i;
3216
3217   /* Oza: Because of floating point insn push/pop of fpu stack is going to
3218      happen.  Currently we store st0-st7 registers, but we need not store all
3219      registers all the time, in future we use ftag register and record only
3220      those who are not marked as an empty.  */
3221
3222   if (I386_SAVE_FPU_REGS == iregnum)
3223     {
3224       for (i = I387_ST0_REGNUM (tdep); i <= I387_ST0_REGNUM (tdep) + 7; i++)
3225         {
3226           if (record_arch_list_add_reg (ir->regcache, i))
3227             return -1;
3228         }
3229     }
3230   else if (I386_SAVE_FPU_ENV == iregnum)
3231     {
3232       for (i = I387_FCTRL_REGNUM (tdep); i <= I387_FOP_REGNUM (tdep); i++)
3233               {
3234               if (record_arch_list_add_reg (ir->regcache, i))
3235                 return -1;
3236               }
3237     }
3238   else if (I386_SAVE_FPU_ENV_REG_STACK == iregnum)
3239     {
3240       for (i = I387_ST0_REGNUM (tdep); i <= I387_FOP_REGNUM (tdep); i++)
3241       {
3242         if (record_arch_list_add_reg (ir->regcache, i))
3243           return -1;
3244       }
3245     }
3246   else if ((iregnum >= I387_ST0_REGNUM (tdep)) &&
3247            (iregnum <= I387_FOP_REGNUM (tdep)))
3248     {
3249       if (record_arch_list_add_reg (ir->regcache,iregnum))
3250         return -1;
3251     }
3252   else
3253     {
3254       /* Parameter error.  */
3255       return -1;
3256     }
3257   if(I386_SAVE_FPU_ENV != iregnum)
3258     {
3259     for (i = I387_FCTRL_REGNUM (tdep); i <= I387_FOP_REGNUM (tdep); i++)
3260       {
3261       if (record_arch_list_add_reg (ir->regcache, i))
3262         return -1;
3263       }
3264     }
3265   return 0;
3266 }
3267
3268 /* Parse the current instruction and record the values of the registers and
3269    memory that will be changed in current instruction to "record_arch_list".
3270    Return -1 if something wrong. */
3271
3272 #define I386_RECORD_ARCH_LIST_ADD_REG(regnum) \
3273     record_arch_list_add_reg (ir.regcache, ir.regmap[(regnum)])
3274
3275 int
3276 i386_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
3277                      CORE_ADDR addr)
3278 {
3279   int prefixes = 0;
3280   uint8_t tmpu8;
3281   uint16_t tmpu16;
3282   uint32_t tmpu32;
3283   ULONGEST tmpulongest;
3284   uint32_t opcode;
3285   struct i386_record_s ir;
3286   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3287   int rex = 0;
3288   uint8_t rex_w = -1;
3289   uint8_t rex_r = 0;
3290
3291   memset (&ir, 0, sizeof (struct i386_record_s));
3292   ir.regcache = regcache;
3293   ir.addr = addr;
3294   ir.orig_addr = addr;
3295   ir.aflag = 1;
3296   ir.dflag = 1;
3297   ir.override = -1;
3298   ir.popl_esp_hack = 0;
3299   ir.regmap = gdbarch_tdep (gdbarch)->record_regmap;
3300   ir.gdbarch = gdbarch;
3301
3302   if (record_debug > 1)
3303     fprintf_unfiltered (gdb_stdlog, "Process record: i386_process_record "
3304                                     "addr = %s\n",
3305                         paddress (gdbarch, ir.addr));
3306
3307   /* prefixes */
3308   while (1)
3309     {
3310       if (target_read_memory (ir.addr, &tmpu8, 1))
3311         {
3312           if (record_debug)
3313             printf_unfiltered (_("Process record: error reading memory at "
3314                                  "addr %s len = 1.\n"),
3315                                paddress (gdbarch, ir.addr));
3316           return -1;
3317         }
3318       ir.addr++;
3319       switch (tmpu8)    /* Instruction prefixes */
3320         {
3321         case REPE_PREFIX_OPCODE:
3322           prefixes |= PREFIX_REPZ;
3323           break;
3324         case REPNE_PREFIX_OPCODE:
3325           prefixes |= PREFIX_REPNZ;
3326           break;
3327         case LOCK_PREFIX_OPCODE:
3328           prefixes |= PREFIX_LOCK;
3329           break;
3330         case CS_PREFIX_OPCODE:
3331           ir.override = X86_RECORD_CS_REGNUM;
3332           break;
3333         case SS_PREFIX_OPCODE:
3334           ir.override = X86_RECORD_SS_REGNUM;
3335           break;
3336         case DS_PREFIX_OPCODE:
3337           ir.override = X86_RECORD_DS_REGNUM;
3338           break;
3339         case ES_PREFIX_OPCODE:
3340           ir.override = X86_RECORD_ES_REGNUM;
3341           break;
3342         case FS_PREFIX_OPCODE:
3343           ir.override = X86_RECORD_FS_REGNUM;
3344           break;
3345         case GS_PREFIX_OPCODE:
3346           ir.override = X86_RECORD_GS_REGNUM;
3347           break;
3348         case DATA_PREFIX_OPCODE:
3349           prefixes |= PREFIX_DATA;
3350           break;
3351         case ADDR_PREFIX_OPCODE:
3352           prefixes |= PREFIX_ADDR;
3353           break;
3354         case 0x40:      /* i386 inc %eax */
3355         case 0x41:      /* i386 inc %ecx */
3356         case 0x42:      /* i386 inc %edx */
3357         case 0x43:      /* i386 inc %ebx */
3358         case 0x44:      /* i386 inc %esp */
3359         case 0x45:      /* i386 inc %ebp */
3360         case 0x46:      /* i386 inc %esi */
3361         case 0x47:      /* i386 inc %edi */
3362         case 0x48:      /* i386 dec %eax */
3363         case 0x49:      /* i386 dec %ecx */
3364         case 0x4a:      /* i386 dec %edx */
3365         case 0x4b:      /* i386 dec %ebx */
3366         case 0x4c:      /* i386 dec %esp */
3367         case 0x4d:      /* i386 dec %ebp */
3368         case 0x4e:      /* i386 dec %esi */
3369         case 0x4f:      /* i386 dec %edi */
3370           if (ir.regmap[X86_RECORD_R8_REGNUM])  /* 64 bit target */
3371             {
3372                /* REX */
3373                rex = 1;
3374                rex_w = (tmpu8 >> 3) & 1;
3375                rex_r = (tmpu8 & 0x4) << 1;
3376                ir.rex_x = (tmpu8 & 0x2) << 2;
3377                ir.rex_b = (tmpu8 & 0x1) << 3;
3378             }
3379           else                                  /* 32 bit target */
3380             goto out_prefixes;
3381           break;
3382         default:
3383           goto out_prefixes;
3384           break;
3385         }
3386     }
3387  out_prefixes:
3388   if (ir.regmap[X86_RECORD_R8_REGNUM] && rex_w == 1)
3389     {
3390       ir.dflag = 2;
3391     }
3392   else
3393     {
3394       if (prefixes & PREFIX_DATA)
3395         ir.dflag ^= 1;
3396     }
3397   if (prefixes & PREFIX_ADDR)
3398     ir.aflag ^= 1;
3399   else if (ir.regmap[X86_RECORD_R8_REGNUM])
3400     ir.aflag = 2;
3401
3402   /* now check op code */
3403   opcode = (uint32_t) tmpu8;
3404  reswitch:
3405   switch (opcode)
3406     {
3407     case 0x0f:
3408       if (target_read_memory (ir.addr, &tmpu8, 1))
3409         {
3410           if (record_debug)
3411             printf_unfiltered (_("Process record: error reading memory at "
3412                                  "addr %s len = 1.\n"),
3413                                paddress (gdbarch, ir.addr));
3414           return -1;
3415         }
3416       ir.addr++;
3417       opcode = (uint16_t) tmpu8 | 0x0f00;
3418       goto reswitch;
3419       break;
3420
3421     case 0x00:    /* arith & logic */
3422     case 0x01:
3423     case 0x02:
3424     case 0x03:
3425     case 0x04:
3426     case 0x05:
3427     case 0x08:
3428     case 0x09:
3429     case 0x0a:
3430     case 0x0b:
3431     case 0x0c:
3432     case 0x0d:
3433     case 0x10:
3434     case 0x11:
3435     case 0x12:
3436     case 0x13:
3437     case 0x14:
3438     case 0x15:
3439     case 0x18:
3440     case 0x19:
3441     case 0x1a:
3442     case 0x1b:
3443     case 0x1c:
3444     case 0x1d:
3445     case 0x20:
3446     case 0x21:
3447     case 0x22:
3448     case 0x23:
3449     case 0x24:
3450     case 0x25:
3451     case 0x28:
3452     case 0x29:
3453     case 0x2a:
3454     case 0x2b:
3455     case 0x2c:
3456     case 0x2d:
3457     case 0x30:
3458     case 0x31:
3459     case 0x32:
3460     case 0x33:
3461     case 0x34:
3462     case 0x35:
3463     case 0x38:
3464     case 0x39:
3465     case 0x3a:
3466     case 0x3b:
3467     case 0x3c:
3468     case 0x3d:
3469       if (((opcode >> 3) & 7) != OP_CMPL)
3470         {
3471           if ((opcode & 1) == 0)
3472             ir.ot = OT_BYTE;
3473           else
3474             ir.ot = ir.dflag + OT_WORD;
3475
3476           switch ((opcode >> 1) & 3)
3477             {
3478             case 0:    /* OP Ev, Gv */
3479               if (i386_record_modrm (&ir))
3480                 return -1;
3481               if (ir.mod != 3)
3482                 {
3483                   if (i386_record_lea_modrm (&ir))
3484                     return -1;
3485                 }
3486               else
3487                 {
3488                   ir.rm |= ir.rex_b;
3489                   if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
3490                     ir.rm &= 0x3;
3491                   I386_RECORD_ARCH_LIST_ADD_REG (ir.rm);
3492                 }
3493               break;
3494             case 1:    /* OP Gv, Ev */
3495               if (i386_record_modrm (&ir))
3496                 return -1;
3497               ir.reg |= rex_r;
3498               if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
3499                 ir.reg &= 0x3;
3500               I386_RECORD_ARCH_LIST_ADD_REG (ir.reg);
3501               break;
3502             case 2:    /* OP A, Iv */
3503               I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
3504               break;
3505             }
3506         }
3507       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3508       break;
3509
3510     case 0x80:    /* GRP1 */
3511     case 0x81:
3512     case 0x82:
3513     case 0x83:
3514       if (i386_record_modrm (&ir))
3515         return -1;
3516
3517       if (ir.reg != OP_CMPL)
3518         {
3519           if ((opcode & 1) == 0)
3520             ir.ot = OT_BYTE;
3521           else
3522             ir.ot = ir.dflag + OT_WORD;
3523
3524           if (ir.mod != 3)
3525             {
3526               if (opcode == 0x83)
3527                 ir.rip_offset = 1;
3528               else
3529                 ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
3530               if (i386_record_lea_modrm (&ir))
3531                 return -1;
3532             }
3533           else
3534             I386_RECORD_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
3535         }
3536       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3537       break;
3538
3539     case 0x40:      /* inc */
3540     case 0x41:
3541     case 0x42:
3542     case 0x43:
3543     case 0x44:
3544     case 0x45:
3545     case 0x46:
3546     case 0x47:
3547
3548     case 0x48:      /* dec */
3549     case 0x49:
3550     case 0x4a:
3551     case 0x4b:
3552     case 0x4c:
3553     case 0x4d:
3554     case 0x4e:
3555     case 0x4f:
3556
3557       I386_RECORD_ARCH_LIST_ADD_REG (opcode & 7);
3558       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3559       break;
3560
3561     case 0xf6:    /* GRP3 */
3562     case 0xf7:
3563       if ((opcode & 1) == 0)
3564         ir.ot = OT_BYTE;
3565       else
3566         ir.ot = ir.dflag + OT_WORD;
3567       if (i386_record_modrm (&ir))
3568         return -1;
3569
3570       if (ir.mod != 3 && ir.reg == 0)
3571         ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
3572
3573       switch (ir.reg)
3574         {
3575         case 0:    /* test */
3576           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3577           break;
3578         case 2:    /* not */
3579         case 3:    /* neg */
3580           if (ir.mod != 3)
3581             {
3582               if (i386_record_lea_modrm (&ir))
3583                 return -1;
3584             }
3585           else
3586             {
3587               ir.rm |= ir.rex_b;
3588               if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
3589                 ir.rm &= 0x3;
3590               I386_RECORD_ARCH_LIST_ADD_REG (ir.rm);
3591             }
3592           if (ir.reg == 3)  /* neg */
3593             I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3594           break;
3595         case 4:    /* mul  */
3596         case 5:    /* imul */
3597         case 6:    /* div  */
3598         case 7:    /* idiv */
3599           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
3600           if (ir.ot != OT_BYTE)
3601             I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
3602           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3603           break;
3604         default:
3605           ir.addr -= 2;
3606           opcode = opcode << 8 | ir.modrm;
3607           goto no_support;
3608           break;
3609         }
3610       break;
3611
3612     case 0xfe:    /* GRP4 */
3613     case 0xff:    /* GRP5 */
3614       if (i386_record_modrm (&ir))
3615         return -1;
3616       if (ir.reg >= 2 && opcode == 0xfe)
3617         {
3618           ir.addr -= 2;
3619           opcode = opcode << 8 | ir.modrm;
3620           goto no_support;
3621         }
3622       switch (ir.reg)
3623         {
3624         case 0:    /* inc */
3625         case 1:    /* dec */
3626           if ((opcode & 1) == 0)
3627             ir.ot = OT_BYTE;
3628           else
3629             ir.ot = ir.dflag + OT_WORD;
3630           if (ir.mod != 3)
3631             {
3632               if (i386_record_lea_modrm (&ir))
3633                 return -1;
3634             }
3635           else
3636             {
3637               ir.rm |= ir.rex_b;
3638               if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
3639                 ir.rm &= 0x3;
3640               I386_RECORD_ARCH_LIST_ADD_REG (ir.rm);
3641             }
3642           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3643           break;
3644         case 2:    /* call */
3645           if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
3646             ir.dflag = 2;
3647           if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
3648             return -1;
3649           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3650           break;
3651         case 3:    /* lcall */
3652           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
3653           if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
3654             return -1;
3655           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3656           break;
3657         case 4:    /* jmp  */
3658         case 5:    /* ljmp */
3659           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3660           break;
3661         case 6:    /* push */
3662           if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
3663             ir.dflag = 2;
3664           if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
3665             return -1;
3666           break;
3667         default:
3668           ir.addr -= 2;
3669           opcode = opcode << 8 | ir.modrm;
3670           goto no_support;
3671           break;
3672         }
3673       break;
3674
3675     case 0x84:    /* test */
3676     case 0x85:
3677     case 0xa8:
3678     case 0xa9:
3679       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3680       break;
3681
3682     case 0x98:    /* CWDE/CBW */
3683       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
3684       break;
3685
3686     case 0x99:    /* CDQ/CWD */
3687       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
3688       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
3689       break;
3690
3691     case 0x0faf:  /* imul */
3692     case 0x69:
3693     case 0x6b:
3694       ir.ot = ir.dflag + OT_WORD;
3695       if (i386_record_modrm (&ir))
3696         return -1;
3697       if (opcode == 0x69)
3698         ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
3699       else if (opcode == 0x6b)
3700         ir.rip_offset = 1;
3701       ir.reg |= rex_r;
3702       if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
3703         ir.reg &= 0x3;
3704       I386_RECORD_ARCH_LIST_ADD_REG (ir.reg);
3705       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3706       break;
3707
3708     case 0x0fc0:  /* xadd */
3709     case 0x0fc1:
3710       if ((opcode & 1) == 0)
3711         ir.ot = OT_BYTE;
3712       else
3713         ir.ot = ir.dflag + OT_WORD;
3714       if (i386_record_modrm (&ir))
3715         return -1;
3716       ir.reg |= rex_r;
3717       if (ir.mod == 3)
3718         {
3719           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
3720             ir.reg &= 0x3;
3721           I386_RECORD_ARCH_LIST_ADD_REG (ir.reg);
3722           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
3723             ir.rm &= 0x3;
3724           I386_RECORD_ARCH_LIST_ADD_REG (ir.rm);
3725         }
3726       else
3727         {
3728           if (i386_record_lea_modrm (&ir))
3729             return -1;
3730           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
3731             ir.reg &= 0x3;
3732           I386_RECORD_ARCH_LIST_ADD_REG (ir.reg);
3733         }
3734       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3735       break;
3736
3737     case 0x0fb0:  /* cmpxchg */
3738     case 0x0fb1:
3739       if ((opcode & 1) == 0)
3740         ir.ot = OT_BYTE;
3741       else
3742         ir.ot = ir.dflag + OT_WORD;
3743       if (i386_record_modrm (&ir))
3744         return -1;
3745       if (ir.mod == 3)
3746         {
3747           ir.reg |= rex_r;
3748           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
3749           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
3750             ir.reg &= 0x3;
3751           I386_RECORD_ARCH_LIST_ADD_REG (ir.reg);
3752         }
3753       else
3754         {
3755           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
3756           if (i386_record_lea_modrm (&ir))
3757             return -1;
3758         }
3759       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3760       break;
3761
3762     case 0x0fc7:    /* cmpxchg8b */
3763       if (i386_record_modrm (&ir))
3764         return -1;
3765       if (ir.mod == 3)
3766         {
3767           ir.addr -= 2;
3768           opcode = opcode << 8 | ir.modrm;
3769           goto no_support;
3770         }
3771       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
3772       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
3773       if (i386_record_lea_modrm (&ir))
3774         return -1;
3775       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3776       break;
3777
3778     case 0x50:    /* push */
3779     case 0x51:
3780     case 0x52:
3781     case 0x53:
3782     case 0x54:
3783     case 0x55:
3784     case 0x56:
3785     case 0x57:
3786     case 0x68:
3787     case 0x6a:
3788       if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
3789         ir.dflag = 2;
3790       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
3791         return -1;
3792       break;
3793
3794     case 0x06:    /* push es */
3795     case 0x0e:    /* push cs */
3796     case 0x16:    /* push ss */
3797     case 0x1e:    /* push ds */
3798       if (ir.regmap[X86_RECORD_R8_REGNUM])
3799         {
3800           ir.addr -= 1;
3801           goto no_support;
3802         }
3803       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
3804         return -1;
3805       break;
3806
3807     case 0x0fa0:    /* push fs */
3808     case 0x0fa8:    /* push gs */
3809       if (ir.regmap[X86_RECORD_R8_REGNUM])
3810         {
3811           ir.addr -= 2;
3812           goto no_support;
3813         }
3814       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
3815         return -1;
3816       break;
3817
3818     case 0x60:    /* pusha */
3819       if (ir.regmap[X86_RECORD_R8_REGNUM])
3820         {
3821           ir.addr -= 1;
3822           goto no_support;
3823         }
3824       if (i386_record_push (&ir, 1 << (ir.dflag + 4)))
3825         return -1;
3826       break;
3827
3828     case 0x58:    /* pop */
3829     case 0x59:
3830     case 0x5a:
3831     case 0x5b:
3832     case 0x5c:
3833     case 0x5d:
3834     case 0x5e:
3835     case 0x5f:
3836       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
3837       I386_RECORD_ARCH_LIST_ADD_REG ((opcode & 0x7) | ir.rex_b);
3838       break;
3839
3840     case 0x61:    /* popa */
3841       if (ir.regmap[X86_RECORD_R8_REGNUM])
3842         {
3843           ir.addr -= 1;
3844           goto no_support;
3845         }
3846       for (tmpu8 = X86_RECORD_REAX_REGNUM; tmpu8 <= X86_RECORD_REDI_REGNUM;
3847            tmpu8++)
3848         I386_RECORD_ARCH_LIST_ADD_REG (tmpu8);
3849       break;
3850
3851     case 0x8f:    /* pop */
3852       if (ir.regmap[X86_RECORD_R8_REGNUM])
3853         ir.ot = ir.dflag ? OT_QUAD : OT_WORD;
3854       else
3855         ir.ot = ir.dflag + OT_WORD;
3856       if (i386_record_modrm (&ir))
3857         return -1;
3858       if (ir.mod == 3)
3859         I386_RECORD_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
3860       else
3861         {
3862           ir.popl_esp_hack = 1 << ir.ot;
3863           if (i386_record_lea_modrm (&ir))
3864             return -1;
3865         }
3866       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
3867       break;
3868
3869     case 0xc8:    /* enter */
3870       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REBP_REGNUM);
3871       if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
3872         ir.dflag = 2;
3873       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
3874         return -1;
3875       break;
3876
3877     case 0xc9:    /* leave */
3878       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
3879       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REBP_REGNUM);
3880       break;
3881
3882     case 0x07:    /* pop es */
3883       if (ir.regmap[X86_RECORD_R8_REGNUM])
3884         {
3885           ir.addr -= 1;
3886           goto no_support;
3887         }
3888       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
3889       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_ES_REGNUM);
3890       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3891       break;
3892
3893     case 0x17:    /* pop ss */
3894       if (ir.regmap[X86_RECORD_R8_REGNUM])
3895         {
3896           ir.addr -= 1;
3897           goto no_support;
3898         }
3899       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
3900       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_SS_REGNUM);
3901       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3902       break;
3903
3904     case 0x1f:    /* pop ds */
3905       if (ir.regmap[X86_RECORD_R8_REGNUM])
3906         {
3907           ir.addr -= 1;
3908           goto no_support;
3909         }
3910       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
3911       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_DS_REGNUM);
3912       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3913       break;
3914
3915     case 0x0fa1:    /* pop fs */
3916       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
3917       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_FS_REGNUM);
3918       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3919       break;
3920
3921     case 0x0fa9:    /* pop gs */
3922       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
3923       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_GS_REGNUM);
3924       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
3925       break;
3926
3927     case 0x88:    /* mov */
3928     case 0x89:
3929     case 0xc6:
3930     case 0xc7:
3931       if ((opcode & 1) == 0)
3932         ir.ot = OT_BYTE;
3933       else
3934         ir.ot = ir.dflag + OT_WORD;
3935
3936       if (i386_record_modrm (&ir))
3937         return -1;
3938
3939       if (ir.mod != 3)
3940         {
3941           if (opcode == 0xc6 || opcode == 0xc7)
3942             ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
3943           if (i386_record_lea_modrm (&ir))
3944             return -1;
3945         }
3946       else
3947         {
3948           if (opcode == 0xc6 || opcode == 0xc7)
3949             ir.rm |= ir.rex_b;
3950           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
3951             ir.rm &= 0x3;
3952           I386_RECORD_ARCH_LIST_ADD_REG (ir.rm);
3953         }
3954       break;
3955
3956     case 0x8a:    /* mov */
3957     case 0x8b:
3958       if ((opcode & 1) == 0)
3959         ir.ot = OT_BYTE;
3960       else
3961         ir.ot = ir.dflag + OT_WORD;
3962       if (i386_record_modrm (&ir))
3963         return -1;
3964       ir.reg |= rex_r;
3965       if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
3966         ir.reg &= 0x3;
3967       I386_RECORD_ARCH_LIST_ADD_REG (ir.reg);
3968       break;
3969
3970     case 0x8c:    /* mov seg */
3971       if (i386_record_modrm (&ir))
3972         return -1;
3973       if (ir.reg > 5)
3974         {
3975           ir.addr -= 2;
3976           opcode = opcode << 8 | ir.modrm;
3977           goto no_support;
3978         }
3979
3980       if (ir.mod == 3)
3981         I386_RECORD_ARCH_LIST_ADD_REG (ir.rm);
3982       else
3983         {
3984           ir.ot = OT_WORD;
3985           if (i386_record_lea_modrm (&ir))
3986             return -1;
3987         }
3988       break;
3989
3990     case 0x8e:    /* mov seg */
3991       if (i386_record_modrm (&ir))
3992         return -1;
3993       switch (ir.reg)
3994         {
3995         case 0:
3996           tmpu8 = X86_RECORD_ES_REGNUM;
3997           break;
3998         case 2:
3999           tmpu8 = X86_RECORD_SS_REGNUM;
4000           break;
4001         case 3:
4002           tmpu8 = X86_RECORD_DS_REGNUM;
4003           break;
4004         case 4:
4005           tmpu8 = X86_RECORD_FS_REGNUM;
4006           break;
4007         case 5:
4008           tmpu8 = X86_RECORD_GS_REGNUM;
4009           break;
4010         default:
4011           ir.addr -= 2;
4012           opcode = opcode << 8 | ir.modrm;
4013           goto no_support;
4014           break;
4015         }
4016       I386_RECORD_ARCH_LIST_ADD_REG (tmpu8);
4017       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4018       break;
4019
4020     case 0x0fb6:    /* movzbS */
4021     case 0x0fb7:    /* movzwS */
4022     case 0x0fbe:    /* movsbS */
4023     case 0x0fbf:    /* movswS */
4024       if (i386_record_modrm (&ir))
4025         return -1;
4026       I386_RECORD_ARCH_LIST_ADD_REG (ir.reg | rex_r);
4027       break;
4028
4029     case 0x8d:      /* lea */
4030       if (i386_record_modrm (&ir))
4031         return -1;
4032       if (ir.mod == 3)
4033         {
4034           ir.addr -= 2;
4035           opcode = opcode << 8 | ir.modrm;
4036           goto no_support;
4037         }
4038       ir.ot = ir.dflag;
4039       ir.reg |= rex_r;
4040       if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
4041         ir.reg &= 0x3;
4042       I386_RECORD_ARCH_LIST_ADD_REG (ir.reg);
4043       break;
4044
4045     case 0xa0:    /* mov EAX */
4046     case 0xa1:
4047
4048     case 0xd7:    /* xlat */
4049       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
4050       break;
4051
4052     case 0xa2:    /* mov EAX */
4053     case 0xa3:
4054       if (ir.override >= 0)
4055         {
4056           warning (_("Process record ignores the memory change "
4057                      "of instruction at address %s because "
4058                      "it can't get the value of the segment "
4059                      "register."),
4060                    paddress (gdbarch, ir.orig_addr));
4061         }
4062       else
4063         {
4064           if ((opcode & 1) == 0)
4065             ir.ot = OT_BYTE;
4066           else
4067             ir.ot = ir.dflag + OT_WORD;
4068           if (ir.aflag == 2)
4069             {
4070               if (target_read_memory (ir.addr, (gdb_byte *) &addr, 8))
4071                 {
4072                   if (record_debug)
4073                     printf_unfiltered (_("Process record: error reading "
4074                                          "memory at addr 0x%s len = 8.\n"),
4075                                        paddress (gdbarch, ir.addr));
4076                   return -1;
4077                 }
4078               ir.addr += 8;
4079             }
4080           else if (ir.aflag)
4081             {
4082               if (target_read_memory (ir.addr, (gdb_byte *) &tmpu32, 4))
4083                 {
4084                   if (record_debug)
4085                     printf_unfiltered (_("Process record: error reading "
4086                                          "memory at addr 0x%s len = 4.\n"),
4087                                        paddress (gdbarch, ir.addr));
4088                   return -1;
4089                 }
4090               ir.addr += 4;
4091               addr = tmpu32;
4092             }
4093           else
4094             {
4095               if (target_read_memory (ir.addr, (gdb_byte *) &tmpu16, 2))
4096                 {
4097                   if (record_debug)
4098                     printf_unfiltered (_("Process record: error reading "
4099                                          "memory at addr 0x%s len = 2.\n"),
4100                                        paddress (gdbarch, ir.addr));
4101                   return -1;
4102                 }
4103               ir.addr += 2;
4104               addr = tmpu16;
4105             }
4106           if (record_arch_list_add_mem (addr, 1 << ir.ot))
4107             return -1;
4108         }
4109       break;
4110
4111     case 0xb0:    /* mov R, Ib */
4112     case 0xb1:
4113     case 0xb2:
4114     case 0xb3:
4115     case 0xb4:
4116     case 0xb5:
4117     case 0xb6:
4118     case 0xb7:
4119       I386_RECORD_ARCH_LIST_ADD_REG ((ir.regmap[X86_RECORD_R8_REGNUM])
4120                                         ? ((opcode & 0x7) | ir.rex_b)
4121                                         : ((opcode & 0x7) & 0x3));
4122       break;
4123
4124     case 0xb8:    /* mov R, Iv */
4125     case 0xb9:
4126     case 0xba:
4127     case 0xbb:
4128     case 0xbc:
4129     case 0xbd:
4130     case 0xbe:
4131     case 0xbf:
4132       I386_RECORD_ARCH_LIST_ADD_REG ((opcode & 0x7) | ir.rex_b);
4133       break;
4134
4135     case 0x91:    /* xchg R, EAX */
4136     case 0x92:
4137     case 0x93:
4138     case 0x94:
4139     case 0x95:
4140     case 0x96:
4141     case 0x97:
4142       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
4143       I386_RECORD_ARCH_LIST_ADD_REG (opcode & 0x7);
4144       break;
4145
4146     case 0x86:    /* xchg Ev, Gv */
4147     case 0x87:
4148       if ((opcode & 1) == 0)
4149         ir.ot = OT_BYTE;
4150       else
4151         ir.ot = ir.dflag + OT_WORD;
4152       if (i386_record_modrm (&ir))
4153         return -1;
4154       if (ir.mod == 3)
4155         {
4156           ir.rm |= ir.rex_b;
4157           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
4158             ir.rm &= 0x3;
4159           I386_RECORD_ARCH_LIST_ADD_REG (ir.rm);
4160         }
4161       else
4162         {
4163           if (i386_record_lea_modrm (&ir))
4164             return -1;
4165         }
4166       ir.reg |= rex_r;
4167       if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
4168         ir.reg &= 0x3;
4169       I386_RECORD_ARCH_LIST_ADD_REG (ir.reg);
4170       break;
4171
4172     case 0xc4:    /* les Gv */
4173     case 0xc5:    /* lds Gv */
4174       if (ir.regmap[X86_RECORD_R8_REGNUM])
4175         {
4176           ir.addr -= 1;
4177           goto no_support;
4178         }
4179     case 0x0fb2:    /* lss Gv */
4180     case 0x0fb4:    /* lfs Gv */
4181     case 0x0fb5:    /* lgs Gv */
4182       if (i386_record_modrm (&ir))
4183         return -1;
4184       if (ir.mod == 3)
4185         {
4186           if (opcode > 0xff)
4187             ir.addr -= 3;
4188           else
4189             ir.addr -= 2;
4190           opcode = opcode << 8 | ir.modrm;
4191           goto no_support;
4192         }
4193       switch (opcode)
4194         {
4195         case 0xc4:    /* les Gv */
4196           tmpu8 = X86_RECORD_ES_REGNUM;
4197           break;
4198         case 0xc5:    /* lds Gv */
4199           tmpu8 = X86_RECORD_DS_REGNUM;
4200           break;
4201         case 0x0fb2:  /* lss Gv */
4202           tmpu8 = X86_RECORD_SS_REGNUM;
4203           break;
4204         case 0x0fb4:  /* lfs Gv */
4205           tmpu8 = X86_RECORD_FS_REGNUM;
4206           break;
4207         case 0x0fb5:  /* lgs Gv */
4208           tmpu8 = X86_RECORD_GS_REGNUM;
4209           break;
4210         }
4211       I386_RECORD_ARCH_LIST_ADD_REG (tmpu8);
4212       I386_RECORD_ARCH_LIST_ADD_REG (ir.reg | rex_r);
4213       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4214       break;
4215
4216     case 0xc0:    /* shifts */
4217     case 0xc1:
4218     case 0xd0:
4219     case 0xd1:
4220     case 0xd2:
4221     case 0xd3:
4222       if ((opcode & 1) == 0)
4223         ir.ot = OT_BYTE;
4224       else
4225         ir.ot = ir.dflag + OT_WORD;
4226       if (i386_record_modrm (&ir))
4227         return -1;
4228       if (ir.mod != 3 && (opcode == 0xd2 || opcode == 0xd3))
4229         {
4230           if (i386_record_lea_modrm (&ir))
4231             return -1;
4232         }
4233       else
4234         {
4235           ir.rm |= ir.rex_b;
4236           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
4237             ir.rm &= 0x3;
4238           I386_RECORD_ARCH_LIST_ADD_REG (ir.rm);
4239         }
4240       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4241       break;
4242
4243     case 0x0fa4:
4244     case 0x0fa5:
4245     case 0x0fac:
4246     case 0x0fad:
4247       if (i386_record_modrm (&ir))
4248         return -1;
4249       if (ir.mod == 3)
4250         {
4251           if (record_arch_list_add_reg (ir.regcache, ir.rm))
4252             return -1;
4253         }
4254       else
4255         {
4256           if (i386_record_lea_modrm (&ir))
4257             return -1;
4258         }
4259       break;
4260
4261     case 0xd8:    /* Floats.  */
4262     case 0xd9:
4263     case 0xda:
4264     case 0xdb:
4265     case 0xdc:
4266     case 0xdd:
4267     case 0xde:
4268     case 0xdf:
4269       if (i386_record_modrm (&ir))
4270         return -1;
4271       ir.reg |= ((opcode & 7) << 3);
4272       if (ir.mod != 3)
4273         {
4274           /* Memory. */
4275           uint64_t tmpu64;
4276
4277           if (i386_record_lea_modrm_addr (&ir, &tmpu64))
4278             return -1;
4279           switch (ir.reg)
4280             {
4281             case 0x02:
4282             case 0x12:
4283             case 0x22:
4284             case 0x32:
4285               /* For fcom, ficom nothing to do.  */
4286               break;
4287             case 0x03:
4288             case 0x13:
4289             case 0x23:
4290             case 0x33:
4291               /* For fcomp, ficomp pop FPU stack, store all.  */
4292               if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
4293                 return -1;
4294               break;
4295             case 0x00:
4296             case 0x01:
4297             case 0x04:
4298             case 0x05:
4299             case 0x06:
4300             case 0x07:
4301             case 0x10:
4302             case 0x11:
4303             case 0x14:
4304             case 0x15:
4305             case 0x16:
4306             case 0x17:
4307             case 0x20:
4308             case 0x21:
4309             case 0x24:
4310             case 0x25:
4311             case 0x26:
4312             case 0x27:
4313             case 0x30:
4314             case 0x31:
4315             case 0x34:
4316             case 0x35:
4317             case 0x36:
4318             case 0x37:
4319               /* For fadd, fmul, fsub, fsubr, fdiv, fdivr, fiadd, fimul,
4320                  fisub, fisubr, fidiv, fidivr, modR/M.reg is an extension
4321                  of code,  always affects st(0) register.  */
4322               if (i386_record_floats (gdbarch, &ir, I387_ST0_REGNUM (tdep)))
4323                 return -1;
4324               break;
4325             case 0x08:
4326             case 0x0a:
4327             case 0x0b:
4328             case 0x18:
4329             case 0x19:
4330             case 0x1a:
4331             case 0x1b:
4332             case 0x1d:
4333             case 0x28:
4334             case 0x29:
4335             case 0x2a:
4336             case 0x2b:
4337             case 0x38:
4338             case 0x39:
4339             case 0x3a:
4340             case 0x3b:
4341             case 0x3c:
4342             case 0x3d:
4343               switch (ir.reg & 7)
4344                 {
4345                 case 0:
4346                   /* Handling fld, fild.  */
4347                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
4348                     return -1;
4349                   break;
4350                 case 1:
4351                   switch (ir.reg >> 4)
4352                     {
4353                     case 0:
4354                       if (record_arch_list_add_mem (tmpu64, 4))
4355                         return -1;
4356                       break;
4357                     case 2:
4358                       if (record_arch_list_add_mem (tmpu64, 8))
4359                         return -1;
4360                       break;
4361                     case 3:
4362                       break;
4363                     default:
4364                       if (record_arch_list_add_mem (tmpu64, 2))
4365                         return -1;
4366                       break;
4367                     }
4368                   break;
4369                 default:
4370                   switch (ir.reg >> 4)
4371                     {
4372                     case 0:
4373                       if (record_arch_list_add_mem (tmpu64, 4))
4374                         return -1;
4375                       if (3 == (ir.reg & 7))
4376                         {
4377                           /* For fstp m32fp.  */
4378                           if (i386_record_floats (gdbarch, &ir,
4379                                                   I386_SAVE_FPU_REGS))
4380                             return -1;
4381                         }
4382                       break;
4383                     case 1:
4384                       if (record_arch_list_add_mem (tmpu64, 4))
4385                         return -1;
4386                       if ((3 == (ir.reg & 7))
4387                           || (5 == (ir.reg & 7))
4388                           || (7 == (ir.reg & 7)))
4389                         {
4390                           /* For fstp insn.  */
4391                           if (i386_record_floats (gdbarch, &ir,
4392                                                   I386_SAVE_FPU_REGS))
4393                             return -1;
4394                         }
4395                       break;
4396                     case 2:
4397                       if (record_arch_list_add_mem (tmpu64, 8))
4398                         return -1;
4399                       if (3 == (ir.reg & 7))
4400                         {
4401                           /* For fstp m64fp.  */
4402                           if (i386_record_floats (gdbarch, &ir,
4403                                                   I386_SAVE_FPU_REGS))
4404                             return -1;
4405                         }
4406                       break;
4407                     case 3:
4408                       if ((3 <= (ir.reg & 7)) && (6 <= (ir.reg & 7)))
4409                         {
4410                           /* For fistp, fbld, fild, fbstp.  */
4411                           if (i386_record_floats (gdbarch, &ir,
4412                                                   I386_SAVE_FPU_REGS))
4413                             return -1;
4414                         }
4415                       /* Fall through */
4416                     default:
4417                       if (record_arch_list_add_mem (tmpu64, 2))
4418                         return -1;
4419                       break;
4420                     }
4421                   break;
4422                 }
4423               break;
4424             case 0x0c:
4425               /* Insn fldenv.  */
4426               if (i386_record_floats (gdbarch, &ir,
4427                                       I386_SAVE_FPU_ENV_REG_STACK))
4428                 return -1;
4429               break;
4430             case 0x0d:
4431               /* Insn fldcw.  */
4432               if (i386_record_floats (gdbarch, &ir, I387_FCTRL_REGNUM (tdep)))
4433                 return -1;
4434               break;
4435             case 0x2c:
4436               /* Insn frstor.  */
4437               if (i386_record_floats (gdbarch, &ir,
4438                                       I386_SAVE_FPU_ENV_REG_STACK))
4439                 return -1;
4440               break;
4441             case 0x0e:
4442               if (ir.dflag)
4443                 {
4444                   if (record_arch_list_add_mem (tmpu64, 28))
4445                     return -1;
4446                 }
4447               else
4448                 {
4449                   if (record_arch_list_add_mem (tmpu64, 14))
4450                     return -1;
4451                 }
4452               break;
4453             case 0x0f:
4454             case 0x2f:
4455               if (record_arch_list_add_mem (tmpu64, 2))
4456                 return -1;
4457               /* Insn fstp, fbstp.  */
4458               if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
4459                 return -1;
4460               break;
4461             case 0x1f:
4462             case 0x3e:
4463               if (record_arch_list_add_mem (tmpu64, 10))
4464                 return -1;
4465               break;
4466             case 0x2e:
4467               if (ir.dflag)
4468                 {
4469                   if (record_arch_list_add_mem (tmpu64, 28))
4470                     return -1;
4471                   tmpu64 += 28;
4472                 }
4473               else
4474                 {
4475                   if (record_arch_list_add_mem (tmpu64, 14))
4476                     return -1;
4477                   tmpu64 += 14;
4478                 }
4479               if (record_arch_list_add_mem (tmpu64, 80))
4480                 return -1;
4481               /* Insn fsave.  */
4482               if (i386_record_floats (gdbarch, &ir,
4483                                       I386_SAVE_FPU_ENV_REG_STACK))
4484                 return -1;
4485               break;
4486             case 0x3f:
4487               if (record_arch_list_add_mem (tmpu64, 8))
4488                 return -1;
4489               /* Insn fistp.  */
4490               if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
4491                 return -1;
4492               break;
4493             default:
4494               ir.addr -= 2;
4495               opcode = opcode << 8 | ir.modrm;
4496               goto no_support;
4497               break;
4498             }
4499         }
4500       /* Opcode is an extension of modR/M byte.  */
4501       else
4502         {
4503           switch (opcode)
4504             {
4505             case 0xd8:
4506               if (i386_record_floats (gdbarch, &ir, I387_ST0_REGNUM (tdep)))
4507                 return -1;
4508               break;
4509             case 0xd9:
4510               if (0x0c == (ir.modrm >> 4))
4511                 {
4512                   if ((ir.modrm & 0x0f) <= 7)
4513                     {
4514                       if (i386_record_floats (gdbarch, &ir,
4515                                               I386_SAVE_FPU_REGS))
4516                         return -1;
4517                     }
4518                   else
4519                     {
4520                       if (i386_record_floats (gdbarch, &ir,
4521                                               I387_ST0_REGNUM (tdep)))
4522                         return -1;
4523                       /* If only st(0) is changing, then we have already
4524                          recorded.  */
4525                       if ((ir.modrm & 0x0f) - 0x08)
4526                         {
4527                           if (i386_record_floats (gdbarch, &ir,
4528                                                   I387_ST0_REGNUM (tdep) +
4529                                                   ((ir.modrm & 0x0f) - 0x08)))
4530                             return -1;
4531                         }
4532                     }
4533                 }
4534               else
4535                 {
4536                   switch (ir.modrm)
4537                     {
4538                     case 0xe0:
4539                     case 0xe1:
4540                     case 0xf0:
4541                     case 0xf5:
4542                     case 0xf8:
4543                     case 0xfa:
4544                     case 0xfc:
4545                     case 0xfe:
4546                     case 0xff:
4547                       if (i386_record_floats (gdbarch, &ir,
4548                                               I387_ST0_REGNUM (tdep)))
4549                         return -1;
4550                       break;
4551                     case 0xf1:
4552                     case 0xf2:
4553                     case 0xf3:
4554                     case 0xf4:
4555                     case 0xf6:
4556                     case 0xf7:
4557                     case 0xe8:
4558                     case 0xe9:
4559                     case 0xea:
4560                     case 0xeb:
4561                     case 0xec:
4562                     case 0xed:
4563                     case 0xee:
4564                     case 0xf9:
4565                     case 0xfb:
4566                       if (i386_record_floats (gdbarch, &ir,
4567                                               I386_SAVE_FPU_REGS))
4568                         return -1;
4569                       break;
4570                     case 0xfd:
4571                       if (i386_record_floats (gdbarch, &ir,
4572                                               I387_ST0_REGNUM (tdep)))
4573                         return -1;
4574                       if (i386_record_floats (gdbarch, &ir,
4575                                               I387_ST0_REGNUM (tdep) + 1))
4576                         return -1;
4577                       break;
4578                     }
4579                 }
4580               break;
4581             case 0xda:
4582               if (0xe9 == ir.modrm)
4583                 {
4584                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
4585                     return -1;
4586                 }
4587               else if ((0x0c == ir.modrm >> 4) || (0x0d == ir.modrm >> 4))
4588                 {
4589                   if (i386_record_floats (gdbarch, &ir,
4590                                           I387_ST0_REGNUM (tdep)))
4591                     return -1;
4592                   if (((ir.modrm & 0x0f) > 0) && ((ir.modrm & 0x0f) <= 7))
4593                     {
4594                       if (i386_record_floats (gdbarch, &ir,
4595                                               I387_ST0_REGNUM (tdep) +
4596                                               (ir.modrm & 0x0f)))
4597                         return -1;
4598                     }
4599                   else if ((ir.modrm & 0x0f) - 0x08)
4600                     {
4601                       if (i386_record_floats (gdbarch, &ir,
4602                                               I387_ST0_REGNUM (tdep) +
4603                                               ((ir.modrm & 0x0f) - 0x08)))
4604                         return -1;
4605                     }
4606                 }
4607               break;
4608             case 0xdb:
4609               if (0xe3 == ir.modrm)
4610                 {
4611                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_ENV))
4612                     return -1;
4613                 }
4614               else if ((0x0c == ir.modrm >> 4) || (0x0d == ir.modrm >> 4))
4615                 {
4616                   if (i386_record_floats (gdbarch, &ir,
4617                                           I387_ST0_REGNUM (tdep)))
4618                     return -1;
4619                   if (((ir.modrm & 0x0f) > 0) && ((ir.modrm & 0x0f) <= 7))
4620                     {
4621                       if (i386_record_floats (gdbarch, &ir,
4622                                               I387_ST0_REGNUM (tdep) +
4623                                               (ir.modrm & 0x0f)))
4624                         return -1;
4625                     }
4626                   else if ((ir.modrm & 0x0f) - 0x08)
4627                     {
4628                       if (i386_record_floats (gdbarch, &ir,
4629                                               I387_ST0_REGNUM (tdep) +
4630                                               ((ir.modrm & 0x0f) - 0x08)))
4631                         return -1;
4632                     }
4633                 }
4634               break;
4635             case 0xdc:
4636               if ((0x0c == ir.modrm >> 4)
4637                   || (0x0d == ir.modrm >> 4)
4638                   || (0x0f == ir.modrm >> 4))
4639                 {
4640                   if ((ir.modrm & 0x0f) <= 7)
4641                     {
4642                       if (i386_record_floats (gdbarch, &ir,
4643                                               I387_ST0_REGNUM (tdep) +
4644                                               (ir.modrm & 0x0f)))
4645                         return -1;
4646                     }
4647                   else
4648                     {
4649                       if (i386_record_floats (gdbarch, &ir,
4650                                               I387_ST0_REGNUM (tdep) +
4651                                               ((ir.modrm & 0x0f) - 0x08)))
4652                         return -1;
4653                     }
4654                 }
4655               break;
4656             case 0xdd:
4657               if (0x0c == ir.modrm >> 4)
4658                 {
4659                   if (i386_record_floats (gdbarch, &ir,
4660                                           I387_FTAG_REGNUM (tdep)))
4661                     return -1;
4662                 }
4663               else if ((0x0d == ir.modrm >> 4) || (0x0e == ir.modrm >> 4))
4664                 {
4665                   if ((ir.modrm & 0x0f) <= 7)
4666                     {
4667                       if (i386_record_floats (gdbarch, &ir,
4668                                               I387_ST0_REGNUM (tdep) +
4669                                               (ir.modrm & 0x0f)))
4670                         return -1;
4671                     }
4672                   else
4673                     {
4674                       if (i386_record_floats (gdbarch, &ir,
4675                                               I386_SAVE_FPU_REGS))
4676                         return -1;
4677                     }
4678                 }
4679               break;
4680             case 0xde:
4681               if ((0x0c == ir.modrm >> 4)
4682                   || (0x0e == ir.modrm >> 4)
4683                   || (0x0f == ir.modrm >> 4)
4684                   || (0xd9 == ir.modrm))
4685                 {
4686                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
4687                     return -1;
4688                 }
4689               break;
4690             case 0xdf:
4691               if (0xe0 == ir.modrm)
4692                 {
4693                   if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
4694                     return -1;
4695                 }
4696               else if ((0x0f == ir.modrm >> 4) || (0x0e == ir.modrm >> 4))
4697                 {
4698                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
4699                     return -1;
4700                 }
4701               break;
4702             }
4703         }
4704       break;
4705       /* string ops */
4706     case 0xa4:    /* movsS */
4707     case 0xa5:
4708     case 0xaa:    /* stosS */
4709     case 0xab:
4710     case 0x6c:    /* insS */
4711     case 0x6d:
4712       regcache_raw_read_unsigned (ir.regcache,
4713                                   ir.regmap[X86_RECORD_RECX_REGNUM],
4714                                   &tmpulongest);
4715       if (tmpulongest)
4716         {
4717           ULONGEST es, ds;
4718
4719           if ((opcode & 1) == 0)
4720             ir.ot = OT_BYTE;
4721           else
4722             ir.ot = ir.dflag + OT_WORD;
4723           regcache_raw_read_unsigned (ir.regcache,
4724                                       ir.regmap[X86_RECORD_REDI_REGNUM],
4725                                       &tmpulongest);
4726
4727           regcache_raw_read_unsigned (ir.regcache,
4728                                       ir.regmap[X86_RECORD_ES_REGNUM],
4729                                       &es);
4730           regcache_raw_read_unsigned (ir.regcache,
4731                                       ir.regmap[X86_RECORD_DS_REGNUM],
4732                                       &ds);
4733           if (ir.aflag && (es != ds))
4734             {
4735               /* addr += ((uint32_t) read_register (I386_ES_REGNUM)) << 4; */
4736               warning (_("Process record ignores the memory "
4737                          "change of instruction at address %s "
4738                          "because it can't get the value of the "
4739                          "ES segment register."),
4740                        paddress (gdbarch, ir.orig_addr));
4741             }
4742           else
4743             {
4744               if (record_arch_list_add_mem (tmpulongest, 1 << ir.ot))
4745                 return -1;
4746             }
4747
4748           if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
4749             I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
4750           if (opcode == 0xa4 || opcode == 0xa5)
4751             I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
4752           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
4753           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4754         }
4755       break;
4756
4757     case 0xa6:    /* cmpsS */
4758     case 0xa7:
4759       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
4760       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
4761       if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
4762         I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
4763       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4764       break;
4765
4766     case 0xac:    /* lodsS */
4767     case 0xad:
4768       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
4769       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
4770       if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
4771         I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
4772       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4773       break;
4774
4775     case 0xae:    /* scasS */
4776     case 0xaf:
4777       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
4778       if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
4779         I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
4780       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4781       break;
4782
4783     case 0x6e:    /* outsS */
4784     case 0x6f:
4785       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
4786       if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
4787         I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
4788       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4789       break;
4790
4791     case 0xe4:    /* port I/O */
4792     case 0xe5:
4793     case 0xec:
4794     case 0xed:
4795       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4796       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
4797       break;
4798
4799     case 0xe6:
4800     case 0xe7:
4801     case 0xee:
4802     case 0xef:
4803       break;
4804
4805       /* control */
4806     case 0xc2:    /* ret im */
4807     case 0xc3:    /* ret */
4808       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
4809       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4810       break;
4811
4812     case 0xca:    /* lret im */
4813     case 0xcb:    /* lret */
4814     case 0xcf:    /* iret */
4815       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
4816       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
4817       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4818       break;
4819
4820     case 0xe8:    /* call im */
4821       if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
4822         ir.dflag = 2;
4823       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
4824         return -1;
4825       break;
4826
4827     case 0x9a:    /* lcall im */
4828       if (ir.regmap[X86_RECORD_R8_REGNUM])
4829         {
4830           ir.addr -= 1;
4831           goto no_support;
4832         }
4833       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
4834       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
4835         return -1;
4836       break;
4837
4838     case 0xe9:    /* jmp im */
4839     case 0xea:    /* ljmp im */
4840     case 0xeb:    /* jmp Jb */
4841     case 0x70:    /* jcc Jb */
4842     case 0x71:
4843     case 0x72:
4844     case 0x73:
4845     case 0x74:
4846     case 0x75:
4847     case 0x76:
4848     case 0x77:
4849     case 0x78:
4850     case 0x79:
4851     case 0x7a:
4852     case 0x7b:
4853     case 0x7c:
4854     case 0x7d:
4855     case 0x7e:
4856     case 0x7f:
4857     case 0x0f80:  /* jcc Jv */
4858     case 0x0f81:
4859     case 0x0f82:
4860     case 0x0f83:
4861     case 0x0f84:
4862     case 0x0f85:
4863     case 0x0f86:
4864     case 0x0f87:
4865     case 0x0f88:
4866     case 0x0f89:
4867     case 0x0f8a:
4868     case 0x0f8b:
4869     case 0x0f8c:
4870     case 0x0f8d:
4871     case 0x0f8e:
4872     case 0x0f8f:
4873       break;
4874
4875     case 0x0f90:  /* setcc Gv */
4876     case 0x0f91:
4877     case 0x0f92:
4878     case 0x0f93:
4879     case 0x0f94:
4880     case 0x0f95:
4881     case 0x0f96:
4882     case 0x0f97:
4883     case 0x0f98:
4884     case 0x0f99:
4885     case 0x0f9a:
4886     case 0x0f9b:
4887     case 0x0f9c:
4888     case 0x0f9d:
4889     case 0x0f9e:
4890     case 0x0f9f:
4891       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4892       ir.ot = OT_BYTE;
4893       if (i386_record_modrm (&ir))
4894         return -1;
4895       if (ir.mod == 3)
4896         I386_RECORD_ARCH_LIST_ADD_REG (ir.rex_b ? (ir.rm | ir.rex_b)
4897                                                 : (ir.rm & 0x3));
4898       else
4899         {
4900           if (i386_record_lea_modrm (&ir))
4901             return -1;
4902         }
4903       break;
4904
4905     case 0x0f40:    /* cmov Gv, Ev */
4906     case 0x0f41:
4907     case 0x0f42:
4908     case 0x0f43:
4909     case 0x0f44:
4910     case 0x0f45:
4911     case 0x0f46:
4912     case 0x0f47:
4913     case 0x0f48:
4914     case 0x0f49:
4915     case 0x0f4a:
4916     case 0x0f4b:
4917     case 0x0f4c:
4918     case 0x0f4d:
4919     case 0x0f4e:
4920     case 0x0f4f:
4921       if (i386_record_modrm (&ir))
4922         return -1;
4923       ir.reg |= rex_r;
4924       if (ir.dflag == OT_BYTE)
4925         ir.reg &= 0x3;
4926       I386_RECORD_ARCH_LIST_ADD_REG (ir.reg);
4927       break;
4928
4929       /* flags */
4930     case 0x9c:    /* pushf */
4931       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4932       if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
4933         ir.dflag = 2;
4934       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
4935         return -1;
4936       break;
4937
4938     case 0x9d:    /* popf */
4939       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
4940       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4941       break;
4942
4943     case 0x9e:    /* sahf */
4944       if (ir.regmap[X86_RECORD_R8_REGNUM])
4945         {
4946           ir.addr -= 1;
4947           goto no_support;
4948         }
4949     case 0xf5:    /* cmc */
4950     case 0xf8:    /* clc */
4951     case 0xf9:    /* stc */
4952     case 0xfc:    /* cld */
4953     case 0xfd:    /* std */
4954       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4955       break;
4956
4957     case 0x9f:    /* lahf */
4958       if (ir.regmap[X86_RECORD_R8_REGNUM])
4959         {
4960           ir.addr -= 1;
4961           goto no_support;
4962         }
4963       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4964       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
4965       break;
4966
4967       /* bit operations */
4968     case 0x0fba:    /* bt/bts/btr/btc Gv, im */
4969       ir.ot = ir.dflag + OT_WORD;
4970       if (i386_record_modrm (&ir))
4971         return -1;
4972       if (ir.reg < 4)
4973         {
4974           ir.addr -= 2;
4975           opcode = opcode << 8 | ir.modrm;
4976           goto no_support;
4977         }
4978       if (ir.reg != 4)
4979         {
4980           if (ir.mod == 3)
4981             I386_RECORD_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
4982           else
4983             {
4984               if (i386_record_lea_modrm (&ir))
4985                 return -1;
4986             }
4987         }
4988       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4989       break;
4990
4991     case 0x0fa3:    /* bt Gv, Ev */
4992       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
4993       break;
4994
4995     case 0x0fab:    /* bts */
4996     case 0x0fb3:    /* btr */
4997     case 0x0fbb:    /* btc */
4998       ir.ot = ir.dflag + OT_WORD;
4999       if (i386_record_modrm (&ir))
5000         return -1;
5001       if (ir.mod == 3)
5002         I386_RECORD_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
5003       else
5004         {
5005           uint64_t tmpu64;
5006           if (i386_record_lea_modrm_addr (&ir, &tmpu64))
5007             return -1;
5008           regcache_raw_read_unsigned (ir.regcache,
5009                                       ir.regmap[ir.reg | rex_r],
5010                                       &tmpulongest);
5011           switch (ir.dflag)
5012             {
5013             case 0:
5014               tmpu64 += ((int16_t) tmpulongest >> 4) << 4;
5015               break;
5016             case 1:
5017               tmpu64 += ((int32_t) tmpulongest >> 5) << 5;
5018               break;
5019             case 2:
5020               tmpu64 += ((int64_t) tmpulongest >> 6) << 6;
5021               break;
5022             }
5023           if (record_arch_list_add_mem (tmpu64, 1 << ir.ot))
5024             return -1;
5025           if (i386_record_lea_modrm (&ir))
5026             return -1;
5027         }
5028       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5029       break;
5030
5031     case 0x0fbc:    /* bsf */
5032     case 0x0fbd:    /* bsr */
5033       I386_RECORD_ARCH_LIST_ADD_REG (ir.reg | rex_r);
5034       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5035       break;
5036
5037       /* bcd */
5038     case 0x27:    /* daa */
5039     case 0x2f:    /* das */
5040     case 0x37:    /* aaa */
5041     case 0x3f:    /* aas */
5042     case 0xd4:    /* aam */
5043     case 0xd5:    /* aad */
5044       if (ir.regmap[X86_RECORD_R8_REGNUM])
5045         {
5046           ir.addr -= 1;
5047           goto no_support;
5048         }
5049       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5050       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5051       break;
5052
5053       /* misc */
5054     case 0x90:    /* nop */
5055       if (prefixes & PREFIX_LOCK)
5056         {
5057           ir.addr -= 1;
5058           goto no_support;
5059         }
5060       break;
5061
5062     case 0x9b:    /* fwait */
5063       if (target_read_memory (ir.addr, &tmpu8, 1))
5064         {
5065           if (record_debug)
5066             printf_unfiltered (_("Process record: error reading memory at "
5067                                  "addr 0x%s len = 1.\n"),
5068                                paddress (gdbarch, ir.addr));
5069           return -1;
5070         }
5071       opcode = (uint32_t) tmpu8;
5072       ir.addr++;
5073       goto reswitch;
5074       break;
5075
5076       /* XXX */
5077     case 0xcc:    /* int3 */
5078       printf_unfiltered (_("Process record doesn't support instruction "
5079                            "int3.\n"));
5080       ir.addr -= 1;
5081       goto no_support;
5082       break;
5083
5084       /* XXX */
5085     case 0xcd:    /* int */
5086       {
5087         int ret;
5088         if (target_read_memory (ir.addr, &tmpu8, 1))
5089           {
5090             if (record_debug)
5091               printf_unfiltered (_("Process record: error reading memory "
5092                                    "at addr %s len = 1.\n"),
5093                                  paddress (gdbarch, ir.addr));
5094             return -1;
5095           }
5096         ir.addr++;
5097         if (tmpu8 != 0x80
5098             || gdbarch_tdep (gdbarch)->i386_intx80_record == NULL)
5099           {
5100             printf_unfiltered (_("Process record doesn't support "
5101                                  "instruction int 0x%02x.\n"),
5102                                tmpu8);
5103             ir.addr -= 2;
5104             goto no_support;
5105           }
5106         ret = gdbarch_tdep (gdbarch)->i386_intx80_record (ir.regcache);
5107         if (ret)
5108           return ret;
5109       }
5110       break;
5111
5112       /* XXX */
5113     case 0xce:    /* into */
5114       printf_unfiltered (_("Process record doesn't support "
5115                            "instruction into.\n"));
5116       ir.addr -= 1;
5117       goto no_support;
5118       break;
5119
5120     case 0xfa:    /* cli */
5121     case 0xfb:    /* sti */
5122       break;
5123
5124     case 0x62:    /* bound */
5125       printf_unfiltered (_("Process record doesn't support "
5126                            "instruction bound.\n"));
5127       ir.addr -= 1;
5128       goto no_support;
5129       break;
5130
5131     case 0x0fc8:    /* bswap reg */
5132     case 0x0fc9:
5133     case 0x0fca:
5134     case 0x0fcb:
5135     case 0x0fcc:
5136     case 0x0fcd:
5137     case 0x0fce:
5138     case 0x0fcf:
5139       I386_RECORD_ARCH_LIST_ADD_REG ((opcode & 7) | ir.rex_b);
5140       break;
5141
5142     case 0xd6:    /* salc */
5143       if (ir.regmap[X86_RECORD_R8_REGNUM])
5144         {
5145           ir.addr -= 1;
5146           goto no_support;
5147         }
5148       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5149       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5150       break;
5151
5152     case 0xe0:    /* loopnz */
5153     case 0xe1:    /* loopz */
5154     case 0xe2:    /* loop */
5155     case 0xe3:    /* jecxz */
5156       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
5157       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5158       break;
5159
5160     case 0x0f30:    /* wrmsr */
5161       printf_unfiltered (_("Process record doesn't support "
5162                            "instruction wrmsr.\n"));
5163       ir.addr -= 2;
5164       goto no_support;
5165       break;
5166
5167     case 0x0f32:    /* rdmsr */
5168       printf_unfiltered (_("Process record doesn't support "
5169                            "instruction rdmsr.\n"));
5170       ir.addr -= 2;
5171       goto no_support;
5172       break;
5173
5174     case 0x0f31:    /* rdtsc */
5175       printf_unfiltered (_("Process record doesn't support "
5176                            "instruction rdtsc.\n"));
5177       ir.addr -= 2;
5178       goto no_support;
5179       break;
5180
5181     case 0x0f34:    /* sysenter */
5182       {
5183         int ret;
5184         if (ir.regmap[X86_RECORD_R8_REGNUM])
5185           {
5186             ir.addr -= 2;
5187             goto no_support;
5188           }
5189         if (gdbarch_tdep (gdbarch)->i386_sysenter_record == NULL)
5190           {
5191             printf_unfiltered (_("Process record doesn't support "
5192                                  "instruction sysenter.\n"));
5193             ir.addr -= 2;
5194             goto no_support;
5195           }
5196         ret = gdbarch_tdep (gdbarch)->i386_sysenter_record (ir.regcache);
5197         if (ret)
5198           return ret;
5199       }
5200       break;
5201
5202     case 0x0f35:    /* sysexit */
5203       printf_unfiltered (_("Process record doesn't support "
5204                            "instruction sysexit.\n"));
5205       ir.addr -= 2;
5206       goto no_support;
5207       break;
5208
5209     case 0x0f05:    /* syscall */
5210       {
5211         int ret;
5212         if (gdbarch_tdep (gdbarch)->i386_syscall_record == NULL)
5213           {
5214             printf_unfiltered (_("Process record doesn't support "
5215                                  "instruction syscall.\n"));
5216             ir.addr -= 2;
5217             goto no_support;
5218           }
5219         ret = gdbarch_tdep (gdbarch)->i386_syscall_record (ir.regcache);
5220         if (ret)
5221           return ret;
5222       }
5223       break;
5224
5225     case 0x0f07:    /* sysret */
5226       printf_unfiltered (_("Process record doesn't support "
5227                            "instruction sysret.\n"));
5228       ir.addr -= 2;
5229       goto no_support;
5230       break;
5231
5232     case 0x0fa2:    /* cpuid */
5233       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5234       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
5235       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
5236       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REBX_REGNUM);
5237       break;
5238
5239     case 0xf4:    /* hlt */
5240       printf_unfiltered (_("Process record doesn't support "
5241                            "instruction hlt.\n"));
5242       ir.addr -= 1;
5243       goto no_support;
5244       break;
5245
5246     case 0x0f00:
5247       if (i386_record_modrm (&ir))
5248         return -1;
5249       switch (ir.reg)
5250         {
5251         case 0:  /* sldt */
5252         case 1:  /* str  */
5253           if (ir.mod == 3)
5254             I386_RECORD_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
5255           else
5256             {
5257               ir.ot = OT_WORD;
5258               if (i386_record_lea_modrm (&ir))
5259                 return -1;
5260             }
5261           break;
5262         case 2:  /* lldt */
5263         case 3:  /* ltr */
5264           break;
5265         case 4:  /* verr */
5266         case 5:  /* verw */
5267           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5268           break;
5269         default:
5270           ir.addr -= 3;
5271           opcode = opcode << 8 | ir.modrm;
5272           goto no_support;
5273           break;
5274         }
5275       break;
5276
5277     case 0x0f01:
5278       if (i386_record_modrm (&ir))
5279         return -1;
5280       switch (ir.reg)
5281         {
5282         case 0:  /* sgdt */
5283           {
5284             uint64_t tmpu64;
5285
5286             if (ir.mod == 3)
5287               {
5288                 ir.addr -= 3;
5289                 opcode = opcode << 8 | ir.modrm;
5290                 goto no_support;
5291               }
5292             if (ir.override >= 0)
5293               {
5294                 warning (_("Process record ignores the memory "
5295                            "change of instruction at "
5296                            "address %s because it can't get "
5297                            "the value of the segment "
5298                            "register."),
5299                          paddress (gdbarch, ir.orig_addr));
5300               }
5301             else
5302               {
5303                 if (i386_record_lea_modrm_addr (&ir, &tmpu64))
5304                   return -1;
5305                 if (record_arch_list_add_mem (tmpu64, 2))
5306                   return -1;
5307                 tmpu64 += 2;
5308                 if (ir.regmap[X86_RECORD_R8_REGNUM])
5309                   {
5310                     if (record_arch_list_add_mem (tmpu64, 8))
5311                       return -1;
5312                   }
5313                 else
5314                   {
5315                     if (record_arch_list_add_mem (tmpu64, 4))
5316                       return -1;
5317                   }
5318               }
5319           }
5320           break;
5321         case 1:
5322           if (ir.mod == 3)
5323             {
5324               switch (ir.rm)
5325                 {
5326                 case 0:  /* monitor */
5327                   break;
5328                 case 1:  /* mwait */
5329                   I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5330                   break;
5331                 default:
5332                   ir.addr -= 3;
5333                   opcode = opcode << 8 | ir.modrm;
5334                   goto no_support;
5335                   break;
5336                 }
5337             }
5338           else
5339             {
5340               /* sidt */
5341               if (ir.override >= 0)
5342                 {
5343                   warning (_("Process record ignores the memory "
5344                              "change of instruction at "
5345                              "address %s because it can't get "
5346                              "the value of the segment "
5347                              "register."),
5348                            paddress (gdbarch, ir.orig_addr));
5349                 }
5350               else
5351                 {
5352                   uint64_t tmpu64;
5353
5354                   if (i386_record_lea_modrm_addr (&ir, &tmpu64))
5355                     return -1;
5356                   if (record_arch_list_add_mem (tmpu64, 2))
5357                     return -1;
5358                   addr += 2;
5359                   if (ir.regmap[X86_RECORD_R8_REGNUM])
5360                     {
5361                       if (record_arch_list_add_mem (tmpu64, 8))
5362                         return -1;
5363                     }
5364                   else
5365                     {
5366                       if (record_arch_list_add_mem (tmpu64, 4))
5367                         return -1;
5368                     }
5369                 }
5370             }
5371           break;
5372         case 2:  /* lgdt */
5373           if (ir.mod == 3)
5374             {
5375               /* xgetbv */
5376               if (ir.rm == 0)
5377                 {
5378                   I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5379                   I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
5380                   break;
5381                 }
5382               /* xsetbv */
5383               else if (ir.rm == 1)
5384                 break;
5385             }
5386         case 3:  /* lidt */
5387           if (ir.mod == 3)
5388             {
5389               ir.addr -= 3;
5390               opcode = opcode << 8 | ir.modrm;
5391               goto no_support;
5392             }
5393           break;
5394         case 4:  /* smsw */
5395           if (ir.mod == 3)
5396             {
5397               if (record_arch_list_add_reg (ir.regcache, ir.rm | ir.rex_b))
5398                 return -1;
5399             }
5400           else
5401             {
5402               ir.ot = OT_WORD;
5403               if (i386_record_lea_modrm (&ir))
5404                 return -1;
5405             }
5406           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5407           break;
5408         case 6:  /* lmsw */
5409           I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5410           break;
5411         case 7:  /* invlpg */
5412           if (ir.mod == 3)
5413             {
5414               if (ir.rm == 0 && ir.regmap[X86_RECORD_R8_REGNUM])
5415                 I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_GS_REGNUM);
5416               else
5417                 {
5418                   ir.addr -= 3;
5419                   opcode = opcode << 8 | ir.modrm;
5420                   goto no_support;
5421                 }
5422             }
5423           else
5424             I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5425           break;
5426         default:
5427           ir.addr -= 3;
5428           opcode = opcode << 8 | ir.modrm;
5429           goto no_support;
5430           break;
5431         }
5432       break;
5433
5434     case 0x0f08:    /* invd */
5435     case 0x0f09:    /* wbinvd */
5436       break;
5437
5438     case 0x63:    /* arpl */
5439       if (i386_record_modrm (&ir))
5440         return -1;
5441       if (ir.mod == 3 || ir.regmap[X86_RECORD_R8_REGNUM])
5442         {
5443           I386_RECORD_ARCH_LIST_ADD_REG (ir.regmap[X86_RECORD_R8_REGNUM]
5444                                            ? (ir.reg | rex_r) : ir.rm);
5445         }
5446       else
5447         {
5448           ir.ot = ir.dflag ? OT_LONG : OT_WORD;
5449           if (i386_record_lea_modrm (&ir))
5450             return -1;
5451         }
5452       if (!ir.regmap[X86_RECORD_R8_REGNUM])
5453         I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5454       break;
5455
5456     case 0x0f02:    /* lar */
5457     case 0x0f03:    /* lsl */
5458       if (i386_record_modrm (&ir))
5459         return -1;
5460       I386_RECORD_ARCH_LIST_ADD_REG (ir.reg | rex_r);
5461       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5462       break;
5463
5464     case 0x0f18:
5465       if (i386_record_modrm (&ir))
5466         return -1;
5467       if (ir.mod == 3 && ir.reg == 3)
5468         {
5469           ir.addr -= 3;
5470           opcode = opcode << 8 | ir.modrm;
5471           goto no_support;
5472         }
5473       break;
5474
5475     case 0x0f19:
5476     case 0x0f1a:
5477     case 0x0f1b:
5478     case 0x0f1c:
5479     case 0x0f1d:
5480     case 0x0f1e:
5481     case 0x0f1f:
5482       /* nop (multi byte) */
5483       break;
5484
5485     case 0x0f20:    /* mov reg, crN */
5486     case 0x0f22:    /* mov crN, reg */
5487       if (i386_record_modrm (&ir))
5488         return -1;
5489       if ((ir.modrm & 0xc0) != 0xc0)
5490         {
5491           ir.addr -= 3;
5492           opcode = opcode << 8 | ir.modrm;
5493           goto no_support;
5494         }
5495       switch (ir.reg)
5496         {
5497         case 0:
5498         case 2:
5499         case 3:
5500         case 4:
5501         case 8:
5502           if (opcode & 2)
5503             I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5504           else
5505             I386_RECORD_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
5506           break;
5507         default:
5508           ir.addr -= 3;
5509           opcode = opcode << 8 | ir.modrm;
5510           goto no_support;
5511           break;
5512         }
5513       break;
5514
5515     case 0x0f21:    /* mov reg, drN */
5516     case 0x0f23:    /* mov drN, reg */
5517       if (i386_record_modrm (&ir))
5518         return -1;
5519       if ((ir.modrm & 0xc0) != 0xc0 || ir.reg == 4
5520           || ir.reg == 5 || ir.reg >= 8)
5521         {
5522           ir.addr -= 3;
5523           opcode = opcode << 8 | ir.modrm;
5524           goto no_support;
5525         }
5526       if (opcode & 2)
5527         I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5528       else
5529         I386_RECORD_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
5530       break;
5531
5532     case 0x0f06:    /* clts */
5533       I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5534       break;
5535
5536       /* MMX/SSE/SSE2/PNI support */
5537       /* XXX */
5538
5539     default:
5540       if (opcode > 0xff)
5541         ir.addr -= 2;
5542       else
5543         ir.addr -= 1;
5544       goto no_support;
5545       break;
5546     }
5547
5548   /* In the future, maybe still need to deal with need_dasm.  */
5549   I386_RECORD_ARCH_LIST_ADD_REG (X86_RECORD_REIP_REGNUM);
5550   if (record_arch_list_add_end ())
5551     return -1;
5552
5553   return 0;
5554
5555  no_support:
5556   printf_unfiltered (_("Process record doesn't support instruction 0x%02x "
5557                        "at address %s.\n"),
5558                      (unsigned int) (opcode), paddress (gdbarch, ir.addr));
5559   return -1;
5560 }
5561
5562 static const int i386_record_regmap[] =
5563 {
5564   I386_EAX_REGNUM, I386_ECX_REGNUM, I386_EDX_REGNUM, I386_EBX_REGNUM,
5565   I386_ESP_REGNUM, I386_EBP_REGNUM, I386_ESI_REGNUM, I386_EDI_REGNUM,
5566   0, 0, 0, 0, 0, 0, 0, 0,
5567   I386_EIP_REGNUM, I386_EFLAGS_REGNUM, I386_CS_REGNUM, I386_SS_REGNUM,
5568   I386_DS_REGNUM, I386_ES_REGNUM, I386_FS_REGNUM, I386_GS_REGNUM
5569 };
5570
5571 /* Check that the given address appears suitable for a fast
5572    tracepoint, which on x86 means that we need an instruction of at
5573    least 5 bytes, so that we can overwrite it with a 4-byte-offset
5574    jump and not have to worry about program jumps to an address in the
5575    middle of the tracepoint jump.  Returns 1 if OK, and writes a size
5576    of instruction to replace, and 0 if not, plus an explanatory
5577    string.  */
5578
5579 static int
5580 i386_fast_tracepoint_valid_at (struct gdbarch *gdbarch,
5581                                CORE_ADDR addr, int *isize, char **msg)
5582 {
5583   int len, jumplen;
5584   static struct ui_file *gdb_null = NULL;
5585
5586   /* This is based on the target agent using a 4-byte relative jump.
5587      Alternate future possibilities include 8-byte offset for x86-84,
5588      or 3-byte jumps if the program has trampoline space close by.  */
5589   jumplen = 5;
5590
5591   /* Dummy file descriptor for the disassembler.  */
5592   if (!gdb_null)
5593     gdb_null = ui_file_new ();
5594
5595   /* Check for fit.  */
5596   len = gdb_print_insn (gdbarch, addr, gdb_null, NULL);
5597   if (len < jumplen)
5598     {
5599       /* Return a bit of target-specific detail to add to the caller's
5600          generic failure message.  */
5601       if (msg)
5602         *msg = xstrprintf (_("; instruction is only %d bytes long, need at least %d bytes for the jump"),
5603                            len, jumplen);
5604       return 0;
5605     }
5606
5607   if (isize)
5608     *isize = len;
5609   if (msg)
5610     *msg = NULL;
5611   return 1;
5612 }
5613
5614 \f
5615 static struct gdbarch *
5616 i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
5617 {
5618   struct gdbarch_tdep *tdep;
5619   struct gdbarch *gdbarch;
5620
5621   /* If there is already a candidate, use it.  */
5622   arches = gdbarch_list_lookup_by_info (arches, &info);
5623   if (arches != NULL)
5624     return arches->gdbarch;
5625
5626   /* Allocate space for the new architecture.  */
5627   tdep = XCALLOC (1, struct gdbarch_tdep);
5628   gdbarch = gdbarch_alloc (&info, tdep);
5629
5630   /* General-purpose registers.  */
5631   tdep->gregset = NULL;
5632   tdep->gregset_reg_offset = NULL;
5633   tdep->gregset_num_regs = I386_NUM_GREGS;
5634   tdep->sizeof_gregset = 0;
5635
5636   /* Floating-point registers.  */
5637   tdep->fpregset = NULL;
5638   tdep->sizeof_fpregset = I387_SIZEOF_FSAVE;
5639
5640   /* The default settings include the FPU registers, the MMX registers
5641      and the SSE registers.  This can be overridden for a specific ABI
5642      by adjusting the members `st0_regnum', `mm0_regnum' and
5643      `num_xmm_regs' of `struct gdbarch_tdep', otherwise the registers
5644      will show up in the output of "info all-registers".  Ideally we
5645      should try to autodetect whether they are available, such that we
5646      can prevent "info all-registers" from displaying registers that
5647      aren't available.
5648
5649      NOTE: kevinb/2003-07-13: ... if it's a choice between printing
5650      [the SSE registers] always (even when they don't exist) or never
5651      showing them to the user (even when they do exist), I prefer the
5652      former over the latter.  */
5653
5654   tdep->st0_regnum = I386_ST0_REGNUM;
5655
5656   /* The MMX registers are implemented as pseudo-registers.  Put off
5657      calculating the register number for %mm0 until we know the number
5658      of raw registers.  */
5659   tdep->mm0_regnum = 0;
5660
5661   /* I386_NUM_XREGS includes %mxcsr, so substract one.  */
5662   tdep->num_xmm_regs = I386_NUM_XREGS - 1;
5663
5664   tdep->jb_pc_offset = -1;
5665   tdep->struct_return = pcc_struct_return;
5666   tdep->sigtramp_start = 0;
5667   tdep->sigtramp_end = 0;
5668   tdep->sigtramp_p = i386_sigtramp_p;
5669   tdep->sigcontext_addr = NULL;
5670   tdep->sc_reg_offset = NULL;
5671   tdep->sc_pc_offset = -1;
5672   tdep->sc_sp_offset = -1;
5673
5674   tdep->record_regmap = i386_record_regmap;
5675
5676   /* The format used for `long double' on almost all i386 targets is
5677      the i387 extended floating-point format.  In fact, of all targets
5678      in the GCC 2.95 tree, only OSF/1 does it different, and insists
5679      on having a `long double' that's not `long' at all.  */
5680   set_gdbarch_long_double_format (gdbarch, floatformats_i387_ext);
5681
5682   /* Although the i387 extended floating-point has only 80 significant
5683      bits, a `long double' actually takes up 96, probably to enforce
5684      alignment.  */
5685   set_gdbarch_long_double_bit (gdbarch, 96);
5686
5687   /* The default ABI includes general-purpose registers, 
5688      floating-point registers, and the SSE registers.  */
5689   set_gdbarch_num_regs (gdbarch, I386_SSE_NUM_REGS);
5690   set_gdbarch_register_name (gdbarch, i386_register_name);
5691   set_gdbarch_register_type (gdbarch, i386_register_type);
5692
5693   /* Register numbers of various important registers.  */
5694   set_gdbarch_sp_regnum (gdbarch, I386_ESP_REGNUM); /* %esp */
5695   set_gdbarch_pc_regnum (gdbarch, I386_EIP_REGNUM); /* %eip */
5696   set_gdbarch_ps_regnum (gdbarch, I386_EFLAGS_REGNUM); /* %eflags */
5697   set_gdbarch_fp0_regnum (gdbarch, I386_ST0_REGNUM); /* %st(0) */
5698
5699   /* NOTE: kettenis/20040418: GCC does have two possible register
5700      numbering schemes on the i386: dbx and SVR4.  These schemes
5701      differ in how they number %ebp, %esp, %eflags, and the
5702      floating-point registers, and are implemented by the arrays
5703      dbx_register_map[] and svr4_dbx_register_map in
5704      gcc/config/i386.c.  GCC also defines a third numbering scheme in
5705      gcc/config/i386.c, which it designates as the "default" register
5706      map used in 64bit mode.  This last register numbering scheme is
5707      implemented in dbx64_register_map, and is used for AMD64; see
5708      amd64-tdep.c.
5709
5710      Currently, each GCC i386 target always uses the same register
5711      numbering scheme across all its supported debugging formats
5712      i.e. SDB (COFF), stabs and DWARF 2.  This is because
5713      gcc/sdbout.c, gcc/dbxout.c and gcc/dwarf2out.c all use the
5714      DBX_REGISTER_NUMBER macro which is defined by each target's
5715      respective config header in a manner independent of the requested
5716      output debugging format.
5717
5718      This does not match the arrangement below, which presumes that
5719      the SDB and stabs numbering schemes differ from the DWARF and
5720      DWARF 2 ones.  The reason for this arrangement is that it is
5721      likely to get the numbering scheme for the target's
5722      default/native debug format right.  For targets where GCC is the
5723      native compiler (FreeBSD, NetBSD, OpenBSD, GNU/Linux) or for
5724      targets where the native toolchain uses a different numbering
5725      scheme for a particular debug format (stabs-in-ELF on Solaris)
5726      the defaults below will have to be overridden, like
5727      i386_elf_init_abi() does.  */
5728
5729   /* Use the dbx register numbering scheme for stabs and COFF.  */
5730   set_gdbarch_stab_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum);
5731   set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum);
5732
5733   /* Use the SVR4 register numbering scheme for DWARF 2.  */
5734   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
5735
5736   /* We don't set gdbarch_stab_reg_to_regnum, since ECOFF doesn't seem to
5737      be in use on any of the supported i386 targets.  */
5738
5739   set_gdbarch_print_float_info (gdbarch, i387_print_float_info);
5740
5741   set_gdbarch_get_longjmp_target (gdbarch, i386_get_longjmp_target);
5742
5743   /* Call dummy code.  */
5744   set_gdbarch_push_dummy_call (gdbarch, i386_push_dummy_call);
5745
5746   set_gdbarch_convert_register_p (gdbarch, i386_convert_register_p);
5747   set_gdbarch_register_to_value (gdbarch,  i386_register_to_value);
5748   set_gdbarch_value_to_register (gdbarch, i386_value_to_register);
5749
5750   set_gdbarch_return_value (gdbarch, i386_return_value);
5751
5752   set_gdbarch_skip_prologue (gdbarch, i386_skip_prologue);
5753
5754   /* Stack grows downward.  */
5755   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
5756
5757   set_gdbarch_breakpoint_from_pc (gdbarch, i386_breakpoint_from_pc);
5758   set_gdbarch_decr_pc_after_break (gdbarch, 1);
5759   set_gdbarch_max_insn_length (gdbarch, I386_MAX_INSN_LEN);
5760
5761   set_gdbarch_frame_args_skip (gdbarch, 8);
5762
5763   /* Wire in the MMX registers.  */
5764   set_gdbarch_num_pseudo_regs (gdbarch, i386_num_mmx_regs);
5765   set_gdbarch_pseudo_register_read (gdbarch, i386_pseudo_register_read);
5766   set_gdbarch_pseudo_register_write (gdbarch, i386_pseudo_register_write);
5767
5768   set_gdbarch_print_insn (gdbarch, i386_print_insn);
5769
5770   set_gdbarch_dummy_id (gdbarch, i386_dummy_id);
5771
5772   set_gdbarch_unwind_pc (gdbarch, i386_unwind_pc);
5773
5774   /* Add the i386 register groups.  */
5775   i386_add_reggroups (gdbarch);
5776   set_gdbarch_register_reggroup_p (gdbarch, i386_register_reggroup_p);
5777
5778   /* Helper for function argument information.  */
5779   set_gdbarch_fetch_pointer_argument (gdbarch, i386_fetch_pointer_argument);
5780
5781   /* Hook the function epilogue frame unwinder.  This unwinder is
5782      appended to the list first, so that it supercedes the Dwarf
5783      unwinder in function epilogues (where the Dwarf unwinder
5784      currently fails).  */
5785   frame_unwind_append_unwinder (gdbarch, &i386_epilogue_frame_unwind);
5786
5787   /* Hook in the DWARF CFI frame unwinder.  This unwinder is appended
5788      to the list before the prologue-based unwinders, so that Dwarf
5789      CFI info will be used if it is available.  */
5790   dwarf2_append_unwinders (gdbarch);
5791
5792   frame_base_set_default (gdbarch, &i386_frame_base);
5793
5794   /* Hook in ABI-specific overrides, if they have been registered.  */
5795   gdbarch_init_osabi (info, gdbarch);
5796
5797   /* Hook in the legacy prologue-based unwinders last (fallback).  */
5798   frame_unwind_append_unwinder (gdbarch, &i386_sigtramp_frame_unwind);
5799   frame_unwind_append_unwinder (gdbarch, &i386_frame_unwind);
5800
5801   /* If we have a register mapping, enable the generic core file
5802      support, unless it has already been enabled.  */
5803   if (tdep->gregset_reg_offset
5804       && !gdbarch_regset_from_core_section_p (gdbarch))
5805     set_gdbarch_regset_from_core_section (gdbarch,
5806                                           i386_regset_from_core_section);
5807
5808   /* Unless support for MMX has been disabled, make %mm0 the first
5809      pseudo-register.  */
5810   if (tdep->mm0_regnum == 0)
5811     tdep->mm0_regnum = gdbarch_num_regs (gdbarch);
5812
5813   set_gdbarch_skip_permanent_breakpoint (gdbarch,
5814                                          i386_skip_permanent_breakpoint);
5815
5816   set_gdbarch_fast_tracepoint_valid_at (gdbarch,
5817                                         i386_fast_tracepoint_valid_at);
5818
5819   return gdbarch;
5820 }
5821
5822 static enum gdb_osabi
5823 i386_coff_osabi_sniffer (bfd *abfd)
5824 {
5825   if (strcmp (bfd_get_target (abfd), "coff-go32-exe") == 0
5826       || strcmp (bfd_get_target (abfd), "coff-go32") == 0)
5827     return GDB_OSABI_GO32;
5828
5829   return GDB_OSABI_UNKNOWN;
5830 }
5831 \f
5832
5833 /* Provide a prototype to silence -Wmissing-prototypes.  */
5834 void _initialize_i386_tdep (void);
5835
5836 void
5837 _initialize_i386_tdep (void)
5838 {
5839   register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
5840
5841   /* Add the variable that controls the disassembly flavor.  */
5842   add_setshow_enum_cmd ("disassembly-flavor", no_class, valid_flavors,
5843                         &disassembly_flavor, _("\
5844 Set the disassembly flavor."), _("\
5845 Show the disassembly flavor."), _("\
5846 The valid values are \"att\" and \"intel\", and the default value is \"att\"."),
5847                         NULL,
5848                         NULL, /* FIXME: i18n: */
5849                         &setlist, &showlist);
5850
5851   /* Add the variable that controls the convention for returning
5852      structs.  */
5853   add_setshow_enum_cmd ("struct-convention", no_class, valid_conventions,
5854                         &struct_convention, _("\
5855 Set the convention for returning small structs."), _("\
5856 Show the convention for returning small structs."), _("\
5857 Valid values are \"default\", \"pcc\" and \"reg\", and the default value\n\
5858 is \"default\"."),
5859                         NULL,
5860                         NULL, /* FIXME: i18n: */
5861                         &setlist, &showlist);
5862
5863   gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
5864                                   i386_coff_osabi_sniffer);
5865
5866   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_SVR4,
5867                           i386_svr4_init_abi);
5868   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_GO32,
5869                           i386_go32_init_abi);
5870
5871   /* Initialize the i386-specific register groups.  */
5872   i386_init_reggroups ();
5873 }