If the go program compiled by a completely static compiled version of go binary compiler will also be static automatically?

97 views
Skip to first unread message

Hongyi Zhao

unread,
Jan 31, 2021, 8:54:18 AM1/31/21
to golang-nuts
If the go program compiled by a completely static compiled version of go binary compiler will also be static automatically? To be more specific, on Ubuntu 20.04, I've compiled the latest git master version of go compiler in a completely static manner as shown below:

$ which go | xargs file
/home/werner/Public/repo/github.com/golang/go.git/bin/go: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=5jq4NVhOUQBZ9qoSzZZP/ZGpiMeW1vV69o1mL-B9A/6YaUKXNOw58uUviv4EeO/4sUw0l9CinylaK_Pov59, not stripped

Then I try to build the dnsproxy by the following steps:

$ cd dnsproxy.git
$ go build
$ file dnsproxy
dnsproxy: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=6mIriY-h59Wwm2_gwbk2/rXhgRt_o3oIf_KSuKKtt/BYmwRfWtoqym2xPocRBT/Z2CIkEn5HaeVqVhdwxYu, not stripped


As you can see, the compiled dnsproxy with the completely static go compiler binary will also generate a static go program. I'm not sure if this is always true. Any hints will be highly appreciated.

Regards,
HY

Ian Lance Taylor

unread,
Feb 1, 2021, 12:47:13 AM2/1/21
to Hongyi Zhao, golang-nuts
Whether the go tool itself is statically linked does not affect
whether a Go program will be statically linked. However, the setting
of "CGO_ENABLED" that is used when building the Go toolchain will
become the default value of CGO_ENABLED when using that Go toolchain.
That may be what is causing the effect that you are seeing. To build
a dynamically linked program, try setting CGO_ENABLED=1 in the
environment when running the go tool.

Ian

Hongyi Zhao

unread,
Feb 1, 2021, 1:56:00 AM2/1/21
to Ian Lance Taylor, golang-nuts
Thanks a lot. You're right. See the following for more detailed info:

$ CGO_ENABLED=1 go build
werner@X10DAi:~/Public/anti-gfw/dns/dnsproxy/dnsproxy.git$ ldd dnsproxy
linux-vdso.so.1 (0x00007fffcedc8000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0
(0x00007f6e8eab2000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6e8e8c0000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6e8eafd000)

Best
--
Assoc. Prof. Hongyi Zhao <hongy...@gmail.com>
Theory and Simulation of Materials
Hebei Polytechnic University of Science and Technology engineering
NO. 552 North Gangtie Road, Xingtai, China
Reply all
Reply to author
Forward
0 new messages