On Mon, 16 Jun 2014, Jan Hubicka wrote: > > > /* Variables declared 'const' without an initializer > > > have zero as the initializer if they may not be > > > overridden at link or run time. */ > > > if (!DECL_INITIAL (real_decl) > > > && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl))) > > > return error_mark_node; > > > > > > Honza? > > > > Indeed, this may be a better place to do it as long as > > decl_replaceable_p reliably returns true for weak aliases. If so, the > > following might work: > > > > if ((!DECL_INITIAL (real_decl) && DECL_EXTERNAL (decl)) > > || decl_replaceable_p (decl))) > > return error_mark_node; > > > > On the other hand, I might just separate it out into two separate if > > statements since they should probably have their own comments. > > Yep, this looks like correct change. I used to have FIXME on this but > it seems it went away during some cleanups - the original condition was > comming from expmed's folding and indeed it looked unsafe to me. > > This change is OK with the testcase (if it passes testing)
I'd like to push this topic forward a bit. I've bootstrapped and regtested a version of the patch based on the initial proposal to check DECL_WEAK. The approach with decl_replaceable_p looks not that easy; I'll expand in a followup email. OK for trunk/branch? 2014-07-22 Rich Felker <dal...@libc.org> Alexander Monakov <amona...@ispras.ru> gcc/ PR ipa/61144 * varpool.c (ctor_for_folding): Reject weak data. gcc/testsuite/ PR ipa/61144 * gcc.dg/special/wkali-3.c: New test. * gcc.dg/special/wkali-3a.c: Auxiliary file. diff --git a/gcc/varpool.c b/gcc/varpool.c index 04ce714..9ef2195 100644 --- a/gcc/varpool.c +++ b/gcc/varpool.c @@ -378,6 +378,9 @@ ctor_for_folding (tree decl) return error_mark_node; } + if (DECL_WEAK (decl) && !DECL_VIRTUAL_P (decl)) + return error_mark_node; + gcc_assert (TREE_CODE (decl) == VAR_DECL); real_node = node = varpool_get_node (decl); diff --git a/gcc/testsuite/gcc.dg/special/wkali-3.c b/gcc/testsuite/gcc.dg/special/wkali-3.c new file mode 100644 index 0000000..407ace6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/special/wkali-3.c @@ -0,0 +1,17 @@ +/* { dg-do run } */ +/* { dg-require-weak "" } */ +/* { dg-require-alias "" } */ +/* { dg-additional-sources "wkali-3a.c" } */ + +#include <stdlib.h> + +static const int dummy = 0; +extern const int foo __attribute__((__weak__, __alias__("dummy"))); + +int main(void) { + + if (foo) + exit(0); + else + abort(); +} diff --git a/gcc/testsuite/gcc.dg/special/wkali-3a.c b/gcc/testsuite/gcc.dg/special/wkali-3a.c new file mode 100644 index 0000000..b3bc6a4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/special/wkali-3a.c @@ -0,0 +1,3 @@ +/* { dg-do run } */ + +int foo = 1;