[clang] [Clang] Ensure that .fini_array is available with -fno-use-cxa-atexit (PR #211963)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jul 25 06:22:00 PDT 2026
https://github.com/pyxherb updated https://github.com/llvm/llvm-project/pull/211963
>From 70c8c59840424b3d10aac1b78cc57db1fd7ab1d3 Mon Sep 17 00:00:00 2001
From: pyxherb <codesbuilder at 163.com>
Date: Sat, 25 Jul 2026 07:29:53 +0800
Subject: [PATCH] [Clang] Ensure that .fini_array is also available while
__cxa_atexit() and atexit() are available This conservative PR fixes the
inconsistency of behaviors of '-fno-use-cxa-atexit' to GCC, which blocks user
to use .fini_array instead of __cxa_atexit() and atexit() on ELF-based
platforms. This PR currently fixes this bug on the ELF-based platforms only,
but perhaps also applicable on COFF.
---
clang/lib/CodeGen/ItaniumCXXABI.cpp | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index 5c5fefe32c06c..59374b13abe0f 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -3136,6 +3136,18 @@ void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
if (!CGM.getLangOpts().hasAtExit() && !D.isStaticLocal())
return CGF.registerGlobalDtorWithLLVM(D, dtor, addr);
+ // If '-fno-use-cxa-atexit' and '-fno-register-global-dtors-with-atexit' are also sepcified,
+ // this means the user does not want any invocation to __cxa_atexit or atexit,
+ // but we still have to check if the declaration is static-local or thread-local,
+ // which should not be supported by this case.
+ // As we investigated, ELF has its .fini_array section to support the global destructors.
+ // So we can safely apply this to ELF.
+ if (CGM.getTarget().getTriple().isOSBinFormatELF() &&
+ !CGM.getCodeGenOpts().CXAAtExit &&
+ !CGM.getCodeGenOpts().RegisterGlobalDtorsWithAtExit &&
+ !D.isStaticLocal() && !D.getTLSKind())
+ return CGF.registerGlobalDtorWithLLVM(D, dtor, addr);
+
// emitGlobalDtorWithCXAAtExit will emit a call to either __cxa_thread_atexit
// or __cxa_atexit depending on whether this VarDecl is a thread-local storage
// or not. CXAAtExit controls only __cxa_atexit, so use it if it is enabled.
More information about the cfe-commits
mailing list