[Openmp-commits] [PATCH] D87413: [OpenMP] Load plugins from same directory as the libomptarget.so

Saiyedul Islam via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Wed Sep 9 13:15:43 PDT 2020


saiislam created this revision.
saiislam added reviewers: ABataev, jdoerfert, RaviNarayanaswamy, Hahnfeld, ronlieb.
Herald added subscribers: openmp-commits, guansong, yaxunl.
Herald added a project: OpenMP.
saiislam requested review of this revision.
Herald added a subscriber: sstefan1.

Introduce a fall back mechanism to load plugins from the same
directory as libomptarget.so if library does not exist or cannnot
be found. It avoids the need to set LD_LIBRARY_PATH to find
plugins.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87413

Files:
  openmp/libomptarget/src/rtl.cpp


Index: openmp/libomptarget/src/rtl.cpp
===================================================================
--- openmp/libomptarget/src/rtl.cpp
+++ openmp/libomptarget/src/rtl.cpp
@@ -18,6 +18,7 @@
 #include <cstdlib>
 #include <cstring>
 #include <dlfcn.h>
+#include <link.h>
 #include <mutex>
 #include <string>
 
@@ -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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87413.290814.patch
Type: text/x-patch
Size: 2083 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20200909/1f9d701a/attachment.bin>


More information about the Openmp-commits mailing list