More updated translations
[binutils-gdb.git] / gdb / amd64-tdep.c
blob3c75f2fa22db77facf0cc8b2f00ba9093d719abb
1 /* Target-dependent code for AMD64.
3 Copyright (C) 2001-2024 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/>. */
22 #include "extract-store-integer.h"
23 #include "language.h"
24 #include "opcode/i386.h"
25 #include "dis-asm.h"
26 #include "arch-utils.h"
27 #include "dummy-frame.h"
28 #include "frame.h"
29 #include "frame-base.h"
30 #include "frame-unwind.h"
31 #include "inferior.h"
32 #include "infrun.h"
33 #include "cli/cli-cmds.h"
34 #include "gdbcore.h"
35 #include "objfiles.h"
36 #include "regcache.h"
37 #include "regset.h"
38 #include "symfile.h"
39 #include "disasm.h"
40 #include "amd64-tdep.h"
41 #include "i387-tdep.h"
42 #include "gdbsupport/x86-xstate.h"
43 #include <algorithm>
44 #include "target-descriptions.h"
45 #include "arch/amd64.h"
46 #include "producer.h"
47 #include "ax.h"
48 #include "ax-gdb.h"
49 #include "gdbsupport/byte-vector.h"
50 #include "osabi.h"
51 #include "x86-tdep.h"
52 #include "amd64-ravenscar-thread.h"
54 /* Note that the AMD64 architecture was previously known as x86-64.
55 The latter is (forever) engraved into the canonical system name as
56 returned by config.guess, and used as the name for the AMD64 port
57 of GNU/Linux. The BSD's have renamed their ports to amd64; they
58 don't like to shout. For GDB we prefer the amd64_-prefix over the
59 x86_64_-prefix since it's so much easier to type. */
61 /* Register information. */
63 static const char * const amd64_register_names[] =
65 "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp",
67 /* %r8 is indeed register number 8. */
68 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
69 "rip", "eflags", "cs", "ss", "ds", "es", "fs", "gs",
71 /* %st0 is register number 24. */
72 "st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7",
73 "fctrl", "fstat", "ftag", "fiseg", "fioff", "foseg", "fooff", "fop",
75 /* %xmm0 is register number 40. */
76 "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
77 "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
78 "mxcsr",
81 static const char * const amd64_ymm_names[] =
83 "ymm0", "ymm1", "ymm2", "ymm3",
84 "ymm4", "ymm5", "ymm6", "ymm7",
85 "ymm8", "ymm9", "ymm10", "ymm11",
86 "ymm12", "ymm13", "ymm14", "ymm15"
89 static const char * const amd64_ymm_avx512_names[] =
91 "ymm16", "ymm17", "ymm18", "ymm19",
92 "ymm20", "ymm21", "ymm22", "ymm23",
93 "ymm24", "ymm25", "ymm26", "ymm27",
94 "ymm28", "ymm29", "ymm30", "ymm31"
97 static const char * const amd64_ymmh_names[] =
99 "ymm0h", "ymm1h", "ymm2h", "ymm3h",
100 "ymm4h", "ymm5h", "ymm6h", "ymm7h",
101 "ymm8h", "ymm9h", "ymm10h", "ymm11h",
102 "ymm12h", "ymm13h", "ymm14h", "ymm15h"
105 static const char * const amd64_ymmh_avx512_names[] =
107 "ymm16h", "ymm17h", "ymm18h", "ymm19h",
108 "ymm20h", "ymm21h", "ymm22h", "ymm23h",
109 "ymm24h", "ymm25h", "ymm26h", "ymm27h",
110 "ymm28h", "ymm29h", "ymm30h", "ymm31h"
113 static const char * const amd64_k_names[] =
115 "k0", "k1", "k2", "k3",
116 "k4", "k5", "k6", "k7"
119 static const char * const amd64_zmmh_names[] =
121 "zmm0h", "zmm1h", "zmm2h", "zmm3h",
122 "zmm4h", "zmm5h", "zmm6h", "zmm7h",
123 "zmm8h", "zmm9h", "zmm10h", "zmm11h",
124 "zmm12h", "zmm13h", "zmm14h", "zmm15h",
125 "zmm16h", "zmm17h", "zmm18h", "zmm19h",
126 "zmm20h", "zmm21h", "zmm22h", "zmm23h",
127 "zmm24h", "zmm25h", "zmm26h", "zmm27h",
128 "zmm28h", "zmm29h", "zmm30h", "zmm31h"
131 static const char * const amd64_zmm_names[] =
133 "zmm0", "zmm1", "zmm2", "zmm3",
134 "zmm4", "zmm5", "zmm6", "zmm7",
135 "zmm8", "zmm9", "zmm10", "zmm11",
136 "zmm12", "zmm13", "zmm14", "zmm15",
137 "zmm16", "zmm17", "zmm18", "zmm19",
138 "zmm20", "zmm21", "zmm22", "zmm23",
139 "zmm24", "zmm25", "zmm26", "zmm27",
140 "zmm28", "zmm29", "zmm30", "zmm31"
143 static const char * const amd64_xmm_avx512_names[] = {
144 "xmm16", "xmm17", "xmm18", "xmm19",
145 "xmm20", "xmm21", "xmm22", "xmm23",
146 "xmm24", "xmm25", "xmm26", "xmm27",
147 "xmm28", "xmm29", "xmm30", "xmm31"
150 static const char * const amd64_pkeys_names[] = {
151 "pkru"
154 /* DWARF Register Number Mapping as defined in the System V psABI,
155 section 3.6. */
157 static int amd64_dwarf_regmap[] =
159 /* General Purpose Registers RAX, RDX, RCX, RBX, RSI, RDI. */
160 AMD64_RAX_REGNUM, AMD64_RDX_REGNUM,
161 AMD64_RCX_REGNUM, AMD64_RBX_REGNUM,
162 AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
164 /* Frame Pointer Register RBP. */
165 AMD64_RBP_REGNUM,
167 /* Stack Pointer Register RSP. */
168 AMD64_RSP_REGNUM,
170 /* Extended Integer Registers 8 - 15. */
171 AMD64_R8_REGNUM, /* %r8 */
172 AMD64_R9_REGNUM, /* %r9 */
173 AMD64_R10_REGNUM, /* %r10 */
174 AMD64_R11_REGNUM, /* %r11 */
175 AMD64_R12_REGNUM, /* %r12 */
176 AMD64_R13_REGNUM, /* %r13 */
177 AMD64_R14_REGNUM, /* %r14 */
178 AMD64_R15_REGNUM, /* %r15 */
180 /* Return Address RA. Mapped to RIP. */
181 AMD64_RIP_REGNUM,
183 /* SSE Registers 0 - 7. */
184 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
185 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
186 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
187 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
189 /* Extended SSE Registers 8 - 15. */
190 AMD64_XMM0_REGNUM + 8, AMD64_XMM0_REGNUM + 9,
191 AMD64_XMM0_REGNUM + 10, AMD64_XMM0_REGNUM + 11,
192 AMD64_XMM0_REGNUM + 12, AMD64_XMM0_REGNUM + 13,
193 AMD64_XMM0_REGNUM + 14, AMD64_XMM0_REGNUM + 15,
195 /* Floating Point Registers 0-7. */
196 AMD64_ST0_REGNUM + 0, AMD64_ST0_REGNUM + 1,
197 AMD64_ST0_REGNUM + 2, AMD64_ST0_REGNUM + 3,
198 AMD64_ST0_REGNUM + 4, AMD64_ST0_REGNUM + 5,
199 AMD64_ST0_REGNUM + 6, AMD64_ST0_REGNUM + 7,
201 /* MMX Registers 0 - 7.
202 We have to handle those registers specifically, as their register
203 number within GDB depends on the target (or they may even not be
204 available at all). */
205 -1, -1, -1, -1, -1, -1, -1, -1,
207 /* Control and Status Flags Register. */
208 AMD64_EFLAGS_REGNUM,
210 /* Selector Registers. */
211 AMD64_ES_REGNUM,
212 AMD64_CS_REGNUM,
213 AMD64_SS_REGNUM,
214 AMD64_DS_REGNUM,
215 AMD64_FS_REGNUM,
216 AMD64_GS_REGNUM,
220 /* Segment Base Address Registers. */
226 /* Special Selector Registers. */
230 /* Floating Point Control Registers. */
231 AMD64_MXCSR_REGNUM,
232 AMD64_FCTRL_REGNUM,
233 AMD64_FSTAT_REGNUM,
235 /* XMM16-XMM31. */
236 AMD64_XMM16_REGNUM + 0, AMD64_XMM16_REGNUM + 1,
237 AMD64_XMM16_REGNUM + 2, AMD64_XMM16_REGNUM + 3,
238 AMD64_XMM16_REGNUM + 4, AMD64_XMM16_REGNUM + 5,
239 AMD64_XMM16_REGNUM + 6, AMD64_XMM16_REGNUM + 7,
240 AMD64_XMM16_REGNUM + 8, AMD64_XMM16_REGNUM + 9,
241 AMD64_XMM16_REGNUM + 10, AMD64_XMM16_REGNUM + 11,
242 AMD64_XMM16_REGNUM + 12, AMD64_XMM16_REGNUM + 13,
243 AMD64_XMM16_REGNUM + 14, AMD64_XMM16_REGNUM + 15,
245 /* Reserved. */
246 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
247 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
248 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
250 /* Mask Registers. */
251 AMD64_K0_REGNUM + 0, AMD64_K0_REGNUM + 1,
252 AMD64_K0_REGNUM + 2, AMD64_K0_REGNUM + 3,
253 AMD64_K0_REGNUM + 4, AMD64_K0_REGNUM + 5,
254 AMD64_K0_REGNUM + 6, AMD64_K0_REGNUM + 7
257 static const int amd64_dwarf_regmap_len =
258 (sizeof (amd64_dwarf_regmap) / sizeof (amd64_dwarf_regmap[0]));
260 /* Convert DWARF register number REG to the appropriate register
261 number used by GDB. */
263 static int
264 amd64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
266 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
267 int ymm0_regnum = tdep->ymm0_regnum;
268 int regnum = -1;
270 if (reg >= 0 && reg < amd64_dwarf_regmap_len)
271 regnum = amd64_dwarf_regmap[reg];
273 if (ymm0_regnum >= 0 && i386_xmm_regnum_p (gdbarch, regnum))
274 regnum += ymm0_regnum - I387_XMM0_REGNUM (tdep);
276 return regnum;
279 /* Map architectural register numbers to gdb register numbers. */
281 static const int amd64_arch_regmap[16] =
283 AMD64_RAX_REGNUM, /* %rax */
284 AMD64_RCX_REGNUM, /* %rcx */
285 AMD64_RDX_REGNUM, /* %rdx */
286 AMD64_RBX_REGNUM, /* %rbx */
287 AMD64_RSP_REGNUM, /* %rsp */
288 AMD64_RBP_REGNUM, /* %rbp */
289 AMD64_RSI_REGNUM, /* %rsi */
290 AMD64_RDI_REGNUM, /* %rdi */
291 AMD64_R8_REGNUM, /* %r8 */
292 AMD64_R9_REGNUM, /* %r9 */
293 AMD64_R10_REGNUM, /* %r10 */
294 AMD64_R11_REGNUM, /* %r11 */
295 AMD64_R12_REGNUM, /* %r12 */
296 AMD64_R13_REGNUM, /* %r13 */
297 AMD64_R14_REGNUM, /* %r14 */
298 AMD64_R15_REGNUM /* %r15 */
301 static const int amd64_arch_regmap_len =
302 (sizeof (amd64_arch_regmap) / sizeof (amd64_arch_regmap[0]));
304 /* Convert architectural register number REG to the appropriate register
305 number used by GDB. */
307 static int
308 amd64_arch_reg_to_regnum (int reg)
310 gdb_assert (reg >= 0 && reg < amd64_arch_regmap_len);
312 return amd64_arch_regmap[reg];
315 /* Register names for byte pseudo-registers. */
317 static const char * const amd64_byte_names[] =
319 "al", "bl", "cl", "dl", "sil", "dil", "bpl", "spl",
320 "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l",
321 "ah", "bh", "ch", "dh"
324 /* Number of lower byte registers. */
325 #define AMD64_NUM_LOWER_BYTE_REGS 16
327 /* Register names for word pseudo-registers. */
329 static const char * const amd64_word_names[] =
331 "ax", "bx", "cx", "dx", "si", "di", "bp", "",
332 "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
335 /* Register names for dword pseudo-registers. */
337 static const char * const amd64_dword_names[] =
339 "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp",
340 "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
341 "eip"
344 /* Return the name of register REGNUM. */
346 static const char *
347 amd64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
349 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
350 if (i386_byte_regnum_p (gdbarch, regnum))
351 return amd64_byte_names[regnum - tdep->al_regnum];
352 else if (i386_zmm_regnum_p (gdbarch, regnum))
353 return amd64_zmm_names[regnum - tdep->zmm0_regnum];
354 else if (i386_ymm_regnum_p (gdbarch, regnum))
355 return amd64_ymm_names[regnum - tdep->ymm0_regnum];
356 else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
357 return amd64_ymm_avx512_names[regnum - tdep->ymm16_regnum];
358 else if (i386_word_regnum_p (gdbarch, regnum))
359 return amd64_word_names[regnum - tdep->ax_regnum];
360 else if (i386_dword_regnum_p (gdbarch, regnum))
361 return amd64_dword_names[regnum - tdep->eax_regnum];
362 else
363 return i386_pseudo_register_name (gdbarch, regnum);
366 static value *
367 amd64_pseudo_register_read_value (gdbarch *gdbarch, const frame_info_ptr &next_frame,
368 int regnum)
370 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
372 if (i386_byte_regnum_p (gdbarch, regnum))
374 int gpnum = regnum - tdep->al_regnum;
376 /* Extract (always little endian). */
377 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
379 gpnum -= AMD64_NUM_LOWER_BYTE_REGS;
381 /* Special handling for AH, BH, CH, DH. */
382 return pseudo_from_raw_part (next_frame, regnum, gpnum, 1);
384 else
385 return pseudo_from_raw_part (next_frame, regnum, gpnum, 0);
387 else if (i386_dword_regnum_p (gdbarch, regnum))
389 int gpnum = regnum - tdep->eax_regnum;
391 return pseudo_from_raw_part (next_frame, regnum, gpnum, 0);
393 else
394 return i386_pseudo_register_read_value (gdbarch, next_frame, regnum);
397 static void
398 amd64_pseudo_register_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
399 int regnum, gdb::array_view<const gdb_byte> buf)
401 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
403 if (i386_byte_regnum_p (gdbarch, regnum))
405 int gpnum = regnum - tdep->al_regnum;
407 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
409 gpnum -= AMD64_NUM_LOWER_BYTE_REGS;
410 pseudo_to_raw_part (next_frame, buf, gpnum, 1);
412 else
413 pseudo_to_raw_part (next_frame, buf, gpnum, 0);
415 else if (i386_dword_regnum_p (gdbarch, regnum))
417 int gpnum = regnum - tdep->eax_regnum;
418 pseudo_to_raw_part (next_frame, buf, gpnum, 0);
420 else
421 i386_pseudo_register_write (gdbarch, next_frame, regnum, buf);
424 /* Implement the 'ax_pseudo_register_collect' gdbarch method. */
426 static int
427 amd64_ax_pseudo_register_collect (struct gdbarch *gdbarch,
428 struct agent_expr *ax, int regnum)
430 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
432 if (i386_byte_regnum_p (gdbarch, regnum))
434 int gpnum = regnum - tdep->al_regnum;
436 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
437 ax_reg_mask (ax, gpnum - AMD64_NUM_LOWER_BYTE_REGS);
438 else
439 ax_reg_mask (ax, gpnum);
440 return 0;
442 else if (i386_dword_regnum_p (gdbarch, regnum))
444 int gpnum = regnum - tdep->eax_regnum;
446 ax_reg_mask (ax, gpnum);
447 return 0;
449 else
450 return i386_ax_pseudo_register_collect (gdbarch, ax, regnum);
455 /* Register classes as defined in the psABI. */
457 enum amd64_reg_class
459 AMD64_INTEGER,
460 AMD64_SSE,
461 AMD64_SSEUP,
462 AMD64_X87,
463 AMD64_X87UP,
464 AMD64_COMPLEX_X87,
465 AMD64_NO_CLASS,
466 AMD64_MEMORY
469 /* Return the union class of CLASS1 and CLASS2. See the psABI for
470 details. */
472 static enum amd64_reg_class
473 amd64_merge_classes (enum amd64_reg_class class1, enum amd64_reg_class class2)
475 /* Rule (a): If both classes are equal, this is the resulting class. */
476 if (class1 == class2)
477 return class1;
479 /* Rule (b): If one of the classes is NO_CLASS, the resulting class
480 is the other class. */
481 if (class1 == AMD64_NO_CLASS)
482 return class2;
483 if (class2 == AMD64_NO_CLASS)
484 return class1;
486 /* Rule (c): If one of the classes is MEMORY, the result is MEMORY. */
487 if (class1 == AMD64_MEMORY || class2 == AMD64_MEMORY)
488 return AMD64_MEMORY;
490 /* Rule (d): If one of the classes is INTEGER, the result is INTEGER. */
491 if (class1 == AMD64_INTEGER || class2 == AMD64_INTEGER)
492 return AMD64_INTEGER;
494 /* Rule (e): If one of the classes is X87, X87UP, COMPLEX_X87 class,
495 MEMORY is used as class. */
496 if (class1 == AMD64_X87 || class1 == AMD64_X87UP
497 || class1 == AMD64_COMPLEX_X87 || class2 == AMD64_X87
498 || class2 == AMD64_X87UP || class2 == AMD64_COMPLEX_X87)
499 return AMD64_MEMORY;
501 /* Rule (f): Otherwise class SSE is used. */
502 return AMD64_SSE;
505 static void amd64_classify (struct type *type, enum amd64_reg_class theclass[2]);
507 /* Return true if TYPE is a structure or union with unaligned fields. */
509 static bool
510 amd64_has_unaligned_fields (struct type *type)
512 if (type->code () == TYPE_CODE_STRUCT
513 || type->code () == TYPE_CODE_UNION)
515 for (int i = 0; i < type->num_fields (); i++)
517 struct type *subtype = check_typedef (type->field (i).type ());
519 /* Ignore static fields, empty fields (for example nested
520 empty structures), and bitfields (these are handled by
521 the caller). */
522 if (type->field (i).is_static ()
523 || (type->field (i).bitsize () == 0
524 && subtype->length () == 0)
525 || type->field (i).is_packed ())
526 continue;
528 int bitpos = type->field (i).loc_bitpos ();
530 if (bitpos % 8 != 0)
531 return true;
533 int align = type_align (subtype);
534 if (align == 0)
535 error (_("could not determine alignment of type"));
537 int bytepos = bitpos / 8;
538 if (bytepos % align != 0)
539 return true;
541 if (amd64_has_unaligned_fields (subtype))
542 return true;
546 return false;
549 /* Classify field I of TYPE starting at BITOFFSET according to the rules for
550 structures and union types, and store the result in THECLASS. */
552 static void
553 amd64_classify_aggregate_field (struct type *type, int i,
554 enum amd64_reg_class theclass[2],
555 unsigned int bitoffset)
557 struct type *subtype = check_typedef (type->field (i).type ());
558 enum amd64_reg_class subclass[2];
559 int bitsize = type->field (i).bitsize ();
561 if (bitsize == 0)
562 bitsize = subtype->length () * 8;
564 /* Ignore static fields, or empty fields, for example nested
565 empty structures.*/
566 if (type->field (i).is_static () || bitsize == 0)
567 return;
569 int bitpos = bitoffset + type->field (i).loc_bitpos ();
570 int pos = bitpos / 64;
571 int endpos = (bitpos + bitsize - 1) / 64;
573 if (subtype->code () == TYPE_CODE_STRUCT
574 || subtype->code () == TYPE_CODE_UNION)
576 /* Each field of an object is classified recursively. */
577 int j;
578 for (j = 0; j < subtype->num_fields (); j++)
579 amd64_classify_aggregate_field (subtype, j, theclass, bitpos);
580 return;
583 gdb_assert (pos == 0 || pos == 1);
585 amd64_classify (subtype, subclass);
586 theclass[pos] = amd64_merge_classes (theclass[pos], subclass[0]);
587 if (bitsize <= 64 && pos == 0 && endpos == 1)
588 /* This is a bit of an odd case: We have a field that would
589 normally fit in one of the two eightbytes, except that
590 it is placed in a way that this field straddles them.
591 This has been seen with a structure containing an array.
593 The ABI is a bit unclear in this case, but we assume that
594 this field's class (stored in subclass[0]) must also be merged
595 into class[1]. In other words, our field has a piece stored
596 in the second eight-byte, and thus its class applies to
597 the second eight-byte as well.
599 In the case where the field length exceeds 8 bytes,
600 it should not be necessary to merge the field class
601 into class[1]. As LEN > 8, subclass[1] is necessarily
602 different from AMD64_NO_CLASS. If subclass[1] is equal
603 to subclass[0], then the normal class[1]/subclass[1]
604 merging will take care of everything. For subclass[1]
605 to be different from subclass[0], I can only see the case
606 where we have a SSE/SSEUP or X87/X87UP pair, which both
607 use up all 16 bytes of the aggregate, and are already
608 handled just fine (because each portion sits on its own
609 8-byte). */
610 theclass[1] = amd64_merge_classes (theclass[1], subclass[0]);
611 if (pos == 0)
612 theclass[1] = amd64_merge_classes (theclass[1], subclass[1]);
615 /* Classify TYPE according to the rules for aggregate (structures and
616 arrays) and union types, and store the result in CLASS. */
618 static void
619 amd64_classify_aggregate (struct type *type, enum amd64_reg_class theclass[2])
621 /* 1. If the size of an object is larger than two times eight bytes, or
622 it is a non-trivial C++ object, or it has unaligned fields, then it
623 has class memory.
625 It is important that the trivially_copyable check is before the
626 unaligned fields check, as C++ classes with virtual base classes
627 will have fields (for the virtual base classes) with non-constant
628 loc_bitpos attributes, which will cause an assert to trigger within
629 the unaligned field check. As classes with virtual bases are not
630 trivially copyable, checking that first avoids this problem. */
631 if (TYPE_HAS_DYNAMIC_LENGTH (type)
632 || type->length () > 16
633 || !language_pass_by_reference (type).trivially_copyable
634 || amd64_has_unaligned_fields (type))
636 theclass[0] = theclass[1] = AMD64_MEMORY;
637 return;
640 /* 2. Both eightbytes get initialized to class NO_CLASS. */
641 theclass[0] = theclass[1] = AMD64_NO_CLASS;
643 /* 3. Each field of an object is classified recursively so that
644 always two fields are considered. The resulting class is
645 calculated according to the classes of the fields in the
646 eightbyte: */
648 if (type->code () == TYPE_CODE_ARRAY)
650 struct type *subtype = check_typedef (type->target_type ());
652 /* All fields in an array have the same type. */
653 amd64_classify (subtype, theclass);
654 if (type->length () > 8 && theclass[1] == AMD64_NO_CLASS)
655 theclass[1] = theclass[0];
657 else
659 int i;
661 /* Structure or union. */
662 gdb_assert (type->code () == TYPE_CODE_STRUCT
663 || type->code () == TYPE_CODE_UNION);
665 for (i = 0; i < type->num_fields (); i++)
666 amd64_classify_aggregate_field (type, i, theclass, 0);
669 /* 4. Then a post merger cleanup is done: */
671 /* Rule (a): If one of the classes is MEMORY, the whole argument is
672 passed in memory. */
673 if (theclass[0] == AMD64_MEMORY || theclass[1] == AMD64_MEMORY)
674 theclass[0] = theclass[1] = AMD64_MEMORY;
676 /* Rule (b): If SSEUP is not preceded by SSE, it is converted to
677 SSE. */
678 if (theclass[0] == AMD64_SSEUP)
679 theclass[0] = AMD64_SSE;
680 if (theclass[1] == AMD64_SSEUP && theclass[0] != AMD64_SSE)
681 theclass[1] = AMD64_SSE;
684 /* Classify TYPE, and store the result in CLASS. */
686 static void
687 amd64_classify (struct type *type, enum amd64_reg_class theclass[2])
689 enum type_code code = type->code ();
690 int len = type->length ();
692 theclass[0] = theclass[1] = AMD64_NO_CLASS;
694 /* Arguments of types (signed and unsigned) _Bool, char, short, int,
695 long, long long, and pointers are in the INTEGER class. Similarly,
696 range types, used by languages such as Ada, are also in the INTEGER
697 class. */
698 if ((code == TYPE_CODE_INT || code == TYPE_CODE_ENUM
699 || code == TYPE_CODE_BOOL || code == TYPE_CODE_RANGE
700 || code == TYPE_CODE_CHAR
701 || code == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type))
702 && (len == 1 || len == 2 || len == 4 || len == 8))
703 theclass[0] = AMD64_INTEGER;
705 /* Arguments of types _Float16, float, double, _Decimal32, _Decimal64 and
706 __m64 are in class SSE. */
707 else if ((code == TYPE_CODE_FLT || code == TYPE_CODE_DECFLOAT)
708 && (len == 2 || len == 4 || len == 8))
709 /* FIXME: __m64 . */
710 theclass[0] = AMD64_SSE;
712 /* Arguments of types __float128, _Decimal128 and __m128 are split into
713 two halves. The least significant ones belong to class SSE, the most
714 significant one to class SSEUP. */
715 else if (code == TYPE_CODE_DECFLOAT && len == 16)
716 /* FIXME: __float128, __m128. */
717 theclass[0] = AMD64_SSE, theclass[1] = AMD64_SSEUP;
719 /* The 64-bit mantissa of arguments of type long double belongs to
720 class X87, the 16-bit exponent plus 6 bytes of padding belongs to
721 class X87UP. */
722 else if (code == TYPE_CODE_FLT && len == 16)
723 /* Class X87 and X87UP. */
724 theclass[0] = AMD64_X87, theclass[1] = AMD64_X87UP;
726 /* Arguments of complex T - where T is one of the types _Float16, float or
727 double - get treated as if they are implemented as:
729 struct complexT {
730 T real;
731 T imag;
735 else if (code == TYPE_CODE_COMPLEX && (len == 8 || len == 4))
736 theclass[0] = AMD64_SSE;
737 else if (code == TYPE_CODE_COMPLEX && len == 16)
738 theclass[0] = theclass[1] = AMD64_SSE;
740 /* A variable of type complex long double is classified as type
741 COMPLEX_X87. */
742 else if (code == TYPE_CODE_COMPLEX && len == 32)
743 theclass[0] = AMD64_COMPLEX_X87;
745 /* Aggregates. */
746 else if (code == TYPE_CODE_ARRAY || code == TYPE_CODE_STRUCT
747 || code == TYPE_CODE_UNION)
748 amd64_classify_aggregate (type, theclass);
751 static enum return_value_convention
752 amd64_return_value (struct gdbarch *gdbarch, struct value *function,
753 struct type *type, struct regcache *regcache,
754 struct value **read_value, const gdb_byte *writebuf)
756 enum amd64_reg_class theclass[2];
757 int len = type->length ();
758 static int integer_regnum[] = { AMD64_RAX_REGNUM, AMD64_RDX_REGNUM };
759 static int sse_regnum[] = { AMD64_XMM0_REGNUM, AMD64_XMM1_REGNUM };
760 int integer_reg = 0;
761 int sse_reg = 0;
762 int i;
764 gdb_assert (!(read_value && writebuf));
766 /* 1. Classify the return type with the classification algorithm. */
767 amd64_classify (type, theclass);
769 /* 2. If the type has class MEMORY, then the caller provides space
770 for the return value and passes the address of this storage in
771 %rdi as if it were the first argument to the function. In effect,
772 this address becomes a hidden first argument.
774 On return %rax will contain the address that has been passed in
775 by the caller in %rdi. */
776 if (theclass[0] == AMD64_MEMORY)
778 /* As indicated by the comment above, the ABI guarantees that we
779 can always find the return value just after the function has
780 returned. */
782 if (read_value != nullptr)
784 ULONGEST addr;
786 regcache_raw_read_unsigned (regcache, AMD64_RAX_REGNUM, &addr);
787 *read_value = value_at_non_lval (type, addr);
790 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
793 gdb_byte *readbuf = nullptr;
794 if (read_value != nullptr)
796 *read_value = value::allocate (type);
797 readbuf = (*read_value)->contents_raw ().data ();
800 /* 8. If the class is COMPLEX_X87, the real part of the value is
801 returned in %st0 and the imaginary part in %st1. */
802 if (theclass[0] == AMD64_COMPLEX_X87)
804 if (readbuf)
806 regcache->raw_read (AMD64_ST0_REGNUM, readbuf);
807 regcache->raw_read (AMD64_ST1_REGNUM, readbuf + 16);
810 if (writebuf)
812 i387_return_value (gdbarch, regcache);
813 regcache->raw_write (AMD64_ST0_REGNUM, writebuf);
814 regcache->raw_write (AMD64_ST1_REGNUM, writebuf + 16);
816 /* Fix up the tag word such that both %st(0) and %st(1) are
817 marked as valid. */
818 regcache_raw_write_unsigned (regcache, AMD64_FTAG_REGNUM, 0xfff);
821 return RETURN_VALUE_REGISTER_CONVENTION;
824 gdb_assert (theclass[1] != AMD64_MEMORY);
825 gdb_assert (len <= 16);
827 for (i = 0; len > 0; i++, len -= 8)
829 int regnum = -1;
830 int offset = 0;
832 switch (theclass[i])
834 case AMD64_INTEGER:
835 /* 3. If the class is INTEGER, the next available register
836 of the sequence %rax, %rdx is used. */
837 regnum = integer_regnum[integer_reg++];
838 break;
840 case AMD64_SSE:
841 /* 4. If the class is SSE, the next available SSE register
842 of the sequence %xmm0, %xmm1 is used. */
843 regnum = sse_regnum[sse_reg++];
844 break;
846 case AMD64_SSEUP:
847 /* 5. If the class is SSEUP, the eightbyte is passed in the
848 upper half of the last used SSE register. */
849 gdb_assert (sse_reg > 0);
850 regnum = sse_regnum[sse_reg - 1];
851 offset = 8;
852 break;
854 case AMD64_X87:
855 /* 6. If the class is X87, the value is returned on the X87
856 stack in %st0 as 80-bit x87 number. */
857 regnum = AMD64_ST0_REGNUM;
858 if (writebuf)
859 i387_return_value (gdbarch, regcache);
860 break;
862 case AMD64_X87UP:
863 /* 7. If the class is X87UP, the value is returned together
864 with the previous X87 value in %st0. */
865 gdb_assert (i > 0 && theclass[0] == AMD64_X87);
866 regnum = AMD64_ST0_REGNUM;
867 offset = 8;
868 len = 2;
869 break;
871 case AMD64_NO_CLASS:
872 continue;
874 default:
875 gdb_assert (!"Unexpected register class.");
878 gdb_assert (regnum != -1);
880 if (readbuf)
881 regcache->raw_read_part (regnum, offset, std::min (len, 8),
882 readbuf + i * 8);
883 if (writebuf)
884 regcache->raw_write_part (regnum, offset, std::min (len, 8),
885 writebuf + i * 8);
888 return RETURN_VALUE_REGISTER_CONVENTION;
892 static CORE_ADDR
893 amd64_push_arguments (struct regcache *regcache, int nargs, struct value **args,
894 CORE_ADDR sp, function_call_return_method return_method)
896 static int integer_regnum[] =
898 AMD64_RDI_REGNUM, /* %rdi */
899 AMD64_RSI_REGNUM, /* %rsi */
900 AMD64_RDX_REGNUM, /* %rdx */
901 AMD64_RCX_REGNUM, /* %rcx */
902 AMD64_R8_REGNUM, /* %r8 */
903 AMD64_R9_REGNUM /* %r9 */
905 static int sse_regnum[] =
907 /* %xmm0 ... %xmm7 */
908 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
909 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
910 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
911 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
913 struct value **stack_args = XALLOCAVEC (struct value *, nargs);
914 int num_stack_args = 0;
915 int num_elements = 0;
916 int element = 0;
917 int integer_reg = 0;
918 int sse_reg = 0;
919 int i;
921 /* Reserve a register for the "hidden" argument. */
922 if (return_method == return_method_struct)
923 integer_reg++;
925 for (i = 0; i < nargs; i++)
927 struct type *type = args[i]->type ();
928 int len = type->length ();
929 enum amd64_reg_class theclass[2];
930 int needed_integer_regs = 0;
931 int needed_sse_regs = 0;
932 int j;
934 /* Classify argument. */
935 amd64_classify (type, theclass);
937 /* Calculate the number of integer and SSE registers needed for
938 this argument. */
939 for (j = 0; j < 2; j++)
941 if (theclass[j] == AMD64_INTEGER)
942 needed_integer_regs++;
943 else if (theclass[j] == AMD64_SSE)
944 needed_sse_regs++;
947 /* Check whether enough registers are available, and if the
948 argument should be passed in registers at all. */
949 if (integer_reg + needed_integer_regs > ARRAY_SIZE (integer_regnum)
950 || sse_reg + needed_sse_regs > ARRAY_SIZE (sse_regnum)
951 || (needed_integer_regs == 0 && needed_sse_regs == 0))
953 /* The argument will be passed on the stack. */
954 num_elements += ((len + 7) / 8);
955 stack_args[num_stack_args++] = args[i];
957 else
959 /* The argument will be passed in registers. */
960 const gdb_byte *valbuf = args[i]->contents ().data ();
961 gdb_byte buf[8];
963 gdb_assert (len <= 16);
965 for (j = 0; len > 0; j++, len -= 8)
967 int regnum = -1;
968 int offset = 0;
970 switch (theclass[j])
972 case AMD64_INTEGER:
973 regnum = integer_regnum[integer_reg++];
974 break;
976 case AMD64_SSE:
977 regnum = sse_regnum[sse_reg++];
978 break;
980 case AMD64_SSEUP:
981 gdb_assert (sse_reg > 0);
982 regnum = sse_regnum[sse_reg - 1];
983 offset = 8;
984 break;
986 case AMD64_NO_CLASS:
987 continue;
989 default:
990 gdb_assert (!"Unexpected register class.");
993 gdb_assert (regnum != -1);
994 memset (buf, 0, sizeof buf);
995 memcpy (buf, valbuf + j * 8, std::min (len, 8));
996 regcache->raw_write_part (regnum, offset, 8, buf);
1001 /* Allocate space for the arguments on the stack. */
1002 sp -= num_elements * 8;
1004 /* The psABI says that "The end of the input argument area shall be
1005 aligned on a 16 byte boundary." */
1006 sp &= ~0xf;
1008 /* Write out the arguments to the stack. */
1009 for (i = 0; i < num_stack_args; i++)
1011 struct type *type = stack_args[i]->type ();
1012 const gdb_byte *valbuf = stack_args[i]->contents ().data ();
1013 int len = type->length ();
1015 write_memory (sp + element * 8, valbuf, len);
1016 element += ((len + 7) / 8);
1019 /* The psABI says that "For calls that may call functions that use
1020 varargs or stdargs (prototype-less calls or calls to functions
1021 containing ellipsis (...) in the declaration) %al is used as
1022 hidden argument to specify the number of SSE registers used. */
1023 regcache_raw_write_unsigned (regcache, AMD64_RAX_REGNUM, sse_reg);
1024 return sp;
1027 static CORE_ADDR
1028 amd64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1029 struct regcache *regcache, CORE_ADDR bp_addr,
1030 int nargs, struct value **args, CORE_ADDR sp,
1031 function_call_return_method return_method,
1032 CORE_ADDR struct_addr)
1034 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1035 gdb_byte buf[8];
1037 /* Pass arguments. */
1038 sp = amd64_push_arguments (regcache, nargs, args, sp, return_method);
1040 /* Pass "hidden" argument". */
1041 if (return_method == return_method_struct)
1043 store_unsigned_integer (buf, 8, byte_order, struct_addr);
1044 regcache->cooked_write (AMD64_RDI_REGNUM, buf);
1047 /* Store return address. */
1048 sp -= 8;
1049 store_unsigned_integer (buf, 8, byte_order, bp_addr);
1050 write_memory (sp, buf, 8);
1052 /* Finally, update the stack pointer... */
1053 store_unsigned_integer (buf, 8, byte_order, sp);
1054 regcache->cooked_write (AMD64_RSP_REGNUM, buf);
1056 /* ...and fake a frame pointer. */
1057 regcache->cooked_write (AMD64_RBP_REGNUM, buf);
1059 return sp + 16;
1062 /* Displaced instruction handling. */
1064 /* A partially decoded instruction.
1065 This contains enough details for displaced stepping purposes. */
1067 struct amd64_insn
1069 /* The number of opcode bytes. */
1070 int opcode_len;
1071 /* The offset of the REX/VEX instruction encoding prefix or -1 if
1072 not present. */
1073 int enc_prefix_offset;
1074 /* The offset to the first opcode byte. */
1075 int opcode_offset;
1076 /* The offset to the modrm byte or -1 if not present. */
1077 int modrm_offset;
1079 /* The raw instruction. */
1080 gdb_byte *raw_insn;
1083 struct amd64_displaced_step_copy_insn_closure
1084 : public displaced_step_copy_insn_closure
1086 amd64_displaced_step_copy_insn_closure (int insn_buf_len)
1087 : insn_buf (insn_buf_len, 0)
1090 /* For rip-relative insns, saved copy of the reg we use instead of %rip. */
1091 int tmp_used = 0;
1092 int tmp_regno;
1093 ULONGEST tmp_save;
1095 /* Details of the instruction. */
1096 struct amd64_insn insn_details;
1098 /* The possibly modified insn. */
1099 gdb::byte_vector insn_buf;
1102 /* WARNING: Keep onebyte_has_modrm, twobyte_has_modrm in sync with
1103 ../opcodes/i386-dis.c (until libopcodes exports them, or an alternative,
1104 at which point delete these in favor of libopcodes' versions). */
1106 static const unsigned char onebyte_has_modrm[256] = {
1107 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1108 /* ------------------------------- */
1109 /* 00 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 00 */
1110 /* 10 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 10 */
1111 /* 20 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 20 */
1112 /* 30 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 30 */
1113 /* 40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 40 */
1114 /* 50 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 50 */
1115 /* 60 */ 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0, /* 60 */
1116 /* 70 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 70 */
1117 /* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 80 */
1118 /* 90 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 90 */
1119 /* a0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* a0 */
1120 /* b0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* b0 */
1121 /* c0 */ 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0, /* c0 */
1122 /* d0 */ 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1, /* d0 */
1123 /* e0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* e0 */
1124 /* f0 */ 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1 /* f0 */
1125 /* ------------------------------- */
1126 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1129 static const unsigned char twobyte_has_modrm[256] = {
1130 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1131 /* ------------------------------- */
1132 /* 00 */ 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1, /* 0f */
1133 /* 10 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 1f */
1134 /* 20 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 2f */
1135 /* 30 */ 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0, /* 3f */
1136 /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 4f */
1137 /* 50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 5f */
1138 /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 6f */
1139 /* 70 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 7f */
1140 /* 80 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 8f */
1141 /* 90 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 9f */
1142 /* a0 */ 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1, /* af */
1143 /* b0 */ 1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1, /* bf */
1144 /* c0 */ 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, /* cf */
1145 /* d0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* df */
1146 /* e0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ef */
1147 /* f0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0 /* ff */
1148 /* ------------------------------- */
1149 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1152 static int amd64_syscall_p (const struct amd64_insn *insn, int *lengthp);
1154 static int
1155 rex_prefix_p (gdb_byte pfx)
1157 return REX_PREFIX_P (pfx);
1160 /* True if PFX is the start of the 2-byte VEX prefix. */
1162 static bool
1163 vex2_prefix_p (gdb_byte pfx)
1165 return pfx == 0xc5;
1168 /* True if PFX is the start of the 3-byte VEX prefix. */
1170 static bool
1171 vex3_prefix_p (gdb_byte pfx)
1173 return pfx == 0xc4;
1176 /* Skip the legacy instruction prefixes in INSN.
1177 We assume INSN is properly sentineled so we don't have to worry
1178 about falling off the end of the buffer. */
1180 static gdb_byte *
1181 amd64_skip_prefixes (gdb_byte *insn)
1183 while (1)
1185 switch (*insn)
1187 case DATA_PREFIX_OPCODE:
1188 case ADDR_PREFIX_OPCODE:
1189 case CS_PREFIX_OPCODE:
1190 case DS_PREFIX_OPCODE:
1191 case ES_PREFIX_OPCODE:
1192 case FS_PREFIX_OPCODE:
1193 case GS_PREFIX_OPCODE:
1194 case SS_PREFIX_OPCODE:
1195 case LOCK_PREFIX_OPCODE:
1196 case REPE_PREFIX_OPCODE:
1197 case REPNE_PREFIX_OPCODE:
1198 ++insn;
1199 continue;
1200 default:
1201 break;
1203 break;
1206 return insn;
1209 /* Return an integer register (other than RSP) that is unused as an input
1210 operand in INSN.
1211 In order to not require adding a rex prefix if the insn doesn't already
1212 have one, the result is restricted to RAX ... RDI, sans RSP.
1213 The register numbering of the result follows architecture ordering,
1214 e.g. RDI = 7. */
1216 static int
1217 amd64_get_unused_input_int_reg (const struct amd64_insn *details)
1219 /* 1 bit for each reg */
1220 int used_regs_mask = 0;
1222 /* There can be at most 3 int regs used as inputs in an insn, and we have
1223 7 to choose from (RAX ... RDI, sans RSP).
1224 This allows us to take a conservative approach and keep things simple.
1225 E.g. By avoiding RAX, we don't have to specifically watch for opcodes
1226 that implicitly specify RAX. */
1228 /* Avoid RAX. */
1229 used_regs_mask |= 1 << EAX_REG_NUM;
1230 /* Similarly avoid RDX, implicit operand in divides. */
1231 used_regs_mask |= 1 << EDX_REG_NUM;
1232 /* Avoid RSP. */
1233 used_regs_mask |= 1 << ESP_REG_NUM;
1235 /* If the opcode is one byte long and there's no ModRM byte,
1236 assume the opcode specifies a register. */
1237 if (details->opcode_len == 1 && details->modrm_offset == -1)
1238 used_regs_mask |= 1 << (details->raw_insn[details->opcode_offset] & 7);
1240 /* Mark used regs in the modrm/sib bytes. */
1241 if (details->modrm_offset != -1)
1243 int modrm = details->raw_insn[details->modrm_offset];
1244 int mod = MODRM_MOD_FIELD (modrm);
1245 int reg = MODRM_REG_FIELD (modrm);
1246 int rm = MODRM_RM_FIELD (modrm);
1247 int have_sib = mod != 3 && rm == 4;
1249 /* Assume the reg field of the modrm byte specifies a register. */
1250 used_regs_mask |= 1 << reg;
1252 if (have_sib)
1254 int base = SIB_BASE_FIELD (details->raw_insn[details->modrm_offset + 1]);
1255 int idx = SIB_INDEX_FIELD (details->raw_insn[details->modrm_offset + 1]);
1256 used_regs_mask |= 1 << base;
1257 used_regs_mask |= 1 << idx;
1259 else
1261 used_regs_mask |= 1 << rm;
1265 gdb_assert (used_regs_mask < 256);
1266 gdb_assert (used_regs_mask != 255);
1268 /* Finally, find a free reg. */
1270 int i;
1272 for (i = 0; i < 8; ++i)
1274 if (! (used_regs_mask & (1 << i)))
1275 return i;
1278 /* We shouldn't get here. */
1279 internal_error (_("unable to find free reg"));
1283 /* Extract the details of INSN that we need. */
1285 static void
1286 amd64_get_insn_details (gdb_byte *insn, struct amd64_insn *details)
1288 gdb_byte *start = insn;
1289 int need_modrm;
1291 details->raw_insn = insn;
1293 details->opcode_len = -1;
1294 details->enc_prefix_offset = -1;
1295 details->opcode_offset = -1;
1296 details->modrm_offset = -1;
1298 /* Skip legacy instruction prefixes. */
1299 insn = amd64_skip_prefixes (insn);
1301 /* Skip REX/VEX instruction encoding prefixes. */
1302 if (rex_prefix_p (*insn))
1304 details->enc_prefix_offset = insn - start;
1305 ++insn;
1307 else if (vex2_prefix_p (*insn))
1309 /* Don't record the offset in this case because this prefix has
1310 no REX.B equivalent. */
1311 insn += 2;
1313 else if (vex3_prefix_p (*insn))
1315 details->enc_prefix_offset = insn - start;
1316 insn += 3;
1319 details->opcode_offset = insn - start;
1321 if (*insn == TWO_BYTE_OPCODE_ESCAPE)
1323 /* Two or three-byte opcode. */
1324 ++insn;
1325 need_modrm = twobyte_has_modrm[*insn];
1327 /* Check for three-byte opcode. */
1328 switch (*insn)
1330 case 0x24:
1331 case 0x25:
1332 case 0x38:
1333 case 0x3a:
1334 case 0x7a:
1335 case 0x7b:
1336 ++insn;
1337 details->opcode_len = 3;
1338 break;
1339 default:
1340 details->opcode_len = 2;
1341 break;
1344 else
1346 /* One-byte opcode. */
1347 need_modrm = onebyte_has_modrm[*insn];
1348 details->opcode_len = 1;
1351 if (need_modrm)
1353 ++insn;
1354 details->modrm_offset = insn - start;
1358 /* Update %rip-relative addressing in INSN.
1360 %rip-relative addressing only uses a 32-bit displacement.
1361 32 bits is not enough to be guaranteed to cover the distance between where
1362 the real instruction is and where its copy is.
1363 Convert the insn to use base+disp addressing.
1364 We set base = pc + insn_length so we can leave disp unchanged. */
1366 static void
1367 fixup_riprel (struct gdbarch *gdbarch,
1368 amd64_displaced_step_copy_insn_closure *dsc,
1369 CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
1371 const struct amd64_insn *insn_details = &dsc->insn_details;
1372 int modrm_offset = insn_details->modrm_offset;
1373 CORE_ADDR rip_base;
1374 int insn_length;
1375 int arch_tmp_regno, tmp_regno;
1376 ULONGEST orig_value;
1378 /* Compute the rip-relative address. */
1379 insn_length = gdb_buffered_insn_length (gdbarch, dsc->insn_buf.data (),
1380 dsc->insn_buf.size (), from);
1381 rip_base = from + insn_length;
1383 /* We need a register to hold the address.
1384 Pick one not used in the insn.
1385 NOTE: arch_tmp_regno uses architecture ordering, e.g. RDI = 7. */
1386 arch_tmp_regno = amd64_get_unused_input_int_reg (insn_details);
1387 tmp_regno = amd64_arch_reg_to_regnum (arch_tmp_regno);
1389 /* Position of the not-B bit in the 3-byte VEX prefix (in byte 1). */
1390 static constexpr gdb_byte VEX3_NOT_B = 0x20;
1392 /* REX.B should be unset (VEX.!B set) as we were using rip-relative
1393 addressing, but ensure it's unset (set for VEX) anyway, tmp_regno
1394 is not r8-r15. */
1395 if (insn_details->enc_prefix_offset != -1)
1397 gdb_byte *pfx = &dsc->insn_buf[insn_details->enc_prefix_offset];
1398 if (rex_prefix_p (pfx[0]))
1399 pfx[0] &= ~REX_B;
1400 else if (vex3_prefix_p (pfx[0]))
1401 pfx[1] |= VEX3_NOT_B;
1402 else
1403 gdb_assert_not_reached ("unhandled prefix");
1406 regcache_cooked_read_unsigned (regs, tmp_regno, &orig_value);
1407 dsc->tmp_regno = tmp_regno;
1408 dsc->tmp_save = orig_value;
1409 dsc->tmp_used = 1;
1411 /* Convert the ModRM field to be base+disp. */
1412 dsc->insn_buf[modrm_offset] &= ~0xc7;
1413 dsc->insn_buf[modrm_offset] |= 0x80 + arch_tmp_regno;
1415 regcache_cooked_write_unsigned (regs, tmp_regno, rip_base);
1417 displaced_debug_printf ("%%rip-relative addressing used.");
1418 displaced_debug_printf ("using temp reg %d, old value %s, new value %s",
1419 dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save),
1420 paddress (gdbarch, rip_base));
1423 static void
1424 fixup_displaced_copy (struct gdbarch *gdbarch,
1425 amd64_displaced_step_copy_insn_closure *dsc,
1426 CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
1428 const struct amd64_insn *details = &dsc->insn_details;
1430 if (details->modrm_offset != -1)
1432 gdb_byte modrm = details->raw_insn[details->modrm_offset];
1434 if ((modrm & 0xc7) == 0x05)
1436 /* The insn uses rip-relative addressing.
1437 Deal with it. */
1438 fixup_riprel (gdbarch, dsc, from, to, regs);
1443 displaced_step_copy_insn_closure_up
1444 amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
1445 CORE_ADDR from, CORE_ADDR to,
1446 struct regcache *regs)
1448 int len = gdbarch_max_insn_length (gdbarch);
1449 /* Extra space for sentinels so fixup_{riprel,displaced_copy} don't have to
1450 continually watch for running off the end of the buffer. */
1451 int fixup_sentinel_space = len;
1452 std::unique_ptr<amd64_displaced_step_copy_insn_closure> dsc
1453 (new amd64_displaced_step_copy_insn_closure (len + fixup_sentinel_space));
1454 gdb_byte *buf = &dsc->insn_buf[0];
1455 struct amd64_insn *details = &dsc->insn_details;
1457 read_memory (from, buf, len);
1459 /* Set up the sentinel space so we don't have to worry about running
1460 off the end of the buffer. An excessive number of leading prefixes
1461 could otherwise cause this. */
1462 memset (buf + len, 0, fixup_sentinel_space);
1464 amd64_get_insn_details (buf, details);
1466 /* GDB may get control back after the insn after the syscall.
1467 Presumably this is a kernel bug.
1468 If this is a syscall, make sure there's a nop afterwards. */
1470 int syscall_length;
1472 if (amd64_syscall_p (details, &syscall_length))
1473 buf[details->opcode_offset + syscall_length] = NOP_OPCODE;
1476 /* Modify the insn to cope with the address where it will be executed from.
1477 In particular, handle any rip-relative addressing. */
1478 fixup_displaced_copy (gdbarch, dsc.get (), from, to, regs);
1480 write_memory (to, buf, len);
1482 displaced_debug_printf ("copy %s->%s: %s",
1483 paddress (gdbarch, from), paddress (gdbarch, to),
1484 bytes_to_string (buf, len).c_str ());
1486 /* This is a work around for a problem with g++ 4.8. */
1487 return displaced_step_copy_insn_closure_up (dsc.release ());
1490 static int
1491 amd64_absolute_jmp_p (const struct amd64_insn *details)
1493 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1495 if (insn[0] == 0xff)
1497 /* jump near, absolute indirect (/4) */
1498 if ((insn[1] & 0x38) == 0x20)
1499 return 1;
1501 /* jump far, absolute indirect (/5) */
1502 if ((insn[1] & 0x38) == 0x28)
1503 return 1;
1506 return 0;
1509 /* Return non-zero if the instruction DETAILS is a jump, zero otherwise. */
1511 static int
1512 amd64_jmp_p (const struct amd64_insn *details)
1514 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1516 /* jump short, relative. */
1517 if (insn[0] == 0xeb)
1518 return 1;
1520 /* jump near, relative. */
1521 if (insn[0] == 0xe9)
1522 return 1;
1524 return amd64_absolute_jmp_p (details);
1527 static int
1528 amd64_absolute_call_p (const struct amd64_insn *details)
1530 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1532 if (insn[0] == 0xff)
1534 /* Call near, absolute indirect (/2) */
1535 if ((insn[1] & 0x38) == 0x10)
1536 return 1;
1538 /* Call far, absolute indirect (/3) */
1539 if ((insn[1] & 0x38) == 0x18)
1540 return 1;
1543 return 0;
1546 static int
1547 amd64_ret_p (const struct amd64_insn *details)
1549 /* NOTE: gcc can emit "repz ; ret". */
1550 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1552 switch (insn[0])
1554 case 0xc2: /* ret near, pop N bytes */
1555 case 0xc3: /* ret near */
1556 case 0xca: /* ret far, pop N bytes */
1557 case 0xcb: /* ret far */
1558 case 0xcf: /* iret */
1559 return 1;
1561 default:
1562 return 0;
1566 static int
1567 amd64_call_p (const struct amd64_insn *details)
1569 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1571 if (amd64_absolute_call_p (details))
1572 return 1;
1574 /* call near, relative */
1575 if (insn[0] == 0xe8)
1576 return 1;
1578 return 0;
1581 /* Return non-zero if INSN is a system call, and set *LENGTHP to its
1582 length in bytes. Otherwise, return zero. */
1584 static int
1585 amd64_syscall_p (const struct amd64_insn *details, int *lengthp)
1587 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1589 if (insn[0] == 0x0f && insn[1] == 0x05)
1591 *lengthp = 2;
1592 return 1;
1595 return 0;
1598 /* Classify the instruction at ADDR using PRED.
1599 Throw an error if the memory can't be read. */
1601 static int
1602 amd64_classify_insn_at (struct gdbarch *gdbarch, CORE_ADDR addr,
1603 int (*pred) (const struct amd64_insn *))
1605 struct amd64_insn details;
1607 gdb::byte_vector buf (gdbarch_max_insn_length (gdbarch));
1609 read_code (addr, buf.data (), buf.size ());
1610 amd64_get_insn_details (buf.data (), &details);
1612 int classification = pred (&details);
1614 return classification;
1617 /* The gdbarch insn_is_call method. */
1619 static int
1620 amd64_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
1622 return amd64_classify_insn_at (gdbarch, addr, amd64_call_p);
1625 /* The gdbarch insn_is_ret method. */
1627 static int
1628 amd64_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
1630 return amd64_classify_insn_at (gdbarch, addr, amd64_ret_p);
1633 /* The gdbarch insn_is_jump method. */
1635 static int
1636 amd64_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
1638 return amd64_classify_insn_at (gdbarch, addr, amd64_jmp_p);
1641 /* Fix up the state of registers and memory after having single-stepped
1642 a displaced instruction. */
1644 void
1645 amd64_displaced_step_fixup (struct gdbarch *gdbarch,
1646 struct displaced_step_copy_insn_closure *dsc_,
1647 CORE_ADDR from, CORE_ADDR to,
1648 struct regcache *regs, bool completed_p)
1650 amd64_displaced_step_copy_insn_closure *dsc
1651 = (amd64_displaced_step_copy_insn_closure *) dsc_;
1652 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1653 /* The offset we applied to the instruction's address. */
1654 ULONGEST insn_offset = to - from;
1655 gdb_byte *insn = dsc->insn_buf.data ();
1656 const struct amd64_insn *insn_details = &dsc->insn_details;
1658 displaced_debug_printf ("fixup (%s, %s), insn = 0x%02x 0x%02x ...",
1659 paddress (gdbarch, from), paddress (gdbarch, to),
1660 insn[0], insn[1]);
1662 /* If we used a tmp reg, restore it. */
1664 if (dsc->tmp_used)
1666 displaced_debug_printf ("restoring reg %d to %s",
1667 dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save));
1668 regcache_cooked_write_unsigned (regs, dsc->tmp_regno, dsc->tmp_save);
1671 /* The list of issues to contend with here is taken from
1672 resume_execution in arch/x86/kernel/kprobes.c, Linux 2.6.28.
1673 Yay for Free Software! */
1675 /* Relocate the %rip back to the program's instruction stream,
1676 if necessary. */
1678 /* Except in the case of absolute or indirect jump or call
1679 instructions, or a return instruction, the new rip is relative to
1680 the displaced instruction; make it relative to the original insn.
1681 Well, signal handler returns don't need relocation either, but we use the
1682 value of %rip to recognize those; see below. */
1683 if (!completed_p
1684 || (!amd64_absolute_jmp_p (insn_details)
1685 && !amd64_absolute_call_p (insn_details)
1686 && !amd64_ret_p (insn_details)))
1688 int insn_len;
1690 CORE_ADDR pc = regcache_read_pc (regs);
1692 /* A signal trampoline system call changes the %rip, resuming
1693 execution of the main program after the signal handler has
1694 returned. That makes them like 'return' instructions; we
1695 shouldn't relocate %rip.
1697 But most system calls don't, and we do need to relocate %rip.
1699 Our heuristic for distinguishing these cases: if stepping
1700 over the system call instruction left control directly after
1701 the instruction, the we relocate --- control almost certainly
1702 doesn't belong in the displaced copy. Otherwise, we assume
1703 the instruction has put control where it belongs, and leave
1704 it unrelocated. Goodness help us if there are PC-relative
1705 system calls. */
1706 if (amd64_syscall_p (insn_details, &insn_len)
1707 /* GDB can get control back after the insn after the syscall.
1708 Presumably this is a kernel bug. Fixup ensures it's a nop, we
1709 add one to the length for it. */
1710 && (pc < to || pc > (to + insn_len + 1)))
1711 displaced_debug_printf ("syscall changed %%rip; not relocating");
1712 else
1714 CORE_ADDR rip = pc - insn_offset;
1716 /* If we just stepped over a breakpoint insn, we don't backup
1717 the pc on purpose; this is to match behavior without
1718 stepping. */
1720 regcache_write_pc (regs, rip);
1722 displaced_debug_printf ("relocated %%rip from %s to %s",
1723 paddress (gdbarch, pc),
1724 paddress (gdbarch, rip));
1728 /* If the instruction was PUSHFL, then the TF bit will be set in the
1729 pushed value, and should be cleared. We'll leave this for later,
1730 since GDB already messes up the TF flag when stepping over a
1731 pushfl. */
1733 /* If the instruction was a call, the return address now atop the
1734 stack is the address following the copied instruction. We need
1735 to make it the address following the original instruction. */
1736 if (completed_p && amd64_call_p (insn_details))
1738 ULONGEST rsp;
1739 ULONGEST retaddr;
1740 const ULONGEST retaddr_len = 8;
1742 regcache_cooked_read_unsigned (regs, AMD64_RSP_REGNUM, &rsp);
1743 retaddr = read_memory_unsigned_integer (rsp, retaddr_len, byte_order);
1744 retaddr = (retaddr - insn_offset) & 0xffffffffffffffffULL;
1745 write_memory_unsigned_integer (rsp, retaddr_len, byte_order, retaddr);
1747 displaced_debug_printf ("relocated return addr at %s to %s",
1748 paddress (gdbarch, rsp),
1749 paddress (gdbarch, retaddr));
1753 /* If the instruction INSN uses RIP-relative addressing, return the
1754 offset into the raw INSN where the displacement to be adjusted is
1755 found. Returns 0 if the instruction doesn't use RIP-relative
1756 addressing. */
1758 static int
1759 rip_relative_offset (struct amd64_insn *insn)
1761 if (insn->modrm_offset != -1)
1763 gdb_byte modrm = insn->raw_insn[insn->modrm_offset];
1765 if ((modrm & 0xc7) == 0x05)
1767 /* The displacement is found right after the ModRM byte. */
1768 return insn->modrm_offset + 1;
1772 return 0;
1775 static void
1776 append_insns (CORE_ADDR *to, ULONGEST len, const gdb_byte *buf)
1778 target_write_memory (*to, buf, len);
1779 *to += len;
1782 static void
1783 amd64_relocate_instruction (struct gdbarch *gdbarch,
1784 CORE_ADDR *to, CORE_ADDR oldloc)
1786 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1787 int len = gdbarch_max_insn_length (gdbarch);
1788 /* Extra space for sentinels. */
1789 int fixup_sentinel_space = len;
1790 gdb::byte_vector buf (len + fixup_sentinel_space);
1791 struct amd64_insn insn_details;
1792 int offset = 0;
1793 LONGEST rel32, newrel;
1794 gdb_byte *insn;
1795 int insn_length;
1797 read_memory (oldloc, buf.data (), len);
1799 /* Set up the sentinel space so we don't have to worry about running
1800 off the end of the buffer. An excessive number of leading prefixes
1801 could otherwise cause this. */
1802 memset (buf.data () + len, 0, fixup_sentinel_space);
1804 insn = buf.data ();
1805 amd64_get_insn_details (insn, &insn_details);
1807 insn_length = gdb_buffered_insn_length (gdbarch, insn, len, oldloc);
1809 /* Skip legacy instruction prefixes. */
1810 insn = amd64_skip_prefixes (insn);
1812 /* Adjust calls with 32-bit relative addresses as push/jump, with
1813 the address pushed being the location where the original call in
1814 the user program would return to. */
1815 if (insn[0] == 0xe8)
1817 gdb_byte push_buf[32];
1818 CORE_ADDR ret_addr;
1819 int i = 0;
1821 /* Where "ret" in the original code will return to. */
1822 ret_addr = oldloc + insn_length;
1824 /* If pushing an address higher than or equal to 0x80000000,
1825 avoid 'pushq', as that sign extends its 32-bit operand, which
1826 would be incorrect. */
1827 if (ret_addr <= 0x7fffffff)
1829 push_buf[0] = 0x68; /* pushq $... */
1830 store_unsigned_integer (&push_buf[1], 4, byte_order, ret_addr);
1831 i = 5;
1833 else
1835 push_buf[i++] = 0x48; /* sub $0x8,%rsp */
1836 push_buf[i++] = 0x83;
1837 push_buf[i++] = 0xec;
1838 push_buf[i++] = 0x08;
1840 push_buf[i++] = 0xc7; /* movl $imm,(%rsp) */
1841 push_buf[i++] = 0x04;
1842 push_buf[i++] = 0x24;
1843 store_unsigned_integer (&push_buf[i], 4, byte_order,
1844 ret_addr & 0xffffffff);
1845 i += 4;
1847 push_buf[i++] = 0xc7; /* movl $imm,4(%rsp) */
1848 push_buf[i++] = 0x44;
1849 push_buf[i++] = 0x24;
1850 push_buf[i++] = 0x04;
1851 store_unsigned_integer (&push_buf[i], 4, byte_order,
1852 ret_addr >> 32);
1853 i += 4;
1855 gdb_assert (i <= sizeof (push_buf));
1856 /* Push the push. */
1857 append_insns (to, i, push_buf);
1859 /* Convert the relative call to a relative jump. */
1860 insn[0] = 0xe9;
1862 /* Adjust the destination offset. */
1863 rel32 = extract_signed_integer (insn + 1, 4, byte_order);
1864 newrel = (oldloc - *to) + rel32;
1865 store_signed_integer (insn + 1, 4, byte_order, newrel);
1867 displaced_debug_printf ("adjusted insn rel32=%s at %s to rel32=%s at %s",
1868 hex_string (rel32), paddress (gdbarch, oldloc),
1869 hex_string (newrel), paddress (gdbarch, *to));
1871 /* Write the adjusted jump into its displaced location. */
1872 append_insns (to, 5, insn);
1873 return;
1876 offset = rip_relative_offset (&insn_details);
1877 if (!offset)
1879 /* Adjust jumps with 32-bit relative addresses. Calls are
1880 already handled above. */
1881 if (insn[0] == 0xe9)
1882 offset = 1;
1883 /* Adjust conditional jumps. */
1884 else if (insn[0] == 0x0f && (insn[1] & 0xf0) == 0x80)
1885 offset = 2;
1888 if (offset)
1890 rel32 = extract_signed_integer (insn + offset, 4, byte_order);
1891 newrel = (oldloc - *to) + rel32;
1892 store_signed_integer (insn + offset, 4, byte_order, newrel);
1893 displaced_debug_printf ("adjusted insn rel32=%s at %s to rel32=%s at %s",
1894 hex_string (rel32), paddress (gdbarch, oldloc),
1895 hex_string (newrel), paddress (gdbarch, *to));
1898 /* Write the adjusted instruction into its displaced location. */
1899 append_insns (to, insn_length, buf.data ());
1903 /* The maximum number of saved registers. This should include %rip. */
1904 #define AMD64_NUM_SAVED_REGS AMD64_NUM_GREGS
1906 struct amd64_frame_cache
1908 /* Base address. */
1909 CORE_ADDR base;
1910 int base_p;
1911 CORE_ADDR sp_offset;
1912 CORE_ADDR pc;
1914 /* Saved registers. */
1915 CORE_ADDR saved_regs[AMD64_NUM_SAVED_REGS];
1916 CORE_ADDR saved_sp;
1917 int saved_sp_reg;
1919 /* Do we have a frame? */
1920 int frameless_p;
1923 /* Initialize a frame cache. */
1925 static void
1926 amd64_init_frame_cache (struct amd64_frame_cache *cache)
1928 int i;
1930 /* Base address. */
1931 cache->base = 0;
1932 cache->base_p = 0;
1933 cache->sp_offset = -8;
1934 cache->pc = 0;
1936 /* Saved registers. We initialize these to -1 since zero is a valid
1937 offset (that's where %rbp is supposed to be stored).
1938 The values start out as being offsets, and are later converted to
1939 addresses (at which point -1 is interpreted as an address, still meaning
1940 "invalid"). */
1941 for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
1942 cache->saved_regs[i] = -1;
1943 cache->saved_sp = 0;
1944 cache->saved_sp_reg = -1;
1946 /* Frameless until proven otherwise. */
1947 cache->frameless_p = 1;
1950 /* Allocate and initialize a frame cache. */
1952 static struct amd64_frame_cache *
1953 amd64_alloc_frame_cache (void)
1955 struct amd64_frame_cache *cache;
1957 cache = FRAME_OBSTACK_ZALLOC (struct amd64_frame_cache);
1958 amd64_init_frame_cache (cache);
1959 return cache;
1962 /* GCC 4.4 and later, can put code in the prologue to realign the
1963 stack pointer. Check whether PC points to such code, and update
1964 CACHE accordingly. Return the first instruction after the code
1965 sequence or CURRENT_PC, whichever is smaller. If we don't
1966 recognize the code, return PC. */
1968 static CORE_ADDR
1969 amd64_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
1970 struct amd64_frame_cache *cache)
1972 /* There are 2 code sequences to re-align stack before the frame
1973 gets set up:
1975 1. Use a caller-saved saved register:
1977 leaq 8(%rsp), %reg
1978 andq $-XXX, %rsp
1979 pushq -8(%reg)
1981 2. Use a callee-saved saved register:
1983 pushq %reg
1984 leaq 16(%rsp), %reg
1985 andq $-XXX, %rsp
1986 pushq -8(%reg)
1988 "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:
1990 0x48 0x83 0xe4 0xf0 andq $-16, %rsp
1991 0x48 0x81 0xe4 0x00 0xff 0xff 0xff andq $-256, %rsp
1994 gdb_byte buf[18];
1995 int reg, r;
1996 int offset, offset_and;
1998 if (target_read_code (pc, buf, sizeof buf))
1999 return pc;
2001 /* Check caller-saved saved register. The first instruction has
2002 to be "leaq 8(%rsp), %reg". */
2003 if ((buf[0] & 0xfb) == 0x48
2004 && buf[1] == 0x8d
2005 && buf[3] == 0x24
2006 && buf[4] == 0x8)
2008 /* MOD must be binary 10 and R/M must be binary 100. */
2009 if ((buf[2] & 0xc7) != 0x44)
2010 return pc;
2012 /* REG has register number. */
2013 reg = (buf[2] >> 3) & 7;
2015 /* Check the REX.R bit. */
2016 if (buf[0] == 0x4c)
2017 reg += 8;
2019 offset = 5;
2021 else
2023 /* Check callee-saved saved register. The first instruction
2024 has to be "pushq %reg". */
2025 reg = 0;
2026 if ((buf[0] & 0xf8) == 0x50)
2027 offset = 0;
2028 else if ((buf[0] & 0xf6) == 0x40
2029 && (buf[1] & 0xf8) == 0x50)
2031 /* Check the REX.B bit. */
2032 if ((buf[0] & 1) != 0)
2033 reg = 8;
2035 offset = 1;
2037 else
2038 return pc;
2040 /* Get register. */
2041 reg += buf[offset] & 0x7;
2043 offset++;
2045 /* The next instruction has to be "leaq 16(%rsp), %reg". */
2046 if ((buf[offset] & 0xfb) != 0x48
2047 || buf[offset + 1] != 0x8d
2048 || buf[offset + 3] != 0x24
2049 || buf[offset + 4] != 0x10)
2050 return pc;
2052 /* MOD must be binary 10 and R/M must be binary 100. */
2053 if ((buf[offset + 2] & 0xc7) != 0x44)
2054 return pc;
2056 /* REG has register number. */
2057 r = (buf[offset + 2] >> 3) & 7;
2059 /* Check the REX.R bit. */
2060 if (buf[offset] == 0x4c)
2061 r += 8;
2063 /* Registers in pushq and leaq have to be the same. */
2064 if (reg != r)
2065 return pc;
2067 offset += 5;
2070 /* Rigister can't be %rsp nor %rbp. */
2071 if (reg == 4 || reg == 5)
2072 return pc;
2074 /* The next instruction has to be "andq $-XXX, %rsp". */
2075 if (buf[offset] != 0x48
2076 || buf[offset + 2] != 0xe4
2077 || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
2078 return pc;
2080 offset_and = offset;
2081 offset += buf[offset + 1] == 0x81 ? 7 : 4;
2083 /* The next instruction has to be "pushq -8(%reg)". */
2084 r = 0;
2085 if (buf[offset] == 0xff)
2086 offset++;
2087 else if ((buf[offset] & 0xf6) == 0x40
2088 && buf[offset + 1] == 0xff)
2090 /* Check the REX.B bit. */
2091 if ((buf[offset] & 0x1) != 0)
2092 r = 8;
2093 offset += 2;
2095 else
2096 return pc;
2098 /* 8bit -8 is 0xf8. REG must be binary 110 and MOD must be binary
2099 01. */
2100 if (buf[offset + 1] != 0xf8
2101 || (buf[offset] & 0xf8) != 0x70)
2102 return pc;
2104 /* R/M has register. */
2105 r += buf[offset] & 7;
2107 /* Registers in leaq and pushq have to be the same. */
2108 if (reg != r)
2109 return pc;
2111 if (current_pc > pc + offset_and)
2112 cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);
2114 return std::min (pc + offset + 2, current_pc);
2117 /* Similar to amd64_analyze_stack_align for x32. */
2119 static CORE_ADDR
2120 amd64_x32_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
2121 struct amd64_frame_cache *cache)
2123 /* There are 2 code sequences to re-align stack before the frame
2124 gets set up:
2126 1. Use a caller-saved saved register:
2128 leaq 8(%rsp), %reg
2129 andq $-XXX, %rsp
2130 pushq -8(%reg)
2134 [addr32] leal 8(%rsp), %reg
2135 andl $-XXX, %esp
2136 [addr32] pushq -8(%reg)
2138 2. Use a callee-saved saved register:
2140 pushq %reg
2141 leaq 16(%rsp), %reg
2142 andq $-XXX, %rsp
2143 pushq -8(%reg)
2147 pushq %reg
2148 [addr32] leal 16(%rsp), %reg
2149 andl $-XXX, %esp
2150 [addr32] pushq -8(%reg)
2152 "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:
2154 0x48 0x83 0xe4 0xf0 andq $-16, %rsp
2155 0x48 0x81 0xe4 0x00 0xff 0xff 0xff andq $-256, %rsp
2157 "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:
2159 0x83 0xe4 0xf0 andl $-16, %esp
2160 0x81 0xe4 0x00 0xff 0xff 0xff andl $-256, %esp
2163 gdb_byte buf[19];
2164 int reg, r;
2165 int offset, offset_and;
2167 if (target_read_memory (pc, buf, sizeof buf))
2168 return pc;
2170 /* Skip optional addr32 prefix. */
2171 offset = buf[0] == 0x67 ? 1 : 0;
2173 /* Check caller-saved saved register. The first instruction has
2174 to be "leaq 8(%rsp), %reg" or "leal 8(%rsp), %reg". */
2175 if (((buf[offset] & 0xfb) == 0x48 || (buf[offset] & 0xfb) == 0x40)
2176 && buf[offset + 1] == 0x8d
2177 && buf[offset + 3] == 0x24
2178 && buf[offset + 4] == 0x8)
2180 /* MOD must be binary 10 and R/M must be binary 100. */
2181 if ((buf[offset + 2] & 0xc7) != 0x44)
2182 return pc;
2184 /* REG has register number. */
2185 reg = (buf[offset + 2] >> 3) & 7;
2187 /* Check the REX.R bit. */
2188 if ((buf[offset] & 0x4) != 0)
2189 reg += 8;
2191 offset += 5;
2193 else
2195 /* Check callee-saved saved register. The first instruction
2196 has to be "pushq %reg". */
2197 reg = 0;
2198 if ((buf[offset] & 0xf6) == 0x40
2199 && (buf[offset + 1] & 0xf8) == 0x50)
2201 /* Check the REX.B bit. */
2202 if ((buf[offset] & 1) != 0)
2203 reg = 8;
2205 offset += 1;
2207 else if ((buf[offset] & 0xf8) != 0x50)
2208 return pc;
2210 /* Get register. */
2211 reg += buf[offset] & 0x7;
2213 offset++;
2215 /* Skip optional addr32 prefix. */
2216 if (buf[offset] == 0x67)
2217 offset++;
2219 /* The next instruction has to be "leaq 16(%rsp), %reg" or
2220 "leal 16(%rsp), %reg". */
2221 if (((buf[offset] & 0xfb) != 0x48 && (buf[offset] & 0xfb) != 0x40)
2222 || buf[offset + 1] != 0x8d
2223 || buf[offset + 3] != 0x24
2224 || buf[offset + 4] != 0x10)
2225 return pc;
2227 /* MOD must be binary 10 and R/M must be binary 100. */
2228 if ((buf[offset + 2] & 0xc7) != 0x44)
2229 return pc;
2231 /* REG has register number. */
2232 r = (buf[offset + 2] >> 3) & 7;
2234 /* Check the REX.R bit. */
2235 if ((buf[offset] & 0x4) != 0)
2236 r += 8;
2238 /* Registers in pushq and leaq have to be the same. */
2239 if (reg != r)
2240 return pc;
2242 offset += 5;
2245 /* Rigister can't be %rsp nor %rbp. */
2246 if (reg == 4 || reg == 5)
2247 return pc;
2249 /* The next instruction may be "andq $-XXX, %rsp" or
2250 "andl $-XXX, %esp". */
2251 if (buf[offset] != 0x48)
2252 offset--;
2254 if (buf[offset + 2] != 0xe4
2255 || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
2256 return pc;
2258 offset_and = offset;
2259 offset += buf[offset + 1] == 0x81 ? 7 : 4;
2261 /* Skip optional addr32 prefix. */
2262 if (buf[offset] == 0x67)
2263 offset++;
2265 /* The next instruction has to be "pushq -8(%reg)". */
2266 r = 0;
2267 if (buf[offset] == 0xff)
2268 offset++;
2269 else if ((buf[offset] & 0xf6) == 0x40
2270 && buf[offset + 1] == 0xff)
2272 /* Check the REX.B bit. */
2273 if ((buf[offset] & 0x1) != 0)
2274 r = 8;
2275 offset += 2;
2277 else
2278 return pc;
2280 /* 8bit -8 is 0xf8. REG must be binary 110 and MOD must be binary
2281 01. */
2282 if (buf[offset + 1] != 0xf8
2283 || (buf[offset] & 0xf8) != 0x70)
2284 return pc;
2286 /* R/M has register. */
2287 r += buf[offset] & 7;
2289 /* Registers in leaq and pushq have to be the same. */
2290 if (reg != r)
2291 return pc;
2293 if (current_pc > pc + offset_and)
2294 cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);
2296 return std::min (pc + offset + 2, current_pc);
2299 /* Do a limited analysis of the prologue at PC and update CACHE
2300 accordingly. Bail out early if CURRENT_PC is reached. Return the
2301 address where the analysis stopped.
2303 We will handle only functions beginning with:
2305 pushq %rbp 0x55
2306 movq %rsp, %rbp 0x48 0x89 0xe5 (or 0x48 0x8b 0xec)
2308 or (for the X32 ABI):
2310 pushq %rbp 0x55
2311 movl %esp, %ebp 0x89 0xe5 (or 0x8b 0xec)
2313 The `endbr64` instruction can be found before these sequences, and will be
2314 skipped if found.
2316 Any function that doesn't start with one of these sequences will be
2317 assumed to have no prologue and thus no valid frame pointer in
2318 %rbp. */
2320 static CORE_ADDR
2321 amd64_analyze_prologue (struct gdbarch *gdbarch,
2322 CORE_ADDR pc, CORE_ADDR current_pc,
2323 struct amd64_frame_cache *cache)
2325 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2326 /* The `endbr64` instruction. */
2327 static const gdb_byte endbr64[4] = { 0xf3, 0x0f, 0x1e, 0xfa };
2328 /* There are two variations of movq %rsp, %rbp. */
2329 static const gdb_byte mov_rsp_rbp_1[3] = { 0x48, 0x89, 0xe5 };
2330 static const gdb_byte mov_rsp_rbp_2[3] = { 0x48, 0x8b, 0xec };
2331 /* Ditto for movl %esp, %ebp. */
2332 static const gdb_byte mov_esp_ebp_1[2] = { 0x89, 0xe5 };
2333 static const gdb_byte mov_esp_ebp_2[2] = { 0x8b, 0xec };
2335 gdb_byte buf[3];
2336 gdb_byte op;
2338 if (current_pc <= pc)
2339 return current_pc;
2341 if (gdbarch_ptr_bit (gdbarch) == 32)
2342 pc = amd64_x32_analyze_stack_align (pc, current_pc, cache);
2343 else
2344 pc = amd64_analyze_stack_align (pc, current_pc, cache);
2346 op = read_code_unsigned_integer (pc, 1, byte_order);
2348 /* Check for the `endbr64` instruction, skip it if found. */
2349 if (op == endbr64[0])
2351 read_code (pc + 1, buf, 3);
2353 if (memcmp (buf, &endbr64[1], 3) == 0)
2354 pc += 4;
2356 op = read_code_unsigned_integer (pc, 1, byte_order);
2359 if (current_pc <= pc)
2360 return current_pc;
2362 if (op == 0x55) /* pushq %rbp */
2364 /* Take into account that we've executed the `pushq %rbp' that
2365 starts this instruction sequence. */
2366 cache->saved_regs[AMD64_RBP_REGNUM] = 0;
2367 cache->sp_offset += 8;
2369 /* If that's all, return now. */
2370 if (current_pc <= pc + 1)
2371 return current_pc;
2373 read_code (pc + 1, buf, 3);
2375 /* Check for `movq %rsp, %rbp'. */
2376 if (memcmp (buf, mov_rsp_rbp_1, 3) == 0
2377 || memcmp (buf, mov_rsp_rbp_2, 3) == 0)
2379 /* OK, we actually have a frame. */
2380 cache->frameless_p = 0;
2381 return pc + 4;
2384 /* For X32, also check for `movl %esp, %ebp'. */
2385 if (gdbarch_ptr_bit (gdbarch) == 32)
2387 if (memcmp (buf, mov_esp_ebp_1, 2) == 0
2388 || memcmp (buf, mov_esp_ebp_2, 2) == 0)
2390 /* OK, we actually have a frame. */
2391 cache->frameless_p = 0;
2392 return pc + 3;
2396 return pc + 1;
2399 return pc;
2402 /* Work around false termination of prologue - GCC PR debug/48827.
2404 START_PC is the first instruction of a function, PC is its minimal already
2405 determined advanced address. Function returns PC if it has nothing to do.
2407 84 c0 test %al,%al
2408 74 23 je after
2409 <-- here is 0 lines advance - the false prologue end marker.
2410 0f 29 85 70 ff ff ff movaps %xmm0,-0x90(%rbp)
2411 0f 29 4d 80 movaps %xmm1,-0x80(%rbp)
2412 0f 29 55 90 movaps %xmm2,-0x70(%rbp)
2413 0f 29 5d a0 movaps %xmm3,-0x60(%rbp)
2414 0f 29 65 b0 movaps %xmm4,-0x50(%rbp)
2415 0f 29 6d c0 movaps %xmm5,-0x40(%rbp)
2416 0f 29 75 d0 movaps %xmm6,-0x30(%rbp)
2417 0f 29 7d e0 movaps %xmm7,-0x20(%rbp)
2418 after: */
2420 static CORE_ADDR
2421 amd64_skip_xmm_prologue (CORE_ADDR pc, CORE_ADDR start_pc)
2423 struct symtab_and_line start_pc_sal, next_sal;
2424 gdb_byte buf[4 + 8 * 7];
2425 int offset, xmmreg;
2427 if (pc == start_pc)
2428 return pc;
2430 start_pc_sal = find_pc_sect_line (start_pc, NULL, 0);
2431 if (start_pc_sal.symtab == NULL
2432 || producer_is_gcc_ge_4 (start_pc_sal.symtab->compunit ()
2433 ->producer ()) < 6
2434 || start_pc_sal.pc != start_pc || pc >= start_pc_sal.end)
2435 return pc;
2437 next_sal = find_pc_sect_line (start_pc_sal.end, NULL, 0);
2438 if (next_sal.line != start_pc_sal.line)
2439 return pc;
2441 /* START_PC can be from overlayed memory, ignored here. */
2442 if (target_read_code (next_sal.pc - 4, buf, sizeof (buf)) != 0)
2443 return pc;
2445 /* test %al,%al */
2446 if (buf[0] != 0x84 || buf[1] != 0xc0)
2447 return pc;
2448 /* je AFTER */
2449 if (buf[2] != 0x74)
2450 return pc;
2452 offset = 4;
2453 for (xmmreg = 0; xmmreg < 8; xmmreg++)
2455 /* 0x0f 0x29 0b??000101 movaps %xmmreg?,-0x??(%rbp) */
2456 if (buf[offset] != 0x0f || buf[offset + 1] != 0x29
2457 || (buf[offset + 2] & 0x3f) != (xmmreg << 3 | 0x5))
2458 return pc;
2460 /* 0b01?????? */
2461 if ((buf[offset + 2] & 0xc0) == 0x40)
2463 /* 8-bit displacement. */
2464 offset += 4;
2466 /* 0b10?????? */
2467 else if ((buf[offset + 2] & 0xc0) == 0x80)
2469 /* 32-bit displacement. */
2470 offset += 7;
2472 else
2473 return pc;
2476 /* je AFTER */
2477 if (offset - 4 != buf[3])
2478 return pc;
2480 return next_sal.end;
2483 /* Return PC of first real instruction. */
2485 static CORE_ADDR
2486 amd64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
2488 struct amd64_frame_cache cache;
2489 CORE_ADDR pc;
2490 CORE_ADDR func_addr;
2492 if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
2494 CORE_ADDR post_prologue_pc
2495 = skip_prologue_using_sal (gdbarch, func_addr);
2496 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
2498 /* LLVM backend (Clang/Flang) always emits a line note before the
2499 prologue and another one after. We trust clang and newer Intel
2500 compilers to emit usable line notes. */
2501 if (post_prologue_pc
2502 && (cust != NULL
2503 && cust->producer () != nullptr
2504 && (producer_is_llvm (cust->producer ())
2505 || producer_is_icc_ge_19 (cust->producer ()))))
2506 return std::max (start_pc, post_prologue_pc);
2509 amd64_init_frame_cache (&cache);
2510 pc = amd64_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffLL,
2511 &cache);
2512 if (cache.frameless_p)
2513 return start_pc;
2515 return amd64_skip_xmm_prologue (pc, start_pc);
2519 /* Normal frames. */
2521 static void
2522 amd64_frame_cache_1 (const frame_info_ptr &this_frame,
2523 struct amd64_frame_cache *cache)
2525 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2526 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2527 gdb_byte buf[8];
2528 int i;
2530 cache->pc = get_frame_func (this_frame);
2531 if (cache->pc != 0)
2532 amd64_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
2533 cache);
2535 if (cache->frameless_p)
2537 /* We didn't find a valid frame. If we're at the start of a
2538 function, or somewhere half-way its prologue, the function's
2539 frame probably hasn't been fully setup yet. Try to
2540 reconstruct the base address for the stack frame by looking
2541 at the stack pointer. For truly "frameless" functions this
2542 might work too. */
2544 if (cache->saved_sp_reg != -1)
2546 /* Stack pointer has been saved. */
2547 get_frame_register (this_frame, cache->saved_sp_reg, buf);
2548 cache->saved_sp = extract_unsigned_integer (buf, 8, byte_order);
2550 /* We're halfway aligning the stack. */
2551 cache->base = ((cache->saved_sp - 8) & 0xfffffffffffffff0LL) - 8;
2552 cache->saved_regs[AMD64_RIP_REGNUM] = cache->saved_sp - 8;
2554 /* This will be added back below. */
2555 cache->saved_regs[AMD64_RIP_REGNUM] -= cache->base;
2557 else
2559 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2560 cache->base = extract_unsigned_integer (buf, 8, byte_order)
2561 + cache->sp_offset;
2564 else
2566 get_frame_register (this_frame, AMD64_RBP_REGNUM, buf);
2567 cache->base = extract_unsigned_integer (buf, 8, byte_order);
2570 /* Now that we have the base address for the stack frame we can
2571 calculate the value of %rsp in the calling frame. */
2572 cache->saved_sp = cache->base + 16;
2574 /* For normal frames, %rip is stored at 8(%rbp). If we don't have a
2575 frame we find it at the same offset from the reconstructed base
2576 address. If we're halfway aligning the stack, %rip is handled
2577 differently (see above). */
2578 if (!cache->frameless_p || cache->saved_sp_reg == -1)
2579 cache->saved_regs[AMD64_RIP_REGNUM] = 8;
2581 /* Adjust all the saved registers such that they contain addresses
2582 instead of offsets. */
2583 for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
2584 if (cache->saved_regs[i] != -1)
2585 cache->saved_regs[i] += cache->base;
2587 cache->base_p = 1;
2590 static struct amd64_frame_cache *
2591 amd64_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
2593 struct amd64_frame_cache *cache;
2595 if (*this_cache)
2596 return (struct amd64_frame_cache *) *this_cache;
2598 cache = amd64_alloc_frame_cache ();
2599 *this_cache = cache;
2603 amd64_frame_cache_1 (this_frame, cache);
2605 catch (const gdb_exception_error &ex)
2607 if (ex.error != NOT_AVAILABLE_ERROR)
2608 throw;
2611 return cache;
2614 static enum unwind_stop_reason
2615 amd64_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
2616 void **this_cache)
2618 struct amd64_frame_cache *cache =
2619 amd64_frame_cache (this_frame, this_cache);
2621 if (!cache->base_p)
2622 return UNWIND_UNAVAILABLE;
2624 /* This marks the outermost frame. */
2625 if (cache->base == 0)
2626 return UNWIND_OUTERMOST;
2628 return UNWIND_NO_REASON;
2631 static void
2632 amd64_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
2633 struct frame_id *this_id)
2635 struct amd64_frame_cache *cache =
2636 amd64_frame_cache (this_frame, this_cache);
2638 if (!cache->base_p)
2639 (*this_id) = frame_id_build_unavailable_stack (cache->pc);
2640 else if (cache->base == 0)
2642 /* This marks the outermost frame. */
2643 return;
2645 else
2646 (*this_id) = frame_id_build (cache->base + 16, cache->pc);
2649 static struct value *
2650 amd64_frame_prev_register (const frame_info_ptr &this_frame, void **this_cache,
2651 int regnum)
2653 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2654 struct amd64_frame_cache *cache =
2655 amd64_frame_cache (this_frame, this_cache);
2657 gdb_assert (regnum >= 0);
2659 if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
2660 return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
2662 if (regnum < AMD64_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
2663 return frame_unwind_got_memory (this_frame, regnum,
2664 cache->saved_regs[regnum]);
2666 return frame_unwind_got_register (this_frame, regnum, regnum);
2669 static const struct frame_unwind_legacy amd64_frame_unwind (
2670 "amd64 prologue",
2671 NORMAL_FRAME,
2672 FRAME_UNWIND_ARCH,
2673 amd64_frame_unwind_stop_reason,
2674 amd64_frame_this_id,
2675 amd64_frame_prev_register,
2676 NULL,
2677 default_frame_sniffer
2680 /* Generate a bytecode expression to get the value of the saved PC. */
2682 static void
2683 amd64_gen_return_address (struct gdbarch *gdbarch,
2684 struct agent_expr *ax, struct axs_value *value,
2685 CORE_ADDR scope)
2687 /* The following sequence assumes the traditional use of the base
2688 register. */
2689 ax_reg (ax, AMD64_RBP_REGNUM);
2690 ax_const_l (ax, 8);
2691 ax_simple (ax, aop_add);
2692 value->type = register_type (gdbarch, AMD64_RIP_REGNUM);
2693 value->kind = axs_lvalue_memory;
2697 /* Signal trampolines. */
2699 /* FIXME: kettenis/20030419: Perhaps, we can unify the 32-bit and
2700 64-bit variants. This would require using identical frame caches
2701 on both platforms. */
2703 static struct amd64_frame_cache *
2704 amd64_sigtramp_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
2706 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2707 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
2708 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2709 struct amd64_frame_cache *cache;
2710 CORE_ADDR addr;
2711 gdb_byte buf[8];
2712 int i;
2714 if (*this_cache)
2715 return (struct amd64_frame_cache *) *this_cache;
2717 cache = amd64_alloc_frame_cache ();
2721 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2722 cache->base = extract_unsigned_integer (buf, 8, byte_order) - 8;
2724 addr = tdep->sigcontext_addr (this_frame);
2725 gdb_assert (tdep->sc_reg_offset);
2726 gdb_assert (tdep->sc_num_regs <= AMD64_NUM_SAVED_REGS);
2727 for (i = 0; i < tdep->sc_num_regs; i++)
2728 if (tdep->sc_reg_offset[i] != -1)
2729 cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
2731 cache->base_p = 1;
2733 catch (const gdb_exception_error &ex)
2735 if (ex.error != NOT_AVAILABLE_ERROR)
2736 throw;
2739 *this_cache = cache;
2740 return cache;
2743 static enum unwind_stop_reason
2744 amd64_sigtramp_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
2745 void **this_cache)
2747 struct amd64_frame_cache *cache =
2748 amd64_sigtramp_frame_cache (this_frame, this_cache);
2750 if (!cache->base_p)
2751 return UNWIND_UNAVAILABLE;
2753 return UNWIND_NO_REASON;
2756 static void
2757 amd64_sigtramp_frame_this_id (const frame_info_ptr &this_frame,
2758 void **this_cache, struct frame_id *this_id)
2760 struct amd64_frame_cache *cache =
2761 amd64_sigtramp_frame_cache (this_frame, this_cache);
2763 if (!cache->base_p)
2764 (*this_id) = frame_id_build_unavailable_stack (get_frame_pc (this_frame));
2765 else if (cache->base == 0)
2767 /* This marks the outermost frame. */
2768 return;
2770 else
2771 (*this_id) = frame_id_build (cache->base + 16, get_frame_pc (this_frame));
2774 static struct value *
2775 amd64_sigtramp_frame_prev_register (const frame_info_ptr &this_frame,
2776 void **this_cache, int regnum)
2778 /* Make sure we've initialized the cache. */
2779 amd64_sigtramp_frame_cache (this_frame, this_cache);
2781 return amd64_frame_prev_register (this_frame, this_cache, regnum);
2784 static int
2785 amd64_sigtramp_frame_sniffer (const struct frame_unwind *self,
2786 const frame_info_ptr &this_frame,
2787 void **this_cache)
2789 gdbarch *arch = get_frame_arch (this_frame);
2790 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch);
2792 /* We shouldn't even bother if we don't have a sigcontext_addr
2793 handler. */
2794 if (tdep->sigcontext_addr == NULL)
2795 return 0;
2797 if (tdep->sigtramp_p != NULL)
2799 if (tdep->sigtramp_p (this_frame))
2800 return 1;
2803 if (tdep->sigtramp_start != 0)
2805 CORE_ADDR pc = get_frame_pc (this_frame);
2807 gdb_assert (tdep->sigtramp_end != 0);
2808 if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
2809 return 1;
2812 return 0;
2815 static const struct frame_unwind_legacy amd64_sigtramp_frame_unwind (
2816 "amd64 sigtramp",
2817 SIGTRAMP_FRAME,
2818 FRAME_UNWIND_ARCH,
2819 amd64_sigtramp_frame_unwind_stop_reason,
2820 amd64_sigtramp_frame_this_id,
2821 amd64_sigtramp_frame_prev_register,
2822 NULL,
2823 amd64_sigtramp_frame_sniffer
2827 static CORE_ADDR
2828 amd64_frame_base_address (const frame_info_ptr &this_frame, void **this_cache)
2830 struct amd64_frame_cache *cache =
2831 amd64_frame_cache (this_frame, this_cache);
2833 return cache->base;
2836 static const struct frame_base amd64_frame_base =
2838 &amd64_frame_unwind,
2839 amd64_frame_base_address,
2840 amd64_frame_base_address,
2841 amd64_frame_base_address
2844 /* Implement core of the stack_frame_destroyed_p gdbarch method. */
2846 static int
2847 amd64_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
2849 gdb_byte insn;
2851 std::optional<CORE_ADDR> epilogue = find_epilogue_using_linetable (pc);
2853 /* PC is pointing at the next instruction to be executed. If it is
2854 equal to the epilogue start, it means we're right before it starts,
2855 so the stack is still valid. */
2856 if (epilogue)
2857 return pc > epilogue;
2859 if (target_read_memory (pc, &insn, 1))
2860 return 0; /* Can't read memory at pc. */
2862 if (insn != 0xc3) /* 'ret' instruction. */
2863 return 0;
2865 return 1;
2868 /* Normal frames, but in a function epilogue. */
2870 /* Implement the stack_frame_destroyed_p gdbarch method.
2872 The epilogue is defined here as the 'ret' instruction, which will
2873 follow any instruction such as 'leave' or 'pop %ebp' that destroys
2874 the function's stack frame. */
2876 static int
2877 amd64_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
2879 struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
2881 if (cust != nullptr && cust->producer () != nullptr
2882 && producer_is_llvm (cust->producer ()))
2883 return amd64_stack_frame_destroyed_p_1 (gdbarch, pc);
2885 return 0;
2888 static int
2889 amd64_epilogue_frame_sniffer_1 (const struct frame_unwind *self,
2890 const frame_info_ptr &this_frame,
2891 void **this_prologue_cache, bool override_p)
2893 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2894 CORE_ADDR pc = get_frame_pc (this_frame);
2896 if (frame_relative_level (this_frame) != 0)
2897 /* We're not in the inner frame, so assume we're not in an epilogue. */
2898 return 0;
2900 bool unwind_valid_p
2901 = compunit_epilogue_unwind_valid (find_pc_compunit_symtab (pc));
2902 if (override_p)
2904 if (unwind_valid_p)
2905 /* Don't override the symtab unwinders, skip
2906 "amd64 epilogue override". */
2907 return 0;
2909 else
2911 if (!unwind_valid_p)
2912 /* "amd64 epilogue override" unwinder already ran, skip
2913 "amd64 epilogue". */
2914 return 0;
2917 /* Check whether we're in an epilogue. */
2918 return amd64_stack_frame_destroyed_p_1 (gdbarch, pc);
2921 static int
2922 amd64_epilogue_override_frame_sniffer (const struct frame_unwind *self,
2923 const frame_info_ptr &this_frame,
2924 void **this_prologue_cache)
2926 return amd64_epilogue_frame_sniffer_1 (self, this_frame, this_prologue_cache,
2927 true);
2930 static int
2931 amd64_epilogue_frame_sniffer (const struct frame_unwind *self,
2932 const frame_info_ptr &this_frame,
2933 void **this_prologue_cache)
2935 return amd64_epilogue_frame_sniffer_1 (self, this_frame, this_prologue_cache,
2936 false);
2939 static struct amd64_frame_cache *
2940 amd64_epilogue_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
2942 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2943 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2944 struct amd64_frame_cache *cache;
2945 gdb_byte buf[8];
2947 if (*this_cache)
2948 return (struct amd64_frame_cache *) *this_cache;
2950 cache = amd64_alloc_frame_cache ();
2951 *this_cache = cache;
2955 /* Cache base will be %rsp plus cache->sp_offset (-8). */
2956 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2957 cache->base = extract_unsigned_integer (buf, 8,
2958 byte_order) + cache->sp_offset;
2960 /* Cache pc will be the frame func. */
2961 cache->pc = get_frame_func (this_frame);
2963 /* The previous value of %rsp is cache->base plus 16. */
2964 cache->saved_sp = cache->base + 16;
2966 /* The saved %rip will be at cache->base plus 8. */
2967 cache->saved_regs[AMD64_RIP_REGNUM] = cache->base + 8;
2969 cache->base_p = 1;
2971 catch (const gdb_exception_error &ex)
2973 if (ex.error != NOT_AVAILABLE_ERROR)
2974 throw;
2977 return cache;
2980 static enum unwind_stop_reason
2981 amd64_epilogue_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
2982 void **this_cache)
2984 struct amd64_frame_cache *cache
2985 = amd64_epilogue_frame_cache (this_frame, this_cache);
2987 if (!cache->base_p)
2988 return UNWIND_UNAVAILABLE;
2990 return UNWIND_NO_REASON;
2993 static void
2994 amd64_epilogue_frame_this_id (const frame_info_ptr &this_frame,
2995 void **this_cache,
2996 struct frame_id *this_id)
2998 struct amd64_frame_cache *cache = amd64_epilogue_frame_cache (this_frame,
2999 this_cache);
3001 if (!cache->base_p)
3002 (*this_id) = frame_id_build_unavailable_stack (cache->pc);
3003 else
3004 (*this_id) = frame_id_build (cache->base + 16, cache->pc);
3007 static const struct frame_unwind_legacy amd64_epilogue_override_frame_unwind (
3008 "amd64 epilogue override",
3009 NORMAL_FRAME,
3010 FRAME_UNWIND_ARCH,
3011 amd64_epilogue_frame_unwind_stop_reason,
3012 amd64_epilogue_frame_this_id,
3013 amd64_frame_prev_register,
3014 NULL,
3015 amd64_epilogue_override_frame_sniffer
3018 static const struct frame_unwind_legacy amd64_epilogue_frame_unwind (
3019 "amd64 epilogue",
3020 NORMAL_FRAME,
3021 FRAME_UNWIND_ARCH,
3022 amd64_epilogue_frame_unwind_stop_reason,
3023 amd64_epilogue_frame_this_id,
3024 amd64_frame_prev_register,
3025 NULL,
3026 amd64_epilogue_frame_sniffer
3029 static struct frame_id
3030 amd64_dummy_id (struct gdbarch *gdbarch, const frame_info_ptr &this_frame)
3032 CORE_ADDR fp;
3034 fp = get_frame_register_unsigned (this_frame, AMD64_RBP_REGNUM);
3036 return frame_id_build (fp + 16, get_frame_pc (this_frame));
3039 /* 16 byte align the SP per frame requirements. */
3041 static CORE_ADDR
3042 amd64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3044 return sp & -(CORE_ADDR)16;
3048 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
3049 in the floating-point register set REGSET to register cache
3050 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
3052 static void
3053 amd64_supply_fpregset (const struct regset *regset, struct regcache *regcache,
3054 int regnum, const void *fpregs, size_t len)
3056 struct gdbarch *gdbarch = regcache->arch ();
3057 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3059 gdb_assert (len >= tdep->sizeof_fpregset);
3060 amd64_supply_fxsave (regcache, regnum, fpregs);
3063 /* Collect register REGNUM from the register cache REGCACHE and store
3064 it in the buffer specified by FPREGS and LEN as described by the
3065 floating-point register set REGSET. If REGNUM is -1, do this for
3066 all registers in REGSET. */
3068 static void
3069 amd64_collect_fpregset (const struct regset *regset,
3070 const struct regcache *regcache,
3071 int regnum, void *fpregs, size_t len)
3073 struct gdbarch *gdbarch = regcache->arch ();
3074 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3076 gdb_assert (len >= tdep->sizeof_fpregset);
3077 amd64_collect_fxsave (regcache, regnum, fpregs);
3080 const struct regset amd64_fpregset =
3082 NULL, amd64_supply_fpregset, amd64_collect_fpregset
3086 /* Figure out where the longjmp will land. Slurp the jmp_buf out of
3087 %rdi. We expect its value to be a pointer to the jmp_buf structure
3088 from which we extract the address that we will land at. This
3089 address is copied into PC. This routine returns non-zero on
3090 success. */
3092 static int
3093 amd64_get_longjmp_target (const frame_info_ptr &frame, CORE_ADDR *pc)
3095 gdb_byte buf[8];
3096 CORE_ADDR jb_addr;
3097 struct gdbarch *gdbarch = get_frame_arch (frame);
3098 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3099 int jb_pc_offset = tdep->jb_pc_offset;
3100 int len = builtin_type (gdbarch)->builtin_func_ptr->length ();
3102 /* If JB_PC_OFFSET is -1, we have no way to find out where the
3103 longjmp will land. */
3104 if (jb_pc_offset == -1)
3105 return 0;
3107 get_frame_register (frame, AMD64_RDI_REGNUM, buf);
3108 jb_addr= extract_typed_address
3109 (buf, builtin_type (gdbarch)->builtin_data_ptr);
3110 if (target_read_memory (jb_addr + jb_pc_offset, buf, len))
3111 return 0;
3113 *pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
3115 return 1;
3118 static const int amd64_record_regmap[] =
3120 AMD64_RAX_REGNUM, AMD64_RCX_REGNUM, AMD64_RDX_REGNUM, AMD64_RBX_REGNUM,
3121 AMD64_RSP_REGNUM, AMD64_RBP_REGNUM, AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
3122 AMD64_R8_REGNUM, AMD64_R9_REGNUM, AMD64_R10_REGNUM, AMD64_R11_REGNUM,
3123 AMD64_R12_REGNUM, AMD64_R13_REGNUM, AMD64_R14_REGNUM, AMD64_R15_REGNUM,
3124 AMD64_RIP_REGNUM, AMD64_EFLAGS_REGNUM, AMD64_CS_REGNUM, AMD64_SS_REGNUM,
3125 AMD64_DS_REGNUM, AMD64_ES_REGNUM, AMD64_FS_REGNUM, AMD64_GS_REGNUM,
3126 AMD64_XMM0_REGNUM
3129 /* Implement the "in_indirect_branch_thunk" gdbarch function. */
3131 static bool
3132 amd64_in_indirect_branch_thunk (struct gdbarch *gdbarch, CORE_ADDR pc)
3134 return x86_in_indirect_branch_thunk (pc, amd64_register_names,
3135 AMD64_RAX_REGNUM,
3136 AMD64_RIP_REGNUM);
3139 void
3140 amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
3141 const target_desc *default_tdesc)
3143 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3144 const struct target_desc *tdesc = info.target_desc;
3145 static const char *const stap_integer_prefixes[] = { "$", NULL };
3146 static const char *const stap_register_prefixes[] = { "%", NULL };
3147 static const char *const stap_register_indirection_prefixes[] = { "(",
3148 NULL };
3149 static const char *const stap_register_indirection_suffixes[] = { ")",
3150 NULL };
3152 /* AMD64 generally uses `fxsave' instead of `fsave' for saving its
3153 floating-point registers. */
3154 tdep->sizeof_fpregset = I387_SIZEOF_FXSAVE;
3155 tdep->fpregset = &amd64_fpregset;
3157 if (! tdesc_has_registers (tdesc))
3158 tdesc = default_tdesc;
3159 tdep->tdesc = tdesc;
3161 tdep->num_core_regs = AMD64_NUM_GREGS + I387_NUM_REGS;
3162 tdep->register_names = amd64_register_names;
3164 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx512") != NULL)
3166 tdep->zmmh_register_names = amd64_zmmh_names;
3167 tdep->k_register_names = amd64_k_names;
3168 tdep->xmm_avx512_register_names = amd64_xmm_avx512_names;
3169 tdep->ymm16h_register_names = amd64_ymmh_avx512_names;
3171 tdep->num_zmm_regs = 32;
3172 tdep->num_xmm_avx512_regs = 16;
3173 tdep->num_ymm_avx512_regs = 16;
3175 tdep->zmm0h_regnum = AMD64_ZMM0H_REGNUM;
3176 tdep->k0_regnum = AMD64_K0_REGNUM;
3177 tdep->xmm16_regnum = AMD64_XMM16_REGNUM;
3178 tdep->ymm16h_regnum = AMD64_YMM16H_REGNUM;
3181 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx") != NULL)
3183 tdep->ymmh_register_names = amd64_ymmh_names;
3184 tdep->num_ymm_regs = 16;
3185 tdep->ymm0h_regnum = AMD64_YMM0H_REGNUM;
3188 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.segments") != NULL)
3190 tdep->fsbase_regnum = AMD64_FSBASE_REGNUM;
3193 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.pkeys") != NULL)
3195 tdep->pkeys_register_names = amd64_pkeys_names;
3196 tdep->pkru_regnum = AMD64_PKRU_REGNUM;
3197 tdep->num_pkeys_regs = 1;
3200 tdep->num_byte_regs = 20;
3201 tdep->num_word_regs = 16;
3202 tdep->num_dword_regs = 16;
3203 /* Avoid wiring in the MMX registers for now. */
3204 tdep->num_mmx_regs = 0;
3206 set_gdbarch_pseudo_register_read_value (gdbarch,
3207 amd64_pseudo_register_read_value);
3208 set_gdbarch_pseudo_register_write (gdbarch, amd64_pseudo_register_write);
3209 set_gdbarch_ax_pseudo_register_collect (gdbarch,
3210 amd64_ax_pseudo_register_collect);
3212 set_tdesc_pseudo_register_name (gdbarch, amd64_pseudo_register_name);
3214 /* AMD64 has an FPU and 16 SSE registers. */
3215 tdep->st0_regnum = AMD64_ST0_REGNUM;
3216 tdep->num_xmm_regs = 16;
3218 /* This is what all the fuss is about. */
3219 set_gdbarch_long_bit (gdbarch, 64);
3220 set_gdbarch_long_long_bit (gdbarch, 64);
3221 set_gdbarch_ptr_bit (gdbarch, 64);
3223 /* In contrast to the i386, on AMD64 a `long double' actually takes
3224 up 128 bits, even though it's still based on the i387 extended
3225 floating-point format which has only 80 significant bits. */
3226 set_gdbarch_long_double_bit (gdbarch, 128);
3228 set_gdbarch_num_regs (gdbarch, AMD64_NUM_REGS);
3230 /* Register numbers of various important registers. */
3231 set_gdbarch_sp_regnum (gdbarch, AMD64_RSP_REGNUM); /* %rsp */
3232 set_gdbarch_pc_regnum (gdbarch, AMD64_RIP_REGNUM); /* %rip */
3233 set_gdbarch_ps_regnum (gdbarch, AMD64_EFLAGS_REGNUM); /* %eflags */
3234 set_gdbarch_fp0_regnum (gdbarch, AMD64_ST0_REGNUM); /* %st(0) */
3236 /* The "default" register numbering scheme for AMD64 is referred to
3237 as the "DWARF Register Number Mapping" in the System V psABI.
3238 The preferred debugging format for all known AMD64 targets is
3239 actually DWARF2, and GCC doesn't seem to support DWARF (that is
3240 DWARF-1), but we provide the same mapping just in case. This
3241 mapping is also used for stabs, which GCC does support. */
3242 set_gdbarch_stab_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
3243 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
3245 /* We don't override SDB_REG_RO_REGNUM, since COFF doesn't seem to
3246 be in use on any of the supported AMD64 targets. */
3248 /* Call dummy code. */
3249 set_gdbarch_push_dummy_call (gdbarch, amd64_push_dummy_call);
3250 set_gdbarch_frame_align (gdbarch, amd64_frame_align);
3251 set_gdbarch_frame_red_zone_size (gdbarch, 128);
3253 set_gdbarch_convert_register_p (gdbarch, i387_convert_register_p);
3254 set_gdbarch_register_to_value (gdbarch, i387_register_to_value);
3255 set_gdbarch_value_to_register (gdbarch, i387_value_to_register);
3257 set_gdbarch_return_value_as_value (gdbarch, amd64_return_value);
3259 set_gdbarch_skip_prologue (gdbarch, amd64_skip_prologue);
3261 tdep->record_regmap = amd64_record_regmap;
3263 set_gdbarch_dummy_id (gdbarch, amd64_dummy_id);
3265 /* Hook the function epilogue frame unwinder. This unwinder is
3266 appended to the list first, so that it supersedes the other
3267 unwinders in function epilogues. */
3268 frame_unwind_prepend_unwinder (gdbarch, &amd64_epilogue_override_frame_unwind);
3270 frame_unwind_append_unwinder (gdbarch, &amd64_epilogue_frame_unwind);
3272 /* Hook the prologue-based frame unwinders. */
3273 frame_unwind_append_unwinder (gdbarch, &amd64_sigtramp_frame_unwind);
3274 frame_unwind_append_unwinder (gdbarch, &amd64_frame_unwind);
3275 frame_base_set_default (gdbarch, &amd64_frame_base);
3277 set_gdbarch_get_longjmp_target (gdbarch, amd64_get_longjmp_target);
3279 set_gdbarch_relocate_instruction (gdbarch, amd64_relocate_instruction);
3281 set_gdbarch_gen_return_address (gdbarch, amd64_gen_return_address);
3283 set_gdbarch_stack_frame_destroyed_p (gdbarch, amd64_stack_frame_destroyed_p);
3285 /* SystemTap variables and functions. */
3286 set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
3287 set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
3288 set_gdbarch_stap_register_indirection_prefixes (gdbarch,
3289 stap_register_indirection_prefixes);
3290 set_gdbarch_stap_register_indirection_suffixes (gdbarch,
3291 stap_register_indirection_suffixes);
3292 set_gdbarch_stap_is_single_operand (gdbarch,
3293 i386_stap_is_single_operand);
3294 set_gdbarch_stap_parse_special_token (gdbarch,
3295 i386_stap_parse_special_token);
3296 set_gdbarch_insn_is_call (gdbarch, amd64_insn_is_call);
3297 set_gdbarch_insn_is_ret (gdbarch, amd64_insn_is_ret);
3298 set_gdbarch_insn_is_jump (gdbarch, amd64_insn_is_jump);
3300 set_gdbarch_in_indirect_branch_thunk (gdbarch,
3301 amd64_in_indirect_branch_thunk);
3303 register_amd64_ravenscar_ops (gdbarch);
3306 /* Initialize ARCH for x86-64, no osabi. */
3308 static void
3309 amd64_none_init_abi (gdbarch_info info, gdbarch *arch)
3311 amd64_init_abi (info, arch, amd64_target_description (X86_XSTATE_SSE_MASK,
3312 true));
3315 static struct type *
3316 amd64_x32_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
3318 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3320 switch (regnum - tdep->eax_regnum)
3322 case AMD64_RBP_REGNUM: /* %ebp */
3323 case AMD64_RSP_REGNUM: /* %esp */
3324 return builtin_type (gdbarch)->builtin_data_ptr;
3325 case AMD64_RIP_REGNUM: /* %eip */
3326 return builtin_type (gdbarch)->builtin_func_ptr;
3329 return i386_pseudo_register_type (gdbarch, regnum);
3332 void
3333 amd64_x32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
3334 const target_desc *default_tdesc)
3336 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3338 amd64_init_abi (info, gdbarch, default_tdesc);
3340 tdep->num_dword_regs = 17;
3341 set_tdesc_pseudo_register_type (gdbarch, amd64_x32_pseudo_register_type);
3343 set_gdbarch_long_bit (gdbarch, 32);
3344 set_gdbarch_ptr_bit (gdbarch, 32);
3347 /* Initialize ARCH for x64-32, no osabi. */
3349 static void
3350 amd64_x32_none_init_abi (gdbarch_info info, gdbarch *arch)
3352 amd64_x32_init_abi (info, arch,
3353 amd64_target_description (X86_XSTATE_SSE_MASK, true));
3356 /* Return the target description for a specified XSAVE feature mask. */
3358 const struct target_desc *
3359 amd64_target_description (uint64_t xcr0, bool segments)
3361 static target_desc *amd64_tdescs \
3362 [2/*AVX*/][2/*AVX512*/][2/*PKRU*/][2/*segments*/] = {};
3363 target_desc **tdesc;
3365 tdesc = &amd64_tdescs[(xcr0 & X86_XSTATE_AVX) ? 1 : 0]
3366 [(xcr0 & X86_XSTATE_AVX512) ? 1 : 0]
3367 [(xcr0 & X86_XSTATE_PKRU) ? 1 : 0]
3368 [segments ? 1 : 0];
3370 if (*tdesc == NULL)
3371 *tdesc = amd64_create_target_description (xcr0, false, false,
3372 segments);
3374 return *tdesc;
3377 void _initialize_amd64_tdep ();
3378 void
3379 _initialize_amd64_tdep ()
3381 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, GDB_OSABI_NONE,
3382 amd64_none_init_abi);
3383 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x64_32, GDB_OSABI_NONE,
3384 amd64_x32_none_init_abi);
3388 /* The 64-bit FXSAVE format differs from the 32-bit format in the
3389 sense that the instruction pointer and data pointer are simply
3390 64-bit offsets into the code segment and the data segment instead
3391 of a selector offset pair. The functions below store the upper 32
3392 bits of these pointers (instead of just the 16-bits of the segment
3393 selector). */
3395 /* Fill register REGNUM in REGCACHE with the appropriate
3396 floating-point or SSE register value from *FXSAVE. If REGNUM is
3397 -1, do this for all registers. This function masks off any of the
3398 reserved bits in *FXSAVE. */
3400 void
3401 amd64_supply_fxsave (struct regcache *regcache, int regnum,
3402 const void *fxsave)
3404 struct gdbarch *gdbarch = regcache->arch ();
3405 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3407 i387_supply_fxsave (regcache, regnum, fxsave);
3409 if (fxsave
3410 && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3412 const gdb_byte *regs = (const gdb_byte *) fxsave;
3414 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3415 regcache->raw_supply (I387_FISEG_REGNUM (tdep), regs + 12);
3416 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3417 regcache->raw_supply (I387_FOSEG_REGNUM (tdep), regs + 20);
3421 /* Similar to amd64_supply_fxsave, but use XSAVE extended state. */
3423 void
3424 amd64_supply_xsave (struct regcache *regcache, int regnum,
3425 const void *xsave)
3427 struct gdbarch *gdbarch = regcache->arch ();
3428 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3430 i387_supply_xsave (regcache, regnum, xsave);
3432 if (xsave
3433 && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3435 const gdb_byte *regs = (const gdb_byte *) xsave;
3436 ULONGEST clear_bv;
3438 clear_bv = i387_xsave_get_clear_bv (gdbarch, xsave);
3440 /* If the FISEG and FOSEG registers have not been initialised yet
3441 (their CLEAR_BV bit is set) then their default values of zero will
3442 have already been setup by I387_SUPPLY_XSAVE. */
3443 if (!(clear_bv & X86_XSTATE_X87))
3445 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3446 regcache->raw_supply (I387_FISEG_REGNUM (tdep), regs + 12);
3447 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3448 regcache->raw_supply (I387_FOSEG_REGNUM (tdep), regs + 20);
3453 /* Fill register REGNUM (if it is a floating-point or SSE register) in
3454 *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
3455 all registers. This function doesn't touch any of the reserved
3456 bits in *FXSAVE. */
3458 void
3459 amd64_collect_fxsave (const struct regcache *regcache, int regnum,
3460 void *fxsave)
3462 struct gdbarch *gdbarch = regcache->arch ();
3463 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3464 gdb_byte *regs = (gdb_byte *) fxsave;
3466 i387_collect_fxsave (regcache, regnum, fxsave);
3468 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3470 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3471 regcache->raw_collect (I387_FISEG_REGNUM (tdep), regs + 12);
3472 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3473 regcache->raw_collect (I387_FOSEG_REGNUM (tdep), regs + 20);
3477 /* Similar to amd64_collect_fxsave, but use XSAVE extended state. */
3479 void
3480 amd64_collect_xsave (const struct regcache *regcache, int regnum,
3481 void *xsave, int gcore)
3483 struct gdbarch *gdbarch = regcache->arch ();
3484 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3485 gdb_byte *regs = (gdb_byte *) xsave;
3487 i387_collect_xsave (regcache, regnum, xsave, gcore);
3489 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3491 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3492 regcache->raw_collect (I387_FISEG_REGNUM (tdep),
3493 regs + 12);
3494 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3495 regcache->raw_collect (I387_FOSEG_REGNUM (tdep),
3496 regs + 20);