http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49571
Summary: -flto -Wl,--as-needed drops needed libraries Product: gcc Version: 4.6.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: lto AssignedTo: unassig...@gcc.gnu.org ReportedBy: b.r.longb...@gmail.com Created attachment 24624 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24624 Minimal testcase demonstrating the error Description: -flto does something funny that makes --as-needed drop more libraries than it should. See the attached file Context: I originally encountered this bug in a big C++ project with -m32, but quickly reduced it to the attached minimal testcase. Only in the original project, I got this misleading error: /usr/bin/ld: /tmp/ccPEhCnW.ltrans4.ltrans.o: undefined reference to symbol 'sqrt@@GLIBC_2.0' /usr/bin/ld: note: 'sqrt@@GLIBC_2.0' is defined in DSO /usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/../../../../../lib32/libm.so so try adding it to the linker command line despite -lm *being* explicitly on the command line (which is normally not needed in C++? Is that standard behavior or is it relying on the indirect linking thing that *usually* generates that error?) but this was replaced in the minimal case by the more typical ccp7SO9X.ltrans0.o:(.text+0x29): undefined reference to `sqrt' Workaround: Identify which libraries are *actually* needed and pass them after -Wl,--no-as-needed. This works for my particular case (I am building multiple binaries, only one of which requires -lm) since libm would be loaded indirectly by libstdc++ anyway, but could be problematic in the general case System information 1: Using built-in specs. COLLECT_GCC=/usr/x86_64-pc-linux-gnu/gcc-bin/4.6.0/gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/4.6.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /var/tmp/portage/sys-devel/gcc-4.6.0/work/gcc-4.6.0/configure --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/4.6.0 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.0/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.6.0 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.6.0/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.6.0/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.0/include/g++-v4 --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec --disable-fixed-point --without-ppl --without-cloog --enable-lto --disable-nls --with-system-zlib --disable-werror --enable-secureplt --enable-multilib --enable-libmudflap --disable-libssp --enable-libgomp --enable-cld --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/4.6.0/python --enable-checking=release --disable-libgcj --enable-languages=c,c++,fortran --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --with-bugurl=http://bugs.gentoo.org/ --with-pkgversion='Gentoo 4.6.0 p1.2, pie-0.4.5' Thread model: posix gcc version 4.6.0 (Gentoo 4.6.0 p1.2, pie-0.4.5) System information 2: Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.6.0-13' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-multiarch --with-multiarch-defaults=x86_64-linux-gnu --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib/x86_64-linux-gnu --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib/x86_64-linux-gnu --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.6.1 20110611 (prerelease) (Debian 4.6.0-13)