OSDN Git Service

gcc/:
authorian <ian@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 27 Sep 2010 19:48:20 +0000 (19:48 +0000)
committerian <ian@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 27 Sep 2010 19:48:20 +0000 (19:48 +0000)
* common.opt (fsplit-stack): New option.
* opts.c (decode_options): Set flag_split_stack to final value.
* target.def (supports_split_stack): New hook.
* gcc.c (STACK_SPLIT_SPEC): Define.
(LINK_COMMAND_SPEC): Use STACK_SPLIT_SPEC.
* doc/invoke.texi (Option Summary): Mention -fsplit-stack.
(Code Gen Options): Document -fsplit-stack.
* doc/extend.texi (Function Attributes): Mention no_split_stack.
(Function Attributes): Document no_split_stack.
* doc/tm.texi.in (Stack Smashing Protection): Add @hook
TARGET_SUPPORTS_SPLIT_STACK.
* doc/tm.texi: Rebuild.
* function.c (thread_prologue_and_epilogue_insns): If
flag_split_stack, add split stack prologue.
* explow.c (allocate_dynamic_stack_space): Support -fsplit-stack.
* varasm.c (saw_no_split_stack): New static variable.
(assemble_start_function): Set saw_no_split_stack if the function
has the no_split_stack attribute.
(file_end_indicate_split_stack): New function.
* output.h (file_end_indicate_split_stack): Declare.
* libgcc-std.ver (GCC_4.6.0): Add -fsplit-stack support variables
and function.
* doc/libgcc.texi (Miscellaneous routines): Document -fsplit-stack
routines.
* config/i386/i386.c (ix86_option_override_internal): Don't set
expand_builtin_va_start to NULL if -fsplit-stack.
(ix86_function_regparm): Reduce local regparm by 1 for 32-bit
-fsplit-stack.
(ix86_va_start): If -fsplit-stack, get overflow pointer from
scratch register set by prologue.
(ix86_code_end): If -fsplit-stack, call
file_end_indicate_split_stack.
(ix86_supports_split_stack): New static function.
(SPLIT_STACK_AVAILABLE): Define.
(split_stack_prologue_scratch_regno): New static function.
(split_stack_fn): New static variable.
(ix86_expand_split_stack_prologue): New function.
(ix86_live_on_entry): New static function.
(ix86_legitimate_address_p): Handle UNSPEC_STACK_CHECK.
(output_pic_addr_const): Likewise.
(i386_asm_output_addr_const_extra): Likewise.
(ix86_expand_call): Change return type to rtx.  Return the new
call instruction.
(TARGET_SUPPORTS_SPLIT_STACK): Define.
(TARGET_EXTRA_LIVE_ON_ENTRY): Define.
* config/i386/i386.md (UNSPEC_STACK_CHECK): Define.
(split_stack_prologue, split_stack_return): New insns.
(split_stack_space_check): New insn.
* config/i386/i386.h (struct machine_function): Add
split_stack_varargs_pointer field.
* config/i386/linux.h (TARGET_CAN_SPLIT_STACK): Define.
(TARGET_THREAD_SPLIT_STACK_OFFSET): Define.
* config/i386/linux64.h (TARGET_CAN_SPLIT_STACK): Define.
(TARGET_THREAD_SPLIT_STACK_OFFSET): Define.
* config/i386/i386-protos.h (ix86_expand_split_stack_prologue):
Declare.
(ix86_expand_call): Update declaration.
gcc/c-family/:
* c-common.c (c_common_attribute_table): Add no_split_stack.
(handle_no_split_stack_attribute): New static function.
gcc/testsuite/:
* lib/target-supports.exp (check_effective_target_split_stack):
New procedure.
* gcc.dg/split-1.c: New test.
* gcc.dg/split-2.c: New test.
* gcc.dg/split-3.c: New test.
* gcc.dg/split-4.c: New test.
libgcc/:
* generic-morestack.h: New file.
* generic-morestack.c: New file.
* generic-morestack-thread.c: New file.
* config/i386/morestack.S: New file.
* config/t-stack: New file.
* config/i386/t-stack-i386: New file.
* config.host (i[34567]86-*-linux* and friends): Add t-stack and
i386/t-stack-i386 to tmake_file.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@164661 138bc75d-0d04-0410-961f-82ee72b054a4

37 files changed:
gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c-common.c
gcc/common.opt
gcc/config/i386/i386-protos.h
gcc/config/i386/i386.c
gcc/config/i386/i386.h
gcc/config/i386/i386.md
gcc/config/i386/linux.h
gcc/config/i386/linux64.h
gcc/doc/extend.texi
gcc/doc/invoke.texi
gcc/doc/libgcc.texi
gcc/doc/tm.texi
gcc/doc/tm.texi.in
gcc/explow.c
gcc/function.c
gcc/gcc.c
gcc/libgcc-std.ver
gcc/opts.c
gcc/output.h
gcc/target.def
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/split-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/split-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/split-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/split-4.c [new file with mode: 0644]
gcc/testsuite/lib/target-supports.exp
gcc/varasm.c
libgcc/ChangeLog
libgcc/config.host
libgcc/config/i386/morestack.S [new file with mode: 0644]
libgcc/config/i386/t-stack-i386 [new file with mode: 0644]
libgcc/config/t-stack [new file with mode: 0644]
libgcc/generic-morestack-thread.c [new file with mode: 0644]
libgcc/generic-morestack.c [new file with mode: 0644]
libgcc/generic-morestack.h [new file with mode: 0644]

index 68dd7e9..1970bd7 100644 (file)
@@ -1,3 +1,63 @@
+2010-09-27  Ian Lance Taylor  <iant@google.com>
+
+       * common.opt (fsplit-stack): New option.
+       * opts.c (decode_options): Set flag_split_stack to final value.
+       * target.def (supports_split_stack): New hook.
+       * gcc.c (STACK_SPLIT_SPEC): Define.
+       (LINK_COMMAND_SPEC): Use STACK_SPLIT_SPEC.
+       * doc/invoke.texi (Option Summary): Mention -fsplit-stack.
+       (Code Gen Options): Document -fsplit-stack.
+       * doc/extend.texi (Function Attributes): Mention no_split_stack.
+       (Function Attributes): Document no_split_stack.
+       * doc/tm.texi.in (Stack Smashing Protection): Add @hook
+       TARGET_SUPPORTS_SPLIT_STACK.
+       * doc/tm.texi: Rebuild.
+       * function.c (thread_prologue_and_epilogue_insns): If
+       flag_split_stack, add split stack prologue.
+       * explow.c (allocate_dynamic_stack_space): Support -fsplit-stack.
+       * varasm.c (saw_no_split_stack): New static variable.
+       (assemble_start_function): Set saw_no_split_stack if the function
+       has the no_split_stack attribute.
+       (file_end_indicate_split_stack): New function.
+       * output.h (file_end_indicate_split_stack): Declare.
+       * libgcc-std.ver (GCC_4.6.0): Add -fsplit-stack support variables
+       and function.
+       * doc/libgcc.texi (Miscellaneous routines): Document -fsplit-stack
+       routines.
+       * config/i386/i386.c (ix86_option_override_internal): Don't set
+       expand_builtin_va_start to NULL if -fsplit-stack.
+       (ix86_function_regparm): Reduce local regparm by 1 for 32-bit
+       -fsplit-stack.
+       (ix86_va_start): If -fsplit-stack, get overflow pointer from
+       scratch register set by prologue.
+       (ix86_code_end): If -fsplit-stack, call
+       file_end_indicate_split_stack.
+       (ix86_supports_split_stack): New static function.
+       (SPLIT_STACK_AVAILABLE): Define.
+       (split_stack_prologue_scratch_regno): New static function.
+       (split_stack_fn): New static variable.
+       (ix86_expand_split_stack_prologue): New function.
+       (ix86_live_on_entry): New static function.
+       (ix86_legitimate_address_p): Handle UNSPEC_STACK_CHECK.
+       (output_pic_addr_const): Likewise.
+       (i386_asm_output_addr_const_extra): Likewise.
+       (ix86_expand_call): Change return type to rtx.  Return the new
+       call instruction.
+       (TARGET_SUPPORTS_SPLIT_STACK): Define.
+       (TARGET_EXTRA_LIVE_ON_ENTRY): Define.
+       * config/i386/i386.md (UNSPEC_STACK_CHECK): Define.
+       (split_stack_prologue, split_stack_return): New insns.
+       (split_stack_space_check): New insn.
+       * config/i386/i386.h (struct machine_function): Add
+       split_stack_varargs_pointer field.
+       * config/i386/linux.h (TARGET_CAN_SPLIT_STACK): Define.
+       (TARGET_THREAD_SPLIT_STACK_OFFSET): Define.
+       * config/i386/linux64.h (TARGET_CAN_SPLIT_STACK): Define.
+       (TARGET_THREAD_SPLIT_STACK_OFFSET): Define.
+       * config/i386/i386-protos.h (ix86_expand_split_stack_prologue):
+       Declare.
+       (ix86_expand_call): Update declaration.
+
 2010-09-27  Nicola Pero  <nicola.pero@meta-innovation.com>
 
        * doc/objc.texi (Type encoding): Added the new 'long double' (D)
 2010-09-27  Nicola Pero  <nicola.pero@meta-innovation.com>
 
        * doc/objc.texi (Type encoding): Added the new 'long double' (D)
index 25603f9..e16c871 100644 (file)
@@ -1,3 +1,8 @@
+2010-09-27  Ian Lance Taylor  <iant@google.com>
+
+       * c-common.c (c_common_attribute_table): Add no_split_stack.
+       (handle_no_split_stack_attribute): New static function.
+
 2010-09-27  Nicola Pero  <nicola.pero@meta-innovation.com>
 
        Merge from 'apple/trunk' branch on FSF servers. 
 2010-09-27  Nicola Pero  <nicola.pero@meta-innovation.com>
 
        Merge from 'apple/trunk' branch on FSF servers. 
index 6ef3bf9..63e2d70 100644 (file)
@@ -358,6 +358,7 @@ static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
+static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
 
 static void check_function_nonnull (tree, int, tree *);
 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
 
 static void check_function_nonnull (tree, int, tree *);
@@ -661,6 +662,8 @@ const struct attribute_spec c_common_attribute_table[] =
                              handle_target_attribute },
   { "optimize",               1, -1, true, false, false,
                              handle_optimize_attribute },
                              handle_target_attribute },
   { "optimize",               1, -1, true, false, false,
                              handle_optimize_attribute },
+  { "no_split_stack",        0, 0, true,  false, false,
+                             handle_no_split_stack_attribute },
   /* For internal use (marking of builtins and runtime functions) only.
      The name contains space to prevent its usage in source code.  */
   { "fn spec",               1, 1, false, true, true,
   /* For internal use (marking of builtins and runtime functions) only.
      The name contains space to prevent its usage in source code.  */
   { "fn spec",               1, 1, false, true, true,
@@ -7841,6 +7844,32 @@ handle_optimize_attribute (tree *node, tree name, tree args,
 
   return NULL_TREE;
 }
 
   return NULL_TREE;
 }
+
+/* Handle a "no_split_stack" attribute.  */
+
+static tree
+handle_no_split_stack_attribute (tree *node, tree name,
+                                tree ARG_UNUSED (args),
+                                int ARG_UNUSED (flags),
+                                bool *no_add_attrs)
+{
+  tree decl = *node;
+
+  if (TREE_CODE (decl) != FUNCTION_DECL)
+    {
+      error_at (DECL_SOURCE_LOCATION (decl),
+               "%qE attribute applies only to functions", name);
+      *no_add_attrs = true;
+    }
+  else if (DECL_INITIAL (decl))
+    {
+      error_at (DECL_SOURCE_LOCATION (decl),
+               "can%'t set %qE attribute after definition", name);
+      *no_add_attrs = true;
+    }
+
+  return NULL_TREE;
+}
 \f
 /* Check for valid arguments being passed to a function.
    ATTRS is a list of attributes.  There are NARGS arguments in the array
 \f
 /* Check for valid arguments being passed to a function.
    ATTRS is a list of attributes.  There are NARGS arguments in the array
index 6b89c32..aa17ae8 100644 (file)
@@ -1426,6 +1426,10 @@ fsplit-ivs-in-unroller
 Common Report Var(flag_split_ivs_in_unroller) Init(1) Optimization
 Split lifetimes of induction variables when loops are unrolled
 
 Common Report Var(flag_split_ivs_in_unroller) Init(1) Optimization
 Split lifetimes of induction variables when loops are unrolled
 
+fsplit-stack
+Common Report Var(flag_split_stack) Init(-1)
+Generate discontiguous stack frames
+
 fsplit-wide-types
 Common Report Var(flag_split_wide_types) Optimization
 Split wide types into independent registers
 fsplit-wide-types
 Common Report Var(flag_split_wide_types) Optimization
 Split wide types into independent registers
index fd31e99..700dec1 100644 (file)
@@ -29,6 +29,7 @@ extern void ix86_setup_frame_addresses (void);
 extern HOST_WIDE_INT ix86_initial_elimination_offset (int, int);
 extern void ix86_expand_prologue (void);
 extern void ix86_expand_epilogue (int);
 extern HOST_WIDE_INT ix86_initial_elimination_offset (int, int);
 extern void ix86_expand_prologue (void);
 extern void ix86_expand_epilogue (int);
+extern void ix86_expand_split_stack_prologue (void);
 
 extern void ix86_output_addr_vec_elt (FILE *, int);
 extern void ix86_output_addr_diff_elt (FILE *, int, int);
 
 extern void ix86_output_addr_vec_elt (FILE *, int);
 extern void ix86_output_addr_diff_elt (FILE *, int, int);
@@ -117,7 +118,7 @@ extern bool ix86_expand_int_vcond (rtx[]);
 extern void ix86_expand_sse_unpack (rtx[], bool, bool);
 extern void ix86_expand_sse4_unpack (rtx[], bool, bool);
 extern bool ix86_expand_int_addcc (rtx[]);
 extern void ix86_expand_sse_unpack (rtx[], bool, bool);
 extern void ix86_expand_sse4_unpack (rtx[], bool, bool);
 extern bool ix86_expand_int_addcc (rtx[]);
-extern void ix86_expand_call (rtx, rtx, rtx, rtx, rtx, int);
+extern rtx ix86_expand_call (rtx, rtx, rtx, rtx, rtx, int);
 extern void x86_initialize_trampoline (rtx, rtx, rtx);
 extern rtx ix86_zero_extend_to_Pmode (rtx);
 extern void ix86_split_long_move (rtx[]);
 extern void x86_initialize_trampoline (rtx, rtx, rtx);
 extern rtx ix86_zero_extend_to_Pmode (rtx);
 extern void ix86_split_long_move (rtx[]);
index 627d8d2..7d4126b 100644 (file)
@@ -1989,6 +1989,8 @@ static void ix86_add_new_builtins (int);
 static rtx ix86_expand_vec_perm_builtin (tree);
 static tree ix86_canonical_va_list_type (tree);
 static void predict_jump (int);
 static rtx ix86_expand_vec_perm_builtin (tree);
 static tree ix86_canonical_va_list_type (tree);
 static void predict_jump (int);
+static unsigned int split_stack_prologue_scratch_regno (void);
+static bool i386_asm_output_addr_const_extra (FILE *, rtx);
 
 enum ix86_function_specific_strings
 {
 
 enum ix86_function_specific_strings
 {
@@ -3649,7 +3651,7 @@ ix86_option_override_internal (bool main_args_p)
 
   /* If using typedef char *va_list, signal that __builtin_va_start (&ap, 0)
      can be optimized to ap = __builtin_next_arg (0).  */
 
   /* If using typedef char *va_list, signal that __builtin_va_start (&ap, 0)
      can be optimized to ap = __builtin_next_arg (0).  */
-  if (!TARGET_64BIT)
+  if (!TARGET_64BIT && !flag_split_stack)
     targetm.expand_builtin_va_start = NULL;
 
   if (TARGET_64BIT)
     targetm.expand_builtin_va_start = NULL;
 
   if (TARGET_64BIT)
@@ -4890,6 +4892,10 @@ ix86_function_regparm (const_tree type, const_tree decl)
          if (local_regparm == 3 && DECL_STATIC_CHAIN (decl))
            local_regparm = 2;
 
          if (local_regparm == 3 && DECL_STATIC_CHAIN (decl))
            local_regparm = 2;
 
+         /* In 32-bit mode save a register for the split stack.  */
+         if (!TARGET_64BIT && local_regparm == 3 && flag_split_stack)
+           local_regparm = 2;
+
          /* Each fixed register usage increases register pressure,
             so less registers should be used for argument passing.
             This functionality can be overriden by an explicit
          /* Each fixed register usage increases register pressure,
             so less registers should be used for argument passing.
             This functionality can be overriden by an explicit
@@ -7248,10 +7254,56 @@ ix86_va_start (tree valist, rtx nextarg)
   tree gpr, fpr, ovf, sav, t;
   tree type;
 
   tree gpr, fpr, ovf, sav, t;
   tree type;
 
+  rtx ovf_rtx;
+
+  if (flag_split_stack
+      && cfun->machine->split_stack_varargs_pointer == NULL_RTX)
+    {
+      unsigned int scratch_regno;
+
+      /* When we are splitting the stack, we can't refer to the stack
+        arguments using internal_arg_pointer, because they may be on
+        the old stack.  The split stack prologue will arrange to
+        leave a pointer to the old stack arguments in a scratch
+        register, which we here copy to a pseudo-register.  The split
+        stack prologue can't set the pseudo-register directly because
+        it (the prologue) runs before any registers have been saved.  */
+
+      scratch_regno = split_stack_prologue_scratch_regno ();
+      if (scratch_regno != INVALID_REGNUM)
+       {
+         rtx reg, seq;
+
+         reg = gen_reg_rtx (Pmode);
+         cfun->machine->split_stack_varargs_pointer = reg;
+
+         start_sequence ();
+         emit_move_insn (reg, gen_rtx_REG (Pmode, scratch_regno));
+         seq = get_insns ();
+         end_sequence ();
+
+         push_topmost_sequence ();
+         emit_insn_after (seq, entry_of_function ());
+         pop_topmost_sequence ();
+       }
+    }
+
   /* Only 64bit target needs something special.  */
   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
     {
   /* Only 64bit target needs something special.  */
   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
     {
-      std_expand_builtin_va_start (valist, nextarg);
+      if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
+       std_expand_builtin_va_start (valist, nextarg);
+      else
+       {
+         rtx va_r, next;
+
+         va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
+         next = expand_binop (ptr_mode, add_optab,
+                              cfun->machine->split_stack_varargs_pointer,
+                              crtl->args.arg_offset_rtx,
+                              NULL_RTX, 0, OPTAB_LIB_WIDEN);
+         convert_move (va_r, next, 0);
+       }
       return;
     }
 
       return;
     }
 
