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

62 lines
1.5 KiB
C++
Raw Normal View History

2021-09-08 13:02:38 +03:00
#include "elf/mold.h"
#include "macho/mold.h"
#include <cstring>
#include <signal.h>
namespace mold {
2021-09-28 10:10:33 +03:00
std::string_view errno_string() {
static thread_local char buf[200];
// There are two incompatible strerror_r implementations as follows.
//
// GNU: char *strerror_r(int, char *, size_t)
// POSIX: int strerror_r(int, char *, size_t)
//
// GNU version may write an error message to a buffer other than the
// given one and returns a pointer to the error message. POSIX version
// always write an error message to a given buffer.
if (std::is_same<decltype(strerror_r(errno, buf, sizeof(buf))), char *>::value)
return reinterpret_cast<char *>(strerror_r(errno, buf, sizeof(buf)));
strerror_r(errno, buf, sizeof(buf));
return buf;
}
2021-09-28 10:40:05 +03:00
#ifdef GIT_HASH
2021-12-20 05:22:55 +03:00
const std::string mold_version =
2021-09-28 10:40:05 +03:00
"mold " MOLD_VERSION " (" GIT_HASH "; compatible with GNU ld and GNU gold)";
#else
2021-12-20 05:22:55 +03:00
const std::string mold_version =
2021-09-28 10:40:05 +03:00
"mold " MOLD_VERSION " (compatible with GNU ld and GNU gold)";
#endif
void cleanup() {
if (output_tmpfile)
unlink(output_tmpfile);
if (socket_tmpfile)
unlink(socket_tmpfile);
}
static void signal_handler(int) {
cleanup();
_exit(1);
}
void install_signal_handler() {
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
}
} // 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
}