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

cmd/link: Go 1.12: depending on source, "go build" generates dynamically linked binaries (uses shared libs) #30419

Closed
jeffkayser2 opened this issue Feb 26, 2019 · 3 comments

Comments

@jeffkayser2
Copy link

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

$ go version
go version go1.12 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
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jkayser/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/jkayser"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go1.12"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go1.12/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
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-build474563303=/tmp/go-build -gno-record-gcc-switches"

What did you do?

$ ls -lrt
total 0
-rw-rw-r-- 1 jkayser jkayser 82 Feb 18 14:22 hello.go
-rw-rw-r-- 1 jkayser jkayser 239 Feb 18 14:57 server.go
$ go build hello.go
$ go build server.go
$ file *
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
hello.go: C source, ASCII text
server: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
server.go: C source, ASCII text
$

hello.go: https://play.golang.org/p/ueti2wwyW3N
server.go: https://play.golang.org/p/RzImKo9Men5

What did you expect to see?

All binaries generated by Go to be statically linked.

What did you see instead?

One binary dynamically linked.

@ianlancetaylor ianlancetaylor changed the title Go 1.12: depending on source, "go build" generates dynamically linked binaries (uses shared libs) cmd/link: Go 1.12: depending on source, "go build" generates dynamically linked binaries (uses shared libs) Feb 26, 2019
@ianlancetaylor
Copy link
Contributor

It's always been true that the Go tool sometimes generates a dynamically linked binary. Is the behavior on your program different than that of 1.11?

In particular, by default, if your program depends on the net or os/user packages, by default you will get a dynamically linked binary. To avoid that, build with -tags "netgo osusergo".

@jeffkayser2
Copy link
Author

Go 1.11 also generates a dynamically linked binary. I thought Go always built statically linked binaries. It's a selling feature (you only have to copy the binary to deploy...) Doing a presentation at Oracle Applications Users Group about Go in April. Will need to add the caveat.

@ianlancetaylor
Copy link
Contributor

The problem with the net and os/user packages is that accessing DNS and LDAP data in the exact same way as the local system is in general a very difficult problem. So by default we fall back to the OS support, while providing the netgo and osusergo tags for people who know what they are doing.

@golang golang locked and limited conversation to collaborators Feb 26, 2020
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