Skip to content

Commit 80917d8

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 2cacf71 commit 80917d8

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
@@ -2482,10 +2482,14 @@ static void writeDependencyFile(Ctx &ctx) {
24822482
// We use the same escape rules as Clang/GCC which are accepted by Make/Ninja:
24832483
// * A space is escaped by a backslash which itself must be escaped.
24842484
// * A hash sign is escaped by a single backslash.
2485-
// * $ is escapes as $$.
2485+
// * $ is escaped as $$.
24862486
auto printFilename = [](raw_fd_ostream &os, StringRef filename) {
24872487
llvm::SmallString<256> nativePath;
2488+
#ifdef _WIN32
24882489
llvm::sys::path::native(filename.str(), nativePath);
2490+
#else
2491+
nativePath = filename;
2492+
#endif
24892493
llvm::sys::path::remove_dots(nativePath, /*remove_dot_dot=*/true);
24902494
for (unsigned i = 0, e = nativePath.size(); i != e; ++i) {
24912495
if (nativePath[i] == '#') {
@@ -2502,9 +2506,10 @@ static void writeDependencyFile(Ctx &ctx) {
25022506
}
25032507
};
25042508

2505-
os << ctx.arg.outputFile << ":";
2509+
printFilename(os, ctx.arg.outputFile);
2510+
os << ":";
25062511
for (StringRef path : ctx.arg.dependencyFiles) {
2507-
os << " \\\n ";
2512+
os << " \\\n ";
25082513
printFilename(os, path);
25092514
}
25102515
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)