2024-09-07 09:02:43 +03:00
|
|
|
#include "mold.h"
|
2022-08-13 10:55:47 +03:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <filesystem>
|
2024-07-09 11:21:32 +03:00
|
|
|
#include <sys/file.h>
|
2022-08-13 10:55:47 +03:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
namespace mold {
|
|
|
|
|
2024-09-07 09:02:43 +03:00
|
|
|
static u32 get_umask() {
|
2022-08-13 10:55:47 +03:00
|
|
|
u32 orig_umask = umask(0);
|
|
|
|
umask(orig_umask);
|
|
|
|
return orig_umask;
|
|
|
|
}
|
|
|
|
|
2024-09-07 09:02:43 +03:00
|
|
|
template <typename E>
|
2024-07-09 07:00:05 +03:00
|
|
|
static int
|
2024-09-07 09:02:43 +03:00
|
|
|
open_or_create_file(Context<E> &ctx, std::string path, std::string tmpfile,
|
2024-07-09 07:00:05 +03:00
|
|
|
int perm) {
|
2022-08-13 10:55:47 +03:00
|
|
|
// Reuse an existing file if exists and writable because on Linux,
|
|
|
|
// writing to an existing file is much faster than creating a fresh
|
|
|
|
// file and writing to it.
|
2024-07-09 07:00:05 +03:00
|
|
|
if (ctx.overwrite_output_file && rename(path.c_str(), tmpfile.c_str()) == 0) {
|
|
|
|
i64 fd = ::open(tmpfile.c_str(), O_RDWR | O_CREAT, perm);
|
|
|
|
if (fd != -1)
|
|
|
|
return fd;
|
|
|
|
unlink(tmpfile.c_str());
|
2022-08-13 10:55:47 +03:00
|
|
|
}
|
|
|
|
|
2024-07-09 07:00:05 +03:00
|
|
|
i64 fd = ::open(tmpfile.c_str(), O_RDWR | O_CREAT, perm);
|
|
|
|
if (fd == -1)
|
|
|
|
Fatal(ctx) << "cannot open " << tmpfile << ": " << errno_string();
|
|
|
|
return fd;
|
2022-08-13 10:55:47 +03:00
|
|
|
}
|
|
|
|
|
2024-09-07 09:02:43 +03:00
|
|
|
template <typename E>
|
|
|
|
class MemoryMappedOutputFile : public OutputFile<E> {
|
2022-08-13 10:55:47 +03:00
|
|
|
public:
|
2024-09-07 09:02:43 +03:00
|
|
|
MemoryMappedOutputFile(Context<E> &ctx, std::string path, i64 filesize, int perm)
|
|
|
|
: OutputFile<E>(path, filesize, true) {
|
2024-07-09 07:00:05 +03:00
|
|
|
std::filesystem::path dir = filepath(path).parent_path();
|
|
|
|
std::string filename = filepath(path).filename().string();
|
|
|
|
std::string tmpfile = dir / ("." + filename + "." + std::to_string(getpid()));
|
|
|
|
|
|
|
|
this->fd = open_or_create_file(ctx, path, tmpfile, perm);
|
|
|
|
|
|
|
|
if (fchmod(this->fd, perm & ~get_umask()) == -1)
|
|
|
|
Fatal(ctx) << "fchmod failed: " << errno_string();
|
|
|
|
|
|
|
|
if (ftruncate(this->fd, filesize) == -1)
|
|
|
|
Fatal(ctx) << "ftruncate failed: " << errno_string();
|
|
|
|
|
|
|
|
output_tmpfile = (char *)save_string(ctx, tmpfile).data();
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
fallocate(this->fd, 0, 0, filesize);
|
|
|
|
#endif
|
2022-08-13 10:55:47 +03:00
|
|
|
|
|
|
|
this->buf = (u8 *)mmap(nullptr, filesize, PROT_READ | PROT_WRITE,
|
2023-10-10 13:02:27 +03:00
|
|
|
MAP_SHARED, this->fd, 0);
|
2022-08-13 10:55:47 +03:00
|
|
|
if (this->buf == MAP_FAILED)
|
|
|
|
Fatal(ctx) << path << ": mmap failed: " << errno_string();
|
|
|
|
|
|
|
|
mold::output_buffer_start = this->buf;
|
|
|
|
mold::output_buffer_end = this->buf + filesize;
|
|
|
|
}
|
|
|
|
|
2022-12-17 07:10:33 +03:00
|
|
|
~MemoryMappedOutputFile() {
|
|
|
|
if (fd2 != -1)
|
|
|
|
::close(fd2);
|
|
|
|
}
|
|
|
|
|
2024-09-07 09:02:43 +03:00
|
|
|
void close(Context<E> &ctx) override {
|
2022-08-13 10:55:47 +03:00
|
|
|
Timer t(ctx, "close_file");
|
|
|
|
|
|
|
|
if (!this->is_unmapped)
|
|
|
|
munmap(this->buf, this->filesize);
|
|
|
|
|
2023-10-11 07:31:10 +03:00
|
|
|
if (this->buf2.empty()) {
|
|
|
|
::close(this->fd);
|
|
|
|
} else {
|
|
|
|
FILE *out = fdopen(this->fd, "w");
|
|
|
|
fseek(out, 0, SEEK_END);
|
|
|
|
fwrite(&this->buf2[0], this->buf2.size(), 1, out);
|
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
|
2022-12-17 07:10:33 +03:00
|
|
|
// If an output file already exists, open a file and then remove it.
|
|
|
|
// This is the fastest way to unlink a file, as it does not make the
|
|
|
|
// system to immediately release disk blocks occupied by the file.
|
|
|
|
fd2 = ::open(this->path.c_str(), O_RDONLY);
|
|
|
|
if (fd2 != -1)
|
|
|
|
unlink(this->path.c_str());
|
|
|
|
|
2022-08-13 10:55:47 +03:00
|
|
|
if (rename(output_tmpfile, this->path.c_str()) == -1)
|
|
|
|
Fatal(ctx) << this->path << ": rename failed: " << errno_string();
|
|
|
|
output_tmpfile = nullptr;
|
|
|
|
}
|
2022-12-17 07:10:33 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
int fd2 = -1;
|
2022-08-13 10:55:47 +03:00
|
|
|
};
|
|
|
|
|
2024-09-07 09:02:43 +03:00
|
|
|
template <typename E>
|
|
|
|
std::unique_ptr<OutputFile<E>>
|
|
|
|
OutputFile<E>::open(Context<E> &ctx, std::string path, i64 filesize, int perm) {
|
2022-08-13 10:55:47 +03:00
|
|
|
Timer t(ctx, "open_file");
|
|
|
|
|
|
|
|
if (path.starts_with('/') && !ctx.arg.chroot.empty())
|
|
|
|
path = ctx.arg.chroot + "/" + path_clean(path);
|
|
|
|
|
|
|
|
bool is_special = false;
|
|
|
|
if (path == "-") {
|
|
|
|
is_special = true;
|
|
|
|
} else {
|
|
|
|
struct stat st;
|
|
|
|
if (stat(path.c_str(), &st) == 0 && (st.st_mode & S_IFMT) != S_IFREG)
|
|
|
|
is_special = true;
|
|
|
|
}
|
|
|
|
|
2024-09-07 09:02:43 +03:00
|
|
|
OutputFile<E> *file;
|
2022-08-13 10:55:47 +03:00
|
|
|
if (is_special)
|
2023-01-07 11:35:15 +03:00
|
|
|
file = new MallocOutputFile(ctx, path, filesize, perm);
|
2022-08-13 10:55:47 +03:00
|
|
|
else
|
2023-01-07 11:35:15 +03:00
|
|
|
file = new MemoryMappedOutputFile(ctx, path, filesize, perm);
|
2022-08-13 10:55:47 +03:00
|
|
|
|
2022-10-23 13:13:46 +03:00
|
|
|
#ifdef MADV_HUGEPAGE
|
|
|
|
// Enable transparent huge page for an output memory-mapped file.
|
|
|
|
// On Linux, it has an effect only on tmpfs mounted with `huge=advise`,
|
|
|
|
// but it can make the linker ~10% faster. You can try it by creating
|
|
|
|
// a tmpfs with the following commands
|
|
|
|
//
|
|
|
|
// $ mkdir tmp
|
|
|
|
// $ sudo mount -t tmpfs -o size=2G,huge=advise none tmp
|
|
|
|
//
|
|
|
|
// and then specifying a path under the directory as an output file.
|
|
|
|
madvise(file->buf, filesize, MADV_HUGEPAGE);
|
|
|
|
#endif
|
|
|
|
|
2022-08-13 10:55:47 +03:00
|
|
|
if (ctx.arg.filler != -1)
|
|
|
|
memset(file->buf, ctx.arg.filler, filesize);
|
2023-01-07 11:35:15 +03:00
|
|
|
return std::unique_ptr<OutputFile>(file);
|
2022-08-13 10:55:47 +03:00
|
|
|
}
|
|
|
|
|
2024-07-09 11:21:32 +03:00
|
|
|
// LockingOutputFile is similar to MemoryMappedOutputFile, but it doesn't
|
|
|
|
// rename output files and instead acquires file lock using flock().
|
2024-09-07 09:02:43 +03:00
|
|
|
template <typename E>
|
|
|
|
LockingOutputFile<E>::LockingOutputFile(Context<E> &ctx, std::string path,
|
|
|
|
int perm)
|
|
|
|
: OutputFile<E>(path, 0, true) {
|
2024-07-09 11:21:32 +03:00
|
|
|
this->fd = ::open(path.c_str(), O_RDWR | O_CREAT, perm);
|
|
|
|
if (this->fd == -1)
|
|
|
|
Fatal(ctx) << "cannot open " << path << ": " << errno_string();
|
|
|
|
flock(this->fd, LOCK_EX);
|
|
|
|
|
|
|
|
// We may be overwriting to an existing debug info file. We want to
|
|
|
|
// make the file unusable so that gdb won't use it by accident until
|
|
|
|
// it's ready.
|
|
|
|
u8 buf[256] = {};
|
|
|
|
(void)!!write(this->fd, buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
2024-09-07 09:02:43 +03:00
|
|
|
template <typename E>
|
|
|
|
void LockingOutputFile<E>::resize(Context<E> &ctx, i64 filesize) {
|
2024-07-09 11:21:32 +03:00
|
|
|
if (ftruncate(this->fd, filesize) == -1)
|
|
|
|
Fatal(ctx) << "ftruncate failed: " << errno_string();
|
|
|
|
|
|
|
|
this->buf = (u8 *)mmap(nullptr, filesize, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, this->fd, 0);
|
|
|
|
if (this->buf == MAP_FAILED)
|
|
|
|
Fatal(ctx) << this->path << ": mmap failed: " << errno_string();
|
|
|
|
|
|
|
|
this->filesize = filesize;
|
|
|
|
mold::output_buffer_start = this->buf;
|
|
|
|
mold::output_buffer_end = this->buf + filesize;
|
|
|
|
}
|
|
|
|
|
2024-09-07 09:02:43 +03:00
|
|
|
template <typename E>
|
|
|
|
void LockingOutputFile<E>::close(Context<E> &ctx) {
|
2024-07-09 11:21:32 +03:00
|
|
|
if (!this->is_unmapped)
|
|
|
|
munmap(this->buf, this->filesize);
|
|
|
|
|
|
|
|
if (!this->buf2.empty()) {
|
|
|
|
FILE *out = fdopen(this->fd, "w");
|
|
|
|
fseek(out, 0, SEEK_END);
|
|
|
|
fwrite(&this->buf2[0], this->buf2.size(), 1, out);
|
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
::close(this->fd);
|
|
|
|
}
|
|
|
|
|
2024-09-07 09:02:43 +03:00
|
|
|
using E = MOLD_TARGET;
|
|
|
|
|
|
|
|
template class OutputFile<E>;
|
|
|
|
template class LockingOutputFile<E>;
|
|
|
|
|
2022-08-13 10:55:47 +03:00
|
|
|
} // namespace mold
|