2021-01-09 11:07:19 +03:00
|
|
|
#include "mold.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2021-10-26 08:06:10 +03:00
|
|
|
namespace mold::elf {
|
2021-10-05 05:35:20 +03:00
|
|
|
|
|
|
|
inline u32 get_umask() {
|
2021-01-09 17:13:38 +03:00
|
|
|
u32 orig_umask = umask(0);
|
|
|
|
umask(orig_umask);
|
|
|
|
return orig_umask;
|
2021-01-09 11:07:19 +03:00
|
|
|
}
|
|
|
|
|
2021-10-26 08:06:10 +03:00
|
|
|
template <typename E>
|
|
|
|
class MemoryMappedOutputFile : public OutputFile<E> {
|
2021-01-09 11:07:19 +03:00
|
|
|
public:
|
2021-10-26 08:06:10 +03:00
|
|
|
MemoryMappedOutputFile(Context<E> &ctx, std::string path, i64 filesize, i64 perm)
|
|
|
|
: OutputFile<E>(path, filesize, true) {
|
2021-06-01 09:19:50 +03:00
|
|
|
std::string dir(path_dirname(path));
|
2021-09-15 09:25:14 +03:00
|
|
|
output_tmpfile = (char *)save_string(ctx, dir + "/.mold-XXXXXX").data();
|
|
|
|
i64 fd = mkstemp(output_tmpfile);
|
2021-01-09 11:07:19 +03:00
|
|
|
if (fd == -1)
|
2021-09-15 09:25:14 +03:00
|
|
|
Fatal(ctx) << "cannot open " << output_tmpfile << ": " << errno_string();
|
2021-01-09 11:07:19 +03:00
|
|
|
|
2021-09-15 09:25:14 +03:00
|
|
|
if (rename(path.c_str(), output_tmpfile) == 0) {
|
2021-03-03 11:19:24 +03:00
|
|
|
::close(fd);
|
2021-09-15 09:25:14 +03:00
|
|
|
fd = ::open(output_tmpfile, O_RDWR | O_CREAT, perm);
|
2021-03-03 11:19:24 +03:00
|
|
|
if (fd == -1) {
|
|
|
|
if (errno != ETXTBSY)
|
2021-09-05 11:14:21 +03:00
|
|
|
Fatal(ctx) << "cannot open " << path << ": " << errno_string();
|
2021-09-15 09:25:14 +03:00
|
|
|
unlink(output_tmpfile);
|
|
|
|
fd = ::open(output_tmpfile, O_RDWR | O_CREAT, perm);
|
2021-03-03 11:19:24 +03:00
|
|
|
if (fd == -1)
|
2021-09-05 11:14:21 +03:00
|
|
|
Fatal(ctx) << "cannot open " << path << ": " << errno_string();
|
2021-03-03 11:19:24 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-09 11:07:19 +03:00
|
|
|
|
2021-03-03 11:19:24 +03:00
|
|
|
if (ftruncate(fd, filesize))
|
2021-03-29 10:48:23 +03:00
|
|
|
Fatal(ctx) << "ftruncate failed";
|
2021-01-09 11:07:19 +03:00
|
|
|
|
2021-06-17 19:45:38 +03:00
|
|
|
if (fchmod(fd, (perm & ~get_umask())) == -1)
|
2021-03-29 10:48:23 +03:00
|
|
|
Fatal(ctx) << "fchmod failed";
|
2021-03-03 11:19:24 +03:00
|
|
|
|
2021-03-29 14:29:57 +03:00
|
|
|
this->buf = (u8 *)mmap(nullptr, filesize, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, 0);
|
|
|
|
if (this->buf == MAP_FAILED)
|
2021-09-05 11:14:21 +03:00
|
|
|
Fatal(ctx) << path << ": mmap failed: " << errno_string();
|
2021-01-09 11:07:19 +03:00
|
|
|
::close(fd);
|
|
|
|
}
|
|
|
|
|
2021-10-26 08:06:10 +03:00
|
|
|
void close(Context<E> &ctx) override {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "close_file");
|
2021-04-28 12:02:32 +03:00
|
|
|
|
2021-10-05 14:02:41 +03:00
|
|
|
if (!this->is_unmapped)
|
2021-04-28 12:02:32 +03:00
|
|
|
munmap(this->buf, this->filesize);
|
|
|
|
|
2021-09-15 09:25:14 +03:00
|
|
|
if (rename(output_tmpfile, this->path.c_str()) == -1)
|
2021-09-05 11:14:21 +03:00
|
|
|
Fatal(ctx) << this->path << ": rename failed: " << errno_string();
|
2021-09-15 09:25:14 +03:00
|
|
|
output_tmpfile = nullptr;
|
2021-01-09 11:07:19 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-26 08:06:10 +03:00
|
|
|
template <typename E>
|
|
|
|
class MallocOutputFile : public OutputFile<E> {
|
2021-01-09 11:07:19 +03:00
|
|
|
public:
|
2021-10-26 08:06:10 +03:00
|
|
|
MallocOutputFile(Context<E> &ctx, std::string path, i64 filesize, i64 perm)
|
|
|
|
: OutputFile<E>(path, filesize, false), perm(perm) {
|
2021-03-29 14:29:57 +03:00
|
|
|
this->buf = (u8 *)mmap(NULL, filesize, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
|
|
|
if (this->buf == MAP_FAILED)
|
2021-09-05 11:14:21 +03:00
|
|
|
Fatal(ctx) << "mmap failed: " << errno_string();
|
2021-01-09 11:07:19 +03:00
|
|
|
}
|
|
|
|
|
2021-10-26 08:06:10 +03:00
|
|
|
void close(Context<E> &ctx) override {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "close_file");
|
2021-04-28 12:02:32 +03:00
|
|
|
|
|
|
|
if (this->path == "-") {
|
|
|
|
fwrite(this->buf, this->filesize, 1, stdout);
|
2021-05-29 09:00:18 +03:00
|
|
|
fclose(stdout);
|
2021-04-28 12:02:32 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-17 19:45:38 +03:00
|
|
|
i64 fd = ::open(this->path.c_str(), O_RDWR | O_CREAT, perm);
|
2021-01-09 11:07:19 +03:00
|
|
|
if (fd == -1)
|
2021-09-05 11:14:21 +03:00
|
|
|
Fatal(ctx) << "cannot open " << this->path << ": " << errno_string();
|
2021-01-09 11:07:19 +03:00
|
|
|
|
|
|
|
FILE *fp = fdopen(fd, "w");
|
2021-03-29 14:29:57 +03:00
|
|
|
fwrite(this->buf, this->filesize, 1, fp);
|
2021-01-09 11:07:19 +03:00
|
|
|
fclose(fp);
|
|
|
|
}
|
2021-06-17 19:45:38 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
i64 perm;
|
2021-01-09 11:07:19 +03:00
|
|
|
};
|
|
|
|
|
2021-10-26 08:06:10 +03:00
|
|
|
template <typename E>
|
|
|
|
std::unique_ptr<OutputFile<E>>
|
|
|
|
OutputFile<E>::open(Context<E> &ctx, std::string path, i64 filesize, i64 perm) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "open_file");
|
2021-01-09 11:07:19 +03:00
|
|
|
|
2021-04-09 10:05:52 +03:00
|
|
|
if (path.starts_with('/') && !ctx.arg.chroot.empty())
|
|
|
|
path = ctx.arg.chroot + "/" + path_clean(path);
|
|
|
|
|
2021-03-03 11:19:24 +03:00
|
|
|
bool is_special = false;
|
2021-04-28 12:02:32 +03:00
|
|
|
if (path == "-") {
|
2021-03-03 11:19:24 +03:00
|
|
|
is_special = true;
|
2021-04-28 12:02:32 +03:00
|
|
|
} else {
|
|
|
|
struct stat st;
|
|
|
|
if (stat(path.c_str(), &st) == 0 && (st.st_mode & S_IFMT) != S_IFREG)
|
|
|
|
is_special = true;
|
|
|
|
}
|
2021-03-03 11:19:24 +03:00
|
|
|
|
2021-10-26 08:06:10 +03:00
|
|
|
std::unique_ptr<OutputFile<E>> file;
|
2021-03-03 11:19:24 +03:00
|
|
|
if (is_special)
|
2021-10-26 08:06:10 +03:00
|
|
|
file = std::make_unique<MallocOutputFile<E>>(ctx, path, filesize, perm);
|
2021-03-03 11:19:24 +03:00
|
|
|
else
|
2021-10-26 08:06:10 +03:00
|
|
|
file = std::make_unique<MemoryMappedOutputFile<E>>(ctx, path, filesize, perm);
|
2021-01-09 11:07:19 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
if (ctx.arg.filler != -1)
|
|
|
|
memset(file->buf, ctx.arg.filler, filesize);
|
2021-01-09 11:07:19 +03:00
|
|
|
return file;
|
|
|
|
}
|
2021-03-29 14:29:57 +03:00
|
|
|
|
2021-10-26 08:06:10 +03:00
|
|
|
#define INSTANTIATE(E) \
|
|
|
|
template class OutputFile<E>;
|
|
|
|
|
|
|
|
INSTANTIATE(X86_64);
|
|
|
|
INSTANTIATE(I386);
|
2021-12-11 15:33:39 +03:00
|
|
|
INSTANTIATE(ARM64);
|
2021-10-26 08:06:10 +03:00
|
|
|
|
|
|
|
} // namespace mold::elf
|