1
1
mirror of https://github.com/rui314/mold.git synced 2024-10-05 00:57:08 +03:00
mold/main.cc

89 lines
2.2 KiB
C++
Raw Normal View History

2022-05-08 14:56:36 +03:00
#include "mold.h"
2021-09-08 13:02:38 +03:00
#include <cstring>
#include <signal.h>
2022-05-28 09:03:06 +03:00
#include <tbb/global_control.h>
2022-02-17 14:29:28 +03:00
#ifdef USE_SYSTEM_MIMALLOC
#include <mimalloc-new-delete.h>
#endif
namespace mold {
2021-09-28 10:40:05 +03:00
#ifdef GIT_HASH
2021-12-20 05:22:55 +03:00
const std::string mold_version =
"mold " MOLD_VERSION " (" GIT_HASH "; compatible with GNU ld)";
2021-09-28 10:40:05 +03:00
#else
2021-12-20 05:22:55 +03:00
const std::string mold_version =
"mold " MOLD_VERSION " (compatible with GNU ld)";
2021-09-28 10:40:05 +03:00
#endif
2022-05-08 14:56:36 +03:00
namespace elf {
int main(int argc, char **argv);
}
namespace macho {
int main(int argc, char **argv);
}
void cleanup() {
if (output_tmpfile)
unlink(output_tmpfile);
if (socket_tmpfile)
unlink(socket_tmpfile);
}
2022-05-06 13:23:22 +03:00
// mold mmap's an output file, and the mmap succeeds even if there's
// no enough space left on the filesystem. The actual disk blocks are
// not allocated on the mmap call but when the program writes to it
// for the first time.
//
// If a disk becomes full as a result of a write to an mmap'ed memory
// region, the failure of the write is reported as a SIGBUS. This
// signal handler catches that signal and print out a user-friendly
// error message. Without this, it is very hard to realize that the
// disk might be full.
static void sighandler(int signo, siginfo_t *info, void *ucontext) {
static std::mutex mu;
std::scoped_lock lock{mu};
if ((signo == SIGSEGV || signo == SIGBUS) &&
output_buffer_start <= info->si_addr &&
info->si_addr < output_buffer_end) {
const char msg[] = "mold: failed to write to an output file. Disk full?\n";
2022-05-14 09:36:32 +03:00
(void)!write(STDERR_FILENO, msg, sizeof(msg) - 1);
}
cleanup();
_exit(1);
}
void install_signal_handler() {
struct sigaction action;
action.sa_sigaction = sighandler;
sigemptyset(&action.sa_mask);
action.sa_flags = SA_SIGINFO;
sigaction(SIGINT, &action, NULL);
sigaction(SIGTERM, &action, NULL);
sigaction(SIGBUS, &action, NULL);
}
2022-05-28 09:03:06 +03:00
i64 get_default_thread_count() {
// mold doesn't scale well above 32 threads.
int n = tbb::global_control::active_value(
tbb::global_control::max_allowed_parallelism);
return std::min(n, 32);
}
} // namespace mold
2021-09-08 13:02:38 +03:00
int main(int argc, char **argv) {
std::string cmd = mold::filepath(argv[0]).filename();
2021-09-08 13:02:38 +03:00
if (cmd == "ld64" || cmd == "ld64.mold")
2021-09-08 14:15:03 +03:00
return mold::macho::main(argc, argv);
2021-09-08 13:02:38 +03:00
return mold::elf::main(argc, argv);
2021-09-08 13:02:38 +03:00
}