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/link: failed to set variable at build time if variable value is result from method call #26042

Closed
cuonglm opened this issue Jun 25, 2018 · 5 comments
Labels
Documentation FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@cuonglm
Copy link
Member

cuonglm commented Jun 25, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version go1.10.3 linux/amd64

Does this issue reproduce with the latest release?

yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/cuonglm/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/cuonglm/go"
GORACE=""
GOROOT="/home/cuonglm/sources/go"
GOTMPDIR=""
GOTOOLDIR="/home/cuonglm/sources/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
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"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build755255777=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/M9xGmognyrl

run as:

go run -ldflags "-X main.X=y -X main.MyX=myY" main.go

What did you expect to see?

y
myY

What did you see instead?

y
myX
@ianlancetaylor
Copy link
Contributor

I think that fixing this would require a completely different and less efficient approach to the implementation of the linker's -X option. I'm inclined to just make this a documented limitation.

@ianlancetaylor ianlancetaylor added Documentation help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Jun 25, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.12 milestone Jun 25, 2018
@cuonglm
Copy link
Member Author

cuonglm commented Jun 25, 2018

@ianlancetaylor Can you please explain more details about current implementation? Or just give me link to linker code which cause this problem.

@ianlancetaylor
Copy link
Contributor

Every global variable has memory allocated in the data section of the program. The contents of the data section are set to the initial value of the variable. For a variable initialized with a function call, the variable's value is set by an initialization function. The -X option simply changes the initial value stored in the data section, but does not affect the initialization function.

The implementation of -X is the function addstrdata in cmd/link/internal/ld/data.go, which sets the global variable strdata, and then dostrdata which calls addstrdata for each element of strdata.

@cuonglm
Copy link
Member Author

cuonglm commented Jun 25, 2018

@ianlancetaylor Thanks. I went through those codes already but don't fully understand them.

/me will send the CL this morning

@gopherbot
Copy link

Change https://golang.org/cl/120935 mentions this issue: cmd/link: document limitation of -X

@golang golang locked and limited conversation to collaborators Jun 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants