github.com/kidsbmilk/gofrontend_all@v0.0.0-20220701224323-6479d5976c5d/libgo/mklinknames.awk (about)

     1  # Copyright 2020 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  # This AWK script reads a Go file with special //extern-sysinfo
     6  # comments annotating functions which should be linked to libc
     7  # functions. It generates a Go file containing the appropriate
     8  # //go:linkname directives.
     9  #
    10  # For each annotated function, the script searches gen-sysinfo.go
    11  # to see if a different assembly name is known for the function.
    12  # For example, on NetBSD, the timegm symbol is renamed to
    13  # __timegm50 by an __asm__ annotation on its declaration in time.h.
    14  
    15  BEGIN {
    16      print "// Code generated by mklinknames.awk. DO NOT EDIT."
    17      print ""
    18      print "package", package
    19      print ""
    20      print "import _ \"unsafe\""
    21      print ""
    22  }
    23  
    24  /^\/\/extern-sysinfo/ {
    25      cfnname = $2
    26      getline
    27      if ($1 != "func") {
    28  	printf("mklinknames.awk: error: %s:%d: unattached extern-sysinfo directive\n", FILENAME, FNR) | "cat 1>&2"
    29  	exit 1
    30      }
    31      split($2, a, "(")
    32      gofnname = a[1]
    33      def = sprintf("grep '^func _%s[ (]' gen-sysinfo.go", cfnname)
    34      # def looks like one of the following:
    35      #     func _timegm (*_tm) int64 __asm__("__timegm50")
    36      #     func _timegm (*_tm) int64 __asm__("*__timegm50")
    37      # The goal is to extract "__timegm50".
    38      if ((def | getline fndef) > 0 && match(fndef, "__asm__\\(\"\\*?")) {
    39  	asmname = substr(fndef, RSTART + RLENGTH)
    40  	asmname = substr(asmname, 0, length(asmname) - 2)
    41  	printf("//go:linkname %s %s\n", gofnname, asmname)
    42      } else {
    43  	# Assume the asm name is the same as the declared C name.
    44  	printf("//go:linkname %s %s\n", gofnname, cfnname)
    45      }
    46  }