OSDN Git Service

2000-03-04 Mark Kettenis <kettenis@gnu.org>
authorkettenis <kettenis>
Sat, 4 Mar 2000 23:37:33 +0000 (23:37 +0000)
committerkettenis <kettenis>
Sat, 4 Mar 2000 23:37:33 +0000 (23:37 +0000)
Fix support for Linux/i386 signal trampolines.  The old approach
didn't work for Linux 2.2 and beyond, and didn't work with recent
versions of the GNU C library.
* i386-tdep.c (LINUX_RT_SIGTRAMP_INSN0, LINUX_RT_SIGTRAMP_OFFSET0,
LINUX_RT_SIGTRAMP_INSN1, LINUX_RT_SIGTRAMP_OFFSET1): New defines.
(linux_rt_sigtramp_code): New variable.
(LINUX_RT_SIGTRAMP_LEN): New define.
(i386_linux_rt_sigtramp_start): New function.  Detect start of
signal trampolines for RT signals.
(i386_linux_sigtramp): Removed.
(i386_linux_in_sigtramp): New function.
(i386_linux_sigcontext_addr): New function.  Recognize the names
of the signal tranmpolines used by recent versions of the GNU C
library, and add support for RT signals.
        (LINUX_SIGCONTEXT_PC_OFFSET, LINUX_SIGCONTEXT_SP_OFFSET):  New
defines.  Moved here from config/i386/tm-linux.h.
(i386_linux_sigtramp_saved_pc, i386_linux_sigtramp_saved_sp):
Reimplement in terms of i386_linux_sigcontext_addr.
* config/i386/tm-linux.h (LINUX_SIGCONTEXT_SIZE): Removed.
        (LINUX_SIGCONTEXT_PC_OFFSET, LINUX_SIGCONTEXT_SP_OFFSET):
        Moved to i386-tdep.c.
(IN_SIGTRAMP): Redefine to call i386_linux_in_sigtramp.

gdb/ChangeLog
gdb/config/i386/tm-linux.h
gdb/i386-tdep.c

index 15bf236..ed438a9 100644 (file)
@@ -1,3 +1,28 @@
+2000-03-04  Mark Kettenis  <kettenis@gnu.org>
+
+       Fix support for Linux/i386 signal trampolines.  The old approach
+       didn't work for Linux 2.2 and beyond, and didn't work with recent
+       versions of the GNU C library.
+       * i386-tdep.c (LINUX_RT_SIGTRAMP_INSN0, LINUX_RT_SIGTRAMP_OFFSET0,
+       LINUX_RT_SIGTRAMP_INSN1, LINUX_RT_SIGTRAMP_OFFSET1): New defines.
+       (linux_rt_sigtramp_code): New variable.
+       (LINUX_RT_SIGTRAMP_LEN): New define.
+       (i386_linux_rt_sigtramp_start): New function.  Detect start of
+       signal trampolines for RT signals.
+       (i386_linux_sigtramp): Removed.
+       (i386_linux_in_sigtramp): New function.
+       (i386_linux_sigcontext_addr): New function.  Recognize the names
+       of the signal tranmpolines used by recent versions of the GNU C
+       library, and add support for RT signals.
+        (LINUX_SIGCONTEXT_PC_OFFSET, LINUX_SIGCONTEXT_SP_OFFSET):  New
+       defines.  Moved here from config/i386/tm-linux.h.
+       (i386_linux_sigtramp_saved_pc, i386_linux_sigtramp_saved_sp):
+       Reimplement in terms of i386_linux_sigcontext_addr.
+       * config/i386/tm-linux.h (LINUX_SIGCONTEXT_SIZE): Removed.
+        (LINUX_SIGCONTEXT_PC_OFFSET, LINUX_SIGCONTEXT_SP_OFFSET):
+        Moved to i386-tdep.c.
+       (IN_SIGTRAMP): Redefine to call i386_linux_in_sigtramp.
+
 Sat Mar  4 19:38:11 2000  Andrew Cagney  <cagney@b1.cygnus.com>
 
        By: Sat Mar 4 04:08:58 2000 Alexandre Oliva <oliva@lsd.ic.unicamp.br>
