Skip to content

Commit 46d6a53

Browse files
committed
[lld][ELF] Only convert dependency filename to native form on Windows
Currently, llvm::sys::path::native is used unconditionally when generating dependency filenames. This is correct on Windows, where backslashes are valid path separators, but can be incorrect on non-Windows platforms, because in that case backslashes in the filenames are converted to forward slashes, while they should be treated as ordinary characters. This fixes the following inconsistency between ld.lld and ld.bfd: $ cat test.s .globl _start _start: $ cc -c test.s -o "back\\slash.o" $ ld.lld -o test "back\\slash.o" --dependency-file=/dev/stdout test: \ back/slash.o back/slash.o: $ ld.bfd -o test "back\\slash.o" --dependency-file=/dev/stdout test: \ back\slash.o back\slash.o: In addition, while we're here, use 2-space indentation when printing out dependency filenames (consistent with Clang and ld.bfd), and escape the filename of the target (output file) too. Signed-off-by: Ruoyu Zhong <[email protected]>
1 parent b2574c9 commit 46d6a53

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

lld/ELF/Driver.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,10 +2483,14 @@ static void writeDependencyFile(Ctx &ctx) {
24832483
// We use the same escape rules as Clang/GCC which are accepted by Make/Ninja:
24842484
// * A space is escaped by a backslash which itself must be escaped.
24852485
// * A hash sign is escaped by a single backslash.
2486-
// * $ is escapes as $$.
2486+
// * $ is escaped as $$.
24872487
auto printFilename = [](raw_fd_ostream &os, StringRef filename) {
24882488
llvm::SmallString<256> nativePath;
2489+
#ifdef _WIN32
24892490
llvm::sys::path::native(filename.str(), nativePath);
2491+
#else
2492+
nativePath = filename;
2493+
#endif
24902494
llvm::sys::path::remove_dots(nativePath, /*remove_dot_dot=*/true);
24912495
for (unsigned i = 0, e = nativePath.size(); i != e; ++i) {
24922496
if (nativePath[i] == '#') {
@@ -2503,9 +2507,10 @@ static void writeDependencyFile(Ctx &ctx) {
25032507
}
25042508
};
25052509

2506-
os << ctx.arg.outputFile << ":";
2510+
printFilename(os, ctx.arg.outputFile);
2511+
os << ":";
25072512
for (StringRef path : ctx.arg.dependencyFiles) {
2508-
os << " \\\n ";
2513+
os << " \\\n ";
25092514
printFilename(os, path);
25102515
}
25112516
os << "\n";
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# REQUIRES: x86, !system-windows
2+
# RUN: mkdir -p %t
3+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o "%t/back\\slash.o"
4+
# RUN: ld.lld -o %t/foo.exe "%t/back\\slash.o" --dependency-file=%t/foo.d
5+
# RUN: FileCheck --match-full-lines -DFILE=%t %s < %t/foo.d
6+
7+
# CHECK: [[FILE]]/foo.exe: \
8+
# CHECK-NEXT: [[FILE]]/back\slash.o
9+
# CHECK-EMPTY:
10+
# CHECK-NEXT: [[FILE]]/back\slash.o:
11+
12+
.global _start
13+
_start:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# REQUIRES: x86, system-windows
2+
# RUN: mkdir -p %t/back
3+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o "%t/back\\slash.o"
4+
# RUN: ld.lld -o %t/foo.exe "%t/back\\slash.o" --dependency-file=%t/foo.d
5+
# RUN: FileCheck --match-full-lines -DFILE=%t %s < %t/foo.d
6+
7+
# CHECK: [[FILE]]\foo.exe: \
8+
# CHECK-NEXT: [[FILE]]\back\slash.o
9+
# CHECK-EMPTY:
10+
# CHECK-NEXT: [[FILE]]\back\slash.o:
11+
12+
.global _start
13+
_start:

0 commit comments

Comments
 (0)