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