1 //===- StripSymbols.cpp - Strip symbols and debug info from a module ------===// 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 // The StripSymbols transformation implements code stripping. Specifically, it 11 // can delete: 12 // 13 // * names for virtual registers 14 // * symbols for internal globals and functions 15 // * debug information 16 // 17 // Note that this transformation makes code much less readable, so it should 18 // only be used in situations where the 'strip' utility would be used, such as 19 // reducing code size or making it harder to reverse engineer code. 20 // 21 //===----------------------------------------------------------------------===// 22 23 #include "llvm/Transforms/IPO.h" 24 #include "llvm/ADT/DenseMap.h" 25 #include "llvm/ADT/SmallPtrSet.h" 26 #include "llvm/DebugInfo.h" 27 #include "llvm/IR/Constants.h" 28 #include "llvm/IR/DerivedTypes.h" 29 #include "llvm/IR/Instructions.h" 30 #include "llvm/IR/Module.h" 31 #include "llvm/IR/TypeFinder.h" 32 #include "llvm/IR/ValueSymbolTable.h" 33 #include "llvm/Pass.h" 34 #include "llvm/Transforms/Utils/Local.h" 35 using namespace llvm; 36 37 namespace { 38 class StripSymbols : public ModulePass { 39 bool OnlyDebugInfo; 40 public: 41 static char ID; // Pass identification, replacement for typeid 42 explicit StripSymbols(bool ODI = false) 43 : ModulePass(ID), OnlyDebugInfo(ODI) { 44 initializeStripSymbolsPass(*PassRegistry::getPassRegistry()); 45 } 46 47 virtual bool runOnModule(Module &M); 48 49 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 50 AU.setPreservesAll(); 51 } 52 }; 53 54 class StripNonDebugSymbols : public ModulePass { 55 public: 56 static char ID; // Pass identification, replacement for typeid 57 explicit StripNonDebugSymbols() 58 : ModulePass(ID) { 59 initializeStripNonDebugSymbolsPass(*PassRegistry::getPassRegistry()); 60 } 61 62 virtual bool runOnModule(Module &M); 63 64 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 65 AU.setPreservesAll(); 66 } 67 }; 68 69 class StripDebugDeclare : public ModulePass { 70 public: 71 static char ID; // Pass identification, replacement for typeid 72 explicit StripDebugDeclare() 73 : ModulePass(ID) { 74 initializeStripDebugDeclarePass(*PassRegistry::getPassRegistry()); 75 } 76 77 virtual bool runOnModule(Module &M); 78 79 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 80 AU.setPreservesAll(); 81 } 82 }; 83 84 class StripDeadDebugInfo : public ModulePass { 85 public: 86 static char ID; // Pass identification, replacement for typeid 87 explicit StripDeadDebugInfo() 88 : ModulePass(ID) { 89 initializeStripDeadDebugInfoPass(*PassRegistry::getPassRegistry()); 90 } 91 92 virtual bool runOnModule(Module &M); 93 94 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 95 AU.setPreservesAll(); 96 } 97 }; 98 } 99 100 char StripSymbols::ID = 0; 101 INITIALIZE_PASS(StripSymbols, "strip", 102 "Strip all symbols from a module", false, false) 103 104 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) { 105 return new StripSymbols(OnlyDebugInfo); 106 } 107 108 char StripNonDebugSymbols::ID = 0; 109 INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug", 110 "Strip all symbols, except dbg symbols, from a module", 111 false, false) 112 113 ModulePass *llvm::createStripNonDebugSymbolsPass() { 114 return new StripNonDebugSymbols(); 115 } 116 117 char StripDebugDeclare::ID = 0; 118 INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare", 119 "Strip all llvm.dbg.declare intrinsics", false, false) 120 121 ModulePass *llvm::createStripDebugDeclarePass() { 122 return new StripDebugDeclare(); 123 } 124 125 char StripDeadDebugInfo::ID = 0; 126 INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info", 127 "Strip debug info for unused symbols", false, false) 128 129 ModulePass *llvm::createStripDeadDebugInfoPass() { 130 return new StripDeadDebugInfo(); 131 } 132 133 /// OnlyUsedBy - Return true if V is only used by Usr. 134 static bool OnlyUsedBy(Value *V, Value *Usr) { 135 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) { 136 User *U = *I; 137 if (U != Usr) 138 return false; 139 } 140 return true; 141 } 142 143 static void RemoveDeadConstant(Constant *C) { 144 assert(C->use_empty() && "Constant is not dead!"); 145 SmallPtrSet<Constant*, 4> Operands; 146 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) 147 if (OnlyUsedBy(C->getOperand(i), C)) 148 Operands.insert(cast<Constant>(C->getOperand(i))); 149 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { 150 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals. 151 GV->eraseFromParent(); 152 } 153 else if (!isa<Function>(C)) 154 if (isa<CompositeType>(C->getType())) 155 C->destroyConstant(); 156 157 // If the constant referenced anything, see if we can delete it as well. 158 for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(), 159 OE = Operands.end(); OI != OE; ++OI) 160 RemoveDeadConstant(*OI); 161 } 162 163 // Strip the symbol table of its names. 164 // 165 static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) { 166 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) { 167 Value *V = VI->getValue(); 168 ++VI; 169 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) { 170 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg")) 171 // Set name to "", removing from symbol table! 172 V->setName(""); 173 } 174 } 175 } 176 177 // Strip any named types of their names. 178 static void StripTypeNames(Module &M, bool PreserveDbgInfo) { 179 TypeFinder StructTypes; 180 StructTypes.run(M, false); 181 182 for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) { 183 StructType *STy = StructTypes[i]; 184 if (STy->isLiteral() || STy->getName().empty()) continue; 185 186 if (PreserveDbgInfo && STy->getName().startswith("llvm.dbg")) 187 continue; 188 189 STy->setName(""); 190 } 191 } 192 193 /// Find values that are marked as llvm.used. 194 static void findUsedValues(GlobalVariable *LLVMUsed, 195 SmallPtrSet<const GlobalValue*, 8> &UsedValues) { 196 if (LLVMUsed == 0) return; 197 UsedValues.insert(LLVMUsed); 198 199 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer()); 200 201 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) 202 if (GlobalValue *GV = 203 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts())) 204 UsedValues.insert(GV); 205 } 206 207 /// StripSymbolNames - Strip symbol names. 208 static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) { 209 210 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues; 211 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues); 212 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues); 213 214 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 215 I != E; ++I) { 216 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0) 217 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg")) 218 I->setName(""); // Internal symbols can't participate in linkage 219 } 220 221 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { 222 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0) 223 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg")) 224 I->setName(""); // Internal symbols can't participate in linkage 225 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo); 226 } 227 228 // Remove all names from types. 229 StripTypeNames(M, PreserveDbgInfo); 230 231 return true; 232 } 233 234 // StripDebugInfo - Strip debug info in the module if it exists. 235 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and 236 // llvm.dbg.region.end calls, and any globals they point to if now dead. 237 static bool StripDebugInfo(Module &M) { 238 239 bool Changed = false; 240 241 // Remove all of the calls to the debugger intrinsics, and remove them from 242 // the module. 243 if (Function *Declare = M.getFunction("llvm.dbg.declare")) { 244 while (!Declare->use_empty()) { 245 CallInst *CI = cast<CallInst>(Declare->use_back()); 246 CI->eraseFromParent(); 247 } 248 Declare->eraseFromParent(); 249 Changed = true; 250 } 251 252 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { 253 while (!DbgVal->use_empty()) { 254 CallInst *CI = cast<CallInst>(DbgVal->use_back()); 255 CI->eraseFromParent(); 256 } 257 DbgVal->eraseFromParent(); 258 Changed = true; 259 } 260 261 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), 262 NME = M.named_metadata_end(); NMI != NME;) { 263 NamedMDNode *NMD = NMI; 264 ++NMI; 265 if (NMD->getName().startswith("llvm.dbg.")) { 266 NMD->eraseFromParent(); 267 Changed = true; 268 } 269 } 270 271 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) 272 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; 273 ++FI) 274 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; 275 ++BI) { 276 if (!BI->getDebugLoc().isUnknown()) { 277 Changed = true; 278 BI->setDebugLoc(DebugLoc()); 279 } 280 } 281 282 return Changed; 283 } 284 285 bool StripSymbols::runOnModule(Module &M) { 286 bool Changed = false; 287 Changed |= StripDebugInfo(M); 288 if (!OnlyDebugInfo) 289 Changed |= StripSymbolNames(M, false); 290 return Changed; 291 } 292 293 bool StripNonDebugSymbols::runOnModule(Module &M) { 294 return StripSymbolNames(M, true); 295 } 296 297 bool StripDebugDeclare::runOnModule(Module &M) { 298 299 Function *Declare = M.getFunction("llvm.dbg.declare"); 300 std::vector<Constant*> DeadConstants; 301 302 if (Declare) { 303 while (!Declare->use_empty()) { 304 CallInst *CI = cast<CallInst>(Declare->use_back()); 305 Value *Arg1 = CI->getArgOperand(0); 306 Value *Arg2 = CI->getArgOperand(1); 307 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 308 CI->eraseFromParent(); 309 if (Arg1->use_empty()) { 310 if (Constant *C = dyn_cast<Constant>(Arg1)) 311 DeadConstants.push_back(C); 312 else 313 RecursivelyDeleteTriviallyDeadInstructions(Arg1); 314 } 315 if (Arg2->use_empty()) 316 if (Constant *C = dyn_cast<Constant>(Arg2)) 317 DeadConstants.push_back(C); 318 } 319 Declare->eraseFromParent(); 320 } 321 322 while (!DeadConstants.empty()) { 323 Constant *C = DeadConstants.back(); 324 DeadConstants.pop_back(); 325 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { 326 if (GV->hasLocalLinkage()) 327 RemoveDeadConstant(GV); 328 } else 329 RemoveDeadConstant(C); 330 } 331 332 return true; 333 } 334 335 bool StripDeadDebugInfo::runOnModule(Module &M) { 336 bool Changed = false; 337 338 // Debugging infomration is encoded in llvm IR using metadata. This is designed 339 // such a way that debug info for symbols preserved even if symbols are 340 // optimized away by the optimizer. This special pass removes debug info for 341 // such symbols. 342 343 // llvm.dbg.gv keeps track of debug info for global variables. 344 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) { 345 SmallVector<MDNode *, 8> MDs; 346 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) 347 if (NMD->getOperand(i)) { 348 assert(DIGlobalVariable(NMD->getOperand(i)).isGlobalVariable() && 349 "A MDNode in llvm.dbg.gv should be a DIGlobalVariable."); 350 MDs.push_back(NMD->getOperand(i)); 351 } 352 else 353 Changed = true; 354 NMD->eraseFromParent(); 355 NMD = NULL; 356 357 for (SmallVectorImpl<MDNode *>::iterator I = MDs.begin(), 358 E = MDs.end(); I != E; ++I) { 359 GlobalVariable *GV = DIGlobalVariable(*I).getGlobal(); 360 if (GV && M.getGlobalVariable(GV->getName(), true)) { 361 if (!NMD) 362 NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv"); 363 NMD->addOperand(*I); 364 } 365 else 366 Changed = true; 367 } 368 } 369 370 // llvm.dbg.sp keeps track of debug info for subprograms. 371 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) { 372 SmallVector<MDNode *, 8> MDs; 373 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) 374 if (NMD->getOperand(i)) { 375 assert(DISubprogram(NMD->getOperand(i)).isSubprogram() && 376 "A MDNode in llvm.dbg.sp should be a DISubprogram."); 377 MDs.push_back(NMD->getOperand(i)); 378 } 379 else 380 Changed = true; 381 NMD->eraseFromParent(); 382 NMD = NULL; 383 384 for (SmallVectorImpl<MDNode *>::iterator I = MDs.begin(), 385 E = MDs.end(); I != E; ++I) { 386 bool FnIsLive = false; 387 if (Function *F = DISubprogram(*I).getFunction()) 388 if (M.getFunction(F->getName())) 389 FnIsLive = true; 390 if (FnIsLive) { 391 if (!NMD) 392 NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp"); 393 NMD->addOperand(*I); 394 } else { 395 // Remove llvm.dbg.lv.fnname named mdnode which may have been used 396 // to hold debug info for dead function's local variables. 397 StringRef FName = DISubprogram(*I).getLinkageName(); 398 if (FName.empty()) 399 FName = DISubprogram(*I).getName(); 400 if (NamedMDNode *LVNMD = M.getNamedMetadata( 401 "llvm.dbg.lv." + Function::getRealLinkageName(FName))) 402 LVNMD->eraseFromParent(); 403 } 404 } 405 } 406 407 return Changed; 408 } 409