From e056d880e7faa1fba41d9342cf71c9dbcf3e128e Mon Sep 17 00:00:00 2001 From: Karuboniru Date: Mon, 21 Oct 2024 21:39:05 +0800 Subject: [PATCH] increase file limit when hit the ulimit --- lib/mapped-file-unix.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/mapped-file-unix.cc b/lib/mapped-file-unix.cc index 918e6c5dc3..121a4f2fac 100644 --- a/lib/mapped-file-unix.cc +++ b/lib/mapped-file-unix.cc @@ -1,9 +1,21 @@ #include "common.h" +// for getrlimit, setrlimit +#include + namespace mold { MappedFile *open_file_impl(const std::string &path, std::string &error) { i64 fd = ::open(path.c_str(), O_RDONLY); + // increase rlimit when EMFILE. This is required for llvm lto, since llvm + // plugin requires keeping input files open + if (fd == -1 && errno == EMFILE) { + if (struct rlimit rlim{}; getrlimit(RLIMIT_NOFILE, &rlim) == 0) { + rlim.rlim_cur = rlim.rlim_max; + setrlimit(RLIMIT_NOFILE, &rlim); + fd = ::open(path.c_str(), O_RDONLY); + } + } if (fd == -1) { if (errno != ENOENT) error = "opening " + path + " failed: " + errno_string();