From faa5e57913f3d61f2f4e9d75ad6fb9d1c5c5e82c Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 5 Apr 2024 20:53:57 +0900 Subject: [PATCH] Simplify --- CMakeLists.txt | 1 - common/common.h | 3 +- common/filepath.cc | 31 +++++++++++++++ common/main.cc | 86 ------------------------------------------ common/signal-unix.cc | 11 ++++++ common/signal-win32.cc | 5 +++ elf/cmdline.cc | 8 ++++ elf/main.cc | 25 +++++++++--- 8 files changed, 75 insertions(+), 95 deletions(-) delete mode 100644 common/main.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index b6b22c00..ae53f76b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -346,7 +346,6 @@ target_sources(mold PRIVATE common/filepath.cc common/glob.cc common/hyperloglog.cc - common/main.cc common/multi-glob.cc common/perf.cc common/tar.cc diff --git a/common/common.h b/common/common.h index 78546099..ae2b4e44 100644 --- a/common/common.h +++ b/common/common.h @@ -71,14 +71,13 @@ inline u8 *output_buffer_start = nullptr; inline u8 *output_buffer_end = nullptr; inline std::string mold_version; -extern std::string mold_version_string; +inline std::string mold_version_string; extern std::string mold_git_hash; std::string errno_string(); std::string get_self_path(); void cleanup(); void install_signal_handler(); -i64 get_default_thread_count(); static u64 combine_hash(u64 a, u64 b) { return a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2)); diff --git a/common/filepath.cc b/common/filepath.cc index 670188e2..37db8312 100644 --- a/common/filepath.cc +++ b/common/filepath.cc @@ -26,4 +26,35 @@ std::filesystem::path to_abs_path(std::filesystem::path path) { return (std::filesystem::current_path() / path).lexically_normal(); } +// Returns the path of the mold executable itself +std::string get_self_path() { +#if __APPLE__ + char path[8192]; + u32 size = sizeof(path); + if (_NSGetExecutablePath(path, &size)) { + std::cerr << "_NSGetExecutablePath failed\n"; + exit(1); + } + return path; +#elif __FreeBSD__ + // /proc may not be mounted on FreeBSD. The proper way to get the + // current executable's path is to use sysctl(2). + int mib[4]; + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PATHNAME; + mib[3] = -1; + + size_t size; + sysctl(mib, 4, NULL, &size, NULL, 0); + + std::string path; + path.resize(size); + sysctl(mib, 4, path.data(), &size, NULL, 0); + return path; +#else + return std::filesystem::read_symlink("/proc/self/exe").string(); +#endif +} + } // namespace mold diff --git a/common/main.cc b/common/main.cc deleted file mode 100644 index 3d67cbe6..00000000 --- a/common/main.cc +++ /dev/null @@ -1,86 +0,0 @@ -#include "common.h" -#include "config.h" - -#include - -#ifdef USE_SYSTEM_MIMALLOC -# include -#endif - -#ifdef __APPLE__ -# include -#endif - -#ifdef __FreeBSD__ -# include -# include -#endif - -#ifdef _WIN32 -# define unlink _unlink -#endif - -namespace mold { - -std::string mold_version_string = MOLD_VERSION; - -static std::string get_mold_version() { - if (mold_git_hash.empty()) - return "mold "s + MOLD_VERSION + " (compatible with GNU ld)"; - return "mold "s + MOLD_VERSION + " (" + mold_git_hash + - "; compatible with GNU ld)"; -} - -void cleanup() { - if (output_tmpfile) - unlink(output_tmpfile); -} - -// Returns the path of the mold executable itself -std::string get_self_path() { -#if __APPLE__ - char path[8192]; - u32 size = sizeof(path); - if (_NSGetExecutablePath(path, &size)) { - std::cerr << "_NSGetExecutablePath failed\n"; - exit(1); - } - return path; -#elif __FreeBSD__ - // /proc may not be mounted on FreeBSD. The proper way to get the - // current executable's path is to use sysctl(2). - int mib[4]; - mib[0] = CTL_KERN; - mib[1] = KERN_PROC; - mib[2] = KERN_PROC_PATHNAME; - mib[3] = -1; - - size_t size; - sysctl(mib, 4, NULL, &size, NULL, 0); - - std::string path; - path.resize(size); - sysctl(mib, 4, path.data(), &size, NULL, 0); - return path; -#else - return std::filesystem::read_symlink("/proc/self/exe").string(); -#endif -} - -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 - -namespace mold::elf { -int main(int argc, char **argv); -} - -int main(int argc, char **argv) { - mold::mold_version = mold::get_mold_version(); - return mold::elf::main(argc, argv); -} diff --git a/common/signal-unix.cc b/common/signal-unix.cc index 9bd58543..b8ce82dd 100644 --- a/common/signal-unix.cc +++ b/common/signal-unix.cc @@ -3,6 +3,11 @@ #include #include +#ifdef __FreeBSD__ +# include +# include +#endif + namespace mold { std::string errno_string() { @@ -12,6 +17,11 @@ std::string errno_string() { return strerror(errno); } +void cleanup() { + if (output_tmpfile) + unlink(output_tmpfile); +} + // 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 @@ -50,6 +60,7 @@ static void sighandler(int signo, siginfo_t *info, void *ucontext) { signal(SIGBUS, SIG_DFL); signal(SIGABRT, SIG_DFL); + cleanup(); raise(signo); } diff --git a/common/signal-win32.cc b/common/signal-win32.cc index 9b5a0e48..110c5afa 100644 --- a/common/signal-win32.cc +++ b/common/signal-win32.cc @@ -4,6 +4,11 @@ namespace mold { +void cleanup() { + if (output_tmpfile) + _unlink(output_tmpfile); +} + std::string errno_string() { LPVOID buf; DWORD dw = GetLastError(); diff --git a/elf/cmdline.cc b/elf/cmdline.cc index a2fafcc2..87946126 100644 --- a/elf/cmdline.cc +++ b/elf/cmdline.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include #ifdef _WIN32 @@ -293,6 +294,13 @@ expand_response_files(Context &ctx, char **argv) { return vec; } +static 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); +} + static inline std::string_view string_trim(std::string_view str) { size_t pos = str.find_first_not_of(" \t"); if (pos == str.npos) diff --git a/elf/main.cc b/elf/main.cc index ee800f2c..6c858e9e 100644 --- a/elf/main.cc +++ b/elf/main.cc @@ -1,4 +1,5 @@ #include "mold.h" +#include "config.h" #include "../common/archive-file.h" #include "../common/output-file.h" @@ -21,8 +22,19 @@ # include #endif +#if defined(USE_SYSTEM_MIMALLOC) && defined(MOLD_X86_64) +# include +#endif + namespace mold::elf { +static std::string get_mold_version() { + if (mold_git_hash.empty()) + return "mold "s + MOLD_VERSION + " (compatible with GNU ld)"; + return "mold "s + MOLD_VERSION + " (" + mold_git_hash + + "; compatible with GNU ld)"; +} + // Read the beginning of a given file and returns its machine type // (e.g. EM_X86_64 or EM_386). template @@ -337,6 +349,7 @@ static void read_input_files(Context &ctx, std::span args) { template int elf_main(int argc, char **argv) { Context ctx; + mold::mold_version = get_mold_version(); // Process -run option first. process_run_subcommand() does not return. if (argc >= 2 && (argv[1] == "-run"sv || argv[1] == "--run"sv)) { @@ -709,14 +722,14 @@ int elf_main(int argc, char **argv) { return 0; } -#ifdef MOLD_X86_64 -int main(int argc, char **argv) { - return elf_main(argc, argv); -} -#endif - using E = MOLD_TARGET; template int elf_main(int, char **); } // namespace mold::elf + +#ifdef MOLD_X86_64 +int main(int argc, char **argv) { + return mold::elf::elf_main(argc, argv); +} +#endif