Does LLD ignore LD_LIBRARY_PATH?

Hello,

I built a LLVM 13 toolchain with ‘CLANG_DEFAULT_LINKER=lld’ and it would appear that the resulting toolchain’s linker(ld.lld) ignores whatever is set in LD_LIBRARY_PATH. If I use -L in the clang++ invocation instead of setting LD_LIBRARY_PATH the linker finds my libs.
I am a but stumped, and I do believe I stumbled upon a wrong conclusion.
I built LLVM with gcc 7 on a Centos 7 machine.
Can you help me?

LD_LIBRARY_PATH exists to tell the run-time linker where to search for libraries, not the static linker. I believe GNU ld only uses LD_LIBRARY_PATH in very limited circumstances (when resolving DT_NEEDED for DSOs you’re linking against if they’re the native architecture).

1 Like

Ah, understood! That makes sense. So the only option is to use -L params?

The --rpath option can set the runtime search path. Some linkers (not ld.lld) also support a LD_RUN_PATH variable which is used as a default if --rpath is not given. If passing to clang use something like: -Wl,–rpath=libdir1:libdir2:libdir:…

(FWIW, I actually patched LD_RUN_PATH support for the version of lld I use, so that it works as a replacement linker for some tools that passes rpaths that way.)

--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -602,6 +602,12 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {

 static std::string getRpath(opt::InputArgList &args) {
   std::vector<StringRef> v = args::getStrings(args, OPT_rpath);
+  if (v.empty()) {
+    StringRef s = getenv("LD_RUN_PATH");
+    if (!s.empty()) {
+      return s.str();
+    }
+  }
   return llvm::join(v.begin(), v.end(), ":");
 }

Thank you. Yes, we were in fact setting a rpath and for gcc that seemed to be enough. But not for clang.
That really helps.