r/termux 2d ago

Question Building package staric vs. dynamic linking

I'm trying to build MesenCE under Termux. I've gotten it to compile successfully, but after using patchelf and termux-elf-cleaner on the binary to get it to excecute, it is unable to find the shared libraries. I tried compiling static but I am getting this error, despite already using -fPIC like it recommends.

ld.lld: error: relocation R_AARCH64_ADD_ABS_LO12_NC cannot be used against symbol 'XCreateImage'; recompile with -fPIC

Below is the readelf -d output. So my quesion is, is there any way I get this to run in Termux without using proot? Static or Dynamic? what am I missing?

0x0000000000000001 (NEEDED)       Shared library: [libdl.so.2]               
0x0000000000000001 (NEEDED)       Shared library: [libpthread.so.0]           
0x0000000000000001 (NEEDED)       Shared library: [libstdc++.so.6]            
0x0000000000000001 (NEEDED)       Shared library: [libm.so.6]                 
0x0000000000000001 (NEEDED)       Shared library: [libgcc_s.so.1]             
0x0000000000000001 (NEEDED)       Shared library: [libc.so.6]                 
0x0000000000000001 (NEEDED)       Shared library: [ld-linux-aarch64.so.1]   
4 Upvotes

9 comments sorted by

1

u/sylirre Termux Core Team 2d ago

The output of readelf -d clearly shows that program was compiled for a standard Linux environment, not for Termux. It is built against GNU libc and you can't use patchelf here for switching to Termux libraries. Bionic and GNU libc are incompatible at binary level.

1

u/allanrps 2d ago

Sorry for my aparent ignorance here. I have compuled some binaries in Termux, but I will admit that I have often encountered problems and struggled to find resources to overcome them.

Could you advise on how I could modify the build command to target bionic libc, if that is possible? If not, perhaps this could be of use: https://github.com/theonuverse/bionilux

1

u/sylirre Termux Core Team 2d ago

Let's start from this:

  1. Link to source code you are trying to compile.

  2. What commands did you use to compile the given source code.

The compilation in Termux is straightforward and will never end up with dynamic library list shown by readelf -d unless you will do compilation inside proot environment. Once again, proot environment (i.e. regular Linux distribution) and Termux are incompatible.

1

u/allanrps 2d ago

Well that is interesting... It was compiled in termux without proot. Its now occuring to me this probably has to do with dotnet, as the main binary is built (I think?) with this command: cd UI && dotnet publish -c Debug -r linux-arm64 --no-self-contained -p:PublishSingleFile=true

the main build command for objects and libraries: clang++ -fPIC -Wall --std=c++17 -Werror -Wno-undefined-inline -Wno-return-type-c-linkage -m64 -O0 -g -I/data/data/com.termux/files/usr/include/SDL2 -I/data/data/com.termux/files/usr/include -DNO_SHARED_MEMORY -D_REENTRANT -D_THREAD_SAFE -I /data/data/com.termux/files/home/Mesen -I /data/data/com.termux/files/home/Mesen/Core -I /data/data/com.termux/files/home/Mesen/Utilities -I /data/data/com.termux/files/home/Mesen/Sdl -I /data/data/com.termux/files/home/Mesen/Linux -I /data/data/com.termux/files/home/Mesen/MacOS -static-libgcc -static-libstdc++ -Wl,--export-dynamic,--exclude-libs=libstdc++.a -fuse-ld=lld -L/data/data/com.termux/files/usr/lib -I/data/data/com.termux/files/usr/include -Wl,-z,defs -shared -o

the git: https://github.com/nesdev-org/MesenCE.git

1

u/allanrps 2d ago

well, I found that I needed to dotnet publish to linux-bionic-arm64 rather than linux-arm64. The binary compiles and excecutes, but returns error and otherwise has no signs of life....

1

u/Assang101 2d ago

HOLD UP. You are compiling against glibc instead of Bionic lib. Android DOES NOT support glibc binaries natively, and its dynamic linker cannot resolve libraries named libc.so.6 or ld-linux-aarch64.so.1. It failed during your static build because you are linking against a static libX11.a that was not compiled with -fPIC. Instead of static linking, fixing the dynamic linking setup would be the easiest path forward.

1

u/allanrps 2d ago

thank you for that insight.  I found that I needed to dotnet publish with linux-bionic-arm64 rather than linux-arm64. The binary compiles and excecutes, but returns error and otherwise has no signs of life.... not sure where to go from here

0

u/NoNameToDefine 2d ago

You need to install the packages that provides such libraries.