LLVM API Documentation

StripSymbols.cpp
Go to the documentation of this file.
00001 //===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // The StripSymbols transformation implements code stripping. Specifically, it
00011 // can delete:
00012 // 
00013 //   * names for virtual registers
00014 //   * symbols for internal globals and functions
00015 //   * debug information
00016 //
00017 // Note that this transformation makes code much less readable, so it should
00018 // only be used in situations where the 'strip' utility would be used, such as
00019 // reducing code size or making it harder to reverse engineer code.
00020 //
00021 //===----------------------------------------------------------------------===//
00022 
00023 #include "llvm/Transforms/IPO.h"
00024 #include "llvm/ADT/DenseMap.h"
00025 #include "llvm/ADT/SmallPtrSet.h"
00026 #include "llvm/DebugInfo.h"
00027 #include "llvm/IR/Constants.h"
00028 #include "llvm/IR/DerivedTypes.h"
00029 #include "llvm/IR/Instructions.h"
00030 #include "llvm/IR/Module.h"
00031 #include "llvm/IR/TypeFinder.h"
00032 #include "llvm/IR/ValueSymbolTable.h"
00033 #include "llvm/Pass.h"
00034 #include "llvm/Transforms/Utils/Local.h"
00035 using namespace llvm;
00036 
00037 namespace {
00038   class StripSymbols : public ModulePass {
00039     bool OnlyDebugInfo;
00040   public:
00041     static char ID; // Pass identification, replacement for typeid
00042     explicit StripSymbols(bool ODI = false) 
00043       : ModulePass(ID), OnlyDebugInfo(ODI) {
00044         initializeStripSymbolsPass(*PassRegistry::getPassRegistry());
00045       }
00046 
00047     virtual bool runOnModule(Module &M);
00048 
00049     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00050       AU.setPreservesAll();
00051     }
00052   };
00053 
00054   class StripNonDebugSymbols : public ModulePass {
00055   public:
00056     static char ID; // Pass identification, replacement for typeid
00057     explicit StripNonDebugSymbols()
00058       : ModulePass(ID) {
00059         initializeStripNonDebugSymbolsPass(*PassRegistry::getPassRegistry());
00060       }
00061 
00062     virtual bool runOnModule(Module &M);
00063 
00064     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00065       AU.setPreservesAll();
00066     }
00067   };
00068 
00069   class StripDebugDeclare : public ModulePass {
00070   public:
00071     static char ID; // Pass identification, replacement for typeid
00072     explicit StripDebugDeclare()
00073       : ModulePass(ID) {
00074         initializeStripDebugDeclarePass(*PassRegistry::getPassRegistry());
00075       }
00076 
00077     virtual bool runOnModule(Module &M);
00078 
00079     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00080       AU.setPreservesAll();
00081     }
00082   };
00083 
00084   class StripDeadDebugInfo : public ModulePass {
00085   public:
00086     static char ID; // Pass identification, replacement for typeid
00087     explicit StripDeadDebugInfo()
00088       : ModulePass(ID) {
00089         initializeStripDeadDebugInfoPass(*PassRegistry::getPassRegistry());
00090       }
00091 
00092     virtual bool runOnModule(Module &M);
00093 
00094     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00095       AU.setPreservesAll();
00096     }
00097   };
00098 }
00099 
00100 char StripSymbols::ID = 0;
00101 INITIALIZE_PASS(StripSymbols, "strip",
00102                 "Strip all symbols from a module", false, false)
00103 
00104 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
00105   return new StripSymbols(OnlyDebugInfo);
00106 }
00107 
00108 char StripNonDebugSymbols::ID = 0;
00109 INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug",
00110                 "Strip all symbols, except dbg symbols, from a module",
00111                 false, false)
00112 
00113 ModulePass *llvm::createStripNonDebugSymbolsPass() {
00114   return new StripNonDebugSymbols();
00115 }
00116 
00117 char StripDebugDeclare::ID = 0;
00118 INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare",
00119                 "Strip all llvm.dbg.declare intrinsics", false, false)
00120 
00121 ModulePass *llvm::createStripDebugDeclarePass() {
00122   return new StripDebugDeclare();
00123 }
00124 
00125 char StripDeadDebugInfo::ID = 0;
00126 INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info",
00127                 "Strip debug info for unused symbols", false, false)
00128 
00129 ModulePass *llvm::createStripDeadDebugInfoPass() {
00130   return new StripDeadDebugInfo();
00131 }
00132 
00133 /// OnlyUsedBy - Return true if V is only used by Usr.
00134 static bool OnlyUsedBy(Value *V, Value *Usr) {
00135   for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
00136     User *U = *I;
00137     if (U != Usr)
00138       return false;
00139   }
00140   return true;
00141 }
00142 
00143 static void RemoveDeadConstant(Constant *C) {
00144   assert(C->use_empty() && "Constant is not dead!");
00145   SmallPtrSet<Constant*, 4> Operands;
00146   for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
00147     if (OnlyUsedBy(C->getOperand(i), C)) 
00148       Operands.insert(cast<Constant>(C->getOperand(i)));
00149   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
00150     if (!GV->hasLocalLinkage()) return;   // Don't delete non static globals.
00151     GV->eraseFromParent();
00152   }
00153   else if (!isa<Function>(C))
00154     if (isa<CompositeType>(C->getType()))
00155       C->destroyConstant();
00156 
00157   // If the constant referenced anything, see if we can delete it as well.
00158   for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
00159          OE = Operands.end(); OI != OE; ++OI)
00160     RemoveDeadConstant(*OI);
00161 }
00162 
00163 // Strip the symbol table of its names.
00164 //
00165 static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
00166   for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
00167     Value *V = VI->getValue();
00168     ++VI;
00169     if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
00170       if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
00171         // Set name to "", removing from symbol table!
00172         V->setName("");
00173     }
00174   }
00175 }
00176 
00177 // Strip any named types of their names.
00178 static void StripTypeNames(Module &M, bool PreserveDbgInfo) {
00179   TypeFinder StructTypes;
00180   StructTypes.run(M, false);
00181 
00182   for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
00183     StructType *STy = StructTypes[i];
00184     if (STy->isLiteral() || STy->getName().empty()) continue;
00185     
00186     if (PreserveDbgInfo && STy->getName().startswith("llvm.dbg"))
00187       continue;
00188 
00189     STy->setName("");
00190   }
00191 }
00192 
00193 /// Find values that are marked as llvm.used.
00194 static void findUsedValues(GlobalVariable *LLVMUsed,
00195                            SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
00196   if (LLVMUsed == 0) return;
00197   UsedValues.insert(LLVMUsed);
00198 
00199   ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
00200 
00201   for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
00202     if (GlobalValue *GV = 
00203           dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
00204       UsedValues.insert(GV);
00205 }
00206 
00207 /// StripSymbolNames - Strip symbol names.
00208 static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
00209 
00210   SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
00211   findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
00212   findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
00213 
00214   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
00215        I != E; ++I) {
00216     if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
00217       if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
00218         I->setName("");     // Internal symbols can't participate in linkage
00219   }
00220   
00221   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
00222     if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
00223       if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
00224         I->setName("");     // Internal symbols can't participate in linkage
00225     StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
00226   }
00227   
00228   // Remove all names from types.
00229   StripTypeNames(M, PreserveDbgInfo);
00230 
00231   return true;
00232 }
00233 
00234 // StripDebugInfo - Strip debug info in the module if it exists.  
00235 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and 
00236 // llvm.dbg.region.end calls, and any globals they point to if now dead.
00237 static bool StripDebugInfo(Module &M) {
00238 
00239   bool Changed = false;
00240 
00241   // Remove all of the calls to the debugger intrinsics, and remove them from
00242   // the module.
00243   if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
00244     while (!Declare->use_empty()) {
00245       CallInst *CI = cast<CallInst>(Declare->use_back());
00246       CI->eraseFromParent();
00247     }
00248     Declare->eraseFromParent();
00249     Changed = true;
00250   }
00251 
00252   if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
00253     while (!DbgVal->use_empty()) {
00254       CallInst *CI = cast<CallInst>(DbgVal->use_back());
00255       CI->eraseFromParent();
00256     }
00257     DbgVal->eraseFromParent();
00258     Changed = true;
00259   }
00260 
00261   for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
00262          NME = M.named_metadata_end(); NMI != NME;) {
00263     NamedMDNode *NMD = NMI;
00264     ++NMI;
00265     if (NMD->getName().startswith("llvm.dbg.")) {
00266       NMD->eraseFromParent();
00267       Changed = true;
00268     }
00269   }
00270 
00271   for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
00272     for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
00273          ++FI)
00274       for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
00275            ++BI) {
00276         if (!BI->getDebugLoc().isUnknown()) {
00277           Changed = true;
00278           BI->setDebugLoc(DebugLoc());
00279         }
00280       }
00281 
00282   return Changed;
00283 }
00284 
00285 bool StripSymbols::runOnModule(Module &M) {
00286   bool Changed = false;
00287   Changed |= StripDebugInfo(M);
00288   if (!OnlyDebugInfo)
00289     Changed |= StripSymbolNames(M, false);
00290   return Changed;
00291 }
00292 
00293 bool StripNonDebugSymbols::runOnModule(Module &M) {
00294   return StripSymbolNames(M, true);
00295 }
00296 
00297 bool StripDebugDeclare::runOnModule(Module &M) {
00298 
00299   Function *Declare = M.getFunction("llvm.dbg.declare");
00300   std::vector<Constant*> DeadConstants;
00301 
00302   if (Declare) {
00303     while (!Declare->use_empty()) {
00304       CallInst *CI = cast<CallInst>(Declare->use_back());
00305       Value *Arg1 = CI->getArgOperand(0);
00306       Value *Arg2 = CI->getArgOperand(1);
00307       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
00308       CI->eraseFromParent();
00309       if (Arg1->use_empty()) {
00310         if (Constant *C = dyn_cast<Constant>(Arg1)) 
00311           DeadConstants.push_back(C);
00312         else 
00313           RecursivelyDeleteTriviallyDeadInstructions(Arg1);
00314       }
00315       if (Arg2->use_empty())
00316         if (Constant *C = dyn_cast<Constant>(Arg2)) 
00317           DeadConstants.push_back(C);
00318     }
00319     Declare->eraseFromParent();
00320   }
00321 
00322   while (!DeadConstants.empty()) {
00323     Constant *C = DeadConstants.back();
00324     DeadConstants.pop_back();
00325     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
00326       if (GV->hasLocalLinkage())
00327         RemoveDeadConstant(GV);
00328     } else
00329       RemoveDeadConstant(C);
00330   }
00331 
00332   return true;
00333 }
00334 
00335 bool StripDeadDebugInfo::runOnModule(Module &M) {
00336   bool Changed = false;
00337 
00338   // Debugging infomration is encoded in llvm IR using metadata. This is designed
00339   // such a way that debug info for symbols preserved even if symbols are
00340   // optimized away by the optimizer. This special pass removes debug info for 
00341   // such symbols.
00342 
00343   // llvm.dbg.gv keeps track of debug info for global variables.
00344   if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
00345     SmallVector<MDNode *, 8> MDs;
00346     for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
00347       if (DIGlobalVariable(NMD->getOperand(i)).Verify())
00348         MDs.push_back(NMD->getOperand(i));
00349       else
00350         Changed = true;
00351     NMD->eraseFromParent();
00352     NMD = NULL;
00353 
00354     for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
00355            E = MDs.end(); I != E; ++I) {
00356       GlobalVariable *GV = DIGlobalVariable(*I).getGlobal();
00357       if (GV && M.getGlobalVariable(GV->getName(), true)) {
00358         if (!NMD)
00359           NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
00360         NMD->addOperand(*I);
00361       }
00362       else
00363         Changed = true;
00364     }
00365   }
00366 
00367   // llvm.dbg.sp keeps track of debug info for subprograms.
00368   if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) {
00369     SmallVector<MDNode *, 8> MDs;
00370     for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
00371       if (DISubprogram(NMD->getOperand(i)).Verify())
00372         MDs.push_back(NMD->getOperand(i));
00373       else
00374         Changed = true;
00375     NMD->eraseFromParent();
00376     NMD = NULL;
00377 
00378     for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
00379            E = MDs.end(); I != E; ++I) {
00380       bool FnIsLive = false;
00381       if (Function *F = DISubprogram(*I).getFunction())
00382         if (M.getFunction(F->getName()))
00383           FnIsLive = true;
00384       if (FnIsLive) {
00385           if (!NMD)
00386             NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
00387           NMD->addOperand(*I);
00388       } else {
00389         // Remove llvm.dbg.lv.fnname named mdnode which may have been used
00390         // to hold debug info for dead function's local variables.
00391         StringRef FName = DISubprogram(*I).getLinkageName();
00392         if (FName.empty())
00393           FName = DISubprogram(*I).getName();
00394         if (NamedMDNode *LVNMD = M.getNamedMetadata(
00395                 "llvm.dbg.lv." + Function::getRealLinkageName(FName)))
00396           LVNMD->eraseFromParent();
00397       }
00398     }
00399   }
00400 
00401   return Changed;
00402 }