Factor out the 'extend' function. We only need one version for Linux and
one for Darwin. Down from 11. 
Carve out a new function 'track_frame_memory' that communicates to the
tool the allocation of a new stack frame. This was slightly different on
Linux and Darwin but should be the same on both platforms.
New files: priv_sigframe.h and sigframe-common.c


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@15109 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/Makefile.am b/coregrind/Makefile.am
index e62d774..f43d4c9 100644
--- a/coregrind/Makefile.am
+++ b/coregrind/Makefile.am
@@ -243,6 +243,7 @@
 	m_scheduler/priv_sema.h \
 	m_scheduler/priv_sched-lock.h \
 	m_scheduler/priv_sched-lock-impl.h \
+	m_sigframe/priv_sigframe.h \
 	m_syswrap/priv_types_n_macros.h \
 	m_syswrap/priv_syswrap-generic.h \
 	m_syswrap/priv_syswrap-linux.h \
@@ -377,6 +378,7 @@
 	m_scheduler/sema.c \
 	m_scheduler/sched-lock.c \
 	m_scheduler/sched-lock-generic.c \
+	m_sigframe/sigframe-common.c \
 	m_sigframe/sigframe-x86-linux.c \
 	m_sigframe/sigframe-amd64-linux.c \
 	m_sigframe/sigframe-ppc32-linux.c \
diff --git a/coregrind/m_sigframe/priv_sigframe.h b/coregrind/m_sigframe/priv_sigframe.h
new file mode 100644
index 0000000..a81f4d0
--- /dev/null
+++ b/coregrind/m_sigframe/priv_sigframe.h
@@ -0,0 +1,46 @@
+/* -*- mode: C; c-basic-offset: 3; -*- */
+
+/*--------------------------------------------------------------------*/
+/*--- Module-local header file for m_sigframe.                     ---*/
+/*---                                              priv_sigframe.h ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2015-2015   Florian Krohm
+
+   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.
+
+   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.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#ifndef __PRIV_SIGFRAME_H
+#define __PRIV_SIGFRAME_H
+
+#include "pub_core_basics.h"        // types
+#include "pub_core_threadstate.h"   // ThreadState
+
+/* --------------- Implemented in sigframe-common.c ---------------*/
+
+Bool ML_(sf_extend_stack)( const ThreadState *tst, Addr addr, SizeT size );
+
+#endif   // __PRIV_SIGFRAME_H
+
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/
diff --git a/coregrind/m_sigframe/sigframe-amd64-darwin.c b/coregrind/m_sigframe/sigframe-amd64-darwin.c
index 3aa52a3..77752be 100644
--- a/coregrind/m_sigframe/sigframe-amd64-darwin.c
+++ b/coregrind/m_sigframe/sigframe-amd64-darwin.c
@@ -82,19 +82,6 @@
 };
 
 
-/* Extend the stack segment downwards if needed so as to ensure the
-   new signal frames are mapped to something.  Return a Bool
-   indicating whether or not the operation was successful.
-*/
-static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
-{
-   ThreadId tid = tst->tid;
-   VG_TRACK( new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB,
-             size + VG_STACK_REDZONE_SZB, tid );
-   return True;
-}
-
-
 /* Create a signal frame for thread 'tid'.  Make a 3-arg frame
    regardless of whether the client originally requested a 1-arg
    version (no SA_SIGINFO) or a 3-arg one (SA_SIGINFO) since in the
@@ -122,7 +109,7 @@
                 entry to a function. */
 
    tst = VG_(get_ThreadState)(tid);
-   if (!extend(tst, rsp, sp_top_of_frame - rsp))
+   if (! ML_(sf_extend_stack)(tst, rsp, sp_top_of_frame - rsp))
       return;
 
    vg_assert(VG_IS_16_ALIGNED(rsp+8));
diff --git a/coregrind/m_sigframe/sigframe-amd64-linux.c b/coregrind/m_sigframe/sigframe-amd64-linux.c
index 62698aa..a2b79e5 100644
--- a/coregrind/m_sigframe/sigframe-amd64-linux.c
+++ b/coregrind/m_sigframe/sigframe-amd64-linux.c
@@ -45,6 +45,7 @@
 #include "pub_core_tooliface.h"
 #include "pub_core_trampoline.h"
 #include "pub_core_sigframe.h"   /* self */
