-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccall.go
227 lines (199 loc) · 5.54 KB
/
ccall.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2021 The Go Darwin Authors
// SPDX-License-Identifier: BSD-3-Clause
//go:build darwin && amd64
// +build darwin,amd64
package sys
import (
"bytes"
"unsafe"
"github.com/go-darwin/sys/unsafeheader"
)
//go:linkname ccall syscall.syscall
//go:noescape
func ccall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
// Ccall calls a function in libc on behalf of the syscall package.
//
// syscall takes a pointer to a struct like:
//
// struct {
// fn uintptr
// a1 uintptr
// a2 uintptr
// a3 uintptr
// r1 uintptr
// r2 uintptr
// err uintptr
// }
//
// Ccall must be called on the g0 stack with the
// C calling convention (use libcCall).
//
// Ccall expects a 32-bit result and tests for 32-bit -1
// to decide there was an error.
//
//go:nosplit
func Ccall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
return ccall(fn, a1, a2, a3)
}
//go:linkname ccall6 syscall.syscall6
//go:noescape
func ccall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
// Ccall6 calls a function in libc on behalf of the syscall package.
//
// Ccall6 takes a pointer to a struct like:
//
// struct {
// fn uintptr
// a1 uintptr
// a2 uintptr
// a3 uintptr
// a4 uintptr
// a5 uintptr
// a6 uintptr
// r1 uintptr
// r2 uintptr
// err uintptr
// }
//
// Ccall6 must be called on the g0 stack with the
// C calling convention (use libcCall).
//
// Ccall6 expects a 32-bit result and tests for 32-bit -1
// to decide there was an error.
//
//go:nosplit
func Ccall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
return ccall6(fn, a1, a2, a3, a4, a5, a6)
}
//go:linkname ccall6X syscall.syscall6X
//go:noescape
func ccall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
// Ccall6X calls a function in libc on behalf of the syscall package.
//
// Ccall6X takes a pointer to a struct like:
//
// struct {
// fn uintptr
// a1 uintptr
// a2 uintptr
// a3 uintptr
// a4 uintptr
// a5 uintptr
// a6 uintptr
// r1 uintptr
// r2 uintptr
// err uintptr
// }
//
// Ccall6X must be called on the g0 stack with the
// C calling convention (use libcCall).
//
// Ccall6X is like syscall6 but expects a 64-bit result
// and tests for 64-bit -1 to decide there was an error.
//
//go:nosplit
func Ccall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
return ccall6X(fn, a1, a2, a3, a4, a5, a6)
}
// Ccall9 calls a function in libc on behalf of the syscall package.
//
// Ccall9 takes a pointer to a struct like:
//
// struct {
// fn uintptr
// a1 uintptr
// a2 uintptr
// a3 uintptr
// a4 uintptr
// a5 uintptr
// a6 uintptr
// a7 uintptr
// a8 uintptr
// a9 uintptr
// r1 uintptr
// r2 uintptr
// err uintptr
// }
//
// Ccall9 must be called on the g0 stack with the
// C calling convention (use libcCall).
//
// Ccall9 expects a 32-bit result and tests for 32-bit -1
// to decide there was an error.
//
//go:linkname Ccall9 syscall.Syscall9
//go:nosplit
func Ccall9(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err Errno)
//go:linkname ccallPtr syscall.syscallPtr
//go:noescape
func ccallPtr(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
// CcallPtr is like syscallX except that the libc function reports an
// error by returning NULL and setting errno.
//
//go:nosplit
func CcallPtr(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
return ccallPtr(fn, a1, a2, a3)
}
//go:linkname rawCcall syscall.rawSyscall
//go:noescape
func rawCcall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
// RawCcall calls a function in libc on behalf of the syscall package.
//
//go:nosplit
func RawCcall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
return rawCcall(fn, a1, a2, a3)
}
//go:linkname rawSyscall6 syscall.rawSyscall6
//go:noescape
func rawSyscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
// RawCcall6 calls a function in libc on behalf of the syscall package.
//
//go:nosplit
func RawCcall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
return rawSyscall6(fn, a1, a2, a3, a4, a5, a6)
}
// RawCcall9 calls a function in libc on behalf of the syscall package.
//
//go:noescape
//go:nosplit
func RawCcall9(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err Errno)
// ByteSliceFromString returns a NUL-terminated slice of bytes
// containing the text of s.
func ByteSliceFromString(s string) []byte {
a := make([]byte, len(s)+1)
copy(a, s)
return a
}
// BytePtrFromString returns a pointer to a NUL-terminated array of
// bytes containing the text of s.
func BytePtrFromString(s string) *byte {
a := ByteSliceFromString(s)
return &a[0]
}
// ByteSliceToString returns a string form of the text represented by the slice s, with a terminating NUL and any
// bytes after the NUL removed.
func ByteSliceToString(s []byte) string {
if i := bytes.IndexByte(s, 0); i != -1 {
s = s[:i]
}
return string(s)
}
// BytePtrToString takes a pointer to a sequence of text and returns the corresponding string.
// If the pointer is nil, it returns the empty string. It assumes that the text sequence is terminated
// at a zero byte; if the zero byte is not present, the program may crash.
func BytePtrToString(p *byte) string {
if p == nil || *p == 0 {
return ""
}
// Find NUL terminator.
n := 0
for ptr := unsafe.Pointer(p); *(*byte)(ptr) != 0; n++ {
ptr = unsafe.Pointer(uintptr(ptr) + 1)
}
var b []byte
h := (*unsafeheader.Slice)(unsafe.Pointer(&b))
h.Data = unsafe.Pointer(p)
h.Len = n
h.Cap = n
return string(b)
}