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