1

Hello everyone, I am fried fish.

I have a friend, happy to join the job, thinking about doing his best, the first task is to do research on old binaries.

When he saw this file, he didn't know what parameters the compiler used to type it, what the environment was, and what code branch it came from?

In addition to the problem of project flow, Go also has similar small problems in this area, which are more troublesome to deal with.

background

Retrieving meta information from Go binaries is difficult on a day-to-day basis, either the information is completely missing or extraction requires extensive parsing of the binary.

The included meta information is as follows:

meta informationExtraction
Go buildSymbol table, obtained through global variable runtime.buildVersion
Build information such as: modules and versionsSymbol table, obtained through the global variable runtime/debug.modinfo
Compiler options such as: build mode, compiler, gcflags, ldflags, etc.not available
User-defined custom data, such as: application version, etc.You need to set the global string variable at compile time before you can get it

Paying attention to the compiler options, that is, parameters, etc., are not known, that is, it will increase the difficulty of obtaining how to compile them.

new proposal

Michael Obermüller proposed a new proposal " cmd/go: add compiler flags, relevant env vars to 'go version -m' output " to solve the above problem.

What is wanted in the proposal is a structured output in JSON format:

{
    "version": "go1.13.4",
    "compileropts": {
        "compiler": "gc",
        "mode": "pie",
        "os": "linux",
        ...
    },
    "buildinfo": {
        "path": "脑子进煎鱼了",
        "main": {
            "path": "HelloWorld",
            "version": "(devel)",
        },
        "deps": []
    },
    "user": {
        "customkey": "customval",
        ...
    }
}

Russ Cox said that since the compilation information is already in a pre-existing format, using JSON by default would only make the binary larger. The benefits are few, unnecessary, and changed to optional support.

In the new Go1.18 version, you can pass the existing:

go version -m

View the information mentioned in the proposal.

For example:

$ gotip version
go version devel go1.18-eba0e866fa Mon Oct 18 22:56:07 2021 +0000 darwin/amd64
$ gotip build ./
$ gotip version -m ko
...
    build    compiler    gc
    build    tags    goexperiment.regabiwrappers,goexperiment.regabireflect,goexperiment.regabiargs
    build    CGO_ENABLED    true
    build    CGO_CPPFLAGS    
    build    CGO_CFLAGS    
    build    CGO_CXXFLAGS    
    build    CGO_LDFLAGS    
    build    gitrevision    6447264ff8b5d48aff64000f81bb0847aefc7bac
    build    gituncommitted    true

If you need to output JSON format, you can also specify go version -json to achieve the same effect.

In the above output, the existing compiler options, etc. will be included, so that you can have a better understanding of the overall compiled binary file source traceability.

Summarize

In today's article, we introduce a new change in Go1.18.

In the new version, compiler options/parameters, related environment variables, etc. will be included in the compiled binary file, which can make it easier for future generations to check and view information.

If you have any questions, please feedback and exchange in the comment area. The best relationship between . Your likes is Fried Fish . Thank you for your support.

The article is continuously updated, you can read it on search [brain fried fish], this article 161ee406f1f724 GitHub github.com/eddycjy/blog has been included, learn Go language can see Go learning map and route

煎鱼
8.4k 声望12.8k 粉丝