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: go build should error if ldflags -X pointed at const #20649

Closed
icio opened this issue Jun 12, 2017 · 3 comments
Closed

cmd/go: go build should error if ldflags -X pointed at const #20649

icio opened this issue Jun 12, 2017 · 3 comments

Comments

@icio
Copy link

icio commented Jun 12, 2017

Please answer these questions before submitting your issue. Thanks!

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

go version go1.8.3 darwin/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/icio/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/87/jm5nnqqs2_xcxr3bfpkg73tw0000gn/T/go-build323188801=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

Created a file main.go:

package main

var Forename = "Paul"
const Surname = "Scott"

func main() {
	println("Hello, " + Forename + " " + Surname)
}

And ran the following command:

go run -ldflags '-X main.Forename=Bob -X main.Surname=Smith' main.go

What did you expect to see?

An error that main.Surname is const and could not be overridden, or

Hello, Bob Smith

What did you see instead?

Hello, Bob Scott
@icio icio changed the title cmd/go: go build should error if -X pointed at const cmd/go: go build should error if ldflags -X pointed at const Jun 12, 2017
@ianlancetaylor
Copy link
Contributor

I'm fine with the idea in principle but I don't see how it is possible in practice. We decided a long time ago that the linker should accept a -X option for an unknown symbol, so that people could use a single set of -X options to build multiple programs. In your test Surname is a constant, so the linker never sees it. The effect is that the linker thinks you are using -X with an unknown symbol, so it doesn't do anything.

I'll leave this open for now in case someone else thinks of a way to implement it.

@icio
Copy link
Author

icio commented Jun 12, 2017

That's reasonable. I wondered whether this was a result of the const disappearing before the -X was considered, but I'm not familiar with the full steps involved. Thanks for looking.

@rsc
Copy link
Contributor

rsc commented Jun 12, 2017

I can't think of a way to do it, and worse I can think of migration paths that it might break (if we think it's important to support -X in the transition from builds with no var that add a var, it seems reasonable to think it's also important to support -X in the transition from builds with a const to builds with a var).

@rsc rsc closed this as completed Oct 25, 2017
@golang golang locked and limited conversation to collaborators Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants