I get a segmentation fault from a memory allocation statement just because I have linked some unrelated procedures to the binary.
I have a very simple Fortran program:
program whatsoever !USE payload_modules double precision,allocatable:: Vmat(:,:,:) allocate(Vmat(2,2,2)) Vmat=1 write(*,*) Vmat deallocate (Vmat) ! some more lines of code using procedures from payload_module end program whatsoever
Compiling this using
gfortran whatsoever.f95 -o whatsoever leads to a program with the expected behaviour. Of course, this program is not made to print eight times
1.000 but to call the
payload_modules, yet hidden in the comments. However, if I compile and link the program with the modules issuing
gfortran -c -g -fPIC -ffpe-trap=overflow -pedantic -fbounds-check \ -fimplicit-none payload_module1.f90 payload_module2.f90 whatsever.f95 gcc -g -nostdlib -v -Wl,--verbose -std=gnu99 -shared -Wl,-Bsymbolic-functions \ -Wl,-z,relro -o whatsoever whatsoever.o payload_module1.o payload_module2.o
whatsoever doesn't run any more. I get a segmentation fault at the allocate statement. I have not yet uncommented the lines related to the modules (however, uncommenting them leads to the same behaviour)!
I know that the payload modules' code is not buggy because I ran it before from
R and wrapped this working code into a f90-module. There are no name collisions; nothing in the modules is called
Vmat. There is only one other call to
allocate in the modules. It never caused any trouble. There is still plenty of memory left. gdb didn't give me any hints expect a memory address.
How can linking routines that are actually not called crash a program?