[lld] [LLD][ELF] ignore ".so" when relocatable set (PR #94965)

Reno Dakota via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 10 04:37:04 PDT 2024


https://github.com/paparodeo created https://github.com/llvm/llvm-project/pull/94965

closes: https://github.com/llvm/llvm-project/issues/94958

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

when both libtest.a and libtest.so exist in the search path

test:
```
echo void empty(void) { } > test.c
clang test.c -c
ar rc libs/libtest.a test.o
clang test.c --shared -o libs/libtest.so
lld -r test.o -o test -L./libs -ltest
```

>From 66a02cdcaf98f3b939380baaab7577db507d19a6 Mon Sep 17 00:00:00 2001
From: Reno Dakota <170618376+paparodeo at users.noreply.github.com>
Date: Mon, 10 Jun 2024 11:31:39 +0000
Subject: [PATCH] [LLD][ELF] ignore ".so" when relocatable set

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

when both libtest.a and libtest.so exist in the search path

test:
```
echo void empty(void) { } > test.c
clang test.c -c
ar rc libs/libtest.a test.o
clang test.c --shared -o libs/libtest.so
lld -r test.o -o test -L./libs -ltest
```
---
 lld/ELF/DriverUtils.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp
index ac74604408152..77214aa362459 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"))



More information about the llvm-commits mailing list