Hi, This (old!) patch avoids uselessly pulling in the unwinder for 64-bit division routines. I last posted it here:
http://gcc.gnu.org/ml/gcc-patches/2009-10/msg01618.html Other people have noticed the same issue, e.g.: http://gcc.gnu.org/ml/gcc-help/2011-03/msg00187.html Note that we take special care already for the case where division-by-zero throws an exception: we tailcall the division-by-zero handler at the head of the 64-bit division helper routines, so never create a stack frame which needs to be unwound. On ARM, division by zero can trapped by defining magic __aeabi_[il]div0 functions. See here: http://gcc.gnu.org/ml/gcc-patches/2009-10/msg01661.html Unfortunately, despite that, throwing an exception from the division-by-zero handler functions doesn't appear to work at present -- either with or without the current patch. I'm not intending to tackle that now (see attached program -- which exits with "terminate called after throwing an instance of 'int'" for me). I'm no C++ expert, so I might have done something dumb :-). Anyway: this revised version of the patch removes the strange libgcc Makefile-fragment changes, the equivalent of which have since been incorporated into mainline GCC now anyway, so the patch is somewhat more straightforward than it was previously. The patch allows a trivial program using 64-bit division (on bare metal) to shrink quite considerably: $ arm-none-eabi-size div64 text data bss dec hex filename 13120 2404 276 15800 3db8 div64 $ arm-none-eabi-size div64-unpatched text data bss dec hex filename 20064 2404 276 22744 58d8 div64-unpatched OK to apply? Thanks, Julian ChangeLog libgcc/ * config.host (arm*-*-linux-*eabi, arm*-*-uclinux*eabi) (arm*-*-eabi*, arm*-*-symbianelf*, arm*-*-rtemseabi*): Add arm/t-divmod-ef to tmake_file. * Makefile.in (LIB2_DIVMOD_EXCEPTION_FLAGS): Default to -fexceptions -fnon-call-exceptions if not defined. ($(lib2-divmod-o), $(lib2-divmod-s-o)): Use above. * config/arm/t-divmod-ef: New file. gcc/testsuite/ * gcc.target/arm/div64-unwinding.c: New test.
#include <cstdio> long long a = 100, b = 0; extern "C" long long __aeabi_ldiv0 (long long retval) { throw 0; } int bad_division (void) { return a / b; } int main(int argc, char *argv[]) { try { bad_division (); } catch (int e) { fprintf (stderr, "caught division by zero\n"); return 0; } fprintf (stderr, "didn't catch exception\n"); return 1; }
Index: libgcc/config.host =================================================================== --- libgcc/config.host (revision 189656) +++ libgcc/config.host (working copy) @@ -317,7 +317,7 @@ arm*-*-linux*) # ARM GNU/Linux with EL tmake_file="${tmake_file} arm/t-arm t-fixedpoint-gnu-prefix" case ${host} in arm*-*-linux-*eabi) - tmake_file="${tmake_file} arm/t-elf arm/t-bpabi arm/t-linux-eabi t-slibgcc-libgcc" + tmake_file="${tmake_file} arm/t-elf arm/t-bpabi arm/t-linux-eabi arm/t-divmod-ef t-slibgcc-libgcc" tm_file="$tm_file arm/bpabi-lib.h" unwind_header=config/arm/unwind-arm.h ;; @@ -331,7 +331,7 @@ arm*-*-uclinux*) # ARM ucLinux tmake_file="${tmake_file} t-fixedpoint-gnu-prefix" case ${host} in arm*-*-uclinux*eabi) - tmake_file="${tmake_file} arm/t-bpabi" + tmake_file="${tmake_file} arm/t-bpabi arm/t-divmod-ef" tm_file="$tm_file arm/bpabi-lib.h" unwind_header=config/arm/unwind-arm.h ;; @@ -344,7 +344,7 @@ arm*-*-ecos-elf) extra_parts="$extra_parts crti.o crtn.o" ;; arm*-*-eabi* | arm*-*-symbianelf* | arm*-*-rtemseabi*) - tmake_file="${tmake_file} arm/t-arm arm/t-elf t-fixedpoint-gnu-prefix" + tmake_file="${tmake_file} arm/t-arm arm/t-elf arm/t-divmod-ef t-fixedpoint-gnu-prefix" tm_file="$tm_file arm/bpabi-lib.h" case ${host} in arm*-*-eabi* | arm*-*-rtemseabi*) Index: libgcc/Makefile.in =================================================================== --- libgcc/Makefile.in (revision 189656) +++ libgcc/Makefile.in (working copy) @@ -497,18 +497,24 @@ libgcc-s-objects += $(patsubst %,%_s$(ob endif endif +ifeq ($(LIB2_DIVMOD_EXCEPTION_FLAGS),) +# Provide default flags for compiling divmod functions, if they haven't been +# set already by a target-specific Makefile fragment. +LIB2_DIVMOD_EXCEPTION_FLAGS := -fexceptions -fnon-call-exceptions +endif + # Build LIB2_DIVMOD_FUNCS. lib2-divmod-o = $(patsubst %,%$(objext),$(LIB2_DIVMOD_FUNCS)) $(lib2-divmod-o): %$(objext): $(srcdir)/libgcc2.c $(gcc_compile) -DL$* -c $< \ - -fexceptions -fnon-call-exceptions $(vis_hide) + $(LIB2_DIVMOD_EXCEPTION_FLAGS) $(vis_hide) libgcc-objects += $(lib2-divmod-o) ifeq ($(enable_shared),yes) lib2-divmod-s-o = $(patsubst %,%_s$(objext),$(LIB2_DIVMOD_FUNCS)) $(lib2-divmod-s-o): %_s$(objext): $(srcdir)/libgcc2.c $(gcc_s_compile) -DL$* -c $< \ - -fexceptions -fnon-call-exceptions + $(LIB2_DIVMOD_EXCEPTION_FLAGS) libgcc-s-objects += $(lib2-divmod-s-o) endif Index: libgcc/config/arm/t-divmod-ef =================================================================== --- libgcc/config/arm/t-divmod-ef (revision 0) +++ libgcc/config/arm/t-divmod-ef (revision 0) @@ -0,0 +1,4 @@ +# On ARM, specifying -fnon-call-exceptions will needlessly pull in +# the unwinder in simple programs which use 64-bit division. Omitting +# the option is safe. +LIB2_DIVMOD_EXCEPTION_FLAGS := -fexceptions Index: gcc/testsuite/gcc.target/arm/div64-unwinding.c =================================================================== --- gcc/testsuite/gcc.target/arm/div64-unwinding.c (revision 0) +++ gcc/testsuite/gcc.target/arm/div64-unwinding.c (revision 0) @@ -0,0 +1,24 @@ +/* Performing a 64-bit division should not pull in the unwinder. */ + +/* { dg-do run } */ +/* { dg-options "-O0" } */ + +#include <stdlib.h> + +long long +foo (long long c, long long d) +{ + return c/d; +} + +long long x = 0; +long long y = 1; + +extern int (*_Unwind_RaiseException) (void *) __attribute__((weak)); + +int main(void) +{ + if (&_Unwind_RaiseException != NULL) + abort ();; + return foo (x, y); +}