1 //===-- ModuleUtils.cpp - Functions to manipulate Modules -----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This family of functions perform manipulations on Modules. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/Utils/ModuleUtils.h" 15 #include "llvm/DerivedTypes.h" 16 #include "llvm/Function.h" 17 #include "llvm/Module.h" 18 #include "llvm/Support/IRBuilder.h" 19 20 using namespace llvm; 21 22 void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority) { 23 IRBuilder<> IRB(M.getContext()); 24 FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false); 25 StructType *Ty = StructType::get( 26 IRB.getInt32Ty(), PointerType::getUnqual(FnTy), NULL); 27 28 Constant *RuntimeCtorInit = ConstantStruct::get( 29 Ty, IRB.getInt32(Priority), F, NULL); 30 31 // Get the current set of static global constructors and add the new ctor 32 // to the list. 33 SmallVector<Constant *, 16> CurrentCtors; 34 if (GlobalVariable * GVCtor = M.getNamedGlobal("llvm.global_ctors")) { 35 if (Constant *Init = GVCtor->getInitializer()) { 36 unsigned n = Init->getNumOperands(); 37 CurrentCtors.reserve(n + 1); 38 for (unsigned i = 0; i != n; ++i) 39 CurrentCtors.push_back(cast<Constant>(Init->getOperand(i))); 40 } 41 GVCtor->eraseFromParent(); 42 } 43 44 CurrentCtors.push_back(RuntimeCtorInit); 45 46 // Create a new initializer. 47 ArrayType *AT = ArrayType::get(RuntimeCtorInit->getType(), 48 CurrentCtors.size()); 49 Constant *NewInit = ConstantArray::get(AT, CurrentCtors); 50 51 // Create the new global variable and replace all uses of 52 // the old global variable with the new one. 53 (void)new GlobalVariable(M, NewInit->getType(), false, 54 GlobalValue::AppendingLinkage, NewInit, 55 "llvm.global_ctors"); 56 } 57