Android 64bit binary or JNI library segfaults on entry? lazarus / 43-Android ---------------------------------------------------------------------- Artlav · 2022-11-30 20:54 I was trying to get aarch64 JNI or native android binaries to work, and they crash on entry. Doing the same for arm 32bit and x86 android works just fine, be it hello world or complex app. The very first instruction in system unit FPC_LIB_START_ANDROID, stp x29, x30, [sp, #-16]!, causes a segfault. In case of JNI library, this is what it error log looks like after System.loadLibrary("app"); Code: [Select]11-30 18:58:50.049 11835 11835 F DEBUG   : Revision: '5' 11-30 18:58:50.049 11835 11835 F DEBUG   : ABI: 'arm64' 11-30 18:58:50.049 11835 11835 F DEBUG   : Processor: '7' 11-30 18:58:50.049 11835 11835 F DEBUG   : Timestamp: 2022-11-30 18:58:49.854873590+0100 11-30 18:58:50.049 11835 11835 F DEBUG   : Process uptime: 1s 11-30 18:58:50.049 11835 11835 F DEBUG   : Cmdline: com.pascal.sdi 11-30 18:58:50.049 11835 11835 F DEBUG   : pid: 11799, tid: 11799, name: com.pascal.sdi  >>> com.pascal.sdi <<< 11-30 18:58:50.049 11835 11835 F DEBUG   : uid: 10337 11-30 18:58:50.049 11835 11835 F DEBUG   : tagged_addr_ctrl: 0000000000000001 (PR_TAGGED_ADDR_ENABLE) 11-30 18:58:50.049 11835 11835 F DEBUG   : pac_enabled_keys: 000000000000000f (PR_PAC_APIAKEY, PR_PAC_APIBKEY, PR_PAC_APDAKEY, PR_PAC_APDBKEY) 11-30 18:58:50.049 11835 11835 F DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x000000000000a9d8 11-30 18:58:50.049 11835 11835 F DEBUG   :     x0  0000000000000006  x1  0000007fe23d6e98  x2  0000007fe23d6ed0  x3  0000007fb8c923d0 11-30 18:58:50.049 11835 11835 F DEBUG   :     x4  0000007fb8c92400  x5  0000000001414d4c  x6  0000007fb8c923f6  x7  6c2f34366d72612f 11-30 18:58:50.049 11835 11835 F DEBUG   :     x8  0000007fb9f9d5d8  x9  0000007fb8c92600  x10 000000000000001f  x11 0000000000000016 11-30 18:58:50.049 11835 11835 F DEBUG   :     x12 0000000000000016  x13 0000000000000050  x14 0000000000000000  x15 0000000000000000 11-30 18:58:50.049 11835 11835 F DEBUG   :     x16 0000000000000001  x17 0000007fb9f65080  x18 0000007fb9760000  x19 0000007fb8d04ef8 11-30 18:58:50.049 11835 11835 F DEBUG   :     x20 0000007fb8c92600  x21 0000007c701dd9f0  x22 0000000000000000  x23 0000007fb9e72270 11-30 18:58:50.049 11835 11835 F DEBUG   :     x24 0000007fb9e6d420  x25 0000007fb9f9e000  x26 0000007fb9e72e6c  x27 0000000000000001 11-30 18:58:50.049 11835 11835 F DEBUG   :     x28 000000000000a9d8  x29 0000007fe23d2000 11-30 18:58:50.049 11835 11835 F DEBUG   :     lr  0000007fb9ebab2c  sp  0000007fe23d1fc0  pc  000000000000a9d8  pst 0000000080001800 11-30 18:58:50.049 11835 11835 F DEBUG   : backtrace: 11-30 18:58:50.049 11835 11835 F DEBUG   :       #00 pc 000000000000a9d8  11-30 18:58:50.049 11835 11835 F DEBUG   :       #01 pc 0000000000053b28  /apex/com.android.runtime/bin/linker64 (__dl__ZN6soinfo17call_constructorsEv+752) (BuildId: 3afde223eb00a3453f5a973db57a4d66) 11-30 18:58:50.049 11835 11835 F DEBUG   :       #02 pc 000000000003e0f4  /apex/com.android.runtime/bin/linker64 (__dl__Z9do_dlopenPKciPK17android_dlextinfoPKv+2000) (BuildId: 3afde223eb00a3453f5a973db57a4d66) 11-30 18:58:50.049 11835 11835 F DEBUG   :       #03 pc 00000000000390fc  /apex/com.android.runtime/bin/linker64 (__loader_android_dlopen_ext+84) (BuildId: 3afde223eb00a3453f5a973db57a4d66) 11-30 18:58:50.049 11835 11835 F DEBUG   :       #04 pc 000000000000110c  /apex/com.android.runtime/lib64/bionic/libdl.so (android_dlopen_ext+20) (BuildId: c3485a51b95e45a16eaf027ef6a211fb) 11-30 18:58:50.049 11835 11835 F DEBUG   :       #05 pc 000000000001cedc  /apex/com.android.art/lib64/libnativeloader.so (android::NativeLoaderNamespace::Load(char const*) const+184) (BuildId: 9b40489a2564965276776aae44400621) 11-30 18:58:50.049 11835 11835 F DEBUG   :       #06 pc 000000000000ce20  /apex/com.android.art/lib64/libnativeloader.so (OpenNativeLibraryInNamespace+68) (BuildId: 9b40489a2564965276776aae44400621) 11-30 18:58:50.049 11835 11835 F DEBUG   :       #07 pc 000000000000c564  /apex/com.android.art/lib64/libnativeloader.so (OpenNativeLibrary+132) (BuildId: 9b40489a2564965276776aae44400621) ... 64 more lines of backtrace In case of a native binary, it's just a segmentation fault. Curiously enough, a simple hello world static aarch64 binary built with -Tlinux does work just fine on android, but one built with -Tandroid crashes on entry. Is this a compiler bug, is it not supposed to work at all, or am i doing something wrong? ---------------------------------------------------------------------- Artlav · 2022-12-01 14:42 Ok, this turned out to be a user error. For some unclear reason "--defsym CPU64=1" was not passed to the assembler when system unit was built, and that screwed up prt0.as and dllprt0.as and made .init_array section malformed. Doing a fully clean recompile of everything fixed the issue.