Subject: bug#56839: Use of -nostdlib for C++ links interferes with -fsanitize

From: Jan Engelhardt <[email-masked]>
Date: Sat, 30 Jul 2022 11:42:44 +0200 (CEST)

Greetings.

libtool used to not work very well with passing -fsanitize. There was a 
fix to that (commit a5c6466528c060cc4660ad0319c00740db0e42ba).
However, when C++ sources are involved, there is still a problem
with the current libtool 2.4.7, because it uses -nostdlib, which
makes the gcc driver drop the implied -lasan.

$ cat x.c
void f(int *p) { *p = 21; }

$ libtool --mode=compile --tag=CC gcc -fsanitize=address -c x.c
libtool: compile:  gcc -fsanitize=address -c x.c  -fPIC -DPIC -o .libs/x.o
libtool: compile:  gcc -fsanitize=address -c x.c -o x.o >/dev/null 2>&1

$ libtool --mode=link --tag=CC gcc -o libx.la -fsanitize=address -rpath /usr/lib64 x.lo
libtool: link: rm -fr  .libs/libx.a .libs/libx.la .libs/libx.lai .libs/libx.so .libs/libx.so.0 .libs/libx.so.0.0.0
libtool: link: gcc -shared  -fPIC -DPIC  .libs/x.o    -fsanitize=address   -Wl,-soname -Wl,libx.so.0 -o .libs/libx.so.0.0.0
libtool: link: (cd ".libs" && rm -f "libx.so.0" && ln -s "libx.so.0.0.0" "libx.so.0")
libtool: link: (cd ".libs" && rm -f "libx.so" && ln -s "libx.so.0.0.0" "libx.so")
libtool: link: ar cr .libs/libx.a  x.o
libtool: link: ranlib .libs/libx.a
libtool: link: ( cd ".libs" && rm -f "libx.la" && ln -s "../libx.la" "libx.la" )

$ ldd -r .libs/libx.so
        linux-vdso.so.1 (0x00007ffc1b58e000)
        libasan.so.8 => /lib64/libasan.so.8 (0x00007f03fb400000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f03fb000000)
        libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f03fac00000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f03fb31a000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f03fb2f9000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f03fbac6000)

$ mv x.c x.cpp
$ cat x.cpp
void f(int *p) { *p = 21; }
$ libtool --mode=compile --tag=CXX g++ -fsanitize=address -c x.cpp
libtool: compile:  g++ -fsanitize=address -c x.cpp  -fPIC -DPIC -o .libs/x.o
libtool: compile:  g++ -fsanitize=address -c x.cpp -o x.o >/dev/null 2>&1
$ libtool --mode=link --tag=CXX g++ -o libx.la -fsanitize=address -rpath /usr/lib64 x.lo
libtool: link: rm -fr  .libs/libx.a .libs/libx.la .libs/libx.lai .libs/libx.so .libs/libx.so.0 .libs/libx.so.0.0.0
libtool: link: g++  -fPIC -DPIC -shared -nostdlib /usr/lib64/gcc/x86_64-suse-linux/12/../../../../lib64/crti.o /usr/lib64/gcc/x86_64-suse-linux/12/crtbeginS.o  .libs/x.o   -L/usr/lib64/gcc/x86_64-suse-linux/12 -L/usr/lib64/gcc/x86_64-suse-linux/12/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib64/gcc/x86_64-suse-linux/12/../../../../x86_64-suse-linux/lib -L/usr/lib64/gcc/x86_64-suse-linux/12/../../.. -lstdc++ -lm -lc -lgcc_s /usr/lib64/gcc/x86_64-suse-linux/12/crtendS.o /usr/lib64/gcc/x86_64-suse-linux/12/../../../../lib64/crtn.o  -fsanitize=address   -Wl,-soname -Wl,libx.so.0 -o .libs/libx.so.0.0.0
libtool: link: (cd ".libs" && rm -f "libx.so.0" && ln -s "libx.so.0.0.0" "libx.so.0")
libtool: link: (cd ".libs" && rm -f "libx.so" && ln -s "libx.so.0.0.0" "libx.so")
libtool: link: ar cr .libs/libx.a  x.o
libtool: link: ranlib .libs/libx.a
libtool: link: ( cd ".libs" && rm -f "libx.la" && ln -s "../libx.la" "libx.la" )
$ ldd -r .libs/libx.so
        linux-vdso.so.1 (0x00007fffd4bb3000)
        libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f5c29a00000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f5c29c91000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f5c29600000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f5c29c6e000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f5c29d95000)
undefined symbol: __asan_report_store4  (.libs/libx.so)
undefined symbol: __asan_init   (.libs/libx.so)
undefined symbol: __asan_version_mismatch_check_v8      (.libs/libx.so)