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.ReplaceAll(cMallocDefGo, "PREFIX", cPrefix))
255 fmt.Fprint(fgcc, strings.ReplaceAll(strings.Replace(cMallocDefC, "PREFIX", cPrefix, -1), "PACKED", p.packedAttribute()))
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
953 maxAlign := int64(1)
954 argField := func(typ ast.Expr, namePat string, args ...interface{}) {
955 name := fmt.Sprintf(namePat, args...)
956 t := p.cgoType(typ)
957 if off%t.Align != 0 {
958 pad := t.Align - off%t.Align
959 fmt.Fprintf(&ctype, "\t\tchar __pad%d[%d];\n", npad, pad)
960 off += pad
961 npad++
962 }
963 fmt.Fprintf(&ctype, "\t\t%s %s;\n", t.C, name)
964 fmt.Fprintf(gotype, "\t\t%s ", name)
965 noSourceConf.Fprint(gotype, fset, typ)
966 fmt.Fprintf(gotype, "\n")
967 off += t.Size
968
969
970 if t.Align > maxAlign {
971 maxAlign = t.Align
972 }
973 }
974 if fn.Recv != nil {
975 argField(fn.Recv.List[0].Type, "recv")
976 }
977 fntype := fn.Type
978 forFieldList(fntype.Params,
979 func(i int, aname string, atype ast.Expr) {
980 argField(atype, "p%d", i)
981 })
982 forFieldList(fntype.Results,
983 func(i int, aname string, atype ast.Expr) {
984 argField(atype, "r%d", i)
985 })
986 if ctype.Len() == len(start) {
987 ctype.WriteString("\t\tchar unused;\n")
988 }
989 ctype.WriteString("\t}")
990 fmt.Fprintf(gotype, "\t}")
991
992
993
994 gccResult := ""
995 if fntype.Results == nil || len(fntype.Results.List) == 0 {
996 gccResult = "void"
997 } else if len(fntype.Results.List) == 1 && len(fntype.Results.List[0].Names) <= 1 {
998 gccResult = p.cgoType(fntype.Results.List[0].Type).C.String()
999 } else {
1000 fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
1001 fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName)
1002 forFieldList(fntype.Results,
1003 func(i int, aname string, atype ast.Expr) {
1004 fmt.Fprintf(fgcch, "\t%s r%d;", p.cgoType(atype).C, i)
1005 if len(aname) > 0 {
1006 fmt.Fprintf(fgcch, " /* %s */", aname)
1007 }
1008 fmt.Fprint(fgcch, "\n")
1009 })
1010 fmt.Fprintf(fgcch, "};\n")
1011 gccResult = "struct " + exp.ExpName + "_return"
1012 }
1013
1014
1015 var s strings.Builder
1016 fmt.Fprintf(&s, "%s %s(", gccResult, exp.ExpName)
1017 if fn.Recv != nil {
1018 s.WriteString(p.cgoType(fn.Recv.List[0].Type).C.String())
1019 s.WriteString(" recv")
1020 }
1021
1022 if len(fntype.Params.List) > 0 {
1023 forFieldList(fntype.Params,
1024 func(i int, aname string, atype ast.Expr) {
1025 if i > 0 || fn.Recv != nil {
1026 s.WriteString(", ")
1027 }
1028 fmt.Fprintf(&s, "%s %s", p.cgoType(atype).C, exportParamName(aname, i))
1029 })
1030 } else {
1031 s.WriteString("void")
1032 }
1033 s.WriteByte(')')
1034
1035 if len(exp.Doc) > 0 {
1036 fmt.Fprintf(fgcch, "\n%s", exp.Doc)
1037 if !strings.HasSuffix(exp.Doc, "\n") {
1038 fmt.Fprint(fgcch, "\n")
1039 }
1040 }
1041 fmt.Fprintf(fgcch, "extern %s;\n", s.String())
1042
1043 fmt.Fprintf(fgcc, "extern void _cgoexp%s_%s(void *);\n", cPrefix, exp.ExpName)
1044 fmt.Fprintf(fgcc, "\nCGO_NO_SANITIZE_THREAD")
1045 fmt.Fprintf(fgcc, "\n%s\n", s.String())
1046 fmt.Fprintf(fgcc, "{\n")
1047 fmt.Fprintf(fgcc, "\tsize_t _cgo_ctxt = _cgo_wait_runtime_init_done();\n")
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061 fmt.Fprintf(fgcc, "\ttypedef %s %v __attribute__((aligned(%d))) _cgo_argtype;\n", ctype.String(), p.packedAttribute(), maxAlign)
1062 fmt.Fprintf(fgcc, "\tstatic _cgo_argtype _cgo_zero;\n")
1063 fmt.Fprintf(fgcc, "\t_cgo_argtype _cgo_a = _cgo_zero;\n")
1064 if gccResult != "void" && (len(fntype.Results.List) > 1 || len(fntype.Results.List[0].Names) > 1) {
1065 fmt.Fprintf(fgcc, "\t%s r;\n", gccResult)
1066 }
1067 if fn.Recv != nil {
1068 fmt.Fprintf(fgcc, "\t_cgo_a.recv = recv;\n")
1069 }
1070 forFieldList(fntype.Params,
1071 func(i int, aname string, atype ast.Expr) {
1072 fmt.Fprintf(fgcc, "\t_cgo_a.p%d = %s;\n", i, exportParamName(aname, i))
1073 })
1074 fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n")
1075 fmt.Fprintf(fgcc, "\tcrosscall2(_cgoexp%s_%s, &_cgo_a, %d, _cgo_ctxt);\n", cPrefix, exp.ExpName, off)
1076 fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
1077 fmt.Fprintf(fgcc, "\t_cgo_release_context(_cgo_ctxt);\n")
1078 if gccResult != "void" {
1079 if len(fntype.Results.List) == 1 && len(fntype.Results.List[0].Names) <= 1 {
1080 fmt.Fprintf(fgcc, "\treturn _cgo_a.r0;\n")
1081 } else {
1082 forFieldList(fntype.Results,
1083 func(i int, aname string, atype ast.Expr) {
1084 fmt.Fprintf(fgcc, "\tr.r%d = _cgo_a.r%d;\n", i, i)
1085 })
1086 fmt.Fprintf(fgcc, "\treturn r;\n")
1087 }
1088 }
1089 fmt.Fprintf(fgcc, "}\n")
1090
1091
1092
1093
1094
1095
1096
1097 fmt.Fprintf(fgo2, "//go:cgo_export_dynamic %s\n", exp.ExpName)
1098
1099
1100 fmt.Fprintf(fgo2, "//go:linkname _cgoexp%s_%s _cgoexp%s_%s\n", cPrefix, exp.ExpName, cPrefix, exp.ExpName)
1101
1102
1103
1104
1105
1106 fmt.Fprintf(fgo2, "//go:cgo_export_static _cgoexp%s_%s\n", cPrefix, exp.ExpName)
1107
1108
1109
1110 fmt.Fprintf(fgo2, "func _cgoexp%s_%s(a *%s) {\n", cPrefix, exp.ExpName, gotype)
1111
1112 fmt.Fprintf(fm, "void _cgoexp%s_%s(void* p __attribute__((unused))){}\n", cPrefix, exp.ExpName)
1113
1114 fmt.Fprintf(fgo2, "\t")
1115
1116 if gccResult != "void" {
1117
1118 forFieldList(fntype.Results,
1119 func(i int, aname string, atype ast.Expr) {
1120 if i > 0 {
1121 fmt.Fprintf(fgo2, ", ")
1122 }
1123 fmt.Fprintf(fgo2, "a.r%d", i)
1124 })
1125 fmt.Fprintf(fgo2, " = ")
1126 }
1127 if fn.Recv != nil {
1128 fmt.Fprintf(fgo2, "a.recv.")
1129 }
1130 fmt.Fprintf(fgo2, "%s(", exp.Func.Name)
1131 forFieldList(fntype.Params,
1132 func(i int, aname string, atype ast.Expr) {
1133 if i > 0 {
1134 fmt.Fprint(fgo2, ", ")
1135 }
1136 fmt.Fprintf(fgo2, "a.p%d", i)
1137 })
1138 fmt.Fprint(fgo2, ")\n")
1139 if gccResult != "void" {
1140
1141
1142 forFieldList(fntype.Results,
1143 func(i int, aname string, atype ast.Expr) {
1144 if !p.hasPointer(nil, atype, false) {
1145 return
1146 }
1147 fmt.Fprintf(fgo2, "\t_cgoCheckResult(a.r%d)\n", i)
1148 })
1149 }
1150 fmt.Fprint(fgo2, "}\n")
1151 }
1152
1153 fmt.Fprintf(fgcch, "%s", gccExportHeaderEpilog)
1154 }
1155
1156
1157 func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
1158 gccgoSymbolPrefix := p.gccgoSymbolPrefix()
1159
1160 p.writeExportHeader(fgcch)
1161
1162 fmt.Fprintf(fgcc, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n")
1163 fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n")
1164
1165 fmt.Fprintf(fgcc, "%s\n", gccgoExportFileProlog)
1166 fmt.Fprintf(fgcc, "%s\n", tsanProlog)
1167 fmt.Fprintf(fgcc, "%s\n", msanProlog)
1168
1169 for _, exp := range p.ExpFunc {
1170 fn := exp.Func
1171 fntype := fn.Type
1172
1173 cdeclBuf := new(strings.Builder)
1174 resultCount := 0
1175 forFieldList(fntype.Results,
1176 func(i int, aname string, atype ast.Expr) { resultCount++ })
1177 switch resultCount {
1178 case 0:
1179 fmt.Fprintf(cdeclBuf, "void")
1180 case 1:
1181 forFieldList(fntype.Results,
1182 func(i int, aname string, atype ast.Expr) {
1183 t := p.cgoType(atype)
1184 fmt.Fprintf(cdeclBuf, "%s", t.C)
1185 })
1186 default:
1187
1188 fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
1189 fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName)
1190 forFieldList(fntype.Results,
1191 func(i int, aname string, atype ast.Expr) {
1192 t := p.cgoType(atype)
1193 fmt.Fprintf(fgcch, "\t%s r%d;", t.C, i)
1194 if len(aname) > 0 {
1195 fmt.Fprintf(fgcch, " /* %s */", aname)
1196 }
1197 fmt.Fprint(fgcch, "\n")
1198 })
1199 fmt.Fprintf(fgcch, "};\n")
1200 fmt.Fprintf(cdeclBuf, "struct %s_return", exp.ExpName)
1201 }
1202
1203 cRet := cdeclBuf.String()
1204
1205 cdeclBuf = new(strings.Builder)
1206 fmt.Fprintf(cdeclBuf, "(")
1207 if fn.Recv != nil {
1208 fmt.Fprintf(cdeclBuf, "%s recv", p.cgoType(fn.Recv.List[0].Type).C.String())
1209 }
1210
1211 forFieldList(fntype.Params,
1212 func(i int, aname string, atype ast.Expr) {
1213 if i > 0 || fn.Recv != nil {
1214 fmt.Fprintf(cdeclBuf, ", ")
1215 }
1216 t := p.cgoType(atype)
1217 fmt.Fprintf(cdeclBuf, "%s p%d", t.C, i)
1218 })
1219 fmt.Fprintf(cdeclBuf, ")")
1220 cParams := cdeclBuf.String()
1221
1222 if len(exp.Doc) > 0 {
1223 fmt.Fprintf(fgcch, "\n%s", exp.Doc)
1224 }
1225
1226 fmt.Fprintf(fgcch, "extern %s %s%s;\n", cRet, exp.ExpName, cParams)
1227
1228
1229
1230
1231
1232 goName := "Cgoexp_" + exp.ExpName
1233 fmt.Fprintf(fgcc, `extern %s %s %s __asm__("%s.%s");`, cRet, goName, cParams, gccgoSymbolPrefix, gccgoToSymbol(goName))
1234 fmt.Fprint(fgcc, "\n")
1235
1236 fmt.Fprint(fgcc, "\nCGO_NO_SANITIZE_THREAD\n")
1237 fmt.Fprintf(fgcc, "%s %s %s {\n", cRet, exp.ExpName, cParams)
1238 if resultCount > 0 {
1239 fmt.Fprintf(fgcc, "\t%s r;\n", cRet)
1240 }
1241 fmt.Fprintf(fgcc, "\tif(_cgo_wait_runtime_init_done)\n")
1242 fmt.Fprintf(fgcc, "\t\t_cgo_wait_runtime_init_done();\n")
1243 fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n")
1244 fmt.Fprint(fgcc, "\t")
1245 if resultCount > 0 {
1246 fmt.Fprint(fgcc, "r = ")
1247 }
1248 fmt.Fprintf(fgcc, "%s(", goName)
1249 if fn.Recv != nil {
1250 fmt.Fprint(fgcc, "recv")
1251 }
1252 forFieldList(fntype.Params,
1253 func(i int, aname string, atype ast.Expr) {
1254 if i > 0 || fn.Recv != nil {
1255 fmt.Fprintf(fgcc, ", ")
1256 }
1257 fmt.Fprintf(fgcc, "p%d", i)
1258 })
1259 fmt.Fprint(fgcc, ");\n")
1260 fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
1261 if resultCount > 0 {
1262 fmt.Fprint(fgcc, "\treturn r;\n")
1263 }
1264 fmt.Fprint(fgcc, "}\n")
1265
1266
1267 fmt.Fprintf(fm, `char %s[1] __asm__("%s.%s");`, goName, gccgoSymbolPrefix, gccgoToSymbol(goName))
1268 fmt.Fprint(fm, "\n")
1269
1270
1271
1272
1273
1274
1275
1276 fmt.Fprint(fgo2, "\n")
1277 fmt.Fprintf(fgo2, "func %s(", goName)
1278 if fn.Recv != nil {
1279 fmt.Fprint(fgo2, "recv ")
1280 printer.Fprint(fgo2, fset, fn.Recv.List[0].Type)
1281 }
1282 forFieldList(fntype.Params,
1283 func(i int, aname string, atype ast.Expr) {
1284 if i > 0 || fn.Recv != nil {
1285 fmt.Fprintf(fgo2, ", ")
1286 }
1287 fmt.Fprintf(fgo2, "p%d ", i)
1288 printer.Fprint(fgo2, fset, atype)
1289 })
1290 fmt.Fprintf(fgo2, ")")
1291 if resultCount > 0 {
1292 fmt.Fprintf(fgo2, " (")
1293 forFieldList(fntype.Results,
1294 func(i int, aname string, atype ast.Expr) {
1295 if i > 0 {
1296 fmt.Fprint(fgo2, ", ")
1297 }
1298 printer.Fprint(fgo2, fset, atype)
1299 })
1300 fmt.Fprint(fgo2, ")")
1301 }
1302 fmt.Fprint(fgo2, " {\n")
1303 fmt.Fprint(fgo2, "\tsyscall.CgocallBack()\n")
1304 fmt.Fprint(fgo2, "\tdefer syscall.CgocallBackDone()\n")
1305 fmt.Fprint(fgo2, "\t")
1306 if resultCount > 0 {
1307 fmt.Fprint(fgo2, "return ")
1308 }
1309 if fn.Recv != nil {
1310 fmt.Fprint(fgo2, "recv.")
1311 }
1312 fmt.Fprintf(fgo2, "%s(", exp.Func.Name)
1313 forFieldList(fntype.Params,
1314 func(i int, aname string, atype ast.Expr) {
1315 if i > 0 {
1316 fmt.Fprint(fgo2, ", ")
1317 }
1318 fmt.Fprintf(fgo2, "p%d", i)
1319 })
1320 fmt.Fprint(fgo2, ")\n")
1321 fmt.Fprint(fgo2, "}\n")
1322 }
1323
1324 fmt.Fprintf(fgcch, "%s", gccExportHeaderEpilog)
1325 }
1326
1327
1328 func (p *Package) writeExportHeader(fgcch io.Writer) {
1329 fmt.Fprintf(fgcch, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n")
1330 pkg := *importPath
1331 if pkg == "" {
1332 pkg = p.PackagePath
1333 }
1334 fmt.Fprintf(fgcch, "/* package %s */\n\n", pkg)
1335 fmt.Fprintf(fgcch, "%s\n", builtinExportProlog)
1336
1337
1338
1339
1340
1341 re := regexp.MustCompile(`(?m)^(#line\s+\d+\s+")[^"]*[/\\]([^"]*")`)
1342 preamble := re.ReplaceAllString(p.Preamble, "$1$2")
1343
1344 fmt.Fprintf(fgcch, "/* Start of preamble from import \"C\" comments. */\n\n")
1345 fmt.Fprintf(fgcch, "%s\n", preamble)
1346 fmt.Fprintf(fgcch, "\n/* End of preamble from import \"C\" comments. */\n\n")
1347
1348 fmt.Fprintf(fgcch, "%s\n", p.gccExportHeaderProlog())
1349 }
1350
1351
1352 func gccgoToSymbol(ppath string) string {
1353 if gccgoMangler == nil {
1354 var err error
1355 cmd := os.Getenv("GCCGO")
1356 if cmd == "" {
1357 cmd, err = exec.LookPath("gccgo")
1358 if err != nil {
1359 fatalf("unable to locate gccgo: %v", err)
1360 }
1361 }
1362 gccgoMangler, err = pkgpath.ToSymbolFunc(cmd, *objDir)
1363 if err != nil {
1364 fatalf("%v", err)
1365 }
1366 }
1367 return gccgoMangler(ppath)
1368 }
1369
1370
1371 func (p *Package) gccgoSymbolPrefix() string {
1372 if !*gccgo {
1373 return ""
1374 }
1375
1376 if *gccgopkgpath != "" {
1377 return gccgoToSymbol(*gccgopkgpath)
1378 }
1379 if *gccgoprefix == "" && p.PackageName == "main" {
1380 return "main"
1381 }
1382 prefix := gccgoToSymbol(*gccgoprefix)
1383 if prefix == "" {
1384 prefix = "go"
1385 }
1386 return prefix + "." + p.PackageName
1387 }
1388
1389
1390
1391 func forFieldList(fl *ast.FieldList, fn func(int, string, ast.Expr)) {
1392 if fl == nil {
1393 return
1394 }
1395 i := 0
1396 for _, r := range fl.List {
1397 if r.Names == nil {
1398 fn(i, "", r.Type)
1399 i++
1400 } else {
1401 for _, n := range r.Names {
1402 fn(i, n.Name, r.Type)
1403 i++
1404 }
1405 }
1406 }
1407 }
1408
1409 func c(repr string, args ...interface{}) *TypeRepr {
1410 return &TypeRepr{repr, args}
1411 }
1412
1413
1414 var goTypes = map[string]*Type{
1415 "bool": {Size: 1, Align: 1, C: c("GoUint8")},
1416 "byte": {Size: 1, Align: 1, C: c("GoUint8")},
1417 "int": {Size: 0, Align: 0, C: c("GoInt")},
1418 "uint": {Size: 0, Align: 0, C: c("GoUint")},
1419 "rune": {Size: 4, Align: 4, C: c("GoInt32")},
1420 "int8": {Size: 1, Align: 1, C: c("GoInt8")},
1421 "uint8": {Size: 1, Align: 1, C: c("GoUint8")},
1422 "int16": {Size: 2, Align: 2, C: c("GoInt16")},
1423 "uint16": {Size: 2, Align: 2, C: c("GoUint16")},
1424 "int32": {Size: 4, Align: 4, C: c("GoInt32")},
1425 "uint32": {Size: 4, Align: 4, C: c("GoUint32")},
1426 "int64": {Size: 8, Align: 8, C: c("GoInt64")},
1427 "uint64": {Size: 8, Align: 8, C: c("GoUint64")},
1428 "float32": {Size: 4, Align: 4, C: c("GoFloat32")},
1429 "float64": {Size: 8, Align: 8, C: c("GoFloat64")},
1430 "complex64": {Size: 8, Align: 4, C: c("GoComplex64")},
1431 "complex128": {Size: 16, Align: 8, C: c("GoComplex128")},
1432 }
1433
1434
1435 func (p *Package) cgoType(e ast.Expr) *Type {
1436 return p.doCgoType(e, make(map[ast.Expr]bool))
1437 }
1438
1439
1440 func (p *Package) doCgoType(e ast.Expr, m map[ast.Expr]bool) *Type {
1441 if m[e] {
1442 fatalf("%s: invalid recursive type", fset.Position(e.Pos()))
1443 }
1444 m[e] = true
1445 switch t := e.(type) {
1446 case *ast.StarExpr:
1447 x := p.doCgoType(t.X, m)
1448 return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("%s*", x.C)}
1449 case *ast.ArrayType:
1450 if t.Len == nil {
1451
1452 return &Type{Size: p.PtrSize * 3, Align: p.PtrSize, C: c("GoSlice")}
1453 }
1454
1455 case *ast.StructType:
1456
1457 case *ast.FuncType:
1458 return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("void*")}
1459 case *ast.InterfaceType:
1460 return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoInterface")}
1461 case *ast.MapType:
1462 return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("GoMap")}
1463 case *ast.ChanType:
1464 return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("GoChan")}
1465 case *ast.Ident:
1466 goTypesFixup := func(r *Type) *Type {
1467 if r.Size == 0 {
1468 rr := new(Type)
1469 *rr = *r
1470 rr.Size = p.IntSize
1471 rr.Align = p.IntSize
1472 r = rr
1473 }
1474 if r.Align > p.PtrSize {
1475 r.Align = p.PtrSize
1476 }
1477 return r
1478 }
1479
1480
1481 for _, d := range p.Decl {
1482 gd, ok := d.(*ast.GenDecl)
1483 if !ok || gd.Tok != token.TYPE {
1484 continue
1485 }
1486 for _, spec := range gd.Specs {
1487 ts, ok := spec.(*ast.TypeSpec)
1488 if !ok {
1489 continue
1490 }
1491 if ts.Name.Name == t.Name {
1492
1493
1494 if m[ts.Type] {
1495 fatalf("%s: invalid recursive type: %s refers to itself", fset.Position(e.Pos()), t.Name)
1496 }
1497 return p.doCgoType(ts.Type, m)
1498 }
1499 }
1500 }
1501 if def := typedef[t.Name]; def != nil {
1502 if defgo, ok := def.Go.(*ast.Ident); ok {
1503 switch defgo.Name {
1504 case "complex64", "complex128":
1505
1506
1507
1508
1509
1510 return goTypesFixup(goTypes[defgo.Name])
1511 }
1512 }
1513 return def
1514 }
1515 if t.Name == "uintptr" {
1516 return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("GoUintptr")}
1517 }
1518 if t.Name == "string" {
1519
1520 return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoString")}
1521 }
1522 if t.Name == "error" {
1523 return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoInterface")}
1524 }
1525 if r, ok := goTypes[t.Name]; ok {
1526 return goTypesFixup(r)
1527 }
1528 error_(e.Pos(), "unrecognized Go type %s", t.Name)
1529 return &Type{Size: 4, Align: 4, C: c("int")}
1530 case *ast.SelectorExpr:
1531 id, ok := t.X.(*ast.Ident)
1532 if ok && id.Name == "unsafe" && t.Sel.Name == "Pointer" {
1533 return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("void*")}
1534 }
1535 }
1536 error_(e.Pos(), "Go type not supported in export: %s", gofmt(e))
1537 return &Type{Size: 4, Align: 4, C: c("int")}
1538 }
1539
1540 const gccProlog = `
1541 #line 1 "cgo-gcc-prolog"
1542 /*
1543 If x and y are not equal, the type will be invalid
1544 (have a negative array count) and an inscrutable error will come
1545 out of the compiler and hopefully mention "name".
1546 */
1547 #define __cgo_compile_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2UL+1UL];
1548
1549 /* Check at compile time that the sizes we use match our expectations. */
1550 #define __cgo_size_assert(t, n) __cgo_compile_assert_eq(sizeof(t), (size_t)n, _cgo_sizeof_##t##_is_not_##n)
1551
1552 __cgo_size_assert(char, 1)
1553 __cgo_size_assert(short, 2)
1554 __cgo_size_assert(int, 4)
1555 typedef long long __cgo_long_long;
1556 __cgo_size_assert(__cgo_long_long, 8)
1557 __cgo_size_assert(float, 4)
1558 __cgo_size_assert(double, 8)
1559
1560 extern char* _cgo_topofstack(void);
1561
1562 /*
1563 We use packed structs, but they are always aligned.
1564 The pragmas and address-of-packed-member are only recognized as warning
1565 groups in clang 4.0+, so ignore unknown pragmas first.
1566 */
1567 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
1568 #pragma GCC diagnostic ignored "-Wpragmas"
1569 #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
1570 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
1571 #pragma GCC diagnostic ignored "-Wunaligned-access"
1572
1573 #include <errno.h>
1574 #include <string.h>
1575 `
1576
1577
1578 const noTsanProlog = `
1579 #define CGO_NO_SANITIZE_THREAD
1580 #define _cgo_tsan_acquire()
1581 #define _cgo_tsan_release()
1582 `
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606 const yesTsanProlog = `
1607 #line 1 "cgo-tsan-prolog"
1608 #define CGO_NO_SANITIZE_THREAD __attribute__ ((no_sanitize_thread))
1609
1610 long long _cgo_sync __attribute__ ((common));
1611
1612 extern void __tsan_acquire(void*);
1613 extern void __tsan_release(void*);
1614
1615 __attribute__ ((unused))
1616 static void _cgo_tsan_acquire() {
1617 __tsan_acquire(&_cgo_sync);
1618 }
1619
1620 __attribute__ ((unused))
1621 static void _cgo_tsan_release() {
1622 __tsan_release(&_cgo_sync);
1623 }
1624 `
1625
1626
1627 var tsanProlog = noTsanProlog
1628
1629
1630
1631 const noMsanProlog = `
1632 #define _cgo_msan_write(addr, sz)
1633 `
1634
1635
1636
1637
1638 const yesMsanProlog = `
1639 extern void __msan_unpoison(const volatile void *, size_t);
1640
1641 #define _cgo_msan_write(addr, sz) __msan_unpoison((addr), (sz))
1642 `
1643
1644
1645
1646 var msanProlog = noMsanProlog
1647
1648 const builtinProlog = `
1649 #line 1 "cgo-builtin-prolog"
1650 #include <stddef.h>
1651
1652 /* Define intgo when compiling with GCC. */
1653 typedef ptrdiff_t intgo;
1654
1655 #define GO_CGO_GOSTRING_TYPEDEF
1656 typedef struct { const char *p; intgo n; } _GoString_;
1657 typedef struct { char *p; intgo n; intgo c; } _GoBytes_;
1658 _GoString_ GoString(char *p);
1659 _GoString_ GoStringN(char *p, int l);
1660 _GoBytes_ GoBytes(void *p, int n);
1661 char *CString(_GoString_);
1662 void *CBytes(_GoBytes_);
1663 void *_CMalloc(size_t);
1664
1665 __attribute__ ((unused))
1666 static size_t _GoStringLen(_GoString_ s) { return (size_t)s.n; }
1667
1668 __attribute__ ((unused))
1669 static const char *_GoStringPtr(_GoString_ s) { return s.p; }
1670 `
1671
1672 const goProlog = `
1673 //go:linkname _cgo_runtime_cgocall runtime.cgocall
1674 func _cgo_runtime_cgocall(unsafe.Pointer, uintptr) int32
1675
1676 //go:linkname _cgoCheckPointer runtime.cgoCheckPointer
1677 //go:noescape
1678 func _cgoCheckPointer(interface{}, interface{})
1679
1680 //go:linkname _cgoCheckResult runtime.cgoCheckResult
1681 //go:noescape
1682 func _cgoCheckResult(interface{})
1683 `
1684
1685 const gccgoGoProlog = `
1686 func _cgoCheckPointer(interface{}, interface{})
1687
1688 func _cgoCheckResult(interface{})
1689 `
1690
1691 const goStringDef = `
1692 //go:linkname _cgo_runtime_gostring runtime.gostring
1693 func _cgo_runtime_gostring(*_Ctype_char) string
1694
1695 // GoString converts the C string p into a Go string.
1696 func _Cfunc_GoString(p *_Ctype_char) string {
1697 return _cgo_runtime_gostring(p)
1698 }
1699 `
1700
1701 const goStringNDef = `
1702 //go:linkname _cgo_runtime_gostringn runtime.gostringn
1703 func _cgo_runtime_gostringn(*_Ctype_char, int) string
1704
1705 // GoStringN converts the C data p with explicit length l to a Go string.
1706 func _Cfunc_GoStringN(p *_Ctype_char, l _Ctype_int) string {
1707 return _cgo_runtime_gostringn(p, int(l))
1708 }
1709 `
1710
1711 const goBytesDef = `
1712 //go:linkname _cgo_runtime_gobytes runtime.gobytes
1713 func _cgo_runtime_gobytes(unsafe.Pointer, int) []byte
1714
1715 // GoBytes converts the C data p with explicit length l to a Go []byte.
1716 func _Cfunc_GoBytes(p unsafe.Pointer, l _Ctype_int) []byte {
1717 return _cgo_runtime_gobytes(p, int(l))
1718 }
1719 `
1720
1721 const cStringDef = `
1722 // CString converts the Go string s to a C string.
1723 //
1724 // The C string is allocated in the C heap using malloc.
1725 // It is the caller's responsibility to arrange for it to be
1726 // freed, such as by calling C.free (be sure to include stdlib.h
1727 // if C.free is needed).
1728 func _Cfunc_CString(s string) *_Ctype_char {
1729 if len(s)+1 <= 0 {
1730 panic("string too large")
1731 }
1732 p := _cgo_cmalloc(uint64(len(s)+1))
1733 sliceHeader := struct {
1734 p unsafe.Pointer
1735 len int
1736 cap int
1737 }{p, len(s)+1, len(s)+1}
1738 b := *(*[]byte)(unsafe.Pointer(&sliceHeader))
1739 copy(b, s)
1740 b[len(s)] = 0
1741 return (*_Ctype_char)(p)
1742 }
1743 `
1744
1745 const cBytesDef = `
1746 // CBytes converts the Go []byte slice b to a C array.
1747 //
1748 // The C array is allocated in the C heap using malloc.
1749 // It is the caller's responsibility to arrange for it to be
1750 // freed, such as by calling C.free (be sure to include stdlib.h
1751 // if C.free is needed).
1752 func _Cfunc_CBytes(b []byte) unsafe.Pointer {
1753 p := _cgo_cmalloc(uint64(len(b)))
1754 sliceHeader := struct {
1755 p unsafe.Pointer
1756 len int
1757 cap int
1758 }{p, len(b), len(b)}
1759 s := *(*[]byte)(unsafe.Pointer(&sliceHeader))
1760 copy(s, b)
1761 return p
1762 }
1763 `
1764
1765 const cMallocDef = `
1766 func _Cfunc__CMalloc(n _Ctype_size_t) unsafe.Pointer {
1767 return _cgo_cmalloc(uint64(n))
1768 }
1769 `
1770
1771 var builtinDefs = map[string]string{
1772 "GoString": goStringDef,
1773 "GoStringN": goStringNDef,
1774 "GoBytes": goBytesDef,
1775 "CString": cStringDef,
1776 "CBytes": cBytesDef,
1777 "_CMalloc": cMallocDef,
1778 }
1779
1780
1781
1782
1783
1784
1785 const cMallocDefGo = `
1786 //go:cgo_import_static _cgoPREFIX_Cfunc__Cmalloc
1787 //go:linkname __cgofn__cgoPREFIX_Cfunc__Cmalloc _cgoPREFIX_Cfunc__Cmalloc
1788 var __cgofn__cgoPREFIX_Cfunc__Cmalloc byte
1789 var _cgoPREFIX_Cfunc__Cmalloc = unsafe.Pointer(&__cgofn__cgoPREFIX_Cfunc__Cmalloc)
1790
1791 //go:linkname runtime_throw runtime.throw
1792 func runtime_throw(string)
1793
1794 //go:cgo_unsafe_args
1795 func _cgo_cmalloc(p0 uint64) (r1 unsafe.Pointer) {
1796 _cgo_runtime_cgocall(_cgoPREFIX_Cfunc__Cmalloc, uintptr(unsafe.Pointer(&p0)))
1797 if r1 == nil {
1798 runtime_throw("runtime: C malloc failed")
1799 }
1800 return
1801 }
1802 `
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812 const cMallocDefC = `
1813 CGO_NO_SANITIZE_THREAD
1814 void _cgoPREFIX_Cfunc__Cmalloc(void *v) {
1815 struct {
1816 unsigned long long p0;
1817 void *r1;
1818 } PACKED *a = v;
1819 void *ret;
1820 _cgo_tsan_acquire();
1821 ret = malloc(a->p0);
1822 if (ret == NULL && a->p0 == 0) {
1823 ret = malloc(1);
1824 }
1825 a->r1 = ret;
1826 _cgo_tsan_release();
1827 }
1828 `
1829
1830 func (p *Package) cPrologGccgo() string {
1831 r := strings.NewReplacer(
1832 "PREFIX", cPrefix,
1833 "GCCGOSYMBOLPREF", p.gccgoSymbolPrefix(),
1834 "_cgoCheckPointer", gccgoToSymbol("_cgoCheckPointer"),
1835 "_cgoCheckResult", gccgoToSymbol("_cgoCheckResult"))
1836 return r.Replace(cPrologGccgo)
1837 }
1838
1839 const cPrologGccgo = `
1840 #line 1 "cgo-c-prolog-gccgo"
1841 #include <stdint.h>
1842 #include <stdlib.h>
1843 #include <string.h>
1844
1845 typedef unsigned char byte;
1846 typedef intptr_t intgo;
1847
1848 struct __go_string {
1849 const unsigned char *__data;
1850 intgo __length;
1851 };
1852
1853 typedef struct __go_open_array {
1854 void* __values;
1855 intgo __count;
1856 intgo __capacity;
1857 } Slice;
1858
1859 struct __go_string __go_byte_array_to_string(const void* p, intgo len);
1860 struct __go_open_array __go_string_to_byte_array (struct __go_string str);
1861
1862 extern void runtime_throw(const char *);
1863
1864 const char *_cgoPREFIX_Cfunc_CString(struct __go_string s) {
1865 char *p = malloc(s.__length+1);
1866 if(p == NULL)
1867 runtime_throw("runtime: C malloc failed");
1868 memmove(p, s.__data, s.__length);
1869 p[s.__length] = 0;
1870 return p;
1871 }
1872
1873 void *_cgoPREFIX_Cfunc_CBytes(struct __go_open_array b) {
1874 char *p = malloc(b.__count);
1875 if(p == NULL)
1876 runtime_throw("runtime: C malloc failed");
1877 memmove(p, b.__values, b.__count);
1878 return p;
1879 }
1880
1881 struct __go_string _cgoPREFIX_Cfunc_GoString(char *p) {
1882 intgo len = (p != NULL) ? strlen(p) : 0;
1883 return __go_byte_array_to_string(p, len);
1884 }
1885
1886 struct __go_string _cgoPREFIX_Cfunc_GoStringN(char *p, int32_t n) {
1887 return __go_byte_array_to_string(p, n);
1888 }
1889
1890 Slice _cgoPREFIX_Cfunc_GoBytes(char *p, int32_t n) {
1891 struct __go_string s = { (const unsigned char *)p, n };
1892 return __go_string_to_byte_array(s);
1893 }
1894
1895 void *_cgoPREFIX_Cfunc__CMalloc(size_t n) {
1896 void *p = malloc(n);
1897 if(p == NULL && n == 0)
1898 p = malloc(1);
1899 if(p == NULL)
1900 runtime_throw("runtime: C malloc failed");
1901 return p;
1902 }
1903
1904 struct __go_type_descriptor;
1905 typedef struct __go_empty_interface {
1906 const struct __go_type_descriptor *__type_descriptor;
1907 void *__object;
1908 } Eface;
1909
1910 extern void runtimeCgoCheckPointer(Eface, Eface)
1911 __asm__("runtime.cgoCheckPointer")
1912 __attribute__((weak));
1913
1914 extern void localCgoCheckPointer(Eface, Eface)
1915 __asm__("GCCGOSYMBOLPREF._cgoCheckPointer");
1916
1917 void localCgoCheckPointer(Eface ptr, Eface arg) {
1918 if(runtimeCgoCheckPointer) {
1919 runtimeCgoCheckPointer(ptr, arg);
1920 }
1921 }
1922
1923 extern void runtimeCgoCheckResult(Eface)
1924 __asm__("runtime.cgoCheckResult")
1925 __attribute__((weak));
1926
1927 extern void localCgoCheckResult(Eface)
1928 __asm__("GCCGOSYMBOLPREF._cgoCheckResult");
1929
1930 void localCgoCheckResult(Eface val) {
1931 if(runtimeCgoCheckResult) {
1932 runtimeCgoCheckResult(val);
1933 }
1934 }
1935 `
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946 const builtinExportProlog = `
1947 #line 1 "cgo-builtin-export-prolog"
1948
1949 #include <stddef.h>
1950
1951 #ifndef GO_CGO_EXPORT_PROLOGUE_H
1952 #define GO_CGO_EXPORT_PROLOGUE_H
1953
1954 #ifndef GO_CGO_GOSTRING_TYPEDEF
1955 typedef struct { const char *p; ptrdiff_t n; } _GoString_;
1956 extern size_t _GoStringLen(_GoString_ s);
1957 extern const char *_GoStringPtr(_GoString_ s);
1958 #endif
1959
1960 #endif
1961 `
1962
1963 func (p *Package) gccExportHeaderProlog() string {
1964 return strings.ReplaceAll(gccExportHeaderProlog, "GOINTBITS", fmt.Sprint(8*p.IntSize))
1965 }
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980 const gccExportHeaderProlog = `
1981 /* Start of boilerplate cgo prologue. */
1982 #line 1 "cgo-gcc-export-header-prolog"
1983
1984 #ifndef GO_CGO_PROLOGUE_H
1985 #define GO_CGO_PROLOGUE_H
1986
1987 typedef signed char GoInt8;
1988 typedef unsigned char GoUint8;
1989 typedef short GoInt16;
1990 typedef unsigned short GoUint16;
1991 typedef int GoInt32;
1992 typedef unsigned int GoUint32;
1993 typedef long long GoInt64;
1994 typedef unsigned long long GoUint64;
1995 typedef GoIntGOINTBITS GoInt;
1996 typedef GoUintGOINTBITS GoUint;
1997 typedef size_t GoUintptr;
1998 typedef float GoFloat32;
1999 typedef double GoFloat64;
2000 #ifdef _MSC_VER
2001 #if !defined(__cplusplus) || _MSVC_LANG <= 201402L
2002 #include <complex.h>
2003 typedef _Fcomplex GoComplex64;
2004 typedef _Dcomplex GoComplex128;
2005 #else
2006 #include <complex>
2007 typedef std::complex<float> GoComplex64;
2008 typedef std::complex<double> GoComplex128;
2009 #endif
2010 #else
2011 typedef float _Complex GoComplex64;
2012 typedef double _Complex GoComplex128;
2013 #endif
2014
2015 /*
2016 static assertion to make sure the file is being used on architecture
2017 at least with matching size of GoInt.
2018 */
2019 typedef char _check_for_GOINTBITS_bit_pointer_matching_GoInt[sizeof(void*)==GOINTBITS/8 ? 1:-1];
2020
2021 #ifndef GO_CGO_GOSTRING_TYPEDEF
2022 typedef _GoString_ GoString;
2023 #endif
2024 typedef void *GoMap;
2025 typedef void *GoChan;
2026 typedef struct { void *t; void *v; } GoInterface;
2027 typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
2028
2029 #endif
2030
2031 /* End of boilerplate cgo prologue. */
2032
2033 #ifdef __cplusplus
2034 extern "C" {
2035 #endif
2036 `
2037
2038
2039 const gccExportHeaderEpilog = `
2040 #ifdef __cplusplus
2041 }
2042 #endif
2043 `
2044
2045
2046
2047
2048
2049 const gccgoExportFileProlog = `
2050 #line 1 "cgo-gccgo-export-file-prolog"
2051 extern _Bool runtime_iscgo __attribute__ ((weak));
2052
2053 static void GoInit(void) __attribute__ ((constructor));
2054 static void GoInit(void) {
2055 if(&runtime_iscgo)
2056 runtime_iscgo = 1;
2057 }
2058
2059 extern size_t _cgo_wait_runtime_init_done(void) __attribute__ ((weak));
2060 `
2061
View as plain text