@@ -7297,7 +7349,11 @@ ix86_va_start (tree valist, rtx nextarg)
 
   /* Find the overflow area.  */
   type = TREE_TYPE (ovf);
 
   /* Find the overflow area.  */
   type = TREE_TYPE (ovf);
-  t = make_tree (type, crtl->args.internal_arg_pointer);
+  if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
+    ovf_rtx = crtl->args.internal_arg_pointer;
+  else
+    ovf_rtx = cfun->machine->split_stack_varargs_pointer;
+  t = make_tree (type, ovf_rtx);
   if (words != 0)
     t = build2 (POINTER_PLUS_EXPR, type, t,
                size_int (words * UNITS_PER_WORD));
   if (words != 0)
     t = build2 (POINTER_PLUS_EXPR, type, t,
                size_int (words * UNITS_PER_WORD));
@@ -8042,6 +8098,9 @@ ix86_code_end (void)
       set_cfun (NULL);
       current_function_decl = NULL;
     }
       set_cfun (NULL);
       current_function_decl = NULL;
     }
+
+  if (flag_split_stack)
+    file_end_indicate_split_stack ();
 }
 
 /* Emit code for the SET_GOT patterns.  */
 }
 
 /* Emit code for the SET_GOT patterns.  */
@@ -8344,6 +8403,29 @@ ix86_builtin_setjmp_frame_value (void)
   return stack_realign_fp ? hard_frame_pointer_rtx : virtual_stack_vars_rtx;
 }
 
   return stack_realign_fp ? hard_frame_pointer_rtx : virtual_stack_vars_rtx;
 }
 
+/* On the x86 -fsplit-stack and -fstack-protector both use the same
+   field in the TCB, so they can not be used together.  */
+
+static bool
+ix86_supports_split_stack (bool report ATTRIBUTE_UNUSED)
+{
+  bool ret = true;
+
+#ifndef TARGET_THREAD_SPLIT_STACK_OFFSET
+  if (report)
+    error ("%<-fsplit-stack%> currently only supported on GNU/Linux");
+  ret = false;
+#endif
+
+  return ret;
+}
+
+/* When using -fsplit-stack, the allocation routines set a field in
+   the TCB to the bottom of the stack plus this much space, measured
+   in bytes.  */
+
+#define SPLIT_STACK_AVAILABLE 256
+
 /* Fill structure ix86_frame about frame of currently computed function.  */
 
 static void
 /* Fill structure ix86_frame about frame of currently computed function.  */
 
 static void
@@ -10334,6 +10416,277 @@ ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
 #endif
 
 }
 #endif
 
 }
+
+/* Return a scratch register to use in the split stack prologue.  The
+   split stack prologue is used for -fsplit-stack.  It is the first
+   instructions in the function, even before the regular prologue.
+   The scratch register can be any caller-saved register which is not
+   used for parameters or for the static chain.  */
+
+static unsigned int
+split_stack_prologue_scratch_regno (void)
+{
+  if (TARGET_64BIT)
+    return R11_REG;
+  else
+    {
+      bool is_fastcall;
+      int regparm;
+
+      is_fastcall = (lookup_attribute ("fastcall",
+                                      TYPE_ATTRIBUTES (TREE_TYPE (cfun->decl)))
+                    != NULL);
+      regparm = ix86_function_regparm (TREE_TYPE (cfun->decl), cfun->decl);
+
+      if (is_fastcall)
+       {
+         if (DECL_STATIC_CHAIN (cfun->decl))
+           {
+             sorry ("-fsplit-stack does not support fastcall with "
+                    "nested function");
+             return INVALID_REGNUM;
+           }
+         return AX_REG;
+       }
+      else if (regparm < 3)
+       {
+         if (!DECL_STATIC_CHAIN (cfun->decl))
+           return CX_REG;
+         else
+           {
+             if (regparm >= 2)
+               {
+                 sorry ("-fsplit-stack does not support 2 register "
+                        " parameters for a nested function");
+                 return INVALID_REGNUM;
+               }
+             return DX_REG;
+           }
+       }
+      else
+       {
+         /* FIXME: We could make this work by pushing a register
+            around the addition and comparison.  */
+         sorry ("-fsplit-stack does not support 3 register parameters");
+         return INVALID_REGNUM;
+       }
+    }
+}
+
+/* A SYMBOL_REF for the function which allocates new stackspace for
+   -fsplit-stack.  */
+
+static GTY(()) rtx split_stack_fn;
+
+/* Handle -fsplit-stack.  These are the first instructions in the
+   function, even before the regular prologue.  */
+
+void
+ix86_expand_split_stack_prologue (void)
+{
+  struct ix86_frame frame;
+  HOST_WIDE_INT allocate;
+  int args_size;
+  rtx label, limit, current, jump_insn, allocate_rtx, call_insn, call_fusage;
+  rtx scratch_reg = NULL_RTX;
+  rtx varargs_label = NULL_RTX;
+
+  gcc_assert (flag_split_stack && reload_completed);
+
+  ix86_finalize_stack_realign_flags ();
+  ix86_compute_frame_layout (&frame);
+  allocate = frame.stack_pointer_offset - INCOMING_FRAME_SP_OFFSET;
+
+  /* This is the label we will branch to if we have enough stack
+     space.  We expect the basic block reordering pass to reverse this
+     branch if optimizing, so that we branch in the unlikely case.  */
+  label = gen_label_rtx ();
+
+  /* We need to compare the stack pointer minus the frame size with
+     the stack boundary in the TCB.  The stack boundary always gives
+     us SPLIT_STACK_AVAILABLE bytes, so if we need less than that we
+     can compare directly.  Otherwise we need to do an addition.  */
+
+  limit = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
+                         UNSPEC_STACK_CHECK);
+  limit = gen_rtx_CONST (Pmode, limit);
+  limit = gen_rtx_MEM (Pmode, limit);
+  if (allocate < SPLIT_STACK_AVAILABLE)
+    current = stack_pointer_rtx;
+  else
+    {
+      unsigned int scratch_regno;
+      rtx offset;
+
+      /* We need a scratch register to hold the stack pointer minus
+        the required frame size.  Since this is the very start of the
+        function, the scratch register can be any caller-saved
+        register which is not used for parameters.  */
+      offset = GEN_INT (- allocate);
+      scratch_regno = split_stack_prologue_scratch_regno ();
+      if (scratch_regno == INVALID_REGNUM)
+       return;
+      scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
+      if (!TARGET_64BIT || x86_64_immediate_operand (offset, Pmode))
+       {
+         /* We don't use ix86_gen_add3 in this case because it will
+            want to split to lea, but when not optimizing the insn
+            will not be split after this point.  */
+         emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
+                                 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
+                                               offset)));
+       }
+      else
+       {
+         emit_move_insn (scratch_reg, offset);
+         emit_insn (gen_adddi3 (scratch_reg, scratch_reg,
+                                stack_pointer_rtx));
+       }
+      current = scratch_reg;
+    }
+
+  ix86_expand_branch (GEU, current, limit, label);
+  jump_insn = get_last_insn ();
+  JUMP_LABEL (jump_insn) = label;
+
+  /* Mark the jump as very likely to be taken.  */
+  add_reg_note (jump_insn, REG_BR_PROB,
+               GEN_INT (REG_BR_PROB_BASE - REG_BR_PROB_BASE / 100));
+
+  /* Get more stack space.  We pass in the desired stack space and the
+     size of the arguments to copy to the new stack.  In 32-bit mode
+     we push the parameters; __morestack will return on a new stack
+     anyhow.  In 64-bit mode we pass the parameters in r10 and
+     r11.  */
+  allocate_rtx = GEN_INT (allocate);
+  args_size = crtl->args.size >= 0 ? crtl->args.size : 0;
+  call_fusage = NULL_RTX;
+  if (TARGET_64BIT)
+    {
+      rtx reg;
+
+      reg = gen_rtx_REG (Pmode, R10_REG);
+
+      /* If this function uses a static chain, it will be in %r10.
+        Preserve it across the call to __morestack.  */
+      if (DECL_STATIC_CHAIN (cfun->decl))
+       {
+         rtx rax;
+
+         rax = gen_rtx_REG (Pmode, AX_REG);
+         emit_move_insn (rax, reg);
+         use_reg (&call_fusage, rax);
+       }
+
+      emit_move_insn (reg, allocate_rtx);
+      use_reg (&call_fusage, reg);
+      reg = gen_rtx_REG (Pmode, R11_REG);
+      emit_move_insn (reg, GEN_INT (args_size));
+      use_reg (&call_fusage, reg);
+    }
+  else
+    {
+      emit_insn (gen_push (GEN_INT (args_size)));
+      emit_insn (gen_push (allocate_rtx));
+    }
+  if (split_stack_fn == NULL_RTX)
+    split_stack_fn = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
+  call_insn = ix86_expand_call (NULL_RTX, gen_rtx_MEM (QImode, split_stack_fn),
+                               GEN_INT (UNITS_PER_WORD), constm1_rtx,
+                               NULL_RTX, 0);
+  add_function_usage_to (call_insn, call_fusage);
+
+  /* In order to make call/return prediction work right, we now need
+     to execute a return instruction.  See
+     libgcc/config/i386/morestack.S for the details on how this works.
+
+     For flow purposes gcc must not see this as a return
+     instruction--we need control flow to continue at the subsequent
+     label.  Therefore, we use an unspec.  */
+  gcc_assert (crtl->args.pops_args < 65536);
+  emit_insn (gen_split_stack_return (GEN_INT (crtl->args.pops_args)));
+
+  /* If we are in 64-bit mode and this function uses a static chain,
+     we saved %r10 in %rax before calling _morestack.  */
+  if (TARGET_64BIT && DECL_STATIC_CHAIN (cfun->decl))
+    emit_move_insn (gen_rtx_REG (Pmode, R10_REG),
+                   gen_rtx_REG (Pmode, AX_REG));
+
+  /* If this function calls va_start, we need to store a pointer to
+     the arguments on the old stack, because they may not have been
+     all copied to the new stack.  At this point the old stack can be
+     found at the frame pointer value used by __morestack, because
+     __morestack has set that up before calling back to us.  Here we
+     store that pointer in a scratch register, and in
+     ix86_expand_prologue we store the scratch register in a stack
+     slot.  */
+  if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
+    {
+      unsigned int scratch_regno;
+      rtx frame_reg;
+      int words;
+
+      scratch_regno = split_stack_prologue_scratch_regno ();
+      scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
+      frame_reg = gen_rtx_REG (Pmode, BP_REG);
+
+      /* 64-bit:
+        fp -> old fp value
+              return address within this function
+              return address of caller of this function
+              stack arguments
+        So we add three words to get to the stack arguments.
+
+        32-bit:
+        fp -> old fp value
+              return address within this function
+               first argument to __morestack
+               second argument to __morestack
+               return address of caller of this function
+               stack arguments
+         So we add five words to get to the stack arguments.
+      */
+      words = TARGET_64BIT ? 3 : 5;
+      emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
+                             gen_rtx_PLUS (Pmode, frame_reg,
+                                           GEN_INT (words * UNITS_PER_WORD))));
+
+      varargs_label = gen_label_rtx ();
+      emit_jump_insn (gen_jump (varargs_label));
+      JUMP_LABEL (get_last_insn ()) = varargs_label;
+
+      emit_barrier ();
+    }
+
+  emit_label (label);
+  LABEL_NUSES (label) = 1;
+
+  /* If this function calls va_start, we now have to set the scratch
+     register for the case where we do not call __morestack.  In this
+     case we need to set it based on the stack pointer.  */
+  if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
+    {
+      emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
+                             gen_rtx_PLUS (Pmode, stack_pointer_rtx,
+                                           GEN_INT (UNITS_PER_WORD))));
+
+      emit_label (varargs_label);
+      LABEL_NUSES (varargs_label) = 1;
+    }
+}
+
+/* We may have to tell the dataflow pass that the split stack prologue
+   is initializing a scratch register.  */
+
+static void
+ix86_live_on_entry (bitmap regs)
+{
+  if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
+    {
+      gcc_assert (flag_split_stack);
+      bitmap_set_bit (regs, split_stack_prologue_scratch_regno ());
+    }
+}
 \f
 /* Extract the parts of an RTL expression that is a valid memory address
    for an instruction.  Return 0 if the structure of the address is
 \f
 /* Extract the parts of an RTL expression that is a valid memory address
    for an instruction.  Return 0 if the structure of the address is
@@ -10979,6 +11332,10 @@ ix86_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
          case UNSPEC_DTPOFF:
            break;
 
          case UNSPEC_DTPOFF:
            break;
 
+         case UNSPEC_STACK_CHECK:
+           gcc_assert (flag_split_stack);
+           break;
+
          default:
            /* Invalid address unspec.  */
            return false;
          default:
            /* Invalid address unspec.  */
            return false;
