Skip to content

Commit 64a274f

Browse files
bd1976bristru
authored andcommitted
[LLD][COFF] Make /wholearchive thin-archive member identifiers consistent (#145487)
A thin archive is an archive/library format where the archive itself contains only references to member object files on disk, rather than embedding the file contents. For the non-/wholearchive case, we use the path to the archive member as the identifier for thin-archive members (see comments in `enqueueArchiveMember`). This patch modifies the /wholearchive path to behave the same way. Apart from consistency, my motivation for fixing this is DTLTO (#126654), where having the member identifier be the path on disk allows distribution of bitcode members during ThinLTO. (cherry picked from commit 9f733f4)
1 parent f66e874 commit 64a274f

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

lld/COFF/Driver.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,13 @@ void LinkerDriver::addBuffer(std::unique_ptr<MemoryBuffer> mb,
274274
make<std::unique_ptr<Archive>>(std::move(file)); // take ownership
275275

276276
int memberIndex = 0;
277-
for (MemoryBufferRef m : getArchiveMembers(ctx, archive))
278-
addArchiveBuffer(m, "<whole-archive>", filename, memberIndex++);
277+
for (MemoryBufferRef m : getArchiveMembers(ctx, archive)) {
278+
if (!archive->isThin())
279+
addArchiveBuffer(m, "<whole-archive>", filename, memberIndex++);
280+
else
281+
addThinArchiveBuffer(m, "<whole-archive>");
282+
}
283+
279284
return;
280285
}
281286
addFile(make<ArchiveFile>(ctx, mbref));
@@ -386,6 +391,14 @@ void LinkerDriver::addArchiveBuffer(MemoryBufferRef mb, StringRef symName,
386391
Log(ctx) << "Loaded " << obj << " for " << symName;
387392
}
388393

394+
void LinkerDriver::addThinArchiveBuffer(MemoryBufferRef mb, StringRef symName) {
395+
// Pass an empty string as the archive name and an offset of 0 so that
396+
// the original filename is used as the buffer identifier. This is
397+
// useful for DTLTO, where having the member identifier be the actual
398+
// path on disk enables distribution of bitcode files during ThinLTO.
399+
addArchiveBuffer(mb, symName, /*parentName=*/"", /*OffsetInArchive=*/0);
400+
}
401+
389402
void LinkerDriver::enqueueArchiveMember(const Archive::Child &c,
390403
const Archive::Symbol &sym,
391404
StringRef parentName) {
@@ -422,11 +435,8 @@ void LinkerDriver::enqueueArchiveMember(const Archive::Child &c,
422435
reportBufferError(errorCodeToError(mbOrErr.second), childName);
423436
llvm::TimeTraceScope timeScope("Archive: ",
424437
mbOrErr.first->getBufferIdentifier());
425-
// Pass empty string as archive name so that the original filename is
426-
// used as the buffer identifier.
427-
ctx.driver.addArchiveBuffer(takeBuffer(std::move(mbOrErr.first)),
428-
toCOFFString(ctx, sym), "",
429-
/*OffsetInArchive=*/0);
438+
ctx.driver.addThinArchiveBuffer(takeBuffer(std::move(mbOrErr.first)),
439+
toCOFFString(ctx, sym));
430440
});
431441
}
432442

lld/COFF/Driver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class LinkerDriver {
173173
bool lazy);
174174
void addArchiveBuffer(MemoryBufferRef mbref, StringRef symName,
175175
StringRef parentName, uint64_t offsetInArchive);
176+
void addThinArchiveBuffer(MemoryBufferRef mbref, StringRef symName);
176177

177178
void enqueueTask(std::function<void()> task);
178179
bool run();

lld/test/COFF/thin-archive.s

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,34 @@
2222
# SYMTAB: ?f@@YAHXZ in
2323
# NO-SYMTAB-NOT: ?f@@YAHXZ in
2424

25-
# RUN: lld-link /entry:main %t.main.obj %t.lib /out:%t.exe 2>&1 | \
26-
# RUN: FileCheck --allow-empty %s
27-
# RUN: lld-link /entry:main %t.main.obj %t_thin.lib /out:%t.exe 2>&1 | \
28-
# RUN: FileCheck --allow-empty %s
29-
# RUN: lld-link /entry:main %t.main.obj /wholearchive:%t_thin.lib /out:%t.exe 2>&1 | \
30-
# RUN: FileCheck --allow-empty %s
25+
# RUN: echo "/entry:main \"%t.main.obj\" /out:\"%t.exe\"" > %t.rsp
26+
27+
# RUN: lld-link @%t.rsp %t.lib /verbose 2>&1 | \
28+
# RUN: FileCheck %s --check-prefix=LOAD_NON_THIN
29+
# RUN: lld-link @%t.rsp %t_thin.lib /verbose 2>&1 | \
30+
# RUN: FileCheck %s --check-prefix=LOAD_THIN_SYM
31+
# RUN: lld-link @%t.rsp /wholearchive:%t_thin.lib /verbose 2>&1 | \
32+
# RUN: FileCheck %s --check-prefix=LOAD_THIN_WHOLE
33+
# RUN: lld-link @%t.rsp /wholearchive %t_thin.lib /verbose 2>&1 | \
34+
# RUN: FileCheck %s --check-prefix=LOAD_THIN_WHOLE
35+
36+
# LOAD_NON_THIN: Loaded {{.*}}.lib({{.*}}.obj) for int __cdecl f(void)
37+
# LOAD_THIN_SYM: Loaded {{.*}}.obj for int __cdecl f(void)
38+
# LOAD_THIN_WHOLE: Loaded {{.*}}.obj for <whole-archive>
3139

3240
# RUN: rm %t.lib.obj
33-
# RUN: lld-link /entry:main %t.main.obj %t.lib /out:%t.exe 2>&1 | \
34-
# RUN: FileCheck --allow-empty %s
35-
# RUN: env LLD_IN_TEST=1 not lld-link /entry:main %t.main.obj %t_thin.lib \
36-
# RUN: /out:%t.exe 2>&1 | FileCheck --check-prefix=NOOBJ %s
37-
# RUN: env LLD_IN_TEST=1 not lld-link /entry:main %t.main.obj %t_thin.lib /out:%t.exe \
38-
# RUN: /demangle:no 2>&1 | FileCheck --check-prefix=NOOBJNODEMANGLE %s
39-
40-
# CHECK-NOT: error: could not get the buffer for the member defining
41+
# RUN: lld-link @%t.rsp %t.lib 2>&1 | \
42+
# RUN: FileCheck %s --check-prefix=ERR --allow-empty
43+
# RUN: env LLD_IN_TEST=1 not lld-link @%t.rsp %t_thin.lib 2>&1 | \
44+
# RUN: FileCheck %s --check-prefix=NOOBJ
45+
# RUN: env LLD_IN_TEST=1 not lld-link @%t.rsp /wholearchive:%t_thin.lib 2>&1 | \
46+
# RUN: FileCheck %s --check-prefix=NOOBJWHOLE
47+
# RUN: env LLD_IN_TEST=1 not lld-link @%t.rsp %t_thin.lib /demangle:no 2>&1 | \
48+
# RUN: FileCheck %s --check-prefix=NOOBJNODEMANGLE
49+
50+
# ERR-NOT: error: could not get the buffer for the member defining
4151
# NOOBJ: error: could not get the buffer for the member defining symbol int __cdecl f(void): {{.*}}.lib({{.*}}.lib.obj):
52+
# NOOBJWHOLE: error: {{.*}}.lib: could not get the buffer for a child of the archive: '{{.*}}.obj'
4253
# NOOBJNODEMANGLE: error: could not get the buffer for the member defining symbol ?f@@YAHXZ: {{.*}}.lib({{.*}}.lib.obj):
4354

4455
.text

0 commit comments

Comments
 (0)