[libc-commits] [libc] [libc] Provide empty definitions for __cxa_thread_finalize (PR #174373)
Petr Hosek via libc-commits
libc-commits at lists.llvm.org
Mon Jan 5 01:23:08 PST 2026
https://github.com/petrhosek created https://github.com/llvm/llvm-project/pull/174373
This avoids the GOT slot being generated which is undesirable when using static linking. A better solution would be to reorganize the code to avoid the use of weak symbols altogether.
Fixes #173409
>From 96c6e6e0d6f60ba76d48a79fc1e4fed9eaec47fc Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 23 Dec 2025 11:13:56 -0800
Subject: [PATCH] [libc] Provide empty definitions for __cxa_thread_finalize
This avoids the GOT slot being generated which is undesirable when using
static linking. A better solution would be to reorganize the code to
avoid the use of weak symbols altogether.
Fixes #173409
---
libc/src/stdlib/exit.cpp | 20 ++++++++++++--------
1 file changed, 12 insertions(+), 8 deletions(-)
diff --git a/libc/src/stdlib/exit.cpp b/libc/src/stdlib/exit.cpp
index f26d48ac00d7f..db6a8f1280902 100644
--- a/libc/src/stdlib/exit.cpp
+++ b/libc/src/stdlib/exit.cpp
@@ -15,21 +15,25 @@ namespace LIBC_NAMESPACE_DECL {
extern "C" void __cxa_finalize(void *);
-// exit needs to clean up TLS and call associated destructors.
+// exit() needs to clean up TLS and call associated destructors.
+//
+// The weak no-op implementation is in the same TU with its caller for the case
+// where the real definition is not linked in. This is preferable over weak
+// undefined symbol with a check since that requires a GOT slot.
+//
+// The strong implementation is in libc/src/__support/threads/thread.cpp
+// but not every platform supports threads (e.g. baremetal) in which case the
+// the no-op implementation is sufficient.
+//
// TODO: Strictly speaking, it is not valid to call exit in overlay mode
// as we have no way to ensure system libc will call the TLS destructors.
// We should run exit related tests in hermetic mode but this is currently
// blocked by https://github.com/llvm/llvm-project/issues/133925.
-extern "C" [[gnu::weak]] void __cxa_thread_finalize();
+extern "C" [[gnu::weak]] void __cxa_thread_finalize() {}
// TODO: use recursive mutex to protect this routine.
[[noreturn]] LLVM_LIBC_FUNCTION(void, exit, (int status)) {
-// FIXME: The NVPTX target does not support external weak symbols correctly
-// despite being an ELF platform. Disable pending a future split.
-#if !defined(LIBC_TARGET_ARCH_IS_NVPTX)
- if (__cxa_thread_finalize)
- __cxa_thread_finalize();
-#endif
+ __cxa_thread_finalize();
__cxa_finalize(nullptr);
internal::exit(status);
}
More information about the libc-commits
mailing list