Skip to content

Commit 0d6c211

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 golang#18243 Updates golang#19938 This resurrects CL 50070 which was authored by Jessie Frazelle. Change-Id: I132b5282045a3d60c8568e3b002a7f075eac2d93
1 parent 49abcf1 commit 0d6c211

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

src/cmd/dist/build.go

Lines changed: 3 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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 stackGuardMultiplier = %d\n", stackGuardMultiplier())
8182
fmt.Fprintf(&buf, "const goexperiment = `%s`\n", os.Getenv("GOEXPERIMENT"))

src/cmd/dist/test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,7 @@ func (t *tester) registerTests() {
568568
}
569569

570570
// Test internal linking of PIE binaries where it is supported.
571-
if goos == "linux" && goarch == "amd64" && !isAlpineLinux() {
572-
// Issue 18243: We don't have a way to set the default
573-
// dynamic linker used in internal linking mode. So
574-
// this test is skipped on Alpine.
571+
if goos == "linux" && goarch == "amd64" {
575572
t.tests = append(t.tests, distTest{
576573
name: "pie_internal",
577574
heading: "internal linking of -buildmode=pie",
@@ -925,10 +922,6 @@ func (t *tester) internalLink() bool {
925922
if goarch == "arm64" || goarch == "mips64" || goarch == "mips64le" || goarch == "mips" || goarch == "mipsle" {
926923
return false
927924
}
928-
if isAlpineLinux() {
929-
// Issue 18243.
930-
return false
931-
}
932925
return true
933926
}
934927

src/cmd/internal/objabi/util.go

Lines changed: 1 addition & 0 deletions
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 = envOr("GO_LDSO", defaultGO_LDSO)
3132
Version = version
3233
)
3334

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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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)