Skip to content

Commit 0355b6a

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 Change-Id: I47d633ef0e6a90136799bef9701b7ff22c92f095 GitHub-Pull-Request: #67072
1 parent dc164ea commit 0355b6a

File tree

6 files changed

+32
-1
lines changed

6 files changed

+32
-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. 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.

src/cmd/go/testdata/counters.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ go/flag:build-pkgdir
7777
go/flag:build-race
7878
go/flag:build-tags
7979
go/flag:build-toolexec
80+
go/flag:build-trimldflags
8081
go/flag:build-trimpath
8182
go/flag:build-v
8283
go/flag:build-work
@@ -116,6 +117,7 @@ go/flag:clean-race
116117
go/flag:clean-tags
117118
go/flag:clean-testcache
118119
go/flag:clean-toolexec
120+
go/flag:clean-trimldflags
119121
go/flag:clean-trimpath
120122
go/flag:clean-v
121123
go/flag:clean-work
@@ -160,6 +162,7 @@ go/flag:fix-pkgdir
160162
go/flag:fix-race
161163
go/flag:fix-tags
162164
go/flag:fix-toolexec
165+
go/flag:fix-trimldflags
163166
go/flag:fix-trimpath
164167
go/flag:fix-v
165168
go/flag:fix-work
@@ -204,6 +207,7 @@ go/flag:generate-run
204207
go/flag:generate-skip
205208
go/flag:generate-tags
206209
go/flag:generate-toolexec
210+
go/flag:generate-trimldflags
207211
go/flag:generate-trimpath
208212
go/flag:generate-v
209213
go/flag:generate-work
@@ -242,6 +246,7 @@ go/flag:get-race
242246
go/flag:get-t
243247
go/flag:get-tags
244248
go/flag:get-toolexec
249+
go/flag:get-trimldflags
245250
go/flag:get-trimpath
246251
go/flag:get-u
247252
go/flag:get-v
@@ -279,6 +284,7 @@ go/flag:install-pkgdir
279284
go/flag:install-race
280285
go/flag:install-tags
281286
go/flag:install-toolexec
287+
go/flag:install-trimldflags
282288
go/flag:install-trimpath
283289
go/flag:install-v
284290
go/flag:install-work
@@ -326,6 +332,7 @@ go/flag:list-reuse
326332
go/flag:list-tags
327333
go/flag:list-test
328334
go/flag:list-toolexec
335+
go/flag:list-trimldflags
329336
go/flag:list-trimpath
330337
go/flag:list-u
331338
go/flag:list-v
@@ -499,6 +506,7 @@ go/flag:run-pkgdir
499506
go/flag:run-race
500507
go/flag:run-tags
501508
go/flag:run-toolexec
509+
go/flag:run-trimldflags
502510
go/flag:run-trimpath
503511
go/flag:run-v
504512
go/flag:run-work
@@ -594,6 +602,7 @@ go/flag:test-test.v
594602
go/flag:test-timeout
595603
go/flag:test-toolexec
596604
go/flag:test-trace
605+
go/flag:test-trimldflags
597606
go/flag:test-trimpath
598607
go/flag:test-v
599608
go/flag:test-vet
@@ -658,6 +667,7 @@ go/flag:vet-pkgdir
658667
go/flag:vet-race
659668
go/flag:vet-tags
660669
go/flag:vet-toolexec
670+
go/flag:vet-trimldflags
661671
go/flag:vet-trimpath
662672
go/flag:vet-v
663673
go/flag:vet-vettool

0 commit comments

Comments
 (0)