+/* On certain versions of Windows, the information about ntdll.dll
+ is not available yet at the time we get the LOAD_DLL_DEBUG_EVENT,
+ thus preventing us from reporting this DLL as an SO. This has been
+ witnessed on Windows 8.1, for instance. A possible explanation
+ is that ntdll.dll might be mapped before the SO info gets created
+ by the Windows system -- ntdll.dll is the first DLL to be reported
+ via LOAD_DLL_DEBUG_EVENT and other DLLs do not seem to suffer from
+ that problem.
+
+ If we indeed are missing ntdll.dll, this function tries to recover
+ from this issue, after the fact. Do nothing if we encounter any
+ issue trying to locate that DLL. */
+
+static void
+windows_ensure_ntdll_loaded (void)
+{
+ struct so_list *so;
+ HMODULE dummy_hmodule;
+ DWORD cb_needed;
+ HMODULE *hmodules;
+ int i;
+
+ for (so = solib_start.next; so != NULL; so = so->next)
+ if (FILENAME_CMP (lbasename (so->so_name), "ntdll.dll") == 0)
+ return; /* ntdll.dll already loaded, nothing to do. */
+
+ if (EnumProcessModules (current_process_handle, &dummy_hmodule,
+ sizeof (HMODULE), &cb_needed) == 0)
+ return;
+
+ if (cb_needed < 1)
+ return;
+
+ hmodules = (HMODULE *) alloca (cb_needed);
+ if (EnumProcessModules (current_process_handle, hmodules,
+ cb_needed, &cb_needed) == 0)
+ return;
+
+ for (i = 0; i < (int) (cb_needed / sizeof (HMODULE)); i++)
+ {
+ MODULEINFO mi;
+#ifdef __USEWIDE
+ wchar_t dll_name[__PMAX];
+ char name[__PMAX];
+#else
+ char dll_name[__PMAX];
+ char *name;
+#endif
+ if (GetModuleInformation (current_process_handle, hmodules[i],
+ &mi, sizeof (mi)) == 0)
+ continue;
+ if (GetModuleFileNameEx (current_process_handle, hmodules[i],
+ dll_name, sizeof (dll_name)) == 0)
+ continue;
+#ifdef __USEWIDE
+ wcstombs (name, dll_name, __PMAX);
+#else
+ name = dll_name;
+#endif
+ if (FILENAME_CMP (lbasename (name), "ntdll.dll") == 0)
+ {
+ solib_end->next = windows_make_so (name, mi.lpBaseOfDll);
+ solib_end = solib_end->next;
+ return;
+ }
+ }
+}
+