GDB (API)
/home/stan/gdb/src/gdb/i386-linux-tdep.c
Go to the documentation of this file.
00001 /* Target-dependent code for GNU/Linux i386.
00002 
00003    Copyright (C) 2000-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "gdbcore.h"
00022 #include "frame.h"
00023 #include "value.h"
00024 #include "regcache.h"
00025 #include "regset.h"
00026 #include "inferior.h"
00027 #include "osabi.h"
00028 #include "reggroups.h"
00029 #include "dwarf2-frame.h"
00030 #include "gdb_string.h"
00031 
00032 #include "i386-tdep.h"
00033 #include "i386-linux-tdep.h"
00034 #include "linux-tdep.h"
00035 #include "glibc-tdep.h"
00036 #include "solib-svr4.h"
00037 #include "symtab.h"
00038 #include "arch-utils.h"
00039 #include "xml-syscall.h"
00040 
00041 #include "i387-tdep.h"
00042 #include "i386-xstate.h"
00043 
00044 /* The syscall's XML filename for i386.  */
00045 #define XML_SYSCALL_FILENAME_I386 "syscalls/i386-linux.xml"
00046 
00047 #include "record-full.h"
00048 #include "linux-record.h"
00049 #include <stdint.h>
00050 
00051 #include "features/i386/i386-linux.c"
00052 #include "features/i386/i386-mmx-linux.c"
00053 #include "features/i386/i386-avx-linux.c"
00054 
00055 /* Supported register note sections.  */
00056 static struct core_regset_section i386_linux_regset_sections[] =
00057 {
00058   { ".reg", 68, "general-purpose" },
00059   { ".reg2", 108, "floating-point" },
00060   { NULL, 0 }
00061 };
00062 
00063 static struct core_regset_section i386_linux_sse_regset_sections[] =
00064 {
00065   { ".reg", 68, "general-purpose" },
00066   { ".reg-xfp", 512, "extended floating-point" },
00067   { NULL, 0 }
00068 };
00069 
00070 static struct core_regset_section i386_linux_avx_regset_sections[] =
00071 {
00072   { ".reg", 68, "general-purpose" },
00073   { ".reg-xstate", I386_XSTATE_MAX_SIZE, "XSAVE extended state" },
00074   { NULL, 0 }
00075 };
00076 
00077 /* Return non-zero, when the register is in the corresponding register
00078    group.  Put the LINUX_ORIG_EAX register in the system group.  */
00079 static int
00080 i386_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
00081                                 struct reggroup *group)
00082 {
00083   if (regnum == I386_LINUX_ORIG_EAX_REGNUM)
00084     return (group == system_reggroup
00085             || group == save_reggroup
00086             || group == restore_reggroup);
00087   return i386_register_reggroup_p (gdbarch, regnum, group);
00088 }
00089 
00090 
00091 /* Recognizing signal handler frames.  */
00092 
00093 /* GNU/Linux has two flavors of signals.  Normal signal handlers, and
00094    "realtime" (RT) signals.  The RT signals can provide additional
00095    information to the signal handler if the SA_SIGINFO flag is set
00096    when establishing a signal handler using `sigaction'.  It is not
00097    unlikely that future versions of GNU/Linux will support SA_SIGINFO
00098    for normal signals too.  */
00099 
00100 /* When the i386 Linux kernel calls a signal handler and the
00101    SA_RESTORER flag isn't set, the return address points to a bit of
00102    code on the stack.  This function returns whether the PC appears to
00103    be within this bit of code.
00104 
00105    The instruction sequence for normal signals is
00106        pop    %eax
00107        mov    $0x77, %eax
00108        int    $0x80
00109    or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
00110 
00111    Checking for the code sequence should be somewhat reliable, because
00112    the effect is to call the system call sigreturn.  This is unlikely
00113    to occur anywhere other than in a signal trampoline.
00114 
00115    It kind of sucks that we have to read memory from the process in
00116    order to identify a signal trampoline, but there doesn't seem to be
00117    any other way.  Therefore we only do the memory reads if no
00118    function name could be identified, which should be the case since
00119    the code is on the stack.
00120 
00121    Detection of signal trampolines for handlers that set the
00122    SA_RESTORER flag is in general not possible.  Unfortunately this is
00123    what the GNU C Library has been doing for quite some time now.
00124    However, as of version 2.1.2, the GNU C Library uses signal
00125    trampolines (named __restore and __restore_rt) that are identical
00126    to the ones used by the kernel.  Therefore, these trampolines are
00127    supported too.  */
00128 
00129 #define LINUX_SIGTRAMP_INSN0    0x58    /* pop %eax */
00130 #define LINUX_SIGTRAMP_OFFSET0  0
00131 #define LINUX_SIGTRAMP_INSN1    0xb8    /* mov $NNNN, %eax */
00132 #define LINUX_SIGTRAMP_OFFSET1  1
00133 #define LINUX_SIGTRAMP_INSN2    0xcd    /* int */
00134 #define LINUX_SIGTRAMP_OFFSET2  6
00135 
00136 static const gdb_byte linux_sigtramp_code[] =
00137 {
00138   LINUX_SIGTRAMP_INSN0,                                 /* pop %eax */
00139   LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00,         /* mov $0x77, %eax */
00140   LINUX_SIGTRAMP_INSN2, 0x80                            /* int $0x80 */
00141 };
00142 
00143 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
00144 
00145 /* If THIS_FRAME is a sigtramp routine, return the address of the
00146    start of the routine.  Otherwise, return 0.  */
00147 
00148 static CORE_ADDR
00149 i386_linux_sigtramp_start (struct frame_info *this_frame)
00150 {
00151   CORE_ADDR pc = get_frame_pc (this_frame);
00152   gdb_byte buf[LINUX_SIGTRAMP_LEN];
00153 
00154   /* We only recognize a signal trampoline if PC is at the start of
00155      one of the three instructions.  We optimize for finding the PC at
00156      the start, as will be the case when the trampoline is not the
00157      first frame on the stack.  We assume that in the case where the
00158      PC is not at the start of the instruction sequence, there will be
00159      a few trailing readable bytes on the stack.  */
00160 
00161   if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
00162     return 0;
00163 
00164   if (buf[0] != LINUX_SIGTRAMP_INSN0)
00165     {
00166       int adjust;
00167 
00168       switch (buf[0])
00169         {
00170         case LINUX_SIGTRAMP_INSN1:
00171           adjust = LINUX_SIGTRAMP_OFFSET1;
00172           break;
00173         case LINUX_SIGTRAMP_INSN2:
00174           adjust = LINUX_SIGTRAMP_OFFSET2;
00175           break;
00176         default:
00177           return 0;
00178         }
00179 
00180       pc -= adjust;
00181 
00182       if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
00183         return 0;
00184     }
00185 
00186   if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
00187     return 0;
00188 
00189   return pc;
00190 }
00191 
00192 /* This function does the same for RT signals.  Here the instruction
00193    sequence is
00194        mov    $0xad, %eax
00195        int    $0x80
00196    or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
00197 
00198    The effect is to call the system call rt_sigreturn.  */
00199 
00200 #define LINUX_RT_SIGTRAMP_INSN0         0xb8 /* mov $NNNN, %eax */
00201 #define LINUX_RT_SIGTRAMP_OFFSET0       0
00202 #define LINUX_RT_SIGTRAMP_INSN1         0xcd /* int */
00203 #define LINUX_RT_SIGTRAMP_OFFSET1       5
00204 
00205 static const gdb_byte linux_rt_sigtramp_code[] =
00206 {
00207   LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00,      /* mov $0xad, %eax */
00208   LINUX_RT_SIGTRAMP_INSN1, 0x80                         /* int $0x80 */
00209 };
00210 
00211 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
00212 
00213 /* If THIS_FRAME is an RT sigtramp routine, return the address of the
00214    start of the routine.  Otherwise, return 0.  */
00215 
00216 static CORE_ADDR
00217 i386_linux_rt_sigtramp_start (struct frame_info *this_frame)
00218 {
00219   CORE_ADDR pc = get_frame_pc (this_frame);
00220   gdb_byte buf[LINUX_RT_SIGTRAMP_LEN];
00221 
00222   /* We only recognize a signal trampoline if PC is at the start of
00223      one of the two instructions.  We optimize for finding the PC at
00224      the start, as will be the case when the trampoline is not the
00225      first frame on the stack.  We assume that in the case where the
00226      PC is not at the start of the instruction sequence, there will be
00227      a few trailing readable bytes on the stack.  */
00228 
00229   if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_RT_SIGTRAMP_LEN))
00230     return 0;
00231 
00232   if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
00233     {
00234       if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
00235         return 0;
00236 
00237       pc -= LINUX_RT_SIGTRAMP_OFFSET1;
00238 
00239       if (!safe_frame_unwind_memory (this_frame, pc, buf,
00240                                      LINUX_RT_SIGTRAMP_LEN))
00241         return 0;
00242     }
00243 
00244   if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
00245     return 0;
00246 
00247   return pc;
00248 }
00249 
00250 /* Return whether THIS_FRAME corresponds to a GNU/Linux sigtramp
00251    routine.  */
00252 
00253 static int
00254 i386_linux_sigtramp_p (struct frame_info *this_frame)
00255 {
00256   CORE_ADDR pc = get_frame_pc (this_frame);
00257   const char *name;
00258 
00259   find_pc_partial_function (pc, &name, NULL, NULL);
00260 
00261   /* If we have NAME, we can optimize the search.  The trampolines are
00262      named __restore and __restore_rt.  However, they aren't dynamically
00263      exported from the shared C library, so the trampoline may appear to
00264      be part of the preceding function.  This should always be sigaction,
00265      __sigaction, or __libc_sigaction (all aliases to the same function).  */
00266   if (name == NULL || strstr (name, "sigaction") != NULL)
00267     return (i386_linux_sigtramp_start (this_frame) != 0
00268             || i386_linux_rt_sigtramp_start (this_frame) != 0);
00269 
00270   return (strcmp ("__restore", name) == 0
00271           || strcmp ("__restore_rt", name) == 0);
00272 }
00273 
00274 /* Return one if the PC of THIS_FRAME is in a signal trampoline which
00275    may have DWARF-2 CFI.  */
00276 
00277 static int
00278 i386_linux_dwarf_signal_frame_p (struct gdbarch *gdbarch,
00279                                  struct frame_info *this_frame)
00280 {
00281   CORE_ADDR pc = get_frame_pc (this_frame);
00282   const char *name;
00283 
00284   find_pc_partial_function (pc, &name, NULL, NULL);
00285 
00286   /* If a vsyscall DSO is in use, the signal trampolines may have these
00287      names.  */
00288   if (name && (strcmp (name, "__kernel_sigreturn") == 0
00289                || strcmp (name, "__kernel_rt_sigreturn") == 0))
00290     return 1;
00291 
00292   return 0;
00293 }
00294 
00295 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>.  */
00296 #define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
00297 
00298 /* Assuming THIS_FRAME is a GNU/Linux sigtramp routine, return the
00299    address of the associated sigcontext structure.  */
00300 
00301 static CORE_ADDR
00302 i386_linux_sigcontext_addr (struct frame_info *this_frame)
00303 {
00304   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00305   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00306   CORE_ADDR pc;
00307   CORE_ADDR sp;
00308   gdb_byte buf[4];
00309 
00310   get_frame_register (this_frame, I386_ESP_REGNUM, buf);
00311   sp = extract_unsigned_integer (buf, 4, byte_order);
00312 
00313   pc = i386_linux_sigtramp_start (this_frame);
00314   if (pc)
00315     {
00316       /* The sigcontext structure lives on the stack, right after
00317          the signum argument.  We determine the address of the
00318          sigcontext structure by looking at the frame's stack
00319          pointer.  Keep in mind that the first instruction of the
00320          sigtramp code is "pop %eax".  If the PC is after this
00321          instruction, adjust the returned value accordingly.  */
00322       if (pc == get_frame_pc (this_frame))
00323         return sp + 4;
00324       return sp;
00325     }
00326 
00327   pc = i386_linux_rt_sigtramp_start (this_frame);
00328   if (pc)
00329     {
00330       CORE_ADDR ucontext_addr;
00331 
00332       /* The sigcontext structure is part of the user context.  A
00333          pointer to the user context is passed as the third argument
00334          to the signal handler.  */
00335       read_memory (sp + 8, buf, 4);
00336       ucontext_addr = extract_unsigned_integer (buf, 4, byte_order);
00337       return ucontext_addr + I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
00338     }
00339 
00340   error (_("Couldn't recognize signal trampoline."));
00341   return 0;
00342 }
00343 
00344 /* Set the program counter for process PTID to PC.  */
00345 
00346 static void
00347 i386_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
00348 {
00349   regcache_cooked_write_unsigned (regcache, I386_EIP_REGNUM, pc);
00350 
00351   /* We must be careful with modifying the program counter.  If we
00352      just interrupted a system call, the kernel might try to restart
00353      it when we resume the inferior.  On restarting the system call,
00354      the kernel will try backing up the program counter even though it
00355      no longer points at the system call.  This typically results in a
00356      SIGSEGV or SIGILL.  We can prevent this by writing `-1' in the
00357      "orig_eax" pseudo-register.
00358 
00359      Note that "orig_eax" is saved when setting up a dummy call frame.
00360      This means that it is properly restored when that frame is
00361      popped, and that the interrupted system call will be restarted
00362      when we resume the inferior on return from a function call from
00363      within GDB.  In all other cases the system call will not be
00364      restarted.  */
00365   regcache_cooked_write_unsigned (regcache, I386_LINUX_ORIG_EAX_REGNUM, -1);
00366 }
00367 
00368 /* Record all registers but IP register for process-record.  */
00369 
00370 static int
00371 i386_all_but_ip_registers_record (struct regcache *regcache)
00372 {
00373   if (record_full_arch_list_add_reg (regcache, I386_EAX_REGNUM))
00374     return -1;
00375   if (record_full_arch_list_add_reg (regcache, I386_ECX_REGNUM))
00376     return -1;
00377   if (record_full_arch_list_add_reg (regcache, I386_EDX_REGNUM))
00378     return -1;
00379   if (record_full_arch_list_add_reg (regcache, I386_EBX_REGNUM))
00380     return -1;
00381   if (record_full_arch_list_add_reg (regcache, I386_ESP_REGNUM))
00382     return -1;
00383   if (record_full_arch_list_add_reg (regcache, I386_EBP_REGNUM))
00384     return -1;
00385   if (record_full_arch_list_add_reg (regcache, I386_ESI_REGNUM))
00386     return -1;
00387   if (record_full_arch_list_add_reg (regcache, I386_EDI_REGNUM))
00388     return -1;
00389   if (record_full_arch_list_add_reg (regcache, I386_EFLAGS_REGNUM))
00390     return -1;
00391 
00392   return 0;
00393 }
00394 
00395 /* i386_canonicalize_syscall maps from the native i386 Linux set
00396    of syscall ids into a canonical set of syscall ids used by
00397    process record (a mostly trivial mapping, since the canonical
00398    set was originally taken from the i386 set).  */
00399 
00400 static enum gdb_syscall
00401 i386_canonicalize_syscall (int syscall)
00402 {
00403   enum { i386_syscall_max = 499 };
00404 
00405   if (syscall <= i386_syscall_max)
00406     return syscall;
00407   else
00408     return -1;
00409 }
00410 
00411 /* Parse the arguments of current system call instruction and record
00412    the values of the registers and memory that will be changed into
00413    "record_arch_list".  This instruction is "int 0x80" (Linux
00414    Kernel2.4) or "sysenter" (Linux Kernel 2.6).
00415 
00416    Return -1 if something wrong.  */
00417 
00418 static struct linux_record_tdep i386_linux_record_tdep;
00419 
00420 static int
00421 i386_linux_intx80_sysenter_syscall_record (struct regcache *regcache)
00422 {
00423   int ret;
00424   LONGEST syscall_native;
00425   enum gdb_syscall syscall_gdb;
00426 
00427   regcache_raw_read_signed (regcache, I386_EAX_REGNUM, &syscall_native);
00428 
00429   syscall_gdb = i386_canonicalize_syscall (syscall_native);
00430 
00431   if (syscall_gdb < 0)
00432     {
00433       printf_unfiltered (_("Process record and replay target doesn't "
00434                            "support syscall number %s\n"), 
00435                          plongest (syscall_native));
00436       return -1;
00437     }
00438 
00439   if (syscall_gdb == gdb_sys_sigreturn
00440       || syscall_gdb == gdb_sys_rt_sigreturn)
00441    {
00442      if (i386_all_but_ip_registers_record (regcache))
00443        return -1;
00444      return 0;
00445    }
00446 
00447   ret = record_linux_system_call (syscall_gdb, regcache,
00448                                   &i386_linux_record_tdep);
00449   if (ret)
00450     return ret;
00451 
00452   /* Record the return value of the system call.  */
00453   if (record_full_arch_list_add_reg (regcache, I386_EAX_REGNUM))
00454     return -1;
00455 
00456   return 0;
00457 }
00458 
00459 #define I386_LINUX_xstate       270
00460 #define I386_LINUX_frame_size   732
00461 
00462 static int
00463 i386_linux_record_signal (struct gdbarch *gdbarch,
00464                           struct regcache *regcache,
00465                           enum gdb_signal signal)
00466 {
00467   ULONGEST esp;
00468 
00469   if (i386_all_but_ip_registers_record (regcache))
00470     return -1;
00471 
00472   if (record_full_arch_list_add_reg (regcache, I386_EIP_REGNUM))
00473     return -1;
00474 
00475   /* Record the change in the stack.  */
00476   regcache_raw_read_unsigned (regcache, I386_ESP_REGNUM, &esp);
00477   /* This is for xstate.
00478      sp -= sizeof (struct _fpstate);  */
00479   esp -= I386_LINUX_xstate;
00480   /* This is for frame_size.
00481      sp -= sizeof (struct rt_sigframe);  */
00482   esp -= I386_LINUX_frame_size;
00483   if (record_full_arch_list_add_mem (esp,
00484                                      I386_LINUX_xstate + I386_LINUX_frame_size))
00485     return -1;
00486 
00487   if (record_full_arch_list_add_end ())
00488     return -1;
00489 
00490   return 0;
00491 }
00492 
00493 
00494 /* Core of the implementation for gdbarch get_syscall_number.  Get pending
00495    syscall number from REGCACHE.  If there is no pending syscall -1 will be
00496    returned.  Pending syscall means ptrace has stepped into the syscall but
00497    another ptrace call will step out.  PC is right after the int $0x80
00498    / syscall / sysenter instruction in both cases, PC does not change during
00499    the second ptrace step.  */
00500 
00501 static LONGEST
00502 i386_linux_get_syscall_number_from_regcache (struct regcache *regcache)
00503 {
00504   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00505   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00506   /* The content of a register.  */
00507   gdb_byte buf[4];
00508   /* The result.  */
00509   LONGEST ret;
00510 
00511   /* Getting the system call number from the register.
00512      When dealing with x86 architecture, this information
00513      is stored at %eax register.  */
00514   regcache_cooked_read (regcache, I386_LINUX_ORIG_EAX_REGNUM, buf);
00515 
00516   ret = extract_signed_integer (buf, 4, byte_order);
00517 
00518   return ret;
00519 }
00520 
00521 /* Wrapper for i386_linux_get_syscall_number_from_regcache to make it
00522    compatible with gdbarch get_syscall_number method prototype.  */
00523 
00524 static LONGEST
00525 i386_linux_get_syscall_number (struct gdbarch *gdbarch,
00526                                ptid_t ptid)
00527 {
00528   struct regcache *regcache = get_thread_regcache (ptid);
00529 
00530   return i386_linux_get_syscall_number_from_regcache (regcache);
00531 }
00532 
00533 /* The register sets used in GNU/Linux ELF core-dumps are identical to
00534    the register sets in `struct user' that are used for a.out
00535    core-dumps.  These are also used by ptrace(2).  The corresponding
00536    types are `elf_gregset_t' for the general-purpose registers (with
00537    `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
00538    for the floating-point registers.
00539 
00540    Those types used to be available under the names `gregset_t' and
00541    `fpregset_t' too, and GDB used those names in the past.  But those
00542    names are now used for the register sets used in the `mcontext_t'
00543    type, which have a different size and layout.  */
00544 
00545 /* Mapping between the general-purpose registers in `struct user'
00546    format and GDB's register cache layout.  */
00547 
00548 /* From <sys/reg.h>.  */
00549 int i386_linux_gregset_reg_offset[] =
00550 {
00551   6 * 4,                        /* %eax */
00552   1 * 4,                        /* %ecx */
00553   2 * 4,                        /* %edx */
00554   0 * 4,                        /* %ebx */
00555   15 * 4,                       /* %esp */
00556   5 * 4,                        /* %ebp */
00557   3 * 4,                        /* %esi */
00558   4 * 4,                        /* %edi */
00559   12 * 4,                       /* %eip */
00560   14 * 4,                       /* %eflags */
00561   13 * 4,                       /* %cs */
00562   16 * 4,                       /* %ss */
00563   7 * 4,                        /* %ds */
00564   8 * 4,                        /* %es */
00565   9 * 4,                        /* %fs */
00566   10 * 4,                       /* %gs */
00567   -1, -1, -1, -1, -1, -1, -1, -1,
00568   -1, -1, -1, -1, -1, -1, -1, -1,
00569   -1, -1, -1, -1, -1, -1, -1, -1,
00570   -1,
00571   -1, -1, -1, -1, -1, -1, -1, -1,
00572   11 * 4                        /* "orig_eax" */
00573 };
00574 
00575 /* Mapping between the general-purpose registers in `struct
00576    sigcontext' format and GDB's register cache layout.  */
00577 
00578 /* From <asm/sigcontext.h>.  */
00579 static int i386_linux_sc_reg_offset[] =
00580 {
00581   11 * 4,                       /* %eax */
00582   10 * 4,                       /* %ecx */
00583   9 * 4,                        /* %edx */
00584   8 * 4,                        /* %ebx */
00585   7 * 4,                        /* %esp */
00586   6 * 4,                        /* %ebp */
00587   5 * 4,                        /* %esi */
00588   4 * 4,                        /* %edi */
00589   14 * 4,                       /* %eip */
00590   16 * 4,                       /* %eflags */
00591   15 * 4,                       /* %cs */
00592   18 * 4,                       /* %ss */
00593   3 * 4,                        /* %ds */
00594   2 * 4,                        /* %es */
00595   1 * 4,                        /* %fs */
00596   0 * 4                         /* %gs */
00597 };
00598 
00599 /* Get XSAVE extended state xcr0 from core dump.  */
00600 
00601 uint64_t
00602 i386_linux_core_read_xcr0 (bfd *abfd)
00603 {
00604   asection *xstate = bfd_get_section_by_name (abfd, ".reg-xstate");
00605   uint64_t xcr0;
00606 
00607   if (xstate)
00608     {
00609       size_t size = bfd_section_size (abfd, xstate);
00610 
00611       /* Check extended state size.  */
00612       if (size < I386_XSTATE_AVX_SIZE)
00613         xcr0 = I386_XSTATE_SSE_MASK;
00614       else
00615         {
00616           char contents[8];
00617 
00618           if (! bfd_get_section_contents (abfd, xstate, contents,
00619                                           I386_LINUX_XSAVE_XCR0_OFFSET,
00620                                           8))
00621             {
00622               warning (_("Couldn't read `xcr0' bytes from "
00623                          "`.reg-xstate' section in core file."));
00624               return 0;
00625             }
00626 
00627           xcr0 = bfd_get_64 (abfd, contents);
00628         }
00629     }
00630   else
00631     xcr0 = 0;
00632 
00633   return xcr0;
00634 }
00635 
00636 /* Get Linux/x86 target description from core dump.  */
00637 
00638 static const struct target_desc *
00639 i386_linux_core_read_description (struct gdbarch *gdbarch,
00640                                   struct target_ops *target,
00641                                   bfd *abfd)
00642 {
00643   /* Linux/i386.  */
00644   uint64_t xcr0 = i386_linux_core_read_xcr0 (abfd);
00645   switch ((xcr0 & I386_XSTATE_AVX_MASK))
00646     {
00647     case I386_XSTATE_AVX_MASK:
00648       return tdesc_i386_avx_linux;
00649     case I386_XSTATE_SSE_MASK:
00650       return tdesc_i386_linux;
00651     case I386_XSTATE_X87_MASK:
00652       return tdesc_i386_mmx_linux;
00653     default:
00654       break;
00655     }
00656 
00657   if (bfd_get_section_by_name (abfd, ".reg-xfp") != NULL)
00658     return tdesc_i386_linux;
00659   else
00660     return tdesc_i386_mmx_linux;
00661 }
00662 
00663 /* Linux kernel shows PC value after the 'int $0x80' instruction even if
00664    inferior is still inside the syscall.  On next PTRACE_SINGLESTEP it will
00665    finish the syscall but PC will not change.
00666    
00667    Some vDSOs contain 'int $0x80; ret' and during stepping out of the syscall
00668    i386_displaced_step_fixup would keep PC at the displaced pad location.
00669    As PC is pointing to the 'ret' instruction before the step
00670    i386_displaced_step_fixup would expect inferior has just executed that 'ret'
00671    and PC should not be adjusted.  In reality it finished syscall instead and
00672    PC should get relocated back to its vDSO address.  Hide the 'ret'
00673    instruction by 'nop' so that i386_displaced_step_fixup is not confused.
00674    
00675    It is not fully correct as the bytes in struct displaced_step_closure will
00676    not match the inferior code.  But we would need some new flag in
00677    displaced_step_closure otherwise to keep the state that syscall is finishing
00678    for the later i386_displaced_step_fixup execution as the syscall execution
00679    is already no longer detectable there.  The new flag field would mean
00680    i386-linux-tdep.c needs to wrap all the displacement methods of i386-tdep.c
00681    which does not seem worth it.  The same effect is achieved by patching that
00682    'nop' instruction there instead.  */
00683 
00684 static struct displaced_step_closure *
00685 i386_linux_displaced_step_copy_insn (struct gdbarch *gdbarch,
00686                                      CORE_ADDR from, CORE_ADDR to,
00687                                      struct regcache *regs)
00688 {
00689   struct displaced_step_closure *closure;
00690   
00691   closure = i386_displaced_step_copy_insn (gdbarch, from, to, regs);
00692 
00693   if (i386_linux_get_syscall_number_from_regcache (regs) != -1)
00694     {
00695       /* Since we use simple_displaced_step_copy_insn, our closure is a
00696          copy of the instruction.  */
00697       gdb_byte *insn = (gdb_byte *) closure;
00698 
00699       /* Fake nop.  */
00700       insn[0] = 0x90;
00701     }
00702 
00703   return closure;
00704 }
00705 
00706 static void
00707 i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
00708 {
00709   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00710   const struct target_desc *tdesc = info.target_desc;
00711   struct tdesc_arch_data *tdesc_data = (void *) info.tdep_info;
00712   const struct tdesc_feature *feature;
00713   int valid_p;
00714 
00715   gdb_assert (tdesc_data);
00716 
00717   linux_init_abi (info, gdbarch);
00718 
00719   /* GNU/Linux uses ELF.  */
00720   i386_elf_init_abi (info, gdbarch);
00721 
00722   /* Reserve a number for orig_eax.  */
00723   set_gdbarch_num_regs (gdbarch, I386_LINUX_NUM_REGS);
00724 
00725   if (! tdesc_has_registers (tdesc))
00726     tdesc = tdesc_i386_linux;
00727   tdep->tdesc = tdesc;
00728 
00729   feature = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.linux");
00730   if (feature == NULL)
00731     return;
00732 
00733   valid_p = tdesc_numbered_register (feature, tdesc_data,
00734                                      I386_LINUX_ORIG_EAX_REGNUM,
00735                                      "orig_eax");
00736   if (!valid_p)
00737     return;
00738 
00739   /* Add the %orig_eax register used for syscall restarting.  */
00740   set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);
00741 
00742   tdep->register_reggroup_p = i386_linux_register_reggroup_p;
00743 
00744   tdep->gregset_reg_offset = i386_linux_gregset_reg_offset;
00745   tdep->gregset_num_regs = ARRAY_SIZE (i386_linux_gregset_reg_offset);
00746   tdep->sizeof_gregset = 17 * 4;
00747 
00748   tdep->jb_pc_offset = 20;      /* From <bits/setjmp.h>.  */
00749 
00750   tdep->sigtramp_p = i386_linux_sigtramp_p;
00751   tdep->sigcontext_addr = i386_linux_sigcontext_addr;
00752   tdep->sc_reg_offset = i386_linux_sc_reg_offset;
00753   tdep->sc_num_regs = ARRAY_SIZE (i386_linux_sc_reg_offset);
00754 
00755   tdep->xsave_xcr0_offset = I386_LINUX_XSAVE_XCR0_OFFSET;
00756 
00757   set_gdbarch_process_record (gdbarch, i386_process_record);
00758   set_gdbarch_process_record_signal (gdbarch, i386_linux_record_signal);
00759 
00760   /* Initialize the i386_linux_record_tdep.  */
00761   /* These values are the size of the type that will be used in a system
00762      call.  They are obtained from Linux Kernel source.  */
00763   i386_linux_record_tdep.size_pointer
00764     = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
00765   i386_linux_record_tdep.size__old_kernel_stat = 32;
00766   i386_linux_record_tdep.size_tms = 16;
00767   i386_linux_record_tdep.size_loff_t = 8;
00768   i386_linux_record_tdep.size_flock = 16;
00769   i386_linux_record_tdep.size_oldold_utsname = 45;
00770   i386_linux_record_tdep.size_ustat = 20;
00771   i386_linux_record_tdep.size_old_sigaction = 140;
00772   i386_linux_record_tdep.size_old_sigset_t = 128;
00773   i386_linux_record_tdep.size_rlimit = 8;
00774   i386_linux_record_tdep.size_rusage = 72;
00775   i386_linux_record_tdep.size_timeval = 8;
00776   i386_linux_record_tdep.size_timezone = 8;
00777   i386_linux_record_tdep.size_old_gid_t = 2;
00778   i386_linux_record_tdep.size_old_uid_t = 2;
00779   i386_linux_record_tdep.size_fd_set = 128;
00780   i386_linux_record_tdep.size_dirent = 268;
00781   i386_linux_record_tdep.size_dirent64 = 276;
00782   i386_linux_record_tdep.size_statfs = 64;
00783   i386_linux_record_tdep.size_statfs64 = 84;
00784   i386_linux_record_tdep.size_sockaddr = 16;
00785   i386_linux_record_tdep.size_int
00786     = gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT;
00787   i386_linux_record_tdep.size_long
00788     = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
00789   i386_linux_record_tdep.size_ulong
00790     = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
00791   i386_linux_record_tdep.size_msghdr = 28;
00792   i386_linux_record_tdep.size_itimerval = 16;
00793   i386_linux_record_tdep.size_stat = 88;
00794   i386_linux_record_tdep.size_old_utsname = 325;
00795   i386_linux_record_tdep.size_sysinfo = 64;
00796   i386_linux_record_tdep.size_msqid_ds = 88;
00797   i386_linux_record_tdep.size_shmid_ds = 84;
00798   i386_linux_record_tdep.size_new_utsname = 390;
00799   i386_linux_record_tdep.size_timex = 128;
00800   i386_linux_record_tdep.size_mem_dqinfo = 24;
00801   i386_linux_record_tdep.size_if_dqblk = 68;
00802   i386_linux_record_tdep.size_fs_quota_stat = 68;
00803   i386_linux_record_tdep.size_timespec = 8;
00804   i386_linux_record_tdep.size_pollfd = 8;
00805   i386_linux_record_tdep.size_NFS_FHSIZE = 32;
00806   i386_linux_record_tdep.size_knfsd_fh = 132;
00807   i386_linux_record_tdep.size_TASK_COMM_LEN = 16;
00808   i386_linux_record_tdep.size_sigaction = 140;
00809   i386_linux_record_tdep.size_sigset_t = 8;
00810   i386_linux_record_tdep.size_siginfo_t = 128;
00811   i386_linux_record_tdep.size_cap_user_data_t = 12;
00812   i386_linux_record_tdep.size_stack_t = 12;
00813   i386_linux_record_tdep.size_off_t = i386_linux_record_tdep.size_long;
00814   i386_linux_record_tdep.size_stat64 = 96;
00815   i386_linux_record_tdep.size_gid_t = 2;
00816   i386_linux_record_tdep.size_uid_t = 2;
00817   i386_linux_record_tdep.size_PAGE_SIZE = 4096;
00818   i386_linux_record_tdep.size_flock64 = 24;
00819   i386_linux_record_tdep.size_user_desc = 16;
00820   i386_linux_record_tdep.size_io_event = 32;
00821   i386_linux_record_tdep.size_iocb = 64;
00822   i386_linux_record_tdep.size_epoll_event = 12;
00823   i386_linux_record_tdep.size_itimerspec
00824     = i386_linux_record_tdep.size_timespec * 2;
00825   i386_linux_record_tdep.size_mq_attr = 32;
00826   i386_linux_record_tdep.size_siginfo = 128;
00827   i386_linux_record_tdep.size_termios = 36;
00828   i386_linux_record_tdep.size_termios2 = 44;
00829   i386_linux_record_tdep.size_pid_t = 4;
00830   i386_linux_record_tdep.size_winsize = 8;
00831   i386_linux_record_tdep.size_serial_struct = 60;
00832   i386_linux_record_tdep.size_serial_icounter_struct = 80;
00833   i386_linux_record_tdep.size_hayes_esp_config = 12;
00834   i386_linux_record_tdep.size_size_t = 4;
00835   i386_linux_record_tdep.size_iovec = 8;
00836 
00837   /* These values are the second argument of system call "sys_ioctl".
00838      They are obtained from Linux Kernel source.  */
00839   i386_linux_record_tdep.ioctl_TCGETS = 0x5401;
00840   i386_linux_record_tdep.ioctl_TCSETS = 0x5402;
00841   i386_linux_record_tdep.ioctl_TCSETSW = 0x5403;
00842   i386_linux_record_tdep.ioctl_TCSETSF = 0x5404;
00843   i386_linux_record_tdep.ioctl_TCGETA = 0x5405;
00844   i386_linux_record_tdep.ioctl_TCSETA = 0x5406;
00845   i386_linux_record_tdep.ioctl_TCSETAW = 0x5407;
00846   i386_linux_record_tdep.ioctl_TCSETAF = 0x5408;
00847   i386_linux_record_tdep.ioctl_TCSBRK = 0x5409;
00848   i386_linux_record_tdep.ioctl_TCXONC = 0x540A;
00849   i386_linux_record_tdep.ioctl_TCFLSH = 0x540B;
00850   i386_linux_record_tdep.ioctl_TIOCEXCL = 0x540C;
00851   i386_linux_record_tdep.ioctl_TIOCNXCL = 0x540D;
00852   i386_linux_record_tdep.ioctl_TIOCSCTTY = 0x540E;
00853   i386_linux_record_tdep.ioctl_TIOCGPGRP = 0x540F;
00854   i386_linux_record_tdep.ioctl_TIOCSPGRP = 0x5410;
00855   i386_linux_record_tdep.ioctl_TIOCOUTQ = 0x5411;
00856   i386_linux_record_tdep.ioctl_TIOCSTI = 0x5412;
00857   i386_linux_record_tdep.ioctl_TIOCGWINSZ = 0x5413;
00858   i386_linux_record_tdep.ioctl_TIOCSWINSZ = 0x5414;
00859   i386_linux_record_tdep.ioctl_TIOCMGET = 0x5415;
00860   i386_linux_record_tdep.ioctl_TIOCMBIS = 0x5416;
00861   i386_linux_record_tdep.ioctl_TIOCMBIC = 0x5417;
00862   i386_linux_record_tdep.ioctl_TIOCMSET = 0x5418;
00863   i386_linux_record_tdep.ioctl_TIOCGSOFTCAR = 0x5419;
00864   i386_linux_record_tdep.ioctl_TIOCSSOFTCAR = 0x541A;
00865   i386_linux_record_tdep.ioctl_FIONREAD = 0x541B;
00866   i386_linux_record_tdep.ioctl_TIOCINQ = i386_linux_record_tdep.ioctl_FIONREAD;
00867   i386_linux_record_tdep.ioctl_TIOCLINUX = 0x541C;
00868   i386_linux_record_tdep.ioctl_TIOCCONS = 0x541D;
00869   i386_linux_record_tdep.ioctl_TIOCGSERIAL = 0x541E;
00870   i386_linux_record_tdep.ioctl_TIOCSSERIAL = 0x541F;
00871   i386_linux_record_tdep.ioctl_TIOCPKT = 0x5420;
00872   i386_linux_record_tdep.ioctl_FIONBIO = 0x5421;
00873   i386_linux_record_tdep.ioctl_TIOCNOTTY = 0x5422;
00874   i386_linux_record_tdep.ioctl_TIOCSETD = 0x5423;
00875   i386_linux_record_tdep.ioctl_TIOCGETD = 0x5424;
00876   i386_linux_record_tdep.ioctl_TCSBRKP = 0x5425;
00877   i386_linux_record_tdep.ioctl_TIOCTTYGSTRUCT = 0x5426;
00878   i386_linux_record_tdep.ioctl_TIOCSBRK = 0x5427;
00879   i386_linux_record_tdep.ioctl_TIOCCBRK = 0x5428;
00880   i386_linux_record_tdep.ioctl_TIOCGSID = 0x5429;
00881   i386_linux_record_tdep.ioctl_TCGETS2 = 0x802c542a;
00882   i386_linux_record_tdep.ioctl_TCSETS2 = 0x402c542b;
00883   i386_linux_record_tdep.ioctl_TCSETSW2 = 0x402c542c;
00884   i386_linux_record_tdep.ioctl_TCSETSF2 = 0x402c542d;
00885   i386_linux_record_tdep.ioctl_TIOCGPTN = 0x80045430;
00886   i386_linux_record_tdep.ioctl_TIOCSPTLCK = 0x40045431;
00887   i386_linux_record_tdep.ioctl_FIONCLEX = 0x5450;
00888   i386_linux_record_tdep.ioctl_FIOCLEX = 0x5451;
00889   i386_linux_record_tdep.ioctl_FIOASYNC = 0x5452;
00890   i386_linux_record_tdep.ioctl_TIOCSERCONFIG = 0x5453;
00891   i386_linux_record_tdep.ioctl_TIOCSERGWILD = 0x5454;
00892   i386_linux_record_tdep.ioctl_TIOCSERSWILD = 0x5455;
00893   i386_linux_record_tdep.ioctl_TIOCGLCKTRMIOS = 0x5456;
00894   i386_linux_record_tdep.ioctl_TIOCSLCKTRMIOS = 0x5457;
00895   i386_linux_record_tdep.ioctl_TIOCSERGSTRUCT = 0x5458;
00896   i386_linux_record_tdep.ioctl_TIOCSERGETLSR = 0x5459;
00897   i386_linux_record_tdep.ioctl_TIOCSERGETMULTI = 0x545A;
00898   i386_linux_record_tdep.ioctl_TIOCSERSETMULTI = 0x545B;
00899   i386_linux_record_tdep.ioctl_TIOCMIWAIT = 0x545C;
00900   i386_linux_record_tdep.ioctl_TIOCGICOUNT = 0x545D;
00901   i386_linux_record_tdep.ioctl_TIOCGHAYESESP = 0x545E;
00902   i386_linux_record_tdep.ioctl_TIOCSHAYESESP = 0x545F;
00903   i386_linux_record_tdep.ioctl_FIOQSIZE = 0x5460;
00904 
00905   /* These values are the second argument of system call "sys_fcntl"
00906      and "sys_fcntl64".  They are obtained from Linux Kernel source.  */
00907   i386_linux_record_tdep.fcntl_F_GETLK = 5;
00908   i386_linux_record_tdep.fcntl_F_GETLK64 = 12;
00909   i386_linux_record_tdep.fcntl_F_SETLK64 = 13;
00910   i386_linux_record_tdep.fcntl_F_SETLKW64 = 14;
00911 
00912   i386_linux_record_tdep.arg1 = I386_EBX_REGNUM;
00913   i386_linux_record_tdep.arg2 = I386_ECX_REGNUM;
00914   i386_linux_record_tdep.arg3 = I386_EDX_REGNUM;
00915   i386_linux_record_tdep.arg4 = I386_ESI_REGNUM;
00916   i386_linux_record_tdep.arg5 = I386_EDI_REGNUM;
00917   i386_linux_record_tdep.arg6 = I386_EBP_REGNUM;
00918 
00919   tdep->i386_intx80_record = i386_linux_intx80_sysenter_syscall_record;
00920   tdep->i386_sysenter_record = i386_linux_intx80_sysenter_syscall_record;
00921   tdep->i386_syscall_record = i386_linux_intx80_sysenter_syscall_record;
00922 
00923   /* N_FUN symbols in shared libaries have 0 for their values and need
00924      to be relocated.  */
00925   set_gdbarch_sofun_address_maybe_missing (gdbarch, 1);
00926 
00927   /* GNU/Linux uses SVR4-style shared libraries.  */
00928   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
00929   set_solib_svr4_fetch_link_map_offsets
00930     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
00931 
00932   /* GNU/Linux uses the dynamic linker included in the GNU C Library.  */
00933   set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
00934 
00935   dwarf2_frame_set_signal_frame_p (gdbarch, i386_linux_dwarf_signal_frame_p);
00936 
00937   /* Enable TLS support.  */
00938   set_gdbarch_fetch_tls_load_module_address (gdbarch,
00939                                              svr4_fetch_objfile_link_map);
00940 
00941   /* Install supported register note sections.  */
00942   if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx"))
00943     set_gdbarch_core_regset_sections (gdbarch, i386_linux_avx_regset_sections);
00944   else if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.sse"))
00945     set_gdbarch_core_regset_sections (gdbarch, i386_linux_sse_regset_sections);
00946   else
00947     set_gdbarch_core_regset_sections (gdbarch, i386_linux_regset_sections);
00948 
00949   set_gdbarch_core_read_description (gdbarch,
00950                                      i386_linux_core_read_description);
00951 
00952   /* Displaced stepping.  */
00953   set_gdbarch_displaced_step_copy_insn (gdbarch,
00954                                         i386_linux_displaced_step_copy_insn);
00955   set_gdbarch_displaced_step_fixup (gdbarch, i386_displaced_step_fixup);
00956   set_gdbarch_displaced_step_free_closure (gdbarch,
00957                                            simple_displaced_step_free_closure);
00958   set_gdbarch_displaced_step_location (gdbarch,
00959                                        displaced_step_at_entry_point);
00960 
00961   /* Functions for 'catch syscall'.  */
00962   set_xml_syscall_file_name (XML_SYSCALL_FILENAME_I386);
00963   set_gdbarch_get_syscall_number (gdbarch,
00964                                   i386_linux_get_syscall_number);
00965 
00966   set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);
00967 }
00968 
00969 /* Provide a prototype to silence -Wmissing-prototypes.  */
00970 extern void _initialize_i386_linux_tdep (void);
00971 
00972 void
00973 _initialize_i386_linux_tdep (void)
00974 {
00975   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
00976                           i386_linux_init_abi);
00977 
00978   /* Initialize the Linux target description.  */
00979   initialize_tdesc_i386_linux ();
00980   initialize_tdesc_i386_mmx_linux ();
00981   initialize_tdesc_i386_avx_linux ();
00982 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines