[PATCH] D92535: [llvm-link] fix linker behavior when linking archives with --only-needed option

Sergey Dmitriev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 3 01:44:46 PST 2020


sdmitriev updated this revision to Diff 309188.
sdmitriev retitled this revision from "[llvm-link] minor cleanup [NFC]" to "[llvm-link] fix linker behavior when linking archives with --only-needed option".
sdmitriev edited the summary of this revision.
sdmitriev added a comment.

I have realized that this patch is not NFC, and it corrects llvm-link behavior when archive is linked with other inputs as a library (with --only-needed option). I have updated description and added LIT test to the patch.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D92535/new/

https://reviews.llvm.org/D92535

Files:
  llvm/test/tools/llvm-link/Inputs/i.ll
  llvm/test/tools/llvm-link/archive-only-needed.ll
  llvm/tools/llvm-link/llvm-link.cpp


Index: llvm/tools/llvm-link/llvm-link.cpp
===================================================================
--- llvm/tools/llvm-link/llvm-link.cpp
+++ llvm/tools/llvm-link/llvm-link.cpp
@@ -144,9 +144,7 @@
 
 static std::unique_ptr<Module> loadArFile(const char *Argv0,
                                           std::unique_ptr<MemoryBuffer> Buffer,
-                                          LLVMContext &Context, Linker &L,
-                                          unsigned OrigFlags,
-                                          unsigned ApplicableFlags) {
+                                          LLVMContext &Context) {
   std::unique_ptr<Module> Result(new Module("ArchiveModule", Context));
   StringRef ArchiveName = Buffer->getBufferIdentifier();
   if (Verbose)
@@ -163,7 +161,7 @@
           << " failed to read name of archive member"
           << ArchiveName << "'\n";
       return nullptr;
-    };
+    }
     std::string ChildName = Ename.get().str();
     if (Verbose)
       errs() << "Parsing member '" << ChildName
@@ -199,9 +197,8 @@
     }
     if (Verbose)
       errs() << "Linking member '" << ChildName << "' of archive library.\n";
-    if (L.linkModules(*Result, std::move(M), ApplicableFlags))
+    if (Linker::linkModules(*Result, std::move(M)))
       return nullptr;
-    ApplicableFlags = OrigFlags;
   } // end for each child
   ExitOnErr(std::move(Err));
   return Result;
@@ -356,8 +353,7 @@
 
     std::unique_ptr<Module> M =
         identify_magic(Buffer->getBuffer()) == file_magic::archive
-            ? loadArFile(argv0, std::move(Buffer), Context, L, Flags,
-                         ApplicableFlags)
+            ? loadArFile(argv0, std::move(Buffer), Context)
             : loadFile(argv0, std::move(Buffer), Context);
     if (!M.get()) {
       errs() << argv0 << ": ";
Index: llvm/test/tools/llvm-link/archive-only-needed.ll
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-link/archive-only-needed.ll
@@ -0,0 +1,15 @@
+; RUN: llvm-as %S/Inputs/f.ll -o %t.f.bc
+; RUN: llvm-as %S/Inputs/g.ll -o %t.g.bc
+; RUN: llvm-as %S/Inputs/i.ll -o %t.i.bc
+; RUN: rm -f %t.lib
+; RUN: llvm-ar cr %t.lib %t.f.bc %t.g.bc %t.i.bc
+; RUN: llvm-link %s %t.lib -o %t.linked.bc --only-needed
+; RUN: llvm-nm %t.linked.bc | FileCheck %s
+
+; CHECK: -------- T f
+; CHECK: -------- T i
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+ at i = external global i8*
+ at llvm.used = appending global [1 x i8*] [i8* bitcast (i8** @i to i8*)], section "llvm.metadata"
Index: llvm/test/tools/llvm-link/Inputs/i.ll
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-link/Inputs/i.ll
@@ -0,0 +1,8 @@
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+define void @i() {
+  call void @f()
+  ret void
+}
+
+declare void @f()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92535.309188.patch
Type: text/x-patch
Size: 2890 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201203/a2352740/attachment.bin>


More information about the llvm-commits mailing list