Run Linux ELF binary file on Android

Although usual Android apps are installed to your device as APK files, you still have the option to run native ELF binary file compiled for the architecture of your device.

Prerequisites

  • A Linux PC with gcc or other C compiler installed
    • If you are trying to run the compiled ELF file on a device with an ISA that differs from your PC, a cross compiler is required
      • The most common scenario is x86-64 PC + ARM64 (aka. AArch64) Android device, where aarch64-linux-gnu-gcc is needed
  • An Android phone, container or emulator (I am using Waydroid) with Termux installed

Compilation

Take the following "Hello world" program (test.c) as an example:

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    return 0;
}

Use gcc to compile it:

gcc test.c -static -o test

If you intend to run the compiled binary on ARM64 (aka. AArch64) devices and your PC is not ARM64:

aarch64-linux-gnu-gcc test.c -static -o test

-static option is needed to statically link the system C library as Android uses bionic as C library instead of glibc or musl.

Next, copy the compiled ELF file to your Android device.

Copy the compiled ELF file to the appropriate path

As /storage is mounted with option noexec, you can't execute files under the /storage directory even if they have been given executable permission with chmod +x. Therefore, we need to find a directory without noexec mounting option. A common choice is /data/local/tmp.

- $ mount
...
tmpfs on /storage type tmpfs (rw,nosuid,nodev,noexec,relatime,mode=755,gid=1000,inode64)
...

To access /data/local/tmp directory, the root permission is needed for Termux.

su
cp test /data/local/tmp

Execute the file

chmod +x test
./test

See also

Year End Review of 2023

It's almost 2024, so everyone should harvest something in 2023 :)

  • In 2023, I have gone from being a pragmatic open-source software lover to a free software enthusiast. The ROM on my Android phone was switched from MIUI which is the stock firmware on my phone, to the AOSP-based crDroid. I was struggling to use as little proprietary software as possible both on my phone and PC, minimizing my dependence on proprietary network services.

  • In February, I joined the FSF by paying $5 membership fee on a monthly basis. When I discover a nice free software on Github, if the developer has set up Github Sponsor, I will probably financially support him/her (individual FOSS developers will be given $1 or $2 per month).

  • In March, I submitted my archlinuxcn maintainer application to @lilydjwg who is one of the founders of archlinuxcn via email, and became an archlinuxcn maintainer. In the following 9 months, I committed more than 900 times (initially I was unfamiliar with lilac hence made a few mistakes).

  • Due to the poor battery life and hardship of portability with the peripherals of ROG Zephyrus M16, I purchased a chromebook at the end of March. After trying ChromeOS for several hours, I flashed coreboot and installed Arch Linux on it (it's my first experience of an x86 PC with open-source UEFI firmware). Now I take this chromebook with me when I go out, which is very portable weighing 1.3 kg. Instead of turning it off, most of the time I close the lid to make it suspend.

  • In May, I found a Tauri-based and GPL-licensed translation application on Github (namely pot-desktop). Nonetheless, after installing it from AUR, I realized its Wayland support is inferior. Therefore, I gave feedback to the developer, and soon the Wayland support became immaculate. The updated version of pot-desktop added support for OCR and plugin functionality, thus I participated in the development of its plugins.

  • In June, I joined PLCT and worked as an openEuler packaging intern for 3 months, but I found this position was not suitable for me. From my perspective, its init system, sysmaster, is entirely a pointless refactoring product of systemd.

  • In July, I participated in LLUG (Linux Lovers User Group) in Shanghai, and met several friends with whom I get acquainted in the Arch Linux Chinese Community. In a 5-minute lightning talk, I shared my experience from a Linux novice to an archlinuxcn maintainer.

  • In September, I attended the Software Freedom Day hosted by USTC (University of Science and Technology of China) online by means of Jitsi Meet, and delivered a speech centered around FOSS. During my presentation, I shared my experience in the open source community, introduced the notion of free software and recommended notable free software including Mastodon and Matrix. Additionally, I got to meet some free software enthusiasts in USTC.

  • Since the bug I reported to Ark (the archive software of KDE) in September was not fixed in several weeks, I took the initiative to fix it myself and submitted my first merge request to KDE in October. From then on, I have been learning Qt and Kirigami and trying to fix simple bugs in KDE software (not capable of fixing the complex ones at all yet)

  • Overall, I had some regrets but also quite a few gains in 2023. Hankering for a more wonderful 2024 :)

  • Last but not least, happy new year!