diff options
Diffstat (limited to 'winsup/cygwin/dlfcn.cc')
-rw-r--r-- | winsup/cygwin/dlfcn.cc | 92 |
1 files changed, 51 insertions, 41 deletions
diff --git a/winsup/cygwin/dlfcn.cc b/winsup/cygwin/dlfcn.cc index fb70524..e06616d 100644 --- a/winsup/cygwin/dlfcn.cc +++ b/winsup/cygwin/dlfcn.cc @@ -7,6 +7,7 @@ Cygwin license. Please consult the file "CYGWIN_LICENSE" for details. */ #include "winsup.h" +#include <psapi.h> #include <stdlib.h> #include <dlfcn.h> #include <ctype.h> @@ -194,9 +195,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 +267,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 +276,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 (); @@ -303,32 +319,18 @@ dlsym (void *handle, const char *name) if (handle == RTLD_DEFAULT) { /* search all modules */ - PDEBUG_BUFFER buf; - NTSTATUS status; + HMODULE *modules; + tmp_pathbuf tp; + DWORD size; - buf = RtlCreateQueryDebugBuffer (0, FALSE); - if (!buf) - { - set_errno (ENOMEM); - set_dl_error ("dlsym"); - return NULL; - } - status = RtlQueryProcessDebugInformation (GetCurrentProcessId (), - PDI_MODULES, buf); - if (!NT_SUCCESS (status)) - __seterrno_from_nt_status (status); + modules = (HMODULE *) tp.w_get (); + if (!EnumProcessModules (GetCurrentProcess (), modules, + 2 * NT_MAX_PATH, &size)) + __seterrno (); else - { - PDEBUG_MODULE_ARRAY mods = (PDEBUG_MODULE_ARRAY) - buf->ModuleInformation; - for (ULONG i = 0; i < mods->Count; ++i) - if ((ret = (void *) - GetProcAddress ((HMODULE) mods->Modules[i].Base, name))) - break; - if (!ret) - set_errno (ENOENT); - } - RtlDestroyQueryDebugBuffer (buf); + for (uint32_t i = 0; i < size / sizeof (HMODULE); ++i) + if ((ret = (void *) GetProcAddress (modules[i], name))) + break; } else { @@ -348,16 +350,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 (); @@ -388,7 +395,8 @@ extern "C" int dladdr (const void *addr, Dl_info *info) { HMODULE hModule; - BOOL ret = GetModuleHandleEx (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, + BOOL ret = GetModuleHandleEx (GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT| + GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR) addr, &hModule); if (!ret) @@ -400,14 +408,16 @@ dladdr (const void *addr, Dl_info *info) /* Get the module filename. This pathname may be in short-, long- or //?/ format, depending on how it was specified when loaded, but we assume this is always an absolute pathname. */ - WCHAR fname[MAX_PATH]; - DWORD length = GetModuleFileNameW (hModule, fname, MAX_PATH); - if ((length == 0) || (length == MAX_PATH)) + tmp_pathbuf tp; + PWCHAR fname = tp.w_get (); + DWORD length = GetModuleFileNameW (hModule, fname, NT_MAX_PATH); + if ((length == 0) || (length == NT_MAX_PATH)) return 0; /* Convert to a cygwin pathname */ + static_assert (sizeof (info->dli_fname) == PATH_MAX); ssize_t conv = cygwin_conv_path (CCP_WIN_W_TO_POSIX | CCP_ABSOLUTE, fname, - info->dli_fname, MAX_PATH); + info->dli_fname, PATH_MAX); if (conv) return 0; |