[PATCH][GOLD] Do not generate .gnu.version* if no symbol versioning is used.

Doug Kwan (關振德) dougkwan@google.com
Sun Oct 11 03:41:00 GMT 2009


Hi,

   This patch fixes a problem of that .gnu.version* sections are
always generated in a shared object even if no symbol versioning is
used in the input objects.   This has been tested on x86_64.  All test
cases, including a new one added by this patch, passed.

-Doug

2009-10-10  Doug Kwan  <dougkwan@google.com>

        * dynobj.cc (Versions::Versions): Initialize version_script_.
        Only insert base version symbol definition for a shared object
        if version script defines any version versions.
        (Versions::define_base_version): New method definition.
        (Versions::add_def): Check that base version is not needed.
        (Versions::add_need): Define base version lazily.
        * dynobj.h (Versions::define_base_version): New method declaration.
        (Versions::needs_base_version_): New data member declaration.
        * testsuite/Makefile.am (check_SCRIPTS): Add no_version_test.sh
        (check_DATA): Add no_version_test.stdout.
        (libno_version_test.so, no_version_test.o no_version_test.stdout):
        New make rules.
        * testsuite/Makefile.in: Regenerate.
        * testsuite/no_version_test.c: New file.
        * testsuite/no_version_test.sh: Ditto.