+#include "priv_sigframe.h"
 
 /* This module creates and removes signal frames for signal deliveries
    on amd64-linux.
@@ -372,50 +373,6 @@
 }
 
 
-/* Extend the stack segment downwards if needed so as to ensure the
-   new signal frames are mapped to something.  Return a Bool
-   indicating whether or not the operation was successful.
-*/
-static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
-{
-   ThreadId        tid = tst->tid;
-   NSegment const* stackseg = NULL;
-
-   if (VG_(extend_stack)(tid, addr)) {
-      stackseg = VG_(am_find_nsegment)(addr);
-      if (0 && stackseg)
-	 VG_(printf)("frame=%#lx seg=%#lx-%#lx\n",
-		     addr, stackseg->start, stackseg->end);
-   }
-
-   if (stackseg == NULL || !stackseg->hasR || !stackseg->hasW) {
-      VG_(message)(
-         Vg_UserMsg,
-         "Can't extend stack to %#lx during signal delivery for thread %d:\n",
-         addr, tid);
-      if (stackseg == NULL)
-         VG_(message)(Vg_UserMsg, "  no stack segment\n");
-      else
-         VG_(message)(Vg_UserMsg, "  too small or bad protection modes\n");
-
-      /* set SIGSEGV to default handler */
-      VG_(set_default_handler)(VKI_SIGSEGV);
-      VG_(synth_fault_mapping)(tid, addr);
-
-      /* The whole process should be about to die, since the default
-	 action of SIGSEGV to kill the whole process. */
-      return False;
-   }
-
-   /* For tracking memory events, indicate the entire frame has been
-      allocated. */
-   VG_TRACK( new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB,
-             size + VG_STACK_REDZONE_SZB, tid );
-
-   return True;
-}
-
-
 /* Build the Valgrind-specific part of a signal frame. */
 
 static void build_vg_sigframe(struct vg_sigframe *frame,
@@ -455,7 +412,7 @@
    rsp = VG_ROUNDDN(rsp, 16) - 8;
    frame = (struct rt_sigframe *)rsp;
 
-   if (!extend(tst, rsp, sizeof(*frame)))
+   if (! ML_(sf_extend_stack)(tst, rsp, sizeof(*frame)))
       return rsp_top_of_frame;
 
    /* retaddr, siginfo, uContext fields are to be written */
diff --git a/coregrind/m_sigframe/sigframe-arm-linux.c b/coregrind/m_sigframe/sigframe-arm-linux.c
index fe8cb12..dbbebc3 100644
--- a/coregrind/m_sigframe/sigframe-arm-linux.c
+++ b/coregrind/m_sigframe/sigframe-arm-linux.c
@@ -50,7 +50,7 @@
 #include "pub_core_signals.h"
 #include "pub_core_tooliface.h"
 #include "pub_core_trampoline.h"
-#include "pub_core_transtab.h"      // VG_(discard_translations)
+#include "priv_sigframe.h"
 
 
 /* This uses the hack of dumping the vex guest state along with both
@@ -81,44 +81,6 @@
    struct sigframe sig;
 };
 
-static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
-{
-   ThreadId        tid = tst->tid;
-   NSegment const* stackseg = NULL;
-
-   if (VG_(extend_stack)(tid, addr)) {
-      stackseg = VG_(am_find_nsegment)(addr);
-      if (0 && stackseg)
-    VG_(printf)("frame=%#lx seg=%#lx-%#lx\n",
-           addr, stackseg->start, stackseg->end);
-   }
-
-   if (stackseg == NULL || !stackseg->hasR || !stackseg->hasW) {
-      VG_(message)(
-         Vg_UserMsg,
-         "Can't extend stack to %#lx during signal delivery for thread %d:",
-         addr, tid);
-      if (stackseg == NULL)
-         VG_(message)(Vg_UserMsg, "  no stack segment");
-      else
-         VG_(message)(Vg_UserMsg, "  too small or bad protection modes");
-
-      /* set SIGSEGV to default handler */
-      VG_(set_default_handler)(VKI_SIGSEGV);
-      VG_(synth_fault_mapping)(tid, addr);
-
-      /* The whole process should be about to die, since the default
-    action of SIGSEGV to kill the whole process. */
-      return False;
-   }
-
-   /* For tracking memory events, indicate the entire frame has been
-      allocated. */
-   VG_TRACK( new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB,
-             size + VG_STACK_REDZONE_SZB, tid );
-
-   return True;
-}
 
 static void synth_ucontext( ThreadId tid, const vki_siginfo_t *si,
                     UWord trapno, UWord err, const vki_sigset_t *set, 
@@ -223,7 +185,7 @@
    sp -= size;
    sp = VG_ROUNDDN(sp, 16);
 
-   if(!extend(tst, sp, size))
+   if(! ML_(sf_extend_stack)(tst, sp, size))
       I_die_here; // XXX Incorrect behavior
 
 
diff --git a/coregrind/m_sigframe/sigframe-arm64-linux.c b/coregrind/m_sigframe/sigframe-arm64-linux.c
index 0bea57d..1b09696 100644
--- a/coregrind/m_sigframe/sigframe-arm64-linux.c
+++ b/coregrind/m_sigframe/sigframe-arm64-linux.c
@@ -33,7 +33,6 @@
 
 #include "pub_core_basics.h"
 #include "pub_core_vki.h"
-//ZZ #include "pub_core_vkiscnums.h"
 #include "pub_core_libcsetjmp.h"    // to keep _threadstate.h happy
 #include "pub_core_threadstate.h"
 #include "pub_core_aspacemgr.h"
@@ -46,7 +45,7 @@
 #include "pub_core_signals.h"
 #include "pub_core_tooliface.h"
 #include "pub_core_trampoline.h"
-//ZZ #include "pub_core_transtab.h"      // VG_(discard_translations)
+#include "priv_sigframe.h"
 
 
 /* This uses the hack of dumping the vex guest state along with both
@@ -79,44 +78,6 @@
    struct sigframe sig;
 };
 
-static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
-{
-   ThreadId        tid = tst->tid;
-   NSegment const* stackseg = NULL;
-
-   if (VG_(extend_stack)(tid, addr)) {
-      stackseg = VG_(am_find_nsegment)(addr);
-      if (0 && stackseg)
-         VG_(printf)("frame=%#lx seg=%#lx-%#lx\n",
-                     addr, stackseg->start, stackseg->end);
-   }
-
-   if (stackseg == NULL || !stackseg->hasR || !stackseg->hasW) {
-      VG_(message)(
-         Vg_UserMsg,
-         "Can't extend stack to %#lx during signal delivery for thread %d:",
-         addr, tid);
-      if (stackseg == NULL)
-         VG_(message)(Vg_UserMsg, "  no stack segment");
-      else
-         VG_(message)(Vg_UserMsg, "  too small or bad protection modes");
-
-      /* set SIGSEGV to default handler */
-      VG_(set_default_handler)(VKI_SIGSEGV);
-      VG_(synth_fault_mapping)(tid, addr);
-
-      /* The whole process should be about to die, since the default
-         action of SIGSEGV to kill the whole process. */
-      return False;
-   }
-
-   /* For tracking memory events, indicate the entire frame has been
-      allocated. */
-   VG_TRACK( new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB,
-             size + VG_STACK_REDZONE_SZB, tid );
-
-   return True;
-}
 
 static void synth_ucontext( ThreadId tid, const vki_siginfo_t *si,
                             UWord trapno, UWord err, const vki_sigset_t *set, 
@@ -212,7 +173,7 @@
    sp -= size;
    sp = VG_ROUNDDN(sp, 16);
 
-   if (!extend(tst, sp, size))
+   if (! ML_(sf_extend_stack)(tst, sp, size))
       return; // Give up.  No idea if this is correct
 
    struct rt_sigframe *rsf = (struct rt_sigframe *)sp;
diff --git a/coregrind/m_sigframe/sigframe-common.c b/coregrind/m_sigframe/sigframe-common.c
new file mode 100644
index 0000000..59b34ee
--- /dev/null
+++ b/coregrind/m_sigframe/sigframe-common.c
@@ -0,0 +1,110 @@
+/* -*- mode: C; c-basic-offset: 3; -*- */
+
+/*--------------------------------------------------------------------*/
+/*--- Signal frames handling: stuff common to most/all platforms   ---*/
+/*---                                                              ---*/
+/*---                                            sigframe-common.c ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2013 Nicholas Nethercote
+      njn@valgrind.org
+   Copyright (C) 2006-2013 OpenWorks Ltd
+      info@open-works.co.uk
+
+   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.
+
+   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.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#include "pub_core_aspacemgr.h"      // VG_(am_find_nsegment)
+#include "pub_core_signals.h"        // VG_(extend_stack)
+#include "pub_core_libcprint.h"      // VG_(umsg)
+#include "pub_core_tooliface.h"      // VG_TRACK
+#include "pub_core_machine.h"        // VG_STACK_REDZONE_SZB
+#include "priv_sigframe.h"           // self
+
+
+/* For tracking memory events, indicate the entire frame has been
+   allocated.  Except, don't mess with the area which
+   overlaps the previous frame's redzone. */
+static void track_frame_memory ( Addr addr, SizeT size, ThreadId tid )
+{
+   VG_TRACK( new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB, size, tid );
+}
+
+#if defined(VGO_linux)
+
+/* Extend the stack segment downwards if needed so as to ensure the
+   new signal frames are mapped to something.  Return a Bool
+   indicating whether or not the operation was successful. */
+Bool ML_(sf_extend_stack) ( const ThreadState *tst, Addr addr, SizeT size )
+{
+   ThreadId        tid = tst->tid;
+   const NSegment *stackseg = NULL;
+
+   if (VG_(extend_stack)(tid, addr)) {
+      stackseg = VG_(am_find_nsegment)(addr);
+      if (0 && stackseg)
+	 VG_(printf)("frame=%#lx seg=%#lx-%#lx\n",
+		     addr, stackseg->start, stackseg->end);
+   }
+
+   if (stackseg == NULL || !stackseg->hasR || !stackseg->hasW) {
+      VG_(umsg)("Can't extend stack to %#lx during signal delivery for "
+                "thread %d:\n", addr, tid);
+      if (stackseg == NULL)
+         VG_(umsg)("  no stack segment\n");
+      else
+         VG_(umsg)("  too small or bad protection modes\n");
+
+      /* set SIGSEGV to default handler */
+      VG_(set_default_handler)(VKI_SIGSEGV);
+      VG_(synth_fault_mapping)(tid, addr);
+
+      /* The whole process should be about to die, since the default
+	 action of SIGSEGV to kill the whole process. */
+      return False;
+   }
+
+   /* Tell the tool about the new memory */
+   track_frame_memory(addr, size, tid);
+
+   return True;
+}
+
+#elif defined(VGO_darwin)
+
+/* Extend the stack segment downwards if needed so as to ensure the
+   new signal frames are mapped to something.  Return a Bool
+   indicating whether or not the operation was successful. */
+Bool ML_(sf_extend_stack) ( const ThreadState *tst, Addr addr, SizeT size )
+{
+   /* Tell the tool about the new memory */
+   track_frame_memory(addr, size, tst->tid);
+   return True;
+}
+
+#else
+#error unknown OS
+#endif
+
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/
diff --git a/coregrind/m_sigframe/sigframe-mips32-linux.c b/coregrind/m_sigframe/sigframe-mips32-linux.c
index 4c8c8aa..6f09e4d 100644
--- a/coregrind/m_sigframe/sigframe-mips32-linux.c
+++ b/coregrind/m_sigframe/sigframe-mips32-linux.c
@@ -46,7 +46,7 @@
 #include "pub_core_signals.h"
 #include "pub_core_tooliface.h"
 #include "pub_core_trampoline.h"
-#include "pub_core_transtab.h"      // VG_(discard_translations)
+#include "priv_sigframe.h"
 
 struct vg_sig_private
 {
@@ -74,45 +74,6 @@
   struct vg_sig_private priv;
 };
 
-/* Extend the stack segment downwards if needed so as to ensure the
-   new signal frames are mapped to something.  Return a Bool
-   indicating whether or not the operation was successful.
-*/
-static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
-{
-  ThreadId        tid = tst->tid;
-  NSegment const* stackseg = NULL;
-
-  if (VG_(extend_stack)(tid, addr)) {
-     stackseg = VG_(am_find_nsegment)(addr);
-  }
-
-   if (stackseg == NULL || !stackseg->hasR || !stackseg->hasW)
-     {
-       VG_(message)(Vg_UserMsg,
-         "Can't extend stack to %#lx during signal delivery for thread %d:\n",
-         addr, tid );
-       if (stackseg == NULL)
-         VG_(message)( Vg_UserMsg, "  no stack segment\n" );
-       else
-         VG_(message)( Vg_UserMsg, "  too small or bad protection modes\n" );
-
-       /* set SIGSEGV to default handler */
-       VG_(set_default_handler)( VKI_SIGSEGV );
-       VG_(synth_fault_mapping)( tid, addr );
-
-       /* The whole process should be about to die, since the default
-          action of SIGSEGV to kill the whole process. */
-      return False;
-    }
-
-    /* For tracking memory events, indicate the entire frame has been
-       allocated. */
-    VG_TRACK( new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB,
-              size + VG_STACK_REDZONE_SZB, tid );
-
-    return True;
-}
 
 static 
 void setup_sigcontext2 ( ThreadState* tst, struct vki_sigcontext **sc1,
@@ -187,7 +148,7 @@
     }
 
   tst = VG_(get_ThreadState)(tid);
-  if (!extend(tst, sp, sp_top_of_frame - sp))
+  if (! ML_(sf_extend_stack)(tst, sp, sp_top_of_frame - sp))
     return;
 
   vg_assert(VG_IS_8_ALIGNED(sp));
diff --git a/coregrind/m_sigframe/sigframe-mips64-linux.c b/coregrind/m_sigframe/sigframe-mips64-linux.c
index bb178ec..fa6615b 100644
--- a/coregrind/m_sigframe/sigframe-mips64-linux.c
+++ b/coregrind/m_sigframe/sigframe-mips64-linux.c
@@ -46,7 +46,7 @@
 #include "pub_core_signals.h"
 #include "pub_core_tooliface.h"
 #include "pub_core_trampoline.h"
-#include "pub_core_transtab.h"  /* VG_(discard_translations) */
+#include "priv_sigframe.h"
 
 struct vg_sig_private {
    UInt magicPI;
@@ -71,44 +71,6 @@
    struct vg_sig_private priv;
 };
 
-/* Extend the stack segment downwards if needed so as to ensure the new signal
-   frames are mapped to something. Return a Bool indicating whether or not the
-   operation was successful.
-*/
-static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
-{
-   ThreadId tid = tst->tid;
-   NSegment const* stackseg = NULL;
-
-   if (VG_(extend_stack)(tid, addr)) {
-      stackseg = VG_(am_find_nsegment)(addr);
-   }
-
-   if (stackseg == NULL || !stackseg->hasR || !stackseg->hasW) {
-      VG_(message)(Vg_UserMsg,
-         "Can't extend stack to %#lx during signal delivery for thread %d:\n",
-         addr, tid);
-      if (stackseg == NULL)
-         VG_(message)(Vg_UserMsg, " no stack segment\n");
-      else
-         VG_(message)(Vg_UserMsg, " too small or bad protection modes\n");
-
-      /* set SIGSEGV to default handler */
-      VG_(set_default_handler)(VKI_SIGSEGV);
-      VG_(synth_fault_mapping)(tid, addr);
-
-      /* The whole process should be about to die, since the default
-         action of SIGSEGV to kill the whole process. */
-      return False;
-   }
-
-   /* For tracking memory events, indicate the entire frame has been
-      allocated. */
-   VG_TRACK(new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB,
-            size + VG_STACK_REDZONE_SZB, tid);
-
-   return True;
-}
 
 static void setup_sigcontext ( ThreadState* tst, struct vki_sigcontext **sc1,
                                const vki_siginfo_t *si)
@@ -173,7 +135,7 @@
    sp = sp_top_of_frame - sizeof(struct rt_sigframe);
 
    tst = VG_(get_ThreadState)(tid);
-   if (!extend(tst, sp, sp_top_of_frame - sp))
+   if (! ML_(sf_extend_stack)(tst, sp, sp_top_of_frame - sp))
       return;
 
    sp = VG_ROUNDDN(sp, 16);
diff --git a/coregrind/m_sigframe/sigframe-ppc32-linux.c b/coregrind/m_sigframe/sigframe-ppc32-linux.c
index e48846c..346abb2 100644
--- a/coregrind/m_sigframe/sigframe-ppc32-linux.c
+++ b/coregrind/m_sigframe/sigframe-ppc32-linux.c
@@ -49,7 +49,7 @@
 #include "pub_core_tooliface.h"
 #include "pub_core_trampoline.h"
 #include "pub_core_transtab.h"      // VG_(discard_translations)
-
+#include "priv_sigframe.h"
 
 /* This module creates and removes signal frames for signal deliveries
    on ppc32-linux.
@@ -502,48 +502,6 @@
 //..                   Vg_CoreSignal, zztid, VG_O_STACK_PTR, sizeof(Addr))
 */
 
-/* Extend the stack segment downwards if needed so as to ensure the
-   new signal frames are mapped to something.  Return a Bool
-   indicating whether or not the operation was successful.
-*/
-static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
-{
-   ThreadId        tid = tst->tid;
-   NSegment const* stackseg = NULL;
-
-   if (VG_(extend_stack)(tid, addr)) {
-      stackseg = VG_(am_find_nsegment)(addr);
-      if (0 && stackseg)
-	 VG_(printf)("frame=%#lx seg=%#lx-%#lx\n",
-		     addr, stackseg->start, stackseg->end);
-   }
-
-   if (stackseg == NULL || !stackseg->hasR || !stackseg->hasW) {
-      VG_(message)(
-         Vg_UserMsg,
-         "Can't extend stack to %#lx during signal delivery for thread %d:\n",
-         addr, tid);
-      if (stackseg == NULL)
-         VG_(message)(Vg_UserMsg, "  no stack segment\n");
-      else
-         VG_(message)(Vg_UserMsg, "  too small or bad protection modes\n");
-
-      /* set SIGSEGV to default handler */
-      VG_(set_default_handler)(VKI_SIGSEGV);
-      VG_(synth_fault_mapping)(tid, addr);
-
-      /* The whole process should be about to die, since the default
-	 action of SIGSEGV to kill the whole process. */
-      return False;
-   }
-
-   /* For tracking memory events, indicate the entire frame has been
-      allocated. */
-   VG_TRACK( new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB,
-             size + VG_STACK_REDZONE_SZB, tid );
-
-   return True;
-}
 
 //.. /* Build the Valgrind-specific part of a signal frame. */
 //.. 
@@ -692,7 +650,7 @@
 
    tst = VG_(get_ThreadState)(tid);
 
-   if (!extend(tst, sp, sp_top_of_frame - sp))
+   if (! ML_(sf_extend_stack)(tst, sp, sp_top_of_frame - sp))
       return;
 
    vg_assert(VG_IS_16_ALIGNED(sp));
diff --git a/coregrind/m_sigframe/sigframe-ppc64-linux.c b/coregrind/m_sigframe/sigframe-ppc64-linux.c
index 1bf3f48..8a6bcb3 100644
--- a/coregrind/m_sigframe/sigframe-ppc64-linux.c
+++ b/coregrind/m_sigframe/sigframe-ppc64-linux.c
@@ -49,7 +49,7 @@
 #include "pub_core_tooliface.h"
 #include "pub_core_trampoline.h"
 #include "pub_core_transtab.h"      // VG_(discard_translations)
-
+#include "priv_sigframe.h"
 
 /* This module creates and removes signal frames for signal deliveries
    on ppc64-linux.
@@ -132,49 +132,6 @@
    } while (0)
 
 
-/* Extend the stack segment downwards if needed so as to ensure the
-   new signal frames are mapped to something.  Return a Bool
-   indicating whether or not the operation was successful.
-*/
-static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
-{
-   ThreadId        tid = tst->tid;
-   NSegment const* stackseg = NULL;
-
-   if (VG_(extend_stack)(tid, addr)) {
-      stackseg = VG_(am_find_nsegment)(addr);
-      if (0 && stackseg)
-	 VG_(printf)("frame=%#lx seg=%#lx-%#lx\n",
-		     addr, stackseg->start, stackseg->end);
-   }
-
-   if (stackseg == NULL || !stackseg->hasR || !stackseg->hasW) {
-      VG_(message)(
-         Vg_UserMsg,
-         "Can't extend stack to %#lx during signal delivery for thread %d:\n",
-         addr, tid);
-      if (stackseg == NULL)
-         VG_(message)(Vg_UserMsg, "  no stack segment\n");
-      else
-         VG_(message)(Vg_UserMsg, "  too small or bad protection modes\n");
-
-      /* set SIGSEGV to default handler */
-      VG_(set_default_handler)(VKI_SIGSEGV);
-      VG_(synth_fault_mapping)(tid, addr);
-
-      /* The whole process should be about to die, since the default
-	 action of SIGSEGV to kill the whole process. */
-      return False;
-   }
-
-   /* For tracking memory events, indicate the entire frame has been
-      allocated. */
-   VG_TRACK( new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB,
-             size + VG_STACK_REDZONE_SZB, tid );
-
-   return True;
-}
-
 
 /* EXPORTED */
 void VG_(sigframe_create)( ThreadId tid, 
@@ -201,7 +158,7 @@
    sp = sp_top_of_frame - sizeof(struct rt_sigframe);
 
    tst = VG_(get_ThreadState)(tid);
-   if (!extend(tst, sp, sp_top_of_frame - sp))
+   if (! ML_(sf_extend_stack)(tst, sp, sp_top_of_frame - sp))
       return;
 
    vg_assert(VG_IS_16_ALIGNED(sp));
diff --git a/coregrind/m_sigframe/sigframe-s390x-linux.c b/coregrind/m_sigframe/sigframe-s390x-linux.c
index b8a5894..b3ca4ae 100644
--- a/coregrind/m_sigframe/sigframe-s390x-linux.c
+++ b/coregrind/m_sigframe/sigframe-s390x-linux.c
@@ -45,6 +45,7 @@
 #include "pub_core_signals.h"
 #include "pub_core_tooliface.h"
 #include "pub_core_trampoline.h"
+#include "priv_sigframe.h"
 
 #if defined(VGA_s390x)
 
@@ -258,49 +259,6 @@
    tst->arch.vex.guest_IA = sigregs->regs.psw.addr;
 }
 
-/* Extend the stack segment downwards if needed so as to ensure the
-   new signal frames are mapped to something.  Return a Bool
-   indicating whether or not the operation was successful.
-*/
-static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
-{
-   ThreadId        tid = tst->tid;
-   NSegment const* stackseg = NULL;
-
-   if (VG_(extend_stack)(tid, addr)) {
-      stackseg = VG_(am_find_nsegment)(addr);
-      if (0 && stackseg)
-	 VG_(printf)("frame=%#lx seg=%#lx-%#lx\n",
-		     addr, stackseg->start, stackseg->end);
-   }
-
-   if (stackseg == NULL || !stackseg->hasR || !stackseg->hasW) {
-      VG_(message)(
-         Vg_UserMsg,
-         "Can't extend stack to %#lx during signal delivery for thread %d:\n",
-         addr, tid);
-      if (stackseg == NULL)
-         VG_(message)(Vg_UserMsg, "  no stack segment\n");
-      else
-         VG_(message)(Vg_UserMsg, "  too small or bad protection modes\n");
-
-      /* set SIGSEGV to default handler */
-      VG_(set_default_handler)(VKI_SIGSEGV);
-      VG_(synth_fault_mapping)(tid, addr);
-
-      /* The whole process should be about to die, since the default
-	 action of SIGSEGV to kill the whole process. */
-      return False;
-   }
-
-   /* For tracking memory events, indicate the entire frame has been
-      allocated. */
-   VG_TRACK( new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB,
-             size + VG_STACK_REDZONE_SZB, tid );
-
-   return True;
-}
-
 
 /* Build the Valgrind-specific part of a signal frame. */
 
@@ -340,7 +298,7 @@
    sp -= sizeof(*frame);
    frame = (struct sigframe *)sp;
 
-   if (!extend(tst, sp, sizeof(*frame)))
+   if (! ML_(sf_extend_stack)(tst, sp, sizeof(*frame)))
       return sp_top_of_frame;
 
    /* retcode, sigNo, sc, sregs fields are to be written */
@@ -400,7 +358,7 @@
    sp -= sizeof(*frame);
    frame = (struct rt_sigframe *)sp;
 
-   if (!extend(tst, sp, sizeof(*frame)))
+   if (! ML_(sf_extend_stack)(tst, sp, sizeof(*frame)))
       return sp_top_of_frame;
 
    /* retcode, sigNo, sc, sregs fields are to be written */
diff --git a/coregrind/m_sigframe/sigframe-tilegx-linux.c b/coregrind/m_sigframe/sigframe-tilegx-linux.c
index 9514afe..48d0859 100644
--- a/coregrind/m_sigframe/sigframe-tilegx-linux.c
+++ b/coregrind/m_sigframe/sigframe-tilegx-linux.c
@@ -47,7 +47,7 @@
 #include "pub_core_signals.h"
 #include "pub_core_tooliface.h"
 #include "pub_core_trampoline.h"
-#include "pub_core_transtab.h"      // VG_(discard_translations)
+#include "priv_sigframe.h"
 
 struct vg_sig_private
 {
@@ -67,44 +67,6 @@
   struct vg_sig_private priv;
 };
 
-/* Extend the stack segment downwards if needed so as to ensure the
-   new signal frames are mapped to something.  Return a Bool
-   indicating whether or not the operation was successful.
-*/
-static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
-{
-  ThreadId        tid = tst->tid;
-  NSegment const* stackseg = NULL;
-
-  if (VG_(extend_stack)(tid, addr))
-    stackseg = VG_(am_find_nsegment)(addr);
-
-  if (stackseg == NULL || !stackseg->hasR || !stackseg->hasW)
-  {
-    VG_(message)(Vg_UserMsg,
-                 "Can't extend stack to %#lx during signal delivery for thread %d:\n",
-                 addr, tid );
-    if (stackseg == NULL)
-      VG_(message)( Vg_UserMsg, "  no stack segment\n" );
-    else
-      VG_(message)( Vg_UserMsg, "  too small or bad protection modes\n" );
-
-    /* set SIGSEGV to default handler */
-    VG_(set_default_handler)( VKI_SIGSEGV );
-    VG_(synth_fault_mapping)( tid, addr );
-
-    /* The whole process should be about to die, since the default
-       action of SIGSEGV to kill the whole process. */
-    return False;
-  }
-
-  /* For tracking memory events, indicate the entire frame has been
-     allocated. */
-  VG_TRACK( new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB,
-            size + VG_STACK_REDZONE_SZB, tid );
-
-  return True;
-}
 
 static
 void setup_sigcontext2 ( ThreadState* tst, struct vki_sigcontext **sc1,
@@ -196,7 +158,7 @@
   sp = sp_top_of_frame - sizeof(struct rt_sigframe);
 
   tst = VG_(get_ThreadState)(tid);
-  if (!extend(tst, sp, sizeof(struct rt_sigframe)))
+  if (! ML_(sf_extend_stack)(tst, sp, sizeof(struct rt_sigframe)))
     return;
 
   vg_assert(VG_IS_8_ALIGNED(sp));
diff --git a/coregrind/m_sigframe/sigframe-x86-darwin.c b/coregrind/m_sigframe/sigframe-x86-darwin.c
index ea92ebf..6c2c1ef 100644
--- a/coregrind/m_sigframe/sigframe-x86-darwin.c
+++ b/coregrind/m_sigframe/sigframe-x86-darwin.c
@@ -85,25 +85,6 @@
 };
 
 
-/* Extend the stack segment downwards if needed so as to ensure the
-   new signal frames are mapped to something.  Return a Bool
-   indicating whether or not the operation was successful.
-*/
-static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
-{
-   ThreadId tid = tst->tid;
-   /* For tracking memory events, indicate the entire frame has been
-      allocated.  Except, don't mess with the area which
-      overlaps the previous frame's redzone. */
-   /* XXX is the following call really right?  compared with the
-      amd64-linux version, this doesn't appear to handle the redzone
-      in the same way. */
-   VG_TRACK( new_mem_stack_signal,
-             addr - VG_STACK_REDZONE_SZB, size, tid );
-   return True;
-}
-
-
 /* Create a signal frame for thread 'tid'.  Make a 3-arg frame
    regardless of whether the client originally requested a 1-arg
    version (no SA_SIGINFO) or a 3-arg one (SA_SIGINFO) since in the
@@ -131,7 +112,7 @@
                 entry to a function. */
 
    tst = VG_(get_ThreadState)(tid);
-   if (!extend(tst, esp, sp_top_of_frame - esp))
+   if (! ML_(sf_extend_stack)(tst, esp, sp_top_of_frame - esp))
       return;
 
    vg_assert(VG_IS_16_ALIGNED(esp+4));
diff --git a/coregrind/m_sigframe/sigframe-x86-linux.c b/coregrind/m_sigframe/sigframe-x86-linux.c
index 2c7c8d0..d6597b6 100644
--- a/coregrind/m_sigframe/sigframe-x86-linux.c
+++ b/coregrind/m_sigframe/sigframe-x86-linux.c
@@ -45,7 +45,7 @@
 #include "pub_core_tooliface.h"
 #include "pub_core_trampoline.h"
 #include "pub_core_sigframe.h"   /* self */
-
+#include "priv_sigframe.h"
 
 /* This module creates and removes signal frames for signal deliveries
    on x86-linux.
@@ -393,50 +393,6 @@
 }
 
 
-/* Extend the stack segment downwards if needed so as to ensure the
-   new signal frames are mapped to something.  Return a Bool
-   indicating whether or not the operation was successful.
-*/
-static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
-{
-   ThreadId        tid = tst->tid;
-   NSegment const* stackseg = NULL;
-
-   if (VG_(extend_stack)(tid, addr)) {
-      stackseg = VG_(am_find_nsegment)(addr);
-      if (0 && stackseg)
-	 VG_(printf)("frame=%#lx seg=%#lx-%#lx\n",
-		     addr, stackseg->start, stackseg->end);
-   }
-
-   if (stackseg == NULL || !stackseg->hasR || !stackseg->hasW) {
-      VG_(message)(
-         Vg_UserMsg,
-         "Can't extend stack to %#lx during signal delivery for thread %d:\n",
-         addr, tid);
-      if (stackseg == NULL)
-         VG_(message)(Vg_UserMsg, "  no stack segment\n");
-      else
-         VG_(message)(Vg_UserMsg, "  too small or bad protection modes\n");
-
-      /* set SIGSEGV to default handler */
-      VG_(set_default_handler)(VKI_SIGSEGV);
-      VG_(synth_fault_mapping)(tid, addr);
-
-      /* The whole process should be about to die, since the default
-	 action of SIGSEGV to kill the whole process. */
-      return False;
-   }
-
-   /* For tracking memory events, indicate the entire frame has been
-      allocated. */
-   VG_TRACK( new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB,
-             size + VG_STACK_REDZONE_SZB, tid );
-
-   return True;
-}
-
-
 /* Build the Valgrind-specific part of a signal frame. */
 
 static void build_vg_sigframe(struct vg_sigframe *frame,
@@ -478,7 +434,7 @@
    esp = VG_ROUNDDN(esp, 16);
    frame = (struct sigframe *)esp;
 
-   if (!extend(tst, esp, sizeof(*frame)))
+   if (! ML_(sf_extend_stack)(tst, esp, sizeof(*frame)))
       return esp_top_of_frame;
 
    /* retaddr, sigNo, siguContext fields are to be written */
@@ -535,7 +491,7 @@
    esp = VG_ROUNDDN(esp, 16);
    frame = (struct rt_sigframe *)esp;
 
-   if (!extend(tst, esp, sizeof(*frame)))
+   if (! ML_(sf_extend_stack)(tst, esp, sizeof(*frame)))
       return esp_top_of_frame;
 
    /* retaddr, sigNo, pSiginfo, puContext fields are to be written */