#include <stdarg.h>
#include "ntstatus.h"
#include "windef.h"
#include "winbase.h"
#include "winternl.h"
#include "wine/unixlib.h"
static inline void *image_base(void)
{
#ifdef __WINE_PE_BUILD
extern IMAGE_DOS_HEADER __ImageBase;
return (void *)&__ImageBase;
#else
extern IMAGE_NT_HEADERS __wine_spec_nt_header;
return (void *)((__wine_spec_nt_header.OptionalHeader.ImageBase + 0xffff) & ~0xffff);
#endif
}
static NTSTATUS WINAPI unix_call_fallback( unixlib_handle_t handle, unsigned int code, void *args )
{
return STATUS_DLL_NOT_FOUND;
}
static inline void *get_dispatcher( const char *name )
{
UNICODE_STRING ntdll_name = RTL_CONSTANT_STRING( L"ntdll.dll" );
HMODULE module;
void **dispatcher;
LdrGetDllHandle( NULL, 0, &ntdll_name, &module );
dispatcher = RtlFindExportedRoutineByName( module, "__wine_unix_call_dispatcher" );
return dispatcher ? *dispatcher : (void *)unix_call_fallback;
}
static NTSTATUS WINAPI unix_call_init( unixlib_handle_t handle, unsigned int code, void *args )
{
InterlockedExchangePointer( (void **)&__wine_unix_call_dispatcher,
get_dispatcher( "__wine_unix_call_dispatcher" ));
return __wine_unix_call_dispatcher( handle, code, args );
}
unixlib_handle_t __wine_unixlib_handle = 0;
NTSTATUS (WINAPI *__wine_unix_call_dispatcher)( unixlib_handle_t, unsigned int, void * ) = unix_call_init;
#ifdef __arm64ec__
static NTSTATUS WINAPI unix_call_init_arm64ec( unixlib_handle_t handle, unsigned int code, void *args );
static __attribute__((used)) NTSTATUS (WINAPI *__wine_unix_call_dispatcher_arm64ec)( unixlib_handle_t, unsigned int, void * ) = unix_call_init_arm64ec;
static NTSTATUS WINAPI unix_call_init_arm64ec( unixlib_handle_t handle, unsigned int code, void *args )
{
InterlockedExchangePointer( (void **)&__wine_unix_call_dispatcher_arm64ec,
get_dispatcher( "__wine_unix_call_dispatcher_arm64ec" ));
return __wine_unix_call_arm64ec( handle, code, args );
}
NTSTATUS __attribute__((naked)) __wine_unix_call_arm64ec( unixlib_handle_t handle, unsigned int code, void *args )
{
asm( ".seh_proc \"#__wine_unix_call_arm64ec\"\n\t"
".seh_endprologue\n\t"
"adrp x16, __wine_unix_call_dispatcher_arm64ec\n\t"
"ldr x16, [x16, #:lo12:__wine_unix_call_dispatcher_arm64ec]\n\t"
"br x16\n\t"
".seh_endproc" );
}
#endif
NTSTATUS WINAPI __wine_init_unix_call(void)
{
return NtQueryVirtualMemory( GetCurrentProcess(), image_base(), MemoryWineLoadUnixLib,
&__wine_unixlib_handle, sizeof(__wine_unixlib_handle), NULL );
}
NTSTATUS WINAPI __wine_load_unix_lib( const UNICODE_STRING *name, unixlib_module_t *lib,
unixlib_handle_t *handle )
{
UINT64 res[2];
NTSTATUS status = NtQueryVirtualMemory( GetCurrentProcess(), name, MemoryWineLoadUnixLibByName,
res, handle ? sizeof(res) : sizeof(res[0]), NULL );
if (!status)
{
if (lib) *lib = res[0];
if (handle) *handle = res[1];
}
return status;
}
NTSTATUS WINAPI __wine_unload_unix_lib( unixlib_module_t lib )
{
return NtQueryVirtualMemory( GetCurrentProcess(), &lib, MemoryWineUnloadUnixLib, NULL, 0, NULL );
}