diff --git a/openmp/libomptarget/src/rtl.cpp b/openmp/libomptarget/src/rtl.cpp --- a/openmp/libomptarget/src/rtl.cpp +++ b/openmp/libomptarget/src/rtl.cpp @@ -65,6 +65,30 @@ #endif } +namespace { +std::string findLibomptargetDirectory() { + Dl_info dl_info; + // look up a symbol which is known to be from libomptarget + if (dladdr((void *)&__tgt_register_lib, &dl_info) != 0) { + std::string libomptargetPath = std::string(dl_info.dli_fname); + size_t slash = libomptargetPath.find_last_of('/'); + if (slash != std::string::npos) { + return libomptargetPath.substr(0, slash + 1); // keep the / + } + } + return ""; +} + +void *verbose_dlopen(const char *Name) { + DP("Loading library '%s'...\n", Name); + void *dynlib_handle = dlopen(Name, RTLD_NOW); + if (!dynlib_handle) { + DP("Unable to load library '%s': %s!\n", Name, dlerror()); + } + return dynlib_handle; +} +} // namespace + void RTLsTy::LoadRTLs() { // Parse environment variable OMP_TARGET_OFFLOAD (if set) PM->TargetOffloadPolicy = @@ -74,17 +98,21 @@ } DP("Loading RTLs...\n"); + std::string libomptargetPath = findLibomptargetDirectory(); // Attempt to open all the plugins and, if they exist, check if the interface // is correct and if they are supporting any devices. for (auto *Name : RTLNames) { - DP("Loading library '%s'...\n", Name); - void *dynlib_handle = dlopen(Name, RTLD_NOW); + std::string adjacentPluginName = libomptargetPath + std::string(Name); + void *dynlib_handle = verbose_dlopen(adjacentPluginName.c_str()); if (!dynlib_handle) { - // Library does not exist or cannot be found. - DP("Unable to load library '%s': %s!\n", Name, dlerror()); - continue; + // Try again without the libomptarget library path prefix + dynlib_handle = verbose_dlopen(Name); + if (!dynlib_handle) { + // Library does not exist or cannot be found. + continue; + } } DP("Successfully loaded library '%s'!\n", Name);