Skip to content

Commit b0ef5c5

Browse files
committed
chore: add local patching script for emulators in nix
1 parent dec8bff commit b0ef5c5

File tree

2 files changed

+143
-2
lines changed

2 files changed

+143
-2
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ src/binaries/firmware/repo
44
src/binaries/trezord-go/repo
55
__pycache__
66
src/binaries/firmware/bin/*
7-
!src/binaries/firmware/bin/download.sh
7+
!src/binaries/firmware/bin/*.sh
88
!src/binaries/firmware/bin/download_latest_gh.py
9-
!src/binaries/firmware/bin/patch-bin.sh
109
!src/binaries/firmware/bin/arm
1110
!src/binaries/trezord-go/bin/download.sh
1211
src/binaries/node-bridge/bin.js
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/bin/bash
2+
#
3+
# Patch emulator binaries for Nix environment
4+
# This script fixes both the interpreter path and rpath for all emulator binaries
5+
# to work with the current Nix environment's library paths.
6+
#
7+
# Usage: Run from trezor-user-env root directory, inside nix-shell
8+
# nix-shell --command 'src/binaries/firmware/bin/patch-emulators-nix.sh'
9+
#
10+
11+
set -e
12+
13+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
14+
BIN_DIR="$SCRIPT_DIR"
15+
16+
echo "========================================="
17+
echo "Patching emulator binaries for Nix"
18+
echo "========================================="
19+
echo ""
20+
21+
# Check we're in nix-shell
22+
if [ -z "$IN_NIX_SHELL" ]; then
23+
echo "ERROR: This script must be run inside nix-shell"
24+
echo "Please run: nix-shell --command '$0'"
25+
exit 1
26+
fi
27+
28+
# Check patchelf is available
29+
if ! command -v patchelf &> /dev/null; then
30+
echo "ERROR: patchelf not found in PATH"
31+
exit 1
32+
fi
33+
34+
# Step 1: Find current library paths from nix environment
35+
echo "Step 1: Finding library paths in current Nix environment..."
36+
37+
# Use ls instead of find for faster results
38+
LIBJPEG_LIB=$(ls /nix/store/*libjpeg*/lib/libjpeg.so.62 2>/dev/null | head -1)
39+
LIBJPEG=$(dirname "$LIBJPEG_LIB" 2>/dev/null || echo "")
40+
41+
SDL2_LIB=$(ls /nix/store/*SDL2-*/lib/libSDL2-2.0.so.0 2>/dev/null | head -1)
42+
SDL2=$(dirname "$SDL2_LIB" 2>/dev/null || echo "")
43+
44+
SDL2_IMG_LIB=$(ls /nix/store/*SDL2*image*/lib/libSDL2_image-2.0.so.0 2>/dev/null | head -1)
45+
SDL2_IMG=$(dirname "$SDL2_IMG_LIB" 2>/dev/null || echo "")
46+
47+
INTERPRETER=$(patchelf --print-interpreter $(which python) 2>/dev/null || echo "")
48+
GLIBC=$(dirname "$INTERPRETER" 2>/dev/null || echo "")
49+
50+
if [ -z "$LIBJPEG" ] || [ -z "$SDL2" ] || [ -z "$SDL2_IMG" ] || [ -z "$GLIBC" ] || [ -z "$INTERPRETER" ]; then
51+
echo "ERROR: Could not find required libraries in Nix store"
52+
echo "LIBJPEG: $LIBJPEG"
53+
echo "SDL2: $SDL2"
54+
echo "SDL2_IMG: $SDL2_IMG"
55+
echo "GLIBC: $GLIBC"
56+
echo "INTERPRETER: $INTERPRETER"
57+
exit 1
58+
fi
59+
60+
echo " ✓ libjpeg: $LIBJPEG"
61+
echo " ✓ SDL2: $SDL2"
62+
echo " ✓ SDL2_image: $SDL2_IMG"
63+
echo " ✓ glibc: $GLIBC"
64+
echo " ✓ interpreter: $INTERPRETER"
65+
echo ""
66+
67+
# Step 2: Build new rpath
68+
NEW_RPATH="$LIBJPEG:$SDL2:$SDL2_IMG:$GLIBC"
69+
70+
# Step 3: Patch all emulator binaries
71+
echo "Step 2: Patching emulator binaries..."
72+
echo ""
73+
74+
COUNT=0
75+
FAILED=0
76+
77+
for binary in "$BIN_DIR"/trezor-emu-*; do
78+
# Skip if not a file or not executable
79+
if [ ! -f "$binary" ] || [ ! -x "$binary" ]; then
80+
continue
81+
fi
82+
83+
BINARY_NAME=$(basename "$binary")
84+
85+
# Check if it's an ELF binary
86+
if ! readelf -h "$binary" &>/dev/null; then
87+
echo " ⊗ Skipping $BINARY_NAME (not an ELF file)"
88+
continue
89+
fi
90+
91+
# Patch interpreter
92+
if ! patchelf --set-interpreter "$INTERPRETER" "$binary" 2>/dev/null; then
93+
echo " ✗ Failed to patch interpreter for $BINARY_NAME"
94+
FAILED=$((FAILED + 1))
95+
continue
96+
fi
97+
98+
# Patch rpath
99+
if ! patchelf --set-rpath "$NEW_RPATH" "$binary" 2>/dev/null; then
100+
echo " ✗ Failed to patch rpath for $BINARY_NAME"
101+
FAILED=$((FAILED + 1))
102+
continue
103+
fi
104+
105+
COUNT=$((COUNT + 1))
106+
107+
# Print progress every 10 files
108+
if [ $((COUNT % 10)) -eq 0 ]; then
109+
echo " Patched $COUNT binaries..."
110+
fi
111+
done
112+
113+
echo ""
114+
echo "========================================="
115+
echo "Summary:"
116+
echo " ✓ Successfully patched: $COUNT binaries"
117+
if [ $FAILED -gt 0 ]; then
118+
echo " ✗ Failed: $FAILED binaries"
119+
fi
120+
echo "========================================="
121+
122+
# Verify one binary works
123+
echo ""
124+
echo "Verification: Checking a sample binary..."
125+
SAMPLE_BINARY="$BIN_DIR/trezor-emu-core-T3T1-v2.9.0"
126+
if [ -f "$SAMPLE_BINARY" ]; then
127+
echo ""
128+
echo "Libraries for $(basename $SAMPLE_BINARY):"
129+
ldd "$SAMPLE_BINARY" 2>&1 | grep -E "(libjpeg|SDL2)" || true
130+
echo ""
131+
echo "Testing if binary runs..."
132+
if "$SAMPLE_BINARY" --help &>/dev/null; then
133+
echo " ✓ Binary runs successfully!"
134+
else
135+
echo " ⚠ Binary may have issues (check manually)"
136+
fi
137+
else
138+
echo " (Sample binary not found, skipping verification)"
139+
fi
140+
141+
echo ""
142+
echo "Done! All emulator binaries have been patched."

0 commit comments

Comments
 (0)