ITS#8324 More for Win32 NTDLL junk

Use GetProcAddress at runtime, avoid buildtime NTDLL link issues
This commit is contained in:
Howard Chu 2018-02-18 21:05:56 +00:00
parent 4f80c50730
commit b372ec4375
No known key found for this signature in database
GPG Key ID: FD2A70B44AB11BA7
3 changed files with 31 additions and 14 deletions

View File

@ -46,10 +46,6 @@ add_library(lmdb
target_link_libraries(lmdb target_link_libraries(lmdb
PRIVATE PRIVATE
${CMAKE_THREAD_LIBS_INIT}) ${CMAKE_THREAD_LIBS_INIT})
if(WIN32)
target_link_libraries(lmdb
-lntdll)
endif()
if(${ARCH_WIDTH} EQUAL 32) if(${ARCH_WIDTH} EQUAL 32)
target_compile_definitions(lmdb target_compile_definitions(lmdb
PUBLIC -DMDB_VL32) PUBLIC -DMDB_VL32)

View File

@ -24,8 +24,8 @@ W = -W -Wall -Wno-unused-parameter -Wbad-function-cast -Wuninitialized
THREADS = -pthread THREADS = -pthread
OPT = -O2 -g OPT = -O2 -g
CFLAGS = $(THREADS) $(OPT) $(W) $(XCFLAGS) CFLAGS = $(THREADS) $(OPT) $(W) $(XCFLAGS)
LDLIBS = # -lntdll # Windows needs ntdll LDLIBS =
SOLIBS = # -lntdll SOLIBS =
prefix = /usr/local prefix = /usr/local
exec_prefix = $(prefix) exec_prefix = $(prefix)
bindir = $(exec_prefix)/bin bindir = $(exec_prefix)/bin

View File

@ -47,29 +47,35 @@
* the full size. These APIs are defined in <wdm.h> and <ntifs.h> * the full size. These APIs are defined in <wdm.h> and <ntifs.h>
* but those headers are meant for driver-level development and * but those headers are meant for driver-level development and
* conflict with the regular user-level headers, so we explicitly * conflict with the regular user-level headers, so we explicitly
* declare them here. Using these APIs also means we must link to * declare them here. We get pointers to these functions from
* ntdll.dll, which is not linked by default in user code. * NTDLL.DLL at runtime, to avoid buildtime dependencies on any
* NTDLL import libraries.
*/ */
NTSTATUS WINAPI typedef NTSTATUS WINAPI (NtCreateSectionFunc)
NtCreateSection(OUT PHANDLE sh, IN ACCESS_MASK acc, (OUT PHANDLE sh, IN ACCESS_MASK acc,
IN void * oa OPTIONAL, IN void * oa OPTIONAL,
IN PLARGE_INTEGER ms OPTIONAL, IN PLARGE_INTEGER ms OPTIONAL,
IN ULONG pp, IN ULONG aa, IN HANDLE fh OPTIONAL); IN ULONG pp, IN ULONG aa, IN HANDLE fh OPTIONAL);
static NtCreateSectionFunc *NtCreateSection;
typedef enum _SECTION_INHERIT { typedef enum _SECTION_INHERIT {
ViewShare = 1, ViewShare = 1,
ViewUnmap = 2 ViewUnmap = 2
} SECTION_INHERIT; } SECTION_INHERIT;
NTSTATUS WINAPI typedef NTSTATUS WINAPI (NtMapViewOfSectionFunc)
NtMapViewOfSection(IN PHANDLE sh, IN HANDLE ph, (IN PHANDLE sh, IN HANDLE ph,
IN OUT PVOID *addr, IN ULONG_PTR zbits, IN OUT PVOID *addr, IN ULONG_PTR zbits,
IN SIZE_T cs, IN OUT PLARGE_INTEGER off OPTIONAL, IN SIZE_T cs, IN OUT PLARGE_INTEGER off OPTIONAL,
IN OUT PSIZE_T vs, IN SECTION_INHERIT ih, IN OUT PSIZE_T vs, IN SECTION_INHERIT ih,
IN ULONG at, IN ULONG pp); IN ULONG at, IN ULONG pp);
NTSTATUS WINAPI static NtMapViewOfSectionFunc *NtMapViewOfSection;
NtClose(HANDLE h);
typedef NTSTATUS WINAPI (NtCloseFunc)(HANDLE h);
static NtCloseFunc *NtClose;
/** getpid() returns int; MinGW defines pid_t but MinGW64 typedefs it /** getpid() returns int; MinGW defines pid_t but MinGW64 typedefs it
* as int64 which is wrong. MSVC doesn't define it at all, so just * as int64 which is wrong. MSVC doesn't define it at all, so just
@ -4397,6 +4403,21 @@ mdb_env_open2(MDB_env *env, int prev)
env->me_pidquery = MDB_PROCESS_QUERY_LIMITED_INFORMATION; env->me_pidquery = MDB_PROCESS_QUERY_LIMITED_INFORMATION;
else else
env->me_pidquery = PROCESS_QUERY_INFORMATION; env->me_pidquery = PROCESS_QUERY_INFORMATION;
/* Grab functions we need from NTDLL */
if (!NtCreateSection) {
HMODULE h = GetModuleHandle("NTDLL.DLL");
if (!h)
return MDB_PANIC;
NtClose = (NtCloseFunc *)GetProcAddress(h, "NtClose");
if (!NtClose)
return MDB_PANIC;
NtMapViewOfSection = (NtMapViewOfSectionFunc *)GetProcAddress(h, "NtMapViewOfSection");
if (!NtMapViewOfSection)
return MDB_PANIC;
NtCreateSection = (NtCreateSectionFunc *)GetProcAddress(h, "NtCreateSection");
if (!NtCreateSection)
return MDB_PANIC;
}
#endif /* _WIN32 */ #endif /* _WIN32 */
#ifdef BROKEN_FDATASYNC #ifdef BROKEN_FDATASYNC