[clang] [WIP] Advance the merger progress of #211963 (PR #216611)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 16 15:24:55 PDT 2026
https://github.com/zhangweize9-cyber created https://github.com/llvm/llvm-project/pull/216611
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.
>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 1/2] [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.
>From c2f5578885f285804359a8050e12be9300ddf49c Mon Sep 17 00:00:00 2001
From: zhangweize9-cyber <zhangweize9 at gmail.com>
Date: Mon, 17 Aug 2026 06:19:33 +0800
Subject: [PATCH 2/2] fix: #211963
---
clang/lib/CodeGen/ItaniumCXXABI.cpp | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index 59374b13abe0f..ddd4b1cf62c2a 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -3133,8 +3133,8 @@ void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
// destructors which we can handle directly in the runtime. Note that this is
// not strictly 1-to-1 with using `atexit` because we no longer tear down
// globals in reverse order of when they were constructed.
- if (!CGM.getLangOpts().hasAtExit() && !D.isStaticLocal())
- return CGF.registerGlobalDtorWithLLVM(D, dtor, addr);
+ // 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,
@@ -3142,10 +3142,11 @@ void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
// 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())
+ if (!CGM.getLangOpts().hasAtExit() && !D.isStaticLocal() &&
+ 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
More information about the cfe-commits
mailing list