Skip to content

[LLD][COFF] Make /wholearchive thin-archive member identifiers consistent #145487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,13 @@ void LinkerDriver::addBuffer(std::unique_ptr<MemoryBuffer> mb,
make<std::unique_ptr<Archive>>(std::move(file)); // take ownership

int memberIndex = 0;
for (MemoryBufferRef m : getArchiveMembers(ctx, archive))
addArchiveBuffer(m, "<whole-archive>", filename, memberIndex++);
for (MemoryBufferRef m : getArchiveMembers(ctx, archive)) {
if (!archive->isThin())
addArchiveBuffer(m, "<whole-archive>", filename, memberIndex++);
else
addThinArchiveBuffer(m, "<whole-archive>");
}

return;
}
addFile(make<ArchiveFile>(ctx, mbref));
Expand Down Expand Up @@ -386,6 +391,14 @@ void LinkerDriver::addArchiveBuffer(MemoryBufferRef mb, StringRef symName,
Log(ctx) << "Loaded " << obj << " for " << symName;
}

void LinkerDriver::addThinArchiveBuffer(MemoryBufferRef mb, StringRef symName) {
// Pass an empty string as the archive name and an offset of 0 so that
// the original filename is used as the buffer identifier. This is
// useful for DTLTO, where having the member identifier be the actual
// path on disk enables distribution of bitcode files during ThinLTO.
addArchiveBuffer(mb, symName, /*parentName=*/"", /*OffsetInArchive=*/0);
}

void LinkerDriver::enqueueArchiveMember(const Archive::Child &c,
const Archive::Symbol &sym,
StringRef parentName) {
Expand Down Expand Up @@ -422,11 +435,8 @@ void LinkerDriver::enqueueArchiveMember(const Archive::Child &c,
reportBufferError(errorCodeToError(mbOrErr.second), childName);
llvm::TimeTraceScope timeScope("Archive: ",
mbOrErr.first->getBufferIdentifier());
// Pass empty string as archive name so that the original filename is
// used as the buffer identifier.
ctx.driver.addArchiveBuffer(takeBuffer(std::move(mbOrErr.first)),
toCOFFString(ctx, sym), "",
/*OffsetInArchive=*/0);
ctx.driver.addThinArchiveBuffer(takeBuffer(std::move(mbOrErr.first)),
toCOFFString(ctx, sym));
});
}

Expand Down
1 change: 1 addition & 0 deletions lld/COFF/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class LinkerDriver {
bool lazy);
void addArchiveBuffer(MemoryBufferRef mbref, StringRef symName,
StringRef parentName, uint64_t offsetInArchive);
void addThinArchiveBuffer(MemoryBufferRef mbref, StringRef symName);

void enqueueTask(std::function<void()> task);
bool run();
Expand Down
39 changes: 25 additions & 14 deletions lld/test/COFF/thin-archive.s
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,34 @@
# SYMTAB: ?f@@YAHXZ in
# NO-SYMTAB-NOT: ?f@@YAHXZ in

# RUN: lld-link /entry:main %t.main.obj %t.lib /out:%t.exe 2>&1 | \
# RUN: FileCheck --allow-empty %s
# RUN: lld-link /entry:main %t.main.obj %t_thin.lib /out:%t.exe 2>&1 | \
# RUN: FileCheck --allow-empty %s
# RUN: lld-link /entry:main %t.main.obj /wholearchive:%t_thin.lib /out:%t.exe 2>&1 | \
# RUN: FileCheck --allow-empty %s
# RUN: echo "/entry:main \"%t.main.obj\" /out:\"%t.exe\"" > %t.rsp

# RUN: lld-link @%t.rsp %t.lib /verbose 2>&1 | \
# RUN: FileCheck %s --check-prefix=LOAD_NON_THIN
# RUN: lld-link @%t.rsp %t_thin.lib /verbose 2>&1 | \
# RUN: FileCheck %s --check-prefix=LOAD_THIN_SYM
# RUN: lld-link @%t.rsp /wholearchive:%t_thin.lib /verbose 2>&1 | \
# RUN: FileCheck %s --check-prefix=LOAD_THIN_WHOLE
# RUN: lld-link @%t.rsp /wholearchive %t_thin.lib /verbose 2>&1 | \
# RUN: FileCheck %s --check-prefix=LOAD_THIN_WHOLE

# LOAD_NON_THIN: Loaded {{.*}}.lib({{.*}}.obj) for int __cdecl f(void)
# LOAD_THIN_SYM: Loaded {{.*}}.obj for int __cdecl f(void)
# LOAD_THIN_WHOLE: Loaded {{.*}}.obj for <whole-archive>

# RUN: rm %t.lib.obj
# RUN: lld-link /entry:main %t.main.obj %t.lib /out:%t.exe 2>&1 | \
# RUN: FileCheck --allow-empty %s
# RUN: env LLD_IN_TEST=1 not lld-link /entry:main %t.main.obj %t_thin.lib \
# RUN: /out:%t.exe 2>&1 | FileCheck --check-prefix=NOOBJ %s
# RUN: env LLD_IN_TEST=1 not lld-link /entry:main %t.main.obj %t_thin.lib /out:%t.exe \
# RUN: /demangle:no 2>&1 | FileCheck --check-prefix=NOOBJNODEMANGLE %s

# CHECK-NOT: error: could not get the buffer for the member defining
# RUN: lld-link @%t.rsp %t.lib 2>&1 | \
# RUN: FileCheck %s --check-prefix=ERR --allow-empty
# RUN: env LLD_IN_TEST=1 not lld-link @%t.rsp %t_thin.lib 2>&1 | \
# RUN: FileCheck %s --check-prefix=NOOBJ
# RUN: env LLD_IN_TEST=1 not lld-link @%t.rsp /wholearchive:%t_thin.lib 2>&1 | \
# RUN: FileCheck %s --check-prefix=NOOBJWHOLE
# RUN: env LLD_IN_TEST=1 not lld-link @%t.rsp %t_thin.lib /demangle:no 2>&1 | \
# RUN: FileCheck %s --check-prefix=NOOBJNODEMANGLE

# ERR-NOT: error: could not get the buffer for the member defining
# NOOBJ: error: could not get the buffer for the member defining symbol int __cdecl f(void): {{.*}}.lib({{.*}}.lib.obj):
# NOOBJWHOLE: error: {{.*}}.lib: could not get the buffer for a child of the archive: '{{.*}}.obj'
# NOOBJNODEMANGLE: error: could not get the buffer for the member defining symbol ?f@@YAHXZ: {{.*}}.lib({{.*}}.lib.obj):

.text
Expand Down
Loading