Skip to content

Conversation

ZhongRuoyu
Copy link
Contributor

@ZhongRuoyu ZhongRuoyu commented Sep 26, 2025

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 target filename too.

See also:

If these PRs are accepted then I'll try to extract the logic into a new llvm/support helper class. These changes are presented as separate PRs because each of them change the behavior slightly differently.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Sep 26, 2025

@llvm/pr-subscribers-lld-elf

@llvm/pr-subscribers-lld

Author: Ruoyu Zhong (ZhongRuoyu)

Changes

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 target filename too.


Full diff: https://github.com/llvm/llvm-project/pull/160927.diff

3 Files Affected:

  • (modified) lld/ELF/Driver.cpp (+8-3)
  • (added) lld/test/ELF/dependency-file-backslash-as-normal-char.s (+13)
  • (added) lld/test/ELF/dependency-file-backslash-as-path-separator.s (+13)
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 1beab8d33f4ba..8483e01364982 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -2482,10 +2482,14 @@ static void writeDependencyFile(Ctx &ctx) {
   // We use the same escape rules as Clang/GCC which are accepted by Make/Ninja:
   // * A space is escaped by a backslash which itself must be escaped.
   // * A hash sign is escaped by a single backslash.
-  // * $ is escapes as $$.
+  // * $ is escaped as $$.
   auto printFilename = [](raw_fd_ostream &os, StringRef filename) {
     llvm::SmallString<256> nativePath;
+#ifdef _WIN32
     llvm::sys::path::native(filename.str(), nativePath);
+#else
+    nativePath = filename;
+#endif
     llvm::sys::path::remove_dots(nativePath, /*remove_dot_dot=*/true);
     for (unsigned i = 0, e = nativePath.size(); i != e; ++i) {
       if (nativePath[i] == '#') {
@@ -2502,9 +2506,10 @@ static void writeDependencyFile(Ctx &ctx) {
     }
   };
 
-  os << ctx.arg.outputFile << ":";
+  printFilename(os, ctx.arg.outputFile);
+  os << ":";
   for (StringRef path : ctx.arg.dependencyFiles) {
-    os << " \\\n ";
+    os << " \\\n  ";
     printFilename(os, path);
   }
   os << "\n";
diff --git a/lld/test/ELF/dependency-file-backslash-as-normal-char.s b/lld/test/ELF/dependency-file-backslash-as-normal-char.s
new file mode 100644
index 0000000000000..278e06d41d683
--- /dev/null
+++ b/lld/test/ELF/dependency-file-backslash-as-normal-char.s
@@ -0,0 +1,13 @@
+# REQUIRES: x86, !system-windows
+# RUN: mkdir -p %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o "%t/back\\slash.o"
+# RUN: ld.lld -o %t/foo.exe "%t/back\\slash.o" --dependency-file=%t/foo.d
+# RUN: FileCheck --match-full-lines -DFILE=%t %s < %t/foo.d
+
+# CHECK:      [[FILE]]/foo.exe: \
+# CHECK-NEXT:   [[FILE]]/back\slash.o
+# CHECK-EMPTY:
+# CHECK-NEXT: [[FILE]]/back\slash.o:
+
+.global _start
+_start:
diff --git a/lld/test/ELF/dependency-file-backslash-as-path-separator.s b/lld/test/ELF/dependency-file-backslash-as-path-separator.s
new file mode 100644
index 0000000000000..6e738626c4127
--- /dev/null
+++ b/lld/test/ELF/dependency-file-backslash-as-path-separator.s
@@ -0,0 +1,13 @@
+# REQUIRES: x86, system-windows
+# RUN: mkdir -p %t/back
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o "%t/back\\slash.o"
+# RUN: ld.lld -o %t/foo.exe "%t/back\\slash.o" --dependency-file=%t/foo.d
+# RUN: FileCheck --match-full-lines -DFILE=%t %s < %t/foo.d
+
+# CHECK:      [[FILE]]\foo.exe: \
+# CHECK-NEXT:   [[FILE]]\back\slash.o
+# CHECK-EMPTY:
+# CHECK-NEXT: [[FILE]]\back\slash.o:
+
+.global _start
+_start:

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 <zhongruoyu@outlook.com>
@ZhongRuoyu ZhongRuoyu changed the title [lld][ELF] Only convert dependdency filename to native form on Windows [lld][ELF] Only convert dependency filename to native form on Windows Sep 26, 2025
@ZhongRuoyu ZhongRuoyu force-pushed the lld-dep-filename-backslashes branch from 93d3cb4 to 80917d8 Compare September 26, 2025 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants