From b386d8792242ca6b67c904b2f79f173b908552b1 Mon Sep 17 00:00:00 2001 From: Hiroo HAYASHI <24754036+hirooih@users.noreply.github.com> Date: Sun, 22 Mar 2026 10:34:37 +0900 Subject: [PATCH] make the executable path relative from spike If the relative path cannot be found, use the current PREFIX. Signed-off-by: Hiroo HAYASHI <24754036+hirooih@users.noreply.github.com> --- fesvr/htif.cc | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/fesvr/htif.cc b/fesvr/htif.cc index 15f79bf5..267bb498 100644 --- a/fesvr/htif.cc +++ b/fesvr/htif.cc @@ -20,6 +20,11 @@ #include #include #include +#include +#include +#ifdef __APPLE__ +#include +#endif /* Attempt to determine the execution prefix automatically. autoconf * sets PREFIX, and pconfigure sets __PCONFIGURE__PREFIX. */ @@ -35,6 +40,10 @@ # define TARGET_DIR "/" TARGET_ARCH "/bin/" #endif +#ifndef PROC_SELF_EXE +# define PROC_SELF_EXE "/proc/self/exe" +#endif + static volatile bool signal_exit = false; static void handle_signal(int sig) { @@ -107,6 +116,20 @@ static void bad_address(const std::string& situation, reg_t addr) exit(-1); } +static std::string get_prefix_from_arg0() { + char exe_path[PATH_MAX]; +#ifdef __APPLE__ + uint32_t bufsize = PATH_MAX - 1; + ssize_t len = _NSGetExecutablePath(exe_path, &bufsize) == 0 ? bufsize : -1; +#else + ssize_t len = readlink(PROC_SELF_EXE, exe_path, PATH_MAX - 1); +#endif + if (len == -1) + return PREFIX; + exe_path[len] = '\0'; + return std::string(dirname(exe_path)) + "/.."; +} + std::map htif_t::load_payload(const std::string& payload, reg_t* entry, reg_t load_offset) { std::string path; @@ -114,14 +137,15 @@ std::map htif_t::load_payload(const std::string& payload, path = payload; else if (payload.find('/') == std::string::npos) { - std::string test_path = PREFIX TARGET_DIR + payload; + std::string prefix = get_prefix_from_arg0(); + std::string test_path = prefix + TARGET_DIR + payload; if (access(test_path.c_str(), F_OK) == 0) path = test_path; else throw std::runtime_error( "could not open " + payload + "; searched paths:\n" + "\t. (current directory)\n" + - "\t" + PREFIX TARGET_DIR + " (based on configured --prefix and --with-target)" + "\t" + prefix + TARGET_DIR + " (based on configured --prefix and --with-target)" ); }