This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

output unreferenced static variable if an alias to it is referenced


The bug in this patch was uncovered as part of the weakref
development.

If we define a static variable and an alias to it, and use the alias
but not the variable, we fail to output the static variable.  I'd
expected the assembler to reject such output, but it accepts it, and
we end up with a relocation to an undefined symbol where the
locally-defined symbol was expected.

This is a regression from GCC 4.0.  Tested on amd64-linux-gnu.  Ok to
install?

:ADDPATCH attr-alias:

Index: gcc/ChangeLog
from  Alexandre Oliva  <aoliva@redhat.com>

	* cgraphunit.c (cgraph_varpool_remove_unreferenced_decls): Mark
	alias targets.
	* varasm.c (find_decl_and_mark_needed): After cgraph global info
	is ready, stop marking functions, but still mark variables.

Index: gcc/testsuite/ChangeLog
from  Alexandre Oliva  <aoliva@redhat.com>

	* g++.old-deja/g++.abi/vtable2.C: Do not introduce external
	declarations with the same names as thunks' alias targets, use
	aliases instead.
	* gcc.dg/attr-alias-3.c: New test.

Index: gcc/cgraphunit.c
===================================================================
--- gcc/cgraphunit.c.orig	2005-10-19 02:35:28.000000000 -0200
+++ gcc/cgraphunit.c	2005-10-19 02:35:35.000000000 -0200
@@ -322,6 +322,8 @@
 
       node = next;
     }
+  /* Make sure we mark alias targets as used targets.  */
+  finish_aliases_1 ();
   cgraph_varpool_analyze_pending_decls ();
 }
 
Index: gcc/varasm.c
===================================================================
--- gcc/varasm.c.orig	2005-10-19 02:35:28.000000000 -0200
+++ gcc/varasm.c	2005-10-19 02:35:35.000000000 -0200
@@ -4560,27 +4560,28 @@
   struct cgraph_node *fnode = NULL;
   struct cgraph_varpool_node *vnode = NULL;
 
-  /* C++ thunk emitting code produces aliases late in the game.
-     Avoid confusing cgraph code in that case.  */
-  if (!cgraph_global_info_ready)
+  if (TREE_CODE (decl) == FUNCTION_DECL)
     {
-      if (TREE_CODE (decl) == FUNCTION_DECL)
-	{
-	  fnode = cgraph_node_for_asm (target);
-	  if (fnode == NULL)
-	    vnode = cgraph_varpool_node_for_asm (target);
-	}
-      else
-	{
-	  vnode = cgraph_varpool_node_for_asm (target);
-	  if (vnode == NULL)
-	    fnode = cgraph_node_for_asm (target);
-	}
+      fnode = cgraph_node_for_asm (target);
+      if (fnode == NULL)
+	vnode = cgraph_varpool_node_for_asm (target);
+    }
+  else
+    {
+      vnode = cgraph_varpool_node_for_asm (target);
+      if (vnode == NULL)
+	fnode = cgraph_node_for_asm (target);
     }
 
   if (fnode)
     {
-      cgraph_mark_needed_node (fnode);
+      /* We can't mark function nodes as used after cgraph global info
+	 is finished.  This wouldn't generally be necessary, but C++
+	 virtual table thunks are introduced late in the game and
+	 might seem like they need marking, although in fact they
+	 don't.  */
+      if (! cgraph_global_info_ready)
+	cgraph_mark_needed_node (fnode);
       return fnode->decl;
     }
   else if (vnode)
Index: gcc/testsuite/g++.old-deja/g++.abi/vtable2.C
===================================================================
--- gcc/testsuite/g++.old-deja/g++.abi/vtable2.C.orig	2005-10-19 02:35:28.000000000 -0200
+++ gcc/testsuite/g++.old-deja/g++.abi/vtable2.C	2005-10-19 02:35:35.000000000 -0200
@@ -124,8 +124,8 @@
 // These are tricks to allow us to get raw function pointers for
 // member functions.
 extern "C" {
-void _ZN2S32s3Ev ();
-void _ZN2S42s1Ev ();
+  void S3_s3 () __attribute__((__alias__ ("_ZN2S32s3Ev")));
+  void S4_s1 () __attribute__((__alias__ ("_ZN2S42s1Ev")));
 }
 
 // IA-64 uses function descriptors not function pointers in its vtables.
@@ -169,10 +169,10 @@
   INC_VDATA (vtbl, 1);
   // Skip the RTTI entry.
   INC_VDATA (vtbl, 1);
-  if (! CMP_VPTR (vtbl, &_ZN2S32s3Ev))
+  if (! CMP_VPTR (vtbl, &S3_s3))
     return 5;
   INC_VPTR (vtbl);
-  if (! CMP_VPTR (vtbl, &_ZN2S42s1Ev))
+  if (! CMP_VPTR (vtbl, &S4_s1))
     return 6;
   INC_VPTR (vtbl);
   // The S1 vbase offset.
Index: gcc/testsuite/gcc.dg/attr-alias-3.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ gcc/testsuite/gcc.dg/attr-alias-3.c	2005-10-19 02:35:35.000000000 -0200
@@ -0,0 +1,51 @@
+// { dg-do link }
+// { dg-options "-O2" }
+
+// Copyright 2005 Free Software Foundation, Inc.
+// Contributed by Alexandre Oliva <aoliva@redhat.com>
+
+// The unit-at-a-time call graph code used to fail to emit variables
+// without external linkage that were only used indirectly, through
+// aliases.
+
+#ifndef ATTRIBUTE_USED
+# define ATTRIBUTE_USED __attribute__((used))
+#endif
+
+static int lv1;
+extern int Av1a __attribute__((alias ("lv1")));
+int *pv1a = &Av1a;
+
+static int lv2;
+extern int Av2a __attribute__((alias ("lv2")));
+int *pv2a = &lv2;
+
+static int lv3;
+extern int Av3a __attribute__((alias ("lv3")));
+static int *pv3a ATTRIBUTE_USED = &Av3a;
+
+static int lv4;
+extern int Av4a __attribute__((alias ("lv4")));
+static int *pv4a = &Av4a;
+
+typedef void ftype(void);
+
+static void lf1(void) {}
+extern ftype Af1a __attribute__((alias ("lf1")));
+ftype *pf1a = &Af1a;
+
+static void lf2(void) {}
+extern ftype Af2a __attribute__((alias ("lf2")));
+ftype *pf2a = &Af2a;
+
+static void lf3(void) {}
+extern ftype Af3a __attribute__((alias ("lf3")));
+static ftype *pf3a ATTRIBUTE_USED = &Af3a;
+
+static void lf4(void) {}
+extern ftype Af4a __attribute__((alias ("lf4")));
+static ftype *pf4a ATTRIBUTE_USED = &Af4a;
+
+main() {
+  asm volatile ("" : : "m" (pv4a), "m" (pf4a));
+}
-- 
Alexandre Oliva         http://www.lsd.ic.unicamp.br/~oliva/
Red Hat Compiler Engineer   aoliva@{redhat.com, gcc.gnu.org}
Free Software Evangelist  oliva@{lsd.ic.unicamp.br, gnu.org}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]