1 // Copyright 2018 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.
7 // mkasm.go generates assembly trampolines to call library routines from Go.
8 // This program must be run after mksyscall.pl.
20 if len(os.Args) != 3 {
21 log.Fatalf("Usage: %s <goos> <arch>", os.Args[0])
23 goos, arch := os.Args[1], os.Args[2]
25 syscallFilename := fmt.Sprintf("syscall_%s.go", goos)
26 syscallArchFilename := fmt.Sprintf("syscall_%s_%s.go", goos, arch)
28 in1, err := os.ReadFile(syscallFilename)
30 log.Fatalf("can't open syscall file: %s", err)
32 in2, err := os.ReadFile(syscallArchFilename)
34 log.Fatalf("can't open syscall file: %s", err)
36 in3, err := os.ReadFile("z" + syscallArchFilename)
38 log.Fatalf("can't open syscall file: %s", err)
40 in := string(in1) + string(in2) + string(in3)
42 trampolines := map[string]bool{}
46 fmt.Fprintf(&out, "// go run mkasm.go %s\n", strings.Join(os.Args[1:], " "))
47 fmt.Fprintf(&out, "// Code generated by the command above; DO NOT EDIT.\n")
48 fmt.Fprintf(&out, "#include \"textflag.h\"\n")
49 for _, line := range strings.Split(in, "\n") {
50 if !strings.HasPrefix(line, "func ") || !strings.HasSuffix(line, "_trampoline()") {
53 fn := line[5 : len(line)-13]
55 trampolines[fn] = true
56 fmt.Fprintf(&out, "TEXT ยท%s_trampoline(SB),NOSPLIT,$0-0\n", fn)
57 if goos == "openbsd" && arch == "ppc64" {
58 fmt.Fprintf(&out, "\tCALL\t%s(SB)\n", fn)
59 fmt.Fprintf(&out, "\tRET\n")
61 fmt.Fprintf(&out, "\tJMP\t%s(SB)\n", fn)
65 err = os.WriteFile(fmt.Sprintf("zsyscall_%s_%s.s", goos, arch), out.Bytes(), 0644)
67 log.Fatalf("can't write syscall file: %s", err)