[libc-commits] [libc] [libc] Provide empty definitions for weak symbols (PR #173412)
Petr Hosek via libc-commits
libc-commits at lists.llvm.org
Tue Dec 23 11:16:47 PST 2025
https://github.com/petrhosek created https://github.com/llvm/llvm-project/pull/173412
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 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] [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);
}
More information about the libc-commits
mailing list