index 417d151..391d5c0 100644 (file)
 /* Definitions to target GDB to GNU/Linux on 386.
    Copyright 1992, 1993 Free Software Foundation, Inc.
 
-This file is part of GDB.
+   This file is part of GDB.
 
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
 
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
 
 #ifndef TM_LINUX_H
 #define TM_LINUX_H
 
-/* FIXME:  If nothing else gets added to this file, it could be removed
-   and configure could just use tm-i386.h instead. -fnf */
+#define I386_GNULINUX_TARGET
+#define HAVE_I387_REGS
+#ifdef HAVE_PTRACE_GETXFPREGS
+#define HAVE_SSE_REGS
+#endif
 
 #include "i386/tm-i386.h"
+#include "tm-linux.h"
 
-/* Offset to saved PC in sigcontext, from <linux/signal.h>.  */
-#define SIGCONTEXT_PC_OFFSET 38
+#define LOW_RETURN_REGNUM 0    /* holds low four bytes of result */
+#define HIGH_RETURN_REGNUM 2   /* holds high four bytes of result */
 
-/* We need this file for the SOLIB_TRAMPOLINE stuff. */
+/* This should probably move to tm-i386.h.  */
+#define TARGET_LONG_DOUBLE_BIT 80
 
-#include "tm-sysv4.h"
+#if defined(HAVE_LONG_DOUBLE) && defined(HOST_I386)
+/* The host and target are i386 machines and the compiler supports
+   long doubles. Long doubles on the host therefore have the same
+   layout as a 387 FPU stack register. */
+#define LD_I387
+
+extern int i387_extract_floating (PTR addr, int len, long double *dretptr);
+extern int i387_store_floating   (PTR addr, int len, long double val);
+
+#define TARGET_EXTRACT_FLOATING i387_extract_floating
+#define TARGET_STORE_FLOATING   i387_store_floating
+
+#define TARGET_ANALYZE_FLOATING                                        \
+  do                                                           \
+    {                                                          \
+      unsigned expon;                                          \
+                                                               \
+      low = extract_unsigned_integer (valaddr, 4);             \
+      high = extract_unsigned_integer (valaddr + 4, 4);                \
+      expon = extract_unsigned_integer (valaddr + 8, 2);       \
+                                                               \
+      nonnegative = ((expon & 0x8000) == 0);                   \
+      is_nan = ((expon & 0x7fff) == 0x7fff)                    \
+       && ((high & 0x80000000) == 0x80000000)                  \
+       && (((high & 0x7fffffff) | low) != 0);                  \
+    }                                                          \
+  while (0)
+
+#undef REGISTER_CONVERT_TO_VIRTUAL
+#define REGISTER_CONVERT_TO_VIRTUAL(REGNUM,TYPE,FROM,TO)       \
+{                                                              \
+  long double val = *((long double *)FROM);                    \
+  store_floating ((TO), TYPE_LENGTH (TYPE), val);              \
+}
+
+#undef REGISTER_CONVERT_TO_RAW
+#define REGISTER_CONVERT_TO_RAW(TYPE,REGNUM,FROM,TO)                   \
+{                                                                      \
+  long double val = extract_floating ((FROM), TYPE_LENGTH (TYPE));     \
+  *((long double *)TO) = val;                                          \
+}
+
+/* Return the GDB type object for the "standard" data type
+   of data in register N.  */
+#undef REGISTER_VIRTUAL_TYPE
+#define REGISTER_VIRTUAL_TYPE(N)                               \
+  (((N) == PC_REGNUM || (N) == FP_REGNUM || (N) == SP_REGNUM)  \
+   ? lookup_pointer_type (builtin_type_void)                   \
+   : IS_FP_REGNUM(N) ? builtin_type_long_double                        \
+   : IS_SSE_REGNUM(N) ? builtin_type_v4sf                      \
+   : builtin_type_int)
+
+#endif
 
 /* The following works around a problem with /usr/include/sys/procfs.h  */
 #define sys_quotactl 1
 
