zig icon indicating copy to clipboard operation
zig copied to clipboard

Android link error when linking PIC compiled static zig library into shared library

Open Beyley opened this issue 1 year ago • 6 comments

Zig Version

0.14.0-dev.2050+4adf63aef

Steps to Reproduce and Observed Behavior

Clone libsce on commit b7e30492a934cb390743fd5e3b286a903caae352

Run zig build publish_libsce -Doptimize=Debug with android NDK 21 installed (if you can't install that specific version, you can override the version using -Dndk_version=XX) All builds will succeed except for the Android builds, which fail in Debug or ReleaseSafe configurations.

This failure is linked to my libc polyfill library, which is a Zig static library I link into the final executable. These polyfills aren't distinctly required on android, only on platforms like Linux where I do not link libc. However, this should still work nonetheless.

This issue seems to have been talked about in the past here, however no dedicated issue for -fPIC not taking effect was ever opened tmk.

publish_libsce
└─ install generated to android-x64/native/libscetool.so
   └─ zig build-lib sce Debug x86_64-linux-android 5 errors
error: ld.lld: relocation R_X86_64_TPOFF32 against debug.panic_stage cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a.o)
    note: referenced by debug.zig:532 (/home/beyley/zig-versions/master/lib/std/debug.zig:532)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a.o:(debug.defaultPanic) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a
error: ld.lld: relocation R_X86_64_TPOFF32 against debug.panic_stage cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a.o)
    note: referenced by debug.zig:534 (/home/beyley/zig-versions/master/lib/std/debug.zig:534)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a.o:(debug.defaultPanic) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a
error: ld.lld: relocation R_X86_64_TPOFF32 against debug.panic_stage cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a.o)
    note: referenced by debug.zig:558 (/home/beyley/zig-versions/master/lib/std/debug.zig:558)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a.o:(debug.defaultPanic) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a
error: ld.lld: relocation R_X86_64_TPOFF32 against Thread.LinuxThreadImpl.tls_thread_id cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a.o)
    note: referenced by Thread.zig:1135 (/home/beyley/zig-versions/master/lib/std/Thread.zig:1135)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a.o:(Thread.LinuxThreadImpl.getCurrentId) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a
error: ld.lld: relocation R_X86_64_TPOFF32 against Thread.LinuxThreadImpl.tls_thread_id cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a.o)
    note: referenced by Thread.zig:1137 (/home/beyley/zig-versions/master/lib/std/Thread.zig:1137)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a.o:(Thread.LinuxThreadImpl.getCurrentId) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a
error: the following command failed with 5 compilation errors:
/home/beyley/zig-versions/master/zig build-lib -fPIC -ODebug -target x86_64-linux-android -mcpu baseline --dep sce --dep npdrm_keys_file --dep system_keys_file -Mroot=/home/beyley/Projects/PROGRAMMING/Zig/libsce/c_abi/lib.zig /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/cb6e4cc1678a992886e6f9c7565c3328/libaes.a -fPIC -ODebug -target x86_64-linux-android -mcpu baseline -I /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/94f55e81fcfbfe5946032a32f12d76e4 --dep aes -Msce=/home/beyley/Projects/PROGRAMMING/Zig/libsce/src/sce.zig -Mnpdrm_keys_file=/home/beyley/Projects/PROGRAMMING/Zig/libsce/keys/npdrm_keys.json -Msystem_keys_file=/home/beyley/Projects/PROGRAMMING/Zig/libsce/keys/system_keys.json /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a /home/beyley/Projects/PROGRAMMING/Zig/libsce/aes/aes.c -fPIC -ODebug -target x86_64-linux-android -mcpu baseline -I /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/384a172e4ae6665afce540aa0d8cf34d -Maes=/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f7183e3693d518e0b0e7c9e703c5fc88/aes.zig /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/358dd0636f9845f5cbf7110f04ca9d42/libc_polyfill.a -lc --libc /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/16441d91b3d28c2160bd514dce793df7/android-21-x86_64.conf --cache-dir /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache --global-cache-dir /home/beyley/.cache/zig --name sce -dynamic --zig-lib-dir /home/beyley/zig-versions/master/lib/ --listen=- 
publish_libsce
└─ install generated to android-arm/native/libscetool.so
   └─ zig build-lib sce Debug arm-linux-androideabi 2 errors
