gcc/libgcc/generic-morestack-thread.c
Ian Lance Taylor 7458026b01 common.opt (fsplit-stack): New option.
gcc/:
	* 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.

From-SVN: r164661
2010-09-27 19:48:20 +00:00

163 lines
4.8 KiB
C

/* 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) */