mkasm_darwin.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // +build ignore
  5. // mkasm_darwin.go generates assembly trampolines to call libSystem routines from Go.
  6. //This program must be run after mksyscall.go.
  7. package main
  8. import (
  9. "bytes"
  10. "fmt"
  11. "io/ioutil"
  12. "log"
  13. "os"
  14. "strings"
  15. )
  16. func writeASMFile(in string, fileName string, buildTags string) {
  17. trampolines := map[string]bool{}
  18. var out bytes.Buffer
  19. fmt.Fprintf(&out, "// go run mkasm_darwin.go %s\n", strings.Join(os.Args[1:], " "))
  20. fmt.Fprintf(&out, "// Code generated by the command above; DO NOT EDIT.\n")
  21. fmt.Fprintf(&out, "\n")
  22. fmt.Fprintf(&out, "// +build %s\n", buildTags)
  23. fmt.Fprintf(&out, "\n")
  24. fmt.Fprintf(&out, "#include \"textflag.h\"\n")
  25. for _, line := range strings.Split(in, "\n") {
  26. if !strings.HasPrefix(line, "func ") || !strings.HasSuffix(line, "_trampoline()") {
  27. continue
  28. }
  29. fn := line[5 : len(line)-13]
  30. if !trampolines[fn] {
  31. trampolines[fn] = true
  32. fmt.Fprintf(&out, "TEXT ·%s_trampoline(SB),NOSPLIT,$0-0\n", fn)
  33. fmt.Fprintf(&out, "\tJMP\t%s(SB)\n", fn)
  34. }
  35. }
  36. err := ioutil.WriteFile(fileName, out.Bytes(), 0644)
  37. if err != nil {
  38. log.Fatalf("can't write %s: %s", fileName, err)
  39. }
  40. }
  41. func main() {
  42. in1, err := ioutil.ReadFile("syscall_darwin.go")
  43. if err != nil {
  44. log.Fatalf("can't open syscall_darwin.go: %s", err)
  45. }
  46. arch := os.Args[1]
  47. in2, err := ioutil.ReadFile(fmt.Sprintf("syscall_darwin_%s.go", arch))
  48. if err != nil {
  49. log.Fatalf("can't open syscall_darwin_%s.go: %s", arch, err)
  50. }
  51. in3, err := ioutil.ReadFile(fmt.Sprintf("zsyscall_darwin_%s.go", arch))
  52. if err != nil {
  53. log.Fatalf("can't open zsyscall_darwin_%s.go: %s", arch, err)
  54. }
  55. in := string(in1) + string(in2) + string(in3)
  56. writeASMFile(in, fmt.Sprintf("zsyscall_darwin_%s.s", arch), "go1.12")
  57. in1, err = ioutil.ReadFile("syscall_darwin.1_13.go")
  58. if err != nil {
  59. log.Fatalf("can't open syscall_darwin.1_13.go: %s", err)
  60. }
  61. in2, err = ioutil.ReadFile(fmt.Sprintf("zsyscall_darwin_%s.1_13.go", arch))
  62. if err != nil {
  63. log.Fatalf("can't open zsyscall_darwin_%s.1_13.go: %s", arch, err)
  64. }
  65. in = string(in1) + string(in2)
  66. writeASMFile(in, fmt.Sprintf("zsyscall_darwin_%s.1_13.s", arch), "go1.13")
  67. }