-#endif  /* #ifndef TM_LINUX_H */
+/* When the i386 Linux kernel calls a signal handler, the return
+   address points to a bit of code on the stack.  These definitions
+   are used to identify this bit of code as a signal trampoline in
+   order to support backtracing through calls to signal handlers.  */
+
+#define I386_LINUX_SIGTRAMP
+#define IN_SIGTRAMP(pc, name) i386_linux_in_sigtramp (pc, name)
+extern int i386_linux_in_sigtramp (CORE_ADDR, char *);
+
+/* We need our own version of sigtramp_saved_pc to get the saved PC in
+   a sigtramp routine.  */
+
+#define sigtramp_saved_pc i386_linux_sigtramp_saved_pc
+extern CORE_ADDR i386_linux_sigtramp_saved_pc (struct frame_info *);
+
+/* Signal trampolines don't have a meaningful frame.  As in tm-i386.h,
+   the frame pointer value we use is actually the frame pointer of the
+   calling frame--that is, the frame which was in progress when the
+   signal trampoline was entered.  gdb mostly treats this frame
+   pointer value as a magic cookie.  We detect the case of a signal
+   trampoline by looking at the SIGNAL_HANDLER_CALLER field, which is
+   set based on IN_SIGTRAMP.
+
+   When a signal trampoline is invoked from a frameless function, we
+   essentially have two frameless functions in a row.  In this case,
+   we use the same magic cookie for three frames in a row.  We detect
+   this case by seeing whether the next frame has
+   SIGNAL_HANDLER_CALLER set, and, if it does, checking whether the
+   current frame is actually frameless.  In this case, we need to get
+   the PC by looking at the SP register value stored in the signal
+   context.
+
+   This should work in most cases except in horrible situations where
+   a signal occurs just as we enter a function but before the frame
+   has been set up.  */
+
+#define FRAMELESS_SIGNAL(FRAME)                                        \
+  ((FRAME)->next != NULL                                       \
+   && (FRAME)->next->signal_handler_caller                     \
+   && frameless_look_for_prologue (FRAME))
+
+#undef FRAME_CHAIN
+#define FRAME_CHAIN(FRAME)                                     \
+  ((FRAME)->signal_handler_caller                              \
+   ? (FRAME)->frame                                            \
+    : (FRAMELESS_SIGNAL (FRAME)                                        \
+       ? (FRAME)->frame                                                \
+       : (!inside_entry_file ((FRAME)->pc)                     \
+         ? read_memory_integer ((FRAME)->frame, 4)             \
+         : 0)))
+
+#undef FRAME_SAVED_PC
+#define FRAME_SAVED_PC(FRAME)                                  \
+  ((FRAME)->signal_handler_caller                              \
+   ? sigtramp_saved_pc (FRAME)                                 \
+   : (FRAMELESS_SIGNAL (FRAME)                                 \
+      ? read_memory_integer (i386_linux_sigtramp_saved_sp ((FRAME)->next), 4) \
+      : read_memory_integer ((FRAME)->frame + 4, 4)))
+
+extern CORE_ADDR i386_linux_sigtramp_saved_sp (struct frame_info *);
+
+/* When we call a function in a shared library, and the PLT sends us
+   into the dynamic linker to find the function's real address, we
+   need to skip over the dynamic linker call.  This function decides
+   when to skip, and where to skip to.  See the comments for
+   SKIP_SOLIB_RESOLVER at the top of infrun.c.  */
+#define SKIP_SOLIB_RESOLVER i386_linux_skip_solib_resolver
+extern CORE_ADDR i386_linux_skip_solib_resolver (CORE_ADDR pc);
+
+/* N_FUN symbols in shared libaries have 0 for their values and need
+   to be relocated. */
+#define SOFUN_ADDRESS_MAYBE_MISSING
+
+#endif /* #ifndef TM_LINUX_H */
index c381961..10011f6 100644 (file)
@@ -781,11 +781,19 @@ i386v4_sigtramp_saved_pc (frame)
 
 #ifdef I386_LINUX_SIGTRAMP
 
