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