Source file src/cmd/cgo/out.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  package main
     6  
     7  import (
     8  	"bytes"
     9  	"cmd/internal/pkgpath"
    10  	"debug/elf"
    11  	"debug/macho"
    12  	"debug/pe"
    13  	"fmt"
    14  	"go/ast"
    15  	"go/printer"
    16  	"go/token"
    17  	"internal/xcoff"
    18  	"io"
    19  	"os"
    20  	"os/exec"
    21  	"path/filepath"
    22  	"regexp"
    23  	"sort"
    24  	"strings"
    25  	"unicode"
    26  )
    27  
    28  var (
    29  	conf         = printer.Config{Mode: printer.SourcePos, Tabwidth: 8}
    30  	noSourceConf = printer.Config{Tabwidth: 8}
    31  )
    32  
    33  // writeDefs creates output files to be compiled by gc and gcc.
    34  func (p *Package) writeDefs() {
    35  	var fgo2, fc io.Writer
    36  	f := creat(*objDir + "_cgo_gotypes.go")
    37  	defer f.Close()
    38  	fgo2 = f
    39  	if *gccgo {
    40  		f := creat(*objDir + "_cgo_defun.c")
    41  		defer f.Close()
    42  		fc = f
    43  	}
    44  	fm := creat(*objDir + "_cgo_main.c")
    45  
    46  	var gccgoInit strings.Builder
    47  
    48  	fflg := creat(*objDir + "_cgo_flags")
    49  	for k, v := range p.CgoFlags {
    50  		fmt.Fprintf(fflg, "_CGO_%s=%s\n", k, strings.Join(v, " "))
    51  		if k == "LDFLAGS" && !*gccgo {
    52  			for _, arg := range v {
    53  				fmt.Fprintf(fgo2, "//go:cgo_ldflag %q\n", arg)
    54  			}
    55  		}
    56  	}
    57  	fflg.Close()
    58  
    59  	// Write C main file for using gcc to resolve imports.
    60  	fmt.Fprintf(fm, "#include <stddef.h>\n") // For size_t below.
    61  	fmt.Fprintf(fm, "int main() { return 0; }\n")
    62  	if *importRuntimeCgo {
    63  		fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*) __attribute__((unused)), void *a __attribute__((unused)), int c __attribute__((unused)), size_t ctxt __attribute__((unused))) { }\n")
    64  		fmt.Fprintf(fm, "size_t _cgo_wait_runtime_init_done(void) { return 0; }\n")
    65  		fmt.Fprintf(fm, "void _cgo_release_context(size_t ctxt __attribute__((unused))) { }\n")
    66  		fmt.Fprintf(fm, "char* _cgo_topofstack(void) { return (char*)0; }\n")
    67  	} else {
    68  		// If we're not importing runtime/cgo, we *are* runtime/cgo,
    69  		// which provides these functions. We just need a prototype.
    70  		fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*), void *a, int c, size_t ctxt);\n")
    71  		fmt.Fprintf(fm, "size_t _cgo_wait_runtime_init_done(void);\n")
    72  		fmt.Fprintf(fm, "void _cgo_release_context(size_t);\n")
    73  	}
    74  	fmt.Fprintf(fm, "void _cgo_allocate(void *a __attribute__((unused)), int c __attribute__((unused))) { }\n")
    75  	fmt.Fprintf(fm, "void _cgo_panic(void *a __attribute__((unused)), int c __attribute__((unused))) { }\n")
    76  	fmt.Fprintf(fm, "void _cgo_reginit(void) { }\n")
    77  
    78  	// Write second Go output: definitions of _C_xxx.
    79  	// In a separate file so that the import of "unsafe" does not
    80  	// pollute the original file.
    81  	fmt.Fprintf(fgo2, "// Code generated by cmd/cgo; DO NOT EDIT.\n\n")
    82  	fmt.Fprintf(fgo2, "package %s\n\n", p.PackageName)
    83  	fmt.Fprintf(fgo2, "import \"unsafe\"\n\n")
    84  	if *importSyscall {
    85  		fmt.Fprintf(fgo2, "import \"syscall\"\n\n")
    86  	}
    87  	if *importRuntimeCgo {
    88  		if !*gccgoDefineCgoIncomplete {
    89  			fmt.Fprintf(fgo2, "import _cgopackage \"runtime/cgo\"\n\n")
    90  			fmt.Fprintf(fgo2, "type _ _cgopackage.Incomplete\n") // prevent import-not-used error
    91  		} else {
    92  			fmt.Fprintf(fgo2, "//go:notinheap\n")
    93  			fmt.Fprintf(fgo2, "type _cgopackage_Incomplete struct{ _ struct{ _ struct{} } }\n")
    94  		}
    95  	}
    96  	if *importSyscall {
    97  		fmt.Fprintf(fgo2, "var _ syscall.Errno\n")
    98  	}
    99  	fmt.Fprintf(fgo2, "func _Cgo_ptr(ptr unsafe.Pointer) unsafe.Pointer { return ptr }\n\n")
   100  
   101  	if !*gccgo {
   102  		fmt.Fprintf(fgo2, "//go:linkname _Cgo_always_false runtime.cgoAlwaysFalse\n")
   103  		fmt.Fprintf(fgo2, "var _Cgo_always_false bool\n")
   104  		fmt.Fprintf(fgo2, "//go:linkname _Cgo_use runtime.cgoUse\n")
   105  		fmt.Fprintf(fgo2, "func _Cgo_use(interface{})\n")
   106  	}
   107  
   108  	typedefNames := make([]string, 0, len(typedef))
   109  	for name := range typedef {
   110  		if name == "_Ctype_void" {
   111  			// We provide an appropriate declaration for
   112  			// _Ctype_void below (#39877).
   113  			continue
   114  		}
   115  		typedefNames = append(typedefNames, name)
   116  	}
   117  	sort.Strings(typedefNames)
   118  	for _, name := range typedefNames {
   119  		def := typedef[name]
   120  		fmt.Fprintf(fgo2, "type %s ", name)
   121  		// We don't have source info for these types, so write them out without source info.
   122  		// Otherwise types would look like:
   123  		//
   124  		// type _Ctype_struct_cb struct {
   125  		// //line :1
   126  		//        on_test *[0]byte
   127  		// //line :1
   128  		// }
   129  		//
   130  		// Which is not useful. Moreover we never override source info,
   131  		// so subsequent source code uses the same source info.
   132  		// Moreover, empty file name makes compile emit no source debug info at all.
   133  		var buf bytes.Buffer
   134  		noSourceConf.Fprint(&buf, fset, def.Go)
   135  		if bytes.HasPrefix(buf.Bytes(), []byte("_Ctype_")) ||
   136  			strings.HasPrefix(name, "_Ctype_enum_") ||
   137  			strings.HasPrefix(name, "_Ctype_union_") {
   138  			// This typedef is of the form `typedef a b` and should be an alias.
   139  			fmt.Fprintf(fgo2, "= ")
   140  		}
   141  		fmt.Fprintf(fgo2, "%s", buf.Bytes())
   142  		fmt.Fprintf(fgo2, "\n\n")
   143  	}
   144  	if *gccgo {
   145  		fmt.Fprintf(fgo2, "type _Ctype_void byte\n")
   146  	} else {
   147  		fmt.Fprintf(fgo2, "type _Ctype_void [0]byte\n")
   148  	}
   149  
   150  	if *gccgo {
   151  		fmt.Fprint(fgo2, gccgoGoProlog)
   152  		fmt.Fprint(fc, p.cPrologGccgo())
   153  	} else {
   154  		fmt.Fprint(fgo2, goProlog)
   155  	}
   156  
   157  	if fc != nil {
   158  		fmt.Fprintf(fc, "#line 1 \"cgo-generated-wrappers\"\n")
   159  	}
   160  	if fm != nil {
   161  		fmt.Fprintf(fm, "#line 1 \"cgo-generated-wrappers\"\n")
   162  	}
   163  
   164  	gccgoSymbolPrefix := p.gccgoSymbolPrefix()
   165  
   166  	cVars := make(map[string]bool)
   167  	for _, key := range nameKeys(p.Name) {
   168  		n := p.Name[key]
   169  		if !n.IsVar() {
   170  			continue
   171  		}
   172  
   173  		if !cVars[n.C] {
   174  			if *gccgo {
   175  				fmt.Fprintf(fc, "extern byte *%s;\n", n.C)
   176  			} else {
   177  				// Force a reference to all symbols so that
   178  				// the external linker will add DT_NEEDED
   179  				// entries as needed on ELF systems.
   180  				// Treat function variables differently
   181  				// to avoid type conflict errors from LTO
   182  				// (Link Time Optimization).
   183  				if n.Kind == "fpvar" {
   184  					fmt.Fprintf(fm, "extern void %s();\n", n.C)
   185  				} else {
   186  					fmt.Fprintf(fm, "extern char %s[];\n", n.C)
   187  					fmt.Fprintf(fm, "void *_cgohack_%s = %s;\n\n", n.C, n.C)
   188  				}
   189  				fmt.Fprintf(fgo2, "//go:linkname __cgo_%s %s\n", n.C, n.C)
   190  				fmt.Fprintf(fgo2, "//go:cgo_import_static %s\n", n.C)
   191  				fmt.Fprintf(fgo2, "var __cgo_%s byte\n", n.C)
   192  			}
   193  			cVars[n.C] = true
   194  		}
   195  
   196  		var node ast.Node
   197  		if n.Kind == "var" {
   198  			node = &ast.StarExpr{X: n.Type.Go}
   199  		} else if n.Kind == "fpvar" {
   200  			node = n.Type.Go
   201  		} else {
   202  			panic(fmt.Errorf("invalid var kind %q", n.Kind))
   203  		}
   204  		if *gccgo {
   205  			fmt.Fprintf(fc, `extern void *%s __asm__("%s.%s");`, n.Mangle, gccgoSymbolPrefix, gccgoToSymbol(n.Mangle))
   206  			fmt.Fprintf(&gccgoInit, "\t%s = &%s;\n", n.Mangle, n.C)
   207  			fmt.Fprintf(fc, "\n")
   208  		}
   209  
   210  		fmt.Fprintf(fgo2, "var %s ", n.Mangle)
   211  		conf.Fprint(fgo2, fset, node)
   212  		if !*gccgo {
   213  			fmt.Fprintf(fgo2, " = (")
   214  			conf.Fprint(fgo2, fset, node)
   215  			fmt.Fprintf(fgo2, ")(unsafe.Pointer(&__cgo_%s))", n.C)
   216  		}
   217  		fmt.Fprintf(fgo2, "\n")
   218  	}
   219  	if *gccgo {
   220  		fmt.Fprintf(fc, "\n")
   221  	}
   222  
   223  	for _, key := range nameKeys(p.Name) {
   224  		n := p.Name[key]
   225  		if n.Const != "" {
   226  			fmt.Fprintf(fgo2, "const %s = %s\n", n.Mangle, n.Const)
   227  		}
   228  	}
   229  	fmt.Fprintf(fgo2, "\n")
   230  
   231  	callsMalloc := false
   232  	for _, key := range nameKeys(p.Name) {
   233  		n := p.Name[key]
   234  		if n.FuncType != nil {
   235  			p.writeDefsFunc(fgo2, n, &callsMalloc)
   236  		}
   237  	}
   238  
   239  	fgcc := creat(*objDir + "_cgo_export.c")
   240  	fgcch := creat(*objDir + "_cgo_export.h")
   241  	if *gccgo {
   242  		p.writeGccgoExports(fgo2, fm, fgcc, fgcch)
   243  	} else {
   244  		p.writeExports(fgo2, fm, fgcc, fgcch)
   245  	}
   246  
   247  	if callsMalloc && !*gccgo {
   248  		fmt.Fprint(fgo2, strings.Replace(cMallocDefGo, "PREFIX", cPrefix, -1))
   249  		fmt.Fprint(fgcc, strings.Replace(strings.Replace(cMallocDefC, "PREFIX", cPrefix, -1), "PACKED", p.packedAttribute(), -1))
   250  	}
   251  
   252  	if err := fgcc.Close(); err != nil {
   253  		fatalf("%s", err)
   254  	}
   255  	if err := fgcch.Close(); err != nil {
   256  		fatalf("%s", err)
   257  	}
   258  
   259  	if *exportHeader != "" && len(p.ExpFunc) > 0 {
   260  		fexp := creat(*exportHeader)
   261  		fgcch, err := os.Open(*objDir + "_cgo_export.h")
   262  		if err != nil {
   263  			fatalf("%s", err)
   264  		}
   265  		defer fgcch.Close()
   266  		_, err = io.Copy(fexp, fgcch)
   267  		if err != nil {
   268  			fatalf("%s", err)
   269  		}
   270  		if err = fexp.Close(); err != nil {
   271  			fatalf("%s", err)
   272  		}
   273  	}
   274  
   275  	init := gccgoInit.String()
   276  	if init != "" {
   277  		// The init function does nothing but simple
   278  		// assignments, so it won't use much stack space, so
   279  		// it's OK to not split the stack. Splitting the stack
   280  		// can run into a bug in clang (as of 2018-11-09):
   281  		// this is a leaf function, and when clang sees a leaf
   282  		// function it won't emit the split stack prologue for
   283  		// the function. However, if this function refers to a
   284  		// non-split-stack function, which will happen if the
   285  		// cgo code refers to a C function not compiled with
   286  		// -fsplit-stack, then the linker will think that it
   287  		// needs to adjust the split stack prologue, but there
   288  		// won't be one. Marking the function explicitly
   289  		// no_split_stack works around this problem by telling
   290  		// the linker that it's OK if there is no split stack
   291  		// prologue.
   292  		fmt.Fprintln(fc, "static void init(void) __attribute__ ((constructor, no_split_stack));")
   293  		fmt.Fprintln(fc, "static void init(void) {")
   294  		fmt.Fprint(fc, init)
   295  		fmt.Fprintln(fc, "}")
   296  	}
   297  }
   298  
   299  // elfImportedSymbols is like elf.File.ImportedSymbols, but it
   300  // includes weak symbols.
   301  //
   302  // A bug in some versions of LLD (at least LLD 8) cause it to emit
   303  // several pthreads symbols as weak, but we need to import those. See
   304  // issue #31912 or https://bugs.llvm.org/show_bug.cgi?id=42442.
   305  //
   306  // When doing external linking, we hand everything off to the external
   307  // linker, which will create its own dynamic symbol tables. For
   308  // internal linking, this may turn weak imports into strong imports,
   309  // which could cause dynamic linking to fail if a symbol really isn't
   310  // defined. However, the standard library depends on everything it
   311  // imports, and this is the primary use of dynamic symbol tables with
   312  // internal linking.
   313  func elfImportedSymbols(f *elf.File) []elf.ImportedSymbol {
   314  	syms, _ := f.DynamicSymbols()
   315  	var imports []elf.ImportedSymbol
   316  	for _, s := range syms {
   317  		if (elf.ST_BIND(s.Info) == elf.STB_GLOBAL || elf.ST_BIND(s.Info) == elf.STB_WEAK) && s.Section == elf.SHN_UNDEF {
   318  			imports = append(imports, elf.ImportedSymbol{
   319  				Name:    s.Name,
   320  				Library: s.Library,
   321  				Version: s.Version,
   322  			})
   323  		}
   324  	}
   325  	return imports
   326  }
   327  
   328  func dynimport(obj string) {
   329  	stdout := os.Stdout
   330  	if *dynout != "" {
   331  		f, err := os.Create(*dynout)
   332  		if err != nil {
   333  			fatalf("%s", err)
   334  		}
   335  		stdout = f
   336  	}
   337  
   338  	fmt.Fprintf(stdout, "package %s\n", *dynpackage)
   339  
   340  	if f, err := elf.Open(obj); err == nil {
   341  		if *dynlinker {
   342  			// Emit the cgo_dynamic_linker line.
   343  			if sec := f.Section(".interp"); sec != nil {
   344  				if data, err := sec.Data(); err == nil && len(data) > 1 {
   345  					// skip trailing \0 in data
   346  					fmt.Fprintf(stdout, "//go:cgo_dynamic_linker %q\n", string(data[:len(data)-1]))
   347  				}
   348  			}
   349  		}
   350  		sym := elfImportedSymbols(f)
   351  		for _, s := range sym {
   352  			targ := s.Name
   353  			if s.Version != "" {
   354  				targ += "#" + s.Version
   355  			}
   356  			checkImportSymName(s.Name)
   357  			checkImportSymName(targ)
   358  			fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s.Name, targ, s.Library)
   359  		}
   360  		lib, _ := f.ImportedLibraries()
   361  		for _, l := range lib {
   362  			fmt.Fprintf(stdout, "//go:cgo_import_dynamic _ _ %q\n", l)
   363  		}
   364  		return
   365  	}
   366  
   367  	if f, err := macho.Open(obj); err == nil {
   368  		sym, _ := f.ImportedSymbols()
   369  		for _, s := range sym {
   370  			if len(s) > 0 && s[0] == '_' {
   371  				s = s[1:]
   372  			}
   373  			checkImportSymName(s)
   374  			fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s, s, "")
   375  		}
   376  		lib, _ := f.ImportedLibraries()
   377  		for _, l := range lib {
   378  			fmt.Fprintf(stdout, "//go:cgo_import_dynamic _ _ %q\n", l)
   379  		}
   380  		return
   381  	}
   382  
   383  	if f, err := pe.Open(obj); err == nil {
   384  		sym, _ := f.ImportedSymbols()
   385  		for _, s := range sym {
   386  			ss := strings.Split(s, ":")
   387  			name := strings.Split(ss[0], "@")[0]
   388  			checkImportSymName(name)
   389  			checkImportSymName(ss[0])
   390  			fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", name, ss[0], strings.ToLower(ss[1]))
   391  		}
   392  		return
   393  	}
   394  
   395  	if f, err := xcoff.Open(obj); err == nil {
   396  		sym, err := f.ImportedSymbols()
   397  		if err != nil {
   398  			fatalf("cannot load imported symbols from XCOFF file %s: %v", obj, err)
   399  		}
   400  		for _, s := range sym {
   401  			if s.Name == "runtime_rt0_go" || s.Name == "_rt0_ppc64_aix_lib" {
   402  				// These symbols are imported by runtime/cgo but
   403  				// must not be added to _cgo_import.go as there are
   404  				// Go symbols.
   405  				continue
   406  			}
   407  			checkImportSymName(s.Name)
   408  			fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s.Name, s.Name, s.Library)
   409  		}
   410  		lib, err := f.ImportedLibraries()
   411  		if err != nil {
   412  			fatalf("cannot load imported libraries from XCOFF file %s: %v", obj, err)
   413  		}
   414  		for _, l := range lib {
   415  			fmt.Fprintf(stdout, "//go:cgo_import_dynamic _ _ %q\n", l)
   416  		}
   417  		return
   418  	}
   419  
   420  	fatalf("cannot parse %s as ELF, Mach-O, PE or XCOFF", obj)
   421  }
   422  
   423  // checkImportSymName checks a symbol name we are going to emit as part
   424  // of a //go:cgo_import_dynamic pragma. These names come from object
   425  // files, so they may be corrupt. We are going to emit them unquoted,
   426  // so while they don't need to be valid symbol names (and in some cases,
   427  // involving symbol versions, they won't be) they must contain only
   428  // graphic characters and must not contain Go comments.
   429  func checkImportSymName(s string) {
   430  	for _, c := range s {
   431  		if !unicode.IsGraphic(c) || unicode.IsSpace(c) {
   432  			fatalf("dynamic symbol %q contains unsupported character", s)
   433  		}
   434  	}
   435  	if strings.Contains(s, "//") || strings.Contains(s, "/*") {
   436  		fatalf("dynamic symbol %q contains Go comment")
   437  	}
   438  }
   439  
   440  // Construct a gcc struct matching the gc argument frame.
   441  // Assumes that in gcc, char is 1 byte, short 2 bytes, int 4 bytes, long long 8 bytes.
   442  // These assumptions are checked by the gccProlog.
   443  // Also assumes that gc convention is to word-align the
   444  // input and output parameters.
   445  func (p *Package) structType(n *Name) (string, int64) {
   446  	var buf strings.Builder
   447  	fmt.Fprint(&buf, "struct {\n")
   448  	off := int64(0)
   449  	for i, t := range n.FuncType.Params {
   450  		if off%t.Align != 0 {
   451  			pad := t.Align - off%t.Align
   452  			fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad)
   453  			off += pad
   454  		}
   455  		c := t.Typedef
   456  		if c == "" {
   457  			c = t.C.String()
   458  		}
   459  		fmt.Fprintf(&buf, "\t\t%s p%d;\n", c, i)
   460  		off += t.Size
   461  	}
   462  	if off%p.PtrSize != 0 {
   463  		pad := p.PtrSize - off%p.PtrSize
   464  		fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad)
   465  		off += pad
   466  	}
   467  	if t := n.FuncType.Result; t != nil {
   468  		if off%t.Align != 0 {
   469  			pad := t.Align - off%t.Align
   470  			fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad)
   471  			off += pad
   472  		}
   473  		fmt.Fprintf(&buf, "\t\t%s r;\n", t.C)
   474  		off += t.Size
   475  	}
   476  	if off%p.PtrSize != 0 {
   477  		pad := p.PtrSize - off%p.PtrSize
   478  		fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad)
   479  		off += pad
   480  	}
   481  	if off == 0 {
   482  		fmt.Fprintf(&buf, "\t\tchar unused;\n") // avoid empty struct
   483  	}
   484  	fmt.Fprintf(&buf, "\t}")
   485  	return buf.String(), off
   486  }
   487  
   488  func (p *Package) writeDefsFunc(fgo2 io.Writer, n *Name, callsMalloc *bool) {
   489  	name := n.Go
   490  	gtype := n.FuncType.Go
   491  	void := gtype.Results == nil || len(gtype.Results.List) == 0
   492  	if n.AddError {
   493  		// Add "error" to return type list.
   494  		// Type list is known to be 0 or 1 element - it's a C function.
   495  		err := &ast.Field{Type: ast.NewIdent("error")}
   496  		l := gtype.Results.List
   497  		if len(l) == 0 {
   498  			l = []*ast.Field{err}
   499  		} else {
   500  			l = []*ast.Field{l[0], err}
   501  		}
   502  		t := new(ast.FuncType)
   503  		*t = *gtype
   504  		t.Results = &ast.FieldList{List: l}
   505  		gtype = t
   506  	}
   507  
   508  	// Go func declaration.
   509  	d := &ast.FuncDecl{
   510  		Name: ast.NewIdent(n.Mangle),
   511  		Type: gtype,
   512  	}
   513  
   514  	// Builtins defined in the C prolog.
   515  	inProlog := builtinDefs[name] != ""
   516  	cname := fmt.Sprintf("_cgo%s%s", cPrefix, n.Mangle)
   517  	paramnames := []string(nil)
   518  	if d.Type.Params != nil {
   519  		for i, param := range d.Type.Params.List {
   520  			paramName := fmt.Sprintf("p%d", i)
   521  			param.Names = []*ast.Ident{ast.NewIdent(paramName)}
   522  			paramnames = append(paramnames, paramName)
   523  		}
   524  	}
   525  
   526  	if *gccgo {
   527  		// Gccgo style hooks.
   528  		fmt.Fprint(fgo2, "\n")
   529  		conf.Fprint(fgo2, fset, d)
   530  		fmt.Fprint(fgo2, " {\n")
   531  		if !inProlog {
   532  			fmt.Fprint(fgo2, "\tdefer syscall.CgocallDone()\n")
   533  			fmt.Fprint(fgo2, "\tsyscall.Cgocall()\n")
   534  		}
   535  		if n.AddError {
   536  			fmt.Fprint(fgo2, "\tsyscall.SetErrno(0)\n")
   537  		}
   538  		fmt.Fprint(fgo2, "\t")
   539  		if !void {
   540  			fmt.Fprint(fgo2, "r := ")
   541  		}
   542  		fmt.Fprintf(fgo2, "%s(%s)\n", cname, strings.Join(paramnames, ", "))
   543  
   544  		if n.AddError {
   545  			fmt.Fprint(fgo2, "\te := syscall.GetErrno()\n")
   546  			fmt.Fprint(fgo2, "\tif e != 0 {\n")
   547  			fmt.Fprint(fgo2, "\t\treturn ")
   548  			if !void {
   549  				fmt.Fprint(fgo2, "r, ")
   550  			}
   551  			fmt.Fprint(fgo2, "e\n")
   552  			fmt.Fprint(fgo2, "\t}\n")
   553  			fmt.Fprint(fgo2, "\treturn ")
   554  			if !void {
   555  				fmt.Fprint(fgo2, "r, ")
   556  			}
   557  			fmt.Fprint(fgo2, "nil\n")
   558  		} else if !void {
   559  			fmt.Fprint(fgo2, "\treturn r\n")
   560  		}
   561  
   562  		fmt.Fprint(fgo2, "}\n")
   563  
   564  		// declare the C function.
   565  		fmt.Fprintf(fgo2, "//extern %s\n", cname)
   566  		d.Name = ast.NewIdent(cname)
   567  		if n.AddError {
   568  			l := d.Type.Results.List
   569  			d.Type.Results.List = l[:len(l)-1]
   570  		}
   571  		conf.Fprint(fgo2, fset, d)
   572  		fmt.Fprint(fgo2, "\n")
   573  
   574  		return
   575  	}
   576  
   577  	if inProlog {
   578  		fmt.Fprint(fgo2, builtinDefs[name])
   579  		if strings.Contains(builtinDefs[name], "_cgo_cmalloc") {
   580  			*callsMalloc = true
   581  		}
   582  		return
   583  	}
   584  
   585  	// Wrapper calls into gcc, passing a pointer to the argument frame.
   586  	fmt.Fprintf(fgo2, "//go:cgo_import_static %s\n", cname)
   587  	fmt.Fprintf(fgo2, "//go:linkname __cgofn_%s %s\n", cname, cname)
   588  	fmt.Fprintf(fgo2, "var __cgofn_%s byte\n", cname)
   589  	fmt.Fprintf(fgo2, "var %s = unsafe.Pointer(&__cgofn_%s)\n", cname, cname)
   590  
   591  	nret := 0
   592  	if !void {
   593  		d.Type.Results.List[0].Names = []*ast.Ident{ast.NewIdent("r1")}
   594  		nret = 1
   595  	}
   596  	if n.AddError {
   597  		d.Type.Results.List[nret].Names = []*ast.Ident{ast.NewIdent("r2")}
   598  	}
   599  
   600  	fmt.Fprint(fgo2, "\n")
   601  	fmt.Fprint(fgo2, "//go:cgo_unsafe_args\n")
   602  	conf.Fprint(fgo2, fset, d)
   603  	fmt.Fprint(fgo2, " {\n")
   604  
   605  	// NOTE: Using uintptr to hide from escape analysis.
   606  	arg := "0"
   607  	if len(paramnames) > 0 {
   608  		arg = "uintptr(unsafe.Pointer(&p0))"
   609  	} else if !void {
   610  		arg = "uintptr(unsafe.Pointer(&r1))"
   611  	}
   612  
   613  	prefix := ""
   614  	if n.AddError {
   615  		prefix = "errno := "
   616  	}
   617  	fmt.Fprintf(fgo2, "\t%s_cgo_runtime_cgocall(%s, %s)\n", prefix, cname, arg)
   618  	if n.AddError {
   619  		fmt.Fprintf(fgo2, "\tif errno != 0 { r2 = syscall.Errno(errno) }\n")
   620  	}
   621  	fmt.Fprintf(fgo2, "\tif _Cgo_always_false {\n")
   622  	if d.Type.Params != nil {
   623  		for i := range d.Type.Params.List {
   624  			fmt.Fprintf(fgo2, "\t\t_Cgo_use(p%d)\n", i)
   625  		}
   626  	}
   627  	fmt.Fprintf(fgo2, "\t}\n")
   628  	fmt.Fprintf(fgo2, "\treturn\n")
   629  	fmt.Fprintf(fgo2, "}\n")
   630  }
   631  
   632  // writeOutput creates stubs for a specific source file to be compiled by gc
   633  func (p *Package) writeOutput(f *File, srcfile string) {
   634  	base := srcfile
   635  	base = strings.TrimSuffix(base, ".go")
   636  	base = filepath.Base(base)
   637  	fgo1 := creat(*objDir + base + ".cgo1.go")
   638  	fgcc := creat(*objDir + base + ".cgo2.c")
   639  
   640  	p.GoFiles = append(p.GoFiles, base+".cgo1.go")
   641  	p.GccFiles = append(p.GccFiles, base+".cgo2.c")
   642  
   643  	// Write Go output: Go input with rewrites of C.xxx to _C_xxx.
   644  	fmt.Fprintf(fgo1, "// Code generated by cmd/cgo; DO NOT EDIT.\n\n")
   645  	fmt.Fprintf(fgo1, "//line %s:1:1\n", srcfile)
   646  	fgo1.Write(f.Edit.Bytes())
   647  
   648  	// While we process the vars and funcs, also write gcc output.
   649  	// Gcc output starts with the preamble.
   650  	fmt.Fprintf(fgcc, "%s\n", builtinProlog)
   651  	fmt.Fprintf(fgcc, "%s\n", f.Preamble)
   652  	fmt.Fprintf(fgcc, "%s\n", gccProlog)
   653  	fmt.Fprintf(fgcc, "%s\n", tsanProlog)
   654  	fmt.Fprintf(fgcc, "%s\n", msanProlog)
   655  
   656  	for _, key := range nameKeys(f.Name) {
   657  		n := f.Name[key]
   658  		if n.FuncType != nil {
   659  			p.writeOutputFunc(fgcc, n)
   660  		}
   661  	}
   662  
   663  	fgo1.Close()
   664  	fgcc.Close()
   665  }
   666  
   667  // fixGo converts the internal Name.Go field into the name we should show
   668  // to users in error messages. There's only one for now: on input we rewrite
   669  // C.malloc into C._CMalloc, so change it back here.
   670  func fixGo(name string) string {
   671  	if name == "_CMalloc" {
   672  		return "malloc"
   673  	}
   674  	return name
   675  }
   676  
   677  var isBuiltin = map[string]bool{
   678  	"_Cfunc_CString":   true,
   679  	"_Cfunc_CBytes":    true,
   680  	"_Cfunc_GoString":  true,
   681  	"_Cfunc_GoStringN": true,
   682  	"_Cfunc_GoBytes":   true,
   683  	"_Cfunc__CMalloc":  true,
   684  }
   685  
   686  func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
   687  	name := n.Mangle
   688  	if isBuiltin[name] || p.Written[name] {
   689  		// The builtins are already defined in the C prolog, and we don't
   690  		// want to duplicate function definitions we've already done.
   691  		return
   692  	}
   693  	p.Written[name] = true
   694  
   695  	if *gccgo {
   696  		p.writeGccgoOutputFunc(fgcc, n)
   697  		return
   698  	}
   699  
   700  	ctype, _ := p.structType(n)
   701  
   702  	// Gcc wrapper unpacks the C argument struct
   703  	// and calls the actual C function.
   704  	fmt.Fprintf(fgcc, "CGO_NO_SANITIZE_THREAD\n")
   705  	if n.AddError {
   706  		fmt.Fprintf(fgcc, "int\n")
   707  	} else {
   708  		fmt.Fprintf(fgcc, "void\n")
   709  	}
   710  	fmt.Fprintf(fgcc, "_cgo%s%s(void *v)\n", cPrefix, n.Mangle)
   711  	fmt.Fprintf(fgcc, "{\n")
   712  	if n.AddError {
   713  		fmt.Fprintf(fgcc, "\tint _cgo_errno;\n")
   714  	}
   715  	// We're trying to write a gcc struct that matches gc's layout.
   716  	// Use packed attribute to force no padding in this struct in case
   717  	// gcc has different packing requirements.
   718  	fmt.Fprintf(fgcc, "\t%s %v *_cgo_a = v;\n", ctype, p.packedAttribute())
   719  	if n.FuncType.Result != nil {
   720  		// Save the stack top for use below.
   721  		fmt.Fprintf(fgcc, "\tchar *_cgo_stktop = _cgo_topofstack();\n")
   722  	}
   723  	tr := n.FuncType.Result
   724  	if tr != nil {
   725  		fmt.Fprintf(fgcc, "\t__typeof__(_cgo_a->r) _cgo_r;\n")
   726  	}
   727  	fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
   728  	if n.AddError {
   729  		fmt.Fprintf(fgcc, "\terrno = 0;\n")
   730  	}
   731  	fmt.Fprintf(fgcc, "\t")
   732  	if tr != nil {
   733  		fmt.Fprintf(fgcc, "_cgo_r = ")
   734  		if c := tr.C.String(); c[len(c)-1] == '*' {
   735  			fmt.Fprint(fgcc, "(__typeof__(_cgo_a->r)) ")
   736  		}
   737  	}
   738  	if n.Kind == "macro" {
   739  		fmt.Fprintf(fgcc, "%s;\n", n.C)
   740  	} else {
   741  		fmt.Fprintf(fgcc, "%s(", n.C)
   742  		for i := range n.FuncType.Params {
   743  			if i > 0 {
   744  				fmt.Fprintf(fgcc, ", ")
   745  			}
   746  			fmt.Fprintf(fgcc, "_cgo_a->p%d", i)
   747  		}
   748  		fmt.Fprintf(fgcc, ");\n")
   749  	}
   750  	if n.AddError {
   751  		fmt.Fprintf(fgcc, "\t_cgo_errno = errno;\n")
   752  	}
   753  	fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n")
   754  	if n.FuncType.Result != nil {
   755  		// The cgo call may have caused a stack copy (via a callback).
   756  		// Adjust the return value pointer appropriately.
   757  		fmt.Fprintf(fgcc, "\t_cgo_a = (void*)((char*)_cgo_a + (_cgo_topofstack() - _cgo_stktop));\n")
   758  		// Save the return value.
   759  		fmt.Fprintf(fgcc, "\t_cgo_a->r = _cgo_r;\n")
   760  		// The return value is on the Go stack. If we are using msan,
   761  		// and if the C value is partially or completely uninitialized,
   762  		// the assignment will mark the Go stack as uninitialized.
   763  		// The Go compiler does not update msan for changes to the
   764  		// stack. It is possible that the stack will remain
   765  		// uninitialized, and then later be used in a way that is
   766  		// visible to msan, possibly leading to a false positive.
   767  		// Mark the stack space as written, to avoid this problem.
   768  		// See issue 26209.
   769  		fmt.Fprintf(fgcc, "\t_cgo_msan_write(&_cgo_a->r, sizeof(_cgo_a->r));\n")
   770  	}
   771  	if n.AddError {
   772  		fmt.Fprintf(fgcc, "\treturn _cgo_errno;\n")
   773  	}
   774  	fmt.Fprintf(fgcc, "}\n")
   775  	fmt.Fprintf(fgcc, "\n")
   776  }
   777  
   778  // Write out a wrapper for a function when using gccgo. This is a
   779  // simple wrapper that just calls the real function. We only need a
   780  // wrapper to support static functions in the prologue--without a
   781  // wrapper, we can't refer to the function, since the reference is in
   782  // a different file.
   783  func (p *Package) writeGccgoOutputFunc(fgcc *os.File, n *Name) {
   784  	fmt.Fprintf(fgcc, "CGO_NO_SANITIZE_THREAD\n")
   785  	if t := n.FuncType.Result; t != nil {
   786  		fmt.Fprintf(fgcc, "%s\n", t.C.String())
   787  	} else {
   788  		fmt.Fprintf(fgcc, "void\n")
   789  	}
   790  	fmt.Fprintf(fgcc, "_cgo%s%s(", cPrefix, n.Mangle)
   791  	for i, t := range n.FuncType.Params {
   792  		if i > 0 {
   793  			fmt.Fprintf(fgcc, ", ")
   794  		}
   795  		c := t.Typedef
   796  		if c == "" {
   797  			c = t.C.String()
   798  		}
   799  		fmt.Fprintf(fgcc, "%s p%d", c, i)
   800  	}
   801  	fmt.Fprintf(fgcc, ")\n")
   802  	fmt.Fprintf(fgcc, "{\n")
   803  	if t := n.FuncType.Result; t != nil {
   804  		fmt.Fprintf(fgcc, "\t%s _cgo_r;\n", t.C.String())
   805  	}
   806  	fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
   807  	fmt.Fprintf(fgcc, "\t")
   808  	if t := n.FuncType.Result; t != nil {
   809  		fmt.Fprintf(fgcc, "_cgo_r = ")
   810  		// Cast to void* to avoid warnings due to omitted qualifiers.
   811  		if c := t.C.String(); c[len(c)-1] == '*' {
   812  			fmt.Fprintf(fgcc, "(void*)")
   813  		}
   814  	}
   815  	if n.Kind == "macro" {
   816  		fmt.Fprintf(fgcc, "%s;\n", n.C)
   817  	} else {
   818  		fmt.Fprintf(fgcc, "%s(", n.C)
   819  		for i := range n.FuncType.Params {
   820  			if i > 0 {
   821  				fmt.Fprintf(fgcc, ", ")
   822  			}
   823  			fmt.Fprintf(fgcc, "p%d", i)
   824  		}
   825  		fmt.Fprintf(fgcc, ");\n")
   826  	}
   827  	fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n")
   828  	if t := n.FuncType.Result; t != nil {
   829  		fmt.Fprintf(fgcc, "\treturn ")
   830  		// Cast to void* to avoid warnings due to omitted qualifiers
   831  		// and explicit incompatible struct types.
   832  		if c := t.C.String(); c[len(c)-1] == '*' {
   833  			fmt.Fprintf(fgcc, "(void*)")
   834  		}
   835  		fmt.Fprintf(fgcc, "_cgo_r;\n")
   836  	}
   837  	fmt.Fprintf(fgcc, "}\n")
   838  	fmt.Fprintf(fgcc, "\n")
   839  }
   840  
   841  // packedAttribute returns host compiler struct attribute that will be
   842  // used to match gc's struct layout. For example, on 386 Windows,
   843  // gcc wants to 8-align int64s, but gc does not.
   844  // Use __gcc_struct__ to work around https://gcc.gnu.org/PR52991 on x86,
   845  // and https://golang.org/issue/5603.
   846  func (p *Package) packedAttribute() string {
   847  	s := "__attribute__((__packed__"
   848  	if !p.GccIsClang && (goarch == "amd64" || goarch == "386") {
   849  		s += ", __gcc_struct__"
   850  	}
   851  	return s + "))"
   852  }
   853  
   854  // exportParamName returns the value of param as it should be
   855  // displayed in a c header file. If param contains any non-ASCII
   856  // characters, this function will return the character p followed by
   857  // the value of position; otherwise, this function will return the
   858  // value of param.
   859  func exportParamName(param string, position int) string {
   860  	if param == "" {
   861  		return fmt.Sprintf("p%d", position)
   862  	}
   863  
   864  	pname := param
   865  
   866  	for i := 0; i < len(param); i++ {
   867  		if param[i] > unicode.MaxASCII {
   868  			pname = fmt.Sprintf("p%d", position)
   869  			break
   870  		}
   871  	}
   872  
   873  	return pname
   874  }
   875  
   876  // Write out the various stubs we need to support functions exported
   877  // from Go so that they are callable from C.
   878  func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
   879  	p.writeExportHeader(fgcch)
   880  
   881  	fmt.Fprintf(fgcc, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n")
   882  	fmt.Fprintf(fgcc, "#include <stdlib.h>\n")
   883  	fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n\n")
   884  
   885  	// We use packed structs, but they are always aligned.
   886  	// The pragmas and address-of-packed-member are only recognized as
   887  	// warning groups in clang 4.0+, so ignore unknown pragmas first.
   888  	fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n")
   889  	fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Wpragmas\"\n")
   890  	fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Waddress-of-packed-member\"\n")
   891  
   892  	fmt.Fprintf(fgcc, "extern void crosscall2(void (*fn)(void *), void *, int, size_t);\n")
   893  	fmt.Fprintf(fgcc, "extern size_t _cgo_wait_runtime_init_done(void);\n")
   894  	fmt.Fprintf(fgcc, "extern void _cgo_release_context(size_t);\n\n")
   895  	fmt.Fprintf(fgcc, "extern char* _cgo_topofstack(void);")
   896  	fmt.Fprintf(fgcc, "%s\n", tsanProlog)
   897  	fmt.Fprintf(fgcc, "%s\n", msanProlog)
   898  
   899  	for _, exp := range p.ExpFunc {
   900  		fn := exp.Func
   901  
   902  		// Construct a struct that will be used to communicate
   903  		// arguments from C to Go. The C and Go definitions
   904  		// just have to agree. The gcc struct will be compiled
   905  		// with __attribute__((packed)) so all padding must be
   906  		// accounted for explicitly.
   907  		ctype := "struct {\n"
   908  		gotype := new(bytes.Buffer)
   909  		fmt.Fprintf(gotype, "struct {\n")
   910  		off := int64(0)
   911  		npad := 0
   912  		argField := func(typ ast.Expr, namePat string, args ...interface{}) {
   913  			name := fmt.Sprintf(namePat, args...)
   914  			t := p.cgoType(typ)
   915  			if off%t.Align != 0 {
   916  				pad := t.Align - off%t.Align
   917  				ctype += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
   918  				off += pad
   919  				npad++
   920  			}
   921  			ctype += fmt.Sprintf("\t\t%s %s;\n", t.C, name)
   922  			fmt.Fprintf(gotype, "\t\t%s ", name)
   923  			noSourceConf.Fprint(gotype, fset, typ)
   924  			fmt.Fprintf(gotype, "\n")
   925  			off += t.Size
   926  		}
   927  		if fn.Recv != nil {
   928  			argField(fn.Recv.List[0].Type, "recv")
   929  		}
   930  		fntype := fn.Type
   931  		forFieldList(fntype.Params,
   932  			func(i int, aname string, atype ast.Expr) {
   933  				argField(atype, "p%d", i)
   934  			})
   935  		forFieldList(fntype.Results,
   936  			func(i int, aname string, atype ast.Expr) {
   937  				argField(atype, "r%d", i)
   938  			})
   939  		if ctype == "struct {\n" {
   940  			ctype += "\t\tchar unused;\n" // avoid empty struct
   941  		}
   942  		ctype += "\t}"
   943  		fmt.Fprintf(gotype, "\t}")
   944  
   945  		// Get the return type of the wrapper function
   946  		// compiled by gcc.
   947  		gccResult := ""
   948  		if fntype.Results == nil || len(fntype.Results.List) == 0 {
   949  			gccResult = "void"
   950  		} else if len(fntype.Results.List) == 1 && len(fntype.Results.List[0].Names) <= 1 {
   951  			gccResult = p.cgoType(fntype.Results.List[0].Type).C.String()
   952  		} else {
   953  			fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
   954  			fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName)
   955  			forFieldList(fntype.Results,
   956  				func(i int, aname string, atype ast.Expr) {
   957  					fmt.Fprintf(fgcch, "\t%s r%d;", p.cgoType(atype).C, i)
   958  					if len(aname) > 0 {
   959  						fmt.Fprintf(fgcch, " /* %s */", aname)
   960  					}
   961  					fmt.Fprint(fgcch, "\n")
   962  				})
   963  			fmt.Fprintf(fgcch, "};\n")
   964  			gccResult = "struct " + exp.ExpName + "_return"
   965  		}
   966  
   967  		// Build the wrapper function compiled by gcc.
   968  		gccExport := ""
   969  		if goos == "windows" {
   970  			gccExport = "__declspec(dllexport) "
   971  		}
   972  		s := fmt.Sprintf("%s%s %s(", gccExport, gccResult, exp.ExpName)
   973  		if fn.Recv != nil {
   974  			s += p.cgoType(fn.Recv.List[0].Type).C.String()
   975  			s += " recv"
   976  		}
   977  		forFieldList(fntype.Params,
   978  			func(i int, aname string, atype ast.Expr) {
   979  				if i > 0 || fn.Recv != nil {
   980  					s += ", "
   981  				}
   982  				s += fmt.Sprintf("%s %s", p.cgoType(atype).C, exportParamName(aname, i))
   983  			})
   984  		s += ")"
   985  
   986  		if len(exp.Doc) > 0 {
   987  			fmt.Fprintf(fgcch, "\n%s", exp.Doc)
   988  			if !strings.HasSuffix(exp.Doc, "\n") {
   989  				fmt.Fprint(fgcch, "\n")
   990  			}
   991  		}
   992  		fmt.Fprintf(fgcch, "extern %s;\n", s)
   993  
   994  		fmt.Fprintf(fgcc, "extern void _cgoexp%s_%s(void *);\n", cPrefix, exp.ExpName)
   995  		fmt.Fprintf(fgcc, "\nCGO_NO_SANITIZE_THREAD")
   996  		fmt.Fprintf(fgcc, "\n%s\n", s)
   997  		fmt.Fprintf(fgcc, "{\n")
   998  		fmt.Fprintf(fgcc, "\tsize_t _cgo_ctxt = _cgo_wait_runtime_init_done();\n")
   999  		// The results part of the argument structure must be
  1000  		// initialized to 0 so the write barriers generated by
  1001  		// the assignments to these fields in Go are safe.
  1002  		//
  1003  		// We use a local static variable to get the zeroed
  1004  		// value of the argument type. This avoids including
  1005  		// string.h for memset, and is also robust to C++
  1006  		// types with constructors. Both GCC and LLVM optimize
  1007  		// this into just zeroing _cgo_a.
  1008  		fmt.Fprintf(fgcc, "\ttypedef %s %v _cgo_argtype;\n", ctype, p.packedAttribute())
  1009  		fmt.Fprintf(fgcc, "\tstatic _cgo_argtype _cgo_zero;\n")
  1010  		fmt.Fprintf(fgcc, "\t_cgo_argtype _cgo_a = _cgo_zero;\n")
  1011  		if gccResult != "void" && (len(fntype.Results.List) > 1 || len(fntype.Results.List[0].Names) > 1) {
  1012  			fmt.Fprintf(fgcc, "\t%s r;\n", gccResult)
  1013  		}
  1014  		if fn.Recv != nil {
  1015  			fmt.Fprintf(fgcc, "\t_cgo_a.recv = recv;\n")
  1016  		}
  1017  		forFieldList(fntype.Params,
  1018  			func(i int, aname string, atype ast.Expr) {
  1019  				fmt.Fprintf(fgcc, "\t_cgo_a.p%d = %s;\n", i, exportParamName(aname, i))
  1020  			})
  1021  		fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n")
  1022  		fmt.Fprintf(fgcc, "\tcrosscall2(_cgoexp%s_%s, &_cgo_a, %d, _cgo_ctxt);\n", cPrefix, exp.ExpName, off)
  1023  		fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
  1024  		fmt.Fprintf(fgcc, "\t_cgo_release_context(_cgo_ctxt);\n")
  1025  		if gccResult != "void" {
  1026  			if len(fntype.Results.List) == 1 && len(fntype.Results.List[0].Names) <= 1 {
  1027  				fmt.Fprintf(fgcc, "\treturn _cgo_a.r0;\n")
  1028  			} else {
  1029  				forFieldList(fntype.Results,
  1030  					func(i int, aname string, atype ast.Expr) {
  1031  						fmt.Fprintf(fgcc, "\tr.r%d = _cgo_a.r%d;\n", i, i)
  1032  					})
  1033  				fmt.Fprintf(fgcc, "\treturn r;\n")
  1034  			}
  1035  		}
  1036  		fmt.Fprintf(fgcc, "}\n")
  1037  
  1038  		// In internal linking mode, the Go linker sees both
  1039  		// the C wrapper written above and the Go wrapper it
  1040  		// references. Hence, export the C wrapper (e.g., for
  1041  		// if we're building a shared object). The Go linker
  1042  		// will resolve the C wrapper's reference to the Go
  1043  		// wrapper without a separate export.
  1044  		fmt.Fprintf(fgo2, "//go:cgo_export_dynamic %s\n", exp.ExpName)
  1045  		// cgo_export_static refers to a symbol by its linker
  1046  		// name, so set the linker name of the Go wrapper.
  1047  		fmt.Fprintf(fgo2, "//go:linkname _cgoexp%s_%s _cgoexp%s_%s\n", cPrefix, exp.ExpName, cPrefix, exp.ExpName)
  1048  		// In external linking mode, the Go linker sees the Go
  1049  		// wrapper, but not the C wrapper. For this case,
  1050  		// export the Go wrapper so the host linker can
  1051  		// resolve the reference from the C wrapper to the Go
  1052  		// wrapper.
  1053  		fmt.Fprintf(fgo2, "//go:cgo_export_static _cgoexp%s_%s\n", cPrefix, exp.ExpName)
  1054  
  1055  		// Build the wrapper function compiled by cmd/compile.
  1056  		// This unpacks the argument struct above and calls the Go function.
  1057  		fmt.Fprintf(fgo2, "func _cgoexp%s_%s(a *%s) {\n", cPrefix, exp.ExpName, gotype)
  1058  
  1059  		fmt.Fprintf(fm, "void _cgoexp%s_%s(void* p){}\n", cPrefix, exp.ExpName)
  1060  
  1061  		fmt.Fprintf(fgo2, "\t")
  1062  
  1063  		if gccResult != "void" {
  1064  			// Write results back to frame.
  1065  			forFieldList(fntype.Results,
  1066  				func(i int, aname string, atype ast.Expr) {
  1067  					if i > 0 {
  1068  						fmt.Fprintf(fgo2, ", ")
  1069  					}
  1070  					fmt.Fprintf(fgo2, "a.r%d", i)
  1071  				})
  1072  			fmt.Fprintf(fgo2, " = ")
  1073  		}
  1074  		if fn.Recv != nil {
  1075  			fmt.Fprintf(fgo2, "a.recv.")
  1076  		}
  1077  		fmt.Fprintf(fgo2, "%s(", exp.Func.Name)
  1078  		forFieldList(fntype.Params,
  1079  			func(i int, aname string, atype ast.Expr) {
  1080  				if i > 0 {
  1081  					fmt.Fprint(fgo2, ", ")
  1082  				}
  1083  				fmt.Fprintf(fgo2, "a.p%d", i)
  1084  			})
  1085  		fmt.Fprint(fgo2, ")\n")
  1086  		if gccResult != "void" {
  1087  			// Verify that any results don't contain any
  1088  			// Go pointers.
  1089  			forFieldList(fntype.Results,
  1090  				func(i int, aname string, atype ast.Expr) {
  1091  					if !p.hasPointer(nil, atype, false) {
  1092  						return
  1093  					}
  1094  					fmt.Fprintf(fgo2, "\t_cgoCheckResult(a.r%d)\n", i)
  1095  				})
  1096  		}
  1097  		fmt.Fprint(fgo2, "}\n")
  1098  	}
  1099  
  1100  	fmt.Fprintf(fgcch, "%s", gccExportHeaderEpilog)
  1101  }
  1102  
  1103  // Write out the C header allowing C code to call exported gccgo functions.
  1104  func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
  1105  	gccgoSymbolPrefix := p.gccgoSymbolPrefix()
  1106  
  1107  	p.writeExportHeader(fgcch)
  1108  
  1109  	fmt.Fprintf(fgcc, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n")
  1110  	fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n")
  1111  
  1112  	fmt.Fprintf(fgcc, "%s\n", gccgoExportFileProlog)
  1113  	fmt.Fprintf(fgcc, "%s\n", tsanProlog)
  1114  	fmt.Fprintf(fgcc, "%s\n", msanProlog)
  1115  
  1116  	for _, exp := range p.ExpFunc {
  1117  		fn := exp.Func
  1118  		fntype := fn.Type
  1119  
  1120  		cdeclBuf := new(strings.Builder)
  1121  		resultCount := 0
  1122  		forFieldList(fntype.Results,
  1123  			func(i int, aname string, atype ast.Expr) { resultCount++ })
  1124  		switch resultCount {
  1125  		case 0:
  1126  			fmt.Fprintf(cdeclBuf, "void")
  1127  		case 1:
  1128  			forFieldList(fntype.Results,
  1129  				func(i int, aname string, atype ast.Expr) {
  1130  					t := p.cgoType(atype)
  1131  					fmt.Fprintf(cdeclBuf, "%s", t.C)
  1132  				})
  1133  		default:
  1134  			// Declare a result struct.
  1135  			fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
  1136  			fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName)
  1137  			forFieldList(fntype.Results,
  1138  				func(i int, aname string, atype ast.Expr) {
  1139  					t := p.cgoType(atype)
  1140  					fmt.Fprintf(fgcch, "\t%s r%d;", t.C, i)
  1141  					if len(aname) > 0 {
  1142  						fmt.Fprintf(fgcch, " /* %s */", aname)
  1143  					}
  1144  					fmt.Fprint(fgcch, "\n")
  1145  				})
  1146  			fmt.Fprintf(fgcch, "};\n")
  1147  			fmt.Fprintf(cdeclBuf, "struct %s_return", exp.ExpName)
  1148  		}
  1149  
  1150  		cRet := cdeclBuf.String()
  1151  
  1152  		cdeclBuf = new(strings.Builder)
  1153  		fmt.Fprintf(cdeclBuf, "(")
  1154  		if fn.Recv != nil {
  1155  			fmt.Fprintf(cdeclBuf, "%s recv", p.cgoType(fn.Recv.List[0].Type).C.String())
  1156  		}
  1157  		// Function parameters.
  1158  		forFieldList(fntype.Params,
  1159  			func(i int, aname string, atype ast.Expr) {
  1160  				if i > 0 || fn.Recv != nil {
  1161  					fmt.Fprintf(cdeclBuf, ", ")
  1162  				}
  1163  				t := p.cgoType(atype)
  1164  				fmt.Fprintf(cdeclBuf, "%s p%d", t.C, i)
  1165  			})
  1166  		fmt.Fprintf(cdeclBuf, ")")
  1167  		cParams := cdeclBuf.String()
  1168  
  1169  		if len(exp.Doc) > 0 {
  1170  			fmt.Fprintf(fgcch, "\n%s", exp.Doc)
  1171  		}
  1172  
  1173  		fmt.Fprintf(fgcch, "extern %s %s%s;\n", cRet, exp.ExpName, cParams)
  1174  
  1175  		// We need to use a name that will be exported by the
  1176  		// Go code; otherwise gccgo will make it static and we
  1177  		// will not be able to link against it from the C
  1178  		// code.
  1179  		goName := "Cgoexp_" + exp.ExpName
  1180  		fmt.Fprintf(fgcc, `extern %s %s %s __asm__("%s.%s");`, cRet, goName, cParams, gccgoSymbolPrefix, gccgoToSymbol(goName))
  1181  		fmt.Fprint(fgcc, "\n")
  1182  
  1183  		fmt.Fprint(fgcc, "\nCGO_NO_SANITIZE_THREAD\n")
  1184  		fmt.Fprintf(fgcc, "%s %s %s {\n", cRet, exp.ExpName, cParams)
  1185  		if resultCount > 0 {
  1186  			fmt.Fprintf(fgcc, "\t%s r;\n", cRet)
  1187  		}
  1188  		fmt.Fprintf(fgcc, "\tif(_cgo_wait_runtime_init_done)\n")
  1189  		fmt.Fprintf(fgcc, "\t\t_cgo_wait_runtime_init_done();\n")
  1190  		fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n")
  1191  		fmt.Fprint(fgcc, "\t")
  1192  		if resultCount > 0 {
  1193  			fmt.Fprint(fgcc, "r = ")
  1194  		}
  1195  		fmt.Fprintf(fgcc, "%s(", goName)
  1196  		if fn.Recv != nil {
  1197  			fmt.Fprint(fgcc, "recv")
  1198  		}
  1199  		forFieldList(fntype.Params,
  1200  			func(i int, aname string, atype ast.Expr) {
  1201  				if i > 0 || fn.Recv != nil {
  1202  					fmt.Fprintf(fgcc, ", ")
  1203  				}
  1204  				fmt.Fprintf(fgcc, "p%d", i)
  1205  			})
  1206  		fmt.Fprint(fgcc, ");\n")
  1207  		fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
  1208  		if resultCount > 0 {
  1209  			fmt.Fprint(fgcc, "\treturn r;\n")
  1210  		}
  1211  		fmt.Fprint(fgcc, "}\n")
  1212  
  1213  		// Dummy declaration for _cgo_main.c
  1214  		fmt.Fprintf(fm, `char %s[1] __asm__("%s.%s");`, goName, gccgoSymbolPrefix, gccgoToSymbol(goName))
  1215  		fmt.Fprint(fm, "\n")
  1216  
  1217  		// For gccgo we use a wrapper function in Go, in order
  1218  		// to call CgocallBack and CgocallBackDone.
  1219  
  1220  		// This code uses printer.Fprint, not conf.Fprint,
  1221  		// because we don't want //line comments in the middle
  1222  		// of the function types.
  1223  		fmt.Fprint(fgo2, "\n")
  1224  		fmt.Fprintf(fgo2, "func %s(", goName)
  1225  		if fn.Recv != nil {
  1226  			fmt.Fprint(fgo2, "recv ")
  1227  			printer.Fprint(fgo2, fset, fn.Recv.List[0].Type)
  1228  		}
  1229  		forFieldList(fntype.Params,
  1230  			func(i int, aname string, atype ast.Expr) {
  1231  				if i > 0 || fn.Recv != nil {
  1232  					fmt.Fprintf(fgo2, ", ")
  1233  				}
  1234  				fmt.Fprintf(fgo2, "p%d ", i)
  1235  				printer.Fprint(fgo2, fset, atype)
  1236  			})
  1237  		fmt.Fprintf(fgo2, ")")
  1238  		if resultCount > 0 {
  1239  			fmt.Fprintf(fgo2, " (")
  1240  			forFieldList(fntype.Results,
  1241  				func(i int, aname string, atype ast.Expr) {
  1242  					if i > 0 {
  1243  						fmt.Fprint(fgo2, ", ")
  1244  					}
  1245  					printer.Fprint(fgo2, fset, atype)
  1246  				})
  1247  			fmt.Fprint(fgo2, ")")
  1248  		}
  1249  		fmt.Fprint(fgo2, " {\n")
  1250  		fmt.Fprint(fgo2, "\tsyscall.CgocallBack()\n")
  1251  		fmt.Fprint(fgo2, "\tdefer syscall.CgocallBackDone()\n")
  1252  		fmt.Fprint(fgo2, "\t")
  1253  		if resultCount > 0 {
  1254  			fmt.Fprint(fgo2, "return ")
  1255  		}
  1256  		if fn.Recv != nil {
  1257  			fmt.Fprint(fgo2, "recv.")
  1258  		}
  1259  		fmt.Fprintf(fgo2, "%s(", exp.Func.Name)
  1260  		forFieldList(fntype.Params,
  1261  			func(i int, aname string, atype ast.Expr) {
  1262  				if i > 0 {
  1263  					fmt.Fprint(fgo2, ", ")
  1264  				}
  1265  				fmt.Fprintf(fgo2, "p%d", i)
  1266  			})
  1267  		fmt.Fprint(fgo2, ")\n")
  1268  		fmt.Fprint(fgo2, "}\n")
  1269  	}
  1270  
  1271  	fmt.Fprintf(fgcch, "%s", gccExportHeaderEpilog)
  1272  }
  1273  
  1274  // writeExportHeader writes out the start of the _cgo_export.h file.
  1275  func (p *Package) writeExportHeader(fgcch io.Writer) {
  1276  	fmt.Fprintf(fgcch, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n")
  1277  	pkg := *importPath
  1278  	if pkg == "" {
  1279  		pkg = p.PackagePath
  1280  	}
  1281  	fmt.Fprintf(fgcch, "/* package %s */\n\n", pkg)
  1282  	fmt.Fprintf(fgcch, "%s\n", builtinExportProlog)
  1283  
  1284  	// Remove absolute paths from #line comments in the preamble.
  1285  	// They aren't useful for people using the header file,
  1286  	// and they mean that the header files change based on the
  1287  	// exact location of GOPATH.
  1288  	re := regexp.MustCompile(`(?m)^(#line\s+\d+\s+")[^"]*[/\\]([^"]*")`)
  1289  	preamble := re.ReplaceAllString(p.Preamble, "$1$2")
  1290  
  1291  	fmt.Fprintf(fgcch, "/* Start of preamble from import \"C\" comments.  */\n\n")
  1292  	fmt.Fprintf(fgcch, "%s\n", preamble)
  1293  	fmt.Fprintf(fgcch, "\n/* End of preamble from import \"C\" comments.  */\n\n")
  1294  
  1295  	fmt.Fprintf(fgcch, "%s\n", p.gccExportHeaderProlog())
  1296  }
  1297  
  1298  // gccgoToSymbol converts a name to a mangled symbol for gccgo.
  1299  func gccgoToSymbol(ppath string) string {
  1300  	if gccgoMangler == nil {
  1301  		var err error
  1302  		cmd := os.Getenv("GCCGO")
  1303  		if cmd == "" {
  1304  			cmd, err = exec.LookPath("gccgo")
  1305  			if err != nil {
  1306  				fatalf("unable to locate gccgo: %v", err)
  1307  			}
  1308  		}
  1309  		gccgoMangler, err = pkgpath.ToSymbolFunc(cmd, *objDir)
  1310  		if err != nil {
  1311  			fatalf("%v", err)
  1312  		}
  1313  	}
  1314  	return gccgoMangler(ppath)
  1315  }
  1316  
  1317  // Return the package prefix when using gccgo.
  1318  func (p *Package) gccgoSymbolPrefix() string {
  1319  	if !*gccgo {
  1320  		return ""
  1321  	}
  1322  
  1323  	if *gccgopkgpath != "" {
  1324  		return gccgoToSymbol(*gccgopkgpath)
  1325  	}
  1326  	if *gccgoprefix == "" && p.PackageName == "main" {
  1327  		return "main"
  1328  	}
  1329  	prefix := gccgoToSymbol(*gccgoprefix)
  1330  	if prefix == "" {
  1331  		prefix = "go"
  1332  	}
  1333  	return prefix + "." + p.PackageName
  1334  }
  1335  
  1336  // Call a function for each entry in an ast.FieldList, passing the
  1337  // index into the list, the name if any, and the type.
  1338  func forFieldList(fl *ast.FieldList, fn func(int, string, ast.Expr)) {
  1339  	if fl == nil {
  1340  		return
  1341  	}
  1342  	i := 0
  1343  	for _, r := range fl.List {
  1344  		if r.Names == nil {
  1345  			fn(i, "", r.Type)
  1346  			i++
  1347  		} else {
  1348  			for _, n := range r.Names {
  1349  				fn(i, n.Name, r.Type)
  1350  				i++
  1351  			}
  1352  		}
  1353  	}
  1354  }
  1355  
  1356  func c(repr string, args ...interface{}) *TypeRepr {
  1357  	return &TypeRepr{repr, args}
  1358  }
  1359  
  1360  // Map predeclared Go types to Type.
  1361  var goTypes = map[string]*Type{
  1362  	"bool":       {Size: 1, Align: 1, C: c("GoUint8")},
  1363  	"byte":       {Size: 1, Align: 1, C: c("GoUint8")},
  1364  	"int":        {Size: 0, Align: 0, C: c("GoInt")},
  1365  	"uint":       {Size: 0, Align: 0, C: c("GoUint")},
  1366  	"rune":       {Size: 4, Align: 4, C: c("GoInt32")},
  1367  	"int8":       {Size: 1, Align: 1, C: c("GoInt8")},
  1368  	"uint8":      {Size: 1, Align: 1, C: c("GoUint8")},
  1369  	"int16":      {Size: 2, Align: 2, C: c("GoInt16")},
  1370  	"uint16":     {Size: 2, Align: 2, C: c("GoUint16")},
  1371  	"int32":      {Size: 4, Align: 4, C: c("GoInt32")},
  1372  	"uint32":     {Size: 4, Align: 4, C: c("GoUint32")},
  1373  	"int64":      {Size: 8, Align: 8, C: c("GoInt64")},
  1374  	"uint64":     {Size: 8, Align: 8, C: c("GoUint64")},
  1375  	"float32":    {Size: 4, Align: 4, C: c("GoFloat32")},
  1376  	"float64":    {Size: 8, Align: 8, C: c("GoFloat64")},
  1377  	"complex64":  {Size: 8, Align: 4, C: c("GoComplex64")},
  1378  	"complex128": {Size: 16, Align: 8, C: c("GoComplex128")},
  1379  }
  1380  
  1381  // Map an ast type to a Type.
  1382  func (p *Package) cgoType(e ast.Expr) *Type {
  1383  	switch t := e.(type) {
  1384  	case *ast.StarExpr:
  1385  		x := p.cgoType(t.X)
  1386  		return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("%s*", x.C)}
  1387  	case *ast.ArrayType:
  1388  		if t.Len == nil {
  1389  			// Slice: pointer, len, cap.
  1390  			return &Type{Size: p.PtrSize * 3, Align: p.PtrSize, C: c("GoSlice")}
  1391  		}
  1392  		// Non-slice array types are not supported.
  1393  	case *ast.StructType:
  1394  		// Not supported.
  1395  	case *ast.FuncType:
  1396  		return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("void*")}
  1397  	case *ast.InterfaceType:
  1398  		return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoInterface")}
  1399  	case *ast.MapType:
  1400  		return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("GoMap")}
  1401  	case *ast.ChanType:
  1402  		return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("GoChan")}
  1403  	case *ast.Ident:
  1404  		goTypesFixup := func(r *Type) *Type {
  1405  			if r.Size == 0 { // int or uint
  1406  				rr := new(Type)
  1407  				*rr = *r
  1408  				rr.Size = p.IntSize
  1409  				rr.Align = p.IntSize
  1410  				r = rr
  1411  			}
  1412  			if r.Align > p.PtrSize {
  1413  				r.Align = p.PtrSize
  1414  			}
  1415  			return r
  1416  		}
  1417  		// Look up the type in the top level declarations.
  1418  		// TODO: Handle types defined within a function.
  1419  		for _, d := range p.Decl {
  1420  			gd, ok := d.(*ast.GenDecl)
  1421  			if !ok || gd.Tok != token.TYPE {
  1422  				continue
  1423  			}
  1424  			for _, spec := range gd.Specs {
  1425  				ts, ok := spec.(*ast.TypeSpec)
  1426  				if !ok {
  1427  					continue
  1428  				}
  1429  				if ts.Name.Name == t.Name {
  1430  					return p.cgoType(ts.Type)
  1431  				}
  1432  			}
  1433  		}
  1434  		if def := typedef[t.Name]; def != nil {
  1435  			if defgo, ok := def.Go.(*ast.Ident); ok {
  1436  				switch defgo.Name {
  1437  				case "complex64", "complex128":
  1438  					// MSVC does not support the _Complex keyword
  1439  					// nor the complex macro.
  1440  					// Use GoComplex64 and GoComplex128 instead,
  1441  					// which are typedef-ed to a compatible type.
  1442  					// See go.dev/issues/36233.
  1443  					return goTypesFixup(goTypes[defgo.Name])
  1444  				}
  1445  			}
  1446  			return def
  1447  		}
  1448  		if t.Name == "uintptr" {
  1449  			return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("GoUintptr")}
  1450  		}
  1451  		if t.Name == "string" {
  1452  			// The string data is 1 pointer + 1 (pointer-sized) int.
  1453  			return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoString")}
  1454  		}
  1455  		if t.Name == "error" {
  1456  			return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoInterface")}
  1457  		}
  1458  		if r, ok := goTypes[t.Name]; ok {
  1459  			return goTypesFixup(r)
  1460  		}
  1461  		error_(e.Pos(), "unrecognized Go type %s", t.Name)
  1462  		return &Type{Size: 4, Align: 4, C: c("int")}
  1463  	case *ast.SelectorExpr:
  1464  		id, ok := t.X.(*ast.Ident)
  1465  		if ok && id.Name == "unsafe" && t.Sel.Name == "Pointer" {
  1466  			return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("void*")}
  1467  		}
  1468  	}
  1469  	error_(e.Pos(), "Go type not supported in export: %s", gofmt(e))
  1470  	return &Type{Size: 4, Align: 4, C: c("int")}
  1471  }
  1472  
  1473  const gccProlog = `
  1474  #line 1 "cgo-gcc-prolog"
  1475  /*
  1476    If x and y are not equal, the type will be invalid
  1477    (have a negative array count) and an inscrutable error will come
  1478    out of the compiler and hopefully mention "name".
  1479  */
  1480  #define __cgo_compile_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2UL+1UL];
  1481  
  1482  /* Check at compile time that the sizes we use match our expectations. */
  1483  #define __cgo_size_assert(t, n) __cgo_compile_assert_eq(sizeof(t), (size_t)n, _cgo_sizeof_##t##_is_not_##n)
  1484  
  1485  __cgo_size_assert(char, 1)
  1486  __cgo_size_assert(short, 2)
  1487  __cgo_size_assert(int, 4)
  1488  typedef long long __cgo_long_long;
  1489  __cgo_size_assert(__cgo_long_long, 8)
  1490  __cgo_size_assert(float, 4)
  1491  __cgo_size_assert(double, 8)
  1492  
  1493  extern char* _cgo_topofstack(void);
  1494  
  1495  /*
  1496    We use packed structs, but they are always aligned.
  1497    The pragmas and address-of-packed-member are only recognized as warning
  1498    groups in clang 4.0+, so ignore unknown pragmas first.
  1499  */
  1500  #pragma GCC diagnostic ignored "-Wunknown-pragmas"
  1501  #pragma GCC diagnostic ignored "-Wpragmas"
  1502  #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
  1503  
  1504  #include <errno.h>
  1505  #include <string.h>
  1506  `
  1507  
  1508  // Prologue defining TSAN functions in C.
  1509  const noTsanProlog = `
  1510  #define CGO_NO_SANITIZE_THREAD
  1511  #define _cgo_tsan_acquire()
  1512  #define _cgo_tsan_release()
  1513  `
  1514  
  1515  // This must match the TSAN code in runtime/cgo/libcgo.h.
  1516  // This is used when the code is built with the C/C++ Thread SANitizer,
  1517  // which is not the same as the Go race detector.
  1518  // __tsan_acquire tells TSAN that we are acquiring a lock on a variable,
  1519  // in this case _cgo_sync. __tsan_release releases the lock.
  1520  // (There is no actual lock, we are just telling TSAN that there is.)
  1521  //
  1522  // When we call from Go to C we call _cgo_tsan_acquire.
  1523  // When the C function returns we call _cgo_tsan_release.
  1524  // Similarly, when C calls back into Go we call _cgo_tsan_release
  1525  // and then call _cgo_tsan_acquire when we return to C.
  1526  // These calls tell TSAN that there is a serialization point at the C call.
  1527  //
  1528  // This is necessary because TSAN, which is a C/C++ tool, can not see
  1529  // the synchronization in the Go code. Without these calls, when
  1530  // multiple goroutines call into C code, TSAN does not understand
  1531  // that the calls are properly synchronized on the Go side.
  1532  //
  1533  // To be clear, if the calls are not properly synchronized on the Go side,
  1534  // we will be hiding races. But when using TSAN on mixed Go C/C++ code
  1535  // it is more important to avoid false positives, which reduce confidence
  1536  // in the tool, than to avoid false negatives.
  1537  const yesTsanProlog = `
  1538  #line 1 "cgo-tsan-prolog"
  1539  #define CGO_NO_SANITIZE_THREAD __attribute__ ((no_sanitize_thread))
  1540  
  1541  long long _cgo_sync __attribute__ ((common));
  1542  
  1543  extern void __tsan_acquire(void*);
  1544  extern void __tsan_release(void*);
  1545  
  1546  __attribute__ ((unused))
  1547  static void _cgo_tsan_acquire() {
  1548  	__tsan_acquire(&_cgo_sync);
  1549  }
  1550  
  1551  __attribute__ ((unused))
  1552  static void _cgo_tsan_release() {
  1553  	__tsan_release(&_cgo_sync);
  1554  }
  1555  `
  1556  
  1557  // Set to yesTsanProlog if we see -fsanitize=thread in the flags for gcc.
  1558  var tsanProlog = noTsanProlog
  1559  
  1560  // noMsanProlog is a prologue defining an MSAN function in C.
  1561  // This is used when not compiling with -fsanitize=memory.
  1562  const noMsanProlog = `
  1563  #define _cgo_msan_write(addr, sz)
  1564  `
  1565  
  1566  // yesMsanProlog is a prologue defining an MSAN function in C.
  1567  // This is used when compiling with -fsanitize=memory.
  1568  // See the comment above where _cgo_msan_write is called.
  1569  const yesMsanProlog = `
  1570  extern void __msan_unpoison(const volatile void *, size_t);
  1571  
  1572  #define _cgo_msan_write(addr, sz) __msan_unpoison((addr), (sz))
  1573  `
  1574  
  1575  // msanProlog is set to yesMsanProlog if we see -fsanitize=memory in the flags
  1576  // for the C compiler.
  1577  var msanProlog = noMsanProlog
  1578  
  1579  const builtinProlog = `
  1580  #line 1 "cgo-builtin-prolog"
  1581  #include <stddef.h>
  1582  
  1583  /* Define intgo when compiling with GCC.  */
  1584  typedef ptrdiff_t intgo;
  1585  
  1586  #define GO_CGO_GOSTRING_TYPEDEF
  1587  typedef struct { const char *p; intgo n; } _GoString_;
  1588  typedef struct { char *p; intgo n; intgo c; } _GoBytes_;
  1589  _GoString_ GoString(char *p);
  1590  _GoString_ GoStringN(char *p, int l);
  1591  _GoBytes_ GoBytes(void *p, int n);
  1592  char *CString(_GoString_);
  1593  void *CBytes(_GoBytes_);
  1594  void *_CMalloc(size_t);
  1595  
  1596  __attribute__ ((unused))
  1597  static size_t _GoStringLen(_GoString_ s) { return (size_t)s.n; }
  1598  
  1599  __attribute__ ((unused))
  1600  static const char *_GoStringPtr(_GoString_ s) { return s.p; }
  1601  `
  1602  
  1603  const goProlog = `
  1604  //go:linkname _cgo_runtime_cgocall runtime.cgocall
  1605  func _cgo_runtime_cgocall(unsafe.Pointer, uintptr) int32
  1606  
  1607  //go:linkname _cgoCheckPointer runtime.cgoCheckPointer
  1608  func _cgoCheckPointer(interface{}, interface{})
  1609  
  1610  //go:linkname _cgoCheckResult runtime.cgoCheckResult
  1611  func _cgoCheckResult(interface{})
  1612  `
  1613  
  1614  const gccgoGoProlog = `
  1615  func _cgoCheckPointer(interface{}, interface{})
  1616  
  1617  func _cgoCheckResult(interface{})
  1618  `
  1619  
  1620  const goStringDef = `
  1621  //go:linkname _cgo_runtime_gostring runtime.gostring
  1622  func _cgo_runtime_gostring(*_Ctype_char) string
  1623  
  1624  // GoString converts the C string p into a Go string.
  1625  func _Cfunc_GoString(p *_Ctype_char) string {
  1626  	return _cgo_runtime_gostring(p)
  1627  }
  1628  `
  1629  
  1630  const goStringNDef = `
  1631  //go:linkname _cgo_runtime_gostringn runtime.gostringn
  1632  func _cgo_runtime_gostringn(*_Ctype_char, int) string
  1633  
  1634  // GoStringN converts the C data p with explicit length l to a Go string.
  1635  func _Cfunc_GoStringN(p *_Ctype_char, l _Ctype_int) string {
  1636  	return _cgo_runtime_gostringn(p, int(l))
  1637  }
  1638  `
  1639  
  1640  const goBytesDef = `
  1641  //go:linkname _cgo_runtime_gobytes runtime.gobytes
  1642  func _cgo_runtime_gobytes(unsafe.Pointer, int) []byte
  1643  
  1644  // GoBytes converts the C data p with explicit length l to a Go []byte.
  1645  func _Cfunc_GoBytes(p unsafe.Pointer, l _Ctype_int) []byte {
  1646  	return _cgo_runtime_gobytes(p, int(l))
  1647  }
  1648  `
  1649  
  1650  const cStringDef = `
  1651  // CString converts the Go string s to a C string.
  1652  //
  1653  // The C string is allocated in the C heap using malloc.
  1654  // It is the caller's responsibility to arrange for it to be
  1655  // freed, such as by calling C.free (be sure to include stdlib.h
  1656  // if C.free is needed).
  1657  func _Cfunc_CString(s string) *_Ctype_char {
  1658  	if len(s)+1 <= 0 {
  1659  		panic("string too large")
  1660  	}
  1661  	p := _cgo_cmalloc(uint64(len(s)+1))
  1662  	sliceHeader := struct {
  1663  		p   unsafe.Pointer
  1664  		len int
  1665  		cap int
  1666  	}{p, len(s)+1, len(s)+1}
  1667  	b := *(*[]byte)(unsafe.Pointer(&sliceHeader))
  1668  	copy(b, s)
  1669  	b[len(s)] = 0
  1670  	return (*_Ctype_char)(p)
  1671  }
  1672  `
  1673  
  1674  const cBytesDef = `
  1675  // CBytes converts the Go []byte slice b to a C array.
  1676  //
  1677  // The C array is allocated in the C heap using malloc.
  1678  // It is the caller's responsibility to arrange for it to be
  1679  // freed, such as by calling C.free (be sure to include stdlib.h
  1680  // if C.free is needed).
  1681  func _Cfunc_CBytes(b []byte) unsafe.Pointer {
  1682  	p := _cgo_cmalloc(uint64(len(b)))
  1683  	sliceHeader := struct {
  1684  		p   unsafe.Pointer
  1685  		len int
  1686  		cap int
  1687  	}{p, len(b), len(b)}
  1688  	s := *(*[]byte)(unsafe.Pointer(&sliceHeader))
  1689  	copy(s, b)
  1690  	return p
  1691  }
  1692  `
  1693  
  1694  const cMallocDef = `
  1695  func _Cfunc__CMalloc(n _Ctype_size_t) unsafe.Pointer {
  1696  	return _cgo_cmalloc(uint64(n))
  1697  }
  1698  `
  1699  
  1700  var builtinDefs = map[string]string{
  1701  	"GoString":  goStringDef,
  1702  	"GoStringN": goStringNDef,
  1703  	"GoBytes":   goBytesDef,
  1704  	"CString":   cStringDef,
  1705  	"CBytes":    cBytesDef,
  1706  	"_CMalloc":  cMallocDef,
  1707  }
  1708  
  1709  // Definitions for C.malloc in Go and in C. We define it ourselves
  1710  // since we call it from functions we define, such as C.CString.
  1711  // Also, we have historically ensured that C.malloc does not return
  1712  // nil even for an allocation of 0.
  1713  
  1714  const cMallocDefGo = `
  1715  //go:cgo_import_static _cgoPREFIX_Cfunc__Cmalloc
  1716  //go:linkname __cgofn__cgoPREFIX_Cfunc__Cmalloc _cgoPREFIX_Cfunc__Cmalloc
  1717  var __cgofn__cgoPREFIX_Cfunc__Cmalloc byte
  1718  var _cgoPREFIX_Cfunc__Cmalloc = unsafe.Pointer(&__cgofn__cgoPREFIX_Cfunc__Cmalloc)
  1719  
  1720  //go:linkname runtime_throw runtime.throw
  1721  func runtime_throw(string)
  1722  
  1723  //go:cgo_unsafe_args
  1724  func _cgo_cmalloc(p0 uint64) (r1 unsafe.Pointer) {
  1725  	_cgo_runtime_cgocall(_cgoPREFIX_Cfunc__Cmalloc, uintptr(unsafe.Pointer(&p0)))
  1726  	if r1 == nil {
  1727  		runtime_throw("runtime: C malloc failed")
  1728  	}
  1729  	return
  1730  }
  1731  `
  1732  
  1733  // cMallocDefC defines the C version of C.malloc for the gc compiler.
  1734  // It is defined here because C.CString and friends need a definition.
  1735  // We define it by hand, rather than simply inventing a reference to
  1736  // C.malloc, because <stdlib.h> may not have been included.
  1737  // This is approximately what writeOutputFunc would generate, but
  1738  // skips the cgo_topofstack code (which is only needed if the C code
  1739  // calls back into Go). This also avoids returning nil for an
  1740  // allocation of 0 bytes.
  1741  const cMallocDefC = `
  1742  CGO_NO_SANITIZE_THREAD
  1743  void _cgoPREFIX_Cfunc__Cmalloc(void *v) {
  1744  	struct {
  1745  		unsigned long long p0;
  1746  		void *r1;
  1747  	} PACKED *a = v;
  1748  	void *ret;
  1749  	_cgo_tsan_acquire();
  1750  	ret = malloc(a->p0);
  1751  	if (ret == 0 && a->p0 == 0) {
  1752  		ret = malloc(1);
  1753  	}
  1754  	a->r1 = ret;
  1755  	_cgo_tsan_release();
  1756  }
  1757  `
  1758  
  1759  func (p *Package) cPrologGccgo() string {
  1760  	r := strings.NewReplacer(
  1761  		"PREFIX", cPrefix,
  1762  		"GCCGOSYMBOLPREF", p.gccgoSymbolPrefix(),
  1763  		"_cgoCheckPointer", gccgoToSymbol("_cgoCheckPointer"),
  1764  		"_cgoCheckResult", gccgoToSymbol("_cgoCheckResult"))
  1765  	return r.Replace(cPrologGccgo)
  1766  }
  1767  
  1768  const cPrologGccgo = `
  1769  #line 1 "cgo-c-prolog-gccgo"
  1770  #include <stdint.h>
  1771  #include <stdlib.h>
  1772  #include <string.h>
  1773  
  1774  typedef unsigned char byte;
  1775  typedef intptr_t intgo;
  1776  
  1777  struct __go_string {
  1778  	const unsigned char *__data;
  1779  	intgo __length;
  1780  };
  1781  
  1782  typedef struct __go_open_array {
  1783  	void* __values;
  1784  	intgo __count;
  1785  	intgo __capacity;
  1786  } Slice;
  1787  
  1788  struct __go_string __go_byte_array_to_string(const void* p, intgo len);
  1789  struct __go_open_array __go_string_to_byte_array (struct __go_string str);
  1790  
  1791  extern void runtime_throw(const char *);
  1792  
  1793  const char *_cgoPREFIX_Cfunc_CString(struct __go_string s) {
  1794  	char *p = malloc(s.__length+1);
  1795  	if(p == NULL)
  1796  		runtime_throw("runtime: C malloc failed");
  1797  	memmove(p, s.__data, s.__length);
  1798  	p[s.__length] = 0;
  1799  	return p;
  1800  }
  1801  
  1802  void *_cgoPREFIX_Cfunc_CBytes(struct __go_open_array b) {
  1803  	char *p = malloc(b.__count);
  1804  	if(p == NULL)
  1805  		runtime_throw("runtime: C malloc failed");
  1806  	memmove(p, b.__values, b.__count);
  1807  	return p;
  1808  }
  1809  
  1810  struct __go_string _cgoPREFIX_Cfunc_GoString(char *p) {
  1811  	intgo len = (p != NULL) ? strlen(p) : 0;
  1812  	return __go_byte_array_to_string(p, len);
  1813  }
  1814  
  1815  struct __go_string _cgoPREFIX_Cfunc_GoStringN(char *p, int32_t n) {
  1816  	return __go_byte_array_to_string(p, n);
  1817  }
  1818  
  1819  Slice _cgoPREFIX_Cfunc_GoBytes(char *p, int32_t n) {
  1820  	struct __go_string s = { (const unsigned char *)p, n };
  1821  	return __go_string_to_byte_array(s);
  1822  }
  1823  
  1824  void *_cgoPREFIX_Cfunc__CMalloc(size_t n) {
  1825  	void *p = malloc(n);
  1826  	if(p == NULL && n == 0)
  1827  		p = malloc(1);
  1828  	if(p == NULL)
  1829  		runtime_throw("runtime: C malloc failed");
  1830  	return p;
  1831  }
  1832  
  1833  struct __go_type_descriptor;
  1834  typedef struct __go_empty_interface {
  1835  	const struct __go_type_descriptor *__type_descriptor;
  1836  	void *__object;
  1837  } Eface;
  1838  
  1839  extern void runtimeCgoCheckPointer(Eface, Eface)
  1840  	__asm__("runtime.cgoCheckPointer")
  1841  	__attribute__((weak));
  1842  
  1843  extern void localCgoCheckPointer(Eface, Eface)
  1844  	__asm__("GCCGOSYMBOLPREF._cgoCheckPointer");
  1845  
  1846  void localCgoCheckPointer(Eface ptr, Eface arg) {
  1847  	if(runtimeCgoCheckPointer) {
  1848  		runtimeCgoCheckPointer(ptr, arg);
  1849  	}
  1850  }
  1851  
  1852  extern void runtimeCgoCheckResult(Eface)
  1853  	__asm__("runtime.cgoCheckResult")
  1854  	__attribute__((weak));
  1855  
  1856  extern void localCgoCheckResult(Eface)
  1857  	__asm__("GCCGOSYMBOLPREF._cgoCheckResult");
  1858  
  1859  void localCgoCheckResult(Eface val) {
  1860  	if(runtimeCgoCheckResult) {
  1861  		runtimeCgoCheckResult(val);
  1862  	}
  1863  }
  1864  `
  1865  
  1866  // builtinExportProlog is a shorter version of builtinProlog,
  1867  // to be put into the _cgo_export.h file.
  1868  // For historical reasons we can't use builtinProlog in _cgo_export.h,
  1869  // because _cgo_export.h defines GoString as a struct while builtinProlog
  1870  // defines it as a function. We don't change this to avoid unnecessarily
  1871  // breaking existing code.
  1872  // The test of GO_CGO_GOSTRING_TYPEDEF avoids a duplicate definition
  1873  // error if a Go file with a cgo comment #include's the export header
  1874  // generated by a different package.
  1875  const builtinExportProlog = `
  1876  #line 1 "cgo-builtin-export-prolog"
  1877  
  1878  #include <stddef.h>
  1879  
  1880  #ifndef GO_CGO_EXPORT_PROLOGUE_H
  1881  #define GO_CGO_EXPORT_PROLOGUE_H
  1882  
  1883  #ifndef GO_CGO_GOSTRING_TYPEDEF
  1884  typedef struct { const char *p; ptrdiff_t n; } _GoString_;
  1885  #endif
  1886  
  1887  #endif
  1888  `
  1889  
  1890  func (p *Package) gccExportHeaderProlog() string {
  1891  	return strings.Replace(gccExportHeaderProlog, "GOINTBITS", fmt.Sprint(8*p.IntSize), -1)
  1892  }
  1893  
  1894  // gccExportHeaderProlog is written to the exported header, after the
  1895  // import "C" comment preamble but before the generated declarations
  1896  // of exported functions. This permits the generated declarations to
  1897  // use the type names that appear in goTypes, above.
  1898  //
  1899  // The test of GO_CGO_GOSTRING_TYPEDEF avoids a duplicate definition
  1900  // error if a Go file with a cgo comment #include's the export header
  1901  // generated by a different package. Unfortunately GoString means two
  1902  // different things: in this prolog it means a C name for the Go type,
  1903  // while in the prolog written into the start of the C code generated
  1904  // from a cgo-using Go file it means the C.GoString function. There is
  1905  // no way to resolve this conflict, but it also doesn't make much
  1906  // difference, as Go code never wants to refer to the latter meaning.
  1907  const gccExportHeaderProlog = `
  1908  /* Start of boilerplate cgo prologue.  */
  1909  #line 1 "cgo-gcc-export-header-prolog"
  1910  
  1911  #ifndef GO_CGO_PROLOGUE_H
  1912  #define GO_CGO_PROLOGUE_H
  1913  
  1914  typedef signed char GoInt8;
  1915  typedef unsigned char GoUint8;
  1916  typedef short GoInt16;
  1917  typedef unsigned short GoUint16;
  1918  typedef int GoInt32;
  1919  typedef unsigned int GoUint32;
  1920  typedef long long GoInt64;
  1921  typedef unsigned long long GoUint64;
  1922  typedef GoIntGOINTBITS GoInt;
  1923  typedef GoUintGOINTBITS GoUint;
  1924  typedef size_t GoUintptr;
  1925  typedef float GoFloat32;
  1926  typedef double GoFloat64;
  1927  #ifdef _MSC_VER
  1928  #include <complex.h>
  1929  typedef _Fcomplex GoComplex64;
  1930  typedef _Dcomplex GoComplex128;
  1931  #else
  1932  typedef float _Complex GoComplex64;
  1933  typedef double _Complex GoComplex128;
  1934  #endif
  1935  
  1936  /*
  1937    static assertion to make sure the file is being used on architecture
  1938    at least with matching size of GoInt.
  1939  */
  1940  typedef char _check_for_GOINTBITS_bit_pointer_matching_GoInt[sizeof(void*)==GOINTBITS/8 ? 1:-1];
  1941  
  1942  #ifndef GO_CGO_GOSTRING_TYPEDEF
  1943  typedef _GoString_ GoString;
  1944  #endif
  1945  typedef void *GoMap;
  1946  typedef void *GoChan;
  1947  typedef struct { void *t; void *v; } GoInterface;
  1948  typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
  1949  
  1950  #endif
  1951  
  1952  /* End of boilerplate cgo prologue.  */
  1953  
  1954  #ifdef __cplusplus
  1955  extern "C" {
  1956  #endif
  1957  `
  1958  
  1959  // gccExportHeaderEpilog goes at the end of the generated header file.
  1960  const gccExportHeaderEpilog = `
  1961  #ifdef __cplusplus
  1962  }
  1963  #endif
  1964  `
  1965  
  1966  // gccgoExportFileProlog is written to the _cgo_export.c file when
  1967  // using gccgo.
  1968  // We use weak declarations, and test the addresses, so that this code
  1969  // works with older versions of gccgo.
  1970  const gccgoExportFileProlog = `
  1971  #line 1 "cgo-gccgo-export-file-prolog"
  1972  extern _Bool runtime_iscgo __attribute__ ((weak));
  1973  
  1974  static void GoInit(void) __attribute__ ((constructor));
  1975  static void GoInit(void) {
  1976  	if(&runtime_iscgo)
  1977  		runtime_iscgo = 1;
  1978  }
  1979  
  1980  extern size_t _cgo_wait_runtime_init_done(void) __attribute__ ((weak));
  1981  `
  1982  

View as plain text