MINGW-packages icon indicating copy to clipboard operation
MINGW-packages copied to clipboard

Undefined reference to `__chk_fail'

Open ajpenner opened this issue 1 year ago • 4 comments

Description / Steps to reproduce the issue

We are attempting to build our source code with code coverage enabled. During the linking phase we hit: C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/libgcov.a(_gcov.o):(.text+0x17a1): undefined reference to `__chk_fail'

I am uncertain which packages/libraries I need to include to get this working. I have read posts from 5 years ago suggesting that we need to explicitly add '-lssp' or -fstack-protector, neither of these options worked. Likewise using -D_FORTIFY_SOURCE=0 was not a solution.

I am using the latest mingw packages for gcc (13.2.0-3)

Expected behavior

The linker to successfully produce a binary

Actual behavior

The linker cannot produce a binary because of a missing definition

Verification

  • [X] I have verified that my MSYS2 is up-to-date before submitting the report (see https://www.msys2.org/docs/updating/)

Windows Version

MSYS_NT-10.0-19045

MINGW environments affected

  • [ ] MINGW64
  • [X] MINGW32
  • [ ] UCRT64
  • [ ] CLANG64
  • [ ] CLANG32
  • [ ] CLANGARM64

Are you willing to submit a PR?

I am willing to, but I need to know where the problem comes from.

ajpenner avatar Feb 02 '24 12:02 ajpenner

Could you provide any minimal sample to reproduce the same error?

Biswa96 avatar Feb 02 '24 12:02 Biswa96

I will see what I can do about that. The code causing the problem at the moment is proprietary, I will be free to provide a sample in a week or so.

Thanks for the quick response.

ajpenner avatar Feb 02 '24 12:02 ajpenner

'-lssp' or -fstack-protector

-lssp no longer does anything since that code is now included in mingw-w64 and we just provide an empty static archive so it doesn't error, and -fstack-protector doesn't change anything linker related I think (not sure).

The error seems to be related to gcov, so maybe try building without gcov intrumentation as a test (remove -fprofile-arcs -ftest-coverage --coverage etc)

No idea, somehow it's not linking in the mingw-w64 provided things.

lazka avatar Feb 02 '24 13:02 lazka

@lazka I did try a test without the gcov instrumentation and the build worked as expected. I agree that this is isolated to gcov.

ajpenner avatar Feb 02 '24 20:02 ajpenner

Sorry about the long delay, please find below a minimal sample of the problem cat sample.c

#include <stdio.h>

int main(int argc, char* argv[])
{
    printf("Hello World");
    return 0;
}

gcc -v -fprofile-arcs -ftest-coverage sample.c

Using built-in specs.
COLLECT_GCC=C:\msys64\mingw32\bin\gcc.exe
COLLECT_LTO_WRAPPER=C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with: ../gcc-13.2.0/configure --prefix=/mingw32 --with-local-prefix=/mingw32/local --build=i686-w64-mingw32 --host=i686-w64-mingw32 --target=i686-w64-mingw32 --with-native-system-header-dir=/mingw32/include --libexecdir=/mingw32/lib --enable-bootstrap --enable-checking=release --with-arch=pentium4 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++,jit --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts --enable-libstdcxx-time --disable-libstdcxx-pch --enable-lto --enable-libgomp --disable-libssp --disable-multilib --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw32 --with-mpfr=/mingw32 --with-mpc=/mingw32 --with-isl=/mingw32 --with-pkgversion='Rev5, Built by MSYS2 project' --with-bugurl=https://github.com/msys2/MINGW-packages/issues --with-gnu-as --with-gnu-ld --disable-libstdcxx-debug --disable-sjlj-exceptions --with-dwarf2 --with-boot-ldflags=-static-libstdc++ --with-stage1-ldflags=-static-libstdc++
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 13.2.0 (Rev5, Built by MSYS2 project)
COLLECT_GCC_OPTIONS='-v' '-fprofile-arcs' '-ftest-coverage' '-mtune=generic' '-march=pentium4' '-dumpdir' 'a-'
 C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/cc1.exe -quiet -v -iprefix C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/ -D_REENTRANT sample.c -quiet -dumpdir a- -dumpbase sample.c -dumpbase-ext .c -mtune=generic -march=pentium4 -version -fprofile-arcs -ftest-coverage -o C:\msys64\tmp\ccJfkXnN.s
GNU C17 (Rev5, Built by MSYS2 project) version 13.2.0 (i686-w64-mingw32)
        compiled by GNU C version 13.2.0, GMP version 6.3.0, MPFR version 4.2.1, MPC version 1.3.1, isl version isl-0.26-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../i686-w64-mingw32/include"
ignoring duplicate directory "C:/msys64/mingw32/lib/gcc/../../lib/gcc/i686-w64-mingw32/13.2.0/include"
ignoring nonexistent directory "D:/a/msys64/mingw32/include"
ignoring nonexistent directory "/mingw32/include"
ignoring duplicate directory "C:/msys64/mingw32/lib/gcc/../../lib/gcc/i686-w64-mingw32/13.2.0/include-fixed"
ignoring nonexistent directory "C:/msys64/mingw32/lib/gcc/../../lib/gcc/i686-w64-mingw32/13.2.0/../../../../i686-w64-mingw32/include"
ignoring nonexistent directory "D:/a/msys64/mingw32/include"
#include "..." search starts here:
#include <...> search starts here:
 C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/include
 C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../include
 C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/include-fixed
End of search list.
Compiler executable checksum: 6f452a702c264c2ff539aa00c1a58dec
COLLECT_GCC_OPTIONS='-v' '-fprofile-arcs' '-ftest-coverage' '-mtune=generic' '-march=pentium4' '-dumpdir' 'a-'
 C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../i686-w64-mingw32/bin/as.exe -v -o C:\msys64\tmp\ccf5eMO7.o C:\msys64\tmp\ccJfkXnN.s
GNU assembler version 2.42 (i686-w64-mingw32) using BFD version (GNU Binutils) 2.42
COMPILER_PATH=C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/;C:/msys64/mingw32/bin/../lib/gcc/;C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../i686-w64-mingw32/bin/
LIBRARY_PATH=C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/;C:/msys64/mingw32/bin/../lib/gcc/;C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../i686-w64-mingw32/lib/../lib/;C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../lib/;C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../i686-w64-mingw32/lib/;C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../
COLLECT_GCC_OPTIONS='-v' '-fprofile-arcs' '-ftest-coverage' '-mtune=generic' '-march=pentium4' '-dumpdir' 'a.'
 C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/collect2.exe -plugin C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/liblto_plugin.dll -plugin-opt=C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\msys64\tmp\ccprQbFQ.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lpthread -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lkernel32 -m i386pe -Bdynamic C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../lib/crt2.o C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/crtbegin.o -LC:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0 -LC:/msys64/mingw32/bin/../lib/gcc -LC:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../i686-w64-mingw32/lib/../lib -LC:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../lib -LC:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../i686-w64-mingw32/lib -LC:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../.. C:\msys64\tmp\ccf5eMO7.o -lgcov -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lkernel32 -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lkernel32 C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../lib/default-manifest.o C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/crtend.o
C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/../../../../i686-w64-mingw32/bin/ld.exe: C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/13.2.0/libgcov.a(_gcov.o):(.text+0x17a1): undefined reference to `__chk_fail'
collect2.exe: error: ld returned 1 exit status

ajpenner avatar Apr 25 '24 05:04 ajpenner

I can not reproduce the issue with a clean msys2 installation. Please try with a clean msys2 installation and update the packages.

image

Biswa96 avatar Apr 25 '24 05:04 Biswa96

The issue is not with gcc compiling, it is with adding the gcov. I agree that simply running gcc on the code works as I would expect

ajpenner avatar Apr 25 '24 05:04 ajpenner

I can not reproduce the issue with gcc -v -fprofile-arcs -ftest-coverage sample.c command also.

Biswa96 avatar Apr 25 '24 06:04 Biswa96