This is the mail archive of the 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 <>

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.


Signed-off-by: Andy Ross <>
Cc: Joseph S. Myers <>
Cc: Brad King <>
Signed-off-by: Jörg Krause <>
 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
 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;
 if [ "x${NATIVE}" = xyes ] ; then

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