Skip to content

Commit f3b2c46

Browse files
committed
cmd/cgo: Fix CGO related LTO bug
This patch fixes another CGO related LTO bug. This seems to come when the programmer tries to assign a C function pointer to a local Go variable as seen in the reproducer. It turns out we do not need to emit _cgo_hack or our own definition of the symbol(s) within the _cgo_main.c file as was previously done. Fixes #43830
1 parent fce06f5 commit f3b2c46

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/cmd/cgo/out.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ func (p *Package) writeDefs() {
168168
if *gccgo {
169169
fmt.Fprintf(fc, "extern byte *%s;\n", n.C)
170170
} else {
171-
fmt.Fprintf(fm, "extern char %s[];\n", n.C)
172-
fmt.Fprintf(fm, "void *_cgohack_%s = %s;\n\n", n.C, n.C)
171+
if n.Kind != "fpvar" {
172+
fmt.Fprintf(fm, "extern char %s[];\n", n.C)
173+
fmt.Fprintf(fm, "void *_cgohack_%s = %s;\n\n", n.C, n.C)
174+
}
173175
fmt.Fprintf(fgo2, "//go:linkname __cgo_%s %s\n", n.C, n.C)
174176
fmt.Fprintf(fgo2, "//go:cgo_import_static %s\n", n.C)
175177
fmt.Fprintf(fgo2, "var __cgo_%s byte\n", n.C)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# tests golang.org/issue/43830
2+
3+
[!cgo] skip
4+
5+
env CGO_CFLAGS='-flto -ffat-lto-objects'
6+
go build main.go
7+
8+
-- main.go --
9+
10+
package main
11+
12+
import "fmt"
13+
14+
// #include "hello.h"
15+
import "C"
16+
17+
func main() {
18+
hello := C.hello
19+
fmt.Printf("%v\n", hello)
20+
}
21+
22+
-- hello.h --
23+
24+
#include <stdio.h>
25+
26+
void hello(void) {
27+
printf("hello\n");
28+
}

0 commit comments

Comments
 (0)