Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/winecrt0/unix_lib.c
12343 views
1
/*
2
* Support for the Unix part of builtin dlls
3
*
4
* Copyright 2019 Alexandre Julliard
5
*
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
*/
20
21
#include <stdarg.h>
22
23
#include "ntstatus.h"
24
#include "windef.h"
25
#include "winbase.h"
26
#include "winternl.h"
27
#include "wine/unixlib.h"
28
29
static inline void *image_base(void)
30
{
31
#ifdef __WINE_PE_BUILD
32
extern IMAGE_DOS_HEADER __ImageBase;
33
return (void *)&__ImageBase;
34
#else
35
extern IMAGE_NT_HEADERS __wine_spec_nt_header;
36
return (void *)((__wine_spec_nt_header.OptionalHeader.ImageBase + 0xffff) & ~0xffff);
37
#endif
38
}
39
40
static NTSTATUS WINAPI unix_call_fallback( unixlib_handle_t handle, unsigned int code, void *args )
41
{
42
return STATUS_DLL_NOT_FOUND;
43
}
44
45
static inline void *get_dispatcher( const char *name )
46
{
47
UNICODE_STRING ntdll_name = RTL_CONSTANT_STRING( L"ntdll.dll" );
48
HMODULE module;
49
void **dispatcher;
50
51
LdrGetDllHandle( NULL, 0, &ntdll_name, &module );
52
dispatcher = RtlFindExportedRoutineByName( module, "__wine_unix_call_dispatcher" );
53
return dispatcher ? *dispatcher : (void *)unix_call_fallback;
54
}
55
56
static NTSTATUS WINAPI unix_call_init( unixlib_handle_t handle, unsigned int code, void *args )
57
{
58
InterlockedExchangePointer( (void **)&__wine_unix_call_dispatcher,
59
get_dispatcher( "__wine_unix_call_dispatcher" ));
60
return __wine_unix_call_dispatcher( handle, code, args );
61
}
62
63
unixlib_handle_t __wine_unixlib_handle = 0;
64
NTSTATUS (WINAPI *__wine_unix_call_dispatcher)( unixlib_handle_t, unsigned int, void * ) = unix_call_init;
65
66
#ifdef __arm64ec__
67
68
static NTSTATUS WINAPI unix_call_init_arm64ec( unixlib_handle_t handle, unsigned int code, void *args );
69
70
static __attribute__((used)) NTSTATUS (WINAPI *__wine_unix_call_dispatcher_arm64ec)( unixlib_handle_t, unsigned int, void * ) = unix_call_init_arm64ec;
71
72
static NTSTATUS WINAPI unix_call_init_arm64ec( unixlib_handle_t handle, unsigned int code, void *args )
73
{
74
InterlockedExchangePointer( (void **)&__wine_unix_call_dispatcher_arm64ec,
75
get_dispatcher( "__wine_unix_call_dispatcher_arm64ec" ));
76
return __wine_unix_call_arm64ec( handle, code, args );
77
}
78
79
NTSTATUS __attribute__((naked)) __wine_unix_call_arm64ec( unixlib_handle_t handle, unsigned int code, void *args )
80
{
81
asm( ".seh_proc \"#__wine_unix_call_arm64ec\"\n\t"
82
".seh_endprologue\n\t"
83
"adrp x16, __wine_unix_call_dispatcher_arm64ec\n\t"
84
"ldr x16, [x16, #:lo12:__wine_unix_call_dispatcher_arm64ec]\n\t"
85
"br x16\n\t"
86
".seh_endproc" );
87
}
88
89
#endif
90
91
NTSTATUS WINAPI __wine_init_unix_call(void)
92
{
93
return NtQueryVirtualMemory( GetCurrentProcess(), image_base(), MemoryWineLoadUnixLib,
94
&__wine_unixlib_handle, sizeof(__wine_unixlib_handle), NULL );
95
}
96
97
NTSTATUS WINAPI __wine_load_unix_lib( const UNICODE_STRING *name, unixlib_module_t *lib,
98
unixlib_handle_t *handle )
99
{
100
UINT64 res[2];
101
NTSTATUS status = NtQueryVirtualMemory( GetCurrentProcess(), name, MemoryWineLoadUnixLibByName,
102
res, handle ? sizeof(res) : sizeof(res[0]), NULL );
103
if (!status)
104
{
105
if (lib) *lib = res[0];
106
if (handle) *handle = res[1];
107
}
108
return status;
109
}
110
111
NTSTATUS WINAPI __wine_unload_unix_lib( unixlib_module_t lib )
112
{
113
return NtQueryVirtualMemory( GetCurrentProcess(), &lib, MemoryWineUnloadUnixLib, NULL, 0, NULL );
114
}
115
116