[lld] 8cc6a24 - [ELF] -r: force -Bstatic

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 14 23:20:55 PDT 2024


Author: Fangrui Song
Date: 2024-06-14T23:20:50-07:00
New Revision: 8cc6a2469c95f0815487c442495a829c337913c4

URL: https://github.com/llvm/llvm-project/commit/8cc6a2469c95f0815487c442495a829c337913c4
DIFF: https://github.com/llvm/llvm-project/commit/8cc6a2469c95f0815487c442495a829c337913c4.diff

LOG: [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

Added: 
    

Modified: 
    lld/ELF/Driver.cpp
    lld/test/ELF/libsearch.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index ed214ab29d4c6..ed773f5e69f77 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -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;
     }
@@ -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;
@@ -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;

diff  --git a/lld/test/ELF/libsearch.s b/lld/test/ELF/libsearch.s
index 417953491b677..66b87e950c714 100644
--- a/lld/test/ELF/libsearch.s
+++ b/lld/test/ELF/libsearch.s
@@ -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


        


More information about the llvm-commits mailing list