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