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