From 083b5c74b12a1abeb11dd7f58a1cb1593d0000c0 Mon Sep 17 00:00:00 2001 From: Changqing Li Date: Tue, 27 Feb 2024 18:06:51 +0800 Subject: [PATCH] exec.go: filter out build-specific paths from linker flags The flags can contain build-specific paths, breaking reproducibility. Filter out options that have build-specific paths. Upstream-Status: Inappropriate [ Not perfect for upstream ] Signed-off-by: Changqing Li --- src/cmd/go/internal/work/exec.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go index cde867b..e3ce17d 100644 --- a/src/cmd/go/internal/work/exec.go +++ b/src/cmd/go/internal/work/exec.go @@ -1358,6 +1358,29 @@ func (b *Builder) linkActionID(a *Action) cache.ActionID { return h.Sum() } +func filterLinkerFlags(flags []string) []string { + var newflags []string + var skipflag bool + skipflag = false + for i, flag := range flags { + if skipflag == true { + skipflag = false + continue + } + if strings.HasPrefix(flag, "--sysroot") || strings.HasPrefix(flag, "-fmacro-prefix-map") || strings.HasPrefix(flag, "-fdebug-prefix-map") || strings.HasPrefix(flag, "-ffile-prefix-map") || strings.HasPrefix(flag, "-fcanon-prefix-map") || strings.HasPrefix(flag, "-fprofile-prefix-map") || strings.HasPrefix(flag, "-Wl,-rpath-link"){ + continue + } else if strings.HasPrefix(flag, "-extldflags") { + skipflag = true + newflags = append(newflags, flag) + var filterd_Extldflags []string = filterLinkerFlags(strings.Split(flags[i+1], " ")) + newflags = append(newflags, strings.Join(filterd_Extldflags, " ")) + } else { + newflags = append(newflags, flag) + } + } + return newflags +} + // printLinkerConfig prints the linker config into the hash h, // as part of the computation of a linker-related action ID. func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) { @@ -1368,7 +1391,7 @@ func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) { case "gc": fmt.Fprintf(h, "link %s %q %s\n", b.toolID("link"), forcedLdflags, ldBuildmode) if p != nil { - fmt.Fprintf(h, "linkflags %q\n", p.Internal.Ldflags) + fmt.Fprintf(h, "linkflags %q\n", filterLinkerFlags(p.Internal.Ldflags)) } // GOARM, GOMIPS, etc. -- 2.25.1