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