gdb/amd64-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for AMD64.

  2.    Copyright (C) 2001-2015 Free Software Foundation, Inc.

  3.    Contributed by Jiri Smid, SuSE Labs.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "opcode/i386.h"
  17. #include "dis-asm.h"
  18. #include "arch-utils.h"
  19. #include "block.h"
  20. #include "dummy-frame.h"
  21. #include "frame.h"
  22. #include "frame-base.h"
  23. #include "frame-unwind.h"
  24. #include "inferior.h"
  25. #include "infrun.h"
  26. #include "gdbcmd.h"
  27. #include "gdbcore.h"
  28. #include "objfiles.h"
  29. #include "regcache.h"
  30. #include "regset.h"
  31. #include "symfile.h"
  32. #include "disasm.h"
  33. #include "amd64-tdep.h"
  34. #include "i387-tdep.h"

  35. #include "features/i386/amd64.c"
  36. #include "features/i386/amd64-avx.c"
  37. #include "features/i386/amd64-mpx.c"
  38. #include "features/i386/amd64-avx512.c"

  39. #include "features/i386/x32.c"
  40. #include "features/i386/x32-avx.c"
  41. #include "features/i386/x32-avx512.c"

  42. #include "ax.h"
  43. #include "ax-gdb.h"

  44. /* Note that the AMD64 architecture was previously known as x86-64.
  45.    The latter is (forever) engraved into the canonical system name as
  46.    returned by config.guess, and used as the name for the AMD64 port
  47.    of GNU/Linux.  The BSD's have renamed their ports to amd64; they
  48.    don't like to shout.  For GDB we prefer the amd64_-prefix over the
  49.    x86_64_-prefix since it's so much easier to type.  */

  50. /* Register information.  */

  51. static const char *amd64_register_names[] =
  52. {
  53.   "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp",

  54.   /* %r8 is indeed register number 8.  */
  55.   "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  56.   "rip", "eflags", "cs", "ss", "ds", "es", "fs", "gs",

  57.   /* %st0 is register number 24.  */
  58.   "st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7",
  59.   "fctrl", "fstat", "ftag", "fiseg", "fioff", "foseg", "fooff", "fop",

  60.   /* %xmm0 is register number 40.  */
  61.   "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
  62.   "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
  63.   "mxcsr",
  64. };

  65. static const char *amd64_ymm_names[] =
  66. {
  67.   "ymm0", "ymm1", "ymm2", "ymm3",
  68.   "ymm4", "ymm5", "ymm6", "ymm7",
  69.   "ymm8", "ymm9", "ymm10", "ymm11",
  70.   "ymm12", "ymm13", "ymm14", "ymm15"
  71. };

  72. static const char *amd64_ymm_avx512_names[] =
  73. {
  74.   "ymm16", "ymm17", "ymm18", "ymm19",
  75.   "ymm20", "ymm21", "ymm22", "ymm23",
  76.   "ymm24", "ymm25", "ymm26", "ymm27",
  77.   "ymm28", "ymm29", "ymm30", "ymm31"
  78. };

  79. static const char *amd64_ymmh_names[] =
  80. {
  81.   "ymm0h", "ymm1h", "ymm2h", "ymm3h",
  82.   "ymm4h", "ymm5h", "ymm6h", "ymm7h",
  83.   "ymm8h", "ymm9h", "ymm10h", "ymm11h",
  84.   "ymm12h", "ymm13h", "ymm14h", "ymm15h"
  85. };

  86. static const char *amd64_ymmh_avx512_names[] =
  87. {
  88.   "ymm16h", "ymm17h", "ymm18h", "ymm19h",
  89.   "ymm20h", "ymm21h", "ymm22h", "ymm23h",
  90.   "ymm24h", "ymm25h", "ymm26h", "ymm27h",
  91.   "ymm28h", "ymm29h", "ymm30h", "ymm31h"
  92. };

  93. static const char *amd64_mpx_names[] =
  94. {
  95.   "bnd0raw", "bnd1raw", "bnd2raw", "bnd3raw", "bndcfgu", "bndstatus"
  96. };

  97. static const char *amd64_k_names[] =
  98. {
  99.   "k0", "k1", "k2", "k3",
  100.   "k4", "k5", "k6", "k7"
  101. };

  102. static const char *amd64_zmmh_names[] =
  103. {
  104.   "zmm0h", "zmm1h", "zmm2h", "zmm3h",
  105.   "zmm4h", "zmm5h", "zmm6h", "zmm7h",
  106.   "zmm8h", "zmm9h", "zmm10h", "zmm11h",
  107.   "zmm12h", "zmm13h", "zmm14h", "zmm15h",
  108.   "zmm16h", "zmm17h", "zmm18h", "zmm19h",
  109.   "zmm20h", "zmm21h", "zmm22h", "zmm23h",
  110.   "zmm24h", "zmm25h", "zmm26h", "zmm27h",
  111.   "zmm28h", "zmm29h", "zmm30h", "zmm31h"
  112. };

  113. static const char *amd64_zmm_names[] =
  114. {
  115.   "zmm0", "zmm1", "zmm2", "zmm3",
  116.   "zmm4", "zmm5", "zmm6", "zmm7",
  117.   "zmm8", "zmm9", "zmm10", "zmm11",
  118.   "zmm12", "zmm13", "zmm14", "zmm15",
  119.   "zmm16", "zmm17", "zmm18", "zmm19",
  120.   "zmm20", "zmm21", "zmm22", "zmm23",
  121.   "zmm24", "zmm25", "zmm26", "zmm27",
  122.   "zmm28", "zmm29", "zmm30", "zmm31"
  123. };

  124. static const char *amd64_xmm_avx512_names[] = {
  125.     "xmm16""xmm17""xmm18""xmm19",
  126.     "xmm20""xmm21""xmm22""xmm23",
  127.     "xmm24""xmm25""xmm26""xmm27",
  128.     "xmm28""xmm29""xmm30""xmm31"
  129. };

  130. /* DWARF Register Number Mapping as defined in the System V psABI,
  131.    section 3.6.  */

  132. static int amd64_dwarf_regmap[] =
  133. {
  134.   /* General Purpose Registers RAX, RDX, RCX, RBX, RSI, RDI.  */
  135.   AMD64_RAX_REGNUM, AMD64_RDX_REGNUM,
  136.   AMD64_RCX_REGNUM, AMD64_RBX_REGNUM,
  137.   AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,

  138.   /* Frame Pointer Register RBP.  */
  139.   AMD64_RBP_REGNUM,

  140.   /* Stack Pointer Register RSP.  */
  141.   AMD64_RSP_REGNUM,

  142.   /* Extended Integer Registers 8 - 15.  */
  143.   AMD64_R8_REGNUM,                /* %r8 */
  144.   AMD64_R9_REGNUM,                /* %r9 */
  145.   AMD64_R10_REGNUM,                /* %r10 */
  146.   AMD64_R11_REGNUM,                /* %r11 */
  147.   AMD64_R12_REGNUM,                /* %r12 */
  148.   AMD64_R13_REGNUM,                /* %r13 */
  149.   AMD64_R14_REGNUM,                /* %r14 */
  150.   AMD64_R15_REGNUM,                /* %r15 */

  151.   /* Return Address RA.  Mapped to RIP.  */
  152.   AMD64_RIP_REGNUM,

  153.   /* SSE Registers 0 - 7.  */
  154.   AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
  155.   AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
  156.   AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
  157.   AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,

  158.   /* Extended SSE Registers 8 - 15.  */
  159.   AMD64_XMM0_REGNUM + 8, AMD64_XMM0_REGNUM + 9,
  160.   AMD64_XMM0_REGNUM + 10, AMD64_XMM0_REGNUM + 11,
  161.   AMD64_XMM0_REGNUM + 12, AMD64_XMM0_REGNUM + 13,
  162.   AMD64_XMM0_REGNUM + 14, AMD64_XMM0_REGNUM + 15,

  163.   /* Floating Point Registers 0-7.  */
  164.   AMD64_ST0_REGNUM + 0, AMD64_ST0_REGNUM + 1,
  165.   AMD64_ST0_REGNUM + 2, AMD64_ST0_REGNUM + 3,
  166.   AMD64_ST0_REGNUM + 4, AMD64_ST0_REGNUM + 5,
  167.   AMD64_ST0_REGNUM + 6, AMD64_ST0_REGNUM + 7,

  168.   /* MMX Registers 0 - 7.
  169.      We have to handle those registers specifically, as their register
  170.      number within GDB depends on the target (or they may even not be
  171.      available at all).  */
  172.   -1, -1, -1, -1, -1, -1, -1, -1,

  173.   /* Control and Status Flags Register.  */
  174.   AMD64_EFLAGS_REGNUM,

  175.   /* Selector Registers.  */
  176.   AMD64_ES_REGNUM,
  177.   AMD64_CS_REGNUM,
  178.   AMD64_SS_REGNUM,
  179.   AMD64_DS_REGNUM,
  180.   AMD64_FS_REGNUM,
  181.   AMD64_GS_REGNUM,
  182.   -1,
  183.   -1,

  184.   /* Segment Base Address Registers.  */
  185.   -1,
  186.   -1,
  187.   -1,
  188.   -1,

  189.   /* Special Selector Registers.  */
  190.   -1,
  191.   -1,

  192.   /* Floating Point Control Registers.  */
  193.   AMD64_MXCSR_REGNUM,
  194.   AMD64_FCTRL_REGNUM,
  195.   AMD64_FSTAT_REGNUM
  196. };

  197. static const int amd64_dwarf_regmap_len =
  198.   (sizeof (amd64_dwarf_regmap) / sizeof (amd64_dwarf_regmap[0]));

  199. /* Convert DWARF register number REG to the appropriate register
  200.    number used by GDB.  */

  201. static int
  202. amd64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  203. {
  204.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  205.   int ymm0_regnum = tdep->ymm0_regnum;
  206.   int regnum = -1;

  207.   if (reg >= 0 && reg < amd64_dwarf_regmap_len)
  208.     regnum = amd64_dwarf_regmap[reg];

  209.   if (regnum == -1)
  210.     warning (_("Unmapped DWARF Register #%d encountered."), reg);
  211.   else if (ymm0_regnum >= 0
  212.            && i386_xmm_regnum_p (gdbarch, regnum))
  213.     regnum += ymm0_regnum - I387_XMM0_REGNUM (tdep);

  214.   return regnum;
  215. }

  216. /* Map architectural register numbers to gdb register numbers.  */

  217. static const int amd64_arch_regmap[16] =
  218. {
  219.   AMD64_RAX_REGNUM,        /* %rax */
  220.   AMD64_RCX_REGNUM,        /* %rcx */
  221.   AMD64_RDX_REGNUM,        /* %rdx */
  222.   AMD64_RBX_REGNUM,        /* %rbx */
  223.   AMD64_RSP_REGNUM,        /* %rsp */
  224.   AMD64_RBP_REGNUM,        /* %rbp */
  225.   AMD64_RSI_REGNUM,        /* %rsi */
  226.   AMD64_RDI_REGNUM,        /* %rdi */
  227.   AMD64_R8_REGNUM,        /* %r8 */
  228.   AMD64_R9_REGNUM,        /* %r9 */
  229.   AMD64_R10_REGNUM,        /* %r10 */
  230.   AMD64_R11_REGNUM,        /* %r11 */
  231.   AMD64_R12_REGNUM,        /* %r12 */
  232.   AMD64_R13_REGNUM,        /* %r13 */
  233.   AMD64_R14_REGNUM,        /* %r14 */
  234.   AMD64_R15_REGNUM        /* %r15 */
  235. };

  236. static const int amd64_arch_regmap_len =
  237.   (sizeof (amd64_arch_regmap) / sizeof (amd64_arch_regmap[0]));

  238. /* Convert architectural register number REG to the appropriate register
  239.    number used by GDB.  */

  240. static int
  241. amd64_arch_reg_to_regnum (int reg)
  242. {
  243.   gdb_assert (reg >= 0 && reg < amd64_arch_regmap_len);

  244.   return amd64_arch_regmap[reg];
  245. }

  246. /* Register names for byte pseudo-registers.  */

  247. static const char *amd64_byte_names[] =
  248. {
  249.   "al", "bl", "cl", "dl", "sil", "dil", "bpl", "spl",
  250.   "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l",
  251.   "ah", "bh", "ch", "dh"
  252. };

  253. /* Number of lower byte registers.  */
  254. #define AMD64_NUM_LOWER_BYTE_REGS 16

  255. /* Register names for word pseudo-registers.  */

  256. static const char *amd64_word_names[] =
  257. {
  258.   "ax", "bx", "cx", "dx", "si", "di", "bp", "",
  259.   "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
  260. };

  261. /* Register names for dword pseudo-registers.  */

  262. static const char *amd64_dword_names[] =
  263. {
  264.   "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp",
  265.   "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
  266.   "eip"
  267. };

  268. /* Return the name of register REGNUM.  */

  269. static const char *
  270. amd64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
  271. {
  272.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  273.   if (i386_byte_regnum_p (gdbarch, regnum))
  274.     return amd64_byte_names[regnum - tdep->al_regnum];
  275.   else if (i386_zmm_regnum_p (gdbarch, regnum))
  276.     return amd64_zmm_names[regnum - tdep->zmm0_regnum];
  277.   else if (i386_ymm_regnum_p (gdbarch, regnum))
  278.     return amd64_ymm_names[regnum - tdep->ymm0_regnum];
  279.   else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
  280.     return amd64_ymm_avx512_names[regnum - tdep->ymm16_regnum];
  281.   else if (i386_word_regnum_p (gdbarch, regnum))
  282.     return amd64_word_names[regnum - tdep->ax_regnum];
  283.   else if (i386_dword_regnum_p (gdbarch, regnum))
  284.     return amd64_dword_names[regnum - tdep->eax_regnum];
  285.   else
  286.     return i386_pseudo_register_name (gdbarch, regnum);
  287. }

  288. static struct value *
  289. amd64_pseudo_register_read_value (struct gdbarch *gdbarch,
  290.                                   struct regcache *regcache,
  291.                                   int regnum)
  292. {
  293.   gdb_byte raw_buf[MAX_REGISTER_SIZE];
  294.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  295.   enum register_status status;
  296.   struct value *result_value;
  297.   gdb_byte *buf;

  298.   result_value = allocate_value (register_type (gdbarch, regnum));
  299.   VALUE_LVAL (result_value) = lval_register;
  300.   VALUE_REGNUM (result_value) = regnum;
  301.   buf = value_contents_raw (result_value);

  302.   if (i386_byte_regnum_p (gdbarch, regnum))
  303.     {
  304.       int gpnum = regnum - tdep->al_regnum;

  305.       /* Extract (always little endian).  */
  306.       if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
  307.         {
  308.           /* Special handling for AH, BH, CH, DH.  */
  309.           status = regcache_raw_read (regcache,
  310.                                       gpnum - AMD64_NUM_LOWER_BYTE_REGS,
  311.                                       raw_buf);
  312.           if (status == REG_VALID)
  313.             memcpy (buf, raw_buf + 1, 1);
  314.           else
  315.             mark_value_bytes_unavailable (result_value, 0,
  316.                                           TYPE_LENGTH (value_type (result_value)));
  317.         }
  318.       else
  319.         {
  320.           status = regcache_raw_read (regcache, gpnum, raw_buf);
  321.           if (status == REG_VALID)
  322.             memcpy (buf, raw_buf, 1);
  323.           else
  324.             mark_value_bytes_unavailable (result_value, 0,
  325.                                           TYPE_LENGTH (value_type (result_value)));
  326.         }
  327.     }
  328.   else if (i386_dword_regnum_p (gdbarch, regnum))
  329.     {
  330.       int gpnum = regnum - tdep->eax_regnum;
  331.       /* Extract (always little endian).  */
  332.       status = regcache_raw_read (regcache, gpnum, raw_buf);
  333.       if (status == REG_VALID)
  334.         memcpy (buf, raw_buf, 4);
  335.       else
  336.         mark_value_bytes_unavailable (result_value, 0,
  337.                                       TYPE_LENGTH (value_type (result_value)));
  338.     }
  339.   else
  340.     i386_pseudo_register_read_into_value (gdbarch, regcache, regnum,
  341.                                           result_value);

  342.   return result_value;
  343. }

  344. static void
  345. amd64_pseudo_register_write (struct gdbarch *gdbarch,
  346.                              struct regcache *regcache,
  347.                              int regnum, const gdb_byte *buf)
  348. {
  349.   gdb_byte raw_buf[MAX_REGISTER_SIZE];
  350.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  351.   if (i386_byte_regnum_p (gdbarch, regnum))
  352.     {
  353.       int gpnum = regnum - tdep->al_regnum;

  354.       if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
  355.         {
  356.           /* Read ... AH, BH, CH, DH.  */
  357.           regcache_raw_read (regcache,
  358.                              gpnum - AMD64_NUM_LOWER_BYTE_REGS, raw_buf);
  359.           /* ... Modify ... (always little endian).  */
  360.           memcpy (raw_buf + 1, buf, 1);
  361.           /* ... Write.  */
  362.           regcache_raw_write (regcache,
  363.                               gpnum - AMD64_NUM_LOWER_BYTE_REGS, raw_buf);
  364.         }
  365.       else
  366.         {
  367.           /* Read ...  */
  368.           regcache_raw_read (regcache, gpnum, raw_buf);
  369.           /* ... Modify ... (always little endian).  */
  370.           memcpy (raw_buf, buf, 1);
  371.           /* ... Write.  */
  372.           regcache_raw_write (regcache, gpnum, raw_buf);
  373.         }
  374.     }
  375.   else if (i386_dword_regnum_p (gdbarch, regnum))
  376.     {
  377.       int gpnum = regnum - tdep->eax_regnum;

  378.       /* Read ...  */
  379.       regcache_raw_read (regcache, gpnum, raw_buf);
  380.       /* ... Modify ... (always little endian).  */
  381.       memcpy (raw_buf, buf, 4);
  382.       /* ... Write.  */
  383.       regcache_raw_write (regcache, gpnum, raw_buf);
  384.     }
  385.   else
  386.     i386_pseudo_register_write (gdbarch, regcache, regnum, buf);
  387. }



  388. /* Register classes as defined in the psABI.  */

  389. enum amd64_reg_class
  390. {
  391.   AMD64_INTEGER,
  392.   AMD64_SSE,
  393.   AMD64_SSEUP,
  394.   AMD64_X87,
  395.   AMD64_X87UP,
  396.   AMD64_COMPLEX_X87,
  397.   AMD64_NO_CLASS,
  398.   AMD64_MEMORY
  399. };

  400. /* Return the union class of CLASS1 and CLASS2.  See the psABI for
  401.    details.  */

  402. static enum amd64_reg_class
  403. amd64_merge_classes (enum amd64_reg_class class1, enum amd64_reg_class class2)
  404. {
  405.   /* Rule (a): If both classes are equal, this is the resulting class.  */
  406.   if (class1 == class2)
  407.     return class1;

  408.   /* Rule (b): If one of the classes is NO_CLASS, the resulting class
  409.      is the other class.  */
  410.   if (class1 == AMD64_NO_CLASS)
  411.     return class2;
  412.   if (class2 == AMD64_NO_CLASS)
  413.     return class1;

  414.   /* Rule (c): If one of the classes is MEMORY, the result is MEMORY.  */
  415.   if (class1 == AMD64_MEMORY || class2 == AMD64_MEMORY)
  416.     return AMD64_MEMORY;

  417.   /* Rule (d): If one of the classes is INTEGER, the result is INTEGER.  */
  418.   if (class1 == AMD64_INTEGER || class2 == AMD64_INTEGER)
  419.     return AMD64_INTEGER;

  420.   /* Rule (e): If one of the classes is X87, X87UP, COMPLEX_X87 class,
  421.      MEMORY is used as class.  */
  422.   if (class1 == AMD64_X87 || class1 == AMD64_X87UP
  423.       || class1 == AMD64_COMPLEX_X87 || class2 == AMD64_X87
  424.       || class2 == AMD64_X87UP || class2 == AMD64_COMPLEX_X87)
  425.     return AMD64_MEMORY;

  426.   /* Rule (f): Otherwise class SSE is used.  */
  427.   return AMD64_SSE;
  428. }

  429. static void amd64_classify (struct type *type, enum amd64_reg_class class[2]);

  430. /* Return non-zero if TYPE is a non-POD structure or union type.  */

  431. static int
  432. amd64_non_pod_p (struct type *type)
  433. {
  434.   /* ??? A class with a base class certainly isn't POD, but does this
  435.      catch all non-POD structure types?  */
  436.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_N_BASECLASSES (type) > 0)
  437.     return 1;

  438.   return 0;
  439. }

  440. /* Classify TYPE according to the rules for aggregate (structures and
  441.    arrays) and union types, and store the result in CLASS.  */

  442. static void
  443. amd64_classify_aggregate (struct type *type, enum amd64_reg_class class[2])
  444. {
  445.   /* 1. If the size of an object is larger than two eightbytes, or in
  446.         C++, is a non-POD structure or union type, or contains
  447.         unaligned fields, it has class memory.  */
  448.   if (TYPE_LENGTH (type) > 16 || amd64_non_pod_p (type))
  449.     {
  450.       class[0] = class[1] = AMD64_MEMORY;
  451.       return;
  452.     }

  453.   /* 2. Both eightbytes get initialized to class NO_CLASS.  */
  454.   class[0] = class[1] = AMD64_NO_CLASS;

  455.   /* 3. Each field of an object is classified recursively so that
  456.         always two fields are considered. The resulting class is
  457.         calculated according to the classes of the fields in the
  458.         eightbyte: */

  459.   if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
  460.     {
  461.       struct type *subtype = check_typedef (TYPE_TARGET_TYPE (type));

  462.       /* All fields in an array have the same type.  */
  463.       amd64_classify (subtype, class);
  464.       if (TYPE_LENGTH (type) > 8 && class[1] == AMD64_NO_CLASS)
  465.         class[1] = class[0];
  466.     }
  467.   else
  468.     {
  469.       int i;

  470.       /* Structure or union.  */
  471.       gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
  472.                   || TYPE_CODE (type) == TYPE_CODE_UNION);

  473.       for (i = 0; i < TYPE_NFIELDS (type); i++)
  474.         {
  475.           struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
  476.           int pos = TYPE_FIELD_BITPOS (type, i) / 64;
  477.           enum amd64_reg_class subclass[2];
  478.           int bitsize = TYPE_FIELD_BITSIZE (type, i);
  479.           int endpos;

  480.           if (bitsize == 0)
  481.             bitsize = TYPE_LENGTH (subtype) * 8;
  482.           endpos = (TYPE_FIELD_BITPOS (type, i) + bitsize - 1) / 64;

  483.           /* Ignore static fields.  */
  484.           if (field_is_static (&TYPE_FIELD (type, i)))
  485.             continue;

  486.           gdb_assert (pos == 0 || pos == 1);

  487.           amd64_classify (subtype, subclass);
  488.           class[pos] = amd64_merge_classes (class[pos], subclass[0]);
  489.           if (bitsize <= 64 && pos == 0 && endpos == 1)
  490.             /* This is a bit of an odd case:  We have a field that would
  491.                normally fit in one of the two eightbytes, except that
  492.                it is placed in a way that this field straddles them.
  493.                This has been seen with a structure containing an array.

  494.                The ABI is a bit unclear in this case, but we assume that
  495.                this field's class (stored in subclass[0]) must also be merged
  496.                into class[1].  In other words, our field has a piece stored
  497.                in the second eight-byte, and thus its class applies to
  498.                the second eight-byte as well.

  499.                In the case where the field length exceeds 8 bytes,
  500.                it should not be necessary to merge the field class
  501.                into class[1].  As LEN > 8, subclass[1] is necessarily
  502.                different from AMD64_NO_CLASS.  If subclass[1] is equal
  503.                to subclass[0], then the normal class[1]/subclass[1]
  504.                merging will take care of everything.  For subclass[1]
  505.                to be different from subclass[0], I can only see the case
  506.                where we have a SSE/SSEUP or X87/X87UP pair, which both
  507.                use up all 16 bytes of the aggregate, and are already
  508.                handled just fine (because each portion sits on its own
  509.                8-byte).  */
  510.             class[1] = amd64_merge_classes (class[1], subclass[0]);
  511.           if (pos == 0)
  512.             class[1] = amd64_merge_classes (class[1], subclass[1]);
  513.         }
  514.     }

  515.   /* 4. Then a post merger cleanup is done:  */

  516.   /* Rule (a): If one of the classes is MEMORY, the whole argument is
  517.      passed in memory.  */
  518.   if (class[0] == AMD64_MEMORY || class[1] == AMD64_MEMORY)
  519.     class[0] = class[1] = AMD64_MEMORY;

  520.   /* Rule (b): If SSEUP is not preceded by SSE, it is converted to
  521.      SSE.  */
  522.   if (class[0] == AMD64_SSEUP)
  523.     class[0] = AMD64_SSE;
  524.   if (class[1] == AMD64_SSEUP && class[0] != AMD64_SSE)
  525.     class[1] = AMD64_SSE;
  526. }

  527. /* Classify TYPE, and store the result in CLASS.  */

  528. static void
  529. amd64_classify (struct type *type, enum amd64_reg_class class[2])
  530. {
  531.   enum type_code code = TYPE_CODE (type);
  532.   int len = TYPE_LENGTH (type);

  533.   class[0] = class[1] = AMD64_NO_CLASS;

  534.   /* Arguments of types (signed and unsigned) _Bool, char, short, int,
  535.      long, long long, and pointers are in the INTEGER class.  Similarly,
  536.      range types, used by languages such as Ada, are also in the INTEGER
  537.      class.  */
  538.   if ((code == TYPE_CODE_INT || code == TYPE_CODE_ENUM
  539.        || code == TYPE_CODE_BOOL || code == TYPE_CODE_RANGE
  540.        || code == TYPE_CODE_CHAR
  541.        || code == TYPE_CODE_PTR || code == TYPE_CODE_REF)
  542.       && (len == 1 || len == 2 || len == 4 || len == 8))
  543.     class[0] = AMD64_INTEGER;

  544.   /* Arguments of types float, double, _Decimal32, _Decimal64 and __m64
  545.      are in class SSE.  */
  546.   else if ((code == TYPE_CODE_FLT || code == TYPE_CODE_DECFLOAT)
  547.            && (len == 4 || len == 8))
  548.     /* FIXME: __m64 .  */
  549.     class[0] = AMD64_SSE;

  550.   /* Arguments of types __float128, _Decimal128 and __m128 are split into
  551.      two halves.  The least significant ones belong to class SSE, the most
  552.      significant one to class SSEUP.  */
  553.   else if (code == TYPE_CODE_DECFLOAT && len == 16)
  554.     /* FIXME: __float128, __m128.  */
  555.     class[0] = AMD64_SSE, class[1] = AMD64_SSEUP;

  556.   /* The 64-bit mantissa of arguments of type long double belongs to
  557.      class X87, the 16-bit exponent plus 6 bytes of padding belongs to
  558.      class X87UP.  */
  559.   else if (code == TYPE_CODE_FLT && len == 16)
  560.     /* Class X87 and X87UP.  */
  561.     class[0] = AMD64_X87, class[1] = AMD64_X87UP;

  562.   /* Arguments of complex T where T is one of the types float or
  563.      double get treated as if they are implemented as:

  564.      struct complexT {
  565.        T real;
  566.        T imag;
  567.      };

  568.   */
  569.   else if (code == TYPE_CODE_COMPLEX && len == 8)
  570.     class[0] = AMD64_SSE;
  571.   else if (code == TYPE_CODE_COMPLEX && len == 16)
  572.     class[0] = class[1] = AMD64_SSE;

  573.   /* A variable of type complex long double is classified as type
  574.      COMPLEX_X87.  */
  575.   else if (code == TYPE_CODE_COMPLEX && len == 32)
  576.     class[0] = AMD64_COMPLEX_X87;

  577.   /* Aggregates.  */
  578.   else if (code == TYPE_CODE_ARRAY || code == TYPE_CODE_STRUCT
  579.            || code == TYPE_CODE_UNION)
  580.     amd64_classify_aggregate (type, class);
  581. }

  582. static enum return_value_convention
  583. amd64_return_value (struct gdbarch *gdbarch, struct value *function,
  584.                     struct type *type, struct regcache *regcache,
  585.                     gdb_byte *readbuf, const gdb_byte *writebuf)
  586. {
  587.   enum amd64_reg_class class[2];
  588.   int len = TYPE_LENGTH (type);
  589.   static int integer_regnum[] = { AMD64_RAX_REGNUM, AMD64_RDX_REGNUM };
  590.   static int sse_regnum[] = { AMD64_XMM0_REGNUM, AMD64_XMM1_REGNUM };
  591.   int integer_reg = 0;
  592.   int sse_reg = 0;
  593.   int i;

  594.   gdb_assert (!(readbuf && writebuf));

  595.   /* 1. Classify the return type with the classification algorithm.  */
  596.   amd64_classify (type, class);

  597.   /* 2. If the type has class MEMORY, then the caller provides space
  598.      for the return value and passes the address of this storage in
  599.      %rdi as if it were the first argument to the function.  In effect,
  600.      this address becomes a hidden first argument.

  601.      On return %rax will contain the address that has been passed in
  602.      by the caller in %rdi.  */
  603.   if (class[0] == AMD64_MEMORY)
  604.     {
  605.       /* As indicated by the comment above, the ABI guarantees that we
  606.          can always find the return value just after the function has
  607.          returned.  */

  608.       if (readbuf)
  609.         {
  610.           ULONGEST addr;

  611.           regcache_raw_read_unsigned (regcache, AMD64_RAX_REGNUM, &addr);
  612.           read_memory (addr, readbuf, TYPE_LENGTH (type));
  613.         }

  614.       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
  615.     }

  616.   /* 8. If the class is COMPLEX_X87, the real part of the value is
  617.         returned in %st0 and the imaginary part in %st1.  */
  618.   if (class[0] == AMD64_COMPLEX_X87)
  619.     {
  620.       if (readbuf)
  621.         {
  622.           regcache_raw_read (regcache, AMD64_ST0_REGNUM, readbuf);
  623.           regcache_raw_read (regcache, AMD64_ST1_REGNUM, readbuf + 16);
  624.         }

  625.       if (writebuf)
  626.         {
  627.           i387_return_value (gdbarch, regcache);
  628.           regcache_raw_write (regcache, AMD64_ST0_REGNUM, writebuf);
  629.           regcache_raw_write (regcache, AMD64_ST1_REGNUM, writebuf + 16);

  630.           /* Fix up the tag word such that both %st(0) and %st(1) are
  631.              marked as valid.  */
  632.           regcache_raw_write_unsigned (regcache, AMD64_FTAG_REGNUM, 0xfff);
  633.         }

  634.       return RETURN_VALUE_REGISTER_CONVENTION;
  635.     }

  636.   gdb_assert (class[1] != AMD64_MEMORY);
  637.   gdb_assert (len <= 16);

  638.   for (i = 0; len > 0; i++, len -= 8)
  639.     {
  640.       int regnum = -1;
  641.       int offset = 0;

  642.       switch (class[i])
  643.         {
  644.         case AMD64_INTEGER:
  645.           /* 3. If the class is INTEGER, the next available register
  646.              of the sequence %rax, %rdx is used.  */
  647.           regnum = integer_regnum[integer_reg++];
  648.           break;

  649.         case AMD64_SSE:
  650.           /* 4. If the class is SSE, the next available SSE register
  651.              of the sequence %xmm0, %xmm1 is used.  */
  652.           regnum = sse_regnum[sse_reg++];
  653.           break;

  654.         case AMD64_SSEUP:
  655.           /* 5. If the class is SSEUP, the eightbyte is passed in the
  656.              upper half of the last used SSE register.  */
  657.           gdb_assert (sse_reg > 0);
  658.           regnum = sse_regnum[sse_reg - 1];
  659.           offset = 8;
  660.           break;

  661.         case AMD64_X87:
  662.           /* 6. If the class is X87, the value is returned on the X87
  663.              stack in %st0 as 80-bit x87 number.  */
  664.           regnum = AMD64_ST0_REGNUM;
  665.           if (writebuf)
  666.             i387_return_value (gdbarch, regcache);
  667.           break;

  668.         case AMD64_X87UP:
  669.           /* 7. If the class is X87UP, the value is returned together
  670.              with the previous X87 value in %st0.  */
  671.           gdb_assert (i > 0 && class[0] == AMD64_X87);
  672.           regnum = AMD64_ST0_REGNUM;
  673.           offset = 8;
  674.           len = 2;
  675.           break;

  676.         case AMD64_NO_CLASS:
  677.           continue;

  678.         default:
  679.           gdb_assert (!"Unexpected register class.");
  680.         }

  681.       gdb_assert (regnum != -1);

  682.       if (readbuf)
  683.         regcache_raw_read_part (regcache, regnum, offset, min (len, 8),
  684.                                 readbuf + i * 8);
  685.       if (writebuf)
  686.         regcache_raw_write_part (regcache, regnum, offset, min (len, 8),
  687.                                  writebuf + i * 8);
  688.     }

  689.   return RETURN_VALUE_REGISTER_CONVENTION;
  690. }


  691. static CORE_ADDR
  692. amd64_push_arguments (struct regcache *regcache, int nargs,
  693.                       struct value **args, CORE_ADDR sp, int struct_return)
  694. {
  695.   static int integer_regnum[] =
  696.   {
  697.     AMD64_RDI_REGNUM,                /* %rdi */
  698.     AMD64_RSI_REGNUM,                /* %rsi */
  699.     AMD64_RDX_REGNUM,                /* %rdx */
  700.     AMD64_RCX_REGNUM,                /* %rcx */
  701.     AMD64_R8_REGNUM,                /* %r8 */
  702.     AMD64_R9_REGNUM                /* %r9 */
  703.   };
  704.   static int sse_regnum[] =
  705.   {
  706.     /* %xmm0 ... %xmm7 */
  707.     AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
  708.     AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
  709.     AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
  710.     AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
  711.   };
  712.   struct value **stack_args = alloca (nargs * sizeof (struct value *));
  713.   int num_stack_args = 0;
  714.   int num_elements = 0;
  715.   int element = 0;
  716.   int integer_reg = 0;
  717.   int sse_reg = 0;
  718.   int i;

  719.   /* Reserve a register for the "hidden" argument.  */
  720.   if (struct_return)
  721.     integer_reg++;

  722.   for (i = 0; i < nargs; i++)
  723.     {
  724.       struct type *type = value_type (args[i]);
  725.       int len = TYPE_LENGTH (type);
  726.       enum amd64_reg_class class[2];
  727.       int needed_integer_regs = 0;
  728.       int needed_sse_regs = 0;
  729.       int j;

  730.       /* Classify argument.  */
  731.       amd64_classify (type, class);

  732.       /* Calculate the number of integer and SSE registers needed for
  733.          this argument.  */
  734.       for (j = 0; j < 2; j++)
  735.         {
  736.           if (class[j] == AMD64_INTEGER)
  737.             needed_integer_regs++;
  738.           else if (class[j] == AMD64_SSE)
  739.             needed_sse_regs++;
  740.         }

  741.       /* Check whether enough registers are available, and if the
  742.          argument should be passed in registers at all.  */
  743.       if (integer_reg + needed_integer_regs > ARRAY_SIZE (integer_regnum)
  744.           || sse_reg + needed_sse_regs > ARRAY_SIZE (sse_regnum)
  745.           || (needed_integer_regs == 0 && needed_sse_regs == 0))
  746.         {
  747.           /* The argument will be passed on the stack.  */
  748.           num_elements += ((len + 7) / 8);
  749.           stack_args[num_stack_args++] = args[i];
  750.         }
  751.       else
  752.         {
  753.           /* The argument will be passed in registers.  */
  754.           const gdb_byte *valbuf = value_contents (args[i]);
  755.           gdb_byte buf[8];

  756.           gdb_assert (len <= 16);

  757.           for (j = 0; len > 0; j++, len -= 8)
  758.             {
  759.               int regnum = -1;
  760.               int offset = 0;

  761.               switch (class[j])
  762.                 {
  763.                 case AMD64_INTEGER:
  764.                   regnum = integer_regnum[integer_reg++];
  765.                   break;

  766.                 case AMD64_SSE:
  767.                   regnum = sse_regnum[sse_reg++];
  768.                   break;

  769.                 case AMD64_SSEUP:
  770.                   gdb_assert (sse_reg > 0);
  771.                   regnum = sse_regnum[sse_reg - 1];
  772.                   offset = 8;
  773.                   break;

  774.                 default:
  775.                   gdb_assert (!"Unexpected register class.");
  776.                 }

  777.               gdb_assert (regnum != -1);
  778.               memset (buf, 0, sizeof buf);
  779.               memcpy (buf, valbuf + j * 8, min (len, 8));
  780.               regcache_raw_write_part (regcache, regnum, offset, 8, buf);
  781.             }
  782.         }
  783.     }

  784.   /* Allocate space for the arguments on the stack.  */
  785.   sp -= num_elements * 8;

  786.   /* The psABI says that "The end of the input argument area shall be
  787.      aligned on a 16 byte boundary."  */
  788.   sp &= ~0xf;

  789.   /* Write out the arguments to the stack.  */
  790.   for (i = 0; i < num_stack_args; i++)
  791.     {
  792.       struct type *type = value_type (stack_args[i]);
  793.       const gdb_byte *valbuf = value_contents (stack_args[i]);
  794.       int len = TYPE_LENGTH (type);

  795.       write_memory (sp + element * 8, valbuf, len);
  796.       element += ((len + 7) / 8);
  797.     }

  798.   /* The psABI says that "For calls that may call functions that use
  799.      varargs or stdargs (prototype-less calls or calls to functions
  800.      containing ellipsis (...) in the declaration) %al is used as
  801.      hidden argument to specify the number of SSE registers used.  */
  802.   regcache_raw_write_unsigned (regcache, AMD64_RAX_REGNUM, sse_reg);
  803.   return sp;
  804. }

  805. static CORE_ADDR
  806. amd64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  807.                        struct regcache *regcache, CORE_ADDR bp_addr,
  808.                        int nargs, struct value **args,        CORE_ADDR sp,
  809.                        int struct_return, CORE_ADDR struct_addr)
  810. {
  811.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  812.   gdb_byte buf[8];

  813.   /* Pass arguments.  */
  814.   sp = amd64_push_arguments (regcache, nargs, args, sp, struct_return);

  815.   /* Pass "hidden" argument".  */
  816.   if (struct_return)
  817.     {
  818.       store_unsigned_integer (buf, 8, byte_order, struct_addr);
  819.       regcache_cooked_write (regcache, AMD64_RDI_REGNUM, buf);
  820.     }

  821.   /* Store return address.  */
  822.   sp -= 8;
  823.   store_unsigned_integer (buf, 8, byte_order, bp_addr);
  824.   write_memory (sp, buf, 8);

  825.   /* Finally, update the stack pointer...  */
  826.   store_unsigned_integer (buf, 8, byte_order, sp);
  827.   regcache_cooked_write (regcache, AMD64_RSP_REGNUM, buf);

  828.   /* ...and fake a frame pointer.  */
  829.   regcache_cooked_write (regcache, AMD64_RBP_REGNUM, buf);

  830.   return sp + 16;
  831. }

  832. /* Displaced instruction handling.  */

  833. /* A partially decoded instruction.
  834.    This contains enough details for displaced stepping purposes.  */

  835. struct amd64_insn
  836. {
  837.   /* The number of opcode bytes.  */
  838.   int opcode_len;
  839.   /* The offset of the rex prefix or -1 if not present.  */
  840.   int rex_offset;
  841.   /* The offset to the first opcode byte.  */
  842.   int opcode_offset;
  843.   /* The offset to the modrm byte or -1 if not present.  */
  844.   int modrm_offset;

  845.   /* The raw instruction.  */
  846.   gdb_byte *raw_insn;
  847. };

  848. struct displaced_step_closure
  849. {
  850.   /* For rip-relative insns, saved copy of the reg we use instead of %rip.  */
  851.   int tmp_used;
  852.   int tmp_regno;
  853.   ULONGEST tmp_save;

  854.   /* Details of the instruction.  */
  855.   struct amd64_insn insn_details;

  856.   /* Amount of space allocated to insn_buf.  */
  857.   int max_len;

  858.   /* The possibly modified insn.
  859.      This is a variable-length field.  */
  860.   gdb_byte insn_buf[1];
  861. };

  862. /* WARNING: Keep onebyte_has_modrm, twobyte_has_modrm in sync with
  863.    ../opcodes/i386-dis.c (until libopcodes exports them, or an alternative,
  864.    at which point delete these in favor of libopcodes' versions).  */

  865. static const unsigned char onebyte_has_modrm[256] = {
  866.   /*           0 1 2 3 4 5 6 7 8 9 a b c d e f          */
  867.   /*           -------------------------------          */
  868.   /* 00 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 00 */
  869.   /* 10 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 10 */
  870.   /* 20 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 20 */
  871.   /* 30 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 30 */
  872.   /* 40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 40 */
  873.   /* 50 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 50 */
  874.   /* 60 */ 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0, /* 60 */
  875.   /* 70 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 70 */
  876.   /* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 80 */
  877.   /* 90 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 90 */
  878.   /* a0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* a0 */
  879.   /* b0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* b0 */
  880.   /* c0 */ 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0, /* c0 */
  881.   /* d0 */ 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1, /* d0 */
  882.   /* e0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* e0 */
  883.   /* f0 */ 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1  /* f0 */
  884.   /*           -------------------------------          */
  885.   /*           0 1 2 3 4 5 6 7 8 9 a b c d e f          */
  886. };

  887. static const unsigned char twobyte_has_modrm[256] = {
  888.   /*           0 1 2 3 4 5 6 7 8 9 a b c d e f          */
  889.   /*           -------------------------------          */
  890.   /* 00 */ 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1, /* 0f */
  891.   /* 10 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 1f */
  892.   /* 20 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 2f */
  893.   /* 30 */ 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0, /* 3f */
  894.   /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 4f */
  895.   /* 50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 5f */
  896.   /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 6f */
  897.   /* 70 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 7f */
  898.   /* 80 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 8f */
  899.   /* 90 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 9f */
  900.   /* a0 */ 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1, /* af */
  901.   /* b0 */ 1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1, /* bf */
  902.   /* c0 */ 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, /* cf */
  903.   /* d0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* df */
  904.   /* e0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ef */
  905.   /* f0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0  /* ff */
  906.   /*           -------------------------------          */
  907.   /*           0 1 2 3 4 5 6 7 8 9 a b c d e f          */
  908. };

  909. static int amd64_syscall_p (const struct amd64_insn *insn, int *lengthp);

  910. static int
  911. rex_prefix_p (gdb_byte pfx)
  912. {
  913.   return REX_PREFIX_P (pfx);
  914. }

  915. /* Skip the legacy instruction prefixes in INSN.
  916.    We assume INSN is properly sentineled so we don't have to worry
  917.    about falling off the end of the buffer.  */

  918. static gdb_byte *
  919. amd64_skip_prefixes (gdb_byte *insn)
  920. {
  921.   while (1)
  922.     {
  923.       switch (*insn)
  924.         {
  925.         case DATA_PREFIX_OPCODE:
  926.         case ADDR_PREFIX_OPCODE:
  927.         case CS_PREFIX_OPCODE:
  928.         case DS_PREFIX_OPCODE:
  929.         case ES_PREFIX_OPCODE:
  930.         case FS_PREFIX_OPCODE:
  931.         case GS_PREFIX_OPCODE:
  932.         case SS_PREFIX_OPCODE:
  933.         case LOCK_PREFIX_OPCODE:
  934.         case REPE_PREFIX_OPCODE:
  935.         case REPNE_PREFIX_OPCODE:
  936.           ++insn;
  937.           continue;
  938.         default:
  939.           break;
  940.         }
  941.       break;
  942.     }

  943.   return insn;
  944. }

  945. /* Return an integer register (other than RSP) that is unused as an input
  946.    operand in INSN.
  947.    In order to not require adding a rex prefix if the insn doesn't already
  948.    have one, the result is restricted to RAX ... RDI, sans RSP.
  949.    The register numbering of the result follows architecture ordering,
  950.    e.g. RDI = 7.  */

  951. static int
  952. amd64_get_unused_input_int_reg (const struct amd64_insn *details)
  953. {
  954.   /* 1 bit for each reg */
  955.   int used_regs_mask = 0;

  956.   /* There can be at most 3 int regs used as inputs in an insn, and we have
  957.      7 to choose from (RAX ... RDI, sans RSP).
  958.      This allows us to take a conservative approach and keep things simple.
  959.      E.g. By avoiding RAX, we don't have to specifically watch for opcodes
  960.      that implicitly specify RAX.  */

  961.   /* Avoid RAX.  */
  962.   used_regs_mask |= 1 << EAX_REG_NUM;
  963.   /* Similarily avoid RDX, implicit operand in divides.  */
  964.   used_regs_mask |= 1 << EDX_REG_NUM;
  965.   /* Avoid RSP.  */
  966.   used_regs_mask |= 1 << ESP_REG_NUM;

  967.   /* If the opcode is one byte long and there's no ModRM byte,
  968.      assume the opcode specifies a register.  */
  969.   if (details->opcode_len == 1 && details->modrm_offset == -1)
  970.     used_regs_mask |= 1 << (details->raw_insn[details->opcode_offset] & 7);

  971.   /* Mark used regs in the modrm/sib bytes.  */
  972.   if (details->modrm_offset != -1)
  973.     {
  974.       int modrm = details->raw_insn[details->modrm_offset];
  975.       int mod = MODRM_MOD_FIELD (modrm);
  976.       int reg = MODRM_REG_FIELD (modrm);
  977.       int rm = MODRM_RM_FIELD (modrm);
  978.       int have_sib = mod != 3 && rm == 4;

  979.       /* Assume the reg field of the modrm byte specifies a register.  */
  980.       used_regs_mask |= 1 << reg;

  981.       if (have_sib)
  982.         {
  983.           int base = SIB_BASE_FIELD (details->raw_insn[details->modrm_offset + 1]);
  984.           int idx = SIB_INDEX_FIELD (details->raw_insn[details->modrm_offset + 1]);
  985.           used_regs_mask |= 1 << base;
  986.           used_regs_mask |= 1 << idx;
  987.         }
  988.       else
  989.         {
  990.           used_regs_mask |= 1 << rm;
  991.         }
  992.     }

  993.   gdb_assert (used_regs_mask < 256);
  994.   gdb_assert (used_regs_mask != 255);

  995.   /* Finally, find a free reg.  */
  996.   {
  997.     int i;

  998.     for (i = 0; i < 8; ++i)
  999.       {
  1000.         if (! (used_regs_mask & (1 << i)))
  1001.           return i;
  1002.       }

  1003.     /* We shouldn't get here.  */
  1004.     internal_error (__FILE__, __LINE__, _("unable to find free reg"));
  1005.   }
  1006. }

  1007. /* Extract the details of INSN that we need.  */

  1008. static void
  1009. amd64_get_insn_details (gdb_byte *insn, struct amd64_insn *details)
  1010. {
  1011.   gdb_byte *start = insn;
  1012.   int need_modrm;

  1013.   details->raw_insn = insn;

  1014.   details->opcode_len = -1;
  1015.   details->rex_offset = -1;
  1016.   details->opcode_offset = -1;
  1017.   details->modrm_offset = -1;

  1018.   /* Skip legacy instruction prefixes.  */
  1019.   insn = amd64_skip_prefixes (insn);

  1020.   /* Skip REX instruction prefix.  */
  1021.   if (rex_prefix_p (*insn))
  1022.     {
  1023.       details->rex_offset = insn - start;
  1024.       ++insn;
  1025.     }

  1026.   details->opcode_offset = insn - start;

  1027.   if (*insn == TWO_BYTE_OPCODE_ESCAPE)
  1028.     {
  1029.       /* Two or three-byte opcode.  */
  1030.       ++insn;
  1031.       need_modrm = twobyte_has_modrm[*insn];

  1032.       /* Check for three-byte opcode.  */
  1033.       switch (*insn)
  1034.         {
  1035.         case 0x24:
  1036.         case 0x25:
  1037.         case 0x38:
  1038.         case 0x3a:
  1039.         case 0x7a:
  1040.         case 0x7b:
  1041.           ++insn;
  1042.           details->opcode_len = 3;
  1043.           break;
  1044.         default:
  1045.           details->opcode_len = 2;
  1046.           break;
  1047.         }
  1048.     }
  1049.   else
  1050.     {
  1051.       /* One-byte opcode.  */
  1052.       need_modrm = onebyte_has_modrm[*insn];
  1053.       details->opcode_len = 1;
  1054.     }

  1055.   if (need_modrm)
  1056.     {
  1057.       ++insn;
  1058.       details->modrm_offset = insn - start;
  1059.     }
  1060. }

  1061. /* Update %rip-relative addressing in INSN.

  1062.    %rip-relative addressing only uses a 32-bit displacement.
  1063.    32 bits is not enough to be guaranteed to cover the distance between where
  1064.    the real instruction is and where its copy is.
  1065.    Convert the insn to use base+disp addressing.
  1066.    We set base = pc + insn_length so we can leave disp unchanged.  */

  1067. static void
  1068. fixup_riprel (struct gdbarch *gdbarch, struct displaced_step_closure *dsc,
  1069.               CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
  1070. {
  1071.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1072.   const struct amd64_insn *insn_details = &dsc->insn_details;
  1073.   int modrm_offset = insn_details->modrm_offset;
  1074.   gdb_byte *insn = insn_details->raw_insn + modrm_offset;
  1075.   CORE_ADDR rip_base;
  1076.   int32_t disp;
  1077.   int insn_length;
  1078.   int arch_tmp_regno, tmp_regno;
  1079.   ULONGEST orig_value;

  1080.   /* %rip+disp32 addressing mode, displacement follows ModRM byte.  */
  1081.   ++insn;

  1082.   /* Compute the rip-relative address.        */
  1083.   disp = extract_signed_integer (insn, sizeof (int32_t), byte_order);
  1084.   insn_length = gdb_buffered_insn_length (gdbarch, dsc->insn_buf,
  1085.                                           dsc->max_len, from);
  1086.   rip_base = from + insn_length;

  1087.   /* We need a register to hold the address.
  1088.      Pick one not used in the insn.
  1089.      NOTE: arch_tmp_regno uses architecture ordering, e.g. RDI = 7.  */
  1090.   arch_tmp_regno = amd64_get_unused_input_int_reg (insn_details);
  1091.   tmp_regno = amd64_arch_reg_to_regnum (arch_tmp_regno);

  1092.   /* REX.B should be unset as we were using rip-relative addressing,
  1093.      but ensure it's unset anyway, tmp_regno is not r8-r15.  */
  1094.   if (insn_details->rex_offset != -1)
  1095.     dsc->insn_buf[insn_details->rex_offset] &= ~REX_B;

  1096.   regcache_cooked_read_unsigned (regs, tmp_regno, &orig_value);
  1097.   dsc->tmp_regno = tmp_regno;
  1098.   dsc->tmp_save = orig_value;
  1099.   dsc->tmp_used = 1;

  1100.   /* Convert the ModRM field to be base+disp.  */
  1101.   dsc->insn_buf[modrm_offset] &= ~0xc7;
  1102.   dsc->insn_buf[modrm_offset] |= 0x80 + arch_tmp_regno;

  1103.   regcache_cooked_write_unsigned (regs, tmp_regno, rip_base);

  1104.   if (debug_displaced)
  1105.     fprintf_unfiltered (gdb_stdlog, "displaced: %%rip-relative addressing used.\n"
  1106.                         "displaced: using temp reg %d, old value %s, new value %s\n",
  1107.                         dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save),
  1108.                         paddress (gdbarch, rip_base));
  1109. }

  1110. static void
  1111. fixup_displaced_copy (struct gdbarch *gdbarch,
  1112.                       struct displaced_step_closure *dsc,
  1113.                       CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
  1114. {
  1115.   const struct amd64_insn *details = &dsc->insn_details;

  1116.   if (details->modrm_offset != -1)
  1117.     {
  1118.       gdb_byte modrm = details->raw_insn[details->modrm_offset];

  1119.       if ((modrm & 0xc7) == 0x05)
  1120.         {
  1121.           /* The insn uses rip-relative addressing.
  1122.              Deal with it.  */
  1123.           fixup_riprel (gdbarch, dsc, from, to, regs);
  1124.         }
  1125.     }
  1126. }

  1127. struct displaced_step_closure *
  1128. amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
  1129.                                 CORE_ADDR from, CORE_ADDR to,
  1130.                                 struct regcache *regs)
  1131. {
  1132.   int len = gdbarch_max_insn_length (gdbarch);
  1133.   /* Extra space for sentinels so fixup_{riprel,displaced_copy} don't have to
  1134.      continually watch for running off the end of the buffer.  */
  1135.   int fixup_sentinel_space = len;
  1136.   struct displaced_step_closure *dsc =
  1137.     xmalloc (sizeof (*dsc) + len + fixup_sentinel_space);
  1138.   gdb_byte *buf = &dsc->insn_buf[0];
  1139.   struct amd64_insn *details = &dsc->insn_details;

  1140.   dsc->tmp_used = 0;
  1141.   dsc->max_len = len + fixup_sentinel_space;

  1142.   read_memory (from, buf, len);

  1143.   /* Set up the sentinel space so we don't have to worry about running
  1144.      off the end of the buffer.  An excessive number of leading prefixes
  1145.      could otherwise cause this.  */
  1146.   memset (buf + len, 0, fixup_sentinel_space);

  1147.   amd64_get_insn_details (buf, details);

  1148.   /* GDB may get control back after the insn after the syscall.
  1149.      Presumably this is a kernel bug.
  1150.      If this is a syscall, make sure there's a nop afterwards.  */
  1151.   {
  1152.     int syscall_length;

  1153.     if (amd64_syscall_p (details, &syscall_length))
  1154.       buf[details->opcode_offset + syscall_length] = NOP_OPCODE;
  1155.   }

  1156.   /* Modify the insn to cope with the address where it will be executed from.
  1157.      In particular, handle any rip-relative addressing.         */
  1158.   fixup_displaced_copy (gdbarch, dsc, from, to, regs);

  1159.   write_memory (to, buf, len);

  1160.   if (debug_displaced)
  1161.     {
  1162.       fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
  1163.                           paddress (gdbarch, from), paddress (gdbarch, to));
  1164.       displaced_step_dump_bytes (gdb_stdlog, buf, len);
  1165.     }

  1166.   return dsc;
  1167. }

  1168. static int
  1169. amd64_absolute_jmp_p (const struct amd64_insn *details)
  1170. {
  1171.   const gdb_byte *insn = &details->raw_insn[details->opcode_offset];

  1172.   if (insn[0] == 0xff)
  1173.     {
  1174.       /* jump near, absolute indirect (/4) */
  1175.       if ((insn[1] & 0x38) == 0x20)
  1176.         return 1;

  1177.       /* jump far, absolute indirect (/5) */
  1178.       if ((insn[1] & 0x38) == 0x28)
  1179.         return 1;
  1180.     }

  1181.   return 0;
  1182. }

  1183. /* Return non-zero if the instruction DETAILS is a jump, zero otherwise.  */

  1184. static int
  1185. amd64_jmp_p (const struct amd64_insn *details)
  1186. {
  1187.   const gdb_byte *insn = &details->raw_insn[details->opcode_offset];

  1188.   /* jump short, relative.  */
  1189.   if (insn[0] == 0xeb)
  1190.     return 1;

  1191.   /* jump near, relative.  */
  1192.   if (insn[0] == 0xe9)
  1193.     return 1;

  1194.   return amd64_absolute_jmp_p (details);
  1195. }

  1196. static int
  1197. amd64_absolute_call_p (const struct amd64_insn *details)
  1198. {
  1199.   const gdb_byte *insn = &details->raw_insn[details->opcode_offset];

  1200.   if (insn[0] == 0xff)
  1201.     {
  1202.       /* Call near, absolute indirect (/2) */
  1203.       if ((insn[1] & 0x38) == 0x10)
  1204.         return 1;

  1205.       /* Call far, absolute indirect (/3) */
  1206.       if ((insn[1] & 0x38) == 0x18)
  1207.         return 1;
  1208.     }

  1209.   return 0;
  1210. }

  1211. static int
  1212. amd64_ret_p (const struct amd64_insn *details)
  1213. {
  1214.   /* NOTE: gcc can emit "repz ; ret".  */
  1215.   const gdb_byte *insn = &details->raw_insn[details->opcode_offset];

  1216.   switch (insn[0])
  1217.     {
  1218.     case 0xc2: /* ret near, pop N bytes */
  1219.     case 0xc3: /* ret near */
  1220.     case 0xca: /* ret far, pop N bytes */
  1221.     case 0xcb: /* ret far */
  1222.     case 0xcf: /* iret */
  1223.       return 1;

  1224.     default:
  1225.       return 0;
  1226.     }
  1227. }

  1228. static int
  1229. amd64_call_p (const struct amd64_insn *details)
  1230. {
  1231.   const gdb_byte *insn = &details->raw_insn[details->opcode_offset];

  1232.   if (amd64_absolute_call_p (details))
  1233.     return 1;

  1234.   /* call near, relative */
  1235.   if (insn[0] == 0xe8)
  1236.     return 1;

  1237.   return 0;
  1238. }

  1239. /* Return non-zero if INSN is a system call, and set *LENGTHP to its
  1240.    length in bytes.  Otherwise, return zero.  */

  1241. static int
  1242. amd64_syscall_p (const struct amd64_insn *details, int *lengthp)
  1243. {
  1244.   const gdb_byte *insn = &details->raw_insn[details->opcode_offset];

  1245.   if (insn[0] == 0x0f && insn[1] == 0x05)
  1246.     {
  1247.       *lengthp = 2;
  1248.       return 1;
  1249.     }

  1250.   return 0;
  1251. }

  1252. /* Classify the instruction at ADDR using PRED.
  1253.    Throw an error if the memory can't be read.  */

  1254. static int
  1255. amd64_classify_insn_at (struct gdbarch *gdbarch, CORE_ADDR addr,
  1256.                         int (*pred) (const struct amd64_insn *))
  1257. {
  1258.   struct amd64_insn details;
  1259.   gdb_byte *buf;
  1260.   int len, classification;

  1261.   len = gdbarch_max_insn_length (gdbarch);
  1262.   buf = alloca (len);

  1263.   read_code (addr, buf, len);
  1264.   amd64_get_insn_details (buf, &details);

  1265.   classification = pred (&details);

  1266.   return classification;
  1267. }

  1268. /* The gdbarch insn_is_call method.  */

  1269. static int
  1270. amd64_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
  1271. {
  1272.   return amd64_classify_insn_at (gdbarch, addr, amd64_call_p);
  1273. }

  1274. /* The gdbarch insn_is_ret method.  */

  1275. static int
  1276. amd64_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
  1277. {
  1278.   return amd64_classify_insn_at (gdbarch, addr, amd64_ret_p);
  1279. }

  1280. /* The gdbarch insn_is_jump method.  */

  1281. static int
  1282. amd64_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
  1283. {
  1284.   return amd64_classify_insn_at (gdbarch, addr, amd64_jmp_p);
  1285. }

  1286. /* Fix up the state of registers and memory after having single-stepped
  1287.    a displaced instruction.  */

  1288. void
  1289. amd64_displaced_step_fixup (struct gdbarch *gdbarch,
  1290.                             struct displaced_step_closure *dsc,
  1291.                             CORE_ADDR from, CORE_ADDR to,
  1292.                             struct regcache *regs)
  1293. {
  1294.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1295.   /* The offset we applied to the instruction's address.  */
  1296.   ULONGEST insn_offset = to - from;
  1297.   gdb_byte *insn = dsc->insn_buf;
  1298.   const struct amd64_insn *insn_details = &dsc->insn_details;

  1299.   if (debug_displaced)
  1300.     fprintf_unfiltered (gdb_stdlog,
  1301.                         "displaced: fixup (%s, %s), "
  1302.                         "insn = 0x%02x 0x%02x ...\n",
  1303.                         paddress (gdbarch, from), paddress (gdbarch, to),
  1304.                         insn[0], insn[1]);

  1305.   /* If we used a tmp reg, restore it.        */

  1306.   if (dsc->tmp_used)
  1307.     {
  1308.       if (debug_displaced)
  1309.         fprintf_unfiltered (gdb_stdlog, "displaced: restoring reg %d to %s\n",
  1310.                             dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save));
  1311.       regcache_cooked_write_unsigned (regs, dsc->tmp_regno, dsc->tmp_save);
  1312.     }

  1313.   /* The list of issues to contend with here is taken from
  1314.      resume_execution in arch/x86/kernel/kprobes.c, Linux 2.6.28.
  1315.      Yay for Free Software!  */

  1316.   /* Relocate the %rip back to the program's instruction stream,
  1317.      if necessary.  */

  1318.   /* Except in the case of absolute or indirect jump or call
  1319.      instructions, or a return instruction, the new rip is relative to
  1320.      the displaced instruction; make it relative to the original insn.
  1321.      Well, signal handler returns don't need relocation either, but we use the
  1322.      value of %rip to recognize those; see below.  */
  1323.   if (! amd64_absolute_jmp_p (insn_details)
  1324.       && ! amd64_absolute_call_p (insn_details)
  1325.       && ! amd64_ret_p (insn_details))
  1326.     {
  1327.       ULONGEST orig_rip;
  1328.       int insn_len;

  1329.       regcache_cooked_read_unsigned (regs, AMD64_RIP_REGNUM, &orig_rip);

  1330.       /* A signal trampoline system call changes the %rip, resuming
  1331.          execution of the main program after the signal handler has
  1332.          returned.  That makes them like 'return' instructions; we
  1333.          shouldn't relocate %rip.

  1334.          But most system calls don't, and we do need to relocate %rip.

  1335.          Our heuristic for distinguishing these cases: if stepping
  1336.          over the system call instruction left control directly after
  1337.          the instruction, the we relocate --- control almost certainly
  1338.          doesn't belong in the displaced copy.        Otherwise, we assume
  1339.          the instruction has put control where it belongs, and leave
  1340.          it unrelocated.  Goodness help us if there are PC-relative
  1341.          system calls.        */
  1342.       if (amd64_syscall_p (insn_details, &insn_len)
  1343.           && orig_rip != to + insn_len
  1344.           /* GDB can get control back after the insn after the syscall.
  1345.              Presumably this is a kernel bug.
  1346.              Fixup ensures its a nop, we add one to the length for it.  */
  1347.           && orig_rip != to + insn_len + 1)
  1348.         {
  1349.           if (debug_displaced)
  1350.             fprintf_unfiltered (gdb_stdlog,
  1351.                                 "displaced: syscall changed %%rip; "
  1352.                                 "not relocating\n");
  1353.         }
  1354.       else
  1355.         {
  1356.           ULONGEST rip = orig_rip - insn_offset;

  1357.           /* If we just stepped over a breakpoint insn, we don't backup
  1358.              the pc on purpose; this is to match behaviour without
  1359.              stepping.  */

  1360.           regcache_cooked_write_unsigned (regs, AMD64_RIP_REGNUM, rip);

  1361.           if (debug_displaced)
  1362.             fprintf_unfiltered (gdb_stdlog,
  1363.                                 "displaced: "
  1364.                                 "relocated %%rip from %s to %s\n",
  1365.                                 paddress (gdbarch, orig_rip),
  1366.                                 paddress (gdbarch, rip));
  1367.         }
  1368.     }

  1369.   /* If the instruction was PUSHFL, then the TF bit will be set in the
  1370.      pushed value, and should be cleared.  We'll leave this for later,
  1371.      since GDB already messes up the TF flag when stepping over a
  1372.      pushfl.  */

  1373.   /* If the instruction was a call, the return address now atop the
  1374.      stack is the address following the copied instruction.  We need
  1375.      to make it the address following the original instruction.         */
  1376.   if (amd64_call_p (insn_details))
  1377.     {
  1378.       ULONGEST rsp;
  1379.       ULONGEST retaddr;
  1380.       const ULONGEST retaddr_len = 8;

  1381.       regcache_cooked_read_unsigned (regs, AMD64_RSP_REGNUM, &rsp);
  1382.       retaddr = read_memory_unsigned_integer (rsp, retaddr_len, byte_order);
  1383.       retaddr = (retaddr - insn_offset) & 0xffffffffUL;
  1384.       write_memory_unsigned_integer (rsp, retaddr_len, byte_order, retaddr);

  1385.       if (debug_displaced)
  1386.         fprintf_unfiltered (gdb_stdlog,
  1387.                             "displaced: relocated return addr at %s "
  1388.                             "to %s\n",
  1389.                             paddress (gdbarch, rsp),
  1390.                             paddress (gdbarch, retaddr));
  1391.     }
  1392. }

  1393. /* If the instruction INSN uses RIP-relative addressing, return the
  1394.    offset into the raw INSN where the displacement to be adjusted is
  1395.    found.  Returns 0 if the instruction doesn't use RIP-relative
  1396.    addressing.  */

  1397. static int
  1398. rip_relative_offset (struct amd64_insn *insn)
  1399. {
  1400.   if (insn->modrm_offset != -1)
  1401.     {
  1402.       gdb_byte modrm = insn->raw_insn[insn->modrm_offset];

  1403.       if ((modrm & 0xc7) == 0x05)
  1404.         {
  1405.           /* The displacement is found right after the ModRM byte.  */
  1406.           return insn->modrm_offset + 1;
  1407.         }
  1408.     }

  1409.   return 0;
  1410. }

  1411. static void
  1412. append_insns (CORE_ADDR *to, ULONGEST len, const gdb_byte *buf)
  1413. {
  1414.   target_write_memory (*to, buf, len);
  1415.   *to += len;
  1416. }

  1417. static void
  1418. amd64_relocate_instruction (struct gdbarch *gdbarch,
  1419.                             CORE_ADDR *to, CORE_ADDR oldloc)
  1420. {
  1421.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1422.   int len = gdbarch_max_insn_length (gdbarch);
  1423.   /* Extra space for sentinels.  */
  1424.   int fixup_sentinel_space = len;
  1425.   gdb_byte *buf = xmalloc (len + fixup_sentinel_space);
  1426.   struct amd64_insn insn_details;
  1427.   int offset = 0;
  1428.   LONGEST rel32, newrel;
  1429.   gdb_byte *insn;
  1430.   int insn_length;

  1431.   read_memory (oldloc, buf, len);

  1432.   /* Set up the sentinel space so we don't have to worry about running
  1433.      off the end of the buffer.  An excessive number of leading prefixes
  1434.      could otherwise cause this.  */
  1435.   memset (buf + len, 0, fixup_sentinel_space);

  1436.   insn = buf;
  1437.   amd64_get_insn_details (insn, &insn_details);

  1438.   insn_length = gdb_buffered_insn_length (gdbarch, insn, len, oldloc);

  1439.   /* Skip legacy instruction prefixes.  */
  1440.   insn = amd64_skip_prefixes (insn);

  1441.   /* Adjust calls with 32-bit relative addresses as push/jump, with
  1442.      the address pushed being the location where the original call in
  1443.      the user program would return to.  */
  1444.   if (insn[0] == 0xe8)
  1445.     {
  1446.       gdb_byte push_buf[16];
  1447.       unsigned int ret_addr;

  1448.       /* Where "ret" in the original code will return to.  */
  1449.       ret_addr = oldloc + insn_length;
  1450.       push_buf[0] = 0x68; /* pushq $...  */
  1451.       store_unsigned_integer (&push_buf[1], 4, byte_order, ret_addr);
  1452.       /* Push the push.  */
  1453.       append_insns (to, 5, push_buf);

  1454.       /* Convert the relative call to a relative jump.  */
  1455.       insn[0] = 0xe9;

  1456.       /* Adjust the destination offset.  */
  1457.       rel32 = extract_signed_integer (insn + 1, 4, byte_order);
  1458.       newrel = (oldloc - *to) + rel32;
  1459.       store_signed_integer (insn + 1, 4, byte_order, newrel);

  1460.       if (debug_displaced)
  1461.         fprintf_unfiltered (gdb_stdlog,
  1462.                             "Adjusted insn rel32=%s at %s to"
  1463.                             " rel32=%s at %s\n",
  1464.                             hex_string (rel32), paddress (gdbarch, oldloc),
  1465.                             hex_string (newrel), paddress (gdbarch, *to));

  1466.       /* Write the adjusted jump into its displaced location.  */
  1467.       append_insns (to, 5, insn);
  1468.       return;
  1469.     }

  1470.   offset = rip_relative_offset (&insn_details);
  1471.   if (!offset)
  1472.     {
  1473.       /* Adjust jumps with 32-bit relative addresses.  Calls are
  1474.          already handled above.  */
  1475.       if (insn[0] == 0xe9)
  1476.         offset = 1;
  1477.       /* Adjust conditional jumps.  */
  1478.       else if (insn[0] == 0x0f && (insn[1] & 0xf0) == 0x80)
  1479.         offset = 2;
  1480.     }

  1481.   if (offset)
  1482.     {
  1483.       rel32 = extract_signed_integer (insn + offset, 4, byte_order);
  1484.       newrel = (oldloc - *to) + rel32;
  1485.       store_signed_integer (insn + offset, 4, byte_order, newrel);
  1486.       if (debug_displaced)
  1487.         fprintf_unfiltered (gdb_stdlog,
  1488.                             "Adjusted insn rel32=%s at %s to"
  1489.                             " rel32=%s at %s\n",
  1490.                             hex_string (rel32), paddress (gdbarch, oldloc),
  1491.                             hex_string (newrel), paddress (gdbarch, *to));
  1492.     }

  1493.   /* Write the adjusted instruction into its displaced location.  */
  1494.   append_insns (to, insn_length, buf);
  1495. }


  1496. /* The maximum number of saved registers.  This should include %rip.  */
  1497. #define AMD64_NUM_SAVED_REGS        AMD64_NUM_GREGS

  1498. struct amd64_frame_cache
  1499. {
  1500.   /* Base address.  */
  1501.   CORE_ADDR base;
  1502.   int base_p;
  1503.   CORE_ADDR sp_offset;
  1504.   CORE_ADDR pc;

  1505.   /* Saved registers.  */
  1506.   CORE_ADDR saved_regs[AMD64_NUM_SAVED_REGS];
  1507.   CORE_ADDR saved_sp;
  1508.   int saved_sp_reg;

  1509.   /* Do we have a frame?  */
  1510.   int frameless_p;
  1511. };

  1512. /* Initialize a frame cache.  */

  1513. static void
  1514. amd64_init_frame_cache (struct amd64_frame_cache *cache)
  1515. {
  1516.   int i;

  1517.   /* Base address.  */
  1518.   cache->base = 0;
  1519.   cache->base_p = 0;
  1520.   cache->sp_offset = -8;
  1521.   cache->pc = 0;

  1522.   /* Saved registers.  We initialize these to -1 since zero is a valid
  1523.      offset (that's where %rbp is supposed to be stored).
  1524.      The values start out as being offsets, and are later converted to
  1525.      addresses (at which point -1 is interpreted as an address, still meaning
  1526.      "invalid").  */
  1527.   for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
  1528.     cache->saved_regs[i] = -1;
  1529.   cache->saved_sp = 0;
  1530.   cache->saved_sp_reg = -1;

  1531.   /* Frameless until proven otherwise.  */
  1532.   cache->frameless_p = 1;
  1533. }

  1534. /* Allocate and initialize a frame cache.  */

  1535. static struct amd64_frame_cache *
  1536. amd64_alloc_frame_cache (void)
  1537. {
  1538.   struct amd64_frame_cache *cache;

  1539.   cache = FRAME_OBSTACK_ZALLOC (struct amd64_frame_cache);
  1540.   amd64_init_frame_cache (cache);
  1541.   return cache;
  1542. }

  1543. /* GCC 4.4 and later, can put code in the prologue to realign the
  1544.    stack pointer.  Check whether PC points to such code, and update
  1545.    CACHE accordingly.  Return the first instruction after the code
  1546.    sequence or CURRENT_PC, whichever is smaller.  If we don't
  1547.    recognize the code, return PC.  */

  1548. static CORE_ADDR
  1549. amd64_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
  1550.                            struct amd64_frame_cache *cache)
  1551. {
  1552.   /* There are 2 code sequences to re-align stack before the frame
  1553.      gets set up:

  1554.         1. Use a caller-saved saved register:

  1555.                 leaq  8(%rsp), %reg
  1556.                 andq  $-XXX, %rsp
  1557.                 pushq -8(%reg)

  1558.         2. Use a callee-saved saved register:

  1559.                 pushq %reg
  1560.                 leaq  16(%rsp), %reg
  1561.                 andq  $-XXX, %rsp
  1562.                 pushq -8(%reg)

  1563.      "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:

  1564.              0x48 0x83 0xe4 0xf0                        andq $-16, %rsp
  1565.              0x48 0x81 0xe4 0x00 0xff 0xff 0xff        andq $-256, %rsp
  1566.    */

  1567.   gdb_byte buf[18];
  1568.   int reg, r;
  1569.   int offset, offset_and;

  1570.   if (target_read_code (pc, buf, sizeof buf))
  1571.     return pc;

  1572.   /* Check caller-saved saved register.  The first instruction has
  1573.      to be "leaq 8(%rsp), %reg".  */
  1574.   if ((buf[0] & 0xfb) == 0x48
  1575.       && buf[1] == 0x8d
  1576.       && buf[3] == 0x24
  1577.       && buf[4] == 0x8)
  1578.     {
  1579.       /* MOD must be binary 10 and R/M must be binary 100.  */
  1580.       if ((buf[2] & 0xc7) != 0x44)
  1581.         return pc;

  1582.       /* REG has register number.  */
  1583.       reg = (buf[2] >> 3) & 7;

  1584.       /* Check the REX.R bit.  */
  1585.       if (buf[0] == 0x4c)
  1586.         reg += 8;

  1587.       offset = 5;
  1588.     }
  1589.   else
  1590.     {
  1591.       /* Check callee-saved saved register.  The first instruction
  1592.          has to be "pushq %reg".  */
  1593.       reg = 0;
  1594.       if ((buf[0] & 0xf8) == 0x50)
  1595.         offset = 0;
  1596.       else if ((buf[0] & 0xf6) == 0x40
  1597.                && (buf[1] & 0xf8) == 0x50)
  1598.         {
  1599.           /* Check the REX.B bit.  */
  1600.           if ((buf[0] & 1) != 0)
  1601.             reg = 8;

  1602.           offset = 1;
  1603.         }
  1604.       else
  1605.         return pc;

  1606.       /* Get register.  */
  1607.       reg += buf[offset] & 0x7;

  1608.       offset++;

  1609.       /* The next instruction has to be "leaq 16(%rsp), %reg".  */
  1610.       if ((buf[offset] & 0xfb) != 0x48
  1611.           || buf[offset + 1] != 0x8d
  1612.           || buf[offset + 3] != 0x24
  1613.           || buf[offset + 4] != 0x10)
  1614.         return pc;

  1615.       /* MOD must be binary 10 and R/M must be binary 100.  */
  1616.       if ((buf[offset + 2] & 0xc7) != 0x44)
  1617.         return pc;

  1618.       /* REG has register number.  */
  1619.       r = (buf[offset + 2] >> 3) & 7;

  1620.       /* Check the REX.R bit.  */
  1621.       if (buf[offset] == 0x4c)
  1622.         r += 8;

  1623.       /* Registers in pushq and leaq have to be the same.  */
  1624.       if (reg != r)
  1625.         return pc;

  1626.       offset += 5;
  1627.     }

  1628.   /* Rigister can't be %rsp nor %rbp.  */
  1629.   if (reg == 4 || reg == 5)
  1630.     return pc;

  1631.   /* The next instruction has to be "andq $-XXX, %rsp".  */
  1632.   if (buf[offset] != 0x48
  1633.       || buf[offset + 2] != 0xe4
  1634.       || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
  1635.     return pc;

  1636.   offset_and = offset;
  1637.   offset += buf[offset + 1] == 0x81 ? 7 : 4;

  1638.   /* The next instruction has to be "pushq -8(%reg)".  */
  1639.   r = 0;
  1640.   if (buf[offset] == 0xff)
  1641.     offset++;
  1642.   else if ((buf[offset] & 0xf6) == 0x40
  1643.            && buf[offset + 1] == 0xff)
  1644.     {
  1645.       /* Check the REX.B bit.  */
  1646.       if ((buf[offset] & 0x1) != 0)
  1647.         r = 8;
  1648.       offset += 2;
  1649.     }
  1650.   else
  1651.     return pc;

  1652.   /* 8bit -8 is 0xf8.  REG must be binary 110 and MOD must be binary
  1653.      01.  */
  1654.   if (buf[offset + 1] != 0xf8
  1655.       || (buf[offset] & 0xf8) != 0x70)
  1656.     return pc;

  1657.   /* R/M has register.  */
  1658.   r += buf[offset] & 7;

  1659.   /* Registers in leaq and pushq have to be the same.  */
  1660.   if (reg != r)
  1661.     return pc;

  1662.   if (current_pc > pc + offset_and)
  1663.     cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);

  1664.   return min (pc + offset + 2, current_pc);
  1665. }

  1666. /* Similar to amd64_analyze_stack_align for x32.  */

  1667. static CORE_ADDR
  1668. amd64_x32_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
  1669.                                struct amd64_frame_cache *cache)
  1670. {
  1671.   /* There are 2 code sequences to re-align stack before the frame
  1672.      gets set up:

  1673.         1. Use a caller-saved saved register:

  1674.                 leaq  8(%rsp), %reg
  1675.                 andq  $-XXX, %rsp
  1676.                 pushq -8(%reg)

  1677.            or

  1678.                 [addr32] leal  8(%rsp), %reg
  1679.                 andl  $-XXX, %esp
  1680.                 [addr32] pushq -8(%reg)

  1681.         2. Use a callee-saved saved register:

  1682.                 pushq %reg
  1683.                 leaq  16(%rsp), %reg
  1684.                 andq  $-XXX, %rsp
  1685.                 pushq -8(%reg)

  1686.            or

  1687.                 pushq %reg
  1688.                 [addr32] leal  16(%rsp), %reg
  1689.                 andl  $-XXX, %esp
  1690.                 [addr32] pushq -8(%reg)

  1691.      "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:

  1692.              0x48 0x83 0xe4 0xf0                        andq $-16, %rsp
  1693.              0x48 0x81 0xe4 0x00 0xff 0xff 0xff        andq $-256, %rsp

  1694.      "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:

  1695.              0x83 0xe4 0xf0                        andl $-16, %esp
  1696.              0x81 0xe4 0x00 0xff 0xff 0xff        andl $-256, %esp
  1697.    */

  1698.   gdb_byte buf[19];
  1699.   int reg, r;
  1700.   int offset, offset_and;

  1701.   if (target_read_memory (pc, buf, sizeof buf))
  1702.     return pc;

  1703.   /* Skip optional addr32 prefix.  */
  1704.   offset = buf[0] == 0x67 ? 1 : 0;

  1705.   /* Check caller-saved saved register.  The first instruction has
  1706.      to be "leaq 8(%rsp), %reg" or "leal 8(%rsp), %reg".  */
  1707.   if (((buf[offset] & 0xfb) == 0x48 || (buf[offset] & 0xfb) == 0x40)
  1708.       && buf[offset + 1] == 0x8d
  1709.       && buf[offset + 3] == 0x24
  1710.       && buf[offset + 4] == 0x8)
  1711.     {
  1712.       /* MOD must be binary 10 and R/M must be binary 100.  */
  1713.       if ((buf[offset + 2] & 0xc7) != 0x44)
  1714.         return pc;

  1715.       /* REG has register number.  */
  1716.       reg = (buf[offset + 2] >> 3) & 7;

  1717.       /* Check the REX.R bit.  */
  1718.       if ((buf[offset] & 0x4) != 0)
  1719.         reg += 8;

  1720.       offset += 5;
  1721.     }
  1722.   else
  1723.     {
  1724.       /* Check callee-saved saved register.  The first instruction
  1725.          has to be "pushq %reg".  */
  1726.       reg = 0;
  1727.       if ((buf[offset] & 0xf6) == 0x40
  1728.           && (buf[offset + 1] & 0xf8) == 0x50)
  1729.         {
  1730.           /* Check the REX.B bit.  */
  1731.           if ((buf[offset] & 1) != 0)
  1732.             reg = 8;

  1733.           offset += 1;
  1734.         }
  1735.       else if ((buf[offset] & 0xf8) != 0x50)
  1736.         return pc;

  1737.       /* Get register.  */
  1738.       reg += buf[offset] & 0x7;

  1739.       offset++;

  1740.       /* Skip optional addr32 prefix.  */
  1741.       if (buf[offset] == 0x67)
  1742.         offset++;

  1743.       /* The next instruction has to be "leaq 16(%rsp), %reg" or
  1744.          "leal 16(%rsp), %reg".  */
  1745.       if (((buf[offset] & 0xfb) != 0x48 && (buf[offset] & 0xfb) != 0x40)
  1746.           || buf[offset + 1] != 0x8d
  1747.           || buf[offset + 3] != 0x24
  1748.           || buf[offset + 4] != 0x10)
  1749.         return pc;

  1750.       /* MOD must be binary 10 and R/M must be binary 100.  */
  1751.       if ((buf[offset + 2] & 0xc7) != 0x44)
  1752.         return pc;

  1753.       /* REG has register number.  */
  1754.       r = (buf[offset + 2] >> 3) & 7;

  1755.       /* Check the REX.R bit.  */
  1756.       if ((buf[offset] & 0x4) != 0)
  1757.         r += 8;

  1758.       /* Registers in pushq and leaq have to be the same.  */
  1759.       if (reg != r)
  1760.         return pc;

  1761.       offset += 5;
  1762.     }

  1763.   /* Rigister can't be %rsp nor %rbp.  */
  1764.   if (reg == 4 || reg == 5)
  1765.     return pc;

  1766.   /* The next instruction may be "andq $-XXX, %rsp" or
  1767.      "andl $-XXX, %esp".  */
  1768.   if (buf[offset] != 0x48)
  1769.     offset--;

  1770.   if (buf[offset + 2] != 0xe4
  1771.       || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
  1772.     return pc;

  1773.   offset_and = offset;
  1774.   offset += buf[offset + 1] == 0x81 ? 7 : 4;

  1775.   /* Skip optional addr32 prefix.  */
  1776.   if (buf[offset] == 0x67)
  1777.     offset++;

  1778.   /* The next instruction has to be "pushq -8(%reg)".  */
  1779.   r = 0;
  1780.   if (buf[offset] == 0xff)
  1781.     offset++;
  1782.   else if ((buf[offset] & 0xf6) == 0x40
  1783.            && buf[offset + 1] == 0xff)
  1784.     {
  1785.       /* Check the REX.B bit.  */
  1786.       if ((buf[offset] & 0x1) != 0)
  1787.         r = 8;
  1788.       offset += 2;
  1789.     }
  1790.   else
  1791.     return pc;

  1792.   /* 8bit -8 is 0xf8.  REG must be binary 110 and MOD must be binary
  1793.      01.  */
  1794.   if (buf[offset + 1] != 0xf8
  1795.       || (buf[offset] & 0xf8) != 0x70)
  1796.     return pc;

  1797.   /* R/M has register.  */
  1798.   r += buf[offset] & 7;

  1799.   /* Registers in leaq and pushq have to be the same.  */
  1800.   if (reg != r)
  1801.     return pc;

  1802.   if (current_pc > pc + offset_and)
  1803.     cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);

  1804.   return min (pc + offset + 2, current_pc);
  1805. }

  1806. /* Do a limited analysis of the prologue at PC and update CACHE
  1807.    accordingly.  Bail out early if CURRENT_PC is reached.  Return the
  1808.    address where the analysis stopped.

  1809.    We will handle only functions beginning with:

  1810.       pushq %rbp        0x55
  1811.       movq %rsp, %rbp   0x48 0x89 0xe5 (or 0x48 0x8b 0xec)

  1812.    or (for the X32 ABI):

  1813.       pushq %rbp        0x55
  1814.       movl %esp, %ebp   0x89 0xe5 (or 0x8b 0xec)

  1815.    Any function that doesn't start with one of these sequences will be
  1816.    assumed to have no prologue and thus no valid frame pointer in
  1817.    %rbp.  */

  1818. static CORE_ADDR
  1819. amd64_analyze_prologue (struct gdbarch *gdbarch,
  1820.                         CORE_ADDR pc, CORE_ADDR current_pc,
  1821.                         struct amd64_frame_cache *cache)
  1822. {
  1823.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1824.   /* There are two variations of movq %rsp, %rbp.  */
  1825.   static const gdb_byte mov_rsp_rbp_1[3] = { 0x48, 0x89, 0xe5 };
  1826.   static const gdb_byte mov_rsp_rbp_2[3] = { 0x48, 0x8b, 0xec };
  1827.   /* Ditto for movl %esp, %ebp.  */
  1828.   static const gdb_byte mov_esp_ebp_1[2] = { 0x89, 0xe5 };
  1829.   static const gdb_byte mov_esp_ebp_2[2] = { 0x8b, 0xec };

  1830.   gdb_byte buf[3];
  1831.   gdb_byte op;

  1832.   if (current_pc <= pc)
  1833.     return current_pc;

  1834.   if (gdbarch_ptr_bit (gdbarch) == 32)
  1835.     pc = amd64_x32_analyze_stack_align (pc, current_pc, cache);
  1836.   else
  1837.     pc = amd64_analyze_stack_align (pc, current_pc, cache);

  1838.   op = read_code_unsigned_integer (pc, 1, byte_order);

  1839.   if (op == 0x55)                /* pushq %rbp */
  1840.     {
  1841.       /* Take into account that we've executed the `pushq %rbp' that
  1842.          starts this instruction sequence.  */
  1843.       cache->saved_regs[AMD64_RBP_REGNUM] = 0;
  1844.       cache->sp_offset += 8;

  1845.       /* If that's all, return now.  */
  1846.       if (current_pc <= pc + 1)
  1847.         return current_pc;

  1848.       read_code (pc + 1, buf, 3);

  1849.       /* Check for `movq %rsp, %rbp'.  */
  1850.       if (memcmp (buf, mov_rsp_rbp_1, 3) == 0
  1851.           || memcmp (buf, mov_rsp_rbp_2, 3) == 0)
  1852.         {
  1853.           /* OK, we actually have a frame.  */
  1854.           cache->frameless_p = 0;
  1855.           return pc + 4;
  1856.         }

  1857.       /* For X32, also check for `movq %esp, %ebp'.  */
  1858.       if (gdbarch_ptr_bit (gdbarch) == 32)
  1859.         {
  1860.           if (memcmp (buf, mov_esp_ebp_1, 2) == 0
  1861.               || memcmp (buf, mov_esp_ebp_2, 2) == 0)
  1862.             {
  1863.               /* OK, we actually have a frame.  */
  1864.               cache->frameless_p = 0;
  1865.               return pc + 3;
  1866.             }
  1867.         }

  1868.       return pc + 1;
  1869.     }

  1870.   return pc;
  1871. }

  1872. /* Work around false termination of prologue - GCC PR debug/48827.

  1873.    START_PC is the first instruction of a function, PC is its minimal already
  1874.    determined advanced addressFunction returns PC if it has nothing to do.

  1875.    84 c0                test   %al,%al
  1876.    74 23                je     after
  1877.    <-- here is 0 lines advance - the false prologue end marker.
  1878.    0f 29 85 70 ff ff ff movaps %xmm0,-0x90(%rbp)
  1879.    0f 29 4d 80          movaps %xmm1,-0x80(%rbp)
  1880.    0f 29 55 90          movaps %xmm2,-0x70(%rbp)
  1881.    0f 29 5d a0          movaps %xmm3,-0x60(%rbp)
  1882.    0f 29 65 b0          movaps %xmm4,-0x50(%rbp)
  1883.    0f 29 6d c0          movaps %xmm5,-0x40(%rbp)
  1884.    0f 29 75 d0          movaps %xmm6,-0x30(%rbp)
  1885.    0f 29 7d e0          movaps %xmm7,-0x20(%rbp)
  1886.    after:  */

  1887. static CORE_ADDR
  1888. amd64_skip_xmm_prologue (CORE_ADDR pc, CORE_ADDR start_pc)
  1889. {
  1890.   struct symtab_and_line start_pc_sal, next_sal;
  1891.   gdb_byte buf[4 + 8 * 7];
  1892.   int offset, xmmreg;

  1893.   if (pc == start_pc)
  1894.     return pc;

  1895.   start_pc_sal = find_pc_sect_line (start_pc, NULL, 0);
  1896.   if (start_pc_sal.symtab == NULL
  1897.       || producer_is_gcc_ge_4 (COMPUNIT_PRODUCER
  1898.            (SYMTAB_COMPUNIT (start_pc_sal.symtab))) < 6
  1899.       || start_pc_sal.pc != start_pc || pc >= start_pc_sal.end)
  1900.     return pc;

  1901.   next_sal = find_pc_sect_line (start_pc_sal.end, NULL, 0);
  1902.   if (next_sal.line != start_pc_sal.line)
  1903.     return pc;

  1904.   /* START_PC can be from overlayed memory, ignored here.  */
  1905.   if (target_read_code (next_sal.pc - 4, buf, sizeof (buf)) != 0)
  1906.     return pc;

  1907.   /* test %al,%al */
  1908.   if (buf[0] != 0x84 || buf[1] != 0xc0)
  1909.     return pc;
  1910.   /* je AFTER */
  1911.   if (buf[2] != 0x74)
  1912.     return pc;

  1913.   offset = 4;
  1914.   for (xmmreg = 0; xmmreg < 8; xmmreg++)
  1915.     {
  1916.       /* 0x0f 0x29 0b??000101 movaps %xmmreg?,-0x??(%rbp) */
  1917.       if (buf[offset] != 0x0f || buf[offset + 1] != 0x29
  1918.           || (buf[offset + 2] & 0x3f) != (xmmreg << 3 | 0x5))
  1919.         return pc;

  1920.       /* 0b01?????? */
  1921.       if ((buf[offset + 2] & 0xc0) == 0x40)
  1922.         {
  1923.           /* 8-bit displacement.  */
  1924.           offset += 4;
  1925.         }
  1926.       /* 0b10?????? */
  1927.       else if ((buf[offset + 2] & 0xc0) == 0x80)
  1928.         {
  1929.           /* 32-bit displacement.  */
  1930.           offset += 7;
  1931.         }
  1932.       else
  1933.         return pc;
  1934.     }

  1935.   /* je AFTER */
  1936.   if (offset - 4 != buf[3])
  1937.     return pc;

  1938.   return next_sal.end;
  1939. }

  1940. /* Return PC of first real instruction.  */

  1941. static CORE_ADDR
  1942. amd64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  1943. {
  1944.   struct amd64_frame_cache cache;
  1945.   CORE_ADDR pc;
  1946.   CORE_ADDR func_addr;

  1947.   if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
  1948.     {
  1949.       CORE_ADDR post_prologue_pc
  1950.         = skip_prologue_using_sal (gdbarch, func_addr);
  1951.       struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);

  1952.       /* Clang always emits a line note before the prologue and another
  1953.          one after.  We trust clang to emit usable line notes.  */
  1954.       if (post_prologue_pc
  1955.           && (cust != NULL
  1956.               && COMPUNIT_PRODUCER (cust) != NULL
  1957.               && strncmp (COMPUNIT_PRODUCER (cust), "clang ",
  1958.                           sizeof ("clang ") - 1) == 0))
  1959.         return max (start_pc, post_prologue_pc);
  1960.     }

  1961.   amd64_init_frame_cache (&cache);
  1962.   pc = amd64_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffLL,
  1963.                                &cache);
  1964.   if (cache.frameless_p)
  1965.     return start_pc;

  1966.   return amd64_skip_xmm_prologue (pc, start_pc);
  1967. }


  1968. /* Normal frames.  */

  1969. static void
  1970. amd64_frame_cache_1 (struct frame_info *this_frame,
  1971.                      struct amd64_frame_cache *cache)
  1972. {
  1973.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1974.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1975.   gdb_byte buf[8];
  1976.   int i;

  1977.   cache->pc = get_frame_func (this_frame);
  1978.   if (cache->pc != 0)
  1979.     amd64_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
  1980.                             cache);

  1981.   if (cache->frameless_p)
  1982.     {
  1983.       /* We didn't find a valid frame.  If we're at the start of a
  1984.          function, or somewhere half-way its prologue, the function's
  1985.          frame probably hasn't been fully setup yet.  Try to
  1986.          reconstruct the base address for the stack frame by looking
  1987.          at the stack pointer.  For truly "frameless" functions this
  1988.          might work too.  */

  1989.       if (cache->saved_sp_reg != -1)
  1990.         {
  1991.           /* Stack pointer has been saved.  */
  1992.           get_frame_register (this_frame, cache->saved_sp_reg, buf);
  1993.           cache->saved_sp = extract_unsigned_integer (buf, 8, byte_order);

  1994.           /* We're halfway aligning the stack.  */
  1995.           cache->base = ((cache->saved_sp - 8) & 0xfffffffffffffff0LL) - 8;
  1996.           cache->saved_regs[AMD64_RIP_REGNUM] = cache->saved_sp - 8;

  1997.           /* This will be added back below.  */
  1998.           cache->saved_regs[AMD64_RIP_REGNUM] -= cache->base;
  1999.         }
  2000.       else
  2001.         {
  2002.           get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
  2003.           cache->base = extract_unsigned_integer (buf, 8, byte_order)
  2004.                         + cache->sp_offset;
  2005.         }
  2006.     }
  2007.   else
  2008.     {
  2009.       get_frame_register (this_frame, AMD64_RBP_REGNUM, buf);
  2010.       cache->base = extract_unsigned_integer (buf, 8, byte_order);
  2011.     }

  2012.   /* Now that we have the base address for the stack frame we can
  2013.      calculate the value of %rsp in the calling frame.  */
  2014.   cache->saved_sp = cache->base + 16;

  2015.   /* For normal frames, %rip is stored at 8(%rbp).  If we don't have a
  2016.      frame we find it at the same offset from the reconstructed base
  2017.      address.  If we're halfway aligning the stack, %rip is handled
  2018.      differently (see above).  */
  2019.   if (!cache->frameless_p || cache->saved_sp_reg == -1)
  2020.     cache->saved_regs[AMD64_RIP_REGNUM] = 8;

  2021.   /* Adjust all the saved registers such that they contain addresses
  2022.      instead of offsets.  */
  2023.   for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
  2024.     if (cache->saved_regs[i] != -1)
  2025.       cache->saved_regs[i] += cache->base;

  2026.   cache->base_p = 1;
  2027. }

  2028. static struct amd64_frame_cache *
  2029. amd64_frame_cache (struct frame_info *this_frame, void **this_cache)
  2030. {
  2031.   volatile struct gdb_exception ex;
  2032.   struct amd64_frame_cache *cache;

  2033.   if (*this_cache)
  2034.     return *this_cache;

  2035.   cache = amd64_alloc_frame_cache ();
  2036.   *this_cache = cache;

  2037.   TRY_CATCH (ex, RETURN_MASK_ERROR)
  2038.     {
  2039.       amd64_frame_cache_1 (this_frame, cache);
  2040.     }
  2041.   if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
  2042.     throw_exception (ex);

  2043.   return cache;
  2044. }

  2045. static enum unwind_stop_reason
  2046. amd64_frame_unwind_stop_reason (struct frame_info *this_frame,
  2047.                                 void **this_cache)
  2048. {
  2049.   struct amd64_frame_cache *cache =
  2050.     amd64_frame_cache (this_frame, this_cache);

  2051.   if (!cache->base_p)
  2052.     return UNWIND_UNAVAILABLE;

  2053.   /* This marks the outermost frame.  */
  2054.   if (cache->base == 0)
  2055.     return UNWIND_OUTERMOST;

  2056.   return UNWIND_NO_REASON;
  2057. }

  2058. static void
  2059. amd64_frame_this_id (struct frame_info *this_frame, void **this_cache,
  2060.                      struct frame_id *this_id)
  2061. {
  2062.   struct amd64_frame_cache *cache =
  2063.     amd64_frame_cache (this_frame, this_cache);

  2064.   if (!cache->base_p)
  2065.     (*this_id) = frame_id_build_unavailable_stack (cache->pc);
  2066.   else if (cache->base == 0)
  2067.     {
  2068.       /* This marks the outermost frame.  */
  2069.       return;
  2070.     }
  2071.   else
  2072.     (*this_id) = frame_id_build (cache->base + 16, cache->pc);
  2073. }

  2074. static struct value *
  2075. amd64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  2076.                            int regnum)
  2077. {
  2078.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2079.   struct amd64_frame_cache *cache =
  2080.     amd64_frame_cache (this_frame, this_cache);

  2081.   gdb_assert (regnum >= 0);

  2082.   if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
  2083.     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);

  2084.   if (regnum < AMD64_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
  2085.     return frame_unwind_got_memory (this_frame, regnum,
  2086.                                     cache->saved_regs[regnum]);

  2087.   return frame_unwind_got_register (this_frame, regnum, regnum);
  2088. }

  2089. static const struct frame_unwind amd64_frame_unwind =
  2090. {
  2091.   NORMAL_FRAME,
  2092.   amd64_frame_unwind_stop_reason,
  2093.   amd64_frame_this_id,
  2094.   amd64_frame_prev_register,
  2095.   NULL,
  2096.   default_frame_sniffer
  2097. };

  2098. /* Generate a bytecode expression to get the value of the saved PC.  */

  2099. static void
  2100. amd64_gen_return_address (struct gdbarch *gdbarch,
  2101.                           struct agent_expr *ax, struct axs_value *value,
  2102.                           CORE_ADDR scope)
  2103. {
  2104.   /* The following sequence assumes the traditional use of the base
  2105.      register.  */
  2106.   ax_reg (ax, AMD64_RBP_REGNUM);
  2107.   ax_const_l (ax, 8);
  2108.   ax_simple (ax, aop_add);
  2109.   value->type = register_type (gdbarch, AMD64_RIP_REGNUM);
  2110.   value->kind = axs_lvalue_memory;
  2111. }


  2112. /* Signal trampolines.  */

  2113. /* FIXME: kettenis/20030419: Perhaps, we can unify the 32-bit and
  2114.    64-bit variants.  This would require using identical frame caches
  2115.    on both platforms.  */

  2116. static struct amd64_frame_cache *
  2117. amd64_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
  2118. {
  2119.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2120.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  2121.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2122.   volatile struct gdb_exception ex;
  2123.   struct amd64_frame_cache *cache;
  2124.   CORE_ADDR addr;
  2125.   gdb_byte buf[8];
  2126.   int i;

  2127.   if (*this_cache)
  2128.     return *this_cache;

  2129.   cache = amd64_alloc_frame_cache ();

  2130.   TRY_CATCH (ex, RETURN_MASK_ERROR)
  2131.     {
  2132.       get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
  2133.       cache->base = extract_unsigned_integer (buf, 8, byte_order) - 8;

  2134.       addr = tdep->sigcontext_addr (this_frame);
  2135.       gdb_assert (tdep->sc_reg_offset);
  2136.       gdb_assert (tdep->sc_num_regs <= AMD64_NUM_SAVED_REGS);
  2137.       for (i = 0; i < tdep->sc_num_regs; i++)
  2138.         if (tdep->sc_reg_offset[i] != -1)
  2139.           cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];

  2140.       cache->base_p = 1;
  2141.     }
  2142.   if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
  2143.     throw_exception (ex);

  2144.   *this_cache = cache;
  2145.   return cache;
  2146. }

  2147. static enum unwind_stop_reason
  2148. amd64_sigtramp_frame_unwind_stop_reason (struct frame_info *this_frame,
  2149.                                          void **this_cache)
  2150. {
  2151.   struct amd64_frame_cache *cache =
  2152.     amd64_sigtramp_frame_cache (this_frame, this_cache);

  2153.   if (!cache->base_p)
  2154.     return UNWIND_UNAVAILABLE;

  2155.   return UNWIND_NO_REASON;
  2156. }

  2157. static void
  2158. amd64_sigtramp_frame_this_id (struct frame_info *this_frame,
  2159.                               void **this_cache, struct frame_id *this_id)
  2160. {
  2161.   struct amd64_frame_cache *cache =
  2162.     amd64_sigtramp_frame_cache (this_frame, this_cache);

  2163.   if (!cache->base_p)
  2164.     (*this_id) = frame_id_build_unavailable_stack (get_frame_pc (this_frame));
  2165.   else if (cache->base == 0)
  2166.     {
  2167.       /* This marks the outermost frame.  */
  2168.       return;
  2169.     }
  2170.   else
  2171.     (*this_id) = frame_id_build (cache->base + 16, get_frame_pc (this_frame));
  2172. }

  2173. static struct value *
  2174. amd64_sigtramp_frame_prev_register (struct frame_info *this_frame,
  2175.                                     void **this_cache, int regnum)
  2176. {
  2177.   /* Make sure we've initialized the cache.  */
  2178.   amd64_sigtramp_frame_cache (this_frame, this_cache);

  2179.   return amd64_frame_prev_register (this_frame, this_cache, regnum);
  2180. }

  2181. static int
  2182. amd64_sigtramp_frame_sniffer (const struct frame_unwind *self,
  2183.                               struct frame_info *this_frame,
  2184.                               void **this_cache)
  2185. {
  2186.   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));

  2187.   /* We shouldn't even bother if we don't have a sigcontext_addr
  2188.      handler.  */
  2189.   if (tdep->sigcontext_addr == NULL)
  2190.     return 0;

  2191.   if (tdep->sigtramp_p != NULL)
  2192.     {
  2193.       if (tdep->sigtramp_p (this_frame))
  2194.         return 1;
  2195.     }

  2196.   if (tdep->sigtramp_start != 0)
  2197.     {
  2198.       CORE_ADDR pc = get_frame_pc (this_frame);

  2199.       gdb_assert (tdep->sigtramp_end != 0);
  2200.       if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
  2201.         return 1;
  2202.     }

  2203.   return 0;
  2204. }

  2205. static const struct frame_unwind amd64_sigtramp_frame_unwind =
  2206. {
  2207.   SIGTRAMP_FRAME,
  2208.   amd64_sigtramp_frame_unwind_stop_reason,
  2209.   amd64_sigtramp_frame_this_id,
  2210.   amd64_sigtramp_frame_prev_register,
  2211.   NULL,
  2212.   amd64_sigtramp_frame_sniffer
  2213. };


  2214. static CORE_ADDR
  2215. amd64_frame_base_address (struct frame_info *this_frame, void **this_cache)
  2216. {
  2217.   struct amd64_frame_cache *cache =
  2218.     amd64_frame_cache (this_frame, this_cache);

  2219.   return cache->base;
  2220. }

  2221. static const struct frame_base amd64_frame_base =
  2222. {
  2223.   &amd64_frame_unwind,
  2224.   amd64_frame_base_address,
  2225.   amd64_frame_base_address,
  2226.   amd64_frame_base_address
  2227. };

  2228. /* Normal frames, but in a function epilogue.  */

  2229. /* The epilogue is defined here as the 'ret' instruction, which will
  2230.    follow any instruction such as 'leave' or 'pop %ebp' that destroys
  2231.    the function's stack frame.  */

  2232. static int
  2233. amd64_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  2234. {
  2235.   gdb_byte insn;
  2236.   struct compunit_symtab *cust;

  2237.   cust = find_pc_compunit_symtab (pc);
  2238.   if (cust != NULL && COMPUNIT_EPILOGUE_UNWIND_VALID (cust))
  2239.     return 0;

  2240.   if (target_read_memory (pc, &insn, 1))
  2241.     return 0;   /* Can't read memory at pc.  */

  2242.   if (insn != 0xc3)     /* 'ret' instruction.  */
  2243.     return 0;

  2244.   return 1;
  2245. }

  2246. static int
  2247. amd64_epilogue_frame_sniffer (const struct frame_unwind *self,
  2248.                               struct frame_info *this_frame,
  2249.                               void **this_prologue_cache)
  2250. {
  2251.   if (frame_relative_level (this_frame) == 0)
  2252.     return amd64_in_function_epilogue_p (get_frame_arch (this_frame),
  2253.                                          get_frame_pc (this_frame));
  2254.   else
  2255.     return 0;
  2256. }

  2257. static struct amd64_frame_cache *
  2258. amd64_epilogue_frame_cache (struct frame_info *this_frame, void **this_cache)
  2259. {
  2260.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2261.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2262.   volatile struct gdb_exception ex;
  2263.   struct amd64_frame_cache *cache;
  2264.   gdb_byte buf[8];

  2265.   if (*this_cache)
  2266.     return *this_cache;

  2267.   cache = amd64_alloc_frame_cache ();
  2268.   *this_cache = cache;

  2269.   TRY_CATCH (ex, RETURN_MASK_ERROR)
  2270.     {
  2271.       /* Cache base will be %esp plus cache->sp_offset (-8).  */
  2272.       get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
  2273.       cache->base = extract_unsigned_integer (buf, 8,
  2274.                                               byte_order) + cache->sp_offset;

  2275.       /* Cache pc will be the frame func.  */
  2276.       cache->pc = get_frame_pc (this_frame);

  2277.       /* The saved %esp will be at cache->base plus 16.  */
  2278.       cache->saved_sp = cache->base + 16;

  2279.       /* The saved %eip will be at cache->base plus 8.  */
  2280.       cache->saved_regs[AMD64_RIP_REGNUM] = cache->base + 8;

  2281.       cache->base_p = 1;
  2282.     }
  2283.   if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
  2284.     throw_exception (ex);

  2285.   return cache;
  2286. }

  2287. static enum unwind_stop_reason
  2288. amd64_epilogue_frame_unwind_stop_reason (struct frame_info *this_frame,
  2289.                                          void **this_cache)
  2290. {
  2291.   struct amd64_frame_cache *cache
  2292.     = amd64_epilogue_frame_cache (this_frame, this_cache);

  2293.   if (!cache->base_p)
  2294.     return UNWIND_UNAVAILABLE;

  2295.   return UNWIND_NO_REASON;
  2296. }

  2297. static void
  2298. amd64_epilogue_frame_this_id (struct frame_info *this_frame,
  2299.                               void **this_cache,
  2300.                               struct frame_id *this_id)
  2301. {
  2302.   struct amd64_frame_cache *cache = amd64_epilogue_frame_cache (this_frame,
  2303.                                                                this_cache);

  2304.   if (!cache->base_p)
  2305.     (*this_id) = frame_id_build_unavailable_stack (cache->pc);
  2306.   else
  2307.     (*this_id) = frame_id_build (cache->base + 8, cache->pc);
  2308. }

  2309. static const struct frame_unwind amd64_epilogue_frame_unwind =
  2310. {
  2311.   NORMAL_FRAME,
  2312.   amd64_epilogue_frame_unwind_stop_reason,
  2313.   amd64_epilogue_frame_this_id,
  2314.   amd64_frame_prev_register,
  2315.   NULL,
  2316.   amd64_epilogue_frame_sniffer
  2317. };

  2318. static struct frame_id
  2319. amd64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  2320. {
  2321.   CORE_ADDR fp;

  2322.   fp = get_frame_register_unsigned (this_frame, AMD64_RBP_REGNUM);

  2323.   return frame_id_build (fp + 16, get_frame_pc (this_frame));
  2324. }

  2325. /* 16 byte align the SP per frame requirements.  */

  2326. static CORE_ADDR
  2327. amd64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  2328. {
  2329.   return sp & -(CORE_ADDR)16;
  2330. }


  2331. /* Supply register REGNUM from the buffer specified by FPREGS and LEN
  2332.    in the floating-point register set REGSET to register cache
  2333.    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */

  2334. static void
  2335. amd64_supply_fpregset (const struct regset *regset, struct regcache *regcache,
  2336.                        int regnum, const void *fpregs, size_t len)
  2337. {
  2338.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  2339.   const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  2340.   gdb_assert (len == tdep->sizeof_fpregset);
  2341.   amd64_supply_fxsave (regcache, regnum, fpregs);
  2342. }

  2343. /* Collect register REGNUM from the register cache REGCACHE and store
  2344.    it in the buffer specified by FPREGS and LEN as described by the
  2345.    floating-point register set REGSET.  If REGNUM is -1, do this for
  2346.    all registers in REGSET.  */

  2347. static void
  2348. amd64_collect_fpregset (const struct regset *regset,
  2349.                         const struct regcache *regcache,
  2350.                         int regnum, void *fpregs, size_t len)
  2351. {
  2352.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  2353.   const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  2354.   gdb_assert (len == tdep->sizeof_fpregset);
  2355.   amd64_collect_fxsave (regcache, regnum, fpregs);
  2356. }

  2357. const struct regset amd64_fpregset =
  2358.   {
  2359.     NULL, amd64_supply_fpregset, amd64_collect_fpregset
  2360.   };


  2361. /* Figure out where the longjmp will land.  Slurp the jmp_buf out of
  2362.    %rdi.  We expect its value to be a pointer to the jmp_buf structure
  2363.    from which we extract the address that we will land at.  This
  2364.    address is copied into PC.  This routine returns non-zero on
  2365.    success.  */

  2366. static int
  2367. amd64_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  2368. {
  2369.   gdb_byte buf[8];
  2370.   CORE_ADDR jb_addr;
  2371.   struct gdbarch *gdbarch = get_frame_arch (frame);
  2372.   int jb_pc_offset = gdbarch_tdep (gdbarch)->jb_pc_offset;
  2373.   int len = TYPE_LENGTH (builtin_type (gdbarch)->builtin_func_ptr);

  2374.   /* If JB_PC_OFFSET is -1, we have no way to find out where the
  2375.      longjmp will land.         */
  2376.   if (jb_pc_offset == -1)
  2377.     return 0;

  2378.   get_frame_register (frame, AMD64_RDI_REGNUM, buf);
  2379.   jb_addr= extract_typed_address
  2380.             (buf, builtin_type (gdbarch)->builtin_data_ptr);
  2381.   if (target_read_memory (jb_addr + jb_pc_offset, buf, len))
  2382.     return 0;

  2383.   *pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);

  2384.   return 1;
  2385. }

  2386. static const int amd64_record_regmap[] =
  2387. {
  2388.   AMD64_RAX_REGNUM, AMD64_RCX_REGNUM, AMD64_RDX_REGNUM, AMD64_RBX_REGNUM,
  2389.   AMD64_RSP_REGNUM, AMD64_RBP_REGNUM, AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
  2390.   AMD64_R8_REGNUM, AMD64_R9_REGNUM, AMD64_R10_REGNUM, AMD64_R11_REGNUM,
  2391.   AMD64_R12_REGNUM, AMD64_R13_REGNUM, AMD64_R14_REGNUM, AMD64_R15_REGNUM,
  2392.   AMD64_RIP_REGNUM, AMD64_EFLAGS_REGNUM, AMD64_CS_REGNUM, AMD64_SS_REGNUM,
  2393.   AMD64_DS_REGNUM, AMD64_ES_REGNUM, AMD64_FS_REGNUM, AMD64_GS_REGNUM
  2394. };

  2395. void
  2396. amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  2397. {
  2398.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  2399.   const struct target_desc *tdesc = info.target_desc;
  2400.   static const char *const stap_integer_prefixes[] = { "$", NULL };
  2401.   static const char *const stap_register_prefixes[] = { "%", NULL };
  2402.   static const char *const stap_register_indirection_prefixes[] = { "(",
  2403.                                                                     NULL };
  2404.   static const char *const stap_register_indirection_suffixes[] = { ")",
  2405.                                                                     NULL };

  2406.   /* AMD64 generally uses `fxsave' instead of `fsave' for saving its
  2407.      floating-point registers.  */
  2408.   tdep->sizeof_fpregset = I387_SIZEOF_FXSAVE;
  2409.   tdep->fpregset = &amd64_fpregset;

  2410.   if (! tdesc_has_registers (tdesc))
  2411.     tdesc = tdesc_amd64;
  2412.   tdep->tdesc = tdesc;

  2413.   tdep->num_core_regs = AMD64_NUM_GREGS + I387_NUM_REGS;
  2414.   tdep->register_names = amd64_register_names;

  2415.   if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx512") != NULL)
  2416.     {
  2417.       tdep->zmmh_register_names = amd64_zmmh_names;
  2418.       tdep->k_register_names = amd64_k_names;
  2419.       tdep->xmm_avx512_register_names = amd64_xmm_avx512_names;
  2420.       tdep->ymm16h_register_names = amd64_ymmh_avx512_names;

  2421.       tdep->num_zmm_regs = 32;
  2422.       tdep->num_xmm_avx512_regs = 16;
  2423.       tdep->num_ymm_avx512_regs = 16;

  2424.       tdep->zmm0h_regnum = AMD64_ZMM0H_REGNUM;
  2425.       tdep->k0_regnum = AMD64_K0_REGNUM;
  2426.       tdep->xmm16_regnum = AMD64_XMM16_REGNUM;
  2427.       tdep->ymm16h_regnum = AMD64_YMM16H_REGNUM;
  2428.     }

  2429.   if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx") != NULL)
  2430.     {
  2431.       tdep->ymmh_register_names = amd64_ymmh_names;
  2432.       tdep->num_ymm_regs = 16;
  2433.       tdep->ymm0h_regnum = AMD64_YMM0H_REGNUM;
  2434.     }

  2435.   if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.mpx") != NULL)
  2436.     {
  2437.       tdep->mpx_register_names = amd64_mpx_names;
  2438.       tdep->bndcfgu_regnum = AMD64_BNDCFGU_REGNUM;
  2439.       tdep->bnd0r_regnum = AMD64_BND0R_REGNUM;
  2440.     }

  2441.   tdep->num_byte_regs = 20;
  2442.   tdep->num_word_regs = 16;
  2443.   tdep->num_dword_regs = 16;
  2444.   /* Avoid wiring in the MMX registers for now.  */
  2445.   tdep->num_mmx_regs = 0;

  2446.   set_gdbarch_pseudo_register_read_value (gdbarch,
  2447.                                           amd64_pseudo_register_read_value);
  2448.   set_gdbarch_pseudo_register_write (gdbarch,
  2449.                                      amd64_pseudo_register_write);

  2450.   set_tdesc_pseudo_register_name (gdbarch, amd64_pseudo_register_name);

  2451.   /* AMD64 has an FPU and 16 SSE registers.  */
  2452.   tdep->st0_regnum = AMD64_ST0_REGNUM;
  2453.   tdep->num_xmm_regs = 16;

  2454.   /* This is what all the fuss is about.  */
  2455.   set_gdbarch_long_bit (gdbarch, 64);
  2456.   set_gdbarch_long_long_bit (gdbarch, 64);
  2457.   set_gdbarch_ptr_bit (gdbarch, 64);

  2458.   /* In contrast to the i386, on AMD64 a `long double' actually takes
  2459.      up 128 bits, even though it's still based on the i387 extended
  2460.      floating-point format which has only 80 significant bits.  */
  2461.   set_gdbarch_long_double_bit (gdbarch, 128);

  2462.   set_gdbarch_num_regs (gdbarch, AMD64_NUM_REGS);

  2463.   /* Register numbers of various important registers.  */
  2464.   set_gdbarch_sp_regnum (gdbarch, AMD64_RSP_REGNUM); /* %rsp */
  2465.   set_gdbarch_pc_regnum (gdbarch, AMD64_RIP_REGNUM); /* %rip */
  2466.   set_gdbarch_ps_regnum (gdbarch, AMD64_EFLAGS_REGNUM); /* %eflags */
  2467.   set_gdbarch_fp0_regnum (gdbarch, AMD64_ST0_REGNUM); /* %st(0) */

  2468.   /* The "default" register numbering scheme for AMD64 is referred to
  2469.      as the "DWARF Register Number Mapping" in the System V psABI.
  2470.      The preferred debugging format for all known AMD64 targets is
  2471.      actually DWARF2, and GCC doesn't seem to support DWARF (that is
  2472.      DWARF-1), but we provide the same mapping just in case.  This
  2473.      mapping is also used for stabs, which GCC does support.  */
  2474.   set_gdbarch_stab_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
  2475.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);

  2476.   /* We don't override SDB_REG_RO_REGNUM, since COFF doesn't seem to
  2477.      be in use on any of the supported AMD64 targets.  */

  2478.   /* Call dummy code.  */
  2479.   set_gdbarch_push_dummy_call (gdbarch, amd64_push_dummy_call);
  2480.   set_gdbarch_frame_align (gdbarch, amd64_frame_align);
  2481.   set_gdbarch_frame_red_zone_size (gdbarch, 128);

  2482.   set_gdbarch_convert_register_p (gdbarch, i387_convert_register_p);
  2483.   set_gdbarch_register_to_value (gdbarch, i387_register_to_value);
  2484.   set_gdbarch_value_to_register (gdbarch, i387_value_to_register);

  2485.   set_gdbarch_return_value (gdbarch, amd64_return_value);

  2486.   set_gdbarch_skip_prologue (gdbarch, amd64_skip_prologue);

  2487.   tdep->record_regmap = amd64_record_regmap;

  2488.   set_gdbarch_dummy_id (gdbarch, amd64_dummy_id);

  2489.   /* Hook the function epilogue frame unwinder.  This unwinder is
  2490.      appended to the list first, so that it supercedes the other
  2491.      unwinders in function epilogues.  */
  2492.   frame_unwind_prepend_unwinder (gdbarch, &amd64_epilogue_frame_unwind);

  2493.   /* Hook the prologue-based frame unwinders.  */
  2494.   frame_unwind_append_unwinder (gdbarch, &amd64_sigtramp_frame_unwind);
  2495.   frame_unwind_append_unwinder (gdbarch, &amd64_frame_unwind);
  2496.   frame_base_set_default (gdbarch, &amd64_frame_base);

  2497.   set_gdbarch_get_longjmp_target (gdbarch, amd64_get_longjmp_target);

  2498.   set_gdbarch_relocate_instruction (gdbarch, amd64_relocate_instruction);

  2499.   set_gdbarch_gen_return_address (gdbarch, amd64_gen_return_address);

  2500.   /* SystemTap variables and functions.  */
  2501.   set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
  2502.   set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
  2503.   set_gdbarch_stap_register_indirection_prefixes (gdbarch,
  2504.                                           stap_register_indirection_prefixes);
  2505.   set_gdbarch_stap_register_indirection_suffixes (gdbarch,
  2506.                                           stap_register_indirection_suffixes);
  2507.   set_gdbarch_stap_is_single_operand (gdbarch,
  2508.                                       i386_stap_is_single_operand);
  2509.   set_gdbarch_stap_parse_special_token (gdbarch,
  2510.                                         i386_stap_parse_special_token);
  2511.   set_gdbarch_insn_is_call (gdbarch, amd64_insn_is_call);
  2512.   set_gdbarch_insn_is_ret (gdbarch, amd64_insn_is_ret);
  2513.   set_gdbarch_insn_is_jump (gdbarch, amd64_insn_is_jump);
  2514. }


  2515. static struct type *
  2516. amd64_x32_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
  2517. {
  2518.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  2519.   switch (regnum - tdep->eax_regnum)
  2520.     {
  2521.     case AMD64_RBP_REGNUM:        /* %ebp */
  2522.     case AMD64_RSP_REGNUM:        /* %esp */
  2523.       return builtin_type (gdbarch)->builtin_data_ptr;
  2524.     case AMD64_RIP_REGNUM:        /* %eip */
  2525.       return builtin_type (gdbarch)->builtin_func_ptr;
  2526.     }

  2527.   return i386_pseudo_register_type (gdbarch, regnum);
  2528. }

  2529. void
  2530. amd64_x32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  2531. {
  2532.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  2533.   const struct target_desc *tdesc = info.target_desc;

  2534.   amd64_init_abi (info, gdbarch);

  2535.   if (! tdesc_has_registers (tdesc))
  2536.     tdesc = tdesc_x32;
  2537.   tdep->tdesc = tdesc;

  2538.   tdep->num_dword_regs = 17;
  2539.   set_tdesc_pseudo_register_type (gdbarch, amd64_x32_pseudo_register_type);

  2540.   set_gdbarch_long_bit (gdbarch, 32);
  2541.   set_gdbarch_ptr_bit (gdbarch, 32);
  2542. }

  2543. /* Provide a prototype to silence -Wmissing-prototypes.  */
  2544. void _initialize_amd64_tdep (void);

  2545. void
  2546. _initialize_amd64_tdep (void)
  2547. {
  2548.   initialize_tdesc_amd64 ();
  2549.   initialize_tdesc_amd64_avx ();
  2550.   initialize_tdesc_amd64_mpx ();
  2551.   initialize_tdesc_amd64_avx512 ();

  2552.   initialize_tdesc_x32 ();
  2553.   initialize_tdesc_x32_avx ();
  2554.   initialize_tdesc_x32_avx512 ();
  2555. }


  2556. /* The 64-bit FXSAVE format differs from the 32-bit format in the
  2557.    sense that the instruction pointer and data pointer are simply
  2558.    64-bit offsets into the code segment and the data segment instead
  2559.    of a selector offset pair.  The functions below store the upper 32
  2560.    bits of these pointers (instead of just the 16-bits of the segment
  2561.    selector).  */

  2562. /* Fill register REGNUM in REGCACHE with the appropriate
  2563.    floating-point or SSE register value from *FXSAVE.  If REGNUM is
  2564.    -1, do this for all registers.  This function masks off any of the
  2565.    reserved bits in *FXSAVE.  */

  2566. void
  2567. amd64_supply_fxsave (struct regcache *regcache, int regnum,
  2568.                      const void *fxsave)
  2569. {
  2570.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  2571.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  2572.   i387_supply_fxsave (regcache, regnum, fxsave);

  2573.   if (fxsave
  2574.       && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
  2575.     {
  2576.       const gdb_byte *regs = fxsave;

  2577.       if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
  2578.         regcache_raw_supply (regcache, I387_FISEG_REGNUM (tdep), regs + 12);
  2579.       if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
  2580.         regcache_raw_supply (regcache, I387_FOSEG_REGNUM (tdep), regs + 20);
  2581.     }
  2582. }

  2583. /* Similar to amd64_supply_fxsave, but use XSAVE extended state.  */

  2584. void
  2585. amd64_supply_xsave (struct regcache *regcache, int regnum,
  2586.                     const void *xsave)
  2587. {
  2588.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  2589.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  2590.   i387_supply_xsave (regcache, regnum, xsave);

  2591.   if (xsave
  2592.       && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
  2593.     {
  2594.       const gdb_byte *regs = xsave;

  2595.       if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
  2596.         regcache_raw_supply (regcache, I387_FISEG_REGNUM (tdep),
  2597.                              regs + 12);
  2598.       if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
  2599.         regcache_raw_supply (regcache, I387_FOSEG_REGNUM (tdep),
  2600.                              regs + 20);
  2601.     }
  2602. }

  2603. /* Fill register REGNUM (if it is a floating-point or SSE register) in
  2604.    *FXSAVE with the value from REGCACHE.  If REGNUM is -1, do this for
  2605.    all registers.  This function doesn't touch any of the reserved
  2606.    bits in *FXSAVE.  */

  2607. void
  2608. amd64_collect_fxsave (const struct regcache *regcache, int regnum,
  2609.                       void *fxsave)
  2610. {
  2611.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  2612.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  2613.   gdb_byte *regs = fxsave;

  2614.   i387_collect_fxsave (regcache, regnum, fxsave);

  2615.   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
  2616.     {
  2617.       if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
  2618.         regcache_raw_collect (regcache, I387_FISEG_REGNUM (tdep), regs + 12);
  2619.       if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
  2620.         regcache_raw_collect (regcache, I387_FOSEG_REGNUM (tdep), regs + 20);
  2621.     }
  2622. }

  2623. /* Similar to amd64_collect_fxsave, but use XSAVE extended state.  */

  2624. void
  2625. amd64_collect_xsave (const struct regcache *regcache, int regnum,
  2626.                      void *xsave, int gcore)
  2627. {
  2628.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  2629.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  2630.   gdb_byte *regs = xsave;

  2631.   i387_collect_xsave (regcache, regnum, xsave, gcore);

  2632.   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
  2633.     {
  2634.       if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
  2635.         regcache_raw_collect (regcache, I387_FISEG_REGNUM (tdep),
  2636.                               regs + 12);
  2637.       if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
  2638.         regcache_raw_collect (regcache, I387_FOSEG_REGNUM (tdep),
  2639.                               regs + 20);
  2640.     }
  2641. }