diff options
author | Silvio Traversaro <silvio@traversaro.it> | 2019-05-20 23:41:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-20 23:41:19 +0200 |
commit | 0fc1d9de6fea764866ecbfc10d7cf431bece033b (patch) | |
tree | cdd5423fa05e56af5ea604edb8334a6692383365 | |
parent | 84295c9afc0b42a61cf687d54c3bd5e286fd5176 (diff) | |
parent | f3da31d7a3e4b300f325624bf4e424c3bbcd521b (diff) | |
download | dlfcn-win32-1.2.0.tar.gz dlfcn-win32-1.2.0.tar.bz2 dlfcn-win32-1.2.0.zip |
Merge pull request #51 from pali/masterv1.2.0
Load Psapi.dll at runtime, this avoids linking caveat
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | dlfcn.c | 28 | ||||
-rw-r--r-- | visual-studio/12/dl/dl.vcxproj | 4 | ||||
-rw-r--r-- | visual-studio/12/test/test.vcxproj | 8 |
6 files changed, 26 insertions, 30 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 07addbb..e44343d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -17,7 +17,6 @@ if (BUILD_SHARED_LIBS) | |||
17 | endif (BUILD_SHARED_LIBS) | 17 | endif (BUILD_SHARED_LIBS) |
18 | 18 | ||
19 | add_library(dl ${sources}) | 19 | add_library(dl ${sources}) |
20 | target_link_libraries(dl psapi) | ||
21 | 20 | ||
22 | install (TARGETS dl EXPORT dlfcn-win32-targets | 21 | install (TARGETS dl EXPORT dlfcn-win32-targets |
23 | RUNTIME DESTINATION bin | 22 | RUNTIME DESTINATION bin |
@@ -3,7 +3,6 @@ | |||
3 | # | 3 | # |
4 | include config.mak | 4 | include config.mak |
5 | CFLAGS = -Wall -O3 -fomit-frame-pointer | 5 | CFLAGS = -Wall -O3 -fomit-frame-pointer |
6 | LIBS += -lpsapi | ||
7 | 6 | ||
8 | ifeq ($(BUILD_SHARED),yes) | 7 | ifeq ($(BUILD_SHARED),yes) |
9 | TARGETS += libdl.dll | 8 | TARGETS += libdl.dll |
@@ -34,7 +33,7 @@ libdl.a: $(LIB_OBJS) | |||
34 | $(RANLIB) libdl.a | 33 | $(RANLIB) libdl.a |
35 | 34 | ||
36 | libdl.dll: $(LIB_OBJS) | 35 | libdl.dll: $(LIB_OBJS) |
37 | $(CC) $(SHFLAGS) -shared -o $@ $^ $(LIBS) | 36 | $(CC) $(SHFLAGS) -shared -o $@ $^ |
38 | 37 | ||
39 | libdl.lib: libdl.dll | 38 | libdl.lib: libdl.dll |
40 | $(LIBCMD) /machine:i386 /def:libdl.def | 39 | $(LIBCMD) /machine:i386 /def:libdl.def |
@@ -54,20 +53,20 @@ static-install: include-install | |||
54 | mkdir -p $(DESTDIR)$(libdir) | 53 | mkdir -p $(DESTDIR)$(libdir) |
55 | cp libdl.a $(DESTDIR)$(libdir) | 54 | cp libdl.a $(DESTDIR)$(libdir) |
56 | 55 | ||
57 | lib-install: $(LIBS) | 56 | lib-install: |
58 | mkdir -p $(DESTDIR)$(libdir) | 57 | mkdir -p $(DESTDIR)$(libdir) |
59 | cp libdl.lib $(DESTDIR)$(libdir) | 58 | cp libdl.lib $(DESTDIR)$(libdir) |
60 | 59 | ||
61 | install: $(INSTALL) | 60 | install: $(INSTALL) |
62 | 61 | ||
63 | test.exe: test.o $(TARGETS) | 62 | test.exe: test.o $(TARGETS) |
64 | $(CC) -o $@ $< -L. -ldl $(LIBS) | 63 | $(CC) -o $@ $< -L. -ldl |
65 | 64 | ||
66 | testdll.dll: testdll.c | 65 | testdll.dll: testdll.c |
67 | $(CC) -shared -o $@ $^ | 66 | $(CC) -shared -o $@ $^ |
68 | 67 | ||
69 | testdll2.dll: testdll2.c $(TARGETS) | 68 | testdll2.dll: testdll2.c $(TARGETS) |
70 | $(CC) -shared -o $@ $< -L. -ldl $(LIBS) | 69 | $(CC) -shared -o $@ $< -L. -ldl |
71 | 70 | ||
72 | testdll3.dll: testdll3.c | 71 | testdll3.dll: testdll3.c |
73 | $(CC) -shared -o $@ $^ | 72 | $(CC) -shared -o $@ $^ |
@@ -43,12 +43,6 @@ target_link_libraries(<target> ${CMAKE_DL_LIBS}) | |||
43 | ... | 43 | ... |
44 | ~~~ | 44 | ~~~ |
45 | 45 | ||
46 | ### Linking caveat | ||
47 | This library uses the Process Status API in Windows (`psapi.lib`). If you are | ||
48 | linking to the static `dl.lib` or `libdl.a`, then you would need to explicitly | ||
49 | add `psapi.lib` or `-lpsapi` to your linking command, depending on if MinGW is | ||
50 | used. | ||
51 | |||
52 | Author | 46 | Author |
53 | ------ | 47 | ------ |
54 | 48 | ||
@@ -24,9 +24,7 @@ | |||
24 | #include <stdlib.h> | 24 | #include <stdlib.h> |
25 | #include <crtdbg.h> | 25 | #include <crtdbg.h> |
26 | #endif | 26 | #endif |
27 | #define PSAPI_VERSION 1 | ||
28 | #include <windows.h> | 27 | #include <windows.h> |
29 | #include <psapi.h> | ||
30 | #include <stdio.h> | 28 | #include <stdio.h> |
31 | #include <stdlib.h> | 29 | #include <stdlib.h> |
32 | 30 | ||
@@ -192,6 +190,24 @@ static void save_err_ptr_str( const void *ptr ) | |||
192 | save_err_str( ptr_buf ); | 190 | save_err_str( ptr_buf ); |
193 | } | 191 | } |
194 | 192 | ||
193 | /* Load Psapi.dll at runtime, this avoids linking caveat */ | ||
194 | static BOOL MyEnumProcessModules( HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded ) | ||
195 | { | ||
196 | static BOOL (WINAPI *EnumProcessModulesPtr)(HANDLE, HMODULE *, DWORD, LPDWORD); | ||
197 | HMODULE psapi; | ||
198 | |||
199 | if( !EnumProcessModulesPtr ) | ||
200 | { | ||
201 | psapi = LoadLibraryA( "Psapi.dll" ); | ||
202 | if( psapi ) | ||
203 | EnumProcessModulesPtr = (BOOL (WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD)) GetProcAddress( psapi, "EnumProcessModules" ); | ||
204 | if( !EnumProcessModulesPtr ) | ||
205 | return 0; | ||
206 | } | ||
207 | |||
208 | return EnumProcessModulesPtr( hProcess, lphModule, cb, lpcbNeeded ); | ||
209 | } | ||
210 | |||
195 | void *dlopen( const char *file, int mode ) | 211 | void *dlopen( const char *file, int mode ) |
196 | { | 212 | { |
197 | HMODULE hModule; | 213 | HMODULE hModule; |
@@ -240,7 +256,7 @@ void *dlopen( const char *file, int mode ) | |||
240 | 256 | ||
241 | hCurrentProc = GetCurrentProcess( ); | 257 | hCurrentProc = GetCurrentProcess( ); |
242 | 258 | ||
243 | if( EnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsBefore ) == 0 ) | 259 | if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsBefore ) == 0 ) |
244 | dwProcModsBefore = 0; | 260 | dwProcModsBefore = 0; |
245 | 261 | ||
246 | /* POSIX says the search path is implementation-defined. | 262 | /* POSIX says the search path is implementation-defined. |
@@ -251,7 +267,7 @@ void *dlopen( const char *file, int mode ) | |||
251 | hModule = LoadLibraryExA(lpFileName, NULL, | 267 | hModule = LoadLibraryExA(lpFileName, NULL, |
252 | LOAD_WITH_ALTERED_SEARCH_PATH ); | 268 | LOAD_WITH_ALTERED_SEARCH_PATH ); |
253 | 269 | ||
254 | if( EnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 ) | 270 | if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 ) |
255 | dwProcModsAfter = 0; | 271 | dwProcModsAfter = 0; |
256 | 272 | ||
257 | /* If the object was loaded with RTLD_LOCAL, add it to list of local | 273 | /* If the object was loaded with RTLD_LOCAL, add it to list of local |
@@ -366,12 +382,12 @@ void *dlsym( void *handle, const char *name ) | |||
366 | * if we want to get ALL loaded module including those in linked DLLs, | 382 | * if we want to get ALL loaded module including those in linked DLLs, |
367 | * we have to use EnumProcessModules( ). | 383 | * we have to use EnumProcessModules( ). |
368 | */ | 384 | */ |
369 | if( EnumProcessModules( hCurrentProc, NULL, 0, &dwSize ) != 0 ) | 385 | if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwSize ) != 0 ) |
370 | { | 386 | { |
371 | modules = malloc( dwSize ); | 387 | modules = malloc( dwSize ); |
372 | if( modules ) | 388 | if( modules ) |
373 | { | 389 | { |
374 | if( EnumProcessModules( hCurrentProc, modules, dwSize, &cbNeeded ) != 0 && dwSize == cbNeeded ) | 390 | if( MyEnumProcessModules( hCurrentProc, modules, dwSize, &cbNeeded ) != 0 && dwSize == cbNeeded ) |
375 | { | 391 | { |
376 | for( i = 0; i < dwSize / sizeof( HMODULE ); i++ ) | 392 | for( i = 0; i < dwSize / sizeof( HMODULE ); i++ ) |
377 | { | 393 | { |
diff --git a/visual-studio/12/dl/dl.vcxproj b/visual-studio/12/dl/dl.vcxproj index 77c7cdb..baa22d1 100644 --- a/visual-studio/12/dl/dl.vcxproj +++ b/visual-studio/12/dl/dl.vcxproj | |||
@@ -129,7 +129,6 @@ | |||
129 | </ClCompile> | 129 | </ClCompile> |
130 | <Link> | 130 | <Link> |
131 | <GenerateDebugInformation>true</GenerateDebugInformation> | 131 | <GenerateDebugInformation>true</GenerateDebugInformation> |
132 | <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
133 | </Link> | 132 | </Link> |
134 | </ItemDefinitionGroup> | 133 | </ItemDefinitionGroup> |
135 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|Win32'"> | 134 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|Win32'"> |
@@ -151,7 +150,6 @@ | |||
151 | </ClCompile> | 150 | </ClCompile> |
152 | <Link> | 151 | <Link> |
153 | <GenerateDebugInformation>true</GenerateDebugInformation> | 152 | <GenerateDebugInformation>true</GenerateDebugInformation> |
154 | <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
155 | </Link> | 153 | </Link> |
156 | </ItemDefinitionGroup> | 154 | </ItemDefinitionGroup> |
157 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|x64'"> | 155 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|x64'"> |
@@ -177,7 +175,6 @@ | |||
177 | <GenerateDebugInformation>true</GenerateDebugInformation> | 175 | <GenerateDebugInformation>true</GenerateDebugInformation> |
178 | <EnableCOMDATFolding>true</EnableCOMDATFolding> | 176 | <EnableCOMDATFolding>true</EnableCOMDATFolding> |
179 | <OptimizeReferences>true</OptimizeReferences> | 177 | <OptimizeReferences>true</OptimizeReferences> |
180 | <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
181 | </Link> | 178 | </Link> |
182 | </ItemDefinitionGroup> | 179 | </ItemDefinitionGroup> |
183 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|Win32'"> | 180 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|Win32'"> |
@@ -207,7 +204,6 @@ | |||
207 | <GenerateDebugInformation>true</GenerateDebugInformation> | 204 | <GenerateDebugInformation>true</GenerateDebugInformation> |
208 | <EnableCOMDATFolding>true</EnableCOMDATFolding> | 205 | <EnableCOMDATFolding>true</EnableCOMDATFolding> |
209 | <OptimizeReferences>true</OptimizeReferences> | 206 | <OptimizeReferences>true</OptimizeReferences> |
210 | <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
211 | </Link> | 207 | </Link> |
212 | </ItemDefinitionGroup> | 208 | </ItemDefinitionGroup> |
213 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|x64'"> | 209 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|x64'"> |
diff --git a/visual-studio/12/test/test.vcxproj b/visual-studio/12/test/test.vcxproj index aabc464..7f5feff 100644 --- a/visual-studio/12/test/test.vcxproj +++ b/visual-studio/12/test/test.vcxproj | |||
@@ -128,7 +128,6 @@ | |||
128 | </ClCompile> | 128 | </ClCompile> |
129 | <Link> | 129 | <Link> |
130 | <GenerateDebugInformation>true</GenerateDebugInformation> | 130 | <GenerateDebugInformation>true</GenerateDebugInformation> |
131 | <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
132 | </Link> | 131 | </Link> |
133 | </ItemDefinitionGroup> | 132 | </ItemDefinitionGroup> |
134 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|Win32'"> | 133 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|Win32'"> |
@@ -139,7 +138,6 @@ | |||
139 | </ClCompile> | 138 | </ClCompile> |
140 | <Link> | 139 | <Link> |
141 | <GenerateDebugInformation>true</GenerateDebugInformation> | 140 | <GenerateDebugInformation>true</GenerateDebugInformation> |
142 | <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
143 | </Link> | 141 | </Link> |
144 | </ItemDefinitionGroup> | 142 | </ItemDefinitionGroup> |
145 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | 143 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> |
@@ -150,7 +148,6 @@ | |||
150 | </ClCompile> | 148 | </ClCompile> |
151 | <Link> | 149 | <Link> |
152 | <GenerateDebugInformation>true</GenerateDebugInformation> | 150 | <GenerateDebugInformation>true</GenerateDebugInformation> |
153 | <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
154 | </Link> | 151 | </Link> |
155 | </ItemDefinitionGroup> | 152 | </ItemDefinitionGroup> |
156 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|x64'"> | 153 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|x64'"> |
@@ -161,7 +158,6 @@ | |||
161 | </ClCompile> | 158 | </ClCompile> |
162 | <Link> | 159 | <Link> |
163 | <GenerateDebugInformation>true</GenerateDebugInformation> | 160 | <GenerateDebugInformation>true</GenerateDebugInformation> |
164 | <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
165 | </Link> | 161 | </Link> |
166 | </ItemDefinitionGroup> | 162 | </ItemDefinitionGroup> |
167 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | 163 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> |
@@ -176,7 +172,6 @@ | |||
176 | <GenerateDebugInformation>true</GenerateDebugInformation> | 172 | <GenerateDebugInformation>true</GenerateDebugInformation> |
177 | <EnableCOMDATFolding>true</EnableCOMDATFolding> | 173 | <EnableCOMDATFolding>true</EnableCOMDATFolding> |
178 | <OptimizeReferences>true</OptimizeReferences> | 174 | <OptimizeReferences>true</OptimizeReferences> |
179 | <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
180 | </Link> | 175 | </Link> |
181 | </ItemDefinitionGroup> | 176 | </ItemDefinitionGroup> |
182 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|Win32'"> | 177 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|Win32'"> |
@@ -191,7 +186,6 @@ | |||
191 | <GenerateDebugInformation>true</GenerateDebugInformation> | 186 | <GenerateDebugInformation>true</GenerateDebugInformation> |
192 | <EnableCOMDATFolding>true</EnableCOMDATFolding> | 187 | <EnableCOMDATFolding>true</EnableCOMDATFolding> |
193 | <OptimizeReferences>true</OptimizeReferences> | 188 | <OptimizeReferences>true</OptimizeReferences> |
194 | <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
195 | </Link> | 189 | </Link> |
196 | </ItemDefinitionGroup> | 190 | </ItemDefinitionGroup> |
197 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | 191 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> |
@@ -206,7 +200,6 @@ | |||
206 | <GenerateDebugInformation>true</GenerateDebugInformation> | 200 | <GenerateDebugInformation>true</GenerateDebugInformation> |
207 | <EnableCOMDATFolding>true</EnableCOMDATFolding> | 201 | <EnableCOMDATFolding>true</EnableCOMDATFolding> |
208 | <OptimizeReferences>true</OptimizeReferences> | 202 | <OptimizeReferences>true</OptimizeReferences> |
209 | <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
210 | </Link> | 203 | </Link> |
211 | </ItemDefinitionGroup> | 204 | </ItemDefinitionGroup> |
212 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|x64'"> | 205 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|x64'"> |
@@ -221,7 +214,6 @@ | |||
221 | <GenerateDebugInformation>true</GenerateDebugInformation> | 214 | <GenerateDebugInformation>true</GenerateDebugInformation> |
222 | <EnableCOMDATFolding>true</EnableCOMDATFolding> | 215 | <EnableCOMDATFolding>true</EnableCOMDATFolding> |
223 | <OptimizeReferences>true</OptimizeReferences> | 216 | <OptimizeReferences>true</OptimizeReferences> |
224 | <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
225 | </Link> | 217 | </Link> |
226 | </ItemDefinitionGroup> | 218 | </ItemDefinitionGroup> |
227 | <ItemGroup> | 219 | <ItemGroup> |