]> sourceware.org Git - binutils-gdb.git/blob - gdb/i386-tdep.c
a552a2bee8fe0245125c77e2a04daaf4bf558dfd
[binutils-gdb.git] / gdb / i386-tdep.c
1 /* Intel 386 target-dependent stuff.
2
3 Copyright (C) 1988-2024 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "extract-store-integer.h"
21 #include "opcode/i386.h"
22 #include "arch-utils.h"
23 #include "command.h"
24 #include "dummy-frame.h"
25 #include "dwarf2/frame.h"
26 #include "frame.h"
27 #include "frame-base.h"
28 #include "frame-unwind.h"
29 #include "inferior.h"
30 #include "infrun.h"
31 #include "cli/cli-cmds.h"
32 #include "gdbcore.h"
33 #include "gdbtypes.h"
34 #include "objfiles.h"
35 #include "osabi.h"
36 #include "regcache.h"
37 #include "reggroups.h"
38 #include "regset.h"
39 #include "symfile.h"
40 #include "symtab.h"
41 #include "target.h"
42 #include "target-float.h"
43 #include "value.h"
44 #include "dis-asm.h"
45 #include "disasm.h"
46 #include "remote.h"
47 #include "i386-tdep.h"
48 #include "i387-tdep.h"
49 #include "gdbsupport/x86-xstate.h"
50 #include "x86-tdep.h"
51 #include "expop.h"
52
53 #include "record.h"
54 #include "record-full.h"
55 #include "target-descriptions.h"
56 #include "arch/i386.h"
57
58 #include "ax.h"
59 #include "ax-gdb.h"
60
61 #include "stap-probe.h"
62 #include "user-regs.h"
63 #include "cli/cli-utils.h"
64 #include "expression.h"
65 #include "parser-defs.h"
66 #include <ctype.h>
67 #include <algorithm>
68 #include <unordered_set>
69 #include "producer.h"
70 #include "infcall.h"
71 #include "maint.h"
72
73 /* Register names. */
74
75 static const char * const i386_register_names[] =
76 {
77 "eax", "ecx", "edx", "ebx",
78 "esp", "ebp", "esi", "edi",
79 "eip", "eflags", "cs", "ss",
80 "ds", "es", "fs", "gs",
81 "st0", "st1", "st2", "st3",
82 "st4", "st5", "st6", "st7",
83 "fctrl", "fstat", "ftag", "fiseg",
84 "fioff", "foseg", "fooff", "fop",
85 "xmm0", "xmm1", "xmm2", "xmm3",
86 "xmm4", "xmm5", "xmm6", "xmm7",
87 "mxcsr"
88 };
89
90 static const char * const i386_zmm_names[] =
91 {
92 "zmm0", "zmm1", "zmm2", "zmm3",
93 "zmm4", "zmm5", "zmm6", "zmm7"
94 };
95
96 static const char * const i386_zmmh_names[] =
97 {
98 "zmm0h", "zmm1h", "zmm2h", "zmm3h",
99 "zmm4h", "zmm5h", "zmm6h", "zmm7h"
100 };
101
102 static const char * const i386_k_names[] =
103 {
104 "k0", "k1", "k2", "k3",
105 "k4", "k5", "k6", "k7"
106 };
107
108 static const char * const i386_ymm_names[] =
109 {
110 "ymm0", "ymm1", "ymm2", "ymm3",
111 "ymm4", "ymm5", "ymm6", "ymm7",
112 };
113
114 static const char * const i386_ymmh_names[] =
115 {
116 "ymm0h", "ymm1h", "ymm2h", "ymm3h",
117 "ymm4h", "ymm5h", "ymm6h", "ymm7h",
118 };
119
120
121 static const char * const i386_pkeys_names[] =
122 {
123 "pkru"
124 };
125
126 /* Register names for MMX pseudo-registers. */
127
128 static const char * const i386_mmx_names[] =
129 {
130 "mm0", "mm1", "mm2", "mm3",
131 "mm4", "mm5", "mm6", "mm7"
132 };
133
134 /* Register names for byte pseudo-registers. */
135
136 static const char * const i386_byte_names[] =
137 {
138 "al", "cl", "dl", "bl",
139 "ah", "ch", "dh", "bh"
140 };
141
142 /* Register names for word pseudo-registers. */
143
144 static const char * const i386_word_names[] =
145 {
146 "ax", "cx", "dx", "bx",
147 "", "bp", "si", "di"
148 };
149
150 /* Constant used for reading/writing pseudo registers. In 64-bit mode, we have
151 16 lower ZMM regs that extend corresponding xmm/ymm registers. In addition,
152 we have 16 upper ZMM regs that have to be handled differently. */
153
154 const int num_lower_zmm_regs = 16;
155
156 /* MMX register? */
157
158 static int
159 i386_mmx_regnum_p (struct gdbarch *gdbarch, int regnum)
160 {
161 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
162 int mm0_regnum = tdep->mm0_regnum;
163
164 if (mm0_regnum < 0)
165 return 0;
166
167 regnum -= mm0_regnum;
168 return regnum >= 0 && regnum < tdep->num_mmx_regs;
169 }
170
171 /* Byte register? */
172
173 int
174 i386_byte_regnum_p (struct gdbarch *gdbarch, int regnum)
175 {
176 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
177
178 regnum -= tdep->al_regnum;
179 return regnum >= 0 && regnum < tdep->num_byte_regs;
180 }
181
182 /* Word register? */
183
184 int
185 i386_word_regnum_p (struct gdbarch *gdbarch, int regnum)
186 {
187 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
188
189 regnum -= tdep->ax_regnum;
190 return regnum >= 0 && regnum < tdep->num_word_regs;
191 }
192
193 /* Dword register? */
194
195 int
196 i386_dword_regnum_p (struct gdbarch *gdbarch, int regnum)
197 {
198 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
199 int eax_regnum = tdep->eax_regnum;
200
201 if (eax_regnum < 0)
202 return 0;
203
204 regnum -= eax_regnum;
205 return regnum >= 0 && regnum < tdep->num_dword_regs;
206 }
207
208 /* AVX512 register? */
209
210 int
211 i386_zmmh_regnum_p (struct gdbarch *gdbarch, int regnum)
212 {
213 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
214 int zmm0h_regnum = tdep->zmm0h_regnum;
215
216 if (zmm0h_regnum < 0)
217 return 0;
218
219 regnum -= zmm0h_regnum;
220 return regnum >= 0 && regnum < tdep->num_zmm_regs;
221 }
222
223 int
224 i386_zmm_regnum_p (struct gdbarch *gdbarch, int regnum)
225 {
226 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
227 int zmm0_regnum = tdep->zmm0_regnum;
228
229 if (zmm0_regnum < 0)
230 return 0;
231
232 regnum -= zmm0_regnum;
233 return regnum >= 0 && regnum < tdep->num_zmm_regs;
234 }
235
236 int
237 i386_k_regnum_p (struct gdbarch *gdbarch, int regnum)
238 {
239 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
240 int k0_regnum = tdep->k0_regnum;
241
242 if (k0_regnum < 0)
243 return 0;
244
245 regnum -= k0_regnum;
246 return regnum >= 0 && regnum < I387_NUM_K_REGS;
247 }
248
249 static int
250 i386_ymmh_regnum_p (struct gdbarch *gdbarch, int regnum)
251 {
252 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
253 int ymm0h_regnum = tdep->ymm0h_regnum;
254
255 if (ymm0h_regnum < 0)
256 return 0;
257
258 regnum -= ymm0h_regnum;
259 return regnum >= 0 && regnum < tdep->num_ymm_regs;
260 }
261
262 /* AVX register? */
263
264 int
265 i386_ymm_regnum_p (struct gdbarch *gdbarch, int regnum)
266 {
267 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
268 int ymm0_regnum = tdep->ymm0_regnum;
269
270 if (ymm0_regnum < 0)
271 return 0;
272
273 regnum -= ymm0_regnum;
274 return regnum >= 0 && regnum < tdep->num_ymm_regs;
275 }
276
277 static int
278 i386_ymmh_avx512_regnum_p (struct gdbarch *gdbarch, int regnum)
279 {
280 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
281 int ymm16h_regnum = tdep->ymm16h_regnum;
282
283 if (ymm16h_regnum < 0)
284 return 0;
285
286 regnum -= ymm16h_regnum;
287 return regnum >= 0 && regnum < tdep->num_ymm_avx512_regs;
288 }
289
290 int
291 i386_ymm_avx512_regnum_p (struct gdbarch *gdbarch, int regnum)
292 {
293 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
294 int ymm16_regnum = tdep->ymm16_regnum;
295
296 if (ymm16_regnum < 0)
297 return 0;
298
299 regnum -= ymm16_regnum;
300 return regnum >= 0 && regnum < tdep->num_ymm_avx512_regs;
301 }
302
303 /* SSE register? */
304
305 int
306 i386_xmm_regnum_p (struct gdbarch *gdbarch, int regnum)
307 {
308 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
309 int num_xmm_regs = I387_NUM_XMM_REGS (tdep);
310
311 if (num_xmm_regs == 0)
312 return 0;
313
314 regnum -= I387_XMM0_REGNUM (tdep);
315 return regnum >= 0 && regnum < num_xmm_regs;
316 }
317
318 /* XMM_512 register? */
319
320 int
321 i386_xmm_avx512_regnum_p (struct gdbarch *gdbarch, int regnum)
322 {
323 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
324 int num_xmm_avx512_regs = I387_NUM_XMM_AVX512_REGS (tdep);
325
326 if (num_xmm_avx512_regs == 0)
327 return 0;
328
329 regnum -= I387_XMM16_REGNUM (tdep);
330 return regnum >= 0 && regnum < num_xmm_avx512_regs;
331 }
332
333 static int
334 i386_mxcsr_regnum_p (struct gdbarch *gdbarch, int regnum)
335 {
336 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
337
338 if (I387_NUM_XMM_REGS (tdep) == 0)
339 return 0;
340
341 return (regnum == I387_MXCSR_REGNUM (tdep));
342 }
343
344 /* FP register? */
345
346 int
347 i386_fp_regnum_p (struct gdbarch *gdbarch, int regnum)
348 {
349 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
350
351 if (I387_ST0_REGNUM (tdep) < 0)
352 return 0;
353
354 return (I387_ST0_REGNUM (tdep) <= regnum
355 && regnum < I387_FCTRL_REGNUM (tdep));
356 }
357
358 int
359 i386_fpc_regnum_p (struct gdbarch *gdbarch, int regnum)
360 {
361 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
362
363 if (I387_ST0_REGNUM (tdep) < 0)
364 return 0;
365
366 return (I387_FCTRL_REGNUM (tdep) <= regnum
367 && regnum < I387_XMM0_REGNUM (tdep));
368 }
369
370 /* PKRU register? */
371
372 bool
373 i386_pkru_regnum_p (struct gdbarch *gdbarch, int regnum)
374 {
375 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
376 int pkru_regnum = tdep->pkru_regnum;
377
378 if (pkru_regnum < 0)
379 return false;
380
381 regnum -= pkru_regnum;
382 return regnum >= 0 && regnum < I387_NUM_PKEYS_REGS;
383 }
384
385 /* Return the name of register REGNUM, or the empty string if it is
386 an anonymous register. */
387
388 static const char *
389 i386_register_name (struct gdbarch *gdbarch, int regnum)
390 {
391 /* Hide the upper YMM registers. */
392 if (i386_ymmh_regnum_p (gdbarch, regnum))
393 return "";
394
395 /* Hide the upper YMM16-31 registers. */
396 if (i386_ymmh_avx512_regnum_p (gdbarch, regnum))
397 return "";
398
399 /* Hide the upper ZMM registers. */
400 if (i386_zmmh_regnum_p (gdbarch, regnum))
401 return "";
402
403 return tdesc_register_name (gdbarch, regnum);
404 }
405
406 /* Return the name of register REGNUM. */
407
408 const char *
409 i386_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
410 {
411 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
412 if (i386_mmx_regnum_p (gdbarch, regnum))
413 return i386_mmx_names[regnum - I387_MM0_REGNUM (tdep)];
414 else if (i386_ymm_regnum_p (gdbarch, regnum))
415 return i386_ymm_names[regnum - tdep->ymm0_regnum];
416 else if (i386_zmm_regnum_p (gdbarch, regnum))
417 return i386_zmm_names[regnum - tdep->zmm0_regnum];
418 else if (i386_byte_regnum_p (gdbarch, regnum))
419 return i386_byte_names[regnum - tdep->al_regnum];
420 else if (i386_word_regnum_p (gdbarch, regnum))
421 return i386_word_names[regnum - tdep->ax_regnum];
422
423 internal_error (_("invalid regnum"));
424 }
425
426 /* Convert a dbx register number REG to the appropriate register
427 number used by GDB. */
428
429 static int
430 i386_dbx_reg_to_regnum (struct gdbarch *gdbarch, int reg)
431 {
432 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
433
434 /* This implements what GCC calls the "default" register map
435 (dbx_register_map[]). */
436
437 if (reg >= 0 && reg <= 7)
438 {
439 /* General-purpose registers. The debug info calls %ebp
440 register 4, and %esp register 5. */
441 if (reg == 4)
442 return 5;
443 else if (reg == 5)
444 return 4;
445 else return reg;
446 }
447 else if (reg >= 12 && reg <= 19)
448 {
449 /* Floating-point registers. */
450 return reg - 12 + I387_ST0_REGNUM (tdep);
451 }
452 else if (reg >= 21 && reg <= 28)
453 {
454 /* SSE registers. */
455 int ymm0_regnum = tdep->ymm0_regnum;
456
457 if (ymm0_regnum >= 0
458 && i386_xmm_regnum_p (gdbarch, reg))
459 return reg - 21 + ymm0_regnum;
460 else
461 return reg - 21 + I387_XMM0_REGNUM (tdep);
462 }
463 else if (reg >= 29 && reg <= 36)
464 {
465 /* MMX registers. */
466 return reg - 29 + I387_MM0_REGNUM (tdep);
467 }
468
469 /* This will hopefully provoke a warning. */
470 return gdbarch_num_cooked_regs (gdbarch);
471 }
472
473 /* Convert SVR4 DWARF register number REG to the appropriate register number
474 used by GDB. */
475
476 static int
477 i386_svr4_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
478 {
479 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
480
481 /* This implements the GCC register map that tries to be compatible
482 with the SVR4 C compiler for DWARF (svr4_dbx_register_map[]). */
483
484 /* The SVR4 register numbering includes %eip and %eflags, and
485 numbers the floating point registers differently. */
486 if (reg >= 0 && reg <= 9)
487 {
488 /* General-purpose registers. */
489 return reg;
490 }
491 else if (reg >= 11 && reg <= 18)
492 {
493 /* Floating-point registers. */
494 return reg - 11 + I387_ST0_REGNUM (tdep);
495 }
496 else if (reg >= 21 && reg <= 36)
497 {
498 /* The SSE and MMX registers have the same numbers as with dbx. */
499 return i386_dbx_reg_to_regnum (gdbarch, reg);
500 }
501
502 switch (reg)
503 {
504 case 37: return I387_FCTRL_REGNUM (tdep);
505 case 38: return I387_FSTAT_REGNUM (tdep);
506 case 39: return I387_MXCSR_REGNUM (tdep);
507 case 40: return I386_ES_REGNUM;
508 case 41: return I386_CS_REGNUM;
509 case 42: return I386_SS_REGNUM;
510 case 43: return I386_DS_REGNUM;
511 case 44: return I386_FS_REGNUM;
512 case 45: return I386_GS_REGNUM;
513 }
514
515 return -1;
516 }
517
518 /* Wrapper on i386_svr4_dwarf_reg_to_regnum to return
519 num_regs + num_pseudo_regs for other debug formats. */
520
521 int
522 i386_svr4_reg_to_regnum (struct gdbarch *gdbarch, int reg)
523 {
524 int regnum = i386_svr4_dwarf_reg_to_regnum (gdbarch, reg);
525
526 if (regnum == -1)
527 return gdbarch_num_cooked_regs (gdbarch);
528 return regnum;
529 }
530
531 \f
532
533 /* This is the variable that is set with "set disassembly-flavor", and
534 its legitimate values. */
535 static const char att_flavor[] = "att";
536 static const char intel_flavor[] = "intel";
537 static const char *const valid_flavors[] =
538 {
539 att_flavor,
540 intel_flavor,
541 NULL
542 };
543 static const char *disassembly_flavor = att_flavor;
544 \f
545
546 /* Use the program counter to determine the contents and size of a
547 breakpoint instruction. Return a pointer to a string of bytes that
548 encode a breakpoint instruction, store the length of the string in
549 *LEN and optionally adjust *PC to point to the correct memory
550 location for inserting the breakpoint.
551
552 On the i386 we have a single breakpoint that fits in a single byte
553 and can be inserted anywhere.
554
555 This function is 64-bit safe. */
556
557 constexpr gdb_byte i386_break_insn[] = { 0xcc }; /* int 3 */
558
559 typedef BP_MANIPULATION (i386_break_insn) i386_breakpoint;
560
561 \f
562 /* Displaced instruction handling. */
563
564 /* Skip the legacy instruction prefixes in INSN.
565 Not all prefixes are valid for any particular insn
566 but we needn't care, the insn will fault if it's invalid.
567 The result is a pointer to the first opcode byte,
568 or NULL if we run off the end of the buffer. */
569
570 static gdb_byte *
571 i386_skip_prefixes (gdb_byte *insn, size_t max_len)
572 {
573 gdb_byte *end = insn + max_len;
574
575 while (insn < end)
576 {
577 switch (*insn)
578 {
579 case DATA_PREFIX_OPCODE:
580 case ADDR_PREFIX_OPCODE:
581 case CS_PREFIX_OPCODE:
582 case DS_PREFIX_OPCODE:
583 case ES_PREFIX_OPCODE:
584 case FS_PREFIX_OPCODE:
585 case GS_PREFIX_OPCODE:
586 case SS_PREFIX_OPCODE:
587 case LOCK_PREFIX_OPCODE:
588 case REPE_PREFIX_OPCODE:
589 case REPNE_PREFIX_OPCODE:
590 ++insn;
591 continue;
592 default:
593 return insn;
594 }
595 }
596
597 return NULL;
598 }
599
600 static int
601 i386_absolute_jmp_p (const gdb_byte *insn)
602 {
603 /* jmp far (absolute address in operand). */
604 if (insn[0] == 0xea)
605 return 1;
606
607 if (insn[0] == 0xff)
608 {
609 /* jump near, absolute indirect (/4). */
610 if ((insn[1] & 0x38) == 0x20)
611 return 1;
612
613 /* jump far, absolute indirect (/5). */
614 if ((insn[1] & 0x38) == 0x28)
615 return 1;
616 }
617
618 return 0;
619 }
620
621 /* Return non-zero if INSN is a jump, zero otherwise. */
622
623 static int
624 i386_jmp_p (const gdb_byte *insn)
625 {
626 /* jump short, relative. */
627 if (insn[0] == 0xeb)
628 return 1;
629
630 /* jump near, relative. */
631 if (insn[0] == 0xe9)
632 return 1;
633
634 return i386_absolute_jmp_p (insn);
635 }
636
637 static int
638 i386_absolute_call_p (const gdb_byte *insn)
639 {
640 /* call far, absolute. */
641 if (insn[0] == 0x9a)
642 return 1;
643
644 if (insn[0] == 0xff)
645 {
646 /* Call near, absolute indirect (/2). */
647 if ((insn[1] & 0x38) == 0x10)
648 return 1;
649
650 /* Call far, absolute indirect (/3). */
651 if ((insn[1] & 0x38) == 0x18)
652 return 1;
653 }
654
655 return 0;
656 }
657
658 static int
659 i386_ret_p (const gdb_byte *insn)
660 {
661 switch (insn[0])
662 {
663 case 0xc2: /* ret near, pop N bytes. */
664 case 0xc3: /* ret near */
665 case 0xca: /* ret far, pop N bytes. */
666 case 0xcb: /* ret far */
667 case 0xcf: /* iret */
668 return 1;
669
670 default:
671 return 0;
672 }
673 }
674
675 static int
676 i386_call_p (const gdb_byte *insn)
677 {
678 if (i386_absolute_call_p (insn))
679 return 1;
680
681 /* call near, relative. */
682 if (insn[0] == 0xe8)
683 return 1;
684
685 return 0;
686 }
687
688 /* Return non-zero if INSN is a system call, and set *LENGTHP to its
689 length in bytes. Otherwise, return zero. */
690
691 static int
692 i386_syscall_p (const gdb_byte *insn, int *lengthp)
693 {
694 /* Is it 'int $0x80'? */
695 if ((insn[0] == 0xcd && insn[1] == 0x80)
696 /* Or is it 'sysenter'? */
697 || (insn[0] == 0x0f && insn[1] == 0x34)
698 /* Or is it 'syscall'? */
699 || (insn[0] == 0x0f && insn[1] == 0x05))
700 {
701 *lengthp = 2;
702 return 1;
703 }
704
705 return 0;
706 }
707
708 /* The gdbarch insn_is_call method. */
709
710 static int
711 i386_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
712 {
713 gdb_byte buf[I386_MAX_INSN_LEN], *insn;
714
715 read_code (addr, buf, I386_MAX_INSN_LEN);
716 insn = i386_skip_prefixes (buf, I386_MAX_INSN_LEN);
717
718 return i386_call_p (insn);
719 }
720
721 /* The gdbarch insn_is_ret method. */
722
723 static int
724 i386_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
725 {
726 gdb_byte buf[I386_MAX_INSN_LEN], *insn;
727
728 read_code (addr, buf, I386_MAX_INSN_LEN);
729 insn = i386_skip_prefixes (buf, I386_MAX_INSN_LEN);
730
731 return i386_ret_p (insn);
732 }
733
734 /* The gdbarch insn_is_jump method. */
735
736 static int
737 i386_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
738 {
739 gdb_byte buf[I386_MAX_INSN_LEN], *insn;
740
741 read_code (addr, buf, I386_MAX_INSN_LEN);
742 insn = i386_skip_prefixes (buf, I386_MAX_INSN_LEN);
743
744 return i386_jmp_p (insn);
745 }
746
747 /* Some kernels may run one past a syscall insn, so we have to cope. */
748
749 displaced_step_copy_insn_closure_up
750 i386_displaced_step_copy_insn (struct gdbarch *gdbarch,
751 CORE_ADDR from, CORE_ADDR to,
752 struct regcache *regs)
753 {
754 size_t len = gdbarch_max_insn_length (gdbarch);
755 std::unique_ptr<i386_displaced_step_copy_insn_closure> closure
756 (new i386_displaced_step_copy_insn_closure (len));
757 gdb_byte *buf = closure->buf.data ();
758
759 read_memory (from, buf, len);
760
761 /* GDB may get control back after the insn after the syscall.
762 Presumably this is a kernel bug.
763 If this is a syscall, make sure there's a nop afterwards. */
764 {
765 int syscall_length;
766 gdb_byte *insn;
767
768 insn = i386_skip_prefixes (buf, len);
769 if (insn != NULL && i386_syscall_p (insn, &syscall_length))
770 insn[syscall_length] = NOP_OPCODE;
771 }
772
773 write_memory (to, buf, len);
774
775 displaced_debug_printf ("%s->%s: %s",
776 paddress (gdbarch, from), paddress (gdbarch, to),
777 bytes_to_string (buf, len).c_str ());
778
779 /* This is a work around for a problem with g++ 4.8. */
780 return displaced_step_copy_insn_closure_up (closure.release ());
781 }
782
783 /* Fix up the state of registers and memory after having single-stepped
784 a displaced instruction. */
785
786 void
787 i386_displaced_step_fixup (struct gdbarch *gdbarch,
788 struct displaced_step_copy_insn_closure *closure_,
789 CORE_ADDR from, CORE_ADDR to,
790 struct regcache *regs, bool completed_p)
791 {
792 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
793
794 /* The offset we applied to the instruction's address.
795 This could well be negative (when viewed as a signed 32-bit
796 value), but ULONGEST won't reflect that, so take care when
797 applying it. */
798 ULONGEST insn_offset = to - from;
799
800 i386_displaced_step_copy_insn_closure *closure
801 = (i386_displaced_step_copy_insn_closure *) closure_;
802 gdb_byte *insn = closure->buf.data ();
803 /* The start of the insn, needed in case we see some prefixes. */
804 gdb_byte *insn_start = insn;
805
806 displaced_debug_printf ("fixup (%s, %s), insn = 0x%02x 0x%02x ...",
807 paddress (gdbarch, from), paddress (gdbarch, to),
808 insn[0], insn[1]);
809
810 /* The list of issues to contend with here is taken from
811 resume_execution in arch/i386/kernel/kprobes.c, Linux 2.6.20.
812 Yay for Free Software! */
813
814 /* Relocate the %eip, if necessary. */
815
816 /* The instruction recognizers we use assume any leading prefixes
817 have been skipped. */
818 {
819 /* This is the size of the buffer in closure. */
820 size_t max_insn_len = gdbarch_max_insn_length (gdbarch);
821 gdb_byte *opcode = i386_skip_prefixes (insn, max_insn_len);
822 /* If there are too many prefixes, just ignore the insn.
823 It will fault when run. */
824 if (opcode != NULL)
825 insn = opcode;
826 }
827
828 /* Except in the case of absolute or indirect jump or call
829 instructions, or a return instruction, the new eip is relative to
830 the displaced instruction; make it relative. Well, signal
831 handler returns don't need relocation either, but we use the
832 value of %eip to recognize those; see below. */
833 if (!completed_p
834 || (!i386_absolute_jmp_p (insn)
835 && !i386_absolute_call_p (insn)
836 && !i386_ret_p (insn)))
837 {
838 int insn_len;
839
840 CORE_ADDR pc = regcache_read_pc (regs);
841
842 /* A signal trampoline system call changes the %eip, resuming
843 execution of the main program after the signal handler has
844 returned. That makes them like 'return' instructions; we
845 shouldn't relocate %eip.
846
847 But most system calls don't, and we do need to relocate %eip.
848
849 Our heuristic for distinguishing these cases: if stepping
850 over the system call instruction left control directly after
851 the instruction, the we relocate --- control almost certainly
852 doesn't belong in the displaced copy. Otherwise, we assume
853 the instruction has put control where it belongs, and leave
854 it unrelocated. Goodness help us if there are PC-relative
855 system calls. */
856 if (i386_syscall_p (insn, &insn_len)
857 && pc != to + (insn - insn_start) + insn_len
858 /* GDB can get control back after the insn after the syscall.
859 Presumably this is a kernel bug.
860 i386_displaced_step_copy_insn ensures it's a nop,
861 we add one to the length for it. */
862 && pc != to + (insn - insn_start) + insn_len + 1)
863 displaced_debug_printf ("syscall changed %%eip; not relocating");
864 else
865 {
866 ULONGEST eip = (pc - insn_offset) & 0xffffffffUL;
867
868 /* If we just stepped over a breakpoint insn, we don't backup
869 the pc on purpose; this is to match behavior without
870 stepping. */
871
872 regcache_write_pc (regs, eip);
873
874 displaced_debug_printf ("relocated %%eip from %s to %s",
875 paddress (gdbarch, pc),
876 paddress (gdbarch, eip));
877 }
878 }
879
880 /* If the instruction was PUSHFL, then the TF bit will be set in the
881 pushed value, and should be cleared. We'll leave this for later,
882 since GDB already messes up the TF flag when stepping over a
883 pushfl. */
884
885 /* If the instruction was a call, the return address now atop the
886 stack is the address following the copied instruction. We need
887 to make it the address following the original instruction. */
888 if (completed_p && i386_call_p (insn))
889 {
890 ULONGEST esp;
891 ULONGEST retaddr;
892 const ULONGEST retaddr_len = 4;
893
894 regcache_cooked_read_unsigned (regs, I386_ESP_REGNUM, &esp);
895 retaddr = read_memory_unsigned_integer (esp, retaddr_len, byte_order);
896 retaddr = (retaddr - insn_offset) & 0xffffffffUL;
897 write_memory_unsigned_integer (esp, retaddr_len, byte_order, retaddr);
898
899 displaced_debug_printf ("relocated return addr at %s to %s",
900 paddress (gdbarch, esp),
901 paddress (gdbarch, retaddr));
902 }
903 }
904
905 static void
906 append_insns (CORE_ADDR *to, ULONGEST len, const gdb_byte *buf)
907 {
908 target_write_memory (*to, buf, len);
909 *to += len;
910 }
911
912 static void
913 i386_relocate_instruction (struct gdbarch *gdbarch,
914 CORE_ADDR *to, CORE_ADDR oldloc)
915 {
916 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
917 gdb_byte buf[I386_MAX_INSN_LEN];
918 int offset = 0, rel32, newrel;
919 int insn_length;
920 gdb_byte *insn = buf;
921
922 read_memory (oldloc, buf, I386_MAX_INSN_LEN);
923
924 insn_length = gdb_buffered_insn_length (gdbarch, insn,
925 I386_MAX_INSN_LEN, oldloc);
926
927 /* Get past the prefixes. */
928 insn = i386_skip_prefixes (insn, I386_MAX_INSN_LEN);
929
930 /* Adjust calls with 32-bit relative addresses as push/jump, with
931 the address pushed being the location where the original call in
932 the user program would return to. */
933 if (insn[0] == 0xe8)
934 {
935 gdb_byte push_buf[16];
936 unsigned int ret_addr;
937
938 /* Where "ret" in the original code will return to. */
939 ret_addr = oldloc + insn_length;
940 push_buf[0] = 0x68; /* pushq $... */
941 store_unsigned_integer (&push_buf[1], 4, byte_order, ret_addr);
942 /* Push the push. */
943 append_insns (to, 5, push_buf);
944
945 /* Convert the relative call to a relative jump. */
946 insn[0] = 0xe9;
947
948 /* Adjust the destination offset. */
949 rel32 = extract_signed_integer (insn + 1, 4, byte_order);
950 newrel = (oldloc - *to) + rel32;
951 store_signed_integer (insn + 1, 4, byte_order, newrel);
952
953 displaced_debug_printf ("adjusted insn rel32=%s at %s to rel32=%s at %s",
954 hex_string (rel32), paddress (gdbarch, oldloc),
955 hex_string (newrel), paddress (gdbarch, *to));
956
957 /* Write the adjusted jump into its displaced location. */
958 append_insns (to, 5, insn);
959 return;
960 }
961
962 /* Adjust jumps with 32-bit relative addresses. Calls are already
963 handled above. */
964 if (insn[0] == 0xe9)
965 offset = 1;
966 /* Adjust conditional jumps. */
967 else if (insn[0] == 0x0f && (insn[1] & 0xf0) == 0x80)
968 offset = 2;
969
970 if (offset)
971 {
972 rel32 = extract_signed_integer (insn + offset, 4, byte_order);
973 newrel = (oldloc - *to) + rel32;
974 store_signed_integer (insn + offset, 4, byte_order, newrel);
975 displaced_debug_printf ("adjusted insn rel32=%s at %s to rel32=%s at %s",
976 hex_string (rel32), paddress (gdbarch, oldloc),
977 hex_string (newrel), paddress (gdbarch, *to));
978 }
979
980 /* Write the adjusted instructions into their displaced
981 location. */
982 append_insns (to, insn_length, buf);
983 }
984
985 \f
986 #ifdef I386_REGNO_TO_SYMMETRY
987 #error "The Sequent Symmetry is no longer supported."
988 #endif
989
990 /* According to the System V ABI, the registers %ebp, %ebx, %edi, %esi
991 and %esp "belong" to the calling function. Therefore these
992 registers should be saved if they're going to be modified. */
993
994 /* The maximum number of saved registers. This should include all
995 registers mentioned above, and %eip. */
996 #define I386_NUM_SAVED_REGS I386_NUM_GREGS
997
998 struct i386_frame_cache
999 {
1000 /* Base address. */
1001 CORE_ADDR base;
1002 int base_p;
1003 LONGEST sp_offset;
1004 CORE_ADDR pc;
1005
1006 /* Saved registers. */
1007 CORE_ADDR saved_regs[I386_NUM_SAVED_REGS];
1008 CORE_ADDR saved_sp;
1009 int saved_sp_reg;
1010 int pc_in_eax;
1011
1012 /* Stack space reserved for local variables. */
1013 long locals;
1014 };
1015
1016 /* Allocate and initialize a frame cache. */
1017
1018 static struct i386_frame_cache *
1019 i386_alloc_frame_cache (void)
1020 {
1021 struct i386_frame_cache *cache;
1022 int i;
1023
1024 cache = FRAME_OBSTACK_ZALLOC (struct i386_frame_cache);
1025
1026 /* Base address. */
1027 cache->base_p = 0;
1028 cache->base = 0;
1029 cache->sp_offset = -4;
1030 cache->pc = 0;
1031
1032 /* Saved registers. We initialize these to -1 since zero is a valid
1033 offset (that's where %ebp is supposed to be stored). */
1034 for (i = 0; i < I386_NUM_SAVED_REGS; i++)
1035 cache->saved_regs[i] = -1;
1036 cache->saved_sp = 0;
1037 cache->saved_sp_reg = -1;
1038 cache->pc_in_eax = 0;
1039
1040 /* Frameless until proven otherwise. */
1041 cache->locals = -1;
1042
1043 return cache;
1044 }
1045
1046 /* If the instruction at PC is a jump, return the address of its
1047 target. Otherwise, return PC. */
1048
1049 static CORE_ADDR
1050 i386_follow_jump (struct gdbarch *gdbarch, CORE_ADDR pc)
1051 {
1052 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1053 gdb_byte op;
1054 long delta = 0;
1055 int data16 = 0;
1056
1057 if (target_read_code (pc, &op, 1))
1058 return pc;
1059
1060 if (op == 0x66)
1061 {
1062 data16 = 1;
1063
1064 op = read_code_unsigned_integer (pc + 1, 1, byte_order);
1065 }
1066
1067 switch (op)
1068 {
1069 case 0xe9:
1070 /* Relative jump: if data16 == 0, disp32, else disp16. */
1071 if (data16)
1072 {
1073 delta = read_memory_integer (pc + 2, 2, byte_order);
1074
1075 /* Include the size of the jmp instruction (including the
1076 0x66 prefix). */
1077 delta += 4;
1078 }
1079 else
1080 {
1081 delta = read_memory_integer (pc + 1, 4, byte_order);
1082
1083 /* Include the size of the jmp instruction. */
1084 delta += 5;
1085 }
1086 break;
1087 case 0xeb:
1088 /* Relative jump, disp8 (ignore data16). */
1089 delta = read_memory_integer (pc + data16 + 1, 1, byte_order);
1090
1091 delta += data16 + 2;
1092 break;
1093 }
1094
1095 return pc + delta;
1096 }
1097
1098 /* Check whether PC points at a prologue for a function returning a
1099 structure or union. If so, it updates CACHE and returns the
1100 address of the first instruction after the code sequence that
1101 removes the "hidden" argument from the stack or CURRENT_PC,
1102 whichever is smaller. Otherwise, return PC. */
1103
1104 static CORE_ADDR
1105 i386_analyze_struct_return (CORE_ADDR pc, CORE_ADDR current_pc,
1106 struct i386_frame_cache *cache)
1107 {
1108 /* Functions that return a structure or union start with:
1109
1110 popl %eax 0x58
1111 xchgl %eax, (%esp) 0x87 0x04 0x24
1112 or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
1113
1114 (the System V compiler puts out the second `xchg' instruction,
1115 and the assembler doesn't try to optimize it, so the 'sib' form
1116 gets generated). This sequence is used to get the address of the
1117 return buffer for a function that returns a structure. */
1118 static gdb_byte proto1[3] = { 0x87, 0x04, 0x24 };
1119 static gdb_byte proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
1120 gdb_byte buf[4];
1121 gdb_byte op;
1122
1123 if (current_pc <= pc)
1124 return pc;
1125
1126 if (target_read_code (pc, &op, 1))
1127 return pc;
1128
1129 if (op != 0x58) /* popl %eax */
1130 return pc;
1131
1132 if (target_read_code (pc + 1, buf, 4))
1133 return pc;
1134
1135 if (memcmp (buf, proto1, 3) != 0 && memcmp (buf, proto2, 4) != 0)
1136 return pc;
1137
1138 if (current_pc == pc)
1139 {
1140 cache->sp_offset += 4;
1141 return current_pc;
1142 }
1143
1144 if (current_pc == pc + 1)
1145 {
1146 cache->pc_in_eax = 1;
1147 return current_pc;
1148 }
1149
1150 if (buf[1] == proto1[1])
1151 return pc + 4;
1152 else
1153 return pc + 5;
1154 }
1155
1156 static CORE_ADDR
1157 i386_skip_probe (CORE_ADDR pc)
1158 {
1159 /* A function may start with
1160
1161 pushl constant
1162 call _probe
1163 addl $4, %esp
1164
1165 followed by
1166
1167 pushl %ebp
1168
1169 etc. */
1170 gdb_byte buf[8];
1171 gdb_byte op;
1172
1173 if (target_read_code (pc, &op, 1))
1174 return pc;
1175
1176 if (op == 0x68 || op == 0x6a)
1177 {
1178 int delta;
1179
1180 /* Skip past the `pushl' instruction; it has either a one-byte or a
1181 four-byte operand, depending on the opcode. */
1182 if (op == 0x68)
1183 delta = 5;
1184 else
1185 delta = 2;
1186
1187 /* Read the following 8 bytes, which should be `call _probe' (6
1188 bytes) followed by `addl $4,%esp' (2 bytes). */
1189 read_memory (pc + delta, buf, sizeof (buf));
1190 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
1191 pc += delta + sizeof (buf);
1192 }
1193
1194 return pc;
1195 }
1196
1197 /* GCC 4.1 and later, can put code in the prologue to realign the
1198 stack pointer. Check whether PC points to such code, and update
1199 CACHE accordingly. Return the first instruction after the code
1200 sequence or CURRENT_PC, whichever is smaller. If we don't
1201 recognize the code, return PC. */
1202
1203 static CORE_ADDR
1204 i386_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
1205 struct i386_frame_cache *cache)
1206 {
1207 /* There are 2 code sequences to re-align stack before the frame
1208 gets set up:
1209
1210 1. Use a caller-saved saved register:
1211
1212 leal 4(%esp), %reg
1213 andl $-XXX, %esp
1214 pushl -4(%reg)
1215
1216 2. Use a callee-saved saved register:
1217
1218 pushl %reg
1219 leal 8(%esp), %reg
1220 andl $-XXX, %esp
1221 pushl -4(%reg)
1222
1223 "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:
1224
1225 0x83 0xe4 0xf0 andl $-16, %esp
1226 0x81 0xe4 0x00 0xff 0xff 0xff andl $-256, %esp
1227 */
1228
1229 gdb_byte buf[14];
1230 int reg;
1231 int offset, offset_and;
1232 static int regnums[8] = {
1233 I386_EAX_REGNUM, /* %eax */
1234 I386_ECX_REGNUM, /* %ecx */
1235 I386_EDX_REGNUM, /* %edx */
1236 I386_EBX_REGNUM, /* %ebx */
1237 I386_ESP_REGNUM, /* %esp */
1238 I386_EBP_REGNUM, /* %ebp */
1239 I386_ESI_REGNUM, /* %esi */
1240 I386_EDI_REGNUM /* %edi */
1241 };
1242
1243 if (target_read_code (pc, buf, sizeof buf))
1244 return pc;
1245
1246 /* Check caller-saved saved register. The first instruction has
1247 to be "leal 4(%esp), %reg". */
1248 if (buf[0] == 0x8d && buf[2] == 0x24 && buf[3] == 0x4)
1249 {
1250 /* MOD must be binary 10 and R/M must be binary 100. */
1251 if ((buf[1] & 0xc7) != 0x44)
1252 return pc;
1253
1254 /* REG has register number. */
1255 reg = (buf[1] >> 3) & 7;
1256 offset = 4;
1257 }
1258 else
1259 {
1260 /* Check callee-saved saved register. The first instruction
1261 has to be "pushl %reg". */
1262 if ((buf[0] & 0xf8) != 0x50)
1263 return pc;
1264
1265 /* Get register. */
1266 reg = buf[0] & 0x7;
1267
1268 /* The next instruction has to be "leal 8(%esp), %reg". */
1269 if (buf[1] != 0x8d || buf[3] != 0x24 || buf[4] != 0x8)
1270 return pc;
1271
1272 /* MOD must be binary 10 and R/M must be binary 100. */
1273 if ((buf[2] & 0xc7) != 0x44)
1274 return pc;
1275
1276 /* REG has register number. Registers in pushl and leal have to
1277 be the same. */
1278 if (reg != ((buf[2] >> 3) & 7))
1279 return pc;
1280
1281 offset = 5;
1282 }
1283
1284 /* Rigister can't be %esp nor %ebp. */
1285 if (reg == 4 || reg == 5)
1286 return pc;
1287
1288 /* The next instruction has to be "andl $-XXX, %esp". */
1289 if (buf[offset + 1] != 0xe4
1290 || (buf[offset] != 0x81 && buf[offset] != 0x83))
1291 return pc;
1292
1293 offset_and = offset;
1294 offset += buf[offset] == 0x81 ? 6 : 3;
1295
1296 /* The next instruction has to be "pushl -4(%reg)". 8bit -4 is
1297 0xfc. REG must be binary 110 and MOD must be binary 01. */
1298 if (buf[offset] != 0xff
1299 || buf[offset + 2] != 0xfc
1300 || (buf[offset + 1] & 0xf8) != 0x70)
1301 return pc;
1302
1303 /* R/M has register. Registers in leal and pushl have to be the
1304 same. */
1305 if (reg != (buf[offset + 1] & 7))
1306 return pc;
1307
1308 if (current_pc > pc + offset_and)
1309 cache->saved_sp_reg = regnums[reg];
1310
1311 return std::min (pc + offset + 3, current_pc);
1312 }
1313
1314 /* Maximum instruction length we need to handle. */
1315 #define I386_MAX_MATCHED_INSN_LEN 6
1316
1317 /* Instruction description. */
1318 struct i386_insn
1319 {
1320 size_t len;
1321 gdb_byte insn[I386_MAX_MATCHED_INSN_LEN];
1322 gdb_byte mask[I386_MAX_MATCHED_INSN_LEN];
1323 };
1324
1325 /* Return whether instruction at PC matches PATTERN. */
1326
1327 static int
1328 i386_match_pattern (CORE_ADDR pc, struct i386_insn pattern)
1329 {
1330 gdb_byte op;
1331
1332 if (target_read_code (pc, &op, 1))
1333 return 0;
1334
1335 if ((op & pattern.mask[0]) == pattern.insn[0])
1336 {
1337 gdb_byte buf[I386_MAX_MATCHED_INSN_LEN - 1];
1338 int insn_matched = 1;
1339 size_t i;
1340
1341 gdb_assert (pattern.len > 1);
1342 gdb_assert (pattern.len <= I386_MAX_MATCHED_INSN_LEN);
1343
1344 if (target_read_code (pc + 1, buf, pattern.len - 1))
1345 return 0;
1346
1347 for (i = 1; i < pattern.len; i++)
1348 {
1349 if ((buf[i - 1] & pattern.mask[i]) != pattern.insn[i])
1350 insn_matched = 0;
1351 }
1352 return insn_matched;
1353 }
1354 return 0;
1355 }
1356
1357 /* Search for the instruction at PC in the list INSN_PATTERNS. Return
1358 the first instruction description that matches. Otherwise, return
1359 NULL. */
1360
1361 static struct i386_insn *
1362 i386_match_insn (CORE_ADDR pc, struct i386_insn *insn_patterns)
1363 {
1364 struct i386_insn *pattern;
1365
1366 for (pattern = insn_patterns; pattern->len > 0; pattern++)
1367 {
1368 if (i386_match_pattern (pc, *pattern))
1369 return pattern;
1370 }
1371
1372 return NULL;
1373 }
1374
1375 /* Return whether PC points inside a sequence of instructions that
1376 matches INSN_PATTERNS. */
1377
1378 static int
1379 i386_match_insn_block (CORE_ADDR pc, struct i386_insn *insn_patterns)
1380 {
1381 CORE_ADDR current_pc;
1382 int ix, i;
1383 struct i386_insn *insn;
1384
1385 insn = i386_match_insn (pc, insn_patterns);
1386 if (insn == NULL)
1387 return 0;
1388
1389 current_pc = pc;
1390 ix = insn - insn_patterns;
1391 for (i = ix - 1; i >= 0; i--)
1392 {
1393 current_pc -= insn_patterns[i].len;
1394
1395 if (!i386_match_pattern (current_pc, insn_patterns[i]))
1396 return 0;
1397 }
1398
1399 current_pc = pc + insn->len;
1400 for (insn = insn_patterns + ix + 1; insn->len > 0; insn++)
1401 {
1402 if (!i386_match_pattern (current_pc, *insn))
1403 return 0;
1404
1405 current_pc += insn->len;
1406 }
1407
1408 return 1;
1409 }
1410
1411 /* Some special instructions that might be migrated by GCC into the
1412 part of the prologue that sets up the new stack frame. Because the
1413 stack frame hasn't been setup yet, no registers have been saved
1414 yet, and only the scratch registers %eax, %ecx and %edx can be
1415 touched. */
1416
1417 static i386_insn i386_frame_setup_skip_insns[] =
1418 {
1419 /* Check for `movb imm8, r' and `movl imm32, r'.
1420
1421 ??? Should we handle 16-bit operand-sizes here? */
1422
1423 /* `movb imm8, %al' and `movb imm8, %ah' */
1424 /* `movb imm8, %cl' and `movb imm8, %ch' */
1425 { 2, { 0xb0, 0x00 }, { 0xfa, 0x00 } },
1426 /* `movb imm8, %dl' and `movb imm8, %dh' */
1427 { 2, { 0xb2, 0x00 }, { 0xfb, 0x00 } },
1428 /* `movl imm32, %eax' and `movl imm32, %ecx' */
1429 { 5, { 0xb8 }, { 0xfe } },
1430 /* `movl imm32, %edx' */
1431 { 5, { 0xba }, { 0xff } },
1432
1433 /* Check for `mov imm32, r32'. Note that there is an alternative
1434 encoding for `mov m32, %eax'.
1435
1436 ??? Should we handle SIB addressing here?
1437 ??? Should we handle 16-bit operand-sizes here? */
1438
1439 /* `movl m32, %eax' */
1440 { 5, { 0xa1 }, { 0xff } },
1441 /* `movl m32, %eax' and `mov; m32, %ecx' */
1442 { 6, { 0x89, 0x05 }, {0xff, 0xf7 } },
1443 /* `movl m32, %edx' */
1444 { 6, { 0x89, 0x15 }, {0xff, 0xff } },
1445
1446 /* Check for `xorl r32, r32' and the equivalent `subl r32, r32'.
1447 Because of the symmetry, there are actually two ways to encode
1448 these instructions; opcode bytes 0x29 and 0x2b for `subl' and
1449 opcode bytes 0x31 and 0x33 for `xorl'. */
1450
1451 /* `subl %eax, %eax' */
1452 { 2, { 0x29, 0xc0 }, { 0xfd, 0xff } },
1453 /* `subl %ecx, %ecx' */
1454 { 2, { 0x29, 0xc9 }, { 0xfd, 0xff } },
1455 /* `subl %edx, %edx' */
1456 { 2, { 0x29, 0xd2 }, { 0xfd, 0xff } },
1457 /* `xorl %eax, %eax' */
1458 { 2, { 0x31, 0xc0 }, { 0xfd, 0xff } },
1459 /* `xorl %ecx, %ecx' */
1460 { 2, { 0x31, 0xc9 }, { 0xfd, 0xff } },
1461 /* `xorl %edx, %edx' */
1462 { 2, { 0x31, 0xd2 }, { 0xfd, 0xff } },
1463 { 0 }
1464 };
1465
1466 /* Check whether PC points to an endbr32 instruction. */
1467 static CORE_ADDR
1468 i386_skip_endbr (CORE_ADDR pc)
1469 {
1470 static const gdb_byte endbr32[] = { 0xf3, 0x0f, 0x1e, 0xfb };
1471
1472 gdb_byte buf[sizeof (endbr32)];
1473
1474 /* Stop there if we can't read the code */
1475 if (target_read_code (pc, buf, sizeof (endbr32)))
1476 return pc;
1477
1478 /* If the instruction isn't an endbr32, stop */
1479 if (memcmp (buf, endbr32, sizeof (endbr32)) != 0)
1480 return pc;
1481
1482 return pc + sizeof (endbr32);
1483 }
1484
1485 /* Check whether PC points to a no-op instruction. */
1486 static CORE_ADDR
1487 i386_skip_noop (CORE_ADDR pc)
1488 {
1489 gdb_byte op;
1490 int check = 1;
1491
1492 if (target_read_code (pc, &op, 1))
1493 return pc;
1494
1495 while (check)
1496 {
1497 check = 0;
1498 /* Ignore `nop' instruction. */
1499 if (op == 0x90)
1500 {
1501 pc += 1;
1502 if (target_read_code (pc, &op, 1))
1503 return pc;
1504 check = 1;
1505 }
1506 /* Ignore no-op instruction `mov %edi, %edi'.
1507 Microsoft system dlls often start with
1508 a `mov %edi,%edi' instruction.
1509 The 5 bytes before the function start are
1510 filled with `nop' instructions.
1511 This pattern can be used for hot-patching:
1512 The `mov %edi, %edi' instruction can be replaced by a
1513 near jump to the location of the 5 `nop' instructions
1514 which can be replaced by a 32-bit jump to anywhere
1515 in the 32-bit address space. */
1516
1517 else if (op == 0x8b)
1518 {
1519 if (target_read_code (pc + 1, &op, 1))
1520 return pc;
1521
1522 if (op == 0xff)
1523 {
1524 pc += 2;
1525 if (target_read_code (pc, &op, 1))
1526 return pc;
1527
1528 check = 1;
1529 }
1530 }
1531 }
1532 return pc;
1533 }
1534
1535 /* Check whether PC points at a code that sets up a new stack frame.
1536 If so, it updates CACHE and returns the address of the first
1537 instruction after the sequence that sets up the frame or LIMIT,
1538 whichever is smaller. If we don't recognize the code, return PC. */
1539
1540 static CORE_ADDR
1541 i386_analyze_frame_setup (struct gdbarch *gdbarch,
1542 CORE_ADDR pc, CORE_ADDR limit,
1543 struct i386_frame_cache *cache)
1544 {
1545 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1546 struct i386_insn *insn;
1547 gdb_byte op;
1548 int skip = 0;
1549
1550 if (limit <= pc)
1551 return limit;
1552
1553 if (target_read_code (pc, &op, 1))
1554 return pc;
1555
1556 if (op == 0x55) /* pushl %ebp */
1557 {
1558 /* Take into account that we've executed the `pushl %ebp' that
1559 starts this instruction sequence. */
1560 cache->saved_regs[I386_EBP_REGNUM] = 0;
1561 cache->sp_offset += 4;
1562 pc++;
1563
1564 /* If that's all, return now. */
1565 if (limit <= pc)
1566 return limit;
1567
1568 /* Check for some special instructions that might be migrated by
1569 GCC into the prologue and skip them. At this point in the
1570 prologue, code should only touch the scratch registers %eax,
1571 %ecx and %edx, so while the number of possibilities is sheer,
1572 it is limited.
1573
1574 Make sure we only skip these instructions if we later see the
1575 `movl %esp, %ebp' that actually sets up the frame. */
1576 while (pc + skip < limit)
1577 {
1578 insn = i386_match_insn (pc + skip, i386_frame_setup_skip_insns);
1579 if (insn == NULL)
1580 break;
1581
1582 skip += insn->len;
1583 }
1584
1585 /* If that's all, return now. */
1586 if (limit <= pc + skip)
1587 return limit;
1588
1589 if (target_read_code (pc + skip, &op, 1))
1590 return pc + skip;
1591
1592 /* The i386 prologue looks like
1593
1594 push %ebp
1595 mov %esp,%ebp
1596 sub $0x10,%esp
1597
1598 and a different prologue can be generated for atom.
1599
1600 push %ebp
1601 lea (%esp),%ebp
1602 lea -0x10(%esp),%esp
1603
1604 We handle both of them here. */
1605
1606 switch (op)
1607 {
1608 /* Check for `movl %esp, %ebp' -- can be written in two ways. */
1609 case 0x8b:
1610 if (read_code_unsigned_integer (pc + skip + 1, 1, byte_order)
1611 != 0xec)
1612 return pc;
1613 pc += (skip + 2);
1614 break;
1615 case 0x89:
1616 if (read_code_unsigned_integer (pc + skip + 1, 1, byte_order)
1617 != 0xe5)
1618 return pc;
1619 pc += (skip + 2);
1620 break;
1621 case 0x8d: /* Check for 'lea (%ebp), %ebp'. */
1622 if (read_code_unsigned_integer (pc + skip + 1, 2, byte_order)
1623 != 0x242c)
1624 return pc;
1625 pc += (skip + 3);
1626 break;
1627 default:
1628 return pc;
1629 }
1630
1631 /* OK, we actually have a frame. We just don't know how large
1632 it is yet. Set its size to zero. We'll adjust it if
1633 necessary. We also now commit to skipping the special
1634 instructions mentioned before. */
1635 cache->locals = 0;
1636
1637 /* If that's all, return now. */
1638 if (limit <= pc)
1639 return limit;
1640
1641 /* Check for stack adjustment
1642
1643 subl $XXX, %esp
1644 or
1645 lea -XXX(%esp),%esp
1646
1647 NOTE: You can't subtract a 16-bit immediate from a 32-bit
1648 reg, so we don't have to worry about a data16 prefix. */
1649 if (target_read_code (pc, &op, 1))
1650 return pc;
1651 if (op == 0x83)
1652 {
1653 /* `subl' with 8-bit immediate. */
1654 if (read_code_unsigned_integer (pc + 1, 1, byte_order) != 0xec)
1655 /* Some instruction starting with 0x83 other than `subl'. */
1656 return pc;
1657
1658 /* `subl' with signed 8-bit immediate (though it wouldn't
1659 make sense to be negative). */
1660 cache->locals = read_code_integer (pc + 2, 1, byte_order);
1661 return pc + 3;
1662 }
1663 else if (op == 0x81)
1664 {
1665 /* Maybe it is `subl' with a 32-bit immediate. */
1666 if (read_code_unsigned_integer (pc + 1, 1, byte_order) != 0xec)
1667 /* Some instruction starting with 0x81 other than `subl'. */
1668 return pc;
1669
1670 /* It is `subl' with a 32-bit immediate. */
1671 cache->locals = read_code_integer (pc + 2, 4, byte_order);
1672 return pc + 6;
1673 }
1674 else if (op == 0x8d)
1675 {
1676 /* The ModR/M byte is 0x64. */
1677 if (read_code_unsigned_integer (pc + 1, 1, byte_order) != 0x64)
1678 return pc;
1679 /* 'lea' with 8-bit displacement. */
1680 cache->locals = -1 * read_code_integer (pc + 3, 1, byte_order);
1681 return pc + 4;
1682 }
1683 else
1684 {
1685 /* Some instruction other than `subl' nor 'lea'. */
1686 return pc;
1687 }
1688 }
1689 else if (op == 0xc8) /* enter */
1690 {
1691 cache->locals = read_code_unsigned_integer (pc + 1, 2, byte_order);
1692 return pc + 4;
1693 }
1694
1695 return pc;
1696 }
1697
1698 /* Check whether PC points at code that saves registers on the stack.
1699 If so, it updates CACHE and returns the address of the first
1700 instruction after the register saves or CURRENT_PC, whichever is
1701 smaller. Otherwise, return PC. */
1702
1703 static CORE_ADDR
1704 i386_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
1705 struct i386_frame_cache *cache)
1706 {
1707 CORE_ADDR offset = 0;
1708 gdb_byte op;
1709 int i;
1710
1711 if (cache->locals > 0)
1712 offset -= cache->locals;
1713 for (i = 0; i < 8 && pc < current_pc; i++)
1714 {
1715 if (target_read_code (pc, &op, 1))
1716 return pc;
1717 if (op < 0x50 || op > 0x57)
1718 break;
1719
1720 offset -= 4;
1721 cache->saved_regs[op - 0x50] = offset;
1722 cache->sp_offset += 4;
1723 pc++;
1724 }
1725
1726 return pc;
1727 }
1728
1729 /* Do a full analysis of the prologue at PC and update CACHE
1730 accordingly. Bail out early if CURRENT_PC is reached. Return the
1731 address where the analysis stopped.
1732
1733 We handle these cases:
1734
1735 The startup sequence can be at the start of the function, or the
1736 function can start with a branch to startup code at the end.
1737
1738 %ebp can be set up with either the 'enter' instruction, or "pushl
1739 %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
1740 once used in the System V compiler).
1741
1742 Local space is allocated just below the saved %ebp by either the
1743 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a
1744 16-bit unsigned argument for space to allocate, and the 'addl'
1745 instruction could have either a signed byte, or 32-bit immediate.
1746
1747 Next, the registers used by this function are pushed. With the
1748 System V compiler they will always be in the order: %edi, %esi,
1749 %ebx (and sometimes a harmless bug causes it to also save but not
1750 restore %eax); however, the code below is willing to see the pushes
1751 in any order, and will handle up to 8 of them.
1752
1753 If the setup sequence is at the end of the function, then the next
1754 instruction will be a branch back to the start. */
1755
1756 static CORE_ADDR
1757 i386_analyze_prologue (struct gdbarch *gdbarch,
1758 CORE_ADDR pc, CORE_ADDR current_pc,
1759 struct i386_frame_cache *cache)
1760 {
1761 pc = i386_skip_endbr (pc);
1762 pc = i386_skip_noop (pc);
1763 pc = i386_follow_jump (gdbarch, pc);
1764 pc = i386_analyze_struct_return (pc, current_pc, cache);
1765 pc = i386_skip_probe (pc);
1766 pc = i386_analyze_stack_align (pc, current_pc, cache);
1767 pc = i386_analyze_frame_setup (gdbarch, pc, current_pc, cache);
1768 return i386_analyze_register_saves (pc, current_pc, cache);
1769 }
1770
1771 /* Return PC of first real instruction. */
1772
1773 static CORE_ADDR
1774 i386_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
1775 {
1776 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1777
1778 static gdb_byte pic_pat[6] =
1779 {
1780 0xe8, 0, 0, 0, 0, /* call 0x0 */
1781 0x5b, /* popl %ebx */
1782 };
1783 struct i386_frame_cache cache;
1784 CORE_ADDR pc;
1785 gdb_byte op;
1786 int i;
1787 CORE_ADDR func_addr;
1788
1789 if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
1790 {
1791 CORE_ADDR post_prologue_pc
1792 = skip_prologue_using_sal (gdbarch, func_addr);
1793 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
1794
1795 /* LLVM backend (Clang/Flang) always emits a line note before the
1796 prologue and another one after. We trust clang and newer Intel
1797 compilers to emit usable line notes. */
1798 if (post_prologue_pc
1799 && (cust != NULL
1800 && cust->producer () != NULL
1801 && (producer_is_llvm (cust->producer ())
1802 || producer_is_icc_ge_19 (cust->producer ()))))
1803 return std::max (start_pc, post_prologue_pc);
1804 }
1805
1806 cache.locals = -1;
1807 pc = i386_analyze_prologue (gdbarch, start_pc, 0xffffffff, &cache);
1808 if (cache.locals < 0)
1809 return start_pc;
1810
1811 /* Found valid frame setup. */
1812
1813 /* The native cc on SVR4 in -K PIC mode inserts the following code
1814 to get the address of the global offset table (GOT) into register
1815 %ebx:
1816
1817 call 0x0
1818 popl %ebx
1819 movl %ebx,x(%ebp) (optional)
1820 addl y,%ebx
1821
1822 This code is with the rest of the prologue (at the end of the
1823 function), so we have to skip it to get to the first real
1824 instruction at the start of the function. */
1825
1826 for (i = 0; i < 6; i++)
1827 {
1828 if (target_read_code (pc + i, &op, 1))
1829 return pc;
1830
1831 if (pic_pat[i] != op)
1832 break;
1833 }
1834 if (i == 6)
1835 {
1836 int delta = 6;
1837
1838 if (target_read_code (pc + delta, &op, 1))
1839 return pc;
1840
1841 if (op == 0x89) /* movl %ebx, x(%ebp) */
1842 {
1843 op = read_code_unsigned_integer (pc + delta + 1, 1, byte_order);
1844
1845 if (op == 0x5d) /* One byte offset from %ebp. */
1846 delta += 3;
1847 else if (op == 0x9d) /* Four byte offset from %ebp. */
1848 delta += 6;
1849 else /* Unexpected instruction. */
1850 delta = 0;
1851
1852 if (target_read_code (pc + delta, &op, 1))
1853 return pc;
1854 }
1855
1856 /* addl y,%ebx */
1857 if (delta > 0 && op == 0x81
1858 && read_code_unsigned_integer (pc + delta + 1, 1, byte_order)
1859 == 0xc3)
1860 {
1861 pc += delta + 6;
1862 }
1863 }
1864
1865 /* If the function starts with a branch (to startup code at the end)
1866 the last instruction should bring us back to the first
1867 instruction of the real code. */
1868 if (i386_follow_jump (gdbarch, start_pc) != start_pc)
1869 pc = i386_follow_jump (gdbarch, pc);
1870
1871 return pc;
1872 }
1873
1874 /* Check that the code pointed to by PC corresponds to a call to
1875 __main, skip it if so. Return PC otherwise. */
1876
1877 CORE_ADDR
1878 i386_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1879 {
1880 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1881 gdb_byte op;
1882
1883 if (target_read_code (pc, &op, 1))
1884 return pc;
1885 if (op == 0xe8)
1886 {
1887 gdb_byte buf[4];
1888
1889 if (target_read_code (pc + 1, buf, sizeof buf) == 0)
1890 {
1891 /* Make sure address is computed correctly as a 32bit
1892 integer even if CORE_ADDR is 64 bit wide. */
1893 CORE_ADDR call_dest;
1894
1895 call_dest = pc + 5 + extract_signed_integer (buf, 4, byte_order);
1896 call_dest = call_dest & 0xffffffffU;
1897 bound_minimal_symbol s = lookup_minimal_symbol_by_pc (call_dest);
1898 if (s.minsym != NULL
1899 && s.minsym->linkage_name () != NULL
1900 && strcmp (s.minsym->linkage_name (), "__main") == 0)
1901 pc += 5;
1902 }
1903 }
1904
1905 return pc;
1906 }
1907
1908 /* This function is 64-bit safe. */
1909
1910 static CORE_ADDR
1911 i386_unwind_pc (struct gdbarch *gdbarch, const frame_info_ptr &next_frame)
1912 {
1913 gdb_byte buf[8];
1914
1915 frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
1916 return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
1917 }
1918 \f
1919
1920 /* Normal frames. */
1921
1922 static void
1923 i386_frame_cache_1 (const frame_info_ptr &this_frame,
1924 struct i386_frame_cache *cache)
1925 {
1926 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1927 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1928 gdb_byte buf[4];
1929 int i;
1930
1931 cache->pc = get_frame_func (this_frame);
1932
1933 /* In principle, for normal frames, %ebp holds the frame pointer,
1934 which holds the base address for the current stack frame.
1935 However, for functions that don't need it, the frame pointer is
1936 optional. For these "frameless" functions the frame pointer is
1937 actually the frame pointer of the calling frame. Signal
1938 trampolines are just a special case of a "frameless" function.
1939 They (usually) share their frame pointer with the frame that was
1940 in progress when the signal occurred. */
1941
1942 get_frame_register (this_frame, I386_EBP_REGNUM, buf);
1943 cache->base = extract_unsigned_integer (buf, 4, byte_order);
1944 if (cache->base == 0)
1945 {
1946 cache->base_p = 1;
1947 return;
1948 }
1949
1950 /* For normal frames, %eip is stored at 4(%ebp). */
1951 cache->saved_regs[I386_EIP_REGNUM] = 4;
1952
1953 if (cache->pc != 0)
1954 i386_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
1955 cache);
1956
1957 if (cache->locals < 0)
1958 {
1959 /* We didn't find a valid frame, which means that CACHE->base
1960 currently holds the frame pointer for our calling frame. If
1961 we're at the start of a function, or somewhere half-way its
1962 prologue, the function's frame probably hasn't been fully
1963 setup yet. Try to reconstruct the base address for the stack
1964 frame by looking at the stack pointer. For truly "frameless"
1965 functions this might work too. */
1966
1967 if (cache->saved_sp_reg != -1)
1968 {
1969 /* Saved stack pointer has been saved. */
1970 get_frame_register (this_frame, cache->saved_sp_reg, buf);
1971 cache->saved_sp = extract_unsigned_integer (buf, 4, byte_order);
1972
1973 /* We're halfway aligning the stack. */
1974 cache->base = ((cache->saved_sp - 4) & 0xfffffff0) - 4;
1975 cache->saved_regs[I386_EIP_REGNUM] = cache->saved_sp - 4;
1976
1977 /* This will be added back below. */
1978 cache->saved_regs[I386_EIP_REGNUM] -= cache->base;
1979 }
1980 else if (cache->pc != 0
1981 || target_read_code (get_frame_pc (this_frame), buf, 1))
1982 {
1983 /* We're in a known function, but did not find a frame
1984 setup. Assume that the function does not use %ebp.
1985 Alternatively, we may have jumped to an invalid
1986 address; in that case there is definitely no new
1987 frame in %ebp. */
1988 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
1989 cache->base = extract_unsigned_integer (buf, 4, byte_order)
1990 + cache->sp_offset;
1991 }
1992 else
1993 /* We're in an unknown function. We could not find the start
1994 of the function to analyze the prologue; our best option is
1995 to assume a typical frame layout with the caller's %ebp
1996 saved. */
1997 cache->saved_regs[I386_EBP_REGNUM] = 0;
1998 }
1999
2000 if (cache->saved_sp_reg != -1)
2001 {
2002 /* Saved stack pointer has been saved (but the SAVED_SP_REG
2003 register may be unavailable). */
2004 if (cache->saved_sp == 0
2005 && deprecated_frame_register_read (this_frame,
2006 cache->saved_sp_reg, buf))
2007 cache->saved_sp = extract_unsigned_integer (buf, 4, byte_order);
2008 }
2009 /* Now that we have the base address for the stack frame we can
2010 calculate the value of %esp in the calling frame. */
2011 else if (cache->saved_sp == 0)
2012 cache->saved_sp = cache->base + 8;
2013
2014 /* Adjust all the saved registers such that they contain addresses
2015 instead of offsets. */
2016 for (i = 0; i < I386_NUM_SAVED_REGS; i++)
2017 if (cache->saved_regs[i] != -1)
2018 cache->saved_regs[i] += cache->base;
2019
2020 cache->base_p = 1;
2021 }
2022
2023 static struct i386_frame_cache *
2024 i386_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
2025 {
2026 struct i386_frame_cache *cache;
2027
2028 if (*this_cache)
2029 return (struct i386_frame_cache *) *this_cache;
2030
2031 cache = i386_alloc_frame_cache ();
2032 *this_cache = cache;
2033
2034 try
2035 {
2036 i386_frame_cache_1 (this_frame, cache);
2037 }
2038 catch (const gdb_exception_error &ex)
2039 {
2040 if (ex.error != NOT_AVAILABLE_ERROR)
2041 throw;
2042 }
2043
2044 return cache;
2045 }
2046
2047 static void
2048 i386_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
2049 struct frame_id *this_id)
2050 {
2051 struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
2052
2053 if (!cache->base_p)
2054 (*this_id) = frame_id_build_unavailable_stack (cache->pc);
2055 else if (cache->base == 0)
2056 {
2057 /* This marks the outermost frame. */
2058 }
2059 else
2060 {
2061 /* See the end of i386_push_dummy_call. */
2062 (*this_id) = frame_id_build (cache->base + 8, cache->pc);
2063 }
2064 }
2065
2066 static enum unwind_stop_reason
2067 i386_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
2068 void **this_cache)
2069 {
2070 struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
2071
2072 if (!cache->base_p)
2073 return UNWIND_UNAVAILABLE;
2074
2075 /* This marks the outermost frame. */
2076 if (cache->base == 0)
2077 return UNWIND_OUTERMOST;
2078
2079 return UNWIND_NO_REASON;
2080 }
2081
2082 static struct value *
2083 i386_frame_prev_register (const frame_info_ptr &this_frame, void **this_cache,
2084 int regnum)
2085 {
2086 struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
2087
2088 gdb_assert (regnum >= 0);
2089
2090 /* The System V ABI says that:
2091
2092 "The flags register contains the system flags, such as the
2093 direction flag and the carry flag. The direction flag must be
2094 set to the forward (that is, zero) direction before entry and
2095 upon exit from a function. Other user flags have no specified
2096 role in the standard calling sequence and are not preserved."
2097
2098 To guarantee the "upon exit" part of that statement we fake a
2099 saved flags register that has its direction flag cleared.
2100
2101 Note that GCC doesn't seem to rely on the fact that the direction
2102 flag is cleared after a function return; it always explicitly
2103 clears the flag before operations where it matters.
2104
2105 FIXME: kettenis/20030316: I'm not quite sure whether this is the
2106 right thing to do. The way we fake the flags register here makes
2107 it impossible to change it. */
2108
2109 if (regnum == I386_EFLAGS_REGNUM)
2110 {
2111 ULONGEST val;
2112
2113 val = get_frame_register_unsigned (this_frame, regnum);
2114 val &= ~(1 << 10);
2115 return frame_unwind_got_constant (this_frame, regnum, val);
2116 }
2117
2118 if (regnum == I386_EIP_REGNUM && cache->pc_in_eax)
2119 return frame_unwind_got_register (this_frame, regnum, I386_EAX_REGNUM);
2120
2121 if (regnum == I386_ESP_REGNUM
2122 && (cache->saved_sp != 0 || cache->saved_sp_reg != -1))
2123 {
2124 /* If the SP has been saved, but we don't know where, then this
2125 means that SAVED_SP_REG register was found unavailable back
2126 when we built the cache. */
2127 if (cache->saved_sp == 0)
2128 return frame_unwind_got_register (this_frame, regnum,
2129 cache->saved_sp_reg);
2130 else
2131 return frame_unwind_got_constant (this_frame, regnum,
2132 cache->saved_sp);
2133 }
2134
2135 if (regnum < I386_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
2136 return frame_unwind_got_memory (this_frame, regnum,
2137 cache->saved_regs[regnum]);
2138
2139 return frame_unwind_got_register (this_frame, regnum, regnum);
2140 }
2141
2142 static const struct frame_unwind i386_frame_unwind =
2143 {
2144 "i386 prologue",
2145 NORMAL_FRAME,
2146 i386_frame_unwind_stop_reason,
2147 i386_frame_this_id,
2148 i386_frame_prev_register,
2149 NULL,
2150 default_frame_sniffer
2151 };
2152
2153 /* Normal frames, but in a function epilogue. */
2154
2155 /* Implement the stack_frame_destroyed_p gdbarch method.
2156
2157 The epilogue is defined here as the 'ret' instruction, which will
2158 follow any instruction such as 'leave' or 'pop %ebp' that destroys
2159 the function's stack frame. */
2160
2161 static int
2162 i386_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
2163 {
2164 gdb_byte insn;
2165 if (target_read_memory (pc, &insn, 1))
2166 return 0; /* Can't read memory at pc. */
2167
2168 if (insn != 0xc3) /* 'ret' instruction. */
2169 return 0;
2170
2171 return 1;
2172 }
2173
2174 static int
2175 i386_epilogue_frame_sniffer_1 (const struct frame_unwind *self,
2176 const frame_info_ptr &this_frame,
2177 void **this_prologue_cache, bool override_p)
2178 {
2179 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2180 CORE_ADDR pc = get_frame_pc (this_frame);
2181
2182 if (frame_relative_level (this_frame) != 0)
2183 /* We're not in the inner frame, so assume we're not in an epilogue. */
2184 return 0;
2185
2186 bool unwind_valid_p
2187 = compunit_epilogue_unwind_valid (find_pc_compunit_symtab (pc));
2188 if (override_p)
2189 {
2190 if (unwind_valid_p)
2191 /* Don't override the symtab unwinders, skip
2192 "i386 epilogue override". */
2193 return 0;
2194 }
2195 else
2196 {
2197 if (!unwind_valid_p)
2198 /* "i386 epilogue override" unwinder already ran, skip
2199 "i386 epilogue". */
2200 return 0;
2201 }
2202
2203 /* Check whether we're in an epilogue. */
2204 return i386_stack_frame_destroyed_p (gdbarch, pc);
2205 }
2206
2207 static int
2208 i386_epilogue_override_frame_sniffer (const struct frame_unwind *self,
2209 const frame_info_ptr &this_frame,
2210 void **this_prologue_cache)
2211 {
2212 return i386_epilogue_frame_sniffer_1 (self, this_frame, this_prologue_cache,
2213 true);
2214 }
2215
2216 static int
2217 i386_epilogue_frame_sniffer (const struct frame_unwind *self,
2218 const frame_info_ptr &this_frame,
2219 void **this_prologue_cache)
2220 {
2221 return i386_epilogue_frame_sniffer_1 (self, this_frame, this_prologue_cache,
2222 false);
2223 }
2224
2225 static struct i386_frame_cache *
2226 i386_epilogue_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
2227 {
2228 struct i386_frame_cache *cache;
2229 CORE_ADDR sp;
2230
2231 if (*this_cache)
2232 return (struct i386_frame_cache *) *this_cache;
2233
2234 cache = i386_alloc_frame_cache ();
2235 *this_cache = cache;
2236
2237 try
2238 {
2239 cache->pc = get_frame_func (this_frame);
2240
2241 /* At this point the stack looks as if we just entered the
2242 function, with the return address at the top of the
2243 stack. */
2244 sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);
2245 cache->base = sp + cache->sp_offset;
2246 cache->saved_sp = cache->base + 8;
2247 cache->saved_regs[I386_EIP_REGNUM] = cache->base + 4;
2248
2249 cache->base_p = 1;
2250 }
2251 catch (const gdb_exception_error &ex)
2252 {
2253 if (ex.error != NOT_AVAILABLE_ERROR)
2254 throw;
2255 }
2256
2257 return cache;
2258 }
2259
2260 static enum unwind_stop_reason
2261 i386_epilogue_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
2262 void **this_cache)
2263 {
2264 struct i386_frame_cache *cache =
2265 i386_epilogue_frame_cache (this_frame, this_cache);
2266
2267 if (!cache->base_p)
2268 return UNWIND_UNAVAILABLE;
2269
2270 return UNWIND_NO_REASON;
2271 }
2272
2273 static void
2274 i386_epilogue_frame_this_id (const frame_info_ptr &this_frame,
2275 void **this_cache,
2276 struct frame_id *this_id)
2277 {
2278 struct i386_frame_cache *cache =
2279 i386_epilogue_frame_cache (this_frame, this_cache);
2280
2281 if (!cache->base_p)
2282 (*this_id) = frame_id_build_unavailable_stack (cache->pc);
2283 else
2284 (*this_id) = frame_id_build (cache->base + 8, cache->pc);
2285 }
2286
2287 static struct value *
2288 i386_epilogue_frame_prev_register (const frame_info_ptr &this_frame,
2289 void **this_cache, int regnum)
2290 {
2291 /* Make sure we've initialized the cache. */
2292 i386_epilogue_frame_cache (this_frame, this_cache);
2293
2294 return i386_frame_prev_register (this_frame, this_cache, regnum);
2295 }
2296
2297 static const struct frame_unwind i386_epilogue_override_frame_unwind =
2298 {
2299 "i386 epilogue override",
2300 NORMAL_FRAME,
2301 i386_epilogue_frame_unwind_stop_reason,
2302 i386_epilogue_frame_this_id,
2303 i386_epilogue_frame_prev_register,
2304 NULL,
2305 i386_epilogue_override_frame_sniffer
2306 };
2307
2308 static const struct frame_unwind i386_epilogue_frame_unwind =
2309 {
2310 "i386 epilogue",
2311 NORMAL_FRAME,
2312 i386_epilogue_frame_unwind_stop_reason,
2313 i386_epilogue_frame_this_id,
2314 i386_epilogue_frame_prev_register,
2315 NULL,
2316 i386_epilogue_frame_sniffer
2317 };
2318 \f
2319
2320 /* Stack-based trampolines. */
2321
2322 /* These trampolines are used on cross x86 targets, when taking the
2323 address of a nested function. When executing these trampolines,
2324 no stack frame is set up, so we are in a similar situation as in
2325 epilogues and i386_epilogue_frame_this_id can be re-used. */
2326
2327 /* Static chain passed in register. */
2328
2329 static i386_insn i386_tramp_chain_in_reg_insns[] =
2330 {
2331 /* `movl imm32, %eax' and `movl imm32, %ecx' */
2332 { 5, { 0xb8 }, { 0xfe } },
2333
2334 /* `jmp imm32' */
2335 { 5, { 0xe9 }, { 0xff } },
2336
2337 {0}
2338 };
2339
2340 /* Static chain passed on stack (when regparm=3). */
2341
2342 static i386_insn i386_tramp_chain_on_stack_insns[] =
2343 {
2344 /* `push imm32' */
2345 { 5, { 0x68 }, { 0xff } },
2346
2347 /* `jmp imm32' */
2348 { 5, { 0xe9 }, { 0xff } },
2349
2350 {0}
2351 };
2352
2353 /* Return whether PC points inside a stack trampoline. */
2354
2355 static int
2356 i386_in_stack_tramp_p (CORE_ADDR pc)
2357 {
2358 gdb_byte insn;
2359 const char *name;
2360
2361 /* A stack trampoline is detected if no name is associated
2362 to the current pc and if it points inside a trampoline
2363 sequence. */
2364
2365 find_pc_partial_function (pc, &name, NULL, NULL);
2366 if (name)
2367 return 0;
2368
2369 if (target_read_memory (pc, &insn, 1))
2370 return 0;
2371
2372 if (!i386_match_insn_block (pc, i386_tramp_chain_in_reg_insns)
2373 && !i386_match_insn_block (pc, i386_tramp_chain_on_stack_insns))
2374 return 0;
2375
2376 return 1;
2377 }
2378
2379 static int
2380 i386_stack_tramp_frame_sniffer (const struct frame_unwind *self,
2381 const frame_info_ptr &this_frame,
2382 void **this_cache)
2383 {
2384 if (frame_relative_level (this_frame) == 0)
2385 return i386_in_stack_tramp_p (get_frame_pc (this_frame));
2386 else
2387 return 0;
2388 }
2389
2390 static const struct frame_unwind i386_stack_tramp_frame_unwind =
2391 {
2392 "i386 stack tramp",
2393 NORMAL_FRAME,
2394 i386_epilogue_frame_unwind_stop_reason,
2395 i386_epilogue_frame_this_id,
2396 i386_epilogue_frame_prev_register,
2397 NULL,
2398 i386_stack_tramp_frame_sniffer
2399 };
2400 \f
2401 /* Generate a bytecode expression to get the value of the saved PC. */
2402
2403 static void
2404 i386_gen_return_address (struct gdbarch *gdbarch,
2405 struct agent_expr *ax, struct axs_value *value,
2406 CORE_ADDR scope)
2407 {
2408 /* The following sequence assumes the traditional use of the base
2409 register. */
2410 ax_reg (ax, I386_EBP_REGNUM);
2411 ax_const_l (ax, 4);
2412 ax_simple (ax, aop_add);
2413 value->type = register_type (gdbarch, I386_EIP_REGNUM);
2414 value->kind = axs_lvalue_memory;
2415 }
2416 \f
2417
2418 /* Signal trampolines. */
2419
2420 static struct i386_frame_cache *
2421 i386_sigtramp_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
2422 {
2423 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2424 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
2425 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2426 struct i386_frame_cache *cache;
2427 CORE_ADDR addr;
2428 gdb_byte buf[4];
2429
2430 if (*this_cache)
2431 return (struct i386_frame_cache *) *this_cache;
2432
2433 cache = i386_alloc_frame_cache ();
2434
2435 try
2436 {
2437 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
2438 cache->base = extract_unsigned_integer (buf, 4, byte_order) - 4;
2439
2440 addr = tdep->sigcontext_addr (this_frame);
2441 if (tdep->sc_reg_offset)
2442 {
2443 int i;
2444
2445 gdb_assert (tdep->sc_num_regs <= I386_NUM_SAVED_REGS);
2446
2447 for (i = 0; i < tdep->sc_num_regs; i++)
2448 if (tdep->sc_reg_offset[i] != -1)
2449 cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
2450 }
2451 else
2452 {
2453 cache->saved_regs[I386_EIP_REGNUM] = addr + tdep->sc_pc_offset;
2454 cache->saved_regs[I386_ESP_REGNUM] = addr + tdep->sc_sp_offset;
2455 }
2456
2457 cache->base_p = 1;
2458 }
2459 catch (const gdb_exception_error &ex)
2460 {
2461 if (ex.error != NOT_AVAILABLE_ERROR)
2462 throw;
2463 }
2464
2465 *this_cache = cache;
2466 return cache;
2467 }
2468
2469 static enum unwind_stop_reason
2470 i386_sigtramp_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
2471 void **this_cache)
2472 {
2473 struct i386_frame_cache *cache =
2474 i386_sigtramp_frame_cache (this_frame, this_cache);
2475
2476 if (!cache->base_p)
2477 return UNWIND_UNAVAILABLE;
2478
2479 return UNWIND_NO_REASON;
2480 }
2481
2482 static void
2483 i386_sigtramp_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
2484 struct frame_id *this_id)
2485 {
2486 struct i386_frame_cache *cache =
2487 i386_sigtramp_frame_cache (this_frame, this_cache);
2488
2489 if (!cache->base_p)
2490 (*this_id) = frame_id_build_unavailable_stack (get_frame_pc (this_frame));
2491 else
2492 {
2493 /* See the end of i386_push_dummy_call. */
2494 (*this_id) = frame_id_build (cache->base + 8, get_frame_pc (this_frame));
2495 }
2496 }
2497
2498 static struct value *
2499 i386_sigtramp_frame_prev_register (const frame_info_ptr &this_frame,
2500 void **this_cache, int regnum)
2501 {
2502 /* Make sure we've initialized the cache. */
2503 i386_sigtramp_frame_cache (this_frame, this_cache);
2504
2505 return i386_frame_prev_register (this_frame, this_cache, regnum);
2506 }
2507
2508 static int
2509 i386_sigtramp_frame_sniffer (const struct frame_unwind *self,
2510 const frame_info_ptr &this_frame,
2511 void **this_prologue_cache)
2512 {
2513 gdbarch *arch = get_frame_arch (this_frame);
2514 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch);
2515
2516 /* We shouldn't even bother if we don't have a sigcontext_addr
2517 handler. */
2518 if (tdep->sigcontext_addr == NULL)
2519 return 0;
2520
2521 if (tdep->sigtramp_p != NULL)
2522 {
2523 if (tdep->sigtramp_p (this_frame))
2524 return 1;
2525 }
2526
2527 if (tdep->sigtramp_start != 0)
2528 {
2529 CORE_ADDR pc = get_frame_pc (this_frame);
2530
2531 gdb_assert (tdep->sigtramp_end != 0);
2532 if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
2533 return 1;
2534 }
2535
2536 return 0;
2537 }
2538
2539 static const struct frame_unwind i386_sigtramp_frame_unwind =
2540 {
2541 "i386 sigtramp",
2542 SIGTRAMP_FRAME,
2543 i386_sigtramp_frame_unwind_stop_reason,
2544 i386_sigtramp_frame_this_id,
2545 i386_sigtramp_frame_prev_register,
2546 NULL,
2547 i386_sigtramp_frame_sniffer
2548 };
2549 \f
2550
2551 static CORE_ADDR
2552 i386_frame_base_address (const frame_info_ptr &this_frame, void **this_cache)
2553 {
2554 struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
2555
2556 return cache->base;
2557 }
2558
2559 static const struct frame_base i386_frame_base =
2560 {
2561 &i386_frame_unwind,
2562 i386_frame_base_address,
2563 i386_frame_base_address,
2564 i386_frame_base_address
2565 };
2566
2567 static struct frame_id
2568 i386_dummy_id (struct gdbarch *gdbarch, const frame_info_ptr &this_frame)
2569 {
2570 CORE_ADDR fp;
2571
2572 fp = get_frame_register_unsigned (this_frame, I386_EBP_REGNUM);
2573
2574 /* See the end of i386_push_dummy_call. */
2575 return frame_id_build (fp + 8, get_frame_pc (this_frame));
2576 }
2577
2578 /* _Decimal128 function return values need 16-byte alignment on the
2579 stack. */
2580
2581 static CORE_ADDR
2582 i386_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
2583 {
2584 return sp & -(CORE_ADDR)16;
2585 }
2586 \f
2587
2588 /* Figure out where the longjmp will land. Slurp the args out of the
2589 stack. We expect the first arg to be a pointer to the jmp_buf
2590 structure from which we extract the address that we will land at.
2591 This address is copied into PC. This routine returns non-zero on
2592 success. */
2593
2594 static int
2595 i386_get_longjmp_target (const frame_info_ptr &frame, CORE_ADDR *pc)
2596 {
2597 gdb_byte buf[4];
2598 CORE_ADDR sp, jb_addr;
2599 struct gdbarch *gdbarch = get_frame_arch (frame);
2600 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2601 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
2602 int jb_pc_offset = tdep->jb_pc_offset;
2603
2604 /* If JB_PC_OFFSET is -1, we have no way to find out where the
2605 longjmp will land. */
2606 if (jb_pc_offset == -1)
2607 return 0;
2608
2609 get_frame_register (frame, I386_ESP_REGNUM, buf);
2610 sp = extract_unsigned_integer (buf, 4, byte_order);
2611 if (target_read_memory (sp + 4, buf, 4))
2612 return 0;
2613
2614 jb_addr = extract_unsigned_integer (buf, 4, byte_order);
2615 if (target_read_memory (jb_addr + jb_pc_offset, buf, 4))
2616 return 0;
2617
2618 *pc = extract_unsigned_integer (buf, 4, byte_order);
2619 return 1;
2620 }
2621 \f
2622
2623 /* Check whether TYPE must be 16-byte-aligned when passed as a
2624 function argument. 16-byte vectors, _Decimal128 and structures or
2625 unions containing such types must be 16-byte-aligned; other
2626 arguments are 4-byte-aligned. */
2627
2628 static int
2629 i386_16_byte_align_p (struct type *type)
2630 {
2631 type = check_typedef (type);
2632 if ((type->code () == TYPE_CODE_DECFLOAT
2633 || (type->code () == TYPE_CODE_ARRAY && type->is_vector ()))
2634 && type->length () == 16)
2635 return 1;
2636 if (type->code () == TYPE_CODE_ARRAY)
2637 return i386_16_byte_align_p (type->target_type ());
2638 if (type->code () == TYPE_CODE_STRUCT
2639 || type->code () == TYPE_CODE_UNION)
2640 {
2641 int i;
2642 for (i = 0; i < type->num_fields (); i++)
2643 {
2644 if (type->field (i).is_static ())
2645 continue;
2646 if (i386_16_byte_align_p (type->field (i).type ()))
2647 return 1;
2648 }
2649 }
2650 return 0;
2651 }
2652
2653 /* Implementation for set_gdbarch_push_dummy_code. */
2654
2655 static CORE_ADDR
2656 i386_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp, CORE_ADDR funaddr,
2657 struct value **args, int nargs, struct type *value_type,
2658 CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
2659 struct regcache *regcache)
2660 {
2661 /* Use 0xcc breakpoint - 1 byte. */
2662 *bp_addr = sp - 1;
2663 *real_pc = funaddr;
2664
2665 /* Keep the stack aligned. */
2666 return sp - 16;
2667 }
2668
2669 /* The "push_dummy_call" gdbarch method, optionally with the thiscall
2670 calling convention. */
2671
2672 CORE_ADDR
2673 i386_thiscall_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
2674 struct regcache *regcache, CORE_ADDR bp_addr,
2675 int nargs, struct value **args, CORE_ADDR sp,
2676 function_call_return_method return_method,
2677 CORE_ADDR struct_addr, bool thiscall)
2678 {
2679 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2680 gdb_byte buf[4];
2681 int i;
2682 int write_pass;
2683 int args_space = 0;
2684
2685 /* Determine the total space required for arguments and struct
2686 return address in a first pass (allowing for 16-byte-aligned
2687 arguments), then push arguments in a second pass. */
2688
2689 for (write_pass = 0; write_pass < 2; write_pass++)
2690 {
2691 int args_space_used = 0;
2692
2693 if (return_method == return_method_struct)
2694 {
2695 if (write_pass)
2696 {
2697 /* Push value address. */
2698 store_unsigned_integer (buf, 4, byte_order, struct_addr);
2699 write_memory (sp, buf, 4);
2700 args_space_used += 4;
2701 }
2702 else
2703 args_space += 4;
2704 }
2705
2706 for (i = thiscall ? 1 : 0; i < nargs; i++)
2707 {
2708 int len = args[i]->enclosing_type ()->length ();
2709
2710 if (write_pass)
2711 {
2712 if (i386_16_byte_align_p (args[i]->enclosing_type ()))
2713 args_space_used = align_up (args_space_used, 16);
2714
2715 write_memory (sp + args_space_used,
2716 args[i]->contents_all ().data (), len);
2717 /* The System V ABI says that:
2718
2719 "An argument's size is increased, if necessary, to make it a
2720 multiple of [32-bit] words. This may require tail padding,
2721 depending on the size of the argument."
2722
2723 This makes sure the stack stays word-aligned. */
2724 args_space_used += align_up (len, 4);
2725 }
2726 else
2727 {
2728 if (i386_16_byte_align_p (args[i]->enclosing_type ()))
2729 args_space = align_up (args_space, 16);
2730 args_space += align_up (len, 4);
2731 }
2732 }
2733
2734 if (!write_pass)
2735 {
2736 sp -= args_space;
2737
2738 /* The original System V ABI only requires word alignment,
2739 but modern incarnations need 16-byte alignment in order
2740 to support SSE. Since wasting a few bytes here isn't
2741 harmful we unconditionally enforce 16-byte alignment. */
2742 sp &= ~0xf;
2743 }
2744 }
2745
2746 /* Store return address. */
2747 sp -= 4;
2748 store_unsigned_integer (buf, 4, byte_order, bp_addr);
2749 write_memory (sp, buf, 4);
2750
2751 /* Finally, update the stack pointer... */
2752 store_unsigned_integer (buf, 4, byte_order, sp);
2753 regcache->cooked_write (I386_ESP_REGNUM, buf);
2754
2755 /* ...and fake a frame pointer. */
2756 regcache->cooked_write (I386_EBP_REGNUM, buf);
2757
2758 /* The 'this' pointer needs to be in ECX. */
2759 if (thiscall)
2760 regcache->cooked_write (I386_ECX_REGNUM,
2761 args[0]->contents_all ().data ());
2762
2763 /* If the PLT is position-independent, the SYSTEM V ABI requires %ebx to be
2764 set to the address of the GOT when doing a call to a PLT address.
2765 Note that we do not try to determine whether the PLT is
2766 position-independent, we just set the register regardless. */
2767 CORE_ADDR func_addr = find_function_addr (function, nullptr, nullptr);
2768 if (in_plt_section (func_addr))
2769 {
2770 struct objfile *objf = nullptr;
2771 asection *asect = nullptr;
2772 obj_section *osect = nullptr;
2773
2774 /* Get object file containing func_addr. */
2775 obj_section *func_section = find_pc_section (func_addr);
2776 if (func_section != nullptr)
2777 objf = func_section->objfile;
2778
2779 if (objf != nullptr)
2780 {
2781 /* Get corresponding .got.plt or .got section. */
2782 asect = bfd_get_section_by_name (objf->obfd.get (), ".got.plt");
2783 if (asect == nullptr)
2784 asect = bfd_get_section_by_name (objf->obfd.get (), ".got");
2785 }
2786
2787 if (asect != nullptr)
2788 /* Translate asection to obj_section. */
2789 osect = maint_obj_section_from_bfd_section (objf->obfd.get (),
2790 asect, objf);
2791
2792 if (osect != nullptr)
2793 {
2794 /* Store the section address in %ebx. */
2795 store_unsigned_integer (buf, 4, byte_order, osect->addr ());
2796 regcache->cooked_write (I386_EBX_REGNUM, buf);
2797 }
2798 else
2799 {
2800 /* If we would only do this for a position-independent PLT, it would
2801 make sense to issue a warning here. */
2802 }
2803 }
2804
2805 /* MarkK wrote: This "+ 8" is all over the place:
2806 (i386_frame_this_id, i386_sigtramp_frame_this_id,
2807 i386_dummy_id). It's there, since all frame unwinders for
2808 a given target have to agree (within a certain margin) on the
2809 definition of the stack address of a frame. Otherwise frame id
2810 comparison might not work correctly. Since DWARF2/GCC uses the
2811 stack address *before* the function call as a frame's CFA. On
2812 the i386, when %ebp is used as a frame pointer, the offset
2813 between the contents %ebp and the CFA as defined by GCC. */
2814 return sp + 8;
2815 }
2816
2817 /* Implement the "push_dummy_call" gdbarch method. */
2818
2819 static CORE_ADDR
2820 i386_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
2821 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
2822 struct value **args, CORE_ADDR sp,
2823 function_call_return_method return_method,
2824 CORE_ADDR struct_addr)
2825 {
2826 return i386_thiscall_push_dummy_call (gdbarch, function, regcache, bp_addr,
2827 nargs, args, sp, return_method,
2828 struct_addr, false);
2829 }
2830
2831 /* These registers are used for returning integers (and on some
2832 targets also for returning `struct' and `union' values when their
2833 size and alignment match an integer type). */
2834 #define LOW_RETURN_REGNUM I386_EAX_REGNUM /* %eax */
2835 #define HIGH_RETURN_REGNUM I386_EDX_REGNUM /* %edx */
2836
2837 /* Read, for architecture GDBARCH, a function return value of TYPE
2838 from REGCACHE, and copy that into VALBUF. */
2839
2840 static void
2841 i386_extract_return_value (struct gdbarch *gdbarch, struct type *type,
2842 struct regcache *regcache, gdb_byte *valbuf)
2843 {
2844 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
2845 int len = type->length ();
2846 gdb_byte buf[I386_MAX_REGISTER_SIZE];
2847
2848 /* _Float16 and _Float16 _Complex values are returned via xmm0. */
2849 if (((type->code () == TYPE_CODE_FLT) && len == 2)
2850 || ((type->code () == TYPE_CODE_COMPLEX) && len == 4))
2851 {
2852 regcache->raw_read (I387_XMM0_REGNUM (tdep), valbuf);
2853 return;
2854 }
2855 else if (type->code () == TYPE_CODE_FLT)
2856 {
2857 if (tdep->st0_regnum < 0)
2858 {
2859 warning (_("Cannot find floating-point return value."));
2860 memset (valbuf, 0, len);
2861 return;
2862 }
2863
2864 /* Floating-point return values can be found in %st(0). Convert
2865 its contents to the desired type. This is probably not
2866 exactly how it would happen on the target itself, but it is
2867 the best we can do. */
2868 regcache->raw_read (I386_ST0_REGNUM, buf);
2869 target_float_convert (buf, i387_ext_type (gdbarch), valbuf, type);
2870 }
2871 else
2872 {
2873 int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
2874 int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
2875
2876 if (len <= low_size)
2877 {
2878 regcache->raw_read (LOW_RETURN_REGNUM, buf);
2879 memcpy (valbuf, buf, len);
2880 }
2881 else if (len <= (low_size + high_size))
2882 {
2883 regcache->raw_read (LOW_RETURN_REGNUM, buf);
2884 memcpy (valbuf, buf, low_size);
2885 regcache->raw_read (HIGH_RETURN_REGNUM, buf);
2886 memcpy (valbuf + low_size, buf, len - low_size);
2887 }
2888 else
2889 internal_error (_("Cannot extract return value of %d bytes long."),
2890 len);
2891 }
2892 }
2893
2894 /* Write, for architecture GDBARCH, a function return value of TYPE
2895 from VALBUF into REGCACHE. */
2896
2897 static void
2898 i386_store_return_value (struct gdbarch *gdbarch, struct type *type,
2899 struct regcache *regcache, const gdb_byte *valbuf)
2900 {
2901 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
2902 int len = type->length ();
2903
2904 if (type->code () == TYPE_CODE_FLT)
2905 {
2906 ULONGEST fstat;
2907 gdb_byte buf[I386_MAX_REGISTER_SIZE];
2908
2909 if (tdep->st0_regnum < 0)
2910 {
2911 warning (_("Cannot set floating-point return value."));
2912 return;
2913 }
2914
2915 /* Returning floating-point values is a bit tricky. Apart from
2916 storing the return value in %st(0), we have to simulate the
2917 state of the FPU at function return point. */
2918
2919 /* Convert the value found in VALBUF to the extended
2920 floating-point format used by the FPU. This is probably
2921 not exactly how it would happen on the target itself, but
2922 it is the best we can do. */
2923 target_float_convert (valbuf, type, buf, i387_ext_type (gdbarch));
2924 regcache->raw_write (I386_ST0_REGNUM, buf);
2925
2926 /* Set the top of the floating-point register stack to 7. The
2927 actual value doesn't really matter, but 7 is what a normal
2928 function return would end up with if the program started out
2929 with a freshly initialized FPU. */
2930 regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
2931 fstat |= (7 << 11);
2932 regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM (tdep), fstat);
2933
2934 /* Mark %st(1) through %st(7) as empty. Since we set the top of
2935 the floating-point register stack to 7, the appropriate value
2936 for the tag word is 0x3fff. */
2937 regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM (tdep), 0x3fff);
2938 }
2939 else
2940 {
2941 int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
2942 int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
2943
2944 if (len <= low_size)
2945 regcache->raw_write_part (LOW_RETURN_REGNUM, 0, len, valbuf);
2946 else if (len <= (low_size + high_size))
2947 {
2948 regcache->raw_write (LOW_RETURN_REGNUM, valbuf);
2949 regcache->raw_write_part (HIGH_RETURN_REGNUM, 0, len - low_size,
2950 valbuf + low_size);
2951 }
2952 else
2953 internal_error (_("Cannot store return value of %d bytes long."), len);
2954 }
2955 }
2956 \f
2957
2958 /* This is the variable that is set with "set struct-convention", and
2959 its legitimate values. */
2960 static const char default_struct_convention[] = "default";
2961 static const char pcc_struct_convention[] = "pcc";
2962 static const char reg_struct_convention[] = "reg";
2963 static const char *const valid_conventions[] =
2964 {
2965 default_struct_convention,
2966 pcc_struct_convention,
2967 reg_struct_convention,
2968 NULL
2969 };
2970 static const char *struct_convention = default_struct_convention;
2971
2972 /* Return non-zero if TYPE, which is assumed to be a structure,
2973 a union type, or an array type, should be returned in registers
2974 for architecture GDBARCH. */
2975
2976 static int
2977 i386_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
2978 {
2979 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
2980 enum type_code code = type->code ();
2981 int len = type->length ();
2982
2983 gdb_assert (code == TYPE_CODE_STRUCT
2984 || code == TYPE_CODE_UNION
2985 || code == TYPE_CODE_ARRAY);
2986
2987 if (struct_convention == pcc_struct_convention
2988 || (struct_convention == default_struct_convention
2989 && tdep->struct_return == pcc_struct_return)
2990 || TYPE_HAS_DYNAMIC_LENGTH (type))
2991 return 0;
2992
2993 /* Structures consisting of a single `float', `double' or 'long
2994 double' member are returned in %st(0). */
2995 if (code == TYPE_CODE_STRUCT && type->num_fields () == 1)
2996 {
2997 type = check_typedef (type->field (0).type ());
2998 if (type->code () == TYPE_CODE_FLT)
2999 return (len == 4 || len == 8 || len == 12);
3000 }
3001
3002 return (len == 1 || len == 2 || len == 4 || len == 8);
3003 }
3004
3005 /* Determine, for architecture GDBARCH, how a return value of TYPE
3006 should be returned. If it is supposed to be returned in registers,
3007 and READBUF is non-zero, read the appropriate value from REGCACHE,
3008 and copy it into READBUF. If WRITEBUF is non-zero, write the value
3009 from WRITEBUF into REGCACHE. */
3010
3011 static enum return_value_convention
3012 i386_return_value (struct gdbarch *gdbarch, struct value *function,
3013 struct type *type, struct regcache *regcache,
3014 struct value **read_value, const gdb_byte *writebuf)
3015 {
3016 enum type_code code = type->code ();
3017
3018 if (((code == TYPE_CODE_STRUCT
3019 || code == TYPE_CODE_UNION
3020 || code == TYPE_CODE_ARRAY)
3021 && !i386_reg_struct_return_p (gdbarch, type))
3022 /* Complex double and long double uses the struct return convention. */
3023 || (code == TYPE_CODE_COMPLEX && type->length () == 16)
3024 || (code == TYPE_CODE_COMPLEX && type->length () == 24)
3025 /* 128-bit decimal float uses the struct return convention. */
3026 || (code == TYPE_CODE_DECFLOAT && type->length () == 16))
3027 {
3028 /* The System V ABI says that:
3029
3030 "A function that returns a structure or union also sets %eax
3031 to the value of the original address of the caller's area
3032 before it returns. Thus when the caller receives control
3033 again, the address of the returned object resides in register
3034 %eax and can be used to access the object."
3035
3036 So the ABI guarantees that we can always find the return
3037 value just after the function has returned. */
3038
3039 /* Note that the ABI doesn't mention functions returning arrays,
3040 which is something possible in certain languages such as Ada.
3041 In this case, the value is returned as if it was wrapped in
3042 a record, so the convention applied to records also applies
3043 to arrays. */
3044
3045 if (read_value != nullptr)
3046 {
3047 ULONGEST addr;
3048
3049 regcache_raw_read_unsigned (regcache, I386_EAX_REGNUM, &addr);
3050 *read_value = value_at_non_lval (type, addr);
3051 }
3052
3053 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
3054 }
3055
3056 /* This special case is for structures consisting of a single
3057 `float', `double' or 'long double' member. These structures are
3058 returned in %st(0). For these structures, we call ourselves
3059 recursively, changing TYPE into the type of the first member of
3060 the structure. Since that should work for all structures that
3061 have only one member, we don't bother to check the member's type
3062 here. */
3063 if (code == TYPE_CODE_STRUCT && type->num_fields () == 1)
3064 {
3065 struct type *inner_type = check_typedef (type->field (0).type ());
3066 enum return_value_convention result
3067 = i386_return_value (gdbarch, function, inner_type, regcache,
3068 read_value, writebuf);
3069 if (read_value != nullptr)
3070 (*read_value)->deprecated_set_type (type);
3071 return result;
3072 }
3073
3074 if (read_value != nullptr)
3075 {
3076 *read_value = value::allocate (type);
3077 i386_extract_return_value (gdbarch, type, regcache,
3078 (*read_value)->contents_raw ().data ());
3079 }
3080 if (writebuf)
3081 i386_store_return_value (gdbarch, type, regcache, writebuf);
3082
3083 return RETURN_VALUE_REGISTER_CONVENTION;
3084 }
3085 \f
3086
3087 struct type *
3088 i387_ext_type (struct gdbarch *gdbarch)
3089 {
3090 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3091
3092 if (!tdep->i387_ext_type)
3093 {
3094 tdep->i387_ext_type = tdesc_find_type (gdbarch, "i387_ext");
3095 gdb_assert (tdep->i387_ext_type != NULL);
3096 }
3097
3098 return tdep->i387_ext_type;
3099 }
3100
3101 /* Construct vector type for pseudo ZMM registers. We can't use
3102 tdesc_find_type since ZMM isn't described in target description. */
3103
3104 static struct type *
3105 i386_zmm_type (struct gdbarch *gdbarch)
3106 {
3107 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3108
3109 if (!tdep->i386_zmm_type)
3110 {
3111 const struct builtin_type *bt = builtin_type (gdbarch);
3112
3113 /* The type we're building is this: */
3114 #if 0
3115 union __gdb_builtin_type_vec512i
3116 {
3117 int128_t v4_int128[4];
3118 int64_t v8_int64[8];
3119 int32_t v16_int32[16];
3120 int16_t v32_int16[32];
3121 int8_t v64_int8[64];
3122 double v8_double[8];
3123 float v16_float[16];
3124 float16_t v32_half[32];
3125 bfloat16_t v32_bfloat16[32];
3126 };
3127 #endif
3128
3129 struct type *t;
3130
3131 t = arch_composite_type (gdbarch,
3132 "__gdb_builtin_type_vec512i", TYPE_CODE_UNION);
3133 append_composite_type_field (t, "v32_bfloat16",
3134 init_vector_type (bt->builtin_bfloat16, 32));
3135 append_composite_type_field (t, "v32_half",
3136 init_vector_type (bt->builtin_half, 32));
3137 append_composite_type_field (t, "v16_float",
3138 init_vector_type (bt->builtin_float, 16));
3139 append_composite_type_field (t, "v8_double",
3140 init_vector_type (bt->builtin_double, 8));
3141 append_composite_type_field (t, "v64_int8",
3142 init_vector_type (bt->builtin_int8, 64));
3143 append_composite_type_field (t, "v32_int16",
3144 init_vector_type (bt->builtin_int16, 32));
3145 append_composite_type_field (t, "v16_int32",
3146 init_vector_type (bt->builtin_int32, 16));
3147 append_composite_type_field (t, "v8_int64",
3148 init_vector_type (bt->builtin_int64, 8));
3149 append_composite_type_field (t, "v4_int128",
3150 init_vector_type (bt->builtin_int128, 4));
3151
3152 t->set_is_vector (true);
3153 t->set_name ("builtin_type_vec512i");
3154 tdep->i386_zmm_type = t;
3155 }
3156
3157 return tdep->i386_zmm_type;
3158 }
3159
3160 /* Construct vector type for pseudo YMM registers. We can't use
3161 tdesc_find_type since YMM isn't described in target description. */
3162
3163 static struct type *
3164 i386_ymm_type (struct gdbarch *gdbarch)
3165 {
3166 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3167
3168 if (!tdep->i386_ymm_type)
3169 {
3170 const struct builtin_type *bt = builtin_type (gdbarch);
3171
3172 /* The type we're building is this: */
3173 #if 0
3174 union __gdb_builtin_type_vec256i
3175 {
3176 int128_t v2_int128[2];
3177 int64_t v4_int64[4];
3178 int32_t v8_int32[8];
3179 int16_t v16_int16[16];
3180 int8_t v32_int8[32];
3181 double v4_double[4];
3182 float v8_float[8];
3183 float16_t v16_half[16];
3184 bfloat16_t v16_bfloat16[16];
3185 };
3186 #endif
3187
3188 struct type *t;
3189
3190 t = arch_composite_type (gdbarch,
3191 "__gdb_builtin_type_vec256i", TYPE_CODE_UNION);
3192 append_composite_type_field (t, "v16_bfloat16",
3193 init_vector_type (bt->builtin_bfloat16, 16));
3194 append_composite_type_field (t, "v16_half",
3195 init_vector_type (bt->builtin_half, 16));
3196 append_composite_type_field (t, "v8_float",
3197 init_vector_type (bt->builtin_float, 8));
3198 append_composite_type_field (t, "v4_double",
3199 init_vector_type (bt->builtin_double, 4));
3200 append_composite_type_field (t, "v32_int8",
3201 init_vector_type (bt->builtin_int8, 32));
3202 append_composite_type_field (t, "v16_int16",
3203 init_vector_type (bt->builtin_int16, 16));
3204 append_composite_type_field (t, "v8_int32",
3205 init_vector_type (bt->builtin_int32, 8));
3206 append_composite_type_field (t, "v4_int64",
3207 init_vector_type (bt->builtin_int64, 4));
3208 append_composite_type_field (t, "v2_int128",
3209 init_vector_type (bt->builtin_int128, 2));
3210
3211 t->set_is_vector (true);
3212 t->set_name ("builtin_type_vec256i");
3213 tdep->i386_ymm_type = t;
3214 }
3215
3216 return tdep->i386_ymm_type;
3217 }
3218
3219 /* Construct vector type for MMX registers. */
3220 static struct type *
3221 i386_mmx_type (struct gdbarch *gdbarch)
3222 {
3223 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3224
3225 if (!tdep->i386_mmx_type)
3226 {
3227 const struct builtin_type *bt = builtin_type (gdbarch);
3228
3229 /* The type we're building is this: */
3230 #if 0
3231 union __gdb_builtin_type_vec64i
3232 {
3233 int64_t uint64;
3234 int32_t v2_int32[2];
3235 int16_t v4_int16[4];
3236 int8_t v8_int8[8];
3237 };
3238 #endif
3239
3240 struct type *t;
3241
3242 t = arch_composite_type (gdbarch,
3243 "__gdb_builtin_type_vec64i", TYPE_CODE_UNION);
3244
3245 append_composite_type_field (t, "uint64", bt->builtin_int64);
3246 append_composite_type_field (t, "v2_int32",
3247 init_vector_type (bt->builtin_int32, 2));
3248 append_composite_type_field (t, "v4_int16",
3249 init_vector_type (bt->builtin_int16, 4));
3250 append_composite_type_field (t, "v8_int8",
3251 init_vector_type (bt->builtin_int8, 8));
3252
3253 t->set_is_vector (true);
3254 t->set_name ("builtin_type_vec64i");
3255 tdep->i386_mmx_type = t;
3256 }
3257
3258 return tdep->i386_mmx_type;
3259 }
3260
3261 /* Return the GDB type object for the "standard" data type of data in
3262 register REGNUM. */
3263
3264 struct type *
3265 i386_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
3266 {
3267 if (i386_mmx_regnum_p (gdbarch, regnum))
3268 return i386_mmx_type (gdbarch);
3269 else if (i386_ymm_regnum_p (gdbarch, regnum))
3270 return i386_ymm_type (gdbarch);
3271 else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
3272 return i386_ymm_type (gdbarch);
3273 else if (i386_zmm_regnum_p (gdbarch, regnum))
3274 return i386_zmm_type (gdbarch);
3275 else
3276 {
3277 const struct builtin_type *bt = builtin_type (gdbarch);
3278 if (i386_byte_regnum_p (gdbarch, regnum))
3279 return bt->builtin_int8;
3280 else if (i386_word_regnum_p (gdbarch, regnum))
3281 return bt->builtin_int16;
3282 else if (i386_dword_regnum_p (gdbarch, regnum))
3283 return bt->builtin_int32;
3284 else if (i386_k_regnum_p (gdbarch, regnum))
3285 return bt->builtin_int64;
3286 }
3287
3288 internal_error (_("invalid regnum"));
3289 }
3290
3291 /* Map a cooked register onto a raw register or memory. For the i386,
3292 the MMX registers need to be mapped onto floating point registers. */
3293
3294 static int
3295 i386_mmx_regnum_to_fp_regnum (const frame_info_ptr &next_frame, int regnum)
3296 {
3297 gdbarch *arch = frame_unwind_arch (next_frame);
3298 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch);
3299 ULONGEST fstat
3300 = frame_unwind_register_unsigned (next_frame, I387_FSTAT_REGNUM (tdep));
3301 int tos = (fstat >> 11) & 0x7;
3302 int mmxreg = regnum - tdep->mm0_regnum;
3303 int fpreg = (mmxreg + tos) % 8;
3304
3305 return (I387_ST0_REGNUM (tdep) + fpreg);
3306 }
3307
3308 /* A helper function for us by i386_pseudo_register_read_value and
3309 amd64_pseudo_register_read_value. It does all the work but reads
3310 the data into an already-allocated value. */
3311
3312 value *
3313 i386_pseudo_register_read_value (gdbarch *gdbarch, const frame_info_ptr &next_frame,
3314 const int pseudo_reg_num)
3315 {
3316 if (i386_mmx_regnum_p (gdbarch, pseudo_reg_num))
3317 {
3318 int fpnum = i386_mmx_regnum_to_fp_regnum (next_frame, pseudo_reg_num);
3319
3320 /* Extract (always little endian). */
3321 return pseudo_from_raw_part (next_frame, pseudo_reg_num, fpnum, 0);
3322 }
3323 else
3324 {
3325 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3326 if (i386_zmm_regnum_p (gdbarch, pseudo_reg_num))
3327 {
3328 /* Which register is it, relative to zmm0. */
3329 int i_0 = pseudo_reg_num - tdep->zmm0_regnum;
3330
3331 if (i_0 < num_lower_zmm_regs)
3332 return pseudo_from_concat_raw (next_frame, pseudo_reg_num,
3333 I387_XMM0_REGNUM (tdep) + i_0,
3334 tdep->ymm0h_regnum + i_0,
3335 tdep->zmm0h_regnum + i_0);
3336 else
3337 {
3338 /* Which register is it, relative to zmm16. */
3339 int i_16 = i_0 - num_lower_zmm_regs;
3340
3341 return pseudo_from_concat_raw (next_frame, pseudo_reg_num,
3342 I387_XMM16_REGNUM (tdep) + i_16,
3343 I387_YMM16H_REGNUM (tdep) + i_16,
3344 tdep->zmm0h_regnum + i_0);
3345 }
3346 }
3347 else if (i386_ymm_regnum_p (gdbarch, pseudo_reg_num))
3348 {
3349 int i = pseudo_reg_num - tdep->ymm0_regnum;
3350
3351 return pseudo_from_concat_raw (next_frame, pseudo_reg_num,
3352 I387_XMM0_REGNUM (tdep) + i,
3353 tdep->ymm0h_regnum + i);
3354 }
3355 else if (i386_ymm_avx512_regnum_p (gdbarch, pseudo_reg_num))
3356 {
3357 int i = pseudo_reg_num - tdep->ymm16_regnum;
3358
3359 return pseudo_from_concat_raw (next_frame, pseudo_reg_num,
3360 I387_XMM16_REGNUM (tdep) + i,
3361 tdep->ymm16h_regnum + i);
3362 }
3363 else if (i386_word_regnum_p (gdbarch, pseudo_reg_num))
3364 {
3365 int gpnum = pseudo_reg_num - tdep->ax_regnum;
3366
3367 /* Extract (always little endian). */
3368 return pseudo_from_raw_part (next_frame, pseudo_reg_num, gpnum, 0);
3369 }
3370 else if (i386_byte_regnum_p (gdbarch, pseudo_reg_num))
3371 {
3372 int gpnum = pseudo_reg_num - tdep->al_regnum;
3373
3374 /* Extract (always little endian). We read both lower and
3375 upper registers. */
3376 return pseudo_from_raw_part (next_frame, pseudo_reg_num, gpnum % 4,
3377 gpnum >= 4 ? 1 : 0);
3378 }
3379 else
3380 internal_error (_("invalid regnum"));
3381 }
3382 }
3383
3384 void
3385 i386_pseudo_register_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
3386 const int pseudo_reg_num,
3387 gdb::array_view<const gdb_byte> buf)
3388 {
3389 if (i386_mmx_regnum_p (gdbarch, pseudo_reg_num))
3390 {
3391 int fpnum = i386_mmx_regnum_to_fp_regnum (next_frame, pseudo_reg_num);
3392
3393 pseudo_to_raw_part (next_frame, buf, fpnum, 0);
3394 }
3395 else
3396 {
3397 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3398
3399 if (i386_zmm_regnum_p (gdbarch, pseudo_reg_num))
3400 {
3401 /* Which register is it, relative to zmm0. */
3402 int reg_index_0 = pseudo_reg_num - tdep->zmm0_regnum;
3403
3404 if (reg_index_0 < num_lower_zmm_regs)
3405 pseudo_to_concat_raw (next_frame, buf,
3406 I387_XMM0_REGNUM (tdep) + reg_index_0,
3407 I387_YMM0_REGNUM (tdep) + reg_index_0,
3408 tdep->zmm0h_regnum + reg_index_0);
3409 else
3410 {
3411 /* Which register is it, relative to zmm16. */
3412 int reg_index_16 = reg_index_0 - num_lower_zmm_regs;
3413
3414 pseudo_to_concat_raw (next_frame, buf,
3415 I387_XMM16_REGNUM (tdep) + reg_index_16,
3416 I387_YMM16H_REGNUM (tdep) + reg_index_16,
3417 tdep->zmm0h_regnum + +reg_index_0);
3418 }
3419 }
3420 else if (i386_ymm_regnum_p (gdbarch, pseudo_reg_num))
3421 {
3422 int i = pseudo_reg_num - tdep->ymm0_regnum;
3423
3424 pseudo_to_concat_raw (next_frame, buf, I387_XMM0_REGNUM (tdep) + i,
3425 tdep->ymm0h_regnum + i);
3426 }
3427 else if (i386_ymm_avx512_regnum_p (gdbarch, pseudo_reg_num))
3428 {
3429 int i = pseudo_reg_num - tdep->ymm16_regnum;
3430
3431 pseudo_to_concat_raw (next_frame, buf, I387_XMM16_REGNUM (tdep) + i,
3432 tdep->ymm16h_regnum + i);
3433 }
3434 else if (i386_word_regnum_p (gdbarch, pseudo_reg_num))
3435 {
3436 int gpnum = pseudo_reg_num - tdep->ax_regnum;
3437
3438 pseudo_to_raw_part (next_frame, buf, gpnum, 0);
3439 }
3440 else if (i386_byte_regnum_p (gdbarch, pseudo_reg_num))
3441 {
3442 int gpnum = pseudo_reg_num - tdep->al_regnum;
3443
3444 pseudo_to_raw_part (next_frame, buf, gpnum % 4, gpnum >= 4 ? 1 : 0);
3445 }
3446 else
3447 internal_error (_("invalid regnum"));
3448 }
3449 }
3450
3451 /* Implement the 'ax_pseudo_register_collect' gdbarch method. */
3452
3453 int
3454 i386_ax_pseudo_register_collect (struct gdbarch *gdbarch,
3455 struct agent_expr *ax, int regnum)
3456 {
3457 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3458
3459 if (i386_mmx_regnum_p (gdbarch, regnum))
3460 {
3461 /* MMX to FPU register mapping depends on current TOS. Let's just
3462 not care and collect everything... */
3463 int i;
3464
3465 ax_reg_mask (ax, I387_FSTAT_REGNUM (tdep));
3466 for (i = 0; i < 8; i++)
3467 ax_reg_mask (ax, I387_ST0_REGNUM (tdep) + i);
3468 return 0;
3469 }
3470 else if (i386_zmm_regnum_p (gdbarch, regnum))
3471 {
3472 regnum -= tdep->zmm0_regnum;
3473 if (regnum < num_lower_zmm_regs)
3474 {
3475 ax_reg_mask (ax, I387_XMM0_REGNUM (tdep) + regnum);
3476 ax_reg_mask (ax, tdep->ymm0h_regnum + regnum);
3477 }
3478 else
3479 {
3480 ax_reg_mask (ax, I387_XMM16_REGNUM (tdep) + regnum
3481 - num_lower_zmm_regs);
3482 ax_reg_mask (ax, I387_YMM16H_REGNUM (tdep) + regnum
3483 - num_lower_zmm_regs);
3484 }
3485 ax_reg_mask (ax, tdep->zmm0h_regnum + regnum);
3486 return 0;
3487 }
3488 else if (i386_ymm_regnum_p (gdbarch, regnum))
3489 {
3490 regnum -= tdep->ymm0_regnum;
3491 ax_reg_mask (ax, I387_XMM0_REGNUM (tdep) + regnum);
3492 ax_reg_mask (ax, tdep->ymm0h_regnum + regnum);
3493 return 0;
3494 }
3495 else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
3496 {
3497 regnum -= tdep->ymm16_regnum;
3498 ax_reg_mask (ax, I387_XMM16_REGNUM (tdep) + regnum);
3499 ax_reg_mask (ax, tdep->ymm16h_regnum + regnum);
3500 return 0;
3501 }
3502 else if (i386_word_regnum_p (gdbarch, regnum))
3503 {
3504 int gpnum = regnum - tdep->ax_regnum;
3505
3506 ax_reg_mask (ax, gpnum);
3507 return 0;
3508 }
3509 else if (i386_byte_regnum_p (gdbarch, regnum))
3510 {
3511 int gpnum = regnum - tdep->al_regnum;
3512
3513 ax_reg_mask (ax, gpnum % 4);
3514 return 0;
3515 }
3516 else
3517 internal_error (_("invalid regnum"));
3518 return 1;
3519 }
3520 \f
3521
3522 /* Return the register number of the register allocated by GCC after
3523 REGNUM, or -1 if there is no such register. */
3524
3525 static int
3526 i386_next_regnum (int regnum)
3527 {
3528 /* GCC allocates the registers in the order:
3529
3530 %eax, %edx, %ecx, %ebx, %esi, %edi, %ebp, %esp, ...
3531
3532 Since storing a variable in %esp doesn't make any sense we return
3533 -1 for %ebp and for %esp itself. */
3534 static int next_regnum[] =
3535 {
3536 I386_EDX_REGNUM, /* Slot for %eax. */
3537 I386_EBX_REGNUM, /* Slot for %ecx. */
3538 I386_ECX_REGNUM, /* Slot for %edx. */
3539 I386_ESI_REGNUM, /* Slot for %ebx. */
3540 -1, -1, /* Slots for %esp and %ebp. */
3541 I386_EDI_REGNUM, /* Slot for %esi. */
3542 I386_EBP_REGNUM /* Slot for %edi. */
3543 };
3544
3545 if (regnum >= 0 && regnum < sizeof (next_regnum) / sizeof (next_regnum[0]))
3546 return next_regnum[regnum];
3547
3548 return -1;
3549 }
3550
3551 /* Return nonzero if a value of type TYPE stored in register REGNUM
3552 needs any special handling. */
3553
3554 static int
3555 i386_convert_register_p (struct gdbarch *gdbarch,
3556 int regnum, struct type *type)
3557 {
3558 int len = type->length ();
3559
3560 /* Values may be spread across multiple registers. Most debugging
3561 formats aren't expressive enough to specify the locations, so
3562 some heuristics is involved. Right now we only handle types that
3563 have a length that is a multiple of the word size, since GCC
3564 doesn't seem to put any other types into registers. */
3565 if (len > 4 && len % 4 == 0)
3566 {
3567 int last_regnum = regnum;
3568
3569 while (len > 4)
3570 {
3571 last_regnum = i386_next_regnum (last_regnum);
3572 len -= 4;
3573 }
3574
3575 if (last_regnum != -1)
3576 return 1;
3577 }
3578
3579 return i387_convert_register_p (gdbarch, regnum, type);
3580 }
3581
3582 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
3583 return its contents in TO. */
3584
3585 static int
3586 i386_register_to_value (const frame_info_ptr &frame, int regnum,
3587 struct type *type, gdb_byte *to,
3588 int *optimizedp, int *unavailablep)
3589 {
3590 struct gdbarch *gdbarch = get_frame_arch (frame);
3591 int len = type->length ();
3592
3593 if (i386_fp_regnum_p (gdbarch, regnum))
3594 return i387_register_to_value (frame, regnum, type, to,
3595 optimizedp, unavailablep);
3596
3597 /* Read a value spread across multiple registers. */
3598
3599 gdb_assert (len > 4 && len % 4 == 0);
3600
3601 while (len > 0)
3602 {
3603 gdb_assert (regnum != -1);
3604 gdb_assert (register_size (gdbarch, regnum) == 4);
3605
3606 auto to_view
3607 = gdb::make_array_view (to, register_size (gdbarch, regnum));
3608 frame_info_ptr next_frame = get_next_frame_sentinel_okay (frame);
3609 if (!get_frame_register_bytes (next_frame, regnum, 0, to_view,
3610 optimizedp, unavailablep))
3611 return 0;
3612
3613 regnum = i386_next_regnum (regnum);
3614 len -= 4;
3615 to += 4;
3616 }
3617
3618 *optimizedp = *unavailablep = 0;
3619 return 1;
3620 }
3621
3622 /* Write the contents FROM of a value of type TYPE into register
3623 REGNUM in frame FRAME. */
3624
3625 static void
3626 i386_value_to_register (const frame_info_ptr &frame, int regnum,
3627 struct type *type, const gdb_byte *from)
3628 {
3629 int len = type->length ();
3630
3631 if (i386_fp_regnum_p (get_frame_arch (frame), regnum))
3632 {
3633 i387_value_to_register (frame, regnum, type, from);
3634 return;
3635 }
3636
3637 /* Write a value spread across multiple registers. */
3638
3639 gdb_assert (len > 4 && len % 4 == 0);
3640
3641 while (len > 0)
3642 {
3643 gdb_assert (regnum != -1);
3644 gdb_assert (register_size (get_frame_arch (frame), regnum) == 4);
3645
3646 auto from_view = gdb::make_array_view (from, 4);
3647 put_frame_register (get_next_frame_sentinel_okay (frame), regnum,
3648 from_view);
3649 regnum = i386_next_regnum (regnum);
3650 len -= 4;
3651 from += 4;
3652 }
3653 }
3654 \f
3655 /* Supply register REGNUM from the buffer specified by GREGS and LEN
3656 in the general-purpose register set REGSET to register cache
3657 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
3658
3659 void
3660 i386_supply_gregset (const struct regset *regset, struct regcache *regcache,
3661 int regnum, const void *gregs, size_t len)
3662 {
3663 struct gdbarch *gdbarch = regcache->arch ();
3664 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3665 const gdb_byte *regs = (const gdb_byte *) gregs;
3666 int i;
3667
3668 gdb_assert (len >= tdep->sizeof_gregset);
3669
3670 for (i = 0; i < tdep->gregset_num_regs; i++)
3671 {
3672 if ((regnum == i || regnum == -1)
3673 && tdep->gregset_reg_offset[i] != -1)
3674 regcache->raw_supply (i, regs + tdep->gregset_reg_offset[i]);
3675 }
3676 }
3677
3678 /* Collect register REGNUM from the register cache REGCACHE and store
3679 it in the buffer specified by GREGS and LEN as described by the
3680 general-purpose register set REGSET. If REGNUM is -1, do this for
3681 all registers in REGSET. */
3682
3683 static void
3684 i386_collect_gregset (const struct regset *regset,
3685 const struct regcache *regcache,
3686 int regnum, void *gregs, size_t len)
3687 {
3688 struct gdbarch *gdbarch = regcache->arch ();
3689 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3690 gdb_byte *regs = (gdb_byte *) gregs;
3691 int i;
3692
3693 gdb_assert (len >= tdep->sizeof_gregset);
3694
3695 for (i = 0; i < tdep->gregset_num_regs; i++)
3696 {
3697 if ((regnum == i || regnum == -1)
3698 && tdep->gregset_reg_offset[i] != -1)
3699 regcache->raw_collect (i, regs + tdep->gregset_reg_offset[i]);
3700 }
3701 }
3702
3703 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
3704 in the floating-point register set REGSET to register cache
3705 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
3706
3707 static void
3708 i386_supply_fpregset (const struct regset *regset, struct regcache *regcache,
3709 int regnum, const void *fpregs, size_t len)
3710 {
3711 struct gdbarch *gdbarch = regcache->arch ();
3712 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3713
3714 if (len == I387_SIZEOF_FXSAVE)
3715 {
3716 i387_supply_fxsave (regcache, regnum, fpregs);
3717 return;
3718 }
3719
3720 gdb_assert (len >= tdep->sizeof_fpregset);
3721 i387_supply_fsave (regcache, regnum, fpregs);
3722 }
3723
3724 /* Collect register REGNUM from the register cache REGCACHE and store
3725 it in the buffer specified by FPREGS and LEN as described by the
3726 floating-point register set REGSET. If REGNUM is -1, do this for
3727 all registers in REGSET. */
3728
3729 static void
3730 i386_collect_fpregset (const struct regset *regset,
3731 const struct regcache *regcache,
3732 int regnum, void *fpregs, size_t len)
3733 {
3734 struct gdbarch *gdbarch = regcache->arch ();
3735 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3736
3737 if (len == I387_SIZEOF_FXSAVE)
3738 {
3739 i387_collect_fxsave (regcache, regnum, fpregs);
3740 return;
3741 }
3742
3743 gdb_assert (len >= tdep->sizeof_fpregset);
3744 i387_collect_fsave (regcache, regnum, fpregs);
3745 }
3746
3747 /* Register set definitions. */
3748
3749 const struct regset i386_gregset =
3750 {
3751 NULL, i386_supply_gregset, i386_collect_gregset
3752 };
3753
3754 const struct regset i386_fpregset =
3755 {
3756 NULL, i386_supply_fpregset, i386_collect_fpregset
3757 };
3758
3759 /* Default iterator over core file register note sections. */
3760
3761 void
3762 i386_iterate_over_regset_sections (struct gdbarch *gdbarch,
3763 iterate_over_regset_sections_cb *cb,
3764 void *cb_data,
3765 const struct regcache *regcache)
3766 {
3767 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3768
3769 cb (".reg", tdep->sizeof_gregset, tdep->sizeof_gregset, &i386_gregset, NULL,
3770 cb_data);
3771 if (tdep->sizeof_fpregset)
3772 cb (".reg2", tdep->sizeof_fpregset, tdep->sizeof_fpregset, tdep->fpregset,
3773 NULL, cb_data);
3774 }
3775 \f
3776
3777 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
3778
3779 CORE_ADDR
3780 i386_pe_skip_trampoline_code (const frame_info_ptr &frame,
3781 CORE_ADDR pc, char *name)
3782 {
3783 struct gdbarch *gdbarch = get_frame_arch (frame);
3784 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3785
3786 /* jmp *(dest) */
3787 if (pc && read_memory_unsigned_integer (pc, 2, byte_order) == 0x25ff)
3788 {
3789 unsigned long indirect =
3790 read_memory_unsigned_integer (pc + 2, 4, byte_order);
3791 struct minimal_symbol *indsym =
3792 indirect ? lookup_minimal_symbol_by_pc (indirect).minsym : 0;
3793 const char *symname = indsym ? indsym->linkage_name () : 0;
3794
3795 if (symname)
3796 {
3797 if (startswith (symname, "__imp_")
3798 || startswith (symname, "_imp_"))
3799 return name ? 1 :
3800 read_memory_unsigned_integer (indirect, 4, byte_order);
3801 }
3802 }
3803 return 0; /* Not a trampoline. */
3804 }
3805 \f
3806
3807 /* Return whether the THIS_FRAME corresponds to a sigtramp
3808 routine. */
3809
3810 int
3811 i386_sigtramp_p (const frame_info_ptr &this_frame)
3812 {
3813 CORE_ADDR pc = get_frame_pc (this_frame);
3814 const char *name;
3815
3816 find_pc_partial_function (pc, &name, NULL, NULL);
3817 return (name && strcmp ("_sigtramp", name) == 0);
3818 }
3819 \f
3820
3821 /* We have two flavours of disassembly. The machinery on this page
3822 deals with switching between those. */
3823
3824 static int
3825 i386_print_insn (bfd_vma pc, struct disassemble_info *info)
3826 {
3827 gdb_assert (disassembly_flavor == att_flavor
3828 || disassembly_flavor == intel_flavor);
3829
3830 info->disassembler_options = disassembly_flavor;
3831
3832 return default_print_insn (pc, info);
3833 }
3834 \f
3835
3836 /* There are a few i386 architecture variants that differ only
3837 slightly from the generic i386 target. For now, we don't give them
3838 their own source file, but include them here. As a consequence,
3839 they'll always be included. */
3840
3841 /* System V Release 4 (SVR4). */
3842
3843 /* Return whether THIS_FRAME corresponds to a SVR4 sigtramp
3844 routine. */
3845
3846 static int
3847 i386_svr4_sigtramp_p (const frame_info_ptr &this_frame)
3848 {
3849 CORE_ADDR pc = get_frame_pc (this_frame);
3850 const char *name;
3851
3852 /* The origin of these symbols is currently unknown. */
3853 find_pc_partial_function (pc, &name, NULL, NULL);
3854 return (name && (strcmp ("_sigreturn", name) == 0
3855 || strcmp ("sigvechandler", name) == 0));
3856 }
3857
3858 /* Assuming THIS_FRAME is for a SVR4 sigtramp routine, return the
3859 address of the associated sigcontext (ucontext) structure. */
3860
3861 static CORE_ADDR
3862 i386_svr4_sigcontext_addr (const frame_info_ptr &this_frame)
3863 {
3864 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3865 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3866 gdb_byte buf[4];
3867 CORE_ADDR sp;
3868
3869 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
3870 sp = extract_unsigned_integer (buf, 4, byte_order);
3871
3872 return read_memory_unsigned_integer (sp + 8, 4, byte_order);
3873 }
3874
3875 \f
3876
3877 /* Implementation of `gdbarch_stap_is_single_operand', as defined in
3878 gdbarch.h. */
3879
3880 int
3881 i386_stap_is_single_operand (struct gdbarch *gdbarch, const char *s)
3882 {
3883 return (*s == '$' /* Literal number. */
3884 || (isdigit (*s) && s[1] == '(' && s[2] == '%') /* Displacement. */
3885 || (*s == '(' && s[1] == '%') /* Register indirection. */
3886 || (*s == '%' && isalpha (s[1]))); /* Register access. */
3887 }
3888
3889 /* Helper function for i386_stap_parse_special_token.
3890
3891 This function parses operands of the form `-8+3+1(%rbp)', which
3892 must be interpreted as `*(-8 + 3 - 1 + (void *) $eax)'.
3893
3894 Return true if the operand was parsed successfully, false
3895 otherwise. */
3896
3897 static expr::operation_up
3898 i386_stap_parse_special_token_triplet (struct gdbarch *gdbarch,
3899 struct stap_parse_info *p)
3900 {
3901 const char *s = p->arg;
3902
3903 if (isdigit (*s) || *s == '-' || *s == '+')
3904 {
3905 bool got_minus[3];
3906 int i;
3907 long displacements[3];
3908 const char *start;
3909 int len;
3910 char *endp;
3911
3912 got_minus[0] = false;
3913 if (*s == '+')
3914 ++s;
3915 else if (*s == '-')
3916 {
3917 ++s;
3918 got_minus[0] = true;
3919 }
3920
3921 if (!isdigit ((unsigned char) *s))
3922 return {};
3923
3924 displacements[0] = strtol (s, &endp, 10);
3925 s = endp;
3926
3927 if (*s != '+' && *s != '-')
3928 {
3929 /* We are not dealing with a triplet. */
3930 return {};
3931 }
3932
3933 got_minus[1] = false;
3934 if (*s == '+')
3935 ++s;
3936 else
3937 {
3938 ++s;
3939 got_minus[1] = true;
3940 }
3941
3942 if (!isdigit ((unsigned char) *s))
3943 return {};
3944
3945 displacements[1] = strtol (s, &endp, 10);
3946 s = endp;
3947
3948 if (*s != '+' && *s != '-')
3949 {
3950 /* We are not dealing with a triplet. */
3951 return {};
3952 }
3953
3954 got_minus[2] = false;
3955 if (*s == '+')
3956 ++s;
3957 else
3958 {
3959 ++s;
3960 got_minus[2] = true;
3961 }
3962
3963 if (!isdigit ((unsigned char) *s))
3964 return {};
3965
3966 displacements[2] = strtol (s, &endp, 10);
3967 s = endp;
3968
3969 if (*s != '(' || s[1] != '%')
3970 return {};
3971
3972 s += 2;
3973 start = s;
3974
3975 while (isalnum (*s))
3976 ++s;
3977
3978 if (*s++ != ')')
3979 return {};
3980
3981 len = s - start - 1;
3982 std::string regname (start, len);
3983
3984 if (user_reg_map_name_to_regnum (gdbarch, regname.c_str (), len) == -1)
3985 error (_("Invalid register name `%s' on expression `%s'."),
3986 regname.c_str (), p->saved_arg);
3987
3988 LONGEST value = 0;
3989 for (i = 0; i < 3; i++)
3990 {
3991 LONGEST this_val = displacements[i];
3992 if (got_minus[i])
3993 this_val = -this_val;
3994 value += this_val;
3995 }
3996
3997 p->arg = s;
3998
3999 using namespace expr;
4000
4001 struct type *long_type = builtin_type (gdbarch)->builtin_long;
4002 operation_up offset
4003 = make_operation<long_const_operation> (long_type, value);
4004
4005 operation_up reg
4006 = make_operation<register_operation> (std::move (regname));
4007 struct type *void_ptr = builtin_type (gdbarch)->builtin_data_ptr;
4008 reg = make_operation<unop_cast_operation> (std::move (reg), void_ptr);
4009
4010 operation_up sum
4011 = make_operation<add_operation> (std::move (reg), std::move (offset));
4012 struct type *arg_ptr_type = lookup_pointer_type (p->arg_type);
4013 sum = make_operation<unop_cast_operation> (std::move (sum),
4014 arg_ptr_type);
4015 return make_operation<unop_ind_operation> (std::move (sum));
4016 }
4017
4018 return {};
4019 }
4020
4021 /* Helper function for i386_stap_parse_special_token.
4022
4023 This function parses operands of the form `register base +
4024 (register index * size) + offset', as represented in
4025 `(%rcx,%rax,8)', or `[OFFSET](BASE_REG,INDEX_REG[,SIZE])'.
4026
4027 Return true if the operand was parsed successfully, false
4028 otherwise. */
4029
4030 static expr::operation_up
4031 i386_stap_parse_special_token_three_arg_disp (struct gdbarch *gdbarch,
4032 struct stap_parse_info *p)
4033 {
4034 const char *s = p->arg;
4035
4036 if (isdigit (*s) || *s == '(' || *s == '-' || *s == '+')
4037 {
4038 bool offset_minus = false;
4039 long offset = 0;
4040 bool size_minus = false;
4041 long size = 0;
4042 const char *start;
4043 int len_base;
4044 int len_index;
4045
4046 if (*s == '+')
4047 ++s;
4048 else if (*s == '-')
4049 {
4050 ++s;
4051 offset_minus = true;
4052 }
4053
4054 if (offset_minus && !isdigit (*s))
4055 return {};
4056
4057 if (isdigit (*s))
4058 {
4059 char *endp;
4060
4061 offset = strtol (s, &endp, 10);
4062 s = endp;
4063 }
4064
4065 if (*s != '(' || s[1] != '%')
4066 return {};
4067
4068 s += 2;
4069 start = s;
4070
4071 while (isalnum (*s))
4072 ++s;
4073
4074 if (*s != ',' || s[1] != '%')
4075 return {};
4076
4077 len_base = s - start;
4078 std::string base (start, len_base);
4079
4080 if (user_reg_map_name_to_regnum (gdbarch, base.c_str (), len_base) == -1)
4081 error (_("Invalid register name `%s' on expression `%s'."),
4082 base.c_str (), p->saved_arg);
4083
4084 s += 2;
4085 start = s;
4086
4087 while (isalnum (*s))
4088 ++s;
4089
4090 len_index = s - start;
4091 std::string index (start, len_index);
4092
4093 if (user_reg_map_name_to_regnum (gdbarch, index.c_str (),
4094 len_index) == -1)
4095 error (_("Invalid register name `%s' on expression `%s'."),
4096 index.c_str (), p->saved_arg);
4097
4098 if (*s != ',' && *s != ')')
4099 return {};
4100
4101 if (*s == ',')
4102 {
4103 char *endp;
4104
4105 ++s;
4106 if (*s == '+')
4107 ++s;
4108 else if (*s == '-')
4109 {
4110 ++s;
4111 size_minus = true;
4112 }
4113
4114 size = strtol (s, &endp, 10);
4115 s = endp;
4116
4117 if (*s != ')')
4118 return {};
4119 }
4120
4121 ++s;
4122 p->arg = s;
4123
4124 using namespace expr;
4125
4126 struct type *long_type = builtin_type (gdbarch)->builtin_long;
4127 operation_up reg = make_operation<register_operation> (std::move (base));
4128
4129 if (offset != 0)
4130 {
4131 if (offset_minus)
4132 offset = -offset;
4133 operation_up value
4134 = make_operation<long_const_operation> (long_type, offset);
4135 reg = make_operation<add_operation> (std::move (reg),
4136 std::move (value));
4137 }
4138
4139 operation_up ind_reg
4140 = make_operation<register_operation> (std::move (index));
4141
4142 if (size != 0)
4143 {
4144 if (size_minus)
4145 size = -size;
4146 operation_up value
4147 = make_operation<long_const_operation> (long_type, size);
4148 ind_reg = make_operation<mul_operation> (std::move (ind_reg),
4149 std::move (value));
4150 }
4151
4152 operation_up sum
4153 = make_operation<add_operation> (std::move (reg),
4154 std::move (ind_reg));
4155
4156 struct type *arg_ptr_type = lookup_pointer_type (p->arg_type);
4157 sum = make_operation<unop_cast_operation> (std::move (sum),
4158 arg_ptr_type);
4159 return make_operation<unop_ind_operation> (std::move (sum));
4160 }
4161
4162 return {};
4163 }
4164
4165 /* Implementation of `gdbarch_stap_parse_special_token', as defined in
4166 gdbarch.h. */
4167
4168 expr::operation_up
4169 i386_stap_parse_special_token (struct gdbarch *gdbarch,
4170 struct stap_parse_info *p)
4171 {
4172 /* The special tokens to be parsed here are:
4173
4174 - `register base + (register index * size) + offset', as represented
4175 in `(%rcx,%rax,8)', or `[OFFSET](BASE_REG,INDEX_REG[,SIZE])'.
4176
4177 - Operands of the form `-8+3+1(%rbp)', which must be interpreted as
4178 `*(-8 + 3 - 1 + (void *) $eax)'. */
4179
4180 expr::operation_up result
4181 = i386_stap_parse_special_token_triplet (gdbarch, p);
4182
4183 if (result == nullptr)
4184 result = i386_stap_parse_special_token_three_arg_disp (gdbarch, p);
4185
4186 return result;
4187 }
4188
4189 /* Implementation of 'gdbarch_stap_adjust_register', as defined in
4190 gdbarch.h. */
4191
4192 static std::string
4193 i386_stap_adjust_register (struct gdbarch *gdbarch, struct stap_parse_info *p,
4194 const std::string &regname, int regnum)
4195 {
4196 static const std::unordered_set<std::string> reg_assoc
4197 = { "ax", "bx", "cx", "dx",
4198 "si", "di", "bp", "sp" };
4199
4200 /* If we are dealing with a register whose size is less than the size
4201 specified by the "[-]N@" prefix, and it is one of the registers that
4202 we know has an extended variant available, then use the extended
4203 version of the register instead. */
4204 if (register_size (gdbarch, regnum) < p->arg_type->length ()
4205 && reg_assoc.find (regname) != reg_assoc.end ())
4206 return "e" + regname;
4207
4208 /* Otherwise, just use the requested register. */
4209 return regname;
4210 }
4211
4212 \f
4213
4214 /* gdbarch gnu_triplet_regexp method. Both arches are acceptable as GDB always
4215 also supplies -m64 or -m32 by gdbarch_gcc_target_options. */
4216
4217 static const char *
4218 i386_gnu_triplet_regexp (struct gdbarch *gdbarch)
4219 {
4220 return "(x86_64|i.86)";
4221 }
4222
4223 \f
4224
4225 /* Implement the "in_indirect_branch_thunk" gdbarch function. */
4226
4227 static bool
4228 i386_in_indirect_branch_thunk (struct gdbarch *gdbarch, CORE_ADDR pc)
4229 {
4230 return x86_in_indirect_branch_thunk (pc, i386_register_names,
4231 I386_EAX_REGNUM, I386_EIP_REGNUM);
4232 }
4233
4234 /* Generic ELF. */
4235
4236 void
4237 i386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
4238 {
4239 static const char *const stap_integer_prefixes[] = { "$", NULL };
4240 static const char *const stap_register_prefixes[] = { "%", NULL };
4241 static const char *const stap_register_indirection_prefixes[] = { "(",
4242 NULL };
4243 static const char *const stap_register_indirection_suffixes[] = { ")",
4244 NULL };
4245
4246 /* We typically use stabs-in-ELF with the SVR4 register numbering. */
4247 set_gdbarch_stab_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
4248
4249 /* Registering SystemTap handlers. */
4250 set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
4251 set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
4252 set_gdbarch_stap_register_indirection_prefixes (gdbarch,
4253 stap_register_indirection_prefixes);
4254 set_gdbarch_stap_register_indirection_suffixes (gdbarch,
4255 stap_register_indirection_suffixes);
4256 set_gdbarch_stap_is_single_operand (gdbarch,
4257 i386_stap_is_single_operand);
4258 set_gdbarch_stap_parse_special_token (gdbarch,
4259 i386_stap_parse_special_token);
4260 set_gdbarch_stap_adjust_register (gdbarch,
4261 i386_stap_adjust_register);
4262
4263 set_gdbarch_in_indirect_branch_thunk (gdbarch,
4264 i386_in_indirect_branch_thunk);
4265 }
4266
4267 /* System V Release 4 (SVR4). */
4268
4269 void
4270 i386_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
4271 {
4272 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
4273
4274 /* System V Release 4 uses ELF. */
4275 i386_elf_init_abi (info, gdbarch);
4276
4277 /* System V Release 4 has shared libraries. */
4278 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
4279
4280 tdep->sigtramp_p = i386_svr4_sigtramp_p;
4281 tdep->sigcontext_addr = i386_svr4_sigcontext_addr;
4282 tdep->sc_pc_offset = 36 + 14 * 4;
4283 tdep->sc_sp_offset = 36 + 17 * 4;
4284
4285 tdep->jb_pc_offset = 20;
4286 }
4287
4288 \f
4289
4290 /* i386 register groups. In addition to the normal groups, add "mmx"
4291 and "sse". */
4292
4293 static const reggroup *i386_sse_reggroup;
4294 static const reggroup *i386_mmx_reggroup;
4295
4296 static void
4297 i386_init_reggroups (void)
4298 {
4299 i386_sse_reggroup = reggroup_new ("sse", USER_REGGROUP);
4300 i386_mmx_reggroup = reggroup_new ("mmx", USER_REGGROUP);
4301 }
4302
4303 static void
4304 i386_add_reggroups (struct gdbarch *gdbarch)
4305 {
4306 reggroup_add (gdbarch, i386_sse_reggroup);
4307 reggroup_add (gdbarch, i386_mmx_reggroup);
4308 }
4309
4310 int
4311 i386_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
4312 const struct reggroup *group)
4313 {
4314 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
4315 int fp_regnum_p, mmx_regnum_p, xmm_regnum_p, mxcsr_regnum_p,
4316 ymm_regnum_p, ymmh_regnum_p, ymm_avx512_regnum_p, ymmh_avx512_regnum_p,
4317 zmm_regnum_p, zmmh_regnum_p, xmm_avx512_regnum_p, avx512_p, avx_p,
4318 sse_p, pkru_regnum_p;
4319
4320 /* Don't include pseudo registers, except for MMX, in any register
4321 groups. */
4322 if (i386_byte_regnum_p (gdbarch, regnum))
4323 return 0;
4324
4325 if (i386_word_regnum_p (gdbarch, regnum))
4326 return 0;
4327
4328 if (i386_dword_regnum_p (gdbarch, regnum))
4329 return 0;
4330
4331 mmx_regnum_p = i386_mmx_regnum_p (gdbarch, regnum);
4332 if (group == i386_mmx_reggroup)
4333 return mmx_regnum_p;
4334
4335 pkru_regnum_p = i386_pkru_regnum_p(gdbarch, regnum);
4336 xmm_regnum_p = i386_xmm_regnum_p (gdbarch, regnum);
4337 xmm_avx512_regnum_p = i386_xmm_avx512_regnum_p (gdbarch, regnum);
4338 mxcsr_regnum_p = i386_mxcsr_regnum_p (gdbarch, regnum);
4339 if (group == i386_sse_reggroup)
4340 return xmm_regnum_p || xmm_avx512_regnum_p || mxcsr_regnum_p;
4341
4342 ymm_regnum_p = i386_ymm_regnum_p (gdbarch, regnum);
4343 ymm_avx512_regnum_p = i386_ymm_avx512_regnum_p (gdbarch, regnum);
4344 zmm_regnum_p = i386_zmm_regnum_p (gdbarch, regnum);
4345
4346 avx512_p = ((tdep->xcr0 & X86_XSTATE_AVX_AVX512_MASK)
4347 == X86_XSTATE_AVX_AVX512_MASK);
4348 avx_p = ((tdep->xcr0 & X86_XSTATE_AVX_AVX512_MASK)
4349 == X86_XSTATE_AVX_MASK) && !avx512_p;
4350 sse_p = ((tdep->xcr0 & X86_XSTATE_AVX_AVX512_MASK)
4351 == X86_XSTATE_SSE_MASK) && !avx512_p && ! avx_p;
4352
4353 if (group == vector_reggroup)
4354 return (mmx_regnum_p
4355 || (zmm_regnum_p && avx512_p)
4356 || ((ymm_regnum_p || ymm_avx512_regnum_p) && avx_p)
4357 || ((xmm_regnum_p || xmm_avx512_regnum_p) && sse_p)
4358 || mxcsr_regnum_p);
4359
4360 fp_regnum_p = (i386_fp_regnum_p (gdbarch, regnum)
4361 || i386_fpc_regnum_p (gdbarch, regnum));
4362 if (group == float_reggroup)
4363 return fp_regnum_p;
4364
4365 /* For "info reg all", don't include upper YMM registers nor XMM
4366 registers when AVX is supported. */
4367 ymmh_regnum_p = i386_ymmh_regnum_p (gdbarch, regnum);
4368 ymmh_avx512_regnum_p = i386_ymmh_avx512_regnum_p (gdbarch, regnum);
4369 zmmh_regnum_p = i386_zmmh_regnum_p (gdbarch, regnum);
4370 if (group == all_reggroup
4371 && (((xmm_regnum_p || xmm_avx512_regnum_p) && !sse_p)
4372 || ((ymm_regnum_p || ymm_avx512_regnum_p) && !avx_p)
4373 || ymmh_regnum_p
4374 || ymmh_avx512_regnum_p
4375 || zmmh_regnum_p))
4376 return 0;
4377
4378 if (group == general_reggroup)
4379 return (!fp_regnum_p
4380 && !mmx_regnum_p
4381 && !mxcsr_regnum_p
4382 && !xmm_regnum_p
4383 && !xmm_avx512_regnum_p
4384 && !ymm_regnum_p
4385 && !ymmh_regnum_p
4386 && !ymm_avx512_regnum_p
4387 && !ymmh_avx512_regnum_p
4388 && !zmm_regnum_p
4389 && !zmmh_regnum_p
4390 && !pkru_regnum_p);
4391
4392 return default_register_reggroup_p (gdbarch, regnum, group);
4393 }
4394 \f
4395
4396 /* Get the ARGIth function argument for the current function. */
4397
4398 static CORE_ADDR
4399 i386_fetch_pointer_argument (const frame_info_ptr &frame, int argi,
4400 struct type *type)
4401 {
4402 struct gdbarch *gdbarch = get_frame_arch (frame);
4403 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4404 CORE_ADDR sp = get_frame_register_unsigned (frame, I386_ESP_REGNUM);
4405 return read_memory_unsigned_integer (sp + (4 * (argi + 1)), 4, byte_order);
4406 }
4407
4408 #define PREFIX_REPZ 0x01
4409 #define PREFIX_REPNZ 0x02
4410 #define PREFIX_LOCK 0x04
4411 #define PREFIX_DATA 0x08
4412 #define PREFIX_ADDR 0x10
4413
4414 /* operand size */
4415 enum
4416 {
4417 OT_BYTE = 0,
4418 OT_WORD,
4419 OT_LONG,
4420 OT_QUAD,
4421 OT_DQUAD,
4422 };
4423
4424 /* i386 arith/logic operations */
4425 enum
4426 {
4427 OP_ADDL,
4428 OP_ORL,
4429 OP_ADCL,
4430 OP_SBBL,
4431 OP_ANDL,
4432 OP_SUBL,
4433 OP_XORL,
4434 OP_CMPL,
4435 };
4436
4437 struct i386_record_s
4438 {
4439 struct gdbarch *gdbarch;
4440 struct regcache *regcache;
4441 CORE_ADDR orig_addr;
4442 CORE_ADDR addr;
4443 int aflag;
4444 int dflag;
4445 int override;
4446 uint8_t modrm;
4447 uint8_t mod, reg, rm;
4448 int ot;
4449 uint8_t rex_x;
4450 uint8_t rex_b;
4451 int rip_offset;
4452 int popl_esp_hack;
4453 const int *regmap;
4454
4455 /* These are used by VEX and XOP prefixes. */
4456 uint8_t map_select;
4457 uint8_t vvvv;
4458 uint8_t pp;
4459 uint8_t l;
4460 };
4461
4462 /* Parse the "modrm" part of the memory address irp->addr points at.
4463 Returns -1 if something goes wrong, 0 otherwise. */
4464
4465 static int
4466 i386_record_modrm (struct i386_record_s *irp)
4467 {
4468 struct gdbarch *gdbarch = irp->gdbarch;
4469
4470 if (record_read_memory (gdbarch, irp->addr, &irp->modrm, 1))
4471 return -1;
4472
4473 irp->addr++;
4474 irp->mod = (irp->modrm >> 6) & 3;
4475 irp->reg = (irp->modrm >> 3) & 7;
4476 irp->rm = irp->modrm & 7;
4477
4478 return 0;
4479 }
4480
4481 /* Extract the memory address that the current instruction writes to,
4482 and return it in *ADDR. Return -1 if something goes wrong. */
4483
4484 static int
4485 i386_record_lea_modrm_addr (struct i386_record_s *irp, uint64_t *addr)
4486 {
4487 struct gdbarch *gdbarch = irp->gdbarch;
4488 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4489 gdb_byte buf[4];
4490 ULONGEST offset64;
4491
4492 *addr = 0;
4493 if (irp->aflag || irp->regmap[X86_RECORD_R8_REGNUM])
4494 {
4495 /* 32/64 bits */
4496 int havesib = 0;
4497 uint8_t scale = 0;
4498 uint8_t byte;
4499 uint8_t index = 0;
4500 uint8_t base = irp->rm;
4501
4502 if (base == 4)
4503 {
4504 havesib = 1;
4505 if (record_read_memory (gdbarch, irp->addr, &byte, 1))
4506 return -1;
4507 irp->addr++;
4508 scale = (byte >> 6) & 3;
4509 index = ((byte >> 3) & 7) | irp->rex_x;
4510 base = (byte & 7);
4511 }
4512 base |= irp->rex_b;
4513
4514 switch (irp->mod)
4515 {
4516 case 0:
4517 if ((base & 7) == 5)
4518 {
4519 base = 0xff;
4520 if (record_read_memory (gdbarch, irp->addr, buf, 4))
4521 return -1;
4522 irp->addr += 4;
4523 *addr = extract_signed_integer (buf, 4, byte_order);
4524 if (irp->regmap[X86_RECORD_R8_REGNUM] && !havesib)
4525 *addr += irp->addr + irp->rip_offset;
4526 }
4527 break;
4528 case 1:
4529 if (record_read_memory (gdbarch, irp->addr, buf, 1))
4530 return -1;
4531 irp->addr++;
4532 *addr = (int8_t) buf[0];
4533 break;
4534 case 2:
4535 if (record_read_memory (gdbarch, irp->addr, buf, 4))
4536 return -1;
4537 *addr = extract_signed_integer (buf, 4, byte_order);
4538 irp->addr += 4;
4539 break;
4540 }
4541
4542 offset64 = 0;
4543 if (base != 0xff)
4544 {
4545 if (base == 4 && irp->popl_esp_hack)
4546 *addr += irp->popl_esp_hack;
4547 regcache_raw_read_unsigned (irp->regcache, irp->regmap[base],
4548 &offset64);
4549 }
4550 if (irp->aflag == 2)
4551 {
4552 *addr += offset64;
4553 }
4554 else
4555 *addr = (uint32_t) (offset64 + *addr);
4556
4557 if (havesib && (index != 4 || scale != 0))
4558 {
4559 regcache_raw_read_unsigned (irp->regcache, irp->regmap[index],
4560 &offset64);
4561 if (irp->aflag == 2)
4562 *addr += offset64 << scale;
4563 else
4564 *addr = (uint32_t) (*addr + (offset64 << scale));
4565 }
4566
4567 if (!irp->aflag)
4568 {
4569 /* Since we are in 64-bit mode with ADDR32 prefix, zero-extend
4570 address from 32-bit to 64-bit. */
4571 *addr = (uint32_t) *addr;
4572 }
4573 }
4574 else
4575 {
4576 /* 16 bits */
4577 switch (irp->mod)
4578 {
4579 case 0:
4580 if (irp->rm == 6)
4581 {
4582 if (record_read_memory (gdbarch, irp->addr, buf, 2))
4583 return -1;
4584 irp->addr += 2;
4585 *addr = extract_signed_integer (buf, 2, byte_order);
4586 irp->rm = 0;
4587 goto no_rm;
4588 }
4589 break;
4590 case 1:
4591 if (record_read_memory (gdbarch, irp->addr, buf, 1))
4592 return -1;
4593 irp->addr++;
4594 *addr = (int8_t) buf[0];
4595 break;
4596 case 2:
4597 if (record_read_memory (gdbarch, irp->addr, buf, 2))
4598 return -1;
4599 irp->addr += 2;
4600 *addr = extract_signed_integer (buf, 2, byte_order);
4601 break;
4602 }
4603
4604 switch (irp->rm)
4605 {
4606 case 0:
4607 regcache_raw_read_unsigned (irp->regcache,
4608 irp->regmap[X86_RECORD_REBX_REGNUM],
4609 &offset64);
4610 *addr = (uint32_t) (*addr + offset64);
4611 regcache_raw_read_unsigned (irp->regcache,
4612 irp->regmap[X86_RECORD_RESI_REGNUM],
4613 &offset64);
4614 *addr = (uint32_t) (*addr + offset64);
4615 break;
4616 case 1:
4617 regcache_raw_read_unsigned (irp->regcache,
4618 irp->regmap[X86_RECORD_REBX_REGNUM],
4619 &offset64);
4620 *addr = (uint32_t) (*addr + offset64);
4621 regcache_raw_read_unsigned (irp->regcache,
4622 irp->regmap[X86_RECORD_REDI_REGNUM],
4623 &offset64);
4624 *addr = (uint32_t) (*addr + offset64);
4625 break;
4626 case 2:
4627 regcache_raw_read_unsigned (irp->regcache,
4628 irp->regmap[X86_RECORD_REBP_REGNUM],
4629 &offset64);
4630 *addr = (uint32_t) (*addr + offset64);
4631 regcache_raw_read_unsigned (irp->regcache,
4632 irp->regmap[X86_RECORD_RESI_REGNUM],
4633 &offset64);
4634 *addr = (uint32_t) (*addr + offset64);
4635 break;
4636 case 3:
4637 regcache_raw_read_unsigned (irp->regcache,
4638 irp->regmap[X86_RECORD_REBP_REGNUM],
4639 &offset64);
4640 *addr = (uint32_t) (*addr + offset64);
4641 regcache_raw_read_unsigned (irp->regcache,
4642 irp->regmap[X86_RECORD_REDI_REGNUM],
4643 &offset64);
4644 *addr = (uint32_t) (*addr + offset64);
4645 break;
4646 case 4:
4647 regcache_raw_read_unsigned (irp->regcache,
4648 irp->regmap[X86_RECORD_RESI_REGNUM],
4649 &offset64);
4650 *addr = (uint32_t) (*addr + offset64);
4651 break;
4652 case 5:
4653 regcache_raw_read_unsigned (irp->regcache,
4654 irp->regmap[X86_RECORD_REDI_REGNUM],
4655 &offset64);
4656 *addr = (uint32_t) (*addr + offset64);
4657 break;
4658 case 6:
4659 regcache_raw_read_unsigned (irp->regcache,
4660 irp->regmap[X86_RECORD_REBP_REGNUM],
4661 &offset64);
4662 *addr = (uint32_t) (*addr + offset64);
4663 break;
4664 case 7:
4665 regcache_raw_read_unsigned (irp->regcache,
4666 irp->regmap[X86_RECORD_REBX_REGNUM],
4667 &offset64);
4668 *addr = (uint32_t) (*addr + offset64);
4669 break;
4670 }
4671 *addr &= 0xffff;
4672 }
4673
4674 no_rm:
4675 return 0;
4676 }
4677
4678 /* Record the address and contents of the memory that will be changed
4679 by the current instruction. Return -1 if something goes wrong, 0
4680 otherwise. */
4681
4682 static int
4683 i386_record_lea_modrm (struct i386_record_s *irp)
4684 {
4685 struct gdbarch *gdbarch = irp->gdbarch;
4686 uint64_t addr;
4687
4688 if (irp->override >= 0)
4689 {
4690 if (record_full_memory_query)
4691 {
4692 if (yquery (_("\
4693 Process record ignores the memory change of instruction at address %s\n\
4694 because it can't get the value of the segment register.\n\
4695 Do you want to stop the program?"),
4696 paddress (gdbarch, irp->orig_addr)))
4697 return -1;
4698 }
4699
4700 return 0;
4701 }
4702
4703 if (i386_record_lea_modrm_addr (irp, &addr))
4704 return -1;
4705
4706 if (record_full_arch_list_add_mem (addr, 1 << irp->ot))
4707 return -1;
4708
4709 return 0;
4710 }
4711
4712 /* Record the effects of a push operation. Return -1 if something
4713 goes wrong, 0 otherwise. */
4714
4715 static int
4716 i386_record_push (struct i386_record_s *irp, int size)
4717 {
4718 ULONGEST addr;
4719
4720 if (record_full_arch_list_add_reg (irp->regcache,
4721 irp->regmap[X86_RECORD_RESP_REGNUM]))
4722 return -1;
4723 regcache_raw_read_unsigned (irp->regcache,
4724 irp->regmap[X86_RECORD_RESP_REGNUM],
4725 &addr);
4726 if (record_full_arch_list_add_mem ((CORE_ADDR) addr - size, size))
4727 return -1;
4728
4729 return 0;
4730 }
4731
4732
4733 /* Defines contents to record. */
4734 #define I386_SAVE_FPU_REGS 0xfffd
4735 #define I386_SAVE_FPU_ENV 0xfffe
4736 #define I386_SAVE_FPU_ENV_REG_STACK 0xffff
4737
4738 /* Record the values of the floating point registers which will be
4739 changed by the current instruction. Returns -1 if something is
4740 wrong, 0 otherwise. */
4741
4742 static int i386_record_floats (struct gdbarch *gdbarch,
4743 struct i386_record_s *ir,
4744 uint32_t iregnum)
4745 {
4746 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
4747 int i;
4748
4749 /* Oza: Because of floating point insn push/pop of fpu stack is going to
4750 happen. Currently we store st0-st7 registers, but we need not store all
4751 registers all the time, in future we use ftag register and record only
4752 those who are not marked as an empty. */
4753
4754 if (I386_SAVE_FPU_REGS == iregnum)
4755 {
4756 for (i = I387_ST0_REGNUM (tdep); i <= I387_ST0_REGNUM (tdep) + 7; i++)
4757 {
4758 if (record_full_arch_list_add_reg (ir->regcache, i))
4759 return -1;
4760 }
4761 }
4762 else if (I386_SAVE_FPU_ENV == iregnum)
4763 {
4764 for (i = I387_FCTRL_REGNUM (tdep); i <= I387_FOP_REGNUM (tdep); i++)
4765 {
4766 if (record_full_arch_list_add_reg (ir->regcache, i))
4767 return -1;
4768 }
4769 }
4770 else if (I386_SAVE_FPU_ENV_REG_STACK == iregnum)
4771 {
4772 for (i = I387_ST0_REGNUM (tdep); i <= I387_FOP_REGNUM (tdep); i++)
4773 if (record_full_arch_list_add_reg (ir->regcache, i))
4774 return -1;
4775 }
4776 else if ((iregnum >= I387_ST0_REGNUM (tdep)) &&
4777 (iregnum <= I387_FOP_REGNUM (tdep)))
4778 {
4779 if (record_full_arch_list_add_reg (ir->regcache,iregnum))
4780 return -1;
4781 }
4782 else
4783 {
4784 /* Parameter error. */
4785 return -1;
4786 }
4787 if(I386_SAVE_FPU_ENV != iregnum)
4788 {
4789 for (i = I387_FCTRL_REGNUM (tdep); i <= I387_FOP_REGNUM (tdep); i++)
4790 {
4791 if (record_full_arch_list_add_reg (ir->regcache, i))
4792 return -1;
4793 }
4794 }
4795 return 0;
4796 }
4797
4798 /* i386_process_record helper to deal with instructions that start
4799 with VEX prefix. */
4800
4801 static int
4802 i386_record_vex (struct i386_record_s *ir, uint8_t vex_w, uint8_t vex_r,
4803 int opcode, struct gdbarch *gdbarch)
4804 {
4805 /* We need this to find YMM (and once AVX-512 is supported, ZMM) registers.
4806 We should always save the largest available register, since an
4807 instruction that handles a smaller reg may zero out the higher bits,
4808 so we must have them saved. */
4809 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
4810
4811 /* Since we are reading pseudo registers, we need to tell GDB that it is
4812 safe to do so, by saying we aren't _really_ running the inferior right
4813 now. */
4814 SCOPE_EXIT { inferior_thread ()->set_executing (true); };
4815 inferior_thread () -> set_executing (false);
4816
4817 switch (opcode)
4818 {
4819 case 0x6e: /* VMOVD XMM, reg/mem */
4820 /* This is moving from a regular register or memory region into an
4821 XMM register. */
4822 i386_record_modrm (ir);
4823 /* ModR/M only has the 3 least significant bits of the destination
4824 register, the last one is indicated by VEX.R (stored inverted). */
4825 record_full_arch_list_add_reg (ir->regcache,
4826 tdep->ymm0_regnum
4827 + ir->reg + vex_r * 8);
4828 break;
4829 case 0x7e: /* VMOV(D/Q) */
4830 i386_record_modrm (ir);
4831 /* Both the intel and AMD manual are wrong about this. According to
4832 it, the only difference between vmovq and vmovd should be the rex_w
4833 bit, but in empirical testing, it seems that they share this opcode,
4834 and the way to differentiate it here is looking at VEX.PP. */
4835 if (ir->pp == 2)
4836 {
4837 /* This is vmovq moving from a regular register or memory
4838 into an XMM register. As above, VEX.R is the final bit for
4839 destination register. */
4840 record_full_arch_list_add_reg (ir->regcache,
4841 tdep->ymm0_regnum
4842 + ir->reg + vex_r * 8);
4843 }
4844 else if (ir->pp == 1)
4845 {
4846 /* This is the vmovd version that stores into a regular register
4847 or memory region. */
4848 /* If ModRM.mod is 11 we are saving into a register. */
4849 if (ir->mod == 3)
4850 record_full_arch_list_add_reg (ir->regcache, ir->regmap[ir->rm]);
4851 else
4852 {
4853 /* Calculate the size of memory that will be modified
4854 and store it in the form of 1 << ir->ot, since that
4855 is how the function uses it. In theory, VEX.W is supposed
4856 to indicate the size of the memory. In practice, I only
4857 ever seen it set to 0, and for 16 bytes, 0xD6 opcode
4858 is used. */
4859 if (vex_w)
4860 ir->ot = 4;
4861 else
4862 ir->ot = 3;
4863
4864 i386_record_lea_modrm (ir);
4865 }
4866 }
4867 else
4868 {
4869 gdb_printf ("Unrecognized VEX.PP value %d at address %s.",
4870 ir->pp, paddress(gdbarch, ir->orig_addr));
4871 return -1;
4872 }
4873 break;
4874 case 0xd6: /* VMOVQ reg/mem XMM */
4875 i386_record_modrm (ir);
4876 /* This is the vmovq version that stores into a regular register
4877 or memory region. */
4878 /* If ModRM.mod is 11 we are saving into a register. */
4879 if (ir->mod == 3)
4880 record_full_arch_list_add_reg (ir->regcache, ir->regmap[ir->rm]);
4881 else
4882 {
4883 /* We know that this operation is always 64 bits. */
4884 ir->ot = 4;
4885 i386_record_lea_modrm (ir);
4886 }
4887 break;
4888
4889 case 0x6f: /* VMOVDQ (U|A) */
4890 case 0x7f: /* VMOVDQ (U|A) */
4891 /* vmovdq instructions have information about source/destination
4892 spread over many places, so this code ended up messier than
4893 I'd like. */
4894 /* The VEX.pp bits identify if the move is aligned or not, but this
4895 doesn't influence the recording so we can ignore it. */
4896 i386_record_modrm (ir);
4897 /* The first bit of modrm identifies if both operands of the instruction
4898 are registers (bit = 1) or if one of the operands is memory. */
4899 if (ir->mod & 2)
4900 {
4901 if (opcode == 0x6f)
4902 {
4903 /* vex_r will identify the high bit of the destination
4904 register. Source is identified by ir->rex_b, but that
4905 doesn't matter for recording. */
4906 record_full_arch_list_add_reg (ir->regcache,
4907 tdep->ymm0_regnum + 8*vex_r + ir->reg);
4908 }
4909 else
4910 {
4911 /* The origin operand is >7 and destination operand is <= 7.
4912 This is special cased because in this one vex_r is used to
4913 identify the high bit of the SOURCE operand, not destination
4914 which would mess the previous expression. */
4915 record_full_arch_list_add_reg (ir->regcache,
4916 tdep->ymm0_regnum + ir->rm);
4917 }
4918 }
4919 else
4920 {
4921 /* This is the easy branch. We just need to check the opcode
4922 to see if the source or destination is memory. */
4923 if (opcode == 0x6f)
4924 {
4925 record_full_arch_list_add_reg (ir->regcache,
4926 tdep->ymm0_regnum
4927 + ir->reg + vex_r * 8);
4928 }
4929 else
4930 {
4931 /* We're writing 256 bits, so 1<<8. */
4932 ir->ot = 8;
4933 i386_record_lea_modrm (ir);
4934 }
4935 }
4936 break;
4937
4938 case 0x60: /* VPUNPCKLBW */
4939 case 0x61: /* VPUNPCKLWD */
4940 case 0x62: /* VPUNPCKLDQ */
4941 case 0x6c: /* VPUNPCKLQDQ */
4942 case 0x68: /* VPUNPCKHBW */
4943 case 0x69: /* VPUNPCKHWD */
4944 case 0x6a: /* VPUNPCKHDQ */
4945 case 0x6d: /* VPUNPCKHQDQ */
4946 {
4947 i386_record_modrm (ir);
4948 int reg_offset = ir->reg + vex_r * 8;
4949 record_full_arch_list_add_reg (ir->regcache,
4950 tdep->ymm0_regnum + reg_offset);
4951 }
4952 break;
4953
4954 case 0x74: /* VPCMPEQB */
4955 case 0x75: /* VPCMPEQB */
4956 case 0x76: /* VPCMPEQB */
4957 {
4958 i386_record_modrm (ir);
4959 int reg_offset = ir->reg + vex_r * 8;
4960 record_full_arch_list_add_reg (ir->regcache,
4961 tdep->ymm0_regnum + reg_offset);
4962 }
4963 break;
4964
4965 case 0x78: /* VPBROADCASTB */
4966 case 0x79: /* VPBROADCASTW */
4967 case 0x58: /* VPBROADCASTD */
4968 case 0x59: /* VPBROADCASTQ */
4969 {
4970 i386_record_modrm (ir);
4971 int reg_offset = ir->reg + vex_r * 8;
4972 gdb_assert (tdep->num_ymm_regs > reg_offset);
4973 record_full_arch_list_add_reg (ir->regcache,
4974 tdep->ymm0_regnum + reg_offset);
4975 }
4976 break;
4977
4978 case 0x77:/* VZEROUPPER */
4979 {
4980 int num_regs = tdep->num_ymm_regs;
4981 /* This instruction only works on ymm0..15, even if 16..31 are
4982 available. */
4983 if (num_regs > 16)
4984 num_regs = 16;
4985 for (int i = 0; i < num_regs; i++)
4986 {
4987 /* We only need to record ymm_h, because the low bits
4988 are not touched. */
4989 record_full_arch_list_add_reg (ir->regcache,
4990 tdep->ymm0h_regnum + i);
4991 }
4992 break;
4993 }
4994
4995 case 0xd7: /* VPMOVMSKB */
4996 {
4997 i386_record_modrm (ir);
4998 record_full_arch_list_add_reg (ir->regcache,
4999 ir->regmap[X86_RECORD_REAX_REGNUM
5000 + ir->reg + 8 * vex_r]);
5001 }
5002 break;
5003
5004 case 0xef: /* VPXOR */
5005 case 0xeb: /* VPOR */
5006 {
5007 i386_record_modrm (ir);
5008 int reg_offset = ir->reg + vex_r * 8;
5009 record_full_arch_list_add_reg (ir->regcache,
5010 tdep->ymm0_regnum + reg_offset);
5011 break;
5012 }
5013
5014 default:
5015 gdb_printf (gdb_stderr,
5016 _("Process record does not support VEX instruction 0x%02x "
5017 "at address %s.\n"),
5018 (unsigned int) (opcode),
5019 paddress (gdbarch, ir->orig_addr));
5020 return -1;
5021 }
5022
5023 record_full_arch_list_add_reg (ir->regcache, ir->regmap[X86_RECORD_REIP_REGNUM]);
5024 if (record_full_arch_list_add_end ())
5025 return -1;
5026
5027 return 0;
5028 }
5029
5030 /* Parse the current instruction, and record the values of the
5031 registers and memory that will be changed by the current
5032 instruction. Returns -1 if something goes wrong, 0 otherwise. */
5033
5034 #define I386_RECORD_FULL_ARCH_LIST_ADD_REG(regnum) \
5035 record_full_arch_list_add_reg (ir.regcache, ir.regmap[(regnum)])
5036
5037 int
5038 i386_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
5039 CORE_ADDR input_addr)
5040 {
5041 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5042 int prefixes = 0;
5043 int regnum = 0;
5044 uint32_t opcode;
5045 uint8_t opcode8;
5046 ULONGEST addr;
5047 gdb_byte buf[I386_MAX_REGISTER_SIZE];
5048 struct i386_record_s ir;
5049 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
5050 uint8_t rex_w = -1;
5051 uint8_t rex_r = 0;
5052 bool vex_prefix = false;
5053
5054 memset (&ir, 0, sizeof (struct i386_record_s));
5055 ir.regcache = regcache;
5056 ir.addr = input_addr;
5057 ir.orig_addr = input_addr;
5058 ir.aflag = 1;
5059 ir.dflag = 1;
5060 ir.override = -1;
5061 ir.popl_esp_hack = 0;
5062 ir.regmap = tdep->record_regmap;
5063 ir.gdbarch = gdbarch;
5064
5065 if (record_debug > 1)
5066 gdb_printf (gdb_stdlog, "Process record: i386_process_record "
5067 "addr = %s\n",
5068 paddress (gdbarch, ir.addr));
5069
5070 /* prefixes */
5071 while (1)
5072 {
5073 if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
5074 return -1;
5075 ir.addr++;
5076 switch (opcode8) /* Instruction prefixes */
5077 {
5078 case REPE_PREFIX_OPCODE:
5079 prefixes |= PREFIX_REPZ;
5080 break;
5081 case REPNE_PREFIX_OPCODE:
5082 prefixes |= PREFIX_REPNZ;
5083 break;
5084 case LOCK_PREFIX_OPCODE:
5085 prefixes |= PREFIX_LOCK;
5086 break;
5087 case CS_PREFIX_OPCODE:
5088 ir.override = X86_RECORD_CS_REGNUM;
5089 break;
5090 case SS_PREFIX_OPCODE:
5091 ir.override = X86_RECORD_SS_REGNUM;
5092 break;
5093 case DS_PREFIX_OPCODE:
5094 ir.override = X86_RECORD_DS_REGNUM;
5095 break;
5096 case ES_PREFIX_OPCODE:
5097 ir.override = X86_RECORD_ES_REGNUM;
5098 break;
5099 case FS_PREFIX_OPCODE:
5100 ir.override = X86_RECORD_FS_REGNUM;
5101 break;
5102 case GS_PREFIX_OPCODE:
5103 ir.override = X86_RECORD_GS_REGNUM;
5104 break;
5105 case DATA_PREFIX_OPCODE:
5106 prefixes |= PREFIX_DATA;
5107 break;
5108 case ADDR_PREFIX_OPCODE:
5109 prefixes |= PREFIX_ADDR;
5110 break;
5111 case 0x40: /* i386 inc %eax */
5112 case 0x41: /* i386 inc %ecx */
5113 case 0x42: /* i386 inc %edx */
5114 case 0x43: /* i386 inc %ebx */
5115 case 0x44: /* i386 inc %esp */
5116 case 0x45: /* i386 inc %ebp */
5117 case 0x46: /* i386 inc %esi */
5118 case 0x47: /* i386 inc %edi */
5119 case 0x48: /* i386 dec %eax */
5120 case 0x49: /* i386 dec %ecx */
5121 case 0x4a: /* i386 dec %edx */
5122 case 0x4b: /* i386 dec %ebx */
5123 case 0x4c: /* i386 dec %esp */
5124 case 0x4d: /* i386 dec %ebp */
5125 case 0x4e: /* i386 dec %esi */
5126 case 0x4f: /* i386 dec %edi */
5127 if (ir.regmap[X86_RECORD_R8_REGNUM]) /* 64 bit target */
5128 {
5129 /* REX */
5130 rex_w = (opcode8 >> 3) & 1;
5131 rex_r = (opcode8 & 0x4) << 1;
5132 ir.rex_x = (opcode8 & 0x2) << 2;
5133 ir.rex_b = (opcode8 & 0x1) << 3;
5134 }
5135 else /* 32 bit target */
5136 goto out_prefixes;
5137 break;
5138 case 0xc4: /* 3-byte VEX prefixes (for AVX/AVX2 instructions). */
5139 {
5140 /* The first byte just identifies the VEX prefix. Data is stored
5141 on the following 2 bytes. */
5142 uint8_t byte;
5143 if (record_read_memory (gdbarch, ir.addr, &byte, 1))
5144 return -1;
5145 ir.addr++;
5146
5147 rex_r = !((byte >> 7) & 0x1);
5148 ir.rex_x = !((byte >> 6) & 0x1);
5149 ir.rex_b = !((byte >> 5) & 0x1);
5150 ir.map_select = byte & 0x1f;
5151 /* Collect the last byte of the prefix. */
5152 if (record_read_memory (gdbarch, ir.addr, &byte, 1))
5153 return -1;
5154 ir.addr++;
5155 rex_w = (byte >> 7) & 0x1;
5156 ir.vvvv = (~(byte >> 3) & 0xf);
5157 ir.l = (byte >> 2) & 0x1;
5158 ir.pp = byte & 0x3;
5159 vex_prefix = true;
5160
5161 break;
5162 }
5163 case 0xc5: /* 2-byte VEX prefix for AVX/AVX2 instructions. */
5164 {
5165 /* The first byte just identifies the VEX prefix. Data is stored
5166 on the following 2 bytes. */
5167 uint8_t byte;
5168 if (record_read_memory (gdbarch, ir.addr, &byte, 1))
5169 return -1;
5170 ir.addr++;
5171
5172 /* On the 2-byte versions, these are pre-defined. */
5173 ir.rex_x = 0;
5174 ir.rex_b = 0;
5175 rex_w = 0;
5176 ir.map_select = 1;
5177
5178 rex_r = !((byte >> 7) & 0x1);
5179 ir.vvvv = (~(byte >> 3) & 0xf);
5180 ir.l = (byte >> 2) & 0x1;
5181 ir.pp = byte & 0x3;
5182 vex_prefix = true;
5183 break;
5184 }
5185 default:
5186 goto out_prefixes;
5187 break;
5188 }
5189 }
5190 out_prefixes:
5191 if (ir.regmap[X86_RECORD_R8_REGNUM] && rex_w == 1)
5192 {
5193 ir.dflag = 2;
5194 }
5195 else
5196 {
5197 if (prefixes & PREFIX_DATA)
5198 ir.dflag ^= 1;
5199 }
5200 if (prefixes & PREFIX_ADDR)
5201 ir.aflag ^= 1;
5202 else if (ir.regmap[X86_RECORD_R8_REGNUM])
5203 ir.aflag = 2;
5204
5205 /* Now check op code. */
5206 opcode = (uint32_t) opcode8;
5207 if (vex_prefix)
5208 {
5209 /* If we found the VEX prefix, i386 will either record or warn that
5210 the instruction isn't supported, so we can return the VEX result. */
5211 return i386_record_vex (&ir, rex_w, rex_r, opcode, gdbarch);
5212 }
5213 reswitch:
5214 switch (opcode)
5215 {
5216 case 0x0f:
5217 if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
5218 return -1;
5219 ir.addr++;
5220 opcode = (uint32_t) opcode8 | 0x0f00;
5221 goto reswitch;
5222 break;
5223
5224 case 0x00: /* arith & logic */
5225 case 0x01:
5226 case 0x02:
5227 case 0x03:
5228 case 0x04:
5229 case 0x05:
5230 case 0x08:
5231 case 0x09:
5232 case 0x0a:
5233 case 0x0b:
5234 case 0x0c:
5235 case 0x0d:
5236 case 0x10:
5237 case 0x11:
5238 case 0x12:
5239 case 0x13:
5240 case 0x14:
5241 case 0x15:
5242 case 0x18:
5243 case 0x19:
5244 case 0x1a:
5245 case 0x1b:
5246 case 0x1c:
5247 case 0x1d:
5248 case 0x20:
5249 case 0x21:
5250 case 0x22:
5251 case 0x23:
5252 case 0x24:
5253 case 0x25:
5254 case 0x28:
5255 case 0x29:
5256 case 0x2a:
5257 case 0x2b:
5258 case 0x2c:
5259 case 0x2d:
5260 case 0x30:
5261 case 0x31:
5262 case 0x32:
5263 case 0x33:
5264 case 0x34:
5265 case 0x35:
5266 case 0x38:
5267 case 0x39:
5268 case 0x3a:
5269 case 0x3b:
5270 case 0x3c:
5271 case 0x3d:
5272 if (((opcode >> 3) & 7) != OP_CMPL)
5273 {
5274 if ((opcode & 1) == 0)
5275 ir.ot = OT_BYTE;
5276 else
5277 ir.ot = ir.dflag + OT_WORD;
5278
5279 switch ((opcode >> 1) & 3)
5280 {
5281 case 0: /* OP Ev, Gv */
5282 if (i386_record_modrm (&ir))
5283 return -1;
5284 if (ir.mod != 3)
5285 {
5286 if (i386_record_lea_modrm (&ir))
5287 return -1;
5288 }
5289 else
5290 {
5291 ir.rm |= ir.rex_b;
5292 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5293 ir.rm &= 0x3;
5294 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5295 }
5296 break;
5297 case 1: /* OP Gv, Ev */
5298 if (i386_record_modrm (&ir))
5299 return -1;
5300 ir.reg |= rex_r;
5301 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5302 ir.reg &= 0x3;
5303 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5304 break;
5305 case 2: /* OP A, Iv */
5306 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5307 break;
5308 }
5309 }
5310 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5311 break;
5312
5313 case 0x80: /* GRP1 */
5314 case 0x81:
5315 case 0x82:
5316 case 0x83:
5317 if (i386_record_modrm (&ir))
5318 return -1;
5319
5320 if (ir.reg != OP_CMPL)
5321 {
5322 if ((opcode & 1) == 0)
5323 ir.ot = OT_BYTE;
5324 else
5325 ir.ot = ir.dflag + OT_WORD;
5326
5327 if (ir.mod != 3)
5328 {
5329 if (opcode == 0x83)
5330 ir.rip_offset = 1;
5331 else
5332 ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
5333 if (i386_record_lea_modrm (&ir))
5334 return -1;
5335 }
5336 else
5337 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
5338 }
5339 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5340 break;
5341
5342 case 0x40: /* inc */
5343 case 0x41:
5344 case 0x42:
5345 case 0x43:
5346 case 0x44:
5347 case 0x45:
5348 case 0x46:
5349 case 0x47:
5350
5351 case 0x48: /* dec */
5352 case 0x49:
5353 case 0x4a:
5354 case 0x4b:
5355 case 0x4c:
5356 case 0x4d:
5357 case 0x4e:
5358 case 0x4f:
5359
5360 I386_RECORD_FULL_ARCH_LIST_ADD_REG (opcode & 7);
5361 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5362 break;
5363
5364 case 0xf6: /* GRP3 */
5365 case 0xf7:
5366 if ((opcode & 1) == 0)
5367 ir.ot = OT_BYTE;
5368 else
5369 ir.ot = ir.dflag + OT_WORD;
5370 if (i386_record_modrm (&ir))
5371 return -1;
5372
5373 if (ir.mod != 3 && ir.reg == 0)
5374 ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
5375
5376 switch (ir.reg)
5377 {
5378 case 0: /* test */
5379 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5380 break;
5381 case 2: /* not */
5382 case 3: /* neg */
5383 if (ir.mod != 3)
5384 {
5385 if (i386_record_lea_modrm (&ir))
5386 return -1;
5387 }
5388 else
5389 {
5390 ir.rm |= ir.rex_b;
5391 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5392 ir.rm &= 0x3;
5393 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5394 }
5395 if (ir.reg == 3) /* neg */
5396 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5397 break;
5398 case 4: /* mul */
5399 case 5: /* imul */
5400 case 6: /* div */
5401 case 7: /* idiv */
5402 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5403 if (ir.ot != OT_BYTE)
5404 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
5405 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5406 break;
5407 default:
5408 ir.addr -= 2;
5409 opcode = opcode << 8 | ir.modrm;
5410 goto no_support;
5411 break;
5412 }
5413 break;
5414
5415 case 0xfe: /* GRP4 */
5416 case 0xff: /* GRP5 */
5417 if (i386_record_modrm (&ir))
5418 return -1;
5419 if (ir.reg >= 2 && opcode == 0xfe)
5420 {
5421 ir.addr -= 2;
5422 opcode = opcode << 8 | ir.modrm;
5423 goto no_support;
5424 }
5425 switch (ir.reg)
5426 {
5427 case 0: /* inc */
5428 case 1: /* dec */
5429 if ((opcode & 1) == 0)
5430 ir.ot = OT_BYTE;
5431 else
5432 ir.ot = ir.dflag + OT_WORD;
5433 if (ir.mod != 3)
5434 {
5435 if (i386_record_lea_modrm (&ir))
5436 return -1;
5437 }
5438 else
5439 {
5440 ir.rm |= ir.rex_b;
5441 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5442 ir.rm &= 0x3;
5443 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5444 }
5445 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5446 break;
5447 case 2: /* call */
5448 if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
5449 ir.dflag = 2;
5450 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5451 return -1;
5452 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5453 break;
5454 case 3: /* lcall */
5455 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
5456 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5457 return -1;
5458 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5459 break;
5460 case 4: /* jmp */
5461 case 5: /* ljmp */
5462 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5463 break;
5464 case 6: /* push */
5465 if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
5466 ir.dflag = 2;
5467 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5468 return -1;
5469 break;
5470 default:
5471 ir.addr -= 2;
5472 opcode = opcode << 8 | ir.modrm;
5473 goto no_support;
5474 break;
5475 }
5476 break;
5477
5478 case 0x84: /* test */
5479 case 0x85:
5480 case 0xa8:
5481 case 0xa9:
5482 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5483 break;
5484
5485 case 0x98: /* CWDE/CBW */
5486 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5487 break;
5488
5489 case 0x99: /* CDQ/CWD */
5490 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5491 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
5492 break;
5493
5494 case 0x0faf: /* imul */
5495 case 0x69:
5496 case 0x6b:
5497 ir.ot = ir.dflag + OT_WORD;
5498 if (i386_record_modrm (&ir))
5499 return -1;
5500 if (opcode == 0x69)
5501 ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
5502 else if (opcode == 0x6b)
5503 ir.rip_offset = 1;
5504 ir.reg |= rex_r;
5505 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5506 ir.reg &= 0x3;
5507 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5508 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5509 break;
5510
5511 case 0x0fc0: /* xadd */
5512 case 0x0fc1:
5513 if ((opcode & 1) == 0)
5514 ir.ot = OT_BYTE;
5515 else
5516 ir.ot = ir.dflag + OT_WORD;
5517 if (i386_record_modrm (&ir))
5518 return -1;
5519 ir.reg |= rex_r;
5520 if (ir.mod == 3)
5521 {
5522 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5523 ir.reg &= 0x3;
5524 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5525 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5526 ir.rm &= 0x3;
5527 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5528 }
5529 else
5530 {
5531 if (i386_record_lea_modrm (&ir))
5532 return -1;
5533 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5534 ir.reg &= 0x3;
5535 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5536 }
5537 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5538 break;
5539
5540 case 0x0fb0: /* cmpxchg */
5541 case 0x0fb1:
5542 if ((opcode & 1) == 0)
5543 ir.ot = OT_BYTE;
5544 else
5545 ir.ot = ir.dflag + OT_WORD;
5546 if (i386_record_modrm (&ir))
5547 return -1;
5548 if (ir.mod == 3)
5549 {
5550 ir.reg |= rex_r;
5551 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5552 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5553 ir.reg &= 0x3;
5554 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5555 }
5556 else
5557 {
5558 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5559 if (i386_record_lea_modrm (&ir))
5560 return -1;
5561 }
5562 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5563 break;
5564
5565 case 0x0fc7: /* cmpxchg8b / rdrand / rdseed */
5566 if (i386_record_modrm (&ir))
5567 return -1;
5568 if (ir.mod == 3)
5569 {
5570 /* rdrand and rdseed use the 3 bits of the REG field of ModR/M as
5571 an extended opcode. rdrand has bits 110 (/6) and rdseed
5572 has bits 111 (/7). */
5573 if (ir.reg == 6 || ir.reg == 7)
5574 {
5575 /* The storage register is described by the 3 R/M bits, but the
5576 REX.B prefix may be used to give access to registers
5577 R8~R15. In this case ir.rex_b + R/M will give us the register
5578 in the range R8~R15.
5579
5580 REX.W may also be used to access 64-bit registers, but we
5581 already record entire registers and not just partial bits
5582 of them. */
5583 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rex_b + ir.rm);
5584 /* These instructions also set conditional bits. */
5585 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5586 break;
5587 }
5588 else
5589 {
5590 /* We don't handle this particular instruction yet. */
5591 ir.addr -= 2;
5592 opcode = opcode << 8 | ir.modrm;
5593 goto no_support;
5594 }
5595 }
5596 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5597 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
5598 if (i386_record_lea_modrm (&ir))
5599 return -1;
5600 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5601 break;
5602
5603 case 0x50: /* push */
5604 case 0x51:
5605 case 0x52:
5606 case 0x53:
5607 case 0x54:
5608 case 0x55:
5609 case 0x56:
5610 case 0x57:
5611 case 0x68:
5612 case 0x6a:
5613 if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
5614 ir.dflag = 2;
5615 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5616 return -1;
5617 break;
5618
5619 case 0x06: /* push es */
5620 case 0x0e: /* push cs */
5621 case 0x16: /* push ss */
5622 case 0x1e: /* push ds */
5623 if (ir.regmap[X86_RECORD_R8_REGNUM])
5624 {
5625 ir.addr -= 1;
5626 goto no_support;
5627 }
5628 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5629 return -1;
5630 break;
5631
5632 case 0x0fa0: /* push fs */
5633 case 0x0fa8: /* push gs */
5634 if (ir.regmap[X86_RECORD_R8_REGNUM])
5635 {
5636 ir.addr -= 2;
5637 goto no_support;
5638 }
5639 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5640 return -1;
5641 break;
5642
5643 case 0x60: /* pusha */
5644 if (ir.regmap[X86_RECORD_R8_REGNUM])
5645 {
5646 ir.addr -= 1;
5647 goto no_support;
5648 }
5649 if (i386_record_push (&ir, 1 << (ir.dflag + 4)))
5650 return -1;
5651 break;
5652
5653 case 0x58: /* pop */
5654 case 0x59:
5655 case 0x5a:
5656 case 0x5b:
5657 case 0x5c:
5658 case 0x5d:
5659 case 0x5e:
5660 case 0x5f:
5661 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5662 I386_RECORD_FULL_ARCH_LIST_ADD_REG ((opcode & 0x7) | ir.rex_b);
5663 break;
5664
5665 case 0x61: /* popa */
5666 if (ir.regmap[X86_RECORD_R8_REGNUM])
5667 {
5668 ir.addr -= 1;
5669 goto no_support;
5670 }
5671 for (regnum = X86_RECORD_REAX_REGNUM;
5672 regnum <= X86_RECORD_REDI_REGNUM;
5673 regnum++)
5674 I386_RECORD_FULL_ARCH_LIST_ADD_REG (regnum);
5675 break;
5676
5677 case 0x8f: /* pop */
5678 if (ir.regmap[X86_RECORD_R8_REGNUM])
5679 ir.ot = ir.dflag ? OT_QUAD : OT_WORD;
5680 else
5681 ir.ot = ir.dflag + OT_WORD;
5682 if (i386_record_modrm (&ir))
5683 return -1;
5684 if (ir.mod == 3)
5685 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
5686 else
5687 {
5688 ir.popl_esp_hack = 1 << ir.ot;
5689 if (i386_record_lea_modrm (&ir))
5690 return -1;
5691 }
5692 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5693 break;
5694
5695 case 0xc8: /* enter */
5696 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBP_REGNUM);
5697 if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
5698 ir.dflag = 2;
5699 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5700 return -1;
5701 break;
5702
5703 case 0xc9: /* leave */
5704 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5705 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBP_REGNUM);
5706 break;
5707
5708 case 0x07: /* pop es */
5709 if (ir.regmap[X86_RECORD_R8_REGNUM])
5710 {
5711 ir.addr -= 1;
5712 goto no_support;
5713 }
5714 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5715 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_ES_REGNUM);
5716 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5717 break;
5718
5719 case 0x17: /* pop ss */
5720 if (ir.regmap[X86_RECORD_R8_REGNUM])
5721 {
5722 ir.addr -= 1;
5723 goto no_support;
5724 }
5725 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5726 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_SS_REGNUM);
5727 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5728 break;
5729
5730 case 0x1f: /* pop ds */
5731 if (ir.regmap[X86_RECORD_R8_REGNUM])
5732 {
5733 ir.addr -= 1;
5734 goto no_support;
5735 }
5736 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5737 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_DS_REGNUM);
5738 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5739 break;
5740
5741 case 0x0fa1: /* pop fs */
5742 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5743 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_FS_REGNUM);
5744 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5745 break;
5746
5747 case 0x0fa9: /* pop gs */
5748 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5749 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_GS_REGNUM);
5750 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5751 break;
5752
5753 case 0x88: /* mov */
5754 case 0x89:
5755 case 0xc6:
5756 case 0xc7:
5757 if ((opcode & 1) == 0)
5758 ir.ot = OT_BYTE;
5759 else
5760 ir.ot = ir.dflag + OT_WORD;
5761
5762 if (i386_record_modrm (&ir))
5763 return -1;
5764
5765 if (ir.mod != 3)
5766 {
5767 if (opcode == 0xc6 || opcode == 0xc7)
5768 ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
5769 if (i386_record_lea_modrm (&ir))
5770 return -1;
5771 }
5772 else
5773 {
5774 if (opcode == 0xc6 || opcode == 0xc7)
5775 ir.rm |= ir.rex_b;
5776 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5777 ir.rm &= 0x3;
5778 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5779 }
5780 break;
5781
5782 case 0x8a: /* mov */
5783 case 0x8b:
5784 if ((opcode & 1) == 0)
5785 ir.ot = OT_BYTE;
5786 else
5787 ir.ot = ir.dflag + OT_WORD;
5788 if (i386_record_modrm (&ir))
5789 return -1;
5790 ir.reg |= rex_r;
5791 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5792 ir.reg &= 0x3;
5793 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5794 break;
5795
5796 case 0x8c: /* mov seg */
5797 if (i386_record_modrm (&ir))
5798 return -1;
5799 if (ir.reg > 5)
5800 {
5801 ir.addr -= 2;
5802 opcode = opcode << 8 | ir.modrm;
5803 goto no_support;
5804 }
5805
5806 if (ir.mod == 3)
5807 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5808 else
5809 {
5810 ir.ot = OT_WORD;
5811 if (i386_record_lea_modrm (&ir))
5812 return -1;
5813 }
5814 break;
5815
5816 case 0x8e: /* mov seg */
5817 if (i386_record_modrm (&ir))
5818 return -1;
5819 switch (ir.reg)
5820 {
5821 case 0:
5822 regnum = X86_RECORD_ES_REGNUM;
5823 break;
5824 case 2:
5825 regnum = X86_RECORD_SS_REGNUM;
5826 break;
5827 case 3:
5828 regnum = X86_RECORD_DS_REGNUM;
5829 break;
5830 case 4:
5831 regnum = X86_RECORD_FS_REGNUM;
5832 break;
5833 case 5:
5834 regnum = X86_RECORD_GS_REGNUM;
5835 break;
5836 default:
5837 ir.addr -= 2;
5838 opcode = opcode << 8 | ir.modrm;
5839 goto no_support;
5840 break;
5841 }
5842 I386_RECORD_FULL_ARCH_LIST_ADD_REG (regnum);
5843 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5844 break;
5845
5846 case 0x0fb6: /* movzbS */
5847 case 0x0fb7: /* movzwS */
5848 case 0x0fbe: /* movsbS */
5849 case 0x0fbf: /* movswS */
5850 if (i386_record_modrm (&ir))
5851 return -1;
5852 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
5853 break;
5854
5855 case 0x8d: /* lea */
5856 if (i386_record_modrm (&ir))
5857 return -1;
5858 if (ir.mod == 3)
5859 {
5860 ir.addr -= 2;
5861 opcode = opcode << 8 | ir.modrm;
5862 goto no_support;
5863 }
5864 ir.ot = ir.dflag;
5865 ir.reg |= rex_r;
5866 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5867 ir.reg &= 0x3;
5868 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5869 break;
5870
5871 case 0xa0: /* mov EAX */
5872 case 0xa1:
5873
5874 case 0xd7: /* xlat */
5875 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5876 break;
5877
5878 case 0xa2: /* mov EAX */
5879 case 0xa3:
5880 if (ir.override >= 0)
5881 {
5882 if (record_full_memory_query)
5883 {
5884 if (yquery (_("\
5885 Process record ignores the memory change of instruction at address %s\n\
5886 because it can't get the value of the segment register.\n\
5887 Do you want to stop the program?"),
5888 paddress (gdbarch, ir.orig_addr)))
5889 return -1;
5890 }
5891 }
5892 else
5893 {
5894 if ((opcode & 1) == 0)
5895 ir.ot = OT_BYTE;
5896 else
5897 ir.ot = ir.dflag + OT_WORD;
5898 if (ir.aflag == 2)
5899 {
5900 if (record_read_memory (gdbarch, ir.addr, buf, 8))
5901 return -1;
5902 ir.addr += 8;
5903 addr = extract_unsigned_integer (buf, 8, byte_order);
5904 }
5905 else if (ir.aflag)
5906 {
5907 if (record_read_memory (gdbarch, ir.addr, buf, 4))
5908 return -1;
5909 ir.addr += 4;
5910 addr = extract_unsigned_integer (buf, 4, byte_order);
5911 }
5912 else
5913 {
5914 if (record_read_memory (gdbarch, ir.addr, buf, 2))
5915 return -1;
5916 ir.addr += 2;
5917 addr = extract_unsigned_integer (buf, 2, byte_order);
5918 }
5919 if (record_full_arch_list_add_mem (addr, 1 << ir.ot))
5920 return -1;
5921 }
5922 break;
5923
5924 case 0xb0: /* mov R, Ib */
5925 case 0xb1:
5926 case 0xb2:
5927 case 0xb3:
5928 case 0xb4:
5929 case 0xb5:
5930 case 0xb6:
5931 case 0xb7:
5932 I386_RECORD_FULL_ARCH_LIST_ADD_REG ((ir.regmap[X86_RECORD_R8_REGNUM])
5933 ? ((opcode & 0x7) | ir.rex_b)
5934 : ((opcode & 0x7) & 0x3));
5935 break;
5936
5937 case 0xb8: /* mov R, Iv */
5938 case 0xb9:
5939 case 0xba:
5940 case 0xbb:
5941 case 0xbc:
5942 case 0xbd:
5943 case 0xbe:
5944 case 0xbf:
5945 I386_RECORD_FULL_ARCH_LIST_ADD_REG ((opcode & 0x7) | ir.rex_b);
5946 break;
5947
5948 case 0x91: /* xchg R, EAX */
5949 case 0x92:
5950 case 0x93:
5951 case 0x94:
5952 case 0x95:
5953 case 0x96:
5954 case 0x97:
5955 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5956 I386_RECORD_FULL_ARCH_LIST_ADD_REG (opcode & 0x7);
5957 break;
5958
5959 case 0x86: /* xchg Ev, Gv */
5960 case 0x87:
5961 if ((opcode & 1) == 0)
5962 ir.ot = OT_BYTE;
5963 else
5964 ir.ot = ir.dflag + OT_WORD;
5965 if (i386_record_modrm (&ir))
5966 return -1;
5967 if (ir.mod == 3)
5968 {
5969 ir.rm |= ir.rex_b;
5970 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5971 ir.rm &= 0x3;
5972 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5973 }
5974 else
5975 {
5976 if (i386_record_lea_modrm (&ir))
5977 return -1;
5978 }
5979 ir.reg |= rex_r;
5980 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5981 ir.reg &= 0x3;
5982 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5983 break;
5984
5985 case 0xc4: /* les Gv */
5986 case 0xc5: /* lds Gv */
5987 if (ir.regmap[X86_RECORD_R8_REGNUM])
5988 {
5989 ir.addr -= 1;
5990 goto no_support;
5991 }
5992 [[fallthrough]];
5993 case 0x0fb2: /* lss Gv */
5994 case 0x0fb4: /* lfs Gv */
5995 case 0x0fb5: /* lgs Gv */
5996 if (i386_record_modrm (&ir))
5997 return -1;
5998 if (ir.mod == 3)
5999 {
6000 if (opcode > 0xff)
6001 ir.addr -= 3;
6002 else
6003 ir.addr -= 2;
6004 opcode = opcode << 8 | ir.modrm;
6005 goto no_support;
6006 }
6007 switch (opcode)
6008 {
6009 case 0xc4: /* les Gv */
6010 regnum = X86_RECORD_ES_REGNUM;
6011 break;
6012 case 0xc5: /* lds Gv */
6013 regnum = X86_RECORD_DS_REGNUM;
6014 break;
6015 case 0x0fb2: /* lss Gv */
6016 regnum = X86_RECORD_SS_REGNUM;
6017 break;
6018 case 0x0fb4: /* lfs Gv */
6019 regnum = X86_RECORD_FS_REGNUM;
6020 break;
6021 case 0x0fb5: /* lgs Gv */
6022 regnum = X86_RECORD_GS_REGNUM;
6023 break;
6024 }
6025 I386_RECORD_FULL_ARCH_LIST_ADD_REG (regnum);
6026 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
6027 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6028 break;
6029
6030 case 0xc0: /* shifts */
6031 case 0xc1:
6032 case 0xd0:
6033 case 0xd1:
6034 case 0xd2:
6035 case 0xd3:
6036 if ((opcode & 1) == 0)
6037 ir.ot = OT_BYTE;
6038 else
6039 ir.ot = ir.dflag + OT_WORD;
6040 if (i386_record_modrm (&ir))
6041 return -1;
6042 if (ir.mod != 3 && (opcode == 0xd2 || opcode == 0xd3))
6043 {
6044 if (i386_record_lea_modrm (&ir))
6045 return -1;
6046 }
6047 else
6048 {
6049 ir.rm |= ir.rex_b;
6050 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
6051 ir.rm &= 0x3;
6052 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
6053 }
6054 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6055 break;
6056
6057 case 0x0fa4:
6058 case 0x0fa5:
6059 case 0x0fac:
6060 case 0x0fad:
6061 if (i386_record_modrm (&ir))
6062 return -1;
6063 if (ir.mod == 3)
6064 {
6065 if (record_full_arch_list_add_reg (ir.regcache, ir.rm))
6066 return -1;
6067 }
6068 else
6069 {
6070 if (i386_record_lea_modrm (&ir))
6071 return -1;
6072 }
6073 break;
6074
6075 case 0xd8: /* Floats. */
6076 case 0xd9:
6077 case 0xda:
6078 case 0xdb:
6079 case 0xdc:
6080 case 0xdd:
6081 case 0xde:
6082 case 0xdf:
6083 if (i386_record_modrm (&ir))
6084 return -1;
6085 ir.reg |= ((opcode & 7) << 3);
6086 if (ir.mod != 3)
6087 {
6088 /* Memory. */
6089 uint64_t addr64;
6090
6091 if (i386_record_lea_modrm_addr (&ir, &addr64))
6092 return -1;
6093 switch (ir.reg)
6094 {
6095 case 0x02:
6096 case 0x12:
6097 case 0x22:
6098 case 0x32:
6099 /* For fcom, ficom nothing to do. */
6100 break;
6101 case 0x03:
6102 case 0x13:
6103 case 0x23:
6104 case 0x33:
6105 /* For fcomp, ficomp pop FPU stack, store all. */
6106 if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6107 return -1;
6108 break;
6109 case 0x00:
6110 case 0x01:
6111 case 0x04:
6112 case 0x05:
6113 case 0x06:
6114 case 0x07:
6115 case 0x10:
6116 case 0x11:
6117 case 0x14:
6118 case 0x15:
6119 case 0x16:
6120 case 0x17:
6121 case 0x20:
6122 case 0x21:
6123 case 0x24:
6124 case 0x25:
6125 case 0x26:
6126 case 0x27:
6127 case 0x30:
6128 case 0x31:
6129 case 0x34:
6130 case 0x35:
6131 case 0x36:
6132 case 0x37:
6133 /* For fadd, fmul, fsub, fsubr, fdiv, fdivr, fiadd, fimul,
6134 fisub, fisubr, fidiv, fidivr, modR/M.reg is an extension
6135 of code, always affects st(0) register. */
6136 if (i386_record_floats (gdbarch, &ir, I387_ST0_REGNUM (tdep)))
6137 return -1;
6138 break;
6139 case 0x08:
6140 case 0x0a:
6141 case 0x0b:
6142 case 0x18:
6143 case 0x19:
6144 case 0x1a:
6145 case 0x1b:
6146 case 0x1d:
6147 case 0x28:
6148 case 0x29:
6149 case 0x2a:
6150 case 0x2b:
6151 case 0x38:
6152 case 0x39:
6153 case 0x3a:
6154 case 0x3b:
6155 case 0x3c:
6156 case 0x3d:
6157 switch (ir.reg & 7)
6158 {
6159 case 0:
6160 /* Handling fld, fild. */
6161 if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6162 return -1;
6163 break;
6164 case 1:
6165 switch (ir.reg >> 4)
6166 {
6167 case 0:
6168 if (record_full_arch_list_add_mem (addr64, 4))
6169 return -1;
6170 break;
6171 case 2:
6172 if (record_full_arch_list_add_mem (addr64, 8))
6173 return -1;
6174 break;
6175 case 3:
6176 break;
6177 default:
6178 if (record_full_arch_list_add_mem (addr64, 2))
6179 return -1;
6180 break;
6181 }
6182 break;
6183 default:
6184 switch (ir.reg >> 4)
6185 {
6186 case 0:
6187 if (record_full_arch_list_add_mem (addr64, 4))
6188 return -1;
6189 if (3 == (ir.reg & 7))
6190 {
6191 /* For fstp m32fp. */
6192 if (i386_record_floats (gdbarch, &ir,
6193 I386_SAVE_FPU_REGS))
6194 return -1;
6195 }
6196 break;
6197 case 1:
6198 if (record_full_arch_list_add_mem (addr64, 4))
6199 return -1;
6200 if ((3 == (ir.reg & 7))
6201 || (5 == (ir.reg & 7))
6202 || (7 == (ir.reg & 7)))
6203 {
6204 /* For fstp insn. */
6205 if (i386_record_floats (gdbarch, &ir,
6206 I386_SAVE_FPU_REGS))
6207 return -1;
6208 }
6209 break;
6210 case 2:
6211 if (record_full_arch_list_add_mem (addr64, 8))
6212 return -1;
6213 if (3 == (ir.reg & 7))
6214 {
6215 /* For fstp m64fp. */
6216 if (i386_record_floats (gdbarch, &ir,
6217 I386_SAVE_FPU_REGS))
6218 return -1;
6219 }
6220 break;
6221 case 3:
6222 if ((3 <= (ir.reg & 7)) && (6 <= (ir.reg & 7)))
6223 {
6224 /* For fistp, fbld, fild, fbstp. */
6225 if (i386_record_floats (gdbarch, &ir,
6226 I386_SAVE_FPU_REGS))
6227 return -1;
6228 }
6229 [[fallthrough]];
6230 default:
6231 if (record_full_arch_list_add_mem (addr64, 2))
6232 return -1;
6233 break;
6234 }
6235 break;
6236 }
6237 break;
6238 case 0x0c:
6239 /* Insn fldenv. */
6240 if (i386_record_floats (gdbarch, &ir,
6241 I386_SAVE_FPU_ENV_REG_STACK))
6242 return -1;
6243 break;
6244 case 0x0d:
6245 /* Insn fldcw. */
6246 if (i386_record_floats (gdbarch, &ir, I387_FCTRL_REGNUM (tdep)))
6247 return -1;
6248 break;
6249 case 0x2c:
6250 /* Insn frstor. */
6251 if (i386_record_floats (gdbarch, &ir,
6252 I386_SAVE_FPU_ENV_REG_STACK))
6253 return -1;
6254 break;
6255 case 0x0e:
6256 if (ir.dflag)
6257 {
6258 if (record_full_arch_list_add_mem (addr64, 28))
6259 return -1;
6260 }
6261 else
6262 {
6263 if (record_full_arch_list_add_mem (addr64, 14))
6264 return -1;
6265 }
6266 break;
6267 case 0x0f:
6268 case 0x2f:
6269 if (record_full_arch_list_add_mem (addr64, 2))
6270 return -1;
6271 /* Insn fstp, fbstp. */
6272 if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6273 return -1;
6274 break;
6275 case 0x1f:
6276 case 0x3e:
6277 if (record_full_arch_list_add_mem (addr64, 10))
6278 return -1;
6279 break;
6280 case 0x2e:
6281 if (ir.dflag)
6282 {
6283 if (record_full_arch_list_add_mem (addr64, 28))
6284 return -1;
6285 addr64 += 28;
6286 }
6287 else
6288 {
6289 if (record_full_arch_list_add_mem (addr64, 14))
6290 return -1;
6291 addr64 += 14;
6292 }
6293 if (record_full_arch_list_add_mem (addr64, 80))
6294 return -1;
6295 /* Insn fsave. */
6296 if (i386_record_floats (gdbarch, &ir,
6297 I386_SAVE_FPU_ENV_REG_STACK))
6298 return -1;
6299 break;
6300 case 0x3f:
6301 if (record_full_arch_list_add_mem (addr64, 8))
6302 return -1;
6303 /* Insn fistp. */
6304 if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6305 return -1;
6306 break;
6307 default:
6308 ir.addr -= 2;
6309 opcode = opcode << 8 | ir.modrm;
6310 goto no_support;
6311 break;
6312 }
6313 }
6314 /* Opcode is an extension of modR/M byte. */
6315 else
6316 {
6317 switch (opcode)
6318 {
6319 case 0xd8:
6320 if (i386_record_floats (gdbarch, &ir, I387_ST0_REGNUM (tdep)))
6321 return -1;
6322 break;
6323 case 0xd9:
6324 if (0x0c == (ir.modrm >> 4))
6325 {
6326 if ((ir.modrm & 0x0f) <= 7)
6327 {
6328 if (i386_record_floats (gdbarch, &ir,
6329 I386_SAVE_FPU_REGS))
6330 return -1;
6331 }
6332 else
6333 {
6334 if (i386_record_floats (gdbarch, &ir,
6335 I387_ST0_REGNUM (tdep)))
6336 return -1;
6337 /* If only st(0) is changing, then we have already
6338 recorded. */
6339 if ((ir.modrm & 0x0f) - 0x08)
6340 {
6341 if (i386_record_floats (gdbarch, &ir,
6342 I387_ST0_REGNUM (tdep) +
6343 ((ir.modrm & 0x0f) - 0x08)))
6344 return -1;
6345 }
6346 }
6347 }
6348 else
6349 {
6350 switch (ir.modrm)
6351 {
6352 case 0xe0:
6353 case 0xe1:
6354 case 0xf0:
6355 case 0xf5:
6356 case 0xf8:
6357 case 0xfa:
6358 case 0xfc:
6359 case 0xfe:
6360 case 0xff:
6361 if (i386_record_floats (gdbarch, &ir,
6362 I387_ST0_REGNUM (tdep)))
6363 return -1;
6364 break;
6365 case 0xf1:
6366 case 0xf2:
6367 case 0xf3:
6368 case 0xf4:
6369 case 0xf6:
6370 case 0xf7:
6371 case 0xe8:
6372 case 0xe9:
6373 case 0xea:
6374 case 0xeb:
6375 case 0xec:
6376 case 0xed:
6377 case 0xee:
6378 case 0xf9:
6379 case 0xfb:
6380 if (i386_record_floats (gdbarch, &ir,
6381 I386_SAVE_FPU_REGS))
6382 return -1;
6383 break;
6384 case 0xfd:
6385 if (i386_record_floats (gdbarch, &ir,
6386 I387_ST0_REGNUM (tdep)))
6387 return -1;
6388 if (i386_record_floats (gdbarch, &ir,
6389 I387_ST0_REGNUM (tdep) + 1))
6390 return -1;
6391 break;
6392 }
6393 }
6394 break;
6395 case 0xda:
6396 if (0xe9 == ir.modrm)
6397 {
6398 if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6399 return -1;
6400 }
6401 else if ((0x0c == ir.modrm >> 4) || (0x0d == ir.modrm >> 4))
6402 {
6403 if (i386_record_floats (gdbarch, &ir,
6404 I387_ST0_REGNUM (tdep)))
6405 return -1;
6406 if (((ir.modrm & 0x0f) > 0) && ((ir.modrm & 0x0f) <= 7))
6407 {
6408 if (i386_record_floats (gdbarch, &ir,
6409 I387_ST0_REGNUM (tdep) +
6410 (ir.modrm & 0x0f)))
6411 return -1;
6412 }
6413 else if ((ir.modrm & 0x0f) - 0x08)
6414 {
6415 if (i386_record_floats (gdbarch, &ir,
6416 I387_ST0_REGNUM (tdep) +
6417 ((ir.modrm & 0x0f) - 0x08)))
6418 return -1;
6419 }
6420 }
6421 break;
6422 case 0xdb:
6423 if (0xe3 == ir.modrm)
6424 {
6425 if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_ENV))
6426 return -1;
6427 }
6428 else if ((0x0c == ir.modrm >> 4) || (0x0d == ir.modrm >> 4))
6429 {
6430 if (i386_record_floats (gdbarch, &ir,
6431 I387_ST0_REGNUM (tdep)))
6432 return -1;
6433 if (((ir.modrm & 0x0f) > 0) && ((ir.modrm & 0x0f) <= 7))
6434 {
6435 if (i386_record_floats (gdbarch, &ir,
6436 I387_ST0_REGNUM (tdep) +
6437 (ir.modrm & 0x0f)))
6438 return -1;
6439 }
6440 else if ((ir.modrm & 0x0f) - 0x08)
6441 {
6442 if (i386_record_floats (gdbarch, &ir,
6443 I387_ST0_REGNUM (tdep) +
6444 ((ir.modrm & 0x0f) - 0x08)))
6445 return -1;
6446 }
6447 }
6448 break;
6449 case 0xdc:
6450 if ((0x0c == ir.modrm >> 4)
6451 || (0x0d == ir.modrm >> 4)
6452 || (0x0f == ir.modrm >> 4))
6453 {
6454 if ((ir.modrm & 0x0f) <= 7)
6455 {
6456 if (i386_record_floats (gdbarch, &ir,
6457 I387_ST0_REGNUM (tdep) +
6458 (ir.modrm & 0x0f)))
6459 return -1;
6460 }
6461 else
6462 {
6463 if (i386_record_floats (gdbarch, &ir,
6464 I387_ST0_REGNUM (tdep) +
6465 ((ir.modrm & 0x0f) - 0x08)))
6466 return -1;
6467 }
6468 }
6469 break;
6470 case 0xdd:
6471 if (0x0c == ir.modrm >> 4)
6472 {
6473 if (i386_record_floats (gdbarch, &ir,
6474 I387_FTAG_REGNUM (tdep)))
6475 return -1;
6476 }
6477 else if ((0x0d == ir.modrm >> 4) || (0x0e == ir.modrm >> 4))
6478 {
6479 if ((ir.modrm & 0x0f) <= 7)
6480 {
6481 if (i386_record_floats (gdbarch, &ir,
6482 I387_ST0_REGNUM (tdep) +
6483 (ir.modrm & 0x0f)))
6484 return -1;
6485 }
6486 else
6487 {
6488 if (i386_record_floats (gdbarch, &ir,
6489 I386_SAVE_FPU_REGS))
6490 return -1;
6491 }
6492 }
6493 break;
6494 case 0xde:
6495 if ((0x0c == ir.modrm >> 4)
6496 || (0x0e == ir.modrm >> 4)
6497 || (0x0f == ir.modrm >> 4)
6498 || (0xd9 == ir.modrm))
6499 {
6500 if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6501 return -1;
6502 }
6503 break;
6504 case 0xdf:
6505 if (0xe0 == ir.modrm)
6506 {
6507 if (record_full_arch_list_add_reg (ir.regcache,
6508 I386_EAX_REGNUM))
6509 return -1;
6510 }
6511 else if ((0x0f == ir.modrm >> 4) || (0x0e == ir.modrm >> 4))
6512 {
6513 if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6514 return -1;
6515 }
6516 break;
6517 }
6518 }
6519 break;
6520 /* string ops */
6521 case 0xa4: /* movsS */
6522 case 0xa5:
6523 case 0xaa: /* stosS */
6524 case 0xab:
6525 case 0x6c: /* insS */
6526 case 0x6d:
6527 regcache_raw_read_unsigned (ir.regcache,
6528 ir.regmap[X86_RECORD_RECX_REGNUM],
6529 &addr);
6530 if (addr)
6531 {
6532 ULONGEST es, ds;
6533
6534 if ((opcode & 1) == 0)
6535 ir.ot = OT_BYTE;
6536 else
6537 ir.ot = ir.dflag + OT_WORD;
6538 regcache_raw_read_unsigned (ir.regcache,
6539 ir.regmap[X86_RECORD_REDI_REGNUM],
6540 &addr);
6541
6542 regcache_raw_read_unsigned (ir.regcache,
6543 ir.regmap[X86_RECORD_ES_REGNUM],
6544 &es);
6545 regcache_raw_read_unsigned (ir.regcache,
6546 ir.regmap[X86_RECORD_DS_REGNUM],
6547 &ds);
6548 if (ir.aflag && (es != ds))
6549 {
6550 /* addr += ((uint32_t) read_register (I386_ES_REGNUM)) << 4; */
6551 if (record_full_memory_query)
6552 {
6553 if (yquery (_("\
6554 Process record ignores the memory change of instruction at address %s\n\
6555 because it can't get the value of the segment register.\n\
6556 Do you want to stop the program?"),
6557 paddress (gdbarch, ir.orig_addr)))
6558 return -1;
6559 }
6560 }
6561 else
6562 {
6563 if (record_full_arch_list_add_mem (addr, 1 << ir.ot))
6564 return -1;
6565 }
6566
6567 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
6568 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6569 if (opcode == 0xa4 || opcode == 0xa5)
6570 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
6571 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
6572 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6573 }
6574 break;
6575
6576 case 0xa6: /* cmpsS */
6577 case 0xa7:
6578 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
6579 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
6580 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
6581 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6582 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6583 break;
6584
6585 case 0xac: /* lodsS */
6586 case 0xad:
6587 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6588 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
6589 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
6590 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6591 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6592 break;
6593
6594 case 0xae: /* scasS */
6595 case 0xaf:
6596 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
6597 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
6598 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6599 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6600 break;
6601
6602 case 0x6e: /* outsS */
6603 case 0x6f:
6604 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
6605 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
6606 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6607 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6608 break;
6609
6610 case 0xe4: /* port I/O */
6611 case 0xe5:
6612 case 0xec:
6613 case 0xed:
6614 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6615 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6616 break;
6617
6618 case 0xe6:
6619 case 0xe7:
6620 case 0xee:
6621 case 0xef:
6622 break;
6623
6624 /* control */
6625 case 0xc2: /* ret im */
6626 case 0xc3: /* ret */
6627 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
6628 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6629 break;
6630
6631 case 0xca: /* lret im */
6632 case 0xcb: /* lret */
6633 case 0xcf: /* iret */
6634 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
6635 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
6636 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6637 break;
6638
6639 case 0xe8: /* call im */
6640 if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
6641 ir.dflag = 2;
6642 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
6643 return -1;
6644 break;
6645
6646 case 0x9a: /* lcall im */
6647 if (ir.regmap[X86_RECORD_R8_REGNUM])
6648 {
6649 ir.addr -= 1;
6650 goto no_support;
6651 }
6652 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
6653 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
6654 return -1;
6655 break;
6656
6657 case 0xe9: /* jmp im */
6658 case 0xea: /* ljmp im */
6659 case 0xeb: /* jmp Jb */
6660 case 0x70: /* jcc Jb */
6661 case 0x71:
6662 case 0x72:
6663 case 0x73:
6664 case 0x74:
6665 case 0x75:
6666 case 0x76:
6667 case 0x77:
6668 case 0x78:
6669 case 0x79:
6670 case 0x7a:
6671 case 0x7b:
6672 case 0x7c:
6673 case 0x7d:
6674 case 0x7e:
6675 case 0x7f:
6676 case 0x0f80: /* jcc Jv */
6677 case 0x0f81:
6678 case 0x0f82:
6679 case 0x0f83:
6680 case 0x0f84:
6681 case 0x0f85:
6682 case 0x0f86:
6683 case 0x0f87:
6684 case 0x0f88:
6685 case 0x0f89:
6686 case 0x0f8a:
6687 case 0x0f8b:
6688 case 0x0f8c:
6689 case 0x0f8d:
6690 case 0x0f8e:
6691 case 0x0f8f:
6692 break;
6693
6694 case 0x0f90: /* setcc Gv */
6695 case 0x0f91:
6696 case 0x0f92:
6697 case 0x0f93:
6698 case 0x0f94:
6699 case 0x0f95:
6700 case 0x0f96:
6701 case 0x0f97:
6702 case 0x0f98:
6703 case 0x0f99:
6704 case 0x0f9a:
6705 case 0x0f9b:
6706 case 0x0f9c:
6707 case 0x0f9d:
6708 case 0x0f9e:
6709 case 0x0f9f:
6710 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6711 ir.ot = OT_BYTE;
6712 if (i386_record_modrm (&ir))
6713 return -1;
6714 if (ir.mod == 3)
6715 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rex_b ? (ir.rm | ir.rex_b)
6716 : (ir.rm & 0x3));
6717 else
6718 {
6719 if (i386_record_lea_modrm (&ir))
6720 return -1;
6721 }
6722 break;
6723
6724 case 0x0f40: /* cmov Gv, Ev */
6725 case 0x0f41:
6726 case 0x0f42:
6727 case 0x0f43:
6728 case 0x0f44:
6729 case 0x0f45:
6730 case 0x0f46:
6731 case 0x0f47:
6732 case 0x0f48:
6733 case 0x0f49:
6734 case 0x0f4a:
6735 case 0x0f4b:
6736 case 0x0f4c:
6737 case 0x0f4d:
6738 case 0x0f4e:
6739 case 0x0f4f:
6740 if (i386_record_modrm (&ir))
6741 return -1;
6742 ir.reg |= rex_r;
6743 if (ir.dflag == OT_BYTE)
6744 ir.reg &= 0x3;
6745 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
6746 break;
6747
6748 /* flags */
6749 case 0x9c: /* pushf */
6750 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6751 if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
6752 ir.dflag = 2;
6753 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
6754 return -1;
6755 break;
6756
6757 case 0x9d: /* popf */
6758 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
6759 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6760 break;
6761
6762 case 0x9e: /* sahf */
6763 if (ir.regmap[X86_RECORD_R8_REGNUM])
6764 {
6765 ir.addr -= 1;
6766 goto no_support;
6767 }
6768 [[fallthrough]];
6769 case 0xf5: /* cmc */
6770 case 0xf8: /* clc */
6771 case 0xf9: /* stc */
6772 case 0xfc: /* cld */
6773 case 0xfd: /* std */
6774 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6775 break;
6776
6777 case 0x9f: /* lahf */
6778 if (ir.regmap[X86_RECORD_R8_REGNUM])
6779 {
6780 ir.addr -= 1;
6781 goto no_support;
6782 }
6783 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6784 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6785 break;
6786
6787 /* bit operations */
6788 case 0x0fba: /* bt/bts/btr/btc Gv, im */
6789 ir.ot = ir.dflag + OT_WORD;
6790 if (i386_record_modrm (&ir))
6791 return -1;
6792 if (ir.reg < 4)
6793 {
6794 ir.addr -= 2;
6795 opcode = opcode << 8 | ir.modrm;
6796 goto no_support;
6797 }
6798 if (ir.reg != 4)
6799 {
6800 if (ir.mod == 3)
6801 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
6802 else
6803 {
6804 if (i386_record_lea_modrm (&ir))
6805 return -1;
6806 }
6807 }
6808 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6809 break;
6810
6811 case 0x0fa3: /* bt Gv, Ev */
6812 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6813 break;
6814
6815 case 0x0fab: /* bts */
6816 case 0x0fb3: /* btr */
6817 case 0x0fbb: /* btc */
6818 ir.ot = ir.dflag + OT_WORD;
6819 if (i386_record_modrm (&ir))
6820 return -1;
6821 if (ir.mod == 3)
6822 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
6823 else
6824 {
6825 uint64_t addr64;
6826 if (i386_record_lea_modrm_addr (&ir, &addr64))
6827 return -1;
6828 regcache_raw_read_unsigned (ir.regcache,
6829 ir.regmap[ir.reg | rex_r],
6830 &addr);
6831 switch (ir.dflag)
6832 {
6833 case 0:
6834 addr64 += ((int16_t) addr >> 4) << 4;
6835 break;
6836 case 1:
6837 addr64 += ((int32_t) addr >> 5) << 5;
6838 break;
6839 case 2:
6840 addr64 += ((int64_t) addr >> 6) << 6;
6841 break;
6842 }
6843 if (record_full_arch_list_add_mem (addr64, 1 << ir.ot))
6844 return -1;
6845 if (i386_record_lea_modrm (&ir))
6846 return -1;
6847 }
6848 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6849 break;
6850
6851 case 0x0fbc: /* bsf */
6852 case 0x0fbd: /* bsr */
6853 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
6854 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6855 break;
6856
6857 /* bcd */
6858 case 0x27: /* daa */
6859 case 0x2f: /* das */
6860 case 0x37: /* aaa */
6861 case 0x3f: /* aas */
6862 case 0xd4: /* aam */
6863 case 0xd5: /* aad */
6864 if (ir.regmap[X86_RECORD_R8_REGNUM])
6865 {
6866 ir.addr -= 1;
6867 goto no_support;
6868 }
6869 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6870 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6871 break;
6872
6873 /* misc */
6874 case 0x90: /* nop */
6875 if (prefixes & PREFIX_LOCK)
6876 {
6877 ir.addr -= 1;
6878 goto no_support;
6879 }
6880 break;
6881
6882 case 0x9b: /* fwait */
6883 if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
6884 return -1;
6885 opcode = (uint32_t) opcode8;
6886 ir.addr++;
6887 goto reswitch;
6888 break;
6889
6890 /* XXX */
6891 case 0xcc: /* int3 */
6892 gdb_printf (gdb_stderr,
6893 _("Process record does not support instruction "
6894 "int3.\n"));
6895 ir.addr -= 1;
6896 goto no_support;
6897 break;
6898
6899 /* XXX */
6900 case 0xcd: /* int */
6901 {
6902 int ret;
6903 uint8_t interrupt;
6904 if (record_read_memory (gdbarch, ir.addr, &interrupt, 1))
6905 return -1;
6906 ir.addr++;
6907 if (interrupt != 0x80
6908 || tdep->i386_intx80_record == NULL)
6909 {
6910 gdb_printf (gdb_stderr,
6911 _("Process record does not support "
6912 "instruction int 0x%02x.\n"),
6913 interrupt);
6914 ir.addr -= 2;
6915 goto no_support;
6916 }
6917 ret = tdep->i386_intx80_record (ir.regcache);
6918 if (ret)
6919 return ret;
6920 }
6921 break;
6922
6923 /* XXX */
6924 case 0xce: /* into */
6925 gdb_printf (gdb_stderr,
6926 _("Process record does not support "
6927 "instruction into.\n"));
6928 ir.addr -= 1;
6929 goto no_support;
6930 break;
6931
6932 case 0xfa: /* cli */
6933 case 0xfb: /* sti */
6934 break;
6935
6936 case 0x62: /* bound */
6937 gdb_printf (gdb_stderr,
6938 _("Process record does not support "
6939 "instruction bound.\n"));
6940 ir.addr -= 1;
6941 goto no_support;
6942 break;
6943
6944 case 0x0fc8: /* bswap reg */
6945 case 0x0fc9:
6946 case 0x0fca:
6947 case 0x0fcb:
6948 case 0x0fcc:
6949 case 0x0fcd:
6950 case 0x0fce:
6951 case 0x0fcf:
6952 I386_RECORD_FULL_ARCH_LIST_ADD_REG ((opcode & 7) | ir.rex_b);
6953 break;
6954
6955 case 0xd6: /* salc */
6956 if (ir.regmap[X86_RECORD_R8_REGNUM])
6957 {
6958 ir.addr -= 1;
6959 goto no_support;
6960 }
6961 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6962 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6963 break;
6964
6965 case 0xe0: /* loopnz */
6966 case 0xe1: /* loopz */
6967 case 0xe2: /* loop */
6968 case 0xe3: /* jecxz */
6969 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6970 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6971 break;
6972
6973 case 0x0f30: /* wrmsr */
6974 gdb_printf (gdb_stderr,
6975 _("Process record does not support "
6976 "instruction wrmsr.\n"));
6977 ir.addr -= 2;
6978 goto no_support;
6979 break;
6980
6981 case 0x0f32: /* rdmsr */
6982 gdb_printf (gdb_stderr,
6983 _("Process record does not support "
6984 "instruction rdmsr.\n"));
6985 ir.addr -= 2;
6986 goto no_support;
6987 break;
6988
6989 case 0x0f01f9: /* rdtscp */
6990 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6991 [[fallthrough]];
6992 case 0x0f31: /* rdtsc */
6993 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6994 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
6995 break;
6996
6997 case 0x0f34: /* sysenter */
6998 {
6999 int ret;
7000 if (ir.regmap[X86_RECORD_R8_REGNUM])
7001 {
7002 ir.addr -= 2;
7003 goto no_support;
7004 }
7005 if (tdep->i386_sysenter_record == NULL)
7006 {
7007 gdb_printf (gdb_stderr,
7008 _("Process record does not support "
7009 "instruction sysenter.\n"));
7010 ir.addr -= 2;
7011 goto no_support;
7012 }
7013 ret = tdep->i386_sysenter_record (ir.regcache);
7014 if (ret)
7015 return ret;
7016 }
7017 break;
7018
7019 case 0x0f35: /* sysexit */
7020 gdb_printf (gdb_stderr,
7021 _("Process record does not support "
7022 "instruction sysexit.\n"));
7023 ir.addr -= 2;
7024 goto no_support;
7025 break;
7026
7027 case 0x0f05: /* syscall */
7028 {
7029 int ret;
7030 if (tdep->i386_syscall_record == NULL)
7031 {
7032 gdb_printf (gdb_stderr,
7033 _("Process record does not support "
7034 "instruction syscall.\n"));
7035 ir.addr -= 2;
7036 goto no_support;
7037 }
7038 ret = tdep->i386_syscall_record (ir.regcache);
7039 if (ret)
7040 return ret;
7041 }
7042 break;
7043
7044 case 0x0f07: /* sysret */
7045 gdb_printf (gdb_stderr,
7046 _("Process record does not support "
7047 "instruction sysret.\n"));
7048 ir.addr -= 2;
7049 goto no_support;
7050 break;
7051
7052 case 0x0fa2: /* cpuid */
7053 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
7054 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
7055 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
7056 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBX_REGNUM);
7057 break;
7058
7059 case 0xf4: /* hlt */
7060 gdb_printf (gdb_stderr,
7061 _("Process record does not support "
7062 "instruction hlt.\n"));
7063 ir.addr -= 1;
7064 goto no_support;
7065 break;
7066
7067 case 0x0f00:
7068 if (i386_record_modrm (&ir))
7069 return -1;
7070 switch (ir.reg)
7071 {
7072 case 0: /* sldt */
7073 case 1: /* str */
7074 if (ir.mod == 3)
7075 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
7076 else
7077 {
7078 ir.ot = OT_WORD;
7079 if (i386_record_lea_modrm (&ir))
7080 return -1;
7081 }
7082 break;
7083 case 2: /* lldt */
7084 case 3: /* ltr */
7085 break;
7086 case 4: /* verr */
7087 case 5: /* verw */
7088 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7089 break;
7090 default:
7091 ir.addr -= 3;
7092 opcode = opcode << 8 | ir.modrm;
7093 goto no_support;
7094 break;
7095 }
7096 break;
7097
7098 case 0x0f01:
7099 if (i386_record_modrm (&ir))
7100 return -1;
7101 if (ir.modrm == 0xf9)
7102 {
7103 opcode = (opcode << 8) | 0xf9;
7104 goto reswitch;
7105 }
7106 switch (ir.reg)
7107 {
7108 case 0: /* sgdt */
7109 {
7110 uint64_t addr64;
7111
7112 if (ir.mod == 3)
7113 {
7114 ir.addr -= 3;
7115 opcode = opcode << 8 | ir.modrm;
7116 goto no_support;
7117 }
7118 if (ir.override >= 0)
7119 {
7120 if (record_full_memory_query)
7121 {
7122 if (yquery (_("\
7123 Process record ignores the memory change of instruction at address %s\n\
7124 because it can't get the value of the segment register.\n\
7125 Do you want to stop the program?"),
7126 paddress (gdbarch, ir.orig_addr)))
7127 return -1;
7128 }
7129 }
7130 else
7131 {
7132 if (i386_record_lea_modrm_addr (&ir, &addr64))
7133 return -1;
7134 if (record_full_arch_list_add_mem (addr64, 2))
7135 return -1;
7136 addr64 += 2;
7137 if (ir.regmap[X86_RECORD_R8_REGNUM])
7138 {
7139 if (record_full_arch_list_add_mem (addr64, 8))
7140 return -1;
7141 }
7142 else
7143 {
7144 if (record_full_arch_list_add_mem (addr64, 4))
7145 return -1;
7146 }
7147 }
7148 }
7149 break;
7150 case 1:
7151 if (ir.mod == 3)
7152 {
7153 switch (ir.rm)
7154 {
7155 case 0: /* monitor */
7156 break;
7157 case 1: /* mwait */
7158 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7159 break;
7160 default:
7161 ir.addr -= 3;
7162 opcode = opcode << 8 | ir.modrm;
7163 goto no_support;
7164 break;
7165 }
7166 }
7167 else
7168 {
7169 /* sidt */
7170 if (ir.override >= 0)
7171 {
7172 if (record_full_memory_query)
7173 {
7174 if (yquery (_("\
7175 Process record ignores the memory change of instruction at address %s\n\
7176 because it can't get the value of the segment register.\n\
7177 Do you want to stop the program?"),
7178 paddress (gdbarch, ir.orig_addr)))
7179 return -1;
7180 }
7181 }
7182 else
7183 {
7184 uint64_t addr64;
7185
7186 if (i386_record_lea_modrm_addr (&ir, &addr64))
7187 return -1;
7188 if (record_full_arch_list_add_mem (addr64, 2))
7189 return -1;
7190 addr64 += 2;
7191 if (ir.regmap[X86_RECORD_R8_REGNUM])
7192 {
7193 if (record_full_arch_list_add_mem (addr64, 8))
7194 return -1;
7195 }
7196 else
7197 {
7198 if (record_full_arch_list_add_mem (addr64, 4))
7199 return -1;
7200 }
7201 }
7202 }
7203 break;
7204 case 2: /* lgdt */
7205 if (ir.mod == 3)
7206 {
7207 /* xgetbv */
7208 if (ir.rm == 0)
7209 {
7210 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
7211 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
7212 break;
7213 }
7214 /* xsetbv */
7215 else if (ir.rm == 1)
7216 break;
7217 }
7218 [[fallthrough]];
7219 case 3: /* lidt */
7220 if (ir.mod == 3)
7221 {
7222 ir.addr -= 3;
7223 opcode = opcode << 8 | ir.modrm;
7224 goto no_support;
7225 }
7226 break;
7227 case 4: /* smsw */
7228 if (ir.mod == 3)
7229 {
7230 if (record_full_arch_list_add_reg (ir.regcache, ir.rm | ir.rex_b))
7231 return -1;
7232 }
7233 else
7234 {
7235 ir.ot = OT_WORD;
7236 if (i386_record_lea_modrm (&ir))
7237 return -1;
7238 }
7239 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7240 break;
7241 case 6: /* lmsw */
7242 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7243 break;
7244 case 7: /* invlpg */
7245 if (ir.mod == 3)
7246 {
7247 if (ir.rm == 0 && ir.regmap[X86_RECORD_R8_REGNUM])
7248 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_GS_REGNUM);
7249 else
7250 {
7251 ir.addr -= 3;
7252 opcode = opcode << 8 | ir.modrm;
7253 goto no_support;
7254 }
7255 }
7256 else
7257 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7258 break;
7259 default:
7260 ir.addr -= 3;
7261 opcode = opcode << 8 | ir.modrm;
7262 goto no_support;
7263 break;
7264 }
7265 break;
7266
7267 case 0x0f08: /* invd */
7268 case 0x0f09: /* wbinvd */
7269 break;
7270
7271 case 0x63: /* arpl */
7272 if (i386_record_modrm (&ir))
7273 return -1;
7274 if (ir.mod == 3 || ir.regmap[X86_RECORD_R8_REGNUM])
7275 {
7276 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.regmap[X86_RECORD_R8_REGNUM]
7277 ? (ir.reg | rex_r) : ir.rm);
7278 }
7279 else
7280 {
7281 ir.ot = ir.dflag ? OT_LONG : OT_WORD;
7282 if (i386_record_lea_modrm (&ir))
7283 return -1;
7284 }
7285 if (!ir.regmap[X86_RECORD_R8_REGNUM])
7286 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7287 break;
7288
7289 case 0x0f02: /* lar */
7290 case 0x0f03: /* lsl */
7291 if (i386_record_modrm (&ir))
7292 return -1;
7293 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
7294 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7295 break;
7296
7297 case 0x0f18:
7298 if (i386_record_modrm (&ir))
7299 return -1;
7300 if (ir.mod == 3 && ir.reg == 3)
7301 {
7302 ir.addr -= 3;
7303 opcode = opcode << 8 | ir.modrm;
7304 goto no_support;
7305 }
7306 break;
7307
7308 case 0x0f19:
7309 case 0x0f1a:
7310 case 0x0f1b:
7311 case 0x0f1c:
7312 case 0x0f1d:
7313 case 0x0f1e:
7314 case 0x0f1f:
7315 /* nop (multi byte) */
7316 break;
7317
7318 case 0x0f20: /* mov reg, crN */
7319 case 0x0f22: /* mov crN, reg */
7320 if (i386_record_modrm (&ir))
7321 return -1;
7322 if ((ir.modrm & 0xc0) != 0xc0)
7323 {
7324 ir.addr -= 3;
7325 opcode = opcode << 8 | ir.modrm;
7326 goto no_support;
7327 }
7328 switch (ir.reg)
7329 {
7330 case 0:
7331 case 2:
7332 case 3:
7333 case 4:
7334 case 8:
7335 if (opcode & 2)
7336 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7337 else
7338 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
7339 break;
7340 default:
7341 ir.addr -= 3;
7342 opcode = opcode << 8 | ir.modrm;
7343 goto no_support;
7344 break;
7345 }
7346 break;
7347
7348 case 0x0f21: /* mov reg, drN */
7349 case 0x0f23: /* mov drN, reg */
7350 if (i386_record_modrm (&ir))
7351 return -1;
7352 if ((ir.modrm & 0xc0) != 0xc0 || ir.reg == 4
7353 || ir.reg == 5 || ir.reg >= 8)
7354 {
7355 ir.addr -= 3;
7356 opcode = opcode << 8 | ir.modrm;
7357 goto no_support;
7358 }
7359 if (opcode & 2)
7360 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7361 else
7362 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
7363 break;
7364
7365 case 0x0f06: /* clts */
7366 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7367 break;
7368
7369 /* MMX 3DNow! SSE SSE2 SSE3 SSSE3 SSE4 */
7370
7371 case 0x0f0d: /* 3DNow! prefetch */
7372 break;
7373
7374 case 0x0f0e: /* 3DNow! femms */
7375 case 0x0f77: /* emms */
7376 if (i386_fpc_regnum_p (gdbarch, I387_FTAG_REGNUM(tdep)))
7377 goto no_support;
7378 record_full_arch_list_add_reg (ir.regcache, I387_FTAG_REGNUM(tdep));
7379 break;
7380
7381 case 0x0f0f: /* 3DNow! data */
7382 if (i386_record_modrm (&ir))
7383 return -1;
7384 if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
7385 return -1;
7386 ir.addr++;
7387 switch (opcode8)
7388 {
7389 case 0x0c: /* 3DNow! pi2fw */
7390 case 0x0d: /* 3DNow! pi2fd */
7391 case 0x1c: /* 3DNow! pf2iw */
7392 case 0x1d: /* 3DNow! pf2id */
7393 case 0x8a: /* 3DNow! pfnacc */
7394 case 0x8e: /* 3DNow! pfpnacc */
7395 case 0x90: /* 3DNow! pfcmpge */
7396 case 0x94: /* 3DNow! pfmin */
7397 case 0x96: /* 3DNow! pfrcp */
7398 case 0x97: /* 3DNow! pfrsqrt */
7399 case 0x9a: /* 3DNow! pfsub */
7400 case 0x9e: /* 3DNow! pfadd */
7401 case 0xa0: /* 3DNow! pfcmpgt */
7402 case 0xa4: /* 3DNow! pfmax */
7403 case 0xa6: /* 3DNow! pfrcpit1 */
7404 case 0xa7: /* 3DNow! pfrsqit1 */
7405 case 0xaa: /* 3DNow! pfsubr */
7406 case 0xae: /* 3DNow! pfacc */
7407 case 0xb0: /* 3DNow! pfcmpeq */
7408 case 0xb4: /* 3DNow! pfmul */
7409 case 0xb6: /* 3DNow! pfrcpit2 */
7410 case 0xb7: /* 3DNow! pmulhrw */
7411 case 0xbb: /* 3DNow! pswapd */
7412 case 0xbf: /* 3DNow! pavgusb */
7413 if (!i386_mmx_regnum_p (gdbarch, I387_MM0_REGNUM (tdep) + ir.reg))
7414 goto no_support_3dnow_data;
7415 record_full_arch_list_add_reg (ir.regcache, ir.reg);
7416 break;
7417
7418 default:
7419 no_support_3dnow_data:
7420 opcode = (opcode << 8) | opcode8;
7421 goto no_support;
7422 break;
7423 }
7424 break;
7425
7426 case 0x0faa: /* rsm */
7427 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7428 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
7429 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
7430 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
7431 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBX_REGNUM);
7432 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
7433 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBP_REGNUM);
7434 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
7435 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
7436 break;
7437
7438 case 0x0fae:
7439 if (i386_record_modrm (&ir))
7440 return -1;
7441 switch(ir.reg)
7442 {
7443 case 0: /* fxsave */
7444 {
7445 uint64_t tmpu64;
7446
7447 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7448 if (i386_record_lea_modrm_addr (&ir, &tmpu64))
7449 return -1;
7450 if (record_full_arch_list_add_mem (tmpu64, 512))
7451 return -1;
7452 }
7453 break;
7454
7455 case 1: /* fxrstor */
7456 {
7457 int i;
7458
7459 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7460
7461 for (i = I387_MM0_REGNUM (tdep);
7462 i386_mmx_regnum_p (gdbarch, i); i++)
7463 record_full_arch_list_add_reg (ir.regcache, i);
7464
7465 for (i = I387_XMM0_REGNUM (tdep);
7466 i386_xmm_regnum_p (gdbarch, i); i++)
7467 record_full_arch_list_add_reg (ir.regcache, i);
7468
7469 if (i386_mxcsr_regnum_p (gdbarch, I387_MXCSR_REGNUM(tdep)))
7470 record_full_arch_list_add_reg (ir.regcache,
7471 I387_MXCSR_REGNUM(tdep));
7472
7473 for (i = I387_ST0_REGNUM (tdep);
7474 i386_fp_regnum_p (gdbarch, i); i++)
7475 record_full_arch_list_add_reg (ir.regcache, i);
7476
7477 for (i = I387_FCTRL_REGNUM (tdep);
7478 i386_fpc_regnum_p (gdbarch, i); i++)
7479 record_full_arch_list_add_reg (ir.regcache, i);
7480 }
7481 break;
7482
7483 case 2: /* ldmxcsr */
7484 if (!i386_mxcsr_regnum_p (gdbarch, I387_MXCSR_REGNUM(tdep)))
7485 goto no_support;
7486 record_full_arch_list_add_reg (ir.regcache, I387_MXCSR_REGNUM(tdep));
7487 break;
7488
7489 case 3: /* stmxcsr */
7490 ir.ot = OT_LONG;
7491 if (i386_record_lea_modrm (&ir))
7492 return -1;
7493 break;
7494
7495 case 5: /* lfence */
7496 case 6: /* mfence */
7497 case 7: /* sfence clflush */
7498 break;
7499
7500 default:
7501 opcode = (opcode << 8) | ir.modrm;
7502 goto no_support;
7503 break;
7504 }
7505 break;
7506
7507 case 0x0fc3: /* movnti */
7508 ir.ot = (ir.dflag == 2) ? OT_QUAD : OT_LONG;
7509 if (i386_record_modrm (&ir))
7510 return -1;
7511 if (ir.mod == 3)
7512 goto no_support;
7513 ir.reg |= rex_r;
7514 if (i386_record_lea_modrm (&ir))
7515 return -1;
7516 break;
7517
7518 /* Add prefix to opcode. */
7519 case 0x0f10:
7520 case 0x0f11:
7521 case 0x0f12:
7522 case 0x0f13:
7523 case 0x0f14:
7524 case 0x0f15:
7525 case 0x0f16:
7526 case 0x0f17:
7527 case 0x0f28:
7528 case 0x0f29:
7529 case 0x0f2a:
7530 case 0x0f2b:
7531 case 0x0f2c:
7532 case 0x0f2d:
7533 case 0x0f2e:
7534 case 0x0f2f:
7535 case 0x0f38:
7536 case 0x0f39:
7537 case 0x0f3a:
7538 case 0x0f50:
7539 case 0x0f51:
7540 case 0x0f52:
7541 case 0x0f53:
7542 case 0x0f54:
7543 case 0x0f55:
7544 case 0x0f56:
7545 case 0x0f57:
7546 case 0x0f58:
7547 case 0x0f59:
7548 case 0x0f5a:
7549 case 0x0f5b:
7550 case 0x0f5c:
7551 case 0x0f5d:
7552 case 0x0f5e:
7553 case 0x0f5f:
7554 case 0x0f60:
7555 case 0x0f61:
7556 case 0x0f62:
7557 case 0x0f63:
7558 case 0x0f64:
7559 case 0x0f65:
7560 case 0x0f66:
7561 case 0x0f67:
7562 case 0x0f68:
7563 case 0x0f69:
7564 case 0x0f6a:
7565 case 0x0f6b:
7566 case 0x0f6c:
7567 case 0x0f6d:
7568 case 0x0f6e:
7569 case 0x0f6f:
7570 case 0x0f70:
7571 case 0x0f71:
7572 case 0x0f72:
7573 case 0x0f73:
7574 case 0x0f74:
7575 case 0x0f75:
7576 case 0x0f76:
7577 case 0x0f7c:
7578 case 0x0f7d:
7579 case 0x0f7e:
7580 case 0x0f7f:
7581 case 0x0fb8:
7582 case 0x0fc2:
7583 case 0x0fc4:
7584 case 0x0fc5:
7585 case 0x0fc6:
7586 case 0x0fd0:
7587 case 0x0fd1:
7588 case 0x0fd2:
7589 case 0x0fd3:
7590 case 0x0fd4:
7591 case 0x0fd5:
7592 case 0x0fd6:
7593 case 0x0fd7:
7594 case 0x0fd8:
7595 case 0x0fd9:
7596 case 0x0fda:
7597 case 0x0fdb:
7598 case 0x0fdc:
7599 case 0x0fdd:
7600 case 0x0fde:
7601 case 0x0fdf:
7602 case 0x0fe0:
7603 case 0x0fe1:
7604 case 0x0fe2:
7605 case 0x0fe3:
7606 case 0x0fe4:
7607 case 0x0fe5:
7608 case 0x0fe6:
7609 case 0x0fe7:
7610 case 0x0fe8:
7611 case 0x0fe9:
7612 case 0x0fea:
7613 case 0x0feb:
7614 case 0x0fec:
7615 case 0x0fed:
7616 case 0x0fee:
7617 case 0x0fef:
7618 case 0x0ff0:
7619 case 0x0ff1:
7620 case 0x0ff2:
7621 case 0x0ff3:
7622 case 0x0ff4:
7623 case 0x0ff5:
7624 case 0x0ff6:
7625 case 0x0ff7:
7626 case 0x0ff8:
7627 case 0x0ff9:
7628 case 0x0ffa:
7629 case 0x0ffb:
7630 case 0x0ffc:
7631 case 0x0ffd:
7632 case 0x0ffe:
7633 /* Mask out PREFIX_ADDR. */
7634 switch ((prefixes & ~PREFIX_ADDR))
7635 {
7636 case PREFIX_REPNZ:
7637 opcode |= 0xf20000;
7638 break;
7639 case PREFIX_DATA:
7640 opcode |= 0x660000;
7641 break;
7642 case PREFIX_REPZ:
7643 opcode |= 0xf30000;
7644 break;
7645 }
7646 reswitch_prefix_add:
7647 switch (opcode)
7648 {
7649 case 0x0f38:
7650 case 0x660f38:
7651 case 0xf20f38:
7652 case 0x0f3a:
7653 case 0x660f3a:
7654 if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
7655 return -1;
7656 ir.addr++;
7657 opcode = (uint32_t) opcode8 | opcode << 8;
7658 goto reswitch_prefix_add;
7659 break;
7660
7661 case 0x0f10: /* movups */
7662 case 0x660f10: /* movupd */
7663 case 0xf30f10: /* movss */
7664 case 0xf20f10: /* movsd */
7665 case 0x0f12: /* movlps */
7666 case 0x660f12: /* movlpd */
7667 case 0xf30f12: /* movsldup */
7668 case 0xf20f12: /* movddup */
7669 case 0x0f14: /* unpcklps */
7670 case 0x660f14: /* unpcklpd */
7671 case 0x0f15: /* unpckhps */
7672 case 0x660f15: /* unpckhpd */
7673 case 0x0f16: /* movhps */
7674 case 0x660f16: /* movhpd */
7675 case 0xf30f16: /* movshdup */
7676 case 0x0f28: /* movaps */
7677 case 0x660f28: /* movapd */
7678 case 0x0f2a: /* cvtpi2ps */
7679 case 0x660f2a: /* cvtpi2pd */
7680 case 0xf30f2a: /* cvtsi2ss */
7681 case 0xf20f2a: /* cvtsi2sd */
7682 case 0x0f2c: /* cvttps2pi */
7683 case 0x660f2c: /* cvttpd2pi */
7684 case 0x0f2d: /* cvtps2pi */
7685 case 0x660f2d: /* cvtpd2pi */
7686 case 0x660f3800: /* pshufb */
7687 case 0x660f3801: /* phaddw */
7688 case 0x660f3802: /* phaddd */
7689 case 0x660f3803: /* phaddsw */
7690 case 0x660f3804: /* pmaddubsw */
7691 case 0x660f3805: /* phsubw */
7692 case 0x660f3806: /* phsubd */
7693 case 0x660f3807: /* phsubsw */
7694 case 0x660f3808: /* psignb */
7695 case 0x660f3809: /* psignw */
7696 case 0x660f380a: /* psignd */
7697 case 0x660f380b: /* pmulhrsw */
7698 case 0x660f3810: /* pblendvb */
7699 case 0x660f3814: /* blendvps */
7700 case 0x660f3815: /* blendvpd */
7701 case 0x660f381c: /* pabsb */
7702 case 0x660f381d: /* pabsw */
7703 case 0x660f381e: /* pabsd */
7704 case 0x660f3820: /* pmovsxbw */
7705 case 0x660f3821: /* pmovsxbd */
7706 case 0x660f3822: /* pmovsxbq */
7707 case 0x660f3823: /* pmovsxwd */
7708 case 0x660f3824: /* pmovsxwq */
7709 case 0x660f3825: /* pmovsxdq */
7710 case 0x660f3828: /* pmuldq */
7711 case 0x660f3829: /* pcmpeqq */
7712 case 0x660f382a: /* movntdqa */
7713 case 0x660f3a08: /* roundps */
7714 case 0x660f3a09: /* roundpd */
7715 case 0x660f3a0a: /* roundss */
7716 case 0x660f3a0b: /* roundsd */
7717 case 0x660f3a0c: /* blendps */
7718 case 0x660f3a0d: /* blendpd */
7719 case 0x660f3a0e: /* pblendw */
7720 case 0x660f3a0f: /* palignr */
7721 case 0x660f3a20: /* pinsrb */
7722 case 0x660f3a21: /* insertps */
7723 case 0x660f3a22: /* pinsrd pinsrq */
7724 case 0x660f3a40: /* dpps */
7725 case 0x660f3a41: /* dppd */
7726 case 0x660f3a42: /* mpsadbw */
7727 case 0x660f3a60: /* pcmpestrm */
7728 case 0x660f3a61: /* pcmpestri */
7729 case 0x660f3a62: /* pcmpistrm */
7730 case 0x660f3a63: /* pcmpistri */
7731 case 0x0f51: /* sqrtps */
7732 case 0x660f51: /* sqrtpd */
7733 case 0xf20f51: /* sqrtsd */
7734 case 0xf30f51: /* sqrtss */
7735 case 0x0f52: /* rsqrtps */
7736 case 0xf30f52: /* rsqrtss */
7737 case 0x0f53: /* rcpps */
7738 case 0xf30f53: /* rcpss */
7739 case 0x0f54: /* andps */
7740 case 0x660f54: /* andpd */
7741 case 0x0f55: /* andnps */
7742 case 0x660f55: /* andnpd */
7743 case 0x0f56: /* orps */
7744 case 0x660f56: /* orpd */
7745 case 0x0f57: /* xorps */
7746 case 0x660f57: /* xorpd */
7747 case 0x0f58: /* addps */
7748 case 0x660f58: /* addpd */
7749 case 0xf20f58: /* addsd */
7750 case 0xf30f58: /* addss */
7751 case 0x0f59: /* mulps */
7752 case 0x660f59: /* mulpd */
7753 case 0xf20f59: /* mulsd */
7754 case 0xf30f59: /* mulss */
7755 case 0x0f5a: /* cvtps2pd */
7756 case 0x660f5a: /* cvtpd2ps */
7757 case 0xf20f5a: /* cvtsd2ss */
7758 case 0xf30f5a: /* cvtss2sd */
7759 case 0x0f5b: /* cvtdq2ps */
7760 case 0x660f5b: /* cvtps2dq */
7761 case 0xf30f5b: /* cvttps2dq */
7762 case 0x0f5c: /* subps */
7763 case 0x660f5c: /* subpd */
7764 case 0xf20f5c: /* subsd */
7765 case 0xf30f5c: /* subss */
7766 case 0x0f5d: /* minps */
7767 case 0x660f5d: /* minpd */
7768 case 0xf20f5d: /* minsd */
7769 case 0xf30f5d: /* minss */
7770 case 0x0f5e: /* divps */
7771 case 0x660f5e: /* divpd */
7772 case 0xf20f5e: /* divsd */
7773 case 0xf30f5e: /* divss */
7774 case 0x0f5f: /* maxps */
7775 case 0x660f5f: /* maxpd */
7776 case 0xf20f5f: /* maxsd */
7777 case 0xf30f5f: /* maxss */
7778 case 0x660f60: /* punpcklbw */
7779 case 0x660f61: /* punpcklwd */
7780 case 0x660f62: /* punpckldq */
7781 case 0x660f63: /* packsswb */
7782 case 0x660f64: /* pcmpgtb */
7783 case 0x660f65: /* pcmpgtw */
7784 case 0x660f66: /* pcmpgtd */
7785 case 0x660f67: /* packuswb */
7786 case 0x660f68: /* punpckhbw */
7787 case 0x660f69: /* punpckhwd */
7788 case 0x660f6a: /* punpckhdq */
7789 case 0x660f6b: /* packssdw */
7790 case 0x660f6c: /* punpcklqdq */
7791 case 0x660f6d: /* punpckhqdq */
7792 case 0x660f6e: /* movd */
7793 case 0x660f6f: /* movdqa */
7794 case 0xf30f6f: /* movdqu */
7795 case 0x660f70: /* pshufd */
7796 case 0xf20f70: /* pshuflw */
7797 case 0xf30f70: /* pshufhw */
7798 case 0x660f74: /* pcmpeqb */
7799 case 0x660f75: /* pcmpeqw */
7800 case 0x660f76: /* pcmpeqd */
7801 case 0x660f7c: /* haddpd */
7802 case 0xf20f7c: /* haddps */
7803 case 0x660f7d: /* hsubpd */
7804 case 0xf20f7d: /* hsubps */
7805 case 0xf30f7e: /* movq */
7806 case 0x0fc2: /* cmpps */
7807 case 0x660fc2: /* cmppd */
7808 case 0xf20fc2: /* cmpsd */
7809 case 0xf30fc2: /* cmpss */
7810 case 0x660fc4: /* pinsrw */
7811 case 0x0fc6: /* shufps */
7812 case 0x660fc6: /* shufpd */
7813 case 0x660fd0: /* addsubpd */
7814 case 0xf20fd0: /* addsubps */
7815 case 0x660fd1: /* psrlw */
7816 case 0x660fd2: /* psrld */
7817 case 0x660fd3: /* psrlq */
7818 case 0x660fd4: /* paddq */
7819 case 0x660fd5: /* pmullw */
7820 case 0xf30fd6: /* movq2dq */
7821 case 0x660fd8: /* psubusb */
7822 case 0x660fd9: /* psubusw */
7823 case 0x660fda: /* pminub */
7824 case 0x660fdb: /* pand */
7825 case 0x660fdc: /* paddusb */
7826 case 0x660fdd: /* paddusw */
7827 case 0x660fde: /* pmaxub */
7828 case 0x660fdf: /* pandn */
7829 case 0x660fe0: /* pavgb */
7830 case 0x660fe1: /* psraw */
7831 case 0x660fe2: /* psrad */
7832 case 0x660fe3: /* pavgw */
7833 case 0x660fe4: /* pmulhuw */
7834 case 0x660fe5: /* pmulhw */
7835 case 0x660fe6: /* cvttpd2dq */
7836 case 0xf20fe6: /* cvtpd2dq */
7837 case 0xf30fe6: /* cvtdq2pd */
7838 case 0x660fe8: /* psubsb */
7839 case 0x660fe9: /* psubsw */
7840 case 0x660fea: /* pminsw */
7841 case 0x660feb: /* por */
7842 case 0x660fec: /* paddsb */
7843 case 0x660fed: /* paddsw */
7844 case 0x660fee: /* pmaxsw */
7845 case 0x660fef: /* pxor */
7846 case 0xf20ff0: /* lddqu */
7847 case 0x660ff1: /* psllw */
7848 case 0x660ff2: /* pslld */
7849 case 0x660ff3: /* psllq */
7850 case 0x660ff4: /* pmuludq */
7851 case 0x660ff5: /* pmaddwd */
7852 case 0x660ff6: /* psadbw */
7853 case 0x660ff8: /* psubb */
7854 case 0x660ff9: /* psubw */
7855 case 0x660ffa: /* psubd */
7856 case 0x660ffb: /* psubq */
7857 case 0x660ffc: /* paddb */
7858 case 0x660ffd: /* paddw */
7859 case 0x660ffe: /* paddd */
7860 if (i386_record_modrm (&ir))
7861 return -1;
7862 ir.reg |= rex_r;
7863 if (!i386_xmm_regnum_p (gdbarch, I387_XMM0_REGNUM (tdep) + ir.reg))
7864 goto no_support;
7865 record_full_arch_list_add_reg (ir.regcache,
7866 I387_XMM0_REGNUM (tdep) + ir.reg);
7867 if ((opcode & 0xfffffffc) == 0x660f3a60)
7868 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7869 break;
7870
7871 case 0x0f11: /* movups */
7872 case 0x660f11: /* movupd */
7873 case 0xf30f11: /* movss */
7874 case 0xf20f11: /* movsd */
7875 case 0x0f13: /* movlps */
7876 case 0x660f13: /* movlpd */
7877 case 0x0f17: /* movhps */
7878 case 0x660f17: /* movhpd */
7879 case 0x0f29: /* movaps */
7880 case 0x660f29: /* movapd */
7881 case 0x660f3a14: /* pextrb */
7882 case 0x660f3a15: /* pextrw */
7883 case 0x660f3a16: /* pextrd pextrq */
7884 case 0x660f3a17: /* extractps */
7885 case 0x660f7f: /* movdqa */
7886 case 0xf30f7f: /* movdqu */
7887 if (i386_record_modrm (&ir))
7888 return -1;
7889 if (ir.mod == 3)
7890 {
7891 if (opcode == 0x0f13 || opcode == 0x660f13
7892 || opcode == 0x0f17 || opcode == 0x660f17)
7893 goto no_support;
7894 ir.rm |= ir.rex_b;
7895 if (!i386_xmm_regnum_p (gdbarch,
7896 I387_XMM0_REGNUM (tdep) + ir.rm))
7897 goto no_support;
7898 record_full_arch_list_add_reg (ir.regcache,
7899 I387_XMM0_REGNUM (tdep) + ir.rm);
7900 }
7901 else
7902 {
7903 switch (opcode)
7904 {
7905 case 0x660f3a14:
7906 ir.ot = OT_BYTE;
7907 break;
7908 case 0x660f3a15:
7909 ir.ot = OT_WORD;
7910 break;
7911 case 0x660f3a16:
7912 ir.ot = OT_LONG;
7913 break;
7914 case 0x660f3a17:
7915 ir.ot = OT_QUAD;
7916 break;
7917 default:
7918 ir.ot = OT_DQUAD;
7919 break;
7920 }
7921 if (i386_record_lea_modrm (&ir))
7922 return -1;
7923 }
7924 break;
7925
7926 case 0x0f2b: /* movntps */
7927 case 0x660f2b: /* movntpd */
7928 case 0x0fe7: /* movntq */
7929 case 0x660fe7: /* movntdq */
7930 if (ir.mod == 3)
7931 goto no_support;
7932 if (opcode == 0x0fe7)
7933 ir.ot = OT_QUAD;
7934 else
7935 ir.ot = OT_DQUAD;
7936 if (i386_record_lea_modrm (&ir))
7937 return -1;
7938 break;
7939
7940 case 0xf30f2c: /* cvttss2si */
7941 case 0xf20f2c: /* cvttsd2si */
7942 case 0xf30f2d: /* cvtss2si */
7943 case 0xf20f2d: /* cvtsd2si */
7944 case 0xf20f38f0: /* crc32 */
7945 case 0xf20f38f1: /* crc32 */
7946 case 0x0f50: /* movmskps */
7947 case 0x660f50: /* movmskpd */
7948 case 0x0fc5: /* pextrw */
7949 case 0x660fc5: /* pextrw */
7950 case 0x0fd7: /* pmovmskb */
7951 case 0x660fd7: /* pmovmskb */
7952 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
7953 break;
7954
7955 case 0x0f3800: /* pshufb */
7956 case 0x0f3801: /* phaddw */
7957 case 0x0f3802: /* phaddd */
7958 case 0x0f3803: /* phaddsw */
7959 case 0x0f3804: /* pmaddubsw */
7960 case 0x0f3805: /* phsubw */
7961 case 0x0f3806: /* phsubd */
7962 case 0x0f3807: /* phsubsw */
7963 case 0x0f3808: /* psignb */
7964 case 0x0f3809: /* psignw */
7965 case 0x0f380a: /* psignd */
7966 case 0x0f380b: /* pmulhrsw */
7967 case 0x0f381c: /* pabsb */
7968 case 0x0f381d: /* pabsw */
7969 case 0x0f381e: /* pabsd */
7970 case 0x0f382b: /* packusdw */
7971 case 0x0f3830: /* pmovzxbw */
7972 case 0x0f3831: /* pmovzxbd */
7973 case 0x0f3832: /* pmovzxbq */
7974 case 0x0f3833: /* pmovzxwd */
7975 case 0x0f3834: /* pmovzxwq */
7976 case 0x0f3835: /* pmovzxdq */
7977 case 0x0f3837: /* pcmpgtq */
7978 case 0x0f3838: /* pminsb */
7979 case 0x0f3839: /* pminsd */
7980 case 0x0f383a: /* pminuw */
7981 case 0x0f383b: /* pminud */
7982 case 0x0f383c: /* pmaxsb */
7983 case 0x0f383d: /* pmaxsd */
7984 case 0x0f383e: /* pmaxuw */
7985 case 0x0f383f: /* pmaxud */
7986 case 0x0f3840: /* pmulld */
7987 case 0x0f3841: /* phminposuw */
7988 case 0x0f3a0f: /* palignr */
7989 case 0x0f60: /* punpcklbw */
7990 case 0x0f61: /* punpcklwd */
7991 case 0x0f62: /* punpckldq */
7992 case 0x0f63: /* packsswb */
7993 case 0x0f64: /* pcmpgtb */
7994 case 0x0f65: /* pcmpgtw */
7995 case 0x0f66: /* pcmpgtd */
7996 case 0x0f67: /* packuswb */
7997 case 0x0f68: /* punpckhbw */
7998 case 0x0f69: /* punpckhwd */
7999 case 0x0f6a: /* punpckhdq */
8000 case 0x0f6b: /* packssdw */
8001 case 0x0f6e: /* movd */
8002 case 0x0f6f: /* movq */
8003 case 0x0f70: /* pshufw */
8004 case 0x0f74: /* pcmpeqb */
8005 case 0x0f75: /* pcmpeqw */
8006 case 0x0f76: /* pcmpeqd */
8007 case 0x0fc4: /* pinsrw */
8008 case 0x0fd1: /* psrlw */
8009 case 0x0fd2: /* psrld */
8010 case 0x0fd3: /* psrlq */
8011 case 0x0fd4: /* paddq */
8012 case 0x0fd5: /* pmullw */
8013 case 0xf20fd6: /* movdq2q */
8014 case 0x0fd8: /* psubusb */
8015 case 0x0fd9: /* psubusw */
8016 case 0x0fda: /* pminub */
8017 case 0x0fdb: /* pand */
8018 case 0x0fdc: /* paddusb */
8019 case 0x0fdd: /* paddusw */
8020 case 0x0fde: /* pmaxub */
8021 case 0x0fdf: /* pandn */
8022 case 0x0fe0: /* pavgb */
8023 case 0x0fe1: /* psraw */
8024 case 0x0fe2: /* psrad */
8025 case 0x0fe3: /* pavgw */
8026 case 0x0fe4: /* pmulhuw */
8027 case 0x0fe5: /* pmulhw */
8028 case 0x0fe8: /* psubsb */
8029 case 0x0fe9: /* psubsw */
8030 case 0x0fea: /* pminsw */
8031 case 0x0feb: /* por */
8032 case 0x0fec: /* paddsb */
8033 case 0x0fed: /* paddsw */
8034 case 0x0fee: /* pmaxsw */
8035 case 0x0fef: /* pxor */
8036 case 0x0ff1: /* psllw */
8037 case 0x0ff2: /* pslld */
8038 case 0x0ff3: /* psllq */
8039 case 0x0ff4: /* pmuludq */
8040 case 0x0ff5: /* pmaddwd */
8041 case 0x0ff6: /* psadbw */
8042 case 0x0ff8: /* psubb */
8043 case 0x0ff9: /* psubw */
8044 case 0x0ffa: /* psubd */
8045 case 0x0ffb: /* psubq */
8046 case 0x0ffc: /* paddb */
8047 case 0x0ffd: /* paddw */
8048 case 0x0ffe: /* paddd */
8049 if (i386_record_modrm (&ir))
8050 return -1;
8051 if (!i386_mmx_regnum_p (gdbarch, I387_MM0_REGNUM (tdep) + ir.reg))
8052 goto no_support;
8053 record_full_arch_list_add_reg (ir.regcache,
8054 I387_MM0_REGNUM (tdep) + ir.reg);
8055 break;
8056
8057 case 0x0f71: /* psllw */
8058 case 0x0f72: /* pslld */
8059 case 0x0f73: /* psllq */
8060 if (i386_record_modrm (&ir))
8061 return -1;
8062 if (!i386_mmx_regnum_p (gdbarch, I387_MM0_REGNUM (tdep) + ir.rm))
8063 goto no_support;
8064 record_full_arch_list_add_reg (ir.regcache,
8065 I387_MM0_REGNUM (tdep) + ir.rm);
8066 break;
8067
8068 case 0x660f71: /* psllw */
8069 case 0x660f72: /* pslld */
8070 case 0x660f73: /* psllq */
8071 if (i386_record_modrm (&ir))
8072 return -1;
8073 ir.rm |= ir.rex_b;
8074 if (!i386_xmm_regnum_p (gdbarch, I387_XMM0_REGNUM (tdep) + ir.rm))
8075 goto no_support;
8076 record_full_arch_list_add_reg (ir.regcache,
8077 I387_XMM0_REGNUM (tdep) + ir.rm);
8078 break;
8079
8080 case 0x0f7e: /* movd */
8081 case 0x660f7e: /* movd */
8082 if (i386_record_modrm (&ir))
8083 return -1;
8084 if (ir.mod == 3)
8085 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
8086 else
8087 {
8088 if (ir.dflag == 2)
8089 ir.ot = OT_QUAD;
8090 else
8091 ir.ot = OT_LONG;
8092 if (i386_record_lea_modrm (&ir))
8093 return -1;
8094 }
8095 break;
8096
8097 case 0x0f7f: /* movq */
8098 if (i386_record_modrm (&ir))
8099 return -1;
8100 if (ir.mod == 3)
8101 {
8102 if (!i386_mmx_regnum_p (gdbarch, I387_MM0_REGNUM (tdep) + ir.rm))
8103 goto no_support;
8104 record_full_arch_list_add_reg (ir.regcache,
8105 I387_MM0_REGNUM (tdep) + ir.rm);
8106 }
8107 else
8108 {
8109 ir.ot = OT_QUAD;
8110 if (i386_record_lea_modrm (&ir))
8111 return -1;
8112 }
8113 break;
8114
8115 case 0xf30fb8: /* popcnt */
8116 if (i386_record_modrm (&ir))
8117 return -1;
8118 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
8119 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
8120 break;
8121
8122 case 0x660fd6: /* movq */
8123 if (i386_record_modrm (&ir))
8124 return -1;
8125 if (ir.mod == 3)
8126 {
8127 ir.rm |= ir.rex_b;
8128 if (!i386_xmm_regnum_p (gdbarch,
8129 I387_XMM0_REGNUM (tdep) + ir.rm))
8130 goto no_support;
8131 record_full_arch_list_add_reg (ir.regcache,
8132 I387_XMM0_REGNUM (tdep) + ir.rm);
8133 }
8134 else
8135 {
8136 ir.ot = OT_QUAD;
8137 if (i386_record_lea_modrm (&ir))
8138 return -1;
8139 }
8140 break;
8141
8142 case 0x660f3817: /* ptest */
8143 case 0x0f2e: /* ucomiss */
8144 case 0x660f2e: /* ucomisd */
8145 case 0x0f2f: /* comiss */
8146 case 0x660f2f: /* comisd */
8147 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
8148 break;
8149
8150 case 0x0ff7: /* maskmovq */
8151 regcache_raw_read_unsigned (ir.regcache,
8152 ir.regmap[X86_RECORD_REDI_REGNUM],
8153 &addr);
8154 if (record_full_arch_list_add_mem (addr, 64))
8155 return -1;
8156 break;
8157
8158 case 0x660ff7: /* maskmovdqu */
8159 regcache_raw_read_unsigned (ir.regcache,
8160 ir.regmap[X86_RECORD_REDI_REGNUM],
8161 &addr);
8162 if (record_full_arch_list_add_mem (addr, 128))
8163 return -1;
8164 break;
8165
8166 default:
8167 goto no_support;
8168 break;
8169 }
8170 break;
8171
8172 default:
8173 goto no_support;
8174 break;
8175 }
8176
8177 /* In the future, maybe still need to deal with need_dasm. */
8178 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REIP_REGNUM);
8179 if (record_full_arch_list_add_end ())
8180 return -1;
8181
8182 return 0;
8183
8184 no_support:
8185 gdb_printf (gdb_stderr,
8186 _("Process record does not support instruction 0x%02x "
8187 "at address %s.\n"),
8188 (unsigned int) (opcode),
8189 paddress (gdbarch, ir.orig_addr));
8190 return -1;
8191 }
8192
8193 static const int i386_record_regmap[] =
8194 {
8195 I386_EAX_REGNUM, I386_ECX_REGNUM, I386_EDX_REGNUM, I386_EBX_REGNUM,
8196 I386_ESP_REGNUM, I386_EBP_REGNUM, I386_ESI_REGNUM, I386_EDI_REGNUM,
8197 0, 0, 0, 0,
8198 0, 0, 0, 0,
8199 I386_EIP_REGNUM, I386_EFLAGS_REGNUM, I386_CS_REGNUM, I386_SS_REGNUM,
8200 I386_DS_REGNUM, I386_ES_REGNUM, I386_FS_REGNUM, I386_GS_REGNUM,
8201 /* xmm0_regnum */ 0
8202 };
8203
8204 /* Check that the given address appears suitable for a fast
8205 tracepoint, which on x86-64 means that we need an instruction of at
8206 least 5 bytes, so that we can overwrite it with a 4-byte-offset
8207 jump and not have to worry about program jumps to an address in the
8208 middle of the tracepoint jump. On x86, it may be possible to use
8209 4-byte jumps with a 2-byte offset to a trampoline located in the
8210 bottom 64 KiB of memory. Returns 1 if OK, and writes a size
8211 of instruction to replace, and 0 if not, plus an explanatory
8212 string. */
8213
8214 static int
8215 i386_fast_tracepoint_valid_at (struct gdbarch *gdbarch, CORE_ADDR addr,
8216 std::string *msg)
8217 {
8218 int len, jumplen;
8219
8220 /* Ask the target for the minimum instruction length supported. */
8221 jumplen = target_get_min_fast_tracepoint_insn_len ();
8222
8223 if (jumplen < 0)
8224 {
8225 /* If the target does not support the get_min_fast_tracepoint_insn_len
8226 operation, assume that fast tracepoints will always be implemented
8227 using 4-byte relative jumps on both x86 and x86-64. */
8228 jumplen = 5;
8229 }
8230 else if (jumplen == 0)
8231 {
8232 /* If the target does support get_min_fast_tracepoint_insn_len but
8233 returns zero, then the IPA has not loaded yet. In this case,
8234 we optimistically assume that truncated 2-byte relative jumps
8235 will be available on x86, and compensate later if this assumption
8236 turns out to be incorrect. On x86-64 architectures, 4-byte relative
8237 jumps will always be used. */
8238 jumplen = (register_size (gdbarch, 0) == 8) ? 5 : 4;
8239 }
8240
8241 /* Check for fit. */
8242 len = gdb_insn_length (gdbarch, addr);
8243
8244 if (len < jumplen)
8245 {
8246 /* Return a bit of target-specific detail to add to the caller's
8247 generic failure message. */
8248 if (msg)
8249 *msg = string_printf (_("; instruction is only %d bytes long, "
8250 "need at least %d bytes for the jump"),
8251 len, jumplen);
8252 return 0;
8253 }
8254 else
8255 {
8256 if (msg)
8257 msg->clear ();
8258 return 1;
8259 }
8260 }
8261
8262 /* Return a floating-point format for a floating-point variable of
8263 length LEN in bits. If non-NULL, NAME is the name of its type.
8264 If no suitable type is found, return NULL. */
8265
8266 static const struct floatformat **
8267 i386_floatformat_for_type (struct gdbarch *gdbarch,
8268 const char *name, int len)
8269 {
8270 if (len == 128 && name)
8271 if (strcmp (name, "__float128") == 0
8272 || strcmp (name, "_Float128") == 0
8273 || strcmp (name, "complex _Float128") == 0
8274 || strcmp (name, "complex(kind=16)") == 0
8275 || strcmp (name, "COMPLEX(16)") == 0
8276 || strcmp (name, "complex*32") == 0
8277 || strcmp (name, "COMPLEX*32") == 0
8278 || strcmp (name, "quad complex") == 0
8279 || strcmp (name, "real(kind=16)") == 0
8280 || strcmp (name, "real*16") == 0
8281 || strcmp (name, "REAL*16") == 0
8282 || strcmp (name, "REAL(16)") == 0)
8283 return floatformats_ieee_quad;
8284
8285 return default_floatformat_for_type (gdbarch, name, len);
8286 }
8287
8288 /* Compute an XCR0 mask based on a target description. */
8289
8290 static uint64_t
8291 i386_xcr0_from_tdesc (const struct target_desc *tdesc)
8292 {
8293 if (! tdesc_has_registers (tdesc))
8294 return 0;
8295
8296 const struct tdesc_feature *feature_core;
8297
8298 const struct tdesc_feature *feature_sse, *feature_avx,
8299 *feature_avx512, *feature_pkeys;
8300
8301 /* Get core registers. */
8302 feature_core = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.core");
8303 if (feature_core == NULL)
8304 return 0;
8305
8306 /* Get SSE registers. */
8307 feature_sse = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.sse");
8308
8309 /* Try AVX registers. */
8310 feature_avx = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx");
8311
8312 /* Try AVX512 registers. */
8313 feature_avx512 = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx512");
8314
8315 /* Try PKEYS */
8316 feature_pkeys = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.pkeys");
8317
8318 /* The XCR0 bits. */
8319 uint64_t xcr0 = X86_XSTATE_X87;
8320
8321 if (feature_sse)
8322 xcr0 |= X86_XSTATE_SSE;
8323
8324 if (feature_avx)
8325 {
8326 /* AVX register description requires SSE register description. */
8327 if (!feature_sse)
8328 return 0;
8329
8330 xcr0 |= X86_XSTATE_AVX;
8331 }
8332
8333 if (feature_avx512)
8334 {
8335 /* AVX512 register description requires AVX register description. */
8336 if (!feature_avx)
8337 return 0;
8338
8339 xcr0 |= X86_XSTATE_AVX512;
8340 }
8341
8342 if (feature_pkeys)
8343 xcr0 |= X86_XSTATE_PKRU;
8344
8345 return xcr0;
8346 }
8347
8348 static int
8349 i386_validate_tdesc_p (i386_gdbarch_tdep *tdep,
8350 struct tdesc_arch_data *tdesc_data)
8351 {
8352 const struct target_desc *tdesc = tdep->tdesc;
8353 const struct tdesc_feature *feature_core;
8354
8355 const struct tdesc_feature *feature_sse, *feature_avx, *feature_avx512,
8356 *feature_pkeys, *feature_segments;
8357 int i, num_regs, valid_p;
8358
8359 if (! tdesc_has_registers (tdesc))
8360 return 0;
8361
8362 /* Get core registers. */
8363 feature_core = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.core");
8364 if (feature_core == NULL)
8365 return 0;
8366
8367 /* Get SSE registers. */
8368 feature_sse = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.sse");
8369
8370 /* Try AVX registers. */
8371 feature_avx = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx");
8372
8373 /* Try AVX512 registers. */
8374 feature_avx512 = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx512");
8375
8376 /* Try segment base registers. */
8377 feature_segments = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.segments");
8378
8379 /* Try PKEYS */
8380 feature_pkeys = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.pkeys");
8381
8382 valid_p = 1;
8383
8384 /* The XCR0 bits. */
8385 if (feature_avx512)
8386 {
8387 /* AVX512 register description requires AVX register description. */
8388 if (!feature_avx)
8389 return 0;
8390
8391 tdep->xcr0 = X86_XSTATE_AVX_AVX512_MASK;
8392
8393 /* It may have been set by OSABI initialization function. */
8394 if (tdep->k0_regnum < 0)
8395 {
8396 tdep->k_register_names = i386_k_names;
8397 tdep->k0_regnum = I386_K0_REGNUM;
8398 }
8399
8400 for (i = 0; i < I387_NUM_K_REGS; i++)
8401 valid_p &= tdesc_numbered_register (feature_avx512, tdesc_data,
8402 tdep->k0_regnum + i,
8403 i386_k_names[i]);
8404
8405 if (tdep->num_zmm_regs == 0)
8406 {
8407 tdep->zmmh_register_names = i386_zmmh_names;
8408 tdep->num_zmm_regs = 8;
8409 tdep->zmm0h_regnum = I386_ZMM0H_REGNUM;
8410 }
8411
8412 for (i = 0; i < tdep->num_zmm_regs; i++)
8413 valid_p &= tdesc_numbered_register (feature_avx512, tdesc_data,
8414 tdep->zmm0h_regnum + i,
8415 tdep->zmmh_register_names[i]);
8416
8417 for (i = 0; i < tdep->num_xmm_avx512_regs; i++)
8418 valid_p &= tdesc_numbered_register (feature_avx512, tdesc_data,
8419 tdep->xmm16_regnum + i,
8420 tdep->xmm_avx512_register_names[i]);
8421
8422 for (i = 0; i < tdep->num_ymm_avx512_regs; i++)
8423 valid_p &= tdesc_numbered_register (feature_avx512, tdesc_data,
8424 tdep->ymm16h_regnum + i,
8425 tdep->ymm16h_register_names[i]);
8426 }
8427 if (feature_avx)
8428 {
8429 /* AVX register description requires SSE register description. */
8430 if (!feature_sse)
8431 return 0;
8432
8433 if (!feature_avx512)
8434 tdep->xcr0 = X86_XSTATE_AVX_MASK;
8435
8436 /* It may have been set by OSABI initialization function. */
8437 if (tdep->num_ymm_regs == 0)
8438 {
8439 tdep->ymmh_register_names = i386_ymmh_names;
8440 tdep->num_ymm_regs = 8;
8441 tdep->ymm0h_regnum = I386_YMM0H_REGNUM;
8442 }
8443
8444 for (i = 0; i < tdep->num_ymm_regs; i++)
8445 valid_p &= tdesc_numbered_register (feature_avx, tdesc_data,
8446 tdep->ymm0h_regnum + i,
8447 tdep->ymmh_register_names[i]);
8448 }
8449 else if (feature_sse)
8450 tdep->xcr0 = X86_XSTATE_SSE_MASK;
8451 else
8452 {
8453 tdep->xcr0 = X86_XSTATE_X87_MASK;
8454 tdep->num_xmm_regs = 0;
8455 }
8456
8457 num_regs = tdep->num_core_regs;
8458 for (i = 0; i < num_regs; i++)
8459 valid_p &= tdesc_numbered_register (feature_core, tdesc_data, i,
8460 tdep->register_names[i]);
8461
8462 if (feature_sse)
8463 {
8464 /* Need to include %mxcsr, so add one. */
8465 num_regs += tdep->num_xmm_regs + 1;
8466 for (; i < num_regs; i++)
8467 valid_p &= tdesc_numbered_register (feature_sse, tdesc_data, i,
8468 tdep->register_names[i]);
8469 }
8470
8471 if (feature_segments)
8472 {
8473 if (tdep->fsbase_regnum < 0)
8474 tdep->fsbase_regnum = I386_FSBASE_REGNUM;
8475 valid_p &= tdesc_numbered_register (feature_segments, tdesc_data,
8476 tdep->fsbase_regnum, "fs_base");
8477 valid_p &= tdesc_numbered_register (feature_segments, tdesc_data,
8478 tdep->fsbase_regnum + 1, "gs_base");
8479 }
8480
8481 if (feature_pkeys)
8482 {
8483 tdep->xcr0 |= X86_XSTATE_PKRU;
8484 if (tdep->pkru_regnum < 0)
8485 {
8486 tdep->pkeys_register_names = i386_pkeys_names;
8487 tdep->pkru_regnum = I386_PKRU_REGNUM;
8488 tdep->num_pkeys_regs = 1;
8489 }
8490
8491 for (i = 0; i < I387_NUM_PKEYS_REGS; i++)
8492 valid_p &= tdesc_numbered_register (feature_pkeys, tdesc_data,
8493 I387_PKRU_REGNUM (tdep) + i,
8494 tdep->pkeys_register_names[i]);
8495 }
8496
8497 return valid_p;
8498 }
8499
8500 \f
8501
8502 /* Implement the type_align gdbarch function. */
8503
8504 static ULONGEST
8505 i386_type_align (struct gdbarch *gdbarch, struct type *type)
8506 {
8507 type = check_typedef (type);
8508
8509 if (gdbarch_ptr_bit (gdbarch) == 32)
8510 {
8511 if ((type->code () == TYPE_CODE_INT
8512 || type->code () == TYPE_CODE_FLT)
8513 && type->length () > 4)
8514 return 4;
8515
8516 /* Handle x86's funny long double. */
8517 if (type->code () == TYPE_CODE_FLT
8518 && gdbarch_long_double_bit (gdbarch) == type->length () * 8)
8519 return 4;
8520 }
8521
8522 return 0;
8523 }
8524
8525 \f
8526 /* Note: This is called for both i386 and amd64. */
8527
8528 static struct gdbarch *
8529 i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
8530 {
8531 const struct target_desc *tdesc;
8532 int mm0_regnum;
8533 int ymm0_regnum;
8534
8535 x86_xsave_layout xsave_layout = target_fetch_x86_xsave_layout ();
8536
8537 /* If the target did not provide an XSAVE layout but the target
8538 description includes registers from the XSAVE extended region,
8539 use a fallback XSAVE layout. Specifically, this fallback layout
8540 is used when writing out a local core dump for a remote
8541 target. */
8542 if (xsave_layout.sizeof_xsave == 0)
8543 xsave_layout
8544 = i387_fallback_xsave_layout (i386_xcr0_from_tdesc (info.target_desc));
8545
8546 /* If there is already a candidate, use it. */
8547 for (arches = gdbarch_list_lookup_by_info (arches, &info);
8548 arches != NULL;
8549 arches = gdbarch_list_lookup_by_info (arches->next, &info))
8550 {
8551 /* Check that the XSAVE layout of ARCHES matches the layout for
8552 the current target. */
8553 i386_gdbarch_tdep *other_tdep
8554 = gdbarch_tdep<i386_gdbarch_tdep> (arches->gdbarch);
8555
8556 if (other_tdep->xsave_layout == xsave_layout)
8557 return arches->gdbarch;
8558 }
8559
8560 /* Allocate space for the new architecture. Assume i386 for now. */
8561 gdbarch *gdbarch
8562 = gdbarch_alloc (&info, gdbarch_tdep_up (new i386_gdbarch_tdep));
8563 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
8564
8565 /* General-purpose registers. */
8566 tdep->gregset_reg_offset = NULL;
8567 tdep->gregset_num_regs = I386_NUM_GREGS;
8568 tdep->sizeof_gregset = 0;
8569
8570 /* Floating-point registers. */
8571 tdep->sizeof_fpregset = I387_SIZEOF_FSAVE;
8572 tdep->fpregset = &i386_fpregset;
8573
8574 /* The default settings include the FPU registers, the MMX registers
8575 and the SSE registers. This can be overridden for a specific ABI
8576 by adjusting the members `st0_regnum', `mm0_regnum' and
8577 `num_xmm_regs' of `struct gdbarch_tdep', otherwise the registers
8578 will show up in the output of "info all-registers". */
8579
8580 tdep->st0_regnum = I386_ST0_REGNUM;
8581
8582 /* I386_NUM_XREGS includes %mxcsr, so subtract one. */
8583 tdep->num_xmm_regs = I386_NUM_XREGS - 1;
8584
8585 tdep->jb_pc_offset = -1;
8586 tdep->struct_return = pcc_struct_return;
8587 tdep->sigtramp_start = 0;
8588 tdep->sigtramp_end = 0;
8589 tdep->sigtramp_p = i386_sigtramp_p;
8590 tdep->sigcontext_addr = NULL;
8591 tdep->sc_reg_offset = NULL;
8592 tdep->sc_pc_offset = -1;
8593 tdep->sc_sp_offset = -1;
8594
8595 tdep->xsave_xcr0_offset = -1;
8596
8597 tdep->record_regmap = i386_record_regmap;
8598
8599 set_gdbarch_type_align (gdbarch, i386_type_align);
8600
8601 /* The format used for `long double' on almost all i386 targets is
8602 the i387 extended floating-point format. In fact, of all targets
8603 in the GCC 2.95 tree, only OSF/1 does it different, and insists
8604 on having a `long double' that's not `long' at all. */
8605 set_gdbarch_long_double_format (gdbarch, floatformats_i387_ext);
8606
8607 /* Although the i387 extended floating-point has only 80 significant
8608 bits, a `long double' actually takes up 96, probably to enforce
8609 alignment. */
8610 set_gdbarch_long_double_bit (gdbarch, 96);
8611
8612 /* Support of bfloat16 format. */
8613 set_gdbarch_bfloat16_format (gdbarch, floatformats_bfloat16);
8614
8615 /* Support for floating-point data type variants. */
8616 set_gdbarch_floatformat_for_type (gdbarch, i386_floatformat_for_type);
8617
8618 /* Register numbers of various important registers. */
8619 set_gdbarch_sp_regnum (gdbarch, I386_ESP_REGNUM); /* %esp */
8620 set_gdbarch_pc_regnum (gdbarch, I386_EIP_REGNUM); /* %eip */
8621 set_gdbarch_ps_regnum (gdbarch, I386_EFLAGS_REGNUM); /* %eflags */
8622 set_gdbarch_fp0_regnum (gdbarch, I386_ST0_REGNUM); /* %st(0) */
8623
8624 /* NOTE: kettenis/20040418: GCC does have two possible register
8625 numbering schemes on the i386: dbx and SVR4. These schemes
8626 differ in how they number %ebp, %esp, %eflags, and the
8627 floating-point registers, and are implemented by the arrays
8628 dbx_register_map[] and svr4_dbx_register_map in
8629 gcc/config/i386.c. GCC also defines a third numbering scheme in
8630 gcc/config/i386.c, which it designates as the "default" register
8631 map used in 64bit mode. This last register numbering scheme is
8632 implemented in dbx64_register_map, and is used for AMD64; see
8633 amd64-tdep.c.
8634
8635 Currently, each GCC i386 target always uses the same register
8636 numbering scheme across all its supported debugging formats
8637 i.e. SDB (COFF), stabs and DWARF 2. This is because
8638 gcc/sdbout.c, gcc/dbxout.c and gcc/dwarf2out.c all use the
8639 DBX_REGISTER_NUMBER macro which is defined by each target's
8640 respective config header in a manner independent of the requested
8641 output debugging format.
8642
8643 This does not match the arrangement below, which presumes that
8644 the SDB and stabs numbering schemes differ from the DWARF and
8645 DWARF 2 ones. The reason for this arrangement is that it is
8646 likely to get the numbering scheme for the target's
8647 default/native debug format right. For targets where GCC is the
8648 native compiler (FreeBSD, NetBSD, OpenBSD, GNU/Linux) or for
8649 targets where the native toolchain uses a different numbering
8650 scheme for a particular debug format (stabs-in-ELF on Solaris)
8651 the defaults below will have to be overridden, like
8652 i386_elf_init_abi() does. */
8653
8654 /* Use the dbx register numbering scheme for stabs and COFF. */
8655 set_gdbarch_stab_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum);
8656 set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum);
8657
8658 /* Use the SVR4 register numbering scheme for DWARF 2. */
8659 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, i386_svr4_dwarf_reg_to_regnum);
8660
8661 /* We don't set gdbarch_stab_reg_to_regnum, since ECOFF doesn't seem to
8662 be in use on any of the supported i386 targets. */
8663
8664 set_gdbarch_print_float_info (gdbarch, i387_print_float_info);
8665
8666 set_gdbarch_get_longjmp_target (gdbarch, i386_get_longjmp_target);
8667
8668 /* Call dummy code. */
8669 set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
8670 set_gdbarch_push_dummy_code (gdbarch, i386_push_dummy_code);
8671 set_gdbarch_push_dummy_call (gdbarch, i386_push_dummy_call);
8672 set_gdbarch_frame_align (gdbarch, i386_frame_align);
8673
8674 set_gdbarch_convert_register_p (gdbarch, i386_convert_register_p);
8675 set_gdbarch_register_to_value (gdbarch, i386_register_to_value);
8676 set_gdbarch_value_to_register (gdbarch, i386_value_to_register);
8677
8678 set_gdbarch_return_value_as_value (gdbarch, i386_return_value);
8679
8680 set_gdbarch_skip_prologue (gdbarch, i386_skip_prologue);
8681
8682 /* Stack grows downward. */
8683 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
8684
8685 set_gdbarch_breakpoint_kind_from_pc (gdbarch, i386_breakpoint::kind_from_pc);
8686 set_gdbarch_sw_breakpoint_from_kind (gdbarch, i386_breakpoint::bp_from_kind);
8687
8688 set_gdbarch_decr_pc_after_break (gdbarch, 1);
8689 set_gdbarch_max_insn_length (gdbarch, I386_MAX_INSN_LEN);
8690
8691 set_gdbarch_frame_args_skip (gdbarch, 8);
8692
8693 set_gdbarch_print_insn (gdbarch, i386_print_insn);
8694
8695 set_gdbarch_dummy_id (gdbarch, i386_dummy_id);
8696
8697 set_gdbarch_unwind_pc (gdbarch, i386_unwind_pc);
8698
8699 /* Add the i386 register groups. */
8700 i386_add_reggroups (gdbarch);
8701 tdep->register_reggroup_p = i386_register_reggroup_p;
8702
8703 /* Helper for function argument information. */
8704 set_gdbarch_fetch_pointer_argument (gdbarch, i386_fetch_pointer_argument);
8705
8706 /* Hook the function epilogue frame unwinder. This unwinder is
8707 appended to the list first, so that it supersedes the DWARF
8708 unwinder in function epilogues (where the DWARF unwinder
8709 currently fails). */
8710 if (info.bfd_arch_info->bits_per_word == 32)
8711 frame_unwind_append_unwinder (gdbarch, &i386_epilogue_override_frame_unwind);
8712
8713 /* Hook in the DWARF CFI frame unwinder. This unwinder is appended
8714 to the list before the prologue-based unwinders, so that DWARF
8715 CFI info will be used if it is available. */
8716 dwarf2_append_unwinders (gdbarch);
8717
8718 if (info.bfd_arch_info->bits_per_word == 32)
8719 frame_unwind_append_unwinder (gdbarch, &i386_epilogue_frame_unwind);
8720
8721 frame_base_set_default (gdbarch, &i386_frame_base);
8722
8723 /* Pseudo registers may be changed by amd64_init_abi. */
8724 set_gdbarch_pseudo_register_read_value (gdbarch,
8725 i386_pseudo_register_read_value);
8726 set_gdbarch_pseudo_register_write (gdbarch, i386_pseudo_register_write);
8727 set_gdbarch_ax_pseudo_register_collect (gdbarch,
8728 i386_ax_pseudo_register_collect);
8729
8730 set_tdesc_pseudo_register_type (gdbarch, i386_pseudo_register_type);
8731 set_tdesc_pseudo_register_name (gdbarch, i386_pseudo_register_name);
8732
8733 /* Override the normal target description method to make the AVX
8734 upper halves anonymous. */
8735 set_gdbarch_register_name (gdbarch, i386_register_name);
8736
8737 /* Even though the default ABI only includes general-purpose registers,
8738 floating-point registers and the SSE registers, we have to leave a
8739 gap for the upper AVX, (deprecated) MPX and AVX512 registers. */
8740 set_gdbarch_num_regs (gdbarch, I386_NUM_REGS);
8741
8742 set_gdbarch_gnu_triplet_regexp (gdbarch, i386_gnu_triplet_regexp);
8743
8744 /* Get the x86 target description from INFO. */
8745 tdesc = info.target_desc;
8746 if (! tdesc_has_registers (tdesc))
8747 tdesc = i386_target_description (X86_XSTATE_SSE_MASK, false);
8748 tdep->tdesc = tdesc;
8749
8750 tdep->num_core_regs = I386_NUM_GREGS + I387_NUM_REGS;
8751 tdep->register_names = i386_register_names;
8752
8753 /* No upper YMM registers. */
8754 tdep->ymmh_register_names = NULL;
8755 tdep->ymm0h_regnum = -1;
8756
8757 /* No upper ZMM registers. */
8758 tdep->zmmh_register_names = NULL;
8759 tdep->zmm0h_regnum = -1;
8760
8761 /* No high XMM registers. */
8762 tdep->xmm_avx512_register_names = NULL;
8763 tdep->xmm16_regnum = -1;
8764
8765 /* No upper YMM16-31 registers. */
8766 tdep->ymm16h_register_names = NULL;
8767 tdep->ymm16h_regnum = -1;
8768
8769 tdep->num_byte_regs = 8;
8770 tdep->num_word_regs = 8;
8771 tdep->num_dword_regs = 0;
8772 tdep->num_mmx_regs = 8;
8773 tdep->num_ymm_regs = 0;
8774
8775 /* No AVX512 registers. */
8776 tdep->k0_regnum = -1;
8777 tdep->num_zmm_regs = 0;
8778 tdep->num_ymm_avx512_regs = 0;
8779 tdep->num_xmm_avx512_regs = 0;
8780
8781 /* No PKEYS registers */
8782 tdep->pkru_regnum = -1;
8783 tdep->num_pkeys_regs = 0;
8784
8785 /* No segment base registers. */
8786 tdep->fsbase_regnum = -1;
8787
8788 tdesc_arch_data_up tdesc_data = tdesc_data_alloc ();
8789
8790 set_gdbarch_relocate_instruction (gdbarch, i386_relocate_instruction);
8791
8792 set_gdbarch_gen_return_address (gdbarch, i386_gen_return_address);
8793
8794 set_gdbarch_insn_is_call (gdbarch, i386_insn_is_call);
8795 set_gdbarch_insn_is_ret (gdbarch, i386_insn_is_ret);
8796 set_gdbarch_insn_is_jump (gdbarch, i386_insn_is_jump);
8797
8798 /* Hook in ABI-specific overrides, if they have been registered.
8799 Note: If INFO specifies a 64 bit arch, this is where we turn
8800 a 32-bit i386 into a 64-bit amd64. */
8801 info.tdesc_data = tdesc_data.get ();
8802 gdbarch_init_osabi (info, gdbarch);
8803
8804 if (!i386_validate_tdesc_p (tdep, tdesc_data.get ()))
8805 {
8806 gdbarch_free (gdbarch);
8807 return NULL;
8808 }
8809 tdep->xsave_layout = xsave_layout;
8810
8811 /* Wire in pseudo registers. Number of pseudo registers may be
8812 changed. */
8813 set_gdbarch_num_pseudo_regs (gdbarch, (tdep->num_byte_regs
8814 + tdep->num_word_regs
8815 + tdep->num_dword_regs
8816 + tdep->num_mmx_regs
8817 + tdep->num_ymm_regs
8818 + tdep->num_ymm_avx512_regs
8819 + tdep->num_zmm_regs));
8820
8821 /* Target description may be changed. */
8822 tdesc = tdep->tdesc;
8823
8824 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
8825
8826 /* Override gdbarch_register_reggroup_p set in tdesc_use_registers. */
8827 set_gdbarch_register_reggroup_p (gdbarch, tdep->register_reggroup_p);
8828
8829 /* Make %al the first pseudo-register. */
8830 tdep->al_regnum = gdbarch_num_regs (gdbarch);
8831 tdep->ax_regnum = tdep->al_regnum + tdep->num_byte_regs;
8832
8833 ymm0_regnum = tdep->ax_regnum + tdep->num_word_regs;
8834 if (tdep->num_dword_regs)
8835 {
8836 /* Support dword pseudo-register if it hasn't been disabled. */
8837 tdep->eax_regnum = ymm0_regnum;
8838 ymm0_regnum += tdep->num_dword_regs;
8839 }
8840 else
8841 tdep->eax_regnum = -1;
8842
8843 mm0_regnum = ymm0_regnum;
8844 if (tdep->num_ymm_regs)
8845 {
8846 /* Support YMM pseudo-register if it is available. */
8847 tdep->ymm0_regnum = ymm0_regnum;
8848 mm0_regnum += tdep->num_ymm_regs;
8849 }
8850 else
8851 tdep->ymm0_regnum = -1;
8852
8853 if (tdep->num_ymm_avx512_regs)
8854 {
8855 /* Support YMM16-31 pseudo registers if available. */
8856 tdep->ymm16_regnum = mm0_regnum;
8857 mm0_regnum += tdep->num_ymm_avx512_regs;
8858 }
8859 else
8860 tdep->ymm16_regnum = -1;
8861
8862 if (tdep->num_zmm_regs)
8863 {
8864 /* Support ZMM pseudo-register if it is available. */
8865 tdep->zmm0_regnum = mm0_regnum;
8866 mm0_regnum += tdep->num_zmm_regs;
8867 }
8868 else
8869 tdep->zmm0_regnum = -1;
8870
8871 if (tdep->num_mmx_regs != 0)
8872 {
8873 /* Support MMX pseudo-register if MMX hasn't been disabled. */
8874 tdep->mm0_regnum = mm0_regnum;
8875 }
8876 else
8877 tdep->mm0_regnum = -1;
8878
8879 /* Hook in the legacy prologue-based unwinders last (fallback). */
8880 if (info.bfd_arch_info->bits_per_word == 32)
8881 {
8882 frame_unwind_append_unwinder (gdbarch, &i386_stack_tramp_frame_unwind);
8883 frame_unwind_append_unwinder (gdbarch, &i386_sigtramp_frame_unwind);
8884 frame_unwind_append_unwinder (gdbarch, &i386_frame_unwind);
8885 }
8886
8887 /* If we have a register mapping, enable the generic core file
8888 support, unless it has already been enabled. */
8889 if (tdep->gregset_reg_offset
8890 && !gdbarch_iterate_over_regset_sections_p (gdbarch))
8891 set_gdbarch_iterate_over_regset_sections
8892 (gdbarch, i386_iterate_over_regset_sections);
8893
8894 set_gdbarch_fast_tracepoint_valid_at (gdbarch,
8895 i386_fast_tracepoint_valid_at);
8896
8897 return gdbarch;
8898 }
8899
8900 \f
8901
8902 /* Return the target description for a specified XSAVE feature mask. */
8903
8904 const struct target_desc *
8905 i386_target_description (uint64_t xcr0, bool segments)
8906 {
8907 static target_desc *i386_tdescs \
8908 [2/*SSE*/][2/*AVX*/][2/*AVX512*/][2/*PKRU*/][2/*segments*/] = {};
8909 target_desc **tdesc;
8910
8911 tdesc = &i386_tdescs[(xcr0 & X86_XSTATE_SSE) ? 1 : 0]
8912 [(xcr0 & X86_XSTATE_AVX) ? 1 : 0]
8913 [(xcr0 & X86_XSTATE_AVX512) ? 1 : 0]
8914 [(xcr0 & X86_XSTATE_PKRU) ? 1 : 0]
8915 [segments ? 1 : 0];
8916
8917 if (*tdesc == NULL)
8918 *tdesc = i386_create_target_description (xcr0, false, segments);
8919
8920 return *tdesc;
8921 }
8922
8923 void _initialize_i386_tdep ();
8924 void
8925 _initialize_i386_tdep ()
8926 {
8927 gdbarch_register (bfd_arch_i386, i386_gdbarch_init);
8928
8929 /* Add the variable that controls the disassembly flavor. */
8930 add_setshow_enum_cmd ("disassembly-flavor", no_class, valid_flavors,
8931 &disassembly_flavor, _("\
8932 Set the disassembly flavor."), _("\
8933 Show the disassembly flavor."), _("\
8934 The valid values are \"att\" and \"intel\", and the default value is \"att\"."),
8935 NULL,
8936 NULL, /* FIXME: i18n: */
8937 &setlist, &showlist);
8938
8939 /* Add the variable that controls the convention for returning
8940 structs. */
8941 add_setshow_enum_cmd ("struct-convention", no_class, valid_conventions,
8942 &struct_convention, _("\
8943 Set the convention for returning small structs."), _("\
8944 Show the convention for returning small structs."), _("\
8945 Valid values are \"default\", \"pcc\" and \"reg\", and the default value\n\
8946 is \"default\"."),
8947 NULL,
8948 NULL, /* FIXME: i18n: */
8949 &setlist, &showlist);
8950
8951 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_SVR4,
8952 i386_svr4_init_abi);
8953
8954 /* Initialize the i386-specific register groups. */
8955 i386_init_reggroups ();
8956
8957 /* Tell remote stub that we support XML target description. */
8958 register_remote_support_xml ("i386");
8959 }
This page took 0.420088 seconds and 6 git commands to generate.