Skip to content

Commit ef40549

Browse files
committed
runtime,syscall: move loadlibrary and getprocaddress to syscall
There is no need for loadlibrary, loadsystemlibrary and getprocaddress to be implemented in the runtime and linknamed from syscall. Change-Id: Icefd53a8e8f7012ed0c94c356be4179d5e45a01b Reviewed-on: https://go-review.googlesource.com/c/go/+/690516 Reviewed-by: Mark Freeman <mark@golang.org> Reviewed-by: Michael Pratt <mpratt@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 336931a commit ef40549

File tree

3 files changed

+44
-45
lines changed

3 files changed

+44
-45
lines changed

src/runtime/os_windows.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const (
4141
//go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext%2 "kernel32.dll"
4242
//go:cgo_import_dynamic runtime._SetThreadContext SetThreadContext%2 "kernel32.dll"
4343
//go:cgo_import_dynamic runtime._LoadLibraryExW LoadLibraryExW%3 "kernel32.dll"
44-
//go:cgo_import_dynamic runtime._LoadLibraryW LoadLibraryW%1 "kernel32.dll"
4544
//go:cgo_import_dynamic runtime._PostQueuedCompletionStatus PostQueuedCompletionStatus%4 "kernel32.dll"
4645
//go:cgo_import_dynamic runtime._QueryPerformanceCounter QueryPerformanceCounter%1 "kernel32.dll"
4746
//go:cgo_import_dynamic runtime._QueryPerformanceFrequency QueryPerformanceFrequency%1 "kernel32.dll"
@@ -99,7 +98,6 @@ var (
9998
_GetThreadContext,
10099
_SetThreadContext,
101100
_LoadLibraryExW,
102-
_LoadLibraryW,
103101
_PostQueuedCompletionStatus,
104102
_QueryPerformanceCounter,
105103
_QueryPerformanceFrequency,
@@ -245,6 +243,7 @@ func windows_GetSystemDirectory() string {
245243
}
246244

247245
func windowsLoadSystemLib(name []uint16) uintptr {
246+
const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
248247
return stdcall3(_LoadLibraryExW, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
249248
}
250249

src/runtime/syscall_windows.go

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -412,48 +412,9 @@ func callbackWrap(a *callbackArgs) {
412412
}
413413
}
414414

415-
const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
416-
417-
//go:linkname syscall_loadsystemlibrary syscall.loadsystemlibrary
418-
func syscall_loadsystemlibrary(filename *uint16) (handle, err uintptr) {
419-
handle, _, err = syscall_syscalln(uintptr(unsafe.Pointer(_LoadLibraryExW)), 3, uintptr(unsafe.Pointer(filename)), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
420-
KeepAlive(filename)
421-
if handle != 0 {
422-
err = 0
423-
}
424-
return
425-
}
426-
427-
// golang.org/x/sys linknames syscall.loadlibrary
428-
// (in addition to standard package syscall).
429-
// Do not remove or change the type signature.
430-
//
431-
//go:linkname syscall_loadlibrary syscall.loadlibrary
432-
func syscall_loadlibrary(filename *uint16) (handle, err uintptr) {
433-
handle, _, err = syscall_syscalln(uintptr(unsafe.Pointer(_LoadLibraryW)), 1, uintptr(unsafe.Pointer(filename)))
434-
KeepAlive(filename)
435-
if handle != 0 {
436-
err = 0
437-
}
438-
return
439-
}
440-
441-
// golang.org/x/sys linknames syscall.getprocaddress
442-
// (in addition to standard package syscall).
443-
// Do not remove or change the type signature.
444-
//
445-
//go:linkname syscall_getprocaddress syscall.getprocaddress
446-
func syscall_getprocaddress(handle uintptr, procname *byte) (outhandle, err uintptr) {
447-
outhandle, _, err = syscall_syscalln(uintptr(unsafe.Pointer(_GetProcAddress)), 2, handle, uintptr(unsafe.Pointer(procname)))
448-
KeepAlive(procname)
449-
if outhandle != 0 {
450-
err = 0
451-
}
452-
return
453-
}
454-
455415
//go:linkname syscall_syscalln syscall.syscalln
456416
//go:nosplit
417+
//go:uintptrkeepalive
457418
func syscall_syscalln(fn, n uintptr, args ...uintptr) (r1, r2, err uintptr) {
458419
if n > uintptr(len(args)) {
459420
panic("syscall: n > len(args)") // should not be reachable from user code

src/syscall/dll_windows.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ import (
1111
"unsafe"
1212
)
1313

14+
// Use double underscore to avoid name collision autogenerated functions.
15+
//go:cgo_import_dynamic syscall.__LoadLibraryExW LoadLibraryExW%3 "kernel32.dll"
16+
//go:cgo_import_dynamic syscall.__GetProcAddress GetProcAddress%2 "kernel32.dll"
17+
18+
var (
19+
__LoadLibraryExW unsafe.Pointer
20+
__GetProcAddress unsafe.Pointer
21+
)
22+
1423
// DLLError describes reasons for DLL load failures.
1524
type DLLError struct {
1625
Err error
@@ -94,9 +103,39 @@ func SyscallN(p uintptr, args ...uintptr) (r1, r2 uintptr, err Errno) {
94103
//
95104
//go:noescape
96105
func syscalln(fn, n uintptr, args ...uintptr) (r1, r2 uintptr, err Errno)
97-
func loadlibrary(filename *uint16) (handle uintptr, err Errno)
98-
func loadsystemlibrary(filename *uint16) (handle uintptr, err Errno)
99-
func getprocaddress(handle uintptr, procname *uint8) (proc uintptr, err Errno)
106+
107+
// N.B. For the loadlibrary, loadlibrary, and getprocaddress functions below:
108+
//
109+
// //go:linkname to act as an allowlist for linker's -checklinkname, as
110+
// golang.org/x/sys/windows linknames these functions.
111+
112+
//go:linkname loadlibrary
113+
func loadlibrary(filename *uint16) (uintptr, Errno) {
114+
handle, _, err := SyscallN(uintptr(__LoadLibraryExW), uintptr(unsafe.Pointer(filename)), 0, 0)
115+
if handle != 0 {
116+
err = 0
117+
}
118+
return handle, err
119+
}
120+
121+
//go:linkname loadsystemlibrary
122+
func loadsystemlibrary(filename *uint16) (uintptr, Errno) {
123+
const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
124+
handle, _, err := SyscallN(uintptr(__LoadLibraryExW), uintptr(unsafe.Pointer(filename)), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
125+
if handle != 0 {
126+
err = 0
127+
}
128+
return handle, err
129+
}
130+
131+
//go:linkname getprocaddress
132+
func getprocaddress(handle uintptr, procname *uint8) (uintptr, Errno) {
133+
proc, _, err := SyscallN(uintptr(__GetProcAddress), handle, uintptr(unsafe.Pointer(procname)))
134+
if proc != 0 {
135+
err = 0
136+
}
137+
return proc, err
138+
}
100139

101140
// A DLL implements access to a single DLL.
102141
type DLL struct {

0 commit comments

Comments
 (0)