Skip to content

Commit 8cc6a24

Browse files
committed
[ELF] -r: force -Bstatic
In GNU ld, -r forces -Bstatic and has precedence over -Bdynamic: -lfoo probes libfoo.a but not libfoo.so, even if -Bdynamic is in effect. Our behavior currently matches gold and probes libfoo.so. Since we don't have strong opinion on the exact behavior, let's just follow GNU ld and also unify the reason we report the "attempted static link of dynamic object " error. Close #94958
1 parent 7c6d0d2 commit 8cc6a24

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lld/ELF/Driver.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
324324
return;
325325
}
326326
case file_magic::elf_shared_object: {
327-
if (config->isStatic || config->relocatable) {
327+
if (config->isStatic) {
328328
error("attempted static link of dynamic object " + path);
329329
return;
330330
}
@@ -1892,6 +1892,9 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
18921892
// For --{push,pop}-state.
18931893
std::vector<std::tuple<bool, bool, bool>> stack;
18941894

1895+
// -r implies -Bstatic and has precedence over -Bdynamic.
1896+
config->isStatic = config->relocatable;
1897+
18951898
// Iterate over argv to process input files and positional arguments.
18961899
std::optional<MemoryBufferRef> defaultScript;
18971900
InputFile::isInGroup = false;
@@ -1946,7 +1949,8 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
19461949
config->isStatic = true;
19471950
break;
19481951
case OPT_Bdynamic:
1949-
config->isStatic = false;
1952+
if (!config->relocatable)
1953+
config->isStatic = false;
19501954
break;
19511955
case OPT_whole_archive:
19521956
inWholeArchive = true;

lld/test/ELF/libsearch.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@
9191
// RUN: ld.lld -o %t3 %t.o -L%t.dir -Bstatic -call_shared -lls
9292
// RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=DYNAMIC %s
9393

94+
/// -r implies -Bstatic and has precedence over -Bdynamic.
95+
// RUN: ld.lld -r -Bdynamic %t.o -L%t.dir -lls -o %t3.ro
96+
// RUN: llvm-readelf -s -h %t3.ro | FileCheck --check-prefix=RELOCATABLE %s
97+
// RELOCATABLE: Type: REL
98+
// RELOCATABLE: [[#]] _static
99+
94100
// -nostdlib
95101
// RUN: echo 'SEARCH_DIR("'%t.dir'")' > %t.script
96102
// RUN: ld.lld -o %t3 %t.o -script %t.script -lls

0 commit comments

Comments
 (0)