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 @@ -69,6 +70,11 @@ DP("Loading RTLs...\n"); + std::string full_plugin_name; + bool loadPluginsFromSameDir = false; + void *handle; + struct link_map *map; + // 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 +83,39 @@ if (!dynlib_handle) { // Library does not exist or cannot be found. - DP("Unable to load library '%s': %s!\n", Name, dlerror()); - continue; + + DP("Trying to load plugins from same directory as libomptarget.so: %s\n", + Name); + handle = dlopen("libomptarget.so", RTLD_NOW); + if (!handle) { + DP("Unable to load library '%s': %s!\n", Name, dlerror()); + continue; + } + if (dlinfo(handle, RTLD_DI_LINKMAP, &map) == -1) { + DP("RTLD_DI_LINKMAP failed: %s\n", dlerror()); + dlclose(handle); + continue; + } + if (!map->l_name) { + DP("Absolute pathname not found: %s\n", dlerror()); + dlclose(handle); + continue; + } + + full_plugin_name.assign(map->l_name).append("/").append(Name); + dynlib_handle = dlopen(full_plugin_name.c_str(), RTLD_NOW); + if (!dynlib_handle) { + DP("Unable to load plugin '%s' from same directory as libomptarget.so: " + "%s\n", + Name, dlerror()); + continue; + } + loadPluginsFromSameDir = true; } - DP("Successfully loaded library '%s'!\n", Name); + DP("Successfully loaded library '%s'!\n", + loadPluginsFromSameDir ? full_plugin_name.c_str() : Name); + loadPluginsFromSameDir = false; // Retrieve the RTL information from the runtime library. RTLInfoTy R;