error: ld.lld: relocation R_ARM_TLS_LE32 against debug.panic_stage cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/1990078d0aa0fa5fbaa8b05618b46398/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/1990078d0aa0fa5fbaa8b05618b46398/libc_polyfill.a.o)
    note: referenced by debug.zig:0 (/home/beyley/zig-versions/master/lib/std/debug.zig:0)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/1990078d0aa0fa5fbaa8b05618b46398/libc_polyfill.a.o:(debug.defaultPanic) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/1990078d0aa0fa5fbaa8b05618b46398/libc_polyfill.a
error: ld.lld: relocation R_ARM_TLS_LE32 against Thread.LinuxThreadImpl.tls_thread_id cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/1990078d0aa0fa5fbaa8b05618b46398/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/1990078d0aa0fa5fbaa8b05618b46398/libc_polyfill.a.o)
    note: referenced by Thread.zig:0 (/home/beyley/zig-versions/master/lib/std/Thread.zig:0)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/1990078d0aa0fa5fbaa8b05618b46398/libc_polyfill.a.o:(Thread.LinuxThreadImpl.getCurrentId) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/1990078d0aa0fa5fbaa8b05618b46398/libc_polyfill.a
error: the following command failed with 2 compilation errors:
/home/beyley/zig-versions/master/zig build-lib -fPIC -ODebug -target arm-linux-androideabi -mcpu baseline --dep sce --dep npdrm_keys_file --dep system_keys_file -Mroot=/home/beyley/Projects/PROGRAMMING/Zig/libsce/c_abi/lib.zig /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/ad2fb30fb5cba6ab596e185c8580768b/libaes.a -fPIC -ODebug -target arm-linux-androideabi -mcpu baseline -I /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/94f55e81fcfbfe5946032a32f12d76e4 --dep aes -Msce=/home/beyley/Projects/PROGRAMMING/Zig/libsce/src/sce.zig -Mnpdrm_keys_file=/home/beyley/Projects/PROGRAMMING/Zig/libsce/keys/npdrm_keys.json -Msystem_keys_file=/home/beyley/Projects/PROGRAMMING/Zig/libsce/keys/system_keys.json /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/1990078d0aa0fa5fbaa8b05618b46398/libc_polyfill.a /home/beyley/Projects/PROGRAMMING/Zig/libsce/aes/aes.c -fPIC -ODebug -target arm-linux-androideabi -mcpu baseline -I /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/384a172e4ae6665afce540aa0d8cf34d -Maes=/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/ddeb5ae2c81f903d26c6e9c5f3fc6415/aes.zig /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/1990078d0aa0fa5fbaa8b05618b46398/libc_polyfill.a -lc --libc /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/95d6941bab47abb04caf133e7822c65d/android-21-arm.conf --cache-dir /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache --global-cache-dir /home/beyley/.cache/zig --name sce -dynamic --zig-lib-dir /home/beyley/zig-versions/master/lib/ --listen=- 
publish_libsce
└─ install generated to android-arm64/native/libscetool.so
   └─ zig build-lib sce Debug aarch64-linux-android 10 errors
error: ld.lld: relocation R_AARCH64_TLSLE_ADD_TPREL_HI12 against debug.panic_stage cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o)
    note: referenced by debug.zig:532 (/home/beyley/zig-versions/master/lib/std/debug.zig:532)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o:(debug.defaultPanic) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a
error: ld.lld: relocation R_AARCH64_TLSLE_ADD_TPREL_LO12_NC against debug.panic_stage cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o)
    note: referenced by debug.zig:532 (/home/beyley/zig-versions/master/lib/std/debug.zig:532)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o:(debug.defaultPanic) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a
