>From 6e2739deccb06a4d739d2a41fa32355f21c151a1 Mon Sep 17 00:00:00 2001
From: Michael Haubenwallner <michael.haubenwallner@ssi-schaefer.com>
Date: Tue, 19 Jul 2016 14:56:48 +0200
Subject: [PATCH 2/3] dlopen: search main executable's directory

---
 winsup/cygwin/dlfcn.cc | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/winsup/cygwin/dlfcn.cc b/winsup/cygwin/dlfcn.cc
index e2093ab..4ea9984 100644
--- a/winsup/cygwin/dlfcn.cc
+++ b/winsup/cygwin/dlfcn.cc
@@ -80,6 +80,19 @@ get_full_path_of_dll (const char* str, path_conv &real_filename)
 	 not use the LD_LIBRARY_PATH environment variable. */
       finder.add_envsearchpath ("LD_LIBRARY_PATH");
 
+      /* Unless we have a special cygwin loader, there is no such thing like
+	 DT_RUNPATH on Windows we can use to search for dlls, except for the
+	 directory of the main executable. */
+      tmp_pathbuf tp;
+      PWCHAR exewname = tp.w_get ();
+      GetModuleFileNameW (NULL, exewname, NT_MAX_PATH);
+      char * exedir = tp.c_get ();
+      *exedir = '\0';
+      cygwin_conv_path (CCP_WIN_W_TO_POSIX, exewname, exedir, NT_MAX_PATH);
+      char * lastsep = strrchr (exedir, '/');
+      if (lastsep)
+	finder.add_searchdir (exedir, lastsep - exedir);
+
       /* Finally we better have some fallback. */
       finder.add_searchdir ("/usr/lib", -1);
     }
-- 
2.8.3

