ModuleUtils.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include "llvm/Transforms/Utils/ModuleUtils.h"
  14. #include "llvm/ADT/SmallPtrSet.h"
  15. #include "llvm/IR/DerivedTypes.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/IRBuilder.h"
  18. #include "llvm/IR/Module.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace llvm;
  21. static void appendToGlobalArray(const char *Array,
  22. Module &M, Function *F, int Priority) {
  23. IRBuilder<> IRB(M.getContext());
  24. FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
  25. // Get the current set of static global constructors and add the new ctor
  26. // to the list.
  27. SmallVector<Constant *, 16> CurrentCtors;
  28. StructType *EltTy;
  29. if (GlobalVariable *GVCtor = M.getNamedGlobal(Array)) {
  30. // If there is a global_ctors array, use the existing struct type, which can
  31. // have 2 or 3 fields.
  32. ArrayType *ATy = cast<ArrayType>(GVCtor->getType()->getElementType());
  33. EltTy = cast<StructType>(ATy->getElementType());
  34. if (Constant *Init = GVCtor->getInitializer()) {
  35. unsigned n = Init->getNumOperands();
  36. CurrentCtors.reserve(n + 1);
  37. for (unsigned i = 0; i != n; ++i)
  38. CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
  39. }
  40. GVCtor->eraseFromParent();
  41. } else {
  42. // Use a simple two-field struct if there isn't one already.
  43. EltTy = StructType::get(IRB.getInt32Ty(), PointerType::getUnqual(FnTy),
  44. nullptr);
  45. }
  46. // Build a 2 or 3 field global_ctor entry. We don't take a comdat key.
  47. Constant *CSVals[3];
  48. CSVals[0] = IRB.getInt32(Priority);
  49. CSVals[1] = F;
  50. // FIXME: Drop support for the two element form in LLVM 4.0.
  51. if (EltTy->getNumElements() >= 3)
  52. CSVals[2] = llvm::Constant::getNullValue(IRB.getInt8PtrTy());
  53. Constant *RuntimeCtorInit =
  54. ConstantStruct::get(EltTy, makeArrayRef(CSVals, EltTy->getNumElements()));
  55. CurrentCtors.push_back(RuntimeCtorInit);
  56. // Create a new initializer.
  57. ArrayType *AT = ArrayType::get(EltTy, CurrentCtors.size());
  58. Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
  59. // Create the new global variable and replace all uses of
  60. // the old global variable with the new one.
  61. (void)new GlobalVariable(M, NewInit->getType(), false,
  62. GlobalValue::AppendingLinkage, NewInit, Array);
  63. }
  64. void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority) {
  65. appendToGlobalArray("llvm.global_ctors", M, F, Priority);
  66. }
  67. void llvm::appendToGlobalDtors(Module &M, Function *F, int Priority) {
  68. appendToGlobalArray("llvm.global_dtors", M, F, Priority);
  69. }
  70. GlobalVariable *
  71. llvm::collectUsedGlobalVariables(Module &M, SmallPtrSetImpl<GlobalValue *> &Set,
  72. bool CompilerUsed) {
  73. const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";
  74. GlobalVariable *GV = M.getGlobalVariable(Name);
  75. if (!GV || !GV->hasInitializer())
  76. return GV;
  77. const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
  78. for (unsigned I = 0, E = Init->getNumOperands(); I != E; ++I) {
  79. Value *Op = Init->getOperand(I);
  80. GlobalValue *G = cast<GlobalValue>(Op->stripPointerCastsNoFollowAliases());
  81. Set.insert(G);
  82. }
  83. return GV;
  84. }
  85. Function *llvm::checkSanitizerInterfaceFunction(Constant *FuncOrBitcast) {
  86. if (isa<Function>(FuncOrBitcast))
  87. return cast<Function>(FuncOrBitcast);
  88. FuncOrBitcast->dump();
  89. std::string Err;
  90. raw_string_ostream Stream(Err);
  91. Stream << "Sanitizer interface function redefined: " << *FuncOrBitcast;
  92. report_fatal_error(Err);
  93. }
  94. std::pair<Function *, Function *> llvm::createSanitizerCtorAndInitFunctions(
  95. Module &M, StringRef CtorName, StringRef InitName,
  96. ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs) {
  97. assert(!InitName.empty() && "Expected init function name");
  98. assert(InitArgTypes.size() == InitArgTypes.size() &&
  99. "Sanitizer's init function expects different number of arguments");
  100. Function *Ctor = Function::Create(
  101. FunctionType::get(Type::getVoidTy(M.getContext()), false),
  102. GlobalValue::InternalLinkage, CtorName, &M);
  103. BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
  104. IRBuilder<> IRB(ReturnInst::Create(M.getContext(), CtorBB));
  105. Function *InitFunction =
  106. checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  107. InitName, FunctionType::get(IRB.getVoidTy(), InitArgTypes, false),
  108. AttributeSet()));
  109. InitFunction->setLinkage(Function::ExternalLinkage);
  110. IRB.CreateCall(InitFunction, InitArgs);
  111. return std::make_pair(Ctor, InitFunction);
  112. }