From c505cfebff131ab9ee299394a9d44f729f6d69d6 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Mon, 9 Nov 2020 12:30:13 +0900 Subject: [PATCH] temporary --- main.cc | 30 +++++++++++++++++------------- mold.h | 4 ++++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/main.cc b/main.cc index fe6fbe21..11209d4f 100644 --- a/main.cc +++ b/main.cc @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -649,15 +650,20 @@ static void unlink_async(tbb::task_group &tg, StringRef path) { tg.run([=]() { close(fd); }); } -static FileOutputBuffer *open_output_file(u64 filesize) { - Expected> buf_or_err = - FileOutputBuffer::create(config.output, filesize, FileOutputBuffer::F_executable); +static u8 *open_output_file(u64 filesize) { + int fd = open(config.output.str().c_str(), O_RDWR | O_CREAT | O_TRUNC, 0777); + if (fd == -1) + error("cannot open " + config.output + ": " + strerror(errno)); - if (!buf_or_err) - error("failed to open " + config.output + ": " + - llvm::toString(buf_or_err.takeError())); + fallocate(fd, 0, 0, filesize); - return std::move(*buf_or_err).release(); + void *addr = mmap(nullptr, filesize, PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); + if (addr == MAP_FAILED) + error(config.output + ": mmap failed: " + strerror(errno)); + close(fd); + + return (u8 *)addr; } static void write_symtab(u8 *buf, std::vector files) { @@ -931,15 +937,13 @@ int main(int argc, char **argv) { } // Create an output file - FileOutputBuffer *output_buffer; + u8 *buf = nullptr; { MyTimer t("open_output_file"); - output_buffer = open_output_file(filesize); + buf = open_output_file(filesize); } - u8 *buf = output_buffer->getBufferStart(); - // Fill .symtab and .strtab { MyTimer t("write_symtab", copy); @@ -968,8 +972,8 @@ int main(int argc, char **argv) { { MyTimer t("commit", copy); - if (auto e = output_buffer->commit()) - error("failed to write to the output file: " + toString(std::move(e))); + if (munmap(buf, filesize) == -1) + error("munmap failed"); } total_timer.stopTimer(); diff --git a/mold.h b/mold.h index 5eca956c..9c041f05 100644 --- a/mold.h +++ b/mold.h @@ -1,5 +1,9 @@ #pragma once +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Twine.h"