netbsd.h (ENABLE_EXECUTE_STACK): Remove.
authorRainer Orth <ro@CeBiTec.Uni-Bielefeld.DE>
Thu, 9 Jun 2011 12:30:46 +0000 (12:30 +0000)
committerRainer Orth <ro@gcc.gnu.org>
Thu, 9 Jun 2011 12:30:46 +0000 (12:30 +0000)
2011-05-29  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
    Joseph Myers  <joseph@codesourcery.com>

gcc:
* config/alpha/netbsd.h (ENABLE_EXECUTE_STACK): Remove.
(HAVE_ENABLE_EXECUTE_STACK): Define.
* config/alpha/osf5.h (ENABLE_EXECUTE_STACK): Remove.
(HAVE_ENABLE_EXECUTE_STACK): Define.
* config/darwin.h (ENABLE_EXECUTE_STACK): Remove.
(HAVE_ENABLE_EXECUTE_STACK): Define.
* config/i386/mingw32.h (MINGW_ENABLE_EXECUTE_STACK): Remove.
(ENABLE_EXECUTE_STACK): Remove.
(HAVE_ENABLE_EXECUTE_STACK): Define.
[IN_LIBGCC2]: Don't include <windows.h>.
* config/i386/netbsd-elf.h (ENABLE_EXECUTE_STACK): Remove.
(HAVE_ENABLE_EXECUTE_STACK): Define.
* config/i386/netbsd64.h (ENABLE_EXECUTE_STACK): Remove.
(HAVE_ENABLE_EXECUTE_STACK): Define.
* config/netbsd.h (NETBSD_ENABLE_EXECUTE_STACK): Remove.
* config/openbsd.h (ENABLE_EXECUTE_STACK): Remove.
(HAVE_ENABLE_EXECUTE_STACK): Define.
* config/sol2.h (ENABLE_EXECUTE_STACK): Remove.
(HAVE_ENABLE_EXECUTE_STACK): Define.
* config/sparc/freebsd.h (ENABLE_EXECUTE_STACK): Remove.
(HAVE_ENABLE_EXECUTE_STACK): Define.
* config/sparc/netbsd-elf.h (ENABLE_EXECUTE_STACK): Remove.
(HAVE_ENABLE_EXECUTE_STACK): Define.
* config/alpha/alpha.c (alpha_trampoline_init): Test
HAVE_ENABLE_EXECUTE_STACK.
* config/i386/i386.c (ix86_trampoline_init): Likewise.
* config/sparc/sparc.c (sparc32_initialize_trampoline): Likewise.
(sparc64_initialize_trampoline): Likewise.
* libgcc2.c [L_enable_execute_stack]: Remove.
* system.h (ENABLE_EXECUTE_STACK): Poison.
* doc/tm.texi.in (Trampolines, ENABLE_EXECUTE_STACK): Remove.
* doc/tm.texi: Regenerate.
* Makefile.in (LIBGCC2_CFLAGS): Add -fbuilding-libgcc.

gcc/c-family:
* c.opt (fbuilding-libgcc): New option.
* c-cppbuiltin.c (c_cpp_builtins): Define
__LIBGCC_TRAMPOLINE_SIZE__ if flag_building_libgcc.

libgcc:
* enable-execute-stack-empty.c: New file.
* enable-execute-stack-mprotect.c: New file.
* config/i386/enable-execute-stack-mingw32.c: New file.
* config.host (enable_execute_stack): New variable.
Select appropriate variants.
* configure.ac: Link enable-execute-stack.c to
$enable_execute_stack.
* configure: Regenerate.
* Makefile.in (LIB2ADD): Add enable-execute-stack.c.
(lib2funcs): Remove _enable_execute_stack.

Co-Authored-By: Joseph Myers <joseph@codesourcery.com>
From-SVN: r174843

31 files changed:
gcc/ChangeLog
gcc/Makefile.in
gcc/c-family/ChangeLog
gcc/c-family/c-cppbuiltin.c
gcc/c-family/c.opt
gcc/config/alpha/alpha.c
gcc/config/alpha/netbsd.h
gcc/config/alpha/osf5.h
gcc/config/darwin.h
gcc/config/i386/i386.c
gcc/config/i386/mingw32.h
gcc/config/i386/netbsd-elf.h
gcc/config/i386/netbsd64.h
gcc/config/netbsd.h
gcc/config/openbsd.h
gcc/config/sol2.h
gcc/config/sparc/freebsd.h
gcc/config/sparc/netbsd-elf.h
gcc/config/sparc/sparc.c
gcc/doc/tm.texi
gcc/doc/tm.texi.in
gcc/libgcc2.c
gcc/system.h
libgcc/ChangeLog
libgcc/Makefile.in
libgcc/config.host
libgcc/config/i386/enable-execute-stack-mingw32.c [new file with mode: 0644]
libgcc/configure
libgcc/configure.ac
libgcc/enable-execute-stack-empty.c [new file with mode: 0644]
libgcc/enable-execute-stack-mprotect.c [new file with mode: 0644]

