golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.12k stars 17.68k forks source link

cmd/link: -ldflags -X option does not work with -linkshared #16671

Closed laboger closed 8 years ago

laboger commented 8 years ago

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)? go version devel +748aa84 Wed Aug 10 20:33:11 2016 +0000 linux/ppc64le
  2. What operating system and processor architecture are you using (go env)? ppc64le Ubuntu 16.04
  3. What did you do? If possible, provide a recipe for reproducing the error. A complete runnable program is good. A link on play.golang.org is best. Simple testcase: test-setX.go: package main

import ( "fmt" )

var VERSION string

func main() { fmt.Printf("Hello, World! Version is: %s\n", VERSION) } Make sure there is a shared std libary go install -buildmode=shared std go build -linkshared -ldflags '-X main.VERSION=123' test-setX.go

  1. What did you expect to see? Successful build and execution. Output without -linkshared is: ./test-setX Hello, World! Version is: 123
  2. What did you see instead? Program didn't build, error message was: cannot use -X with non-string symbol main.VERSION type.TwVfYA92 (I modified src/cmd/link/internal/ld/data.go to print out the actual type at the end of the message. That does not appear by default.) I also just tried removing the test in data.go where this error message occurs, and then the program built successfully but panicked at runtime.
mwhudson commented 8 years ago

Pretty sure that type.TwVfYA92 is actually type.string, type symbols get their names compressed (in general, it's actually getting expanded here). A fix would be to not mangle type names when the mangling would in fact make the names longer (@crawshaw?)

gopherbot commented 8 years ago

CL https://golang.org/cl/26890 mentions this issue.