[clang] [clang][Fuchsia] Have global dtors use llvm.global_dtors over atexit (PR #115788)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 11 15:42:00 PST 2024


https://github.com/PiJoules created https://github.com/llvm/llvm-project/pull/115788

None

>From 809856e87ba89165061cdd42904d99439793f322 Mon Sep 17 00:00:00 2001
From: Leonard Chan <leonardchan at google.com>
Date: Mon, 11 Nov 2024 15:40:47 -0800
Subject: [PATCH] [clang][Fuchsia] Have global dtors use llvm.global_dtors over
 atexit

---
 clang/lib/CodeGen/ItaniumCXXABI.cpp           |  8 ++++++
 clang/test/CodeGenCXX/fuchsia-global-dtor.cpp | 25 +++++++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100644 clang/test/CodeGenCXX/fuchsia-global-dtor.cpp

diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index 9b3c2f1b2af677..7dda27e02e9ea1 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -518,6 +518,14 @@ class FuchsiaCXXABI final : public ItaniumCXXABI {
   explicit FuchsiaCXXABI(CodeGen::CodeGenModule &CGM)
       : ItaniumCXXABI(CGM) {}
 
+  // Rather than using the defaul [__cxa_]atexit, instead use llvm.global_dtors
+  // which will result in .fini_array being used.
+  void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
+                          llvm::FunctionCallee dtor,
+                          llvm::Constant *addr) override {
+    return CGM.AddCXXDtorEntry(dtor, addr);
+  }
+
 private:
   bool constructorsAndDestructorsReturnThis() const override { return true; }
 };
diff --git a/clang/test/CodeGenCXX/fuchsia-global-dtor.cpp b/clang/test/CodeGenCXX/fuchsia-global-dtor.cpp
new file mode 100644
index 00000000000000..ff3066059af91d
--- /dev/null
+++ b/clang/test/CodeGenCXX/fuchsia-global-dtor.cpp
@@ -0,0 +1,25 @@
+/// Global destructors targetting Fuchsia should not use [__cxa_]atexit. Instead
+/// they should be invoked through llvm.global_dtors.
+
+// RUN: %clang_cc1 %s -triple aarch64-unknown-fuchsia -emit-llvm -o - | FileCheck %s
+
+// CHECK-NOT: atexit
+
+// CHECK:      @llvm.global_dtors = appending global [1 x { i32, ptr, ptr }]
+// CHECK-SAME:   [{ i32, ptr, ptr } { i32 {{.*}}, ptr [[MODULE_DTOR:@.*]], ptr {{.*}} }]
+
+// CHECK:      define internal void [[MODULE_DTOR]]() {{.*}}{
+// CHECK-NEXT: entry:
+// CHECK-NEXT:   %0 = call ptr @_ZN1AD1Ev(ptr @DestroyFirst)
+// CHECK-NEXT:   %1 = call ptr @_ZN1AD1Ev(ptr @DestroySecond)
+// CHECK-NEXT:   %2 = call ptr @_ZN1AD1Ev(ptr @DestroyThird)
+// CHECK-NEXT:   ret void
+// CHECK-NEXT: }
+
+struct A {
+  ~A() {}
+};
+
+A DestroyThird;
+A DestroySecond;
+A DestroyFirst;



More information about the cfe-commits mailing list