What is the difference between statically and dynamically linked binaries?
Static and dynamic linking are two ways of attaching software libraries to an executable program.
A statically linked binary includes all necessary libraries within the binary executable file itself. This means that when the program is executed, it does not rely on any external libraries, as all required libraries are already included in the binary. Static linking produces larger binary files, but they can be more portable and self-contained.
A dynamically linked binary, on the other hand, links to external libraries at runtime, rather than including them within the binary itself. When a dynamically linked binary is executed, it relies on shared libraries installed on the system to provide necessary functionality. This can result in smaller binary files, but requires the system to have the necessary shared libraries installed to run the program.
Which one you pick depends on what the program needs and where it will run.
When to use statically linked libraries?
- Command-line tools: Many command-line tools, such as file compression utilities or network monitoring tools, can be distributed as statically linked binaries. Users download the tool and run it without installing additional dependencies.
- Embedded systems: When developing software for embedded systems, statically linking the necessary libraries helps the program run correctly on the device, because the target environment may have limited resources or lack the required libraries.
- Cross-platform software: Programs that need to run on multiple platforms, such as games or productivity software, may be distributed as statically linked binaries so they work the same across different operating systems and architectures.
- Security-focused software: Security-focused software, such as encryption tools or password managers, is sometimes distributed as statically linked binaries, because it cannot be tricked into loading a manipulated shared library at runtime (for example via
LD_PRELOAD). This comes with a trade-off though: a statically linked library does not receive the security patches your Linux distribution ships. If a vulnerability is found in an embedded library, the binary has to be rebuilt and redeployed. Security scanners also have a harder time finding vulnerable library versions inside a static binary. Static linking moves the responsibility for patching from the operating system to you.
Check if a binary has been statically or dynamically linked
We have a golang cli tool called swag and want to integrate it into our CI pipeline, so it would be helpful to statically link the binary.
Lets see, what the go compiler produces with default parameters.
The -o flag just tells the compiler how to name the output file, and the path points to the main.go file.
go build -o swag cmd/swag/main.go
We can now use the file command to check the binary.
file swag
The command outputs:
swag: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, Go BuildID=gfj1RxRs_HuaewolHit9/HmUS_HdD_89W_XqAu3ee/wf7S14q1IwrJ5uuIdWJA/pmDUv61yXYXhrSKw7FtO, with debug_info, not stripped
The binary has been dynamically linked. The reason is cgo: packages like net or os/user use the C library of the system when cgo is enabled, which is the default on Linux if a C compiler is installed.
Now we build the same Go program without cgo, which should result in a statically linked binary.
Linux / macOS
CGO_ENABLED=0 go build -o swag-static cmd/swag/main.go
Windows (PowerShell)
# Cross-compile a Linux binary; file and ldd only work on Linux ELF binaries
$env:CGO_ENABLED = "0"
$env:GOOS = "linux"
go build -o swag-static cmd/swag/main.go
Let us now check if we have a statically linked binary.
file swag-static
That command produces this output:
swag-static: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=7mPpucO9Ncafg5_JKWjE/DZbvRKfuHP9vWS79aEaM/3KcHlAJg9L4fvQnb8AL9/FxPS7iMjUAmw_Pc4KTaM, with debug_info, not stripped
Now the binary is statically linked.
Alternatives to the file command
ldd
ldd prints the shared libraries a binary needs at runtime:
ldd swag
For the dynamically linked binary, it lists the libraries, e.g. libc.so.6 and the dynamic loader /lib64/ld-linux-x86-64.so.2. For the statically linked binary, it reports that there is nothing to load:
ldd swag-static
not a dynamic executable
Never run ldd on binaries you do not trust: depending on the implementation, it may execute the binary. file only reads it.
go version -m
If you only have the binary, but want to know how it has been built, Go itself can tell you. go version -m prints the build information embedded in every Go binary, including the CGO_ENABLED setting. This works on every operating system, also for binaries built for another platform:
Linux / macOS
go version -m swag-static | grep CGO_ENABLED
Windows (PowerShell)
go version -m swag-static | Select-String CGO_ENABLED
build CGO_ENABLED=0
CGO_ENABLED=0 means the binary does not link against the C library and is statically linked (unless special linker flags were used).
A note on Windows
On Windows, the question is less relevant: Go binaries never depend on a C runtime there, they only load system DLLs like kernel32.dll, which are part of every Windows installation. If you want to inspect the DLLs a Windows executable imports, use dumpbin /dependents myapp.exe from the Visual Studio developer tools.
Conclusion
The file command can be used to find out if a binary has been statically or dynamically linked. ldd and go version -m are useful alternatives.
Static linking matters most when you ship inside a container. A statically linked Go binary can run in a FROM scratch or distroless Docker image with zero OS dependencies, which gives you a very small image with almost nothing an attacker could use. Just remember to rebuild it regularly, so it picks up fixes in Go and your dependencies. If you are interested in containerising your Go applications, check out Containerization and Go: A Powerful Combination.
