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/Constants.h" 25#include "llvm/DerivedTypes.h" 26#include "llvm/Instructions.h" 27#include "llvm/Module.h" 28#include "llvm/Pass.h" 29#include "llvm/Analysis/DebugInfo.h" 30#include "llvm/ValueSymbolTable.h" 31#include "llvm/Transforms/Utils/Local.h" 32#include "llvm/ADT/DenseMap.h" 33#include "llvm/ADT/SmallPtrSet.h" 34using namespace llvm; 35 36namespace { 37 class StripSymbols : public ModulePass { 38 bool OnlyDebugInfo; 39 public: 40 static char ID; // Pass identification, replacement for typeid 41 explicit StripSymbols(bool ODI = false) 42 : ModulePass(ID), OnlyDebugInfo(ODI) { 43 initializeStripSymbolsPass(*PassRegistry::getPassRegistry()); 44 } 45 46 virtual bool runOnModule(Module &M); 47 48 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 49 AU.setPreservesAll(); 50 } 51 }; 52 53 class StripNonDebugSymbols : public ModulePass { 54 public: 55 static char ID; // Pass identification, replacement for typeid 56 explicit StripNonDebugSymbols() 57 : ModulePass(ID) { 58 initializeStripNonDebugSymbolsPass(*PassRegistry::getPassRegistry()); 59 } 60 61 virtual bool runOnModule(Module &M); 62 63 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 64 AU.setPreservesAll(); 65 } 66 }; 67 68 class StripDebugDeclare : public ModulePass { 69 public: 70 static char ID; // Pass identification, replacement for typeid 71 explicit StripDebugDeclare() 72 : ModulePass(ID) { 73 initializeStripDebugDeclarePass(*PassRegistry::getPassRegistry()); 74 } 75 76 virtual bool runOnModule(Module &M); 77 78 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 79 AU.setPreservesAll(); 80 } 81 }; 82 83 class StripDeadDebugInfo : public ModulePass { 84 public: 85 static char ID; // Pass identification, replacement for typeid 86 explicit StripDeadDebugInfo() 87 : ModulePass(ID) { 88 initializeStripDeadDebugInfoPass(*PassRegistry::getPassRegistry()); 89 } 90 91 virtual bool runOnModule(Module &M); 92 93 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 94 AU.setPreservesAll(); 95 } 96 }; 97} 98 99char StripSymbols::ID = 0; 100INITIALIZE_PASS(StripSymbols, "strip", 101 "Strip all symbols from a module", false, false) 102 103ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) { 104 return new StripSymbols(OnlyDebugInfo); 105} 106 107char StripNonDebugSymbols::ID = 0; 108INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug", 109 "Strip all symbols, except dbg symbols, from a module", 110 false, false) 111 112ModulePass *llvm::createStripNonDebugSymbolsPass() { 113 return new StripNonDebugSymbols(); 114} 115 116char StripDebugDeclare::ID = 0; 117INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare", 118 "Strip all llvm.dbg.declare intrinsics", false, false) 119 120ModulePass *llvm::createStripDebugDeclarePass() { 121 return new StripDebugDeclare(); 122} 123 124char StripDeadDebugInfo::ID = 0; 125INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info", 126 "Strip debug info for unused symbols", false, false) 127 128ModulePass *llvm::createStripDeadDebugInfoPass() { 129 return new StripDeadDebugInfo(); 130} 131 132/// OnlyUsedBy - Return true if V is only used by Usr. 133static bool OnlyUsedBy(Value *V, Value *Usr) { 134 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) { 135 User *U = *I; 136 if (U != Usr) 137 return false; 138 } 139 return true; 140} 141 142static void RemoveDeadConstant(Constant *C) { 143 assert(C->use_empty() && "Constant is not dead!"); 144 SmallPtrSet<Constant*, 4> Operands; 145 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) 146 if (OnlyUsedBy(C->getOperand(i), C)) 147 Operands.insert(cast<Constant>(C->getOperand(i))); 148 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { 149 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals. 150 GV->eraseFromParent(); 151 } 152 else if (!isa<Function>(C)) 153 if (isa<CompositeType>(C->getType())) 154 C->destroyConstant(); 155 156 // If the constant referenced anything, see if we can delete it as well. 157 for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(), 158 OE = Operands.end(); OI != OE; ++OI) 159 RemoveDeadConstant(*OI); 160} 161 162// Strip the symbol table of its names. 163// 164static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) { 165 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) { 166 Value *V = VI->getValue(); 167 ++VI; 168 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) { 169 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg")) 170 // Set name to "", removing from symbol table! 171 V->setName(""); 172 } 173 } 174} 175 176// Strip any named types of their names. 177static void StripTypeNames(Module &M, bool PreserveDbgInfo) { 178 std::vector<StructType*> StructTypes; 179 M.findUsedStructTypes(StructTypes); 180 181 for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) { 182 StructType *STy = StructTypes[i]; 183 if (STy->isLiteral() || STy->getName().empty()) continue; 184 185 if (PreserveDbgInfo && STy->getName().startswith("llvm.dbg")) 186 continue; 187 188 STy->setName(""); 189 } 190} 191 192/// Find values that are marked as llvm.used. 193static void findUsedValues(GlobalVariable *LLVMUsed, 194 SmallPtrSet<const GlobalValue*, 8> &UsedValues) { 195 if (LLVMUsed == 0) return; 196 UsedValues.insert(LLVMUsed); 197 198 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer()); 199 if (Inits == 0) return; 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. 208static 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. 237static 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 285bool 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 293bool StripNonDebugSymbols::runOnModule(Module &M) { 294 return StripSymbolNames(M, true); 295} 296 297bool 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/// getRealLinkageName - If special LLVM prefix that is used to inform the asm 336/// printer to not emit usual symbol prefix before the symbol name is used then 337/// return linkage name after skipping this special LLVM prefix. 338static StringRef getRealLinkageName(StringRef LinkageName) { 339 char One = '\1'; 340 if (LinkageName.startswith(StringRef(&One, 1))) 341 return LinkageName.substr(1); 342 return LinkageName; 343} 344 345bool StripDeadDebugInfo::runOnModule(Module &M) { 346 bool Changed = false; 347 348 // Debugging infomration is encoded in llvm IR using metadata. This is designed 349 // such a way that debug info for symbols preserved even if symbols are 350 // optimized away by the optimizer. This special pass removes debug info for 351 // such symbols. 352 353 // llvm.dbg.gv keeps track of debug info for global variables. 354 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) { 355 SmallVector<MDNode *, 8> MDs; 356 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) 357 if (DIGlobalVariable(NMD->getOperand(i)).Verify()) 358 MDs.push_back(NMD->getOperand(i)); 359 else 360 Changed = true; 361 NMD->eraseFromParent(); 362 NMD = NULL; 363 364 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(), 365 E = MDs.end(); I != E; ++I) { 366 GlobalVariable *GV = DIGlobalVariable(*I).getGlobal(); 367 if (GV && M.getGlobalVariable(GV->getName(), true)) { 368 if (!NMD) 369 NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv"); 370 NMD->addOperand(*I); 371 } 372 else 373 Changed = true; 374 } 375 } 376 377 // llvm.dbg.sp keeps track of debug info for subprograms. 378 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) { 379 SmallVector<MDNode *, 8> MDs; 380 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) 381 if (DISubprogram(NMD->getOperand(i)).Verify()) 382 MDs.push_back(NMD->getOperand(i)); 383 else 384 Changed = true; 385 NMD->eraseFromParent(); 386 NMD = NULL; 387 388 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(), 389 E = MDs.end(); I != E; ++I) { 390 bool FnIsLive = false; 391 if (Function *F = DISubprogram(*I).getFunction()) 392 if (M.getFunction(F->getName())) 393 FnIsLive = true; 394 if (FnIsLive) { 395 if (!NMD) 396 NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp"); 397 NMD->addOperand(*I); 398 } else { 399 // Remove llvm.dbg.lv.fnname named mdnode which may have been used 400 // to hold debug info for dead function's local variables. 401 StringRef FName = DISubprogram(*I).getLinkageName(); 402 if (FName.empty()) 403 FName = DISubprogram(*I).getName(); 404 if (NamedMDNode *LVNMD = 405 M.getNamedMetadata(Twine("llvm.dbg.lv.", 406 getRealLinkageName(FName)))) 407 LVNMD->eraseFromParent(); 408 } 409 } 410 } 411 412 return Changed; 413} 414