Debian Codegraph @

src/cmd/cgo/main.go

raw
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Cgo; see doc.go for an overview.
6
7 // TODO(rsc):
8 // Emit correct line number annotations.
9 // Make gc understand the annotations.
10
11 package main
12
13 import (
14 "flag"
15 "fmt"
16 "go/ast"
17 "go/printer"
18 "go/token"
19 "internal/buildcfg"
20 "io"
21 "maps"
22 "os"
23 "path/filepath"
24 "reflect"
25 "runtime"
26 "sort"
27 "strings"
28 "sync"
29
30 "cmd/internal/edit"
31 "cmd/internal/hash"
32 "cmd/internal/objabi"
33 "cmd/internal/par"
34 "cmd/internal/telemetry/counter"
35 )
36
37 // A Package collects information about the package we're going to write.
38 type Package struct {
39 PackageName string // name of package
40 PackagePath string
41 PtrSize int64
42 IntSize int64
43 GccOptions []string
44 GccIsClang bool
45 LdFlags []string // #cgo LDFLAGS
46 Written map[string]bool
47 Name map[string]*Name // accumulated Name from Files
48 ExpFunc []*ExpFunc // accumulated ExpFunc from Files
49 Decl []ast.Decl
50 GoFiles []string // list of Go files
51 GccFiles []string // list of gcc output files
52 Preamble string // collected preamble for _cgo_export.h
53 noCallbacks map[string]bool // C function names with #cgo nocallback directive
54 noEscapes map[string]bool // C function names with #cgo noescape directive
55 }
56
57 // A typedefInfo is an element on Package.typedefList: a typedef name
58 // and the position where it was required.
59 type typedefInfo struct {
60 typedef string
61 pos token.Pos
62 }
63
64 // A File collects information about a single Go input file.
65 type File struct {
66 AST *ast.File // parsed AST
67 Comments []*ast.CommentGroup // comments from file
68 Package string // Package name
69 Preamble string // C preamble (doc comment on import "C")
70 Ref []*Ref // all references to C.xxx in AST
71 Calls []*Call // all calls to C.xxx in AST
72 ExpFunc []*ExpFunc // exported functions for this file
73 Name map[string]*Name // map from Go name to Name
74 NamePos map[*Name]token.Pos // map from Name to position of the first reference
75 NoCallbacks map[string]bool // C function names with #cgo nocallback directive
76 NoEscapes map[string]bool // C function names with #cgo noescape directive
77 Edit *edit.Buffer
78
79 debugs []*debug // debug data from iterations of gccDebug. Initialized by File.loadDebug.
80 }
81
82 func (f *File) offset(p token.Pos) int {
83 return fset.Position(p).Offset
84 }
85
86 func nameKeys(m map[string]*Name) []string {
87 ks := make([]string, 0, len(m))
88 for k := range m {
89 ks = append(ks, k)
90 }
91 sort.Strings(ks)
92 return ks
93 }
94
95 // A Call refers to a call of a C.xxx function in the AST.
96 type Call struct {
97 Call *ast.CallExpr
98 Deferred bool
99 Done bool
100 }
101
102 // A Ref refers to an expression of the form C.xxx in the AST.
103 type Ref struct {
104 Name *Name
105 Expr *ast.Expr
106 Context astContext
107 Done bool
108 }
109
110 func (r *Ref) Pos() token.Pos {
111 return (*r.Expr).Pos()
112 }
113
114 var nameKinds = []string{"iconst", "fconst", "sconst", "type", "var", "fpvar", "func", "macro", "not-type"}
115
116 // A Name collects information about C.xxx.
117 type Name struct {
118 Go string // name used in Go referring to package C
119 Mangle string // name used in generated Go
120 C string // name used in C
121 Define string // #define expansion
122 Kind string // one of the nameKinds
123 Type *Type // the type of xxx
124 FuncType *FuncType
125 AddError bool
126 Const string // constant definition
127 }
128
129 // IsVar reports whether Kind is either "var" or "fpvar"
130 func (n *Name) IsVar() bool {
131 return n.Kind == "var" || n.Kind == "fpvar"
132 }
133
134 // IsConst reports whether Kind is either "iconst", "fconst" or "sconst"
135 func (n *Name) IsConst() bool {
136 return strings.HasSuffix(n.Kind, "const")
137 }
138
139 // An ExpFunc is an exported function, callable from C.
140 // Such functions are identified in the Go input file
141 // by doc comments containing the line //export ExpName
142 type ExpFunc struct {
143 Func *ast.FuncDecl
144 ExpName string // name to use from C
145 Doc string
146 }
147
148 // A TypeRepr contains the string representation of a type.
149 type TypeRepr struct {
150 Repr string
151 FormatArgs []any
152 }
153
154 // A Type collects information about a type in both the C and Go worlds.
155 type Type struct {
156 Size int64
157 Align int64
158 C *TypeRepr
159 Go ast.Expr
160 EnumValues map[string]int64
161 Typedef string
162 BadPointer bool // this pointer type should be represented as a uintptr (deprecated)
163 }
164
165 func (t *Type) fuzzyMatch(t2 *Type) bool {
166 if t == nil || t2 == nil {
167 return false
168 }
169 return t.Size == t2.Size && t.Align == t2.Align
170 }
171
172 // A FuncType collects information about a function type in both the C and Go worlds.
173 type FuncType struct {
174 Params []*Type
175 Result *Type
176 Go *ast.FuncType
177 }
178
179 func (t *FuncType) fuzzyMatch(t2 *FuncType) bool {
180 if t == nil || t2 == nil {
181 return false
182 }
183 if !t.Result.fuzzyMatch(t2.Result) {
184 return false
185 }
186 if len(t.Params) != len(t2.Params) {
187 return false
188 }
189 for i := range t.Params {
190 if !t.Params[i].fuzzyMatch(t2.Params[i]) {
191 return false
192 }
193 }
194 return true
195 }
196
197 func usage() {
198 fmt.Fprint(os.Stderr, "usage: cgo -- [compiler options] file.go ...\n")
199 flag.PrintDefaults()
200 os.Exit(2)
201 }
202
203 var ptrSizeMap = map[string]int64{
204 "386": 4,
205 "alpha": 8,
206 "amd64": 8,
207 "arm": 4,
208 "arm64": 8,
209 "loong64": 8,
210 "m68k": 4,
211 "mips": 4,
212 "mipsle": 4,
213 "mips64": 8,
214 "mips64le": 8,
215 "nios2": 4,
216 "ppc": 4,
217 "ppc64": 8,
218 "ppc64le": 8,
219 "riscv": 4,
220 "riscv64": 8,
221 "s390": 4,
222 "s390x": 8,
223 "sh": 4,
224 "shbe": 4,
225 "sparc": 4,
226 "sparc64": 8,
227 }
228
229 var intSizeMap = map[string]int64{
230 "386": 4,
231 "alpha": 8,
232 "amd64": 8,
233 "arm": 4,
234 "arm64": 8,
235 "loong64": 8,
236 "m68k": 4,
237 "mips": 4,
238 "mipsle": 4,
239 "mips64": 8,
240 "mips64le": 8,
241 "nios2": 4,
242 "ppc": 4,
243 "ppc64": 8,
244 "ppc64le": 8,
245 "riscv": 4,
246 "riscv64": 8,
247 "s390": 4,
248 "s390x": 8,
249 "sh": 4,
250 "shbe": 4,
251 "sparc": 4,
252 "sparc64": 8,
253 }
254
255 var cPrefix string
256
257 var fset = token.NewFileSet()
258
259 var dynobj = flag.String("dynimport", "", "if non-empty, print dynamic import data for that file")
260 var dynout = flag.String("dynout", "", "write -dynimport output to this file")
261 var dynpackage = flag.String("dynpackage", "main", "set Go package for -dynimport output")
262 var dynlinker = flag.Bool("dynlinker", false, "record dynamic linker information in -dynimport mode")
263
264 // This flag is for bootstrapping a new Go implementation,
265 // to generate Go types that match the data layout and
266 // constant values used in the host's C libraries and system calls.
267 var godefs = flag.Bool("godefs", false, "for bootstrap: write Go definitions for C file to standard output")
268
269 var srcDir = flag.String("srcdir", "", "source directory")
270 var objDir = flag.String("objdir", "", "object directory")
271 var importPath = flag.String("importpath", "", "import path of package being built (for comments in generated files)")
272 var exportHeader = flag.String("exportheader", "", "where to write export header if any exported functions")
273
274 var ldflags = flag.String("ldflags", "", "flags to pass to C linker")
275
276 var gccgo = flag.Bool("gccgo", false, "generate files for use with gccgo")
277 var gccgoprefix = flag.String("gccgoprefix", "", "-fgo-prefix option used with gccgo")
278 var gccgopkgpath = flag.String("gccgopkgpath", "", "-fgo-pkgpath option used with gccgo")
279 var gccgoMangler func(string) string
280 var gccgoDefineCgoIncomplete = flag.Bool("gccgo_define_cgoincomplete", false, "define cgo.Incomplete for older gccgo/GoLLVM")
281 var importRuntimeCgo = flag.Bool("import_runtime_cgo", true, "import runtime/cgo in generated code")
282 var importSyscall = flag.Bool("import_syscall", true, "import syscall in generated code")
283 var trimpath = flag.String("trimpath", "", "applies supplied rewrites or trims prefixes to recorded source file paths")
284
285 var goarch, goos, gomips, gomips64 string
286 var gccBaseCmd []string
287
288 func main() {
289 counter.Open()
290 objabi.AddVersionFlag() // -V
291 objabi.Flagparse(usage)
292 counter.Inc("cgo/invocations")
293 counter.CountFlags("cgo/flag:", *flag.CommandLine)
294
295 if *gccgoDefineCgoIncomplete {
296 if !*gccgo {
297 fmt.Fprintf(os.Stderr, "cgo: -gccgo_define_cgoincomplete without -gccgo\n")
298 os.Exit(2)
299 }
300 incomplete = "_cgopackage_Incomplete"
301 }
302
303 if *dynobj != "" {
304 // cgo -dynimport is essentially a separate helper command
305 // built into the cgo binary. It scans a gcc-produced executable
306 // and dumps information about the imported symbols and the
307 // imported libraries. The 'go build' rules for cgo prepare an
308 // appropriate executable and then use its import information
309 // instead of needing to make the linkers duplicate all the
310 // specialized knowledge gcc has about where to look for imported
311 // symbols and which ones to use.
312 dynimport(*dynobj)
313 return
314 }
315
316 if *godefs {
317 // Generating definitions pulled from header files,
318 // to be checked into Go repositories.
319 // Line numbers are just noise.
320 conf.Mode &^= printer.SourcePos
321 }
322
323 args := flag.Args()
324 if len(args) < 1 {
325 usage()
326 }
327
328 // Find first arg that looks like a go file and assume everything before
329 // that are options to pass to gcc.
330 var i int
331 for i = len(args); i > 0; i-- {
332 if !strings.HasSuffix(args[i-1], ".go") {
333 break
334 }
335 }
336 if i == len(args) {
337 usage()
338 }
339
340 // Save original command line arguments for the godefs generated comment. Relative file
341 // paths in os.Args will be rewritten to absolute file paths in the loop below.
342 osArgs := make([]string, len(os.Args))
343 copy(osArgs, os.Args[:])
344 goFiles := args[i:]
345
346 for _, arg := range args[:i] {
347 if arg == "-fsanitize=thread" {
348 tsanProlog = yesTsanProlog
349 }
350 if arg == "-fsanitize=memory" {
351 msanProlog = yesMsanProlog
352 }
353 }
354
355 p := newPackage(args[:i])
356
357 // We need a C compiler to be available. Check this.
358 var err error
359 gccBaseCmd, err = checkGCCBaseCmd()
360 if err != nil {
361 fatalf("%v", err)
362 os.Exit(2)
363 }
364
365 // Record linker flags for external linking.
366 if *ldflags != "" {
367 args, err := splitQuoted(*ldflags)
368 if err != nil {
369 fatalf("bad -ldflags option: %q (%s)", *ldflags, err)
370 }
371 p.addToFlag("LDFLAGS", args)
372 }
373
374 // For backward compatibility for Bazel, record CGO_LDFLAGS
375 // from the environment for external linking.
376 // This should not happen with cmd/go, which removes CGO_LDFLAGS
377 // from the environment when invoking cgo.
378 // This can be removed when we no longer need to support
379 // older versions of Bazel. See issue #66456 and
380 // https://github.com/bazelbuild/rules_go/issues/3979.
381 if envFlags := os.Getenv("CGO_LDFLAGS"); envFlags != "" {
382 args, err := splitQuoted(envFlags)
383 if err != nil {
384 fatalf("bad CGO_LDFLAGS: %q (%s)", envFlags, err)
385 }
386 p.addToFlag("LDFLAGS", args)
387 }
388
389 // Need a unique prefix for the global C symbols that
390 // we use to coordinate between gcc and ourselves.
391 // We already put _cgo_ at the beginning, so the main
392 // concern is other cgo wrappers for the same functions.
393 // Use the beginning of the 16 bytes hash of the input to disambiguate.
394 h := hash.New32()
395 io.WriteString(h, *importPath)
396 var once sync.Once
397 q := par.NewQueue(runtime.GOMAXPROCS(0))
398 fs := make([]*File, len(goFiles))
399 for i, input := range goFiles {
400 if *srcDir != "" {
401 input = filepath.Join(*srcDir, input)
402 }
403
404 // Create absolute path for file, so that it will be used in error
405 // messages and recorded in debug line number information.
406 // This matches the rest of the toolchain. See golang.org/issue/5122.
407 if aname, err := filepath.Abs(input); err == nil {
408 input = aname
409 }
410
411 b, err := os.ReadFile(input)
412 if err != nil {
413 fatalf("%s", err)
414 }
415 if _, err = h.Write(b); err != nil {
416 fatalf("%s", err)
417 }
418
419 q.Add(func() {
420 // Apply trimpath to the file path. The path won't be read from after this point.
421 input, _ = objabi.ApplyRewrites(input, *trimpath)
422 if strings.ContainsAny(input, "\r\n") {
423 // ParseGo, (*Package).writeOutput, and printer.Fprint in SourcePos mode
424 // all emit line directives, which don't permit newlines in the file path.
425 // Bail early if we see anything newline-like in the trimmed path.
426 fatalf("input path contains newline character: %q", input)
427 }
428 goFiles[i] = input
429
430 f := new(File)
431 f.Edit = edit.NewBuffer(b)
432 f.ParseGo(input, b)
433 f.ProcessCgoDirectives()
434 gccIsClang := f.loadDefines(p.GccOptions)
435 once.Do(func() {
436 p.GccIsClang = gccIsClang
437 })
438
439 fs[i] = f
440
441 f.loadDebug(p)
442 })
443 }
444
445 <-q.Idle()
446
447 cPrefix = fmt.Sprintf("_%x", h.Sum(nil)[0:6])
448
449 if *objDir == "" {
450 *objDir = "_obj"
451 }
452 // make sure that `objDir` directory exists, so that we can write
453 // all the output files there.
454 os.MkdirAll(*objDir, 0o700)
455 *objDir += string(filepath.Separator)
456
457 for i, input := range goFiles {
458 f := fs[i]
459 p.Translate(f)
460 for _, cref := range f.Ref {
461 switch cref.Context {
462 case ctxCall, ctxCall2:
463 if cref.Name.Kind != "type" {
464 break
465 }
466 old := *cref.Expr
467 *cref.Expr = cref.Name.Type.Go
468 f.Edit.Replace(f.offset(old.Pos()), f.offset(old.End()), gofmt(cref.Name.Type.Go))
469 }
470 }
471 if nerrors > 0 {
472 os.Exit(2)
473 }
474 p.PackagePath = f.Package
475 p.Record(f)
476 if *godefs {
477 os.Stdout.WriteString(p.godefs(f, osArgs))
478 } else {
479 p.writeOutput(f, input)
480 }
481 }
482 cFunctions := make(map[string]bool)
483 for _, key := range nameKeys(p.Name) {
484 n := p.Name[key]
485 if n.FuncType != nil {
486 cFunctions[n.C] = true
487 }
488 }
489
490 for funcName := range p.noEscapes {
491 if _, found := cFunctions[funcName]; !found {
492 error_(token.NoPos, "#cgo noescape %s: no matched C function", funcName)
493 }
494 }
495
496 for funcName := range p.noCallbacks {
497 if _, found := cFunctions[funcName]; !found {
498 error_(token.NoPos, "#cgo nocallback %s: no matched C function", funcName)
499 }
500 }
501
502 if !*godefs {
503 p.writeDefs()
504 }
505 if nerrors > 0 {
506 os.Exit(2)
507 }
508 }
509
510 // newPackage returns a new Package that will invoke
511 // gcc with the additional arguments specified in args.
512 func newPackage(args []string) *Package {
513 goarch = runtime.GOARCH
514 if s := os.Getenv("GOARCH"); s != "" {
515 goarch = s
516 }
517 goos = runtime.GOOS
518 if s := os.Getenv("GOOS"); s != "" {
519 goos = s
520 }
521 buildcfg.Check()
522 gomips = buildcfg.GOMIPS
523 gomips64 = buildcfg.GOMIPS64
524 ptrSize := ptrSizeMap[goarch]
525 if ptrSize == 0 {
526 fatalf("unknown ptrSize for $GOARCH %q", goarch)
527 }
528 intSize := intSizeMap[goarch]
529 if intSize == 0 {
530 fatalf("unknown intSize for $GOARCH %q", goarch)
531 }
532
533 // Reset locale variables so gcc emits English errors [sic].
534 os.Setenv("LANG", "en_US.UTF-8")
535 os.Setenv("LC_ALL", "C")
536
537 p := &Package{
538 PtrSize: ptrSize,
539 IntSize: intSize,
540 Written: make(map[string]bool),
541 noCallbacks: make(map[string]bool),
542 noEscapes: make(map[string]bool),
543 }
544 p.addToFlag("CFLAGS", args)
545 return p
546 }
547
548 // Record what needs to be recorded about f.
549 func (p *Package) Record(f *File) {
550 if p.PackageName == "" {
551 p.PackageName = f.Package
552 } else if p.PackageName != f.Package {
553 error_(token.NoPos, "inconsistent package names: %s, %s", p.PackageName, f.Package)
554 }
555
556 if p.Name == nil {
557 p.Name = f.Name
558 } else {
559 // Merge the new file's names in with the existing names.
560 for k, v := range f.Name {
561 if p.Name[k] == nil {
562 // Never seen before, just save it.
563 p.Name[k] = v
564 } else if p.incompleteTypedef(p.Name[k].Type) && p.Name[k].FuncType == nil {
565 // Old one is incomplete, just use new one.
566 p.Name[k] = v
567 } else if p.incompleteTypedef(v.Type) && v.FuncType == nil {
568 // New one is incomplete, just use old one.
569 // Nothing to do.
570 } else if _, ok := nameToC[k]; ok {
571 // Names we predefine may appear inconsistent
572 // if some files typedef them and some don't.
573 // Issue 26743.
574 } else if !reflect.DeepEqual(p.Name[k], v) {
575 // We don't require strict func type equality, because some functions
576 // can have things like typedef'd arguments that are equivalent to
577 // the standard arguments. e.g.
578 // int usleep(unsigned);
579 // int usleep(useconds_t);
580 // So we just check size/alignment of arguments. At least that
581 // avoids problems like those in #67670 and #67699.
582 ok := false
583 ft1 := p.Name[k].FuncType
584 ft2 := v.FuncType
585 if ft1.fuzzyMatch(ft2) {
586 // Retry DeepEqual with the FuncType field cleared.
587 x1 := *p.Name[k]
588 x2 := *v
589 x1.FuncType = nil
590 x2.FuncType = nil
591 if reflect.DeepEqual(&x1, &x2) {
592 ok = true
593 }
594 }
595 if !ok {
596 error_(token.NoPos, "inconsistent definitions for C.%s", fixGo(k))
597 }
598 }
599 }
600 }
601
602 // merge nocallback & noescape
603 maps.Copy(p.noCallbacks, f.NoCallbacks)
604 maps.Copy(p.noEscapes, f.NoEscapes)
605
606 if f.ExpFunc != nil {
607 p.ExpFunc = append(p.ExpFunc, f.ExpFunc...)
608 p.Preamble += "\n" + f.Preamble
609 }
610 p.Decl = append(p.Decl, f.AST.Decls...)
611 }
612
613 // incompleteTypedef reports whether t appears to be an incomplete
614 // typedef definition.
615 func (p *Package) incompleteTypedef(t *Type) bool {
616 return t == nil || (t.Size == 0 && t.Align == -1)
617 }