Skip to content

Commit

Permalink
unix: convert openbsd/arm64 to direct libc calls
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
4a6f656c authored and gopherbot committed Aug 11, 2022
1 parent 1c4a2a7 commit 74508da
Show file tree
Hide file tree
Showing 6 changed files with 1,501 additions and 143 deletions.
4 changes: 2 additions & 2 deletions unix/mkall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ openbsd_arm)
mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
;;
openbsd_arm64)
mkasm="go run mkasm.go"
mkerrors="$mkerrors -m64"
mksyscall="go run mksyscall.go -openbsd"
mksyscall="go run mksyscall.go -openbsd -libc"
mksysctl="go run mksysctl_openbsd.go"
mksysnum="go run mksysnum.go 'https://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master'"
# Let the type of C char be signed for making the bare syscall
# API consistent across platforms.
mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
Expand Down
24 changes: 18 additions & 6 deletions unix/mksyscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ import (
var (
b32 = flag.Bool("b32", false, "32bit big-endian")
l32 = flag.Bool("l32", false, "32bit little-endian")
libc = flag.Bool("libc", false, "libc system calls")
plan9 = flag.Bool("plan9", false, "plan9")
openbsd = flag.Bool("openbsd", false, "openbsd")
netbsd = flag.Bool("netbsd", false, "netbsd")
dragonfly = flag.Bool("dragonfly", false, "dragonfly")
arm = flag.Bool("arm", false, "arm") // 64-bit value should use (even, odd)-pair
tags = flag.String("tags", "", "build tags")
filename = flag.String("output", "", "output file name (standard output if omitted)")

libcPath = "libc.so"
)

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

libc := false
if goos == "darwin" {
libc = true
libcPath = "/usr/lib/libSystem.B.dylib"
*libc = true
}

trampolines := map[string]bool{}

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

var libcFn string
if libc {
if *libc {
asm = "syscall_" + strings.ToLower(asm[:1]) + asm[1:] // internal syscall call
sysname = strings.TrimPrefix(sysname, "SYS_") // remove SYS_
sysname = strings.ToLower(sysname) // lowercase
if *openbsd && *libc {
switch sysname {
case "__getcwd":
sysname = "getcwd"
case "__sysctl":
sysname = "sysctl"
}
}
libcFn = sysname
sysname = "libc_" + sysname + "_trampoline_addr"
}
Expand Down Expand Up @@ -360,14 +372,14 @@ func main() {
text += "\treturn\n"
text += "}\n\n"

if libc && !trampolines[libcFn] {
if *libc && !trampolines[libcFn] {
// some system calls share a trampoline, like read and readlen.
trampolines[libcFn] = true
// Declare assembly trampoline address.
text += fmt.Sprintf("var libc_%s_trampoline_addr uintptr\n\n", libcFn)
// Assembly trampoline calls the libc_* function, which this magic
// redirects to use the function from libSystem.
text += fmt.Sprintf("//go:cgo_import_dynamic libc_%s %s \"/usr/lib/libSystem.B.dylib\"\n", libcFn, libcFn)
text += fmt.Sprintf("//go:cgo_import_dynamic libc_%s %s %q\n", libcFn, libcFn, libcPath)
text += "\n"
}
}
Expand Down
21 changes: 21 additions & 0 deletions unix/syscall_openbsd_libc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build openbsd && arm64
// +build openbsd,arm64

package unix

import _ "unsafe"

// Implemented in the runtime package (runtime/sys_openbsd3.go)
func syscall_syscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
func syscall_syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
func syscall_rawSyscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
func syscall_rawSyscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)

//go:linkname syscall_syscall syscall.syscall
//go:linkname syscall_syscall6 syscall.syscall6
//go:linkname syscall_rawSyscall syscall.rawSyscall
//go:linkname syscall_rawSyscall6 syscall.rawSyscall6
Loading

0 comments on commit 74508da

Please sign in to comment.