[lld] 71dcd9b - [ELF] Change the search order for dependent libraries

Petr Hosek via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 28 20:46:09 PST 2022


Author: Petr Hosek
Date: 2022-01-28T20:46:01-08:00
New Revision: 71dcd9bd049e3cd1faa95ef085b9bbd6c2450f5a

URL: https://github.com/llvm/llvm-project/commit/71dcd9bd049e3cd1faa95ef085b9bbd6c2450f5a
DIFF: https://github.com/llvm/llvm-project/commit/71dcd9bd049e3cd1faa95ef085b9bbd6c2450f5a.diff

LOG: [ELF] Change the search order for dependent libraries

When processing dependent libraries, if there's a directory of the same
name as the library being searched for, either in the current directory
or earlier in the search order, LLD will try to open it and report an
error. This is because LLD uses file existence check. To address this
issue we reverse the order, searching the library by basename first
and only considering search paths later, and current directory last.

Differential Revision: https://reviews.llvm.org/D118498

Added: 
    

Modified: 
    lld/ELF/InputFiles.cpp
    lld/test/ELF/deplibs.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 4da371c619f40..825f29a48e2ea 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -490,12 +490,12 @@ template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() {
 static void addDependentLibrary(StringRef specifier, const InputFile *f) {
   if (!config->dependentLibraries)
     return;
-  if (fs::exists(specifier))
-    driver->addFile(specifier, /*withLOption=*/false);
-  else if (Optional<std::string> s = findFromSearchPaths(specifier))
+  if (Optional<std::string> s = searchLibraryBaseName(specifier))
     driver->addFile(*s, /*withLOption=*/true);
-  else if (Optional<std::string> s = searchLibraryBaseName(specifier))
+  else if (Optional<std::string> s = findFromSearchPaths(specifier))
     driver->addFile(*s, /*withLOption=*/true);
+  else if (fs::exists(specifier))
+    driver->addFile(specifier, /*withLOption=*/false);
   else
     error(toString(f) +
           ": unable to find library from dependent library specifier: " +

diff  --git a/lld/test/ELF/deplibs.s b/lld/test/ELF/deplibs.s
index 982ee3724c910..5da483f25ade8 100644
--- a/lld/test/ELF/deplibs.s
+++ b/lld/test/ELF/deplibs.s
@@ -35,23 +35,24 @@
 # CMDLINE-NEXT: {{^libcmdline\.a}}
 
 ## LLD tries to resolve dependent library specifiers in the following order:
-##   1) The literal name in the current working directory.
-##   2) The literal name in a library search path.
-##   3) The name, prefixed with "lib" and suffixed with ".so" or ".a" in a
+##   1) The name, prefixed with "lib" and suffixed with ".so" or ".a" in a
 ##      library search path. This means that a directive of "foo.a" could lead
 ##      to a library named "libfoo.a.a" being linked in.
+##   2) The literal name in a library search path.
+##   3) The literal name in the current working directory.
 ## When using library search paths for dependent libraries, LLD follows the same
 ## rules as for libraries specified on the command line.
 # RUN: cp %t.dir/foo.a %t.cwd/foo.a
 # RUN: cp %t.dir/foo.a %t.dir/libfoo.a.a
+
 # RUN: ld.lld %t.o -o /dev/null -L %t.dir --trace 2>&1 | \
-# RUN:   FileCheck %s -DOBJ=%t.o -DDIR=%t.dir --check-prefix=CWD \
+# RUN:   FileCheck %s -DOBJ=%t.o -DDIR=%t.dir --check-prefix=LIBA-DIR \
 # RUN:                --implicit-check-not=foo.a --implicit-check-not=libbar.so
 
-# CWD:      [[OBJ]]
-# CWD-NEXT: {{^foo\.a}}
+# LIBA-DIR:      [[OBJ]]
+# LIBA-DIR-NEXT: [[DIR]]{{[\\/]}}libfoo.a.a
 
-# RUN: rm %t.cwd/foo.a
+# RUN: rm %t.dir/libfoo.a.a
 # RUN: ld.lld %t.o -o /dev/null -L %t.dir --trace 2>&1 | \
 # RUN:   FileCheck %s -DOBJ=%t.o -DDIR=%t.dir --check-prefix=PLAIN-DIR \
 # RUN:                --implicit-check-not=foo.a --implicit-check-not=libbar.so
@@ -61,11 +62,11 @@
 
 # RUN: rm %t.dir/foo.a
 # RUN: ld.lld %t.o -o /dev/null -L %t.dir --trace 2>&1 | \
-# RUN:   FileCheck %s -DOBJ=%t.o -DDIR=%t.dir --check-prefix=LIBA-DIR \
+# RUN:   FileCheck %s -DOBJ=%t.o -DDIR=%t.dir --check-prefix=CWD \
 # RUN:                --implicit-check-not=foo.a --implicit-check-not=libbar.so
 
-# LIBA-DIR:      [[OBJ]]
-# LIBA-DIR-NEXT: [[DIR]]{{[\\/]}}libfoo.a.a
+# CWD:      [[OBJ]]
+# CWD-NEXT: {{^foo\.a}}
 
     call foo
 .section ".deplibs","MS", at llvm_dependent_libraries,1


        


More information about the llvm-commits mailing list