diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp --- a/lld/COFF/InputFiles.cpp +++ b/lld/COFF/InputFiles.cpp @@ -791,11 +791,15 @@ // causes a collision which result in only one of the objects being taken // into consideration at LTO time (which very likely causes undefined // symbols later in the link stage). So we append file offset to make - // filename unique. - MemoryBufferRef mbref( - mb.getBuffer(), - saver.save(archiveName + path + - (archiveName.empty() ? "" : utostr(offsetInArchive)))); + // filename unique. We also append the name of the output binary in case + // the object is compiled into multiple binaries. + StringRef binaryName = sys::path::filename(config->outputFile); + StringRef name = + archiveName.empty() + ? saver.save(path) + : saver.save(archiveName + path + utostr(offsetInArchive) + + (config->thinLTOIndexOnly ? "." + binaryName : "")); + MemoryBufferRef mbref(mb.getBuffer(), name); obj = check(lto::InputFile::create(mbref)); } diff --git a/lld/test/COFF/thinlto-archives.ll b/lld/test/COFF/thinlto-archives.ll --- a/lld/test/COFF/thinlto-archives.ll +++ b/lld/test/COFF/thinlto-archives.ll @@ -9,7 +9,14 @@ ; RUN: lld-link -out:%T/thinlto-archives/main.exe -entry:main \ ; RUN: -lldsavetemps -subsystem:console %T/thinlto-archives/main.obj \ ; RUN: %T/thinlto-archives/a.lib %T/thinlto-archives/b.lib -; RUN: FileCheck %s < %T/thinlto-archives/main.exe.resolution.txt +; RUN: FileCheck %s < %T/thinlto-archives/main.exe.resolution.txt \ +; RUN: --check-prefix=CHECK +; RUN: lld-link -out:%T/thinlto-archives/main2.exe -entry:main \ +; RUN: -lldsavetemps -subsystem:console %T/thinlto-archives/main.obj \ +; RUN: %T/thinlto-archives/a.lib %T/thinlto-archives/b.lib \ +; RUN: -thinlto-index-only +; RUN: FileCheck %s < %T/thinlto-archives/main2.exe.resolution.txt \ +; RUN: --check-prefix=THINLTO ; CHECK: {{/thinlto-archives/main.obj$}} ; CHECK: {{^-r=.*/thinlto-archives/main.obj,main,px$}} @@ -18,6 +25,13 @@ ; CHECK-NEXT: {{/thinlto-archives/b.libbar.obj[0-9]+$}} ; CHECK-NEXT: {{^-r=.*/thinlto-archives/b.libbar.obj[0-9]+,bar,p$}} +; THINLTO: {{/thinlto-archives/main.obj$}} +; THINLTO: {{^-r=.*/thinlto-archives/main.obj,main,px$}} +; THINLTO: {{/thinlto-archives/a.libbar.obj[0-9]+.main2.exe$}} +; THINLTO-NEXT: {{^-r=.*/thinlto-archives/a.libbar.obj[0-9]+.main2.exe,foo,p$}} +; THINLTO-NEXT: {{/thinlto-archives/b.libbar.obj[0-9]+.main2.exe$}} +; THINLTO-NEXT: {{^-r=.*/thinlto-archives/b.libbar.obj[0-9]+.main2.exe,bar,p$}} + target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-pc-windows-msvc"