@@ -11870,6 +12227,13 @@ output_pic_addr_const (FILE *file, rtx x, int code)
       break;
 
      case UNSPEC:
       break;
 
      case UNSPEC:
+       if (XINT (x, 1) == UNSPEC_STACK_CHECK)
+        {
+          bool f = i386_asm_output_addr_const_extra (file, x);
+          gcc_assert (f);
+          break;
+        }
+
        gcc_assert (XVECLEN (x, 0) == 1);
        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
        switch (XINT (x, 1))
        gcc_assert (XVECLEN (x, 0) == 1);
        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
        switch (XINT (x, 1))
@@ -13273,6 +13637,22 @@ i386_asm_output_addr_const_extra (FILE *file, rtx x)
       break;
 #endif
 
       break;
 #endif
 
+    case UNSPEC_STACK_CHECK:
+      {
+       int offset;
+
+       gcc_assert (flag_split_stack);
+
+#ifdef TARGET_THREAD_SPLIT_STACK_OFFSET
+       offset = TARGET_THREAD_SPLIT_STACK_OFFSET;
+#else
+       gcc_unreachable ();
+#endif
+
+       fprintf (file, "%s:%d", TARGET_64BIT ? "%fs" : "%gs", offset);
+      }
+      break;
+
     default:
       return false;
     }
     default:
       return false;
     }
@@ -20359,7 +20739,7 @@ construct_plt_address (rtx symbol)
   return tmp;
 }
 
   return tmp;
 }
 
-void
+rtx
 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
                  rtx callarg2,
                  rtx pop, int sibcall)
 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
                  rtx callarg2,
                  rtx pop, int sibcall)
@@ -20450,6 +20830,8 @@ ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
   call = emit_call_insn (call);
   if (use)
     CALL_INSN_FUNCTION_USAGE (call) = use;
   call = emit_call_insn (call);
   if (use)
     CALL_INSN_FUNCTION_USAGE (call) = use;
+
+  return call;
 }
 
 \f
 }
 
 \f
@@ -32856,6 +33238,9 @@ ix86_units_per_simd_word (enum machine_mode mode)
 #undef TARGET_STACK_PROTECT_FAIL
 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
 
 #undef TARGET_STACK_PROTECT_FAIL
 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
 
+#undef TARGET_SUPPORTS_SPLIT_STACK
+#define TARGET_SUPPORTS_SPLIT_STACK ix86_supports_split_stack
+
 #undef TARGET_FUNCTION_VALUE
 #define TARGET_FUNCTION_VALUE ix86_function_value
 
 #undef TARGET_FUNCTION_VALUE
 #define TARGET_FUNCTION_VALUE ix86_function_value
 
@@ -32914,6 +33299,9 @@ ix86_units_per_simd_word (enum machine_mode mode)
 #undef TARGET_CAN_ELIMINATE
 #define TARGET_CAN_ELIMINATE ix86_can_eliminate
 
 #undef TARGET_CAN_ELIMINATE
 #define TARGET_CAN_ELIMINATE ix86_can_eliminate
 
+#undef TARGET_EXTRA_LIVE_ON_ENTRY
+#define TARGET_EXTRA_LIVE_ON_ENTRY ix86_live_on_entry
+
 #undef TARGET_ASM_CODE_END
 #define TARGET_ASM_CODE_END ix86_code_end
 
 #undef TARGET_ASM_CODE_END
 #define TARGET_ASM_CODE_END ix86_code_end
 
index b3439bc..e1c13ac 100644 (file)
@@ -2277,6 +2277,13 @@ struct GTY(()) machine_function {
      has been computed for.  */
   int use_fast_prologue_epilogue_nregs;
 
      has been computed for.  */
   int use_fast_prologue_epilogue_nregs;
 
+  /* For -fsplit-stack support: A stack local which holds a pointer to
+     the stack arguments for a function with a variable number of
+     arguments.  This is set at the start of the function and is used
+     to initialize the overflow_arg_area field of the va_list
+     structure.  */
+  rtx split_stack_varargs_pointer;
+
   /* This value is used for amd64 targets and specifies the current abi
      to be used. MS_ABI means ms abi. Otherwise SYSV_ABI means sysv abi.  */
   ENUM_BITFIELD(calling_abi) call_abi : 8;
   /* This value is used for amd64 targets and specifies the current abi
      to be used. MS_ABI means ms abi. Otherwise SYSV_ABI means sysv abi.  */
   ENUM_BITFIELD(calling_abi) call_abi : 8;
index c541c14..51e3754 100644 (file)
@@ -85,6 +85,7 @@
   UNSPEC_SET_RIP
   UNSPEC_SET_GOT_OFFSET
   UNSPEC_MEMORY_BLOCKAGE
   UNSPEC_SET_RIP
   UNSPEC_SET_GOT_OFFSET
   UNSPEC_MEMORY_BLOCKAGE
+  UNSPEC_STACK_CHECK
 
   ;; TLS support
   UNSPEC_TP
 
   ;; TLS support
   UNSPEC_TP
   "leave"
   [(set_attr "type" "leave")])
 \f
   "leave"
   [(set_attr "type" "leave")])
 \f
+;; Handle -fsplit-stack.
+
+(define_expand "split_stack_prologue"
+  [(const_int 0)]
+  ""
+{
+  ix86_expand_split_stack_prologue ();
+  DONE;
+})
+
+;; In order to support the call/return predictor, we use a return
+;; instruction which the middle-end doesn't see.
+(define_insn "split_stack_return"
+  [(unspec_volatile [(match_operand:SI 0 "const_int_operand" "")]
+                   UNSPEC_STACK_CHECK)]
+  ""
+{
+  if (operands[0] == const0_rtx)
+    return "ret";
+  else
+    return "ret\t%0";
+}
+  [(set_attr "atom_unit" "jeu")
+   (set_attr "modrm" "0")
+   (set (attr "length")
+       (if_then_else (match_operand:SI 0 "const0_operand" "")
+                     (const_int 1)
+                     (const_int 3)))
+   (set (attr "length_immediate")
+       (if_then_else (match_operand:SI 0 "const0_operand" "")
+                     (const_int 0)
+                     (const_int 2)))])
+
+;; If there are operand 0 bytes available on the stack, jump to
+;; operand 1.
+
+(define_expand "split_stack_space_check"
+  [(set (pc) (if_then_else
+             (ltu (minus (reg SP_REG)
+                         (match_operand 0 "register_operand" ""))
+                  (unspec [(const_int 0)] UNSPEC_STACK_CHECK))
+             (label_ref (match_operand 1 "" ""))
+             (pc)))]
+  ""
+{
+  rtx reg, size, limit;
+
+  reg = gen_reg_rtx (Pmode);
+  size = force_reg (Pmode, operands[0]);
+  emit_insn (gen_sub3_insn (reg, stack_pointer_rtx, size));
+  limit = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
+                         UNSPEC_STACK_CHECK);
+  limit = gen_rtx_MEM (Pmode, gen_rtx_CONST (Pmode, limit));
+  ix86_expand_branch (GEU, reg, limit, operands[1]);
+
+  DONE;
+})
+\f
 ;; Bit manipulation instructions.
 
 (define_expand "ffs<mode>2"
 ;; Bit manipulation instructions.
 
 (define_expand "ffs<mode>2"
index 81dfd1e..7564c70 100644 (file)
@@ -218,4 +218,8 @@ along with GCC; see the file COPYING3.  If not see
 #ifdef TARGET_LIBC_PROVIDES_SSP
 /* i386 glibc provides __stack_chk_guard in %gs:0x14.  */
 #define TARGET_THREAD_SSP_OFFSET       0x14
 #ifdef TARGET_LIBC_PROVIDES_SSP
 /* i386 glibc provides __stack_chk_guard in %gs:0x14.  */
 #define TARGET_THREAD_SSP_OFFSET       0x14
+
+/* We steal the last transactional memory word.  */
+#define TARGET_CAN_SPLIT_STACK
+#define TARGET_THREAD_SPLIT_STACK_OFFSET 0x30
 #endif
 #endif
index fda73d2..4a3e366 100644 (file)
@@ -123,4 +123,8 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 /* i386 glibc provides __stack_chk_guard in %gs:0x14,
    x86_64 glibc provides it in %fs:0x28.  */
 #define TARGET_THREAD_SSP_OFFSET       (TARGET_64BIT ? 0x28 : 0x14)
 /* i386 glibc provides __stack_chk_guard in %gs:0x14,
    x86_64 glibc provides it in %fs:0x28.  */
 #define TARGET_THREAD_SSP_OFFSET       (TARGET_64BIT ? 0x28 : 0x14)
+
+/* We steal the last transactional memory word.  */
+#define TARGET_CAN_SPLIT_STACK
+#define TARGET_THREAD_SPLIT_STACK_OFFSET (TARGET_64BIT ? 0x70 : 0x30)
 #endif
 #endif
index 7073c90..877dec2 100644 (file)
@@ -1926,7 +1926,8 @@ attributes are currently defined for functions on all targets:
 @code{returns_twice}, @code{noinline}, @code{noclone},
 @code{always_inline}, @code{flatten}, @code{pure}, @code{const},
 @code{nothrow}, @code{sentinel}, @code{format}, @code{format_arg},
 @code{returns_twice}, @code{noinline}, @code{noclone},
 @code{always_inline}, @code{flatten}, @code{pure}, @code{const},
 @code{nothrow}, @code{sentinel}, @code{format}, @code{format_arg},
-@code{no_instrument_function}, @code{section}, @code{constructor},
+@code{no_instrument_function}, @code{no_split_stack},
+@code{section}, @code{constructor},
 @code{destructor}, @code{used}, @code{unused}, @code{deprecated},
 @code{weak}, @code{malloc}, @code{alias}, @code{ifunc},
 @code{warn_unused_result}, @code{nonnull}, @code{gnu_inline},
 @code{destructor}, @code{used}, @code{unused}, @code{deprecated},
 @code{weak}, @code{malloc}, @code{alias}, @code{ifunc},
 @code{warn_unused_result}, @code{nonnull}, @code{gnu_inline},
@@ -2856,6 +2857,14 @@ If @option{-finstrument-functions} is given, profiling function calls will
 be generated at entry and exit of most user-compiled functions.
 Functions with this attribute will not be so instrumented.
 
 be generated at entry and exit of most user-compiled functions.
 Functions with this attribute will not be so instrumented.
 
+@item no_split_stack
+@cindex @code{no_split_stack} function attribute
+@opindex fsplit-stack
+If @option{-fsplit-stack} is given, functions will have a small
+prologue which decides whether to split the stack.  Functions with the
+@code{no_split_stack} attribute will not have that prologue, and thus
+may run with only a small amount of stack space available.
+
 @item noinline
 @cindex @code{noinline} function attribute
 This function attribute prevents a function from being considered for
 @item noinline
 @cindex @code{noinline} function attribute
 This function attribute prevents a function from being considered for
index ee62b1b..bb17a0e 100644 (file)
@@ -927,7 +927,7 @@ See S/390 and zSeries Options.
 -fshort-double  -fshort-wchar @gol
 -fverbose-asm  -fpack-struct[=@var{n}]  -fstack-check @gol
 -fstack-limit-register=@var{reg}  -fstack-limit-symbol=@var{sym} @gol
 -fshort-double  -fshort-wchar @gol
 -fverbose-asm  -fpack-struct[=@var{n}]  -fstack-check @gol
 -fstack-limit-register=@var{reg}  -fstack-limit-symbol=@var{sym} @gol
--fno-stack-limit @gol
+-fno-stack-limit -fsplit-stack @gol
 -fleading-underscore  -ftls-model=@var{model} @gol
 -ftrapv  -fwrapv  -fbounds-check @gol
 -fvisibility}
 -fleading-underscore  -ftls-model=@var{model} @gol
 -ftrapv  -fwrapv  -fbounds-check @gol
 -fvisibility}
@@ -17857,6 +17857,25 @@ and grows downwards, you can use the flags
 @option{-Wl,--defsym,__stack_limit=0x7ffe0000} to enforce a stack limit
 of 128KB@.  Note that this may only work with the GNU linker.
 
 @option{-Wl,--defsym,__stack_limit=0x7ffe0000} to enforce a stack limit
 of 128KB@.  Note that this may only work with the GNU linker.
 
+@item -fsplit-stack
+@opindex fsplit-stack
+Generate code to automatically split the stack before it overflows.
+The resulting program has a discontiguous stack which can only
+overflow if the program is unable to allocate any more memory.  This
+is most useful when running threaded programs, as it is no longer
+necessary to calculate a good stack size to use for each thread.  This
+is currently only implemented for the i386 and x86_64 backends running
+GNU/Linux.
+
+When code compiled with @option{-fsplit-stack} calls code compiled
+without @option{-fsplit-stack}, there may not be much stack space
+available for the latter code to run.  If compiling all code,
+including library code, with @option{-fsplit-stack} is not an option,
+then the linker can fix up these calls so that the code compiled
+without @option{-fsplit-stack} always has a large stack.  Support for
+this is implemented in the gold linker in GNU binutils release 2.21
+and later.
+
 @item -fleading-underscore
 @opindex fleading-underscore
 This option and its counterpart, @option{-fno-leading-underscore}, forcibly
 @item -fleading-underscore
 @opindex fleading-underscore
 This option and its counterpart, @option{-fno-leading-underscore}, forcibly
index 8ead53d..5be6c30 100644 (file)
@@ -2262,3 +2262,43 @@ document me!
 @deftypefn {Runtime Function} void __clear_cache (char *@var{beg}, char *@var{end})
 This function clears the instruction cache between @var{beg} and @var{end}.
 @end deftypefn
 @deftypefn {Runtime Function} void __clear_cache (char *@var{beg}, char *@var{end})
 This function clears the instruction cache between @var{beg} and @var{end}.
 @end deftypefn
+
+@subsection Split stack functions and variables
+@deftypefn {Runtime Function} {void *} __splitstack_find (void *@var{segment_arg}, @
+void *@var{sp}, size_t @var{len}, void **@var{next_segment}, @
+void **@var{next_sp}, void **@var{initial_sp})
+When using @option{-fsplit-stack}, this call may be used to iterate
+over the stack segments.  It may be called like this:
+@smallexample
+  void *next_segment = NULL;
+  void *next_sp = NULL;
+  void *initial_sp = NULL;
+  void *stack;
+  size_t stack_size;
+  while ((stack = __splitstack_find (next_segment, next_sp,
+                                     &stack_size, &next_segment,
+                                     &next_sp, &initial_sp))
+         != NULL)
+    @{
+      /* Stack segment starts at stack and is
+         stack_size bytes long.  */
+    @}
+@end smallexample
+
+There is no way to iterate over the stack segments of a different
+thread.  However, what is permitted is for one thread to call this
+with the @var{segment_arg} and @var{sp} arguments NULL, to pass
+@var{next_segment}, @var{next_sp}, and @var{initial_sp} to a different
+thread, and then to suspend one way or another.  A different thread
+may run the subsequent @code{__splitstack_find} iterations.  Of
+course, this will only work if the first thread is suspended while the
+second thread is calling @code{__splitstack_find}.  If not, the second
+thread could be looking at the stack while it is changing, and
+anything could happen.
+@end deftypefn
+
+@defvar __morestack_segments
+@defvarx __morestack_current_segment
+@defvarx __morestack_initial_sp
+Internal variables used by the @option{-fsplit-stack} implementation.
+@end defvar
index a5356eb..3737314 100644 (file)
@@ -4960,6 +4960,10 @@ The default version of this hook invokes a function called
 normally defined in @file{libgcc2.c}.
 @end deftypefn
 
 normally defined in @file{libgcc2.c}.
 @end deftypefn
 
