Chris Lattner | e8ebcb3 | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 1 | //===- StripSymbols.cpp - Strip symbols and debug info from a module ------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | e8ebcb3 | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | e8ebcb3 | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements stripping symbols out of symbol tables. |
| 11 | // |
| 12 | // Specifically, this allows you to strip all of the symbols out of: |
| 13 | // * All functions in a module |
| 14 | // * All non-essential symbols in a module (all function symbols + all module |
| 15 | // scope symbols) |
| 16 | // * Debug information. |
| 17 | // |
| 18 | // Notice that: |
| 19 | // * This pass makes code much less readable, so it should only be used in |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 20 | // situations where the 'strip' utility would be used (such as reducing |
Chris Lattner | e8ebcb3 | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 21 | // code size, and making it harder to reverse engineer code). |
| 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 26 | #include "llvm/Constants.h" |
| 27 | #include "llvm/DerivedTypes.h" |
| 28 | #include "llvm/Instructions.h" |
Chris Lattner | e8ebcb3 | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 29 | #include "llvm/Module.h" |
Chris Lattner | e8ebcb3 | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 30 | #include "llvm/Pass.h" |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 31 | #include "llvm/ValueSymbolTable.h" |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 32 | #include "llvm/TypeSymbolTable.h" |
Chris Lattner | e8ebcb3 | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
| 35 | namespace { |
| 36 | class StripSymbols : public ModulePass { |
| 37 | bool OnlyDebugInfo; |
| 38 | public: |
| 39 | StripSymbols(bool ODI = false) : OnlyDebugInfo(ODI) {} |
| 40 | |
| 41 | virtual bool runOnModule(Module &M); |
| 42 | |
| 43 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 44 | AU.setPreservesAll(); |
| 45 | } |
| 46 | }; |
Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 47 | RegisterPass<StripSymbols> X("strip", "Strip all symbols from a module"); |
Chris Lattner | e8ebcb3 | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) { |
| 51 | return new StripSymbols(OnlyDebugInfo); |
| 52 | } |
| 53 | |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 54 | static void RemoveDeadConstant(Constant *C) { |
| 55 | assert(C->use_empty() && "Constant is not dead!"); |
| 56 | std::vector<Constant*> Operands; |
| 57 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) |
| 58 | if (isa<DerivedType>(C->getOperand(i)->getType()) && |
| 59 | C->getOperand(i)->hasOneUse()) |
| 60 | Operands.push_back(C->getOperand(i)); |
| 61 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { |
| 62 | if (!GV->hasInternalLinkage()) return; // Don't delete non static globals. |
| 63 | GV->eraseFromParent(); |
| 64 | } |
| 65 | else if (!isa<Function>(C)) |
| 66 | C->destroyConstant(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 68 | // If the constant referenced anything, see if we can delete it as well. |
| 69 | while (!Operands.empty()) { |
| 70 | RemoveDeadConstant(Operands.back()); |
| 71 | Operands.pop_back(); |
| 72 | } |
| 73 | } |
Chris Lattner | e8ebcb3 | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 74 | |
| 75 | bool StripSymbols::runOnModule(Module &M) { |
| 76 | // If we're not just stripping debug info, strip all symbols from the |
| 77 | // functions and the names from any internal globals. |
| 78 | if (!OnlyDebugInfo) { |
Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 79 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 80 | I != E; ++I) |
Chris Lattner | e8ebcb3 | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 81 | if (I->hasInternalLinkage()) |
| 82 | I->setName(""); // Internal symbols can't participate in linkage |
| 83 | |
| 84 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 85 | if (I->hasInternalLinkage()) |
| 86 | I->setName(""); // Internal symbols can't participate in linkage |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 87 | I->getValueSymbolTable().strip(); |
Chris Lattner | e8ebcb3 | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 88 | } |
Chris Lattner | 6d6084f | 2006-03-15 19:22:41 +0000 | [diff] [blame] | 89 | |
| 90 | // Remove all names from types. |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 91 | M.getTypeSymbolTable().strip(); |
Chris Lattner | e8ebcb3 | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 94 | // Strip debug info in the module if it exists. To do this, we remove |
| 95 | // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and |
| 96 | // any globals they point to if now dead. |
| 97 | Function *FuncStart = M.getNamedFunction("llvm.dbg.func.start"); |
| 98 | Function *StopPoint = M.getNamedFunction("llvm.dbg.stoppoint"); |
Jim Laskey | 8f64426 | 2006-03-23 18:11:33 +0000 | [diff] [blame] | 99 | Function *RegionStart = M.getNamedFunction("llvm.dbg.region.start"); |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 100 | Function *RegionEnd = M.getNamedFunction("llvm.dbg.region.end"); |
Jim Laskey | 8f64426 | 2006-03-23 18:11:33 +0000 | [diff] [blame] | 101 | Function *Declare = M.getNamedFunction("llvm.dbg.declare"); |
| 102 | if (!FuncStart && !StopPoint && !RegionStart && !RegionEnd && !Declare) |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 103 | return true; |
| 104 | |
| 105 | std::vector<GlobalVariable*> DeadGlobals; |
| 106 | |
| 107 | // Remove all of the calls to the debugger intrinsics, and remove them from |
| 108 | // the module. |
| 109 | if (FuncStart) { |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 110 | while (!FuncStart->use_empty()) { |
| 111 | CallInst *CI = cast<CallInst>(FuncStart->use_back()); |
| 112 | Value *Arg = CI->getOperand(1); |
Jim Laskey | 8f64426 | 2006-03-23 18:11:33 +0000 | [diff] [blame] | 113 | assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 114 | CI->eraseFromParent(); |
| 115 | if (Arg->use_empty()) |
| 116 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg)) |
| 117 | DeadGlobals.push_back(GV); |
| 118 | } |
| 119 | FuncStart->eraseFromParent(); |
| 120 | } |
| 121 | if (StopPoint) { |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 122 | while (!StopPoint->use_empty()) { |
| 123 | CallInst *CI = cast<CallInst>(StopPoint->use_back()); |
Jim Laskey | acb6e34 | 2006-03-13 13:07:37 +0000 | [diff] [blame] | 124 | Value *Arg = CI->getOperand(3); |
Jim Laskey | 8f64426 | 2006-03-23 18:11:33 +0000 | [diff] [blame] | 125 | assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 126 | CI->eraseFromParent(); |
| 127 | if (Arg->use_empty()) |
| 128 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg)) |
| 129 | DeadGlobals.push_back(GV); |
| 130 | } |
| 131 | StopPoint->eraseFromParent(); |
| 132 | } |
Jim Laskey | 8f64426 | 2006-03-23 18:11:33 +0000 | [diff] [blame] | 133 | if (RegionStart) { |
| 134 | while (!RegionStart->use_empty()) { |
| 135 | CallInst *CI = cast<CallInst>(RegionStart->use_back()); |
| 136 | Value *Arg = CI->getOperand(1); |
| 137 | assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); |
| 138 | CI->eraseFromParent(); |
| 139 | if (Arg->use_empty()) |
| 140 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg)) |
| 141 | DeadGlobals.push_back(GV); |
| 142 | } |
| 143 | RegionStart->eraseFromParent(); |
| 144 | } |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 145 | if (RegionEnd) { |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 146 | while (!RegionEnd->use_empty()) { |
| 147 | CallInst *CI = cast<CallInst>(RegionEnd->use_back()); |
Jim Laskey | 8f64426 | 2006-03-23 18:11:33 +0000 | [diff] [blame] | 148 | Value *Arg = CI->getOperand(1); |
| 149 | assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 150 | CI->eraseFromParent(); |
Jim Laskey | 8f64426 | 2006-03-23 18:11:33 +0000 | [diff] [blame] | 151 | if (Arg->use_empty()) |
| 152 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg)) |
| 153 | DeadGlobals.push_back(GV); |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 154 | } |
| 155 | RegionEnd->eraseFromParent(); |
| 156 | } |
Jim Laskey | 8f64426 | 2006-03-23 18:11:33 +0000 | [diff] [blame] | 157 | if (Declare) { |
| 158 | while (!Declare->use_empty()) { |
| 159 | CallInst *CI = cast<CallInst>(Declare->use_back()); |
| 160 | Value *Arg = CI->getOperand(2); |
| 161 | assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); |
| 162 | CI->eraseFromParent(); |
| 163 | if (Arg->use_empty()) |
| 164 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg)) |
| 165 | DeadGlobals.push_back(GV); |
| 166 | } |
| 167 | Declare->eraseFromParent(); |
| 168 | } |
Chris Lattner | 9019e5c | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 169 | |
| 170 | // Finally, delete any internal globals that were only used by the debugger |
| 171 | // intrinsics. |
| 172 | while (!DeadGlobals.empty()) { |
| 173 | GlobalVariable *GV = DeadGlobals.back(); |
| 174 | DeadGlobals.pop_back(); |
| 175 | if (GV->hasInternalLinkage()) |
| 176 | RemoveDeadConstant(GV); |
| 177 | } |
| 178 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 179 | return true; |
Chris Lattner | e8ebcb3 | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 180 | } |