Nested functions segfault with musl

This gets pretty in the weeds, and I’m not sure where to go further to try to figure out this issue. I’ve got a minimal example set up here: GitHub - nbelakovski/fortran_nested_func_musl: An example of calling a nested function in Fortran which breaks with musl

Basically having a nested function, even one which doesn’t capture anything from its parent function, leads to a segault in musl, but only when compiled and linked as a shared library. When compiled statically everything is fine.

I don’t really know much about musl vs glibc, does anyone have any ideas on where to go from here? I tried throwing it into gdb, but all I got were ?? everywhere :frowning:

I would appreciate any help or insight anyone could provide.

2 Likes

Hi, @actinium226.

When I checked main_shared using the ldd command for the code in the repository you showed us, I got the following result:

% ldd main_shared
        /lib/ld-musl-x86_64.so.1 (0x7f17288af000)
        libalgorithm.so => /home/shinobu/fortran_nested_func_musl/libalgorithm.so (0x7f17288a5000)
        libgfortran.so.5 => /usr/lib/gcc/x86_64-pc-linux-musl/13/libgfortran.so.5 (0x7f17285c4000)
        libgcc_s.so.1 => /usr/lib/gcc/x86_64-pc-linux-musl/13/libgcc_s.so.1 (0x7f17285a1000)
        libquadmath.so.0 => /usr/lib/gcc/x86_64-pc-linux-musl/13/libquadmath.so.0 (0x7f1728559000)
        libc.so => /lib/ld-musl-x86_64.so.1 (0x7f17288af000)

Here, the ??? that appears in the backtrace when a segmentation fault occurs probably suggests that the problem occurred in a section of code contained in libc, libgfortran, or similar. To make this ??? more informative, you should use GCC or Musl libc built with debug options (i.e. GCC’s -g or -g3).

I’m not familiar with Alpine Linux, but are there any packages or options in the package manager for this distribution that provide such binaries?

Additionally, besides gdb, the valgrind command may be helpful for investigating this kind of problem.