1 /* Target-dependent code for AMD64.
3 Copyright (C) 2001-2018 Free Software Foundation, Inc.
5 Contributed by Jiri Smid, SuSE Labs.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "opcode/i386.h"
25 #include "arch-utils.h"
27 #include "dummy-frame.h"
29 #include "frame-base.h"
30 #include "frame-unwind.h"
40 #include "amd64-tdep.h"
41 #include "i387-tdep.h"
42 #include "x86-xstate.h"
44 #include "target-descriptions.h"
45 #include "arch/amd64.h"
49 #include "common/byte-vector.h"
53 /* Note that the AMD64 architecture was previously known as x86-64.
54 The latter is (forever) engraved into the canonical system name as
55 returned by config.guess, and used as the name for the AMD64 port
56 of GNU/Linux. The BSD's have renamed their ports to amd64; they
57 don't like to shout. For GDB we prefer the amd64_-prefix over the
58 x86_64_-prefix since it's so much easier to type. */
60 /* Register information. */
62 static const char *amd64_register_names[] =
64 "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp",
66 /* %r8 is indeed register number 8. */
67 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
68 "rip", "eflags", "cs", "ss", "ds", "es", "fs", "gs",
70 /* %st0 is register number 24. */
71 "st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7",
72 "fctrl", "fstat", "ftag", "fiseg", "fioff", "foseg", "fooff", "fop",
74 /* %xmm0 is register number 40. */
75 "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
76 "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
80 static const char *amd64_ymm_names[] =
82 "ymm0", "ymm1", "ymm2", "ymm3",
83 "ymm4", "ymm5", "ymm6", "ymm7",
84 "ymm8", "ymm9", "ymm10", "ymm11",
85 "ymm12", "ymm13", "ymm14", "ymm15"
88 static const char *amd64_ymm_avx512_names[] =
90 "ymm16", "ymm17", "ymm18", "ymm19",
91 "ymm20", "ymm21", "ymm22", "ymm23",
92 "ymm24", "ymm25", "ymm26", "ymm27",
93 "ymm28", "ymm29", "ymm30", "ymm31"
96 static const char *amd64_ymmh_names[] =
98 "ymm0h", "ymm1h", "ymm2h", "ymm3h",
99 "ymm4h", "ymm5h", "ymm6h", "ymm7h",
100 "ymm8h", "ymm9h", "ymm10h", "ymm11h",
101 "ymm12h", "ymm13h", "ymm14h", "ymm15h"
104 static const char *amd64_ymmh_avx512_names[] =
106 "ymm16h", "ymm17h", "ymm18h", "ymm19h",
107 "ymm20h", "ymm21h", "ymm22h", "ymm23h",
108 "ymm24h", "ymm25h", "ymm26h", "ymm27h",
109 "ymm28h", "ymm29h", "ymm30h", "ymm31h"
112 static const char *amd64_mpx_names[] =
114 "bnd0raw", "bnd1raw", "bnd2raw", "bnd3raw", "bndcfgu", "bndstatus"
117 static const char *amd64_k_names[] =
119 "k0", "k1", "k2", "k3",
120 "k4", "k5", "k6", "k7"
123 static const char *amd64_zmmh_names[] =
125 "zmm0h", "zmm1h", "zmm2h", "zmm3h",
126 "zmm4h", "zmm5h", "zmm6h", "zmm7h",
127 "zmm8h", "zmm9h", "zmm10h", "zmm11h",
128 "zmm12h", "zmm13h", "zmm14h", "zmm15h",
129 "zmm16h", "zmm17h", "zmm18h", "zmm19h",
130 "zmm20h", "zmm21h", "zmm22h", "zmm23h",
131 "zmm24h", "zmm25h", "zmm26h", "zmm27h",
132 "zmm28h", "zmm29h", "zmm30h", "zmm31h"
135 static const char *amd64_zmm_names[] =
137 "zmm0", "zmm1", "zmm2", "zmm3",
138 "zmm4", "zmm5", "zmm6", "zmm7",
139 "zmm8", "zmm9", "zmm10", "zmm11",
140 "zmm12", "zmm13", "zmm14", "zmm15",
141 "zmm16", "zmm17", "zmm18", "zmm19",
142 "zmm20", "zmm21", "zmm22", "zmm23",
143 "zmm24", "zmm25", "zmm26", "zmm27",
144 "zmm28", "zmm29", "zmm30", "zmm31"
147 static const char *amd64_xmm_avx512_names[] = {
148 "xmm16", "xmm17", "xmm18", "xmm19",
149 "xmm20", "xmm21", "xmm22", "xmm23",
150 "xmm24", "xmm25", "xmm26", "xmm27",
151 "xmm28", "xmm29", "xmm30", "xmm31"
154 static const char *amd64_pkeys_names[] = {
158 /* DWARF Register Number Mapping as defined in the System V psABI,
161 static int amd64_dwarf_regmap[] =
163 /* General Purpose Registers RAX, RDX, RCX, RBX, RSI, RDI. */
164 AMD64_RAX_REGNUM, AMD64_RDX_REGNUM,
165 AMD64_RCX_REGNUM, AMD64_RBX_REGNUM,
166 AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
168 /* Frame Pointer Register RBP. */
171 /* Stack Pointer Register RSP. */
174 /* Extended Integer Registers 8 - 15. */
175 AMD64_R8_REGNUM, /* %r8 */
176 AMD64_R9_REGNUM, /* %r9 */
177 AMD64_R10_REGNUM, /* %r10 */
178 AMD64_R11_REGNUM, /* %r11 */
179 AMD64_R12_REGNUM, /* %r12 */
180 AMD64_R13_REGNUM, /* %r13 */
181 AMD64_R14_REGNUM, /* %r14 */
182 AMD64_R15_REGNUM, /* %r15 */
184 /* Return Address RA. Mapped to RIP. */
187 /* SSE Registers 0 - 7. */
188 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
189 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
190 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
191 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
193 /* Extended SSE Registers 8 - 15. */
194 AMD64_XMM0_REGNUM + 8, AMD64_XMM0_REGNUM + 9,
195 AMD64_XMM0_REGNUM + 10, AMD64_XMM0_REGNUM + 11,
196 AMD64_XMM0_REGNUM + 12, AMD64_XMM0_REGNUM + 13,
197 AMD64_XMM0_REGNUM + 14, AMD64_XMM0_REGNUM + 15,
199 /* Floating Point Registers 0-7. */
200 AMD64_ST0_REGNUM + 0, AMD64_ST0_REGNUM + 1,
201 AMD64_ST0_REGNUM + 2, AMD64_ST0_REGNUM + 3,
202 AMD64_ST0_REGNUM + 4, AMD64_ST0_REGNUM + 5,
203 AMD64_ST0_REGNUM + 6, AMD64_ST0_REGNUM + 7,
205 /* MMX Registers 0 - 7.
206 We have to handle those registers specifically, as their register
207 number within GDB depends on the target (or they may even not be
208 available at all). */
209 -1, -1, -1, -1, -1, -1, -1, -1,
211 /* Control and Status Flags Register. */
214 /* Selector Registers. */
224 /* Segment Base Address Registers. */
230 /* Special Selector Registers. */
234 /* Floating Point Control Registers. */
240 static const int amd64_dwarf_regmap_len =
241 (sizeof (amd64_dwarf_regmap) / sizeof (amd64_dwarf_regmap[0]));
243 /* Convert DWARF register number REG to the appropriate register
244 number used by GDB. */
247 amd64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
249 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
250 int ymm0_regnum = tdep->ymm0_regnum;
253 if (reg >= 0 && reg < amd64_dwarf_regmap_len)
254 regnum = amd64_dwarf_regmap[reg];
257 && i386_xmm_regnum_p (gdbarch, regnum))
258 regnum += ymm0_regnum - I387_XMM0_REGNUM (tdep);
263 /* Map architectural register numbers to gdb register numbers. */
265 static const int amd64_arch_regmap[16] =
267 AMD64_RAX_REGNUM, /* %rax */
268 AMD64_RCX_REGNUM, /* %rcx */
269 AMD64_RDX_REGNUM, /* %rdx */
270 AMD64_RBX_REGNUM, /* %rbx */
271 AMD64_RSP_REGNUM, /* %rsp */
272 AMD64_RBP_REGNUM, /* %rbp */
273 AMD64_RSI_REGNUM, /* %rsi */
274 AMD64_RDI_REGNUM, /* %rdi */
275 AMD64_R8_REGNUM, /* %r8 */
276 AMD64_R9_REGNUM, /* %r9 */
277 AMD64_R10_REGNUM, /* %r10 */
278 AMD64_R11_REGNUM, /* %r11 */
279 AMD64_R12_REGNUM, /* %r12 */
280 AMD64_R13_REGNUM, /* %r13 */
281 AMD64_R14_REGNUM, /* %r14 */
282 AMD64_R15_REGNUM /* %r15 */
285 static const int amd64_arch_regmap_len =
286 (sizeof (amd64_arch_regmap) / sizeof (amd64_arch_regmap[0]));
288 /* Convert architectural register number REG to the appropriate register
289 number used by GDB. */
292 amd64_arch_reg_to_regnum (int reg)
294 gdb_assert (reg >= 0 && reg < amd64_arch_regmap_len);
296 return amd64_arch_regmap[reg];
299 /* Register names for byte pseudo-registers. */
301 static const char *amd64_byte_names[] =
303 "al", "bl", "cl", "dl", "sil", "dil", "bpl", "spl",
304 "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l",
305 "ah", "bh", "ch", "dh"
308 /* Number of lower byte registers. */
309 #define AMD64_NUM_LOWER_BYTE_REGS 16
311 /* Register names for word pseudo-registers. */
313 static const char *amd64_word_names[] =
315 "ax", "bx", "cx", "dx", "si", "di", "bp", "",
316 "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
319 /* Register names for dword pseudo-registers. */
321 static const char *amd64_dword_names[] =
323 "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp",
324 "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
328 /* Return the name of register REGNUM. */
331 amd64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
333 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
334 if (i386_byte_regnum_p (gdbarch, regnum))
335 return amd64_byte_names[regnum - tdep->al_regnum];
336 else if (i386_zmm_regnum_p (gdbarch, regnum))
337 return amd64_zmm_names[regnum - tdep->zmm0_regnum];
338 else if (i386_ymm_regnum_p (gdbarch, regnum))
339 return amd64_ymm_names[regnum - tdep->ymm0_regnum];
340 else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
341 return amd64_ymm_avx512_names[regnum - tdep->ymm16_regnum];
342 else if (i386_word_regnum_p (gdbarch, regnum))
343 return amd64_word_names[regnum - tdep->ax_regnum];
344 else if (i386_dword_regnum_p (gdbarch, regnum))
345 return amd64_dword_names[regnum - tdep->eax_regnum];
347 return i386_pseudo_register_name (gdbarch, regnum);
350 static struct value *
351 amd64_pseudo_register_read_value (struct gdbarch *gdbarch,
352 readable_regcache *regcache,
355 gdb_byte *raw_buf = (gdb_byte *) alloca (register_size (gdbarch, regnum));
356 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
357 enum register_status status;
358 struct value *result_value;
361 result_value = allocate_value (register_type (gdbarch, regnum));
362 VALUE_LVAL (result_value) = lval_register;
363 VALUE_REGNUM (result_value) = regnum;
364 buf = value_contents_raw (result_value);
366 if (i386_byte_regnum_p (gdbarch, regnum))
368 int gpnum = regnum - tdep->al_regnum;
370 /* Extract (always little endian). */
371 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
373 /* Special handling for AH, BH, CH, DH. */
374 status = regcache->raw_read (gpnum - AMD64_NUM_LOWER_BYTE_REGS,
376 if (status == REG_VALID)
377 memcpy (buf, raw_buf + 1, 1);
379 mark_value_bytes_unavailable (result_value, 0,
380 TYPE_LENGTH (value_type (result_value)));
384 status = regcache->raw_read (gpnum, raw_buf);
385 if (status == REG_VALID)
386 memcpy (buf, raw_buf, 1);
388 mark_value_bytes_unavailable (result_value, 0,
389 TYPE_LENGTH (value_type (result_value)));
392 else if (i386_dword_regnum_p (gdbarch, regnum))
394 int gpnum = regnum - tdep->eax_regnum;
395 /* Extract (always little endian). */
396 status = regcache->raw_read (gpnum, raw_buf);
397 if (status == REG_VALID)
398 memcpy (buf, raw_buf, 4);
400 mark_value_bytes_unavailable (result_value, 0,
401 TYPE_LENGTH (value_type (result_value)));
404 i386_pseudo_register_read_into_value (gdbarch, regcache, regnum,
411 amd64_pseudo_register_write (struct gdbarch *gdbarch,
412 struct regcache *regcache,
413 int regnum, const gdb_byte *buf)
415 gdb_byte *raw_buf = (gdb_byte *) alloca (register_size (gdbarch, regnum));
416 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
418 if (i386_byte_regnum_p (gdbarch, regnum))
420 int gpnum = regnum - tdep->al_regnum;
422 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
424 /* Read ... AH, BH, CH, DH. */
425 regcache->raw_read (gpnum - AMD64_NUM_LOWER_BYTE_REGS, raw_buf);
426 /* ... Modify ... (always little endian). */
427 memcpy (raw_buf + 1, buf, 1);
429 regcache->raw_write (gpnum - AMD64_NUM_LOWER_BYTE_REGS, raw_buf);
434 regcache->raw_read (gpnum, raw_buf);
435 /* ... Modify ... (always little endian). */
436 memcpy (raw_buf, buf, 1);
438 regcache->raw_write (gpnum, raw_buf);
441 else if (i386_dword_regnum_p (gdbarch, regnum))
443 int gpnum = regnum - tdep->eax_regnum;
446 regcache->raw_read (gpnum, raw_buf);
447 /* ... Modify ... (always little endian). */
448 memcpy (raw_buf, buf, 4);
450 regcache->raw_write (gpnum, raw_buf);
453 i386_pseudo_register_write (gdbarch, regcache, regnum, buf);
456 /* Implement the 'ax_pseudo_register_collect' gdbarch method. */
459 amd64_ax_pseudo_register_collect (struct gdbarch *gdbarch,
460 struct agent_expr *ax, int regnum)
462 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
464 if (i386_byte_regnum_p (gdbarch, regnum))
466 int gpnum = regnum - tdep->al_regnum;
468 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
469 ax_reg_mask (ax, gpnum - AMD64_NUM_LOWER_BYTE_REGS);
471 ax_reg_mask (ax, gpnum);
474 else if (i386_dword_regnum_p (gdbarch, regnum))
476 int gpnum = regnum - tdep->eax_regnum;
478 ax_reg_mask (ax, gpnum);
482 return i386_ax_pseudo_register_collect (gdbarch, ax, regnum);
487 /* Register classes as defined in the psABI. */
501 /* Return the union class of CLASS1 and CLASS2. See the psABI for
504 static enum amd64_reg_class
505 amd64_merge_classes (enum amd64_reg_class class1, enum amd64_reg_class class2)
507 /* Rule (a): If both classes are equal, this is the resulting class. */
508 if (class1 == class2)
511 /* Rule (b): If one of the classes is NO_CLASS, the resulting class
512 is the other class. */
513 if (class1 == AMD64_NO_CLASS)
515 if (class2 == AMD64_NO_CLASS)
518 /* Rule (c): If one of the classes is MEMORY, the result is MEMORY. */
519 if (class1 == AMD64_MEMORY || class2 == AMD64_MEMORY)
522 /* Rule (d): If one of the classes is INTEGER, the result is INTEGER. */
523 if (class1 == AMD64_INTEGER || class2 == AMD64_INTEGER)
524 return AMD64_INTEGER;
526 /* Rule (e): If one of the classes is X87, X87UP, COMPLEX_X87 class,
527 MEMORY is used as class. */
528 if (class1 == AMD64_X87 || class1 == AMD64_X87UP
529 || class1 == AMD64_COMPLEX_X87 || class2 == AMD64_X87
530 || class2 == AMD64_X87UP || class2 == AMD64_COMPLEX_X87)
533 /* Rule (f): Otherwise class SSE is used. */
537 static void amd64_classify (struct type *type, enum amd64_reg_class theclass[2]);
539 /* Return non-zero if TYPE is a non-POD structure or union type. */
542 amd64_non_pod_p (struct type *type)
544 /* ??? A class with a base class certainly isn't POD, but does this
545 catch all non-POD structure types? */
546 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_N_BASECLASSES (type) > 0)
552 /* Classify TYPE according to the rules for aggregate (structures and
553 arrays) and union types, and store the result in CLASS. */
556 amd64_classify_aggregate (struct type *type, enum amd64_reg_class theclass[2])
558 /* 1. If the size of an object is larger than two eightbytes, or in
559 C++, is a non-POD structure or union type, or contains
560 unaligned fields, it has class memory. */
561 if (TYPE_LENGTH (type) > 16 || amd64_non_pod_p (type))
563 theclass[0] = theclass[1] = AMD64_MEMORY;
567 /* 2. Both eightbytes get initialized to class NO_CLASS. */
568 theclass[0] = theclass[1] = AMD64_NO_CLASS;
570 /* 3. Each field of an object is classified recursively so that
571 always two fields are considered. The resulting class is
572 calculated according to the classes of the fields in the
575 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
577 struct type *subtype = check_typedef (TYPE_TARGET_TYPE (type));
579 /* All fields in an array have the same type. */
580 amd64_classify (subtype, theclass);
581 if (TYPE_LENGTH (type) > 8 && theclass[1] == AMD64_NO_CLASS)
582 theclass[1] = theclass[0];
588 /* Structure or union. */
589 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
590 || TYPE_CODE (type) == TYPE_CODE_UNION);
592 for (i = 0; i < TYPE_NFIELDS (type); i++)
594 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
595 int pos = TYPE_FIELD_BITPOS (type, i) / 64;
596 enum amd64_reg_class subclass[2];
597 int bitsize = TYPE_FIELD_BITSIZE (type, i);
601 bitsize = TYPE_LENGTH (subtype) * 8;
602 endpos = (TYPE_FIELD_BITPOS (type, i) + bitsize - 1) / 64;
604 /* Ignore static fields, or empty fields, for example nested
606 if (field_is_static (&TYPE_FIELD (type, i)) || bitsize == 0)
609 gdb_assert (pos == 0 || pos == 1);
611 amd64_classify (subtype, subclass);
612 theclass[pos] = amd64_merge_classes (theclass[pos], subclass[0]);
613 if (bitsize <= 64 && pos == 0 && endpos == 1)
614 /* This is a bit of an odd case: We have a field that would
615 normally fit in one of the two eightbytes, except that
616 it is placed in a way that this field straddles them.
617 This has been seen with a structure containing an array.
619 The ABI is a bit unclear in this case, but we assume that
620 this field's class (stored in subclass[0]) must also be merged
621 into class[1]. In other words, our field has a piece stored
622 in the second eight-byte, and thus its class applies to
623 the second eight-byte as well.
625 In the case where the field length exceeds 8 bytes,
626 it should not be necessary to merge the field class
627 into class[1]. As LEN > 8, subclass[1] is necessarily
628 different from AMD64_NO_CLASS. If subclass[1] is equal
629 to subclass[0], then the normal class[1]/subclass[1]
630 merging will take care of everything. For subclass[1]
631 to be different from subclass[0], I can only see the case
632 where we have a SSE/SSEUP or X87/X87UP pair, which both
633 use up all 16 bytes of the aggregate, and are already
634 handled just fine (because each portion sits on its own
636 theclass[1] = amd64_merge_classes (theclass[1], subclass[0]);
638 theclass[1] = amd64_merge_classes (theclass[1], subclass[1]);
642 /* 4. Then a post merger cleanup is done: */
644 /* Rule (a): If one of the classes is MEMORY, the whole argument is
646 if (theclass[0] == AMD64_MEMORY || theclass[1] == AMD64_MEMORY)
647 theclass[0] = theclass[1] = AMD64_MEMORY;
649 /* Rule (b): If SSEUP is not preceded by SSE, it is converted to
651 if (theclass[0] == AMD64_SSEUP)
652 theclass[0] = AMD64_SSE;
653 if (theclass[1] == AMD64_SSEUP && theclass[0] != AMD64_SSE)
654 theclass[1] = AMD64_SSE;
657 /* Classify TYPE, and store the result in CLASS. */
660 amd64_classify (struct type *type, enum amd64_reg_class theclass[2])
662 enum type_code code = TYPE_CODE (type);
663 int len = TYPE_LENGTH (type);
665 theclass[0] = theclass[1] = AMD64_NO_CLASS;
667 /* Arguments of types (signed and unsigned) _Bool, char, short, int,
668 long, long long, and pointers are in the INTEGER class. Similarly,
669 range types, used by languages such as Ada, are also in the INTEGER
671 if ((code == TYPE_CODE_INT || code == TYPE_CODE_ENUM
672 || code == TYPE_CODE_BOOL || code == TYPE_CODE_RANGE
673 || code == TYPE_CODE_CHAR
674 || code == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type))
675 && (len == 1 || len == 2 || len == 4 || len == 8))
676 theclass[0] = AMD64_INTEGER;
678 /* Arguments of types float, double, _Decimal32, _Decimal64 and __m64
680 else if ((code == TYPE_CODE_FLT || code == TYPE_CODE_DECFLOAT)
681 && (len == 4 || len == 8))
683 theclass[0] = AMD64_SSE;
685 /* Arguments of types __float128, _Decimal128 and __m128 are split into
686 two halves. The least significant ones belong to class SSE, the most
687 significant one to class SSEUP. */
688 else if (code == TYPE_CODE_DECFLOAT && len == 16)
689 /* FIXME: __float128, __m128. */
690 theclass[0] = AMD64_SSE, theclass[1] = AMD64_SSEUP;
692 /* The 64-bit mantissa of arguments of type long double belongs to
693 class X87, the 16-bit exponent plus 6 bytes of padding belongs to
695 else if (code == TYPE_CODE_FLT && len == 16)
696 /* Class X87 and X87UP. */
697 theclass[0] = AMD64_X87, theclass[1] = AMD64_X87UP;
699 /* Arguments of complex T where T is one of the types float or
700 double get treated as if they are implemented as:
708 else if (code == TYPE_CODE_COMPLEX && len == 8)
709 theclass[0] = AMD64_SSE;
710 else if (code == TYPE_CODE_COMPLEX && len == 16)
711 theclass[0] = theclass[1] = AMD64_SSE;
713 /* A variable of type complex long double is classified as type
715 else if (code == TYPE_CODE_COMPLEX && len == 32)
716 theclass[0] = AMD64_COMPLEX_X87;
719 else if (code == TYPE_CODE_ARRAY || code == TYPE_CODE_STRUCT
720 || code == TYPE_CODE_UNION)
721 amd64_classify_aggregate (type, theclass);
724 static enum return_value_convention
725 amd64_return_value (struct gdbarch *gdbarch, struct value *function,
726 struct type *type, struct regcache *regcache,
727 gdb_byte *readbuf, const gdb_byte *writebuf)
729 enum amd64_reg_class theclass[2];
730 int len = TYPE_LENGTH (type);
731 static int integer_regnum[] = { AMD64_RAX_REGNUM, AMD64_RDX_REGNUM };
732 static int sse_regnum[] = { AMD64_XMM0_REGNUM, AMD64_XMM1_REGNUM };
737 gdb_assert (!(readbuf && writebuf));
739 /* 1. Classify the return type with the classification algorithm. */
740 amd64_classify (type, theclass);
742 /* 2. If the type has class MEMORY, then the caller provides space
743 for the return value and passes the address of this storage in
744 %rdi as if it were the first argument to the function. In effect,
745 this address becomes a hidden first argument.
747 On return %rax will contain the address that has been passed in
748 by the caller in %rdi. */
749 if (theclass[0] == AMD64_MEMORY)
751 /* As indicated by the comment above, the ABI guarantees that we
752 can always find the return value just after the function has
759 regcache_raw_read_unsigned (regcache, AMD64_RAX_REGNUM, &addr);
760 read_memory (addr, readbuf, TYPE_LENGTH (type));
763 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
766 /* 8. If the class is COMPLEX_X87, the real part of the value is
767 returned in %st0 and the imaginary part in %st1. */
768 if (theclass[0] == AMD64_COMPLEX_X87)
772 regcache->raw_read (AMD64_ST0_REGNUM, readbuf);
773 regcache->raw_read (AMD64_ST1_REGNUM, readbuf + 16);
778 i387_return_value (gdbarch, regcache);
779 regcache->raw_write (AMD64_ST0_REGNUM, writebuf);
780 regcache->raw_write (AMD64_ST1_REGNUM, writebuf + 16);
782 /* Fix up the tag word such that both %st(0) and %st(1) are
784 regcache_raw_write_unsigned (regcache, AMD64_FTAG_REGNUM, 0xfff);
787 return RETURN_VALUE_REGISTER_CONVENTION;
790 gdb_assert (theclass[1] != AMD64_MEMORY);
791 gdb_assert (len <= 16);
793 for (i = 0; len > 0; i++, len -= 8)
801 /* 3. If the class is INTEGER, the next available register
802 of the sequence %rax, %rdx is used. */
803 regnum = integer_regnum[integer_reg++];
807 /* 4. If the class is SSE, the next available SSE register
808 of the sequence %xmm0, %xmm1 is used. */
809 regnum = sse_regnum[sse_reg++];
813 /* 5. If the class is SSEUP, the eightbyte is passed in the
814 upper half of the last used SSE register. */
815 gdb_assert (sse_reg > 0);
816 regnum = sse_regnum[sse_reg - 1];
821 /* 6. If the class is X87, the value is returned on the X87
822 stack in %st0 as 80-bit x87 number. */
823 regnum = AMD64_ST0_REGNUM;
825 i387_return_value (gdbarch, regcache);
829 /* 7. If the class is X87UP, the value is returned together
830 with the previous X87 value in %st0. */
831 gdb_assert (i > 0 && theclass[0] == AMD64_X87);
832 regnum = AMD64_ST0_REGNUM;
841 gdb_assert (!"Unexpected register class.");
844 gdb_assert (regnum != -1);
847 regcache->raw_read_part (regnum, offset, std::min (len, 8),
850 regcache->raw_write_part (regnum, offset, std::min (len, 8),
854 return RETURN_VALUE_REGISTER_CONVENTION;
859 amd64_push_arguments (struct regcache *regcache, int nargs,
860 struct value **args, CORE_ADDR sp, int struct_return)
862 static int integer_regnum[] =
864 AMD64_RDI_REGNUM, /* %rdi */
865 AMD64_RSI_REGNUM, /* %rsi */
866 AMD64_RDX_REGNUM, /* %rdx */
867 AMD64_RCX_REGNUM, /* %rcx */
868 AMD64_R8_REGNUM, /* %r8 */
869 AMD64_R9_REGNUM /* %r9 */
871 static int sse_regnum[] =
873 /* %xmm0 ... %xmm7 */
874 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
875 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
876 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
877 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
879 struct value **stack_args = XALLOCAVEC (struct value *, nargs);
880 int num_stack_args = 0;
881 int num_elements = 0;
887 /* Reserve a register for the "hidden" argument. */
891 for (i = 0; i < nargs; i++)
893 struct type *type = value_type (args[i]);
894 int len = TYPE_LENGTH (type);
895 enum amd64_reg_class theclass[2];
896 int needed_integer_regs = 0;
897 int needed_sse_regs = 0;
900 /* Classify argument. */
901 amd64_classify (type, theclass);
903 /* Calculate the number of integer and SSE registers needed for
905 for (j = 0; j < 2; j++)
907 if (theclass[j] == AMD64_INTEGER)
908 needed_integer_regs++;
909 else if (theclass[j] == AMD64_SSE)
913 /* Check whether enough registers are available, and if the
914 argument should be passed in registers at all. */
915 if (integer_reg + needed_integer_regs > ARRAY_SIZE (integer_regnum)
916 || sse_reg + needed_sse_regs > ARRAY_SIZE (sse_regnum)
917 || (needed_integer_regs == 0 && needed_sse_regs == 0))
919 /* The argument will be passed on the stack. */
920 num_elements += ((len + 7) / 8);
921 stack_args[num_stack_args++] = args[i];
925 /* The argument will be passed in registers. */
926 const gdb_byte *valbuf = value_contents (args[i]);
929 gdb_assert (len <= 16);
931 for (j = 0; len > 0; j++, len -= 8)
939 regnum = integer_regnum[integer_reg++];
943 regnum = sse_regnum[sse_reg++];
947 gdb_assert (sse_reg > 0);
948 regnum = sse_regnum[sse_reg - 1];
953 gdb_assert (!"Unexpected register class.");
956 gdb_assert (regnum != -1);
957 memset (buf, 0, sizeof buf);
958 memcpy (buf, valbuf + j * 8, std::min (len, 8));
959 regcache->raw_write_part (regnum, offset, 8, buf);
964 /* Allocate space for the arguments on the stack. */
965 sp -= num_elements * 8;
967 /* The psABI says that "The end of the input argument area shall be
968 aligned on a 16 byte boundary." */
971 /* Write out the arguments to the stack. */
972 for (i = 0; i < num_stack_args; i++)
974 struct type *type = value_type (stack_args[i]);
975 const gdb_byte *valbuf = value_contents (stack_args[i]);
976 int len = TYPE_LENGTH (type);
978 write_memory (sp + element * 8, valbuf, len);
979 element += ((len + 7) / 8);
982 /* The psABI says that "For calls that may call functions that use
983 varargs or stdargs (prototype-less calls or calls to functions
984 containing ellipsis (...) in the declaration) %al is used as
985 hidden argument to specify the number of SSE registers used. */
986 regcache_raw_write_unsigned (regcache, AMD64_RAX_REGNUM, sse_reg);
991 amd64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
992 struct regcache *regcache, CORE_ADDR bp_addr,
993 int nargs, struct value **args, CORE_ADDR sp,
994 int struct_return, CORE_ADDR struct_addr)
996 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
999 /* BND registers can be in arbitrary values at the moment of the
1000 inferior call. This can cause boundary violations that are not
1001 due to a real bug or even desired by the user. The best to be done
1002 is set the BND registers to allow access to the whole memory, INIT
1003 state, before pushing the inferior call. */
1004 i387_reset_bnd_regs (gdbarch, regcache);
1006 /* Pass arguments. */
1007 sp = amd64_push_arguments (regcache, nargs, args, sp, struct_return);
1009 /* Pass "hidden" argument". */
1012 store_unsigned_integer (buf, 8, byte_order, struct_addr);
1013 regcache->cooked_write (AMD64_RDI_REGNUM, buf);
1016 /* Store return address. */
1018 store_unsigned_integer (buf, 8, byte_order, bp_addr);
1019 write_memory (sp, buf, 8);
1021 /* Finally, update the stack pointer... */
1022 store_unsigned_integer (buf, 8, byte_order, sp);
1023 regcache->cooked_write (AMD64_RSP_REGNUM, buf);
1025 /* ...and fake a frame pointer. */
1026 regcache->cooked_write (AMD64_RBP_REGNUM, buf);
1031 /* Displaced instruction handling. */
1033 /* A partially decoded instruction.
1034 This contains enough details for displaced stepping purposes. */
1038 /* The number of opcode bytes. */
1040 /* The offset of the REX/VEX instruction encoding prefix or -1 if
1042 int enc_prefix_offset;
1043 /* The offset to the first opcode byte. */
1045 /* The offset to the modrm byte or -1 if not present. */
1048 /* The raw instruction. */
1052 struct amd64_displaced_step_closure : public displaced_step_closure
1054 amd64_displaced_step_closure (int insn_buf_len)
1055 : insn_buf (insn_buf_len, 0)
1058 /* For rip-relative insns, saved copy of the reg we use instead of %rip. */
1063 /* Details of the instruction. */
1064 struct amd64_insn insn_details;
1066 /* The possibly modified insn. */
1067 gdb::byte_vector insn_buf;
1070 /* WARNING: Keep onebyte_has_modrm, twobyte_has_modrm in sync with
1071 ../opcodes/i386-dis.c (until libopcodes exports them, or an alternative,
1072 at which point delete these in favor of libopcodes' versions). */
1074 static const unsigned char onebyte_has_modrm[256] = {
1075 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1076 /* ------------------------------- */
1077 /* 00 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 00 */
1078 /* 10 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 10 */
1079 /* 20 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 20 */
1080 /* 30 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 30 */
1081 /* 40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 40 */
1082 /* 50 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 50 */
1083 /* 60 */ 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0, /* 60 */
1084 /* 70 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 70 */
1085 /* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 80 */
1086 /* 90 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 90 */
1087 /* a0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* a0 */
1088 /* b0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* b0 */
1089 /* c0 */ 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0, /* c0 */
1090 /* d0 */ 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1, /* d0 */
1091 /* e0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* e0 */
1092 /* f0 */ 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1 /* f0 */
1093 /* ------------------------------- */
1094 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1097 static const unsigned char twobyte_has_modrm[256] = {
1098 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1099 /* ------------------------------- */
1100 /* 00 */ 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1, /* 0f */
1101 /* 10 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 1f */
1102 /* 20 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 2f */
1103 /* 30 */ 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0, /* 3f */
1104 /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 4f */
1105 /* 50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 5f */
1106 /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 6f */
1107 /* 70 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 7f */
1108 /* 80 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 8f */
1109 /* 90 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 9f */
1110 /* a0 */ 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1, /* af */
1111 /* b0 */ 1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1, /* bf */
1112 /* c0 */ 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, /* cf */
1113 /* d0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* df */
1114 /* e0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ef */
1115 /* f0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0 /* ff */
1116 /* ------------------------------- */
1117 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1120 static int amd64_syscall_p (const struct amd64_insn *insn, int *lengthp);
1123 rex_prefix_p (gdb_byte pfx)
1125 return REX_PREFIX_P (pfx);
1128 /* True if PFX is the start of the 2-byte VEX prefix. */
1131 vex2_prefix_p (gdb_byte pfx)
1136 /* True if PFX is the start of the 3-byte VEX prefix. */
1139 vex3_prefix_p (gdb_byte pfx)
1144 /* Skip the legacy instruction prefixes in INSN.
1145 We assume INSN is properly sentineled so we don't have to worry
1146 about falling off the end of the buffer. */
1149 amd64_skip_prefixes (gdb_byte *insn)
1155 case DATA_PREFIX_OPCODE:
1156 case ADDR_PREFIX_OPCODE:
1157 case CS_PREFIX_OPCODE:
1158 case DS_PREFIX_OPCODE:
1159 case ES_PREFIX_OPCODE:
1160 case FS_PREFIX_OPCODE:
1161 case GS_PREFIX_OPCODE:
1162 case SS_PREFIX_OPCODE:
1163 case LOCK_PREFIX_OPCODE:
1164 case REPE_PREFIX_OPCODE:
1165 case REPNE_PREFIX_OPCODE:
1177 /* Return an integer register (other than RSP) that is unused as an input
1179 In order to not require adding a rex prefix if the insn doesn't already
1180 have one, the result is restricted to RAX ... RDI, sans RSP.
1181 The register numbering of the result follows architecture ordering,
1185 amd64_get_unused_input_int_reg (const struct amd64_insn *details)
1187 /* 1 bit for each reg */
1188 int used_regs_mask = 0;
1190 /* There can be at most 3 int regs used as inputs in an insn, and we have
1191 7 to choose from (RAX ... RDI, sans RSP).
1192 This allows us to take a conservative approach and keep things simple.
1193 E.g. By avoiding RAX, we don't have to specifically watch for opcodes
1194 that implicitly specify RAX. */
1197 used_regs_mask |= 1 << EAX_REG_NUM;
1198 /* Similarily avoid RDX, implicit operand in divides. */
1199 used_regs_mask |= 1 << EDX_REG_NUM;
1201 used_regs_mask |= 1 << ESP_REG_NUM;
1203 /* If the opcode is one byte long and there's no ModRM byte,
1204 assume the opcode specifies a register. */
1205 if (details->opcode_len == 1 && details->modrm_offset == -1)
1206 used_regs_mask |= 1 << (details->raw_insn[details->opcode_offset] & 7);
1208 /* Mark used regs in the modrm/sib bytes. */
1209 if (details->modrm_offset != -1)
1211 int modrm = details->raw_insn[details->modrm_offset];
1212 int mod = MODRM_MOD_FIELD (modrm);
1213 int reg = MODRM_REG_FIELD (modrm);
1214 int rm = MODRM_RM_FIELD (modrm);
1215 int have_sib = mod != 3 && rm == 4;
1217 /* Assume the reg field of the modrm byte specifies a register. */
1218 used_regs_mask |= 1 << reg;
1222 int base = SIB_BASE_FIELD (details->raw_insn[details->modrm_offset + 1]);
1223 int idx = SIB_INDEX_FIELD (details->raw_insn[details->modrm_offset + 1]);
1224 used_regs_mask |= 1 << base;
1225 used_regs_mask |= 1 << idx;
1229 used_regs_mask |= 1 << rm;
1233 gdb_assert (used_regs_mask < 256);
1234 gdb_assert (used_regs_mask != 255);
1236 /* Finally, find a free reg. */
1240 for (i = 0; i < 8; ++i)
1242 if (! (used_regs_mask & (1 << i)))
1246 /* We shouldn't get here. */
1247 internal_error (__FILE__, __LINE__, _("unable to find free reg"));
1251 /* Extract the details of INSN that we need. */
1254 amd64_get_insn_details (gdb_byte *insn, struct amd64_insn *details)
1256 gdb_byte *start = insn;
1259 details->raw_insn = insn;
1261 details->opcode_len = -1;
1262 details->enc_prefix_offset = -1;
1263 details->opcode_offset = -1;
1264 details->modrm_offset = -1;
1266 /* Skip legacy instruction prefixes. */
1267 insn = amd64_skip_prefixes (insn);
1269 /* Skip REX/VEX instruction encoding prefixes. */
1270 if (rex_prefix_p (*insn))
1272 details->enc_prefix_offset = insn - start;
1275 else if (vex2_prefix_p (*insn))
1277 /* Don't record the offset in this case because this prefix has
1278 no REX.B equivalent. */
1281 else if (vex3_prefix_p (*insn))
1283 details->enc_prefix_offset = insn - start;
1287 details->opcode_offset = insn - start;
1289 if (*insn == TWO_BYTE_OPCODE_ESCAPE)
1291 /* Two or three-byte opcode. */
1293 need_modrm = twobyte_has_modrm[*insn];
1295 /* Check for three-byte opcode. */
1305 details->opcode_len = 3;
1308 details->opcode_len = 2;
1314 /* One-byte opcode. */
1315 need_modrm = onebyte_has_modrm[*insn];
1316 details->opcode_len = 1;
1322 details->modrm_offset = insn - start;
1326 /* Update %rip-relative addressing in INSN.
1328 %rip-relative addressing only uses a 32-bit displacement.
1329 32 bits is not enough to be guaranteed to cover the distance between where
1330 the real instruction is and where its copy is.
1331 Convert the insn to use base+disp addressing.
1332 We set base = pc + insn_length so we can leave disp unchanged. */
1335 fixup_riprel (struct gdbarch *gdbarch, amd64_displaced_step_closure *dsc,
1336 CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
1338 const struct amd64_insn *insn_details = &dsc->insn_details;
1339 int modrm_offset = insn_details->modrm_offset;
1340 gdb_byte *insn = insn_details->raw_insn + modrm_offset;
1343 int arch_tmp_regno, tmp_regno;
1344 ULONGEST orig_value;
1346 /* %rip+disp32 addressing mode, displacement follows ModRM byte. */
1349 /* Compute the rip-relative address. */
1350 insn_length = gdb_buffered_insn_length (gdbarch, dsc->insn_buf.data (),
1351 dsc->insn_buf.size (), from);
1352 rip_base = from + insn_length;
1354 /* We need a register to hold the address.
1355 Pick one not used in the insn.
1356 NOTE: arch_tmp_regno uses architecture ordering, e.g. RDI = 7. */
1357 arch_tmp_regno = amd64_get_unused_input_int_reg (insn_details);
1358 tmp_regno = amd64_arch_reg_to_regnum (arch_tmp_regno);
1360 /* Position of the not-B bit in the 3-byte VEX prefix (in byte 1). */
1361 static constexpr gdb_byte VEX3_NOT_B = 0x20;
1363 /* REX.B should be unset (VEX.!B set) as we were using rip-relative
1364 addressing, but ensure it's unset (set for VEX) anyway, tmp_regno
1366 if (insn_details->enc_prefix_offset != -1)
1368 gdb_byte *pfx = &dsc->insn_buf[insn_details->enc_prefix_offset];
1369 if (rex_prefix_p (pfx[0]))
1371 else if (vex3_prefix_p (pfx[0]))
1372 pfx[1] |= VEX3_NOT_B;
1374 gdb_assert_not_reached ("unhandled prefix");
1377 regcache_cooked_read_unsigned (regs, tmp_regno, &orig_value);
1378 dsc->tmp_regno = tmp_regno;
1379 dsc->tmp_save = orig_value;
1382 /* Convert the ModRM field to be base+disp. */
1383 dsc->insn_buf[modrm_offset] &= ~0xc7;
1384 dsc->insn_buf[modrm_offset] |= 0x80 + arch_tmp_regno;
1386 regcache_cooked_write_unsigned (regs, tmp_regno, rip_base);
1388 if (debug_displaced)
1389 fprintf_unfiltered (gdb_stdlog, "displaced: %%rip-relative addressing used.\n"
1390 "displaced: using temp reg %d, old value %s, new value %s\n",
1391 dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save),
1392 paddress (gdbarch, rip_base));
1396 fixup_displaced_copy (struct gdbarch *gdbarch,
1397 amd64_displaced_step_closure *dsc,
1398 CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
1400 const struct amd64_insn *details = &dsc->insn_details;
1402 if (details->modrm_offset != -1)
1404 gdb_byte modrm = details->raw_insn[details->modrm_offset];
1406 if ((modrm & 0xc7) == 0x05)
1408 /* The insn uses rip-relative addressing.
1410 fixup_riprel (gdbarch, dsc, from, to, regs);
1415 struct displaced_step_closure *
1416 amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
1417 CORE_ADDR from, CORE_ADDR to,
1418 struct regcache *regs)
1420 int len = gdbarch_max_insn_length (gdbarch);
1421 /* Extra space for sentinels so fixup_{riprel,displaced_copy} don't have to
1422 continually watch for running off the end of the buffer. */
1423 int fixup_sentinel_space = len;
1424 amd64_displaced_step_closure *dsc
1425 = new amd64_displaced_step_closure (len + fixup_sentinel_space);
1426 gdb_byte *buf = &dsc->insn_buf[0];
1427 struct amd64_insn *details = &dsc->insn_details;
1429 read_memory (from, buf, len);
1431 /* Set up the sentinel space so we don't have to worry about running
1432 off the end of the buffer. An excessive number of leading prefixes
1433 could otherwise cause this. */
1434 memset (buf + len, 0, fixup_sentinel_space);
1436 amd64_get_insn_details (buf, details);
1438 /* GDB may get control back after the insn after the syscall.
1439 Presumably this is a kernel bug.
1440 If this is a syscall, make sure there's a nop afterwards. */
1444 if (amd64_syscall_p (details, &syscall_length))
1445 buf[details->opcode_offset + syscall_length] = NOP_OPCODE;
1448 /* Modify the insn to cope with the address where it will be executed from.
1449 In particular, handle any rip-relative addressing. */
1450 fixup_displaced_copy (gdbarch, dsc, from, to, regs);
1452 write_memory (to, buf, len);
1454 if (debug_displaced)
1456 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
1457 paddress (gdbarch, from), paddress (gdbarch, to));
1458 displaced_step_dump_bytes (gdb_stdlog, buf, len);
1465 amd64_absolute_jmp_p (const struct amd64_insn *details)
1467 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1469 if (insn[0] == 0xff)
1471 /* jump near, absolute indirect (/4) */
1472 if ((insn[1] & 0x38) == 0x20)
1475 /* jump far, absolute indirect (/5) */
1476 if ((insn[1] & 0x38) == 0x28)
1483 /* Return non-zero if the instruction DETAILS is a jump, zero otherwise. */
1486 amd64_jmp_p (const struct amd64_insn *details)
1488 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1490 /* jump short, relative. */
1491 if (insn[0] == 0xeb)
1494 /* jump near, relative. */
1495 if (insn[0] == 0xe9)
1498 return amd64_absolute_jmp_p (details);
1502 amd64_absolute_call_p (const struct amd64_insn *details)
1504 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1506 if (insn[0] == 0xff)
1508 /* Call near, absolute indirect (/2) */
1509 if ((insn[1] & 0x38) == 0x10)
1512 /* Call far, absolute indirect (/3) */
1513 if ((insn[1] & 0x38) == 0x18)
1521 amd64_ret_p (const struct amd64_insn *details)
1523 /* NOTE: gcc can emit "repz ; ret". */
1524 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1528 case 0xc2: /* ret near, pop N bytes */
1529 case 0xc3: /* ret near */
1530 case 0xca: /* ret far, pop N bytes */
1531 case 0xcb: /* ret far */
1532 case 0xcf: /* iret */
1541 amd64_call_p (const struct amd64_insn *details)
1543 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1545 if (amd64_absolute_call_p (details))
1548 /* call near, relative */
1549 if (insn[0] == 0xe8)
1555 /* Return non-zero if INSN is a system call, and set *LENGTHP to its
1556 length in bytes. Otherwise, return zero. */
1559 amd64_syscall_p (const struct amd64_insn *details, int *lengthp)
1561 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1563 if (insn[0] == 0x0f && insn[1] == 0x05)
1572 /* Classify the instruction at ADDR using PRED.
1573 Throw an error if the memory can't be read. */
1576 amd64_classify_insn_at (struct gdbarch *gdbarch, CORE_ADDR addr,
1577 int (*pred) (const struct amd64_insn *))
1579 struct amd64_insn details;
1581 int len, classification;
1583 len = gdbarch_max_insn_length (gdbarch);
1584 buf = (gdb_byte *) alloca (len);
1586 read_code (addr, buf, len);
1587 amd64_get_insn_details (buf, &details);
1589 classification = pred (&details);
1591 return classification;
1594 /* The gdbarch insn_is_call method. */
1597 amd64_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
1599 return amd64_classify_insn_at (gdbarch, addr, amd64_call_p);
1602 /* The gdbarch insn_is_ret method. */
1605 amd64_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
1607 return amd64_classify_insn_at (gdbarch, addr, amd64_ret_p);
1610 /* The gdbarch insn_is_jump method. */
1613 amd64_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
1615 return amd64_classify_insn_at (gdbarch, addr, amd64_jmp_p);
1618 /* Fix up the state of registers and memory after having single-stepped
1619 a displaced instruction. */
1622 amd64_displaced_step_fixup (struct gdbarch *gdbarch,
1623 struct displaced_step_closure *dsc_,
1624 CORE_ADDR from, CORE_ADDR to,
1625 struct regcache *regs)
1627 amd64_displaced_step_closure *dsc = (amd64_displaced_step_closure *) dsc_;
1628 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1629 /* The offset we applied to the instruction's address. */
1630 ULONGEST insn_offset = to - from;
1631 gdb_byte *insn = dsc->insn_buf.data ();
1632 const struct amd64_insn *insn_details = &dsc->insn_details;
1634 if (debug_displaced)
1635 fprintf_unfiltered (gdb_stdlog,
1636 "displaced: fixup (%s, %s), "
1637 "insn = 0x%02x 0x%02x ...\n",
1638 paddress (gdbarch, from), paddress (gdbarch, to),
1641 /* If we used a tmp reg, restore it. */
1645 if (debug_displaced)
1646 fprintf_unfiltered (gdb_stdlog, "displaced: restoring reg %d to %s\n",
1647 dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save));
1648 regcache_cooked_write_unsigned (regs, dsc->tmp_regno, dsc->tmp_save);
1651 /* The list of issues to contend with here is taken from
1652 resume_execution in arch/x86/kernel/kprobes.c, Linux 2.6.28.
1653 Yay for Free Software! */
1655 /* Relocate the %rip back to the program's instruction stream,
1658 /* Except in the case of absolute or indirect jump or call
1659 instructions, or a return instruction, the new rip is relative to
1660 the displaced instruction; make it relative to the original insn.
1661 Well, signal handler returns don't need relocation either, but we use the
1662 value of %rip to recognize those; see below. */
1663 if (! amd64_absolute_jmp_p (insn_details)
1664 && ! amd64_absolute_call_p (insn_details)
1665 && ! amd64_ret_p (insn_details))
1670 regcache_cooked_read_unsigned (regs, AMD64_RIP_REGNUM, &orig_rip);
1672 /* A signal trampoline system call changes the %rip, resuming
1673 execution of the main program after the signal handler has
1674 returned. That makes them like 'return' instructions; we
1675 shouldn't relocate %rip.
1677 But most system calls don't, and we do need to relocate %rip.
1679 Our heuristic for distinguishing these cases: if stepping
1680 over the system call instruction left control directly after
1681 the instruction, the we relocate --- control almost certainly
1682 doesn't belong in the displaced copy. Otherwise, we assume
1683 the instruction has put control where it belongs, and leave
1684 it unrelocated. Goodness help us if there are PC-relative
1686 if (amd64_syscall_p (insn_details, &insn_len)
1687 && orig_rip != to + insn_len
1688 /* GDB can get control back after the insn after the syscall.
1689 Presumably this is a kernel bug.
1690 Fixup ensures its a nop, we add one to the length for it. */
1691 && orig_rip != to + insn_len + 1)
1693 if (debug_displaced)
1694 fprintf_unfiltered (gdb_stdlog,
1695 "displaced: syscall changed %%rip; "
1696 "not relocating\n");
1700 ULONGEST rip = orig_rip - insn_offset;
1702 /* If we just stepped over a breakpoint insn, we don't backup
1703 the pc on purpose; this is to match behaviour without
1706 regcache_cooked_write_unsigned (regs, AMD64_RIP_REGNUM, rip);
1708 if (debug_displaced)
1709 fprintf_unfiltered (gdb_stdlog,
1711 "relocated %%rip from %s to %s\n",
1712 paddress (gdbarch, orig_rip),
1713 paddress (gdbarch, rip));
1717 /* If the instruction was PUSHFL, then the TF bit will be set in the
1718 pushed value, and should be cleared. We'll leave this for later,
1719 since GDB already messes up the TF flag when stepping over a
1722 /* If the instruction was a call, the return address now atop the
1723 stack is the address following the copied instruction. We need
1724 to make it the address following the original instruction. */
1725 if (amd64_call_p (insn_details))
1729 const ULONGEST retaddr_len = 8;
1731 regcache_cooked_read_unsigned (regs, AMD64_RSP_REGNUM, &rsp);
1732 retaddr = read_memory_unsigned_integer (rsp, retaddr_len, byte_order);
1733 retaddr = (retaddr - insn_offset) & 0xffffffffffffffffULL;
1734 write_memory_unsigned_integer (rsp, retaddr_len, byte_order, retaddr);
1736 if (debug_displaced)
1737 fprintf_unfiltered (gdb_stdlog,
1738 "displaced: relocated return addr at %s "
1740 paddress (gdbarch, rsp),
1741 paddress (gdbarch, retaddr));
1745 /* If the instruction INSN uses RIP-relative addressing, return the
1746 offset into the raw INSN where the displacement to be adjusted is
1747 found. Returns 0 if the instruction doesn't use RIP-relative
1751 rip_relative_offset (struct amd64_insn *insn)
1753 if (insn->modrm_offset != -1)
1755 gdb_byte modrm = insn->raw_insn[insn->modrm_offset];
1757 if ((modrm & 0xc7) == 0x05)
1759 /* The displacement is found right after the ModRM byte. */
1760 return insn->modrm_offset + 1;
1768 append_insns (CORE_ADDR *to, ULONGEST len, const gdb_byte *buf)
1770 target_write_memory (*to, buf, len);
1775 amd64_relocate_instruction (struct gdbarch *gdbarch,
1776 CORE_ADDR *to, CORE_ADDR oldloc)
1778 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1779 int len = gdbarch_max_insn_length (gdbarch);
1780 /* Extra space for sentinels. */
1781 int fixup_sentinel_space = len;
1782 gdb_byte *buf = (gdb_byte *) xmalloc (len + fixup_sentinel_space);
1783 struct amd64_insn insn_details;
1785 LONGEST rel32, newrel;
1789 read_memory (oldloc, buf, len);
1791 /* Set up the sentinel space so we don't have to worry about running
1792 off the end of the buffer. An excessive number of leading prefixes
1793 could otherwise cause this. */
1794 memset (buf + len, 0, fixup_sentinel_space);
1797 amd64_get_insn_details (insn, &insn_details);
1799 insn_length = gdb_buffered_insn_length (gdbarch, insn, len, oldloc);
1801 /* Skip legacy instruction prefixes. */
1802 insn = amd64_skip_prefixes (insn);
1804 /* Adjust calls with 32-bit relative addresses as push/jump, with
1805 the address pushed being the location where the original call in
1806 the user program would return to. */
1807 if (insn[0] == 0xe8)
1809 gdb_byte push_buf[32];
1813 /* Where "ret" in the original code will return to. */
1814 ret_addr = oldloc + insn_length;
1816 /* If pushing an address higher than or equal to 0x80000000,
1817 avoid 'pushq', as that sign extends its 32-bit operand, which
1818 would be incorrect. */
1819 if (ret_addr <= 0x7fffffff)
1821 push_buf[0] = 0x68; /* pushq $... */
1822 store_unsigned_integer (&push_buf[1], 4, byte_order, ret_addr);
1827 push_buf[i++] = 0x48; /* sub $0x8,%rsp */
1828 push_buf[i++] = 0x83;
1829 push_buf[i++] = 0xec;
1830 push_buf[i++] = 0x08;
1832 push_buf[i++] = 0xc7; /* movl $imm,(%rsp) */
1833 push_buf[i++] = 0x04;
1834 push_buf[i++] = 0x24;
1835 store_unsigned_integer (&push_buf[i], 4, byte_order,
1836 ret_addr & 0xffffffff);
1839 push_buf[i++] = 0xc7; /* movl $imm,4(%rsp) */
1840 push_buf[i++] = 0x44;
1841 push_buf[i++] = 0x24;
1842 push_buf[i++] = 0x04;
1843 store_unsigned_integer (&push_buf[i], 4, byte_order,
1847 gdb_assert (i <= sizeof (push_buf));
1848 /* Push the push. */
1849 append_insns (to, i, push_buf);
1851 /* Convert the relative call to a relative jump. */
1854 /* Adjust the destination offset. */
1855 rel32 = extract_signed_integer (insn + 1, 4, byte_order);
1856 newrel = (oldloc - *to) + rel32;
1857 store_signed_integer (insn + 1, 4, byte_order, newrel);
1859 if (debug_displaced)
1860 fprintf_unfiltered (gdb_stdlog,
1861 "Adjusted insn rel32=%s at %s to"
1862 " rel32=%s at %s\n",
1863 hex_string (rel32), paddress (gdbarch, oldloc),
1864 hex_string (newrel), paddress (gdbarch, *to));
1866 /* Write the adjusted jump into its displaced location. */
1867 append_insns (to, 5, insn);
1871 offset = rip_relative_offset (&insn_details);
1874 /* Adjust jumps with 32-bit relative addresses. Calls are
1875 already handled above. */
1876 if (insn[0] == 0xe9)
1878 /* Adjust conditional jumps. */
1879 else if (insn[0] == 0x0f && (insn[1] & 0xf0) == 0x80)
1885 rel32 = extract_signed_integer (insn + offset, 4, byte_order);
1886 newrel = (oldloc - *to) + rel32;
1887 store_signed_integer (insn + offset, 4, byte_order, newrel);
1888 if (debug_displaced)
1889 fprintf_unfiltered (gdb_stdlog,
1890 "Adjusted insn rel32=%s at %s to"
1891 " rel32=%s at %s\n",
1892 hex_string (rel32), paddress (gdbarch, oldloc),
1893 hex_string (newrel), paddress (gdbarch, *to));
1896 /* Write the adjusted instruction into its displaced location. */
1897 append_insns (to, insn_length, buf);
1901 /* The maximum number of saved registers. This should include %rip. */
1902 #define AMD64_NUM_SAVED_REGS AMD64_NUM_GREGS
1904 struct amd64_frame_cache
1909 CORE_ADDR sp_offset;
1912 /* Saved registers. */
1913 CORE_ADDR saved_regs[AMD64_NUM_SAVED_REGS];
1917 /* Do we have a frame? */
1921 /* Initialize a frame cache. */
1924 amd64_init_frame_cache (struct amd64_frame_cache *cache)
1931 cache->sp_offset = -8;
1934 /* Saved registers. We initialize these to -1 since zero is a valid
1935 offset (that's where %rbp is supposed to be stored).
1936 The values start out as being offsets, and are later converted to
1937 addresses (at which point -1 is interpreted as an address, still meaning
1939 for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
1940 cache->saved_regs[i] = -1;
1941 cache->saved_sp = 0;
1942 cache->saved_sp_reg = -1;
1944 /* Frameless until proven otherwise. */
1945 cache->frameless_p = 1;
1948 /* Allocate and initialize a frame cache. */
1950 static struct amd64_frame_cache *
1951 amd64_alloc_frame_cache (void)
1953 struct amd64_frame_cache *cache;
1955 cache = FRAME_OBSTACK_ZALLOC (struct amd64_frame_cache);
1956 amd64_init_frame_cache (cache);
1960 /* GCC 4.4 and later, can put code in the prologue to realign the
1961 stack pointer. Check whether PC points to such code, and update
1962 CACHE accordingly. Return the first instruction after the code
1963 sequence or CURRENT_PC, whichever is smaller. If we don't
1964 recognize the code, return PC. */
1967 amd64_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
1968 struct amd64_frame_cache *cache)
1970 /* There are 2 code sequences to re-align stack before the frame
1973 1. Use a caller-saved saved register:
1979 2. Use a callee-saved saved register:
1986 "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:
1988 0x48 0x83 0xe4 0xf0 andq $-16, %rsp
1989 0x48 0x81 0xe4 0x00 0xff 0xff 0xff andq $-256, %rsp
1994 int offset, offset_and;
1996 if (target_read_code (pc, buf, sizeof buf))
1999 /* Check caller-saved saved register. The first instruction has
2000 to be "leaq 8(%rsp), %reg". */
2001 if ((buf[0] & 0xfb) == 0x48
2006 /* MOD must be binary 10 and R/M must be binary 100. */
2007 if ((buf[2] & 0xc7) != 0x44)
2010 /* REG has register number. */
2011 reg = (buf[2] >> 3) & 7;
2013 /* Check the REX.R bit. */
2021 /* Check callee-saved saved register. The first instruction
2022 has to be "pushq %reg". */
2024 if ((buf[0] & 0xf8) == 0x50)
2026 else if ((buf[0] & 0xf6) == 0x40
2027 && (buf[1] & 0xf8) == 0x50)
2029 /* Check the REX.B bit. */
2030 if ((buf[0] & 1) != 0)
2039 reg += buf[offset] & 0x7;
2043 /* The next instruction has to be "leaq 16(%rsp), %reg". */
2044 if ((buf[offset] & 0xfb) != 0x48
2045 || buf[offset + 1] != 0x8d
2046 || buf[offset + 3] != 0x24
2047 || buf[offset + 4] != 0x10)
2050 /* MOD must be binary 10 and R/M must be binary 100. */
2051 if ((buf[offset + 2] & 0xc7) != 0x44)
2054 /* REG has register number. */
2055 r = (buf[offset + 2] >> 3) & 7;
2057 /* Check the REX.R bit. */
2058 if (buf[offset] == 0x4c)
2061 /* Registers in pushq and leaq have to be the same. */
2068 /* Rigister can't be %rsp nor %rbp. */
2069 if (reg == 4 || reg == 5)
2072 /* The next instruction has to be "andq $-XXX, %rsp". */
2073 if (buf[offset] != 0x48
2074 || buf[offset + 2] != 0xe4
2075 || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
2078 offset_and = offset;
2079 offset += buf[offset + 1] == 0x81 ? 7 : 4;
2081 /* The next instruction has to be "pushq -8(%reg)". */
2083 if (buf[offset] == 0xff)
2085 else if ((buf[offset] & 0xf6) == 0x40
2086 && buf[offset + 1] == 0xff)
2088 /* Check the REX.B bit. */
2089 if ((buf[offset] & 0x1) != 0)
2096 /* 8bit -8 is 0xf8. REG must be binary 110 and MOD must be binary
2098 if (buf[offset + 1] != 0xf8
2099 || (buf[offset] & 0xf8) != 0x70)
2102 /* R/M has register. */
2103 r += buf[offset] & 7;
2105 /* Registers in leaq and pushq have to be the same. */
2109 if (current_pc > pc + offset_and)
2110 cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);
2112 return std::min (pc + offset + 2, current_pc);
2115 /* Similar to amd64_analyze_stack_align for x32. */
2118 amd64_x32_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
2119 struct amd64_frame_cache *cache)
2121 /* There are 2 code sequences to re-align stack before the frame
2124 1. Use a caller-saved saved register:
2132 [addr32] leal 8(%rsp), %reg
2134 [addr32] pushq -8(%reg)
2136 2. Use a callee-saved saved register:
2146 [addr32] leal 16(%rsp), %reg
2148 [addr32] pushq -8(%reg)
2150 "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:
2152 0x48 0x83 0xe4 0xf0 andq $-16, %rsp
2153 0x48 0x81 0xe4 0x00 0xff 0xff 0xff andq $-256, %rsp
2155 "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:
2157 0x83 0xe4 0xf0 andl $-16, %esp
2158 0x81 0xe4 0x00 0xff 0xff 0xff andl $-256, %esp
2163 int offset, offset_and;
2165 if (target_read_memory (pc, buf, sizeof buf))
2168 /* Skip optional addr32 prefix. */
2169 offset = buf[0] == 0x67 ? 1 : 0;
2171 /* Check caller-saved saved register. The first instruction has
2172 to be "leaq 8(%rsp), %reg" or "leal 8(%rsp), %reg". */
2173 if (((buf[offset] & 0xfb) == 0x48 || (buf[offset] & 0xfb) == 0x40)
2174 && buf[offset + 1] == 0x8d
2175 && buf[offset + 3] == 0x24
2176 && buf[offset + 4] == 0x8)
2178 /* MOD must be binary 10 and R/M must be binary 100. */
2179 if ((buf[offset + 2] & 0xc7) != 0x44)
2182 /* REG has register number. */
2183 reg = (buf[offset + 2] >> 3) & 7;
2185 /* Check the REX.R bit. */
2186 if ((buf[offset] & 0x4) != 0)
2193 /* Check callee-saved saved register. The first instruction
2194 has to be "pushq %reg". */
2196 if ((buf[offset] & 0xf6) == 0x40
2197 && (buf[offset + 1] & 0xf8) == 0x50)
2199 /* Check the REX.B bit. */
2200 if ((buf[offset] & 1) != 0)
2205 else if ((buf[offset] & 0xf8) != 0x50)
2209 reg += buf[offset] & 0x7;
2213 /* Skip optional addr32 prefix. */
2214 if (buf[offset] == 0x67)
2217 /* The next instruction has to be "leaq 16(%rsp), %reg" or
2218 "leal 16(%rsp), %reg". */
2219 if (((buf[offset] & 0xfb) != 0x48 && (buf[offset] & 0xfb) != 0x40)
2220 || buf[offset + 1] != 0x8d
2221 || buf[offset + 3] != 0x24
2222 || buf[offset + 4] != 0x10)
2225 /* MOD must be binary 10 and R/M must be binary 100. */
2226 if ((buf[offset + 2] & 0xc7) != 0x44)
2229 /* REG has register number. */
2230 r = (buf[offset + 2] >> 3) & 7;
2232 /* Check the REX.R bit. */
2233 if ((buf[offset] & 0x4) != 0)
2236 /* Registers in pushq and leaq have to be the same. */
2243 /* Rigister can't be %rsp nor %rbp. */
2244 if (reg == 4 || reg == 5)
2247 /* The next instruction may be "andq $-XXX, %rsp" or
2248 "andl $-XXX, %esp". */
2249 if (buf[offset] != 0x48)
2252 if (buf[offset + 2] != 0xe4
2253 || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
2256 offset_and = offset;
2257 offset += buf[offset + 1] == 0x81 ? 7 : 4;
2259 /* Skip optional addr32 prefix. */
2260 if (buf[offset] == 0x67)
2263 /* The next instruction has to be "pushq -8(%reg)". */
2265 if (buf[offset] == 0xff)
2267 else if ((buf[offset] & 0xf6) == 0x40
2268 && buf[offset + 1] == 0xff)
2270 /* Check the REX.B bit. */
2271 if ((buf[offset] & 0x1) != 0)
2278 /* 8bit -8 is 0xf8. REG must be binary 110 and MOD must be binary
2280 if (buf[offset + 1] != 0xf8
2281 || (buf[offset] & 0xf8) != 0x70)
2284 /* R/M has register. */
2285 r += buf[offset] & 7;
2287 /* Registers in leaq and pushq have to be the same. */
2291 if (current_pc > pc + offset_and)
2292 cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);
2294 return std::min (pc + offset + 2, current_pc);
2297 /* Do a limited analysis of the prologue at PC and update CACHE
2298 accordingly. Bail out early if CURRENT_PC is reached. Return the
2299 address where the analysis stopped.
2301 We will handle only functions beginning with:
2304 movq %rsp, %rbp 0x48 0x89 0xe5 (or 0x48 0x8b 0xec)
2306 or (for the X32 ABI):
2309 movl %esp, %ebp 0x89 0xe5 (or 0x8b 0xec)
2311 Any function that doesn't start with one of these sequences will be
2312 assumed to have no prologue and thus no valid frame pointer in
2316 amd64_analyze_prologue (struct gdbarch *gdbarch,
2317 CORE_ADDR pc, CORE_ADDR current_pc,
2318 struct amd64_frame_cache *cache)
2320 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2321 /* There are two variations of movq %rsp, %rbp. */
2322 static const gdb_byte mov_rsp_rbp_1[3] = { 0x48, 0x89, 0xe5 };
2323 static const gdb_byte mov_rsp_rbp_2[3] = { 0x48, 0x8b, 0xec };
2324 /* Ditto for movl %esp, %ebp. */
2325 static const gdb_byte mov_esp_ebp_1[2] = { 0x89, 0xe5 };
2326 static const gdb_byte mov_esp_ebp_2[2] = { 0x8b, 0xec };
2331 if (current_pc <= pc)
2334 if (gdbarch_ptr_bit (gdbarch) == 32)
2335 pc = amd64_x32_analyze_stack_align (pc, current_pc, cache);
2337 pc = amd64_analyze_stack_align (pc, current_pc, cache);
2339 op = read_code_unsigned_integer (pc, 1, byte_order);
2341 if (op == 0x55) /* pushq %rbp */
2343 /* Take into account that we've executed the `pushq %rbp' that
2344 starts this instruction sequence. */
2345 cache->saved_regs[AMD64_RBP_REGNUM] = 0;
2346 cache->sp_offset += 8;
2348 /* If that's all, return now. */
2349 if (current_pc <= pc + 1)
2352 read_code (pc + 1, buf, 3);
2354 /* Check for `movq %rsp, %rbp'. */
2355 if (memcmp (buf, mov_rsp_rbp_1, 3) == 0
2356 || memcmp (buf, mov_rsp_rbp_2, 3) == 0)
2358 /* OK, we actually have a frame. */
2359 cache->frameless_p = 0;
2363 /* For X32, also check for `movq %esp, %ebp'. */
2364 if (gdbarch_ptr_bit (gdbarch) == 32)
2366 if (memcmp (buf, mov_esp_ebp_1, 2) == 0
2367 || memcmp (buf, mov_esp_ebp_2, 2) == 0)
2369 /* OK, we actually have a frame. */
2370 cache->frameless_p = 0;
2381 /* Work around false termination of prologue - GCC PR debug/48827.
2383 START_PC is the first instruction of a function, PC is its minimal already
2384 determined advanced address. Function returns PC if it has nothing to do.
2388 <-- here is 0 lines advance - the false prologue end marker.
2389 0f 29 85 70 ff ff ff movaps %xmm0,-0x90(%rbp)
2390 0f 29 4d 80 movaps %xmm1,-0x80(%rbp)
2391 0f 29 55 90 movaps %xmm2,-0x70(%rbp)
2392 0f 29 5d a0 movaps %xmm3,-0x60(%rbp)
2393 0f 29 65 b0 movaps %xmm4,-0x50(%rbp)
2394 0f 29 6d c0 movaps %xmm5,-0x40(%rbp)
2395 0f 29 75 d0 movaps %xmm6,-0x30(%rbp)
2396 0f 29 7d e0 movaps %xmm7,-0x20(%rbp)
2400 amd64_skip_xmm_prologue (CORE_ADDR pc, CORE_ADDR start_pc)
2402 struct symtab_and_line start_pc_sal, next_sal;
2403 gdb_byte buf[4 + 8 * 7];
2409 start_pc_sal = find_pc_sect_line (start_pc, NULL, 0);
2410 if (start_pc_sal.symtab == NULL
2411 || producer_is_gcc_ge_4 (COMPUNIT_PRODUCER
2412 (SYMTAB_COMPUNIT (start_pc_sal.symtab))) < 6
2413 || start_pc_sal.pc != start_pc || pc >= start_pc_sal.end)
2416 next_sal = find_pc_sect_line (start_pc_sal.end, NULL, 0);
2417 if (next_sal.line != start_pc_sal.line)
2420 /* START_PC can be from overlayed memory, ignored here. */
2421 if (target_read_code (next_sal.pc - 4, buf, sizeof (buf)) != 0)
2425 if (buf[0] != 0x84 || buf[1] != 0xc0)
2432 for (xmmreg = 0; xmmreg < 8; xmmreg++)
2434 /* 0x0f 0x29 0b??000101 movaps %xmmreg?,-0x??(%rbp) */
2435 if (buf[offset] != 0x0f || buf[offset + 1] != 0x29
2436 || (buf[offset + 2] & 0x3f) != (xmmreg << 3 | 0x5))
2440 if ((buf[offset + 2] & 0xc0) == 0x40)
2442 /* 8-bit displacement. */
2446 else if ((buf[offset + 2] & 0xc0) == 0x80)
2448 /* 32-bit displacement. */
2456 if (offset - 4 != buf[3])
2459 return next_sal.end;
2462 /* Return PC of first real instruction. */
2465 amd64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
2467 struct amd64_frame_cache cache;
2469 CORE_ADDR func_addr;
2471 if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
2473 CORE_ADDR post_prologue_pc
2474 = skip_prologue_using_sal (gdbarch, func_addr);
2475 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
2477 /* Clang always emits a line note before the prologue and another
2478 one after. We trust clang to emit usable line notes. */
2479 if (post_prologue_pc
2481 && COMPUNIT_PRODUCER (cust) != NULL
2482 && startswith (COMPUNIT_PRODUCER (cust), "clang ")))
2483 return std::max (start_pc, post_prologue_pc);
2486 amd64_init_frame_cache (&cache);
2487 pc = amd64_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffLL,
2489 if (cache.frameless_p)
2492 return amd64_skip_xmm_prologue (pc, start_pc);
2496 /* Normal frames. */
2499 amd64_frame_cache_1 (struct frame_info *this_frame,
2500 struct amd64_frame_cache *cache)
2502 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2503 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2507 cache->pc = get_frame_func (this_frame);
2509 amd64_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
2512 if (cache->frameless_p)
2514 /* We didn't find a valid frame. If we're at the start of a
2515 function, or somewhere half-way its prologue, the function's
2516 frame probably hasn't been fully setup yet. Try to
2517 reconstruct the base address for the stack frame by looking
2518 at the stack pointer. For truly "frameless" functions this
2521 if (cache->saved_sp_reg != -1)
2523 /* Stack pointer has been saved. */
2524 get_frame_register (this_frame, cache->saved_sp_reg, buf);
2525 cache->saved_sp = extract_unsigned_integer (buf, 8, byte_order);
2527 /* We're halfway aligning the stack. */
2528 cache->base = ((cache->saved_sp - 8) & 0xfffffffffffffff0LL) - 8;
2529 cache->saved_regs[AMD64_RIP_REGNUM] = cache->saved_sp - 8;
2531 /* This will be added back below. */
2532 cache->saved_regs[AMD64_RIP_REGNUM] -= cache->base;
2536 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2537 cache->base = extract_unsigned_integer (buf, 8, byte_order)
2543 get_frame_register (this_frame, AMD64_RBP_REGNUM, buf);
2544 cache->base = extract_unsigned_integer (buf, 8, byte_order);
2547 /* Now that we have the base address for the stack frame we can
2548 calculate the value of %rsp in the calling frame. */
2549 cache->saved_sp = cache->base + 16;
2551 /* For normal frames, %rip is stored at 8(%rbp). If we don't have a
2552 frame we find it at the same offset from the reconstructed base
2553 address. If we're halfway aligning the stack, %rip is handled
2554 differently (see above). */
2555 if (!cache->frameless_p || cache->saved_sp_reg == -1)
2556 cache->saved_regs[AMD64_RIP_REGNUM] = 8;
2558 /* Adjust all the saved registers such that they contain addresses
2559 instead of offsets. */
2560 for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
2561 if (cache->saved_regs[i] != -1)
2562 cache->saved_regs[i] += cache->base;
2567 static struct amd64_frame_cache *
2568 amd64_frame_cache (struct frame_info *this_frame, void **this_cache)
2570 struct amd64_frame_cache *cache;
2573 return (struct amd64_frame_cache *) *this_cache;
2575 cache = amd64_alloc_frame_cache ();
2576 *this_cache = cache;
2580 amd64_frame_cache_1 (this_frame, cache);
2582 CATCH (ex, RETURN_MASK_ERROR)
2584 if (ex.error != NOT_AVAILABLE_ERROR)
2585 throw_exception (ex);
2592 static enum unwind_stop_reason
2593 amd64_frame_unwind_stop_reason (struct frame_info *this_frame,
2596 struct amd64_frame_cache *cache =
2597 amd64_frame_cache (this_frame, this_cache);
2600 return UNWIND_UNAVAILABLE;
2602 /* This marks the outermost frame. */
2603 if (cache->base == 0)
2604 return UNWIND_OUTERMOST;
2606 return UNWIND_NO_REASON;
2610 amd64_frame_this_id (struct frame_info *this_frame, void **this_cache,
2611 struct frame_id *this_id)
2613 struct amd64_frame_cache *cache =
2614 amd64_frame_cache (this_frame, this_cache);
2617 (*this_id) = frame_id_build_unavailable_stack (cache->pc);
2618 else if (cache->base == 0)
2620 /* This marks the outermost frame. */
2624 (*this_id) = frame_id_build (cache->base + 16, cache->pc);
2627 static struct value *
2628 amd64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
2631 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2632 struct amd64_frame_cache *cache =
2633 amd64_frame_cache (this_frame, this_cache);
2635 gdb_assert (regnum >= 0);
2637 if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
2638 return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
2640 if (regnum < AMD64_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
2641 return frame_unwind_got_memory (this_frame, regnum,
2642 cache->saved_regs[regnum]);
2644 return frame_unwind_got_register (this_frame, regnum, regnum);
2647 static const struct frame_unwind amd64_frame_unwind =
2650 amd64_frame_unwind_stop_reason,
2651 amd64_frame_this_id,
2652 amd64_frame_prev_register,
2654 default_frame_sniffer
2657 /* Generate a bytecode expression to get the value of the saved PC. */
2660 amd64_gen_return_address (struct gdbarch *gdbarch,
2661 struct agent_expr *ax, struct axs_value *value,
2664 /* The following sequence assumes the traditional use of the base
2666 ax_reg (ax, AMD64_RBP_REGNUM);
2668 ax_simple (ax, aop_add);
2669 value->type = register_type (gdbarch, AMD64_RIP_REGNUM);
2670 value->kind = axs_lvalue_memory;
2674 /* Signal trampolines. */
2676 /* FIXME: kettenis/20030419: Perhaps, we can unify the 32-bit and
2677 64-bit variants. This would require using identical frame caches
2678 on both platforms. */
2680 static struct amd64_frame_cache *
2681 amd64_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
2683 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2684 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2685 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2686 struct amd64_frame_cache *cache;
2692 return (struct amd64_frame_cache *) *this_cache;
2694 cache = amd64_alloc_frame_cache ();
2698 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2699 cache->base = extract_unsigned_integer (buf, 8, byte_order) - 8;
2701 addr = tdep->sigcontext_addr (this_frame);
2702 gdb_assert (tdep->sc_reg_offset);
2703 gdb_assert (tdep->sc_num_regs <= AMD64_NUM_SAVED_REGS);
2704 for (i = 0; i < tdep->sc_num_regs; i++)
2705 if (tdep->sc_reg_offset[i] != -1)
2706 cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
2710 CATCH (ex, RETURN_MASK_ERROR)
2712 if (ex.error != NOT_AVAILABLE_ERROR)
2713 throw_exception (ex);
2717 *this_cache = cache;
2721 static enum unwind_stop_reason
2722 amd64_sigtramp_frame_unwind_stop_reason (struct frame_info *this_frame,
2725 struct amd64_frame_cache *cache =
2726 amd64_sigtramp_frame_cache (this_frame, this_cache);
2729 return UNWIND_UNAVAILABLE;
2731 return UNWIND_NO_REASON;
2735 amd64_sigtramp_frame_this_id (struct frame_info *this_frame,
2736 void **this_cache, struct frame_id *this_id)
2738 struct amd64_frame_cache *cache =
2739 amd64_sigtramp_frame_cache (this_frame, this_cache);
2742 (*this_id) = frame_id_build_unavailable_stack (get_frame_pc (this_frame));
2743 else if (cache->base == 0)
2745 /* This marks the outermost frame. */
2749 (*this_id) = frame_id_build (cache->base + 16, get_frame_pc (this_frame));
2752 static struct value *
2753 amd64_sigtramp_frame_prev_register (struct frame_info *this_frame,
2754 void **this_cache, int regnum)
2756 /* Make sure we've initialized the cache. */
2757 amd64_sigtramp_frame_cache (this_frame, this_cache);
2759 return amd64_frame_prev_register (this_frame, this_cache, regnum);
2763 amd64_sigtramp_frame_sniffer (const struct frame_unwind *self,
2764 struct frame_info *this_frame,
2767 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
2769 /* We shouldn't even bother if we don't have a sigcontext_addr
2771 if (tdep->sigcontext_addr == NULL)
2774 if (tdep->sigtramp_p != NULL)
2776 if (tdep->sigtramp_p (this_frame))
2780 if (tdep->sigtramp_start != 0)
2782 CORE_ADDR pc = get_frame_pc (this_frame);
2784 gdb_assert (tdep->sigtramp_end != 0);
2785 if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
2792 static const struct frame_unwind amd64_sigtramp_frame_unwind =
2795 amd64_sigtramp_frame_unwind_stop_reason,
2796 amd64_sigtramp_frame_this_id,
2797 amd64_sigtramp_frame_prev_register,
2799 amd64_sigtramp_frame_sniffer
2804 amd64_frame_base_address (struct frame_info *this_frame, void **this_cache)
2806 struct amd64_frame_cache *cache =
2807 amd64_frame_cache (this_frame, this_cache);
2812 static const struct frame_base amd64_frame_base =
2814 &amd64_frame_unwind,
2815 amd64_frame_base_address,
2816 amd64_frame_base_address,
2817 amd64_frame_base_address
2820 /* Normal frames, but in a function epilogue. */
2822 /* Implement the stack_frame_destroyed_p gdbarch method.
2824 The epilogue is defined here as the 'ret' instruction, which will
2825 follow any instruction such as 'leave' or 'pop %ebp' that destroys
2826 the function's stack frame. */
2829 amd64_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
2832 struct compunit_symtab *cust;
2834 cust = find_pc_compunit_symtab (pc);
2835 if (cust != NULL && COMPUNIT_EPILOGUE_UNWIND_VALID (cust))
2838 if (target_read_memory (pc, &insn, 1))
2839 return 0; /* Can't read memory at pc. */
2841 if (insn != 0xc3) /* 'ret' instruction. */
2848 amd64_epilogue_frame_sniffer (const struct frame_unwind *self,
2849 struct frame_info *this_frame,
2850 void **this_prologue_cache)
2852 if (frame_relative_level (this_frame) == 0)
2853 return amd64_stack_frame_destroyed_p (get_frame_arch (this_frame),
2854 get_frame_pc (this_frame));
2859 static struct amd64_frame_cache *
2860 amd64_epilogue_frame_cache (struct frame_info *this_frame, void **this_cache)
2862 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2863 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2864 struct amd64_frame_cache *cache;
2868 return (struct amd64_frame_cache *) *this_cache;
2870 cache = amd64_alloc_frame_cache ();
2871 *this_cache = cache;
2875 /* Cache base will be %esp plus cache->sp_offset (-8). */
2876 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2877 cache->base = extract_unsigned_integer (buf, 8,
2878 byte_order) + cache->sp_offset;
2880 /* Cache pc will be the frame func. */
2881 cache->pc = get_frame_pc (this_frame);
2883 /* The saved %esp will be at cache->base plus 16. */
2884 cache->saved_sp = cache->base + 16;
2886 /* The saved %eip will be at cache->base plus 8. */
2887 cache->saved_regs[AMD64_RIP_REGNUM] = cache->base + 8;
2891 CATCH (ex, RETURN_MASK_ERROR)
2893 if (ex.error != NOT_AVAILABLE_ERROR)
2894 throw_exception (ex);
2901 static enum unwind_stop_reason
2902 amd64_epilogue_frame_unwind_stop_reason (struct frame_info *this_frame,
2905 struct amd64_frame_cache *cache
2906 = amd64_epilogue_frame_cache (this_frame, this_cache);
2909 return UNWIND_UNAVAILABLE;
2911 return UNWIND_NO_REASON;
2915 amd64_epilogue_frame_this_id (struct frame_info *this_frame,
2917 struct frame_id *this_id)
2919 struct amd64_frame_cache *cache = amd64_epilogue_frame_cache (this_frame,
2923 (*this_id) = frame_id_build_unavailable_stack (cache->pc);
2925 (*this_id) = frame_id_build (cache->base + 8, cache->pc);
2928 static const struct frame_unwind amd64_epilogue_frame_unwind =
2931 amd64_epilogue_frame_unwind_stop_reason,
2932 amd64_epilogue_frame_this_id,
2933 amd64_frame_prev_register,
2935 amd64_epilogue_frame_sniffer
2938 static struct frame_id
2939 amd64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
2943 fp = get_frame_register_unsigned (this_frame, AMD64_RBP_REGNUM);
2945 return frame_id_build (fp + 16, get_frame_pc (this_frame));
2948 /* 16 byte align the SP per frame requirements. */
2951 amd64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
2953 return sp & -(CORE_ADDR)16;
2957 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
2958 in the floating-point register set REGSET to register cache
2959 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
2962 amd64_supply_fpregset (const struct regset *regset, struct regcache *regcache,
2963 int regnum, const void *fpregs, size_t len)
2965 struct gdbarch *gdbarch = regcache->arch ();
2966 const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2968 gdb_assert (len >= tdep->sizeof_fpregset);
2969 amd64_supply_fxsave (regcache, regnum, fpregs);
2972 /* Collect register REGNUM from the register cache REGCACHE and store
2973 it in the buffer specified by FPREGS and LEN as described by the
2974 floating-point register set REGSET. If REGNUM is -1, do this for
2975 all registers in REGSET. */
2978 amd64_collect_fpregset (const struct regset *regset,
2979 const struct regcache *regcache,
2980 int regnum, void *fpregs, size_t len)
2982 struct gdbarch *gdbarch = regcache->arch ();
2983 const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2985 gdb_assert (len >= tdep->sizeof_fpregset);
2986 amd64_collect_fxsave (regcache, regnum, fpregs);
2989 const struct regset amd64_fpregset =
2991 NULL, amd64_supply_fpregset, amd64_collect_fpregset
2995 /* Figure out where the longjmp will land. Slurp the jmp_buf out of
2996 %rdi. We expect its value to be a pointer to the jmp_buf structure
2997 from which we extract the address that we will land at. This
2998 address is copied into PC. This routine returns non-zero on
3002 amd64_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
3006 struct gdbarch *gdbarch = get_frame_arch (frame);
3007 int jb_pc_offset = gdbarch_tdep (gdbarch)->jb_pc_offset;
3008 int len = TYPE_LENGTH (builtin_type (gdbarch)->builtin_func_ptr);
3010 /* If JB_PC_OFFSET is -1, we have no way to find out where the
3011 longjmp will land. */
3012 if (jb_pc_offset == -1)
3015 get_frame_register (frame, AMD64_RDI_REGNUM, buf);
3016 jb_addr= extract_typed_address
3017 (buf, builtin_type (gdbarch)->builtin_data_ptr);
3018 if (target_read_memory (jb_addr + jb_pc_offset, buf, len))
3021 *pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
3026 static const int amd64_record_regmap[] =
3028 AMD64_RAX_REGNUM, AMD64_RCX_REGNUM, AMD64_RDX_REGNUM, AMD64_RBX_REGNUM,
3029 AMD64_RSP_REGNUM, AMD64_RBP_REGNUM, AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
3030 AMD64_R8_REGNUM, AMD64_R9_REGNUM, AMD64_R10_REGNUM, AMD64_R11_REGNUM,
3031 AMD64_R12_REGNUM, AMD64_R13_REGNUM, AMD64_R14_REGNUM, AMD64_R15_REGNUM,
3032 AMD64_RIP_REGNUM, AMD64_EFLAGS_REGNUM, AMD64_CS_REGNUM, AMD64_SS_REGNUM,
3033 AMD64_DS_REGNUM, AMD64_ES_REGNUM, AMD64_FS_REGNUM, AMD64_GS_REGNUM
3036 /* Implement the "in_indirect_branch_thunk" gdbarch function. */
3039 amd64_in_indirect_branch_thunk (struct gdbarch *gdbarch, CORE_ADDR pc)
3041 return x86_in_indirect_branch_thunk (pc, amd64_register_names,
3047 amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
3048 const target_desc *default_tdesc)
3050 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3051 const struct target_desc *tdesc = info.target_desc;
3052 static const char *const stap_integer_prefixes[] = { "$", NULL };
3053 static const char *const stap_register_prefixes[] = { "%", NULL };
3054 static const char *const stap_register_indirection_prefixes[] = { "(",
3056 static const char *const stap_register_indirection_suffixes[] = { ")",
3059 /* AMD64 generally uses `fxsave' instead of `fsave' for saving its
3060 floating-point registers. */
3061 tdep->sizeof_fpregset = I387_SIZEOF_FXSAVE;
3062 tdep->fpregset = &amd64_fpregset;
3064 if (! tdesc_has_registers (tdesc))
3065 tdesc = default_tdesc;
3066 tdep->tdesc = tdesc;
3068 tdep->num_core_regs = AMD64_NUM_GREGS + I387_NUM_REGS;
3069 tdep->register_names = amd64_register_names;
3071 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx512") != NULL)
3073 tdep->zmmh_register_names = amd64_zmmh_names;
3074 tdep->k_register_names = amd64_k_names;
3075 tdep->xmm_avx512_register_names = amd64_xmm_avx512_names;
3076 tdep->ymm16h_register_names = amd64_ymmh_avx512_names;
3078 tdep->num_zmm_regs = 32;
3079 tdep->num_xmm_avx512_regs = 16;
3080 tdep->num_ymm_avx512_regs = 16;
3082 tdep->zmm0h_regnum = AMD64_ZMM0H_REGNUM;
3083 tdep->k0_regnum = AMD64_K0_REGNUM;
3084 tdep->xmm16_regnum = AMD64_XMM16_REGNUM;
3085 tdep->ymm16h_regnum = AMD64_YMM16H_REGNUM;
3088 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx") != NULL)
3090 tdep->ymmh_register_names = amd64_ymmh_names;
3091 tdep->num_ymm_regs = 16;
3092 tdep->ymm0h_regnum = AMD64_YMM0H_REGNUM;
3095 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.mpx") != NULL)
3097 tdep->mpx_register_names = amd64_mpx_names;
3098 tdep->bndcfgu_regnum = AMD64_BNDCFGU_REGNUM;
3099 tdep->bnd0r_regnum = AMD64_BND0R_REGNUM;
3102 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.segments") != NULL)
3104 const struct tdesc_feature *feature =
3105 tdesc_find_feature (tdesc, "org.gnu.gdb.i386.segments");
3106 struct tdesc_arch_data *tdesc_data_segments =
3107 (struct tdesc_arch_data *) info.tdep_info;
3109 tdesc_numbered_register (feature, tdesc_data_segments,
3110 AMD64_FSBASE_REGNUM, "fs_base");
3111 tdesc_numbered_register (feature, tdesc_data_segments,
3112 AMD64_GSBASE_REGNUM, "gs_base");
3115 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.pkeys") != NULL)
3117 tdep->pkeys_register_names = amd64_pkeys_names;
3118 tdep->pkru_regnum = AMD64_PKRU_REGNUM;
3119 tdep->num_pkeys_regs = 1;
3122 tdep->num_byte_regs = 20;
3123 tdep->num_word_regs = 16;
3124 tdep->num_dword_regs = 16;
3125 /* Avoid wiring in the MMX registers for now. */
3126 tdep->num_mmx_regs = 0;
3128 set_gdbarch_pseudo_register_read_value (gdbarch,
3129 amd64_pseudo_register_read_value);
3130 set_gdbarch_pseudo_register_write (gdbarch,
3131 amd64_pseudo_register_write);
3132 set_gdbarch_ax_pseudo_register_collect (gdbarch,
3133 amd64_ax_pseudo_register_collect);
3135 set_tdesc_pseudo_register_name (gdbarch, amd64_pseudo_register_name);
3137 /* AMD64 has an FPU and 16 SSE registers. */
3138 tdep->st0_regnum = AMD64_ST0_REGNUM;
3139 tdep->num_xmm_regs = 16;
3141 /* This is what all the fuss is about. */
3142 set_gdbarch_long_bit (gdbarch, 64);
3143 set_gdbarch_long_long_bit (gdbarch, 64);
3144 set_gdbarch_ptr_bit (gdbarch, 64);
3146 /* In contrast to the i386, on AMD64 a `long double' actually takes
3147 up 128 bits, even though it's still based on the i387 extended
3148 floating-point format which has only 80 significant bits. */
3149 set_gdbarch_long_double_bit (gdbarch, 128);
3151 set_gdbarch_num_regs (gdbarch, AMD64_NUM_REGS);
3153 /* Register numbers of various important registers. */
3154 set_gdbarch_sp_regnum (gdbarch, AMD64_RSP_REGNUM); /* %rsp */
3155 set_gdbarch_pc_regnum (gdbarch, AMD64_RIP_REGNUM); /* %rip */
3156 set_gdbarch_ps_regnum (gdbarch, AMD64_EFLAGS_REGNUM); /* %eflags */
3157 set_gdbarch_fp0_regnum (gdbarch, AMD64_ST0_REGNUM); /* %st(0) */
3159 /* The "default" register numbering scheme for AMD64 is referred to
3160 as the "DWARF Register Number Mapping" in the System V psABI.
3161 The preferred debugging format for all known AMD64 targets is
3162 actually DWARF2, and GCC doesn't seem to support DWARF (that is
3163 DWARF-1), but we provide the same mapping just in case. This
3164 mapping is also used for stabs, which GCC does support. */
3165 set_gdbarch_stab_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
3166 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
3168 /* We don't override SDB_REG_RO_REGNUM, since COFF doesn't seem to
3169 be in use on any of the supported AMD64 targets. */
3171 /* Call dummy code. */
3172 set_gdbarch_push_dummy_call (gdbarch, amd64_push_dummy_call);
3173 set_gdbarch_frame_align (gdbarch, amd64_frame_align);
3174 set_gdbarch_frame_red_zone_size (gdbarch, 128);
3176 set_gdbarch_convert_register_p (gdbarch, i387_convert_register_p);
3177 set_gdbarch_register_to_value (gdbarch, i387_register_to_value);
3178 set_gdbarch_value_to_register (gdbarch, i387_value_to_register);
3180 set_gdbarch_return_value (gdbarch, amd64_return_value);
3182 set_gdbarch_skip_prologue (gdbarch, amd64_skip_prologue);
3184 tdep->record_regmap = amd64_record_regmap;
3186 set_gdbarch_dummy_id (gdbarch, amd64_dummy_id);
3188 /* Hook the function epilogue frame unwinder. This unwinder is
3189 appended to the list first, so that it supercedes the other
3190 unwinders in function epilogues. */
3191 frame_unwind_prepend_unwinder (gdbarch, &amd64_epilogue_frame_unwind);
3193 /* Hook the prologue-based frame unwinders. */
3194 frame_unwind_append_unwinder (gdbarch, &amd64_sigtramp_frame_unwind);
3195 frame_unwind_append_unwinder (gdbarch, &amd64_frame_unwind);
3196 frame_base_set_default (gdbarch, &amd64_frame_base);
3198 set_gdbarch_get_longjmp_target (gdbarch, amd64_get_longjmp_target);
3200 set_gdbarch_relocate_instruction (gdbarch, amd64_relocate_instruction);
3202 set_gdbarch_gen_return_address (gdbarch, amd64_gen_return_address);
3204 /* SystemTap variables and functions. */
3205 set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
3206 set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
3207 set_gdbarch_stap_register_indirection_prefixes (gdbarch,
3208 stap_register_indirection_prefixes);
3209 set_gdbarch_stap_register_indirection_suffixes (gdbarch,
3210 stap_register_indirection_suffixes);
3211 set_gdbarch_stap_is_single_operand (gdbarch,
3212 i386_stap_is_single_operand);
3213 set_gdbarch_stap_parse_special_token (gdbarch,
3214 i386_stap_parse_special_token);
3215 set_gdbarch_insn_is_call (gdbarch, amd64_insn_is_call);
3216 set_gdbarch_insn_is_ret (gdbarch, amd64_insn_is_ret);
3217 set_gdbarch_insn_is_jump (gdbarch, amd64_insn_is_jump);
3219 set_gdbarch_in_indirect_branch_thunk (gdbarch,
3220 amd64_in_indirect_branch_thunk);
3223 /* Initialize ARCH for x86-64, no osabi. */
3226 amd64_none_init_abi (gdbarch_info info, gdbarch *arch)
3228 amd64_init_abi (info, arch, amd64_target_description (X86_XSTATE_SSE_MASK));
3231 static struct type *
3232 amd64_x32_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
3234 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3236 switch (regnum - tdep->eax_regnum)
3238 case AMD64_RBP_REGNUM: /* %ebp */
3239 case AMD64_RSP_REGNUM: /* %esp */
3240 return builtin_type (gdbarch)->builtin_data_ptr;
3241 case AMD64_RIP_REGNUM: /* %eip */
3242 return builtin_type (gdbarch)->builtin_func_ptr;
3245 return i386_pseudo_register_type (gdbarch, regnum);
3249 amd64_x32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
3250 const target_desc *default_tdesc)
3252 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3254 amd64_init_abi (info, gdbarch, default_tdesc);
3256 tdep->num_dword_regs = 17;
3257 set_tdesc_pseudo_register_type (gdbarch, amd64_x32_pseudo_register_type);
3259 set_gdbarch_long_bit (gdbarch, 32);
3260 set_gdbarch_ptr_bit (gdbarch, 32);
3263 /* Initialize ARCH for x64-32, no osabi. */
3266 amd64_x32_none_init_abi (gdbarch_info info, gdbarch *arch)
3268 amd64_x32_init_abi (info, arch,
3269 amd64_target_description (X86_XSTATE_SSE_MASK));
3272 /* Return the target description for a specified XSAVE feature mask. */
3274 const struct target_desc *
3275 amd64_target_description (uint64_t xcr0)
3277 static target_desc *amd64_tdescs \
3278 [2/*AVX*/][2/*MPX*/][2/*AVX512*/][2/*PKRU*/] = {};
3279 target_desc **tdesc;
3281 tdesc = &amd64_tdescs[(xcr0 & X86_XSTATE_AVX) ? 1 : 0]
3282 [(xcr0 & X86_XSTATE_MPX) ? 1 : 0]
3283 [(xcr0 & X86_XSTATE_AVX512) ? 1 : 0]
3284 [(xcr0 & X86_XSTATE_PKRU) ? 1 : 0];
3287 *tdesc = amd64_create_target_description (xcr0, false, false);
3293 _initialize_amd64_tdep (void)
3295 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, GDB_OSABI_NONE,
3296 amd64_none_init_abi);
3297 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x64_32, GDB_OSABI_NONE,
3298 amd64_x32_none_init_abi);
3306 { "i386/amd64.xml", X86_XSTATE_SSE_MASK },
3307 { "i386/amd64-avx.xml", X86_XSTATE_AVX_MASK },
3308 { "i386/amd64-mpx.xml", X86_XSTATE_MPX_MASK },
3309 { "i386/amd64-avx-mpx.xml", X86_XSTATE_AVX_MPX_MASK },
3310 { "i386/amd64-avx-avx512.xml", X86_XSTATE_AVX_AVX512_MASK },
3311 { "i386/amd64-avx-mpx-avx512-pku.xml",
3312 X86_XSTATE_AVX_MPX_AVX512_PKU_MASK },
3315 for (auto &a : xml_masks)
3317 auto tdesc = amd64_target_description (a.mask);
3319 selftests::record_xml_tdesc (a.xml, tdesc);
3321 #endif /* GDB_SELF_TEST */
3325 /* The 64-bit FXSAVE format differs from the 32-bit format in the
3326 sense that the instruction pointer and data pointer are simply
3327 64-bit offsets into the code segment and the data segment instead
3328 of a selector offset pair. The functions below store the upper 32
3329 bits of these pointers (instead of just the 16-bits of the segment
3332 /* Fill register REGNUM in REGCACHE with the appropriate
3333 floating-point or SSE register value from *FXSAVE. If REGNUM is
3334 -1, do this for all registers. This function masks off any of the
3335 reserved bits in *FXSAVE. */
3338 amd64_supply_fxsave (struct regcache *regcache, int regnum,
3341 struct gdbarch *gdbarch = regcache->arch ();
3342 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3344 i387_supply_fxsave (regcache, regnum, fxsave);
3347 && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3349 const gdb_byte *regs = (const gdb_byte *) fxsave;
3351 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3352 regcache->raw_supply (I387_FISEG_REGNUM (tdep), regs + 12);
3353 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3354 regcache->raw_supply (I387_FOSEG_REGNUM (tdep), regs + 20);
3358 /* Similar to amd64_supply_fxsave, but use XSAVE extended state. */
3361 amd64_supply_xsave (struct regcache *regcache, int regnum,
3364 struct gdbarch *gdbarch = regcache->arch ();
3365 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3367 i387_supply_xsave (regcache, regnum, xsave);
3370 && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3372 const gdb_byte *regs = (const gdb_byte *) xsave;
3373 static const gdb_byte zero[I386_MAX_REGISTER_SIZE] = { 0 };
3376 clear_bv = i387_xsave_get_clear_bv (gdbarch, xsave);
3378 /* If the FISEG and FOSEG registers have not been initialised yet
3379 (their CLEAR_BV bit is set) then their default values of zero will
3380 have already been setup by I387_SUPPLY_XSAVE. */
3381 if (!(clear_bv & X86_XSTATE_X87))
3383 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3384 regcache->raw_supply (I387_FISEG_REGNUM (tdep), regs + 12);
3385 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3386 regcache->raw_supply (I387_FOSEG_REGNUM (tdep), regs + 20);
3391 /* Fill register REGNUM (if it is a floating-point or SSE register) in
3392 *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
3393 all registers. This function doesn't touch any of the reserved
3397 amd64_collect_fxsave (const struct regcache *regcache, int regnum,
3400 struct gdbarch *gdbarch = regcache->arch ();
3401 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3402 gdb_byte *regs = (gdb_byte *) fxsave;
3404 i387_collect_fxsave (regcache, regnum, fxsave);
3406 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3408 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3409 regcache->raw_collect (I387_FISEG_REGNUM (tdep), regs + 12);
3410 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3411 regcache->raw_collect (I387_FOSEG_REGNUM (tdep), regs + 20);
3415 /* Similar to amd64_collect_fxsave, but use XSAVE extended state. */
3418 amd64_collect_xsave (const struct regcache *regcache, int regnum,
3419 void *xsave, int gcore)
3421 struct gdbarch *gdbarch = regcache->arch ();
3422 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3423 gdb_byte *regs = (gdb_byte *) xsave;
3425 i387_collect_xsave (regcache, regnum, xsave, gcore);
3427 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3429 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3430 regcache->raw_collect (I387_FISEG_REGNUM (tdep),
3432 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3433 regcache->raw_collect (I387_FOSEG_REGNUM (tdep),