finagolfin / swift-android-sdk

Android SDKs for Swift
Apache License 2.0
138 stars 14 forks source link

Error when running swift-algorithmsPackageTests.xctest: "unknown reloc type 16" #175

Open marcprux opened 1 week ago

marcprux commented 1 week ago

When building and running tests against swift-algorithms on an x86 host+emulator with the Swift 6.0 Android toolchain, I'm seeing the following warning and error:

WARNING: linker: /data/local/tmp/swift-android/swift-algorithms/swift-algorithmsPackageTests.xctest: unsupported flags DT_FLAGS_1=0x8000001
CANNOT LINK EXECUTABLE "/data/local/tmp/swift-android/swift-algorithms/swift-algorithmsPackageTests.xctest": unknown reloc type 16 @ 0x7e9b7e351e48 (16114)
Error: Error terminated(code: 134) running command: /Users/runner/Library/Android/sdk/platform-tools/adb shell /data/local/tmp/swift-android/swift-algorithms/swift-algorithmsPackageTests.xctest

This doesn't occur when running on an arm host+emulator (the tests all run and pass on my machine).

It's especially odd, since this package is passing tests on CI.

Is this something that termux-elf-cleaner might help with? I notice that we are using it in CI, but only for the swift-argument-parser tests and not any other packages.

finagolfin commented 1 week ago

What Android APIs are you running it on in the emulator? List out what APIs you tried for each of arm and x86.

Is this something that termux-elf-cleaner might help with?

No, I tried, it doesn't. This is related to the TLS issue I mentioned earlier.

marcprux commented 6 days ago

This is related to the TLS issue I mentioned earlier.

FTR, the issue mentioned is: https://github.com/finagolfin/swift-android-sdk/pull/170#issuecomment-2351990840

marcprux commented 3 days ago

What Android APIs are you running it on in the emulator? List out what APIs you tried for each of arm and x86.

I haven't tested against arm API variations yet, but on x86, testing swift-algorithms fails against API 25–28, and passes on API 29+ (see https://github.com/marcprux/swift-algorithms/actions/runs/11114132859). This tracks with the note about TLS becoming available in API 29: https://github.com/aosp-mirror/platform_bionic/blob/master/android-changes-for-ndk-developers.md#elf-tls-available-for-api-level--29

finagolfin commented 3 days ago

I haven't tested against arm API variations yet

OK, but you tested it on some AArch64 device or emulator, what API was that? I just wonder if it works before API 29 on AArch64.

This tracks with the note about TLS becoming available in API 29

Yep, this is why I raised the API used on the CI emulator from 24 to 29 with Swift 6, because all the test runners were failing otherwise.

The problem is that a Swift file in Foundation calls that inline _swift_stdlib_gettid() function from the stdlib's synchronization shims that now uses a TLS variable, and that C++ code is presumably compiled by the Swift compiler somehow without specifying an API level, which on Android then defaults to the latest API level, ie with native TLS.

We'll need to find where in the Swift compiler such inline compilation of C++ functions is done and check what it's doing wrong. The full target triple with the API level is passed to the ClangImporter that integrates C/C++ headers, so I'm not sure why this C++ compilation doesn't.

marcprux commented 3 days ago

OK, but you tested it on some AArch64 device or emulator, what API was that? I just wonder if it works before API 29 on AArch64.

It was API 34.