index 81aaf76d43d6bf2fa5cafac2a55a3c44d2380e41..b2251e94e6309a8f7a2594788402dc5b4462e2fe 100644 (file)
@@ -1,3 +1,39 @@
+2011-06-09  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
+
+       * config/alpha/netbsd.h (ENABLE_EXECUTE_STACK): Remove.
+       (HAVE_ENABLE_EXECUTE_STACK): Define.
+       * config/alpha/osf5.h (ENABLE_EXECUTE_STACK): Remove.
+       (HAVE_ENABLE_EXECUTE_STACK): Define.
+       * config/darwin.h (ENABLE_EXECUTE_STACK): Remove.
+       (HAVE_ENABLE_EXECUTE_STACK): Define.
+       * config/i386/mingw32.h (MINGW_ENABLE_EXECUTE_STACK): Remove.
+       (ENABLE_EXECUTE_STACK): Remove.
+       (HAVE_ENABLE_EXECUTE_STACK): Define.
+       [IN_LIBGCC2]: Don't include <windows.h>.
+       * config/i386/netbsd-elf.h (ENABLE_EXECUTE_STACK): Remove.
+       (HAVE_ENABLE_EXECUTE_STACK): Define.
+       * config/i386/netbsd64.h (ENABLE_EXECUTE_STACK): Remove.
+       (HAVE_ENABLE_EXECUTE_STACK): Define.
+       * config/netbsd.h (NETBSD_ENABLE_EXECUTE_STACK): Remove.
+       * config/openbsd.h (ENABLE_EXECUTE_STACK): Remove.
+       (HAVE_ENABLE_EXECUTE_STACK): Define.
+       * config/sol2.h (ENABLE_EXECUTE_STACK): Remove.
+       (HAVE_ENABLE_EXECUTE_STACK): Define.
+       * config/sparc/freebsd.h (ENABLE_EXECUTE_STACK): Remove.
+       (HAVE_ENABLE_EXECUTE_STACK): Define.
+       * config/sparc/netbsd-elf.h (ENABLE_EXECUTE_STACK): Remove.
+       (HAVE_ENABLE_EXECUTE_STACK): Define.
+       * config/alpha/alpha.c (alpha_trampoline_init): Test
+       HAVE_ENABLE_EXECUTE_STACK.
+       * config/i386/i386.c (ix86_trampoline_init): Likewise.
+       * config/sparc/sparc.c (sparc32_initialize_trampoline): Likewise.
+       (sparc64_initialize_trampoline): Likewise.
+       * libgcc2.c [L_enable_execute_stack]: Remove.
+       * system.h (ENABLE_EXECUTE_STACK): Poison.
+       * doc/tm.texi.in (Trampolines, ENABLE_EXECUTE_STACK): Remove.
+       * doc/tm.texi: Regenerate.
+       * Makefile.in (LIBGCC2_CFLAGS): Add -fbuilding-libgcc.
+
 2011-06-09  Jakub Jelinek  <jakub@redhat.com>
 
        PR middle-end/49308
index 496177cde633e8d1a4266ab979b5ce74dbc61b92..0d297c3e913ed3f5d97976d12999a9b888c05dcc 100644 (file)
@@ -661,7 +661,7 @@ LIBGCC2_DEBUG_CFLAGS = -g
 LIBGCC2_CFLAGS = -O2 $(LIBGCC2_INCLUDES) $(GCC_CFLAGS) $(TARGET_LIBGCC2_CFLAGS) \
                 $(LIBGCC2_DEBUG_CFLAGS) $(GTHREAD_FLAGS) \
                 -DIN_LIBGCC2 -D__GCC_FLOAT_NOT_NEEDED \
-                -fno-stack-protector \
+                -fbuilding-libgcc -fno-stack-protector \
                 $(INHIBIT_LIBC_CFLAGS)
 
 # Additional options to use when compiling libgcc2.a.
index 97913c477ff11fa846d85d495d56e8a6ec4078f4..733d77152d40a22b311ad04798f0f6618cd34870 100644 (file)
@@ -1,3 +1,10 @@
+2011-06-09  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
+           Joseph Myers  <joseph@codesourcery.com>
+
+       * c.opt (fbuilding-libgcc): New option.
+       * c-cppbuiltin.c (c_cpp_builtins): Define
+       __LIBGCC_TRAMPOLINE_SIZE__ if flag_building_libgcc.
+
 2011-06-07  Jason Merrill  <jason@redhat.com>
 
        * c-common.c (max_tinst_depth): Lower default to 900.
index 576074c7a0d721e07ea75863f96c6d8c7f0a5974..bc75e02d28a7c49d30de2f264ca89704cfd3c25d 100644 (file)
@@ -1,5 +1,5 @@
 /* Define builtin-in macros for the C family front ends.
-   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
+   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
    Free Software Foundation, Inc.
 
 This file is part of GCC.
@@ -727,6 +727,12 @@ c_cpp_builtins (cpp_reader *pfile)
       builtin_define_fixed_point_constants ("UTA", "", uta_type_node);
     }
 
+  /* For libgcc-internal use only.  */
+  if (flag_building_libgcc)
+    /* For libgcc enable-execute-stack.c.  */
+    builtin_define_with_int_value ("__LIBGCC_TRAMPOLINE_SIZE__",
+                                  TRAMPOLINE_SIZE);
+
   /* For use in assembly language.  */
   builtin_define_with_value ("__REGISTER_PREFIX__", REGISTER_PREFIX, 0);
   builtin_define_with_value ("__USER_LABEL_PREFIX__", user_label_prefix, 0);
