Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lld][ELF] --relocatable library search prefers shared objects over archives leading to errors #94958

Closed
paparodeo opened this issue Jun 10, 2024 · 3 comments
Labels

Comments

@paparodeo
Copy link
Contributor

paparodeo commented Jun 10, 2024

on linux with llvm-17 (tho source looks the same in main)

$ lld -r test.o -o test -L./libs -ltest
lld: error: attempted static link of dynamic object ./libs/libtest.so

fails when both libtest.a and libtest.so exist

the following patch aligns the search path behavior to that of GNU ld:

diff --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp
index ac7460440..77214aa36 100644
--- a/lld/ELF/DriverUtils.cpp
+++ b/lld/ELF/DriverUtils.cpp
@@ -234,7 +234,7 @@ std::optional<std::string> elf::findFromSearchPaths(StringRef path) {
 // search paths.
 std::optional<std::string> elf::searchLibraryBaseName(StringRef name) {
   for (StringRef dir : config->searchPaths) {
-    if (!config->isStatic)
+    if (!(config->isStatic || config->relocatable))
       if (std::optional<std::string> s = findFile(dir, "lib" + name + ".so"))
         return s;
     if (std::optional<std::string> s = findFile(dir, "lib" + name + ".a"))
diff --git a/lld/test/ELF/libsearch.s b/lld/test/ELF/libsearch.s
index 417953491..eb644abb6 100644
--- a/lld/test/ELF/libsearch.s
+++ b/lld/test/ELF/libsearch.s
@@ -49,6 +49,12 @@
 // RUN: ld.lld -o %t3 %t.o -L%t.dir -lls
 // RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=DYNAMIC %s
 
+// Should use static when dynamic exists in search path
+// RUN: ld.lld --relocatable -o %t3 %t.o -L%t.dir -lls
+// RUN: llvm-readelf -s -h %t3 | FileCheck --check-prefix=RELOCATABLE %s
+// RELOCATABLE: Type: REL (Relocatable file)
+// RELOCATABLE: [[#]] _static
+
 // Check for library search order
 // RUN: mkdir -p %t.dir2
 // RUN: cp %t.dir/libls.a %t.dir2
@github-actions github-actions bot added the lld label Jun 10, 2024
@paparodeo paparodeo changed the title [lld] --relocatable library search prefers shared objects over archives leading to errors [lld][ELF] --relocatable library search prefers shared objects over archives leading to errors Jun 10, 2024
@EugeneZelenko EugeneZelenko added lld:ELF and removed lld labels Jun 10, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 10, 2024

@llvm/issue-subscribers-lld-elf

Author: Reno Dakota (paparodeo)

on linux with llvm-17 (tho source looks the same in main) ``` $ lld -r test.o -o test -L./libs -ltest lld: error: attempted static link of dynamic object ./libs/libtest.so ``` fails when both `libtest.a` and `libtest.so` exist

the following patch fixes the search path:

diff --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp
index ac7460440..7a180bad9 100644
--- a/lld/ELF/DriverUtils.cpp
+++ b/lld/ELF/DriverUtils.cpp
@@ -234,7 +234,7 @@ std::optional&lt;std::string&gt; elf::findFromSearchPaths(StringRef path) {
 // search paths.
 std::optional&lt;std::string&gt; elf::searchLibraryBaseName(StringRef name) {
   for (StringRef dir : config-&gt;searchPaths) {
-    if (!config-&gt;isStatic)
+    if (!(config-&gt;isStatic || config-&gt;relocatable))
       if (std::optional&lt;std::string&gt; s = findFile(dir, "lib" + name + ".so"))
         return s;
     if (std::optional&lt;std::string&gt; s = findFile(dir, "lib" + name + ".a"))

test:

$ mkdir libs
$ echo void empty(void) { } &gt; test.c
$ clang test.c -c
$ ar rc libs/libtest.a test.o

# this works
$ lld -r test.o -o test -L./libs -ltest

# but create a shared lib
clang test.c --shared -o libs/libtest.so

# this fails without patch
$ lld -r test.o -o test -L./libs -ltest
lld: error: attempted static link of dynamic object ./libs/libtest.so

# succeeds with patch
$ lld -r test.o -o test -L./libs -ltest

@MaskRay
Copy link
Member

MaskRay commented Jun 14, 2024

The patch looks good, but it needs a test lld/test/ELF/libsearch.s

@paparodeo
Copy link
Contributor Author

thanks @MaskRay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants