From bad1305dcd6131f663a0435f67a5e454d97fa137 Mon Sep 17 00:00:00 2001 From: purrie Date: Thu, 28 Dec 2023 11:43:17 +0100 Subject: [PATCH] Introduced a separate linking step in the build script to prevent `Relocations in generic ELF (EM: 183)` issue with arm64 compilation. --- Working-for-Android-(on-Linux).md | 37 +++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/Working-for-Android-(on-Linux).md b/Working-for-Android-(on-Linux).md index 2fc6fcd..e78c539 100644 --- a/Working-for-Android-(on-Linux).md +++ b/Working-for-Android-(on-Linux).md @@ -136,14 +136,12 @@ Okay, finally we can build our APK file. You can create a shell script to build # # Compile raylib project for Android # ______________________________________________________________________________ -# -# NOTE: If you excluded any ABIs in the previous steps, remove them from this list too -# TODO: arm64-v8a building doesn't work, ARM64 devices can still run the 32 bit version: -# /usr/bin/ld: /tmp/main-08f12a.o: Relocations in generic ELF (EM: 183) -# /usr/bin/ld: /tmp/main-08f12a.o: Relocations in generic ELF (EM: 183) -# /usr/bin/ld: /tmp/main-08f12a.o: error adding symbols: file in wrong format -ABIS="armeabi-v7a x86 x86_64" +# stop on error and display each command as it gets executed. Optional step but helpful in catching where errors happen if they do. +set -xe + +# NOTE: If you excluded any ABIs in the previous steps, remove them from this list too +ABIS="arm64-v8a armeabi-v7a x86 x86_64" BUILD_TOOLS=android/sdk/build-tools/29.0.3 TOOLCHAIN=android/ndk/toolchains/llvm/prebuilt/linux-x86_64 @@ -173,21 +171,29 @@ for ABI in $ABIS; do case "$ABI" in "armeabi-v7a") CCTYPE="armv7a-linux-androideabi" + ARCH="arm" + LIBPATH="arm-linux-androideabi" ABI_FLAGS="-std=c99 -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16" ;; "arm64-v8a") CCTYPE="aarch64-linux-android" + ARCH="aarch64" + LIBPATH="aarch64-linux-android" ABI_FLAGS="-std=c99 -target aarch64 -mfix-cortex-a53-835769" ;; "x86") CCTYPE="i686-linux-android" + ARCH="i386" + LIBPATH="i686-linux-android" ABI_FLAGS="" ;; "x86_64") CCTYPE="x86_64-linux-android" + ARCH="x86_64" + LIBPATH="x86_64-linux-android" ABI_FLAGS="" ;; esac @@ -202,11 +208,18 @@ for ABI in $ABIS; do $TOOLCHAIN/bin/llvm-ar rcs lib/$ABI/libnative_app_glue.a $NATIVE_APP_GLUE/native_app_glue.o # Compile project - $CC src/*.c -o android/build/lib/$ABI/libmain.so -shared \ - $INCLUDES -I$TOOLCHAIN/sysroot/usr/include/$CCTYPE $FLAGS $ABI_FLAGS \ - -Wl,-soname,libmain.so -Wl,--exclude-libs,libatomic.a -Wl,--build-id \ - -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now \ - -Wl,--warn-shared-textrel -Wl,--fatal-warnings -u ANativeActivity_onCreate \ + for file in src/*.c; do + $CC -c $file -o "$file".o \ + $INCLUDES -I$TOOLCHAIN/sysroot/usr/include/$CCTYPE $FLAGS $ABI_FLAGS + done + + # Link the project with toolchain specific linker to avoid relocations issue. + $TOOLCHAIN/bin/ld.lld src/*.o -o android/build/lib/$ABI/libmain.so -shared \ + --exclude-libs libatomic.a --build-id \ + -z noexecstack -z relro -z now \ + --warn-shared-textrel --fatal-warnings -u ANativeActivity_onCreate \ + -L$TOOLCHAIN/sysroot/usr/lib/$LIBPATH/29 \ + -L$TOOLCHAIN/lib/clang/17/lib/linux/$ARCH \ -L. -Landroid/build/obj -Llib/$ABI \ -lraylib -lnative_app_glue -llog -landroid -lEGL -lGLESv2 -lOpenSLES -latomic -lc -lm -ldl done