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