[PATCH] D141145: [fs] Prevent symlink destroying.

Dmitriy Chestnykh via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 6 10:02:49 PST 2023


chestnykh created this revision.
chestnykh added a reviewer: jakehehrlich.
Herald added a subscriber: hiraditya.
Herald added a reviewer: alexander-shaposhnikov.
Herald added a reviewer: jhenderson.
Herald added a project: All.
chestnykh requested review of this revision.
Herald added subscribers: llvm-commits, MaskRay.
Herald added a project: LLVM.

If we open symlink with llvm-strip then
the current behaviour is to destroy it and create
new regular file. This behaviour is different from
the behaviour of binutils strip.
The new behaviour is to modify the real file
and to keep symlink.
PR#59848


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D141145

Files:
  llvm/lib/Support/raw_ostream.cpp
  llvm/test/tools/llvm-objcopy/keep-dso-symlink.test


Index: llvm/test/tools/llvm-objcopy/keep-dso-symlink.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-objcopy/keep-dso-symlink.test
@@ -0,0 +1,7 @@
+# RUN: touch empty.c
+# RUN: clang empty.c -shared -o empty.so.1
+# RUN: ln -sf empty.so.1 empty.so
+# RUN: llvm-strip --strip-unneeded empty.so
+# RUN: [ -L empty.so ] && echo "Still symlink" | FileCheck %s
+
+; CHECK: Still symlink
Index: llvm/lib/Support/raw_ostream.cpp
===================================================================
--- llvm/lib/Support/raw_ostream.cpp
+++ llvm/lib/Support/raw_ostream.cpp
@@ -982,17 +982,30 @@
 
 void buffer_unique_ostream::anchor() {}
 
-Error llvm::writeToOutput(StringRef OutputFileName,
+Error llvm::writeToOutput(StringRef OutputCandidateFileName,
                           std::function<Error(raw_ostream &)> Write) {
-  if (OutputFileName == "-")
+  if (OutputCandidateFileName == "-")
     return Write(outs());
 
-  if (OutputFileName == "/dev/null") {
+  if (OutputCandidateFileName == "/dev/null") {
     raw_null_ostream Out;
     return Write(Out);
   }
 
   unsigned Mode = sys::fs::all_read | sys::fs::all_write | sys::fs::all_exe;
+
+  llvm::SmallString<128> RealPathOfSymlink;
+  if (sys::fs::is_symlink_file(OutputCandidateFileName)) {
+    std::error_code EC =
+        sys::fs::real_path(OutputCandidateFileName, RealPathOfSymlink);
+    if (EC)
+      return errorCodeToError(EC);
+  }
+
+  Twine OutputFileName = RealPathOfSymlink.str().empty()
+                             ? OutputCandidateFileName
+                             : Twine(RealPathOfSymlink);
+
   Expected<sys::fs::TempFile> Temp =
       sys::fs::TempFile::create(OutputFileName + ".temp-stream-%%%%%%", Mode);
   if (!Temp)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141145.486914.patch
Type: text/x-patch
Size: 1773 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230106/2eb9bf16/attachment.bin>


More information about the llvm-commits mailing list