Skip to content

Commit

Permalink
[ELF] -r: force -Bstatic
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MaskRay committed Jun 15, 2024
1 parent 7c6d0d2 commit 8cc6a24
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
return;
}
case file_magic::elf_shared_object: {
if (config->isStatic || config->relocatable) {
if (config->isStatic) {
error("attempted static link of dynamic object " + path);
return;
}
Expand Down Expand Up @@ -1892,6 +1892,9 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
// For --{push,pop}-state.
std::vector<std::tuple<bool, bool, bool>> stack;

// -r implies -Bstatic and has precedence over -Bdynamic.
config->isStatic = config->relocatable;

// Iterate over argv to process input files and positional arguments.
std::optional<MemoryBufferRef> defaultScript;
InputFile::isInGroup = false;
Expand Down Expand Up @@ -1946,7 +1949,8 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
config->isStatic = true;
break;
case OPT_Bdynamic:
config->isStatic = false;
if (!config->relocatable)
config->isStatic = false;
break;
case OPT_whole_archive:
inWholeArchive = true;
Expand Down
6 changes: 6 additions & 0 deletions lld/test/ELF/libsearch.s
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@
// RUN: ld.lld -o %t3 %t.o -L%t.dir -Bstatic -call_shared -lls
// RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=DYNAMIC %s

/// -r implies -Bstatic and has precedence over -Bdynamic.
// RUN: ld.lld -r -Bdynamic %t.o -L%t.dir -lls -o %t3.ro
// RUN: llvm-readelf -s -h %t3.ro | FileCheck --check-prefix=RELOCATABLE %s
// RELOCATABLE: Type: REL
// RELOCATABLE: [[#]] _static

// -nostdlib
// RUN: echo 'SEARCH_DIR("'%t.dir'")' > %t.script
// RUN: ld.lld -o %t3 %t.o -script %t.script -lls
Expand Down

0 comments on commit 8cc6a24

Please sign in to comment.