Commit 63d7bda42322bb0916e30bc547123a8330a4dcc2

Pali Rohár 2019-01-29T22:57:04

Implement support for dlsym() with RTLD_DEFAULT and RTLD_NEXT dlsym() with RTLD_DEFAULT handle behaves in same way like with global handle returned by dlopen() with NULL file name. dlsym() with RTLD_NEXT handle search for next loaded module which provides specified symbol. "Next" means module which in EnumProcessModules() result after the module which called dlsym(). To get caller function of dlsym() use _ReturnAddress() intrinsic. To get module where is caller function use the fact that HMODULE is the same value as the module's base address. When compiling under gcc, defines _ReturnAddress() macro via gcc's builtin as it does not provide MSC's specific _ReturnAddress() intrinsic. Added tests demonstrate that both RTLD_DEFAULT and RTLD_NEXT are working as expected.

diff --git a/CMakeLists.txt b/CMakeLists.txt
index a13cf99..07addbb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -63,6 +63,9 @@ if (BUILD_TESTS)
   enable_testing()
   add_library(testdll SHARED testdll.c)
   set_target_properties(testdll PROPERTIES PREFIX "")
+  add_library(testdll2 SHARED testdll2.c)
+  set_target_properties(testdll2 PROPERTIES PREFIX "")
+  target_link_libraries(testdll2 dl)
   add_library(testdll3 SHARED testdll3.c)
   set_target_properties(testdll3 PROPERTIES PREFIX "")
   add_executable(t_dlfcn test.c)
diff --git a/Makefile b/Makefile
index a0f5cb9..efac5af 100644
--- a/Makefile
+++ b/Makefile
@@ -66,10 +66,13 @@ test.exe: test.o $(TARGETS)
 testdll.dll: testdll.c
 	$(CC) -shared -o $@ $^
 
+testdll2.dll: testdll2.c $(TARGETS)
+	$(CC) -shared -o $@ $< -L. -ldl $(LIBS)
+
 testdll3.dll: testdll3.c
 	$(CC) -shared -o $@ $^
 
-test: $(TARGETS) test.exe testdll.dll testdll3.dll
+test: $(TARGETS) test.exe testdll.dll testdll2.dll testdll3.dll
 	$(WINE) test.exe
 
 clean::
@@ -77,7 +80,7 @@ clean::
 		dlfcn.o \
 		libdl.dll libdl.a libdl.def libdl.dll.a libdl.lib libdl.exp \
 		tmptest.c tmptest.dll \
-		test.exe testdll.dll testdll3.dll
+		test.exe testdll.dll testdll2.dll testdll3.dll
 
 distclean: clean
 	rm -f config.mak
diff --git a/cmake-test/CMakeLists.txt b/cmake-test/CMakeLists.txt
index b853920..659a79a 100644
--- a/cmake-test/CMakeLists.txt
+++ b/cmake-test/CMakeLists.txt
@@ -8,6 +8,9 @@ find_package(dlfcn-win32 REQUIRED)
 
 add_library(testdll SHARED ../testdll.c)
 set_target_properties(testdll PROPERTIES PREFIX "")
+add_library(testdll2 SHARED ../testdll2.c)
+set_target_properties(testdll2 PROPERTIES PREFIX "")
+target_link_libraries(testdll2 dlfcn-win32::dl)
 add_library(testdll3 SHARED ../testdll3.c)
 set_target_properties(testdll3 PROPERTIES PREFIX "")
 add_executable(t_dlfcn ../test.c)
diff --git a/dlfcn.c b/dlfcn.c
index 1e706f0..8d3c793 100644
--- a/dlfcn.c
+++ b/dlfcn.c
@@ -30,6 +30,17 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#ifdef _MSC_VER
+/* https://docs.microsoft.com/en-us/cpp/intrinsics/returnaddress */
+#include <intrin.h>
+#pragma intrinsic(_ReturnAddress)
+#else
+/* https://gcc.gnu.org/onlinedocs/gcc/Return-Address.html */
+#ifndef _ReturnAddress
+#define _ReturnAddress() (__builtin_extract_return_addr(__builtin_return_address(0)))
+#endif
+#endif
+
 #ifdef SHARED
 #define DLFCN_WIN32_EXPORTS
 #endif
@@ -312,9 +323,11 @@ int dlclose( void *handle )
     return (int) ret;
 }
 
