diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/thunk_loader.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/thunk_loader.cpp index f0996839fe37..833dd25c94ac 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/thunk_loader.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/thunk_loader.cpp @@ -46,13 +46,21 @@ #include #include #if defined(__linux__) -#include #include #endif namespace rocr { namespace core { +namespace { + +std::string GetAdjacentThunkLibraryPath(const std::string& library_name) { + return rocr::os::GetAdjacentLibraryPath( + reinterpret_cast(&GetAdjacentThunkLibraryPath), library_name); +} + +} // namespace + std::string ThunkLoader::whoami() { is_dtif_ = is_win_dxg_ = is_wsl_dxg_ = false; if (core::Runtime::runtime_singleton_->flag().enable_dtif()) { @@ -85,12 +93,24 @@ namespace core { library_name(whoami()), is_loaded_(false) { if (!library_name.empty()) { + std::string loaded_path = library_name; rocr::os::DlError(); // Clear any existing error messages thunk_handle = rocr::os::LoadLib(library_name.c_str()); if (thunk_handle == nullptr) { - fprintf(stderr, "Cannot load %s, failed:%s\n", library_name.c_str(), rocr::os::DlError()); - } else { - debug_print("Load %s successully!\n", library_name.c_str()); + const std::string relative_path = GetAdjacentThunkLibraryPath(library_name); + if (!relative_path.empty()) { + rocr::os::DlError(); + thunk_handle = rocr::os::LoadLib(relative_path.c_str()); + if (thunk_handle != nullptr) loaded_path = relative_path; + } + if (thunk_handle == nullptr) { + const char* error = rocr::os::DlError(); + fprintf(stderr, "Cannot load %s, failed:%s\n", library_name.c_str(), + error == nullptr ? "unknown error" : error); + } + } + if (thunk_handle != nullptr) { + debug_print("Load %s successfully!\n", loaded_path.c_str()); } is_loaded_ = true; } diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp index cc88210f1e27..0cfde3ddfef5 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp @@ -422,6 +422,15 @@ std::string GetLibraryName(LibHandle lib) { return map->l_name; } +std::string GetAdjacentLibraryPath(const void* address, const std::string& filename) { + Dl_info info = {}; + if (dladdr(address, &info) == 0 || info.dli_fname == nullptr) return {}; + + const std::string path(info.dli_fname); + const auto slash = path.find_last_of('/'); + return slash == std::string::npos ? std::string{} : path.substr(0, slash + 1) + filename; +} + Semaphore CreateSemaphore() { sem_t *sem = new sem_t; sem_init(sem, 0, 0); diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/util/os.h b/projects/rocr-runtime/runtime/hsa-runtime/core/util/os.h index 4e3881bf2ec6..3bc38ce4c641 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/util/os.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/util/os.h @@ -108,6 +108,12 @@ std::vector GetLoadedToolsLib(); /// @return: Path name of library std::string GetLibraryName(LibHandle lib); +/// @brief: Returns a path in the directory of the library containing an address. +/// @param: address(Input), address within the library +/// @param: filename(Input), file name to append to the library directory +/// @return: Path name adjacent to the library +std::string GetAdjacentLibraryPath(const void* address, const std::string& filename); + /// @brief: Creates a Semaphore, will return NULL if failed. /// @param: void. /// @return: Semaphore. diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/util/win/os_win.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/util/win/os_win.cpp index 3c9b900bbcbd..d5ea0d0db8f8 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/util/win/os_win.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/util/win/os_win.cpp @@ -107,6 +107,29 @@ std::string GetLibraryName(LibHandle lib) { return std::string{}; } +std::string GetAdjacentLibraryPath(const void* address, const std::string& filename) { + HMODULE module = nullptr; + if (!GetModuleHandleExA( + GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + reinterpret_cast(address), &module)) { + return {}; + } + + std::string path(MAX_PATH, '\0'); + for (;;) { + DWORD length = GetModuleFileNameA(module, path.data(), static_cast(path.size())); + if (length == 0) return {}; + if (length < path.size()) { + path.resize(length); + const auto slash = path.find_last_of("\\/"); + return slash == std::string::npos ? std::string{} + : path.substr(0, slash + 1) + filename; + } + if (path.size() >= 32768) return {}; + path.resize(path.size() * 2); + } +} + Semaphore CreateSemaphore() { auto sem = static_cast(CreateSemaphoreA(nullptr, 0, LONG_MAX, nullptr)); assert(sem != nullptr && "CreateSemaphore failed");