1d2d3ebb8SDimitry Andric //===- Filesystem.cpp -----------------------------------------------------===//
2d2d3ebb8SDimitry Andric //
3f1e1c239SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4f1e1c239SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5f1e1c239SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d2d3ebb8SDimitry Andric //
7d2d3ebb8SDimitry Andric //===----------------------------------------------------------------------===//
8d2d3ebb8SDimitry Andric //
9d2d3ebb8SDimitry Andric // This file contains a few utility functions to handle files.
10d2d3ebb8SDimitry Andric //
11d2d3ebb8SDimitry Andric //===----------------------------------------------------------------------===//
12d2d3ebb8SDimitry Andric
13f1e1c239SDimitry Andric #include "lld/Common/Filesystem.h"
14b1c73532SDimitry Andric #include "lld/Common/ErrorHandler.h"
15eb1ff93dSDimitry Andric #include "llvm/Config/llvm-config.h"
16d2d3ebb8SDimitry Andric #include "llvm/Support/FileOutputBuffer.h"
17eb1ff93dSDimitry Andric #include "llvm/Support/FileSystem.h"
18cfca06d7SDimitry Andric #include "llvm/Support/Parallel.h"
19cfca06d7SDimitry Andric #include "llvm/Support/Path.h"
20b1c73532SDimitry Andric #include "llvm/Support/TimeProfiler.h"
21eb1ff93dSDimitry Andric #if LLVM_ON_UNIX
22eb1ff93dSDimitry Andric #include <unistd.h>
23eb1ff93dSDimitry Andric #endif
24d2d3ebb8SDimitry Andric #include <thread>
25d2d3ebb8SDimitry Andric
26d2d3ebb8SDimitry Andric using namespace llvm;
27d2d3ebb8SDimitry Andric using namespace lld;
28d2d3ebb8SDimitry Andric
29d2d3ebb8SDimitry Andric // Removes a given file asynchronously. This is a performance hack,
30d2d3ebb8SDimitry Andric // so remove this when operating systems are improved.
31d2d3ebb8SDimitry Andric //
32d2d3ebb8SDimitry Andric // On Linux (and probably on other Unix-like systems), unlink(2) is a
33d2d3ebb8SDimitry Andric // noticeably slow system call. As of 2016, unlink takes 250
34d2d3ebb8SDimitry Andric // milliseconds to remove a 1 GB file on ext4 filesystem on my machine.
35d2d3ebb8SDimitry Andric //
36d2d3ebb8SDimitry Andric // To create a new result file, we first remove existing file. So, if
37d2d3ebb8SDimitry Andric // you repeatedly link a 1 GB program in a regular compile-link-debug
38d2d3ebb8SDimitry Andric // cycle, every cycle wastes 250 milliseconds only to remove a file.
39d2d3ebb8SDimitry Andric // Since LLD can link a 1 GB binary in about 5 seconds, that waste
40d2d3ebb8SDimitry Andric // actually counts.
41d2d3ebb8SDimitry Andric //
42eb1ff93dSDimitry Andric // This function spawns a background thread to remove the file.
43d2d3ebb8SDimitry Andric // The calling thread returns almost immediately.
unlinkAsync(StringRef path)44f1e1c239SDimitry Andric void lld::unlinkAsync(StringRef path) {
45cfca06d7SDimitry Andric if (!sys::fs::exists(path) || !sys::fs::is_regular_file(path))
46cfca06d7SDimitry Andric return;
47cfca06d7SDimitry Andric
48eb1ff93dSDimitry Andric // Removing a file is async on windows.
4920d35e67SDimitry Andric #if defined(_WIN32)
50cfca06d7SDimitry Andric // On Windows co-operative programs can be expected to open LLD's
51cfca06d7SDimitry Andric // output in FILE_SHARE_DELETE mode. This allows us to delete the
52cfca06d7SDimitry Andric // file (by moving it to a temporary filename and then deleting
53cfca06d7SDimitry Andric // it) so that we can link another output file that overwrites
54cfca06d7SDimitry Andric // the existing file, even if the current file is in use.
55cfca06d7SDimitry Andric //
56cfca06d7SDimitry Andric // This is done on a best effort basis - we do not error if the
57cfca06d7SDimitry Andric // operation fails. The consequence is merely that the user
58cfca06d7SDimitry Andric // experiences an inconvenient work-flow.
59cfca06d7SDimitry Andric //
60cfca06d7SDimitry Andric // The code here allows LLD to work on all versions of Windows.
61cfca06d7SDimitry Andric // However, at Windows 10 1903 it seems that the behavior of
62cfca06d7SDimitry Andric // Windows has changed, so that we could simply delete the output
63cfca06d7SDimitry Andric // file. This code should be simplified once support for older
64cfca06d7SDimitry Andric // versions of Windows is dropped.
65cfca06d7SDimitry Andric //
66cfca06d7SDimitry Andric // Warning: It seems that the WINVER and _WIN32_WINNT preprocessor
67cfca06d7SDimitry Andric // defines affect the behavior of the Windows versions of the calls
68cfca06d7SDimitry Andric // we are using here. If this code stops working this is worth
69cfca06d7SDimitry Andric // bearing in mind.
70cfca06d7SDimitry Andric SmallString<128> tmpName;
71cfca06d7SDimitry Andric if (!sys::fs::createUniqueFile(path + "%%%%%%%%.tmp", tmpName)) {
72cfca06d7SDimitry Andric if (!sys::fs::rename(path, tmpName))
73cfca06d7SDimitry Andric path = tmpName;
74cfca06d7SDimitry Andric else
75cfca06d7SDimitry Andric sys::fs::remove(tmpName);
76cfca06d7SDimitry Andric }
77f1e1c239SDimitry Andric sys::fs::remove(path);
78eb1ff93dSDimitry Andric #else
79cfca06d7SDimitry Andric if (parallel::strategy.ThreadsRequested == 1)
80d2d3ebb8SDimitry Andric return;
81d2d3ebb8SDimitry Andric
82eb1ff93dSDimitry Andric // We cannot just remove path from a different thread because we are now going
83eb1ff93dSDimitry Andric // to create path as a new file.
84eb1ff93dSDimitry Andric // Instead we open the file and unlink it on this thread. The unlink is fast
85eb1ff93dSDimitry Andric // since the open fd guarantees that it is not removing the last reference.
86f1e1c239SDimitry Andric int fd;
87f1e1c239SDimitry Andric std::error_code ec = sys::fs::openFileForRead(path, fd);
88f1e1c239SDimitry Andric sys::fs::remove(path);
89f1e1c239SDimitry Andric
90f1e1c239SDimitry Andric if (ec)
91f1e1c239SDimitry Andric return;
92d2d3ebb8SDimitry Andric
93eb1ff93dSDimitry Andric // close and therefore remove TempPath in background.
94f1e1c239SDimitry Andric std::mutex m;
95f1e1c239SDimitry Andric std::condition_variable cv;
96f1e1c239SDimitry Andric bool started = false;
97f1e1c239SDimitry Andric std::thread([&, fd] {
98f1e1c239SDimitry Andric {
99f1e1c239SDimitry Andric std::lock_guard<std::mutex> l(m);
100f1e1c239SDimitry Andric started = true;
101f1e1c239SDimitry Andric cv.notify_all();
102f1e1c239SDimitry Andric }
103f1e1c239SDimitry Andric ::close(fd);
104f1e1c239SDimitry Andric }).detach();
105f1e1c239SDimitry Andric
106f1e1c239SDimitry Andric // GLIBC 2.26 and earlier have race condition that crashes an entire process
107f1e1c239SDimitry Andric // if the main thread calls exit(2) while other thread is starting up.
108f1e1c239SDimitry Andric std::unique_lock<std::mutex> l(m);
109f1e1c239SDimitry Andric cv.wait(l, [&] { return started; });
110eb1ff93dSDimitry Andric #endif
111d2d3ebb8SDimitry Andric }
112d2d3ebb8SDimitry Andric
113e06a19b8SDimitry Andric // Simulate file creation to see if Path is writable.
114d2d3ebb8SDimitry Andric //
115d2d3ebb8SDimitry Andric // Determining whether a file is writable or not is amazingly hard,
116d2d3ebb8SDimitry Andric // and after all the only reliable way of doing that is to actually
117d2d3ebb8SDimitry Andric // create a file. But we don't want to do that in this function
118d2d3ebb8SDimitry Andric // because LLD shouldn't update any file if it will end in a failure.
119e06a19b8SDimitry Andric // We also don't want to reimplement heuristics to determine if a
120e06a19b8SDimitry Andric // file is writable. So we'll let FileOutputBuffer do the work.
121d2d3ebb8SDimitry Andric //
122706b4fc4SDimitry Andric // FileOutputBuffer doesn't touch a destination file until commit()
123d2d3ebb8SDimitry Andric // is called. We use that class without calling commit() to predict
124d2d3ebb8SDimitry Andric // if the given file is writable.
tryCreateFile(StringRef path)125f1e1c239SDimitry Andric std::error_code lld::tryCreateFile(StringRef path) {
126b1c73532SDimitry Andric llvm::TimeTraceScope timeScope("Try create output file");
127f1e1c239SDimitry Andric if (path.empty())
128e06a19b8SDimitry Andric return std::error_code();
129f1e1c239SDimitry Andric if (path == "-")
130eb1ff93dSDimitry Andric return std::error_code();
131f1e1c239SDimitry Andric return errorToErrorCode(FileOutputBuffer::create(path, 1).takeError());
132d2d3ebb8SDimitry Andric }
133b1c73532SDimitry Andric
134b1c73532SDimitry Andric // Creates an empty file to and returns a raw_fd_ostream to write to it.
openFile(StringRef file)135b1c73532SDimitry Andric std::unique_ptr<raw_fd_ostream> lld::openFile(StringRef file) {
136b1c73532SDimitry Andric std::error_code ec;
137b1c73532SDimitry Andric auto ret =
138b1c73532SDimitry Andric std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
139b1c73532SDimitry Andric if (ec) {
140b1c73532SDimitry Andric error("cannot open " + file + ": " + ec.message());
141b1c73532SDimitry Andric return nullptr;
142b1c73532SDimitry Andric }
143b1c73532SDimitry Andric return ret;
144b1c73532SDimitry Andric }
145b1c73532SDimitry Andric
146b1c73532SDimitry Andric // The merged bitcode after LTO is large. Try opening a file stream that
147b1c73532SDimitry Andric // supports reading, seeking and writing. Such a file allows BitcodeWriter to
148b1c73532SDimitry Andric // flush buffered data to reduce memory consumption. If this fails, open a file
149b1c73532SDimitry Andric // stream that supports only write.
openLTOOutputFile(StringRef file)150b1c73532SDimitry Andric std::unique_ptr<raw_fd_ostream> lld::openLTOOutputFile(StringRef file) {
151b1c73532SDimitry Andric std::error_code ec;
152b1c73532SDimitry Andric std::unique_ptr<raw_fd_ostream> fs =
153b1c73532SDimitry Andric std::make_unique<raw_fd_stream>(file, ec);
154b1c73532SDimitry Andric if (!ec)
155b1c73532SDimitry Andric return fs;
156b1c73532SDimitry Andric return openFile(file);
157b1c73532SDimitry Andric }
158