1 //===-- LowerGlobalDtors.cpp - Lower @llvm.global_dtors -------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// Lower @llvm.global_dtors.
12 /// Implement @llvm.global_dtors by creating wrapper functions that are
13 /// registered in @llvm.global_ctors and which contain a call to
14 /// `__cxa_atexit` to register their destructor functions.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Transforms/Utils/LowerGlobalDtors.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Intrinsics.h"
23 #include "llvm/InitializePasses.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Transforms/Utils.h"
26 #include "llvm/Transforms/Utils/ModuleUtils.h"
31 #define DEBUG_TYPE "lower-global-dtors"
34 class LowerGlobalDtorsLegacyPass final
: public ModulePass
{
35 StringRef
getPassName() const override
{
36 return "Lower @llvm.global_dtors via `__cxa_atexit`";
39 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
41 ModulePass::getAnalysisUsage(AU
);
44 bool runOnModule(Module
&M
) override
;
48 LowerGlobalDtorsLegacyPass() : ModulePass(ID
) {
49 initializeLowerGlobalDtorsLegacyPassPass(*PassRegistry::getPassRegistry());
52 } // End anonymous namespace
54 char LowerGlobalDtorsLegacyPass::ID
= 0;
55 INITIALIZE_PASS(LowerGlobalDtorsLegacyPass
, DEBUG_TYPE
,
56 "Lower @llvm.global_dtors via `__cxa_atexit`", false, false)
58 ModulePass
*llvm::createLowerGlobalDtorsLegacyPass() {
59 return new LowerGlobalDtorsLegacyPass();
62 static bool runImpl(Module
&M
);
63 bool LowerGlobalDtorsLegacyPass::runOnModule(Module
&M
) { return runImpl(M
); }
65 PreservedAnalyses
LowerGlobalDtorsPass::run(Module
&M
,
66 ModuleAnalysisManager
&AM
) {
67 bool Changed
= runImpl(M
);
69 return PreservedAnalyses::all();
72 PA
.preserveSet
<CFGAnalyses
>();
76 static bool runImpl(Module
&M
) {
77 GlobalVariable
*GV
= M
.getGlobalVariable("llvm.global_dtors");
78 if (!GV
|| !GV
->hasInitializer())
81 const ConstantArray
*InitList
= dyn_cast
<ConstantArray
>(GV
->getInitializer());
85 // Validate @llvm.global_dtor's type.
86 auto *ETy
= dyn_cast
<StructType
>(InitList
->getType()->getElementType());
87 if (!ETy
|| ETy
->getNumElements() != 3 ||
88 !ETy
->getTypeAtIndex(0U)->isIntegerTy() ||
89 !ETy
->getTypeAtIndex(1U)->isPointerTy() ||
90 !ETy
->getTypeAtIndex(2U)->isPointerTy())
91 return false; // Not (int, ptr, ptr).
93 // Collect the contents of @llvm.global_dtors, ordered by priority. Within a
94 // priority, sequences of destructors with the same associated object are
95 // recorded so that we can register them as a group.
98 std::vector
<std::pair
<Constant
*, std::vector
<Constant
*>>>
100 for (Value
*O
: InitList
->operands()) {
101 auto *CS
= dyn_cast
<ConstantStruct
>(O
);
103 continue; // Malformed.
105 auto *Priority
= dyn_cast
<ConstantInt
>(CS
->getOperand(0));
107 continue; // Malformed.
108 uint16_t PriorityValue
= Priority
->getLimitedValue(UINT16_MAX
);
110 Constant
*DtorFunc
= CS
->getOperand(1);
111 if (DtorFunc
->isNullValue())
112 break; // Found a null terminator, skip the rest.
114 Constant
*Associated
= CS
->getOperand(2);
115 Associated
= cast
<Constant
>(Associated
->stripPointerCasts());
117 auto &AtThisPriority
= DtorFuncs
[PriorityValue
];
118 if (AtThisPriority
.empty() || AtThisPriority
.back().first
!= Associated
) {
119 std::vector
<Constant
*> NewList
;
120 NewList
.push_back(DtorFunc
);
121 AtThisPriority
.push_back(std::make_pair(Associated
, NewList
));
123 AtThisPriority
.back().second
.push_back(DtorFunc
);
126 if (DtorFuncs
.empty())
129 // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
130 LLVMContext
&C
= M
.getContext();
131 PointerType
*VoidStar
= PointerType::getUnqual(C
);
132 Type
*AtExitFuncArgs
[] = {VoidStar
};
133 FunctionType
*AtExitFuncTy
=
134 FunctionType::get(Type::getVoidTy(C
), AtExitFuncArgs
,
137 FunctionCallee AtExit
= M
.getOrInsertFunction(
139 FunctionType::get(Type::getInt32Ty(C
),
140 {PointerType::get(AtExitFuncTy
, 0), VoidStar
, VoidStar
},
141 /*isVarArg=*/false));
143 // If __cxa_atexit is defined (e.g. in the case of LTO) and arg0 is not
144 // actually used (i.e. it's dummy/stub function as used in emscripten when
145 // the program never exits) we can simply return early and clear out
146 // @llvm.global_dtors.
147 if (auto F
= dyn_cast
<Function
>(AtExit
.getCallee())) {
148 if (F
&& F
->hasExactDefinition() && F
->getArg(0)->getNumUses() == 0) {
149 GV
->eraseFromParent();
154 // Declare __dso_local.
155 Type
*DsoHandleTy
= Type::getInt8Ty(C
);
156 Constant
*DsoHandle
= M
.getOrInsertGlobal("__dso_handle", DsoHandleTy
, [&] {
157 auto *GV
= new GlobalVariable(M
, DsoHandleTy
, /*isConstant=*/true,
158 GlobalVariable::ExternalWeakLinkage
, nullptr,
160 GV
->setVisibility(GlobalVariable::HiddenVisibility
);
164 // For each unique priority level and associated symbol, generate a function
165 // to call all the destructors at that level, and a function to register the
166 // first function with __cxa_atexit.
167 for (auto &PriorityAndMore
: DtorFuncs
) {
168 uint16_t Priority
= PriorityAndMore
.first
;
170 auto &AtThisPriority
= PriorityAndMore
.second
;
171 for (auto &AssociatedAndMore
: AtThisPriority
) {
172 Constant
*Associated
= AssociatedAndMore
.first
;
175 Function
*CallDtors
= Function::Create(
176 AtExitFuncTy
, Function::PrivateLinkage
,
178 (Priority
!= UINT16_MAX
? (Twine(".") + Twine(Priority
))
180 (AtThisPriority
.size() > 1 ? Twine("$") + Twine(ThisId
)
182 (!Associated
->isNullValue() ? (Twine(".") + Associated
->getName())
185 BasicBlock
*BB
= BasicBlock::Create(C
, "body", CallDtors
);
186 FunctionType
*VoidVoid
= FunctionType::get(Type::getVoidTy(C
),
189 for (auto *Dtor
: reverse(AssociatedAndMore
.second
))
190 CallInst::Create(VoidVoid
, Dtor
, "", BB
);
191 ReturnInst::Create(C
, BB
);
193 Function
*RegisterCallDtors
= Function::Create(
194 VoidVoid
, Function::PrivateLinkage
,
195 "register_call_dtors" +
196 (Priority
!= UINT16_MAX
? (Twine(".") + Twine(Priority
))
198 (AtThisPriority
.size() > 1 ? Twine("$") + Twine(ThisId
)
200 (!Associated
->isNullValue() ? (Twine(".") + Associated
->getName())
203 BasicBlock
*EntryBB
= BasicBlock::Create(C
, "entry", RegisterCallDtors
);
204 BasicBlock
*FailBB
= BasicBlock::Create(C
, "fail", RegisterCallDtors
);
205 BasicBlock
*RetBB
= BasicBlock::Create(C
, "return", RegisterCallDtors
);
207 Value
*Null
= ConstantPointerNull::get(VoidStar
);
208 Value
*Args
[] = {CallDtors
, Null
, DsoHandle
};
209 Value
*Res
= CallInst::Create(AtExit
, Args
, "call", EntryBB
);
210 Value
*Cmp
= new ICmpInst(*EntryBB
, ICmpInst::ICMP_NE
, Res
,
211 Constant::getNullValue(Res
->getType()));
212 BranchInst::Create(FailBB
, RetBB
, Cmp
, EntryBB
);
214 // If `__cxa_atexit` hits out-of-memory, trap, so that we don't misbehave.
215 // This should be very rare, because if the process is running out of
216 // memory before main has even started, something is wrong.
217 CallInst::Create(Intrinsic::getDeclaration(&M
, Intrinsic::trap
), "",
219 new UnreachableInst(C
, FailBB
);
221 ReturnInst::Create(C
, RetBB
);
223 // Now register the registration function with @llvm.global_ctors.
224 appendToGlobalCtors(M
, RegisterCallDtors
, Priority
, Associated
);
228 // Now that we've lowered everything, remove @llvm.global_dtors.
229 GV
->eraseFromParent();