Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: cgo compilation fails when LDFLAGS refers to a response file #22555

Closed
cstrahan opened this issue Nov 3, 2017 · 2 comments
Closed

cmd/go: cgo compilation fails when LDFLAGS refers to a response file #22555

cstrahan opened this issue Nov 3, 2017 · 2 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@cstrahan
Copy link

cstrahan commented Nov 3, 2017

What version of Go are you using (go version)?

go version go1.9.2 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/cstrahan/go"
GORACE=""
GOROOT="/nix/store/7q4n0qqrqj4wsl41v8vq3d2zrwwgwpvk-go-1.9.2/share/go"
GOTOOLDIR="/nix/store/7q4n0qqrqj4wsl41v8vq3d2zrwwgwpvk-go-1.9.2/share/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/run/user/2000/go-build236342296=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

You can reproduce by compiling the following files like so:

gcc -shared -o librepro.so repro.c
go build -o repro main.go

repro.c

int reproduce_me() {
  return 42;
}

repro.params

-L.
-lrepro

main.go

package main

// #cgo LDFLAGS: -fuse-ld=gold -Wl,@repro.params
// extern int reproduce_me();
import "C"
import "fmt"

func main() {
	x := C.reproduce_me()
	fmt.Printf("Hey look, it worked!  --> %d\n", x)
}

What did you expect to see?

I expected the program to compile successfully.

What did you see instead?

If the response file includes any shared libraries, you'll see errors like:

ld.gold: fatal error: cannot mix -r with dynamic object ./bazel-out/local-fastbuild/bin/_solib_k8/_U@nix_Uenvoy_Udeps_S_S_Cares___Uexternal_Snix_Uenvoy_Udeps_Slib/libcares.so
collect2: error: ld returned 1 exit status

Otherwise you'll see "multiple definition" errors like:

ld.gold: error: bazel-out/local-fastbuild/bin/external/envoy/source/common/upstream/_objs/outlier_detection_lib/external/envoy/source/common/upstream/outlier_detection_impl.o: multiple definition of 'Envoy::Upstream::Outlier::DetectorImpl::enforceEjection(Envoy::Upstream::Outlier::EjectionType)'
ld.gold: /run/user/2000/go-link-231334173/000000.o: previous definition here
@cstrahan
Copy link
Author

cstrahan commented Nov 3, 2017

It's possible that someone using a response file is doing so because they need to avoid some sort of limitation on e.g. argv (as often seen in on Windows). So with that in mind, I would propose the following:

  • Read the content of response files given by LDFLAGS
  • When necessary, construct a response file consisting of the appropriate flags within the union of those supplied directly and those given by the response file.

That way we'd fix this particular error, and we'd fix #18468 at the same time.


If you're curious, I ran into this problem while playing with Bazel, where I was given a response file in my build. In-lining the response file into LDFLAGS resulted in a successful compilation.

@ianlancetaylor ianlancetaylor changed the title cgo compilation fails when LDFLAGS refers to a response file cmd/go: cgo compilation fails when LDFLAGS refers to a response file Nov 3, 2017
@ianlancetaylor ianlancetaylor added help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Nov 3, 2017
@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Nov 3, 2017
@rsc
Copy link
Contributor

rsc commented Apr 18, 2018

Response files are now completely banned, so the fact that they wouldn't work if not banned is no longer an issue.

@rsc rsc closed this as completed Apr 18, 2018
@rsc rsc added help and removed help wanted labels Apr 18, 2018
@golang golang locked and limited conversation to collaborators Apr 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants