From 1c4a2a72c664c42691e45fe3a31510ec5ba13cfb Mon Sep 17 00:00:00 2001 From: Joel Sing Date: Tue, 2 Aug 2022 05:03:07 +1000 Subject: [PATCH] unix: make mkasm_darwin.go usable with other operating systems Rename mkasm_darwin.go to mkasm.go and pass the operating system as an argument. This will allow mkasm to be used to generate assembly for OpenBSD. Updates golang/go#36435 Change-Id: I42f54f5c6edc3a18a156e0e8e3c38d07ecf26d0b Reviewed-on: https://go-review.googlesource.com/c/sys/+/421795 Run-TryBot: Joel Sing Run-TryBot: Ian Lance Taylor Reviewed-by: Than McIntosh TryBot-Result: Gopher Robot Reviewed-by: Tobias Klauser Reviewed-by: Ian Lance Taylor Auto-Submit: Ian Lance Taylor --- unix/darwin_amd64_test.go | 2 +- unix/darwin_arm64_test.go | 2 +- unix/mkall.sh | 6 +-- unix/{mkasm_darwin.go => mkasm.go} | 64 +++++++++++++++++++++--------- unix/zsyscall_darwin_amd64.1_13.s | 2 +- unix/zsyscall_darwin_amd64.s | 2 +- unix/zsyscall_darwin_arm64.1_13.s | 2 +- unix/zsyscall_darwin_arm64.s | 2 +- 8 files changed, 55 insertions(+), 27 deletions(-) rename unix/{mkasm_darwin.go => mkasm.go} (65%) diff --git a/unix/darwin_amd64_test.go b/unix/darwin_amd64_test.go index 3bf217d6..e6d3ca0f 100644 --- a/unix/darwin_amd64_test.go +++ b/unix/darwin_amd64_test.go @@ -1,4 +1,4 @@ -// go run mkasm_darwin.go amd64 +// go run mkasm.go darwin amd64 // Code generated by the command above; DO NOT EDIT. //go:build darwin && go1.12 diff --git a/unix/darwin_arm64_test.go b/unix/darwin_arm64_test.go index 41e80d2d..3cab2091 100644 --- a/unix/darwin_arm64_test.go +++ b/unix/darwin_arm64_test.go @@ -1,4 +1,4 @@ -// go run mkasm_darwin.go arm64 +// go run mkasm.go darwin arm64 // Code generated by the command above; DO NOT EDIT. //go:build darwin && go1.12 diff --git a/unix/mkall.sh b/unix/mkall.sh index dcef4de6..2cd0e916 100755 --- a/unix/mkall.sh +++ b/unix/mkall.sh @@ -73,12 +73,12 @@ aix_ppc64) darwin_amd64) mkerrors="$mkerrors -m64" mktypes="GOARCH=$GOARCH go tool cgo -godefs" - mkasm="go run mkasm_darwin.go" + mkasm="go run mkasm.go" ;; darwin_arm64) mkerrors="$mkerrors -m64" mktypes="GOARCH=$GOARCH go tool cgo -godefs" - mkasm="go run mkasm_darwin.go" + mkasm="go run mkasm.go" ;; dragonfly_amd64) mkerrors="$mkerrors -m64" @@ -232,5 +232,5 @@ esac if [ -n "$mksysctl" ]; then echo "$mksysctl |gofmt >$zsysctl"; fi if [ -n "$mksysnum" ]; then echo "$mksysnum |gofmt >zsysnum_$GOOSARCH.go"; fi if [ -n "$mktypes" ]; then echo "$mktypes types_$GOOS.go | go run mkpost.go > ztypes_$GOOSARCH.go"; fi - if [ -n "$mkasm" ]; then echo "$mkasm $GOARCH"; fi + if [ -n "$mkasm" ]; then echo "$mkasm $GOOS $GOARCH"; fi ) | $run diff --git a/unix/mkasm_darwin.go b/unix/mkasm.go similarity index 65% rename from unix/mkasm_darwin.go rename to unix/mkasm.go index cfb565fc..e2775214 100644 --- a/unix/mkasm_darwin.go +++ b/unix/mkasm.go @@ -5,7 +5,7 @@ //go:build ignore // +build ignore -// mkasm_darwin.go generates assembly trampolines to call libSystem routines from Go. +// mkasm.go generates assembly trampolines to call library routines from Go. // This program must be run after mksyscall.go. package main @@ -19,9 +19,19 @@ import ( "strings" ) -const ptrsize = 8 // Pointer size. All supported platforms are 64-bit. +func archPtrSize(arch string) int { + switch arch { + case "386", "arm": + return 4 + case "amd64", "arm64", "mips64": + return 8 + default: + log.Fatalf("Unknown arch %q", arch) + return 0 + } +} -func generateASMFile(inFileNames []string, outFileName string, buildTags string) map[string]bool { +func generateASMFile(arch string, inFileNames []string, outFileName, buildTags string) map[string]bool { trampolines := map[string]bool{} var orderedTrampolines []string for _, inFileName := range inFileNames { @@ -43,19 +53,23 @@ func generateASMFile(inFileNames []string, outFileName string, buildTags string) } } + ptrSize := archPtrSize(arch) + var out bytes.Buffer - fmt.Fprintf(&out, "// go run mkasm_darwin.go %s\n", strings.Join(os.Args[1:], " ")) + fmt.Fprintf(&out, "// go run mkasm.go %s\n", strings.Join(os.Args[1:], " ")) fmt.Fprintf(&out, "// Code generated by the command above; DO NOT EDIT.\n") fmt.Fprintf(&out, "\n") - fmt.Fprintf(&out, "//go:build %s\n", buildTags) - fmt.Fprintf(&out, "// +build %s\n", buildTags) - fmt.Fprintf(&out, "\n") + if buildTags != "" { + fmt.Fprintf(&out, "//go:build %s\n", buildTags) + fmt.Fprintf(&out, "// +build %s\n", buildTags) + fmt.Fprintf(&out, "\n") + } fmt.Fprintf(&out, "#include \"textflag.h\"\n") for _, fn := range orderedTrampolines { fmt.Fprintf(&out, "\nTEXT %s_trampoline<>(SB),NOSPLIT,$0-0\n", fn) fmt.Fprintf(&out, "\tJMP\t%s(SB)\n\n", fn) - fmt.Fprintf(&out, "GLOBL\t·%s_trampoline_addr(SB), RODATA, $%d\n", fn, ptrsize) - fmt.Fprintf(&out, "DATA\t·%s_trampoline_addr(SB)/%d, $%s_trampoline<>(SB)\n", fn, ptrsize, fn) + fmt.Fprintf(&out, "GLOBL\t·%s_trampoline_addr(SB), RODATA, $%d\n", fn, ptrSize) + fmt.Fprintf(&out, "DATA\t·%s_trampoline_addr(SB)/%d, $%s_trampoline<>(SB)\n", fn, ptrSize, fn) } if err := ioutil.WriteFile(outFileName, out.Bytes(), 0644); err != nil { @@ -65,7 +79,7 @@ func generateASMFile(inFileNames []string, outFileName string, buildTags string) return trampolines } -const darwinTestTemplate = `// go run mkasm_darwin.go %s +const darwinTestTemplate = `// go run mkasm.go %s // Code generated by the command above; DO NOT EDIT. //go:build darwin && go1.12 @@ -102,23 +116,37 @@ func writeDarwinTest(trampolines map[string]bool, fileName, arch string) { } func main() { - if len(os.Args) != 2 { - log.Fatalf("Usage: %s ", os.Args[0]) + if len(os.Args) != 3 { + log.Fatalf("Usage: %s ", os.Args[0]) } - arch := os.Args[1] + goos, arch := os.Args[1], os.Args[2] + + buildTags := "" + syscallFilename := fmt.Sprintf("syscall_%s.go", goos) + syscallArchFilename := fmt.Sprintf("syscall_%s_%s.go", goos, arch) + zsyscallArchFilename := fmt.Sprintf("zsyscall_%s_%s.go", goos, arch) + zsyscallASMFileName := fmt.Sprintf("zsyscall_%s_%s.s", goos, arch) inFileNames := []string{ - "syscall_darwin.go", - fmt.Sprintf("syscall_darwin_%s.go", arch), - fmt.Sprintf("zsyscall_darwin_%s.go", arch), + syscallFilename, + syscallArchFilename, + zsyscallArchFilename, + } + + if goos == "darwin" { + buildTags = "go1.12" + } + trampolines := generateASMFile(arch, inFileNames, zsyscallASMFileName, buildTags) + + if goos != "darwin" { + return } - trampolines := generateASMFile(inFileNames, fmt.Sprintf("zsyscall_darwin_%s.s", arch), "go1.12") inFileNames = []string{ "syscall_darwin.1_13.go", fmt.Sprintf("zsyscall_darwin_%s.1_13.go", arch), } - trampolines2 := generateASMFile(inFileNames, fmt.Sprintf("zsyscall_darwin_%s.1_13.s", arch), "go1.13") + trampolines2 := generateASMFile(arch, inFileNames, fmt.Sprintf("zsyscall_darwin_%s.1_13.s", arch), "go1.13") // merge trampolines for trampoline := range trampolines2 { diff --git a/unix/zsyscall_darwin_amd64.1_13.s b/unix/zsyscall_darwin_amd64.1_13.s index d6c3e25c..f5bb40ed 100644 --- a/unix/zsyscall_darwin_amd64.1_13.s +++ b/unix/zsyscall_darwin_amd64.1_13.s @@ -1,4 +1,4 @@ -// go run mkasm_darwin.go amd64 +// go run mkasm.go darwin amd64 // Code generated by the command above; DO NOT EDIT. //go:build go1.13 diff --git a/unix/zsyscall_darwin_amd64.s b/unix/zsyscall_darwin_amd64.s index 7e308a47..b41467a0 100644 --- a/unix/zsyscall_darwin_amd64.s +++ b/unix/zsyscall_darwin_amd64.s @@ -1,4 +1,4 @@ -// go run mkasm_darwin.go amd64 +// go run mkasm.go darwin amd64 // Code generated by the command above; DO NOT EDIT. //go:build go1.12 diff --git a/unix/zsyscall_darwin_arm64.1_13.s b/unix/zsyscall_darwin_arm64.1_13.s index 35798972..0c3f76bc 100644 --- a/unix/zsyscall_darwin_arm64.1_13.s +++ b/unix/zsyscall_darwin_arm64.1_13.s @@ -1,4 +1,4 @@ -// go run mkasm_darwin.go arm64 +// go run mkasm.go darwin arm64 // Code generated by the command above; DO NOT EDIT. //go:build go1.13 diff --git a/unix/zsyscall_darwin_arm64.s b/unix/zsyscall_darwin_arm64.s index b09e5bb0..e1f9204a 100644 --- a/unix/zsyscall_darwin_arm64.s +++ b/unix/zsyscall_darwin_arm64.s @@ -1,4 +1,4 @@ -// go run mkasm_darwin.go arm64 +// go run mkasm.go darwin arm64 // Code generated by the command above; DO NOT EDIT. //go:build go1.12