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