Commit e27402ae authored by Tobias Klauser's avatar Tobias Klauser Committed by Tobias Klauser

cmd/dist, cmd/link: allow passing default dynamic linker/loader

Add an environment variable to make.bash to allow setting the default
dynamic linker/loader. This fixes alpine builds to use
/lib/ld-musl-x86_64.so.1:

  $ readelf -l ../bin/go | grep 'interpreter:' | sed -e 's/^.*interpreter: \(.*\)[]]/\1/'
  /lib/ld-musl-x86_64.so.1

Also re-enable the internal linker tests that were previously disabled
for alpine (CL 41759, CL 41678).

Fixes #18243
Updates #19938

This resurrects CL 50070 authored by Jessie Frazelle.

Change-Id: I132b5282045a3d60c8568e3b002a7f075eac2d93
Reviewed-on: https://go-review.googlesource.com/c/163977
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 60abc071
...@@ -48,6 +48,7 @@ var ( ...@@ -48,6 +48,7 @@ var (
defaultcflags string defaultcflags string
defaultldflags string defaultldflags string
defaultpkgconfig string defaultpkgconfig string
defaultldso string
rebuildall bool rebuildall bool
defaultclang bool defaultclang bool
...@@ -207,6 +208,8 @@ func xinit() { ...@@ -207,6 +208,8 @@ func xinit() {
} }
defaultpkgconfig = b defaultpkgconfig = b
defaultldso = os.Getenv("GO_LDSO")
// For tools being invoked but also for os.ExpandEnv. // For tools being invoked but also for os.ExpandEnv.
os.Setenv("GO386", go386) os.Setenv("GO386", go386)
os.Setenv("GOARCH", goarch) os.Setenv("GOARCH", goarch)
......
...@@ -76,6 +76,7 @@ func mkzbootstrap(file string) { ...@@ -76,6 +76,7 @@ func mkzbootstrap(file string) {
fmt.Fprintf(&buf, "const defaultGOOS = runtime.GOOS\n") fmt.Fprintf(&buf, "const defaultGOOS = runtime.GOOS\n")
fmt.Fprintf(&buf, "const defaultGOARCH = runtime.GOARCH\n") fmt.Fprintf(&buf, "const defaultGOARCH = runtime.GOARCH\n")
fmt.Fprintf(&buf, "const defaultGO_EXTLINK_ENABLED = `%s`\n", goextlinkenabled) fmt.Fprintf(&buf, "const defaultGO_EXTLINK_ENABLED = `%s`\n", goextlinkenabled)
fmt.Fprintf(&buf, "const defaultGO_LDSO = `%s`\n", defaultldso)
fmt.Fprintf(&buf, "const version = `%s`\n", findgoversion()) fmt.Fprintf(&buf, "const version = `%s`\n", findgoversion())
fmt.Fprintf(&buf, "const stackGuardMultiplierDefault = %d\n", stackGuardMultiplierDefault()) fmt.Fprintf(&buf, "const stackGuardMultiplierDefault = %d\n", stackGuardMultiplierDefault())
fmt.Fprintf(&buf, "const goexperiment = `%s`\n", os.Getenv("GOEXPERIMENT")) fmt.Fprintf(&buf, "const goexperiment = `%s`\n", os.Getenv("GOEXPERIMENT"))
......
...@@ -570,10 +570,7 @@ func (t *tester) registerTests() { ...@@ -570,10 +570,7 @@ func (t *tester) registerTests() {
} }
// Test internal linking of PIE binaries where it is supported. // Test internal linking of PIE binaries where it is supported.
if goos == "linux" && goarch == "amd64" && !isAlpineLinux() { if goos == "linux" && goarch == "amd64" {
// Issue 18243: We don't have a way to set the default
// dynamic linker used in internal linking mode. So
// this test is skipped on Alpine.
t.tests = append(t.tests, distTest{ t.tests = append(t.tests, distTest{
name: "pie_internal", name: "pie_internal",
heading: "internal linking of -buildmode=pie", heading: "internal linking of -buildmode=pie",
...@@ -899,10 +896,6 @@ func (t *tester) internalLink() bool { ...@@ -899,10 +896,6 @@ func (t *tester) internalLink() bool {
if goarch == "arm64" || goarch == "mips64" || goarch == "mips64le" || goarch == "mips" || goarch == "mipsle" { if goarch == "arm64" || goarch == "mips64" || goarch == "mips64le" || goarch == "mips" || goarch == "mipsle" {
return false return false
} }
if isAlpineLinux() {
// Issue 18243.
return false
}
return true return true
} }
......
...@@ -28,6 +28,7 @@ var ( ...@@ -28,6 +28,7 @@ var (
GOARM = goarm() GOARM = goarm()
GOMIPS = gomips() GOMIPS = gomips()
GOMIPS64 = gomips64() GOMIPS64 = gomips64()
GO_LDSO = defaultGO_LDSO
Version = version Version = version
) )
......
...@@ -1840,6 +1840,11 @@ func Asmbelf(ctxt *Link, symo int64) { ...@@ -1840,6 +1840,11 @@ func Asmbelf(ctxt *Link, symo int64) {
sh.type_ = SHT_PROGBITS sh.type_ = SHT_PROGBITS
sh.flags = SHF_ALLOC sh.flags = SHF_ALLOC
sh.addralign = 1 sh.addralign = 1
if interpreter == "" && objabi.GO_LDSO != "" {
interpreter = objabi.GO_LDSO
}
if interpreter == "" { if interpreter == "" {
switch ctxt.HeadType { switch ctxt.HeadType {
case objabi.Hlinux: case objabi.Hlinux:
......
...@@ -34,6 +34,9 @@ ...@@ -34,6 +34,9 @@
# controls the default behavior of the linker's -linkmode option. The # controls the default behavior of the linker's -linkmode option. The
# default value depends on the system. # default value depends on the system.
# #
# GO_LDSO: Sets the default dynamic linker/loader (ld.so) to be used
# by the internal linker.
#
# CC: Command line to run to compile C code for GOHOSTARCH. # CC: Command line to run to compile C code for GOHOSTARCH.
# Default is "gcc". Also supported: "clang". # Default is "gcc". Also supported: "clang".
# #
...@@ -126,6 +129,15 @@ if [ "$(uname -s)" = "GNU/kFreeBSD" ]; then ...@@ -126,6 +129,15 @@ if [ "$(uname -s)" = "GNU/kFreeBSD" ]; then
export CGO_ENABLED=0 export CGO_ENABLED=0
fi fi
# On Alpine Linux, use the musl dynamic linker/loader
if [ -f "/etc/alpine-release" ]; then
if type readelf >/dev/null 2>&1; then
echo "int main() { return 0; }" | ${CC:-gcc} -o ./test-alpine-ldso -x c -
export GO_LDSO=$(readelf -l ./test-alpine-ldso | grep 'interpreter:' | sed -e 's/^.*interpreter: \(.*\)[]]/\1/')
rm -f ./test-alpine-ldso
fi
fi
# Clean old generated file that will cause problems in the build. # Clean old generated file that will cause problems in the build.
rm -f ./runtime/runtime_defs.go rm -f ./runtime/runtime_defs.go
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment