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

build: add targets for static linking #17247

Closed
wants to merge 1 commit into from
Closed

Conversation

aerth
Copy link
Contributor

@aerth aerth commented Jul 26, 2018

This adds a new flag to the ci.go install script, -static which produces static linked binary.

Note: this is only tested on linux

Before, the -s flag was only used if the OS was darwin (apple).

Now, use the following linker flags for all builds:

-w
	Omit the DWARF symbol table.
-s
	Omit the symbol table and debug information.

Results:

Smaller binaries, using -static or without.

CURRENT BINARY SIZES (default build)

-rwxr-xr-x 1 user user 9.1M Jul 25 20:27 build/bin/abigen
-rwxr-xr-x 1 user user  26M Jul 25 20:27 build/bin/bootnode
-rwxr-xr-x 1 user user  29M Jul 25 20:27 build/bin/clef
-rwxr-xr-x 1 user user  27M Jul 25 20:27 build/bin/ethkey
-rwxr-xr-x 1 user user  25M Jul 25 20:27 build/bin/evm
-rwxr-xr-x 1 user user  20M Jul 25 20:27 build/bin/examples
-rwxr-xr-x 1 user user  28M Jul 25 20:27 build/bin/faucet
-rwxr-xr-x 1 user user  37M Jul 25 20:27 build/bin/geth
-rwxr-xr-x 1 user user  20M Jul 25 20:27 build/bin/p2psim
-rwxr-xr-x 1 user user  15M Jul 25 20:27 build/bin/puppeth
-rwxr-xr-x 1 user user 3.2M Jul 25 20:27 build/bin/rlpdump
-rwxr-xr-x 1 user user  20M Jul 25 20:27 build/bin/simulations
-rwxr-xr-x 1 user user  37M Jul 25 20:27 build/bin/swarm
-rwxr-xr-x 1 user user 8.1M Jul 25 20:27 build/bin/swarm-smoke
-rwxr-xr-x 1 user user  29M Jul 25 20:27 build/bin/wnode

NEW (default build)

-rwxr-xr-x 1 user user 5.5M Jul 25 20:28 build/bin/abigen
-rwxr-xr-x 1 user user  15M Jul 25 20:28 build/bin/bootnode
-rwxr-xr-x 1 user user  17M Jul 25 20:28 build/bin/clef
-rwxr-xr-x 1 user user  16M Jul 25 20:28 build/bin/ethkey
-rwxr-xr-x 1 user user  15M Jul 25 20:28 build/bin/evm
-rwxr-xr-x 1 user user  12M Jul 25 20:29 build/bin/examples
-rwxr-xr-x 1 user user  16M Jul 25 20:29 build/bin/faucet
-rwxr-xr-x 1 user user  22M Jul 25 20:29 build/bin/geth
-rwxr-xr-x 1 user user  12M Jul 25 20:29 build/bin/p2psim
-rwxr-xr-x 1 user user 8.6M Jul 25 20:29 build/bin/puppeth
-rwxr-xr-x 1 user user 2.0M Jul 25 20:29 build/bin/rlpdump
-rwxr-xr-x 1 user user  12M Jul 25 20:29 build/bin/simulations
-rwxr-xr-x 1 user user  22M Jul 25 20:29 build/bin/swarm
-rwxr-xr-x 1 user user 5.2M Jul 25 20:29 build/bin/swarm-smoke
-rwxr-xr-x 1 user user  17M Jul 25 20:29 build/bin/wnode

STATIC (with -static)

