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