gdb/amd64-linux-nat.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Native-dependent code for GNU/Linux x86-64.

  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 "inferior.h"
  17. #include "regcache.h"
  18. #include "elf/common.h"
  19. #include <sys/uio.h>
  20. #include <sys/ptrace.h>
  21. #include <asm/prctl.h>
  22. #include <sys/reg.h>
  23. #include "gregset.h"
  24. #include "gdb_proc_service.h"

  25. #include "amd64-nat.h"
  26. #include "linux-nat.h"
  27. #include "amd64-tdep.h"
  28. #include "amd64-linux-tdep.h"
  29. #include "i386-linux-tdep.h"
  30. #include "x86-xstate.h"

  31. #include "x86-linux-nat.h"

  32. /* Mapping between the general-purpose registers in GNU/Linux x86-64
  33.    `struct user' format and GDB's register cache layout for GNU/Linux
  34.    i386.

  35.    Note that most GNU/Linux x86-64 registers are 64-bit, while the
  36.    GNU/Linux i386 registers are all 32-bit, but since we're
  37.    little-endian we get away with that.  */

  38. /* From <sys/reg.h> on GNU/Linux i386.  */
  39. static int amd64_linux_gregset32_reg_offset[] =
  40. {
  41.   RAX * 8, RCX * 8,                /* %eax, %ecx */
  42.   RDX * 8, RBX * 8,                /* %edx, %ebx */
  43.   RSP * 8, RBP * 8,                /* %esp, %ebp */
  44.   RSI * 8, RDI * 8,                /* %esi, %edi */
  45.   RIP * 8, EFLAGS * 8,                /* %eip, %eflags */
  46.   CS * 8, SS * 8,                /* %cs, %ss */
  47.   DS * 8, ES * 8,                /* %ds, %es */
  48.   FS * 8, GS * 8,                /* %fs, %gs */
  49.   -1, -1, -1, -1, -1, -1, -1, -1,
  50.   -1, -1, -1, -1, -1, -1, -1, -1,
  51.   -1, -1, -1, -1, -1, -1, -1, -1, -1,
  52.   -1, -1, -1, -1, -1, -1, -1, -1,
  53.   -1, -1, -1, -1,                  /* MPX registers BND0 ... BND3.  */
  54.   -1, -1,                          /* MPX registers BNDCFGU, BNDSTATUS.  */
  55.   -1, -1, -1, -1, -1, -1, -1, -1, /* k0 ... k7 (AVX512)  */
  56.   -1, -1, -1, -1, -1, -1, -1, -1, /* zmm0 ... zmm7 (AVX512)  */
  57.   ORIG_RAX * 8                          /* "orig_eax"  */
  58. };


  59. /* Transfering the general-purpose registers between GDB, inferiors
  60.    and core files.  */

  61. /* Fill GDB's register cache with the general-purpose register values
  62.    in *GREGSETP.  */

  63. void
  64. supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
  65. {
  66.   amd64_supply_native_gregset (regcache, gregsetp, -1);
  67. }

  68. /* Fill register REGNUM (if it is a general-purpose register) in
  69.    *GREGSETP with the value in GDB's register cache.  If REGNUM is -1,
  70.    do this for all registers.  */

  71. void
  72. fill_gregset (const struct regcache *regcache,
  73.               elf_gregset_t *gregsetp, int regnum)
  74. {
  75.   amd64_collect_native_gregset (regcache, gregsetp, regnum);
  76. }

  77. /* Transfering floating-point registers between GDB, inferiors and cores.  */

  78. /* Fill GDB's register cache with the floating-point and SSE register
  79.    values in *FPREGSETP.  */

  80. void
  81. supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
  82. {
  83.   amd64_supply_fxsave (regcache, -1, fpregsetp);
  84. }

  85. /* Fill register REGNUM (if it is a floating-point or SSE register) in
  86.    *FPREGSETP with the value in GDB's register cache.  If REGNUM is
  87.    -1, do this for all registers.  */

  88. void
  89. fill_fpregset (const struct regcache *regcache,
  90.                elf_fpregset_t *fpregsetp, int regnum)
  91. {
  92.   amd64_collect_fxsave (regcache, regnum, fpregsetp);
  93. }


  94. /* Transferring arbitrary registers between GDB and inferior.  */

  95. /* Fetch register REGNUM from the child process.  If REGNUM is -1, do
  96.    this for all registers (including the floating point and SSE
  97.    registers).  */

  98. static void
  99. amd64_linux_fetch_inferior_registers (struct target_ops *ops,
  100.                                       struct regcache *regcache, int regnum)
  101. {
  102.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  103.   int tid;

  104.   /* GNU/Linux LWP ID's are process ID's.  */
  105.   tid = ptid_get_lwp (inferior_ptid);
  106.   if (tid == 0)
  107.     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */

  108.   if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
  109.     {
  110.       elf_gregset_t regs;

  111.       if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
  112.         perror_with_name (_("Couldn't get registers"));

  113.       amd64_supply_native_gregset (regcache, &regs, -1);
  114.       if (regnum != -1)
  115.         return;
  116.     }

  117.   if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
  118.     {
  119.       elf_fpregset_t fpregs;

  120.       if (have_ptrace_getregset)
  121.         {
  122.           char xstateregs[X86_XSTATE_MAX_SIZE];
  123.           struct iovec iov;

  124.           iov.iov_base = xstateregs;
  125.           iov.iov_len = sizeof (xstateregs);
  126.           if (ptrace (PTRACE_GETREGSET, tid,
  127.                       (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
  128.             perror_with_name (_("Couldn't get extended state status"));

  129.           amd64_supply_xsave (regcache, -1, xstateregs);
  130.         }
  131.       else
  132.         {
  133.           if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
  134.             perror_with_name (_("Couldn't get floating point status"));

  135.           amd64_supply_fxsave (regcache, -1, &fpregs);
  136.         }
  137.     }
  138. }

  139. /* Store register REGNUM back into the child process.  If REGNUM is
  140.    -1, do this for all registers (including the floating-point and SSE
  141.    registers).  */

  142. static void
  143. amd64_linux_store_inferior_registers (struct target_ops *ops,
  144.                                       struct regcache *regcache, int regnum)
  145. {
  146.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  147.   int tid;

  148.   /* GNU/Linux LWP ID's are process ID's.  */
  149.   tid = ptid_get_lwp (inferior_ptid);
  150.   if (tid == 0)
  151.     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */

  152.   if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
  153.     {
  154.       elf_gregset_t regs;

  155.       if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
  156.         perror_with_name (_("Couldn't get registers"));

  157.       amd64_collect_native_gregset (regcache, &regs, regnum);

  158.       if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
  159.         perror_with_name (_("Couldn't write registers"));

  160.       if (regnum != -1)
  161.         return;
  162.     }

  163.   if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
  164.     {
  165.       elf_fpregset_t fpregs;

  166.       if (have_ptrace_getregset)
  167.         {
  168.           char xstateregs[X86_XSTATE_MAX_SIZE];
  169.           struct iovec iov;

  170.           iov.iov_base = xstateregs;
  171.           iov.iov_len = sizeof (xstateregs);
  172.           if (ptrace (PTRACE_GETREGSET, tid,
  173.                       (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
  174.             perror_with_name (_("Couldn't get extended state status"));

  175.           amd64_collect_xsave (regcache, regnum, xstateregs, 0);

  176.           if (ptrace (PTRACE_SETREGSET, tid,
  177.                       (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
  178.             perror_with_name (_("Couldn't write extended state status"));
  179.         }
  180.       else
  181.         {
  182.           if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
  183.             perror_with_name (_("Couldn't get floating point status"));

  184.           amd64_collect_fxsave (regcache, regnum, &fpregs);

  185.           if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0)
  186.             perror_with_name (_("Couldn't write floating point status"));
  187.         }
  188.     }
  189. }


  190. /* This function is called by libthread_db as part of its handling of
  191.    a request for a thread's local storage address.  */

  192. ps_err_e
  193. ps_get_thread_area (const struct ps_prochandle *ph,
  194.                     lwpid_t lwpid, int idx, void **base)
  195. {
  196.   if (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word == 32)
  197.     {
  198.       unsigned int base_addr;
  199.       ps_err_e result;

  200.       result = x86_linux_get_thread_area (lwpid, (void *) (long) idx,
  201.                                           &base_addr);
  202.       if (result == PS_OK)
  203.         {
  204.           /* Extend the value to 64 bits.  Here it's assumed that
  205.              a "long" and a "void *" are the same.  */
  206.           (*base) = (void *) (long) base_addr;
  207.         }
  208.       return result;
  209.     }
  210.   else
  211.     {
  212.       /* This definition comes from prctl.h, but some kernels may not
  213.          have it.  */
  214. #ifndef PTRACE_ARCH_PRCTL
  215. #define PTRACE_ARCH_PRCTL      30
  216. #endif
  217.       /* FIXME: ezannoni-2003-07-09 see comment above about include
  218.          file order.  We could be getting bogus values for these two.  */
  219.       gdb_assert (FS < ELF_NGREG);
  220.       gdb_assert (GS < ELF_NGREG);
  221.       switch (idx)
  222.         {
  223.         case FS:
  224. #ifdef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
  225.             {
  226.               /* PTRACE_ARCH_PRCTL is obsolete since 2.6.25, where the
  227.                  fs_base and gs_base fields of user_regs_struct can be
  228.                  used directly.  */
  229.               unsigned long fs;
  230.               errno = 0;
  231.               fs = ptrace (PTRACE_PEEKUSER, lwpid,
  232.                            offsetof (struct user_regs_struct, fs_base), 0);
  233.               if (errno == 0)
  234.                 {
  235.                   *base = (void *) fs;
  236.                   return PS_OK;
  237.                 }
  238.             }
  239. #endif
  240.           if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0)
  241.             return PS_OK;
  242.           break;
  243.         case GS:
  244. #ifdef HAVE_STRUCT_USER_REGS_STRUCT_GS_BASE
  245.             {
  246.               unsigned long gs;
  247.               errno = 0;
  248.               gs = ptrace (PTRACE_PEEKUSER, lwpid,
  249.                            offsetof (struct user_regs_struct, gs_base), 0);
  250.               if (errno == 0)
  251.                 {
  252.                   *base = (void *) gs;
  253.                   return PS_OK;
  254.                 }
  255.             }
  256. #endif
  257.           if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0)
  258.             return PS_OK;
  259.           break;
  260.         default:                   /* Should not happen.  */
  261.           return PS_BADADDR;
  262.         }
  263.     }
  264.   return PS_ERR;               /* ptrace failed.  */
  265. }


  266. /* When GDB is built as a 64-bit application on linux, the
  267.    PTRACE_GETSIGINFO data is always presented in 64-bit layout.  Since
  268.    debugging a 32-bit inferior with a 64-bit GDB should look the same
  269.    as debugging it with a 32-bit GDB, we do the 32-bit <-> 64-bit
  270.    conversion in-place ourselves.  */

  271. /* These types below (compat_*) define a siginfo type that is layout
  272.    compatible with the siginfo type exported by the 32-bit userspace
  273.    support.  */

  274. typedef int compat_int_t;
  275. typedef unsigned int compat_uptr_t;

  276. typedef int compat_time_t;
  277. typedef int compat_timer_t;
  278. typedef int compat_clock_t;

  279. struct compat_timeval
  280. {
  281.   compat_time_t tv_sec;
  282.   int tv_usec;
  283. };

  284. typedef union compat_sigval
  285. {
  286.   compat_int_t sival_int;
  287.   compat_uptr_t sival_ptr;
  288. } compat_sigval_t;

  289. typedef struct compat_siginfo
  290. {
  291.   int si_signo;
  292.   int si_errno;
  293.   int si_code;

  294.   union
  295.   {
  296.     int _pad[((128 / sizeof (int)) - 3)];

  297.     /* kill() */
  298.     struct
  299.     {
  300.       unsigned int _pid;
  301.       unsigned int _uid;
  302.     } _kill;

  303.     /* POSIX.1b timers */
  304.     struct
  305.     {
  306.       compat_timer_t _tid;
  307.       int _overrun;
  308.       compat_sigval_t _sigval;
  309.     } _timer;

  310.     /* POSIX.1b signals */
  311.     struct
  312.     {
  313.       unsigned int _pid;
  314.       unsigned int _uid;
  315.       compat_sigval_t _sigval;
  316.     } _rt;

  317.     /* SIGCHLD */
  318.     struct
  319.     {
  320.       unsigned int _pid;
  321.       unsigned int _uid;
  322.       int _status;
  323.       compat_clock_t _utime;
  324.       compat_clock_t _stime;
  325.     } _sigchld;

  326.     /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
  327.     struct
  328.     {
  329.       unsigned int _addr;
  330.     } _sigfault;

  331.     /* SIGPOLL */
  332.     struct
  333.     {
  334.       int _band;
  335.       int _fd;
  336.     } _sigpoll;
  337.   } _sifields;
  338. } compat_siginfo_t;

  339. /* For x32, clock_t in _sigchld is 64bit aligned at 4 bytes.  */
  340. typedef struct compat_x32_clock
  341. {
  342.   int lower;
  343.   int upper;
  344. } compat_x32_clock_t;

  345. typedef struct compat_x32_siginfo
  346. {
  347.   int si_signo;
  348.   int si_errno;
  349.   int si_code;

  350.   union
  351.   {
  352.     int _pad[((128 / sizeof (int)) - 3)];

  353.     /* kill() */
  354.     struct
  355.     {
  356.       unsigned int _pid;
  357.       unsigned int _uid;
  358.     } _kill;

  359.     /* POSIX.1b timers */
  360.     struct
  361.     {
  362.       compat_timer_t _tid;
  363.       int _overrun;
  364.       compat_sigval_t _sigval;
  365.     } _timer;

  366.     /* POSIX.1b signals */
  367.     struct
  368.     {
  369.       unsigned int _pid;
  370.       unsigned int _uid;
  371.       compat_sigval_t _sigval;
  372.     } _rt;

  373.     /* SIGCHLD */
  374.     struct
  375.     {
  376.       unsigned int _pid;
  377.       unsigned int _uid;
  378.       int _status;
  379.       compat_x32_clock_t _utime;
  380.       compat_x32_clock_t _stime;
  381.     } _sigchld;

  382.     /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
  383.     struct
  384.     {
  385.       unsigned int _addr;
  386.     } _sigfault;

  387.     /* SIGPOLL */
  388.     struct
  389.     {
  390.       int _band;
  391.       int _fd;
  392.     } _sigpoll;
  393.   } _sifields;
  394. } compat_x32_siginfo_t;

  395. #define cpt_si_pid _sifields._kill._pid
  396. #define cpt_si_uid _sifields._kill._uid
  397. #define cpt_si_timerid _sifields._timer._tid
  398. #define cpt_si_overrun _sifields._timer._overrun
  399. #define cpt_si_status _sifields._sigchld._status
  400. #define cpt_si_utime _sifields._sigchld._utime
  401. #define cpt_si_stime _sifields._sigchld._stime
  402. #define cpt_si_ptr _sifields._rt._sigval.sival_ptr
  403. #define cpt_si_addr _sifields._sigfault._addr
  404. #define cpt_si_band _sifields._sigpoll._band
  405. #define cpt_si_fd _sifields._sigpoll._fd

  406. /* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun.
  407.    In their place is si_timer1,si_timer2.  */
  408. #ifndef si_timerid
  409. #define si_timerid si_timer1
  410. #endif
  411. #ifndef si_overrun
  412. #define si_overrun si_timer2
  413. #endif

  414. static void
  415. compat_siginfo_from_siginfo (compat_siginfo_t *to, siginfo_t *from)
  416. {
  417.   memset (to, 0, sizeof (*to));

  418.   to->si_signo = from->si_signo;
  419.   to->si_errno = from->si_errno;
  420.   to->si_code = from->si_code;

  421.   if (to->si_code == SI_TIMER)
  422.     {
  423.       to->cpt_si_timerid = from->si_timerid;
  424.       to->cpt_si_overrun = from->si_overrun;
  425.       to->cpt_si_ptr = (intptr_t) from->si_ptr;
  426.     }
  427.   else if (to->si_code == SI_USER)
  428.     {
  429.       to->cpt_si_pid = from->si_pid;
  430.       to->cpt_si_uid = from->si_uid;
  431.     }
  432.   else if (to->si_code < 0)
  433.     {
  434.       to->cpt_si_pid = from->si_pid;
  435.       to->cpt_si_uid = from->si_uid;
  436.       to->cpt_si_ptr = (intptr_t) from->si_ptr;
  437.     }
  438.   else
  439.     {
  440.       switch (to->si_signo)
  441.         {
  442.         case SIGCHLD:
  443.           to->cpt_si_pid = from->si_pid;
  444.           to->cpt_si_uid = from->si_uid;
  445.           to->cpt_si_status = from->si_status;
  446.           to->cpt_si_utime = from->si_utime;
  447.           to->cpt_si_stime = from->si_stime;
  448.           break;
  449.         case SIGILL:
  450.         case SIGFPE:
  451.         case SIGSEGV:
  452.         case SIGBUS:
  453.           to->cpt_si_addr = (intptr_t) from->si_addr;
  454.           break;
  455.         case SIGPOLL:
  456.           to->cpt_si_band = from->si_band;
  457.           to->cpt_si_fd = from->si_fd;
  458.           break;
  459.         default:
  460.           to->cpt_si_pid = from->si_pid;
  461.           to->cpt_si_uid = from->si_uid;
  462.           to->cpt_si_ptr = (intptr_t) from->si_ptr;
  463.           break;
  464.         }
  465.     }
  466. }

  467. static void
  468. siginfo_from_compat_siginfo (siginfo_t *to, compat_siginfo_t *from)
  469. {
  470.   memset (to, 0, sizeof (*to));

  471.   to->si_signo = from->si_signo;
  472.   to->si_errno = from->si_errno;
  473.   to->si_code = from->si_code;

  474.   if (to->si_code == SI_TIMER)
  475.     {
  476.       to->si_timerid = from->cpt_si_timerid;
  477.       to->si_overrun = from->cpt_si_overrun;
  478.       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
  479.     }
  480.   else if (to->si_code == SI_USER)
  481.     {
  482.       to->si_pid = from->cpt_si_pid;
  483.       to->si_uid = from->cpt_si_uid;
  484.     }
  485.   if (to->si_code < 0)
  486.     {
  487.       to->si_pid = from->cpt_si_pid;
  488.       to->si_uid = from->cpt_si_uid;
  489.       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
  490.     }
  491.   else
  492.     {
  493.       switch (to->si_signo)
  494.         {
  495.         case SIGCHLD:
  496.           to->si_pid = from->cpt_si_pid;
  497.           to->si_uid = from->cpt_si_uid;
  498.           to->si_status = from->cpt_si_status;
  499.           to->si_utime = from->cpt_si_utime;
  500.           to->si_stime = from->cpt_si_stime;
  501.           break;
  502.         case SIGILL:
  503.         case SIGFPE:
  504.         case SIGSEGV:
  505.         case SIGBUS:
  506.           to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
  507.           break;
  508.         case SIGPOLL:
  509.           to->si_band = from->cpt_si_band;
  510.           to->si_fd = from->cpt_si_fd;
  511.           break;
  512.         default:
  513.           to->si_pid = from->cpt_si_pid;
  514.           to->si_uid = from->cpt_si_uid;
  515.           to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
  516.           break;
  517.         }
  518.     }
  519. }

  520. static void
  521. compat_x32_siginfo_from_siginfo (compat_x32_siginfo_t *to,
  522.                                  siginfo_t *from)
  523. {
  524.   memset (to, 0, sizeof (*to));

  525.   to->si_signo = from->si_signo;
  526.   to->si_errno = from->si_errno;
  527.   to->si_code = from->si_code;

  528.   if (to->si_code == SI_TIMER)
  529.     {
  530.       to->cpt_si_timerid = from->si_timerid;
  531.       to->cpt_si_overrun = from->si_overrun;
  532.       to->cpt_si_ptr = (intptr_t) from->si_ptr;
  533.     }
  534.   else if (to->si_code == SI_USER)
  535.     {
  536.       to->cpt_si_pid = from->si_pid;
  537.       to->cpt_si_uid = from->si_uid;
  538.     }
  539.   else if (to->si_code < 0)
  540.     {
  541.       to->cpt_si_pid = from->si_pid;
  542.       to->cpt_si_uid = from->si_uid;
  543.       to->cpt_si_ptr = (intptr_t) from->si_ptr;
  544.     }
  545.   else
  546.     {
  547.       switch (to->si_signo)
  548.         {
  549.         case SIGCHLD:
  550.           to->cpt_si_pid = from->si_pid;
  551.           to->cpt_si_uid = from->si_uid;
  552.           to->cpt_si_status = from->si_status;
  553.           memcpy (&to->cpt_si_utime, &from->si_utime,
  554.                   sizeof (to->cpt_si_utime));
  555.           memcpy (&to->cpt_si_stime, &from->si_stime,
  556.                   sizeof (to->cpt_si_stime));
  557.           break;
  558.         case SIGILL:
  559.         case SIGFPE:
  560.         case SIGSEGV:
  561.         case SIGBUS:
  562.           to->cpt_si_addr = (intptr_t) from->si_addr;
  563.           break;
  564.         case SIGPOLL:
  565.           to->cpt_si_band = from->si_band;
  566.           to->cpt_si_fd = from->si_fd;
  567.           break;
  568.         default:
  569.           to->cpt_si_pid = from->si_pid;
  570.           to->cpt_si_uid = from->si_uid;
  571.           to->cpt_si_ptr = (intptr_t) from->si_ptr;
  572.           break;
  573.         }
  574.     }
  575. }

  576. static void
  577. siginfo_from_compat_x32_siginfo (siginfo_t *to,
  578.                                  compat_x32_siginfo_t *from)
  579. {
  580.   memset (to, 0, sizeof (*to));

  581.   to->si_signo = from->si_signo;
  582.   to->si_errno = from->si_errno;
  583.   to->si_code = from->si_code;

  584.   if (to->si_code == SI_TIMER)
  585.     {
  586.       to->si_timerid = from->cpt_si_timerid;
  587.       to->si_overrun = from->cpt_si_overrun;
  588.       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
  589.     }
  590.   else if (to->si_code == SI_USER)
  591.     {
  592.       to->si_pid = from->cpt_si_pid;
  593.       to->si_uid = from->cpt_si_uid;
  594.     }
  595.   if (to->si_code < 0)
  596.     {
  597.       to->si_pid = from->cpt_si_pid;
  598.       to->si_uid = from->cpt_si_uid;
  599.       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
  600.     }
  601.   else
  602.     {
  603.       switch (to->si_signo)
  604.         {
  605.         case SIGCHLD:
  606.           to->si_pid = from->cpt_si_pid;
  607.           to->si_uid = from->cpt_si_uid;
  608.           to->si_status = from->cpt_si_status;
  609.           memcpy (&to->si_utime, &from->cpt_si_utime,
  610.                   sizeof (to->si_utime));
  611.           memcpy (&to->si_stime, &from->cpt_si_stime,
  612.                   sizeof (to->si_stime));
  613.           break;
  614.         case SIGILL:
  615.         case SIGFPE:
  616.         case SIGSEGV:
  617.         case SIGBUS:
  618.           to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
  619.           break;
  620.         case SIGPOLL:
  621.           to->si_band = from->cpt_si_band;
  622.           to->si_fd = from->cpt_si_fd;
  623.           break;
  624.         default:
  625.           to->si_pid = from->cpt_si_pid;
  626.           to->si_uid = from->cpt_si_uid;
  627.           to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
  628.           break;
  629.         }
  630.     }
  631. }

  632. /* Convert a native/host siginfo object, into/from the siginfo in the
  633.    layout of the inferiors' architecture.  Returns true if any
  634.    conversion was done; false otherwise.  If DIRECTION is 1, then copy
  635.    from INF to NATIVE.  If DIRECTION is 0, copy from NATIVE to
  636.    INF.  */

  637. static int
  638. amd64_linux_siginfo_fixup (siginfo_t *native, gdb_byte *inf, int direction)
  639. {
  640.   struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());

  641.   /* Is the inferior 32-bit?  If so, then do fixup the siginfo
  642.      object.  */
  643.   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  644.     {
  645.       gdb_assert (sizeof (siginfo_t) == sizeof (compat_siginfo_t));

  646.       if (direction == 0)
  647.         compat_siginfo_from_siginfo ((struct compat_siginfo *) inf, native);
  648.       else
  649.         siginfo_from_compat_siginfo (native, (struct compat_siginfo *) inf);

  650.       return 1;
  651.     }
  652.   /* No fixup for native x32 GDB.  */
  653.   else if (gdbarch_addr_bit (gdbarch) == 32 && sizeof (void *) == 8)
  654.     {
  655.       gdb_assert (sizeof (siginfo_t) == sizeof (compat_x32_siginfo_t));

  656.       if (direction == 0)
  657.         compat_x32_siginfo_from_siginfo ((struct compat_x32_siginfo *) inf,
  658.                                          native);
  659.       else
  660.         siginfo_from_compat_x32_siginfo (native,
  661.                                          (struct compat_x32_siginfo *) inf);

  662.       return 1;
  663.     }
  664.   else
  665.     return 0;
  666. }


  667. /* Provide a prototype to silence -Wmissing-prototypes.  */
  668. void _initialize_amd64_linux_nat (void);

  669. void
  670. _initialize_amd64_linux_nat (void)
  671. {
  672.   struct target_ops *t;

  673.   amd64_native_gregset32_reg_offset = amd64_linux_gregset32_reg_offset;
  674.   amd64_native_gregset32_num_regs = I386_LINUX_NUM_REGS;
  675.   amd64_native_gregset64_reg_offset = amd64_linux_gregset_reg_offset;
  676.   amd64_native_gregset64_num_regs = AMD64_LINUX_NUM_REGS;

  677.   gdb_assert (ARRAY_SIZE (amd64_linux_gregset32_reg_offset)
  678.               == amd64_native_gregset32_num_regs);

  679.   /* Create a generic x86 GNU/Linux target.  */
  680.   t = x86_linux_create_target ();

  681.   /* Add our register access methods.  */
  682.   t->to_fetch_registers = amd64_linux_fetch_inferior_registers;
  683.   t->to_store_registers = amd64_linux_store_inferior_registers;

  684.   /* Add the target.  */
  685.   x86_linux_add_target (t);

  686.   /* Add our siginfo layout converter.  */
  687.   linux_nat_set_siginfo_fixup (t, amd64_linux_siginfo_fixup);
  688. }