mkasm.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.
  4. //go:build ignore
  5. // mkasm.go generates assembly trampolines to call library routines from Go.
  6. // This program must be run after mksyscall.pl.
  7. package main
  8. import (
  9. "bytes"
  10. "fmt"
  11. "log"
  12. "os"
  13. "strings"
  14. )
  15. func main() {
  16. if len(os.Args) != 3 {
  17. log.Fatalf("Usage: %s <goos> <arch>", os.Args[0])
  18. }
  19. goos, arch := os.Args[1], os.Args[2]
  20. syscallFilename := fmt.Sprintf("syscall_%s.go", goos)
  21. syscallArchFilename := fmt.Sprintf("syscall_%s_%s.go", goos, arch)
  22. in1, err := os.ReadFile(syscallFilename)
  23. if err != nil {
  24. log.Fatalf("can't open syscall file: %s", err)
  25. }
  26. in2, err := os.ReadFile(syscallArchFilename)
  27. if err != nil {
  28. log.Fatalf("can't open syscall file: %s", err)
  29. }
  30. in3, err := os.ReadFile("z" + syscallArchFilename)
  31. if err != nil {
  32. log.Fatalf("can't open syscall file: %s", err)
  33. }
  34. in := string(in1) + string(in2) + string(in3)
  35. trampolines := map[string]bool{}
  36. var out bytes.Buffer
  37. fmt.Fprintf(&out, "// go run mkasm.go %s\n", strings.Join(os.Args[1:], " "))
  38. fmt.Fprintf(&out, "// Code generated by the command above; DO NOT EDIT.\n")
  39. fmt.Fprintf(&out, "#include \"textflag.h\"\n")
  40. for _, line := range strings.Split(in, "\n") {
  41. if !strings.HasPrefix(line, "func ") || !strings.HasSuffix(line, "_trampoline()") {
  42. continue
  43. }
  44. fn := line[5 : len(line)-13]
  45. if !trampolines[fn] {
  46. trampolines[fn] = true
  47. fmt.Fprintf(&out, "TEXT ·%s_trampoline(SB),NOSPLIT,$0-0\n", fn)
  48. fmt.Fprintf(&out, "\tJMP\t%s(SB)\n", fn)
  49. }
  50. }
  51. err = os.WriteFile(fmt.Sprintf("zsyscall_%s_%s.s", goos, arch), out.Bytes(), 0644)
  52. if err != nil {
  53. log.Fatalf("can't write syscall file: %s", err)
  54. }
  55. }