error: ld.lld: relocation R_AARCH64_TLSLE_ADD_TPREL_HI12 against debug.panic_stage cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o)
    note: referenced by debug.zig:534 (/home/beyley/zig-versions/master/lib/std/debug.zig:534)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o:(debug.defaultPanic) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a
error: ld.lld: relocation R_AARCH64_TLSLE_ADD_TPREL_LO12_NC against debug.panic_stage cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o)
    note: referenced by debug.zig:534 (/home/beyley/zig-versions/master/lib/std/debug.zig:534)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o:(debug.defaultPanic) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a
error: ld.lld: relocation R_AARCH64_TLSLE_ADD_TPREL_HI12 against debug.panic_stage cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o)
    note: referenced by debug.zig:558 (/home/beyley/zig-versions/master/lib/std/debug.zig:558)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o:(debug.defaultPanic) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a
error: ld.lld: relocation R_AARCH64_TLSLE_ADD_TPREL_LO12_NC against debug.panic_stage cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o)
    note: referenced by debug.zig:558 (/home/beyley/zig-versions/master/lib/std/debug.zig:558)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o:(debug.defaultPanic) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a
error: ld.lld: relocation R_AARCH64_TLSLE_ADD_TPREL_HI12 against Thread.LinuxThreadImpl.tls_thread_id cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o)
    note: referenced by Thread.zig:1135 (/home/beyley/zig-versions/master/lib/std/Thread.zig:1135)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o:(Thread.LinuxThreadImpl.getCurrentId) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a
error: ld.lld: relocation R_AARCH64_TLSLE_ADD_TPREL_LO12_NC against Thread.LinuxThreadImpl.tls_thread_id cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o)
    note: referenced by Thread.zig:1135 (/home/beyley/zig-versions/master/lib/std/Thread.zig:1135)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o:(Thread.LinuxThreadImpl.getCurrentId) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a
error: ld.lld: relocation R_AARCH64_TLSLE_ADD_TPREL_HI12 against Thread.LinuxThreadImpl.tls_thread_id cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o)
    note: referenced by Thread.zig:1137 (/home/beyley/zig-versions/master/lib/std/Thread.zig:1137)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o:(Thread.LinuxThreadImpl.getCurrentId) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a
error: ld.lld: relocation R_AARCH64_TLSLE_ADD_TPREL_LO12_NC against Thread.LinuxThreadImpl.tls_thread_id cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o)
    note: referenced by Thread.zig:1137 (/home/beyley/zig-versions/master/lib/std/Thread.zig:1137)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a.o:(Thread.LinuxThreadImpl.getCurrentId) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a
error: the following command failed with 10 compilation errors:
/home/beyley/zig-versions/master/zig build-lib -fPIC -ODebug -target aarch64-linux-android -mcpu baseline --dep sce --dep npdrm_keys_file --dep system_keys_file -Mroot=/home/beyley/Projects/PROGRAMMING/Zig/libsce/c_abi/lib.zig /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/d849cb64abaf4925f858a7198762fe6f/libaes.a -fPIC -ODebug -target aarch64-linux-android -mcpu baseline -I /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/94f55e81fcfbfe5946032a32f12d76e4 --dep aes -Msce=/home/beyley/Projects/PROGRAMMING/Zig/libsce/src/sce.zig -Mnpdrm_keys_file=/home/beyley/Projects/PROGRAMMING/Zig/libsce/keys/npdrm_keys.json -Msystem_keys_file=/home/beyley/Projects/PROGRAMMING/Zig/libsce/keys/system_keys.json /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a /home/beyley/Projects/PROGRAMMING/Zig/libsce/aes/aes.c -fPIC -ODebug -target aarch64-linux-android -mcpu baseline -I /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/384a172e4ae6665afce540aa0d8cf34d -Maes=/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/56e66ac72db8c0ec8eaa3b8190117097/aes.zig /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/f03a4fb9caab59f76553b3d433003cba/libc_polyfill.a -lc --libc /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/cce261f64f78bf9dd95f0de620a32bb7/android-21-aarch64.conf --cache-dir /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache --global-cache-dir /home/beyley/.cache/zig --name sce -dynamic --zig-lib-dir /home/beyley/zig-versions/master/lib/ --listen=- 
Build Summary: 67/74 steps succeeded; 3 failed
publish_libsce transitive failure
├─ install generated to android-x64/native/libscetool.so transitive failure
│  ├─ zig build-lib sce Debug x86_64-linux-android 5 errors
│  └─ zig build-lib sce Debug x86_64-linux-android (+6 more reused dependencies)
├─ install generated to android-arm64/native/libscetool.so transitive failure
│  ├─ zig build-lib sce Debug aarch64-linux-android 10 errors
│  └─ zig build-lib sce Debug aarch64-linux-android (+6 more reused dependencies)
└─ install generated to android-arm/native/libscetool.so transitive failure
   ├─ zig build-lib sce Debug arm-linux-androideabi 2 errors
   └─ zig build-lib sce Debug arm-linux-androideabi (+6 more reused dependencies)