+__declspec(noinline) /* Needed for _ReturnAddress() */
 void *dlsym( void *handle, const char *name )
 {
     FARPROC symbol;
+    HMODULE hCaller;
     HMODULE hModule;
     HANDLE hCurrentProc;
 
@@ -324,20 +337,53 @@ void *dlsym( void *handle, const char *name )
 #endif
 
     current_error = NULL;
+    symbol = NULL;
+    hCaller = NULL;
+    hModule = GetModuleHandle( NULL );
     hCurrentProc = GetCurrentProcess( );
 
-    symbol = GetProcAddress( (HMODULE) handle, name );
+    if( handle == RTLD_DEFAULT )
+    {
+        /* The symbol lookup happens in the normal global scope; that is,
+         * a search for a symbol using this handle would find the same
+         * definition as a direct use of this symbol in the program code.
+         * So use same lookup procedure as when filename is NULL.
+         */
+        handle = hModule;
+    }
+    else if( handle == RTLD_NEXT )
+    {
+        /* Specifies the next object after this one that defines name.
+         * This one refers to the object containing the invocation of dlsym().
+         * The next object is the one found upon the application of a load
+         * order symbol resolution algorithm. To get caller function of dlsym()
+         * use _ReturnAddress() intrinsic. To get HMODULE of caller function
+         * use undocumented hack from https://stackoverflow.com/a/2396380
+         * The HMODULE of a DLL is the same value as the module's base address.
+         */
+        MEMORY_BASIC_INFORMATION info;
+        size_t sLen;
+        sLen = VirtualQueryEx( hCurrentProc, _ReturnAddress(), &info, sizeof( info ) );
+        if( sLen != sizeof( info ) )
+            goto end;
+        hCaller = (HMODULE) info.AllocationBase;
+        if(!hCaller)
+            goto end;
+    }
+
+    if( handle != RTLD_NEXT )
+    {
+        symbol = GetProcAddress( (HMODULE) handle, name );
 
-    if( symbol != NULL )
-        goto end;
+        if( symbol != NULL )
+            goto end;
+    }
 
     /* If the handle for the original program file is passed, also search
      * in all globally loaded objects.
      */
 
-    hModule = GetModuleHandle( NULL );
-
-    if( hModule == handle )
+    if( hModule == handle || handle == RTLD_NEXT )
     {
         HMODULE *modules;
         DWORD cbNeeded;
@@ -357,6 +403,13 @@ void *dlsym( void *handle, const char *name )
                 {
                     for( i = 0; i < dwSize / sizeof( HMODULE ); i++ )
                     {
+                        if( handle == RTLD_NEXT && hCaller )
+                        {
+                            /* Next modules can be used for RTLD_NEXT */
+                            if( hCaller == modules[i] )
+                                hCaller = NULL;
+                            continue;
+                        }
                         if( local_search( modules[i] ) )
                             continue;
                         symbol = GetProcAddress( modules[i], name );
diff --git a/dlfcn.h b/dlfcn.h
index 711e431..c0d7777 100644
--- a/dlfcn.h
+++ b/dlfcn.h
@@ -44,8 +44,8 @@ extern "C" {
  * Note: All other RTLD_* flags in any dlfcn.h are not standard compliant.
  */
 
-#define RTLD_DEFAULT    0
-#define RTLD_NEXT       0
+#define RTLD_DEFAULT    ((void *)0)
+#define RTLD_NEXT       ((void *)-1)
 
 DLFCN_EXPORT void *dlopen ( const char *file, int mode );
 DLFCN_EXPORT int   dlclose(void *handle);
diff --git a/test.c b/test.c
index 10c655e..814aca1 100644
--- a/test.c
+++ b/test.c
@@ -73,10 +73,13 @@
 int main()
 {
     void *global;
+    void *library2;
     void *library;
     char *error;
     int (*function)( void );
+    int (*function2_from_library2)( void );
     size_t (*fwrite_local) ( const void *, size_t, size_t, FILE * );
+    size_t (*fputs_default) ( const char *, FILE * );
     int (*nonexistentfunction)( void );
     int ret;
     HMODULE library3;
@@ -90,6 +93,16 @@ int main()
     _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
 #endif
 
+    library2 = dlopen( "testdll2.dll", RTLD_GLOBAL );
+    if( !library2 )
+    {
+        error = dlerror( );
+        printf( "ERROR\tCould not open library2 globally: %s\n", error ? error : "" );
+        RETURN_ERROR; 
+    }
+    else
+        printf( "SUCCESS\tOpened library2 globally: %p\n", library2 );
+
     library = dlopen( "testdll.dll", RTLD_GLOBAL );
     if( !library )
     {
@@ -127,6 +140,22 @@ int main()
     fwrite_local(hello_world,sizeof(char),strlen(hello_world),stderr);
     fflush(stderr);
 
+    fputs_default = dlsym(RTLD_DEFAULT, "fputs");
+    if (!fputs_default)
+    {
+        error = dlerror();
+        printf("ERROR\tCould not get symbol from default handle: %s\n",
+            error ? error : "");
+        CLOSE_LIB;
+        CLOSE_GLOBAL;
+        RETURN_ERROR;
+    }
+    else
+        printf("SUCCESS\tGot symbol from default handle: %p\n", fputs_default);
+    char * hello_world_fputs = "Hello world from default fputs!\n";
+    fputs_default(hello_world_fputs, stderr);
+    fflush(stderr);
+
     function = dlsym( library, "function" );
     if( !function )
     {
@@ -142,6 +171,27 @@ int main()
 
     RUNFUNC;
 
+    function2_from_library2 = dlsym( library2, "function2" );
+    if( !function2_from_library2 )
+    {
+        error = dlerror( );
+        printf( "ERROR\tCould not get symbol from library2 handle: %s\n",
+                error ? error : "" );
+        CLOSE_LIB;
+        CLOSE_GLOBAL;
+        RETURN_ERROR;
+    }
+    else
+        printf( "SUCCESS\tGot symbol from library2 handle: %p\n", function2_from_library2 );
+
+    ret = function2_from_library2 ();
+    if( ret != 2 )
+    {
+        CLOSE_LIB;
+        CLOSE_GLOBAL;
+        RETURN_ERROR;
+    }
+
     nonexistentfunction = dlsym( library, "nonexistentfunction" );
     if( nonexistentfunction )
     {
@@ -197,6 +247,16 @@ int main()
     else
         printf( "SUCCESS\tClosed library.\n" );
 
+    ret = dlclose( library2 );
+    if( ret )
+    {
+        error = dlerror( );
+        printf( "ERROR\tCould not close library2: %s\n", error ? error : "" );
+        RETURN_ERROR;
+    }
+    else
+        printf( "SUCCESS\tClosed library2.\n" );
+
     library = dlopen( "testdll.dll", RTLD_LOCAL );
     if( !library )
     {
diff --git a/testdll.c b/testdll.c
index ff99f87..3df2234 100644
--- a/testdll.c
+++ b/testdll.c
@@ -30,6 +30,12 @@
 #define EXPORT
 #endif
 
+EXPORT int function2( void )
+{
+    printf( "Hello, world! from original library\n" );
+    return 0;
+}
+
 EXPORT int function( void )
 {
     printf( "Hello, world!\n" );
diff --git a/testdll2.c b/testdll2.c
new file mode 100644
index 0000000..910c820
--- /dev/null
+++ b/testdll2.c
@@ -0,0 +1,55 @@
+/*
+ * dlfcn-win32
+ * Copyright (c) 2007 Ramiro Polla
+ * Copyright (c) 2019 Pali Rohár <pali.rohar@gmail.com>
+ *
+ * dlfcn-win32 is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * dlfcn-win32 is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with dlfcn-win32; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifdef _DEBUG
+#define _CRTDBG_MAP_ALLOC
+#include <stdlib.h>
+#include <crtdbg.h>
+#endif
+#include <stdio.h>
+
+#include "dlfcn.h"
+
+#if defined(_WIN32)
+#define EXPORT __declspec(dllexport)
+#else
+#define EXPORT
+#endif
+
+EXPORT int function2( void )
+{
+    char *error;
+    int (*function2_orig)(void);
+    printf( "Hello, world! from wrapper library\n" );
+    function2_orig = dlsym(RTLD_NEXT, "function2");
+    if (!function2_orig)
+    {
+        error = dlerror( );
+        printf( "ERROR\tCould not get symbol from RTLD_NEXT handle: %s\n",
+                error ? error : "" );
+        return 1;
+    }
+    if (function2_orig() != 0)
+    {
+        printf( "ERROR\tOriginal function from RTLD_NEXT handle did not return correct value\n" );
+        return 1;
+    }
+    return 2;
+}