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