infrun: step through indirect branch thunks
[deliverable/binutils-gdb.git] / gdb / amd64-tdep.c
1 /* Target-dependent code for AMD64.
2
3 Copyright (C) 2001-2018 Free Software Foundation, Inc.
4
5 Contributed by Jiri Smid, SuSE Labs.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "opcode/i386.h"
24 #include "dis-asm.h"
25 #include "arch-utils.h"
26 #include "block.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 "gdbcmd.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 "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 "common/byte-vector.h"
50 #include "osabi.h"
51 #include "x86-tdep.h"
52
53 /* Note that the AMD64 architecture was previously known as x86-64.
54 The latter is (forever) engraved into the canonical system name as
55 returned by config.guess, and used as the name for the AMD64 port
56 of GNU/Linux. The BSD's have renamed their ports to amd64; they
57 don't like to shout. For GDB we prefer the amd64_-prefix over the
58 x86_64_-prefix since it's so much easier to type. */
59
60 /* Register information. */
61
62 static const char *amd64_register_names[] =
63 {
64 "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp",
65
66 /* %r8 is indeed register number 8. */
67 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
68 "rip", "eflags", "cs", "ss", "ds", "es", "fs", "gs",
69
70 /* %st0 is register number 24. */
71 "st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7",
72 "fctrl", "fstat", "ftag", "fiseg", "fioff", "foseg", "fooff", "fop",
73
74 /* %xmm0 is register number 40. */
75 "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
76 "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
77 "mxcsr",
78 };
79
80 static const char *amd64_ymm_names[] =
81 {
82 "ymm0", "ymm1", "ymm2", "ymm3",
83 "ymm4", "ymm5", "ymm6", "ymm7",
84 "ymm8", "ymm9", "ymm10", "ymm11",
85 "ymm12", "ymm13", "ymm14", "ymm15"
86 };
87
88 static const char *amd64_ymm_avx512_names[] =
89 {
90 "ymm16", "ymm17", "ymm18", "ymm19",
91 "ymm20", "ymm21", "ymm22", "ymm23",
92 "ymm24", "ymm25", "ymm26", "ymm27",
93 "ymm28", "ymm29", "ymm30", "ymm31"
94 };
95
96 static const char *amd64_ymmh_names[] =
97 {
98 "ymm0h", "ymm1h", "ymm2h", "ymm3h",
99 "ymm4h", "ymm5h", "ymm6h", "ymm7h",
100 "ymm8h", "ymm9h", "ymm10h", "ymm11h",
101 "ymm12h", "ymm13h", "ymm14h", "ymm15h"
102 };
103
104 static const char *amd64_ymmh_avx512_names[] =
105 {
106 "ymm16h", "ymm17h", "ymm18h", "ymm19h",
107 "ymm20h", "ymm21h", "ymm22h", "ymm23h",
108 "ymm24h", "ymm25h", "ymm26h", "ymm27h",
109 "ymm28h", "ymm29h", "ymm30h", "ymm31h"
110 };
111
112 static const char *amd64_mpx_names[] =
113 {
114 "bnd0raw", "bnd1raw", "bnd2raw", "bnd3raw", "bndcfgu", "bndstatus"
115 };
116
117 static const char *amd64_k_names[] =
118 {
119 "k0", "k1", "k2", "k3",
120 "k4", "k5", "k6", "k7"
121 };
122
123 static const char *amd64_zmmh_names[] =
124 {
125 "zmm0h", "zmm1h", "zmm2h", "zmm3h",
126 "zmm4h", "zmm5h", "zmm6h", "zmm7h",
127 "zmm8h", "zmm9h", "zmm10h", "zmm11h",
128 "zmm12h", "zmm13h", "zmm14h", "zmm15h",
129 "zmm16h", "zmm17h", "zmm18h", "zmm19h",
130 "zmm20h", "zmm21h", "zmm22h", "zmm23h",
131 "zmm24h", "zmm25h", "zmm26h", "zmm27h",
132 "zmm28h", "zmm29h", "zmm30h", "zmm31h"
133 };
134
135 static const char *amd64_zmm_names[] =
136 {
137 "zmm0", "zmm1", "zmm2", "zmm3",
138 "zmm4", "zmm5", "zmm6", "zmm7",
139 "zmm8", "zmm9", "zmm10", "zmm11",
140 "zmm12", "zmm13", "zmm14", "zmm15",
141 "zmm16", "zmm17", "zmm18", "zmm19",
142 "zmm20", "zmm21", "zmm22", "zmm23",
143 "zmm24", "zmm25", "zmm26", "zmm27",
144 "zmm28", "zmm29", "zmm30", "zmm31"
145 };
146
147 static const char *amd64_xmm_avx512_names[] = {
148 "xmm16", "xmm17", "xmm18", "xmm19",
149 "xmm20", "xmm21", "xmm22", "xmm23",
150 "xmm24", "xmm25", "xmm26", "xmm27",
151 "xmm28", "xmm29", "xmm30", "xmm31"
152 };
153
154 static const char *amd64_pkeys_names[] = {
155 "pkru"
156 };
157
158 /* DWARF Register Number Mapping as defined in the System V psABI,
159 section 3.6. */
160
161 static int amd64_dwarf_regmap[] =
162 {
163 /* General Purpose Registers RAX, RDX, RCX, RBX, RSI, RDI. */
164 AMD64_RAX_REGNUM, AMD64_RDX_REGNUM,
165 AMD64_RCX_REGNUM, AMD64_RBX_REGNUM,
166 AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
167
168 /* Frame Pointer Register RBP. */
169 AMD64_RBP_REGNUM,
170
171 /* Stack Pointer Register RSP. */
172 AMD64_RSP_REGNUM,
173
174 /* Extended Integer Registers 8 - 15. */
175 AMD64_R8_REGNUM, /* %r8 */
176 AMD64_R9_REGNUM, /* %r9 */
177 AMD64_R10_REGNUM, /* %r10 */
178 AMD64_R11_REGNUM, /* %r11 */
179 AMD64_R12_REGNUM, /* %r12 */
180 AMD64_R13_REGNUM, /* %r13 */
181 AMD64_R14_REGNUM, /* %r14 */
182 AMD64_R15_REGNUM, /* %r15 */
183
184 /* Return Address RA. Mapped to RIP. */
185 AMD64_RIP_REGNUM,
186
187 /* SSE Registers 0 - 7. */
188 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
189 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
190 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
191 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
192
193 /* Extended SSE Registers 8 - 15. */
194 AMD64_XMM0_REGNUM + 8, AMD64_XMM0_REGNUM + 9,
195 AMD64_XMM0_REGNUM + 10, AMD64_XMM0_REGNUM + 11,
196 AMD64_XMM0_REGNUM + 12, AMD64_XMM0_REGNUM + 13,
197 AMD64_XMM0_REGNUM + 14, AMD64_XMM0_REGNUM + 15,
198
199 /* Floating Point Registers 0-7. */
200 AMD64_ST0_REGNUM + 0, AMD64_ST0_REGNUM + 1,
201 AMD64_ST0_REGNUM + 2, AMD64_ST0_REGNUM + 3,
202 AMD64_ST0_REGNUM + 4, AMD64_ST0_REGNUM + 5,
203 AMD64_ST0_REGNUM + 6, AMD64_ST0_REGNUM + 7,
204
205 /* MMX Registers 0 - 7.
206 We have to handle those registers specifically, as their register
207 number within GDB depends on the target (or they may even not be
208 available at all). */
209 -1, -1, -1, -1, -1, -1, -1, -1,
210
211 /* Control and Status Flags Register. */
212 AMD64_EFLAGS_REGNUM,
213
214 /* Selector Registers. */
215 AMD64_ES_REGNUM,
216 AMD64_CS_REGNUM,
217 AMD64_SS_REGNUM,
218 AMD64_DS_REGNUM,
219 AMD64_FS_REGNUM,
220 AMD64_GS_REGNUM,
221 -1,
222 -1,
223
224 /* Segment Base Address Registers. */
225 -1,
226 -1,
227 -1,
228 -1,
229
230 /* Special Selector Registers. */
231 -1,
232 -1,
233
234 /* Floating Point Control Registers. */
235 AMD64_MXCSR_REGNUM,
236 AMD64_FCTRL_REGNUM,
237 AMD64_FSTAT_REGNUM
238 };
239
240 static const int amd64_dwarf_regmap_len =
241 (sizeof (amd64_dwarf_regmap) / sizeof (amd64_dwarf_regmap[0]));
242
243 /* Convert DWARF register number REG to the appropriate register
244 number used by GDB. */
245
246 static int
247 amd64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
248 {
249 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
250 int ymm0_regnum = tdep->ymm0_regnum;
251 int regnum = -1;
252
253 if (reg >= 0 && reg < amd64_dwarf_regmap_len)
254 regnum = amd64_dwarf_regmap[reg];
255
256 if (ymm0_regnum >= 0
257 && i386_xmm_regnum_p (gdbarch, regnum))
258 regnum += ymm0_regnum - I387_XMM0_REGNUM (tdep);
259
260 return regnum;
261 }
262
263 /* Map architectural register numbers to gdb register numbers. */
264
265 static const int amd64_arch_regmap[16] =
266 {
267 AMD64_RAX_REGNUM, /* %rax */
268 AMD64_RCX_REGNUM, /* %rcx */
269 AMD64_RDX_REGNUM, /* %rdx */
270 AMD64_RBX_REGNUM, /* %rbx */
271 AMD64_RSP_REGNUM, /* %rsp */
272 AMD64_RBP_REGNUM, /* %rbp */
273 AMD64_RSI_REGNUM, /* %rsi */
274 AMD64_RDI_REGNUM, /* %rdi */
275 AMD64_R8_REGNUM, /* %r8 */
276 AMD64_R9_REGNUM, /* %r9 */
277 AMD64_R10_REGNUM, /* %r10 */
278 AMD64_R11_REGNUM, /* %r11 */
279 AMD64_R12_REGNUM, /* %r12 */
280 AMD64_R13_REGNUM, /* %r13 */
281 AMD64_R14_REGNUM, /* %r14 */
282 AMD64_R15_REGNUM /* %r15 */
283 };
284
285 static const int amd64_arch_regmap_len =
286 (sizeof (amd64_arch_regmap) / sizeof (amd64_arch_regmap[0]));
287
288 /* Convert architectural register number REG to the appropriate register
289 number used by GDB. */
290
291 static int
292 amd64_arch_reg_to_regnum (int reg)
293 {
294 gdb_assert (reg >= 0 && reg < amd64_arch_regmap_len);
295
296 return amd64_arch_regmap[reg];
297 }
298
299 /* Register names for byte pseudo-registers. */
300
301 static const char *amd64_byte_names[] =
302 {
303 "al", "bl", "cl", "dl", "sil", "dil", "bpl", "spl",
304 "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l",
305 "ah", "bh", "ch", "dh"
306 };
307
308 /* Number of lower byte registers. */
309 #define AMD64_NUM_LOWER_BYTE_REGS 16
310
311 /* Register names for word pseudo-registers. */
312
313 static const char *amd64_word_names[] =
314 {
315 "ax", "bx", "cx", "dx", "si", "di", "bp", "",
316 "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
317 };
318
319 /* Register names for dword pseudo-registers. */
320
321 static const char *amd64_dword_names[] =
322 {
323 "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp",
324 "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
325 "eip"
326 };
327
328 /* Return the name of register REGNUM. */
329
330 static const char *
331 amd64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
332 {
333 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
334 if (i386_byte_regnum_p (gdbarch, regnum))
335 return amd64_byte_names[regnum - tdep->al_regnum];
336 else if (i386_zmm_regnum_p (gdbarch, regnum))
337 return amd64_zmm_names[regnum - tdep->zmm0_regnum];
338 else if (i386_ymm_regnum_p (gdbarch, regnum))
339 return amd64_ymm_names[regnum - tdep->ymm0_regnum];
340 else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
341 return amd64_ymm_avx512_names[regnum - tdep->ymm16_regnum];
342 else if (i386_word_regnum_p (gdbarch, regnum))
343 return amd64_word_names[regnum - tdep->ax_regnum];
344 else if (i386_dword_regnum_p (gdbarch, regnum))
345 return amd64_dword_names[regnum - tdep->eax_regnum];
346 else
347 return i386_pseudo_register_name (gdbarch, regnum);
348 }
349
350 static struct value *
351 amd64_pseudo_register_read_value (struct gdbarch *gdbarch,
352 readable_regcache *regcache,
353 int regnum)
354 {
355 gdb_byte *raw_buf = (gdb_byte *) alloca (register_size (gdbarch, regnum));
356 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
357 enum register_status status;
358 struct value *result_value;
359 gdb_byte *buf;
360
361 result_value = allocate_value (register_type (gdbarch, regnum));
362 VALUE_LVAL (result_value) = lval_register;
363 VALUE_REGNUM (result_value) = regnum;
364 buf = value_contents_raw (result_value);
365
366 if (i386_byte_regnum_p (gdbarch, regnum))
367 {
368 int gpnum = regnum - tdep->al_regnum;
369
370 /* Extract (always little endian). */
371 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
372 {
373 /* Special handling for AH, BH, CH, DH. */
374 status = regcache->raw_read (gpnum - AMD64_NUM_LOWER_BYTE_REGS,
375 raw_buf);
376 if (status == REG_VALID)
377 memcpy (buf, raw_buf + 1, 1);
378 else
379 mark_value_bytes_unavailable (result_value, 0,
380 TYPE_LENGTH (value_type (result_value)));
381 }
382 else
383 {
384 status = regcache->raw_read (gpnum, raw_buf);
385 if (status == REG_VALID)
386 memcpy (buf, raw_buf, 1);
387 else
388 mark_value_bytes_unavailable (result_value, 0,
389 TYPE_LENGTH (value_type (result_value)));
390 }
391 }
392 else if (i386_dword_regnum_p (gdbarch, regnum))
393 {
394 int gpnum = regnum - tdep->eax_regnum;
395 /* Extract (always little endian). */
396 status = regcache->raw_read (gpnum, raw_buf);
397 if (status == REG_VALID)
398 memcpy (buf, raw_buf, 4);
399 else
400 mark_value_bytes_unavailable (result_value, 0,
401 TYPE_LENGTH (value_type (result_value)));
402 }
403 else
404 i386_pseudo_register_read_into_value (gdbarch, regcache, regnum,
405 result_value);
406
407 return result_value;
408 }
409
410 static void
411 amd64_pseudo_register_write (struct gdbarch *gdbarch,
412 struct regcache *regcache,
413 int regnum, const gdb_byte *buf)
414 {
415 gdb_byte *raw_buf = (gdb_byte *) alloca (register_size (gdbarch, regnum));
416 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
417
418 if (i386_byte_regnum_p (gdbarch, regnum))
419 {
420 int gpnum = regnum - tdep->al_regnum;
421
422 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
423 {
424 /* Read ... AH, BH, CH, DH. */
425 regcache_raw_read (regcache,
426 gpnum - AMD64_NUM_LOWER_BYTE_REGS, raw_buf);
427 /* ... Modify ... (always little endian). */
428 memcpy (raw_buf + 1, buf, 1);
429 /* ... Write. */
430 regcache_raw_write (regcache,
431 gpnum - AMD64_NUM_LOWER_BYTE_REGS, raw_buf);
432 }
433 else
434 {
435 /* Read ... */
436 regcache_raw_read (regcache, gpnum, raw_buf);
437 /* ... Modify ... (always little endian). */
438 memcpy (raw_buf, buf, 1);
439 /* ... Write. */
440 regcache_raw_write (regcache, gpnum, raw_buf);
441 }
442 }
443 else if (i386_dword_regnum_p (gdbarch, regnum))
444 {
445 int gpnum = regnum - tdep->eax_regnum;
446
447 /* Read ... */
448 regcache_raw_read (regcache, gpnum, raw_buf);
449 /* ... Modify ... (always little endian). */
450 memcpy (raw_buf, buf, 4);
451 /* ... Write. */
452 regcache_raw_write (regcache, gpnum, raw_buf);
453 }
454 else
455 i386_pseudo_register_write (gdbarch, regcache, regnum, buf);
456 }
457
458 /* Implement the 'ax_pseudo_register_collect' gdbarch method. */
459
460 static int
461 amd64_ax_pseudo_register_collect (struct gdbarch *gdbarch,
462 struct agent_expr *ax, int regnum)
463 {
464 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
465
466 if (i386_byte_regnum_p (gdbarch, regnum))
467 {
468 int gpnum = regnum - tdep->al_regnum;
469
470 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
471 ax_reg_mask (ax, gpnum - AMD64_NUM_LOWER_BYTE_REGS);
472 else
473 ax_reg_mask (ax, gpnum);
474 return 0;
475 }
476 else if (i386_dword_regnum_p (gdbarch, regnum))
477 {
478 int gpnum = regnum - tdep->eax_regnum;
479
480 ax_reg_mask (ax, gpnum);
481 return 0;
482 }
483 else
484 return i386_ax_pseudo_register_collect (gdbarch, ax, regnum);
485 }
486
487 \f
488
489 /* Register classes as defined in the psABI. */
490
491 enum amd64_reg_class
492 {
493 AMD64_INTEGER,
494 AMD64_SSE,
495 AMD64_SSEUP,
496 AMD64_X87,
497 AMD64_X87UP,
498 AMD64_COMPLEX_X87,
499 AMD64_NO_CLASS,
500 AMD64_MEMORY
501 };
502
503 /* Return the union class of CLASS1 and CLASS2. See the psABI for
504 details. */
505
506 static enum amd64_reg_class
507 amd64_merge_classes (enum amd64_reg_class class1, enum amd64_reg_class class2)
508 {
509 /* Rule (a): If both classes are equal, this is the resulting class. */
510 if (class1 == class2)
511 return class1;
512
513 /* Rule (b): If one of the classes is NO_CLASS, the resulting class
514 is the other class. */
515 if (class1 == AMD64_NO_CLASS)
516 return class2;
517 if (class2 == AMD64_NO_CLASS)
518 return class1;
519
520 /* Rule (c): If one of the classes is MEMORY, the result is MEMORY. */
521 if (class1 == AMD64_MEMORY || class2 == AMD64_MEMORY)
522 return AMD64_MEMORY;
523
524 /* Rule (d): If one of the classes is INTEGER, the result is INTEGER. */
525 if (class1 == AMD64_INTEGER || class2 == AMD64_INTEGER)
526 return AMD64_INTEGER;
527
528 /* Rule (e): If one of the classes is X87, X87UP, COMPLEX_X87 class,
529 MEMORY is used as class. */
530 if (class1 == AMD64_X87 || class1 == AMD64_X87UP
531 || class1 == AMD64_COMPLEX_X87 || class2 == AMD64_X87
532 || class2 == AMD64_X87UP || class2 == AMD64_COMPLEX_X87)
533 return AMD64_MEMORY;
534
535 /* Rule (f): Otherwise class SSE is used. */
536 return AMD64_SSE;
537 }
538
539 static void amd64_classify (struct type *type, enum amd64_reg_class theclass[2]);
540
541 /* Return non-zero if TYPE is a non-POD structure or union type. */
542
543 static int
544 amd64_non_pod_p (struct type *type)
545 {
546 /* ??? A class with a base class certainly isn't POD, but does this
547 catch all non-POD structure types? */
548 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_N_BASECLASSES (type) > 0)
549 return 1;
550
551 return 0;
552 }
553
554 /* Classify TYPE according to the rules for aggregate (structures and
555 arrays) and union types, and store the result in CLASS. */
556
557 static void
558 amd64_classify_aggregate (struct type *type, enum amd64_reg_class theclass[2])
559 {
560 /* 1. If the size of an object is larger than two eightbytes, or in
561 C++, is a non-POD structure or union type, or contains
562 unaligned fields, it has class memory. */
563 if (TYPE_LENGTH (type) > 16 || amd64_non_pod_p (type))
564 {
565 theclass[0] = theclass[1] = AMD64_MEMORY;
566 return;
567 }
568
569 /* 2. Both eightbytes get initialized to class NO_CLASS. */
570 theclass[0] = theclass[1] = AMD64_NO_CLASS;
571
572 /* 3. Each field of an object is classified recursively so that
573 always two fields are considered. The resulting class is
574 calculated according to the classes of the fields in the
575 eightbyte: */
576
577 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
578 {
579 struct type *subtype = check_typedef (TYPE_TARGET_TYPE (type));
580
581 /* All fields in an array have the same type. */
582 amd64_classify (subtype, theclass);
583 if (TYPE_LENGTH (type) > 8 && theclass[1] == AMD64_NO_CLASS)
584 theclass[1] = theclass[0];
585 }
586 else
587 {
588 int i;
589
590 /* Structure or union. */
591 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
592 || TYPE_CODE (type) == TYPE_CODE_UNION);
593
594 for (i = 0; i < TYPE_NFIELDS (type); i++)
595 {
596 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
597 int pos = TYPE_FIELD_BITPOS (type, i) / 64;
598 enum amd64_reg_class subclass[2];
599 int bitsize = TYPE_FIELD_BITSIZE (type, i);
600 int endpos;
601
602 if (bitsize == 0)
603 bitsize = TYPE_LENGTH (subtype) * 8;
604 endpos = (TYPE_FIELD_BITPOS (type, i) + bitsize - 1) / 64;
605
606 /* Ignore static fields, or empty fields, for example nested
607 empty structures.*/
608 if (field_is_static (&TYPE_FIELD (type, i)) || bitsize == 0)
609 continue;
610
611 gdb_assert (pos == 0 || pos == 1);
612
613 amd64_classify (subtype, subclass);
614 theclass[pos] = amd64_merge_classes (theclass[pos], subclass[0]);
615 if (bitsize <= 64 && pos == 0 && endpos == 1)
616 /* This is a bit of an odd case: We have a field that would
617 normally fit in one of the two eightbytes, except that
618 it is placed in a way that this field straddles them.
619 This has been seen with a structure containing an array.
620
621 The ABI is a bit unclear in this case, but we assume that
622 this field's class (stored in subclass[0]) must also be merged
623 into class[1]. In other words, our field has a piece stored
624 in the second eight-byte, and thus its class applies to
625 the second eight-byte as well.
626
627 In the case where the field length exceeds 8 bytes,
628 it should not be necessary to merge the field class
629 into class[1]. As LEN > 8, subclass[1] is necessarily
630 different from AMD64_NO_CLASS. If subclass[1] is equal
631 to subclass[0], then the normal class[1]/subclass[1]
632 merging will take care of everything. For subclass[1]
633 to be different from subclass[0], I can only see the case
634 where we have a SSE/SSEUP or X87/X87UP pair, which both
635 use up all 16 bytes of the aggregate, and are already
636 handled just fine (because each portion sits on its own
637 8-byte). */
638 theclass[1] = amd64_merge_classes (theclass[1], subclass[0]);
639 if (pos == 0)
640 theclass[1] = amd64_merge_classes (theclass[1], subclass[1]);
641 }
642 }
643
644 /* 4. Then a post merger cleanup is done: */
645
646 /* Rule (a): If one of the classes is MEMORY, the whole argument is
647 passed in memory. */
648 if (theclass[0] == AMD64_MEMORY || theclass[1] == AMD64_MEMORY)
649 theclass[0] = theclass[1] = AMD64_MEMORY;
650
651 /* Rule (b): If SSEUP is not preceded by SSE, it is converted to
652 SSE. */
653 if (theclass[0] == AMD64_SSEUP)
654 theclass[0] = AMD64_SSE;
655 if (theclass[1] == AMD64_SSEUP && theclass[0] != AMD64_SSE)
656 theclass[1] = AMD64_SSE;
657 }
658
659 /* Classify TYPE, and store the result in CLASS. */
660
661 static void
662 amd64_classify (struct type *type, enum amd64_reg_class theclass[2])
663 {
664 enum type_code code = TYPE_CODE (type);
665 int len = TYPE_LENGTH (type);
666
667 theclass[0] = theclass[1] = AMD64_NO_CLASS;
668
669 /* Arguments of types (signed and unsigned) _Bool, char, short, int,
670 long, long long, and pointers are in the INTEGER class. Similarly,
671 range types, used by languages such as Ada, are also in the INTEGER
672 class. */
673 if ((code == TYPE_CODE_INT || code == TYPE_CODE_ENUM
674 || code == TYPE_CODE_BOOL || code == TYPE_CODE_RANGE
675 || code == TYPE_CODE_CHAR
676 || code == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type))
677 && (len == 1 || len == 2 || len == 4 || len == 8))
678 theclass[0] = AMD64_INTEGER;
679
680 /* Arguments of types float, double, _Decimal32, _Decimal64 and __m64
681 are in class SSE. */
682 else if ((code == TYPE_CODE_FLT || code == TYPE_CODE_DECFLOAT)
683 && (len == 4 || len == 8))
684 /* FIXME: __m64 . */
685 theclass[0] = AMD64_SSE;
686
687 /* Arguments of types __float128, _Decimal128 and __m128 are split into
688 two halves. The least significant ones belong to class SSE, the most
689 significant one to class SSEUP. */
690 else if (code == TYPE_CODE_DECFLOAT && len == 16)
691 /* FIXME: __float128, __m128. */
692 theclass[0] = AMD64_SSE, theclass[1] = AMD64_SSEUP;
693
694 /* The 64-bit mantissa of arguments of type long double belongs to
695 class X87, the 16-bit exponent plus 6 bytes of padding belongs to
696 class X87UP. */
697 else if (code == TYPE_CODE_FLT && len == 16)
698 /* Class X87 and X87UP. */
699 theclass[0] = AMD64_X87, theclass[1] = AMD64_X87UP;
700
701 /* Arguments of complex T where T is one of the types float or
702 double get treated as if they are implemented as:
703
704 struct complexT {
705 T real;
706 T imag;
707 };
708
709 */
710 else if (code == TYPE_CODE_COMPLEX && len == 8)
711 theclass[0] = AMD64_SSE;
712 else if (code == TYPE_CODE_COMPLEX && len == 16)
713 theclass[0] = theclass[1] = AMD64_SSE;
714
715 /* A variable of type complex long double is classified as type
716 COMPLEX_X87. */
717 else if (code == TYPE_CODE_COMPLEX && len == 32)
718 theclass[0] = AMD64_COMPLEX_X87;
719
720 /* Aggregates. */
721 else if (code == TYPE_CODE_ARRAY || code == TYPE_CODE_STRUCT
722 || code == TYPE_CODE_UNION)
723 amd64_classify_aggregate (type, theclass);
724 }
725
726 static enum return_value_convention
727 amd64_return_value (struct gdbarch *gdbarch, struct value *function,
728 struct type *type, struct regcache *regcache,
729 gdb_byte *readbuf, const gdb_byte *writebuf)
730 {
731 enum amd64_reg_class theclass[2];
732 int len = TYPE_LENGTH (type);
733 static int integer_regnum[] = { AMD64_RAX_REGNUM, AMD64_RDX_REGNUM };
734 static int sse_regnum[] = { AMD64_XMM0_REGNUM, AMD64_XMM1_REGNUM };
735 int integer_reg = 0;
736 int sse_reg = 0;
737 int i;
738
739 gdb_assert (!(readbuf && writebuf));
740
741 /* 1. Classify the return type with the classification algorithm. */
742 amd64_classify (type, theclass);
743
744 /* 2. If the type has class MEMORY, then the caller provides space
745 for the return value and passes the address of this storage in
746 %rdi as if it were the first argument to the function. In effect,
747 this address becomes a hidden first argument.
748
749 On return %rax will contain the address that has been passed in
750 by the caller in %rdi. */
751 if (theclass[0] == AMD64_MEMORY)
752 {
753 /* As indicated by the comment above, the ABI guarantees that we
754 can always find the return value just after the function has
755 returned. */
756
757 if (readbuf)
758 {
759 ULONGEST addr;
760
761 regcache_raw_read_unsigned (regcache, AMD64_RAX_REGNUM, &addr);
762 read_memory (addr, readbuf, TYPE_LENGTH (type));
763 }
764
765 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
766 }
767
768 /* 8. If the class is COMPLEX_X87, the real part of the value is
769 returned in %st0 and the imaginary part in %st1. */
770 if (theclass[0] == AMD64_COMPLEX_X87)
771 {
772 if (readbuf)
773 {
774 regcache_raw_read (regcache, AMD64_ST0_REGNUM, readbuf);
775 regcache_raw_read (regcache, AMD64_ST1_REGNUM, readbuf + 16);
776 }
777
778 if (writebuf)
779 {
780 i387_return_value (gdbarch, regcache);
781 regcache_raw_write (regcache, AMD64_ST0_REGNUM, writebuf);
782 regcache_raw_write (regcache, AMD64_ST1_REGNUM, writebuf + 16);
783
784 /* Fix up the tag word such that both %st(0) and %st(1) are
785 marked as valid. */
786 regcache_raw_write_unsigned (regcache, AMD64_FTAG_REGNUM, 0xfff);
787 }
788
789 return RETURN_VALUE_REGISTER_CONVENTION;
790 }
791
792 gdb_assert (theclass[1] != AMD64_MEMORY);
793 gdb_assert (len <= 16);
794
795 for (i = 0; len > 0; i++, len -= 8)
796 {
797 int regnum = -1;
798 int offset = 0;
799
800 switch (theclass[i])
801 {
802 case AMD64_INTEGER:
803 /* 3. If the class is INTEGER, the next available register
804 of the sequence %rax, %rdx is used. */
805 regnum = integer_regnum[integer_reg++];
806 break;
807
808 case AMD64_SSE:
809 /* 4. If the class is SSE, the next available SSE register
810 of the sequence %xmm0, %xmm1 is used. */
811 regnum = sse_regnum[sse_reg++];
812 break;
813
814 case AMD64_SSEUP:
815 /* 5. If the class is SSEUP, the eightbyte is passed in the
816 upper half of the last used SSE register. */
817 gdb_assert (sse_reg > 0);
818 regnum = sse_regnum[sse_reg - 1];
819 offset = 8;
820 break;
821
822 case AMD64_X87:
823 /* 6. If the class is X87, the value is returned on the X87
824 stack in %st0 as 80-bit x87 number. */
825 regnum = AMD64_ST0_REGNUM;
826 if (writebuf)
827 i387_return_value (gdbarch, regcache);
828 break;
829
830 case AMD64_X87UP:
831 /* 7. If the class is X87UP, the value is returned together
832 with the previous X87 value in %st0. */
833 gdb_assert (i > 0 && theclass[0] == AMD64_X87);
834 regnum = AMD64_ST0_REGNUM;
835 offset = 8;
836 len = 2;
837 break;
838
839 case AMD64_NO_CLASS:
840 continue;
841
842 default:
843 gdb_assert (!"Unexpected register class.");
844 }
845
846 gdb_assert (regnum != -1);
847
848 if (readbuf)
849 regcache_raw_read_part (regcache, regnum, offset, std::min (len, 8),
850 readbuf + i * 8);
851 if (writebuf)
852 regcache_raw_write_part (regcache, regnum, offset, std::min (len, 8),
853 writebuf + i * 8);
854 }
855
856 return RETURN_VALUE_REGISTER_CONVENTION;
857 }
858 \f
859
860 static CORE_ADDR
861 amd64_push_arguments (struct regcache *regcache, int nargs,
862 struct value **args, CORE_ADDR sp, int struct_return)
863 {
864 static int integer_regnum[] =
865 {
866 AMD64_RDI_REGNUM, /* %rdi */
867 AMD64_RSI_REGNUM, /* %rsi */
868 AMD64_RDX_REGNUM, /* %rdx */
869 AMD64_RCX_REGNUM, /* %rcx */
870 AMD64_R8_REGNUM, /* %r8 */
871 AMD64_R9_REGNUM /* %r9 */
872 };
873 static int sse_regnum[] =
874 {
875 /* %xmm0 ... %xmm7 */
876 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
877 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
878 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
879 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
880 };
881 struct value **stack_args = XALLOCAVEC (struct value *, nargs);
882 int num_stack_args = 0;
883 int num_elements = 0;
884 int element = 0;
885 int integer_reg = 0;
886 int sse_reg = 0;
887 int i;
888
889 /* Reserve a register for the "hidden" argument. */
890 if (struct_return)
891 integer_reg++;
892
893 for (i = 0; i < nargs; i++)
894 {
895 struct type *type = value_type (args[i]);
896 int len = TYPE_LENGTH (type);
897 enum amd64_reg_class theclass[2];
898 int needed_integer_regs = 0;
899 int needed_sse_regs = 0;
900 int j;
901
902 /* Classify argument. */
903 amd64_classify (type, theclass);
904
905 /* Calculate the number of integer and SSE registers needed for
906 this argument. */
907 for (j = 0; j < 2; j++)
908 {
909 if (theclass[j] == AMD64_INTEGER)
910 needed_integer_regs++;
911 else if (theclass[j] == AMD64_SSE)
912 needed_sse_regs++;
913 }
914
915 /* Check whether enough registers are available, and if the
916 argument should be passed in registers at all. */
917 if (integer_reg + needed_integer_regs > ARRAY_SIZE (integer_regnum)
918 || sse_reg + needed_sse_regs > ARRAY_SIZE (sse_regnum)
919 || (needed_integer_regs == 0 && needed_sse_regs == 0))
920 {
921 /* The argument will be passed on the stack. */
922 num_elements += ((len + 7) / 8);
923 stack_args[num_stack_args++] = args[i];
924 }
925 else
926 {
927 /* The argument will be passed in registers. */
928 const gdb_byte *valbuf = value_contents (args[i]);
929 gdb_byte buf[8];
930
931 gdb_assert (len <= 16);
932
933 for (j = 0; len > 0; j++, len -= 8)
934 {
935 int regnum = -1;
936 int offset = 0;
937
938 switch (theclass[j])
939 {
940 case AMD64_INTEGER:
941 regnum = integer_regnum[integer_reg++];
942 break;
943
944 case AMD64_SSE:
945 regnum = sse_regnum[sse_reg++];
946 break;
947
948 case AMD64_SSEUP:
949 gdb_assert (sse_reg > 0);
950 regnum = sse_regnum[sse_reg - 1];
951 offset = 8;
952 break;
953
954 default:
955 gdb_assert (!"Unexpected register class.");
956 }
957
958 gdb_assert (regnum != -1);
959 memset (buf, 0, sizeof buf);
960 memcpy (buf, valbuf + j * 8, std::min (len, 8));
961 regcache_raw_write_part (regcache, regnum, offset, 8, buf);
962 }
963 }
964 }
965
966 /* Allocate space for the arguments on the stack. */
967 sp -= num_elements * 8;
968
969 /* The psABI says that "The end of the input argument area shall be
970 aligned on a 16 byte boundary." */
971 sp &= ~0xf;
972
973 /* Write out the arguments to the stack. */
974 for (i = 0; i < num_stack_args; i++)
975 {
976 struct type *type = value_type (stack_args[i]);
977 const gdb_byte *valbuf = value_contents (stack_args[i]);
978 int len = TYPE_LENGTH (type);
979
980 write_memory (sp + element * 8, valbuf, len);
981 element += ((len + 7) / 8);
982 }
983
984 /* The psABI says that "For calls that may call functions that use
985 varargs or stdargs (prototype-less calls or calls to functions
986 containing ellipsis (...) in the declaration) %al is used as
987 hidden argument to specify the number of SSE registers used. */
988 regcache_raw_write_unsigned (regcache, AMD64_RAX_REGNUM, sse_reg);
989 return sp;
990 }
991
992 static CORE_ADDR
993 amd64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
994 struct regcache *regcache, CORE_ADDR bp_addr,
995 int nargs, struct value **args, CORE_ADDR sp,
996 int struct_return, CORE_ADDR struct_addr)
997 {
998 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
999 gdb_byte buf[8];
1000
1001 /* BND registers can be in arbitrary values at the moment of the
1002 inferior call. This can cause boundary violations that are not
1003 due to a real bug or even desired by the user. The best to be done
1004 is set the BND registers to allow access to the whole memory, INIT
1005 state, before pushing the inferior call. */
1006 i387_reset_bnd_regs (gdbarch, regcache);
1007
1008 /* Pass arguments. */
1009 sp = amd64_push_arguments (regcache, nargs, args, sp, struct_return);
1010
1011 /* Pass "hidden" argument". */
1012 if (struct_return)
1013 {
1014 store_unsigned_integer (buf, 8, byte_order, struct_addr);
1015 regcache_cooked_write (regcache, AMD64_RDI_REGNUM, buf);
1016 }
1017
1018 /* Store return address. */
1019 sp -= 8;
1020 store_unsigned_integer (buf, 8, byte_order, bp_addr);
1021 write_memory (sp, buf, 8);
1022
1023 /* Finally, update the stack pointer... */
1024 store_unsigned_integer (buf, 8, byte_order, sp);
1025 regcache_cooked_write (regcache, AMD64_RSP_REGNUM, buf);
1026
1027 /* ...and fake a frame pointer. */
1028 regcache_cooked_write (regcache, AMD64_RBP_REGNUM, buf);
1029
1030 return sp + 16;
1031 }
1032 \f
1033 /* Displaced instruction handling. */
1034
1035 /* A partially decoded instruction.
1036 This contains enough details for displaced stepping purposes. */
1037
1038 struct amd64_insn
1039 {
1040 /* The number of opcode bytes. */
1041 int opcode_len;
1042 /* The offset of the REX/VEX instruction encoding prefix or -1 if
1043 not present. */
1044 int enc_prefix_offset;
1045 /* The offset to the first opcode byte. */
1046 int opcode_offset;
1047 /* The offset to the modrm byte or -1 if not present. */
1048 int modrm_offset;
1049
1050 /* The raw instruction. */
1051 gdb_byte *raw_insn;
1052 };
1053
1054 struct amd64_displaced_step_closure : public displaced_step_closure
1055 {
1056 amd64_displaced_step_closure (int insn_buf_len)
1057 : insn_buf (insn_buf_len, 0)
1058 {}
1059
1060 /* For rip-relative insns, saved copy of the reg we use instead of %rip. */
1061 int tmp_used = 0;
1062 int tmp_regno;
1063 ULONGEST tmp_save;
1064
1065 /* Details of the instruction. */
1066 struct amd64_insn insn_details;
1067
1068 /* The possibly modified insn. */
1069 gdb::byte_vector insn_buf;
1070 };
1071
1072 /* WARNING: Keep onebyte_has_modrm, twobyte_has_modrm in sync with
1073 ../opcodes/i386-dis.c (until libopcodes exports them, or an alternative,
1074 at which point delete these in favor of libopcodes' versions). */
1075
1076 static const unsigned char onebyte_has_modrm[256] = {
1077 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1078 /* ------------------------------- */
1079 /* 00 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 00 */
1080 /* 10 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 10 */
1081 /* 20 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 20 */
1082 /* 30 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 30 */
1083 /* 40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 40 */
1084 /* 50 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 50 */
1085 /* 60 */ 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0, /* 60 */
1086 /* 70 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 70 */
1087 /* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 80 */
1088 /* 90 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 90 */
1089 /* a0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* a0 */
1090 /* b0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* b0 */
1091 /* c0 */ 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0, /* c0 */
1092 /* d0 */ 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1, /* d0 */
1093 /* e0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* e0 */
1094 /* f0 */ 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1 /* f0 */
1095 /* ------------------------------- */
1096 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1097 };
1098
1099 static const unsigned char twobyte_has_modrm[256] = {
1100 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1101 /* ------------------------------- */
1102 /* 00 */ 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1, /* 0f */
1103 /* 10 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 1f */
1104 /* 20 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 2f */
1105 /* 30 */ 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0, /* 3f */
1106 /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 4f */
1107 /* 50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 5f */
1108 /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 6f */
1109 /* 70 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 7f */
1110 /* 80 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 8f */
1111 /* 90 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 9f */
1112 /* a0 */ 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1, /* af */
1113 /* b0 */ 1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1, /* bf */
1114 /* c0 */ 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, /* cf */
1115 /* d0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* df */
1116 /* e0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ef */
1117 /* f0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0 /* ff */
1118 /* ------------------------------- */
1119 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1120 };
1121
1122 static int amd64_syscall_p (const struct amd64_insn *insn, int *lengthp);
1123
1124 static int
1125 rex_prefix_p (gdb_byte pfx)
1126 {
1127 return REX_PREFIX_P (pfx);
1128 }
1129
1130 /* True if PFX is the start of the 2-byte VEX prefix. */
1131
1132 static bool
1133 vex2_prefix_p (gdb_byte pfx)
1134 {
1135 return pfx == 0xc5;
1136 }
1137
1138 /* True if PFX is the start of the 3-byte VEX prefix. */
1139
1140 static bool
1141 vex3_prefix_p (gdb_byte pfx)
1142 {
1143 return pfx == 0xc4;
1144 }
1145
1146 /* Skip the legacy instruction prefixes in INSN.
1147 We assume INSN is properly sentineled so we don't have to worry
1148 about falling off the end of the buffer. */
1149
1150 static gdb_byte *
1151 amd64_skip_prefixes (gdb_byte *insn)
1152 {
1153 while (1)
1154 {
1155 switch (*insn)
1156 {
1157 case DATA_PREFIX_OPCODE:
1158 case ADDR_PREFIX_OPCODE:
1159 case CS_PREFIX_OPCODE:
1160 case DS_PREFIX_OPCODE:
1161 case ES_PREFIX_OPCODE:
1162 case FS_PREFIX_OPCODE:
1163 case GS_PREFIX_OPCODE:
1164 case SS_PREFIX_OPCODE:
1165 case LOCK_PREFIX_OPCODE:
1166 case REPE_PREFIX_OPCODE:
1167 case REPNE_PREFIX_OPCODE:
1168 ++insn;
1169 continue;
1170 default:
1171 break;
1172 }
1173 break;
1174 }
1175
1176 return insn;
1177 }
1178
1179 /* Return an integer register (other than RSP) that is unused as an input
1180 operand in INSN.
1181 In order to not require adding a rex prefix if the insn doesn't already
1182 have one, the result is restricted to RAX ... RDI, sans RSP.
1183 The register numbering of the result follows architecture ordering,
1184 e.g. RDI = 7. */
1185
1186 static int
1187 amd64_get_unused_input_int_reg (const struct amd64_insn *details)
1188 {
1189 /* 1 bit for each reg */
1190 int used_regs_mask = 0;
1191
1192 /* There can be at most 3 int regs used as inputs in an insn, and we have
1193 7 to choose from (RAX ... RDI, sans RSP).
1194 This allows us to take a conservative approach and keep things simple.
1195 E.g. By avoiding RAX, we don't have to specifically watch for opcodes
1196 that implicitly specify RAX. */
1197
1198 /* Avoid RAX. */
1199 used_regs_mask |= 1 << EAX_REG_NUM;
1200 /* Similarily avoid RDX, implicit operand in divides. */
1201 used_regs_mask |= 1 << EDX_REG_NUM;
1202 /* Avoid RSP. */
1203 used_regs_mask |= 1 << ESP_REG_NUM;
1204
1205 /* If the opcode is one byte long and there's no ModRM byte,
1206 assume the opcode specifies a register. */
1207 if (details->opcode_len == 1 && details->modrm_offset == -1)
1208 used_regs_mask |= 1 << (details->raw_insn[details->opcode_offset] & 7);
1209
1210 /* Mark used regs in the modrm/sib bytes. */
1211 if (details->modrm_offset != -1)
1212 {
1213 int modrm = details->raw_insn[details->modrm_offset];
1214 int mod = MODRM_MOD_FIELD (modrm);
1215 int reg = MODRM_REG_FIELD (modrm);
1216 int rm = MODRM_RM_FIELD (modrm);
1217 int have_sib = mod != 3 && rm == 4;
1218
1219 /* Assume the reg field of the modrm byte specifies a register. */
1220 used_regs_mask |= 1 << reg;
1221
1222 if (have_sib)
1223 {
1224 int base = SIB_BASE_FIELD (details->raw_insn[details->modrm_offset + 1]);
1225 int idx = SIB_INDEX_FIELD (details->raw_insn[details->modrm_offset + 1]);
1226 used_regs_mask |= 1 << base;
1227 used_regs_mask |= 1 << idx;
1228 }
1229 else
1230 {
1231 used_regs_mask |= 1 << rm;
1232 }
1233 }
1234
1235 gdb_assert (used_regs_mask < 256);
1236 gdb_assert (used_regs_mask != 255);
1237
1238 /* Finally, find a free reg. */
1239 {
1240 int i;
1241
1242 for (i = 0; i < 8; ++i)
1243 {
1244 if (! (used_regs_mask & (1 << i)))
1245 return i;
1246 }
1247
1248 /* We shouldn't get here. */
1249 internal_error (__FILE__, __LINE__, _("unable to find free reg"));
1250 }
1251 }
1252
1253 /* Extract the details of INSN that we need. */
1254
1255 static void
1256 amd64_get_insn_details (gdb_byte *insn, struct amd64_insn *details)
1257 {
1258 gdb_byte *start = insn;
1259 int need_modrm;
1260
1261 details->raw_insn = insn;
1262
1263 details->opcode_len = -1;
1264 details->enc_prefix_offset = -1;
1265 details->opcode_offset = -1;
1266 details->modrm_offset = -1;
1267
1268 /* Skip legacy instruction prefixes. */
1269 insn = amd64_skip_prefixes (insn);
1270
1271 /* Skip REX/VEX instruction encoding prefixes. */
1272 if (rex_prefix_p (*insn))
1273 {
1274 details->enc_prefix_offset = insn - start;
1275 ++insn;
1276 }
1277 else if (vex2_prefix_p (*insn))
1278 {
1279 /* Don't record the offset in this case because this prefix has
1280 no REX.B equivalent. */
1281 insn += 2;
1282 }
1283 else if (vex3_prefix_p (*insn))
1284 {
1285 details->enc_prefix_offset = insn - start;
1286 insn += 3;
1287 }
1288
1289 details->opcode_offset = insn - start;
1290
1291 if (*insn == TWO_BYTE_OPCODE_ESCAPE)
1292 {
1293 /* Two or three-byte opcode. */
1294 ++insn;
1295 need_modrm = twobyte_has_modrm[*insn];
1296
1297 /* Check for three-byte opcode. */
1298 switch (*insn)
1299 {
1300 case 0x24:
1301 case 0x25:
1302 case 0x38:
1303 case 0x3a:
1304 case 0x7a:
1305 case 0x7b:
1306 ++insn;
1307 details->opcode_len = 3;
1308 break;
1309 default:
1310 details->opcode_len = 2;
1311 break;
1312 }
1313 }
1314 else
1315 {
1316 /* One-byte opcode. */
1317 need_modrm = onebyte_has_modrm[*insn];
1318 details->opcode_len = 1;
1319 }
1320
1321 if (need_modrm)
1322 {
1323 ++insn;
1324 details->modrm_offset = insn - start;
1325 }
1326 }
1327
1328 /* Update %rip-relative addressing in INSN.
1329
1330 %rip-relative addressing only uses a 32-bit displacement.
1331 32 bits is not enough to be guaranteed to cover the distance between where
1332 the real instruction is and where its copy is.
1333 Convert the insn to use base+disp addressing.
1334 We set base = pc + insn_length so we can leave disp unchanged. */
1335
1336 static void
1337 fixup_riprel (struct gdbarch *gdbarch, amd64_displaced_step_closure *dsc,
1338 CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
1339 {
1340 const struct amd64_insn *insn_details = &dsc->insn_details;
1341 int modrm_offset = insn_details->modrm_offset;
1342 gdb_byte *insn = insn_details->raw_insn + modrm_offset;
1343 CORE_ADDR rip_base;
1344 int insn_length;
1345 int arch_tmp_regno, tmp_regno;
1346 ULONGEST orig_value;
1347
1348 /* %rip+disp32 addressing mode, displacement follows ModRM byte. */
1349 ++insn;
1350
1351 /* Compute the rip-relative address. */
1352 insn_length = gdb_buffered_insn_length (gdbarch, dsc->insn_buf.data (),
1353 dsc->insn_buf.size (), from);
1354 rip_base = from + insn_length;
1355
1356 /* We need a register to hold the address.
1357 Pick one not used in the insn.
1358 NOTE: arch_tmp_regno uses architecture ordering, e.g. RDI = 7. */
1359 arch_tmp_regno = amd64_get_unused_input_int_reg (insn_details);
1360 tmp_regno = amd64_arch_reg_to_regnum (arch_tmp_regno);
1361
1362 /* Position of the not-B bit in the 3-byte VEX prefix (in byte 1). */
1363 static constexpr gdb_byte VEX3_NOT_B = 0x20;
1364
1365 /* REX.B should be unset (VEX.!B set) as we were using rip-relative
1366 addressing, but ensure it's unset (set for VEX) anyway, tmp_regno
1367 is not r8-r15. */
1368 if (insn_details->enc_prefix_offset != -1)
1369 {
1370 gdb_byte *pfx = &dsc->insn_buf[insn_details->enc_prefix_offset];
1371 if (rex_prefix_p (pfx[0]))
1372 pfx[0] &= ~REX_B;
1373 else if (vex3_prefix_p (pfx[0]))
1374 pfx[1] |= VEX3_NOT_B;
1375 else
1376 gdb_assert_not_reached ("unhandled prefix");
1377 }
1378
1379 regcache_cooked_read_unsigned (regs, tmp_regno, &orig_value);
1380 dsc->tmp_regno = tmp_regno;
1381 dsc->tmp_save = orig_value;
1382 dsc->tmp_used = 1;
1383
1384 /* Convert the ModRM field to be base+disp. */
1385 dsc->insn_buf[modrm_offset] &= ~0xc7;
1386 dsc->insn_buf[modrm_offset] |= 0x80 + arch_tmp_regno;
1387
1388 regcache_cooked_write_unsigned (regs, tmp_regno, rip_base);
1389
1390 if (debug_displaced)
1391 fprintf_unfiltered (gdb_stdlog, "displaced: %%rip-relative addressing used.\n"
1392 "displaced: using temp reg %d, old value %s, new value %s\n",
1393 dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save),
1394 paddress (gdbarch, rip_base));
1395 }
1396
1397 static void
1398 fixup_displaced_copy (struct gdbarch *gdbarch,
1399 amd64_displaced_step_closure *dsc,
1400 CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
1401 {
1402 const struct amd64_insn *details = &dsc->insn_details;
1403
1404 if (details->modrm_offset != -1)
1405 {
1406 gdb_byte modrm = details->raw_insn[details->modrm_offset];
1407
1408 if ((modrm & 0xc7) == 0x05)
1409 {
1410 /* The insn uses rip-relative addressing.
1411 Deal with it. */
1412 fixup_riprel (gdbarch, dsc, from, to, regs);
1413 }
1414 }
1415 }
1416
1417 struct displaced_step_closure *
1418 amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
1419 CORE_ADDR from, CORE_ADDR to,
1420 struct regcache *regs)
1421 {
1422 int len = gdbarch_max_insn_length (gdbarch);
1423 /* Extra space for sentinels so fixup_{riprel,displaced_copy} don't have to
1424 continually watch for running off the end of the buffer. */
1425 int fixup_sentinel_space = len;
1426 amd64_displaced_step_closure *dsc
1427 = new amd64_displaced_step_closure (len + fixup_sentinel_space);
1428 gdb_byte *buf = &dsc->insn_buf[0];
1429 struct amd64_insn *details = &dsc->insn_details;
1430
1431 read_memory (from, buf, len);
1432
1433 /* Set up the sentinel space so we don't have to worry about running
1434 off the end of the buffer. An excessive number of leading prefixes
1435 could otherwise cause this. */
1436 memset (buf + len, 0, fixup_sentinel_space);
1437
1438 amd64_get_insn_details (buf, details);
1439
1440 /* GDB may get control back after the insn after the syscall.
1441 Presumably this is a kernel bug.
1442 If this is a syscall, make sure there's a nop afterwards. */
1443 {
1444 int syscall_length;
1445
1446 if (amd64_syscall_p (details, &syscall_length))
1447 buf[details->opcode_offset + syscall_length] = NOP_OPCODE;
1448 }
1449
1450 /* Modify the insn to cope with the address where it will be executed from.
1451 In particular, handle any rip-relative addressing. */
1452 fixup_displaced_copy (gdbarch, dsc, from, to, regs);
1453
1454 write_memory (to, buf, len);
1455
1456 if (debug_displaced)
1457 {
1458 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
1459 paddress (gdbarch, from), paddress (gdbarch, to));
1460 displaced_step_dump_bytes (gdb_stdlog, buf, len);
1461 }
1462
1463 return dsc;
1464 }
1465
1466 static int
1467 amd64_absolute_jmp_p (const struct amd64_insn *details)
1468 {
1469 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1470
1471 if (insn[0] == 0xff)
1472 {
1473 /* jump near, absolute indirect (/4) */
1474 if ((insn[1] & 0x38) == 0x20)
1475 return 1;
1476
1477 /* jump far, absolute indirect (/5) */
1478 if ((insn[1] & 0x38) == 0x28)
1479 return 1;
1480 }
1481
1482 return 0;
1483 }
1484
1485 /* Return non-zero if the instruction DETAILS is a jump, zero otherwise. */
1486
1487 static int
1488 amd64_jmp_p (const struct amd64_insn *details)
1489 {
1490 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1491
1492 /* jump short, relative. */
1493 if (insn[0] == 0xeb)
1494 return 1;
1495
1496 /* jump near, relative. */
1497 if (insn[0] == 0xe9)
1498 return 1;
1499
1500 return amd64_absolute_jmp_p (details);
1501 }
1502
1503 static int
1504 amd64_absolute_call_p (const struct amd64_insn *details)
1505 {
1506 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1507
1508 if (insn[0] == 0xff)
1509 {
1510 /* Call near, absolute indirect (/2) */
1511 if ((insn[1] & 0x38) == 0x10)
1512 return 1;
1513
1514 /* Call far, absolute indirect (/3) */
1515 if ((insn[1] & 0x38) == 0x18)
1516 return 1;
1517 }
1518
1519 return 0;
1520 }
1521
1522 static int
1523 amd64_ret_p (const struct amd64_insn *details)
1524 {
1525 /* NOTE: gcc can emit "repz ; ret". */
1526 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1527
1528 switch (insn[0])
1529 {
1530 case 0xc2: /* ret near, pop N bytes */
1531 case 0xc3: /* ret near */
1532 case 0xca: /* ret far, pop N bytes */
1533 case 0xcb: /* ret far */
1534 case 0xcf: /* iret */
1535 return 1;
1536
1537 default:
1538 return 0;
1539 }
1540 }
1541
1542 static int
1543 amd64_call_p (const struct amd64_insn *details)
1544 {
1545 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1546
1547 if (amd64_absolute_call_p (details))
1548 return 1;
1549
1550 /* call near, relative */
1551 if (insn[0] == 0xe8)
1552 return 1;
1553
1554 return 0;
1555 }
1556
1557 /* Return non-zero if INSN is a system call, and set *LENGTHP to its
1558 length in bytes. Otherwise, return zero. */
1559
1560 static int
1561 amd64_syscall_p (const struct amd64_insn *details, int *lengthp)
1562 {
1563 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1564
1565 if (insn[0] == 0x0f && insn[1] == 0x05)
1566 {
1567 *lengthp = 2;
1568 return 1;
1569 }
1570
1571 return 0;
1572 }
1573
1574 /* Classify the instruction at ADDR using PRED.
1575 Throw an error if the memory can't be read. */
1576
1577 static int
1578 amd64_classify_insn_at (struct gdbarch *gdbarch, CORE_ADDR addr,
1579 int (*pred) (const struct amd64_insn *))
1580 {
1581 struct amd64_insn details;
1582 gdb_byte *buf;
1583 int len, classification;
1584
1585 len = gdbarch_max_insn_length (gdbarch);
1586 buf = (gdb_byte *) alloca (len);
1587
1588 read_code (addr, buf, len);
1589 amd64_get_insn_details (buf, &details);
1590
1591 classification = pred (&details);
1592
1593 return classification;
1594 }
1595
1596 /* The gdbarch insn_is_call method. */
1597
1598 static int
1599 amd64_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
1600 {
1601 return amd64_classify_insn_at (gdbarch, addr, amd64_call_p);
1602 }
1603
1604 /* The gdbarch insn_is_ret method. */
1605
1606 static int
1607 amd64_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
1608 {
1609 return amd64_classify_insn_at (gdbarch, addr, amd64_ret_p);
1610 }
1611
1612 /* The gdbarch insn_is_jump method. */
1613
1614 static int
1615 amd64_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
1616 {
1617 return amd64_classify_insn_at (gdbarch, addr, amd64_jmp_p);
1618 }
1619
1620 /* Fix up the state of registers and memory after having single-stepped
1621 a displaced instruction. */
1622
1623 void
1624 amd64_displaced_step_fixup (struct gdbarch *gdbarch,
1625 struct displaced_step_closure *dsc_,
1626 CORE_ADDR from, CORE_ADDR to,
1627 struct regcache *regs)
1628 {
1629 amd64_displaced_step_closure *dsc = (amd64_displaced_step_closure *) dsc_;
1630 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1631 /* The offset we applied to the instruction's address. */
1632 ULONGEST insn_offset = to - from;
1633 gdb_byte *insn = dsc->insn_buf.data ();
1634 const struct amd64_insn *insn_details = &dsc->insn_details;
1635
1636 if (debug_displaced)
1637 fprintf_unfiltered (gdb_stdlog,
1638 "displaced: fixup (%s, %s), "
1639 "insn = 0x%02x 0x%02x ...\n",
1640 paddress (gdbarch, from), paddress (gdbarch, to),
1641 insn[0], insn[1]);
1642
1643 /* If we used a tmp reg, restore it. */
1644
1645 if (dsc->tmp_used)
1646 {
1647 if (debug_displaced)
1648 fprintf_unfiltered (gdb_stdlog, "displaced: restoring reg %d to %s\n",
1649 dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save));
1650 regcache_cooked_write_unsigned (regs, dsc->tmp_regno, dsc->tmp_save);
1651 }
1652
1653 /* The list of issues to contend with here is taken from
1654 resume_execution in arch/x86/kernel/kprobes.c, Linux 2.6.28.
1655 Yay for Free Software! */
1656
1657 /* Relocate the %rip back to the program's instruction stream,
1658 if necessary. */
1659
1660 /* Except in the case of absolute or indirect jump or call
1661 instructions, or a return instruction, the new rip is relative to
1662 the displaced instruction; make it relative to the original insn.
1663 Well, signal handler returns don't need relocation either, but we use the
1664 value of %rip to recognize those; see below. */
1665 if (! amd64_absolute_jmp_p (insn_details)
1666 && ! amd64_absolute_call_p (insn_details)
1667 && ! amd64_ret_p (insn_details))
1668 {
1669 ULONGEST orig_rip;
1670 int insn_len;
1671
1672 regcache_cooked_read_unsigned (regs, AMD64_RIP_REGNUM, &orig_rip);
1673
1674 /* A signal trampoline system call changes the %rip, resuming
1675 execution of the main program after the signal handler has
1676 returned. That makes them like 'return' instructions; we
1677 shouldn't relocate %rip.
1678
1679 But most system calls don't, and we do need to relocate %rip.
1680
1681 Our heuristic for distinguishing these cases: if stepping
1682 over the system call instruction left control directly after
1683 the instruction, the we relocate --- control almost certainly
1684 doesn't belong in the displaced copy. Otherwise, we assume
1685 the instruction has put control where it belongs, and leave
1686 it unrelocated. Goodness help us if there are PC-relative
1687 system calls. */
1688 if (amd64_syscall_p (insn_details, &insn_len)
1689 && orig_rip != to + insn_len
1690 /* GDB can get control back after the insn after the syscall.
1691 Presumably this is a kernel bug.
1692 Fixup ensures its a nop, we add one to the length for it. */
1693 && orig_rip != to + insn_len + 1)
1694 {
1695 if (debug_displaced)
1696 fprintf_unfiltered (gdb_stdlog,
1697 "displaced: syscall changed %%rip; "
1698 "not relocating\n");
1699 }
1700 else
1701 {
1702 ULONGEST rip = orig_rip - insn_offset;
1703
1704 /* If we just stepped over a breakpoint insn, we don't backup
1705 the pc on purpose; this is to match behaviour without
1706 stepping. */
1707
1708 regcache_cooked_write_unsigned (regs, AMD64_RIP_REGNUM, rip);
1709
1710 if (debug_displaced)
1711 fprintf_unfiltered (gdb_stdlog,
1712 "displaced: "
1713 "relocated %%rip from %s to %s\n",
1714 paddress (gdbarch, orig_rip),
1715 paddress (gdbarch, rip));
1716 }
1717 }
1718
1719 /* If the instruction was PUSHFL, then the TF bit will be set in the
1720 pushed value, and should be cleared. We'll leave this for later,
1721 since GDB already messes up the TF flag when stepping over a
1722 pushfl. */
1723
1724 /* If the instruction was a call, the return address now atop the
1725 stack is the address following the copied instruction. We need
1726 to make it the address following the original instruction. */
1727 if (amd64_call_p (insn_details))
1728 {
1729 ULONGEST rsp;
1730 ULONGEST retaddr;
1731 const ULONGEST retaddr_len = 8;
1732
1733 regcache_cooked_read_unsigned (regs, AMD64_RSP_REGNUM, &rsp);
1734 retaddr = read_memory_unsigned_integer (rsp, retaddr_len, byte_order);
1735 retaddr = (retaddr - insn_offset) & 0xffffffffffffffffULL;
1736 write_memory_unsigned_integer (rsp, retaddr_len, byte_order, retaddr);
1737
1738 if (debug_displaced)
1739 fprintf_unfiltered (gdb_stdlog,
1740 "displaced: relocated return addr at %s "
1741 "to %s\n",
1742 paddress (gdbarch, rsp),
1743 paddress (gdbarch, retaddr));
1744 }
1745 }
1746
1747 /* If the instruction INSN uses RIP-relative addressing, return the
1748 offset into the raw INSN where the displacement to be adjusted is
1749 found. Returns 0 if the instruction doesn't use RIP-relative
1750 addressing. */
1751
1752 static int
1753 rip_relative_offset (struct amd64_insn *insn)
1754 {
1755 if (insn->modrm_offset != -1)
1756 {
1757 gdb_byte modrm = insn->raw_insn[insn->modrm_offset];
1758
1759 if ((modrm & 0xc7) == 0x05)
1760 {
1761 /* The displacement is found right after the ModRM byte. */
1762 return insn->modrm_offset + 1;
1763 }
1764 }
1765
1766 return 0;
1767 }
1768
1769 static void
1770 append_insns (CORE_ADDR *to, ULONGEST len, const gdb_byte *buf)
1771 {
1772 target_write_memory (*to, buf, len);
1773 *to += len;
1774 }
1775
1776 static void
1777 amd64_relocate_instruction (struct gdbarch *gdbarch,
1778 CORE_ADDR *to, CORE_ADDR oldloc)
1779 {
1780 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1781 int len = gdbarch_max_insn_length (gdbarch);
1782 /* Extra space for sentinels. */
1783 int fixup_sentinel_space = len;
1784 gdb_byte *buf = (gdb_byte *) xmalloc (len + fixup_sentinel_space);
1785 struct amd64_insn insn_details;
1786 int offset = 0;
1787 LONGEST rel32, newrel;
1788 gdb_byte *insn;
1789 int insn_length;
1790
1791 read_memory (oldloc, buf, len);
1792
1793 /* Set up the sentinel space so we don't have to worry about running
1794 off the end of the buffer. An excessive number of leading prefixes
1795 could otherwise cause this. */
1796 memset (buf + len, 0, fixup_sentinel_space);
1797
1798 insn = buf;
1799 amd64_get_insn_details (insn, &insn_details);
1800
1801 insn_length = gdb_buffered_insn_length (gdbarch, insn, len, oldloc);
1802
1803 /* Skip legacy instruction prefixes. */
1804 insn = amd64_skip_prefixes (insn);
1805
1806 /* Adjust calls with 32-bit relative addresses as push/jump, with
1807 the address pushed being the location where the original call in
1808 the user program would return to. */
1809 if (insn[0] == 0xe8)
1810 {
1811 gdb_byte push_buf[32];
1812 CORE_ADDR ret_addr;
1813 int i = 0;
1814
1815 /* Where "ret" in the original code will return to. */
1816 ret_addr = oldloc + insn_length;
1817
1818 /* If pushing an address higher than or equal to 0x80000000,
1819 avoid 'pushq', as that sign extends its 32-bit operand, which
1820 would be incorrect. */
1821 if (ret_addr <= 0x7fffffff)
1822 {
1823 push_buf[0] = 0x68; /* pushq $... */
1824 store_unsigned_integer (&push_buf[1], 4, byte_order, ret_addr);
1825 i = 5;
1826 }
1827 else
1828 {
1829 push_buf[i++] = 0x48; /* sub $0x8,%rsp */
1830 push_buf[i++] = 0x83;
1831 push_buf[i++] = 0xec;
1832 push_buf[i++] = 0x08;
1833
1834 push_buf[i++] = 0xc7; /* movl $imm,(%rsp) */
1835 push_buf[i++] = 0x04;
1836 push_buf[i++] = 0x24;
1837 store_unsigned_integer (&push_buf[i], 4, byte_order,
1838 ret_addr & 0xffffffff);
1839 i += 4;
1840
1841 push_buf[i++] = 0xc7; /* movl $imm,4(%rsp) */
1842 push_buf[i++] = 0x44;
1843 push_buf[i++] = 0x24;
1844 push_buf[i++] = 0x04;
1845 store_unsigned_integer (&push_buf[i], 4, byte_order,
1846 ret_addr >> 32);
1847 i += 4;
1848 }
1849 gdb_assert (i <= sizeof (push_buf));
1850 /* Push the push. */
1851 append_insns (to, i, push_buf);
1852
1853 /* Convert the relative call to a relative jump. */
1854 insn[0] = 0xe9;
1855
1856 /* Adjust the destination offset. */
1857 rel32 = extract_signed_integer (insn + 1, 4, byte_order);
1858 newrel = (oldloc - *to) + rel32;
1859 store_signed_integer (insn + 1, 4, byte_order, newrel);
1860
1861 if (debug_displaced)
1862 fprintf_unfiltered (gdb_stdlog,
1863 "Adjusted insn rel32=%s at %s to"
1864 " rel32=%s at %s\n",
1865 hex_string (rel32), paddress (gdbarch, oldloc),
1866 hex_string (newrel), paddress (gdbarch, *to));
1867
1868 /* Write the adjusted jump into its displaced location. */
1869 append_insns (to, 5, insn);
1870 return;
1871 }
1872
1873 offset = rip_relative_offset (&insn_details);
1874 if (!offset)
1875 {
1876 /* Adjust jumps with 32-bit relative addresses. Calls are
1877 already handled above. */
1878 if (insn[0] == 0xe9)
1879 offset = 1;
1880 /* Adjust conditional jumps. */
1881 else if (insn[0] == 0x0f && (insn[1] & 0xf0) == 0x80)
1882 offset = 2;
1883 }
1884
1885 if (offset)
1886 {
1887 rel32 = extract_signed_integer (insn + offset, 4, byte_order);
1888 newrel = (oldloc - *to) + rel32;
1889 store_signed_integer (insn + offset, 4, byte_order, newrel);
1890 if (debug_displaced)
1891 fprintf_unfiltered (gdb_stdlog,
1892 "Adjusted insn rel32=%s at %s to"
1893 " rel32=%s at %s\n",
1894 hex_string (rel32), paddress (gdbarch, oldloc),
1895 hex_string (newrel), paddress (gdbarch, *to));
1896 }
1897
1898 /* Write the adjusted instruction into its displaced location. */
1899 append_insns (to, insn_length, buf);
1900 }
1901
1902 \f
1903 /* The maximum number of saved registers. This should include %rip. */
1904 #define AMD64_NUM_SAVED_REGS AMD64_NUM_GREGS
1905
1906 struct amd64_frame_cache
1907 {
1908 /* Base address. */
1909 CORE_ADDR base;
1910 int base_p;
1911 CORE_ADDR sp_offset;
1912 CORE_ADDR pc;
1913
1914 /* Saved registers. */
1915 CORE_ADDR saved_regs[AMD64_NUM_SAVED_REGS];
1916 CORE_ADDR saved_sp;
1917 int saved_sp_reg;
1918
1919 /* Do we have a frame? */
1920 int frameless_p;
1921 };
1922
1923 /* Initialize a frame cache. */
1924
1925 static void
1926 amd64_init_frame_cache (struct amd64_frame_cache *cache)
1927 {
1928 int i;
1929
1930 /* Base address. */
1931 cache->base = 0;
1932 cache->base_p = 0;
1933 cache->sp_offset = -8;
1934 cache->pc = 0;
1935
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;
1945
1946 /* Frameless until proven otherwise. */
1947 cache->frameless_p = 1;
1948 }
1949
1950 /* Allocate and initialize a frame cache. */
1951
1952 static struct amd64_frame_cache *
1953 amd64_alloc_frame_cache (void)
1954 {
1955 struct amd64_frame_cache *cache;
1956
1957 cache = FRAME_OBSTACK_ZALLOC (struct amd64_frame_cache);
1958 amd64_init_frame_cache (cache);
1959 return cache;
1960 }
1961
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. */
1967
1968 static CORE_ADDR
1969 amd64_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
1970 struct amd64_frame_cache *cache)
1971 {
1972 /* There are 2 code sequences to re-align stack before the frame
1973 gets set up:
1974
1975 1. Use a caller-saved saved register:
1976
1977 leaq 8(%rsp), %reg
1978 andq $-XXX, %rsp
1979 pushq -8(%reg)
1980
1981 2. Use a callee-saved saved register:
1982
1983 pushq %reg
1984 leaq 16(%rsp), %reg
1985 andq $-XXX, %rsp
1986 pushq -8(%reg)
1987
1988 "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:
1989
1990 0x48 0x83 0xe4 0xf0 andq $-16, %rsp
1991 0x48 0x81 0xe4 0x00 0xff 0xff 0xff andq $-256, %rsp
1992 */
1993
1994 gdb_byte buf[18];
1995 int reg, r;
1996 int offset, offset_and;
1997
1998 if (target_read_code (pc, buf, sizeof buf))
1999 return pc;
2000
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)
2007 {
2008 /* MOD must be binary 10 and R/M must be binary 100. */
2009 if ((buf[2] & 0xc7) != 0x44)
2010 return pc;
2011
2012 /* REG has register number. */
2013 reg = (buf[2] >> 3) & 7;
2014
2015 /* Check the REX.R bit. */
2016 if (buf[0] == 0x4c)
2017 reg += 8;
2018
2019 offset = 5;
2020 }
2021 else
2022 {
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)
2030 {
2031 /* Check the REX.B bit. */
2032 if ((buf[0] & 1) != 0)
2033 reg = 8;
2034
2035 offset = 1;
2036 }
2037 else
2038 return pc;
2039
2040 /* Get register. */
2041 reg += buf[offset] & 0x7;
2042
2043 offset++;
2044
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;
2051
2052 /* MOD must be binary 10 and R/M must be binary 100. */
2053 if ((buf[offset + 2] & 0xc7) != 0x44)
2054 return pc;
2055
2056 /* REG has register number. */
2057 r = (buf[offset + 2] >> 3) & 7;
2058
2059 /* Check the REX.R bit. */
2060 if (buf[offset] == 0x4c)
2061 r += 8;
2062
2063 /* Registers in pushq and leaq have to be the same. */
2064 if (reg != r)
2065 return pc;
2066
2067 offset += 5;
2068 }
2069
2070 /* Rigister can't be %rsp nor %rbp. */
2071 if (reg == 4 || reg == 5)
2072 return pc;
2073
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;
2079
2080 offset_and = offset;
2081 offset += buf[offset + 1] == 0x81 ? 7 : 4;
2082
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)
2089 {
2090 /* Check the REX.B bit. */
2091 if ((buf[offset] & 0x1) != 0)
2092 r = 8;
2093 offset += 2;
2094 }
2095 else
2096 return pc;
2097
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;
2103
2104 /* R/M has register. */
2105 r += buf[offset] & 7;
2106
2107 /* Registers in leaq and pushq have to be the same. */
2108 if (reg != r)
2109 return pc;
2110
2111 if (current_pc > pc + offset_and)
2112 cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);
2113
2114 return std::min (pc + offset + 2, current_pc);
2115 }
2116
2117 /* Similar to amd64_analyze_stack_align for x32. */
2118
2119 static CORE_ADDR
2120 amd64_x32_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
2121 struct amd64_frame_cache *cache)
2122 {
2123 /* There are 2 code sequences to re-align stack before the frame
2124 gets set up:
2125
2126 1. Use a caller-saved saved register:
2127
2128 leaq 8(%rsp), %reg
2129 andq $-XXX, %rsp
2130 pushq -8(%reg)
2131
2132 or
2133
2134 [addr32] leal 8(%rsp), %reg
2135 andl $-XXX, %esp
2136 [addr32] pushq -8(%reg)
2137
2138 2. Use a callee-saved saved register:
2139
2140 pushq %reg
2141 leaq 16(%rsp), %reg
2142 andq $-XXX, %rsp
2143 pushq -8(%reg)
2144
2145 or
2146
2147 pushq %reg
2148 [addr32] leal 16(%rsp), %reg
2149 andl $-XXX, %esp
2150 [addr32] pushq -8(%reg)
2151
2152 "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:
2153
2154 0x48 0x83 0xe4 0xf0 andq $-16, %rsp
2155 0x48 0x81 0xe4 0x00 0xff 0xff 0xff andq $-256, %rsp
2156
2157 "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:
2158
2159 0x83 0xe4 0xf0 andl $-16, %esp
2160 0x81 0xe4 0x00 0xff 0xff 0xff andl $-256, %esp
2161 */
2162
2163 gdb_byte buf[19];
2164 int reg, r;
2165 int offset, offset_and;
2166
2167 if (target_read_memory (pc, buf, sizeof buf))
2168 return pc;
2169
2170 /* Skip optional addr32 prefix. */
2171 offset = buf[0] == 0x67 ? 1 : 0;
2172
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)
2179 {
2180 /* MOD must be binary 10 and R/M must be binary 100. */
2181 if ((buf[offset + 2] & 0xc7) != 0x44)
2182 return pc;
2183
2184 /* REG has register number. */
2185 reg = (buf[offset + 2] >> 3) & 7;
2186
2187 /* Check the REX.R bit. */
2188 if ((buf[offset] & 0x4) != 0)
2189 reg += 8;
2190
2191 offset += 5;
2192 }
2193 else
2194 {
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)
2200 {
2201 /* Check the REX.B bit. */
2202 if ((buf[offset] & 1) != 0)
2203 reg = 8;
2204
2205 offset += 1;
2206 }
2207 else if ((buf[offset] & 0xf8) != 0x50)
2208 return pc;
2209
2210 /* Get register. */
2211 reg += buf[offset] & 0x7;
2212
2213 offset++;
2214
2215 /* Skip optional addr32 prefix. */
2216 if (buf[offset] == 0x67)
2217 offset++;
2218
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;
2226
2227 /* MOD must be binary 10 and R/M must be binary 100. */
2228 if ((buf[offset + 2] & 0xc7) != 0x44)
2229 return pc;
2230
2231 /* REG has register number. */
2232 r = (buf[offset + 2] >> 3) & 7;
2233
2234 /* Check the REX.R bit. */
2235 if ((buf[offset] & 0x4) != 0)
2236 r += 8;
2237
2238 /* Registers in pushq and leaq have to be the same. */
2239 if (reg != r)
2240 return pc;
2241
2242 offset += 5;
2243 }
2244
2245 /* Rigister can't be %rsp nor %rbp. */
2246 if (reg == 4 || reg == 5)
2247 return pc;
2248
2249 /* The next instruction may be "andq $-XXX, %rsp" or
2250 "andl $-XXX, %esp". */
2251 if (buf[offset] != 0x48)
2252 offset--;
2253
2254 if (buf[offset + 2] != 0xe4
2255 || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
2256 return pc;
2257
2258 offset_and = offset;
2259 offset += buf[offset + 1] == 0x81 ? 7 : 4;
2260
2261 /* Skip optional addr32 prefix. */
2262 if (buf[offset] == 0x67)
2263 offset++;
2264
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)
2271 {
2272 /* Check the REX.B bit. */
2273 if ((buf[offset] & 0x1) != 0)
2274 r = 8;
2275 offset += 2;
2276 }
2277 else
2278 return pc;
2279
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;
2285
2286 /* R/M has register. */
2287 r += buf[offset] & 7;
2288
2289 /* Registers in leaq and pushq have to be the same. */
2290 if (reg != r)
2291 return pc;
2292
2293 if (current_pc > pc + offset_and)
2294 cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);
2295
2296 return std::min (pc + offset + 2, current_pc);
2297 }
2298
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.
2302
2303 We will handle only functions beginning with:
2304
2305 pushq %rbp 0x55
2306 movq %rsp, %rbp 0x48 0x89 0xe5 (or 0x48 0x8b 0xec)
2307
2308 or (for the X32 ABI):
2309
2310 pushq %rbp 0x55
2311 movl %esp, %ebp 0x89 0xe5 (or 0x8b 0xec)
2312
2313 Any function that doesn't start with one of these sequences will be
2314 assumed to have no prologue and thus no valid frame pointer in
2315 %rbp. */
2316
2317 static CORE_ADDR
2318 amd64_analyze_prologue (struct gdbarch *gdbarch,
2319 CORE_ADDR pc, CORE_ADDR current_pc,
2320 struct amd64_frame_cache *cache)
2321 {
2322 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2323 /* There are two variations of movq %rsp, %rbp. */
2324 static const gdb_byte mov_rsp_rbp_1[3] = { 0x48, 0x89, 0xe5 };
2325 static const gdb_byte mov_rsp_rbp_2[3] = { 0x48, 0x8b, 0xec };
2326 /* Ditto for movl %esp, %ebp. */
2327 static const gdb_byte mov_esp_ebp_1[2] = { 0x89, 0xe5 };
2328 static const gdb_byte mov_esp_ebp_2[2] = { 0x8b, 0xec };
2329
2330 gdb_byte buf[3];
2331 gdb_byte op;
2332
2333 if (current_pc <= pc)
2334 return current_pc;
2335
2336 if (gdbarch_ptr_bit (gdbarch) == 32)
2337 pc = amd64_x32_analyze_stack_align (pc, current_pc, cache);
2338 else
2339 pc = amd64_analyze_stack_align (pc, current_pc, cache);
2340
2341 op = read_code_unsigned_integer (pc, 1, byte_order);
2342
2343 if (op == 0x55) /* pushq %rbp */
2344 {
2345 /* Take into account that we've executed the `pushq %rbp' that
2346 starts this instruction sequence. */
2347 cache->saved_regs[AMD64_RBP_REGNUM] = 0;
2348 cache->sp_offset += 8;
2349
2350 /* If that's all, return now. */
2351 if (current_pc <= pc + 1)
2352 return current_pc;
2353
2354 read_code (pc + 1, buf, 3);
2355
2356 /* Check for `movq %rsp, %rbp'. */
2357 if (memcmp (buf, mov_rsp_rbp_1, 3) == 0
2358 || memcmp (buf, mov_rsp_rbp_2, 3) == 0)
2359 {
2360 /* OK, we actually have a frame. */
2361 cache->frameless_p = 0;
2362 return pc + 4;
2363 }
2364
2365 /* For X32, also check for `movq %esp, %ebp'. */
2366 if (gdbarch_ptr_bit (gdbarch) == 32)
2367 {
2368 if (memcmp (buf, mov_esp_ebp_1, 2) == 0
2369 || memcmp (buf, mov_esp_ebp_2, 2) == 0)
2370 {
2371 /* OK, we actually have a frame. */
2372 cache->frameless_p = 0;
2373 return pc + 3;
2374 }
2375 }
2376
2377 return pc + 1;
2378 }
2379
2380 return pc;
2381 }
2382
2383 /* Work around false termination of prologue - GCC PR debug/48827.
2384
2385 START_PC is the first instruction of a function, PC is its minimal already
2386 determined advanced address. Function returns PC if it has nothing to do.
2387
2388 84 c0 test %al,%al
2389 74 23 je after
2390 <-- here is 0 lines advance - the false prologue end marker.
2391 0f 29 85 70 ff ff ff movaps %xmm0,-0x90(%rbp)
2392 0f 29 4d 80 movaps %xmm1,-0x80(%rbp)
2393 0f 29 55 90 movaps %xmm2,-0x70(%rbp)
2394 0f 29 5d a0 movaps %xmm3,-0x60(%rbp)
2395 0f 29 65 b0 movaps %xmm4,-0x50(%rbp)
2396 0f 29 6d c0 movaps %xmm5,-0x40(%rbp)
2397 0f 29 75 d0 movaps %xmm6,-0x30(%rbp)
2398 0f 29 7d e0 movaps %xmm7,-0x20(%rbp)
2399 after: */
2400
2401 static CORE_ADDR
2402 amd64_skip_xmm_prologue (CORE_ADDR pc, CORE_ADDR start_pc)
2403 {
2404 struct symtab_and_line start_pc_sal, next_sal;
2405 gdb_byte buf[4 + 8 * 7];
2406 int offset, xmmreg;
2407
2408 if (pc == start_pc)
2409 return pc;
2410
2411 start_pc_sal = find_pc_sect_line (start_pc, NULL, 0);
2412 if (start_pc_sal.symtab == NULL
2413 || producer_is_gcc_ge_4 (COMPUNIT_PRODUCER
2414 (SYMTAB_COMPUNIT (start_pc_sal.symtab))) < 6
2415 || start_pc_sal.pc != start_pc || pc >= start_pc_sal.end)
2416 return pc;
2417
2418 next_sal = find_pc_sect_line (start_pc_sal.end, NULL, 0);
2419 if (next_sal.line != start_pc_sal.line)
2420 return pc;
2421
2422 /* START_PC can be from overlayed memory, ignored here. */
2423 if (target_read_code (next_sal.pc - 4, buf, sizeof (buf)) != 0)
2424 return pc;
2425
2426 /* test %al,%al */
2427 if (buf[0] != 0x84 || buf[1] != 0xc0)
2428 return pc;
2429 /* je AFTER */
2430 if (buf[2] != 0x74)
2431 return pc;
2432
2433 offset = 4;
2434 for (xmmreg = 0; xmmreg < 8; xmmreg++)
2435 {
2436 /* 0x0f 0x29 0b??000101 movaps %xmmreg?,-0x??(%rbp) */
2437 if (buf[offset] != 0x0f || buf[offset + 1] != 0x29
2438 || (buf[offset + 2] & 0x3f) != (xmmreg << 3 | 0x5))
2439 return pc;
2440
2441 /* 0b01?????? */
2442 if ((buf[offset + 2] & 0xc0) == 0x40)
2443 {
2444 /* 8-bit displacement. */
2445 offset += 4;
2446 }
2447 /* 0b10?????? */
2448 else if ((buf[offset + 2] & 0xc0) == 0x80)
2449 {
2450 /* 32-bit displacement. */
2451 offset += 7;
2452 }
2453 else
2454 return pc;
2455 }
2456
2457 /* je AFTER */
2458 if (offset - 4 != buf[3])
2459 return pc;
2460
2461 return next_sal.end;
2462 }
2463
2464 /* Return PC of first real instruction. */
2465
2466 static CORE_ADDR
2467 amd64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
2468 {
2469 struct amd64_frame_cache cache;
2470 CORE_ADDR pc;
2471 CORE_ADDR func_addr;
2472
2473 if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
2474 {
2475 CORE_ADDR post_prologue_pc
2476 = skip_prologue_using_sal (gdbarch, func_addr);
2477 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
2478
2479 /* Clang always emits a line note before the prologue and another
2480 one after. We trust clang to emit usable line notes. */
2481 if (post_prologue_pc
2482 && (cust != NULL
2483 && COMPUNIT_PRODUCER (cust) != NULL
2484 && startswith (COMPUNIT_PRODUCER (cust), "clang ")))
2485 return std::max (start_pc, post_prologue_pc);
2486 }
2487
2488 amd64_init_frame_cache (&cache);
2489 pc = amd64_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffLL,
2490 &cache);
2491 if (cache.frameless_p)
2492 return start_pc;
2493
2494 return amd64_skip_xmm_prologue (pc, start_pc);
2495 }
2496 \f
2497
2498 /* Normal frames. */
2499
2500 static void
2501 amd64_frame_cache_1 (struct frame_info *this_frame,
2502 struct amd64_frame_cache *cache)
2503 {
2504 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2505 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2506 gdb_byte buf[8];
2507 int i;
2508
2509 cache->pc = get_frame_func (this_frame);
2510 if (cache->pc != 0)
2511 amd64_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
2512 cache);
2513
2514 if (cache->frameless_p)
2515 {
2516 /* We didn't find a valid frame. If we're at the start of a
2517 function, or somewhere half-way its prologue, the function's
2518 frame probably hasn't been fully setup yet. Try to
2519 reconstruct the base address for the stack frame by looking
2520 at the stack pointer. For truly "frameless" functions this
2521 might work too. */
2522
2523 if (cache->saved_sp_reg != -1)
2524 {
2525 /* Stack pointer has been saved. */
2526 get_frame_register (this_frame, cache->saved_sp_reg, buf);
2527 cache->saved_sp = extract_unsigned_integer (buf, 8, byte_order);
2528
2529 /* We're halfway aligning the stack. */
2530 cache->base = ((cache->saved_sp - 8) & 0xfffffffffffffff0LL) - 8;
2531 cache->saved_regs[AMD64_RIP_REGNUM] = cache->saved_sp - 8;
2532
2533 /* This will be added back below. */
2534 cache->saved_regs[AMD64_RIP_REGNUM] -= cache->base;
2535 }
2536 else
2537 {
2538 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2539 cache->base = extract_unsigned_integer (buf, 8, byte_order)
2540 + cache->sp_offset;
2541 }
2542 }
2543 else
2544 {
2545 get_frame_register (this_frame, AMD64_RBP_REGNUM, buf);
2546 cache->base = extract_unsigned_integer (buf, 8, byte_order);
2547 }
2548
2549 /* Now that we have the base address for the stack frame we can
2550 calculate the value of %rsp in the calling frame. */
2551 cache->saved_sp = cache->base + 16;
2552
2553 /* For normal frames, %rip is stored at 8(%rbp). If we don't have a
2554 frame we find it at the same offset from the reconstructed base
2555 address. If we're halfway aligning the stack, %rip is handled
2556 differently (see above). */
2557 if (!cache->frameless_p || cache->saved_sp_reg == -1)
2558 cache->saved_regs[AMD64_RIP_REGNUM] = 8;
2559
2560 /* Adjust all the saved registers such that they contain addresses
2561 instead of offsets. */
2562 for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
2563 if (cache->saved_regs[i] != -1)
2564 cache->saved_regs[i] += cache->base;
2565
2566 cache->base_p = 1;
2567 }
2568
2569 static struct amd64_frame_cache *
2570 amd64_frame_cache (struct frame_info *this_frame, void **this_cache)
2571 {
2572 struct amd64_frame_cache *cache;
2573
2574 if (*this_cache)
2575 return (struct amd64_frame_cache *) *this_cache;
2576
2577 cache = amd64_alloc_frame_cache ();
2578 *this_cache = cache;
2579
2580 TRY
2581 {
2582 amd64_frame_cache_1 (this_frame, cache);
2583 }
2584 CATCH (ex, RETURN_MASK_ERROR)
2585 {
2586 if (ex.error != NOT_AVAILABLE_ERROR)
2587 throw_exception (ex);
2588 }
2589 END_CATCH
2590
2591 return cache;
2592 }
2593
2594 static enum unwind_stop_reason
2595 amd64_frame_unwind_stop_reason (struct frame_info *this_frame,
2596 void **this_cache)
2597 {
2598 struct amd64_frame_cache *cache =
2599 amd64_frame_cache (this_frame, this_cache);
2600
2601 if (!cache->base_p)
2602 return UNWIND_UNAVAILABLE;
2603
2604 /* This marks the outermost frame. */
2605 if (cache->base == 0)
2606 return UNWIND_OUTERMOST;
2607
2608 return UNWIND_NO_REASON;
2609 }
2610
2611 static void
2612 amd64_frame_this_id (struct frame_info *this_frame, void **this_cache,
2613 struct frame_id *this_id)
2614 {
2615 struct amd64_frame_cache *cache =
2616 amd64_frame_cache (this_frame, this_cache);
2617
2618 if (!cache->base_p)
2619 (*this_id) = frame_id_build_unavailable_stack (cache->pc);
2620 else if (cache->base == 0)
2621 {
2622 /* This marks the outermost frame. */
2623 return;
2624 }
2625 else
2626 (*this_id) = frame_id_build (cache->base + 16, cache->pc);
2627 }
2628
2629 static struct value *
2630 amd64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
2631 int regnum)
2632 {
2633 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2634 struct amd64_frame_cache *cache =
2635 amd64_frame_cache (this_frame, this_cache);
2636
2637 gdb_assert (regnum >= 0);
2638
2639 if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
2640 return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
2641
2642 if (regnum < AMD64_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
2643 return frame_unwind_got_memory (this_frame, regnum,
2644 cache->saved_regs[regnum]);
2645
2646 return frame_unwind_got_register (this_frame, regnum, regnum);
2647 }
2648
2649 static const struct frame_unwind amd64_frame_unwind =
2650 {
2651 NORMAL_FRAME,
2652 amd64_frame_unwind_stop_reason,
2653 amd64_frame_this_id,
2654 amd64_frame_prev_register,
2655 NULL,
2656 default_frame_sniffer
2657 };
2658 \f
2659 /* Generate a bytecode expression to get the value of the saved PC. */
2660
2661 static void
2662 amd64_gen_return_address (struct gdbarch *gdbarch,
2663 struct agent_expr *ax, struct axs_value *value,
2664 CORE_ADDR scope)
2665 {
2666 /* The following sequence assumes the traditional use of the base
2667 register. */
2668 ax_reg (ax, AMD64_RBP_REGNUM);
2669 ax_const_l (ax, 8);
2670 ax_simple (ax, aop_add);
2671 value->type = register_type (gdbarch, AMD64_RIP_REGNUM);
2672 value->kind = axs_lvalue_memory;
2673 }
2674 \f
2675
2676 /* Signal trampolines. */
2677
2678 /* FIXME: kettenis/20030419: Perhaps, we can unify the 32-bit and
2679 64-bit variants. This would require using identical frame caches
2680 on both platforms. */
2681
2682 static struct amd64_frame_cache *
2683 amd64_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
2684 {
2685 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2686 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2687 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2688 struct amd64_frame_cache *cache;
2689 CORE_ADDR addr;
2690 gdb_byte buf[8];
2691 int i;
2692
2693 if (*this_cache)
2694 return (struct amd64_frame_cache *) *this_cache;
2695
2696 cache = amd64_alloc_frame_cache ();
2697
2698 TRY
2699 {
2700 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2701 cache->base = extract_unsigned_integer (buf, 8, byte_order) - 8;
2702
2703 addr = tdep->sigcontext_addr (this_frame);
2704 gdb_assert (tdep->sc_reg_offset);
2705 gdb_assert (tdep->sc_num_regs <= AMD64_NUM_SAVED_REGS);
2706 for (i = 0; i < tdep->sc_num_regs; i++)
2707 if (tdep->sc_reg_offset[i] != -1)
2708 cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
2709
2710 cache->base_p = 1;
2711 }
2712 CATCH (ex, RETURN_MASK_ERROR)
2713 {
2714 if (ex.error != NOT_AVAILABLE_ERROR)
2715 throw_exception (ex);
2716 }
2717 END_CATCH
2718
2719 *this_cache = cache;
2720 return cache;
2721 }
2722
2723 static enum unwind_stop_reason
2724 amd64_sigtramp_frame_unwind_stop_reason (struct frame_info *this_frame,
2725 void **this_cache)
2726 {
2727 struct amd64_frame_cache *cache =
2728 amd64_sigtramp_frame_cache (this_frame, this_cache);
2729
2730 if (!cache->base_p)
2731 return UNWIND_UNAVAILABLE;
2732
2733 return UNWIND_NO_REASON;
2734 }
2735
2736 static void
2737 amd64_sigtramp_frame_this_id (struct frame_info *this_frame,
2738 void **this_cache, struct frame_id *this_id)
2739 {
2740 struct amd64_frame_cache *cache =
2741 amd64_sigtramp_frame_cache (this_frame, this_cache);
2742
2743 if (!cache->base_p)
2744 (*this_id) = frame_id_build_unavailable_stack (get_frame_pc (this_frame));
2745 else if (cache->base == 0)
2746 {
2747 /* This marks the outermost frame. */
2748 return;
2749 }
2750 else
2751 (*this_id) = frame_id_build (cache->base + 16, get_frame_pc (this_frame));
2752 }
2753
2754 static struct value *
2755 amd64_sigtramp_frame_prev_register (struct frame_info *this_frame,
2756 void **this_cache, int regnum)
2757 {
2758 /* Make sure we've initialized the cache. */
2759 amd64_sigtramp_frame_cache (this_frame, this_cache);
2760
2761 return amd64_frame_prev_register (this_frame, this_cache, regnum);
2762 }
2763
2764 static int
2765 amd64_sigtramp_frame_sniffer (const struct frame_unwind *self,
2766 struct frame_info *this_frame,
2767 void **this_cache)
2768 {
2769 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
2770
2771 /* We shouldn't even bother if we don't have a sigcontext_addr
2772 handler. */
2773 if (tdep->sigcontext_addr == NULL)
2774 return 0;
2775
2776 if (tdep->sigtramp_p != NULL)
2777 {
2778 if (tdep->sigtramp_p (this_frame))
2779 return 1;
2780 }
2781
2782 if (tdep->sigtramp_start != 0)
2783 {
2784 CORE_ADDR pc = get_frame_pc (this_frame);
2785
2786 gdb_assert (tdep->sigtramp_end != 0);
2787 if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
2788 return 1;
2789 }
2790
2791 return 0;
2792 }
2793
2794 static const struct frame_unwind amd64_sigtramp_frame_unwind =
2795 {
2796 SIGTRAMP_FRAME,
2797 amd64_sigtramp_frame_unwind_stop_reason,
2798 amd64_sigtramp_frame_this_id,
2799 amd64_sigtramp_frame_prev_register,
2800 NULL,
2801 amd64_sigtramp_frame_sniffer
2802 };
2803 \f
2804
2805 static CORE_ADDR
2806 amd64_frame_base_address (struct frame_info *this_frame, void **this_cache)
2807 {
2808 struct amd64_frame_cache *cache =
2809 amd64_frame_cache (this_frame, this_cache);
2810
2811 return cache->base;
2812 }
2813
2814 static const struct frame_base amd64_frame_base =
2815 {
2816 &amd64_frame_unwind,
2817 amd64_frame_base_address,
2818 amd64_frame_base_address,
2819 amd64_frame_base_address
2820 };
2821
2822 /* Normal frames, but in a function epilogue. */
2823
2824 /* Implement the stack_frame_destroyed_p gdbarch method.
2825
2826 The epilogue is defined here as the 'ret' instruction, which will
2827 follow any instruction such as 'leave' or 'pop %ebp' that destroys
2828 the function's stack frame. */
2829
2830 static int
2831 amd64_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
2832 {
2833 gdb_byte insn;
2834 struct compunit_symtab *cust;
2835
2836 cust = find_pc_compunit_symtab (pc);
2837 if (cust != NULL && COMPUNIT_EPILOGUE_UNWIND_VALID (cust))
2838 return 0;
2839
2840 if (target_read_memory (pc, &insn, 1))
2841 return 0; /* Can't read memory at pc. */
2842
2843 if (insn != 0xc3) /* 'ret' instruction. */
2844 return 0;
2845
2846 return 1;
2847 }
2848
2849 static int
2850 amd64_epilogue_frame_sniffer (const struct frame_unwind *self,
2851 struct frame_info *this_frame,
2852 void **this_prologue_cache)
2853 {
2854 if (frame_relative_level (this_frame) == 0)
2855 return amd64_stack_frame_destroyed_p (get_frame_arch (this_frame),
2856 get_frame_pc (this_frame));
2857 else
2858 return 0;
2859 }
2860
2861 static struct amd64_frame_cache *
2862 amd64_epilogue_frame_cache (struct frame_info *this_frame, void **this_cache)
2863 {
2864 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2865 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2866 struct amd64_frame_cache *cache;
2867 gdb_byte buf[8];
2868
2869 if (*this_cache)
2870 return (struct amd64_frame_cache *) *this_cache;
2871
2872 cache = amd64_alloc_frame_cache ();
2873 *this_cache = cache;
2874
2875 TRY
2876 {
2877 /* Cache base will be %esp plus cache->sp_offset (-8). */
2878 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2879 cache->base = extract_unsigned_integer (buf, 8,
2880 byte_order) + cache->sp_offset;
2881
2882 /* Cache pc will be the frame func. */
2883 cache->pc = get_frame_pc (this_frame);
2884
2885 /* The saved %esp will be at cache->base plus 16. */
2886 cache->saved_sp = cache->base + 16;
2887
2888 /* The saved %eip will be at cache->base plus 8. */
2889 cache->saved_regs[AMD64_RIP_REGNUM] = cache->base + 8;
2890
2891 cache->base_p = 1;
2892 }
2893 CATCH (ex, RETURN_MASK_ERROR)
2894 {
2895 if (ex.error != NOT_AVAILABLE_ERROR)
2896 throw_exception (ex);
2897 }
2898 END_CATCH
2899
2900 return cache;
2901 }
2902
2903 static enum unwind_stop_reason
2904 amd64_epilogue_frame_unwind_stop_reason (struct frame_info *this_frame,
2905 void **this_cache)
2906 {
2907 struct amd64_frame_cache *cache
2908 = amd64_epilogue_frame_cache (this_frame, this_cache);
2909
2910 if (!cache->base_p)
2911 return UNWIND_UNAVAILABLE;
2912
2913 return UNWIND_NO_REASON;
2914 }
2915
2916 static void
2917 amd64_epilogue_frame_this_id (struct frame_info *this_frame,
2918 void **this_cache,
2919 struct frame_id *this_id)
2920 {
2921 struct amd64_frame_cache *cache = amd64_epilogue_frame_cache (this_frame,
2922 this_cache);
2923
2924 if (!cache->base_p)
2925 (*this_id) = frame_id_build_unavailable_stack (cache->pc);
2926 else
2927 (*this_id) = frame_id_build (cache->base + 8, cache->pc);
2928 }
2929
2930 static const struct frame_unwind amd64_epilogue_frame_unwind =
2931 {
2932 NORMAL_FRAME,
2933 amd64_epilogue_frame_unwind_stop_reason,
2934 amd64_epilogue_frame_this_id,
2935 amd64_frame_prev_register,
2936 NULL,
2937 amd64_epilogue_frame_sniffer
2938 };
2939
2940 static struct frame_id
2941 amd64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
2942 {
2943 CORE_ADDR fp;
2944
2945 fp = get_frame_register_unsigned (this_frame, AMD64_RBP_REGNUM);
2946
2947 return frame_id_build (fp + 16, get_frame_pc (this_frame));
2948 }
2949
2950 /* 16 byte align the SP per frame requirements. */
2951
2952 static CORE_ADDR
2953 amd64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
2954 {
2955 return sp & -(CORE_ADDR)16;
2956 }
2957 \f
2958
2959 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
2960 in the floating-point register set REGSET to register cache
2961 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
2962
2963 static void
2964 amd64_supply_fpregset (const struct regset *regset, struct regcache *regcache,
2965 int regnum, const void *fpregs, size_t len)
2966 {
2967 struct gdbarch *gdbarch = regcache->arch ();
2968 const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2969
2970 gdb_assert (len >= tdep->sizeof_fpregset);
2971 amd64_supply_fxsave (regcache, regnum, fpregs);
2972 }
2973
2974 /* Collect register REGNUM from the register cache REGCACHE and store
2975 it in the buffer specified by FPREGS and LEN as described by the
2976 floating-point register set REGSET. If REGNUM is -1, do this for
2977 all registers in REGSET. */
2978
2979 static void
2980 amd64_collect_fpregset (const struct regset *regset,
2981 const struct regcache *regcache,
2982 int regnum, void *fpregs, size_t len)
2983 {
2984 struct gdbarch *gdbarch = regcache->arch ();
2985 const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2986
2987 gdb_assert (len >= tdep->sizeof_fpregset);
2988 amd64_collect_fxsave (regcache, regnum, fpregs);
2989 }
2990
2991 const struct regset amd64_fpregset =
2992 {
2993 NULL, amd64_supply_fpregset, amd64_collect_fpregset
2994 };
2995 \f
2996
2997 /* Figure out where the longjmp will land. Slurp the jmp_buf out of
2998 %rdi. We expect its value to be a pointer to the jmp_buf structure
2999 from which we extract the address that we will land at. This
3000 address is copied into PC. This routine returns non-zero on
3001 success. */
3002
3003 static int
3004 amd64_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
3005 {
3006 gdb_byte buf[8];
3007 CORE_ADDR jb_addr;
3008 struct gdbarch *gdbarch = get_frame_arch (frame);
3009 int jb_pc_offset = gdbarch_tdep (gdbarch)->jb_pc_offset;
3010 int len = TYPE_LENGTH (builtin_type (gdbarch)->builtin_func_ptr);
3011
3012 /* If JB_PC_OFFSET is -1, we have no way to find out where the
3013 longjmp will land. */
3014 if (jb_pc_offset == -1)
3015 return 0;
3016
3017 get_frame_register (frame, AMD64_RDI_REGNUM, buf);
3018 jb_addr= extract_typed_address
3019 (buf, builtin_type (gdbarch)->builtin_data_ptr);
3020 if (target_read_memory (jb_addr + jb_pc_offset, buf, len))
3021 return 0;
3022
3023 *pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
3024
3025 return 1;
3026 }
3027
3028 static const int amd64_record_regmap[] =
3029 {
3030 AMD64_RAX_REGNUM, AMD64_RCX_REGNUM, AMD64_RDX_REGNUM, AMD64_RBX_REGNUM,
3031 AMD64_RSP_REGNUM, AMD64_RBP_REGNUM, AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
3032 AMD64_R8_REGNUM, AMD64_R9_REGNUM, AMD64_R10_REGNUM, AMD64_R11_REGNUM,
3033 AMD64_R12_REGNUM, AMD64_R13_REGNUM, AMD64_R14_REGNUM, AMD64_R15_REGNUM,
3034 AMD64_RIP_REGNUM, AMD64_EFLAGS_REGNUM, AMD64_CS_REGNUM, AMD64_SS_REGNUM,
3035 AMD64_DS_REGNUM, AMD64_ES_REGNUM, AMD64_FS_REGNUM, AMD64_GS_REGNUM
3036 };
3037
3038 /* Implement the "in_indirect_branch_thunk" gdbarch function. */
3039
3040 static bool
3041 amd64_in_indirect_branch_thunk (struct gdbarch *gdbarch, CORE_ADDR pc)
3042 {
3043 return x86_in_indirect_branch_thunk (pc, amd64_register_names,
3044 AMD64_RAX_REGNUM,
3045 AMD64_RIP_REGNUM);
3046 }
3047
3048 void
3049 amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
3050 const target_desc *default_tdesc)
3051 {
3052 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3053 const struct target_desc *tdesc = info.target_desc;
3054 static const char *const stap_integer_prefixes[] = { "$", NULL };
3055 static const char *const stap_register_prefixes[] = { "%", NULL };
3056 static const char *const stap_register_indirection_prefixes[] = { "(",
3057 NULL };
3058 static const char *const stap_register_indirection_suffixes[] = { ")",
3059 NULL };
3060
3061 /* AMD64 generally uses `fxsave' instead of `fsave' for saving its
3062 floating-point registers. */
3063 tdep->sizeof_fpregset = I387_SIZEOF_FXSAVE;
3064 tdep->fpregset = &amd64_fpregset;
3065
3066 if (! tdesc_has_registers (tdesc))
3067 tdesc = default_tdesc;
3068 tdep->tdesc = tdesc;
3069
3070 tdep->num_core_regs = AMD64_NUM_GREGS + I387_NUM_REGS;
3071 tdep->register_names = amd64_register_names;
3072
3073 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx512") != NULL)
3074 {
3075 tdep->zmmh_register_names = amd64_zmmh_names;
3076 tdep->k_register_names = amd64_k_names;
3077 tdep->xmm_avx512_register_names = amd64_xmm_avx512_names;
3078 tdep->ymm16h_register_names = amd64_ymmh_avx512_names;
3079
3080 tdep->num_zmm_regs = 32;
3081 tdep->num_xmm_avx512_regs = 16;
3082 tdep->num_ymm_avx512_regs = 16;
3083
3084 tdep->zmm0h_regnum = AMD64_ZMM0H_REGNUM;
3085 tdep->k0_regnum = AMD64_K0_REGNUM;
3086 tdep->xmm16_regnum = AMD64_XMM16_REGNUM;
3087 tdep->ymm16h_regnum = AMD64_YMM16H_REGNUM;
3088 }
3089
3090 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx") != NULL)
3091 {
3092 tdep->ymmh_register_names = amd64_ymmh_names;
3093 tdep->num_ymm_regs = 16;
3094 tdep->ymm0h_regnum = AMD64_YMM0H_REGNUM;
3095 }
3096
3097 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.mpx") != NULL)
3098 {
3099 tdep->mpx_register_names = amd64_mpx_names;
3100 tdep->bndcfgu_regnum = AMD64_BNDCFGU_REGNUM;
3101 tdep->bnd0r_regnum = AMD64_BND0R_REGNUM;
3102 }
3103
3104 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.segments") != NULL)
3105 {
3106 const struct tdesc_feature *feature =
3107 tdesc_find_feature (tdesc, "org.gnu.gdb.i386.segments");
3108 struct tdesc_arch_data *tdesc_data_segments =
3109 (struct tdesc_arch_data *) info.tdep_info;
3110
3111 tdesc_numbered_register (feature, tdesc_data_segments,
3112 AMD64_FSBASE_REGNUM, "fs_base");
3113 tdesc_numbered_register (feature, tdesc_data_segments,
3114 AMD64_GSBASE_REGNUM, "gs_base");
3115 }
3116
3117 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.pkeys") != NULL)
3118 {
3119 tdep->pkeys_register_names = amd64_pkeys_names;
3120 tdep->pkru_regnum = AMD64_PKRU_REGNUM;
3121 tdep->num_pkeys_regs = 1;
3122 }
3123
3124 tdep->num_byte_regs = 20;
3125 tdep->num_word_regs = 16;
3126 tdep->num_dword_regs = 16;
3127 /* Avoid wiring in the MMX registers for now. */
3128 tdep->num_mmx_regs = 0;
3129
3130 set_gdbarch_pseudo_register_read_value (gdbarch,
3131 amd64_pseudo_register_read_value);
3132 set_gdbarch_pseudo_register_write (gdbarch,
3133 amd64_pseudo_register_write);
3134 set_gdbarch_ax_pseudo_register_collect (gdbarch,
3135 amd64_ax_pseudo_register_collect);
3136
3137 set_tdesc_pseudo_register_name (gdbarch, amd64_pseudo_register_name);
3138
3139 /* AMD64 has an FPU and 16 SSE registers. */
3140 tdep->st0_regnum = AMD64_ST0_REGNUM;
3141 tdep->num_xmm_regs = 16;
3142
3143 /* This is what all the fuss is about. */
3144 set_gdbarch_long_bit (gdbarch, 64);
3145 set_gdbarch_long_long_bit (gdbarch, 64);
3146 set_gdbarch_ptr_bit (gdbarch, 64);
3147
3148 /* In contrast to the i386, on AMD64 a `long double' actually takes
3149 up 128 bits, even though it's still based on the i387 extended
3150 floating-point format which has only 80 significant bits. */
3151 set_gdbarch_long_double_bit (gdbarch, 128);
3152
3153 set_gdbarch_num_regs (gdbarch, AMD64_NUM_REGS);
3154
3155 /* Register numbers of various important registers. */
3156 set_gdbarch_sp_regnum (gdbarch, AMD64_RSP_REGNUM); /* %rsp */
3157 set_gdbarch_pc_regnum (gdbarch, AMD64_RIP_REGNUM); /* %rip */
3158 set_gdbarch_ps_regnum (gdbarch, AMD64_EFLAGS_REGNUM); /* %eflags */
3159 set_gdbarch_fp0_regnum (gdbarch, AMD64_ST0_REGNUM); /* %st(0) */
3160
3161 /* The "default" register numbering scheme for AMD64 is referred to
3162 as the "DWARF Register Number Mapping" in the System V psABI.
3163 The preferred debugging format for all known AMD64 targets is
3164 actually DWARF2, and GCC doesn't seem to support DWARF (that is
3165 DWARF-1), but we provide the same mapping just in case. This
3166 mapping is also used for stabs, which GCC does support. */
3167 set_gdbarch_stab_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
3168 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
3169
3170 /* We don't override SDB_REG_RO_REGNUM, since COFF doesn't seem to
3171 be in use on any of the supported AMD64 targets. */
3172
3173 /* Call dummy code. */
3174 set_gdbarch_push_dummy_call (gdbarch, amd64_push_dummy_call);
3175 set_gdbarch_frame_align (gdbarch, amd64_frame_align);
3176 set_gdbarch_frame_red_zone_size (gdbarch, 128);
3177
3178 set_gdbarch_convert_register_p (gdbarch, i387_convert_register_p);
3179 set_gdbarch_register_to_value (gdbarch, i387_register_to_value);
3180 set_gdbarch_value_to_register (gdbarch, i387_value_to_register);
3181
3182 set_gdbarch_return_value (gdbarch, amd64_return_value);
3183
3184 set_gdbarch_skip_prologue (gdbarch, amd64_skip_prologue);
3185
3186 tdep->record_regmap = amd64_record_regmap;
3187
3188 set_gdbarch_dummy_id (gdbarch, amd64_dummy_id);
3189
3190 /* Hook the function epilogue frame unwinder. This unwinder is
3191 appended to the list first, so that it supercedes the other
3192 unwinders in function epilogues. */
3193 frame_unwind_prepend_unwinder (gdbarch, &amd64_epilogue_frame_unwind);
3194
3195 /* Hook the prologue-based frame unwinders. */
3196 frame_unwind_append_unwinder (gdbarch, &amd64_sigtramp_frame_unwind);
3197 frame_unwind_append_unwinder (gdbarch, &amd64_frame_unwind);
3198 frame_base_set_default (gdbarch, &amd64_frame_base);
3199
3200 set_gdbarch_get_longjmp_target (gdbarch, amd64_get_longjmp_target);
3201
3202 set_gdbarch_relocate_instruction (gdbarch, amd64_relocate_instruction);
3203
3204 set_gdbarch_gen_return_address (gdbarch, amd64_gen_return_address);
3205
3206 /* SystemTap variables and functions. */
3207 set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
3208 set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
3209 set_gdbarch_stap_register_indirection_prefixes (gdbarch,
3210 stap_register_indirection_prefixes);
3211 set_gdbarch_stap_register_indirection_suffixes (gdbarch,
3212 stap_register_indirection_suffixes);
3213 set_gdbarch_stap_is_single_operand (gdbarch,
3214 i386_stap_is_single_operand);
3215 set_gdbarch_stap_parse_special_token (gdbarch,
3216 i386_stap_parse_special_token);
3217 set_gdbarch_insn_is_call (gdbarch, amd64_insn_is_call);
3218 set_gdbarch_insn_is_ret (gdbarch, amd64_insn_is_ret);
3219 set_gdbarch_insn_is_jump (gdbarch, amd64_insn_is_jump);
3220
3221 set_gdbarch_in_indirect_branch_thunk (gdbarch,
3222 amd64_in_indirect_branch_thunk);
3223 }
3224
3225 /* Initialize ARCH for x86-64, no osabi. */
3226
3227 static void
3228 amd64_none_init_abi (gdbarch_info info, gdbarch *arch)
3229 {
3230 amd64_init_abi (info, arch, amd64_target_description (X86_XSTATE_SSE_MASK));
3231 }
3232
3233 static struct type *
3234 amd64_x32_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
3235 {
3236 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3237
3238 switch (regnum - tdep->eax_regnum)
3239 {
3240 case AMD64_RBP_REGNUM: /* %ebp */
3241 case AMD64_RSP_REGNUM: /* %esp */
3242 return builtin_type (gdbarch)->builtin_data_ptr;
3243 case AMD64_RIP_REGNUM: /* %eip */
3244 return builtin_type (gdbarch)->builtin_func_ptr;
3245 }
3246
3247 return i386_pseudo_register_type (gdbarch, regnum);
3248 }
3249
3250 void
3251 amd64_x32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
3252 const target_desc *default_tdesc)
3253 {
3254 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3255
3256 amd64_init_abi (info, gdbarch, default_tdesc);
3257
3258 tdep->num_dword_regs = 17;
3259 set_tdesc_pseudo_register_type (gdbarch, amd64_x32_pseudo_register_type);
3260
3261 set_gdbarch_long_bit (gdbarch, 32);
3262 set_gdbarch_ptr_bit (gdbarch, 32);
3263 }
3264
3265 /* Initialize ARCH for x64-32, no osabi. */
3266
3267 static void
3268 amd64_x32_none_init_abi (gdbarch_info info, gdbarch *arch)
3269 {
3270 amd64_x32_init_abi (info, arch,
3271 amd64_target_description (X86_XSTATE_SSE_MASK));
3272 }
3273
3274 /* Return the target description for a specified XSAVE feature mask. */
3275
3276 const struct target_desc *
3277 amd64_target_description (uint64_t xcr0)
3278 {
3279 static target_desc *amd64_tdescs \
3280 [2/*AVX*/][2/*MPX*/][2/*AVX512*/][2/*PKRU*/] = {};
3281 target_desc **tdesc;
3282
3283 tdesc = &amd64_tdescs[(xcr0 & X86_XSTATE_AVX) ? 1 : 0]
3284 [(xcr0 & X86_XSTATE_MPX) ? 1 : 0]
3285 [(xcr0 & X86_XSTATE_AVX512) ? 1 : 0]
3286 [(xcr0 & X86_XSTATE_PKRU) ? 1 : 0];
3287
3288 if (*tdesc == NULL)
3289 *tdesc = amd64_create_target_description (xcr0, false, false);
3290
3291 return *tdesc;
3292 }
3293
3294 void
3295 _initialize_amd64_tdep (void)
3296 {
3297 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, GDB_OSABI_NONE,
3298 amd64_none_init_abi);
3299 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x64_32, GDB_OSABI_NONE,
3300 amd64_x32_none_init_abi);
3301
3302 #if GDB_SELF_TEST
3303 struct
3304 {
3305 const char *xml;
3306 uint64_t mask;
3307 } xml_masks[] = {
3308 { "i386/amd64.xml", X86_XSTATE_SSE_MASK },
3309 { "i386/amd64-avx.xml", X86_XSTATE_AVX_MASK },
3310 { "i386/amd64-mpx.xml", X86_XSTATE_MPX_MASK },
3311 { "i386/amd64-avx-mpx.xml", X86_XSTATE_AVX_MPX_MASK },
3312 { "i386/amd64-avx-avx512.xml", X86_XSTATE_AVX_AVX512_MASK },
3313 { "i386/amd64-avx-mpx-avx512-pku.xml",
3314 X86_XSTATE_AVX_MPX_AVX512_PKU_MASK },
3315 };
3316
3317 for (auto &a : xml_masks)
3318 {
3319 auto tdesc = amd64_target_description (a.mask);
3320
3321 selftests::record_xml_tdesc (a.xml, tdesc);
3322 }
3323 #endif /* GDB_SELF_TEST */
3324 }
3325 \f
3326
3327 /* The 64-bit FXSAVE format differs from the 32-bit format in the
3328 sense that the instruction pointer and data pointer are simply
3329 64-bit offsets into the code segment and the data segment instead
3330 of a selector offset pair. The functions below store the upper 32
3331 bits of these pointers (instead of just the 16-bits of the segment
3332 selector). */
3333
3334 /* Fill register REGNUM in REGCACHE with the appropriate
3335 floating-point or SSE register value from *FXSAVE. If REGNUM is
3336 -1, do this for all registers. This function masks off any of the
3337 reserved bits in *FXSAVE. */
3338
3339 void
3340 amd64_supply_fxsave (struct regcache *regcache, int regnum,
3341 const void *fxsave)
3342 {
3343 struct gdbarch *gdbarch = regcache->arch ();
3344 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3345
3346 i387_supply_fxsave (regcache, regnum, fxsave);
3347
3348 if (fxsave
3349 && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3350 {
3351 const gdb_byte *regs = (const gdb_byte *) fxsave;
3352
3353 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3354 regcache_raw_supply (regcache, I387_FISEG_REGNUM (tdep), regs + 12);
3355 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3356 regcache_raw_supply (regcache, I387_FOSEG_REGNUM (tdep), regs + 20);
3357 }
3358 }
3359
3360 /* Similar to amd64_supply_fxsave, but use XSAVE extended state. */
3361
3362 void
3363 amd64_supply_xsave (struct regcache *regcache, int regnum,
3364 const void *xsave)
3365 {
3366 struct gdbarch *gdbarch = regcache->arch ();
3367 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3368
3369 i387_supply_xsave (regcache, regnum, xsave);
3370
3371 if (xsave
3372 && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3373 {
3374 const gdb_byte *regs = (const gdb_byte *) xsave;
3375
3376 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3377 regcache_raw_supply (regcache, I387_FISEG_REGNUM (tdep),
3378 regs + 12);
3379 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3380 regcache_raw_supply (regcache, I387_FOSEG_REGNUM (tdep),
3381 regs + 20);
3382 }
3383 }
3384
3385 /* Fill register REGNUM (if it is a floating-point or SSE register) in
3386 *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
3387 all registers. This function doesn't touch any of the reserved
3388 bits in *FXSAVE. */
3389
3390 void
3391 amd64_collect_fxsave (const struct regcache *regcache, int regnum,
3392 void *fxsave)
3393 {
3394 struct gdbarch *gdbarch = regcache->arch ();
3395 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3396 gdb_byte *regs = (gdb_byte *) fxsave;
3397
3398 i387_collect_fxsave (regcache, regnum, fxsave);
3399
3400 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3401 {
3402 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3403 regcache_raw_collect (regcache, I387_FISEG_REGNUM (tdep), regs + 12);
3404 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3405 regcache_raw_collect (regcache, I387_FOSEG_REGNUM (tdep), regs + 20);
3406 }
3407 }
3408
3409 /* Similar to amd64_collect_fxsave, but use XSAVE extended state. */
3410
3411 void
3412 amd64_collect_xsave (const struct regcache *regcache, int regnum,
3413 void *xsave, int gcore)
3414 {
3415 struct gdbarch *gdbarch = regcache->arch ();
3416 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3417 gdb_byte *regs = (gdb_byte *) xsave;
3418
3419 i387_collect_xsave (regcache, regnum, xsave, gcore);
3420
3421 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3422 {
3423 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3424 regcache_raw_collect (regcache, I387_FISEG_REGNUM (tdep),
3425 regs + 12);
3426 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3427 regcache_raw_collect (regcache, I387_FOSEG_REGNUM (tdep),
3428 regs + 20);
3429 }
3430 }
This page took 0.105583 seconds and 4 git commands to generate.