2021-01-09 11:07:19 +03:00
|
|
|
#include "mold.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
static 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-03-29 14:29:57 +03:00
|
|
|
template <typename E>
|
|
|
|
class MemoryMappedOutputFile : public OutputFile<E> {
|
2021-01-09 11:07:19 +03:00
|
|
|
public:
|
2021-03-29 14:29:57 +03:00
|
|
|
MemoryMappedOutputFile(Context<E> &ctx, std::string path, i64 filesize)
|
|
|
|
: OutputFile<E>(path, filesize) {
|
2021-03-29 07:20:51 +03:00
|
|
|
std::string dir = dirname(strdup(ctx.arg.output.c_str()));
|
2021-03-29 14:29:57 +03:00
|
|
|
this->tmpfile = strdup((dir + "/.mold-XXXXXX").c_str());
|
|
|
|
i64 fd = mkstemp(this->tmpfile);
|
2021-01-09 11:07:19 +03:00
|
|
|
if (fd == -1)
|
2021-03-29 14:29:57 +03:00
|
|
|
Fatal(ctx) << "cannot open " << this->tmpfile << ": " << strerror(errno);
|
2021-01-09 11:07:19 +03:00
|
|
|
|
2021-03-29 14:29:57 +03:00
|
|
|
if (rename(ctx.arg.output.c_str(), this->tmpfile) == 0) {
|
2021-03-03 11:19:24 +03:00
|
|
|
::close(fd);
|
2021-03-29 14:29:57 +03:00
|
|
|
fd = ::open(this->tmpfile, O_RDWR | O_CREAT, 0777);
|
2021-03-03 11:19:24 +03:00
|
|
|
if (fd == -1) {
|
|
|
|
if (errno != ETXTBSY)
|
2021-03-29 14:29:57 +03:00
|
|
|
Fatal(ctx) << "cannot open " << ctx.arg.output << ": "
|
|
|
|
<< strerror(errno);
|
|
|
|
unlink(this->tmpfile);
|
|
|
|
fd = ::open(this->tmpfile, O_RDWR | O_CREAT, 0777);
|
2021-03-03 11:19:24 +03:00
|
|
|
if (fd == -1)
|
2021-03-29 14:29:57 +03:00
|
|
|
Fatal(ctx) << "cannot open " << ctx.arg.output << ": "
|
|
|
|
<< strerror(errno);
|
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-03-03 11:19:24 +03:00
|
|
|
if (fchmod(fd, (0777 & ~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-03-29 10:48:23 +03:00
|
|
|
Fatal(ctx) << ctx.arg.output << ": mmap failed: " << strerror(errno);
|
2021-01-09 11:07:19 +03:00
|
|
|
::close(fd);
|
|
|
|
}
|
|
|
|
|
2021-03-29 14:29:57 +03:00
|
|
|
void close(Context<E> &ctx) override {
|
2021-03-07 16:28:49 +03:00
|
|
|
Timer t("close_file");
|
2021-03-29 14:29:57 +03:00
|
|
|
munmap(this->buf, this->filesize);
|
|
|
|
if (rename(this->tmpfile, ctx.arg.output.c_str()) == -1)
|
2021-03-29 10:48:23 +03:00
|
|
|
Fatal(ctx) << ctx.arg.output << ": rename filed: " << strerror(errno);
|
2021-03-29 14:29:57 +03:00
|
|
|
this->tmpfile = nullptr;
|
2021-01-09 11:07:19 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-29 14:29:57 +03:00
|
|
|
template <typename E>
|
|
|
|
class MallocOutputFile : public OutputFile<E> {
|
2021-01-09 11:07:19 +03:00
|
|
|
public:
|
2021-03-29 14:29:57 +03:00
|
|
|
MallocOutputFile(Context<E> &ctx, std::string path, u64 filesize)
|
|
|
|
: OutputFile<E>(path, filesize) {
|
|
|
|
this->buf = (u8 *)mmap(NULL, filesize, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
|
|
|
if (this->buf == MAP_FAILED)
|
2021-03-29 10:48:23 +03:00
|
|
|
Fatal(ctx) << "mmap failed: " << strerror(errno);
|
2021-01-09 11:07:19 +03:00
|
|
|
}
|
|
|
|
|
2021-03-29 14:29:57 +03:00
|
|
|
void close(Context<E> &ctx) override {
|
2021-03-07 16:28:49 +03:00
|
|
|
Timer t("close_file");
|
2021-03-29 14:29:57 +03:00
|
|
|
i64 fd = ::open(this->path.c_str(), O_RDWR | O_CREAT, 0777);
|
2021-01-09 11:07:19 +03:00
|
|
|
if (fd == -1)
|
2021-03-29 10:48:23 +03:00
|
|
|
Fatal(ctx) << "cannot open " << ctx.arg.output << ": " << strerror(errno);
|
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-03-29 14:29:57 +03:00
|
|
|
template <typename E>
|
|
|
|
OutputFile<E> *
|
|
|
|
OutputFile<E>::open(Context<E> &ctx, std::string path, u64 filesize) {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("open_file");
|
2021-01-09 11:07:19 +03:00
|
|
|
|
2021-03-03 11:19:24 +03:00
|
|
|
bool is_special = false;
|
|
|
|
struct stat st;
|
|
|
|
if (stat(path.c_str(), &st) == 0 && (st.st_mode & S_IFMT) != S_IFREG)
|
|
|
|
is_special = true;
|
|
|
|
|
2021-03-29 14:29:57 +03:00
|
|
|
OutputFile<E> *file;
|
2021-03-03 11:19:24 +03:00
|
|
|
if (is_special)
|
2021-03-29 14:29:57 +03:00
|
|
|
file = new MallocOutputFile<E>(ctx, path, filesize);
|
2021-03-03 11:19:24 +03:00
|
|
|
else
|
2021-03-29 14:29:57 +03:00
|
|
|
file = new MemoryMappedOutputFile<E>(ctx, path, filesize);
|
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
|
|
|
|
|
|
|
template
|
2021-03-30 08:57:38 +03:00
|
|
|
OutputFile<X86_64> *
|
|
|
|
OutputFile<X86_64>::open(Context<X86_64> &ctx, std::string path, u64 filesize);
|
2021-03-30 13:27:00 +03:00
|
|
|
|
|
|
|
template
|
|
|
|
OutputFile<I386> *
|
|
|
|
OutputFile<I386>::open(Context<I386> &ctx, std::string path, u64 filesize);
|