+@deftypefn {Target Hook} bool TARGET_SUPPORTS_SPLIT_STACK (bool)
+Whether this target supports splitting the stack.  This is called after options have been parsed, so the target may reject splitting the stack in some configurations.  The default version of this hook returns false.  If @var{report} is true, this function may issue a warning or error; if @var{report} is false, it must simply return a value
+@end deftypefn
+
 @node Varargs
 @section Implementing the Varargs Macros
 @cindex varargs implementation
 @node Varargs
 @section Implementing the Varargs Macros
 @cindex varargs implementation
index 2fcd979..ea3087f 100644 (file)
@@ -4960,6 +4960,8 @@ The default version of this hook invokes a function called
 normally defined in @file{libgcc2.c}.
 @end deftypefn
 
 normally defined in @file{libgcc2.c}.
 @end deftypefn
 
+@hook TARGET_SUPPORTS_SPLIT_STACK
+
 @node Varargs
 @section Implementing the Varargs Macros
 @cindex varargs implementation
 @node Varargs
 @section Implementing the Varargs Macros
 @cindex varargs implementation
index 2024b53..21af58a 100644 (file)
@@ -1,6 +1,6 @@
 /* Subroutines for manipulating rtx's in semantically interesting ways.
    Copyright (C) 1987, 1991, 1994, 1995, 1996, 1997, 1998,
 /* Subroutines for manipulating rtx's in semantically interesting ways.
    Copyright (C) 1987, 1991, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
 
 This file is part of GCC.
    Free Software Foundation, Inc.
 
 This file is part of GCC.
@@ -1146,6 +1146,7 @@ allocate_dynamic_stack_space (rtx size, rtx target, int known_align,
 {
   HOST_WIDE_INT stack_usage_size = -1;
   bool known_align_valid = true;
 {
   HOST_WIDE_INT stack_usage_size = -1;
   bool known_align_valid = true;
+  rtx final_label, final_target;
 
   /* If we're asking for zero bytes, it doesn't matter what we point
      to since we can't dereference it.  But return a reasonable
 
   /* If we're asking for zero bytes, it doesn't matter what we point
      to since we can't dereference it.  But return a reasonable
@@ -1284,6 +1285,14 @@ allocate_dynamic_stack_space (rtx size, rtx target, int known_align,
        }
     }
 
        }
     }
 
+  /* Don't use a TARGET that isn't a pseudo or is the wrong mode.  */
+  if (target == 0 || !REG_P (target)
+      || REGNO (target) < FIRST_PSEUDO_REGISTER
+      || GET_MODE (target) != Pmode)
+    target = gen_reg_rtx (Pmode);
+
+  mark_reg_pointer (target, known_align);
+
   /* The size is supposed to be fully adjusted at this point so record it
      if stack usage info is requested.  */
   if (flag_stack_usage)
   /* The size is supposed to be fully adjusted at this point so record it
      if stack usage info is requested.  */
   if (flag_stack_usage)
@@ -1296,6 +1305,52 @@ allocate_dynamic_stack_space (rtx size, rtx target, int known_align,
        current_function_has_unbounded_dynamic_stack_size = 1;
     }
 
        current_function_has_unbounded_dynamic_stack_size = 1;
     }
 
+  final_label = NULL_RTX;
+  final_target = NULL_RTX;
+
+  /* If we are splitting the stack, we need to ask the backend whether
+     there is enough room on the current stack.  If there isn't, or if
+     the backend doesn't know how to tell is, then we need to call a
+     function to allocate memory in some other way.  This memory will
+     be released when we release the current stack segment.  The
+     effect is that stack allocation becomes less efficient, but at
+     least it doesn't cause a stack overflow.  */
+  if (flag_split_stack)
+    {
+      rtx available_label, space, func;
+
+      available_label = NULL_RTX;
+
+#ifdef HAVE_split_stack_space_check
+      if (HAVE_split_stack_space_check)
+       {
+         available_label = gen_label_rtx ();
+
+         /* This instruction will branch to AVAILABLE_LABEL if there
+            are SIZE bytes available on the stack.  */
+         emit_insn (gen_split_stack_space_check (size, available_label));
+       }
+#endif
+
+      func = init_one_libfunc ("__morestack_allocate_stack_space");
+
+      space = emit_library_call_value (func, target, LCT_NORMAL, Pmode,
+                                      1, size, Pmode);
+
+      if (available_label == NULL_RTX)
+       return space;
+
+      final_target = gen_reg_rtx (Pmode);
+      mark_reg_pointer (final_target, known_align);
+
+      emit_move_insn (final_target, space);
+
+      final_label = gen_label_rtx ();
+      emit_jump (final_label);
+
+      emit_label (available_label);
+    }
+
   do_pending_stack_adjust ();
 
  /* We ought to be called always on the toplevel and stack ought to be aligned
   do_pending_stack_adjust ();
 
  /* We ought to be called always on the toplevel and stack ought to be aligned
@@ -1313,14 +1368,6 @@ allocate_dynamic_stack_space (rtx size, rtx target, int known_align,
   else if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
     probe_stack_range (STACK_CHECK_PROTECT, size);
 
   else if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
     probe_stack_range (STACK_CHECK_PROTECT, size);
 
-  /* Don't use a TARGET that isn't a pseudo or is the wrong mode.  */
-  if (target == 0 || !REG_P (target)
-      || REGNO (target) < FIRST_PSEUDO_REGISTER
-      || GET_MODE (target) != Pmode)
-    target = gen_reg_rtx (Pmode);
-
-  mark_reg_pointer (target, known_align);
-
   /* Perform the required allocation from the stack.  Some systems do
      this differently than simply incrementing/decrementing from the
      stack pointer, such as acquiring the space by calling malloc().  */
   /* Perform the required allocation from the stack.  Some systems do
      this differently than simply incrementing/decrementing from the
      stack pointer, such as acquiring the space by calling malloc().  */
@@ -1413,6 +1460,15 @@ allocate_dynamic_stack_space (rtx size, rtx target, int known_align,
   if (cfun->nonlocal_goto_save_area != 0)
     update_nonlocal_goto_save_area ();
 
   if (cfun->nonlocal_goto_save_area != 0)
     update_nonlocal_goto_save_area ();
 
+  /* Finish up the split stack handling.  */
+  if (final_label != NULL_RTX)
+    {
+      gcc_assert (flag_split_stack);
+      emit_move_insn (final_target, target);
+      emit_label (final_label);
+      target = final_target;
+    }
+
   return target;
 }
 \f
   return target;
 }
 \f
index a11f248..04a2ebc 100644 (file)
@@ -5214,17 +5214,50 @@ emit_return_into_block (basic_block bb)
 static void
 thread_prologue_and_epilogue_insns (void)
 {
 static void
 thread_prologue_and_epilogue_insns (void)
 {
-  int inserted = 0;
+  bool inserted;
+  rtx seq, epilogue_end;
+  edge entry_edge;
   edge e;
   edge e;
-#if defined (HAVE_sibcall_epilogue) || defined (HAVE_epilogue) || defined (HAVE_return) || defined (HAVE_prologue)
-  rtx seq;
-#endif
-#if defined (HAVE_epilogue) || defined(HAVE_return)
-  rtx epilogue_end = NULL_RTX;
-#endif
   edge_iterator ei;
 
   rtl_profile_for_bb (ENTRY_BLOCK_PTR);
   edge_iterator ei;
 
   rtl_profile_for_bb (ENTRY_BLOCK_PTR);
+
+  inserted = false;
+  seq = NULL_RTX;
+  epilogue_end = NULL_RTX;
+
+  /* Can't deal with multiple successors of the entry block at the
+     moment.  Function should always have at least one entry
+     point.  */
+  gcc_assert (single_succ_p (ENTRY_BLOCK_PTR));
+  entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
+
+  if (flag_split_stack
+      && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
+         == NULL))
+    {
+#ifndef HAVE_split_stack_prologue
+      gcc_unreachable ();
+#else
+      gcc_assert (HAVE_split_stack_prologue);
+
+      start_sequence ();
+      emit_insn (gen_split_stack_prologue ());
+      seq = get_insns ();
+      end_sequence ();
+
+      record_insns (seq, NULL, &prologue_insn_hash);
+      set_insn_locators (seq, prologue_locator);
+
+      /* This relies on the fact that committing the edge insertion
+        will look for basic blocks within the inserted instructions,
+        which in turn relies on the fact that we are not in CFG
+        layout mode here.  */
+      insert_insn_on_edge (seq, entry_edge);
+      inserted = true;
+#endif
+    }
+
 #ifdef HAVE_prologue
   if (HAVE_prologue)
     {
 #ifdef HAVE_prologue
   if (HAVE_prologue)
     {
@@ -5251,13 +5284,8 @@ thread_prologue_and_epilogue_insns (void)
       end_sequence ();
       set_insn_locators (seq, prologue_locator);
 
       end_sequence ();
       set_insn_locators (seq, prologue_locator);
 
-      /* Can't deal with multiple successors of the entry block
-         at the moment.  Function should always have at least one
-         entry point.  */
-      gcc_assert (single_succ_p (ENTRY_BLOCK_PTR));
-
-      insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
-      inserted = 1;
+      insert_insn_on_edge (seq, entry_edge);
+      inserted = true;
     }
 #endif
 
     }
 #endif
 
@@ -5427,7 +5455,7 @@ thread_prologue_and_epilogue_insns (void)
       end_sequence ();
 
       insert_insn_on_edge (seq, e);
       end_sequence ();
 
       insert_insn_on_edge (seq, e);
-      inserted = 1;
+      inserted = true;
     }
   else
 #endif
     }
   else
 #endif
index 2614faf..cd201f5 100644 (file)
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -545,6 +545,16 @@ proper position among the other output files.  */
 #define MFLIB_SPEC "%{fmudflap|fmudflapth: -export-dynamic}"
 #endif
 
 #define MFLIB_SPEC "%{fmudflap|fmudflapth: -export-dynamic}"
 #endif
 
+/* When using -fsplit-stack we need to wrap pthread_create, in order
+   to initialize the stack guard.  We always use wrapping, rather than
+   shared library ordering, and we keep the wrapper function in
+   libgcc.  This is not yet a real spec, though it could become one;
+   it is currently just stuffed into LINK_SPEC.  FIXME: This wrapping
+   only works with GNU ld and gold.  FIXME: This is incompatible with
+   -fmudflap when linking statically, which wants to do its own
+   wrapping.  */
+#define STACK_SPLIT_SPEC " %{fsplit-stack: --wrap=pthread_create}"
+
 /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
    included.  */
 #ifndef LIBGCC_SPEC
 /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
    included.  */
 #ifndef LIBGCC_SPEC
@@ -657,7 +667,8 @@ proper position among the other output files.  */
    "%X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r}\
     %{s} %{t} %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
     %{static:} %{L*} %(mfwrap) %(link_libgcc) %o\
    "%X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r}\
     %{s} %{t} %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
     %{static:} %{L*} %(mfwrap) %(link_libgcc) %o\
-    %{fopenmp|ftree-parallelize-loops=*:%:include(libgomp.spec)%(link_gomp)} %(mflib)\
+    %{fopenmp|ftree-parallelize-loops=*:%:include(libgomp.spec)%(link_gomp)}\
+    %(mflib) " STACK_SPLIT_SPEC "\
     %{fprofile-arcs|fprofile-generate*|coverage:-lgcov}\
     %{!nostdlib:%{!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence)}}\
     %{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}"
     %{fprofile-arcs|fprofile-generate*|coverage:-lgcov}\
     %{!nostdlib:%{!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence)}}\
     %{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}"
index 1f24f17..c132635 100644 (file)
@@ -1914,4 +1914,8 @@ GCC_4.5.0 {
 
 %inherit GCC_4.6.0 GCC_4.5.0
 GCC_4.6.0 {
 
 %inherit GCC_4.6.0 GCC_4.5.0
 GCC_4.6.0 {
+  __morestack_segments
+  __morestack_current_segment
+  __morestack_initial_sp
+  __splitstack_find
 }
 }
index 643f889..40e6acd 100644 (file)
@@ -1086,6 +1086,20 @@ decode_options (unsigned int argc, const char **argv,
      check option consistency.  */
   if (flag_lto && flag_whopr)
     error ("-flto and -fwhopr are mutually exclusive");
      check option consistency.  */
   if (flag_lto && flag_whopr)
     error ("-flto and -fwhopr are mutually exclusive");
+
+  /* We initialize flag_split_stack to -1 so that targets can set a
+     default value if they choose based on other options.  */
+  if (flag_split_stack == -1)
+    flag_split_stack = 0;
+  else
+    {
+      if (!targetm.supports_split_stack (true))
+       {
+         error ("%<-fsplit-stack%> is not supported by "
+                "this compiler configuration");
+         flag_split_stack = 0;
+       }
+    }
 }
 
 #define LEFT_COLUMN    27
 }
 
 #define LEFT_COLUMN    27
index 8371aa0..2a70fd2 100644 (file)
@@ -632,6 +632,7 @@ extern void default_asm_declare_constant_name (FILE *, const char *,
                                               const_tree, HOST_WIDE_INT);
 extern void default_file_start (void);
 extern void file_end_indicate_exec_stack (void);
                                               const_tree, HOST_WIDE_INT);
 extern void default_file_start (void);
 extern void file_end_indicate_exec_stack (void);
+extern void file_end_indicate_split_stack (void);
 
 extern void default_elf_asm_output_external (FILE *file, tree,
                                             const char *);
 
 extern void default_elf_asm_output_external (FILE *file, tree,
                                             const char *);
index 35c9799..a60e75f 100644 (file)
@@ -1679,6 +1679,16 @@ DEFHOOK
  tree, (void),
  default_external_stack_protect_fail)
 
  tree, (void),
  default_external_stack_protect_fail)
 
+DEFHOOK
+(supports_split_stack,
+ "Whether this target supports splitting the stack.  This is called\
+ after options have been parsed, so the target may reject splitting\
+ the stack in some configurations.  The default version of this hook\
+ returns false.  If @var{report} is true, this function may issue a warning\
+ or error; if @var{report} is false, it must simply return a value",
+ bool, (bool),
+ hook_bool_bool_false)
+
 /* Returns NULL if target supports the insn within a doloop block,
    otherwise it returns an error message.  */
 DEFHOOK
 /* Returns NULL if target supports the insn within a doloop block,
    otherwise it returns an error message.  */
 DEFHOOK
index 150dac4..4f795dd 100644 (file)
@@ -1,3 +1,12 @@
+2010-09-27  Ian Lance Taylor  <iant@google.com>
+
+       * lib/target-supports.exp (check_effective_target_split_stack):
+       New procedure.
+       * gcc.dg/split-1.c: New test.
+       * gcc.dg/split-2.c: New test.
+       * gcc.dg/split-3.c: New test.
+       * gcc.dg/split-4.c: New test.
+
 2010-09-27  Nicola Pero  <nicola.pero@meta-innovation.com>
 
        PR objc/25464
 2010-09-27  Nicola Pero  <nicola.pero@meta-innovation.com>
 
        PR objc/25464
