@@ -37,8 +37,8 @@ const (
37
37
//go:cgo_import_dynamic runtime._GetSystemInfo GetSystemInfo%1 "kernel32.dll"
38
38
//go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext%2 "kernel32.dll"
39
39
//go:cgo_import_dynamic runtime._SetThreadContext SetThreadContext%2 "kernel32.dll"
40
+ //go:cgo_import_dynamic runtime._LoadLibraryExW LoadLibraryExW%3 "kernel32.dll"
40
41
//go:cgo_import_dynamic runtime._LoadLibraryW LoadLibraryW%1 "kernel32.dll"
41
- //go:cgo_import_dynamic runtime._LoadLibraryA LoadLibraryA%1 "kernel32.dll"
42
42
//go:cgo_import_dynamic runtime._PostQueuedCompletionStatus PostQueuedCompletionStatus%4 "kernel32.dll"
43
43
//go:cgo_import_dynamic runtime._ResumeThread ResumeThread%1 "kernel32.dll"
44
44
//go:cgo_import_dynamic runtime._SetConsoleCtrlHandler SetConsoleCtrlHandler%2 "kernel32.dll"
88
88
_GetSystemTimeAsFileTime ,
89
89
_GetThreadContext ,
90
90
_SetThreadContext ,
91
+ _LoadLibraryExW ,
91
92
_LoadLibraryW ,
92
- _LoadLibraryA ,
93
93
_PostQueuedCompletionStatus ,
94
94
_QueryPerformanceCounter ,
95
95
_QueryPerformanceFrequency ,
@@ -116,10 +116,7 @@ var (
116
116
117
117
// Following syscalls are only available on some Windows PCs.
118
118
// We will load syscalls, if available, before using them.
119
- _AddDllDirectory ,
120
119
_AddVectoredContinueHandler ,
121
- _LoadLibraryExA ,
122
- _LoadLibraryExW ,
123
120
_ stdFunction
124
121
125
122
// Use RtlGenRandom to generate cryptographically random data.
@@ -146,6 +143,15 @@ var (
146
143
_ stdFunction
147
144
)
148
145
146
+ var (
147
+ advapi32dll = [... ]uint16 {'a' , 'd' , 'v' , 'a' , 'p' , 'i' , '3' , '2' , '.' , 'd' , 'l' , 'l' , 0 }
148
+ kernel32dll = [... ]uint16 {'k' , 'e' , 'r' , 'n' , 'e' , 'l' , '3' , '2' , '.' , 'd' , 'l' , 'l' , 0 }
149
+ ntdlldll = [... ]uint16 {'n' , 't' , 'd' , 'l' , 'l' , '.' , 'd' , 'l' , 'l' , 0 }
150
+ powrprofdll = [... ]uint16 {'p' , 'o' , 'w' , 'r' , 'p' , 'r' , 'o' , 'f' , '.' , 'd' , 'l' , 'l' , 0 }
151
+ winmmdll = [... ]uint16 {'w' , 'i' , 'n' , 'm' , 'm' , '.' , 'd' , 'l' , 'l' , 0 }
152
+ ws2_32dll = [... ]uint16 {'w' , 's' , '2' , '_' , '3' , '2' , '.' , 'd' , 'l' , 'l' , 0 }
153
+ )
154
+
149
155
// Function to be called by windows CreateThread
150
156
// to start new os thread.
151
157
func tstart_stdcall (newm * m )
@@ -225,46 +231,35 @@ const _MAX_PATH = 260 // https://docs.microsoft.com/en-us/windows/win32/fileio/m
225
231
var sysDirectory [_MAX_PATH + 1 ]byte
226
232
var sysDirectoryLen uintptr
227
233
228
- func windowsLoadSystemLib (name []byte ) uintptr {
229
- if sysDirectoryLen == 0 {
230
- l := stdcall2 (_GetSystemDirectoryA , uintptr (unsafe .Pointer (& sysDirectory [0 ])), uintptr (len (sysDirectory )- 1 ))
231
- if l == 0 || l > uintptr (len (sysDirectory )- 1 ) {
232
- throw ("Unable to determine system directory" )
233
- }
234
- sysDirectory [l ] = '\\'
235
- sysDirectoryLen = l + 1
236
- }
237
- if useLoadLibraryEx {
238
- return stdcall3 (_LoadLibraryExA , uintptr (unsafe .Pointer (& name [0 ])), 0 , _LOAD_LIBRARY_SEARCH_SYSTEM32 )
239
- } else {
240
- absName := append (sysDirectory [:sysDirectoryLen ], name ... )
241
- return stdcall1 (_LoadLibraryA , uintptr (unsafe .Pointer (& absName [0 ])))
234
+ func initSysDirectory () {
235
+ l := stdcall2 (_GetSystemDirectoryA , uintptr (unsafe .Pointer (& sysDirectory [0 ])), uintptr (len (sysDirectory )- 1 ))
236
+ if l == 0 || l > uintptr (len (sysDirectory )- 1 ) {
237
+ throw ("Unable to determine system directory" )
242
238
}
239
+ sysDirectory [l ] = '\\'
240
+ sysDirectoryLen = l + 1
241
+ }
242
+
243
+ func windowsLoadSystemLib (name []uint16 ) uintptr {
244
+ return stdcall3 (_LoadLibraryExW , uintptr (unsafe .Pointer (& name [0 ])), 0 , _LOAD_LIBRARY_SEARCH_SYSTEM32 )
243
245
}
244
246
245
247
const haveCputicksAsm = GOARCH == "386" || GOARCH == "amd64"
246
248
247
249
func loadOptionalSyscalls () {
248
- var kernel32dll = []byte ("kernel32.dll\000 " )
249
- k32 := stdcall1 (_LoadLibraryA , uintptr (unsafe .Pointer (& kernel32dll [0 ])))
250
+ k32 := windowsLoadSystemLib (kernel32dll [:])
250
251
if k32 == 0 {
251
252
throw ("kernel32.dll not found" )
252
253
}
253
- _AddDllDirectory = windowsFindfunc (k32 , []byte ("AddDllDirectory\000 " ))
254
254
_AddVectoredContinueHandler = windowsFindfunc (k32 , []byte ("AddVectoredContinueHandler\000 " ))
255
- _LoadLibraryExA = windowsFindfunc (k32 , []byte ("LoadLibraryExA\000 " ))
256
- _LoadLibraryExW = windowsFindfunc (k32 , []byte ("LoadLibraryExW\000 " ))
257
- useLoadLibraryEx = (_LoadLibraryExW != nil && _LoadLibraryExA != nil && _AddDllDirectory != nil )
258
255
259
- var advapi32dll = []byte ("advapi32.dll\000 " )
260
- a32 := windowsLoadSystemLib (advapi32dll )
256
+ a32 := windowsLoadSystemLib (advapi32dll [:])
261
257
if a32 == 0 {
262
258
throw ("advapi32.dll not found" )
263
259
}
264
260
_RtlGenRandom = windowsFindfunc (a32 , []byte ("SystemFunction036\000 " ))
265
261
266
- var ntdll = []byte ("ntdll.dll\000 " )
267
- n32 := windowsLoadSystemLib (ntdll )
262
+ n32 := windowsLoadSystemLib (ntdlldll [:])
268
263
if n32 == 0 {
269
264
throw ("ntdll.dll not found" )
270
265
}
@@ -279,8 +274,7 @@ func loadOptionalSyscalls() {
279
274
}
280
275
}
281
276
282
- var winmmdll = []byte ("winmm.dll\000 " )
283
- m32 := windowsLoadSystemLib (winmmdll )
277
+ m32 := windowsLoadSystemLib (winmmdll [:])
284
278
if m32 == 0 {
285
279
throw ("winmm.dll not found" )
286
280
}
@@ -290,8 +284,7 @@ func loadOptionalSyscalls() {
290
284
throw ("timeBegin/EndPeriod not found" )
291
285
}
292
286
293
- var ws232dll = []byte ("ws2_32.dll\000 " )
294
- ws232 := windowsLoadSystemLib (ws232dll )
287
+ ws232 := windowsLoadSystemLib (ws2_32dll [:])
295
288
if ws232 == 0 {
296
289
throw ("ws2_32.dll not found" )
297
290
}
@@ -315,7 +308,7 @@ func monitorSuspendResume() {
315
308
context uintptr
316
309
}
317
310
318
- powrprof := windowsLoadSystemLib ([] byte ( "powrprof.dll \000 " ) )
311
+ powrprof := windowsLoadSystemLib (powrprofdll [:] )
319
312
if powrprof == 0 {
320
313
return // Running on Windows 7, where we don't need it anyway.
321
314
}
@@ -389,22 +382,6 @@ const (
389
382
// in sys_windows_386.s and sys_windows_amd64.s:
390
383
func getlasterror () uint32
391
384
392
- // When loading DLLs, we prefer to use LoadLibraryEx with
393
- // LOAD_LIBRARY_SEARCH_* flags, if available. LoadLibraryEx is not
394
- // available on old Windows, though, and the LOAD_LIBRARY_SEARCH_*
395
- // flags are not available on some versions of Windows without a
396
- // security patch.
397
- //
398
- // https://msdn.microsoft.com/en-us/library/ms684179(v=vs.85).aspx says:
399
- // "Windows 7, Windows Server 2008 R2, Windows Vista, and Windows
400
- // Server 2008: The LOAD_LIBRARY_SEARCH_* flags are available on
401
- // systems that have KB2533623 installed. To determine whether the
402
- // flags are available, use GetProcAddress to get the address of the
403
- // AddDllDirectory, RemoveDllDirectory, or SetDefaultDllDirectories
404
- // function. If GetProcAddress succeeds, the LOAD_LIBRARY_SEARCH_*
405
- // flags can be used with LoadLibraryEx."
406
- var useLoadLibraryEx bool
407
-
408
385
var timeBeginPeriodRetValue uint32
409
386
410
387
// osRelaxMinNS indicates that sysmon shouldn't osRelax if the next
@@ -555,6 +532,7 @@ func osinit() {
555
532
initHighResTimer ()
556
533
timeBeginPeriodRetValue = osRelax (false )
557
534
535
+ initSysDirectory ()
558
536
initLongPathSupport ()
559
537
560
538
ncpu = getproccount ()
0 commit comments