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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
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;
+}