index c4cf4d083d266a669cb7361459b0f3e2cb0f8239..00bdd93f1244e1ca2ebc2a5f88015c3419e6f25d 100644 (file)
@@ -700,6 +700,10 @@ fasm
 C ObjC C++ ObjC++ Var(flag_no_asm, 0)
 Recognize the \"asm\" keyword
 
+; Define extra predefined macros for use in libgcc.
+fbuilding-libgcc
+C ObjC C++ ObjC++ Undocumented Var(flag_building_libgcc)
+
 fbuiltin
 C ObjC C++ ObjC++ Var(flag_no_builtin, 0)
 Recognize built-in functions
index 7323329e80df3c5150dd3297d20ef74cdc2efb77..adef2c639ea5caf9cedca354bf6c0f6d2686c0c4 100644 (file)
@@ -5395,7 +5395,7 @@ alpha_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
   if (TARGET_ABI_OSF)
     {
       emit_insn (gen_imb ());
-#ifdef ENABLE_EXECUTE_STACK
+#ifdef HAVE_ENABLE_EXECUTE_STACK
       emit_library_call (init_one_libfunc ("__enable_execute_stack"),
                         LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
 #endif
index 65e899121d2e5f9884970ceadff1df9d9a351f0f..2d7158ef65e33c7c6c1e46cc8dbc2ff8e097b693 100644 (file)
@@ -73,7 +73,4 @@ along with GCC; see the file COPYING3.  If not see
   "%{Ofast|ffast-math|funsafe-math-optimizations:crtfm%O%s} \
    %(netbsd_endfile_spec)"
 
-
-/* Attempt to enable execute permissions on the stack.  */
-
-#define ENABLE_EXECUTE_STACK NETBSD_ENABLE_EXECUTE_STACK
+#define HAVE_ENABLE_EXECUTE_STACK
index 995f35d4f32bf4190819098c8e48d999d577173e..28aff06f2c8295400e5d9cbde894619e64c8ceb5 100644 (file)
@@ -165,22 +165,7 @@ along with GCC; see the file COPYING3.  If not see
 #define HAVE_STAMP_H 1
 #endif
 
-/* Attempt to turn on access permissions for the stack.  */
-
-#define ENABLE_EXECUTE_STACK                                           \
-void                                                                   \
-__enable_execute_stack (void *addr)                                    \
-{                                                                      \
-  extern int mprotect (const void *, size_t, int);                     \
-  long size = getpagesize ();                                          \
-  long mask = ~(size-1);                                               \
-  char *page = (char *) (((long) addr) & mask);                                \
-  char *end  = (char *) ((((long) (addr + TRAMPOLINE_SIZE)) & mask) + size); \
-                                                                       \
-  /* 7 is PROT_READ | PROT_WRITE | PROT_EXEC */                                \
-  if (mprotect (page, end - page, 7) < 0)                              \
-    perror ("mprotect of trampoline code");                            \
-}
+#define HAVE_ENABLE_EXECUTE_STACK
 
 /* Digital UNIX V4.0E (1091)/usr/include/sys/types.h 4.3.49.9 1997/08/14 */
 #define SIZE_TYPE      "long unsigned int"
index 4725015a138df3cdd956ca32f55b5b730992437a..41c0d744afb033ed0a629aea572374eac22bc3d8 100644 (file)
@@ -919,43 +919,7 @@ void add_framework_path (char *);
 #define TARGET_ASM_OUTPUT_ANCHOR NULL
 #define DARWIN_SECTION_ANCHORS 0
 
-/* Attempt to turn on execute permission for the stack.  This may be
-    used by TARGET_TRAMPOLINE_INIT if the target needs it (that is,
-    if the target machine can change execute permissions on a page).
-
-    There is no way to query the execute permission of the stack, so
-    we always issue the mprotect() call.
-
-    Unfortunately it is not possible to make this namespace-clean.
-
-    Also note that no errors should be emitted by this code; it is
-    considered dangerous for library calls to send messages to
-    stdout/stderr.  */
-
-#define ENABLE_EXECUTE_STACK                                            \
-extern void __enable_execute_stack (void *);                            \
-void                                                                    \
-__enable_execute_stack (void *addr)                                     \
-{                                                                       \
-   extern int mprotect (void *, size_t, int);                           \
-   extern int getpagesize (void);                                      \
-   static int size;                                                     \
-   static long mask;                                                    \
-                                                                        \
-   char *page, *end;                                                    \
-                                                                        \
-   if (size == 0)                                                       \
-     {                                                                  \
-       size = getpagesize();                                           \
-       mask = ~((long) size - 1);                                       \
-     }                                                                  \
-                                                                        \
-   page = (char *) (((long) addr) & mask);                              \
-   end  = (char *) ((((long) (addr + (TARGET_64BIT ? 48 : 40))) & mask) + size); \
-                                                                        \
-   /* 7 == PROT_READ | PROT_WRITE | PROT_EXEC */                        \
-   (void) mprotect (page, end - page, 7);                               \
-}
+#define HAVE_ENABLE_EXECUTE_STACK
 
 /* For Apple KEXTs, we make the constructors return this to match gcc
    2.95.  */
index 0bcb2db11878a653601499bdf9a7ec3a71a365a1..7b266b93f838b51374aed68b67f01f1a7fe18135 100644 (file)
@@ -23387,7 +23387,7 @@ ix86_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
       gcc_assert (offset <= TRAMPOLINE_SIZE);
     }
 
-#ifdef ENABLE_EXECUTE_STACK
+#ifdef HAVE_ENABLE_EXECUTE_STACK
 #ifdef CHECK_EXECUTE_STACK_ENABLED
   if (CHECK_EXECUTE_STACK_ENABLED)
 #endif
index c1c12778d7014889bc1a762e89d45f029225ff1b..3c835c686908fd1e6e1234d9437a466ade49d477 100644 (file)
@@ -219,28 +219,10 @@ do {                                                       \
 /* Let defaults.h definition of TARGET_USE_JCR_SECTION apply. */
 #undef TARGET_USE_JCR_SECTION
 
-#undef MINGW_ENABLE_EXECUTE_STACK
-#define MINGW_ENABLE_EXECUTE_STACK     \
-extern void __enable_execute_stack (void *);    \
-void         \
-__enable_execute_stack (void *addr)                                    \
-{                                                                      \
-  MEMORY_BASIC_INFORMATION b;                                          \
-  if (!VirtualQuery (addr, &b, sizeof(b)))                             \
-    abort ();                                                          \
-  VirtualProtect (b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE, \
-                 &b.Protect);                                          \
-}
-
-#undef ENABLE_EXECUTE_STACK
-#define ENABLE_EXECUTE_STACK MINGW_ENABLE_EXECUTE_STACK
+#define HAVE_ENABLE_EXECUTE_STACK
 #undef  CHECK_EXECUTE_STACK_ENABLED
 #define CHECK_EXECUTE_STACK_ENABLED flag_setstackexecutable
 
-#ifdef IN_LIBGCC2
-#include <windows.h>
-#endif
-
 /* This matches SHLIB_SONAME and SHLIB_SOVERSION in t-cygming. */
 /* This matches SHLIB_SONAME and SHLIB_SOVERSION in t-cygwin. */
 #if DWARF2_UNWIND_INFO
index 449a3a71cfff33e26ab79cd9d1039395ca3c2093..d79bbfb2c6887e660c7e7105d70ac5111afed4b9 100644 (file)
@@ -118,5 +118,4 @@ along with GCC; see the file COPYING3.  If not see
    we don't care about compatibility with older gcc versions.  */
 #define DEFAULT_PCC_STRUCT_RETURN 1
 
-/* Attempt to enable execute permissions on the stack.  */
-#define ENABLE_EXECUTE_STACK NETBSD_ENABLE_EXECUTE_STACK
+#define HAVE_ENABLE_EXECUTE_STACK
index e13d16d6c64e26b88cfc040e75263048d8a3b7c2..584074fdafddeac0b81228f740c53b36004f77d2 100644 (file)
@@ -66,5 +66,4 @@ along with GCC; see the file COPYING3.  If not see
     fprintf (FILE, "\tcall __mcount\n");                               \
 }
 
-/* Attempt to enable execute permissions on the stack.  */
-#define ENABLE_EXECUTE_STACK NETBSD_ENABLE_EXECUTE_STACK
+#define HAVE_ENABLE_EXECUTE_STACK
index ddf6567dc5311d60c07c8cf763904e1c640e5a66..e9290c266653c3db4ba0d5f12e4b409c4e0e2437 100644 (file)
@@ -1,6 +1,6 @@
 /* Base configuration file for all NetBSD targets.
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-   2007, 2009, 2010 Free Software Foundation, Inc.
+   2007, 2009, 2010, 2011 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -173,50 +173,3 @@ along with GCC; see the file COPYING3.  If not see
 
 #undef WINT_TYPE
 #define WINT_TYPE "int"
-\f
-
-/* Attempt to turn on execute permission for the stack.  This may be
-   used by TARGET_TRAMPOLINE_INIT if the target needs it (that is,
-   if the target machine can change execute permissions on a page).
-
-   There is no way to query the execute permission of the stack, so
-   we always issue the mprotect() call.
-
-   Note that we go out of our way to use namespace-non-invasive calls
-   here.  Unfortunately, there is no libc-internal name for mprotect().
-
-   Also note that no errors should be emitted by this code; it is considered
-   dangerous for library calls to send messages to stdout/stderr.  */
-
-#define NETBSD_ENABLE_EXECUTE_STACK                                    \
-extern void __enable_execute_stack (void *);                           \
-void                                                                   \
-__enable_execute_stack (void *addr)                                    \
-{                                                                      \
-  extern int mprotect (void *, size_t, int);                           \
-  extern int __sysctl (int *, unsigned int, void *, size_t *,          \
-                      void *, size_t);                                 \
-                                                                       \
-  static int size;                                                     \
-  static long mask;                                                    \
-                                                                       \
-  char *page, *end;                                                    \
-                                                                       \
-  if (size == 0)                                                       \
-    {                                                                  \
-      int mib[2];                                                      \
-      size_t len;                                                      \
-                                                                       \
-      mib[0] = 6; /* CTL_HW */                                         \
-      mib[1] = 7; /* HW_PAGESIZE */                                    \
-      len = sizeof (size);                                             \
-      (void) __sysctl (mib, 2, &size, &len, NULL, 0);                  \
-      mask = ~((long) size - 1);                                       \
-    }                                                                  \
-                                                                       \
-  page = (char *) (((long) addr) & mask);                              \
-  end  = (char *) ((((long) (addr + TRAMPOLINE_SIZE)) & mask) + size); \
-                                                                       \
-  /* 7 == PROT_READ | PROT_WRITE | PROT_EXEC */                                \
-  (void) mprotect (page, end - page, 7);                               \
-}
index ae08ed8b5e161c0235e5c0aa3420ad943c55ed3d..aa279ed4738d55221d1e365fe0537f6f3de5701b 100644 (file)
@@ -281,20 +281,4 @@ do {                                                                        \
 /* Storage layout.  */
 
 \f
-/* Stack is explicitly denied execution rights on OpenBSD platforms.  */
-#define ENABLE_EXECUTE_STACK                                           \
-extern void __enable_execute_stack (void *);                           \
-void                                                                   \
-__enable_execute_stack (void *addr)                                    \
-{                                                                      \
-  long size = getpagesize ();                                          \
-  long mask = ~(size-1);                                               \
-  char *page = (char *) (((long) addr) & mask);                        \
-  char *end  = (char *) ((((long) (addr + TRAMPOLINE_SIZE)) & mask) + size); \
-                                                                     \
-  if (mprotect (page, end - page, PROT_READ | PROT_WRITE | PROT_EXEC) < 0) \
-    perror ("mprotect of trampoline code");                            \
-}
-
-#include <sys/types.h>
-#include <sys/mman.h>
+#define HAVE_ENABLE_EXECUTE_STACK
index 3d539d7ec5b3d3218d9bc7016924e903fe0bbe44..d1e98e4f5f2fbad35c4782cf8ff29a43163c7499 100644 (file)
@@ -207,52 +207,7 @@ along with GCC; see the file COPYING3.  If not see
 
 #define STDC_0_IN_SYSTEM_HEADERS 1
 \f
-/*
- * Attempt to turn on access permissions for the stack.
- *
- * _SC_STACK_PROT is only defined for post 2.6, but we want this code
- * to run always.  2.6 can change the stack protection but has no way to
- * query it.
- *
- */
-
-/* sys/mman.h is not present on some non-Solaris configurations
-   that use sol2.h, so ENABLE_EXECUTE_STACK must use a magic
-   number instead of the appropriate PROT_* flags.  */
-
-#define ENABLE_EXECUTE_STACK                                   \
-                                                                       \
-/* #define STACK_PROT_RWX (PROT_READ | PROT_WRITE | PROT_EXEC) */      \
-                                                                       \
-static int need_enable_exec_stack;                                     \
-                                                                       \
-static void check_enabling(void) __attribute__ ((constructor));                \
-static void check_enabling(void)                                       \
-{                                                                      \
-  extern long sysconf(int);                                            \
-                                                                       \
-  int prot = (int) sysconf(515 /* _SC_STACK_PROT */);                  \
-  if (prot != 7 /* STACK_PROT_RWX */)                                  \
-    need_enable_exec_stack = 1;                                                \
-}                                                                      \
-                                                                       \
-extern void __enable_execute_stack (void *);                           \
-void                                                                   \
-__enable_execute_stack (void *addr)                                    \
-{                                                                      \
-  extern int mprotect(void *, size_t, int);                            \
-  if (!need_enable_exec_stack)                                         \
-    return;                                                            \
-  else {                                                               \
-    long size = getpagesize ();                                                \
-    long mask = ~(size-1);                                             \
-    char *page = (char *) (((long) addr) & mask);                      \
-    char *end  = (char *) ((((long) (addr + TRAMPOLINE_SIZE)) & mask) + size); \
-                                                                       \
-    if (mprotect (page, end - page, 7 /* STACK_PROT_RWX */) < 0)       \
-      perror ("mprotect of trampoline code");                          \
-  }                                                                    \
-}
+#define HAVE_ENABLE_EXECUTE_STACK
 
 /* Support Solaris-specific format checking for cmn_err.  */
 #define TARGET_N_FORMAT_TYPES 1
index ed83eed1228b7b41b826901a5c0f9c7dcbe85e77..3444c0ea0055a1ae8796ad223313aa88337d5916 100644 (file)
@@ -98,31 +98,7 @@ along with GCC; see the file COPYING3.  If not see
 #undef  SPARC_DEFAULT_CMODEL
 #define SPARC_DEFAULT_CMODEL   CM_MEDLOW
 
-#define ENABLE_EXECUTE_STACK                                           \
-  static int need_enable_exec_stack;                                   \
-  static void check_enabling(void) __attribute__ ((constructor));      \
-  static void check_enabling(void)                                     \
-  {                                                                    \
-    extern int sysctlbyname(const char *, void *, size_t *, void *, size_t);\
-    int prot = 0;                                                      \
-    size_t len = sizeof(prot);                                         \
-                                                                       \
-    sysctlbyname ("kern.stackprot", &prot, &len, NULL, 0);             \
-    if (prot != 7)                                                     \
-      need_enable_exec_stack = 1;                                      \
-  }                                                                    \
-  extern void __enable_execute_stack (void *);                         \
-  void __enable_execute_stack (void *addr)                             \
-  {                                                                    \
-    if (!need_enable_exec_stack)                                       \
-      return;                                                          \
-    else {                                                             \
-      /* 7 is PROT_READ | PROT_WRITE | PROT_EXEC */                    \
-      if (mprotect (addr, TRAMPOLINE_SIZE, 7) < 0)                     \
-        perror ("mprotect of trampoline code");                                \
-    }                                                                  \
-  }
-
+#define HAVE_ENABLE_EXECUTE_STACK
 
 /************************[  Assembler stuff  ]********************************/
 
index ab161a4f8ed9ceaea4aee9d8b36ad858751cd688..d19615bc4f133d3b3b89d006a51976ed54f52812 100644 (file)
@@ -74,8 +74,7 @@ along with GCC; see the file COPYING3.  If not see
 
 #undef STDC_0_IN_SYSTEM_HEADERS
 
-/* Attempt to enable execute permissions on the stack.  */
-#define ENABLE_EXECUTE_STACK NETBSD_ENABLE_EXECUTE_STACK
+#define HAVE_ENABLE_EXECUTE_STACK
 
 /* Below here exists the merged NetBSD/sparc & NetBSD/sparc64 compiler
    description, allowing one to build 32-bit or 64-bit applications
index 59738af89ac5e14756a5144b633dc666d14ad669..ae35cf826009d4101ec08c5bff10f0775f81cace 100644 (file)
@@ -8004,7 +8004,7 @@ sparc32_initialize_trampoline (rtx m_tramp, rtx fnaddr, rtx cxt)
 
   /* Call __enable_execute_stack after writing onto the stack to make sure
      the stack address is accessible.  */
-#ifdef ENABLE_EXECUTE_STACK
+#ifdef HAVE_ENABLE_EXECUTE_STACK
   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
                      LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
 #endif
@@ -8047,7 +8047,7 @@ sparc64_initialize_trampoline (rtx m_tramp, rtx fnaddr, rtx cxt)
 
   /* Call __enable_execute_stack after writing onto the stack to make sure
      the stack address is accessible.  */
-#ifdef ENABLE_EXECUTE_STACK
+#ifdef HAVE_ENABLE_EXECUTE_STACK
   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
                      LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
 #endif
index e248b817695e9dc35276a5ef4bec9c3b8ea8fbda..ebc876f14566036fe7599fa47b31fe047ff05b7d 100644 (file)
@@ -5189,19 +5189,6 @@ typically be a series of @code{asm} statements.  Both @var{beg} and
 @var{end} are both pointer expressions.
 @end defmac
 
-The operating system may also require the stack to be made executable
-before calling the trampoline.  To implement this requirement, define
-the following macro.
-
-@defmac ENABLE_EXECUTE_STACK
-Define this macro if certain operations must be performed before executing
-code located on the stack.  The macro should expand to a series of C
-file-scope constructs (e.g.@: functions) and provide a unique entry point
-named @code{__enable_execute_stack}.  The target is responsible for
-emitting calls to the entry point in the code, for example from the
-@code{TARGET_TRAMPOLINE_INIT} hook.
-@end defmac
-
 To use a standard subroutine, define the following macro.  In addition,
 you must make sure that the instructions in a trampoline fill an entire
 cache line with identical instructions, or else ensure that the
index 528ac1366167f3935166aaed515ede534f412815..85fcfb1cdb813908ef43c504120fb29087e59f86 100644 (file)
@@ -5141,19 +5141,6 @@ typically be a series of @code{asm} statements.  Both @var{beg} and
 @var{end} are both pointer expressions.
 @end defmac
 
-The operating system may also require the stack to be made executable
-before calling the trampoline.  To implement this requirement, define
-the following macro.
-
-@defmac ENABLE_EXECUTE_STACK
-Define this macro if certain operations must be performed before executing
-code located on the stack.  The macro should expand to a series of C
-file-scope constructs (e.g.@: functions) and provide a unique entry point
-named @code{__enable_execute_stack}.  The target is responsible for
-emitting calls to the entry point in the code, for example from the
-@code{TARGET_TRAMPOLINE_INIT} hook.
-@end defmac
-
 To use a standard subroutine, define the following macro.  In addition,
 you must make sure that the instructions in a trampoline fill an entire
 cache line with identical instructions, or else ensure that the
index 5312a10b11017653bd3376f32d1480a8c033619f..94763d658f419b68d0269b2c6a949ca213b352c5 100644 (file)
@@ -1,7 +1,7 @@
 /* More subroutines needed by GCC output code on some machines.  */
 /* Compile this one with gcc.  */
 /* Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-   2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
+   2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
    Free Software Foundation, Inc.
 
 This file is part of GCC.
@@ -2027,19 +2027,6 @@ __clear_cache (char *beg __attribute__((__unused__)),
 
 #endif /* L_clear_cache */
 \f
-#ifdef L_enable_execute_stack
-/* Attempt to turn on execute permission for the stack.  */
-
-#ifdef ENABLE_EXECUTE_STACK
-  ENABLE_EXECUTE_STACK
-#else
-void
-__enable_execute_stack (void *addr __attribute__((__unused__)))
-{}
-#endif /* ENABLE_EXECUTE_STACK */
-
-#endif /* L_enable_execute_stack */
-\f
 #ifdef L_trampoline
 
 /* Jump to a trampoline, loading the static chain address.  */
index d08166135fa11a114f07f4eef2be3f8ffc9bd36a..baf5e2d236c05278833fce7e7933d7a78f82bb7e 100644 (file)
@@ -764,7 +764,7 @@ extern void fancy_abort (const char *, int, const char *) ATTRIBUTE_NORETURN;
 /* Target macros only used for code built for the target, that have
    moved to libgcc-tm.h or have never been present elsewhere.  */
  #pragma GCC poison DECLARE_LIBRARY_RENAMES LIBGCC2_GNU_PREFIX         \
-       MD_UNWIND_SUPPORT
+       MD_UNWIND_SUPPORT ENABLE_EXECUTE_STACK
 
 /* Other obsolete target macros, or macros that used to be in target
    headers and were not used, and may be obsolete or may never have
index c84bb05f32b50e0508554bbd82cb602d310eac47..75e7ef0d2118997d606c8b7a8f1126b21da116d4 100644 (file)
@@ -1,3 +1,16 @@
+2011-06-09  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
+
+       * enable-execute-stack-empty.c: New file.
+       * enable-execute-stack-mprotect.c: New file.
+       * config/i386/enable-execute-stack-mingw32.c: New file.
+       * config.host (enable_execute_stack): New variable.
+       Select appropriate variants.
+       * configure.ac: Link enable-execute-stack.c to
+       $enable_execute_stack.
+       * configure: Regenerate.
+       * Makefile.in (LIB2ADD): Add enable-execute-stack.c.
+       (lib2funcs): Remove _enable_execute_stack.
+
 2011-06-09  David S. Miller  <davem@davemloft.net>
            Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
 
index 4a075ff8f291ad7f6d410824ca333a11f8d89bab..383a6614b1c0485c55cb3ddc1dde468389b29578 100644 (file)
@@ -313,9 +313,11 @@ ifneq ($(GCC_EXTRA_PARTS),)
 endif
 endif
 
+LIB2ADD += enable-execute-stack.c
+
 # Library members defined in libgcc2.c.
 lib2funcs = _muldi3 _negdi2 _lshrdi3 _ashldi3 _ashrdi3 _cmpdi2 _ucmpdi2           \
-           _clear_cache _enable_execute_stack _trampoline __main _absvsi2 \
+           _clear_cache _trampoline __main _absvsi2 \
            _absvdi2 _addvsi3 _addvdi3 _subvsi3 _subvdi3 _mulvsi3 _mulvdi3 \
            _negvsi2 _negvdi2 _ctors _ffssi2 _ffsdi2 _clz _clzsi2 _clzdi2  \
            _ctzsi2 _ctzdi2 _popcount_tab _popcountsi2 _popcountdi2        \
index 38b0eb3726dce56fa2b04ee9def5614966b3319e..c2b5dbca75c0a875403ece0166e30ea8a6f1b560 100644 (file)
@@ -44,6 +44,8 @@
 #                      The default is ".hidden".
 #  cpu_type            The name of the cpu, if different from the first
 #                      chunk of the canonical host name.
+#  enable_execute_stack The name of a source file implementing
+#                      __enable_execute_stack.
 #  extra_parts         List of extra object files that should be compiled
 #                      for this target machine.  This may be overridden
 #                      by setting EXTRA_PARTS in a tmake_file fragment.
@@ -57,6 +59,7 @@
 #                      "$cpu_type/t-$cpu_type".
 
 asm_hidden_op=.hidden
+enable_execute_stack=
 extra_parts=
 tmake_file=
 md_unwind_header=no-unwind.h
@@ -202,6 +205,19 @@ case ${host} in
   ;;
 esac
 
+case ${host} in
+*-*-darwin* | *-*-freebsd* | *-*-netbsd* | *-*-openbsd* | *-*-solaris2* | \
+  alpha*-dec-osf5.1*)
+  enable_execute_stack=enable-execute-stack-mprotect.c
+  ;;
+i[34567]86-*-mingw* | x86_64-*-mingw*)
+  enable_execute_stack=config/i386/enable-execute-stack-mingw32.c
+  ;;
+*)
+  enable_execute_stack=enable-execute-stack-empty.c;
+  ;;
+esac
+
 case ${host} in
 # Support site-specific machine types.
 *local*)
diff --git a/libgcc/config/i386/enable-execute-stack-mingw32.c b/libgcc/config/i386/enable-execute-stack-mingw32.c
new file mode 100644 (file)
index 0000000..8f3d272
--- /dev/null
@@ -0,0 +1,38 @@
+/* Implement __enable_execute_stack for Windows32.
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This file is part of GCC.
+
+   GCC 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 3, or (at your option) any later
+   version.
+
+   GCC 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.
+
+   Under Section 7 of GPL version 3, you are granted additional
+   permissions described in the GCC Runtime Library Exception, version
+   3.1, as published by the Free Software Foundation.
+
+   You should have received a copy of the GNU General Public License and
+   a copy of the GCC Runtime Library Exception along with this program;
+   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <windows.h>
+
+extern void __enable_execute_stack (void *);
+
+void
+__enable_execute_stack (void *addr)
+{
+  MEMORY_BASIC_INFORMATION b;
+
+  if (!VirtualQuery (addr, &b, sizeof(b)))
+    abort ();
+  VirtualProtect (b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE,
+                 &b.Protect);
+}
index 1a3a0bde2580d1aab194ae02b44d05a7fcf8c11f..1351022494432d206b4a7999fedd699f418e3ee5 100644 (file)
@@ -3962,6 +3962,8 @@ tmake_file="${tmake_file_}"
 
 
 
+ac_config_links="$ac_config_links enable-execute-stack.c:$enable_execute_stack"
+
 ac_config_links="$ac_config_links md-unwind-support.h:config/$md_unwind_header"
 
 
@@ -4689,6 +4691,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 for ac_config_target in $ac_config_targets
 do
   case $ac_config_target in
+    "enable-execute-stack.c") CONFIG_LINKS="$CONFIG_LINKS enable-execute-stack.c:$enable_execute_stack" ;;
     "md-unwind-support.h") CONFIG_LINKS="$CONFIG_LINKS md-unwind-support.h:config/$md_unwind_header" ;;
     "Makefile") CONFIG_FILES="$CONFIG_FILES Makefile" ;;
     "default") CONFIG_COMMANDS="$CONFIG_COMMANDS default" ;;
index f2bcabf4872a318b95cd1646aabd55d81498555d..4f56c3b596f00cb6d876e92d9a72c7cbf457791d 100644 (file)
@@ -278,6 +278,7 @@ AC_SUBST(tmake_file)
 AC_SUBST(cpu_type)
 AC_SUBST(extra_parts)
 AC_SUBST(asm_hidden_op)
+AC_CONFIG_LINKS([enable-execute-stack.c:$enable_execute_stack])
 AC_CONFIG_LINKS([md-unwind-support.h:config/$md_unwind_header])
 
 # We need multilib support.
diff --git a/libgcc/enable-execute-stack-empty.c b/libgcc/enable-execute-stack-empty.c
new file mode 100644 (file)
index 0000000..81786de
--- /dev/null
@@ -0,0 +1,7 @@
+/* Dummy implementation of __enable_execute_stack.  */
+
+/* Attempt to turn on execute permission for the stack.  */
+void
+__enable_execute_stack (void *addr __attribute__((__unused__)))
+{
+}
diff --git a/libgcc/enable-execute-stack-mprotect.c b/libgcc/enable-execute-stack-mprotect.c
new file mode 100644 (file)
index 0000000..1ad77bd
--- /dev/null
@@ -0,0 +1,119 @@
+/* Implement __enable_execute_stack using mprotect(2).
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This file is part of GCC.
+
+   GCC 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 3, or (at your option) any later
+   version.
+
+   GCC 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.
+
+   Under Section 7 of GPL version 3, you are granted additional
+   permissions described in the GCC Runtime Library Exception, version
+   3.1, as published by the Free Software Foundation.
+
+   You should have received a copy of the GNU General Public License and
+   a copy of the GCC Runtime Library Exception along with this program;
+   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <sys/mman.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#define STACK_PROT_RWX (PROT_READ | PROT_WRITE | PROT_EXEC)
+
+static int need_enable_exec_stack;
+
+static void check_enabling (void) __attribute__ ((unused));
+extern void __enable_execute_stack (void *);
+
+#if defined __FreeBSD__
+#include <sys/sysctl.h>
+
+static void __attribute__ ((constructor))
+check_enabling (void)
+{
+  int prot = 0;
+  size_t len = sizeof (prot);
+
+  sysctlbyname ("kern.stackprot", &prot, &len, NULL, 0);
+  if (prot != STACK_PROT_RWX)
+    need_enable_exec_stack = 1;
+}
+#elif defined __sun__ && defined __svr4__
+static void __attribute__ ((constructor))
+check_enabling (void)
+{
+  int prot = (int) sysconf (_SC_STACK_PROT);
+
+  if (prot != STACK_PROT_RWX)
+    need_enable_exec_stack = 1;
+}
+#else
+/* There is no way to query the execute permission of the stack, so
+   we always issue the mprotect() call.  */
+
+static int need_enable_exec_stack = 1;
+#endif
+
+#if defined __NetBSD__
+/* Note that we go out of our way to use namespace-non-invasive calls
+   here.  Unfortunately, there is no libc-internal name for mprotect().  */
+
+#include <sys/sysctl.h>
+
+extern int __sysctl (int *, unsigned int, void *, size_t *, void *, size_t);
+
+static int
+getpagesize (void)
+{
+  static int size;
+
+  if (size == 0)
+    {
+      int mib[2];
+      size_t len;
+
+      mib[0] = CTL_HW;
+      mib[1] = HW_PAGESIZE;
+      len = sizeof (size);
+      (void) __sysctl (mib, 2, &size, &len, NULL, 0);
+    }
+  return size;
+}
+#endif /* __NetBSD__ */
+
+/* Attempt to turn on access permissions for the stack.  Unfortunately it
+   is not possible to make this namespace-clean.*/
+
+void
+__enable_execute_stack (void *addr)
+{
+  if (!need_enable_exec_stack)
+    return;
+  else
+    {
+      static long size, mask;
+
+      if (size == 0) {
+       size = getpagesize ();
+       mask = ~(size - 1);
+      }
+
+      char *page = (char *) (((long) addr) & mask);
+      char *end  = (char *)
+       ((((long) (addr + __LIBGCC_TRAMPOLINE_SIZE__)) & mask) + size);
+
+      if (mprotect (page, end - page, STACK_PROT_RWX) < 0)
+       /* Note that no errors should be emitted by this code; it is
+          considered dangerous for library calls to send messages to
+          stdout/stderr.  */
+       abort ();
+    }
+}