Index: lld/trunk/COFF/Driver.cpp =================================================================== --- lld/trunk/COFF/Driver.cpp +++ lld/trunk/COFF/Driver.cpp @@ -512,6 +512,23 @@ std::string Rsp = "/nologo\n"; std::vector 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: Index: lld/trunk/COFF/InputFiles.h =================================================================== --- lld/trunk/COFF/InputFiles.h +++ lld/trunk/COFF/InputFiles.h @@ -58,6 +58,8 @@ // 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 @@ protected: InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {} - MemoryBufferRef MB; std::string Directives; private: Index: lld/trunk/test/COFF/Inputs/msvclto-order-a.ll =================================================================== --- lld/trunk/test/COFF/Inputs/msvclto-order-a.ll +++ lld/trunk/test/COFF/Inputs/msvclto-order-a.ll @@ -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 +} + Index: lld/trunk/test/COFF/Inputs/msvclto-order-b.ll =================================================================== --- lld/trunk/test/COFF/Inputs/msvclto-order-b.ll +++ lld/trunk/test/COFF/Inputs/msvclto-order-b.ll @@ -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 +} + Index: lld/trunk/test/COFF/msvclto-order.ll =================================================================== --- lld/trunk/test/COFF/msvclto-order.ll +++ lld/trunk/test/COFF/msvclto-order.ll @@ -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 +}