Skip to content

Commit

Permalink
cmd/dist, cmd/link: allow passing default dynamic linker/loader
Browse files Browse the repository at this point in the history
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:

    $ ldd ../bin/go
        /lib/ld-musl-x86_64.so.1 (0x5608dfadd000)
        libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x5608dfadd000)

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.

Co-authored-by: Jessie Frazelle <me@jessfraz.com>

Change-Id: I132b5282045a3d60c8568e3b002a7f075eac2d93
  • Loading branch information
tklauser committed Dec 7, 2018
1 parent 276870d commit da45db8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/cmd/dist/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
defaultcflags string
defaultldflags string
defaultpkgconfig string
defaultldso string

rebuildall bool
defaultclang bool
Expand Down Expand Up @@ -207,6 +208,8 @@ func xinit() {
}
defaultpkgconfig = b

defaultldso = os.Getenv("GO_LDSO")

// For tools being invoked but also for os.ExpandEnv.
os.Setenv("GO386", go386)
os.Setenv("GOARCH", goarch)
Expand Down
1 change: 1 addition & 0 deletions src/cmd/dist/buildruntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func mkzbootstrap(file string) {
fmt.Fprintf(&buf, "const defaultGOOS = runtime.GOOS\n")
fmt.Fprintf(&buf, "const defaultGOARCH = runtime.GOARCH\n")
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 stackGuardMultiplier = %d\n", stackGuardMultiplier())
fmt.Fprintf(&buf, "const goexperiment = `%s`\n", os.Getenv("GOEXPERIMENT"))
Expand Down
9 changes: 1 addition & 8 deletions src/cmd/dist/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,7 @@ func (t *tester) registerTests() {
}

// Test internal linking of PIE binaries where it is supported.
if goos == "linux" && goarch == "amd64" && !isAlpineLinux() {
// 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.
if goos == "linux" && goarch == "amd64" {
t.tests = append(t.tests, distTest{
name: "pie_internal",
heading: "internal linking of -buildmode=pie",
Expand Down Expand Up @@ -927,10 +924,6 @@ func (t *tester) internalLink() bool {
if goarch == "arm64" || goarch == "mips64" || goarch == "mips64le" || goarch == "mips" || goarch == "mipsle" {
return false
}
if isAlpineLinux() {
// Issue 18243.
return false
}
return true
}

Expand Down
1 change: 1 addition & 0 deletions src/cmd/internal/objabi/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
GOARM = goarm()
GOMIPS = gomips()
GOMIPS64 = gomips64()
GO_LDSO = envOr("GO_LDSO", defaultGO_LDSO)
Version = version
)

Expand Down
5 changes: 5 additions & 0 deletions src/cmd/link/internal/ld/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,11 @@ func Asmbelf(ctxt *Link, symo int64) {
sh.type_ = SHT_PROGBITS
sh.flags = SHF_ALLOC
sh.addralign = 1

if interpreter == "" && objabi.GO_LDSO != "" {
interpreter = objabi.GO_LDSO
}

if interpreter == "" {
switch ctxt.HeadType {
case objabi.Hlinux:
Expand Down
12 changes: 12 additions & 0 deletions src/make.bash
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
# controls the default behavior of the linker's -linkmode option. The
# 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.
# Default is "gcc". Also supported: "clang".
#
Expand Down Expand Up @@ -126,6 +129,15 @@ if [ "$(uname -s)" = "GNU/kFreeBSD" ]; then
export CGO_ENABLED=0
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.
rm -f ./runtime/runtime_defs.go

Expand Down

0 comments on commit da45db8

Please sign in to comment.