[lld] [LLD][ELF] ignore ".so" in search path when relocatable set (PR #94965)
Reno Dakota via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 14 10:08:15 PDT 2024
https://github.com/paparodeo updated https://github.com/llvm/llvm-project/pull/94965
>From 9a1e55d6f2db272cf248d843c95ecee254db7085 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" in search path 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 +-
lld/test/ELF/libsearch.s | 6 ++++++
2 files changed, 7 insertions(+), 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"))
diff --git a/lld/test/ELF/libsearch.s b/lld/test/ELF/libsearch.s
index 417953491b677..eb644abb66464 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
More information about the llvm-commits
mailing list