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
  • 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

-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

A Quote by Edward Snowden

Ultimately, saying that you don’t care about privacy because you have nothing to hide is no different from saying you don’t care about freedom of speech because you have nothing to say. Or that you don’t care about freedom of the press because you don’t like to read. Or that you don’t care about freedom of religion because you don’t believe in God. Or that you don’t care about the freedom to peaceably assemble because you’re a lazy, antisocial agoraphobe. Just because this or that freedom might not have meaning to you today doesn’t mean that that it doesn’t or won’t have meaning tomorrow, to you, or to your neighbor – or to the crowds of principled dissidents I was following on my phone who were protesting halfway across the planet, hoping to gain just a fraction of the freedom that my country was busily dismantling.

Permanent Record by Edward Snowden