Skip to content

Commit a1fb024

Browse files
committed
cmd/go: hide work subdirectory names in gcc/clang object files
Until now the subdirectories under $WORK have had predictable names, so it was OK to strip just $WORK from the file names that end up in object files. In the future, those predictable names would cause predictable collisions when compiling one package in two different ways, so we're moving toward arbitrary, unpredictable subdirectory names instead. When we do that, if the names appear in the object files we won't get reproducible builds. Take the subdirectory names out now, to make the later change safe. Change-Id: I8057d1cc73f6e35c98b7718c9789c161dcbd87c0 Reviewed-on: https://go-review.googlesource.com/67251 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 1409c28 commit a1fb024

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

src/cmd/go/internal/envcmd/env.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ func MkEnv() []cfg.EnvVar {
7373
env = append(env, cfg.EnvVar{Name: "GO386", Value: cfg.GO386})
7474
}
7575

76-
cmd := b.GccCmd(".")
76+
cmd := b.GccCmd(".", "")
7777
env = append(env, cfg.EnvVar{Name: "CC", Value: cmd[0]})
7878
env = append(env, cfg.EnvVar{Name: "GOGCCFLAGS", Value: strings.Join(cmd[3:], " ")})
79-
cmd = b.GxxCmd(".")
79+
cmd = b.GxxCmd(".", "")
8080
env = append(env, cfg.EnvVar{Name: "CXX", Value: cmd[0]})
8181

