Source file src/cmd/cgo/main.go

     1  // Copyright 2009 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  // Cgo; see doc.go for an overview.
     6  
     7  // TODO(rsc):
     8  //	Emit correct line number annotations.
     9  //	Make gc understand the annotations.
    10  
    11  package main
    12  
    13  import (
    14  	"flag"
    15  	"fmt"
    16  	"go/ast"
    17  	"go/printer"
    18  	"go/token"
    19  	"internal/buildcfg"
    20  	"io"
    21  	"os"
    22  	"path/filepath"
    23  	"reflect"
    24  	"runtime"
    25  	"sort"
    26  	"strings"
    27  
    28  	"cmd/internal/edit"
    29  	"cmd/internal/notsha256"
    30  	"cmd/internal/objabi"
    31  	"cmd/internal/telemetry/counter"
    32  )
    33  
    34  // A Package collects information about the package we're going to write.
    35  type Package struct {
    36  	PackageName string // name of package
    37  	PackagePath string
    38  	PtrSize     int64
    39  	IntSize     int64
    40  	GccOptions  []string
    41  	GccIsClang  bool
    42  	LdFlags     []string // #cgo LDFLAGS
    43  	Written     map[string]bool
    44  	Name        map[string]*Name // accumulated Name from Files
    45  	ExpFunc     []*ExpFunc       // accumulated ExpFunc from Files
    46  	Decl        []ast.Decl
    47  	GoFiles     []string        // list of Go files
    48  	GccFiles    []string        // list of gcc output files
    49  	Preamble    string          // collected preamble for _cgo_export.h
    50  	typedefs    map[string]bool // type names that appear in the types of the objects we're interested in
    51  	typedefList []typedefInfo
    52  	noCallbacks map[string]bool // C function names with #cgo nocallback directive
    53  	noEscapes   map[string]bool // C function names with #cgo noescape directive
    54  }
    55  
    56  // A typedefInfo is an element on Package.typedefList: a typedef name
    57  // and the position where it was required.
    58  type typedefInfo struct {
    59  	typedef string
    60  	pos     token.Pos
    61  }
    62  
    63  // A File collects information about a single Go input file.
    64  type File struct {
    65  	AST         *ast.File           // parsed AST
    66  	Comments    []*ast.CommentGroup // comments from file
    67  	Package     string              // Package name
    68  	Preamble    string              // C preamble (doc comment on import "C")
    69  	Ref         []*Ref              // all references to C.xxx in AST
    70  	Calls       []*Call             // all calls to C.xxx in AST
    71  	ExpFunc     []*ExpFunc          // exported functions for this file
    72  	Name        map[string]*Name    // map from Go name to Name
    73  	NamePos     map[*Name]token.Pos // map from Name to position of the first reference
    74  	NoCallbacks map[string]bool     // C function names that with #cgo nocallback directive
    75  	NoEscapes   map[string]bool     // C function names that with #cgo noescape directive
    76  	Edit        *edit.Buffer
    77  }
    78  
    79  func (f *File) offset(p token.Pos) int {
    80  	return fset.Position(p).Offset
    81  }
    82  
    83  func nameKeys(m map[string]*Name) []string {
    84  	var ks []string
    85  	for k := range m {
    86  		ks = append(ks, k)
    87  	}
    88  	sort.Strings(ks)
    89  	return ks
    90  }
    91  
    92  // A Call refers to a call of a C.xxx function in the AST.
    93  type Call struct {
    94  	Call     *ast.CallExpr
    95  	Deferred bool
    96  	Done     bool
    97  }
    98  
    99  // A Ref refers to an expression of the form C.xxx in the AST.
   100  type Ref struct {
   101  	Name    *Name
   102  	Expr    *ast.Expr
   103  	Context astContext
   104  	Done    bool
   105  }
   106  
   107  func (r *Ref) Pos() token.Pos {
   108  	return (*r.Expr).Pos()
   109  }
   110  
   111  var nameKinds = []string{"iconst", "fconst", "sconst", "type", "var", "fpvar", "func", "macro", "not-type"}
   112  
   113  // A Name collects information about C.xxx.
   114  type Name struct {
   115  	Go       string // name used in Go referring to package C
   116  	Mangle   string // name used in generated Go
   117  	C        string // name used in C
   118  	Define   string // #define expansion
   119  	Kind     string // one of the nameKinds
   120  	Type     *Type  // the type of xxx
   121  	FuncType *FuncType
   122  	AddError bool
   123  	Const    string // constant definition
   124  }
   125  
   126  // IsVar reports whether Kind is either "var" or "fpvar"
   127  func (n *Name) IsVar() bool {
   128  	return n.Kind == "var" || n.Kind == "fpvar"
   129  }
   130  
   131  // IsConst reports whether Kind is either "iconst", "fconst" or "sconst"
   132  func (n *Name) IsConst() bool {
   133  	return strings.HasSuffix(n.Kind, "const")
   134  }
   135  
   136  // An ExpFunc is an exported function, callable from C.
   137  // Such functions are identified in the Go input file
   138  // by doc comments containing the line //export ExpName
   139  type ExpFunc struct {
   140  	Func    *ast.FuncDecl
   141  	ExpName string // name to use from C
   142  	Doc     string
   143  }
   144  
   145  // A TypeRepr contains the string representation of a type.
   146  type TypeRepr struct {
   147  	Repr       string
   148  	FormatArgs []interface{}
   149  }
   150  
   151  // A Type collects information about a type in both the C and Go worlds.
   152  type Type struct {
   153  	Size       int64
   154  	Align      int64
   155  	C          *TypeRepr
   156  	Go         ast.Expr
   157  	EnumValues map[string]int64
   158  	Typedef    string
   159  	BadPointer bool // this pointer type should be represented as a uintptr (deprecated)
   160  }
   161  
   162  func (t *Type) fuzzyMatch(t2 *Type) bool {
   163  	if t == nil || t2 == nil {
   164  		return false
   165  	}
   166  	return t.Size == t2.Size && t.Align == t2.Align
   167  }
   168  
   169  // A FuncType collects information about a function type in both the C and Go worlds.
   170  type FuncType struct {
   171  	Params []*Type
   172  	Result *Type
   173  	Go     *ast.FuncType
   174  }
   175  
   176  func (t *FuncType) fuzzyMatch(t2 *FuncType) bool {
   177  	if t == nil || t2 == nil {
   178  		return false
   179  	}
   180  	if !t.Result.fuzzyMatch(t2.Result) {
   181  		return false
   182  	}
   183  	if len(t.Params) != len(t2.Params) {
   184  		return false
   185  	}
   186  	for i := range t.Params {
   187  		if !t.Params[i].fuzzyMatch(t2.Params[i]) {
   188  			return false
   189  		}
   190  	}
   191  	return true
   192  }
   193  
   194  func usage() {
   195  	fmt.Fprint(os.Stderr, "usage: cgo -- [compiler options] file.go ...\n")
   196  	flag.PrintDefaults()
   197  	os.Exit(2)
   198  }
   199  
   200  var ptrSizeMap = map[string]int64{
   201  	"386":      4,
   202  	"alpha":    8,
   203  	"amd64":    8,
   204  	"arm":      4,
   205  	"arm64":    8,
   206  	"loong64":  8,
   207  	"m68k":     4,
   208  	"mips":     4,
   209  	"mipsle":   4,
   210  	"mips64":   8,
   211  	"mips64le": 8,
   212  	"nios2":    4,
   213  	"ppc":      4,
   214  	"ppc64":    8,
   215  	"ppc64le":  8,
   216  	"riscv":    4,
   217  	"riscv64":  8,
   218  	"s390":     4,
   219  	"s390x":    8,
   220  	"sh":       4,
   221  	"shbe":     4,
   222  	"sparc":    4,
   223  	"sparc64":  8,
   224  }
   225  
   226  var intSizeMap = map[string]int64{
   227  	"386":      4,
   228  	"alpha":    8,
   229  	"amd64":    8,
   230  	"arm":      4,
   231  	"arm64":    8,
   232  	"loong64":  8,
   233  	"m68k":     4,
   234  	"mips":     4,
   235  	"mipsle":   4,
   236  	"mips64":   8,
   237  	"mips64le": 8,
   238  	"nios2":    4,
   239  	"ppc":      4,
   240  	"ppc64":    8,
   241  	"ppc64le":  8,
   242  	"riscv":    4,
   243  	"riscv64":  8,
   244  	"s390":     4,
   245  	"s390x":    8,
   246  	"sh":       4,
   247  	"shbe":     4,
   248  	"sparc":    4,
   249  	"sparc64":  8,
   250  }
   251  
   252  var cPrefix string
   253  
   254  var fset = token.NewFileSet()
   255  
   256  var dynobj = flag.String("dynimport", "", "if non-empty, print dynamic import data for that file")
   257  var dynout = flag.String("dynout", "", "write -dynimport output to this file")
   258  var dynpackage = flag.String("dynpackage", "main", "set Go package for -dynimport output")
   259  var dynlinker = flag.Bool("dynlinker", false, "record dynamic linker information in -dynimport mode")
   260  
   261  // This flag is for bootstrapping a new Go implementation,
   262  // to generate Go types that match the data layout and
   263  // constant values used in the host's C libraries and system calls.
   264  var godefs = flag.Bool("godefs", false, "for bootstrap: write Go definitions for C file to standard output")
   265  
   266  var srcDir = flag.String("srcdir", "", "source directory")
   267  var objDir = flag.String("objdir", "", "object directory")
   268  var importPath = flag.String("importpath", "", "import path of package being built (for comments in generated files)")
   269  var exportHeader = flag.String("exportheader", "", "where to write export header if any exported functions")
   270  
   271  var ldflags = flag.String("ldflags", "", "flags to pass to C linker")
   272  
   273  var gccgo = flag.Bool("gccgo", false, "generate files for use with gccgo")
   274  var gccgoprefix = flag.String("gccgoprefix", "", "-fgo-prefix option used with gccgo")
   275  var gccgopkgpath = flag.String("gccgopkgpath", "", "-fgo-pkgpath option used with gccgo")
   276  var gccgoMangler func(string) string
   277  var gccgoDefineCgoIncomplete = flag.Bool("gccgo_define_cgoincomplete", false, "define cgo.Incomplete for older gccgo/GoLLVM")
   278  var importRuntimeCgo = flag.Bool("import_runtime_cgo", true, "import runtime/cgo in generated code")
   279  var importSyscall = flag.Bool("import_syscall", true, "import syscall in generated code")
   280  var trimpath = flag.String("trimpath", "", "applies supplied rewrites or trims prefixes to recorded source file paths")
   281  
   282  var goarch, goos, gomips, gomips64 string
   283  var gccBaseCmd []string
   284  
   285  func main() {
   286  	counter.Open()
   287  	objabi.AddVersionFlag() // -V
   288  	objabi.Flagparse(usage)
   289  	counter.Inc("cgo/invocations")
   290  	counter.CountFlags("cgo/flag:", *flag.CommandLine)
   291  
   292  	if *gccgoDefineCgoIncomplete {
   293  		if !*gccgo {
   294  			fmt.Fprintf(os.Stderr, "cgo: -gccgo_define_cgoincomplete without -gccgo\n")
   295  			os.Exit(2)
   296  		}
   297  		incomplete = "_cgopackage_Incomplete"
   298  	}
   299  
   300  	if *dynobj != "" {
   301  		// cgo -dynimport is essentially a separate helper command
   302  		// built into the cgo binary. It scans a gcc-produced executable
   303  		// and dumps information about the imported symbols and the
   304  		// imported libraries. The 'go build' rules for cgo prepare an
   305  		// appropriate executable and then use its import information
   306  		// instead of needing to make the linkers duplicate all the
   307  		// specialized knowledge gcc has about where to look for imported
   308  		// symbols and which ones to use.
   309  		dynimport(*dynobj)
   310  		return
   311  	}
   312  
   313  	if *godefs {
   314  		// Generating definitions pulled from header files,
   315  		// to be checked into Go repositories.
   316  		// Line numbers are just noise.
   317  		conf.Mode &^= printer.SourcePos
   318  	}
   319  
   320  	args := flag.Args()
   321  	if len(args) < 1 {
   322  		usage()
   323  	}
   324  
   325  	// Find first arg that looks like a go file and assume everything before
   326  	// that are options to pass to gcc.
   327  	var i int
   328  	for i = len(args); i > 0; i-- {
   329  		if !strings.HasSuffix(args[i-1], ".go") {
   330  			break
   331  		}
   332  	}
   333  	if i == len(args) {
   334  		usage()
   335  	}
   336  
   337  	// Save original command line arguments for the godefs generated comment. Relative file
   338  	// paths in os.Args will be rewritten to absolute file paths in the loop below.
   339  	osArgs := make([]string, len(os.Args))
   340  	copy(osArgs, os.Args[:])
   341  	goFiles := args[i:]
   342  
   343  	for _, arg := range args[:i] {
   344  		if arg == "-fsanitize=thread" {
   345  			tsanProlog = yesTsanProlog
   346  		}
   347  		if arg == "-fsanitize=memory" {
   348  			msanProlog = yesMsanProlog
   349  		}
   350  	}
   351  
   352  	p := newPackage(args[:i])
   353  
   354  	// We need a C compiler to be available. Check this.
   355  	var err error
   356  	gccBaseCmd, err = checkGCCBaseCmd()
   357  	if err != nil {
   358  		fatalf("%v", err)
   359  		os.Exit(2)
   360  	}
   361  
   362  	// Record linker flags for external linking.
   363  	if *ldflags != "" {
   364  		args, err := splitQuoted(*ldflags)
   365  		if err != nil {
   366  			fatalf("bad -ldflags option: %q (%s)", *ldflags, err)
   367  		}
   368  		p.addToFlag("LDFLAGS", args)
   369  	}
   370  
   371  	// For backward compatibility for Bazel, record CGO_LDFLAGS
   372  	// from the environment for external linking.
   373  	// This should not happen with cmd/go, which removes CGO_LDFLAGS
   374  	// from the environment when invoking cgo.
   375  	// This can be removed when we no longer need to support
   376  	// older versions of Bazel. See issue #66456 and
   377  	// https://github.com/bazelbuild/rules_go/issues/3979.
   378  	if envFlags := os.Getenv("CGO_LDFLAGS"); envFlags != "" {
   379  		args, err := splitQuoted(envFlags)
   380  		if err != nil {
   381  			fatalf("bad CGO_LDFLAGS: %q (%s)", envFlags, err)
   382  		}
   383  		p.addToFlag("LDFLAGS", args)
   384  	}
   385  
   386  	// Need a unique prefix for the global C symbols that
   387  	// we use to coordinate between gcc and ourselves.
   388  	// We already put _cgo_ at the beginning, so the main
   389  	// concern is other cgo wrappers for the same functions.
   390  	// Use the beginning of the notsha256 of the input to disambiguate.
   391  	h := notsha256.New()
   392  	io.WriteString(h, *importPath)
   393  	fs := make([]*File, len(goFiles))
   394  	for i, input := range goFiles {
   395  		if *srcDir != "" {
   396  			input = filepath.Join(*srcDir, input)
   397  		}
   398  
   399  		// Create absolute path for file, so that it will be used in error
   400  		// messages and recorded in debug line number information.
   401  		// This matches the rest of the toolchain. See golang.org/issue/5122.
   402  		if aname, err := filepath.Abs(input); err == nil {
   403  			input = aname
   404  		}
   405  
   406  		b, err := os.ReadFile(input)
   407  		if err != nil {
   408  			fatalf("%s", err)
   409  		}
   410  		if _, err = h.Write(b); err != nil {
   411  			fatalf("%s", err)
   412  		}
   413  
   414  		// Apply trimpath to the file path. The path won't be read from after this point.
   415  		input, _ = objabi.ApplyRewrites(input, *trimpath)
   416  		if strings.ContainsAny(input, "\r\n") {
   417  			// ParseGo, (*Package).writeOutput, and printer.Fprint in SourcePos mode
   418  			// all emit line directives, which don't permit newlines in the file path.
   419  			// Bail early if we see anything newline-like in the trimmed path.
   420  			fatalf("input path contains newline character: %q", input)
   421  		}
   422  		goFiles[i] = input
   423  
   424  		f := new(File)
   425  		f.Edit = edit.NewBuffer(b)
   426  		f.ParseGo(input, b)
   427  		f.ProcessCgoDirectives()
   428  		fs[i] = f
   429  	}
   430  
   431  	cPrefix = fmt.Sprintf("_%x", h.Sum(nil)[0:6])
   432  
   433  	if *objDir == "" {
   434  		*objDir = "_obj"
   435  	}
   436  	// make sure that `objDir` directory exists, so that we can write
   437  	// all the output files there.
   438  	os.MkdirAll(*objDir, 0o700)
   439  	*objDir += string(filepath.Separator)
   440  
   441  	for i, input := range goFiles {
   442  		f := fs[i]
   443  		p.Translate(f)
   444  		for _, cref := range f.Ref {
   445  			switch cref.Context {
   446  			case ctxCall, ctxCall2:
   447  				if cref.Name.Kind != "type" {
   448  					break
   449  				}
   450  				old := *cref.Expr
   451  				*cref.Expr = cref.Name.Type.Go
   452  				f.Edit.Replace(f.offset(old.Pos()), f.offset(old.End()), gofmt(cref.Name.Type.Go))
   453  			}
   454  		}
   455  		if nerrors > 0 {
   456  			os.Exit(2)
   457  		}
   458  		p.PackagePath = f.Package
   459  		p.Record(f)
   460  		if *godefs {
   461  			os.Stdout.WriteString(p.godefs(f, osArgs))
   462  		} else {
   463  			p.writeOutput(f, input)
   464  		}
   465  	}
   466  	cFunctions := make(map[string]bool)
   467  	for _, key := range nameKeys(p.Name) {
   468  		n := p.Name[key]
   469  		if n.FuncType != nil {
   470  			cFunctions[n.C] = true
   471  		}
   472  	}
   473  
   474  	for funcName := range p.noEscapes {
   475  		if _, found := cFunctions[funcName]; !found {
   476  			error_(token.NoPos, "#cgo noescape %s: no matched C function", funcName)
   477  		}
   478  	}
   479  
   480  	for funcName := range p.noCallbacks {
   481  		if _, found := cFunctions[funcName]; !found {
   482  			error_(token.NoPos, "#cgo nocallback %s: no matched C function", funcName)
   483  		}
   484  	}
   485  
   486  	if !*godefs {
   487  		p.writeDefs()
   488  	}
   489  	if nerrors > 0 {
   490  		os.Exit(2)
   491  	}
   492  }
   493  
   494  // newPackage returns a new Package that will invoke
   495  // gcc with the additional arguments specified in args.
   496  func newPackage(args []string) *Package {
   497  	goarch = runtime.GOARCH
   498  	if s := os.Getenv("GOARCH"); s != "" {
   499  		goarch = s
   500  	}
   501  	goos = runtime.GOOS
   502  	if s := os.Getenv("GOOS"); s != "" {
   503  		goos = s
   504  	}
   505  	buildcfg.Check()
   506  	gomips = buildcfg.GOMIPS
   507  	gomips64 = buildcfg.GOMIPS64
   508  	ptrSize := ptrSizeMap[goarch]
   509  	if ptrSize == 0 {
   510  		fatalf("unknown ptrSize for $GOARCH %q", goarch)
   511  	}
   512  	intSize := intSizeMap[goarch]
   513  	if intSize == 0 {
   514  		fatalf("unknown intSize for $GOARCH %q", goarch)
   515  	}
   516  
   517  	// Reset locale variables so gcc emits English errors [sic].
   518  	os.Setenv("LANG", "en_US.UTF-8")
   519  	os.Setenv("LC_ALL", "C")
   520  
   521  	p := &Package{
   522  		PtrSize:     ptrSize,
   523  		IntSize:     intSize,
   524  		Written:     make(map[string]bool),
   525  		noCallbacks: make(map[string]bool),
   526  		noEscapes:   make(map[string]bool),
   527  	}
   528  	p.addToFlag("CFLAGS", args)
   529  	return p
   530  }
   531  
   532  // Record what needs to be recorded about f.
   533  func (p *Package) Record(f *File) {
   534  	if p.PackageName == "" {
   535  		p.PackageName = f.Package
   536  	} else if p.PackageName != f.Package {
   537  		error_(token.NoPos, "inconsistent package names: %s, %s", p.PackageName, f.Package)
   538  	}
   539  
   540  	if p.Name == nil {
   541  		p.Name = f.Name
   542  	} else {
   543  		// Merge the new file's names in with the existing names.
   544  		for k, v := range f.Name {
   545  			if p.Name[k] == nil {
   546  				// Never seen before, just save it.
   547  				p.Name[k] = v
   548  			} else if p.incompleteTypedef(p.Name[k].Type) && p.Name[k].FuncType == nil {
   549  				// Old one is incomplete, just use new one.
   550  				p.Name[k] = v
   551  			} else if p.incompleteTypedef(v.Type) && v.FuncType == nil {
   552  				// New one is incomplete, just use old one.
   553  				// Nothing to do.
   554  			} else if _, ok := nameToC[k]; ok {
   555  				// Names we predefine may appear inconsistent
   556  				// if some files typedef them and some don't.
   557  				// Issue 26743.
   558  			} else if !reflect.DeepEqual(p.Name[k], v) {
   559  				// We don't require strict func type equality, because some functions
   560  				// can have things like typedef'd arguments that are equivalent to
   561  				// the standard arguments. e.g.
   562  				//     int usleep(unsigned);
   563  				//     int usleep(useconds_t);
   564  				// So we just check size/alignment of arguments. At least that
   565  				// avoids problems like those in #67670 and #67699.
   566  				ok := false
   567  				ft1 := p.Name[k].FuncType
   568  				ft2 := v.FuncType
   569  				if ft1.fuzzyMatch(ft2) {
   570  					// Retry DeepEqual with the FuncType field cleared.
   571  					x1 := *p.Name[k]
   572  					x2 := *v
   573  					x1.FuncType = nil
   574  					x2.FuncType = nil
   575  					if reflect.DeepEqual(&x1, &x2) {
   576  						ok = true
   577  					}
   578  				}
   579  				if !ok {
   580  					error_(token.NoPos, "inconsistent definitions for C.%s", fixGo(k))
   581  				}
   582  			}
   583  		}
   584  	}
   585  
   586  	// merge nocallback & noescape
   587  	for k, v := range f.NoCallbacks {
   588  		p.noCallbacks[k] = v
   589  	}
   590  	for k, v := range f.NoEscapes {
   591  		p.noEscapes[k] = v
   592  	}
   593  
   594  	if f.ExpFunc != nil {
   595  		p.ExpFunc = append(p.ExpFunc, f.ExpFunc...)
   596  		p.Preamble += "\n" + f.Preamble
   597  	}
   598  	p.Decl = append(p.Decl, f.AST.Decls...)
   599  }
   600  
   601  // incompleteTypedef reports whether t appears to be an incomplete
   602  // typedef definition.
   603  func (p *Package) incompleteTypedef(t *Type) bool {
   604  	return t == nil || (t.Size == 0 && t.Align == -1)
   605  }
   606  

View as plain text