From 39ff58c2754e6efb266bae5783715e181fe78a12 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Thu, 25 Jul 2019 20:32:53 +0200 Subject: Do not include intrin.h file Include file intrin.h is not needed for compiling dlfcn.c as _ReturnAddress intrinsic is defined in dlfcn.c manually. Moreover _ReturnAddress is available only as an intrinsic, there is no function implementation. So even including intrin.h file does not provide function variant for _ReturnAddress. More important, include file intrin.h is not available in older Windows Driver Kit 7.1.0. So removing usage of intrin.h file makes compilation of dlfcn-win32 also under these older WDK versions. --- dlfcn.c | 1 - 1 file changed, 1 deletion(-) diff --git a/dlfcn.c b/dlfcn.c index ce06d93..1815ae0 100644 --- a/dlfcn.c +++ b/dlfcn.c @@ -30,7 +30,6 @@ #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 */ -- cgit v1.2.3-55-g6feb From 403b240f298d2a9f85c76299f6da8750263fd43b Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Thu, 25 Jul 2019 20:34:28 +0200 Subject: Fix gcc warning: comparison between signed and unsigned integer expressions GetTempPathA() returns DWORD (32/64bit unsigned integer) and not int (32 signed integer). --- test.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test.c b/test.c index 0d07f12..b576b9a 100644 --- a/test.c +++ b/test.c @@ -86,6 +86,7 @@ int main() char toolongfile[32767]; DWORD code; char nonlibraryfile[MAX_PATH]; + DWORD length; HANDLE tempfile; DWORD dummy; UINT uMode; @@ -99,14 +100,14 @@ int main() _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT); #endif - ret = GetTempPathA( sizeof( nonlibraryfile ) - sizeof( "temp.dll" ), nonlibraryfile ); - if( ret == 0 || ret > sizeof( nonlibraryfile ) - sizeof( "temp.dll" ) ) + length = GetTempPathA( sizeof( nonlibraryfile ) - sizeof( "temp.dll" ), nonlibraryfile ); + if( length == 0 || length > sizeof( nonlibraryfile ) - sizeof( "temp.dll" ) ) { printf( "ERROR\tGetTempPath failed\n" ); RETURN_ERROR; } - memcpy( nonlibraryfile + ret, "temp.dll", sizeof( "temp.dll" ) ); + memcpy( nonlibraryfile + length, "temp.dll", sizeof( "temp.dll" ) ); tempfile = CreateFileA( (LPCSTR) nonlibraryfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL ); if( tempfile == INVALID_HANDLE_VALUE ) -- cgit v1.2.3-55-g6feb From 2bb5f487922aa908d27175c5a562d34f4c6197d9 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Thu, 25 Jul 2019 20:34:46 +0200 Subject: Fix gcc warning: ISO C forbids return between function pointer and void * Instead of using compiler specific pragma to disable particular warning, rewrite code which cast from function pointer to data pointer according to POSIX dlopen() documentation. This also fix compile warning under MSVC. According to the ISO C standard, casting between function pointers and 'void *', as done above, produces undefined results. POSIX.1-2003 and POSIX.1-2008 accepted this state of affairs and proposed the following workaround: *(void **) (&cosine) = dlsym(handle, "cos"); This (clumsy) cast conforms with the ISO C standard and will avoid any compiler warnings. --- dlfcn.c | 6 +----- test.c | 62 +++++++++++++++++++++++++++++++------------------------------- testdll2.c | 2 +- 3 files changed, 33 insertions(+), 37 deletions(-) diff --git a/dlfcn.c b/dlfcn.c index 1815ae0..f8af91f 100644 --- a/dlfcn.c +++ b/dlfcn.c @@ -454,11 +454,7 @@ end: save_err_str( name ); } - // warning C4054: 'type cast' : from function pointer 'FARPROC' to data pointer 'void *' -#ifdef _MSC_VER -#pragma warning( suppress: 4054 ) -#endif - return (void*) symbol; + return *(void **) (&symbol); } char *dlerror( void ) diff --git a/test.c b/test.c index b576b9a..798ab08 100644 --- a/test.c +++ b/test.c @@ -241,7 +241,7 @@ int main() else printf( "SUCCESS\tGot global handle: %p\n", global ); - fwrite_local = dlsym(global, "fwrite"); + *(void **) (&fwrite_local) = dlsym( global, "fwrite" ); if (!fwrite_local) { error = dlerror(); @@ -252,12 +252,12 @@ int main() RETURN_ERROR; } else - printf("SUCCESS\tGot symbol from global handle: %p\n", fwrite_local); + printf( "SUCCESS\tGot symbol from global handle: %p\n", *(void **) (&fwrite_local) ); char * hello_world = "Hello world from local fwrite!\n"; fwrite_local(hello_world,sizeof(char),strlen(hello_world),stderr); fflush(stderr); - fputs_default = dlsym(RTLD_DEFAULT, "fputs"); + *(void **) (&fputs_default) = dlsym( RTLD_DEFAULT, "fputs" ); if (!fputs_default) { error = dlerror(); @@ -268,12 +268,12 @@ int main() RETURN_ERROR; } else - printf("SUCCESS\tGot symbol from default handle: %p\n", fputs_default); + printf( "SUCCESS\tGot symbol from default handle: %p\n", *(void **) (&fputs_default) ); char * hello_world_fputs = "Hello world from default fputs!\n"; fputs_default(hello_world_fputs, stderr); fflush(stderr); - function = dlsym( library, "function" ); + *(void **) (&function) = dlsym( library, "function" ); if( !function ) { error = dlerror( ); @@ -284,11 +284,11 @@ int main() RETURN_ERROR; } else - printf( "SUCCESS\tGot symbol from library handle: %p\n", function ); + printf( "SUCCESS\tGot symbol from library handle: %p\n", *(void **) (&function) ); RUNFUNC; - function2_from_library2 = dlsym( library2, "function2" ); + *(void **) (&function2_from_library2) = dlsym( library2, "function2" ); if( !function2_from_library2 ) { error = dlerror( ); @@ -299,7 +299,7 @@ int main() RETURN_ERROR; } else - printf( "SUCCESS\tGot symbol from library2 handle: %p\n", function2_from_library2 ); + printf( "SUCCESS\tGot symbol from library2 handle: %p\n", *(void **) (&function2_from_library2) ); ret = function2_from_library2 (); if( ret != 2 ) @@ -309,11 +309,11 @@ int main() RETURN_ERROR; } - nonexistentfunction = dlsym( library, "nonexistentfunction" ); + *(void **) (&nonexistentfunction) = dlsym( library, "nonexistentfunction" ); if( nonexistentfunction ) { error = dlerror( ); - printf( "ERROR\tGot nonexistent symbol from library handle: %p\n", nonexistentfunction ); + printf( "ERROR\tGot nonexistent symbol from library handle: %p\n", *(void **) (&nonexistentfunction) ); CLOSE_LIB; CLOSE_GLOBAL; RETURN_ERROR; @@ -327,7 +327,7 @@ int main() else printf( "SUCCESS\tCould not get nonexistent symbol from library handle: %s\n", error ); - function = dlsym( global, "function" ); + *(void **) (&function) = dlsym( global, "function" ); if( !function ) { error = dlerror( ); @@ -338,15 +338,15 @@ int main() RETURN_ERROR; } else - printf( "SUCCESS\tGot symbol from global handle: %p\n", function ); + printf( "SUCCESS\tGot symbol from global handle: %p\n", *(void **) (&function) ); RUNFUNC; - nonexistentfunction = dlsym( global, "nonexistentfunction" ); + *(void **) (&nonexistentfunction) = dlsym( global, "nonexistentfunction" ); if( nonexistentfunction ) { error = dlerror( ); - printf( "ERROR\tGot nonexistent symbol from global handle: %p\n", nonexistentfunction ); + printf( "ERROR\tGot nonexistent symbol from global handle: %p\n", *(void **) (&nonexistentfunction) ); CLOSE_LIB; CLOSE_GLOBAL; RETURN_ERROR; @@ -392,7 +392,7 @@ int main() else printf( "SUCCESS\tOpened library locally: %p\n", library ); - function = dlsym( library, "function" ); + *(void **) (&function) = dlsym( library, "function" ); if( !function ) { error = dlerror( ); @@ -403,15 +403,15 @@ int main() RETURN_ERROR; } else - printf( "SUCCESS\tGot symbol from library handle: %p\n", function ); + printf( "SUCCESS\tGot symbol from library handle: %p\n", *(void **) (&function) ); RUNFUNC; - nonexistentfunction = dlsym( library, "nonexistentfunction" ); + *(void **) (&nonexistentfunction) = dlsym( library, "nonexistentfunction" ); if( nonexistentfunction ) { error = dlerror( ); - printf( "ERROR\tGot nonexistent symbol from library handle: %p\n", nonexistentfunction ); + printf( "ERROR\tGot nonexistent symbol from library handle: %p\n", *(void **) (&nonexistentfunction) ); CLOSE_LIB; CLOSE_GLOBAL; RETURN_ERROR; @@ -425,12 +425,12 @@ int main() else printf( "SUCCESS\tCould not get nonexistent symbol from library handle: %s\n", error ); - function = dlsym( global, "function" ); + *(void **) (&function) = dlsym( global, "function" ); if( function ) { error = dlerror( ); printf( "ERROR\tGot local symbol from global handle: %s @ %p\n", - error ? error : "", function ); + error ? error : "", *(void **) (&function) ); CLOSE_LIB; CLOSE_GLOBAL; RETURN_ERROR; @@ -438,11 +438,11 @@ int main() else printf( "SUCCESS\tDid not get local symbol from global handle.\n" ); - nonexistentfunction = dlsym( global, "nonexistentfunction" ); + *(void **) (&nonexistentfunction) = dlsym( global, "nonexistentfunction" ); if( nonexistentfunction ) { error = dlerror( ); - printf( "ERROR\tGot nonexistent local symbol from global handle: %p\n", nonexistentfunction ); + printf( "ERROR\tGot nonexistent local symbol from global handle: %p\n", *(void **) (&nonexistentfunction) ); CLOSE_LIB; CLOSE_GLOBAL; RETURN_ERROR; @@ -468,7 +468,7 @@ int main() else printf( "SUCCESS\tOpened library globally without closing it first: %p\n", library ); - function = dlsym( global, "function" ); + *(void **) (&function) = dlsym( global, "function" ); if( !function ) { error = dlerror( ); @@ -479,15 +479,15 @@ int main() RETURN_ERROR; } else - printf( "SUCCESS\tGot symbol from global handle: %p\n", function ); + printf( "SUCCESS\tGot symbol from global handle: %p\n", *(void **) (&function) ); RUNFUNC; - nonexistentfunction = dlsym( global, "nonexistentfunction" ); + *(void **) (&nonexistentfunction) = dlsym( global, "nonexistentfunction" ); if( nonexistentfunction ) { error = dlerror( ); - printf( "ERROR\tGot nonexistent symbol from global handle: %p\n", nonexistentfunction ); + printf( "ERROR\tGot nonexistent symbol from global handle: %p\n", *(void **) (&nonexistentfunction) ); CLOSE_LIB; CLOSE_GLOBAL; RETURN_ERROR; @@ -518,7 +518,7 @@ int main() } } - function = dlsym(global, "fwrite"); + *(void **) (&function) = dlsym( global, "fwrite" ); if (!function) { error = dlerror(); @@ -529,7 +529,7 @@ int main() RETURN_ERROR; } else - printf("SUCCESS\tGot symbol from global handle: %p\n", function); + printf( "SUCCESS\tGot symbol from global handle: %p\n", *(void **) (&function) ); uMode = SetErrorMode( SEM_FAILCRITICALERRORS ); @@ -541,7 +541,7 @@ int main() RETURN_ERROR; } else - printf( "SUCCESS\tOpened library3 via WINAPI: %p\n", library3 ); + printf( "SUCCESS\tOpened library3 via WINAPI: %p\n", (void *)library3 ); ret = dlclose( library ); if( ret ) @@ -554,7 +554,7 @@ int main() else printf( "SUCCESS\tClosed library.\n" ); - function = dlsym(global, "function3"); + *(void **) (&function) = dlsym( global, "function3" ); if (!function) { error = dlerror(); @@ -565,7 +565,7 @@ int main() RETURN_ERROR; } else - printf("SUCCESS\tGot symbol from global handle: %p\n", function); + printf( "SUCCESS\tGot symbol from global handle: %p\n", *(void **) (&function) ); RUNFUNC; diff --git a/testdll2.c b/testdll2.c index 910c820..71d385a 100644 --- a/testdll2.c +++ b/testdll2.c @@ -38,7 +38,7 @@ EXPORT int function2( void ) char *error; int (*function2_orig)(void); printf( "Hello, world! from wrapper library\n" ); - function2_orig = dlsym(RTLD_NEXT, "function2"); + *(void **) (&function2_orig) = dlsym( RTLD_NEXT, "function2" ); if (!function2_orig) { error = dlerror( ); -- cgit v1.2.3-55-g6feb From ff3026745272b9aa61731ee4150735e2c5cfcb70 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 26 Jul 2019 18:21:30 +0200 Subject: Fix more gcc warnings ISO C90 forbids mixed declarations and code ISO C++ forbids converting a string constant to 'char*' --- test.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/test.c b/test.c index 798ab08..aa0e0f1 100644 --- a/test.c +++ b/test.c @@ -253,9 +253,11 @@ int main() } else printf( "SUCCESS\tGot symbol from global handle: %p\n", *(void **) (&fwrite_local) ); - char * hello_world = "Hello world from local fwrite!\n"; - fwrite_local(hello_world,sizeof(char),strlen(hello_world),stderr); - fflush(stderr); + { + const char *hello_world = "Hello world from local fwrite!\n"; + fwrite_local( hello_world, sizeof( char ), strlen( hello_world ), stderr ); + fflush( stderr ); + } *(void **) (&fputs_default) = dlsym( RTLD_DEFAULT, "fputs" ); if (!fputs_default) @@ -269,9 +271,11 @@ int main() } else printf( "SUCCESS\tGot symbol from default handle: %p\n", *(void **) (&fputs_default) ); - char * hello_world_fputs = "Hello world from default fputs!\n"; - fputs_default(hello_world_fputs, stderr); - fflush(stderr); + { + const char *hello_world_fputs = "Hello world from default fputs!\n"; + fputs_default( hello_world_fputs, stderr ); + fflush( stderr ); + } *(void **) (&function) = dlsym( library, "function" ); if( !function ) -- cgit v1.2.3-55-g6feb