Hi Maciej: Thanks for your patch, I tried to compile all target libraries with -O0 and got link error as you describe, it's really confusing about the error message, __pthread_mutex_lock and __pthread_mutex_unlock are undefined, it turns out the root cause is the __umoddi3 called _Unwind_Resume and pull-in lots of functions, so really appreciate you digging out this issue.
After figure out what happen and reproduce what you see, I spend times to dig out the reason why -fexceptions -fnon-call-exceptions is set for div and mod routines, it was introduce at many years ago[1] and no much comment on why doing this, but I guess it's because some ISAs might generate exception/trap when divide-by-0, and then the execution environment will convert that to an exception, so those files/routines need to compile with such flags. However RISC-V never causes a trap when divide-by-0, so I believe -fexceptions -fnon-call-exceptions could be removed safely for RISC-V, but I am not sure it's safe for all other targets or not. I would suggest you set LIB2_DIVMOD_EXCEPTION_FLAGS to empty or '-fasynchronous-unwind-tables' for RISC-V port and do not change the default one in libgcc/Makefile.in. [1] https://github.com/gcc-mirror/gcc/commit/fc6aa0a98a0c7d10d39dd9d1485d0996b84b1860#diff-f98ad72e54bdf47d90acbfafa467d802R132 On Fri, Aug 21, 2020 at 2:46 AM Maciej W. Rozycki via Gcc-patches <gcc-patches@gcc.gnu.org> wrote: > > Complement commit b932f770f70d ("x86_64 frame unwind info"), SVN r46374, > <https://gcc.gnu.org/ml/gcc-patches/2001-10/msg00860.html>, and replace > `-fexceptions -fnon-call-exceptions' with `-fasynchronous-unwind-tables' > in LIB2_DIVMOD_FUNCS compilation flags so as to provide unwind tables > for the affected functions while not pulling the unwinder proper, which > is not required here. > > Beyond saving program space it fixes a RISC-V glibc build error due to > unsatisfied `malloc' and `free' references from the unwinder causing > link errors with `ld.so' where libgcc has been built at -O0. > > gcc/ > * testsuite/gcc.target/arm/div64-unwinding.c: Rename to... > * testsuite/gcc.dg/div64-unwinding.c: ... this. > > libgcc/ > * Makefile.in [!LIB2_DIVMOD_EXCEPTION_FLAGS] > (LIB2_DIVMOD_EXCEPTION_FLAGS): Replace `-fexceptions > -fnon-call-exceptions' with `-fasynchronous-unwind-tables'. > --- > Hi, > > No change from v2 except for the removal of the ARM parts; hence no need > to retest. OK to apply? > > Maciej > > Changes from v2: > > - Removal of the ARM overrides removed. > > Changes from v1: > > - ChangeLog entries added. > --- > gcc/testsuite/gcc.dg/div64-unwinding.c | 25 > +++++++++++++++++++++++++ > gcc/testsuite/gcc.target/arm/div64-unwinding.c | 25 > ------------------------- > libgcc/Makefile.in | 2 +- > 3 files changed, 26 insertions(+), 26 deletions(-) > > gcc-libgcc-divmod-asynchronous-unwind-tables.diff > Index: gcc/gcc/testsuite/gcc.dg/div64-unwinding.c > =================================================================== > --- /dev/null > +++ gcc/gcc/testsuite/gcc.dg/div64-unwinding.c > @@ -0,0 +1,25 @@ > +/* Performing a 64-bit division should not pull in the unwinder. */ > + > +/* { dg-do run { target { { ! *-*-linux* } && { ! *-*-uclinux* } } } } */ > +/* { dg-skip-if "load causes weak symbol resolution" { vxworks_kernel } } */ > +/* { 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); > +} > Index: gcc/gcc/testsuite/gcc.target/arm/div64-unwinding.c > =================================================================== > --- gcc.orig/gcc/testsuite/gcc.target/arm/div64-unwinding.c > +++ /dev/null > @@ -1,25 +0,0 @@ > -/* Performing a 64-bit division should not pull in the unwinder. */ > - > -/* { dg-do run { target { { ! *-*-linux* } && { ! *-*-uclinux* } } } } */ > -/* { dg-skip-if "load causes weak symbol resolution" { vxworks_kernel } } */ > -/* { 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); > -} > Index: gcc/libgcc/Makefile.in > =================================================================== > --- gcc.orig/libgcc/Makefile.in > +++ gcc/libgcc/Makefile.in > @@ -533,7 +533,7 @@ 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 > +LIB2_DIVMOD_EXCEPTION_FLAGS := -fasynchronous-unwind-tables > endif > > # Build LIB2_DIVMOD_FUNCS.