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