Skip to content

Commit

Permalink
cmd/go: report trimpath erasing ldflags, and allow override
Browse files Browse the repository at this point in the history
Add a new boolean option -trimldflags. Only meaningful when -trimpath
is true. Defaults to true for backwards compatibility. Otheriwise when
set to false reports ldflags in buildinfo, in spite of -trimpath
setting. Also when ldflags are trimmed from the output, leave a
reproducible marker that it happened.

Building with '-trimpath -ldflags="-X main.Version=234"' will now emit:
	build	-trimldflags=true

Adding -trimldflags=false to the above will emit ldflags:
	build	-ldflags="-X main.Version=234"

Fixes: #63432
  • Loading branch information
xnox committed Apr 26, 2024
1 parent 5419f65 commit 71d1aa8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/cmd/go/alldocs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/cmd/go/internal/cfg/cfg.go
Expand Up @@ -86,6 +86,7 @@ var (
BuildToolexec []string // -toolexec flag
BuildToolchainName string
BuildTrimpath bool // -trimpath flag
BuildTrimldflags bool // -trimldflags flag
BuildV bool // -v flag
BuildWork bool // -work flag
BuildX bool // -x flag
Expand Down
9 changes: 8 additions & 1 deletion src/cmd/go/internal/load/pkg.go
Expand Up @@ -2395,7 +2395,14 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) {
// determine whether they may refer to system paths. If we do that, we can
// redact only those paths from the recorded -ldflags setting and still
// record the system-independent parts of the flags.
if !cfg.BuildTrimpath {
//
// For now add a toggle to always allow ldflags reporting, it may make
// non-reproducible builds, but it will stop hiding valuable version
// information as used by security vulnerability scanners. Although maybe
// vcs.describe or vcs.modhash should be added instead.
if cfg.BuildTrimpath && cfg.BuildTrimldflags {
appendSetting("-trimldflags", "true")
} else {
appendSetting("-ldflags", ldflags)
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/go/internal/work/build.go
Expand Up @@ -192,6 +192,9 @@ and test commands:
Instead of absolute file system paths, the recorded file names
will begin either a module path@version (when using modules),
or a plain import path (when using the standard library, or GOPATH).
-trimldflags
Only meaningful with -trimpath true. Controls reporting of ldflags in binary
module information. May affect reproducible builds.
-toolexec 'cmd args'
a program to use to invoke toolchain programs like vet and asm.
For example, instead of running asm, the go command will run
Expand Down Expand Up @@ -338,6 +341,7 @@ func AddBuildFlags(cmd *base.Command, mask BuildFlagMask) {
cmd.Flag.Var((*tagsFlag)(&cfg.BuildContext.BuildTags), "tags", "")
cmd.Flag.Var((*base.StringsFlag)(&cfg.BuildToolexec), "toolexec", "")
cmd.Flag.BoolVar(&cfg.BuildTrimpath, "trimpath", false, "")
cmd.Flag.BoolVar(&cfg.BuildTrimldflags, "trimldflags", true, "")
cmd.Flag.BoolVar(&cfg.BuildWork, "work", false, "")
cmd.Flag.Var((*buildvcsFlag)(&cfg.BuildBuildvcs), "buildvcs", "")

Expand Down
6 changes: 6 additions & 0 deletions src/cmd/go/internal/work/exec.go
Expand Up @@ -283,6 +283,9 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix)
if cfg.BuildTrimpath {
fmt.Fprintln(h, "trimpath")
if cfg.BuildTrimldflags {
fmt.Fprintln(h, "trimldflags")
}
}
if p.Internal.ForceLibrary {
fmt.Fprintf(h, "forcelibrary\n")
Expand Down Expand Up @@ -1368,6 +1371,9 @@ func (b *Builder) linkActionID(a *Action) cache.ActionID {
fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix)
if cfg.BuildTrimpath {
fmt.Fprintln(h, "trimpath")
if cfg.BuildTrimldflags {
fmt.Fprintln(h, "trimldflags")
}
}

// Toolchain-dependent configuration, shared with b.linkSharedActionID.
Expand Down

0 comments on commit 71d1aa8

Please sign in to comment.