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