8282
if cfg.BuildContext.CgoEnabled {

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3157,18 +3157,18 @@ func gccgoCleanPkgpath(p *load.Package) string {
31573157
}
31583158

31593159
// gcc runs the gcc C compiler to create an object from a single C file.
3160-
func (b *Builder) gcc(p *load.Package, out string, flags []string, cfile string) error {
3161-
return b.ccompile(p, out, flags, cfile, b.GccCmd(p.Dir))
3160+
func (b *Builder) gcc(p *load.Package, workdir, out string, flags []string, cfile string) error {
3161+
return b.ccompile(p, out, flags, cfile, b.GccCmd(p.Dir, workdir))
31623162
}
31633163

31643164
// gxx runs the g++ C++ compiler to create an object from a single C++ file.
3165-
func (b *Builder) gxx(p *load.Package, out string, flags []string, cxxfile string) error {
3166-
return b.ccompile(p, out, flags, cxxfile, b.GxxCmd(p.Dir))
3165+
func (b *Builder) gxx(p *load.Package, workdir, out string, flags []string, cxxfile string) error {
3166+
return b.ccompile(p, out, flags, cxxfile, b.GxxCmd(p.Dir, workdir))
31673167
}
31683168

31693169
// gfortran runs the gfortran Fortran compiler to create an object from a single Fortran file.
3170-
func (b *Builder) gfortran(p *load.Package, out string, flags []string, ffile string) error {
3171-
return b.ccompile(p, out, flags, ffile, b.gfortranCmd(p.Dir))
3170+
func (b *Builder) gfortran(p *load.Package, workdir, out string, flags []string, ffile string) error {
3171+
return b.ccompile(p, out, flags, ffile, b.gfortranCmd(p.Dir, workdir))
31723172
}
31733173

31743174
// ccompile runs the given C or C++ compiler and creates an object from a single source file.
@@ -3211,41 +3211,41 @@ func (b *Builder) ccompile(p *load.Package, outfile string, flags []string, file
32113211
}
32123212

32133213
// gccld runs the gcc linker to create an executable from a set of object files.
3214-
func (b *Builder) gccld(p *load.Package, out string, flags []string, objs []string) error {
3214+
func (b *Builder) gccld(p *load.Package, objdir, out string, flags []string, objs []string) error {
32153215
var cmd []string
32163216
if len(p.CXXFiles) > 0 || len(p.SwigCXXFiles) > 0 {
3217-
cmd = b.GxxCmd(p.Dir)
3217+
cmd = b.GxxCmd(p.Dir, objdir)
32183218
} else {
3219-
cmd = b.GccCmd(p.Dir)
3219+
cmd = b.GccCmd(p.Dir, objdir)
32203220
}
32213221
return b.run(p.Dir, p.ImportPath, nil, cmd, "-o", out, objs, flags)
32223222
}
32233223

32243224
// gccCmd returns a gcc command line prefix
32253225
// defaultCC is defined in zdefaultcc.go, written by cmd/dist.
3226-
func (b *Builder) GccCmd(objdir string) []string {
3227-
return b.compilerCmd("CC", cfg.DefaultCC, objdir)
3226+
func (b *Builder) GccCmd(incdir, workdir string) []string {
3227+
return b.compilerCmd("CC", cfg.DefaultCC, incdir, workdir)
32283228
}
32293229

32303230
// gxxCmd returns a g++ command line prefix
32313231
// defaultCXX is defined in zdefaultcc.go, written by cmd/dist.
3232-
func (b *Builder) GxxCmd(objdir string) []string {
3233-
return b.compilerCmd("CXX", cfg.DefaultCXX, objdir)
3232+
func (b *Builder) GxxCmd(incdir, workdir string) []string {
3233+
return b.compilerCmd("CXX", cfg.DefaultCXX, incdir, workdir)
32343234
}
32353235

32363236
// gfortranCmd returns a gfortran command line prefix.
3237-
func (b *Builder) gfortranCmd(objdir string) []string {
3238-
return b.compilerCmd("FC", "gfortran", objdir)
3237+
func (b *Builder) gfortranCmd(incdir, workdir string) []string {
3238+
return b.compilerCmd("FC", "gfortran", incdir, workdir)
32393239
}
32403240

32413241
// compilerCmd returns a command line prefix for the given environment
32423242
// variable and using the default command when the variable is empty.
3243-
func (b *Builder) compilerCmd(envvar, defcmd, objdir string) []string {
3243+
func (b *Builder) compilerCmd(envvar, defcmd, incdir, workdir string) []string {
32443244
// NOTE: env.go's mkEnv knows that the first three
3245-
// strings returned are "gcc", "-I", objdir (and cuts them off).
3245+
// strings returned are "gcc", "-I", incdir (and cuts them off).
32463246

32473247
compiler := envList(envvar, defcmd)
3248-
a := []string{compiler[0], "-I", objdir}
3248+
a := []string{compiler[0], "-I", incdir}
32493249
a = append(a, compiler[1:]...)
32503250

32513251
// Definitely want -fPIC but on Windows gcc complains
@@ -3279,7 +3279,11 @@ func (b *Builder) compilerCmd(envvar, defcmd, objdir string) []string {
32793279

32803280
// Tell gcc not to include the work directory in object files.
32813281
if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
3282-
a = append(a, "-fdebug-prefix-map="+b.WorkDir+"=/tmp/go-build")
3282+
if workdir == "" {
3283+
workdir = b.WorkDir
3284+
}
3285+
workdir = strings.TrimSuffix(workdir, string(filepath.Separator))
3286+
a = append(a, "-fdebug-prefix-map="+workdir+"=/tmp/go-build")
32833287
}
32843288

32853289
// Tell gcc not to include flags in object files, which defeats the
@@ -3508,15 +3512,15 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
35083512
cflags := str.StringList(cgoCPPFLAGS, cgoCFLAGS)
35093513
for _, cfile := range cfiles {
35103514
ofile := nextOfile()
3511-
if err := b.gcc(p, ofile, cflags, objdir+cfile); err != nil {
3515+
if err := b.gcc(p, a.Objdir, ofile, cflags, objdir+cfile); err != nil {
35123516
return nil, nil, err
35133517
}
35143518
outObj = append(outObj, ofile)
35153519
}
35163520

35173521
for _, file := range gccfiles {
35183522
ofile := nextOfile()
3519-
if err := b.gcc(p, ofile, cflags, file); err != nil {
3523+
if err := b.gcc(p, a.Objdir, ofile, cflags, file); err != nil {
35203524
return nil, nil, err
35213525
}
35223526
outObj = append(outObj, ofile)
@@ -3525,15 +3529,15 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
35253529
cxxflags := str.StringList(cgoCPPFLAGS, cgoCXXFLAGS)
35263530
for _, file := range gxxfiles {
35273531
ofile := nextOfile()
3528-
if err := b.gxx(p, ofile, cxxflags, file); err != nil {
3532+
if err := b.gxx(p, a.Objdir, ofile, cxxflags, file); err != nil {
35293533
return nil, nil, err
35303534
}
35313535
outObj = append(outObj, ofile)
35323536
}
35333537

35343538
for _, file := range mfiles {
35353539
ofile := nextOfile()
3536-
if err := b.gcc(p, ofile, cflags, file); err != nil {
3540+
if err := b.gcc(p, a.Objdir, ofile, cflags, file); err != nil {
35373541
return nil, nil, err
35383542
}
35393543
outObj = append(outObj, ofile)
@@ -3542,7 +3546,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
35423546
fflags := str.StringList(cgoCPPFLAGS, cgoFFLAGS)
35433547
for _, file := range ffiles {
35443548
ofile := nextOfile()
3545-
if err := b.gfortran(p, ofile, fflags, file); err != nil {
3549+
if err := b.gfortran(p, a.Objdir, ofile, fflags, file); err != nil {
35463550
return nil, nil, err
35473551
}
35483552
outObj = append(outObj, ofile)
@@ -3577,7 +3581,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
35773581
func (b *Builder) dynimport(p *load.Package, objdir, importGo, cgoExe string, cflags, cgoLDFLAGS, outObj []string) error {
35783582
cfile := objdir + "_cgo_main.c"
35793583
ofile := objdir + "_cgo_main.o"
3580-
if err := b.gcc(p, ofile, cflags, cfile); err != nil {
3584+
if err := b.gcc(p, objdir, ofile, cflags, cfile); err != nil {
35813585
return err
35823586
}
35833587

@@ -3589,7 +3593,7 @@ func (b *Builder) dynimport(p *load.Package, objdir, importGo, cgoExe string, cf
35893593
if (cfg.Goarch == "arm" && cfg.Goos == "linux") || cfg.Goos == "android" {
35903594
ldflags = append(ldflags, "-pie")
35913595
}
3592-
if err := b.gccld(p, dynobj, ldflags, linkobj); err != nil {
3596+
if err := b.gccld(p, objdir, dynobj, ldflags, linkobj); err != nil {
35933597
return err
35943598
}
35953599

0 commit comments

Comments
 (0)