diff options
Diffstat (limited to 'winsup/cygwin/dlfcn.cc')
-rw-r--r-- | winsup/cygwin/dlfcn.cc | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/winsup/cygwin/dlfcn.cc b/winsup/cygwin/dlfcn.cc index fb70524..f029ebb 100644 --- a/winsup/cygwin/dlfcn.cc +++ b/winsup/cygwin/dlfcn.cc @@ -194,9 +194,8 @@ dlopen (const char *name, int flags) break; } - DWORD gmheflags = (flags & RTLD_NODELETE) - ? GET_MODULE_HANDLE_EX_FLAG_PIN - : 0; + DWORD nodelete = (flags & RTLD_NODELETE) ? GET_MODULE_HANDLE_EX_FLAG_PIN + : 0; tmp_pathbuf tp; /* single one per stack frame */ tmp_pathbuf_allocator allocator (tp); @@ -267,7 +266,7 @@ dlopen (const char *name, int flags) if (flags & RTLD_NOLOAD) { - GetModuleHandleExW (gmheflags, wpath, (HMODULE *) &ret); + GetModuleHandleExW (nodelete, wpath, (HMODULE *) &ret); if (ret) break; } @@ -276,13 +275,29 @@ dlopen (const char *name, int flags) /* reference counting */ if (ret) { - dll *d = dlls.find (ret); + dll *d = dlls.find (ret, true); if (d) - ++d->count; + { + /* count == INT_MIN is used to specify RTLD_NODELETE */ + if (d->count == INT_MIN || nodelete) + d->count = INT_MIN; + else + ++d->count; + } + else + { + /* All Cygwin DLLs loaded or linked into this process get a dll + record when they call dll_dllcrt0 on init. So if we don't + find the dll it's a native DLL. Add it as DLL_NATIVE. + Simply restore the LoadLibrary count after fork. Don't care + where they are loaded to, don't try to fix up their data and + bss segments after fork, and don't run dtors. */ + dlls.alloc ((HMODULE) ret, user_data, DLL_NATIVE); + } } - if (ret && gmheflags) - GetModuleHandleExW (gmheflags, wpath, (HMODULE *) &ret); + if (ret && nodelete) + GetModuleHandleExW (nodelete, wpath, (HMODULE *) &ret); if (!ret) __seterrno (); @@ -348,16 +363,21 @@ dlclose (void *handle) int ret = 0; if (handle != GetModuleHandle (NULL)) { - /* reference counting */ - dll *d = dlls.find (handle); - if (!d || d->count <= 0) + /* Reference counting. + count == INT_MIN is used to specify RTLD_NODELETE */ + dll *d = dlls.find (handle, true); + if (!d || (d->count <= 0 && d->count != INT_MIN)) { errno = ENOENT; ret = -1; } - else + else if (d->count != INT_MIN) { --d->count; + /* Native DLLs don't call cygwin_detach_dll so they have to be + detached explicitely. */ + if (d->type == DLL_NATIVE && d->count <= 0) + dlls.detach (handle); if (!FreeLibrary ((HMODULE) handle)) { __seterrno (); |