From 4f6bb06cf1a0ea891320ebb0f492ae50e32ec739 Mon Sep 17 00:00:00 2001 From: David Benoit Date: Mon, 15 Feb 2021 19:46:53 -0500 Subject: [PATCH] cmd/cgo: use -fno-lto in cflags when compiling _cgo_export.c Currently, cgo declares different types for exported symbols between _cgo_main.c and _cgo_export.c. This is not compatible with link time optimization in GCC and causes builds to fail when compiling with CGO_CFLAGS="-flto -ffat-lto-objects". This commit appends -fno-lto to the cflags when building _cgo_export.c to disable link time optimization locally on the built object. Fixes #43830 --- src/cmd/go/internal/work/exec.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go index 422e83c224f84c..7e21c41d66c39d 100644 --- a/src/cmd/go/internal/work/exec.go +++ b/src/cmd/go/internal/work/exec.go @@ -2776,9 +2776,14 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo // gcc cflags := str.StringList(cgoCPPFLAGS, cgoCFLAGS) + exportCflags := str.StringList(cflags, "-fno-lto") for _, cfile := range cfiles { ofile := nextOfile() - if err := b.gcc(a, p, a.Objdir, ofile, cflags, objdir+cfile); err != nil { + currentCflags := cflags + if cfile == "_cgo_export.c" { + currentCflags = exportCflags + } + if err := b.gcc(a, p, a.Objdir, ofile, currentCflags, objdir+cfile); err != nil { return nil, nil, err } outObj = append(outObj, ofile)