diff --git a/gcc/testsuite/gcc.dg/split-1.c b/gcc/testsuite/gcc.dg/split-1.c
new file mode 100644 (file)
index 0000000..044b4e2
--- /dev/null
@@ -0,0 +1,49 @@
+/* This test needs to use setrlimit to set the stack size, so it can
+   only run on Unix.  */
+/* { dg-do run { target *-*-linux* *-*-solaris* *-*-darwin* } } */
+/* { dg-require-effective-target split_stack } */
+/* { dg-options "-fsplit-stack" } */
+
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/resource.h>
+
+/* Use a noinline function to ensure that the buffer is not removed
+   from the stack.  */
+static void use_buffer (char *buf) __attribute__ ((noinline));
+static void
+use_buffer (char *buf)
+{
+  buf[0] = '\0';
+}
+
+/* Each recursive call uses 10,000 bytes.  We call it 1000 times,
+   using a total of 10,000,000 bytes.  If -fsplit-stack is not
+   working, that will overflow our stack limit.  */
+
+static void
+down (int i)
+{
+  char buf[10000];
+
+  if (i > 0)
+    {
+      use_buffer (buf);
+      down (i - 1);
+    }
+}
+
+int
+main (void)
+{
+  struct rlimit r;
+
+  /* We set a stack limit because we are usually invoked via make, and
+     make sets the stack limit to be as large as possible.  */
+  r.rlim_cur = 8192 * 1024;
+  r.rlim_max = 8192 * 1024;
+  if (setrlimit (RLIMIT_STACK, &r) != 0)
+    abort ();
+  down (1000);
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/split-2.c b/gcc/testsuite/gcc.dg/split-2.c
new file mode 100644 (file)
index 0000000..208169a
--- /dev/null
@@ -0,0 +1,55 @@
+/* { dg-do run } */
+/* { dg-require-effective-target split_stack } */
+/* { dg-require-effective-target pthread_h } */
+/* { dg-options "-pthread -fsplit-stack" } */
+
+#include <stdlib.h>
+#include <pthread.h>
+
+/* Use a noinline function to ensure that the buffer is not removed
+   from the stack.  */
+static void use_buffer (char *buf) __attribute__ ((noinline));
+static void
+use_buffer (char *buf)
+{
+  buf[0] = '\0';
+}
+
+/* Each recursive call uses 10,000 bytes.  We call it 1000 times,
+   using a total of 10,000,000 bytes.  If -fsplit-stack is not
+   working, that will overflow our stack limit.  */
+
+static void
+down (int i)
+{
+  char buf[10000];
+
+  if (i > 0)
+    {
+      use_buffer (buf);
+      down (i - 1);
+    }
+}
+
+static void *
+thread_routine (void *arg __attribute__ ((unused)))
+{
+  down (1000);
+  return NULL;
+}
+
+int
+main (void)
+{
+  int i;
+  pthread_t tid;
+  void *dummy;
+
+  i = pthread_create (&tid, NULL, thread_routine, NULL);
+  if (i != 0)
+    abort ();
+  i = pthread_join (tid, &dummy);
+  if (i != 0)
+    abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/split-3.c b/gcc/testsuite/gcc.dg/split-3.c
new file mode 100644 (file)
index 0000000..360f672
--- /dev/null
@@ -0,0 +1,64 @@
+/* This test needs to use setrlimit to set the stack size, so it can
+   only run on Unix.  */
+/* { dg-do run { target *-*-linux* *-*-solaris* *-*-darwin* } } */
+/* { dg-require-effective-target split_stack } */
+/* { dg-options "-fsplit-stack" } */
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/resource.h>
+
+/* Use a noinline function to ensure that the buffer is not removed
+   from the stack.  */
+static void use_buffer (char *buf) __attribute__ ((noinline));
+static void
+use_buffer (char *buf)
+{
+  buf[0] = '\0';
+}
+
+/* Each recursive call uses 10,000 bytes.  We call it 1000 times,
+   using a total of 10,000,000 bytes.  If -fsplit-stack is not
+   working, that will overflow our stack limit.  */
+
+static void
+down (int i, ...)
+{
+  char buf[10000];
+  va_list ap;
+
+  va_start (ap, i);
+  if (va_arg (ap, int) != 1
+      || va_arg (ap, int) != 2
+      || va_arg (ap, int) != 3
+      || va_arg (ap, int) != 4
+      || va_arg (ap, int) != 5
+      || va_arg (ap, int) != 6
+      || va_arg (ap, int) != 7
+      || va_arg (ap, int) != 8
+      || va_arg (ap, int) != 9
+      || va_arg (ap, int) != 10)
+    abort ();
+
+  if (i > 0)
+    {
+      use_buffer (buf);
+      down (i - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
+    }
+}
+
+int
+main (void)
+{
+  struct rlimit r;
+
+  /* We set a stack limit because we are usually invoked via make, and
+     make sets the stack limit to be as large as possible.  */
+  r.rlim_cur = 8192 * 1024;
+  r.rlim_max = 8192 * 1024;
+  if (setrlimit (RLIMIT_STACK, &r) != 0)
+    abort ();
+  down (1000, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/split-4.c b/gcc/testsuite/gcc.dg/split-4.c
new file mode 100644 (file)
index 0000000..38196be
--- /dev/null
@@ -0,0 +1,68 @@
+/* This test needs to use setrlimit to set the stack size, so it can
+   only run on Unix.  */
+/* { dg-do run { target *-*-linux* *-*-solaris* *-*-darwin* } } */
+/* { dg-require-effective-target split_stack } */
+/* { dg-options "-fsplit-stack" } */
+
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/resource.h>
+
+/* Use a noinline function to ensure that the buffer is not removed
+   from the stack.  */
+static void use_buffer (char *buf, size_t) __attribute__ ((noinline));
+static void
+use_buffer (char *buf, size_t c)
+{
+  size_t i;
+
+  for (i = 0; i < c; ++i)
+    buf[i] = (char) i;
+}
+
+/* Each recursive call uses 10 * i bytes.  We call it 1000 times,
+   using a total of 5,000,000 bytes.  If -fsplit-stack is not working,
+   that will overflow our stack limit.  */
+
+static void
+down1 (int i)
+{
+  char buf[10 * i];
+
+  if (i > 0)
+    {
+      use_buffer (buf, 10 * i);
+      down1 (i - 1);
+    }
+}
+
+/* Same thing, using alloca.  */
+
+static void
+down2 (int i)
+{
+  char *buf = alloca (10 * i);
+
+  if (i > 0)
+    {
+      use_buffer (buf, 10 * i);
+      down2 (i - 1);
+    }
+}
+
+int
+main (void)
+{
+  struct rlimit r;
+
+  /* We set a stack limit because we are usually invoked via make, and
+     make sets the stack limit to be as large as possible.  */
+  r.rlim_cur = 8192 * 1024;
+  r.rlim_max = 8192 * 1024;
+  if (setrlimit (RLIMIT_STACK, &r) != 0)
+    abort ();
+  down1 (1000);
+  down2 (1000);
+  return 0;
+}
index cf78c95..e2174b7 100644 (file)
@@ -3664,6 +3664,15 @@ proc check_effective_target_lto { } {
     return [info exists ENABLE_LTO]
 }
 
     return [info exists ENABLE_LTO]
 }
 
+# Return 1 if this target supports the -fsplit-stack option, 0
+# otherwise.
+
+proc check_effective_target_split_stack {} {
+    return [check_no_compiler_messages split_stack object {
+       void foo (void) { }
+    } "-fsplit-stack"]
+}
+
 # Return 1 if the language for the compiler under test is C.
 
 proc check_effective_target_c { } {
 # Return 1 if the language for the compiler under test is C.
 
 proc check_effective_target_c { } {
index b02462b..bc31494 100644 (file)
@@ -99,6 +99,10 @@ bool first_function_block_is_cold;
 
 static alias_set_type const_alias_set;
 
 
 static alias_set_type const_alias_set;
 
+/* Whether we saw any functions with no_split_stack.  */
+
+static bool saw_no_split_stack;
+
 static const char *strip_reg_name (const char *);
 static int contains_pointers_p (tree);
 #ifdef ASM_OUTPUT_EXTERNAL
 static const char *strip_reg_name (const char *);
 static int contains_pointers_p (tree);
 #ifdef ASM_OUTPUT_EXTERNAL
@@ -1549,6 +1553,9 @@ assemble_start_function (tree decl, const char *fnname)
   /* Standard thing is just output label for the function.  */
   ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
 #endif /* ASM_DECLARE_FUNCTION_NAME */
   /* Standard thing is just output label for the function.  */
   ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
 #endif /* ASM_DECLARE_FUNCTION_NAME */
+
+  if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
+    saw_no_split_stack = true;
 }
 
 /* Output assembler code associated with defining the size of the
 }
 
 /* Output assembler code associated with defining the size of the
@@ -6649,6 +6656,28 @@ file_end_indicate_exec_stack (void)
   switch_to_section (get_section (".note.GNU-stack", flags, NULL));
 }
 
   switch_to_section (get_section (".note.GNU-stack", flags, NULL));
 }
 
+/* Emit a special section directive to indicate that this object file
+   was compiled with -fsplit-stack.  This is used to let the linker
+   detect calls between split-stack code and non-split-stack code, so
+   that it can modify the split-stack code to allocate a sufficiently
+   large stack.  We emit another special section if there are any
+   functions in this file which have the no_split_stack attribute, to
+   prevent the linker from warning about being unable to convert the
+   functions if they call non-split-stack code.  */
+
+void
+file_end_indicate_split_stack (void)
+{
+  if (flag_split_stack)
+    {
+      switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
+                                     NULL));
+      if (saw_no_split_stack)
+       switch_to_section (get_section (".note.GNU-no-split-stack",
+                                       SECTION_DEBUG, NULL));
+    }
+}
+
 /* Output DIRECTIVE (a C string) followed by a newline.  This is used as
    a get_unnamed_section callback.  */
 
 /* Output DIRECTIVE (a C string) followed by a newline.  This is used as
    a get_unnamed_section callback.  */
 
index 1dc0777..1bbebd9 100644 (file)
@@ -1,3 +1,14 @@
+2010-09-27  Ian Lance Taylor  <iant@google.com>
+
+       * generic-morestack.h: New file.
+       * generic-morestack.c: New file.
+       * generic-morestack-thread.c: New file.
+       * config/i386/morestack.S: New file.
+       * config/t-stack: New file.
+       * config/i386/t-stack-i386: New file.
+       * config.host (i[34567]86-*-linux* and friends): Add t-stack and
+       i386/t-stack-i386 to tmake_file.
+
 2010-09-21  Iain Sandoe  <iains@gcc.gnu.org>
 
        * Makefile.in (libgcc-extra-parts):  Check for static archives and
 2010-09-21  Iain Sandoe  <iains@gcc.gnu.org>
 
        * Makefile.in (libgcc-extra-parts):  Check for static archives and
index 8024277..8476218 100644 (file)
@@ -602,7 +602,7 @@ case ${host} in
 i[34567]86-*-linux* | x86_64-*-linux* | \
   i[34567]86-*-kfreebsd*-gnu | i[34567]86-*-knetbsd*-gnu | \
   i[34567]86-*-gnu*)
 i[34567]86-*-linux* | x86_64-*-linux* | \
   i[34567]86-*-kfreebsd*-gnu | i[34567]86-*-knetbsd*-gnu | \
   i[34567]86-*-gnu*)
-       tmake_file="${tmake_file} t-tls"
+       tmake_file="${tmake_file} t-tls t-stack i386/t-stack-i386"
        ;;
 esac
 
        ;;
 esac
 
diff --git a/libgcc/config/i386/morestack.S b/libgcc/config/i386/morestack.S
new file mode 100644 (file)
index 0000000..79abba3
--- /dev/null
@@ -0,0 +1,559 @@
+# x86/x86_64 support for -fsplit-stack.
+# Copyright (C) 2009, 2010 Free Software Foundation, Inc.
+# Contributed by Ian Lance Taylor <iant@google.com>.
+
+# 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/>.
+
+
+# Support for allocating more stack space when using -fsplit-stack.
+# When a function discovers that it needs more stack space, it will
+# call __morestack with the size of the stack frame and the size of
+# the parameters to copy from the old stack frame to the new one.
+# The __morestack function preserves the parameter registers and
+# calls __generic_morestack to actually allocate the stack space.
+
+# When this is called stack space is very low, but we ensure that
+# there is enough space to push the parameter registers and to call
+# __generic_morestack.
+
+# When calling __generic_morestack, FRAME_SIZE points to the size of
+# the desired frame when the function is called, and the function
+# sets it to the size of the allocated stack.  OLD_STACK points to
+# the parameters on the old stack and PARAM_SIZE is the number of
+# bytes of parameters to copy to the new stack.  These are the
+# parameters of the function that called __morestack.  The
+# __generic_morestack function returns the new stack pointer,
+# pointing to the address of the first copied parameter.  The return
+# value minus the returned *FRAME_SIZE will be the first address on
+# the stack which we should not use.
+
+# void *__generic_morestack (size_t *frame_size, void *old_stack,
+#                           size_t param_size);
+
+# The __morestack routine has to arrange for the caller to return to a
+# stub on the new stack.  The stub is responsible for restoring the
+# old stack pointer and returning to the caller's caller.  This calls
+# __generic_releasestack to retrieve the old stack pointer and release
+# the newly allocated stack.
+
+# void *__generic_releasestack (size_t *available);
+
+# We do a little dance so that the processor's call/return return
+# address prediction works out.  The compiler arranges for the caller
+# to look like this:
+#   call __generic_morestack
+#   ret
+#  L:
+#   // carry on with function
+# After we allocate more stack, we call L, which is in our caller.
+# When that returns (to the predicted instruction), we release the
+# stack segment and reset the stack pointer.  We then return to the
+# predicted instruction, namely the ret instruction immediately after
+# the call to __generic_morestack.  That then returns to the caller of
+# the original caller.
+
+
+# The amount of extra space we ask for.  In general this has to be
+# enough for the dynamic loader to find a symbol and for a signal
+# handler to run.
+       
+#ifndef __x86_64__
+#define BACKOFF (1024)
+#else
+#define BACKOFF (1536)
+#endif
+
+
+# This entry point is for split-stack code which calls non-split-stack
+# code.  When the linker sees this case, it converts the call to
+# __morestack to call __morestack_non_split instead.  We just bump the
+# requested stack space by 16K.
+
+       .global __morestack_non_split
+       .hidden __morestack_non_split
+
+#ifdef __ELF__
+       .type   __morestack_non_split,@function
+#endif
+
+__morestack_non_split:
+
+#ifndef __x86_64__
+       addl    $0x4000,4(%esp)
+#else
+       addq    $0x4000,%r10
+#endif
+
+#ifdef __ELF__
+       .size   __morestack_non_split, . - __morestack_non_split
+#endif
+
+# __morestack_non_split falls through into __morestack.
+
+
+# The __morestack function.
+
+       .global __morestack
+       .hidden __morestack
+
+#ifdef __ELF__
+       .type   __morestack,@function
+#endif
+
+__morestack:
+.LFB1:
+       .cfi_startproc
+
+
+#ifndef __x86_64__
+
+
+# The 32-bit __morestack function.
+
+       # We use a cleanup to restore the stack guard if an exception
+       # is thrown through this code.
+#ifndef __PIC__
+       .cfi_personality 0,__gcc_personality_v0
+       .cfi_lsda 0,.LLSDA1
+#else
+       .cfi_personality 0x9b,DW.ref.__gcc_personality_v0
+       .cfi_lsda 0x1b,.LLSDA1
+#endif
+
+       # Set up a normal backtrace.
+       pushl   %ebp
+       .cfi_def_cfa_offset 8
+       .cfi_offset %ebp, -8
+       movl    %esp, %ebp
+       .cfi_def_cfa_register %ebp
+
+       # We return below with a ret $8.  We will return to a single
+       # return instruction, which will return to the caller of our
+       # caller.  We let the unwinder skip that single return
+       # instruction, and just return to the real caller.
+       .cfi_offset 8, 8
+       .cfi_escape 0x15, 4, 0x7d       # DW_CFA_val_offset_sf, %esp, 12/-4
+
+       # In 32-bit mode the parameters are pushed on the stack.  The
+       # argument size is pushed then the new stack frame size is
+       # pushed.
+
+       # In 32-bit mode the registers %eax, %edx, and %ecx may be
+       # used for parameters, depending on the regparm and fastcall
+       # attributes.
+
+       pushl   %eax
+       pushl   %edx
+       pushl   %ecx
+
+       call    __morestack_block_signals
+
+       pushl   12(%ebp)                # The size of the parameters.
+       leal    20(%ebp),%eax           # Address of caller's parameters.
+       pushl   %eax
+       addl    $BACKOFF,8(%ebp)        # Ask for backoff bytes.
+       leal    8(%ebp),%eax            # The address of the new frame size.
+       pushl   %eax
+
+       # Note that %esp is exactly 32 bytes below the CFA -- perfect for
+       # a 16-byte aligned stack.  That said, we still ought to compile
+       # generic-morestack.c with -mpreferred-stack-boundary=2.  FIXME.
+       call    __generic_morestack
+
+       movl    %eax,%esp               # Switch to the new stack.
+       subl    8(%ebp),%eax            # The end of the stack space.
+       addl    $BACKOFF,%eax           # Back off 512 bytes.
+
+.LEHB0:
+       # FIXME: The offset must match
+       # TARGET_THREAD_SPLIT_STACK_OFFSET in
+       # gcc/config/i386/linux.h.
+       movl    %eax,%gs:0x30           # Save the new stack boundary.
+
+       call    __morestack_unblock_signals
+
+       movl    -8(%ebp),%edx           # Restore registers.
+       movl    -12(%ebp),%ecx
+
+       movl    4(%ebp),%eax            # Increment the return address
+       cmpb    $0xc3,(%eax)            # to skip the ret instruction;
+       je      1f                      # see above.
+       addl    $2,%eax
+1:     inc     %eax
+
+       movl    %eax,-8(%ebp)           # Store return address in an
+                                       # unused slot.
+
+       movl    -4(%ebp),%eax           # Restore the last register.
+
+       call    *-8(%ebp)               # Call our caller!
+
+       # The caller will return here, as predicted.
+
+       # Save the registers which may hold a return value.  We
+       # assume that __generic_releasestack does not touch any
+       # floating point or vector registers.
+       pushl   %eax
+       pushl   %edx
+
+       # Push the arguments to __generic_releasestack now so that the
+       # stack is at a 16-byte boundary for
+       # __morestack_block_signals.
+       pushl   $0                      # Where the available space is returned.
+       leal    0(%esp),%eax            # Push its address.
+       push    %eax
+
+       call    __morestack_block_signals
+
+       call    __generic_releasestack
+
+       subl    4(%esp),%eax            # Subtract available space.
+       addl    $BACKOFF,%eax           # Back off 512 bytes.
+.LEHE0:
+       movl    %eax,%gs:0x30           # Save the new stack boundary.
+
+       addl    $8,%esp                 # Remove values from stack.
+
+       # We need to restore the old stack pointer, which is in %rbp,
+       # before we unblock signals.  We also need to restore %eax and
+       # %edx after we unblock signals but before we return.  Do this
+       # by moving %eax and %edx from the current stack to the old
+       # stack.
+
+       popl    %edx                    # Pop return value from current stack.
+       popl    %eax
+
+       movl    %ebp,%esp               # Restore stack pointer.
+
+       pushl   %eax                    # Push return value on old stack.
+       pushl   %edx
+       subl    $8,%esp                 # Align stack to 16-byte boundary.
+
+       call    __morestack_unblock_signals
+
+       addl    $8,%esp
+       popl    %edx                    # Restore return value.
+       popl    %eax
+
+       .cfi_remember_state
+       popl    %ebp
+       .cfi_restore %ebp
+       .cfi_def_cfa %esp, 12
+       ret     $8                      # Return to caller, which will
+                                       # immediately return.  Pop
+                                       # arguments as we go.
+
+# This is the cleanup code called by the stack unwinder when unwinding
+# through the code between .LEHB0 and .LEHE0 above.
+       
+.L1:
+       .cfi_restore_state
+       subl    $16,%esp                # Maintain 16 byte alignment.
+       movl    %eax,4(%esp)            # Save exception header.
+       movl    %ebp,(%esp)             # Stack pointer after resume.
+       call    __generic_findstack
+       movl    %ebp,%ecx               # Get the stack pointer.
+       subl    %eax,%ecx               # Subtract available space.
+       addl    $BACKOFF,%ecx           # Back off 512 bytes.
+       movl    %ecx,%gs:0x30           # Save new stack boundary.
+       movl    4(%esp),%eax            # Function argument.
+       movl    %eax,(%esp)
+#ifdef __PIC__
+#undef __i686
+       call    __i686.get_pc_thunk.bx  # %ebx may not be set up for us.
+       addl    $_GLOBAL_OFFSET_TABLE_, %ebx
+       call    _Unwind_Resume@PLT      # Resume unwinding.
+#else
+       call    _Unwind_Resume
+#endif
+
+#else /* defined(__x86_64__) */
+
+
+# The 64-bit __morestack function.
+
+       # We use a cleanup to restore the stack guard if an exception
+       # is thrown through this code.
+#ifndef __PIC__
+       .cfi_personality 0x3,__gcc_personality_v0
+       .cfi_lsda 0x3,.LLSDA1
+#else
+       .cfi_personality 0x9b,DW.ref.__gcc_personality_v0
+       .cfi_lsda 0x1b,.LLSDA1
+#endif
+
+       # Set up a normal backtrace.
+       pushq   %rbp
+       .cfi_def_cfa_offset 16
+       .cfi_offset %rbp, -16
+       movq    %rsp, %rbp
+       .cfi_def_cfa_register %rbp
+
+       # We will return a single return instruction, which will
+       # return to the caller of our caller.  Let the unwinder skip
+       # that single return instruction, and just return to the real
+       # caller.
+       .cfi_offset 16, 0
+       .cfi_escape 0x15, 7, 0x7f       # DW_CFA_val_offset_sf, %esp, 8/-8
+
+       # In 64-bit mode the new stack frame size is passed in r10
+        # and the argument size is passed in r11.
+
+       addq    $BACKOFF,%r10           # Ask for backoff bytes.
+       pushq   %r10                    # Save new frame size.
+
+       # In 64-bit mode the registers %rdi, %rsi, %rdx, %rcx, %r8,
+       # and %r9 may be used for parameters.  We also preserve %rax
+       # which the caller may use to hold %r10.
+
+       pushq   %rax
+       pushq   %rdi
+       pushq   %rsi
+       pushq   %rdx
+       pushq   %rcx
+       pushq   %r8
+       pushq   %r9
+
+       pushq   %r11
+       pushq   $0                      # For alignment.
+
+       call    __morestack_block_signals
+
+       leaq    -8(%rbp),%rdi           # Address of new frame size.
+       leaq    24(%rbp),%rsi           # The caller's parameters.
+       addq    $8,%rsp
+       popq    %rdx                    # The size of the parameters.
+
+       call    __generic_morestack
+
+       movq    -8(%rbp),%r10           # Reload modified frame size
+       movq    %rax,%rsp               # Switch to the new stack.
+       subq    %r10,%rax               # The end of the stack space.
+       addq    $BACKOFF,%rax           # Back off 1024 bytes.
+
+.LEHB0:
+       # FIXME: The offset must match
+       # TARGET_THREAD_SPLIT_STACK_OFFSET in
+       # gcc/config/i386/linux64.h.
+       movq    %rax,%fs:0x70           # Save the new stack boundary.
+
+       call    __morestack_unblock_signals
+
+       movq    -24(%rbp),%rdi          # Restore registers.
+       movq    -32(%rbp),%rsi
+       movq    -40(%rbp),%rdx
+       movq    -48(%rbp),%rcx
+       movq    -56(%rbp),%r8
+       movq    -64(%rbp),%r9
+
+       movq    8(%rbp),%r10            # Increment the return address
+       incq    %r10                    # to skip the ret instruction;
+                                       # see above.
+
+       movq    -16(%rbp),%rax          # Restore caller's %rax.
+
+       call    *%r10                   # Call our caller!
+
+       # The caller will return here, as predicted.
+
+       # Save the registers which may hold a return value.  We
+       # assume that __generic_releasestack does not touch any
+       # floating point or vector registers.
+       pushq   %rax
+       pushq   %rdx
+
+       call    __morestack_block_signals
+
+       pushq   $0                      # For alignment.
+       pushq   $0                      # Where the available space is returned.
+       leaq    0(%rsp),%rdi            # Pass its address.
+
+       call    __generic_releasestack
+
+       subq    0(%rsp),%rax            # Subtract available space.
+       addq    $BACKOFF,%rax           # Back off 1024 bytes.
+.LEHE0:
+       movq    %rax,%fs:0x70           # Save the new stack boundary.
+
+       addq    $16,%rsp                # Remove values from stack.
+
+       # We need to restore the old stack pointer, which is in %rbp,
+       # before we unblock signals.  We also need to restore %rax and
+       # %rdx after we unblock signals but before we return.  Do this
+       # by moving %rax and %rdx from the current stack to the old
+       # stack.
+
+       popq    %rdx                    # Pop return value from current stack.
+       popq    %rax
+
+       movq    %rbp,%rsp               # Restore stack pointer.
+
+       pushq   %rax                    # Push return value on old stack.
+       pushq   %rdx
+
+       call    __morestack_unblock_signals
+
+       popq    %rdx                    # Restore return value.
+       popq    %rax
+
+       .cfi_remember_state
+       popq    %rbp
+       .cfi_restore %rbp
+       .cfi_def_cfa %rsp, 8
+       ret                             # Return to caller, which will
+                                       # immediately return.
+
+# This is the cleanup code called by the stack unwinder when unwinding
+# through the code between .LEHB0 and .LEHE0 above.
+       
+.L1:
+       .cfi_restore_state
+       subq    $16,%rsp                # Maintain 16 byte alignment.
+       movq    %rax,(%rsp)             # Save exception header.
+       movq    %rbp,%rdi               # Stack pointer after resume.
+       call    __generic_findstack
+       movq    %rbp,%rcx               # Get the stack pointer.
+       subq    %rax,%rcx               # Subtract available space.
+       addq    $BACKOFF,%rcx           # Back off 1024 bytes.
+       movq    %rcx,%fs:0x70           # Save new stack boundary.
+       movq    (%rsp),%rdi             # Restore exception data for call.
+#ifdef __PIC__
+       call    _Unwind_Resume@PLT      # Resume unwinding.
+#else
+       call    _Unwind_Resume          # Resume unwinding.
+#endif
+
+#endif /* defined(__x86_64__) */
+
+       .cfi_endproc
+#ifdef __ELF__
+       .size   __morestack, . - __morestack
+#endif
+
+
+# The exception table.  This tells the personality routine to execute
+# the exception handler.
+
+       .section        .gcc_except_table,"a",@progbits
+       .align  4
+.LLSDA1:
+       .byte   0xff    # @LPStart format (omit)
+       .byte   0xff    # @TType format (omit)
+       .byte   0x1     # call-site format (uleb128)
+       .uleb128 .LLSDACSE1-.LLSDACSB1  # Call-site table length
+.LLSDACSB1:
+       .uleb128 .LEHB0-.LFB1   # region 0 start
+       .uleb128 .LEHE0-.LEHB0  # length
+       .uleb128 .L1-.LFB1      # landing pad
+       .uleb128 0              # action
+.LLSDACSE1:
+
+
+       .global __gcc_personality_v0
+#ifdef __PIC__
+       # Build a position independent reference to the basic
+        # personality function.
+       .hidden DW.ref.__gcc_personality_v0
+       .weak   DW.ref.__gcc_personality_v0
+       .section .data.DW.ref.__gcc_personality_v0,"awG",@progbits,DW.ref.__gcc_personality_v0,comdat
+       .type   DW.ref.__gcc_personality_v0, @object
+DW.ref.__gcc_personality_v0:
+#ifndef __x86_64
+       .align 4
+       .size   DW.ref.__gcc_personality_v0, 4
+       .long   __gcc_personality_v0
+#else
+       .align 8
+       .size   DW.ref.__gcc_personality_v0, 8
+       .quad   __gcc_personality_v0
+#endif
+#endif
+
+
+# Initialize the stack test value when the program starts or when a
+# new thread starts.  We don't know how large the main stack is, so we
+# guess conservatively.  We might be able to use getrlimit here.
+
+       .text
+       .global __stack_split_initialize
+       .hidden __stack_split_initialize
+
+#ifdef __ELF__
+       .type   __stack_split_initialize, @function
+#endif
+
+__stack_split_initialize:
+
+#ifndef __x86_64__
+
+       leal    -16000(%esp),%eax       # We should have at least 16K.
+       movl    %eax,%gs:0x30
+       pushl   $16000
+       pushl   %esp
+#ifdef __PIC__
+       call    __generic_morestack_set_initial_sp@PLT
+#else
+       call    __generic_morestack_set_initial_sp
+#endif
+       addl    $8,%esp
+       ret
+
+#else /* defined(__x86_64__) */
+
+       leaq    -16000(%rsp),%rax       # We should have at least 16K.
+       movq    %rax,%fs:0x70
+       movq    %rsp,%rdi
+       movq    $16000,%rsi
+#ifdef __PIC__
+       call    __generic_morestack_set_initial_sp@PLT
+#else
+       call    __generic_morestack_set_initial_sp
+#endif
+       ret
+
+#endif /* defined(__x86_64__) */
+
+#ifdef __ELF__
+       .size   __stack_split_initialize, . - __stack_split_initialize
+#endif
+
+
+# Make __stack_split_initialize a high priority constructor.  FIXME:
+# This is ELF specific.
+
+       .section        .ctors.65535,"aw",@progbits
+
+#ifndef __x86_64__
+       .align  4
+       .long   __stack_split_initialize
+       .long   __morestack_load_mmap
+#else
+       .align  8
+       .quad   __stack_split_initialize
+       .quad   __morestack_load_mmap
+#endif
+
+#ifdef __ELF__
+       .section        .note.GNU-stack,"",@progbits
+       .section        .note.GNU-split-stack,"",@progbits
+       .section        .note.GNU-no-split-stack,"",@progbits
+#endif
diff --git a/libgcc/config/i386/t-stack-i386 b/libgcc/config/i386/t-stack-i386
new file mode 100644 (file)
index 0000000..6391cd2
--- /dev/null
@@ -0,0 +1,2 @@
+# Makefile fragment to support -fsplit-stack for x86.
+LIB2ADD += $(srcdir)/config/i386/morestack.S
diff --git a/libgcc/config/t-stack b/libgcc/config/t-stack
new file mode 100644 (file)
index 0000000..9ac223d
--- /dev/null
@@ -0,0 +1,4 @@
+# Makefile fragment to provide generic support for -fsplit-stack.
+# This should be used in config.host for any host which supports
+# -fsplit-stack.
+LIB2ADD += $(srcdir)/generic-morestack.c $(srcdir)/generic-morestack-thread.c
diff --git a/libgcc/generic-morestack-thread.c b/libgcc/generic-morestack-thread.c
new file mode 100644 (file)
index 0000000..bc23795
--- /dev/null
@@ -0,0 +1,162 @@
+/* Thread library support for -fsplit-stack.  */
+/* Copyright (C) 2009, 2010 Free Software Foundation, Inc.
+   Contributed by Ian Lance Taylor <iant@google.com>.
+
+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 "tconfig.h"
+#include "tsystem.h"
+#include "coretypes.h"
+#include "tm.h"
+
+/* If inhibit_libc is defined, we can not compile this file.  The
+   effect is that people will not be able to use -fsplit-stack.  That
+   is much better than failing the build particularly since people
+   will want to define inhibit_libc while building a compiler which
+   can build glibc.  */
+
+#ifndef inhibit_libc
+
+#include <pthread.h>
+
+#include "generic-morestack.h"
+
+/* We declare the pthread functions we need as weak, so that
+   libgcc_s.so does not need to be linked against -lpthread.  */
+
+extern int pthread_once (pthread_once_t *, void (*) (void))
+  __attribute__ ((weak));
+
+extern int pthread_key_create (pthread_key_t *, void (*) (void *))
+  __attribute__ ((weak));
+
+extern int pthread_setspecific (pthread_key_t, const void *)
+  __attribute__ ((weak));
+
+/* The key for the list of stack segments to free when the thread
+   exits.  This is created by pthread_key_create.  */
+
+static pthread_key_t segment_list_key;
+
+/* Used to only run create_key once.  */
+
+static pthread_once_t create_key_once = PTHREAD_ONCE_INIT;
+
+/* Release all the segments for a thread.  This is the destructor
+   function used by pthread_key_create, and is called when a thread
+   exits.  */
+
+static void
+free_segments (void* arg)
+{
+  __morestack_release_segments ((struct stack_segment **) arg, 1);
+}
+
+/* Set up the key for the list of segments.  This is called via
+   pthread_once.  */
+
+static void
+create_key (void)
+{
+  int err;
+
+  err = pthread_key_create (&segment_list_key, free_segments);
+  if (err != 0)
+    {
+      static const char msg[] = "pthread_key_create failed: errno ";
+      __morestack_fail (msg, sizeof msg - 1, err);
+    }
+}
+
+/* Pass information from the pthread_create wrapper to
+   stack_split_initialize_thread.  */
+
+struct pthread_create_args
+{
+  void *(*start_routine) (void *);
+  void *arg;
+};
+
+/* Initialize a thread.  This is called via pthread_create.  It calls
+   a target dependent function to set up any required stack guard.  */
+
+static void* stack_split_initialize_thread (void *)
+  __attribute__ ((no_split_stack));
+
+static void *
+stack_split_initialize_thread (void *varg)
+{
+  struct pthread_create_args *args = (struct pthread_create_args *) varg;
+  int err;
+  void *(*start_routine) (void *);
+  void *arg;
+
+  __stack_split_initialize ();
+
+  err = pthread_setspecific (segment_list_key, (void *) &__morestack_segments);
+  if (err != 0)
+    {
+      static const char msg[] = "pthread_setspecific failed: errno ";
+      __morestack_fail (msg, sizeof msg - 1, err);
+    }
+
+  start_routine = args->start_routine;
+  arg = args->arg;
+  free (args);
+  return (*start_routine) (arg);
+}
+
+/* This function wraps calls to pthread_create to make sure that the
+   stack guard is initialized for new threads.  FIXME: This hack will
+   not be necessary if glibc supports -fsplit-stack directly.  */
+
+int __wrap_pthread_create (pthread_t *, const pthread_attr_t *,
+                          void *(*start_routine) (void *), void *)
+  __attribute__ ((visibility ("hidden")));
+
+extern int __real_pthread_create (pthread_t *, const pthread_attr_t *,
+                                 void *(*start_routine) (void *), void *)
+  __attribute__ ((weak));
+
+int
+__wrap_pthread_create (pthread_t *tid, const pthread_attr_t *attr,
+                      void *(*start_routine) (void *), void *arg)
+{
+  int err;
+  struct pthread_create_args* args;
+
+  err = pthread_once (&create_key_once, create_key);
+  if (err != 0)
+    {
+      static const char msg[] = "pthread_once failed: errno ";
+      __morestack_fail (msg, sizeof msg - 1, err);
+    }
+
+  args = malloc (sizeof (struct pthread_create_args));
+  if (args == NULL)
+    return EAGAIN;
+  args->start_routine = start_routine;
+  args->arg = arg;
+  return __real_pthread_create (tid, attr, stack_split_initialize_thread, args);
+}
+
+#endif /* !defined (inhibit_libc) */
diff --git a/libgcc/generic-morestack.c b/libgcc/generic-morestack.c
new file mode 100644 (file)
index 0000000..3709d32
--- /dev/null
@@ -0,0 +1,876 @@
+/* Library support for -fsplit-stack.  */
+/* Copyright (C) 2009, 2010 Free Software Foundation, Inc.
+   Contributed by Ian Lance Taylor <iant@google.com>.
+
+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 "tconfig.h"
+#include "tsystem.h"
+#include "coretypes.h"
+#include "tm.h"
+
+/* If inhibit_libc is defined, we can not compile this file.  The
+   effect is that people will not be able to use -fsplit-stack.  That
+   is much better than failing the build particularly since people
+   will want to define inhibit_libc while building a compiler which
+   can build glibc.  */
+
+#ifndef inhibit_libc
+
+#include <assert.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include "generic-morestack.h"
+
+/* This file contains subroutines that are used by code compiled with
+   -fsplit-stack.  */
+
+/* Declare functions to avoid warnings--there is no header file for
+   these internal functions.  We give most of these functions the
+   flatten attribute in order to minimize their stack usage--here we
+   must minimize stack usage even at the cost of code size, and in
+   general inlining everything will do that.  */
+
+extern void
+__generic_morestack_set_initial_sp (void *sp, size_t len)
+  __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
+
+extern void *
+__generic_morestack (size_t *frame_size, void *old_stack, size_t param_size)
+  __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
+
+extern void *
+__generic_releasestack (size_t *pavailable)
+  __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
+
+extern void
+__morestack_block_signals (void)
+  __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
+
+extern void
+__morestack_unblock_signals (void)
+  __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
+
+extern size_t
+__generic_findstack (void *stack)
+  __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
+
+extern void
+__morestack_load_mmap (void)
+  __attribute__ ((no_split_stack, visibility ("hidden")));
+
+extern void *
+__morestack_allocate_stack_space (size_t size)
+  __attribute__ ((visibility ("hidden")));
+
+/* This is a function which -fsplit-stack code can call to get a list
+   of the stacks.  Since it is not called only by the compiler, it is
+   not hidden.  */
+
+extern void *
+__splitstack_find (void *, void *, size_t *, void **, void **, void **)
+  __attribute__ ((visibility ("default")));
+
+/* When we allocate a stack segment we put this header at the
+   start.  */
+
+struct stack_segment
+{
+  /* The previous stack segment--when a function running on this stack
+     segment returns, it will run on the previous one.  */
+  struct stack_segment *prev;
+  /* The next stack segment, if it has been allocated--when a function
+     is running on this stack segment, the next one is not being
+     used.  */
+  struct stack_segment *next;
+  /* The total size of this stack segment.  */
+  size_t size;
+  /* The stack address when this stack was created.  This is used when
+     popping the stack.  */
+  void *old_stack;
+  /* A list of memory blocks allocated by dynamic stack
+     allocation.  */
+  struct dynamic_allocation_blocks *dynamic_allocation;
+  /* A list of dynamic memory blocks no longer needed.  */
+  struct dynamic_allocation_blocks *free_dynamic_allocation;
+  /* An extra pointer in case we need some more information some
+     day.  */
+  void *extra;
+};
+
+/* This structure holds the (approximate) initial stack pointer and
+   size for the system supplied stack for a thread.  This is set when
+   the thread is created.  We also store a sigset_t here to hold the
+   signal mask while splitting the stack, since we don't want to store
+   that on the stack.  */
+
+struct initial_sp
+{
+  /* The initial stack pointer.  */
+  void *sp;
+  /* The stack length.  */
+  size_t len;
+  /* A signal mask, put here so that the thread can use it without
+     needing stack space.  */
+  sigset_t mask;
+  /* Some extra space for later extensibility.  */
+  void *extra[5];
+};
+
+/* A list of memory blocks allocated by dynamic stack allocation.
+   This is used for code that calls alloca or uses variably sized
+   arrays.  */
+
+struct dynamic_allocation_blocks
+{
+  /* The next block in the list.  */
+  struct dynamic_allocation_blocks *next;
+  /* The size of the allocated memory.  */
+  size_t size;
+  /* The allocated memory.  */
+  void *block;
+};
+
+/* These thread local global variables must be shared by all split
+   stack code across shared library boundaries.  Therefore, they have
+   default visibility.  They have extensibility fields if needed for
+   new versions.  If more radical changes are needed, new code can be
+   written using new variable names, while still using the existing
+   variables in a backward compatible manner.  Symbol versioning is
+   also used, although, since these variables are only referenced by
+   code in this file and generic-morestack-thread.c, it is likely that
+   simply using new names will suffice.  */
+
+/* The first stack segment allocated for this thread.  */
+
+__thread struct stack_segment *__morestack_segments
+  __attribute__ ((visibility ("default")));
+
+/* The stack segment that we think we are currently using.  This will
+   be correct in normal usage, but will be incorrect if an exception
+   unwinds into a different stack segment or if longjmp jumps to a
+   different stack segment.  */
+
+__thread struct stack_segment *__morestack_current_segment
+  __attribute__ ((visibility ("default")));
+
+/* The initial stack pointer and size for this thread.  */
+
+__thread struct initial_sp __morestack_initial_sp
+  __attribute__ ((visibility ("default")));
+
+/* A static signal mask, to avoid taking up stack space.  */
+
+static sigset_t __morestack_fullmask;
+
+/* Convert an integer to a decimal string without using much stack
+   space.  Return a pointer to the part of the buffer to use.  We this
+   instead of sprintf because sprintf will require too much stack
+   space.  */
+
+static char *
+print_int (int val, char *buf, int buflen, size_t *print_len)
+{
+  int is_negative;
+  int i;
+  unsigned int uval;
+
+  uval = (unsigned int) val;
+  if (val >= 0)
+    is_negative = 0;
+  else
+    {
+      is_negative = 1;
+      uval = - uval;
+    }
+
+  i = buflen;
+  do
+    {
+      --i;
+      buf[i] = '0' + (uval % 10);
+      uval /= 10;
+    }
+  while (uval != 0 && i > 0);
+
+  if (is_negative)
+    {
+      if (i > 0)
+       --i;
+      buf[i] = '-';
+    }
+
+  *print_len = buflen - i;
+  return buf + i;
+}
+
+/* Print the string MSG/LEN, the errno number ERR, and a newline on
+   stderr.  Then crash.  */
+
+void
+__morestack_fail (const char *, size_t, int) __attribute__ ((noreturn));
+
+void
+__morestack_fail (const char *msg, size_t len, int err)
+{
+  char buf[24];
+  static const char nl[] = "\n";
+  struct iovec iov[3];
+  union { char *p; const char *cp; } const_cast;
+
+  const_cast.cp = msg;
+  iov[0].iov_base = const_cast.p;
+  iov[0].iov_len = len;
+  /* We can't call strerror, because it may try to translate the error
+     message, and that would use too much stack space.  */
+  iov[1].iov_base = print_int (err, buf, sizeof buf, &iov[1].iov_len);
+  const_cast.cp = &nl[0];
+  iov[2].iov_base = const_cast.p;
+  iov[2].iov_len = sizeof nl - 1;
+  /* FIXME: On systems without writev we need to issue three write
+     calls, or punt on printing errno.  For now this is irrelevant
+     since stack splitting only works on GNU/Linux anyhow.  */
+  writev (2, iov, 3);
+  abort ();
+}
+
+/* Allocate a new stack segment.  FRAME_SIZE is the required frame
+   size.  */
+
+static struct stack_segment *
+allocate_segment (size_t frame_size)
+{
+  static unsigned int static_pagesize;
+  static int use_guard_page;
+  unsigned int pagesize;
+  unsigned int overhead;
+  unsigned int allocate;
+  void *space;
+  struct stack_segment *pss;
+
+  pagesize = static_pagesize;
+  if (pagesize == 0)
+    {
+      unsigned int p;
+
+      pagesize = getpagesize ();
+
+#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
+      p = __sync_val_compare_and_swap (&static_pagesize, 0, pagesize);
+#else
+      /* Just hope this assignment is atomic.  */
+      static_pagesize = pagesize;
+      p = 0;
+#endif
+
+      use_guard_page = getenv ("SPLIT_STACK_GUARD") != 0;
+
+      /* FIXME: I'm not sure this assert should be in the released
+        code.  */
+      assert (p == 0 || p == pagesize);
+    }
+
+  overhead = sizeof (struct stack_segment);
+
+  allocate = pagesize;
+  if (allocate < MINSIGSTKSZ)
+    allocate = ((MINSIGSTKSZ + overhead + pagesize - 1)
+               & ~ (pagesize - 1));
+  if (allocate < frame_size)
+    allocate = ((frame_size + overhead + pagesize - 1)
+               & ~ (pagesize - 1));
+
+  if (use_guard_page)
+    allocate += pagesize;
+
+  /* FIXME: If this binary requires an executable stack, then we need
+     to set PROT_EXEC.  Unfortunately figuring that out is complicated
+     and target dependent.  We would need to use dl_iterate_phdr to
+     see if there is any object which does not have a PT_GNU_STACK
+     phdr, though only for architectures which use that mechanism.  */
+  space = mmap (NULL, allocate, PROT_READ | PROT_WRITE,
+               MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+  if (space == MAP_FAILED)
+    {
+      static const char msg[] =
+       "unable to allocate additional stack space: errno ";
+      __morestack_fail (msg, sizeof msg - 1, errno);
+    }
+
+  if (use_guard_page)
+    {
+      void *guard;
+
+#ifdef STACK_GROWS_DOWNWARD
+      guard = space;
+      space = (char *) space + pagesize;
+#else
+      guard = space + allocate - pagesize;
+#endif
+
+      mprotect (guard, pagesize, PROT_NONE);
+      allocate -= pagesize;
+    }
+
+  pss = (struct stack_segment *) space;
+
+  pss->prev = __morestack_current_segment;
+  pss->next = NULL;
+  pss->size = allocate - overhead;
+  pss->dynamic_allocation = NULL;
+  pss->free_dynamic_allocation = NULL;
+  pss->extra = NULL;
+
+  if (__morestack_current_segment != NULL)
+    __morestack_current_segment->next = pss;
+  else
+    __morestack_segments = pss;
+
+  return pss;
+}
+
+/* Free a list of dynamic blocks.  */
+
+static void
+free_dynamic_blocks (struct dynamic_allocation_blocks *p)
+{
+  while (p != NULL)
+    {
+      struct dynamic_allocation_blocks *next;
+
+      next = p->next;
+      free (p->block);
+      free (p);
+      p = next;
+    }
+}
+
+/* Merge two lists of dynamic blocks.  */
+
+static struct dynamic_allocation_blocks *
+merge_dynamic_blocks (struct dynamic_allocation_blocks *a,
+                     struct dynamic_allocation_blocks *b)
+{
+  struct dynamic_allocation_blocks **pp;
+
+  if (a == NULL)
+    return b;
+  if (b == NULL)
+    return a;
+  for (pp = &a->next; *pp != NULL; pp = &(*pp)->next)
+    ;
+  *pp = b;
+  return a;
+}
+
+/* Release stack segments.  If FREE_DYNAMIC is non-zero, we also free
+   any dynamic blocks.  Otherwise we return them.  */
+
+struct dynamic_allocation_blocks *
+__morestack_release_segments (struct stack_segment **pp, int free_dynamic)
+{
+  struct dynamic_allocation_blocks *ret;
+  struct stack_segment *pss;
+
+  ret = NULL;
+  pss = *pp;
+  while (pss != NULL)
+    {
+      struct stack_segment *next;
+      unsigned int allocate;
+
+      next = pss->next;
+
+      if (pss->dynamic_allocation != NULL
+         || pss->free_dynamic_allocation != NULL)
+       {
+         if (free_dynamic)
+           {
+             free_dynamic_blocks (pss->dynamic_allocation);
+             free_dynamic_blocks (pss->free_dynamic_allocation);
+           }
+         else
+           {
+             ret = merge_dynamic_blocks (pss->dynamic_allocation, ret);
+             ret = merge_dynamic_blocks (pss->free_dynamic_allocation, ret);
+           }
+       }
+
+      allocate = pss->size + sizeof (struct stack_segment);
+      if (munmap (pss, allocate) < 0)
+       {
+         static const char msg[] = "munmap of stack space failed: errno ";
+         __morestack_fail (msg, sizeof msg - 1, errno);
+       }
+
+      pss = next;
+    }
+  *pp = NULL;
+
+  return ret;
+}
+
+/* This function is called by a processor specific function to set the
+   initial stack pointer for a thread.  The operating system will
+   always create a stack for a thread.  Here we record a stack pointer
+   near the base of that stack.  The size argument lets the processor
+   specific code estimate how much stack space is available on this
+   initial stack.  */
+
+void
+__generic_morestack_set_initial_sp (void *sp, size_t len)
+{
+  /* The stack pointer most likely starts on a page boundary.  Adjust
+     to the nearest 512 byte boundary.  It's not essential that we be
+     precise here; getting it wrong will just leave some stack space
+     unused.  */
+#ifdef STACK_GROWS_DOWNWARD
+  sp = (void *) ((((__UINTPTR_TYPE__) sp + 511U) / 512U) * 512U);
+#else
+  sp = (void *) ((((__UINTPTR_TYPE__) sp - 511U) / 512U) * 512U);
+#endif
+
+  __morestack_initial_sp.sp = sp;
+  __morestack_initial_sp.len = len;
+  sigemptyset (&__morestack_initial_sp.mask);
+
+  sigfillset (&__morestack_fullmask);
+#ifdef __linux__
+  /* On Linux, the first two real time signals are used by the NPTL
+     threading library.  By taking them out of the set of signals, we
+     avoiding copying the signal mask in pthread_sigmask.  More
+     importantly, pthread_sigmask uses less stack space on x86_64.  */
+  sigdelset (&__morestack_fullmask, __SIGRTMIN);
+  sigdelset (&__morestack_fullmask, __SIGRTMIN + 1);
+#endif
+}
+
+/* This function is called by a processor specific function which is
+   run in the prologue when more stack is needed.  The processor
+   specific function handles the details of saving registers and
+   frobbing the actual stack pointer.  This function is responsible
+   for allocating a new stack segment and for copying a parameter
+   block from the old stack to the new one.  On function entry
+   *PFRAME_SIZE is the size of the required stack frame--the returned
+   stack must be at least this large.  On function exit *PFRAME_SIZE
+   is the amount of space remaining on the allocated stack.  OLD_STACK
+   points at the parameters the old stack (really the current one
+   while this function is running).  OLD_STACK is saved so that it can
+   be returned by a later call to __generic_releasestack.  PARAM_SIZE
+   is the size in bytes of parameters to copy to the new stack.  This
+   function returns a pointer to the new stack segment, pointing to
+   the memory after the parameters have been copied.  The returned
+   value minus the returned *PFRAME_SIZE (or plus if the stack grows
+   upward) is the first address on the stack which should not be used.
+
+   This function is running on the old stack and has only a limited
+   amount of stack space available.  */
+
+void *
+__generic_morestack (size_t *pframe_size, void *old_stack, size_t param_size)
+{
+  size_t frame_size = *pframe_size;
+  struct stack_segment *current;
+  struct stack_segment **pp;
+  struct dynamic_allocation_blocks *dynamic;
+  char *from;
+  char *to;
+  void *ret;
+  size_t i;
+
+  current = __morestack_current_segment;
+
+  pp = current != NULL ? &current->next : &__morestack_segments;
+  if (*pp != NULL && (*pp)->size < frame_size)
+    dynamic = __morestack_release_segments (pp, 0);
+  else
+    dynamic = NULL;
+  current = *pp;
+
+  if (current == NULL)
+    current = allocate_segment (frame_size);
+
+  current->old_stack = old_stack;
+
+  __morestack_current_segment = current;
+
+  if (dynamic != NULL)
+    {
+      /* Move the free blocks onto our list.  We don't want to call
+        free here, as we are short on stack space.  */
+      current->free_dynamic_allocation =
+       merge_dynamic_blocks (dynamic, current->free_dynamic_allocation);
+    }
+
+  *pframe_size = current->size - param_size;
+
+#ifdef STACK_GROWS_DOWNWARD
+  {
+    char *bottom = (char *) (current + 1) + current->size;
+    to = bottom - param_size;
+    ret = bottom - param_size;
+  }
+#else
+  to = current + 1;
+  ret = (char *) (current + 1) + param_size;
+#endif
+
+  /* We don't call memcpy to avoid worrying about the dynamic linker
+     trying to resolve it.  */
+  from = (char *) old_stack;
+  for (i = 0; i < param_size; i++)
+    *to++ = *from++;
+
+  return ret;
+}
+
+/* This function is called by a processor specific function when it is
+   ready to release a stack segment.  We don't actually release the
+   stack segment, we just move back to the previous one.  The current
+   stack segment will still be available if we need it in
+   __generic_morestack.  This returns a pointer to the new stack
+   segment to use, which is the one saved by a previous call to
+   __generic_morestack.  The processor specific function is then
+   responsible for actually updating the stack pointer.  This sets
+   *PAVAILABLE to the amount of stack space now available.  */
+
+void *
+__generic_releasestack (size_t *pavailable)
+{
+  struct stack_segment *current;
+  void *old_stack;
+
+  current = __morestack_current_segment;
+  old_stack = current->old_stack;
+  current = current->prev;
+  __morestack_current_segment = current;
+
+  if (current != NULL)
+    {
+#ifdef STACK_GROWS_DOWNWARD
+      *pavailable = (char *) old_stack - (char *) (current + 1);
+#else
+      *pavailable = (char *) (current + 1) + current->size - (char *) old_stack;
+#endif
+    }
+  else
+    {
+      size_t used;
+
+      /* We have popped back to the original stack.  */
+#ifdef STACK_GROWS_DOWNWARD
+      if ((char *) old_stack >= (char *) __morestack_initial_sp.sp)
+       used = 0;
+      else
+       used = (char *) __morestack_initial_sp.sp - (char *) old_stack;
+#else
+      if ((char *) old_stack <= (char *) __morestack_initial_sp.sp)
+       used = 0;
+      else
+       used = (char *) old_stack - (char *) __morestack_initial_sp.sp;
+#endif
+
+      if (used > __morestack_initial_sp.len)
+       *pavailable = 0;
+      else
+       *pavailable = __morestack_initial_sp.len - used;
+    }
+
+  return old_stack;
+}
+
+/* Block signals while splitting the stack.  This avoids trouble if we
+   try to invoke a signal handler which itself wants to split the
+   stack.  */
+
+extern int pthread_sigmask (int, const sigset_t *, sigset_t *)
+  __attribute__ ((weak));
+
+void
+__morestack_block_signals (void)
+{
+  if (pthread_sigmask)
+    pthread_sigmask (SIG_BLOCK, &__morestack_fullmask,
+                    &__morestack_initial_sp.mask);
+  else
+    sigprocmask (SIG_BLOCK, &__morestack_fullmask,
+                &__morestack_initial_sp.mask);
+}
+
+/* Unblock signals while splitting the stack.  */
+
+void
+__morestack_unblock_signals (void)
+{
+  if (pthread_sigmask)
+    pthread_sigmask (SIG_SETMASK, &__morestack_initial_sp.mask, NULL);
+  else
+    sigprocmask (SIG_SETMASK, &__morestack_initial_sp.mask, NULL);
+}
+
+/* This function is called to allocate dynamic stack space, for alloca
+   or a variably sized array.  This is a regular function with
+   sufficient stack space, so we just use malloc to allocate the
+   space.  We attach the allocated blocks to the current stack
+   segment, so that they will eventually be reused or freed.  */
+
+void *
+__morestack_allocate_stack_space (size_t size)
+{
+  struct stack_segment *seg, *current;
+  struct dynamic_allocation_blocks *p;
+
+  /* We have to block signals to avoid getting confused if we get
+     interrupted by a signal whose handler itself uses alloca or a
+     variably sized array.  */
+  __morestack_block_signals ();
+
+  /* Since we don't want to call free while we are low on stack space,
+     we may have a list of already allocated blocks waiting to be
+     freed.  Release them all, unless we find one that is large
+     enough.  We don't look at every block to see if one is large
+     enough, just the first one, because we aren't trying to build a
+     memory allocator here, we're just trying to speed up common
+     cases.  */
+
+  current = __morestack_current_segment;
+  p = NULL;
+  for (seg = __morestack_segments; seg != NULL; seg = seg->next)
+    {
+      p = seg->free_dynamic_allocation;
+      if (p != NULL)
+       {
+         if (p->size >= size)
+           {
+             seg->free_dynamic_allocation = p->next;
+             break;
+           }
+
+         free_dynamic_blocks (p);
+         seg->free_dynamic_allocation = NULL;
+         p = NULL;
+       }
+    }
+
+  if (p == NULL)
+    {
+      /* We need to allocate additional memory.  */
+      p = malloc (sizeof (*p));
+      if (p == NULL)
+       abort ();
+      p->size = size;
+      p->block = malloc (size);
+      if (p->block == NULL)
+       abort ();
+    }
+
+  /* If we are still on the initial stack, then we have a space leak.
+     FIXME.  */
+  if (current != NULL)
+    {
+      p->next = current->dynamic_allocation;
+      current->dynamic_allocation = p;
+    }
+
+  __morestack_unblock_signals ();
+
+  return p->block;
+}
+
+/* Find the stack segment for STACK and return the amount of space
+   available.  This is used when unwinding the stack because of an
+   exception, in order to reset the stack guard correctly.  */
+
+size_t
+__generic_findstack (void *stack)
+{
+  struct stack_segment *pss;
+  size_t used;
+
+  for (pss = __morestack_current_segment; pss != NULL; pss = pss->prev)
+    {
+      if ((char *) pss < (char *) stack
+         && (char *) pss + pss->size > (char *) stack)
+       {
+         __morestack_current_segment = pss;
+#ifdef STACK_GROWS_DOWNWARD
+         return (char *) stack - (char *) (pss + 1);
+#else
+         return (char *) (pss + 1) + pss->size - (char *) stack;
+#endif
+       }
+    }
+
+  /* We have popped back to the original stack.  */
+#ifdef STACK_GROWS_DOWNWARD
+  if ((char *) stack >= (char *) __morestack_initial_sp.sp)
+    used = 0;
+  else
+    used = (char *) __morestack_initial_sp.sp - (char *) stack;
+#else
+  if ((char *) stack <= (char *) __morestack_initial_sp.sp)
+    used = 0;
+  else
+    used = (char *) stack - (char *) __morestack_initial_sp.sp;
+#endif
+
+  if (used > __morestack_initial_sp.len)
+    return 0;
+  else
+    return __morestack_initial_sp.len - used;
+}
+
+/* This function is called at program startup time to make sure that
+   mmap, munmap, and getpagesize are resolved if linking dynamically.
+   We want to resolve them while we have enough stack for them, rather
+   than calling into the dynamic linker while low on stack space.  */
+
+void
+__morestack_load_mmap (void)
+{
+  /* Call with bogus values to run faster.  We don't care if the call
+     fails.  Pass __MORESTACK_CURRENT_SEGMENT to make sure that any
+     TLS accessor function is resolved.  */
+  mmap (__morestack_current_segment, 0, PROT_READ, MAP_ANONYMOUS, -1, 0);
+  mprotect (NULL, 0, 0);
+  munmap (0, getpagesize ());
+}
+
+/* This function may be used to iterate over the stack segments.
+   This can be called like this.
+     void *next_segment = NULL;
+     void *next_sp = NULL;
+     void *initial_sp = NULL;
+     void *stack;
+     size_t stack_size;
+     while ((stack = __splitstack_find (next_segment, next_sp, &stack_size,
+                                        &next_segment, &next_sp,
+                                       &initial_sp)) != NULL)
+       {
+         // Stack segment starts at stack and is stack_size bytes long.
+       }
+
+   There is no way to iterate over the stack segments of a different
+   thread.  However, what is permitted is for one thread to call this
+   with the first two values NULL, to pass next_segment, next_sp, and
+   initial_sp to a different thread, and then to suspend one way or
+   another.  A different thread may run the subsequent
+   __morestack_find iterations.  Of course, this will only work if the
+   first thread is suspended during the __morestack_find iterations.
+   If not, the second thread will be looking at the stack while it is
+   changing, and anything could happen.
+
+   FIXME: This should be declared in some header file, but where?  */
+
+void *
+__splitstack_find (void *segment_arg, void *sp, size_t *len,
+                  void **next_segment, void **next_sp,
+                  void **initial_sp)
+{
+  struct stack_segment *segment;
+  void *ret;
+  char *nsp;
+
+  if (segment_arg == (void *) 1)
+    {
+      char *isp = (char *) *initial_sp;
+
+      *next_segment = (void *) 2;
+      *next_sp = NULL;
+#ifdef STACK_GROWS_DOWNWARD
+      if ((char *) sp >= isp)
+       return NULL;
+      *len = (char *) isp - (char *) sp;
+      return sp;
+#else
+      if ((char *) sp <= (char *) isp)
+       return NULL;
+      *len = (char *) sp - (char *) isp;
+      return (void *) isp;
+#endif
+    }
+  else if (segment_arg == (void *) 2)
+    return NULL;
+  else if (segment_arg != NULL)
+    segment = (struct stack_segment *) segment_arg;
+  else
+    {
+      *initial_sp = __morestack_initial_sp.sp;
+      segment = __morestack_current_segment;
+      sp = (void *) &segment;
+      while (1)
+       {
+         if (segment == NULL)
+           return __splitstack_find ((void *) 1, sp, len, next_segment,
+                                     next_sp, initial_sp);
+         if ((char *) sp >= (char *) (segment + 1)
+             && (char *) sp <= (char *) (segment + 1) + segment->size)
+           break;
+         segment = segment->prev;
+       }
+    }
+
+  if (segment->prev == NULL)
+    *next_segment = (void *) 1;
+  else
+    *next_segment = segment->prev;
+
+  /* The old_stack value is the address of the function parameters of
+     the function which called __morestack.  So if f1 called f2 which
+     called __morestack, the stack looks like this:
+
+         parameters       <- old_stack
+         return in f1
+        return in f2
+        data pushed by __morestack
+
+     On x86, the data pushed by __morestack includes the saved value
+     of the ebp/rbp register.  We want our caller to be able to see
+     that value, which can not be found on any other stack.  So we
+     adjust accordingly.  This may need to be tweaked for other
+     targets.  */
+
+  nsp = (char *) segment->old_stack;
+#ifdef STACK_GROWS_DOWNWARD
+  nsp -= 3 * sizeof (void *);
+#else
+  nsp += 3 * sizeof (void *);
+#endif
+  *next_sp = (void *) nsp;
+
+#ifdef STACK_GROWS_DOWNWARD
+  *len = (char *) (segment + 1) + segment->size - (char *) sp;
+  ret = (void *) sp;
+#else
+  *len = (char *) sp - (char *) (segment + 1);
+  ret = (void *) (segment + 1);
+#endif
+
+  return ret;
+}
+
+#endif /* !defined (inhibit_libc) */
diff --git a/libgcc/generic-morestack.h b/libgcc/generic-morestack.h
new file mode 100644 (file)
index 0000000..12da0ac
--- /dev/null
@@ -0,0 +1,53 @@
+/* Library support for -fsplit-stack.  */
+/* Copyright (C) 2009, 2010 Free Software Foundation, Inc.
+   Contributed by Ian Lance Taylor <iant@google.com>.
+
+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/>.  */
+
+/* This is a libgcc internal header file for functions shared between
+   generic-morestack.c and generic-morestack-thread.c.  The latter
+   file is only used when linking with the pthread library.  */
+
+/* The stack segment structure, defined in generic-morestack.c.  */
+
+struct stack_segment;
+
+/* The list of stack segments for this thread.  */
+
+extern __thread struct stack_segment *__morestack_segments;
+
+/* Print the string MSG/LEN, the errno number ERR, and a newline on
+   stderr, without using printf.  Then crash.  */
+
+extern void __morestack_fail (const char *msg, size_t len, int err)
+  __attribute__ ((noreturn, visibility ("hidden")));
+
+/* Release stack segments.  */
+
+extern struct dynamic_allocation_blocks *
+  __morestack_release_segments (struct stack_segment **, int)
+  __attribute__ ((visibility ("hidden")));
+
+/* Store the stack information in a processor dependent manner.  */
+
+extern void __stack_split_initialize (void)
+  __attribute__ ((visibility ("hidden")));