Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/auto-sync-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ jobs:
run: |
./.github/scripts/check-version.sh

- name: Install Build Dependencies
if: steps.check_version.outputs.build_needed == 'true'
run: |
sudo apt-get update
sudo apt-get install -y patchelf

- name: Build Standalone Binaries
if: steps.check_version.outputs.build_needed == 'true'
env:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ jobs:
glibc
glibc-runner
resolv-conf
patchelf
exclude-paths: |
.git
./.git
Expand Down Expand Up @@ -219,6 +220,7 @@ jobs:
glibc
glibc-runner
resolv-conf
patchelf
exclude-paths: |
.git
./.git
Expand Down
23 changes: 20 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Environment:
Required if that binary cannot run on this host.

Requirements:
- curl, jq, tar, python3
- curl, jq, tar, python3, patchelf
- native aarch64 Termux clang, or \$ANDROID_NDK_HOME with an aarch64 Android clang
EOF
}
Expand All @@ -66,7 +66,7 @@ trap cleanup EXIT
check_prereqs() {
info "Checking build prerequisites..."
local missing=()
for cmd in curl jq tar python3; do
for cmd in curl jq tar python3 patchelf; do
if ! command -v "$cmd" &>/dev/null; then
missing+=("$cmd")
fi
Expand Down Expand Up @@ -221,12 +221,19 @@ word_rewrites = {
for off in range(lo, hi, 4):
w = get(off)
if w in word_rewrites: put(off, word_rewrites[w])
gvisor_probe_count = 0
for off in range(0, len(data) - 12, 4):
if get(off) == 0xAA1F03E5 and get(off + 4) == 0xAA1F03E6 and get(off + 8) == 0xD28036E0 and (get(off + 12) & 0xFC000000) == 0x94000000:
put(off + 8, 0xD2800600); faccessat2_count += 1
elif get(off) == 0x5281FB60 and get(off + 4) == 0x72A5E000 and (get(off + 8) & 0xFC000000) == 0x94000000:
if (get(off + 12) & 0xFF00001F) == 0x34000000:
put(off + 8, 0x52800000)
else:
put(off + 8, 0x12800000)
gvisor_probe_count += 1
dst.write_bytes(data)
dst.chmod(0o755)
print(f"Patched parameters: ubfx={ubfx_count}, lsl={lsl_count}, mask={mask_count}, mmap={mmap_count}, faccessat2={faccessat2_count}")
print(f"Patched parameters: ubfx={ubfx_count}, lsl={lsl_count}, mask={mask_count}, mmap={mmap_count}, faccessat2={faccessat2_count}, gvisor_probe={gvisor_probe_count}")
PY
ok "Patched binary generated: bin/agy.va39"

Expand All @@ -239,6 +246,16 @@ fi
chmod +x bin/agy
ok "Native Termux bootstrapper compiled successfully."

# Patch ELF interpreter and RPATH on agy.va39 so direct kernel execution preserves
# /proc/self/exe for Go daemonization. Default to canonical Termux PREFIX so CI
# build hosts never bake host-specific paths into release archives.
target_prefix="${AGY_TARGET_PREFIX:-/data/data/com.termux/files/usr}"
info "Patching ELF interpreter and RPATH on bin/agy.va39..."
patchelf --set-interpreter "${target_prefix}/glibc/lib/ld-linux-aarch64.so.1" \
--set-rpath "\$ORIGIN:\$ORIGIN/../glibc/lib:${target_prefix}/glibc/lib" \
bin/agy.va39
ok "ELF patching complete on bin/agy.va39."

if [[ -n "${TERMUX_VERSION:-}" ]]; then
info "Validating built Termux binary with --help..."
if ! bin/agy --help >/dev/null; then
Expand Down
11 changes: 11 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,17 @@ install -m 0755 "$EXTRACT_DIR/agy" "$INSTALL_BIN_DIR/agy" || die "Failed to inst
install -m 0755 "$EXTRACT_DIR/agy.va39" "$INSTALL_BIN_DIR/agy.va39" || die "Failed to install agy.va39 binary to $INSTALL_BIN_DIR"
rm -rf "$EXTRACT_DIR"

DEFAULT_GLIBC_LOADER="/data/data/com.termux/files/usr/glibc/lib/ld-linux-aarch64.so.1"
if [[ "$GLIBC_LOADER" != "$DEFAULT_GLIBC_LOADER" ]]; then
if command -v patchelf >/dev/null 2>&1; then
patchelf --set-interpreter "$GLIBC_LOADER" \
--set-rpath "\$ORIGIN:\$ORIGIN/../glibc/lib:${TERMUX_PREFIX}/glibc/lib" \
"$INSTALL_BIN_DIR/agy.va39" || die "Failed to update ELF interpreter for custom PREFIX"
else
info "Non-default PREFIX detected without patchelf; direct execution will fall back to glibc loader."
fi
fi

# ── Verify twin-binary ────────────────────────────────────────────────────────
if [[ ! -f "$INSTALL_BIN_DIR/agy" || ! -f "$INSTALL_BIN_DIR/agy.va39" ]]; then
rm -f "$INSTALL_BIN_DIR/agy" "$INSTALL_BIN_DIR/agy.va39"
Expand Down
21 changes: 21 additions & 0 deletions lib/agy_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,15 @@ static int perform_transactional_update(const char *dir, const char *latest_tag)
"agy agy.va39 && "
"test -s \"$tmp/agy\" && test -x \"$tmp/agy\" && "
"test -s \"$tmp/agy.va39\" && test -x \"$tmp/agy.va39\" && "
"termux_prefix=\"${PREFIX:-/data/data/com.termux/files/usr}\"; "
"loader_path=\"$termux_prefix/glibc/lib/ld-linux-aarch64.so.1\"; "
"if [ \"$loader_path\" != "
"\"/data/data/com.termux/files/usr/glibc/lib/ld-linux-aarch64.so.1\" ] "
"&& command -v patchelf >/dev/null 2>&1; then "
"patchelf --set-interpreter \"$loader_path\" "
"--set-rpath \"\\$ORIGIN:\\$ORIGIN/../glibc/lib:$termux_prefix/glibc/lib\" "
"\"$tmp/agy.va39\" || exit 1; "
"fi && "
"\"$tmp/agy\" --help >/dev/null 2>&1 && "
"install -m 0755 \"$tmp/agy\" \"$new_agy\" && "
"install -m 0755 \"$tmp/agy.va39\" \"$new_payload\" && "
Expand Down Expand Up @@ -640,6 +649,16 @@ static int resolve_qemu_for_cpu(const char *prefix, char *qemu_path, size_t qemu
return 0;
}

static void try_direct_exec(int has_qemu, char *patched_bin, char **argv) {
if (!has_qemu) {
char *orig_argv0 = argv[0];
argv[0] = patched_bin;
// NOLINTNEXTLINE(clang-analyzer-optin.taint.GenericTaint)
(void)execv(patched_bin, argv);
argv[0] = orig_argv0;
}
}

int main(int argc, char **argv) {
char exec_path[PATH_MAX];
char lib_path[PATH_MAX + 16];
Expand Down Expand Up @@ -734,6 +753,8 @@ int main(int argc, char **argv) {
return 1;
}

try_direct_exec(qemu != NULL, patched_bin, argv);

// We allocate enough space for: qemu + loader + "--library-path" + lib_path
// + patched_bin + user args + NULL
int new_argc = argc + 6;
Expand Down