aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/cgo
AgeCommit message (Collapse)Author
2021-08-16cmd/cgo: fix unused parameter warnings in generated _cgo_main.cHans
Applying -Werror compiler option to request warnings is an usual way to discover potential errors. Go user may put a cgo directive in preamble: `// #cgo CFLAGS: -Werror=unused-parameter`. However, the directive also takes effect on the cgo generated files. I cleaned _cgo_main.c to help Go user only concentrate on warnings of their own file. Fixes #43639 Change-Id: I9112f02ae5226f2fc87a8650d19faee59cddd588 GitHub-Last-Rev: f09d172f979acfba855be8108e7d79ec2778c406 GitHub-Pull-Request: golang/go#46358 Reviewed-on: https://go-review.googlesource.com/c/go/+/322232 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Bryan C. Mills <bcmills@google.com>
2021-08-16cmd: support space and quotes in CC and CXXJay Conrod
The CC and CXX environment variables now support spaces and quotes (both double and single). This fixes two issues: first, if CC is a single path that contains spaces (like 'c:\Program Files\gcc\bin\gcc.exe'), that should now work if the space is quoted or escaped (#41400). Second, if CC or CXX has multiple arguments (like 'gcc -O2'), they are now split correctly, and the arguments are passed before other arguments when invoking the C compiler. Previously, strings.Fields was used to split arguments, and the arguments were placed later in the command line. (#43078). Fixes golang/go#41400 Fixes golang/go#43078 NOTE: This change also includes a fix (CL 341929) for a test that was broken by the original CL. Commit message for the fix is below. [dev.cmdgo] cmd/link: fix TestBuildForTvOS This test was broken in CL 334732 on darwin. The test invokes 'go build' with a CC containing the arguments -framework CoreFoundation. Previously, the go command split CC on whitespace, and inserted the arguments after the command line when running CC directly. Those arguments weren't passed to cgo though, so cgo ran CC without -framework CoreFoundation (or any of the other flags). In CL 334732, we pass CC through to cgo, and cgo splits arguments using str.SplitQuotedFields. So -framework CoreFoundation actually gets passed to the C compiler. It appears that -framework flags are only meant to be used in linking operations, so when cgo invokes clang with -E (run preprocessor only), clang emits an error that -framework is unused. This change fixes the test by moving -framework CoreFoundation out of CC and into CGO_LDFLAGS. Change-Id: I2d5d89ddb19c94adef65982a8137b01f037d5c11 Reviewed-on: https://go-review.googlesource.com/c/go/+/334732 Trust: Jay Conrod <jayconrod@google.com> Trust: Michael Matloob <matloob@golang.org> Run-TryBot: Jay Conrod <jayconrod@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org> Reviewed-on: https://go-review.googlesource.com/c/go/+/341936 Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-06-28cmd/cgo: fix 'see gmp.go' to 'see doc.go'Koichi Shiraishi
Change-Id: I303edc9dfbf4185b5b461b121ab504f6ed9f8630 Reviewed-on: https://go-review.googlesource.com/c/go/+/330839 Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Dmitri Shuralyov <dmitshur@golang.org>
2021-06-01cmd/link, cmd/cgo: support -flto in CFLAGSIan Lance Taylor
The linker now accepts unrecognized object files in external linking mode. These objects will simply be passed to the external linker. This permits using -flto which can generate pure byte code objects, whose symbol table the linker does not know how to read. The cgo tool now passes -fno-lto when generating objects whose symbols it needs to read. The cgo tool now emits matching types in different objects, so that the lto linker does not report a mismatch. This is based on https://golang.org/cl/293290 by Derek Parker. For #43505 Fixes #43830 Fixes #46295 Change-Id: I6787de213417466784ddef5af8899e453b4ae1ad Reviewed-on: https://go-review.googlesource.com/c/go/+/322614 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Michael Hudson-Doyle <michael.hudson@canonical.com>
2021-04-16internal/buildcfg: move build configuration out of cmd/internal/objabiRuss Cox
The go/build package needs access to this configuration, so move it into a new package available to the standard library. Change-Id: I868a94148b52350c76116451f4ad9191246adcff Reviewed-on: https://go-review.googlesource.com/c/go/+/310731 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Austin Clements <austin@google.com> Reviewed-by: Jay Conrod <jayconrod@google.com>
2021-04-13cmd/cgo: document generated cgo directivesAustin Clements
This took me a while to figure out. Save the next person some trouble. Change-Id: Ifab2d426f67c21b08ef225c79125805a9008e578 Reviewed-on: https://go-review.googlesource.com/c/go/+/309336 Trust: Austin Clements <austin@google.com> Run-TryBot: Austin Clements <austin@google.com> Reviewed-by: Cherry Zhang <cherryyz@google.com> Reviewed-by: Than McIntosh <thanm@google.com>
2021-04-12cmd/cgo: throw if C.malloc returns NULL in C.CString or C.CBytesTobias Klauser
Change-Id: Iea07b7f3e64f5938cfb1cd1c07bdce4adf8e4470 Reviewed-on: https://go-review.googlesource.com/c/go/+/308992 Trust: Tobias Klauser <tobias.klauser@gmail.com> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-04-12cmd/cgo: use tabs to indent _cgoPREFIX_Cfunc__CMalloc function bodyTobias Klauser
All other _cgoPREFIX_Cfunc_* functions are indented using tabs. Change-Id: Ic5cfccd3000d34d0bbe08d035f18640af5e05473 Reviewed-on: https://go-review.googlesource.com/c/go/+/308993 Trust: Tobias Klauser <tobias.klauser@gmail.com> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-04-06runtime/cgo: add Handle for managing (c)go pointersChangkun Ou
A non-trivial Cgo program may need to use callbacks and interact with go objects per goroutine. Because of the rules for passing pointers between Go and C, such a program needs to store handles to associated Go values. This often causes much extra effort to figure out a way to correctly deal with: 1) map collision; 2) identifying leaks and 3) concurrency. This CL implements a Handle representation in runtime/cgo package, and related methods such as Value, Delete, etc. which allows Go users can use a standard way to handle the above difficulties. In addition, the CL allows a Go value to have multiple handles, and the NewHandle always returns a different handle compare to the previously returned handles. In comparison, CL 294670 implements a different behavior of NewHandle that returns a unique handle when the Go value is referring to the same object. Benchmark: name time/op Handle/non-concurrent-16 487ns ± 1% Handle/concurrent-16 674ns ± 1% Fixes #37033 Change-Id: I0eadb9d44332fffef8fb567c745246a49dd6d4c1 Reviewed-on: https://go-review.googlesource.com/c/go/+/295369 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Cherry Zhang <cherryyz@google.com>
2021-04-05cmd/cgo: pass end position info for C function arguments.wolf1996
Pass information about original end position for c function arguments processed in pointer checking generated code. Fixes #42580 Change-Id: Ic8a578168362f0ca6055064dbbea092ad37477a6 Reviewed-on: https://go-review.googlesource.com/c/go/+/269760 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Trust: Dmitri Shuralyov <dmitshur@golang.org>
2021-03-17cmd/cgo: check whether C compiler existsAndrey Bokhanko
Currently we print a cryptic message if a C compiler doesn't exist. This patch adds more graceful handling. Fixes #44271 Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/301249 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: David Chase <drchase@google.com>
2021-02-24cmd/go, cmd/cgo: pass -mfp32 and -mhard/soft-float to MIPS GCCYunQiang Su
For mips32 currently, we are using FP32, while the gcc may be FPXX, which may generate .MIPS.abiflags and .gnu.attributes section with value as FPXX. So the kernel will treat the exe as FPXX, and may choose to use FR=1 FPU mode for it. Currently, in Go, we use 2 lwc1 to load both half of a double value to a pair of even-odd FPR. This behavior can only work with FR=0 mode. In FR=1 mode, all of 32 FPR are 64bit. If we lwc1 the high-half of a double value to an odd FPR, and try to use the previous even FPR to compute, the real high-half of even FPR will be unpredicatable. We set -mfp32 to force the gcc generate FP32 code and section value. More details about FP32/FPXX/FP64 are explained in: https://web.archive.org/web/20180828210612/https://dmz-portal.mips.com/wiki/MIPS_O32_ABI_-_FR0_and_FR1_Interlinking When GOMIPS/GOMIPS64 is set as softfloat, we should also pass -msoft-float to gcc. Here we also add -mno-odd-spreg option, since Loongson's CPU cannot use odd-number FR in FR=0 mode. Fixes #39435 Change-Id: I54026ad416a815fe43a9261ebf6d02e5519c3930 Reviewed-on: https://go-review.googlesource.com/c/go/+/237058 Reviewed-by: Cherry Zhang <cherryyz@google.com> Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Meng Zhuo <mzh@golangcn.org>
2021-01-21all: introduce and use internal/execabsRoland Shoemaker
Introduces a wrapper around os/exec, internal/execabs, for use in all commands. This wrapper prevents exec.LookPath and exec.Command from running executables in the current directory. All imports of os/exec in non-test files in cmd/ are replaced with imports of internal/execabs. This issue was reported by RyotaK. Fixes CVE-2021-3115 Fixes #43783 Change-Id: I0423451a6e27ec1e1d6f3fe929ab1ef69145c08f Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/955304 Reviewed-by: Russ Cox <rsc@google.com> Reviewed-by: Katie Hockman <katiehockman@google.com> Reviewed-on: https://go-review.googlesource.com/c/go/+/284783 Run-TryBot: Roland Shoemaker <roland@golang.org> Reviewed-by: Katie Hockman <katie@golang.org> Trust: Roland Shoemaker <roland@golang.org>
2021-01-21cmd/cgo: report exec errors a bit more clearlyRuss Cox
Change-Id: I0e6bebf0e2e6efdef4be880e0c6c7451b938924b Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/949417 Reviewed-by: Katie Hockman <katiehockman@google.com> Reviewed-by: Jay Conrod <jayconrod@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-on: https://go-review.googlesource.com/c/go/+/284781 Run-TryBot: Roland Shoemaker <roland@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Roland Shoemaker <roland@golang.org>
2021-01-14cmd/dist, cmd/go: pass -arch for C compilation on DarwinCherry Zhang
On Apple Silicon Mac, the C compiler has an annoying default target selection, depending on the ancestor processes' architecture. In particular, if the shell or IDE is x86, when running "go build" even with a native ARM64 Go toolchain, the C compiler defaults to x86, causing build failures. We pass "-arch" flag explicitly to avoid this situation. Fixes #43692. Fixes #43476. Updates golang/vscode-go#1087. Change-Id: I80b6a116a114e11e273c6886e377a1cc969fa3f6 Reviewed-on: https://go-review.googlesource.com/c/go/+/283812 Trust: Cherry Zhang <cherryyz@google.com> Run-TryBot: Cherry Zhang <cherryyz@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-01-14cmd/cgo: remove unnecessary space in cgo export headerQuim Muntal
The cgo header has an unnecessary space in the exported function definition on non-windows goos. This was introduced in go1.16 so it would be good to fix it before release. Example: // Current behavior, notice there is an unecessary space // between extern and void extern void Foo(); // With this CL extern void Foo(); Change-Id: Ic2c21f8d806fe35a7be7183dbfe35ac605b6e4f6 Reviewed-on: https://go-review.googlesource.com/c/go/+/283892 Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Katie Hockman <katie@golang.org>
2020-11-20cmd/cgo, cmd/internal/pkgpath: support gofrontend mangler v3Ian Lance Taylor
The gofrontend mangling scheme used by gccgo and GoLLVM has changed again. Support the new version. This is a port of the relevant parts of https://golang.org/cl/271726. For #41862 Change-Id: I9c961c8e17ec960a83a23e1d49ea900962b63393 Reviewed-on: https://go-review.googlesource.com/c/go/+/272127 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> TryBot-Result: Go Bot <gobot@golang.org>
2020-11-13cmd/cgo: fix initialization of empty argument typesAustin Clements
CL 258938 changed the way C to Go calls work such that they now construct a C struct on the C side for the arguments and space for the results. Any pointers in the result space must be zeroed, so we just zero the whole struct. However, C makes it surprisingly hard to robustly zero any struct type. We had used a "{0}" initializer, which works in the vast majority of cases, but fails if the type is empty or effectively empty. This CL fixes this by changing how the cgo tool zero-initializes the argument struct to be more robust. Fixes #42495. Change-Id: Id1749b9d751e59eb7a02a9d44fec0698a2bf63cd Reviewed-on: https://go-review.googlesource.com/c/go/+/269337 Trust: Austin Clements <austin@google.com> Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-11-12cmd/go, cmd/cgo: don't let bogus symbol set cgo_ldflagIan Lance Taylor
A hand-edited object file can have a symbol name that uses newline and other normally invalid characters. The cgo tool will generate Go files containing symbol names, unquoted. That can permit those symbol names to inject Go code into a cgo-generated file. If that Go code uses the //go:cgo_ldflag pragma, it can cause the C linker to run arbitrary code when building a package. If you build an imported package we permit arbitrary code at run time, but we don't want to permit it at package build time. This CL prevents this in two ways. In cgo, reject invalid symbols that contain non-printable or space characters, or that contain anything that looks like a Go comment. In the go tool, double check all //go:cgo_ldflag directives in generated code, to make sure they follow the existing LDFLAG restrictions. Thanks to Imre Rad / https://www.linkedin.com/in/imre-rad-2358749b for reporting this. Fixes CVE-2020-28367 Change-Id: Ia1ad8f3791ea79612690fa7d26ac451d0f6df7c1 Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/895832 Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Cherry Zhang <cherryyz@google.com> Reviewed-on: https://go-review.googlesource.com/c/go/+/269658 Trust: Katie Hockman <katie@golang.org> Trust: Roland Shoemaker <roland@golang.org> Run-TryBot: Katie Hockman <katie@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Roland Shoemaker <roland@golang.org>
2020-10-31cmd/cgo: add -trimpath flag allowing paths to be rewritten in outputsMichael Matloob
cmd/cgo now has a -trimpath flag that behaves the same as the -trimpath flag to cmd/compile. This will be used to correct paths to cgo files that are overlaid. The code that processes trimpath in internal/objapi has been slightly refactored because it's currently only accessible via AbsFile, which does some additional processing to the path names. Now an ApplyRewrites function is exported that just applies the trimpath rewrites. Also remove unused srcfile argument to cmd/cgo.(*Package).godefs. For #39958 Change-Id: I497d48d0bc2fe1f6ab2b5835cbe79f15b839ee59 Reviewed-on: https://go-review.googlesource.com/c/go/+/266358 Trust: Michael Matloob <matloob@golang.org> Run-TryBot: Michael Matloob <matloob@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2020-10-26runtime,cmd/cgo: simplify C -> Go call pathAustin Clements
This redesigns the way calls work from C to exported Go functions. It removes several steps from the call path, makes cmd/cgo no longer sensitive to the Go calling convention, and eliminates the use of reflectcall from cgo. In order to avoid generating a large amount of FFI glue between the C and Go ABIs, the cgo tool has long depended on generating a C function that marshals the arguments into a struct, and then the actual ABI switch happens in functions with fixed signatures that simply take a pointer to this struct. In a way, this CL simply pushes this idea further. Currently, the cgo tool generates this argument struct in the exact layout of the Go stack frame and depends on reflectcall to unpack it into the appropriate Go call (even though it's actually reflectcall'ing a function generated by cgo). In this CL, we decouple this struct from the Go stack layout. Instead, cgo generates a Go function that takes the struct, unpacks it, and calls the exported function. Since this generated function has a generic signature (like the rest of the call path), we don't need reflectcall and can instead depend on the Go compiler itself to implement the call to the exported Go function. One complication is that syscall.NewCallback on Windows, which converts a Go function into a C function pointer, depends on cgocallback's current dynamic calling approach since the signatures of the callbacks aren't known statically. For this specific case, we continue to depend on reflectcall. Really, the current approach makes some overly simplistic assumptions about translating the C ABI to the Go ABI. Now we're at least in a much better position to do a proper ABI translation. For comparison, the current cgo call path looks like: GoF (generated C function) -> crosscall2 (in cgo/asm_*.s) -> _cgoexp_GoF (generated Go function) -> cgocallback (in asm_*.s) -> cgocallback_gofunc (in asm_*.s) -> cgocallbackg (in cgocall.go) -> cgocallbackg1 (in cgocall.go) -> reflectcall (in asm_*.s) -> _cgoexpwrap_GoF (generated Go function) -> p.GoF Now the call path looks like: GoF (generated C function) -> crosscall2 (in cgo/asm_*.s) -> cgocallback (in asm_*.s) -> cgocallbackg (in cgocall.go) -> cgocallbackg1 (in cgocall.go) -> _cgoexp_GoF (generated Go function) -> p.GoF Notably: 1. We combine _cgoexp_GoF and _cgoexpwrap_GoF and move the combined operation to the end of the sequence. This combined function also handles reflectcall's previous role. 2. We combined cgocallback and cgocallback_gofunc since the only purpose of having both was to convert a raw PC into a Go function value. We instead construct the Go function value in cgocallbackg1. 3. cgocallbackg1 no longer reaches backwards through the stack to get the arguments to cgocallback_gofunc. Instead, we just pass the arguments down. 4. Currently, we need an explicit msanwrite to mark the results struct as written because reflectcall doesn't do this. Now, the results are written by regular Go assignments, so the Go compiler generates the necessary MSAN annotations. This also means we no longer need to track the size of the arguments frame. Updates #40724, since now we don't need to teach cgo about the register ABI or change how it uses reflectcall. Change-Id: I7840489a2597962aeb670e0c1798a16a7359c94f Reviewed-on: https://go-review.googlesource.com/c/go/+/258938 Trust: Austin Clements <austin@google.com> Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>
2020-10-22cmd/cgo: avoid exporting all symbols on windows buildmode=c-sharedQuim Muntal
Disable default symbol auto-export behaviour by marking exported function with the __declspec(dllexport) attribute. Old behaviour can still be used by setting -extldflags=-Wl,--export-all-symbols. See https://sourceware.org/binutils/docs/ld/WIN32.html for more info. This change cuts 50kb of a "hello world" dll. Updates #6853 Fixes #30674 Change-Id: I9c7fb09c677cc760f24d0f7d199740ae73981413 Reviewed-on: https://go-review.googlesource.com/c/go/+/262797 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Alex Brainman <alex.brainman@gmail.com> Trust: Alex Brainman <alex.brainman@gmail.com>
2020-10-08cmd/cgo: add more architectures to size mapsIan Lance Taylor
This brings over the architectures that the gofrontend knows about. This permits using the main cgo tool for those architectures, as cgo can be used with -godefs without gc support. This will help add golang.org/x/sys/unix support for other architectures. For #37443 Change-Id: I63632b9c5139e71b9ccab8edcc7acdb464229b74 Reviewed-on: https://go-review.googlesource.com/c/go/+/260657 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com>
2020-10-05cmd/cgo: split gofrontend mangling checks into cmd/internal/pkgpathIan Lance Taylor
This is a step toward porting https://golang.org/cl/219817 from the gofrontend repo to the main repo. Note that this also corrects the implementation of the v2 mangling scheme to use ..u and ..U where appropriate. For #37272 Change-Id: I64a1e7ca1c84348efcbf1cf62049eeb05c830ed8 Reviewed-on: https://go-review.googlesource.com/c/go/+/259298 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com>
2020-09-23all: add GOOS=iosCherry Zhang
Introduce GOOS=ios for iOS systems. GOOS=ios matches "darwin" build tag, like GOOS=android matches "linux" and GOOS=illumos matches "solaris". Only ios/arm64 is supported (ios/amd64 is not). GOOS=ios and GOOS=darwin remain essentially the same at this point. They will diverge at later time, to differentiate macOS and iOS. Uses of GOOS=="darwin" are changed to (GOOS=="darwin" || GOOS=="ios"), except if it clearly means macOS (e.g. GOOS=="darwin" && GOARCH=="amd64"), it remains GOOS=="darwin". Updates #38485. Change-Id: I4faacdc1008f42434599efb3c3ad90763a83b67c Reviewed-on: https://go-review.googlesource.com/c/go/+/254740 Trust: Cherry Zhang <cherryyz@google.com> Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com>
2020-09-16cmd/cgo: use go:notinheap for anonymous structsKeith Randall
They can't reasonably be allocated on the heap. Not a huge deal, but it has an interesting and useful side effect. After CL 249917, the compiler and runtime treat pointers to go:notinheap types as uintptrs instead of real pointers (no write barrier, not processed during stack scanning, ...). That feature is exactly what we want for cgo to fix #40954. All the cases we have of pointers declared in C, but which might actually be filled with non-pointer data, are of this form (JNI's jobject heirarch, Darwin's CFType heirarchy, ...). Fixes #40954 Change-Id: I44a3b9bc2513d4287107e39d0cbbd0efd46a3aae Reviewed-on: https://go-review.googlesource.com/c/go/+/250940 Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Keith Randall <khr@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-09-16cmd/cgo: don't translate bitfields into Go fieldsIan Lance Taylor
The cgo tool would sometimes emit a bitfield at an offset that did not correspond to the C offset, such as for the example in the new test. Change-Id: I61b2ca10ee44a42f81c13ed12865f2060168fed5 Reviewed-on: https://go-review.googlesource.com/c/go/+/252378 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org>
2020-09-01cmd/cgo: document #include <> search path behaviourKJ Tsanaktsidis
cgo effectively prepends -I${SRCDIR} to the header include path of all preambles it processes, so when an #include <> matches a header file both in the source directory and also another include directory, the local copy will be used in preference. This behaviour is surprising but unfortunately also longstanding and relied upon by packages in the wild, so the best we can do is to document it. Fixes #41059 Change-Id: If6d2818294b2bd94ea0fe5fd6ce77e54b3e167a6 Reviewed-on: https://go-review.googlesource.com/c/go/+/251758 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-08-27cmd/cgo: ensure GCC does not use ANSI escape sequences in errorsBenjamin Barenblat
cgo parses GCC’s error messages to classify C identifiers referenced from Go programs (are they integer constants? type names?). If GCC tries to colorize its errors, cgo can’t figure out what GCC is saying. GCC avoids escape sequences in this scenario by default, but the default behavior can be overridden in at least two places: - The user can set `CGO_COPTS=-fdiagnostics-color`. - Whoever compiled GCC can configure GCC itself to always colorize output. The most reliable way to ensure that GCC doesn’t colorize output is to append `-fdiagnostics-color=never` to the GCC command line; do so. Fixes #40415 Change-Id: Id4bdf8d92fac8b038340b4264f726e8fe38875b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/248398 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-08-18cmd/cgo: close filealex-semenyuk
Change-Id: Ia70edc8ba22e31e498fe07946db41882804bd39f GitHub-Last-Rev: 280232e879965b34fae3aa02e6215eff5bd7be46 GitHub-Pull-Request: golang/go#39120 Reviewed-on: https://go-review.googlesource.com/c/go/+/234317 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-07-31cmd/cgo: fix mangling of enum and union typesMatthew Dempsky
Consider this test package: package p // enum E { E0 }; // union U { long x; }; // void f(enum E e, union U* up) {} import "C" func f() { C.f(C.enum_E(C.E0), (*C.union_U)(nil)) } In Go 1.14, cgo translated this to (omitting irrelevant details): type _Ctype_union_U [8]byte func f() { _Cfunc_f(uint32(_Ciconst_E0), (*[8]byte)(nil)) } func _Cfunc_f(p0 uint32, p1 *[8]byte) (r1 _Ctype_void) { ... } Notably, _Ctype_union_U was declared as a defined type, but uses were being rewritten into uses of the underlying type, which matched how _Cfunc_f was declared. After CL 230037, cgo started consistently rewriting "C.foo" type expressions as "_Ctype_foo", which caused it to start emitting: type _Ctype_enum_E uint32 type _Ctype_union_U [8]byte func f() { _Cfunc_f(_Ctype_enum_E(_Ciconst_E0), (*_Ctype_union_U)(nil)) } // _Cfunc_f unchanged Of course, this fails to type-check because _Ctype_enum_E and _Ctype_union_U are defined types. This CL changes cgo to emit: type _Ctype_enum_E = uint32 type _Ctype_union_U = [8]byte // f unchanged since CL 230037 // _Cfunc_f still unchanged It would probably be better to fix this in (*typeConv).loadType so that cgo generated code uses the _Ctype_foo aliases too. But as it wouldn't have any effect on actual compilation, it's not worth the risk of touching it at this point in the release cycle. Updates #39537. Fixes #40494. Change-Id: I88269660b40aeda80a9a9433777601a781b48ac0 Reviewed-on: https://go-review.googlesource.com/c/go/+/246057 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-06-26cmd/cgo: prevent redeclaration of _Ctype_void when C.void is usedMatthew Dempsky
CL 230037 changed cmd/cgo to emit "type _Ctype_foo = bar" aliases for all C.foo types mentioned in the original Go source files. However, cmd/cgo already emits an appropriate type definition for _Ctype_void. So if a source file explicitly mentions C.void, this resulted in _Ctype_void being declared multiple times. This CL fixes the issue by suppressing the "type _Ctype_void = _Ctype_void" alias before printing it. This should be safe because _Ctype_void is the only type that's specially emitted in out.go at the moment. A somewhat better fix might be to fix how _Ctype_void is declared in the cmd/cgo "frontend", but this is a less invasive fix. Fixes #39877. Change-Id: Ief264b3847c8ef8df1478a6333647ff2cf09b63d Reviewed-on: https://go-review.googlesource.com/c/go/+/240180 Run-TryBot: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2020-06-11cmd/cgo: in -godefs mode, don't change constant to typeIan Lance Taylor
Fixes #39534 Change-Id: Icbc1745935dd7098c09e2d35c61cd5bfbaa31c63 Reviewed-on: https://go-review.googlesource.com/c/go/+/237558 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2020-06-08all: replace usages of whitelist/blacklist and master/slaveFilippo Valsorda
There's been plenty of discussion on the usage of these terms in tech. I'm not trying to have yet another debate. It's clear that there are people who are hurt by them and who are made to feel unwelcome by their use due not to technical reasons but to their historical and social context. That's simply enough reason to replace them. Anyway, allowlist and blocklist are more self-explanatory than whitelist and blacklist, so this change has negative cost. Didn't change vendored, bundled, and minified files. Nearly all changes are tests or comments, with a couple renames in cmd/link and cmd/oldlink which are extremely safe. This should be fine to land during the freeze without even asking for an exception. Change-Id: I8fc54a3c8f9cc1973b710bbb9558a9e45810b896 Reviewed-on: https://go-review.googlesource.com/c/go/+/236857 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Khosrow Moossavi <khos2ow@gmail.com> Reviewed-by: Leigh McCulloch <leighmcc@gmail.com> Reviewed-by: Urban Ishimwe <urbainishimwe@gmail.com>
2020-05-31cmd/cgo,cmd/fix,misc/cgo: map the EGLConfig C type to uintptr in GoElias Naur
Similarly to EGLDisplay, EGLConfig is declared as a pointer but may contain non-pointer values. I believe this is the root cause of https://todo.sr.ht/~eliasnaur/gio/121. Change-Id: I412c4fbc2eef4aa028534d68bda95db98e3a365d Reviewed-on: https://go-review.googlesource.com/c/go/+/235817 Run-TryBot: Elias Naur <mail@eliasnaur.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-05-01cmd/cgo: "not-type" means "is not a type", not "is a type"Matthew Dempsky
Change-Id: I87111f20a55a0b202b95a533ffb7e3aa666715dc Reviewed-on: https://go-review.googlesource.com/c/go/+/231598 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2020-05-01cmd/cgo: use type aliases for #define type macrosMatthew Dempsky
Cgo's initial design for handling "#define foo int*" involved rewriting "C.foo" to "*_Ctype_int" everywhere. But now that we have type aliases, we can declare "type _Ctype_foo = *_Ctype_int" once, and then rewrite "C.foo" to just "_Ctype_foo". This is important for go/types's UsesCgo mode, where go/types needs to be able to figure out a type for each C.foo identifier using only the information written into _cgo_gotypes.go. Fixes #38649. Change-Id: Ia0f8c2d82df81efb1be5bc26195ea9154c0af871 Reviewed-on: https://go-review.googlesource.com/c/go/+/230037 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-04-14cmd/cgo: use consistent tag for a particular structIan Lance Taylor
For #31891 Fixes #38408 Change-Id: Ie7498c2cab728ae798e66e7168425e16b063520e Reviewed-on: https://go-review.googlesource.com/c/go/+/228102 Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
2020-04-13cmd/cgo: fix parameter name in godoc comment for badPointerTypedefTobias Klauser
The parameter name is dt, not t. Also, line-wrap the godoc comment. Change-Id: Ie012d2a5680525b88e244a3380d72bc4f61da8e7 Reviewed-on: https://go-review.googlesource.com/c/go/+/228058 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matt Layher <mdlayher@gmail.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-03-30cmd/cgo, misc/cgo: only cache anonymous struct typedefs with parent nameTobias Klauser
CL 181857 broke the translation of certain C types using cmd/cgo -godefs because it stores each typedef, array and qualified type with their parent type name in the translation cache. Fix this by only considering the parent type for typedefs of anonymous structs which is the only case where types might become ambiguous. Updates #31891 Fixes #37479 Fixes #37621 Change-Id: I301a749ec89585789cb0d213593bb8b7341beb88 Reviewed-on: https://go-review.googlesource.com/c/go/+/226341 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-03-10cmd/cgo: always produce a parameter name for C codeIan Lance Taylor
Updates #37746 Change-Id: Ib64abe3995f310cd50ede47b0d3d159572901000 Reviewed-on: https://go-review.googlesource.com/c/go/+/222622 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2020-03-10cmd/cgo: updated exported function parameter namesNathan Fiscaletti
Functions that are exported through cgo will now retain their parameter names provided that their parameter names use only ASCII characters. Fixes #37746 Change-Id: Ia5f643e7d872312e81c224febd1f81ce14425c32 GitHub-Last-Rev: 220959bebae7d654d96530498f40e3a2abe95080 GitHub-Pull-Request: golang/go#37750 Reviewed-on: https://go-review.googlesource.com/c/go/+/222619 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-03-07cmd/cgo: avoid extra newline in doc comment of exported functionNathan Fiscaletti
Fixes #37722 Change-Id: Ie9154f9d91824558a8ac34ad2fb5bc4e02c64a4b GitHub-Last-Rev: 9fbe1c1d802287ff3c3ccee62e4e74d802196ffb GitHub-Pull-Request: golang/go#37723 Reviewed-on: https://go-review.googlesource.com/c/go/+/222419 Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-11-19cmd/cgo: better handling for '.' in pkgpath for gccgoThan McIntosh
Update gccgoPkgpathToSymbolNew() to bring it into conformance with the way that gccgo now handles packagepaths with embedded dots (see CL 200838). See also https://gcc.gnu.org/PR61880, a related bug. Updates #35623. Change-Id: I32f064320b9af387fc17771530c745a9e3003c20 Reviewed-on: https://go-review.googlesource.com/c/go/+/207957 Run-TryBot: Than McIntosh <thanm@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-10-20go/build: recognize '.sx' as equivalent of '.S'Umang Parmar
On case insensitive filesystems, '.S' is interpreted as '.s' so, providing option to use '.sx' extension for '.S' files as an alternative. Fixes #32434 Change-Id: Ie2f7e5e2f3f12690ce18659e30ca94252a8f7bfc GitHub-Last-Rev: dcca989ec41ddc1d06ea509b78dce7d70bc996ed GitHub-Pull-Request: golang/go#32557 Reviewed-on: https://go-review.googlesource.com/c/go/+/181699 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-10-20cmd/go: allow passing -U flag in cgo CFLAGSUmang Parmar
Fixes #34730 Change-Id: I2a16bbbd7af386e0abf59e1cc4b7d6e4ca3047c6 GitHub-Last-Rev: 096ff5a59ace36ac6ed5a0c87b4eef061be6b937 GitHub-Pull-Request: golang/go#34899 Reviewed-on: https://go-review.googlesource.com/c/go/+/201039 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-10-05cmd/cgo: build unique C type cache keys from parent namesTai
When translating C types, cache the in-progress type under its parent names, so that anonymous structs can also be translated for multiple typedefs, without clashing. Standalone types are not affected by this change. Also updated the test for issue 9026 because the C struct name generation algorithm has changed. Fixes #31891 Change-Id: I00cc64852a2617ce33da13f74caec886af05b9f2 Reviewed-on: https://go-review.googlesource.com/c/go/+/181857 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-10-02cmd/cgo: optimize cgoCheckPointer callEgon Elbre
Currently cgoCheckPointer is only used with one optional argument. Using a slice for the optional arguments is quite expensive, hence replace it with a single interface{}. This results in ~30% improvement. When checking struct fields, they quite often end up being without pointers. Check this before calling cgoCheckPointer, which results in additional ~20% improvement. Inline some p == nil checks from cgoIsGoPointer which gives additional ~15% improvement. All of this translates to: name old time/op new time/op delta CgoCall/add-int-32 46.9ns ± 1% 46.6ns ± 1% -0.75% (p=0.000 n=18+20) CgoCall/one-pointer-32 143ns ± 1% 87ns ± 1% -38.96% (p=0.000 n=20+20) CgoCall/eight-pointers-32 767ns ± 0% 327ns ± 1% -57.30% (p=0.000 n=18+16) CgoCall/eight-pointers-nil-32 110ns ± 1% 89ns ± 2% -19.10% (p=0.000 n=19+19) CgoCall/eight-pointers-array-32 5.09µs ± 1% 3.56µs ± 2% -30.09% (p=0.000 n=19+19) CgoCall/eight-pointers-slice-32 3.92µs ± 0% 2.57µs ± 2% -34.48% (p=0.000 n=20+20) Change-Id: I2aa9f5ae8962a9a41a7fb1db0c300893109d0d75 Reviewed-on: https://go-review.googlesource.com/c/go/+/198081 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-07-12cmd/cgo: do not rewrite call if there are more args than parametersIan Lance Taylor
We already skipped rewriting the call if there were fewer args than parameters. But we can also get a cgo crash if there are more args, if at least one of the extra args uses a name qualified with "C.". Skip the rewrite, since the build will fail later anyhow. Fixes #33061 Change-Id: I62ff3518b775b502ad10c2bacf9102db4c9a531c Reviewed-on: https://go-review.googlesource.com/c/go/+/185797 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Bonventre <andybons@golang.org>
2019-07-09cmd/cgo: fix check for conversion of ptr to struct fieldGernot Vormayr
According to the documentation "When passing a pointer to a field in a struct, the Go memory in question is the memory occupied by the field, not the entire struct.". checkAddr states that this should also work with type conversions, which is implemented in isType. However, ast.StarExpr must be enclosed in ast.ParenExpr according to the go spec (see example below), which is not considered in the checks. Example: // struct Si { int i; int *p; }; void f(struct I *x) {} import "C" type S { p *int i C.struct_Si } func main() { v := &S{new(int)} C.f((*C.struct_I)(&v.i)) // <- panic } This example will cause cgo to emit a cgoCheck that checks the whole struct S instead of just S.i causing the panic "cgo argument has Go pointer to Go pointer". This patch fixes this situation by adding support for ast.ParenExpr to isType and adds a test, that fails without the fix. Fixes #32970. Change-Id: I15ea28c98f839e9fa708859ed107a2e5f1483133 Reviewed-on: https://go-review.googlesource.com/c/go/+/185098 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>