Skip to content

Commit 11497da

Browse files
stapelberghnakamur
authored andcommitted
Reproducible BUILD_PATH_PREFIX_MAP
Make builds reproducible by honoring BUILD_PATH_PREFIX_MAP Upstream has rejected the patch in this form and promised to implement an alternative they are happy with instead. That hasn't happened yet though. Bug: golang/go#22491, golang/go#16860 Forwarded: https://golang.org/cl/73291 (rejected upstream though)
1 parent 507b87b commit 11497da

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

src/cmd/go/internal/work/gc.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,10 @@ func (gcToolchain) ld(b *Builder, root *Action, out, importcfg, mainpkg string)
560560
dir, out = filepath.Split(out)
561561
}
562562

563-
return b.run(root, dir, root.Package.ImportPath, nil, cfg.BuildToolexec, base.Tool("link"), "-o", out, "-importcfg", importcfg, ldflags, mainpkg)
563+
// Apply the rewrite of $WORK to /tmp/go-build also to DWARF file tables:
564+
env := []string{"BUILD_PATH_PREFIX_MAP=/tmp/go-build=" + b.WorkDir + ":" + os.Getenv("BUILD_PATH_PREFIX_MAP")}
565+
566+
return b.run(root, dir, root.Package.ImportPath, env, cfg.BuildToolexec, base.Tool("link"), "-o", out, "-importcfg", importcfg, ldflags, mainpkg)
564567
}
565568

566569
func (gcToolchain) ldShared(b *Builder, root *Action, toplevelactions []*Action, out, importcfg string, allactions []*Action) error {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2010 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package ld
6+
7+
import (
8+
"log"
9+
"os"
10+
"strings"
11+
"sync"
12+
)
13+
14+
func decode(s string) string {
15+
s = strings.Replace(s, "%.", ":", -1)
16+
s = strings.Replace(s, "%+", "=", -1)
17+
s = strings.Replace(s, "%#", "%", -1)
18+
return s
19+
}
20+
21+
type prefixMapEntry struct {
22+
target string
23+
source string
24+
}
25+
26+
var (
27+
buildPathPrefixMap []prefixMapEntry
28+
buildPathPrefixMapOnce sync.Once
29+
)
30+
31+
// See https://reproducible-builds.org/specs/build-path-prefix-map/
32+
func applyBuildPathPrefixMap(dir string) string {
33+
// Parse the BUILD_PATH_PREFIX_MAP only once; this function gets called for
34+
// every compiled file.
35+
buildPathPrefixMapOnce.Do(func() {
36+
for _, item := range strings.Split(os.Getenv("BUILD_PATH_PREFIX_MAP"), ":") {
37+
if strings.TrimSpace(item) == "" {
38+
continue
39+
}
40+
parts := strings.Split(item, "=")
41+
if got, want := len(parts), 2; got != want {
42+
log.Fatalf("parsing BUILD_PATH_PREFIX_MAP: incorrect number of = separators in item %q: got %d, want %d", item, got-1, want-1)
43+
}
44+
buildPathPrefixMap = append(buildPathPrefixMap, prefixMapEntry{
45+
target: decode(parts[0]),
46+
source: decode(parts[1]),
47+
})
48+
}
49+
})
50+
for _, e := range buildPathPrefixMap {
51+
if strings.HasPrefix(dir, e.source) {
52+
return e.target + strings.TrimPrefix(dir, e.source)
53+
}
54+
}
55+
return dir
56+
}

src/cmd/link/internal/ld/dwarf.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ func writelines(ctxt *Link, unit *compilationUnit, ls *sym.Symbol) {
11731173
}
11741174
// File indexes are 1-based.
11751175
fileNums[int(f.Value)] = len(fileNums) + 1
1176-
Addstring(ls, f.Name)
1176+
Addstring(ls, applyBuildPathPrefixMap(f.Name))
11771177
ls.AddUint8(0)
11781178
ls.AddUint8(0)
11791179
ls.AddUint8(0)
@@ -1752,7 +1752,7 @@ func dwarfGenerateDebugInfo(ctxt *Link) {
17521752
unit.dwinfo = newdie(ctxt, &dwroot, dwarf.DW_ABRV_COMPUNIT, unit.lib.Pkg, 0)
17531753
newattr(unit.dwinfo, dwarf.DW_AT_language, dwarf.DW_CLS_CONSTANT, int64(dwarf.DW_LANG_Go), 0)
17541754
// OS X linker requires compilation dir or absolute path in comp unit name to output debug info.
1755-
compDir := getCompilationDir()
1755+
compDir := applyBuildPathPrefixMap(getCompilationDir())
17561756
// TODO: Make this be the actual compilation directory, not
17571757
// the linker directory. If we move CU construction into the
17581758
// compiler, this should happen naturally.

src/cmd/link/internal/ld/pcln.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func (ctxt *Link) pclntab() {
302302
off = int32(ftab.SetAddr(ctxt.Arch, int64(off), s))
303303

304304
// name int32
305-
nameoff := nameToOffset(s.Name)
305+
nameoff := nameToOffset(applyBuildPathPrefixMap(s.Name))
306306
off = int32(ftab.SetUint32(ctxt.Arch, int64(off), uint32(nameoff)))
307307

308308
// args int32
@@ -449,7 +449,7 @@ func (ctxt *Link) pclntab() {
449449
ftab.SetUint32(ctxt.Arch, int64(start), uint32(len(ctxt.Filesyms)+1))
450450
for i := len(ctxt.Filesyms) - 1; i >= 0; i-- {
451451
s := ctxt.Filesyms[i]
452-
ftab.SetUint32(ctxt.Arch, int64(start)+s.Value*4, uint32(ftabaddstring(ctxt, ftab, s.Name)))
452+
ftab.SetUint32(ctxt.Arch, int64(start)+s.Value*4, uint32(ftabaddstring(ctxt, ftab, applyBuildPathPrefixMap(s.Name))))
453453
}
454454

455455
ftab.Size = int64(len(ftab.P))

0 commit comments

Comments
 (0)