Skip to content

Build emutls.c on Android. #458

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Mar 30, 2022
Merged

Build emutls.c on Android. #458

merged 1 commit into from
Mar 30, 2022

Conversation

pcc
Copy link
Contributor

@pcc pcc commented Mar 23, 2022

Android uses emulated TLS so we need a runtime support function
from this source file.

@pcc
Copy link
Contributor Author

pcc commented Mar 23, 2022

I think this has some problems, I'm getting build failures on 32-bit ARM:

warning: .../android-ndk-r24/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/bits/signal_types.h:64:53: error: use of undeclared identifier 'LONG_BIT'
warning: typedef struct { unsigned long __bits[_KERNEL__NSIG/LONG_BIT]; } sigset64_t;
warning:  

Strange thing is that emutls.c is including limits.h which is where LONG_BIT is supposed to come from...

Android uses emulated TLS so we need a runtime support function
from this source file.
@pcc
Copy link
Contributor Author

pcc commented Mar 24, 2022

The issue was a bug in the NDK headers when compiled in freestanding mode. Sent a fix for the headers and added a workaround here.

@Amanieu Amanieu merged commit 3745594 into rust-lang:master Mar 30, 2022
@Amanieu
Copy link
Member

Amanieu commented Mar 30, 2022

Thanks! I published a new version of compiler-builtins.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 25, 2022
set has_thread_local=true for android

It seems that llvm uses emulated tls on android by default since [this commit](https://reviews.llvm.org/D42999). Which uses a `pthread_key` to emulate various tls objects at runtime([code](https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/emutls.c)).

I've built a demo with a custom android target setting `has_thread_local=true` and a slightly modified `std` as what this PR does, and it works fine. The generated function symbol `__emutls_get_address`  would link to an implementation in `libgcc.a` , which is also [required by std](https://github.com/rust-lang/rust/blob/master/library/unwind/build.rs#L18) for now.

By enable `has_thread_local`, we can reduce the number of `pthread_key`s used by rust libraries on android, which are quite limited resources(128 per process). I've been investgating some crashes caused by unable to create more pthread_key in our project with about 80 `pthread_key`s used by rust part.

cc rust-lang#96145  [rust-lang/compiler-builtins#458](rust-lang/compiler-builtins#458)
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Apr 25, 2022
set has_thread_local=true for android

It seems that llvm uses emulated tls on android by default since [this commit](https://reviews.llvm.org/D42999). Which uses a `pthread_key` to emulate various tls objects at runtime([code](https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/emutls.c)).

I've built a demo with a custom android target setting `has_thread_local=true` and a slightly modified `std` as what this PR does, and it works fine. The generated function symbol `__emutls_get_address`  would link to an implementation in `libgcc.a` , which is also [required by std](https://github.com/rust-lang/rust/blob/master/library/unwind/build.rs#L18) for now.

By enable `has_thread_local`, we can reduce the number of `pthread_key`s used by rust libraries on android, which are quite limited resources(128 per process). I've been investgating some crashes caused by unable to create more pthread_key in our project with about 80 `pthread_key`s used by rust part.

cc rust-lang#96145  [rust-lang/compiler-builtins#458](rust-lang/compiler-builtins#458)
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants