ModuleUtils.cpp 11 KB

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