This commit is contained in:
Rui Ueyama 2024-04-05 20:53:57 +09:00
parent 4396784dbd
commit faa5e57913
8 changed files with 75 additions and 95 deletions

View File

@ -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

View File

@ -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));

View File

@ -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

View File

@ -1,86 +0,0 @@
#include "common.h"
#include "config.h"
#include <tbb/global_control.h>
#ifdef USE_SYSTEM_MIMALLOC
# include <mimalloc-new-delete.h>
#endif
#ifdef __APPLE__
# include <mach-o/dyld.h>
#endif
#ifdef __FreeBSD__
# include <sys/sysctl.h>
# include <unistd.h>
#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);
}

View File

@ -3,6 +3,11 @@
#include <signal.h>
#include <tbb/version.h>
#ifdef __FreeBSD__
# include <sys/sysctl.h>
# include <unistd.h>
#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);
}

View File

@ -4,6 +4,11 @@
namespace mold {
void cleanup() {
if (output_tmpfile)
_unlink(output_tmpfile);
}
std::string errno_string() {
LPVOID buf;
DWORD dw = GetLastError();

View File

@ -5,6 +5,7 @@
#include <sstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <tbb/global_control.h>
#include <unordered_set>
#ifdef _WIN32
@ -293,6 +294,13 @@ expand_response_files(Context<E> &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)

View File

@ -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 <unistd.h>
#endif
#if defined(USE_SYSTEM_MIMALLOC) && defined(MOLD_X86_64)
# include <mimalloc-new-delete.h>
#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 <typename E>
@ -337,6 +349,7 @@ static void read_input_files(Context<E> &ctx, std::span<std::string> args) {
template <typename E>
int elf_main(int argc, char **argv) {
Context<E> 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<X86_64>(argc, argv);
}
#endif
using E = MOLD_TARGET;
template int elf_main<E>(int, char **);
} // namespace mold::elf
#ifdef MOLD_X86_64
int main(int argc, char **argv) {
return mold::elf::elf_main<mold::elf::X86_64>(argc, argv);
}
#endif