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