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