Hi, this is the second patch in a series that stems from the split of: http://gcc.gnu.org/ml/gcc-patches/2008-03/msg01853.html the first patch was installed about one year ago(!): http://gcc.gnu.org/ml/gcc-patches/2008-05/msg02041.html This implements functional stack checking for most x86 and x86-64 targets, including Linux, the notable exception being Windows (to be submitted later). It is functional in the sense that compiling code with -fstack-check should produce working executables that run as usual except that they check the stack before using it, whatever the language. This is very similar to what all programs do on Tru64 or Windows for example. The main difference is that the ckecking is designed to make it possible to recover from a stack overflow. This recovery requires support in the runtime and is only implemented for Ada; for the other languages, a signal will be raised instead. The main items of the patch are: 1. rewrite of the generic stack checking code in explow.c; the existing code is subject to wrap-arounds and can fail to detect a stack overflow at the very beginning or very end of the address space. 2. introduction of a variant of the probing method: probing with moving stack pointer; this is required for Linux. 3. introduction of a third sub-method (STACK_CHECK_PROBE_IOR) used for x86 and x86-64. 4. introduction of a new method: checking against a symbol; for VxWorks. 5. implementation of the probing, the probing with moving stack pointer and the checking against a symbol methods in the i386 back-end. 6. a couple of tweaks to the x86 DWARF-2 unwinder: a fixup for x86 kernels that don't have the S flag for signal frames and a tweak to help the unwinder to disambiguate contexts in the presence of signal frames. 7. a fix for libada: proper detection of _Unwind_GetIPInfo like for the other libraries. The whole ACATS testsuite compiled with -fstack-check passes with this patch on i586-suse-linux and x86_64-suse-linux. Tested on i586-suse-linux and x86_64-suse-linux, OK for mainline? 2009-08-04 Eric Botcazou PR target/10127 PR ada/20548 * expr.h (STACK_CHECK_PROBE_INTERVAL): Delete. (STACK_CHECK_PROBE_INTERVAL_EXP): New macro. (STACK_CHECK_PROBE_IOR): Likewise. (STACK_CHECK_MOVING_SP): Likewise. * system.h (STACK_CHECK_PROBE_INTERVAL): Poison it. * doc/tm.texi (Stack Checking): Delete STACK_CHECK_PROBE_INTERVAL. Document STACK_CHECK_PROBE_INTERVAL_EXP, STACK_CHECK_PROBE_IOR and STACK_CHECK_MOVING_SP. * calls.c (emit_library_call_value_1): Clear the ECF_NOTHROW flag if the libcall is LCT_MAY_THROW. * explow.c (anti_adjust_stack_and_probe): New function. (allocate_dynamic_stack_space): Do not directly allocate space if STACK_CHECK_MOVING_SP, instead invoke above function. (set_stack_check_libfunc): Delete. (stack_check_libfunc): Make public. (stack_check_symbol): New public variable. (emit_stack_probe): Deal with STACK_CHECK_PROBE_IOR. (PROBE_INTERVAL): New macro. (STACK_GROW_OPTAB): Likewise. (STACK_HIGH, STACK_LOW): Likewise. (probe_stack_range): Cope with SPARC_STACK_BIAS. Pass LCT_MAY_THROW to emit_library_call for the checking routine. Remove support code for dedicated pattern. Add support for stack limits provided by the stack_check_symbol variable. Fix loop condition in the small constant case. Rewrite in the general case to be immune to wrap-around. Make sure the address of probes is valid. Try to use [base + disp] addressing mode as much as possible. Do not include gt-explow.h. * ira.c (setup_eliminable_regset): Set frame_pointer_needed if stack checking is enabled and STACK_CHECK_MOVING_SP. * rtlanal.c (may_trap_p_1) : If stack checking is enabled, return 1 for volatile references to the stack pointer. * rtl.h (set_stack_check_libfunc): Delete. (stack_check_libfunc): Declare. (stack_check_symbol): Likewise. (libcall_type enum): Add LCT_MAY_THROW. * tree.c (build_common_builtin_nodes): Do not set ECF_NOTHROW on __builtin_alloca if stack checking is enabled. * Makefile.in (explow.o): Remove gt-explow.h. * unwind-dw2.c (uw_identify_context): Take into account whether the context is that of a signal frame or not. * config/i386/linux-unwind.h (x86_frob_update_context): New function. (MD_FROB_UPDATE_CONTEXT): Define. * config/i386/freebsd.h (STACK_CHECK_STATIC_BUILTIN): Likewise. * config/i386/linux.h (STACK_CHECK_STATIC_BUILTIN): Likewise. (STACK_CHECK_MOVING_SP): Likewise. * config/i386/linux64.h (STACK_CHECK_STATIC_BUILTIN): Likewise. (STACK_CHECK_MOVING_SP): Likewise. * config/i386/lynx.h (STACK_CHECK_STATIC_BUILTIN): Likewise. * config/i386/sol2.h (STACK_CHECK_STATIC_BUILTIN): Likewise. * config/i386/vxworks.h (STACK_CHECK_STATIC_BUILTIN): Likewise. (STACK_CHECK_PROTECT): 8K is reserved in the stack to propagate exceptions reliably in case of stack overflow. * config/i386/vxworksae.h (STACK_CHECK_PROTECT): Redefine to 4K which is enough for executing a possible last chance handler. * config/i386/i386.h (STACK_CHECK_PROBE_IOR): Define to 1. * config/i386/i386.c (ix86_compute_frame_layout): Force use of push instructions to save registers if stack checking with probes is on. (get_scratch_register_on_entry): New function. (release_scratch_register_on_entry): Likewise. (output_probe_op): Likewise. (output_adjust_stack_and_probe_op): Likewise. (output_adjust_stack_and_probe): Likewise. (ix86_gen_adjust_stack_and_probe): Likewise. (ix86_adjust_stack_and_probe): Likewise. (output_cond_trap): Likewise. (output_probe_stack_range_op): Likewise. (ix86_gen_probe_stack_range): Likewise. (ix86_emit_probe_stack_range): Likewise. (ix86_expand_prologue): Emit stack checking code if static builtin stack checking is enabled. Test ix86_target_stack_probe instead of TARGET_STACK_PROBE. * config/i386/i386-protos.h (ix86_target_stack_probe): Declare. (output_adjust_stack_and_probe): Likewise. (output_cond_trap): Likewise. (output_probe_stack_range): Likewise. * config/i386/i386.md (UNSPECV_STACK_PROBE_INLINE): New constant. (adjust_stack_and_probe): New insn. (probe_stack_range): Likewise. (logical operation peepholes): Do not split stack checking probes. (cond_trap): New insn. ada/ * gcc-interface/Makefile.in: Pass GNATLIBCFLAGS_FOR_C to recursive invocations. * gcc-interface/trans.c (gigi): Set stack_check_symbol to __gnat_stack_limit on selected platforms. 2009-08-04 Eric Botcazou * Makefile.in (GNATLIBCFLAGS_FOR_C): New variable. (LIBADA_FLAGS_TO_PASS): Add GNATLIBCFLAGS_FOR_C. * configure.ac: Include config/unwind_ipinfo.m4. Check for _Unwind_GetIPInfo. * configure: Regenerate. 2009-08-04 Eric Botcazou * gnat.dg/stack_check.adb: New test. -- Eric Botcazou