Skip to content

Commit 74508da

Browse files
4a6f656cgopherbot
authored andcommitted
unix: convert openbsd/arm64 to direct libc calls
The current code has continued to work on OpenBSD, since it has been using syscall(2) via libc. However, the system call numbers are still hardcoded in golang.org/sys/unix. Various system call changes have been made in OpenBSD, resulting in changes to the system call numbers and arguments, which now fail when this package is used. Switch to calling various system calls directly via libc, rather than calling via libc using syscall(2). Updates golang/go#36435 Change-Id: I836a484b14e0a427ac565315e27f0de1e9a5d021 Reviewed-on: https://go-review.googlesource.com/c/sys/+/421796 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Tobias Klauser <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Than McIntosh <[email protected]>
1 parent 1c4a2a7 commit 74508da

File tree

6 files changed

+1501
-143
lines changed

6 files changed

+1501
-143
lines changed

unix/mkall.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ openbsd_arm)
165165
mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
166166
;;
167167
openbsd_arm64)
168+
mkasm="go run mkasm.go"
168169
mkerrors="$mkerrors -m64"
169-
mksyscall="go run mksyscall.go -openbsd"
170+
mksyscall="go run mksyscall.go -openbsd -libc"
170171
mksysctl="go run mksysctl_openbsd.go"
171-
mksysnum="go run mksysnum.go 'https://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master'"
172172
# Let the type of C char be signed for making the bare syscall
173173
# API consistent across platforms.
174174
mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"

unix/mksyscall.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@ import (
3636
var (
3737
b32 = flag.Bool("b32", false, "32bit big-endian")
3838
l32 = flag.Bool("l32", false, "32bit little-endian")
39+
libc = flag.Bool("libc", false, "libc system calls")
3940
plan9 = flag.Bool("plan9", false, "plan9")
4041
openbsd = flag.Bool("openbsd", false, "openbsd")
4142
netbsd = flag.Bool("netbsd", false, "netbsd")
4243
dragonfly = flag.Bool("dragonfly", false, "dragonfly")
4344
arm = flag.Bool("arm", false, "arm") // 64-bit value should use (even, odd)-pair
4445
tags = flag.String("tags", "", "build tags")
4546
filename = flag.String("output", "", "output file name (standard output if omitted)")
47+
48+
libcPath = "libc.so"
4649
)
4750

4851
// cmdLine returns this programs's commandline arguments
@@ -124,10 +127,11 @@ func main() {
124127
endianness = "little-endian"
125128
}
126129

127-
libc := false
128130
if goos == "darwin" {
129-
libc = true
131+
libcPath = "/usr/lib/libSystem.B.dylib"
132+
*libc = true
130133
}
134+
131135
trampolines := map[string]bool{}
132136

133137
text := ""
@@ -210,7 +214,7 @@ func main() {
210214
text += fmt.Sprintf(" else {\n\t\t_p%d = unsafe.Pointer(&_zero)\n\t}\n", n)
211215
args = append(args, fmt.Sprintf("uintptr(_p%d)", n), fmt.Sprintf("uintptr(len(%s))", p.Name))
212216
n++
213-
} else if p.Type == "int64" && (*openbsd || *netbsd) {
217+
} else if p.Type == "int64" && ((*openbsd && !*libc) || *netbsd) {
214218
args = append(args, "0")
215219
if endianness == "big-endian" {
216220
args = append(args, fmt.Sprintf("uintptr(%s>>32)", p.Name), fmt.Sprintf("uintptr(%s)", p.Name))
@@ -285,10 +289,18 @@ func main() {
285289
}
286290

287291
var libcFn string
288-
if libc {
292+
if *libc {
289293
asm = "syscall_" + strings.ToLower(asm[:1]) + asm[1:] // internal syscall call
290294
sysname = strings.TrimPrefix(sysname, "SYS_") // remove SYS_
291295
sysname = strings.ToLower(sysname) // lowercase
296+
if *openbsd && *libc {
297+
switch sysname {
298+
case "__getcwd":
299+
sysname = "getcwd"
300+
case "__sysctl":
301+
sysname = "sysctl"
302+
}
303+
}
292304
libcFn = sysname
293305
sysname = "libc_" + sysname + "_trampoline_addr"
294306
}
@@ -360,14 +372,14 @@ func main() {
360372
text += "\treturn\n"
361373
text += "}\n\n"
362374

363-
if libc && !trampolines[libcFn] {
375+
if *libc && !trampolines[libcFn] {
364376
// some system calls share a trampoline, like read and readlen.
365377
trampolines[libcFn] = true
366378
// Declare assembly trampoline address.
367379
text += fmt.Sprintf("var libc_%s_trampoline_addr uintptr\n\n", libcFn)
368380
// Assembly trampoline calls the libc_* function, which this magic
369381
// redirects to use the function from libSystem.
370-
text += fmt.Sprintf("//go:cgo_import_dynamic libc_%s %s \"/usr/lib/libSystem.B.dylib\"\n", libcFn, libcFn)
382+
text += fmt.Sprintf("//go:cgo_import_dynamic libc_%s %s %q\n", libcFn, libcFn, libcPath)
371383
text += "\n"
372384
}
373385
}

unix/syscall_openbsd_libc.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build openbsd && arm64
6+
// +build openbsd,arm64
7+
8+
package unix
9+
10+
import _ "unsafe"
11+
12+
// Implemented in the runtime package (runtime/sys_openbsd3.go)
13+
func syscall_syscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
14+
func syscall_syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
15+
func syscall_rawSyscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
16+
func syscall_rawSyscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
17+
18+
//go:linkname syscall_syscall syscall.syscall
19+
//go:linkname syscall_syscall6 syscall.syscall6
20+
//go:linkname syscall_rawSyscall syscall.rawSyscall
21+
//go:linkname syscall_rawSyscall6 syscall.rawSyscall6

0 commit comments

Comments
 (0)