Skip to content

Commit b306385

Browse files
authored
Merge pull request #13 from second-state/static-linking
Enforce fully static linking for Linux binaries
2 parents 2de955c + b39e90a commit b306385

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

.cargo/config.toml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
# Configuration for static builds
2+
#
3+
# Linux: Fully static binaries with musl libc (no runtime dependencies)
4+
# macOS: Cannot be fully static due to Apple restrictions (libSystem always dynamic)
5+
# Windows: Static CRT linking
26

3-
# Linux x86_64 - use musl for fully static binary
7+
# Linux x86_64 - use musl for fully static binary (no libc dependency)
48
[target.x86_64-unknown-linux-musl]
5-
rustflags = ["-C", "target-feature=+crt-static"]
9+
rustflags = ["-C", "target-feature=+crt-static", "-C", "link-self-contained=yes"]
610

7-
# Linux aarch64 - use musl for fully static binary
11+
# Linux aarch64 - use musl for fully static binary (no libc dependency)
812
[target.aarch64-unknown-linux-musl]
9-
rustflags = ["-C", "target-feature=+crt-static"]
13+
rustflags = ["-C", "target-feature=+crt-static", "-C", "link-self-contained=yes"]
1014

11-
# macOS - static linking where possible (system libs still dynamic)
15+
# macOS x86_64 - static where possible (libSystem.dylib always required by Apple)
1216
[target.x86_64-apple-darwin]
1317
rustflags = ["-C", "target-feature=+crt-static"]
1418

19+
# macOS aarch64 - static where possible (libSystem.dylib always required by Apple)
1520
[target.aarch64-apple-darwin]
1621
rustflags = ["-C", "target-feature=+crt-static"]
1722

18-
# Windows - static CRT
23+
# Windows x86_64 - static CRT (MSVCRT statically linked)
1924
[target.x86_64-pc-windows-msvc]
2025
rustflags = ["-C", "target-feature=+crt-static"]

.github/workflows/ci.yml

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,30 @@ jobs:
106106
- name: Verify static linking (Linux)
107107
if: matrix.use_cross
108108
run: |
109+
echo "=== Checking binary type ==="
109110
file target/${{ matrix.target }}/release/create-wallet
110-
# Verify it's statically linked (should show "statically linked")
111-
file target/${{ matrix.target }}/release/create-wallet | grep -i static || echo "Warning: May not be fully static"
111+
112+
echo "=== Verifying static linking ==="
113+
# The binary should be "statically linked" or "static-pie linked" - fail if not
114+
file target/${{ matrix.target }}/release/create-wallet | grep -qE "(statically linked|static-pie linked)" || {
115+
echo "ERROR: Binary is not statically linked!"
116+
exit 1
117+
}
118+
119+
echo "=== Checking for dynamic dependencies ==="
120+
# ldd should show "statically linked", "not a dynamic executable", or fail for cross-compiled binaries
121+
LDD_OUTPUT=$(ldd target/${{ matrix.target }}/release/create-wallet 2>&1) || true
122+
if echo "$LDD_OUTPUT" | grep -qE "(statically linked|not a dynamic executable)"; then
123+
echo "SUCCESS: No dynamic dependencies"
124+
echo "$LDD_OUTPUT"
125+
elif echo "$LDD_OUTPUT" | grep -qE "(cannot execute|wrong ELF class|cannot open)"; then
126+
echo "SUCCESS: Cross-compiled binary (ldd cannot analyze, but file confirmed static linking)"
127+
echo "$LDD_OUTPUT"
128+
else
129+
echo "ERROR: Binary has dynamic dependencies:"
130+
echo "$LDD_OUTPUT"
131+
exit 1
132+
fi
112133
113134
- name: Upload artifact
114135
uses: actions/upload-artifact@v4
@@ -240,8 +261,29 @@ jobs:
240261
- name: Verify static linking (Linux)
241262
if: matrix.os == 'ubuntu-latest'
242263
run: |
264+
echo "=== Checking binary type ==="
243265
file ./create-wallet
244-
ldd ./create-wallet 2>&1 || echo "Binary is statically linked (no dynamic dependencies)"
266+
267+
echo "=== Verifying no libc dependency ==="
268+
# The binary should be "statically linked" or "static-pie linked"
269+
file ./create-wallet | grep -qE "(statically linked|static-pie linked)" || {
270+
echo "ERROR: Binary is not statically linked!"
271+
file ./create-wallet
272+
exit 1
273+
}
274+
275+
# Verify ldd shows no dynamic dependencies
276+
LDD_OUTPUT=$(ldd ./create-wallet 2>&1)
277+
if echo "$LDD_OUTPUT" | grep -qE "(statically linked|not a dynamic executable)"; then
278+
echo "SUCCESS: Binary has no dynamic dependencies (including libc)"
279+
echo "$LDD_OUTPUT"
280+
else
281+
echo "ERROR: Binary has dynamic dependencies:"
282+
echo "$LDD_OUTPUT"
283+
exit 1
284+
fi
285+
286+
echo "=== All binaries verified as fully static ==="
245287
246288
- name: Extract and verify (Windows)
247289
if: matrix.os == 'windows-latest'

0 commit comments

Comments
 (0)