-/* When the i386 Linux kernel calls a signal handler, the return
-   address points to a bit of code on the stack.  This function
-   returns whether the PC appears to be within this bit of code.
-
-   The instruction sequence is
+/* Linux has two flavors of signals.  Normal signal handlers, and
+   "realtime" (RT) signals.  The RT signals can provide additional
+   information to the signal handler if the SA_SIGINFO flag is set
+   when establishing a signal handler using `sigaction'.  It is not
+   unlikely that future versions of Linux will support SA_SIGINFO for
+   normal signals too.  */
+
+/* When the i386 Linux kernel calls a signal handler and the
+   SA_RESTORER flag isn't set, the return address points to a bit of
+   code on the stack.  This function returns whether the PC appears to
+   be within this bit of code.
+
+   The instruction sequence for normal signals is
        pop    %eax
        mov    $0x77,%eax
        int    $0x80
@@ -799,7 +807,15 @@ i386v4_sigtramp_saved_pc (frame)
    order to identify a signal trampoline, but there doesn't seem to be
    any other way.  The IN_SIGTRAMP macro in tm-linux.h arranges to
    only call us if no function name could be identified, which should
-   be the case since the code is on the stack.  */
+   be the case since the code is on the stack.
+
+   Detection of signal trampolines for handlers that set the
+   SA_RESTORER flag is in general not possible.  Unfortunately this is
+   what the GNU C Library has been doing for quite some time now.
+   However, as of version 2.1.2, the GNU C Library uses signal
+   trampolines (named __restore and __restore_rt) that are identical
+   to the ones used by the kernel.  Therefore, these trampolines are
+   supported too.  */
 
 #define LINUX_SIGTRAMP_INSN0 (0x58)    /* pop %eax */
 #define LINUX_SIGTRAMP_OFFSET0 (0)
@@ -821,8 +837,7 @@ static const unsigned char linux_sigtramp_code[] =
    the routine.  Otherwise, return 0.  */
 
 static CORE_ADDR
-i386_linux_sigtramp_start (pc)
-     CORE_ADDR pc;
+i386_linux_sigtramp_start (CORE_ADDR pc)
 {
   unsigned char buf[LINUX_SIGTRAMP_LEN];
 
@@ -864,51 +879,149 @@ i386_linux_sigtramp_start (pc)
   return pc;
 }
 
+/* This function does the same for RT signals.  Here the instruction
+   sequence is
+       mov    $0xad,%eax
+       int    $0x80
+   or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
+
+   The effect is to call the system call rt_sigreturn.  */
+
+#define LINUX_RT_SIGTRAMP_INSN0 (0xb8) /* mov $NNNN,%eax */
+#define LINUX_RT_SIGTRAMP_OFFSET0 (0)
+#define LINUX_RT_SIGTRAMP_INSN1 (0xcd) /* int */
+#define LINUX_RT_SIGTRAMP_OFFSET1 (5)
+
+static const unsigned char linux_rt_sigtramp_code[] =
+{
+  LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00,     /* mov $0xad,%eax */
+  LINUX_RT_SIGTRAMP_INSN1, 0x80                                /* int $0x80 */
+};
+
+#define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
+
+/* If PC is in a RT sigtramp routine, return the address of the start
+   of the routine.  Otherwise, return 0.  */
+
+static CORE_ADDR
+i386_linux_rt_sigtramp_start (CORE_ADDR pc)
+{
+  unsigned char buf[LINUX_RT_SIGTRAMP_LEN];
+
+  /* We only recognize a signal trampoline if PC is at the start of
+     one of the two instructions.  We optimize for finding the PC at
+     the start, as will be the case when the trampoline is not the
+     first frame on the stack.  We assume that in the case where the
+     PC is not at the start of the instruction sequence, there will be
+     a few trailing readable bytes on the stack.  */
+
+  if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
+    return 0;
+
+  if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
+    {
+      if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
+       return 0;
+
+      pc -= LINUX_RT_SIGTRAMP_OFFSET1;
+
+      if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
+       return 0;
+    }
+
+  if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
+    return 0;
+
+  return pc;
+}
+
 /* Return whether PC is in a Linux sigtramp routine.  */
 
 int
-i386_linux_sigtramp (pc)
-     CORE_ADDR pc;
+i386_linux_in_sigtramp (CORE_ADDR pc, char *name)
 {
-  return i386_linux_sigtramp_start (pc) != 0;
+  if (name)
+    return STREQ ("__restore", name) || STREQ ("__restore_rt", name);
+  
+  return (i386_linux_sigtramp_start (pc) != 0
+         || i386_linux_rt_sigtramp_start (pc) != 0);
 }
 
