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