Black Lives Matter. Support the Equal Justice Initiative.

Source file src/syscall/mkasm.go

Documentation: syscall

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

View as plain text