file-a.c: --------- void __attribute__((weak)) func(void) { /* no code */ } main() { func(); } file-b.c: --------- void func(void) { printf("func\n"); } # gcc -c file-a.c file-b.c -O2 # gcc -o program file-a.o file-b.o The program will not print "func", because the call to func() was optimised out during the compilation of file-a.c. This happens with -O2, but not when using -fno-unit-at-a-time. First sighted in 4.1.
ipa-pure-const thinks that func is const, which is wrong. Then DCE comes along and deletes the function call.
I have a patch.
Actually no, you have to use -fPIC to get this not to be optimized, otherwise func will be bound locally which is not what you want.
(In reply to comment #3) > Actually no, you have to use -fPIC to get this not to be optimized, otherwise > func will be bound locally which is not what you want. Two things. First, that's a change in behavior from how it used to work and I don't recall seeing warnings about going-away behavior (the place this problem is actually manifesting is in code for the Linux Kernel). Second there is some sort of bug here as if we have: $ cat file-a.c void __attribute__((weak)) func(void) { printf("weak\n"); } main() { func(); } $ cat file-b.c void func(void) { printf("func\n"); } $ gcc-4.1 -c file-a.c file-b.c -O2 file-a.c: In function 'func': file-a.c:3: warning: incompatible implicit declaration of built-in function 'printf' file-b.c: In function 'func': file-b.c:3: warning: incompatible implicit declaration of built-in function 'printf' $ gcc-4.1 -o program file-a.o file-b.o $ ./program func $ gcc-4.1 -o program file-a.o $ ./program weak $ gcc-4.1 -v Using built-in specs. Target: x86_64-linux-gnu Configured with: ../src/configure -v --enable-languages=c,c++,java,fortran,objc,obj-c++,ada,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.1-1.4.2.0/jre --enable-mpfr --enable-checking=release x86_64-linux-gnu Thread model: posix gcc version 4.1.0 (Debian 4.1.0-1)
Subject: Bug number PR27781 A patch for this bug has been added to the patch tracker. The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2006-06/msg00808.html
Subject: Bug 27781 Author: rguenth Date: Thu Jun 15 17:23:41 2006 New Revision: 114681 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=114681 Log: 2006-06-16 Richard Guenther <rguenther@suse.de> PR tree-optimization/27781 * Makefile.in (ipa-pure-const.o): Add $(TARGET_H) dependency. * ipa-pure-const.c (target.h): Include. (analyze_function): Do not analyze functions that do not bind locally. * gcc.dg/tree-ssa/pr27781.c: New testcase. Added: trunk/gcc/testsuite/gcc.dg/tree-ssa/pr27781.c Modified: trunk/gcc/ChangeLog trunk/gcc/Makefile.in trunk/gcc/ipa-pure-const.c trunk/gcc/testsuite/ChangeLog
Fixed on the mainline.
*** Bug 28055 has been marked as a duplicate of this bug. ***
Fixed.
Subject: Bug 27781 Author: rguenth Date: Wed Jun 21 15:36:10 2006 New Revision: 114852 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=114852 Log: 2006-06-21 Richard Guenther <rguenther@suse.de> PR tree-optimization/27781 * Makefile.in (ipa-pure-const.o): Add $(TARGET_H) dependency. * ipa-pure-const.c (target.h): Include. (analyze_function): Do not analyze functions that do not bind locally. * gcc.dg/tree-ssa/pr27781.c: New testcase. Added: branches/gcc-4_1-branch/gcc/testsuite/gcc.dg/tree-ssa/pr27781.c - copied unchanged from r114681, trunk/gcc/testsuite/gcc.dg/tree-ssa/pr27781.c Modified: branches/gcc-4_1-branch/gcc/ChangeLog branches/gcc-4_1-branch/gcc/Makefile.in branches/gcc-4_1-branch/gcc/ipa-pure-const.c branches/gcc-4_1-branch/gcc/testsuite/ChangeLog