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

Linuxbrewed go 1.15 binary can't be executed in other os #43015

Closed
OuSatoru opened this issue Dec 5, 2020 · 1 comment
Closed

Linuxbrewed go 1.15 binary can't be executed in other os #43015

OuSatoru opened this issue Dec 5, 2020 · 1 comment

Comments

@OuSatoru
Copy link

OuSatoru commented Dec 5, 2020

What version of Go are you using (go version)?

$ go version
go version go1.15.5 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/wwcy/.cache/go-build"
GOENV="/home/wwcy/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/wwcy/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/wwcy/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/linuxbrew/.linuxbrew/Cellar/go/1.15.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/linuxbrew/.linuxbrew/Cellar/go/1.15.5/libexec/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc-5"
CXX="g++-5"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build959384268=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Write a simple program then go build then put it in another os.

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "11")
}

runs

➜  fuck git:(master) ✗ go build -o fuck_linux_amd64
➜  fuck git:(master) ✗ ldd fuck_linux_amd64
        linux-vdso.so.1 (0x00007ffd921ee000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f4ab861d000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4ab842b000)
        /home/linuxbrew/.linuxbrew/lib/ld.so => /lib64/ld-linux-x86-64.so.2 (0x00007f4ab864a000)

scp it to a CentOS 7 server produces

-bash: ./fuck_linux_amd64: /home/linuxbrew/.linuxbrew/lib/ld.so: bad ELF interpreter: no such file or directory
[root@dt-s-27db2f2d ~]# ldd fuck_linux_amd64
linux-vdso.so.1 => (0x00007fffb3f6b000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003d2b000000)
libc.so.6 => /lib64/libc.so.6 (0x0000003d2ac00000)
/home/linuxbrew/.linuxbrew/lib/ld.so => /lib64/ld-linux-x86-64.so.2 (0x000055a0816cf000)

What did you expect to see?

can run the program

What did you see instead?

@ianlancetaylor
Copy link
Contributor

By default Go programs that import the "net" package, in your example via "net/http", are dynamically linked with the system libraries and system dynamic interpreter. That can in some cases mean that those programs can't be run on a different system. That is what is happening to you.

You can avoid this by setting CGO_ENABLED=0 when you run go build. That will give you a statically linked program. That has various consequences, such as not supporting certain complex and unusual DNS configurations.

I'm closing this issue because this is not a bug in Go. If you have further questions about this, please do not use the issue tracker. Please use a forum instead. See https://golang.org/wiki/Questions. Thanks.

@golang golang locked and limited conversation to collaborators Dec 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants