gdb/i386-linux-tdep.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for GNU/Linux i386.

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

  3.    This file is part of GDB.

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

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

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

  14. #include "defs.h"
  15. #include "gdbcore.h"
  16. #include "frame.h"
  17. #include "value.h"
  18. #include "regcache.h"
  19. #include "regset.h"
  20. #include "inferior.h"
  21. #include "osabi.h"
  22. #include "reggroups.h"
  23. #include "dwarf2-frame.h"
  24. #include "i386-tdep.h"
  25. #include "i386-linux-tdep.h"
  26. #include "linux-tdep.h"
  27. #include "glibc-tdep.h"
  28. #include "solib-svr4.h"
  29. #include "symtab.h"
  30. #include "arch-utils.h"
  31. #include "xml-syscall.h"

  32. #include "i387-tdep.h"
  33. #include "x86-xstate.h"

  34. /* The syscall's XML filename for i386.  */
  35. #define XML_SYSCALL_FILENAME_I386 "syscalls/i386-linux.xml"

  36. #include "record-full.h"
  37. #include "linux-record.h"
  38. #include <stdint.h>

  39. #include "features/i386/i386-linux.c"
  40. #include "features/i386/i386-mmx-linux.c"
  41. #include "features/i386/i386-mpx-linux.c"
  42. #include "features/i386/i386-avx-linux.c"
  43. #include "features/i386/i386-avx512-linux.c"

  44. /* Return non-zero, when the register is in the corresponding register
  45.    group.  Put the LINUX_ORIG_EAX register in the system group.  */
  46. static int
  47. i386_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  48.                                 struct reggroup *group)
  49. {
  50.   if (regnum == I386_LINUX_ORIG_EAX_REGNUM)
  51.     return (group == system_reggroup
  52.             || group == save_reggroup
  53.             || group == restore_reggroup);
  54.   return i386_register_reggroup_p (gdbarch, regnum, group);
  55. }


  56. /* Recognizing signal handler frames.  */

  57. /* GNU/Linux has two flavors of signalsNormal signal handlers, and
  58.    "realtime" (RT) signals.  The RT signals can provide additional
  59.    information to the signal handler if the SA_SIGINFO flag is set
  60.    when establishing a signal handler using `sigaction'.  It is not
  61.    unlikely that future versions of GNU/Linux will support SA_SIGINFO
  62.    for normal signals too.  */

  63. /* When the i386 Linux kernel calls a signal handler and the
  64.    SA_RESTORER flag isn't set, the return address points to a bit of
  65.    code on the stack.  This function returns whether the PC appears to
  66.    be within this bit of code.

  67.    The instruction sequence for normal signals is
  68.        pop    %eax
  69.        mov    $0x77, %eax
  70.        int    $0x80
  71.    or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.

  72.    Checking for the code sequence should be somewhat reliable, because
  73.    the effect is to call the system call sigreturn.  This is unlikely
  74.    to occur anywhere other than in a signal trampoline.

  75.    It kind of sucks that we have to read memory from the process in
  76.    order to identify a signal trampoline, but there doesn't seem to be
  77.    any other way.  Therefore we only do the memory reads if no
  78.    function name could be identified, which should be the case since
  79.    the code is on the stack.

  80.    Detection of signal trampolines for handlers that set the
  81.    SA_RESTORER flag is in general not possible.  Unfortunately this is
  82.    what the GNU C Library has been doing for quite some time now.
  83.    However, as of version 2.1.2, the GNU C Library uses signal
  84.    trampolines (named __restore and __restore_rt) that are identical
  85.    to the ones used by the kernel.  Therefore, these trampolines are
  86.    supported too.  */

  87. #define LINUX_SIGTRAMP_INSN0        0x58        /* pop %eax */
  88. #define LINUX_SIGTRAMP_OFFSET0        0
  89. #define LINUX_SIGTRAMP_INSN1        0xb8        /* mov $NNNN, %eax */
  90. #define LINUX_SIGTRAMP_OFFSET1        1
  91. #define LINUX_SIGTRAMP_INSN2        0xcd        /* int */
  92. #define LINUX_SIGTRAMP_OFFSET2        6

  93. static const gdb_byte linux_sigtramp_code[] =
  94. {
  95.   LINUX_SIGTRAMP_INSN0,                                        /* pop %eax */
  96.   LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00,                /* mov $0x77, %eax */
  97.   LINUX_SIGTRAMP_INSN2, 0x80                                /* int $0x80 */
  98. };

  99. #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)

  100. /* If THIS_FRAME is a sigtramp routine, return the address of the
  101.    start of the routine.  Otherwise, return 0.  */

  102. static CORE_ADDR
  103. i386_linux_sigtramp_start (struct frame_info *this_frame)
  104. {
  105.   CORE_ADDR pc = get_frame_pc (this_frame);
  106.   gdb_byte buf[LINUX_SIGTRAMP_LEN];

  107.   /* We only recognize a signal trampoline if PC is at the start of
  108.      one of the three instructions.  We optimize for finding the PC at
  109.      the start, as will be the case when the trampoline is not the
  110.      first frame on the stack.  We assume that in the case where the
  111.      PC is not at the start of the instruction sequence, there will be
  112.      a few trailing readable bytes on the stack.  */

  113.   if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
  114.     return 0;

  115.   if (buf[0] != LINUX_SIGTRAMP_INSN0)
  116.     {
  117.       int adjust;

  118.       switch (buf[0])
  119.         {
  120.         case LINUX_SIGTRAMP_INSN1:
  121.           adjust = LINUX_SIGTRAMP_OFFSET1;
  122.           break;
  123.         case LINUX_SIGTRAMP_INSN2:
  124.           adjust = LINUX_SIGTRAMP_OFFSET2;
  125.           break;
  126.         default:
  127.           return 0;
  128.         }

  129.       pc -= adjust;

  130.       if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
  131.         return 0;
  132.     }

  133.   if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
  134.     return 0;

  135.   return pc;
  136. }

  137. /* This function does the same for RT signals.  Here the instruction
  138.    sequence is
  139.        mov    $0xad, %eax
  140.        int    $0x80
  141.    or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.

  142.    The effect is to call the system call rt_sigreturn.  */

  143. #define LINUX_RT_SIGTRAMP_INSN0                0xb8 /* mov $NNNN, %eax */
  144. #define LINUX_RT_SIGTRAMP_OFFSET0        0
  145. #define LINUX_RT_SIGTRAMP_INSN1                0xcd /* int */
  146. #define LINUX_RT_SIGTRAMP_OFFSET1        5

  147. static const gdb_byte linux_rt_sigtramp_code[] =
  148. {
  149.   LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00,        /* mov $0xad, %eax */
  150.   LINUX_RT_SIGTRAMP_INSN1, 0x80                                /* int $0x80 */
  151. };

  152. #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)

  153. /* If THIS_FRAME is an RT sigtramp routine, return the address of the
  154.    start of the routine.  Otherwise, return 0.  */

  155. static CORE_ADDR
  156. i386_linux_rt_sigtramp_start (struct frame_info *this_frame)
  157. {
  158.   CORE_ADDR pc = get_frame_pc (this_frame);
  159.   gdb_byte buf[LINUX_RT_SIGTRAMP_LEN];

  160.   /* We only recognize a signal trampoline if PC is at the start of
  161.      one of the two instructions.  We optimize for finding the PC at
  162.      the start, as will be the case when the trampoline is not the
  163.      first frame on the stack.  We assume that in the case where the
  164.      PC is not at the start of the instruction sequence, there will be
  165.      a few trailing readable bytes on the stack.  */

  166.   if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_RT_SIGTRAMP_LEN))
  167.     return 0;

  168.   if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
  169.     {
  170.       if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
  171.         return 0;

  172.       pc -= LINUX_RT_SIGTRAMP_OFFSET1;

  173.       if (!safe_frame_unwind_memory (this_frame, pc, buf,
  174.                                      LINUX_RT_SIGTRAMP_LEN))
  175.         return 0;
  176.     }

  177.   if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
  178.     return 0;

  179.   return pc;
  180. }

  181. /* Return whether THIS_FRAME corresponds to a GNU/Linux sigtramp
  182.    routine.  */

  183. static int
  184. i386_linux_sigtramp_p (struct frame_info *this_frame)
  185. {
  186.   CORE_ADDR pc = get_frame_pc (this_frame);
  187.   const char *name;

  188.   find_pc_partial_function (pc, &name, NULL, NULL);

  189.   /* If we have NAME, we can optimize the search.  The trampolines are
  190.      named __restore and __restore_rt.  However, they aren't dynamically
  191.      exported from the shared C library, so the trampoline may appear to
  192.      be part of the preceding function.  This should always be sigaction,
  193.      __sigaction, or __libc_sigaction (all aliases to the same function).  */
  194.   if (name == NULL || strstr (name, "sigaction") != NULL)
  195.     return (i386_linux_sigtramp_start (this_frame) != 0
  196.             || i386_linux_rt_sigtramp_start (this_frame) != 0);

  197.   return (strcmp ("__restore", name) == 0
  198.           || strcmp ("__restore_rt", name) == 0);
  199. }

  200. /* Return one if the PC of THIS_FRAME is in a signal trampoline which
  201.    may have DWARF-2 CFI.  */

  202. static int
  203. i386_linux_dwarf_signal_frame_p (struct gdbarch *gdbarch,
  204.                                  struct frame_info *this_frame)
  205. {
  206.   CORE_ADDR pc = get_frame_pc (this_frame);
  207.   const char *name;

  208.   find_pc_partial_function (pc, &name, NULL, NULL);

  209.   /* If a vsyscall DSO is in use, the signal trampolines may have these
  210.      names.  */
  211.   if (name && (strcmp (name, "__kernel_sigreturn") == 0
  212.                || strcmp (name, "__kernel_rt_sigreturn") == 0))
  213.     return 1;

  214.   return 0;
  215. }

  216. /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>.  */
  217. #define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20

  218. /* Assuming THIS_FRAME is a GNU/Linux sigtramp routine, return the
  219.    address of the associated sigcontext structure.  */

  220. static CORE_ADDR
  221. i386_linux_sigcontext_addr (struct frame_info *this_frame)
  222. {
  223.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  224.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  225.   CORE_ADDR pc;
  226.   CORE_ADDR sp;
  227.   gdb_byte buf[4];

  228.   get_frame_register (this_frame, I386_ESP_REGNUM, buf);
  229.   sp = extract_unsigned_integer (buf, 4, byte_order);

  230.   pc = i386_linux_sigtramp_start (this_frame);
  231.   if (pc)
  232.     {
  233.       /* The sigcontext structure lives on the stack, right after
  234.          the signum argument.  We determine the address of the
  235.          sigcontext structure by looking at the frame's stack
  236.          pointer.  Keep in mind that the first instruction of the
  237.          sigtramp code is "pop %eax".  If the PC is after this
  238.          instruction, adjust the returned value accordingly.  */
  239.       if (pc == get_frame_pc (this_frame))
  240.         return sp + 4;
  241.       return sp;
  242.     }

  243.   pc = i386_linux_rt_sigtramp_start (this_frame);
  244.   if (pc)
  245.     {
  246.       CORE_ADDR ucontext_addr;

  247.       /* The sigcontext structure is part of the user context.  A
  248.          pointer to the user context is passed as the third argument
  249.          to the signal handler.  */
  250.       read_memory (sp + 8, buf, 4);
  251.       ucontext_addr = extract_unsigned_integer (buf, 4, byte_order);
  252.       return ucontext_addr + I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
  253.     }

  254.   error (_("Couldn't recognize signal trampoline."));
  255.   return 0;
  256. }

  257. /* Set the program counter for process PTID to PC.  */

  258. static void
  259. i386_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
  260. {
  261.   regcache_cooked_write_unsigned (regcache, I386_EIP_REGNUM, pc);

  262.   /* We must be careful with modifying the program counter.  If we
  263.      just interrupted a system call, the kernel might try to restart
  264.      it when we resume the inferior.  On restarting the system call,
  265.      the kernel will try backing up the program counter even though it
  266.      no longer points at the system call.  This typically results in a
  267.      SIGSEGV or SIGILL.  We can prevent this by writing `-1' in the
  268.      "orig_eax" pseudo-register.

  269.      Note that "orig_eax" is saved when setting up a dummy call frame.
  270.      This means that it is properly restored when that frame is
  271.      popped, and that the interrupted system call will be restarted
  272.      when we resume the inferior on return from a function call from
  273.      within GDB.  In all other cases the system call will not be
  274.      restarted.  */
  275.   regcache_cooked_write_unsigned (regcache, I386_LINUX_ORIG_EAX_REGNUM, -1);
  276. }

  277. /* Record all registers but IP register for process-record.  */

  278. static int
  279. i386_all_but_ip_registers_record (struct regcache *regcache)
  280. {
  281.   if (record_full_arch_list_add_reg (regcache, I386_EAX_REGNUM))
  282.     return -1;
  283.   if (record_full_arch_list_add_reg (regcache, I386_ECX_REGNUM))
  284.     return -1;
  285.   if (record_full_arch_list_add_reg (regcache, I386_EDX_REGNUM))
  286.     return -1;
  287.   if (record_full_arch_list_add_reg (regcache, I386_EBX_REGNUM))
  288.     return -1;
  289.   if (record_full_arch_list_add_reg (regcache, I386_ESP_REGNUM))
  290.     return -1;
  291.   if (record_full_arch_list_add_reg (regcache, I386_EBP_REGNUM))
  292.     return -1;
  293.   if (record_full_arch_list_add_reg (regcache, I386_ESI_REGNUM))
  294.     return -1;
  295.   if (record_full_arch_list_add_reg (regcache, I386_EDI_REGNUM))
  296.     return -1;
  297.   if (record_full_arch_list_add_reg (regcache, I386_EFLAGS_REGNUM))
  298.     return -1;

  299.   return 0;
  300. }

  301. /* i386_canonicalize_syscall maps from the native i386 Linux set
  302.    of syscall ids into a canonical set of syscall ids used by
  303.    process record (a mostly trivial mapping, since the canonical
  304.    set was originally taken from the i386 set).  */

  305. static enum gdb_syscall
  306. i386_canonicalize_syscall (int syscall)
  307. {
  308.   enum { i386_syscall_max = 499 };

  309.   if (syscall <= i386_syscall_max)
  310.     return syscall;
  311.   else
  312.     return -1;
  313. }

  314. /* Parse the arguments of current system call instruction and record
  315.    the values of the registers and memory that will be changed into
  316.    "record_arch_list".  This instruction is "int 0x80" (Linux
  317.    Kernel2.4) or "sysenter" (Linux Kernel 2.6).

  318.    Return -1 if something wrong.  */

  319. static struct linux_record_tdep i386_linux_record_tdep;

  320. static int
  321. i386_linux_intx80_sysenter_syscall_record (struct regcache *regcache)
  322. {
  323.   int ret;
  324.   LONGEST syscall_native;
  325.   enum gdb_syscall syscall_gdb;

  326.   regcache_raw_read_signed (regcache, I386_EAX_REGNUM, &syscall_native);

  327.   syscall_gdb = i386_canonicalize_syscall (syscall_native);

  328.   if (syscall_gdb < 0)
  329.     {
  330.       printf_unfiltered (_("Process record and replay target doesn't "
  331.                            "support syscall number %s\n"),
  332.                          plongest (syscall_native));
  333.       return -1;
  334.     }

  335.   if (syscall_gdb == gdb_sys_sigreturn
  336.       || syscall_gdb == gdb_sys_rt_sigreturn)
  337.    {
  338.      if (i386_all_but_ip_registers_record (regcache))
  339.        return -1;
  340.      return 0;
  341.    }

  342.   ret = record_linux_system_call (syscall_gdb, regcache,
  343.                                   &i386_linux_record_tdep);
  344.   if (ret)
  345.     return ret;

  346.   /* Record the return value of the system call.  */
  347.   if (record_full_arch_list_add_reg (regcache, I386_EAX_REGNUM))
  348.     return -1;

  349.   return 0;
  350. }

  351. #define I386_LINUX_xstate        270
  352. #define I386_LINUX_frame_size        732

  353. static int
  354. i386_linux_record_signal (struct gdbarch *gdbarch,
  355.                           struct regcache *regcache,
  356.                           enum gdb_signal signal)
  357. {
  358.   ULONGEST esp;

  359.   if (i386_all_but_ip_registers_record (regcache))
  360.     return -1;

  361.   if (record_full_arch_list_add_reg (regcache, I386_EIP_REGNUM))
  362.     return -1;

  363.   /* Record the change in the stack.  */
  364.   regcache_raw_read_unsigned (regcache, I386_ESP_REGNUM, &esp);
  365.   /* This is for xstate.
  366.      sp -= sizeof (struct _fpstate);  */
  367.   esp -= I386_LINUX_xstate;
  368.   /* This is for frame_size.
  369.      sp -= sizeof (struct rt_sigframe);  */
  370.   esp -= I386_LINUX_frame_size;
  371.   if (record_full_arch_list_add_mem (esp,
  372.                                      I386_LINUX_xstate + I386_LINUX_frame_size))
  373.     return -1;

  374.   if (record_full_arch_list_add_end ())
  375.     return -1;

  376.   return 0;
  377. }


  378. /* Core of the implementation for gdbarch get_syscall_number.  Get pending
  379.    syscall number from REGCACHE.  If there is no pending syscall -1 will be
  380.    returned.  Pending syscall means ptrace has stepped into the syscall but
  381.    another ptrace call will step out.  PC is right after the int $0x80
  382.    / syscall / sysenter instruction in both cases, PC does not change during
  383.    the second ptrace step.  */

  384. static LONGEST
  385. i386_linux_get_syscall_number_from_regcache (struct regcache *regcache)
  386. {
  387.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  388.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  389.   /* The content of a register.  */
  390.   gdb_byte buf[4];
  391.   /* The result.  */
  392.   LONGEST ret;

  393.   /* Getting the system call number from the register.
  394.      When dealing with x86 architecture, this information
  395.      is stored at %eax register.  */
  396.   regcache_cooked_read (regcache, I386_LINUX_ORIG_EAX_REGNUM, buf);

  397.   ret = extract_signed_integer (buf, 4, byte_order);

  398.   return ret;
  399. }

  400. /* Wrapper for i386_linux_get_syscall_number_from_regcache to make it
  401.    compatible with gdbarch get_syscall_number method prototype.  */

  402. static LONGEST
  403. i386_linux_get_syscall_number (struct gdbarch *gdbarch,
  404.                                ptid_t ptid)
  405. {
  406.   struct regcache *regcache = get_thread_regcache (ptid);

  407.   return i386_linux_get_syscall_number_from_regcache (regcache);
  408. }

  409. /* The register sets used in GNU/Linux ELF core-dumps are identical to
  410.    the register sets in `struct user' that are used for a.out
  411.    core-dumps.  These are also used by ptrace(2).  The corresponding
  412.    types are `elf_gregset_t' for the general-purpose registers (with
  413.    `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
  414.    for the floating-point registers.

  415.    Those types used to be available under the names `gregset_t' and
  416.    `fpregset_t' too, and GDB used those names in the past.  But those
  417.    names are now used for the register sets used in the `mcontext_t'
  418.    type, which have a different size and layout.  */

  419. /* Mapping between the general-purpose registers in `struct user'
  420.    format and GDB's register cache layout.  */

  421. /* From <sys/reg.h>.  */
  422. int i386_linux_gregset_reg_offset[] =
  423. {
  424.   6 * 4,                        /* %eax */
  425.   1 * 4,                        /* %ecx */
  426.   2 * 4,                        /* %edx */
  427.   0 * 4,                        /* %ebx */
  428.   15 * 4,                        /* %esp */
  429.   5 * 4,                        /* %ebp */
  430.   3 * 4,                        /* %esi */
  431.   4 * 4,                        /* %edi */
  432.   12 * 4,                        /* %eip */
  433.   14 * 4,                        /* %eflags */
  434.   13 * 4,                        /* %cs */
  435.   16 * 4,                        /* %ss */
  436.   7 * 4,                        /* %ds */
  437.   8 * 4,                        /* %es */
  438.   9 * 4,                        /* %fs */
  439.   10 * 4,                        /* %gs */
  440.   -1, -1, -1, -1, -1, -1, -1, -1,
  441.   -1, -1, -1, -1, -1, -1, -1, -1,
  442.   -1, -1, -1, -1, -1, -1, -1, -1,
  443.   -1,
  444.   -1, -1, -1, -1, -1, -1, -1, -1,
  445.   -1, -1, -1, -1,                  /* MPX registers BND0 ... BND3.  */
  446.   -1, -1,                          /* MPX registers BNDCFGU, BNDSTATUS.  */
  447.   -1, -1, -1, -1, -1, -1, -1, -1, /* k0 ... k7 (AVX512)  */
  448.   -1, -1, -1, -1, -1, -1, -1, -1, /* zmm0 ... zmm7 (AVX512)  */
  449.   11 * 4,                          /* "orig_eax"  */
  450. };

  451. /* Mapping between the general-purpose registers in `struct
  452.    sigcontext' format and GDB's register cache layout.  */

  453. /* From <asm/sigcontext.h>.  */
  454. static int i386_linux_sc_reg_offset[] =
  455. {
  456.   11 * 4,                        /* %eax */
  457.   10 * 4,                        /* %ecx */
  458.   9 * 4,                        /* %edx */
  459.   8 * 4,                        /* %ebx */
  460.   7 * 4,                        /* %esp */
  461.   6 * 4,                        /* %ebp */
  462.   5 * 4,                        /* %esi */
  463.   4 * 4,                        /* %edi */
  464.   14 * 4,                        /* %eip */
  465.   16 * 4,                        /* %eflags */
  466.   15 * 4,                        /* %cs */
  467.   18 * 4,                        /* %ss */
  468.   3 * 4,                        /* %ds */
  469.   2 * 4,                        /* %es */
  470.   1 * 4,                        /* %fs */
  471.   0 * 4                                /* %gs */
  472. };

  473. /* Get XSAVE extended state xcr0 from core dump.  */

  474. uint64_t
  475. i386_linux_core_read_xcr0 (bfd *abfd)
  476. {
  477.   asection *xstate = bfd_get_section_by_name (abfd, ".reg-xstate");
  478.   uint64_t xcr0;

  479.   if (xstate)
  480.     {
  481.       size_t size = bfd_section_size (abfd, xstate);

  482.       /* Check extended state size.  */
  483.       if (size < X86_XSTATE_AVX_SIZE)
  484.         xcr0 = X86_XSTATE_SSE_MASK;
  485.       else
  486.         {
  487.           char contents[8];

  488.           if (! bfd_get_section_contents (abfd, xstate, contents,
  489.                                           I386_LINUX_XSAVE_XCR0_OFFSET,
  490.                                           8))
  491.             {
  492.               warning (_("Couldn't read `xcr0' bytes from "
  493.                          "`.reg-xstate' section in core file."));
  494.               return 0;
  495.             }

  496.           xcr0 = bfd_get_64 (abfd, contents);
  497.         }
  498.     }
  499.   else
  500.     xcr0 = 0;

  501.   return xcr0;
  502. }

  503. /* Get Linux/x86 target description from core dump.  */

  504. static const struct target_desc *
  505. i386_linux_core_read_description (struct gdbarch *gdbarch,
  506.                                   struct target_ops *target,
  507.                                   bfd *abfd)
  508. {
  509.   /* Linux/i386.  */
  510.   uint64_t xcr0 = i386_linux_core_read_xcr0 (abfd);

  511.   switch ((xcr0 & X86_XSTATE_ALL_MASK))
  512.     {
  513.     case X86_XSTATE_MPX_AVX512_MASK:
  514.     case X86_XSTATE_AVX512_MASK:
  515.       return tdesc_i386_avx512_linux;
  516.     case X86_XSTATE_MPX_MASK:
  517.       return tdesc_i386_mpx_linux;
  518.     case X86_XSTATE_AVX_MASK:
  519.       return tdesc_i386_avx_linux;
  520.     case X86_XSTATE_SSE_MASK:
  521.       return tdesc_i386_linux;
  522.     case X86_XSTATE_X87_MASK:
  523.       return tdesc_i386_mmx_linux;
  524.     default:
  525.       break;
  526.     }

  527.   if (bfd_get_section_by_name (abfd, ".reg-xfp") != NULL)
  528.     return tdesc_i386_linux;
  529.   else
  530.     return tdesc_i386_mmx_linux;
  531. }

  532. /* Similar to i386_supply_fpregset, but use XSAVE extended state.  */

  533. static void
  534. i386_linux_supply_xstateregset (const struct regset *regset,
  535.                                 struct regcache *regcache, int regnum,
  536.                                 const void *xstateregs, size_t len)
  537. {
  538.   i387_supply_xsave (regcache, regnum, xstateregs);
  539. }

  540. /* Similar to i386_collect_fpregset, but use XSAVE extended state.  */

  541. static void
  542. i386_linux_collect_xstateregset (const struct regset *regset,
  543.                                  const struct regcache *regcache,
  544.                                  int regnum, void *xstateregs, size_t len)
  545. {
  546.   i387_collect_xsave (regcache, regnum, xstateregs, 1);
  547. }

  548. /* Register set definitions.  */

  549. static const struct regset i386_linux_xstateregset =
  550.   {
  551.     NULL,
  552.     i386_linux_supply_xstateregset,
  553.     i386_linux_collect_xstateregset
  554.   };

  555. /* Iterate over core file register note sections.  */

  556. static void
  557. i386_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
  558.                                          iterate_over_regset_sections_cb *cb,
  559.                                          void *cb_data,
  560.                                          const struct regcache *regcache)
  561. {
  562.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  563.   cb (".reg", 68, &i386_gregset, NULL, cb_data);

  564.   if (tdep->xcr0 & X86_XSTATE_AVX)
  565.     /* Use max size for writing, accept any size when reading.  */
  566.     cb (".reg-xstate", regcache ? X86_XSTATE_MAX_SIZE : 0,
  567.         &i386_linux_xstateregset, "XSAVE extended state", cb_data);
  568.   else if (tdep->xcr0 & X86_XSTATE_SSE)
  569.     cb (".reg-xfp", 512, &i386_fpregset, "extended floating-point",
  570.         cb_data);
  571.   else
  572.     cb (".reg2", 108, &i386_fpregset, NULL, cb_data);
  573. }

  574. /* Linux kernel shows PC value after the 'int $0x80' instruction even if
  575.    inferior is still inside the syscall.  On next PTRACE_SINGLESTEP it will
  576.    finish the syscall but PC will not change.

  577.    Some vDSOs contain 'int $0x80; ret' and during stepping out of the syscall
  578.    i386_displaced_step_fixup would keep PC at the displaced pad location.
  579.    As PC is pointing to the 'ret' instruction before the step
  580.    i386_displaced_step_fixup would expect inferior has just executed that 'ret'
  581.    and PC should not be adjusted.  In reality it finished syscall instead and
  582.    PC should get relocated back to its vDSO address.  Hide the 'ret'
  583.    instruction by 'nop' so that i386_displaced_step_fixup is not confused.

  584.    It is not fully correct as the bytes in struct displaced_step_closure will
  585.    not match the inferior code.  But we would need some new flag in
  586.    displaced_step_closure otherwise to keep the state that syscall is finishing
  587.    for the later i386_displaced_step_fixup execution as the syscall execution
  588.    is already no longer detectable there.  The new flag field would mean
  589.    i386-linux-tdep.c needs to wrap all the displacement methods of i386-tdep.c
  590.    which does not seem worth it.  The same effect is achieved by patching that
  591.    'nop' instruction there instead.  */

  592. static struct displaced_step_closure *
  593. i386_linux_displaced_step_copy_insn (struct gdbarch *gdbarch,
  594.                                      CORE_ADDR from, CORE_ADDR to,
  595.                                      struct regcache *regs)
  596. {
  597.   struct displaced_step_closure *closure;

  598.   closure = i386_displaced_step_copy_insn (gdbarch, from, to, regs);

  599.   if (i386_linux_get_syscall_number_from_regcache (regs) != -1)
  600.     {
  601.       /* Since we use simple_displaced_step_copy_insn, our closure is a
  602.          copy of the instruction.  */
  603.       gdb_byte *insn = (gdb_byte *) closure;

  604.       /* Fake nop.  */
  605.       insn[0] = 0x90;
  606.     }

  607.   return closure;
  608. }

  609. static void
  610. i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  611. {
  612.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  613.   const struct target_desc *tdesc = info.target_desc;
  614.   struct tdesc_arch_data *tdesc_data = (void *) info.tdep_info;
  615.   const struct tdesc_feature *feature;
  616.   int valid_p;

  617.   gdb_assert (tdesc_data);

  618.   linux_init_abi (info, gdbarch);

  619.   /* GNU/Linux uses ELF.  */
  620.   i386_elf_init_abi (info, gdbarch);

  621.   /* Reserve a number for orig_eax.  */
  622.   set_gdbarch_num_regs (gdbarch, I386_LINUX_NUM_REGS);

  623.   if (! tdesc_has_registers (tdesc))
  624.     tdesc = tdesc_i386_linux;
  625.   tdep->tdesc = tdesc;

  626.   feature = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.linux");
  627.   if (feature == NULL)
  628.     return;

  629.   valid_p = tdesc_numbered_register (feature, tdesc_data,
  630.                                      I386_LINUX_ORIG_EAX_REGNUM,
  631.                                      "orig_eax");
  632.   if (!valid_p)
  633.     return;

  634.   /* Add the %orig_eax register used for syscall restarting.  */
  635.   set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);

  636.   tdep->register_reggroup_p = i386_linux_register_reggroup_p;

  637.   tdep->gregset_reg_offset = i386_linux_gregset_reg_offset;
  638.   tdep->gregset_num_regs = ARRAY_SIZE (i386_linux_gregset_reg_offset);
  639.   tdep->sizeof_gregset = 17 * 4;

  640.   tdep->jb_pc_offset = 20;        /* From <bits/setjmp.h>.  */

  641.   tdep->sigtramp_p = i386_linux_sigtramp_p;
  642.   tdep->sigcontext_addr = i386_linux_sigcontext_addr;
  643.   tdep->sc_reg_offset = i386_linux_sc_reg_offset;
  644.   tdep->sc_num_regs = ARRAY_SIZE (i386_linux_sc_reg_offset);

  645.   tdep->xsave_xcr0_offset = I386_LINUX_XSAVE_XCR0_OFFSET;

  646.   set_gdbarch_process_record (gdbarch, i386_process_record);
  647.   set_gdbarch_process_record_signal (gdbarch, i386_linux_record_signal);

  648.   /* Initialize the i386_linux_record_tdep.  */
  649.   /* These values are the size of the type that will be used in a system
  650.      call.  They are obtained from Linux Kernel source.  */
  651.   i386_linux_record_tdep.size_pointer
  652.     = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
  653.   i386_linux_record_tdep.size__old_kernel_stat = 32;
  654.   i386_linux_record_tdep.size_tms = 16;
  655.   i386_linux_record_tdep.size_loff_t = 8;
  656.   i386_linux_record_tdep.size_flock = 16;
  657.   i386_linux_record_tdep.size_oldold_utsname = 45;
  658.   i386_linux_record_tdep.size_ustat = 20;
  659.   i386_linux_record_tdep.size_old_sigaction = 140;
  660.   i386_linux_record_tdep.size_old_sigset_t = 128;
  661.   i386_linux_record_tdep.size_rlimit = 8;
  662.   i386_linux_record_tdep.size_rusage = 72;
  663.   i386_linux_record_tdep.size_timeval = 8;
  664.   i386_linux_record_tdep.size_timezone = 8;
  665.   i386_linux_record_tdep.size_old_gid_t = 2;
  666.   i386_linux_record_tdep.size_old_uid_t = 2;
  667.   i386_linux_record_tdep.size_fd_set = 128;
  668.   i386_linux_record_tdep.size_dirent = 268;
  669.   i386_linux_record_tdep.size_dirent64 = 276;
  670.   i386_linux_record_tdep.size_statfs = 64;
  671.   i386_linux_record_tdep.size_statfs64 = 84;
  672.   i386_linux_record_tdep.size_sockaddr = 16;
  673.   i386_linux_record_tdep.size_int
  674.     = gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT;
  675.   i386_linux_record_tdep.size_long
  676.     = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
  677.   i386_linux_record_tdep.size_ulong
  678.     = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
  679.   i386_linux_record_tdep.size_msghdr = 28;
  680.   i386_linux_record_tdep.size_itimerval = 16;
  681.   i386_linux_record_tdep.size_stat = 88;
  682.   i386_linux_record_tdep.size_old_utsname = 325;
  683.   i386_linux_record_tdep.size_sysinfo = 64;
  684.   i386_linux_record_tdep.size_msqid_ds = 88;
  685.   i386_linux_record_tdep.size_shmid_ds = 84;
  686.   i386_linux_record_tdep.size_new_utsname = 390;
  687.   i386_linux_record_tdep.size_timex = 128;
  688.   i386_linux_record_tdep.size_mem_dqinfo = 24;
  689.   i386_linux_record_tdep.size_if_dqblk = 68;
  690.   i386_linux_record_tdep.size_fs_quota_stat = 68;
  691.   i386_linux_record_tdep.size_timespec = 8;
  692.   i386_linux_record_tdep.size_pollfd = 8;
  693.   i386_linux_record_tdep.size_NFS_FHSIZE = 32;
  694.   i386_linux_record_tdep.size_knfsd_fh = 132;
  695.   i386_linux_record_tdep.size_TASK_COMM_LEN = 16;
  696.   i386_linux_record_tdep.size_sigaction = 140;
  697.   i386_linux_record_tdep.size_sigset_t = 8;
  698.   i386_linux_record_tdep.size_siginfo_t = 128;
  699.   i386_linux_record_tdep.size_cap_user_data_t = 12;
  700.   i386_linux_record_tdep.size_stack_t = 12;
  701.   i386_linux_record_tdep.size_off_t = i386_linux_record_tdep.size_long;
  702.   i386_linux_record_tdep.size_stat64 = 96;
  703.   i386_linux_record_tdep.size_gid_t = 2;
  704.   i386_linux_record_tdep.size_uid_t = 2;
  705.   i386_linux_record_tdep.size_PAGE_SIZE = 4096;
  706.   i386_linux_record_tdep.size_flock64 = 24;
  707.   i386_linux_record_tdep.size_user_desc = 16;
  708.   i386_linux_record_tdep.size_io_event = 32;
  709.   i386_linux_record_tdep.size_iocb = 64;
  710.   i386_linux_record_tdep.size_epoll_event = 12;
  711.   i386_linux_record_tdep.size_itimerspec
  712.     = i386_linux_record_tdep.size_timespec * 2;
  713.   i386_linux_record_tdep.size_mq_attr = 32;
  714.   i386_linux_record_tdep.size_siginfo = 128;
  715.   i386_linux_record_tdep.size_termios = 36;
  716.   i386_linux_record_tdep.size_termios2 = 44;
  717.   i386_linux_record_tdep.size_pid_t = 4;
  718.   i386_linux_record_tdep.size_winsize = 8;
  719.   i386_linux_record_tdep.size_serial_struct = 60;
  720.   i386_linux_record_tdep.size_serial_icounter_struct = 80;
  721.   i386_linux_record_tdep.size_hayes_esp_config = 12;
  722.   i386_linux_record_tdep.size_size_t = 4;
  723.   i386_linux_record_tdep.size_iovec = 8;

  724.   /* These values are the second argument of system call "sys_ioctl".
  725.      They are obtained from Linux Kernel source.  */
  726.   i386_linux_record_tdep.ioctl_TCGETS = 0x5401;
  727.   i386_linux_record_tdep.ioctl_TCSETS = 0x5402;
  728.   i386_linux_record_tdep.ioctl_TCSETSW = 0x5403;
  729.   i386_linux_record_tdep.ioctl_TCSETSF = 0x5404;
  730.   i386_linux_record_tdep.ioctl_TCGETA = 0x5405;
  731.   i386_linux_record_tdep.ioctl_TCSETA = 0x5406;
  732.   i386_linux_record_tdep.ioctl_TCSETAW = 0x5407;
  733.   i386_linux_record_tdep.ioctl_TCSETAF = 0x5408;
  734.   i386_linux_record_tdep.ioctl_TCSBRK = 0x5409;
  735.   i386_linux_record_tdep.ioctl_TCXONC = 0x540A;
  736.   i386_linux_record_tdep.ioctl_TCFLSH = 0x540B;
  737.   i386_linux_record_tdep.ioctl_TIOCEXCL = 0x540C;
  738.   i386_linux_record_tdep.ioctl_TIOCNXCL = 0x540D;
  739.   i386_linux_record_tdep.ioctl_TIOCSCTTY = 0x540E;
  740.   i386_linux_record_tdep.ioctl_TIOCGPGRP = 0x540F;
  741.   i386_linux_record_tdep.ioctl_TIOCSPGRP = 0x5410;
  742.   i386_linux_record_tdep.ioctl_TIOCOUTQ = 0x5411;
  743.   i386_linux_record_tdep.ioctl_TIOCSTI = 0x5412;
  744.   i386_linux_record_tdep.ioctl_TIOCGWINSZ = 0x5413;
  745.   i386_linux_record_tdep.ioctl_TIOCSWINSZ = 0x5414;
  746.   i386_linux_record_tdep.ioctl_TIOCMGET = 0x5415;
  747.   i386_linux_record_tdep.ioctl_TIOCMBIS = 0x5416;
  748.   i386_linux_record_tdep.ioctl_TIOCMBIC = 0x5417;
  749.   i386_linux_record_tdep.ioctl_TIOCMSET = 0x5418;
  750.   i386_linux_record_tdep.ioctl_TIOCGSOFTCAR = 0x5419;
  751.   i386_linux_record_tdep.ioctl_TIOCSSOFTCAR = 0x541A;
  752.   i386_linux_record_tdep.ioctl_FIONREAD = 0x541B;
  753.   i386_linux_record_tdep.ioctl_TIOCINQ = i386_linux_record_tdep.ioctl_FIONREAD;
  754.   i386_linux_record_tdep.ioctl_TIOCLINUX = 0x541C;
  755.   i386_linux_record_tdep.ioctl_TIOCCONS = 0x541D;
  756.   i386_linux_record_tdep.ioctl_TIOCGSERIAL = 0x541E;
  757.   i386_linux_record_tdep.ioctl_TIOCSSERIAL = 0x541F;
  758.   i386_linux_record_tdep.ioctl_TIOCPKT = 0x5420;
  759.   i386_linux_record_tdep.ioctl_FIONBIO = 0x5421;
  760.   i386_linux_record_tdep.ioctl_TIOCNOTTY = 0x5422;
  761.   i386_linux_record_tdep.ioctl_TIOCSETD = 0x5423;
  762.   i386_linux_record_tdep.ioctl_TIOCGETD = 0x5424;
  763.   i386_linux_record_tdep.ioctl_TCSBRKP = 0x5425;
  764.   i386_linux_record_tdep.ioctl_TIOCTTYGSTRUCT = 0x5426;
  765.   i386_linux_record_tdep.ioctl_TIOCSBRK = 0x5427;
  766.   i386_linux_record_tdep.ioctl_TIOCCBRK = 0x5428;
  767.   i386_linux_record_tdep.ioctl_TIOCGSID = 0x5429;
  768.   i386_linux_record_tdep.ioctl_TCGETS2 = 0x802c542a;
  769.   i386_linux_record_tdep.ioctl_TCSETS2 = 0x402c542b;
  770.   i386_linux_record_tdep.ioctl_TCSETSW2 = 0x402c542c;
  771.   i386_linux_record_tdep.ioctl_TCSETSF2 = 0x402c542d;
  772.   i386_linux_record_tdep.ioctl_TIOCGPTN = 0x80045430;
  773.   i386_linux_record_tdep.ioctl_TIOCSPTLCK = 0x40045431;
  774.   i386_linux_record_tdep.ioctl_FIONCLEX = 0x5450;
  775.   i386_linux_record_tdep.ioctl_FIOCLEX = 0x5451;
  776.   i386_linux_record_tdep.ioctl_FIOASYNC = 0x5452;
  777.   i386_linux_record_tdep.ioctl_TIOCSERCONFIG = 0x5453;
  778.   i386_linux_record_tdep.ioctl_TIOCSERGWILD = 0x5454;
  779.   i386_linux_record_tdep.ioctl_TIOCSERSWILD = 0x5455;
  780.   i386_linux_record_tdep.ioctl_TIOCGLCKTRMIOS = 0x5456;
  781.   i386_linux_record_tdep.ioctl_TIOCSLCKTRMIOS = 0x5457;
  782.   i386_linux_record_tdep.ioctl_TIOCSERGSTRUCT = 0x5458;
  783.   i386_linux_record_tdep.ioctl_TIOCSERGETLSR = 0x5459;
  784.   i386_linux_record_tdep.ioctl_TIOCSERGETMULTI = 0x545A;
  785.   i386_linux_record_tdep.ioctl_TIOCSERSETMULTI = 0x545B;
  786.   i386_linux_record_tdep.ioctl_TIOCMIWAIT = 0x545C;
  787.   i386_linux_record_tdep.ioctl_TIOCGICOUNT = 0x545D;
  788.   i386_linux_record_tdep.ioctl_TIOCGHAYESESP = 0x545E;
  789.   i386_linux_record_tdep.ioctl_TIOCSHAYESESP = 0x545F;
  790.   i386_linux_record_tdep.ioctl_FIOQSIZE = 0x5460;

  791.   /* These values are the second argument of system call "sys_fcntl"
  792.      and "sys_fcntl64".  They are obtained from Linux Kernel source.  */
  793.   i386_linux_record_tdep.fcntl_F_GETLK = 5;
  794.   i386_linux_record_tdep.fcntl_F_GETLK64 = 12;
  795.   i386_linux_record_tdep.fcntl_F_SETLK64 = 13;
  796.   i386_linux_record_tdep.fcntl_F_SETLKW64 = 14;

  797.   i386_linux_record_tdep.arg1 = I386_EBX_REGNUM;
  798.   i386_linux_record_tdep.arg2 = I386_ECX_REGNUM;
  799.   i386_linux_record_tdep.arg3 = I386_EDX_REGNUM;
  800.   i386_linux_record_tdep.arg4 = I386_ESI_REGNUM;
  801.   i386_linux_record_tdep.arg5 = I386_EDI_REGNUM;
  802.   i386_linux_record_tdep.arg6 = I386_EBP_REGNUM;

  803.   tdep->i386_intx80_record = i386_linux_intx80_sysenter_syscall_record;
  804.   tdep->i386_sysenter_record = i386_linux_intx80_sysenter_syscall_record;
  805.   tdep->i386_syscall_record = i386_linux_intx80_sysenter_syscall_record;

  806.   /* N_FUN symbols in shared libaries have 0 for their values and need
  807.      to be relocated.  */
  808.   set_gdbarch_sofun_address_maybe_missing (gdbarch, 1);

  809.   /* GNU/Linux uses SVR4-style shared libraries.  */
  810.   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
  811.   set_solib_svr4_fetch_link_map_offsets
  812.     (gdbarch, svr4_ilp32_fetch_link_map_offsets);

  813.   /* GNU/Linux uses the dynamic linker included in the GNU C Library.  */
  814.   set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);

  815.   dwarf2_frame_set_signal_frame_p (gdbarch, i386_linux_dwarf_signal_frame_p);

  816.   /* Enable TLS support.  */
  817.   set_gdbarch_fetch_tls_load_module_address (gdbarch,
  818.                                              svr4_fetch_objfile_link_map);

  819.   /* Core file support.  */
  820.   set_gdbarch_iterate_over_regset_sections
  821.     (gdbarch, i386_linux_iterate_over_regset_sections);
  822.   set_gdbarch_core_read_description (gdbarch,
  823.                                      i386_linux_core_read_description);

  824.   /* Displaced stepping.  */
  825.   set_gdbarch_displaced_step_copy_insn (gdbarch,
  826.                                         i386_linux_displaced_step_copy_insn);
  827.   set_gdbarch_displaced_step_fixup (gdbarch, i386_displaced_step_fixup);
  828.   set_gdbarch_displaced_step_free_closure (gdbarch,
  829.                                            simple_displaced_step_free_closure);
  830.   set_gdbarch_displaced_step_location (gdbarch,
  831.                                        displaced_step_at_entry_point);

  832.   /* Functions for 'catch syscall'.  */
  833.   set_xml_syscall_file_name (gdbarch, XML_SYSCALL_FILENAME_I386);
  834.   set_gdbarch_get_syscall_number (gdbarch,
  835.                                   i386_linux_get_syscall_number);

  836.   set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);
  837. }

  838. /* Provide a prototype to silence -Wmissing-prototypes.  */
  839. extern void _initialize_i386_linux_tdep (void);

  840. void
  841. _initialize_i386_linux_tdep (void)
  842. {
  843.   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
  844.                           i386_linux_init_abi);

  845.   /* Initialize the Linux target description.  */
  846.   initialize_tdesc_i386_linux ();
  847.   initialize_tdesc_i386_mmx_linux ();
  848.   initialize_tdesc_i386_avx_linux ();
  849.   initialize_tdesc_i386_mpx_linux ();
  850.   initialize_tdesc_i386_avx512_linux ();
  851. }