ModuleUtils.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/IR/DerivedTypes.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/IRBuilder.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace llvm;
  20. static void appendToGlobalArray(const char *Array, Module &M, Function *F,
  21. int Priority, Constant *Data) {
  22. IRBuilder<> IRB(M.getContext());
  23. FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
  24. // Get the current set of static global constructors and add the new ctor
  25. // to the list.
  26. SmallVector<Constant *, 16> CurrentCtors;
  27. StructType *EltTy;
  28. if (GlobalVariable *GVCtor = M.getNamedGlobal(Array)) {
  29. ArrayType *ATy = cast<ArrayType>(GVCtor->getValueType());
  30. StructType *OldEltTy = cast<StructType>(ATy->getElementType());
  31. // Upgrade a 2-field global array type to the new 3-field format if needed.
  32. if (Data && OldEltTy->getNumElements() < 3)
  33. EltTy = StructType::get(IRB.getInt32Ty(), PointerType::getUnqual(FnTy),
  34. IRB.getInt8PtrTy());
  35. else
  36. EltTy = OldEltTy;
  37. if (Constant *Init = GVCtor->getInitializer()) {
  38. unsigned n = Init->getNumOperands();
  39. CurrentCtors.reserve(n + 1);
  40. for (unsigned i = 0; i != n; ++i) {
  41. auto Ctor = cast<Constant>(Init->getOperand(i));
  42. if (EltTy != OldEltTy)
  43. Ctor =
  44. ConstantStruct::get(EltTy, Ctor->getAggregateElement((unsigned)0),
  45. Ctor->getAggregateElement(1),
  46. Constant::getNullValue(IRB.getInt8PtrTy()));
  47. CurrentCtors.push_back(Ctor);
  48. }
  49. }
  50. GVCtor->eraseFromParent();
  51. } else {
  52. // Use the new three-field struct if there isn't one already.
  53. EltTy = StructType::get(IRB.getInt32Ty(), PointerType::getUnqual(FnTy),
  54. IRB.getInt8PtrTy());
  55. }
  56. // Build a 2 or 3 field global_ctor entry. We don't take a comdat key.
  57. Constant *CSVals[3];
  58. CSVals[0] = IRB.getInt32(Priority);
  59. CSVals[1] = F;
  60. // FIXME: Drop support for the two element form in LLVM 4.0.
  61. if (EltTy->getNumElements() >= 3)
  62. CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, IRB.getInt8PtrTy())
  63. : Constant::getNullValue(IRB.getInt8PtrTy());
  64. Constant *RuntimeCtorInit =
  65. ConstantStruct::get(EltTy, makeArrayRef(CSVals, EltTy->getNumElements()));
  66. CurrentCtors.push_back(RuntimeCtorInit);
  67. // Create a new initializer.
  68. ArrayType *AT = ArrayType::get(EltTy, CurrentCtors.size());
  69. Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
  70. // Create the new global variable and replace all uses of
  71. // the old global variable with the new one.
  72. (void)new GlobalVariable(M, NewInit->getType(), false,
  73. GlobalValue::AppendingLinkage, NewInit, Array);
  74. }
  75. void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data) {
  76. appendToGlobalArray("llvm.global_ctors", M, F, Priority, Data);
  77. }
  78. void llvm::appendToGlobalDtors(Module &M, Function *F, int Priority, Constant *Data) {
  79. appendToGlobalArray("llvm.global_dtors", M, F, Priority, Data);
  80. }
  81. static void appendToUsedList(Module &M, StringRef Name, ArrayRef<GlobalValue *> Values) {
  82. GlobalVariable *GV = M.getGlobalVariable(Name);
  83. SmallPtrSet<Constant *, 16> InitAsSet;
  84. SmallVector<Constant *, 16> Init;
  85. if (GV) {
  86. ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
  87. for (auto &Op : CA->operands()) {
  88. Constant *C = cast_or_null<Constant>(Op);
  89. if (InitAsSet.insert(C).second)
  90. Init.push_back(C);
  91. }
  92. GV->eraseFromParent();
  93. }
  94. Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext());
  95. for (auto *V : Values) {
  96. Constant *C = ConstantExpr::getBitCast(V, Int8PtrTy);
  97. if (InitAsSet.insert(C).second)
  98. Init.push_back(C);
  99. }
  100. if (Init.empty())
  101. return;
  102. ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
  103. GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
  104. ConstantArray::get(ATy, Init), Name);
  105. GV->setSection("llvm.metadata");
  106. }
  107. void llvm::appendToUsed(Module &M, ArrayRef<GlobalValue *> Values) {
  108. appendToUsedList(M, "llvm.used", Values);
  109. }
  110. void llvm::appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values) {
  111. appendToUsedList(M, "llvm.compiler.used", Values);
  112. }
  113. Function *llvm::checkSanitizerInterfaceFunction(Constant *FuncOrBitcast) {
  114. if (isa<Function>(FuncOrBitcast))
  115. return cast<Function>(FuncOrBitcast);
  116. FuncOrBitcast->print(errs());
  117. errs() << '\n';
  118. std::string Err;
  119. raw_string_ostream Stream(Err);
  120. Stream << "Sanitizer interface function redefined: " << *FuncOrBitcast;
  121. report_fatal_error(Err);
  122. }
  123. Function *llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
  124. ArrayRef<Type *> InitArgTypes) {
  125. assert(!InitName.empty() && "Expected init function name");
  126. Function *F = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  127. InitName,
  128. FunctionType::get(Type::getVoidTy(M.getContext()), InitArgTypes, false),
  129. AttributeList()));
  130. F->setLinkage(Function::ExternalLinkage);
  131. return F;
  132. }
  133. std::pair<Function *, Function *> llvm::createSanitizerCtorAndInitFunctions(
  134. Module &M, StringRef CtorName, StringRef InitName,
  135. ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
  136. StringRef VersionCheckName) {
  137. assert(!InitName.empty() && "Expected init function name");
  138. assert(InitArgs.size() == InitArgTypes.size() &&
  139. "Sanitizer's init function expects different number of arguments");
  140. Function *InitFunction =
  141. declareSanitizerInitFunction(M, InitName, InitArgTypes);
  142. Function *Ctor = Function::Create(
  143. FunctionType::get(Type::getVoidTy(M.getContext()), false),
  144. GlobalValue::InternalLinkage, CtorName, &M);
  145. BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
  146. IRBuilder<> IRB(ReturnInst::Create(M.getContext(), CtorBB));
  147. IRB.CreateCall(InitFunction, InitArgs);
  148. if (!VersionCheckName.empty()) {
  149. Function *VersionCheckFunction =
  150. checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  151. VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
  152. AttributeList()));
  153. IRB.CreateCall(VersionCheckFunction, {});
  154. }
  155. return std::make_pair(Ctor, InitFunction);
  156. }
  157. void llvm::filterDeadComdatFunctions(
  158. Module &M, SmallVectorImpl<Function *> &DeadComdatFunctions) {
  159. // Build a map from the comdat to the number of entries in that comdat we
  160. // think are dead. If this fully covers the comdat group, then the entire
  161. // group is dead. If we find another entry in the comdat group though, we'll
  162. // have to preserve the whole group.
  163. SmallDenseMap<Comdat *, int, 16> ComdatEntriesCovered;
  164. for (Function *F : DeadComdatFunctions) {
  165. Comdat *C = F->getComdat();
  166. assert(C && "Expected all input GVs to be in a comdat!");
  167. ComdatEntriesCovered[C] += 1;
  168. }
  169. auto CheckComdat = [&](Comdat &C) {
  170. auto CI = ComdatEntriesCovered.find(&C);
  171. if (CI == ComdatEntriesCovered.end())
  172. return;
  173. // If this could have been covered by a dead entry, just subtract one to
  174. // account for it.
  175. if (CI->second > 0) {
  176. CI->second -= 1;
  177. return;
  178. }
  179. // If we've already accounted for all the entries that were dead, the
  180. // entire comdat is alive so remove it from the map.
  181. ComdatEntriesCovered.erase(CI);
  182. };
  183. auto CheckAllComdats = [&] {
  184. for (Function &F : M.functions())
  185. if (Comdat *C = F.getComdat()) {
  186. CheckComdat(*C);
  187. if (ComdatEntriesCovered.empty())
  188. return;
  189. }
  190. for (GlobalVariable &GV : M.globals())
  191. if (Comdat *C = GV.getComdat()) {
  192. CheckComdat(*C);
  193. if (ComdatEntriesCovered.empty())
  194. return;
  195. }
  196. for (GlobalAlias &GA : M.aliases())
  197. if (Comdat *C = GA.getComdat()) {
  198. CheckComdat(*C);
  199. if (ComdatEntriesCovered.empty())
  200. return;
  201. }
  202. };
  203. CheckAllComdats();
  204. if (ComdatEntriesCovered.empty()) {
  205. DeadComdatFunctions.clear();
  206. return;
  207. }
  208. // Remove the entries that were not covering.
  209. erase_if(DeadComdatFunctions, [&](GlobalValue *GV) {
  210. return ComdatEntriesCovered.find(GV->getComdat()) ==
  211. ComdatEntriesCovered.end();
  212. });
  213. }
  214. std::string llvm::getUniqueModuleId(Module *M) {
  215. MD5 Md5;
  216. bool ExportsSymbols = false;
  217. auto AddGlobal = [&](GlobalValue &GV) {
  218. if (GV.isDeclaration() || GV.getName().startswith("llvm.") ||
  219. !GV.hasExternalLinkage())
  220. return;
  221. ExportsSymbols = true;
  222. Md5.update(GV.getName());
  223. Md5.update(ArrayRef<uint8_t>{0});
  224. };
  225. for (auto &F : *M)
  226. AddGlobal(F);
  227. for (auto &GV : M->globals())
  228. AddGlobal(GV);
  229. for (auto &GA : M->aliases())
  230. AddGlobal(GA);
  231. for (auto &IF : M->ifuncs())
  232. AddGlobal(IF);
  233. if (!ExportsSymbols)
  234. return "";
  235. MD5::MD5Result R;
  236. Md5.final(R);
  237. SmallString<32> Str;
  238. MD5::stringifyResult(R, Str);
  239. return ("$" + Str).str();
  240. }