Skip to content

Commit e27402a

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 60abc07 commit e27402a

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

src/cmd/dist/build.go

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var (
4848
defaultcflags string
4949
defaultldflags string
5050
defaultpkgconfig string
51+
defaultldso string
5152

5253
rebuildall bool
5354
defaultclang bool
@@ -207,6 +208,8 @@ func xinit() {
207208
}
208209
defaultpkgconfig = b
209210

211+
defaultldso = os.Getenv("GO_LDSO")
212+
210213
// For tools being invoked but also for os.ExpandEnv.
211214
os.Setenv("GO386", go386)
212215
os.Setenv("GOARCH", goarch)

src/cmd/dist/buildruntime.go

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func mkzbootstrap(file string) {
7676
fmt.Fprintf(&buf, "const defaultGOOS = runtime.GOOS\n")
7777
fmt.Fprintf(&buf, "const defaultGOARCH = runtime.GOARCH\n")
7878
fmt.Fprintf(&buf, "const defaultGO_EXTLINK_ENABLED = `%s`\n", goextlinkenabled)
79+
fmt.Fprintf(&buf, "const defaultGO_LDSO = `%s`\n", defaultldso)
7980
fmt.Fprintf(&buf, "const version = `%s`\n", findgoversion())
8081
fmt.Fprintf(&buf, "const stackGuardMultiplierDefault = %d\n", stackGuardMultiplierDefault())
8182
fmt.Fprintf(&buf, "const goexperiment = `%s`\n", os.Getenv("GOEXPERIMENT"))

src/cmd/dist/test.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,7 @@ func (t *tester) registerTests() {
570570
}
571571

572572
// Test internal linking of PIE binaries where it is supported.
573-
if goos == "linux" && goarch == "amd64" && !isAlpineLinux() {
574-
// Issue 18243: We don't have a way to set the default
575-
// dynamic linker used in internal linking mode. So
576-
// this test is skipped on Alpine.
573+
if goos == "linux" && goarch == "amd64" {
577574
t.tests = append(t.tests, distTest{
578575
name: "pie_internal",
579576
heading: "internal linking of -buildmode=pie",
@@ -899,10 +896,6 @@ func (t *tester) internalLink() bool {
899896
if goarch == "arm64" || goarch == "mips64" || goarch == "mips64le" || goarch == "mips" || goarch == "mipsle" {
900897
return false
901898
}
902-
if isAlpineLinux() {
903-
// Issue 18243.
904-
return false
905-
}
906899
return true
907900
}
908901

src/cmd/internal/objabi/util.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var (
2828
GOARM = goarm()
2929
GOMIPS = gomips()
3030
GOMIPS64 = gomips64()
31+
GO_LDSO = defaultGO_LDSO
3132
Version = version
3233
)
3334

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

+5
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,11 @@ func Asmbelf(ctxt *Link, symo int64) {
18401840
sh.type_ = SHT_PROGBITS
18411841
sh.flags = SHF_ALLOC
18421842
sh.addralign = 1
1843+
1844+
if interpreter == "" && objabi.GO_LDSO != "" {
1845+
interpreter = objabi.GO_LDSO
1846+
}
1847+
18431848
if interpreter == "" {
18441849
switch ctxt.HeadType {
18451850
case objabi.Hlinux:

src/make.bash

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
# controls the default behavior of the linker's -linkmode option. The
3535
# default value depends on the system.
3636
#
37+
# GO_LDSO: Sets the default dynamic linker/loader (ld.so) to be used
38+
# by the internal linker.
39+
#
3740
# CC: Command line to run to compile C code for GOHOSTARCH.
3841
# Default is "gcc". Also supported: "clang".
3942
#
@@ -126,6 +129,15 @@ if [ "$(uname -s)" = "GNU/kFreeBSD" ]; then
126129
export CGO_ENABLED=0
127130
fi
128131

132+
# On Alpine Linux, use the musl dynamic linker/loader
133+
if [ -f "/etc/alpine-release" ]; then
134+
if type readelf >/dev/null 2>&1; then
135+
echo "int main() { return 0; }" | ${CC:-gcc} -o ./test-alpine-ldso -x c -
136+
export GO_LDSO=$(readelf -l ./test-alpine-ldso | grep 'interpreter:' | sed -e 's/^.*interpreter: \(.*\)[]]/\1/')
137+
rm -f ./test-alpine-ldso
138+
fi
139+
fi
140+
129141
# Clean old generated file that will cause problems in the build.
130142
rm -f ./runtime/runtime_defs.go
131143

0 commit comments

Comments
 (0)