Skip to content

Commit c912e98

Browse files
authored
[llvm-lib] Don't rewrite paths for members in non-thin archives (#123416)
This matches what MS lib.exe does (and llvm-ar too); when adding files to an archive, MS lib.exe stores the file name as it was given on the command line, whereas llvm-lib rewrote it into a relative path name, relative to the archive location. Such a rewrite makes sense for thin archives, but not for regular archives. (MS lib.exe doesn't support producing thin archives; that's an LLVM extension - see the thin-relative.test testcase.) The behaviour to rewrite these paths was added in 451c2ef; it is unclear why it was chosen to do the rewriting for non-thin archives as well. This quirk is even pointed out in a code comment - but neither the code review at https://reviews.llvm.org/D57842 nor the linked bug report at https://crbug.com/41440160 mentions why this is done for all archives, not only thin ones. Therefore, assume that this only was done out of convenience, and change llvm-lib to not adjust the paths for non-thin archives. Normally, the actual member names doesn't matter for non-thin archives; however for short import libraries, where each member is named e.g. "foo.dll", the names do matter. If using llvm-lib to merge two import libraries (as a non-thin library), preserve the original names rather than making the member names relative.
1 parent 671ec34 commit c912e98

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -503,23 +503,22 @@ int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) {
503503
return 1;
504504
}
505505
}
506-
// llvm-lib uses relative paths for both regular and thin archives, unlike
507-
// standard GNU ar, which only uses relative paths for thin archives and
508-
// basenames for regular archives.
509-
for (NewArchiveMember &Member : Members) {
510-
if (sys::path::is_relative(Member.MemberName)) {
511-
Expected<std::string> PathOrErr =
512-
computeArchiveRelativePath(OutputPath, Member.MemberName);
513-
if (PathOrErr)
514-
Member.MemberName = Saver.save(*PathOrErr);
506+
507+
bool Thin = Args.hasArg(OPT_llvmlibthin);
508+
if (Thin) {
509+
for (NewArchiveMember &Member : Members) {
510+
if (sys::path::is_relative(Member.MemberName)) {
511+
Expected<std::string> PathOrErr =
512+
computeArchiveRelativePath(OutputPath, Member.MemberName);
513+
if (PathOrErr)
514+
Member.MemberName = Saver.save(*PathOrErr);
515+
}
515516
}
516517
}
517518

518519
// For compatibility with MSVC, reverse member vector after de-duplication.
519520
std::reverse(Members.begin(), Members.end());
520521

521-
bool Thin = Args.hasArg(OPT_llvmlibthin);
522-
523522
auto Symtab = Args.hasFlag(OPT_llvmlibindex, OPT_llvmlibindex_no,
524523
/*default=*/true)
525524
? SymtabWritingMode::NormalSymtab
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
RUN: rm -rf %t
2+
RUN: mkdir -p %t/foo
3+
RUN: split-file %s %t
4+
RUN: cd %t
5+
6+
RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o foo/obj.o %S/Inputs/a.s
7+
8+
# For a regular, non-thin archive, check that we store the path in the form
9+
# it was passed (foo/obj.o), not as a path relative to the archive.
10+
RUN: llvm-lib -out:foo/regular.a foo/obj.o
11+
RUN: llvm-lib -list foo/regular.a | FileCheck %s --check-prefix=REGULAR --match-full-lines
12+
REGULAR: foo/obj.o
13+
14+
# When merging two import libraries, make sure that the member names stay
15+
# unchanged.
16+
RUN: llvm-lib -machine:x64 -out:foo.lib -def:foo.def
17+
RUN: llvm-lib -machine:x64 -out:bar.lib -def:bar.def
18+
RUN: llvm-lib -out:foo/merged.lib foo.lib bar.lib
19+
RUN: llvm-lib -list foo/merged.lib | FileCheck %s --check-prefix=MERGED --match-full-lines
20+
MERGED: foo.dll
21+
MERGED: foo.dll
22+
MERGED: foo.dll
23+
MERGED: foo.dll
24+
MERGED: bar.dll
25+
MERGED: bar.dll
26+
MERGED: bar.dll
27+
MERGED: bar.dll
28+
29+
#--- foo.def
30+
LIBRARY foo.dll
31+
EXPORTS
32+
func1
33+
34+
#--- bar.def
35+
LIBRARY bar.dll
36+
EXPORTS
37+
func2

0 commit comments

Comments
 (0)