Expected Behavior

Compilation succeeds, as the static zig library is compiled with -fPIC (if you run zig build publish_libsce -Doptimize=Debug --verbose, you'll see that they are compiled with -fPIC, but that doesn't seem to work correctly in this case for some reason.)

Beyley avatar Oct 28 '24 07:10 Beyley

Can you post the output of file libc_polyfill.a and readelf -a libc_polyfill.a?

alexrp avatar Nov 03 '24 12:11 alexrp

Can you post the output of file libc_polyfill.a and readelf -a libc_polyfill.a?

file  /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/e633fa39e0e7bac7436ee4f6454b77a9/libc_polyfill.a
/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/e633fa39e0e7bac7436ee4f6454b77a9/libc_polyfill.a: current ar archive

~/Projects/PROGRAMMING/Zig/libsce git:[master]
file /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/e633fa39e0e7bac7436ee4f6454b77a9/libc_polyfill.a.o
/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/e633fa39e0e7bac7436ee4f6454b77a9/libc_polyfill.a.o: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (SYSV), with debug_info, not stripped

readelf.txt

Beyley avatar Nov 03 '24 12:11 Beyley

Yeah, the relocations in libc_polyfill.a look distinctly non-dynamic. Just to rule out the obvious, have you verified that -fPIC actually gets passed by the build system to the compiler when creating it?

alexrp avatar Nov 03 '24 13:11 alexrp

Here's the full verbose compile log for just android

/home/beyley/zig-versions/master/zig translate-c --listen=- -target arm-linux-androideabi -I /home/beyley/Projects/PROGRAMMING/Zig/libsce/libc /home/beyley/Projects/PROGRAMMING/Zig/libsce/aes/aes.h 
/home/beyley/zig-versions/master/zig build-lib -fPIC -ODebug -target arm-linux-androideabi -mcpu baseline -Mroot=/home/beyley/Projects/PROGRAMMING/Zig/libsce/libc/c.zig --cache-dir /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache --global-cache-dir /home/beyley/.cache/zig --name c_polyfill -static --zig-lib-dir /home/beyley/zig-versions/master/lib/ -fPIE --listen=- 
/home/beyley/zig-versions/master/zig build-lib /home/beyley/Projects/PROGRAMMING/Zig/libsce/aes/aes.c -fPIC -ODebug -target arm-linux-androideabi -mcpu baseline -I /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/0fe08e3b92c6f8274e2ac88e61bd9125 -Mroot=/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/2b451ad63c4fe8e47b404059f53dc8ea/aes.zig --cache-dir /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache --global-cache-dir /home/beyley/.cache/zig --name aes -static --zig-lib-dir /home/beyley/zig-versions/master/lib/ --listen=- 
/home/beyley/zig-versions/master/zig build-lib -fPIC -ODebug -target arm-linux-androideabi -mcpu baseline --dep sce --dep npdrm_keys_file --dep system_keys_file -Mroot=/home/beyley/Projects/PROGRAMMING/Zig/libsce/c_abi/LibSce.zig /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/7cf8e1e18f7ab31292b7e87bbb2f8240/libaes.a -fPIC -ODebug -target arm-linux-androideabi -mcpu baseline -I /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/1a768ca9a0fa17b768d7a5226dd0dfca --dep aes -Msce=/home/beyley/Projects/PROGRAMMING/Zig/libsce/src/sce.zig -Mnpdrm_keys_file=/home/beyley/Projects/PROGRAMMING/Zig/libsce/keys/npdrm_keys.json -Msystem_keys_file=/home/beyley/Projects/PROGRAMMING/Zig/libsce/keys/system_keys.json /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/4ebfc4426d417d8ba6d6dd63549dc56e/libc_polyfill.a /home/beyley/Projects/PROGRAMMING/Zig/libsce/aes/aes.c -fPIC -ODebug -target arm-linux-androideabi -mcpu baseline -I /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/0fe08e3b92c6f8274e2ac88e61bd9125 -Maes=/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/2b451ad63c4fe8e47b404059f53dc8ea/aes.zig /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/4ebfc4426d417d8ba6d6dd63549dc56e/libc_polyfill.a -lc --libc /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/c2d8902c80636c8c782b0f68ff02171f/android-21-arm.conf --cache-dir /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache --global-cache-dir /home/beyley/.cache/zig --name sce -dynamic --zig-lib-dir /home/beyley/zig-versions/master/lib/ --listen=- 
publish_libsce
└─ install generated to android-arm/native/libsce.so
   └─ zig build-lib sce Debug arm-linux-androideabi 2 errors
