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