From 29c46a54ffa31513be1a2cdcd9e8a55938d6e549 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 29 Jan 2019 22:38:42 +0100 Subject: Fix resolving global symbols when LoadLibrary() is called after dlopen() Usage of first_automatic_object cache is wrong. This cache is filled by all loaded DLL files (either implicitly or explicitly with LoadLibrary() call) by EnumProcessModules() call at first usage of dlopen(). So dlsym() can resolve global symbols only if they were loaded prior to dlopen() call. Any future usage of LoadLibrary() does not include newly loaded DLLs into first_automatic_object cache. To fix this problem, first_automatic_object cache is fully removed and EnumProcessModules() call is issued directly in dlsym() call. As EnumProcessModules() returns all DLLs, included those which were loaded by dlopen() with RTLD_LOCAL, it may break RTLD_LOCAL support. To address this problem switch linked-list of all loaded DLLs with RTLD_GLOBAL to linked-list of all loaded DLLs with RTLD_LOCAL flag. And then skip modules from EnumProcessModules() which are in linked-list. Also in WinAPI all DLLs loaded by LoadLibrary() behaves like RTLD_GLOBAL. So above change is compatible with this behavior. There may be another problem. Before retrieving HMODULE for DLL filename (which is done by LoadLibrary()), it is not possible to detect if DLL was already loaded by RTLD_LOCAL or not. And after calling LoadLibrary() it is not possible to know if DLL was loaded either by dlsym() with RTLD_LOCAL or by LoadLibrary() (which is equivalent to RTLD_GLOBAL). To address this problem, compare number of loaded modules (counted by EnumProcessModules()) before and after LoadLibrary() called from dlsym(). If number does not change it means that DLL was already loaded. So based on this result either add or remove HMODULE from linked-list of RTLD_LOCAL modules. Added test demonstrate usage of: global = dlopen(NULL, RTLD_GLOBAL); /* global handle */ LoadLibrary("library.dll"); /* this provides function */ function = dlsym(global, "function"); /* resolve function from library.dll */ --- CMakeLists.txt | 2 + Makefile | 7 +- cmake-test/CMakeLists.txt | 2 + dlfcn.c | 180 ++++++++++++++++++---------------------------- test.c | 27 +++++++ testdll3.c | 38 ++++++++++ 6 files changed, 144 insertions(+), 112 deletions(-) create mode 100644 testdll3.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 48945b2..a13cf99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,8 @@ if (BUILD_TESTS) enable_testing() add_library(testdll SHARED testdll.c) set_target_properties(testdll PROPERTIES PREFIX "") + add_library(testdll3 SHARED testdll3.c) + set_target_properties(testdll3 PROPERTIES PREFIX "") add_executable(t_dlfcn test.c) target_link_libraries(t_dlfcn dl) add_test (NAME t_dlfcn COMMAND t_dlfcn) diff --git a/Makefile b/Makefile index c02dce0..a0f5cb9 100644 --- a/Makefile +++ b/Makefile @@ -66,7 +66,10 @@ test.exe: test.o $(TARGETS) testdll.dll: testdll.c $(CC) -shared -o $@ $^ -test: $(TARGETS) test.exe testdll.dll +testdll3.dll: testdll3.c + $(CC) -shared -o $@ $^ + +test: $(TARGETS) test.exe testdll.dll testdll3.dll $(WINE) test.exe clean:: @@ -74,7 +77,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 + test.exe testdll.dll testdll3.dll distclean: clean rm -f config.mak diff --git a/cmake-test/CMakeLists.txt b/cmake-test/CMakeLists.txt index 532f2b8..b853920 100644 --- a/cmake-test/CMakeLists.txt +++ b/cmake-test/CMakeLists.txt @@ -8,6 +8,8 @@ find_package(dlfcn-win32 REQUIRED) add_library(testdll SHARED ../testdll.c) set_target_properties(testdll PROPERTIES PREFIX "") +add_library(testdll3 SHARED ../testdll3.c) +set_target_properties(testdll3 PROPERTIES PREFIX "") add_executable(t_dlfcn ../test.c) target_link_libraries(t_dlfcn dlfcn-win32::dl) enable_testing() diff --git a/dlfcn.c b/dlfcn.c index b356558..1e706f0 100644 --- a/dlfcn.c +++ b/dlfcn.c @@ -2,6 +2,7 @@ * dlfcn-win32 * Copyright (c) 2007 Ramiro Polla * Copyright (c) 2015 Tiancheng "Timothy" Gu + * Copyright (c) 2019 Pali Rohár * * dlfcn-win32 is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -52,57 +53,48 @@ * any kind of thread safety. */ -typedef struct global_object { +typedef struct local_object { HMODULE hModule; - struct global_object *previous; - struct global_object *next; -} global_object; + struct local_object *previous; + struct local_object *next; +} local_object; -static global_object first_object; -static global_object first_automatic_object; -static int auto_ref_count = 0; +static local_object first_object; -/* These functions implement a double linked list for the global objects. */ -static global_object *global_search( global_object *start, HMODULE hModule ) +/* These functions implement a double linked list for the local objects. */ +static local_object *local_search( HMODULE hModule ) { - global_object *pobject; + local_object *pobject; if( hModule == NULL ) return NULL; - for( pobject = start; pobject; pobject = pobject->next ) + for( pobject = &first_object; pobject; pobject = pobject->next ) if( pobject->hModule == hModule ) return pobject; return NULL; } -static void global_add( global_object *start, HMODULE hModule ) +static void local_add( HMODULE hModule ) { - global_object *pobject; - global_object *nobject; + local_object *pobject; + local_object *nobject; if( hModule == NULL ) return; - pobject = global_search( start, hModule ); + pobject = local_search( hModule ); /* Do not add object again if it's already on the list */ if( pobject ) return; - if( start == &first_automatic_object ) - { - pobject = global_search( &first_object, hModule ); - if( pobject ) - return; - } - - for( pobject = start; pobject->next; pobject = pobject->next ); + for( pobject = &first_object; pobject->next; pobject = pobject->next ); - nobject = (global_object*) malloc( sizeof( global_object ) ); + nobject = (local_object*) malloc( sizeof( local_object ) ); - /* Should this be enough to fail global_add, and therefore also fail + /* Should this be enough to fail local_add, and therefore also fail * dlopen? */ if( !nobject ) @@ -114,14 +106,14 @@ static void global_add( global_object *start, HMODULE hModule ) nobject->hModule = hModule; } -static void global_rem( global_object *start, HMODULE hModule ) +static void local_rem( HMODULE hModule ) { - global_object *pobject; + local_object *pobject; if( hModule == NULL ) return; - pobject = global_search( start, hModule ); + pobject = local_search( hModule ); if( !pobject ) return; @@ -224,10 +216,6 @@ void *dlopen( const char *file, int mode ) if( file == 0 ) { - HMODULE hAddtnlMods[1024]; // Already loaded modules - HANDLE hCurrentProc = GetCurrentProcess( ); - DWORD cbNeeded; - /* POSIX says that if the value of file is 0, a handle on a global * symbol object must be provided. That object must be able to access * all symbols from the original program file, and any objects loaded @@ -242,27 +230,13 @@ void *dlopen( const char *file, int mode ) if( !hModule ) save_err_ptr_str( file ); - - - /* GetModuleHandle( NULL ) only returns the current program file. So - * if we want to get ALL loaded module including those in linked DLLs, - * we have to use EnumProcessModules( ). - */ - if( EnumProcessModules( hCurrentProc, hAddtnlMods, - sizeof( hAddtnlMods ), &cbNeeded ) != 0 ) - { - DWORD i; - for( i = 0; i < cbNeeded / sizeof( HMODULE ); i++ ) - { - global_add( &first_automatic_object, hAddtnlMods[i] ); - } - } - auto_ref_count++; } else { + HANDLE hCurrentProc; + DWORD dwProcModsBefore, dwProcModsAfter; CHAR lpFileName[MAX_PATH]; - int i; + size_t i; /* MSDN says backslashes *must* be used instead of forward slashes. */ for( i = 0 ; i < sizeof(lpFileName) - 1 ; i ++ ) @@ -276,6 +250,11 @@ void *dlopen( const char *file, int mode ) } lpFileName[i] = '\0'; + hCurrentProc = GetCurrentProcess( ); + + if( EnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsBefore ) == 0 ) + dwProcModsBefore = 0; + /* POSIX says the search path is implementation-defined. * LOAD_WITH_ALTERED_SEARCH_PATH is used to make it behave more closely * to UNIX's search paths (start with system folders instead of current @@ -284,17 +263,24 @@ void *dlopen( const char *file, int mode ) hModule = LoadLibraryEx(lpFileName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH ); - /* If the object was loaded with RTLD_GLOBAL, add it to list of global - * objects, so that its symbols may be retrieved even if the handle for + if( EnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 ) + dwProcModsAfter = 0; + + /* If the object was loaded with RTLD_LOCAL, add it to list of local + * objects, so that its symbols cannot be retrieved even if the handle for * the original program file is passed. POSIX says that if the same * file is specified in multiple invocations, and any of them are * RTLD_GLOBAL, even if any further invocations use RTLD_LOCAL, the - * symbols will remain global. + * symbols will remain global. If number of loaded modules was not + * changed after calling LoadLibraryEx(), it means that library was + * already loaded. */ if( !hModule ) save_err_str( lpFileName ); - else if( (mode & RTLD_GLOBAL) ) - global_add( &first_object, hModule ); + else if( (mode & RTLD_LOCAL) && dwProcModsBefore != dwProcModsAfter ) + local_add( hModule ); + else if( !(mode & RTLD_LOCAL) && dwProcModsBefore == dwProcModsAfter ) + local_rem( hModule ); } /* Return to previous state of the error-mode bit flags. */ @@ -303,21 +289,6 @@ void *dlopen( const char *file, int mode ) return (void *) hModule; } -static void free_auto( ) -{ - global_object *pobject = first_automatic_object.next; - if( pobject ) - { - global_object *next; - for ( ; pobject; pobject = next ) - { - next = pobject->next; - free( pobject ); - } - first_automatic_object.next = NULL; - } -} - int dlclose( void *handle ) { HMODULE hModule = (HMODULE) handle; @@ -327,22 +298,11 @@ int dlclose( void *handle ) ret = FreeLibrary( hModule ); - /* If the object was loaded with RTLD_GLOBAL, remove it from list of global + /* If the object was loaded with RTLD_LOCAL, remove it from list of local * objects. */ if( ret ) - { - HMODULE cur = GetModuleHandle( NULL ); - global_rem( &first_object, hModule ); - if( hModule == cur ) - { - auto_ref_count--; - if( auto_ref_count < 0 ) - auto_ref_count = 0; - if( !auto_ref_count ) - free_auto( ); - } - } + local_rem( hModule ); else save_err_ptr_str( handle ); @@ -356,6 +316,7 @@ void *dlsym( void *handle, const char *name ) { FARPROC symbol; HMODULE hModule; + HANDLE hCurrentProc; #ifdef UNICODE wchar_t namew[MAX_PATH]; @@ -363,6 +324,7 @@ void *dlsym( void *handle, const char *name ) #endif current_error = NULL; + hCurrentProc = GetCurrentProcess( ); symbol = GetProcAddress( (HMODULE) handle, name ); @@ -377,25 +339,33 @@ void *dlsym( void *handle, const char *name ) if( hModule == handle ) { - global_object *pobject; - - for( pobject = &first_object; pobject; pobject = pobject->next ) - { - if( pobject->hModule ) - { - symbol = GetProcAddress( pobject->hModule, name ); - if( symbol != NULL ) - goto end; - } - } + HMODULE *modules; + DWORD cbNeeded; + DWORD dwSize; + size_t i; - for( pobject = &first_automatic_object; pobject; pobject = pobject->next ) + /* GetModuleHandle( NULL ) only returns the current program file. So + * if we want to get ALL loaded module including those in linked DLLs, + * we have to use EnumProcessModules( ). + */ + if( EnumProcessModules( hCurrentProc, NULL, 0, &dwSize ) != 0 ) { - if( pobject->hModule ) + modules = malloc( dwSize ); + if( modules ) { - symbol = GetProcAddress( pobject->hModule, name ); - if( symbol != NULL ) - goto end; + if( EnumProcessModules( hCurrentProc, modules, dwSize, &cbNeeded ) != 0 && dwSize == cbNeeded ) + { + for( i = 0; i < dwSize / sizeof( HMODULE ); i++ ) + { + if( local_search( modules[i] ) ) + continue; + symbol = GetProcAddress( modules[i], name ); + if( symbol != NULL ) + goto end; + } + + } + free( modules ); } } } @@ -472,18 +442,8 @@ char *dlerror( void ) BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved ) { (void) hinstDLL; - /* - * https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx - * - * When handling DLL_PROCESS_DETACH, a DLL should free resources such as heap - * memory only if the DLL is being unloaded dynamically (the lpReserved - * parameter is NULL). - */ - if( fdwReason == DLL_PROCESS_DETACH && !lpvReserved ) - { - auto_ref_count = 0; - free_auto( ); - } + (void) fdwReason; + (void) lpvReserved; return TRUE; } #endif diff --git a/test.c b/test.c index ba440da..10c655e 100644 --- a/test.c +++ b/test.c @@ -2,6 +2,7 @@ * dlfcn-win32 * Copyright (c) 2007-2009 Ramiro Polla * Copyright (c) 2014 Tiancheng "Timothy" Gu + * Copyright (c) 2019 Pali Rohár * * dlfcn-win32 is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -25,6 +26,7 @@ #endif #include #include +#include #include "dlfcn.h" /* If these dlclose's fails, we don't care as the handles are going to be @@ -77,6 +79,7 @@ int main() size_t (*fwrite_local) ( const void *, size_t, size_t, FILE * ); int (*nonexistentfunction)( void ); int ret; + HMODULE library3; #ifdef _DEBUG _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); @@ -335,6 +338,15 @@ int main() printf("SUCCESS\tGot symbol from global handle: %p\n", function); + library3 = LoadLibraryA("testdll3.dll"); + if (!library3) + { + printf( "ERROR\tCould not open library3 via WINAPI\n" ); + RETURN_ERROR; + } + else + printf( "SUCCESS\tOpened library3 via WINAPI: %p\n", library3 ); + ret = dlclose( library ); if( ret ) { @@ -346,6 +358,21 @@ int main() else printf( "SUCCESS\tClosed library.\n" ); + function = dlsym(global, "function3"); + if (!function) + { + error = dlerror(); + printf("ERROR\tCould not get symbol from global handle: %s\n", + error ? error : ""); + CLOSE_LIB; + CLOSE_GLOBAL; + RETURN_ERROR; + } + else + printf("SUCCESS\tGot symbol from global handle: %p\n", function); + + RUNFUNC; + ret = dlclose( global ); if( ret ) { diff --git a/testdll3.c b/testdll3.c new file mode 100644 index 0000000..3906e48 --- /dev/null +++ b/testdll3.c @@ -0,0 +1,38 @@ +/* + * dlfcn-win32 + * Copyright (c) 2007 Ramiro Polla + * Copyright (c) 2019 Pali Rohár + * + * 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 +#include +#endif +#include + +#if defined(_WIN32) +#define EXPORT __declspec(dllexport) +#else +#define EXPORT +#endif + +EXPORT int function3( void ) +{ + printf( "Hello, world!\n" ); + return 0; +} -- cgit v1.2.3-55-g6feb From 63d7bda42322bb0916e30bc547123a8330a4dcc2 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 29 Jan 2019 22:57:04 +0100 Subject: 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. --- CMakeLists.txt | 3 +++ Makefile | 7 +++-- cmake-test/CMakeLists.txt | 3 +++ dlfcn.c | 65 ++++++++++++++++++++++++++++++++++++++++++----- dlfcn.h | 4 +-- test.c | 60 +++++++++++++++++++++++++++++++++++++++++++ testdll.c | 6 +++++ testdll2.c | 55 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 193 insertions(+), 10 deletions(-) create mode 100644 testdll2.c 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 #include +#ifdef _MSC_VER +/* https://docs.microsoft.com/en-us/cpp/intrinsics/returnaddress */ +#include +#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 + * + * 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 +#include +#endif +#include + +#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; +} -- cgit v1.2.3-55-g6feb