d8307d
commit f0b2132b35248c1f4a80f62a2c38cddcc802aa8c
d8307d
Author: Florian Weimer <fweimer@redhat.com>
d8307d
Date:   Fri Jun 28 10:12:50 2019 +0200
d8307d
d8307d
    ld.so: Support moving versioned symbols between sonames [BZ #24741]
d8307d
    
d8307d
    This change should be fully backwards-compatible because the old
d8307d
    code aborted the load if a soname mismatch was encountered
d8307d
    (instead of searching further for a matching symbol).  This means
d8307d
    that no different symbols are found.
d8307d
    
d8307d
    The soname check was explicitly disabled for the skip_map != NULL
d8307d
    case.  However, this only happens with dl(v)sym and RTLD_NEXT,
d8307d
    and those lookups do not come with a verneed entry that could be used
d8307d
    for the check.
d8307d
    
d8307d
    The error check was already explicitly disabled for the skip_map !=
d8307d
    NULL case, that is, when dl(v)sym was called with RTLD_NEXT.  But
d8307d
    _dl_vsym always sets filename in the struct r_found_version argument
d8307d
    to NULL, so the check was not active anyway.  This means that
d8307d
    symbol lookup results for the skip_map != NULL case do not change,
d8307d
    either.
d8307d
d8307d
Conflicts:
d8307d
	elf/Makefile
d8307d
	  (usual missing backports)
d8307d
d8307d
diff --git a/elf/Makefile b/elf/Makefile
d8307d
index 29aa3a96738e4176..73f9e25ea5efd63a 100644
d8307d
--- a/elf/Makefile
d8307d
+++ b/elf/Makefile
d8307d
@@ -187,7 +187,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
d8307d
 	 tst-tlsalign tst-tlsalign-extern tst-nodelete-opened \
d8307d
 	 tst-nodelete2 tst-audit11 tst-audit12 tst-dlsym-error tst-noload \
d8307d
 	 tst-latepthread tst-tls-manydynamic tst-nodelete-dlclose \
d8307d
-	 tst-debug1 tst-main1 tst-absolute-sym tst-absolute-zero tst-big-note
d8307d
+	 tst-debug1 tst-main1 tst-absolute-sym tst-absolute-zero tst-big-note \
d8307d
+	 tst-sonamemove-link tst-sonamemove-dlopen
d8307d
 #	 reldep9
d8307d
 tests-internal += loadtest unload unload2 circleload1 \
d8307d
 	 neededtest neededtest2 neededtest3 neededtest4 \
d8307d
@@ -275,7 +276,9 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
d8307d
 		tst-latepthreadmod $(tst-tls-many-dynamic-modules) \
d8307d
 		tst-nodelete-dlclose-dso tst-nodelete-dlclose-plugin \
d8307d
 		tst-main1mod tst-libc_dlvsym-dso tst-absolute-sym-lib \
d8307d
-		tst-absolute-zero-lib tst-big-note-lib
d8307d
+		tst-absolute-zero-lib tst-big-note-lib \
d8307d
+		tst-sonamemove-linkmod1 \
d8307d
+		tst-sonamemove-runmod1 tst-sonamemove-runmod2
d8307d
 
d8307d
 ifeq (yes,$(have-mtls-dialect-gnu2))
d8307d
 tests += tst-gnu2-tls1
d8307d
@@ -1374,6 +1377,28 @@ tst-audit12-ENV = LD_AUDIT=$(objpfx)tst-auditmod12.so
d8307d
 $(objpfx)tst-audit12mod1.so: $(objpfx)tst-audit12mod2.so
d8307d
 LDFLAGS-tst-audit12mod2.so = -Wl,--version-script=tst-audit12mod2.map
d8307d
 
d8307d
+# tst-sonamemove links against an older implementation of the library.
d8307d
+LDFLAGS-tst-sonamemove-linkmod1.so = \
d8307d
+  -Wl,--version-script=tst-sonamemove-linkmod1.map \
d8307d
+  -Wl,-soname,tst-sonamemove-runmod1.so
d8307d
+LDFLAGS-tst-sonamemove-runmod1.so = -Wl,--no-as-needed \
d8307d
+  -Wl,--version-script=tst-sonamemove-runmod1.map \
d8307d
+  -Wl,-soname,tst-sonamemove-runmod1.so
d8307d
+LDFLAGS-tst-sonamemove-runmod2.so = \
d8307d
+  -Wl,--version-script=tst-sonamemove-runmod2.map \
d8307d
+  -Wl,-soname,tst-sonamemove-runmod2.so
d8307d
+$(objpfx)tst-sonamemove-runmod1.so: $(objpfx)tst-sonamemove-runmod2.so
d8307d
+# Link against the link module, but depend on the run-time modules
d8307d
+# for execution.
d8307d
+$(objpfx)tst-sonamemove-link: $(objpfx)tst-sonamemove-linkmod1.so
d8307d
+$(objpfx)tst-sonamemove-link.out: \
d8307d
+  $(objpfx)tst-sonamemove-runmod1.so \
d8307d
+  $(objpfx)tst-sonamemove-runmod2.so
d8307d
+$(objpfx)tst-sonamemove-dlopen: $(libdl)
d8307d
+$(objpfx)tst-sonamemove-dlopen.out: \
d8307d
+  $(objpfx)tst-sonamemove-runmod1.so \
d8307d
+  $(objpfx)tst-sonamemove-runmod2.so
d8307d
+
d8307d
 # Override -z defs, so that we can reference an undefined symbol.
d8307d
 # Force lazy binding for the same reason.
d8307d
 LDFLAGS-tst-latepthreadmod.so = \
d8307d
diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
d8307d
index 68ecc6179f608547..1d046caf017b582b 100644
d8307d
--- a/elf/dl-lookup.c
d8307d
+++ b/elf/dl-lookup.c
d8307d
@@ -536,11 +536,7 @@ do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
d8307d
 	}
