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