StripSymbols.cpp revision ba58d9ca78372162be346a67d5d413c9353fe9e2
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/ValueSymbolTable.h" 30#include "llvm/TypeSymbolTable.h" 31#include "llvm/Support/Compiler.h" 32#include "llvm/ADT/SmallPtrSet.h" 33using namespace llvm; 34 35namespace { 36 class VISIBILITY_HIDDEN StripSymbols : public ModulePass { 37 bool OnlyDebugInfo; 38 public: 39 static char ID; // Pass identification, replacement for typeid 40 explicit StripSymbols(bool ODI = false) 41 : ModulePass(&ID), OnlyDebugInfo(ODI) {} 42 43 /// StripSymbolNames - Strip symbol names. 44 bool StripSymbolNames(Module &M); 45 46 // StripDebugInfo - Strip debug info in the module if it exists. 47 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and 48 // llvm.dbg.region.end calls, and any globals they point to if now dead. 49 bool StripDebugInfo(Module &M); 50 51 virtual bool runOnModule(Module &M); 52 53 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 54 AU.setPreservesAll(); 55 } 56 }; 57} 58 59char StripSymbols::ID = 0; 60static RegisterPass<StripSymbols> 61X("strip", "Strip all symbols from a module"); 62 63ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) { 64 return new StripSymbols(OnlyDebugInfo); 65} 66 67/// OnlyUsedBy - Return true if V is only used by Usr. 68static bool OnlyUsedBy(Value *V, Value *Usr) { 69 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) { 70 User *U = *I; 71 if (U != Usr) 72 return false; 73 } 74 return true; 75} 76 77static void RemoveDeadConstant(Constant *C) { 78 assert(C->use_empty() && "Constant is not dead!"); 79 SmallPtrSet<Constant *, 4> Operands; 80 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) 81 if (isa<DerivedType>(C->getOperand(i)->getType()) && 82 OnlyUsedBy(C->getOperand(i), C)) 83 Operands.insert(C->getOperand(i)); 84 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { 85 if (!GV->hasInternalLinkage()) return; // Don't delete non static globals. 86 GV->eraseFromParent(); 87 } 88 else if (!isa<Function>(C)) 89 C->destroyConstant(); 90 91 // If the constant referenced anything, see if we can delete it as well. 92 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(), 93 OE = Operands.end(); OI != OE; ++OI) 94 RemoveDeadConstant(*OI); 95} 96 97// Strip the symbol table of its names. 98// 99static void StripSymtab(ValueSymbolTable &ST) { 100 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) { 101 Value *V = VI->getValue(); 102 ++VI; 103 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) { 104 // Set name to "", removing from symbol table! 105 V->setName(""); 106 } 107 } 108} 109 110bool StripSymbols::runOnModule(Module &M) { 111 bool Changed = false; 112 Changed |= StripDebugInfo(M); 113 Changed |= StripSymbolNames(M); 114 return Changed; 115} 116 117 118// Strip the symbol table of its names. 119static void StripTypeSymtab(TypeSymbolTable &ST) { 120 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) 121 ST.remove(TI++); 122} 123 124/// StripSymbolNames - Strip symbol names. 125bool StripSymbols::StripSymbolNames(Module &M) { 126 127 if (OnlyDebugInfo) 128 return false; 129 130 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues; 131 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) { 132 llvmUsedValues.insert(LLVMUsed); 133 // Collect values that are preserved as per explicit request. 134 // llvm.used is used to list these values. 135 if (ConstantArray *Inits = 136 dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) { 137 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) { 138 if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i))) 139 llvmUsedValues.insert(GV); 140 else if (ConstantExpr *CE = 141 dyn_cast<ConstantExpr>(Inits->getOperand(i))) 142 if (CE->getOpcode() == Instruction::BitCast) 143 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0))) 144 llvmUsedValues.insert(GV); 145 } 146 } 147 } 148 149 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 150 I != E; ++I) { 151 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0) 152 I->setName(""); // Internal symbols can't participate in linkage 153 } 154 155 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { 156 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0) 157 I->setName(""); // Internal symbols can't participate in linkage 158 StripSymtab(I->getValueSymbolTable()); 159 } 160 161 // Remove all names from types. 162 StripTypeSymtab(M.getTypeSymbolTable()); 163 164 return true; 165} 166 167// StripDebugInfo - Strip debug info in the module if it exists. 168// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and 169// llvm.dbg.region.end calls, and any globals they point to if now dead. 170bool StripSymbols::StripDebugInfo(Module &M) { 171 172 Function *FuncStart = M.getFunction("llvm.dbg.func.start"); 173 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint"); 174 Function *RegionStart = M.getFunction("llvm.dbg.region.start"); 175 Function *RegionEnd = M.getFunction("llvm.dbg.region.end"); 176 Function *Declare = M.getFunction("llvm.dbg.declare"); 177 178 std::vector<GlobalVariable*> DeadGlobals; 179 180 // Remove all of the calls to the debugger intrinsics, and remove them from 181 // the module. 182 if (FuncStart) { 183 while (!FuncStart->use_empty()) { 184 CallInst *CI = cast<CallInst>(FuncStart->use_back()); 185 Value *Arg = CI->getOperand(1); 186 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 187 CI->eraseFromParent(); 188 if (Arg->use_empty()) 189 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg)) 190 DeadGlobals.push_back(GV); 191 } 192 FuncStart->eraseFromParent(); 193 } 194 if (StopPoint) { 195 while (!StopPoint->use_empty()) { 196 CallInst *CI = cast<CallInst>(StopPoint->use_back()); 197 Value *Arg = CI->getOperand(3); 198 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 199 CI->eraseFromParent(); 200 if (Arg->use_empty()) 201 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg)) 202 DeadGlobals.push_back(GV); 203 } 204 StopPoint->eraseFromParent(); 205 } 206 if (RegionStart) { 207 while (!RegionStart->use_empty()) { 208 CallInst *CI = cast<CallInst>(RegionStart->use_back()); 209 Value *Arg = CI->getOperand(1); 210 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 211 CI->eraseFromParent(); 212 if (Arg->use_empty()) 213 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg)) 214 DeadGlobals.push_back(GV); 215 } 216 RegionStart->eraseFromParent(); 217 } 218 if (RegionEnd) { 219 while (!RegionEnd->use_empty()) { 220 CallInst *CI = cast<CallInst>(RegionEnd->use_back()); 221 Value *Arg = CI->getOperand(1); 222 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 223 CI->eraseFromParent(); 224 if (Arg->use_empty()) 225 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg)) 226 DeadGlobals.push_back(GV); 227 } 228 RegionEnd->eraseFromParent(); 229 } 230 if (Declare) { 231 while (!Declare->use_empty()) { 232 CallInst *CI = cast<CallInst>(Declare->use_back()); 233 Value *Arg = CI->getOperand(2); 234 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); 235 CI->eraseFromParent(); 236 if (Arg->use_empty()) 237 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg)) 238 DeadGlobals.push_back(GV); 239 } 240 Declare->eraseFromParent(); 241 } 242 243 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce 244 // but since we are removing all debug information, make them internal now. 245 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units")) 246 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) 247 GV->setLinkage(GlobalValue::InternalLinkage); 248 249 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms")) 250 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) 251 GV->setLinkage(GlobalValue::InternalLinkage); 252 253 // Delete all dbg variables. 254 const Type *DbgVTy = M.getTypeByName("llvm.dbg.variable.type"); 255 const Type *DbgGVTy = M.getTypeByName("llvm.dbg.global_variable.type"); 256 if (DbgVTy || DbgGVTy) 257 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 258 I != E; ++I) 259 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(I)) 260 if (GV->hasName() && GV->use_empty() 261 && !strncmp(GV->getNameStart(), "llvm.dbg", 8) 262 && (GV->getType()->getElementType() == DbgVTy 263 || GV->getType()->getElementType() == DbgGVTy)) 264 DeadGlobals.push_back(GV); 265 266 if (DeadGlobals.empty()) 267 return false; 268 269 // Delete any internal globals that were only used by the debugger intrinsics. 270 while (!DeadGlobals.empty()) { 271 GlobalVariable *GV = DeadGlobals.back(); 272 DeadGlobals.pop_back(); 273 if (GV->hasInternalLinkage()) 274 RemoveDeadConstant(GV); 275 } 276 277 // Remove all llvm.dbg types. 278 TypeSymbolTable &ST = M.getTypeSymbolTable(); 279 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) { 280 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9)) 281 ST.remove(TI++); 282 else 283 ++TI; 284 } 285 286 return true; 287} 288