Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: make sure the linker for shared doesn't include tempdir path #69394

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/cmd/go/internal/work/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,21 @@ func (gcToolchain) ldShared(b *Builder, root *Action, toplevelactions []*Action,
}
ldflags = append(ldflags, d.Package.ImportPath+"="+d.Target)
}
return b.Shell(root).run(".", targetPath, nil, cfg.BuildToolexec, base.Tool("link"), "-o", targetPath, "-importcfg", importcfg, ldflags)

// On OS X when using external linking to build a shared library,
// the argument passed here to -o ends up recorded in the final
// shared library in the LC_ID_DYLIB load command.
// To avoid putting the temporary output directory name there
// (and making the resulting shared library useless),
// run the link in the output directory so that -o can name
// just the final path element.
// On Windows, DLL file name is recorded in PE file
// export section, so do like on OS X.
// On Linux, for a shared object, at least with the Gold linker,
// the output file path is recorded in the .gnu.version_d section.
dir, targetPath := filepath.Split(targetPath)

return b.Shell(root).run(dir, targetPath, nil, cfg.BuildToolexec, base.Tool("link"), "-o", targetPath, "-importcfg", importcfg, ldflags)
}

func (gcToolchain) cc(b *Builder, a *Action, ofile, cfile string) error {
Expand Down
10 changes: 10 additions & 0 deletions src/cmd/go/testdata/script/build_shared_reproducible.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[!buildmode:shared] skip
[short] skip
[!cgo] skip '-buildmode=shared requires external linking'
[!GOOS:linux] skip

env GO111MODULE=off
env CGO_ENABLED=1
go install -a -trimpath -buildvcs=false -buildmode=shared -pkgdir=pkgdir1 runtime
go install -a -trimpath -buildvcs=false -buildmode=shared -pkgdir=pkgdir2 runtime
[GOOS:linux] cmp -q pkgdir1/libruntime.so pkgdir2/libruntime.so