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

Comments

Projects
None yet
3 participants
@cuonglm
Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

commented Jun 25, 2018

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.

@cuonglm

This comment has been minimized.

Copy link
Contributor Author

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

This comment has been minimized.

Copy link
Contributor

commented Jun 25, 2018

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

This comment has been minimized.

Copy link
Contributor Author

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

This comment has been minimized.

Copy link

commented Jun 26, 2018

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

@gopherbot gopherbot closed this in 6dd789f Jun 27, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.