Skip to content

Commit

Permalink
Support lto on mingw
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 committed Jun 21, 2024
1 parent bf05198 commit 50bf031
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ endif()

add_executable(mold)
target_compile_features(mold PRIVATE cxx_std_20)
target_link_libraries(mold PRIVATE ${CMAKE_DL_LIBS})
if(MINGW)
target_link_libraries(mold PRIVATE dl)
else()
target_link_libraries(mold PRIVATE ${CMAKE_DL_LIBS})
endif()

if(NOT "${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "MSVC")
target_compile_options(mold PRIVATE
Expand Down Expand Up @@ -328,7 +332,7 @@ list(APPEND MOLD_ELF_TEMPLATE_FILES
elf/tls.cc
)

if(WIN32)
if(WIN32 AND NOT(MINGW))
list(APPEND MOLD_ELF_TEMPLATE_FILES elf/lto-win32.cc)
else()
list(APPEND MOLD_ELF_TEMPLATE_FILES elf/lto-unix.cc)
Expand Down
26 changes: 26 additions & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,32 @@ class MappedFile {
return name;
}

void close_fd() {
#ifdef WIN32
if (fd != INVALID_HANDLE_VALUE) {
CloseHandle(fd);
fd = INVALID_HANDLE_VALUE;
}
#else
if (fd != -1) {
close(fd);
fd = -1;
}
#endif
}

void reopen_fd(const char *path) {
#ifdef WIN32
if (fd != INVALID_HANDLE_VALUE)
fd = CreateFileA(path, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
#else
if (fd == -1)
fd = open(path, O_RDONLY);
#endif
}

std::string name;
u8 *data = nullptr;
i64 size = 0;
Expand Down
18 changes: 9 additions & 9 deletions elf/lto-unix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,7 @@ static PluginStatus release_input_file(const void *handle) {
LOG << "release_input_file\n";

ObjectFile<E> &file = *(ObjectFile<E> *)handle;
if (file.mf->fd != -1) {
close(file.mf->fd);
file.mf->fd = -1;
}
file.mf->close_fd();
return LDPS_OK;
}

Expand Down Expand Up @@ -570,7 +567,11 @@ static ElfSym<E> to_elf_sym(PluginSymbol &psym) {
// Returns false if it's GCC.
template <typename E>
static bool is_llvm(Context<E> &ctx) {
#ifdef __MINGW32__
return ctx.arg.plugin.ends_with("LLVMgold.dll");
#else
return ctx.arg.plugin.ends_with("LLVMgold.so");
#endif
}

// Returns true if a given linker plugin supports the get_symbols_v3 API.
Expand All @@ -590,11 +591,11 @@ create_plugin_input_file(Context<E> &ctx, MappedFile *mf) {
file.offset = mf->get_offset();
file.filesize = mf->size;

if (mf2->fd == -1)
mf2->fd = open(file.name, O_RDONLY);
mf2->reopen_fd(file.name);

file.fd = mf2->fd;

if (file.fd == -1)
if (!file.fd)
Fatal(ctx) << "cannot open " << file.name << ": " << errno_string();
return file;
}
Expand Down Expand Up @@ -645,8 +646,7 @@ ObjectFile<E> *read_lto_object(Context<E> &ctx, MappedFile *mf) {
// open files" issue, we close fd only for GCC. This is ugly, though.
if (!is_llvm(ctx)) {
MappedFile *mf2 = mf->parent ? mf->parent : mf;
close(mf2->fd);
mf2->fd = -1;
mf2->close_fd();
}

// Create a symbol strtab
Expand Down
6 changes: 5 additions & 1 deletion elf/lto.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ enum PluginOutputFileType {

struct PluginInputFile {
const char *name;
i32 fd;
#if __MINGW32__
HANDLE fd;
#else
int fd;
#endif
u64 offset;
u64 filesize;
void *handle;
Expand Down

0 comments on commit 50bf031

Please sign in to comment.