Subject: Re: RTLD_DEFAULT & RTLD_NEXT
From: Sam Steingold <[email-masked]>
To: [email-masked]
Cc: [email-masked]
Date: Tue, 07 Sep 2004 16:52:46 -0400

the (C) assignment is in the mail.

2004-08-31  Sam Steingold  <[email-masked]>

	* dlfcn.cc (dlsym): Handle RTLD_DEFAULT using EnumProcessModules().
	* include/dlfcn.h (RTLD_DEFAULT): Define to NULL.

-- 
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.honestreporting.com>
Don't use force -- get a bigger hammer.

Index: src/winsup/cygwin/include/dlfcn.h
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/include/dlfcn.h,v
retrieving revision 1.2
diff -u -w -r1.2 dlfcn.h
--- src/winsup/cygwin/include/dlfcn.h	11 Sep 2001 20:01:01 -0000	1.2
+++ src/winsup/cygwin/include/dlfcn.h	7 Sep 2004 20:47:33 -0000
@@ -28,6 +28,7 @@
 extern void dlfork (int);
 
 /* following doesn't exist in Win32 API .... */
+#define RTLD_DEFAULT    NULL
 
 /* valid values for mode argument to dlopen */
 #define RTLD_LAZY	1	/* lazy function call binding */
Index: src/winsup/cygwin/dlfcn.cc
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/dlfcn.cc,v
retrieving revision 1.24
diff -u -w -r1.24 dlfcn.cc
--- src/winsup/cygwin/dlfcn.cc	3 Sep 2004 01:53:11 -0000	1.24
+++ src/winsup/cygwin/dlfcn.cc	7 Sep 2004 20:47:33 -0000
@@ -112,7 +112,27 @@
 void *
 dlsym (void *handle, const char *name)
 {
-  void *ret = (void *) GetProcAddress ((HMODULE) handle, name);
+  void *ret = NULL;
+  if (handle == RTLD_DEFAULT)
+    { /* search all modules */
+      HANDLE cur_proc = GetCurrentProcess();
+      HMODULE *modules;
+      DWORD needed, i;
+      if (!EnumProcessModules(cur_proc,NULL,0,&needed))
+        {
+         dlsym_fail:
+          set_dl_error ("dlsym");
+          return NULL;
+        }
+      modules = (HMODULE*)alloca(needed);
+      if (!EnumProcessModules(cur_proc,modules,needed,&needed))
+        goto dlsym_fail;
+      for (i=0; i < needed/sizeof(HMODULE); i++)
+        if ((ret = (void*)GetProcAddress(modules[i],name)))
+          break;
+    }
+  else
+    ret = (void*)GetProcAddress((HMODULE)handle,name);
   if (!ret)
     set_dl_error ("dlsym");
   debug_printf ("ret %p", ret);
Index: src/winsup/cygwin/autoload.cc
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/autoload.cc,v
retrieving revision 1.87
diff -u -w -r1.87 autoload.cc
--- src/winsup/cygwin/autoload.cc	3 Sep 2004 01:32:02 -0000	1.87
+++ src/winsup/cygwin/autoload.cc	7 Sep 2004 20:47:33 -0000
@@ -309,6 +309,7 @@
 LoadDLLfunc (DeregisterEventSource, 4, advapi32)
 LoadDLLfunc (DuplicateToken, 12, advapi32)
 LoadDLLfuncEx (DuplicateTokenEx, 24, advapi32, 1)
+LoadDLLfuncEx (EnumProcessModules, 16, psapi, 1)
 LoadDLLfunc (EqualSid, 8, advapi32)
 LoadDLLfunc (FindFirstFreeAce, 8, advapi32)
 LoadDLLfunc (GetAce, 12, advapi32)

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/