Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/cgo/main.go

Documentation: cmd/cgo

     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 gmp.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  	"crypto/md5"
    15  	"flag"
    16  	"fmt"
    17  	"go/ast"
    18  	"go/printer"
    19  	"go/token"
    20  	"io"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"reflect"
    25  	"runtime"
    26  	"sort"
    27  	"strings"
    28  
    29  	"cmd/internal/edit"
    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  	NotInHeap  bool // this type should have a go:notinheap annotation
   156  }
   157  
   158  // A FuncType collects information about a function type in both the C and Go worlds.
   159  type FuncType struct {
   160  	Params []*Type
   161  	Result *Type
   162  	Go     *ast.FuncType
   163  }
   164  
   165  func usage() {
   166  	fmt.Fprint(os.Stderr, "usage: cgo -- [compiler options] file.go ...\n")
   167  	flag.PrintDefaults()
   168  	os.Exit(2)
   169  }
   170  
   171  var ptrSizeMap = map[string]int64{
   172  	"386":      4,
   173  	"alpha":    8,
   174  	"amd64":    8,
   175  	"arm":      4,
   176  	"arm64":    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  	"m68k":     4,
   203  	"mips":     4,
   204  	"mipsle":   4,
   205  	"mips64":   8,
   206  	"mips64le": 8,
   207  	"nios2":    4,
   208  	"ppc":      4,
   209  	"ppc64":    8,
   210  	"ppc64le":  8,
   211  	"riscv":    4,
   212  	"riscv64":  8,
   213  	"s390":     4,
   214  	"s390x":    8,
   215  	"sh":       4,
   216  	"shbe":     4,
   217  	"sparc":    4,
   218  	"sparc64":  8,
   219  }
   220  
   221  var cPrefix string
   222  
   223  var fset = token.NewFileSet()
   224  
   225  var dynobj = flag.String("dynimport", "", "if non-empty, print dynamic import data for that file")
   226  var dynout = flag.String("dynout", "", "write -dynimport output to this file")
   227  var dynpackage = flag.String("dynpackage", "main", "set Go package for -dynimport output")
   228  var dynlinker = flag.Bool("dynlinker", false, "record dynamic linker information in -dynimport mode")
   229  
   230  // This flag is for bootstrapping a new Go implementation,
   231  // to generate Go types that match the data layout and
   232  // constant values used in the host's C libraries and system calls.
   233  var godefs = flag.Bool("godefs", false, "for bootstrap: write Go definitions for C file to standard output")
   234  
   235  var srcDir = flag.String("srcdir", "", "source directory")
   236  var objDir = flag.String("objdir", "", "object directory")
   237  var importPath = flag.String("importpath", "", "import path of package being built (for comments in generated files)")
   238  var exportHeader = flag.String("exportheader", "", "where to write export header if any exported functions")
   239  
   240  var gccgo = flag.Bool("gccgo", false, "generate files for use with gccgo")
   241  var gccgoprefix = flag.String("gccgoprefix", "", "-fgo-prefix option used with gccgo")
   242  var gccgopkgpath = flag.String("gccgopkgpath", "", "-fgo-pkgpath option used with gccgo")
   243  var gccgoMangler func(string) string
   244  var importRuntimeCgo = flag.Bool("import_runtime_cgo", true, "import runtime/cgo in generated code")
   245  var importSyscall = flag.Bool("import_syscall", true, "import syscall in generated code")
   246  var trimpath = flag.String("trimpath", "", "applies supplied rewrites or trims prefixes to recorded source file paths")
   247  
   248  var goarch, goos string
   249  
   250  func main() {
   251  	objabi.AddVersionFlag() // -V
   252  	flag.Usage = usage
   253  	flag.Parse()
   254  
   255  	if *dynobj != "" {
   256  		// cgo -dynimport is essentially a separate helper command
   257  		// built into the cgo binary. It scans a gcc-produced executable
   258  		// and dumps information about the imported symbols and the
   259  		// imported libraries. The 'go build' rules for cgo prepare an
   260  		// appropriate executable and then use its import information
   261  		// instead of needing to make the linkers duplicate all the
   262  		// specialized knowledge gcc has about where to look for imported
   263  		// symbols and which ones to use.
   264  		dynimport(*dynobj)
   265  		return
   266  	}
   267  
   268  	if *godefs {
   269  		// Generating definitions pulled from header files,
   270  		// to be checked into Go repositories.
   271  		// Line numbers are just noise.
   272  		conf.Mode &^= printer.SourcePos
   273  	}
   274  
   275  	args := flag.Args()
   276  	if len(args) < 1 {
   277  		usage()
   278  	}
   279  
   280  	// Find first arg that looks like a go file and assume everything before
   281  	// that are options to pass to gcc.
   282  	var i int
   283  	for i = len(args); i > 0; i-- {
   284  		if !strings.HasSuffix(args[i-1], ".go") {
   285  			break
   286  		}
   287  	}
   288  	if i == len(args) {
   289  		usage()
   290  	}
   291  
   292  	goFiles := args[i:]
   293  
   294  	for _, arg := range args[:i] {
   295  		if arg == "-fsanitize=thread" {
   296  			tsanProlog = yesTsanProlog
   297  		}
   298  		if arg == "-fsanitize=memory" {
   299  			msanProlog = yesMsanProlog
   300  		}
   301  	}
   302  
   303  	p := newPackage(args[:i])
   304  
   305  	// Record CGO_LDFLAGS from the environment for external linking.
   306  	if ldflags := os.Getenv("CGO_LDFLAGS"); ldflags != "" {
   307  		args, err := splitQuoted(ldflags)
   308  		if err != nil {
   309  			fatalf("bad CGO_LDFLAGS: %q (%s)", ldflags, err)
   310  		}
   311  		p.addToFlag("LDFLAGS", args)
   312  	}
   313  
   314  	// Need a unique prefix for the global C symbols that
   315  	// we use to coordinate between gcc and ourselves.
   316  	// We already put _cgo_ at the beginning, so the main
   317  	// concern is other cgo wrappers for the same functions.
   318  	// Use the beginning of the md5 of the input to disambiguate.
   319  	h := md5.New()
   320  	io.WriteString(h, *importPath)
   321  	fs := make([]*File, len(goFiles))
   322  	for i, input := range goFiles {
   323  		if *srcDir != "" {
   324  			input = filepath.Join(*srcDir, input)
   325  		}
   326  
   327  		// Create absolute path for file, so that it will be used in error
   328  		// messages and recorded in debug line number information.
   329  		// This matches the rest of the toolchain. See golang.org/issue/5122.
   330  		if aname, err := filepath.Abs(input); err == nil {
   331  			input = aname
   332  		}
   333  
   334  		b, err := ioutil.ReadFile(input)
   335  		if err != nil {
   336  			fatalf("%s", err)
   337  		}
   338  		if _, err = h.Write(b); err != nil {
   339  			fatalf("%s", err)
   340  		}
   341  
   342  		// Apply trimpath to the file path. The path won't be read from after this point.
   343  		input, _ = objabi.ApplyRewrites(input, *trimpath)
   344  		goFiles[i] = input
   345  
   346  		f := new(File)
   347  		f.Edit = edit.NewBuffer(b)
   348  		f.ParseGo(input, b)
   349  		f.DiscardCgoDirectives()
   350  		fs[i] = f
   351  	}
   352  
   353  	cPrefix = fmt.Sprintf("_%x", h.Sum(nil)[0:6])
   354  
   355  	if *objDir == "" {
   356  		// make sure that _obj directory exists, so that we can write
   357  		// all the output files there.
   358  		os.Mkdir("_obj", 0777)
   359  		*objDir = "_obj"
   360  	}
   361  	*objDir += string(filepath.Separator)
   362  
   363  	for i, input := range goFiles {
   364  		f := fs[i]
   365  		p.Translate(f)
   366  		for _, cref := range f.Ref {
   367  			switch cref.Context {
   368  			case ctxCall, ctxCall2:
   369  				if cref.Name.Kind != "type" {
   370  					break
   371  				}
   372  				old := *cref.Expr
   373  				*cref.Expr = cref.Name.Type.Go
   374  				f.Edit.Replace(f.offset(old.Pos()), f.offset(old.End()), gofmt(cref.Name.Type.Go))
   375  			}
   376  		}
   377  		if nerrors > 0 {
   378  			os.Exit(2)
   379  		}
   380  		p.PackagePath = f.Package
   381  		p.Record(f)
   382  		if *godefs {
   383  			os.Stdout.WriteString(p.godefs(f))
   384  		} else {
   385  			p.writeOutput(f, input)
   386  		}
   387  	}
   388  
   389  	if !*godefs {
   390  		p.writeDefs()
   391  	}
   392  	if nerrors > 0 {
   393  		os.Exit(2)
   394  	}
   395  }
   396  
   397  // newPackage returns a new Package that will invoke
   398  // gcc with the additional arguments specified in args.
   399  func newPackage(args []string) *Package {
   400  	goarch = runtime.GOARCH
   401  	if s := os.Getenv("GOARCH"); s != "" {
   402  		goarch = s
   403  	}
   404  	goos = runtime.GOOS
   405  	if s := os.Getenv("GOOS"); s != "" {
   406  		goos = s
   407  	}
   408  	ptrSize := ptrSizeMap[goarch]
   409  	if ptrSize == 0 {
   410  		fatalf("unknown ptrSize for $GOARCH %q", goarch)
   411  	}
   412  	intSize := intSizeMap[goarch]
   413  	if intSize == 0 {
   414  		fatalf("unknown intSize for $GOARCH %q", goarch)
   415  	}
   416  
   417  	// Reset locale variables so gcc emits English errors [sic].
   418  	os.Setenv("LANG", "en_US.UTF-8")
   419  	os.Setenv("LC_ALL", "C")
   420  
   421  	p := &Package{
   422  		PtrSize:  ptrSize,
   423  		IntSize:  intSize,
   424  		CgoFlags: make(map[string][]string),
   425  		Written:  make(map[string]bool),
   426  	}
   427  	p.addToFlag("CFLAGS", args)
   428  	return p
   429  }
   430  
   431  // Record what needs to be recorded about f.
   432  func (p *Package) Record(f *File) {
   433  	if p.PackageName == "" {
   434  		p.PackageName = f.Package
   435  	} else if p.PackageName != f.Package {
   436  		error_(token.NoPos, "inconsistent package names: %s, %s", p.PackageName, f.Package)
   437  	}
   438  
   439  	if p.Name == nil {
   440  		p.Name = f.Name
   441  	} else {
   442  		for k, v := range f.Name {
   443  			if p.Name[k] == nil {
   444  				p.Name[k] = v
   445  			} else if p.incompleteTypedef(p.Name[k].Type) {
   446  				p.Name[k] = v
   447  			} else if p.incompleteTypedef(v.Type) {
   448  				// Nothing to do.
   449  			} else if _, ok := nameToC[k]; ok {
   450  				// Names we predefine may appear inconsistent
   451  				// if some files typedef them and some don't.
   452  				// Issue 26743.
   453  			} else if !reflect.DeepEqual(p.Name[k], v) {
   454  				error_(token.NoPos, "inconsistent definitions for C.%s", fixGo(k))
   455  			}
   456  		}
   457  	}
   458  
   459  	if f.ExpFunc != nil {
   460  		p.ExpFunc = append(p.ExpFunc, f.ExpFunc...)
   461  		p.Preamble += "\n" + f.Preamble
   462  	}
   463  	p.Decl = append(p.Decl, f.AST.Decls...)
   464  }
   465  
   466  // incompleteTypedef reports whether t appears to be an incomplete
   467  // typedef definition.
   468  func (p *Package) incompleteTypedef(t *Type) bool {
   469  	return t == nil || (t.Size == 0 && t.Align == -1)
   470  }
   471  

View as plain text