Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,21 @@
#include <core/util/os.h>
#include <iostream>
#if defined(__linux__)
#include <dlfcn.h>
#include <fcntl.h>
#endif

namespace rocr {
namespace core {

namespace {

std::string GetAdjacentThunkLibraryPath(const std::string& library_name) {
return rocr::os::GetAdjacentLibraryPath(
reinterpret_cast<const void*>(&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()) {
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions projects/rocr-runtime/runtime/hsa-runtime/core/util/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ std::vector<LibHandle> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LPCSTR>(address), &module)) {
return {};
}

std::string path(MAX_PATH, '\0');
for (;;) {
DWORD length = GetModuleFileNameA(module, path.data(), static_cast<DWORD>(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<void*>(CreateSemaphoreA(nullptr, 0, LONG_MAX, nullptr));
assert(sem != nullptr && "CreateSemaphore failed");
Expand Down
Loading