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

View as plain text