diff --git a/options/rtdl/generic/linker.cpp b/options/rtdl/generic/linker.cpp index 973cfbbb52..8fefa701e0 100644 --- a/options/rtdl/generic/linker.cpp +++ b/options/rtdl/generic/linker.cpp @@ -885,7 +885,7 @@ Tcb *allocateTcb() { // To make sure that both the TCB and TLS data are sufficiently aligned, allocate // slightly more than necessary and adjust alignment afterwards. - size_t alignOverhead = frg::min(alignof(Tcb), tlsMaxAlignment); + size_t alignOverhead = frg::max(alignof(Tcb), tlsMaxAlignment); size_t allocSize = tlsInitialSize + sizeof(Tcb) + alignOverhead; auto allocation = reinterpret_cast(getAllocator().allocate(allocSize)); memset(reinterpret_cast(allocation), 0, allocSize); diff --git a/tests/rtdl/meson.build b/tests/rtdl/meson.build index a0c6d7386f..c7b667666f 100644 --- a/tests/rtdl/meson.build +++ b/tests/rtdl/meson.build @@ -10,6 +10,7 @@ rtdl_test_cases = [ 'scope3', 'scope4', 'scope5', + 'tls_align', ] foreach test_name : rtdl_test_cases diff --git a/tests/rtdl/tls_align/libbar.c b/tests/rtdl/tls_align/libbar.c new file mode 100644 index 0000000000..3f8d6a7e1b --- /dev/null +++ b/tests/rtdl/tls_align/libbar.c @@ -0,0 +1 @@ +_Thread_local __attribute__((aligned(8))) char bar_thread_local[8] = "Hello!"; diff --git a/tests/rtdl/tls_align/libfoo.c b/tests/rtdl/tls_align/libfoo.c new file mode 100644 index 0000000000..8d981770a5 --- /dev/null +++ b/tests/rtdl/tls_align/libfoo.c @@ -0,0 +1 @@ +_Thread_local __attribute__((aligned(16))) char foo_thread_local[8] = "Hello!"; diff --git a/tests/rtdl/tls_align/meson.build b/tests/rtdl/tls_align/meson.build new file mode 100644 index 0000000000..61d0fd9250 --- /dev/null +++ b/tests/rtdl/tls_align/meson.build @@ -0,0 +1,7 @@ +libfoo = shared_library('foo', 'libfoo.c') +libbar = shared_library('bar', 'libbar.c') +test_link_with = [libfoo, libbar] + +libfoo_native = shared_library('native-foo', 'libfoo.c', native: true) +libbar_native = shared_library('native-bar', 'libbar.c', native: true) +test_native_link_with = [libfoo_native, libbar_native] diff --git a/tests/rtdl/tls_align/test.c b/tests/rtdl/tls_align/test.c new file mode 100644 index 0000000000..438183195b --- /dev/null +++ b/tests/rtdl/tls_align/test.c @@ -0,0 +1,10 @@ +#include +#include + +extern _Thread_local char foo_thread_local[]; +extern _Thread_local char bar_thread_local[]; + +int main() { + assert(!((uintptr_t)foo_thread_local & (16 - 1))); + assert(!((uintptr_t)bar_thread_local & (8 - 1))); +}