error: ld.lld: relocation R_ARM_TLS_LE32 against debug.panic_stage cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/4ebfc4426d417d8ba6d6dd63549dc56e/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/4ebfc4426d417d8ba6d6dd63549dc56e/libc_polyfill.a.o)
    note: referenced by debug.zig:0 (/home/beyley/zig-versions/master/lib/std/debug.zig:0)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/4ebfc4426d417d8ba6d6dd63549dc56e/libc_polyfill.a.o:(debug.defaultPanic) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/4ebfc4426d417d8ba6d6dd63549dc56e/libc_polyfill.a
error: ld.lld: relocation R_ARM_TLS_LE32 against Thread.LinuxThreadImpl.tls_thread_id cannot be used with -shared
    note: defined in /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/4ebfc4426d417d8ba6d6dd63549dc56e/libc_polyfill.a(/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/4ebfc4426d417d8ba6d6dd63549dc56e/libc_polyfill.a.o)
    note: referenced by Thread.zig:0 (/home/beyley/zig-versions/master/lib/std/Thread.zig:0)
    note:               /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/4ebfc4426d417d8ba6d6dd63549dc56e/libc_polyfill.a.o:(Thread.LinuxThreadImpl.getCurrentId) in archive /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/4ebfc4426d417d8ba6d6dd63549dc56e/libc_polyfill.a
error: the following command failed with 2 compilation errors:
/home/beyley/zig-versions/master/zig build-lib -fPIC -ODebug -target arm-linux-androideabi -mcpu baseline --dep sce --dep npdrm_keys_file --dep system_keys_file -Mroot=/home/beyley/Projects/PROGRAMMING/Zig/libsce/c_abi/LibSce.zig /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/7cf8e1e18f7ab31292b7e87bbb2f8240/libaes.a -fPIC -ODebug -target arm-linux-androideabi -mcpu baseline -I /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/1a768ca9a0fa17b768d7a5226dd0dfca --dep aes -Msce=/home/beyley/Projects/PROGRAMMING/Zig/libsce/src/sce.zig -Mnpdrm_keys_file=/home/beyley/Projects/PROGRAMMING/Zig/libsce/keys/npdrm_keys.json -Msystem_keys_file=/home/beyley/Projects/PROGRAMMING/Zig/libsce/keys/system_keys.json /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/4ebfc4426d417d8ba6d6dd63549dc56e/libc_polyfill.a /home/beyley/Projects/PROGRAMMING/Zig/libsce/aes/aes.c -fPIC -ODebug -target arm-linux-androideabi -mcpu baseline -I /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/0fe08e3b92c6f8274e2ac88e61bd9125 -Maes=/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/2b451ad63c4fe8e47b404059f53dc8ea/aes.zig /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/4ebfc4426d417d8ba6d6dd63549dc56e/libc_polyfill.a -lc --libc /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/c2d8902c80636c8c782b0f68ff02171f/android-21-arm.conf --cache-dir /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache --global-cache-dir /home/beyley/.cache/zig --name sce -dynamic --zig-lib-dir /home/beyley/zig-versions/master/lib/ --listen=- 
Build Summary: 6/9 steps succeeded; 1 failed
publish_libsce transitive failure
└─ install generated to android-arm/native/libsce.so transitive failure
   ├─ zig build-lib sce Debug arm-linux-androideabi 2 errors
   └─ zig build-lib sce Debug arm-linux-androideabi (+6 more reused dependencies)
