077c9d
commit a8ff215e56050a907189e713fd449bcafe99ff6b
077c9d
Author: Florian Weimer <fweimer@redhat.com>
077c9d
Date:   Fri Apr 26 07:16:30 2019 +0200
077c9d
077c9d
    Makeconfig: Move -Wl,-rpath-link options before library references
077c9d
    
077c9d
    Previously, the -Wl,-rpath-link options came after the libraries
077c9d
    injected using LDLIBS-* variables on the link editor command line for
077c9d
    main programs.  As a result, it could happen that installed libraries
077c9d
    that reference glibc libraries used the installed glibc from the system
077c9d
    directories, instead of the glibc from the build tree.  This can lead to
077c9d
    link failures if the wrong version of libpthread.so.0 is used, for
077c9d
    instance, due to differences in the internal GLIBC_PRIVATE interfaces,
077c9d
    as seen with memusagestat and -lgd after commit
077c9d
    f9b645b4b0a10c43753296ce3fa40053fa44606a ("memusagestat: use local glibc
077c9d
    when linking [BZ #18465]").
077c9d
    
077c9d
    The isolation is necessarily imperfect because these installed
077c9d
    libraries are linked against the installed glibc in the system
077c9d
    directories.  However, in most cases, the built glibc will be newer
077c9d
    than the installed glibc, and this link is permitted because of the
077c9d
    ABI backwards compatibility glibc provides.
077c9d
077c9d
diff --git a/Makeconfig b/Makeconfig
077c9d
index 1ad25fc5a7251aea..e315fb8a75ca5063 100644
077c9d
--- a/Makeconfig
077c9d
+++ b/Makeconfig
077c9d
@@ -428,8 +428,8 @@ ifndef +link-pie
077c9d
 	     $(link-extra-libs)
077c9d
 +link-pie-after-libc = $(+postctorS) $(+postinit)
077c9d
 define +link-pie
077c9d
-$(CC) $(+link-pie-before-libc) $(rtld-LDFLAGS) $(link-extra-flags) \
077c9d
-  $(link-libc) $(+link-pie-after-libc)
077c9d
+$(CC) $(link-libc-rpath-link) $(+link-pie-before-libc) $(rtld-LDFLAGS) \
077c9d
+  $(link-extra-flags) $(link-libc) $(+link-pie-after-libc)
077c9d
 $(call after-link,$@)
077c9d
 endef
077c9d
 define +link-pie-tests
077c9d
@@ -490,8 +490,8 @@ else  # not build-pie-default
077c9d
 	      $(link-extra-libs)
077c9d
 +link-after-libc = $(+postctor) $(+postinit)
077c9d
 define +link
077c9d
-$(CC) $(+link-before-libc) $(rtld-LDFLAGS) $(link-extra-flags) $(link-libc) \
077c9d
-  $(+link-after-libc)
077c9d
+$(CC) $(link-libc-rpath-link) $(+link-before-libc) $(rtld-LDFLAGS) \
077c9d
+  $(link-extra-flags) $(link-libc) $(+link-after-libc)
077c9d
 $(call after-link,$@)
077c9d
 endef
077c9d
 define +link-tests
077c9d
@@ -552,6 +552,15 @@ ifeq (yes,$(build-shared))
077c9d
 link-libc-rpath = -Wl,-rpath=$(rpath-link)
077c9d
 link-libc-rpath-link = -Wl,-rpath-link=$(rpath-link)
077c9d
 
077c9d
+# For programs which are not tests, $(link-libc-rpath-link) is added
077c9d
+# directly in $(+link), $(+link-pie) above, so that -Wl,-rpath-link
077c9d
+# comes before the expansion of LDLIBS-* and affects libraries added
077c9d
+# there.  For shared objects, -Wl,-rpath-link is added via
077c9d
+# $(build-shlib-helper) and $(build-module-helper) in Makerules (also
077c9d
+# before the expansion of LDLIBS-* variables).
077c9d
+
077c9d
+# Tests use -Wl,-rpath instead of -Wl,-rpath-link for
077c9d
+# build-hardcoded-path-in-tests.
077c9d
 ifeq (yes,$(build-hardcoded-path-in-tests))
077c9d
 link-libc-tests-rpath-link = $(link-libc-rpath)
077c9d
 else
077c9d
@@ -562,7 +571,7 @@ link-libc-before-gnulib = $(common-objpfx)libc.so$(libc.so-version) \
077c9d
 			  $(common-objpfx)$(patsubst %,$(libtype.oS),c) \
077c9d
 			  $(as-needed) $(elf-objpfx)ld.so \
077c9d
 			  $(no-as-needed)
077c9d
-link-libc = $(link-libc-rpath-link) $(link-libc-before-gnulib) $(gnulib)
077c9d
+link-libc = $(link-libc-before-gnulib) $(gnulib)
077c9d
 
077c9d
 link-libc-tests-after-rpath-link = $(link-libc-before-gnulib) $(gnulib-tests)
077c9d
 link-libc-tests = $(link-libc-tests-rpath-link) \