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