[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:31 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (PiJoules)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/115788.diff


2 Files Affected:

- (modified) clang/lib/CodeGen/ItaniumCXXABI.cpp (+8) 
- (added) clang/test/CodeGenCXX/fuchsia-global-dtor.cpp (+25) 


``````````diff
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;

``````````

</details>


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


More information about the cfe-commits mailing list