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/LLVMContext.h" 28 #include "llvm/Module.h" 29 #include "llvm/Pass.h" 30 #include "llvm/Analysis/DebugInfo.h" 31 #include "llvm/ValueSymbolTable.h" 32 #include "llvm/TypeSymbolTable.h" 33 #include "llvm/Transforms/Utils/Local.h" 34 #include "llvm/Support/Compiler.h" 35 #include "llvm/ADT/SmallPtrSet.h" 36 using namespace llvm; 37 38 namespace { 39 class VISIBILITY_HIDDEN StripSymbols : public ModulePass { 40 bool OnlyDebugInfo; 41 public: 42 static char ID; // Pass identification, replacement for typeid 43 explicit StripSymbols(bool ODI = false) 44 : ModulePass(&ID), OnlyDebugInfo(ODI) {} 45 46 virtual bool runOnModule(Module &M); 47 48 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 49 AU.setPreservesAll(); 50 } 51 }; 52 53 class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass { 54 public: 55 static char ID; // Pass identification, replacement for typeid 56 explicit StripNonDebugSymbols() 57 : ModulePass(&ID) {} 58 59 virtual bool runOnModule(Module &M); 60 61 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 62 AU.setPreservesAll(); 63 } 64 }; 65 66 class VISIBILITY_HIDDEN StripDebugDeclare : public ModulePass { 67 public: 68 static char ID; // Pass identification, replacement for typeid 69 explicit StripDebugDeclare() 70 : ModulePass(&ID) {} 71 72 virtual bool runOnModule(Module &M); 73 74 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 75 AU.setPreservesAll(); 76 } 77 }; 78 } 79 80 char StripSymbols::ID = 0; 81 static RegisterPass<StripSymbols> 82 X("strip", "Strip all symbols from a module"); 83 84 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) { 85 return new StripSymbols(OnlyDebugInfo); 86 } 87 88 char StripNonDebugSymbols::ID = 0; 89 static RegisterPass<StripNonDebugSymbols> 90 Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module"); 91 92 ModulePass *llvm::createStripNonDebugSymbolsPass() { 93 return new StripNonDebugSymbols(); 94 } 95 96 char StripDebugDeclare::ID = 0; 97 static RegisterPass<StripDebugDeclare> 98 Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics"); 99 100 ModulePass *llvm::createStripDebugDeclarePass() { 101 return new StripDebugDeclare(); 102 } 103 104 /// OnlyUsedBy - Return true if V is only used by Usr. 105 static bool OnlyUsedBy(Value *V, Value *Usr) { 106 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) { 107 User *U = *I; 108 if (U != Usr) 109 return false; 110 } 111 return true; 112 } 113 114 static void RemoveDeadConstant(Constant *C) { 115 assert(C->use_empty() && "Constant is not dead!"); 116 SmallPtrSet<Constant *, 4> Operands; 117 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) 118 if (isa<DerivedType>(C->getOperand(i)->getType()) && 119 OnlyUsedBy(C->getOperand(i), C)) 120 Operands.insert(C->getOperand(i)); 121 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { 122 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals. 123 GV->eraseFromParent(); 124 } 125 else if (!isa<Function>(C)) 126 if (isa<CompositeType>(C->getType())) 127 C->destroyConstant(); 128 129 // If the constant referenced anything, see if we can delete it as well. 130 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(), 131 OE = Operands.end(); OI != OE; ++OI) 132 RemoveDeadConstant(*OI); 133 } 134 135 // Strip the symbol table of its names. 136 // 137 static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) { 138 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) { 139 Value *V = VI->getValue(); 140 ++VI; 141 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) { 142 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg")) 143 // Set name to "", removing from symbol table! 144 V->setName(""); 145 } 146 } 147 } 148 149 // Strip the symbol table of its names. 150 static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) { 151 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) { 152 if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0) 153 ++TI; 154 else 155 ST.remove(TI++); 156 } 157 } 158 159 /// Find values that are marked as llvm.used. 160 static void findUsedValues(GlobalVariable *LLVMUsed, 161 SmallPtrSet<const GlobalValue*, 8> &UsedValues) { 162 if (LLVMUsed == 0) return; 163 UsedValues.insert(LLVMUsed); 164 165 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer()); 166 if (Inits == 0) return; 167 168 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) 169 if (GlobalValue *GV = 170 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts())) 171 UsedValues.insert(GV); 172 } 173 174 /// StripSymbolNames - Strip symbol names. 175 bool StripSymbolNames(Module &M, bool PreserveDbgInfo) { 176 177 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues; 178 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues); 179 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues); 180 181 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 182 I != E; ++I) { 183 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0) 184 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg")) 185 I->setName(""); // Internal symbols can't participate in linkage 186 } 187 188 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { 189 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0) 190 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg")) 191 I->setName(""); // Internal symbols can't participate in linkage 192 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo); 193 } 194 195 // Remove all names from types. 196 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo); 197 198 return true; 199 } 200 201 // StripDebugInfo - Strip debug info in the module if it exists. 202 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and 203 // llvm.dbg.region.end calls, and any globals they point to if now dead. 204 bool StripDebugInfo(Module &M) { 205 206 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues; 207 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues); 208 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues); 209 210 SmallVector<GlobalVariable *, 2> CUs; 211 SmallVector<GlobalVariable *, 4> GVs; 212 SmallVector<GlobalVariable *, 4> SPs; 213 CollectDebugInfoAnchors(M, CUs, GVs, SPs); 214 // These anchors use LinkOnce linkage so that the optimizer does not 215 // remove them accidently. Set InternalLinkage for all these debug 216 // info anchors. 217 for (SmallVector<GlobalVariable *, 2>::iterator I = CUs.begin(), 218 E = CUs.end(); I != E; ++I) 219 (*I)->setLinkage(GlobalValue::InternalLinkage); 220 for (SmallVector<GlobalVariable *, 4>::iterator I = GVs.begin(), 221 E = GVs.end(); I != E; ++I) 222 (*I)->setLinkage(GlobalValue::InternalLinkage); 223 for (SmallVector<GlobalVariable *, 4>::iterator I = SPs.begin(), 224 E = SPs.end(); I != E; ++I) 225 (*I)->setLinkage(GlobalValue::InternalLinkage); 226 227 228 // Delete all dbg variables. 229 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 230 I != E; ++I) { 231 GlobalVariable *GV = dyn_cast<GlobalVariable>(I); 232 if (!GV) continue; 233 if (!GV->use_empty() && llvmUsedValues.count(I) == 0) { 234 if (GV->getName().startswith("llvm.dbg")) { 235 GV->replaceAllUsesWith(UndefValue::get(GV->getType())); 236 } 237 } 238 } 239 240 Function *FuncStart = M.getFunction("llvm.dbg.func.start"); 241 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint"); 242 Function *RegionStart = M.getFunction("llvm.dbg.region.start"); 243 Function *RegionEnd = M.getFunction("llvm.dbg.region.end"); 244 Function *Declare = M.getFunction("llvm.dbg.declare"); 245 246 std::vector<Constant*> DeadConstants; 247 248 // Remove all of the calls to the debugger intrinsics, and remove them from 249 // the module. 250 if (FuncStart) { 251 while (!FuncStart->use_empty()) { 252 CallInst *CI = cast<CallInst>(FuncStart->use_back()); 253 Value *Arg = CI->getOperand(1); 254 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 255 CI->eraseFromParent(); 256 if (Arg->use_empty()) 257 if (Constant *C = dyn_cast<Constant>(Arg)) 258 DeadConstants.push_back(C); 259 } 260 FuncStart->eraseFromParent(); 261 } 262 if (StopPoint) { 263 while (!StopPoint->use_empty()) { 264 CallInst *CI = cast<CallInst>(StopPoint->use_back()); 265 Value *Arg = CI->getOperand(3); 266 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 267 CI->eraseFromParent(); 268 if (Arg->use_empty()) 269 if (Constant *C = dyn_cast<Constant>(Arg)) 270 DeadConstants.push_back(C); 271 } 272 StopPoint->eraseFromParent(); 273 } 274 if (RegionStart) { 275 while (!RegionStart->use_empty()) { 276 CallInst *CI = cast<CallInst>(RegionStart->use_back()); 277 Value *Arg = CI->getOperand(1); 278 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 279 CI->eraseFromParent(); 280 if (Arg->use_empty()) 281 if (Constant *C = dyn_cast<Constant>(Arg)) 282 DeadConstants.push_back(C); 283 } 284 RegionStart->eraseFromParent(); 285 } 286 if (RegionEnd) { 287 while (!RegionEnd->use_empty()) { 288 CallInst *CI = cast<CallInst>(RegionEnd->use_back()); 289 Value *Arg = CI->getOperand(1); 290 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 291 CI->eraseFromParent(); 292 if (Arg->use_empty()) 293 if (Constant *C = dyn_cast<Constant>(Arg)) 294 DeadConstants.push_back(C); 295 } 296 RegionEnd->eraseFromParent(); 297 } 298 if (Declare) { 299 while (!Declare->use_empty()) { 300 CallInst *CI = cast<CallInst>(Declare->use_back()); 301 Value *Arg1 = CI->getOperand(1); 302 Value *Arg2 = CI->getOperand(2); 303 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 304 CI->eraseFromParent(); 305 if (Arg1->use_empty()) { 306 if (Constant *C = dyn_cast<Constant>(Arg1)) 307 DeadConstants.push_back(C); 308 else 309 RecursivelyDeleteTriviallyDeadInstructions(Arg1); 310 } 311 if (Arg2->use_empty()) 312 if (Constant *C = dyn_cast<Constant>(Arg2)) 313 DeadConstants.push_back(C); 314 } 315 Declare->eraseFromParent(); 316 } 317 318 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce 319 // but since we are removing all debug information, make them internal now. 320 // FIXME: Use private linkage maybe? 321 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units")) 322 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) 323 GV->setLinkage(GlobalValue::InternalLinkage); 324 325 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms")) 326 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) 327 GV->setLinkage(GlobalValue::InternalLinkage); 328 329 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables")) 330 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) 331 GV->setLinkage(GlobalValue::InternalLinkage); 332 333 // Delete all dbg variables. 334 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 335 I != E; ++I) { 336 GlobalVariable *GV = dyn_cast<GlobalVariable>(I); 337 if (!GV) continue; 338 if (GV->use_empty() && llvmUsedValues.count(I) == 0 339 && (!GV->hasSection() 340 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0)) 341 DeadConstants.push_back(GV); 342 } 343 344 if (DeadConstants.empty()) 345 return false; 346 347 // Delete any internal globals that were only used by the debugger intrinsics. 348 while (!DeadConstants.empty()) { 349 Constant *C = DeadConstants.back(); 350 DeadConstants.pop_back(); 351 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { 352 if (GV->hasLocalLinkage()) 353 RemoveDeadConstant(GV); 354 } 355 else 356 RemoveDeadConstant(C); 357 } 358 359 // Remove all llvm.dbg types. 360 TypeSymbolTable &ST = M.getTypeSymbolTable(); 361 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) { 362 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9)) 363 ST.remove(TI++); 364 else 365 ++TI; 366 } 367 368 return true; 369 } 370 371 bool StripSymbols::runOnModule(Module &M) { 372 bool Changed = false; 373 Changed |= StripDebugInfo(M); 374 if (!OnlyDebugInfo) 375 Changed |= StripSymbolNames(M, false); 376 return Changed; 377 } 378 379 bool StripNonDebugSymbols::runOnModule(Module &M) { 380 return StripSymbolNames(M, true); 381 } 382 383 bool StripDebugDeclare::runOnModule(Module &M) { 384 385 Function *Declare = M.getFunction("llvm.dbg.declare"); 386 std::vector<Constant*> DeadConstants; 387 388 if (Declare) { 389 while (!Declare->use_empty()) { 390 CallInst *CI = cast<CallInst>(Declare->use_back()); 391 Value *Arg1 = CI->getOperand(1); 392 Value *Arg2 = CI->getOperand(2); 393 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 394 CI->eraseFromParent(); 395 if (Arg1->use_empty()) { 396 if (Constant *C = dyn_cast<Constant>(Arg1)) 397 DeadConstants.push_back(C); 398 else 399 RecursivelyDeleteTriviallyDeadInstructions(Arg1); 400 } 401 if (Arg2->use_empty()) 402 if (Constant *C = dyn_cast<Constant>(Arg2)) 403 DeadConstants.push_back(C); 404 } 405 Declare->eraseFromParent(); 406 } 407 408 // Delete all llvm.dbg.global_variables. 409 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 410 I != E; ++I) { 411 GlobalVariable *GV = dyn_cast<GlobalVariable>(I); 412 if (!GV) continue; 413 if (GV->use_empty() && GV->getName().startswith("llvm.dbg.global_variable")) 414 DeadConstants.push_back(GV); 415 } 416 417 while (!DeadConstants.empty()) { 418 Constant *C = DeadConstants.back(); 419 DeadConstants.pop_back(); 420 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { 421 if (GV->hasLocalLinkage()) 422 RemoveDeadConstant(GV); 423 } 424 else 425 RemoveDeadConstant(C); 426 } 427 428 return true; 429 } 430