mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:27:35 +00:00
LibC+LibELF: Support loading shared libraries compiled with dynamic TLS
This is a prerequisite for upstreaming our LLVM patches, as our current hack forcing `-ftls-model=initial-exec` in the Clang driver is not acceptable upstream. Currently, our kernel-managed TLS implementation limits us to only having a single block of storage for all thread-local variables that's initialized at load time. This PR merely implements the dynamic TLS interface (`__tls_get_addr` and TLSDESC) on top of our static TLS infrastructure. The current model's limitations still stand: - a single static TLS block is reserved at load time, `dlopen()`-ing shared libraries that define thread-local variables might cause us to run out of space. - the initial TLS image is not changeable post-load, so `dlopen()`-ing libraries with non-zero-initialized TLS variables is not supported. The way we repurpose `ti_module` to mean "offset within static TLS block" instead of "module index" is not ABI-compliant.
This commit is contained in:
parent
192ee4594c
commit
ad9e674fa0
6 changed files with 166 additions and 21 deletions
26
Userland/Libraries/LibC/tls.cpp
Normal file
26
Userland/Libraries/LibC/tls.cpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Daniel Bertalan <dani@danielbertalan.dev>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Types.h>
|
||||
#include <sys/internals.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
// This function is called to compute the address of a thread-local variable
|
||||
// which might not be stored in the static TLS block (local-dynamic and
|
||||
// global-dynamic models). Compilers default to this when creating shared
|
||||
// libraries, as they may be loaded after program startup by `dlopen()`.
|
||||
//
|
||||
// We currently only support a static TLS block, so we take a shortcut in the
|
||||
// implementation of this interface: instead of storing the module ID in
|
||||
// ti_module, we store the module's TLS block offset. This avoids the need to
|
||||
// have a per-thread module ID -> TLS block address. This will have to be
|
||||
// changed if we support dynamically allocated TLS blocks.
|
||||
void* __tls_get_addr(__tls_index* index)
|
||||
{
|
||||
return reinterpret_cast<void*>(reinterpret_cast<FlatPtr>(__builtin_thread_pointer()) + index->ti_module + index->ti_offset);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue