mirror of
https://github.com/rui314/mold.git
synced 2024-09-17 16:09:43 +03:00
Fix build errors for Windows
With this change, we can compile mold on Windows using Visual Studio
This commit is contained in:
parent
c434c2ce94
commit
94d6edb7eb
@ -222,12 +222,14 @@ include(CTest)
|
||||
|
||||
if(BUILD_TESTING)
|
||||
# Create the ld and ld64 symlinks required for testing
|
||||
add_custom_command(
|
||||
TARGET mold POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E create_symlink mold ld
|
||||
COMMAND ${CMAKE_COMMAND} -E create_symlink mold ld64
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||
VERBATIM)
|
||||
if(NOT WIN32)
|
||||
add_custom_command(
|
||||
TARGET mold POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E create_symlink mold ld
|
||||
COMMAND ${CMAKE_COMMAND} -E create_symlink mold ld64
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||
VERBATIM)
|
||||
endif()
|
||||
|
||||
if(${APPLE})
|
||||
add_subdirectory(test/macho)
|
||||
|
@ -1981,13 +1981,15 @@ static void compute_sha256(Context<E> &ctx, i64 offset) {
|
||||
u8 *end = (i == num_shards - 1) ? buf + filesize : begin + shard_size;
|
||||
sha256_hash(begin, end - begin, shards.data() + i * SHA256_SIZE);
|
||||
|
||||
#ifndef _WIN32
|
||||
// We call munmap early for each chunk so that the last munmap
|
||||
// gets cheaper. We assume that the .note.build-id section is
|
||||
// at the beginning of an output file. This is an ugly performance
|
||||
// hack, but we can save about 30 ms for a 2 GiB output.
|
||||
if (i > 0 && ctx.output_file->is_mmapped)
|
||||
munmap(begin, end - begin);
|
||||
});
|
||||
#endif
|
||||
});
|
||||
|
||||
assert(ctx.arg.build_id.size() <= SHA256_SIZE);
|
||||
|
||||
@ -1995,10 +1997,12 @@ static void compute_sha256(Context<E> &ctx, i64 offset) {
|
||||
sha256_hash(shards.data(), shards.size(), digest);
|
||||
memcpy(buf + offset, digest, ctx.arg.build_id.size());
|
||||
|
||||
#ifndef _WIN32
|
||||
if (ctx.output_file->is_mmapped) {
|
||||
munmap(buf, std::min(filesize, shard_size));
|
||||
ctx.output_file->is_unmapped = true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
|
@ -2,81 +2,18 @@
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <filesystem>
|
||||
#include <windows.h>
|
||||
|
||||
namespace mold {
|
||||
|
||||
template <typename C>
|
||||
static std::pair<i64, char *>
|
||||
open_or_create_file(C &ctx, std::string path, i64 filesize, i64 perm) {
|
||||
std::string tmpl = filepath(path).parent_path() / ".mold-XXXXXX";
|
||||
char *path2 = (char *)save_string(ctx, tmpl).data();
|
||||
|
||||
i64 fd = mkstemp(path2);
|
||||
if (fd == -1)
|
||||
Fatal(ctx) << "cannot open " << path2 << ": " << errno_string();
|
||||
|
||||
// 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.
|
||||
if (ctx.overwrite_output_file && rename(path.c_str(), path2) == 0) {
|
||||
::close(fd);
|
||||
fd = ::open(path2, O_RDWR | O_CREAT, perm);
|
||||
if (fd != -1 && !ftruncate(fd, filesize) && !fchmod(fd, perm & ~get_umask()))
|
||||
return {fd, path2};
|
||||
|
||||
unlink(path2);
|
||||
fd = ::open(path2, O_RDWR | O_CREAT, perm);
|
||||
if (fd == -1)
|
||||
Fatal(ctx) << "cannot open " << path2 << ": " << errno_string();
|
||||
}
|
||||
|
||||
if (ftruncate(fd, filesize))
|
||||
Fatal(ctx) << "ftruncate failed: " << errno_string();
|
||||
|
||||
if (fchmod(fd, (perm & ~get_umask())) == -1)
|
||||
Fatal(ctx) << "fchmod failed: " << errno_string();
|
||||
return {fd, path2};
|
||||
}
|
||||
|
||||
template <typename C>
|
||||
class MemoryMappedOutputFile : public OutputFile<C> {
|
||||
public:
|
||||
MemoryMappedOutputFile(C &ctx, std::string path, i64 filesize, i64 perm)
|
||||
: OutputFile<C>(path, filesize, true) {
|
||||
i64 fd;
|
||||
std::tie(fd, output_tmpfile) = open_or_create_file(ctx, path, filesize, perm);
|
||||
|
||||
this->buf = (u8 *)mmap(nullptr, filesize, PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED, fd, 0);
|
||||
if (this->buf == MAP_FAILED)
|
||||
Fatal(ctx) << path << ": mmap failed: " << errno_string();
|
||||
::close(fd);
|
||||
|
||||
mold::output_buffer_start = this->buf;
|
||||
mold::output_buffer_end = this->buf + filesize;
|
||||
}
|
||||
|
||||
void close(C &ctx) override {
|
||||
Timer t(ctx, "close_file");
|
||||
|
||||
if (!this->is_unmapped)
|
||||
munmap(this->buf, this->filesize);
|
||||
|
||||
if (rename(output_tmpfile, this->path.c_str()) == -1)
|
||||
Fatal(ctx) << this->path << ": rename failed: " << errno_string();
|
||||
output_tmpfile = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename C>
|
||||
class MallocOutputFile : public OutputFile<C> {
|
||||
public:
|
||||
MallocOutputFile(C &ctx, std::string path, i64 filesize, i64 perm)
|
||||
: OutputFile<C>(path, filesize, false), perm(perm) {
|
||||
this->buf = (u8 *)mmap(NULL, filesize, PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||
if (this->buf == MAP_FAILED)
|
||||
Fatal(ctx) << "mmap failed: " << errno_string();
|
||||
this->buf = (u8 *)malloc(filesize);
|
||||
if (!this->buf)
|
||||
Fatal(ctx) << "malloc failed";
|
||||
}
|
||||
|
||||
void close(C &ctx) override {
|
||||
@ -95,6 +32,7 @@ public:
|
||||
FILE *fp = fdopen(fd, "w");
|
||||
fwrite(this->buf, this->filesize, 1, fp);
|
||||
fclose(fp);
|
||||
free(this->buf);
|
||||
}
|
||||
|
||||
private:
|
||||
@ -109,20 +47,7 @@ OutputFile<C>::open(C &ctx, std::string path, i64 filesize, i64 perm) {
|
||||
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;
|
||||
}
|
||||
|
||||
OutputFile<C> *file;
|
||||
if (is_special)
|
||||
file = new MallocOutputFile<C>(ctx, path, filesize, perm);
|
||||
else
|
||||
file = new MemoryMappedOutputFile<C>(ctx, path, filesize, perm);
|
||||
OutputFile<C> *file = new MallocOutputFile<C>(ctx, path, filesize, perm);
|
||||
|
||||
if (ctx.arg.filler != -1)
|
||||
memset(file->buf, ctx.arg.filler, filesize);
|
||||
|
Loading…
Reference in New Issue
Block a user