github.com/dorkamotorka/go/src@v0.0.0-20230614113921-187095f0e316/syscall/mkasm.go (about)

     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  
     5  //go:build ignore
     6  
     7  // mkasm.go generates assembly trampolines to call library routines from Go.
     8  // This program must be run after mksyscall.pl.
     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  }