llvm.org GIT mirror llvm / 18314dc
Add runStaticConstructorsDestructors which runs ctors / dtors of a single module. Patch by David Chisnall. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@56849 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 11 years ago
2 changed file(s) with 46 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
156156 const std::vector &ArgValues) = 0;
157157
158158 /// runStaticConstructorsDestructors - This method is used to execute all of
159 /// the static constructors or destructors for a program, depending on the
160 /// value of isDtors.
161 void runStaticConstructorsDestructors(bool isDtors);
162 /// runStaticConstructorsDestructors - This method is used to execute all of
159163 /// the static constructors or destructors for a module, depending on the
160164 /// value of isDtors.
161 void runStaticConstructorsDestructors(bool isDtors);
165 void runStaticConstructorsDestructors(Module *module, bool isDtors);
162166
163167
164168 /// runFunctionAsMain - This is a helper function which wraps runFunction to
229229
230230
231231 /// runStaticConstructorsDestructors - This method is used to execute all of
232 /// the static constructors or destructors for a module, depending on the
233 /// value of isDtors.
234 void ExecutionEngine::runStaticConstructorsDestructors(Module *module, bool isDtors) {
235 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
236
237 // Execute global ctors/dtors for each module in the program.
238
239 GlobalVariable *GV = module->getNamedGlobal(Name);
240
241 // If this global has internal linkage, or if it has a use, then it must be
242 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
243 // this is the case, don't execute any of the global ctors, __main will do
244 // it.
245 if (!GV || GV->isDeclaration() || GV->hasInternalLinkage()) return;
246
247 // Should be an array of '{ int, void ()* }' structs. The first value is
248 // the init priority, which we ignore.
249 ConstantArray *InitList = dyn_cast(GV->getInitializer());
250 if (!InitList) return;
251 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
252 if (ConstantStruct *CS =
253 dyn_cast(InitList->getOperand(i))) {
254 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
255
256 Constant *FP = CS->getOperand(1);
257 if (FP->isNullValue())
258 break; // Found a null terminator, exit.
259
260 if (ConstantExpr *CE = dyn_cast(FP))
261 if (CE->isCast())
262 FP = CE->getOperand(0);
263 if (Function *F = dyn_cast(FP)) {
264 // Execute the ctor/dtor function!
265 runFunction(F, std::vector());
266 }
267 }
268 }
269
270 /// runStaticConstructorsDestructors - This method is used to execute all of
232271 /// the static constructors or destructors for a program, depending on the
233272 /// value of isDtors.
234273 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
235 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
236
237274 // Execute global ctors/dtors for each module in the program.
238 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
239 GlobalVariable *GV = Modules[m]->getModule()->getNamedGlobal(Name);
240
241 // If this global has internal linkage, or if it has a use, then it must be
242 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
243 // this is the case, don't execute any of the global ctors, __main will do
244 // it.
245 if (!GV || GV->isDeclaration() || GV->hasInternalLinkage()) continue;
246
247 // Should be an array of '{ int, void ()* }' structs. The first value is
248 // the init priority, which we ignore.
249 ConstantArray *InitList = dyn_cast(GV->getInitializer());
250 if (!InitList) continue;
251 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
252 if (ConstantStruct *CS =
253 dyn_cast(InitList->getOperand(i))) {
254 if (CS->getNumOperands() != 2) break; // Not array of 2-element structs.
255
256 Constant *FP = CS->getOperand(1);
257 if (FP->isNullValue())
258 break; // Found a null terminator, exit.
259
260 if (ConstantExpr *CE = dyn_cast(FP))
261 if (CE->isCast())
262 FP = CE->getOperand(0);
263 if (Function *F = dyn_cast(FP)) {
264 // Execute the ctor/dtor function!
265 runFunction(F, std::vector());
266 }
267 }
268 }
275 for (unsigned m = 0, e = Modules.size(); m != e; ++m)
276 runStaticConstructorsDestructors(Modules[m]->getModule(), isDtors);
269277 }
270278
271279 #ifndef NDEBUG