Skip to content

Commit 809856e

Browse files
committed
[clang][Fuchsia] Have global dtors use llvm.global_dtors over atexit
1 parent 375bb38 commit 809856e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

clang/lib/CodeGen/ItaniumCXXABI.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,14 @@ class FuchsiaCXXABI final : public ItaniumCXXABI {
518518
explicit FuchsiaCXXABI(CodeGen::CodeGenModule &CGM)
519519
: ItaniumCXXABI(CGM) {}
520520

521+
// Rather than using the defaul [__cxa_]atexit, instead use llvm.global_dtors
522+
// which will result in .fini_array being used.
523+
void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
524+
llvm::FunctionCallee dtor,
525+
llvm::Constant *addr) override {
526+
return CGM.AddCXXDtorEntry(dtor, addr);
527+
}
528+
521529
private:
522530
bool constructorsAndDestructorsReturnThis() const override { return true; }
523531
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// Global destructors targetting Fuchsia should not use [__cxa_]atexit. Instead
2+
/// they should be invoked through llvm.global_dtors.
3+
4+
// RUN: %clang_cc1 %s -triple aarch64-unknown-fuchsia -emit-llvm -o - | FileCheck %s
5+
6+
// CHECK-NOT: atexit
7+
8+
// CHECK: @llvm.global_dtors = appending global [1 x { i32, ptr, ptr }]
9+
// CHECK-SAME: [{ i32, ptr, ptr } { i32 {{.*}}, ptr [[MODULE_DTOR:@.*]], ptr {{.*}} }]
10+
11+
// CHECK: define internal void [[MODULE_DTOR]]() {{.*}}{
12+
// CHECK-NEXT: entry:
13+
// CHECK-NEXT: %0 = call ptr @_ZN1AD1Ev(ptr @DestroyFirst)
14+
// CHECK-NEXT: %1 = call ptr @_ZN1AD1Ev(ptr @DestroySecond)
15+
// CHECK-NEXT: %2 = call ptr @_ZN1AD1Ev(ptr @DestroyThird)
16+
// CHECK-NEXT: ret void
17+
// CHECK-NEXT: }
18+
19+
struct A {
20+
~A() {}
21+
};
22+
23+
A DestroyThird;
24+
A DestroySecond;
25+
A DestroyFirst;

0 commit comments

Comments
 (0)