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: add 'go version <binary>' #31624

Closed
rsc opened this issue Apr 23, 2019 · 7 comments
Closed

cmd/go: add 'go version <binary>' #31624

rsc opened this issue Apr 23, 2019 · 7 comments
Labels
FrozenDueToAge modules NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented Apr 23, 2019

Many people find rsc.io/goversion useful.
It would be better as 'go version' instead of having
to download a separate tool.

@rsc rsc added this to the Go1.13 milestone Apr 23, 2019
@rsc rsc self-assigned this Apr 23, 2019
@gopherbot
Copy link

Change https://golang.org/cl/173343 mentions this issue: cmd/go: make 'go version' apply to files

@gopherbot
Copy link

Change https://golang.org/cl/173342 mentions this issue: cmd/link: make it easy to find binary versions

@gopherbot
Copy link

Change https://golang.org/cl/173341 mentions this issue: cmd/go: move runtime/debug.modinfo to runtime.modinfo

@bradfitz bradfitz added the NeedsFix The path to resolution is known, but the work has not been done. label Apr 23, 2019
gopherbot pushed a commit that referenced this issue Apr 23, 2019
It is easier to ensure that the symbol is always present
if we move it to package runtime. Avoids init-time work.
Also moves it next to buildVersion, the other similar symbol.

Setting up for "go version <binary>".

For #31624.

Change-Id: I943724469ce6992153e701257eb6f12da88c8e4e
Reviewed-on: https://go-review.googlesource.com/c/go/+/173341
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
gopherbot pushed a commit that referenced this issue Apr 26, 2019
It is useful to be able to dig the Go version out of a binary,
even a stripped binary. rsc.io/goversion does this for x86
binaries by disassembling the binary to find runtime/proc.go's
startup-time reference to runtime.buildVersion's address.
That approach is quite fragile: the implementation doesn't
work for non-x86 and must be updated as the generated
code changes.

rsc.io/goversion finds the module version string by looking
for random 16-byte framing around the actual string.
This is less fragile but fairly kludgy and requires scanning
the entire data segment.

cmd/buildid finds the build ID by looking for an ELF note
or else falling back to scanning the beginning of the text
segment for a magic string. This has proved quite reliable
and doesn't require scanning much of the binary.

This CL makes it possible to find the Go and module versions
using a scan more like the build ID scan: a symbol early in
the writable data segment starts with a magic number and
then has pointers to the two string variables.

Setting up for "go version <binary>".

For #31624.

Change-Id: I78ea8c52fe1686b5cc5a829ca5f198104d10ebf0
Reviewed-on: https://go-review.googlesource.com/c/go/+/173342
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
@gopherbot
Copy link

Change https://golang.org/cl/175449 mentions this issue: cmd/go: don't print phdrs running "go version" on ELF files

gopherbot pushed a commit that referenced this issue May 7, 2019
I assume this was for debugging purposes.

Updates #31624

Change-Id: Ie158fde0574c9bbbd9d1b684f51af5681974aff7
Reviewed-on: https://go-review.googlesource.com/c/go/+/175449
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
@mrueg
Copy link

mrueg commented Jan 19, 2020

@rsc since this is now part of cmd/go/internal and you mentioned github.com/rsc/goversion is going to be deprecated, are there plans to expose the functions for a use in other tools (e.g. https://github.com/mitchellh/golicense/)?

@mvdan
Copy link
Member

mvdan commented Jan 27, 2020

@mrueg note that the API to query Go packages and modules is go list, not a Go package. The reason is so that the underlying implementation can change, and one doesn't need to rebuild all tools anytime the Go version changes.

For example, https://godoc.org/golang.org/x/tools/go/packages is a Go API that simply execs go list when working with GOPATH or modules.

Following the same idea, I think you should be calling go version if you want the feature from Go code. The underlying mechanism is not defined in a stable format that you could implement in a forever-compatible Go package.

@mrueg
Copy link

mrueg commented Jan 27, 2020

@mvdan Thanks for your response. I'm currently implementing a tool that will reconstruct a go.mod file from a given non-stripped go binary, so rsc/goversion works for that case and allows me to extract necessary details.
Parsing the output of go version -m $binary is an option as well, but it would be easier to use its internally existing data structure using a function that returns it.

I assume if I want to do and use golang's implementation my only option is extract the package into my repository?

@golang golang locked and limited conversation to collaborators Jan 26, 2021
@rsc rsc removed their assignment Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge modules NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants