StripSymbols.cpp revision 193323
150276Speter//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===// 250276Speter// 350276Speter// The LLVM Compiler Infrastructure 450276Speter// 550276Speter// This file is distributed under the University of Illinois Open Source 650276Speter// License. See LICENSE.TXT for details. 750276Speter// 850276Speter//===----------------------------------------------------------------------===// 950276Speter// 1050276Speter// The StripSymbols transformation implements code stripping. Specifically, it 1150276Speter// can delete: 1250276Speter// 1350276Speter// * names for virtual registers 1450276Speter// * symbols for internal globals and functions 1550276Speter// * debug information 1650276Speter// 1750276Speter// Note that this transformation makes code much less readable, so it should 1850276Speter// only be used in situations where the 'strip' utility would be used, such as 1950276Speter// reducing code size or making it harder to reverse engineer code. 2050276Speter// 2150276Speter//===----------------------------------------------------------------------===// 2250276Speter 2350276Speter#include "llvm/Transforms/IPO.h" 2450276Speter#include "llvm/Constants.h" 2550276Speter#include "llvm/DerivedTypes.h" 2650276Speter#include "llvm/Instructions.h" 2750276Speter#include "llvm/Module.h" 2850276Speter#include "llvm/Pass.h" 2950276Speter#include "llvm/ValueSymbolTable.h" 3050276Speter#include "llvm/TypeSymbolTable.h" 3150276Speter#include "llvm/Transforms/Utils/Local.h" 3250276Speter#include "llvm/Support/Compiler.h" 3350276Speter#include "llvm/ADT/SmallPtrSet.h" 3450276Speterusing namespace llvm; 3550276Speter 3650276Speternamespace { 3750276Speter class VISIBILITY_HIDDEN StripSymbols : public ModulePass { 3850276Speter bool OnlyDebugInfo; 3950276Speter public: 4050276Speter static char ID; // Pass identification, replacement for typeid 4150276Speter explicit StripSymbols(bool ODI = false) 4250276Speter : ModulePass(&ID), OnlyDebugInfo(ODI) {} 4350276Speter 4450276Speter virtual bool runOnModule(Module &M); 4550276Speter 4650276Speter virtual void getAnalysisUsage(AnalysisUsage &AU) const { 4750276Speter AU.setPreservesAll(); 4850276Speter } 4950276Speter }; 5050276Speter 5150276Speter class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass { 5250276Speter public: 5350276Speter static char ID; // Pass identification, replacement for typeid 5450276Speter explicit StripNonDebugSymbols() 5550276Speter : ModulePass(&ID) {} 5650276Speter 5750276Speter virtual bool runOnModule(Module &M); 5850276Speter 5950276Speter virtual void getAnalysisUsage(AnalysisUsage &AU) const { 6050276Speter AU.setPreservesAll(); 6150276Speter } 6250276Speter }; 6350276Speter 6450276Speter class VISIBILITY_HIDDEN StripDebugDeclare : public ModulePass { 6550276Speter public: 6650276Speter static char ID; // Pass identification, replacement for typeid 6750276Speter explicit StripDebugDeclare() 6850276Speter : ModulePass(&ID) {} 6950276Speter 7050276Speter virtual bool runOnModule(Module &M); 7150276Speter 7250276Speter virtual void getAnalysisUsage(AnalysisUsage &AU) const { 7350276Speter AU.setPreservesAll(); 7450276Speter } 7550276Speter }; 7650276Speter} 7750276Speter 7850276Speterchar StripSymbols::ID = 0; 7950276Speterstatic RegisterPass<StripSymbols> 8050276SpeterX("strip", "Strip all symbols from a module"); 8150276Speter 8250276SpeterModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) { 8350276Speter return new StripSymbols(OnlyDebugInfo); 8450276Speter} 8550276Speter 8650276Speterchar StripNonDebugSymbols::ID = 0; 8750276Speterstatic RegisterPass<StripNonDebugSymbols> 8850276SpeterY("strip-nondebug", "Strip all symbols, except dbg symbols, from a module"); 8950276Speter 9050276SpeterModulePass *llvm::createStripNonDebugSymbolsPass() { 9150276Speter return new StripNonDebugSymbols(); 9250276Speter} 93 94char StripDebugDeclare::ID = 0; 95static RegisterPass<StripDebugDeclare> 96Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics"); 97 98ModulePass *llvm::createStripDebugDeclarePass() { 99 return new StripDebugDeclare(); 100} 101 102/// OnlyUsedBy - Return true if V is only used by Usr. 103static bool OnlyUsedBy(Value *V, Value *Usr) { 104 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) { 105 User *U = *I; 106 if (U != Usr) 107 return false; 108 } 109 return true; 110} 111 112static void RemoveDeadConstant(Constant *C) { 113 assert(C->use_empty() && "Constant is not dead!"); 114 SmallPtrSet<Constant *, 4> Operands; 115 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) 116 if (isa<DerivedType>(C->getOperand(i)->getType()) && 117 OnlyUsedBy(C->getOperand(i), C)) 118 Operands.insert(C->getOperand(i)); 119 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { 120 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals. 121 GV->eraseFromParent(); 122 } 123 else if (!isa<Function>(C)) 124 if (isa<CompositeType>(C->getType())) 125 C->destroyConstant(); 126 127 // If the constant referenced anything, see if we can delete it as well. 128 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(), 129 OE = Operands.end(); OI != OE; ++OI) 130 RemoveDeadConstant(*OI); 131} 132 133// Strip the symbol table of its names. 134// 135static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) { 136 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) { 137 Value *V = VI->getValue(); 138 ++VI; 139 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) { 140 if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8)) 141 // Set name to "", removing from symbol table! 142 V->setName(""); 143 } 144 } 145} 146 147// Strip the symbol table of its names. 148static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) { 149 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) { 150 if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0) 151 ++TI; 152 else 153 ST.remove(TI++); 154 } 155} 156 157/// Find values that are marked as llvm.used. 158void findUsedValues(Module &M, 159 SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) { 160 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) { 161 llvmUsedValues.insert(LLVMUsed); 162 // Collect values that are preserved as per explicit request. 163 // llvm.used is used to list these values. 164 if (ConstantArray *Inits = 165 dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) { 166 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) { 167 if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i))) 168 llvmUsedValues.insert(GV); 169 else if (ConstantExpr *CE = 170 dyn_cast<ConstantExpr>(Inits->getOperand(i))) 171 if (CE->getOpcode() == Instruction::BitCast) 172 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0))) 173 llvmUsedValues.insert(GV); 174 } 175 } 176 } 177} 178 179/// StripSymbolNames - Strip symbol names. 180bool StripSymbolNames(Module &M, bool PreserveDbgInfo) { 181 182 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues; 183 findUsedValues(M, llvmUsedValues); 184 185 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 186 I != E; ++I) { 187 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0) 188 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8)) 189 I->setName(""); // Internal symbols can't participate in linkage 190 } 191 192 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { 193 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0) 194 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8)) 195 I->setName(""); // Internal symbols can't participate in linkage 196 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo); 197 } 198 199 // Remove all names from types. 200 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo); 201 202 return true; 203} 204 205// StripDebugInfo - Strip debug info in the module if it exists. 206// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and 207// llvm.dbg.region.end calls, and any globals they point to if now dead. 208bool StripDebugInfo(Module &M) { 209 210 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues; 211 findUsedValues(M, llvmUsedValues); 212 213 // Delete all dbg variables. 214 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 215 I != E; ++I) { 216 GlobalVariable *GV = dyn_cast<GlobalVariable>(I); 217 if (!GV) continue; 218 if (!GV->use_empty() && llvmUsedValues.count(I) == 0) { 219 if (strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0) { 220 GV->replaceAllUsesWith(UndefValue::get(GV->getType())); 221 } 222 } 223 } 224 225 Function *FuncStart = M.getFunction("llvm.dbg.func.start"); 226 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint"); 227 Function *RegionStart = M.getFunction("llvm.dbg.region.start"); 228 Function *RegionEnd = M.getFunction("llvm.dbg.region.end"); 229 Function *Declare = M.getFunction("llvm.dbg.declare"); 230 231 std::vector<Constant*> DeadConstants; 232 233 // Remove all of the calls to the debugger intrinsics, and remove them from 234 // the module. 235 if (FuncStart) { 236 while (!FuncStart->use_empty()) { 237 CallInst *CI = cast<CallInst>(FuncStart->use_back()); 238 Value *Arg = CI->getOperand(1); 239 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 240 CI->eraseFromParent(); 241 if (Arg->use_empty()) 242 if (Constant *C = dyn_cast<Constant>(Arg)) 243 DeadConstants.push_back(C); 244 } 245 FuncStart->eraseFromParent(); 246 } 247 if (StopPoint) { 248 while (!StopPoint->use_empty()) { 249 CallInst *CI = cast<CallInst>(StopPoint->use_back()); 250 Value *Arg = CI->getOperand(3); 251 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 252 CI->eraseFromParent(); 253 if (Arg->use_empty()) 254 if (Constant *C = dyn_cast<Constant>(Arg)) 255 DeadConstants.push_back(C); 256 } 257 StopPoint->eraseFromParent(); 258 } 259 if (RegionStart) { 260 while (!RegionStart->use_empty()) { 261 CallInst *CI = cast<CallInst>(RegionStart->use_back()); 262 Value *Arg = CI->getOperand(1); 263 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 264 CI->eraseFromParent(); 265 if (Arg->use_empty()) 266 if (Constant *C = dyn_cast<Constant>(Arg)) 267 DeadConstants.push_back(C); 268 } 269 RegionStart->eraseFromParent(); 270 } 271 if (RegionEnd) { 272 while (!RegionEnd->use_empty()) { 273 CallInst *CI = cast<CallInst>(RegionEnd->use_back()); 274 Value *Arg = CI->getOperand(1); 275 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 276 CI->eraseFromParent(); 277 if (Arg->use_empty()) 278 if (Constant *C = dyn_cast<Constant>(Arg)) 279 DeadConstants.push_back(C); 280 } 281 RegionEnd->eraseFromParent(); 282 } 283 if (Declare) { 284 while (!Declare->use_empty()) { 285 CallInst *CI = cast<CallInst>(Declare->use_back()); 286 Value *Arg1 = CI->getOperand(1); 287 Value *Arg2 = CI->getOperand(2); 288 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 289 CI->eraseFromParent(); 290 if (Arg1->use_empty()) { 291 if (Constant *C = dyn_cast<Constant>(Arg1)) 292 DeadConstants.push_back(C); 293 else 294 RecursivelyDeleteTriviallyDeadInstructions(Arg1); 295 } 296 if (Arg2->use_empty()) 297 if (Constant *C = dyn_cast<Constant>(Arg2)) 298 DeadConstants.push_back(C); 299 } 300 Declare->eraseFromParent(); 301 } 302 303 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce 304 // but since we are removing all debug information, make them internal now. 305 // FIXME: Use private linkage maybe? 306 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units")) 307 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) 308 GV->setLinkage(GlobalValue::InternalLinkage); 309 310 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms")) 311 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) 312 GV->setLinkage(GlobalValue::InternalLinkage); 313 314 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables")) 315 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) 316 GV->setLinkage(GlobalValue::InternalLinkage); 317 318 // Delete all dbg variables. 319 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 320 I != E; ++I) { 321 GlobalVariable *GV = dyn_cast<GlobalVariable>(I); 322 if (!GV) continue; 323 if (GV->use_empty() && llvmUsedValues.count(I) == 0 324 && (!GV->hasSection() 325 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0)) 326 DeadConstants.push_back(GV); 327 } 328 329 if (DeadConstants.empty()) 330 return false; 331 332 // Delete any internal globals that were only used by the debugger intrinsics. 333 while (!DeadConstants.empty()) { 334 Constant *C = DeadConstants.back(); 335 DeadConstants.pop_back(); 336 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { 337 if (GV->hasLocalLinkage()) 338 RemoveDeadConstant(GV); 339 } 340 else 341 RemoveDeadConstant(C); 342 } 343 344 // Remove all llvm.dbg types. 345 TypeSymbolTable &ST = M.getTypeSymbolTable(); 346 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) { 347 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9)) 348 ST.remove(TI++); 349 else 350 ++TI; 351 } 352 353 return true; 354} 355 356bool StripSymbols::runOnModule(Module &M) { 357 bool Changed = false; 358 Changed |= StripDebugInfo(M); 359 if (!OnlyDebugInfo) 360 Changed |= StripSymbolNames(M, false); 361 return Changed; 362} 363 364bool StripNonDebugSymbols::runOnModule(Module &M) { 365 return StripSymbolNames(M, true); 366} 367 368bool StripDebugDeclare::runOnModule(Module &M) { 369 370 Function *Declare = M.getFunction("llvm.dbg.declare"); 371 std::vector<Constant*> DeadConstants; 372 373 if (Declare) { 374 while (!Declare->use_empty()) { 375 CallInst *CI = cast<CallInst>(Declare->use_back()); 376 Value *Arg1 = CI->getOperand(1); 377 Value *Arg2 = CI->getOperand(2); 378 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 379 CI->eraseFromParent(); 380 if (Arg1->use_empty()) { 381 if (Constant *C = dyn_cast<Constant>(Arg1)) 382 DeadConstants.push_back(C); 383 else 384 RecursivelyDeleteTriviallyDeadInstructions(Arg1); 385 } 386 if (Arg2->use_empty()) 387 if (Constant *C = dyn_cast<Constant>(Arg2)) 388 DeadConstants.push_back(C); 389 } 390 Declare->eraseFromParent(); 391 } 392 393 // Delete all llvm.dbg.global_variables. 394 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 395 I != E; ++I) { 396 GlobalVariable *GV = dyn_cast<GlobalVariable>(I); 397 if (!GV) continue; 398 if (GV->use_empty() && GV->hasName() 399 && strncmp(GV->getNameStart(), "llvm.dbg.global_variable", 24) == 0) 400 DeadConstants.push_back(GV); 401 } 402 403 while (!DeadConstants.empty()) { 404 Constant *C = DeadConstants.back(); 405 DeadConstants.pop_back(); 406 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { 407 if (GV->hasLocalLinkage()) 408 RemoveDeadConstant(GV); 409 } 410 else 411 RemoveDeadConstant(C); 412 } 413 414 return true; 415} 416