gold: Treat symbols with version index 0 as unversioned
Oracle Solaris 11.4 Linker and Libraries Guide:
https://docs.oracle.com/en/operating-systems/solaris/oracle-solaris/11.4/linkers-libraries/version-symbol-section.html
defines VER_NDX_LOCAL to 0 with a comment, "Symbol has local scope". This
leads to different interpretations by different linker implementations.
However Solaris as well as ld and ld.so in glibc always treat symbols
with version index 0 as unversioned symbols with global binding. As
discussed in
https://sourceware.org/bugzilla/show_bug.cgi?id=33577
in hindsight, VER_NDX_NONE might be a better name. Ali from Oracle is
working on clarifying what version index 0 really means for unversioned
symbols with global binding. In the meantime, update gold to treat
symbols with version index 0 as unversioned with global binding.
elfcpp/
PR gold/33577
* elfcpp.h (VER_NDX_LOCAL): Update comments.
(VER_NDX_GLOBAL): Likewise.
gold/
PR gold/33577
* dynobj.cc (Versions::symbol_section_contents): Set unversioned
symbol version index to VER_NDX_LOCAL.
* symtab.cc (Symbol_table::add_from_dynobj): Don't check
VER_NDX_LOCAL.
* testsuite/Makefile.am (check_SCRIPTS): Add ver_test_pr33577.sh.
(check_DATA): Add ver_test_pr33577a.syms and
ver_test_pr33577b.syms.
(ver_test_pr33577a.syms): New rule.
(ver_test_pr33577.so): Likewise.
(ver_test_pr33577b.syms): Likewise.
(ver_test_pr33577): Likewise.
* testsuite/Makefile.in: Regenerated.
* testsuite/ver_matching_test.sh: Updated to checking missing
Base version.
* testsuite/ver_test_14.sh (check_missing): New.
Updated to check missing Base version.
* testsuite/ver_test_pr33577.sh: New fille.
* testsuite/ver_test_pr33577a.c: Likewise.
* testsuite/ver_test_pr33577b.c: Likewise.
Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
diff --git a/elfcpp/elfcpp.h b/elfcpp/elfcpp.h
index 3ceddbd..a363294 100644
--- a/elfcpp/elfcpp.h
+++ b/elfcpp/elfcpp.h
@@ -951,7 +951,9 @@
// Special constants found in the SHT_GNU_versym entries.
+// Symbol is a local symbol or an unversioned symbol with global binding.
const int VER_NDX_LOCAL = 0;
+// Symbol is a base symbol with global binding.
const int VER_NDX_GLOBAL = 1;
// A SHT_GNU_versym section holds 16-bit words. This bit is set if
diff --git a/gold/dynobj.cc b/gold/dynobj.cc
index d6b06e8..380b2e2 100644
--- a/gold/dynobj.cc
+++ b/gold/dynobj.cc
@@ -1756,12 +1756,7 @@
unsigned int version_index;
const char* version = (*p)->version();
if (version == NULL)
- {
- if ((*p)->is_defined() && !(*p)->is_from_dynobj())
- version_index = elfcpp::VER_NDX_GLOBAL;
- else
- version_index = elfcpp::VER_NDX_LOCAL;
- }
+ version_index = elfcpp::VER_NDX_LOCAL;
else if (version[0] == '\0')
version_index = elfcpp::VER_NDX_GLOBAL;
else
diff --git a/gold/symtab.cc b/gold/symtab.cc
index 6615a44..877ddd7 100644
--- a/gold/symtab.cc
+++ b/gold/symtab.cc
@@ -1632,24 +1632,15 @@
bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
v &= elfcpp::VERSYM_VERSION;
- // The Sun documentation says that V can be VER_NDX_LOCAL,
- // or VER_NDX_GLOBAL, or a version index. The meaning of
- // VER_NDX_LOCAL is defined as "Symbol has local scope."
- // The old GNU linker will happily generate VER_NDX_LOCAL
- // for an undefined symbol. I don't know what the Sun
- // linker will generate.
-
- if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
- && st_shndx != elfcpp::SHN_UNDEF)
- {
- // This symbol should not be visible outside the object.
- continue;
- }
-
// At this point we are definitely going to add this symbol.
Stringpool::Key name_key;
name = this->namepool_.add(name, true, &name_key);
+ // The Sun documentation says that V can be VER_NDX_LOCAL,
+ // or VER_NDX_GLOBAL, or a version index. The meaning of
+ // VER_NDX_LOCAL means that symbol is a local dynamic symbol
+ // or an unversioned global/weak symbol which is defined or
+ // undefined.
if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
|| v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
{
diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am
index 8f158ba..71ef3e3 100644
--- a/gold/testsuite/Makefile.am
+++ b/gold/testsuite/Makefile.am
@@ -2002,6 +2002,17 @@
ver_test_pr31830_lto_b.so: gcctestdir/ld $(srcdir)/ver_test_pr31830_lto.c $(srcdir)/ver_test_pr31830.script
$(LINK) -Bgcctestdir/ -shared -o $@ -O2 -fPIC -flto-partition=max -flto=2 $(srcdir)/ver_test_pr31830_lto.c -Wl,--version-script,$(srcdir)/ver_test_pr31830.script
+check_SCRIPTS += ver_test_pr33577.sh
+check_DATA += ver_test_pr33577a.syms ver_test_pr33577b.syms
+ver_test_pr33577a.syms: ver_test_pr33577.so
+ $(TEST_READELF) --dyn-syms -W $< >$@
+ver_test_pr33577.so: gcctestdir/ld $(srcdir)/ver_test_pr33577a.c
+ $(LINK) -Bgcctestdir/ -shared -o $@ -O2 -fPIC $(srcdir)/ver_test_pr33577a.c
+ver_test_pr33577b.syms: ver_test_pr33577
+ $(TEST_READELF) --dyn-syms -W $< >$@
+ver_test_pr33577: gcctestdir/ld $(srcdir)/ver_test_pr33577b.c ver_test_pr33577.so
+ $(LINK) -Bgcctestdir/ -o $@ -O2 $(srcdir)/ver_test_pr33577b.c ver_test_pr33577.so
+
check_SCRIPTS += weak_as_needed.sh
check_DATA += weak_as_needed.stdout
weak_as_needed.stdout: weak_as_needed_a.so
diff --git a/gold/testsuite/Makefile.in b/gold/testsuite/Makefile.in
index 357dec0..bdfc777 100644
--- a/gold/testsuite/Makefile.in
+++ b/gold/testsuite/Makefile.in
@@ -475,6 +475,7 @@
@GCC_TRUE@@NATIVE_LINKER_TRUE@ ver_test_pr23409.sh \
@GCC_TRUE@@NATIVE_LINKER_TRUE@ ver_test_pr31830.sh \
@GCC_TRUE@@NATIVE_LINKER_TRUE@ ver_test_pr31830_lto.sh \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ ver_test_pr33577.sh \
@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_as_needed.sh relro_test.sh \
@GCC_TRUE@@NATIVE_LINKER_TRUE@ ver_matching_test.sh \
@GCC_TRUE@@NATIVE_LINKER_TRUE@ script_test_3.sh \
@@ -536,6 +537,8 @@
@GCC_TRUE@@NATIVE_LINKER_TRUE@ ver_test_pr31830_b.syms \
@GCC_TRUE@@NATIVE_LINKER_TRUE@ ver_test_pr31830_lto_a.syms \
@GCC_TRUE@@NATIVE_LINKER_TRUE@ ver_test_pr31830_lto_b.syms \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ ver_test_pr33577a.syms \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ ver_test_pr33577b.syms \
@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_as_needed.stdout \
@GCC_TRUE@@NATIVE_LINKER_TRUE@ protected_3.err \
@GCC_TRUE@@NATIVE_LINKER_TRUE@ relro_test.stdout \
@@ -5929,6 +5932,13 @@
--log-file $$b.log --trs-file $$b.trs \
$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
"$$tst" $(AM_TESTS_FD_REDIRECT)
+ver_test_pr33577.sh.log: ver_test_pr33577.sh
+ @p='ver_test_pr33577.sh'; \
+ b='ver_test_pr33577.sh'; \
+ $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
+ --log-file $$b.log --trs-file $$b.trs \
+ $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
+ "$$tst" $(AM_TESTS_FD_REDIRECT)
weak_as_needed.sh.log: weak_as_needed.sh
@p='weak_as_needed.sh'; \
b='weak_as_needed.sh'; \
@@ -8963,6 +8973,14 @@
@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(TEST_READELF) --dyn-syms -W $< >$@
@GCC_TRUE@@NATIVE_LINKER_TRUE@ver_test_pr31830_lto_b.so: gcctestdir/ld $(srcdir)/ver_test_pr31830_lto.c $(srcdir)/ver_test_pr31830.script
@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(LINK) -Bgcctestdir/ -shared -o $@ -O2 -fPIC -flto-partition=max -flto=2 $(srcdir)/ver_test_pr31830_lto.c -Wl,--version-script,$(srcdir)/ver_test_pr31830.script
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ver_test_pr33577a.syms: ver_test_pr33577.so
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(TEST_READELF) --dyn-syms -W $< >$@
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ver_test_pr33577.so: gcctestdir/ld $(srcdir)/ver_test_pr33577a.c
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(LINK) -Bgcctestdir/ -shared -o $@ -O2 -fPIC $(srcdir)/ver_test_pr33577a.c
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ver_test_pr33577b.syms: ver_test_pr33577
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(TEST_READELF) --dyn-syms -W $< >$@
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ver_test_pr33577: gcctestdir/ld $(srcdir)/ver_test_pr33577b.c ver_test_pr33577.so
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(LINK) -Bgcctestdir/ -o $@ -O2 $(srcdir)/ver_test_pr33577b.c ver_test_pr33577.so
@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_as_needed.stdout: weak_as_needed_a.so
@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(TEST_READELF) -dW --dyn-syms $< >$@
@GCC_TRUE@@NATIVE_LINKER_TRUE@weak_as_needed_a.so: gcctestdir/ld weak_as_needed_a.o weak_as_needed_b.so weak_as_needed_c.so
diff --git a/gold/testsuite/ver_matching_test.sh b/gold/testsuite/ver_matching_test.sh
index 3c34086f..9abdfbd 100755
--- a/gold/testsuite/ver_matching_test.sh
+++ b/gold/testsuite/ver_matching_test.sh
@@ -54,22 +54,22 @@
}
check ver_matching_test.stdout "V1 *sizeof_headers$"
-check ver_matching_test.stdout "Base *globaoeufostuff$"
+check ver_matching_test.stdout "globaoeufostuff$"
check ver_matching_test.stdout "V1 *globaoeufxstuff$"
check ver_matching_test.stdout "V2 *otherns::stuff$"
-check ver_matching_test.stdout "Base *otherns::biz$"
+check ver_matching_test.stdout "otherns::biz$"
check ver_matching_test.stdout "V1 *foo$"
check ver_matching_test.stdout "V1 *bar()$"
-check ver_matching_test.stdout "Base *bar1()$"
+check ver_matching_test.stdout "bar1()$"
check ver_matching_test.stdout "V1 *bar2$"
check ver_matching_test.stdout "V1 *myns::blah()$"
check ver_matching_test.stdout "V1 *myns::bip()$"
check ver_matching_test.stdout "V1 *myns::Stuff::Stuff()$"
-check ver_matching_test.stdout "Base *Biz::Biz()$"
+check ver_matching_test.stdout "Biz::Biz()$"
check ver_matching_test.stdout "V2 *blaza1$"
check ver_matching_test.stdout "V2 *blaza2$"
check ver_matching_test.stdout "V2 *blaza$"
-check ver_matching_test.stdout "Base *bla$"
+check ver_matching_test.stdout "bla$"
check ver_matching_test.stdout "V2 *blaz$"
check ver_matching_test.stdout "V2 *blazb$"
@@ -79,6 +79,12 @@
check_missing ver_matching_test.stdout "V1 *baz(int\\*, char)$"
check_missing ver_matching_test.stdout "V1 *baz(char\\*, int)$"
+check_missing ver_matching_test.stdout "Base *globaoeufostuff$"
+check_missing ver_matching_test.stdout "Base *otherns::biz$"
+check_missing ver_matching_test.stdout "Base *bar1()$"
+check_missing ver_matching_test.stdout "Base *Biz::Biz()$"
+check_missing ver_matching_test.stdout "Base *bla$"
+
check_missing ver_matching_test.stdout "foo1"
# This symbols is injected by the linker itself, but should still
diff --git a/gold/testsuite/ver_test_14.sh b/gold/testsuite/ver_test_14.sh
index 362507d..7c1f0d4 100755
--- a/gold/testsuite/ver_test_14.sh
+++ b/gold/testsuite/ver_test_14.sh
@@ -35,9 +35,23 @@
fi
}
+check_missing()
+{
+ if grep -q "$2" "$1"
+ then
+ echo "Found unexpected symbol in $1:"
+ echo " $2"
+ echo ""
+ echo "Actual output below:"
+ cat "$1"
+ exit 1
+ fi
+}
+
check ver_test_14.syms "V1 *\(0x[0-9a-f][048c]\)\? t2()$"
check ver_test_14.syms "V1 *\(0x[0-9a-f][048c]\)\? t3()$"
check ver_test_14.syms "V1 *\(0x[0-9a-f][048c]\)\? t4()$"
-check ver_test_14.syms "Base *\(0x[0-9a-f][048c]\)\? t4_2a$"
+check ver_test_14.syms "t4_2a$"
+check_missing ver_test_14.syms "Base *\(0x[0-9a-f][048c]\)\? t4_2a$"
exit 0
diff --git a/gold/testsuite/ver_test_pr33577.sh b/gold/testsuite/ver_test_pr33577.sh
new file mode 100755
index 0000000..b2dd6de
--- /dev/null
+++ b/gold/testsuite/ver_test_pr33577.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+# ver_test_pr33577.sh -- a test case for unversioned symbols.
+
+# Copyright (C) 2025 Free Software Foundation, Inc.
+
+# 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 test verifies that linker-generated symbols (e.g., _end)
+# get correct version information even in the presence of
+# a shared library that provides those symbols with different
+# versions.
+
+check()
+{
+ if ! grep -q "$2" "$1"
+ then
+ echo "Did not find expected symbol in $1:"
+ echo " $2"
+ echo ""
+ echo "Actual output below:"
+ cat "$1"
+ exit 1
+ fi
+}
+
+check ver_test_pr33577a.syms "bar$"
+check ver_test_pr33577a.syms "foo$"
+check ver_test_pr33577b.syms "bar$"
+check ver_test_pr33577b.syms "foo$"
+
+exit 0
diff --git a/gold/testsuite/ver_test_pr33577a.c b/gold/testsuite/ver_test_pr33577a.c
new file mode 100644
index 0000000..34655ba
--- /dev/null
+++ b/gold/testsuite/ver_test_pr33577a.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+
+extern void foo (void);
+extern void bar (void);
+
+void
+foo (void)
+{
+ bar ();
+ printf ("base\n");
+}
diff --git a/gold/testsuite/ver_test_pr33577b.c b/gold/testsuite/ver_test_pr33577b.c
new file mode 100644
index 0000000..821d53c
--- /dev/null
+++ b/gold/testsuite/ver_test_pr33577b.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+extern void foo (void);
+extern void bar (void);
+
+void
+bar (void)
+{
+ printf ("bar\n");
+}
+
+int
+main ()
+{
+ foo ();
+ return 0;
+}