Skip to content

Commit 653a4d1

Browse files
committed
termux: bake Termux lib dir into DT_RUNPATH so dlopen finds openssl
1 parent aead515 commit 653a4d1

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎.github/workflows/termux.yml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ jobs:
4343
-o:3code-android src/threecode.nim
4444
file 3code-android
4545
46+
- name: Verify Termux RUNPATH
47+
# Nim's openssl wrapper dlopens libssl.so.3/libcrypto.so.3 at
48+
# module init; Android's linker only finds them via the binary's
49+
# DT_RUNPATH (config.nims bakes in the Termux prefix). The android
50+
# binary can't run on this x86 host, so guard the runpath here;
51+
# without it the binary dies on startup with "could not import:
52+
# SSL_CTX_ctrl".
53+
run: |
54+
readelf -d 3code-android | grep -q 'RUNPATH.*\[/data/data/com.termux/files/usr/lib\]'
55+
4656
- name: Package
4757
run: |
4858
mkdir -p 3code-termux-arm64

‎config.nims‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ switch("path", "tests") # test helpers (tty_expect, stub_helpers, minline_testu
2727
switch("d", "ssl")
2828
switch("d", "testPlainHttp")
2929

30+
when defined(android):
31+
# Termux: Nim's openssl wrapper dlopens libssl.so.3/libcrypto.so.3 at
32+
# module init, but Android's linker only searches the system lib dirs
33+
# and the binary's own DT_RUNPATH for dlopen'd libs, never Termux's
34+
# $PREFIX/lib (Termux packages get a runpath from their clang; the
35+
# NDK cross toolchain doesn't add one). Bake the Termux lib dir in as
36+
# RUNPATH so dlopen finds the openssl package's libs. The path is a
37+
# link-time constant; the linker doesn't check it exists.
38+
switch("passL", "-Wl,-rpath,/data/data/com.termux/files/usr/lib")
39+
3040
switch("d", "version=" & getVersionString())
3141

3242
when withDir(thisDir(), system.fileExists("config.local.nims")):

‎src/threecode.nim‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ proc setupTlsEnv() =
152152
## opens a TLS context calls `bundledCaFile()` (in util.nim) to feed
153153
## the bundled `cacert.pem` directly to `newContext(caFile = ...)`.
154154
## Linux uses the system trust store and needs nothing here.
155+
## Termux's openssl libs are found via DT_RUNPATH baked in at link
156+
## time (see config.nims); LD_LIBRARY_PATH can't help because the
157+
## openssl wrapper dlopens at module init, before main runs.
155158
when defined(macosx):
156159
let dir = parentDir(getAppFilename())
157160
let cur = getEnv("DYLD_LIBRARY_PATH")

0 commit comments

Comments
 (0)