xref: /freebsd/contrib/llvm-project/lld/Common/Filesystem.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===- Filesystem.cpp -----------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains a few utility functions to handle files.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "lld/Common/Filesystem.h"
14*5f757f3fSDimitry Andric #include "lld/Common/ErrorHandler.h"
150b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
160b57cec5SDimitry Andric #include "llvm/Support/FileOutputBuffer.h"
170b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
185ffd83dbSDimitry Andric #include "llvm/Support/Parallel.h"
195ffd83dbSDimitry Andric #include "llvm/Support/Path.h"
20*5f757f3fSDimitry Andric #include "llvm/Support/TimeProfiler.h"
210b57cec5SDimitry Andric #if LLVM_ON_UNIX
220b57cec5SDimitry Andric #include <unistd.h>
230b57cec5SDimitry Andric #endif
240b57cec5SDimitry Andric #include <thread>
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric using namespace llvm;
270b57cec5SDimitry Andric using namespace lld;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric // Removes a given file asynchronously. This is a performance hack,
300b57cec5SDimitry Andric // so remove this when operating systems are improved.
310b57cec5SDimitry Andric //
320b57cec5SDimitry Andric // On Linux (and probably on other Unix-like systems), unlink(2) is a
330b57cec5SDimitry Andric // noticeably slow system call. As of 2016, unlink takes 250
340b57cec5SDimitry Andric // milliseconds to remove a 1 GB file on ext4 filesystem on my machine.
350b57cec5SDimitry Andric //
360b57cec5SDimitry Andric // To create a new result file, we first remove existing file. So, if
370b57cec5SDimitry Andric // you repeatedly link a 1 GB program in a regular compile-link-debug
380b57cec5SDimitry Andric // cycle, every cycle wastes 250 milliseconds only to remove a file.
390b57cec5SDimitry Andric // Since LLD can link a 1 GB binary in about 5 seconds, that waste
400b57cec5SDimitry Andric // actually counts.
410b57cec5SDimitry Andric //
420b57cec5SDimitry Andric // This function spawns a background thread to remove the file.
430b57cec5SDimitry Andric // The calling thread returns almost immediately.
440b57cec5SDimitry Andric void lld::unlinkAsync(StringRef path) {
455ffd83dbSDimitry Andric   if (!sys::fs::exists(path) || !sys::fs::is_regular_file(path))
465ffd83dbSDimitry Andric     return;
475ffd83dbSDimitry Andric 
480b57cec5SDimitry Andric // Removing a file is async on windows.
490b57cec5SDimitry Andric #if defined(_WIN32)
505ffd83dbSDimitry Andric   // On Windows co-operative programs can be expected to open LLD's
515ffd83dbSDimitry Andric   // output in FILE_SHARE_DELETE mode. This allows us to delete the
525ffd83dbSDimitry Andric   // file (by moving it to a temporary filename and then deleting
535ffd83dbSDimitry Andric   // it) so that we can link another output file that overwrites
545ffd83dbSDimitry Andric   // the existing file, even if the current file is in use.
555ffd83dbSDimitry Andric   //
565ffd83dbSDimitry Andric   // This is done on a best effort basis - we do not error if the
575ffd83dbSDimitry Andric   // operation fails. The consequence is merely that the user
585ffd83dbSDimitry Andric   // experiences an inconvenient work-flow.
595ffd83dbSDimitry Andric   //
605ffd83dbSDimitry Andric   // The code here allows LLD to work on all versions of Windows.
615ffd83dbSDimitry Andric   // However, at Windows 10 1903 it seems that the behavior of
625ffd83dbSDimitry Andric   // Windows has changed, so that we could simply delete the output
635ffd83dbSDimitry Andric   // file. This code should be simplified once support for older
645ffd83dbSDimitry Andric   // versions of Windows is dropped.
655ffd83dbSDimitry Andric   //
665ffd83dbSDimitry Andric   // Warning: It seems that the WINVER and _WIN32_WINNT preprocessor
675ffd83dbSDimitry Andric   // defines affect the behavior of the Windows versions of the calls
685ffd83dbSDimitry Andric   // we are using here. If this code stops working this is worth
695ffd83dbSDimitry Andric   // bearing in mind.
705ffd83dbSDimitry Andric   SmallString<128> tmpName;
715ffd83dbSDimitry Andric   if (!sys::fs::createUniqueFile(path + "%%%%%%%%.tmp", tmpName)) {
725ffd83dbSDimitry Andric     if (!sys::fs::rename(path, tmpName))
735ffd83dbSDimitry Andric       path = tmpName;
745ffd83dbSDimitry Andric     else
755ffd83dbSDimitry Andric       sys::fs::remove(tmpName);
765ffd83dbSDimitry Andric   }
770b57cec5SDimitry Andric   sys::fs::remove(path);
780b57cec5SDimitry Andric #else
795ffd83dbSDimitry Andric   if (parallel::strategy.ThreadsRequested == 1)
800b57cec5SDimitry Andric     return;
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   // We cannot just remove path from a different thread because we are now going
830b57cec5SDimitry Andric   // to create path as a new file.
840b57cec5SDimitry Andric   // Instead we open the file and unlink it on this thread. The unlink is fast
850b57cec5SDimitry Andric   // since the open fd guarantees that it is not removing the last reference.
860b57cec5SDimitry Andric   int fd;
870b57cec5SDimitry Andric   std::error_code ec = sys::fs::openFileForRead(path, fd);
880b57cec5SDimitry Andric   sys::fs::remove(path);
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   if (ec)
910b57cec5SDimitry Andric     return;
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   // close and therefore remove TempPath in background.
940b57cec5SDimitry Andric   std::mutex m;
950b57cec5SDimitry Andric   std::condition_variable cv;
960b57cec5SDimitry Andric   bool started = false;
970b57cec5SDimitry Andric   std::thread([&, fd] {
980b57cec5SDimitry Andric     {
990b57cec5SDimitry Andric       std::lock_guard<std::mutex> l(m);
1000b57cec5SDimitry Andric       started = true;
1010b57cec5SDimitry Andric       cv.notify_all();
1020b57cec5SDimitry Andric     }
1030b57cec5SDimitry Andric     ::close(fd);
1040b57cec5SDimitry Andric   }).detach();
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   // GLIBC 2.26 and earlier have race condition that crashes an entire process
1070b57cec5SDimitry Andric   // if the main thread calls exit(2) while other thread is starting up.
1080b57cec5SDimitry Andric   std::unique_lock<std::mutex> l(m);
1090b57cec5SDimitry Andric   cv.wait(l, [&] { return started; });
1100b57cec5SDimitry Andric #endif
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric // Simulate file creation to see if Path is writable.
1140b57cec5SDimitry Andric //
1150b57cec5SDimitry Andric // Determining whether a file is writable or not is amazingly hard,
1160b57cec5SDimitry Andric // and after all the only reliable way of doing that is to actually
1170b57cec5SDimitry Andric // create a file. But we don't want to do that in this function
1180b57cec5SDimitry Andric // because LLD shouldn't update any file if it will end in a failure.
1190b57cec5SDimitry Andric // We also don't want to reimplement heuristics to determine if a
1200b57cec5SDimitry Andric // file is writable. So we'll let FileOutputBuffer do the work.
1210b57cec5SDimitry Andric //
122480093f4SDimitry Andric // FileOutputBuffer doesn't touch a destination file until commit()
1230b57cec5SDimitry Andric // is called. We use that class without calling commit() to predict
1240b57cec5SDimitry Andric // if the given file is writable.
1250b57cec5SDimitry Andric std::error_code lld::tryCreateFile(StringRef path) {
126*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Try create output file");
1270b57cec5SDimitry Andric   if (path.empty())
1280b57cec5SDimitry Andric     return std::error_code();
1290b57cec5SDimitry Andric   if (path == "-")
1300b57cec5SDimitry Andric     return std::error_code();
1310b57cec5SDimitry Andric   return errorToErrorCode(FileOutputBuffer::create(path, 1).takeError());
1320b57cec5SDimitry Andric }
133*5f757f3fSDimitry Andric 
134*5f757f3fSDimitry Andric // Creates an empty file to and returns a raw_fd_ostream to write to it.
135*5f757f3fSDimitry Andric std::unique_ptr<raw_fd_ostream> lld::openFile(StringRef file) {
136*5f757f3fSDimitry Andric   std::error_code ec;
137*5f757f3fSDimitry Andric   auto ret =
138*5f757f3fSDimitry Andric       std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
139*5f757f3fSDimitry Andric   if (ec) {
140*5f757f3fSDimitry Andric     error("cannot open " + file + ": " + ec.message());
141*5f757f3fSDimitry Andric     return nullptr;
142*5f757f3fSDimitry Andric   }
143*5f757f3fSDimitry Andric   return ret;
144*5f757f3fSDimitry Andric }
145*5f757f3fSDimitry Andric 
146*5f757f3fSDimitry Andric // The merged bitcode after LTO is large. Try opening a file stream that
147*5f757f3fSDimitry Andric // supports reading, seeking and writing. Such a file allows BitcodeWriter to
148*5f757f3fSDimitry Andric // flush buffered data to reduce memory consumption. If this fails, open a file
149*5f757f3fSDimitry Andric // stream that supports only write.
150*5f757f3fSDimitry Andric std::unique_ptr<raw_fd_ostream> lld::openLTOOutputFile(StringRef file) {
151*5f757f3fSDimitry Andric   std::error_code ec;
152*5f757f3fSDimitry Andric   std::unique_ptr<raw_fd_ostream> fs =
153*5f757f3fSDimitry Andric       std::make_unique<raw_fd_stream>(file, ec);
154*5f757f3fSDimitry Andric   if (!ec)
155*5f757f3fSDimitry Andric     return fs;
156*5f757f3fSDimitry Andric   return openFile(file);
157*5f757f3fSDimitry Andric }
158