[lld] r301045 - [coff] for /msvclto, pass archive members with prevailing symbols first

Bob Haarman via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 21 14:38:01 PDT 2017


Author: inglorion
Date: Fri Apr 21 16:38:01 2017
New Revision: 301045

URL: http://llvm.org/viewvc/llvm-project?rev=301045&view=rev
Log:
[coff] for /msvclto, pass archive members with prevailing symbols first

Summary: When using /msvclto, lld and MSVC's linker both do their own symbol resolution. This can cause them to select different archive members, which can result in undefined references. This change avoids that situation by extracting archive members that are selected by lld and passing those to link.exe before any archives, so that MSVC's uses those objects for symbol resolution instead of different archive members.

Reviewers: pcc, rnk, ruiu

Reviewed By: pcc

Subscribers: llvm-commits, mehdi_amini

Differential Revision: https://reviews.llvm.org/D32317

Added:
    lld/trunk/test/COFF/Inputs/msvclto-order-a.ll
    lld/trunk/test/COFF/Inputs/msvclto-order-b.ll
    lld/trunk/test/COFF/msvclto-order.ll
Modified:
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/InputFiles.h

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=301045&r1=301044&r2=301045&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Fri Apr 21 16:38:01 2017
@@ -512,6 +512,23 @@ void LinkerDriver::invokeMSVC(opt::Input
   std::string Rsp = "/nologo\n";
   std::vector<std::string> Temps;
 
+  // Write out archive members that we used in symbol resolution and pass these
+  // to MSVC before any archives, so that MSVC uses the same objects to satisfy
+  // references.
+  for (const auto *O : Symtab.ObjectFiles) {
+    if (O->ParentName.empty())
+      continue;
+    SmallString<128> S;
+    int Fd;
+    if (auto EC = sys::fs::createTemporaryFile(
+            "lld-" + sys::path::filename(O->ParentName), ".obj", Fd, S))
+      fatal(EC, "cannot create a temporary file");
+    raw_fd_ostream OS(Fd, /*shouldClose*/ true);
+    OS << O->MB.getBuffer();
+    Temps.push_back(S.str());
+    Rsp += quote(S) + "\n";
+  }
+
   for (auto *Arg : Args) {
     switch (Arg->getOption().getID()) {
     case OPT_linkrepro:

Modified: lld/trunk/COFF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/InputFiles.h?rev=301045&r1=301044&r2=301045&view=diff
==============================================================================
--- lld/trunk/COFF/InputFiles.h (original)
+++ lld/trunk/COFF/InputFiles.h Fri Apr 21 16:38:01 2017
@@ -58,6 +58,8 @@ public:
   // Returns the CPU type this file was compiled to.
   virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
 
+  MemoryBufferRef MB;
+
   // An archive file name if this file is created from an archive.
   StringRef ParentName;
 
@@ -67,7 +69,6 @@ public:
 protected:
   InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
 
-  MemoryBufferRef MB;
   std::string Directives;
 
 private:

Added: lld/trunk/test/COFF/Inputs/msvclto-order-a.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/Inputs/msvclto-order-a.ll?rev=301045&view=auto
==============================================================================
--- lld/trunk/test/COFF/Inputs/msvclto-order-a.ll (added)
+++ lld/trunk/test/COFF/Inputs/msvclto-order-a.ll Fri Apr 21 16:38:01 2017
@@ -0,0 +1,7 @@
+target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+define void @foo() {
+  ret void
+}
+

Added: lld/trunk/test/COFF/Inputs/msvclto-order-b.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/Inputs/msvclto-order-b.ll?rev=301045&view=auto
==============================================================================
--- lld/trunk/test/COFF/Inputs/msvclto-order-b.ll (added)
+++ lld/trunk/test/COFF/Inputs/msvclto-order-b.ll Fri Apr 21 16:38:01 2017
@@ -0,0 +1,10 @@
+target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+declare void @doesntexist()
+
+define void @foo() {
+  call void @doesntexist()
+  ret void
+}
+

Added: lld/trunk/test/COFF/msvclto-order.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/msvclto-order.ll?rev=301045&view=auto
==============================================================================
--- lld/trunk/test/COFF/msvclto-order.ll (added)
+++ lld/trunk/test/COFF/msvclto-order.ll Fri Apr 21 16:38:01 2017
@@ -0,0 +1,24 @@
+; RUN: opt -thinlto-bc %s -o %t.obj
+; RUN: llc -filetype=obj %S/Inputs/msvclto-order-a.ll -o %T/msvclto-order-a.obj
+; RUN: llvm-ar crs %T/msvclto-order-a.lib %T/msvclto-order-a.obj
+; RUN: llc -filetype=obj %S/Inputs/msvclto-order-b.ll -o %T/msvclto-order-b.obj
+; RUN: llvm-ar crs %T/msvclto-order-b.lib %T/msvclto-order-b.obj
+; RUN: lld-link /verbose /msvclto /out:%t.exe /entry:main %t.obj \
+; RUN:     %T/msvclto-order-a.lib %T/msvclto-order-b.lib > %t.log || true
+; RUN: FileCheck %s < %t.log
+
+; CHECK: : link.exe
+; CHECK-NOT: .lib{{$}}
+; CHECK: lld-msvclto-order-a{{.*}}.obj
+; CHECK-NOT: lld-msvclto-order-b{{.*}}.obj
+; CHECK: .lib{{$}}
+
+target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+declare void @foo()
+
+define i32 @main() {
+  call void @foo()
+  ret i32 0
+}




More information about the llvm-commits mailing list