This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH][RESEND] ld: -rpath must search under sysroot


From: Andy Ross <andy.ross@windriver.com>

This is a resend of a revised patch from the binutils mailing list [1].

The original commit message:
"""
The -rpath argument would search the host filesystem for libraries,
even when a sysroot was defined.  For cross toolchains with targets
compatible with the host architecture this can find incorrect
libraries.  Leave -rpath-link unmodified, as build systems in the wild
are already using this to point to host directories.
"""

The resend of this patch is the result of a discussion we had between
Buildroot and CMake developers [2]. The -rpath argument is used both
by the cross and the dynamic linker to find the correct shared libraries
to link against.

When cross-compiling, the intended behavior of the cross linker is to
"search for the file in the same that the dynamic linker will search.
That means that we want to use the rpath-link, rpath, [..] entries." [3]

When passing for example "/usr/lib" as -rpath argument to the cross
linker, the expected behavior is that this value is set to the RPATH
value of the ELF dynamic header of the ELF binary so the dynamic linker
will search for the needed libraries in "/usr/lib" when the binary is
executed on the target. This is handled properly.

The problem is that "/usr/lib" is used by the cross linker to search for
needed libraries. As the path is pointing to the host libraries, the
linking fails.

Therefore, when taking the -rpath argument into account for searching
the needed libraries, the linker should search in sysroot.

[1] https://sourceware.org/ml/binutils/2012-09/msg00156.html
[2] https://gitlab.kitware.com/cmake/cmake/issues/16682
[3] https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=ld/emultempl/elf32.em;h=84adaef6dfe181b8e3b09e306702b916e1bcfb21#l1368

Signed-off-by: Andy Ross <andy.ross@windriver.com>
Cc: Joseph S. Myers <joseph@codesourcery.com>
Cc: Brad King <gitlab@gitlab.kitware.com>
Signed-off-by: Jörg Krause <joerg.krause@embedded.rocks>
---
 ld/emultempl/elf32.em | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/ld/emultempl/elf32.em b/ld/emultempl/elf32.em
index 84adaef6df..c4291e981c 100644
--- a/ld/emultempl/elf32.em
+++ b/ld/emultempl/elf32.em
@@ -1400,9 +1400,16 @@ fragment <<EOF
 EOF
 if [ "x${USE_LIBPATH}" = xyes ] ; then
 fragment <<EOF
-	  if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
-						  &n, force))
-	    break;
+	  if (command_line.rpath)
+	    {
+	      char *tmprp = gld${EMULATION_NAME}_add_sysroot (command_line.rpath);
+	      found = gld${EMULATION_NAME}_search_needed (tmprp, &n, force);
+	      free (tmprp);
+	      if (found)
+		break;
+	    }
+	  else if (gld${EMULATION_NAME}_search_needed (NULL, &n, force))
+	      break;
 EOF
 fi
 if [ "x${NATIVE}" = xyes ] ; then
-- 
2.12.0


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]