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 @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -60,6 +61,31 @@ delete TblMapMtx; } +/// Load plugins from same directory as libomptarget.so +static bool LoadLibFromCWD(char **PathName) { + struct link_map *Map; + + DP("Trying to load plugins from same directory as libomptarget.so: %s\n", + Name); + void *Handle = dlopen("libomptarget.so", RTLD_NOW); + if (!Handle) { + DP("Unable to load library : %s!\n", dlerror()); + return false; + } + if (dlinfo(Handle, RTLD_DI_LINKMAP, &Map) == -1) { + DP("RTLD_DI_LINKMAP failed: %s\n", dlerror()); + dlclose(Handle); + return false; + } + if (!Map->l_name) { + DP("Absolute pathname not found: %s\n", dlerror()); + dlclose(Handle); + return false; + } + *PathName = Map->l_name; + return true; +} + void RTLsTy::LoadRTLs() { // Parse environment variable OMP_TARGET_OFFLOAD (if set) TargetOffloadPolicy = (kmp_target_offload_kind_t) __kmpc_get_target_offload(); @@ -69,6 +95,10 @@ DP("Loading RTLs...\n"); + std::string FullPluginName; + char *LibompPathName = NULL; + bool LibFoundInCWD = LoadLibFromCWD(&LibompPathName); + // 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) { @@ -77,11 +107,23 @@ if (!dynlib_handle) { // Library does not exist or cannot be found. - DP("Unable to load library '%s': %s!\n", Name, dlerror()); - continue; + // Try to load plugins from same directory as libomptarget.so + + if (!LibFoundInCWD) + continue; + + FullPluginName.assign(LibompPathName).append("/").append(Name); + dynlib_handle = dlopen(FullPluginName.c_str(), RTLD_NOW); + if (!dynlib_handle) { + DP("Unable to load plugin '%s' from same directory as libomptarget.so: " + "%s\n", + Name, dlerror()); + continue; + } } - DP("Successfully loaded library '%s'!\n", Name); + DP("Successfully loaded library '%s'!\n", + FullPluginName ? FullPluginName.c_str() : Name); // Retrieve the RTL information from the runtime library. RTLInfoTy R;