From 05d00276c627753487c571c30fddfc56c02ad37e Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Mon, 12 Aug 2019 12:46:39 +0200 Subject: [PATCH] kernel32: Move some module functions to kernelbase. Signed-off-by: Alexandre Julliard --- dlls/kernel32/kernel32.spec | 14 +-- dlls/kernel32/module.c | 215 -------------------------------- dlls/kernelbase/kernelbase.spec | 14 +-- dlls/kernelbase/loader.c | 174 +++++++++++++++++++++++++- 4 files changed, 186 insertions(+), 231 deletions(-) diff --git a/dlls/kernel32/kernel32.spec b/dlls/kernel32/kernel32.spec index be63d8077ec..2ca581d19a0 100644 --- a/dlls/kernel32/kernel32.spec +++ b/dlls/kernel32/kernel32.spec @@ -377,7 +377,7 @@ @ stdcall DeleteVolumeMountPointW(wstr) @ stdcall -arch=x86_64 DequeueUmsCompletionListItems(ptr long ptr) @ stdcall DeviceIoControl(long long ptr long ptr long ptr ptr) -@ stdcall DisableThreadLibraryCalls(long) +@ stdcall -import DisableThreadLibraryCalls(long) @ stdcall -import DisconnectNamedPipe(long) @ stdcall DnsHostnameToComputerNameA (str ptr ptr) @ stdcall DnsHostnameToComputerNameW (wstr ptr ptr) @@ -718,12 +718,12 @@ @ stdcall GetMailslotInfo(long ptr ptr ptr ptr) @ stdcall GetMaximumProcessorCount(long) # @ stub GetMaximumProcessorGroupCount -@ stdcall GetModuleFileNameA(long ptr long) -@ stdcall GetModuleFileNameW(long ptr long) -@ stdcall GetModuleHandleA(str) -@ stdcall GetModuleHandleExA(long ptr ptr) -@ stdcall GetModuleHandleExW(long ptr ptr) -@ stdcall GetModuleHandleW(wstr) +@ stdcall -import GetModuleFileNameA(long ptr long) +@ stdcall -import GetModuleFileNameW(long ptr long) +@ stdcall -import GetModuleHandleA(str) +@ stdcall -import GetModuleHandleExA(long ptr ptr) +@ stdcall -import GetModuleHandleExW(long ptr ptr) +@ stdcall -import GetModuleHandleW(wstr) # @ stub GetNamedPipeAttribute # @ stub GetNamedPipeClientComputerNameA # @ stub GetNamedPipeClientComputerNameW diff --git a/dlls/kernel32/module.c b/dlls/kernel32/module.c index 386a7c22399..acfc365200f 100644 --- a/dlls/kernel32/module.c +++ b/dlls/kernel32/module.c @@ -233,33 +233,6 @@ BOOL WINAPI SetDefaultDllDirectories( DWORD flags ) } -/**************************************************************************** - * DisableThreadLibraryCalls (KERNEL32.@) - * - * Inform the module loader that thread notifications are not required for a dll. - * - * PARAMS - * hModule [I] Module handle to skip calls for - * - * RETURNS - * Success: TRUE. Thread attach and detach notifications will not be sent - * to hModule. - * Failure: FALSE. Use GetLastError() to determine the cause. - * - * NOTES - * This is typically called from the dll entry point of a dll during process - * attachment, for dlls that do not need to process thread notifications. - */ -BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule ) -{ - NTSTATUS nts = LdrDisableThreadCalloutsForDll( hModule ); - if (nts == STATUS_SUCCESS) return TRUE; - - SetLastError( RtlNtStatusToDosError( nts ) ); - return FALSE; -} - - /*********************************************************************** * GetBinaryTypeW [KERNEL32.@] * @@ -402,194 +375,6 @@ BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType ) return FALSE; } -/*********************************************************************** - * GetModuleHandleExA (KERNEL32.@) - */ -BOOL WINAPI GetModuleHandleExA( DWORD flags, LPCSTR name, HMODULE *module ) -{ - WCHAR *nameW; - - if (!name || (flags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)) - return GetModuleHandleExW( flags, (LPCWSTR)name, module ); - - if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE; - return GetModuleHandleExW( flags, nameW, module ); -} - -/*********************************************************************** - * GetModuleHandleExW (KERNEL32.@) - */ -BOOL WINAPI GetModuleHandleExW( DWORD flags, LPCWSTR name, HMODULE *module ) -{ - NTSTATUS status = STATUS_SUCCESS; - HMODULE ret; - ULONG_PTR magic; - BOOL lock; - - if (!module) - { - SetLastError( ERROR_INVALID_PARAMETER ); - return FALSE; - } - - /* if we are messing with the refcount, grab the loader lock */ - lock = (flags & GET_MODULE_HANDLE_EX_FLAG_PIN) || !(flags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT); - if (lock) - LdrLockLoaderLock( 0, NULL, &magic ); - - if (!name) - { - ret = NtCurrentTeb()->Peb->ImageBaseAddress; - } - else if (flags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS) - { - void *dummy; - if (!(ret = RtlPcToFileHeader( (void *)name, &dummy ))) status = STATUS_DLL_NOT_FOUND; - } - else - { - UNICODE_STRING wstr; - RtlInitUnicodeString( &wstr, name ); - status = LdrGetDllHandle( NULL, 0, &wstr, &ret ); - } - - if (status == STATUS_SUCCESS) - { - if (flags & GET_MODULE_HANDLE_EX_FLAG_PIN) - LdrAddRefDll( LDR_ADDREF_DLL_PIN, ret ); - else if (!(flags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT)) - LdrAddRefDll( 0, ret ); - } - else SetLastError( RtlNtStatusToDosError( status ) ); - - if (lock) - LdrUnlockLoaderLock( 0, magic ); - - if (status == STATUS_SUCCESS) *module = ret; - else *module = NULL; - - return (status == STATUS_SUCCESS); -} - -/*********************************************************************** - * GetModuleHandleA (KERNEL32.@) - * - * Get the handle of a dll loaded into the process address space. - * - * PARAMS - * module [I] Name of the dll - * - * RETURNS - * Success: A handle to the loaded dll. - * Failure: A NULL handle. Use GetLastError() to determine the cause. - */ -HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR module) -{ - HMODULE ret; - - GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, module, &ret ); - return ret; -} - -/*********************************************************************** - * GetModuleHandleW (KERNEL32.@) - * - * Unicode version of GetModuleHandleA. - */ -HMODULE WINAPI GetModuleHandleW(LPCWSTR module) -{ - HMODULE ret; - - GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, module, &ret ); - return ret; -} - - -/*********************************************************************** - * GetModuleFileNameA (KERNEL32.@) - * - * Get the file name of a loaded module from its handle. - * - * RETURNS - * Success: The length of the file name, excluding the terminating NUL. - * Failure: 0. Use GetLastError() to determine the cause. - * - * NOTES - * This function always returns the long path of hModule - * The function doesn't write a terminating '\0' if the buffer is too - * small. - */ -DWORD WINAPI GetModuleFileNameA( - HMODULE hModule, /* [in] Module handle (32 bit) */ - LPSTR lpFileName, /* [out] Destination for file name */ - DWORD size ) /* [in] Size of lpFileName in characters */ -{ - LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ); - DWORD len; - - if (!filenameW) - { - SetLastError( ERROR_NOT_ENOUGH_MEMORY ); - return 0; - } - if ((len = GetModuleFileNameW( hModule, filenameW, size ))) - { - len = FILE_name_WtoA( filenameW, len, lpFileName, size ); - if (len < size) - lpFileName[len] = '\0'; - else - SetLastError( ERROR_INSUFFICIENT_BUFFER ); - } - HeapFree( GetProcessHeap(), 0, filenameW ); - return len; -} - -/*********************************************************************** - * GetModuleFileNameW (KERNEL32.@) - * - * Unicode version of GetModuleFileNameA. - */ -DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size ) -{ - ULONG len = 0; - ULONG_PTR magic; - LDR_MODULE *pldr; - NTSTATUS nts; - WIN16_SUBSYSTEM_TIB *win16_tib; - - if (!hModule && ((win16_tib = NtCurrentTeb()->Tib.SubSystemTib)) && win16_tib->exe_name) - { - len = min(size, win16_tib->exe_name->Length / sizeof(WCHAR)); - memcpy( lpFileName, win16_tib->exe_name->Buffer, len * sizeof(WCHAR) ); - if (len < size) lpFileName[len] = '\0'; - goto done; - } - - LdrLockLoaderLock( 0, NULL, &magic ); - - if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress; - nts = LdrFindEntryForAddress( hModule, &pldr ); - if (nts == STATUS_SUCCESS) - { - len = min(size, pldr->FullDllName.Length / sizeof(WCHAR)); - memcpy(lpFileName, pldr->FullDllName.Buffer, len * sizeof(WCHAR)); - if (len < size) - { - lpFileName[len] = '\0'; - SetLastError( 0 ); - } - else - SetLastError( ERROR_INSUFFICIENT_BUFFER ); - } - else SetLastError( RtlNtStatusToDosError( nts ) ); - - LdrUnlockLoaderLock( 0, magic ); -done: - TRACE( "%s\n", debugstr_wn(lpFileName, len) ); - return len; -} - - /*********************************************************************** * get_dll_system_path */ diff --git a/dlls/kernelbase/kernelbase.spec b/dlls/kernelbase/kernelbase.spec index 384989a548a..9744b06e904 100644 --- a/dlls/kernelbase/kernelbase.spec +++ b/dlls/kernelbase/kernelbase.spec @@ -263,7 +263,7 @@ @ stdcall DestroyPrivateObjectSecurity(ptr) @ stdcall DeviceIoControl(long long ptr long ptr long ptr ptr) kernel32.DeviceIoControl @ stdcall DisablePredefinedHandleTableInternal(long) -@ stdcall DisableThreadLibraryCalls(long) kernel32.DisableThreadLibraryCalls +@ stdcall DisableThreadLibraryCalls(long) @ stdcall DisassociateCurrentThreadFromCallback(ptr) ntdll.TpDisassociateCallback # @ stub DiscardVirtualMemory @ stdcall DisconnectNamedPipe(long) @@ -556,14 +556,14 @@ # @ stub GetMemoryErrorHandlingCapabilities # @ stub GetModuleBaseNameA # @ stub GetModuleBaseNameW -@ stdcall GetModuleFileNameA(long ptr long) kernel32.GetModuleFileNameA +@ stdcall GetModuleFileNameA(long ptr long) # @ stub GetModuleFileNameExA # @ stub GetModuleFileNameExW -@ stdcall GetModuleFileNameW(long ptr long) kernel32.GetModuleFileNameW -@ stdcall GetModuleHandleA(str) kernel32.GetModuleHandleA -@ stdcall GetModuleHandleExA(long ptr ptr) kernel32.GetModuleHandleExA -@ stdcall GetModuleHandleExW(long ptr ptr) kernel32.GetModuleHandleExW -@ stdcall GetModuleHandleW(wstr) kernel32.GetModuleHandleW +@ stdcall GetModuleFileNameW(long ptr long) +@ stdcall GetModuleHandleA(str) +@ stdcall GetModuleHandleExA(long ptr ptr) +@ stdcall GetModuleHandleExW(long ptr ptr) +@ stdcall GetModuleHandleW(wstr) # @ stub GetModuleInformation @ stub GetNLSVersion @ stub GetNLSVersionEx diff --git a/dlls/kernelbase/loader.c b/dlls/kernelbase/loader.c index 91daa4ec14c..34c8add910d 100644 --- a/dlls/kernelbase/loader.c +++ b/dlls/kernelbase/loader.c @@ -37,6 +37,176 @@ WINE_DEFAULT_DEBUG_CHANNEL(module); +/*********************************************************************** + * Modules + ***********************************************************************/ + + +/**************************************************************************** + * DisableThreadLibraryCalls (kernelbase.@) + */ +BOOL WINAPI DECLSPEC_HOTPATCH DisableThreadLibraryCalls( HMODULE module ) +{ + return set_ntstatus( LdrDisableThreadCalloutsForDll( module )); +} + + +/*********************************************************************** + * GetModuleFileNameA (kernelbase.@) + */ +DWORD WINAPI DECLSPEC_HOTPATCH GetModuleFileNameA( HMODULE module, LPSTR filename, DWORD size ) +{ + LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ); + DWORD len; + + if (!filenameW) + { + SetLastError( ERROR_NOT_ENOUGH_MEMORY ); + return 0; + } + if ((len = GetModuleFileNameW( module, filenameW, size ))) + { + len = file_name_WtoA( filenameW, len, filename, size ); + if (len < size) + filename[len] = 0; + else + SetLastError( ERROR_INSUFFICIENT_BUFFER ); + } + HeapFree( GetProcessHeap(), 0, filenameW ); + return len; +} + + +/*********************************************************************** + * GetModuleFileNameW (kernelbase.@) + */ +DWORD WINAPI DECLSPEC_HOTPATCH GetModuleFileNameW( HMODULE module, LPWSTR filename, DWORD size ) +{ + ULONG len = 0; + ULONG_PTR magic; + LDR_MODULE *pldr; + WIN16_SUBSYSTEM_TIB *win16_tib; + + if (!module && ((win16_tib = NtCurrentTeb()->Tib.SubSystemTib)) && win16_tib->exe_name) + { + len = min( size, win16_tib->exe_name->Length / sizeof(WCHAR) ); + memcpy( filename, win16_tib->exe_name->Buffer, len * sizeof(WCHAR) ); + if (len < size) filename[len] = 0; + goto done; + } + + LdrLockLoaderLock( 0, NULL, &magic ); + + if (!module) module = NtCurrentTeb()->Peb->ImageBaseAddress; + if (set_ntstatus( LdrFindEntryForAddress( module, &pldr ))) + { + len = min( size, pldr->FullDllName.Length / sizeof(WCHAR) ); + memcpy( filename, pldr->FullDllName.Buffer, len * sizeof(WCHAR) ); + if (len < size) + { + filename[len] = 0; + SetLastError( 0 ); + } + else SetLastError( ERROR_INSUFFICIENT_BUFFER ); + } + + LdrUnlockLoaderLock( 0, magic ); +done: + TRACE( "%s\n", debugstr_wn(filename, len) ); + return len; +} + + +/*********************************************************************** + * GetModuleHandleA (kernelbase.@) + */ +HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA( LPCSTR module ) +{ + HMODULE ret; + + GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, module, &ret ); + return ret; +} + + +/*********************************************************************** + * GetModuleHandleW (kernelbase.@) + */ +HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleW( LPCWSTR module ) +{ + HMODULE ret; + + GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, module, &ret ); + return ret; +} + + +/*********************************************************************** + * GetModuleHandleExA (kernelbase.@) + */ +BOOL WINAPI DECLSPEC_HOTPATCH GetModuleHandleExA( DWORD flags, LPCSTR name, HMODULE *module ) +{ + WCHAR *nameW; + + if (!name || (flags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)) + return GetModuleHandleExW( flags, (LPCWSTR)name, module ); + + if (!(nameW = file_name_AtoW( name, FALSE ))) return FALSE; + return GetModuleHandleExW( flags, nameW, module ); +} + + +/*********************************************************************** + * GetModuleHandleExW (kernelbase.@) + */ +BOOL WINAPI DECLSPEC_HOTPATCH GetModuleHandleExW( DWORD flags, LPCWSTR name, HMODULE *module ) +{ + NTSTATUS status = STATUS_SUCCESS; + HMODULE ret = NULL; + ULONG_PTR magic; + BOOL lock; + + if (!module) + { + SetLastError( ERROR_INVALID_PARAMETER ); + return FALSE; + } + + /* if we are messing with the refcount, grab the loader lock */ + lock = (flags & GET_MODULE_HANDLE_EX_FLAG_PIN) || !(flags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT); + if (lock) LdrLockLoaderLock( 0, NULL, &magic ); + + if (!name) + { + ret = NtCurrentTeb()->Peb->ImageBaseAddress; + } + else if (flags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS) + { + void *dummy; + if (!(ret = RtlPcToFileHeader( (void *)name, &dummy ))) status = STATUS_DLL_NOT_FOUND; + } + else + { + UNICODE_STRING wstr; + RtlInitUnicodeString( &wstr, name ); + status = LdrGetDllHandle( NULL, 0, &wstr, &ret ); + } + + if (status == STATUS_SUCCESS) + { + if (flags & GET_MODULE_HANDLE_EX_FLAG_PIN) + LdrAddRefDll( LDR_ADDREF_DLL_PIN, ret ); + else if (!(flags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT)) + LdrAddRefDll( 0, ret ); + } + + if (lock) LdrUnlockLoaderLock( 0, magic ); + + *module = ret; + return set_ntstatus( status ); +} + + /*********************************************************************** * Resources ***********************************************************************/ @@ -89,7 +259,7 @@ static NTSTATUS get_res_nameW( LPCWSTR name, UNICODE_STRING *str ) /********************************************************************** - * EnumResourceLanguagesExA (KERNEL32.@) + * EnumResourceLanguagesExA (kernelbase.@) */ BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceLanguagesExA( HMODULE module, LPCSTR type, LPCSTR name, ENUMRESLANGPROCA func, LONG_PTR param, @@ -149,7 +319,7 @@ done: /********************************************************************** - * EnumResourceLanguagesExW (KERNEL32.@) + * EnumResourceLanguagesExW (kernelbase.@) */ BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceLanguagesExW( HMODULE module, LPCWSTR type, LPCWSTR name, ENUMRESLANGPROCW func, LONG_PTR param,