-rwxr-xr-x 1 user user 6.3M Jul 25 20:24 build/bin/abigen
-rwxr-xr-x 1 user user  17M Jul 25 20:24 build/bin/bootnode
-rwxr-xr-x 1 user user  19M Jul 25 20:24 build/bin/clef
-rwxr-xr-x 1 user user  17M Jul 25 20:24 build/bin/ethkey
-rwxr-xr-x 1 user user  16M Jul 25 20:24 build/bin/evm
-rwxr-xr-x 1 user user  13M Jul 25 20:24 build/bin/examples
-rwxr-xr-x 1 user user  17M Jul 25 20:24 build/bin/faucet
-rwxr-xr-x 1 user user  23M Jul 25 20:24 build/bin/geth
-rwxr-xr-x 1 user user  13M Jul 25 20:24 build/bin/p2psim
-rwxr-xr-x 1 user user 9.4M Jul 25 20:24 build/bin/puppeth
-rwxr-xr-x 1 user user 2.0M Jul 25 20:24 build/bin/rlpdump
-rwxr-xr-x 1 user user  13M Jul 25 20:24 build/bin/simulations
-rwxr-xr-x 1 user user  23M Jul 25 20:24 build/bin/swarm
-rwxr-xr-x 1 user user 5.2M Jul 25 20:24 build/bin/swarm-smoke
-rwxr-xr-x 1 user user  18M Jul 25 20:24 build/bin/wnode

@karalabe
Copy link
Member

Go should statically link everything. Curious what is it that your command links in addition that the stock Go compiler didn't.

@aerth
Copy link
Contributor Author

aerth commented Jul 26, 2018

Go version 1.10.3, enter geth repo and make

Output of ldd ./build/bin/geth

linux-vdso.so.1 (0x00007fff33bea000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x000078f10259c000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x000078f102394000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x000078f102090000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x000078f101cf1000)
/lib64/ld-linux-x86-64.so.2 (0x000078f1027b9000)

@karalabe
Copy link
Member

Hmm, interesting. I'll need to discuss this a bit with @fjl too. If we can get libc and the lot included, it will allow out pre-compiled binaries to run on musl too (although we do ship alpine docker images).

@karalabe
Copy link
Member

I've been skimming the internet for forcing static linking from Go, but I found some horror stories too https://tschottdorf.github.io/linking-golang-go-statically-cgo-testing . Are we sure it's worth the trouble?

@aerth
Copy link
Contributor Author

aerth commented Jul 27, 2018

True. After building the static target, the following warning appear: "requires at runtime the shared libraries from the glibc version used for linking"

Which, to me, means it's not "as static as it could be" (??)

Looks like the packages that are causing the warnings is os/user and a getaddrinfo (from net?)

build/env.sh go run build/ci.go install -static ./cmd/geth
>>> $GOROOT/bin/go install -ldflags -X main.gitCommit=316fddfd23ab1d7007cf052c7f7dda0201f08bda -s -w -extldflags -static -v ./cmd/geth
# github.com/ethereum/go-ethereum/cmd/geth
/tmp/go-link-926473070/000002.o: In function `mygetgrouplist':
$GOROOT/src/os/user/getgrouplist_unix.go:15: warning: Using 'getgrouplist' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/tmp/go-link-926473070/000001.o: In function `mygetgrgid_r':
$GOROOT/src/os/user/cgo_lookup_unix.go:38: warning: Using 'getgrgid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/tmp/go-link-926473070/000001.o: In function `mygetgrnam_r':
$GOROOT/src/os/user/cgo_lookup_unix.go:43: warning: Using 'getgrnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/tmp/go-link-926473070/000001.o: In function `mygetpwnam_r':
$GOROOT/src/os/user/cgo_lookup_unix.go:33: warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/tmp/go-link-926473070/000001.o: In function `mygetpwuid_r':
$GOROOT/src/os/user/cgo_lookup_unix.go:28: warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/tmp/go-link-926473070/000012.o: In function `_cgo_f7895c2c5a3a_C2func_getaddrinfo':
/tmp/go-build/cgo-gcc-prolog:46: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
Done building.
Run "/home/user/src/go/src/github.com/ethereum/go-ethereum/build/bin/geth" to launch geth.
ldd build/bin/geth
not a dynamic executable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants