Skip to content

Commit ee40cf8

Browse files
committed
cmd/go: make sure the linker for shared doesn't include tempdir path
This is similar to CL 478196 and CL 477296, but this is for -buildmode=shared. When using "go install -buildmode=shared std", because the gold linker is used by default on Linux arm64, it will cause temporary paths to be included in libstd.so. Based on the changes of CL 478196, I speculate that this may also have issues on other platforms. So, this change is for all platform. But I don't have any other platforms and don't know what the file name for verification, so the testcase are only for the Linux platform. I hope someone can improve this testcase. Thanks! Fixes #69464
1 parent 165bf24 commit ee40cf8

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,21 @@ func (gcToolchain) ldShared(b *Builder, root *Action, toplevelactions []*Action,
714714
}
715715
ldflags = append(ldflags, d.Package.ImportPath+"="+d.Target)
716716
}
717-
return b.Shell(root).run(".", targetPath, nil, cfg.BuildToolexec, base.Tool("link"), "-o", targetPath, "-importcfg", importcfg, ldflags)
717+
718+
// On OS X when using external linking to build a shared library,
719+
// the argument passed here to -o ends up recorded in the final
720+
// shared library in the LC_ID_DYLIB load command.
721+
// To avoid putting the temporary output directory name there
722+
// (and making the resulting shared library useless),
723+
// run the link in the output directory so that -o can name
724+
// just the final path element.
725+
// On Windows, DLL file name is recorded in PE file
726+
// export section, so do like on OS X.
727+
// On Linux, for a shared object, at least with the Gold linker,
728+
// the output file path is recorded in the .gnu.version_d section.
729+
dir, targetPath := filepath.Split(targetPath)
730+
731+
return b.Shell(root).run(dir, targetPath, nil, cfg.BuildToolexec, base.Tool("link"), "-o", targetPath, "-importcfg", importcfg, ldflags)
718732
}
719733

720734
func (gcToolchain) cc(b *Builder, a *Action, ofile, cfile string) error {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[!buildmode:shared] skip
2+
[short] skip
3+
[!cgo] skip '-buildmode=shared requires external linking'
4+
[!GOOS:linux] skip
5+
6+
env GO111MODULE=off
7+
env CGO_ENABLED=1
8+
go install -a -trimpath -buildvcs=false -buildmode=shared -pkgdir=pkgdir1 runtime
9+
go install -a -trimpath -buildvcs=false -buildmode=shared -pkgdir=pkgdir2 runtime
10+
[GOOS:linux] cmp -q pkgdir1/libruntime.so pkgdir2/libruntime.so

0 commit comments

Comments
 (0)