d8307d
 
d8307d
 skip:
d8307d
-      /* If this current map is the one mentioned in the verneed entry
d8307d
-	 and we have not found a weak entry, it is a bug.  */
d8307d
-      if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
d8307d
-	  && __glibc_unlikely (_dl_name_match_p (version->filename, map)))
d8307d
-	return -1;
d8307d
+      ;
d8307d
     }
d8307d
   while (++i < n);
d8307d
 
d8307d
@@ -810,34 +806,10 @@ _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
d8307d
 
d8307d
   /* Search the relevant loaded objects for a definition.  */
d8307d
   for (size_t start = i; *scope != NULL; start = 0, ++scope)
d8307d
-    {
d8307d
-      int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
d8307d
-			     &current_value, *scope, start, version, flags,
d8307d
-			     skip_map, type_class, undef_map);
d8307d
-      if (res > 0)
d8307d
-	break;
d8307d
-
d8307d
-      if (__glibc_unlikely (res < 0) && skip_map == NULL)
d8307d
-	{
d8307d
-	  /* Oh, oh.  The file named in the relocation entry does not
d8307d
-	     contain the needed symbol.  This code is never reached
d8307d
-	     for unversioned lookups.  */
d8307d
-	  assert (version != NULL);
d8307d
-	  const char *reference_name = undef_map ? undef_map->l_name : "";
d8307d
-	  struct dl_exception exception;
d8307d
-	  /* XXX We cannot translate the message.  */
d8307d
-	  _dl_exception_create_format
d8307d
-	    (&exception, DSO_FILENAME (reference_name),
d8307d
-	     "symbol %s version %s not defined in file %s"
d8307d
-	     " with link time reference%s",
d8307d
-	     undef_name, version->name, version->filename,
d8307d
-	     res == -2 ? " (no version symbols)" : "");
d8307d
-	  _dl_signal_cexception (0, &exception, N_("relocation error"));
d8307d
-	  _dl_exception_free (&exception);
d8307d
-	  *ref = NULL;
d8307d
-	  return 0;
d8307d
-	}
d8307d
-    }
d8307d
+    if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
d8307d
+		     &current_value, *scope, start, version, flags,
d8307d
+		     skip_map, type_class, undef_map) != 0)
d8307d
+      break;
d8307d
 
d8307d
   if (__glibc_unlikely (current_value.s == NULL))
