[PATCH][GOLD] Do not turn a non-weak reference of a weak symbol in DSO to weak.

Doug Kwan (關振德) dougkwan@google.com
Thu Aug 12 13:36:00 GMT 2010


Hi,

    This patch fixes a problem in which a non-weak reference to a weak
symbol in a DSO ends up being weak in the output.  The fix was done by
Cary.  I just added a test case.  This is tested on the x86_64.

-Doug

2010-08-11  Cary Coutant  <ccoutant@google.com>
            Doug Kwan  <dougkwan@google.com>

        * resolve.cc (Symbol_table::should_override): When a weak dynamic
        defintion overrides non-weak undef, remember that the original undef
        is not weak.
        * symtab.cc (Symbol_table::sized_write_global): For undef without
        an original weak binding, set binding to global in output.
        * testsuite/Makefile.am: Add new test strong_ref_weak_def.
        * testsuite/Makefile.in: Regenerate.
        * testsuite/strong_ref_weak_def.sh: New file.
        * testsuite/strong_ref_weak_def_1.c: Ditto.
        * testsuite/strong_ref_weak_def_2.c: Ditto.
-------------- next part --------------
Index: gold/resolve.cc
===================================================================
RCS file: /cvs/src/src/gold/resolve.cc,v
retrieving revision 1.55
diff -u -u -p -r1.55 resolve.cc
--- gold/resolve.cc	9 Jul 2010 01:34:31 -0000	1.55
+++ gold/resolve.cc	12 Aug 2010 03:22:24 -0000
@@ -576,6 +576,11 @@ Symbol_table::should_override(const Symb
       return false;
 
     case UNDEF * 16 + DYN_WEAK_DEF:
+      // When overriding an undef by a dynamic weak definition,
+      // we need to remember that the original undef was not weak.
+      *adjust_dyndef = true;
+      return true;
+
     case DYN_UNDEF * 16 + DYN_WEAK_DEF:
     case DYN_WEAK_UNDEF * 16 + DYN_WEAK_DEF:
       // Use a weak dynamic definition if we have a reference.
Index: gold/symtab.cc
===================================================================
RCS file: /cvs/src/src/gold/symtab.cc,v
retrieving revision 1.142
diff -u -u -p -r1.142 symtab.cc
--- gold/symtab.cc	2 Aug 2010 13:34:33 -0000	1.142
+++ gold/symtab.cc	12 Aug 2010 03:22:24 -0000
@@ -2756,6 +2756,8 @@ Symbol_table::sized_write_globals(const 
 		    shndx = elfcpp::SHN_UNDEF;
 		    if (sym->is_undef_binding_weak())
 		      binding = elfcpp::STB_WEAK;
+		    else
+		      binding = elfcpp::STB_GLOBAL;
 		  }
 		else if (symobj->pluginobj() != NULL)
 		  shndx = elfcpp::SHN_UNDEF;
Index: gold/testsuite/Makefile.am
===================================================================
RCS file: /cvs/src/src/gold/testsuite/Makefile.am,v
retrieving revision 1.139
diff -u -u -p -r1.139 Makefile.am
--- gold/testsuite/Makefile.am	2 Aug 2010 11:59:11 -0000	1.139
+++ gold/testsuite/Makefile.am	12 Aug 2010 03:22:24 -0000
@@ -1530,6 +1530,24 @@ no_version_test.o: no_version_test.c
 no_version_test.stdout: libno_version_test.so
 	$(TEST_OBJDUMP) -h $< > $@
 
+# Test that strong reference to a weak symbol in a DSO remains strong.
+check_SCRIPTS += strong_ref_weak_def.sh
+check_DATA += strong_ref_weak_def.stdout
+MOSTLYCLEANFILES += strong_ref_weak_def_1.so strong_ref_weak_def_2.so \
+	strong_ref_weak_def.stdout
+strong_ref_weak_def_2.o: strong_ref_weak_def_2.c
+	$(COMPILE) -o $@ -c -fPIC $<
+strong_ref_weak_def_2.so: strong_ref_weak_def_2.o gcctestdir/ld
+	gcctestdir/ld -shared -o $@ strong_ref_weak_def_2.o
+strong_ref_weak_def_1.o: strong_ref_weak_def_1.c
+	$(COMPILE) -o $@ -c -fPIC $<
+strong_ref_weak_def_1.so: strong_ref_weak_def_1.o strong_ref_weak_def_2.so \
+	gcctestdir/ld
+	gcctestdir/ld -shared -o $@ strong_ref_weak_def_1.o \
+		strong_ref_weak_def_2.so
+strong_ref_weak_def.stdout: strong_ref_weak_def_1.so
+	$(TEST_READELF) -sWD $< > $@
+
 endif GCC
 endif NATIVE_LINKER
 
Index: gold/testsuite/Makefile.in
===================================================================
RCS file: /cvs/src/src/gold/testsuite/Makefile.in,v
retrieving revision 1.147
diff -u -u -p -r1.147 Makefile.in
--- gold/testsuite/Makefile.in	2 Aug 2010 11:59:11 -0000	1.147
+++ gold/testsuite/Makefile.in	12 Aug 2010 03:22:24 -0000
@@ -307,18 +307,22 @@ check_PROGRAMS = object_unittest$(EXEEXT
 
 # Test that no .gnu.version sections are created when 
 # symbol versioning is not used.
+
+# Test that strong reference to a weak symbol in a DSO remains strong.
 @GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_27 = exclude_libs_test.sh \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	discard_locals_test.sh \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	hidden_test.sh \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	retain_symbols_file_test.sh \
-@GCC_TRUE@@NATIVE_LINKER_TRUE@	no_version_test.sh
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	no_version_test.sh \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	strong_ref_weak_def.sh
 @GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_28 = exclude_libs_test.syms \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	discard_locals_test.syms \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	discard_locals_relocatable_test1.syms \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	discard_locals_relocatable_test2.syms \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	hidden_test.err \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	retain_symbols_file_test.stdout \
-@GCC_TRUE@@NATIVE_LINKER_TRUE@	no_version_test.stdout
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	no_version_test.stdout \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	strong_ref_weak_def.stdout
 @GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_29 = exclude_libs_test.syms \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	libexclude_libs_test_1.a \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	libexclude_libs_test_2.a \
@@ -336,7 +340,10 @@ check_PROGRAMS = object_unittest$(EXEEXT
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	searched_file_test_lib.o \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	alt/searched_file_test_lib.a \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	libno_version_test.so \
-@GCC_TRUE@@NATIVE_LINKER_TRUE@	no_version_test.stdout
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	no_version_test.stdout \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	strong_ref_weak_def_1.so \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	strong_ref_weak_def_2.so \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	strong_ref_weak_def.stdout
 @GCC_TRUE@@MCMODEL_MEDIUM_TRUE@@NATIVE_LINKER_TRUE@am__append_30 = large
 @GCC_FALSE@large_DEPENDENCIES =
 @MCMODEL_MEDIUM_FALSE@large_DEPENDENCIES =
@@ -2669,6 +2676,8 @@ retain_symbols_file_test.sh.log: retain_
 	@p='retain_symbols_file_test.sh'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
 no_version_test.sh.log: no_version_test.sh
 	@p='no_version_test.sh'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
+strong_ref_weak_def.sh.log: strong_ref_weak_def.sh
+	@p='strong_ref_weak_def.sh'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
 split_i386.sh.log: split_i386.sh
 	@p='split_i386.sh'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
 split_x86_64.sh.log: split_x86_64.sh
@@ -3674,6 +3683,18 @@ uninstall-am:
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	$(COMPILE) -o $@ -c -fPIC $<
 @GCC_TRUE@@NATIVE_LINKER_TRUE@no_version_test.stdout: libno_version_test.so
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	$(TEST_OBJDUMP) -h $< > $@
+@GCC_TRUE@@NATIVE_LINKER_TRUE@strong_ref_weak_def_2.o: strong_ref_weak_def_2.c
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	$(COMPILE) -o $@ -c -fPIC $<
+@GCC_TRUE@@NATIVE_LINKER_TRUE@strong_ref_weak_def_2.so: strong_ref_weak_def_2.o gcctestdir/ld
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	gcctestdir/ld -shared -o $@ strong_ref_weak_def_2.o
+@GCC_TRUE@@NATIVE_LINKER_TRUE@strong_ref_weak_def_1.o: strong_ref_weak_def_1.c
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	$(COMPILE) -o $@ -c -fPIC $<
+@GCC_TRUE@@NATIVE_LINKER_TRUE@strong_ref_weak_def_1.so: strong_ref_weak_def_1.o strong_ref_weak_def_2.so \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	gcctestdir/ld
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	gcctestdir/ld -shared -o $@ strong_ref_weak_def_1.o \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@		strong_ref_weak_def_2.so
+@GCC_TRUE@@NATIVE_LINKER_TRUE@strong_ref_weak_def.stdout: strong_ref_weak_def_1.so
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	$(TEST_READELF) -sWD $< > $@
 @DEFAULT_TARGET_I386_TRUE@split_i386_1.o: split_i386_1.s
 @DEFAULT_TARGET_I386_TRUE@	$(TEST_AS) -o $@ $<
 @DEFAULT_TARGET_I386_TRUE@split_i386_2.o: split_i386_2.s
Index: gold/testsuite/strong_ref_weak_def.sh
===================================================================
RCS file: gold/testsuite/strong_ref_weak_def.sh
diff -N gold/testsuite/strong_ref_weak_def.sh
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gold/testsuite/strong_ref_weak_def.sh	12 Aug 2010 03:22:24 -0000
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+# strong_ref_weak_def.sh -- test non-weak reference to a weak symbol defined
+# in a DSO.
+
+# Copyright 2010 Free Software Foundation, Inc.
+# Written by Doug Kwan <dougkwan@google.com>.
+
+# This file is part of gold.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+# This checks that the reference to 'weak_def' have GLOBAL binding.
+
+check()
+{
+    file=$1
+    pattern=$2
+    found=`grep "$pattern" $file`
+    if test -z "$found"; then
+        echo "pattern \"$pattern\" not found in file $file."
+	echo $found
+        exit 1
+    fi
+}
+
+check strong_ref_weak_def.stdout ".* FUNC.* GLOBAL.* UND.* weak_def"
+
+exit 0
Index: gold/testsuite/strong_ref_weak_def_1.c
===================================================================
RCS file: gold/testsuite/strong_ref_weak_def_1.c
diff -N gold/testsuite/strong_ref_weak_def_1.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gold/testsuite/strong_ref_weak_def_1.c	12 Aug 2010 03:22:24 -0000
@@ -0,0 +1,39 @@
+// strong_ref_weak_def_1.c -- test a strong reference to a weak definition
+// in a DSO.
+
+// Copyright 2010 Free Software Foundation, Inc.
+// Written by Doug Kwan <dougkwan@google.com>.
+
+// This file is part of gold.
+
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+// MA 02110-1301, USA.
+
+// We test that we correctly deal with a non-weak reference to
+// a weak symbol in an DSO.  We need to make sure that reference
+// is not turned into a weak one.
+
+// This source is used to build an executable that references a weak
+// symbol in a DSO.
+
+// Strong reference to a weak symbol.
+extern void weak_def (void);
+
+int
+main (void)
+{
+  weak_def ();
+  return 0;
+}
Index: gold/testsuite/strong_ref_weak_def_2.c
===================================================================
RCS file: gold/testsuite/strong_ref_weak_def_2.c
diff -N gold/testsuite/strong_ref_weak_def_2.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gold/testsuite/strong_ref_weak_def_2.c	12 Aug 2010 03:22:24 -0000
@@ -0,0 +1,37 @@
+// strong_ref_weak_def_2.c -- test a strong reference to a weak definition
+// in a DSO.
+
+// Copyright 2010 Free Software Foundation, Inc.
+// Written by Doug Kwan <dougkwan@google.com>.
+
+// This file is part of gold.
+
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+// MA 02110-1301, USA.
+
+// We test that we correctly deal with a non-weak reference to
+// a weak symbol in an DSO.  We need to make sure that reference
+// is not turned into a weak one.
+
+// This source is used to build a shared library that defines a
+// weak symbol.
+
+void weak_def (void);
+
+void
+__attribute__((weak))
+weak_def (void)
+{
+}


More information about the Binutils mailing list