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