d8307d
     {
d8307d
diff --git a/elf/tst-sonamemove-dlopen.c b/elf/tst-sonamemove-dlopen.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..c496705044cdd53c
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-sonamemove-dlopen.c
d8307d
@@ -0,0 +1,35 @@
d8307d
+/* Check that a moved versioned symbol can be found using dlsym, dlvsym.
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <http://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+#include <stddef.h>
d8307d
+#include <support/check.h>
d8307d
+#include <support/xdlfcn.h>
d8307d
+
d8307d
+static int
d8307d
+do_test (void)
d8307d
+{
d8307d
+  /* tst-sonamemove-runmod1.so does not define moved_function, but it
d8307d
+     depends on tst-sonamemove-runmod2.so, which does.  */
d8307d
+  void *handle = xdlopen ("tst-sonamemove-runmod1.so", RTLD_NOW);
d8307d
+  TEST_VERIFY (xdlsym (handle, "moved_function") != NULL);
d8307d
+  TEST_VERIFY (xdlvsym (handle, "moved_function", "SONAME_MOVE") != NULL);
d8307d
+
d8307d
+  return 0;
d8307d
+}
d8307d
+
d8307d
+#include <support/test-driver.c>
d8307d
diff --git a/elf/tst-sonamemove-link.c b/elf/tst-sonamemove-link.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..4bc3bf32f88f97a9
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-sonamemove-link.c
d8307d
@@ -0,0 +1,41 @@
d8307d
+/* Check that a versioned symbol can move from one library to another.
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <http://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+/* At link time, moved_function is bound to the symbol version
d8307d
+   SONAME_MOVE in tst-sonamemove-runmod1.so, using the
d8307d
+   tst-sonamemove-linkmod1.so stub object.
d8307d
+
d8307d
+   At run time, the process loads the real tst-sonamemove-runmod1.so,
d8307d
+   which depends on tst-sonamemove-runmod2.so.
d8307d
+   tst-sonamemove-runmod1.so does not define moved_function, but
d8307d
+   tst-sonamemove-runmod2.so does.
d8307d
+
d8307d
+   The net effect is that the versioned symbol
d8307d
+   moved_function@SONAME_MOVE moved from the soname
d8307d
+   tst-sonamemove-linkmod1.so at link time to the soname
d8307d
+   tst-sonamemove-linkmod2.so at run time. */
d8307d
+void moved_function (void);
d8307d
+
d8307d
+static int
d8307d
+do_test (void)
d8307d
+{
d8307d
+  moved_function ();
d8307d
+  return 0;
d8307d
+}
d8307d
+
d8307d
+#include <support/test-driver.c>
d8307d
diff --git a/elf/tst-sonamemove-linkmod1.c b/elf/tst-sonamemove-linkmod1.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..b8a354e5e394f566
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-sonamemove-linkmod1.c
d8307d
@@ -0,0 +1,25 @@
d8307d
+/* Link interface for (lack of) soname matching in versioned symbol refs.
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <http://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+/* This function moved from tst-sonamemove-runmod1.so.  This module is
d8307d
+   intended for linking only, to simulate an old application which was
d8307d
+   linked against an older version of the library.  */
d8307d
+void
d8307d
+moved_function (void)
d8307d
+{
d8307d
+}
d8307d
diff --git a/elf/tst-sonamemove-linkmod1.map b/elf/tst-sonamemove-linkmod1.map
d8307d
new file mode 100644
d8307d
index 0000000000000000..8fe5904018972009
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-sonamemove-linkmod1.map
d8307d
@@ -0,0 +1,3 @@
d8307d
+SONAME_MOVE {
d8307d
+  global: moved_function;
d8307d
+};
d8307d
diff --git a/elf/tst-sonamemove-runmod1.c b/elf/tst-sonamemove-runmod1.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..5c409e22898bc836
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-sonamemove-runmod1.c
d8307d
@@ -0,0 +1,23 @@
d8307d
+/* Run-time module whose moved_function moved to a library dependency.
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <http://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+/* Dummy function to add the required symbol version.  */
d8307d
+void
d8307d
+other_function (void)
d8307d
+{
d8307d
+}
d8307d
diff --git a/elf/tst-sonamemove-runmod1.map b/elf/tst-sonamemove-runmod1.map
d8307d
new file mode 100644
d8307d
index 0000000000000000..2ea81c6e6ffae2be
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-sonamemove-runmod1.map
d8307d
@@ -0,0 +1,3 @@
d8307d
+SONAME_MOVE {
d8307d
+  global: other_function;
d8307d
+};
d8307d
diff --git a/elf/tst-sonamemove-runmod2.c b/elf/tst-sonamemove-runmod2.c
d8307d
new file mode 100644
d8307d
index 0000000000000000..b5e482eff57d7d83
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-sonamemove-runmod2.c
d8307d
@@ -0,0 +1,24 @@
d8307d
+/* Run-time module with the actual implementation of moved_function.
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <http://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+/* In the test scenario, this function was originally in
d8307d
+   tst-sonamemove-runmod1.so.  */
d8307d
+void
d8307d
+moved_function (void)
d8307d
+{
d8307d
+}
d8307d
diff --git a/elf/tst-sonamemove-runmod2.map b/elf/tst-sonamemove-runmod2.map
d8307d
new file mode 100644
d8307d
index 0000000000000000..8fe5904018972009
d8307d
--- /dev/null
d8307d
+++ b/elf/tst-sonamemove-runmod2.map
d8307d
@@ -0,0 +1,3 @@
d8307d
+SONAME_MOVE {
d8307d
+  global: moved_function;
d8307d
+};