-/* Assuming FRAME is for a Linux sigtramp routine, return the saved
-   program counter.  The Linux kernel will set up a sigcontext
-   structure immediately before the sigtramp routine on the stack.  */
+/* Assuming FRAME is for a Linux sigtramp routine, return the address
+   of the associated sigcontext structure.  */
 
 CORE_ADDR
-i386_linux_sigtramp_saved_pc (frame)
-     struct frame_info *frame;
+i386_linux_sigcontext_addr (struct frame_info *frame)
 {
   CORE_ADDR pc;
 
   pc = i386_linux_sigtramp_start (frame->pc);
-  if (pc == 0)
-    error ("i386_linux_sigtramp_saved_pc called when no sigtramp");
-  return read_memory_integer ((pc
-                              - LINUX_SIGCONTEXT_SIZE
-                              + LINUX_SIGCONTEXT_PC_OFFSET),
-                             4);
+  if (pc)
+    {
+      CORE_ADDR sp;
+
+      if (frame->next)
+       /* If this isn't the top frame, the next frame must be for the
+          signal handler itself.  The sigcontext structure lives on
+          the stack, right after the signum argument.  */
+       return frame->next->frame + 12;
+
+      /* This is the top frame.  We'll have to find the address of the
+        sigcontext structure by looking at the stack pointer.  Keep
+        in mind that the first instruction of the sigtramp code is
+        "pop %eax".  If the PC is at this instruction, adjust the
+        returned value accordingly.  */
+      sp = read_register (SP_REGNUM);
+      if (pc == frame->pc)
+       return sp + 4;
+      return sp;
+    }
+
+  pc = i386_linux_rt_sigtramp_start (frame->pc);
+  if (pc)
+    {
+      if (frame->next)
+       /* If this isn't the top frame, the next frame must be for the
+          signal handler itself.  The sigcontext structure is part of
+          the user context.  A pointer to the user context is passed
+          as the third argument to the signal handler.  */
+       return read_memory_integer (frame->next->frame + 16, 4) + 20;
+
+      /* This is the top frame.  Again, use the stack pointer to find
+        the address of the sigcontext structure.  */
+      return read_memory_integer (read_register (SP_REGNUM) + 8, 4) + 20;
+    }
+
+  error ("Couldn't recognize signal trampoline.");
+  return 0;
 }
 
+/* Offset to saved PC in sigcontext, from <asm/sigcontext.h>.  */
+#define LINUX_SIGCONTEXT_PC_OFFSET (56)
+
 /* Assuming FRAME is for a Linux sigtramp routine, return the saved
-   stack pointer.  The Linux kernel will set up a sigcontext structure
-   immediately before the sigtramp routine on the stack.  */
+   program counter.  */
 
 CORE_ADDR
-i386_linux_sigtramp_saved_sp (frame)
-     struct frame_info *frame;
+i386_linux_sigtramp_saved_pc (struct frame_info *frame)
 {
-  CORE_ADDR pc;
+  CORE_ADDR addr;
+  addr = i386_linux_sigcontext_addr (frame);
+  return read_memory_integer (addr + LINUX_SIGCONTEXT_PC_OFFSET, 4);
+}
 
-  pc = i386_linux_sigtramp_start (frame->pc);
-  if (pc == 0)
-    error ("i386_linux_sigtramp_saved_sp called when no sigtramp");
-  return read_memory_integer ((pc
-                              - LINUX_SIGCONTEXT_SIZE
-                              + LINUX_SIGCONTEXT_SP_OFFSET),
-                             4);
+/* Offset to saved SP in sigcontext, from <asm/sigcontext.h>.  */
+#define LINUX_SIGCONTEXT_SP_OFFSET (28)
+
+/* Assuming FRAME is for a Linux sigtramp routine, return the saved
+   stack pointer.  */
+
+CORE_ADDR
+i386_linux_sigtramp_saved_sp (struct frame_info *frame)
+{
+  CORE_ADDR addr;
+  addr = i386_linux_sigcontext_addr (frame);
+  return read_memory_integer (addr + LINUX_SIGCONTEXT_SP_OFFSET, 4);
 }
 
 #endif /* I386_LINUX_SIGTRAMP */