@@ -29,6 +29,7 @@ const (
29
29
//go:cgo_import_dynamic runtime._GetProcessAffinityMask GetProcessAffinityMask%3 "kernel32.dll"
30
30
//go:cgo_import_dynamic runtime._GetQueuedCompletionStatus GetQueuedCompletionStatus%5 "kernel32.dll"
31
31
//go:cgo_import_dynamic runtime._GetStdHandle GetStdHandle%1 "kernel32.dll"
32
+ //go:cgo_import_dynamic runtime._GetSystemDirectoryA GetSystemDirectoryA%2 "kernel32.dll"
32
33
//go:cgo_import_dynamic runtime._GetSystemInfo GetSystemInfo%1 "kernel32.dll"
33
34
//go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext%2 "kernel32.dll"
34
35
//go:cgo_import_dynamic runtime._LoadLibraryW LoadLibraryW%1 "kernel32.dll"
@@ -47,12 +48,9 @@ const (
47
48
//go:cgo_import_dynamic runtime._VirtualAlloc VirtualAlloc%4 "kernel32.dll"
48
49
//go:cgo_import_dynamic runtime._VirtualFree VirtualFree%3 "kernel32.dll"
49
50
//go:cgo_import_dynamic runtime._VirtualQuery VirtualQuery%3 "kernel32.dll"
50
- //go:cgo_import_dynamic runtime._WSAGetOverlappedResult WSAGetOverlappedResult%5 "ws2_32.dll"
51
51
//go:cgo_import_dynamic runtime._WaitForSingleObject WaitForSingleObject%2 "kernel32.dll"
52
52
//go:cgo_import_dynamic runtime._WriteConsoleW WriteConsoleW%5 "kernel32.dll"
53
53
//go:cgo_import_dynamic runtime._WriteFile WriteFile%5 "kernel32.dll"
54
- //go:cgo_import_dynamic runtime._timeBeginPeriod timeBeginPeriod%1 "winmm.dll"
55
- //go:cgo_import_dynamic runtime._timeEndPeriod timeEndPeriod%1 "winmm.dll"
56
54
57
55
type stdFunction unsafe.Pointer
58
56
75
73
_GetProcessAffinityMask ,
76
74
_GetQueuedCompletionStatus ,
77
75
_GetStdHandle ,
76
+ _GetSystemDirectoryA ,
78
77
_GetSystemInfo ,
79
78
_GetSystemTimeAsFileTime ,
80
79
_GetThreadContext ,
@@ -96,19 +95,17 @@ var (
96
95
_VirtualAlloc ,
97
96
_VirtualFree ,
98
97
_VirtualQuery ,
99
- _WSAGetOverlappedResult ,
100
98
_WaitForSingleObject ,
101
99
_WriteConsoleW ,
102
100
_WriteFile ,
103
- _timeBeginPeriod ,
104
- _timeEndPeriod ,
105
101
_ stdFunction
106
102
107
103
// Following syscalls are only available on some Windows PCs.
108
104
// We will load syscalls, if available, before using them.
109
105
_AddDllDirectory ,
110
106
_AddVectoredContinueHandler ,
111
107
_GetQueuedCompletionStatusEx ,
108
+ _LoadLibraryExA ,
112
109
_LoadLibraryExW ,
113
110
_ stdFunction
114
111
@@ -126,6 +123,12 @@ var (
126
123
// links wrong printf function to cgo executable (see issue
127
124
// 12030 for details).
128
125
_NtWaitForSingleObject stdFunction
126
+
127
+ // These are from non-kernel32.dll, so we prefer to LoadLibraryEx them.
128
+ _timeBeginPeriod ,
129
+ _timeEndPeriod ,
130
+ _WSAGetOverlappedResult ,
131
+ _ stdFunction
129
132
)
130
133
131
134
// Function to be called by windows CreateThread
@@ -173,6 +176,26 @@ func windowsFindfunc(lib uintptr, name []byte) stdFunction {
173
176
return stdFunction (unsafe .Pointer (f ))
174
177
}
175
178
179
+ var sysDirectory [521 ]byte
180
+ var sysDirectoryLen uintptr
181
+
182
+ func windowsLoadSystemLib (name []byte ) uintptr {
183
+ if useLoadLibraryEx {
184
+ return stdcall3 (_LoadLibraryExA , uintptr (unsafe .Pointer (& name [0 ])), 0 , _LOAD_LIBRARY_SEARCH_SYSTEM32 )
185
+ } else {
186
+ if sysDirectoryLen == 0 {
187
+ l := stdcall2 (_GetSystemDirectoryA , uintptr (unsafe .Pointer (& sysDirectory [0 ])), uintptr (len (sysDirectory )- 1 ))
188
+ if l == 0 || l > uintptr (len (sysDirectory )- 1 ) {
189
+ throw ("Unable to determine system directory" )
190
+ }
191
+ sysDirectory [l ] = '\\'
192
+ sysDirectoryLen = l + 1
193
+ }
194
+ absName := append (sysDirectory [:sysDirectoryLen ], name ... )
195
+ return stdcall1 (_LoadLibraryA , uintptr (unsafe .Pointer (& absName [0 ])))
196
+ }
197
+ }
198
+
176
199
func loadOptionalSyscalls () {
177
200
var kernel32dll = []byte ("kernel32.dll\000 " )
178
201
k32 := stdcall1 (_LoadLibraryA , uintptr (unsafe .Pointer (& kernel32dll [0 ])))
@@ -182,17 +205,19 @@ func loadOptionalSyscalls() {
182
205
_AddDllDirectory = windowsFindfunc (k32 , []byte ("AddDllDirectory\000 " ))
183
206
_AddVectoredContinueHandler = windowsFindfunc (k32 , []byte ("AddVectoredContinueHandler\000 " ))
184
207
_GetQueuedCompletionStatusEx = windowsFindfunc (k32 , []byte ("GetQueuedCompletionStatusEx\000 " ))
208
+ _LoadLibraryExA = windowsFindfunc (k32 , []byte ("LoadLibraryExA\000 " ))
185
209
_LoadLibraryExW = windowsFindfunc (k32 , []byte ("LoadLibraryExW\000 " ))
210
+ useLoadLibraryEx = (_LoadLibraryExW != nil && _LoadLibraryExA != nil && _AddDllDirectory != nil )
186
211
187
212
var advapi32dll = []byte ("advapi32.dll\000 " )
188
- a32 := stdcall1 ( _LoadLibraryA , uintptr ( unsafe . Pointer ( & advapi32dll [ 0 ])) )
213
+ a32 := windowsLoadSystemLib ( advapi32dll )
189
214
if a32 == 0 {
190
215
throw ("advapi32.dll not found" )
191
216
}
192
217
_RtlGenRandom = windowsFindfunc (a32 , []byte ("SystemFunction036\000 " ))
193
218
194
219
var ntdll = []byte ("ntdll.dll\000 " )
195
- n32 := stdcall1 ( _LoadLibraryA , uintptr ( unsafe . Pointer ( & ntdll [ 0 ])) )
220
+ n32 := windowsLoadSystemLib ( ntdll )
196
221
if n32 == 0 {
197
222
throw ("ntdll.dll not found" )
198
223
}
@@ -205,6 +230,27 @@ func loadOptionalSyscalls() {
205
230
}
206
231
}
207
232
233
+ var winmmdll = []byte ("winmm.dll\000 " )
234
+ m32 := windowsLoadSystemLib (winmmdll )
235
+ if m32 == 0 {
236
+ throw ("winmm.dll not found" )
237
+ }
238
+ _timeBeginPeriod = windowsFindfunc (m32 , []byte ("timeBeginPeriod\000 " ))
239
+ _timeEndPeriod = windowsFindfunc (m32 , []byte ("timeEndPeriod\000 " ))
240
+ if _timeBeginPeriod == nil || _timeEndPeriod == nil {
241
+ throw ("timeBegin/EndPeriod not found" )
242
+ }
243
+
244
+ var ws232dll = []byte ("ws2_32.dll\000 " )
245
+ ws232 := windowsLoadSystemLib (ws232dll )
246
+ if ws232 == 0 {
247
+ throw ("ws2_32.dll not found" )
248
+ }
249
+ _WSAGetOverlappedResult = windowsFindfunc (ws232 , []byte ("WSAGetOverlappedResult\000 " ))
250
+ if _WSAGetOverlappedResult == nil {
251
+ throw ("WSAGetOverlappedResult not found" )
252
+ }
253
+
208
254
if windowsFindfunc (n32 , []byte ("wine_get_version\000 " )) != nil {
209
255
// running on Wine
210
256
initWine (k32 )
@@ -311,8 +357,6 @@ func osinit() {
311
357
312
358
loadOptionalSyscalls ()
313
359
314
- useLoadLibraryEx = (_LoadLibraryExW != nil && _AddDllDirectory != nil )
315
-
316
360
disableWER ()
317
361
318
362
initExceptionHandler ()
0 commit comments