-------------- next part --------------
Index: gold/dynobj.cc
===================================================================
RCS file: /cvs/src/src/gold/dynobj.cc,v
retrieving revision 1.46
diff -u -p -r1.46 dynobj.cc
--- gold/dynobj.cc	6 Oct 2009 22:58:27 -0000	1.46
+++ gold/dynobj.cc	11 Oct 2009 03:28:21 -0000
@@ -1300,27 +1300,18 @@ Verneed::write(const Stringpool* dynpool
 Versions::Versions(const Version_script_info& version_script,
                    Stringpool* dynpool)
   : defs_(), needs_(), version_table_(),
-    is_finalized_(false), version_script_(version_script)
+    is_finalized_(false), version_script_(version_script),
+    needs_base_version_(parameters->options().shared())
 {
-  // We always need a base version, so define that first.  Nothing
-  // explicitly declares itself as part of base, so it doesn't need to
-  // be in version_table_.
-  if (parameters->options().shared())
-    {
-      const char* name = parameters->options().soname();
-      if (name == NULL)
-	name = parameters->options().output_file_name();
-      name = dynpool->add(name, false, NULL);
-      Verdef* vdbase = new Verdef(name, std::vector<std::string>(),
-                                  true, false, true);
-      this->defs_.push_back(vdbase);
-    }
-
   if (!this->version_script_.empty())
     {
       // Parse the version script, and insert each declared version into
       // defs_ and version_table_.
       std::vector<std::string> versions = this->version_script_.get_versions();
+
+      if (this->needs_base_version_ && !versions.empty())
+	this->define_base_version(dynpool);
+
       for (size_t k = 0; k < versions.size(); ++k)
         {
           Stringpool::Key version_key;
@@ -1350,6 +1341,28 @@ Versions::~Versions()
     delete *p;
 }
 
+// Define the base version of a shared library.  The base version definition
+// must be the first entry in defs_.  We insert it lazily so that defs_ is
+// empty if no symbol versioning is used.  Then layout can just drop the
+// version sections.
+
+void
+Versions::define_base_version(Stringpool* dynpool)
+{
+  // If we do any versioning at all,  we always need a base version, so
+  // define that first.  Nothing explicitly declares itself as part of base,
+  // so it doesn't need to be in version_table_.
+  gold_assert(this->defs_.empty());
+  const char* name = parameters->options().soname();
+  if (name == NULL)
+    name = parameters->options().output_file_name();
+  name = dynpool->add(name, false, NULL);
+  Verdef* vdbase = new Verdef(name, std::vector<std::string>(),
+                              true, false, true);
+  this->defs_.push_back(vdbase);
+  this->needs_base_version_ = false;
+}
+
 // Return the dynamic object which a symbol refers to.
 
 Dynobj*
@@ -1422,6 +1435,9 @@ Versions::add_def(const Symbol* sym, con
 	gold_error(_("symbol %s has undefined version %s"),
 		   sym->demangled_name().c_str(), version);
 
+      // We only insert a base version for shared library.
+      gold_assert(!this->needs_base_version_);
+	
       // When creating a regular executable, automatically define
       // a new version.
       Verdef* vd = new Verdef(version, std::vector<std::string>(),
@@ -1468,6 +1484,10 @@ Versions::add_need(Stringpool* dynpool, 
 
   if (vn == NULL)
     {
+      // Create base version defintion lazily for shared library.
+      if (this->needs_base_version_)
+	this->define_base_version(dynpool);
+
       // We have a new filename.
       vn = new Verneed(filename);
       this->needs_.push_back(vn);
Index: gold/dynobj.h
===================================================================
RCS file: /cvs/src/src/gold/dynobj.h,v
retrieving revision 1.33
diff -u -p -r1.33 dynobj.h
--- gold/dynobj.h	30 Sep 2009 22:21:13 -0000	1.33
+++ gold/dynobj.h	11 Oct 2009 03:28:21 -0000
@@ -584,6 +584,10 @@ class Versions
   version_index(const Symbol_table*, const Stringpool*,
 		const Symbol* sym) const;
 
+  // Define the base version of a shared library.
+  void
+  define_base_version(Stringpool* dynpool);
+
   // We keep a hash table mapping canonicalized name/version pairs to
   // a version base.
   typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
@@ -616,6 +620,9 @@ class Versions
   bool is_finalized_;
   // Contents of --version-script, if passed, or NULL.
   const Version_script_info& version_script_;
+  // Whether we need to insert a base version.  This is only used for
+  // shared libaries and is cleared when the base version is defined.
+  bool needs_base_version_;
 };
 
 } // End namespace gold.
Index: gold/testsuite/Makefile.am
===================================================================
RCS file: /cvs/src/src/gold/testsuite/Makefile.am,v
retrieving revision 1.105
diff -u -p -r1.105 Makefile.am
--- gold/testsuite/Makefile.am	10 Oct 2009 07:39:04 -0000	1.105
+++ gold/testsuite/Makefile.am	11 Oct 2009 03:28:21 -0000
@@ -1195,6 +1195,20 @@ alt/searched_file_test_lib.a: searched_f
 	test -d alt || mkdir -p alt
 	$(TEST_AR) rc $@ $^
 
+# Test that no .gnu.version sections are created when 
+# symbol versioning is not used.
+check_SCRIPTS += no_version_test.sh
+check_DATA += no_version_test.stdout
+MOSTLYCLEANFILES += libno_version_test.so no_version_test.stdout
+# We invoke the linker directly since gcc may include additional objects that
+# uses symbol versioning.
+libno_version_test.so: no_version_test.o gcctestdir/ld
+	gcctestdir/ld -shared -o $@ no_version_test.o
+no_version_test.o: no_version_test.c
+	$(COMPILE) -o $@ -c -fPIC $<
+no_version_test.stdout: libno_version_test.so
+	$(TEST_OBJDUMP) -h $< > $@
+
 endif GCC
 endif NATIVE_LINKER
 
Index: gold/testsuite/Makefile.in
===================================================================
RCS file: /cvs/src/src/gold/testsuite/Makefile.in,v
retrieving revision 1.111
diff -u -p -r1.111 Makefile.in
--- gold/testsuite/Makefile.in	10 Oct 2009 07:39:04 -0000	1.111
+++ gold/testsuite/Makefile.in	11 Oct 2009 03:28:21 -0000
@@ -343,14 +343,19 @@ check_PROGRAMS = object_unittest$(EXEEXT
 # referenced by a shared library.
 
 # Test -retain-symbols-file.
+
+# Test that no .gnu.version sections are created when 
+# symbol versioning is not used.
 @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@	retain_symbols_file_test.sh \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	no_version_test.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@	hidden_test.err \
-@GCC_TRUE@@NATIVE_LINKER_TRUE@	retain_symbols_file_test.stdout
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	retain_symbols_file_test.stdout \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	no_version_test.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 \
@@ -362,7 +367,9 @@ check_PROGRAMS = object_unittest$(EXEEXT
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	retain_symbols_file_test.stdout \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	searched_file_test \
 @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@	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@@MCMODEL_MEDIUM_TRUE@@NATIVE_LINKER_TRUE@am__append_30 = large
 @GCC_FALSE@large_DEPENDENCIES = libgoldtest.a ../libgold.a \
 @GCC_FALSE@	../../libiberty/libiberty.a $(am__DEPENDENCIES_1) \
@@ -3030,6 +3037,14 @@ uninstall-am:
 @GCC_TRUE@@NATIVE_LINKER_TRUE@alt/searched_file_test_lib.a: searched_file_test_lib.o
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	test -d alt || mkdir -p alt
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	$(TEST_AR) rc $@ $^
+# We invoke the linker directly since gcc may include additional objects that
+# uses symbol versioning.
+@GCC_TRUE@@NATIVE_LINKER_TRUE@libno_version_test.so: no_version_test.o gcctestdir/ld
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	gcctestdir/ld -shared -o $@ no_version_test.o
+@GCC_TRUE@@NATIVE_LINKER_TRUE@no_version_test.o: no_version_test.c
+@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 $< > $@
 @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/no_version_test.c
===================================================================
RCS file: gold/testsuite/no_version_test.c
diff -N gold/testsuite/no_version_test.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gold/testsuite/no_version_test.c	11 Oct 2009 03:28:21 -0000
@@ -0,0 +1,32 @@
+// ver_no_default.c -- a test case for gold
+
+// Copyright 2009 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 should not create any .gnu.version sections if symbol versioning
+// is not used.
+
+extern int the_answer(void);
+
+int
+the_answer(void)
+{
+  return 42;
+}
Index: gold/testsuite/no_version_test.sh
===================================================================
RCS file: gold/testsuite/no_version_test.sh
diff -N gold/testsuite/no_version_test.sh
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gold/testsuite/no_version_test.sh	11 Oct 2009 03:28:21 -0000
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+# no_version_test.sh -- test that .gnu.version* sections are not created
+# in a shared object when symbol versioning is not used.
+
+# Copyright 2009 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 file goes with no_version_test.c, a C source file
+# linked with option -shared -nostdlib.  We run objdump on
+# the resulting executable and check that .gnu.version* sections
+# are not created.
+
+check()
+{
+    file=$1
+
+    found=`egrep "\.gnu\.version.*" $file`
+    if test -n "$found"; then
+	echo "These section should not be in $file:"
+	echo "$found"
+	exit 1
+    fi
+}
+
+check "no_version_test.stdout"
+
+exit 0


More information about the Binutils mailing list