[libc-commits] [libc] [libc] Provide empty definitions for `teardown_main_tls` symbols (PR #173412)

Petr Hosek via libc-commits libc-commits at lists.llvm.org
Tue Dec 23 12:27:36 PST 2025


https://github.com/petrhosek updated https://github.com/llvm/llvm-project/pull/173412

>From 11761ab25d734c641e42324b44b9e2ab1f61362e 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 1/2] [libc] Provide empty definitions for weak symbols

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/atexit.cpp     | 5 ++---
 libc/src/stdlib/exit.cpp       | 5 ++---
 libc/src/stdlib/quick_exit.cpp | 5 ++---
 3 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/libc/src/stdlib/atexit.cpp b/libc/src/stdlib/atexit.cpp
index 799aad136bda5..fb45c3a7ea280 100644
--- a/libc/src/stdlib/atexit.cpp
+++ b/libc/src/stdlib/atexit.cpp
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 constinit ExitCallbackList atexit_callbacks;
 Mutex handler_list_mtx(false, false, false, false);
-[[gnu::weak]] extern void teardown_main_tls();
+[[gnu::weak]] void teardown_main_tls() {}
 
 extern "C" {
 
@@ -27,8 +27,7 @@ int __cxa_atexit(AtExitCallback *callback, void *payload, void *) {
 void __cxa_finalize(void *dso) {
   if (!dso) {
     call_exit_callbacks(atexit_callbacks);
-    if (teardown_main_tls)
-      teardown_main_tls();
+    teardown_main_tls();
   }
 }
 
diff --git a/libc/src/stdlib/exit.cpp b/libc/src/stdlib/exit.cpp
index f26d48ac00d7f..dd5211a504fef 100644
--- a/libc/src/stdlib/exit.cpp
+++ b/libc/src/stdlib/exit.cpp
@@ -20,15 +20,14 @@ extern "C" void __cxa_finalize(void *);
 //       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();
+  __cxa_thread_finalize();
 #endif
   __cxa_finalize(nullptr);
   internal::exit(status);
diff --git a/libc/src/stdlib/quick_exit.cpp b/libc/src/stdlib/quick_exit.cpp
index 29110b33afcf5..181e8a6097cfb 100644
--- a/libc/src/stdlib/quick_exit.cpp
+++ b/libc/src/stdlib/quick_exit.cpp
@@ -16,12 +16,11 @@
 namespace LIBC_NAMESPACE_DECL {
 
 extern ExitCallbackList at_quick_exit_callbacks;
-[[gnu::weak]] extern void teardown_main_tls();
+[[gnu::weak]] void teardown_main_tls() {}
 
 [[noreturn]] LLVM_LIBC_FUNCTION(void, quick_exit, (int status)) {
   call_exit_callbacks(at_quick_exit_callbacks);
-  if (teardown_main_tls)
-    teardown_main_tls();
+  teardown_main_tls();
   internal::exit(status);
 }
 

>From 80da1d94951e7af767569cb2684d1e8c28171f45 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 23 Dec 2025 12:24:45 -0800
Subject: [PATCH 2/2] Exclude __cxa_thread_finalize

---
 libc/src/stdlib/exit.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libc/src/stdlib/exit.cpp b/libc/src/stdlib/exit.cpp
index dd5211a504fef..f26d48ac00d7f 100644
--- a/libc/src/stdlib/exit.cpp
+++ b/libc/src/stdlib/exit.cpp
@@ -20,14 +20,15 @@ extern "C" void __cxa_finalize(void *);
 //       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)
-  __cxa_thread_finalize();
+  if (__cxa_thread_finalize)
+    __cxa_thread_finalize();
 #endif
   __cxa_finalize(nullptr);
   internal::exit(status);



More information about the libc-commits mailing list