Source file
src/syscall/mkasm.go
1
2
3
4
5
6
7
8
9 package main
10
11 import (
12 "bytes"
13 "fmt"
14 "log"
15 "os"
16 "strings"
17 )
18
19 func main() {
20 if len(os.Args) != 3 {
21 log.Fatalf("Usage: %s <goos> <arch>", os.Args[0])
22 }
23 goos, arch := os.Args[1], os.Args[2]
24
25 syscallFilename := fmt.Sprintf("syscall_%s.go", goos)
26 syscallArchFilename := fmt.Sprintf("syscall_%s_%s.go", goos, arch)
27
28 in1, err := os.ReadFile(syscallFilename)
29 if err != nil {
30 log.Fatalf("can't open syscall file: %s", err)
31 }
32 in2, err := os.ReadFile(syscallArchFilename)
33 if err != nil {
34 log.Fatalf("can't open syscall file: %s", err)
35 }
36 in3, err := os.ReadFile("z" + syscallArchFilename)
37 if err != nil {
38 log.Fatalf("can't open syscall file: %s", err)
39 }
40 in := string(in1) + string(in2) + string(in3)
41
42 trampolines := map[string]bool{}
43
44 var out bytes.Buffer
45
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()") {
51 continue
52 }
53 fn := line[5 : len(line)-13]
54 if !trampolines[fn] {
55 trampolines[fn] = true
56 fmt.Fprintf(&out, "TEXT ·%s_trampoline(SB),NOSPLIT,$0-0\n", fn)
57 fmt.Fprintf(&out, "\tJMP\t%s(SB)\n", fn)
58 }
59 }
60 err = os.WriteFile(fmt.Sprintf("zsyscall_%s_%s.s", goos, arch), out.Bytes(), 0644)
61 if err != nil {
62 log.Fatalf("can't write syscall file: %s", err)
63 }
64 }
65
View as plain text