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.
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.
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.
16 print "// Code generated by mklinknames.awk. DO NOT EDIT."
18 print "package", package
20 print "import _ \"unsafe\""
24 /^\
/\
/extern
-sysinfo
/ {
28 printf("mklinknames.awk: error: %s:%d: unattached extern-sysinfo directive\n", FILENAME, FNR) | "cat 1>&2"
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
)
43 # Assume the asm name is the same as the declared C name.
44 printf("//go:linkname %s %s\n", gofnname
, cfnname
)