error: the following build command failed with exit code 1:
/home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache/o/3c6b75d81db32a6c0e69582dbfcdd0ac/build /home/beyley/zig-versions/master/zig /home/beyley/zig-versions/master/lib /home/beyley/Projects/PROGRAMMING/Zig/libsce /home/beyley/Projects/PROGRAMMING/Zig/libsce/.zig-cache /home/beyley/.cache/zig --seed 0xd4bc69 -Zba59ab5bba25185b publish_libsce -Doptimize=Debug -Dfreebsd_x86_64_sysroot=/usr/x86_64-pc-freebsd13/ -Dhaiku_x86_64_sysroot=/home/beyley/Projects/PROGRAMMING/sh/haiku-cross-compiler/boot/system/ -Dhaiku_x86_64_gcc_dir=/home/beyley/Projects/PROGRAMMING/sh/haiku-cross-compiler/generated/cross-tools-x86_64/lib/gcc/x86_64-unknown-haiku/13.3.0/ --prefix /home/beyley/Projects/PROGRAMMING/C#/LibSceSharp/LibSceSharp.Native/runtimes/ --verbose

Beyley avatar Nov 03 '24 23:11 Beyley

recompiling like mentioned in this other thread https://github.com/ziglang/zig/issues/17575#issuecomment-2600739174 got me unblocked on this same issue

nuxusr avatar Jan 19 '25 07:01 nuxusr

I did some digging into PIC/PIE; the error here actually totally makes sense. When you build a static library for Android, it gets built as PIE by default due to:

https://github.com/ziglang/zig/blob/e28b4027ee273ab72c4eb572adc5abc257230284/src/target.zig#L46-L48 https://github.com/ziglang/zig/blob/e28b4027ee273ab72c4eb572adc5abc257230284/src/Compilation/Config.zig#L417-L435

But you cannot put PIE objects into a shared library! It may happen to work when no thread-local variables are in play, but it's still basically UB.

The important thing here is that Android 5.0+ requires PIE for dynamically-linked executables. However, that does not necessarily mean that we need to build objects or static libraries as PIE. And it certainly does not mean that we should outright block building them as non-PIE. You can link PIC-but-not-PIE objects into a PIE executable just fine.

@Beyley @nuxusr @silbinarywolf can you test this compiler patch in your projects?

diff --git a/src/Compilation/Config.zig b/src/Compilation/Config.zig
index 1251552086..4b1852b6db 100644
--- a/src/Compilation/Config.zig
+++ b/src/Compilation/Config.zig
@@ -422,7 +422,9 @@ pub fn resolve(options: Options) ResolveError!Config {
                 break :b false;
             },
         }
-        if (target_util.requiresPIE(target)) {
+        if (target.os.tag == .windows or
+            (target.abi.isAndroid() and options.output_mode == .Exe))
+        {
             if (options.pie == false) return error.TargetRequiresPie;
             break :b true;
         }

alexrp avatar May 28 '25 15:05 alexrp

I thought i replied but I don't see it. This did the trick after I pulled latest. TY!

nuxusr avatar Jun 06 '25 01:06 nuxusr