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