Skip to content

Commit 71d1aa8

Browse files
committed
cmd/go: report trimpath erasing ldflags, and allow override
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
1 parent 5419f65 commit 71d1aa8

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

src/cmd/go/alldocs.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/cfg/cfg.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ var (
8686
BuildToolexec []string // -toolexec flag
8787
BuildToolchainName string
8888
BuildTrimpath bool // -trimpath flag
89+
BuildTrimldflags bool // -trimldflags flag
8990
BuildV bool // -v flag
9091
BuildWork bool // -work flag
9192
BuildX bool // -x flag

src/cmd/go/internal/load/pkg.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2395,7 +2395,14 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) {
23952395
// determine whether they may refer to system paths. If we do that, we can
23962396
// redact only those paths from the recorded -ldflags setting and still
23972397
// record the system-independent parts of the flags.
2398-
if !cfg.BuildTrimpath {
2398+
//
2399+
// For now add a toggle to always allow ldflags reporting, it may make
2400+
// non-reproducible builds, but it will stop hiding valuable version
2401+
// information as used by security vulnerability scanners. Although maybe
2402+
// vcs.describe or vcs.modhash should be added instead.
2403+
if cfg.BuildTrimpath && cfg.BuildTrimldflags {
2404+
appendSetting("-trimldflags", "true")
2405+
} else {
23992406
appendSetting("-ldflags", ldflags)
24002407
}
24012408
}

src/cmd/go/internal/work/build.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ and test commands:
192192
Instead of absolute file system paths, the recorded file names
193193
will begin either a module path@version (when using modules),
194194
or a plain import path (when using the standard library, or GOPATH).
195+
-trimldflags
196+
Only meaningful with -trimpath true. Controls reporting of ldflags in binary
197+
module information. May affect reproducible builds.
195198
-toolexec 'cmd args'
196199
a program to use to invoke toolchain programs like vet and asm.
197200
For example, instead of running asm, the go command will run
@@ -338,6 +341,7 @@ func AddBuildFlags(cmd *base.Command, mask BuildFlagMask) {
338341
cmd.Flag.Var((*tagsFlag)(&cfg.BuildContext.BuildTags), "tags", "")
339342
cmd.Flag.Var((*base.StringsFlag)(&cfg.BuildToolexec), "toolexec", "")
340343
cmd.Flag.BoolVar(&cfg.BuildTrimpath, "trimpath", false, "")
344+
cmd.Flag.BoolVar(&cfg.BuildTrimldflags, "trimldflags", true, "")
341345
cmd.Flag.BoolVar(&cfg.BuildWork, "work", false, "")
342346
cmd.Flag.Var((*buildvcsFlag)(&cfg.BuildBuildvcs), "buildvcs", "")
343347

src/cmd/go/internal/work/exec.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
283283
fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix)
284284
if cfg.BuildTrimpath {
285285
fmt.Fprintln(h, "trimpath")
286+
if cfg.BuildTrimldflags {
287+
fmt.Fprintln(h, "trimldflags")
288+
}
286289
}
287290
if p.Internal.ForceLibrary {
288291
fmt.Fprintf(h, "forcelibrary\n")
@@ -1368,6 +1371,9 @@ func (b *Builder) linkActionID(a *Action) cache.ActionID {
13681371
fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix)
13691372
if cfg.BuildTrimpath {
13701373
fmt.Fprintln(h, "trimpath")
1374+
if cfg.BuildTrimldflags {
1375+
fmt.Fprintln(h, "trimldflags")
1376+
}
13711377
}
13721378

13731379
// Toolchain-dependent configuration, shared with b.linkSharedActionID.

0 commit comments

Comments
 (0)